@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 @@
1
+ {"version":3,"file":"Tool.mjs","names":[],"sources":["../../src/tool/Tool.ts"],"sourcesContent":["import type { StandardJSONSchemaV1, StandardSchemaV1 } from \"@standard-schema/spec\"\nimport { Effect, Schema, Stream } from \"effect\"\nimport type { FunctionCall, FunctionCallOutput } from \"../domain/Items.js\"\nimport { functionCallOutput } from \"../domain/Items.js\"\n\nexport class ToolError extends Schema.TaggedErrorClass<ToolError>(\"@betalyra/effect-uai/ToolError\")(\n \"ToolError\",\n {\n call_id: Schema.String,\n tool: Schema.String,\n message: Schema.String,\n cause: Schema.optional(Schema.Unknown),\n },\n) {}\n\n/**\n * Schemas accepted on `Tool.inputSchema`. Must implement both Standard\n * Schema validation and JSON Schema conversion (for rendering tool\n * descriptors to provider request bodies).\n *\n * Any Standard-Schema-compliant library that exposes both interfaces\n * works directly: Zod 4+, Valibot, ArkType, Effect Schema (after\n * `fromEffectSchema`), etc.\n */\nexport type ToolInputSchema<Input = unknown> = StandardSchemaV1<unknown, Input> &\n StandardJSONSchemaV1<unknown, Input>\n\n/**\n * Convenience wrapper for Effect Schema users - adds both the\n * `validate` and `jsonSchema` extensions to a plain Effect Schema so it\n * can be used as a `Tool.inputSchema`.\n */\nexport const fromEffectSchema = <S extends Schema.Codec<any, any, never, any>>(\n schema: S,\n): S & ToolInputSchema<S[\"Type\"]> =>\n Schema.toStandardJSONSchemaV1(Schema.toStandardSchemaV1(schema)) as unknown as S &\n ToolInputSchema<S[\"Type\"]>\n\nexport interface Tool<Name extends string, Input, Output, R = never> {\n readonly name: Name\n readonly description: string\n readonly inputSchema: ToolInputSchema<Input>\n readonly run: (input: Input) => Effect.Effect<Output, unknown, R>\n /**\n * Whether the provider should render this tool with its strict-mode\n * flag (OpenAI's `strict: true`, etc). Default: true. The framework\n * never rewrites the schema; if the rendered JSON Schema isn't\n * compatible, the provider returns an error.\n */\n readonly strict?: boolean\n}\n\n/**\n * Provider-agnostic tool descriptor. Each provider maps `inputSchema`\n * to its own wire field (OpenAI → `parameters`, Anthropic →\n * `input_schema`). Built from a `Tool` by `Toolkit.toDescriptors`.\n */\nexport interface ToolDescriptor {\n readonly name: string\n readonly description: string\n readonly inputSchema: Record<string, unknown>\n readonly strict?: boolean\n}\n\nexport const make = <Name extends string, Input, Output, R = never>(\n spec: Tool<Name, Input, Output, R>,\n): Tool<Name, Input, Output, R> => spec\n\n// ---------------------------------------------------------------------------\n// Streaming tools\n//\n// `run` returns a `Stream<Event>` instead of an `Effect<Output>`. Events\n// flow through to the consumer as `ToolEvent.Intermediate`s in real time;\n// at end-of-stream `finalize(events)` reduces them to the model-facing\n// `Output`. Sub-agents, slow downloads with progress, recipe streamers.\n// ---------------------------------------------------------------------------\n\nexport interface StreamingTool<Name extends string, Input, Event, Output, R = never> {\n readonly _kind: \"streaming\"\n readonly name: Name\n readonly description: string\n readonly inputSchema: ToolInputSchema<Input>\n readonly run: (input: Input) => Stream.Stream<Event, unknown, R>\n readonly finalize: (events: ReadonlyArray<Event>) => Output\n readonly strict?: boolean\n}\n\nexport const streaming = <Name extends string, Input, Event, Output, R = never>(\n spec: Omit<StreamingTool<Name, Input, Event, Output, R>, \"_kind\">,\n): StreamingTool<Name, Input, Event, Output, R> => ({ _kind: \"streaming\", ...spec })\n\nexport type AnyStreamingTool = StreamingTool<string, any, any, any, never>\nexport type AnyPlainTool = Tool<string, any, any, never>\nexport type AnyKindTool = AnyStreamingTool | AnyPlainTool\n\nexport const isStreamingTool = (t: AnyKindTool): t is AnyStreamingTool =>\n \"_kind\" in t && t._kind === \"streaming\"\n\n/**\n * Render any-kind tools (mixed plain and streaming) to provider-agnostic\n * descriptors. Mirrors `Toolkit.toDescriptors` but accepts the union type\n * so a single list can carry both kinds.\n */\nexport const toDescriptors = (\n tools: ReadonlyArray<AnyKindTool>,\n): ReadonlyArray<ToolDescriptor> =>\n tools.map((tool) => {\n const inputSchema = tool.inputSchema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\",\n })\n return tool.strict !== undefined\n ? { name: tool.name, description: tool.description, inputSchema, strict: tool.strict }\n : { name: tool.name, description: tool.description, inputSchema }\n })\n\nconst toToolError = (call: FunctionCall, toolName: string, message: string) => (cause: unknown) =>\n new ToolError({ call_id: call.call_id, tool: toolName, message, cause })\n\n/**\n * Decode and validate the JSON arguments of a function_call against the\n * tool's input schema, run the tool, and serialize the output into a\n * function_call_output item.\n */\nexport const execute = <Name extends string, Input, Output, R>(\n tool: Tool<Name, Input, Output, R>,\n call: FunctionCall,\n): Effect.Effect<FunctionCallOutput, ToolError, R> =>\n Effect.gen(function* () {\n const parsed = yield* Effect.try({\n try: () => JSON.parse(call.arguments) as unknown,\n catch: toToolError(call, tool.name, \"Failed to parse JSON arguments\"),\n })\n\n const result = yield* Effect.promise(() =>\n Promise.resolve(tool.inputSchema[\"~standard\"].validate(parsed)),\n )\n if (result.issues !== undefined) {\n return yield* new ToolError({\n call_id: call.call_id,\n tool: tool.name,\n message: \"Tool input failed schema validation\",\n cause: result.issues,\n })\n }\n\n const output = yield* tool\n .run(result.value)\n .pipe(Effect.mapError(toToolError(call, tool.name, \"Tool execution failed\")))\n return functionCallOutput(call.call_id, JSON.stringify(output))\n })\n"],"mappings":";;;;;;;;;;;;;AAKA,IAAa,YAAb,cAA+B,OAAO,iBAA4B,iCAAiC,CACjG,aACA;CACE,SAAS,OAAO;CAChB,MAAM,OAAO;CACb,SAAS,OAAO;CAChB,OAAO,OAAO,SAAS,OAAO,QAAQ;CACvC,CACF,CAAC;;;;;;AAmBF,MAAa,oBACX,WAEA,OAAO,uBAAuB,OAAO,mBAAmB,OAAO,CAAC;AA6BlE,MAAa,QACX,SACiC;AAqBnC,MAAa,aACX,UACkD;CAAE,OAAO;CAAa,GAAG;CAAM;AAMnF,MAAa,mBAAmB,MAC9B,WAAW,KAAK,EAAE,UAAU;;;;;;AAO9B,MAAa,iBACX,UAEA,MAAM,KAAK,SAAS;CAClB,MAAM,cAAc,KAAK,YAAY,aAAa,WAAW,MAAM,EACjE,QAAQ,iBACT,CAAC;AACF,QAAO,KAAK,WAAW,KAAA,IACnB;EAAE,MAAM,KAAK;EAAM,aAAa,KAAK;EAAa;EAAa,QAAQ,KAAK;EAAQ,GACpF;EAAE,MAAM,KAAK;EAAM,aAAa,KAAK;EAAa;EAAa;EACnE;AAEJ,MAAM,eAAe,MAAoB,UAAkB,aAAqB,UAC9E,IAAI,UAAU;CAAE,SAAS,KAAK;CAAS,MAAM;CAAU;CAAS;CAAO,CAAC;;;;;;AAO1E,MAAa,WACX,MACA,SAEA,OAAO,IAAI,aAAa;CACtB,MAAM,SAAS,OAAO,OAAO,IAAI;EAC/B,WAAW,KAAK,MAAM,KAAK,UAAU;EACrC,OAAO,YAAY,MAAM,KAAK,MAAM,iCAAiC;EACtE,CAAC;CAEF,MAAM,SAAS,OAAO,OAAO,cAC3B,QAAQ,QAAQ,KAAK,YAAY,aAAa,SAAS,OAAO,CAAC,CAChE;AACD,KAAI,OAAO,WAAW,KAAA,EACpB,QAAO,OAAO,IAAI,UAAU;EAC1B,SAAS,KAAK;EACd,MAAM,KAAK;EACX,SAAS;EACT,OAAO,OAAO;EACf,CAAC;CAGJ,MAAM,SAAS,OAAO,KACnB,IAAI,OAAO,MAAM,CACjB,KAAK,OAAO,SAAS,YAAY,MAAM,KAAK,MAAM,wBAAwB,CAAC,CAAC;AAC/E,QAAO,mBAAmB,KAAK,SAAS,KAAK,UAAU,OAAO,CAAC;EAC/D"}
@@ -0,0 +1,2 @@
1
+ import { i as isOutput, n as isApprovalRequested, r as isIntermediate, t as ToolEvent } from "../ToolEvent-B2N10hr3.mjs";
2
+ export { ToolEvent, isApprovalRequested, isIntermediate, isOutput };
@@ -0,0 +1,8 @@
1
+ //#region src/tool/ToolEvent.ts
2
+ const isApprovalRequested = (e) => e._tag === "ApprovalRequested";
3
+ const isIntermediate = (e) => e._tag === "Intermediate";
4
+ const isOutput = (e) => e._tag === "Output";
5
+ //#endregion
6
+ export { isApprovalRequested, isIntermediate, isOutput };
7
+
8
+ //# sourceMappingURL=ToolEvent.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ToolEvent.mjs","names":[],"sources":["../../src/tool/ToolEvent.ts"],"sourcesContent":["/**\n * The event type emitted by `Toolkit.executeAllWithResolver`.\n *\n * - ApprovalRequested : gated calls before resolver returns\n * - Intermediate : per-element passthrough from a streaming tool's run\n * - Output : terminal result (carries a structured ToolResult)\n *\n * Recipes thread `ToolEvent.Output.result` through `nextStateFrom` and apply\n * `toFunctionCallOutput` when appending to history.\n */\nimport type { ToolResult } from \"./Outcome.js\"\n\nexport type ToolEvent =\n | {\n readonly _tag: \"ApprovalRequested\"\n readonly call_id: string\n readonly tool: string\n readonly arguments: string\n }\n | {\n readonly _tag: \"Intermediate\"\n readonly call_id: string\n readonly tool: string\n readonly data: unknown\n }\n | { readonly _tag: \"Output\"; readonly result: ToolResult }\n\nexport const isApprovalRequested = (\n e: ToolEvent,\n): e is Extract<ToolEvent, { _tag: \"ApprovalRequested\" }> => e._tag === \"ApprovalRequested\"\n\nexport const isIntermediate = (\n e: ToolEvent,\n): e is Extract<ToolEvent, { _tag: \"Intermediate\" }> => e._tag === \"Intermediate\"\n\nexport const isOutput = (e: ToolEvent): e is Extract<ToolEvent, { _tag: \"Output\" }> =>\n e._tag === \"Output\"\n"],"mappings":";AA2BA,MAAa,uBACX,MAC2D,EAAE,SAAS;AAExE,MAAa,kBACX,MACsD,EAAE,SAAS;AAEnE,MAAa,YAAY,MACvB,EAAE,SAAS"}
@@ -0,0 +1,34 @@
1
+ import { o as FunctionCall } from "../Items-D1C2686t.mjs";
2
+ import { a as Tool, o as ToolDescriptor, t as AnyKindTool } from "../Tool-5wxOCuOh.mjs";
3
+ import { t as Event } from "../Loop-CzSJo1h8.mjs";
4
+ import { n as ToolResult, t as ToolDecision } from "../Outcome-C2JYknCu.mjs";
5
+ import { t as ToolEvent } from "../ToolEvent-B2N10hr3.mjs";
6
+ import { Effect, Stream } from "effect";
7
+
8
+ //#region src/tool/Toolkit.d.ts
9
+ declare namespace Toolkit_d_exports {
10
+ export { AnyTool, ExecuteOptions, Resolver, Toolkit, ToolsR, executeAll, executeAllWithResolver, make, nextStateFrom, toDescriptors };
11
+ }
12
+ type AnyTool = Tool<string, any, any, any>;
13
+ type Toolkit<Tools extends ReadonlyArray<AnyTool>> = {
14
+ readonly tools: Tools;
15
+ };
16
+ type ToolsR<Tools extends ReadonlyArray<AnyTool>> = Tools[number] extends Tool<any, any, any, infer R> ? R : never;
17
+ declare const make: <const Tools extends ReadonlyArray<AnyTool>>(tools: Tools) => Toolkit<Tools>;
18
+ /**
19
+ * Render every tool in a toolkit to a provider-agnostic descriptor.
20
+ * `inputSchema` is the JSON Schema document produced by the tool's
21
+ * Standard Schema converter (draft 2020-12).
22
+ */
23
+ declare const toDescriptors: <Tools extends ReadonlyArray<AnyTool>>(toolkit: Toolkit<Tools>) => ReadonlyArray<ToolDescriptor>;
24
+ type Resolver = (call: FunctionCall) => Effect.Effect<ToolDecision>;
25
+ interface ExecuteOptions {
26
+ readonly concurrency?: number | "unbounded";
27
+ }
28
+ declare const executeAllWithResolver: (tools: ReadonlyArray<AnyKindTool>, calls: ReadonlyArray<FunctionCall>, resolve: Resolver, options?: ExecuteOptions) => Stream.Stream<ToolEvent>;
29
+ /** No-resolver shortcut: every call gets `Execute`. */
30
+ declare const executeAll: (tools: ReadonlyArray<AnyKindTool>, calls: ReadonlyArray<FunctionCall>, options?: ExecuteOptions) => Stream.Stream<ToolEvent>;
31
+ declare const nextStateFrom: <S>(stream: Stream.Stream<ToolEvent>, build: (results: ReadonlyArray<ToolResult>) => S) => Stream.Stream<Event<ToolEvent, S>>;
32
+ //#endregion
33
+ export { AnyTool, ExecuteOptions, Resolver, Toolkit, ToolsR, executeAll, executeAllWithResolver, make, nextStateFrom, Toolkit_d_exports as t, toDescriptors };
34
+ //# sourceMappingURL=Toolkit.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Toolkit.d.mts","names":[],"sources":["../../src/tool/Toolkit.ts"],"mappings":";;;;;;;;;;;KAqBY,OAAA,GAAU,IAAA;AAAA,KAEV,OAAA,eAAsB,aAAA,CAAc,OAAA;EAAA,SACrC,KAAA,EAAO,KAAA;AAAA;AAAA,KAGN,MAAA,eAAqB,aAAA,CAAc,OAAA,KAC7C,KAAA,iBAAsB,IAAA,2BAA+B,CAAA;AAAA,cAE1C,IAAA,uBAA4B,aAAA,CAAc,OAAA,GAAU,KAAA,EAAO,KAAA,KAAQ,OAAA,CAAQ,KAAA;;;;;;cAS3E,aAAA,iBAA+B,aAAA,CAAc,OAAA,GACxD,OAAA,EAAS,OAAA,CAAQ,KAAA,MAChB,aAAA,CAAc,cAAA;AAAA,KAmBL,QAAA,IAAY,IAAA,EAAM,YAAA,KAAiB,MAAA,CAAO,MAAA,CAAO,YAAA;AAAA,UAE5C,cAAA;EAAA,SACN,WAAA;AAAA;AAAA,cAGE,sBAAA,GACX,KAAA,EAAO,aAAA,CAAc,WAAA,GACrB,KAAA,EAAO,aAAA,CAAc,YAAA,GACrB,OAAA,EAAS,QAAA,EACT,OAAA,GAAU,cAAA,KACT,MAAA,CAAO,MAAA,CAAO,SAAA;;cAYJ,UAAA,GACX,KAAA,EAAO,aAAA,CAAc,WAAA,GACrB,KAAA,EAAO,aAAA,CAAc,YAAA,GACrB,OAAA,GAAU,cAAA,KACT,MAAA,CAAO,MAAA,CAAO,SAAA;AAAA,cAmIJ,aAAA,MACX,MAAA,EAAQ,MAAA,CAAO,MAAA,CAAO,SAAA,GACtB,KAAA,GAAQ,OAAA,EAAS,aAAA,CAAc,UAAA,MAAgB,CAAA,KAC9C,MAAA,CAAO,MAAA,CAAO,KAAA,CAAW,SAAA,EAAW,CAAA"}
@@ -0,0 +1,105 @@
1
+ import { t as __exportAll } from "../chunk-CfYAbeIz.mjs";
2
+ import { nextAfterFold } from "../loop/Loop.mjs";
3
+ import { isStreamingTool } from "./Tool.mjs";
4
+ import { execute, executionError, rejected } from "./Outcome.mjs";
5
+ import { isOutput } from "./ToolEvent.mjs";
6
+ import { Array, Effect, Match, Ref, Stream } from "effect";
7
+ //#region src/tool/Toolkit.ts
8
+ var Toolkit_exports = /* @__PURE__ */ __exportAll({
9
+ executeAll: () => executeAll,
10
+ executeAllWithResolver: () => executeAllWithResolver,
11
+ make: () => make,
12
+ nextStateFrom: () => nextStateFrom,
13
+ toDescriptors: () => toDescriptors
14
+ });
15
+ const make = (tools) => ({ tools });
16
+ /**
17
+ * Render every tool in a toolkit to a provider-agnostic descriptor.
18
+ * `inputSchema` is the JSON Schema document produced by the tool's
19
+ * Standard Schema converter (draft 2020-12).
20
+ */
21
+ const toDescriptors = (toolkit) => toolkit.tools.map((tool) => {
22
+ const inputSchema = tool.inputSchema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
23
+ return tool.strict !== void 0 ? {
24
+ name: tool.name,
25
+ description: tool.description,
26
+ inputSchema,
27
+ strict: tool.strict
28
+ } : {
29
+ name: tool.name,
30
+ description: tool.description,
31
+ inputSchema
32
+ };
33
+ });
34
+ const executeAllWithResolver = (tools, calls, resolve, options) => Stream.fromIterable(calls).pipe(Stream.flatMap((call) => Stream.unwrap(resolve(call).pipe(Effect.map((decision) => dispatch(tools, call, decision)))), { concurrency: options?.concurrency ?? "unbounded" }));
35
+ /** No-resolver shortcut: every call gets `Execute`. */
36
+ const executeAll = (tools, calls, options) => executeAllWithResolver(tools, calls, () => Effect.succeed(execute), options);
37
+ const dispatch = (tools, call, decision) => Match.value(decision).pipe(Match.tag("Execute", () => runOne(tools, call)), Match.tag("Reject", (d) => Stream.succeed({
38
+ _tag: "Output",
39
+ result: d.result
40
+ })), Match.exhaustive);
41
+ const valueResult = (call, tool, value) => ({
42
+ _tag: "Value",
43
+ call_id: call.call_id,
44
+ tool,
45
+ value
46
+ });
47
+ const runOne = (tools, call) => {
48
+ const tool = tools.find((t) => t.name === call.name);
49
+ if (tool === void 0) return Stream.succeed({
50
+ _tag: "Output",
51
+ result: rejected(call, "unknown_tool", `No tool registered with name "${call.name}"`)
52
+ });
53
+ if (isStreamingTool(tool)) return runStreaming(tool, call);
54
+ return runPlain(tool, call);
55
+ };
56
+ const runPlain = (tool, call) => Stream.fromEffect(Effect.gen(function* () {
57
+ const parsed = yield* Effect.try({
58
+ try: () => JSON.parse(call.arguments),
59
+ catch: () => "json_parse_error"
60
+ });
61
+ const validated = yield* Effect.tryPromise({
62
+ try: () => Promise.resolve(tool.inputSchema["~standard"].validate(parsed)),
63
+ catch: () => "validation_threw"
64
+ });
65
+ if (validated.issues !== void 0) return executionError(call, "Tool input failed schema validation");
66
+ const output = yield* tool.run(validated.value);
67
+ return valueResult(call, tool.name, output);
68
+ }).pipe(Effect.catchCause(() => Effect.succeed(executionError(call, "Tool execution failed"))), Effect.map((result) => ({
69
+ _tag: "Output",
70
+ result
71
+ }))));
72
+ const runStreaming = (tool, call) => Stream.unwrap(Effect.gen(function* () {
73
+ const parsed = yield* Effect.try({
74
+ try: () => JSON.parse(call.arguments),
75
+ catch: () => "json_parse_error"
76
+ });
77
+ const validated = yield* Effect.tryPromise({
78
+ try: () => Promise.resolve(tool.inputSchema["~standard"].validate(parsed)),
79
+ catch: () => "validation_threw"
80
+ });
81
+ if (validated.issues !== void 0) return Stream.succeed({
82
+ _tag: "Output",
83
+ result: executionError(call, "Tool input failed schema validation")
84
+ });
85
+ const ref = yield* Ref.make([]);
86
+ const intermediates = tool.run(validated.value).pipe(Stream.tap((event) => Ref.update(ref, Array.append(event))), Stream.map((data) => ({
87
+ _tag: "Intermediate",
88
+ call_id: call.call_id,
89
+ tool: tool.name,
90
+ data
91
+ })));
92
+ const output = Stream.fromEffect(Ref.get(ref).pipe(Effect.map((events) => ({
93
+ _tag: "Output",
94
+ result: valueResult(call, tool.name, tool.finalize(events))
95
+ }))));
96
+ return intermediates.pipe(Stream.concat(output));
97
+ })).pipe(Stream.catchCause(() => Stream.succeed({
98
+ _tag: "Output",
99
+ result: executionError(call, "Tool execution failed")
100
+ })));
101
+ const nextStateFrom = (stream, build) => nextAfterFold(stream, [], (acc, e) => isOutput(e) ? Array.append(acc, e.result) : acc, build);
102
+ //#endregion
103
+ export { executeAll, executeAllWithResolver, make, nextStateFrom, Toolkit_exports as t, toDescriptors };
104
+
105
+ //# sourceMappingURL=Toolkit.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Toolkit.mjs","names":["executeDecision","Arr","Loop.nextAfterFold"],"sources":["../../src/tool/Toolkit.ts"],"sourcesContent":["import { Array as Arr, Effect, Match, Ref, Stream } from \"effect\"\nimport * as Loop from \"../loop/Loop.js\"\nimport type { FunctionCall } from \"../domain/Items.js\"\nimport {\n type AnyKindTool,\n type AnyPlainTool,\n type AnyStreamingTool,\n isStreamingTool,\n type Tool,\n type ToolDescriptor,\n} from \"./Tool.js\"\nimport {\n type ToolDecision,\n type ToolResult,\n execute as executeDecision,\n executionError,\n rejected,\n} from \"./Outcome.js\"\nimport type { ToolEvent } from \"./ToolEvent.js\"\nimport { isOutput } from \"./ToolEvent.js\"\n\nexport type AnyTool = Tool<string, any, any, any>\n\nexport type Toolkit<Tools extends ReadonlyArray<AnyTool>> = {\n readonly tools: Tools\n}\n\nexport type ToolsR<Tools extends ReadonlyArray<AnyTool>> =\n Tools[number] extends Tool<any, any, any, infer R> ? R : never\n\nexport const make = <const Tools extends ReadonlyArray<AnyTool>>(tools: Tools): Toolkit<Tools> => ({\n tools,\n})\n\n/**\n * Render every tool in a toolkit to a provider-agnostic descriptor.\n * `inputSchema` is the JSON Schema document produced by the tool's\n * Standard Schema converter (draft 2020-12).\n */\nexport const toDescriptors = <Tools extends ReadonlyArray<AnyTool>>(\n toolkit: Toolkit<Tools>,\n): ReadonlyArray<ToolDescriptor> =>\n toolkit.tools.map((tool) => {\n const inputSchema = tool.inputSchema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\",\n })\n return tool.strict !== undefined\n ? { name: tool.name, description: tool.description, inputSchema, strict: tool.strict }\n : { name: tool.name, description: tool.description, inputSchema }\n })\n\n// ---------------------------------------------------------------------------\n// Resolver-based executor. Streams `ToolEvent`s in real time, dispatches\n// streaming and plain tools uniformly, and lets the caller decide what\n// happens to each call (Execute or Reject) before execution.\n//\n// `executeAllWithResolver` is the general primitive. `executeAllStream` is\n// the no-resolver shortcut.\n// ---------------------------------------------------------------------------\n\nexport type Resolver = (call: FunctionCall) => Effect.Effect<ToolDecision>\n\nexport interface ExecuteOptions {\n readonly concurrency?: number | \"unbounded\"\n}\n\nexport const executeAllWithResolver = (\n tools: ReadonlyArray<AnyKindTool>,\n calls: ReadonlyArray<FunctionCall>,\n resolve: Resolver,\n options?: ExecuteOptions,\n): Stream.Stream<ToolEvent> =>\n Stream.fromIterable(calls).pipe(\n Stream.flatMap(\n (call) =>\n Stream.unwrap(\n resolve(call).pipe(Effect.map((decision) => dispatch(tools, call, decision))),\n ),\n { concurrency: options?.concurrency ?? \"unbounded\" },\n ),\n )\n\n/** No-resolver shortcut: every call gets `Execute`. */\nexport const executeAll = (\n tools: ReadonlyArray<AnyKindTool>,\n calls: ReadonlyArray<FunctionCall>,\n options?: ExecuteOptions,\n): Stream.Stream<ToolEvent> =>\n executeAllWithResolver(tools, calls, () => Effect.succeed(executeDecision), options)\n\nconst dispatch = (\n tools: ReadonlyArray<AnyKindTool>,\n call: FunctionCall,\n decision: ToolDecision,\n): Stream.Stream<ToolEvent> =>\n Match.value(decision).pipe(\n Match.tag(\"Execute\", () => runOne(tools, call)),\n Match.tag(\"Reject\", (d) =>\n Stream.succeed<ToolEvent>({ _tag: \"Output\", result: d.result }),\n ),\n Match.exhaustive,\n )\n\nconst valueResult = (call: FunctionCall, tool: string, value: unknown): ToolResult => ({\n _tag: \"Value\",\n call_id: call.call_id,\n tool,\n value,\n})\n\nconst runOne = (\n tools: ReadonlyArray<AnyKindTool>,\n call: FunctionCall,\n): Stream.Stream<ToolEvent> => {\n const tool = tools.find((t) => t.name === call.name)\n if (tool === undefined) {\n // Graceful: emit a synthetic Failure so OTHER calls in this turn\n // still execute. LLMs hallucinate tool names; MCP tools come and go.\n return Stream.succeed<ToolEvent>({\n _tag: \"Output\",\n result: rejected(call, \"unknown_tool\", `No tool registered with name \"${call.name}\"`),\n })\n }\n if (isStreamingTool(tool)) return runStreaming(tool, call)\n return runPlain(tool, call)\n}\n\nconst runPlain = (\n tool: AnyPlainTool,\n call: FunctionCall,\n): Stream.Stream<ToolEvent> =>\n Stream.fromEffect(\n Effect.gen(function* () {\n const parsed = yield* Effect.try({\n try: () => JSON.parse(call.arguments) as unknown,\n catch: () => \"json_parse_error\" as const,\n })\n const validated = yield* Effect.tryPromise({\n try: () => Promise.resolve(tool.inputSchema[\"~standard\"].validate(parsed)),\n catch: () => \"validation_threw\" as const,\n })\n if (validated.issues !== undefined) {\n return executionError(call, \"Tool input failed schema validation\")\n }\n const output = yield* tool.run(validated.value)\n return valueResult(call, tool.name, output)\n }).pipe(\n Effect.catchCause(() => Effect.succeed(executionError(call, \"Tool execution failed\"))),\n Effect.map((result) => ({ _tag: \"Output\", result }) satisfies ToolEvent),\n ),\n )\n\nconst runStreaming = (\n tool: AnyStreamingTool,\n call: FunctionCall,\n): Stream.Stream<ToolEvent> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const parsed = yield* Effect.try({\n try: () => JSON.parse(call.arguments) as unknown,\n catch: () => \"json_parse_error\" as const,\n })\n const validated = yield* Effect.tryPromise({\n try: () => Promise.resolve(tool.inputSchema[\"~standard\"].validate(parsed)),\n catch: () => \"validation_threw\" as const,\n })\n if (validated.issues !== undefined) {\n return Stream.succeed<ToolEvent>({\n _tag: \"Output\",\n result: executionError(call, \"Tool input failed schema validation\"),\n })\n }\n\n // Real-time: tap each event into a Ref as it flows; emit one\n // Intermediate per event; then concat one synthetic Output element\n // built from the accumulated Ref via `finalize`.\n const ref = yield* Ref.make<Array<unknown>>([])\n const intermediates = tool.run(validated.value).pipe(\n Stream.tap((event) => Ref.update(ref, Arr.append(event))),\n Stream.map(\n (data) =>\n ({\n _tag: \"Intermediate\",\n call_id: call.call_id,\n tool: tool.name,\n data,\n }) satisfies ToolEvent,\n ),\n )\n const output = Stream.fromEffect(\n Ref.get(ref).pipe(\n Effect.map(\n (events) =>\n ({\n _tag: \"Output\",\n result: valueResult(call, tool.name, tool.finalize(events)),\n }) satisfies ToolEvent,\n ),\n ),\n )\n return intermediates.pipe(Stream.concat(output))\n }),\n ).pipe(\n Stream.catchCause(() =>\n Stream.succeed<ToolEvent>({\n _tag: \"Output\",\n result: executionError(call, \"Tool execution failed\"),\n }),\n ),\n )\n\n// ---------------------------------------------------------------------------\n// `nextStateFrom` - bridge from a `Stream<ToolEvent>` to the loop's emit\n// shape. Drains the stream to the consumer in real-time, taps every\n// `Output` into an internal Ref, and at end-of-stream emits\n// `Loop.next(build(results))`. Recipe never sees the Ref.\n// ---------------------------------------------------------------------------\n\nexport const nextStateFrom = <S>(\n stream: Stream.Stream<ToolEvent>,\n build: (results: ReadonlyArray<ToolResult>) => S,\n): Stream.Stream<Loop.Event<ToolEvent, S>> =>\n Loop.nextAfterFold(\n stream,\n [] as ReadonlyArray<ToolResult>,\n (acc, e) => (isOutput(e) ? Arr.append(acc, e.result) : acc),\n build,\n )\n"],"mappings":";;;;;;;;;;;;;;AA8BA,MAAa,QAAoD,WAAkC,EACjG,OACD;;;;;;AAOD,MAAa,iBACX,YAEA,QAAQ,MAAM,KAAK,SAAS;CAC1B,MAAM,cAAc,KAAK,YAAY,aAAa,WAAW,MAAM,EACjE,QAAQ,iBACT,CAAC;AACF,QAAO,KAAK,WAAW,KAAA,IACnB;EAAE,MAAM,KAAK;EAAM,aAAa,KAAK;EAAa;EAAa,QAAQ,KAAK;EAAQ,GACpF;EAAE,MAAM,KAAK;EAAM,aAAa,KAAK;EAAa;EAAa;EACnE;AAiBJ,MAAa,0BACX,OACA,OACA,SACA,YAEA,OAAO,aAAa,MAAM,CAAC,KACzB,OAAO,SACJ,SACC,OAAO,OACL,QAAQ,KAAK,CAAC,KAAK,OAAO,KAAK,aAAa,SAAS,OAAO,MAAM,SAAS,CAAC,CAAC,CAC9E,EACH,EAAE,aAAa,SAAS,eAAe,aAAa,CACrD,CACF;;AAGH,MAAa,cACX,OACA,OACA,YAEA,uBAAuB,OAAO,aAAa,OAAO,QAAQA,QAAgB,EAAE,QAAQ;AAEtF,MAAM,YACJ,OACA,MACA,aAEA,MAAM,MAAM,SAAS,CAAC,KACpB,MAAM,IAAI,iBAAiB,OAAO,OAAO,KAAK,CAAC,EAC/C,MAAM,IAAI,WAAW,MACnB,OAAO,QAAmB;CAAE,MAAM;CAAU,QAAQ,EAAE;CAAQ,CAAC,CAChE,EACD,MAAM,WACP;AAEH,MAAM,eAAe,MAAoB,MAAc,WAAgC;CACrF,MAAM;CACN,SAAS,KAAK;CACd;CACA;CACD;AAED,MAAM,UACJ,OACA,SAC6B;CAC7B,MAAM,OAAO,MAAM,MAAM,MAAM,EAAE,SAAS,KAAK,KAAK;AACpD,KAAI,SAAS,KAAA,EAGX,QAAO,OAAO,QAAmB;EAC/B,MAAM;EACN,QAAQ,SAAS,MAAM,gBAAgB,iCAAiC,KAAK,KAAK,GAAG;EACtF,CAAC;AAEJ,KAAI,gBAAgB,KAAK,CAAE,QAAO,aAAa,MAAM,KAAK;AAC1D,QAAO,SAAS,MAAM,KAAK;;AAG7B,MAAM,YACJ,MACA,SAEA,OAAO,WACL,OAAO,IAAI,aAAa;CACtB,MAAM,SAAS,OAAO,OAAO,IAAI;EAC/B,WAAW,KAAK,MAAM,KAAK,UAAU;EACrC,aAAa;EACd,CAAC;CACF,MAAM,YAAY,OAAO,OAAO,WAAW;EACzC,WAAW,QAAQ,QAAQ,KAAK,YAAY,aAAa,SAAS,OAAO,CAAC;EAC1E,aAAa;EACd,CAAC;AACF,KAAI,UAAU,WAAW,KAAA,EACvB,QAAO,eAAe,MAAM,sCAAsC;CAEpE,MAAM,SAAS,OAAO,KAAK,IAAI,UAAU,MAAM;AAC/C,QAAO,YAAY,MAAM,KAAK,MAAM,OAAO;EAC3C,CAAC,KACD,OAAO,iBAAiB,OAAO,QAAQ,eAAe,MAAM,wBAAwB,CAAC,CAAC,EACtF,OAAO,KAAK,YAAY;CAAE,MAAM;CAAU;CAAQ,EAAsB,CACzE,CACF;AAEH,MAAM,gBACJ,MACA,SAEA,OAAO,OACL,OAAO,IAAI,aAAa;CACtB,MAAM,SAAS,OAAO,OAAO,IAAI;EAC/B,WAAW,KAAK,MAAM,KAAK,UAAU;EACrC,aAAa;EACd,CAAC;CACF,MAAM,YAAY,OAAO,OAAO,WAAW;EACzC,WAAW,QAAQ,QAAQ,KAAK,YAAY,aAAa,SAAS,OAAO,CAAC;EAC1E,aAAa;EACd,CAAC;AACF,KAAI,UAAU,WAAW,KAAA,EACvB,QAAO,OAAO,QAAmB;EAC/B,MAAM;EACN,QAAQ,eAAe,MAAM,sCAAsC;EACpE,CAAC;CAMJ,MAAM,MAAM,OAAO,IAAI,KAAqB,EAAE,CAAC;CAC/C,MAAM,gBAAgB,KAAK,IAAI,UAAU,MAAM,CAAC,KAC9C,OAAO,KAAK,UAAU,IAAI,OAAO,KAAKC,MAAI,OAAO,MAAM,CAAC,CAAC,EACzD,OAAO,KACJ,UACE;EACC,MAAM;EACN,SAAS,KAAK;EACd,MAAM,KAAK;EACX;EACD,EACJ,CACF;CACD,MAAM,SAAS,OAAO,WACpB,IAAI,IAAI,IAAI,CAAC,KACX,OAAO,KACJ,YACE;EACC,MAAM;EACN,QAAQ,YAAY,MAAM,KAAK,MAAM,KAAK,SAAS,OAAO,CAAC;EAC5D,EACJ,CACF,CACF;AACD,QAAO,cAAc,KAAK,OAAO,OAAO,OAAO,CAAC;EAChD,CACH,CAAC,KACA,OAAO,iBACL,OAAO,QAAmB;CACxB,MAAM;CACN,QAAQ,eAAe,MAAM,wBAAwB;CACtD,CAAC,CACH,CACF;AASH,MAAa,iBACX,QACA,UAEAC,cACE,QACA,EAAE,GACD,KAAK,MAAO,SAAS,EAAE,GAAGD,MAAI,OAAO,KAAK,EAAE,OAAO,GAAG,KACvD,MACD"}
package/package.json ADDED
@@ -0,0 +1,127 @@
1
+ {
2
+ "name": "@effect-uai/core",
3
+ "version": "0.1.0",
4
+ "description": "Low-level primitives (loop, conversation, items, tools, streaming codecs) for building AI agents with Effect.",
5
+ "keywords": [
6
+ "agents",
7
+ "ai",
8
+ "effect",
9
+ "llm",
10
+ "streaming"
11
+ ],
12
+ "homepage": "https://github.com/betalyra/effect-uai/tree/main/packages/core",
13
+ "bugs": {
14
+ "url": "https://github.com/betalyra/effect-uai/issues"
15
+ },
16
+ "license": "MIT",
17
+ "author": "Betalyra",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/betalyra/effect-uai",
21
+ "directory": "packages/core"
22
+ },
23
+ "type": "module",
24
+ "main": "./dist/index.mjs",
25
+ "types": "./dist/index.d.mts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.mts",
29
+ "import": "./dist/index.mjs"
30
+ },
31
+ "./AiError": {
32
+ "types": "./dist/domain/AiError.d.mts",
33
+ "import": "./dist/domain/AiError.mjs"
34
+ },
35
+ "./Items": {
36
+ "types": "./dist/domain/Items.d.mts",
37
+ "import": "./dist/domain/Items.mjs"
38
+ },
39
+ "./Turn": {
40
+ "types": "./dist/domain/Turn.d.mts",
41
+ "import": "./dist/domain/Turn.mjs"
42
+ },
43
+ "./LanguageModel": {
44
+ "types": "./dist/language-model/LanguageModel.d.mts",
45
+ "import": "./dist/language-model/LanguageModel.mjs"
46
+ },
47
+ "./Loop": {
48
+ "types": "./dist/loop/Loop.d.mts",
49
+ "import": "./dist/loop/Loop.mjs"
50
+ },
51
+ "./Match": {
52
+ "types": "./dist/match/Match.d.mts",
53
+ "import": "./dist/match/Match.mjs"
54
+ },
55
+ "./Tool": {
56
+ "types": "./dist/tool/Tool.d.mts",
57
+ "import": "./dist/tool/Tool.mjs"
58
+ },
59
+ "./Toolkit": {
60
+ "types": "./dist/tool/Toolkit.d.mts",
61
+ "import": "./dist/tool/Toolkit.mjs"
62
+ },
63
+ "./Outcome": {
64
+ "types": "./dist/tool/Outcome.d.mts",
65
+ "import": "./dist/tool/Outcome.mjs"
66
+ },
67
+ "./ToolEvent": {
68
+ "types": "./dist/tool/ToolEvent.d.mts",
69
+ "import": "./dist/tool/ToolEvent.mjs"
70
+ },
71
+ "./Resolvers": {
72
+ "types": "./dist/tool/Resolvers.d.mts",
73
+ "import": "./dist/tool/Resolvers.mjs"
74
+ },
75
+ "./HistoryCheck": {
76
+ "types": "./dist/tool/HistoryCheck.d.mts",
77
+ "import": "./dist/tool/HistoryCheck.mjs"
78
+ },
79
+ "./JSONL": {
80
+ "types": "./dist/streaming/JSONL.d.mts",
81
+ "import": "./dist/streaming/JSONL.mjs"
82
+ },
83
+ "./Lines": {
84
+ "types": "./dist/streaming/Lines.d.mts",
85
+ "import": "./dist/streaming/Lines.mjs"
86
+ },
87
+ "./SSE": {
88
+ "types": "./dist/streaming/SSE.d.mts",
89
+ "import": "./dist/streaming/SSE.mjs"
90
+ },
91
+ "./StructuredFormat": {
92
+ "types": "./dist/structured-format/StructuredFormat.d.mts",
93
+ "import": "./dist/structured-format/StructuredFormat.mjs"
94
+ },
95
+ "./Metrics": {
96
+ "types": "./dist/observability/Metrics.d.mts",
97
+ "import": "./dist/observability/Metrics.mjs"
98
+ },
99
+ "./testing/MockProvider": {
100
+ "types": "./dist/testing/MockProvider.d.mts",
101
+ "import": "./dist/testing/MockProvider.mjs"
102
+ }
103
+ },
104
+ "files": [
105
+ "dist",
106
+ "src",
107
+ "README.md",
108
+ "LICENSE"
109
+ ],
110
+ "publishConfig": {
111
+ "access": "public"
112
+ },
113
+ "dependencies": {
114
+ "@standard-schema/spec": "^1.1.0"
115
+ },
116
+ "peerDependencies": {
117
+ "effect": "4.0.0-beta.57"
118
+ },
119
+ "devDependencies": {
120
+ "effect": "4.0.0-beta.57",
121
+ "typescript": "^6.0.3"
122
+ },
123
+ "scripts": {
124
+ "build": "tsdown",
125
+ "typecheck": "tsc --noEmit"
126
+ }
127
+ }
@@ -0,0 +1,93 @@
1
+ import { Data, Duration } from "effect"
2
+
3
+ export type Scope = "rpm" | "tpm" | "rpd" | "tpd"
4
+
5
+ export class RateLimited extends Data.TaggedError("RateLimited")<{
6
+ provider: string
7
+ retryAfter?: Duration.Duration
8
+ scope?: Scope
9
+ requestId?: string
10
+ raw: unknown
11
+ }> {}
12
+
13
+ export class Unavailable extends Data.TaggedError("Unavailable")<{
14
+ provider: string
15
+ retryAfter?: Duration.Duration
16
+ status?: number
17
+ requestId?: string
18
+ raw: unknown
19
+ }> {}
20
+
21
+ export class Timeout extends Data.TaggedError("Timeout")<{
22
+ provider: string
23
+ requestId?: string
24
+ raw: unknown
25
+ }> {}
26
+
27
+ export class ContentFiltered extends Data.TaggedError("ContentFiltered")<{
28
+ provider: string
29
+ reason?: string
30
+ requestId?: string
31
+ raw: unknown
32
+ }> {}
33
+
34
+ export class ContextLengthExceeded extends Data.TaggedError("ContextLengthExceeded")<{
35
+ provider: string
36
+ modelLimit?: number
37
+ requested?: number
38
+ raw: unknown
39
+ }> {}
40
+
41
+ export class InvalidRequest extends Data.TaggedError("InvalidRequest")<{
42
+ provider: string
43
+ param?: string
44
+ requestId?: string
45
+ raw: unknown
46
+ }> {}
47
+
48
+ export type AuthSubtype = "auth" | "permission" | "billing" | "quota"
49
+
50
+ export class AuthFailed extends Data.TaggedError("AuthFailed")<{
51
+ provider: string
52
+ subtype: AuthSubtype
53
+ raw: unknown
54
+ }> {}
55
+
56
+ export class Cancelled extends Data.TaggedError("Cancelled")<{
57
+ provider: string
58
+ }> {}
59
+
60
+ /**
61
+ * The model errored mid-generation. Distinct from `Unavailable` (transport
62
+ * problem before generation started) and from `IncompleteTurn` (provider
63
+ * stream ended without a terminal event). The provider's own error message,
64
+ * if any, is on `message`.
65
+ */
66
+ export class GenerationFailed extends Data.TaggedError("GenerationFailed")<{
67
+ provider: string
68
+ code?: string
69
+ message?: string
70
+ requestId?: string
71
+ raw: unknown
72
+ }> {}
73
+
74
+ /**
75
+ * The provider's delta stream ended without a terminal `turn_complete`.
76
+ * Indicates a misbehaving provider or a connection that dropped mid-flight.
77
+ * Non-terminal deltas seen so far have already been emitted downstream.
78
+ */
79
+ export class IncompleteTurn extends Data.TaggedError("IncompleteTurn")<{
80
+ raw?: unknown
81
+ }> {}
82
+
83
+ export type AiError =
84
+ | RateLimited
85
+ | Unavailable
86
+ | Timeout
87
+ | ContentFiltered
88
+ | ContextLengthExceeded
89
+ | InvalidRequest
90
+ | AuthFailed
91
+ | Cancelled
92
+ | IncompleteTurn
93
+ | GenerationFailed