tnega 0.1.0 → 0.3.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.
- package/README.md +115 -189
- package/dist/agent.js +2907 -0
- package/dist/agent.js.map +7 -0
- package/dist/bin.js +32512 -4419
- package/dist/bin.js.map +4 -4
- package/dist/cli-runtime.js +33404 -0
- package/dist/cli-runtime.js.map +7 -0
- package/dist/coding-agent.js +19088 -0
- package/dist/coding-agent.js.map +7 -0
- package/dist/core.js +1398 -0
- package/dist/core.js.map +7 -0
- package/dist/eval.js +23905 -0
- package/dist/eval.js.map +7 -0
- package/dist/events.js +1215 -0
- package/dist/events.js.map +7 -0
- package/dist/evolve.js +18303 -0
- package/dist/evolve.js.map +7 -0
- package/dist/index.js +33087 -4450
- package/dist/index.js.map +4 -4
- package/dist/llm.js +1016 -0
- package/dist/llm.js.map +7 -0
- package/dist/services.js +1142 -0
- package/dist/services.js.map +7 -0
- package/dist/session.js +1045 -0
- package/dist/session.js.map +7 -0
- package/dist/tools.js +2577 -0
- package/dist/tools.js.map +7 -0
- package/dist/types/packages/agent/src/definition.d.ts +17 -0
- package/dist/types/packages/agent/src/inbox-durable.d.ts +54 -0
- package/dist/types/packages/agent/src/index.d.ts +7 -0
- package/dist/types/packages/agent/src/live.d.ts +115 -0
- package/dist/types/packages/agent/src/llm-service.d.ts +32 -0
- package/dist/types/packages/agent/src/prompt.d.ts +55 -0
- package/dist/types/packages/agent/src/service.d.ts +68 -0
- package/dist/types/packages/agent/src/types.d.ts +215 -0
- package/dist/types/packages/benchmark/src/bigcodebench.d.ts +6 -0
- package/dist/types/packages/benchmark/src/humaneval.d.ts +11 -0
- package/dist/types/packages/benchmark/src/index.d.ts +9 -0
- package/dist/types/packages/benchmark/src/manifest.d.ts +10 -0
- package/dist/types/packages/benchmark/src/mbpp.d.ts +10 -0
- package/dist/types/packages/benchmark/src/parquet.d.ts +2 -0
- package/dist/types/packages/benchmark/src/stdlib.d.ts +3 -0
- package/dist/types/packages/benchmark/src/swebench.d.ts +2 -0
- package/dist/types/packages/benchmark/src/types.d.ts +58 -0
- package/dist/types/packages/cli/src/bin.d.ts +2 -0
- package/dist/types/packages/cli/src/commands.d.ts +146 -0
- package/dist/types/packages/cli/src/config.d.ts +28 -0
- package/dist/types/packages/cli/src/index.d.ts +9 -0
- package/dist/types/packages/cli/src/profile-file.d.ts +26 -0
- package/dist/types/packages/cli/src/profile.d.ts +9 -0
- package/dist/types/packages/cli/src/server.d.ts +14 -0
- package/dist/types/packages/cli/src/store.d.ts +60 -0
- package/dist/types/packages/cli/src/yaml.d.ts +1 -0
- package/dist/types/packages/coding-agent/src/codingAgent.d.ts +25 -0
- package/dist/types/packages/coding-agent/src/index.d.ts +6 -0
- package/dist/types/packages/coding-agent/src/mcp.d.ts +24 -0
- package/dist/types/packages/coding-agent/src/plan.d.ts +13 -0
- package/dist/types/packages/coding-agent/src/skills.d.ts +12 -0
- package/dist/types/packages/coding-agent/src/slash.d.ts +28 -0
- package/dist/types/packages/coding-agent/src/types.d.ts +41 -0
- package/dist/types/packages/core/src/context.d.ts +28 -0
- package/dist/types/packages/core/src/events.d.ts +57 -0
- package/dist/types/packages/core/src/fiber.d.ts +79 -0
- package/dist/types/packages/core/src/index.d.ts +10 -0
- package/dist/types/packages/core/src/invariant.d.ts +45 -0
- package/dist/types/packages/core/src/logger.d.ts +48 -0
- package/dist/types/packages/core/src/reflect.d.ts +42 -0
- package/dist/types/packages/core/src/registry.d.ts +54 -0
- package/dist/types/packages/core/src/service.d.ts +19 -0
- package/dist/types/packages/core/src/utils.d.ts +54 -0
- package/dist/types/packages/eval/src/codingRuntime.d.ts +18 -0
- package/dist/types/packages/eval/src/index.d.ts +5 -0
- package/dist/types/packages/eval/src/policy.d.ts +8 -0
- package/dist/types/packages/eval/src/runner.d.ts +31 -0
- package/dist/types/packages/eval/src/service.d.ts +23 -0
- package/dist/types/packages/eval/src/strategies.d.ts +58 -0
- package/dist/types/packages/eval/src/trace.d.ts +7 -0
- package/dist/types/packages/eval/src/types.d.ts +281 -0
- package/dist/types/packages/eval/src/utils.d.ts +5 -0
- package/dist/types/packages/eval/src/workspace.d.ts +17 -0
- package/dist/types/packages/evolve/src/diagnose.d.ts +9 -0
- package/dist/types/packages/evolve/src/index.d.ts +7 -0
- package/dist/types/packages/evolve/src/llm.d.ts +39 -0
- package/dist/types/packages/evolve/src/log.d.ts +26 -0
- package/dist/types/packages/evolve/src/select.d.ts +3 -0
- package/dist/types/packages/evolve/src/service.d.ts +29 -0
- package/dist/types/packages/evolve/src/types.d.ts +176 -0
- package/dist/types/packages/llm/src/anthropic-messages.d.ts +4 -0
- package/dist/types/packages/llm/src/errors.d.ts +6 -0
- package/dist/types/packages/llm/src/index.d.ts +7 -0
- package/dist/types/packages/llm/src/models.d.ts +29 -0
- package/dist/types/packages/llm/src/openai.d.ts +4 -0
- package/dist/types/packages/llm/src/provider.d.ts +3 -0
- package/dist/types/packages/llm/src/shared.d.ts +15 -0
- package/dist/types/packages/llm/src/types.d.ts +13 -0
- package/dist/types/packages/session/src/index.d.ts +500 -0
- package/dist/types/packages/session/src/invariant.d.ts +33 -0
- package/dist/types/packages/tools/src/builtins.d.ts +25 -0
- package/dist/types/packages/tools/src/calc.d.ts +4 -0
- package/dist/types/packages/tools/src/execution.d.ts +31 -0
- package/dist/types/packages/tools/src/index.d.ts +107 -0
- package/dist/types/packages/tools/src/path.d.ts +4 -0
- package/dist/types/packages/tools/src/policy.d.ts +14 -0
- package/dist/types/src/events.d.ts +1 -0
- package/dist/types/src/index.d.ts +15 -0
- package/dist/types/src/services.d.ts +4 -0
- package/dist/web/assets/index-45pokiBH.js +270 -0
- package/dist/web/assets/index-DCLL69Kz.css +1 -0
- package/dist/web/index.html +13 -0
- package/package.json +92 -35
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
import type { Context } from "../../core/src/index.js";
|
|
2
|
+
import { type SessionInvariantFailure } from './invariant.js';
|
|
3
|
+
/**
|
|
4
|
+
* v7 derives model-visible messages from the *folded surface* instead of from
|
|
5
|
+
* raw file order, so compaction can stay strictly append-only.
|
|
6
|
+
*
|
|
7
|
+
* v6 rewrote the log on every real compaction: the kept tail was physically
|
|
8
|
+
* reordered after the checkpoint (and every seq renumbered) purely so that a
|
|
9
|
+
* file-order projection would equal the surface. v7 removes that coupling:
|
|
10
|
+
* `deriveMessages()` walks the surface nodes in surface position order (a
|
|
11
|
+
* replacement node may carry a *higher* seq than the nodes that follow it),
|
|
12
|
+
* so compaction appends `compaction/start` → `checkpoint` (prefix +
|
|
13
|
+
* `surfaceOp.replace` range) → `compaction/end` and never rewrites history.
|
|
14
|
+
* To keep every surface node self-describing, `assistant/message` now carries
|
|
15
|
+
* its own `toolCalls` (the assistant turn is not reassembled from separate
|
|
16
|
+
* `tool/call` events at projection time).
|
|
17
|
+
*
|
|
18
|
+
* v6 logs are rejected (no migration in the pre-release window).
|
|
19
|
+
*/
|
|
20
|
+
export declare const SESSION_FORMAT_VERSION = 7;
|
|
21
|
+
/** Serialized tool schema, structurally compatible with @tnega/tools ToolSchema. */
|
|
22
|
+
export interface ToolSchemaSnapshot {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
parameters?: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
/** Call configuration recorded beside a request so it can be reconstructed. */
|
|
28
|
+
export interface LlmCallConfig {
|
|
29
|
+
provider?: string;
|
|
30
|
+
model?: string;
|
|
31
|
+
maxTokens?: number;
|
|
32
|
+
temperature?: number;
|
|
33
|
+
reasoningEffort?: string;
|
|
34
|
+
}
|
|
35
|
+
export declare class SessionFormatError extends Error {
|
|
36
|
+
name: string;
|
|
37
|
+
}
|
|
38
|
+
export type ModelRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
39
|
+
export interface ModelToolCall {
|
|
40
|
+
id: string;
|
|
41
|
+
name: string;
|
|
42
|
+
arguments: unknown;
|
|
43
|
+
}
|
|
44
|
+
export interface ModelMessage {
|
|
45
|
+
role: ModelRole;
|
|
46
|
+
content: string;
|
|
47
|
+
name?: string;
|
|
48
|
+
tool_call_id?: string;
|
|
49
|
+
tool_calls?: ModelToolCall[];
|
|
50
|
+
toolOk?: boolean;
|
|
51
|
+
toolError?: ToolResultErrorPayload;
|
|
52
|
+
}
|
|
53
|
+
export interface UserMessagePayload {
|
|
54
|
+
content: string;
|
|
55
|
+
name?: string;
|
|
56
|
+
parentId?: string;
|
|
57
|
+
}
|
|
58
|
+
export interface AssistantMessagePayload {
|
|
59
|
+
content: string;
|
|
60
|
+
name?: string;
|
|
61
|
+
parentId?: string;
|
|
62
|
+
interrupted?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* The tool requests this assistant turn made, when it made any. The model
|
|
65
|
+
* transcript is derived node-by-node from the surface, so an assistant turn
|
|
66
|
+
* must carry its calls itself rather than being reassembled from the
|
|
67
|
+
* separate (log-only) `tool/call` events that follow it.
|
|
68
|
+
*/
|
|
69
|
+
toolCalls?: ModelToolCall[];
|
|
70
|
+
}
|
|
71
|
+
export interface AssistantChunkPayload {
|
|
72
|
+
id: string;
|
|
73
|
+
content: string;
|
|
74
|
+
index?: number;
|
|
75
|
+
}
|
|
76
|
+
export interface SystemMessagePayload {
|
|
77
|
+
content: string;
|
|
78
|
+
name?: string;
|
|
79
|
+
parentId?: string;
|
|
80
|
+
}
|
|
81
|
+
export type MessageEventPayload = UserMessagePayload | AssistantMessagePayload | SystemMessagePayload;
|
|
82
|
+
export type MessageEventType = 'user/message' | 'assistant/message' | 'system/message';
|
|
83
|
+
export interface CompactionStartPayload {
|
|
84
|
+
boundary?: number;
|
|
85
|
+
keep?: number;
|
|
86
|
+
tokensBefore?: number;
|
|
87
|
+
}
|
|
88
|
+
export interface CompactionEndPayload {
|
|
89
|
+
checkpointId?: string;
|
|
90
|
+
keep?: number;
|
|
91
|
+
}
|
|
92
|
+
export interface ToolCallPayload {
|
|
93
|
+
id: string;
|
|
94
|
+
name: string;
|
|
95
|
+
arguments: unknown;
|
|
96
|
+
/** Exact raw arguments JSON as produced by the model; preserves serialization. */
|
|
97
|
+
argRaw?: string;
|
|
98
|
+
turn?: number;
|
|
99
|
+
step?: number;
|
|
100
|
+
}
|
|
101
|
+
export interface ToolResultErrorPayload {
|
|
102
|
+
name?: string;
|
|
103
|
+
message: string;
|
|
104
|
+
stack?: string;
|
|
105
|
+
}
|
|
106
|
+
export interface ToolResultPayload {
|
|
107
|
+
id: string;
|
|
108
|
+
toolCallId: string;
|
|
109
|
+
name: string;
|
|
110
|
+
ok: boolean;
|
|
111
|
+
durationMs?: number;
|
|
112
|
+
output?: unknown;
|
|
113
|
+
error?: ToolResultErrorPayload;
|
|
114
|
+
argRaw?: string;
|
|
115
|
+
turn?: number;
|
|
116
|
+
step?: number;
|
|
117
|
+
}
|
|
118
|
+
export type CancelCause = {
|
|
119
|
+
type: 'user';
|
|
120
|
+
} | {
|
|
121
|
+
type: 'parent';
|
|
122
|
+
} | {
|
|
123
|
+
type: 'disposed';
|
|
124
|
+
} | {
|
|
125
|
+
type: 'abort';
|
|
126
|
+
message?: string;
|
|
127
|
+
} | {
|
|
128
|
+
type: 'timeout';
|
|
129
|
+
timeoutMs: number;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Why a turn ended — the durable machine outcome, distinct from the
|
|
133
|
+
* per-step model `finishReason`. Written on `turn/end`.
|
|
134
|
+
*/
|
|
135
|
+
export type TurnEndReason = {
|
|
136
|
+
kind: 'completed';
|
|
137
|
+
} | {
|
|
138
|
+
kind: 'aborted';
|
|
139
|
+
cause: CancelCause;
|
|
140
|
+
} | {
|
|
141
|
+
kind: 'blocked';
|
|
142
|
+
} | {
|
|
143
|
+
kind: 'error';
|
|
144
|
+
error: ToolResultErrorPayload;
|
|
145
|
+
} | {
|
|
146
|
+
kind: 'max-tokens';
|
|
147
|
+
} | {
|
|
148
|
+
kind: 'max-steps';
|
|
149
|
+
} | {
|
|
150
|
+
kind: 'max-turns';
|
|
151
|
+
} | {
|
|
152
|
+
kind: 'interrupted';
|
|
153
|
+
};
|
|
154
|
+
export interface TurnStartPayload {
|
|
155
|
+
turn: number;
|
|
156
|
+
input?: unknown;
|
|
157
|
+
reason?: string;
|
|
158
|
+
}
|
|
159
|
+
export interface TurnEndPayload {
|
|
160
|
+
turn: number;
|
|
161
|
+
finishReason?: string;
|
|
162
|
+
/** The typed durable reason this turn ended. Supersedes `finishReason`. */
|
|
163
|
+
reason?: TurnEndReason;
|
|
164
|
+
output?: string;
|
|
165
|
+
steps?: number;
|
|
166
|
+
interrupted?: boolean;
|
|
167
|
+
cancelCause?: CancelCause;
|
|
168
|
+
error?: ToolResultErrorPayload;
|
|
169
|
+
}
|
|
170
|
+
export interface StepStartPayload {
|
|
171
|
+
turn: number;
|
|
172
|
+
step: number;
|
|
173
|
+
}
|
|
174
|
+
export interface StepEndPayload {
|
|
175
|
+
turn: number;
|
|
176
|
+
step: number;
|
|
177
|
+
finishReason?: string;
|
|
178
|
+
toolCalls?: number;
|
|
179
|
+
interrupted?: boolean;
|
|
180
|
+
cancelCause?: CancelCause;
|
|
181
|
+
error?: ToolResultErrorPayload;
|
|
182
|
+
}
|
|
183
|
+
export interface LLMRetryPayload {
|
|
184
|
+
retryId: string;
|
|
185
|
+
retry: number;
|
|
186
|
+
delayMs?: number;
|
|
187
|
+
failure?: ToolResultErrorPayload;
|
|
188
|
+
}
|
|
189
|
+
export interface LLMRetryStartedPayload {
|
|
190
|
+
retryId: string;
|
|
191
|
+
retry: number;
|
|
192
|
+
}
|
|
193
|
+
/** How a `request/header` snapshot enters the log. */
|
|
194
|
+
export type RequestHeaderReason = 'initial' | 'resume' | 'change' | 'series' | 'change-series';
|
|
195
|
+
export interface RequestHeaderPayload {
|
|
196
|
+
reason: RequestHeaderReason;
|
|
197
|
+
config?: LlmCallConfig;
|
|
198
|
+
/** Rendered system prompt; absent for a system-less request. */
|
|
199
|
+
system?: string;
|
|
200
|
+
/** Assembled tool schemas; absent for a tool-less request. */
|
|
201
|
+
tools?: ToolSchemaSnapshot[];
|
|
202
|
+
/** True when this snapshot also begins a new model-message series. */
|
|
203
|
+
startsSeries?: boolean;
|
|
204
|
+
}
|
|
205
|
+
export interface RequestContextPayload {
|
|
206
|
+
provider?: string;
|
|
207
|
+
model?: string;
|
|
208
|
+
contextWindow?: number;
|
|
209
|
+
}
|
|
210
|
+
export interface CheckpointPayload {
|
|
211
|
+
/** The compacted prefix: messages the model keeps seeing after compaction. */
|
|
212
|
+
messages: ModelMessage[];
|
|
213
|
+
summary?: string;
|
|
214
|
+
tokensBefore?: number;
|
|
215
|
+
/**
|
|
216
|
+
* The surface span this checkpoint shadows, when written by a v7
|
|
217
|
+
* compaction: `start`/`end` are the seqs of the first and last *surface
|
|
218
|
+
* nodes* being replaced (surface positions, so `start` may be numerically
|
|
219
|
+
* greater than `end` after an earlier compaction). The fold removes every
|
|
220
|
+
* live node between those two positions and inserts this checkpoint there;
|
|
221
|
+
* the kept tail stays in place and the log is never rewritten.
|
|
222
|
+
* `'replace'` (bare string) is the legacy v5 form and shadows nothing
|
|
223
|
+
* structurally; projection treats it as a full-surface seed.
|
|
224
|
+
*/
|
|
225
|
+
surfaceOp?: {
|
|
226
|
+
op: 'replace';
|
|
227
|
+
start: number;
|
|
228
|
+
end: number;
|
|
229
|
+
} | 'replace';
|
|
230
|
+
/** @deprecated v0.2 keeps raw events in place; use messages instead. */
|
|
231
|
+
snapshot?: SessionEvent[];
|
|
232
|
+
}
|
|
233
|
+
export type PlanItemStatus = 'pending' | 'done' | 'failed';
|
|
234
|
+
export interface PlanItemPayload {
|
|
235
|
+
id: string;
|
|
236
|
+
title: string;
|
|
237
|
+
status: PlanItemStatus;
|
|
238
|
+
detail?: string;
|
|
239
|
+
}
|
|
240
|
+
export interface PlanPayload {
|
|
241
|
+
items: PlanItemPayload[];
|
|
242
|
+
status?: 'pending' | 'running' | 'done' | 'failed';
|
|
243
|
+
summary?: string;
|
|
244
|
+
}
|
|
245
|
+
export interface InboxSplicePayload {
|
|
246
|
+
target: 'next-turn' | 'next-step';
|
|
247
|
+
index?: number;
|
|
248
|
+
deleteCount?: number;
|
|
249
|
+
inserted?: Array<{
|
|
250
|
+
id: string;
|
|
251
|
+
content: string;
|
|
252
|
+
/** Structured message payload preserved across restart when present. */
|
|
253
|
+
payload?: unknown;
|
|
254
|
+
mode?: 'followup' | 'steer';
|
|
255
|
+
}>;
|
|
256
|
+
}
|
|
257
|
+
/** A durable change to session display metadata (title, mode, agentType). */
|
|
258
|
+
export interface MetaPatchPayload {
|
|
259
|
+
/** Keys changed by this event; only present keys are touched. */
|
|
260
|
+
fields: Array<'title' | 'agentType' | 'mode'>;
|
|
261
|
+
title?: string;
|
|
262
|
+
agentType?: AgentType;
|
|
263
|
+
mode?: SessionMode;
|
|
264
|
+
}
|
|
265
|
+
export type AgentType = 'general' | 'coding';
|
|
266
|
+
export type SessionMode = 'auto' | 'plan' | 'execute';
|
|
267
|
+
export type SessionEventType = MessageEventType | 'assistant/chunk' | 'tool/call' | 'tool/result' | 'request/header' | 'request/context' | 'agent/inbox/spliced' | 'plan' | 'checkpoint' | 'compaction/start' | 'compaction/end' | 'meta' | 'meta/patch' | 'llm/retry' | 'llm/retry-started' | 'turn/start' | 'turn/end' | 'step/start' | 'step/end';
|
|
268
|
+
export interface SessionEventBase<T extends SessionEventType, P> {
|
|
269
|
+
id: string;
|
|
270
|
+
seq: number;
|
|
271
|
+
ts: number;
|
|
272
|
+
type: T;
|
|
273
|
+
payload: P;
|
|
274
|
+
/** Surface placement; required on message-producing events. */
|
|
275
|
+
surfaceOp?: SurfaceOp;
|
|
276
|
+
/** Seqs of earlier raw events this event derives from. */
|
|
277
|
+
sourceEventSeqs?: number[];
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The message-producing subset of event types. Every variant is a surface
|
|
281
|
+
* node that carries `surfaceOp` and participates in the ordered fold.
|
|
282
|
+
* `system/message` is included because tnega keeps the system prompt on the
|
|
283
|
+
* transcript (OpenAI-style), so it must be replaceable and derivable like any
|
|
284
|
+
* other message rather than living only in `request/header`.
|
|
285
|
+
*/
|
|
286
|
+
export type SurfaceEventType = 'user/message' | 'system/message' | 'assistant/message' | 'tool/result';
|
|
287
|
+
export type SurfaceOp = 'append' | {
|
|
288
|
+
op: 'replace';
|
|
289
|
+
start: number;
|
|
290
|
+
end: number;
|
|
291
|
+
};
|
|
292
|
+
export type SessionEvent = SessionEventBase<'user/message', UserMessagePayload> | SessionEventBase<'assistant/message', AssistantMessagePayload> | SessionEventBase<'assistant/chunk', AssistantChunkPayload> | SessionEventBase<'system/message', SystemMessagePayload> | SessionEventBase<'tool/call', ToolCallPayload> | SessionEventBase<'tool/result', ToolResultPayload> | SessionEventBase<'request/header', RequestHeaderPayload> | SessionEventBase<'request/context', RequestContextPayload> | SessionEventBase<'agent/inbox/spliced', InboxSplicePayload> | SessionEventBase<'plan', PlanPayload> | SessionEventBase<'checkpoint', CheckpointPayload> | SessionEventBase<'compaction/start', CompactionStartPayload> | SessionEventBase<'compaction/end', CompactionEndPayload> | SessionEventBase<'meta', Record<string, unknown>> | SessionEventBase<'meta/patch', MetaPatchPayload> | SessionEventBase<'llm/retry', LLMRetryPayload> | SessionEventBase<'llm/retry-started', LLMRetryStartedPayload> | SessionEventBase<'turn/start', TurnStartPayload> | SessionEventBase<'turn/end', TurnEndPayload> | SessionEventBase<'step/start', StepStartPayload> | SessionEventBase<'step/end', StepEndPayload>;
|
|
293
|
+
export interface SessionConfig {
|
|
294
|
+
file: string;
|
|
295
|
+
broadcast?: SessionBroadcast;
|
|
296
|
+
}
|
|
297
|
+
export interface CompactOptions {
|
|
298
|
+
keep?: number;
|
|
299
|
+
keepTokens?: number;
|
|
300
|
+
summary?: string;
|
|
301
|
+
tokensBefore?: number;
|
|
302
|
+
messages?: ModelMessage[];
|
|
303
|
+
}
|
|
304
|
+
export type ReplayReducer<T> = (state: T, event: SessionEvent) => T | Promise<T>;
|
|
305
|
+
export type SessionBroadcast = (type: 'event' | 'flush', payload: unknown) => void;
|
|
306
|
+
export interface ContextUsage {
|
|
307
|
+
tokens: number;
|
|
308
|
+
limit: number;
|
|
309
|
+
ratio: number;
|
|
310
|
+
}
|
|
311
|
+
export declare const DEFAULT_CONTEXT_LIMIT = 128000;
|
|
312
|
+
/**
|
|
313
|
+
* Project an event list to model messages, in the list's own order. Each
|
|
314
|
+
* surface event is mapped through {@link deriveEventMessage}; a `checkpoint`
|
|
315
|
+
* splices its compacted prefix in at its position. This is the building block
|
|
316
|
+
* both {@link deriveSurfaceMessages} (surface order) and callers holding a
|
|
317
|
+
* compact slice (e.g. the events a summarizer will read) share.
|
|
318
|
+
*/
|
|
319
|
+
export declare function projectEvents(events: readonly SessionEvent[]): ModelMessage[];
|
|
320
|
+
export declare function isAppendSurfaceEvent(event: SessionEvent): boolean;
|
|
321
|
+
export declare function isSurfaceEventType(type: SessionEventType): type is SurfaceEventType;
|
|
322
|
+
/** A v7 checkpoint shadows a surface span; legacy ('replace') shadows nothing structurally. */
|
|
323
|
+
export declare function checkpointShadowRange(event: SessionEvent): {
|
|
324
|
+
start: number;
|
|
325
|
+
end: number;
|
|
326
|
+
} | undefined;
|
|
327
|
+
/** Detached current surface nodes plus every replacement's shadowed ranges. */
|
|
328
|
+
export interface SurfaceFoldReplacement {
|
|
329
|
+
seq: number;
|
|
330
|
+
start: number;
|
|
331
|
+
end: number;
|
|
332
|
+
shadowedSeqs: number[];
|
|
333
|
+
}
|
|
334
|
+
export interface SurfaceFoldResult {
|
|
335
|
+
nodes: number[];
|
|
336
|
+
replacements: SurfaceFoldReplacement[];
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Fold a log's surface operations into the current surface node order.
|
|
340
|
+
*
|
|
341
|
+
* The surface is *positional*: a replacement removes the live nodes between
|
|
342
|
+
* the positions of `start`/`end` and inserts the replacing event's seq there,
|
|
343
|
+
* so surface order is independent of raw seq order — after compaction the
|
|
344
|
+
* checkpoint node may carry a higher seq than the kept tail that follows it.
|
|
345
|
+
*/
|
|
346
|
+
export declare function foldSurface(events: readonly SessionEvent[]): SurfaceFoldResult;
|
|
347
|
+
/**
|
|
348
|
+
* Derive one surface node's model-visible message. A content-less assistant
|
|
349
|
+
* turn (e.g. a max-token cutoff with no output and no tool calls) is skipped
|
|
350
|
+
* so it never enters a provider transcript; `tool/result` yields the `tool`
|
|
351
|
+
* role message that pairs with the assistant's `toolCalls`.
|
|
352
|
+
*/
|
|
353
|
+
export declare function deriveEventMessage(event: SessionEvent): ModelMessage | null;
|
|
354
|
+
/**
|
|
355
|
+
* Derive the model transcript strictly from the folded surface: walk the
|
|
356
|
+
* surface nodes in surface position order (so a checkpoint's prefix is placed
|
|
357
|
+
* where it replaced history, whatever its raw seq), mapping each node through
|
|
358
|
+
* {@link deriveEventMessage}. This is the single source of the model's view;
|
|
359
|
+
* raw log order never influences it.
|
|
360
|
+
*/
|
|
361
|
+
export declare function deriveSurfaceMessages(events: readonly SessionEvent[]): ModelMessage[];
|
|
362
|
+
/**
|
|
363
|
+
* Project the log into a human-facing transcript (the web view). Unlike the
|
|
364
|
+
* model surface, a compaction does **not** hide the history it summarized:
|
|
365
|
+
* shadowed messages are kept in place, and each live checkpoint is retained
|
|
366
|
+
* as a marker exactly where it replaced them (with nested checkpoints
|
|
367
|
+
* expanded recursively). Readers see the full conversation; the model keeps
|
|
368
|
+
* deriving from the folded surface via {@link deriveSurfaceMessages}.
|
|
369
|
+
*/
|
|
370
|
+
export declare function transcriptEvents(events: readonly SessionEvent[]): SessionEvent[];
|
|
371
|
+
export declare function foldRequestHeader(events: readonly SessionEvent[]): RequestHeaderPayload | undefined;
|
|
372
|
+
export declare function foldRequestContext(events: readonly SessionEvent[]): RequestContextPayload | undefined;
|
|
373
|
+
/**
|
|
374
|
+
* Session display metadata, rebuilt from the immutable events: the head
|
|
375
|
+
* `meta` base followed by every `meta/patch` in seq order. No mutable
|
|
376
|
+
* side file or in-place rewrite is involved, so title/mode/agentType stay
|
|
377
|
+
* replayable and fork-clean like every other durable fact.
|
|
378
|
+
*/
|
|
379
|
+
export declare function foldSessionMeta(events: readonly SessionEvent[]): {
|
|
380
|
+
title?: string;
|
|
381
|
+
agentType?: AgentType;
|
|
382
|
+
mode?: SessionMode;
|
|
383
|
+
};
|
|
384
|
+
/** Convenience wrapper: fold session metadata from a `SessionLog`'s events. */
|
|
385
|
+
export declare function foldMetaFromLog(log: {
|
|
386
|
+
read(): Promise<SessionEvent[]>;
|
|
387
|
+
}): Promise<{
|
|
388
|
+
title?: string;
|
|
389
|
+
agentType?: AgentType;
|
|
390
|
+
mode?: SessionMode;
|
|
391
|
+
}>;
|
|
392
|
+
export declare function estimateMessageTokens(messages: readonly ModelMessage[]): number;
|
|
393
|
+
export declare function estimateEventTokens(event: SessionEvent): number;
|
|
394
|
+
export declare function suffixStartIndexForTokens(events: readonly SessionEvent[], targetTokens: number): number;
|
|
395
|
+
export declare function safeCompactSplit(events: readonly SessionEvent[], splitIndex: number): number;
|
|
396
|
+
export declare function repairUnclosed(events: readonly SessionEvent[]): SessionEvent[];
|
|
397
|
+
export declare function resolveCompactKeep(events: readonly SessionEvent[], options: CompactOptions): number;
|
|
398
|
+
export declare function estimateContextUsage(messages: readonly ModelMessage[], limit?: number): ContextUsage;
|
|
399
|
+
export declare class SessionLog {
|
|
400
|
+
readonly file: string;
|
|
401
|
+
private _broadcast?;
|
|
402
|
+
private _events;
|
|
403
|
+
private _surface;
|
|
404
|
+
private _surfaceNodes;
|
|
405
|
+
private _requestHeader;
|
|
406
|
+
private _requestContext;
|
|
407
|
+
private _loaded;
|
|
408
|
+
private _nextSeq;
|
|
409
|
+
private _queue;
|
|
410
|
+
private _writeTail;
|
|
411
|
+
private _pending;
|
|
412
|
+
private _drainScheduled;
|
|
413
|
+
private _writeError;
|
|
414
|
+
constructor(file: string, _broadcast?: SessionBroadcast | undefined);
|
|
415
|
+
init(): Promise<void>;
|
|
416
|
+
append(type: 'user/message', payload: UserMessagePayload): Promise<SessionEvent>;
|
|
417
|
+
append(type: 'assistant/message', payload: AssistantMessagePayload): Promise<SessionEvent>;
|
|
418
|
+
append(type: 'assistant/chunk', payload: AssistantChunkPayload): Promise<SessionEvent>;
|
|
419
|
+
append(type: 'system/message', payload: SystemMessagePayload): Promise<SessionEvent>;
|
|
420
|
+
append(type: 'tool/call', payload: ToolCallPayload): Promise<SessionEvent>;
|
|
421
|
+
append(type: 'tool/result', payload: ToolResultPayload): Promise<SessionEvent>;
|
|
422
|
+
append(type: 'request/header', payload: RequestHeaderPayload): Promise<SessionEvent>;
|
|
423
|
+
append(type: 'request/context', payload: RequestContextPayload): Promise<SessionEvent>;
|
|
424
|
+
append(type: 'agent/inbox/spliced', payload: InboxSplicePayload): Promise<SessionEvent>;
|
|
425
|
+
append(type: 'plan', payload: PlanPayload): Promise<SessionEvent>;
|
|
426
|
+
append(type: 'checkpoint', payload: CheckpointPayload): Promise<SessionEvent>;
|
|
427
|
+
append(type: 'compaction/start', payload: CompactionStartPayload): Promise<SessionEvent>;
|
|
428
|
+
append(type: 'compaction/end', payload: CompactionEndPayload): Promise<SessionEvent>;
|
|
429
|
+
append(type: 'meta', payload: Record<string, unknown>): Promise<SessionEvent>;
|
|
430
|
+
append(type: 'meta/patch', payload: MetaPatchPayload): Promise<SessionEvent>;
|
|
431
|
+
append(type: 'llm/retry', payload: LLMRetryPayload): Promise<SessionEvent>;
|
|
432
|
+
append(type: 'llm/retry-started', payload: LLMRetryStartedPayload): Promise<SessionEvent>;
|
|
433
|
+
append(type: 'turn/start', payload: TurnStartPayload): Promise<SessionEvent>;
|
|
434
|
+
append(type: 'turn/end', payload: TurnEndPayload): Promise<SessionEvent>;
|
|
435
|
+
append(type: 'step/start', payload: StepStartPayload): Promise<SessionEvent>;
|
|
436
|
+
append(type: 'step/end', payload: StepEndPayload): Promise<SessionEvent>;
|
|
437
|
+
private _buildEvent;
|
|
438
|
+
private _commitEvent;
|
|
439
|
+
/**
|
|
440
|
+
* Rebuild both surface views from the committed log. The nodes come from the
|
|
441
|
+
* positional {@link foldSurface} fold; the messages come from
|
|
442
|
+
* {@link deriveSurfaceMessages}, which walks those nodes in surface order —
|
|
443
|
+
* the model view is a pure function of the surface, never of raw file order.
|
|
444
|
+
*/
|
|
445
|
+
private _refreshSurface;
|
|
446
|
+
private _enqueue;
|
|
447
|
+
private _drainWrite;
|
|
448
|
+
flush(): Promise<number>;
|
|
449
|
+
lineage(messageId: string): Promise<SessionEvent[]>;
|
|
450
|
+
/** The next turn number: one more than the highest recorded `turn/start`. */
|
|
451
|
+
nextTurn(): Promise<number>;
|
|
452
|
+
forkAt(messageId: string): Promise<SessionEvent[]>;
|
|
453
|
+
read(): Promise<SessionEvent[]>;
|
|
454
|
+
deriveMessages(): Promise<ModelMessage[]>;
|
|
455
|
+
/** The ordered surface events, after compaction replacements are applied. */
|
|
456
|
+
surfaceEvents(): Promise<SessionEvent[]>;
|
|
457
|
+
/** The latest `request/header` snapshot, or undefined before the first one. */
|
|
458
|
+
requestHeader(): RequestHeaderPayload | undefined;
|
|
459
|
+
/** The latest resolved route metadata, or undefined before the first one. */
|
|
460
|
+
requestContext(): RequestContextPayload | undefined;
|
|
461
|
+
/** Display metadata folded from the durable `meta` + `meta/patch` events. */
|
|
462
|
+
meta(): Promise<{
|
|
463
|
+
title?: string;
|
|
464
|
+
agentType?: AgentType;
|
|
465
|
+
mode?: SessionMode;
|
|
466
|
+
}>;
|
|
467
|
+
estimateContext(limit?: number): Promise<ContextUsage>;
|
|
468
|
+
replay(): Promise<SessionEvent[]>;
|
|
469
|
+
replay<T>(reduce: ReplayReducer<T>, initial: T): Promise<T>;
|
|
470
|
+
/** Structural violations in the loaded event stream (empty = balanced). */
|
|
471
|
+
runInvariants(): Promise<SessionInvariantFailure[]>;
|
|
472
|
+
/**
|
|
473
|
+
* Compact the session without ever rewriting history.
|
|
474
|
+
*
|
|
475
|
+
* The surface (not the raw log) is the unit of compaction. A kept suffix —
|
|
476
|
+
* measured in tokens via `keepTokens`, or in nodes via `keep`, and always
|
|
477
|
+
* snapped to start on a fresh `user/message` turn so a tool result never
|
|
478
|
+
* outlives the assistant call it answers — stays visible; every earlier
|
|
479
|
+
* surface node is shadowed by the checkpoint's `surfaceOp.replace` range.
|
|
480
|
+
* The three compaction events are appended like any other event, so seqs
|
|
481
|
+
* stay immutable, live observers see the transition, and a crash can only
|
|
482
|
+
* leave an orphaned `compaction/start` rather than a torn rewrite.
|
|
483
|
+
*
|
|
484
|
+
* Returns the new event count, or the unchanged count when there is no
|
|
485
|
+
* useful surface span to shadow.
|
|
486
|
+
*/
|
|
487
|
+
compact(options?: CompactOptions): Promise<number>;
|
|
488
|
+
close(): Promise<void>;
|
|
489
|
+
private _run;
|
|
490
|
+
private _ensureLoaded;
|
|
491
|
+
private _read;
|
|
492
|
+
private _assertFormat;
|
|
493
|
+
private _resolveLineage;
|
|
494
|
+
}
|
|
495
|
+
export declare const session: {
|
|
496
|
+
name: string;
|
|
497
|
+
apply: (ctx: Context, config: SessionConfig) => Promise<() => Promise<void>>;
|
|
498
|
+
};
|
|
499
|
+
export * from './invariant.js';
|
|
500
|
+
export declare const name = "index.js";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session invariant companion.
|
|
3
|
+
*
|
|
4
|
+
* Pure structural checks over a session event stream, mirroring the DSH
|
|
5
|
+
* "each package ships a ./invariant" discipline. They assert the relationships
|
|
6
|
+
* the event-sourcing layer promises:
|
|
7
|
+
*
|
|
8
|
+
* - durable lifecycle structures (turn / step / tool call) stay balanced —
|
|
9
|
+
* every `turn/start` has a `turn/end`, every `tool/call` a `tool/result`;
|
|
10
|
+
* - seq stays strictly monotonic;
|
|
11
|
+
* - message events that reach the model surface stay append-only unless they
|
|
12
|
+
* belong to a compaction replacement.
|
|
13
|
+
*
|
|
14
|
+
* The checks are pure over `SessionEvent[]`, so they run equally on a full
|
|
15
|
+
* log, on a single newly-committed event, or in tests. Recovery (repairing an
|
|
16
|
+
* interrupted log into a closed one) is `repairUnclosed`; these checks assert
|
|
17
|
+
* that the result is actually legal.
|
|
18
|
+
*/
|
|
19
|
+
import type { SessionEvent } from './index.js';
|
|
20
|
+
export interface SessionInvariantFailure {
|
|
21
|
+
name: string;
|
|
22
|
+
detail: string;
|
|
23
|
+
}
|
|
24
|
+
/** Balanced `turn/start` ↔ `turn/end` per turn number. */
|
|
25
|
+
export declare function checkBalancedTurns(events: readonly SessionEvent[]): SessionInvariantFailure[];
|
|
26
|
+
/** Balanced `step/start` ↔ `step/end` per (turn, step). */
|
|
27
|
+
export declare function checkBalancedSteps(events: readonly SessionEvent[]): SessionInvariantFailure[];
|
|
28
|
+
/** Balanced `tool/call` ↔ `tool/result` per call id. */
|
|
29
|
+
export declare function checkBalancedToolCalls(events: readonly SessionEvent[]): SessionInvariantFailure[];
|
|
30
|
+
/** seq is strictly monotonic (an append-only log never rewinds). */
|
|
31
|
+
export declare function checkMonotonicSeq(events: readonly SessionEvent[]): SessionInvariantFailure[];
|
|
32
|
+
/** Aggregate all structural session checks over an event stream. */
|
|
33
|
+
export declare function checkSessionInvariants(events: readonly SessionEvent[]): SessionInvariantFailure[];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Context } from "../../core/src/index.js";
|
|
2
|
+
import type { ToolDefinition } from './index.js';
|
|
3
|
+
import { type ExecutionProvider } from './execution.js';
|
|
4
|
+
export interface BuiltinToolsConfig {
|
|
5
|
+
cwd?: string;
|
|
6
|
+
allowNetwork?: boolean;
|
|
7
|
+
allowShell?: boolean;
|
|
8
|
+
disabled?: readonly string[];
|
|
9
|
+
maxReadBytes?: number;
|
|
10
|
+
maxWriteBytes?: number;
|
|
11
|
+
maxSearchBytes?: number;
|
|
12
|
+
maxResults?: number;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
execution?: ExecutionProvider;
|
|
15
|
+
}
|
|
16
|
+
export declare class ToolInputError extends Error {
|
|
17
|
+
name: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function createBuiltinToolDefinitions(config?: BuiltinToolsConfig): ToolDefinition[];
|
|
20
|
+
export declare const builtinTools: {
|
|
21
|
+
name: string;
|
|
22
|
+
inject: string[];
|
|
23
|
+
apply(ctx: Context, config?: BuiltinToolsConfig): void;
|
|
24
|
+
};
|
|
25
|
+
export declare const DEFAULT_BUILTIN_TOOL_NAMES: readonly string[];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface ShellRequest {
|
|
2
|
+
command: string;
|
|
3
|
+
cwd: string;
|
|
4
|
+
timeoutMs?: number;
|
|
5
|
+
maxBuffer?: number;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export interface ShellResult {
|
|
9
|
+
exitCode: number;
|
|
10
|
+
stdout: string;
|
|
11
|
+
stderr: string;
|
|
12
|
+
}
|
|
13
|
+
export interface HttpRequest {
|
|
14
|
+
url: string;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
maxBytes?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface HttpResponse {
|
|
20
|
+
status: number;
|
|
21
|
+
ok: boolean;
|
|
22
|
+
headers: Record<string, string>;
|
|
23
|
+
body: string;
|
|
24
|
+
truncated: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** Replaceable execution boundary used by builtin network/shell tools. */
|
|
27
|
+
export interface ExecutionProvider {
|
|
28
|
+
runShell(request: ShellRequest): Promise<ShellResult>;
|
|
29
|
+
fetchHttp(request: HttpRequest): Promise<HttpResponse>;
|
|
30
|
+
}
|
|
31
|
+
export declare const localExecutionProvider: ExecutionProvider;
|