Skip to content

Commit 8ce8c4f

Browse files
committed
fix: support NewAPI config test token limit
1 parent 95871fb commit 8ce8c4f

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

server/src/routes/config.test.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import assert from 'node:assert/strict';
2+
import type { AddressInfo } from 'node:net';
3+
import { mkdtempSync } from 'node:fs';
4+
import { tmpdir } from 'node:os';
5+
import { join } from 'node:path';
6+
import test from 'node:test';
7+
import Fastify from 'fastify';
8+
9+
process.env.DATA_DIR = mkdtempSync(join(tmpdir(), 'chatcrystal-config-route-test-'));
10+
11+
const { updateConfig } = await import('../config.js');
12+
const { configRoutes } = await import('./config.js');
13+
14+
test('config test uses an OpenAI-compatible max_output_tokens value accepted by NewAPI', async () => {
15+
const upstream = Fastify();
16+
let capturedMaxOutputTokens: unknown;
17+
18+
upstream.post('/v1/responses', async (req, reply) => {
19+
const body = req.body as { max_output_tokens?: number; model?: string };
20+
capturedMaxOutputTokens = body.max_output_tokens;
21+
22+
if (
23+
typeof body.max_output_tokens !== 'number' ||
24+
body.max_output_tokens < 16
25+
) {
26+
reply.status(400);
27+
return {
28+
error: {
29+
message: `Invalid 'max_output_tokens': integer below minimum value. Expected a value >= 16, but got ${body.max_output_tokens} instead.`,
30+
type: 'invalid_request_error',
31+
param: 'max_output_tokens',
32+
code: 'integer_below_minimum',
33+
},
34+
};
35+
}
36+
37+
return {
38+
id: 'resp_test',
39+
created_at: 1_700_000_000,
40+
model: body.model ?? 'gpt-5.4',
41+
output: [
42+
{
43+
type: 'message',
44+
id: 'msg_test',
45+
role: 'assistant',
46+
content: [{ type: 'output_text', text: 'OK', annotations: [] }],
47+
},
48+
],
49+
usage: { input_tokens: 1, output_tokens: 1 },
50+
};
51+
});
52+
53+
upstream.post('/v1/embeddings', async () => ({
54+
data: [{ embedding: [0.1, 0.2, 0.3] }],
55+
usage: { prompt_tokens: 1 },
56+
}));
57+
58+
await upstream.listen({ host: '127.0.0.1', port: 0 });
59+
const address = upstream.server.address() as AddressInfo;
60+
const baseURL = `http://127.0.0.1:${address.port}/v1`;
61+
62+
updateConfig({
63+
llm: {
64+
provider: 'custom',
65+
baseURL,
66+
apiKey: 'test-api-key',
67+
model: 'gpt-5.4',
68+
},
69+
embedding: {
70+
provider: 'custom',
71+
baseURL,
72+
apiKey: 'test-api-key',
73+
model: 'text-embedding-ada-002',
74+
},
75+
});
76+
77+
const app = Fastify();
78+
await app.register(configRoutes);
79+
80+
try {
81+
const response = await app.inject({
82+
method: 'POST',
83+
url: '/api/config/test',
84+
});
85+
const body = response.json();
86+
87+
assert.equal(response.statusCode, 200);
88+
assert.equal(body.data.llm.connected, true, body.data.llm.error);
89+
assert.equal(body.data.llm.response, 'OK');
90+
assert.equal(
91+
body.data.embedding.connected,
92+
true,
93+
body.data.embedding.error,
94+
);
95+
assert.equal(body.data.connected, true);
96+
assert.equal(typeof capturedMaxOutputTokens, 'number');
97+
assert.ok(capturedMaxOutputTokens >= 16);
98+
} finally {
99+
await app.close();
100+
await upstream.close();
101+
}
102+
});

server/src/routes/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { clearEmbeddingIndex } from "../services/embedding.js";
88
import { getLanguageModel } from "../services/llm.js";
99
import { getProvider, listProviders } from "../services/providers.js";
1010

11+
// OpenAI Responses-compatible gateways such as NewAPI reject lower values.
12+
const CONFIG_TEST_MAX_OUTPUT_TOKENS = 16;
13+
1114
export async function configRoutes(app: FastifyInstance) {
1215
// List available providers
1316
app.get("/api/providers", async () => {
@@ -149,7 +152,7 @@ export async function configRoutes(app: FastifyInstance) {
149152
const llmResult = await generateText({
150153
model,
151154
prompt: "Reply with exactly: OK",
152-
maxOutputTokens: 5,
155+
maxOutputTokens: CONFIG_TEST_MAX_OUTPUT_TOKENS,
153156
});
154157
result.llm = { connected: true, response: llmResult.text.trim() };
155158
} catch (err) {

0 commit comments

Comments
 (0)