@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,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proxy stream function for apps that route LLM calls through a server.
|
|
3
|
+
* The server manages auth and proxies requests to LLM providers.
|
|
4
|
+
*/
|
|
5
|
+
import { type AssistantMessage, type AssistantMessageEvent, type Context, EventStream, type Model, type SimpleStreamOptions, type StopReason } from "@gajae-code/ai";
|
|
6
|
+
declare class ProxyMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Proxy event types - server sends these with partial field stripped to reduce bandwidth.
|
|
11
|
+
*/
|
|
12
|
+
export type ProxyAssistantMessageEvent = {
|
|
13
|
+
type: "start";
|
|
14
|
+
} | {
|
|
15
|
+
type: "text_start";
|
|
16
|
+
contentIndex: number;
|
|
17
|
+
} | {
|
|
18
|
+
type: "text_delta";
|
|
19
|
+
contentIndex: number;
|
|
20
|
+
delta: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: "text_end";
|
|
23
|
+
contentIndex: number;
|
|
24
|
+
contentSignature?: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "thinking_start";
|
|
27
|
+
contentIndex: number;
|
|
28
|
+
} | {
|
|
29
|
+
type: "thinking_delta";
|
|
30
|
+
contentIndex: number;
|
|
31
|
+
delta: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: "thinking_end";
|
|
34
|
+
contentIndex: number;
|
|
35
|
+
contentSignature?: string;
|
|
36
|
+
} | {
|
|
37
|
+
type: "toolcall_start";
|
|
38
|
+
contentIndex: number;
|
|
39
|
+
id: string;
|
|
40
|
+
toolName: string;
|
|
41
|
+
} | {
|
|
42
|
+
type: "toolcall_delta";
|
|
43
|
+
contentIndex: number;
|
|
44
|
+
delta: string;
|
|
45
|
+
} | {
|
|
46
|
+
type: "toolcall_end";
|
|
47
|
+
contentIndex: number;
|
|
48
|
+
} | {
|
|
49
|
+
type: "done";
|
|
50
|
+
reason: Extract<StopReason, "stop" | "length" | "toolUse">;
|
|
51
|
+
usage: AssistantMessage["usage"];
|
|
52
|
+
} | {
|
|
53
|
+
type: "error";
|
|
54
|
+
reason: Extract<StopReason, "aborted" | "error">;
|
|
55
|
+
errorMessage?: string;
|
|
56
|
+
usage: AssistantMessage["usage"];
|
|
57
|
+
};
|
|
58
|
+
export interface ProxyStreamOptions extends SimpleStreamOptions {
|
|
59
|
+
/** Auth token for the proxy server */
|
|
60
|
+
authToken: string;
|
|
61
|
+
/** Proxy server URL (e.g., "https://genai.example.com") */
|
|
62
|
+
proxyUrl: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Stream function that proxies through a server instead of calling LLM providers directly.
|
|
66
|
+
* The server strips the partial field from delta events to reduce bandwidth.
|
|
67
|
+
* We reconstruct the partial message client-side.
|
|
68
|
+
*
|
|
69
|
+
* Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const agent = new Agent({
|
|
74
|
+
* streamFn: (model, context, options) =>
|
|
75
|
+
* streamProxy(model, context, {
|
|
76
|
+
* ...options,
|
|
77
|
+
* authToken: await getAuthToken(),
|
|
78
|
+
* proxyUrl: "https://genai.example.com",
|
|
79
|
+
* }),
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function streamProxy(model: Model, context: Context, options: ProxyStreamOptions): ProxyMessageEventStream;
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-invocation run aggregator. Buffers per-chat and per-tool records as the
|
|
3
|
+
* loop executes and folds them into a single {@link AgentRunSummary} +
|
|
4
|
+
* {@link AgentRunCoverage} value at the end.
|
|
5
|
+
*
|
|
6
|
+
* One collector lives on each {@link AgentTelemetry} handle, which is
|
|
7
|
+
* constructed once per `agentLoop` invocation in {@link resolveTelemetry}.
|
|
8
|
+
* Collector lookups use the live `Span` as a `WeakMap` key — bounded memory,
|
|
9
|
+
* no cross-invoke leakage.
|
|
10
|
+
*
|
|
11
|
+
* The collector is fed exclusively by helpers in `./telemetry.ts`. Loop
|
|
12
|
+
* authors do not interact with it directly except via the public
|
|
13
|
+
* `recordSkippedTool` helper used for the two skip paths that bypass spans
|
|
14
|
+
* entirely (pre-run interrupt and the tail-sweep for tool calls that never
|
|
15
|
+
* produced a result message).
|
|
16
|
+
*/
|
|
17
|
+
import type { AssistantMessage, Model, StopReason } from "@gajae-code/ai";
|
|
18
|
+
import type { Span } from "@opentelemetry/api";
|
|
19
|
+
/** Terminal status reported by an `execute_tool` span. */
|
|
20
|
+
export type ToolStatus = "ok" | "error" | "skipped" | "blocked" | "timeout" | "aborted";
|
|
21
|
+
/** Raw record for a single `chat` step, finalized by `finishChatSpan`. */
|
|
22
|
+
export interface ChatRecord {
|
|
23
|
+
readonly stepNumber: number;
|
|
24
|
+
readonly model: string;
|
|
25
|
+
readonly provider: string;
|
|
26
|
+
readonly stopReason: StopReason | undefined;
|
|
27
|
+
readonly latencyMs: number;
|
|
28
|
+
readonly inputTokens: number;
|
|
29
|
+
readonly outputTokens: number;
|
|
30
|
+
readonly cachedInputTokens: number;
|
|
31
|
+
readonly cacheWriteTokens: number;
|
|
32
|
+
readonly reasoningOutputTokens: number;
|
|
33
|
+
readonly totalTokens: number;
|
|
34
|
+
readonly costUsd: number | undefined;
|
|
35
|
+
readonly costUnavailableReason: string | undefined;
|
|
36
|
+
readonly errorType: string | undefined;
|
|
37
|
+
}
|
|
38
|
+
/** Raw record for a single `execute_tool` invocation. */
|
|
39
|
+
export interface ToolRecord {
|
|
40
|
+
readonly toolCallId: string;
|
|
41
|
+
readonly toolName: string;
|
|
42
|
+
readonly status: ToolStatus;
|
|
43
|
+
readonly latencyMs: number;
|
|
44
|
+
readonly errorType: string | undefined;
|
|
45
|
+
}
|
|
46
|
+
/** Per-tool counters surfaced under {@link AgentRunSummary.tools.byName}. */
|
|
47
|
+
export interface ToolCounters {
|
|
48
|
+
readonly total: number;
|
|
49
|
+
readonly ok: number;
|
|
50
|
+
readonly error: number;
|
|
51
|
+
readonly skipped: number;
|
|
52
|
+
readonly blocked: number;
|
|
53
|
+
readonly timeout: number;
|
|
54
|
+
readonly aborted: number;
|
|
55
|
+
readonly totalLatencyMs: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Run-level rollup returned in the `agent_end` event and passed to
|
|
59
|
+
* {@link AgentTelemetryConfig.onRunEnd}. Pure aggregation — no references to
|
|
60
|
+
* spans, no callbacks, no live state. Safe to persist / diff / assert.
|
|
61
|
+
*/
|
|
62
|
+
export interface AgentRunSummary {
|
|
63
|
+
readonly chats: {
|
|
64
|
+
readonly total: number;
|
|
65
|
+
/** Bucketed by raw {@link StopReason}; absent reasons omitted. */
|
|
66
|
+
readonly byStopReason: Readonly<Record<string, number>>;
|
|
67
|
+
readonly totalLatencyMs: number;
|
|
68
|
+
};
|
|
69
|
+
readonly tools: {
|
|
70
|
+
readonly total: number;
|
|
71
|
+
readonly ok: number;
|
|
72
|
+
readonly error: number;
|
|
73
|
+
readonly skipped: number;
|
|
74
|
+
readonly blocked: number;
|
|
75
|
+
readonly timeout: number;
|
|
76
|
+
readonly aborted: number;
|
|
77
|
+
readonly totalLatencyMs: number;
|
|
78
|
+
/** Per-tool-name counters; keys sorted by name on snapshot. */
|
|
79
|
+
readonly byName: Readonly<Record<string, ToolCounters>>;
|
|
80
|
+
};
|
|
81
|
+
readonly usage: {
|
|
82
|
+
readonly inputTokens: number;
|
|
83
|
+
readonly outputTokens: number;
|
|
84
|
+
readonly cachedInputTokens: number;
|
|
85
|
+
readonly cacheWriteTokens: number;
|
|
86
|
+
readonly reasoningOutputTokens: number;
|
|
87
|
+
readonly totalTokens: number;
|
|
88
|
+
};
|
|
89
|
+
readonly cost: {
|
|
90
|
+
readonly estimatedUsd: number;
|
|
91
|
+
/** Sorted, deduped. */
|
|
92
|
+
readonly unavailableReasons: readonly string[];
|
|
93
|
+
};
|
|
94
|
+
readonly errors: {
|
|
95
|
+
readonly total: number;
|
|
96
|
+
readonly byType: Readonly<Record<string, number>>;
|
|
97
|
+
};
|
|
98
|
+
readonly stepCount: number;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Coverage rollup: registered-vs-invoked across the run. All arrays are
|
|
102
|
+
* sorted ascending and deduped so the value is stable for diffing.
|
|
103
|
+
*/
|
|
104
|
+
export interface AgentRunCoverage {
|
|
105
|
+
readonly toolsAvailable: readonly string[];
|
|
106
|
+
readonly toolsInvoked: readonly string[];
|
|
107
|
+
readonly toolsUnused: readonly string[];
|
|
108
|
+
readonly modelsUsed: readonly string[];
|
|
109
|
+
readonly providersUsed: readonly string[];
|
|
110
|
+
}
|
|
111
|
+
export declare class AgentRunCollector {
|
|
112
|
+
#private;
|
|
113
|
+
/** True once `markRunEnded()` has been called for this invocation. */
|
|
114
|
+
get runEnded(): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Mark this run as logically ended. Callers use this to coordinate the
|
|
117
|
+
* `onRunEnd` hook between the success path (fires inside
|
|
118
|
+
* `buildAgentEndEvent`, before `stream.end()`) and the error path (fires
|
|
119
|
+
* inside `finishInvokeAgentSpan`'s finally). Idempotent — returns `true`
|
|
120
|
+
* the first time, `false` on subsequent calls.
|
|
121
|
+
*/
|
|
122
|
+
markRunEnded(): boolean;
|
|
123
|
+
/** Record the tool names exposed on a single chat step. */
|
|
124
|
+
noteAvailableTools(tools: readonly {
|
|
125
|
+
readonly name: string;
|
|
126
|
+
}[] | undefined): void;
|
|
127
|
+
beginChat(span: Span, init: {
|
|
128
|
+
readonly stepNumber: number;
|
|
129
|
+
readonly model: Model;
|
|
130
|
+
readonly provider?: string;
|
|
131
|
+
}): void;
|
|
132
|
+
endChat(span: Span, message: AssistantMessage, fields: {
|
|
133
|
+
readonly costUsd: number | undefined;
|
|
134
|
+
readonly costUnavailableReason: string | undefined;
|
|
135
|
+
}): void;
|
|
136
|
+
/**
|
|
137
|
+
* Stamp the chat span as failed without a finalized AssistantMessage. Used
|
|
138
|
+
* by the `catch` arm of `streamAssistantResponse` so error chats still
|
|
139
|
+
* appear in the run summary.
|
|
140
|
+
*/
|
|
141
|
+
failChat(span: Span, fields: {
|
|
142
|
+
readonly errorType: string;
|
|
143
|
+
}): void;
|
|
144
|
+
beginTool(span: Span, init: {
|
|
145
|
+
readonly toolCallId: string;
|
|
146
|
+
readonly toolName: string;
|
|
147
|
+
}): void;
|
|
148
|
+
endTool(span: Span, fields: {
|
|
149
|
+
readonly status: ToolStatus;
|
|
150
|
+
readonly errorType: string | undefined;
|
|
151
|
+
}): void;
|
|
152
|
+
/**
|
|
153
|
+
* Record a tool that never produced a span — pre-run interrupt or tail
|
|
154
|
+
* sweep. The LLM still asked for it, so it counts toward
|
|
155
|
+
* {@link AgentRunCoverage.toolsInvoked}.
|
|
156
|
+
*/
|
|
157
|
+
recordOrphanTool(record: {
|
|
158
|
+
readonly toolCallId: string;
|
|
159
|
+
readonly toolName: string;
|
|
160
|
+
readonly status: ToolStatus;
|
|
161
|
+
}): void;
|
|
162
|
+
/** Build the immutable summary value from buffered records. */
|
|
163
|
+
snapshot(opts: {
|
|
164
|
+
readonly stepCount: number;
|
|
165
|
+
}): {
|
|
166
|
+
readonly summary: AgentRunSummary;
|
|
167
|
+
readonly coverage: AgentRunCoverage;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Fold multiple per-run summaries into one. Pure aggregation — useful when a
|
|
172
|
+
* caller (verify pass, benchmark harness) drives the agent loop N times and
|
|
173
|
+
* needs a single rollup across all invocations.
|
|
174
|
+
*
|
|
175
|
+
* Counters sum element-wise. Sets (cost reasons, error types, per-tool
|
|
176
|
+
* counters) merge by key. Numeric totals sum. The output is in the same
|
|
177
|
+
* shape as a single `AgentRunSummary`, so all dashboards and persistence
|
|
178
|
+
* layers handle it uniformly.
|
|
179
|
+
*/
|
|
180
|
+
export declare function aggregateAgentRunSummaries(summaries: readonly AgentRunSummary[]): AgentRunSummary;
|
|
181
|
+
/** Union-merge multiple coverage values, preserving the sorted+deduped invariant. */
|
|
182
|
+
export declare function aggregateAgentRunCoverage(coverages: readonly AgentRunCoverage[]): AgentRunCoverage;
|
|
183
|
+
/** Empty `AgentRunSummary` constant. Exported for tests and default-initializers. */
|
|
184
|
+
export declare function emptyAgentRunSummary(): AgentRunSummary;
|
|
185
|
+
/** Empty `AgentRunCoverage` constant. Exported for tests and default-initializers. */
|
|
186
|
+
export declare function emptyAgentRunCoverage(): AgentRunCoverage;
|
|
187
|
+
/**
|
|
188
|
+
* Distinguishable error class thrown when `beforeToolCall` returns
|
|
189
|
+
* `{ block: true }`. Lets the catch arm of `runTool` set the terminal status
|
|
190
|
+
* on the execute_tool span to `"blocked"` instead of conflating with a real
|
|
191
|
+
* tool exception.
|
|
192
|
+
*/
|
|
193
|
+
export declare class ToolCallBlockedError extends Error {
|
|
194
|
+
readonly name = "ToolCallBlockedError";
|
|
195
|
+
constructor(reason?: string);
|
|
196
|
+
}
|