fix: generate random tool call id when serving API does not have one

This commit is contained in:
koalazf.99
2025-08-14 19:09:02 +08:00
parent 41500814b0
commit f9d3fe6fad

View File

@@ -351,6 +351,7 @@ export class OpenAIContentGenerator implements ContentGenerator {
); );
} }
// console.log('createParams', createParams); // console.log('createParams', createParams);
const stream = (await this.client.chat.completions.create( const stream = (await this.client.chat.completions.create(
@@ -587,7 +588,7 @@ export class OpenAIContentGenerator implements ContentGenerator {
// Add combined text if any // Add combined text if any
if (combinedText) { if (combinedText) {
combinedParts.push({ text: combinedText }); combinedParts.push({ text: combinedText.trimEnd() });
} }
// Add function calls // Add function calls
@@ -1161,8 +1162,12 @@ export class OpenAIContentGenerator implements ContentGenerator {
// Handle text content // Handle text content
if (choice.message.content) { if (choice.message.content) {
if (typeof choice.message.content === 'string') {
parts.push({ text: choice.message.content.trimEnd() });
} else {
parts.push({ text: choice.message.content }); parts.push({ text: choice.message.content });
} }
}
// Handle tool calls // Handle tool calls
if (choice.message.tool_calls) { if (choice.message.tool_calls) {
@@ -1251,8 +1256,12 @@ export class OpenAIContentGenerator implements ContentGenerator {
// Handle text content // Handle text content
if (choice.delta?.content) { if (choice.delta?.content) {
if (typeof choice.delta.content === 'string') {
parts.push({ text: choice.delta.content.trimEnd() });
} else {
parts.push({ text: choice.delta.content }); parts.push({ text: choice.delta.content });
} }
}
// Handle tool calls - only accumulate during streaming, emit when complete // Handle tool calls - only accumulate during streaming, emit when complete
if (choice.delta?.tool_calls) { if (choice.delta?.tool_calls) {
@@ -1299,7 +1308,7 @@ export class OpenAIContentGenerator implements ContentGenerator {
parts.push({ parts.push({
functionCall: { functionCall: {
id: accumulatedCall.id, id: accumulatedCall.id || `call_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`,
name: accumulatedCall.name, name: accumulatedCall.name,
args, args,
}, },
@@ -1775,7 +1784,7 @@ export class OpenAIContentGenerator implements ContentGenerator {
} }
} }
messageContent = textParts.join(''); messageContent = textParts.join('').trimEnd();
} }
const choice: OpenAIChoice = { const choice: OpenAIChoice = {