fix: normalize OpenAI/GitHub finish_reason to Flynn stopReason conventions
OpenAI-compatible providers return 'stop' and 'tool_calls' as finish_reason values, but Flynn's agent loop expects Anthropic-style 'end_turn' and 'tool_use'. This caused the agent to exit the tool loop prematurely when falling back to GitHub Copilot (due to Anthropic API quota exhaustion). - openai.ts: Map 'stop' → 'end_turn', 'length' → 'max_tokens', tool_calls with actual tools → 'tool_use', tool_calls without tools → 'end_turn' - github.ts: Handle edge case where finish_reason is 'tool_calls' but no tools were parsed - agent.ts: Accept both 'tool_use' and 'tool_calls' as valid stop reasons (belt-and-suspenders), extract toolCalls to local variable for TS narrowing - openai.test.ts: Update expectations to match new normalized values
This commit is contained in:
@@ -174,8 +174,12 @@ export class NativeAgent {
|
||||
this._totalUsage.outputTokens += response.usage.outputTokens;
|
||||
this._callCount++;
|
||||
|
||||
// If the model didn't request tool use, we're done
|
||||
if (response.stopReason !== 'tool_use' || !response.toolCalls?.length) {
|
||||
// If the model didn't request tool use, we're done.
|
||||
// Check both 'tool_use' (Anthropic) and 'tool_calls' (OpenAI-compatible) stop reasons,
|
||||
// but always require actual toolCalls to be present.
|
||||
const wantsToolUse = (response.stopReason === 'tool_use' || response.stopReason === 'tool_calls')
|
||||
&& response.toolCalls && response.toolCalls.length > 0;
|
||||
if (!wantsToolUse) {
|
||||
let finalContent = response.content;
|
||||
if (response.thinkingContent) {
|
||||
finalContent = `<thinking>\n${response.thinkingContent}\n</thinking>\n\n${response.content}`;
|
||||
@@ -185,8 +189,11 @@ export class NativeAgent {
|
||||
return finalContent;
|
||||
}
|
||||
|
||||
// Safe to assert non-null — wantsToolUse guarantees toolCalls exists and is non-empty
|
||||
const toolCalls = response.toolCalls!;
|
||||
|
||||
// Check for repeated tool calls — build a fingerprint from tool names + args
|
||||
const fingerprint = response.toolCalls
|
||||
const fingerprint = toolCalls
|
||||
.map(tc => `${tc.name}:${JSON.stringify(tc.args)}`)
|
||||
.sort()
|
||||
.join('|');
|
||||
@@ -199,7 +206,7 @@ export class NativeAgent {
|
||||
}
|
||||
|
||||
// Track consecutive calls to the same tool (by name, ignoring args)
|
||||
const toolNames = response.toolCalls.map(tc => tc.name).sort().join(',');
|
||||
const toolNames = toolCalls.map(tc => tc.name).sort().join(',');
|
||||
if (toolNames === lastToolName) {
|
||||
sameToolStreak++;
|
||||
} else {
|
||||
@@ -213,7 +220,7 @@ export class NativeAgent {
|
||||
if (response.content) {
|
||||
assistantContent.push({ type: 'text', text: response.content });
|
||||
}
|
||||
for (const tc of response.toolCalls) {
|
||||
for (const tc of toolCalls) {
|
||||
assistantContent.push({
|
||||
type: 'tool_use',
|
||||
id: tc.id,
|
||||
@@ -226,7 +233,7 @@ export class NativeAgent {
|
||||
// Execute each tool call and collect results
|
||||
const toolResultBlocks: unknown[] = [];
|
||||
lastToolResults = [];
|
||||
for (const tc of response.toolCalls) {
|
||||
for (const tc of toolCalls) {
|
||||
const internalName = this.toolRegistry!.getByApiName(tc.name)?.name ?? tc.name;
|
||||
this.onToolUse?.({ type: 'start', tool: internalName, args: tc.args });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user