Fix final context and tool status updates (#917)

Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
ekko
2026-05-21 23:21:26 +08:00
committed by GitHub
parent ff1f471745
commit 254573400d
3 changed files with 350 additions and 14 deletions
+40 -7
View File
@@ -79,6 +79,21 @@ function uid(): string {
return Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
}
function isToolOutputError(output: unknown): boolean {
if (typeof output !== 'string' || !output.trim()) return false
try {
const parsed = JSON.parse(output)
if (parsed && typeof parsed === 'object') {
const record = parsed as Record<string, unknown>
if (record.success === false) return true
if (record.error != null && String(record.error).trim() !== '') return true
}
} catch {
return false
}
return false
}
async function uploadFiles(attachments: Attachment[]): Promise<{ name: string; path: string }[]> {
if (attachments.length === 0) return []
const formData = new FormData()
@@ -607,10 +622,11 @@ export const useChatStore = defineStore('chat', () => {
? msgs.filter(m => m.role === 'tool' && m.toolCallId === toolCallId)
: msgs.filter(m => m.role === 'tool' && m.toolStatus === 'running')
if (toolMsgs.length > 0) {
const output = typeof e.output === 'string' ? e.output : undefined
updateMessage(sessionId, toolMsgs[toolMsgs.length - 1].id, {
toolStatus: e.error === true ? 'error' : 'done',
toolStatus: e.error === true || isToolOutputError(output) ? 'error' : 'done',
toolDuration: e.duration,
toolResult: typeof e.output === 'string' ? e.output : undefined,
toolResult: output,
})
}
}
@@ -1224,13 +1240,13 @@ export const useChatStore = defineStore('chat', () => {
: msgs.filter(m => m.role === 'tool' && m.toolStatus === 'running')
if (toolMsgs.length > 0) {
const last = toolMsgs[toolMsgs.length - 1]
// Check if tool errored
const hasError = (evt as any).error === true
const output = typeof (evt as any).output === 'string' ? (evt as any).output : undefined
const hasError = (evt as any).error === true || isToolOutputError(output)
const duration = (evt as any).duration
updateMessage(sid, last.id, {
toolStatus: hasError ? 'error' : 'done',
toolDuration: duration,
toolResult: typeof (evt as any).output === 'string' ? (evt as any).output : undefined,
toolResult: output,
})
}
@@ -1351,6 +1367,14 @@ export const useChatStore = defineStore('chat', () => {
}
case 'run.failed': {
if ((evt as any).inputTokens != null) {
const target = sessions.value.find(s => s.id === sid)
if (target) {
target.inputTokens = (evt as any).inputTokens
target.outputTokens = (evt as any).outputTokens
if ((evt as any).contextTokens != null) target.contextTokens = (evt as any).contextTokens
}
}
addAgentErrorMessage(sid, evt.error)
const msgs = getSessionMsgs(sid)
msgs.forEach((m, i) => {
@@ -1653,11 +1677,12 @@ export const useChatStore = defineStore('chat', () => {
? msgs.filter(m => m.role === 'tool' && m.toolCallId === toolCallId)
: msgs.filter(m => m.role === 'tool' && m.toolStatus === 'running')
if (toolMsgs.length > 0) {
const hasError = (evt as any).error === true
const output = typeof (evt as any).output === 'string' ? (evt as any).output : undefined
const hasError = (evt as any).error === true || isToolOutputError(output)
updateMessage(sid, toolMsgs[toolMsgs.length - 1].id, {
toolStatus: hasError ? 'error' : 'done',
toolDuration: (evt as any).duration,
toolResult: typeof (evt as any).output === 'string' ? (evt as any).output : undefined,
toolResult: output,
})
}
@@ -1764,6 +1789,14 @@ export const useChatStore = defineStore('chat', () => {
}
case 'run.failed': {
if ((evt as any).inputTokens != null) {
const target = sessions.value.find(s => s.id === sid)
if (target) {
target.inputTokens = (evt as any).inputTokens
target.outputTokens = (evt as any).outputTokens
if ((evt as any).contextTokens != null) target.contextTokens = (evt as any).contextTokens
}
}
const hasQueue = (evt as any).queue_remaining > 0
if (hasQueue) {
queueLengths.value.set(sid, (evt as any).queue_remaining)