Skip to content

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

Enhance assistant response handling and node type support

Ylber Gashiee111a845f3a
diff --git a/apps/server/src/jobs/assistant-response.ts b/apps/server/src/jobs/assistant-response.ts
index 4df5846c..41c421b4 100644
--- a/apps/server/src/jobs/assistant-response.ts
+++ b/apps/server/src/jobs/assistant-response.ts
@@ -506,7 +506,7 @@ export const assistantResponseHandler: JobHandler<
// ---------------------------------------------------------------------
async function createAndPublishResponse(
response: string,
- citations: Array<{ sourceId: string; quote: string }>,
+ citations: Array<{ sourceId: string; quote: string }> | undefined,
originalMessage: any,
workspaceId: string
) {
@@ -515,7 +515,7 @@ async function createAndPublishResponse(
const messageAttributes: NodeAttributes = {
type: 'message',
- subtype: 'standard',
+ subtype: 'answer',
parentId: originalMessage.parent_id || originalMessage.id,
content: {
[blockId]: {
@@ -525,7 +525,7 @@ async function createAndPublishResponse(
index: generateNodeIndex(),
parentId: id,
},
- ...citations.reduce((acc, citation) => {
+ ...(citations?.reduce((acc, citation) => {
const citationBlockId = generateId(IdType.Block);
return {
...acc,
@@ -540,7 +540,7 @@ async function createAndPublishResponse(
parentId: id,
},
};
- }, {}),
+ }, {}) || {}),
},
};
diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.ts
index 37b83397..309f4b0c 100644
--- a/apps/server/src/lib/nodes.ts
+++ b/apps/server/src/lib/nodes.ts
@@ -9,6 +9,7 @@ import {
Node,
NodeAttributes,
UpdateNodeMutationData,
+ MessageAttributes,
} from '@colanode/core';
import { YDoc } from '@colanode/crdt';
import { cloneDeep } from 'lodash-es';
@@ -170,7 +171,18 @@ export const createNode = async (
});
}
- await scheduleNodeEmbedding(input.nodeId);
+ await scheduleNodeEmbedding(createdNode);
+
+ if (
+ attributes.type === 'message' &&
+ (attributes as MessageAttributes).subtype === 'question'
+ ) {
+ await jobService.addJob({
+ type: 'assistant_response',
+ messageId: input.nodeId,
+ workspaceId: input.workspaceId,
+ });
+ }
return {
node: createdNode,
@@ -293,7 +305,7 @@ export const tryUpdateNode = async (
});
}
- await scheduleNodeEmbedding(input.nodeId);
+ await scheduleNodeEmbedding(updatedNode);
return {
type: 'success',
@@ -401,7 +413,7 @@ export const createNodeFromMutation = async (
});
}
- await scheduleNodeEmbedding(mutation.id);
+ await scheduleNodeEmbedding(createdNode);
return {
node: createdNode,
@@ -534,7 +546,7 @@ const tryUpdateNodeFromMutation = async (
});
}
- await scheduleNodeEmbedding(mutation.id);
+ await scheduleNodeEmbedding(updatedNode);
return {
type: 'success',
@@ -660,15 +672,28 @@ export const deleteNode = async (
};
};
-async function scheduleNodeEmbedding(nodeId: string) {
+const scheduleNodeEmbedding = async (node: SelectNode) => {
+ if (node.type === 'message') {
+ const attributes = node.attributes as MessageAttributes;
+ if (attributes.subtype === 'question' || attributes.subtype === 'answer') {
+ return;
+ }
+ }
+
+ const jobOptions: { jobId: string; delay?: number } = {
+ jobId: `embed_node:${node.id}`,
+ };
+
+ // Only add delay for non-message nodes
+ if (node.type !== 'message') {
+ jobOptions.delay = configuration.ai.embedDelay;
+ }
+
await jobService.addJob(
{
type: 'embed_node',
- nodeId,
+ nodeId: node.id,
},
- {
- jobId: `embed_node:${nodeId}`,
- delay: configuration.ai.embedDelay,
- }
+ jobOptions
);
-}
+};
diff --git a/apps/server/src/services/document-retrieval-service.ts b/apps/server/src/services/document-retrieval-service.ts
index f48eee75..63aded40 100644
--- a/apps/server/src/services/document-retrieval-service.ts
+++ b/apps/server/src/services/document-retrieval-service.ts
@@ -64,7 +64,7 @@ export class DocumentRetrievalService {
'document_embeddings.text',
'documents.created_at',
'document_embeddings.chunk as chunk_index',
- sql<number>`('[${embedding.join(',')}]'::vector) <=> document_embeddings.embedding_vector`.as(
+ sql<number>`${sql.raw(`'[${embedding}]'::vector`)} <=> document_embeddings.embedding_vector`.as(
'similarity'
),
])
diff --git a/apps/server/src/services/llm-service.ts b/apps/server/src/services/llm-service.ts
index 9556225e..75cfb208 100644
--- a/apps/server/src/services/llm-service.ts
+++ b/apps/server/src/services/llm-service.ts
@@ -10,6 +10,13 @@ import type {
ChunkingMetadata,
NodeMetadata,
} from '@/services/chunking-service';
+import { CallbackHandler } from 'langfuse-langchain';
+
+const langfuseCallback = new CallbackHandler({
+ publicKey: configuration.ai.langfuse.publicKey,
+ secretKey: configuration.ai.langfuse.secretKey,
+ baseUrl: configuration.ai.langfuse.baseUrl,
+});
const rerankedDocumentsSchema = z.object({
rankings: z.array(
@@ -186,22 +193,32 @@ For each filter, use the exact field IDs from the database schema.
Use appropriate operators based on field types.
Example Response:
-{
+{{
"shouldFilter": true,
"filters": [
- {
+ {{
"databaseId": "db1",
"filters": [
- {
+ {{
"type": "field",
"fieldId": "field1",
"operator": "contains",
"value": "search term"
- }
+ }}
]
- }
+ }}
]
-}`
+}}
+
+Return a JSON object with:
+- shouldFilter: boolean
+- filters: array of objects with:
+ - databaseId: string
+ - filters: array of DatabaseViewFilterAttributes
+
+Only include databases that are relevant to the query.
+For each filter, use the exact field IDs from the database schema.
+Use appropriate operators based on field types.`
);
export async function rewriteQuery(query: string): Promise<string> {
@@ -454,9 +471,12 @@ export async function addContextToChunk(
prompt
);
- const response = await model.invoke([
- new SystemMessage({ content: formattedPrompt }),
- ]);
+ const response = await model.invoke(
+ [new SystemMessage({ content: formattedPrompt })],
+ {
+ callbacks: [langfuseCallback],
+ }
+ );
const prefix = (response.content.toString() || '').trim();
return prefix ? `${prefix}\n\n${chunk}` : chunk;
diff --git a/apps/server/src/services/node-retrieval-service.ts b/apps/server/src/services/node-retrieval-service.ts
index e01600a5..e36c303e 100644
--- a/apps/server/src/services/node-retrieval-service.ts
+++ b/apps/server/src/services/node-retrieval-service.ts
@@ -63,7 +63,7 @@ export class NodeRetrievalService {
'node_embeddings.text',
'nodes.created_at',
'node_embeddings.chunk as chunk_index',
- sql<number>`('[${embedding.join(',')}]'::vector) <=> node_embeddings.embedding_vector`.as(
+ sql<number>`${sql.raw(`'[${embedding}]'::vector`)} <=> node_embeddings.embedding_vector`.as(
'similarity'
),
])
diff --git a/packages/core/src/registry/nodes/message.ts b/packages/core/src/registry/nodes/message.ts
index 484a91f8..f023642a 100644
--- a/packages/core/src/registry/nodes/message.ts
+++ b/packages/core/src/registry/nodes/message.ts
@@ -9,7 +9,7 @@ import { hasNodeRole } from '../../lib/permissions';
export const messageAttributesSchema = z.object({
type: z.literal('message'),
- subtype: z.enum(['standard']),
+ subtype: z.enum(['standard', 'question', 'answer']),
name: z.string().optional(),
parentId: z.string(),
referenceId: z.string().nullable().optional(),