|
|
import { AgentStep, AgentTrace, AgentTraceMetadata, FinalStep } from '@/types/agent'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const extractFinalAnswer = (steps: AgentStep[]): string | null => { |
|
|
if (!steps || steps.length === 0) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
for (let i = steps.length - 1; i >= 0; i--) { |
|
|
const step = steps[i]; |
|
|
|
|
|
if (step.actions && Array.isArray(step.actions)) { |
|
|
const finalAnswerAction = step.actions.find( |
|
|
(action) => action.function_name === 'final_answer' |
|
|
); |
|
|
|
|
|
if (finalAnswerAction && finalAnswerAction.parameters?.answer) { |
|
|
return String(finalAnswerAction.parameters.answer); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const lastStep = steps[steps.length - 1]; |
|
|
return lastStep?.thought || null; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const exportTraceToJson = ( |
|
|
trace: AgentTrace, |
|
|
steps: AgentStep[], |
|
|
metadata?: AgentTraceMetadata, |
|
|
finalStep?: FinalStep |
|
|
): string => { |
|
|
const exportData = { |
|
|
trace: { |
|
|
id: trace.id, |
|
|
timestamp: trace.timestamp, |
|
|
instruction: trace.instruction, |
|
|
modelId: trace.modelId, |
|
|
isRunning: trace.isRunning, |
|
|
}, |
|
|
completion: finalStep ? { |
|
|
status: finalStep.type, |
|
|
message: finalStep.message || null, |
|
|
finalAnswer: extractFinalAnswer(steps), |
|
|
} : null, |
|
|
metadata: metadata || trace.traceMetadata, |
|
|
steps: steps.map((step) => ({ |
|
|
traceId: step.traceId, |
|
|
stepId: step.stepId, |
|
|
error: step.error, |
|
|
image: step.image, |
|
|
thought: step.thought, |
|
|
actions: step.actions, |
|
|
duration: step.duration, |
|
|
inputTokensUsed: step.inputTokensUsed, |
|
|
outputTokensUsed: step.outputTokensUsed, |
|
|
step_evaluation: step.step_evaluation, |
|
|
})), |
|
|
exportedAt: new Date().toISOString(), |
|
|
}; |
|
|
|
|
|
return JSON.stringify(exportData, null, 2); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const downloadJson = (jsonString: string, filename: string = 'trace.json') => { |
|
|
const blob = new Blob([jsonString], { type: 'application/json' }); |
|
|
const url = URL.createObjectURL(blob); |
|
|
const link = document.createElement('a'); |
|
|
link.href = url; |
|
|
link.download = filename; |
|
|
document.body.appendChild(link); |
|
|
link.click(); |
|
|
document.body.removeChild(link); |
|
|
URL.revokeObjectURL(url); |
|
|
}; |
|
|
|