-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVercelAIExecutor.ts
More file actions
191 lines (177 loc) · 5.92 KB
/
VercelAIExecutor.ts
File metadata and controls
191 lines (177 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Vercel AI SDK executor - handles agentic loop with tool calling
*/
import type {
Executor,
ExecutorConfig,
ExecutionResult,
ExecutionStep,
} from "./types.js";
import type { PromptBuilder } from "../PromptBuilder.js";
/**
* Create a Vercel AI SDK executor
* This is the primary executor that most users will use
*
* @param config - Executor configuration including model and options
* @returns Executor function
*
* @example
* ```typescript
* import { createVercelAIExecutor } from '@marrakesh/core/executors'
* import { openai } from '@ai-sdk/openai'
*
* const executor = createVercelAIExecutor({
* model: openai('gpt-4'),
* maxSteps: 5
* })
* ```
*/
export function createVercelAIExecutor(config: ExecutorConfig): Executor {
// Capture config in closure
const maxSteps = config.maxSteps ?? 5;
const timeout = config.timeout ?? 30000;
return async (
prompt: PromptBuilder,
input: string,
): Promise<ExecutionResult> => {
const steps: ExecutionStep[] = [];
try {
// Dynamically import generateText from AI SDK v5
const { generateText } = await import("ai");
// Convert prompt to Vercel AI format
const { messages, tools, responseFormat } = prompt.toVercelAI([
{ role: "user", content: input },
]);
// Get the tool descriptions from the tools object
const toolDescriptions = tools
? Object.values(tools).map(
(t: { description: string }) => t.description,
)
: [];
// Execute with tool calling loop using Promise.race for timeout
// Type assertions are needed because:
// 1. config.model is 'unknown' to support any AI SDK model
// 2. Tool call structure varies between AI SDK v5
const generateTextOptions = {
model: config.model as never,
messages,
tools,
maxSteps,
temperature: config.temperature,
maxTokens: config.maxTokens,
onStepFinish: (step: {
toolCalls?: unknown;
toolResults?: unknown;
text?: string;
}) => {
// Track each tool calling round
const toolCalls = step.toolCalls as unknown as Array<{
id?: string;
name?: string;
function?: { name?: string };
arguments?: unknown;
args?: unknown;
input?: unknown;
}>;
const toolResults = step.toolResults as unknown as Array<{
toolCallId: string;
result?: unknown;
output?: unknown;
}>;
steps.push({
stepNumber: steps.length + 1,
toolCalls: toolCalls?.map((tc, index) => {
// Use tool description as the display name
const toolName = toolDescriptions[index] ?? "unnamed";
return {
toolName,
input: tc.arguments ?? tc.args ?? tc.input,
output:
toolResults?.find((tr) => tr.toolCallId === tc.id)?.result ??
toolResults?.find((tr) => tr.toolCallId === tc.id)?.output,
};
}),
text: step.text,
});
},
} as Parameters<typeof generateText>[0];
const result = await Promise.race([
generateText(generateTextOptions),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Execution timeout")), timeout),
),
]);
// Extract final output
let output: unknown;
const resultObj = result as Record<string, unknown>;
if (responseFormat?.type === "json_schema") {
// Check if we expect structured output
// Try to parse JSON from the model's text response
if (resultObj.text && typeof resultObj.text === "string") {
const trimmedText = resultObj.text.trim();
if (trimmedText) {
try {
output = JSON.parse(trimmedText);
} catch (parseError) {
// If JSON parsing fails, return the raw text
output = trimmedText;
}
} else {
output = "";
}
} else {
// No text response, return empty string
output = "";
}
} else {
// Text output - but check if we have tool results instead
const resultObj = result as Record<string, unknown>;
if (resultObj.text) {
output = resultObj.text;
} else if (
resultObj.toolResults &&
Array.isArray(resultObj.toolResults) &&
resultObj.toolResults.length > 0
) {
// If we have tool results, use the last one as output
const lastToolResult = resultObj.toolResults[
resultObj.toolResults.length - 1
] as Record<string, unknown>;
output = lastToolResult.result || lastToolResult.output;
} else if (
resultObj.toolCalls &&
Array.isArray(resultObj.toolCalls) &&
resultObj.toolCalls.length > 0
) {
// If we have tool calls but no results, extract from steps
const lastStep = steps[steps.length - 1];
if (lastStep?.toolCalls && lastStep.toolCalls.length > 0) {
output = lastStep.toolCalls[lastStep.toolCalls.length - 1].output;
}
} else {
output = resultObj.text || "";
}
}
return {
output,
steps,
finishReason: (result as { finishReason: string })
.finishReason as ExecutionResult["finishReason"],
usage: (result as { usage?: unknown }).usage as
| ExecutionResult["usage"]
| undefined,
};
} catch (error) {
// Handle timeout or other errors
const errorMessage =
error instanceof Error ? error.message : String(error);
return {
output: null,
steps,
finishReason:
errorMessage === "Execution timeout" ? "timeout" : "error",
error: errorMessage,
};
}
};
}