@lloyal-labs/lloyal-agents 1.0.0 → 1.2.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 (48) hide show
  1. package/README.md +28 -50
  2. package/dist/Tool.d.ts +13 -3
  3. package/dist/Tool.d.ts.map +1 -1
  4. package/dist/Tool.js +7 -2
  5. package/dist/Tool.js.map +1 -1
  6. package/dist/agent-pool.d.ts +9 -11
  7. package/dist/agent-pool.d.ts.map +1 -1
  8. package/dist/agent-pool.js +156 -101
  9. package/dist/agent-pool.js.map +1 -1
  10. package/dist/context.d.ts +32 -1
  11. package/dist/context.d.ts.map +1 -1
  12. package/dist/context.js +31 -1
  13. package/dist/context.js.map +1 -1
  14. package/dist/generate.d.ts +20 -5
  15. package/dist/generate.d.ts.map +1 -1
  16. package/dist/generate.js +80 -9
  17. package/dist/generate.js.map +1 -1
  18. package/dist/index.d.ts +6 -1
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +11 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/init.d.ts +4 -1
  23. package/dist/init.d.ts.map +1 -1
  24. package/dist/init.js +5 -1
  25. package/dist/init.js.map +1 -1
  26. package/dist/shared-root.d.ts +3 -3
  27. package/dist/shared-root.d.ts.map +1 -1
  28. package/dist/shared-root.js +66 -5
  29. package/dist/shared-root.js.map +1 -1
  30. package/dist/source.d.ts +27 -0
  31. package/dist/source.d.ts.map +1 -0
  32. package/dist/source.js +25 -0
  33. package/dist/source.js.map +1 -0
  34. package/dist/trace-scope.d.ts +25 -0
  35. package/dist/trace-scope.d.ts.map +1 -0
  36. package/dist/trace-scope.js +41 -0
  37. package/dist/trace-scope.js.map +1 -0
  38. package/dist/trace-types.d.ts +178 -0
  39. package/dist/trace-types.d.ts.map +1 -0
  40. package/dist/trace-types.js +3 -0
  41. package/dist/trace-types.js.map +1 -0
  42. package/dist/trace-writer.d.ts +58 -0
  43. package/dist/trace-writer.d.ts.map +1 -0
  44. package/dist/trace-writer.js +56 -0
  45. package/dist/trace-writer.js.map +1 -0
  46. package/dist/types.d.ts +8 -6
  47. package/dist/types.d.ts.map +1 -1
  48. package/package.json +1 -1
@@ -3,14 +3,19 @@ import type { GenerateOptions, GenerateResult } from './types';
3
3
  /**
4
4
  * Single-branch grammar-constrained generation as an Effection operation
5
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.
6
+ * Creates a fresh branch (or forks from `opts.parent`), prefills the prompt,
7
+ * generates to EOG, and prunes the branch. Uses {@link Branch}'s async
8
+ * iterator — single-branch generation doesn't need batched commit.
9
+ *
10
+ * When `parent` is provided, the prompt is prefilled as a delta (with turn
11
+ * separator) on a fork of the parent. This is the attention scratchpad
12
+ * pattern: the fork sees the parent's context, attends to the prompt
13
+ * content, generates a result, and is pruned — zero net KV cost.
9
14
  *
10
15
  * The branch is always cleaned up via try/finally, even on error or
11
16
  * scope cancellation.
12
17
  *
13
- * @param opts - Generation options (prompt, grammar, params, parse)
18
+ * @param opts - Generation options (prompt, grammar, params, parse, parent)
14
19
  * @returns Generated text, token count, and optionally parsed result
15
20
  *
16
21
  * @example Grammar-constrained JSON generation
@@ -21,7 +26,17 @@ import type { GenerateOptions, GenerateResult } from './types';
21
26
  * params: { temperature: 0.3 },
22
27
  * parse: output => JSON.parse(output),
23
28
  * });
24
- * console.log(plan.parsed); // typed result from parse()
29
+ * ```
30
+ *
31
+ * @example Attention scratchpad — fork, attend, extract, prune
32
+ * ```typescript
33
+ * const extracted = yield* generate({
34
+ * prompt: contentToAttend,
35
+ * grammar: extractionGrammar,
36
+ * parse: output => JSON.parse(output),
37
+ * parent: agentBranch,
38
+ * });
39
+ * // Fork is pruned — parent's KV unchanged
25
40
  * ```
26
41
  *
27
42
  * @category Agents
@@ -1 +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"}
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAI3C,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAiB,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,eAAe,GAAG,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAoF1F"}
package/dist/generate.js CHANGED
@@ -4,17 +4,23 @@ exports.generate = generate;
4
4
  const effection_1 = require("effection");
5
5
  const sdk_1 = require("@lloyal-labs/sdk");
6
6
  const context_1 = require("./context");
7
+ const trace_scope_1 = require("./trace-scope");
7
8
  /**
8
9
  * Single-branch grammar-constrained generation as an Effection operation
9
10
  *
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.
11
+ * Creates a fresh branch (or forks from `opts.parent`), prefills the prompt,
12
+ * generates to EOG, and prunes the branch. Uses {@link Branch}'s async
13
+ * iterator — single-branch generation doesn't need batched commit.
14
+ *
15
+ * When `parent` is provided, the prompt is prefilled as a delta (with turn
16
+ * separator) on a fork of the parent. This is the attention scratchpad
17
+ * pattern: the fork sees the parent's context, attends to the prompt
18
+ * content, generates a result, and is pruned — zero net KV cost.
13
19
  *
14
20
  * The branch is always cleaned up via try/finally, even on error or
15
21
  * scope cancellation.
16
22
  *
17
- * @param opts - Generation options (prompt, grammar, params, parse)
23
+ * @param opts - Generation options (prompt, grammar, params, parse, parent)
18
24
  * @returns Generated text, token count, and optionally parsed result
19
25
  *
20
26
  * @example Grammar-constrained JSON generation
@@ -25,18 +31,72 @@ const context_1 = require("./context");
25
31
  * params: { temperature: 0.3 },
26
32
  * parse: output => JSON.parse(output),
27
33
  * });
28
- * console.log(plan.parsed); // typed result from parse()
34
+ * ```
35
+ *
36
+ * @example Attention scratchpad — fork, attend, extract, prune
37
+ * ```typescript
38
+ * const extracted = yield* generate({
39
+ * prompt: contentToAttend,
40
+ * grammar: extractionGrammar,
41
+ * parse: output => JSON.parse(output),
42
+ * parent: agentBranch,
43
+ * });
44
+ * // Fork is pruned — parent's KV unchanged
29
45
  * ```
30
46
  *
31
47
  * @category Agents
32
48
  */
33
49
  function* generate(opts) {
34
50
  const ctx = yield* context_1.Ctx.expect();
51
+ const tw = yield* context_1.Trace.expect();
35
52
  const samplerParams = opts.params ?? {};
36
- const branch = sdk_1.Branch.create(ctx, 0, samplerParams, undefined, opts.grammar);
53
+ const hasParent = !!opts.parent;
54
+ const role = hasParent ? 'scratchpad' : 'root';
55
+ const scope = (0, trace_scope_1.traceScope)(tw, null, 'generate', { role, hasGrammar: !!opts.grammar });
56
+ let branch;
57
+ if (opts.parent) {
58
+ branch = yield* (0, effection_1.call)(() => opts.parent.fork());
59
+ }
60
+ else {
61
+ branch = sdk_1.Branch.create(ctx, 0, samplerParams, undefined, opts.grammar);
62
+ }
63
+ tw.write({
64
+ traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
65
+ type: 'branch:create', branchHandle: branch.handle,
66
+ parentHandle: opts.parent?.handle ?? null,
67
+ position: 0, role: hasParent ? 'scratchpad' : 'root',
68
+ });
37
69
  try {
38
- const tokens = ctx.tokenizeSync(opts.prompt);
39
- yield* (0, effection_1.call)(() => branch.prefill(tokens));
70
+ let prefillCount;
71
+ if (opts.parent) {
72
+ if (opts.grammar)
73
+ branch.setGrammar(opts.grammar);
74
+ const sep = ctx.getTurnSeparator();
75
+ const delta = yield* (0, effection_1.call)(() => ctx.tokenize(opts.prompt, false));
76
+ const tokens = [...sep, ...delta];
77
+ prefillCount = tokens.length;
78
+ yield* (0, effection_1.call)(() => branch.prefill(tokens));
79
+ }
80
+ else {
81
+ const tokens = ctx.tokenizeSync(opts.prompt);
82
+ prefillCount = tokens.length;
83
+ yield* (0, effection_1.call)(() => branch.prefill(tokens));
84
+ }
85
+ tw.write({
86
+ traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
87
+ type: 'prompt:format', promptText: opts.prompt, tokenCount: prefillCount,
88
+ messages: '', role: 'generate', grammar: opts.grammar,
89
+ });
90
+ tw.write({
91
+ traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
92
+ type: 'branch:prefill', branchHandle: branch.handle, tokenCount: prefillCount,
93
+ role: hasParent ? 'scratchpad' : 'sharedPrefix',
94
+ });
95
+ tw.write({
96
+ traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
97
+ type: 'generate:start', branchHandle: branch.handle,
98
+ hasGrammar: !!opts.grammar, hasParent, role,
99
+ });
40
100
  // Consume async iterator inside call() — generators can't use for-await
41
101
  const { output, tokenCount } = yield* (0, effection_1.call)(async () => {
42
102
  let output = '';
@@ -48,11 +108,22 @@ function* generate(opts) {
48
108
  return { output, tokenCount };
49
109
  });
50
110
  const parsed = opts.parse ? opts.parse(output) : undefined;
111
+ tw.write({
112
+ traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
113
+ type: 'generate:end', branchHandle: branch.handle, tokenCount, output,
114
+ parsed: parsed !== undefined ? parsed : undefined,
115
+ });
51
116
  return { output, tokenCount, parsed };
52
117
  }
53
118
  finally {
54
- if (!branch.disposed)
119
+ if (!branch.disposed) {
120
+ tw.write({
121
+ traceId: tw.nextId(), parentTraceId: scope.traceId, ts: performance.now(),
122
+ type: 'branch:prune', branchHandle: branch.handle, position: 0,
123
+ });
55
124
  branch.pruneSync();
125
+ }
126
+ scope.close();
56
127
  }
57
128
  }
58
129
  //# sourceMappingURL=generate.js.map
@@ -1 +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,EAA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CAAC,SAAS,EAAE,CAAC;QACrB,CAAC;QACD,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;AACH,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export { Ctx, Store, Events } from './context';
1
+ export { Ctx, Store, Events, Trace, TraceParent, ScratchpadParent } from './context';
2
2
  export { Tool } from './Tool';
3
+ export { Source } from './source';
3
4
  export { buildUserDelta, buildToolResultDelta } from '@lloyal-labs/sdk';
4
5
  export { generate } from './generate';
5
6
  export { diverge } from './diverge';
@@ -8,7 +9,11 @@ export { runAgents } from './run-agents';
8
9
  export { createToolkit } from './toolkit';
9
10
  export { initAgents } from './init';
10
11
  export { withSharedRoot } from './shared-root';
12
+ export { NullTraceWriter, JsonlTraceWriter } from './trace-writer';
13
+ export { traceScope } from './trace-scope';
11
14
  export type { Toolkit } from './toolkit';
15
+ export type { TraceWriter } from './trace-writer';
16
+ export type { TraceEvent, TraceId } from './trace-types';
12
17
  export type { AgentHandle } from './init';
13
18
  export type { SharedRootOptions } from './shared-root';
14
19
  export type { TraceToken, JsonSchema, ToolSchema, ToolContext, PressureThresholds, AgentTaskSpec, AgentPoolOptions, AgentResult, AgentPoolResult, GenerateOptions, GenerateResult, DivergeOptions, DivergeAttempt, DivergeResult, AgentEvent, } from './types';
@@ -1 +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,C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E,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,YAAY,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACzC,YAAY,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACzD,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 CHANGED
@@ -1,12 +1,17 @@
1
1
  "use strict";
2
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;
3
+ exports.traceScope = exports.JsonlTraceWriter = exports.NullTraceWriter = exports.withSharedRoot = exports.initAgents = exports.createToolkit = exports.runAgents = exports.ContextPressure = exports.useAgentPool = exports.diverge = exports.generate = exports.buildToolResultDelta = exports.buildUserDelta = exports.Source = exports.Tool = exports.ScratchpadParent = exports.TraceParent = exports.Trace = exports.Events = exports.Store = exports.Ctx = void 0;
4
4
  var context_1 = require("./context");
5
5
  Object.defineProperty(exports, "Ctx", { enumerable: true, get: function () { return context_1.Ctx; } });
6
6
  Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return context_1.Store; } });
7
7
  Object.defineProperty(exports, "Events", { enumerable: true, get: function () { return context_1.Events; } });
8
+ Object.defineProperty(exports, "Trace", { enumerable: true, get: function () { return context_1.Trace; } });
9
+ Object.defineProperty(exports, "TraceParent", { enumerable: true, get: function () { return context_1.TraceParent; } });
10
+ Object.defineProperty(exports, "ScratchpadParent", { enumerable: true, get: function () { return context_1.ScratchpadParent; } });
8
11
  var Tool_1 = require("./Tool");
9
12
  Object.defineProperty(exports, "Tool", { enumerable: true, get: function () { return Tool_1.Tool; } });
13
+ var source_1 = require("./source");
14
+ Object.defineProperty(exports, "Source", { enumerable: true, get: function () { return source_1.Source; } });
10
15
  var sdk_1 = require("@lloyal-labs/sdk");
11
16
  Object.defineProperty(exports, "buildUserDelta", { enumerable: true, get: function () { return sdk_1.buildUserDelta; } });
12
17
  Object.defineProperty(exports, "buildToolResultDelta", { enumerable: true, get: function () { return sdk_1.buildToolResultDelta; } });
@@ -25,4 +30,9 @@ var init_1 = require("./init");
25
30
  Object.defineProperty(exports, "initAgents", { enumerable: true, get: function () { return init_1.initAgents; } });
26
31
  var shared_root_1 = require("./shared-root");
27
32
  Object.defineProperty(exports, "withSharedRoot", { enumerable: true, get: function () { return shared_root_1.withSharedRoot; } });
33
+ var trace_writer_1 = require("./trace-writer");
34
+ Object.defineProperty(exports, "NullTraceWriter", { enumerable: true, get: function () { return trace_writer_1.NullTraceWriter; } });
35
+ Object.defineProperty(exports, "JsonlTraceWriter", { enumerable: true, get: function () { return trace_writer_1.JsonlTraceWriter; } });
36
+ var trace_scope_1 = require("./trace-scope");
37
+ Object.defineProperty(exports, "traceScope", { enumerable: true, get: function () { return trace_scope_1.traceScope; } });
28
38
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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,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.ts CHANGED
@@ -3,6 +3,7 @@ import { BranchStore } from '@lloyal-labs/sdk';
3
3
  import { Session } from '@lloyal-labs/sdk';
4
4
  import type { SessionContext } from '@lloyal-labs/sdk';
5
5
  import type { AgentEvent } from './types';
6
+ import type { TraceWriter } from './trace-writer';
6
7
  /**
7
8
  * Handle returned by {@link initAgents} containing all agent resources
8
9
  *
@@ -54,5 +55,7 @@ export interface AgentHandle<E = AgentEvent> {
54
55
  *
55
56
  * @category Agents
56
57
  */
57
- export declare function initAgents<E = AgentEvent>(ctx: SessionContext): Operation<AgentHandle<E>>;
58
+ export declare function initAgents<E = AgentEvent>(ctx: SessionContext, opts?: {
59
+ traceWriter?: TraceWriter;
60
+ }): Operation<AgentHandle<E>>;
58
61
  //# sourceMappingURL=init.d.ts.map
@@ -1 +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"}
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;AAC1C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAGlD;;;;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,EACnB,IAAI,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,WAAW,CAAA;CAAE,GACnC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAkB3B"}
package/dist/init.js CHANGED
@@ -5,6 +5,7 @@ const effection_1 = require("effection");
5
5
  const sdk_1 = require("@lloyal-labs/sdk");
6
6
  const sdk_2 = require("@lloyal-labs/sdk");
7
7
  const context_1 = require("./context");
8
+ const trace_writer_1 = require("./trace-writer");
8
9
  /**
9
10
  * Bootstrap the agent infrastructure and register structured cleanup
10
11
  *
@@ -41,14 +42,17 @@ const context_1 = require("./context");
41
42
  *
42
43
  * @category Agents
43
44
  */
44
- function* initAgents(ctx) {
45
+ function* initAgents(ctx, opts) {
45
46
  const store = new sdk_1.BranchStore(ctx);
46
47
  const session = new sdk_2.Session({ ctx, store });
47
48
  const events = (0, effection_1.createChannel)();
48
49
  yield* context_1.Ctx.set(ctx);
49
50
  yield* context_1.Store.set(store);
50
51
  yield* context_1.Events.set(events);
52
+ yield* context_1.Trace.set(opts?.traceWriter ?? new trace_writer_1.NullTraceWriter());
51
53
  yield* (0, effection_1.ensure)(function* () {
54
+ const tw = yield* context_1.Trace.expect();
55
+ tw.flush();
52
56
  yield* (0, effection_1.call)(() => session.dispose());
53
57
  ctx.dispose();
54
58
  });
package/dist/init.js.map CHANGED
@@ -1 +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,EA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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"}
@@ -1,6 +1,6 @@
1
- import type { Operation } from 'effection';
2
- import { Branch } from '@lloyal-labs/sdk';
3
- import type { SamplingParams } from './types';
1
+ import type { Operation } from "effection";
2
+ import { Branch } from "@lloyal-labs/sdk";
3
+ import type { SamplingParams } from "./types";
4
4
  /**
5
5
  * Configuration for {@link withSharedRoot}
6
6
  *
@@ -1 +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"}
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;AAI1C,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,CAkFd"}
@@ -4,6 +4,7 @@ exports.withSharedRoot = withSharedRoot;
4
4
  const effection_1 = require("effection");
5
5
  const sdk_1 = require("@lloyal-labs/sdk");
6
6
  const context_1 = require("./context");
7
+ const trace_scope_1 = require("./trace-scope");
7
8
  /**
8
9
  * Scoped shared root branch with guaranteed cleanup
9
10
  *
@@ -43,20 +44,80 @@ const context_1 = require("./context");
43
44
  */
44
45
  function* withSharedRoot(opts, body) {
45
46
  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 };
47
+ const tw = yield* context_1.Trace.expect();
48
+ // Read parent trace ID — connects nested pools to the outer DISPATCH that spawned them
49
+ let parentTraceId = null;
50
+ try {
51
+ const p = yield* context_1.TraceParent.get();
52
+ if (p != null)
53
+ parentTraceId = p;
54
+ }
55
+ catch {
56
+ /* no parent — top level */
57
+ }
58
+ const scope = (0, trace_scope_1.traceScope)(tw, parentTraceId, "withSharedRoot", {
59
+ hasTools: !!opts.tools,
60
+ systemPromptLength: opts.systemPrompt.length,
61
+ });
62
+ const messages = [{ role: "system", content: opts.systemPrompt }];
63
+ const fmtOpts = {
64
+ addGenerationPrompt: false,
65
+ enableThinking: false,
66
+ };
67
+ if (opts.tools)
68
+ fmtOpts.tools = opts.tools;
50
69
  const fmt = ctx.formatChatSync(JSON.stringify(messages), fmtOpts);
51
70
  const sharedTokens = ctx.tokenizeSync(fmt.prompt);
71
+ tw.write({
72
+ traceId: tw.nextId(),
73
+ parentTraceId: scope.traceId,
74
+ ts: performance.now(),
75
+ type: "prompt:format",
76
+ promptText: fmt.prompt,
77
+ tokenCount: sharedTokens.length,
78
+ messages: JSON.stringify(messages),
79
+ tools: opts.tools,
80
+ grammar: fmt.grammar || undefined,
81
+ role: "sharedRoot",
82
+ });
52
83
  const root = sdk_1.Branch.create(ctx, 0, opts.params ?? { temperature: 0.5 });
84
+ tw.write({
85
+ traceId: tw.nextId(),
86
+ parentTraceId: scope.traceId,
87
+ ts: performance.now(),
88
+ type: "branch:create",
89
+ branchHandle: root.handle,
90
+ parentHandle: null,
91
+ position: 0,
92
+ role: "sharedRoot",
93
+ });
53
94
  yield* (0, effection_1.call)(() => root.prefill(sharedTokens));
95
+ tw.write({
96
+ traceId: tw.nextId(),
97
+ parentTraceId: scope.traceId,
98
+ ts: performance.now(),
99
+ type: "branch:prefill",
100
+ branchHandle: root.handle,
101
+ tokenCount: sharedTokens.length,
102
+ role: "sharedPrefix",
103
+ });
54
104
  try {
105
+ yield* context_1.ScratchpadParent.set(root);
55
106
  return yield* body(root, sharedTokens.length);
56
107
  }
57
108
  finally {
58
- if (!root.disposed)
109
+ if (!root.disposed) {
110
+ tw.write({
111
+ traceId: tw.nextId(),
112
+ parentTraceId: scope.traceId,
113
+ ts: performance.now(),
114
+ type: "branch:prune",
115
+ branchHandle: root.handle,
116
+ position: 0,
117
+ });
59
118
  root.pruneSubtreeSync();
119
+ }
120
+ scope.close();
60
121
  }
61
122
  }
62
123
  //# sourceMappingURL=shared-root.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared-root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AAE,EAAE,WAAW,CAAC,GAAG,EAAE;QACrB,IAAI,EAAE,gBAAgB;QACtB,YAAY,EAAE,IAAI,CAAC,MAAM;QACzB,UAAU,EAAE,YAAY,CAAC,MAAM;QAC/B,IAAI,EAAE,cAAc;KACrB,CAAC,CAAC;IAEH,IAAI,CAAC;QACH,KAAK,CAAC,CAAC,0BAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAClC,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC;YAAS,CAAC;QACT,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,EAAE,CAAC,KAAK,CAAC;gBACP,OAAO,EAAE,EAAE,CAAC,MAAM,EAAE;gBACpB,aAAa,EAAE,KAAK,CAAC,OAAO;gBAC5B,EAAE,EAAE,WAAW,CAAC,GAAG,EAAE;gBACrB,IAAI,EAAE,cAAc;gBACpB,YAAY,EAAE,IAAI,CAAC,MAAM;gBACzB,QAAQ,EAAE,CAAC;aACZ,CAAC,CAAC;YACH,IAAI,CAAC,gBAAgB,EAAE,CAAC;QAC1B,CAAC;QACD,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;AACH,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { Operation } from 'effection';
2
+ import type { Tool } from './Tool';
3
+ /**
4
+ * Abstract base class for data sources usable by the research pipeline
5
+ *
6
+ * Each source builds its own atomic research tool — a self-contained
7
+ * swarm with source-specific prompt, toolkit, and self-referential
8
+ * recursion. The orchestrator sees only source research tools + report.
9
+ *
10
+ * @typeParam TCtx - Runtime context passed to {@link bind} (e.g. parent branch, reranker)
11
+ * @typeParam TChunk - Chunk type returned by {@link getChunks} for post-research reranking
12
+ *
13
+ * @category Agents
14
+ */
15
+ export declare abstract class Source<TCtx = Record<string, unknown>, TChunk = unknown> {
16
+ /** Human-readable source name (e.g. 'web', 'corpus') for labeling findings */
17
+ abstract readonly name: string;
18
+ /** The configured research tool — atomic swarm with source-specific prompt + toolkit */
19
+ abstract get researchTool(): Tool;
20
+ /** Late-bind runtime deps not available at construction. Called before tools are used. */
21
+ bind(_ctx: TCtx): Operation<void>;
22
+ /** Post-research chunks for reranking. Called after research completes. */
23
+ getChunks(): TChunk[];
24
+ /** Grounding tools for independent verification (e.g. search, read_file, grep). Empty by default. */
25
+ get groundingTools(): Tool[];
26
+ }
27
+ //# sourceMappingURL=source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source.d.ts","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC;;;;;;;;;;;GAWG;AACH,8BAAsB,MAAM,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO;IAC3E,8EAA8E;IAC9E,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAC/B,wFAAwF;IACxF,QAAQ,KAAK,YAAY,IAAI,IAAI,CAAC;IAElC,0FAA0F;IACzF,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC;IAClC,2EAA2E;IAC3E,SAAS,IAAI,MAAM,EAAE;IACrB,qGAAqG;IACrG,IAAI,cAAc,IAAI,IAAI,EAAE,CAAe;CAC5C"}
package/dist/source.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Source = void 0;
4
+ /**
5
+ * Abstract base class for data sources usable by the research pipeline
6
+ *
7
+ * Each source builds its own atomic research tool — a self-contained
8
+ * swarm with source-specific prompt, toolkit, and self-referential
9
+ * recursion. The orchestrator sees only source research tools + report.
10
+ *
11
+ * @typeParam TCtx - Runtime context passed to {@link bind} (e.g. parent branch, reranker)
12
+ * @typeParam TChunk - Chunk type returned by {@link getChunks} for post-research reranking
13
+ *
14
+ * @category Agents
15
+ */
16
+ class Source {
17
+ /** Late-bind runtime deps not available at construction. Called before tools are used. */
18
+ *bind(_ctx) { }
19
+ /** Post-research chunks for reranking. Called after research completes. */
20
+ getChunks() { return []; }
21
+ /** Grounding tools for independent verification (e.g. search, read_file, grep). Empty by default. */
22
+ get groundingTools() { return []; }
23
+ }
24
+ exports.Source = Source;
25
+ //# sourceMappingURL=source.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source.js","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":";;;AAGA;;;;;;;;;;;GAWG;AACH,MAAsB,MAAM;IAM1B,0FAA0F;IAC1F,CAAC,IAAI,CAAC,IAAU,IAAoB,CAAC;IACrC,2EAA2E;IAC3E,SAAS,KAAe,OAAO,EAAE,CAAC,CAAC,CAAC;IACpC,qGAAqG;IACrG,IAAI,cAAc,KAAa,OAAO,EAAE,CAAC,CAAC,CAAC;CAC5C;AAZD,wBAYC"}
@@ -0,0 +1,25 @@
1
+ import type { TraceWriter } from './trace-writer';
2
+ import type { TraceId } from './trace-types';
3
+ /**
4
+ * Create matched `scope:open` / `scope:close` pairs for building the trace tree
5
+ *
6
+ * Opens a named scope immediately and returns a handle to close it later.
7
+ * The close callback emits a `scope:close` event with the elapsed duration
8
+ * and flushes the writer, ensuring scope boundaries are always persisted.
9
+ *
10
+ * Used throughout the runtime to bracket agent pools, tool dispatches,
11
+ * shared-root regions, and generation passes.
12
+ *
13
+ * @param writer - Active {@link TraceWriter} to emit events to
14
+ * @param parentTraceId - Trace ID of the enclosing scope, or `null` for root scopes
15
+ * @param name - Human-readable scope label (e.g. `"pool"`, `"tool:search"`)
16
+ * @param meta - Optional key-value metadata attached to the `scope:open` event
17
+ * @returns Object with the allocated `traceId` and a `close` callback
18
+ *
19
+ * @category Agents
20
+ */
21
+ export declare function traceScope(writer: TraceWriter, parentTraceId: TraceId | null, name: string, meta?: Record<string, unknown>): {
22
+ traceId: TraceId;
23
+ close: () => void;
24
+ };
25
+ //# sourceMappingURL=trace-scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace-scope.d.ts","sourceRoot":"","sources":["../src/trace-scope.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,WAAW,EACnB,aAAa,EAAE,OAAO,GAAG,IAAI,EAC7B,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,IAAI,CAAA;CAAE,CAkBzC"}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.traceScope = traceScope;
4
+ /**
5
+ * Create matched `scope:open` / `scope:close` pairs for building the trace tree
6
+ *
7
+ * Opens a named scope immediately and returns a handle to close it later.
8
+ * The close callback emits a `scope:close` event with the elapsed duration
9
+ * and flushes the writer, ensuring scope boundaries are always persisted.
10
+ *
11
+ * Used throughout the runtime to bracket agent pools, tool dispatches,
12
+ * shared-root regions, and generation passes.
13
+ *
14
+ * @param writer - Active {@link TraceWriter} to emit events to
15
+ * @param parentTraceId - Trace ID of the enclosing scope, or `null` for root scopes
16
+ * @param name - Human-readable scope label (e.g. `"pool"`, `"tool:search"`)
17
+ * @param meta - Optional key-value metadata attached to the `scope:open` event
18
+ * @returns Object with the allocated `traceId` and a `close` callback
19
+ *
20
+ * @category Agents
21
+ */
22
+ function traceScope(writer, parentTraceId, name, meta) {
23
+ const traceId = writer.nextId();
24
+ const ts = performance.now();
25
+ writer.write({
26
+ traceId, parentTraceId, ts,
27
+ type: 'scope:open', name, meta,
28
+ });
29
+ return {
30
+ traceId,
31
+ close() {
32
+ writer.write({
33
+ traceId: writer.nextId(), parentTraceId: traceId,
34
+ ts: performance.now(),
35
+ type: 'scope:close', name, durationMs: performance.now() - ts,
36
+ });
37
+ writer.flush();
38
+ },
39
+ };
40
+ }
41
+ //# sourceMappingURL=trace-scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace-scope.js","sourceRoot":"","sources":["../src/trace-scope.ts"],"names":[],"mappings":";;AAqBA,gCAuBC;AAzCD;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,UAAU,CACxB,MAAmB,EACnB,aAA6B,EAC7B,IAAY,EACZ,IAA8B;IAE9B,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;IAChC,MAAM,EAAE,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAC7B,MAAM,CAAC,KAAK,CAAC;QACX,OAAO,EAAE,aAAa,EAAE,EAAE;QAC1B,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,IAAI;KAC/B,CAAC,CAAC;IACH,OAAO;QACL,OAAO;QACP,KAAK;YACH,MAAM,CAAC,KAAK,CAAC;gBACX,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,aAAa,EAAE,OAAO;gBAChD,EAAE,EAAE,WAAW,CAAC,GAAG,EAAE;gBACrB,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE;aAC9D,CAAC,CAAC;YACH,MAAM,CAAC,KAAK,EAAE,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC"}