Skip to content

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

Refactored text extraction to return structured data (name/attributes), allowing for more context. Separated node/document embedding delay configurations. Updated services to use new extraction method and improved optional text handling.

Ylber Gashi646925558717
diff --git a/apps/server/src/jobs/assistant-response.ts b/apps/server/src/jobs/assistant-response.ts
index 41c421b4..c5bb998c 100644
--- a/apps/server/src/jobs/assistant-response.ts
+++ b/apps/server/src/jobs/assistant-response.ts
@@ -219,10 +219,10 @@ async function fetchChatHistory(state: typeof ResponseState.State) {
(message) =>
new Document({
pageContent:
- getNodeModel(message.attributes.type)?.getAttributesText(
+ getNodeModel(message.attributes.type)?.extractNodeText(
message.id,
message.attributes
- ) || '',
+ )?.attributes || '',
metadata: {
id: message.id,
type: 'message',
@@ -443,18 +443,23 @@ export const assistantResponseHandler: JobHandler<
}
const message = await fetchNode(messageId);
- if (!message) return;
+ if (!message) {
+ return;
+ }
const messageModel = getNodeModel(message.attributes.type);
- if (!messageModel) return;
+ if (!messageModel) {
+ return;
+ }
- const messageText = messageModel.getAttributesText(
+ const messageText = messageModel.extractNodeText(
message.id,
message.attributes
);
- if (!messageText) return;
+ if (!messageText || !messageText.attributes) {
+ return;
+ }
- // Fetch user and workspace details only ONCE
const [user, workspace] = await Promise.all([
database
.selectFrom('users')
@@ -463,12 +468,14 @@ export const assistantResponseHandler: JobHandler<
.executeTakeFirst(),
fetchWorkspaceDetails(workspaceId),
]);
- if (!user || !workspace) return;
+ if (!user || !workspace) {
+ return;
+ }
try {
const chainResult = await assistantResponseChain.invoke(
{
- userInput: messageText,
+ userInput: messageText.attributes,
workspaceId,
userId: user.id,
userDetails: {
@@ -554,11 +561,13 @@ async function createAndPublishResponse(
attributes: JSON.stringify(messageAttributes),
created_by: 'colanode_ai',
created_at: new Date(),
- state: Buffer.from([]),
+ revision: BigInt(0),
})
.executeTakeFirst();
- if (!createdMessage) return;
+ if (!createdMessage) {
+ return;
+ }
eventBus.publish({
type: 'node_created',
diff --git a/apps/server/src/jobs/embed-document.ts b/apps/server/src/jobs/embed-document.ts
index 270a5ef0..1a5919cd 100644
--- a/apps/server/src/jobs/embed-document.ts
+++ b/apps/server/src/jobs/embed-document.ts
@@ -2,10 +2,10 @@ import { OpenAIEmbeddings } from '@langchain/openai';
import { ChunkingService } from '@/services/chunking-service';
import { database } from '@/data/database';
import { configuration } from '@/lib/configuration';
-import { CreateDocumentEmbedding, SelectNode } from '@/data/schema';
+import { CreateDocumentEmbedding } from '@/data/schema';
import { sql } from 'kysely';
import { fetchNode } from '@/lib/nodes';
-import { DocumentContent, extractBlockTexts } from '@colanode/core';
+import { extractDocumentText } from '@colanode/core';
import { getNodeModel } from '@colanode/core';
export type EmbedDocumentInput = {
@@ -21,29 +21,6 @@ declare module '@/types/jobs' {
}
}
-const extractDocumentText = async (
- node: SelectNode,
- content: DocumentContent
-): Promise<string> => {
- const nodeModel = getNodeModel(node.attributes.type);
- if (!nodeModel) {
- return '';
- }
-
- const documentText = nodeModel.getDocumentText(node.id, content);
- if (documentText) {
- return documentText;
- } else {
- // Fallback to block text extraction if the node model doesn't handle it
- const blocksText = extractBlockTexts(node.id, content.blocks);
- if (blocksText) {
- return blocksText;
- }
- }
-
- return '';
-};
-
export const embedDocumentHandler = async (input: {
type: 'embed_document';
documentId: string;
@@ -72,7 +49,7 @@ export const embedDocumentHandler = async (input: {
return;
}
- const text = await extractDocumentText(node, document.content);
+ const text = extractDocumentText(node.id, document.content);
if (!text || text.trim() === '') {
await database
.deleteFrom('document_embeddings')
diff --git a/apps/server/src/jobs/embed-node.ts b/apps/server/src/jobs/embed-node.ts
index 4dd028ff..d751c2de 100644
--- a/apps/server/src/jobs/embed-node.ts
+++ b/apps/server/src/jobs/embed-node.ts
@@ -30,12 +30,20 @@ const extractNodeText = (node: SelectNode): string => {
return '';
}
- const attributesText = nodeModel.getAttributesText(node.id, node.attributes);
- if (attributesText) {
- return attributesText;
+ const nodeText = nodeModel.extractNodeText(node.id, node.attributes);
+ if (!nodeText) {
+ return '';
+ }
+
+ const textParts: string[] = [];
+ if (nodeText.name) {
+ textParts.push(nodeText.name);
+ }
+ if (nodeText.attributes) {
+ textParts.push(nodeText.attributes);
}
- return '';
+ return textParts.join('\n').trim();
};
export const embedNodeHandler = async (input: {
diff --git a/apps/server/src/lib/configuration.ts b/apps/server/src/lib/configuration.ts
index 2b3340b8..ff341ad0 100644
--- a/apps/server/src/lib/configuration.ts
+++ b/apps/server/src/lib/configuration.ts
@@ -84,7 +84,8 @@ export interface AIModelConfiguration {
export interface AiConfiguration {
enabled: boolean;
- embedDelay: number;
+ nodeEmbeddingDelay: number;
+ documentEmbeddingDelay: number;
providers: {
openai: AIProviderConfiguration;
google: AIProviderConfiguration;
@@ -207,7 +208,12 @@ export const configuration: Configuration = {
},
ai: {
enabled: getOptionalEnv('AI_ENABLED') === 'true',
- embedDelay: parseInt(getOptionalEnv('AI_EMBED_DELAY') || '10000'),
+ nodeEmbeddingDelay: parseInt(
+ getOptionalEnv('AI_NODE_EMBEDDING_DELAY') || '5000'
+ ),
+ documentEmbeddingDelay: parseInt(
+ getOptionalEnv('AI_DOCUMENT_EMBEDDING_DELAY') || '10000'
+ ),
providers: {
openai: {
apiKey: getOptionalEnv('OPENAI_API_KEY') || '',
diff --git a/apps/server/src/lib/documents.ts b/apps/server/src/lib/documents.ts
index 75c38bbd..ad16d33b 100644
--- a/apps/server/src/lib/documents.ts
+++ b/apps/server/src/lib/documents.ts
@@ -211,7 +211,7 @@ const tryUpdateDocumentFromMutation = async (
}
};
-async function scheduleDocumentEmbedding(documentId: string) {
+const scheduleDocumentEmbedding = async (documentId: string) => {
await jobService.addJob(
{
type: 'embed_document',
@@ -219,7 +219,7 @@ async function scheduleDocumentEmbedding(documentId: string) {
},
{
jobId: `embed_document:${documentId}`,
- delay: configuration.ai.embedDelay,
+ delay: configuration.ai.documentEmbeddingDelay,
}
);
-}
+};
diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.ts
index 4454030c..fe6101f3 100644
--- a/apps/server/src/lib/nodes.ts
+++ b/apps/server/src/lib/nodes.ts
@@ -785,7 +785,7 @@ const scheduleNodeEmbedding = async (node: SelectNode) => {
// Only add delay for non-message nodes
if (node.type !== 'message') {
- jobOptions.delay = configuration.ai.embedDelay;
+ jobOptions.delay = configuration.ai.nodeEmbeddingDelay;
}
await jobService.addJob(
diff --git a/apps/server/src/services/chunking-service.ts b/apps/server/src/services/chunking-service.ts
index 8c4a8f24..0682d8bf 100644
--- a/apps/server/src/services/chunking-service.ts
+++ b/apps/server/src/services/chunking-service.ts
@@ -2,12 +2,12 @@ import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { configuration } from '@/lib/configuration';
import { database } from '@/data/database';
import { addContextToChunk } from '@/services/llm-service';
-import { DocumentContent, getNodeModel, NodeType } from '@colanode/core';
+import { getNodeModel, NodeType } from '@colanode/core';
import type { SelectNode, SelectDocument } from '@/data/schema';
type BaseMetadata = {
id: string;
- name?: string;
+ name?: string | null;
createdAt: Date;
createdBy: string;
updatedAt?: Date | null;
@@ -146,9 +146,9 @@ export class ChunkingService {
const nodeModel = getNodeModel(node.attributes.type);
if (nodeModel) {
- const nodeName = nodeModel.getName(node.id, node.attributes);
- if (nodeName) {
- baseMetadata.name = nodeName;
+ const nodeText = nodeModel.extractNodeText(node.id, node.attributes);
+ if (nodeText) {
+ baseMetadata.name = nodeText.name;
}
// Add parent context if available
@@ -174,7 +174,10 @@ export class ChunkingService {
return undefined;
}
- const nodeName = nodeModel.getName(node.id, node.attributes);
+ const nodeText = nodeModel.extractNodeText(node.id, node.attributes);
+ if (!nodeText || !nodeText.name) {
+ return undefined;
+ }
const author = await database
.selectFrom('users')
@@ -198,7 +201,7 @@ export class ChunkingService {
return {
id: node.id,
- name: nodeName ?? '',
+ name: nodeText.name,
createdAt: node.created_at,
createdBy: node.created_by,
updatedAt: node.updated_at,
@@ -227,7 +230,7 @@ export class ChunkingService {
return undefined;
}
- const parentName = parentModel.getName(
+ const parentText = parentModel.extractNodeText(
parentNode.id,
parentNode.attributes
);
@@ -244,14 +247,14 @@ export class ChunkingService {
const path = pathNodes
.map((n) => {
const model = getNodeModel(n.attributes.type);
- return model?.getName(n.id, n.attributes) ?? 'Untitled';
+ return model?.extractNodeText(n.id, n.attributes)?.name ?? 'Untitled';
})
.join(' / ');
return {
id: parentNode.id,
type: parentNode.attributes.type,
- name: parentName ?? undefined,
+ name: parentText?.name ?? undefined,
path,
};
}