@linxiraos/pi-agent-core 1.0.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +1263 -0
  2. package/README.md +473 -0
  3. package/dist/types/agent-loop.d.ts +137 -0
  4. package/dist/types/agent.d.ts +465 -0
  5. package/dist/types/append-only-context.d.ts +131 -0
  6. package/dist/types/compaction/branch-summarization.d.ts +101 -0
  7. package/dist/types/compaction/compaction-v2-streaming.d.ts +85 -0
  8. package/dist/types/compaction/compaction.d.ts +307 -0
  9. package/dist/types/compaction/entries.d.ts +111 -0
  10. package/dist/types/compaction/errors.d.ts +38 -0
  11. package/dist/types/compaction/index.d.ts +13 -0
  12. package/dist/types/compaction/message-cache.d.ts +23 -0
  13. package/dist/types/compaction/messages.d.ts +79 -0
  14. package/dist/types/compaction/openai.d.ts +117 -0
  15. package/dist/types/compaction/pruning.d.ts +105 -0
  16. package/dist/types/compaction/shake.d.ts +97 -0
  17. package/dist/types/compaction/tool-protection.d.ts +19 -0
  18. package/dist/types/compaction/utils.d.ts +61 -0
  19. package/dist/types/compaction.d.ts +1 -0
  20. package/dist/types/index.d.ts +13 -0
  21. package/dist/types/pause.d.ts +46 -0
  22. package/dist/types/proxy.d.ts +91 -0
  23. package/dist/types/replay-policy.d.ts +5 -0
  24. package/dist/types/run-collector.d.ts +196 -0
  25. package/dist/types/telemetry.d.ts +608 -0
  26. package/dist/types/thinking.d.ts +18 -0
  27. package/dist/types/tokenizer.d.ts +2 -0
  28. package/dist/types/types.d.ts +781 -0
  29. package/dist/types/utils/yield.d.ts +71 -0
  30. package/package.json +79 -0
  31. package/src/agent-loop.ts +2926 -0
  32. package/src/agent.ts +1748 -0
  33. package/src/append-only-context.ts +348 -0
  34. package/src/compaction/branch-summarization.ts +369 -0
  35. package/src/compaction/compaction-v2-streaming.ts +846 -0
  36. package/src/compaction/compaction.ts +1733 -0
  37. package/src/compaction/entries.ts +141 -0
  38. package/src/compaction/errors.ts +47 -0
  39. package/src/compaction/index.ts +14 -0
  40. package/src/compaction/message-cache.ts +92 -0
  41. package/src/compaction/messages.ts +241 -0
  42. package/src/compaction/openai.ts +992 -0
  43. package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
  44. package/src/compaction/prompts/branch-summary-context.md +5 -0
  45. package/src/compaction/prompts/branch-summary-preamble.md +2 -0
  46. package/src/compaction/prompts/branch-summary.md +30 -0
  47. package/src/compaction/prompts/compaction-short-summary.md +9 -0
  48. package/src/compaction/prompts/compaction-summary-context.md +5 -0
  49. package/src/compaction/prompts/compaction-summary.md +38 -0
  50. package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
  51. package/src/compaction/prompts/compaction-update-summary.md +45 -0
  52. package/src/compaction/prompts/context-window-truncated-output.md +1 -0
  53. package/src/compaction/prompts/file-operations.md +5 -0
  54. package/src/compaction/prompts/handoff-document.md +49 -0
  55. package/src/compaction/prompts/snapcompact-archive-context.md +3 -0
  56. package/src/compaction/prompts/summarization-system.md +3 -0
  57. package/src/compaction/pruning.ts +427 -0
  58. package/src/compaction/shake.ts +446 -0
  59. package/src/compaction/tool-protection.ts +65 -0
  60. package/src/compaction/utils.ts +343 -0
  61. package/src/compaction.ts +1 -0
  62. package/src/index.ts +26 -0
  63. package/src/pause.ts +107 -0
  64. package/src/proxy.ts +391 -0
  65. package/src/replay-policy.ts +13 -0
  66. package/src/run-collector.ts +631 -0
  67. package/src/telemetry.ts +2078 -0
  68. package/src/thinking.ts +20 -0
  69. package/src/tokenizer.ts +27 -0
  70. package/src/types.ts +875 -0
  71. package/src/utils/yield.ts +183 -0
@@ -0,0 +1,465 @@
1
+ import { type ApiKey, type AssistantMessage, type AssistantMessageEvent, type Context, type CursorExecHandlers, type CursorToolResultHandler, type Effort, type ImageContent, type Message, type Model, type ProviderSessionState, type ServiceTier, type SimpleStreamOptions, type ThinkingBudgets, type ToolChoice } from "@linxiraos/pi-ai";
2
+ import type { Dialect } from "@linxiraos/pi-ai/dialect";
3
+ import type { HarmonyAuditEvent } from "@linxiraos/pi-ai/utils/harmony-leak";
4
+ import type { AppendOnlyContextManager } from "./append-only-context.js";
5
+ import type { AgentBeforeModelCall, AgentEvent, AgentLoopConfig, AgentMessage, AgentState, AgentTool, AgentToolContext, AgentTurnEndContext, AsideMessage, StreamFn, ToolCallContext, ToolChoiceDirective } from "./types.js";
6
+ export declare class AgentBusyError extends Error {
7
+ constructor(message?: string);
8
+ }
9
+ export interface AgentOptions {
10
+ initialState?: Partial<AgentState>;
11
+ /**
12
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
13
+ * Default filters to user/assistant/toolResult and converts attachments.
14
+ */
15
+ convertToLlm?: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
16
+ /**
17
+ * Optional transform applied to context before convertToLlm.
18
+ * Use for context pruning, injecting external context, etc.
19
+ */
20
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
21
+ /**
22
+ * Optional transform applied after provider context assembly and before
23
+ * telemetry capture/provider send.
24
+ */
25
+ transformProviderContext?: (context: Context, model: Model) => Context | Promise<Context>;
26
+ /**
27
+ * Steering mode: "all" = send all steering messages at once, "one-at-a-time" = one per turn
28
+ */
29
+ steeringMode?: "all" | "one-at-a-time";
30
+ /**
31
+ * Follow-up mode: "all" = send all follow-up messages at once, "one-at-a-time" = one per turn
32
+ */
33
+ followUpMode?: "all" | "one-at-a-time";
34
+ /**
35
+ * When to interrupt tool execution for steering messages.
36
+ * - "immediate": check after each tool call (default)
37
+ * - "wait": defer steering until the current turn completes
38
+ */
39
+ interruptMode?: "immediate" | "wait";
40
+ /**
41
+ * API format for Kimi Code provider: "openai" or "anthropic" (default: "anthropic")
42
+ */
43
+ kimiApiFormat?: "openai" | "anthropic";
44
+ /** Hint that websocket transport should be preferred when supported by the provider implementation. */
45
+ preferWebsockets?: boolean;
46
+ /**
47
+ * Custom stream function (for proxy backends, etc.). Default uses streamSimple.
48
+ */
49
+ streamFn?: StreamFn;
50
+ /** Absolute wall-clock deadline in Unix epoch milliseconds. */
51
+ deadline?: number;
52
+ /**
53
+ * Optional session identifier forwarded to LLM providers.
54
+ * Used by providers that support session-based caching (e.g., OpenAI Codex).
55
+ */
56
+ sessionId?: string;
57
+ /**
58
+ * Optional prompt cache key forwarded to LLM providers.
59
+ * When omitted, providers may fall back to sessionId.
60
+ */
61
+ promptCacheKey?: string;
62
+ /**
63
+ * Shared provider state map for session-scoped transport/session caches.
64
+ */
65
+ providerSessionState?: Map<string, ProviderSessionState>;
66
+ /**
67
+ * Resolves an API key or resolver dynamically for each LLM call.
68
+ * Useful for expiring tokens and model-scoped credential routing.
69
+ */
70
+ getApiKey?: (model: Model) => Promise<ApiKey | undefined> | ApiKey | undefined;
71
+ /**
72
+ * Inspect or replace provider payloads before they are sent.
73
+ */
74
+ onPayload?: SimpleStreamOptions["onPayload"];
75
+ /**
76
+ * Inspect provider response metadata after headers arrive and before streaming body consumption.
77
+ */
78
+ onResponse?: SimpleStreamOptions["onResponse"];
79
+ /**
80
+ * Inspect raw Server-Sent Events from HTTP streaming providers.
81
+ */
82
+ onSseEvent?: SimpleStreamOptions["onSseEvent"];
83
+ /**
84
+ * Inspect assistant streaming events before they are emitted to subscribers.
85
+ * Use this when abort decisions must happen before buffered events continue flowing.
86
+ */
87
+ onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
88
+ /**
89
+ * Called when GPT-5 Harmony protocol leakage is detected and mitigated.
90
+ */
91
+ onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
92
+ /**
93
+ * Custom token budgets for thinking levels (token-based providers only).
94
+ */
95
+ thinkingBudgets?: ThinkingBudgets;
96
+ /**
97
+ * Sampling temperature for LLM calls. `undefined` uses provider default.
98
+ */
99
+ temperature?: number;
100
+ /** Additional sampling controls for providers that support them. */
101
+ topP?: number;
102
+ topK?: number;
103
+ minP?: number;
104
+ presencePenalty?: number;
105
+ repetitionPenalty?: number;
106
+ serviceTier?: ServiceTier;
107
+ /**
108
+ * Per-call effective service-tier resolver. When set, it authoritatively
109
+ * supplies the request's tier (replacing the static `serviceTier` and its
110
+ * telemetry) per model — used to scope a provider/model into a priority
111
+ * serving path without mutating the shared session `serviceTier`.
112
+ */
113
+ serviceTierResolver?: (model: Model) => ServiceTier | undefined;
114
+ /**
115
+ * If true, request that the underlying provider omit reasoning/thinking summaries
116
+ * from the response. The model still reasons internally; only the human-readable
117
+ * summary stream is suppressed. Useful when the UI hides thinking blocks anyway.
118
+ */
119
+ hideThinkingSummary?: boolean;
120
+ /**
121
+ * Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
122
+ * If the server's requested delay exceeds this value, the request fails immediately,
123
+ * allowing higher-level retry logic to handle it with user visibility.
124
+ * Default: 60000 (60 seconds). Set to 0 to disable the cap.
125
+ */
126
+ maxRetryDelayMs?: number;
127
+ /**
128
+ * Provides tool execution context, resolved per tool call.
129
+ * Use for late-bound UI or session state access.
130
+ */
131
+ getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
132
+ /**
133
+ * Optional transform applied to tool call arguments before execution.
134
+ * Use for deobfuscating secrets or rewriting arguments.
135
+ */
136
+ transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
137
+ /**
138
+ * Resolve a tool call whose name matched no advertised tool. Lets hosts
139
+ * route calls to tools exposed through side transports (e.g. `xd://`
140
+ * device mounts) instead of failing with "Tool not found".
141
+ */
142
+ resolveFallbackTool?: (name: string) => AgentTool<any> | undefined;
143
+ /** Enable intent tracing schema injection/stripping in the harness. */
144
+ intentTracing?: boolean;
145
+ /**
146
+ * Strip tool descriptions from provider-bound tool specs (top-level + nested
147
+ * schema annotations). Use when the full catalog is rendered into the system
148
+ * prompt so descriptions are not duplicated on the wire. Native tool calling only.
149
+ */
150
+ pruneToolDescriptions?: boolean;
151
+ /** Owned tool-calling dialect. Undefined keeps provider-native tool calling. */
152
+ dialect?: Dialect;
153
+ /**
154
+ * When owned tool calling is active and the model fabricates a tool result
155
+ * mid-turn: `true` (default) aborts the provider request immediately; `false`
156
+ * drains the request and discards the fabricated continuation. Forwarded to
157
+ * the loop's {@link AgentLoopConfig.abortOnFabricatedToolResult}.
158
+ */
159
+ abortOnFabricatedToolResult?: boolean;
160
+ /** Dynamic tool-choice directive (hard {@link ToolChoice} or {@link SoftToolRequirement}), resolved once per turn. */
161
+ getToolChoice?: () => ToolChoiceDirective | undefined;
162
+ /** Reject a deferred hard choice when its named tool is no longer active. */
163
+ onToolChoiceUnavailable?: () => void;
164
+ /**
165
+ * Cursor exec handlers for local tool execution.
166
+ */
167
+ cursorExecHandlers?: CursorExecHandlers;
168
+ /** Additional tools Cursor executes through its MCP request-context bridge, resolved before each provider call. */
169
+ getCursorTools?: () => AgentTool[];
170
+ /**
171
+ * Optional rewrite of Cursor exec-channel tool results. May return a Promise.
172
+ *
173
+ * The Agent reserves the original result in its Cursor result buffer first,
174
+ * then awaits this hook and patches the reserved entry in place. That keeps
175
+ * the call paired even if `message_end` arrives while the Promise is still
176
+ * pending, and `#emitCursorSplitAssistantMessage` waits for any transformer
177
+ * still in flight before persisting, so a late rewrite is not lost. A
178
+ * rejecting transformer is swallowed and the reserved payload stands in.
179
+ * Hosts that only pass `cursorExecHandlers` (the coding-agent path) never
180
+ * hit this hook.
181
+ */
182
+ cursorOnToolResult?: CursorToolResultHandler;
183
+ /** Current working directory used by local tool execution. */
184
+ cwd?: string;
185
+ /**
186
+ * Resolver for the live working directory, re-read on every turn. When set, it
187
+ * overrides the static {@link cwd} at config-build time so a session move
188
+ * (`/move`, which updates the host's cwd without reconstructing the Agent) is
189
+ * reflected in provider options — e.g. GitLab Duo Agent namespace/project
190
+ * discovery keys off this cwd's git remote. Falls back to `cwd` when it returns
191
+ * `undefined`.
192
+ */
193
+ cwdResolver?: () => string | undefined;
194
+ /**
195
+ * Called after a tool call has been validated and is about to execute.
196
+ * See {@link AgentLoopConfig.beforeToolCall} for full semantics.
197
+ */
198
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
199
+ /**
200
+ * Called after a tool finishes executing, before `tool_execution_end` and the tool-result
201
+ * message are emitted. See {@link AgentLoopConfig.afterToolCall} for full semantics.
202
+ */
203
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
204
+ /**
205
+ * Called once an assistant message is finalized, before it reaches the
206
+ * context, the UI, or tool dispatch. May mutate the message in place (text +
207
+ * tool-call arguments). See {@link AgentLoopConfig.transformAssistantMessage}.
208
+ */
209
+ transformAssistantMessage?: AgentLoopConfig["transformAssistantMessage"];
210
+ /**
211
+ * Opt-in OpenTelemetry instrumentation. Passing `{}` enables the loop's
212
+ * GenAI-semantic-convention spans using the global tracer provider. See
213
+ * {@link AgentLoopConfig.telemetry} for the full surface.
214
+ */
215
+ telemetry?: AgentLoopConfig["telemetry"];
216
+ /**
217
+ * Immutable context mode — stabilizes system prompt + tool spec bytes
218
+ * across turns so DeepSeek/Anthropic prefix caches hit at maximum rate.
219
+ */
220
+ appendOnlyContext?: AppendOnlyContextManager;
221
+ }
222
+ export interface AgentPromptOptions {
223
+ toolChoice?: ToolChoice;
224
+ }
225
+ export declare class Agent {
226
+ #private;
227
+ streamFn: StreamFn;
228
+ getApiKey?: (model: Model) => Promise<ApiKey | undefined> | ApiKey | undefined;
229
+ /**
230
+ * Hook invoked after tool arguments are validated and before execution.
231
+ * Reassign at any time to swap the implementation (e.g. on extension reload).
232
+ */
233
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
234
+ /**
235
+ * Hook invoked after tool execution and before `tool_execution_end` / tool-result
236
+ * message emission. Reassign at any time to swap the implementation.
237
+ */
238
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
239
+ /**
240
+ * Hook invoked once an assistant message is finalized, before context append,
241
+ * UI emission, and tool dispatch. Reassign at any time to swap the implementation.
242
+ */
243
+ transformAssistantMessage?: AgentLoopConfig["transformAssistantMessage"];
244
+ /**
245
+ * Hook that peeks whether interrupting IRC asides are queued for the next boundary.
246
+ */
247
+ hasIrcInterrupts?: AgentLoopConfig["hasIrcInterrupts"];
248
+ constructor(opts?: AgentOptions);
249
+ /**
250
+ * Get the current session ID used for provider caching.
251
+ */
252
+ get sessionId(): string | undefined;
253
+ /**
254
+ * Set the session ID for provider caching.
255
+ * Call this when switching sessions (new session, branch, resume).
256
+ */
257
+ set sessionId(value: string | undefined);
258
+ /**
259
+ * Get the prompt cache key forwarded to providers.
260
+ */
261
+ get promptCacheKey(): string | undefined;
262
+ /**
263
+ * Set the prompt cache key forwarded to providers.
264
+ */
265
+ set promptCacheKey(value: string | undefined);
266
+ /**
267
+ * Static metadata forwarded to every API request when no resolver is installed
268
+ * (e.g. `metadata.user_id` for Anthropic session attribution). Setting this
269
+ * clears any installed resolver.
270
+ *
271
+ * For live/provider-aware metadata (e.g. Anthropic OAuth `account_uuid` that
272
+ * must reflect the credential selected per-request), use
273
+ * {@link setMetadataResolver} and read via {@link metadataForProvider}.
274
+ */
275
+ get metadata(): Record<string, unknown> | undefined;
276
+ set metadata(value: Record<string, unknown> | undefined);
277
+ /**
278
+ * Resolve request metadata for the given provider at call time. When a
279
+ * resolver is installed via {@link setMetadataResolver}, it is invoked with
280
+ * the provider string so the result can be scoped (e.g. `account_uuid` is
281
+ * only included for `"anthropic"` requests). Falls back to the static
282
+ * {@link metadata} value when no resolver is set.
283
+ */
284
+ metadataForProvider(provider: string): Record<string, unknown> | undefined;
285
+ /**
286
+ * Install a function that resolves request metadata at call time. The
287
+ * resolver receives the target provider string and can gate provider-specific
288
+ * fields (e.g. `account_uuid` only for `"anthropic"`). Invoked per LLM
289
+ * request by `agent-loop` after `getApiKey` selects the session-sticky
290
+ * credential. Pass `undefined` to clear and revert to the static
291
+ * {@link metadata} value.
292
+ */
293
+ setMetadataResolver(resolver: ((provider: string) => Record<string, unknown> | undefined) | undefined): void;
294
+ /**
295
+ * Read the active OpenTelemetry configuration. Returns `undefined` when
296
+ * instrumentation is disabled. Callers spawning child runs (e.g. subagent
297
+ * dispatch) forward this to the child's loop so its spans appear under the
298
+ * parent's active context with the subagent's own identity stamped.
299
+ */
300
+ get telemetry(): AgentLoopConfig["telemetry"] | undefined;
301
+ /**
302
+ * Replace the active OpenTelemetry configuration. Pass `undefined` to
303
+ * disable instrumentation. Applies to the *next* `agentLoop` invocation —
304
+ * in-flight loops keep the configuration they started with.
305
+ */
306
+ setTelemetry(telemetry: AgentLoopConfig["telemetry"] | undefined): void;
307
+ /**
308
+ * Get provider-scoped mutable session state store.
309
+ */
310
+ get providerSessionState(): Map<string, ProviderSessionState> | undefined;
311
+ /**
312
+ * Set provider-scoped mutable session state store.
313
+ */
314
+ set providerSessionState(value: Map<string, ProviderSessionState> | undefined);
315
+ /**
316
+ * Get the current thinking budgets.
317
+ */
318
+ get thinkingBudgets(): ThinkingBudgets | undefined;
319
+ /**
320
+ * Set custom thinking budgets for token-based providers.
321
+ */
322
+ set thinkingBudgets(value: ThinkingBudgets | undefined);
323
+ /**
324
+ * Get the current sampling temperature.
325
+ */
326
+ get temperature(): number | undefined;
327
+ /**
328
+ * Set sampling temperature for LLM calls. `undefined` uses provider default.
329
+ */
330
+ set temperature(value: number | undefined);
331
+ get topP(): number | undefined;
332
+ set topP(value: number | undefined);
333
+ get topK(): number | undefined;
334
+ set topK(value: number | undefined);
335
+ get minP(): number | undefined;
336
+ set minP(value: number | undefined);
337
+ get presencePenalty(): number | undefined;
338
+ set presencePenalty(value: number | undefined);
339
+ get repetitionPenalty(): number | undefined;
340
+ set repetitionPenalty(value: number | undefined);
341
+ get serviceTier(): ServiceTier | undefined;
342
+ set serviceTier(value: ServiceTier | undefined);
343
+ get serviceTierResolver(): ((model: Model) => ServiceTier | undefined) | undefined;
344
+ set serviceTierResolver(value: ((model: Model) => ServiceTier | undefined) | undefined);
345
+ get hideThinkingSummary(): boolean | undefined;
346
+ set hideThinkingSummary(value: boolean | undefined);
347
+ /**
348
+ * Get the current max retry delay in milliseconds.
349
+ */
350
+ get maxRetryDelayMs(): number | undefined;
351
+ /**
352
+ * Set the maximum delay to wait for server-requested retries.
353
+ * Set to 0 to disable the cap.
354
+ */
355
+ set maxRetryDelayMs(value: number | undefined);
356
+ get state(): AgentState;
357
+ get appendOnlyContext(): AppendOnlyContextManager | undefined;
358
+ setAppendOnlyContext(manager?: AppendOnlyContextManager): void;
359
+ /**
360
+ * Assemble the provider Context for a side-channel (no-loop) request, mirroring
361
+ * the main loop's prefix (system + normalized tools) so it shares the prompt
362
+ * cache. Never touches the append-only log or the tool-choice queue. Owned/
363
+ * in-band dialect sessions stay tools-less (matching their no-native-tools wire
364
+ * shape and avoiding tool-markup leakage). `llmMessages` is already converted
365
+ * (and, in production, obfuscated) by the caller.
366
+ *
367
+ * `systemPrompt` defaults to the live agent prompt so the side request hits the
368
+ * same cached prefix as the main loop. Callers that must pin a different prompt
369
+ * (e.g. handoff generation, which uses the base prompt rather than a per-turn
370
+ * `before_agent_start` hook override) pass it explicitly.
371
+ */
372
+ buildSideRequestContext(llmMessages: Message[], systemPrompt?: string[]): Promise<Context>;
373
+ subscribe(fn: (e: AgentEvent) => void): () => void;
374
+ /** Register an independently removable hook that runs before queued messages are consumed. */
375
+ addBeforeQueuedMessageDequeueHook(hook: (signal?: AbortSignal) => Promise<void> | void): () => void;
376
+ /** Register an independently removable hook that runs immediately before each model call. */
377
+ addBeforeModelCallHook(hook: (signal?: AbortSignal) => Promise<void> | void): () => void;
378
+ setProviderResponseInterceptor(fn: SimpleStreamOptions["onResponse"] | undefined): void;
379
+ setRawSseEventInterceptor(fn: SimpleStreamOptions["onSseEvent"] | undefined): void;
380
+ setAssistantMessageEventInterceptor(fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined): void;
381
+ setOnBeforeYield(fn: (() => Promise<void> | void) | undefined): void;
382
+ setOnTurnEnd(fn: ((messages: AgentMessage[], signal?: AbortSignal, context?: AgentTurnEndContext) => Promise<void> | void) | undefined): void;
383
+ /**
384
+ * Install or replace the host pre-model-call gate; pass `undefined` to
385
+ * remove it. Gates are sampled when a run starts: installing the first
386
+ * gate while a run is in flight takes effect on the next run.
387
+ */
388
+ setBeforeModelCall(fn: AgentBeforeModelCall | undefined): void;
389
+ /**
390
+ * Add a pre-model callback without replacing callbacks owned by the host.
391
+ * Returns a disposer that removes only this callback. Like
392
+ * {@link setBeforeModelCall}, the first gate installed while a run is in
393
+ * flight takes effect on the next run.
394
+ */
395
+ addBeforeModelCall(fn: AgentBeforeModelCall): () => void;
396
+ /**
397
+ * Provide a source of non-interrupting "aside" messages (e.g. background-job
398
+ * completions, late LSP diagnostics) drained at each step boundary. Never
399
+ * aborts in-flight tools. See `AgentLoopConfig.getAsideMessages`.
400
+ */
401
+ setAsideMessageProvider(fn: (() => AsideMessage[] | Promise<AsideMessage[]>) | undefined): void;
402
+ emitExternalEvent(event: AgentEvent): void;
403
+ setSystemPrompt(v: string[] | string): void;
404
+ setModel(m: Model): void;
405
+ setThinkingLevel(l: Effort | undefined): void;
406
+ setDisableReasoning(disabled: boolean): void;
407
+ setSteeringMode(mode: "all" | "one-at-a-time"): void;
408
+ getSteeringMode(): "all" | "one-at-a-time";
409
+ setFollowUpMode(mode: "all" | "one-at-a-time"): void;
410
+ getFollowUpMode(): "all" | "one-at-a-time";
411
+ setInterruptMode(mode: "immediate" | "wait"): void;
412
+ getInterruptMode(): "immediate" | "wait";
413
+ setTools(t: AgentTool<any>[]): void;
414
+ replaceMessages(ms: AgentMessage[]): void;
415
+ replaceQueues(steering: AgentMessage[], followUp: AgentMessage[]): void;
416
+ appendMessage(m: AgentMessage): void;
417
+ popMessage(): AgentMessage | undefined;
418
+ /**
419
+ * Queue a steering message to interrupt the agent mid-run.
420
+ * Delivered after current tool execution, skips remaining tools.
421
+ */
422
+ steer(m: AgentMessage): void;
423
+ /**
424
+ * Queue a follow-up message to be processed after the agent finishes.
425
+ * Delivered only when agent has no more tool calls or steering messages.
426
+ */
427
+ followUp(m: AgentMessage): void;
428
+ clearSteeringQueue(): void;
429
+ clearFollowUpQueue(): void;
430
+ /**
431
+ * Drop tool-directive state retained across a gate-stopped run: the
432
+ * deferred hard choice and the soft-requirement lifecycle.
433
+ */
434
+ clearDeferredToolDirectives(): void;
435
+ clearAllQueues(): void;
436
+ hasQueuedMessages(): boolean;
437
+ /** Non-consuming view of the pending steering queue (insertion order, newest
438
+ * last). The session layer derives its queued-message display/count from
439
+ * this live view instead of a mirror, so the agent-core queue stays the
440
+ * single source of truth. */
441
+ peekSteeringQueue(): readonly AgentMessage[];
442
+ /** Non-consuming view of the pending follow-up queue. See
443
+ * {@link peekSteeringQueue}. */
444
+ peekFollowUpQueue(): readonly AgentMessage[];
445
+ get isAborting(): boolean;
446
+ /**
447
+ * Remove and return the last steering message from the queue (LIFO).
448
+ * Used by dequeue keybinding.
449
+ */
450
+ popLastSteer(): AgentMessage | undefined;
451
+ /**
452
+ * Remove and return the last follow-up message from the queue (LIFO).
453
+ * Used by dequeue keybinding.
454
+ */
455
+ popLastFollowUp(): AgentMessage | undefined;
456
+ clearMessages(): void;
457
+ abort(reason?: unknown): void;
458
+ waitForIdle(): Promise<void>;
459
+ reset(): void;
460
+ /** Send a prompt with an AgentMessage */
461
+ prompt(message: AgentMessage | AgentMessage[], options?: AgentPromptOptions): Promise<void>;
462
+ prompt(input: string, options?: AgentPromptOptions): Promise<void>;
463
+ prompt(input: string, images?: ImageContent[], options?: AgentPromptOptions): Promise<void>;
464
+ continue(signal?: AbortSignal): Promise<void>;
465
+ }
@@ -0,0 +1,131 @@
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 "@linxiraos/pi-ai";
17
+ import type { AgentContext } from "./types.js";
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
+ /** Strip tool descriptions from the provider-bound specs (must match normalizeTools). */
29
+ pruneToolDescriptions?: boolean;
30
+ }
31
+ /**
32
+ * A frozen prefix (system prompt + tools) that produces stable byte
33
+ * sequences across `build()` calls.
34
+ *
35
+ * The first `build()` snapshots the live state. Subsequent calls reuse
36
+ * the cached copy until `invalidate()` is called or the live state's
37
+ * fingerprint changes.
38
+ */
39
+ export declare class StablePrefix {
40
+ #private;
41
+ get fingerprint(): string;
42
+ get version(): number;
43
+ get built(): boolean;
44
+ /**
45
+ * Build or rebuild from live context.
46
+ * Returns `true` if the prefix actually changed (cache miss imminent).
47
+ */
48
+ build(context: AgentContext, options: BuildOptions): boolean;
49
+ /** Force rebuild on the next `build()` call. */
50
+ invalidate(): void;
51
+ /**
52
+ * Returns the cached prefix.
53
+ * @throws if `build()` was never called.
54
+ */
55
+ toContext(): {
56
+ systemPrompt: string[];
57
+ tools: Tool[];
58
+ };
59
+ }
60
+ /**
61
+ * Append-only message log at the `Message[]` (provider-level) layer.
62
+ *
63
+ * The only mutation path is `replaceTail()`, reserved for compaction.
64
+ * Every other operation is append-only.
65
+ */
66
+ export declare class AppendOnlyLog {
67
+ #private;
68
+ get length(): number;
69
+ append(message: any): void;
70
+ extend(messages: any[]): void;
71
+ /** Replace the last entry — only legal for compaction. */
72
+ replaceTail(replacement: any): void;
73
+ /** Returns a shallow copy of all entries. */
74
+ toMessages(): Message[];
75
+ /** Direct readonly access for in-place inspection. */
76
+ entries(): readonly Message[];
77
+ /** Drop entries past index `count`, keeping the first `count` byte-stable.
78
+ * Used by {@link AppendOnlyContextManager.syncMessages} to preserve the
79
+ * already-on-the-wire prefix when a later message diverges. */
80
+ truncate(count: number): void;
81
+ clear(): void;
82
+ }
83
+ /**
84
+ * Manages a stable prefix + append-only log for the agent loop.
85
+ *
86
+ * Call `build(context)` each turn to get a `Context` with stable
87
+ * `systemPrompt` and `tools` and append-only messages. Call
88
+ * `syncMessages(normalizedMessages)` after `convertToLlm` each
89
+ * turn to keep the log in sync.
90
+ *
91
+ * Example:
92
+ * ```
93
+ * const mgr = new AppendOnlyContextManager();
94
+ * const ctx = mgr.build(context); // first call snapshots prefix
95
+ * mgr.syncMessages(normalized); // grow the log
96
+ * ctx = mgr.build(context); // subsequent calls use cache
97
+ * ```
98
+ */
99
+ export declare class AppendOnlyContextManager {
100
+ #private;
101
+ readonly prefix: StablePrefix;
102
+ readonly log: AppendOnlyLog;
103
+ build(context: AgentContext, options: BuildOptions): Context;
104
+ /**
105
+ * Sync normalized (provider-level) messages into the append-only log.
106
+ *
107
+ * Three cases:
108
+ *
109
+ * 1. **Append**: same prefix, new tail → push the new entries.
110
+ * 2. **Compaction**: shorter array → clear the log and replay.
111
+ * 3. **In-place rewrite** (per-turn pruning, transformContext re-render,
112
+ * image strip, etc.): find the longest byte-stable prefix between
113
+ * the previously-synced messages and the new ones, drop the log
114
+ * down to that prefix, then append the diverged tail. Earlier
115
+ * revisions cleared the whole log on any digest change, which on
116
+ * llama.cpp / local backends forced a full ~40k-token re-prefill
117
+ * every turn that an extension, prune pass, or steering re-wrap
118
+ * rewrote a single message (#3406). Preserving the stable prefix
119
+ * lets the provider's KV cache stay warm up to the divergence
120
+ * point — the model only re-prefills from the changed message on.
121
+ */
122
+ syncMessages(normalizedMessages: any[]): void;
123
+ /** Reset prefix + log for a model/provider switch while mode stays active. */
124
+ invalidateForModelChange(): void;
125
+ /** Reset the sync cursor AND clear the log. */
126
+ resetSyncCursor(): void;
127
+ appendMessage(message: any): void;
128
+ replaceTailMessage(message: any): void;
129
+ invalidate(): void;
130
+ reset(context: AgentContext, options: BuildOptions): void;
131
+ }