cencori 1.8.2 → 1.9.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 (48) hide show
  1. package/README.md +21 -0
  2. package/dist/ai/index.d.mts +14 -1
  3. package/dist/ai/index.d.ts +14 -1
  4. package/dist/ai/index.js +43 -3
  5. package/dist/ai/index.js.map +1 -1
  6. package/dist/ai/index.mjs +43 -3
  7. package/dist/ai/index.mjs.map +1 -1
  8. package/dist/compute/index.d.mts +1 -1
  9. package/dist/compute/index.d.ts +1 -1
  10. package/dist/documents/index.d.mts +1 -1
  11. package/dist/documents/index.d.ts +1 -1
  12. package/dist/index.d.mts +68 -9
  13. package/dist/index.d.ts +68 -9
  14. package/dist/index.js +187 -19
  15. package/dist/index.js.map +1 -1
  16. package/dist/index.mjs +186 -19
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/judgment/index.d.mts +128 -0
  19. package/dist/judgment/index.d.ts +128 -0
  20. package/dist/judgment/index.js +79 -0
  21. package/dist/judgment/index.js.map +1 -0
  22. package/dist/judgment/index.mjs +54 -0
  23. package/dist/judgment/index.mjs.map +1 -0
  24. package/dist/memory/index.d.mts +1 -1
  25. package/dist/memory/index.d.ts +1 -1
  26. package/dist/storage/index.d.mts +5 -7
  27. package/dist/storage/index.d.ts +5 -7
  28. package/dist/storage/index.js +6 -8
  29. package/dist/storage/index.js.map +1 -1
  30. package/dist/storage/index.mjs +6 -8
  31. package/dist/storage/index.mjs.map +1 -1
  32. package/dist/telemetry/index.d.mts +1 -1
  33. package/dist/telemetry/index.d.ts +1 -1
  34. package/dist/{types-CAhDuNTz.d.mts → types-D91o22Gb.d.mts} +28 -1
  35. package/dist/{types-CAhDuNTz.d.ts → types-D91o22Gb.d.ts} +28 -1
  36. package/dist/vercel/index.js +66 -6
  37. package/dist/vercel/index.js.map +1 -1
  38. package/dist/vercel/index.mjs +66 -6
  39. package/dist/vercel/index.mjs.map +1 -1
  40. package/dist/vision/index.d.mts +1 -1
  41. package/dist/vision/index.d.ts +1 -1
  42. package/dist/voice/index.d.mts +1 -1
  43. package/dist/voice/index.d.ts +1 -1
  44. package/dist/web/index.d.mts +1 -1
  45. package/dist/web/index.d.ts +1 -1
  46. package/dist/workflow/index.d.mts +1 -1
  47. package/dist/workflow/index.d.ts +1 -1
  48. package/package.json +6 -1
package/dist/index.js CHANGED
@@ -36,6 +36,7 @@ __export(src_exports, {
36
36
  DocumentsNamespace: () => DocumentsNamespace,
37
37
  EndUsersNamespace: () => EndUsersNamespace,
38
38
  InstallationsNamespace: () => InstallationsNamespace,
39
+ JudgmentNamespace: () => JudgmentNamespace,
39
40
  KnowledgeNamespace: () => KnowledgeNamespace2,
40
41
  MEMORY_FETCH_TOOL: () => MEMORY_FETCH_TOOL,
41
42
  McpServersNamespace: () => McpServersNamespace,
@@ -200,6 +201,25 @@ function throwCencoriError(response, body) {
200
201
  }
201
202
 
202
203
  // src/ai/index.ts
204
+ function toKnowledgeCitations(raw) {
205
+ return (raw ?? []).map((c) => ({
206
+ n: c.n,
207
+ chunkId: c.chunk_id,
208
+ sourceId: c.source_id,
209
+ ord: c.ord ?? null,
210
+ page: c.page ?? null,
211
+ score: c.score
212
+ }));
213
+ }
214
+ function citationsFromHeader(response) {
215
+ const raw = response.headers.get("X-Cencori-Knowledge-Citations");
216
+ if (!raw) return void 0;
217
+ try {
218
+ return toKnowledgeCitations(JSON.parse(raw));
219
+ } catch {
220
+ return void 0;
221
+ }
222
+ }
203
223
  var AINamespace = class {
204
224
  constructor(config) {
205
225
  this.config = config;
@@ -229,7 +249,8 @@ var AINamespace = class {
229
249
  stream: false,
230
250
  tools: request2.tools,
231
251
  toolChoice: request2.toolChoice,
232
- prompt: request2.prompt
252
+ prompt: request2.prompt,
253
+ knowledgeBases: request2.knowledgeBases
233
254
  })
234
255
  });
235
256
  if (!response.ok) {
@@ -261,7 +282,13 @@ var AINamespace = class {
261
282
  costUsd: data.cost_usd,
262
283
  provider: data.provider,
263
284
  requestId: response.headers.get("X-Request-Id"),
264
- safety: data.safety
285
+ safety: data.safety,
286
+ ...data.knowledge ? {
287
+ knowledge: {
288
+ knowledgeBases: data.knowledge.knowledge_bases,
289
+ citations: toKnowledgeCitations(data.knowledge.citations)
290
+ }
291
+ } : {}
265
292
  };
266
293
  }
267
294
  /**
@@ -289,7 +316,8 @@ var AINamespace = class {
289
316
  stream: true,
290
317
  tools: request2.tools,
291
318
  toolChoice: request2.toolChoice,
292
- prompt: request2.prompt
319
+ prompt: request2.prompt,
320
+ knowledgeBases: request2.knowledgeBases
293
321
  })
294
322
  });
295
323
  if (!response.ok) {
@@ -302,6 +330,7 @@ var AINamespace = class {
302
330
  const reader = response.body.getReader();
303
331
  const decoder = new TextDecoder();
304
332
  let buffer = "";
333
+ let citations = citationsFromHeader(response);
305
334
  try {
306
335
  while (true) {
307
336
  const { done, value } = await reader.read();
@@ -318,6 +347,10 @@ var AINamespace = class {
318
347
  }
319
348
  try {
320
349
  const chunk = JSON.parse(data);
350
+ if (citations) {
351
+ chunk.citations = citations;
352
+ citations = void 0;
353
+ }
321
354
  yield chunk;
322
355
  } catch {
323
356
  }
@@ -500,6 +533,10 @@ var AINamespace = class {
500
533
  };
501
534
  }
502
535
  /**
536
+ * @deprecated `/api/ai/rag` is not available on the hosted gateway. Use
537
+ * knowledge bases instead: `cencori.ai.chat({ model, messages,
538
+ * knowledgeBases: ['kb_...'] })` returns the answer with citations.
539
+ *
503
540
  * RAG (Retrieval-Augmented Generation) - Chat with automatic memory context
504
541
  *
505
542
  * Searches your memory namespace for relevant context and includes it
@@ -696,6 +733,10 @@ var AINamespace = class {
696
733
  }
697
734
  }
698
735
  /**
736
+ * @deprecated `/api/ai/rag` is not available on the hosted gateway. Use
737
+ * `cencori.ai.chatStream({ ..., knowledgeBases: ['kb_...'] })`; the first
738
+ * chunk carries `citations`.
739
+ *
699
740
  * Stream RAG responses with automatic memory context
700
741
  *
701
742
  * @example
@@ -1043,6 +1084,57 @@ var VoiceNamespace = class {
1043
1084
  }
1044
1085
  };
1045
1086
 
1087
+ // src/judgment/index.ts
1088
+ var JudgmentNamespace = class {
1089
+ constructor(config) {
1090
+ this.config = config;
1091
+ }
1092
+ /**
1093
+ * Judge a state against typed questions. Returns one answer per
1094
+ * question id with probabilities your code can branch on.
1095
+ */
1096
+ async judge(request2) {
1097
+ if (request2.state === void 0 || request2.state === null) {
1098
+ throw new Error("judgment.judge requires `state`");
1099
+ }
1100
+ if (!request2.questions || Object.keys(request2.questions).length === 0) {
1101
+ throw new Error("judgment.judge requires a non-empty `questions` object");
1102
+ }
1103
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
1104
+ method: "POST",
1105
+ headers: {
1106
+ "CENCORI_API_KEY": this.config.apiKey,
1107
+ "Content-Type": "application/json",
1108
+ ...this.config.headers
1109
+ },
1110
+ body: JSON.stringify(request2)
1111
+ });
1112
+ if (!response.ok) {
1113
+ throw await this.error(response, "judgment.judge");
1114
+ }
1115
+ return await response.json();
1116
+ }
1117
+ /** List the judgment models with their pricing. No API key required. */
1118
+ async listModels() {
1119
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
1120
+ headers: this.config.headers
1121
+ });
1122
+ if (!response.ok) {
1123
+ throw await this.error(response, "judgment.listModels");
1124
+ }
1125
+ return await response.json();
1126
+ }
1127
+ async error(response, op) {
1128
+ const data = await response.json().catch(() => ({}));
1129
+ const message = data && typeof data === "object" && "message" in data && typeof data.message === "string" ? data.message : `${op} failed with status ${response.status}`;
1130
+ const code = data && typeof data === "object" && "error" in data && typeof data.error === "string" ? data.error : "request_failed";
1131
+ const err = new Error(message);
1132
+ err.code = code;
1133
+ err.details = data;
1134
+ return err;
1135
+ }
1136
+ };
1137
+
1046
1138
  // src/documents/index.ts
1047
1139
  function toBody2(request2, extra = {}) {
1048
1140
  const body = {
@@ -1310,23 +1402,21 @@ var VectorsNamespace = class {
1310
1402
  };
1311
1403
  var KnowledgeNamespace = class {
1312
1404
  /**
1313
- * Query the knowledge base
1314
- *
1315
- * 🚧 Coming Soon - This feature is not yet available.
1405
+ * @deprecated Never implemented. Use `cencori.knowledge.search()`, or
1406
+ * ground a chat with `cencori.ai.chat({ knowledgeBases: [...] })`.
1316
1407
  */
1317
1408
  async query(question) {
1318
1409
  throw new Error(
1319
- `cencori.storage.knowledge.query() is coming soon! Join our waitlist at https://cencori.com/memory`
1410
+ `cencori.storage.knowledge.query() is not available. Use cencori.knowledge.search(kbId, { query }), or cencori.ai.chat({ knowledgeBases: [kbId], ... }).`
1320
1411
  );
1321
1412
  }
1322
1413
  /**
1323
- * Add documents to knowledge base
1324
- *
1325
- * 🚧 Coming Soon - This feature is not yet available.
1414
+ * @deprecated Never implemented. Use `cencori.knowledge.addInlineSource()`
1415
+ * or `cencori.knowledge.addFileSource()`.
1326
1416
  */
1327
1417
  async add(documents) {
1328
1418
  throw new Error(
1329
- `cencori.storage.knowledge.add() is coming soon! Join our waitlist at https://cencori.com/memory`
1419
+ `cencori.storage.knowledge.add() is not available. Use cencori.knowledge.addInlineSource(kbId, text) or cencori.knowledge.addFileSource(kbId, file).`
1330
1420
  );
1331
1421
  }
1332
1422
  };
@@ -2095,16 +2185,17 @@ function parseRetryAfterSeconds(value, nowMs = Date.now()) {
2095
2185
  return Math.max(0, Math.ceil((timestamp - nowMs) / 1e3));
2096
2186
  }
2097
2187
  async function request(config, method, path, body, extraHeaders, options) {
2188
+ const isForm = typeof FormData !== "undefined" && body instanceof FormData;
2098
2189
  const response = await fetch(`${config.baseUrl}${path}`, {
2099
2190
  method,
2100
2191
  signal: options?.signal,
2101
2192
  headers: {
2102
2193
  "CENCORI_API_KEY": config.apiKey,
2103
- "Content-Type": "application/json",
2194
+ ...isForm ? {} : { "Content-Type": "application/json" },
2104
2195
  ...config.headers,
2105
2196
  ...extraHeaders
2106
2197
  },
2107
- body: body ? JSON.stringify(body) : void 0
2198
+ body: isForm ? body : body ? JSON.stringify(body) : void 0
2108
2199
  });
2109
2200
  if (!response.ok) {
2110
2201
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
@@ -2354,6 +2445,21 @@ var KnowledgeNamespace2 = class {
2354
2445
  addInlineSource(kbId, text, metadata) {
2355
2446
  return request(this.config, "POST", `/v1/knowledge-bases/${kbId}/sources`, { text, metadata });
2356
2447
  }
2448
+ /**
2449
+ * Upload a file (PDF, DOCX, Markdown, HTML, CSV, JSON or plain text, up
2450
+ * to 8MB). It is extracted, chunked and embedded server-side.
2451
+ *
2452
+ * @example
2453
+ * const pdf = new Blob([await fs.readFile('handbook.pdf')], { type: 'application/pdf' });
2454
+ * await cencori.knowledge.addFileSource(kb.id, pdf, { filename: 'handbook.pdf' });
2455
+ */
2456
+ addFileSource(kbId, file, options) {
2457
+ const form = new FormData();
2458
+ const filename = options?.filename ?? file.name ?? "upload";
2459
+ form.append("file", file, filename);
2460
+ if (options?.metadata) form.append("metadata", JSON.stringify(options.metadata));
2461
+ return request(this.config, "POST", `/v1/knowledge-bases/${kbId}/sources`, form);
2462
+ }
2357
2463
  listSources(kbId) {
2358
2464
  return request(this.config, "GET", `/v1/knowledge-bases/${kbId}/sources`);
2359
2465
  }
@@ -2614,6 +2720,7 @@ var Cencori = class {
2614
2720
  this.chat = new ChatNamespace(this.config);
2615
2721
  this.vision = new VisionNamespace(this.config);
2616
2722
  this.voice = new VoiceNamespace(this.config);
2723
+ this.judgment = new JudgmentNamespace(this.config);
2617
2724
  this.documents = new DocumentsNamespace(this.config);
2618
2725
  this.agents = new AgentsNamespace(this.config);
2619
2726
  this.compute = new ComputeNamespace();
@@ -2765,6 +2872,44 @@ function providerLogoUrl(providerId, baseUrl = DEFAULT_CATALOG_BASE_URL) {
2765
2872
  }
2766
2873
 
2767
2874
  // src/vercel/cencori-chat-model.ts
2875
+ function extractStreamError(chunk) {
2876
+ const error = chunk.error;
2877
+ if (typeof error === "string" && error) return error;
2878
+ if (error && typeof error === "object") {
2879
+ const record = error;
2880
+ if (typeof record.message === "string" && record.message) {
2881
+ return record.message;
2882
+ }
2883
+ try {
2884
+ return JSON.stringify(error);
2885
+ } catch {
2886
+ return "Provider stream error";
2887
+ }
2888
+ }
2889
+ if (typeof chunk.message === "string" && chunk.message && chunk.delta === void 0 && chunk.finish_reason === void 0 && chunk.tool_calls === void 0 && chunk.toolCalls === void 0 && chunk.usage === void 0) {
2890
+ return chunk.message;
2891
+ }
2892
+ return void 0;
2893
+ }
2894
+ function extractHttpError(body, fallback) {
2895
+ if (!body || typeof body !== "object") return fallback;
2896
+ const record = body;
2897
+ if (typeof record.message === "string" && record.message) return record.message;
2898
+ const error = record.error;
2899
+ if (typeof error === "string" && error) {
2900
+ return typeof record.message === "string" && record.message ? record.message : error;
2901
+ }
2902
+ if (error && typeof error === "object") {
2903
+ const nested = error;
2904
+ if (typeof nested.message === "string" && nested.message) return nested.message;
2905
+ try {
2906
+ return JSON.stringify(error);
2907
+ } catch {
2908
+ return fallback;
2909
+ }
2910
+ }
2911
+ return fallback;
2912
+ }
2768
2913
  var CencoriChatLanguageModel = class {
2769
2914
  constructor(modelId, settings) {
2770
2915
  this.specificationVersion = "v3";
@@ -2903,8 +3048,10 @@ var CencoriChatLanguageModel = class {
2903
3048
  signal: options.abortSignal
2904
3049
  });
2905
3050
  if (!response.ok) {
2906
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
2907
- throw new Error(`Cencori API error: ${error.error || response.statusText}`);
3051
+ const body = await response.json().catch(() => ({ error: "Unknown error" }));
3052
+ throw new Error(
3053
+ `Cencori API error: ${extractHttpError(body, response.statusText)}`
3054
+ );
2908
3055
  }
2909
3056
  const data = await response.json();
2910
3057
  const content = [];
@@ -2955,8 +3102,10 @@ var CencoriChatLanguageModel = class {
2955
3102
  signal: options.abortSignal
2956
3103
  });
2957
3104
  if (!response.ok) {
2958
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
2959
- throw new Error(`Cencori API error: ${error.error || response.statusText}`);
3105
+ const body = await response.json().catch(() => ({ error: "Unknown error" }));
3106
+ throw new Error(
3107
+ `Cencori API error: ${extractHttpError(body, response.statusText)}`
3108
+ );
2960
3109
  }
2961
3110
  const reader = response.body?.getReader();
2962
3111
  if (!reader) {
@@ -3011,6 +3160,15 @@ var CencoriChatLanguageModel = class {
3011
3160
  }
3012
3161
  try {
3013
3162
  const chunk = JSON.parse(data);
3163
+ const streamError = extractStreamError(
3164
+ chunk
3165
+ );
3166
+ if (streamError) {
3167
+ controller.error(
3168
+ new Error(`Cencori API error: ${streamError}`)
3169
+ );
3170
+ return;
3171
+ }
3014
3172
  if (chunk.delta) {
3015
3173
  if (!started) {
3016
3174
  started = true;
@@ -3026,8 +3184,9 @@ var CencoriChatLanguageModel = class {
3026
3184
  delta: chunk.delta
3027
3185
  });
3028
3186
  }
3029
- if (chunk.tool_calls && chunk.tool_calls.length > 0) {
3030
- for (const tc of chunk.tool_calls) {
3187
+ const toolCalls = chunk.tool_calls && chunk.tool_calls.length > 0 ? chunk.tool_calls : chunk.toolCalls && chunk.toolCalls.length > 0 ? chunk.toolCalls : void 0;
3188
+ if (toolCalls) {
3189
+ for (const tc of toolCalls) {
3031
3190
  controller.enqueue({
3032
3191
  type: "tool-call",
3033
3192
  toolCallId: tc.id,
@@ -3038,6 +3197,14 @@ var CencoriChatLanguageModel = class {
3038
3197
  }
3039
3198
  }
3040
3199
  if (chunk.finish_reason) {
3200
+ if (chunk.finish_reason === "error") {
3201
+ controller.error(
3202
+ new Error(
3203
+ `Cencori API error: ${extractStreamError(chunk) ?? "stream failed with error"}`
3204
+ )
3205
+ );
3206
+ return;
3207
+ }
3041
3208
  if (started) {
3042
3209
  controller.enqueue({
3043
3210
  type: "text-end",
@@ -3120,6 +3287,7 @@ cencori.chat = cencori;
3120
3287
  DocumentsNamespace,
3121
3288
  EndUsersNamespace,
3122
3289
  InstallationsNamespace,
3290
+ JudgmentNamespace,
3123
3291
  KnowledgeNamespace,
3124
3292
  MEMORY_FETCH_TOOL,
3125
3293
  McpServersNamespace,