@autumnsgrove/lyra 0.0.0-stage → 0.1.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 (178) hide show
  1. package/dist/adapters/cloudflare/index.d.ts +11 -0
  2. package/dist/adapters/cloudflare/index.d.ts.map +1 -0
  3. package/dist/adapters/cloudflare/index.js +11 -0
  4. package/dist/adapters/cloudflare/index.js.map +1 -0
  5. package/dist/adapters/cloudflare/secrets.d.ts +15 -0
  6. package/dist/adapters/cloudflare/secrets.d.ts.map +1 -0
  7. package/dist/adapters/cloudflare/secrets.js +24 -0
  8. package/dist/adapters/cloudflare/secrets.js.map +1 -0
  9. package/dist/adapters/cloudflare/storage.d.ts +43 -0
  10. package/dist/adapters/cloudflare/storage.d.ts.map +1 -0
  11. package/dist/adapters/cloudflare/storage.js +89 -0
  12. package/dist/adapters/cloudflare/storage.js.map +1 -0
  13. package/dist/adapters/cloudflare/waking.d.ts +34 -0
  14. package/dist/adapters/cloudflare/waking.d.ts.map +1 -0
  15. package/dist/adapters/cloudflare/waking.js +63 -0
  16. package/dist/adapters/cloudflare/waking.js.map +1 -0
  17. package/dist/adapters/memory/ids.d.ts +12 -0
  18. package/dist/adapters/memory/ids.d.ts.map +1 -0
  19. package/dist/adapters/memory/ids.js +20 -0
  20. package/dist/adapters/memory/ids.js.map +1 -0
  21. package/dist/adapters/memory/index.d.ts +13 -0
  22. package/dist/adapters/memory/index.d.ts.map +1 -0
  23. package/dist/adapters/memory/index.js +13 -0
  24. package/dist/adapters/memory/index.js.map +1 -0
  25. package/dist/adapters/memory/secrets.d.ts +14 -0
  26. package/dist/adapters/memory/secrets.d.ts.map +1 -0
  27. package/dist/adapters/memory/secrets.js +19 -0
  28. package/dist/adapters/memory/secrets.js.map +1 -0
  29. package/dist/adapters/memory/storage.d.ts +24 -0
  30. package/dist/adapters/memory/storage.d.ts.map +1 -0
  31. package/dist/adapters/memory/storage.js +40 -0
  32. package/dist/adapters/memory/storage.js.map +1 -0
  33. package/dist/adapters/memory/waking.d.ts +21 -0
  34. package/dist/adapters/memory/waking.d.ts.map +1 -0
  35. package/dist/adapters/memory/waking.js +29 -0
  36. package/dist/adapters/memory/waking.js.map +1 -0
  37. package/dist/core/clock.d.ts +22 -0
  38. package/dist/core/clock.d.ts.map +1 -0
  39. package/dist/core/clock.js +10 -0
  40. package/dist/core/clock.js.map +1 -0
  41. package/dist/core/conversation.d.ts +28 -0
  42. package/dist/core/conversation.d.ts.map +1 -0
  43. package/dist/core/conversation.js +9 -0
  44. package/dist/core/conversation.js.map +1 -0
  45. package/dist/core/decode-reply.d.ts +28 -0
  46. package/dist/core/decode-reply.d.ts.map +1 -0
  47. package/dist/core/decode-reply.js +158 -0
  48. package/dist/core/decode-reply.js.map +1 -0
  49. package/dist/core/define-recipe.d.ts +54 -0
  50. package/dist/core/define-recipe.d.ts.map +1 -0
  51. package/dist/core/define-recipe.js +96 -0
  52. package/dist/core/define-recipe.js.map +1 -0
  53. package/dist/core/events.d.ts +98 -0
  54. package/dist/core/events.d.ts.map +1 -0
  55. package/dist/core/events.js +14 -0
  56. package/dist/core/events.js.map +1 -0
  57. package/dist/core/execute-tools.d.ts +61 -0
  58. package/dist/core/execute-tools.d.ts.map +1 -0
  59. package/dist/core/execute-tools.js +147 -0
  60. package/dist/core/execute-tools.js.map +1 -0
  61. package/dist/core/finish.d.ts +53 -0
  62. package/dist/core/finish.d.ts.map +1 -0
  63. package/dist/core/finish.js +106 -0
  64. package/dist/core/finish.js.map +1 -0
  65. package/dist/core/handles.d.ts +34 -0
  66. package/dist/core/handles.d.ts.map +1 -0
  67. package/dist/core/handles.js +81 -0
  68. package/dist/core/handles.js.map +1 -0
  69. package/dist/core/json.d.ts +19 -0
  70. package/dist/core/json.d.ts.map +1 -0
  71. package/dist/core/json.js +41 -0
  72. package/dist/core/json.js.map +1 -0
  73. package/dist/core/limits.d.ts +53 -0
  74. package/dist/core/limits.d.ts.map +1 -0
  75. package/dist/core/limits.js +81 -0
  76. package/dist/core/limits.js.map +1 -0
  77. package/dist/core/model.d.ts +66 -0
  78. package/dist/core/model.d.ts.map +1 -0
  79. package/dist/core/model.js +10 -0
  80. package/dist/core/model.js.map +1 -0
  81. package/dist/core/recipe.d.ts +45 -0
  82. package/dist/core/recipe.d.ts.map +1 -0
  83. package/dist/core/recipe.js +102 -0
  84. package/dist/core/recipe.js.map +1 -0
  85. package/dist/core/result.d.ts +38 -0
  86. package/dist/core/result.d.ts.map +1 -0
  87. package/dist/core/result.js +35 -0
  88. package/dist/core/result.js.map +1 -0
  89. package/dist/core/resume.d.ts +71 -0
  90. package/dist/core/resume.d.ts.map +1 -0
  91. package/dist/core/resume.js +133 -0
  92. package/dist/core/resume.js.map +1 -0
  93. package/dist/core/run-handle.d.ts +39 -0
  94. package/dist/core/run-handle.d.ts.map +1 -0
  95. package/dist/core/run-handle.js +101 -0
  96. package/dist/core/run-handle.js.map +1 -0
  97. package/dist/core/run.d.ts +136 -0
  98. package/dist/core/run.d.ts.map +1 -0
  99. package/dist/core/run.js +1095 -0
  100. package/dist/core/run.js.map +1 -0
  101. package/dist/core/runtime.d.ts +81 -0
  102. package/dist/core/runtime.d.ts.map +1 -0
  103. package/dist/core/runtime.js +201 -0
  104. package/dist/core/runtime.js.map +1 -0
  105. package/dist/core/schema.d.ts +119 -0
  106. package/dist/core/schema.d.ts.map +1 -0
  107. package/dist/core/schema.js +469 -0
  108. package/dist/core/schema.js.map +1 -0
  109. package/dist/core/slots.d.ts +32 -0
  110. package/dist/core/slots.d.ts.map +1 -0
  111. package/dist/core/slots.js +10 -0
  112. package/dist/core/slots.js.map +1 -0
  113. package/dist/core/state.d.ts +18 -0
  114. package/dist/core/state.d.ts.map +1 -0
  115. package/dist/core/state.js +53 -0
  116. package/dist/core/state.js.map +1 -0
  117. package/dist/core/system-clock.d.ts +17 -0
  118. package/dist/core/system-clock.d.ts.map +1 -0
  119. package/dist/core/system-clock.js +59 -0
  120. package/dist/core/system-clock.js.map +1 -0
  121. package/dist/core/tool.d.ts +74 -0
  122. package/dist/core/tool.d.ts.map +1 -0
  123. package/dist/core/tool.js +48 -0
  124. package/dist/core/tool.js.map +1 -0
  125. package/dist/index.d.ts +35 -0
  126. package/dist/index.d.ts.map +1 -0
  127. package/dist/index.js +35 -0
  128. package/dist/index.js.map +1 -0
  129. package/dist/record/entries.d.ts +152 -0
  130. package/dist/record/entries.d.ts.map +1 -0
  131. package/dist/record/entries.js +497 -0
  132. package/dist/record/entries.js.map +1 -0
  133. package/dist/record/load.d.ts +29 -0
  134. package/dist/record/load.d.ts.map +1 -0
  135. package/dist/record/load.js +106 -0
  136. package/dist/record/load.js.map +1 -0
  137. package/dist/record/replay.d.ts +16 -0
  138. package/dist/record/replay.d.ts.map +1 -0
  139. package/dist/record/replay.js +63 -0
  140. package/dist/record/replay.js.map +1 -0
  141. package/dist/record/truncate.d.ts +25 -0
  142. package/dist/record/truncate.d.ts.map +1 -0
  143. package/dist/record/truncate.js +167 -0
  144. package/dist/record/truncate.js.map +1 -0
  145. package/dist/testkit/clock.d.ts +19 -0
  146. package/dist/testkit/clock.d.ts.map +1 -0
  147. package/dist/testkit/clock.js +77 -0
  148. package/dist/testkit/clock.js.map +1 -0
  149. package/dist/testkit/crashing-storage.d.ts +23 -0
  150. package/dist/testkit/crashing-storage.d.ts.map +1 -0
  151. package/dist/testkit/crashing-storage.js +39 -0
  152. package/dist/testkit/crashing-storage.js.map +1 -0
  153. package/dist/testkit/index.d.ts +15 -0
  154. package/dist/testkit/index.d.ts.map +1 -0
  155. package/dist/testkit/index.js +15 -0
  156. package/dist/testkit/index.js.map +1 -0
  157. package/dist/testkit/model.d.ts +53 -0
  158. package/dist/testkit/model.d.ts.map +1 -0
  159. package/dist/testkit/model.js +80 -0
  160. package/dist/testkit/model.js.map +1 -0
  161. package/dist/testkit/scenario.d.ts +55 -0
  162. package/dist/testkit/scenario.d.ts.map +1 -0
  163. package/dist/testkit/scenario.js +505 -0
  164. package/dist/testkit/scenario.js.map +1 -0
  165. package/dist/testkit/storage.d.ts +18 -0
  166. package/dist/testkit/storage.d.ts.map +1 -0
  167. package/dist/testkit/storage.js +27 -0
  168. package/dist/testkit/storage.js.map +1 -0
  169. package/dist/testkit/timeline-recipe.d.ts +59 -0
  170. package/dist/testkit/timeline-recipe.d.ts.map +1 -0
  171. package/dist/testkit/timeline-recipe.js +322 -0
  172. package/dist/testkit/timeline-recipe.js.map +1 -0
  173. package/dist/testkit/tools.d.ts +47 -0
  174. package/dist/testkit/tools.d.ts.map +1 -0
  175. package/dist/testkit/tools.js +82 -0
  176. package/dist/testkit/tools.js.map +1 -0
  177. package/package.json +42 -5
  178. package/README.md +0 -3
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Running one reply's tool calls, in order (docs/core-loop.md, LOOP02 and LOOP09).
3
+ *
4
+ * Everything a model can get wrong arrives here: arguments that are not JSON,
5
+ * a tool that does not exist, the same call id twice, a tool that throws, and one
6
+ * that never finishes. Each becomes an error result the model reads and corrects
7
+ * (decision 21), never a crash, and every call in the batch gets an answer so the
8
+ * record stays paired.
9
+ *
10
+ * A call runs under a deadline measured by the injected clock, and its own abort
11
+ * signal fires on that deadline or when the run stops, so a tool that waits can
12
+ * stop waiting.
13
+ */
14
+ import { isPlainObject } from "./json.js";
15
+ import { formatErrors } from "./schema.js";
16
+ import {} from "./tool.js";
17
+ /** A model's arguments, or the reason they cannot be used. */
18
+ export function decodeArguments(raw) {
19
+ let parsed;
20
+ try {
21
+ parsed = JSON.parse(raw);
22
+ }
23
+ catch {
24
+ return { ok: false, reason: "its arguments are not valid JSON" };
25
+ }
26
+ if (!isPlainObject(parsed)) {
27
+ // `null`, a list and a bare value are all valid JSON and all wrong here.
28
+ return { ok: false, reason: "its arguments are not a JSON object" };
29
+ }
30
+ return { ok: true, value: parsed };
31
+ }
32
+ function errorResult(content) {
33
+ return { isError: true, content };
34
+ }
35
+ /**
36
+ * The arguments a tool may run with: JSON, and the tool's own schema's word.
37
+ *
38
+ * It is one function because the live path and the replay path have to agree, and
39
+ * two copies of "is this call usable" would drift the moment one changed.
40
+ */
41
+ export function usableArguments(tool, raw) {
42
+ const decoded = decodeArguments(raw);
43
+ if (!decoded.ok) {
44
+ return { ok: false, reason: decoded.reason, invalid: false };
45
+ }
46
+ if (tool.validate === undefined) {
47
+ return { ok: true, value: decoded.value };
48
+ }
49
+ const checked = tool.validate(decoded.value);
50
+ if (!checked.ok) {
51
+ return { ok: false, reason: formatErrors(checked.errors), invalid: true };
52
+ }
53
+ return { ok: true, value: checked.value };
54
+ }
55
+ export async function executeTools(options) {
56
+ const byName = new Map(options.tools.map((tool) => [tool.spec.name, tool]));
57
+ const available = [...byName.keys()].join(", ");
58
+ for (const call of options.calls) {
59
+ // The calls arrive with unique, non-empty ids: the loop gives the model's
60
+ // unusable ones a name before anything is written (`normalizeCalls`).
61
+ if (options.isFinished()) {
62
+ await options.onFinish(call, errorResult("the run already finished, so this call was not run"));
63
+ continue;
64
+ }
65
+ if (options.signal.aborted) {
66
+ // A stopped run does not start more work, but every call still gets an
67
+ // answer, so the record stays paired (LOOP06).
68
+ await options.onFinish(call, errorResult("the run was stopped before this call ran"));
69
+ continue;
70
+ }
71
+ if (call.name === "") {
72
+ await options.onFinish(call, errorResult(`a tool call with no name was not run; ${available === "" ? "no tools are available" : `the tools are ${available}`}`));
73
+ continue;
74
+ }
75
+ const tool = byName.get(call.name);
76
+ if (tool === undefined) {
77
+ const names = available === "" ? "no tools are available" : `the tools are ${available}`;
78
+ await options.onFinish(call, errorResult(`there is no tool called "${call.name.slice(0, 60)}"; ${names}`));
79
+ continue;
80
+ }
81
+ // A tool's own schema is the boundary its arguments cross (decision 21). A
82
+ // refused call never reaches the tool, and the model gets the specific
83
+ // message it needs to correct itself (TOL02).
84
+ const args = usableArguments(tool, call.arguments);
85
+ if (!args.ok) {
86
+ await options.onFinish(call, {
87
+ isError: true,
88
+ content: `the call to ${call.name} was not run because ${args.reason}`,
89
+ ...(args.invalid ? { details: { code: "bad_request" } } : {}),
90
+ });
91
+ continue;
92
+ }
93
+ options.onStart(call);
94
+ const outcome = await runOne(tool, args.value, call.id, options);
95
+ await options.onFinish(call, outcome, args.value);
96
+ }
97
+ }
98
+ /**
99
+ * One tool, under its deadline. The result is always an outcome: a throw becomes
100
+ * an error result, and a call that outstays its deadline does too.
101
+ */
102
+ async function runOne(tool, args, callId, options) {
103
+ const call = new AbortController();
104
+ const onRunAbort = () => {
105
+ call.abort();
106
+ };
107
+ options.signal.addEventListener("abort", onRunAbort, { once: true });
108
+ // The stop may have landed between this call being announced and this listener
109
+ // being attached, and a listener added afterwards never fires for it. The
110
+ // scenario file for a stop during a tool found exactly that gap.
111
+ if (options.signal.aborted) {
112
+ call.abort();
113
+ }
114
+ // The sleep is cancelled through the same signal, so a call that finishes
115
+ // first leaves no timer behind (`clock.pending()` is how a test checks).
116
+ const expired = options.clock
117
+ .sleep(Math.max(0, options.deadlineMs), call.signal)
118
+ .then(() => "expired",
119
+ // The sleep is cancelled either by the deadline or by the run stopping.
120
+ () => "stopped");
121
+ const ran = Promise.resolve()
122
+ .then(() => tool.execute(args, {
123
+ signal: call.signal,
124
+ runId: options.runId,
125
+ callId,
126
+ callKey: `${options.runId}:${callId}`,
127
+ state: options.readState?.(),
128
+ }))
129
+ .then((outcome) => ({ kind: "outcome", outcome }), (error) => ({ kind: "threw", error }));
130
+ const first = await Promise.race([expired, ran]);
131
+ call.abort();
132
+ options.signal.removeEventListener("abort", onRunAbort);
133
+ if (first === "expired") {
134
+ return errorResult(`${tool.spec.name} ran past its deadline of ${options.deadlineMs}ms and was stopped`);
135
+ }
136
+ if (first === "stopped") {
137
+ return errorResult(`${tool.spec.name} was stopped before it finished, so its outcome is unknown`);
138
+ }
139
+ if (first.kind === "threw") {
140
+ // A tool that throws is a tool failure, not a run failure. The message is
141
+ // the tool's own; a stack would be noise for the model.
142
+ const message = first.error instanceof Error ? first.error.message : String(first.error);
143
+ return errorResult(`${tool.spec.name} failed: ${message}`);
144
+ }
145
+ return first.outcome;
146
+ }
147
+ //# sourceMappingURL=execute-tools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execute-tools.js","sourceRoot":"","sources":["../../src/core/execute-tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAA+B,MAAM,WAAW,CAAC;AAuBxD,8DAA8D;AAC9D,MAAM,UAAU,eAAe,CAC9B,GAAW;IAEX,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACJ,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,kCAAkC,EAAE,CAAC;IAClE,CAAC;IACD,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,yEAAyE;QACzE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,qCAAqC,EAAE,CAAC;IACrE,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;AACpC,CAAC;AAED,SAAS,WAAW,CAAC,OAAe;IACnC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AACnC,CAAC;AAKD;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,IAAU,EAAE,GAAW;IACtD,MAAM,OAAO,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;IACrC,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC;QACjB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC9D,CAAC;IACD,IAAI,IAAI,CAAC,Q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1,53 @@
1
+ /**
2
+ * `finish`, and the coverage check that makes its word worth something
3
+ * (docs/recipes-and-tools.md, FIN01 to FIN06; decision 25).
4
+ *
5
+ * A run ends `done` when a tool answers with `done: true` and no error, so the
6
+ * recipe has to be the one to say what done means. `createFinishTool({ check })`
7
+ * takes that answer from the recipe's own state; a refusal is an ordinary error
8
+ * result carrying the message, which the model reads and works from, and three
9
+ * identical refusals park the run as stuck through step 2's safeguard instead of
10
+ * spending an allowance on a loop that cannot finish.
11
+ *
12
+ * `coverageCheck` is the shape almost every recipe wants: every handle the day
13
+ * produced is covered by a line, or left out with a stated reason. Its message is
14
+ * sorted, so the same missing set always reads the same way.
15
+ */
16
+ import type { Tool } from "./tool.js";
17
+ export type DoneCheck = {
18
+ ok: true;
19
+ } | {
20
+ ok: false;
21
+ message: string;
22
+ };
23
+ /** One thing the recipe agreed to leave out, and why. */
24
+ export interface Skipped {
25
+ handle: string;
26
+ reason: string;
27
+ }
28
+ /** The arguments `finish` takes: a list of what was left out, each with a reason. */
29
+ export interface FinishArguments {
30
+ skipped?: Skipped[];
31
+ }
32
+ export interface FinishOptions<S> {
33
+ /** Whether the job is really done, in the recipe's own terms, given its state. */
34
+ check(state: S, skipped: readonly Skipped[]): DoneCheck;
35
+ /** What an accepted finish tells the model. */
36
+ content?: string;
37
+ }
38
+ export declare function createFinishTool<S = unknown>(options: FinishOptions<S>): Tool;
39
+ export interface CoverageRequest {
40
+ /** The handles the task has to account for. */
41
+ expected: readonly string[];
42
+ /** The handles the state says are covered. */
43
+ covered: Iterable<string>;
44
+ /** What they are called in the message, such as `issues`. */
45
+ label: string;
46
+ /** Handles the recipe agreed to leave out, each with its reason. */
47
+ skipped?: readonly Skipped[];
48
+ /** How many missing handles the message names. Twelve by default. */
49
+ cap?: number;
50
+ }
51
+ /** "not covered yet: 4 issues (i3, i7, i9, i12)", with the missing set sorted. */
52
+ export declare function coverageCheck(request: CoverageRequest): DoneCheck;
53
+ //# sourceMappingURL=finish.d.ts.map
@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/**
2
+ * `finish`, and the coverage check that makes its word worth something
3
+ * (docs/recipes-and-tools.md, FIN01 to FIN06; decision 25).
4
+ *
5
+ * A run ends `done` when a tool answers with `done: true` and no error, so the
6
+ * recipe has to be the one to say what done means. `createFinishTool({ check })`
7
+ * takes that answer from the recipe's own state; a refusal is an ordinary error
8
+ * result carrying the message, which the model reads and works from, and three
9
+ * identical refusals park the run as stuck through step 2's safeguard instead of
10
+ * spending an allowance on a loop that cannot finish.
11
+ *
12
+ * `coverageCheck` is the shape almost every recipe wants: every handle the day
13
+ * produced is covered by a line, or left out with a stated reason. Its message is
14
+ * sorted, so the same missing set always reads the same way.
15
+ */
16
+ import { compileSchema, formatErrors, validateValue } from "./schema.js";
17
+ const PARAMETERS = {
18
+ type: "object",
19
+ properties: {
20
+ skipped: {
21
+ type: "array",
22
+ maxItems: 200,
23
+ items: {
24
+ type: "object",
25
+ properties: {
26
+ handle: { type: "string", minLength: 1, maxLength: 80 },
27
+ reason: { type: "string", maxLength: 500 },
28
+ },
29
+ required: ["handle", "reason"],
30
+ },
31
+ },
32
+ },
33
+ };
34
+ export function createFinishTool(options) {
35
+ const compiled = compileSchema(PARAMETERS);
36
+ const tool = {
37
+ spec: {
38
+ name: "finish",
39
+ description: "End the run when the job is really done. Everything the task must cover has to be covered by a line, or skipped with a reason.",
40
+ parameters: PARAMETERS,
41
+ },
42
+ // `defineRecipe` uses this to tell a real done-check from a tool that merely
43
+ // borrowed the name (RCP04).
44
+ finish: true,
45
+ // The check is pure, and a kill between the call and its result leaves the run
46
+ // with nothing that running it again could duplicate.
47
+ safeToRepeat: true,
48
+ validate: (args) => validateValue(compiled, args),
49
+ execute: async (args, ctx) => {
50
+ const checked = validateValue(compiled, args);
51
+ if (!checked.ok) {
52
+ return { isError: true, content: formatErrors(checked.errors) };
53
+ }
54
+ const skipped = checked.value.skipped ?? [];
55
+ for (const item of skipped) {
56
+ if (item.reason.trim() === "") {
57
+ // A skip without a reason is not a skip; it is a thing left out.
58
+ return {
59
+ isError: true,
60
+ content: `a skip needs a reason: say why ${item.handle} is left out of the page`,
61
+ };
62
+ }
63
+ }
64
+ const decision = options.check(ctx.state, skipped);
65
+ if (!decision.ok) {
66
+ return { isError: true, content: decision.message };
67
+ }
68
+ const outcome = {
69
+ isError: false,
70
+ content: options.content ?? "finished",
71
+ done: true,
72
+ };
73
+ if (skipped.length > 0) {
74
+ outcome.details = {
75
+ skipped: skipped.map((item) => ({ handle: item.handle, reason: item.reason })),
76
+ };
77
+ }
78
+ return outcome;
79
+ },
80
+ };
81
+ return tool;
82
+ }
83
+ const DEFAULT_COVERAGE_CAP = 12;
84
+ /** "not covered yet: 4 issues (i3, i7, i9, i12)", with the missing set sorted. */
85
+ export function coverageCheck(request) {
86
+ const accounted = new Set(request.covered);
87
+ for (const item of request.skipped ?? []) {
88
+ accounted.add(item.handle);
89
+ }
90
+ const missing = [...new Set(request.expected)]
91
+ .filter((handle) => !accounted.has(handle))
92
+ .sort((left, right) => (left < right ? -1 : left > right ? 1 : 0));
93
+ if (missing.length === 0) {
94
+ return { ok: true };
95
+ }
96
+ const cap = request.cap ?? DEFAULT_COVERAGE_CAP;
97
+ const shown = missing.slice(0, cap).join(", ");
98
+ const left = missing.length - cap;
99
+ const list = left > 0 ? `${shown}, and ${left} more` : shown;
100
+ // A label is written in the plural, because that is how a task names its world.
101
+ const noun = missing.length === 1 && request.label.endsWith("s")
102
+ ? request.label.slice(0, -1)
103
+ : request.label;
104
+ return { ok: false, message: `not covered yet: ${missing.length} ${noun} (${list})` };
105
+ }
106
+ //# sourceMappingURL=finish.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finish.js","sourceRoot":"","sources":["../../src/core/finish.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAgBzE,MAAM,UAAU,GAAe;IAC9B,IAAI,EAAE,QAAQ;IACd,UAAU,EAA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GAAG,EAAE,CAAC;AACvF,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Short names for things that have to be exact (docs/recipes-and-tools.md, HND01 to
3
+ * HND06; decision 114).
4
+ *
5
+ * A model should never copy a commit hash or a link: Lumen's scrubber may rewrite a
6
+ * long string on the way to the model (decision 8), and a copied hash is a typo
7
+ * waiting to happen. So a tool mints a handle such as `c1` or `pr12`, keeps the map
8
+ * back to the real value, and the model cites the handle. The table lives inside a
9
+ * recipe's state, so replaying the saved results rebuilds it exactly (decision 32).
10
+ *
11
+ * It is a `Map`, never a plain object, because a handle is a model's input: a key
12
+ * of `__proto__` or `constructor` must be an ordinary miss, not a prototype.
13
+ *
14
+ * Minting is deterministic: the same kind and canonical target always give the same
15
+ * handle, counters start at one per kind, and minting in the same order rebuilds the
16
+ * same table. That is what makes a resumed run's handles equal to a live run's.
17
+ */
18
+ import { type JsonValue } from "./json.js";
19
+ export interface HandleTable {
20
+ /** Name a target, or return the name it already has (HND02). */
21
+ mint(kind: string, target: JsonValue): string;
22
+ /** The real value behind a handle, or `undefined` for one this table never made. */
23
+ resolve(handle: string): JsonValue | undefined;
24
+ has(handle: string): boolean;
25
+ /** Every handle of a kind, or every handle, in the order it was minted. */
26
+ list(kind?: string): string[];
27
+ /** A short line naming what a model could have used, capped (HND04). */
28
+ describe(kind?: string, cap?: number): string;
29
+ size(): number;
30
+ }
31
+ export declare function createHandleTable(): HandleTable;
32
+ /** The message a tool gives a model that named a handle this table never made. */
33
+ export declare function unknownHandle(table: HandleTable, handle: string, kind?: string): string;
34
+ //# sourceMappingURL=handles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handles.d.ts","sourceRoot":"","sources":["../../src/core/handles.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAe,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AASxD,MAAM,WAAW,WAAW;IAC3B,gEAAgE;IAChE,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,GAAG,MAAM,CAAC;IAC9C,oFAAoF;IACpF,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAAC;IAC/C,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7B,2EAA2E;IAC3E,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9B,wEAAwE;IACxE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9C,IAAI,IAAI,MAAM,CAAC;CACf;AAED,wBAAgB,iBAAiB,IAAI,WAAW,CAwD/C;AAED,kFAAkF;AAClF,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAGvF"}
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Short names for things that have to be exact (docs/recipes-and-tools.md, HND01 to
3
+ * HND06; decision 114).
4
+ *
5
+ * A model should never copy a commit hash or a link: Lumen's scrubber may rewrite a
6
+ * long string on the way to the model (decision 8), and a copied hash is a typo
7
+ * waiting to happen. So a tool mints a handle such as `c1` or `pr12`, keeps the map
8
+ * back to the real value, and the model cites the handle. The table lives inside a
9
+ * recipe's state, so replaying the saved results rebuilds it exactly (decision 32).
10
+ *
11
+ * It is a `Map`, never a plain object, because a handle is a model's input: a key
12
+ * of `__proto__` or `constructor` must be an ordinary miss, not a prototype.
13
+ *
14
+ * Minting is deterministic: the same kind and canonical target always give the same
15
+ * handle, counters start at one per kind, and minting in the same order rebuilds the
16
+ * same table. That is what makes a resumed run's handles equal to a live run's.
17
+ */
18
+ import { isJsonValue } from "./json.js";
19
+ import { canonicalJson } from "./recipe.js";
20
+ /** The most names one error message lists before it says how many are left. */
21
+ const DEFAULT_LIST_CAP = 10;
22
+ /** A handle's shape, as a rule a tool author can rely on. */
23
+ const KIND = /^[a-z]{1,8}$/;
24
+ export function createHandleTable() {
25
+ const byHandle = new Map();
26
+ const byTarget = new Map();
27
+ const counters = new Map();
28
+ const minted = [];
29
+ const byKind = new Map();
30
+ return {
31
+ mint(kind, target) {
32
+ if (!KIND.test(kind)) {
33
+ // A kind is the tool author's own word, never the model's, so this is a
34
+ // programming error rather than a refusal the model reads.
35
+ throw new Error(`a handle kind must be one to eight lowercase letters, not "${kind}"`);
36
+ }
37
+ if (!isJsonValue(target)) {
38
+ throw new Error("a handle's target must be JSON, so replay can rebuild it");
39
+ }
40
+ const key = `${kind}\u0000${canonicalJson(target)}`;
41
+ const known = byTarget.get(key);
42
+ if (known !== undefined) {
43
+ return known;
44
+ }
45
+ const next = (counters.get(kind) ?? 0) + 1;
46
+ counters.set(kind, next);
47
+ const handle = `${kind}${next}`;
48
+ byHandle.set(handle, target);
49
+ byTarget.set(key, handle);
50
+ minted.push(handle);
51
+ const ofKind = byKind.get(kind);
52
+ if (ofKind === undefined) {
53
+ byKind.set(kind, [handle]);
54
+ }
55
+ else {
56
+ ofKind.push(handle);
57
+ }
58
+ return handle;
59
+ },
60
+ resolve: (handle) => byHandle.get(handle),
61
+ has: (handle) => byHandle.has(handle),
62
+ list: (kind) => [...(kind === undefined ? minted : (byKind.get(kind) ?? []))],
63
+ describe(kind, cap = DEFAULT_LIST_CAP) {
64
+ const names = kind === undefined ? minted : (byKind.get(kind) ?? []);
65
+ if (names.length === 0) {
66
+ return kind === undefined ? "no handles are known" : `no ${kind} handles are known`;
67
+ }
68
+ const shown = names.slice(0, cap).join(", ");
69
+ const left = names.length - cap;
70
+ const what = kind === undefined ? "the handles" : `the ${kind} handles`;
71
+ return left > 0 ? `${what} are ${shown}, and ${left} more` : `${what} are ${shown}`;
72
+ },
73
+ size: () => byHandle.size,
74
+ };
75
+ }
76
+ /** The message a tool gives a model that named a handle this table never made. */
77
+ export function unknownHandle(table, handle, kind) {
78
+ const shown = handle.length > 40 ? `${handle.slice(0, 40)}...` : handle;
79
+ return `there is no handle "${shown}"; ${table.describe(kind)}`;
80
+ }
81
+ //# sourceMappingURL=handles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handles.js","sourceRoot":"","sources":["../../src/core/handles.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,WAAW,EAAkB,MAAM,WAAW,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,+EAA+E;AAC/E,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,6DAA6D;AAC7D,MAAM,IAAI,GAAG,cAAc,CAAC;AAe5B,MAAM,UAAU,iBAAiB;IAChC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAqB,CAAC;IAC9C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC3C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC3C,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,MAAM,GAAG,IAAI,GAAG,EAAoB,CAAC;IAE3C,OAAO;QACN,IAAI,CAAC,IAAI,EAAE,MAAM;YAChB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;gBACtB,wEAAwE;gBACxE,2DAA2D;gBAC3D,MAAM,IAAI,KAAK,CACd,8DAA8D,IAAI,GAAG,CACrE,CAAC;YACH,CAAC;YACD,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC1B,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;YAC7E,CAAC;YACD,MAAM,GAAG,GAAG,GAAG,IAAI,SAAS,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;YACpD,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAChC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACzB,OAAO,KA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,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;YACrE,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACxB,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,CAAC,MAAM,IAAI,oBAAoB,CAAC;YACrF,CAAC;YACD,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC7C,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,GAAG,GAAG,CAAC;YAChC,MAAM,IAAI,GAAG,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,IAAI,UAAU,CAAC;YACxE,OAAO,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,QAAQ,KAAK,SAAS,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,QAAQ,KAAK,EAAE,CAAC;QACrF,CAAC;QAED,IAAI,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI;KACzB,CAAC;AACH,CAAC;AAED,kFAAkF;AAClF,MAAM,UAAU,aAAa,CAAC,KAAkB,EAAE,MAAc,EAAE,IAAa;IAC9E,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC;IACxE,OAAO,uBAAuB,KAAK,MAAM,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;AACjE,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * JSON as plain data.
3
+ *
4
+ * Tool arguments, tool specs and record details all cross a boundary as JSON,
5
+ * so they are typed as JSON rather than as `unknown` or `any`. A value that is
6
+ * not JSON cannot be declared, and the decoders in the modules that take
7
+ * outside data check the shape again at the edge.
8
+ */
9
+ export type JsonValue = string | number | boolean | null | readonly JsonValue[] | JsonObject;
10
+ export interface JsonObject {
11
+ [key: string]: JsonValue;
12
+ }
13
+ /**
14
+ * A plain object, told apart from `null`, a list and a primitive. This is the
15
+ * first check every decoder makes, so it lives beside the type it narrows to.
16
+ */
17
+ export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
18
+ export declare function isJsonValue(value: unknown, depth?: number): value is JsonValue;
19
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/core/json.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,SAAS,EAAE,GAAG,UAAU,CAAC;AAE7F,MAAM,WAAW,UAAU;IAC1B,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CACzB;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE9E;AAUD,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,SAAI,GAAG,KAAK,IAAI,SAAS,CAiBzE"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * JSON as plain data.
3
+ *
4
+ * Tool arguments, tool specs and record details all cross a boundary as JSON,
5
+ * so they are typed as JSON rather than as `unknown` or `any`. A value that is
6
+ * not JSON cannot be declared, and the decoders in the modules that take
7
+ * outside data check the shape again at the edge.
8
+ */
9
+ /**
10
+ * A plain object, told apart from `null`, a list and a primitive. This is the
11
+ * first check every decoder makes, so it lives beside the type it narrows to.
12
+ */
13
+ export function isPlainObject(value) {
14
+ return typeof value === "object" && value !== null && !Array.isArray(value);
15
+ }
16
+ /**
17
+ * A JSON value, checked all the way down. A tool's `details` is the only place
18
+ * outside data can be any shape, and it is stored in the record, so it has to be
19
+ * something that can be written back out. The depth cap keeps a hand-written or
20
+ * hostile document from recursing further than the decoder's own stack.
21
+ */
22
+ const MAX_JSON_DEPTH = 64;
23
+ export function isJsonValue(value, depth = 0) {
24
+ if (depth > MAX_JSON_DEPTH) {
25
+ return false;
26
+ }
27
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
28
+ return true;
29
+ }
30
+ if (typeof value === "number") {
31
+ return Number.isFinite(value);
32
+ }
33
+ if (Array.isArray(value)) {
34
+ return value.every((item) => isJsonValue(item, depth + 1));
35
+ }
36
+ if (isPlainObject(value)) {
37
+ return Object.values(value).every((item) => isJsonValue(item, depth + 1));
38
+ }
39
+ return false;
40
+ }
41
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../../src/core/json.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAQH;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC3C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC7E,CAAC;AAED;;;;;GAKG;AACH,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B,MAAM,UAAU,WAAW,CAAC,KAAc,EAAE,KAAK,GAAG,CAAC;IACpD,IAAI,KAAK,GAAG,cAAc,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC;IACd,CAAC;IACD,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;QAC/E,OAAO,IAAI,CAAC;IACb,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IAC5D,CAAC;IACD,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3E,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Limits (docs/limits-and-interruptions.md, LIM01 to LIM05 and LIM07; decisions
3
+ * 18 and 22).
4
+ *
5
+ * Every limit is optional and there is none by default. Reaching one parks the run
6
+ * rather than killing it, so the work done so far is kept and a person can resume
7
+ * it with a larger allowance. The check happens before each model request, which
8
+ * is the only place a run can pause without cutting a step in half.
9
+ *
10
+ * Time is active time, measured from a status on the injected clock, so a parked
11
+ * run costs nothing. A cost limit counts reported cost of any source; when a reply
12
+ * reports none the total is understated, so the limit stops being enforced for the
13
+ * run and the record says so once (decision 15).
14
+ */
15
+ import { type Result } from "./result.js";
16
+ export interface Limits {
17
+ /** Model turns. `3` allows three requests, and a run that ends on its third is done. */
18
+ steps?: number;
19
+ /** Input plus output tokens summed over the replies that arrived. */
20
+ tokens?: number;
21
+ /** Active milliseconds since the run's latest `running` or `resumed` status. */
22
+ timeMs?: number;
23
+ /** Reported cost, summed over the replies, whatever its source. */
24
+ cost?: number;
25
+ }
26
+ /** Which limit stopped a run, as the parked status says it. */
27
+ export type LimitDetail = "steps" | "tokens" | "time" | "cost";
28
+ /**
29
+ * The order limits are checked in, and so the one named when several are reached
30
+ * at once. It is fixed rather than incidental so the same state always writes the
31
+ * same detail.
32
+ */
33
+ export declare const LIMIT_DETAILS: readonly LimitDetail[];
34
+ /** What a run has used so far, which is what a limit is compared against. */
35
+ export interface LimitProgress {
36
+ /** Model requests already made. */
37
+ turns: number;
38
+ tokens: number;
39
+ /** The reported cost so far, and whether every reply reported one. */
40
+ cost: {
41
+ amount: number;
42
+ known: boolean;
43
+ };
44
+ activeMs: number;
45
+ }
46
+ /**
47
+ * A limit a recipe set, checked once when the run is created so a bad config fails
48
+ * fast instead of parking a run before it does anything (LIM03).
49
+ */
50
+ export declare function validateLimits(limits: Limits): Result<Limits>;
51
+ /** The first limit the run has reached, or nothing when it may keep going (LIM01). */
52
+ export declare function reachedLimit(limits: Limits | undefined, progress: LimitProgress): LimitDetail | undefined;
53
+ //# sourceMappingURL=limits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"limits.d.ts","sourceRoot":"","sources":["../../src/core/limits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AAEpD,MAAM,WAAW,MAAM;IACtB,wFAAwF;IACxF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,IAAI,CAAC,EAAE,MAAM,CAAC;CACd;AAED,+DAA+D;AAC/D,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAE/D;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,SAAS,WAAW,EAAwC,CAAC;AAEzF,6EAA6E;AAC7E,MAAM,WAAW,aAAa;IAC7B,mCAAmC;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,sEAAsE;IACtE,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC;IACzC,QAAQ,EAAE,MAAM,CAAC;CACjB;AAUD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CA2B7D;AAED,sFAAsF;AACtF,wBAAgB,YAAY,CAC3B,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,QAAQ,EAAE,aAAa,GACrB,WAAW,GAAG,SAAS,CAmBzB"}