feat(core): add rawRequest in history for debug

This commit is contained in:
Simon
2026-01-28 14:30:04 +08:00
parent c80de93d3b
commit f6394a04b1
5 changed files with 30 additions and 23 deletions

View File

@@ -227,24 +227,21 @@ export class PageAgentCore extends EventTarget {
console.log(chalk.blue('Thinking...')) console.log(chalk.blue('Thinking...'))
this.emitActivity({ type: 'thinking' }) this.emitActivity({ type: 'thinking' })
const result = await this.#llm.invoke( // invoke LLM
[
{ const messages = [
role: 'system', { role: 'system' as const, content: this.#getSystemPrompt() },
content: this.#getSystemPrompt(), { role: 'user' as const, content: await this.#assembleUserPrompt() },
}, ]
{
role: 'user', const tools = { AgentOutput: this.#packMacroTool() }
content: await this.#assembleUserPrompt(),
}, const result = await this.#llm.invoke(messages, tools, this.#abortController.signal, {
], toolChoiceName: 'AgentOutput',
{ AgentOutput: this.#packMacroTool() }, normalizeResponse,
this.#abortController.signal, })
{
toolChoiceName: 'AgentOutput', // assemble history event
normalizeResponse,
}
)
const macroResult = result.toolResult as MacroToolResult const macroResult = result.toolResult as MacroToolResult
const input = macroResult.input const input = macroResult.input
@@ -268,9 +265,12 @@ export class PageAgentCore extends EventTarget {
action, action,
usage: result.usage, usage: result.usage,
rawResponse: result.rawResponse, rawResponse: result.rawResponse,
rawRequest: result.rawRequest,
} as AgentStepEvent) } as AgentStepEvent)
this.#emitHistoryChange() this.#emitHistoryChange()
//
console.log(chalk.green('Step finished:'), actionName) console.log(chalk.green('Step finished:'), actionName)
console.groupEnd() console.groupEnd()
@@ -278,7 +278,7 @@ export class PageAgentCore extends EventTarget {
step++ step++
if (step > this.config.maxSteps) { if (step > this.config.maxSteps) {
this.#onDone('Step count exceeded maximum limit', false) this.#onDone(false)
const result: ExecutionResult = { const result: ExecutionResult = {
success: false, success: false,
data: 'Step count exceeded maximum limit', data: 'Step count exceeded maximum limit',
@@ -291,7 +291,7 @@ export class PageAgentCore extends EventTarget {
const success = action.input?.success ?? false const success = action.input?.success ?? false
const text = action.input?.text || 'no text provided' const text = action.input?.text || 'no text provided'
console.log(chalk.green.bold('Task completed'), success, text) console.log(chalk.green.bold('Task completed'), success, text)
this.#onDone(text, success) this.#onDone(success)
const result: ExecutionResult = { const result: ExecutionResult = {
success, success,
data: text, data: text,
@@ -305,7 +305,7 @@ export class PageAgentCore extends EventTarget {
console.error('Task failed', error) console.error('Task failed', error)
const errorMessage = String(error) const errorMessage = String(error)
this.emitActivity({ type: 'error', message: errorMessage }) this.emitActivity({ type: 'error', message: errorMessage })
this.#onDone(errorMessage, false) this.#onDone(false)
const result: ExecutionResult = { const result: ExecutionResult = {
success: false, success: false,
data: errorMessage, data: errorMessage,
@@ -556,7 +556,7 @@ export class PageAgentCore extends EventTarget {
return trimLines(prompt) return trimLines(prompt)
} }
#onDone(text: string, success = true) { #onDone(success = true) {
this.pageController.cleanUpHighlights() this.pageController.cleanUpHighlights()
this.pageController.hideMask() // No await - fire and forget this.pageController.hideMask() // No await - fire and forget
this.#setStatus(success ? 'completed' : 'error') this.#setStatus(success ? 'completed' : 'error')

View File

@@ -51,6 +51,8 @@ export interface AgentStepEvent {
} }
/** Raw LLM response for debugging */ /** Raw LLM response for debugging */
rawResponse?: unknown rawResponse?: unknown
/** Raw LLM request for debugging */
rawRequest?: unknown
} }
/** /**

View File

@@ -39,6 +39,8 @@ export class OpenAIClient implements LLMClient {
: 'required', : 'required',
} }
modelPatch(requestBody)
// 2. Call API // 2. Call API
let response: Response let response: Response
try { try {
@@ -48,7 +50,7 @@ export class OpenAIClient implements LLMClient {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: `Bearer ${this.config.apiKey}`, Authorization: `Bearer ${this.config.apiKey}`,
}, },
body: JSON.stringify(modelPatch(requestBody)), body: JSON.stringify(requestBody),
signal: abortSignal, signal: abortSignal,
}) })
} catch (error: unknown) { } catch (error: unknown) {
@@ -216,6 +218,7 @@ export class OpenAIClient implements LLMClient {
reasoningTokens: data.usage?.completion_tokens_details?.reasoning_tokens, reasoningTokens: data.usage?.completion_tokens_details?.reasoning_tokens,
}, },
rawResponse: data, rawResponse: data,
rawRequest: requestBody,
} }
} }
} }

View File

@@ -81,6 +81,7 @@ export interface InvokeResult<TResult = unknown> {
reasoningTokens?: number // OpenAI o1 series reasoning tokens reasoningTokens?: number // OpenAI o1 series reasoning tokens
} }
rawResponse?: unknown // Raw response for debugging rawResponse?: unknown // Raw response for debugging
rawRequest?: unknown // Raw request for debugging
} }
/** /**

View File

@@ -27,6 +27,7 @@ export function zodToOpenAITool(name: string, tool: Tool) {
/** /**
* Patch model specific parameters * Patch model specific parameters
* @note in-place modification
*/ */
export function modelPatch(body: Record<string, any>) { export function modelPatch(body: Record<string, any>) {
const model: string = body.model || '' const model: string = body.model || ''