@lloyal-labs/lloyal-agents 1.5.6 → 1.7.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/dist/Agent.d.ts +34 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +65 -1
- package/dist/Agent.js.map +1 -1
- package/dist/AgentPolicy.d.ts +51 -8
- package/dist/AgentPolicy.d.ts.map +1 -1
- package/dist/AgentPolicy.js +105 -63
- package/dist/AgentPolicy.js.map +1 -1
- package/dist/Tool.d.ts +5 -7
- package/dist/Tool.d.ts.map +1 -1
- package/dist/Tool.js +5 -7
- package/dist/Tool.js.map +1 -1
- package/dist/agent-pool.d.ts +9 -3
- package/dist/agent-pool.d.ts.map +1 -1
- package/dist/agent-pool.js +446 -407
- package/dist/agent-pool.js.map +1 -1
- package/dist/combinators.d.ts +29 -0
- package/dist/combinators.d.ts.map +1 -0
- package/dist/combinators.js +37 -0
- package/dist/combinators.js.map +1 -0
- package/dist/create-agent-pool.d.ts +78 -0
- package/dist/create-agent-pool.d.ts.map +1 -0
- package/dist/create-agent-pool.js +60 -0
- package/dist/create-agent-pool.js.map +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js.map +1 -1
- package/dist/trace-types.d.ts +4 -2
- package/dist/trace-types.d.ts.map +1 -1
- package/dist/trace-writer.d.ts +4 -1
- package/dist/trace-writer.d.ts.map +1 -1
- package/dist/trace-writer.js +6 -2
- package/dist/trace-writer.js.map +1 -1
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/use-agent.d.ts +92 -0
- package/dist/use-agent.d.ts.map +1 -0
- package/dist/use-agent.js +131 -0
- package/dist/use-agent.js.map +1 -0
- package/package.json +2 -2
- package/dist/generate.d.ts +0 -77
- package/dist/generate.d.ts.map +0 -1
- package/dist/generate.js +0 -166
- package/dist/generate.js.map +0 -1
- package/dist/run-agents.d.ts +0 -39
- package/dist/run-agents.d.ts.map +0 -1
- package/dist/run-agents.js +0 -46
- package/dist/run-agents.js.map +0 -1
- package/dist/spawn-agents.d.ts +0 -104
- package/dist/spawn-agents.d.ts.map +0 -1
- package/dist/spawn-agents.js +0 -255
- package/dist/spawn-agents.js.map +0 -1
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAIhD;;;;;;;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,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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,CAAC;IAAC,uBAAuB,CAAC,EAAE,MAAM,CAAA;CAAE,GAC9G;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,MAAM,EAAE,MAAM,CAAA;CAAE,GACzD;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACvC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Operation } from 'effection';
|
|
2
|
+
import { Branch } from '@lloyal-labs/sdk';
|
|
3
|
+
import type { Session } from '@lloyal-labs/sdk';
|
|
4
|
+
import { Agent } from './Agent';
|
|
5
|
+
import type { Tool } from './Tool';
|
|
6
|
+
import type { AgentPolicy } from './AgentPolicy';
|
|
7
|
+
import type { JsonSchema, SamplingParams } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* Options for {@link useAgent} and {@link agent}.
|
|
10
|
+
*
|
|
11
|
+
* @category Agents
|
|
12
|
+
*/
|
|
13
|
+
export interface UseAgentOpts {
|
|
14
|
+
/** System prompt defining the agent's role and behavior. */
|
|
15
|
+
systemPrompt: string;
|
|
16
|
+
/** User message content — the agent's task. */
|
|
17
|
+
task: string;
|
|
18
|
+
/** Tools available to the agent. Optional — pool degenerates cleanly without tools. */
|
|
19
|
+
tools?: Tool[];
|
|
20
|
+
/** Terminal tool name — tool must be in the tools array. */
|
|
21
|
+
terminalTool?: string;
|
|
22
|
+
/** Max tool-use turns before hard cut. @default 100 */
|
|
23
|
+
maxTurns?: number;
|
|
24
|
+
/** JSON Schema for eager grammar constraint (deferred: Zod support). */
|
|
25
|
+
schema?: JsonSchema;
|
|
26
|
+
/** Sampling parameters. */
|
|
27
|
+
params?: SamplingParams;
|
|
28
|
+
/** Explicit parent branch for warm path (Continuous Context). */
|
|
29
|
+
parent?: Branch;
|
|
30
|
+
/** Session for warm path via trunk. */
|
|
31
|
+
session?: Session;
|
|
32
|
+
/** Custom agent policy. */
|
|
33
|
+
policy?: AgentPolicy;
|
|
34
|
+
/** Enable structured trace events. */
|
|
35
|
+
trace?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Single-agent resource — delegates to {@link useAgentPool} N=1.
|
|
39
|
+
*
|
|
40
|
+
* One path. No conditional. Tools optional — `useAgentPool` with no tools
|
|
41
|
+
* degenerates cleanly (tick loop: produce → commit → stop, no dispatch).
|
|
42
|
+
*
|
|
43
|
+
* Provides a completed Agent with branches alive. The resource keeps the
|
|
44
|
+
* root and agent branch alive until the caller's scope exits — caller can
|
|
45
|
+
* fork from the Agent's branch for verification or follow-up.
|
|
46
|
+
*
|
|
47
|
+
* Root managed via `ensure()` (not `withSharedRoot`) because the resource
|
|
48
|
+
* lifetime requires the root alive until the caller's scope exits.
|
|
49
|
+
*
|
|
50
|
+
* Events stream passively to the broadcast Channel during the inline drain.
|
|
51
|
+
*
|
|
52
|
+
* @param opts - Agent configuration
|
|
53
|
+
* @returns Agent with result populated, branches alive
|
|
54
|
+
*
|
|
55
|
+
* @example Single agent with tools
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const agent = yield* useAgent({
|
|
58
|
+
* systemPrompt: "You are a research assistant.",
|
|
59
|
+
* task: "Find information about X",
|
|
60
|
+
* tools: [searchTool, reportTool],
|
|
61
|
+
* terminalTool: 'report',
|
|
62
|
+
* });
|
|
63
|
+
* // agent.result — findings
|
|
64
|
+
* // agent.branch — alive, can fork from
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @category Agents
|
|
68
|
+
*/
|
|
69
|
+
export declare function useAgent(opts: UseAgentOpts): Operation<Agent>;
|
|
70
|
+
/**
|
|
71
|
+
* Single-agent scoped operation — wraps {@link useAgent} in `scoped()`.
|
|
72
|
+
*
|
|
73
|
+
* Returns completed Agent with result populated. Branches pruned on scope exit.
|
|
74
|
+
* This is the harness-level API for single-agent steps (plan, eval, bridge).
|
|
75
|
+
*
|
|
76
|
+
* @param opts - Agent configuration (same as {@link UseAgentOpts})
|
|
77
|
+
* @returns Completed Agent with `.result`, `.rawOutput`, `.tokenCount`
|
|
78
|
+
*
|
|
79
|
+
* @example Plan step
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const a = yield* agent({
|
|
82
|
+
* systemPrompt: PLAN.system,
|
|
83
|
+
* task: query,
|
|
84
|
+
* schema: planSchema,
|
|
85
|
+
* });
|
|
86
|
+
* const plan = JSON.parse(a.rawOutput);
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @category Agents
|
|
90
|
+
*/
|
|
91
|
+
export declare function agent(opts: UseAgentOpts): Operation<Agent>;
|
|
92
|
+
//# sourceMappingURL=use-agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-agent.d.ts","sourceRoot":"","sources":["../src/use-agent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,EAAE,OAAO,EAAkB,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAKhC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAc,MAAM,SAAS,CAAC;AAEtE;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,4DAA4D;IAC5D,YAAY,EAAE,MAAM,CAAC;IACrB,+CAA+C;IAC/C,IAAI,EAAE,MAAM,CAAC;IACb,uFAAuF;IACvF,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;IACf,4DAA4D;IAC5D,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uDAAuD;IACvD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wEAAwE;IACxE,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,2BAA2B;IAC3B,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uCAAuC;IACvC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,2BAA2B;IAC3B,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,sCAAsC;IACtC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,SAAS,CAAC,KAAK,CAAC,CAoE7D;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAiB,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,SAAS,CAAC,KAAK,CAAC,CAI3D"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAgent = useAgent;
|
|
4
|
+
exports.agent = agent;
|
|
5
|
+
const effection_1 = require("effection");
|
|
6
|
+
const sdk_1 = require("@lloyal-labs/sdk");
|
|
7
|
+
const context_1 = require("./context");
|
|
8
|
+
const agent_pool_1 = require("./agent-pool");
|
|
9
|
+
const toolkit_1 = require("./toolkit");
|
|
10
|
+
const trace_scope_1 = require("./trace-scope");
|
|
11
|
+
/**
|
|
12
|
+
* Single-agent resource — delegates to {@link useAgentPool} N=1.
|
|
13
|
+
*
|
|
14
|
+
* One path. No conditional. Tools optional — `useAgentPool` with no tools
|
|
15
|
+
* degenerates cleanly (tick loop: produce → commit → stop, no dispatch).
|
|
16
|
+
*
|
|
17
|
+
* Provides a completed Agent with branches alive. The resource keeps the
|
|
18
|
+
* root and agent branch alive until the caller's scope exits — caller can
|
|
19
|
+
* fork from the Agent's branch for verification or follow-up.
|
|
20
|
+
*
|
|
21
|
+
* Root managed via `ensure()` (not `withSharedRoot`) because the resource
|
|
22
|
+
* lifetime requires the root alive until the caller's scope exits.
|
|
23
|
+
*
|
|
24
|
+
* Events stream passively to the broadcast Channel during the inline drain.
|
|
25
|
+
*
|
|
26
|
+
* @param opts - Agent configuration
|
|
27
|
+
* @returns Agent with result populated, branches alive
|
|
28
|
+
*
|
|
29
|
+
* @example Single agent with tools
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const agent = yield* useAgent({
|
|
32
|
+
* systemPrompt: "You are a research assistant.",
|
|
33
|
+
* task: "Find information about X",
|
|
34
|
+
* tools: [searchTool, reportTool],
|
|
35
|
+
* terminalTool: 'report',
|
|
36
|
+
* });
|
|
37
|
+
* // agent.result — findings
|
|
38
|
+
* // agent.branch — alive, can fork from
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @category Agents
|
|
42
|
+
*/
|
|
43
|
+
function useAgent(opts) {
|
|
44
|
+
return (0, effection_1.resource)(function* (provide) {
|
|
45
|
+
const ctx = yield* context_1.Ctx.expect();
|
|
46
|
+
const broadcast = yield* context_1.Events.expect();
|
|
47
|
+
const tw = yield* context_1.Trace.expect();
|
|
48
|
+
const toolkit = (0, toolkit_1.createToolkit)(opts.tools ?? []);
|
|
49
|
+
const warmParent = opts.parent ?? opts.session?.trunk ?? undefined;
|
|
50
|
+
const scope = (0, trace_scope_1.traceScope)(tw, null, 'useAgent', {
|
|
51
|
+
hasTools: !!(opts.tools?.length),
|
|
52
|
+
hasParent: !!warmParent,
|
|
53
|
+
});
|
|
54
|
+
// Format shared prefix
|
|
55
|
+
const messages = [{ role: 'system', content: opts.systemPrompt }];
|
|
56
|
+
const fmtOpts = { addGenerationPrompt: false, enableThinking: false };
|
|
57
|
+
if (opts.tools?.length)
|
|
58
|
+
fmtOpts.tools = toolkit.toolsJson;
|
|
59
|
+
const fmt = ctx.formatChatSync(JSON.stringify(messages), fmtOpts);
|
|
60
|
+
const sharedTokens = ctx.tokenizeSync(fmt.prompt);
|
|
61
|
+
// Create root — ensure() for resource lifetime (not withSharedRoot's try/finally)
|
|
62
|
+
const root = warmParent
|
|
63
|
+
? warmParent.forkSync()
|
|
64
|
+
: sdk_1.Branch.create(ctx, 0, opts.params ?? { temperature: 0.5 });
|
|
65
|
+
yield* (0, effection_1.ensure)(() => { if (!root.disposed)
|
|
66
|
+
root.pruneSubtreeSync(); });
|
|
67
|
+
const prefillTokens = warmParent ? ctx.getTurnSeparator() : sharedTokens;
|
|
68
|
+
yield* (0, effection_1.call)(() => root.prefill(prefillTokens));
|
|
69
|
+
// Eager grammar from schema — set on root before fork.
|
|
70
|
+
// Fork inherits grammar state. formatChatSync returns no grammar for
|
|
71
|
+
// no-tools case, so applyLazyGrammar is a no-op and the inherited
|
|
72
|
+
// eager grammar persists on the forked agent branch.
|
|
73
|
+
if (opts.schema) {
|
|
74
|
+
const grammar = yield* (0, effection_1.call)(() => ctx.jsonSchemaToGrammar(JSON.stringify(opts.schema)));
|
|
75
|
+
root.setGrammar(grammar);
|
|
76
|
+
}
|
|
77
|
+
// Delegate to useAgentPool N=1
|
|
78
|
+
const hasTools = !!(opts.tools?.length);
|
|
79
|
+
const sub = yield* (0, agent_pool_1.useAgentPool)({
|
|
80
|
+
tasks: [{
|
|
81
|
+
systemPrompt: opts.systemPrompt,
|
|
82
|
+
content: opts.task,
|
|
83
|
+
tools: hasTools ? toolkit.toolsJson : undefined,
|
|
84
|
+
parent: root,
|
|
85
|
+
}],
|
|
86
|
+
tools: toolkit.toolMap,
|
|
87
|
+
terminalTool: opts.terminalTool,
|
|
88
|
+
maxTurns: opts.maxTurns,
|
|
89
|
+
policy: opts.policy,
|
|
90
|
+
trace: opts.trace,
|
|
91
|
+
});
|
|
92
|
+
// Drain Subscription inline — forward to broadcast
|
|
93
|
+
let next = yield* sub.next();
|
|
94
|
+
while (!next.done) {
|
|
95
|
+
yield* broadcast.send(next.value);
|
|
96
|
+
next = yield* sub.next();
|
|
97
|
+
}
|
|
98
|
+
const pool = next.value;
|
|
99
|
+
scope.close();
|
|
100
|
+
yield* provide(pool.agents[0].agent);
|
|
101
|
+
// Resource stays alive — branch alive for caller to fork from
|
|
102
|
+
// ensure() prunes root on scope exit
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Single-agent scoped operation — wraps {@link useAgent} in `scoped()`.
|
|
107
|
+
*
|
|
108
|
+
* Returns completed Agent with result populated. Branches pruned on scope exit.
|
|
109
|
+
* This is the harness-level API for single-agent steps (plan, eval, bridge).
|
|
110
|
+
*
|
|
111
|
+
* @param opts - Agent configuration (same as {@link UseAgentOpts})
|
|
112
|
+
* @returns Completed Agent with `.result`, `.rawOutput`, `.tokenCount`
|
|
113
|
+
*
|
|
114
|
+
* @example Plan step
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const a = yield* agent({
|
|
117
|
+
* systemPrompt: PLAN.system,
|
|
118
|
+
* task: query,
|
|
119
|
+
* schema: planSchema,
|
|
120
|
+
* });
|
|
121
|
+
* const plan = JSON.parse(a.rawOutput);
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @category Agents
|
|
125
|
+
*/
|
|
126
|
+
function* agent(opts) {
|
|
127
|
+
return yield* (0, effection_1.scoped)(function* () {
|
|
128
|
+
return yield* useAgent(op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AC,CAAC,IAAA,kBAAM,EAAC,QAAQ,CAAC;QAC5B,OAAO,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lloyal-labs/lloyal-agents",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Multi-agent inference inside the decode loop — structured concurrency over shared KV state",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"build": "tsc -b"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@lloyal-labs/sdk": "^1.
|
|
34
|
+
"@lloyal-labs/sdk": "^1.7.0",
|
|
35
35
|
"effection": "^4.0.2",
|
|
36
36
|
"eta": "^4.5.1"
|
|
37
37
|
},
|
package/dist/generate.d.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import type { Operation } from 'effection';
|
|
2
|
-
import { Branch } from '@lloyal-labs/sdk';
|
|
3
|
-
import type { GenerateOptions, GenerateResult } from './types';
|
|
4
|
-
/**
|
|
5
|
-
* Prepare a branch for generation — create/fork, set grammar, prefill prompt
|
|
6
|
-
*
|
|
7
|
-
* Returns the prepared Branch ready for token production. The caller owns the
|
|
8
|
-
* branch and decides how to consume it:
|
|
9
|
-
*
|
|
10
|
-
* - **Manual loop** — call `produceSync()` / `commit()` for per-token control,
|
|
11
|
-
* streaming UI updates, or integration into a batched tick loop
|
|
12
|
-
* - **Async iterator** — `for await (const { text } of branch)` for convenience
|
|
13
|
-
* - **Pass to `generate()`** — which calls `prepare()` internally
|
|
14
|
-
*
|
|
15
|
-
* The caller is responsible for pruning the branch when done.
|
|
16
|
-
*
|
|
17
|
-
* When `parent` is provided, forks from it and prefills the prompt as a delta
|
|
18
|
-
* (with turn separator). Otherwise creates a fresh root branch.
|
|
19
|
-
*
|
|
20
|
-
* @param opts - Generation options (prompt, grammar, params, parent)
|
|
21
|
-
* @returns Prepared Branch with prompt prefilled, ready for produce/commit
|
|
22
|
-
*
|
|
23
|
-
* @example Stream tokens to UI
|
|
24
|
-
* ```typescript
|
|
25
|
-
* const branch = yield* prepare({ prompt, grammar });
|
|
26
|
-
* try {
|
|
27
|
-
* let output = '';
|
|
28
|
-
* while (true) {
|
|
29
|
-
* const { token, text, isStop } = branch.produceSync();
|
|
30
|
-
* if (isStop) break;
|
|
31
|
-
* yield* call(() => branch.commit(token));
|
|
32
|
-
* output += text;
|
|
33
|
-
* updateUI(text); // per-token streaming
|
|
34
|
-
* }
|
|
35
|
-
* } finally {
|
|
36
|
-
* if (!branch.disposed) branch.pruneSync();
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example Batch multiple prepared branches
|
|
41
|
-
* ```typescript
|
|
42
|
-
* const branches = [];
|
|
43
|
-
* for (const task of tasks) {
|
|
44
|
-
* branches.push(yield* prepare({ prompt: task.prompt, grammar, parent: root }));
|
|
45
|
-
* }
|
|
46
|
-
* // Caller batches via BranchStore.commit() for continuous tree batching
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @category Agents
|
|
50
|
-
*/
|
|
51
|
-
export declare function prepare(opts: GenerateOptions): Operation<Branch>;
|
|
52
|
-
/**
|
|
53
|
-
* Single-branch grammar-constrained generation as an Effection operation
|
|
54
|
-
*
|
|
55
|
-
* Convenience wrapper over {@link prepare} — creates/forks a branch, prefills
|
|
56
|
-
* the prompt, generates to EOG, parses the output, and prunes the branch.
|
|
57
|
-
*
|
|
58
|
-
* For per-token streaming or batched generation, use {@link prepare} directly
|
|
59
|
-
* and run your own produce/commit loop.
|
|
60
|
-
*
|
|
61
|
-
* @param opts - Generation options (prompt, grammar, params, parse, parent)
|
|
62
|
-
* @returns Generated text, token count, and optionally parsed result
|
|
63
|
-
*
|
|
64
|
-
* @example Grammar-constrained JSON generation
|
|
65
|
-
* ```typescript
|
|
66
|
-
* const plan = yield* generate({
|
|
67
|
-
* prompt: planPrompt,
|
|
68
|
-
* grammar: planGrammar,
|
|
69
|
-
* params: { temperature: 0.3 },
|
|
70
|
-
* parse: output => JSON.parse(output),
|
|
71
|
-
* });
|
|
72
|
-
* ```
|
|
73
|
-
*
|
|
74
|
-
* @category Agents
|
|
75
|
-
*/
|
|
76
|
-
export declare function generate<T = unknown>(opts: GenerateOptions): Operation<GenerateResult<T>>;
|
|
77
|
-
//# sourceMappingURL=generate.d.ts.map
|
package/dist/generate.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,wBAAiB,OAAO,CAAC,IAAI,EAAE,eAAe,GAAG,SAAS,CAAC,MAAM,CAAC,CAiDjE;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAiB,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,eAAe,GAAG,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CA2C1F"}
|
package/dist/generate.js
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.prepare = prepare;
|
|
4
|
-
exports.generate = generate;
|
|
5
|
-
const effection_1 = require("effection");
|
|
6
|
-
const sdk_1 = require("@lloyal-labs/sdk");
|
|
7
|
-
const context_1 = require("./context");
|
|
8
|
-
const trace_scope_1 = require("./trace-scope");
|
|
9
|
-
/**
|
|
10
|
-
* Prepare a branch for generation — create/fork, set grammar, prefill prompt
|
|
11
|
-
*
|
|
12
|
-
* Returns the prepared Branch ready for token production. The caller owns the
|
|
13
|
-
* branch and decides how to consume it:
|
|
14
|
-
*
|
|
15
|
-
* - **Manual loop** — call `produceSync()` / `commit()` for per-token control,
|
|
16
|
-
* streaming UI updates, or integration into a batched tick loop
|
|
17
|
-
* - **Async iterator** — `for await (const { text } of branch)` for convenience
|
|
18
|
-
* - **Pass to `generate()`** — which calls `prepare()` internally
|
|
19
|
-
*
|
|
20
|
-
* The caller is responsible for pruning the branch when done.
|
|
21
|
-
*
|
|
22
|
-
* When `parent` is provided, forks from it and prefills the prompt as a delta
|
|
23
|
-
* (with turn separator). Otherwise creates a fresh root branch.
|
|
24
|
-
*
|
|
25
|
-
* @param opts - Generation options (prompt, grammar, params, parent)
|
|
26
|
-
* @returns Prepared Branch with prompt prefilled, ready for produce/commit
|
|
27
|
-
*
|
|
28
|
-
* @example Stream tokens to UI
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const branch = yield* prepare({ prompt, grammar });
|
|
31
|
-
* try {
|
|
32
|
-
* let output = '';
|
|
33
|
-
* while (true) {
|
|
34
|
-
* const { token, text, isStop } = branch.produceSync();
|
|
35
|
-
* if (isStop) break;
|
|
36
|
-
* yield* call(() => branch.commit(token));
|
|
37
|
-
* output += text;
|
|
38
|
-
* updateUI(text); // per-token streaming
|
|
39
|
-
* }
|
|
40
|
-
* } finally {
|
|
41
|
-
* if (!branch.disposed) branch.pruneSync();
|
|
42
|
-
* }
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* @example Batch multiple prepared branches
|
|
46
|
-
* ```typescript
|
|
47
|
-
* const branches = [];
|
|
48
|
-
* for (const task of tasks) {
|
|
49
|
-
* branches.push(yield* prepare({ prompt: task.prompt, grammar, parent: root }));
|
|
50
|
-
* }
|
|
51
|
-
* // Caller batches via BranchStore.commit() for continuous tree batching
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* @category Agents
|
|
55
|
-
*/
|
|
56
|
-
function* prepare(opts) {
|
|
57
|
-
const ctx = yield* context_1.Ctx.expect();
|
|
58
|
-
const tw = yield* context_1.Trace.expect();
|
|
59
|
-
const samplerParams = opts.params ?? {};
|
|
60
|
-
const hasParent = !!opts.parent;
|
|
61
|
-
const scope = (0, trace_scope_1.traceScope)(tw, null, 'prepare', { role: hasParent ? 'scratchpad' : 'root', hasGrammar: !!opts.grammar });
|
|
62
|
-
let branch;
|
|
63
|
-
if (opts.parent) {
|
|
64
|
-
branch = yield* (0, effection_1.call)(() => opts.parent.fork());
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
branch = sdk_1.Branch.create(ctx, 0, samplerParams, undefined, opts.grammar);
|
|
68
|
-
}
|
|
69
|
-
tw.write({
|
|
70
|
-
traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
|
|
71
|
-
type: 'branch:create', branchHandle: branch.handle,
|
|
72
|
-
parentHandle: opts.parent?.handle ?? null,
|
|
73
|
-
position: 0, role: hasParent ? 'scratchpad' : 'root',
|
|
74
|
-
});
|
|
75
|
-
let prefillCount;
|
|
76
|
-
if (opts.parent) {
|
|
77
|
-
if (opts.grammar)
|
|
78
|
-
branch.setGrammar(opts.grammar);
|
|
79
|
-
const sep = ctx.getTurnSeparator();
|
|
80
|
-
const delta = yield* (0, effection_1.call)(() => ctx.tokenize(opts.prompt, false));
|
|
81
|
-
const tokens = [...sep, ...delta];
|
|
82
|
-
prefillCount = tokens.length;
|
|
83
|
-
yield* (0, effection_1.call)(() => branch.prefill(tokens));
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
const tokens = ctx.tokenizeSync(opts.prompt);
|
|
87
|
-
prefillCount = tokens.length;
|
|
88
|
-
yield* (0, effection_1.call)(() => branch.prefill(tokens));
|
|
89
|
-
}
|
|
90
|
-
tw.write({
|
|
91
|
-
traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
|
|
92
|
-
type: 'prompt:format', promptText: opts.prompt, tokenCount: prefillCount,
|
|
93
|
-
messages: '', role: 'generate', grammar: opts.grammar,
|
|
94
|
-
});
|
|
95
|
-
tw.write({
|
|
96
|
-
traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
|
|
97
|
-
type: 'branch:prefill', branchHandle: branch.handle, tokenCount: prefillCount,
|
|
98
|
-
role: hasParent ? 'scratchpad' : 'sharedPrefix',
|
|
99
|
-
});
|
|
100
|
-
scope.close();
|
|
101
|
-
return branch;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Single-branch grammar-constrained generation as an Effection operation
|
|
105
|
-
*
|
|
106
|
-
* Convenience wrapper over {@link prepare} — creates/forks a branch, prefills
|
|
107
|
-
* the prompt, generates to EOG, parses the output, and prunes the branch.
|
|
108
|
-
*
|
|
109
|
-
* For per-token streaming or batched generation, use {@link prepare} directly
|
|
110
|
-
* and run your own produce/commit loop.
|
|
111
|
-
*
|
|
112
|
-
* @param opts - Generation options (prompt, grammar, params, parse, parent)
|
|
113
|
-
* @returns Generated text, token count, and optionally parsed result
|
|
114
|
-
*
|
|
115
|
-
* @example Grammar-constrained JSON generation
|
|
116
|
-
* ```typescript
|
|
117
|
-
* const plan = yield* generate({
|
|
118
|
-
* prompt: planPrompt,
|
|
119
|
-
* grammar: planGrammar,
|
|
120
|
-
* params: { temperature: 0.3 },
|
|
121
|
-
* parse: output => JSON.parse(output),
|
|
122
|
-
* });
|
|
123
|
-
* ```
|
|
124
|
-
*
|
|
125
|
-
* @category Agents
|
|
126
|
-
*/
|
|
127
|
-
function* generate(opts) {
|
|
128
|
-
const tw = yield* context_1.Trace.expect();
|
|
129
|
-
const scope = (0, trace_scope_1.traceScope)(tw, null, 'generate', { hasGrammar: !!opts.grammar, hasParent: !!opts.parent });
|
|
130
|
-
const branch = yield* prepare(opts);
|
|
131
|
-
tw.write({
|
|
132
|
-
traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
|
|
133
|
-
type: 'generate:start', branchHandle: branch.handle,
|
|
134
|
-
hasGrammar: !!opts.grammar, hasParent: !!opts.parent,
|
|
135
|
-
role: opts.parent ? 'scratchpad' : 'root',
|
|
136
|
-
});
|
|
137
|
-
try {
|
|
138
|
-
const { output, tokenCount } = yield* (0, effection_1.call)(async () => {
|
|
139
|
-
let output = '';
|
|
140
|
-
let tokenCount = 0;
|
|
141
|
-
for await (const { text } of branch) {
|
|
142
|
-
output += text;
|
|
143
|
-
tokenCount++;
|
|
144
|
-
}
|
|
145
|
-
return { output, tokenCount };
|
|
146
|
-
});
|
|
147
|
-
const parsed = opts.parse ? opts.parse(output) : undefined;
|
|
148
|
-
tw.write({
|
|
149
|
-
traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
|
|
150
|
-
type: 'generate:end', branchHandle: branch.handle, tokenCount, output,
|
|
151
|
-
parsed: parsed !== undefined ? parsed : undefined,
|
|
152
|
-
});
|
|
153
|
-
return { output, tokenCount, parsed };
|
|
154
|
-
}
|
|
155
|
-
finally {
|
|
156
|
-
if (!branch.disposed) {
|
|
157
|
-
tw.write({
|
|
158
|
-
traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
|
|
159
|
-
type: 'branch:prune', branchHandle: branch.handle, position: 0,
|
|
160
|
-
});
|
|
161
|
-
branch.pruneSync();
|
|
162
|
-
}
|
|
163
|
-
scope.close();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
//# sourceMappingURL=generate.js.map
|
package/dist/generate.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generate.js","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":";;AAsDA,0BAiDC;AA0BD,4BA2CC;AA5KD,yCAAiC;AAEjC,0CAA0C;AAC1C,uCAAuC;AACvC,+CAA2C;AAG3C;;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Y,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC;aAC/D,CAAC,CAAC;YACH,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,CAAC;QACD,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;AACH,CAAC"}
|
package/dist/run-agents.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { Operation } from 'effection';
|
|
2
|
-
import type { AgentPoolOptions, AgentPoolResult } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Run an agent pool with automatic branch cleanup on return
|
|
5
|
-
*
|
|
6
|
-
* Wraps {@link useAgentPool} in `scoped()` — agent branches are pruned
|
|
7
|
-
* when the scope exits, before this operation returns. Use this when you
|
|
8
|
-
* don't need to fork from agent branches after the pool completes.
|
|
9
|
-
*
|
|
10
|
-
* For multi-level tree topology (forking from agent branches for
|
|
11
|
-
* verification or follow-up), use {@link useAgentPool} directly within
|
|
12
|
-
* your own scope management.
|
|
13
|
-
*
|
|
14
|
-
* @param opts - Pool configuration: tasks, tools, sampling params, max turns
|
|
15
|
-
* @returns Agent pool result (branches already pruned)
|
|
16
|
-
*
|
|
17
|
-
* @example Research agents with shared root
|
|
18
|
-
* ```typescript
|
|
19
|
-
* const pool = yield* withSharedRoot(
|
|
20
|
-
* { systemPrompt: RESEARCH_PROMPT, tools: toolsJson },
|
|
21
|
-
* function*(root, prefixLen) {
|
|
22
|
-
* return yield* runAgents({
|
|
23
|
-
* tasks: questions.map(q => ({
|
|
24
|
-
* systemPrompt: RESEARCH_PROMPT,
|
|
25
|
-
* content: q,
|
|
26
|
-
* tools: toolsJson,
|
|
27
|
-
* parent: root,
|
|
28
|
-
* })),
|
|
29
|
-
* tools: toolMap,
|
|
30
|
-
* maxTurns: 6,
|
|
31
|
-
* });
|
|
32
|
-
* },
|
|
33
|
-
* );
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @category Agents
|
|
37
|
-
*/
|
|
38
|
-
export declare function runAgents(opts: AgentPoolOptions): Operation<AgentPoolResult>;
|
|
39
|
-
//# sourceMappingURL=run-agents.d.ts.map
|
package/dist/run-agents.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run-agents.d.ts","sourceRoot":"","sources":["../src/run-agents.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAiB,SAAS,CAAC,IAAI,EAAE,gBAAgB,GAAG,SAAS,CAAC,eAAe,CAAC,CAI7E"}
|
package/dist/run-agents.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.runAgents = runAgents;
|
|
4
|
-
const effection_1 = require("effection");
|
|
5
|
-
const agent_pool_1 = require("./agent-pool");
|
|
6
|
-
/**
|
|
7
|
-
* Run an agent pool with automatic branch cleanup on return
|
|
8
|
-
*
|
|
9
|
-
* Wraps {@link useAgentPool} in `scoped()` — agent branches are pruned
|
|
10
|
-
* when the scope exits, before this operation returns. Use this when you
|
|
11
|
-
* don't need to fork from agent branches after the pool completes.
|
|
12
|
-
*
|
|
13
|
-
* For multi-level tree topology (forking from agent branches for
|
|
14
|
-
* verification or follow-up), use {@link useAgentPool} directly within
|
|
15
|
-
* your own scope management.
|
|
16
|
-
*
|
|
17
|
-
* @param opts - Pool configuration: tasks, tools, sampling params, max turns
|
|
18
|
-
* @returns Agent pool result (branches already pruned)
|
|
19
|
-
*
|
|
20
|
-
* @example Research agents with shared root
|
|
21
|
-
* ```typescript
|
|
22
|
-
* const pool = yield* withSharedRoot(
|
|
23
|
-
* { systemPrompt: RESEARCH_PROMPT, tools: toolsJson },
|
|
24
|
-
* function*(root, prefixLen) {
|
|
25
|
-
* return yield* runAgents({
|
|
26
|
-
* tasks: questions.map(q => ({
|
|
27
|
-
* systemPrompt: RESEARCH_PROMPT,
|
|
28
|
-
* content: q,
|
|
29
|
-
* tools: toolsJson,
|
|
30
|
-
* parent: root,
|
|
31
|
-
* })),
|
|
32
|
-
* tools: toolMap,
|
|
33
|
-
* maxTurns: 6,
|
|
34
|
-
* });
|
|
35
|
-
* },
|
|
36
|
-
* );
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @category Agents
|
|
40
|
-
*/
|
|
41
|
-
function* runAgents(opts) {
|
|
42
|
-
return yield* (0, effection_1.scoped)(function* () {
|
|
43
|
-
return yield* (0, agent_pool_1.useAgentPool)({ pruneOnReport: true, ...opts });
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=run-agents.js.map
|
package/dist/run-agents.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run-agents.js","sourceRoot":"","sources":["../src/run-agents.ts"],"names":[],"mappings":";;AAwCA,8BAIC;AA5CD,yCAAmC;AAEnC,6CAA4C;AAG5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,QAAe,CAAC,CAAC,SAAS,CAAC,IAAsB;IAC/C,OAAO,KAAK,CAAC,CAAC,IAAA,kBAAM,EAAC,QAAQ,CAAC;QAC5B,OAAO,KAAK,CAAC,CAAC,IAAA,yBAAY,EAAC,EAAE,aAAa,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;AACL,CAAC"}
|