@openclaw/opencode-provider 0.0.0 → 2026.8.1-beta.2
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/README.md +15 -2
- package/dist/doctor-contract-api.js +170 -0
- package/dist/index.js +742 -0
- package/dist/media-understanding-provider.js +25 -0
- package/dist/onboard.js +20 -0
- package/dist/provider-catalog.js +1218 -0
- package/dist/provider-discovery.js +14 -0
- package/dist/provider-policy-api.js +37 -0
- package/dist/session-catalog-plugin.js +407 -0
- package/dist/session-catalog-shared.js +12 -0
- package/dist/session-catalog-terminal.js +104 -0
- package/dist/session-catalog.js +409 -0
- package/dist/session-upstream-activity.js +232 -0
- package/dist/stream.js +166 -0
- package/openclaw.plugin.json +685 -0
- package/package.json +43 -8
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { buildStaticOpencodeZenProviderConfig } from "./provider-catalog.js";
|
|
2
|
+
//#region extensions/opencode/provider-discovery.ts
|
|
3
|
+
const opencodeProviderDiscovery = {
|
|
4
|
+
id: "opencode",
|
|
5
|
+
label: "OpenCode Zen",
|
|
6
|
+
docsPath: "/providers/models",
|
|
7
|
+
auth: [],
|
|
8
|
+
staticCatalog: {
|
|
9
|
+
order: "simple",
|
|
10
|
+
run: async () => ({ provider: buildStaticOpencodeZenProviderConfig() })
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { opencodeProviderDiscovery as default };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { resolveClaudeThinkingProfile } from "openclaw/plugin-sdk/provider-model-shared";
|
|
2
|
+
//#region extensions/opencode/provider-policy-api.ts
|
|
3
|
+
const FIXED_REASONING_PROFILE = {
|
|
4
|
+
levels: [{
|
|
5
|
+
id: "off",
|
|
6
|
+
label: "always on"
|
|
7
|
+
}],
|
|
8
|
+
defaultLevel: "off"
|
|
9
|
+
};
|
|
10
|
+
const THINKING_LEVEL_IDS = /* @__PURE__ */ new Set([
|
|
11
|
+
"off",
|
|
12
|
+
"minimal",
|
|
13
|
+
"low",
|
|
14
|
+
"medium",
|
|
15
|
+
"high",
|
|
16
|
+
"xhigh",
|
|
17
|
+
"max"
|
|
18
|
+
]);
|
|
19
|
+
function resolveEffortThinkingProfile(efforts) {
|
|
20
|
+
if (!efforts || efforts.length === 0) return;
|
|
21
|
+
const acceptedLevelIds = ["off", ...efforts.map((effort) => effort === "none" ? "off" : effort)].filter((id) => THINKING_LEVEL_IDS.has(id)).filter((id, index, values) => values.indexOf(id) === index);
|
|
22
|
+
const levels = acceptedLevelIds.map((id) => ({ id }));
|
|
23
|
+
const levelIdSet = new Set(acceptedLevelIds);
|
|
24
|
+
return {
|
|
25
|
+
levels,
|
|
26
|
+
defaultLevel: levelIdSet.has("medium") ? "medium" : levelIdSet.has("high") ? "high" : levelIdSet.has("low") ? "low" : "off"
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function resolveThinkingProfile(params) {
|
|
30
|
+
const modelId = params.modelId.trim().toLowerCase();
|
|
31
|
+
if (modelId.startsWith("claude-")) return resolveClaudeThinkingProfile(modelId);
|
|
32
|
+
const effortProfile = resolveEffortThinkingProfile(params.compat?.supportedReasoningEfforts);
|
|
33
|
+
if (effortProfile) return effortProfile;
|
|
34
|
+
return params.reasoning === true && params.api !== "anthropic-messages" ? FIXED_REASONING_PROFILE : void 0;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { resolveThinkingProfile };
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { OPENCODE_LOCAL_SESSION_HOST_ID, OPENCODE_NODE_INVOKE_TIMEOUT_MS, OPENCODE_SESSIONS_CAPABILITY, OPENCODE_SESSIONS_LIST_COMMAND, OPENCODE_SESSION_ID_PATTERN, OPENCODE_SESSION_READ_COMMAND, OPENCODE_TERMINAL_RESUME_COMMAND, OpenCodeCatalogParamsError } from "./session-catalog-shared.js";
|
|
2
|
+
import { isExactOpenCodeSessionCursor, listLocalOpenCodeSessionPage, readLocalOpenCodeTranscriptPage } from "./session-catalog.js";
|
|
3
|
+
import { createOpenCodeTerminalNodeHostCommand, openOpenCodeCatalogTerminal } from "./session-catalog-terminal.js";
|
|
4
|
+
import { checkOpenCodeUpstreamActivity, linkContinuedOpenCodeSession } from "./session-upstream-activity.js";
|
|
5
|
+
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
6
|
+
import { accessSync, constants, statSync } from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import process from "node:process";
|
|
9
|
+
import { resolveAcpSessionAvailability } from "openclaw/plugin-sdk/acp-runtime";
|
|
10
|
+
import { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
|
|
11
|
+
import { resolveNodeHostExecutable } from "openclaw/plugin-sdk/node-host";
|
|
12
|
+
import { createSessionCatalogAdoptionCoordinator, importSessionCatalogHistory, listAdoptedSessionCatalogSessions, sessionCatalogAdoptedSessionKey, sessionCatalogAdoptedSourceKey } from "openclaw/plugin-sdk/session-catalog";
|
|
13
|
+
//#region extensions/opencode/session-catalog-plugin.ts
|
|
14
|
+
const MAX_HOSTS = 100;
|
|
15
|
+
const TRANSCRIPT_ITEM_TYPES = /* @__PURE__ */ new Set([
|
|
16
|
+
"userMessage",
|
|
17
|
+
"agentMessage",
|
|
18
|
+
"reasoning",
|
|
19
|
+
"toolCall",
|
|
20
|
+
"toolResult",
|
|
21
|
+
"other"
|
|
22
|
+
]);
|
|
23
|
+
const ACPX_BACKEND_ID = "acpx";
|
|
24
|
+
const OPENCODE_ACP_AGENT_ID = "opencode";
|
|
25
|
+
const OPENCODE_ADOPTED_SESSION_KEY_PREFIX = "plugin:opencode:catalog-adopt:";
|
|
26
|
+
const continueAdoption = createSessionCatalogAdoptionCoordinator();
|
|
27
|
+
function isOptionalString(value) {
|
|
28
|
+
return value === void 0 || typeof value === "string";
|
|
29
|
+
}
|
|
30
|
+
function isOptionalNumber(value) {
|
|
31
|
+
return value === void 0 || typeof value === "number";
|
|
32
|
+
}
|
|
33
|
+
function isNodeSession(value) {
|
|
34
|
+
return isRecord(value) && typeof value.threadId === "string" && OPENCODE_SESSION_ID_PATTERN.test(value.threadId) && typeof value.status === "string" && value.status.length > 0 && typeof value.archived === "boolean" && typeof value.canContinue === "boolean" && typeof value.canArchive === "boolean" && isOptionalString(value.name) && isOptionalString(value.cwd) && isOptionalString(value.source) && isOptionalString(value.modelProvider) && isOptionalString(value.cliVersion) && isOptionalString(value.gitBranch) && isOptionalString(value.sessionKey) && isOptionalNumber(value.createdAt) && isOptionalNumber(value.updatedAt) && isOptionalNumber(value.recencyAt);
|
|
35
|
+
}
|
|
36
|
+
function isNodeTranscriptItem(value) {
|
|
37
|
+
return isRecord(value) && typeof value.type === "string" && TRANSCRIPT_ITEM_TYPES.has(value.type) && isOptionalString(value.id) && isOptionalString(value.text) && isOptionalString(value.timestamp) && isOptionalString(value.model) && (value.truncated === void 0 || typeof value.truncated === "boolean");
|
|
38
|
+
}
|
|
39
|
+
function executableOnPath(command, env) {
|
|
40
|
+
const pathValue = env.PATH ?? env.Path ?? "";
|
|
41
|
+
const delimiter = process.platform === "win32" ? ";" : path.delimiter;
|
|
42
|
+
const extensions = process.platform === "win32" ? (env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";") : [""];
|
|
43
|
+
for (const directory of pathValue.split(delimiter)) for (const extension of extensions) {
|
|
44
|
+
if (!directory.trim()) continue;
|
|
45
|
+
const candidate = path.join(directory, `${command}${extension}`);
|
|
46
|
+
try {
|
|
47
|
+
if (!statSync(candidate).isFile()) continue;
|
|
48
|
+
if (process.platform !== "win32") accessSync(candidate, constants.X_OK);
|
|
49
|
+
return true;
|
|
50
|
+
} catch {}
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
function parseNodeParams(paramsJSON) {
|
|
55
|
+
if (!paramsJSON) return;
|
|
56
|
+
try {
|
|
57
|
+
return JSON.parse(paramsJSON);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
throw new Error("OpenCode session parameters must be valid JSON", { cause: error });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function fullConfigCatalogEnabled(config) {
|
|
63
|
+
if (!isRecord(config) || !isRecord(config.plugins) || !isRecord(config.plugins.entries)) return true;
|
|
64
|
+
const entry = config.plugins.entries.opencode;
|
|
65
|
+
if (!isRecord(entry) || !isRecord(entry.config) || !isRecord(entry.config.sessionCatalog)) return true;
|
|
66
|
+
return entry.config.sessionCatalog.enabled !== false;
|
|
67
|
+
}
|
|
68
|
+
function isOpenCodeSessionCatalogEnabled(pluginConfig) {
|
|
69
|
+
return !isRecord(pluginConfig) || !isRecord(pluginConfig.sessionCatalog) || pluginConfig.sessionCatalog.enabled !== false;
|
|
70
|
+
}
|
|
71
|
+
function openCodeUsesProcessHomeFallback(env) {
|
|
72
|
+
return !env.OPENCODE_DB?.trim() && !path.isAbsolute(env.XDG_DATA_HOME?.trim() ?? "");
|
|
73
|
+
}
|
|
74
|
+
function assertOpenCodeLocalAccess(hostId, allowProcessHomeFallback) {
|
|
75
|
+
if (hostId === "gateway" && allowProcessHomeFallback === false && openCodeUsesProcessHomeFallback(process.env)) throw new OpenCodeCatalogParamsError("local OpenCode sessions are unavailable in isolated state");
|
|
76
|
+
}
|
|
77
|
+
function createOpenCodeSessionNodeHostCommands(api) {
|
|
78
|
+
const available = ({ config, env }) => fullConfigCatalogEnabled(config) && executableOnPath("opencode", env);
|
|
79
|
+
return [
|
|
80
|
+
{
|
|
81
|
+
command: OPENCODE_SESSIONS_LIST_COMMAND,
|
|
82
|
+
cap: OPENCODE_SESSIONS_CAPABILITY,
|
|
83
|
+
dangerous: false,
|
|
84
|
+
isAvailable: available,
|
|
85
|
+
handle: async (paramsJSON) => JSON.stringify(await listLocalOpenCodeSessionPage(parseNodeParams(paramsJSON), { configIdentity: currentOpenCodeCatalogConfig(api) }))
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
command: OPENCODE_SESSION_READ_COMMAND,
|
|
89
|
+
cap: OPENCODE_SESSIONS_CAPABILITY,
|
|
90
|
+
dangerous: false,
|
|
91
|
+
isAvailable: available,
|
|
92
|
+
handle: async (paramsJSON) => JSON.stringify(await readLocalOpenCodeTranscriptPage(parseNodeParams(paramsJSON)))
|
|
93
|
+
},
|
|
94
|
+
createOpenCodeTerminalNodeHostCommand(available)
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
function createOpenCodeSessionNodeInvokePolicies() {
|
|
98
|
+
return [{
|
|
99
|
+
commands: [
|
|
100
|
+
OPENCODE_SESSIONS_LIST_COMMAND,
|
|
101
|
+
OPENCODE_SESSION_READ_COMMAND,
|
|
102
|
+
OPENCODE_TERMINAL_RESUME_COMMAND
|
|
103
|
+
],
|
|
104
|
+
defaultPlatforms: [
|
|
105
|
+
"macos",
|
|
106
|
+
"linux",
|
|
107
|
+
"windows"
|
|
108
|
+
],
|
|
109
|
+
handle: (context) => context.command === "opencode.terminal.resume.v1" ? { ok: true } : context.invokeNode()
|
|
110
|
+
}];
|
|
111
|
+
}
|
|
112
|
+
function nodeLabel(node) {
|
|
113
|
+
return node.displayName?.trim() || node.remoteIp?.trim() || node.nodeId;
|
|
114
|
+
}
|
|
115
|
+
function unwrapNodePayload(value) {
|
|
116
|
+
return isRecord(value) && typeof value.payloadJSON === "string" ? JSON.parse(value.payloadJSON) : value;
|
|
117
|
+
}
|
|
118
|
+
function setCatalogCapabilities(page, capabilities) {
|
|
119
|
+
for (const session of page.sessions) {
|
|
120
|
+
session.canContinue = capabilities.canContinue;
|
|
121
|
+
session.canOpenTerminal = capabilities.canOpenTerminal;
|
|
122
|
+
}
|
|
123
|
+
return page;
|
|
124
|
+
}
|
|
125
|
+
function projectOpenCodeAdoptedSessions(page, adopted) {
|
|
126
|
+
return {
|
|
127
|
+
...page,
|
|
128
|
+
sessions: page.sessions.map((session) => {
|
|
129
|
+
const sessionKey = adopted.get(sessionCatalogAdoptedSourceKey(OPENCODE_LOCAL_SESSION_HOST_ID, session.threadId));
|
|
130
|
+
return sessionKey ? {
|
|
131
|
+
...session,
|
|
132
|
+
sessionKey
|
|
133
|
+
} : session;
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
async function listOpenCodeNodeHost(runtime, query, node) {
|
|
138
|
+
const hostId = `node:${node.nodeId}`;
|
|
139
|
+
const common = {
|
|
140
|
+
hostId,
|
|
141
|
+
label: nodeLabel(node),
|
|
142
|
+
kind: "node",
|
|
143
|
+
connected: node.connected === true,
|
|
144
|
+
nodeId: node.nodeId
|
|
145
|
+
};
|
|
146
|
+
if (node.connected !== true) return {
|
|
147
|
+
...common,
|
|
148
|
+
sessions: [],
|
|
149
|
+
error: {
|
|
150
|
+
code: "NODE_OFFLINE",
|
|
151
|
+
message: "Paired node is offline"
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
try {
|
|
155
|
+
const cursor = query.cursors?.[hostId];
|
|
156
|
+
if (cursor !== void 0 && !isExactOpenCodeSessionCursor(cursor)) throw new Error("cursor is invalid");
|
|
157
|
+
const page = parseNodeSessionPage(unwrapNodePayload(await runtime.nodes.invoke({
|
|
158
|
+
nodeId: node.nodeId,
|
|
159
|
+
command: OPENCODE_SESSIONS_LIST_COMMAND,
|
|
160
|
+
params: {
|
|
161
|
+
...query.limitPerHost ? { limit: query.limitPerHost } : {},
|
|
162
|
+
...query.search ? { searchTerm: query.search } : {},
|
|
163
|
+
...cursor !== void 0 ? { cursor } : {}
|
|
164
|
+
},
|
|
165
|
+
timeoutMs: OPENCODE_NODE_INVOKE_TIMEOUT_MS,
|
|
166
|
+
scopes: ["operator.write"]
|
|
167
|
+
})));
|
|
168
|
+
const canOpenTerminal = (node.invocableCommands ?? node.commands)?.includes(OPENCODE_TERMINAL_RESUME_COMMAND) === true;
|
|
169
|
+
return {
|
|
170
|
+
...common,
|
|
171
|
+
...setCatalogCapabilities(page, {
|
|
172
|
+
canContinue: false,
|
|
173
|
+
canOpenTerminal
|
|
174
|
+
})
|
|
175
|
+
};
|
|
176
|
+
} catch {
|
|
177
|
+
return {
|
|
178
|
+
...common,
|
|
179
|
+
sessions: [],
|
|
180
|
+
error: {
|
|
181
|
+
code: "NODE_INVOKE_FAILED",
|
|
182
|
+
message: "Paired node OpenCode sessions are unavailable"
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function parseNodeSessionPage(value) {
|
|
188
|
+
if (!isRecord(value) || !Array.isArray(value.sessions) || value.sessions.length > 100) throw new Error("OpenCode node returned an invalid session page");
|
|
189
|
+
if (!value.sessions.every(isNodeSession)) throw new Error("OpenCode node returned an invalid session page");
|
|
190
|
+
const sessions = value.sessions;
|
|
191
|
+
const nextCursor = value.nextCursor;
|
|
192
|
+
if (nextCursor !== void 0 && !isExactOpenCodeSessionCursor(nextCursor)) throw new Error("OpenCode node returned an invalid cursor");
|
|
193
|
+
return {
|
|
194
|
+
sessions,
|
|
195
|
+
...nextCursor !== void 0 ? { nextCursor } : {}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function parseNodeTranscriptPage(value, threadId) {
|
|
199
|
+
if (!isRecord(value) || value.threadId !== threadId || !Array.isArray(value.items) || value.items.length > 100 || !value.items.every(isNodeTranscriptItem)) throw new Error("OpenCode node returned an invalid transcript page");
|
|
200
|
+
const nextCursor = value.nextCursor;
|
|
201
|
+
if (nextCursor !== void 0 && !isExactOpenCodeSessionCursor(nextCursor)) throw new Error("OpenCode node returned an invalid cursor");
|
|
202
|
+
return {
|
|
203
|
+
hostId: OPENCODE_LOCAL_SESSION_HOST_ID,
|
|
204
|
+
threadId,
|
|
205
|
+
items: value.items,
|
|
206
|
+
...nextCursor !== void 0 ? { nextCursor } : {}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
async function listOpenCodeHosts(api, query) {
|
|
210
|
+
const runtime = api.runtime;
|
|
211
|
+
const config = currentOpenCodeCatalogConfig(api);
|
|
212
|
+
const canContinue = resolveAcpSessionAvailability({
|
|
213
|
+
config,
|
|
214
|
+
backendId: ACPX_BACKEND_ID,
|
|
215
|
+
agentId: OPENCODE_ACP_AGENT_ID
|
|
216
|
+
}).available;
|
|
217
|
+
const adopted = query.sessionEntries ? listAdoptedOpenCodeSessions(api, query.sessionEntries) : /* @__PURE__ */ new Map();
|
|
218
|
+
const requested = query.hostIds ? new Set(query.hostIds) : void 0;
|
|
219
|
+
const hosts = [];
|
|
220
|
+
if ((!requested || requested.has("gateway")) && (query.allowProcessHomeFallback !== false || !openCodeUsesProcessHomeFallback(process.env)) && resolveNodeHostExecutable("opencode", {
|
|
221
|
+
env: process.env,
|
|
222
|
+
pathEnv: process.env.PATH ?? "",
|
|
223
|
+
strategy: "fallback"
|
|
224
|
+
})) try {
|
|
225
|
+
hosts.push({
|
|
226
|
+
hostId: OPENCODE_LOCAL_SESSION_HOST_ID,
|
|
227
|
+
label: "Local OpenCode",
|
|
228
|
+
kind: "gateway",
|
|
229
|
+
connected: true,
|
|
230
|
+
...await listLocalOpenCodeSessionPage({
|
|
231
|
+
limit: query.limitPerHost,
|
|
232
|
+
...query.search ? { searchTerm: query.search } : {},
|
|
233
|
+
cursor: query.cursors?.[OPENCODE_LOCAL_SESSION_HOST_ID]
|
|
234
|
+
}, { configIdentity: config }).then((page) => projectOpenCodeAdoptedSessions(setCatalogCapabilities(page, {
|
|
235
|
+
canContinue,
|
|
236
|
+
canOpenTerminal: true
|
|
237
|
+
}), adopted))
|
|
238
|
+
});
|
|
239
|
+
} catch {
|
|
240
|
+
hosts.push({
|
|
241
|
+
hostId: OPENCODE_LOCAL_SESSION_HOST_ID,
|
|
242
|
+
label: "Local OpenCode",
|
|
243
|
+
kind: "gateway",
|
|
244
|
+
connected: true,
|
|
245
|
+
sessions: [],
|
|
246
|
+
error: {
|
|
247
|
+
code: "LOCAL_READ_FAILED",
|
|
248
|
+
message: "Local OpenCode sessions are unavailable"
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
let nodes;
|
|
253
|
+
try {
|
|
254
|
+
nodes = (await (query.listNodes?.() ?? runtime.nodes.list())).nodes;
|
|
255
|
+
} catch {
|
|
256
|
+
return hosts;
|
|
257
|
+
}
|
|
258
|
+
const eligible = nodes.filter((node) => node.commands?.includes("opencode.sessions.list.v1") && (!requested || requested.has(`node:${node.nodeId}`))).toSorted((left, right) => nodeLabel(left).localeCompare(nodeLabel(right))).slice(0, MAX_HOSTS - hosts.length);
|
|
259
|
+
const nodeHosts = await Promise.all(eligible.map((node) => listOpenCodeNodeHost(runtime, query, node)));
|
|
260
|
+
return [...hosts, ...nodeHosts];
|
|
261
|
+
}
|
|
262
|
+
async function readOpenCodeTranscript(runtime, request) {
|
|
263
|
+
const cursor = request.cursor;
|
|
264
|
+
if (cursor !== void 0 && !isExactOpenCodeSessionCursor(cursor)) throw new Error("cursor is invalid");
|
|
265
|
+
if (request.hostId === "gateway") {
|
|
266
|
+
assertOpenCodeLocalAccess(request.hostId, request.allowProcessHomeFallback);
|
|
267
|
+
return await readLocalOpenCodeTranscriptPage({
|
|
268
|
+
threadId: request.threadId,
|
|
269
|
+
...request.limit ? { limit: request.limit } : {},
|
|
270
|
+
...cursor !== void 0 ? { cursor } : {}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
if (!request.hostId.startsWith("node:")) throw new Error("hostId is invalid");
|
|
274
|
+
const nodeId = request.hostId.slice(5);
|
|
275
|
+
const node = (await runtime.nodes.list()).nodes.find((candidate) => candidate.nodeId === nodeId && candidate.connected === true && candidate.commands?.includes("opencode.sessions.read.v1"));
|
|
276
|
+
if (!node) throw new Error("paired-node OpenCode session host is unavailable");
|
|
277
|
+
return {
|
|
278
|
+
...parseNodeTranscriptPage(unwrapNodePayload(await runtime.nodes.invoke({
|
|
279
|
+
nodeId,
|
|
280
|
+
command: OPENCODE_SESSION_READ_COMMAND,
|
|
281
|
+
params: {
|
|
282
|
+
threadId: request.threadId,
|
|
283
|
+
...request.limit ? { limit: request.limit } : {},
|
|
284
|
+
...cursor !== void 0 ? { cursor } : {}
|
|
285
|
+
},
|
|
286
|
+
timeoutMs: OPENCODE_NODE_INVOKE_TIMEOUT_MS,
|
|
287
|
+
scopes: ["operator.write"]
|
|
288
|
+
})), request.threadId),
|
|
289
|
+
hostId: request.hostId,
|
|
290
|
+
label: nodeLabel(node)
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function currentOpenCodeCatalogConfig(api) {
|
|
294
|
+
return api.runtime.config?.current?.() ?? api.config ?? {};
|
|
295
|
+
}
|
|
296
|
+
function listAdoptedOpenCodeSessions(api, sessionEntries) {
|
|
297
|
+
return listAdoptedSessionCatalogSessions({
|
|
298
|
+
config: currentOpenCodeCatalogConfig(api),
|
|
299
|
+
pluginId: api.id,
|
|
300
|
+
runtime: api.runtime,
|
|
301
|
+
sessionEntries,
|
|
302
|
+
sourceFromEntry: (entry) => {
|
|
303
|
+
const opencode = isRecord(entry.pluginExtensions?.opencode) ? entry.pluginExtensions.opencode : void 0;
|
|
304
|
+
const marker = opencode && isRecord(opencode.sessionCatalog) ? opencode.sessionCatalog : void 0;
|
|
305
|
+
return marker && typeof marker.sourceThreadId === "string" ? {
|
|
306
|
+
hostId: OPENCODE_LOCAL_SESSION_HOST_ID,
|
|
307
|
+
threadId: marker.sourceThreadId
|
|
308
|
+
} : void 0;
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
async function continueOpenCodeSession(api, hostId, threadId) {
|
|
313
|
+
if (hostId.startsWith("node:")) throw new OpenCodeCatalogParamsError("paired-node OpenCode session rows are view-only");
|
|
314
|
+
if (hostId !== "gateway") throw new OpenCodeCatalogParamsError("OpenCode session catalog hostId is invalid");
|
|
315
|
+
const availability = resolveAcpSessionAvailability({
|
|
316
|
+
config: currentOpenCodeCatalogConfig(api),
|
|
317
|
+
backendId: ACPX_BACKEND_ID,
|
|
318
|
+
agentId: OPENCODE_ACP_AGENT_ID
|
|
319
|
+
});
|
|
320
|
+
if (!availability.available) throw new OpenCodeCatalogParamsError(availability.message);
|
|
321
|
+
const sourceKey = sessionCatalogAdoptedSourceKey(hostId, threadId);
|
|
322
|
+
return await continueAdoption({
|
|
323
|
+
sourceKey,
|
|
324
|
+
findExisting: () => listAdoptedOpenCodeSessions(api).get(sourceKey),
|
|
325
|
+
create: async () => {
|
|
326
|
+
const config = currentOpenCodeCatalogConfig(api);
|
|
327
|
+
const record = (await listLocalOpenCodeSessionPage({
|
|
328
|
+
searchTerm: threadId,
|
|
329
|
+
limit: 100
|
|
330
|
+
}, {
|
|
331
|
+
configIdentity: config,
|
|
332
|
+
forceRefresh: true
|
|
333
|
+
}).catch(() => void 0))?.sessions.find((session) => session.threadId === threadId);
|
|
334
|
+
if (!record) throw new OpenCodeCatalogParamsError("OpenCode session is unavailable");
|
|
335
|
+
const currentAvailability = resolveAcpSessionAvailability({
|
|
336
|
+
config,
|
|
337
|
+
backendId: ACPX_BACKEND_ID,
|
|
338
|
+
agentId: OPENCODE_ACP_AGENT_ID
|
|
339
|
+
});
|
|
340
|
+
if (!currentAvailability.available) throw new OpenCodeCatalogParamsError(currentAvailability.message);
|
|
341
|
+
const marker = { sourceThreadId: threadId };
|
|
342
|
+
return { sessionKey: (await api.runtime.agent.session.createSessionEntry({
|
|
343
|
+
cfg: config,
|
|
344
|
+
key: sessionCatalogAdoptedSessionKey(OPENCODE_ADOPTED_SESSION_KEY_PREFIX, threadId),
|
|
345
|
+
agentId: resolveDefaultAgentId(config),
|
|
346
|
+
recoverMatchingInitialEntry: true,
|
|
347
|
+
...record.name ? { label: record.name } : {},
|
|
348
|
+
...record.cwd ? { spawnedCwd: record.cwd } : {},
|
|
349
|
+
initialEntry: {
|
|
350
|
+
acpBackendId: ACPX_BACKEND_ID,
|
|
351
|
+
acpSessionBinding: {
|
|
352
|
+
acpAgentId: OPENCODE_ACP_AGENT_ID,
|
|
353
|
+
agentSessionId: threadId
|
|
354
|
+
},
|
|
355
|
+
pluginExtensions: { opencode: { sessionCatalog: marker } }
|
|
356
|
+
},
|
|
357
|
+
afterCreate: async (entry) => {
|
|
358
|
+
await importSessionCatalogHistory({
|
|
359
|
+
catalogId: "opencode",
|
|
360
|
+
threadId,
|
|
361
|
+
read: async ({ cursor, limit }) => await readOpenCodeTranscript(api.runtime, {
|
|
362
|
+
hostId,
|
|
363
|
+
threadId,
|
|
364
|
+
limit,
|
|
365
|
+
...cursor ? { cursor } : {}
|
|
366
|
+
}),
|
|
367
|
+
sessionId: entry.sessionId,
|
|
368
|
+
sessionKey: entry.key,
|
|
369
|
+
agentId: entry.agentId,
|
|
370
|
+
...record.cwd ? { cwd: record.cwd } : {},
|
|
371
|
+
config
|
|
372
|
+
});
|
|
373
|
+
return { pluginExtensions: { opencode: { sessionCatalog: marker } } };
|
|
374
|
+
}
|
|
375
|
+
})).key };
|
|
376
|
+
},
|
|
377
|
+
complete: async (continued) => await linkContinuedOpenCodeSession(continued.sessionKey, threadId)
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
function registerOpenCodeSessionCatalog(api) {
|
|
381
|
+
if (!isOpenCodeSessionCatalogEnabled(api.pluginConfig)) return;
|
|
382
|
+
api.registerSessionCatalog({
|
|
383
|
+
id: "opencode",
|
|
384
|
+
label: "OpenCode",
|
|
385
|
+
supportsProcessHomeIsolation: true,
|
|
386
|
+
list: async (query) => await listOpenCodeHosts(api, query),
|
|
387
|
+
read: async (request) => await readOpenCodeTranscript(api.runtime, request),
|
|
388
|
+
continueSession: async (request) => {
|
|
389
|
+
assertOpenCodeLocalAccess(request.hostId, request.allowProcessHomeFallback);
|
|
390
|
+
return await continueOpenCodeSession(api, request.hostId, request.threadId);
|
|
391
|
+
},
|
|
392
|
+
checkUpstreamActivity: (probes, policy) => checkOpenCodeUpstreamActivity(probes.filter((probe) => probe.hostId !== "gateway" || policy?.allowProcessHomeFallback !== false || !openCodeUsesProcessHomeFallback(process.env))),
|
|
393
|
+
openTerminal: async (request) => {
|
|
394
|
+
assertOpenCodeLocalAccess(request.hostId, request.allowProcessHomeFallback);
|
|
395
|
+
return await openOpenCodeCatalogTerminal({
|
|
396
|
+
runtime: api.runtime,
|
|
397
|
+
...request,
|
|
398
|
+
parseNodeSessionPage,
|
|
399
|
+
unwrapNodePayload
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
for (const command of createOpenCodeSessionNodeHostCommands(api)) api.registerNodeHostCommand(command);
|
|
404
|
+
for (const policy of createOpenCodeSessionNodeInvokePolicies()) api.registerNodeInvokePolicy(policy);
|
|
405
|
+
}
|
|
406
|
+
//#endregion
|
|
407
|
+
export { registerOpenCodeSessionCatalog };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region extensions/opencode/session-catalog-shared.ts
|
|
2
|
+
const OPENCODE_SESSIONS_LIST_COMMAND = "opencode.sessions.list.v1";
|
|
3
|
+
const OPENCODE_SESSION_READ_COMMAND = "opencode.sessions.read.v1";
|
|
4
|
+
const OPENCODE_TERMINAL_RESUME_COMMAND = "opencode.terminal.resume.v1";
|
|
5
|
+
const OPENCODE_SESSIONS_CAPABILITY = "opencode-sessions";
|
|
6
|
+
const OPENCODE_LOCAL_SESSION_HOST_ID = "gateway";
|
|
7
|
+
const OPENCODE_SESSION_CATALOG_MAX_PAGE_LIMIT = 100;
|
|
8
|
+
const OPENCODE_NODE_INVOKE_TIMEOUT_MS = 35e3;
|
|
9
|
+
const OPENCODE_SESSION_ID_PATTERN = /^(?!-)[A-Za-z0-9._:-]{1,256}$/u;
|
|
10
|
+
var OpenCodeCatalogParamsError = class extends Error {};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { OPENCODE_LOCAL_SESSION_HOST_ID, OPENCODE_NODE_INVOKE_TIMEOUT_MS, OPENCODE_SESSIONS_CAPABILITY, OPENCODE_SESSIONS_LIST_COMMAND, OPENCODE_SESSION_CATALOG_MAX_PAGE_LIMIT, OPENCODE_SESSION_ID_PATTERN, OPENCODE_SESSION_READ_COMMAND, OPENCODE_TERMINAL_RESUME_COMMAND, OpenCodeCatalogParamsError };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { OPENCODE_NODE_INVOKE_TIMEOUT_MS, OPENCODE_SESSIONS_CAPABILITY, OPENCODE_SESSIONS_LIST_COMMAND, OPENCODE_SESSION_ID_PATTERN, OPENCODE_TERMINAL_RESUME_COMMAND } from "./session-catalog-shared.js";
|
|
2
|
+
import { listLocalOpenCodeSessionPage } from "./session-catalog.js";
|
|
3
|
+
import { decodeNodePtyResumeParams, resolveNodeHostExecutable, runNodePtyCommand } from "openclaw/plugin-sdk/node-host";
|
|
4
|
+
//#region extensions/opencode/session-catalog-terminal.ts
|
|
5
|
+
function validateOpenCodeThreadId(value) {
|
|
6
|
+
if (typeof value !== "string" || !OPENCODE_SESSION_ID_PATTERN.test(value)) throw new Error("INVALID_REQUEST: threadId is invalid");
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
async function requireLocalOpenCodeSession(threadId) {
|
|
10
|
+
const record = (await listLocalOpenCodeSessionPage({
|
|
11
|
+
searchTerm: threadId,
|
|
12
|
+
limit: 100
|
|
13
|
+
})).sessions.find((session) => session.threadId === threadId);
|
|
14
|
+
if (!record) throw new Error("OpenCode session is unavailable");
|
|
15
|
+
return record;
|
|
16
|
+
}
|
|
17
|
+
function createOpenCodeTerminalNodeHostCommand(isAvailable) {
|
|
18
|
+
return {
|
|
19
|
+
command: OPENCODE_TERMINAL_RESUME_COMMAND,
|
|
20
|
+
cap: OPENCODE_SESSIONS_CAPABILITY,
|
|
21
|
+
dangerous: false,
|
|
22
|
+
duplex: true,
|
|
23
|
+
isAvailable,
|
|
24
|
+
handle: async (paramsJSON, io) => {
|
|
25
|
+
if (!io) throw new Error("OpenCode terminal command requires duplex transport");
|
|
26
|
+
const params = decodeNodePtyResumeParams(paramsJSON, validateOpenCodeThreadId);
|
|
27
|
+
const record = await requireLocalOpenCodeSession(params.threadId);
|
|
28
|
+
const resolution = resolveNodeHostExecutable("opencode", {
|
|
29
|
+
env: process.env,
|
|
30
|
+
pathEnv: process.env.PATH ?? process.env.Path ?? "",
|
|
31
|
+
strategy: "direct"
|
|
32
|
+
});
|
|
33
|
+
if (!resolution) throw new Error("OpenCode CLI is unavailable");
|
|
34
|
+
return JSON.stringify(await runNodePtyCommand({
|
|
35
|
+
file: resolution.executable,
|
|
36
|
+
args: ["--session", params.threadId],
|
|
37
|
+
cwd: record.cwd,
|
|
38
|
+
cols: params.cols,
|
|
39
|
+
rows: params.rows
|
|
40
|
+
}, io));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
async function resolveNodeOpenCodeSession(params) {
|
|
45
|
+
const raw = await params.runtime.nodes.invoke({
|
|
46
|
+
nodeId: params.nodeId,
|
|
47
|
+
command: OPENCODE_SESSIONS_LIST_COMMAND,
|
|
48
|
+
params: {
|
|
49
|
+
searchTerm: params.threadId,
|
|
50
|
+
limit: 100
|
|
51
|
+
},
|
|
52
|
+
timeoutMs: OPENCODE_NODE_INVOKE_TIMEOUT_MS,
|
|
53
|
+
scopes: ["operator.write"]
|
|
54
|
+
});
|
|
55
|
+
const record = params.parseNodeSessionPage(params.unwrapNodePayload(raw)).sessions.find((session) => session.threadId === params.threadId);
|
|
56
|
+
if (!record) throw new Error("OpenCode session is unavailable");
|
|
57
|
+
return record;
|
|
58
|
+
}
|
|
59
|
+
async function openOpenCodeCatalogTerminal(params) {
|
|
60
|
+
const title = `opencode --session ${params.threadId.slice(0, 12)}…`;
|
|
61
|
+
if (params.hostId === "gateway") {
|
|
62
|
+
const record = await requireLocalOpenCodeSession(params.threadId);
|
|
63
|
+
const resolution = resolveNodeHostExecutable("opencode", {
|
|
64
|
+
env: process.env,
|
|
65
|
+
pathEnv: process.env.PATH ?? "",
|
|
66
|
+
strategy: "fallback"
|
|
67
|
+
});
|
|
68
|
+
if (!resolution) throw new Error("OpenCode CLI is unavailable");
|
|
69
|
+
return {
|
|
70
|
+
kind: "local",
|
|
71
|
+
argv: [
|
|
72
|
+
resolution.executable,
|
|
73
|
+
"--session",
|
|
74
|
+
params.threadId
|
|
75
|
+
],
|
|
76
|
+
...record.cwd ? { cwd: record.cwd } : {},
|
|
77
|
+
...resolution.pathEnv ? { pathEnv: resolution.pathEnv } : {},
|
|
78
|
+
title
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (!params.hostId.startsWith("node:")) throw new Error("hostId is invalid");
|
|
82
|
+
const nodeId = params.hostId.slice(5);
|
|
83
|
+
if (!(await params.runtime.nodes.list()).nodes.find((candidate) => {
|
|
84
|
+
const commands = candidate.invocableCommands ?? candidate.commands;
|
|
85
|
+
return candidate.nodeId === nodeId && candidate.connected === true && commands?.includes("opencode.sessions.list.v1") === true && commands.includes("opencode.terminal.resume.v1");
|
|
86
|
+
})) throw new Error("paired-node OpenCode terminal is unavailable");
|
|
87
|
+
const record = await resolveNodeOpenCodeSession({
|
|
88
|
+
runtime: params.runtime,
|
|
89
|
+
nodeId,
|
|
90
|
+
threadId: params.threadId,
|
|
91
|
+
parseNodeSessionPage: params.parseNodeSessionPage,
|
|
92
|
+
unwrapNodePayload: params.unwrapNodePayload
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
kind: "node",
|
|
96
|
+
nodeId,
|
|
97
|
+
command: OPENCODE_TERMINAL_RESUME_COMMAND,
|
|
98
|
+
paramsJSON: JSON.stringify({ threadId: params.threadId }),
|
|
99
|
+
...record.cwd ? { cwd: record.cwd } : {},
|
|
100
|
+
title
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
export { createOpenCodeTerminalNodeHostCommand, openOpenCodeCatalogTerminal };
|