Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Refactor LLM service with improved context generation for chunking
Ylber Gashi2509ad5d75d2
diff --git a/apps/server/src/jobs/embed-document.ts b/apps/server/src/jobs/embed-document.tsindex 73969007..2001f4b8 100644--- a/apps/server/src/jobs/embed-document.ts+++ b/apps/server/src/jobs/embed-document.ts@@ -6,6 +6,7 @@ import { CreateDocumentEmbedding } from '@/data/schema';import { sql } from 'kysely';import { fetchNode } from '@/lib/nodes';import { DocumentContent, extractBlockTexts } from '@colanode/core';+import { getNodeModel } from '@colanode/core';export type EmbedDocumentInput = {type: 'embed_document';@@ -20,58 +21,48 @@ declare module '@/types/jobs' {}}-const getNodeDisplayName = (- node: Awaited<ReturnType<typeof fetchNode>>-): string => {- if (!node) {- return '';- }-- switch (node.attributes.type) {- case 'page':- case 'database':- case 'folder':- case 'database_view':- return node.attributes.name;- case 'record':- return node.attributes.name;- default:- return '';- }-};-const extractDocumentText = async (documentId: string,content: DocumentContent): Promise<string> => {const sections: string[] = [];- if (documentId) {- const node = await fetchNode(documentId);- if (!node) return '';+ const node = await fetchNode(documentId);+ if (!node) {+ return '';+ }- const nodeName = getNodeDisplayName(node);+ const nodeModel = getNodeModel(node.attributes.type);+ if (!nodeModel) {+ return '';+ }- if (node.attributes.type === 'record') {- const databaseNode = await fetchNode(node.attributes.databaseId);- const databaseName =- databaseNode?.attributes.type === 'database'- ? databaseNode.attributes.name- : node.attributes.databaseId;+ const nodeName = nodeModel.getName(documentId, node.attributes);+ if (nodeName) {+ sections.push(`${node.attributes.type} "${nodeName}"`);+ }- sections.push(`Record "${nodeName}" in database "${databaseName}"`);- } else if (nodeName) {- sections.push(`${node.attributes.type} "${nodeName}"`);- }+ const attributesText = nodeModel.getAttributesText(+ documentId,+ node.attributes+ );+ if (attributesText) {+ sections.push(attributesText);}- const blocksText = extractBlockTexts(documentId, content.blocks);- if (blocksText) {- sections.push('Content:');- sections.push(blocksText);+ const documentText = nodeModel.getDocumentText(documentId, content);+ if (documentText) {+ sections.push(documentText);+ } else {+ // Fallback to block text extraction if the model doesn't handle it+ const blocksText = extractBlockTexts(documentId, content.blocks);+ if (blocksText) {+ sections.push('Content:');+ sections.push(blocksText);+ }}- return sections.join('\n\n');+ return sections.filter(Boolean).join('\n\n');};export const embedDocumentHandler = async (input: {@@ -94,6 +85,14 @@ export const embedDocumentHandler = async (input: {return;}+ const node = await fetchNode(documentId);+ if (!node) return;++ const nodeModel = getNodeModel(node.attributes.type);+ if (!nodeModel?.documentSchema) {+ return;+ }+const text = await extractDocumentText(documentId, document.content);if (!text || text.trim() === '') {await databasediff --git a/apps/server/src/jobs/embed-node.ts b/apps/server/src/jobs/embed-node.tsindex 0ead62cd..9229bdfd 100644--- a/apps/server/src/jobs/embed-node.ts+++ b/apps/server/src/jobs/embed-node.ts@@ -1,12 +1,11 @@import { OpenAIEmbeddings } from '@langchain/openai';-import { extractBlockTexts, NodeAttributes, FieldValue } from '@colanode/core';import { ChunkingService } from '@/services/chunking-service';import { database } from '@/data/database';import { configuration } from '@/lib/configuration';import { CreateNodeEmbedding } from '@/data/schema';import { sql } from 'kysely';import { fetchNode } from '@/lib/nodes';-import { JobHandler } from '@/types/jobs';+import { getNodeModel } from '@colanode/core';export type EmbedNodeInput = {type: 'embed_node';@@ -21,58 +20,39 @@ declare module '@/types/jobs' {}}-const formatFieldValue = (fieldValue: FieldValue): string => {- switch (fieldValue.type) {- case 'boolean':- return `${fieldValue.value ? 'Yes' : 'No'}`;- case 'string_array':- return (fieldValue.value as string[]).join(', ');- case 'number':- case 'string':- case 'text':- return String(fieldValue.value);- default:- return '';- }-};-const extractNodeText = async (nodeId: string,- attributes: NodeAttributes+ node: Awaited<ReturnType<typeof fetchNode>>): Promise<string> => {- switch (attributes.type) {- case 'message':- return extractBlockTexts(nodeId, attributes.content) ?? '';- case 'record': {- const sections: string[] = [];-- // Fetch the database node to get its name- const databaseNode = await fetchNode(attributes.databaseId);- const databaseName =- databaseNode?.attributes.type === 'database'- ? databaseNode.attributes.name- : attributes.databaseId;-- // Add field context with database name- sections.push(`Field "${attributes.name}" in database "${databaseName}"`);-- // Process field value- Object.entries(attributes.fields).forEach(([fieldName, fieldValue]) => {- if (!fieldValue || !('type' in fieldValue)) {- return;- }-- const value = formatFieldValue(fieldValue as FieldValue);- if (value) {- sections.push(value);- }- });-- return sections.join('\n');+ if (!node) return '';++ // Get the node model to use its text extraction methods+ const nodeModel = getNodeModel(node.attributes.type);+ if (!nodeModel) return '';++ const sections: string[] = [];++ // Get the node's name if available+ const nodeName = nodeModel.getName(nodeId, node.attributes);+ if (nodeName) {+ sections.push(`${node.attributes.type} "${nodeName}"`);+ }++ // Get text from attributes (this handles message content, record fields, etc.)+ const attributesText = nodeModel.getAttributesText(nodeId, node.attributes);+ if (attributesText) {+ sections.push(attributesText);+ }++ // For records, add database context+ if (node.attributes.type === 'record') {+ const databaseNode = await fetchNode(node.attributes.databaseId);+ if (databaseNode?.attributes.type === 'database') {+ sections.push(`In database "${databaseNode.attributes.name}"`);}- default:- return '';}++ return sections.filter(Boolean).join('\n');};export const embedNodeHandler = async (input: {@@ -89,12 +69,13 @@ export const embedNodeHandler = async (input: {return;}- // Skip page nodes (document content is handled separately in the embed document job)- if (node.type === 'page') {+ // Skip nodes that are handled by document embeddings+ const nodeModel = getNodeModel(node.attributes.type);+ if (!nodeModel || nodeModel.documentSchema) {return;}- const text = await extractNodeText(node.id, node.attributes);+ const text = await extractNodeText(nodeId, node);if (!text || text.trim() === '') {await database.deleteFrom('node_embeddings')diff --git a/apps/server/src/services/chunking-service.ts b/apps/server/src/services/chunking-service.tsindex fa7b7be6..8e70eadf 100644--- a/apps/server/src/services/chunking-service.ts+++ b/apps/server/src/services/chunking-service.ts@@ -4,87 +4,38 @@ import { database } from '@/data/database';import { addContextToChunk } from '@/services/llm-service';import {DocumentContent,+ getNodeModel,+ Node,NodeAttributes,- MessageAttributes,- ChatAttributes,- ChannelAttributes,- RecordAttributes,- DatabaseAttributes,- extractBlockTexts,} from '@colanode/core';+import type { SelectNode, SelectDocument, SelectUser } from '@/data/schema';-type BaseNodeMetadata = {+type BaseMetadata = {id: string;type: string;- attributes: NodeAttributes;+ name?: string;createdAt: Date;createdBy: string;author?: { id: string; name: string };-};--type MessageNodeMetadata = BaseNodeMetadata & {- nodeType: 'message';parentContext?: {- type: 'chat' | 'channel';- name?: string;- collaborators?: Array<{ id: string; name: string }>;- };- referencedMessage?: {id: string;- content: string;- author?: { id: string; name: string };+ type: string;+ name?: string;};-};--type ChatNodeMetadata = BaseNodeMetadata & {- nodeType: 'chat';- collaborators: Array<{ id: string; name: string }>;-};--type ChannelNodeMetadata = BaseNodeMetadata & {- nodeType: 'channel';collaborators?: Array<{ id: string; name: string }>;};-type RecordNodeMetadata = BaseNodeMetadata & {- nodeType: 'record';- databaseName?: string;-};--type DatabaseNodeMetadata = BaseNodeMetadata & {- nodeType: 'database';-};--type PageNodeMetadata = BaseNodeMetadata & {- nodeType: 'page';- parentName?: string;-};--type FileNodeMetadata = BaseNodeMetadata & {- nodeType: 'file';-};--type NodeMetadata = {+export type NodeMetadata = {type: 'node';- node:- | MessageNodeMetadata- | ChatNodeMetadata- | ChannelNodeMetadata- | RecordNodeMetadata- | DatabaseNodeMetadata- | PageNodeMetadata- | FileNodeMetadata;+ metadata: BaseMetadata & {+ fields?: Record<string, unknown> | null;+ };};-type DocumentMetadata = {+export type DocumentMetadata = {type: 'document';- document: {- id: string;+ metadata: BaseMetadata & {content: DocumentContent;- createdAt: Date;- nodeType?: string;- nodeName?: string;- parentNodeName?: string;};};@@ -104,6 +55,7 @@ export class ChunkingService {const docs = await splitter.createDocuments([text]);let chunks = docs.map((doc) => doc.pageContent);chunks = chunks.filter((c) => c.trim().length > 10);+if (configuration.ai.chunking.enhanceWithContext) {const enrichedMetadata = await this.fetchMetadata(metadata);const enriched: string[] = [];@@ -125,301 +77,173 @@ export class ChunkingService {}if (metadata.type === 'node') {- const node = await database+ const node = (await database.selectFrom('nodes').selectAll().where('id', '=', metadata.id)- .executeTakeFirst();+ .executeTakeFirst()) as SelectNode | undefined;if (!node) {return undefined;}- const attributes = node.attributes as NodeAttributes;- const author = await database- .selectFrom('users')- .select(['id', 'name'])- .where('id', '=', node.created_by)- .executeTakeFirst();-- const baseMetadata: BaseNodeMetadata = {- id: node.id,- type: attributes.type,- attributes,- createdAt: node.created_at,- createdBy: node.created_by,- author: author ?? undefined,- };-- switch (attributes.type) {- case 'message': {- const messageMetadata: MessageNodeMetadata = {- ...baseMetadata,- nodeType: 'message',- };-- // If message has a reference, fetch it- if (attributes.referenceId) {- const referencedNode = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', attributes.referenceId)- .executeTakeFirst();-- if (- referencedNode &&- referencedNode.attributes.type === 'message'- ) {- const refAttributes =- referencedNode.attributes as MessageAttributes;- const refAuthor = await database- .selectFrom('users')- .select(['id', 'name'])- .where('id', '=', referencedNode.created_by)- .executeTakeFirst();+ return this.buildNodeMetadata(node);+ } else {+ const document = (await database+ .selectFrom('documents')+ .selectAll()+ .where('id', '=', metadata.id)+ .executeTakeFirst()) as SelectDocument | undefined;+ if (!document) {+ return undefined;+ }- messageMetadata.referencedMessage = {- id: referencedNode.id,- content:- extractBlockTexts(referencedNode.id, refAttributes.content) ??- '',- author: refAuthor ?? undefined,- };- }- }+ const node = (await database+ .selectFrom('nodes')+ .selectAll()+ .where('id', '=', document.id)+ .executeTakeFirst()) as SelectNode | undefined;- // Get parent context (chat or channel) if available- if (node.parent_id) {- const parentNode = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', node.parent_id)- .executeTakeFirst();+ return this.buildDocumentMetadata(document, node);+ }+ }- if (parentNode) {- switch (parentNode.attributes.type) {- case 'chat': {- const chatAttributes =- parentNode.attributes as ChatAttributes;- messageMetadata.parentContext = {- type: 'chat',- };+ private async buildNodeMetadata(node: SelectNode): Promise<NodeMetadata> {+ const nodeModel = getNodeModel(node.attributes.type);+ if (!nodeModel) {+ throw new Error(`No model found for node type: ${node.attributes.type}`);+ }- // Fetch chat collaborators- if (chatAttributes.collaborators) {- const collaborators = await database- .selectFrom('users')- .select(['id', 'name'])- .where(- 'id',- 'in',- Object.keys(chatAttributes.collaborators)- )- .execute();- messageMetadata.parentContext.collaborators = collaborators;- }- break;- }- case 'channel': {- const channelAttributes =- parentNode.attributes as ChannelAttributes;- messageMetadata.parentContext = {- type: 'channel',- name: channelAttributes.name,- };+ const baseMetadata = await this.buildBaseMetadata(node);- // Fetch channel collaborators if they exist- if (- 'collaborators' in channelAttributes &&- channelAttributes.collaborators- ) {- const collaborators = await database- .selectFrom('users')- .select(['id', 'name'])- .where(- 'id',- 'in',- Object.keys(channelAttributes.collaborators)- )- .execute();- messageMetadata.parentContext.collaborators = collaborators;- }- break;- }- }+ // Add collaborators if the node type supports them+ if ('collaborators' in node.attributes) {+ baseMetadata.collaborators = await this.fetchCollaborators(+ Object.keys(+ (+ node.attributes as NodeAttributes & {+ collaborators: Record<string, string>;}- }-- return { type: 'node', node: messageMetadata };- }-- case 'chat': {- const chatAttributes = attributes as ChatAttributes;- let collaborators: Array<{ id: string; name: string }> = [];- if (chatAttributes.collaborators) {- collaborators = await database- .selectFrom('users')- .select(['id', 'name'])- .where('id', 'in', Object.keys(chatAttributes.collaborators))- .execute();- }-- return {- type: 'node',- node: {- ...baseMetadata,- nodeType: 'chat',- collaborators,- },- };- }-- case 'channel': {- const channelAttributes = attributes as ChannelAttributes;- let collaborators: Array<{ id: string; name: string }> | undefined;-- // Only fetch collaborators if the channel has them- if (- 'collaborators' in channelAttributes &&- channelAttributes.collaborators- ) {- collaborators = await database- .selectFrom('users')- .select(['id', 'name'])- .where('id', 'in', Object.keys(channelAttributes.collaborators))- .execute();- }-- return {- type: 'node',- node: {- ...baseMetadata,- nodeType: 'channel',- collaborators,- },- };- }-- case 'record': {- const recordAttributes = attributes as RecordAttributes;- const recordMetadata: RecordNodeMetadata = {- ...baseMetadata,- nodeType: 'record',- };-- // Fetch database name- const databaseNode = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', recordAttributes.databaseId)- .executeTakeFirst();+ ).collaborators+ )+ );+ }- if (databaseNode?.attributes.type === 'database') {- const dbAttributes = databaseNode.attributes as DatabaseAttributes;- recordMetadata.databaseName = dbAttributes.name;- }+ // Add parent context if needed+ if (node.parent_id) {+ const parentContext = await this.buildParentContext(node);+ if (parentContext) {+ baseMetadata.parentContext = parentContext;+ }+ }- return { type: 'node', node: recordMetadata };- }+ return {+ type: 'node',+ metadata: {+ ...baseMetadata,+ fields: 'fields' in node.attributes ? node.attributes.fields : null,+ },+ };+ }- case 'database': {- return {- type: 'node',- node: {- ...baseMetadata,- nodeType: 'database',- },- };+ private async buildDocumentMetadata(+ document: SelectDocument,+ node?: SelectNode+ ): Promise<DocumentMetadata> {+ let baseMetadata: BaseMetadata = {+ id: document.id,+ type: 'document',+ createdAt: document.created_at,+ createdBy: document.created_by,+ };++ if (node) {+ const nodeModel = getNodeModel(node.attributes.type);+ if (nodeModel) {+ const nodeName = nodeModel.getName(node.id, node.attributes);+ if (nodeName) {+ baseMetadata.name = nodeName;}- case 'page': {- const pageMetadata: PageNodeMetadata = {- ...baseMetadata,- nodeType: 'page',- };-- // Get parent folder/space name for context- if (node.parent_id) {- const parentNode = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', node.parent_id)- .executeTakeFirst();-- if (parentNode && 'name' in parentNode.attributes) {- pageMetadata.parentName = parentNode.attributes.name;- }+ // Add parent context if available+ if (node.parent_id) {+ const parentContext = await this.buildParentContext(node);+ if (parentContext) {+ baseMetadata.parentContext = parentContext;}-- return { type: 'node', node: pageMetadata };}+ }+ }- case 'file': {- return {- type: 'node',- node: {- ...baseMetadata,- nodeType: 'file',- },- };- }+ return {+ type: 'document',+ metadata: {+ ...baseMetadata,+ content: document.content,+ },+ };+ }- default:- return {- type: 'node',- node: {- ...baseMetadata,- nodeType: attributes.type as any,- },- };- }- } else {- // For documents, fetch both document and its associated node- const document = await database- .selectFrom('documents')- .selectAll()- .where('id', '=', metadata.id)- .executeTakeFirst();- if (!document) {- return undefined;- }+ private async buildBaseMetadata(node: SelectNode): Promise<BaseMetadata> {+ const nodeModel = getNodeModel(node.attributes.type);+ const nodeName = nodeModel?.getName(node.id, node.attributes);++ const author = (await database+ .selectFrom('users')+ .select(['id', 'name'])+ .where('id', '=', node.created_by)+ .executeTakeFirst()) as SelectUser | undefined;++ return {+ id: node.id,+ type: node.attributes.type,+ name: nodeName ?? undefined,+ createdAt: node.created_at,+ createdBy: node.created_by,+ author: author ?? undefined,+ };+ }- const documentMetadata: DocumentMetadata = {- type: 'document',- document: {- id: document.id,- content: document.content,- createdAt: document.created_at,- },- };+ private async buildParentContext(+ node: SelectNode+ ): Promise<BaseMetadata['parentContext'] | undefined> {+ const parentNode = (await database+ .selectFrom('nodes')+ .selectAll()+ .where('id', '=', node.parent_id)+ .executeTakeFirst()) as SelectNode | undefined;- // Try to fetch associated node for additional context- const node = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', document.id)- .executeTakeFirst();+ if (!parentNode) {+ return undefined;+ }- if (node) {- documentMetadata.document.nodeType = node.attributes.type;- if ('name' in node.attributes) {- documentMetadata.document.nodeName = node.attributes.name;- }+ const parentModel = getNodeModel(parentNode.attributes.type);+ if (!parentModel) {+ return undefined;+ }- if (node.parent_id) {- const parentNode = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', node.parent_id)- .executeTakeFirst();+ const parentName = parentModel.getName(+ parentNode.id,+ parentNode.attributes+ );- if (parentNode && 'name' in parentNode.attributes) {- documentMetadata.document.parentNodeName =- parentNode.attributes.name;- }- }- }+ return {+ id: parentNode.id,+ type: parentNode.attributes.type,+ name: parentName ?? undefined,+ };+ }- return documentMetadata;+ private async fetchCollaborators(+ collaboratorIds: string[]+ ): Promise<Array<{ id: string; name: string }>> {+ if (!collaboratorIds.length) {+ return [];}++ return database+ .selectFrom('users')+ .select(['id', 'name'])+ .where('id', 'in', collaboratorIds)+ .execute() as Promise<Array<{ id: string; name: string }>>;}}diff --git a/apps/server/src/services/llm-service.ts b/apps/server/src/services/llm-service.tsindex f4df156f..d6723335 100644--- a/apps/server/src/services/llm-service.ts+++ b/apps/server/src/services/llm-service.ts@@ -7,7 +7,12 @@ import { HumanMessage } from '@langchain/core/messages';import { configuration } from '@/lib/configuration';import { Document } from '@langchain/core/documents';import { z } from 'zod';-import { NodeAttributes } from '@colanode/core';+import { NodeAttributes, NodeType } from '@colanode/core';+import type {+ ChunkingMetadata,+ NodeMetadata,+ DocumentMetadata,+} from '@/services/chunking-service';// Use proper Zod schemas and updated prompt templates@@ -237,38 +242,162 @@ export async function assessUserIntent(: 'retrieve';}+interface NodeContextData {+ metadata: {+ type: NodeType;+ name?: string;+ author?: { id: string; name: string };+ parentContext?: {+ type: string;+ name?: string;+ };+ collaborators?: Array<{ id: string; name: string }>;+ fields?: Record<string, unknown>;+ };+}++const getNodeContextPrompt = (metadata: NodeMetadata): string => {+ const basePrompt = `Given the following context about a {nodeType}:+Name: {name}+Created by: {authorName}+{additionalContext}++Full content:+{fullText}++Current chunk:+{chunk}++Generate a brief (50-100 tokens) contextual prefix that:+1. Explains what this chunk is part of+2. Provides relevant context from the metadata+3. Makes the chunk more understandable in isolation+Do not repeat the chunk content. Return only the contextual prefix.`;++ const getCollaboratorNames = (+ collaborators?: Array<{ id: string; name: string }>+ ) => collaborators?.map((c) => c.name).join(', ') ?? 'unknown';++ switch (metadata.metadata.type) {+ case 'message':+ return basePrompt.replace(+ '{additionalContext}',+ `In: ${metadata.metadata.parentContext?.type ?? 'unknown'} "${metadata.metadata.parentContext?.name ?? 'unknown'}"+Participants: ${getCollaboratorNames(metadata.metadata.collaborators)}`+ );++ case 'record':+ return basePrompt.replace(+ '{additionalContext}',+ `Database: ${metadata.metadata.parentContext?.name ?? 'unknown'}+Fields: ${Object.keys(metadata.metadata.fields ?? {}).join(', ')}`+ );++ case 'page':+ return basePrompt.replace(+ '{additionalContext}',+ `Location: ${metadata.metadata.parentContext?.name ? `in ${metadata.metadata.parentContext.name}` : 'root level'}`+ );++ case 'database':+ return basePrompt.replace(+ '{additionalContext}',+ `Fields: ${Object.keys(metadata.metadata.fields ?? {}).join(', ')}`+ );++ case 'channel':+ return basePrompt.replace(+ '{additionalContext}',+ `Type: Channel+Members: ${getCollaboratorNames(metadata.metadata.collaborators)}`+ );++ default:+ return basePrompt.replace('{additionalContext}', '');+ }+};++interface PromptVariables {+ nodeType: string;+ name: string;+ authorName: string;+ fullText: string;+ chunk: string;+ [key: string]: string;+}++const documentContextPrompt = PromptTemplate.fromTemplate(+ `Given the following context about a document:+Type: {nodeType}+Name: {name}+Parent: {parentName}+Created by: {authorName}++Full content:+{fullText}++Current chunk:+{chunk}++Generate a brief (50-100 tokens) contextual prefix that:+1. Explains what this document is+2. Provides relevant context about its location and purpose+3. Makes the chunk more understandable in isolation+Do not repeat the chunk content. Return only the contextual prefix.`+);+export async function addContextToChunk(chunk: string,fullText: string,- metadata?: any+ metadata?: ChunkingMetadata): Promise<string> {try {+ if (!metadata) {+ return chunk;+ }+const task = 'contextEnhancer';const model = getChatModel(task);- // Choose a prompt variant based on metadata type (node/document) if available.- const promptTemplate = PromptTemplate.fromTemplate(- `Using the following context information:-{contextInfo}-Full content:-{fullText}+ let prompt: string;+ let promptVars: PromptVariables;-Given this chunk:-{chunk}+ if (metadata.type === 'node') {+ prompt = getNodeContextPrompt(metadata);+ promptVars = {+ nodeType: metadata.metadata.type,+ name: metadata.metadata.name ?? 'Untitled',+ authorName: metadata.metadata.author?.name ?? 'Unknown',+ fullText,+ chunk,+ };+ } else {+ prompt = await documentContextPrompt.format({+ nodeType: metadata.metadata.type,+ name: metadata.metadata.name ?? 'Untitled',+ parentName: metadata.metadata.parentContext?.name ?? 'Unknown',+ authorName: metadata.metadata.author?.name ?? 'Unknown',+ fullText,+ chunk,+ });+ promptVars = {+ nodeType: metadata.metadata.type,+ name: metadata.metadata.name ?? 'Untitled',+ authorName: metadata.metadata.author?.name ?? 'Unknown',+ fullText,+ chunk,+ };+ }-Generate a short (50–100 tokens) contextual prefix (do not repeat the chunk) and prepend it to the chunk.`+ const formattedPrompt = Object.entries(promptVars).reduce(+ (acc, [key, value]) => acc.replace(`{${key}}`, value),+ prompt);- const contextInfo = metadata- ? `Type: ${metadata.type}, Name: ${metadata.name || 'N/A'}, Parent: ${metadata.parentName || 'N/A'}, Space: ${metadata.spaceName || 'N/A'}`- : 'No additional context available.';- const prompt = await promptTemplate.format({- contextInfo,- fullText,- chunk,- });+const response = await model.invoke([- new HumanMessage({ content: prompt }),+ new HumanMessage({ content: formattedPrompt }),]);+const prefix = (response.content.toString() || '').trim();return prefix ? `${prefix}\n\n${chunk}` : chunk;} catch (err) {