@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,39 @@
1
+ import type { Operation } from 'effection';
2
+ import type { DivergeOptions, DivergeResult } from './types';
3
+ /**
4
+ * Multi-branch perplexity selection as an Effection operation
5
+ *
6
+ * Forks N branches from a parent (or a fresh root), generates to EOG via
7
+ * batched {@link BranchStore.commit}, then selects the lowest-perplexity
8
+ * attempt. Loser branches are pruned; the caller receives the best branch
9
+ * still alive.
10
+ *
11
+ * When `opts.parent` is provided, the parent branch is NOT pruned — it's
12
+ * owned by the calling scope. Only the forked attempt branches (losers)
13
+ * are pruned. The caller owns the winning branch's lifecycle, typically
14
+ * via {@link Session.promote}.
15
+ *
16
+ * Cleanup is structured: each forked branch registers an `ensure()` callback
17
+ * that prunes it on scope exit. Winners are marked disposed-safe (already
18
+ * pruned or ownership transferred) before the ensure fires.
19
+ *
20
+ * @param opts - Diverge options specifying parent or prompt, attempt count,
21
+ * and sampling parameters
22
+ * @returns Result containing the best branch, all attempt outputs, and
23
+ * aggregate statistics
24
+ *
25
+ * @example Verify with perplexity selection
26
+ * ```typescript
27
+ * const verified = yield* diverge({
28
+ * prompt: verifyPrompt,
29
+ * attempts: 3,
30
+ * params: { temperature: 0.7 },
31
+ * });
32
+ * // verified.best is the lowest-perplexity branch, still alive
33
+ * yield* call(() => session.promote(verified.best));
34
+ * ```
35
+ *
36
+ * @category Agents
37
+ */
38
+ export declare function diverge(opts: DivergeOptions): Operation<DivergeResult>;
39
+ //# sourceMappingURL=diverge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diverge.d.ts","sourceRoot":"","sources":["../src/diverge.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAI3C,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAkB,MAAM,SAAS,CAAC;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAiB,OAAO,CAAC,IAAI,EAAE,cAAc,GAAG,SAAS,CAAC,aAAa,CAAC,CAsGvE"}
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.diverge = diverge;
4
+ const effection_1 = require("effection");
5
+ const sdk_1 = require("@lloyal-labs/sdk");
6
+ const context_1 = require("./context");
7
+ const agent_pool_1 = require("./agent-pool");
8
+ /**
9
+ * Multi-branch perplexity selection as an Effection operation
10
+ *
11
+ * Forks N branches from a parent (or a fresh root), generates to EOG via
12
+ * batched {@link BranchStore.commit}, then selects the lowest-perplexity
13
+ * attempt. Loser branches are pruned; the caller receives the best branch
14
+ * still alive.
15
+ *
16
+ * When `opts.parent` is provided, the parent branch is NOT pruned — it's
17
+ * owned by the calling scope. Only the forked attempt branches (losers)
18
+ * are pruned. The caller owns the winning branch's lifecycle, typically
19
+ * via {@link Session.promote}.
20
+ *
21
+ * Cleanup is structured: each forked branch registers an `ensure()` callback
22
+ * that prunes it on scope exit. Winners are marked disposed-safe (already
23
+ * pruned or ownership transferred) before the ensure fires.
24
+ *
25
+ * @param opts - Diverge options specifying parent or prompt, attempt count,
26
+ * and sampling parameters
27
+ * @returns Result containing the best branch, all attempt outputs, and
28
+ * aggregate statistics
29
+ *
30
+ * @example Verify with perplexity selection
31
+ * ```typescript
32
+ * const verified = yield* diverge({
33
+ * prompt: verifyPrompt,
34
+ * attempts: 3,
35
+ * params: { temperature: 0.7 },
36
+ * });
37
+ * // verified.best is the lowest-perplexity branch, still alive
38
+ * yield* call(() => session.promote(verified.best));
39
+ * ```
40
+ *
41
+ * @category Agents
42
+ */
43
+ function* diverge(opts) {
44
+ const ctx = yield* context_1.Ctx.expect();
45
+ const store = yield* context_1.Store.expect();
46
+ // If parent provided, fork from it. Otherwise create a fresh root.
47
+ let root;
48
+ let ownRoot = false;
49
+ let prefixLength;
50
+ if (opts.parent) {
51
+ root = opts.parent;
52
+ prefixLength = root.position;
53
+ }
54
+ else {
55
+ if (!opts.prompt)
56
+ throw new Error('diverge() requires either opts.parent or opts.prompt');
57
+ const tokens = ctx.tokenizeSync(opts.prompt);
58
+ root = sdk_1.Branch.create(ctx, 0, opts.params ?? {});
59
+ yield* (0, effection_1.call)(() => root.prefill(tokens));
60
+ prefixLength = tokens.length;
61
+ ownRoot = true;
62
+ // If we created the root, ensure it's cleaned up
63
+ yield* (0, effection_1.ensure)(() => {
64
+ if (ownRoot && !root.disposed) {
65
+ try {
66
+ root.pruneSync();
67
+ }
68
+ catch { /* children may remain */ }
69
+ }
70
+ });
71
+ }
72
+ const live = [];
73
+ for (let i = 0; i < opts.attempts; i++) {
74
+ const branch = root.forkSync();
75
+ // Each forked branch gets its own ensure() for structured cleanup
76
+ yield* (0, effection_1.ensure)(() => {
77
+ if (!branch.disposed) {
78
+ try {
79
+ branch.pruneSync();
80
+ }
81
+ catch { /* already gone */ }
82
+ }
83
+ });
84
+ branch.reseedSampler(2000 + i);
85
+ live.push({ branch, output: '', done: false, tokenCount: 0, ppl: Infinity });
86
+ }
87
+ // Batched generation — produceSync/commit loop
88
+ let steps = 0;
89
+ for (;;) {
90
+ const pressure = new agent_pool_1.ContextPressure(ctx);
91
+ if (pressure.critical) {
92
+ for (const a of live) {
93
+ if (!a.done)
94
+ a.done = true;
95
+ }
96
+ break;
97
+ }
98
+ const entries = [];
99
+ for (const a of live) {
100
+ if (a.done)
101
+ continue;
102
+ const { token, text, isStop } = a.branch.produceSync();
103
+ if (isStop) {
104
+ const p = a.branch.perplexity;
105
+ a.ppl = Number.isFinite(p) ? p : Infinity;
106
+ a.done = true;
107
+ continue;
108
+ }
109
+ entries.push([a.branch, token]);
110
+ a.output += text;
111
+ a.tokenCount++;
112
+ }
113
+ if (entries.length === 0)
114
+ break;
115
+ yield* (0, effection_1.call)(() => store.commit(entries));
116
+ steps++;
117
+ }
118
+ // Select by lowest perplexity (most coherent)
119
+ const bestIdx = live.reduce((bi, a, i) => a.ppl <= live[bi].ppl ? i : bi, 0);
120
+ // Prune losers now — winner stays alive as caller's result.
121
+ // ensure() will be a no-op for these since they're already disposed.
122
+ for (let i = 0; i < live.length; i++) {
123
+ if (i !== bestIdx && !live[i].branch.disposed) {
124
+ live[i].branch.pruneSync();
125
+ }
126
+ }
127
+ // If we created root and it's no longer needed, prune it now.
128
+ // (ensure() will be a no-op since it checks disposed)
129
+ if (ownRoot && !root.disposed && root.children.length === 0) {
130
+ root.pruneSync();
131
+ }
132
+ const totalTokens = live.reduce((s, a) => s + a.tokenCount, 0);
133
+ const attempts = live.map(a => ({
134
+ branch: a.branch,
135
+ output: a.output,
136
+ tokenCount: a.tokenCount,
137
+ ppl: a.ppl,
138
+ }));
139
+ return {
140
+ best: live[bestIdx].branch,
141
+ bestOutput: live[bestIdx].output,
142
+ attempts,
143
+ totalTokens,
144
+ steps,
145
+ prefixLength,
146
+ };
147
+ }
148
+ //# sourceMappingURL=diverge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diverge.js","sourceRoot":"","sources":["../src/diverge.ts"],"names":[],"mappings":";;AA0CA,0BAsGC;AAhJD,yCAAyC;AAEzC,0CAA0C;AAC1C,uCAAuC;AACvC,6CAA+C;AAG/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,QAAe,CAAC,CAAC,OAAO,CAAC,IAAoB;IAC3C,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,aAAG,CAAC,MAAM,EAAE,CAAC;IAChC,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,eAAK,CAAC,MAAM,EAAE,CAAC;IAEpC,mEAAmE;IACnE,IAAI,IAAY,CAAC;IACjB,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,IAAI,YAAoB,CAAC;IAEzB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QAChB,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC;QACnB,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC;IAC/B,CAAC;SAAM,CAAC;QACN,IAAI,CAAC,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AE,CAAC,CAAC,MAAM;QAChB,UAAU,EAAE,CAAC,CAAC,UAAU;QACxB,GAAG,EAAE,CAAC,CAAC,GAAG;KACX,CAAC,CAAC,CAAC;IAEJ,OAAO;QACL,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM;QAC1B,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM;QAChC,QAAQ;QACR,WAAW;QACX,KAAK;QACL,YAAY;KACb,CAAC;AACJ,CAAC"}
@@ -0,0 +1,30 @@
1
+ import type { Operation } from 'effection';
2
+ import type { GenerateOptions, GenerateResult } from './types';
3
+ /**
4
+ * Single-branch grammar-constrained generation as an Effection operation
5
+ *
6
+ * Creates a fresh branch at position 0, prefills the prompt, generates
7
+ * to EOG, and prunes the branch. Uses {@link Branch}'s async iterator
8
+ * — single-branch generation doesn't need batched commit.
9
+ *
10
+ * The branch is always cleaned up via try/finally, even on error or
11
+ * scope cancellation.
12
+ *
13
+ * @param opts - Generation options (prompt, grammar, params, parse)
14
+ * @returns Generated text, token count, and optionally parsed result
15
+ *
16
+ * @example Grammar-constrained JSON generation
17
+ * ```typescript
18
+ * const plan = yield* generate({
19
+ * prompt: planPrompt,
20
+ * grammar: planGrammar,
21
+ * params: { temperature: 0.3 },
22
+ * parse: output => JSON.parse(output),
23
+ * });
24
+ * console.log(plan.parsed); // typed result from parse()
25
+ * ```
26
+ *
27
+ * @category Agents
28
+ */
29
+ export declare function generate<T = unknown>(opts: GenerateOptions): Operation<GenerateResult<T>>;
30
+ //# sourceMappingURL=generate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAG3C,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAiB,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,eAAe,GAAG,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CA0B1F"}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generate = generate;
4
+ const effection_1 = require("effection");
5
+ const sdk_1 = require("@lloyal-labs/sdk");
6
+ const context_1 = require("./context");
7
+ /**
8
+ * Single-branch grammar-constrained generation as an Effection operation
9
+ *
10
+ * Creates a fresh branch at position 0, prefills the prompt, generates
11
+ * to EOG, and prunes the branch. Uses {@link Branch}'s async iterator
12
+ * — single-branch generation doesn't need batched commit.
13
+ *
14
+ * The branch is always cleaned up via try/finally, even on error or
15
+ * scope cancellation.
16
+ *
17
+ * @param opts - Generation options (prompt, grammar, params, parse)
18
+ * @returns Generated text, token count, and optionally parsed result
19
+ *
20
+ * @example Grammar-constrained JSON generation
21
+ * ```typescript
22
+ * const plan = yield* generate({
23
+ * prompt: planPrompt,
24
+ * grammar: planGrammar,
25
+ * params: { temperature: 0.3 },
26
+ * parse: output => JSON.parse(output),
27
+ * });
28
+ * console.log(plan.parsed); // typed result from parse()
29
+ * ```
30
+ *
31
+ * @category Agents
32
+ */
33
+ function* generate(opts) {
34
+ const ctx = yield* context_1.Ctx.expect();
35
+ const samplerParams = opts.params ?? {};
36
+ const branch = sdk_1.Branch.create(ctx, 0, samplerParams, undefined, opts.grammar);
37
+ try {
38
+ const tokens = ctx.tokenizeSync(opts.prompt);
39
+ yield* (0, effection_1.call)(() => branch.prefill(tokens));
40
+ // Consume async iterator inside call() — generators can't use for-await
41
+ const { output, tokenCount } = yield* (0, effection_1.call)(async () => {
42
+ let output = '';
43
+ let tokenCount = 0;
44
+ for await (const { text } of branch) {
45
+ output += text;
46
+ tokenCount++;
47
+ }
48
+ return { output, tokenCount };
49
+ });
50
+ const parsed = opts.parse ? opts.parse(output) : undefined;
51
+ return { output, tokenCount, parsed };
52
+ }
53
+ finally {
54
+ if (!branch.disposed)
55
+ branch.pruneSync();
56
+ }
57
+ }
58
+ //# sourceMappingURL=generate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.js","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":";;AAgCA,4BA0BC;AA1DD,yCAAiC;AAEjC,0CAA0C;AAC1C,uCAAgC;AAGhC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,QAAe,CAAC,CAAC,QAAQ,CAAc,IAAqB;IAC1D,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,aAAG,CAAC,MAAM,EAAE,CAAC;IAEhC,MAAM,aAAa,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC;IACxC,MAAM,MAAM,GAAG,YAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,aAAa,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IAE7E,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,GAAG,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC7C,KAAK,CAAC,CAAC,IAAA,gBAAI,EAAC,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;QAE1C,wEAAwE;QACxE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC,CAAC,IAAA,gBAAI,EAAC,KAAK,IAAI,EAAE;YACpD,IAAI,MAAM,GAAG,EAAE,CAAC;YAChB,IAAI,UAAU,GAAG,CAAC,CAAC;YACnB,IAAI,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,MAAM,EAAE,CAAC;gBACpC,MAAM,IAAI,IAAI,CAAC;gBACf,UAAU,EAAE,CAAC;YACf,CAAC;YACD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;QAChC,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAM,CAAC,CAAC,CAAC,SAAS,CAAC;QAChE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;IACxC,CAAC;YAAS,CAAC;QACT,IAAI,CAAC,MAAM,CAAC,QAAQ;YAAE,MAAM,CAAC,SAAS,EAAE,CAAC;IAC3C,CAAC;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ export { Ctx, Store, Events } from './context';
2
+ export { Tool } from './Tool';
3
+ export { buildUserDelta, buildToolResultDelta } from '@lloyal-labs/sdk';
4
+ export { generate } from './generate';
5
+ export { diverge } from './diverge';
6
+ export { useAgentPool, ContextPressure } from './agent-pool';
7
+ export { runAgents } from './run-agents';
8
+ export { createToolkit } from './toolkit';
9
+ export { initAgents } from './init';
10
+ export { withSharedRoot } from './shared-root';
11
+ export type { Toolkit } from './toolkit';
12
+ export type { AgentHandle } from './init';
13
+ export type { SharedRootOptions } from './shared-root';
14
+ export type { TraceToken, JsonSchema, ToolSchema, ToolContext, PressureThresholds, AgentTaskSpec, AgentPoolOptions, AgentResult, AgentPoolResult, GenerateOptions, GenerateResult, DivergeOptions, DivergeAttempt, DivergeResult, AgentEvent, } from './types';
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,YAAY,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACzC,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAC1C,YAAY,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,YAAY,EACV,UAAU,EACV,UAAU,EACV,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf,eAAe,EACf,cAAc,EACd,cAAc,EACd,cAAc,EACd,aAAa,EACb,UAAU,GACX,MAAM,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withSharedRoot = exports.initAgents = exports.createToolkit = exports.runAgents = exports.ContextPressure = exports.useAgentPool = exports.diverge = exports.generate = exports.buildToolResultDelta = exports.buildUserDelta = exports.Tool = exports.Events = exports.Store = exports.Ctx = void 0;
4
+ var context_1 = require("./context");
5
+ Object.defineProperty(exports, "Ctx", { enumerable: true, get: function () { return context_1.Ctx; } });
6
+ Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return context_1.Store; } });
7
+ Object.defineProperty(exports, "Events", { enumerable: true, get: function () { return context_1.Events; } });
8
+ var Tool_1 = require("./Tool");
9
+ Object.defineProperty(exports, "Tool", { enumerable: true, get: function () { return Tool_1.Tool; } });
10
+ var sdk_1 = require("@lloyal-labs/sdk");
11
+ Object.defineProperty(exports, "buildUserDelta", { enumerable: true, get: function () { return sdk_1.buildUserDelta; } });
12
+ Object.defineProperty(exports, "buildToolResultDelta", { enumerable: true, get: function () { return sdk_1.buildToolResultDelta; } });
13
+ var generate_1 = require("./generate");
14
+ Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return generate_1.generate; } });
15
+ var diverge_1 = require("./diverge");
16
+ Object.defineProperty(exports, "diverge", { enumerable: true, get: function () { return diverge_1.diverge; } });
17
+ var agent_pool_1 = require("./agent-pool");
18
+ Object.defineProperty(exports, "useAgentPool", { enumerable: true, get: function () { return agent_pool_1.useAgentPool; } });
19
+ Object.defineProperty(exports, "ContextPressure", { enumerable: true, get: function () { return agent_pool_1.ContextPressure; } });
20
+ var run_agents_1 = require("./run-agents");
21
+ Object.defineProperty(exports, "runAgents", { enumerable: true, get: function () { return run_agents_1.runAgents; } });
22
+ var toolkit_1 = require("./toolkit");
23
+ Object.defineProperty(exports, "createToolkit", { enumerable: true, get: function () { return toolkit_1.createToolkit; } });
24
+ var init_1 = require("./init");
25
+ Object.defineProperty(exports, "initAgents", { enumerable: true, get: function () { return init_1.initAgents; } });
26
+ var shared_root_1 = require("./shared-root");
27
+ Object.defineProperty(exports, "withSharedRoot", { enumerable: true, get: function () { return shared_root_1.withSharedRoot; } });
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAA+C;AAAtC,8FAAA,GAAG,OAAA;AAAE,gGAAA,KAAK,OAAA;AAAE,iGAAA,MAAM,OAAA;AAC3B,+BAA8B;AAArB,4FAAA,IAAI,OAAA;AACb,wCAAwE;AAA/D,qGAAA,cAAc,OAAA;AAAE,2GAAA,oBAAoB,OAAA;AAC7C,uCAAsC;AAA7B,oGAAA,QAAQ,OAAA;AACjB,qCAAoC;AAA3B,kGAAA,OAAO,OAAA;AAChB,2CAA6D;AAApD,0GAAA,YAAY,OAAA;AAAE,6GAAA,eAAe,OAAA;AACtC,2CAAyC;AAAhC,uGAAA,SAAS,OAAA;AAClB,qCAA0C;AAAjC,wGAAA,aAAa,OAAA;AACtB,+BAAoC;AAA3B,kGAAA,UAAU,OAAA;AACnB,6CAA+C;AAAtC,6GAAA,cAAc,OAAA"}
package/dist/init.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ import type { Operation, Channel } from 'effection';
2
+ import { BranchStore } from '@lloyal-labs/sdk';
3
+ import { Session } from '@lloyal-labs/sdk';
4
+ import type { SessionContext } from '@lloyal-labs/sdk';
5
+ import type { AgentEvent } from './types';
6
+ /**
7
+ * Handle returned by {@link initAgents} containing all agent resources
8
+ *
9
+ * @category Agents
10
+ */
11
+ export interface AgentHandle<E = AgentEvent> {
12
+ /** The session context (model, tokenizer, KV cache) */
13
+ ctx: SessionContext;
14
+ /** Branch store for batched commit/prefill across branches */
15
+ store: BranchStore;
16
+ /** Session managing conversation trunk and branch lifecycle */
17
+ session: Session;
18
+ /** Channel for subscribing to agent events */
19
+ events: Channel<E, void>;
20
+ }
21
+ /**
22
+ * Bootstrap the agent infrastructure and register structured cleanup
23
+ *
24
+ * Creates {@link BranchStore}, {@link Session}, and an event channel, then
25
+ * sets all three Effection contexts ({@link Ctx}, {@link Store},
26
+ * {@link Events}) in the caller's scope. Cleanup runs on scope exit
27
+ * (Ctrl-C, error, normal completion) via `ensure()`.
28
+ *
29
+ * Context values are set in the caller's scope — visible to all subsequent
30
+ * operations. This is why `initAgents` uses `ensure()` rather than
31
+ * `resource()`: a resource creates a child scope where `Ctx.set()` would
32
+ * be invisible to sibling operations.
33
+ *
34
+ * The caller creates the {@link SessionContext} (model path, nCtx, KV types
35
+ * are harness-specific decisions) and passes it in.
36
+ *
37
+ * @param ctx - Session context created via `createContext()`
38
+ * @returns Agent handle with session, store, and event channel
39
+ *
40
+ * @example Canonical bootstrap
41
+ * ```typescript
42
+ * main(function*() {
43
+ * const ctx = yield* call(() => createContext({
44
+ * modelPath, nCtx: 16384,
45
+ * nSeqMax: 4, typeK: 'q4_0', typeV: 'q4_0',
46
+ * }));
47
+ *
48
+ * const { session, events } = yield* initAgents(ctx);
49
+ * // Ctx, Store, Events are now set — generate(), diverge(),
50
+ * // useAgentPool() will find them automatically.
51
+ * // Cleanup runs on scope exit.
52
+ * });
53
+ * ```
54
+ *
55
+ * @category Agents
56
+ */
57
+ export declare function initAgents<E = AgentEvent>(ctx: SessionContext): Operation<AgentHandle<E>>;
58
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C;;;;GAIG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,UAAU;IACzC,uDAAuD;IACvD,GAAG,EAAE,cAAc,CAAC;IACpB,8DAA8D;IAC9D,KAAK,EAAE,WAAW,CAAC;IACnB,+DAA+D;IAC/D,OAAO,EAAE,OAAO,CAAC;IACjB,8CAA8C;IAC9C,MAAM,EAAE,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAiB,UAAU,CAAC,CAAC,GAAG,UAAU,EACxC,GAAG,EAAE,cAAc,GAClB,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAe3B"}
package/dist/init.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initAgents = initAgents;
4
+ const effection_1 = require("effection");
5
+ const sdk_1 = require("@lloyal-labs/sdk");
6
+ const sdk_2 = require("@lloyal-labs/sdk");
7
+ const context_1 = require("./context");
8
+ /**
9
+ * Bootstrap the agent infrastructure and register structured cleanup
10
+ *
11
+ * Creates {@link BranchStore}, {@link Session}, and an event channel, then
12
+ * sets all three Effection contexts ({@link Ctx}, {@link Store},
13
+ * {@link Events}) in the caller's scope. Cleanup runs on scope exit
14
+ * (Ctrl-C, error, normal completion) via `ensure()`.
15
+ *
16
+ * Context values are set in the caller's scope — visible to all subsequent
17
+ * operations. This is why `initAgents` uses `ensure()` rather than
18
+ * `resource()`: a resource creates a child scope where `Ctx.set()` would
19
+ * be invisible to sibling operations.
20
+ *
21
+ * The caller creates the {@link SessionContext} (model path, nCtx, KV types
22
+ * are harness-specific decisions) and passes it in.
23
+ *
24
+ * @param ctx - Session context created via `createContext()`
25
+ * @returns Agent handle with session, store, and event channel
26
+ *
27
+ * @example Canonical bootstrap
28
+ * ```typescript
29
+ * main(function*() {
30
+ * const ctx = yield* call(() => createContext({
31
+ * modelPath, nCtx: 16384,
32
+ * nSeqMax: 4, typeK: 'q4_0', typeV: 'q4_0',
33
+ * }));
34
+ *
35
+ * const { session, events } = yield* initAgents(ctx);
36
+ * // Ctx, Store, Events are now set — generate(), diverge(),
37
+ * // useAgentPool() will find them automatically.
38
+ * // Cleanup runs on scope exit.
39
+ * });
40
+ * ```
41
+ *
42
+ * @category Agents
43
+ */
44
+ function* initAgents(ctx) {
45
+ const store = new sdk_1.BranchStore(ctx);
46
+ const session = new sdk_2.Session({ ctx, store });
47
+ const events = (0, effection_1.createChannel)();
48
+ yield* context_1.Ctx.set(ctx);
49
+ yield* context_1.Store.set(store);
50
+ yield* context_1.Events.set(events);
51
+ yield* (0, effection_1.ensure)(function* () {
52
+ yield* (0, effection_1.call)(() => session.dispose());
53
+ ctx.dispose();
54
+ });
55
+ return { ctx, store, session, events };
56
+ }
57
+ //# sourceMappingURL=init.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":";;AA4DA,gCAiBC;AA7ED,yCAAwD;AAExD,0CAA+C;AAC/C,0CAA2C;AAE3C,uCAA+C;AAmB/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,QAAe,CAAC,CAAC,UAAU,CACzB,GAAmB;IAEnB,MAAM,KAAK,GAAG,IAAI,iBAAW,CAAC,GAAG,CAAC,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,aAAO,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5C,MAAM,MAAM,GAAqB,IAAA,yBAAa,GAAW,CAAC;IAE1D,KAAK,CAAC,CAAC,aAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACpB,KAAK,CAAC,CAAC,eAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACxB,KAAK,CAAC,CAAC,gBAAM,CAAC,GAAG,CAAC,MAA8C,CAAC,CAAC;IAElE,KAAK,CAAC,CAAC,IAAA,kBAAM,EAAC,QAAQ,CAAC;QACrB,KAAK,CAAC,CAAC,IAAA,gBAAI,EAAC,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;QACrC,GAAG,CAAC,OAAO,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACzC,CAAC"}
@@ -0,0 +1,39 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,46 @@
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)(opts);
44
+ });
45
+ }
46
+ //# sourceMappingURL=run-agents.js.map
@@ -0,0 +1 @@
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,IAAI,CAAC,CAAC;IACnC,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,55 @@
1
+ import type { Operation } from 'effection';
2
+ import { Branch } from '@lloyal-labs/sdk';
3
+ import type { SamplingParams } from './types';
4
+ /**
5
+ * Configuration for {@link withSharedRoot}
6
+ *
7
+ * @category Agents
8
+ */
9
+ export interface SharedRootOptions {
10
+ /** System prompt to tokenize and prefill into the shared root */
11
+ systemPrompt: string;
12
+ /** JSON-serialized tool schemas for tool-aware prompt formatting */
13
+ tools?: string;
14
+ /** Sampling parameters for the root branch */
15
+ params?: SamplingParams;
16
+ }
17
+ /**
18
+ * Scoped shared root branch with guaranteed cleanup
19
+ *
20
+ * Creates a root branch, prefills the system prompt, and passes it to
21
+ * the body function. The root is pruned via try/finally when the body
22
+ * returns or throws, regardless of whether children still exist.
23
+ *
24
+ * Use this for the cold-path pattern where multiple agents share a
25
+ * tokenized system prompt prefix. The `sharedPrefixLength` passed to
26
+ * the body enables KV savings calculation.
27
+ *
28
+ * @param opts - System prompt, tools, and sampling parameters
29
+ * @param body - Operation that receives the root branch and prefix length.
30
+ * Typically calls {@link runAgents} or {@link useAgentPool} inside.
31
+ * @returns The body's return value
32
+ *
33
+ * @example Cold-path research with shared prefix
34
+ * ```typescript
35
+ * const { result, prefixLen } = yield* withSharedRoot(
36
+ * { systemPrompt: RESEARCH_PROMPT, tools: toolsJson },
37
+ * function*(root, prefixLen) {
38
+ * const result = yield* runAgents({
39
+ * tasks: questions.map(q => ({
40
+ * systemPrompt: RESEARCH_PROMPT,
41
+ * content: q,
42
+ * tools: toolsJson,
43
+ * parent: root,
44
+ * })),
45
+ * tools: toolMap,
46
+ * });
47
+ * return { result, prefixLen };
48
+ * },
49
+ * );
50
+ * ```
51
+ *
52
+ * @category Agents
53
+ */
54
+ export declare function withSharedRoot<T>(opts: SharedRootOptions, body: (root: Branch, sharedPrefixLength: number) => Operation<T>): Operation<T>;
55
+ //# sourceMappingURL=shared-root.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-root.d.ts","sourceRoot":"","sources":["../src/shared-root.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,iEAAiE;IACjE,YAAY,EAAE,MAAM,CAAC;IACrB,oEAAoE;IACpE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8CAA8C;IAC9C,MAAM,CAAC,EAAE,cAAc,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAiB,cAAc,CAAC,CAAC,EAC/B,IAAI,EAAE,iBAAiB,EACvB,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,GAC/D,SAAS,CAAC,CAAC,CAAC,CAkBd"}