@push.rocks/smartai 2.2.0 → 2.3.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.
@@ -1,38 +1,12 @@
1
- import type { LanguageModelV3Middleware, LanguageModelV3Prompt } from '@ai-sdk/provider';
1
+ import type { LanguageModelV3Middleware } from '@ai-sdk/provider';
2
+ import { createSmartAiCachingMiddleware } from './smartai.cache.js';
3
+ import type { ISmartAiCacheOptions } from './smartai.cache.js';
2
4
 
3
5
  /**
4
6
  * Creates middleware that adds Anthropic prompt caching directives.
5
7
  * Marks the last system message and last user message with ephemeral cache control,
6
8
  * reducing input token cost and latency on repeated calls.
7
9
  */
8
- export function createAnthropicCachingMiddleware(): LanguageModelV3Middleware {
9
- return {
10
- specificationVersion: 'v3',
11
- transformParams: async ({ params }) => {
12
- const messages = [...params.prompt] as Array<Record<string, unknown>>;
13
-
14
- // Find the last system message and last user message
15
- let lastSystemIdx = -1;
16
- let lastUserIdx = -1;
17
- for (let i = 0; i < messages.length; i++) {
18
- if (messages[i].role === 'system') lastSystemIdx = i;
19
- if (messages[i].role === 'user') lastUserIdx = i;
20
- }
21
-
22
- const targets = [lastSystemIdx, lastUserIdx].filter(i => i >= 0);
23
- for (const idx of targets) {
24
- const msg = { ...messages[idx] };
25
- msg.providerOptions = {
26
- ...(msg.providerOptions as Record<string, unknown> || {}),
27
- anthropic: {
28
- ...((msg.providerOptions as Record<string, unknown>)?.anthropic as Record<string, unknown> || {}),
29
- cacheControl: { type: 'ephemeral' },
30
- },
31
- };
32
- messages[idx] = msg;
33
- }
34
-
35
- return { ...params, prompt: messages as unknown as LanguageModelV3Prompt };
36
- },
37
- };
10
+ export function createAnthropicCachingMiddleware(options: ISmartAiCacheOptions = {}): LanguageModelV3Middleware {
11
+ return createSmartAiCachingMiddleware({ ...options, provider: 'anthropic' });
38
12
  }