修复若干bug,当前已经可以完美适配
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { randomBytes, randomUUID } from 'node:crypto';
|
||||
import { gzipSync } from 'node:zlib';
|
||||
import { loadApiKey, loadCliUserContextBlocks, loadSystemPrompt } from './direct-config';
|
||||
import { logDebug, logError, logInfo } from './direct-logger';
|
||||
import type { DirectUpstreamMessage, DirectUpstreamOptions, UpstreamChunk } from './direct-types';
|
||||
import { parseUpstreamChunk, type CanonicalEvent } from './direct-canonical';
|
||||
|
||||
@@ -45,6 +46,7 @@ export function buildDirectRequestBody(messages: DirectUpstreamMessage[], option
|
||||
model: options.model ?? defaultModel,
|
||||
messages,
|
||||
tools: options.tools,
|
||||
tool_choice: options.tool_choice,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
temperature: options.temperature ?? Number(process.env.CODEBUDDY_TEMPERATURE ?? 1),
|
||||
@@ -59,6 +61,15 @@ export async function* streamDirectCanonicalEvents(
|
||||
messages: DirectUpstreamMessage[],
|
||||
options: DirectUpstreamOptions = {},
|
||||
): AsyncGenerator<CanonicalEvent> {
|
||||
const startedAt = Date.now();
|
||||
logInfo('upstream request started', {
|
||||
endpoint,
|
||||
model: options.model ?? defaultModel,
|
||||
messages: messages.length,
|
||||
tools: options.tools?.length ?? 0,
|
||||
stream: true,
|
||||
});
|
||||
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: buildDirectHeaders(),
|
||||
@@ -66,13 +77,28 @@ export async function* streamDirectCanonicalEvents(
|
||||
});
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
|
||||
const responseText = await response.text();
|
||||
logError('upstream request failed', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
durationMs: Date.now() - startedAt,
|
||||
bodyPreview: responseText.slice(0, 1000),
|
||||
});
|
||||
throw new Error(`HTTP ${response.status}: ${responseText}`);
|
||||
}
|
||||
|
||||
logInfo('upstream stream opened', {
|
||||
status: response.status,
|
||||
durationMs: Date.now() - startedAt,
|
||||
});
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
let chunkCount = 0;
|
||||
let eventCount = 0;
|
||||
|
||||
for await (const chunk of response.body) {
|
||||
chunkCount += 1;
|
||||
buffer += decoder.decode(chunk, { stream: true });
|
||||
const lines = buffer.split(/\r?\n/);
|
||||
buffer = lines.pop() ?? '';
|
||||
@@ -84,11 +110,22 @@ export async function* streamDirectCanonicalEvents(
|
||||
try {
|
||||
const parsed = JSON.parse(data) as UpstreamChunk;
|
||||
for (const event of parseUpstreamChunk(parsed)) {
|
||||
eventCount += 1;
|
||||
yield event;
|
||||
}
|
||||
} catch {
|
||||
} catch (error) {
|
||||
logDebug('ignored malformed upstream stream data', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
dataPreview: data.slice(0, 300),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logInfo('upstream stream finished', {
|
||||
durationMs: Date.now() - startedAt,
|
||||
chunks: chunkCount,
|
||||
events: eventCount,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user