| 50 | } |
| 51 | |
| 52 | function buildExecuteResponsePayload( |
| 53 | result: Awaited<ReturnType<typeof runHeadlessCopilotLifecycle>>, |
| 54 | effectiveChatId: string, |
| 55 | integrationTools: Array<{ name: string }> |
| 56 | ) { |
| 57 | const clientToolNames = new Set(integrationTools.map((t) => t.name)) |
| 58 | const clientToolCalls = (result.toolCalls || []).filter( |
| 59 | (tc: { name: string }) => clientToolNames.has(tc.name) || tc.name.startsWith('mcp-') |
| 60 | ) |
| 61 | |
| 62 | return { |
| 63 | content: result.content, |
| 64 | model: 'mothership', |
| 65 | conversationId: effectiveChatId, |
| 66 | tokens: result.usage |
| 67 | ? { |
| 68 | prompt: result.usage.prompt, |
| 69 | completion: result.usage.completion, |
| 70 | total: (result.usage.prompt || 0) + (result.usage.completion || 0), |
| 71 | } |
| 72 | : {}, |
| 73 | cost: result.cost || undefined, |
| 74 | toolCalls: clientToolCalls, |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * POST /api/mothership/execute |