|
| 1 | +import { Schema } from "effect" |
| 2 | +import { type Response, Tool } from "effect/unstable/ai" |
| 3 | +import { describe, expect, it } from "tstyche" |
| 4 | + |
| 5 | +const BooleanTool = Tool.make("BooleanTool", { |
| 6 | + parameters: Schema.Boolean, |
| 7 | + success: Schema.String, |
| 8 | + failure: Schema.Boolean |
| 9 | +}) |
| 10 | + |
| 11 | +const TransformTool = Tool.make("TransformTool", { |
| 12 | + parameters: Schema.FiniteFromString, |
| 13 | + success: Schema.Number, |
| 14 | + failure: Schema.Struct({ message: Schema.String }) |
| 15 | +}) |
| 16 | + |
| 17 | +type Tools = { |
| 18 | + readonly BooleanTool: typeof BooleanTool |
| 19 | + readonly TransformTool: typeof TransformTool |
| 20 | +} |
| 21 | + |
| 22 | +describe("Response", () => { |
| 23 | + it("preserves generic intersected tool records after narrowing", () => { |
| 24 | + const narrow = < |
| 25 | + StaticTools extends Record<string, Tool.Any>, |
| 26 | + DynamicTools extends Record<string, Tool.Any>, |
| 27 | + EncodedParameters extends boolean |
| 28 | + >(part: Response.StreamPart<StaticTools & DynamicTools, EncodedParameters>) => { |
| 29 | + if (part.type === "error") return |
| 30 | + |
| 31 | + const narrowed: Response.StreamPart<StaticTools & DynamicTools, EncodedParameters> = part |
| 32 | + return narrowed |
| 33 | + } |
| 34 | + |
| 35 | + void narrow |
| 36 | + }) |
| 37 | + |
| 38 | + it("preserves tool names with decoded and encoded parameters", () => { |
| 39 | + const decoded = null as unknown as Response.ToolCallParts<Tools> |
| 40 | + if (decoded.name === "BooleanTool") { |
| 41 | + expect(decoded.params).type.toBe<boolean>() |
| 42 | + } else { |
| 43 | + expect(decoded.params).type.toBe<number>() |
| 44 | + } |
| 45 | + |
| 46 | + const encoded = null as unknown as Response.ToolCallParts<Tools, true> |
| 47 | + if (encoded.name === "BooleanTool") { |
| 48 | + expect(encoded.params).type.toBe<boolean>() |
| 49 | + } else { |
| 50 | + expect(encoded.params).type.toBe<string>() |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + it("preserves tool names with success and failure results", () => { |
| 55 | + const result = null as unknown as Response.ToolResultParts<Tools> |
| 56 | + if (result.name === "BooleanTool") { |
| 57 | + if (result.isFailure) { |
| 58 | + expect(result.result).type.toBe<boolean>() |
| 59 | + } else { |
| 60 | + expect(result.result).type.toBe<string>() |
| 61 | + } |
| 62 | + } else if (result.isFailure) { |
| 63 | + expect(result.result).type.toBe<{ readonly message: string }>() |
| 64 | + } else { |
| 65 | + expect(result.result).type.toBe<number>() |
| 66 | + } |
| 67 | + }) |
| 68 | +}) |
0 commit comments