@contractspec/lib.knowledge 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/access/guard.d.ts +3 -0
  2. package/dist/access/guard.js +290 -5
  3. package/dist/access/index.js +290 -5
  4. package/dist/i18n/catalogs/en.d.ts +8 -0
  5. package/dist/i18n/catalogs/en.js +107 -0
  6. package/dist/i18n/catalogs/es.d.ts +6 -0
  7. package/dist/i18n/catalogs/es.js +80 -0
  8. package/dist/i18n/catalogs/fr.d.ts +6 -0
  9. package/dist/i18n/catalogs/fr.js +80 -0
  10. package/dist/i18n/catalogs/index.d.ts +8 -0
  11. package/dist/i18n/catalogs/index.js +263 -0
  12. package/dist/i18n/i18n.test.d.ts +1 -0
  13. package/dist/i18n/index.d.ts +27 -0
  14. package/dist/i18n/index.js +321 -0
  15. package/dist/i18n/keys.d.ts +74 -0
  16. package/dist/i18n/keys.js +33 -0
  17. package/dist/i18n/locale.d.ts +8 -0
  18. package/dist/i18n/locale.js +14 -0
  19. package/dist/i18n/messages.d.ts +14 -0
  20. package/dist/i18n/messages.js +275 -0
  21. package/dist/index.js +316 -23
  22. package/dist/ingestion/gmail-adapter.d.ts +2 -1
  23. package/dist/ingestion/gmail-adapter.js +284 -8
  24. package/dist/ingestion/index.js +284 -8
  25. package/dist/node/access/guard.js +290 -5
  26. package/dist/node/access/index.js +290 -5
  27. package/dist/node/i18n/catalogs/en.js +106 -0
  28. package/dist/node/i18n/catalogs/es.js +79 -0
  29. package/dist/node/i18n/catalogs/fr.js +79 -0
  30. package/dist/node/i18n/catalogs/index.js +262 -0
  31. package/dist/node/i18n/index.js +320 -0
  32. package/dist/node/i18n/keys.js +32 -0
  33. package/dist/node/i18n/locale.js +13 -0
  34. package/dist/node/i18n/messages.js +274 -0
  35. package/dist/node/index.js +316 -23
  36. package/dist/node/ingestion/gmail-adapter.js +284 -8
  37. package/dist/node/ingestion/index.js +284 -8
  38. package/dist/node/query/index.js +282 -10
  39. package/dist/node/query/service.js +282 -10
  40. package/dist/query/index.js +282 -10
  41. package/dist/query/service.d.ts +3 -0
  42. package/dist/query/service.js +282 -10
  43. package/dist/types.d.ts +2 -0
  44. package/package.json +125 -5
@@ -1,15 +1,287 @@
1
1
  // @bun
2
+ // src/i18n/catalogs/en.ts
3
+ import { defineTranslation } from "@contractspec/lib.contracts-spec/translations";
4
+ var enMessages = defineTranslation({
5
+ meta: {
6
+ key: "knowledge.messages",
7
+ version: "1.0.0",
8
+ domain: "knowledge",
9
+ description: "All user-facing and LLM-facing strings for the knowledge package",
10
+ owners: ["platform"],
11
+ stability: "experimental"
12
+ },
13
+ locale: "en",
14
+ fallback: "en",
15
+ messages: {
16
+ "access.notBound": {
17
+ value: 'Knowledge space "{spaceKey}" is not bound in the resolved app config.',
18
+ description: "Denial reason when a knowledge space is not bound",
19
+ placeholders: [{ name: "spaceKey", type: "string" }]
20
+ },
21
+ "access.readOnly": {
22
+ value: 'Knowledge space "{spaceKey}" is category "{category}" and is read-only.',
23
+ description: "Denial reason when write is attempted on a read-only space",
24
+ placeholders: [
25
+ { name: "spaceKey", type: "string" },
26
+ { name: "category", type: "string" }
27
+ ]
28
+ },
29
+ "access.workflowUnauthorized": {
30
+ value: 'Workflow "{workflowName}" is not authorized to access knowledge space "{spaceKey}".',
31
+ description: "Denial reason when a workflow lacks space access",
32
+ placeholders: [
33
+ { name: "workflowName", type: "string" },
34
+ { name: "spaceKey", type: "string" }
35
+ ]
36
+ },
37
+ "access.agentUnauthorized": {
38
+ value: 'Agent "{agentName}" is not authorized to access knowledge space "{spaceKey}".',
39
+ description: "Denial reason when an agent lacks space access",
40
+ placeholders: [
41
+ { name: "agentName", type: "string" },
42
+ { name: "spaceKey", type: "string" }
43
+ ]
44
+ },
45
+ "access.ephemeralWarning": {
46
+ value: 'Knowledge space "{spaceKey}" is ephemeral; results may be transient.',
47
+ description: "Warning for ephemeral knowledge spaces",
48
+ placeholders: [{ name: "spaceKey", type: "string" }]
49
+ },
50
+ "query.systemPrompt": {
51
+ value: "You are a knowledge assistant that answers questions using the provided context. Cite relevant sources if possible.",
52
+ description: "Default LLM system prompt for knowledge queries"
53
+ },
54
+ "query.userMessage": {
55
+ value: `Question:
56
+ {question}
57
+
58
+ Context:
59
+ {context}`,
60
+ description: "User message template combining question and context",
61
+ placeholders: [
62
+ { name: "question", type: "string" },
63
+ { name: "context", type: "string" }
64
+ ]
65
+ },
66
+ "query.noResults": {
67
+ value: "No relevant documents found.",
68
+ description: "Displayed when vector search returns zero results"
69
+ },
70
+ "query.sourceLabel": {
71
+ value: "Source {index} (score: {score}):",
72
+ description: "Label prefix for each source in the context block",
73
+ placeholders: [
74
+ { name: "index", type: "number" },
75
+ { name: "score", type: "string" }
76
+ ]
77
+ },
78
+ "ingestion.gmail.subject": {
79
+ value: "Subject: {subject}",
80
+ description: "Gmail thread subject label",
81
+ placeholders: [{ name: "subject", type: "string" }]
82
+ },
83
+ "ingestion.gmail.snippet": {
84
+ value: "Snippet: {snippet}",
85
+ description: "Gmail thread snippet label",
86
+ placeholders: [{ name: "snippet", type: "string" }]
87
+ },
88
+ "ingestion.gmail.from": {
89
+ value: "From: {from}",
90
+ description: "Gmail message sender label",
91
+ placeholders: [{ name: "from", type: "string" }]
92
+ },
93
+ "ingestion.gmail.to": {
94
+ value: "To: {to}",
95
+ description: "Gmail message recipients label",
96
+ placeholders: [{ name: "to", type: "string" }]
97
+ },
98
+ "ingestion.gmail.date": {
99
+ value: "Date: {date}",
100
+ description: "Gmail message date label",
101
+ placeholders: [{ name: "date", type: "string" }]
102
+ }
103
+ }
104
+ });
105
+
106
+ // src/i18n/catalogs/fr.ts
107
+ import { defineTranslation as defineTranslation2 } from "@contractspec/lib.contracts-spec/translations";
108
+ var frMessages = defineTranslation2({
109
+ meta: {
110
+ key: "knowledge.messages",
111
+ version: "1.0.0",
112
+ domain: "knowledge",
113
+ description: "All user-facing and LLM-facing strings for the knowledge package (French)",
114
+ owners: ["platform"],
115
+ stability: "experimental"
116
+ },
117
+ locale: "fr",
118
+ fallback: "en",
119
+ messages: {
120
+ "access.notBound": {
121
+ value: `L'espace de connaissances "{spaceKey}" n'est pas li\xE9 dans la configuration de l'application.`,
122
+ description: "Denial reason when a knowledge space is not bound"
123
+ },
124
+ "access.readOnly": {
125
+ value: `L'espace de connaissances "{spaceKey}" est de cat\xE9gorie "{category}" et est en lecture seule.`,
126
+ description: "Denial reason when write is attempted on a read-only space"
127
+ },
128
+ "access.workflowUnauthorized": {
129
+ value: `Le workflow "{workflowName}" n'est pas autoris\xE9 \xE0 acc\xE9der \xE0 l'espace de connaissances "{spaceKey}".`,
130
+ description: "Denial reason when a workflow lacks space access"
131
+ },
132
+ "access.agentUnauthorized": {
133
+ value: `L'agent "{agentName}" n'est pas autoris\xE9 \xE0 acc\xE9der \xE0 l'espace de connaissances "{spaceKey}".`,
134
+ description: "Denial reason when an agent lacks space access"
135
+ },
136
+ "access.ephemeralWarning": {
137
+ value: `L'espace de connaissances "{spaceKey}" est \xE9ph\xE9m\xE8re ; les r\xE9sultats peuvent \xEAtre transitoires.`,
138
+ description: "Warning for ephemeral knowledge spaces"
139
+ },
140
+ "query.systemPrompt": {
141
+ value: "Vous \xEAtes un assistant de connaissances qui r\xE9pond aux questions en utilisant le contexte fourni. Citez les sources pertinentes si possible.",
142
+ description: "Default LLM system prompt for knowledge queries"
143
+ },
144
+ "query.userMessage": {
145
+ value: `Question :
146
+ {question}
147
+
148
+ Contexte :
149
+ {context}`,
150
+ description: "User message template combining question and context"
151
+ },
152
+ "query.noResults": {
153
+ value: "Aucun document pertinent trouv\xE9.",
154
+ description: "Displayed when vector search returns zero results"
155
+ },
156
+ "query.sourceLabel": {
157
+ value: "Source {index} (score : {score}) :",
158
+ description: "Label prefix for each source in the context block"
159
+ },
160
+ "ingestion.gmail.subject": {
161
+ value: "Objet : {subject}",
162
+ description: "Gmail thread subject label"
163
+ },
164
+ "ingestion.gmail.snippet": {
165
+ value: "Extrait : {snippet}",
166
+ description: "Gmail thread snippet label"
167
+ },
168
+ "ingestion.gmail.from": {
169
+ value: "De : {from}",
170
+ description: "Gmail message sender label"
171
+ },
172
+ "ingestion.gmail.to": {
173
+ value: "\xC0 : {to}",
174
+ description: "Gmail message recipients label"
175
+ },
176
+ "ingestion.gmail.date": {
177
+ value: "Date : {date}",
178
+ description: "Gmail message date label"
179
+ }
180
+ }
181
+ });
182
+
183
+ // src/i18n/catalogs/es.ts
184
+ import { defineTranslation as defineTranslation3 } from "@contractspec/lib.contracts-spec/translations";
185
+ var esMessages = defineTranslation3({
186
+ meta: {
187
+ key: "knowledge.messages",
188
+ version: "1.0.0",
189
+ domain: "knowledge",
190
+ description: "All user-facing and LLM-facing strings for the knowledge package (Spanish)",
191
+ owners: ["platform"],
192
+ stability: "experimental"
193
+ },
194
+ locale: "es",
195
+ fallback: "en",
196
+ messages: {
197
+ "access.notBound": {
198
+ value: 'El espacio de conocimiento "{spaceKey}" no est\xE1 vinculado en la configuraci\xF3n de la aplicaci\xF3n.',
199
+ description: "Denial reason when a knowledge space is not bound"
200
+ },
201
+ "access.readOnly": {
202
+ value: 'El espacio de conocimiento "{spaceKey}" es de categor\xEDa "{category}" y es de solo lectura.',
203
+ description: "Denial reason when write is attempted on a read-only space"
204
+ },
205
+ "access.workflowUnauthorized": {
206
+ value: 'El flujo de trabajo "{workflowName}" no est\xE1 autorizado para acceder al espacio de conocimiento "{spaceKey}".',
207
+ description: "Denial reason when a workflow lacks space access"
208
+ },
209
+ "access.agentUnauthorized": {
210
+ value: 'El agente "{agentName}" no est\xE1 autorizado para acceder al espacio de conocimiento "{spaceKey}".',
211
+ description: "Denial reason when an agent lacks space access"
212
+ },
213
+ "access.ephemeralWarning": {
214
+ value: 'El espacio de conocimiento "{spaceKey}" es ef\xEDmero; los resultados pueden ser transitorios.',
215
+ description: "Warning for ephemeral knowledge spaces"
216
+ },
217
+ "query.systemPrompt": {
218
+ value: "Eres un asistente de conocimiento que responde preguntas utilizando el contexto proporcionado. Cita las fuentes relevantes si es posible.",
219
+ description: "Default LLM system prompt for knowledge queries"
220
+ },
221
+ "query.userMessage": {
222
+ value: `Pregunta:
223
+ {question}
224
+
225
+ Contexto:
226
+ {context}`,
227
+ description: "User message template combining question and context"
228
+ },
229
+ "query.noResults": {
230
+ value: "No se encontraron documentos relevantes.",
231
+ description: "Displayed when vector search returns zero results"
232
+ },
233
+ "query.sourceLabel": {
234
+ value: "Fuente {index} (puntuaci\xF3n: {score}):",
235
+ description: "Label prefix for each source in the context block"
236
+ },
237
+ "ingestion.gmail.subject": {
238
+ value: "Asunto: {subject}",
239
+ description: "Gmail thread subject label"
240
+ },
241
+ "ingestion.gmail.snippet": {
242
+ value: "Extracto: {snippet}",
243
+ description: "Gmail thread snippet label"
244
+ },
245
+ "ingestion.gmail.from": {
246
+ value: "De: {from}",
247
+ description: "Gmail message sender label"
248
+ },
249
+ "ingestion.gmail.to": {
250
+ value: "Para: {to}",
251
+ description: "Gmail message recipients label"
252
+ },
253
+ "ingestion.gmail.date": {
254
+ value: "Fecha: {date}",
255
+ description: "Gmail message date label"
256
+ }
257
+ }
258
+ });
259
+
260
+ // src/i18n/messages.ts
261
+ import {
262
+ createI18nFactory
263
+ } from "@contractspec/lib.contracts-spec/translations";
264
+ var factory = createI18nFactory({
265
+ specKey: "knowledge.messages",
266
+ catalogs: [enMessages, frMessages, esMessages]
267
+ });
268
+ var createKnowledgeI18n = factory.create;
269
+ var getDefaultI18n = factory.getDefault;
270
+ var resetI18nRegistry = factory.resetRegistry;
271
+
2
272
  // src/query/service.ts
3
273
  class KnowledgeQueryService {
4
274
  embeddings;
5
275
  vectorStore;
6
276
  llm;
7
277
  config;
278
+ i18n;
8
279
  constructor(embeddings, vectorStore, llm, config) {
9
280
  this.embeddings = embeddings;
10
281
  this.vectorStore = vectorStore;
11
282
  this.llm = llm;
12
283
  this.config = config;
284
+ this.i18n = config.locale ? createKnowledgeI18n(config.locale) : getDefaultI18n();
13
285
  }
14
286
  async query(question) {
15
287
  const embedding = await this.embeddings.embedQuery(question);
@@ -20,7 +292,7 @@ class KnowledgeQueryService {
20
292
  namespace: this.config.namespace,
21
293
  filter: undefined
22
294
  });
23
- const context = buildContext(results);
295
+ const context = buildContext(results, this.i18n);
24
296
  const messages = this.buildMessages(question, context);
25
297
  const response = await this.llm.chat(messages);
26
298
  return {
@@ -33,7 +305,7 @@ class KnowledgeQueryService {
33
305
  };
34
306
  }
35
307
  buildMessages(question, context) {
36
- const systemPrompt = this.config.systemPrompt ?? "You are a knowledge assistant that answers questions using the provided context. Cite relevant sources if possible.";
308
+ const systemPrompt = this.config.systemPrompt ?? this.i18n.t("query.systemPrompt");
37
309
  return [
38
310
  {
39
311
  role: "system",
@@ -44,24 +316,24 @@ class KnowledgeQueryService {
44
316
  content: [
45
317
  {
46
318
  type: "text",
47
- text: `Question:
48
- ${question}
49
-
50
- Context:
51
- ${context}`
319
+ text: this.i18n.t("query.userMessage", { question, context })
52
320
  }
53
321
  ]
54
322
  }
55
323
  ];
56
324
  }
57
325
  }
58
- function buildContext(results) {
326
+ function buildContext(results, i18n) {
59
327
  if (results.length === 0) {
60
- return "No relevant documents found.";
328
+ return i18n.t("query.noResults");
61
329
  }
62
330
  return results.map((result, index) => {
63
331
  const text = extractText(result);
64
- return `Source ${index + 1} (score: ${result.score.toFixed(3)}):
332
+ const label = i18n.t("query.sourceLabel", {
333
+ index: index + 1,
334
+ score: result.score.toFixed(3)
335
+ });
336
+ return `${label}
65
337
  ${text}`;
66
338
  }).join(`
67
339
 
@@ -6,6 +6,8 @@ export interface KnowledgeQueryConfig {
6
6
  namespace?: string;
7
7
  topK?: number;
8
8
  systemPrompt?: string;
9
+ /** Locale for LLM prompts and context labels */
10
+ locale?: string;
9
11
  }
10
12
  export interface KnowledgeAnswer {
11
13
  answer: string;
@@ -19,6 +21,7 @@ export declare class KnowledgeQueryService {
19
21
  private readonly vectorStore;
20
22
  private readonly llm;
21
23
  private readonly config;
24
+ private readonly i18n;
22
25
  constructor(embeddings: EmbeddingProvider, vectorStore: VectorStoreProvider, llm: LLMProvider, config: KnowledgeQueryConfig);
23
26
  query(question: string): Promise<KnowledgeAnswer>;
24
27
  private buildMessages;
@@ -1,15 +1,287 @@
1
1
  // @bun
2
+ // src/i18n/catalogs/en.ts
3
+ import { defineTranslation } from "@contractspec/lib.contracts-spec/translations";
4
+ var enMessages = defineTranslation({
5
+ meta: {
6
+ key: "knowledge.messages",
7
+ version: "1.0.0",
8
+ domain: "knowledge",
9
+ description: "All user-facing and LLM-facing strings for the knowledge package",
10
+ owners: ["platform"],
11
+ stability: "experimental"
12
+ },
13
+ locale: "en",
14
+ fallback: "en",
15
+ messages: {
16
+ "access.notBound": {
17
+ value: 'Knowledge space "{spaceKey}" is not bound in the resolved app config.',
18
+ description: "Denial reason when a knowledge space is not bound",
19
+ placeholders: [{ name: "spaceKey", type: "string" }]
20
+ },
21
+ "access.readOnly": {
22
+ value: 'Knowledge space "{spaceKey}" is category "{category}" and is read-only.',
23
+ description: "Denial reason when write is attempted on a read-only space",
24
+ placeholders: [
25
+ { name: "spaceKey", type: "string" },
26
+ { name: "category", type: "string" }
27
+ ]
28
+ },
29
+ "access.workflowUnauthorized": {
30
+ value: 'Workflow "{workflowName}" is not authorized to access knowledge space "{spaceKey}".',
31
+ description: "Denial reason when a workflow lacks space access",
32
+ placeholders: [
33
+ { name: "workflowName", type: "string" },
34
+ { name: "spaceKey", type: "string" }
35
+ ]
36
+ },
37
+ "access.agentUnauthorized": {
38
+ value: 'Agent "{agentName}" is not authorized to access knowledge space "{spaceKey}".',
39
+ description: "Denial reason when an agent lacks space access",
40
+ placeholders: [
41
+ { name: "agentName", type: "string" },
42
+ { name: "spaceKey", type: "string" }
43
+ ]
44
+ },
45
+ "access.ephemeralWarning": {
46
+ value: 'Knowledge space "{spaceKey}" is ephemeral; results may be transient.',
47
+ description: "Warning for ephemeral knowledge spaces",
48
+ placeholders: [{ name: "spaceKey", type: "string" }]
49
+ },
50
+ "query.systemPrompt": {
51
+ value: "You are a knowledge assistant that answers questions using the provided context. Cite relevant sources if possible.",
52
+ description: "Default LLM system prompt for knowledge queries"
53
+ },
54
+ "query.userMessage": {
55
+ value: `Question:
56
+ {question}
57
+
58
+ Context:
59
+ {context}`,
60
+ description: "User message template combining question and context",
61
+ placeholders: [
62
+ { name: "question", type: "string" },
63
+ { name: "context", type: "string" }
64
+ ]
65
+ },
66
+ "query.noResults": {
67
+ value: "No relevant documents found.",
68
+ description: "Displayed when vector search returns zero results"
69
+ },
70
+ "query.sourceLabel": {
71
+ value: "Source {index} (score: {score}):",
72
+ description: "Label prefix for each source in the context block",
73
+ placeholders: [
74
+ { name: "index", type: "number" },
75
+ { name: "score", type: "string" }
76
+ ]
77
+ },
78
+ "ingestion.gmail.subject": {
79
+ value: "Subject: {subject}",
80
+ description: "Gmail thread subject label",
81
+ placeholders: [{ name: "subject", type: "string" }]
82
+ },
83
+ "ingestion.gmail.snippet": {
84
+ value: "Snippet: {snippet}",
85
+ description: "Gmail thread snippet label",
86
+ placeholders: [{ name: "snippet", type: "string" }]
87
+ },
88
+ "ingestion.gmail.from": {
89
+ value: "From: {from}",
90
+ description: "Gmail message sender label",
91
+ placeholders: [{ name: "from", type: "string" }]
92
+ },
93
+ "ingestion.gmail.to": {
94
+ value: "To: {to}",
95
+ description: "Gmail message recipients label",
96
+ placeholders: [{ name: "to", type: "string" }]
97
+ },
98
+ "ingestion.gmail.date": {
99
+ value: "Date: {date}",
100
+ description: "Gmail message date label",
101
+ placeholders: [{ name: "date", type: "string" }]
102
+ }
103
+ }
104
+ });
105
+
106
+ // src/i18n/catalogs/fr.ts
107
+ import { defineTranslation as defineTranslation2 } from "@contractspec/lib.contracts-spec/translations";
108
+ var frMessages = defineTranslation2({
109
+ meta: {
110
+ key: "knowledge.messages",
111
+ version: "1.0.0",
112
+ domain: "knowledge",
113
+ description: "All user-facing and LLM-facing strings for the knowledge package (French)",
114
+ owners: ["platform"],
115
+ stability: "experimental"
116
+ },
117
+ locale: "fr",
118
+ fallback: "en",
119
+ messages: {
120
+ "access.notBound": {
121
+ value: `L'espace de connaissances "{spaceKey}" n'est pas li\xE9 dans la configuration de l'application.`,
122
+ description: "Denial reason when a knowledge space is not bound"
123
+ },
124
+ "access.readOnly": {
125
+ value: `L'espace de connaissances "{spaceKey}" est de cat\xE9gorie "{category}" et est en lecture seule.`,
126
+ description: "Denial reason when write is attempted on a read-only space"
127
+ },
128
+ "access.workflowUnauthorized": {
129
+ value: `Le workflow "{workflowName}" n'est pas autoris\xE9 \xE0 acc\xE9der \xE0 l'espace de connaissances "{spaceKey}".`,
130
+ description: "Denial reason when a workflow lacks space access"
131
+ },
132
+ "access.agentUnauthorized": {
133
+ value: `L'agent "{agentName}" n'est pas autoris\xE9 \xE0 acc\xE9der \xE0 l'espace de connaissances "{spaceKey}".`,
134
+ description: "Denial reason when an agent lacks space access"
135
+ },
136
+ "access.ephemeralWarning": {
137
+ value: `L'espace de connaissances "{spaceKey}" est \xE9ph\xE9m\xE8re ; les r\xE9sultats peuvent \xEAtre transitoires.`,
138
+ description: "Warning for ephemeral knowledge spaces"
139
+ },
140
+ "query.systemPrompt": {
141
+ value: "Vous \xEAtes un assistant de connaissances qui r\xE9pond aux questions en utilisant le contexte fourni. Citez les sources pertinentes si possible.",
142
+ description: "Default LLM system prompt for knowledge queries"
143
+ },
144
+ "query.userMessage": {
145
+ value: `Question :
146
+ {question}
147
+
148
+ Contexte :
149
+ {context}`,
150
+ description: "User message template combining question and context"
151
+ },
152
+ "query.noResults": {
153
+ value: "Aucun document pertinent trouv\xE9.",
154
+ description: "Displayed when vector search returns zero results"
155
+ },
156
+ "query.sourceLabel": {
157
+ value: "Source {index} (score : {score}) :",
158
+ description: "Label prefix for each source in the context block"
159
+ },
160
+ "ingestion.gmail.subject": {
161
+ value: "Objet : {subject}",
162
+ description: "Gmail thread subject label"
163
+ },
164
+ "ingestion.gmail.snippet": {
165
+ value: "Extrait : {snippet}",
166
+ description: "Gmail thread snippet label"
167
+ },
168
+ "ingestion.gmail.from": {
169
+ value: "De : {from}",
170
+ description: "Gmail message sender label"
171
+ },
172
+ "ingestion.gmail.to": {
173
+ value: "\xC0 : {to}",
174
+ description: "Gmail message recipients label"
175
+ },
176
+ "ingestion.gmail.date": {
177
+ value: "Date : {date}",
178
+ description: "Gmail message date label"
179
+ }
180
+ }
181
+ });
182
+
183
+ // src/i18n/catalogs/es.ts
184
+ import { defineTranslation as defineTranslation3 } from "@contractspec/lib.contracts-spec/translations";
185
+ var esMessages = defineTranslation3({
186
+ meta: {
187
+ key: "knowledge.messages",
188
+ version: "1.0.0",
189
+ domain: "knowledge",
190
+ description: "All user-facing and LLM-facing strings for the knowledge package (Spanish)",
191
+ owners: ["platform"],
192
+ stability: "experimental"
193
+ },
194
+ locale: "es",
195
+ fallback: "en",
196
+ messages: {
197
+ "access.notBound": {
198
+ value: 'El espacio de conocimiento "{spaceKey}" no est\xE1 vinculado en la configuraci\xF3n de la aplicaci\xF3n.',
199
+ description: "Denial reason when a knowledge space is not bound"
200
+ },
201
+ "access.readOnly": {
202
+ value: 'El espacio de conocimiento "{spaceKey}" es de categor\xEDa "{category}" y es de solo lectura.',
203
+ description: "Denial reason when write is attempted on a read-only space"
204
+ },
205
+ "access.workflowUnauthorized": {
206
+ value: 'El flujo de trabajo "{workflowName}" no est\xE1 autorizado para acceder al espacio de conocimiento "{spaceKey}".',
207
+ description: "Denial reason when a workflow lacks space access"
208
+ },
209
+ "access.agentUnauthorized": {
210
+ value: 'El agente "{agentName}" no est\xE1 autorizado para acceder al espacio de conocimiento "{spaceKey}".',
211
+ description: "Denial reason when an agent lacks space access"
212
+ },
213
+ "access.ephemeralWarning": {
214
+ value: 'El espacio de conocimiento "{spaceKey}" es ef\xEDmero; los resultados pueden ser transitorios.',
215
+ description: "Warning for ephemeral knowledge spaces"
216
+ },
217
+ "query.systemPrompt": {
218
+ value: "Eres un asistente de conocimiento que responde preguntas utilizando el contexto proporcionado. Cita las fuentes relevantes si es posible.",
219
+ description: "Default LLM system prompt for knowledge queries"
220
+ },
221
+ "query.userMessage": {
222
+ value: `Pregunta:
223
+ {question}
224
+
225
+ Contexto:
226
+ {context}`,
227
+ description: "User message template combining question and context"
228
+ },
229
+ "query.noResults": {
230
+ value: "No se encontraron documentos relevantes.",
231
+ description: "Displayed when vector search returns zero results"
232
+ },
233
+ "query.sourceLabel": {
234
+ value: "Fuente {index} (puntuaci\xF3n: {score}):",
235
+ description: "Label prefix for each source in the context block"
236
+ },
237
+ "ingestion.gmail.subject": {
238
+ value: "Asunto: {subject}",
239
+ description: "Gmail thread subject label"
240
+ },
241
+ "ingestion.gmail.snippet": {
242
+ value: "Extracto: {snippet}",
243
+ description: "Gmail thread snippet label"
244
+ },
245
+ "ingestion.gmail.from": {
246
+ value: "De: {from}",
247
+ description: "Gmail message sender label"
248
+ },
249
+ "ingestion.gmail.to": {
250
+ value: "Para: {to}",
251
+ description: "Gmail message recipients label"
252
+ },
253
+ "ingestion.gmail.date": {
254
+ value: "Fecha: {date}",
255
+ description: "Gmail message date label"
256
+ }
257
+ }
258
+ });
259
+
260
+ // src/i18n/messages.ts
261
+ import {
262
+ createI18nFactory
263
+ } from "@contractspec/lib.contracts-spec/translations";
264
+ var factory = createI18nFactory({
265
+ specKey: "knowledge.messages",
266
+ catalogs: [enMessages, frMessages, esMessages]
267
+ });
268
+ var createKnowledgeI18n = factory.create;
269
+ var getDefaultI18n = factory.getDefault;
270
+ var resetI18nRegistry = factory.resetRegistry;
271
+
2
272
  // src/query/service.ts
3
273
  class KnowledgeQueryService {
4
274
  embeddings;
5
275
  vectorStore;
6
276
  llm;
7
277
  config;
278
+ i18n;
8
279
  constructor(embeddings, vectorStore, llm, config) {
9
280
  this.embeddings = embeddings;
10
281
  this.vectorStore = vectorStore;
11
282
  this.llm = llm;
12
283
  this.config = config;
284
+ this.i18n = config.locale ? createKnowledgeI18n(config.locale) : getDefaultI18n();
13
285
  }
14
286
  async query(question) {
15
287
  const embedding = await this.embeddings.embedQuery(question);
@@ -20,7 +292,7 @@ class KnowledgeQueryService {
20
292
  namespace: this.config.namespace,
21
293
  filter: undefined
22
294
  });
23
- const context = buildContext(results);
295
+ const context = buildContext(results, this.i18n);
24
296
  const messages = this.buildMessages(question, context);
25
297
  const response = await this.llm.chat(messages);
26
298
  return {
@@ -33,7 +305,7 @@ class KnowledgeQueryService {
33
305
  };
34
306
  }
35
307
  buildMessages(question, context) {
36
- const systemPrompt = this.config.systemPrompt ?? "You are a knowledge assistant that answers questions using the provided context. Cite relevant sources if possible.";
308
+ const systemPrompt = this.config.systemPrompt ?? this.i18n.t("query.systemPrompt");
37
309
  return [
38
310
  {
39
311
  role: "system",
@@ -44,24 +316,24 @@ class KnowledgeQueryService {
44
316
  content: [
45
317
  {
46
318
  type: "text",
47
- text: `Question:
48
- ${question}
49
-
50
- Context:
51
- ${context}`
319
+ text: this.i18n.t("query.userMessage", { question, context })
52
320
  }
53
321
  ]
54
322
  }
55
323
  ];
56
324
  }
57
325
  }
58
- function buildContext(results) {
326
+ function buildContext(results, i18n) {
59
327
  if (results.length === 0) {
60
- return "No relevant documents found.";
328
+ return i18n.t("query.noResults");
61
329
  }
62
330
  return results.map((result, index) => {
63
331
  const text = extractText(result);
64
- return `Source ${index + 1} (score: ${result.score.toFixed(3)}):
332
+ const label = i18n.t("query.sourceLabel", {
333
+ index: index + 1,
334
+ score: result.score.toFixed(3)
335
+ });
336
+ return `${label}
65
337
  ${text}`;
66
338
  }).join(`
67
339