@pikoloo/codex-proxy 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +199 -0
- package/bin/cli.js +118 -0
- package/docs/ACCOUNTS.md +202 -0
- package/docs/API.md +289 -0
- package/docs/ARCHITECTURE.md +129 -0
- package/docs/CLAUDE_INTEGRATION.md +163 -0
- package/docs/OAUTH.md +85 -0
- package/docs/OPENCLAW.md +34 -0
- package/docs/legal.md +11 -0
- package/images/dashboard-screenshot.png +0 -0
- package/images/demo-screenshot.png +0 -0
- package/images/f757093f-507b-4453-994e-f8275f8b07a9.png +0 -0
- package/package.json +61 -0
- package/public/css/style.css +1502 -0
- package/public/index.html +827 -0
- package/public/js/app.js +601 -0
- package/src/account-manager.js +528 -0
- package/src/account-rotation/index.js +93 -0
- package/src/account-rotation/rate-limits.js +293 -0
- package/src/account-rotation/strategies/base-strategy.js +48 -0
- package/src/account-rotation/strategies/index.js +31 -0
- package/src/account-rotation/strategies/round-robin-strategy.js +42 -0
- package/src/account-rotation/strategies/sticky-strategy.js +97 -0
- package/src/claude-config.js +153 -0
- package/src/cli/accounts.js +557 -0
- package/src/direct-api.js +164 -0
- package/src/format-converter.js +420 -0
- package/src/index.js +46 -0
- package/src/kilo-api.js +68 -0
- package/src/kilo-format-converter.js +285 -0
- package/src/kilo-models.js +103 -0
- package/src/kilo-streamer.js +243 -0
- package/src/middleware/credentials.js +116 -0
- package/src/middleware/sse.js +96 -0
- package/src/model-api.js +189 -0
- package/src/model-mapper.js +157 -0
- package/src/oauth.js +666 -0
- package/src/response-streamer.js +409 -0
- package/src/routes/accounts-route.js +332 -0
- package/src/routes/api-routes.js +98 -0
- package/src/routes/chat-route.js +229 -0
- package/src/routes/claude-config-route.js +121 -0
- package/src/routes/logs-route.js +43 -0
- package/src/routes/messages-route.js +203 -0
- package/src/routes/models-route.js +119 -0
- package/src/routes/settings-route.js +143 -0
- package/src/security.js +142 -0
- package/src/server-settings.js +56 -0
- package/src/server.js +58 -0
- package/src/signature-cache.js +106 -0
- package/src/thinking-utils.js +312 -0
- package/src/utils/logger.js +156 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response Streamer
|
|
3
|
+
* Streams SSE events from OpenAI Responses API and converts to Anthropic format
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { generateMessageId, toAnthropicToolId } from './format-converter.js';
|
|
7
|
+
import { cacheSignature, cacheThinkingSignature, SIGNATURE_CONSTANTS } from './signature-cache.js';
|
|
8
|
+
|
|
9
|
+
const { MIN_SIGNATURE_LENGTH } = SIGNATURE_CONSTANTS;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Stream OpenAI Responses API SSE events and yield Anthropic-format events
|
|
13
|
+
*
|
|
14
|
+
* OpenAI Responses API event types:
|
|
15
|
+
* - response.created
|
|
16
|
+
* - response.in_progress
|
|
17
|
+
* - response.output_item.added (type: message, function_call, reasoning)
|
|
18
|
+
* - response.output_text.delta
|
|
19
|
+
* - response.function_call_arguments.delta
|
|
20
|
+
* - response.function_call_arguments.done
|
|
21
|
+
* - response.output_item.done
|
|
22
|
+
* - response.completed
|
|
23
|
+
*
|
|
24
|
+
* Anthropic event types:
|
|
25
|
+
* - message_start
|
|
26
|
+
* - content_block_start
|
|
27
|
+
* - content_block_delta
|
|
28
|
+
* - content_block_stop
|
|
29
|
+
* - message_delta
|
|
30
|
+
* - message_stop
|
|
31
|
+
*
|
|
32
|
+
* @param {Response} response - The HTTP response with SSE body
|
|
33
|
+
* @param {string} model - The model name
|
|
34
|
+
* @yields {Object} Anthropic-format SSE events
|
|
35
|
+
*/
|
|
36
|
+
export async function* streamResponsesAPI(response, model) {
|
|
37
|
+
const messageId = generateMessageId();
|
|
38
|
+
let hasEmittedStart = false;
|
|
39
|
+
let blockIndex = 0;
|
|
40
|
+
let currentBlockType = null;
|
|
41
|
+
let currentBlockId = null;
|
|
42
|
+
let currentToolName = null;
|
|
43
|
+
let currentThinkingSignature = '';
|
|
44
|
+
let inputTokens = 0;
|
|
45
|
+
let outputTokens = 0;
|
|
46
|
+
let stopReason = 'end_turn';
|
|
47
|
+
let pendingArguments = '';
|
|
48
|
+
let usage = { input_tokens: 0, output_tokens: 0 };
|
|
49
|
+
|
|
50
|
+
const reader = response.body.getReader();
|
|
51
|
+
const decoder = new TextDecoder();
|
|
52
|
+
let buffer = '';
|
|
53
|
+
|
|
54
|
+
while (true) {
|
|
55
|
+
const { done, value } = await reader.read();
|
|
56
|
+
if (done) break;
|
|
57
|
+
|
|
58
|
+
buffer += decoder.decode(value, { stream: true });
|
|
59
|
+
const lines = buffer.split('\n');
|
|
60
|
+
buffer = lines.pop() || '';
|
|
61
|
+
|
|
62
|
+
for (const line of lines) {
|
|
63
|
+
if (!line.startsWith('data:')) continue;
|
|
64
|
+
|
|
65
|
+
const jsonText = line.slice(5).trim();
|
|
66
|
+
if (!jsonText) continue;
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const event = JSON.parse(jsonText);
|
|
70
|
+
const eventType = event.type;
|
|
71
|
+
|
|
72
|
+
// Extract usage from completed response
|
|
73
|
+
if (eventType === 'response.completed' && event.response?.usage) {
|
|
74
|
+
inputTokens = event.response.usage.input_tokens || 0;
|
|
75
|
+
outputTokens = event.response.usage.output_tokens || 0;
|
|
76
|
+
usage = {
|
|
77
|
+
input_tokens: inputTokens,
|
|
78
|
+
output_tokens: outputTokens,
|
|
79
|
+
cache_read_input_tokens: event.response.usage.cache_read_input_tokens || 0
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Handle output item added
|
|
84
|
+
if (eventType === 'response.output_item.added') {
|
|
85
|
+
const item = event.item;
|
|
86
|
+
|
|
87
|
+
if (!hasEmittedStart) {
|
|
88
|
+
hasEmittedStart = true;
|
|
89
|
+
yield {
|
|
90
|
+
event: 'message_start',
|
|
91
|
+
data: {
|
|
92
|
+
type: 'message_start',
|
|
93
|
+
message: {
|
|
94
|
+
id: messageId,
|
|
95
|
+
type: 'message',
|
|
96
|
+
role: 'assistant',
|
|
97
|
+
model: model,
|
|
98
|
+
content: [],
|
|
99
|
+
stop_reason: null,
|
|
100
|
+
stop_sequence: null,
|
|
101
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Close previous block if any (with signature_delta for thinking)
|
|
108
|
+
if (currentBlockType !== null) {
|
|
109
|
+
// Emit signature_delta before closing thinking block
|
|
110
|
+
if (currentBlockType === 'thinking' && currentThinkingSignature) {
|
|
111
|
+
yield {
|
|
112
|
+
event: 'content_block_delta',
|
|
113
|
+
data: {
|
|
114
|
+
type: 'content_block_delta',
|
|
115
|
+
index: blockIndex,
|
|
116
|
+
delta: { type: 'signature_delta', signature: currentThinkingSignature }
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
currentThinkingSignature = '';
|
|
120
|
+
}
|
|
121
|
+
yield {
|
|
122
|
+
event: 'content_block_stop',
|
|
123
|
+
data: { type: 'content_block_stop', index: blockIndex }
|
|
124
|
+
};
|
|
125
|
+
blockIndex++;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Start new block based on item type
|
|
129
|
+
if (item.type === 'message') {
|
|
130
|
+
currentBlockType = 'text';
|
|
131
|
+
currentBlockId = item.id;
|
|
132
|
+
yield {
|
|
133
|
+
event: 'content_block_start',
|
|
134
|
+
data: {
|
|
135
|
+
type: 'content_block_start',
|
|
136
|
+
index: blockIndex,
|
|
137
|
+
content_block: { type: 'text', text: '' }
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
} else if (item.type === 'function_call') {
|
|
141
|
+
currentBlockType = 'tool_use';
|
|
142
|
+
// Convert OpenAI fc_ ID back to Anthropic ID
|
|
143
|
+
const openAIId = item.call_id || item.id;
|
|
144
|
+
currentBlockId = toAnthropicToolId(openAIId);
|
|
145
|
+
currentToolName = item.name;
|
|
146
|
+
stopReason = 'tool_use';
|
|
147
|
+
|
|
148
|
+
yield {
|
|
149
|
+
event: 'content_block_start',
|
|
150
|
+
data: {
|
|
151
|
+
type: 'content_block_start',
|
|
152
|
+
index: blockIndex,
|
|
153
|
+
content_block: {
|
|
154
|
+
type: 'tool_use',
|
|
155
|
+
id: currentBlockId,
|
|
156
|
+
name: item.name,
|
|
157
|
+
input: {}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
} else if (item.type === 'reasoning') {
|
|
162
|
+
currentBlockType = 'thinking';
|
|
163
|
+
currentBlockId = item.id;
|
|
164
|
+
currentThinkingSignature = '';
|
|
165
|
+
|
|
166
|
+
yield {
|
|
167
|
+
event: 'content_block_start',
|
|
168
|
+
data: {
|
|
169
|
+
type: 'content_block_start',
|
|
170
|
+
index: blockIndex,
|
|
171
|
+
content_block: { type: 'thinking', thinking: '' }
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Handle text delta
|
|
178
|
+
if (eventType === 'response.output_text.delta') {
|
|
179
|
+
const delta = event.delta;
|
|
180
|
+
if (delta) {
|
|
181
|
+
// If we're in a thinking block, treat text as thinking content
|
|
182
|
+
if (currentBlockType === 'thinking') {
|
|
183
|
+
yield {
|
|
184
|
+
event: 'content_block_delta',
|
|
185
|
+
data: {
|
|
186
|
+
type: 'content_block_delta',
|
|
187
|
+
index: blockIndex,
|
|
188
|
+
delta: { type: 'thinking_delta', thinking: delta }
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
} else if (currentBlockType === 'text') {
|
|
192
|
+
yield {
|
|
193
|
+
event: 'content_block_delta',
|
|
194
|
+
data: {
|
|
195
|
+
type: 'content_block_delta',
|
|
196
|
+
index: blockIndex,
|
|
197
|
+
delta: { type: 'text_delta', text: delta }
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Handle thinking/reasoning delta
|
|
205
|
+
if (eventType === 'response.reasoning.delta' || eventType === 'response.thinking.delta') {
|
|
206
|
+
const delta = event.delta || event.thinking;
|
|
207
|
+
if (delta && currentBlockType === 'thinking') {
|
|
208
|
+
// Check for signature in the event
|
|
209
|
+
if (event.signature && event.signature.length >= MIN_SIGNATURE_LENGTH) {
|
|
210
|
+
currentThinkingSignature = event.signature;
|
|
211
|
+
// Cache the signature with model family
|
|
212
|
+
cacheThinkingSignature(event.signature, 'openai');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
yield {
|
|
216
|
+
event: 'content_block_delta',
|
|
217
|
+
data: {
|
|
218
|
+
type: 'content_block_delta',
|
|
219
|
+
index: blockIndex,
|
|
220
|
+
delta: { type: 'thinking_delta', thinking: delta }
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Handle function call arguments delta
|
|
227
|
+
if (eventType === 'response.function_call_arguments.delta') {
|
|
228
|
+
const delta = event.delta;
|
|
229
|
+
if (delta && currentBlockType === 'tool_use') {
|
|
230
|
+
pendingArguments += delta;
|
|
231
|
+
yield {
|
|
232
|
+
event: 'content_block_delta',
|
|
233
|
+
data: {
|
|
234
|
+
type: 'content_block_delta',
|
|
235
|
+
index: blockIndex,
|
|
236
|
+
delta: { type: 'input_json_delta', partial_json: delta }
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Handle function call arguments done
|
|
243
|
+
if (eventType === 'response.function_call_arguments.done') {
|
|
244
|
+
// Check for signature on the tool call
|
|
245
|
+
if (event.signature && event.signature.length >= MIN_SIGNATURE_LENGTH && currentBlockId) {
|
|
246
|
+
cacheSignature(currentBlockId, event.signature);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Handle output item done - capture signature if present
|
|
251
|
+
if (eventType === 'response.output_item.done') {
|
|
252
|
+
const item = event.item;
|
|
253
|
+
if (item) {
|
|
254
|
+
// Capture thinking signature
|
|
255
|
+
if (item.type === 'reasoning' && item.signature) {
|
|
256
|
+
if (item.signature.length >= MIN_SIGNATURE_LENGTH) {
|
|
257
|
+
currentThinkingSignature = item.signature;
|
|
258
|
+
cacheThinkingSignature(item.signature, 'openai');
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// Capture tool signature
|
|
262
|
+
if (item.type === 'function_call' && item.signature && currentBlockId) {
|
|
263
|
+
cacheSignature(currentBlockId, item.signature);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
} catch (parseError) {
|
|
269
|
+
// Ignore parse errors for individual lines
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Handle no content received
|
|
275
|
+
if (!hasEmittedStart) {
|
|
276
|
+
hasEmittedStart = true;
|
|
277
|
+
yield {
|
|
278
|
+
event: 'message_start',
|
|
279
|
+
data: {
|
|
280
|
+
type: 'message_start',
|
|
281
|
+
message: {
|
|
282
|
+
id: messageId,
|
|
283
|
+
type: 'message',
|
|
284
|
+
role: 'assistant',
|
|
285
|
+
model: model,
|
|
286
|
+
content: [],
|
|
287
|
+
stop_reason: null,
|
|
288
|
+
stop_sequence: null,
|
|
289
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
// Emit empty text block
|
|
295
|
+
yield {
|
|
296
|
+
event: 'content_block_start',
|
|
297
|
+
data: {
|
|
298
|
+
type: 'content_block_start',
|
|
299
|
+
index: 0,
|
|
300
|
+
content_block: { type: 'text', text: '' }
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
yield {
|
|
305
|
+
event: 'content_block_delta',
|
|
306
|
+
data: {
|
|
307
|
+
type: 'content_block_delta',
|
|
308
|
+
index: 0,
|
|
309
|
+
delta: { type: 'text_delta', text: '' }
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
yield {
|
|
314
|
+
event: 'content_block_stop',
|
|
315
|
+
data: { type: 'content_block_stop', index: 0 }
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
blockIndex = 1;
|
|
319
|
+
currentBlockType = null;
|
|
320
|
+
} else if (currentBlockType !== null) {
|
|
321
|
+
// Close any open block with signature_delta if thinking
|
|
322
|
+
if (currentBlockType === 'thinking' && currentThinkingSignature) {
|
|
323
|
+
yield {
|
|
324
|
+
event: 'content_block_delta',
|
|
325
|
+
data: {
|
|
326
|
+
type: 'content_block_delta',
|
|
327
|
+
index: blockIndex,
|
|
328
|
+
delta: { type: 'signature_delta', signature: currentThinkingSignature }
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
yield {
|
|
333
|
+
event: 'content_block_stop',
|
|
334
|
+
data: { type: 'content_block_stop', index: blockIndex }
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Emit message_delta with final usage
|
|
339
|
+
yield {
|
|
340
|
+
event: 'message_delta',
|
|
341
|
+
data: {
|
|
342
|
+
type: 'message_delta',
|
|
343
|
+
delta: { stop_reason: stopReason, stop_sequence: null },
|
|
344
|
+
usage: usage
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
// Emit message_stop
|
|
349
|
+
yield {
|
|
350
|
+
event: 'message_stop',
|
|
351
|
+
data: { type: 'message_stop' }
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Parse SSE events from OpenAI Responses API (non-streaming)
|
|
357
|
+
* Returns the final response object
|
|
358
|
+
*
|
|
359
|
+
* @param {Response} response - The HTTP response with SSE body
|
|
360
|
+
* @returns {Object} The parsed response object
|
|
361
|
+
*/
|
|
362
|
+
export async function parseResponsesAPIResponse(response) {
|
|
363
|
+
const reader = response.body.getReader();
|
|
364
|
+
const decoder = new TextDecoder();
|
|
365
|
+
let buffer = '';
|
|
366
|
+
let finalResponse = null;
|
|
367
|
+
|
|
368
|
+
while (true) {
|
|
369
|
+
const { done, value } = await reader.read();
|
|
370
|
+
if (done) break;
|
|
371
|
+
|
|
372
|
+
buffer += decoder.decode(value, { stream: true });
|
|
373
|
+
const lines = buffer.split('\n');
|
|
374
|
+
buffer = lines.pop() || '';
|
|
375
|
+
|
|
376
|
+
for (const line of lines) {
|
|
377
|
+
if (!line.startsWith('data:')) continue;
|
|
378
|
+
|
|
379
|
+
const jsonText = line.slice(5).trim();
|
|
380
|
+
if (!jsonText) continue;
|
|
381
|
+
|
|
382
|
+
try {
|
|
383
|
+
const event = JSON.parse(jsonText);
|
|
384
|
+
|
|
385
|
+
// Capture the completed response
|
|
386
|
+
if (event.type === 'response.completed') {
|
|
387
|
+
finalResponse = event.response;
|
|
388
|
+
}
|
|
389
|
+
} catch (parseError) {
|
|
390
|
+
// Ignore parse errors
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return finalResponse;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Format Anthropic SSE event for HTTP response
|
|
400
|
+
*/
|
|
401
|
+
export function formatSSEEvent(event) {
|
|
402
|
+
return `event: ${event.event}\ndata: ${JSON.stringify(event.data)}\n\n`;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export default {
|
|
406
|
+
streamResponsesAPI,
|
|
407
|
+
parseResponsesAPIResponse,
|
|
408
|
+
formatSSEEvent
|
|
409
|
+
};
|