@providerprotocol/ai 0.0.35 → 0.0.37

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 (115) hide show
  1. package/README.md +45 -13
  2. package/dist/anthropic/index.d.ts +2 -2
  3. package/dist/anthropic/index.js +12 -10
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/cerebras/index.d.ts +2 -2
  6. package/dist/cerebras/index.js +12 -10
  7. package/dist/cerebras/index.js.map +1 -1
  8. package/dist/{chunk-7DXVRILR.js → chunk-2YXFLRQ6.js} +2 -2
  9. package/dist/chunk-2YXFLRQ6.js.map +1 -0
  10. package/dist/{chunk-HB4ZIH3T.js → chunk-4RX4VQCB.js} +2 -2
  11. package/dist/chunk-4RX4VQCB.js.map +1 -0
  12. package/dist/{chunk-ZI67WIQS.js → chunk-5IWHCXKN.js} +2 -2
  13. package/dist/chunk-5IWHCXKN.js.map +1 -0
  14. package/dist/{chunk-UFFJDYCE.js → chunk-7GTWHZY2.js} +2 -2
  15. package/dist/{chunk-VOEWHQUB.js → chunk-CRP6Y7NF.js} +2 -2
  16. package/dist/chunk-CRP6Y7NF.js.map +1 -0
  17. package/dist/{chunk-3GWM5GR3.js → chunk-EPB3GQNL.js} +30 -65
  18. package/dist/chunk-EPB3GQNL.js.map +1 -0
  19. package/dist/{chunk-OIEWDFQU.js → chunk-FYSZFIZS.js} +2 -2
  20. package/dist/chunk-GIDT7C6I.js +37 -0
  21. package/dist/chunk-GIDT7C6I.js.map +1 -0
  22. package/dist/{chunk-IDZOVWP3.js → chunk-IK6NRCW5.js} +7 -7
  23. package/dist/{chunk-VGKZIGVI.js → chunk-LTEMH3CI.js} +2 -2
  24. package/dist/{chunk-6S222DHN.js → chunk-MJI74VEJ.js} +25 -1
  25. package/dist/chunk-MJI74VEJ.js.map +1 -0
  26. package/dist/{chunk-AY55T37A.js → chunk-SBGZJVTJ.js} +4 -30
  27. package/dist/chunk-SBGZJVTJ.js.map +1 -0
  28. package/dist/{chunk-A2IM7PGT.js → chunk-TUTYMOBL.js} +2 -2
  29. package/dist/{chunk-BRP5XJ6Q.js → chunk-WU4U6IHF.js} +2 -2
  30. package/dist/chunk-WU4U6IHF.js.map +1 -0
  31. package/dist/{chunk-C4JP64VW.js → chunk-YQLR3XOA.js} +2 -2
  32. package/dist/chunk-ZKNPQBIE.js +265 -0
  33. package/dist/chunk-ZKNPQBIE.js.map +1 -0
  34. package/dist/{chunk-4OGB7JZA.js → chunk-ZRVNAET3.js} +2 -2
  35. package/dist/{embedding-CW6SaOOz.d.ts → embedding-CwZ1ZNWv.d.ts} +1 -1
  36. package/dist/google/index.d.ts +2 -2
  37. package/dist/google/index.js +12 -10
  38. package/dist/google/index.js.map +1 -1
  39. package/dist/groq/index.d.ts +2 -2
  40. package/dist/groq/index.js +12 -10
  41. package/dist/groq/index.js.map +1 -1
  42. package/dist/http/index.d.ts +3 -3
  43. package/dist/http/index.js +2 -1
  44. package/dist/{image-stream-C0ciACM2.d.ts → image-stream-CeQHtjxS.d.ts} +1 -1
  45. package/dist/index.d.ts +7 -7
  46. package/dist/index.js +86 -283
  47. package/dist/index.js.map +1 -1
  48. package/dist/{llm-DwbUK7un.d.ts → llm-DS_-l71X.d.ts} +11 -3
  49. package/dist/middleware/logging/index.d.ts +2 -2
  50. package/dist/middleware/parsed-object/index.d.ts +2 -2
  51. package/dist/middleware/parsed-object/index.js +1 -1
  52. package/dist/middleware/persistence/index.d.ts +128 -0
  53. package/dist/middleware/persistence/index.js +144 -0
  54. package/dist/middleware/persistence/index.js.map +1 -0
  55. package/dist/middleware/pubsub/index.d.ts +27 -34
  56. package/dist/middleware/pubsub/index.js +49 -119
  57. package/dist/middleware/pubsub/index.js.map +1 -1
  58. package/dist/middleware/pubsub/server/express/index.d.ts +24 -10
  59. package/dist/middleware/pubsub/server/express/index.js +2 -2
  60. package/dist/middleware/pubsub/server/fastify/index.d.ts +24 -10
  61. package/dist/middleware/pubsub/server/fastify/index.js +2 -2
  62. package/dist/middleware/pubsub/server/h3/index.d.ts +23 -9
  63. package/dist/middleware/pubsub/server/h3/index.js +2 -2
  64. package/dist/middleware/pubsub/server/index.d.ts +2 -2
  65. package/dist/middleware/pubsub/server/index.js +5 -5
  66. package/dist/middleware/pubsub/server/webapi/index.d.ts +23 -13
  67. package/dist/middleware/pubsub/server/webapi/index.js +2 -2
  68. package/dist/ollama/index.d.ts +2 -2
  69. package/dist/ollama/index.js +12 -10
  70. package/dist/ollama/index.js.map +1 -1
  71. package/dist/openai/index.d.ts +2 -2
  72. package/dist/openai/index.js +12 -10
  73. package/dist/openai/index.js.map +1 -1
  74. package/dist/openrouter/index.d.ts +2 -2
  75. package/dist/openrouter/index.js +12 -10
  76. package/dist/openrouter/index.js.map +1 -1
  77. package/dist/proxy/index.d.ts +4 -4
  78. package/dist/proxy/index.js +20 -18
  79. package/dist/proxy/index.js.map +1 -1
  80. package/dist/proxy/server/express/index.d.ts +4 -4
  81. package/dist/proxy/server/express/index.js +3 -3
  82. package/dist/proxy/server/fastify/index.d.ts +4 -4
  83. package/dist/proxy/server/fastify/index.js +3 -3
  84. package/dist/proxy/server/h3/index.d.ts +4 -4
  85. package/dist/proxy/server/h3/index.js +3 -3
  86. package/dist/proxy/server/index.d.ts +4 -4
  87. package/dist/proxy/server/index.js +9 -9
  88. package/dist/proxy/server/webapi/index.d.ts +4 -4
  89. package/dist/proxy/server/webapi/index.js +3 -3
  90. package/dist/responses/index.d.ts +2 -2
  91. package/dist/responses/index.js +12 -10
  92. package/dist/responses/index.js.map +1 -1
  93. package/dist/{retry-YayV42GV.d.ts → retry-CgoBNa51.d.ts} +1 -1
  94. package/dist/{stream-CecfVCPO.d.ts → stream-sXhBtWjl.d.ts} +21 -6
  95. package/dist/types-Cr4F0tVy.d.ts +96 -0
  96. package/dist/xai/index.d.ts +2 -2
  97. package/dist/xai/index.js +12 -10
  98. package/dist/xai/index.js.map +1 -1
  99. package/package.json +6 -1
  100. package/dist/chunk-3GWM5GR3.js.map +0 -1
  101. package/dist/chunk-6S222DHN.js.map +0 -1
  102. package/dist/chunk-7DXVRILR.js.map +0 -1
  103. package/dist/chunk-AY55T37A.js.map +0 -1
  104. package/dist/chunk-BRP5XJ6Q.js.map +0 -1
  105. package/dist/chunk-HB4ZIH3T.js.map +0 -1
  106. package/dist/chunk-VOEWHQUB.js.map +0 -1
  107. package/dist/chunk-ZI67WIQS.js.map +0 -1
  108. package/dist/types-C8Gciizr.d.ts +0 -168
  109. /package/dist/{chunk-UFFJDYCE.js.map → chunk-7GTWHZY2.js.map} +0 -0
  110. /package/dist/{chunk-OIEWDFQU.js.map → chunk-FYSZFIZS.js.map} +0 -0
  111. /package/dist/{chunk-IDZOVWP3.js.map → chunk-IK6NRCW5.js.map} +0 -0
  112. /package/dist/{chunk-VGKZIGVI.js.map → chunk-LTEMH3CI.js.map} +0 -0
  113. /package/dist/{chunk-A2IM7PGT.js.map → chunk-TUTYMOBL.js.map} +0 -0
  114. /package/dist/{chunk-C4JP64VW.js.map → chunk-YQLR3XOA.js.map} +0 -0
  115. /package/dist/{chunk-4OGB7JZA.js.map → chunk-ZRVNAET3.js.map} +0 -0
package/README.md CHANGED
@@ -47,6 +47,12 @@ for await (const event of stream) {
47
47
  const turn = await stream.turn;
48
48
  ```
49
49
 
50
+ Stream results are PromiseLike, so you can also await the stream directly to auto-drain:
51
+
52
+ ```typescript
53
+ const turn = await claude.stream('Count to 5');
54
+ ```
55
+
50
56
  **Stream Control:**
51
57
 
52
58
  ```typescript
@@ -640,9 +646,39 @@ const model = llm({
640
646
  const result = await model.generate('Hello');
641
647
  ```
642
648
 
649
+ ### Persistence Middleware
650
+
651
+ Load and save conversation threads around LLM requests:
652
+
653
+ ```typescript
654
+ import { llm, Thread } from '@providerprotocol/ai';
655
+ import { anthropic } from '@providerprotocol/ai/anthropic';
656
+ import { persistenceMiddleware, PersistenceAdapter } from '@providerprotocol/ai/middleware/persistence';
657
+
658
+ const adapter = new PersistenceAdapter({
659
+ id: 'conversation-123',
660
+ load: async (id) => loadThreadFromDatabase(id), // Thread | ThreadJSON | null
661
+ save: async (id, thread, turn) => {
662
+ await saveThreadToDatabase(id, thread);
663
+ if (turn) {
664
+ await saveTurnToDatabase(id, turn);
665
+ }
666
+ },
667
+ });
668
+
669
+ const model = llm({
670
+ model: anthropic('claude-sonnet-4-20250514'),
671
+ middleware: [persistenceMiddleware({ adapter })],
672
+ });
673
+
674
+ const turn = await model.generate('Hello!');
675
+ ```
676
+
643
677
  ### Pub-Sub Middleware (Stream Resumption)
644
678
 
645
- Enable reconnecting clients to catch up on missed events during active generation. The middleware buffers events and publishes them to subscribers.
679
+ Enable reconnecting clients to catch up on missed events during active generation. The middleware buffers events, publishes them to subscribers, and removes streams on completion/abort/error.
680
+ If a stream never reaches those hooks (for example, a process crash), the adapter may retain the entry. Custom adapters should invoke `onComplete` when `remove()` runs so subscriber streams can terminate.
681
+ Streams are created lazily on first `append()` or `subscribe()` call.
646
682
 
647
683
  ```typescript
648
684
  import { llm } from '@providerprotocol/ai';
@@ -662,11 +698,12 @@ Bun.serve({
662
698
 
663
699
  if (!exists) {
664
700
  // Start background generation (fire and forget)
701
+ // Stream is created lazily on first append()
665
702
  const model = llm({
666
703
  model: anthropic('claude-sonnet-4-20250514'),
667
704
  middleware: [pubsubMiddleware({ adapter, streamId })],
668
705
  });
669
- consumeInBackground(model.stream(messages));
706
+ model.stream(messages).then(turn => { /* save to DB */ });
670
707
  }
671
708
 
672
709
  // Both new and reconnect: subscribe to events
@@ -710,17 +747,12 @@ Implement `PubSubAdapter` for custom backends (Redis, etc.):
710
747
  import type { PubSubAdapter } from '@providerprotocol/ai/middleware/pubsub';
711
748
 
712
749
  const redisAdapter: PubSubAdapter = {
713
- async exists(streamId) { /* ... */ },
714
- async create(streamId, metadata) { /* ... */ },
715
- async append(streamId, event) { /* ... */ },
716
- async markCompleted(streamId) { /* ... */ },
717
- async isCompleted(streamId) { /* ... */ },
718
- async getEvents(streamId) { /* ... */ },
719
- async getStream(streamId) { /* ... */ },
720
- subscribe(streamId, callback) { /* ... */ },
721
- publish(streamId, event) { /* ... */ },
722
- async remove(streamId) { /* ... */ },
723
- async cleanup(maxAge) { /* ... */ },
750
+ async exists(streamId) { /* check if stream exists */ },
751
+ async append(streamId, event) { /* append event, create lazily */ },
752
+ async getEvents(streamId) { /* return events or [] */ },
753
+ subscribe(streamId, onEvent, onComplete) { /* subscribe to live events */ },
754
+ publish(streamId, event) { /* broadcast to subscribers */ },
755
+ async remove(streamId) { /* notify onComplete then delete */ },
724
756
  };
725
757
  ```
726
758
 
@@ -1,5 +1,5 @@
1
- import { d as Provider } from '../llm-DwbUK7un.js';
2
- import '../stream-CecfVCPO.js';
1
+ import { e as Provider } from '../llm-DS_-l71X.js';
2
+ import '../stream-sXhBtWjl.js';
3
3
 
4
4
  /**
5
5
  * @fileoverview Anthropic API type definitions.
@@ -3,35 +3,37 @@ import {
3
3
  } from "../chunk-PMK5LZ5Z.js";
4
4
  import {
5
5
  parseJsonResponse
6
- } from "../chunk-A2IM7PGT.js";
6
+ } from "../chunk-TUTYMOBL.js";
7
7
  import {
8
8
  resolveApiKey
9
9
  } from "../chunk-ARVM24K2.js";
10
10
  import {
11
11
  createProvider
12
12
  } from "../chunk-JA3UZALR.js";
13
+ import {
14
+ doFetch,
15
+ doStreamFetch,
16
+ normalizeHttpError
17
+ } from "../chunk-SBGZJVTJ.js";
18
+ import {
19
+ StreamEventType,
20
+ objectDelta
21
+ } from "../chunk-MJI74VEJ.js";
13
22
  import {
14
23
  AssistantMessage,
15
24
  generateId,
16
25
  isAssistantMessage,
17
26
  isToolResultMessage,
18
27
  isUserMessage
19
- } from "../chunk-BRP5XJ6Q.js";
28
+ } from "../chunk-WU4U6IHF.js";
20
29
  import {
21
- doFetch,
22
- doStreamFetch,
23
- normalizeHttpError,
24
30
  toError
25
- } from "../chunk-AY55T37A.js";
31
+ } from "../chunk-GIDT7C6I.js";
26
32
  import {
27
33
  ErrorCode,
28
34
  ModalityType,
29
35
  UPPError
30
36
  } from "../chunk-COS4ON4G.js";
31
- import {
32
- StreamEventType,
33
- objectDelta
34
- } from "../chunk-6S222DHN.js";
35
37
 
36
38
  // src/providers/anthropic/types.ts
37
39
  var betas = {