From 4aaa68d21bf8cb684cf731b802e8861f0f397f79 Mon Sep 17 00:00:00 2001 From: Siddharth Ganesan Date: Tue, 8 Jul 2025 19:52:06 -0700 Subject: [PATCH] Better --- apps/sim/lib/copilot/config.ts | 4 ++-- apps/sim/lib/copilot/service.ts | 40 +++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/apps/sim/lib/copilot/config.ts b/apps/sim/lib/copilot/config.ts index b4fc9a749a..c266b5c55b 100644 --- a/apps/sim/lib/copilot/config.ts +++ b/apps/sim/lib/copilot/config.ts @@ -80,8 +80,8 @@ IMPORTANT: Always provide complete, helpful responses. If you add citations, con temperature: 0.1, maxTokens: 2000, embeddingModel: 'text-embedding-3-small', - maxSources: 5, - similarityThreshold: 0.5, + maxSources: 10, + similarityThreshold: 0.3, }, general: { streamingEnabled: true, diff --git a/apps/sim/lib/copilot/service.ts b/apps/sim/lib/copilot/service.ts index 519ecd91fc..a6e3da67ad 100644 --- a/apps/sim/lib/copilot/service.ts +++ b/apps/sim/lib/copilot/service.ts @@ -690,7 +690,7 @@ export async function deleteChat(chatId: string, userId: string): Promise }> { @@ -718,6 +718,41 @@ export async function sendMessage(request: SendMessageRequest): Promise<{ workflowId, }) + // Extract citations from StreamingExecution if available + let citations: Array<{ id: number; title: string; url: string; similarity?: number }> = [] + + if (typeof response === 'object' && response && 'execution' in response) { + // This is a StreamingExecution - extract citations from tool calls + const execution = (response as any).execution + logger.info('Extracting citations from StreamingExecution', { + hasExecution: !!execution, + hasToolResults: !!execution?.toolResults, + toolResultsLength: execution?.toolResults?.length || 0, + }) + + if (execution?.toolResults) { + for (const toolResult of execution.toolResults) { + logger.info('Processing tool result for citations', { + hasResult: !!toolResult, + resultKeys: toolResult && typeof toolResult === 'object' ? Object.keys(toolResult) : [], + hasResultsArray: !!(toolResult && typeof toolResult === 'object' && toolResult.results), + }) + + if (toolResult && typeof toolResult === 'object' && toolResult.results) { + // Convert documentation search results to citations + citations = toolResult.results.map((result: any, index: number) => ({ + id: index + 1, + title: result.title || 'Documentation', + url: result.url || '#', + similarity: result.similarity, + })) + logger.info(`Extracted ${citations.length} citations from tool results`) + break // Use first set of results found + } + } + } + } + // If we have a chat, update it with the new messages if (currentChat) { const userMessage: CopilotMessage = { @@ -732,6 +767,7 @@ export async function sendMessage(request: SendMessageRequest): Promise<{ role: 'assistant', content: typeof response === 'string' ? response : '[Streaming Response]', timestamp: new Date().toISOString(), + citations: citations.length > 0 ? citations : undefined, } const updatedMessages = [...conversationHistory, userMessage, assistantMessage] @@ -751,7 +787,7 @@ export async function sendMessage(request: SendMessageRequest): Promise<{ return { response, chatId: currentChat?.id, - citations: [], // Will be populated when RAG is implemented + citations, } } catch (error) { logger.error('Failed to send message:', error)