shmem-opencode 0.3.27 → 0.3.28
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 +69 -10
- package/package.json +2 -1
package/index.ts
CHANGED
|
@@ -1,36 +1,95 @@
|
|
|
1
|
-
import type { Plugin
|
|
1
|
+
import type { Plugin as V1Plugin } from "@opencode-ai/plugin"
|
|
2
|
+
import type { Plugin as V2 } from "@opencode/plugin"
|
|
2
3
|
import { execFileSync } from "child_process"
|
|
3
4
|
|
|
5
|
+
// One module serves both plugin APIs. OpenCode 2.x loads `id` and `setup`;
|
|
6
|
+
// OpenCode 1.x calls `server`. The plugin packages are imported for types
|
|
7
|
+
// only, so neither is needed at runtime.
|
|
8
|
+
|
|
9
|
+
const ID = "shmem-opencode"
|
|
10
|
+
const RESUBSCRIBE_MS = 1_000
|
|
11
|
+
|
|
12
|
+
// shmemRoot is the data root `shmem install` wrote into shmem's MCP entry
|
|
13
|
+
// when it was a custom one. OpenCode runs this plugin with its own
|
|
14
|
+
// environment, which lacks a SHMEM_ROOT set only for install, so the hooks
|
|
15
|
+
// take the root from the config instead. It is passed as an environment
|
|
16
|
+
// variable rather than `--root`, which older shmem binaries reject.
|
|
17
|
+
let shmemRoot: string | undefined
|
|
18
|
+
|
|
19
|
+
function rootFrom(entry: { type?: string; environment?: Record<string, string> } | undefined) {
|
|
20
|
+
if (entry?.type === "local") {
|
|
21
|
+
shmemRoot = entry.environment?.SHMEM_ROOT || undefined
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
4
25
|
function shmemHook(subcommand: string, payload: Record<string, string>): void {
|
|
5
26
|
try {
|
|
6
27
|
execFileSync("shmem", ["hook", subcommand], {
|
|
7
28
|
input: JSON.stringify(payload),
|
|
8
29
|
timeout: 10_000,
|
|
9
30
|
stdio: ["pipe", "ignore", "ignore"],
|
|
31
|
+
env: shmemRoot && !process.env.SHMEM_ROOT ? { ...process.env, SHMEM_ROOT: shmemRoot } : undefined,
|
|
10
32
|
})
|
|
11
33
|
} catch {
|
|
12
34
|
// hook failures must not surface to the user
|
|
13
35
|
}
|
|
14
36
|
}
|
|
15
37
|
|
|
16
|
-
|
|
38
|
+
function userMessage(text: string, cwd: string) {
|
|
39
|
+
if (text.length >= 10) shmemHook("user-message", { prompt: text, cwd })
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function response(text: string, cwd: string) {
|
|
43
|
+
if (text.length >= 20) shmemHook("response", { last_assistant_message: text, cwd })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// OpenCode 2.x.
|
|
47
|
+
async function setup(ctx: V2.Context): Promise<V2.Cleanup> {
|
|
48
|
+
const cwd = ctx.location.directory
|
|
49
|
+
await ctx.mcp.transform((mcp) => rootFrom(mcp.get("shmem")))
|
|
50
|
+
await ctx.session.hook("prompt", (input) => userMessage(input.prompt.text, cwd))
|
|
51
|
+
|
|
52
|
+
// A reply's text arrives whole in session.text.ended, the 2.x counterpart
|
|
53
|
+
// of 1.x's experimental.text.complete.
|
|
54
|
+
// The stream can error or end while OpenCode keeps running; subscribe again
|
|
55
|
+
// after a pause, so replies don't stop being captured until a restart.
|
|
56
|
+
// Only cleanup (the abort) ends it. Errors never reach OpenCode.
|
|
57
|
+
const controller = new AbortController()
|
|
58
|
+
const { signal } = controller
|
|
59
|
+
void (async () => {
|
|
60
|
+
while (!signal.aborted) {
|
|
61
|
+
try {
|
|
62
|
+
for await (const event of ctx.event.subscribe({ types: ["session.text.ended"], signal })) {
|
|
63
|
+
if (event.type === "session.text.ended") response(event.data.text, cwd)
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
// fall through to resubscribe
|
|
67
|
+
}
|
|
68
|
+
if (!signal.aborted) await new Promise((resolve) => setTimeout(resolve, RESUBSCRIBE_MS))
|
|
69
|
+
}
|
|
70
|
+
})()
|
|
71
|
+
return () => controller.abort()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// OpenCode 1.x.
|
|
75
|
+
const server: V1Plugin = async ({ directory }) => ({
|
|
76
|
+
config: async (config) => rootFrom(config.mcp?.shmem),
|
|
77
|
+
|
|
17
78
|
"chat.message": async (_input, output) => {
|
|
18
79
|
const text = output.parts
|
|
19
80
|
.filter((p): p is Extract<typeof p, { type: "text" }> => p.type === "text")
|
|
20
81
|
.map((p) => p.text)
|
|
21
82
|
.join("")
|
|
22
|
-
|
|
23
|
-
shmemHook("user-message", { prompt: text, cwd: directory })
|
|
24
|
-
}
|
|
83
|
+
userMessage(text, directory)
|
|
25
84
|
},
|
|
26
85
|
|
|
27
86
|
"experimental.text.complete": async (_input, output) => {
|
|
28
|
-
|
|
29
|
-
shmemHook("response", { last_assistant_message: output.text, cwd: directory })
|
|
30
|
-
}
|
|
87
|
+
response(output.text, directory)
|
|
31
88
|
},
|
|
32
89
|
})
|
|
33
90
|
|
|
34
91
|
export default {
|
|
35
|
-
|
|
36
|
-
|
|
92
|
+
id: ID,
|
|
93
|
+
setup,
|
|
94
|
+
server,
|
|
95
|
+
} satisfies V2.Plugin & { server: V1Plugin }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shmem-opencode",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.28",
|
|
4
4
|
"description": "shmem memory hooks for OpenCode",
|
|
5
5
|
"main": "index.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@opencode-ai/plugin": "^1.17.13",
|
|
14
|
+
"@opencode/plugin": "^2.2.7",
|
|
14
15
|
"@types/node": "^26.1.0",
|
|
15
16
|
"typescript": "^5.0.0"
|
|
16
17
|
},
|