shmem-opencode 0.3.37 → 0.3.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +48 -5
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -215,6 +215,42 @@ function userMessage(text: string, cwd: string, sessionID: string) {
|
|
|
215
215
|
setTurnContext(sessionID, text.trim() ? hookContext(shmemHook("user-message", { prompt: text, session_id: sessionID, cwd })) : "")
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
+
// subagentCheck answers, once per session, whether a session is a
|
|
219
|
+
// subagent's. OpenCode's task tool starts a child session (one with a
|
|
220
|
+
// parentID) whose user turns the parent agent writes, so they are not the
|
|
221
|
+
// person's words. A failed lookup counts as top-level.
|
|
222
|
+
function subagentCheck(parentOf: (sessionID: string) => Promise<string | undefined>) {
|
|
223
|
+
const known = new Map<string, Promise<boolean>>()
|
|
224
|
+
return (sessionID: string): Promise<boolean> => {
|
|
225
|
+
let child = known.get(sessionID)
|
|
226
|
+
if (!child) {
|
|
227
|
+
child = parentOf(sessionID).then(
|
|
228
|
+
(parent) => !!parent,
|
|
229
|
+
() => false,
|
|
230
|
+
)
|
|
231
|
+
known.set(sessionID, child)
|
|
232
|
+
}
|
|
233
|
+
return child
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ATTACHED_FILE matches a file OpenCode 2.x inlined into a prompt's text
|
|
238
|
+
// (`opencode run -f`): its contents, not what the person typed. The importer
|
|
239
|
+
// strips the same blocks (cli/import_opencode.go).
|
|
240
|
+
// OpenCode appends the blocks after the typed text, so the match runs from
|
|
241
|
+
// the first block to the end: a file whose body holds "</file>" is still
|
|
242
|
+
// stripped whole.
|
|
243
|
+
const ATTACHED_FILE = /\s*<file name="[^"]*">[\s\S]*<\/file>\s*$/
|
|
244
|
+
|
|
245
|
+
// promptText is what to capture from a prompt: in a subagent's session, the
|
|
246
|
+
// parent agent's brief, framed as another agent's message so shmem hook
|
|
247
|
+
// saves it as llm (origin=agent), not as the person's words. It still goes
|
|
248
|
+
// through the hook, so the subagent gets shmem's context too.
|
|
249
|
+
function promptText(text: string, subagent: boolean): string {
|
|
250
|
+
if (!subagent || !text.trim()) return text
|
|
251
|
+
return `<agent-message from="opencode-subagent">\n${text}\n</agent-message>`
|
|
252
|
+
}
|
|
253
|
+
|
|
218
254
|
function response(text: string, cwd: string, sessionID: string) {
|
|
219
255
|
if (text.trim()) shmemHookAsync("response", { last_assistant_message: text, session_id: sessionID, cwd })
|
|
220
256
|
}
|
|
@@ -281,7 +317,11 @@ function toolUse(
|
|
|
281
317
|
async function setup(ctx: V2.Context): Promise<V2.Cleanup> {
|
|
282
318
|
const cwd = ctx.location.directory
|
|
283
319
|
await ctx.mcp.transform((mcp) => rootFrom(mcp.get("shmem")))
|
|
284
|
-
|
|
320
|
+
const isSubagent = subagentCheck(async (sessionID) => (await ctx.session.get({ sessionID })).parentID)
|
|
321
|
+
await ctx.session.hook("prompt", async (input) => {
|
|
322
|
+
const text = input.prompt.text.replace(ATTACHED_FILE, "")
|
|
323
|
+
userMessage(promptText(text, await isSubagent(input.sessionID)), cwd, input.sessionID)
|
|
324
|
+
})
|
|
285
325
|
await ctx.session.hook("context", (input) => {
|
|
286
326
|
const extra = contextFor(input.sessionID)
|
|
287
327
|
if (extra) input.system.push({ type: "text", text: extra })
|
|
@@ -369,7 +409,8 @@ const server: V1Plugin = async ({ directory, client }) => {
|
|
|
369
409
|
.catch(() => {})
|
|
370
410
|
}
|
|
371
411
|
}
|
|
372
|
-
|
|
412
|
+
const isSubagent = subagentCheck(async (id) => (await client.session.get({ path: { id } })).data?.parentID)
|
|
413
|
+
return hooksV1(directory, isSubagent)
|
|
373
414
|
}
|
|
374
415
|
|
|
375
416
|
// A 1.x reply is captured from the stable event stream: a text part whose
|
|
@@ -402,17 +443,19 @@ function replyEvents(directory: string) {
|
|
|
402
443
|
}
|
|
403
444
|
}
|
|
404
445
|
|
|
405
|
-
const hooksV1 = (directory: string): Awaited<ReturnType<V1Plugin>> => ({
|
|
446
|
+
const hooksV1 = (directory: string, isSubagent: (sessionID: string) => Promise<boolean>): Awaited<ReturnType<V1Plugin>> => ({
|
|
406
447
|
event: replyEvents(directory),
|
|
407
448
|
|
|
408
449
|
config: async (config) => rootFrom(config.mcp?.shmem),
|
|
409
450
|
|
|
451
|
+
// Synthetic parts are text OpenCode added to the message, such as an
|
|
452
|
+
// attached file's contents, not what the person typed.
|
|
410
453
|
"chat.message": async (input, output) => {
|
|
411
454
|
const text = output.parts
|
|
412
|
-
.filter((p): p is Extract<typeof p, { type: "text" }> => p.type === "text")
|
|
455
|
+
.filter((p): p is Extract<typeof p, { type: "text" }> => p.type === "text" && !p.synthetic)
|
|
413
456
|
.map((p) => p.text)
|
|
414
457
|
.join("")
|
|
415
|
-
userMessage(text, directory, input.sessionID)
|
|
458
|
+
userMessage(promptText(text, await isSubagent(input.sessionID)), directory, input.sessionID)
|
|
416
459
|
},
|
|
417
460
|
|
|
418
461
|
// The 1.x counterpart of 2.x's context hook: the only place a plugin can
|