(
teamId, question, conversationHistory = [], conversation = null, context = null, options = {}
)
| 1656 | } |
| 1657 | |
| 1658 | async function orchestrate( |
| 1659 | teamId, question, conversationHistory = [], conversation = null, context = null, options = {} |
| 1660 | ) { |
| 1661 | // Extract optional tool progress callback |
| 1662 | const { toolProgressCallback, userId } = options; |
| 1663 | if (!openaiClient) { |
| 1664 | throw new Error("OpenAI client is not initialized. Please check your environment variables."); |
| 1665 | } |
| 1666 | |
| 1667 | // Sanitize conversation history to ensure OpenAI API compliance |
| 1668 | // This removes any assistant messages with tool_calls that don't have complete tool responses |
| 1669 | const sanitizedHistory = sanitizeConversationHistory(conversationHistory); |
| 1670 | |
| 1671 | // Emit initial processing event |
| 1672 | if (conversation?.id) { |
| 1673 | emitProgressEvent(socketManager, conversation.id, "PROCESSING_START", { question }); |
| 1674 | } |
| 1675 | |
| 1676 | const semanticLayer = await buildSemanticLayer(teamId); |
| 1677 | |
| 1678 | // Check if this is a capability question |
| 1679 | if (isCapabilityQuestion(question)) { |
| 1680 | // Generate capability response without AI calls |
| 1681 | const capabilityResponse = generateCapabilityResponse(semanticLayer); |
| 1682 | |
| 1683 | // Prepare messages for database recording |
| 1684 | const messages = [ |
| 1685 | { role: "system", content: "System prompt for capability response" }, // Simplified for recording |
| 1686 | ...sanitizedHistory, |
| 1687 | { role: "user", content: question }, |
| 1688 | { role: "assistant", content: capabilityResponse } |
| 1689 | ]; |
| 1690 | |
| 1691 | // Emit completion event |
| 1692 | if (conversation?.id) { |
| 1693 | emitProgressEvent(socketManager, conversation.id, "PROCESSING_COMPLETE"); |
| 1694 | } |
| 1695 | |
| 1696 | // Return with 0 token usage |
| 1697 | return { |
| 1698 | message: capabilityResponse, |
| 1699 | conversationHistory: messages, |
| 1700 | usage: { |
| 1701 | prompt_tokens: 0, |
| 1702 | completion_tokens: 0, |
| 1703 | total_tokens: 0 |
| 1704 | }, |
| 1705 | usageRecords: [{ |
| 1706 | model: openAiModel || "gpt-5.4-nano", |
| 1707 | prompt_tokens: 0, |
| 1708 | completion_tokens: 0, |
| 1709 | total_tokens: 0, |
| 1710 | elapsed_ms: 0, |
| 1711 | }], |
| 1712 | iterations: 0, |
| 1713 | }; |
| 1714 | } |
| 1715 |
no test coverage detected