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.mjs CHANGED
@@ -128,6 +128,25 @@ function throwCencoriError(response, body) {
128
128
  }
129
129
 
130
130
  // src/ai/index.ts
131
+ function toKnowledgeCitations(raw) {
132
+ return (raw ?? []).map((c) => ({
133
+ n: c.n,
134
+ chunkId: c.chunk_id,
135
+ sourceId: c.source_id,
136
+ ord: c.ord ?? null,
137
+ page: c.page ?? null,
138
+ score: c.score
139
+ }));
140
+ }
141
+ function citationsFromHeader(response) {
142
+ const raw = response.headers.get("X-Cencori-Knowledge-Citations");
143
+ if (!raw) return void 0;
144
+ try {
145
+ return toKnowledgeCitations(JSON.parse(raw));
146
+ } catch {
147
+ return void 0;
148
+ }
149
+ }
131
150
  var AINamespace = class {
132
151
  constructor(config) {
133
152
  this.config = config;
@@ -157,7 +176,8 @@ var AINamespace = class {
157
176
  stream: false,
158
177
  tools: request2.tools,
159
178
  toolChoice: request2.toolChoice,
160
- prompt: request2.prompt
179
+ prompt: request2.prompt,
180
+ knowledgeBases: request2.knowledgeBases
161
181
  })
162
182
  });
163
183
  if (!response.ok) {
@@ -189,7 +209,13 @@ var AINamespace = class {
189
209
  costUsd: data.cost_usd,
190
210
  provider: data.provider,
191
211
  requestId: response.headers.get("X-Request-Id"),
192
- safety: data.safety
212
+ safety: data.safety,
213
+ ...data.knowledge ? {
214
+ knowledge: {
215
+ knowledgeBases: data.knowledge.knowledge_bases,
216
+ citations: toKnowledgeCitations(data.knowledge.citations)
217
+ }
218
+ } : {}
193
219
  };
194
220
  }
195
221
  /**
@@ -217,7 +243,8 @@ var AINamespace = class {
217
243
  stream: true,
218
244
  tools: request2.tools,
219
245
  toolChoice: request2.toolChoice,
220
- prompt: request2.prompt
246
+ prompt: request2.prompt,
247
+ knowledgeBases: request2.knowledgeBases
221
248
  })
222
249
  });
223
250
  if (!response.ok) {
@@ -230,6 +257,7 @@ var AINamespace = class {
230
257
  const reader = response.body.getReader();
231
258
  const decoder = new TextDecoder();
232
259
  let buffer = "";
260
+ let citations = citationsFromHeader(response);
233
261
  try {
234
262
  while (true) {
235
263
  const { done, value } = await reader.read();
@@ -246,6 +274,10 @@ var AINamespace = class {
246
274
  }
247
275
  try {
248
276
  const chunk = JSON.parse(data);
277
+ if (citations) {
278
+ chunk.citations = citations;
279
+ citations = void 0;
280
+ }
249
281
  yield chunk;
250
282
  } catch {
251
283
  }
@@ -428,6 +460,10 @@ var AINamespace = class {
428
460
  };
429
461
  }
430
462
  /**
463
+ * @deprecated `/api/ai/rag` is not available on the hosted gateway. Use
464
+ * knowledge bases instead: `cencori.ai.chat({ model, messages,
465
+ * knowledgeBases: ['kb_...'] })` returns the answer with citations.
466
+ *
431
467
  * RAG (Retrieval-Augmented Generation) - Chat with automatic memory context
432
468
  *
433
469
  * Searches your memory namespace for relevant context and includes it
@@ -624,6 +660,10 @@ var AINamespace = class {
624
660
  }
625
661
  }
626
662
  /**
663
+ * @deprecated `/api/ai/rag` is not available on the hosted gateway. Use
664
+ * `cencori.ai.chatStream({ ..., knowledgeBases: ['kb_...'] })`; the first
665
+ * chunk carries `citations`.
666
+ *
627
667
  * Stream RAG responses with automatic memory context
628
668
  *
629
669
  * @example
@@ -971,6 +1011,57 @@ var VoiceNamespace = class {
971
1011
  }
972
1012
  };
973
1013
 
1014
+ // src/judgment/index.ts
1015
+ var JudgmentNamespace = class {
1016
+ constructor(config) {
1017
+ this.config = config;
1018
+ }
1019
+ /**
1020
+ * Judge a state against typed questions. Returns one answer per
1021
+ * question id with probabilities your code can branch on.
1022
+ */
1023
+ async judge(request2) {
1024
+ if (request2.state === void 0 || request2.state === null) {
1025
+ throw new Error("judgment.judge requires `state`");
1026
+ }
1027
+ if (!request2.questions || Object.keys(request2.questions).length === 0) {
1028
+ throw new Error("judgment.judge requires a non-empty `questions` object");
1029
+ }
1030
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
1031
+ method: "POST",
1032
+ headers: {
1033
+ "CENCORI_API_KEY": this.config.apiKey,
1034
+ "Content-Type": "application/json",
1035
+ ...this.config.headers
1036
+ },
1037
+ body: JSON.stringify(request2)
1038
+ });
1039
+ if (!response.ok) {
1040
+ throw await this.error(response, "judgment.judge");
1041
+ }
1042
+ return await response.json();
1043
+ }
1044
+ /** List the judgment models with their pricing. No API key required. */
1045
+ async listModels() {
1046
+ const response = await fetch(`${this.config.baseUrl}/api/v1/judgments`, {
1047
+ headers: this.config.headers
1048
+ });
1049
+ if (!response.ok) {
1050
+ throw await this.error(response, "judgment.listModels");
1051
+ }
1052
+ return await response.json();
1053
+ }
1054
+ async error(response, op) {
1055
+ const data = await response.json().catch(() => ({}));
1056
+ const message = data && typeof data === "object" && "message" in data && typeof data.message === "string" ? data.message : `${op} failed with status ${response.status}`;
1057
+ const code = data && typeof data === "object" && "error" in data && typeof data.error === "string" ? data.error : "request_failed";
1058
+ const err = new Error(message);
1059
+ err.code = code;
1060
+ err.details = data;
1061
+ return err;
1062
+ }
1063
+ };
1064
+
974
1065
  // src/documents/index.ts
975
1066
  function toBody2(request2, extra = {}) {
976
1067
  const body = {
@@ -1238,23 +1329,21 @@ var VectorsNamespace = class {
1238
1329
  };
1239
1330
  var KnowledgeNamespace = class {
1240
1331
  /**
1241
- * Query the knowledge base
1242
- *
1243
- * 🚧 Coming Soon - This feature is not yet available.
1332
+ * @deprecated Never implemented. Use `cencori.knowledge.search()`, or
1333
+ * ground a chat with `cencori.ai.chat({ knowledgeBases: [...] })`.
1244
1334
  */
1245
1335
  async query(question) {
1246
1336
  throw new Error(
1247
- `cencori.storage.knowledge.query() is coming soon! Join our waitlist at https://cencori.com/memory`
1337
+ `cencori.storage.knowledge.query() is not available. Use cencori.knowledge.search(kbId, { query }), or cencori.ai.chat({ knowledgeBases: [kbId], ... }).`
1248
1338
  );
1249
1339
  }
1250
1340
  /**
1251
- * Add documents to knowledge base
1252
- *
1253
- * 🚧 Coming Soon - This feature is not yet available.
1341
+ * @deprecated Never implemented. Use `cencori.knowledge.addInlineSource()`
1342
+ * or `cencori.knowledge.addFileSource()`.
1254
1343
  */
1255
1344
  async add(documents) {
1256
1345
  throw new Error(
1257
- `cencori.storage.knowledge.add() is coming soon! Join our waitlist at https://cencori.com/memory`
1346
+ `cencori.storage.knowledge.add() is not available. Use cencori.knowledge.addInlineSource(kbId, text) or cencori.knowledge.addFileSource(kbId, file).`
1258
1347
  );
1259
1348
  }
1260
1349
  };
@@ -2023,16 +2112,17 @@ function parseRetryAfterSeconds(value, nowMs = Date.now()) {
2023
2112
  return Math.max(0, Math.ceil((timestamp - nowMs) / 1e3));
2024
2113
  }
2025
2114
  async function request(config, method, path, body, extraHeaders, options) {
2115
+ const isForm = typeof FormData !== "undefined" && body instanceof FormData;
2026
2116
  const response = await fetch(`${config.baseUrl}${path}`, {
2027
2117
  method,
2028
2118
  signal: options?.signal,
2029
2119
  headers: {
2030
2120
  "CENCORI_API_KEY": config.apiKey,
2031
- "Content-Type": "application/json",
2121
+ ...isForm ? {} : { "Content-Type": "application/json" },
2032
2122
  ...config.headers,
2033
2123
  ...extraHeaders
2034
2124
  },
2035
- body: body ? JSON.stringify(body) : void 0
2125
+ body: isForm ? body : body ? JSON.stringify(body) : void 0
2036
2126
  });
2037
2127
  if (!response.ok) {
2038
2128
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
@@ -2282,6 +2372,21 @@ var KnowledgeNamespace2 = class {
2282
2372
  addInlineSource(kbId, text, metadata) {
2283
2373
  return request(this.config, "POST", `/v1/knowledge-bases/${kbId}/sources`, { text, metadata });
2284
2374
  }
2375
+ /**
2376
+ * Upload a file (PDF, DOCX, Markdown, HTML, CSV, JSON or plain text, up
2377
+ * to 8MB). It is extracted, chunked and embedded server-side.
2378
+ *
2379
+ * @example
2380
+ * const pdf = new Blob([await fs.readFile('handbook.pdf')], { type: 'application/pdf' });
2381
+ * await cencori.knowledge.addFileSource(kb.id, pdf, { filename: 'handbook.pdf' });
2382
+ */
2383
+ addFileSource(kbId, file, options) {
2384
+ const form = new FormData();
2385
+ const filename = options?.filename ?? file.name ?? "upload";
2386
+ form.append("file", file, filename);
2387
+ if (options?.metadata) form.append("metadata", JSON.stringify(options.metadata));
2388
+ return request(this.config, "POST", `/v1/knowledge-bases/${kbId}/sources`, form);
2389
+ }
2285
2390
  listSources(kbId) {
2286
2391
  return request(this.config, "GET", `/v1/knowledge-bases/${kbId}/sources`);
2287
2392
  }
@@ -2542,6 +2647,7 @@ var Cencori = class {
2542
2647
  this.chat = new ChatNamespace(this.config);
2543
2648
  this.vision = new VisionNamespace(this.config);
2544
2649
  this.voice = new VoiceNamespace(this.config);
2650
+ this.judgment = new JudgmentNamespace(this.config);
2545
2651
  this.documents = new DocumentsNamespace(this.config);
2546
2652
  this.agents = new AgentsNamespace(this.config);
2547
2653
  this.compute = new ComputeNamespace();
@@ -2693,6 +2799,44 @@ function providerLogoUrl(providerId, baseUrl = DEFAULT_CATALOG_BASE_URL) {
2693
2799
  }
2694
2800
 
2695
2801
  // src/vercel/cencori-chat-model.ts
2802
+ function extractStreamError(chunk) {
2803
+ const error = chunk.error;
2804
+ if (typeof error === "string" && error) return error;
2805
+ if (error && typeof error === "object") {
2806
+ const record = error;
2807
+ if (typeof record.message === "string" && record.message) {
2808
+ return record.message;
2809
+ }
2810
+ try {
2811
+ return JSON.stringify(error);
2812
+ } catch {
2813
+ return "Provider stream error";
2814
+ }
2815
+ }
2816
+ 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) {
2817
+ return chunk.message;
2818
+ }
2819
+ return void 0;
2820
+ }
2821
+ function extractHttpError(body, fallback) {
2822
+ if (!body || typeof body !== "object") return fallback;
2823
+ const record = body;
2824
+ if (typeof record.message === "string" && record.message) return record.message;
2825
+ const error = record.error;
2826
+ if (typeof error === "string" && error) {
2827
+ return typeof record.message === "string" && record.message ? record.message : error;
2828
+ }
2829
+ if (error && typeof error === "object") {
2830
+ const nested = error;
2831
+ if (typeof nested.message === "string" && nested.message) return nested.message;
2832
+ try {
2833
+ return JSON.stringify(error);
2834
+ } catch {
2835
+ return fallback;
2836
+ }
2837
+ }
2838
+ return fallback;
2839
+ }
2696
2840
  var CencoriChatLanguageModel = class {
2697
2841
  constructor(modelId, settings) {
2698
2842
  this.specificationVersion = "v3";
@@ -2831,8 +2975,10 @@ var CencoriChatLanguageModel = class {
2831
2975
  signal: options.abortSignal
2832
2976
  });
2833
2977
  if (!response.ok) {
2834
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
2835
- throw new Error(`Cencori API error: ${error.error || response.statusText}`);
2978
+ const body = await response.json().catch(() => ({ error: "Unknown error" }));
2979
+ throw new Error(
2980
+ `Cencori API error: ${extractHttpError(body, response.statusText)}`
2981
+ );
2836
2982
  }
2837
2983
  const data = await response.json();
2838
2984
  const content = [];
@@ -2883,8 +3029,10 @@ var CencoriChatLanguageModel = class {
2883
3029
  signal: options.abortSignal
2884
3030
  });
2885
3031
  if (!response.ok) {
2886
- const error = await response.json().catch(() => ({ error: "Unknown error" }));
2887
- throw new Error(`Cencori API error: ${error.error || response.statusText}`);
3032
+ const body = await response.json().catch(() => ({ error: "Unknown error" }));
3033
+ throw new Error(
3034
+ `Cencori API error: ${extractHttpError(body, response.statusText)}`
3035
+ );
2888
3036
  }
2889
3037
  const reader = response.body?.getReader();
2890
3038
  if (!reader) {
@@ -2939,6 +3087,15 @@ var CencoriChatLanguageModel = class {
2939
3087
  }
2940
3088
  try {
2941
3089
  const chunk = JSON.parse(data);
3090
+ const streamError = extractStreamError(
3091
+ chunk
3092
+ );
3093
+ if (streamError) {
3094
+ controller.error(
3095
+ new Error(`Cencori API error: ${streamError}`)
3096
+ );
3097
+ return;
3098
+ }
2942
3099
  if (chunk.delta) {
2943
3100
  if (!started) {
2944
3101
  started = true;
@@ -2954,8 +3111,9 @@ var CencoriChatLanguageModel = class {
2954
3111
  delta: chunk.delta
2955
3112
  });
2956
3113
  }
2957
- if (chunk.tool_calls && chunk.tool_calls.length > 0) {
2958
- for (const tc of chunk.tool_calls) {
3114
+ const toolCalls = chunk.tool_calls && chunk.tool_calls.length > 0 ? chunk.tool_calls : chunk.toolCalls && chunk.toolCalls.length > 0 ? chunk.toolCalls : void 0;
3115
+ if (toolCalls) {
3116
+ for (const tc of toolCalls) {
2959
3117
  controller.enqueue({
2960
3118
  type: "tool-call",
2961
3119
  toolCallId: tc.id,
@@ -2966,6 +3124,14 @@ var CencoriChatLanguageModel = class {
2966
3124
  }
2967
3125
  }
2968
3126
  if (chunk.finish_reason) {
3127
+ if (chunk.finish_reason === "error") {
3128
+ controller.error(
3129
+ new Error(
3130
+ `Cencori API error: ${extractStreamError(chunk) ?? "stream failed with error"}`
3131
+ )
3132
+ );
3133
+ return;
3134
+ }
2969
3135
  if (started) {
2970
3136
  controller.enqueue({
2971
3137
  type: "text-end",
@@ -3047,6 +3213,7 @@ export {
3047
3213
  DocumentsNamespace,
3048
3214
  EndUsersNamespace,
3049
3215
  InstallationsNamespace,
3216
+ JudgmentNamespace,
3050
3217
  KnowledgeNamespace2 as KnowledgeNamespace,
3051
3218
  MEMORY_FETCH_TOOL,
3052
3219
  McpServersNamespace,