@kamel-ahmed/proxy-claude 1.0.0
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 +622 -0
- package/bin/cli.js +124 -0
- package/package.json +80 -0
- package/public/app.js +228 -0
- package/public/css/src/input.css +523 -0
- package/public/css/style.css +1 -0
- package/public/favicon.svg +10 -0
- package/public/index.html +381 -0
- package/public/js/components/account-manager.js +245 -0
- package/public/js/components/claude-config.js +420 -0
- package/public/js/components/dashboard/charts.js +589 -0
- package/public/js/components/dashboard/filters.js +362 -0
- package/public/js/components/dashboard/stats.js +110 -0
- package/public/js/components/dashboard.js +236 -0
- package/public/js/components/logs-viewer.js +100 -0
- package/public/js/components/models.js +36 -0
- package/public/js/components/server-config.js +349 -0
- package/public/js/config/constants.js +102 -0
- package/public/js/data-store.js +386 -0
- package/public/js/settings-store.js +58 -0
- package/public/js/store.js +78 -0
- package/public/js/translations/en.js +351 -0
- package/public/js/translations/id.js +396 -0
- package/public/js/translations/pt.js +287 -0
- package/public/js/translations/tr.js +342 -0
- package/public/js/translations/zh.js +357 -0
- package/public/js/utils/account-actions.js +189 -0
- package/public/js/utils/error-handler.js +96 -0
- package/public/js/utils/model-config.js +42 -0
- package/public/js/utils/validators.js +77 -0
- package/public/js/utils.js +69 -0
- package/public/views/accounts.html +329 -0
- package/public/views/dashboard.html +484 -0
- package/public/views/logs.html +97 -0
- package/public/views/models.html +331 -0
- package/public/views/settings.html +1329 -0
- package/src/account-manager/credentials.js +243 -0
- package/src/account-manager/index.js +380 -0
- package/src/account-manager/onboarding.js +117 -0
- package/src/account-manager/rate-limits.js +237 -0
- package/src/account-manager/storage.js +136 -0
- package/src/account-manager/strategies/base-strategy.js +104 -0
- package/src/account-manager/strategies/hybrid-strategy.js +195 -0
- package/src/account-manager/strategies/index.js +79 -0
- package/src/account-manager/strategies/round-robin-strategy.js +76 -0
- package/src/account-manager/strategies/sticky-strategy.js +138 -0
- package/src/account-manager/strategies/trackers/health-tracker.js +162 -0
- package/src/account-manager/strategies/trackers/index.js +8 -0
- package/src/account-manager/strategies/trackers/token-bucket-tracker.js +121 -0
- package/src/auth/database.js +169 -0
- package/src/auth/oauth.js +419 -0
- package/src/auth/token-extractor.js +117 -0
- package/src/cli/accounts.js +512 -0
- package/src/cli/refresh.js +201 -0
- package/src/cli/setup.js +338 -0
- package/src/cloudcode/index.js +29 -0
- package/src/cloudcode/message-handler.js +386 -0
- package/src/cloudcode/model-api.js +248 -0
- package/src/cloudcode/rate-limit-parser.js +181 -0
- package/src/cloudcode/request-builder.js +93 -0
- package/src/cloudcode/session-manager.js +47 -0
- package/src/cloudcode/sse-parser.js +121 -0
- package/src/cloudcode/sse-streamer.js +293 -0
- package/src/cloudcode/streaming-handler.js +492 -0
- package/src/config.js +107 -0
- package/src/constants.js +278 -0
- package/src/errors.js +238 -0
- package/src/fallback-config.js +29 -0
- package/src/format/content-converter.js +193 -0
- package/src/format/index.js +20 -0
- package/src/format/request-converter.js +248 -0
- package/src/format/response-converter.js +120 -0
- package/src/format/schema-sanitizer.js +673 -0
- package/src/format/signature-cache.js +88 -0
- package/src/format/thinking-utils.js +558 -0
- package/src/index.js +146 -0
- package/src/modules/usage-stats.js +205 -0
- package/src/server.js +861 -0
- package/src/utils/claude-config.js +245 -0
- package/src/utils/helpers.js +51 -0
- package/src/utils/logger.js +142 -0
- package/src/utils/native-module-helper.js +162 -0
- package/src/webui/index.js +707 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSE Streamer for Cloud Code
|
|
3
|
+
*
|
|
4
|
+
* Streams SSE events in real-time, converting Google format to Anthropic format.
|
|
5
|
+
* Handles thinking blocks, text blocks, and tool use blocks.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import crypto from 'crypto';
|
|
9
|
+
import { MIN_SIGNATURE_LENGTH, getModelFamily } from '../constants.js';
|
|
10
|
+
import { EmptyResponseError } from '../errors.js';
|
|
11
|
+
import { cacheSignature, cacheThinkingSignature } from '../format/signature-cache.js';
|
|
12
|
+
import { logger } from '../utils/logger.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Stream SSE response and yield Anthropic-format events
|
|
16
|
+
*
|
|
17
|
+
* @param {Response} response - The HTTP response with SSE body
|
|
18
|
+
* @param {string} originalModel - The original model name
|
|
19
|
+
* @yields {Object} Anthropic-format SSE events
|
|
20
|
+
*/
|
|
21
|
+
export async function* streamSSEResponse(response, originalModel) {
|
|
22
|
+
const messageId = `msg_${crypto.randomBytes(16).toString('hex')}`;
|
|
23
|
+
let hasEmittedStart = false;
|
|
24
|
+
let blockIndex = 0;
|
|
25
|
+
let currentBlockType = null;
|
|
26
|
+
let currentThinkingSignature = '';
|
|
27
|
+
let inputTokens = 0;
|
|
28
|
+
let outputTokens = 0;
|
|
29
|
+
let cacheReadTokens = 0;
|
|
30
|
+
let stopReason = null;
|
|
31
|
+
|
|
32
|
+
const reader = response.body.getReader();
|
|
33
|
+
const decoder = new TextDecoder();
|
|
34
|
+
let buffer = '';
|
|
35
|
+
|
|
36
|
+
while (true) {
|
|
37
|
+
const { done, value } = await reader.read();
|
|
38
|
+
if (done) break;
|
|
39
|
+
|
|
40
|
+
buffer += decoder.decode(value, { stream: true });
|
|
41
|
+
const lines = buffer.split('\n');
|
|
42
|
+
buffer = lines.pop() || '';
|
|
43
|
+
|
|
44
|
+
for (const line of lines) {
|
|
45
|
+
if (!line.startsWith('data:')) continue;
|
|
46
|
+
|
|
47
|
+
const jsonText = line.slice(5).trim();
|
|
48
|
+
if (!jsonText) continue;
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const data = JSON.parse(jsonText);
|
|
52
|
+
const innerResponse = data.response || data;
|
|
53
|
+
|
|
54
|
+
// Extract usage metadata (including cache tokens)
|
|
55
|
+
const usage = innerResponse.usageMetadata;
|
|
56
|
+
if (usage) {
|
|
57
|
+
inputTokens = usage.promptTokenCount || inputTokens;
|
|
58
|
+
outputTokens = usage.candidatesTokenCount || outputTokens;
|
|
59
|
+
cacheReadTokens = usage.cachedContentTokenCount || cacheReadTokens;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const candidates = innerResponse.candidates || [];
|
|
63
|
+
const firstCandidate = candidates[0] || {};
|
|
64
|
+
const content = firstCandidate.content || {};
|
|
65
|
+
const parts = content.parts || [];
|
|
66
|
+
|
|
67
|
+
// Emit message_start on first data
|
|
68
|
+
// Note: input_tokens = promptTokenCount - cachedContentTokenCount (Antigravity includes cached in total)
|
|
69
|
+
if (!hasEmittedStart && parts.length > 0) {
|
|
70
|
+
hasEmittedStart = true;
|
|
71
|
+
yield {
|
|
72
|
+
type: 'message_start',
|
|
73
|
+
message: {
|
|
74
|
+
id: messageId,
|
|
75
|
+
type: 'message',
|
|
76
|
+
role: 'assistant',
|
|
77
|
+
content: [],
|
|
78
|
+
model: originalModel,
|
|
79
|
+
stop_reason: null,
|
|
80
|
+
stop_sequence: null,
|
|
81
|
+
usage: {
|
|
82
|
+
input_tokens: inputTokens - cacheReadTokens,
|
|
83
|
+
output_tokens: 0,
|
|
84
|
+
cache_read_input_tokens: cacheReadTokens,
|
|
85
|
+
cache_creation_input_tokens: 0
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Process each part
|
|
92
|
+
for (const part of parts) {
|
|
93
|
+
if (part.thought === true) {
|
|
94
|
+
// Handle thinking block
|
|
95
|
+
const text = part.text || '';
|
|
96
|
+
const signature = part.thoughtSignature || '';
|
|
97
|
+
|
|
98
|
+
if (currentBlockType !== 'thinking') {
|
|
99
|
+
if (currentBlockType !== null) {
|
|
100
|
+
yield { type: 'content_block_stop', index: blockIndex };
|
|
101
|
+
blockIndex++;
|
|
102
|
+
}
|
|
103
|
+
currentBlockType = 'thinking';
|
|
104
|
+
currentThinkingSignature = '';
|
|
105
|
+
yield {
|
|
106
|
+
type: 'content_block_start',
|
|
107
|
+
index: blockIndex,
|
|
108
|
+
content_block: { type: 'thinking', thinking: '' }
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (signature && signature.length >= MIN_SIGNATURE_LENGTH) {
|
|
113
|
+
currentThinkingSignature = signature;
|
|
114
|
+
// Cache thinking signature with model family for cross-model compatibility
|
|
115
|
+
const modelFamily = getModelFamily(originalModel);
|
|
116
|
+
cacheThinkingSignature(signature, modelFamily);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
yield {
|
|
120
|
+
type: 'content_block_delta',
|
|
121
|
+
index: blockIndex,
|
|
122
|
+
delta: { type: 'thinking_delta', thinking: text }
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
} else if (part.text !== undefined) {
|
|
126
|
+
// Skip empty text parts (but preserve whitespace-only chunks for proper spacing)
|
|
127
|
+
if (part.text === '') {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Handle regular text
|
|
132
|
+
if (currentBlockType !== 'text') {
|
|
133
|
+
if (currentBlockType === 'thinking' && currentThinkingSignature) {
|
|
134
|
+
yield {
|
|
135
|
+
type: 'content_block_delta',
|
|
136
|
+
index: blockIndex,
|
|
137
|
+
delta: { type: 'signature_delta', signature: currentThinkingSignature }
|
|
138
|
+
};
|
|
139
|
+
currentThinkingSignature = '';
|
|
140
|
+
}
|
|
141
|
+
if (currentBlockType !== null) {
|
|
142
|
+
yield { type: 'content_block_stop', index: blockIndex };
|
|
143
|
+
blockIndex++;
|
|
144
|
+
}
|
|
145
|
+
currentBlockType = 'text';
|
|
146
|
+
yield {
|
|
147
|
+
type: 'content_block_start',
|
|
148
|
+
index: blockIndex,
|
|
149
|
+
content_block: { type: 'text', text: '' }
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
yield {
|
|
154
|
+
type: 'content_block_delta',
|
|
155
|
+
index: blockIndex,
|
|
156
|
+
delta: { type: 'text_delta', text: part.text }
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
} else if (part.functionCall) {
|
|
160
|
+
// Handle tool use
|
|
161
|
+
// For Gemini 3+, capture thoughtSignature from the functionCall part
|
|
162
|
+
// The signature is a sibling to functionCall, not inside it
|
|
163
|
+
const functionCallSignature = part.thoughtSignature || '';
|
|
164
|
+
|
|
165
|
+
if (currentBlockType === 'thinking' && currentThinkingSignature) {
|
|
166
|
+
yield {
|
|
167
|
+
type: 'content_block_delta',
|
|
168
|
+
index: blockIndex,
|
|
169
|
+
delta: { type: 'signature_delta', signature: currentThinkingSignature }
|
|
170
|
+
};
|
|
171
|
+
currentThinkingSignature = '';
|
|
172
|
+
}
|
|
173
|
+
if (currentBlockType !== null) {
|
|
174
|
+
yield { type: 'content_block_stop', index: blockIndex };
|
|
175
|
+
blockIndex++;
|
|
176
|
+
}
|
|
177
|
+
currentBlockType = 'tool_use';
|
|
178
|
+
stopReason = 'tool_use';
|
|
179
|
+
|
|
180
|
+
const toolId = part.functionCall.id || `toolu_${crypto.randomBytes(12).toString('hex')}`;
|
|
181
|
+
|
|
182
|
+
// For Gemini, include the thoughtSignature in the tool_use block
|
|
183
|
+
// so it can be sent back in subsequent requests
|
|
184
|
+
const toolUseBlock = {
|
|
185
|
+
type: 'tool_use',
|
|
186
|
+
id: toolId,
|
|
187
|
+
name: part.functionCall.name,
|
|
188
|
+
input: {}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// Store the signature in the tool_use block for later retrieval
|
|
192
|
+
if (functionCallSignature && functionCallSignature.length >= MIN_SIGNATURE_LENGTH) {
|
|
193
|
+
toolUseBlock.thoughtSignature = functionCallSignature;
|
|
194
|
+
// Cache for future requests (Claude Code may strip this field)
|
|
195
|
+
cacheSignature(toolId, functionCallSignature);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
yield {
|
|
199
|
+
type: 'content_block_start',
|
|
200
|
+
index: blockIndex,
|
|
201
|
+
content_block: toolUseBlock
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
yield {
|
|
205
|
+
type: 'content_block_delta',
|
|
206
|
+
index: blockIndex,
|
|
207
|
+
delta: {
|
|
208
|
+
type: 'input_json_delta',
|
|
209
|
+
partial_json: JSON.stringify(part.functionCall.args || {})
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
} else if (part.inlineData) {
|
|
213
|
+
// Handle image content from Google format
|
|
214
|
+
if (currentBlockType === 'thinking' && currentThinkingSignature) {
|
|
215
|
+
yield {
|
|
216
|
+
type: 'content_block_delta',
|
|
217
|
+
index: blockIndex,
|
|
218
|
+
delta: { type: 'signature_delta', signature: currentThinkingSignature }
|
|
219
|
+
};
|
|
220
|
+
currentThinkingSignature = '';
|
|
221
|
+
}
|
|
222
|
+
if (currentBlockType !== null) {
|
|
223
|
+
yield { type: 'content_block_stop', index: blockIndex };
|
|
224
|
+
blockIndex++;
|
|
225
|
+
}
|
|
226
|
+
currentBlockType = 'image';
|
|
227
|
+
|
|
228
|
+
// Emit image block as a complete block
|
|
229
|
+
yield {
|
|
230
|
+
type: 'content_block_start',
|
|
231
|
+
index: blockIndex,
|
|
232
|
+
content_block: {
|
|
233
|
+
type: 'image',
|
|
234
|
+
source: {
|
|
235
|
+
type: 'base64',
|
|
236
|
+
media_type: part.inlineData.mimeType,
|
|
237
|
+
data: part.inlineData.data
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
yield { type: 'content_block_stop', index: blockIndex };
|
|
243
|
+
blockIndex++;
|
|
244
|
+
currentBlockType = null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Check finish reason (only if not already set by tool_use)
|
|
249
|
+
if (firstCandidate.finishReason && !stopReason) {
|
|
250
|
+
if (firstCandidate.finishReason === 'MAX_TOKENS') {
|
|
251
|
+
stopReason = 'max_tokens';
|
|
252
|
+
} else if (firstCandidate.finishReason === 'STOP') {
|
|
253
|
+
stopReason = 'end_turn';
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
} catch (parseError) {
|
|
258
|
+
logger.warn('[CloudCode] SSE parse error:', parseError.message);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Handle no content received - throw error to trigger retry in streaming-handler
|
|
264
|
+
if (!hasEmittedStart) {
|
|
265
|
+
logger.warn('[CloudCode] No content parts received, throwing for retry');
|
|
266
|
+
throw new EmptyResponseError('No content parts received from API');
|
|
267
|
+
} else {
|
|
268
|
+
// Close any open block
|
|
269
|
+
if (currentBlockType !== null) {
|
|
270
|
+
if (currentBlockType === 'thinking' && currentThinkingSignature) {
|
|
271
|
+
yield {
|
|
272
|
+
type: 'content_block_delta',
|
|
273
|
+
index: blockIndex,
|
|
274
|
+
delta: { type: 'signature_delta', signature: currentThinkingSignature }
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
yield { type: 'content_block_stop', index: blockIndex };
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Emit message_delta and message_stop
|
|
282
|
+
yield {
|
|
283
|
+
type: 'message_delta',
|
|
284
|
+
delta: { stop_reason: stopReason || 'end_turn', stop_sequence: null },
|
|
285
|
+
usage: {
|
|
286
|
+
output_tokens: outputTokens,
|
|
287
|
+
cache_read_input_tokens: cacheReadTokens,
|
|
288
|
+
cache_creation_input_tokens: 0
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
yield { type: 'message_stop' };
|
|
293
|
+
}
|