talon-agent 3.33.0 → 3.33.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/package.json +3 -3
- 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/server.ts +55 -272
- package/src/backend/kilo/sessions.ts +5 -83
- 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/server.ts +57 -217
- package/src/backend/opencode/sessions.ts +5 -83
- package/src/backend/remote-server/chat-turn.ts +359 -0
- package/src/backend/remote-server/factory.ts +150 -0
- package/src/backend/remote-server/index.ts +14 -19
- package/src/backend/remote-server/server-bindings.ts +232 -0
- package/src/backend/{kilo/handler → remote-server}/turn.ts +103 -67
- 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/service.ts +72 -894
- package/src/frontend/discord/handlers/access.ts +7 -56
- package/src/frontend/discord/handlers/state.ts +15 -8
- package/src/frontend/native/server.ts +391 -323
- package/src/frontend/shared/access.ts +152 -0
- package/src/frontend/telegram/handlers/access.ts +8 -25
- package/src/frontend/telegram/handlers/queue.ts +3 -35
- package/src/frontend/telegram/handlers/state.ts +15 -8
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "talon-agent",
|
|
3
|
-
"version": "3.33.
|
|
3
|
+
"version": "3.33.2",
|
|
4
4
|
"description": "Multi-frontend AI agent with full tool access, streaming, cron jobs, and plugin system",
|
|
5
5
|
"author": "Dylan Neve",
|
|
6
6
|
"license": "MIT",
|
|
@@ -106,9 +106,9 @@
|
|
|
106
106
|
"@kilocode/sdk": "^7.2.22",
|
|
107
107
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
108
108
|
"@openai/agents": "^0.17.0",
|
|
109
|
-
"@openai/codex-sdk": "^0.
|
|
109
|
+
"@openai/codex-sdk": "^0.153.2",
|
|
110
110
|
"@opencode-ai/sdk": "^1.17.4",
|
|
111
|
-
"@playwright/mcp": "0.0.
|
|
111
|
+
"@playwright/mcp": "0.0.80",
|
|
112
112
|
"@types/cross-spawn": "^6.0.6",
|
|
113
113
|
"@types/qrcode": "^1.5.6",
|
|
114
114
|
"baileys": "^7.0.0-rc14",
|
|
@@ -3,131 +3,51 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Side-effect import (`import "./factory.js"` from `bootstrap.ts`) calls
|
|
5
5
|
* `registerBackend(...)` at module load, making Kilo available under
|
|
6
|
-
* `config.backend === "kilo"`.
|
|
7
|
-
*
|
|
8
|
-
* Returns a composed `Backend` with capability slots for chat,
|
|
9
|
-
* background, models, and usage.
|
|
6
|
+
* `config.backend === "kilo"`. The capability wiring is the shared
|
|
7
|
+
* remote-server composition; only the bound functions are Kilo's.
|
|
10
8
|
*/
|
|
11
9
|
|
|
12
10
|
import { registerBackend } from "../../core/agent-runtime/backend-registry.js";
|
|
13
|
-
import
|
|
14
|
-
import { log } from "../../util/log.js";
|
|
15
|
-
import { handlerToEvents } from "../shared/handler-to-events.js";
|
|
16
|
-
import { interruptChatTurn } from "../shared/turn-interrupt.js";
|
|
17
|
-
import {
|
|
18
|
-
composeBackend,
|
|
19
|
-
type ChatBackend,
|
|
20
|
-
type BackgroundRunner,
|
|
21
|
-
type ModelCatalog,
|
|
22
|
-
type SessionBackend,
|
|
23
|
-
type SystemControl,
|
|
24
|
-
type ToolRuntime,
|
|
25
|
-
type UsageTelemetry,
|
|
26
|
-
} from "../../core/agent-runtime/capabilities.js";
|
|
27
|
-
|
|
11
|
+
import { createRemoteBackendFactory } from "../remote-server/factory.js";
|
|
28
12
|
import {
|
|
29
13
|
initKiloAgent,
|
|
30
14
|
stopKiloServer,
|
|
31
|
-
handleMessage
|
|
32
|
-
runOneShotAgent
|
|
15
|
+
handleMessage,
|
|
16
|
+
runOneShotAgent,
|
|
33
17
|
getKiloSessionSnapshot,
|
|
34
|
-
resolveModel
|
|
35
|
-
getModelInfo
|
|
36
|
-
getSettingsPresentation
|
|
37
|
-
getProviders
|
|
38
|
-
getProviderModels
|
|
39
|
-
formatModelError
|
|
40
|
-
listModels
|
|
41
|
-
refreshPluginMcpServers
|
|
42
|
-
updateSystemPrompt
|
|
43
|
-
warmSession
|
|
18
|
+
resolveModel,
|
|
19
|
+
getModelInfo,
|
|
20
|
+
getSettingsPresentation,
|
|
21
|
+
getProviders,
|
|
22
|
+
getProviderModels,
|
|
23
|
+
formatModelError,
|
|
24
|
+
listModels,
|
|
25
|
+
refreshPluginMcpServers,
|
|
26
|
+
updateSystemPrompt,
|
|
27
|
+
warmSession,
|
|
44
28
|
} from "./index.js";
|
|
45
29
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
initKiloAgent
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
getDefaultModelId: () => undefined,
|
|
71
|
-
getRawModelInfo: (id) => kiloGetModelInfo(id),
|
|
72
|
-
getSettingsPresentation: (m, options) =>
|
|
73
|
-
kiloGetSettingsPresentation(m, options),
|
|
74
|
-
getProviders: () => kiloGetProviders(),
|
|
75
|
-
getProviderModels: (p, pg, ps) => kiloGetProviderModels(p, pg, ps),
|
|
76
|
-
formatModelError: (q, r) => kiloFormatModelError(q, r),
|
|
77
|
-
listModels: (f) => kiloListModels(f),
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const usage: UsageTelemetry = {
|
|
81
|
-
getSessionSnapshot: async (sessionId) => {
|
|
82
|
-
const snap = await getKiloSessionSnapshot(sessionId);
|
|
83
|
-
if (!snap) return undefined;
|
|
84
|
-
return {
|
|
85
|
-
inputTokens: snap.usage?.totalInputTokens,
|
|
86
|
-
outputTokens: snap.usage?.totalOutputTokens,
|
|
87
|
-
cacheRead: snap.usage?.totalCacheRead,
|
|
88
|
-
cacheWrite: snap.usage?.totalCacheWrite,
|
|
89
|
-
contextModelId: snap.assistant?.modelID,
|
|
90
|
-
};
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const tools: ToolRuntime = {
|
|
95
|
-
refreshTools: (chatId) => kiloRefreshPluginMcpServers(chatId),
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// Session state lives on the Kilo server, and `/reset` already clears
|
|
99
|
-
// Talon's stored id centrally (storage/sessions.ts), so the next turn
|
|
100
|
-
// creates a fresh one — no `resetChat` needed. `warmSession` front-loads
|
|
101
|
-
// that creation plus the plugin-MCP sweep, matching what the Claude
|
|
102
|
-
// backend does after a reset.
|
|
103
|
-
const sessions: SessionBackend = {
|
|
104
|
-
warmSession: (chatId) => kiloWarmSession(chatId),
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const control: SystemControl = {
|
|
108
|
-
updateSystemPrompt: (prompt) => kiloUpdateSystemPrompt(prompt),
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const backend = composeBackend({
|
|
112
|
-
id: "kilo",
|
|
113
|
-
label: "Kilo",
|
|
114
|
-
cacheMetrics: "readwrite",
|
|
115
|
-
chat,
|
|
116
|
-
background,
|
|
117
|
-
models,
|
|
118
|
-
sessions,
|
|
119
|
-
tools,
|
|
120
|
-
usage,
|
|
121
|
-
control,
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
backend,
|
|
126
|
-
cleanup: () => {
|
|
127
|
-
stopKiloServer();
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
registerBackend(kiloFactory);
|
|
30
|
+
registerBackend(
|
|
31
|
+
createRemoteBackendFactory({
|
|
32
|
+
id: "kilo",
|
|
33
|
+
label: "Kilo",
|
|
34
|
+
sdkPackage: "@kilocode/sdk",
|
|
35
|
+
init: initKiloAgent,
|
|
36
|
+
stop: stopKiloServer,
|
|
37
|
+
handleMessage,
|
|
38
|
+
runOneShotAgent,
|
|
39
|
+
getSessionSnapshot: getKiloSessionSnapshot,
|
|
40
|
+
models: {
|
|
41
|
+
resolveModel,
|
|
42
|
+
getModelInfo,
|
|
43
|
+
getSettingsPresentation,
|
|
44
|
+
getProviders,
|
|
45
|
+
getProviderModels,
|
|
46
|
+
formatModelError,
|
|
47
|
+
listModels,
|
|
48
|
+
},
|
|
49
|
+
refreshPluginMcpServers,
|
|
50
|
+
warmSession,
|
|
51
|
+
updateSystemPrompt,
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
@@ -1,12 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Kilo message handler.
|
|
3
|
-
*
|
|
4
|
-
* Split by responsibility:
|
|
5
|
-
* - `state` — in-flight session registry (activeSessions)
|
|
6
|
-
* - `turn` — runKiloTurn + SSE subscription + last-assistant lookup
|
|
7
|
-
* - `message` — the handleMessage entry point + post-loop accounting
|
|
8
|
-
*
|
|
9
|
-
* Re-exports the same public surface the old single-file module exposed.
|
|
10
|
-
*/
|
|
11
|
-
|
|
1
|
+
/** Kilo message handler barrel. */
|
|
12
2
|
export { handleMessage } from "./message.js";
|
|
@@ -1,23 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Kilo main message handler
|
|
3
|
-
*
|
|
4
|
-
* Orchestrates the full turn lifecycle on top of Kilo's HTTP API + SSE event
|
|
5
|
-
* stream. Shares the non-SDK-specific primitives via `../../shared/`. The
|
|
6
|
-
* Kilo-specific turn driver + SSE subscription live in `turn.ts`.
|
|
2
|
+
* Kilo main message handler — the shared remote-server chat turn bound to
|
|
3
|
+
* Kilo's server bindings.
|
|
7
4
|
*/
|
|
8
5
|
|
|
9
|
-
import {
|
|
10
|
-
getSession,
|
|
11
|
-
incrementTurns,
|
|
12
|
-
recordUsage,
|
|
13
|
-
setSessionId,
|
|
14
|
-
setSessionName,
|
|
15
|
-
} from "../../../storage/sessions.js";
|
|
16
|
-
import { getChatSettings } from "../../../storage/chat-settings.js";
|
|
17
|
-
import { log, logError } from "../../../util/log.js";
|
|
18
|
-
import { traceMessage } from "../../../util/trace.js";
|
|
19
6
|
import type { QueryParams, QueryResult } from "../../shared/handler-types.js";
|
|
20
|
-
|
|
7
|
+
import { runRemoteChatTurn } from "../../remote-server/chat-turn.js";
|
|
21
8
|
import {
|
|
22
9
|
ensureServer,
|
|
23
10
|
ensureSession,
|
|
@@ -29,308 +16,24 @@ import {
|
|
|
29
16
|
getConfig,
|
|
30
17
|
kiloSystemPromptSuffix,
|
|
31
18
|
} from "../server.js";
|
|
32
|
-
import {
|
|
33
|
-
frontendsForChat,
|
|
34
|
-
nonTerminalFrontends,
|
|
35
|
-
} from "../../shared/frontends.js";
|
|
36
|
-
import { getKiloTurnSummary } from "../sessions.js";
|
|
37
|
-
import {
|
|
38
|
-
createStreamState,
|
|
39
|
-
recordTokens,
|
|
40
|
-
finalizeResponseText,
|
|
41
|
-
formatUserPrompt,
|
|
42
|
-
prepareSystemPrompt,
|
|
43
|
-
extractSessionName,
|
|
44
|
-
summarizeUsage,
|
|
45
|
-
routeDelivery,
|
|
46
|
-
applyRetryDecision,
|
|
47
|
-
recordTurnMetrics,
|
|
48
|
-
recordFailedTurnAccounting,
|
|
49
|
-
registerTurnInterrupt,
|
|
50
|
-
} from "../../shared/index.js";
|
|
51
|
-
import { activeSessions } from "./state.js";
|
|
52
|
-
import { runKiloTurn } from "./turn.js";
|
|
53
|
-
|
|
54
|
-
export async function handleMessage(
|
|
55
|
-
params: QueryParams,
|
|
56
|
-
_retried = false,
|
|
57
|
-
): Promise<QueryResult> {
|
|
58
|
-
const config = getConfig();
|
|
59
|
-
if (!config) throw new Error("Kilo agent not initialized");
|
|
60
|
-
|
|
61
|
-
const {
|
|
62
|
-
chatId,
|
|
63
|
-
text,
|
|
64
|
-
senderName,
|
|
65
|
-
senderHandle,
|
|
66
|
-
isGroup,
|
|
67
|
-
messageId,
|
|
68
|
-
onTextBlock,
|
|
69
|
-
onToolUse,
|
|
70
|
-
} = params;
|
|
71
|
-
const t0 = Date.now();
|
|
72
|
-
const session = getSession(chatId);
|
|
73
|
-
const previousTurns = session.turns;
|
|
74
|
-
|
|
75
|
-
// Resolve active model + provider lookup against Kilo's catalog
|
|
76
|
-
const chatSettings = getChatSettings(chatId);
|
|
77
|
-
const activeModel = params.model ?? chatSettings.model ?? config.model;
|
|
78
|
-
const { providerID: selectedProviderID, modelID } =
|
|
79
|
-
parseStoredKiloModelSelection(activeModel);
|
|
80
|
-
|
|
81
|
-
const oc = await ensureServer();
|
|
82
|
-
const providerID =
|
|
83
|
-
selectedProviderID ?? (await resolveProviderID(oc, modelID));
|
|
84
|
-
log(
|
|
85
|
-
"agent",
|
|
86
|
-
`[${chatId}] Kilo model resolved: provider=${providerID} model=${modelID}` +
|
|
87
|
-
(selectedProviderID ? "" : " (provider via catalog lookup)"),
|
|
88
|
-
);
|
|
89
|
-
const sessionId = await ensureSession(oc, chatId);
|
|
90
|
-
const chatMcpServerName = await ensureChatMcpServer(oc, chatId);
|
|
91
|
-
const pluginMcpServerNames = await ensurePluginMcpServers(oc, chatId);
|
|
92
|
-
const toolOverrides = await buildToolOverrides(
|
|
93
|
-
oc,
|
|
94
|
-
chatMcpServerName,
|
|
95
|
-
pluginMcpServerNames,
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
// Build the prompt (time tag + sender + msg_id reference)
|
|
99
|
-
const prompt = formatUserPrompt({
|
|
100
|
-
text,
|
|
101
|
-
senderName: senderName ?? "user",
|
|
102
|
-
senderHandle,
|
|
103
|
-
isGroup,
|
|
104
|
-
messageId,
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
// Per-session frozen prompt + Kilo-specific delivery suffix
|
|
108
|
-
const { text: systemPrompt } = prepareSystemPrompt({
|
|
109
|
-
config,
|
|
110
|
-
previousTurns,
|
|
111
|
-
backendSuffix: kiloSystemPromptSuffix(
|
|
112
|
-
frontendsForChat(chatId, nonTerminalFrontends(config.frontend))[0] ??
|
|
113
|
-
"telegram",
|
|
114
|
-
),
|
|
115
|
-
chatId,
|
|
116
|
-
sessionEpoch: session.createdAt,
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
log("agent", `[${chatId}] <- (${text.length} chars)`);
|
|
120
|
-
traceMessage(chatId, "in", text, { senderName, isGroup });
|
|
121
|
-
activeSessions.set(chatId, sessionId);
|
|
122
|
-
|
|
123
|
-
// Bind the stream state to the chat so token mutators mirror counts
|
|
124
|
-
// into the live-turn overlay — /status updates while the turn runs.
|
|
125
|
-
const state = createStreamState(chatId);
|
|
126
|
-
state.newSessionId = sessionId;
|
|
127
|
-
// A user interrupt is a synthetic turn terminator: marking the flag
|
|
128
|
-
// before aborting the session routes the close through the same clean
|
|
129
|
-
// path a model-fired end_turn takes (MessageAborted swallowed as the
|
|
130
|
-
// expected close, no retry), settling with whatever the turn produced.
|
|
131
|
-
const unregisterInterrupt = registerTurnInterrupt(chatId, async () => {
|
|
132
|
-
state.turnTerminated = true;
|
|
133
|
-
await oc.session.abort({ sessionID: sessionId });
|
|
134
|
-
});
|
|
135
|
-
const promptStartedAt = Date.now();
|
|
136
|
-
const seenQuestionIds = new Set<string>();
|
|
137
|
-
const seenPermissionIds = new Set<string>();
|
|
138
|
-
const seenToolCallIds = new Set<string>();
|
|
139
|
-
|
|
140
|
-
const setupMs = Date.now() - t0;
|
|
141
|
-
let promptMs = 0;
|
|
142
|
-
|
|
143
|
-
try {
|
|
144
|
-
// Drive the Kilo turn: subscribe to SSE events in parallel with
|
|
145
|
-
// promptAsync, surface tool calls + terminator into shared state, and
|
|
146
|
-
// exit when the turn closes / goes idle. `onStreamDelta` is
|
|
147
|
-
// intentionally NOT forwarded — Telegram's contract is "send the final
|
|
148
|
-
// reply once", not live edit_message updates. Final delivery happens
|
|
149
|
-
// through `end_turn` / `send`.
|
|
150
|
-
const turnStart = Date.now();
|
|
151
|
-
await runKiloTurn({
|
|
152
|
-
oc,
|
|
153
|
-
sessionId,
|
|
154
|
-
prompt,
|
|
155
|
-
systemPrompt,
|
|
156
|
-
providerID,
|
|
157
|
-
modelID,
|
|
158
|
-
state,
|
|
159
|
-
chatId,
|
|
160
|
-
seenQuestionIds,
|
|
161
|
-
seenPermissionIds,
|
|
162
|
-
seenToolCallIds,
|
|
163
|
-
toolOverrides,
|
|
164
|
-
onStreamDelta: undefined,
|
|
165
|
-
onTextBlock,
|
|
166
|
-
onToolUse,
|
|
167
|
-
});
|
|
168
|
-
promptMs = Date.now() - turnStart;
|
|
169
|
-
} catch (err) {
|
|
170
|
-
const outcome = await applyRetryDecision({
|
|
171
|
-
err,
|
|
172
|
-
chatId,
|
|
173
|
-
activeModel,
|
|
174
|
-
retried: _retried,
|
|
175
|
-
params,
|
|
176
|
-
recurseWithRetried: (p) => handleMessage(p, true),
|
|
177
|
-
backendLabel: "Kilo",
|
|
178
|
-
});
|
|
179
|
-
if (outcome.retry) return outcome.retry;
|
|
180
19
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
contextTokens: state.contextTokens,
|
|
198
|
-
contextWindow: state.contextWindow,
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
logError("agent", `[${chatId}] Kilo error: ${outcome.classified.message}`);
|
|
202
|
-
throw outcome.classified;
|
|
203
|
-
} finally {
|
|
204
|
-
unregisterInterrupt();
|
|
205
|
-
if (activeSessions.get(chatId) === sessionId) {
|
|
206
|
-
activeSessions.delete(chatId);
|
|
207
|
-
}
|
|
208
|
-
// Note: we deliberately do NOT disconnect the chat MCP server here.
|
|
209
|
-
// The server is named per-chat so it's safe to keep across turns;
|
|
210
|
-
// re-spawning the subprocess each turn cost ~800ms per message.
|
|
211
|
-
// Per-prompt overrides isolate visibility across concurrent chats.
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// ── Post-loop accounting ──────────────────────────────────────────────────
|
|
215
|
-
|
|
216
|
-
// If the SSE loop missed any usage info, fall back to the session
|
|
217
|
-
// summary endpoint (which always reflects the final server state).
|
|
218
|
-
if (
|
|
219
|
-
state.sdkInputTokens === 0 &&
|
|
220
|
-
state.sdkOutputTokens === 0 &&
|
|
221
|
-
state.sdkCacheRead === 0
|
|
222
|
-
) {
|
|
223
|
-
try {
|
|
224
|
-
const summary = await getKiloTurnSummary(oc, sessionId, promptStartedAt);
|
|
225
|
-
if (summary.usage.assistantMessages > 0) {
|
|
226
|
-
recordTokens(state, {
|
|
227
|
-
inputTokens: summary.usage.inputTokens,
|
|
228
|
-
outputTokens: summary.usage.outputTokens,
|
|
229
|
-
cacheRead: summary.usage.cacheRead,
|
|
230
|
-
cacheWrite: summary.usage.cacheWrite,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
} catch {
|
|
234
|
-
// best-effort — Kilo session summaries can race on cancellation
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const responseText = finalizeResponseText(state);
|
|
239
|
-
const durationMs = Date.now() - t0;
|
|
240
|
-
recordTurnMetrics({
|
|
241
|
-
chatId,
|
|
242
|
-
backend: "kilo",
|
|
243
|
-
durationMs,
|
|
244
|
-
toolCalls: state.toolCalls,
|
|
245
|
-
apiCalls: state.numApiCalls,
|
|
246
|
-
usage: {
|
|
247
|
-
inputTokens: state.sdkInputTokens,
|
|
248
|
-
outputTokens: state.sdkOutputTokens,
|
|
249
|
-
cacheRead: state.sdkCacheRead,
|
|
250
|
-
cacheWrite: state.sdkCacheWrite,
|
|
20
|
+
export function handleMessage(params: QueryParams): Promise<QueryResult> {
|
|
21
|
+
// Assembled per call, not at module load: tests mock `../server.js`
|
|
22
|
+
// with a partial surface and only touch the members they need.
|
|
23
|
+
return runRemoteChatTurn(
|
|
24
|
+
{
|
|
25
|
+
id: "kilo",
|
|
26
|
+
label: "Kilo",
|
|
27
|
+
getConfig,
|
|
28
|
+
ensureServer,
|
|
29
|
+
parseModelSelection: parseStoredKiloModelSelection,
|
|
30
|
+
resolveProviderID,
|
|
31
|
+
ensureSession,
|
|
32
|
+
ensureChatMcpServer,
|
|
33
|
+
ensurePluginMcpServers,
|
|
34
|
+
buildToolOverrides,
|
|
35
|
+
systemPromptSuffix: kiloSystemPromptSuffix,
|
|
251
36
|
},
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if (state.newSessionId) {
|
|
255
|
-
// setSessionId tolerates "same id" calls — keep it simple.
|
|
256
|
-
const stored = getSession(chatId).sessionId;
|
|
257
|
-
if (stored !== state.newSessionId) {
|
|
258
|
-
setSessionId(chatId, state.newSessionId);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
incrementTurns(chatId);
|
|
263
|
-
recordUsage(chatId, {
|
|
264
|
-
inputTokens: state.sdkInputTokens,
|
|
265
|
-
outputTokens: state.sdkOutputTokens,
|
|
266
|
-
cacheRead: state.sdkCacheRead,
|
|
267
|
-
cacheWrite: state.sdkCacheWrite,
|
|
268
|
-
durationMs,
|
|
269
|
-
model: activeModel,
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
// Set a descriptive session name from the user's first message
|
|
273
|
-
if (previousTurns === 0) {
|
|
274
|
-
const name = extractSessionName(text);
|
|
275
|
-
if (name) setSessionName(chatId, name);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// ── Delivery — decision tree shared with the OpenCode backend ──────────────
|
|
279
|
-
const delivery = await routeDelivery({
|
|
280
|
-
backendLabel: "Kilo",
|
|
281
|
-
chatId,
|
|
282
|
-
state,
|
|
283
|
-
responseText,
|
|
284
|
-
onTextBlock,
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
log(
|
|
288
|
-
"agent",
|
|
289
|
-
`[${chatId}] delivery: ${delivery.route} (${delivery.chars} chars)`,
|
|
290
|
-
);
|
|
291
|
-
|
|
292
|
-
log(
|
|
293
|
-
"agent",
|
|
294
|
-
`[${chatId}] -> (${summarizeUsage(
|
|
295
|
-
{
|
|
296
|
-
inputTokens: state.sdkInputTokens,
|
|
297
|
-
outputTokens: state.sdkOutputTokens,
|
|
298
|
-
cacheRead: state.sdkCacheRead,
|
|
299
|
-
cacheWrite: state.sdkCacheWrite,
|
|
300
|
-
},
|
|
301
|
-
{ durationMs, toolCalls: state.toolCalls },
|
|
302
|
-
)} terminator=${state.turnTerminated ? "yes" : "no"} ` +
|
|
303
|
-
`delivered=${state.deliveredTextNorms.length} ` +
|
|
304
|
-
`respLen=${responseText.length} ` +
|
|
305
|
-
`setup=${setupMs}ms turn=${promptMs}ms ` +
|
|
306
|
-
`events=${formatEventCounts(state.eventCounts)})`,
|
|
37
|
+
params,
|
|
307
38
|
);
|
|
308
|
-
traceMessage(chatId, "out", responseText, {
|
|
309
|
-
durationMs,
|
|
310
|
-
toolCalls: state.toolCalls,
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
return {
|
|
314
|
-
text: responseText,
|
|
315
|
-
durationMs,
|
|
316
|
-
inputTokens: state.sdkInputTokens,
|
|
317
|
-
outputTokens: state.sdkOutputTokens,
|
|
318
|
-
cacheRead: state.sdkCacheRead,
|
|
319
|
-
cacheWrite: state.sdkCacheWrite,
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Compact summary of which SSE event types fired this turn. `none` for a
|
|
325
|
-
* silent turn (itself a useful diagnostic — the SSE socket dropped or never
|
|
326
|
-
* matched our session id). Otherwise renders as `delta×42,part.updated×1`.
|
|
327
|
-
*/
|
|
328
|
-
function formatEventCounts(counts: Record<string, number>): string {
|
|
329
|
-
const entries = Object.entries(counts);
|
|
330
|
-
if (entries.length === 0) return "none";
|
|
331
|
-
// Trim the noisy `message.` / `session.` prefixes so the line stays
|
|
332
|
-
// readable in the live log tail.
|
|
333
|
-
return entries
|
|
334
|
-
.map(([type, n]) => `${type.replace(/^(message|session)\./, "")}×${n}`)
|
|
335
|
-
.join(",");
|
|
336
39
|
}
|