Skip to content
Open
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
15 changes: 14 additions & 1 deletion src/models/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ async function generateTextWithModel(
params: GenerateTextParams & {
tools?: Record<string, Tool>;
toolChoice?: ToolChoice<Record<string, Tool>>;
onStepFinish?: (stepResult: any) => void | Promise<void>;
},
): Promise<string | ToolResponse> {
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;
Expand Down Expand Up @@ -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,
Expand All @@ -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));
}
}
};
}

Expand Down Expand Up @@ -137,6 +148,7 @@ export async function handleTextSmall(
params: GenerateTextParams & {
tools?: Record<string, Tool>;
toolChoice?: ToolChoice<Record<string, Tool>>;
onStepFinish?: (stepResult: any) => void | Promise<void>;
},
): Promise<string | ToolResponse> {
return generateTextWithModel(runtime, ModelType.TEXT_SMALL, params);
Expand All @@ -150,6 +162,7 @@ export async function handleTextLarge(
params: GenerateTextParams & {
tools?: Record<string, Tool>;
toolChoice?: ToolChoice<Record<string, Tool>>;
onStepFinish?: (stepResult: any) => void | Promise<void>;
},
): Promise<string | ToolResponse> {
return generateTextWithModel(runtime, ModelType.TEXT_LARGE, params);
Expand Down