@meetsmore-oss/use-ai-server 1.2.3 → 1.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.
- package/dist/agents/AISDKAgent.d.ts +55 -0
- package/dist/agents/AISDKAgent.d.ts.map +1 -1
- package/dist/agents/anthropicCache.d.ts +65 -0
- package/dist/agents/anthropicCache.d.ts.map +1 -0
- package/dist/agents/index.d.ts +1 -0
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +83 -19
- package/dist/server.d.ts.map +1 -1
- package/dist/src/agents/AISDKAgent.d.ts +55 -0
- package/dist/src/agents/AISDKAgent.d.ts.map +1 -1
- package/dist/src/agents/anthropicCache.d.ts +65 -0
- package/dist/src/agents/anthropicCache.d.ts.map +1 -0
- package/dist/src/agents/index.d.ts +1 -0
- package/dist/src/agents/index.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/server.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LanguageModel } from 'ai';
|
|
2
2
|
import type { Agent, AgentInput, EventEmitter, AgentResult } from './types';
|
|
3
3
|
import type { ToolDefinition } from '../types';
|
|
4
|
+
import { type CacheBreakpointFn } from './anthropicCache';
|
|
4
5
|
/**
|
|
5
6
|
* Configuration for AISDKAgent.
|
|
6
7
|
*/
|
|
@@ -99,6 +100,53 @@ export interface AISDKAgentConfig {
|
|
|
99
100
|
* ```
|
|
100
101
|
*/
|
|
101
102
|
toolFilter?: (tool: ToolDefinition) => boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Anthropic-specific: Configure cache breakpoints for prompt caching.
|
|
105
|
+
* Only applies when using Anthropic models (Claude).
|
|
106
|
+
*
|
|
107
|
+
* Prompt caching reduces costs and latency by caching message prefixes.
|
|
108
|
+
* Cache breakpoints mark where the cacheable prefix ends.
|
|
109
|
+
*
|
|
110
|
+
* The function receives each message with positional context and returns
|
|
111
|
+
* true to add a cache breakpoint after that message.
|
|
112
|
+
*
|
|
113
|
+
* System prompt is included as role: 'system' at index 0 when present.
|
|
114
|
+
*
|
|
115
|
+
* @see https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Cache system prompt + last message (most common pattern)
|
|
120
|
+
* {
|
|
121
|
+
* cacheBreakpoint: (msg) => msg.role === 'system' || msg.isLast
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* // Cache only the last message
|
|
125
|
+
* {
|
|
126
|
+
* cacheBreakpoint: (msg) => msg.isLast
|
|
127
|
+
* }
|
|
128
|
+
*
|
|
129
|
+
* // Cache system prompt only
|
|
130
|
+
* {
|
|
131
|
+
* cacheBreakpoint: (msg) => msg.role === 'system'
|
|
132
|
+
* }
|
|
133
|
+
*
|
|
134
|
+
* // Cache first 3 messages + last
|
|
135
|
+
* {
|
|
136
|
+
* cacheBreakpoint: (msg) => msg.index < 3 || msg.isLast
|
|
137
|
+
* }
|
|
138
|
+
*
|
|
139
|
+
* // System prompt with 1h TTL, last message with 5m TTL
|
|
140
|
+
* {
|
|
141
|
+
* cacheBreakpoint: (msg) => {
|
|
142
|
+
* if (msg.role === 'system') return '1h';
|
|
143
|
+
* if (msg.isLast) return '5m';
|
|
144
|
+
* return false;
|
|
145
|
+
* }
|
|
146
|
+
* }
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
cacheBreakpoint?: CacheBreakpointFn;
|
|
102
150
|
}
|
|
103
151
|
/**
|
|
104
152
|
* Agent implementation for AI SDK models (Anthropic, OpenAI, Google, etc.).
|
|
@@ -151,6 +199,7 @@ export declare class AISDKAgent implements Agent {
|
|
|
151
199
|
private langfuse;
|
|
152
200
|
private toolFilter?;
|
|
153
201
|
private systemPrompt?;
|
|
202
|
+
private cacheBreakpoint?;
|
|
154
203
|
constructor(config: AISDKAgentConfig);
|
|
155
204
|
getName(): string;
|
|
156
205
|
getAnnotation(): string | undefined;
|
|
@@ -203,6 +252,12 @@ export declare class AISDKAgent implements Agent {
|
|
|
203
252
|
private static readonly textContentSchema;
|
|
204
253
|
private static readonly imageContentSchema;
|
|
205
254
|
private static readonly fileContentSchema;
|
|
255
|
+
/**
|
|
256
|
+
* Schema for transformed file content.
|
|
257
|
+
* Transforms the input to a text content part with file context.
|
|
258
|
+
* This allows file transformers on the client to send pre-processed file content.
|
|
259
|
+
*/
|
|
260
|
+
private static readonly transformedFileContentSchema;
|
|
206
261
|
private static readonly contentPartSchema;
|
|
207
262
|
private static readonly messageSchema;
|
|
208
263
|
private static readonly messagesArraySchema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AISDKAgent.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,aAAa,EAA2D,MAAM,IAAI,CAAC;AAIpH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAiB,MAAM,SAAS,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"AISDKAgent.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,aAAa,EAA2D,MAAM,IAAI,CAAC;AAIpH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAiB,MAAM,SAAS,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAqB/C,OAAO,EAAyB,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AA0BjF;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;;;;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CAAC,WAAW;IAsBnB;;;;OAIG;IACH,OAAO,CAAC,qBAAqB;IA6B7B,OAAO,CAAC,mBAAmB;IA2B3B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,uBAAuB,CAMpC;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,qBAAqB,CAKlC;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAG9B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAG/B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAI9B;IAEX;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,4BAA4B,CAWhD;IAEJ,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAOtC;IAEH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAM1B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,mBAAmB,CAAqC;IAEhF;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;CAgBzB"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic prompt caching utilities for AI SDK Agent.
|
|
3
|
+
*
|
|
4
|
+
* This module handles cache breakpoint configuration for Anthropic models (Claude).
|
|
5
|
+
* Prompt caching reduces costs and latency by caching message prefixes.
|
|
6
|
+
*
|
|
7
|
+
* @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching
|
|
8
|
+
*/
|
|
9
|
+
import type { LanguageModel, ModelMessage } from 'ai';
|
|
10
|
+
/**
|
|
11
|
+
* ModelMessage extended with positional context for cache breakpoint decisions.
|
|
12
|
+
* Used by the cacheBreakpoint config to determine which messages should have
|
|
13
|
+
* Anthropic's cache_control breakpoints applied.
|
|
14
|
+
*
|
|
15
|
+
* System prompt is included as role: 'system' at index 0 when present.
|
|
16
|
+
*/
|
|
17
|
+
export type MessageWithCacheContext = ModelMessage & {
|
|
18
|
+
/** Position in the messages array (0-indexed, system prompt is 0 if present) */
|
|
19
|
+
index: number;
|
|
20
|
+
/** Total number of messages including system prompt */
|
|
21
|
+
totalCount: number;
|
|
22
|
+
/** True if this is the first message */
|
|
23
|
+
isFirst: boolean;
|
|
24
|
+
/** True if this is the last message */
|
|
25
|
+
isLast: boolean;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Cache TTL options for Anthropic prompt caching.
|
|
29
|
+
* - '5m': 5-minute cache (default, refreshed on each use at no cost)
|
|
30
|
+
* - '1h': 1-hour cache (additional cost, useful for infrequent access patterns)
|
|
31
|
+
*
|
|
32
|
+
* @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching#1-hour-cache-duration
|
|
33
|
+
*/
|
|
34
|
+
export type CacheTtl = '5m' | '1h';
|
|
35
|
+
/**
|
|
36
|
+
* Return type for cacheBreakpoint function.
|
|
37
|
+
* - `false` / `null` / `undefined`: No cache breakpoint
|
|
38
|
+
* - `true`: Cache breakpoint with default TTL (5m)
|
|
39
|
+
* - `'5m'` / `'1h'`: Cache breakpoint with specified TTL
|
|
40
|
+
*/
|
|
41
|
+
export type CacheBreakpointResult = boolean | CacheTtl | null | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Function type for determining cache breakpoints.
|
|
44
|
+
*/
|
|
45
|
+
export type CacheBreakpointFn = (message: MessageWithCacheContext) => CacheBreakpointResult;
|
|
46
|
+
/**
|
|
47
|
+
* Checks if the given model is an Anthropic model (Claude).
|
|
48
|
+
*/
|
|
49
|
+
export declare function isAnthropicModel(model: LanguageModel): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Applies cache breakpoints to messages for Anthropic prompt caching.
|
|
52
|
+
* Only applies when:
|
|
53
|
+
* 1. cacheBreakpoint config is provided
|
|
54
|
+
* 2. Model is an Anthropic model
|
|
55
|
+
*
|
|
56
|
+
* Adds providerOptions.anthropic.cacheControl to messages where
|
|
57
|
+
* the cacheBreakpoint function returns true.
|
|
58
|
+
*
|
|
59
|
+
* @param messages - The messages array (system prompt should be prepended as role: 'system')
|
|
60
|
+
* @param cacheBreakpoint - Function to determine which messages should have cache breakpoints
|
|
61
|
+
* @param model - The AI SDK LanguageModel to check if it's Anthropic
|
|
62
|
+
* @returns Messages with cache control providerOptions added where applicable
|
|
63
|
+
*/
|
|
64
|
+
export declare function applyCacheBreakpoints(messages: ModelMessage[], cacheBreakpoint: CacheBreakpointFn | undefined, model: LanguageModel): ModelMessage[];
|
|
65
|
+
//# sourceMappingURL=anthropicCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropicCache.d.ts","sourceRoot":"","sources":["../../../src/agents/anthropicCache.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAGtD;;;;;;GAMG;AACH,MAAM,MAAM,uBAAuB,GAAG,YAAY,GAAG;IACnD,gFAAgF;IAChF,KAAK,EAAE,MAAM,CAAC;IACd,uDAAuD;IACvD,UAAU,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,OAAO,EAAE,OAAO,CAAC;IACjB,uCAAuC;IACvC,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC;AAEnC;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,GAAG,OAAO,GAAG,QAAQ,GAAG,IAAI,GAAG,SAAS,CAAC;AAE1E;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,uBAAuB,KAAK,qBAAqB,CAAC;AAE5F;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAQ9D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,YAAY,EAAE,EACxB,eAAe,EAAE,iBAAiB,GAAG,SAAS,EAC9C,KAAK,EAAE,aAAa,GACnB,YAAY,EAAE,CAyDhB"}
|
package/dist/agents/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export type { Agent, AgentInput, EventEmitter, AgentResult, ClientSession } from './types';
|
|
6
6
|
export { AISDKAgent, type AISDKAgentConfig } from './AISDKAgent';
|
|
7
|
+
export { applyCacheBreakpoints, isAnthropicModel, type MessageWithCacheContext, type CacheTtl, type CacheBreakpointResult, type CacheBreakpointFn, } from './anthropicCache';
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,uBAAuB,EAC5B,KAAK,QAAQ,EACb,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { UseAIServer } from './server';
|
|
|
2
2
|
export type { UseAIServerConfig, McpEndpointConfig, ToolDefinition, CorsOptions } from './types';
|
|
3
3
|
export type { ClientSession } from './server';
|
|
4
4
|
export type { Agent, AgentInput, EventEmitter, AgentResult } from './agents';
|
|
5
|
-
export { AISDKAgent, type AISDKAgentConfig } from './agents';
|
|
5
|
+
export { AISDKAgent, type AISDKAgentConfig, type MessageWithCacheContext, type CacheTtl, type CacheBreakpointResult, type CacheBreakpointFn } from './agents';
|
|
6
6
|
export type { UseAIServerPlugin, MessageHandler } from './plugins';
|
|
7
7
|
export { logger } from './logger';
|
|
8
8
|
export { createClientToolExecutor, isRemoteTool, createGlobFilter, and, or, not, } from './utils';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjG,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjG,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,KAAK,QAAQ,EAAE,KAAK,qBAAqB,EAAE,KAAK,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAG9J,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGnE,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,OAAO,EACL,wBAAwB,EACxB,YAAY,EACZ,gBAAgB,EAChB,GAAG,EACH,EAAE,EACF,GAAG,GACJ,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -9046,17 +9046,6 @@ var require_db = __commonJS((exports, module) => {
|
|
|
9046
9046
|
};
|
|
9047
9047
|
});
|
|
9048
9048
|
|
|
9049
|
-
// ../../node_modules/.bun/mime-db@1.52.0/node_modules/mime-db/index.js
|
|
9050
|
-
var require_mime_db = __commonJS((exports, module) => {
|
|
9051
|
-
/*!
|
|
9052
|
-
* mime-db
|
|
9053
|
-
* Copyright(c) 2014 Jonathan Ong
|
|
9054
|
-
* Copyright(c) 2015-2022 Douglas Christopher Wilson
|
|
9055
|
-
* MIT Licensed
|
|
9056
|
-
*/
|
|
9057
|
-
module.exports = require_db();
|
|
9058
|
-
});
|
|
9059
|
-
|
|
9060
9049
|
// ../../node_modules/.bun/mime-types@2.1.35/node_modules/mime-types/index.js
|
|
9061
9050
|
var require_mime_types = __commonJS((exports) => {
|
|
9062
9051
|
/*!
|
|
@@ -9065,7 +9054,7 @@ var require_mime_types = __commonJS((exports) => {
|
|
|
9065
9054
|
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
9066
9055
|
* MIT Licensed
|
|
9067
9056
|
*/
|
|
9068
|
-
var db =
|
|
9057
|
+
var db = require_db();
|
|
9069
9058
|
var extname = __require("path").extname;
|
|
9070
9059
|
var EXTRACT_TYPE_REGEXP = /^\s*([^;\s]*)(?:;|\s|$)/;
|
|
9071
9060
|
var TEXT_TYPE_REGEXP = /^text\//i;
|
|
@@ -19005,7 +18994,7 @@ var require_package = __commonJS((exports, module) => {
|
|
|
19005
18994
|
|
|
19006
18995
|
// ../../node_modules/.bun/socket.io@4.8.1/node_modules/socket.io/dist/index.js
|
|
19007
18996
|
var require_dist2 = __commonJS((exports, module) => {
|
|
19008
|
-
var __dirname = "/home/
|
|
18997
|
+
var __dirname = "/home/runner/work/use-ai/use-ai/node_modules/.bun/socket.io@4.8.1/node_modules/socket.io/dist";
|
|
19009
18998
|
var __createBinding = exports && exports.__createBinding || (Object.create ? function(o, m, k, k2) {
|
|
19010
18999
|
if (k2 === undefined)
|
|
19011
19000
|
k2 = k;
|
|
@@ -35522,7 +35511,7 @@ var require_minimal = __commonJS((exports) => {
|
|
|
35522
35511
|
util3.isString = function isString(value) {
|
|
35523
35512
|
return typeof value === "string" || value instanceof String;
|
|
35524
35513
|
};
|
|
35525
|
-
util3.isObject = function
|
|
35514
|
+
util3.isObject = function isObject2(value) {
|
|
35526
35515
|
return value && typeof value === "object";
|
|
35527
35516
|
};
|
|
35528
35517
|
util3.isset = util3.isSet = function isSet(obj, prop) {
|
|
@@ -35675,7 +35664,7 @@ var require_writer = __commonJS((exports, module) => {
|
|
|
35675
35664
|
this.tail = this.head;
|
|
35676
35665
|
this.states = null;
|
|
35677
35666
|
}
|
|
35678
|
-
var create = function
|
|
35667
|
+
var create = function create2() {
|
|
35679
35668
|
return util3.Buffer ? function create_buffer_setup() {
|
|
35680
35669
|
return (Writer.create = function create_buffer() {
|
|
35681
35670
|
return new BufferWriter;
|
|
@@ -35899,12 +35888,12 @@ var require_reader = __commonJS((exports, module) => {
|
|
|
35899
35888
|
if (buffer instanceof Uint8Array || Array.isArray(buffer))
|
|
35900
35889
|
return new Reader(buffer);
|
|
35901
35890
|
throw Error("illegal buffer");
|
|
35902
|
-
} : function
|
|
35891
|
+
} : function create_array2(buffer) {
|
|
35903
35892
|
if (Array.isArray(buffer))
|
|
35904
35893
|
return new Reader(buffer);
|
|
35905
35894
|
throw Error("illegal buffer");
|
|
35906
35895
|
};
|
|
35907
|
-
var create = function
|
|
35896
|
+
var create = function create2() {
|
|
35908
35897
|
return util3.Buffer ? function create_buffer_setup(buffer) {
|
|
35909
35898
|
return (Reader.create = function create_buffer(buffer2) {
|
|
35910
35899
|
return util3.Buffer.isBuffer(buffer2) ? new BufferReader(buffer2) : create_array(buffer2);
|
|
@@ -50262,7 +50251,7 @@ var require_RandomIdGenerator = __commonJS((exports) => {
|
|
|
50262
50251
|
exports.RandomIdGenerator = RandomIdGenerator;
|
|
50263
50252
|
var SHARED_BUFFER = Buffer.allocUnsafe(TRACE_ID_BYTES);
|
|
50264
50253
|
function getIdGenerator(bytes) {
|
|
50265
|
-
return function
|
|
50254
|
+
return function generateId2() {
|
|
50266
50255
|
for (let i = 0;i < bytes / 4; i++) {
|
|
50267
50256
|
SHARED_BUFFER.writeUInt32BE(Math.random() * 2 ** 32 >>> 0, i * 4);
|
|
50268
50257
|
}
|
|
@@ -56674,6 +56663,16 @@ class UseAIServer {
|
|
|
56674
56663
|
data: block.url,
|
|
56675
56664
|
mediaType: block.mimeType || "application/octet-stream"
|
|
56676
56665
|
});
|
|
56666
|
+
} else if (block.type === "transformed_file" && "text" in block) {
|
|
56667
|
+
const originalFile = block.originalFile;
|
|
56668
|
+
const fileName = originalFile?.name || "file";
|
|
56669
|
+
const mimeType = originalFile?.mimeType || "application/octet-stream";
|
|
56670
|
+
parts.push({
|
|
56671
|
+
type: "text",
|
|
56672
|
+
text: `[Content of file "${fileName}" (${mimeType})]:
|
|
56673
|
+
|
|
56674
|
+
${block.text}`
|
|
56675
|
+
});
|
|
56677
56676
|
}
|
|
56678
56677
|
}
|
|
56679
56678
|
if (parts.length === 1 && parts[0].type === "text") {
|
|
@@ -80244,6 +80243,53 @@ function initializeLangfuse() {
|
|
|
80244
80243
|
}
|
|
80245
80244
|
}
|
|
80246
80245
|
|
|
80246
|
+
// src/agents/anthropicCache.ts
|
|
80247
|
+
function isAnthropicModel(model) {
|
|
80248
|
+
if (typeof model === "string") {
|
|
80249
|
+
return model.startsWith("anthropic/");
|
|
80250
|
+
}
|
|
80251
|
+
const provider = model.provider;
|
|
80252
|
+
return provider?.startsWith("anthropic") ?? false;
|
|
80253
|
+
}
|
|
80254
|
+
function applyCacheBreakpoints(messages, cacheBreakpoint, model) {
|
|
80255
|
+
if (!cacheBreakpoint || !isAnthropicModel(model)) {
|
|
80256
|
+
return messages;
|
|
80257
|
+
}
|
|
80258
|
+
const totalCount = messages.length;
|
|
80259
|
+
let cacheBreakpointCount = 0;
|
|
80260
|
+
const result = messages.map((message, index) => {
|
|
80261
|
+
const context = {
|
|
80262
|
+
...message,
|
|
80263
|
+
index,
|
|
80264
|
+
totalCount,
|
|
80265
|
+
isFirst: index === 0,
|
|
80266
|
+
isLast: index === totalCount - 1
|
|
80267
|
+
};
|
|
80268
|
+
const breakpointResult = cacheBreakpoint(context);
|
|
80269
|
+
if (breakpointResult) {
|
|
80270
|
+
cacheBreakpointCount++;
|
|
80271
|
+
const cacheControl = typeof breakpointResult === "string" ? { type: "ephemeral", ttl: breakpointResult } : { type: "ephemeral" };
|
|
80272
|
+
return {
|
|
80273
|
+
...message,
|
|
80274
|
+
providerOptions: {
|
|
80275
|
+
...message.providerOptions,
|
|
80276
|
+
anthropic: {
|
|
80277
|
+
cacheControl
|
|
80278
|
+
}
|
|
80279
|
+
}
|
|
80280
|
+
};
|
|
80281
|
+
}
|
|
80282
|
+
return message;
|
|
80283
|
+
});
|
|
80284
|
+
if (cacheBreakpointCount > 0) {
|
|
80285
|
+
logger2.debug("Applied cache breakpoints", {
|
|
80286
|
+
totalMessages: totalCount,
|
|
80287
|
+
cacheBreakpointCount
|
|
80288
|
+
});
|
|
80289
|
+
}
|
|
80290
|
+
return result;
|
|
80291
|
+
}
|
|
80292
|
+
|
|
80247
80293
|
// src/agents/AISDKAgent.ts
|
|
80248
80294
|
class AISDKAgent {
|
|
80249
80295
|
model;
|
|
@@ -80252,12 +80298,14 @@ class AISDKAgent {
|
|
|
80252
80298
|
langfuse;
|
|
80253
80299
|
toolFilter;
|
|
80254
80300
|
systemPrompt;
|
|
80301
|
+
cacheBreakpoint;
|
|
80255
80302
|
constructor(config2) {
|
|
80256
80303
|
this.model = config2.model;
|
|
80257
80304
|
this.name = config2.name || "ai-sdk";
|
|
80258
80305
|
this.annotation = config2.annotation;
|
|
80259
80306
|
this.toolFilter = config2.toolFilter;
|
|
80260
80307
|
this.systemPrompt = config2.systemPrompt;
|
|
80308
|
+
this.cacheBreakpoint = config2.cacheBreakpoint;
|
|
80261
80309
|
this.langfuse = initializeLangfuse();
|
|
80262
80310
|
}
|
|
80263
80311
|
getName() {
|
|
@@ -80312,9 +80360,10 @@ class AISDKAgent {
|
|
|
80312
80360
|
})),
|
|
80313
80361
|
systemMessages: systemMessages?.map((m) => m.content.substring(0, 80) + (m.content.length > 80 ? "..." : ""))
|
|
80314
80362
|
});
|
|
80363
|
+
const messagesWithCache = applyCacheBreakpoints(messagesWithSystem, this.cacheBreakpoint, this.model);
|
|
80315
80364
|
const stream = streamText({
|
|
80316
80365
|
model: this.model,
|
|
80317
|
-
messages:
|
|
80366
|
+
messages: messagesWithCache,
|
|
80318
80367
|
tools: tools.length > 0 ? this.sanitizeToolsForAPI(this.filterTools(tools), session) : undefined,
|
|
80319
80368
|
stopWhen: stepCountIs(10),
|
|
80320
80369
|
maxOutputTokens: 4096,
|
|
@@ -80621,10 +80670,25 @@ class AISDKAgent {
|
|
|
80621
80670
|
data: exports_external2.string(),
|
|
80622
80671
|
mediaType: exports_external2.string()
|
|
80623
80672
|
}).strip();
|
|
80673
|
+
static transformedFileContentSchema = exports_external2.object({
|
|
80674
|
+
type: exports_external2.literal("transformed_file"),
|
|
80675
|
+
text: exports_external2.string(),
|
|
80676
|
+
originalFile: exports_external2.object({
|
|
80677
|
+
name: exports_external2.string(),
|
|
80678
|
+
mimeType: exports_external2.string(),
|
|
80679
|
+
size: exports_external2.number()
|
|
80680
|
+
})
|
|
80681
|
+
}).transform((val) => ({
|
|
80682
|
+
type: "text",
|
|
80683
|
+
text: `[Content of file "${val.originalFile.name}" (${val.originalFile.mimeType})]:
|
|
80684
|
+
|
|
80685
|
+
${val.text}`
|
|
80686
|
+
}));
|
|
80624
80687
|
static contentPartSchema = exports_external2.union([
|
|
80625
80688
|
AISDKAgent.textContentSchema,
|
|
80626
80689
|
AISDKAgent.imageContentSchema,
|
|
80627
80690
|
AISDKAgent.fileContentSchema,
|
|
80691
|
+
AISDKAgent.transformedFileContentSchema,
|
|
80628
80692
|
AISDKAgent.toolCallContentSchema,
|
|
80629
80693
|
AISDKAgent.toolResultContentSchema
|
|
80630
80694
|
]);
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,iBAAiB,EAWlB,MAAM,SAAS,CAAC;AAMjB,OAAO,KAAK,EAAqB,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAKzE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,EAAE,CAAiB;IAC3B,OAAO,CAAC,UAAU,CAAkC;IACpD,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,MAAM,CAAwB;IACtC,OAAO,CAAC,OAAO,CAAyC;IACxD,OAAO,CAAC,MAAM,CAA+K;IAC7L,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,eAAe,CAAiB;IACxC,OAAO,CAAC,eAAe,CAAK;IAC5B,OAAO,CAAC,OAAO,CAA2B;IAC1C,OAAO,CAAC,eAAe,CAA0C;IACjE,OAAO,CAAC,YAAY,CAAgC;IAEpD;;;;;OAKG;gBACS,MAAM,EAAE,iBAAiB;IAyErC;;;OAGG;IACG,UAAU,IAAI,OAAO,CA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nB,cAAc;IAyQ5B,OAAO,CAAC,iBAAiB;IAqCzB,OAAO,CAAC,gBAAgB;IAUxB,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,SAAS;IAMjB;;;;;;;;;;;OAWG;YACW,qBAAqB;IA+CnC;;;;;;;OAOG;IACH,OAAO,CAAC,oBAAoB;IAwC5B;;;;;OAKG;IACH,OAAO,CAAC,cAAc;IAoBtB;;;;;;OAMG;IACH,OAAO,CAAC,yBAAyB;IAYjC;;;OAGG;IACI,KAAK;CASb"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LanguageModel } from 'ai';
|
|
2
2
|
import type { Agent, AgentInput, EventEmitter, AgentResult } from './types';
|
|
3
3
|
import type { ToolDefinition } from '../types';
|
|
4
|
+
import { type CacheBreakpointFn } from './anthropicCache';
|
|
4
5
|
/**
|
|
5
6
|
* Configuration for AISDKAgent.
|
|
6
7
|
*/
|
|
@@ -99,6 +100,53 @@ export interface AISDKAgentConfig {
|
|
|
99
100
|
* ```
|
|
100
101
|
*/
|
|
101
102
|
toolFilter?: (tool: ToolDefinition) => boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Anthropic-specific: Configure cache breakpoints for prompt caching.
|
|
105
|
+
* Only applies when using Anthropic models (Claude).
|
|
106
|
+
*
|
|
107
|
+
* Prompt caching reduces costs and latency by caching message prefixes.
|
|
108
|
+
* Cache breakpoints mark where the cacheable prefix ends.
|
|
109
|
+
*
|
|
110
|
+
* The function receives each message with positional context and returns
|
|
111
|
+
* true to add a cache breakpoint after that message.
|
|
112
|
+
*
|
|
113
|
+
* System prompt is included as role: 'system' at index 0 when present.
|
|
114
|
+
*
|
|
115
|
+
* @see https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Cache system prompt + last message (most common pattern)
|
|
120
|
+
* {
|
|
121
|
+
* cacheBreakpoint: (msg) => msg.role === 'system' || msg.isLast
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* // Cache only the last message
|
|
125
|
+
* {
|
|
126
|
+
* cacheBreakpoint: (msg) => msg.isLast
|
|
127
|
+
* }
|
|
128
|
+
*
|
|
129
|
+
* // Cache system prompt only
|
|
130
|
+
* {
|
|
131
|
+
* cacheBreakpoint: (msg) => msg.role === 'system'
|
|
132
|
+
* }
|
|
133
|
+
*
|
|
134
|
+
* // Cache first 3 messages + last
|
|
135
|
+
* {
|
|
136
|
+
* cacheBreakpoint: (msg) => msg.index < 3 || msg.isLast
|
|
137
|
+
* }
|
|
138
|
+
*
|
|
139
|
+
* // System prompt with 1h TTL, last message with 5m TTL
|
|
140
|
+
* {
|
|
141
|
+
* cacheBreakpoint: (msg) => {
|
|
142
|
+
* if (msg.role === 'system') return '1h';
|
|
143
|
+
* if (msg.isLast) return '5m';
|
|
144
|
+
* return false;
|
|
145
|
+
* }
|
|
146
|
+
* }
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
cacheBreakpoint?: CacheBreakpointFn;
|
|
102
150
|
}
|
|
103
151
|
/**
|
|
104
152
|
* Agent implementation for AI SDK models (Anthropic, OpenAI, Google, etc.).
|
|
@@ -151,6 +199,7 @@ export declare class AISDKAgent implements Agent {
|
|
|
151
199
|
private langfuse;
|
|
152
200
|
private toolFilter?;
|
|
153
201
|
private systemPrompt?;
|
|
202
|
+
private cacheBreakpoint?;
|
|
154
203
|
constructor(config: AISDKAgentConfig);
|
|
155
204
|
getName(): string;
|
|
156
205
|
getAnnotation(): string | undefined;
|
|
@@ -203,6 +252,12 @@ export declare class AISDKAgent implements Agent {
|
|
|
203
252
|
private static readonly textContentSchema;
|
|
204
253
|
private static readonly imageContentSchema;
|
|
205
254
|
private static readonly fileContentSchema;
|
|
255
|
+
/**
|
|
256
|
+
* Schema for transformed file content.
|
|
257
|
+
* Transforms the input to a text content part with file context.
|
|
258
|
+
* This allows file transformers on the client to send pre-processed file content.
|
|
259
|
+
*/
|
|
260
|
+
private static readonly transformedFileContentSchema;
|
|
206
261
|
private static readonly contentPartSchema;
|
|
207
262
|
private static readonly messageSchema;
|
|
208
263
|
private static readonly messagesArraySchema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AISDKAgent.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,aAAa,EAA2D,MAAM,IAAI,CAAC;AAIpH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAiB,MAAM,SAAS,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"AISDKAgent.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,aAAa,EAA2D,MAAM,IAAI,CAAC;AAIpH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAiB,MAAM,SAAS,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAqB/C,OAAO,EAAyB,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AA0BjF;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,EAAE,aAAa,CAAC;IAErB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;;;;;;;OAUG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,YAAY,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAEzD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAA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,CAI9B;IAEX;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,4BAA4B,CAWhD;IAEJ,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAOtC;IAEH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAM1B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,mBAAmB,CAAqC;IAEhF;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;CAgBzB"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic prompt caching utilities for AI SDK Agent.
|
|
3
|
+
*
|
|
4
|
+
* This module handles cache breakpoint configuration for Anthropic models (Claude).
|
|
5
|
+
* Prompt caching reduces costs and latency by caching message prefixes.
|
|
6
|
+
*
|
|
7
|
+
* @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching
|
|
8
|
+
*/
|
|
9
|
+
import type { LanguageModel, ModelMessage } from 'ai';
|
|
10
|
+
/**
|
|
11
|
+
* ModelMessage extended with positional context for cache breakpoint decisions.
|
|
12
|
+
* Used by the cacheBreakpoint config to determine which messages should have
|
|
13
|
+
* Anthropic's cache_control breakpoints applied.
|
|
14
|
+
*
|
|
15
|
+
* System prompt is included as role: 'system' at index 0 when present.
|
|
16
|
+
*/
|
|
17
|
+
export type MessageWithCacheContext = ModelMessage & {
|
|
18
|
+
/** Position in the messages array (0-indexed, system prompt is 0 if present) */
|
|
19
|
+
index: number;
|
|
20
|
+
/** Total number of messages including system prompt */
|
|
21
|
+
totalCount: number;
|
|
22
|
+
/** True if this is the first message */
|
|
23
|
+
isFirst: boolean;
|
|
24
|
+
/** True if this is the last message */
|
|
25
|
+
isLast: boolean;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Cache TTL options for Anthropic prompt caching.
|
|
29
|
+
* - '5m': 5-minute cache (default, refreshed on each use at no cost)
|
|
30
|
+
* - '1h': 1-hour cache (additional cost, useful for infrequent access patterns)
|
|
31
|
+
*
|
|
32
|
+
* @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching#1-hour-cache-duration
|
|
33
|
+
*/
|
|
34
|
+
export type CacheTtl = '5m' | '1h';
|
|
35
|
+
/**
|
|
36
|
+
* Return type for cacheBreakpoint function.
|
|
37
|
+
* - `false` / `null` / `undefined`: No cache breakpoint
|
|
38
|
+
* - `true`: Cache breakpoint with default TTL (5m)
|
|
39
|
+
* - `'5m'` / `'1h'`: Cache breakpoint with specified TTL
|
|
40
|
+
*/
|
|
41
|
+
export type CacheBreakpointResult = boolean | CacheTtl | null | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Function type for determining cache breakpoints.
|
|
44
|
+
*/
|
|
45
|
+
export type CacheBreakpointFn = (message: MessageWithCacheContext) => CacheBreakpointResult;
|
|
46
|
+
/**
|
|
47
|
+
* Checks if the given model is an Anthropic model (Claude).
|
|
48
|
+
*/
|
|
49
|
+
export declare function isAnthropicModel(model: LanguageModel): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Applies cache breakpoints to messages for Anthropic prompt caching.
|
|
52
|
+
* Only applies when:
|
|
53
|
+
* 1. cacheBreakpoint config is provided
|
|
54
|
+
* 2. Model is an Anthropic model
|
|
55
|
+
*
|
|
56
|
+
* Adds providerOptions.anthropic.cacheControl to messages where
|
|
57
|
+
* the cacheBreakpoint function returns true.
|
|
58
|
+
*
|
|
59
|
+
* @param messages - The messages array (system prompt should be prepended as role: 'system')
|
|
60
|
+
* @param cacheBreakpoint - Function to determine which messages should have cache breakpoints
|
|
61
|
+
* @param model - The AI SDK LanguageModel to check if it's Anthropic
|
|
62
|
+
* @returns Messages with cache control providerOptions added where applicable
|
|
63
|
+
*/
|
|
64
|
+
export declare function applyCacheBreakpoints(messages: ModelMessage[], cacheBreakpoint: CacheBreakpointFn | undefined, model: LanguageModel): ModelMessage[];
|
|
65
|
+
//# sourceMappingURL=anthropicCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropicCache.d.ts","sourceRoot":"","sources":["../../../src/agents/anthropicCache.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAGtD;;;;;;GAMG;AACH,MAAM,MAAM,uBAAuB,GAAG,YAAY,GAAG;IACnD,gFAAgF;IAChF,KAAK,EAAE,MAAM,CAAC;IACd,uDAAuD;IACvD,UAAU,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,OAAO,EAAE,OAAO,CAAC;IACjB,uCAAuC;IACvC,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC;AAEnC;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,GAAG,OAAO,GAAG,QAAQ,GAAG,IAAI,GAAG,SAAS,CAAC;AAE1E;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,uBAAuB,KAAK,qBAAqB,CAAC;AAE5F;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAQ9D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,YAAY,EAAE,EACxB,eAAe,EAAE,iBAAiB,GAAG,SAAS,EAC9C,KAAK,EAAE,aAAa,GACnB,YAAY,EAAE,CAyDhB"}
|
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export type { Agent, AgentInput, EventEmitter, AgentResult, ClientSession } from './types';
|
|
6
6
|
export { AISDKAgent, type AISDKAgentConfig } from './AISDKAgent';
|
|
7
|
+
export { applyCacheBreakpoints, isAnthropicModel, type MessageWithCacheContext, type CacheTtl, type CacheBreakpointResult, type CacheBreakpointFn, } from './anthropicCache';
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,uBAAuB,EAC5B,KAAK,QAAQ,EACb,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { UseAIServer } from './server';
|
|
|
2
2
|
export type { UseAIServerConfig, McpEndpointConfig, ToolDefinition, CorsOptions } from './types';
|
|
3
3
|
export type { ClientSession } from './server';
|
|
4
4
|
export type { Agent, AgentInput, EventEmitter, AgentResult } from './agents';
|
|
5
|
-
export { AISDKAgent, type AISDKAgentConfig } from './agents';
|
|
5
|
+
export { AISDKAgent, type AISDKAgentConfig, type MessageWithCacheContext, type CacheTtl, type CacheBreakpointResult, type CacheBreakpointFn } from './agents';
|
|
6
6
|
export type { UseAIServerPlugin, MessageHandler } from './plugins';
|
|
7
7
|
export { logger } from './logger';
|
|
8
8
|
export { createClientToolExecutor, isRemoteTool, createGlobFilter, and, or, not, } from './utils';
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjG,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACjG,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,KAAK,QAAQ,EAAE,KAAK,qBAAqB,EAAE,KAAK,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAG9J,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGnE,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,OAAO,EACL,wBAAwB,EACxB,YAAY,EACZ,gBAAgB,EAChB,GAAG,EACH,EAAE,EACF,GAAG,GACJ,MAAM,SAAS,CAAC"}
|
package/dist/src/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../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