Spaces:
Running
Running
File size: 12,973 Bytes
bd925df | 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 | /**
* SSE Streamer for Cloud Code
*
* Streams SSE events in real-time, converting Google format to Anthropic format.
* Handles thinking blocks, text blocks, and tool use blocks.
*/
import crypto from 'crypto';
import { MIN_SIGNATURE_LENGTH, getModelFamily } from '../constants.js';
import { EmptyResponseError } from '../errors.js';
import { cacheSignature, cacheThinkingSignature } from '../format/signature-cache.js';
import { logger } from '../utils/logger.js';
/**
* Stream SSE response and yield Anthropic-format events
*
* @param {Response} response - The HTTP response with SSE body
* @param {string} originalModel - The original model name
* @yields {Object} Anthropic-format SSE events
*/
export async function* streamSSEResponse(response, originalModel) {
const messageId = `msg_${crypto.randomBytes(16).toString('hex')}`;
let hasEmittedStart = false;
let blockIndex = 0;
let currentBlockType = null;
let currentThinkingSignature = '';
let inputTokens = 0;
let outputTokens = 0;
let cacheReadTokens = 0;
let stopReason = null;
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (!line.startsWith('data:')) continue;
const jsonText = line.slice(5).trim();
if (!jsonText) continue;
try {
const data = JSON.parse(jsonText);
const innerResponse = data.response || data;
// Extract usage metadata (including cache tokens)
const usage = innerResponse.usageMetadata;
if (usage) {
inputTokens = usage.promptTokenCount || inputTokens;
outputTokens = usage.candidatesTokenCount || outputTokens;
cacheReadTokens = usage.cachedContentTokenCount || cacheReadTokens;
}
const candidates = innerResponse.candidates || [];
const firstCandidate = candidates[0] || {};
const content = firstCandidate.content || {};
const parts = content.parts || [];
// Emit message_start on first data
// Note: input_tokens = promptTokenCount - cachedContentTokenCount (Antigravity includes cached in total)
if (!hasEmittedStart && parts.length > 0) {
hasEmittedStart = true;
yield {
type: 'message_start',
message: {
id: messageId,
type: 'message',
role: 'assistant',
content: [],
model: originalModel,
stop_reason: null,
stop_sequence: null,
usage: {
input_tokens: inputTokens - cacheReadTokens,
output_tokens: 0,
cache_read_input_tokens: cacheReadTokens,
cache_creation_input_tokens: 0
}
}
};
}
// Process each part
for (const part of parts) {
if (part.thought === true) {
// Handle thinking block
const text = part.text || '';
const signature = part.thoughtSignature || '';
if (currentBlockType !== 'thinking') {
if (currentBlockType !== null) {
yield { type: 'content_block_stop', index: blockIndex };
blockIndex++;
}
currentBlockType = 'thinking';
currentThinkingSignature = '';
yield {
type: 'content_block_start',
index: blockIndex,
content_block: { type: 'thinking', thinking: '' }
};
}
if (signature && signature.length >= MIN_SIGNATURE_LENGTH) {
currentThinkingSignature = signature;
// Cache thinking signature with model family for cross-model compatibility
const modelFamily = getModelFamily(originalModel);
cacheThinkingSignature(signature, modelFamily);
}
yield {
type: 'content_block_delta',
index: blockIndex,
delta: { type: 'thinking_delta', thinking: text }
};
} else if (part.text !== undefined) {
// Skip empty text parts (but preserve whitespace-only chunks for proper spacing)
if (part.text === '') {
continue;
}
// Handle regular text
if (currentBlockType !== 'text') {
if (currentBlockType === 'thinking' && currentThinkingSignature) {
yield {
type: 'content_block_delta',
index: blockIndex,
delta: { type: 'signature_delta', signature: currentThinkingSignature }
};
currentThinkingSignature = '';
}
if (currentBlockType !== null) {
yield { type: 'content_block_stop', index: blockIndex };
blockIndex++;
}
currentBlockType = 'text';
yield {
type: 'content_block_start',
index: blockIndex,
content_block: { type: 'text', text: '' }
};
}
yield {
type: 'content_block_delta',
index: blockIndex,
delta: { type: 'text_delta', text: part.text }
};
} else if (part.functionCall) {
// Handle tool use
// For Gemini 3+, capture thoughtSignature from the functionCall part
// The signature is a sibling to functionCall, not inside it
const functionCallSignature = part.thoughtSignature || '';
if (currentBlockType === 'thinking' && currentThinkingSignature) {
yield {
type: 'content_block_delta',
index: blockIndex,
delta: { type: 'signature_delta', signature: currentThinkingSignature }
};
currentThinkingSignature = '';
}
if (currentBlockType !== null) {
yield { type: 'content_block_stop', index: blockIndex };
blockIndex++;
}
currentBlockType = 'tool_use';
stopReason = 'tool_use';
const toolId = part.functionCall.id || `toolu_${crypto.randomBytes(12).toString('hex')}`;
// For Gemini, include the thoughtSignature in the tool_use block
// so it can be sent back in subsequent requests
const toolUseBlock = {
type: 'tool_use',
id: toolId,
name: part.functionCall.name,
input: {}
};
// Store the signature in the tool_use block for later retrieval
if (functionCallSignature && functionCallSignature.length >= MIN_SIGNATURE_LENGTH) {
toolUseBlock.thoughtSignature = functionCallSignature;
// Cache for future requests (Claude Code may strip this field)
cacheSignature(toolId, functionCallSignature);
}
yield {
type: 'content_block_start',
index: blockIndex,
content_block: toolUseBlock
};
yield {
type: 'content_block_delta',
index: blockIndex,
delta: {
type: 'input_json_delta',
partial_json: JSON.stringify(part.functionCall.args || {})
}
};
} else if (part.inlineData) {
// Handle image content from Google format
if (currentBlockType === 'thinking' && currentThinkingSignature) {
yield {
type: 'content_block_delta',
index: blockIndex,
delta: { type: 'signature_delta', signature: currentThinkingSignature }
};
currentThinkingSignature = '';
}
if (currentBlockType !== null) {
yield { type: 'content_block_stop', index: blockIndex };
blockIndex++;
}
currentBlockType = 'image';
// Emit image block as a complete block
yield {
type: 'content_block_start',
index: blockIndex,
content_block: {
type: 'image',
source: {
type: 'base64',
media_type: part.inlineData.mimeType,
data: part.inlineData.data
}
}
};
yield { type: 'content_block_stop', index: blockIndex };
blockIndex++;
currentBlockType = null;
}
}
// Check finish reason (only if not already set by tool_use)
if (firstCandidate.finishReason && !stopReason) {
if (firstCandidate.finishReason === 'MAX_TOKENS') {
stopReason = 'max_tokens';
} else if (firstCandidate.finishReason === 'STOP') {
stopReason = 'end_turn';
}
}
} catch (parseError) {
logger.warn('[CloudCode] SSE parse error:', parseError.message);
}
}
}
// Handle no content received - throw error to trigger retry in streaming-handler
if (!hasEmittedStart) {
logger.warn('[CloudCode] No content parts received, throwing for retry');
throw new EmptyResponseError('No content parts received from API');
} else {
// Close any open block
if (currentBlockType !== null) {
if (currentBlockType === 'thinking' && currentThinkingSignature) {
yield {
type: 'content_block_delta',
index: blockIndex,
delta: { type: 'signature_delta', signature: currentThinkingSignature }
};
}
yield { type: 'content_block_stop', index: blockIndex };
}
}
// Emit message_delta and message_stop
yield {
type: 'message_delta',
delta: { stop_reason: stopReason || 'end_turn', stop_sequence: null },
usage: {
output_tokens: outputTokens,
cache_read_input_tokens: cacheReadTokens,
cache_creation_input_tokens: 0
}
};
yield { type: 'message_stop' };
}
|