forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-object.test.ts
More file actions
184 lines (167 loc) · 6.21 KB
/
Copy pathgenerate-object.test.ts
File metadata and controls
184 lines (167 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { describe, expect, test } from "bun:test"
import { Effect, Schema } from "effect"
import { LLM } from "../src"
import * as OpenAIChat from "../src/protocols/openai-chat"
import { Auth } from "../src/route"
import { Tool, toDefinitions } from "../src/tool"
import { it } from "./lib/effect"
import { dynamicResponse } from "./lib/http"
import { finishChunk, toolCallChunk } from "./lib/openai-chunks"
import { sseEvents } from "./lib/sse"
type OpenAIChatBody = {
readonly tool_choice?: unknown
readonly tools?: ReadonlyArray<{
readonly function: {
readonly parameters: unknown
}
}>
}
const model = OpenAIChat.route
.with({ endpoint: { baseURL: "https://api.openai.test/v1/" }, auth: Auth.bearer("test") })
.model({ id: "gpt-4o-mini" })
const Json = Schema.fromJsonString(Schema.Unknown)
const decodeJson = Schema.decodeUnknownSync(Json)
const decodeBody = (text: string): OpenAIChatBody => decodeJson(text) as OpenAIChatBody
describe("Tool.make (dynamic JSON Schema)", () => {
test("forwards JSON Schema and description through toDefinitions", () => {
const jsonSchema = {
type: "object" as const,
properties: { city: { type: "string" } },
required: ["city"],
}
const lookup = Tool.make({
description: "Look up something",
jsonSchema,
execute: () => Effect.succeed({ ok: true }),
})
const [definition] = toDefinitions({ lookup })
expect(definition?.name).toBe("lookup")
expect(definition?.description).toBe("Look up something")
expect(definition?.inputSchema).toEqual(jsonSchema)
})
test("execute receives the raw input untouched", async () => {
const seen: unknown[] = []
const tool = Tool.make({
description: "echo",
jsonSchema: { type: "object" },
execute: (params) =>
Effect.sync(() => {
seen.push(params)
return { ok: true }
}),
})
const result = await Effect.runPromise(tool.execute({ hello: "world" }))
expect(seen).toEqual([{ hello: "world" }])
expect(result).toEqual({ ok: true })
})
})
describe("LLM.generateObject", () => {
it.effect("forces a synthetic tool call and decodes the input", () =>
Effect.gen(function* () {
const bodies: OpenAIChatBody[] = []
const layer = dynamicResponse((input) =>
Effect.sync(() => {
bodies.push(decodeBody(input.text))
return input.respond(
sseEvents(
toolCallChunk("call_1", "generate_object", '{"city":"Paris","temp":22}'),
finishChunk("tool_calls"),
),
{ headers: { "content-type": "text/event-stream" } },
)
}),
)
const response = yield* LLM.generateObject({
model,
prompt: "Return a structured weather report.",
schema: Schema.Struct({ city: Schema.String, temp: Schema.Number }),
}).pipe(Effect.provide(layer))
expect(response.object).toEqual({ city: "Paris", temp: 22 })
expect(response.response.toolCalls).toHaveLength(1)
expect(bodies).toHaveLength(1)
expect(bodies[0].tool_choice).toEqual({ type: "function", function: { name: "generate_object" } })
const tool = bodies[0].tools?.[0]
expect(bodies[0].tools).toHaveLength(1)
expect(tool).toMatchObject({
type: "function",
function: { name: "generate_object" },
})
const params = tool?.function.parameters as {
readonly type?: unknown
readonly required?: unknown
readonly properties?: Record<string, unknown>
}
expect(params.type).toBe("object")
expect(params.required).toEqual(["city", "temp"])
expect(params.properties?.city).toMatchObject({ type: "string" })
expect(params.properties?.temp).toBeDefined()
}),
)
it.effect("accepts a raw JSON Schema and returns the input untouched", () =>
Effect.gen(function* () {
const bodies: OpenAIChatBody[] = []
const layer = dynamicResponse((input) =>
Effect.sync(() => {
bodies.push(decodeBody(input.text))
return input.respond(
sseEvents(toolCallChunk("call_1", "generate_object", '{"name":"Ada","age":30}'), finishChunk("tool_calls")),
{ headers: { "content-type": "text/event-stream" } },
)
}),
)
const response = yield* LLM.generateObject({
model,
prompt: "Extract the user.",
jsonSchema: {
type: "object",
properties: { name: { type: "string" }, age: { type: "number" } },
required: ["name", "age"],
},
}).pipe(Effect.provide(layer))
expect(response.object).toEqual({ name: "Ada", age: 30 })
expect(bodies[0].tools?.[0]?.function.parameters).toEqual({
type: "object",
properties: { name: { type: "string" }, age: { type: "number" } },
required: ["name", "age"],
})
}),
)
it.effect("fails when the model does not call the synthetic tool", () =>
Effect.gen(function* () {
const layer = dynamicResponse((input) =>
Effect.sync(() =>
input.respond(sseEvents({ id: "x", choices: [{ delta: { content: "no thanks" }, finish_reason: "stop" }] }), {
headers: { "content-type": "text/event-stream" },
}),
),
)
const exit = yield* LLM.generateObject({
model,
prompt: "Return a structured value.",
schema: Schema.Struct({ value: Schema.Number }),
}).pipe(Effect.provide(layer), Effect.exit)
expect(exit._tag).toBe("Failure")
}),
)
it.effect("fails with a decode error when the tool input does not match the schema", () =>
Effect.gen(function* () {
const layer = dynamicResponse((input) =>
Effect.sync(() =>
input.respond(
sseEvents(
toolCallChunk("call_1", "generate_object", '{"value":"not-a-number"}'),
finishChunk("tool_calls"),
),
{ headers: { "content-type": "text/event-stream" } },
),
),
)
const exit = yield* LLM.generateObject({
model,
prompt: "Return a structured value.",
schema: Schema.Struct({ value: Schema.Number }),
}).pipe(Effect.provide(layer), Effect.exit)
expect(exit._tag).toBe("Failure")
}),
)
})