-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.js
More file actions
383 lines (344 loc) · 10 KB
/
Copy pathconverter.js
File metadata and controls
383 lines (344 loc) · 10 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
const { v4: uuidv4 } = require('uuid');
const config = require('./config');
/**
* Convert OpenAI messages array to a prompt string for the SDK
*
* OpenAI format: [{ role: "system"|"user"|"assistant", content: "..." }]
* SDK format: single prompt string
*/
function openaiMessagesToPrompt(messages) {
if (!Array.isArray(messages) || messages.length === 0) {
return '';
}
const parts = [];
for (const msg of messages) {
const role = msg.role || 'user';
let content = '';
if (typeof msg.content === 'string') {
content = msg.content;
} else if (Array.isArray(msg.content)) {
// Handle multi-part content (e.g., text + images)
const textParts = msg.content
.filter(part => part.type === 'text')
.map(part => part.text);
content = textParts.join('\n');
}
if (!content) continue;
switch (role) {
case 'system':
parts.push(`[System]\n${content}`);
break;
case 'user':
parts.push(`[User]\n${content}`);
break;
case 'assistant':
parts.push(`[Assistant]\n${content}`);
break;
default:
parts.push(`[${role}]\n${content}`);
}
}
return parts.join('\n\n');
}
/**
* Create an OpenAI-compatible streaming chunk from SDK stream_event
*/
function sdkStreamEventToOpenAIChunk(msg, model, completionId) {
if (!msg || msg.type !== 'stream_event' || !msg.event) {
return null;
}
const event = msg.event;
const delta = event.delta || {};
const contentBlock = event.content_block || {};
let content = null;
let qoderType = 'text';
let qoderTool = null;
// Handle text delta
if (delta.text) {
content = delta.text;
qoderType = 'text';
}
// Handle text content block start
else if (contentBlock.type === 'text' && contentBlock.text) {
content = contentBlock.text;
qoderType = 'text';
}
// Handle thinking content - include as special format
else if (delta.thinking) {
content = delta.thinking;
qoderType = 'thinking';
}
else if (contentBlock.type === 'thinking' && contentBlock.thinking) {
content = contentBlock.thinking;
qoderType = 'thinking';
}
// Handle tool_use content block start
else if (contentBlock.type === 'tool_use') {
qoderType = 'tool_use';
qoderTool = {
id: contentBlock.id || '',
type: 'tool_use',
name: contentBlock.name || '',
input: {},
};
content = null;
}
// Handle tool_use input delta
else if (delta.partial_json) {
qoderType = 'tool_use';
content = null;
}
if (content === null && qoderType !== 'tool_use') {
return null;
}
const chunk = {
id: completionId,
object: 'chat.completion.chunk',
created: Math.floor(Date.now() / 1000),
model: model,
choices: [{
index: 0,
delta: content !== null ? {
content: content,
} : {},
finish_reason: null,
}],
};
// Add custom _qoder_type field for all chunks
chunk._qoder_type = qoderType;
// Add _qoder_tool field for tool_use chunks
if (qoderTool) {
chunk._qoder_tool = qoderTool;
}
return chunk;
}
/**
* Create an OpenAI-compatible completion from SDK result message
*/
function sdkResultToOpenAICompletion(resultMsg, fullText, model, completionId) {
const usage = {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
};
if (resultMsg && resultMsg.usage) {
usage.prompt_tokens = resultMsg.usage.input_tokens || 0;
usage.completion_tokens = resultMsg.usage.output_tokens || 0;
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
}
// If we have no text from streaming, try to get it from the result
let content = fullText || '';
if (!content && resultMsg && resultMsg.result) {
content = resultMsg.result;
}
const finishReason = resultMsg && resultMsg.subtype === 'error_during_execution' ? 'error' : 'stop';
return {
id: completionId,
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: model,
choices: [{
index: 0,
message: {
role: 'assistant',
content: content,
},
finish_reason: finishReason,
}],
usage: usage,
};
}
/**
* Extract text content from SDK assistant message
*/
function extractTextFromAssistantMessage(msg) {
if (!msg || msg.type !== 'assistant' || !msg.message || !msg.message.content) {
return '';
}
const textParts = [];
for (const block of msg.message.content) {
if (block.type === 'text' && block.text) {
textParts.push(block.text);
}
// Skip tool_use and thinking content for the main response
}
return textParts.join('\n');
}
/**
* Extract thinking content from SDK assistant message
*/
function extractThinkingFromAssistantMessage(msg) {
if (!msg || msg.type !== 'assistant' || !msg.message || !msg.message.content) {
return '';
}
const thinkingParts = [];
for (const block of msg.message.content) {
if (block.type === 'thinking' && block.thinking) {
thinkingParts.push(block.thinking);
}
}
return thinkingParts.join('\n');
}
/**
* Extract tool_use content blocks from SDK assistant message
*/
function extractToolUseFromAssistantMessage(msg) {
if (!msg || msg.type !== 'assistant' || !msg.message || !msg.message.content) { return []; }
const toolUses = [];
for (const block of msg.message.content) {
if (block.type === 'tool_use') {
toolUses.push({ id: block.id || '', name: block.name || '', input: block.input || {} });
}
}
return toolUses;
}
function extractToolResultFromUserMessage(msg) {
if (!msg || msg.type !== 'user' || !msg.message || !msg.message.content) { return []; }
const results = [];
for (const block of msg.message.content) {
if (block.type === 'tool_result') {
let contentText = '';
if (typeof block.content === 'string') { contentText = block.content; }
else if (Array.isArray(block.content)) {
contentText = block.content.filter(c => c.type === 'text').map(c => c.text).join('\n');
}
if (!contentText && block.tool_use_result && block.tool_use_result.stdout) {
contentText = block.tool_use_result.stdout;
}
results.push({ tool_use_id: block.tool_use_id || '', content: contentText, is_error: block.is_error || false });
}
}
return results;
}
/**
* Generate a completion ID
*/
function generateCompletionId() {
return 'chatcmpl-' + uuidv4().replace(/-/g, '').substring(0, 24);
}
/**
* Create an OpenAI-compatible model list response
*/
function createModelListResponse() {
const models = Object.values(config.models);
return {
object: 'list',
data: models.map(m => ({
id: m.id,
object: 'model',
created: Math.floor(Date.now() / 1000),
owned_by: m.owned_by,
})),
};
}
/**
* Create an OpenAI-compatible model info response
*/
function createModelInfoResponse(modelId) {
const model = config.models[modelId];
if (!model) return null;
return {
id: model.id,
object: 'model',
created: Math.floor(Date.now() / 1000),
owned_by: model.owned_by,
permission: [{
id: 'modelperm-' + uuidv4().replace(/-/g, '').substring(0, 24),
object: 'model_permission',
created: Math.floor(Date.now() / 1000),
allow_create_engine: false,
allow_sampling: true,
allow_logprobs: false,
allow_search_indices: false,
allow_view: true,
allow_fine_tuning: false,
organization: '*',
group: null,
is_blocking: false,
}],
};
}
/**
* Detect Qoder upstream errors embedded in assistant text content.
* Returns { statusCode, code, message, raw } when the text matches a known
* Qoder API error pattern, or null if the text looks like normal content.
*
* Known patterns:
* - "Qoder API error: FORBIDDEN - {"code":"112",...}" -> 403 (subscription required, code 112)
* - "Qoder API error: UNAUTHORIZED ..." -> 401
* - "Qoder API error: ..." (generic) -> 502
*/
function detectQoderError(text) {
if (!text || typeof text !== 'string') return null;
const match = text.match(/Qoder API error:\s*(\w+)\s*-\s*(\{.*\})/);
if (!match) return null;
const errorType = match[1];
let innerCode = null;
let innerMessage = '';
try {
const inner = JSON.parse(match[2]);
innerCode = inner.code ? String(inner.code) : null;
if (inner.message) {
// message may itself be a JSON string
try {
const msgObj = JSON.parse(inner.message);
innerMessage = msgObj.pricingUrl ? 'Subscription required' : inner.message;
} catch {
innerMessage = inner.message;
}
}
} catch {
// not JSON, use raw
}
// Map to HTTP status
let statusCode = 502;
let httpCode = 'upstream_error';
if (errorType === 'FORBIDDEN' && innerCode === '112') {
// QoderWork server returns 403 FORBIDDEN with code 112 when the account
// has no valid subscription (even for the "free" qmodel_latest model).
// We mirror the server's actual 403 status instead of remapping to 402.
statusCode = 403;
httpCode = 'forbidden';
innerMessage = innerMessage || 'QoderWork subscription required to use this model';
} else if (errorType === 'UNAUTHORIZED') {
statusCode = 401;
httpCode = 'upstream_unauthorized';
innerMessage = innerMessage || 'QoderWork authentication required';
} else if (errorType === 'FORBIDDEN') {
statusCode = 403;
httpCode = 'upstream_forbidden';
}
return {
statusCode,
code: httpCode,
message: innerMessage || `Qoder API error: ${errorType}`,
raw: match[0],
};
}
/**
* Create SSE data line
*/
function formatSSE(data) {
return `data: ${JSON.stringify(data)}\n\n`;
}
/**
* Create SSE done signal
*/
function formatSSEDone() {
return 'data: [DONE]\n\n';
}
module.exports = {
openaiMessagesToPrompt,
sdkStreamEventToOpenAIChunk,
sdkResultToOpenAICompletion,
extractTextFromAssistantMessage,
extractThinkingFromAssistantMessage,
extractToolUseFromAssistantMessage,
extractToolResultFromUserMessage,
generateCompletionId,
createModelListResponse,
createModelInfoResponse,
formatSSE,
formatSSEDone,
detectQoderError,
};