@tangle-network/agent-runtime 0.18.0 → 0.19.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.
@@ -0,0 +1,225 @@
1
+ import { AgentProfile, CreateSandboxOptions, SandboxEvent, SandboxInstance } from '@tangle-network/sandbox';
2
+ import { R as RuntimeRunHandle } from './runtime-run-4pbY3Jq5.js';
3
+
4
+ /**
5
+ * @experimental
6
+ *
7
+ * Driven-loop substrate — type surface.
8
+ *
9
+ * The loop kernel orchestrates around the sandbox SDK; it does not invent
10
+ * its own notion of "what an agent is". Each iteration is a sandbox-SDK
11
+ * `streamPrompt` call against an `AgentProfile`. The kernel owns iteration
12
+ * accounting, concurrency, abort propagation, cost aggregation, and trace
13
+ * emission; the driver owns topology (plan + decide); the validator owns
14
+ * output scoring; the output adapter owns event-stream → typed-output decode.
15
+ */
16
+
17
+ /** @experimental */
18
+ interface DefaultVerdict {
19
+ /** Whether the output meets the validator's pass criteria. */
20
+ valid: boolean;
21
+ /** Aggregate score in [0, 1]. Drivers use this for winner selection. */
22
+ score: number;
23
+ /** Per-dimension scores. Free-form; weighted into `score` by the validator. */
24
+ scores?: Record<string, number>;
25
+ /** Human-readable rationale; surfaces in trace + final-result `winner.verdict`. */
26
+ notes?: string;
27
+ }
28
+ /** @experimental */
29
+ interface ValidationCtx {
30
+ /** Iteration index this output came from (0-based). */
31
+ iteration: number;
32
+ /** Cooperative cancellation channel. */
33
+ signal: AbortSignal;
34
+ }
35
+ /** @experimental */
36
+ interface Validator<Output, Verdict = DefaultVerdict> {
37
+ validate(output: Output, ctx: ValidationCtx): Promise<Verdict>;
38
+ }
39
+ /**
40
+ * Sandbox-SDK-shaped agent specification.
41
+ *
42
+ * The kernel uses `profile` to instantiate a sandbox per iteration, formats
43
+ * `task` into a prompt via `taskToPrompt`, and merges `sandboxOverrides` into
44
+ * the `CreateSandboxOptions` it passes to `client.create`. Heterogeneous
45
+ * fanout supplies multiple `AgentRunSpec`s and the kernel round-robins
46
+ * through them when the driver plans N tasks.
47
+ *
48
+ * @experimental
49
+ */
50
+ interface AgentRunSpec<Task> {
51
+ /** Sandbox SDK profile — what kind of agent runs the task. */
52
+ profile: AgentProfile;
53
+ /** Task → prompt formatter. Pure and deterministic. */
54
+ taskToPrompt: (task: Task) => string;
55
+ /**
56
+ * Per-spec stable name. Surfaced in trace events and the default winner
57
+ * selector tiebreak. Falls back to `profile.name ?? 'agent'`.
58
+ */
59
+ name?: string;
60
+ /**
61
+ * Optional sandbox-SDK `CreateSandboxOptions` overrides merged on top of
62
+ * the kernel's defaults. `backend.profile` is set to `profile` by the
63
+ * kernel and cannot be overridden here — use `profile` itself for that.
64
+ */
65
+ sandboxOverrides?: Partial<Omit<CreateSandboxOptions, 'backend'>> & {
66
+ backend?: Omit<NonNullable<CreateSandboxOptions['backend']>, 'profile'>;
67
+ };
68
+ }
69
+ /**
70
+ * Stream of `SandboxEvent`s → typed `Output`.
71
+ *
72
+ * Adapters are pure functions over the already-collected event array; they
73
+ * do not receive the live AsyncIterable so they can be replayed against
74
+ * persisted streams during tests / replays.
75
+ *
76
+ * @experimental
77
+ */
78
+ interface OutputAdapter<Output> {
79
+ parse(events: SandboxEvent[]): Output;
80
+ }
81
+ /** @experimental */
82
+ interface Iteration<Task, Output> {
83
+ /** 0-based iteration index assigned by the kernel. */
84
+ index: number;
85
+ task: Task;
86
+ /** Stable name of the `AgentRunSpec` that produced this iteration. */
87
+ agentRunName: string;
88
+ output?: Output;
89
+ verdict?: DefaultVerdict;
90
+ error?: Error;
91
+ /** Raw sandbox event stream collected for this iteration. */
92
+ events: SandboxEvent[];
93
+ startedAt: number;
94
+ endedAt: number;
95
+ costUsd: number;
96
+ }
97
+ /** @experimental */
98
+ interface Driver<Task, Output, Decision> {
99
+ /**
100
+ * Stable identifier surfaced in trace events. Default `'driver'`.
101
+ */
102
+ readonly name?: string;
103
+ /**
104
+ * Tasks to issue this iteration. `[task]` → refine; N copies → fanout;
105
+ * `[]` → no more work this round (kernel proceeds to `decide`).
106
+ */
107
+ plan(task: Task, history: ReadonlyArray<Iteration<Task, Output>>): Promise<Task[]>;
108
+ /**
109
+ * Inspect history and return the next state. The kernel terminates the
110
+ * loop when `decide` returns a value listed in `isTerminalDecision`
111
+ * (`'stop' | 'pick-winner' | 'fail' | 'done'`), when `maxIterations`
112
+ * is hit, or when the abort signal fires.
113
+ */
114
+ decide(history: ReadonlyArray<Iteration<Task, Output>>): Decision | Promise<Decision>;
115
+ }
116
+ /** @experimental */
117
+ interface LoopWinner<Task, Output> {
118
+ task: Task;
119
+ output: Output;
120
+ verdict?: DefaultVerdict;
121
+ iterationIndex: number;
122
+ agentRunName: string;
123
+ }
124
+ /** @experimental */
125
+ interface LoopResult<Task, Output, Decision> {
126
+ decision: Decision;
127
+ iterations: Iteration<Task, Output>[];
128
+ winner?: LoopWinner<Task, Output>;
129
+ durationMs: number;
130
+ /** Sum of every iteration's `costUsd`. */
131
+ costUsd: number;
132
+ }
133
+ /**
134
+ * Minimal sandbox client surface the kernel calls. Satisfied structurally by
135
+ * `new Sandbox({ apiKey, baseUrl })` — declared as a structural type so
136
+ * tests can pass a stub without instantiating the SDK.
137
+ *
138
+ * @experimental
139
+ */
140
+ interface LoopSandboxClient {
141
+ create(options?: CreateSandboxOptions): Promise<SandboxInstance>;
142
+ }
143
+ /** @experimental */
144
+ interface LoopTraceEmitter {
145
+ emit(event: LoopTraceEvent): void | Promise<void>;
146
+ }
147
+ /** @experimental */
148
+ type LoopTraceEvent = {
149
+ kind: 'loop.started';
150
+ runId: string;
151
+ timestamp: number;
152
+ payload: LoopStartedPayload;
153
+ } | {
154
+ kind: 'loop.iteration.started';
155
+ runId: string;
156
+ timestamp: number;
157
+ payload: LoopIterationStartedPayload;
158
+ } | {
159
+ kind: 'loop.iteration.ended';
160
+ runId: string;
161
+ timestamp: number;
162
+ payload: LoopIterationEndedPayload;
163
+ } | {
164
+ kind: 'loop.decision';
165
+ runId: string;
166
+ timestamp: number;
167
+ payload: LoopDecisionPayload;
168
+ } | {
169
+ kind: 'loop.ended';
170
+ runId: string;
171
+ timestamp: number;
172
+ payload: LoopEndedPayload;
173
+ };
174
+ /** @experimental */
175
+ interface LoopStartedPayload {
176
+ driver: string;
177
+ agentRunNames: string[];
178
+ maxIterations: number;
179
+ maxConcurrency: number;
180
+ }
181
+ /** @experimental */
182
+ interface LoopIterationStartedPayload {
183
+ iterationIndex: number;
184
+ agentRunName: string;
185
+ taskHash: string;
186
+ }
187
+ /** @experimental */
188
+ interface LoopIterationEndedPayload {
189
+ iterationIndex: number;
190
+ agentRunName: string;
191
+ outputHash?: string;
192
+ verdict?: DefaultVerdict;
193
+ error?: string;
194
+ costUsd: number;
195
+ durationMs: number;
196
+ }
197
+ /** @experimental */
198
+ interface LoopDecisionPayload {
199
+ decision: string;
200
+ historyLength: number;
201
+ }
202
+ /** @experimental */
203
+ interface LoopEndedPayload {
204
+ winnerIterationIndex?: number;
205
+ totalCostUsd: number;
206
+ durationMs: number;
207
+ iterations: number;
208
+ }
209
+ /** @experimental */
210
+ interface ExecCtx {
211
+ /** Sandbox SDK client — the kernel calls `.create()` per iteration. */
212
+ sandboxClient: LoopSandboxClient;
213
+ /** Optional trace emitter. When set, the kernel emits `loop.*` events. */
214
+ traceEmitter?: LoopTraceEmitter;
215
+ /**
216
+ * Optional production-run handle. When set, every synthesized `llm_call`
217
+ * the kernel infers from a sandbox event stream is forwarded via
218
+ * `runHandle.observe` so per-run cost aggregates pick up loop spend.
219
+ */
220
+ runHandle?: RuntimeRunHandle;
221
+ /** Cooperative cancellation signal. */
222
+ signal?: AbortSignal;
223
+ }
224
+
225
+ export type { AgentRunSpec as A, DefaultVerdict as D, ExecCtx as E, Iteration as I, LoopWinner as L, OutputAdapter as O, Validator as V, Driver as a, LoopResult as b, LoopDecisionPayload as c, LoopEndedPayload as d, LoopIterationEndedPayload as e, LoopIterationStartedPayload as f, LoopSandboxClient as g, LoopStartedPayload as h, LoopTraceEmitter as i, LoopTraceEvent as j, ValidationCtx as k };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/agent-runtime",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "description": "Reusable runtime lifecycle for domain-specific agents.",
5
5
  "homepage": "https://github.com/tangle-network/agent-runtime#readme",
6
6
  "repository": {
@@ -33,6 +33,16 @@
33
33
  "types": "./dist/agent.d.ts",
34
34
  "import": "./dist/agent.js",
35
35
  "default": "./dist/agent.js"
36
+ },
37
+ "./loops": {
38
+ "types": "./dist/loops.d.ts",
39
+ "import": "./dist/loops.js",
40
+ "default": "./dist/loops.js"
41
+ },
42
+ "./profiles": {
43
+ "types": "./dist/profiles.d.ts",
44
+ "import": "./dist/profiles.js",
45
+ "default": "./dist/profiles.js"
36
46
  }
37
47
  },
38
48
  "files": [