@providerkit/core 0.23.0 → 0.24.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 (62) hide show
  1. package/README.md +29 -7
  2. package/dist/auth/with-token.d.ts +4 -1
  3. package/dist/auth/with-token.d.ts.map +1 -1
  4. package/dist/auth/with-token.js +4 -1
  5. package/dist/auth/with-token.js.map +1 -1
  6. package/dist/gate.d.ts +29 -0
  7. package/dist/gate.d.ts.map +1 -0
  8. package/dist/gate.js +96 -0
  9. package/dist/gate.js.map +1 -0
  10. package/dist/index.d.ts +3 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -0
  13. package/dist/index.js.map +1 -1
  14. package/dist/key-pool.d.ts +11 -0
  15. package/dist/key-pool.d.ts.map +1 -1
  16. package/dist/key-pool.js +14 -0
  17. package/dist/key-pool.js.map +1 -1
  18. package/dist/providers/anthropic.d.ts +7 -0
  19. package/dist/providers/anthropic.d.ts.map +1 -1
  20. package/dist/providers/anthropic.js +3 -1
  21. package/dist/providers/anthropic.js.map +1 -1
  22. package/dist/providers/factory.d.ts +26 -0
  23. package/dist/providers/factory.d.ts.map +1 -1
  24. package/dist/providers/factory.js +41 -9
  25. package/dist/providers/factory.js.map +1 -1
  26. package/dist/providers/openai.d.ts +7 -0
  27. package/dist/providers/openai.d.ts.map +1 -1
  28. package/dist/providers/openai.js +3 -1
  29. package/dist/providers/openai.js.map +1 -1
  30. package/dist/providers/responses.d.ts +11 -0
  31. package/dist/providers/responses.d.ts.map +1 -1
  32. package/dist/providers/responses.js +3 -1
  33. package/dist/providers/responses.js.map +1 -1
  34. package/dist/registry.d.ts +105 -0
  35. package/dist/registry.d.ts.map +1 -0
  36. package/dist/registry.js +291 -0
  37. package/dist/registry.js.map +1 -0
  38. package/dist/tools.d.ts +8 -0
  39. package/dist/tools.d.ts.map +1 -1
  40. package/dist/tools.js +2 -2
  41. package/dist/tools.js.map +1 -1
  42. package/dist/turn.d.ts +55 -0
  43. package/dist/turn.d.ts.map +1 -0
  44. package/dist/turn.js +125 -0
  45. package/dist/turn.js.map +1 -0
  46. package/dist/types.d.ts +8 -22
  47. package/dist/types.d.ts.map +1 -1
  48. package/dist/types.js +0 -45
  49. package/dist/types.js.map +1 -1
  50. package/package.json +1 -1
  51. package/src/auth/with-token.ts +4 -1
  52. package/src/gate.ts +152 -0
  53. package/src/index.ts +3 -0
  54. package/src/key-pool.ts +15 -0
  55. package/src/providers/anthropic.ts +10 -1
  56. package/src/providers/factory.ts +66 -21
  57. package/src/providers/openai.ts +10 -1
  58. package/src/providers/responses.ts +14 -1
  59. package/src/registry.ts +420 -0
  60. package/src/tools.ts +10 -2
  61. package/src/turn.ts +166 -0
  62. package/src/types.ts +8 -62
package/src/types.ts CHANGED
@@ -71,8 +71,9 @@ export type ContentPart = TextPart | ImagePart | FilePart;
71
71
 
72
72
  /**
73
73
  * A tool the model asked to run. `arguments` is the RAW JSON string, not a
74
- * parsed object: it arrives in fragments and can be truncated mid-stream, so
75
- * assembly and validation are the consumer's job (see tools/define).
74
+ * parsed object: it arrives in fragments and can be truncated mid-stream.
75
+ * `collectTurn` joins the fragments; healing and validating them is the
76
+ * consumer's job (see tools/define).
76
77
  */
77
78
  export interface ToolCall {
78
79
  id: string;
@@ -151,7 +152,7 @@ export interface ToolDefinition {
151
152
 
152
153
  export type FinishReason = "stop" | "length" | "tool_calls" | "content_filter";
153
154
 
154
- /** Incremental tool-call fragment, assembled by `index` by the consumer. */
155
+ /** Incremental tool-call fragment, assembled by `index`. `collectTurn` does it. */
155
156
  export interface ToolCallDelta {
156
157
  index: number;
157
158
  id?: string;
@@ -385,9 +386,10 @@ export interface StreamOptions {
385
386
  */
386
387
  jsonWithTools?: JsonWithTools;
387
388
  /**
388
- * Stable id for the conversation or job this call belongs to. Sent only to
389
- * endpoints whose preset names a `sessionHeader` (OpenCode Go routes and
390
- * caches by it); every other endpoint ignores it.
389
+ * Stable id for the conversation or job this call belongs to. Sent in the
390
+ * `sessionHeader` of endpoints that name one (OpenCode Go routes and caches
391
+ * by it), and as `prompt_cache_key` on the Responses shape. A call without
392
+ * one sends the provider's `instanceSessionId` in that header instead.
391
393
  */
392
394
  sessionId?: string;
393
395
  /**
@@ -440,62 +442,6 @@ export interface Provider {
440
442
  ): AsyncIterable<ProviderChunk>;
441
443
  }
442
444
 
443
- export interface Completion {
444
- text: string;
445
- /** Present when the stream identifies the endpoint that answered. */
446
- provider?: string;
447
- reasoning: string;
448
- /** Present only where the provider sent one. See ChatMessage.reasoningDetails. */
449
- reasoningDetails?: unknown[];
450
- /** Present only where the provider sent them. See ChatMessage.reasoningItems. */
451
- reasoningItems?: ReasoningItems;
452
- usage: TokenUsage;
453
- finishReason: FinishReason | null;
454
- model: string;
455
- }
456
-
457
- /** Drain a no-tools stream into a Completion. Usage chunks are cumulative on
458
- * some providers and final-only on others: the LAST one wins. */
459
- export async function drainStream(
460
- stream: AsyncIterable<ProviderChunk>,
461
- model: string,
462
- ): Promise<Completion> {
463
- let text = "";
464
- let reasoning = "";
465
- // Not concatenated: this half of the record is a payload the provider owns,
466
- // and it arrives whole on one delta rather than in fragments. Dropped here,
467
- // a drained turn replays only half its own reasoning on the next round —
468
- // which is the failure `reasoningDetails` exists to prevent.
469
- let reasoningDetails: unknown[] | undefined;
470
- let reasoningItems: ReasoningItems | undefined;
471
- let usage: TokenUsage = EMPTY_USAGE;
472
- let finishReason: FinishReason | null = null;
473
- let source: ProviderChunk["source"];
474
- for await (const chunk of stream) {
475
- if (chunk.source) source = chunk.source;
476
- if (chunk.type === "delta") {
477
- if (chunk.content) text += chunk.content;
478
- if (chunk.reasoning) reasoning += chunk.reasoning;
479
- if (chunk.reasoningDetails?.length) reasoningDetails = chunk.reasoningDetails;
480
- if (chunk.reasoningItems?.items.length) reasoningItems = chunk.reasoningItems;
481
- } else if (chunk.type === "usage" && chunk.usage) {
482
- usage = chunk.usage;
483
- } else if (chunk.type === "finish" && chunk.finishReason) {
484
- finishReason = chunk.finishReason;
485
- }
486
- }
487
- return {
488
- text,
489
- reasoning,
490
- ...(reasoningDetails ? { reasoningDetails } : {}),
491
- ...(reasoningItems ? { reasoningItems } : {}),
492
- usage,
493
- finishReason,
494
- model: source?.model ?? model,
495
- ...(source ? { provider: source.provider } : {}),
496
- };
497
- }
498
-
499
445
  /**
500
446
  * Prepare a history for a thinking-DISABLED turn: strip `reasoning` from every
501
447
  * assistant message.