@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
@@ -5,11 +5,14 @@ export interface KnowledgeAccessGuardOptions {
5
5
  disallowWriteCategories?: KnowledgeCategory[];
6
6
  requireWorkflowBinding?: boolean;
7
7
  requireAgentBinding?: boolean;
8
+ /** Default locale for access denial/warning messages */
9
+ locale?: string;
8
10
  }
9
11
  export declare class KnowledgeAccessGuard {
10
12
  private readonly disallowedWrite;
11
13
  private readonly requireWorkflowBinding;
12
14
  private readonly requireAgentBinding;
15
+ private readonly i18n;
13
16
  constructor(options?: KnowledgeAccessGuardOptions);
14
17
  checkAccess(spaceBinding: ResolvedKnowledge, context: KnowledgeAccessContext, appConfig: ResolvedAppConfig): KnowledgeAccessResult;
15
18
  private isSpaceBound;
@@ -1,4 +1,274 @@
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/access/guard.ts
3
273
  var DEFAULT_DISALLOWED_WRITE = ["external", "ephemeral"];
4
274
 
@@ -6,23 +276,30 @@ class KnowledgeAccessGuard {
6
276
  disallowedWrite;
7
277
  requireWorkflowBinding;
8
278
  requireAgentBinding;
279
+ i18n;
9
280
  constructor(options = {}) {
10
281
  this.disallowedWrite = new Set(options.disallowWriteCategories ?? DEFAULT_DISALLOWED_WRITE);
11
282
  this.requireWorkflowBinding = options.requireWorkflowBinding ?? true;
12
283
  this.requireAgentBinding = options.requireAgentBinding ?? false;
284
+ this.i18n = options.locale ? createKnowledgeI18n(options.locale) : getDefaultI18n();
13
285
  }
14
286
  checkAccess(spaceBinding, context, appConfig) {
15
287
  const { binding, space } = spaceBinding;
16
288
  if (binding.required !== false && !this.isSpaceBound(spaceBinding, appConfig)) {
17
289
  return {
18
290
  allowed: false,
19
- reason: `Knowledge space "${space.meta.key}" is not bound in the resolved app config.`
291
+ reason: this.i18n.t("access.notBound", {
292
+ spaceKey: space.meta.key
293
+ })
20
294
  };
21
295
  }
22
296
  if (context.operation === "write" && this.disallowedWrite.has(space.meta.category)) {
23
297
  return {
24
298
  allowed: false,
25
- reason: `Knowledge space "${space.meta.key}" is category "${space.meta.category}" and is read-only.`
299
+ reason: this.i18n.t("access.readOnly", {
300
+ spaceKey: space.meta.key,
301
+ category: space.meta.category
302
+ })
26
303
  };
27
304
  }
28
305
  if (this.requireWorkflowBinding && context.workflowName) {
@@ -30,7 +307,10 @@ class KnowledgeAccessGuard {
30
307
  if (allowedWorkflows && !allowedWorkflows.includes(context.workflowName)) {
31
308
  return {
32
309
  allowed: false,
33
- reason: `Workflow "${context.workflowName}" is not authorized to access knowledge space "${space.meta.key}".`
310
+ reason: this.i18n.t("access.workflowUnauthorized", {
311
+ workflowName: context.workflowName,
312
+ spaceKey: space.meta.key
313
+ })
34
314
  };
35
315
  }
36
316
  }
@@ -39,7 +319,10 @@ class KnowledgeAccessGuard {
39
319
  if (allowedAgents && !allowedAgents.includes(context.agentName)) {
40
320
  return {
41
321
  allowed: false,
42
- reason: `Agent "${context.agentName}" is not authorized to access knowledge space "${space.meta.key}".`
322
+ reason: this.i18n.t("access.agentUnauthorized", {
323
+ agentName: context.agentName,
324
+ spaceKey: space.meta.key
325
+ })
43
326
  };
44
327
  }
45
328
  }
@@ -47,7 +330,9 @@ class KnowledgeAccessGuard {
47
330
  return {
48
331
  allowed: true,
49
332
  severity: "warning",
50
- reason: `Knowledge space "${space.meta.key}" is ephemeral; results may be transient.`
333
+ reason: this.i18n.t("access.ephemeralWarning", {
334
+ spaceKey: space.meta.key
335
+ })
51
336
  };
52
337
  }
53
338
  return { allowed: true };