MCPcopy Create free account
hub / github.com/dwgx/WindsurfAPI / handler

Function handler

src/handlers/chat.js:3133–4044  ·  view source on GitHub ↗
(res)

Source from the content-addressed store, hash-verified

3131 'X-Accel-Buffering': 'no',
3132 },
3133 async handler(res) {
3134 const abortController = new AbortController();
3135 let unregisterSse = () => {};
3136 res.on('close', () => {
3137 if (!res.writableEnded) {
3138 log.info('Client disconnected mid-stream, aborting upstream');
3139 abortController.abort();
3140 }
3141 });
3142 const send = (data) => {
3143 if (!res.writableEnded) res.write(`data: ${JSON.stringify(data)}\n\n`);
3144 };
3145 unregisterSse = registerSseController({
3146 abort(reason) {
3147 send(chatStreamError(reason || 'server shutting down', 'server_error', 'server_shutdown'));
3148 if (!res.writableEnded) {
3149 res.write('data: [DONE]\n\n');
3150 res.end();
3151 }
3152 abortController.abort(reason);
3153 },
3154 });
3155
3156 // SSE heartbeat: keep the TCP/HTTP connection alive through any silent
3157 // period (LS warmup, Cascade "thinking", queue wait). `:` prefix is a
3158 // comment line per the SSE spec — clients ignore it, intermediaries see
3159 // bytes flowing, idle timers get reset.
3160 const heartbeat = setInterval(() => {
3161 if (!res.writableEnded) res.write(': ping\n\n');
3162 }, HEARTBEAT_MS);
3163 const stopHeartbeat = () => clearInterval(heartbeat);
3164 res.on('close', stopHeartbeat);
3165
3166 // ── Cache hit: replay stored response as a fake stream ──
3167 const cached = cacheGet(ckey);
3168 if (cached) {
3169 log.info(`Chat: cache HIT model=${model} flow=stream`);
3170 recordRequest(model, true, 0, null);
3171 try {
3172 send({ id, object: 'chat.completion.chunk', created, model,
3173 choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] });
3174 if (cached.thinking) {
3175 send({ id, object: 'chat.completion.chunk', created, model,
3176 choices: [{ index: 0, delta: { reasoning_content: cached.thinking }, finish_reason: null }] });
3177 }
3178 if (cached.text) {
3179 send({ id, object: 'chat.completion.chunk', created, model,
3180 choices: [{ index: 0, delta: { content: cached.text }, finish_reason: null }] });
3181 }
3182 send({ id, object: 'chat.completion.chunk', created, model,
3183 choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] });
3184 send({ id, object: 'chat.completion.chunk', created, model,
3185 choices: [], usage: cachedUsage(messages, cached.text) });
3186 if (!res.writableEnded) { res.write('data: [DONE]\n\n'); res.end(); }
3187 } finally {
3188 unregisterSse();
3189 stopHeartbeat();
3190 }

Callers

nothing calls this directly

Calls 15

feedMethod · 0.95
flushMethod · 0.95
registerSseControllerFunction · 0.90
cacheGetFunction · 0.90
recordRequestFunction · 0.90
getAccountListFunction · 0.90
isExperimentalEnabledFunction · 0.90
fingerprintBeforeFunction · 0.90
acquireAccountByKeyFunction · 0.90
getAccountAvailabilityFunction · 0.90
isAllRateLimitedFunction · 0.90

Tested by

no test coverage detected