Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 56 additions & 57 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,80 +17,79 @@ export async function sendChatRequest(ctx: seal.MsgContext, msg: seal.Message, a
const tools = ai.tool.getToolsInfo(msg.messageType);

const maxRetries = 3;
const bodyObject = parseBody(bodyTemplate, messages, tools, tool_choice);

const data = await withTimeout(() => fetchData(url, apiKey, bodyObject), timeout);
try {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
if (attempt > 1) {
logger.info(`第 ${attempt} 次重试请求...`);
}

if (attempt > 1) {
logger.info(`第 ${attempt} 次重试请求...`);
}
const data = await withTimeout(() => fetchData(url, apiKey, bodyObject), timeout);

const data = await fetchData(url, apiKey, bodyObject);
if (data.choices && data.choices.length > 0) {
AIManager.updateUsage(data.model, data.usage);

if (data.choices && data.choices.length > 0) {
AIManager.updateUsage(data.model, data.usage);
const message = data.choices[0].message;
const finish_reason = data.choices[0].finish_reason;

const message = data.choices[0].message;
const finish_reason = data.choices[0].finish_reason;
if (message.hasOwnProperty('reasoning_content')) {
logger.info(`思维链内容:`, message.reasoning_content);
}

if (message.hasOwnProperty('reasoning_content')) {
logger.info(`思维链内容:`, message.reasoning_content);
}
const reply = message.content || '';

const reply = message.content || '';
if (isTool) {
if (usePromptEngineering) {
const match = reply.match(/<function(?:_call)?>([\s\S]*)<\/function(?:_call)?>/);
if (match) {
await ai.context.addMessage(ctx, msg, ai, match[0], [], "assistant", '');

try {
await ToolManager.handlePromptToolCall(ctx, msg, ai, match[1]);
} catch (e) {
logger.error(`在handlePromptToolCall中出错:`, e.message);
return '';
}

if (isTool) {
if (usePromptEngineering) {
const match = reply.match(/<function(?:_call)?>([\s\S]*)<\/function(?:_call)?>/);
if (match) {
await ai.context.addMessage(ctx, msg, ai, match[0], [], "assistant", '');
try {
await ToolManager.handlePromptToolCall(ctx, msg, ai, match[1]);
} catch (e) {
logger.error(`在handlePromptToolCall中出错:`, e.message);
return '';
}
}
} else if (finish_reason === 'tool_calls' && message.tool_calls) {
await ai.context.addToolCallsMessage(message.tool_calls, ai);

let tool_choice = 'auto';
try {
await ToolManager.handlePromptToolCall(ctx, msg, ai, match[1]);
tool_choice = await ToolManager.handleToolCalls(ctx, msg, ai, message.tool_calls);
} catch (e) {
logger.error(`在handlePromptToolCall中出错:`, e.message);
logger.error(`在handleToolCalls中出错:`, e.message);
return '';
}

let tool_choice = 'auto';
try {
tool_choice = await ToolManager.handleToolCalls(ctx, msg, ai, message.tool_calls);
} catch (e) {
logger.error(`在handleToolCalls中出错:`, e.message);
return '';
}
}
}

return reply;
} else {
throw new Error(`服务器响应中没有choices或choices为空\n响应体:${JSON.stringify(data, null, 2)}`);
}
} catch (error) {
logger.error(`在sendChatRequest中出错(第 ${attempt}/${maxRetries} 次尝试):`, error);

// 检查是否应该重试
const shouldRetry = attempt < maxRetries && (
error.message?.includes('状态码:') ||
error.message?.includes('响应体为空') ||
error.message?.includes('EOF')
);

if (!shouldRetry) {
// 如果不应该重试,直接返回
return '';
return reply;
} else {
throw new Error(`服务器响应中没有choices或choices为空\n响应体:${JSON.stringify(data, null, 2)}`);
}
} catch (error) {
logger.error(`在sendChatRequest中出错(第 ${attempt}/${maxRetries} 次尝试):`, error);

// 检查是否应该重试
const shouldRetry = attempt < maxRetries && (
error.message?.includes('状态码:') ||
error.message?.includes('响应体为空') ||
error.message?.includes('EOF')
);

if (!shouldRetry) {
// 如果不应该重试,直接返回
return '';
}

// 等待一段时间后重试(指数退避)
const delay = Math.min(1000 * Math.pow(2, attempt - 1), 5000);
logger.info(`等待 ${delay}ms 后重试...`);
await new Promise(resolve => setTimeout(resolve, delay));
}

// 等待一段时间后重试(指数退避)
const delay = Math.min(1000 * Math.pow(2, attempt - 1), 5000);
logger.info(`等待 ${delay}ms 后重试...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
} catch (e) {
logger.error("在sendChatRequest中出错:", e.message);
Expand Down