-
Notifications
You must be signed in to change notification settings - Fork 615
/
Copy pathOpenAICUAClient.ts
616 lines (546 loc) · 17.9 KB
/
OpenAICUAClient.ts
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import OpenAI from "openai";
import { LogLine } from "../../types/log";
import {
AgentAction,
AgentResult,
AgentType,
AgentExecutionOptions,
ResponseInputItem,
ResponseItem,
ComputerCallItem,
FunctionCallItem,
} from "@/types/agent";
import { AgentClient } from "./AgentClient";
import { AgentScreenshotProviderError } from "@/types/stagehandErrors";
import * as fs from "fs";
import * as path from "path";
/**
* Client for OpenAI's Computer Use Assistant API
* This implementation uses the official OpenAI Responses API for Computer Use
*/
export class OpenAICUAClient extends AgentClient {
private apiKey: string;
private organization?: string;
private baseURL: string;
private client: OpenAI;
public lastResponseId?: string;
private currentViewport = { width: 1024, height: 768 };
private currentUrl?: string;
private screenshotProvider?: () => Promise<string>;
private actionHandler?: (action: AgentAction) => Promise<void>;
private reasoningItems: Map<string, ResponseItem> = new Map();
private environment: string = "browser"; // "browser", "mac", "windows", or "ubuntu"
constructor(
type: AgentType,
modelName: string,
userProvidedInstructions?: string,
clientOptions?: Record<string, unknown>,
) {
super(type, modelName, userProvidedInstructions);
// Process client options
this.apiKey =
(clientOptions?.apiKey as string) || process.env.OPENAI_API_KEY || "";
this.organization =
(clientOptions?.organization as string) || process.env.OPENAI_ORG;
// Get environment if specified
if (
clientOptions?.environment &&
typeof clientOptions.environment === "string"
) {
this.environment = clientOptions.environment;
}
// Store client options for reference
this.clientOptions = {
apiKey: this.apiKey,
};
// Initialize the OpenAI client
this.client = new OpenAI(this.clientOptions);
}
setViewport(width: number, height: number): void {
this.currentViewport = { width, height };
}
setCurrentUrl(url: string): void {
this.currentUrl = url;
}
setScreenshotProvider(provider: () => Promise<string>): void {
this.screenshotProvider = provider;
}
setActionHandler(handler: (action: AgentAction) => Promise<void>): void {
this.actionHandler = handler;
}
/**
* Execute a task with the OpenAI CUA
* This is the main entry point for the agent
* @implements AgentClient.execute
*/
async execute(executionOptions: AgentExecutionOptions): Promise<AgentResult> {
const { options, logger } = executionOptions;
const { instruction } = options;
const maxSteps = options.maxSteps || 10;
let currentStep = 0;
let completed = false;
const actions: AgentAction[] = [];
const messageList: string[] = [];
let finalMessage = "";
this.reasoningItems.clear(); // Clear any previous reasoning items
// Start with the initial instruction
let inputItems = this.createInitialInputItems(instruction);
let previousResponseId: string | undefined = undefined;
try {
// Execute steps until completion or max steps reached
while (!completed && currentStep < maxSteps) {
logger({
category: "agent",
message: `Executing step ${currentStep + 1}/${maxSteps}`,
level: 2,
});
const result = await this.executeStep(
inputItems,
previousResponseId,
logger,
);
// Add actions to the list
actions.push(...result.actions);
// Update completion status
completed = result.completed;
// Store the previous response ID for the next request
previousResponseId = result.responseId;
// Update the input items for the next step if we're continuing
if (!completed) {
inputItems = result.nextInputItems;
}
// Record any message for this step
if (result.message) {
messageList.push(result.message);
finalMessage = result.message;
}
// Increment step counter
currentStep++;
}
// Return the final result
return {
success: completed,
actions,
message: finalMessage,
completed,
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error executing agent task: ${errorMessage}`,
level: 0,
});
return {
success: false,
actions,
message: `Failed to execute task: ${errorMessage}`,
completed: false,
};
}
}
/**
* Execute a single step of the agent
* This coordinates the flow: Request → Get Action → Execute Action
*/
async executeStep(
inputItems: ResponseInputItem[],
previousResponseId: string | undefined,
logger: (message: LogLine) => void,
): Promise<{
actions: AgentAction[];
message: string;
completed: boolean;
nextInputItems: ResponseInputItem[];
responseId: string;
}> {
try {
// Get response from the model
const result = await this.getAction(inputItems, previousResponseId);
const output = result.output;
const responseId = result.responseId;
// Add any reasoning items to our map
for (const item of output) {
if (item.type === "reasoning") {
this.reasoningItems.set(item.id, item);
}
}
// Extract actions from the output
const stepActions: AgentAction[] = [];
for (const item of output) {
if (item.type === "computer_call" && this.isComputerCallItem(item)) {
const action = this.convertComputerCallToAction(item);
if (action) {
stepActions.push(action);
}
} else if (
item.type === "function_call" &&
this.isFunctionCallItem(item)
) {
const action = this.convertFunctionCallToAction(item);
if (action) {
stepActions.push(action);
}
}
}
// Extract message text
let message = "";
for (const item of output) {
if (item.type === "message") {
if (item.content && Array.isArray(item.content)) {
for (const content of item.content) {
if (content.type === "output_text" && content.text) {
message += content.text + "\n";
}
}
}
}
}
// Take actions and get results
const nextInputItems = await this.takeAction(output, logger);
// Check if completed
const completed =
output.length === 0 ||
output.every(
(item) => item.type === "message" || item.type === "reasoning",
);
return {
actions: stepActions,
message: message.trim(),
completed,
nextInputItems,
responseId,
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error executing step: ${errorMessage}`,
level: 0,
});
throw error;
}
}
private isComputerCallItem(item: ResponseItem): item is ComputerCallItem {
return (
item.type === "computer_call" &&
"call_id" in item &&
"action" in item &&
typeof item.action === "object"
);
}
private isFunctionCallItem(item: ResponseItem): item is FunctionCallItem {
return (
item.type === "function_call" &&
"call_id" in item &&
"name" in item &&
"arguments" in item
);
}
private createInitialInputItems(instruction: string): ResponseInputItem[] {
// For the initial request, we use a simple array with the user's instruction
return [
{
role: "system",
content: this.userProvidedInstructions,
},
{
role: "user",
content: instruction,
},
];
}
async getAction(
inputItems: ResponseInputItem[],
previousResponseId?: string,
): Promise<{
output: ResponseItem[];
responseId: string;
}> {
try {
// Create the request parameters
const requestParams: Record<string, unknown> = {
model: this.modelName,
tools: [
{
type: "computer_use_preview",
display_width: this.currentViewport.width,
display_height: this.currentViewport.height,
environment: this.environment,
},
],
input: inputItems,
truncation: "auto",
};
// Add previous_response_id if available
if (previousResponseId) {
requestParams.previous_response_id = previousResponseId;
}
// Create the response using the OpenAI Responses API
// @ts-expect-error - Force type to match what the OpenAI SDK expects
const response = await this.client.responses.create(requestParams);
// Store the response ID for future use
this.lastResponseId = response.id;
// Return the output and response ID
return {
output: response.output as unknown as ResponseItem[],
responseId: response.id,
};
} catch (error) {
console.error("Error getting action from OpenAI:", error);
throw error;
}
}
async takeAction(
output: ResponseItem[],
logger: (message: LogLine) => void,
): Promise<ResponseInputItem[]> {
const nextInputItems: ResponseInputItem[] = [];
// Add any computer calls to process
for (const item of output) {
if (item.type === "computer_call" && this.isComputerCallItem(item)) {
// Execute the action
try {
const action = this.convertComputerCallToAction(item);
if (action && this.actionHandler) {
await this.actionHandler(action);
}
// Capture a screenshot
const screenshot = await this.captureScreenshot();
// Create a computer_call_output for the next request
const outputItem = {
type: "computer_call_output" as const,
call_id: item.call_id,
output: {
type: "input_image" as const,
image_url: screenshot,
},
} as ResponseInputItem;
// Add current URL if available
if (this.currentUrl) {
const computerCallOutput = outputItem as {
type: "computer_call_output";
call_id: string;
output: {
type: "input_image";
image_url: string;
current_url?: string;
};
acknowledged_safety_checks?: Array<{
id: string;
code: string;
message: string;
}>;
};
computerCallOutput.output.current_url = this.currentUrl;
}
// Add any safety checks that need to be acknowledged
if (
item.pending_safety_checks &&
item.pending_safety_checks.length > 0
) {
const computerCallOutput = outputItem as {
type: "computer_call_output";
call_id: string;
output: {
type: "input_image";
image_url: string;
};
acknowledged_safety_checks?: Array<{
id: string;
code: string;
message: string;
}>;
};
computerCallOutput.acknowledged_safety_checks =
item.pending_safety_checks;
}
nextInputItems.push(outputItem);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error executing computer call: ${errorMessage}`,
level: 0,
});
try {
// Capture a screenshot even on error
const screenshot = await this.captureScreenshot();
const errorOutputItem = {
type: "computer_call_output" as const,
call_id: item.call_id,
output: {
type: "input_image" as const,
image_url: screenshot,
error: errorMessage,
},
} as ResponseInputItem;
// Add current URL if available
if (this.currentUrl) {
const computerCallOutput = errorOutputItem as {
type: "computer_call_output";
call_id: string;
output: {
type: "input_image";
image_url: string;
current_url?: string;
};
acknowledged_safety_checks?: Array<{
id: string;
code: string;
message: string;
}>;
};
computerCallOutput.output.current_url = this.currentUrl;
}
// Add any safety checks that need to be acknowledged
if (
item.pending_safety_checks &&
item.pending_safety_checks.length > 0
) {
const computerCallOutput = errorOutputItem as {
type: "computer_call_output";
call_id: string;
output: {
type: "input_image";
image_url: string;
};
acknowledged_safety_checks?: Array<{
id: string;
code: string;
message: string;
}>;
};
computerCallOutput.acknowledged_safety_checks =
item.pending_safety_checks;
}
nextInputItems.push(errorOutputItem);
} catch (screenshotError) {
// If we can't capture a screenshot, just send the error
logger({
category: "agent",
message: `Error capturing screenshot: ${String(screenshotError)}`,
level: 0,
});
// For error cases without a screenshot, we need to use a string output
nextInputItems.push({
type: "computer_call_output",
call_id: item.call_id,
output: `Error: ${errorMessage}`,
} as ResponseInputItem);
}
}
} else if (
item.type === "function_call" &&
this.isFunctionCallItem(item)
) {
// Execute the function
try {
const action = this.convertFunctionCallToAction(item);
if (action && this.actionHandler) {
await this.actionHandler(action);
}
// Add the result
nextInputItems.push({
type: "function_call_output",
call_id: item.call_id,
output: "success",
});
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error executing function call: ${errorMessage}`,
level: 0,
});
nextInputItems.push({
type: "function_call_output",
call_id: item.call_id,
output: `Error: ${errorMessage}`,
});
}
}
}
return nextInputItems;
}
private convertComputerCallToAction(
call: ComputerCallItem,
): AgentAction | null {
const { action } = call;
// Instead of wrapping the action in a params object, spread the action properties directly
// This ensures properties like x, y, button, etc. are directly accessible on the AgentAction
return {
type: action.type as string,
...action, // Spread all properties from the action
};
}
private convertFunctionCallToAction(
call: FunctionCallItem,
): AgentAction | null {
try {
const args = JSON.parse(call.arguments);
return {
type: call.name,
params: args,
};
} catch (error) {
console.error("Error parsing function call arguments:", error);
return null;
}
}
async captureScreenshot(options?: {
base64Image?: string;
currentUrl?: string;
}): Promise<string> {
let imageData = "";
// Use provided options if available
if (options?.base64Image) {
imageData = `data:image/png;base64,${options.base64Image}`;
}
// Use the screenshot provider if available
else if (this.screenshotProvider) {
try {
const base64Image = await this.screenshotProvider();
imageData = `data:image/png;base64,${base64Image}`;
} catch (error) {
console.error("Error capturing screenshot:", error);
throw error;
}
} else {
throw new AgentScreenshotProviderError(
"`screenshotProvider` has not been set. " +
"Please call `setScreenshotProvider()` with a valid function that returns a base64-encoded image",
);
}
// Save the screenshot to file if we have valid image data
if (imageData) {
try {
// Create screenshots directory if it doesn't exist
const screenshotsDir = path.resolve("screenshots");
if (!fs.existsSync(screenshotsDir)) {
fs.mkdirSync(screenshotsDir, { recursive: true });
}
// Generate filename with timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
const filename = path.join(
screenshotsDir,
`screenshot-${timestamp}.png`,
);
// Extract base64 data without the data URL prefix
const base64Data = imageData.replace(/^data:image\/png;base64,/, "");
// Write file
fs.writeFileSync(filename, base64Data, "base64");
console.log(`Screenshot saved to ${filename}`);
} catch (saveError) {
// Log error but don't affect the function's behavior
console.error("Error saving screenshot to file:", saveError);
// Intentionally not re-throwing the error to keep function working
}
}
return imageData;
}
}