@demicodes/agent 0.1.0
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/LICENSE +201 -0
- package/README.md +20 -0
- package/dist/client-entry.d.mts +4 -0
- package/dist/client-entry.mjs +2 -0
- package/dist/index.d.mts +230 -0
- package/dist/index.mjs +3059 -0
- package/dist/json-codec-BFLXlg3K.mjs +70 -0
- package/dist/stdio-transport.d.mts +8 -0
- package/dist/stdio-transport.mjs +45 -0
- package/dist/transport-BkAbPvRc.d.mts +533 -0
- package/dist/websocket-transport-D6zJ4PLC.d.mts +88 -0
- package/dist/websocket-transport-o6r-jV5p.mjs +473 -0
- package/package.json +53 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//#region src/json-codec.ts
|
|
2
|
+
const BINARY_MARKER = "__demiUint8Array";
|
|
3
|
+
const BIGINT_MARKER = "__demiBigInt";
|
|
4
|
+
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
5
|
+
function stringifyAgentJson(value) {
|
|
6
|
+
return JSON.stringify(value, (_key, nested) => {
|
|
7
|
+
if (nested instanceof Uint8Array) return {
|
|
8
|
+
[BINARY_MARKER]: true,
|
|
9
|
+
base64: bytesToBase64(nested)
|
|
10
|
+
};
|
|
11
|
+
if (typeof nested === "bigint") return {
|
|
12
|
+
[BIGINT_MARKER]: true,
|
|
13
|
+
value: nested.toString()
|
|
14
|
+
};
|
|
15
|
+
return nested;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function parseAgentJson(text) {
|
|
19
|
+
return JSON.parse(text, (_key, nested) => {
|
|
20
|
+
if (isEncodedUint8Array(nested)) return base64ToBytes(nested.base64);
|
|
21
|
+
if (isEncodedBigInt(nested)) return BigInt(nested.value);
|
|
22
|
+
return nested;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function isEncodedUint8Array(value) {
|
|
26
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && value[BINARY_MARKER] === true && typeof value.base64 === "string";
|
|
27
|
+
}
|
|
28
|
+
function isEncodedBigInt(value) {
|
|
29
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && value[BIGINT_MARKER] === true && typeof value.value === "string";
|
|
30
|
+
}
|
|
31
|
+
function bytesToBase64(bytes) {
|
|
32
|
+
let output = "";
|
|
33
|
+
for (let index = 0; index < bytes.byteLength; index += 3) {
|
|
34
|
+
const first = bytes[index];
|
|
35
|
+
const second = bytes[index + 1];
|
|
36
|
+
const third = bytes[index + 2];
|
|
37
|
+
output += BASE64_ALPHABET[first >> 2];
|
|
38
|
+
output += BASE64_ALPHABET[(first & 3) << 4 | (second ?? 0) >> 4];
|
|
39
|
+
output += second === void 0 ? "=" : BASE64_ALPHABET[(second & 15) << 2 | (third ?? 0) >> 6];
|
|
40
|
+
output += third === void 0 ? "=" : BASE64_ALPHABET[third & 63];
|
|
41
|
+
}
|
|
42
|
+
return output;
|
|
43
|
+
}
|
|
44
|
+
function base64ToBytes(base64) {
|
|
45
|
+
const clean = base64.replace(/\s+/g, "");
|
|
46
|
+
if (clean.length === 0) return /* @__PURE__ */ new Uint8Array();
|
|
47
|
+
if (clean.length % 4 !== 0) throw new Error("Invalid base64 payload length");
|
|
48
|
+
const padding = clean.endsWith("==") ? 2 : clean.endsWith("=") ? 1 : 0;
|
|
49
|
+
const bytes = new Uint8Array(clean.length / 4 * 3 - padding);
|
|
50
|
+
let offset = 0;
|
|
51
|
+
for (let index = 0; index < clean.length; index += 4) {
|
|
52
|
+
const first = base64Value(clean[index]);
|
|
53
|
+
const second = base64Value(clean[index + 1]);
|
|
54
|
+
const third = clean[index + 2] === "=" ? 0 : base64Value(clean[index + 2]);
|
|
55
|
+
const fourth = clean[index + 3] === "=" ? 0 : base64Value(clean[index + 3]);
|
|
56
|
+
const triple = first << 18 | second << 12 | third << 6 | fourth;
|
|
57
|
+
if (offset < bytes.byteLength) bytes[offset++] = triple >> 16 & 255;
|
|
58
|
+
if (offset < bytes.byteLength) bytes[offset++] = triple >> 8 & 255;
|
|
59
|
+
if (offset < bytes.byteLength) bytes[offset++] = triple & 255;
|
|
60
|
+
}
|
|
61
|
+
return bytes;
|
|
62
|
+
}
|
|
63
|
+
function base64Value(char) {
|
|
64
|
+
if (!char || char === "=") throw new Error("Invalid base64 payload");
|
|
65
|
+
const value = BASE64_ALPHABET.indexOf(char);
|
|
66
|
+
if (value === -1) throw new Error("Invalid base64 payload");
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
export { stringifyAgentJson as n, parseAgentJson as t };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { n as AgentServerTransport, t as AgentClientTransport } from "./transport-BkAbPvRc.mjs";
|
|
2
|
+
import { Readable, Writable } from "node:stream";
|
|
3
|
+
|
|
4
|
+
//#region src/stdio-transport.d.ts
|
|
5
|
+
declare function createStdioClientTransport(readable: Readable, writable: Writable): AgentClientTransport;
|
|
6
|
+
declare function createStdioServerTransport(readable: Readable, writable: Writable): AgentServerTransport;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { createStdioClientTransport, createStdioServerTransport };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { n as stringifyAgentJson, t as parseAgentJson } from "./json-codec-BFLXlg3K.mjs";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
//#region src/stdio-transport.ts
|
|
4
|
+
function createStdioClientTransport(readable, writable) {
|
|
5
|
+
return new JsonLineTransport(readable, writable);
|
|
6
|
+
}
|
|
7
|
+
function createStdioServerTransport(readable, writable) {
|
|
8
|
+
return new JsonLineTransport(readable, writable);
|
|
9
|
+
}
|
|
10
|
+
var JsonLineTransport = class {
|
|
11
|
+
readable;
|
|
12
|
+
writable;
|
|
13
|
+
handlers = /* @__PURE__ */ new Set();
|
|
14
|
+
readline;
|
|
15
|
+
closed = false;
|
|
16
|
+
constructor(readable, writable) {
|
|
17
|
+
this.readable = readable;
|
|
18
|
+
this.writable = writable;
|
|
19
|
+
this.readline = createInterface({ input: readable });
|
|
20
|
+
this.readline.on("line", (line) => {
|
|
21
|
+
if (this.closed || line.trim() === "") return;
|
|
22
|
+
const frame = parseAgentJson(line);
|
|
23
|
+
for (const handler of this.handlers) handler(frame);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
send(frame) {
|
|
27
|
+
if (this.closed) throw new Error("Agent transport is closed");
|
|
28
|
+
this.writable.write(`${stringifyAgentJson(frame)}\n`);
|
|
29
|
+
}
|
|
30
|
+
onFrame(handler) {
|
|
31
|
+
this.handlers.add(handler);
|
|
32
|
+
return () => {
|
|
33
|
+
this.handlers.delete(handler);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
close() {
|
|
37
|
+
this.closed = true;
|
|
38
|
+
this.handlers.clear();
|
|
39
|
+
this.readline.close();
|
|
40
|
+
this.readable.destroy();
|
|
41
|
+
this.writable.end();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
//#endregion
|
|
45
|
+
export { createStdioClientTransport, createStdioServerTransport };
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { BashAuditEvent, CommandSpec, Host, ShellCommandSnapshot } from "@demicodes/shell";
|
|
2
|
+
import { AgentProvider, InferenceItem, ProviderEvent, ProviderSelection, ToolDefinition } from "@demicodes/provider";
|
|
3
|
+
import { Block, ModelSelection, QueuedMessage, SessionPhase, ToolResultContentBlock, Transcript, UserContentBlock } from "@demicodes/core";
|
|
4
|
+
|
|
5
|
+
//#region src/retry-policy.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Retry policy for provider requests, applied by the agent runtime (turn loop
|
|
8
|
+
* and compaction) — providers stay single-shot and only classify error codes.
|
|
9
|
+
* A request is retried only while it has produced no transcript content, so a
|
|
10
|
+
* retry can never duplicate partially streamed output.
|
|
11
|
+
*/
|
|
12
|
+
interface TurnRetryPolicy {
|
|
13
|
+
/** Total attempts including the first (default 4 = 1 original + 3 retries). */
|
|
14
|
+
maxAttempts: number;
|
|
15
|
+
/** Base for exponential backoff with full jitter (default 1000ms). */
|
|
16
|
+
baseDelayMs: number;
|
|
17
|
+
/** Backoff ceiling (default 30000ms). */
|
|
18
|
+
maxDelayMs: number;
|
|
19
|
+
/** Provider error codes that are retried (default rate_limit, overloaded). */
|
|
20
|
+
retryableCodes: string[];
|
|
21
|
+
}
|
|
22
|
+
declare const DEFAULT_TURN_RETRY_POLICY: TurnRetryPolicy;
|
|
23
|
+
declare function resolveRetryPolicy(overrides: Partial<TurnRetryPolicy> | undefined): TurnRetryPolicy;
|
|
24
|
+
declare function isRetryableCode(policy: TurnRetryPolicy, code: string | null): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Delay before retry `attempt` (1-based): a provider-supplied Retry-After wins,
|
|
27
|
+
* otherwise exponential backoff with full jitter, capped at `maxDelayMs`.
|
|
28
|
+
*/
|
|
29
|
+
declare function retryDelayMs(policy: TurnRetryPolicy, attempt: number, retryAfterMs: number | null): number;
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/transcript.d.ts
|
|
32
|
+
interface TranscriptOptions {
|
|
33
|
+
idFactory?: () => string;
|
|
34
|
+
now?: () => string;
|
|
35
|
+
/**
|
|
36
|
+
* Per-text-block bounds applied when replaying to the model (head+tail with
|
|
37
|
+
* an elision marker). Defaults to 8000/8000 characters.
|
|
38
|
+
*/
|
|
39
|
+
replayTextBounds?: {
|
|
40
|
+
headChars: number;
|
|
41
|
+
tailChars: number;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
interface CompactionWindow {
|
|
45
|
+
startIndex: number;
|
|
46
|
+
cutPoint: number;
|
|
47
|
+
}
|
|
48
|
+
interface DrainedTranscriptPatches {
|
|
49
|
+
revision: number;
|
|
50
|
+
patches: TranscriptPatch[];
|
|
51
|
+
}
|
|
52
|
+
declare class Transcript$1 implements Transcript {
|
|
53
|
+
readonly blocks: Block[];
|
|
54
|
+
private readonly idFactory;
|
|
55
|
+
private readonly now;
|
|
56
|
+
private journal;
|
|
57
|
+
private revisionCounter;
|
|
58
|
+
private readonly replayHeadChars;
|
|
59
|
+
private readonly replayTailChars;
|
|
60
|
+
constructor(blocks?: Block[], options?: TranscriptOptions);
|
|
61
|
+
snapshot(): Transcript;
|
|
62
|
+
/** Monotonic revision, advanced once per drained patch batch. */
|
|
63
|
+
get revision(): number;
|
|
64
|
+
/**
|
|
65
|
+
* Drains the patches recorded since the last drain, advancing the revision.
|
|
66
|
+
* Returns null when nothing changed.
|
|
67
|
+
*/
|
|
68
|
+
takePatches(): DrainedTranscriptPatches | null;
|
|
69
|
+
private record;
|
|
70
|
+
private recordAdd;
|
|
71
|
+
private recordBlockReplace;
|
|
72
|
+
private recordReplaceAll;
|
|
73
|
+
pushUserTurn(turnId: string, model: ModelSelection, content: UserContentBlock[], preamble?: string | null, hidden?: boolean): Block;
|
|
74
|
+
pushResumeTurn(turnId: string, model: ModelSelection): Block;
|
|
75
|
+
pushSteer(turnId: string, model: ModelSelection, content: UserContentBlock[], id?: string, hidden?: boolean): Block;
|
|
76
|
+
pushAbort(model: ModelSelection, isResumed?: boolean): Block;
|
|
77
|
+
markLatestAbortResumed(): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Rewrites the transcript for a retry: drops everything after the last user
|
|
80
|
+
* turn except that turn's steers (which are replayed to the new attempt).
|
|
81
|
+
* Returns the user block, or null when there is no user turn to retry.
|
|
82
|
+
*/
|
|
83
|
+
rewindToLastUserTurn(): Extract<Block, {
|
|
84
|
+
type: 'user';
|
|
85
|
+
}> | null;
|
|
86
|
+
applyProviderEvent(model: ModelSelection, event: ProviderEvent): Block | null;
|
|
87
|
+
completeToolCall(toolUseId: string, output: ToolResultContentBlock[], isError?: boolean, metadata?: unknown | null): Block | null;
|
|
88
|
+
findPendingToolUseId(): string | null;
|
|
89
|
+
pendingToolCalls(): Extract<Block, {
|
|
90
|
+
type: 'tool_call';
|
|
91
|
+
}>[];
|
|
92
|
+
removeDanglingToolCalls(): Block[];
|
|
93
|
+
findLastCompactionIndex(): number | null;
|
|
94
|
+
findCompactionCutPoint(keepRecentTokens: number): number | null;
|
|
95
|
+
findCompactionWindow(keepRecentTokens: number): CompactionWindow | null;
|
|
96
|
+
insertCompactionBoundary(index: number, model: ModelSelection, summary: string, summaryTokens: number): Block;
|
|
97
|
+
appendCompactionMarker(model: ModelSelection, boundaryId: string, compactedTokens: number): Block;
|
|
98
|
+
appendExtensionStateSnapshot(extensionName: string, state: unknown): Block;
|
|
99
|
+
latestExtensionStateSnapshot(extensionName?: string): Extract<Block, {
|
|
100
|
+
type: 'extension_state_snapshot';
|
|
101
|
+
}> | null;
|
|
102
|
+
replayableBlocks(): Block[];
|
|
103
|
+
collectInferenceItems(): InferenceItem[];
|
|
104
|
+
/**
|
|
105
|
+
* Estimates the next request's context size. When a provider-reported usage
|
|
106
|
+
* exists after the last compaction (the most recent real measurement of this
|
|
107
|
+
* history), it anchors the estimate and only blocks streamed after it are
|
|
108
|
+
* char-estimated; otherwise the whole replay window is estimated at ~4
|
|
109
|
+
* chars/token with fixed weights for images and documents.
|
|
110
|
+
*/
|
|
111
|
+
estimateContextTokens(): number;
|
|
112
|
+
/**
|
|
113
|
+
* The latest response block's usage, valid only when no compaction happened
|
|
114
|
+
* after it (compaction shrinks the history the usage was measured against).
|
|
115
|
+
*/
|
|
116
|
+
private usageAnchor;
|
|
117
|
+
private appendText;
|
|
118
|
+
private appendThinking;
|
|
119
|
+
private setLatestThinkingSignature;
|
|
120
|
+
private appendBlock;
|
|
121
|
+
}
|
|
122
|
+
/** Char-based per-block estimate, exported for compaction metrics. */
|
|
123
|
+
declare function estimateTranscriptBlockTokens(block: Block): number;
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/types.d.ts
|
|
126
|
+
interface AgentPromptContext<State> {
|
|
127
|
+
agentSessionId: string;
|
|
128
|
+
state: State;
|
|
129
|
+
cwd: string;
|
|
130
|
+
transcript: Transcript$1;
|
|
131
|
+
}
|
|
132
|
+
interface AgentSystemPromptContext<State> extends AgentPromptContext<State> {
|
|
133
|
+
/**
|
|
134
|
+
* Rendered help for every registered command (summary, subcommands, parameters,
|
|
135
|
+
* stdin fields, examples), produced by the server from the session's actual
|
|
136
|
+
* CommandRegistry. Harnesses embed it wherever their system prompt wants the
|
|
137
|
+
* command reference; empty string when no commands are registered.
|
|
138
|
+
*/
|
|
139
|
+
commandsPrompt: string;
|
|
140
|
+
}
|
|
141
|
+
interface AgentToolContext<State> {
|
|
142
|
+
agentSessionId: string;
|
|
143
|
+
state: State;
|
|
144
|
+
cwd: string;
|
|
145
|
+
}
|
|
146
|
+
interface AgentDisposeContext<State> {
|
|
147
|
+
agentSessionId: string;
|
|
148
|
+
state: State;
|
|
149
|
+
cwd: string;
|
|
150
|
+
transcript: Transcript$1;
|
|
151
|
+
}
|
|
152
|
+
interface AgentReferenceResolveContext<State> {
|
|
153
|
+
agentSessionId: string;
|
|
154
|
+
state: State;
|
|
155
|
+
cwd: string;
|
|
156
|
+
transcript: Transcript$1;
|
|
157
|
+
signal: AbortSignal;
|
|
158
|
+
}
|
|
159
|
+
interface AgentHarnessContext<State> {
|
|
160
|
+
state: State;
|
|
161
|
+
cwd: string;
|
|
162
|
+
}
|
|
163
|
+
interface AgentHarness<State = unknown> {
|
|
164
|
+
name: string;
|
|
165
|
+
initialState(): State;
|
|
166
|
+
host(ctx: AgentHarnessContext<State>): Host;
|
|
167
|
+
commands?(ctx: AgentHarnessContext<State>): CommandSpec[];
|
|
168
|
+
systemPrompt(ctx: AgentSystemPromptContext<State>): string;
|
|
169
|
+
preamble?(ctx: AgentPromptContext<State>): string | null;
|
|
170
|
+
resolveReferences?(ctx: AgentReferenceResolveContext<State>, content: UserContentBlock[]): Promise<UserContentBlock[]> | UserContentBlock[];
|
|
171
|
+
lifecycle?(event: AgentLifecycleEvent<State>): Promise<void> | void;
|
|
172
|
+
dispose?(ctx: AgentDisposeContext<State>): Promise<void> | void;
|
|
173
|
+
}
|
|
174
|
+
interface AgentToolInvokeContext<State> {
|
|
175
|
+
agentSessionId: string;
|
|
176
|
+
state: State;
|
|
177
|
+
cwd: string;
|
|
178
|
+
model: ModelSelection;
|
|
179
|
+
toolCallId: string;
|
|
180
|
+
signal: AbortSignal;
|
|
181
|
+
emitProgress(progress: unknown): void;
|
|
182
|
+
}
|
|
183
|
+
interface AgentToolInvokeResult {
|
|
184
|
+
output: ToolResultContentBlock[];
|
|
185
|
+
isError?: boolean;
|
|
186
|
+
metadata?: unknown | null;
|
|
187
|
+
continuation?: ToolContinuation;
|
|
188
|
+
stopAfterToolResult?: boolean;
|
|
189
|
+
}
|
|
190
|
+
interface ToolContinuation {
|
|
191
|
+
toolCallId: string;
|
|
192
|
+
shellId: string;
|
|
193
|
+
commandId: string;
|
|
194
|
+
status: 'running';
|
|
195
|
+
}
|
|
196
|
+
type AbortTarget = 'active_provider_stream' | 'active_tool' | 'active_compaction' | 'active_turn' | 'queued_action' | 'queued_message' | 'pending_yield_wakeup';
|
|
197
|
+
interface AbortResult {
|
|
198
|
+
aborted: boolean;
|
|
199
|
+
target: AbortTarget | null;
|
|
200
|
+
canAbortAgain: boolean;
|
|
201
|
+
}
|
|
202
|
+
interface AgentTool<State = unknown> extends ToolDefinition {
|
|
203
|
+
invoke(ctx: AgentToolInvokeContext<State>, input: unknown): Promise<AgentToolInvokeResult> | AgentToolInvokeResult;
|
|
204
|
+
}
|
|
205
|
+
type AgentLifecycleEvent<State> = {
|
|
206
|
+
type: 'before_round_start';
|
|
207
|
+
agentSessionId: string;
|
|
208
|
+
state: State;
|
|
209
|
+
transcript: Transcript$1;
|
|
210
|
+
content: UserContentBlock[];
|
|
211
|
+
} | {
|
|
212
|
+
type: 'after_tool_call';
|
|
213
|
+
agentSessionId: string;
|
|
214
|
+
state: State;
|
|
215
|
+
transcript: Transcript$1;
|
|
216
|
+
toolCallId: string;
|
|
217
|
+
toolName: string;
|
|
218
|
+
result: AgentToolInvokeResult;
|
|
219
|
+
} | {
|
|
220
|
+
type: 'after_transcript_rewrite';
|
|
221
|
+
agentSessionId: string;
|
|
222
|
+
state: State;
|
|
223
|
+
transcript: Transcript$1;
|
|
224
|
+
reason: 'retry';
|
|
225
|
+
};
|
|
226
|
+
interface AgentHarnessRuntime<State> {
|
|
227
|
+
harnessName: string;
|
|
228
|
+
initialState(): State;
|
|
229
|
+
systemPrompt(ctx: AgentPromptContext<State>): string;
|
|
230
|
+
preamble?(ctx: AgentPromptContext<State>): string | null;
|
|
231
|
+
resolveReferences?(ctx: AgentReferenceResolveContext<State>, content: UserContentBlock[]): Promise<UserContentBlock[]> | UserContentBlock[];
|
|
232
|
+
tools(ctx: AgentToolContext<State>): AgentTool<State>[];
|
|
233
|
+
lifecycle?(event: AgentLifecycleEvent<State>): Promise<void> | void;
|
|
234
|
+
dispose?(ctx: AgentDisposeContext<State>): Promise<void> | void;
|
|
235
|
+
}
|
|
236
|
+
interface AgentSessionParams<State> {
|
|
237
|
+
provider: AgentProvider;
|
|
238
|
+
model: ModelSelection;
|
|
239
|
+
cwd: string;
|
|
240
|
+
runtime: AgentHarnessRuntime<State>;
|
|
241
|
+
transcript?: Transcript | Transcript$1;
|
|
242
|
+
state?: State;
|
|
243
|
+
}
|
|
244
|
+
interface AgentSessionSnapshot<State> {
|
|
245
|
+
transcript: Transcript;
|
|
246
|
+
state: State;
|
|
247
|
+
phase: SessionPhase;
|
|
248
|
+
queue: QueuedMessage[];
|
|
249
|
+
cwd: string;
|
|
250
|
+
model: ModelSelection;
|
|
251
|
+
harnessName: string;
|
|
252
|
+
}
|
|
253
|
+
interface AgentSessionStore<State = unknown> {
|
|
254
|
+
saveSnapshot(snapshot: AgentSessionSnapshot<State>): Promise<void> | void;
|
|
255
|
+
/** Load a previously saved snapshot for this session, or null if none exists. */
|
|
256
|
+
loadSnapshot(): Promise<AgentSessionSnapshot<State> | null>;
|
|
257
|
+
}
|
|
258
|
+
interface AgentSessionRestoreParams<State> {
|
|
259
|
+
provider: AgentProvider;
|
|
260
|
+
runtime: AgentHarnessRuntime<State>;
|
|
261
|
+
snapshot: AgentSessionSnapshot<State>;
|
|
262
|
+
}
|
|
263
|
+
interface AgentSessionOptions<State = unknown> {
|
|
264
|
+
agentSessionId?: string;
|
|
265
|
+
idFactory?: () => string;
|
|
266
|
+
now?: () => string;
|
|
267
|
+
store?: AgentSessionStore<State>;
|
|
268
|
+
compaction?: {
|
|
269
|
+
keepRecentTokens?: number;
|
|
270
|
+
preflightThresholdRatio?: number;
|
|
271
|
+
};
|
|
272
|
+
/**
|
|
273
|
+
* Maximum interval between snapshot writes while a turn is streaming. Writes
|
|
274
|
+
* always flush at action boundaries (turn end, abort, dispose); this only
|
|
275
|
+
* bounds staleness during streaming. Default 1000ms.
|
|
276
|
+
*/
|
|
277
|
+
persistIntervalMs?: number;
|
|
278
|
+
/** Overrides for the transient-failure retry policy (see TurnRetryPolicy). */
|
|
279
|
+
retry?: Partial<TurnRetryPolicy>;
|
|
280
|
+
}
|
|
281
|
+
type SessionEvent = {
|
|
282
|
+
type: 'transcript_changed';
|
|
283
|
+
patches: TranscriptPatch[];
|
|
284
|
+
revision: number;
|
|
285
|
+
} | {
|
|
286
|
+
type: 'phase_changed';
|
|
287
|
+
phase: SessionPhase;
|
|
288
|
+
} | {
|
|
289
|
+
type: 'queue_changed';
|
|
290
|
+
queue: QueuedMessage[];
|
|
291
|
+
} | {
|
|
292
|
+
type: 'tool_progress';
|
|
293
|
+
toolCallId: string;
|
|
294
|
+
toolName: string;
|
|
295
|
+
progress: unknown;
|
|
296
|
+
} | {
|
|
297
|
+
type: 'retry_scheduled';
|
|
298
|
+
attempt: number;
|
|
299
|
+
delayMs: number;
|
|
300
|
+
code: string | null;
|
|
301
|
+
} | {
|
|
302
|
+
type: 'error';
|
|
303
|
+
error: Error;
|
|
304
|
+
};
|
|
305
|
+
type SessionEventListener = (event: SessionEvent) => void;
|
|
306
|
+
interface ExternalMutationReservation {
|
|
307
|
+
release(): void;
|
|
308
|
+
}
|
|
309
|
+
//#endregion
|
|
310
|
+
//#region src/frames.d.ts
|
|
311
|
+
/** A persisted conversation in a workspace (cwd), for the resume/history list. */
|
|
312
|
+
interface ConversationSummary {
|
|
313
|
+
id: string;
|
|
314
|
+
title: string;
|
|
315
|
+
createdAt: string;
|
|
316
|
+
updatedAt: string;
|
|
317
|
+
}
|
|
318
|
+
type ClientFrame = {
|
|
319
|
+
type: 'open';
|
|
320
|
+
provider: ProviderSelection;
|
|
321
|
+
cwd: string;
|
|
322
|
+
sessionId: string;
|
|
323
|
+
} | {
|
|
324
|
+
type: 'send';
|
|
325
|
+
messageId: string;
|
|
326
|
+
content: UserContentBlock[];
|
|
327
|
+
} | {
|
|
328
|
+
type: 'dequeue_message';
|
|
329
|
+
messageId: string;
|
|
330
|
+
} | {
|
|
331
|
+
type: 'send_queued_message';
|
|
332
|
+
messageId: string;
|
|
333
|
+
} | {
|
|
334
|
+
type: 'steer_queued_message';
|
|
335
|
+
messageId: string;
|
|
336
|
+
steerId: string;
|
|
337
|
+
} | {
|
|
338
|
+
type: 'clear_message_queue';
|
|
339
|
+
} | {
|
|
340
|
+
type: 'steer';
|
|
341
|
+
steerId: string;
|
|
342
|
+
content: UserContentBlock[];
|
|
343
|
+
} | {
|
|
344
|
+
type: 'cancel_pending_steer';
|
|
345
|
+
steerId: string;
|
|
346
|
+
} | {
|
|
347
|
+
type: 'set_provider';
|
|
348
|
+
provider: ProviderSelection;
|
|
349
|
+
} | {
|
|
350
|
+
type: 'abort';
|
|
351
|
+
} | {
|
|
352
|
+
type: 'retry';
|
|
353
|
+
} | {
|
|
354
|
+
type: 'resume';
|
|
355
|
+
} | {
|
|
356
|
+
type: 'compact';
|
|
357
|
+
} | {
|
|
358
|
+
type: 'shell_write';
|
|
359
|
+
commandId: string;
|
|
360
|
+
stdin: string;
|
|
361
|
+
} | {
|
|
362
|
+
type: 'list_conversations';
|
|
363
|
+
cwd: string;
|
|
364
|
+
} | {
|
|
365
|
+
type: 'sync_transcript';
|
|
366
|
+
} | {
|
|
367
|
+
type: 'close';
|
|
368
|
+
};
|
|
369
|
+
type ServerFrame = {
|
|
370
|
+
type: 'opened';
|
|
371
|
+
} | {
|
|
372
|
+
type: 'rejected';
|
|
373
|
+
command: string;
|
|
374
|
+
reason: string;
|
|
375
|
+
} | {
|
|
376
|
+
type: 'transcript_snapshot';
|
|
377
|
+
blocks: Block[];
|
|
378
|
+
revision: number;
|
|
379
|
+
} | {
|
|
380
|
+
type: 'transcript_patch';
|
|
381
|
+
patches: TranscriptPatch[];
|
|
382
|
+
revision: number;
|
|
383
|
+
} | {
|
|
384
|
+
type: 'phase';
|
|
385
|
+
phase: SessionPhase;
|
|
386
|
+
} | {
|
|
387
|
+
type: 'queue';
|
|
388
|
+
queue: QueuedMessage[];
|
|
389
|
+
} | {
|
|
390
|
+
type: 'steer_result';
|
|
391
|
+
steerId: string;
|
|
392
|
+
status: 'accepted';
|
|
393
|
+
} | {
|
|
394
|
+
type: 'steer_result';
|
|
395
|
+
steerId: string;
|
|
396
|
+
status: 'rejected';
|
|
397
|
+
reason: string;
|
|
398
|
+
} | {
|
|
399
|
+
type: 'abort_result';
|
|
400
|
+
result: AbortResult;
|
|
401
|
+
} | {
|
|
402
|
+
type: 'tool_progress';
|
|
403
|
+
toolUseId: string;
|
|
404
|
+
output: ToolResultContentBlock[];
|
|
405
|
+
} | {
|
|
406
|
+
type: 'shell_output';
|
|
407
|
+
shellId: string;
|
|
408
|
+
commandId: string;
|
|
409
|
+
snapshot: ShellCommandSnapshotLike;
|
|
410
|
+
} | {
|
|
411
|
+
type: 'shell_write_result';
|
|
412
|
+
commandId: string;
|
|
413
|
+
output: ToolResultContentBlock[];
|
|
414
|
+
} | {
|
|
415
|
+
type: 'audit';
|
|
416
|
+
events: BashAuditEvent[];
|
|
417
|
+
} | {
|
|
418
|
+
type: 'conversations';
|
|
419
|
+
conversations: ConversationSummary[];
|
|
420
|
+
} | {
|
|
421
|
+
type: 'retry_scheduled';
|
|
422
|
+
attempt: number;
|
|
423
|
+
delayMs: number;
|
|
424
|
+
code: string | null;
|
|
425
|
+
} | {
|
|
426
|
+
type: 'error';
|
|
427
|
+
message: string;
|
|
428
|
+
code?: string;
|
|
429
|
+
} | {
|
|
430
|
+
type: 'closed';
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* Wire patches for transcript replication. Produced directly by the Transcript's
|
|
434
|
+
* mutation journal (never diff-derived). `append_text` carries streaming deltas
|
|
435
|
+
* for the `text` field of the block at the index (text/thinking blocks), keeping
|
|
436
|
+
* per-delta cost O(delta) instead of O(block) or O(transcript).
|
|
437
|
+
*/
|
|
438
|
+
type TranscriptPatch = {
|
|
439
|
+
op: 'add';
|
|
440
|
+
path: ['blocks', number];
|
|
441
|
+
value: Block;
|
|
442
|
+
} | {
|
|
443
|
+
op: 'remove';
|
|
444
|
+
path: ['blocks', number];
|
|
445
|
+
} | {
|
|
446
|
+
op: 'replace_block';
|
|
447
|
+
path: ['blocks', number];
|
|
448
|
+
value: Block;
|
|
449
|
+
} | {
|
|
450
|
+
op: 'append_text';
|
|
451
|
+
path: ['blocks', number];
|
|
452
|
+
delta: string;
|
|
453
|
+
} | {
|
|
454
|
+
op: 'replace';
|
|
455
|
+
path: ['blocks'];
|
|
456
|
+
value: Block[];
|
|
457
|
+
};
|
|
458
|
+
type ShellCommandSnapshotLike = ShellCommandSnapshot;
|
|
459
|
+
type ClientSessionEvent = {
|
|
460
|
+
type: 'transcript_snapshot';
|
|
461
|
+
blocks: Block[];
|
|
462
|
+
} | {
|
|
463
|
+
type: 'transcript_patch';
|
|
464
|
+
patches: TranscriptPatch[];
|
|
465
|
+
blocks: Block[];
|
|
466
|
+
} | {
|
|
467
|
+
type: 'phase';
|
|
468
|
+
phase: SessionPhase;
|
|
469
|
+
} | {
|
|
470
|
+
type: 'queue';
|
|
471
|
+
queue: QueuedMessage[];
|
|
472
|
+
} | {
|
|
473
|
+
type: 'steer_result';
|
|
474
|
+
steerId: string;
|
|
475
|
+
status: 'accepted';
|
|
476
|
+
} | {
|
|
477
|
+
type: 'steer_result';
|
|
478
|
+
steerId: string;
|
|
479
|
+
status: 'rejected';
|
|
480
|
+
reason: string;
|
|
481
|
+
} | {
|
|
482
|
+
type: 'abort_result';
|
|
483
|
+
result: AbortResult;
|
|
484
|
+
} | {
|
|
485
|
+
type: 'tool_progress';
|
|
486
|
+
toolUseId: string;
|
|
487
|
+
output: ToolResultContentBlock[];
|
|
488
|
+
} | {
|
|
489
|
+
type: 'shell_output';
|
|
490
|
+
shellId: string;
|
|
491
|
+
commandId: string;
|
|
492
|
+
snapshot: ShellCommandSnapshotLike;
|
|
493
|
+
} | {
|
|
494
|
+
type: 'shell_write_result';
|
|
495
|
+
commandId: string;
|
|
496
|
+
output: ToolResultContentBlock[];
|
|
497
|
+
} | {
|
|
498
|
+
type: 'audit';
|
|
499
|
+
events: BashAuditEvent[];
|
|
500
|
+
} | {
|
|
501
|
+
type: 'retry_scheduled';
|
|
502
|
+
attempt: number;
|
|
503
|
+
delayMs: number;
|
|
504
|
+
code: string | null;
|
|
505
|
+
} | {
|
|
506
|
+
type: 'rejected';
|
|
507
|
+
command: string;
|
|
508
|
+
reason: string;
|
|
509
|
+
} | {
|
|
510
|
+
type: 'error';
|
|
511
|
+
message: string;
|
|
512
|
+
code?: string;
|
|
513
|
+
} | {
|
|
514
|
+
type: 'opened';
|
|
515
|
+
} | {
|
|
516
|
+
type: 'closed';
|
|
517
|
+
};
|
|
518
|
+
//#endregion
|
|
519
|
+
//#region src/transport.d.ts
|
|
520
|
+
interface AgentTransport<SendFrame, ReceiveFrame> {
|
|
521
|
+
send(frame: SendFrame): void;
|
|
522
|
+
onFrame(handler: (frame: ReceiveFrame) => void): () => void;
|
|
523
|
+
close(): void;
|
|
524
|
+
}
|
|
525
|
+
type AgentClientTransport = AgentTransport<ClientFrame, ServerFrame>;
|
|
526
|
+
type AgentServerTransport = AgentTransport<ServerFrame, ClientFrame>;
|
|
527
|
+
interface InProcessTransportPair {
|
|
528
|
+
client: AgentClientTransport;
|
|
529
|
+
server: AgentServerTransport;
|
|
530
|
+
}
|
|
531
|
+
declare function createInProcessTransportPair(): InProcessTransportPair;
|
|
532
|
+
//#endregion
|
|
533
|
+
export { AgentToolInvokeResult as A, DEFAULT_TURN_RETRY_POLICY as B, AgentSessionRestoreParams as C, AgentTool as D, AgentSystemPromptContext as E, CompactionWindow as F, isRetryableCode as H, DrainedTranscriptPatches as I, Transcript$1 as L, SessionEvent as M, SessionEventListener as N, AgentToolContext as O, ToolContinuation as P, TranscriptOptions as R, AgentSessionParams as S, AgentSessionStore as T, resolveRetryPolicy as U, TurnRetryPolicy as V, retryDelayMs as W, AgentHarnessRuntime as _, createInProcessTransportPair as a, AgentReferenceResolveContext as b, ConversationSummary as c, TranscriptPatch as d, AbortResult as f, AgentHarnessContext as g, AgentHarness as h, InProcessTransportPair as i, ExternalMutationReservation as j, AgentToolInvokeContext as k, ServerFrame as l, AgentDisposeContext as m, AgentServerTransport as n, ClientFrame as o, AbortTarget as p, AgentTransport as r, ClientSessionEvent as s, AgentClientTransport as t, ShellCommandSnapshotLike as u, AgentLifecycleEvent as v, AgentSessionSnapshot as w, AgentSessionOptions as x, AgentPromptContext as y, estimateTranscriptBlockTokens as z };
|