@linxiraos/pi-agent-core 1.0.0 → 1.0.2
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 +34 -17
- package/package.json +76 -77
- package/src/agent-loop.ts +13 -4
- package/src/compaction/prompts/branch-summary-context.md +1 -1
- package/src/compaction/prompts/branch-summary-preamble.md +2 -2
- package/src/compaction/prompts/compaction-short-summary.md +3 -9
- package/src/compaction/prompts/compaction-summary-context.md +2 -1
- package/src/compaction/prompts/compaction-turn-prefix.md +4 -4
- package/src/compaction/prompts/compaction-update-summary.md +15 -16
- package/src/compaction/prompts/context-window-truncated-output.md +1 -1
- package/src/compaction/prompts/summarization-system.md +2 -2
- package/src/compaction/shake.ts +9 -3
- package/src/types.ts +11 -1
- package/dist/types/agent-loop.d.ts +0 -137
- package/dist/types/agent.d.ts +0 -465
- package/dist/types/append-only-context.d.ts +0 -131
- package/dist/types/compaction/branch-summarization.d.ts +0 -101
- package/dist/types/compaction/compaction-v2-streaming.d.ts +0 -85
- package/dist/types/compaction/compaction.d.ts +0 -307
- package/dist/types/compaction/entries.d.ts +0 -111
- package/dist/types/compaction/errors.d.ts +0 -38
- package/dist/types/compaction/index.d.ts +0 -13
- package/dist/types/compaction/message-cache.d.ts +0 -23
- package/dist/types/compaction/messages.d.ts +0 -79
- package/dist/types/compaction/openai.d.ts +0 -117
- package/dist/types/compaction/pruning.d.ts +0 -105
- package/dist/types/compaction/shake.d.ts +0 -97
- package/dist/types/compaction/tool-protection.d.ts +0 -19
- package/dist/types/compaction/utils.d.ts +0 -61
- package/dist/types/compaction.d.ts +0 -1
- package/dist/types/index.d.ts +0 -13
- package/dist/types/pause.d.ts +0 -46
- package/dist/types/proxy.d.ts +0 -91
- package/dist/types/replay-policy.d.ts +0 -5
- package/dist/types/run-collector.d.ts +0 -196
- package/dist/types/telemetry.d.ts +0 -608
- package/dist/types/thinking.d.ts +0 -18
- package/dist/types/tokenizer.d.ts +0 -2
- package/dist/types/types.d.ts +0 -781
- package/dist/types/utils/yield.d.ts +0 -71
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./compaction/index.js";
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export * from "./agent.js";
|
|
2
|
-
export * from "./agent-loop.js";
|
|
3
|
-
export * from "./append-only-context.js";
|
|
4
|
-
export * from "./compaction.js";
|
|
5
|
-
export * from "./pause.js";
|
|
6
|
-
export * from "./proxy.js";
|
|
7
|
-
export * from "./replay-policy.js";
|
|
8
|
-
export * from "./run-collector.js";
|
|
9
|
-
export * from "./telemetry.js";
|
|
10
|
-
export * from "./thinking.js";
|
|
11
|
-
export * from "./tokenizer.js";
|
|
12
|
-
export * from "./types.js";
|
|
13
|
-
export * from "./utils/yield.js";
|
package/dist/types/pause.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Process-global pause gate for agent loops.
|
|
3
|
-
*
|
|
4
|
-
* Every agent in a process — main session, in-process subagents, advisor —
|
|
5
|
-
* funnels through {@link ../agent-loop!agentLoop}, which polls this gate at its
|
|
6
|
-
* two action boundaries: before each model call and before each tool call
|
|
7
|
-
* starts. Engaging the gate therefore freezes all of them at the next safe
|
|
8
|
-
* point without aborting anything: in-flight provider streams and
|
|
9
|
-
* already-started tool executions run to completion, then every loop parks
|
|
10
|
-
* until {@link AgentPauseGate.resume}. Queued steering/follow-up messages stay
|
|
11
|
-
* queued and deliver normally after resume.
|
|
12
|
-
*
|
|
13
|
-
* A run's own `AbortSignal` still unwinds a parked loop immediately: the park
|
|
14
|
-
* releases on abort (without releasing the gate), so cancelling one run never
|
|
15
|
-
* requires resuming the whole process.
|
|
16
|
-
*
|
|
17
|
-
* Hosts drive the singleton {@link agentPauseGate} (e.g. the TUI `/pause`
|
|
18
|
-
* command); library code only ever reads it.
|
|
19
|
-
*/
|
|
20
|
-
/** Listener invoked with the new state on every pause/resume transition. */
|
|
21
|
-
export type AgentPauseListener = (paused: boolean) => void;
|
|
22
|
-
/** Freeze switch shared by every agent loop in the process. See module docs. */
|
|
23
|
-
export declare class AgentPauseGate {
|
|
24
|
-
#private;
|
|
25
|
-
/** True while the gate is engaged. */
|
|
26
|
-
get paused(): boolean;
|
|
27
|
-
/** Epoch ms when the current pause began; undefined when running. */
|
|
28
|
-
get pausedAt(): number | undefined;
|
|
29
|
-
/** Engage the gate. Returns false (and does nothing) when already paused. */
|
|
30
|
-
pause(): boolean;
|
|
31
|
-
/**
|
|
32
|
-
* Release the gate, waking every parked loop. Returns the pause duration in
|
|
33
|
-
* ms, or undefined when the gate was not engaged.
|
|
34
|
-
*/
|
|
35
|
-
resume(): number | undefined;
|
|
36
|
-
/** Subscribe to pause/resume transitions. Returns an unsubscribe function. */
|
|
37
|
-
onChange(listener: AgentPauseListener): () => void;
|
|
38
|
-
/**
|
|
39
|
-
* Park until the gate is released. Resolves immediately when not paused.
|
|
40
|
-
* An abort on `signal` releases only this wait — the gate stays engaged —
|
|
41
|
-
* so a cancelled run unwinds while the rest of the process stays frozen.
|
|
42
|
-
*/
|
|
43
|
-
waitUntilResumed(signal?: AbortSignal): Promise<void>;
|
|
44
|
-
}
|
|
45
|
-
/** The process-wide gate polled by the agent loop. */
|
|
46
|
-
export declare const agentPauseGate: AgentPauseGate;
|
package/dist/types/proxy.d.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
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 FetchImpl, type ImageContent, type Model, type SimpleStreamOptions, type StopReason } from "@linxiraos/pi-ai";
|
|
6
|
-
export 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: "image_end";
|
|
38
|
-
contentIndex: number;
|
|
39
|
-
content: ImageContent;
|
|
40
|
-
} | {
|
|
41
|
-
type: "toolcall_start";
|
|
42
|
-
contentIndex: number;
|
|
43
|
-
id: string;
|
|
44
|
-
toolName: string;
|
|
45
|
-
} | {
|
|
46
|
-
type: "toolcall_delta";
|
|
47
|
-
contentIndex: number;
|
|
48
|
-
delta: string;
|
|
49
|
-
} | {
|
|
50
|
-
type: "toolcall_end";
|
|
51
|
-
contentIndex: number;
|
|
52
|
-
} | {
|
|
53
|
-
type: "done";
|
|
54
|
-
reason: Extract<StopReason, "stop" | "length" | "toolUse">;
|
|
55
|
-
usage: AssistantMessage["usage"];
|
|
56
|
-
content?: AssistantMessage["content"];
|
|
57
|
-
} | {
|
|
58
|
-
type: "error";
|
|
59
|
-
reason: Extract<StopReason, "aborted" | "error">;
|
|
60
|
-
errorMessage?: string;
|
|
61
|
-
usage: AssistantMessage["usage"];
|
|
62
|
-
content?: AssistantMessage["content"];
|
|
63
|
-
};
|
|
64
|
-
export interface ProxyStreamOptions extends SimpleStreamOptions {
|
|
65
|
-
/** Auth token for the proxy server */
|
|
66
|
-
authToken: string;
|
|
67
|
-
/** Proxy server URL (e.g., "https://genai.example.com") */
|
|
68
|
-
proxyUrl: string;
|
|
69
|
-
/** Optional fetch implementation; defaults to global fetch. */
|
|
70
|
-
fetch?: FetchImpl;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Stream function that proxies through a server instead of calling LLM providers directly.
|
|
74
|
-
* The server strips the partial field from delta events to reduce bandwidth.
|
|
75
|
-
* We reconstruct the partial message client-side.
|
|
76
|
-
*
|
|
77
|
-
* Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* const agent = new Agent({
|
|
82
|
-
* streamFn: (model, context, options) =>
|
|
83
|
-
* streamProxy(model, context, {
|
|
84
|
-
* ...options,
|
|
85
|
-
* authToken: await getAuthToken(),
|
|
86
|
-
* proxyUrl: "https://genai.example.com",
|
|
87
|
-
* }),
|
|
88
|
-
* });
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
export declare function streamProxy(model: Model, context: Context, options: ProxyStreamOptions): ProxyMessageEventStream;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { AssistantMessage, Message } from "@linxiraos/pi-ai";
|
|
2
|
-
/** Detects API-level provider refusals that are terminal errors, not dialogue to replay. */
|
|
3
|
-
export declare function isProviderRefusalMessage(message: AssistantMessage): boolean;
|
|
4
|
-
/** Removes API-level provider refusals from live provider replay while preserving other messages. */
|
|
5
|
-
export declare function filterProviderReplayMessages(messages: readonly Message[]): Message[];
|
|
@@ -1,196 +0,0 @@
|
|
|
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 "@linxiraos/pi-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
|
-
}
|