Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion packages/core/lib/inferenceLogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,13 @@ function readSummaryFile<T>(inferenceType: string): Record<string, T[]> {
) {
return parsed;
}
} catch {
} catch (error) {
// If we fail to parse for any reason, fall back to empty array
// This is a debug utility file - using console.warn for visibility
console.warn(
`Failed to parse inference summary file ${summaryPath}:`,
error instanceof Error ? error.message : String(error),
);
}
return { [arrayKey]: [] };
}
2 changes: 2 additions & 0 deletions packages/core/lib/v3/agent/AgentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AgentType,
AgentExecutionOptions,
} from "../types/public/agent";
import { LogLine } from "../types/public/logs";

/**
* Abstract base class for agent clients
Expand All @@ -29,6 +30,7 @@ export abstract class AgentClient {
abstract execute(options: AgentExecutionOptions): Promise<AgentResult>;

abstract captureScreenshot(
logger: (message: LogLine) => void,
options?: Record<string, unknown>,
): Promise<unknown>;

Expand Down
89 changes: 74 additions & 15 deletions packages/core/lib/v3/agent/AnthropicCUAClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class AnthropicCUAClient extends AgentClient {
}> {
try {
// Get response from the model
const result = await this.getAction(inputItems);
const result = await this.getAction(inputItems, logger);
const content = result.content;
const usage = {
input_tokens: result.usage.input_tokens,
Expand Down Expand Up @@ -269,7 +269,7 @@ export class AnthropicCUAClient extends AgentClient {
});

// Convert tool use to action and add to actions list
const action = this.convertToolUseToAction(toolUseItem);
const action = this.convertToolUseToAction(toolUseItem, logger);
if (action) {
logger({
category: "agent",
Expand Down Expand Up @@ -397,7 +397,10 @@ export class AnthropicCUAClient extends AgentClient {
];
}

async getAction(inputItems: ResponseInputItem[]): Promise<{
async getAction(
inputItems: ResponseInputItem[],
logger: (message: LogLine) => void,
): Promise<{
content: AnthropicContentBlock[];
id: string;
usage: Record<string, number>;
Expand Down Expand Up @@ -501,7 +504,20 @@ export class AnthropicCUAClient extends AgentClient {
usage,
};
} catch (error) {
console.error("Error getting action from Anthropic:", error);
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error getting action from Anthropic: ${errorMessage}`,
level: 0,
auxiliary: {
error: { value: errorMessage, type: "string" },
stack: {
value: error instanceof Error ? error.stack || "" : "",
type: "string",
},
},
});
throw error;
}
}
Expand Down Expand Up @@ -540,7 +556,7 @@ export class AnthropicCUAClient extends AgentClient {
});

// Capture a screenshot for the response
const screenshot = await this.captureScreenshot();
const screenshot = await this.captureScreenshot(logger);
logger({
category: "agent",
message: `Screenshot captured, length: ${screenshot.length}`,
Expand Down Expand Up @@ -654,7 +670,7 @@ export class AnthropicCUAClient extends AgentClient {
try {
// For computer tool, try to capture a screenshot even on error
if (item.name === "computer") {
const screenshot = await this.captureScreenshot();
const screenshot = await this.captureScreenshot(logger);

toolResults.push({
type: "tool_result",
Expand Down Expand Up @@ -736,7 +752,10 @@ export class AnthropicCUAClient extends AgentClient {
return toolResults;
}

private convertToolUseToAction(item: ToolUseItem): AgentAction | null {
private convertToolUseToAction(
item: ToolUseItem,
logger: (message: LogLine) => void,
): AgentAction | null {
try {
const { name, input } = item;

Expand All @@ -745,7 +764,14 @@ export class AnthropicCUAClient extends AgentClient {
const action = input.action as string;

if (!action) {
console.warn("Missing action in tool use item:", item);
logger({
category: "agent",
message: "Missing action in tool use item",
level: 1,
auxiliary: {
item: { value: JSON.stringify(item), type: "object" },
},
});
return null;
}

Expand Down Expand Up @@ -901,18 +927,38 @@ export class AnthropicCUAClient extends AgentClient {
return null;
}

console.warn(`Unknown tool name: ${name}`);
logger({
category: "agent",
message: `Unknown tool name: ${name}`,
level: 1,
});
return null;
} catch (error) {
console.error("Error converting tool use to action:", error);
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error converting tool use to action: ${errorMessage}`,
level: 0,
auxiliary: {
error: { value: errorMessage, type: "string" },
stack: {
value: error instanceof Error ? error.stack || "" : "",
type: "string",
},
},
});
return null;
}
}

async captureScreenshot(options?: {
base64Image?: string;
currentUrl?: string;
}): Promise<string> {
async captureScreenshot(
logger: (message: LogLine) => void,
options?: {
base64Image?: string;
currentUrl?: string;
},
): Promise<string> {
// Use provided options if available
if (options?.base64Image) {
return `data:image/png;base64,${options.base64Image}`;
Expand All @@ -924,7 +970,20 @@ export class AnthropicCUAClient extends AgentClient {
const base64Image = await this.screenshotProvider();
return `data:image/png;base64,${base64Image}`;
} catch (error) {
console.error("Error capturing screenshot:", error);
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error capturing screenshot: ${errorMessage}`,
level: 0,
auxiliary: {
error: { value: errorMessage, type: "string" },
stack: {
value: error instanceof Error ? error.stack || "" : "",
type: "string",
},
},
});
throw error;
}
}
Expand Down
47 changes: 38 additions & 9 deletions packages/core/lib/v3/agent/GoogleCUAClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export class GoogleCUAClient extends AgentClient {
level: 2,
});

const screenshot = await this.captureScreenshot();
const screenshot = await this.captureScreenshot(logger);
const base64Data = screenshot.replace(
/^data:image\/png;base64,/,
"",
Expand Down Expand Up @@ -511,10 +511,15 @@ export class GoogleCUAClient extends AgentClient {
functionResponses.push(functionResponsePart);
}
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error capturing screenshot: ${error}`,
message: `Error capturing screenshot: ${errorMessage}`,
level: 0,
auxiliary: {
error: { value: errorMessage, type: "string" },
},
});
}
}
Expand Down Expand Up @@ -609,7 +614,10 @@ export class GoogleCUAClient extends AgentClient {
});

// Convert function call to action(s)
const action = this.convertFunctionCallToAction(part.functionCall);
const action = this.convertFunctionCallToAction(
part.functionCall,
logger,
);
if (action) {
// Special handling for type_text_at - we need to click first
if (
Expand Down Expand Up @@ -688,6 +696,7 @@ export class GoogleCUAClient extends AgentClient {
*/
private convertFunctionCallToAction(
functionCall: FunctionCall,
logger: (message: LogLine) => void,
): AgentAction | null {
const { name, args } = functionCall;

Expand Down Expand Up @@ -857,7 +866,11 @@ export class GoogleCUAClient extends AgentClient {
pageUrl: this.currentUrl,
};
}
console.warn(`Unsupported Google CUA function: ${name}`);
logger({
category: "agent",
message: `Unsupported Google CUA function: ${name}`,
level: 1,
});
return null;
}
}
Expand All @@ -874,10 +887,13 @@ export class GoogleCUAClient extends AgentClient {
};
}

async captureScreenshot(options?: {
base64Image?: string;
currentUrl?: string;
}): Promise<string> {
async captureScreenshot(
logger: (message: LogLine) => void,
options?: {
base64Image?: string;
currentUrl?: string;
},
): Promise<string> {
// Update current URL if provided
if (options?.currentUrl) {
this.currentUrl = options.currentUrl;
Expand All @@ -894,7 +910,20 @@ export class GoogleCUAClient extends AgentClient {
const base64Image = await this.screenshotProvider();
return `data:image/png;base64,${base64Image}`;
} catch (error) {
console.error("Error capturing screenshot:", error);
const errorMessage =
error instanceof Error ? error.message : String(error);
logger({
category: "agent",
message: `Error capturing screenshot: ${errorMessage}`,
level: 0,
auxiliary: {
error: { value: errorMessage, type: "string" },
stack: {
value: error instanceof Error ? error.stack || "" : "",
type: "string",
},
},
});
throw error;
}
}
Expand Down
Loading