@meetsmore-oss/use-ai-server 1.2.3 → 1.2.4

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 (47) hide show
  1. package/dist/agents/AISDKAgent.d.ts +49 -0
  2. package/dist/agents/AISDKAgent.d.ts.map +1 -1
  3. package/dist/agents/anthropicCache.d.ts +65 -0
  4. package/dist/agents/anthropicCache.d.ts.map +1 -0
  5. package/dist/agents/index.d.ts +1 -0
  6. package/dist/agents/index.d.ts.map +1 -1
  7. package/dist/agents/plugins/__tests__/runner.test.d.ts +2 -0
  8. package/dist/agents/plugins/__tests__/runner.test.d.ts.map +1 -0
  9. package/dist/agents/plugins/citations/citationPlugin.d.ts +72 -0
  10. package/dist/agents/plugins/citations/citationPlugin.d.ts.map +1 -0
  11. package/dist/agents/plugins/citations/citationPlugin.test.d.ts +2 -0
  12. package/dist/agents/plugins/citations/citationPlugin.test.d.ts.map +1 -0
  13. package/dist/agents/plugins/citations/index.d.ts +7 -0
  14. package/dist/agents/plugins/citations/index.d.ts.map +1 -0
  15. package/dist/agents/plugins/index.d.ts +34 -0
  16. package/dist/agents/plugins/index.d.ts.map +1 -0
  17. package/dist/agents/plugins/runner.d.ts +76 -0
  18. package/dist/agents/plugins/runner.d.ts.map +1 -0
  19. package/dist/agents/plugins/types.d.ts +280 -0
  20. package/dist/agents/plugins/types.d.ts.map +1 -0
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +51 -1
  24. package/dist/src/agents/AISDKAgent.d.ts +49 -0
  25. package/dist/src/agents/AISDKAgent.d.ts.map +1 -1
  26. package/dist/src/agents/anthropicCache.d.ts +65 -0
  27. package/dist/src/agents/anthropicCache.d.ts.map +1 -0
  28. package/dist/src/agents/index.d.ts +1 -0
  29. package/dist/src/agents/index.d.ts.map +1 -1
  30. package/dist/src/agents/plugins/__tests__/runner.test.d.ts +2 -0
  31. package/dist/src/agents/plugins/__tests__/runner.test.d.ts.map +1 -0
  32. package/dist/src/agents/plugins/citations/citationPlugin.d.ts +72 -0
  33. package/dist/src/agents/plugins/citations/citationPlugin.d.ts.map +1 -0
  34. package/dist/src/agents/plugins/citations/citationPlugin.test.d.ts +2 -0
  35. package/dist/src/agents/plugins/citations/citationPlugin.test.d.ts.map +1 -0
  36. package/dist/src/agents/plugins/citations/index.d.ts +7 -0
  37. package/dist/src/agents/plugins/citations/index.d.ts.map +1 -0
  38. package/dist/src/agents/plugins/index.d.ts +34 -0
  39. package/dist/src/agents/plugins/index.d.ts.map +1 -0
  40. package/dist/src/agents/plugins/runner.d.ts +76 -0
  41. package/dist/src/agents/plugins/runner.d.ts.map +1 -0
  42. package/dist/src/agents/plugins/types.d.ts +280 -0
  43. package/dist/src/agents/plugins/types.d.ts.map +1 -0
  44. package/dist/src/index.d.ts +1 -1
  45. package/dist/src/index.d.ts.map +1 -1
  46. package/dist/tsconfig.tsbuildinfo +1 -1
  47. 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;
@@ -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;AA8C/C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,EAAE,aAAa,CAAC;IAErB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;;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B,CAMtC;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"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=runner.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runner.test.d.ts","sourceRoot":"","sources":["../../../../../src/agents/plugins/__tests__/runner.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,72 @@
1
+ import type { AgentPlugin } from '../types';
2
+ /**
3
+ * Citation source that can be included in tool results.
4
+ * Tools (like RAG) can return citations in their results using this format.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * // In your RAG tool implementation:
9
+ * return {
10
+ * answer: "Based on the documentation...",
11
+ * _citations: [
12
+ * { url: "https://docs.example.com/api", title: "API Reference" },
13
+ * { url: "https://docs.example.com/guide", title: "Getting Started Guide" },
14
+ * ]
15
+ * };
16
+ * ```
17
+ */
18
+ export interface ToolCitationSource {
19
+ /** URL of the source document */
20
+ url: string;
21
+ /** Title of the source (optional) */
22
+ title?: string;
23
+ /** Additional metadata (optional) */
24
+ metadata?: Record<string, unknown>;
25
+ }
26
+ /**
27
+ * Available citation types.
28
+ *
29
+ * - `anthropic`: Extracts from Anthropic web search source chunks (streaming) and result.sources
30
+ * - `openai`: Extracts from OpenAI url_citation annotations in response messages
31
+ * - `rag`: Extracts from tool results with `_citations`, `citations`, or `sources` fields
32
+ */
33
+ export type CitationType = 'rag';
34
+ /**
35
+ * Plugin that handles citation collection and emission.
36
+ *
37
+ * Supports multiple citation sources:
38
+ * - **rag**: Citations in tool results via `_citations`, `citations`, or `sources` fields
39
+ * - **anthropic**: Source chunks during streaming + result.sources (for future use)
40
+ * - **openai**: url_citation annotations in response (for future use)
41
+ *
42
+ * @param enabledTypes - Which extractors to enable. If not provided, all extractors are enabled.
43
+ * Extractors run in the order specified.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // With custom RAG tool - return citations in the tool result
48
+ * import { AISDKAgent, citationPlugin } from '@meetsmore-oss/use-ai-server';
49
+ *
50
+ * const ragTool = defineTool(
51
+ * 'Search internal documentation',
52
+ * z.object({ query: z.string() }),
53
+ * async ({ query }) => {
54
+ * const results = await vectorDb.search(query);
55
+ * return {
56
+ * answer: results.map(r => r.content).join('\n'),
57
+ * _citations: results.map(r => ({
58
+ * url: r.sourceUrl,
59
+ * title: r.title,
60
+ * })),
61
+ * };
62
+ * }
63
+ * );
64
+ *
65
+ * const agent = new AISDKAgent({
66
+ * model: anthropic('claude-sonnet-4-20250514'),
67
+ * plugins: [citationPlugin(['rag'])],
68
+ * });
69
+ * ```
70
+ */
71
+ export declare function citationPlugin(enabledTypes?: CitationType[]): AgentPlugin;
72
+ //# sourceMappingURL=citationPlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"citationPlugin.d.ts","sourceRoot":"","sources":["../../../../../src/agents/plugins/citations/citationPlugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAgF,MAAM,UAAU,CAAC;AAI1H;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,kBAAkB;IACjC,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,qCAAqC;IACrC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qCAAqC;IACrC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,CAAC;AAoPjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,cAAc,CAAC,YAAY,CAAC,EAAE,YAAY,EAAE,GAAG,WAAW,CA4CzE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=citationPlugin.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"citationPlugin.test.d.ts","sourceRoot":"","sources":["../../../../../src/agents/plugins/citations/citationPlugin.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Citation plugin for AISDKAgent.
3
+ * Collects sources from AI responses and emits citation events.
4
+ */
5
+ export { citationPlugin } from './citationPlugin';
6
+ export type { ToolCitationSource, CitationType } from './citationPlugin';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/agents/plugins/citations/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,YAAY,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Agent plugin system for extending AISDKAgent functionality.
3
+ *
4
+ * Plugins can hook into the agent lifecycle to:
5
+ * - Modify inputs before sending to AI
6
+ * - Transform streaming chunks
7
+ * - Intercept or modify tool calls
8
+ * - Process and transform responses
9
+ * - Handle errors
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { AISDKAgent } from '@meetsmore-oss/use-ai-server';
14
+ * import type { AgentPlugin } from '@meetsmore-oss/use-ai-server';
15
+ *
16
+ * const loggingPlugin: AgentPlugin = {
17
+ * id: 'logging',
18
+ * onUserMessage(input, ctx) {
19
+ * ctx.logger.info('Message received');
20
+ * return input;
21
+ * },
22
+ * };
23
+ *
24
+ * const agent = new AISDKAgent({
25
+ * model: anthropic('claude-3-5-sonnet-20241022'),
26
+ * plugins: [loggingPlugin],
27
+ * });
28
+ * ```
29
+ */
30
+ export type { AgentPlugin, AgentPluginContext, AgentRunInput, AgentRunResult, ToolCallInfo, ToolResultInfo, StreamChunk, TextDeltaChunk, ReasoningDeltaChunk, SourceChunk, ToolInputStartChunk, ToolInputDeltaChunk, ToolCallChunk, ToolResultChunk, StartStepChunk, FinishStepChunk, ErrorChunk, UnknownChunk, } from './types';
31
+ export { AgentPluginRunner } from './runner';
32
+ export { citationPlugin } from './citations';
33
+ export type { ToolCitationSource, CitationType } from './citations';
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/agents/plugins/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,YAAY,EACV,WAAW,EACX,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,YAAY,EACZ,cAAc,EAEd,WAAW,EACX,cAAc,EACd,mBAAmB,EACnB,WAAW,EACX,mBAAmB,EACnB,mBAAmB,EACnB,aAAa,EACb,eAAe,EACf,cAAc,EACd,eAAe,EACf,UAAU,EACV,YAAY,GACb,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAG7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,YAAY,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,76 @@
1
+ import type { AgentPlugin, AgentPluginContext, AgentRunInput, AgentRunResult, ToolCallInfo, ToolResultInfo, StreamChunk } from './types';
2
+ /**
3
+ * Orchestrates plugin execution for AISDKAgent.
4
+ *
5
+ * Plugins are executed in the order they were registered.
6
+ * Each hook is called sequentially, with the output of one plugin
7
+ * becoming the input of the next.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const runner = new AgentPluginRunner([
12
+ * loggingPlugin,
13
+ * metricsPlugin,
14
+ * ]);
15
+ *
16
+ * // Initialize plugins
17
+ * await runner.initialize({ provider: 'anthropic' });
18
+ *
19
+ * // Use in agent run
20
+ * const processedInput = await runner.onUserMessage(input, context);
21
+ * ```
22
+ */
23
+ export declare class AgentPluginRunner {
24
+ private plugins;
25
+ constructor(plugins?: AgentPlugin[]);
26
+ /**
27
+ * Initialize all plugins.
28
+ * Called once when the agent is created.
29
+ */
30
+ initialize(context: {
31
+ provider: string;
32
+ }): Promise<void>;
33
+ /**
34
+ * Run onUserMessage hook for all plugins.
35
+ * Each plugin receives the output of the previous plugin.
36
+ */
37
+ onUserMessage(input: AgentRunInput, context: AgentPluginContext): Promise<AgentRunInput>;
38
+ /**
39
+ * Run onAgentResponse hook for all plugins.
40
+ * Each plugin receives the output of the previous plugin.
41
+ */
42
+ onAgentResponse(result: AgentRunResult, context: AgentPluginContext): Promise<AgentRunResult>;
43
+ /**
44
+ * Run onTextChunk hook for all plugins.
45
+ * Returns the transformed chunk (or original if no transform).
46
+ */
47
+ onTextChunk(chunk: string, context: AgentPluginContext): Promise<string>;
48
+ /**
49
+ * Run onBeforeToolCall hook for all plugins.
50
+ * Returns null if any plugin returns null (tool call should be skipped).
51
+ */
52
+ onBeforeToolCall(toolCall: ToolCallInfo, context: AgentPluginContext): Promise<ToolCallInfo | null>;
53
+ /**
54
+ * Run onAfterToolCall hook for all plugins.
55
+ * Returns the final transformed result.
56
+ */
57
+ onAfterToolCall(toolResult: ToolResultInfo, context: AgentPluginContext): Promise<unknown>;
58
+ /**
59
+ * Run onChunk hook for all plugins.
60
+ * Called for each chunk from the AI SDK stream.
61
+ */
62
+ onChunk(chunk: StreamChunk, context: AgentPluginContext): Promise<void>;
63
+ /**
64
+ * Destroy all plugins and release resources.
65
+ */
66
+ destroy(): Promise<void>;
67
+ /**
68
+ * Check if any plugins are registered.
69
+ */
70
+ hasPlugins(): boolean;
71
+ /**
72
+ * Get the number of registered plugins.
73
+ */
74
+ get pluginCount(): number;
75
+ }
76
+ //# sourceMappingURL=runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../../../src/agents/plugins/runner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,YAAY,EACZ,cAAc,EACd,WAAW,EACZ,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,OAAO,CAAqB;gBAExB,OAAO,GAAE,WAAW,EAAO;IAIvC;;;OAGG;IACG,UAAU,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAM9D;;;OAGG;IACG,aAAa,CACjB,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,aAAa,CAAC;IAUzB;;;OAGG;IACG,eAAe,CACnB,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,cAAc,CAAC;IAU1B;;;OAGG;IACG,WAAW,CACf,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,MAAM,CAAC;IAalB;;;OAGG;IACG,gBAAgB,CACpB,QAAQ,EAAE,YAAY,EACtB,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAW/B;;;OAGG;IACG,eAAe,CACnB,UAAU,EAAE,cAAc,EAC1B,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,OAAO,CAAC;IAUnB;;;OAGG;IACG,OAAO,CACX,KAAK,EAAE,WAAW,EAClB,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,IAAI,CAAC;IAQhB;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAM9B;;OAEG;IACH,UAAU,IAAI,OAAO;IAIrB;;OAEG;IACH,IAAI,WAAW,IAAI,MAAM,CAExB;CACF"}
@@ -0,0 +1,280 @@
1
+ import type { ModelMessage, SystemModelMessage } from 'ai';
2
+ import type { EventEmitter, ClientSession } from '../types';
3
+ import type { ToolDefinition } from '../../types';
4
+ import type { Logger } from '../../logger';
5
+ /**
6
+ * Context available to plugins during a run.
7
+ * Contains session info, event emitter, and shared state.
8
+ */
9
+ export interface AgentPluginContext {
10
+ /** Unique identifier for this run */
11
+ runId: string;
12
+ /** Client identifier from the session */
13
+ clientId: string;
14
+ /** Thread identifier for the conversation */
15
+ threadId?: string;
16
+ /** AI provider name (e.g., 'openai', 'anthropic') */
17
+ provider: string;
18
+ /** Event emitter for sending AG-UI events */
19
+ events: EventEmitter;
20
+ /** Shared state between plugins within a single run */
21
+ state: Map<string, unknown>;
22
+ /** Logger instance */
23
+ logger: Logger;
24
+ /** The client session */
25
+ session: ClientSession;
26
+ }
27
+ /**
28
+ * Input data passed to onUserMessage hook.
29
+ * Plugins can modify these values before they're sent to the AI SDK.
30
+ */
31
+ export interface AgentRunInput {
32
+ /** Conversation messages in AI SDK format */
33
+ messages: ModelMessage[];
34
+ /** System messages (if any). Passed directly to AI SDK. */
35
+ systemMessages?: SystemModelMessage[];
36
+ /** Available tools for this run */
37
+ tools: ToolDefinition[];
38
+ }
39
+ /**
40
+ * Result data passed to onAgentResponse hook.
41
+ * Plugins can process or transform the response.
42
+ */
43
+ export interface AgentRunResult {
44
+ /** The generated text response */
45
+ text: string;
46
+ /** Sources/citations from the AI response */
47
+ sources?: unknown[];
48
+ /** Raw response object from AI SDK */
49
+ response?: {
50
+ messages: unknown[];
51
+ };
52
+ /** Provider-specific metadata */
53
+ providerMetadata?: Record<string, unknown>;
54
+ /** Tool calls made during the run */
55
+ toolCalls?: unknown[];
56
+ /** Results from tool executions */
57
+ toolResults?: unknown[];
58
+ /** Token usage information */
59
+ usage?: {
60
+ promptTokens: number;
61
+ completionTokens: number;
62
+ };
63
+ }
64
+ /**
65
+ * Information about a tool call (before execution).
66
+ */
67
+ export interface ToolCallInfo {
68
+ /** Unique identifier for this tool call */
69
+ id: string;
70
+ /** Name of the tool being called */
71
+ name: string;
72
+ /** Arguments passed to the tool */
73
+ args: unknown;
74
+ }
75
+ /**
76
+ * Information about a tool result (after execution).
77
+ */
78
+ export interface ToolResultInfo extends ToolCallInfo {
79
+ /** Result returned from the tool */
80
+ result: unknown;
81
+ /** Error if the tool execution failed */
82
+ error?: Error;
83
+ }
84
+ /**
85
+ * Known AI SDK stream chunk types.
86
+ * These are the chunk types commonly emitted by the AI SDK fullStream.
87
+ */
88
+ /** Text content being streamed */
89
+ export interface TextDeltaChunk {
90
+ type: 'text-delta';
91
+ text: string;
92
+ }
93
+ /** Extended thinking/reasoning content (Claude) */
94
+ export interface ReasoningDeltaChunk {
95
+ type: 'reasoning-delta';
96
+ text: string;
97
+ }
98
+ /** Source/citation from web search or RAG */
99
+ export interface SourceChunk {
100
+ type: 'source';
101
+ id: string;
102
+ sourceType: string;
103
+ url?: string;
104
+ title?: string;
105
+ providerMetadata?: Record<string, unknown>;
106
+ }
107
+ /** Tool call starting to stream */
108
+ export interface ToolInputStartChunk {
109
+ type: 'tool-input-start';
110
+ id: string;
111
+ toolName: string;
112
+ }
113
+ /** Tool arguments being streamed */
114
+ export interface ToolInputDeltaChunk {
115
+ type: 'tool-input-delta';
116
+ id: string;
117
+ delta: string;
118
+ }
119
+ /** Tool call complete with parsed input */
120
+ export interface ToolCallChunk {
121
+ type: 'tool-call';
122
+ toolCallId: string;
123
+ toolName: string;
124
+ input: unknown;
125
+ }
126
+ /** Tool execution result */
127
+ export interface ToolResultChunk {
128
+ type: 'tool-result';
129
+ toolCallId: string;
130
+ toolName: string;
131
+ output: unknown;
132
+ }
133
+ /** Step started in multi-step execution */
134
+ export interface StartStepChunk {
135
+ type: 'start-step';
136
+ }
137
+ /** Step finished in multi-step execution */
138
+ export interface FinishStepChunk {
139
+ type: 'finish-step';
140
+ usage?: {
141
+ promptTokens: number;
142
+ completionTokens: number;
143
+ };
144
+ finishReason?: string;
145
+ }
146
+ /** Stream error */
147
+ export interface ErrorChunk {
148
+ type: 'error';
149
+ error: Error;
150
+ }
151
+ /** Unknown chunk type (fallback for future AI SDK chunk types) */
152
+ export interface UnknownChunk {
153
+ type: string;
154
+ [key: string]: unknown;
155
+ }
156
+ /**
157
+ * Union of all known AI SDK stream chunk types.
158
+ * Plugins can use type narrowing with `chunk.type` to handle specific chunk types.
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * onChunk(chunk, context) {
163
+ * if (chunk.type === 'source') {
164
+ * // TypeScript knows chunk is SourceChunk here
165
+ * console.log(chunk.url);
166
+ * }
167
+ * }
168
+ * ```
169
+ */
170
+ export type StreamChunk = TextDeltaChunk | ReasoningDeltaChunk | SourceChunk | ToolInputStartChunk | ToolInputDeltaChunk | ToolCallChunk | ToolResultChunk | StartStepChunk | FinishStepChunk | ErrorChunk | UnknownChunk;
171
+ /**
172
+ * Plugin interface for extending AISDKAgent functionality.
173
+ *
174
+ * Plugins can hook into various points of the agent lifecycle to:
175
+ * - Modify inputs before sending to AI
176
+ * - Transform streaming chunks
177
+ * - Intercept or modify tool calls
178
+ * - Process and transform responses
179
+ * - Handle errors
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * const loggingPlugin: AgentPlugin = {
184
+ * id: 'logging',
185
+ *
186
+ * onUserMessage(input, context) {
187
+ * context.logger.info('User message received', {
188
+ * messageCount: input.messages.length,
189
+ * });
190
+ * return input;
191
+ * },
192
+ *
193
+ * onAgentResponse(result, context) {
194
+ * context.logger.info('Agent response', {
195
+ * textLength: result.text.length,
196
+ * });
197
+ * return result;
198
+ * },
199
+ * };
200
+ * ```
201
+ */
202
+ export interface AgentPlugin {
203
+ /** Unique plugin identifier */
204
+ id: string;
205
+ /**
206
+ * Initialize plugin (called once when agent is created).
207
+ * Use this to set up any resources the plugin needs.
208
+ *
209
+ * @param context - Initialization context with provider info
210
+ */
211
+ initialize?(context: {
212
+ provider: string;
213
+ }): void | Promise<void>;
214
+ /**
215
+ * Called before the user message is sent to AI SDK.
216
+ * Can modify messages, system prompt, or tools.
217
+ *
218
+ * @param input - The input data (messages, system prompt, tools)
219
+ * @param context - Plugin context with run info and shared state
220
+ * @returns Modified input or unchanged input
221
+ */
222
+ onUserMessage?(input: AgentRunInput, context: AgentPluginContext): AgentRunInput | Promise<AgentRunInput>;
223
+ /**
224
+ * Called after AI SDK completes processing (including all tool calls).
225
+ * Can process results, emit events, or transform the final text.
226
+ *
227
+ * @param result - The agent's response data
228
+ * @param context - Plugin context with run info and shared state
229
+ * @returns Modified result or unchanged result
230
+ */
231
+ onAgentResponse?(result: AgentRunResult, context: AgentPluginContext): AgentRunResult | Promise<AgentRunResult>;
232
+ /**
233
+ * Called for each streaming text chunk.
234
+ * Can transform chunks. Return undefined/void to keep chunk unchanged.
235
+ *
236
+ * @param chunk - The text chunk being streamed
237
+ * @param context - Plugin context with run info and shared state
238
+ * @returns Transformed chunk, or undefined to keep original
239
+ */
240
+ onTextChunk?(chunk: string, context: AgentPluginContext): string | void | Promise<string | void>;
241
+ /**
242
+ * Called before a tool is executed.
243
+ * Can modify the tool call or return null to skip execution.
244
+ *
245
+ * @param toolCall - Information about the tool being called
246
+ * @param context - Plugin context with run info and shared state
247
+ * @returns Modified tool call, null to skip, or unchanged
248
+ */
249
+ onBeforeToolCall?(toolCall: ToolCallInfo, context: AgentPluginContext): ToolCallInfo | null | Promise<ToolCallInfo | null>;
250
+ /**
251
+ * Called after a tool is executed.
252
+ * Can modify the tool result before it's sent back to the AI.
253
+ *
254
+ * @param toolResult - Information about the completed tool call
255
+ * @param context - Plugin context with run info and shared state
256
+ * @returns Modified result or unchanged result
257
+ */
258
+ onAfterToolCall?(toolResult: ToolResultInfo, context: AgentPluginContext): unknown | Promise<unknown>;
259
+ /**
260
+ * Called for each chunk from the AI SDK stream.
261
+ * Plugins can filter by chunk.type to handle specific chunk types.
262
+ *
263
+ * Common chunk types:
264
+ * - 'text-delta': Text content being streamed
265
+ * - 'source': Citation/source from web search or RAG
266
+ * - 'tool-call': Tool invocation
267
+ * - 'tool-result': Tool execution result
268
+ * - 'start-step', 'finish-step': Multi-step execution markers
269
+ *
270
+ * @param chunk - The stream chunk with type and type-specific properties
271
+ * @param context - Plugin context with run info and shared state
272
+ */
273
+ onChunk?(chunk: StreamChunk, context: AgentPluginContext): void | Promise<void>;
274
+ /**
275
+ * Cleanup (called when agent is destroyed).
276
+ * Use to release any resources the plugin acquired.
277
+ */
278
+ destroy?(): void | Promise<void>;
279
+ }
280
+ //# sourceMappingURL=types.d.ts.map