ai-chatbot/services/pascal-editor/apps/editor/lib/pascal-agent-recovery.ts

52 lines
1.5 KiB
TypeScript

import type { UIMessage } from 'ai'
export interface PendingPascalAgentRun {
conversationId: string
startedAt: number
userMessageId: string
}
export function parsePendingPascalAgentRun(value: string | null): PendingPascalAgentRun | null {
if (!value) return null
try {
const parsed = JSON.parse(value) as Partial<PendingPascalAgentRun>
if (
typeof parsed.conversationId !== 'string' ||
typeof parsed.startedAt !== 'number' ||
typeof parsed.userMessageId !== 'string'
) {
return null
}
return parsed as PendingPascalAgentRun
} catch {
return null
}
}
export function hasCompletedPascalAgentResponse(
messages: UIMessage[],
userMessageId: string,
): boolean {
const userIndex = messages.findIndex(
(message) => message.id === userMessageId && message.role === 'user',
)
if (userIndex < 0) return false
return messages.slice(userIndex + 1).some(
(message) =>
message.role === 'assistant' &&
message.parts.some((part) => {
if (part.type === 'text') return Boolean(part.text.trim())
if (part.type === 'data-pascal-tool-activity') {
const state = (part.data as { state?: unknown } | undefined)?.state
return state === 'output-available' || state === 'output-error'
}
if (part.type.startsWith('tool-')) {
const state = (part as { state?: unknown }).state
return state === 'output-available' || state === 'output-error'
}
return false
}),
)
}