talon-agent 3.32.1 → 3.33.1
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/package.json +1 -1
- package/src/backend/claude-sdk/constants.ts +1 -6
- package/src/backend/claude-sdk/index.ts +2 -11
- package/src/backend/claude-sdk/models/parsing.ts +2 -2
- package/src/backend/claude-sdk/plan-usage.ts +0 -5
- package/src/backend/claude-sdk/stream.ts +1 -1
- package/src/backend/codex/auth.ts +2 -2
- package/src/backend/codex/constants.ts +0 -21
- package/src/backend/codex/handler/index.ts +0 -1
- package/src/backend/codex/mcp-config.ts +2 -2
- package/src/backend/codex/plan-usage.ts +0 -5
- package/src/backend/codex/state.ts +1 -1
- package/src/backend/codex/token-usage.ts +1 -1
- package/src/backend/kilo/factory.ts +39 -119
- package/src/backend/kilo/handler/index.ts +1 -11
- package/src/backend/kilo/handler/message.ts +20 -317
- package/src/backend/kilo/index.ts +0 -6
- package/src/backend/kilo/server.ts +55 -292
- package/src/backend/kilo/sessions.ts +5 -86
- package/src/backend/openai-agents/handler/index.ts +0 -1
- package/src/backend/openai-agents/handler/state.ts +0 -5
- package/src/backend/openai-agents/init.ts +1 -18
- package/src/backend/opencode/factory.ts +41 -118
- package/src/backend/opencode/handler/index.ts +1 -11
- package/src/backend/opencode/handler/message.ts +20 -321
- package/src/backend/opencode/index.ts +0 -4
- package/src/backend/opencode/server.ts +56 -225
- package/src/backend/opencode/sessions.ts +5 -86
- package/src/backend/remote-server/chat-turn.ts +359 -0
- package/src/backend/remote-server/client.ts +1 -1
- package/src/backend/remote-server/events.ts +1 -1
- package/src/backend/remote-server/factory.ts +150 -0
- package/src/backend/remote-server/index.ts +15 -43
- package/src/backend/remote-server/mcp.ts +1 -4
- package/src/backend/remote-server/model-catalog/catalog.ts +1 -1
- package/src/backend/remote-server/model-catalog/index.ts +1 -15
- package/src/backend/remote-server/model-catalog/provider.ts +0 -1
- package/src/backend/remote-server/server-bindings.ts +232 -0
- package/src/backend/remote-server/sse-stream.ts +1 -3
- package/src/backend/remote-server/turn-timeout.ts +1 -1
- package/src/backend/{kilo/handler → remote-server}/turn.ts +103 -67
- package/src/backend/shared/index.ts +7 -43
- package/src/cli/logs.ts +1 -1
- package/src/cli/status.ts +1 -1
- package/src/core/agent-runtime/backend-registry.ts +1 -1
- package/src/core/agent-runtime/capabilities.ts +1 -1
- package/src/core/agent-runtime/events.ts +1 -1
- package/src/core/background/heartbeat/agent.ts +1 -1
- package/src/core/background/heartbeat/index.ts +1 -2
- package/src/core/background/isolated-agent.ts +1 -1
- package/src/core/background/triggers/exit.ts +1 -1
- package/src/core/background/triggers/index.ts +1 -1
- package/src/core/bus/events.ts +2 -2
- package/src/core/doctor.ts +1 -1
- package/src/core/engine/backend-controller/index.ts +1 -3
- package/src/core/engine/backend-controller/rebind.ts +2 -2
- package/src/core/engine/dispatcher.ts +1 -15
- package/src/core/engine/gateway-actions/index.ts +0 -3
- package/src/core/engine/gateway-actions/types.ts +1 -1
- package/src/core/frontend-runtime/capabilities.ts +0 -2
- package/src/core/frontend-runtime/index.ts +1 -6
- package/src/core/frontend-runtime/routing.ts +0 -1
- package/src/core/mcp-hub/children.ts +0 -5
- package/src/core/mesh/bridge-links.ts +319 -0
- package/src/core/mesh/common.ts +30 -0
- package/src/core/mesh/device-files.ts +710 -0
- package/src/core/mesh/index.ts +1 -18
- package/src/core/mesh/service.ts +72 -894
- package/src/core/models/active-model.ts +0 -19
- package/src/core/plugin/index.ts +0 -1
- package/src/core/plugin/registry.ts +1 -1
- package/src/core/prompt/templates.ts +0 -5
- package/src/core/scripting/lua-runner.ts +1 -1
- package/src/core/scripts/runner.ts +1 -1
- package/src/core/soul/critic.ts +1 -1
- package/src/core/soul/embedder.ts +2 -2
- package/src/core/soul/governance.ts +1 -1
- package/src/core/soul/hdc.ts +1 -1
- package/src/core/soul/reflex.ts +2 -2
- package/src/core/soul/retrieve.ts +1 -1
- package/src/core/soul/signals.ts +4 -4
- package/src/core/soul/types.ts +1 -1
- package/src/core/tasks/index.ts +0 -4
- package/src/core/tasks/types.ts +1 -1
- package/src/core/tools/index.ts +1 -6
- package/src/core/tools/mcp-env.ts +3 -33
- package/src/core/types.ts +1 -1
- package/src/core/update/self-update.ts +1 -1
- package/src/core/vfs/fusefs.ts +1 -1
- package/src/core/vfs/types.ts +1 -1
- package/src/core/weaver/index.ts +1 -1
- package/src/core/weaver/typing-loop.ts +1 -1
- package/src/frontend/discord/actions/types.ts +1 -1
- package/src/frontend/discord/commands/definitions.ts +1 -1
- package/src/frontend/discord/handlers/access.ts +7 -61
- package/src/frontend/discord/handlers/index.ts +2 -15
- package/src/frontend/discord/handlers/registry.ts +0 -6
- package/src/frontend/discord/handlers/state.ts +15 -8
- package/src/frontend/native/protocol.ts +1 -4
- package/src/frontend/native/server.ts +392 -324
- package/src/frontend/shared/access.ts +152 -0
- package/src/frontend/shared/status-context.ts +1 -1
- package/src/frontend/telegram/actions/types.ts +1 -1
- package/src/frontend/telegram/commands/index.ts +1 -1
- package/src/frontend/telegram/commands/state.ts +1 -1
- package/src/frontend/telegram/handlers/access.ts +8 -25
- package/src/frontend/telegram/handlers/index.ts +0 -1
- package/src/frontend/telegram/handlers/queue.ts +3 -35
- package/src/frontend/telegram/handlers/state.ts +15 -8
- package/src/frontend/telegram/helpers/menu.ts +1 -1
- package/src/frontend/terminal/commands.ts +1 -4
- package/src/frontend/terminal/renderer.ts +1 -1
- package/src/frontend/whatsapp/actions/chat-info.ts +0 -3
- package/src/frontend/whatsapp/actions/messaging.ts +1 -7
- package/src/frontend/whatsapp/actions/shared.ts +1 -1
- package/src/frontend/whatsapp/actions/types.ts +1 -1
- package/src/frontend/whatsapp/formatting.ts +1 -1
- package/src/frontend/whatsapp/media-store.ts +1 -1
- package/src/frontend/whatsapp/pins.ts +0 -5
- package/src/native/fusefs.ts +3 -3
- package/src/native/scheduler-core.ts +1 -1
- package/src/native/warden.ts +2 -2
- package/src/plugins/mempalace/provision.ts +1 -5
- package/src/plugins/playwright/provision.ts +1 -1
- package/src/storage/db.ts +1 -1
- package/src/storage/goal-store.ts +1 -5
- package/src/storage/metrics.ts +0 -5
- package/src/storage/repositories/scripts-repo.ts +0 -5
- package/src/storage/scheduled-store.ts +1 -5
- package/src/storage/script-store.ts +5 -13
- package/src/storage/sessions.ts +0 -3
- package/src/storage/skill-store.ts +3 -3
- package/src/util/exec-output.ts +1 -1
- package/src/util/mcp-launcher.ts +3 -0
- package/src/backend/kilo/events.ts +0 -60
- package/src/backend/kilo/handler/state.ts +0 -9
- package/src/backend/opencode/handler/state.ts +0 -9
- package/src/backend/opencode/handler/turn.ts +0 -314
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* OpenCode server lifecycle —
|
|
2
|
+
* OpenCode server lifecycle — the OpenCode profile bound through
|
|
3
|
+
* `backend/remote-server/server-bindings.ts`.
|
|
3
4
|
*
|
|
4
|
-
* The MCP
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* - Backend-specific model parser (the fuzzy `provider/model` splitter
|
|
12
|
-
* from `./models.ts`).
|
|
13
|
-
* - Pre-warm hook that triggers immediately at init.
|
|
14
|
-
*
|
|
15
|
-
* The shared machinery (server spawn, MCP registration, session creation,
|
|
16
|
-
* provider resolution) lives in `backend/remote-server/`.
|
|
5
|
+
* The server spawn, MCP registration, session creation, and provider
|
|
6
|
+
* resolution are the shared remote-server family implementation (Kilo is
|
|
7
|
+
* a fork with the same HTTP API). This module holds only what is
|
|
8
|
+
* OpenCode's: the SDK constructors, port 4096, the text-preferred
|
|
9
|
+
* delivery contract, and the fuzzy `provider/model` parser. The exports
|
|
10
|
+
* keep their historical names — the models module, one-shot runner,
|
|
11
|
+
* tests, and `vi.mock("../backend/opencode/server.js")` all address them.
|
|
17
12
|
*/
|
|
18
13
|
|
|
19
14
|
import {
|
|
@@ -21,228 +16,64 @@ import {
|
|
|
21
16
|
createOpencodeServer,
|
|
22
17
|
type OpencodeClient,
|
|
23
18
|
} from "@opencode-ai/sdk/v2";
|
|
24
|
-
import type { TalonConfig } from "../../util/config.js";
|
|
25
|
-
import type { FrontendName } from "../../core/agent-runtime/backend-registry.js";
|
|
26
|
-
import { buildDeliveryContract } from "../shared/delivery-contract.js";
|
|
27
19
|
import {
|
|
28
|
-
guessProviderID,
|
|
29
|
-
getBucketPriority,
|
|
30
20
|
normalizeModelLookup,
|
|
31
|
-
parseRemoteModelQuery
|
|
21
|
+
parseRemoteModelQuery,
|
|
32
22
|
} from "../remote-server/model-catalog/index.js";
|
|
33
23
|
import {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
ensureChatMcpServer as ensureChatMcpServerShared,
|
|
38
|
-
ensurePluginMcpServers as ensurePluginMcpServersShared,
|
|
39
|
-
buildToolOverrides as buildToolOverridesShared,
|
|
40
|
-
disconnectChatMcpServer as disconnectChatMcpServerShared,
|
|
41
|
-
refreshPluginMcpServers as refreshPluginMcpServersShared,
|
|
42
|
-
ensureRemoteSession,
|
|
43
|
-
warmRemoteSession,
|
|
44
|
-
resolveProviderID as resolveProviderIDShared,
|
|
45
|
-
getRegisteredMcpServerNames as getRegisteredMcpServerNamesShared,
|
|
46
|
-
errMsg as sharedErrMsg,
|
|
47
|
-
TALON_MCP_SERVER_NAME as SHARED_TALON_MCP_SERVER_NAME,
|
|
48
|
-
type RemoteServerState,
|
|
49
|
-
} from "../remote-server/index.js";
|
|
50
|
-
|
|
51
|
-
// ── Constants ───────────────────────────────────────────────────────────────
|
|
52
|
-
|
|
53
|
-
const OPENCODE_HOSTNAME = "127.0.0.1";
|
|
54
|
-
// Overridable via `OPENCODE_PORT` env so integration tests can spawn an
|
|
55
|
-
// isolated server alongside a running production Talon (which holds 4096).
|
|
56
|
-
const OPENCODE_PORT = Number(process.env.OPENCODE_PORT ?? 4096);
|
|
57
|
-
const OPENCODE_BASE_URL = `http://${OPENCODE_HOSTNAME}:${OPENCODE_PORT}`;
|
|
58
|
-
const TALON_MCP_SERVER_NAME = SHARED_TALON_MCP_SERVER_NAME;
|
|
59
|
-
// Text-preferred delivery: plain assistant text is the reply; tools
|
|
60
|
-
// only for genuine side effects. Single-sourced from the shared
|
|
61
|
-
// contract templates (prompts/system/contract-text-preferred.md).
|
|
62
|
-
export function opencodeSystemPromptSuffix(frontend: string): string {
|
|
63
|
-
return `\n\n${buildDeliveryContract("text-preferred", frontend)}\n`;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Telegram-shaped default, kept for one-shot (cross-surface) paths. */
|
|
67
|
-
const OPENCODE_SYSTEM_PROMPT_SUFFIX = opencodeSystemPromptSuffix("telegram");
|
|
68
|
-
|
|
69
|
-
// ── State ───────────────────────────────────────────────────────────────────
|
|
70
|
-
|
|
71
|
-
const state: RemoteServerState<OpencodeClient> =
|
|
72
|
-
createRemoteServerState<OpencodeClient>({
|
|
73
|
-
label: "OpenCode",
|
|
74
|
-
hostname: OPENCODE_HOSTNAME,
|
|
75
|
-
port: OPENCODE_PORT,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
function createStrictOpencodeClient(baseUrl: string): OpencodeClient {
|
|
79
|
-
return createOpencodeClient({
|
|
80
|
-
baseUrl,
|
|
81
|
-
throwOnError: true,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// ── Init / teardown ─────────────────────────────────────────────────────────
|
|
86
|
-
|
|
87
|
-
export function initOpenCodeAgent(
|
|
88
|
-
cfg: TalonConfig,
|
|
89
|
-
getGatewayPort?: () => number,
|
|
90
|
-
frontend?: FrontendName,
|
|
91
|
-
): void {
|
|
92
|
-
state.config = cfg;
|
|
93
|
-
if (getGatewayPort) state.gatewayPortFn = getGatewayPort;
|
|
94
|
-
if (frontend) state.frontendName = frontend;
|
|
95
|
-
}
|
|
24
|
+
bindRemoteServer,
|
|
25
|
+
type RemoteModelSelection,
|
|
26
|
+
} from "../remote-server/server-bindings.js";
|
|
96
27
|
|
|
97
28
|
/**
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
29
|
+
* Parse the stored model-selection string into a `{providerID?, modelID}`
|
|
30
|
+
* pair. The parser is fuzzy — it tries to extract a provider hint from the
|
|
31
|
+
* prefix while preserving the full model id when ambiguous. See
|
|
32
|
+
* `remote-server/model-catalog/` for the underlying `parseRemoteModelQuery`.
|
|
101
33
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
stopHooks.add(hook);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function stopOpenCodeServer(): void {
|
|
110
|
-
stopRemoteServer(state, () => {
|
|
111
|
-
for (const hook of stopHooks) hook();
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// ── Server lifecycle ────────────────────────────────────────────────────────
|
|
116
|
-
|
|
117
|
-
export function ensureServer(): Promise<OpencodeClient> {
|
|
118
|
-
return ensureRemoteServerShared({
|
|
119
|
-
state,
|
|
120
|
-
createClient: createStrictOpencodeClient,
|
|
121
|
-
createServer: ({ hostname, port, timeout }) =>
|
|
122
|
-
createOpencodeServer({ hostname, port, timeout }),
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// ── MCP server registration ─────────────────────────────────────────────────
|
|
127
|
-
|
|
128
|
-
export function ensureChatMcpServer(
|
|
129
|
-
oc: OpencodeClient,
|
|
130
|
-
chatId: string,
|
|
131
|
-
): Promise<string> {
|
|
132
|
-
return ensureChatMcpServerShared(oc, state, chatId);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export function ensurePluginMcpServers(
|
|
136
|
-
oc: OpencodeClient,
|
|
137
|
-
chatId: string,
|
|
138
|
-
): Promise<string[]> {
|
|
139
|
-
return ensurePluginMcpServersShared(oc, state, chatId);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function buildToolOverrides(
|
|
143
|
-
oc: OpencodeClient,
|
|
144
|
-
chatServerName: string,
|
|
145
|
-
pluginServerNames: readonly string[] = [],
|
|
146
|
-
): Promise<Record<string, boolean> | undefined> {
|
|
147
|
-
return buildToolOverridesShared(oc, state, chatServerName, pluginServerNames);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function disconnectChatMcpServer(
|
|
151
|
-
oc: OpencodeClient,
|
|
152
|
-
serverName: string,
|
|
153
|
-
): Promise<void> {
|
|
154
|
-
return disconnectChatMcpServerShared(oc, state, serverName);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export async function refreshPluginMcpServers(chatId: string) {
|
|
158
|
-
const oc = await ensureServer();
|
|
159
|
-
return refreshPluginMcpServersShared(oc, state, chatId);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export function updateSystemPrompt(prompt: string): void {
|
|
163
|
-
if (state.config) state.config.systemPrompt = prompt;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// ── Session management ─────────────────────────────────────────────────────
|
|
167
|
-
|
|
168
|
-
export function ensureSession(
|
|
169
|
-
oc: OpencodeClient,
|
|
170
|
-
chatId: string,
|
|
171
|
-
): Promise<string> {
|
|
172
|
-
return ensureRemoteSession(oc, state, chatId);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Front-load a chat's cold start after `/reset`. Mirrors the Claude
|
|
177
|
-
* backend's `warmSession` so the `sessions` capability slot behaves the
|
|
178
|
-
* same across backends. Never throws — see `warmRemoteSession`.
|
|
179
|
-
*/
|
|
180
|
-
export function warmSession(chatId: string): Promise<void> {
|
|
181
|
-
return warmRemoteSession(state, chatId, {
|
|
182
|
-
ensureServer,
|
|
183
|
-
ensureSession,
|
|
184
|
-
ensureChatMcpServer,
|
|
185
|
-
ensurePluginMcpServers,
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// ── Provider resolution ────────────────────────────────────────────────────
|
|
190
|
-
|
|
191
|
-
export function resolveProviderID(
|
|
192
|
-
oc: OpencodeClient,
|
|
193
|
-
modelID: string,
|
|
194
|
-
): Promise<string> {
|
|
195
|
-
return resolveProviderIDShared(oc, state, modelID, {
|
|
196
|
-
guessProviderID,
|
|
197
|
-
getBucketPriority,
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Parse the stored model-selection string into a `{providerID?, modelID}` pair.
|
|
203
|
-
*
|
|
204
|
-
* OpenCode model ids occasionally encode the provider as a `provider/model`
|
|
205
|
-
* prefix (e.g. `openrouter/anthropic/claude-3.5-sonnet`). The parser here
|
|
206
|
-
* is fuzzy — it tries to extract a provider hint from the prefix while
|
|
207
|
-
* preserving the full model id when ambiguous. See `./models.ts` for the
|
|
208
|
-
* underlying `parseOpenCodeModelQuery` logic.
|
|
209
|
-
*/
|
|
210
|
-
export function parseStoredOpenCodeModelSelection(value: string): {
|
|
211
|
-
providerID?: string;
|
|
212
|
-
modelID: string;
|
|
213
|
-
} {
|
|
214
|
-
const { providerQuery, modelQuery } = parseOpenCodeModelQuery(value);
|
|
34
|
+
export function parseStoredOpenCodeModelSelection(
|
|
35
|
+
value: string,
|
|
36
|
+
): RemoteModelSelection {
|
|
37
|
+
const { providerQuery, modelQuery } = parseRemoteModelQuery(value);
|
|
215
38
|
return {
|
|
216
39
|
providerID: providerQuery ? normalizeModelLookup(providerQuery) : undefined,
|
|
217
40
|
modelID: modelQuery,
|
|
218
41
|
};
|
|
219
42
|
}
|
|
220
43
|
|
|
221
|
-
//
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
export {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
44
|
+
// Text-preferred delivery: plain assistant text is the reply; tools only
|
|
45
|
+
// for genuine side effects. Single-sourced from the shared contract
|
|
46
|
+
// templates (prompts/system/contract-text-preferred.md).
|
|
47
|
+
const opencode = bindRemoteServer<OpencodeClient>({
|
|
48
|
+
label: "OpenCode",
|
|
49
|
+
defaultPort: 4096,
|
|
50
|
+
portEnv: "OPENCODE_PORT",
|
|
51
|
+
deliveryContract: "text-preferred",
|
|
52
|
+
createClient: (baseUrl) =>
|
|
53
|
+
createOpencodeClient({ baseUrl, throwOnError: true }),
|
|
54
|
+
createServer: ({ hostname, port, timeout }) =>
|
|
55
|
+
createOpencodeServer({ hostname, port, timeout }),
|
|
56
|
+
parseModelSelection: parseStoredOpenCodeModelSelection,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const OPENCODE_BASE_URL = opencode.baseUrl;
|
|
60
|
+
export const opencodeSystemPromptSuffix = opencode.systemPromptSuffix;
|
|
61
|
+
export const OPENCODE_SYSTEM_PROMPT_SUFFIX = opencode.defaultSystemPromptSuffix;
|
|
62
|
+
export const initOpenCodeAgent = opencode.init;
|
|
63
|
+
export const stopOpenCodeServer = opencode.stop;
|
|
64
|
+
export const {
|
|
65
|
+
onServerStop,
|
|
66
|
+
ensureServer,
|
|
67
|
+
ensureChatMcpServer,
|
|
68
|
+
ensurePluginMcpServers,
|
|
69
|
+
buildToolOverrides,
|
|
70
|
+
disconnectChatMcpServer,
|
|
71
|
+
refreshPluginMcpServers,
|
|
72
|
+
updateSystemPrompt,
|
|
73
|
+
ensureSession,
|
|
74
|
+
warmSession,
|
|
75
|
+
resolveProviderID,
|
|
76
|
+
getConfig,
|
|
77
|
+
getRegisteredMcpServerNames,
|
|
247
78
|
errMsg,
|
|
248
|
-
};
|
|
79
|
+
} = opencode;
|
|
@@ -1,116 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* OpenCode session helpers —
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* Kilo is a fork of OpenCode that exposes the same session message shape
|
|
6
|
-
* and question API, so the parsing / summarisation / snapshot logic
|
|
7
|
-
* lives in the shared module. This file binds the OpenCode backend label
|
|
8
|
-
* and re-exports under OpenCode-named symbols for back-compat with the
|
|
9
|
-
* handler and the public OpenCode barrel.
|
|
2
|
+
* OpenCode session helpers — the shared `remote-server/session-helpers.ts`
|
|
3
|
+
* bound to the OpenCode client, re-exported under OpenCode-named symbols
|
|
4
|
+
* for the handler / index / test imports.
|
|
10
5
|
*/
|
|
11
6
|
|
|
12
7
|
import type { OpencodeClient } from "@opencode-ai/sdk/v2";
|
|
13
8
|
import {
|
|
14
|
-
REMOTE_SESSION_MESSAGE_LIMIT,
|
|
15
9
|
extractPartsSummary as extractPartsSummaryShared,
|
|
16
|
-
extractAssistantUsage as extractAssistantUsageShared,
|
|
17
10
|
summarizeAssistantMessages,
|
|
18
|
-
getTurnSummary,
|
|
19
11
|
getSessionSnapshot,
|
|
20
|
-
rejectPendingQuestions as rejectPendingQuestionsShared,
|
|
21
|
-
approvePendingPermissions as approvePendingPermissionsShared,
|
|
22
|
-
type RemoteAssistantInfo,
|
|
23
12
|
type RemoteSessionSnapshot,
|
|
24
13
|
type RemoteSessionClient,
|
|
25
14
|
} from "../remote-server/session-helpers.js";
|
|
26
15
|
import { ensureServer } from "./server.js";
|
|
27
16
|
|
|
28
|
-
// ── Constants / type aliases (OpenCode-named surface) ──────────────────────
|
|
29
|
-
|
|
30
|
-
export const OPENCODE_SESSION_MESSAGE_LIMIT = REMOTE_SESSION_MESSAGE_LIMIT;
|
|
31
|
-
|
|
32
|
-
/** Subset of OpenCode's `Message.info` used by Talon for usage accounting. */
|
|
33
|
-
export type OpenCodeAssistantInfo = RemoteAssistantInfo;
|
|
34
|
-
|
|
35
17
|
/** Snapshot of an OpenCode session's lifetime + last-turn assistant info. */
|
|
36
18
|
export type OpenCodeSessionSnapshot = RemoteSessionSnapshot;
|
|
37
19
|
|
|
38
|
-
// ── Re-exports of pure helpers ─────────────────────────────────────────────
|
|
39
|
-
|
|
40
20
|
export const extractPartsSummary = extractPartsSummaryShared;
|
|
41
|
-
export const extractAssistantUsage = extractAssistantUsageShared;
|
|
42
21
|
|
|
43
|
-
/**
|
|
44
|
-
* Summarise a batch of session messages into per-turn usage totals.
|
|
45
|
-
* Re-exported under the OpenCode-prefixed name for back-compat with
|
|
46
|
-
* existing imports.
|
|
47
|
-
*/
|
|
22
|
+
/** Summarise a batch of session messages into per-turn usage totals. */
|
|
48
23
|
export const summarizeOpenCodeAssistantMessages = summarizeAssistantMessages;
|
|
49
24
|
|
|
50
|
-
// ── Session-messages aggregators ──────────────────────────────────────────
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Aggregate the most recent turn's assistant messages into a usage
|
|
54
|
-
* summary. Delegates to the shared helper with an OpenCode-typed client.
|
|
55
|
-
*/
|
|
56
|
-
export function getOpenCodeTurnSummary(
|
|
57
|
-
oc: OpencodeClient,
|
|
58
|
-
sessionId: string,
|
|
59
|
-
minCreatedAt: number,
|
|
60
|
-
): Promise<ReturnType<typeof summarizeAssistantMessages>> {
|
|
61
|
-
return getTurnSummary(
|
|
62
|
-
oc as unknown as RemoteSessionClient,
|
|
63
|
-
sessionId,
|
|
64
|
-
minCreatedAt,
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
25
|
/**
|
|
69
26
|
* Build an {@link OpenCodeSessionSnapshot} for the given session id.
|
|
70
|
-
*
|
|
71
27
|
* Returns `undefined` if no session id was provided.
|
|
72
28
|
*/
|
|
73
29
|
export async function getOpenCodeSessionSnapshot(
|
|
74
30
|
sessionId: string | undefined,
|
|
75
31
|
): Promise<OpenCodeSessionSnapshot | undefined> {
|
|
76
32
|
if (!sessionId) return undefined;
|
|
77
|
-
const oc = await ensureServer();
|
|
33
|
+
const oc: OpencodeClient = await ensureServer();
|
|
78
34
|
return getSessionSnapshot(oc as unknown as RemoteSessionClient, sessionId);
|
|
79
35
|
}
|
|
80
|
-
|
|
81
|
-
// ── Pending-question guard ─────────────────────────────────────────────────
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Auto-respond to pending OpenCode questions for this session. Delegates
|
|
85
|
-
* to the shared helper with the OpenCode backend label.
|
|
86
|
-
*/
|
|
87
|
-
export function rejectPendingQuestions(
|
|
88
|
-
oc: OpencodeClient,
|
|
89
|
-
sessionId: string,
|
|
90
|
-
chatId: string,
|
|
91
|
-
seenQuestionIds: Set<string>,
|
|
92
|
-
): Promise<void> {
|
|
93
|
-
return rejectPendingQuestionsShared(
|
|
94
|
-
oc as unknown as RemoteSessionClient,
|
|
95
|
-
sessionId,
|
|
96
|
-
chatId,
|
|
97
|
-
seenQuestionIds,
|
|
98
|
-
"OpenCode",
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** Auto-approve pending OpenCode permissions for this headless session. */
|
|
103
|
-
export function approvePendingPermissions(
|
|
104
|
-
oc: OpencodeClient,
|
|
105
|
-
sessionId: string,
|
|
106
|
-
chatId: string,
|
|
107
|
-
seenPermissionIds: Set<string>,
|
|
108
|
-
): Promise<void> {
|
|
109
|
-
return approvePendingPermissionsShared(
|
|
110
|
-
oc as unknown as RemoteSessionClient,
|
|
111
|
-
sessionId,
|
|
112
|
-
chatId,
|
|
113
|
-
seenPermissionIds,
|
|
114
|
-
"OpenCode",
|
|
115
|
-
);
|
|
116
|
-
}
|