@yolk-sdk/harness 0.1.0-canary.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +206 -0
  3. package/dist/coordinator.d.mts +72 -0
  4. package/dist/coordinator.d.mts.map +1 -0
  5. package/dist/coordinator.mjs +154 -0
  6. package/dist/coordinator.mjs.map +1 -0
  7. package/dist/driver/durable-object.d.mts +20 -0
  8. package/dist/driver/durable-object.d.mts.map +1 -0
  9. package/dist/driver/durable-object.mjs +12 -0
  10. package/dist/driver/durable-object.mjs.map +1 -0
  11. package/dist/driver/memory.d.mts +21 -0
  12. package/dist/driver/memory.d.mts.map +1 -0
  13. package/dist/driver/memory.mjs +15 -0
  14. package/dist/driver/memory.mjs.map +1 -0
  15. package/dist/driver.d.mts +126 -0
  16. package/dist/driver.d.mts.map +1 -0
  17. package/dist/driver.mjs +96 -0
  18. package/dist/driver.mjs.map +1 -0
  19. package/dist/inbox.d.mts +368 -0
  20. package/dist/inbox.d.mts.map +1 -0
  21. package/dist/inbox.mjs +235 -0
  22. package/dist/inbox.mjs.map +1 -0
  23. package/dist/index.d.mts +1 -0
  24. package/dist/index.mjs +1 -0
  25. package/dist/outcome-constructors-internal.d.mts +403 -0
  26. package/dist/outcome-constructors-internal.d.mts.map +1 -0
  27. package/dist/outcome-constructors-internal.mjs +11 -0
  28. package/dist/outcome-constructors-internal.mjs.map +1 -0
  29. package/dist/outcome.d.mts +361 -0
  30. package/dist/outcome.d.mts.map +1 -0
  31. package/dist/outcome.mjs +136 -0
  32. package/dist/outcome.mjs.map +1 -0
  33. package/dist/store.d.mts +39 -0
  34. package/dist/store.d.mts.map +1 -0
  35. package/dist/store.mjs +98 -0
  36. package/dist/store.mjs.map +1 -0
  37. package/package.json +90 -0
  38. package/src/coordinator.ts +355 -0
  39. package/src/driver/durable-object.ts +28 -0
  40. package/src/driver/memory.ts +39 -0
  41. package/src/driver.ts +270 -0
  42. package/src/inbox.ts +528 -0
  43. package/src/index.ts +2 -0
  44. package/src/outcome-constructors-internal.ts +18 -0
  45. package/src/outcome.ts +377 -0
  46. package/src/store.ts +161 -0
@@ -0,0 +1,361 @@
1
+ import { Data, Effect } from "effect";
2
+ import { AgentEvent, AgentMessage, AgentUsage, HitlRequest, HitlResponse } from "@yolk-sdk/agent/protocol";
3
+ import { AgentLoopError, ContextTransformer, LLMProvider, LoopConfig, ModelTurnConfig, ModelTurnResult, ToolBatchConfig, ToolExecutor } from "@yolk-sdk/agent/loop";
4
+
5
+ //#region src/outcome.d.ts
6
+ type OverflowCompactionResult = {
7
+ readonly _tag: 'Compacted';
8
+ readonly messages: ReadonlyArray<AgentMessage>;
9
+ } | {
10
+ readonly _tag: 'Skipped';
11
+ };
12
+ declare const OverflowCompactionResult: {
13
+ readonly Compacted: Data.TaggedEnum.ConstructorFrom<{
14
+ readonly _tag: "Compacted";
15
+ readonly messages: ReadonlyArray<AgentMessage>;
16
+ }, "_tag">;
17
+ readonly Skipped: Data.TaggedEnum.ConstructorFrom<{
18
+ readonly _tag: "Skipped";
19
+ }, "_tag">;
20
+ readonly $is: <Tag extends "Compacted" | "Skipped">(tag: Tag) => (u: unknown) => u is Extract<{
21
+ readonly _tag: "Compacted";
22
+ readonly messages: ReadonlyArray<AgentMessage>;
23
+ }, {
24
+ readonly _tag: Tag;
25
+ }> | Extract<{
26
+ readonly _tag: "Skipped";
27
+ }, {
28
+ readonly _tag: Tag;
29
+ }>;
30
+ readonly $match: {
31
+ <Cases extends {
32
+ readonly Compacted: (args: {
33
+ readonly _tag: "Compacted";
34
+ readonly messages: ReadonlyArray<AgentMessage>;
35
+ }) => any;
36
+ readonly Skipped: (args: {
37
+ readonly _tag: "Skipped";
38
+ }) => any;
39
+ }>(cases: Cases): (value: OverflowCompactionResult) => import("effect/Unify").Unify<ReturnType<Cases["Compacted" | "Skipped"]>>;
40
+ <Cases extends {
41
+ readonly Compacted: (args: {
42
+ readonly _tag: "Compacted";
43
+ readonly messages: ReadonlyArray<AgentMessage>;
44
+ }) => any;
45
+ readonly Skipped: (args: {
46
+ readonly _tag: "Skipped";
47
+ }) => any;
48
+ }>(value: OverflowCompactionResult, cases: Cases): import("effect/Unify").Unify<ReturnType<Cases["Compacted" | "Skipped"]>>;
49
+ };
50
+ };
51
+ type StepOutcome = Data.TaggedEnum<{
52
+ Completed: {
53
+ readonly needsContinuation: boolean;
54
+ readonly assistantMessage: ModelTurnResult['assistantMessage'];
55
+ readonly toolCalls: ModelTurnResult['toolCalls'];
56
+ readonly usage: ModelTurnResult['usage'];
57
+ readonly stopReason: ModelTurnResult['stopReason'];
58
+ };
59
+ Retry: {
60
+ readonly error: AgentLoopError;
61
+ };
62
+ Continue: {
63
+ readonly error: AgentLoopError;
64
+ readonly assistantMessage: ModelTurnResult['assistantMessage'];
65
+ readonly toolCalls: ModelTurnResult['toolCalls'];
66
+ readonly usage: ModelTurnResult['usage'];
67
+ readonly stopReason: ModelTurnResult['stopReason'];
68
+ };
69
+ RecoverFull: {
70
+ readonly error: AgentLoopError;
71
+ };
72
+ Compacted: {
73
+ readonly messages: ReadonlyArray<AgentMessage>;
74
+ readonly overflowCompactionAttempt: number;
75
+ };
76
+ AwaitingInput: {
77
+ readonly requests: ReadonlyArray<HitlRequest>;
78
+ readonly usage: AgentUsage;
79
+ };
80
+ }>;
81
+ declare const StepOutcome: {
82
+ readonly Compacted: Data.TaggedEnum.ConstructorFrom<{
83
+ readonly _tag: "Compacted";
84
+ readonly messages: ReadonlyArray<AgentMessage>;
85
+ readonly overflowCompactionAttempt: number;
86
+ }, "_tag">;
87
+ readonly Completed: Data.TaggedEnum.ConstructorFrom<{
88
+ readonly _tag: "Completed";
89
+ readonly needsContinuation: boolean;
90
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
91
+ readonly toolCalls: ModelTurnResult["toolCalls"];
92
+ readonly usage: ModelTurnResult["usage"];
93
+ readonly stopReason: ModelTurnResult["stopReason"];
94
+ }, "_tag">;
95
+ readonly Retry: Data.TaggedEnum.ConstructorFrom<{
96
+ readonly _tag: "Retry";
97
+ readonly error: AgentLoopError;
98
+ }, "_tag">;
99
+ readonly Continue: Data.TaggedEnum.ConstructorFrom<{
100
+ readonly _tag: "Continue";
101
+ readonly error: AgentLoopError;
102
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
103
+ readonly toolCalls: ModelTurnResult["toolCalls"];
104
+ readonly usage: ModelTurnResult["usage"];
105
+ readonly stopReason: ModelTurnResult["stopReason"];
106
+ }, "_tag">;
107
+ readonly RecoverFull: Data.TaggedEnum.ConstructorFrom<{
108
+ readonly _tag: "RecoverFull";
109
+ readonly error: AgentLoopError;
110
+ }, "_tag">;
111
+ readonly AwaitingInput: Data.TaggedEnum.ConstructorFrom<{
112
+ readonly _tag: "AwaitingInput";
113
+ readonly requests: ReadonlyArray<HitlRequest>;
114
+ readonly usage: AgentUsage;
115
+ }, "_tag">;
116
+ readonly $is: <Tag extends "Compacted" | "Completed" | "Retry" | "Continue" | "RecoverFull" | "AwaitingInput">(tag: Tag) => (u: unknown) => u is Extract<{
117
+ readonly _tag: "Compacted";
118
+ readonly messages: ReadonlyArray<AgentMessage>;
119
+ readonly overflowCompactionAttempt: number;
120
+ }, {
121
+ readonly _tag: Tag;
122
+ }> | Extract<{
123
+ readonly _tag: "Completed";
124
+ readonly needsContinuation: boolean;
125
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
126
+ readonly toolCalls: ModelTurnResult["toolCalls"];
127
+ readonly usage: ModelTurnResult["usage"];
128
+ readonly stopReason: ModelTurnResult["stopReason"];
129
+ }, {
130
+ readonly _tag: Tag;
131
+ }> | Extract<{
132
+ readonly _tag: "Retry";
133
+ readonly error: AgentLoopError;
134
+ }, {
135
+ readonly _tag: Tag;
136
+ }> | Extract<{
137
+ readonly _tag: "Continue";
138
+ readonly error: AgentLoopError;
139
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
140
+ readonly toolCalls: ModelTurnResult["toolCalls"];
141
+ readonly usage: ModelTurnResult["usage"];
142
+ readonly stopReason: ModelTurnResult["stopReason"];
143
+ }, {
144
+ readonly _tag: Tag;
145
+ }> | Extract<{
146
+ readonly _tag: "RecoverFull";
147
+ readonly error: AgentLoopError;
148
+ }, {
149
+ readonly _tag: Tag;
150
+ }> | Extract<{
151
+ readonly _tag: "AwaitingInput";
152
+ readonly requests: ReadonlyArray<HitlRequest>;
153
+ readonly usage: AgentUsage;
154
+ }, {
155
+ readonly _tag: Tag;
156
+ }>;
157
+ readonly $match: {
158
+ <Cases extends {
159
+ readonly Compacted: (args: {
160
+ readonly _tag: "Compacted";
161
+ readonly messages: ReadonlyArray<AgentMessage>;
162
+ readonly overflowCompactionAttempt: number;
163
+ }) => any;
164
+ readonly Completed: (args: {
165
+ readonly _tag: "Completed";
166
+ readonly needsContinuation: boolean;
167
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
168
+ readonly toolCalls: ModelTurnResult["toolCalls"];
169
+ readonly usage: ModelTurnResult["usage"];
170
+ readonly stopReason: ModelTurnResult["stopReason"];
171
+ }) => any;
172
+ readonly Retry: (args: {
173
+ readonly _tag: "Retry";
174
+ readonly error: AgentLoopError;
175
+ }) => any;
176
+ readonly Continue: (args: {
177
+ readonly _tag: "Continue";
178
+ readonly error: AgentLoopError;
179
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
180
+ readonly toolCalls: ModelTurnResult["toolCalls"];
181
+ readonly usage: ModelTurnResult["usage"];
182
+ readonly stopReason: ModelTurnResult["stopReason"];
183
+ }) => any;
184
+ readonly RecoverFull: (args: {
185
+ readonly _tag: "RecoverFull";
186
+ readonly error: AgentLoopError;
187
+ }) => any;
188
+ readonly AwaitingInput: (args: {
189
+ readonly _tag: "AwaitingInput";
190
+ readonly requests: ReadonlyArray<HitlRequest>;
191
+ readonly usage: AgentUsage;
192
+ }) => any;
193
+ }>(cases: Cases): (value: {
194
+ readonly _tag: "Compacted";
195
+ readonly messages: ReadonlyArray<AgentMessage>;
196
+ readonly overflowCompactionAttempt: number;
197
+ } | {
198
+ readonly _tag: "Completed";
199
+ readonly needsContinuation: boolean;
200
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
201
+ readonly toolCalls: ModelTurnResult["toolCalls"];
202
+ readonly usage: ModelTurnResult["usage"];
203
+ readonly stopReason: ModelTurnResult["stopReason"];
204
+ } | {
205
+ readonly _tag: "Retry";
206
+ readonly error: AgentLoopError;
207
+ } | {
208
+ readonly _tag: "Continue";
209
+ readonly error: AgentLoopError;
210
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
211
+ readonly toolCalls: ModelTurnResult["toolCalls"];
212
+ readonly usage: ModelTurnResult["usage"];
213
+ readonly stopReason: ModelTurnResult["stopReason"];
214
+ } | {
215
+ readonly _tag: "RecoverFull";
216
+ readonly error: AgentLoopError;
217
+ } | {
218
+ readonly _tag: "AwaitingInput";
219
+ readonly requests: ReadonlyArray<HitlRequest>;
220
+ readonly usage: AgentUsage;
221
+ }) => import("effect/Unify").Unify<ReturnType<Cases["Compacted" | "Completed" | "Retry" | "Continue" | "RecoverFull" | "AwaitingInput"]>>;
222
+ <Cases extends {
223
+ readonly Compacted: (args: {
224
+ readonly _tag: "Compacted";
225
+ readonly messages: ReadonlyArray<AgentMessage>;
226
+ readonly overflowCompactionAttempt: number;
227
+ }) => any;
228
+ readonly Completed: (args: {
229
+ readonly _tag: "Completed";
230
+ readonly needsContinuation: boolean;
231
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
232
+ readonly toolCalls: ModelTurnResult["toolCalls"];
233
+ readonly usage: ModelTurnResult["usage"];
234
+ readonly stopReason: ModelTurnResult["stopReason"];
235
+ }) => any;
236
+ readonly Retry: (args: {
237
+ readonly _tag: "Retry";
238
+ readonly error: AgentLoopError;
239
+ }) => any;
240
+ readonly Continue: (args: {
241
+ readonly _tag: "Continue";
242
+ readonly error: AgentLoopError;
243
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
244
+ readonly toolCalls: ModelTurnResult["toolCalls"];
245
+ readonly usage: ModelTurnResult["usage"];
246
+ readonly stopReason: ModelTurnResult["stopReason"];
247
+ }) => any;
248
+ readonly RecoverFull: (args: {
249
+ readonly _tag: "RecoverFull";
250
+ readonly error: AgentLoopError;
251
+ }) => any;
252
+ readonly AwaitingInput: (args: {
253
+ readonly _tag: "AwaitingInput";
254
+ readonly requests: ReadonlyArray<HitlRequest>;
255
+ readonly usage: AgentUsage;
256
+ }) => any;
257
+ }>(value: {
258
+ readonly _tag: "Compacted";
259
+ readonly messages: ReadonlyArray<AgentMessage>;
260
+ readonly overflowCompactionAttempt: number;
261
+ } | {
262
+ readonly _tag: "Completed";
263
+ readonly needsContinuation: boolean;
264
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
265
+ readonly toolCalls: ModelTurnResult["toolCalls"];
266
+ readonly usage: ModelTurnResult["usage"];
267
+ readonly stopReason: ModelTurnResult["stopReason"];
268
+ } | {
269
+ readonly _tag: "Retry";
270
+ readonly error: AgentLoopError;
271
+ } | {
272
+ readonly _tag: "Continue";
273
+ readonly error: AgentLoopError;
274
+ readonly assistantMessage: ModelTurnResult["assistantMessage"];
275
+ readonly toolCalls: ModelTurnResult["toolCalls"];
276
+ readonly usage: ModelTurnResult["usage"];
277
+ readonly stopReason: ModelTurnResult["stopReason"];
278
+ } | {
279
+ readonly _tag: "RecoverFull";
280
+ readonly error: AgentLoopError;
281
+ } | {
282
+ readonly _tag: "AwaitingInput";
283
+ readonly requests: ReadonlyArray<HitlRequest>;
284
+ readonly usage: AgentUsage;
285
+ }, cases: Cases): import("effect/Unify").Unify<ReturnType<Cases["Compacted" | "Completed" | "Retry" | "Continue" | "RecoverFull" | "AwaitingInput"]>>;
286
+ };
287
+ };
288
+ type CompletedTurn = Extract<StepOutcome, {
289
+ readonly _tag: 'Completed';
290
+ }>;
291
+ type ModelTurnOutcome = Exclude<StepOutcome, {
292
+ readonly _tag: 'AwaitingInput';
293
+ }>;
294
+ type ToolBatchOutcome = Extract<StepOutcome, {
295
+ readonly _tag: 'Completed' | 'AwaitingInput';
296
+ }>;
297
+ declare const attemptModelTurn: <E2 = never, R2 = never>(config: ModelTurnConfig, options?: {
298
+ readonly onEvent?: (event: AgentEvent) => Effect.Effect<void, E2, R2>;
299
+ readonly initialUsage?: AgentUsage;
300
+ readonly overflowCompactionAttempt?: number;
301
+ readonly compact?: (messages: ReadonlyArray<AgentMessage>) => Effect.Effect<OverflowCompactionResult, E2, R2>;
302
+ }) => Effect.Effect<ModelTurnOutcome, AgentLoopError | E2, ContextTransformer | LLMProvider | LoopConfig | R2>;
303
+ declare const attemptToolBatch: <E2 = never, R2 = never>(config: ToolBatchConfig, options?: {
304
+ readonly onEvent?: (event: AgentEvent) => Effect.Effect<void, E2, R2>;
305
+ }) => Effect.Effect<ToolBatchOutcome, AgentLoopError | E2, LoopConfig | ToolExecutor | R2>;
306
+ type HitlMatch = {
307
+ readonly _tag: 'Match';
308
+ readonly requestId: string;
309
+ } | {
310
+ readonly _tag: 'Mismatch';
311
+ };
312
+ declare const HitlMatch: {
313
+ readonly Match: Data.TaggedEnum.ConstructorFrom<{
314
+ readonly _tag: "Match";
315
+ readonly requestId: string;
316
+ }, "_tag">;
317
+ readonly Mismatch: Data.TaggedEnum.ConstructorFrom<{
318
+ readonly _tag: "Mismatch";
319
+ }, "_tag">;
320
+ readonly $is: <Tag extends "Match" | "Mismatch">(tag: Tag) => (u: unknown) => u is Extract<{
321
+ readonly _tag: "Match";
322
+ readonly requestId: string;
323
+ }, {
324
+ readonly _tag: Tag;
325
+ }> | Extract<{
326
+ readonly _tag: "Mismatch";
327
+ }, {
328
+ readonly _tag: Tag;
329
+ }>;
330
+ readonly $match: {
331
+ <Cases extends {
332
+ readonly Match: (args: {
333
+ readonly _tag: "Match";
334
+ readonly requestId: string;
335
+ }) => any;
336
+ readonly Mismatch: (args: {
337
+ readonly _tag: "Mismatch";
338
+ }) => any;
339
+ }>(cases: Cases): (value: HitlMatch) => import("effect/Unify").Unify<ReturnType<Cases["Match" | "Mismatch"]>>;
340
+ <Cases extends {
341
+ readonly Match: (args: {
342
+ readonly _tag: "Match";
343
+ readonly requestId: string;
344
+ }) => any;
345
+ readonly Mismatch: (args: {
346
+ readonly _tag: "Mismatch";
347
+ }) => any;
348
+ }>(value: HitlMatch, cases: Cases): import("effect/Unify").Unify<ReturnType<Cases["Match" | "Mismatch"]>>;
349
+ };
350
+ };
351
+ declare const matchHitlResponse: (pending: ReadonlyArray<HitlRequest>, response: HitlResponse) => HitlMatch;
352
+ declare const resumeHitlIfMatched: <A, E, R>(input: {
353
+ readonly pending: ReadonlyArray<HitlRequest>;
354
+ readonly response: HitlResponse;
355
+ readonly resume: (requestId: string) => Effect.Effect<A, E, R>;
356
+ }) => Effect.Effect<A | Extract<HitlMatch, {
357
+ readonly _tag: "Mismatch";
358
+ }>, E, R>;
359
+ //#endregion
360
+ export { CompletedTurn, HitlMatch, ModelTurnOutcome, OverflowCompactionResult, StepOutcome, ToolBatchOutcome, attemptModelTurn, attemptToolBatch, matchHitlResponse, resumeHitlIfMatched };
361
+ //# sourceMappingURL=outcome.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outcome.d.mts","names":[],"sources":["../src/outcome.ts"],"mappings":";;;;;KAkCY,wBAAA;EAAA,SACG,IAAA;EAAA,SAA4B,QAAA,EAAU,aAAa,CAAC,YAAA;AAAA;EAAA,SACpD,IAAA;AAAA;AAAA,cAEF,wBAAA;EAAA;;uBAHwC,aAAA,CAAc,YAAA;EAAA;EAAA;;;;;uBAAd,aAAA,CAAc,YAAA;EAAA;IAAA;;;;;;;;;;2BAAd,aAAA,CAAc,YAAA;MAAA;MAAA;;;;;;;2BAAd,aAAA,CAAc,YAAA;MAAA;MAAA;;;;;;KAKvD,WAAA,GAAc,IAAA,CAAK,UAAA;EAC7B,SAAA;IAAA,SACW,iBAAA;IAAA,SACA,gBAAA,EAAkB,eAAA;IAAA,SAClB,SAAA,EAAW,eAAA;IAAA,SAC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YAAA;EAAA,SACV,MAAA,GAAS,SAAA,aAAsB,MAAA,CAAO,MAAA,CAAO,CAAA,EAAG,CAAA,EAAG,CAAA;AAAA,MAC1D,MAAA,CAAO,MAAA,CAAO,CAAA,GAAI,OAAA,CAAQ,SAAA;EAAA,SAAsB,IAAA;AAAA,IAAqB,CAAA,EAAG,CAAA"}
@@ -0,0 +1,136 @@
1
+ import { HitlMatch as HitlMatch$1, OverflowCompactionResult as OverflowCompactionResult$1, StepOutcome as StepOutcome$1 } from "./outcome-constructors-internal.mjs";
2
+ import { Effect, Match, Predicate, Stream } from "effect";
3
+ import { addAgentUsage, zeroAgentUsage } from "@yolk-sdk/agent/protocol";
4
+ import { LLMError, collectModelTurnAttempt, runModelTurn, runToolBatch } from "@yolk-sdk/agent/loop";
5
+ import { applyOverflowCompaction, isOverflowCompactionAttemptCount } from "@yolk-sdk/agent/compaction";
6
+ //#region src/outcome.ts
7
+ const OverflowCompactionResult = OverflowCompactionResult$1;
8
+ const StepOutcome = StepOutcome$1;
9
+ const completedOutcome = (result) => StepOutcome.Completed({
10
+ needsContinuation: result.stopReason === "tool_use",
11
+ assistantMessage: result.assistantMessage,
12
+ toolCalls: result.toolCalls,
13
+ usage: result.usage,
14
+ stopReason: result.stopReason
15
+ });
16
+ const modelTurnResult = (result) => ({
17
+ assistantMessage: result.assistantMessage,
18
+ toolCalls: result.toolCalls,
19
+ usage: result.usage,
20
+ stopReason: result.stopReason
21
+ });
22
+ const invalidOverflowCompactionAttemptError = () => new LLMError({
23
+ cause: "validation_error",
24
+ message: "overflowCompactionAttempt must be a finite integer >= 0",
25
+ retryable: false
26
+ });
27
+ const isAgentLoopError = (error) => {
28
+ if (!Predicate.isObjectOrArray(error) || !("_tag" in error)) return false;
29
+ const tag = error._tag;
30
+ return tag === "LLMError" || tag === "FauxExhaustedError" || tag === "ToolError" || tag === "ContextTransformError" || tag === "AbortError";
31
+ };
32
+ const isOverflow = (error) => Match.value(error).pipe(Match.tag("LLMError", "ContextTransformError", (current) => current.cause === "context_overflow"), Match.orElse(() => false));
33
+ const isMissingDone = (error) => Match.value(error).pipe(Match.tag("LLMError", (current) => current.responseIssue === "missing_done"), Match.orElse(() => false));
34
+ const isRetryableLlm = (error) => Predicate.isTagged(error, "LLMError") && error.retryable;
35
+ const classifyModelTurnFailure = (input) => {
36
+ if (isOverflow(input.error)) {
37
+ if (input.compact === void 0) return Effect.fail(input.error);
38
+ return applyOverflowCompaction({
39
+ compact: input.compact,
40
+ messages: input.messages,
41
+ attempt: input.overflowCompactionAttempt,
42
+ outputStarted: input.outputStarted
43
+ }).pipe(Effect.flatMap((result) => Match.value(result).pipe(Match.tag("Compacted", (current) => Effect.succeed(StepOutcome.Compacted({
44
+ messages: current.messages,
45
+ overflowCompactionAttempt: input.overflowCompactionAttempt + 1
46
+ }))), Match.orElse(() => Effect.fail(input.error)))));
47
+ }
48
+ if (isMissingDone(input.error) && !input.outputStarted) return Effect.succeed(StepOutcome.RecoverFull({ error: input.error }));
49
+ if (isMissingDone(input.error) && input.outputStarted) return Effect.succeed(StepOutcome.Continue({
50
+ error: input.error,
51
+ ...modelTurnResult(input.collected)
52
+ }));
53
+ if (isRetryableLlm(input.error) && !input.outputStarted) {
54
+ if (input.error.cause === "invalid_response") return Effect.succeed(StepOutcome.RecoverFull({ error: input.error }));
55
+ return Effect.succeed(StepOutcome.Retry({ error: input.error }));
56
+ }
57
+ if (isRetryableLlm(input.error) && input.outputStarted) return Effect.succeed(StepOutcome.Continue({
58
+ error: input.error,
59
+ ...modelTurnResult(input.collected)
60
+ }));
61
+ return Effect.fail(input.error);
62
+ };
63
+ const attemptModelTurn = (config, options) => Effect.gen(function* () {
64
+ const overflowCompactionAttempt = options?.overflowCompactionAttempt ?? 0;
65
+ if (!isOverflowCompactionAttemptCount(overflowCompactionAttempt)) return yield* Effect.fail(invalidOverflowCompactionAttemptError());
66
+ const outcome = yield* collectModelTurnAttempt(runModelTurn(config), {
67
+ onEvent: options?.onEvent,
68
+ initialUsage: options?.initialUsage
69
+ });
70
+ return yield* Match.value(outcome).pipe(Match.tag("Collected", (current) => Effect.succeed(completedOutcome(current.collection))), Match.tag("SinkFailed", (current) => Effect.fail(current.error)), Match.tag("StreamFailed", (current) => {
71
+ if (!isAgentLoopError(current.error)) return Effect.fail(current.error);
72
+ return classifyModelTurnFailure({
73
+ error: current.error,
74
+ collected: {
75
+ assistantMessage: current.collection.assistantMessage ?? current.collection.partialAssistantMessage,
76
+ toolCalls: current.collection.toolCalls,
77
+ usage: current.collection.usage,
78
+ stopReason: current.collection.stopReason
79
+ },
80
+ outputStarted: current.collection.outputStarted,
81
+ messages: config.messages,
82
+ overflowCompactionAttempt,
83
+ compact: options?.compact
84
+ });
85
+ }), Match.exhaustive);
86
+ });
87
+ const attemptToolBatch = (config, options) => {
88
+ const onEvent = options?.onEvent;
89
+ return runToolBatch(config).pipe(Stream.runFoldEffect(() => ({
90
+ requests: [],
91
+ usage: config.usage ?? zeroAgentUsage,
92
+ toolCalls: []
93
+ }), (acc, event) => {
94
+ const next = Predicate.isTagged(event, "AgentAwaitingInput") ? {
95
+ ...acc,
96
+ requests: event.requests,
97
+ usage: event.usage
98
+ } : Predicate.isTagged(event, "UsageUpdate") ? {
99
+ ...acc,
100
+ usage: addAgentUsage(acc.usage, event.usage)
101
+ } : Predicate.isTagged(event, "ToolExecutionCompleted") || Predicate.isTagged(event, "ToolExecutionAccepted") ? {
102
+ ...acc,
103
+ toolCalls: [...acc.toolCalls, event.call]
104
+ } : acc;
105
+ return onEvent === void 0 ? Effect.succeed(next) : onEvent(event).pipe(Effect.as(next));
106
+ }), Effect.map((result) => {
107
+ if (result.requests.length === 0) {
108
+ const needsContinuation = result.toolCalls.length > 0;
109
+ return StepOutcome.Completed({
110
+ needsContinuation,
111
+ assistantMessage: void 0,
112
+ toolCalls: result.toolCalls,
113
+ usage: result.usage,
114
+ stopReason: needsContinuation ? "tool_use" : "stop"
115
+ });
116
+ }
117
+ return StepOutcome.AwaitingInput({
118
+ requests: result.requests,
119
+ usage: result.usage
120
+ });
121
+ }));
122
+ };
123
+ const HitlMatch = HitlMatch$1;
124
+ const hitlResponseMatchesRequest = (response, request) => Match.value(response).pipe(Match.tag("ToolApprovalResponse", (current) => Predicate.isTagged(request, "ToolApprovalRequest") && current.requestId === request.requestId && current.toolCallId === request.toolCallId), Match.tag("QuestionResponse", (current) => Predicate.isTagged(request, "QuestionRequest") && current.requestId === request.requestId && current.toolCallId === request.toolCallId), Match.tag("InputResponse", (current) => Predicate.isTagged(request, "InputRequest") && current.requestId === request.requestId && current.toolCallId === request.toolCallId), Match.tag("InteractionResponse", (current) => Predicate.isTagged(request, "InteractionRequest") && current.requestId === request.requestId && current.toolCallId === request.toolCallId), Match.exhaustive);
125
+ const matchHitlResponse = (pending, response) => {
126
+ const matched = pending.find((request) => hitlResponseMatchesRequest(response, request));
127
+ return matched === void 0 ? HitlMatch.Mismatch() : HitlMatch.Match({ requestId: matched.requestId });
128
+ };
129
+ const resumeHitlIfMatched = (input) => {
130
+ const matched = matchHitlResponse(input.pending, input.response);
131
+ return Match.value(matched).pipe(Match.tag("Mismatch", (current) => Effect.succeed(current)), Match.tag("Match", (current) => input.resume(current.requestId)), Match.exhaustive);
132
+ };
133
+ //#endregion
134
+ export { HitlMatch, OverflowCompactionResult, StepOutcome, attemptModelTurn, attemptToolBatch, matchHitlResponse, resumeHitlIfMatched };
135
+
136
+ //# sourceMappingURL=outcome.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outcome.mjs","names":["overflowCompactionResult","stepOutcome","hitlMatch"],"sources":["../src/outcome.ts"],"sourcesContent":["import { Effect, Match, Predicate, Stream, type Data } from 'effect'\nimport {\n HitlMatch as hitlMatch,\n OverflowCompactionResult as overflowCompactionResult,\n StepOutcome as stepOutcome\n} from './outcome-constructors-internal.ts'\nimport {\n addAgentUsage,\n zeroAgentUsage,\n type AgentEvent,\n type AgentMessage,\n type AgentUsage,\n type HitlRequest,\n type HitlResponse\n} from '@yolk-sdk/agent/protocol'\nimport {\n collectModelTurnAttempt,\n LLMError,\n runModelTurn,\n runToolBatch,\n type AgentLoopError,\n type ContextTransformer,\n type LLMProvider,\n type LoopConfig,\n type ModelTurnConfig,\n type ModelTurnResult,\n type ToolBatchConfig,\n type ToolExecutor\n} from '@yolk-sdk/agent/loop'\nimport {\n applyOverflowCompaction,\n isOverflowCompactionAttemptCount\n} from '@yolk-sdk/agent/compaction'\n\nexport type OverflowCompactionResult =\n | { readonly _tag: 'Compacted'; readonly messages: ReadonlyArray<AgentMessage> }\n | { readonly _tag: 'Skipped' }\n\nexport const OverflowCompactionResult = overflowCompactionResult\n\nexport type StepOutcome = Data.TaggedEnum<{\n Completed: {\n readonly needsContinuation: boolean\n readonly assistantMessage: ModelTurnResult['assistantMessage']\n readonly toolCalls: ModelTurnResult['toolCalls']\n readonly usage: ModelTurnResult['usage']\n readonly stopReason: ModelTurnResult['stopReason']\n }\n Retry: {\n readonly error: AgentLoopError\n }\n Continue: {\n readonly error: AgentLoopError\n readonly assistantMessage: ModelTurnResult['assistantMessage']\n readonly toolCalls: ModelTurnResult['toolCalls']\n readonly usage: ModelTurnResult['usage']\n readonly stopReason: ModelTurnResult['stopReason']\n }\n RecoverFull: {\n readonly error: AgentLoopError\n }\n Compacted: {\n readonly messages: ReadonlyArray<AgentMessage>\n readonly overflowCompactionAttempt: number\n }\n AwaitingInput: {\n readonly requests: ReadonlyArray<HitlRequest>\n readonly usage: AgentUsage\n }\n}>\n\nexport const StepOutcome = stepOutcome\n\nexport type CompletedTurn = Extract<StepOutcome, { readonly _tag: 'Completed' }>\n\nexport type ModelTurnOutcome = Exclude<StepOutcome, { readonly _tag: 'AwaitingInput' }>\n\nexport type ToolBatchOutcome = Extract<\n StepOutcome,\n { readonly _tag: 'Completed' | 'AwaitingInput' }\n>\n\nconst completedOutcome = (result: ModelTurnResult): CompletedTurn =>\n StepOutcome.Completed({\n needsContinuation: result.stopReason === 'tool_use',\n assistantMessage: result.assistantMessage,\n toolCalls: result.toolCalls,\n usage: result.usage,\n stopReason: result.stopReason\n })\n\nconst modelTurnResult = (result: ModelTurnResult): ModelTurnResult => ({\n assistantMessage: result.assistantMessage,\n toolCalls: result.toolCalls,\n usage: result.usage,\n stopReason: result.stopReason\n})\n\nconst invalidOverflowCompactionAttemptError = () =>\n new LLMError({\n cause: 'validation_error',\n message: 'overflowCompactionAttempt must be a finite integer >= 0',\n retryable: false\n })\n\nconst isAgentLoopError = (error: unknown): error is AgentLoopError => {\n if (!Predicate.isObjectOrArray(error) || !('_tag' in error)) {\n return false\n }\n\n const tag = error._tag\n\n return (\n tag === 'LLMError' ||\n tag === 'FauxExhaustedError' ||\n tag === 'ToolError' ||\n tag === 'ContextTransformError' ||\n tag === 'AbortError'\n )\n}\n\nconst isOverflow = (error: AgentLoopError) =>\n Match.value(error).pipe(\n Match.tag('LLMError', 'ContextTransformError', current => current.cause === 'context_overflow'),\n Match.orElse(() => false)\n )\n\nconst isMissingDone = (error: AgentLoopError) =>\n Match.value(error).pipe(\n Match.tag('LLMError', current => current.responseIssue === 'missing_done'),\n Match.orElse(() => false)\n )\n\nconst isRetryableLlm = (\n error: AgentLoopError\n): error is Extract<AgentLoopError, { _tag: 'LLMError' }> =>\n Predicate.isTagged(error, 'LLMError') && error.retryable\n\nconst classifyModelTurnFailure = <E2, R2>(input: {\n readonly error: AgentLoopError\n readonly collected: ModelTurnResult\n readonly outputStarted: boolean\n readonly messages: ReadonlyArray<AgentMessage>\n readonly overflowCompactionAttempt: number\n readonly compact?: (\n messages: ReadonlyArray<AgentMessage>\n ) => Effect.Effect<OverflowCompactionResult, E2, R2>\n}): Effect.Effect<ModelTurnOutcome, AgentLoopError | E2, R2> => {\n if (isOverflow(input.error)) {\n if (input.compact === undefined) {\n return Effect.fail(input.error)\n }\n\n return applyOverflowCompaction({\n compact: input.compact,\n messages: input.messages,\n attempt: input.overflowCompactionAttempt,\n outputStarted: input.outputStarted\n }).pipe(\n Effect.flatMap(result =>\n Match.value(result).pipe(\n Match.tag('Compacted', current =>\n Effect.succeed(\n StepOutcome.Compacted({\n messages: current.messages,\n overflowCompactionAttempt: input.overflowCompactionAttempt + 1\n })\n )\n ),\n Match.orElse(() => Effect.fail(input.error))\n )\n )\n )\n }\n\n if (isMissingDone(input.error) && !input.outputStarted) {\n return Effect.succeed(StepOutcome.RecoverFull({ error: input.error }))\n }\n\n if (isMissingDone(input.error) && input.outputStarted) {\n return Effect.succeed(\n StepOutcome.Continue({\n error: input.error,\n ...modelTurnResult(input.collected)\n })\n )\n }\n\n if (isRetryableLlm(input.error) && !input.outputStarted) {\n if (input.error.cause === 'invalid_response') {\n return Effect.succeed(StepOutcome.RecoverFull({ error: input.error }))\n }\n\n return Effect.succeed(StepOutcome.Retry({ error: input.error }))\n }\n\n if (isRetryableLlm(input.error) && input.outputStarted) {\n return Effect.succeed(\n StepOutcome.Continue({\n error: input.error,\n ...modelTurnResult(input.collected)\n })\n )\n }\n\n return Effect.fail(input.error)\n}\n\nexport const attemptModelTurn = <E2 = never, R2 = never>(\n config: ModelTurnConfig,\n options?: {\n readonly onEvent?: (event: AgentEvent) => Effect.Effect<void, E2, R2>\n readonly initialUsage?: AgentUsage\n readonly overflowCompactionAttempt?: number\n readonly compact?: (\n messages: ReadonlyArray<AgentMessage>\n ) => Effect.Effect<OverflowCompactionResult, E2, R2>\n }\n): Effect.Effect<\n ModelTurnOutcome,\n AgentLoopError | E2,\n ContextTransformer | LLMProvider | LoopConfig | R2\n> =>\n Effect.gen(function* () {\n const overflowCompactionAttempt = options?.overflowCompactionAttempt ?? 0\n\n if (!isOverflowCompactionAttemptCount(overflowCompactionAttempt)) {\n return yield* Effect.fail(invalidOverflowCompactionAttemptError())\n }\n\n const outcome = yield* collectModelTurnAttempt(runModelTurn(config), {\n onEvent: options?.onEvent,\n initialUsage: options?.initialUsage\n })\n\n return yield* Match.value(outcome).pipe(\n Match.tag('Collected', current => Effect.succeed(completedOutcome(current.collection))),\n Match.tag('SinkFailed', current => Effect.fail(current.error)),\n Match.tag('StreamFailed', current => {\n if (!isAgentLoopError(current.error)) {\n return Effect.fail(current.error)\n }\n\n return classifyModelTurnFailure({\n error: current.error,\n collected: {\n assistantMessage:\n current.collection.assistantMessage ?? current.collection.partialAssistantMessage,\n toolCalls: current.collection.toolCalls,\n usage: current.collection.usage,\n stopReason: current.collection.stopReason\n },\n outputStarted: current.collection.outputStarted,\n messages: config.messages,\n overflowCompactionAttempt,\n compact: options?.compact\n })\n }),\n Match.exhaustive\n )\n })\n\ntype AttemptToolBatchFoldState = {\n requests: ReadonlyArray<HitlRequest>\n usage: AgentUsage\n toolCalls: ModelTurnResult['toolCalls']\n}\n\nexport const attemptToolBatch = <E2 = never, R2 = never>(\n config: ToolBatchConfig,\n options?: {\n readonly onEvent?: (event: AgentEvent) => Effect.Effect<void, E2, R2>\n }\n): Effect.Effect<ToolBatchOutcome, AgentLoopError | E2, LoopConfig | ToolExecutor | R2> => {\n const onEvent = options?.onEvent\n\n return runToolBatch(config).pipe(\n Stream.runFoldEffect(\n (): AttemptToolBatchFoldState => ({\n requests: [],\n usage: config.usage ?? zeroAgentUsage,\n toolCalls: []\n }),\n (acc, event) => {\n const next = Predicate.isTagged(event, 'AgentAwaitingInput')\n ? { ...acc, requests: event.requests, usage: event.usage }\n : Predicate.isTagged(event, 'UsageUpdate')\n ? { ...acc, usage: addAgentUsage(acc.usage, event.usage) }\n : Predicate.isTagged(event, 'ToolExecutionCompleted') ||\n Predicate.isTagged(event, 'ToolExecutionAccepted')\n ? { ...acc, toolCalls: [...acc.toolCalls, event.call] }\n : acc\n\n return onEvent === undefined ? Effect.succeed(next) : onEvent(event).pipe(Effect.as(next))\n }\n ),\n Effect.map((result): ToolBatchOutcome => {\n if (result.requests.length === 0) {\n const needsContinuation = result.toolCalls.length > 0\n\n return StepOutcome.Completed({\n needsContinuation,\n assistantMessage: undefined,\n toolCalls: result.toolCalls,\n usage: result.usage,\n stopReason: needsContinuation ? 'tool_use' : 'stop'\n })\n }\n\n return StepOutcome.AwaitingInput({ requests: result.requests, usage: result.usage })\n })\n )\n}\n\nexport type HitlMatch =\n | { readonly _tag: 'Match'; readonly requestId: string }\n | { readonly _tag: 'Mismatch' }\n\nexport const HitlMatch = hitlMatch\n\nconst hitlResponseMatchesRequest = (response: HitlResponse, request: HitlRequest) =>\n Match.value(response).pipe(\n Match.tag(\n 'ToolApprovalResponse',\n current =>\n Predicate.isTagged(request, 'ToolApprovalRequest') &&\n current.requestId === request.requestId &&\n current.toolCallId === request.toolCallId\n ),\n Match.tag(\n 'QuestionResponse',\n current =>\n Predicate.isTagged(request, 'QuestionRequest') &&\n current.requestId === request.requestId &&\n current.toolCallId === request.toolCallId\n ),\n Match.tag(\n 'InputResponse',\n current =>\n Predicate.isTagged(request, 'InputRequest') &&\n current.requestId === request.requestId &&\n current.toolCallId === request.toolCallId\n ),\n Match.tag(\n 'InteractionResponse',\n current =>\n Predicate.isTagged(request, 'InteractionRequest') &&\n current.requestId === request.requestId &&\n current.toolCallId === request.toolCallId\n ),\n Match.exhaustive\n )\n\nexport const matchHitlResponse = (\n pending: ReadonlyArray<HitlRequest>,\n response: HitlResponse\n): HitlMatch => {\n const matched = pending.find(request => hitlResponseMatchesRequest(response, request))\n\n return matched === undefined\n ? HitlMatch.Mismatch()\n : HitlMatch.Match({ requestId: matched.requestId })\n}\n\nexport const resumeHitlIfMatched = <A, E, R>(input: {\n readonly pending: ReadonlyArray<HitlRequest>\n readonly response: HitlResponse\n readonly resume: (requestId: string) => Effect.Effect<A, E, R>\n}): Effect.Effect<A | Extract<HitlMatch, { readonly _tag: 'Mismatch' }>, E, R> => {\n const matched = matchHitlResponse(input.pending, input.response)\n\n return Match.value(matched).pipe(\n Match.tag('Mismatch', current => Effect.succeed(current)),\n Match.tag('Match', current => input.resume(current.requestId)),\n Match.exhaustive\n )\n}\n"],"mappings":";;;;;;AAsCA,MAAa,2BAA2BA;AAiCxC,MAAa,cAAcC;AAW3B,MAAM,oBAAoB,WACxB,YAAY,UAAU;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string) => Effect.Effect<void>;
6
+ readonly release: (runId: string) => Effect.Effect<void>;
7
+ readonly isClaimed: (runId: string) => Effect.Effect<boolean>;
8
+ readonly claimed: Effect.Effect<ReadonlySet<string>>;
9
+ readonly incrementResumeCount: (runId: string) => Effect.Effect<number>;
10
+ readonly resumeCount: (runId: string) => Effect.Effect<number>;
11
+ };
12
+ type SnapshotRunStoreOptions = {
13
+ readonly load: Effect.Effect<DurableRunStoreSnapshot | undefined>;
14
+ readonly save: (snapshot: DurableRunStoreSnapshot) => Effect.Effect<void>;
15
+ };
16
+ type DurableRunStoreSnapshot = {
17
+ readonly claimed: ReadonlyArray<string>;
18
+ readonly resumes: ReadonlyArray<readonly [string, number]>;
19
+ };
20
+ declare const RunStore_base: Context.ServiceClass<RunStore, "@yolk-sdk/harness/RunStore", RunStoreApi>;
21
+ declare class RunStore extends RunStore_base {
22
+ /**
23
+ * Canonical owning layer for snapshot-backed claims. Each call builds a fresh layer so
24
+ * composed harnesses never share `Ref` state or the snapshot lock across factory calls.
25
+ */
26
+ static snapshotLayer: (options: SnapshotRunStoreOptions) => Layer.Layer<RunStore>;
27
+ /**
28
+ * Canonical owning layer for process-local claims. Each call builds a fresh layer so
29
+ * composed harnesses never share `Ref` state across factory calls.
30
+ */
31
+ static inMemoryLayer: () => Layer.Layer<RunStore>;
32
+ }
33
+ /** Backward-compatible delegation to {@link RunStore.snapshotLayer}. */
34
+ declare const makeSnapshotRunStoreLayer: (options: SnapshotRunStoreOptions) => Layer.Layer<RunStore>;
35
+ /** Backward-compatible delegation to {@link RunStore.inMemoryLayer}. */
36
+ declare const makeInMemoryRunStoreLayer: () => Layer.Layer<RunStore>;
37
+ //#endregion
38
+ export { DurableRunStoreSnapshot, RunStore, RunStoreApi, SnapshotRunStoreOptions, makeInMemoryRunStoreLayer, makeSnapshotRunStoreLayer };
39
+ //# sourceMappingURL=store.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.mts","names":[],"sources":["../src/store.ts"],"mappings":";;;KAEY,WAAA;EAAA,SACD,KAAA,GAAQ,KAAA,aAAkB,MAAA,CAAO,MAAA;EAAA,SACjC,OAAA,GAAU,KAAA,aAAkB,MAAA,CAAO,MAAA;EAAA,SACnC,SAAA,GAAY,KAAA,aAAkB,MAAA,CAAO,MAAA;EAAA,SACrC,OAAA,EAAS,MAAA,CAAO,MAAA,CAAO,WAAA;EAAA,SACvB,oBAAA,GAAuB,KAAA,aAAkB,MAAA,CAAO,MAAA;EAAA,SAChD,WAAA,GAAc,KAAA,aAAkB,MAAA,CAAO,MAAA;AAAA;AAAA,KAGtC,uBAAA;EAAA,SACD,IAAA,EAAM,MAAA,CAAO,MAAA,CAAO,uBAAA;EAAA,SACpB,IAAA,GAAO,QAAA,EAAU,uBAAA,KAA4B,MAAA,CAAO,MAAA;AAAA;AAAA,KAGnD,uBAAA;EAAA,SACD,OAAA,EAAS,aAAA;EAAA,SACT,OAAA,EAAS,aAAa;AAAA;AAAA,cAChC,aAAA;cAsBY,QAAA,SAAiB,aAAA;EArCnB;;;;EAAA,OA4CF,aAAA,GAAiB,OAAA,EAAS,uBAAA,KAA0B,KAAA,CAAM,KAAA,CAAM,QAAA;EA3ClD;;;;EAAA,OA4Gd,aAAA,QAAoB,KAAA,CAAM,KAAA,CAAM,QAAA;AAAA;;cA0C5B,yBAAA,GACX,OAAA,EAAS,uBAAA,KACR,KAAA,CAAM,KAAA,CAAM,QAAA;;cAGF,yBAAA,QAAgC,KAAA,CAAM,KAAK,CAAC,QAAA"}