Skip to content

Commit c92fa95

Browse files
committed
test(core): narrow AI SDK endpoint regressions
1 parent f64a7fd commit c92fa95

2 files changed

Lines changed: 68 additions & 256 deletions

File tree

Lines changed: 15 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { createOpenAICompatible } from "@ai-sdk/openai-compatible"
2-
import { AIError, LLM, LanguageModel, Message } from "@opencode/ai"
2+
import { LLM, LanguageModel, Message } from "@opencode/ai"
33
import { LLMClient, RequestExecutor } from "@opencode/ai/route"
4-
import { HttpClientRequest } from "effect/http"
54
import { expect } from "bun:test"
6-
import { Effect, Layer, Result } from "effect"
5+
import { Effect, Layer } from "effect"
76
import { AISDK } from "../src/aisdk"
87
import { Model } from "@opencode/core/model"
98
import { Provider } from "@opencode/core/provider"
@@ -21,185 +20,25 @@ const client = LLMClient.layer.pipe(
2120
const chunk =
2221
'data: {"id":"r","object":"chat.completion.chunk","created":0,"model":"api-model","choices":[{"index":0,"delta":{"content":"ok"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n'
2322

24-
// Returning the original route from AISDK.route.with must fail the overridden cases.
25-
for (const baseURL of [
26-
undefined,
27-
"https://replacement.example.invalid/v1",
28-
"https://replacement.example.invalid/custom/v2/",
29-
]) {
30-
it.effect(`keeps the AI SDK request endpoint local to the model route: ${baseURL ?? "unchanged"}`, () =>
31-
Effect.gen(function* () {
32-
const sent: string[] = []
33-
const aisdk = yield* AISDK.Service
34-
yield* aisdk.hook.sdk((event) => {
35-
event.sdk = createOpenAICompatible({
36-
...event.options,
37-
name: String(event.options.name),
38-
baseURL: String(event.options.baseURL),
39-
})
40-
})
41-
const resolved = yield* aisdk.model({
42-
...Model.Info.make({
43-
...Model.Info.default(Provider.ID.make("probe"), Model.ID.make("api-model")),
44-
package: Provider.aisdk("@ai-sdk/openai-compatible"),
45-
}),
46-
settings: {
47-
baseURL: "https://original.example.invalid/v1",
48-
apiKey: "test",
49-
fetch: Object.assign(
50-
async (input: string | URL | Request) => {
51-
sent.push(input instanceof Request ? input.url : String(input))
52-
return new Response(chunk, { headers: { "content-type": "text/event-stream" } })
53-
},
54-
{ preconnect: fetch.preconnect },
55-
),
56-
},
57-
})
58-
const selected =
59-
baseURL === undefined
60-
? resolved
61-
: LanguageModel.update(resolved, {
62-
route: resolved.route.with({ endpoint: { baseURL } }),
63-
})
64-
yield* LLMClient.generate(LLM.request({ model: selected, messages: [Message.user("hello")] })).pipe(
65-
Effect.provide(client),
66-
)
67-
yield* LLMClient.generate(LLM.request({ model: resolved, messages: [Message.user("control")] })).pipe(
68-
Effect.provide(client),
69-
)
70-
expect(sent).toEqual([
71-
`${(baseURL ?? "https://original.example.invalid/v1").replace(/\/$/, "")}/chat/completions`,
72-
"https://original.example.invalid/v1/chat/completions",
73-
])
74-
}),
75-
)
76-
}
77-
78-
it.effect("caches overridden endpoints separately while retaining SDK and request hooks", () =>
23+
it.effect("overrides the AI SDK request URL without changing the original model", () =>
7924
Effect.gen(function* () {
80-
const aisdk = yield* AISDK.Service
81-
const loaded: string[] = []
82-
const sent: Array<{ url: string; headers: Headers }> = []
83-
yield* aisdk.hook.sdk((event) => {
84-
loaded.push(String(event.options.baseURL))
85-
event.sdk = createOpenAICompatible({
86-
...event.options,
87-
name: String(event.options.name),
88-
baseURL: String(event.options.baseURL),
89-
})
90-
})
91-
const original = yield* aisdk.model({
92-
...Model.Info.make({
93-
...Model.Info.default(Provider.ID.make("probe"), Model.ID.make("api-model")),
94-
package: Provider.aisdk("@ai-sdk/openai-compatible"),
95-
}),
96-
headers: { "x-config": "retained" },
97-
settings: {
98-
baseURL: "https://original.example.invalid/v1",
99-
apiKey: "test",
100-
fetch: Object.assign(
101-
async (input: string | URL | Request, init?: RequestInit) => {
102-
sent.push({
103-
url: input instanceof Request ? input.url : String(input),
104-
headers: new Headers(init?.headers),
105-
})
106-
return new Response(chunk, { headers: { "content-type": "text/event-stream" } })
107-
},
108-
{ preconnect: fetch.preconnect },
109-
),
110-
},
111-
})
112-
const first = LanguageModel.update(original, {
113-
route: original.route.with({ endpoint: { baseURL: "https://first.example.invalid/v2" } }),
114-
})
115-
const second = LanguageModel.update(first, {
116-
route: first.route.with({ endpoint: { baseURL: "https://second.example.invalid/v3" } }),
117-
})
118-
for (const selected of [first, second, first, original]) {
119-
yield* LLMClient.generate(LLM.request({ model: selected, prompt: "hello" }), {
120-
http: (request, handler) => handler(HttpClientRequest.setHeader(request, "x-hook", "retained")),
121-
}).pipe(Effect.provide(client))
122-
}
123-
expect(loaded).toEqual([
124-
"https://original.example.invalid/v1",
125-
"https://first.example.invalid/v2",
126-
"https://second.example.invalid/v3",
127-
])
128-
expect(sent.map((request) => request.url)).toEqual([
129-
"https://first.example.invalid/v2/chat/completions",
130-
"https://second.example.invalid/v3/chat/completions",
131-
"https://first.example.invalid/v2/chat/completions",
132-
"https://original.example.invalid/v1/chat/completions",
133-
])
134-
expect(sent.every((request) => request.headers.get("authorization") === "Bearer test")).toBe(true)
135-
expect(sent.every((request) => request.headers.get("x-config") === "retained")).toBe(true)
136-
expect(sent.every((request) => request.headers.get("x-hook") === "retained")).toBe(true)
137-
}),
138-
)
139-
140-
it.effect("overrides an SDK default endpoint and reruns language hooks without changing the original", () =>
141-
Effect.gen(function* () {
142-
const aisdk = yield* AISDK.Service
14325
const sent: string[] = []
144-
const languages: unknown[] = []
145-
yield* aisdk.hook.sdk((event) => {
146-
event.sdk = createOpenAICompatible({
147-
...event.options,
148-
name: String(event.options.name),
149-
baseURL: String(event.options.baseURL ?? "https://default.example.invalid/v1"),
150-
})
151-
})
152-
yield* aisdk.hook.language((event) => {
153-
languages.push(event.model.settings?.baseURL)
154-
})
155-
const original = yield* aisdk.model({
156-
...Model.Info.make({
157-
...Model.Info.default(Provider.ID.make("probe"), Model.ID.make("api-model")),
158-
package: Provider.aisdk("@ai-sdk/openai-compatible"),
159-
}),
160-
settings: {
161-
fetch: Object.assign(
162-
async (input: string | URL | Request) => {
163-
sent.push(input instanceof Request ? input.url : String(input))
164-
return new Response(chunk, { headers: { "content-type": "text/event-stream" } })
165-
},
166-
{ preconnect: fetch.preconnect },
167-
),
168-
},
169-
})
170-
const changed = LanguageModel.update(original, {
171-
route: original.route.with({ endpoint: { baseURL: "https://override.example.invalid/v2" } }),
172-
})
173-
for (const selected of [changed, original]) {
174-
yield* LLMClient.generate(LLM.request({ model: selected, prompt: "hello" })).pipe(Effect.provide(client))
175-
}
176-
expect(languages).toEqual([undefined, "https://override.example.invalid/v2"])
177-
expect(sent).toEqual([
178-
"https://override.example.invalid/v2/chat/completions",
179-
"https://default.example.invalid/v1/chat/completions",
180-
])
181-
}),
182-
)
183-
184-
it.effect("reports overridden SDK initialization failures without poisoning the original model", () =>
185-
Effect.gen(function* () {
18626
const aisdk = yield* AISDK.Service
187-
const sent: string[] = []
18827
yield* aisdk.hook.sdk((event) => {
189-
if (event.options.baseURL === "https://broken.example.invalid/v2") throw new Error("SDK initialization failed")
19028
event.sdk = createOpenAICompatible({
19129
...event.options,
19230
name: String(event.options.name),
19331
baseURL: String(event.options.baseURL),
19432
})
19533
})
196-
const original = yield* aisdk.model({
34+
const resolved = yield* aisdk.model({
19735
...Model.Info.make({
19836
...Model.Info.default(Provider.ID.make("probe"), Model.ID.make("api-model")),
19937
package: Provider.aisdk("@ai-sdk/openai-compatible"),
20038
}),
20139
settings: {
20240
baseURL: "https://original.example.invalid/v1",
41+
apiKey: "test",
20342
fetch: Object.assign(
20443
async (input: string | URL | Request) => {
20544
sent.push(input instanceof Request ? input.url : String(input))
@@ -209,17 +48,18 @@ it.effect("reports overridden SDK initialization failures without poisoning the
20948
),
21049
},
21150
})
212-
const changed = LanguageModel.update(original, {
213-
route: original.route.with({ endpoint: { baseURL: "https://broken.example.invalid/v2" } }),
51+
const selected = LanguageModel.update(resolved, {
52+
route: resolved.route.with({ endpoint: { baseURL: "https://replacement.example.invalid/custom/v2/" } }),
21453
})
215-
const result = yield* LLMClient.generate(LLM.request({ model: changed, prompt: "hello" })).pipe(
54+
yield* LLMClient.generate(LLM.request({ model: selected, messages: [Message.user("hello")] })).pipe(
55+
Effect.provide(client),
56+
)
57+
yield* LLMClient.generate(LLM.request({ model: resolved, messages: [Message.user("control")] })).pipe(
21658
Effect.provide(client),
217-
Effect.result,
21859
)
219-
expect(Result.isFailure(result)).toBe(true)
220-
if (Result.isFailure(result)) expect(result.failure).toBeInstanceOf(AIError)
221-
expect(sent).toEqual([])
222-
yield* LLMClient.generate(LLM.request({ model: original, prompt: "control" })).pipe(Effect.provide(client))
223-
expect(sent).toEqual(["https://original.example.invalid/v1/chat/completions"])
60+
expect(sent).toEqual([
61+
"https://replacement.example.invalid/custom/v2/chat/completions",
62+
"https://original.example.invalid/v1/chat/completions",
63+
])
22464
}),
22565
)

‎packages/core/test/session-model-request-aisdk.test.ts‎

Lines changed: 53 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -44,86 +44,58 @@ const session = Session.Info.make({
4444
const chunk =
4545
'data: {"id":"r","object":"chat.completion.chunk","created":0,"model":"api-model","choices":[{"index":0,"delta":{"content":"ok"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n'
4646

47-
for (const kind of ["primary", "compaction", "title", "generate"] as const) {
48-
it.effect(`applies session model and HTTP hooks to an AI SDK ${kind} request`, () =>
49-
Effect.gen(function* () {
50-
const aisdk = yield* AISDK.Service
51-
const hooks = yield* PluginHooks.Service
52-
const sent: Array<{ url: string; headers: Headers }> = []
53-
const observed: Array<{ baseURL: string | undefined; kind: string }> = []
54-
const httpURLs: string[] = []
55-
let responses = 0
56-
yield* aisdk.hook.sdk((event) => {
57-
event.sdk = createOpenAICompatible({
58-
...event.options,
59-
name: String(event.options.name),
60-
baseURL: String(event.options.baseURL),
61-
})
47+
it.effect("applies a session model.request hook to the AI SDK request URL", () =>
48+
Effect.gen(function* () {
49+
const aisdk = yield* AISDK.Service
50+
const hooks = yield* PluginHooks.Service
51+
const sent: string[] = []
52+
const observed: Array<string | undefined> = []
53+
yield* aisdk.hook.sdk((event) => {
54+
event.sdk = createOpenAICompatible({
55+
...event.options,
56+
name: String(event.options.name),
57+
baseURL: String(event.options.baseURL),
6258
})
63-
const original = yield* aisdk.model({
64-
...Model.Info.make({
65-
...Model.Info.default(Provider.ID.make("probe"), Model.ID.make("api-model")),
66-
package: Provider.aisdk("@ai-sdk/openai-compatible"),
67-
}),
68-
settings: {
69-
baseURL: "https://original.example.invalid/v1",
70-
apiKey: "test",
71-
fetch: Object.assign(
72-
async (input: string | URL | Request, init?: RequestInit) => {
73-
sent.push({
74-
url: input instanceof Request ? input.url : String(input),
75-
headers: new Headers(init?.headers),
76-
})
77-
return new Response(chunk, { headers: { "content-type": "text/event-stream" } })
78-
},
79-
{ preconnect: fetch.preconnect },
80-
),
81-
},
82-
})
83-
const routing = yield* hooks.register("session", "model.request", (event) =>
84-
Effect.sync(() => {
85-
observed.push({ baseURL: event.baseURL, kind: event.kind })
86-
event.baseURL = "https://replacement.example.invalid/custom/v2/"
87-
event.headers["x-model-hook"] = "retained"
88-
}),
89-
)
90-
yield* hooks.register("session", "http.request", (event) =>
91-
Effect.sync(() => {
92-
httpURLs.push(event.request.url)
93-
event.request.headers.set("x-http-hook", "retained")
94-
}),
95-
)
96-
yield* hooks.register("session", "http.response", () => Effect.sync(() => void responses++))
97-
const requests = yield* SessionModelRequest.Service.pipe(Effect.provide(SessionModelRequest.layer))
98-
const input = {
99-
session,
100-
agent: Agent.ID.make("build"),
101-
model: SessionRunnerModel.resolved(original, {
102-
capabilities: { tools: true, input: ["text"], output: ["text"] },
103-
cost: [],
104-
limit: { context: 200_000, output: 32_000 },
105-
}),
106-
system: [],
107-
messages: [Message.user("hello")],
108-
}
109-
const prepared = yield* requests[kind](input)
110-
yield* LLMClient.generate(prepared.request, prepared.options).pipe(Effect.provide(client))
111-
yield* routing.dispose
112-
const control = yield* requests[kind](input)
113-
yield* LLMClient.generate(control.request, control.options).pipe(Effect.provide(client))
59+
})
60+
const original = yield* aisdk.model({
61+
...Model.Info.make({
62+
...Model.Info.default(Provider.ID.make("probe"), Model.ID.make("api-model")),
63+
package: Provider.aisdk("@ai-sdk/openai-compatible"),
64+
}),
65+
settings: {
66+
baseURL: "https://original.example.invalid/v1",
67+
apiKey: "test",
68+
fetch: Object.assign(
69+
async (input: string | URL | Request) => {
70+
sent.push(input instanceof Request ? input.url : String(input))
71+
return new Response(chunk, { headers: { "content-type": "text/event-stream" } })
72+
},
73+
{ preconnect: fetch.preconnect },
74+
),
75+
},
76+
})
77+
yield* hooks.register("session", "model.request", (event) =>
78+
Effect.sync(() => {
79+
observed.push(event.baseURL)
80+
event.baseURL = "https://replacement.example.invalid/custom/v2/"
81+
}),
82+
)
83+
const requests = yield* SessionModelRequest.Service.pipe(Effect.provide(SessionModelRequest.layer))
84+
const input = {
85+
session,
86+
agent: Agent.ID.make("build"),
87+
model: SessionRunnerModel.resolved(original, {
88+
capabilities: { tools: true, input: ["text"], output: ["text"] },
89+
cost: [],
90+
limit: { context: 200_000, output: 32_000 },
91+
}),
92+
system: [],
93+
messages: [Message.user("hello")],
94+
}
95+
const prepared = yield* requests.primary(input)
96+
yield* LLMClient.generate(prepared.request, prepared.options).pipe(Effect.provide(client))
11497

115-
expect(sent.map((request) => request.url)).toEqual([
116-
"https://replacement.example.invalid/custom/v2/chat/completions",
117-
"https://original.example.invalid/v1/chat/completions",
118-
])
119-
expect(observed).toEqual([{ baseURL: "https://original.example.invalid/v1", kind }])
120-
expect(httpURLs).toEqual(sent.map((request) => request.url))
121-
expect(responses).toBe(2)
122-
expect(sent[0].headers.get("x-model-hook")).toBe("retained")
123-
expect(sent[1].headers.has("x-model-hook")).toBe(false)
124-
expect(sent.every((request) => request.headers.get("x-http-hook") === "retained")).toBe(true)
125-
expect(sent.every((request) => request.headers.get("authorization") === "Bearer test")).toBe(true)
126-
expect(sent.every((request) => request.headers.get("x-opencode-session-id") === session.id)).toBe(true)
127-
}).pipe(Effect.provideService(SessionModelTransport.Service, transport)),
128-
)
129-
}
98+
expect(sent).toEqual(["https://replacement.example.invalid/custom/v2/chat/completions"])
99+
expect(observed).toEqual(["https://original.example.invalid/v1"])
100+
}).pipe(Effect.provideService(SessionModelTransport.Service, transport)),
101+
)

0 commit comments

Comments
 (0)