@lloyal-labs/lloyal-agents 1.0.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.
Files changed (46) hide show
  1. package/README.md +280 -0
  2. package/dist/Tool.d.ts +65 -0
  3. package/dist/Tool.d.ts.map +1 -0
  4. package/dist/Tool.js +59 -0
  5. package/dist/Tool.js.map +1 -0
  6. package/dist/agent-pool.d.ts +114 -0
  7. package/dist/agent-pool.d.ts.map +1 -0
  8. package/dist/agent-pool.js +528 -0
  9. package/dist/agent-pool.js.map +1 -0
  10. package/dist/context.d.ts +33 -0
  11. package/dist/context.d.ts.map +1 -0
  12. package/dist/context.js +33 -0
  13. package/dist/context.js.map +1 -0
  14. package/dist/diverge.d.ts +39 -0
  15. package/dist/diverge.d.ts.map +1 -0
  16. package/dist/diverge.js +148 -0
  17. package/dist/diverge.js.map +1 -0
  18. package/dist/generate.d.ts +30 -0
  19. package/dist/generate.d.ts.map +1 -0
  20. package/dist/generate.js +58 -0
  21. package/dist/generate.js.map +1 -0
  22. package/dist/index.d.ts +15 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +28 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/init.d.ts +58 -0
  27. package/dist/init.d.ts.map +1 -0
  28. package/dist/init.js +57 -0
  29. package/dist/init.js.map +1 -0
  30. package/dist/run-agents.d.ts +39 -0
  31. package/dist/run-agents.d.ts.map +1 -0
  32. package/dist/run-agents.js +46 -0
  33. package/dist/run-agents.js.map +1 -0
  34. package/dist/shared-root.d.ts +55 -0
  35. package/dist/shared-root.d.ts.map +1 -0
  36. package/dist/shared-root.js +62 -0
  37. package/dist/shared-root.js.map +1 -0
  38. package/dist/toolkit.d.ts +38 -0
  39. package/dist/toolkit.d.ts.map +1 -0
  40. package/dist/toolkit.js +31 -0
  41. package/dist/toolkit.js.map +1 -0
  42. package/dist/types.d.ts +380 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/dist/types.js +3 -0
  45. package/dist/types.js.map +1 -0
  46. package/package.json +40 -0
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withSharedRoot = withSharedRoot;
4
+ const effection_1 = require("effection");
5
+ const sdk_1 = require("@lloyal-labs/sdk");
6
+ const context_1 = require("./context");
7
+ /**
8
+ * Scoped shared root branch with guaranteed cleanup
9
+ *
10
+ * Creates a root branch, prefills the system prompt, and passes it to
11
+ * the body function. The root is pruned via try/finally when the body
12
+ * returns or throws, regardless of whether children still exist.
13
+ *
14
+ * Use this for the cold-path pattern where multiple agents share a
15
+ * tokenized system prompt prefix. The `sharedPrefixLength` passed to
16
+ * the body enables KV savings calculation.
17
+ *
18
+ * @param opts - System prompt, tools, and sampling parameters
19
+ * @param body - Operation that receives the root branch and prefix length.
20
+ * Typically calls {@link runAgents} or {@link useAgentPool} inside.
21
+ * @returns The body's return value
22
+ *
23
+ * @example Cold-path research with shared prefix
24
+ * ```typescript
25
+ * const { result, prefixLen } = yield* withSharedRoot(
26
+ * { systemPrompt: RESEARCH_PROMPT, tools: toolsJson },
27
+ * function*(root, prefixLen) {
28
+ * const result = yield* runAgents({
29
+ * tasks: questions.map(q => ({
30
+ * systemPrompt: RESEARCH_PROMPT,
31
+ * content: q,
32
+ * tools: toolsJson,
33
+ * parent: root,
34
+ * })),
35
+ * tools: toolMap,
36
+ * });
37
+ * return { result, prefixLen };
38
+ * },
39
+ * );
40
+ * ```
41
+ *
42
+ * @category Agents
43
+ */
44
+ function* withSharedRoot(opts, body) {
45
+ const ctx = yield* context_1.Ctx.expect();
46
+ const messages = [{ role: 'system', content: opts.systemPrompt }];
47
+ const fmtOpts = opts.tools
48
+ ? { tools: opts.tools, addGenerationPrompt: false }
49
+ : { addGenerationPrompt: false };
50
+ const fmt = ctx.formatChatSync(JSON.stringify(messages), fmtOpts);
51
+ const sharedTokens = ctx.tokenizeSync(fmt.prompt);
52
+ const root = sdk_1.Branch.create(ctx, 0, opts.params ?? { temperature: 0.5 });
53
+ yield* (0, effection_1.call)(() => root.prefill(sharedTokens));
54
+ try {
55
+ return yield* body(root, sharedTokens.length);
56
+ }
57
+ finally {
58
+ if (!root.disposed)
59
+ root.pruneSubtreeSync();
60
+ }
61
+ }
62
+ //# sourceMappingURL=shared-root.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-root.js","sourceRoot":"","sources":["../src/shared-root.ts"],"names":[],"mappings":";;AA0DA,wCAqBC;AA/ED,yCAAiC;AAEjC,0CAA0C;AAE1C,uCAAgC;AAiBhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,QAAe,CAAC,CAAC,cAAc,CAC7B,IAAuB,EACvB,IAAgE;IAEhE,MAAM,GAAG,GAAmB,KAAK,CAAC,CAAC,aAAG,CAAC,MAAM,EAAE,CAAC;IAEhD,MAAM,QAAQ,GAAG,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC;IAClE,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK;QACxB,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE;QACnD,CAAC,CAAC,EAAE,mBAAmB,EAAE,KAAK,EAAE,CAAC;IACnC,MAAM,GAAG,GAAG,GAAG,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,CAAC;IAClE,MAAM,YAAY,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAElD,MAAM,IAAI,GAAG,YAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,MAAM,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;IACxE,KAAK,CAAC,CAAC,IAAA,gBAAI,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC;IAE9C,IAAI,CAAC;QACH,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC;YAAS,CAAC;QACT,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,IAAI,CAAC,gBAAgB,EAAE,CAAC;IAC9C,CAAC;AACH,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { Tool } from './Tool';
2
+ /**
3
+ * Aggregated tool registry for agent pool consumption
4
+ *
5
+ * Contains the `toolMap` for dispatch and `toolsJson` for prompt
6
+ * formatting. Created by {@link createToolkit}.
7
+ *
8
+ * @category Agents
9
+ */
10
+ export interface Toolkit {
11
+ /** Name-to-instance map used by {@link useAgentPool} for tool dispatch */
12
+ toolMap: Map<string, Tool>;
13
+ /** JSON-serialized tool schemas passed to `formatChat()` via task specs */
14
+ toolsJson: string;
15
+ }
16
+ /**
17
+ * Aggregate an array of {@link Tool} instances into a toolkit
18
+ *
19
+ * Builds both the dispatch map and the JSON schema string from the
20
+ * tool array. Pass the result directly to {@link AgentPoolOptions}
21
+ * and {@link AgentTaskSpec}.
22
+ *
23
+ * @param tools - Tool instances to aggregate
24
+ * @returns Toolkit with `toolMap` and `toolsJson`
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const { toolMap, toolsJson } = createToolkit([
29
+ * new SearchTool(chunks, reranker),
30
+ * new ReadFileTool(resources),
31
+ * new GrepTool(resources),
32
+ * ]);
33
+ * ```
34
+ *
35
+ * @category Agents
36
+ */
37
+ export declare function createToolkit(tools: Tool[]): Toolkit;
38
+ //# sourceMappingURL=toolkit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolkit.d.ts","sourceRoot":"","sources":["../src/toolkit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC;;;;;;;GAOG;AACH,MAAM,WAAW,OAAO;IACtB,0EAA0E;IAC1E,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC3B,2EAA2E;IAC3E,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,OAAO,CAKpD"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createToolkit = createToolkit;
4
+ /**
5
+ * Aggregate an array of {@link Tool} instances into a toolkit
6
+ *
7
+ * Builds both the dispatch map and the JSON schema string from the
8
+ * tool array. Pass the result directly to {@link AgentPoolOptions}
9
+ * and {@link AgentTaskSpec}.
10
+ *
11
+ * @param tools - Tool instances to aggregate
12
+ * @returns Toolkit with `toolMap` and `toolsJson`
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const { toolMap, toolsJson } = createToolkit([
17
+ * new SearchTool(chunks, reranker),
18
+ * new ReadFileTool(resources),
19
+ * new GrepTool(resources),
20
+ * ]);
21
+ * ```
22
+ *
23
+ * @category Agents
24
+ */
25
+ function createToolkit(tools) {
26
+ return {
27
+ toolMap: new Map(tools.map(t => [t.name, t])),
28
+ toolsJson: JSON.stringify(tools.map(t => t.schema)),
29
+ };
30
+ }
31
+ //# sourceMappingURL=toolkit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolkit.js","sourceRoot":"","sources":["../src/toolkit.ts"],"names":[],"mappings":";;AAsCA,sCAKC;AA1BD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,aAAa,CAAC,KAAa;IACzC,OAAO;QACL,OAAO,EAAE,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QAC7C,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;KACpD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,380 @@
1
+ import type { Branch } from '@lloyal-labs/sdk';
2
+ /**
3
+ * JSON Schema definition for tool parameter validation
4
+ *
5
+ * Describes the shape of arguments a {@link Tool} accepts. Passed to the
6
+ * model via `formatChat()` so it can generate valid tool-call arguments.
7
+ *
8
+ * @category Agents
9
+ */
10
+ export interface JsonSchema {
11
+ /** JSON Schema type (e.g. `"object"`, `"string"`, `"array"`) */
12
+ type: string;
13
+ /** Property definitions when `type` is `"object"` */
14
+ properties?: Record<string, unknown>;
15
+ /** Required property names when `type` is `"object"` */
16
+ required?: string[];
17
+ /** Additional schema constraints (minItems, enum, etc.) */
18
+ [key: string]: unknown;
19
+ }
20
+ /**
21
+ * OpenAI-compatible function tool schema
22
+ *
23
+ * The wrapper format expected by `formatChat()` when passing tools to the
24
+ * model. {@link Tool.schema} generates this automatically from the tool's
25
+ * `name`, `description`, and `parameters`.
26
+ *
27
+ * @category Agents
28
+ */
29
+ export interface ToolSchema {
30
+ /** Always `"function"` for function-calling tools */
31
+ type: 'function';
32
+ /** Function definition containing name, description, and parameter schema */
33
+ function: {
34
+ /** Tool name — used as the function identifier in tool calls */
35
+ name: string;
36
+ /** Human-readable description shown to the model */
37
+ description: string;
38
+ /** JSON Schema describing the tool's arguments */
39
+ parameters: JsonSchema;
40
+ };
41
+ }
42
+ /**
43
+ * Execution context passed to {@link Tool.execute}
44
+ *
45
+ * Provides callbacks for reporting progress during long-running tool
46
+ * operations (e.g. reranker scoring chunks).
47
+ *
48
+ * @category Agents
49
+ */
50
+ export interface ToolContext {
51
+ /** Progress callback for long-running operations */
52
+ onProgress?: (p: {
53
+ filled: number;
54
+ total: number;
55
+ }) => void;
56
+ }
57
+ /**
58
+ * Per-token trace entry captured when {@link AgentPoolOptions.trace} is true
59
+ *
60
+ * Each entry corresponds to one sampled token and the distribution state
61
+ * at the moment it was drawn. Available on {@link AgentResult.trace} after
62
+ * pool completion.
63
+ *
64
+ * @category Agents
65
+ */
66
+ export interface TraceToken {
67
+ /** Decoded text for this token */
68
+ text: string;
69
+ /** Shannon entropy of the full vocabulary distribution (bits, base-2) */
70
+ entropy: number;
71
+ /** Surprisal of the chosen token: -log2(p) */
72
+ surprisal: number;
73
+ }
74
+ /**
75
+ * Task specification for a single agent in {@link useAgentPool}
76
+ *
77
+ * Each task defines the agent's system prompt, user content, available
78
+ * tools, and parent branch to fork from. The parent branch determines
79
+ * the agent's KV prefix — fork from a shared root to amortize system
80
+ * prompt tokenization across agents.
81
+ *
82
+ * @category Agents
83
+ */
84
+ export interface AgentTaskSpec {
85
+ /** System prompt defining the agent's role and behavior */
86
+ systemPrompt: string;
87
+ /** User message content — the agent's specific sub-question or task */
88
+ content: string;
89
+ /** JSON-serialized tool schemas (from {@link createToolkit}) */
90
+ tools?: string;
91
+ /** PRNG seed for sampler diversity — pass different seeds per agent */
92
+ seed?: number;
93
+ /** Parent branch to fork from (required by {@link useAgentPool}) */
94
+ parent?: Branch;
95
+ }
96
+ /**
97
+ * Sampling parameters for generation
98
+ *
99
+ * Controls the sampler chain applied during token generation. Passed to
100
+ * {@link Branch.create}, {@link generate}, {@link diverge}, and agent
101
+ * pool tasks.
102
+ *
103
+ * @category Agents
104
+ */
105
+ export interface SamplingParams {
106
+ /** Temperature for softmax scaling (0 = greedy, higher = more random) */
107
+ temperature?: number;
108
+ /** Nucleus sampling threshold — cumulative probability cutoff */
109
+ topP?: number;
110
+ /** Top-K sampling — keep only the K most likely tokens */
111
+ topK?: number;
112
+ /** Minimum probability threshold relative to the most likely token */
113
+ minP?: number;
114
+ /** Additional sampler-specific parameters */
115
+ [key: string]: unknown;
116
+ }
117
+ /**
118
+ * KV pressure thresholds controlling agent shutdown under context exhaustion
119
+ *
120
+ * Two thresholds govern what happens as remaining KV shrinks:
121
+ *
122
+ * **softLimit** (default 1024) — remaining KV floor for new work.
123
+ * Enforced at three points:
124
+ * - **SETTLE**: tool results that would cross this floor are rejected and
125
+ * the agent is marked done. This is the primary enforcement point — tool
126
+ * results (search results, etc.) are the largest KV consumers.
127
+ * - **PRODUCE (stop-token boundary)**: agents that want a non-terminal tool
128
+ * call are hard-cut. Terminal tools (e.g. `report()`) still pass.
129
+ * - **INIT prefill**: agents that don't fit above this floor are dropped.
130
+ *
131
+ * Set to account for downstream pool needs (reporters, verification).
132
+ *
133
+ * **hardLimit** (default 128) — crash-prevention floor.
134
+ * When remaining drops below this, agents are killed immediately before
135
+ * `produceSync()`. Prevents `llama_decode` "no memory slot" failures.
136
+ * Pure safety net — should never be the primary budget control.
137
+ *
138
+ * @category Agents
139
+ */
140
+ export interface PressureThresholds {
141
+ /**
142
+ * Remaining KV floor for new work (tokens). When remaining drops below
143
+ * this, SETTLE rejects tool results, PRODUCE hard-cuts non-terminal tool
144
+ * calls, and INIT drops agents that don't fit.
145
+ *
146
+ * Set to account for downstream pool needs (reporters, verification).
147
+ * Default: 1024
148
+ */
149
+ softLimit?: number;
150
+ /**
151
+ * Crash-prevention floor (tokens). When remaining drops below this,
152
+ * agents are killed immediately before `produceSync()`. Prevents
153
+ * `llama_decode` "no memory slot for batch" failures.
154
+ * Default: 128
155
+ */
156
+ hardLimit?: number;
157
+ }
158
+ /**
159
+ * Configuration for {@link useAgentPool} and {@link runAgents}
160
+ *
161
+ * @category Agents
162
+ */
163
+ export interface AgentPoolOptions {
164
+ /** Agent task specifications — one per concurrent agent */
165
+ tasks: AgentTaskSpec[];
166
+ /**
167
+ * Tool registry mapping tool names to {@link Tool} instances.
168
+ *
169
+ * This is the **execution registry** — it determines which tools can be
170
+ * dispatched at runtime. It is distinct from the per-task `task.tools`
171
+ * JSON schema that tells the model which tools are available.
172
+ *
173
+ * The registry also controls {@link AgentPoolOptions.terminalTool | terminalTool}
174
+ * gating: if the registry contains only the terminal tool, agents are
175
+ * allowed to call it as their first action (e.g. reporter sub-agents).
176
+ * If the registry contains other tools, the first call must be
177
+ * non-terminal to prevent agents from reporting without doing work.
178
+ */
179
+ tools: Map<string, import('./Tool').Tool>;
180
+ /** Sampling parameters applied to all agents */
181
+ params?: SamplingParams;
182
+ /** Maximum tool-call turns per agent before forced termination */
183
+ maxTurns?: number;
184
+ /** Tool name that signals agent completion. When the model calls this tool,
185
+ * findings are extracted from arguments and the agent is marked done.
186
+ * The tool is intercepted — never dispatched to execute(). If omitted,
187
+ * agents complete only via stop token or hard-cut. */
188
+ terminalTool?: string;
189
+ /** Enable per-token entropy/surprisal on `agent:produce` events */
190
+ trace?: boolean;
191
+ /** KV pressure thresholds — tune per pool. Reporter pools typically use
192
+ * lower thresholds than research pools since they complete in a single
193
+ * terminal tool call. See {@link PressureThresholds} for tuning guidance. */
194
+ pressure?: PressureThresholds;
195
+ }
196
+ /**
197
+ * Result for a single completed agent
198
+ *
199
+ * @category Agents
200
+ */
201
+ export interface AgentResult {
202
+ /** Stable agent identifier (branch handle at creation time) */
203
+ agentId: number;
204
+ /** Parent branch handle — shared root for top-level agents, parent agentId for sub-agents */
205
+ parentAgentId: number;
206
+ /** The agent's branch — still alive when returned from {@link useAgentPool} */
207
+ branch: Branch;
208
+ /** Agent's research findings (from terminal tool or final output), or null */
209
+ findings: string | null;
210
+ /** Number of tool calls the agent made */
211
+ toolCallCount: number;
212
+ /** Total tokens generated by this agent */
213
+ tokenCount: number;
214
+ /** Model-level perplexity at completion (exp of mean NLL from raw logits) */
215
+ ppl: number;
216
+ /** Sampling-level perplexity at completion (from filtered distribution) */
217
+ samplingPpl: number;
218
+ /** Per-token trace data (present only when {@link AgentPoolOptions.trace} is true) */
219
+ trace?: TraceToken[];
220
+ }
221
+ /**
222
+ * Aggregate result from a completed agent pool run
223
+ *
224
+ * Returned by both {@link useAgentPool} and {@link runAgents}. Contains
225
+ * per-agent results plus aggregate statistics for display and telemetry.
226
+ *
227
+ * @category Agents
228
+ */
229
+ export interface AgentPoolResult {
230
+ /** Per-agent results in task order */
231
+ agents: AgentResult[];
232
+ /** Sum of all agent token counts */
233
+ totalTokens: number;
234
+ /** Sum of all agent tool calls */
235
+ totalToolCalls: number;
236
+ /** Number of batched commit steps in the tick loop */
237
+ steps: number;
238
+ /** Internal performance counters for telemetry */
239
+ counters: {
240
+ /** Number of batch prefill calls for tool result injection */
241
+ warmPrefillCalls: number;
242
+ /** Total branches across all warm prefill batches */
243
+ warmPrefillBranches: number;
244
+ /** Ticks where no agent was generating (all awaiting tools) */
245
+ stalledTicks: number;
246
+ /** Peak concurrent tool executions */
247
+ maxConcurrentTools: number;
248
+ /** Ticks spent idle-waiting via action() */
249
+ idleTicks: number;
250
+ };
251
+ }
252
+ /**
253
+ * Options for single-branch {@link generate}
254
+ *
255
+ * @category Agents
256
+ */
257
+ export interface GenerateOptions {
258
+ /** Pre-formatted prompt string (from `formatChat()` + `tokenize()`) */
259
+ prompt: string;
260
+ /** GBNF grammar string for constrained generation */
261
+ grammar?: string;
262
+ /** Sampling parameters */
263
+ params?: SamplingParams;
264
+ /** Optional parser applied to the raw output string */
265
+ parse?: (output: string) => unknown;
266
+ }
267
+ /**
268
+ * Result from single-branch {@link generate}
269
+ *
270
+ * @category Agents
271
+ */
272
+ export interface GenerateResult<T = unknown> {
273
+ /** Raw generated text */
274
+ output: string;
275
+ /** Number of tokens generated */
276
+ tokenCount: number;
277
+ /** Parsed output (present only when `parse` was provided in options) */
278
+ parsed?: T;
279
+ }
280
+ /**
281
+ * Options for multi-branch {@link diverge}
282
+ *
283
+ * Either `parent` or `prompt` must be provided. When `parent` is given,
284
+ * branches fork from it and no new root is created. When only `prompt`
285
+ * is given, a fresh root is created, prefilled, and cleaned up on error.
286
+ *
287
+ * @category Agents
288
+ */
289
+ export interface DivergeOptions {
290
+ /** Pre-formatted prompt for creating a fresh root (mutually exclusive with parent) */
291
+ prompt?: string;
292
+ /** Number of parallel generation attempts */
293
+ attempts: number;
294
+ /** Parent branch to fork from (mutually exclusive with prompt) */
295
+ parent?: Branch;
296
+ /** Sampling parameters for all attempts */
297
+ params?: SamplingParams;
298
+ }
299
+ /**
300
+ * Single attempt result from {@link diverge}
301
+ *
302
+ * @category Agents
303
+ */
304
+ export interface DivergeAttempt {
305
+ /** The attempt's branch (only the best branch survives after diverge) */
306
+ branch: Branch;
307
+ /** Generated text for this attempt */
308
+ output: string;
309
+ /** Number of tokens generated */
310
+ tokenCount: number;
311
+ /** Model perplexity — lower indicates more coherent generation */
312
+ ppl: number;
313
+ }
314
+ /**
315
+ * Aggregate result from {@link diverge}
316
+ *
317
+ * The `best` branch is still alive; all other attempt branches have been
318
+ * pruned. The caller owns cleanup — typically via {@link Session.promote}
319
+ * to make the best branch the new conversation trunk.
320
+ *
321
+ * @category Agents
322
+ */
323
+ export interface DivergeResult {
324
+ /** Lowest-perplexity branch — still alive, caller owns cleanup */
325
+ best: Branch;
326
+ /** Text output from the best attempt */
327
+ bestOutput: string;
328
+ /** All attempts (losers already pruned, branches disposed) */
329
+ attempts: DivergeAttempt[];
330
+ /** Sum of all attempt token counts */
331
+ totalTokens: number;
332
+ /** Number of batched commit steps */
333
+ steps: number;
334
+ /** Shared prefix length in tokens (for KV savings calculation) */
335
+ prefixLength: number;
336
+ }
337
+ /**
338
+ * Events emitted by the runtime during agent pool execution
339
+ *
340
+ * Subscribe to these via the `events` channel from {@link initAgents}.
341
+ * Harnesses can extend this union with phase-level events for display.
342
+ *
343
+ * @category Agents
344
+ */
345
+ export type AgentEvent = {
346
+ type: 'agent:spawn';
347
+ agentId: number;
348
+ parentAgentId: number;
349
+ } | {
350
+ type: 'agent:produce';
351
+ agentId: number;
352
+ text: string;
353
+ tokenCount: number;
354
+ entropy?: number;
355
+ surprisal?: number;
356
+ } | {
357
+ type: 'agent:tool_call';
358
+ agentId: number;
359
+ tool: string;
360
+ args: string;
361
+ } | {
362
+ type: 'agent:tool_result';
363
+ agentId: number;
364
+ tool: string;
365
+ result: string;
366
+ } | {
367
+ type: 'agent:tool_progress';
368
+ agentId: number;
369
+ tool: string;
370
+ filled: number;
371
+ total: number;
372
+ } | {
373
+ type: 'agent:report';
374
+ agentId: number;
375
+ findings: string;
376
+ } | {
377
+ type: 'agent:done';
378
+ agentId: number;
379
+ };
380
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAK/C;;;;;;;GAOG;AACH,MAAM,WAAW,UAAU;IACzB,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAC;IACb,qDAAqD;IACrD,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,2DAA2D;IAC3D,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,UAAU;IACzB,qDAAqD;IACrD,IAAI,EAAE,UAAU,CAAC;IACjB,6EAA6E;IAC7E,QAAQ,EAAE;QACR,gEAAgE;QAChE,IAAI,EAAE,MAAM,CAAC;QACb,oDAAoD;QACpD,WAAW,EAAE,MAAM,CAAC;QACpB,kDAAkD;QAClD,UAAU,EAAE,UAAU,CAAC;KACxB,CAAC;CACH;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,WAAW;IAC1B,oDAAoD;IACpD,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;CAC7D;AAID;;;;;;;;GAQG;AACH,MAAM,WAAW,UAAU;IACzB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,yEAAyE;IACzE,OAAO,EAAE,MAAM,CAAC;IAChB,8CAA8C;IAC9C,SAAS,EAAE,MAAM,CAAC;CACnB;AAID;;;;;;;;;GASG;AAC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AAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GAClH;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACxE;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC5E;IAAE,IAAI,EAAE,qBAAqB,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC7F;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC3D;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@lloyal-labs/lloyal-agents",
3
+ "version": "1.0.0",
4
+ "description": "Multi-agent inference inside the decode loop — structured concurrency over shared KV state",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/lloyal-ai/sdk.git",
13
+ "directory": "packages/agents"
14
+ },
15
+ "homepage": "https://github.com/lloyal-ai/sdk/tree/main/packages/agents#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/lloyal-ai/sdk/issues"
18
+ },
19
+ "keywords": [
20
+ "llm",
21
+ "agents",
22
+ "multi-agent",
23
+ "inference",
24
+ "structured-concurrency",
25
+ "effection",
26
+ "llama.cpp"
27
+ ],
28
+ "license": "Apache-2.0",
29
+ "type": "commonjs",
30
+ "scripts": {
31
+ "build": "tsc -b"
32
+ },
33
+ "dependencies": {
34
+ "@lloyal-labs/sdk": "*",
35
+ "effection": "^4.0.2"
36
+ },
37
+ "files": [
38
+ "dist/"
39
+ ]
40
+ }