@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.
Files changed (55) hide show
  1. package/CHANGELOG.md +482 -0
  2. package/README.md +473 -0
  3. package/dist/types/agent-loop.d.ts +55 -0
  4. package/dist/types/agent.d.ts +334 -0
  5. package/dist/types/append-only-context.d.ts +113 -0
  6. package/dist/types/compaction/branch-summarization.d.ts +94 -0
  7. package/dist/types/compaction/compaction.d.ts +166 -0
  8. package/dist/types/compaction/entries.d.ts +103 -0
  9. package/dist/types/compaction/errors.d.ts +26 -0
  10. package/dist/types/compaction/index.d.ts +11 -0
  11. package/dist/types/compaction/messages.d.ts +61 -0
  12. package/dist/types/compaction/openai.d.ts +58 -0
  13. package/dist/types/compaction/pruning.d.ts +18 -0
  14. package/dist/types/compaction/utils.d.ts +32 -0
  15. package/dist/types/compaction.d.ts +1 -0
  16. package/dist/types/harmony-leak.d.ts +99 -0
  17. package/dist/types/index.d.ts +10 -0
  18. package/dist/types/proxy.d.ts +84 -0
  19. package/dist/types/run-collector.d.ts +196 -0
  20. package/dist/types/telemetry.d.ts +588 -0
  21. package/dist/types/thinking.d.ts +17 -0
  22. package/dist/types/types.d.ts +407 -0
  23. package/package.json +75 -0
  24. package/src/agent-loop.ts +1279 -0
  25. package/src/agent.ts +1399 -0
  26. package/src/append-only-context.ts +297 -0
  27. package/src/compaction/branch-summarization.ts +339 -0
  28. package/src/compaction/compaction.ts +1065 -0
  29. package/src/compaction/entries.ts +133 -0
  30. package/src/compaction/errors.ts +31 -0
  31. package/src/compaction/index.ts +12 -0
  32. package/src/compaction/messages.ts +212 -0
  33. package/src/compaction/openai.ts +552 -0
  34. package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
  35. package/src/compaction/prompts/branch-summary-context.md +5 -0
  36. package/src/compaction/prompts/branch-summary-preamble.md +2 -0
  37. package/src/compaction/prompts/branch-summary.md +30 -0
  38. package/src/compaction/prompts/compaction-short-summary.md +9 -0
  39. package/src/compaction/prompts/compaction-summary-context.md +5 -0
  40. package/src/compaction/prompts/compaction-summary.md +38 -0
  41. package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
  42. package/src/compaction/prompts/compaction-update-summary.md +45 -0
  43. package/src/compaction/prompts/file-operations.md +10 -0
  44. package/src/compaction/prompts/handoff-document.md +49 -0
  45. package/src/compaction/prompts/summarization-system.md +3 -0
  46. package/src/compaction/pruning.ts +92 -0
  47. package/src/compaction/utils.ts +185 -0
  48. package/src/compaction.ts +1 -0
  49. package/src/harmony-leak.ts +427 -0
  50. package/src/index.ts +19 -0
  51. package/src/proxy.ts +326 -0
  52. package/src/run-collector.ts +631 -0
  53. package/src/telemetry.ts +2018 -0
  54. package/src/thinking.ts +19 -0
  55. package/src/types.ts +467 -0
@@ -0,0 +1,19 @@
1
+ import { Effort } from "@gajae-code/ai";
2
+
3
+ /**
4
+ * Agent-local thinking selector.
5
+ *
6
+ * `off` disables reasoning, while `inherit` defers to a higher-level selector.
7
+ */
8
+ export const ThinkingLevel = {
9
+ Inherit: "inherit",
10
+ Off: "off",
11
+ Minimal: Effort.Minimal,
12
+ Low: Effort.Low,
13
+ Medium: Effort.Medium,
14
+ High: Effort.High,
15
+ XHigh: Effort.XHigh,
16
+ } as const;
17
+
18
+ export type ThinkingLevel = (typeof ThinkingLevel)[keyof typeof ThinkingLevel];
19
+ export type ResolvedThinkingLevel = Exclude<ThinkingLevel, "inherit">;
package/src/types.ts ADDED
@@ -0,0 +1,467 @@
1
+ import type {
2
+ AssistantMessage,
3
+ AssistantMessageEvent,
4
+ AssistantMessageEventStream,
5
+ Effort,
6
+ ImageContent,
7
+ Message,
8
+ Model,
9
+ SimpleStreamOptions,
10
+ Static,
11
+ streamSimple,
12
+ TextContent,
13
+ Tool,
14
+ ToolChoice,
15
+ ToolResultMessage,
16
+ TSchema,
17
+ } from "@gajae-code/ai";
18
+ import type { AppendOnlyContextManager } from "./append-only-context";
19
+ import type { HarmonyAuditEvent } from "./harmony-leak";
20
+ import type { AgentRunCoverage, AgentRunSummary } from "./run-collector";
21
+ import type { AgentTelemetryConfig } from "./telemetry";
22
+
23
+ /** Stream function - can return sync or Promise for async config lookup */
24
+ export type StreamFn = (
25
+ ...args: Parameters<typeof streamSimple>
26
+ ) => AssistantMessageEventStream | Promise<AssistantMessageEventStream>;
27
+
28
+ /**
29
+ * Configuration for the agent loop.
30
+ */
31
+ export interface AgentLoopConfig extends SimpleStreamOptions {
32
+ model: Model;
33
+
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
+ /**
42
+ * Optional session identifier forwarded to LLM providers.
43
+ * Used by providers that support session-based caching (e.g., OpenAI code provider).
44
+ */
45
+ sessionId?: string;
46
+
47
+ /**
48
+ * Optional resolver called per LLM request to produce request metadata.
49
+ * When set, the agent loop evaluates it **after** `getApiKey` resolves the
50
+ * session-sticky credential, ensuring the metadata's `account_uuid` reflects
51
+ * the credential actually used for the request (not the credential that was
52
+ * current when `AgentLoopConfig` was first constructed). Overrides the static
53
+ * `metadata` field when present.
54
+ */
55
+ metadataResolver?: (provider: string) => Record<string, unknown> | undefined;
56
+
57
+ /**
58
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
59
+ *
60
+ * Each AgentMessage must be converted to a UserMessage, AssistantMessage, or ToolResultMessage
61
+ * that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,
62
+ * status messages) should be filtered out.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * convertToLlm: (messages) => messages.flatMap(m => {
67
+ * if (m.role === "custom") {
68
+ * // Convert custom message to user message
69
+ * return [{ role: "user", content: m.content, timestamp: m.timestamp }];
70
+ * }
71
+ * if (m.role === "notification") {
72
+ * // Filter out UI-only messages
73
+ * return [];
74
+ * }
75
+ * // Pass through standard LLM messages
76
+ * return [m];
77
+ * })
78
+ * ```
79
+ */
80
+ convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
81
+
82
+ /**
83
+ * Optional transform applied to the context before `convertToLlm`.
84
+ *
85
+ * Use this for operations that work at the AgentMessage level:
86
+ * - Context window management (pruning old messages)
87
+ * - Injecting context from external sources
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * transformContext: async (messages) => {
92
+ * if (estimateTokens(messages) > MAX_TOKENS) {
93
+ * return pruneOldMessages(messages);
94
+ * }
95
+ * return messages;
96
+ * }
97
+ * ```
98
+ */
99
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
100
+
101
+ /**
102
+ * Resolves an API key dynamically for each LLM call.
103
+ *
104
+ * Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire
105
+ * during long-running tool execution phases.
106
+ */
107
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
108
+
109
+ /**
110
+ * Returns steering messages to inject into the conversation mid-run.
111
+ *
112
+ * Called after each tool execution to check for user interruptions unless interruptMode is "wait".
113
+ * If messages are returned, remaining tool calls are skipped and
114
+ * these messages are added to the context before the next LLM call.
115
+ */
116
+ getSteeringMessages?: () => Promise<AgentMessage[]>;
117
+
118
+ /**
119
+ * Returns follow-up messages to process after the agent would otherwise stop.
120
+ *
121
+ * Called when the agent has no more tool calls and no steering messages.
122
+ * If messages are returned, they're added to the context and the agent
123
+ * continues with another turn.
124
+ */
125
+ getFollowUpMessages?: () => Promise<AgentMessage[]>;
126
+ /**
127
+ * Hook fired right before the loop would exit.
128
+ *
129
+ * Called when the agent has no more tool calls and no steering messages,
130
+ * immediately before polling follow-up messages.
131
+ */
132
+ onBeforeYield?: () => Promise<void> | void;
133
+
134
+ /**
135
+ * Provides tool execution context, resolved per tool call.
136
+ * Use for late-bound UI or session state access.
137
+ */
138
+ getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
139
+
140
+ /**
141
+ * Refreshes prompt/tool context from live session state before each model call.
142
+ * Use this when tool availability or the system prompt can change mid-turn.
143
+ */
144
+ syncContextBeforeModelCall?: (context: AgentContext) => void | Promise<void>;
145
+
146
+ /**
147
+ * Optional transform applied to tool call arguments before execution.
148
+ * Use for deobfuscating secrets or rewriting arguments.
149
+ */
150
+ transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
151
+
152
+ /**
153
+ * Enable intent tracing for tool calls.
154
+ * When enabled, the harness injects a `string` field into tool schemas sent to the model,
155
+ * then strips from arguments before executing tools.
156
+ */
157
+ intentTracing?: boolean;
158
+ /**
159
+ * Append-only context mode — stabilizes system prompt + tool spec bytes
160
+ * across turns so provider prefix caches hit at maximum rate.
161
+ *
162
+ * When set, the loop reads messages from the append-only log (stable
163
+ * byte prefix) and caches system prompt + tools. Tools exclude per-turn
164
+ * `_i` intent fields.
165
+ */
166
+ appendOnlyContext?: AppendOnlyContextManager;
167
+
168
+ /**
169
+ * Inspect assistant streaming events before they are published to the outer agent event stream.
170
+ * Callers may abort synchronously to stop consuming buffered provider events.
171
+ */
172
+ onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
173
+
174
+ /**
175
+ * Called when GPT-5 Harmony protocol leakage is detected and mitigated.
176
+ */
177
+ onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
178
+
179
+ /**
180
+ * Dynamic tool choice override, resolved per LLM call.
181
+ * When set and returns a value, overrides the static `toolChoice`.
182
+ */
183
+ getToolChoice?: () => ToolChoice | undefined;
184
+
185
+ /**
186
+ * Dynamic reasoning effort override, resolved per LLM call.
187
+ * When set and returns a value, overrides the static `reasoning` captured
188
+ * at run-loop start. Use this so mid-run thinking-level changes apply on
189
+ * the next model call instead of waiting for the next prompt.
190
+ */
191
+ getReasoning?: () => Effort | undefined;
192
+
193
+ /**
194
+ * Called after a tool call has been validated and is about to execute.
195
+ *
196
+ * Return `{ block: true }` to prevent execution. The loop emits an error tool
197
+ * result instead (using `reason` as the error text, or a default if omitted).
198
+ *
199
+ * Mutating `context.args` in place changes the arguments passed to `tool.execute`
200
+ * — the loop does **not** re-validate after this hook runs.
201
+ *
202
+ * The hook receives the tool abort signal (`signal`) and is responsible for
203
+ * honoring it. Throwing surfaces as a tool-error result and does not abort the
204
+ * rest of the batch.
205
+ */
206
+ beforeToolCall?: (
207
+ context: BeforeToolCallContext,
208
+ signal?: AbortSignal,
209
+ ) => Promise<BeforeToolCallResult | undefined> | BeforeToolCallResult | undefined;
210
+
211
+ /**
212
+ * Called after a tool finishes executing, before `tool_execution_end` and the
213
+ * tool-result message are emitted.
214
+ *
215
+ * Return an `AfterToolCallResult` to override individual fields of the executed
216
+ * tool result. Omitted fields keep their original values; there is no deep merge.
217
+ *
218
+ * Throwing surfaces as a tool-error result and does not abort the rest of the batch.
219
+ */
220
+ afterToolCall?: (
221
+ context: AfterToolCallContext,
222
+ signal?: AbortSignal,
223
+ ) => Promise<AfterToolCallResult | undefined> | AfterToolCallResult | undefined;
224
+ /**
225
+ * Opt-in OpenTelemetry instrumentation. Passing `{}` enables the loop's
226
+ * GenAI-semantic-convention spans (`invoke_agent`, `chat`, `execute_tool`)
227
+ * using the global tracer provider. Leaving this field undefined disables
228
+ * the instrumentation entirely — the loop performs zero tracer lookups.
229
+ *
230
+ * See {@link AgentTelemetryConfig} for the full surface (hooks, content
231
+ * capture, cost estimator, agent identity).
232
+ */
233
+ telemetry?: AgentTelemetryConfig;
234
+ }
235
+
236
+ /**
237
+ * Batch/sequencing metadata for the tool call currently being processed.
238
+ */
239
+ export interface ToolCallContext {
240
+ batchId: string;
241
+ index: number;
242
+ total: number;
243
+ toolCalls: Array<{ id: string; name: string }>;
244
+ }
245
+
246
+ /** A single tool-call content block emitted by an assistant message. */
247
+ export type AgentToolCall = Extract<AssistantMessage["content"][number], { type: "toolCall" }>;
248
+
249
+ /**
250
+ * Result returned from `beforeToolCall`.
251
+ *
252
+ * Set `block: true` to prevent the tool from executing. The loop emits an error tool
253
+ * result instead, using `reason` as the error text (or a default if omitted).
254
+ *
255
+ * Mutating the `args` reference passed in `BeforeToolCallContext` is supported and
256
+ * survives into execution — the loop does **not** re-validate after this hook runs.
257
+ */
258
+ export interface BeforeToolCallResult {
259
+ block?: boolean;
260
+ reason?: string;
261
+ }
262
+
263
+ /**
264
+ * Partial override returned from `afterToolCall`.
265
+ *
266
+ * Merge semantics are field-by-field; omitted fields keep the executed values.
267
+ * No deep merge is performed.
268
+ */
269
+ export interface AfterToolCallResult {
270
+ /** If provided, replaces the tool result content array in full. */
271
+ content?: (TextContent | ImageContent)[];
272
+ /** If provided, replaces the tool result details payload in full. */
273
+ details?: unknown;
274
+ /** If provided, replaces the error flag carried with the tool result. */
275
+ isError?: boolean;
276
+ }
277
+
278
+ /** Context passed to `beforeToolCall`. */
279
+ export interface BeforeToolCallContext {
280
+ /** The assistant message that requested the tool call. */
281
+ assistantMessage: AssistantMessage;
282
+ /** The raw tool call block from `assistantMessage.content`. */
283
+ toolCall: AgentToolCall;
284
+ /**
285
+ * Validated tool arguments. The same reference is forwarded to `tool.execute`
286
+ * (after any `transformToolCallArguments` pass), so in-place mutations stick.
287
+ */
288
+ args: Record<string, unknown>;
289
+ /** Current agent context at the time the tool call is prepared. */
290
+ context: AgentContext;
291
+ }
292
+
293
+ /** Context passed to `afterToolCall`. */
294
+ export interface AfterToolCallContext {
295
+ /** The assistant message that requested the tool call. */
296
+ assistantMessage: AssistantMessage;
297
+ /** The raw tool call block from `assistantMessage.content`. */
298
+ toolCall: AgentToolCall;
299
+ /** Validated tool arguments used for execution (post `beforeToolCall` mutations). */
300
+ args: Record<string, unknown>;
301
+ /** The executed tool result before any `afterToolCall` overrides are applied. */
302
+ result: AgentToolResult<any>;
303
+ /** Whether the executed tool result is currently treated as an error. */
304
+ isError: boolean;
305
+ /** Current agent context at the time the tool call is finalized. */
306
+ context: AgentContext;
307
+ }
308
+
309
+ /**
310
+ * Extensible interface for custom app messages.
311
+ * Apps can extend via declaration merging:
312
+ *
313
+ * @example
314
+ * ```typescript
315
+ * declare module "@gajae-code/agent" {
316
+ * interface CustomAgentMessages {
317
+ * artifact: ArtifactMessage;
318
+ * notification: NotificationMessage;
319
+ * }
320
+ * }
321
+ * ```
322
+ */
323
+ export interface CustomAgentMessages {
324
+ // Empty by default - apps extend via declaration merging
325
+ }
326
+
327
+ /**
328
+ * AgentMessage: Union of LLM messages + custom messages.
329
+ * This abstraction allows apps to add custom message types while maintaining
330
+ * type safety and compatibility with the base LLM messages.
331
+ */
332
+ export type AgentMessage = Message | CustomAgentMessages[keyof CustomAgentMessages];
333
+
334
+ /**
335
+ * Agent state containing all configuration and conversation data.
336
+ */
337
+ export interface AgentState {
338
+ systemPrompt: string[];
339
+ model: Model;
340
+ thinkingLevel?: Effort;
341
+ tools: AgentTool<any>[];
342
+ messages: AgentMessage[]; // Can include attachments + custom message types
343
+ isStreaming: boolean;
344
+ streamMessage: AgentMessage | null;
345
+ pendingToolCalls: Set<string>;
346
+ error?: string;
347
+ }
348
+
349
+ export interface AgentToolResult<T = any, _TInput = unknown> {
350
+ // Content blocks supporting text and images
351
+ content: (TextContent | ImageContent)[];
352
+ // Details to be displayed in a UI or logged
353
+ details?: T;
354
+ // Marks a non-throwing failure (e.g. an aggregator catching per-entry errors).
355
+ // agent-loop honors this and surfaces it as a tool error on the wire.
356
+ isError?: boolean;
357
+ }
358
+
359
+ // Callback for streaming tool execution updates
360
+ export type AgentToolUpdateCallback<T = any, TInput = unknown> = (partialResult: AgentToolResult<T, TInput>) => void;
361
+
362
+ /** Options passed to renderResult */
363
+ export interface RenderResultOptions {
364
+ /** Whether the result view is expanded */
365
+ expanded: boolean;
366
+ /** Whether this is a partial/streaming result */
367
+ isPartial: boolean;
368
+ /** Current spinner frame index for animated elements (optional) */
369
+ spinnerFrame?: number;
370
+ }
371
+
372
+ /**
373
+ * Context passed to tool execution.
374
+ * Apps can extend via declaration merging.
375
+ */
376
+ export interface AgentToolContext {
377
+ // Empty by default - apps extend via declaration merging
378
+ }
379
+
380
+ export type AgentToolExecFn<TParameters extends TSchema = TSchema, TDetails = any, TTheme = unknown> = (
381
+ this: AgentTool<TParameters, TDetails, TTheme>,
382
+ toolCallId: string,
383
+ params: Static<TParameters>,
384
+ signal?: AbortSignal,
385
+ onUpdate?: AgentToolUpdateCallback<TDetails, TParameters>,
386
+ context?: AgentToolContext,
387
+ ) => Promise<AgentToolResult<TDetails, TParameters>>;
388
+
389
+ // AgentTool extends Tool but adds the execute function
390
+ export interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any, TTheme = unknown>
391
+ extends Tool<TParameters> {
392
+ // A human-readable label for the tool to be displayed in UI
393
+ label: string;
394
+ /** If true, tool is excluded unless explicitly listed in --tools or agent's tools field */
395
+ hidden?: boolean;
396
+ /** If true, tool can stage a pending action that requires explicit resolution via the resolve tool. */
397
+ deferrable?: boolean;
398
+ /** Built-in tool loading behavior. "essential" loads initially; "discoverable" can be activated by tool search. */
399
+ loadMode?: "essential" | "discoverable";
400
+ /** Short one-line summary used for tool discovery indexes. */
401
+ summary?: string;
402
+ /** If true, tool execution ignores abort signals (runs to completion) */
403
+ nonAbortable?: boolean;
404
+ /**
405
+ * Concurrency mode for tool scheduling when multiple calls are in one turn.
406
+ * - "shared": can run alongside other shared tools (default)
407
+ * - "exclusive": runs alone; other tools wait until it finishes
408
+ */
409
+ concurrency?: "shared" | "exclusive";
410
+ /** If true, argument validation errors are non-fatal: raw args are passed to execute() instead of returning an error to the LLM. */
411
+ lenientArgValidation?: boolean;
412
+ /**
413
+ * Controls how the INTENT_FIELD (`_i`) is handled for this tool.
414
+ * - `"require"` (default): `_i` is injected and required in the parameter schema.
415
+ * - `"optional"`: `_i` is injected as an optional/nullable field.
416
+ * - `"omit"`: `_i` is NOT injected. Use for tools where intent is obvious (yield, resolve, todo_write, …).
417
+ * - function: `_i` is NOT injected; intent is derived dynamically from (potentially partial / streaming) args.
418
+ */
419
+ intent?: "omit" | "optional" | "require" | ((args: Partial<Static<TParameters>>) => string | undefined);
420
+
421
+ /** The main execution callback for this tool. */
422
+ execute: AgentToolExecFn<TParameters, TDetails, TTheme>;
423
+
424
+ /** Optional custom rendering for tool call display (returns UI component) */
425
+ renderCall?: (args: Static<TParameters>, options: RenderResultOptions, theme: TTheme) => unknown;
426
+
427
+ /** Optional custom rendering for tool result display (returns UI component) */
428
+ renderResult?: (
429
+ result: AgentToolResult<TDetails, TParameters>,
430
+ options: RenderResultOptions,
431
+ theme: TTheme,
432
+ ) => unknown;
433
+ }
434
+
435
+ // AgentContext is like Context but uses AgentTool
436
+ export interface AgentContext {
437
+ systemPrompt: string[];
438
+ messages: AgentMessage[];
439
+ tools?: AgentTool<any>[];
440
+ }
441
+
442
+ /**
443
+ * Events emitted by the Agent for UI updates.
444
+ * These events provide fine-grained lifecycle information for messages, turns, and tool executions.
445
+ */
446
+ export type AgentEvent =
447
+ // Agent lifecycle
448
+ | { type: "agent_start" }
449
+ | {
450
+ type: "agent_end";
451
+ messages: AgentMessage[];
452
+ /** Present iff `AgentTelemetryConfig` was supplied on this run. */
453
+ telemetry?: AgentRunSummary;
454
+ coverage?: AgentRunCoverage;
455
+ }
456
+ // Turn lifecycle - a turn is one assistant response + any tool calls/results
457
+ | { type: "turn_start" }
458
+ | { type: "turn_end"; message: AgentMessage; toolResults: ToolResultMessage[] }
459
+ // Message lifecycle - emitted for user, assistant, and toolResult messages
460
+ | { type: "message_start"; message: AgentMessage }
461
+ // Only emitted for assistant messages during streaming
462
+ | { type: "message_update"; message: AgentMessage; assistantMessageEvent: AssistantMessageEvent }
463
+ | { type: "message_end"; message: AgentMessage }
464
+ // Tool execution lifecycle
465
+ | { type: "tool_execution_start"; toolCallId: string; toolName: string; args: any; intent?: string }
466
+ | { type: "tool_execution_update"; toolCallId: string; toolName: string; args: any; partialResult: any }
467
+ | { type: "tool_execution_end"; toolCallId: string; toolName: string; result: any; isError?: boolean };