Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Add support for specifying context nodes in AI assistant response

Ylber Gashi29c96f7dd64b
diff --git a/apps/server/src/jobs/assistant-response.ts b/apps/server/src/jobs/assistant-response.ts
index 13bceebb..d70d65bb 100644
--- a/apps/server/src/jobs/assistant-response.ts
+++ b/apps/server/src/jobs/assistant-response.ts
@@ -12,6 +12,7 @@ export type AssistantResponseInput = {
type: 'assistant_response';
messageId: string;
workspaceId: string;
+ selectedContextNodeIds?: string[];
};
declare module '@/types/jobs' {
@@ -25,10 +26,11 @@ declare module '@/types/jobs' {
export const assistantResponseHandler: JobHandler<
AssistantResponseInput
> = async (input) => {
- const { messageId, workspaceId } = input;
+ const { messageId, workspaceId, selectedContextNodeIds } = input;
console.log('Starting assistant response handler', {
messageId,
workspaceId,
+ selectedContextNodeIds,
});
if (!configuration.ai.enabled) {
@@ -84,6 +86,7 @@ export const assistantResponseHandler: JobHandler<
parentMessageId: message.parent_id || message.id,
currentMessageId: message.id,
originalMessage: message,
+ selectedContextNodeIds,
});
await createAndPublishResponse(
diff --git a/apps/server/src/jobs/embed-node.ts b/apps/server/src/jobs/embed-node.ts
index d5e823c1..d634677b 100644
--- a/apps/server/src/jobs/embed-node.ts
+++ b/apps/server/src/jobs/embed-node.ts
@@ -6,7 +6,15 @@ import { CreateNodeEmbedding, SelectNode } from '@/data/schema';
import { sql } from 'kysely';
import { fetchNode } from '@/lib/nodes';
import { getNodeModel } from '@colanode/core';
-import { FieldValue, DatabaseAttributes } from '@colanode/core';
+import {
+ FieldValue,
+ DatabaseAttributes,
+ TextFieldValue,
+ NumberFieldValue,
+ BooleanFieldValue,
+ StringArrayFieldValue,
+ StringFieldValue,
+} from '@colanode/core';
import { enrichChunk } from '@/services/llm-service';
export type EmbedNodeInput = {
@@ -65,19 +73,43 @@ const extractNodeText = async (
}
let valueText = '';
- switch (typedField.type) {
- case 'string':
+ switch (fieldInfo.type) {
case 'text':
- valueText = typedField.value.toString();
+ case 'email':
+ case 'phone':
+ case 'url':
+ valueText = (typedField as TextFieldValue).value;
break;
case 'number':
- valueText = typedField.value.toString();
+ valueText = (typedField as NumberFieldValue).value.toString();
break;
case 'boolean':
- valueText = typedField.value ? 'Yes' : 'No';
+ valueText = (typedField as BooleanFieldValue).value ? 'Yes' : 'No';
break;
- case 'string_array':
- valueText = typedField.value.join(', ');
+ case 'multi_select':
+ const multiSelectValue = (typedField as StringArrayFieldValue)
+ .value;
+ valueText = multiSelectValue
+ .map((id) => fieldInfo.options?.[id]?.name || id)
+ .join(', ');
+ break;
+ case 'relation':
+ const relationValue = (typedField as StringArrayFieldValue).value;
+ valueText = `Related records: ${relationValue.join(', ')}`;
+ break;
+ case 'collaborator':
+ const collaboratorValue = (typedField as StringArrayFieldValue)
+ .value;
+ valueText = `Collaborators: ${collaboratorValue.join(', ')}`;
+ break;
+ case 'date':
+ valueText = new Date(
+ (typedField as StringFieldValue).value
+ ).toLocaleString();
+ break;
+ case 'select':
+ const selectValue = (typedField as StringFieldValue).value;
+ valueText = fieldInfo.options?.[selectValue]?.name || selectValue;
break;
default:
break;
diff --git a/apps/server/src/lib/assistant.ts b/apps/server/src/lib/assistant.ts
index 75f832be..8e0c7db0 100644
--- a/apps/server/src/lib/assistant.ts
+++ b/apps/server/src/lib/assistant.ts
@@ -3,7 +3,7 @@ import { Document } from '@langchain/core/documents';
import { database } from '@/data/database';
import { configuration } from '@/lib/configuration';
import { CallbackHandler } from 'langfuse-langchain';
-import { fetchNode } from '@/lib/nodes';
+import { fetchNode, fetchNodeDescendants } from '@/lib/nodes';
import {
rewriteQuery,
assessUserIntent,
@@ -75,12 +75,16 @@ async function fetchContextDocuments(state: AssistantChainState) {
nodeRetrievalService.retrieve(
state.rewrittenQuery,
state.workspaceId,
- state.userId
+ state.userId,
+ configuration.ai.retrieval.hybridSearch.maxResults,
+ state.selectedContextNodeIds
),
documentRetrievalService.retrieve(
state.rewrittenQuery,
state.workspaceId,
- state.userId
+ state.userId,
+ configuration.ai.retrieval.hybridSearch.maxResults,
+ state.selectedContextNodeIds
),
]);
let databaseResults: Document[] = [];
@@ -280,14 +284,7 @@ function selectTopContext(
.slice(0, max)
.map((item) => {
if (item.index >= 0 && item.index < contextDocuments.length) {
- const doc = contextDocuments[item.index];
- if (
- doc &&
- doc.metadata.id === item.sourceId &&
- doc.metadata.type === item.type
- ) {
- return doc;
- }
+ return contextDocuments[item.index];
}
return undefined;
})
@@ -320,11 +317,29 @@ const assistantResponseChain = new StateGraph(ResponseState)
.addEdge('generateNoContextResponse', '__end__')
.compile();
-const langfuseCallback = new CallbackHandler({
- publicKey: configuration.ai.langfuse.publicKey,
- secretKey: configuration.ai.langfuse.secretKey,
- baseUrl: configuration.ai.langfuse.baseUrl,
-});
+const langfuseCallback = configuration.ai.langfuse.enabled
+ ? new CallbackHandler({
+ publicKey: configuration.ai.langfuse.publicKey,
+ secretKey: configuration.ai.langfuse.secretKey,
+ baseUrl: configuration.ai.langfuse.baseUrl,
+ })
+ : undefined;
+
+async function getFullContextNodeIds(selectedIds: string[]): Promise<string[]> {
+ const fullSet = new Set<string>();
+ console.log('selectedIds', selectedIds);
+ for (const id of selectedIds) {
+ fullSet.add(id);
+ try {
+ const descendants = await fetchNodeDescendants(id);
+ descendants.forEach((descId) => fullSet.add(descId));
+ } catch (error) {
+ console.error(`Error fetching descendants for node ${id}:`, error);
+ }
+ }
+ console.log('fullSet', fullSet);
+ return Array.from(fullSet);
+}
export async function runAssistantResponseChain(input: {
userInput: string;
@@ -334,17 +349,31 @@ export async function runAssistantResponseChain(input: {
parentMessageId: string;
currentMessageId: string;
originalMessage: any;
+ selectedContextNodeIds?: string[];
}): Promise<{
finalAnswer: string;
citations: Array<{ sourceId: string; quote: string }>;
}> {
- const result = await assistantResponseChain.invoke(
- {
- ...input,
- intent: 'retrieve',
- databaseFilters: { shouldFilter: false, filters: [] },
- },
- { callbacks: [langfuseCallback] }
- );
+ // Process the selected context node IDs if provided
+ let fullContextNodeIds: string[] = [];
+ if (input.selectedContextNodeIds && input.selectedContextNodeIds.length > 0) {
+ fullContextNodeIds = await getFullContextNodeIds(
+ input.selectedContextNodeIds
+ );
+ }
+
+ // Build the chain input with the new property added into the state.
+ const chainInput = {
+ ...input,
+ selectedContextNodeIds: fullContextNodeIds,
+ intent: 'retrieve' as const,
+ databaseFilters: { shouldFilter: false, filters: [] },
+ };
+
+ const callbacks = langfuseCallback ? [langfuseCallback] : undefined;
+
+ const result = await assistantResponseChain.invoke(chainInput, {
+ callbacks,
+ });
return { finalAnswer: result.finalAnswer, citations: result.citations };
}
diff --git a/apps/server/src/lib/configuration.ts b/apps/server/src/lib/configuration.ts
index ff341ad0..f2759aef 100644
--- a/apps/server/src/lib/configuration.ts
+++ b/apps/server/src/lib/configuration.ts
@@ -91,6 +91,7 @@ export interface AiConfiguration {
google: AIProviderConfiguration;
};
langfuse: {
+ enabled: boolean;
publicKey: string;
secretKey: string;
baseUrl: string;
@@ -225,6 +226,7 @@ export const configuration: Configuration = {
},
},
langfuse: {
+ enabled: getOptionalEnv('LANGFUSE_ENABLED') === 'true',
publicKey: getOptionalEnv('LANGFUSE_PUBLIC_KEY') || '',
secretKey: getOptionalEnv('LANGFUSE_SECRET_KEY') || '',
baseUrl:
diff --git a/apps/server/src/lib/llm-prompts.ts b/apps/server/src/lib/llm-prompts.ts
index c9232cf4..061bf9d4 100644
--- a/apps/server/src/lib/llm-prompts.ts
+++ b/apps/server/src/lib/llm-prompts.ts
@@ -57,6 +57,7 @@ USER QUERY:
{question}
Based solely on the conversation history and the relevant context above, provide a clear and professional answer to the user's query. In your answer, include exact quotes from the provided context that support your answer.
+If the relevant context does not contain any information that answers the user's query, respond with "No relevant information found."
Return your response as a JSON object with the following structure:
{{
diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.ts
index 67f155f5..0303f077 100644
--- a/apps/server/src/lib/nodes.ts
+++ b/apps/server/src/lib/nodes.ts
@@ -214,6 +214,8 @@ export const createNode = async (
type: 'assistant_response',
messageId: input.nodeId,
workspaceId: input.workspaceId,
+ selectedContextNodeIds:
+ (attributes as MessageAttributes).selectedContextNodeIds || [],
});
}
@@ -486,6 +488,19 @@ export const createNodeFromMutation = async (
await scheduleNodeEmbedding(createdNode);
+ if (
+ attributes.type === 'message' &&
+ (attributes as MessageAttributes).subtype === 'question'
+ ) {
+ await jobService.addJob({
+ type: 'assistant_response',
+ messageId: mutation.nodeId,
+ workspaceId: user.workspace_id,
+ selectedContextNodeIds:
+ (attributes as MessageAttributes).selectedContextNodeIds || [],
+ });
+ }
+
return {
node: createdNode,
};
diff --git a/apps/server/src/services/document-retrieval-service.ts b/apps/server/src/services/document-retrieval-service.ts
index a7737f51..9e2923ba 100644
--- a/apps/server/src/services/document-retrieval-service.ts
+++ b/apps/server/src/services/document-retrieval-service.ts
@@ -15,26 +15,24 @@ export class DocumentRetrievalService {
query: string,
workspaceId: string,
userId: string,
- limit = configuration.ai.retrieval.hybridSearch.maxResults
+ limit = configuration.ai.retrieval.hybridSearch.maxResults,
+ contextNodeIds?: string[]
): Promise<Document[]> {
const embedding = await this.embeddings.embedQuery(query);
if (!embedding) {
return [];
}
- const semanticResults = await this.semanticSearch(
- embedding,
- workspaceId,
- userId,
- limit
- );
-
- const keywordResults = await this.keywordSearch(
- query,
- workspaceId,
- userId,
- limit
- );
+ const [semanticResults, keywordResults] = await Promise.all([
+ this.semanticSearch(
+ embedding,
+ workspaceId,
+ userId,
+ limit,
+ contextNodeIds
+ ),
+ this.keywordSearch(query, workspaceId, userId, limit, contextNodeIds),
+ ]);
return this.combineSearchResults(semanticResults, keywordResults);
}
@@ -43,9 +41,10 @@ export class DocumentRetrievalService {
embedding: number[],
workspaceId: string,
userId: string,
- limit: number
+ limit: number,
+ contextNodeIds?: string[]
): Promise<SearchResult[]> {
- const results = await database
+ let queryBuilder = database
.selectFrom('document_embeddings')
.innerJoin('documents', 'documents.id', 'document_embeddings.document_id')
.innerJoin('nodes', 'nodes.id', 'documents.id')
@@ -65,7 +64,17 @@ export class DocumentRetrievalService {
'similarity'
),
])
- .where('document_embeddings.workspace_id', '=', workspaceId)
+ .where('document_embeddings.workspace_id', '=', workspaceId);
+
+ if (contextNodeIds && contextNodeIds.length > 0) {
+ queryBuilder = queryBuilder.where(
+ 'document_embeddings.document_id',
+ 'in',
+ contextNodeIds
+ );
+ }
+
+ const results = await queryBuilder
.groupBy([
'document_embeddings.document_id',
'document_embeddings.text',
@@ -92,9 +101,10 @@ export class DocumentRetrievalService {
query: string,
workspaceId: string,
userId: string,
- limit: number
+ limit: number,
+ contextNodeIds?: string[]
): Promise<SearchResult[]> {
- const results = await database
+ let queryBuilder = database
.selectFrom('document_embeddings')
.innerJoin('documents', 'documents.id', 'document_embeddings.document_id')
.innerJoin('nodes', 'nodes.id', 'documents.id')
@@ -118,7 +128,17 @@ export class DocumentRetrievalService {
.where(
(eb) =>
sql`document_embeddings.search_vector @@ websearch_to_tsquery('english', ${query})`
- )
+ );
+
+ if (contextNodeIds && contextNodeIds.length > 0) {
+ queryBuilder = queryBuilder.where(
+ 'document_embeddings.document_id',
+ 'in',
+ contextNodeIds
+ );
+ }
+
+ const results = await queryBuilder
.groupBy([
'document_embeddings.document_id',
'document_embeddings.text',
@@ -152,11 +172,11 @@ export class DocumentRetrievalService {
1
);
const maxKeywordScore = Math.max(...keywordResults.map((r) => r.score), 1);
-
+
const combined = new Map<string, SearchResult & { finalScore: number }>();
const createKey = (result: SearchResult) =>
`${result.id}-${result.chunkIndex}`;
-
+
const calculateRecencyBoost = (
createdAt: Date | undefined | null
): number => {
diff --git a/apps/server/src/services/llm-service.ts b/apps/server/src/services/llm-service.ts
index b5315e63..3eacf356 100644
--- a/apps/server/src/services/llm-service.ts
+++ b/apps/server/src/services/llm-service.ts
@@ -24,11 +24,13 @@ import {
} from '@/lib/llm-prompts';
import { SystemMessage } from '@langchain/core/messages';
-const langfuseCallback = new CallbackHandler({
- publicKey: configuration.ai.langfuse.publicKey,
- secretKey: configuration.ai.langfuse.secretKey,
- baseUrl: configuration.ai.langfuse.baseUrl,
-});
+const langfuseCallback = configuration.ai.langfuse.enabled
+ ? new CallbackHandler({
+ publicKey: configuration.ai.langfuse.publicKey,
+ secretKey: configuration.ai.langfuse.secretKey,
+ baseUrl: configuration.ai.langfuse.baseUrl,
+ })
+ : undefined;
const getChatModel = (
task: keyof typeof configuration.ai.models
@@ -192,8 +194,9 @@ export async function enrichChunk(
const task = 'contextEnhancer';
const model = getChatModel(task);
const messages = [new SystemMessage({ content: prompt })];
+ const callbacks = langfuseCallback ? [langfuseCallback] : undefined;
const response = await model.invoke(messages, {
- callbacks: [langfuseCallback],
+ callbacks,
});
return (response.content.toString() || '').trim();
diff --git a/apps/server/src/services/node-retrieval-service.ts b/apps/server/src/services/node-retrieval-service.ts
index c52b85be..96eee6cd 100644
--- a/apps/server/src/services/node-retrieval-service.ts
+++ b/apps/server/src/services/node-retrieval-service.ts
@@ -16,26 +16,24 @@ export class NodeRetrievalService {
query: string,
workspaceId: string,
userId: string,
- limit = configuration.ai.retrieval.hybridSearch.maxResults
+ limit = configuration.ai.retrieval.hybridSearch.maxResults,
+ contextNodeIds?: string[]
): Promise<Document[]> {
const embedding = await this.embeddings.embedQuery(query);
if (!embedding) {
return [];
}
- const semanticResults = await this.semanticSearch(
- embedding,
- workspaceId,
- userId,
- limit
- );
-
- const keywordResults = await this.keywordSearch(
- query,
- workspaceId,
- userId,
- limit
- );
+ const [semanticResults, keywordResults] = await Promise.all([
+ this.semanticSearch(
+ embedding,
+ workspaceId,
+ userId,
+ limit,
+ contextNodeIds
+ ),
+ this.keywordSearch(query, workspaceId, userId, limit, contextNodeIds),
+ ]);
return this.combineSearchResults(semanticResults, keywordResults);
}
@@ -44,9 +42,10 @@ export class NodeRetrievalService {
embedding: number[],
workspaceId: string,
userId: string,
- limit: number
+ limit: number,
+ contextNodeIds?: string[]
): Promise<SearchResult[]> {
- const results = await database
+ let queryBuilder = database
.selectFrom('node_embeddings')
.innerJoin('nodes', 'nodes.id', 'node_embeddings.node_id')
.innerJoin('collaborations', (join) =>
@@ -65,7 +64,17 @@ export class NodeRetrievalService {
'similarity'
),
])
- .where('node_embeddings.workspace_id', '=', workspaceId)
+ .where('node_embeddings.workspace_id', '=', workspaceId);
+
+ if (contextNodeIds && contextNodeIds.length > 0) {
+ queryBuilder = queryBuilder.where(
+ 'node_embeddings.node_id',
+ 'in',
+ contextNodeIds
+ );
+ }
+
+ const results = await queryBuilder
.groupBy([
'node_embeddings.node_id',
'node_embeddings.text',
@@ -92,9 +101,10 @@ export class NodeRetrievalService {
query: string,
workspaceId: string,
userId: string,
- limit: number
+ limit: number,
+ contextNodeIds?: string[]
): Promise<SearchResult[]> {
- const results = await database
+ let queryBuilder = database
.selectFrom('node_embeddings')
.innerJoin('nodes', 'nodes.id', 'node_embeddings.node_id')
.innerJoin('collaborations', (join) =>
@@ -117,7 +127,17 @@ export class NodeRetrievalService {
.where(
(eb) =>
sql`node_embeddings.search_vector @@ websearch_to_tsquery('english', ${query})`
- )
+ );
+
+ if (contextNodeIds && contextNodeIds.length > 0) {
+ queryBuilder = queryBuilder.where(
+ 'node_embeddings.node_id',
+ 'in',
+ contextNodeIds
+ );
+ }
+
+ const results = await queryBuilder
.groupBy([
'node_embeddings.node_id',
'node_embeddings.text',
diff --git a/apps/server/src/types/assistant.ts b/apps/server/src/types/assistant.ts
index 94977900..f1df25df 100644
--- a/apps/server/src/types/assistant.ts
+++ b/apps/server/src/types/assistant.ts
@@ -61,6 +61,7 @@ export const ResponseState = Annotation.Root({
filters: any[];
}>;
}>(),
+ selectedContextNodeIds: Annotation<string[]>(),
});
export type AssistantChainState = typeof ResponseState.State;
diff --git a/packages/core/src/registry/nodes/message.ts b/packages/core/src/registry/nodes/message.ts
index 38121b8d..ab915460 100644
--- a/packages/core/src/registry/nodes/message.ts
+++ b/packages/core/src/registry/nodes/message.ts
@@ -14,6 +14,7 @@ export const messageAttributesSchema = z.object({
parentId: z.string(),
referenceId: z.string().nullable().optional(),
content: z.record(blockSchema).optional().nullable(),
+ selectedContextNodeIds: z.array(z.string()).optional().nullable(),
});
export type MessageAttributes = z.infer<typeof messageAttributesSchema>;