@nexrall/code-core 1.4.13 → 1.4.15
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/dist/agent/loop.d.ts +13 -0
- package/dist/agent/loop.d.ts.map +1 -1
- package/dist/agent/loop.js +150 -16
- package/dist/api/client.d.ts +31 -1
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +636 -57
- package/dist/types.d.ts +111 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +44 -0
- package/package.json +1 -1
package/dist/types.d.ts
CHANGED
|
@@ -46,6 +46,24 @@ export interface SSEMessageCompleteEvent {
|
|
|
46
46
|
export interface SSEUsageEvent {
|
|
47
47
|
type: 'usage';
|
|
48
48
|
usage: UsageInfo;
|
|
49
|
+
/**
|
|
50
|
+
* True when these tokens belong to an attempt that was CUT SHORT (the client
|
|
51
|
+
* disconnected or the turn was restarted) rather than to the turn's completed
|
|
52
|
+
* response. They were still billed — the upstream vendor charged for every token
|
|
53
|
+
* generated before the cut — so this is real spend, but a UI that shows "this
|
|
54
|
+
* turn's output" should not add it to the successful attempt's count or it will
|
|
55
|
+
* appear to double-report. Sum it for COST, keep it out of a single turn's token
|
|
56
|
+
* total. Absent/false for an ordinary completed turn.
|
|
57
|
+
*/
|
|
58
|
+
partial?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* True when this usage is a RE-REPORT of a turn that was already billed. The previous
|
|
61
|
+
* attempt completed and was charged, but its `done` never reached the client, so the
|
|
62
|
+
* client retried and the backend served the stored answer from its turn-idempotency
|
|
63
|
+
* cache instead of re-invoking the model. No new charge occurred — a cost display must
|
|
64
|
+
* not add these tokens again. Mutually exclusive with `partial`.
|
|
65
|
+
*/
|
|
66
|
+
replayed?: boolean;
|
|
49
67
|
}
|
|
50
68
|
export interface SSEDoneEvent {
|
|
51
69
|
type: 'done';
|
|
@@ -97,7 +115,40 @@ export interface SSEBalanceStatusEvent {
|
|
|
97
115
|
balance: number;
|
|
98
116
|
zero: boolean;
|
|
99
117
|
}
|
|
100
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Emitted when a stream attempt died AFTER partial output had already been
|
|
120
|
+
* rendered, and the network layer is restarting the SAME turn from scratch.
|
|
121
|
+
*
|
|
122
|
+
* Unlike `retry` (which only ever fires when nothing was shown yet, so the UI
|
|
123
|
+
* merely needs a "reconnecting…" hint), this one carries a hard requirement:
|
|
124
|
+
* the consumer MUST discard the partial assistant output it has rendered for
|
|
125
|
+
* this turn — the text/thinking streamed so far will be re-sent in full by the
|
|
126
|
+
* new attempt, so keeping it would duplicate it on screen.
|
|
127
|
+
*
|
|
128
|
+
* LOCAL side effects are safe: tool calls are dispatched by the agent loop from
|
|
129
|
+
* the COMPLETED message, after `streamChat` returns, and a restart means it threw
|
|
130
|
+
* instead — so no tool ran, no file changed, no checkpoint or hook fired.
|
|
131
|
+
*
|
|
132
|
+
* BILLING IS NOT ROLLED BACK, and callers should not assume otherwise. The
|
|
133
|
+
* backend charges whatever tokens the model produced before the cut (its stream
|
|
134
|
+
* `abort` handler), because the upstream vendor charged us for them. A restarted
|
|
135
|
+
* turn therefore costs more than one attempt's worth of tokens — bounded by the
|
|
136
|
+
* reconnect budget, but real. The backend emits a `usage` event tagged
|
|
137
|
+
* `partial: true` for the discarded attempt so a cost display can stay accurate.
|
|
138
|
+
*/
|
|
139
|
+
export interface SSEStreamRestartEvent {
|
|
140
|
+
type: 'stream_restart';
|
|
141
|
+
/** Human-readable cause, for an optional dim status line. */
|
|
142
|
+
reason: string;
|
|
143
|
+
/**
|
|
144
|
+
* How many characters of rendered output this attempt had already emitted — counting
|
|
145
|
+
* both visible text AND streamed thinking, since consumers display both and both must
|
|
146
|
+
* be discarded. Only ever 0 when the attempt rendered nothing at all, in which case a
|
|
147
|
+
* plain `retry` is emitted instead of this event.
|
|
148
|
+
*/
|
|
149
|
+
discardedChars: number;
|
|
150
|
+
}
|
|
151
|
+
export type SSEEvent = SSETextEvent | SSEToolUseEvent | SSEMessageCompleteEvent | SSEUsageEvent | SSEDoneEvent | SSEErrorEvent | SSEThinkingEvent | SSEThinkingProgressEvent | SSEThinkingDeltaEvent | SSERetryEvent | SSERetryResolvedEvent | SSEStreamRestartEvent | SSEBalanceStatusEvent;
|
|
101
152
|
export interface UsageInfo {
|
|
102
153
|
input_tokens: number;
|
|
103
154
|
output_tokens: number;
|
|
@@ -173,7 +224,13 @@ export interface AgentLoopOptions {
|
|
|
173
224
|
onThinkingProgress?: (tokens: number) => void;
|
|
174
225
|
onToolUse: (name: string, input: Record<string, unknown>, isSubTask?: boolean) => void;
|
|
175
226
|
onToolResult: (name: string, result: ToolResult, isSubTask?: boolean) => void;
|
|
176
|
-
|
|
227
|
+
/**
|
|
228
|
+
* Token usage for a model call. `partial` marks tokens from an attempt that was cut
|
|
229
|
+
* short and restarted — real, already-billed spend that should be counted toward
|
|
230
|
+
* COST, but kept out of any "tokens this turn produced" display (see SSEUsageEvent).
|
|
231
|
+
* Optional second argument, so existing implementations keep compiling unchanged.
|
|
232
|
+
*/
|
|
233
|
+
onUsage: (u: UsageInfo, partial?: boolean) => void;
|
|
177
234
|
/**
|
|
178
235
|
* Fired when the network layer is about to transparently retry a turn after
|
|
179
236
|
* a transient disconnect (network drop, machine sleep/wake, overloaded
|
|
@@ -184,6 +241,17 @@ export interface AgentLoopOptions {
|
|
|
184
241
|
onRetry?: (attempt: number, maxAttempts: number, reason: string) => void;
|
|
185
242
|
/** Fired once a retried attempt starts receiving data again — clear any "reconnecting…" UI. */
|
|
186
243
|
onRetryResolved?: () => void;
|
|
244
|
+
/**
|
|
245
|
+
* Fired when a stream died mid-render and the SAME turn is being restarted.
|
|
246
|
+
* The consumer MUST discard whatever partial assistant text/thinking it has
|
|
247
|
+
* already displayed for this turn — the new attempt re-sends it in full, so
|
|
248
|
+
* keeping the old fragment would duplicate it. No tool has executed at this
|
|
249
|
+
* point (tools only run after `message_complete`), so nothing beyond the
|
|
250
|
+
* rendered output needs undoing. If a caller does not implement this, the
|
|
251
|
+
* loop falls back to NOT retrying post-render failures, preserving the old
|
|
252
|
+
* (safe but turn-killing) behaviour.
|
|
253
|
+
*/
|
|
254
|
+
onStreamRestart?: (reason: string, discardedChars: number) => void;
|
|
187
255
|
/**
|
|
188
256
|
* Fired when the backend reports the wallet balance dropped under the
|
|
189
257
|
* low-balance threshold ($5) after billing a turn, or when a turn couldn't
|
|
@@ -270,4 +338,45 @@ export interface AgentLoopOptions {
|
|
|
270
338
|
*/
|
|
271
339
|
_agentScope?: string;
|
|
272
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Thrown by `runAgentLoop` when a turn cannot continue (stream died, upstream
|
|
343
|
+
* error, etc.) but real work HAS already been completed in this turn.
|
|
344
|
+
*
|
|
345
|
+
* Why this exists: the loop operates on its own copy of the history
|
|
346
|
+
* (`[...initialMessages]`), so a plain `throw` used to leave the CALLER's array
|
|
347
|
+
* still holding only the original user message. An agent that had already run
|
|
348
|
+
* 25 tool calls over eight minutes would lose every one of them — the user was
|
|
349
|
+
* billed for the work, then had to start from scratch because "continue" had no
|
|
350
|
+
* memory of it. Carrying the partial history on the error lets the caller
|
|
351
|
+
* salvage it, so the very next "continue" resumes exactly where it stopped.
|
|
352
|
+
*
|
|
353
|
+
* `messages` is always a VALID history to resend: the loop only ever appends in
|
|
354
|
+
* complete (assistant → tool_result) pairs, so it never ends on an assistant
|
|
355
|
+
* turn with unanswered tool_use blocks.
|
|
356
|
+
*/
|
|
357
|
+
export declare class AgentTurnError extends Error {
|
|
358
|
+
/** The turn's history up to the point of failure — safe to resend to the API. */
|
|
359
|
+
readonly messages: Message[];
|
|
360
|
+
/**
|
|
361
|
+
* How many turn steps completed before the failure (0 = nothing salvageable).
|
|
362
|
+
*
|
|
363
|
+
* Supplied by the loop as an explicit counter rather than derived from array
|
|
364
|
+
* lengths: auto-compaction SPLICES the history down mid-turn, so a long run that
|
|
365
|
+
* compacted and then died can end up SHORTER than it started. A length delta would
|
|
366
|
+
* read as "no progress" and discard exactly the expensive, long-running sessions
|
|
367
|
+
* this class exists to protect.
|
|
368
|
+
*/
|
|
369
|
+
readonly progressCount: number;
|
|
370
|
+
/** The original underlying error, if any. */
|
|
371
|
+
readonly cause?: unknown;
|
|
372
|
+
constructor(message: string, messages: Message[], progressCount: number, cause?: unknown);
|
|
373
|
+
}
|
|
374
|
+
/** Type guard — narrows an unknown catch binding to an AgentTurnError. */
|
|
375
|
+
export declare function isAgentTurnError(err: unknown): err is AgentTurnError;
|
|
376
|
+
/**
|
|
377
|
+
* Salvage a partial history out of a failed turn. Returns the partial history
|
|
378
|
+
* when the error carried one AND real progress was made, otherwise `null`.
|
|
379
|
+
* Callers use it as: `messages = salvageHistory(err) ?? messages;`
|
|
380
|
+
*/
|
|
381
|
+
export declare function salvageHistory(err: unknown): Message[] | null;
|
|
273
382
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,UAAU,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,aAAa,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,YAAY,GAAG,eAAe,CAAC;AAItE,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,OAAO,EAAE,YA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,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,aAAa,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAOzF;AAED,0EAA0E;AAC1E,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,cAAc,CAEpE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,EAAE,GAAG,IAAI,CAG7D"}
|
package/dist/types.js
CHANGED
|
@@ -1,4 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// ─── Content Blocks ───────────────────────────────────────────────────────────
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AgentTurnError = void 0;
|
|
5
|
+
exports.isAgentTurnError = isAgentTurnError;
|
|
6
|
+
exports.salvageHistory = salvageHistory;
|
|
7
|
+
// ─── Turn failure that preserves work already done ───────────────────────────
|
|
8
|
+
/**
|
|
9
|
+
* Thrown by `runAgentLoop` when a turn cannot continue (stream died, upstream
|
|
10
|
+
* error, etc.) but real work HAS already been completed in this turn.
|
|
11
|
+
*
|
|
12
|
+
* Why this exists: the loop operates on its own copy of the history
|
|
13
|
+
* (`[...initialMessages]`), so a plain `throw` used to leave the CALLER's array
|
|
14
|
+
* still holding only the original user message. An agent that had already run
|
|
15
|
+
* 25 tool calls over eight minutes would lose every one of them — the user was
|
|
16
|
+
* billed for the work, then had to start from scratch because "continue" had no
|
|
17
|
+
* memory of it. Carrying the partial history on the error lets the caller
|
|
18
|
+
* salvage it, so the very next "continue" resumes exactly where it stopped.
|
|
19
|
+
*
|
|
20
|
+
* `messages` is always a VALID history to resend: the loop only ever appends in
|
|
21
|
+
* complete (assistant → tool_result) pairs, so it never ends on an assistant
|
|
22
|
+
* turn with unanswered tool_use blocks.
|
|
23
|
+
*/
|
|
24
|
+
class AgentTurnError extends Error {
|
|
25
|
+
constructor(message, messages, progressCount, cause) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = 'AgentTurnError';
|
|
28
|
+
this.messages = messages;
|
|
29
|
+
this.progressCount = Math.max(0, progressCount);
|
|
30
|
+
this.cause = cause;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.AgentTurnError = AgentTurnError;
|
|
34
|
+
/** Type guard — narrows an unknown catch binding to an AgentTurnError. */
|
|
35
|
+
function isAgentTurnError(err) {
|
|
36
|
+
return err instanceof AgentTurnError;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Salvage a partial history out of a failed turn. Returns the partial history
|
|
40
|
+
* when the error carried one AND real progress was made, otherwise `null`.
|
|
41
|
+
* Callers use it as: `messages = salvageHistory(err) ?? messages;`
|
|
42
|
+
*/
|
|
43
|
+
function salvageHistory(err) {
|
|
44
|
+
if (!isAgentTurnError(err))
|
|
45
|
+
return null;
|
|
46
|
+
return err.progressCount > 0 ? err.messages : null;
|
|
47
|
+
}
|
|
4
48
|
//# sourceMappingURL=types.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexrall/code-core",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.15",
|
|
4
4
|
"description": "Core agent loop, tools, and extension primitives for Nexrall Code — embed an AI coding agent in any Node.js application.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Nexrall <support@nexrall.com> (https://nexrall.com)",
|