@snap-agent/core 0.1.0 → 0.1.2

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/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as ProviderConfig, a as ProviderType, A as AgentData, S as StorageAdapter, b as AgentConfig, c as AgentFile, d as Plugin, R as RAGDocument, I as IngestOptions, e as IngestResult, B as BulkOperation, f as BulkResult, U as URLSource, g as URLIngestResult, T as ThreadData, h as ThreadConfig, M as MessageRole, i as MessageAttachment, j as MessageData, C as ClientConfig, k as ChatRequest, l as ChatResponse, m as StreamCallbacks, n as RAGPlugin, o as ToolPlugin, p as MiddlewarePlugin, q as AnalyticsPlugin, r as RAGContext } from './index-CDsqnM8L.mjs';
2
- export { F as AgentNotFoundError, D as AgentSDKError, t as BasePlugin, E as ErrorTrackingData, J as InvalidConfigError, L as MemoryStorage, K as MongoDBStorage, O as MongoDBStorageConfig, v as PerformanceTimings, H as ProviderNotFoundError, s as RAGConfig, w as RAGMetrics, y as RequestTrackingData, z as ResponseTrackingData, G as ThreadNotFoundError, x as TokenMetrics, u as Tool, N as UpstashStorage, Q as UpstashStorageConfig } from './index-CDsqnM8L.mjs';
1
+ import { P as ProviderConfig, a as ProviderType, b as Plugin, S as StoredPluginConfig, A as AgentData, c as StorageAdapter, d as AgentConfig, e as AgentFile, R as RAGDocument, I as IngestOptions, f as IngestResult, B as BulkOperation, g as BulkResult, U as URLSource, h as URLIngestResult, T as ThreadData, i as ThreadConfig, M as MessageRole, j as MessageAttachment, k as MessageData, C as ClientConfig, l as ChatRequest, m as ChatResponse, n as StreamCallbacks, o as RAGPlugin, p as ToolPlugin, q as MiddlewarePlugin, r as AnalyticsPlugin, s as RAGContext } from './index-m2vDW79n.mjs';
2
+ export { L as AgentNotFoundError, K as AgentSDKError, u as BasePlugin, D as DataTransform, H as ErrorTrackingData, w as IngestionSchedule, Q as InvalidConfigError, W as MemoryStorage, V as MongoDBStorage, Y as MongoDBStorageConfig, y as PerformanceTimings, J as PluginRegistryInterface, O as ProviderNotFoundError, t as RAGConfig, z as RAGMetrics, F as RequestTrackingData, G as ResponseTrackingData, N as ThreadNotFoundError, E as TokenMetrics, x as Tool, v as URLSourceAuth, X as UpstashStorage, Z as UpstashStorageConfig } from './index-m2vDW79n.mjs';
3
3
  import { LanguageModel, UserModelMessage, AssistantModelMessage } from 'ai';
4
4
 
5
5
  /**
@@ -50,6 +50,118 @@ declare const Models: {
50
50
  };
51
51
  };
52
52
 
53
+ /**
54
+ * Plugin Registry for serializing/deserializing plugins
55
+ *
56
+ * Plugins are runtime objects that can't be stored in a database.
57
+ * This registry enables:
58
+ * 1. Storing serializable plugin configurations in MongoDB
59
+ * 2. Reinstantiating plugins from stored config when loading agents
60
+ */
61
+
62
+ /**
63
+ * Factory function that creates a plugin instance from config
64
+ */
65
+ type PluginFactory<T extends Plugin = Plugin> = (config: Record<string, any>) => T | Promise<T>;
66
+ /**
67
+ * Resolves environment variable references in config values
68
+ * Format: "${ENV_VAR_NAME}" or "${ENV_VAR_NAME:default_value}"
69
+ *
70
+ * @example
71
+ * resolveEnvVars({ apiKey: "${OPENAI_API_KEY}" })
72
+ * // Returns: { apiKey: "sk-..." } (actual env value)
73
+ *
74
+ * @example
75
+ * resolveEnvVars({ timeout: "${TIMEOUT:5000}" })
76
+ * // Returns: { timeout: "5000" } (default if env not set)
77
+ */
78
+ declare function resolveEnvVars(config: Record<string, any>): Record<string, any>;
79
+ /**
80
+ * Global plugin registry for managing plugin factories
81
+ *
82
+ * @example
83
+ * // Register a plugin factory
84
+ * pluginRegistry.register('@snap-agent/rag-ecommerce', (config) =>
85
+ * new EcommerceRAGPlugin(config)
86
+ * );
87
+ *
88
+ * // Later, instantiate from stored config
89
+ * const plugin = await pluginRegistry.instantiate({
90
+ * type: 'rag',
91
+ * name: '@snap-agent/rag-ecommerce',
92
+ * config: { mongoUri: '${MONGO_URI}', voyageApiKey: '${VOYAGE_API_KEY}' }
93
+ * });
94
+ */
95
+ declare class PluginRegistry {
96
+ private registrations;
97
+ /**
98
+ * Register a plugin factory
99
+ *
100
+ * @param name - Unique plugin identifier (e.g., "@snap-agent/rag-ecommerce")
101
+ * @param factory - Function that creates plugin instance from config
102
+ * @param defaultConfig - Optional default configuration values
103
+ */
104
+ register(name: string, factory: PluginFactory, defaultConfig?: Record<string, any>): void;
105
+ /**
106
+ * Unregister a plugin factory
107
+ */
108
+ unregister(name: string): boolean;
109
+ /**
110
+ * Check if a plugin is registered
111
+ */
112
+ isRegistered(name: string): boolean;
113
+ /**
114
+ * Get all registered plugin names
115
+ */
116
+ getRegisteredPlugins(): string[];
117
+ /**
118
+ * Instantiate a plugin from stored configuration
119
+ *
120
+ * @param storedConfig - Serialized plugin configuration from database
121
+ * @returns Plugin instance
122
+ * @throws Error if plugin is not registered
123
+ */
124
+ instantiate(storedConfig: StoredPluginConfig): Promise<Plugin>;
125
+ /**
126
+ * Instantiate multiple plugins from stored configurations
127
+ *
128
+ * @param storedConfigs - Array of serialized plugin configurations
129
+ * @returns Array of plugin instances (skips disabled plugins)
130
+ */
131
+ instantiateAll(storedConfigs: StoredPluginConfig[]): Promise<Plugin[]>;
132
+ /**
133
+ * Extract serializable configuration from a plugin instance
134
+ * Requires the plugin to implement getConfig() method
135
+ *
136
+ * @param plugin - Plugin instance
137
+ * @returns Stored plugin configuration
138
+ */
139
+ extractConfig(plugin: Plugin & {
140
+ getConfig?: () => Record<string, any>;
141
+ }): StoredPluginConfig;
142
+ /**
143
+ * Extract configurations from multiple plugins
144
+ */
145
+ extractAllConfigs(plugins: Array<Plugin & {
146
+ getConfig?: () => Record<string, any>;
147
+ }>): StoredPluginConfig[];
148
+ }
149
+ /**
150
+ * Default global plugin registry instance
151
+ * Use this for simple setups, or create your own PluginRegistry for isolation
152
+ */
153
+ declare const pluginRegistry: PluginRegistry;
154
+ /**
155
+ * Create an env var reference for use in stored plugin configs
156
+ *
157
+ * @example
158
+ * const config = {
159
+ * apiKey: envRef('OPENAI_API_KEY'),
160
+ * timeout: envRef('TIMEOUT', '5000'),
161
+ * };
162
+ */
163
+ declare function envRef(envVarName: string, defaultValue?: string): string;
164
+
53
165
  type AIMessage$1 = UserModelMessage | AssistantModelMessage;
54
166
  /**
55
167
  * Agent class representing an AI agent with persistent state
@@ -62,12 +174,32 @@ declare class Agent {
62
174
  constructor(data: AgentData, storage: StorageAdapter, providerFactory: ProviderFactory);
63
175
  /**
64
176
  * Create a new agent
177
+ *
178
+ * If plugins are provided, their configurations will be extracted (if they implement getConfig())
179
+ * and stored in the database for later reinstantiation.
65
180
  */
66
181
  static create(config: AgentConfig, storage: StorageAdapter, providerFactory: ProviderFactory): Promise<Agent>;
67
182
  /**
68
183
  * Load an existing agent by ID
69
- */
70
- static load(agentId: string, storage: StorageAdapter, providerFactory: ProviderFactory): Promise<Agent | null>;
184
+ *
185
+ * Plugins can be attached in three ways (in order of priority):
186
+ * 1. Direct plugins array - runtime plugin instances passed directly
187
+ * 2. Plugin registry - reinstantiate from stored configs using registered factories
188
+ * 3. No plugins - agent loads without plugin functionality
189
+ *
190
+ * @param agentId - The agent ID to load
191
+ * @param storage - Storage adapter
192
+ * @param providerFactory - Provider factory
193
+ * @param options - Either:
194
+ * - Plugin[] array (legacy, for backwards compatibility)
195
+ * - Options object with plugins and/or registry
196
+ */
197
+ static load(agentId: string, storage: StorageAdapter, providerFactory: ProviderFactory, options?: Plugin[] | {
198
+ /** Direct plugin instances to attach */
199
+ plugins?: Plugin[];
200
+ /** Registry to reinstantiate plugins from stored configs */
201
+ registry?: PluginRegistry;
202
+ }): Promise<Agent | null>;
71
203
  /**
72
204
  * Update agent properties
73
205
  */
@@ -241,6 +373,7 @@ declare class AgentClient {
241
373
  private storage;
242
374
  private providerFactory;
243
375
  private providers;
376
+ private pluginRegistry?;
244
377
  constructor(config: ClientConfig);
245
378
  private validateConfig;
246
379
  /**
@@ -251,8 +384,21 @@ declare class AgentClient {
251
384
  }): Promise<Agent>;
252
385
  /**
253
386
  * Get an agent by ID
254
- */
255
- getAgent(agentId: string): Promise<Agent>;
387
+ *
388
+ * Plugin loading priority:
389
+ * 1. Direct plugins array passed to this method
390
+ * 2. Plugin registry (if configured) - reinstantiates from stored configs
391
+ * 3. No plugins
392
+ *
393
+ * @param agentId - The agent ID to load
394
+ * @param options - Either Plugin[] for backwards compatibility, or options object
395
+ */
396
+ getAgent(agentId: string, options?: Plugin[] | {
397
+ /** Direct plugin instances to attach (highest priority) */
398
+ plugins?: Plugin[];
399
+ /** Override the client's registry for this call */
400
+ registry?: PluginRegistry;
401
+ }): Promise<Agent>;
256
402
  /**
257
403
  * List agents for a user
258
404
  */
@@ -469,4 +615,4 @@ declare class DefaultRAGPlugin implements RAGPlugin {
469
615
 
470
616
  declare function createClient(config: ClientConfig): AgentClient;
471
617
 
472
- export { Agent, AgentClient, AgentConfig, AgentData, AgentFile, AnalyticsPlugin, BulkOperation, BulkResult, ChatRequest, ChatResponse, ClientConfig, type DefaultRAGConfig, DefaultRAGPlugin, IngestOptions, IngestResult, MessageAttachment, MessageData, MessageRole, MiddlewarePlugin, Models, Plugin, PluginManager, ProviderConfig, ProviderFactory, ProviderType, RAGContext, RAGDocument, RAGPlugin, StorageAdapter, StreamCallbacks, Thread, ThreadConfig, ThreadData, ToolPlugin, createClient };
618
+ export { Agent, AgentClient, AgentConfig, AgentData, AgentFile, AnalyticsPlugin, BulkOperation, BulkResult, ChatRequest, ChatResponse, ClientConfig, type DefaultRAGConfig, DefaultRAGPlugin, IngestOptions, IngestResult, MessageAttachment, MessageData, MessageRole, MiddlewarePlugin, Models, Plugin, type PluginFactory, PluginManager, PluginRegistry, ProviderConfig, ProviderFactory, ProviderType, RAGContext, RAGDocument, RAGPlugin, StorageAdapter, StoredPluginConfig, StreamCallbacks, Thread, ThreadConfig, ThreadData, ToolPlugin, URLIngestResult, URLSource, createClient, envRef, pluginRegistry, resolveEnvVars };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as ProviderConfig, a as ProviderType, A as AgentData, S as StorageAdapter, b as AgentConfig, c as AgentFile, d as Plugin, R as RAGDocument, I as IngestOptions, e as IngestResult, B as BulkOperation, f as BulkResult, U as URLSource, g as URLIngestResult, T as ThreadData, h as ThreadConfig, M as MessageRole, i as MessageAttachment, j as MessageData, C as ClientConfig, k as ChatRequest, l as ChatResponse, m as StreamCallbacks, n as RAGPlugin, o as ToolPlugin, p as MiddlewarePlugin, q as AnalyticsPlugin, r as RAGContext } from './index-CDsqnM8L.js';
2
- export { F as AgentNotFoundError, D as AgentSDKError, t as BasePlugin, E as ErrorTrackingData, J as InvalidConfigError, L as MemoryStorage, K as MongoDBStorage, O as MongoDBStorageConfig, v as PerformanceTimings, H as ProviderNotFoundError, s as RAGConfig, w as RAGMetrics, y as RequestTrackingData, z as ResponseTrackingData, G as ThreadNotFoundError, x as TokenMetrics, u as Tool, N as UpstashStorage, Q as UpstashStorageConfig } from './index-CDsqnM8L.js';
1
+ import { P as ProviderConfig, a as ProviderType, b as Plugin, S as StoredPluginConfig, A as AgentData, c as StorageAdapter, d as AgentConfig, e as AgentFile, R as RAGDocument, I as IngestOptions, f as IngestResult, B as BulkOperation, g as BulkResult, U as URLSource, h as URLIngestResult, T as ThreadData, i as ThreadConfig, M as MessageRole, j as MessageAttachment, k as MessageData, C as ClientConfig, l as ChatRequest, m as ChatResponse, n as StreamCallbacks, o as RAGPlugin, p as ToolPlugin, q as MiddlewarePlugin, r as AnalyticsPlugin, s as RAGContext } from './index-m2vDW79n.js';
2
+ export { L as AgentNotFoundError, K as AgentSDKError, u as BasePlugin, D as DataTransform, H as ErrorTrackingData, w as IngestionSchedule, Q as InvalidConfigError, W as MemoryStorage, V as MongoDBStorage, Y as MongoDBStorageConfig, y as PerformanceTimings, J as PluginRegistryInterface, O as ProviderNotFoundError, t as RAGConfig, z as RAGMetrics, F as RequestTrackingData, G as ResponseTrackingData, N as ThreadNotFoundError, E as TokenMetrics, x as Tool, v as URLSourceAuth, X as UpstashStorage, Z as UpstashStorageConfig } from './index-m2vDW79n.js';
3
3
  import { LanguageModel, UserModelMessage, AssistantModelMessage } from 'ai';
4
4
 
5
5
  /**
@@ -50,6 +50,118 @@ declare const Models: {
50
50
  };
51
51
  };
52
52
 
53
+ /**
54
+ * Plugin Registry for serializing/deserializing plugins
55
+ *
56
+ * Plugins are runtime objects that can't be stored in a database.
57
+ * This registry enables:
58
+ * 1. Storing serializable plugin configurations in MongoDB
59
+ * 2. Reinstantiating plugins from stored config when loading agents
60
+ */
61
+
62
+ /**
63
+ * Factory function that creates a plugin instance from config
64
+ */
65
+ type PluginFactory<T extends Plugin = Plugin> = (config: Record<string, any>) => T | Promise<T>;
66
+ /**
67
+ * Resolves environment variable references in config values
68
+ * Format: "${ENV_VAR_NAME}" or "${ENV_VAR_NAME:default_value}"
69
+ *
70
+ * @example
71
+ * resolveEnvVars({ apiKey: "${OPENAI_API_KEY}" })
72
+ * // Returns: { apiKey: "sk-..." } (actual env value)
73
+ *
74
+ * @example
75
+ * resolveEnvVars({ timeout: "${TIMEOUT:5000}" })
76
+ * // Returns: { timeout: "5000" } (default if env not set)
77
+ */
78
+ declare function resolveEnvVars(config: Record<string, any>): Record<string, any>;
79
+ /**
80
+ * Global plugin registry for managing plugin factories
81
+ *
82
+ * @example
83
+ * // Register a plugin factory
84
+ * pluginRegistry.register('@snap-agent/rag-ecommerce', (config) =>
85
+ * new EcommerceRAGPlugin(config)
86
+ * );
87
+ *
88
+ * // Later, instantiate from stored config
89
+ * const plugin = await pluginRegistry.instantiate({
90
+ * type: 'rag',
91
+ * name: '@snap-agent/rag-ecommerce',
92
+ * config: { mongoUri: '${MONGO_URI}', voyageApiKey: '${VOYAGE_API_KEY}' }
93
+ * });
94
+ */
95
+ declare class PluginRegistry {
96
+ private registrations;
97
+ /**
98
+ * Register a plugin factory
99
+ *
100
+ * @param name - Unique plugin identifier (e.g., "@snap-agent/rag-ecommerce")
101
+ * @param factory - Function that creates plugin instance from config
102
+ * @param defaultConfig - Optional default configuration values
103
+ */
104
+ register(name: string, factory: PluginFactory, defaultConfig?: Record<string, any>): void;
105
+ /**
106
+ * Unregister a plugin factory
107
+ */
108
+ unregister(name: string): boolean;
109
+ /**
110
+ * Check if a plugin is registered
111
+ */
112
+ isRegistered(name: string): boolean;
113
+ /**
114
+ * Get all registered plugin names
115
+ */
116
+ getRegisteredPlugins(): string[];
117
+ /**
118
+ * Instantiate a plugin from stored configuration
119
+ *
120
+ * @param storedConfig - Serialized plugin configuration from database
121
+ * @returns Plugin instance
122
+ * @throws Error if plugin is not registered
123
+ */
124
+ instantiate(storedConfig: StoredPluginConfig): Promise<Plugin>;
125
+ /**
126
+ * Instantiate multiple plugins from stored configurations
127
+ *
128
+ * @param storedConfigs - Array of serialized plugin configurations
129
+ * @returns Array of plugin instances (skips disabled plugins)
130
+ */
131
+ instantiateAll(storedConfigs: StoredPluginConfig[]): Promise<Plugin[]>;
132
+ /**
133
+ * Extract serializable configuration from a plugin instance
134
+ * Requires the plugin to implement getConfig() method
135
+ *
136
+ * @param plugin - Plugin instance
137
+ * @returns Stored plugin configuration
138
+ */
139
+ extractConfig(plugin: Plugin & {
140
+ getConfig?: () => Record<string, any>;
141
+ }): StoredPluginConfig;
142
+ /**
143
+ * Extract configurations from multiple plugins
144
+ */
145
+ extractAllConfigs(plugins: Array<Plugin & {
146
+ getConfig?: () => Record<string, any>;
147
+ }>): StoredPluginConfig[];
148
+ }
149
+ /**
150
+ * Default global plugin registry instance
151
+ * Use this for simple setups, or create your own PluginRegistry for isolation
152
+ */
153
+ declare const pluginRegistry: PluginRegistry;
154
+ /**
155
+ * Create an env var reference for use in stored plugin configs
156
+ *
157
+ * @example
158
+ * const config = {
159
+ * apiKey: envRef('OPENAI_API_KEY'),
160
+ * timeout: envRef('TIMEOUT', '5000'),
161
+ * };
162
+ */
163
+ declare function envRef(envVarName: string, defaultValue?: string): string;
164
+
53
165
  type AIMessage$1 = UserModelMessage | AssistantModelMessage;
54
166
  /**
55
167
  * Agent class representing an AI agent with persistent state
@@ -62,12 +174,32 @@ declare class Agent {
62
174
  constructor(data: AgentData, storage: StorageAdapter, providerFactory: ProviderFactory);
63
175
  /**
64
176
  * Create a new agent
177
+ *
178
+ * If plugins are provided, their configurations will be extracted (if they implement getConfig())
179
+ * and stored in the database for later reinstantiation.
65
180
  */
66
181
  static create(config: AgentConfig, storage: StorageAdapter, providerFactory: ProviderFactory): Promise<Agent>;
67
182
  /**
68
183
  * Load an existing agent by ID
69
- */
70
- static load(agentId: string, storage: StorageAdapter, providerFactory: ProviderFactory): Promise<Agent | null>;
184
+ *
185
+ * Plugins can be attached in three ways (in order of priority):
186
+ * 1. Direct plugins array - runtime plugin instances passed directly
187
+ * 2. Plugin registry - reinstantiate from stored configs using registered factories
188
+ * 3. No plugins - agent loads without plugin functionality
189
+ *
190
+ * @param agentId - The agent ID to load
191
+ * @param storage - Storage adapter
192
+ * @param providerFactory - Provider factory
193
+ * @param options - Either:
194
+ * - Plugin[] array (legacy, for backwards compatibility)
195
+ * - Options object with plugins and/or registry
196
+ */
197
+ static load(agentId: string, storage: StorageAdapter, providerFactory: ProviderFactory, options?: Plugin[] | {
198
+ /** Direct plugin instances to attach */
199
+ plugins?: Plugin[];
200
+ /** Registry to reinstantiate plugins from stored configs */
201
+ registry?: PluginRegistry;
202
+ }): Promise<Agent | null>;
71
203
  /**
72
204
  * Update agent properties
73
205
  */
@@ -241,6 +373,7 @@ declare class AgentClient {
241
373
  private storage;
242
374
  private providerFactory;
243
375
  private providers;
376
+ private pluginRegistry?;
244
377
  constructor(config: ClientConfig);
245
378
  private validateConfig;
246
379
  /**
@@ -251,8 +384,21 @@ declare class AgentClient {
251
384
  }): Promise<Agent>;
252
385
  /**
253
386
  * Get an agent by ID
254
- */
255
- getAgent(agentId: string): Promise<Agent>;
387
+ *
388
+ * Plugin loading priority:
389
+ * 1. Direct plugins array passed to this method
390
+ * 2. Plugin registry (if configured) - reinstantiates from stored configs
391
+ * 3. No plugins
392
+ *
393
+ * @param agentId - The agent ID to load
394
+ * @param options - Either Plugin[] for backwards compatibility, or options object
395
+ */
396
+ getAgent(agentId: string, options?: Plugin[] | {
397
+ /** Direct plugin instances to attach (highest priority) */
398
+ plugins?: Plugin[];
399
+ /** Override the client's registry for this call */
400
+ registry?: PluginRegistry;
401
+ }): Promise<Agent>;
256
402
  /**
257
403
  * List agents for a user
258
404
  */
@@ -469,4 +615,4 @@ declare class DefaultRAGPlugin implements RAGPlugin {
469
615
 
470
616
  declare function createClient(config: ClientConfig): AgentClient;
471
617
 
472
- export { Agent, AgentClient, AgentConfig, AgentData, AgentFile, AnalyticsPlugin, BulkOperation, BulkResult, ChatRequest, ChatResponse, ClientConfig, type DefaultRAGConfig, DefaultRAGPlugin, IngestOptions, IngestResult, MessageAttachment, MessageData, MessageRole, MiddlewarePlugin, Models, Plugin, PluginManager, ProviderConfig, ProviderFactory, ProviderType, RAGContext, RAGDocument, RAGPlugin, StorageAdapter, StreamCallbacks, Thread, ThreadConfig, ThreadData, ToolPlugin, createClient };
618
+ export { Agent, AgentClient, AgentConfig, AgentData, AgentFile, AnalyticsPlugin, BulkOperation, BulkResult, ChatRequest, ChatResponse, ClientConfig, type DefaultRAGConfig, DefaultRAGPlugin, IngestOptions, IngestResult, MessageAttachment, MessageData, MessageRole, MiddlewarePlugin, Models, Plugin, type PluginFactory, PluginManager, PluginRegistry, ProviderConfig, ProviderFactory, ProviderType, RAGContext, RAGDocument, RAGPlugin, StorageAdapter, StoredPluginConfig, StreamCallbacks, Thread, ThreadConfig, ThreadData, ToolPlugin, URLIngestResult, URLSource, createClient, envRef, pluginRegistry, resolveEnvVars };