@aid-framework/runtime 0.0.0 → 0.1.48

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 (188) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +183 -0
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/agents/deep_agents.d.ts +78 -0
  5. package/dist/agents/deep_agents.d.ts.map +1 -0
  6. package/dist/agents/deep_agents.js +477 -0
  7. package/dist/agents/deep_agents.js.map +1 -0
  8. package/dist/agents/engine.d.ts +85 -0
  9. package/dist/agents/engine.d.ts.map +1 -0
  10. package/dist/agents/engine.js +129 -0
  11. package/dist/agents/engine.js.map +1 -0
  12. package/dist/agents/gateway_chat_model.d.ts +151 -0
  13. package/dist/agents/gateway_chat_model.d.ts.map +1 -0
  14. package/dist/agents/gateway_chat_model.js +350 -0
  15. package/dist/agents/gateway_chat_model.js.map +1 -0
  16. package/dist/agents/guardrail_middleware.d.ts +57 -0
  17. package/dist/agents/guardrail_middleware.d.ts.map +1 -0
  18. package/dist/agents/guardrail_middleware.js +85 -0
  19. package/dist/agents/guardrail_middleware.js.map +1 -0
  20. package/dist/agents/guardrails.d.ts +124 -0
  21. package/dist/agents/guardrails.d.ts.map +1 -0
  22. package/dist/agents/guardrails.js +336 -0
  23. package/dist/agents/guardrails.js.map +1 -0
  24. package/dist/agents/index.d.ts +15 -0
  25. package/dist/agents/index.d.ts.map +1 -0
  26. package/dist/agents/index.js +15 -0
  27. package/dist/agents/index.js.map +1 -0
  28. package/dist/agents/ir.d.ts +144 -0
  29. package/dist/agents/ir.d.ts.map +1 -0
  30. package/dist/agents/ir.js +513 -0
  31. package/dist/agents/ir.js.map +1 -0
  32. package/dist/agents/loop.d.ts +145 -0
  33. package/dist/agents/loop.d.ts.map +1 -0
  34. package/dist/agents/loop.js +1025 -0
  35. package/dist/agents/loop.js.map +1 -0
  36. package/dist/agents/memory.d.ts +115 -0
  37. package/dist/agents/memory.d.ts.map +1 -0
  38. package/dist/agents/memory.js +258 -0
  39. package/dist/agents/memory.js.map +1 -0
  40. package/dist/agents/state.d.ts +137 -0
  41. package/dist/agents/state.d.ts.map +1 -0
  42. package/dist/agents/state.js +255 -0
  43. package/dist/agents/state.js.map +1 -0
  44. package/dist/concurrency.d.ts +36 -0
  45. package/dist/concurrency.d.ts.map +1 -0
  46. package/dist/concurrency.js +86 -0
  47. package/dist/concurrency.js.map +1 -0
  48. package/dist/control.d.ts +116 -0
  49. package/dist/control.d.ts.map +1 -0
  50. package/dist/control.js +281 -0
  51. package/dist/control.js.map +1 -0
  52. package/dist/cost.d.ts +95 -0
  53. package/dist/cost.d.ts.map +1 -0
  54. package/dist/cost.js +161 -0
  55. package/dist/cost.js.map +1 -0
  56. package/dist/errors.d.ts +213 -0
  57. package/dist/errors.d.ts.map +1 -0
  58. package/dist/errors.js +316 -0
  59. package/dist/errors.js.map +1 -0
  60. package/dist/evals/dataset.d.ts +45 -0
  61. package/dist/evals/dataset.d.ts.map +1 -0
  62. package/dist/evals/dataset.js +147 -0
  63. package/dist/evals/dataset.js.map +1 -0
  64. package/dist/evals/gate.d.ts +236 -0
  65. package/dist/evals/gate.d.ts.map +1 -0
  66. package/dist/evals/gate.js +572 -0
  67. package/dist/evals/gate.js.map +1 -0
  68. package/dist/evals/metrics.d.ts +141 -0
  69. package/dist/evals/metrics.d.ts.map +1 -0
  70. package/dist/evals/metrics.js +364 -0
  71. package/dist/evals/metrics.js.map +1 -0
  72. package/dist/evals/predicate.d.ts +79 -0
  73. package/dist/evals/predicate.d.ts.map +1 -0
  74. package/dist/evals/predicate.js +178 -0
  75. package/dist/evals/predicate.js.map +1 -0
  76. package/dist/evals/runner.d.ts +94 -0
  77. package/dist/evals/runner.d.ts.map +1 -0
  78. package/dist/evals/runner.js +202 -0
  79. package/dist/evals/runner.js.map +1 -0
  80. package/dist/gateway.d.ts +118 -0
  81. package/dist/gateway.d.ts.map +1 -0
  82. package/dist/gateway.js +250 -0
  83. package/dist/gateway.js.map +1 -0
  84. package/dist/index.d.ts +83 -0
  85. package/dist/index.d.ts.map +1 -0
  86. package/dist/index.js +59 -0
  87. package/dist/index.js.map +1 -0
  88. package/dist/internal/json.d.ts +44 -0
  89. package/dist/internal/json.d.ts.map +1 -0
  90. package/dist/internal/json.js +90 -0
  91. package/dist/internal/json.js.map +1 -0
  92. package/dist/internal/text.d.ts +17 -0
  93. package/dist/internal/text.d.ts.map +1 -0
  94. package/dist/internal/text.js +22 -0
  95. package/dist/internal/text.js.map +1 -0
  96. package/dist/pipeline.d.ts +80 -0
  97. package/dist/pipeline.d.ts.map +1 -0
  98. package/dist/pipeline.js +159 -0
  99. package/dist/pipeline.js.map +1 -0
  100. package/dist/policy.d.ts +52 -0
  101. package/dist/policy.d.ts.map +1 -0
  102. package/dist/policy.js +199 -0
  103. package/dist/policy.js.map +1 -0
  104. package/dist/prompts.d.ts +86 -0
  105. package/dist/prompts.d.ts.map +1 -0
  106. package/dist/prompts.js +337 -0
  107. package/dist/prompts.js.map +1 -0
  108. package/dist/providers/base.d.ts +19 -0
  109. package/dist/providers/base.d.ts.map +1 -0
  110. package/dist/providers/base.js +13 -0
  111. package/dist/providers/base.js.map +1 -0
  112. package/dist/providers/fake.d.ts +98 -0
  113. package/dist/providers/fake.d.ts.map +1 -0
  114. package/dist/providers/fake.js +181 -0
  115. package/dist/providers/fake.js.map +1 -0
  116. package/dist/providers/http.d.ts +62 -0
  117. package/dist/providers/http.d.ts.map +1 -0
  118. package/dist/providers/http.js +245 -0
  119. package/dist/providers/http.js.map +1 -0
  120. package/dist/retrieval/chunking.d.ts +88 -0
  121. package/dist/retrieval/chunking.d.ts.map +1 -0
  122. package/dist/retrieval/chunking.js +188 -0
  123. package/dist/retrieval/chunking.js.map +1 -0
  124. package/dist/retrieval/citations.d.ts +98 -0
  125. package/dist/retrieval/citations.d.ts.map +1 -0
  126. package/dist/retrieval/citations.js +134 -0
  127. package/dist/retrieval/citations.js.map +1 -0
  128. package/dist/retrieval/connectors.d.ts +74 -0
  129. package/dist/retrieval/connectors.d.ts.map +1 -0
  130. package/dist/retrieval/connectors.js +218 -0
  131. package/dist/retrieval/connectors.js.map +1 -0
  132. package/dist/retrieval/embedding.d.ts +116 -0
  133. package/dist/retrieval/embedding.d.ts.map +1 -0
  134. package/dist/retrieval/embedding.js +282 -0
  135. package/dist/retrieval/embedding.js.map +1 -0
  136. package/dist/retrieval/gate.d.ts +52 -0
  137. package/dist/retrieval/gate.d.ts.map +1 -0
  138. package/dist/retrieval/gate.js +91 -0
  139. package/dist/retrieval/gate.js.map +1 -0
  140. package/dist/retrieval/hybrid.d.ts +76 -0
  141. package/dist/retrieval/hybrid.d.ts.map +1 -0
  142. package/dist/retrieval/hybrid.js +150 -0
  143. package/dist/retrieval/hybrid.js.map +1 -0
  144. package/dist/retrieval/metrics.d.ts +126 -0
  145. package/dist/retrieval/metrics.d.ts.map +1 -0
  146. package/dist/retrieval/metrics.js +200 -0
  147. package/dist/retrieval/metrics.js.map +1 -0
  148. package/dist/retrieval/pgvector.d.ts +62 -0
  149. package/dist/retrieval/pgvector.d.ts.map +1 -0
  150. package/dist/retrieval/pgvector.js +262 -0
  151. package/dist/retrieval/pgvector.js.map +1 -0
  152. package/dist/retrieval/rerank.d.ts +61 -0
  153. package/dist/retrieval/rerank.d.ts.map +1 -0
  154. package/dist/retrieval/rerank.js +105 -0
  155. package/dist/retrieval/rerank.js.map +1 -0
  156. package/dist/retrieval/retriever.d.ts +125 -0
  157. package/dist/retrieval/retriever.d.ts.map +1 -0
  158. package/dist/retrieval/retriever.js +281 -0
  159. package/dist/retrieval/retriever.js.map +1 -0
  160. package/dist/retrieval/stores.d.ts +106 -0
  161. package/dist/retrieval/stores.d.ts.map +1 -0
  162. package/dist/retrieval/stores.js +142 -0
  163. package/dist/retrieval/stores.js.map +1 -0
  164. package/dist/retrieval/types.d.ts +314 -0
  165. package/dist/retrieval/types.d.ts.map +1 -0
  166. package/dist/retrieval/types.js +388 -0
  167. package/dist/retrieval/types.js.map +1 -0
  168. package/dist/steps.d.ts +156 -0
  169. package/dist/steps.d.ts.map +1 -0
  170. package/dist/steps.js +199 -0
  171. package/dist/steps.js.map +1 -0
  172. package/dist/structured.d.ts +91 -0
  173. package/dist/structured.d.ts.map +1 -0
  174. package/dist/structured.js +155 -0
  175. package/dist/structured.js.map +1 -0
  176. package/dist/tools.d.ts +100 -0
  177. package/dist/tools.d.ts.map +1 -0
  178. package/dist/tools.js +201 -0
  179. package/dist/tools.js.map +1 -0
  180. package/dist/tracing.d.ts +104 -0
  181. package/dist/tracing.d.ts.map +1 -0
  182. package/dist/tracing.js +155 -0
  183. package/dist/tracing.js.map +1 -0
  184. package/dist/types.d.ts +240 -0
  185. package/dist/types.d.ts.map +1 -0
  186. package/dist/types.js +357 -0
  187. package/dist/types.js.map +1 -0
  188. package/package.json +80 -14
@@ -0,0 +1,255 @@
1
+ import { Message, ToolCall } from '../types.js';
2
+ /**
3
+ * `PAUSED` is an outcome, not an error. So is `BLOCKED`: a human rejected an escalation
4
+ * the guardrails raised, which is a decision the run respected rather than a failure it
5
+ * suffered.
6
+ */
7
+ export const RUN_STATUSES = ['completed', 'paused', 'max-steps', 'blocked'];
8
+ export const STEP_KINDS = ['answer', 'tools', 'transfer', 'delegate'];
9
+ export function pendingApprovalFrom(checkpoint) {
10
+ return {
11
+ agentId: checkpoint.agentId,
12
+ channel: checkpoint.channel,
13
+ step: checkpoint.step,
14
+ calls: checkpoint.calls,
15
+ when: checkpoint.when,
16
+ path: checkpoint.path,
17
+ escalation: checkpoint.escalation,
18
+ };
19
+ }
20
+ /** What a run produced -- including when it produced no answer. */
21
+ export class RunResult {
22
+ status;
23
+ agentId;
24
+ answer;
25
+ steps;
26
+ path;
27
+ costUsd;
28
+ totalTokens;
29
+ checkpoint;
30
+ guardrails;
31
+ constructor(init) {
32
+ this.status = init.status;
33
+ this.agentId = init.agentId;
34
+ this.answer = init.answer ?? '';
35
+ this.steps = [...(init.steps ?? [])];
36
+ this.path = [...(init.path ?? [])];
37
+ this.costUsd = init.costUsd ?? 0;
38
+ this.totalTokens = init.totalTokens ?? 0;
39
+ this.checkpoint = init.checkpoint;
40
+ this.guardrails = [...(init.guardrails ?? [])];
41
+ }
42
+ get completed() {
43
+ return this.status === 'completed';
44
+ }
45
+ /** `(from, to)` for every step that stepped down the alias ladder. */
46
+ get degraded() {
47
+ const pairs = [];
48
+ for (const step of this.steps) {
49
+ if (step.degradedFrom !== undefined)
50
+ pairs.push([step.degradedFrom, step.alias]);
51
+ }
52
+ return pairs;
53
+ }
54
+ /** The question a paused run is asking, or `undefined` when it did not pause. */
55
+ get approval() {
56
+ return this.checkpoint === undefined ? undefined : pendingApprovalFrom(this.checkpoint);
57
+ }
58
+ }
59
+ function toolCallToJson(call) {
60
+ return { id: call.id, name: call.name, arguments: { ...call.args } };
61
+ }
62
+ function toolCallFromJson(raw) {
63
+ return new ToolCall({
64
+ id: String(raw.id),
65
+ name: String(raw.name),
66
+ arguments: { ...(raw.arguments ?? {}) },
67
+ });
68
+ }
69
+ function messageToJson(message) {
70
+ return {
71
+ role: message.role,
72
+ content: message.content,
73
+ tool_calls: message.toolCalls.map(toolCallToJson),
74
+ tool_call_id: message.toolCallId ?? null,
75
+ name: message.name ?? null,
76
+ };
77
+ }
78
+ function messageFromJson(raw) {
79
+ const toolCalls = (raw.tool_calls ?? []).map(toolCallFromJson);
80
+ const toolCallId = raw.tool_call_id;
81
+ const name = raw.name;
82
+ return new Message(String(raw.role), String(raw.content), {
83
+ toolCalls,
84
+ ...(toolCallId === null || toolCallId === undefined ? {} : { toolCallId }),
85
+ ...(name === null || name === undefined ? {} : { name }),
86
+ });
87
+ }
88
+ function stepToJson(step) {
89
+ return {
90
+ index: step.index,
91
+ agent_id: step.agentId,
92
+ kind: step.kind,
93
+ alias: step.alias,
94
+ model: step.model,
95
+ cost_usd: step.costUsd,
96
+ total_tokens: step.totalTokens,
97
+ tool_names: [...step.toolNames],
98
+ target: step.target ?? null,
99
+ degraded_from: step.degradedFrom ?? null,
100
+ };
101
+ }
102
+ function stepFromJson(raw) {
103
+ return {
104
+ index: Number(raw.index),
105
+ agentId: String(raw.agent_id),
106
+ kind: String(raw.kind),
107
+ alias: String(raw.alias),
108
+ model: String(raw.model),
109
+ costUsd: Number(raw.cost_usd),
110
+ totalTokens: Number(raw.total_tokens),
111
+ toolNames: (raw.tool_names ?? []).map(String),
112
+ target: raw.target ?? undefined,
113
+ degradedFrom: raw.degraded_from ?? undefined,
114
+ };
115
+ }
116
+ function recordToJson(record) {
117
+ return {
118
+ guardrail: record.guardrail,
119
+ stage: record.stage,
120
+ action: record.action,
121
+ policies: [...record.policies],
122
+ message: record.message,
123
+ };
124
+ }
125
+ function recordFromJson(raw) {
126
+ return {
127
+ guardrail: String(raw.guardrail),
128
+ stage: String(raw.stage),
129
+ action: String(raw.action),
130
+ policies: (raw.policies ?? []).map(String),
131
+ message: String(raw.message),
132
+ };
133
+ }
134
+ function escalationToJson(escalation) {
135
+ return {
136
+ guardrail: escalation.guardrail,
137
+ stage: escalation.stage,
138
+ message: escalation.message,
139
+ channel: escalation.channel,
140
+ text: escalation.text,
141
+ };
142
+ }
143
+ function escalationFromJson(raw) {
144
+ return {
145
+ guardrail: String(raw.guardrail),
146
+ stage: String(raw.stage),
147
+ message: String(raw.message),
148
+ channel: String(raw.channel ?? ''),
149
+ text: String(raw.text ?? ''),
150
+ };
151
+ }
152
+ export function checkpointToJson(checkpoint) {
153
+ return {
154
+ agent_id: checkpoint.agentId,
155
+ step: checkpoint.step,
156
+ messages: checkpoint.messages.map(messageToJson),
157
+ steps: checkpoint.steps.map(stepToJson),
158
+ calls: checkpoint.calls.map(toolCallToJson),
159
+ channel: checkpoint.channel,
160
+ when: { ...checkpoint.when },
161
+ resume_kind: checkpoint.resumeKind,
162
+ cost_anchor_usd: checkpoint.costAnchorUsd,
163
+ token_anchor: checkpoint.tokenAnchor,
164
+ spent_usd: checkpoint.spentUsd,
165
+ spent_tokens: checkpoint.spentTokens,
166
+ degraded: checkpoint.degraded,
167
+ alias: checkpoint.alias,
168
+ path: [...checkpoint.path],
169
+ memory_key: checkpoint.memoryKey,
170
+ caller: checkpoint.caller === undefined ? null : checkpointToJson(checkpoint.caller),
171
+ escalation: checkpoint.escalation === undefined ? null : escalationToJson(checkpoint.escalation),
172
+ guardrails: checkpoint.guardrails.map(recordToJson),
173
+ };
174
+ }
175
+ export function checkpointFromJson(raw) {
176
+ const caller = raw.caller;
177
+ const escalation = raw.escalation;
178
+ return {
179
+ agentId: String(raw.agent_id),
180
+ step: Number(raw.step),
181
+ messages: (raw.messages ?? []).map(messageFromJson),
182
+ steps: (raw.steps ?? []).map(stepFromJson),
183
+ calls: (raw.calls ?? []).map(toolCallFromJson),
184
+ channel: String(raw.channel ?? ''),
185
+ when: { ...(raw.when ?? {}) },
186
+ resumeKind: (raw.resume_kind === 'delegate'
187
+ ? 'delegate'
188
+ : 'approve'),
189
+ costAnchorUsd: Number(raw.cost_anchor_usd),
190
+ tokenAnchor: Number(raw.token_anchor),
191
+ spentUsd: Number(raw.spent_usd),
192
+ spentTokens: Number(raw.spent_tokens),
193
+ degraded: Boolean(raw.degraded ?? false),
194
+ alias: String(raw.alias ?? ''),
195
+ path: (raw.path ?? []).map(String),
196
+ memoryKey: String(raw.memory_key ?? ''),
197
+ caller: caller === null || caller === undefined ? undefined : checkpointFromJson(caller),
198
+ escalation: escalation === null || escalation === undefined ? undefined : escalationFromJson(escalation),
199
+ guardrails: (raw.guardrails ?? []).map(recordFromJson),
200
+ };
201
+ }
202
+ /**
203
+ * Whether two checkpoints are the same value.
204
+ *
205
+ * A round trip that only compares the JSON it wrote cannot notice a field the codec
206
+ * dropped on both sides, so this compares the restored value to the original and asserts
207
+ * the class instances came back as instances.
208
+ */
209
+ export function checkpointsEqual(left, right) {
210
+ if (left === right)
211
+ return true;
212
+ if (left.messages.length !== right.messages.length)
213
+ return false;
214
+ if (left.calls.length !== right.calls.length)
215
+ return false;
216
+ if (left.steps.length !== right.steps.length)
217
+ return false;
218
+ if (left.guardrails.length !== right.guardrails.length)
219
+ return false;
220
+ for (const message of [...left.messages, ...right.messages]) {
221
+ if (!(message instanceof Message))
222
+ return false;
223
+ }
224
+ for (const call of [...left.calls, ...right.calls]) {
225
+ if (!(call instanceof ToolCall))
226
+ return false;
227
+ }
228
+ return sameJson(checkpointToJson(left), checkpointToJson(right));
229
+ }
230
+ function sameJson(left, right) {
231
+ if (left === right)
232
+ return true;
233
+ if (typeof left !== typeof right)
234
+ return false;
235
+ if (left === null || right === null)
236
+ return false;
237
+ if (Array.isArray(left) || Array.isArray(right)) {
238
+ if (!Array.isArray(left) || !Array.isArray(right))
239
+ return false;
240
+ if (left.length !== right.length)
241
+ return false;
242
+ return left.every((value, index) => sameJson(value, right[index]));
243
+ }
244
+ if (typeof left === 'object') {
245
+ const leftKeys = Object.keys(left).sort();
246
+ const rightKeys = Object.keys(right).sort();
247
+ if (leftKeys.length !== rightKeys.length)
248
+ return false;
249
+ if (!leftKeys.every((key, index) => key === rightKeys[index]))
250
+ return false;
251
+ return leftKeys.every((key) => sameJson(left[key], right[key]));
252
+ }
253
+ return false;
254
+ }
255
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/agents/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAa,QAAQ,EAAE,MAAM,aAAa,CAAC;AAsB3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAyB,CAAC,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC;AAKlG,MAAM,CAAC,MAAM,UAAU,GAAwB,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC;AAyE3F,MAAM,UAAU,mBAAmB,CAAC,UAA2B;IAC7D,OAAO;QACL,OAAO,EAAE,UAAU,CAAC,OAAO;QAC3B,OAAO,EAAE,UAAU,CAAC,OAAO;QAC3B,IAA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AC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACrE,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,IAAY,CAAC,CAAC,IAAI,EAAE,CAAC;QAClD,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAa,CAAC,CAAC,IAAI,EAAE,CAAC;QACpD,IAAI,QAAQ,CAAC,MAAM,KAAK,SAAS,CAAC,MAAM;YAAE,OAAO,KAAK,CAAC;QACvD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,SAAS,CAAC,KAAK,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;QAC5E,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAE,IAAa,CAAC,GAAG,CAAC,EAAG,KAAc,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACtF,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * A single shared concurrency limiter for a generated application.
3
+ *
4
+ * `deployments.concurrency` produces exactly one {@link ConcurrencyLimiter}. The model
5
+ * gateway, the tool executor and the agent engine's delegation all acquire it, so there
6
+ * is one limiter -- not one per component -- and a single cap bounds every unit of work
7
+ * the app performs concurrently.
8
+ */
9
+ /** Whether this async context already holds a slot of the limiter it would acquire. */
10
+ export declare function holdsSlot(): boolean;
11
+ export declare class ConcurrencyLimiter {
12
+ /** The cap this limiter was constructed with. */
13
+ readonly limit: number;
14
+ private active;
15
+ private readonly waiters;
16
+ constructor(limit: number);
17
+ /** How many slots are held right now. Exposed so a caller can report the cap. */
18
+ get inFlight(): number;
19
+ /**
20
+ * Hold one slot for the duration of a single unit of work.
21
+ *
22
+ * Errors and rejections release the slot: the `finally` below runs on both paths, so
23
+ * a failed call cannot leak capacity. A waiter woken by a release is handed the slot
24
+ * directly rather than rejoining the back of the queue, which keeps the counter from
25
+ * briefly exceeding the cap.
26
+ *
27
+ * Nested acquisition inside an already-held slot yields at once rather than waiting:
28
+ * the enclosing unit of work is already counted, and waiting would deadlock at a cap
29
+ * of one, where the fan-out worker holding the only slot is the caller of the gateway
30
+ * that wants it.
31
+ */
32
+ run<T>(body: () => Promise<T> | T): Promise<T>;
33
+ private acquire;
34
+ private release;
35
+ }
36
+ //# sourceMappingURL=concurrency.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"concurrency.d.ts","sourceRoot":"","sources":["../src/concurrency.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAkBH,uFAAuF;AACvF,wBAAgB,SAAS,IAAI,OAAO,CAEnC;AAED,qBAAa,kBAAkB;IAC7B,iDAAiD;IACjD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAyB;IAEjD,YAAY,KAAK,EAAE,MAAM,EAKxB;IAED,iFAAiF;IACjF,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED;;;;;;;;;;;;OAYG;IACG,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAQnD;YAEa,OAAO;IAUrB,OAAO,CAAC,OAAO;CAWhB"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * A single shared concurrency limiter for a generated application.
3
+ *
4
+ * `deployments.concurrency` produces exactly one {@link ConcurrencyLimiter}. The model
5
+ * gateway, the tool executor and the agent engine's delegation all acquire it, so there
6
+ * is one limiter -- not one per component -- and a single cap bounds every unit of work
7
+ * the app performs concurrently.
8
+ */
9
+ import { AsyncLocalStorage } from 'node:async_hooks';
10
+ import { ConfigurationError } from './errors.js';
11
+ /**
12
+ * Whether the current async context is already inside {@link ConcurrencyLimiter.run}.
13
+ *
14
+ * A fan-out worker holds a slot for its whole body, and that body's model and tool
15
+ * calls acquire the same limiter. Acquiring twice would deadlock at a cap of one, so an
16
+ * acquisition made while a slot is already held is a no-op: the work is already counted
17
+ * against the cap above it. `AsyncLocalStorage` is the counterpart of the reference
18
+ * runtime's `ContextVar` -- each worker has its own store, and a worker's store must not
19
+ * leak to a sibling.
20
+ */
21
+ const HELD = new AsyncLocalStorage();
22
+ /** Whether this async context already holds a slot of the limiter it would acquire. */
23
+ export function holdsSlot() {
24
+ return HELD.getStore() === true;
25
+ }
26
+ export class ConcurrencyLimiter {
27
+ /** The cap this limiter was constructed with. */
28
+ limit;
29
+ active = 0;
30
+ waiters = [];
31
+ constructor(limit) {
32
+ if (!Number.isInteger(limit) || limit < 1) {
33
+ throw new ConfigurationError(`concurrency must be at least 1, got ${limit}`);
34
+ }
35
+ this.limit = limit;
36
+ }
37
+ /** How many slots are held right now. Exposed so a caller can report the cap. */
38
+ get inFlight() {
39
+ return this.active;
40
+ }
41
+ /**
42
+ * Hold one slot for the duration of a single unit of work.
43
+ *
44
+ * Errors and rejections release the slot: the `finally` below runs on both paths, so
45
+ * a failed call cannot leak capacity. A waiter woken by a release is handed the slot
46
+ * directly rather than rejoining the back of the queue, which keeps the counter from
47
+ * briefly exceeding the cap.
48
+ *
49
+ * Nested acquisition inside an already-held slot yields at once rather than waiting:
50
+ * the enclosing unit of work is already counted, and waiting would deadlock at a cap
51
+ * of one, where the fan-out worker holding the only slot is the caller of the gateway
52
+ * that wants it.
53
+ */
54
+ async run(body) {
55
+ if (holdsSlot())
56
+ return body();
57
+ await this.acquire();
58
+ try {
59
+ return await HELD.run(true, body);
60
+ }
61
+ finally {
62
+ this.release();
63
+ }
64
+ }
65
+ async acquire() {
66
+ if (this.active < this.limit) {
67
+ this.active += 1;
68
+ return;
69
+ }
70
+ await new Promise((resolve) => {
71
+ this.waiters.push(resolve);
72
+ });
73
+ }
74
+ release() {
75
+ const next = this.waiters.shift();
76
+ if (next !== undefined) {
77
+ // Hand the slot over without touching the count: the releaser's slot becomes the
78
+ // waiter's, so no third party can slip in between release and wake and the counter
79
+ // never rises above the cap. The waiter's own `finally` releases it in turn.
80
+ next();
81
+ return;
82
+ }
83
+ this.active -= 1;
84
+ }
85
+ }
86
+ //# sourceMappingURL=concurrency.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"concurrency.js","sourceRoot":"","sources":["../src/concurrency.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;;;;;;GASG;AACH,MAAM,IAAI,GAAG,IAAI,iBAAiB,EAAQ,CAAC;AAE3C,uFAAuF;AACvF,MAAM,UAAU,SAAS;IACvB,OAAO,IAAI,CAAC,QAAQ,EAAE,KAAK,IAAI,CAAC;AAClC,CAAC;AAED,MAAM,OAAO,kBAAkB;IAC7B,iDAAiD;IACxC,KAAK,CAAS;IAEf,MAAM,GAAG,CAAC,CAAC;IACF,OAAO,GAAsB,EAAE,CAAC;IAEjD,YAAY,KAAa;QACvB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YAC1C,MAAM,IAAI,kBAAkB,CAAC,uCAAuC,KAAK,EAAE,CAAC,CAAC;QAC/E,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,iFAAiF;IACjF,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,KAAK,CAAC,GAAG,CAAI,IAA0B;QACrC,IAAI,SAAS,EAAE;YAAE,OAAO,IAAI,EAAE,CAAC;QAC/B,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QACrB,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACpC,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,OAAO;QACnB,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;YAC7B,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;YACjB,OAAO;QACT,CAAC;QACD,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YAClC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,OAAO;QACb,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QAClC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,iFAAiF;YACjF,mFAAmF;YACnF,6EAA6E;YAC7E,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QACD,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;IACnB,CAAC;CACF"}
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Composite pipeline steps: `branch`, `parallel`, `map` and `reduce`.
3
+ *
4
+ * {@link Pipeline} is a sequential reducer, and it stays one. Each factory here returns
5
+ * an ordinary {@link Step}, so a composite composes with the flat steps by the same rule
6
+ * and replays unchanged on the streaming route, which inlines the pipeline instead of
7
+ * driving it step by step.
8
+ *
9
+ * Six context keys are reserved for the family, and no step may bind a name equal to one
10
+ * of them: `principal`, `limiter`, `item`, `index`, `depth` and `reduced`. The first
11
+ * two are seeded by the application, the rest are written and removed by the composites
12
+ * themselves, so an inner step can read `item` without a matching binding leaking to a
13
+ * later sibling.
14
+ *
15
+ * Two bounds are enforced here and both fail the run rather than degrade it:
16
+ *
17
+ * - nesting, at {@link MAX_STEP_DEPTH}. The spec layer rejects a deeper pipeline before
18
+ * generation, so reaching this bound at run time means a hand-built pipeline, and the
19
+ * reason is named in the error.
20
+ * - concurrency, through the application's one {@link ConcurrencyLimiter}. A fan-out
21
+ * with no limiter in the context raises rather than running unbounded, because the cap
22
+ * is what makes a fan-out safe and its absence is a wiring bug.
23
+ *
24
+ * This file mirrors `aid_runtime/control.py`.
25
+ */
26
+ import { type Pipeline, type PipelineContext } from './pipeline.js';
27
+ import { type PolicyNode } from './policy.js';
28
+ /**
29
+ * Deepest composite nesting a pipeline may declare.
30
+ *
31
+ * The spec layer owns this number and the generator *emits* it into the generated
32
+ * package, so a generated app passes its own build's value; this default is what a
33
+ * hand-written pipeline gets. A test reads the spec constant and holds the two
34
+ * together, so a drift fails instead of diverging silently.
35
+ */
36
+ export declare const MAX_STEP_DEPTH = 4;
37
+ /** Who the caller is, for `branch`. Seeded by the route. */
38
+ export declare const PRINCIPAL_KEY = "principal";
39
+ /** The application's single limiter, from `deployments.concurrency`. */
40
+ export declare const LIMITER_KEY = "limiter";
41
+ /** The current item and its position, written by `map`/`reduce` for one iteration. */
42
+ export declare const ITEM_KEY = "item";
43
+ export declare const INDEX_KEY = "index";
44
+ /** How many composites deep the current step is. */
45
+ export declare const DEPTH_KEY = "depth";
46
+ /**
47
+ * The binding `reduce` collects from each iteration. Reserved so a reduce body's own
48
+ * binding cannot collide with the name the composite reads.
49
+ */
50
+ export declare const REDUCED_KEY = "reduced";
51
+ /** Names a composite owns. A spec binding equal to one of them is refused upstream. */
52
+ export declare const RESERVED_KEYS: ReadonlySet<string>;
53
+ /** A composite: always async, so `await step(context)` is well-typed. */
54
+ export type CompositeStep = (context: PipelineContext) => Promise<PipelineContext>;
55
+ /**
56
+ * Run `then` when the caller's principal satisfies `when`, else `otherwise`.
57
+ *
58
+ * `when` is a `PolicyExpr` AST, evaluated against the principal and nothing else: the
59
+ * grammar has no operand for a context value, so a branch cannot read what an earlier
60
+ * step produced. It is a plain mapping rather than a parsed object so the generated code
61
+ * passes the AST the IR already carries, and {@link evaluatePolicy} stays the only
62
+ * evaluator.
63
+ *
64
+ * No principal in the context is an error, not a false. Defaulting would decide an
65
+ * authorization question for a caller nobody identified.
66
+ */
67
+ export declare function branch(options: {
68
+ readonly when: PolicyNode;
69
+ readonly then: Pipeline;
70
+ readonly otherwise?: Pipeline;
71
+ readonly maxDepth?: number;
72
+ readonly name?: string;
73
+ }): CompositeStep;
74
+ /**
75
+ * Run every arm concurrently under the shared cap, then merge what they bound.
76
+ *
77
+ * A failing arm fails the whole run: the results are inspected in input order and the
78
+ * first failure is raised, so which error surfaces does not depend on which arm happened
79
+ * to fail first. Two arms binding the same new name is an error rather than a silent
80
+ * last-writer-wins, because the winning value would depend on completion order and the
81
+ * ordering rule exists to remove that.
82
+ */
83
+ export declare function parallel(options: {
84
+ readonly branches: readonly Pipeline[];
85
+ readonly maxDepth?: number;
86
+ readonly name?: string;
87
+ }): CompositeStep;
88
+ /**
89
+ * Run `step` once per item of `over`, binding the results in input order.
90
+ *
91
+ * `as` holds one mapping of the names the body bound for that item, so an item that
92
+ * bound nothing contributes `{}` rather than vanishing: the list keeps the length of the
93
+ * input. An empty or absent-over value binds an empty list.
94
+ */
95
+ export declare function mapStep(options: {
96
+ readonly over: string;
97
+ readonly as: string;
98
+ readonly step: Pipeline;
99
+ readonly maxDepth?: number;
100
+ readonly name?: string;
101
+ }): CompositeStep;
102
+ /**
103
+ * Run `step` once per item of `over`, binding its {@link REDUCED_KEY} results.
104
+ *
105
+ * `step` is a one-step pipeline that renders the reduce prompt for one item and binds
106
+ * `reduced`; the composite collects that binding into `as` in input order. Combining the
107
+ * results is a following `generate` step, so this layer never invents a merge strategy.
108
+ */
109
+ export declare function reduceStep(options: {
110
+ readonly over: string;
111
+ readonly as: string;
112
+ readonly step: Pipeline;
113
+ readonly maxDepth?: number;
114
+ readonly name?: string;
115
+ }): CompositeStep;
116
+ //# sourceMappingURL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