chore: improve logging

This commit is contained in:
Simon
2026-03-05 16:54:41 +08:00
parent 8bc27414a4
commit 0dacbda9da
3 changed files with 10 additions and 12 deletions

View File

@@ -4,6 +4,8 @@ import * as z from 'zod'
import type { PageAgentTool } from '../tools' import type { PageAgentTool } from '../tools'
const log = console.log.bind(console, chalk.yellow('[autoFixer]'))
/** /**
* Normalize LLM response and fix common format issues. * Normalize LLM response and fix common format issues.
* *
@@ -34,7 +36,7 @@ export function normalizeResponse(response: any, tools?: Map<string, PageAgentTo
// case: sometimes the model only returns the action level // case: sometimes the model only returns the action level
if (toolCall.function.name && toolCall.function.name !== 'AgentOutput') { if (toolCall.function.name && toolCall.function.name !== 'AgentOutput') {
console.log(chalk.yellow(`[normalizeResponse] #1: fixing tool_call`)) log(`#1: fixing tool_call`)
resolvedArguments = { action: safeJsonParse(resolvedArguments) } resolvedArguments = { action: safeJsonParse(resolvedArguments) }
} }
} else { } else {
@@ -47,13 +49,13 @@ export function normalizeResponse(response: any, tools?: Map<string, PageAgentTo
// case: sometimes the content json includes upper level wrapper // case: sometimes the content json includes upper level wrapper
if (resolvedArguments?.name === 'AgentOutput') { if (resolvedArguments?.name === 'AgentOutput') {
console.log(chalk.yellow(`[normalizeResponse] #2: fixing tool_call`)) log(`#2: fixing tool_call`)
resolvedArguments = safeJsonParse(resolvedArguments.arguments) resolvedArguments = safeJsonParse(resolvedArguments.arguments)
} }
// case: sometimes even 2-levels of wrapping // case: sometimes even 2-levels of wrapping
if (resolvedArguments?.type === 'function') { if (resolvedArguments?.type === 'function') {
console.log(chalk.yellow(`[normalizeResponse] #3: fixing tool_call`)) log(`#3: fixing tool_call`)
resolvedArguments = safeJsonParse(resolvedArguments.function.arguments) resolvedArguments = safeJsonParse(resolvedArguments.function.arguments)
} }
@@ -66,7 +68,7 @@ export function normalizeResponse(response: any, tools?: Map<string, PageAgentTo
!resolvedArguments?.next_goal && !resolvedArguments?.next_goal &&
!resolvedArguments?.thinking !resolvedArguments?.thinking
) { ) {
console.log(chalk.yellow(`[normalizeResponse] #4: fixing tool_call`)) log(`#4: fixing tool_call`)
resolvedArguments = { action: safeJsonParse(resolvedArguments) } resolvedArguments = { action: safeJsonParse(resolvedArguments) }
} }
} else { } else {
@@ -90,7 +92,7 @@ export function normalizeResponse(response: any, tools?: Map<string, PageAgentTo
// fix incomplete formats // fix incomplete formats
if (!resolvedArguments.action) { if (!resolvedArguments.action) {
console.log(chalk.yellow(`[normalizeResponse] #5: fixing tool_call`)) log(`#5: fixing tool_call`)
resolvedArguments.action = { name: 'wait', input: { seconds: 1 } } resolvedArguments.action = { name: 'wait', input: { seconds: 1 } }
} }
@@ -149,9 +151,7 @@ function validateAction(action: any, tools: Map<string, PageAgentTool>): any {
(k) => !(schema.shape as Record<string, z.ZodType>)[k].safeParse(undefined).success (k) => !(schema.shape as Record<string, z.ZodType>)[k].safeParse(undefined).success
) )
if (requiredKey) { if (requiredKey) {
console.log( log(`coercing primitive action input for "${toolName}"`)
chalk.yellow(`[normalizeResponse] coercing primitive action input for "${toolName}"`)
)
value = { [requiredKey]: value } value = { [requiredKey]: value }
} }
} }

View File

@@ -1,6 +1,6 @@
import chalk from 'chalk' import chalk from 'chalk'
export { normalizeResponse } from './autoFixer' export * from './autoFixer'
export async function waitFor(seconds: number): Promise<void> { export async function waitFor(seconds: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, seconds * 1000)) await new Promise((resolve) => setTimeout(resolve, seconds * 1000))

View File

@@ -6,9 +6,7 @@ import * as z from 'zod'
import type { Tool } from './types' import type { Tool } from './types'
function debug(message: string) { const debug = console.debug.bind(console, chalk.gray('[LLM]'))
console.debug(chalk.gray('[LLM]'), message)
}
/** /**
* Convert Zod schema to OpenAI tool format * Convert Zod schema to OpenAI tool format