@gajae-code/agent-core 0.1.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/CHANGELOG.md +482 -0
- package/README.md +473 -0
- package/dist/types/agent-loop.d.ts +55 -0
- package/dist/types/agent.d.ts +334 -0
- package/dist/types/append-only-context.d.ts +113 -0
- package/dist/types/compaction/branch-summarization.d.ts +94 -0
- package/dist/types/compaction/compaction.d.ts +166 -0
- package/dist/types/compaction/entries.d.ts +103 -0
- package/dist/types/compaction/errors.d.ts +26 -0
- package/dist/types/compaction/index.d.ts +11 -0
- package/dist/types/compaction/messages.d.ts +61 -0
- package/dist/types/compaction/openai.d.ts +58 -0
- package/dist/types/compaction/pruning.d.ts +18 -0
- package/dist/types/compaction/utils.d.ts +32 -0
- package/dist/types/compaction.d.ts +1 -0
- package/dist/types/harmony-leak.d.ts +99 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/proxy.d.ts +84 -0
- package/dist/types/run-collector.d.ts +196 -0
- package/dist/types/telemetry.d.ts +588 -0
- package/dist/types/thinking.d.ts +17 -0
- package/dist/types/types.d.ts +407 -0
- package/package.json +75 -0
- package/src/agent-loop.ts +1279 -0
- package/src/agent.ts +1399 -0
- package/src/append-only-context.ts +297 -0
- package/src/compaction/branch-summarization.ts +339 -0
- package/src/compaction/compaction.ts +1065 -0
- package/src/compaction/entries.ts +133 -0
- package/src/compaction/errors.ts +31 -0
- package/src/compaction/index.ts +12 -0
- package/src/compaction/messages.ts +212 -0
- package/src/compaction/openai.ts +552 -0
- package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
- package/src/compaction/prompts/branch-summary-context.md +5 -0
- package/src/compaction/prompts/branch-summary-preamble.md +2 -0
- package/src/compaction/prompts/branch-summary.md +30 -0
- package/src/compaction/prompts/compaction-short-summary.md +9 -0
- package/src/compaction/prompts/compaction-summary-context.md +5 -0
- package/src/compaction/prompts/compaction-summary.md +38 -0
- package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
- package/src/compaction/prompts/compaction-update-summary.md +45 -0
- package/src/compaction/prompts/file-operations.md +10 -0
- package/src/compaction/prompts/handoff-document.md +49 -0
- package/src/compaction/prompts/summarization-system.md +3 -0
- package/src/compaction/pruning.ts +92 -0
- package/src/compaction/utils.ts +185 -0
- package/src/compaction.ts +1 -0
- package/src/harmony-leak.ts +427 -0
- package/src/index.ts +19 -0
- package/src/proxy.ts +326 -0
- package/src/run-collector.ts +631 -0
- package/src/telemetry.ts +2018 -0
- package/src/thinking.ts +19 -0
- package/src/types.ts +467 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/** Agent class that uses the agent-loop directly.
|
|
2
|
+
* No transport abstraction - calls streamSimple via the loop.
|
|
3
|
+
*/
|
|
4
|
+
import { type AssistantMessage, type AssistantMessageEvent, type CursorExecHandlers, type CursorToolResultHandler, type Effort, type ImageContent, type Message, type Model, type ProviderSessionState, type ServiceTier, type SimpleStreamOptions, type ThinkingBudgets, type ToolChoice } from "@gajae-code/ai";
|
|
5
|
+
import type { AppendOnlyContextManager } from "./append-only-context";
|
|
6
|
+
import type { HarmonyAuditEvent } from "./harmony-leak";
|
|
7
|
+
import type { AgentEvent, AgentLoopConfig, AgentMessage, AgentState, AgentTool, AgentToolContext, StreamFn, ToolCallContext } from "./types";
|
|
8
|
+
export declare class AgentBusyError extends Error {
|
|
9
|
+
constructor(message?: string);
|
|
10
|
+
}
|
|
11
|
+
export interface AgentOptions {
|
|
12
|
+
initialState?: Partial<AgentState>;
|
|
13
|
+
/**
|
|
14
|
+
* Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
|
|
15
|
+
* Default filters to user/assistant/toolResult and converts attachments.
|
|
16
|
+
*/
|
|
17
|
+
convertToLlm?: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Optional transform applied to context before convertToLlm.
|
|
20
|
+
* Use for context pruning, injecting external context, etc.
|
|
21
|
+
*/
|
|
22
|
+
transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Steering mode: "all" = send all steering messages at once, "one-at-a-time" = one per turn
|
|
25
|
+
*/
|
|
26
|
+
steeringMode?: "all" | "one-at-a-time";
|
|
27
|
+
/**
|
|
28
|
+
* Follow-up mode: "all" = send all follow-up messages at once, "one-at-a-time" = one per turn
|
|
29
|
+
*/
|
|
30
|
+
followUpMode?: "all" | "one-at-a-time";
|
|
31
|
+
/**
|
|
32
|
+
* When to interrupt tool execution for steering messages.
|
|
33
|
+
* - "immediate": check after each tool call (default)
|
|
34
|
+
* - "wait": defer steering until the current turn completes
|
|
35
|
+
*/
|
|
36
|
+
interruptMode?: "immediate" | "wait";
|
|
37
|
+
/**
|
|
38
|
+
* API format for Kimi Code provider: "openai" or "anthropic" (default: "anthropic")
|
|
39
|
+
*/
|
|
40
|
+
kimiApiFormat?: "openai" | "anthropic";
|
|
41
|
+
/** Hint that websocket transport should be preferred when supported by the provider implementation. */
|
|
42
|
+
preferWebsockets?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Custom stream function (for proxy backends, etc.). Default uses streamSimple.
|
|
45
|
+
*/
|
|
46
|
+
streamFn?: StreamFn;
|
|
47
|
+
/**
|
|
48
|
+
* Optional session identifier forwarded to LLM providers.
|
|
49
|
+
* Used by providers that support session-based caching (e.g., OpenAI code provider).
|
|
50
|
+
*/
|
|
51
|
+
sessionId?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Shared provider state map for session-scoped transport/session caches.
|
|
54
|
+
*/
|
|
55
|
+
providerSessionState?: Map<string, ProviderSessionState>;
|
|
56
|
+
/**
|
|
57
|
+
* Resolves an API key dynamically for each LLM call.
|
|
58
|
+
* Useful for expiring tokens (e.g., GitHub Copilot OAuth).
|
|
59
|
+
*/
|
|
60
|
+
getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Inspect or replace provider payloads before they are sent.
|
|
63
|
+
*/
|
|
64
|
+
onPayload?: SimpleStreamOptions["onPayload"];
|
|
65
|
+
/**
|
|
66
|
+
* Inspect provider response metadata after headers arrive and before streaming body consumption.
|
|
67
|
+
*/
|
|
68
|
+
onResponse?: SimpleStreamOptions["onResponse"];
|
|
69
|
+
/**
|
|
70
|
+
* Inspect raw Server-Sent Events from HTTP streaming providers.
|
|
71
|
+
*/
|
|
72
|
+
onSseEvent?: SimpleStreamOptions["onSseEvent"];
|
|
73
|
+
/**
|
|
74
|
+
* Inspect assistant streaming events before they are emitted to subscribers.
|
|
75
|
+
* Use this when abort decisions must happen before buffered events continue flowing.
|
|
76
|
+
*/
|
|
77
|
+
onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
|
|
78
|
+
/**
|
|
79
|
+
* Called when GPT-5 Harmony protocol leakage is detected and mitigated.
|
|
80
|
+
*/
|
|
81
|
+
onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Custom token budgets for thinking levels (token-based providers only).
|
|
84
|
+
*/
|
|
85
|
+
thinkingBudgets?: ThinkingBudgets;
|
|
86
|
+
/**
|
|
87
|
+
* Sampling temperature for LLM calls. `undefined` uses provider default.
|
|
88
|
+
*/
|
|
89
|
+
temperature?: number;
|
|
90
|
+
/** Additional sampling controls for providers that support them. */
|
|
91
|
+
topP?: number;
|
|
92
|
+
topK?: number;
|
|
93
|
+
minP?: number;
|
|
94
|
+
presencePenalty?: number;
|
|
95
|
+
repetitionPenalty?: number;
|
|
96
|
+
serviceTier?: ServiceTier;
|
|
97
|
+
/**
|
|
98
|
+
* If true, request that the underlying provider omit reasoning/thinking summaries
|
|
99
|
+
* from the response. The model still reasons internally; only the human-readable
|
|
100
|
+
* summary stream is suppressed. Useful when the UI hides thinking blocks anyway.
|
|
101
|
+
*/
|
|
102
|
+
hideThinkingSummary?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
|
|
105
|
+
* If the server's requested delay exceeds this value, the request fails immediately,
|
|
106
|
+
* allowing higher-level retry logic to handle it with user visibility.
|
|
107
|
+
* Default: 60000 (60 seconds). Set to 0 to disable the cap.
|
|
108
|
+
*/
|
|
109
|
+
maxRetryDelayMs?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Provides tool execution context, resolved per tool call.
|
|
112
|
+
* Use for late-bound UI or session state access.
|
|
113
|
+
*/
|
|
114
|
+
getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* Optional transform applied to tool call arguments before execution.
|
|
117
|
+
* Use for deobfuscating secrets or rewriting arguments.
|
|
118
|
+
*/
|
|
119
|
+
transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
|
|
120
|
+
/** Enable intent tracing schema injection/stripping in the harness. */
|
|
121
|
+
intentTracing?: boolean;
|
|
122
|
+
/** Dynamic tool choice override, resolved per LLM call. */
|
|
123
|
+
getToolChoice?: () => ToolChoice | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* Cursor exec handlers for local tool execution.
|
|
126
|
+
*/
|
|
127
|
+
cursorExecHandlers?: CursorExecHandlers;
|
|
128
|
+
/**
|
|
129
|
+
* Cursor tool result callback for exec tool responses.
|
|
130
|
+
*/
|
|
131
|
+
cursorOnToolResult?: CursorToolResultHandler;
|
|
132
|
+
/**
|
|
133
|
+
* Called after a tool call has been validated and is about to execute.
|
|
134
|
+
* See {@link AgentLoopConfig.beforeToolCall} for full semantics.
|
|
135
|
+
*/
|
|
136
|
+
beforeToolCall?: AgentLoopConfig["beforeToolCall"];
|
|
137
|
+
/**
|
|
138
|
+
* Called after a tool finishes executing, before `tool_execution_end` and the tool-result
|
|
139
|
+
* message are emitted. See {@link AgentLoopConfig.afterToolCall} for full semantics.
|
|
140
|
+
*/
|
|
141
|
+
afterToolCall?: AgentLoopConfig["afterToolCall"];
|
|
142
|
+
/**
|
|
143
|
+
* Opt-in OpenTelemetry instrumentation. Passing `{}` enables the loop's
|
|
144
|
+
* GenAI-semantic-convention spans using the global tracer provider. See
|
|
145
|
+
* {@link AgentLoopConfig.telemetry} for the full surface.
|
|
146
|
+
*/
|
|
147
|
+
telemetry?: AgentLoopConfig["telemetry"];
|
|
148
|
+
/**
|
|
149
|
+
* Immutable context mode — stabilizes system prompt + tool spec bytes
|
|
150
|
+
* across turns so DeepSeek/Anthropic prefix caches hit at maximum rate.
|
|
151
|
+
*/
|
|
152
|
+
appendOnlyContext?: AppendOnlyContextManager;
|
|
153
|
+
}
|
|
154
|
+
export interface AgentPromptOptions {
|
|
155
|
+
toolChoice?: ToolChoice;
|
|
156
|
+
}
|
|
157
|
+
export declare class Agent {
|
|
158
|
+
#private;
|
|
159
|
+
streamFn: StreamFn;
|
|
160
|
+
getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Hook invoked after tool arguments are validated and before execution.
|
|
163
|
+
* Reassign at any time to swap the implementation (e.g. on extension reload).
|
|
164
|
+
*/
|
|
165
|
+
beforeToolCall?: AgentLoopConfig["beforeToolCall"];
|
|
166
|
+
/**
|
|
167
|
+
* Hook invoked after tool execution and before `tool_execution_end` / tool-result
|
|
168
|
+
* message emission. Reassign at any time to swap the implementation.
|
|
169
|
+
*/
|
|
170
|
+
afterToolCall?: AgentLoopConfig["afterToolCall"];
|
|
171
|
+
constructor(opts?: AgentOptions);
|
|
172
|
+
/**
|
|
173
|
+
* Get the current session ID used for provider caching.
|
|
174
|
+
*/
|
|
175
|
+
get sessionId(): string | undefined;
|
|
176
|
+
/**
|
|
177
|
+
* Set the session ID for provider caching.
|
|
178
|
+
* Call this when switching sessions (new session, branch, resume).
|
|
179
|
+
*/
|
|
180
|
+
set sessionId(value: string | undefined);
|
|
181
|
+
/**
|
|
182
|
+
* Static metadata forwarded to every API request when no resolver is installed
|
|
183
|
+
* (e.g. `metadata.user_id` for Anthropic session attribution). Setting this
|
|
184
|
+
* clears any installed resolver.
|
|
185
|
+
*
|
|
186
|
+
* For live/provider-aware metadata (e.g. Anthropic OAuth `account_uuid` that
|
|
187
|
+
* must reflect the credential selected per-request), use
|
|
188
|
+
* {@link setMetadataResolver} and read via {@link metadataForProvider}.
|
|
189
|
+
*/
|
|
190
|
+
get metadata(): Record<string, unknown> | undefined;
|
|
191
|
+
set metadata(value: Record<string, unknown> | undefined);
|
|
192
|
+
/**
|
|
193
|
+
* Resolve request metadata for the given provider at call time. When a
|
|
194
|
+
* resolver is installed via {@link setMetadataResolver}, it is invoked with
|
|
195
|
+
* the provider string so the result can be scoped (e.g. `account_uuid` is
|
|
196
|
+
* only included for `"anthropic"` requests). Falls back to the static
|
|
197
|
+
* {@link metadata} value when no resolver is set.
|
|
198
|
+
*/
|
|
199
|
+
metadataForProvider(provider: string): Record<string, unknown> | undefined;
|
|
200
|
+
/**
|
|
201
|
+
* Install a function that resolves request metadata at call time. The
|
|
202
|
+
* resolver receives the target provider string and can gate provider-specific
|
|
203
|
+
* fields (e.g. `account_uuid` only for `"anthropic"`). Invoked per LLM
|
|
204
|
+
* request by `agent-loop` after `getApiKey` selects the session-sticky
|
|
205
|
+
* credential. Pass `undefined` to clear and revert to the static
|
|
206
|
+
* {@link metadata} value.
|
|
207
|
+
*/
|
|
208
|
+
setMetadataResolver(resolver: ((provider: string) => Record<string, unknown> | undefined) | undefined): void;
|
|
209
|
+
/**
|
|
210
|
+
* Read the active OpenTelemetry configuration. Returns `undefined` when
|
|
211
|
+
* instrumentation is disabled. Callers spawning child runs (e.g. subagent
|
|
212
|
+
* dispatch) forward this to the child's loop so its spans appear under the
|
|
213
|
+
* parent's active context with the subagent's own identity stamped.
|
|
214
|
+
*/
|
|
215
|
+
get telemetry(): AgentLoopConfig["telemetry"] | undefined;
|
|
216
|
+
/**
|
|
217
|
+
* Replace the active OpenTelemetry configuration. Pass `undefined` to
|
|
218
|
+
* disable instrumentation. Applies to the *next* `agentLoop` invocation —
|
|
219
|
+
* in-flight loops keep the configuration they started with.
|
|
220
|
+
*/
|
|
221
|
+
setTelemetry(telemetry: AgentLoopConfig["telemetry"] | undefined): void;
|
|
222
|
+
/**
|
|
223
|
+
* Get provider-scoped mutable session state store.
|
|
224
|
+
*/
|
|
225
|
+
get providerSessionState(): Map<string, ProviderSessionState> | undefined;
|
|
226
|
+
/**
|
|
227
|
+
* Set provider-scoped mutable session state store.
|
|
228
|
+
*/
|
|
229
|
+
set providerSessionState(value: Map<string, ProviderSessionState> | undefined);
|
|
230
|
+
/**
|
|
231
|
+
* Get the current thinking budgets.
|
|
232
|
+
*/
|
|
233
|
+
get thinkingBudgets(): ThinkingBudgets | undefined;
|
|
234
|
+
/**
|
|
235
|
+
* Set custom thinking budgets for token-based providers.
|
|
236
|
+
*/
|
|
237
|
+
set thinkingBudgets(value: ThinkingBudgets | undefined);
|
|
238
|
+
/**
|
|
239
|
+
* Get the current sampling temperature.
|
|
240
|
+
*/
|
|
241
|
+
get temperature(): number | undefined;
|
|
242
|
+
/**
|
|
243
|
+
* Set sampling temperature for LLM calls. `undefined` uses provider default.
|
|
244
|
+
*/
|
|
245
|
+
set temperature(value: number | undefined);
|
|
246
|
+
get topP(): number | undefined;
|
|
247
|
+
set topP(value: number | undefined);
|
|
248
|
+
get topK(): number | undefined;
|
|
249
|
+
set topK(value: number | undefined);
|
|
250
|
+
get minP(): number | undefined;
|
|
251
|
+
set minP(value: number | undefined);
|
|
252
|
+
get presencePenalty(): number | undefined;
|
|
253
|
+
set presencePenalty(value: number | undefined);
|
|
254
|
+
get repetitionPenalty(): number | undefined;
|
|
255
|
+
set repetitionPenalty(value: number | undefined);
|
|
256
|
+
get serviceTier(): ServiceTier | undefined;
|
|
257
|
+
set serviceTier(value: ServiceTier | undefined);
|
|
258
|
+
get hideThinkingSummary(): boolean | undefined;
|
|
259
|
+
set hideThinkingSummary(value: boolean | undefined);
|
|
260
|
+
/**
|
|
261
|
+
* Get the current max retry delay in milliseconds.
|
|
262
|
+
*/
|
|
263
|
+
get maxRetryDelayMs(): number | undefined;
|
|
264
|
+
/**
|
|
265
|
+
* Set the maximum delay to wait for server-requested retries.
|
|
266
|
+
* Set to 0 to disable the cap.
|
|
267
|
+
*/
|
|
268
|
+
set maxRetryDelayMs(value: number | undefined);
|
|
269
|
+
get state(): AgentState;
|
|
270
|
+
get appendOnlyContext(): AppendOnlyContextManager | undefined;
|
|
271
|
+
setAppendOnlyContext(manager?: AppendOnlyContextManager): void;
|
|
272
|
+
subscribe(fn: (e: AgentEvent) => void): () => void;
|
|
273
|
+
setProviderResponseInterceptor(fn: SimpleStreamOptions["onResponse"] | undefined): void;
|
|
274
|
+
setRawSseEventInterceptor(fn: SimpleStreamOptions["onSseEvent"] | undefined): void;
|
|
275
|
+
setAssistantMessageEventInterceptor(fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined): void;
|
|
276
|
+
setOnBeforeYield(fn: (() => Promise<void> | void) | undefined): void;
|
|
277
|
+
emitExternalEvent(event: AgentEvent): void;
|
|
278
|
+
createExternalEventEmitterForCurrentRun(): ((event: AgentEvent) => void) | undefined;
|
|
279
|
+
setSystemPrompt(v: string[]): void;
|
|
280
|
+
setModel(m: Model): void;
|
|
281
|
+
setThinkingLevel(l: Effort | undefined): void;
|
|
282
|
+
setSteeringMode(mode: "all" | "one-at-a-time"): void;
|
|
283
|
+
getSteeringMode(): "all" | "one-at-a-time";
|
|
284
|
+
setFollowUpMode(mode: "all" | "one-at-a-time"): void;
|
|
285
|
+
getFollowUpMode(): "all" | "one-at-a-time";
|
|
286
|
+
setInterruptMode(mode: "immediate" | "wait"): void;
|
|
287
|
+
getInterruptMode(): "immediate" | "wait";
|
|
288
|
+
setTools(t: AgentTool<any>[]): void;
|
|
289
|
+
replaceMessages(ms: AgentMessage[]): void;
|
|
290
|
+
appendMessage(m: AgentMessage): void;
|
|
291
|
+
popMessage(): AgentMessage | undefined;
|
|
292
|
+
/**
|
|
293
|
+
* Queue a steering message to interrupt the agent mid-run.
|
|
294
|
+
* Delivered after current tool execution, skips remaining tools.
|
|
295
|
+
*/
|
|
296
|
+
steer(m: AgentMessage): void;
|
|
297
|
+
/**
|
|
298
|
+
* Queue a follow-up message to be processed after the agent finishes.
|
|
299
|
+
* Delivered only when agent has no more tool calls or steering messages.
|
|
300
|
+
*/
|
|
301
|
+
followUp(m: AgentMessage): void;
|
|
302
|
+
clearSteeringQueue(): void;
|
|
303
|
+
clearFollowUpQueue(): void;
|
|
304
|
+
clearAllQueues(): void;
|
|
305
|
+
hasQueuedMessages(): boolean;
|
|
306
|
+
/**
|
|
307
|
+
* Remove and return the last steering message from the queue (LIFO).
|
|
308
|
+
* Used by dequeue keybinding.
|
|
309
|
+
*/
|
|
310
|
+
popLastSteer(): AgentMessage | undefined;
|
|
311
|
+
/**
|
|
312
|
+
* Remove and return the last follow-up message from the queue (LIFO).
|
|
313
|
+
* Used by dequeue keybinding.
|
|
314
|
+
*/
|
|
315
|
+
popLastFollowUp(): AgentMessage | undefined;
|
|
316
|
+
clearMessages(): void;
|
|
317
|
+
abort(): void;
|
|
318
|
+
/**
|
|
319
|
+
* Force the current run out of the busy/streaming state when cooperative abort
|
|
320
|
+
* did not drain. The abandoned provider/tool stream may still settle later, so
|
|
321
|
+
* #runLoop guards every state mutation with a run id.
|
|
322
|
+
*/
|
|
323
|
+
forceAbort(reason?: string): boolean;
|
|
324
|
+
waitForIdle(): Promise<void>;
|
|
325
|
+
reset(): void;
|
|
326
|
+
/** Send a prompt with an AgentMessage */
|
|
327
|
+
prompt(message: AgentMessage | AgentMessage[], options?: AgentPromptOptions): Promise<void>;
|
|
328
|
+
prompt(input: string, options?: AgentPromptOptions): Promise<void>;
|
|
329
|
+
prompt(input: string, images?: ImageContent[], options?: AgentPromptOptions): Promise<void>;
|
|
330
|
+
/**
|
|
331
|
+
* Continue from current context (used for retries and resuming queued messages).
|
|
332
|
+
*/
|
|
333
|
+
continue(): Promise<void>;
|
|
334
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append-only context mode — stabilizes the byte prefix sent to the LLM
|
|
3
|
+
* across turns so provider prefix caches (DeepSeek, Anthropic, etc.)
|
|
4
|
+
* hit at the maximum possible rate.
|
|
5
|
+
*
|
|
6
|
+
* Two mechanisms:
|
|
7
|
+
*
|
|
8
|
+
* 1. **StablePrefix** — system prompt + tool specs are computed once
|
|
9
|
+
* and frozen. Subsequent turns reuse the exact same byte sequence
|
|
10
|
+
* unless `invalidate()` is called (e.g. after MCP reconnect).
|
|
11
|
+
*
|
|
12
|
+
* 2. **AppendOnlyLog** — messages only grow; prior turns are never
|
|
13
|
+
* re-serialized. Combined with a stable prefix, only the user's new
|
|
14
|
+
* message delta is a cache miss each turn.
|
|
15
|
+
*/
|
|
16
|
+
import type { Context, Message, Tool } from "@gajae-code/ai";
|
|
17
|
+
import type { AgentContext } from "./types";
|
|
18
|
+
/** Frozen system prompt + tool spec snapshot. */
|
|
19
|
+
export interface StablePrefixSnapshot {
|
|
20
|
+
systemPrompt: string[];
|
|
21
|
+
tools: Tool[];
|
|
22
|
+
fingerprint: string;
|
|
23
|
+
}
|
|
24
|
+
/** Options threaded through `build()` so the snapshot reflects loop-time settings. */
|
|
25
|
+
export interface BuildOptions {
|
|
26
|
+
/** Inject the `_i` intent field into tool schemas (must match agent-loop's normalizeTools). */
|
|
27
|
+
intentTracing: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A frozen prefix (system prompt + tools) that produces stable byte
|
|
31
|
+
* sequences across `build()` calls.
|
|
32
|
+
*
|
|
33
|
+
* The first `build()` snapshots the live state. Subsequent calls reuse
|
|
34
|
+
* the cached copy until `invalidate()` is called or the live state's
|
|
35
|
+
* fingerprint changes.
|
|
36
|
+
*/
|
|
37
|
+
export declare class StablePrefix {
|
|
38
|
+
#private;
|
|
39
|
+
get fingerprint(): string;
|
|
40
|
+
get version(): number;
|
|
41
|
+
get built(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Build or rebuild from live context.
|
|
44
|
+
* Returns `true` if the prefix actually changed (cache miss imminent).
|
|
45
|
+
*/
|
|
46
|
+
build(context: AgentContext, options: BuildOptions): boolean;
|
|
47
|
+
/** Force rebuild on the next `build()` call. */
|
|
48
|
+
invalidate(): void;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the cached prefix.
|
|
51
|
+
* @throws if `build()` was never called.
|
|
52
|
+
*/
|
|
53
|
+
toContext(): {
|
|
54
|
+
systemPrompt: string[];
|
|
55
|
+
tools: Tool[];
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Append-only message log at the `Message[]` (provider-level) layer.
|
|
60
|
+
*
|
|
61
|
+
* The only mutation path is `replaceTail()`, reserved for compaction.
|
|
62
|
+
* Every other operation is append-only.
|
|
63
|
+
*/
|
|
64
|
+
export declare class AppendOnlyLog {
|
|
65
|
+
#private;
|
|
66
|
+
get length(): number;
|
|
67
|
+
append(message: any): void;
|
|
68
|
+
extend(messages: any[]): void;
|
|
69
|
+
/** Replace the last entry — only legal for compaction. */
|
|
70
|
+
replaceTail(replacement: any): void;
|
|
71
|
+
/** Returns a shallow copy of all entries. */
|
|
72
|
+
toMessages(): Message[];
|
|
73
|
+
/** Direct readonly access for in-place inspection. */
|
|
74
|
+
entries(): readonly Message[];
|
|
75
|
+
clear(): void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Manages a stable prefix + append-only log for the agent loop.
|
|
79
|
+
*
|
|
80
|
+
* Call `build(context)` each turn to get a `Context` with stable
|
|
81
|
+
* `systemPrompt` and `tools` and append-only messages. Call
|
|
82
|
+
* `syncMessages(normalizedMessages)` after `convertToLlm` each
|
|
83
|
+
* turn to keep the log in sync.
|
|
84
|
+
*
|
|
85
|
+
* Example:
|
|
86
|
+
* ```
|
|
87
|
+
* const mgr = new AppendOnlyContextManager();
|
|
88
|
+
* const ctx = mgr.build(context); // first call snapshots prefix
|
|
89
|
+
* mgr.syncMessages(normalized); // grow the log
|
|
90
|
+
* ctx = mgr.build(context); // subsequent calls use cache
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare class AppendOnlyContextManager {
|
|
94
|
+
#private;
|
|
95
|
+
readonly prefix: StablePrefix;
|
|
96
|
+
readonly log: AppendOnlyLog;
|
|
97
|
+
build(context: AgentContext, options: BuildOptions): Context;
|
|
98
|
+
/**
|
|
99
|
+
* Sync normalized (provider-level) messages into the append-only log.
|
|
100
|
+
*
|
|
101
|
+
* Detects both compaction (shorter array) and in-place rewrites
|
|
102
|
+
* (same length, changed content via a rolling digest).
|
|
103
|
+
*/
|
|
104
|
+
syncMessages(normalizedMessages: any[]): void;
|
|
105
|
+
/** Reset prefix + log for a model/provider switch while mode stays active. */
|
|
106
|
+
invalidateForModelChange(): void;
|
|
107
|
+
/** Reset the sync cursor AND clear the log. */
|
|
108
|
+
resetSyncCursor(): void;
|
|
109
|
+
appendMessage(message: any): void;
|
|
110
|
+
replaceTailMessage(message: any): void;
|
|
111
|
+
invalidate(): void;
|
|
112
|
+
reset(context: AgentContext, options: BuildOptions): void;
|
|
113
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branch summarization for tree navigation.
|
|
3
|
+
*
|
|
4
|
+
* When navigating to a different point in the session tree, this generates
|
|
5
|
+
* a summary of the branch being left so context isn't lost.
|
|
6
|
+
*/
|
|
7
|
+
import type { Model } from "@gajae-code/ai";
|
|
8
|
+
import { type AgentTelemetry } from "../telemetry";
|
|
9
|
+
import type { AgentMessage } from "../types";
|
|
10
|
+
import type { ReadonlySessionManager, SessionEntry } from "./entries";
|
|
11
|
+
import { type ConvertToLlm } from "./messages";
|
|
12
|
+
import { type FileOperations } from "./utils";
|
|
13
|
+
export interface BranchSummaryResult {
|
|
14
|
+
summary?: string;
|
|
15
|
+
readFiles?: string[];
|
|
16
|
+
modifiedFiles?: string[];
|
|
17
|
+
aborted?: boolean;
|
|
18
|
+
error?: string;
|
|
19
|
+
}
|
|
20
|
+
/** Details stored in BranchSummaryEntry.details for file tracking */
|
|
21
|
+
export interface BranchSummaryDetails {
|
|
22
|
+
readFiles: string[];
|
|
23
|
+
modifiedFiles: string[];
|
|
24
|
+
}
|
|
25
|
+
export type { FileOperations } from "./utils";
|
|
26
|
+
export interface BranchPreparation {
|
|
27
|
+
/** Messages extracted for summarization, in chronological order */
|
|
28
|
+
messages: AgentMessage[];
|
|
29
|
+
/** File operations extracted from tool calls */
|
|
30
|
+
fileOps: FileOperations;
|
|
31
|
+
/** Total estimated tokens in messages */
|
|
32
|
+
totalTokens: number;
|
|
33
|
+
}
|
|
34
|
+
export interface CollectEntriesResult {
|
|
35
|
+
/** Entries to summarize, in chronological order */
|
|
36
|
+
entries: SessionEntry[];
|
|
37
|
+
/** Common ancestor between old and new position, if any */
|
|
38
|
+
commonAncestorId: string | null;
|
|
39
|
+
}
|
|
40
|
+
export interface GenerateBranchSummaryOptions {
|
|
41
|
+
/** Model to use for summarization */
|
|
42
|
+
model: Model;
|
|
43
|
+
/** API key for the model */
|
|
44
|
+
apiKey: string;
|
|
45
|
+
/** Abort signal for cancellation */
|
|
46
|
+
signal: AbortSignal;
|
|
47
|
+
/** Optional custom instructions for summarization */
|
|
48
|
+
customInstructions?: string;
|
|
49
|
+
/** Tokens reserved for prompt + LLM response (default 16384) */
|
|
50
|
+
reserveTokens?: number;
|
|
51
|
+
/** Optional metadata forwarded to the underlying API request (e.g. user_id for session attribution). */
|
|
52
|
+
metadata?: Record<string, unknown>;
|
|
53
|
+
/** Convert app-specific messages before serializing the branch summary prompt. */
|
|
54
|
+
convertToLlm?: ConvertToLlm;
|
|
55
|
+
/**
|
|
56
|
+
* Optional telemetry handle. When provided, the branch summary LLM call is
|
|
57
|
+
* wrapped in an OTEL chat span tagged with `pi.gen_ai.oneshot.kind = "branch_summary"`.
|
|
58
|
+
*/
|
|
59
|
+
telemetry?: AgentTelemetry;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Collect entries that should be summarized when navigating from one position to another.
|
|
63
|
+
*
|
|
64
|
+
* Walks from oldLeafId back to the common ancestor with targetId, collecting entries
|
|
65
|
+
* along the way. Does NOT stop at compaction boundaries - those are included and their
|
|
66
|
+
* summaries become context.
|
|
67
|
+
*
|
|
68
|
+
* @param session - Session manager (read-only access)
|
|
69
|
+
* @param oldLeafId - Current position (where we're navigating from)
|
|
70
|
+
* @param targetId - Target position (where we're navigating to)
|
|
71
|
+
* @returns Entries to summarize and the common ancestor
|
|
72
|
+
*/
|
|
73
|
+
export declare function collectEntriesForBranchSummary(session: ReadonlySessionManager, oldLeafId: string | null, targetId: string): CollectEntriesResult;
|
|
74
|
+
/**
|
|
75
|
+
* Prepare entries for summarization with token budget.
|
|
76
|
+
*
|
|
77
|
+
* Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.
|
|
78
|
+
* This ensures we keep the most recent context when the branch is too long.
|
|
79
|
+
*
|
|
80
|
+
* Also collects file operations from:
|
|
81
|
+
* - Tool calls in assistant messages
|
|
82
|
+
* - Existing branch_summary entries' details (for cumulative tracking)
|
|
83
|
+
*
|
|
84
|
+
* @param entries - Entries in chronological order
|
|
85
|
+
* @param tokenBudget - Maximum tokens to include (0 = no limit)
|
|
86
|
+
*/
|
|
87
|
+
export declare function prepareBranchEntries(entries: SessionEntry[], tokenBudget?: number): BranchPreparation;
|
|
88
|
+
/**
|
|
89
|
+
* Generate a summary of abandoned branch entries.
|
|
90
|
+
*
|
|
91
|
+
* @param entries - Session entries to summarize (chronological order)
|
|
92
|
+
* @param options - Generation options
|
|
93
|
+
*/
|
|
94
|
+
export declare function generateBranchSummary(entries: SessionEntry[], options: GenerateBranchSummaryOptions): Promise<BranchSummaryResult>;
|