@effect-uai/core 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 (110) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +43 -0
  3. package/dist/AiError-CqmYjXyx.d.mts +110 -0
  4. package/dist/AiError-CqmYjXyx.d.mts.map +1 -0
  5. package/dist/Items-D1C2686t.d.mts +372 -0
  6. package/dist/Items-D1C2686t.d.mts.map +1 -0
  7. package/dist/Loop-CzSJo1h8.d.mts +87 -0
  8. package/dist/Loop-CzSJo1h8.d.mts.map +1 -0
  9. package/dist/Outcome-C2JYknCu.d.mts +40 -0
  10. package/dist/Outcome-C2JYknCu.d.mts.map +1 -0
  11. package/dist/StructuredFormat-B5ueioNr.d.mts +88 -0
  12. package/dist/StructuredFormat-B5ueioNr.d.mts.map +1 -0
  13. package/dist/Tool-5wxOCuOh.d.mts +86 -0
  14. package/dist/Tool-5wxOCuOh.d.mts.map +1 -0
  15. package/dist/ToolEvent-B2N10hr3.d.mts +29 -0
  16. package/dist/ToolEvent-B2N10hr3.d.mts.map +1 -0
  17. package/dist/Turn-rlTfuHaQ.d.mts +211 -0
  18. package/dist/Turn-rlTfuHaQ.d.mts.map +1 -0
  19. package/dist/chunk-CfYAbeIz.mjs +13 -0
  20. package/dist/domain/AiError.d.mts +2 -0
  21. package/dist/domain/AiError.mjs +40 -0
  22. package/dist/domain/AiError.mjs.map +1 -0
  23. package/dist/domain/Items.d.mts +2 -0
  24. package/dist/domain/Items.mjs +238 -0
  25. package/dist/domain/Items.mjs.map +1 -0
  26. package/dist/domain/Turn.d.mts +2 -0
  27. package/dist/domain/Turn.mjs +82 -0
  28. package/dist/domain/Turn.mjs.map +1 -0
  29. package/dist/index.d.mts +14 -0
  30. package/dist/index.mjs +14 -0
  31. package/dist/language-model/LanguageModel.d.mts +60 -0
  32. package/dist/language-model/LanguageModel.d.mts.map +1 -0
  33. package/dist/language-model/LanguageModel.mjs +33 -0
  34. package/dist/language-model/LanguageModel.mjs.map +1 -0
  35. package/dist/loop/Loop.d.mts +2 -0
  36. package/dist/loop/Loop.mjs +172 -0
  37. package/dist/loop/Loop.mjs.map +1 -0
  38. package/dist/match/Match.d.mts +16 -0
  39. package/dist/match/Match.d.mts.map +1 -0
  40. package/dist/match/Match.mjs +15 -0
  41. package/dist/match/Match.mjs.map +1 -0
  42. package/dist/observability/Metrics.d.mts +45 -0
  43. package/dist/observability/Metrics.d.mts.map +1 -0
  44. package/dist/observability/Metrics.mjs +52 -0
  45. package/dist/observability/Metrics.mjs.map +1 -0
  46. package/dist/streaming/JSONL.d.mts +34 -0
  47. package/dist/streaming/JSONL.d.mts.map +1 -0
  48. package/dist/streaming/JSONL.mjs +51 -0
  49. package/dist/streaming/JSONL.mjs.map +1 -0
  50. package/dist/streaming/Lines.d.mts +27 -0
  51. package/dist/streaming/Lines.d.mts.map +1 -0
  52. package/dist/streaming/Lines.mjs +32 -0
  53. package/dist/streaming/Lines.mjs.map +1 -0
  54. package/dist/streaming/SSE.d.mts +31 -0
  55. package/dist/streaming/SSE.d.mts.map +1 -0
  56. package/dist/streaming/SSE.mjs +58 -0
  57. package/dist/streaming/SSE.mjs.map +1 -0
  58. package/dist/structured-format/StructuredFormat.d.mts +2 -0
  59. package/dist/structured-format/StructuredFormat.mjs +68 -0
  60. package/dist/structured-format/StructuredFormat.mjs.map +1 -0
  61. package/dist/testing/MockProvider.d.mts +48 -0
  62. package/dist/testing/MockProvider.d.mts.map +1 -0
  63. package/dist/testing/MockProvider.mjs +95 -0
  64. package/dist/testing/MockProvider.mjs.map +1 -0
  65. package/dist/tool/HistoryCheck.d.mts +24 -0
  66. package/dist/tool/HistoryCheck.d.mts.map +1 -0
  67. package/dist/tool/HistoryCheck.mjs +39 -0
  68. package/dist/tool/HistoryCheck.mjs.map +1 -0
  69. package/dist/tool/Outcome.d.mts +2 -0
  70. package/dist/tool/Outcome.mjs +45 -0
  71. package/dist/tool/Outcome.mjs.map +1 -0
  72. package/dist/tool/Resolvers.d.mts +44 -0
  73. package/dist/tool/Resolvers.d.mts.map +1 -0
  74. package/dist/tool/Resolvers.mjs +67 -0
  75. package/dist/tool/Resolvers.mjs.map +1 -0
  76. package/dist/tool/Tool.d.mts +2 -0
  77. package/dist/tool/Tool.mjs +79 -0
  78. package/dist/tool/Tool.mjs.map +1 -0
  79. package/dist/tool/ToolEvent.d.mts +2 -0
  80. package/dist/tool/ToolEvent.mjs +8 -0
  81. package/dist/tool/ToolEvent.mjs.map +1 -0
  82. package/dist/tool/Toolkit.d.mts +34 -0
  83. package/dist/tool/Toolkit.d.mts.map +1 -0
  84. package/dist/tool/Toolkit.mjs +105 -0
  85. package/dist/tool/Toolkit.mjs.map +1 -0
  86. package/package.json +127 -0
  87. package/src/domain/AiError.ts +93 -0
  88. package/src/domain/Items.ts +260 -0
  89. package/src/domain/Turn.ts +174 -0
  90. package/src/index.ts +13 -0
  91. package/src/language-model/LanguageModel.ts +73 -0
  92. package/src/loop/Loop.test.ts +412 -0
  93. package/src/loop/Loop.ts +295 -0
  94. package/src/match/Match.ts +9 -0
  95. package/src/observability/Metrics.ts +87 -0
  96. package/src/streaming/JSONL.test.ts +85 -0
  97. package/src/streaming/JSONL.ts +96 -0
  98. package/src/streaming/Lines.ts +34 -0
  99. package/src/streaming/SSE.test.ts +72 -0
  100. package/src/streaming/SSE.ts +114 -0
  101. package/src/structured-format/StructuredFormat.ts +160 -0
  102. package/src/testing/MockProvider.ts +161 -0
  103. package/src/tool/HistoryCheck.ts +49 -0
  104. package/src/tool/Outcome.ts +101 -0
  105. package/src/tool/Resolvers.test.ts +426 -0
  106. package/src/tool/Resolvers.ts +166 -0
  107. package/src/tool/Tool.ts +150 -0
  108. package/src/tool/ToolEvent.ts +37 -0
  109. package/src/tool/Toolkit.test.ts +45 -0
  110. package/src/tool/Toolkit.ts +228 -0
@@ -0,0 +1,412 @@
1
+ import { Deferred, Effect, Fiber, Ref, Stream } from "effect"
2
+ import { describe, expect, it } from "vitest"
3
+ import { type Event, loop, next, nextAfter, stopEvent, stopAfter, value } from "./Loop.js"
4
+
5
+ describe("Loop.loop", () => {
6
+ it("threads state across iterations and emits each iteration's substream in order", async () => {
7
+ // Each iter emits [n, n + 0.5] then continues; final iter emits [n] and stops.
8
+ const stream = loop(0, (n: number) =>
9
+ n >= 3
10
+ ? stopAfter(Stream.fromIterable([n]))
11
+ : nextAfter(Stream.fromIterable([n, n + 0.5]), n + 1),
12
+ )
13
+
14
+ const result = await Effect.runPromise(Stream.runCollect(stream))
15
+ expect(result).toEqual([0, 0.5, 1, 1.5, 2, 2.5, 3])
16
+ })
17
+
18
+ it("supports iterations that emit zero values and only decide", async () => {
19
+ // Every iteration emits nothing, just bumps state; stops at 5.
20
+ const stream = loop(0, (n: number) =>
21
+ n >= 5 ? Stream.fromIterable([stopEvent]) : Stream.fromIterable([next(n + 1)]),
22
+ )
23
+
24
+ const result = await Effect.runPromise(Stream.runCollect(stream))
25
+ expect(result).toEqual([])
26
+ })
27
+
28
+ it("supports Effect-returning bodies directly (no Stream.unwrap needed)", async () => {
29
+ // Each iter yields an Effect that doubles the state, then emits it.
30
+ // Body returns Effect<Stream> directly; loop unwraps internally.
31
+ const stream = loop(1, (n: number) =>
32
+ Effect.gen(function* () {
33
+ const doubled = yield* Effect.succeed(n * 2)
34
+ return doubled >= 16
35
+ ? stopAfter(Stream.fromIterable([doubled]))
36
+ : nextAfter(Stream.fromIterable([doubled]), doubled)
37
+ }),
38
+ )
39
+
40
+ const result = await Effect.runPromise(Stream.runCollect(stream))
41
+ expect(result).toEqual([2, 4, 8, 16])
42
+ })
43
+
44
+ it("still accepts Stream.unwrap-wrapped bodies for backward compatibility", async () => {
45
+ const stream = loop(1, (n: number) =>
46
+ Stream.unwrap(
47
+ Effect.gen(function* () {
48
+ const doubled = yield* Effect.succeed(n * 2)
49
+ return doubled >= 4
50
+ ? stopAfter(Stream.fromIterable([doubled]))
51
+ : nextAfter(Stream.fromIterable([doubled]), doubled)
52
+ }),
53
+ ),
54
+ )
55
+
56
+ const result = await Effect.runPromise(Stream.runCollect(stream))
57
+ expect(result).toEqual([2, 4])
58
+ })
59
+
60
+ it("propagates errors from the body's stream", async () => {
61
+ const boom = new Error("boom")
62
+ const stream = loop(
63
+ 0,
64
+ (n: number): Stream.Stream<Event<number, number>, Error> =>
65
+ n === 2 ? Stream.fail(boom) : Stream.fromIterable([value(n), next(n + 1)]),
66
+ )
67
+
68
+ const result = await Effect.runPromiseExit(Stream.runCollect(stream))
69
+ expect(result._tag).toBe("Failure")
70
+ })
71
+
72
+ it("terminates silently if the body emits no Decision (mirrors paginate's silent stop)", async () => {
73
+ // No decision emitted - loop just ends after the body's stream completes.
74
+ const stream = loop(0, (n: number) => Stream.fromIterable([value(n), value(n + 1)]))
75
+
76
+ const result = await Effect.runPromise(Stream.runCollect(stream))
77
+ expect(result).toEqual([0, 1])
78
+ })
79
+
80
+ it("short-circuits the body's stream when a Decision is seen", async () => {
81
+ // Body emits [n, next(n+1), n+10]. Once the Decision is encountered, the
82
+ // body's stream is interrupted - `n+10` is never pulled, so it never
83
+ // flows to the outer stream. This is the correct behavior: a Decision
84
+ // marks "I'm done with this iteration"; anything after it is dead code.
85
+ const stream = loop(0, (n: number) =>
86
+ n >= 2
87
+ ? Stream.fromIterable([value(n), stopEvent])
88
+ : Stream.fromIterable([value(n), next(n + 1), value(n + 10)]),
89
+ )
90
+
91
+ const result = await Effect.runPromise(Stream.runCollect(stream))
92
+ expect(result).toEqual([0, 1, 2])
93
+ })
94
+
95
+ it("is stack-safe and linear-time across many iterations", async () => {
96
+ // 100k iterations far exceeds V8's typical stack depth (~10–15k frames).
97
+ const N = 100_000
98
+ const stream = loop(0, (n: number) =>
99
+ n >= N
100
+ ? Stream.fromIterable([value(n), stopEvent])
101
+ : Stream.fromIterable([value(n), next(n + 1)]),
102
+ )
103
+
104
+ const count = await Effect.runPromise(
105
+ Stream.runFold(
106
+ stream,
107
+ (): number => 0,
108
+ (acc) => acc + 1,
109
+ ),
110
+ )
111
+ expect(count).toBe(N + 1) // 0..N inclusive
112
+ }, 10_000)
113
+ })
114
+
115
+ // ---------------------------------------------------------------------------
116
+ // Mock LLM scenario - proves the loop forwards deltas in real time and
117
+ // correctly threads tool-result events between turns.
118
+ // ---------------------------------------------------------------------------
119
+
120
+ type Delta =
121
+ | { readonly type: "text"; readonly text: string }
122
+ | { readonly type: "tool_call"; readonly id: string; readonly name: string }
123
+
124
+ type HistoryItem =
125
+ | { readonly type: "user"; readonly text: string }
126
+ | { readonly type: "assistant"; readonly text: string }
127
+ | { readonly type: "tool_call"; readonly id: string; readonly name: string }
128
+ | { readonly type: "tool_result"; readonly id: string; readonly output: string }
129
+
130
+ type UiEvent =
131
+ | { readonly type: "text"; readonly text: string }
132
+ | { readonly type: "tool_started"; readonly id: string; readonly name: string }
133
+ | { readonly type: "tool_result"; readonly id: string; readonly output: string }
134
+
135
+ interface MockModel {
136
+ readonly streamTurn: (history: ReadonlyArray<HistoryItem>) => Stream.Stream<Delta>
137
+ }
138
+
139
+ interface State {
140
+ readonly history: ReadonlyArray<HistoryItem>
141
+ readonly model: MockModel
142
+ }
143
+
144
+ interface ToolOutcome {
145
+ readonly output: string
146
+ readonly nextModel?: MockModel
147
+ }
148
+
149
+ type ToolRunner = (call: { id: string; name: string }) => ToolOutcome
150
+
151
+ const scriptedModel = (script: ReadonlyArray<ReadonlyArray<Delta>>): MockModel => {
152
+ let i = 0
153
+ return {
154
+ streamTurn: () => {
155
+ const turn = script[i] ?? []
156
+ i += 1
157
+ return Stream.fromIterable(turn)
158
+ },
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Body factored out so both tests share it. Per iteration:
164
+ * 1. Stream the model's deltas; tap captures texts + tool calls into Refs.
165
+ * 2. flatMap projects deltas into UiEvents forwarded to the outer stream.
166
+ * 3. Continuation reads the captured calls; if any, runs them, emits
167
+ * tool_result events, builds the next state (with model swap if a tool
168
+ * asked for one), and emits `next(state)`. Otherwise `stop`.
169
+ */
170
+ const conversationLoop = (initial: State, runTool: ToolRunner) =>
171
+ loop(initial, (state) =>
172
+ Stream.unwrap(
173
+ Effect.gen(function* () {
174
+ const textsRef = yield* Ref.make<ReadonlyArray<string>>([])
175
+ const toolCallsRef = yield* Ref.make<
176
+ ReadonlyArray<{ readonly id: string; readonly name: string }>
177
+ >([])
178
+
179
+ const deltas: Stream.Stream<Event<UiEvent, State>> = state.model
180
+ .streamTurn(state.history)
181
+ .pipe(
182
+ Stream.tap((d) =>
183
+ d.type === "text"
184
+ ? Ref.update(textsRef, (t) => [...t, d.text])
185
+ : Ref.update(toolCallsRef, (t) => [...t, { id: d.id, name: d.name }]),
186
+ ),
187
+ Stream.flatMap(
188
+ (d): Stream.Stream<Event<UiEvent, State>> =>
189
+ d.type === "text"
190
+ ? Stream.fromIterable([value<UiEvent>({ type: "text", text: d.text })])
191
+ : Stream.fromIterable([
192
+ value<UiEvent>({ type: "tool_started", id: d.id, name: d.name }),
193
+ ]),
194
+ ),
195
+ )
196
+
197
+ const continuation: Stream.Stream<Event<UiEvent, State>> = Stream.unwrap(
198
+ Effect.gen(function* () {
199
+ const texts = yield* Ref.get(textsRef)
200
+ const toolCalls = yield* Ref.get(toolCallsRef)
201
+
202
+ if (toolCalls.length === 0) {
203
+ return stopAfter(Stream.empty)
204
+ }
205
+
206
+ const turnItems: ReadonlyArray<HistoryItem> = [
207
+ ...(texts.length > 0 ? [{ type: "assistant" as const, text: texts.join("") }] : []),
208
+ ...toolCalls.map(
209
+ (tc): HistoryItem => ({ type: "tool_call", id: tc.id, name: tc.name }),
210
+ ),
211
+ ]
212
+
213
+ const outcomes = toolCalls.map((call) => ({ call, outcome: runTool(call) }))
214
+
215
+ const events: ReadonlyArray<UiEvent> = outcomes.map(({ call, outcome }) => ({
216
+ type: "tool_result",
217
+ id: call.id,
218
+ output: outcome.output,
219
+ }))
220
+
221
+ const resultItems: ReadonlyArray<HistoryItem> = outcomes.map(
222
+ ({ call, outcome }): HistoryItem => ({
223
+ type: "tool_result",
224
+ id: call.id,
225
+ output: outcome.output,
226
+ }),
227
+ )
228
+
229
+ // Last requested model wins; default to the current one.
230
+ const nextModel = outcomes.reduce(
231
+ (m, { outcome }) => outcome.nextModel ?? m,
232
+ state.model,
233
+ )
234
+
235
+ const nextState: State = {
236
+ history: [...state.history, ...turnItems, ...resultItems],
237
+ model: nextModel,
238
+ }
239
+
240
+ return nextAfter(Stream.fromIterable(events), nextState)
241
+ }),
242
+ )
243
+
244
+ return Stream.concat(deltas, continuation)
245
+ }),
246
+ ),
247
+ )
248
+
249
+ describe("Loop.loop - LLM-style scenarios", () => {
250
+ it("forwards text deltas, tool start, tool result, and post-tool text in order", async () => {
251
+ const m = scriptedModel([
252
+ [
253
+ { type: "text", text: "hello" },
254
+ { type: "text", text: " " },
255
+ { type: "text", text: "world" },
256
+ { type: "tool_call", id: "c1", name: "get_time" },
257
+ ],
258
+ [
259
+ { type: "text", text: " time is " },
260
+ { type: "text", text: "12:00" },
261
+ ],
262
+ ])
263
+
264
+ const runTool: ToolRunner = (call) => ({
265
+ output: call.name === "get_time" ? "12:00" : "?",
266
+ })
267
+
268
+ const initial: State = {
269
+ history: [{ type: "user", text: "what time is it?" }],
270
+ model: m,
271
+ }
272
+
273
+ const events = await Effect.runPromise(Stream.runCollect(conversationLoop(initial, runTool)))
274
+
275
+ expect(events).toEqual([
276
+ { type: "text", text: "hello" },
277
+ { type: "text", text: " " },
278
+ { type: "text", text: "world" },
279
+ { type: "tool_started", id: "c1", name: "get_time" },
280
+ { type: "tool_result", id: "c1", output: "12:00" },
281
+ { type: "text", text: " time is " },
282
+ { type: "text", text: "12:00" },
283
+ ])
284
+ })
285
+
286
+ it("model swap mid-stream: m1 calls upgrade, m2 finishes the response", async () => {
287
+ const m2 = scriptedModel([
288
+ [
289
+ { type: "text", text: "I am m2." },
290
+ { type: "text", text: " The answer is 42." },
291
+ ],
292
+ ])
293
+
294
+ const m1 = scriptedModel([
295
+ [
296
+ { type: "text", text: "Hard question." },
297
+ { type: "text", text: " Upgrading." },
298
+ { type: "tool_call", id: "u1", name: "upgrade" },
299
+ ],
300
+ ])
301
+
302
+ const runTool: ToolRunner = (call) =>
303
+ call.name === "upgrade" ? { output: "ok", nextModel: m2 } : { output: "?" }
304
+
305
+ const initial: State = {
306
+ history: [{ type: "user", text: "what is the meaning of life?" }],
307
+ model: m1,
308
+ }
309
+
310
+ const events = await Effect.runPromise(Stream.runCollect(conversationLoop(initial, runTool)))
311
+
312
+ expect(events).toEqual([
313
+ { type: "text", text: "Hard question." },
314
+ { type: "text", text: " Upgrading." },
315
+ { type: "tool_started", id: "u1", name: "upgrade" },
316
+ { type: "tool_result", id: "u1", output: "ok" },
317
+ { type: "text", text: "I am m2." },
318
+ { type: "text", text: " The answer is 42." },
319
+ ])
320
+ })
321
+ })
322
+
323
+ describe("Loop.loop - pull-specific stream semantics", () => {
324
+ it("does not start the next iteration when downstream only takes the first value", async () => {
325
+ const bodyCalls = await Effect.runPromise(
326
+ Effect.gen(function* () {
327
+ const callsRef = yield* Ref.make(0)
328
+ const stream = loop(0, (n: number) =>
329
+ Stream.unwrap(
330
+ Ref.update(callsRef, (calls) => calls + 1).pipe(
331
+ Effect.as(
332
+ n >= 10
333
+ ? Stream.fromIterable([value(n), stopEvent])
334
+ : Stream.fromIterable([value(n), next(n + 1)]),
335
+ ),
336
+ ),
337
+ ),
338
+ )
339
+
340
+ yield* stream.pipe(Stream.take(1), Stream.runCollect)
341
+ return yield* Ref.get(callsRef)
342
+ }),
343
+ )
344
+
345
+ expect(bodyCalls).toBe(1)
346
+ })
347
+
348
+ it("propagates defects from the body instead of leaving the consumer waiting", async () => {
349
+ const defect = new Error("defect")
350
+ const stream = loop(0, () => Stream.die(defect))
351
+
352
+ const result = await Effect.runPromiseExit(Stream.runCollect(stream))
353
+
354
+ expect(result._tag).toBe("Failure")
355
+ })
356
+
357
+ it("runs body finalizers when a Decision short-circuits the body", async () => {
358
+ const releases = await Effect.runPromise(
359
+ Effect.gen(function* () {
360
+ const releasesRef = yield* Ref.make<ReadonlyArray<number>>([])
361
+ const stream = loop(0, (n: number) =>
362
+ (n >= 1
363
+ ? Stream.fromIterable([value(n), stopEvent])
364
+ : Stream.fromIterable([value(n), next(n + 1), value(n + 10)])
365
+ ).pipe(Stream.ensuring(Ref.update(releasesRef, (values) => [...values, n]))),
366
+ )
367
+
368
+ const values = yield* Stream.runCollect(stream)
369
+ expect(values).toEqual([0, 1])
370
+ return yield* Ref.get(releasesRef)
371
+ }),
372
+ )
373
+
374
+ expect(releases).toEqual([0, 1])
375
+ })
376
+
377
+ it("runs the active body finalizer when the downstream consumer is interrupted", async () => {
378
+ const releases = await Effect.runPromise(
379
+ Effect.gen(function* () {
380
+ const started = yield* Deferred.make<void>()
381
+ const releasesRef = yield* Ref.make(0)
382
+ const body = (): Stream.Stream<Event<number, never>> =>
383
+ Stream.concat(
384
+ Stream.fromEffect(Deferred.succeed(started, undefined).pipe(Effect.as(value(0)))),
385
+ Stream.never,
386
+ ).pipe(Stream.ensuring(Ref.update(releasesRef, (n) => n + 1)))
387
+ const stream = loop(0, body)
388
+
389
+ const fiber = yield* Effect.forkChild(Stream.runCollect(stream))
390
+ yield* Deferred.await(started)
391
+ yield* Fiber.interrupt(fiber)
392
+
393
+ return yield* Ref.get(releasesRef)
394
+ }),
395
+ )
396
+
397
+ expect(releases).toBe(1)
398
+ })
399
+
400
+ it("does not create a body scope if constructing the body stream defects", async () => {
401
+ const defect = new Error("body construction failed")
402
+ const result = await Effect.runPromiseExit(
403
+ Stream.runCollect(
404
+ loop(0, (): Stream.Stream<Event<number, never>> => {
405
+ throw defect
406
+ }),
407
+ ),
408
+ )
409
+
410
+ expect(result._tag).toBe("Failure")
411
+ })
412
+ })