cencori 1.1.0 → 1.2.1

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.
package/dist/index.mjs CHANGED
@@ -27,7 +27,8 @@ var AINamespace = class {
27
27
  maxTokens: request.maxTokens,
28
28
  stream: false,
29
29
  tools: request.tools,
30
- toolChoice: request.toolChoice
30
+ toolChoice: request.toolChoice,
31
+ prompt: request.prompt
31
32
  })
32
33
  });
33
34
  if (!response.ok) {
@@ -36,7 +37,8 @@ var AINamespace = class {
36
37
  }
37
38
  const data = await response.json();
38
39
  const choice = data.choices?.[0];
39
- const toolCalls = choice?.message?.tool_calls?.map((tc) => ({
40
+ const rawToolCalls = data.toolCalls ?? data.tool_calls ?? choice?.message?.tool_calls;
41
+ const toolCalls = rawToolCalls?.map((tc) => ({
40
42
  id: tc.id,
41
43
  type: tc.type,
42
44
  function: {
@@ -45,11 +47,11 @@ var AINamespace = class {
45
47
  }
46
48
  }));
47
49
  return {
48
- id: data.id,
49
- model: data.model,
50
- content: choice?.message?.content ?? "",
50
+ id: data.id ?? `chatcmpl-${Date.now()}`,
51
+ model: data.model ?? request.model,
52
+ content: data.content ?? choice?.message?.content ?? "",
51
53
  toolCalls,
52
- finishReason: choice?.finish_reason,
54
+ finishReason: data.finish_reason ?? choice?.finish_reason,
53
55
  usage: {
54
56
  promptTokens: data.usage?.prompt_tokens ?? 0,
55
57
  completionTokens: data.usage?.completion_tokens ?? 0,
@@ -81,7 +83,8 @@ var AINamespace = class {
81
83
  maxTokens: request.maxTokens,
82
84
  stream: true,
83
85
  tools: request.tools,
84
- toolChoice: request.toolChoice
86
+ toolChoice: request.toolChoice,
87
+ prompt: request.prompt
85
88
  })
86
89
  });
87
90
  if (!response.ok) {
@@ -225,7 +228,7 @@ var AINamespace = class {
225
228
  throw new Error(`Cencori API error: ${errorData.error || response.statusText}`);
226
229
  }
227
230
  const data = await response.json();
228
- const toolCall = data.choices?.[0]?.message?.tool_calls?.[0];
231
+ const toolCall = data.toolCalls?.[0] ?? data.tool_calls?.[0] ?? data.choices?.[0]?.message?.tool_calls?.[0];
229
232
  if (!toolCall) {
230
233
  throw new Error("Model did not return structured output");
231
234
  }
@@ -349,6 +352,137 @@ var AINamespace = class {
349
352
  latencyMs: data.latency_ms
350
353
  };
351
354
  }
355
+ /**
356
+ * Send a request to the OpenAI-compatible Responses API.
357
+ * Supports built-in tools: web_search_preview, file_search, code_interpreter.
358
+ *
359
+ * @example
360
+ * const response = await cencori.ai.responses({
361
+ * model: 'gpt-4o',
362
+ * input: 'What is the weather in San Francisco?',
363
+ * tools: [{ type: 'web_search_preview' }]
364
+ * });
365
+ * console.log(response.output[0].content?.[0]?.text);
366
+ */
367
+ async responses(request) {
368
+ const response = await fetch(`${this.config.baseUrl}/v1/responses`, {
369
+ method: "POST",
370
+ headers: {
371
+ "CENCORI_API_KEY": this.config.apiKey,
372
+ "Content-Type": "application/json",
373
+ ...this.config.headers
374
+ },
375
+ body: JSON.stringify({
376
+ model: request.model,
377
+ input: request.input,
378
+ instructions: request.instructions,
379
+ tools: request.tools,
380
+ tool_choice: request.tool_choice,
381
+ temperature: request.temperature,
382
+ max_output_tokens: request.max_output_tokens,
383
+ top_p: request.top_p,
384
+ store: request.store,
385
+ metadata: request.metadata,
386
+ previous_response_id: request.previous_response_id,
387
+ parallel_tool_calls: request.parallel_tool_calls,
388
+ truncation: request.truncation,
389
+ response_format: request.response_format,
390
+ include: request.include,
391
+ stream: false,
392
+ user: request.user
393
+ })
394
+ });
395
+ if (!response.ok) {
396
+ const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
397
+ throw new Error(`Cencori API error: ${errorData.error || response.statusText}`);
398
+ }
399
+ return response.json();
400
+ }
401
+ /**
402
+ * Stream responses from the Responses API via SSE.
403
+ * Yields parsed events as they arrive.
404
+ *
405
+ * @example
406
+ * for await (const event of cencori.ai.responsesStream({
407
+ * model: 'gpt-4o',
408
+ * input: 'Tell me about AI',
409
+ * })) {
410
+ * if (event.type === 'response.output_text.delta') {
411
+ * process.stdout.write(event.data.delta);
412
+ * }
413
+ * }
414
+ */
415
+ async *responsesStream(request) {
416
+ const response = await fetch(`${this.config.baseUrl}/v1/responses`, {
417
+ method: "POST",
418
+ headers: {
419
+ "CENCORI_API_KEY": this.config.apiKey,
420
+ "Content-Type": "application/json",
421
+ ...this.config.headers
422
+ },
423
+ body: JSON.stringify({
424
+ model: request.model,
425
+ input: request.input,
426
+ instructions: request.instructions,
427
+ tools: request.tools,
428
+ tool_choice: request.tool_choice,
429
+ temperature: request.temperature,
430
+ max_output_tokens: request.max_output_tokens,
431
+ top_p: request.top_p,
432
+ store: request.store,
433
+ metadata: request.metadata,
434
+ previous_response_id: request.previous_response_id,
435
+ parallel_tool_calls: request.parallel_tool_calls,
436
+ truncation: request.truncation,
437
+ response_format: request.response_format,
438
+ include: request.include,
439
+ stream: true,
440
+ user: request.user
441
+ })
442
+ });
443
+ if (!response.ok) {
444
+ const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
445
+ throw new Error(`Cencori API error: ${errorData.error || response.statusText}`);
446
+ }
447
+ if (!response.body) {
448
+ throw new Error("Response body is null");
449
+ }
450
+ const reader = response.body.getReader();
451
+ const decoder = new TextDecoder();
452
+ let buffer = "";
453
+ let eventType = "";
454
+ try {
455
+ while (true) {
456
+ const { done, value } = await reader.read();
457
+ if (done) break;
458
+ buffer += decoder.decode(value, { stream: true });
459
+ const lines = buffer.split("\n");
460
+ buffer = lines.pop() || "";
461
+ for (const line of lines) {
462
+ if (line.trim() === "") {
463
+ eventType = "";
464
+ continue;
465
+ }
466
+ if (line.startsWith("event: ")) {
467
+ eventType = line.slice(7).trim();
468
+ continue;
469
+ }
470
+ if (line.startsWith("data: ")) {
471
+ const data = line.slice(6).trim();
472
+ if (!data) continue;
473
+ try {
474
+ const parsed = JSON.parse(data);
475
+ yield { type: eventType || "message", data: parsed };
476
+ } catch {
477
+ }
478
+ eventType = "";
479
+ }
480
+ }
481
+ }
482
+ } finally {
483
+ reader.releaseLock();
484
+ }
485
+ }
352
486
  /**
353
487
  * Stream RAG responses with automatic memory context
354
488
  *
@@ -502,7 +636,7 @@ var VectorsNamespace = class {
502
636
  */
503
637
  async search(query, options) {
504
638
  throw new Error(
505
- `cencori.storage.vectors.search() is coming soon! Join our waitlist at https://cencori.com/storage`
639
+ `cencori.storage.vectors.search() is coming soon! Join our waitlist at https://cencori.com/memory`
506
640
  );
507
641
  }
508
642
  /**
@@ -512,7 +646,7 @@ var VectorsNamespace = class {
512
646
  */
513
647
  async upsert(vectors) {
514
648
  throw new Error(
515
- `cencori.storage.vectors.upsert() is coming soon! Join our waitlist at https://cencori.com/storage`
649
+ `cencori.storage.vectors.upsert() is coming soon! Join our waitlist at https://cencori.com/memory`
516
650
  );
517
651
  }
518
652
  /**
@@ -522,7 +656,7 @@ var VectorsNamespace = class {
522
656
  */
523
657
  async delete(ids) {
524
658
  throw new Error(
525
- `cencori.storage.vectors.delete() is coming soon! Join our waitlist at https://cencori.com/storage`
659
+ `cencori.storage.vectors.delete() is coming soon! Join our waitlist at https://cencori.com/memory`
526
660
  );
527
661
  }
528
662
  };
@@ -534,7 +668,7 @@ var KnowledgeNamespace = class {
534
668
  */
535
669
  async query(question) {
536
670
  throw new Error(
537
- `cencori.storage.knowledge.query() is coming soon! Join our waitlist at https://cencori.com/storage`
671
+ `cencori.storage.knowledge.query() is coming soon! Join our waitlist at https://cencori.com/memory`
538
672
  );
539
673
  }
540
674
  /**
@@ -544,7 +678,7 @@ var KnowledgeNamespace = class {
544
678
  */
545
679
  async add(documents) {
546
680
  throw new Error(
547
- `cencori.storage.knowledge.add() is coming soon! Join our waitlist at https://cencori.com/storage`
681
+ `cencori.storage.knowledge.add() is coming soon! Join our waitlist at https://cencori.com/memory`
548
682
  );
549
683
  }
550
684
  };
@@ -556,7 +690,7 @@ var FilesNamespace = class {
556
690
  */
557
691
  async upload(file, name) {
558
692
  throw new Error(
559
- `cencori.storage.files.upload() is coming soon! Join our waitlist at https://cencori.com/storage`
693
+ `cencori.storage.files.upload() is coming soon! Join our waitlist at https://cencori.com/memory`
560
694
  );
561
695
  }
562
696
  /**
@@ -566,7 +700,7 @@ var FilesNamespace = class {
566
700
  */
567
701
  async process(fileId) {
568
702
  throw new Error(
569
- `cencori.storage.files.process() is coming soon! Join our waitlist at https://cencori.com/storage`
703
+ `cencori.storage.files.process() is coming soon! Join our waitlist at https://cencori.com/memory`
570
704
  );
571
705
  }
572
706
  };