| import type { StatelessChatRequest } from '@/lib/types/chat'; |
|
|
| |
|
|
| |
| |
| |
| |
| export function convertMessagesToOpenAI( |
| messages: StatelessChatRequest['messages'], |
| currentAgentId?: string, |
| ): Array<{ role: 'system' | 'user' | 'assistant'; content: string }> { |
| return messages |
| .filter((msg) => msg.role === 'user' || msg.role === 'assistant') |
| .map((msg) => { |
| if (msg.role === 'assistant') { |
| |
| |
| const items: Array<{ type: string; [key: string]: string }> = []; |
|
|
| if (msg.parts) { |
| for (const part of msg.parts) { |
| const p = part as Record<string, unknown>; |
|
|
| if (p.type === 'text' && p.text) { |
| items.push({ type: 'text', content: p.text as string }); |
| } else if ((p.type as string)?.startsWith('action-') && p.state === 'result') { |
| const actionName = (p.actionName || |
| (p.type as string).replace('action-', '')) as string; |
| const output = p.output as Record<string, unknown> | undefined; |
| const isSuccess = output?.success === true; |
| const resultSummary = isSuccess |
| ? output?.data |
| ? `result: ${JSON.stringify(output.data).slice(0, 100)}` |
| : 'success' |
| : (output?.error as string) || 'failed'; |
| items.push({ |
| type: 'action', |
| name: actionName, |
| result: resultSummary, |
| }); |
| } |
| } |
| } |
|
|
| const content = items.length > 0 ? JSON.stringify(items) : ''; |
| const msgAgentId = msg.metadata?.agentId; |
|
|
| |
| |
| if (currentAgentId && msgAgentId && msgAgentId !== currentAgentId) { |
| const agentName = msg.metadata?.senderName || msgAgentId; |
| return { |
| role: 'user' as const, |
| content: content ? `[${agentName}]: ${content}` : '', |
| }; |
| } |
|
|
| return { |
| role: 'assistant' as const, |
| content, |
| }; |
| } |
|
|
| |
| const contentParts: string[] = []; |
|
|
| if (msg.parts) { |
| for (const part of msg.parts) { |
| const p = part as Record<string, unknown>; |
|
|
| if (p.type === 'text' && p.text) { |
| contentParts.push(p.text as string); |
| } else if ((p.type as string)?.startsWith('action-') && p.state === 'result') { |
| const actionName = (p.actionName || |
| (p.type as string).replace('action-', '')) as string; |
| const output = p.output as Record<string, unknown> | undefined; |
| const isSuccess = output?.success === true; |
| const resultSummary = isSuccess |
| ? output?.data |
| ? `result: ${JSON.stringify(output.data).slice(0, 100)}` |
| : 'success' |
| : (output?.error as string) || 'failed'; |
| contentParts.push(`[Action ${actionName}: ${resultSummary}]`); |
| } |
| } |
| } |
|
|
| |
| const senderName = msg.metadata?.senderName; |
| let content = contentParts.join('\n'); |
| if (senderName) { |
| content = `[${senderName}]: ${content}`; |
| } |
|
|
| |
| const isInterrupted = |
| (msg as unknown as Record<string, unknown>).metadata && |
| ((msg as unknown as Record<string, unknown>).metadata as Record<string, unknown>) |
| ?.interrupted; |
| return { |
| role: 'user' as const, |
| content: isInterrupted |
| ? `${content}\n[This response was interrupted — do NOT continue it. Start a new JSON array response.]` |
| : content, |
| }; |
| }) |
| .filter((msg) => { |
| |
| |
| const stripped = msg.content.replace(/[.\s…]+/g, ''); |
| return stripped.length > 0; |
| }); |
| } |
|
|