Skip to content

Commit 4aaa68d

Browse files
committed
Better
1 parent 776ae06 commit 4aaa68d

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

apps/sim/lib/copilot/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ IMPORTANT: Always provide complete, helpful responses. If you add citations, con
8080
temperature: 0.1,
8181
maxTokens: 2000,
8282
embeddingModel: 'text-embedding-3-small',
83-
maxSources: 5,
84-
similarityThreshold: 0.5,
83+
maxSources: 10,
84+
similarityThreshold: 0.3,
8585
},
8686
general: {
8787
streamingEnabled: true,

apps/sim/lib/copilot/service.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ export async function deleteChat(chatId: string, userId: string): Promise<boolea
690690
* Send a message and get a response
691691
*/
692692
export async function sendMessage(request: SendMessageRequest): Promise<{
693-
response: string | ReadableStream
693+
response: string | ReadableStream | any
694694
chatId?: string
695695
citations?: Array<{ id: number; title: string; url: string; similarity?: number }>
696696
}> {
@@ -718,6 +718,41 @@ export async function sendMessage(request: SendMessageRequest): Promise<{
718718
workflowId,
719719
})
720720

721+
// Extract citations from StreamingExecution if available
722+
let citations: Array<{ id: number; title: string; url: string; similarity?: number }> = []
723+
724+
if (typeof response === 'object' && response && 'execution' in response) {
725+
// This is a StreamingExecution - extract citations from tool calls
726+
const execution = (response as any).execution
727+
logger.info('Extracting citations from StreamingExecution', {
728+
hasExecution: !!execution,
729+
hasToolResults: !!execution?.toolResults,
730+
toolResultsLength: execution?.toolResults?.length || 0,
731+
})
732+
733+
if (execution?.toolResults) {
734+
for (const toolResult of execution.toolResults) {
735+
logger.info('Processing tool result for citations', {
736+
hasResult: !!toolResult,
737+
resultKeys: toolResult && typeof toolResult === 'object' ? Object.keys(toolResult) : [],
738+
hasResultsArray: !!(toolResult && typeof toolResult === 'object' && toolResult.results),
739+
})
740+
741+
if (toolResult && typeof toolResult === 'object' && toolResult.results) {
742+
// Convert documentation search results to citations
743+
citations = toolResult.results.map((result: any, index: number) => ({
744+
id: index + 1,
745+
title: result.title || 'Documentation',
746+
url: result.url || '#',
747+
similarity: result.similarity,
748+
}))
749+
logger.info(`Extracted ${citations.length} citations from tool results`)
750+
break // Use first set of results found
751+
}
752+
}
753+
}
754+
}
755+
721756
// If we have a chat, update it with the new messages
722757
if (currentChat) {
723758
const userMessage: CopilotMessage = {
@@ -732,6 +767,7 @@ export async function sendMessage(request: SendMessageRequest): Promise<{
732767
role: 'assistant',
733768
content: typeof response === 'string' ? response : '[Streaming Response]',
734769
timestamp: new Date().toISOString(),
770+
citations: citations.length > 0 ? citations : undefined,
735771
}
736772

737773
const updatedMessages = [...conversationHistory, userMessage, assistantMessage]
@@ -751,7 +787,7 @@ export async function sendMessage(request: SendMessageRequest): Promise<{
751787
return {
752788
response,
753789
chatId: currentChat?.id,
754-
citations: [], // Will be populated when RAG is implemented
790+
citations,
755791
}
756792
} catch (error) {
757793
logger.error('Failed to send message:', error)

0 commit comments

Comments
 (0)