@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 @@
1
+ {"version":3,"file":"clock.js","sourceRoot":"","sources":["../../src/core/clock.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * One conversation, as a model sees it.
3
+ *
4
+ * This is the shape the Model slot sends and the record replays to
5
+ * (docs/run-record.md). It carries no token, no tenant and no quota idea: the
6
+ * core must not learn Lumen's vocabulary (docs/adapters.md, "What it costs").
7
+ */
8
+ import type { ToolCall } from "./model.js";
9
+ export interface UserMessage {
10
+ role: "user";
11
+ text: string;
12
+ }
13
+ export interface AssistantMessage {
14
+ role: "assistant";
15
+ text: string;
16
+ /** Present only when the model volunteered it (decision 33). */
17
+ reasoning?: string;
18
+ toolCalls: ToolCall[];
19
+ }
20
+ export interface ToolMessage {
21
+ role: "tool";
22
+ toolCallId: string;
23
+ /** What the model reads. An error is a result it corrects, not a crash (decision 21). */
24
+ content: string;
25
+ isError: boolean;
26
+ }
27
+ export type ConversationMessage = UserMessage | AssistantMessage | ToolMessage;
28
+ //# sourceMappingURL=conversation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation.d.ts","sourceRoot":"","sources":["../../src/core/conversation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,gBAAgB;IAChC,IAAI,EAAE,WAAW,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,gEAAgE;IAChE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,QAAQ,EAAE,CAAC;CACtB;AAED,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,yFAAyF;IACzF,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,MAAM,mBAAmB,GAAG,WAAW,GAAG,gBAAgB,GAAG,WAAW,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * One conversation, as a model sees it.
3
+ *
4
+ * This is the shape the Model slot sends and the record replays to
5
+ * (docs/run-record.md). It carries no token, no tenant and no quota idea: the
6
+ * core must not learn Lumen's vocabulary (docs/adapters.md, "What it costs").
7
+ */
8
+ export {};
9
+ //# sourceMappingURL=conversation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation.js","sourceRoot":"","sources":["../../src/core/conversation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Decoding what a model sends back (docs/core-loop.md, "Decoding what a model
3
+ * sends back").
4
+ *
5
+ * An adapter builds this value from a wire response, and everything after it —
6
+ * the record, the loop, a viewer — trusts what comes out. So the decoder is
7
+ * tolerant about fields a provider may leave out or rename, strict about a field
8
+ * of the wrong type, and it never throws: the reply is either a `ModelReply` or
9
+ * a `bad_request` naming the field that was wrong.
10
+ */
11
+ import type { FinishReason, ModelReply, ToolCall, Usage } from "./model.js";
12
+ import { type Result } from "./result.js";
13
+ /**
14
+ * Usage, with a missing count read as zero. A provider that forgets to say is
15
+ * not a reason to fail a run, and a count that is not a count is.
16
+ */
17
+ export declare function decodeUsage(value: unknown): Result<Usage>;
18
+ /**
19
+ * The tool calls of a reply, each with raw argument text. Ids must be unique
20
+ * within a reply: the loop pairs a result to its call by id, so two calls with
21
+ * one id would make the record ambiguous.
22
+ */
23
+ export declare function decodeToolCalls(value: unknown): Result<ToolCall[]>;
24
+ export declare function isFinishReason(value: unknown): value is FinishReason;
25
+ /** A reason Lyra does not model is `other`: the run still needs an answer, not a failure. */
26
+ export declare function decodeFinishReason(value: unknown): FinishReason;
27
+ export declare function decodeReply(value: unknown): Result<ModelReply>;
28
+ //# sourceMappingURL=decode-reply.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode-reply.d.ts","sourceRoot":"","sources":["../../src/core/decode-reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAC5E,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AAkBpD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAuCzD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC,CAgClE;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED,6FAA6F;AAC7F,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAE/D;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,CAiD9D"}
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Decoding what a model sends back (docs/core-loop.md, "Decoding what a model
3
+ * sends back").
4
+ *
5
+ * An adapter builds this value from a wire response, and everything after it —
6
+ * the record, the loop, a viewer — trusts what comes out. So the decoder is
7
+ * tolerant about fields a provider may leave out or rename, strict about a field
8
+ * of the wrong type, and it never throws: the reply is either a `ModelReply` or
9
+ * a `bad_request` naming the field that was wrong.
10
+ */
11
+ import { isPlainObject } from "./json.js";
12
+ import { fail, ok } from "./result.js";
13
+ const FINISH_REASONS = [
14
+ "stop",
15
+ "length",
16
+ "tool_calls",
17
+ "content_filter",
18
+ "other",
19
+ ];
20
+ function bad(problem) {
21
+ return fail("bad_request", `the model reply ${problem}`);
22
+ }
23
+ function isTokenCount(value) {
24
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
25
+ }
26
+ /**
27
+ * Usage, with a missing count read as zero. A provider that forgets to say is
28
+ * not a reason to fail a run, and a count that is not a count is.
29
+ */
30
+ export function decodeUsage(value) {
31
+ if (value === undefined) {
32
+ return ok({ inputTokens: 0, outputTokens: 0 });
33
+ }
34
+ if (!isPlainObject(value)) {
35
+ return bad("has usage that is not an object");
36
+ }
37
+ const inputTokens = value["inputTokens"] === undefined ? 0 : value["inputTokens"];
38
+ const outputTokens = value["outputTokens"] === undefined ? 0 : value["outputTokens"];
39
+ if (!isTokenCount(inputTokens)) {
40
+ return bad("has a usage.inputTokens that is not a count");
41
+ }
42
+ if (!isTokenCount(outputTokens)) {
43
+ return bad("has a usage.outputTokens that is not a count");
44
+ }
45
+ const rawCost = value["cost"];
46
+ if (rawCost === undefined) {
47
+ return ok({ inputTokens, outputTokens });
48
+ }
49
+ if (!isPlainObject(rawCost)) {
50
+ return bad("has a usage.cost that is not an object");
51
+ }
52
+ const amount = rawCost["amount"];
53
+ if (typeof amount !== "number" || !Number.isFinite(amount) || amount < 0) {
54
+ return bad("has a usage.cost.amount that is not a non-negative number");
55
+ }
56
+ if (rawCost["currency"] !== "USD") {
57
+ return bad("has a usage.cost.currency that is not USD");
58
+ }
59
+ const source = rawCost["source"];
60
+ if (source !== "provider" && source !== "allowance" && source !== "catalog") {
61
+ return bad("has a usage.cost.source that is not provider, allowance or catalog");
62
+ }
63
+ return ok({
64
+ inputTokens,
65
+ outputTokens,
66
+ cost: { amount, currency: "USD", source },
67
+ });
68
+ }
69
+ /**
70
+ * The tool calls of a reply, each with raw argument text. Ids must be unique
71
+ * within a reply: the loop pairs a result to its call by id, so two calls with
72
+ * one id would make the record ambiguous.
73
+ */
74
+ export function decodeToolCalls(value) {
75
+ if (value === undefined) {
76
+ return ok([]);
77
+ }
78
+ if (!Array.isArray(value)) {
79
+ return bad("has toolCalls that are not a list");
80
+ }
81
+ const calls = [];
82
+ const ids = new Set();
83
+ for (const [index, call] of value.entries()) {
84
+ if (!isPlainObject(call)) {
85
+ return bad(`has a toolCalls[${index}] that is not an object`);
86
+ }
87
+ const id = call["id"];
88
+ if (typeof id !== "string" || id === "") {
89
+ return bad(`has a toolCalls[${index}].id that is not a non-empty string`);
90
+ }
91
+ if (ids.has(id)) {
92
+ return bad(`has a duplicate tool call id "${id}"`);
93
+ }
94
+ ids.add(id);
95
+ const name = call["name"];
96
+ if (typeof name !== "string" || name === "") {
97
+ return bad(`has a toolCalls[${index}].name that is not a non-empty string`);
98
+ }
99
+ const args = call["arguments"];
100
+ if (typeof args !== "string") {
101
+ return bad(`has a toolCalls[${index}].arguments that are not a string`);
102
+ }
103
+ calls.push({ id, name, arguments: args });
104
+ }
105
+ return ok(calls);
106
+ }
107
+ export function isFinishReason(value) {
108
+ return FINISH_REASONS.some((reason) => reason === value);
109
+ }
110
+ /** A reason Lyra does not model is `other`: the run still needs an answer, not a failure. */
111
+ export function decodeFinishReason(value) {
112
+ return isFinishReason(value) ? value : "other";
113
+ }
114
+ export function decodeReply(value) {
115
+ if (!isPlainObject(value)) {
116
+ return bad("is not an object");
117
+ }
118
+ const rawText = value["text"];
119
+ if (rawText !== undefined && typeof rawText !== "string") {
120
+ return bad("has a text that is not a string");
121
+ }
122
+ const text = rawText ?? "";
123
+ const rawReasoning = value["reasoning"];
124
+ if (rawReasoning !== undefined && typeof rawReasoning !== "string") {
125
+ return bad("has a reasoning that is not a string");
126
+ }
127
+ const rawModel = value["model"];
128
+ if (rawModel !== undefined && typeof rawModel !== "string") {
129
+ return bad("has a model that is not a string");
130
+ }
131
+ const model = rawModel ?? "";
132
+ const usage = decodeUsage(value["usage"]);
133
+ if (!usage.ok) {
134
+ return usage;
135
+ }
136
+ const toolCalls = decodeToolCalls(value["toolCalls"]);
137
+ if (!toolCalls.ok) {
138
+ return toolCalls;
139
+ }
140
+ const reply = rawReasoning === undefined
141
+ ? {
142
+ text,
143
+ toolCalls: toolCalls.value,
144
+ usage: usage.value,
145
+ finishReason: decodeFinishReason(value["finishReason"]),
146
+ model,
147
+ }
148
+ : {
149
+ text,
150
+ reasoning: rawReasoning,
151
+ toolCalls: toolCalls.value,
152
+ usage: usage.value,
153
+ finishReason: decodeFinishReason(value["finishReason"]),
154
+ model,
155
+ };
156
+ return ok(reply);
157
+ }
158
+ //# sourceMappingURL=decode-reply.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode-reply.js","sourceRoot":"","sources":["../../src/core/decode-reply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,EAAE,EAAe,MAAM,aAAa,CAAC;AAEpD,MAAM,cAAc,GAA4B;IAC/C,MAAM;IACN,QAAQ;IACR,YAAY;IACZ,gBAAgB;IAChB,OAAO;CACP,CAAC;AAEF,SAAS,GAAG,CAAC,OAAe;IAC3B,OAAO,IAAI,CAAC,aAAa,EAAE,mBAAmB,OAAO,EAAE,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;AAC/E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,KAA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CAAC,KAAK;YAC1B,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,YAAY,EAAE,kBAAkB,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;YACvD,KAAK;SACL,CAAC;IACL,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;AAClB,CAAC"}
@@ -0,0 +1,54 @@
1
+ /**
2
+ * A recipe: a task's instructions, its input, its tools, its limits, and what done
3
+ * means (docs/recipes-and-tools.md, RCP01 to RCP06; decision 5).
4
+ *
5
+ * `defineRecipe` checks the whole thing where it is written, not on a person's first
6
+ * day: a duplicate tool name, a schema the subset cannot compile, a `finish` with no
7
+ * done-check and a limit that can never be met are all refused at module load. The
8
+ * input schema is kept as plain JSON Schema, and the runtime validates a run's input
9
+ * against it before anything is saved.
10
+ */
11
+ import { type JsonObject, type JsonValue } from "./json.js";
12
+ import { type Limits } from "./limits.js";
13
+ import { type InferSchema } from "./schema.js";
14
+ import type { StuckSettings } from "./run.js";
15
+ import type { Tool } from "./tool.js";
16
+ export declare class RecipeDefinitionError extends Error {
17
+ readonly name = "RecipeDefinitionError";
18
+ }
19
+ /**
20
+ * The recipe as the runtime and the core see it: the input is already checked, so
21
+ * `setup` takes plain JSON. `defineRecipe` wraps a typed setup into this shape.
22
+ */
23
+ export interface Recipe<S = unknown> {
24
+ name: string;
25
+ version: number;
26
+ /** The instructions the model is given, which is the run's system text. */
27
+ instructions: string;
28
+ /** The schema the task's input must match, when the recipe wants one checked. */
29
+ input?: JsonObject;
30
+ /** Builds the first message and the initial state from the input (decision 112). */
31
+ setup?(input: JsonValue): {
32
+ message: string;
33
+ state: S;
34
+ };
35
+ tools: readonly Tool[];
36
+ limits?: Limits;
37
+ fallbackModel?: string;
38
+ stuck?: StuckSettings;
39
+ }
40
+ export declare function defineRecipe<const I extends JsonObject, S = unknown>(options: {
41
+ name: string;
42
+ version: number;
43
+ instructions: string;
44
+ input: I;
45
+ setup(input: InferSchema<I>): {
46
+ message: string;
47
+ state: S;
48
+ };
49
+ tools: readonly Tool[];
50
+ limits?: Limits;
51
+ fallbackModel?: string;
52
+ stuck?: StuckSettings;
53
+ }): Recipe<S>;
54
+ //# sourceMappingURL=define-recipe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"define-recipe.d.ts","sourceRoot":"","sources":["../../src/core/define-recipe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAe,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AACzE,OAAO,EAAkB,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAwC,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AACrF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,qBAAa,qBAAsB,SAAQ,KAAK;IAC/C,SAAkB,IAAI,2BAA2B;CACjD;AAED;;;GAGG;AACH,MAAM,WAAW,MAAM,CAAC,CAAC,GAAG,OAAO;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,YAAY,EAAE,MAAM,CAAC;IACrB,iFAAiF;IACjF,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,oFAAoF;IACpF,KAAK,CAAC,CAAC,KAAK,EAAE,SAAS,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,CAAC,CAAA;KAAE,CAAC;IACxD,KAAK,EAAE,SAAS,IAAI,EAAE,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE,aAAa,CAAC;CACtB;AAaD,wBAAgB,YAAY,CAAC,KAAK,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,CAAC,CAAA;KAAE,CAAC;IAC5D,KAAK,EAAE,SAAS,IAAI,EAAE,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE,aAAa,CAAC;CACtB,GAAG,MAAM,CAAC,CAAC,CAAC,CA4EZ"}
@@ -0,0 +1,96 @@
1
+ /**
2
+ * A recipe: a task's instructions, its input, its tools, its limits, and what done
3
+ * means (docs/recipes-and-tools.md, RCP01 to RCP06; decision 5).
4
+ *
5
+ * `defineRecipe` checks the whole thing where it is written, not on a person's first
6
+ * day: a duplicate tool name, a schema the subset cannot compile, a `finish` with no
7
+ * done-check and a limit that can never be met are all refused at module load. The
8
+ * input schema is kept as plain JSON Schema, and the runtime validates a run's input
9
+ * against it before anything is saved.
10
+ */
11
+ import { isJsonValue } from "./json.js";
12
+ import { validateLimits } from "./limits.js";
13
+ import { compileSchema, SchemaDefinitionError } from "./schema.js";
14
+ export class RecipeDefinitionError extends Error {
15
+ name = "RecipeDefinitionError";
16
+ }
17
+ function refuse(message) {
18
+ throw new RecipeDefinitionError(message);
19
+ }
20
+ function checkText(value, field) {
21
+ if (typeof value !== "string" || value.trim() === "") {
22
+ return refuse(`a recipe needs a ${field}`);
23
+ }
24
+ return value;
25
+ }
26
+ export function defineRecipe(options) {
27
+ const name = checkText(options.name, "name");
28
+ checkText(options.instructions, "instructions");
29
+ if (typeof options.version !== "number" ||
30
+ !Number.isSafeInteger(options.version) ||
31
+ options.version < 1) {
32
+ refuse("a recipe needs a version of one or more");
33
+ }
34
+ if (typeof options.setup !== "function") {
35
+ refuse("a recipe needs a setup, which makes the first message and the state");
36
+ }
37
+ try {
38
+ compileSchema(options.input, "input");
39
+ }
40
+ catch (error) {
41
+ if (error instanceof SchemaDefinitionError) {
42
+ refuse(`a recipe's input schema is not one this build can check: ${error.message}`);
43
+ }
44
+ throw error;
45
+ }
46
+ const names = new Set();
47
+ for (const tool of options.tools) {
48
+ if (typeof tool.spec.name !== "string" || tool.spec.name === "") {
49
+ refuse("a recipe has a tool with no name");
50
+ }
51
+ if (names.has(tool.spec.name)) {
52
+ refuse(`a recipe has two tools called "${tool.spec.name}"`);
53
+ }
54
+ names.add(tool.spec.name);
55
+ try {
56
+ compileSchema(tool.spec.parameters, `the ${tool.spec.name} tool's parameters`);
57
+ }
58
+ catch (error) {
59
+ if (error instanceof SchemaDefinitionError) {
60
+ refuse(`the ${tool.spec.name} tool has parameters this build cannot check: ${error.message}`);
61
+ }
62
+ throw error;
63
+ }
64
+ }
65
+ const finish = options.tools.find((tool) => tool.spec.name === "finish");
66
+ if (finish !== undefined && finish.finish !== true) {
67
+ refuse('a recipe has a tool called "finish" that was not made by createFinishTool, so its word that the job is done is not checked');
68
+ }
69
+ if (options.limits !== undefined) {
70
+ const limits = validateLimits(options.limits);
71
+ if (!limits.ok) {
72
+ refuse(`a recipe's limits cannot be enforced: ${limits.error.message}`);
73
+ }
74
+ }
75
+ const recipe = {
76
+ name,
77
+ version: options.version,
78
+ instructions: options.instructions,
79
+ input: isJsonValue(options.input) ? options.input : refuse("a recipe's input must be JSON"),
80
+ // The input was checked against the schema just above, and every call here
81
+ // comes through this function, so the cast is the same one `defineTool` makes.
82
+ setup: (input) => options.setup(input),
83
+ tools: options.tools,
84
+ };
85
+ if (options.limits !== undefined) {
86
+ recipe.limits = options.limits;
87
+ }
88
+ if (options.fallbackModel !== undefined) {
89
+ recipe.fallbackModel = options.fallbackModel;
90
+ }
91
+ if (options.stuck !== undefined) {
92
+ recipe.stuck = options.stuck;
93
+ }
94
+ return recipe;
95
+ }
96
+ //# sourceMappingURL=define-recipe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@@ -0,0 +1,98 @@
1
+ /**
2
+ * What a run says while it happens (docs/core-loop.md, "Events").
3
+ *
4
+ * The names are the protocol's, so step 6 maps one to one onto a frame and does
5
+ * not need a second vocabulary. Every event carries the run and the time from the
6
+ * injected clock, and an event that comes from a record entry carries that
7
+ * entry's `seq`, so a client that reconnects catches up by reading the record
8
+ * (decision 86).
9
+ *
10
+ * Nothing is emitted after `run_end`, and a listener that throws is isolated: it
11
+ * cannot reach the loop (LOOP11).
12
+ */
13
+ import type { FinishReason, ToolCall, Usage } from "./model.js";
14
+ import type { LyraError } from "./result.js";
15
+ export type RunEndReason = "done" | "parked" | "failed";
16
+ /** Why a run parked. Approvals join in a later slice (decision 84). */
17
+ export type ParkedReason = "stopped" | "stuck" | "limit" | "model_unavailable";
18
+ interface EventBase {
19
+ /** The run this event belongs to. Constant for every event of a run. */
20
+ run: string;
21
+ /** The clock's time when the event was made, not the time it was sent. */
22
+ ts: number;
23
+ }
24
+ export type RunEvent = (EventBase & {
25
+ type: "run_start";
26
+ task: string;
27
+ }) | (EventBase & {
28
+ type: "turn_start";
29
+ turn: number;
30
+ })
31
+ /** The assistant message, saved. `seq` is that entry's position in the record. */
32
+ | (EventBase & {
33
+ type: "message_end";
34
+ seq: number;
35
+ text: string;
36
+ toolCalls: ToolCall[];
37
+ })
38
+ /**
39
+ * A call the model asked for, announced before anything runs. It carries no
40
+ * `seq` because no record entry is a call: the assistant message it came from
41
+ * is already announced by `message_end`, and the call is named by its id.
42
+ */
43
+ | (EventBase & {
44
+ type: "tool_call";
45
+ call: ToolCall;
46
+ }) | (EventBase & {
47
+ type: "tool_start";
48
+ callId: string;
49
+ name: string;
50
+ })
51
+ /** The tool's result, saved. */
52
+ | (EventBase & {
53
+ type: "tool_end";
54
+ seq: number;
55
+ callId: string;
56
+ name: string;
57
+ isError: boolean;
58
+ content: string;
59
+ }) | (EventBase & {
60
+ type: "turn_end";
61
+ turn: number;
62
+ finishReason: FinishReason;
63
+ }) | (EventBase & {
64
+ type: "usage";
65
+ inputTokens: number;
66
+ outputTokens: number;
67
+ cost?: Usage["cost"];
68
+ }) | (EventBase & {
69
+ type: "error";
70
+ error: LyraError;
71
+ })
72
+ /**
73
+ * A retryable model failure, and how long the run will wait before trying that
74
+ * turn again (decision 26).
75
+ */
76
+ | (EventBase & {
77
+ type: "retry";
78
+ attempt: number;
79
+ delayMs: number;
80
+ error: LyraError;
81
+ })
82
+ /** The run moved to another model after the first failed its retries (decision 58). */
83
+ | (EventBase & {
84
+ type: "fallback";
85
+ from: string;
86
+ to: string;
87
+ reason: string;
88
+ }) | (EventBase & {
89
+ type: "run_end";
90
+ reason: RunEndReason;
91
+ parkedReason?: ParkedReason;
92
+ /** Which part of the reason, when it has one: a limit's `steps`, `tokens`, `time` or `cost`. */
93
+ detail?: string;
94
+ });
95
+ /** A listener is never allowed to be the reason a run fails. */
96
+ export type RunEventListener = (event: RunEvent) => void;
97
+ export {};
98
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/core/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAExD,uEAAuE;AACvE,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO,GAAG,mBAAmB,CAAC;AAE/E,UAAU,SAAS;IAClB,wEAAwE;IACxE,GAAG,EAAE,MAAM,CAAC;IACZ,0EAA0E;IAC1E,EAAE,EAAE,MAAM,CAAC;CACX;AAED,MAAM,MAAM,QAAQ,GACjB,CAAC,SAAS,GAAG;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,IAAI,EAAE,MAAM,CAA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,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AACrF,uFAAuF;GACrF,CAAC,SAAS,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,GAC5E,CAAC,SAAS,GAAG;IACb,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,EAAE,YAAY,CAAC;IACrB,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,gGAAgG;IAChG,MAAM,CAAC,EAAE,MAAM,CAAC;CACf,CAAC,CAAC;AAEN,gEAAgE;AAChE,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,QAAQ,KAAK,IAAI,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * What a run says while it happens (docs/core-loop.md, "Events").
3
+ *
4
+ * The names are the protocol's, so step 6 maps one to one onto a frame and does
5
+ * not need a second vocabulary. Every event carries the run and the time from the
6
+ * injected clock, and an event that comes from a record entry carries that
7
+ * entry's `seq`, so a client that reconnects catches up by reading the record
8
+ * (decision 86).
9
+ *
10
+ * Nothing is emitted after `run_end`, and a listener that throws is isolated: it
11
+ * cannot reach the loop (LOOP11).
12
+ */
13
+ export {};
14
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/core/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG"}
@@ -0,0 +1,61 @@
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 type { Clock } from "./clock.js";
15
+ import type { ToolCall } from "./model.js";
16
+ import { type Tool, type ToolOutcome } from "./tool.js";
17
+ export interface ExecuteToolsOptions {
18
+ calls: readonly ToolCall[];
19
+ tools: readonly Tool[];
20
+ clock: Clock;
21
+ signal: AbortSignal;
22
+ /** How long one call may take, measured on the clock. */
23
+ deadlineMs: number;
24
+ runId: string;
25
+ /** Called for a call that is about to run, after its arguments are known to be usable. */
26
+ onStart(call: ToolCall): void;
27
+ /**
28
+ * Called for every call, in order, when its result is ready to be saved. A call
29
+ * that ran carries the arguments it ran with, which is what `apply` needs.
30
+ */
31
+ onFinish(call: ToolCall, outcome: ToolOutcome, args?: unknown): Promise<void>;
32
+ /** The recipe's state, read at the moment a call runs (not copied, not changed). */
33
+ readState?(): unknown;
34
+ /** True once the run has already finished, so later calls are answered but not run. */
35
+ isFinished(): boolean;
36
+ }
37
+ /** A model's arguments, or the reason they cannot be used. */
38
+ export declare function decodeArguments(raw: string): {
39
+ ok: true;
40
+ value: unknown;
41
+ } | {
42
+ ok: false;
43
+ reason: string;
44
+ };
45
+ export type UsableArguments = {
46
+ ok: true;
47
+ value: unknown;
48
+ } | {
49
+ ok: false;
50
+ reason: string;
51
+ invalid: boolean;
52
+ };
53
+ /**
54
+ * The arguments a tool may run with: JSON, and the tool's own schema's word.
55
+ *
56
+ * It is one function because the live path and the replay path have to agree, and
57
+ * two copies of "is this call usable" would drift the moment one changed.
58
+ */
59
+ export declare function usableArguments(tool: Tool, raw: string): UsableArguments;
60
+ export declare function executeTools(options: ExecuteToolsOptions): Promise<void>;
61
+ //# sourceMappingURL=execute-tools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execute-tools.d.ts","sourceRoot":"","sources":["../../src/core/execute-tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;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