@happyvertical/smrt-agents 0.30.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/AGENTS.md +96 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +7 -0
- package/README.md +139 -0
- package/dist/__smrt-register__.d.ts +2 -0
- package/dist/__smrt-register__.d.ts.map +1 -0
- package/dist/agent.d.ts +545 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/ai-config.d.ts +27 -0
- package/dist/ai-config.d.ts.map +1 -0
- package/dist/chunks/config-BYbOxt24.js +179 -0
- package/dist/chunks/config-BYbOxt24.js.map +1 -0
- package/dist/chunks/manifest-utils-DLXfTOq0.js +69 -0
- package/dist/chunks/manifest-utils-DLXfTOq0.js.map +1 -0
- package/dist/config.d.ts +117 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/identity.d.ts +19 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1477 -0
- package/dist/index.js.map +1 -0
- package/dist/interests.d.ts +291 -0
- package/dist/interests.d.ts.map +1 -0
- package/dist/manifest.json +2012 -0
- package/dist/playground.d.ts +2 -0
- package/dist/playground.d.ts.map +1 -0
- package/dist/playground.js +156 -0
- package/dist/playground.js.map +1 -0
- package/dist/schedule.d.ts +168 -0
- package/dist/schedule.d.ts.map +1 -0
- package/dist/server/action-types.d.ts +65 -0
- package/dist/server/action-types.d.ts.map +1 -0
- package/dist/server/action-types.js +2 -0
- package/dist/server/action-types.js.map +1 -0
- package/dist/server/api-routes.d.ts +57 -0
- package/dist/server/api-routes.d.ts.map +1 -0
- package/dist/server/config-loader.d.ts +17 -0
- package/dist/server/config-loader.d.ts.map +1 -0
- package/dist/server/index.d.ts +34 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/manifest-utils.d.ts +63 -0
- package/dist/server/manifest-utils.d.ts.map +1 -0
- package/dist/server/serialization.d.ts +58 -0
- package/dist/server/serialization.d.ts.map +1 -0
- package/dist/server.js +105 -0
- package/dist/server.js.map +1 -0
- package/dist/smrt-knowledge.json +983 -0
- package/dist/summary-article.d.ts +30 -0
- package/dist/summary-article.d.ts.map +1 -0
- package/dist/summary-article.js +2 -0
- package/dist/summary-article.js.map +1 -0
- package/dist/svelte/components/AgentDashboard.svelte +250 -0
- package/dist/svelte/components/AgentDashboard.svelte.d.ts +21 -0
- package/dist/svelte/components/AgentDashboard.svelte.d.ts.map +1 -0
- package/dist/svelte/components/AgentRunHistory.svelte +225 -0
- package/dist/svelte/components/AgentRunHistory.svelte.d.ts +16 -0
- package/dist/svelte/components/AgentRunHistory.svelte.d.ts.map +1 -0
- package/dist/svelte/components/AgentScheduleForm.svelte +381 -0
- package/dist/svelte/components/AgentScheduleForm.svelte.d.ts +19 -0
- package/dist/svelte/components/AgentScheduleForm.svelte.d.ts.map +1 -0
- package/dist/svelte/components/AgentScheduleList.svelte +370 -0
- package/dist/svelte/components/AgentScheduleList.svelte.d.ts +24 -0
- package/dist/svelte/components/AgentScheduleList.svelte.d.ts.map +1 -0
- package/dist/svelte/components/ScheduleStatusBadge.svelte +23 -0
- package/dist/svelte/components/ScheduleStatusBadge.svelte.d.ts +9 -0
- package/dist/svelte/components/ScheduleStatusBadge.svelte.d.ts.map +1 -0
- package/dist/svelte/i18n.d.ts +33 -0
- package/dist/svelte/i18n.d.ts.map +1 -0
- package/dist/svelte/i18n.js +37 -0
- package/dist/svelte/index.d.ts +23 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +26 -0
- package/dist/svelte/playground.d.ts +196 -0
- package/dist/svelte/playground.d.ts.map +1 -0
- package/dist/svelte/playground.js +151 -0
- package/dist/svelte/types.d.ts +155 -0
- package/dist/svelte/types.d.ts.map +1 -0
- package/dist/svelte/types.js +116 -0
- package/dist/tenant-agent.d.ts +106 -0
- package/dist/tenant-agent.d.ts.map +1 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui.d.ts +298 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +133 -0
- package/dist/ui.js.map +1 -0
- package/dist/vite-plugin.d.ts +61 -0
- package/dist/vite-plugin.d.ts.map +1 -0
- package/dist/vite-plugin.js +173 -0
- package/dist/vite-plugin.js.map +1 -0
- package/package.json +104 -0
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import { Logger } from '@happyvertical/logger';
|
|
2
|
+
import { ConfigResolver, DispatchBus, DispatchMetadata, SmrtObject, SmrtObjectOptions } from '@happyvertical/smrt-core';
|
|
3
|
+
import { AgentAIOptions } from './ai-config.js';
|
|
4
|
+
import { AgentWithInterestsOptions, InterestOptions, InterestResult } from './interests.js';
|
|
5
|
+
import { AgentStatusType } from './types.js';
|
|
6
|
+
import { AgentAdminRoute, AgentUISlots } from './ui.js';
|
|
7
|
+
/**
|
|
8
|
+
* Agent constructor options
|
|
9
|
+
*/
|
|
10
|
+
export interface AgentOptions extends SmrtObjectOptions, AgentWithInterestsOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Optional AI configuration for this agent.
|
|
13
|
+
*
|
|
14
|
+
* When `apiKey` is omitted, the runtime can resolve provider credentials from
|
|
15
|
+
* tenant secrets based on the active tenant context.
|
|
16
|
+
*/
|
|
17
|
+
ai?: AgentAIOptions;
|
|
18
|
+
/**
|
|
19
|
+
* Suppress all log output (useful for CLI --json mode)
|
|
20
|
+
* When true, creates a no-op logger that discards all messages
|
|
21
|
+
*/
|
|
22
|
+
silent?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Opt into process-level SIGTERM/SIGINT handling for this instance.
|
|
25
|
+
*
|
|
26
|
+
* Host runtimes should generally own process lifecycle; this remains available
|
|
27
|
+
* for single-agent CLIs and scripts that explicitly want it. Do not enable
|
|
28
|
+
* this for multiple agents in the same process unless the host coordinates
|
|
29
|
+
* shutdown itself; the first handler to finish exits the process.
|
|
30
|
+
*/
|
|
31
|
+
manageProcessSignals?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Base Agent class for building autonomous actors in the SMRT ecosystem
|
|
35
|
+
*
|
|
36
|
+
* Agents are SmrtObjects that perform specific tasks with:
|
|
37
|
+
* - Status tracking (idle, initializing, running, error, shutdown)
|
|
38
|
+
* - Configuration management via @have/config
|
|
39
|
+
* - Structured logging via @happyvertical/logger
|
|
40
|
+
* - Lifecycle hooks (initialize, validate, run, shutdown)
|
|
41
|
+
* - Optional process signal handling for graceful shutdown
|
|
42
|
+
*
|
|
43
|
+
* Agents can define their own properties for state management - since they extend
|
|
44
|
+
* SmrtObject, any properties defined will be automatically persisted to the database.
|
|
45
|
+
*
|
|
46
|
+
* **Important**: Extending classes must add the `@smrt()` decorator themselves
|
|
47
|
+
* to configure CLI/API/MCP exposure.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import { Agent } from '@have/agents';
|
|
52
|
+
* import { getModuleConfig } from '@have/config';
|
|
53
|
+
* import { smrt } from '@happyvertical/smrt-core';
|
|
54
|
+
*
|
|
55
|
+
* @smrt()
|
|
56
|
+
* class MyAgent extends Agent {
|
|
57
|
+
* protected config = getModuleConfig('my-agent', {
|
|
58
|
+
* cronSchedule: '0 2 * * *',
|
|
59
|
+
* maxRetries: 3
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* // Define your own state properties (automatically persisted)
|
|
63
|
+
* lastCrawl: Date | null = null;
|
|
64
|
+
* itemsProcessed: number = 0;
|
|
65
|
+
*
|
|
66
|
+
* async validate(): Promise<void> {
|
|
67
|
+
* if (!this.config.cronSchedule) {
|
|
68
|
+
* throw new Error('cronSchedule is required');
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* async run(): Promise<void> {
|
|
73
|
+
* // Agent logic here
|
|
74
|
+
* this.itemsProcessed = 42;
|
|
75
|
+
* this.lastCrawl = new Date();
|
|
76
|
+
* await this.save(); // Persist state
|
|
77
|
+
* }
|
|
78
|
+
* }
|
|
79
|
+
*
|
|
80
|
+
* const agent = new MyAgent({ name: 'my-agent' });
|
|
81
|
+
* await agent.execute();
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare abstract class Agent extends SmrtObject {
|
|
85
|
+
/**
|
|
86
|
+
* Tenant ID for multi-tenant isolation
|
|
87
|
+
* Nullable to support both tenant-scoped and global agents
|
|
88
|
+
*/
|
|
89
|
+
tenantId: string | null;
|
|
90
|
+
/**
|
|
91
|
+
* UI slots this agent supports for admin panels
|
|
92
|
+
*
|
|
93
|
+
* Subclasses override this to declare their admin UI slots.
|
|
94
|
+
* Each slot can be implemented by a Svelte component.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* static override uiSlots: AgentUISlots = {
|
|
99
|
+
* sources: {
|
|
100
|
+
* id: 'sources',
|
|
101
|
+
* label: 'News Sources',
|
|
102
|
+
* description: 'Configure scrapers and data sources',
|
|
103
|
+
* icon: 'database',
|
|
104
|
+
* order: 1,
|
|
105
|
+
* },
|
|
106
|
+
* settings: {
|
|
107
|
+
* id: 'settings',
|
|
108
|
+
* label: 'Agent Settings',
|
|
109
|
+
* description: 'Configure agent behavior',
|
|
110
|
+
* icon: 'settings',
|
|
111
|
+
* order: 2,
|
|
112
|
+
* },
|
|
113
|
+
* };
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
static uiSlots: AgentUISlots;
|
|
117
|
+
/**
|
|
118
|
+
* Admin routes this agent provides
|
|
119
|
+
*
|
|
120
|
+
* Subclasses override this to declare admin route metadata.
|
|
121
|
+
* The vitePluginAgentRoutes Vite plugin reads these from the manifest
|
|
122
|
+
* and registers them so host applications can discover and render them.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* static override adminRoutes: AgentAdminRoute[] = [
|
|
127
|
+
* { path: 'sources', component: 'SourcesPanel', load: 'loadSources' },
|
|
128
|
+
* { path: 'sources/[sourceId]', component: 'SourceDetail', load: 'loadSourceDetail' },
|
|
129
|
+
* ];
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
static adminRoutes: AgentAdminRoute[];
|
|
133
|
+
/**
|
|
134
|
+
* Signal types this agent subscribes to by default
|
|
135
|
+
*
|
|
136
|
+
* These are seedable defaults — on `initialize()`, the agent checks the
|
|
137
|
+
* database first and only creates subscriptions that don't already exist.
|
|
138
|
+
* The database is the runtime source of truth, allowing users to customize
|
|
139
|
+
* subscriptions per-tenant via the dashboard without code changes.
|
|
140
|
+
*
|
|
141
|
+
* When declared, `execute()` will automatically call `processDispatches()`
|
|
142
|
+
* before `run()`, so handler agents don't need to manually poll.
|
|
143
|
+
* Override `handleDispatch()` to process incoming dispatches.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* @smrt({ agent: { icon: 'mail', tier: 'standard' } })
|
|
148
|
+
* class EmailHandler extends Agent {
|
|
149
|
+
* static override signalSubscriptions = ['email.received', 'email.bounced'];
|
|
150
|
+
*
|
|
151
|
+
* async handleDispatch(payload: unknown, metadata: DispatchMetadata) {
|
|
152
|
+
* // Called automatically during execute() for each pending dispatch
|
|
153
|
+
* }
|
|
154
|
+
*
|
|
155
|
+
* async run() { ... }
|
|
156
|
+
* }
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
static signalSubscriptions: string[];
|
|
160
|
+
/**
|
|
161
|
+
* Execute-time resolvers for `agent_config` fields that should be computed
|
|
162
|
+
* lazily rather than snapshotted at sync time.
|
|
163
|
+
*
|
|
164
|
+
* Each entry is keyed by the agent_config field it produces. The runtime
|
|
165
|
+
* (see {@link resolveLazyConfig}) calls every resolver and overlays the
|
|
166
|
+
* results on top of the persisted config before constructing the agent.
|
|
167
|
+
* That means env-derived values like asset storage paths, S3 buckets, AI
|
|
168
|
+
* provider keys, or tenant-scoped DB URLs stay live: rotating an env var
|
|
169
|
+
* is reflected on the next scheduled run without rewriting the schedule
|
|
170
|
+
* row.
|
|
171
|
+
*
|
|
172
|
+
* Resolvers may be sync or async. Returning `undefined` or `null` leaves
|
|
173
|
+
* the persisted value in place — both are treated as "no overlay" so the
|
|
174
|
+
* common `() => process.env.X ?? null` pattern is safe and won't clobber
|
|
175
|
+
* a snapshotted value when the env var is unset. Throwing falls back to
|
|
176
|
+
* the persisted value (or to whatever
|
|
177
|
+
* {@link ResolveLazyConfigOptions.onError} dictates).
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* class Praeco extends Agent {
|
|
182
|
+
* static override configResolvers = {
|
|
183
|
+
* assetStorage: () => resolveSharedAssetStorage(),
|
|
184
|
+
* aiKey: async () => loadAIKeyFromSecretsManager(),
|
|
185
|
+
* };
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
static configResolvers: Record<string, ConfigResolver>;
|
|
190
|
+
/**
|
|
191
|
+
* Current agent status
|
|
192
|
+
*/
|
|
193
|
+
status: AgentStatusType;
|
|
194
|
+
/**
|
|
195
|
+
* Structured logger instance
|
|
196
|
+
* Created with agent's class name as context
|
|
197
|
+
*/
|
|
198
|
+
protected logger: Logger;
|
|
199
|
+
/**
|
|
200
|
+
* Agent configuration
|
|
201
|
+
* Must be defined by extending classes using getModuleConfig()
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* protected config = getModuleConfig('my-agent', {
|
|
206
|
+
* cronSchedule: '0 0 * * *',
|
|
207
|
+
* maxRetries: 3
|
|
208
|
+
* });
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
protected abstract config: unknown;
|
|
212
|
+
/**
|
|
213
|
+
* Signal handlers for graceful shutdown
|
|
214
|
+
*/
|
|
215
|
+
private signalHandlers;
|
|
216
|
+
/**
|
|
217
|
+
* Cached DispatchBus instance for inter-agent communication
|
|
218
|
+
*/
|
|
219
|
+
private _dispatch;
|
|
220
|
+
/**
|
|
221
|
+
* Creates a new Agent instance
|
|
222
|
+
*
|
|
223
|
+
* @param options - Configuration options including identifiers and metadata
|
|
224
|
+
*/
|
|
225
|
+
constructor(options?: AgentOptions);
|
|
226
|
+
/**
|
|
227
|
+
* Interest configuration for this agent
|
|
228
|
+
* Lazily accessed from options on first interesting() call
|
|
229
|
+
*/
|
|
230
|
+
protected get interests(): InterestOptions | undefined;
|
|
231
|
+
/**
|
|
232
|
+
* Canonical agent type for persistence and dispatch routing.
|
|
233
|
+
*/
|
|
234
|
+
protected getAgentTypeName(): string;
|
|
235
|
+
/**
|
|
236
|
+
* Human-readable class name for logs and UI.
|
|
237
|
+
*/
|
|
238
|
+
protected getAgentClassName(): string;
|
|
239
|
+
/**
|
|
240
|
+
* Get UI slot definitions for this agent instance
|
|
241
|
+
*
|
|
242
|
+
* Returns the static uiSlots defined on the agent's class.
|
|
243
|
+
* Used by host applications to discover available admin panels.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```typescript
|
|
247
|
+
* const slots = agent.getUISlots();
|
|
248
|
+
* for (const [slotId, slot] of Object.entries(slots)) {
|
|
249
|
+
* console.log(`${slot.label}: ${slot.description}`);
|
|
250
|
+
* }
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
getUISlots(): AgentUISlots;
|
|
254
|
+
/**
|
|
255
|
+
* Load all database-persisted configs for this agent
|
|
256
|
+
*
|
|
257
|
+
* Returns a Map of slotId → configData for all saved configurations.
|
|
258
|
+
* Use getMergedConfig() to get file + db merged config for a slot.
|
|
259
|
+
*
|
|
260
|
+
* @returns Map of slotId to config data
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* const configs = await agent.loadConfigs();
|
|
265
|
+
* const sources = configs.get('sources');
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
loadConfigs(): Promise<Map<string, any>>;
|
|
269
|
+
/**
|
|
270
|
+
* Save config for a specific UI slot to the database
|
|
271
|
+
*
|
|
272
|
+
* Persists configuration data that can be modified by admin panels.
|
|
273
|
+
* Use this when the user saves changes in an admin UI.
|
|
274
|
+
*
|
|
275
|
+
* @param slotId - The UI slot ID (e.g., 'sources', 'settings')
|
|
276
|
+
* @param data - Configuration data to save
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* await agent.saveSlotConfig('sources', {
|
|
281
|
+
* scrapers: ['civicweb', 'govstack'],
|
|
282
|
+
* refreshInterval: 3600
|
|
283
|
+
* });
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
saveSlotConfig(slotId: string, data: Record<string, any>): Promise<void>;
|
|
287
|
+
/**
|
|
288
|
+
* Get merged config for a slot (file-based + database)
|
|
289
|
+
*
|
|
290
|
+
* Priority order (highest to lowest):
|
|
291
|
+
* 1. Database-persisted config (from saveSlotConfig)
|
|
292
|
+
* 2. File-based config (from getModuleConfig)
|
|
293
|
+
* 3. Agent class defaults
|
|
294
|
+
*
|
|
295
|
+
* @param slotId - The UI slot ID
|
|
296
|
+
* @returns Merged configuration object
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* const sourcesConfig = await agent.getMergedConfig('sources');
|
|
301
|
+
* // Returns file config merged with any db overrides
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
getMergedConfig(slotId: string): Promise<any>;
|
|
305
|
+
/**
|
|
306
|
+
* Export all config for this agent (for static site generation)
|
|
307
|
+
*
|
|
308
|
+
* Merges file-based and database configs, then optionally sanitizes
|
|
309
|
+
* to remove secrets. Use this before building a static site.
|
|
310
|
+
*
|
|
311
|
+
* @param options - Export options
|
|
312
|
+
* @param options.includeSecrets - If true, includes API keys and secrets (default: false)
|
|
313
|
+
* @returns Merged configuration object
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* // Export for static build (secrets filtered)
|
|
318
|
+
* const config = await agent.exportConfig();
|
|
319
|
+
*
|
|
320
|
+
* // Export with secrets (for secure environments)
|
|
321
|
+
* const fullConfig = await agent.exportConfig({ includeSecrets: true });
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
exportConfig(options?: {
|
|
325
|
+
includeSecrets?: boolean;
|
|
326
|
+
}): Promise<any>;
|
|
327
|
+
/**
|
|
328
|
+
* Get the DispatchBus for inter-agent communication
|
|
329
|
+
*
|
|
330
|
+
* Creates a DispatchBus lazily on first access. Requires database configuration.
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* ```typescript
|
|
334
|
+
* // Emit a dispatch to other agents
|
|
335
|
+
* await this.dispatch.emit('campaign.completed', {
|
|
336
|
+
* campaignId: '123',
|
|
337
|
+
* revenue: 5000
|
|
338
|
+
* }, { source: this.constructor.name });
|
|
339
|
+
*
|
|
340
|
+
* // Subscribe to dispatches
|
|
341
|
+
* await this.dispatch.subscribe({
|
|
342
|
+
* signalType: 'campaign.*',
|
|
343
|
+
* subscriber: this.constructor.name
|
|
344
|
+
* });
|
|
345
|
+
* ```
|
|
346
|
+
*
|
|
347
|
+
* @throws Error if database is not configured
|
|
348
|
+
*/
|
|
349
|
+
getDispatch(): Promise<DispatchBus>;
|
|
350
|
+
/**
|
|
351
|
+
* Handle incoming dispatches
|
|
352
|
+
*
|
|
353
|
+
* Override this method to process dispatches targeted at this agent.
|
|
354
|
+
* Called when process() is invoked for this agent's subscriber name.
|
|
355
|
+
*
|
|
356
|
+
* @param payload - Dispatch payload data
|
|
357
|
+
* @param metadata - Dispatch metadata including type, source, and timing
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```typescript
|
|
361
|
+
* async handleDispatch(payload: unknown, metadata: DispatchMetadata): Promise<void> {
|
|
362
|
+
* if (metadata.type === 'campaign.completed') {
|
|
363
|
+
* const data = payload as { campaignId: string; revenue: number };
|
|
364
|
+
* await this.recordRevenue(data.campaignId, data.revenue);
|
|
365
|
+
* }
|
|
366
|
+
* }
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
handleDispatch(_payload: unknown, _metadata: DispatchMetadata): Promise<void>;
|
|
370
|
+
/**
|
|
371
|
+
* Process pending dispatches for this agent
|
|
372
|
+
*
|
|
373
|
+
* Finds and processes all pending dispatches that match this agent's subscriptions.
|
|
374
|
+
* Uses handleDispatch() to process each dispatch.
|
|
375
|
+
*
|
|
376
|
+
* @returns Number of dispatches processed
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```typescript
|
|
380
|
+
* // In your run() method
|
|
381
|
+
* const processed = await this.processDispatches();
|
|
382
|
+
* this.logger.info(`Processed ${processed} dispatches`);
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
processDispatches(): Promise<number>;
|
|
386
|
+
/**
|
|
387
|
+
* Initialize the agent
|
|
388
|
+
* Sets status to 'initializing' and sets up signal handlers
|
|
389
|
+
*
|
|
390
|
+
* Override to perform setup after construction, but always call super.initialize()
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* async initialize(): Promise<void> {
|
|
395
|
+
* await super.initialize();
|
|
396
|
+
* // Custom initialization logic
|
|
397
|
+
* }
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
initialize(): Promise<this>;
|
|
401
|
+
/**
|
|
402
|
+
* Set up signal handlers for graceful shutdown
|
|
403
|
+
* Handles SIGTERM and SIGINT for single-agent processes that explicitly opt in.
|
|
404
|
+
*/
|
|
405
|
+
private setupSignalHandlers;
|
|
406
|
+
/**
|
|
407
|
+
* Migrate legacy simple-name dispatch subscribers to the canonical agent type.
|
|
408
|
+
*
|
|
409
|
+
* Older releases used `this.constructor.name` directly for subscriber IDs.
|
|
410
|
+
* That collides across packages and leaves fan-out dispatches targeted at the
|
|
411
|
+
* wrong subscriber once qualified names are available.
|
|
412
|
+
*/
|
|
413
|
+
private migrateLegacyDispatchSubscriptions;
|
|
414
|
+
/**
|
|
415
|
+
* Clean up signal handlers
|
|
416
|
+
*/
|
|
417
|
+
private cleanupSignalHandlers;
|
|
418
|
+
/**
|
|
419
|
+
* Validate configuration and dependencies
|
|
420
|
+
* Override to check agent-specific requirements
|
|
421
|
+
*
|
|
422
|
+
* @throws Error if validation fails
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```typescript
|
|
426
|
+
* async validate(): Promise<void> {
|
|
427
|
+
* if (!this.config.apiKey) {
|
|
428
|
+
* throw new Error('API key is required');
|
|
429
|
+
* }
|
|
430
|
+
* }
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
validate(): Promise<void>;
|
|
434
|
+
/**
|
|
435
|
+
* Main agent logic
|
|
436
|
+
* Must be implemented by extending class
|
|
437
|
+
*
|
|
438
|
+
* Update this.lastRun.itemsProcessed to track work done
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```typescript
|
|
442
|
+
* async run(): Promise<void> {
|
|
443
|
+
* this.logger.info('Starting agent work');
|
|
444
|
+
* let processed = 0;
|
|
445
|
+
*
|
|
446
|
+
* for (const item of items) {
|
|
447
|
+
* await this.processItem(item);
|
|
448
|
+
* processed++;
|
|
449
|
+
* }
|
|
450
|
+
*
|
|
451
|
+
* this.lastRun.itemsProcessed = processed;
|
|
452
|
+
* this.logger.info(`Processed ${processed} items`);
|
|
453
|
+
* }
|
|
454
|
+
* ```
|
|
455
|
+
*/
|
|
456
|
+
abstract run(): Promise<void>;
|
|
457
|
+
/**
|
|
458
|
+
* Cleanup and shutdown
|
|
459
|
+
* Override to perform graceful shutdown
|
|
460
|
+
*
|
|
461
|
+
* Always call super.shutdown() to clean up signal handlers
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* ```typescript
|
|
465
|
+
* async shutdown(): Promise<void> {
|
|
466
|
+
* this.logger.info('Cleaning up resources');
|
|
467
|
+
* await this.cleanup();
|
|
468
|
+
* await super.shutdown();
|
|
469
|
+
* }
|
|
470
|
+
* ```
|
|
471
|
+
*/
|
|
472
|
+
shutdown(): Promise<void>;
|
|
473
|
+
/**
|
|
474
|
+
* Execute agent with lifecycle management
|
|
475
|
+
*
|
|
476
|
+
* Runs the full lifecycle:
|
|
477
|
+
* 1. initialize() — seeds signal subscriptions if declared
|
|
478
|
+
* 2. validate()
|
|
479
|
+
* 3. processDispatches() — auto-processes pending dispatches if subscriptions exist
|
|
480
|
+
* 4. run()
|
|
481
|
+
*
|
|
482
|
+
* Note: handleDispatch() callbacks may fire before run() is entered.
|
|
483
|
+
*
|
|
484
|
+
* On error:
|
|
485
|
+
* 1. Sets status to 'error'
|
|
486
|
+
* 2. Logs error
|
|
487
|
+
* 3. Re-throws error
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```typescript
|
|
491
|
+
* const agent = new MyAgent({ name: 'my-agent' });
|
|
492
|
+
*
|
|
493
|
+
* try {
|
|
494
|
+
* await agent.execute();
|
|
495
|
+
* console.log('Agent completed successfully');
|
|
496
|
+
* } catch (error) {
|
|
497
|
+
* console.error('Agent failed:', error);
|
|
498
|
+
* }
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
execute(): Promise<void>;
|
|
502
|
+
/**
|
|
503
|
+
* Query objects this agent is interested in
|
|
504
|
+
*
|
|
505
|
+
* Returns items from all configured object types, filtered and sorted
|
|
506
|
+
* according to interest configuration. If handlers are defined on filters,
|
|
507
|
+
* they are called for each matched item and the result is included.
|
|
508
|
+
*
|
|
509
|
+
* @returns Array of { type, data, name?, handled? } results
|
|
510
|
+
* @throws Error if no interests are configured
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* ```typescript
|
|
514
|
+
* const items = await this.interesting();
|
|
515
|
+
* for (const { type, data, name, handled } of items) {
|
|
516
|
+
* console.log(`Processing ${type} from "${name}": action=${handled?.action}`);
|
|
517
|
+
* }
|
|
518
|
+
* ```
|
|
519
|
+
*/
|
|
520
|
+
interesting(): Promise<InterestResult[]>;
|
|
521
|
+
/**
|
|
522
|
+
* Query a single object type based on interest config
|
|
523
|
+
*
|
|
524
|
+
* Supports both single filter and array of filters.
|
|
525
|
+
* Each filter can use standard SDK filters OR custom query function.
|
|
526
|
+
* Returns InterestResult[] with handler results included.
|
|
527
|
+
*/
|
|
528
|
+
private queryInterestingObjects;
|
|
529
|
+
/**
|
|
530
|
+
* Normalize ObjectInterestConfig to array format
|
|
531
|
+
*/
|
|
532
|
+
private normalizeInterestConfig;
|
|
533
|
+
/**
|
|
534
|
+
* Query a single interest filter
|
|
535
|
+
*
|
|
536
|
+
* Uses collection.query() for custom query functions,
|
|
537
|
+
* or collection.list() for standard SDK filters.
|
|
538
|
+
*/
|
|
539
|
+
private queryInterestFilter;
|
|
540
|
+
/**
|
|
541
|
+
* Sort results by field(s) across all types
|
|
542
|
+
*/
|
|
543
|
+
private sortResults;
|
|
544
|
+
}
|
|
545
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAElE,OAAO,EACL,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EAKrB,UAAU,EACV,KAAK,iBAAiB,EAEvB,MAAM,0BAA0B,CAAC;AAMlC,OAAO,EAAE,KAAK,cAAc,EAAyB,MAAM,gBAAgB,CAAC;AAM5E,OAAO,KAAK,EACV,yBAAyB,EAEzB,eAAe,EACf,cAAc,EAEf,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,YAA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G;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAM/B;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA8B9B;;;;;;;;;;;;;;;;;OAiBG;IACG,WAAW,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAuD9C;;;;;;OAMG;YACW,uBAAuB;IAoDrC;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAM/B;;;;;OAKG;YACW,mBAAmB;IAwHjC;;OAEG;IACH,OAAO,CAAC,WAAW;CAwBpB"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AIClientOptions } from '@happyvertical/ai';
|
|
2
|
+
import { DatabaseInterface } from '@happyvertical/sql';
|
|
3
|
+
export type AgentAISecretFallback = 'none' | 'ancestors';
|
|
4
|
+
export interface AgentAIOptions extends AIClientOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Secret name to resolve for the provider API key.
|
|
7
|
+
*
|
|
8
|
+
* When omitted, the agent runtime falls back to a provider-specific default
|
|
9
|
+
* for known providers such as Gemini, OpenAI, and Anthropic.
|
|
10
|
+
*/
|
|
11
|
+
apiKeySecretName?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to fall back to ancestor tenants when the current tenant does not
|
|
14
|
+
* define the requested secret.
|
|
15
|
+
*
|
|
16
|
+
* Defaults to `'ancestors'`.
|
|
17
|
+
*/
|
|
18
|
+
apiKeySecretFallback?: AgentAISecretFallback;
|
|
19
|
+
}
|
|
20
|
+
interface ResolveAgentAIOptionsInput {
|
|
21
|
+
aiConfig: AgentAIOptions | undefined;
|
|
22
|
+
db: DatabaseInterface | null | undefined;
|
|
23
|
+
tenantId?: string | null;
|
|
24
|
+
}
|
|
25
|
+
export declare function resolveAgentAIOptions(input: ResolveAgentAIOptionsInput): Promise<AIClientOptions | undefined>;
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=ai-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-config.d.ts","sourceRoot":"","sources":["../src/ai-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAIzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,WAAW,CAAC;AAEzD,MAAM,WAAW,cAAe,SAAQ,eAAe;IACrD;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,qBAAqB,CAAC;CAC9C;AAED,UAAU,0BAA0B;IAClC,QAAQ,EAAE,cAAc,GAAG,SAAS,CAAC;IACrC,EAAE,EAAE,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAmID,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,0BAA0B,GAChC,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC,CAsCtC"}
|