diff --git a/src/models/text.ts b/src/models/text.ts index 921b5d9..bda1688 100644 --- a/src/models/text.ts +++ b/src/models/text.ts @@ -18,9 +18,10 @@ async function generateTextWithModel( params: GenerateTextParams & { tools?: Record; toolChoice?: ToolChoice>; + onStepFinish?: (stepResult: any) => void | Promise; }, ): Promise { - const { prompt, stopSequences = [], tools, toolChoice } = params; + const { prompt, stopSequences = [], tools, toolChoice, onStepFinish } = params; const temperature = params.temperature ?? 0.7; const frequencyPenalty = params.frequencyPenalty ?? 0.7; const presencePenalty = params.presencePenalty ?? 0.7; @@ -72,6 +73,7 @@ async function generateTextWithModel( if (tools) { (generateParams as any).onStepFinish = async (stepResult: any) => { + // Internal capture logic if (stepResult.toolCalls && stepResult.toolCalls.length > 0) { capturedToolCalls = [ ...capturedToolCalls, @@ -92,6 +94,15 @@ async function generateTextWithModel( capturedToolResults = [...capturedToolResults, ...toolResultsFromContent]; } } + + // Call user-provided callback if provided + if (onStepFinish) { + try { + await onStepFinish(stepResult); + } catch (error) { + logger.error('[OpenRouter] Error in onStepFinish callback:', error instanceof Error ? error.message : String(error)); + } + } }; } @@ -137,6 +148,7 @@ export async function handleTextSmall( params: GenerateTextParams & { tools?: Record; toolChoice?: ToolChoice>; + onStepFinish?: (stepResult: any) => void | Promise; }, ): Promise { return generateTextWithModel(runtime, ModelType.TEXT_SMALL, params); @@ -150,6 +162,7 @@ export async function handleTextLarge( params: GenerateTextParams & { tools?: Record; toolChoice?: ToolChoice>; + onStepFinish?: (stepResult: any) => void | Promise; }, ): Promise { return generateTextWithModel(runtime, ModelType.TEXT_LARGE, params);