talon-agent 1.33.0 → 1.34.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 +3 -3
- package/src/backend/claude-sdk/handler.ts +13 -0
- package/src/backend/claude-sdk/stream.ts +33 -0
- package/src/bootstrap.ts +5 -1
- package/src/core/agent-runtime/events.ts +4 -0
- package/src/core/engine/gateway.ts +7 -6
- package/src/core/errors.ts +62 -4
- package/src/core/tools/index.ts +29 -0
- package/src/core/tools/messaging.ts +5 -0
- package/src/core/tools/types.ts +11 -0
- package/src/core/weaver/index.ts +9 -2
- package/src/core/weaver/loom.ts +33 -2
- package/src/core/weaver/memory-prefetch.ts +49 -0
- package/src/core/weaver/shuttle.ts +63 -0
- package/src/core/weaver/typing-loop.ts +36 -0
- package/src/core/weaver/warp-resolver.ts +123 -0
- package/src/core/weaver/weaver.ts +118 -209
- package/src/frontend/native/index.ts +17 -2
- package/src/frontend/native/logs.ts +95 -0
- package/src/frontend/native/protocol.ts +39 -0
- package/src/frontend/native/server.ts +25 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WarpResolver — turns an incoming turn into the model/backend binding
|
|
3
|
+
* (the warp) it will run on.
|
|
4
|
+
*
|
|
5
|
+
* Owns the whole resolution ladder so the Weaver never sees a partial
|
|
6
|
+
* state: active-model resolution, the send-time null-model guard, and
|
|
7
|
+
* the per-run override (triggers/cron) with its fall-back-to-chat-model
|
|
8
|
+
* safety. The result is a discriminated union — either a bound warp or
|
|
9
|
+
* a refusal with the user-facing message to deliver — so callers can't
|
|
10
|
+
* forget to handle the "no model" case.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { ModelRef } from "../agent-runtime/model-ref.js";
|
|
14
|
+
import { logDebug, logWarn } from "../../util/log.js";
|
|
15
|
+
|
|
16
|
+
export type WarpResolverDeps = {
|
|
17
|
+
/**
|
|
18
|
+
* Walks the 5-step active-model resolution chain for the chat. When
|
|
19
|
+
* `model`/`ref` are both `null` (catalog-driven backend with no
|
|
20
|
+
* per-chat pick and no operator default) the turn is refused — see
|
|
21
|
+
* `resolve()`.
|
|
22
|
+
*/
|
|
23
|
+
resolveActiveModel: (chatId: string) => Promise<{
|
|
24
|
+
model: string | null;
|
|
25
|
+
ref: ModelRef | null;
|
|
26
|
+
backendId: string;
|
|
27
|
+
}>;
|
|
28
|
+
/**
|
|
29
|
+
* Validates + materialises an explicit per-run model id against the
|
|
30
|
+
* chat's backend. Returns `null` when the id isn't selectable, so
|
|
31
|
+
* the resolver falls back to the chat model. Restricted to the
|
|
32
|
+
* chat's own backend so the session still resumes.
|
|
33
|
+
*/
|
|
34
|
+
resolveModelOverride?: (
|
|
35
|
+
chatId: string,
|
|
36
|
+
modelId: string,
|
|
37
|
+
) => Promise<ModelRef | null>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type ResolveWarpInput = {
|
|
41
|
+
chatId: string;
|
|
42
|
+
/** Optional per-run model override (triggers/cron). */
|
|
43
|
+
modelOverride?: string;
|
|
44
|
+
/** Turn source, used only for override logging. */
|
|
45
|
+
source: string;
|
|
46
|
+
/** Request id for log correlation. */
|
|
47
|
+
reqId: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type WarpResolution =
|
|
51
|
+
| {
|
|
52
|
+
ok: true;
|
|
53
|
+
/** The ModelRef the turn runs on (post-override). */
|
|
54
|
+
ref: ModelRef;
|
|
55
|
+
backendId: string;
|
|
56
|
+
/** A per-run override was applied for this turn. */
|
|
57
|
+
overridden: boolean;
|
|
58
|
+
}
|
|
59
|
+
| {
|
|
60
|
+
ok: false;
|
|
61
|
+
backendId: string;
|
|
62
|
+
/** User-facing refusal ("use /model to pick one"). */
|
|
63
|
+
message: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export async function resolveWarp(
|
|
67
|
+
deps: WarpResolverDeps,
|
|
68
|
+
input: ResolveWarpInput,
|
|
69
|
+
): Promise<WarpResolution> {
|
|
70
|
+
const { chatId, modelOverride, source, reqId } = input;
|
|
71
|
+
|
|
72
|
+
// Send-time null-model guard. When the active-model resolver returns
|
|
73
|
+
// no usable model, refuse to call the backend — it would either error
|
|
74
|
+
// opaquely or run on the wrong default.
|
|
75
|
+
const { model, ref, backendId } = await deps.resolveActiveModel(chatId);
|
|
76
|
+
if (model === null || ref === null) {
|
|
77
|
+
logWarn(
|
|
78
|
+
"dispatcher",
|
|
79
|
+
`[${reqId}] refusing query: no model resolved (chat=${chatId}, backend=${backendId})`,
|
|
80
|
+
);
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
backendId,
|
|
84
|
+
message:
|
|
85
|
+
`No model selected for backend \`${backendId}\`. ` +
|
|
86
|
+
`Use /model to pick one — or set ` +
|
|
87
|
+
`\`backendDefaults.${backendId}\` in talon.json to apply a ` +
|
|
88
|
+
`default for all chats on this backend.`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Per-run model override (triggers/cron). Resolve against the chat's
|
|
93
|
+
// backend; on success swap the ref for this turn only, on failure fall
|
|
94
|
+
// back to the chat model so a stale override never kills the run.
|
|
95
|
+
let runRef = ref;
|
|
96
|
+
if (modelOverride && deps.resolveModelOverride) {
|
|
97
|
+
try {
|
|
98
|
+
const overrideRef = await deps.resolveModelOverride(
|
|
99
|
+
chatId,
|
|
100
|
+
modelOverride,
|
|
101
|
+
);
|
|
102
|
+
if (overrideRef) {
|
|
103
|
+
runRef = overrideRef;
|
|
104
|
+
logDebug(
|
|
105
|
+
"dispatcher",
|
|
106
|
+
`[${reqId}] model override → ${modelOverride} (${source})`,
|
|
107
|
+
);
|
|
108
|
+
} else {
|
|
109
|
+
logWarn(
|
|
110
|
+
"dispatcher",
|
|
111
|
+
`[${reqId}] model override "${modelOverride}" not resolvable on backend ${backendId}; using chat model`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
} catch (err) {
|
|
115
|
+
logWarn(
|
|
116
|
+
"dispatcher",
|
|
117
|
+
`[${reqId}] model override resolution threw: ${err instanceof Error ? err.message : String(err)}; using chat model`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return { ok: true, ref: runRef, backendId, overridden: runRef !== ref };
|
|
123
|
+
}
|
|
@@ -1,18 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Weaver — the turn orchestrator. `runTurn` serializes each turn onto
|
|
3
|
+
* its chat's Thread (per-chat FIFO, cross-chat parallel) and drives the
|
|
4
|
+
* turn lifecycle by composing the weaver's single-purpose collaborators:
|
|
5
|
+
*
|
|
6
|
+
* - `resolveWarp` (warp-resolver.ts) — model/backend binding + the
|
|
7
|
+
* null-model guard and per-run override fallback;
|
|
8
|
+
* - `startTypingLoop` (typing-loop.ts) — keeps the frontend's typing
|
|
9
|
+
* indicator alive for the duration of the turn;
|
|
10
|
+
* - `prefetchMemory` (memory-prefetch.ts) — optional fail-closed
|
|
11
|
+
* palace pre-retrieval for live user messages;
|
|
12
|
+
* - `carryTurnEvents` (shuttle.ts) — pumps the backend's AgentEvent
|
|
13
|
+
* stream into the frontend sink, settles delivery acks, captures
|
|
14
|
+
* the result and rethrows error terminators.
|
|
15
|
+
*
|
|
16
|
+
* The Weaver itself only sequences those stages and brackets them with
|
|
17
|
+
* the Thread's execution context — it holds no per-chat state (that's
|
|
18
|
+
* the Thread's) and no policy of its own beyond ordering.
|
|
19
|
+
*/
|
|
20
|
+
|
|
1
21
|
import { randomBytes } from "node:crypto";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
RetrievedMemory,
|
|
5
|
-
} from "../agent-runtime/capabilities.js";
|
|
6
|
-
import { AgentRunError, type AgentResult } from "../agent-runtime/events.js";
|
|
22
|
+
import type { Backend } from "../agent-runtime/capabilities.js";
|
|
23
|
+
import type { AgentResult } from "../agent-runtime/events.js";
|
|
7
24
|
import type { ModelRef } from "../agent-runtime/model-ref.js";
|
|
8
|
-
import { maybeStartDream } from "../background/dream.js";
|
|
9
25
|
import type { MemoryRetriever } from "../memory/retrieval.js";
|
|
10
26
|
import type { ContextManager, ExecuteParams, ExecuteResult } from "../types.js";
|
|
11
27
|
import { log, logDebug, logWarn } from "../../util/log.js";
|
|
12
28
|
import { Loom } from "./loom.js";
|
|
29
|
+
import { prefetchMemory } from "./memory-prefetch.js";
|
|
30
|
+
import { carryTurnEvents } from "./shuttle.js";
|
|
13
31
|
import type { Thread, ThreadSnapshot } from "./thread.js";
|
|
32
|
+
import { startTypingLoop } from "./typing-loop.js";
|
|
33
|
+
import { resolveWarp } from "./warp-resolver.js";
|
|
14
34
|
|
|
15
35
|
export type WeaverDeps = {
|
|
36
|
+
/**
|
|
37
|
+
* Read fresh per call so backend swaps (chat-role rebinds or
|
|
38
|
+
* per-chat overrides via the controller) take effect on the next
|
|
39
|
+
* query without a dispatcher re-init.
|
|
40
|
+
*/
|
|
16
41
|
getBackend: (chatId?: string) => Backend;
|
|
17
42
|
resolveActiveModel: (chatId: string) => Promise<{
|
|
18
43
|
model: string | null;
|
|
@@ -26,6 +51,14 @@ export type WeaverDeps = {
|
|
|
26
51
|
context: ContextManager;
|
|
27
52
|
sendTyping: (chatId: number, stringId?: string) => Promise<void>;
|
|
28
53
|
onActivity: () => void;
|
|
54
|
+
/**
|
|
55
|
+
* Optional fire-and-forget hook invoked at the start of every turn,
|
|
56
|
+
* after the warp is bound and before the backend is called. The
|
|
57
|
+
* composition root wires background work here (e.g. dream memory
|
|
58
|
+
* consolidation) so the Weaver stays ignorant of those subsystems.
|
|
59
|
+
* Must not throw and must not block — it is called synchronously.
|
|
60
|
+
*/
|
|
61
|
+
onTurnStart?: () => void;
|
|
29
62
|
/**
|
|
30
63
|
* Optional memory pre-retrieval (Phase B). Called for `source: "message"`
|
|
31
64
|
* turns after model/backend resolution and context acquisition, before
|
|
@@ -38,6 +71,7 @@ export type WeaverDeps = {
|
|
|
38
71
|
export class Weaver {
|
|
39
72
|
readonly loom: Loom;
|
|
40
73
|
private readonly deps: WeaverDeps;
|
|
74
|
+
private activeCount = 0;
|
|
41
75
|
|
|
42
76
|
constructor(deps: WeaverDeps, loom = new Loom()) {
|
|
43
77
|
this.deps = deps;
|
|
@@ -49,6 +83,7 @@ export class Weaver {
|
|
|
49
83
|
return thread.enqueue(() => this.run(thread, params));
|
|
50
84
|
}
|
|
51
85
|
|
|
86
|
+
/** Number of turns currently running (not queued) across all chats. */
|
|
52
87
|
getActiveCount(): number {
|
|
53
88
|
return this.activeCount;
|
|
54
89
|
}
|
|
@@ -59,14 +94,9 @@ export class Weaver {
|
|
|
59
94
|
* frontends. Reading is side-effect free.
|
|
60
95
|
*/
|
|
61
96
|
snapshot(): ThreadSnapshot[] {
|
|
62
|
-
return this.loom
|
|
63
|
-
.chatIds()
|
|
64
|
-
.map((id) => this.loom.get(id)?.describe())
|
|
65
|
-
.filter((s): s is ThreadSnapshot => s !== undefined);
|
|
97
|
+
return this.loom.snapshot();
|
|
66
98
|
}
|
|
67
99
|
|
|
68
|
-
private activeCount = 0;
|
|
69
|
-
|
|
70
100
|
private async run(
|
|
71
101
|
thread: Thread,
|
|
72
102
|
params: ExecuteParams,
|
|
@@ -83,95 +113,32 @@ export class Weaver {
|
|
|
83
113
|
thread: Thread,
|
|
84
114
|
params: ExecuteParams,
|
|
85
115
|
): Promise<ExecuteResult> {
|
|
86
|
-
const {
|
|
87
|
-
|
|
88
|
-
resolveActiveModel,
|
|
89
|
-
resolveModelOverride,
|
|
90
|
-
context,
|
|
91
|
-
sendTyping,
|
|
92
|
-
onActivity,
|
|
93
|
-
retrieveMemory,
|
|
94
|
-
} = this.deps;
|
|
95
|
-
// Read the backend fresh per call so backend swaps (chat-role
|
|
96
|
-
// rebinds or per-chat overrides via the controller) take effect on
|
|
97
|
-
// the next query without a dispatcher re-init.
|
|
98
|
-
const backend = getBackend(params.chatId);
|
|
116
|
+
const { context, onActivity, retrieveMemory } = this.deps;
|
|
117
|
+
const backend = this.deps.getBackend(params.chatId);
|
|
99
118
|
const reqId = randomBytes(4).toString("hex");
|
|
100
119
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
backendId,
|
|
112
|
-
} = await resolveActiveModel(params.chatId);
|
|
113
|
-
if (resolvedModel === null || resolvedRef === null) {
|
|
114
|
-
const message =
|
|
115
|
-
`No model selected for backend \`${backendId}\`. ` +
|
|
116
|
-
`Use /model to pick one — or set ` +
|
|
117
|
-
`\`backendDefaults.${backendId}\` in talon.json to apply a ` +
|
|
118
|
-
`default for all chats on this backend.`;
|
|
119
|
-
logWarn(
|
|
120
|
-
"dispatcher",
|
|
121
|
-
`[${reqId}] refusing query: no model resolved (chat=${params.chatId}, backend=${backendId})`,
|
|
122
|
-
);
|
|
120
|
+
const warp = await resolveWarp(this.deps, {
|
|
121
|
+
chatId: params.chatId,
|
|
122
|
+
modelOverride: params.modelOverride,
|
|
123
|
+
source: params.source,
|
|
124
|
+
reqId,
|
|
125
|
+
});
|
|
126
|
+
if (!warp.ok) {
|
|
127
|
+
// Refusals are delivered through the same event sink the backend
|
|
128
|
+
// would use for output (as an `assistant_message` event, so the
|
|
129
|
+
// frontend delivers it normally).
|
|
123
130
|
try {
|
|
124
|
-
await params.onEvent?.({
|
|
131
|
+
await params.onEvent?.({
|
|
132
|
+
type: "assistant_message",
|
|
133
|
+
text: warp.message,
|
|
134
|
+
});
|
|
125
135
|
} catch (err) {
|
|
126
136
|
logWarn(
|
|
127
137
|
"dispatcher",
|
|
128
138
|
`onEvent(no-model) threw: ${err instanceof Error ? err.message : String(err)}`,
|
|
129
139
|
);
|
|
130
140
|
}
|
|
131
|
-
return
|
|
132
|
-
text: message,
|
|
133
|
-
durationMs: 0,
|
|
134
|
-
inputTokens: 0,
|
|
135
|
-
outputTokens: 0,
|
|
136
|
-
cacheRead: 0,
|
|
137
|
-
cacheWrite: 0,
|
|
138
|
-
bridgeMessageCount: context.getMessageCount(
|
|
139
|
-
params.numericChatId,
|
|
140
|
-
params.chatId,
|
|
141
|
-
),
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Per-run model override (triggers/cron). Resolve against the chat's
|
|
146
|
-
// backend; on success swap the ref for this turn only, on failure fall
|
|
147
|
-
// back to the chat model so a stale override never kills the run. The
|
|
148
|
-
// override is restricted to the chat's own backend, so the session still
|
|
149
|
-
// resumes — only the model changes.
|
|
150
|
-
let runRef = resolvedRef;
|
|
151
|
-
if (params.modelOverride && resolveModelOverride) {
|
|
152
|
-
try {
|
|
153
|
-
const overrideRef = await resolveModelOverride(
|
|
154
|
-
params.chatId,
|
|
155
|
-
params.modelOverride,
|
|
156
|
-
);
|
|
157
|
-
if (overrideRef) {
|
|
158
|
-
runRef = overrideRef;
|
|
159
|
-
logDebug(
|
|
160
|
-
"dispatcher",
|
|
161
|
-
`[${reqId}] model override → ${params.modelOverride} (${params.source})`,
|
|
162
|
-
);
|
|
163
|
-
} else {
|
|
164
|
-
logWarn(
|
|
165
|
-
"dispatcher",
|
|
166
|
-
`[${reqId}] model override "${params.modelOverride}" not resolvable on backend ${backendId}; using chat model`,
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
} catch (err) {
|
|
170
|
-
logWarn(
|
|
171
|
-
"dispatcher",
|
|
172
|
-
`[${reqId}] model override resolution threw: ${err instanceof Error ? err.message : String(err)}; using chat model`,
|
|
173
|
-
);
|
|
174
|
-
}
|
|
141
|
+
return this.emptyResult(warp.message, params);
|
|
175
142
|
}
|
|
176
143
|
|
|
177
144
|
// Bind the warp — record the model/backend actually resolved for this turn
|
|
@@ -179,158 +146,105 @@ export class Weaver {
|
|
|
179
146
|
// last turn (per-chat rebind, per-run override, or config drift) is logged
|
|
180
147
|
// rather than passing silently.
|
|
181
148
|
const { drifted, previous } = thread.bindWarp({
|
|
182
|
-
model:
|
|
183
|
-
backendId,
|
|
184
|
-
overridden:
|
|
149
|
+
model: warp.ref.id,
|
|
150
|
+
backendId: warp.backendId,
|
|
151
|
+
overridden: warp.overridden,
|
|
185
152
|
boundAt: Date.now(),
|
|
186
153
|
});
|
|
187
154
|
if (drifted && previous) {
|
|
188
155
|
logDebug(
|
|
189
156
|
"dispatcher",
|
|
190
|
-
`[${reqId}] warp drift chat=${params.chatId}: ${previous.backendId}/${previous.model} → ${backendId}/${
|
|
157
|
+
`[${reqId}] warp drift chat=${params.chatId}: ${previous.backendId}/${previous.model} → ${warp.backendId}/${warp.ref.id}`,
|
|
191
158
|
);
|
|
192
159
|
}
|
|
193
160
|
|
|
194
|
-
|
|
195
|
-
maybeStartDream();
|
|
161
|
+
this.deps.onTurnStart?.();
|
|
196
162
|
|
|
197
163
|
logDebug(
|
|
198
164
|
"dispatcher",
|
|
199
165
|
`[${reqId}] ${params.source} chat=${params.chatId} started (active=${this.activeCount})`,
|
|
200
166
|
);
|
|
201
167
|
context.acquire(params.numericChatId, params.chatId);
|
|
202
|
-
|
|
203
|
-
|
|
168
|
+
const stopTyping = startTypingLoop(
|
|
169
|
+
this.deps.sendTyping,
|
|
170
|
+
params.numericChatId,
|
|
171
|
+
params.chatId,
|
|
172
|
+
);
|
|
204
173
|
try {
|
|
205
|
-
await sendTyping(params.numericChatId, params.chatId).catch(
|
|
206
|
-
(err: unknown) => {
|
|
207
|
-
logWarn(
|
|
208
|
-
"dispatcher",
|
|
209
|
-
`sendTyping failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
210
|
-
);
|
|
211
|
-
},
|
|
212
|
-
);
|
|
213
|
-
typingTimer = setInterval(() => {
|
|
214
|
-
sendTyping(params.numericChatId, params.chatId).catch(
|
|
215
|
-
(err: unknown) => {
|
|
216
|
-
logWarn(
|
|
217
|
-
"dispatcher",
|
|
218
|
-
`sendTyping interval failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
219
|
-
);
|
|
220
|
-
},
|
|
221
|
-
);
|
|
222
|
-
}, 4000);
|
|
223
|
-
|
|
224
|
-
// Consume the backend's native `AgentEvent` stream and forward
|
|
225
|
-
// every event straight to the frontend's `onEvent` sink (no
|
|
226
|
-
// callback bridge). Capture the `completed` event's `AgentResult`
|
|
227
|
-
// for the dispatcher's return value, and rethrow an `error`
|
|
228
|
-
// terminator as `AgentRunError` so callers' catch paths keep
|
|
229
|
-
// working. Events are awaited in stream order so a consumer that
|
|
230
|
-
// needs serial delivery gets it.
|
|
231
174
|
if (!backend.chat) {
|
|
232
175
|
throw new Error(
|
|
233
176
|
`Backend "${backend.id}" has no chat capability — cannot run a turn.`,
|
|
234
177
|
);
|
|
235
178
|
}
|
|
236
179
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
text: params.prompt,
|
|
248
|
-
senderName: params.senderName,
|
|
249
|
-
isGroup: params.isGroup,
|
|
250
|
-
});
|
|
251
|
-
if (retrievedMemory) {
|
|
252
|
-
logDebug(
|
|
253
|
-
"dispatcher",
|
|
254
|
-
`[${reqId}] memory pre-retrieval: ${retrievedMemory.items.length} item(s), ` +
|
|
255
|
-
`${retrievedMemory.items.reduce((n, i) => n + i.text.length, 0)} chars`,
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
} catch (err) {
|
|
259
|
-
logWarn(
|
|
260
|
-
"dispatcher",
|
|
261
|
-
`[${reqId}] memory pre-retrieval failed (running turn without it): ` +
|
|
262
|
-
(err instanceof Error ? err.message : String(err)),
|
|
263
|
-
);
|
|
264
|
-
retrievedMemory = undefined;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
180
|
+
const retrievedMemory =
|
|
181
|
+
retrieveMemory && params.source === "message"
|
|
182
|
+
? await prefetchMemory(retrieveMemory, {
|
|
183
|
+
chatId: params.chatId,
|
|
184
|
+
text: params.prompt,
|
|
185
|
+
senderName: params.senderName,
|
|
186
|
+
isGroup: params.isGroup,
|
|
187
|
+
reqId,
|
|
188
|
+
})
|
|
189
|
+
: undefined;
|
|
267
190
|
|
|
268
191
|
const stream = backend.chat.runChatTurn({
|
|
269
192
|
chatId: params.chatId,
|
|
270
|
-
model:
|
|
193
|
+
model: warp.ref,
|
|
271
194
|
text: params.prompt,
|
|
272
195
|
senderName: params.senderName,
|
|
273
196
|
isGroup: params.isGroup,
|
|
274
197
|
messageId: params.messageId,
|
|
275
198
|
retrievedMemory,
|
|
276
199
|
});
|
|
277
|
-
|
|
278
|
-
for await (const event of stream) {
|
|
279
|
-
if (event.type === "completed") {
|
|
280
|
-
agentResult = event.result;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// The dispatcher owns `assistant_message.deliveryAck` settlement
|
|
284
|
-
// so it is ALWAYS resolved — even when no `onEvent` sink is
|
|
285
|
-
// supplied, or the sink ignores the event. Otherwise the
|
|
286
|
-
// callback-shaped backend (handler-to-events) blocks forever
|
|
287
|
-
// awaiting delivery confirmation. The frontend's job is just to
|
|
288
|
-
// deliver and throw on failure; the dispatcher maps that onto the
|
|
289
|
-
// ack (resolve on success → block delivered; reject on throw →
|
|
290
|
-
// backend retries, e.g. Codex oversized-message path).
|
|
291
|
-
if (event.type === "assistant_message" && event.deliveryAck) {
|
|
292
|
-
try {
|
|
293
|
-
await params.onEvent?.(event);
|
|
294
|
-
event.deliveryAck.resolve();
|
|
295
|
-
} catch (err) {
|
|
296
|
-
event.deliveryAck.reject(err);
|
|
297
|
-
}
|
|
298
|
-
continue;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
await params.onEvent?.(event);
|
|
302
|
-
if (event.type === "error") {
|
|
303
|
-
throw new AgentRunError(event.error);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
const result = {
|
|
307
|
-
text: agentResult?.text ?? "",
|
|
308
|
-
durationMs: agentResult?.durationMs ?? 0,
|
|
309
|
-
inputTokens: agentResult?.usage.inputTokens ?? 0,
|
|
310
|
-
outputTokens: agentResult?.usage.outputTokens ?? 0,
|
|
311
|
-
cacheRead: agentResult?.usage.cacheRead ?? 0,
|
|
312
|
-
cacheWrite: agentResult?.usage.cacheWrite ?? 0,
|
|
313
|
-
};
|
|
200
|
+
const agentResult = await carryTurnEvents(stream, params.onEvent);
|
|
314
201
|
|
|
315
202
|
onActivity();
|
|
316
|
-
|
|
317
203
|
logDebug(
|
|
318
204
|
"dispatcher",
|
|
319
|
-
`[${reqId}] completed in ${
|
|
205
|
+
`[${reqId}] completed in ${agentResult?.durationMs ?? 0}ms ` +
|
|
206
|
+
`(in=${agentResult?.usage.inputTokens ?? 0} out=${agentResult?.usage.outputTokens ?? 0})`,
|
|
320
207
|
);
|
|
321
208
|
|
|
322
|
-
return
|
|
323
|
-
...result,
|
|
324
|
-
bridgeMessageCount: context.getMessageCount(
|
|
325
|
-
params.numericChatId,
|
|
326
|
-
params.chatId,
|
|
327
|
-
),
|
|
328
|
-
};
|
|
209
|
+
return this.toExecuteResult(agentResult, params);
|
|
329
210
|
} finally {
|
|
330
|
-
|
|
211
|
+
stopTyping();
|
|
331
212
|
context.release(params.numericChatId, params.chatId);
|
|
332
213
|
}
|
|
333
214
|
}
|
|
215
|
+
|
|
216
|
+
private toExecuteResult(
|
|
217
|
+
result: AgentResult | undefined,
|
|
218
|
+
params: ExecuteParams,
|
|
219
|
+
): ExecuteResult {
|
|
220
|
+
return {
|
|
221
|
+
text: result?.text ?? "",
|
|
222
|
+
durationMs: result?.durationMs ?? 0,
|
|
223
|
+
inputTokens: result?.usage.inputTokens ?? 0,
|
|
224
|
+
outputTokens: result?.usage.outputTokens ?? 0,
|
|
225
|
+
cacheRead: result?.usage.cacheRead ?? 0,
|
|
226
|
+
cacheWrite: result?.usage.cacheWrite ?? 0,
|
|
227
|
+
bridgeMessageCount: this.deps.context.getMessageCount(
|
|
228
|
+
params.numericChatId,
|
|
229
|
+
params.chatId,
|
|
230
|
+
),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private emptyResult(text: string, params: ExecuteParams): ExecuteResult {
|
|
235
|
+
return {
|
|
236
|
+
text,
|
|
237
|
+
durationMs: 0,
|
|
238
|
+
inputTokens: 0,
|
|
239
|
+
outputTokens: 0,
|
|
240
|
+
cacheRead: 0,
|
|
241
|
+
cacheWrite: 0,
|
|
242
|
+
bridgeMessageCount: this.deps.context.getMessageCount(
|
|
243
|
+
params.numericChatId,
|
|
244
|
+
params.chatId,
|
|
245
|
+
),
|
|
246
|
+
};
|
|
247
|
+
}
|
|
334
248
|
}
|
|
335
249
|
|
|
336
250
|
let weaver: Weaver | null = null;
|
|
@@ -341,11 +255,6 @@ export function initWeaver(deps: WeaverDeps): Weaver {
|
|
|
341
255
|
return weaver;
|
|
342
256
|
}
|
|
343
257
|
|
|
344
|
-
export function getWeaver(): Weaver {
|
|
345
|
-
if (!weaver) throw new Error("Weaver not initialized");
|
|
346
|
-
return weaver;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
258
|
/**
|
|
350
259
|
* The active Weaver's Loom, or `null` before the dispatcher is wired. The
|
|
351
260
|
* gateway delegates its per-chat context bookkeeping here so the Loom is the
|
|
@@ -22,7 +22,7 @@ import { mkdir, writeFile } from "node:fs/promises";
|
|
|
22
22
|
import { basename, dirname, join, resolve } from "node:path";
|
|
23
23
|
import { spawn } from "node:child_process";
|
|
24
24
|
import { log, logError } from "../../util/log.js";
|
|
25
|
-
import { dirs } from "../../util/paths.js";
|
|
25
|
+
import { dirs, files } from "../../util/paths.js";
|
|
26
26
|
import { PKG_ROOT } from "../../cli/context.js";
|
|
27
27
|
import { getSessionInfo } from "../../storage/sessions.js";
|
|
28
28
|
import { buildContextDisplay } from "../shared/status-context.js";
|
|
@@ -30,6 +30,7 @@ import { resolveActiveModelForChat } from "../../core/models/active-model.js";
|
|
|
30
30
|
import { forceDream } from "../../core/background/dream.js";
|
|
31
31
|
import { execute } from "../../core/engine/dispatcher.js";
|
|
32
32
|
import { toolInputToRecord } from "../../core/agent-runtime/events.js";
|
|
33
|
+
import { isDeliveryTool } from "../../core/tools/index.js";
|
|
33
34
|
import {
|
|
34
35
|
pushMessage,
|
|
35
36
|
getRecentHistory,
|
|
@@ -66,6 +67,7 @@ import { extractSessionName } from "../../backend/shared/session-name.js";
|
|
|
66
67
|
import { BridgeServer, type BridgeServerHandlers } from "./server.js";
|
|
67
68
|
import { createNativeActionHandler } from "./actions.js";
|
|
68
69
|
import { removeBridgeDiscovery, writeBridgeDiscovery } from "./discovery.js";
|
|
70
|
+
import { readLogEntries } from "./logs.js";
|
|
69
71
|
import {
|
|
70
72
|
BRIDGE_PROTOCOL_VERSION,
|
|
71
73
|
BOT_SENDER_ID,
|
|
@@ -575,6 +577,12 @@ export function createNativeFrontend(
|
|
|
575
577
|
broadcast({ kind: "delta", chatId: entry.id, text: event.text });
|
|
576
578
|
break;
|
|
577
579
|
case "tool_call": {
|
|
580
|
+
// Delivery plumbing (end_turn / send_message / react) never
|
|
581
|
+
// enters the tool timeline — its effect arrives as the
|
|
582
|
+
// `message`/reaction itself. Skipping here keeps the live
|
|
583
|
+
// stream, the mid-turn replay to late joiners, and the
|
|
584
|
+
// persisted turn meta consistent from one choke point.
|
|
585
|
+
if (isDeliveryTool(event.name)) break;
|
|
578
586
|
const input = toolInputToRecord(event.name, event.input);
|
|
579
587
|
turnTools.set(event.id, {
|
|
580
588
|
call: { id: event.id, name: event.name, input },
|
|
@@ -591,6 +599,7 @@ export function createNativeFrontend(
|
|
|
591
599
|
break;
|
|
592
600
|
}
|
|
593
601
|
case "tool_result": {
|
|
602
|
+
if (isDeliveryTool(event.name)) break;
|
|
594
603
|
const output = summarizeToolResult(event.result);
|
|
595
604
|
const live = turnTools.get(event.id);
|
|
596
605
|
if (live) {
|
|
@@ -1026,7 +1035,11 @@ export function createNativeFrontend(
|
|
|
1026
1035
|
if (msg.role === "assistant") {
|
|
1027
1036
|
const meta = getTurnMeta(id, msg.id);
|
|
1028
1037
|
if (meta) {
|
|
1029
|
-
|
|
1038
|
+
// Delivery tools are excluded at record time, but metas
|
|
1039
|
+
// persisted by older daemons still carry them — filter on
|
|
1040
|
+
// the way out so upgraded installs get clean history too.
|
|
1041
|
+
const tools = meta.tools?.filter((t) => !isDeliveryTool(t.name));
|
|
1042
|
+
if (tools?.length) msg.tools = tools;
|
|
1030
1043
|
if (meta.durationMs) msg.durationMs = meta.durationMs;
|
|
1031
1044
|
if (meta.tokensIn) msg.tokensIn = meta.tokensIn;
|
|
1032
1045
|
if (meta.tokensOut) msg.tokensOut = meta.tokensOut;
|
|
@@ -1144,6 +1157,8 @@ export function createNativeFrontend(
|
|
|
1144
1157
|
return snap;
|
|
1145
1158
|
},
|
|
1146
1159
|
control,
|
|
1160
|
+
logs: ({ lines, minLevel, component }) =>
|
|
1161
|
+
readLogEntries(files.log, { limit: lines, minLevel, component }),
|
|
1147
1162
|
liveTurnEvents,
|
|
1148
1163
|
mediaPath: (id) => media.get(id) ?? null,
|
|
1149
1164
|
};
|