-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_opencode_reasoning_worker.mjs
More file actions
192 lines (182 loc) · 5.5 KB
/
Copy pathtest_opencode_reasoning_worker.mjs
File metadata and controls
192 lines (182 loc) · 5.5 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
185
186
187
188
189
190
191
192
import assert from "node:assert/strict";
import test from "node:test";
import { loadWorkerModule } from "./helpers/load_cloudflare_worker.mjs";
const worker = (await loadWorkerModule()).default;
const OPENCODE_CHAT_URL =
"https://multillm-proxy.cserules.workers.dev/opencode/v1/chat/completions";
function makeContainerEnv(fetchImpl) {
let calls = 0;
return {
getCalls() {
return calls;
},
env: {
ADMIN_API_KEY: "admin-live-key",
OPENCODE_GO_API_KEY: "opencode-go-key",
OPENCODE_EDGE_FETCH: "false",
MULTILLM_PROXY_CONTAINER: {
getByName(name) {
assert.equal(name, "primary");
return {
async fetch(request) {
calls += 1;
return fetchImpl(request);
},
};
},
},
},
};
}
function streamedContent(body) {
return body
.split(/\r?\n/)
.filter((line) => line.startsWith("data: ") && line !== "data: [DONE]")
.flatMap((line) => {
const payload = JSON.parse(line.slice("data: ".length));
const content = payload?.choices?.[0]?.delta?.content;
return typeof content === "string" ? [content] : [];
})
.join("");
}
test("container-backed OpenCode chat emits one route-labelled reasoning block", async () => {
const reasoning = "The user wants a concise philosophical reply.";
const malformedContent = [
`<think>${reasoning}<think>Draft Holly's response.</think>\n\n`,
"Draft Holly's response.</think>\n\n",
"*Holly closes her notebook.*",
].join("");
const upstreamEvents = [
`data: ${JSON.stringify({
id: "chatcmpl-ox-reasoning",
model: "ox-alpha-free",
choices: [
{
index: 0,
delta: { reasoning_content: reasoning },
finish_reason: null,
},
],
})}\n\n`,
`data: ${JSON.stringify({
id: "chatcmpl-ox-reasoning",
model: "ox-alpha-free",
choices: [
{
index: 0,
delta: { content: malformedContent },
finish_reason: "stop",
},
],
})}\n\n`,
"data: [DONE]\n\n",
].join("");
const requestBody = JSON.stringify({
model: "ox-alpha-free",
messages: [{ role: "user", content: "Continue." }],
stream: true,
});
const fixture = makeContainerEnv(async (request) => {
assert.equal(request.url, OPENCODE_CHAT_URL);
assert.equal(await request.text(), requestBody);
return new Response(upstreamEvents, {
headers: { "Content-Type": "text/event-stream" },
});
});
const response = await worker.fetch(
new Request(OPENCODE_CHAT_URL, {
method: "POST",
headers: {
Authorization: "Bearer admin-live-key",
"Content-Type": "application/json",
Origin: "https://janitorai.com",
},
body: requestBody,
}),
fixture.env,
);
const content = streamedContent(await response.text());
assert.equal(response.status, 200);
assert.equal(response.headers.get("Access-Control-Allow-Origin"), "https://janitorai.com");
assert.equal(response.headers.get("X-MultiLLM-Provider"), "opencode");
assert.equal(response.headers.get("X-MultiLLM-Model"), "ox-alpha-free");
assert.equal(content.match(/<think>/g)?.length, 1);
assert.equal(content.match(/<\/think>/g)?.length, 1);
assert.equal(
content,
[
"<think>[provider: opencode | model: ox-alpha-free]\n",
`${reasoning}Draft Holly's response.`,
"</think>\n\n",
"*Holly closes her notebook.*",
].join(""),
);
assert.equal(fixture.getCalls(), 1);
});
test("container-backed non-stream OpenCode chat removes duplicate reasoning fields", async () => {
const reasoning = "Plan the next visible story beat.";
const requestBody = JSON.stringify({
model: "ox-alpha-free",
messages: [{ role: "user", content: "Continue." }],
stream: false,
});
const fixture = makeContainerEnv(async () =>
new Response(
JSON.stringify({
id: "chatcmpl-ox-json",
model: "ox-alpha-free",
choices: [
{
index: 0,
message: {
role: "assistant",
reasoning_content: reasoning,
content: [
`<think>${reasoning}</think>\n\n`,
`${reasoning}</think>\n\n`,
"*Holly raises one eyebrow.*",
].join(""),
},
finish_reason: "stop",
},
],
}),
{
headers: {
"Content-Type": "application/json",
ETag: '"upstream-body"',
},
},
),
);
const response = await worker.fetch(
new Request(OPENCODE_CHAT_URL, {
method: "POST",
headers: {
Authorization: "Bearer admin-live-key",
"Content-Type": "application/json",
},
body: requestBody,
}),
fixture.env,
);
const payload = await response.json();
const message = payload.choices[0].message;
assert.equal(response.status, 200);
assert.equal(response.headers.get("X-MultiLLM-Provider"), "opencode");
assert.equal(response.headers.get("X-MultiLLM-Model"), "ox-alpha-free");
assert.equal(response.headers.get("ETag"), null);
assert.equal("reasoning_content" in message, false);
assert.equal(message.content.match(/<think>/g)?.length, 1);
assert.equal(message.content.match(/<\/think>/g)?.length, 1);
assert.equal(
message.content,
[
"<think>[provider: opencode | model: ox-alpha-free]\n",
reasoning,
"</think>\n\n",
"*Holly raises one eyebrow.*",
].join(""),
);
assert.equal(fixture.getCalls(), 1);
});