-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavior-adjustment-plugin.js
More file actions
executable file
·333 lines (279 loc) · 12.1 KB
/
behavior-adjustment-plugin.js
File metadata and controls
executable file
·333 lines (279 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Behavior Adjustment Plugin
*
* Enhances technical collaboration by injecting contextual behavioral reminders
* based on conversation content. Supports type-based template deduplication,
* adaptive model parameter adjustment, and configurable injection frequencies.
*
* CONDITIONAL LOGGING: Logs behavioral adjustment events when logging is enabled
*/
import { promises as fs } from "fs";
import { join } from "path";
// LOGGING CONFIGURATION
const LOG_FILE_PATH = join(process.env.HOME || "", ".config/opencode/behavior-adjustment.log");
// LOGGING FUNCTION - only active when config.logging is true
async function logInjectionEvent(logData, loggingEnabled) {
if (!loggingEnabled) return;
try {
const logEntry =
JSON.stringify({
timestamp: new Date().toISOString(),
...logData,
}) + "\n";
await fs.appendFile(LOG_FILE_PATH, logEntry);
} catch (error) {
console.warn("Behavior Adjustment Plugin: Failed to write log entry:", error.message);
}
}
// Load configuration with project-level precedence over user-level settings
async function loadConfig() {
// Helper to load and parse JSON configuration files
async function loadConfigFile(path) {
try {
const content = await fs.readFile(path, "utf8");
return JSON.parse(content);
} catch (e) {
return null; // File doesn't exist or can't be read
}
}
let finalConfig = null;
// 1. Load user-level config (base layer)
const userConfigPaths = [join(process.env.HOME, ".config/opencode/behavior-config.json")];
for (const path of userConfigPaths) {
const config = await loadConfigFile(path);
if (config) {
finalConfig = config;
break; // Use first found user config
}
}
// 2. Load project-level config (takes precedence)
const projectConfigPaths = [join(process.cwd(), ".opencode/behavior-config.json")];
for (const path of projectConfigPaths) {
const config = await loadConfigFile(path);
if (config) {
finalConfig = { ...finalConfig, ...config };
break; // Use first found project config
}
}
// Return null if no config found - caller will handle gracefully
if (!finalConfig) {
console.log("Behavior Adjustment Plugin: No configuration file found. Plugin disabled.");
return null;
}
// Validate that required structure exists
if (!finalConfig.contexts?.default) {
console.log("Behavior Adjustment Plugin: Invalid configuration - missing 'contexts.default'. Plugin disabled.");
return null;
}
if (!finalConfig.templates || Object.keys(finalConfig.templates).length === 0) {
console.log("Behavior Adjustment Plugin: Invalid configuration - no templates defined. Plugin disabled.");
return null;
}
return finalConfig;
}
// Scan message content for context keywords and return matching contexts
function detectContexts(message, config) {
if (!message) {
return [{ name: "default", ...config.contexts.default }];
}
const lowerMessage = message.toLowerCase();
const matchedContexts = [];
// Check each defined context for keyword matches (excluding default fallback)
for (const [contextName, contextConfig] of Object.entries(config.contexts)) {
if (contextName === "default" || !contextConfig.keywords) {
continue;
}
// Test each keyword - if any match, include this context
for (const keyword of contextConfig.keywords) {
if (lowerMessage.includes(keyword.toLowerCase())) {
matchedContexts.push({
name: contextName,
...contextConfig,
});
break; // Each context added only once per message
}
}
}
return matchedContexts.length > 0 ? matchedContexts : [{ name: "default", ...config.contexts.default }];
}
// Apply type-based deduplication to resolve final template set
// Templates with same 'type' field are mutually exclusive (highest priority wins)
// Different types can combine (e.g., behavior + domain-specific standards)
function resolveContexts(matchedContexts, config) {
if (!matchedContexts.length) {
// This shouldn't happen since detectContexts always returns default context
const defaultContext = config.contexts.default;
return {
templates: [defaultContext.template],
injectionRate: defaultContext.injectionRate,
context: "default",
temperature: defaultContext.temperature,
};
}
const templatesByType = new Map(); // type -> {template, priority, injectionRate, context}
// Process contexts in priority order (highest first)
// All contexts must have priority defined for proper ordering
matchedContexts.sort((a, b) => b.priority - a.priority);
for (const ctx of matchedContexts) {
const templateConfig = config.templates[ctx.template];
if (!templateConfig) continue;
// Only keep the highest priority template for each type
const existing = templatesByType.get(templateConfig.type);
if (!existing || ctx.priority > existing.priority) {
templatesByType.set(templateConfig.type, {
template: ctx.template,
priority: ctx.priority,
injectionRate: ctx.injectionRate,
context: ctx.name,
});
}
}
// Find highest priority context for temperature setting
let temperatureContext = null;
let highestTempPriority = -1;
for (const ctx of matchedContexts) {
if (ctx.temperature !== undefined && ctx.priority > highestTempPriority) {
temperatureContext = ctx;
highestTempPriority = ctx.priority;
}
}
// Build final template selection with highest injection rate
const selectedTemplates = [];
let highestRate = 0;
const contextNames = [];
for (const selection of templatesByType.values()) {
selectedTemplates.push(selection.template);
if (selection.injectionRate > highestRate) {
highestRate = selection.injectionRate;
}
contextNames.push(selection.context);
}
return {
templates: selectedTemplates,
injectionRate: highestRate,
context: contextNames.join("+"),
temperature: temperatureContext?.temperature ?? config.contexts.default.temperature,
};
}
// Build final behavioral reminder from resolved template set
function generateReminder(resolved, config) {
if (!resolved.templates || resolved.templates.length === 0) {
// No templates resolved - skip injection
return null;
}
const reminders = [];
for (const templateName of resolved.templates) {
const templateConfig = config.templates[templateName];
if (!templateConfig) continue;
const prompt = Array.isArray(templateConfig.prompt) ? templateConfig.prompt.join("\n") : templateConfig.prompt;
reminders.push(prompt);
}
if (reminders.length === 0) {
// No valid templates found - skip injection
return null;
}
// Single template: return as-is with context substitution
if (reminders.length === 1) {
return reminders[0].replace("{context}", resolved.context);
}
// Multiple templates: concatenate with context substitution
return reminders.join("\n\n").replace("{context}", resolved.context);
}
export const BehaviorAdjustment = async () => {
const config = await loadConfig();
// If no config found or invalid config, return disabled plugin
if (!config) {
return {
"chat.message": async () => {}, // No-op
"chat.params": async () => {}, // No-op
};
}
return {
/**
* Main injection logic: analyze message content and conditionally inject behavioral reminders
*/
"chat.message": async (_, output) => {
if (!config.enabled) return;
// Safety check for output.parts
if (!output.parts || !Array.isArray(output.parts)) {
output.parts = [];
}
// Prevent duplicate injections into the same message output
const alreadyInjected = output.parts.some((p) => p.synthetic && p.id?.startsWith("behavior-adj-"));
if (alreadyInjected) return;
// Context detection and template resolution
let resolved;
let messageText = "";
if (config.adaptiveMode) {
// Extract user message text for analysis
const textParts = output.parts.filter((p) => p.type === "text" && !p.synthetic);
messageText = textParts.map((p) => p.text || "").join(" ");
const matchedContexts = detectContexts(messageText, config);
resolved = resolveContexts(matchedContexts, config);
} else {
// Fixed behavior when adaptive mode disabled - use default context
const defaultContext = config.contexts.default;
resolved = {
templates: [defaultContext.template],
injectionRate: defaultContext.injectionRate,
context: "default",
temperature: defaultContext.temperature,
};
}
// Probabilistic injection based on context-specific rates
const shouldInject = Math.random() < resolved.injectionRate;
let injectionText = null;
if (shouldInject) {
const reminder = generateReminder(resolved, config);
// Only inject if reminder was successfully generated
if (reminder) {
injectionText = reminder;
output.parts.unshift({
id: `behavior-adj-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`,
type: "text",
text: reminder,
synthetic: true,
messageID: output.message.id,
sessionID: output.message.sessionID,
});
}
}
// CONDITIONAL LOGGING - only logs when config.logging is true
if (config.logging) {
const matchedContexts = config.adaptiveMode ? detectContexts(messageText, config) : [{ name: "default", ...config.contexts.default }];
await logInjectionEvent(
{
sessionId: output.message.sessionID,
messageId: output.message.id,
userMessage: messageText,
detectedContexts: matchedContexts.map((ctx) => ctx.name),
resolvedContext: resolved.context,
injectionRate: resolved.injectionRate,
injectionOccurred: !!injectionText,
injectionText: injectionText,
},
config.logging,
);
}
},
/**
* Apply context-specific model parameters (temperature, etc.)
*/
"chat.params": async (_, output) => {
if (!config.enabled || !config.adaptiveMode) return;
// Safety check for output.parts
if (!output.parts || !Array.isArray(output.parts)) {
return; // Skip parameter adjustment if no parts available
}
// Detect contexts for temperature resolution
const textParts = output.parts.filter((p) => p.type === "text" && !p.synthetic);
const messageText = textParts.map((p) => p.text || "").join(" ");
const matchedContexts = detectContexts(messageText, config);
const resolved = resolveContexts(matchedContexts, config);
// Apply temperature from highest priority context
if (resolved.temperature !== undefined) {
output.temperature = resolved.temperature;
}
},
};
};