@naylence/agent-sdk 0.3.11 → 0.3.13
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/README.md +30 -0
- package/dist/browser/index.js +3268 -1913
- package/dist/browser/index.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +27 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/naylence/agent/agent-proxy.js.map +1 -1
- package/dist/cjs/naylence/agent/agent.js.map +1 -1
- package/dist/cjs/naylence/agent/background-task-agent.js.map +1 -1
- package/dist/cjs/naylence/agent/base-agent.js.map +1 -1
- package/dist/cjs/naylence/agent/configs.d.ts +12 -0
- package/dist/cjs/naylence/agent/configs.js +12 -0
- package/dist/cjs/naylence/agent/configs.js.map +1 -1
- package/dist/cjs/public-api.d.ts +7 -0
- package/dist/cjs/public-api.js +26 -0
- package/dist/cjs/public-api.js.map +1 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +31 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/naylence/agent/agent-proxy.d.ts +148 -0
- package/dist/esm/naylence/agent/agent-proxy.js +130 -0
- package/dist/esm/naylence/agent/agent-proxy.js.map +1 -1
- package/dist/esm/naylence/agent/agent.d.ts +194 -2
- package/dist/esm/naylence/agent/agent.js +123 -0
- package/dist/esm/naylence/agent/agent.js.map +1 -1
- package/dist/esm/naylence/agent/background-task-agent.d.ts +164 -0
- package/dist/esm/naylence/agent/background-task-agent.js +150 -0
- package/dist/esm/naylence/agent/background-task-agent.js.map +1 -1
- package/dist/esm/naylence/agent/base-agent.d.ts +171 -0
- package/dist/esm/naylence/agent/base-agent.js +170 -1
- package/dist/esm/naylence/agent/base-agent.js.map +1 -1
- package/dist/esm/naylence/agent/configs.d.ts +12 -0
- package/dist/esm/naylence/agent/configs.js +12 -0
- package/dist/esm/naylence/agent/configs.js.map +1 -1
- package/dist/esm/public-api.d.ts +15 -0
- package/dist/esm/public-api.js +16 -0
- package/dist/esm/public-api.js.map +1 -0
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +2 -2
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/naylence/agent/agent-proxy.d.ts +148 -0
- package/dist/types/naylence/agent/agent-proxy.d.ts.map +1 -1
- package/dist/types/naylence/agent/agent.d.ts +194 -2
- package/dist/types/naylence/agent/agent.d.ts.map +1 -1
- package/dist/types/naylence/agent/background-task-agent.d.ts +164 -0
- package/dist/types/naylence/agent/background-task-agent.d.ts.map +1 -1
- package/dist/types/naylence/agent/base-agent.d.ts +171 -0
- package/dist/types/naylence/agent/base-agent.d.ts.map +1 -1
- package/dist/types/naylence/agent/configs.d.ts +12 -0
- package/dist/types/naylence/agent/configs.d.ts.map +1 -1
- package/dist/types/public-api.d.ts +16 -0
- package/dist/types/public-api.d.ts.map +1 -0
- package/dist/types/version.d.ts +1 -1
- package/package.json +7 -2
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent module providing the abstract {@link Agent} class.
|
|
3
|
+
*
|
|
4
|
+
* An Agent is a self-contained unit of work that can receive tasks, process them,
|
|
5
|
+
* and return results. Agents communicate over the Fame fabric using a standard
|
|
6
|
+
* task-based protocol.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* For concrete implementations:
|
|
10
|
+
* - Use {@link BaseAgent} when you need full control over task handling and state.
|
|
11
|
+
* - Use {@link BackgroundTaskAgent} for long-running or async background work.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
1
15
|
import { FameAddress, FameFabric, LogLevel } from '@naylence/runtime';
|
|
2
16
|
import { RpcMixin } from '@naylence/runtime';
|
|
3
17
|
import type { FameService } from '@naylence/core';
|
|
@@ -6,25 +20,57 @@ export { registerAgentProxyFactory } from './agent-proxy-registry.js';
|
|
|
6
20
|
export type { AgentProxyConstructor } from './agent-proxy-registry.js';
|
|
7
21
|
import type { BaseAgent } from './base-agent.js';
|
|
8
22
|
import type { AgentProxy } from './agent-proxy.js';
|
|
23
|
+
/**
|
|
24
|
+
* Payload type for agent task messages.
|
|
25
|
+
*
|
|
26
|
+
* Can be a JSON object, a string, or null.
|
|
27
|
+
*/
|
|
9
28
|
export type Payload = Record<string, unknown> | string | null;
|
|
29
|
+
/**
|
|
30
|
+
* Collection of address-payload pairs for broadcasting tasks to multiple agents.
|
|
31
|
+
*/
|
|
10
32
|
export type Targets = Iterable<readonly [FameAddress | string, Payload]>;
|
|
33
|
+
/** @internal */
|
|
11
34
|
type AgentTaskHandler = (payload: Payload, id: string | null) => unknown | Promise<unknown>;
|
|
35
|
+
/**
|
|
36
|
+
* Constructor signature for BaseAgent implementations.
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
12
39
|
export type BaseAgentConstructor = new (name?: string | null, options?: any) => BaseAgent;
|
|
13
40
|
/** @internal */
|
|
14
41
|
export declare function registerBaseAgentConstructor(ctor: BaseAgentConstructor): void;
|
|
42
|
+
/**
|
|
43
|
+
* Options for creating a remote agent proxy.
|
|
44
|
+
*
|
|
45
|
+
* Provide exactly one of `address` or `capabilities` to identify the target.
|
|
46
|
+
*/
|
|
15
47
|
export interface AgentRemoteOptions {
|
|
48
|
+
/** Direct address of the target agent. */
|
|
16
49
|
address?: FameAddress | string;
|
|
50
|
+
/** Required capabilities for capability-based discovery. */
|
|
17
51
|
capabilities?: string[];
|
|
52
|
+
/** Fabric instance to use. Defaults to the current fabric. */
|
|
18
53
|
fabric?: FameFabric;
|
|
19
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Options for running tasks on multiple agents.
|
|
57
|
+
*/
|
|
20
58
|
export interface AgentRunManyOptions {
|
|
59
|
+
/** Fabric instance to use. Defaults to the current fabric. */
|
|
21
60
|
fabric?: FameFabric;
|
|
61
|
+
/**
|
|
62
|
+
* If true (default), errors are collected alongside results.
|
|
63
|
+
* If false, the first error rejects the promise.
|
|
64
|
+
*/
|
|
22
65
|
gatherExceptions?: boolean;
|
|
23
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Options for serving an agent at a given address.
|
|
69
|
+
*/
|
|
24
70
|
export interface AgentServeOptions {
|
|
25
71
|
/**
|
|
26
|
-
* Log level for the agent.
|
|
27
|
-
*
|
|
72
|
+
* Log level for the agent.
|
|
73
|
+
* Accepts 'debug', 'info', 'warning', 'error', 'critical', or a LogLevel value.
|
|
28
74
|
*/
|
|
29
75
|
logLevel?: string | number | LogLevel | null;
|
|
30
76
|
/**
|
|
@@ -40,31 +86,177 @@ export interface AgentServeOptions {
|
|
|
40
86
|
*/
|
|
41
87
|
[key: string]: unknown;
|
|
42
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Abstract base class for all agents.
|
|
91
|
+
*
|
|
92
|
+
* Agents are addressable services that handle tasks over the Fame fabric.
|
|
93
|
+
* This class defines the core protocol methods every agent must implement.
|
|
94
|
+
*
|
|
95
|
+
* @remarks
|
|
96
|
+
* Do not extend Agent directly. Instead:
|
|
97
|
+
* - Extend {@link BaseAgent} for standard request-response agents.
|
|
98
|
+
* - Extend {@link BackgroundTaskAgent} for long-running background work.
|
|
99
|
+
*
|
|
100
|
+
* Use {@link Agent.remote} or {@link Agent.remoteByAddress} to create proxies
|
|
101
|
+
* for communicating with remote agents.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import { BaseAgent, Payload } from '@naylence/agent-sdk';
|
|
106
|
+
*
|
|
107
|
+
* class EchoAgent extends BaseAgent {
|
|
108
|
+
* async runTask(payload: Payload): Promise<Payload> {
|
|
109
|
+
* return payload;
|
|
110
|
+
* }
|
|
111
|
+
* }
|
|
112
|
+
*
|
|
113
|
+
* const agent = new EchoAgent('echo');
|
|
114
|
+
* await agent.serve('fame://echo');
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
43
117
|
export declare abstract class Agent extends RpcMixin implements FameService {
|
|
118
|
+
/**
|
|
119
|
+
* Capabilities advertised by this agent for discovery.
|
|
120
|
+
*/
|
|
44
121
|
get capabilities(): string[] | undefined;
|
|
122
|
+
/** The agent's name, used for logging and identification. */
|
|
45
123
|
abstract get name(): string | null;
|
|
124
|
+
/** Returns metadata about this agent (address, capabilities, etc.). */
|
|
46
125
|
abstract get spec(): Record<string, unknown>;
|
|
126
|
+
/** Returns the agent's card describing its capabilities and metadata. */
|
|
47
127
|
abstract getAgentCard(): Promise<AgentCard>;
|
|
128
|
+
/**
|
|
129
|
+
* Validates authentication credentials.
|
|
130
|
+
* @param credentials - The credentials to validate.
|
|
131
|
+
* @returns True if authentication succeeds.
|
|
132
|
+
*/
|
|
48
133
|
abstract authenticate(credentials: AuthenticationInfo): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Initiates a new task.
|
|
136
|
+
* @param params - Task parameters including message and optional metadata.
|
|
137
|
+
* @returns The created task with its initial status.
|
|
138
|
+
*/
|
|
49
139
|
abstract startTask(params: TaskSendParams): Promise<Task>;
|
|
140
|
+
/**
|
|
141
|
+
* Executes a task synchronously and returns the result.
|
|
142
|
+
* @param payload - The task payload.
|
|
143
|
+
* @param id - Optional task identifier.
|
|
144
|
+
* @returns The task result.
|
|
145
|
+
*/
|
|
50
146
|
abstract runTask(payload: Payload, id: string | null): Promise<any>;
|
|
147
|
+
/**
|
|
148
|
+
* Retrieves the current status of a task.
|
|
149
|
+
* @param params - Query parameters including the task ID.
|
|
150
|
+
*/
|
|
51
151
|
abstract getTaskStatus(params: TaskQueryParams): Promise<Task>;
|
|
152
|
+
/**
|
|
153
|
+
* Requests cancellation of a running task.
|
|
154
|
+
* @param params - Parameters including the task ID.
|
|
155
|
+
* @returns The task with updated status.
|
|
156
|
+
*/
|
|
52
157
|
abstract cancelTask(params: TaskIdParams): Promise<Task>;
|
|
158
|
+
/**
|
|
159
|
+
* Subscribes to real-time updates for a task.
|
|
160
|
+
* @param params - Task parameters.
|
|
161
|
+
* @returns An async iterable of status and artifact update events.
|
|
162
|
+
*/
|
|
53
163
|
abstract subscribeToTaskUpdates(params: TaskSendParams): AsyncIterable<TaskStatusUpdateEvent | TaskArtifactUpdateEvent>;
|
|
164
|
+
/**
|
|
165
|
+
* Cancels a task subscription.
|
|
166
|
+
* @param params - Parameters including the task ID.
|
|
167
|
+
*/
|
|
54
168
|
abstract unsubscribeTask(params: TaskIdParams): Promise<any>;
|
|
169
|
+
/**
|
|
170
|
+
* Registers a push notification endpoint for task updates.
|
|
171
|
+
* @param config - Push notification configuration.
|
|
172
|
+
*/
|
|
55
173
|
abstract registerPushEndpoint(config: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig>;
|
|
174
|
+
/**
|
|
175
|
+
* Retrieves the push notification config for a task.
|
|
176
|
+
* @param params - Parameters including the task ID.
|
|
177
|
+
*/
|
|
56
178
|
abstract getPushNotificationConfig(params: TaskIdParams): Promise<TaskPushNotificationConfig>;
|
|
179
|
+
/**
|
|
180
|
+
* Creates a proxy for communicating with a remote agent.
|
|
181
|
+
*
|
|
182
|
+
* @remarks
|
|
183
|
+
* Provide exactly one of `address` or `capabilities` in the options.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const proxy = Agent.remote<EchoAgent>({ address: 'fame://echo' });
|
|
188
|
+
* const result = await proxy.runTask('hello');
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @param options - Remote agent options.
|
|
192
|
+
* @returns A proxy for the remote agent.
|
|
193
|
+
* @throws Error if both or neither of address/capabilities are provided.
|
|
194
|
+
*/
|
|
57
195
|
static remote<TAgent extends Agent>(this: typeof Agent, options: AgentRemoteOptions): AgentProxy<TAgent>;
|
|
196
|
+
/**
|
|
197
|
+
* Creates a proxy for a remote agent by its address.
|
|
198
|
+
*
|
|
199
|
+
* @param address - The target agent's address.
|
|
200
|
+
* @param options - Optional fabric configuration.
|
|
201
|
+
* @returns A proxy for the remote agent.
|
|
202
|
+
*/
|
|
58
203
|
static remoteByAddress<TAgent extends Agent>(this: typeof Agent, address: FameAddress | string, options?: {
|
|
59
204
|
fabric?: FameFabric;
|
|
60
205
|
}): AgentProxy<TAgent>;
|
|
206
|
+
/**
|
|
207
|
+
* Creates a proxy for a remote agent by required capabilities.
|
|
208
|
+
*
|
|
209
|
+
* @param capabilities - Required capabilities for discovery.
|
|
210
|
+
* @param options - Optional fabric configuration.
|
|
211
|
+
* @returns A proxy for a matching remote agent.
|
|
212
|
+
*/
|
|
61
213
|
static remoteByCapabilities<TAgent extends Agent>(this: typeof Agent, capabilities: string[], options?: {
|
|
62
214
|
fabric?: FameFabric;
|
|
63
215
|
}): AgentProxy<TAgent>;
|
|
216
|
+
/**
|
|
217
|
+
* Creates an agent from a simple handler function.
|
|
218
|
+
*
|
|
219
|
+
* @remarks
|
|
220
|
+
* Useful for quick prototyping without defining a full agent class.
|
|
221
|
+
*
|
|
222
|
+
* @param handler - Function that processes task payloads.
|
|
223
|
+
* @returns A new agent instance wrapping the handler.
|
|
224
|
+
*/
|
|
64
225
|
static fromHandler(handler: AgentTaskHandler): Promise<Agent>;
|
|
226
|
+
/**
|
|
227
|
+
* Sends the same payload to multiple agents.
|
|
228
|
+
*
|
|
229
|
+
* @param addresses - List of agent addresses.
|
|
230
|
+
* @param payload - Payload to send to all agents.
|
|
231
|
+
* @param options - Execution options.
|
|
232
|
+
* @returns Array of [address, result|error] tuples.
|
|
233
|
+
*/
|
|
65
234
|
static broadcast(this: typeof Agent, addresses: Array<FameAddress | string>, payload?: Payload, options?: AgentRunManyOptions): Promise<Array<[string, any | Error]>>;
|
|
235
|
+
/**
|
|
236
|
+
* Runs tasks on multiple agents with individual payloads.
|
|
237
|
+
*
|
|
238
|
+
* @param targets - Iterable of [address, payload] pairs.
|
|
239
|
+
* @param options - Execution options.
|
|
240
|
+
* @returns Array of [address, result|error] tuples.
|
|
241
|
+
*/
|
|
66
242
|
static runMany<TAgent extends Agent>(this: typeof Agent, targets: Targets, options?: AgentRunManyOptions): Promise<Array<[string, any | Error]>>;
|
|
243
|
+
/**
|
|
244
|
+
* Starts serving this agent at the given address.
|
|
245
|
+
*
|
|
246
|
+
* @remarks
|
|
247
|
+
* In Node.js, the agent listens for SIGINT/SIGTERM to shut down gracefully.
|
|
248
|
+
* The method returns when the agent stops serving.
|
|
249
|
+
*
|
|
250
|
+
* @param address - The address to serve at (e.g., 'fame://my-agent').
|
|
251
|
+
* @param options - Serve options including log level and fabric config.
|
|
252
|
+
*/
|
|
67
253
|
aserve(address: FameAddress | string, options?: AgentServeOptions): Promise<void>;
|
|
254
|
+
/**
|
|
255
|
+
* Alias for {@link Agent.aserve}.
|
|
256
|
+
*
|
|
257
|
+
* @param address - The address to serve at.
|
|
258
|
+
* @param options - Serve options.
|
|
259
|
+
*/
|
|
68
260
|
serve(address: FameAddress | string, options?: AgentServeOptions): Promise<void>;
|
|
69
261
|
}
|
|
70
262
|
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../../../src/naylence/agent/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,UAAU,EAKV,QAAQ,EACT,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EACV,SAAS,EACT,kBAAkB,EAClB,IAAI,EACJ,uBAAuB,EACvB,YAAY,EACZ,0BAA0B,EAC1B,eAAe,EACf,cAAc,EACd,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,YAAY,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAEvE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CA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OAMG;IACH,MAAM,CAAC,oBAAoB,CAAC,MAAM,SAAS,KAAK,EAC9C,IAAI,EAAE,OAAO,KAAK,EAClB,YAAY,EAAE,MAAM,EAAE,EACtB,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,UAAU,CAAA;KAAO,GACpC,UAAU,CAAC,MAAM,CAAC;IAQrB;;;;;;;;OAQG;WACU,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,KAAK,CAAC;IAoBnE;;;;;;;OAOG;WACU,SAAS,CACpB,IAAI,EAAE,OAAO,KAAK,EAClB,SAAS,EAAE,KAAK,CAAC,WAAW,GAAG,MAAM,CAAC,EACtC,OAAO,GAAE,OAAc,EACvB,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC;IAOxC;;;;;;OAMG;WACU,OAAO,CAAC,MAAM,SAAS,KAAK,EACvC,IAAI,EAAE,OAAO,KAAK,EAClB,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC;IAyCxC;;;;;;;;;OASG;IACG,MAAM,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;IAiC3F;;;;;OAKG;IACH,KAAK,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;CAGrF"}
|
|
@@ -1,36 +1,200 @@
|
|
|
1
1
|
import { type Artifact, type Message, Task, type TaskArtifactUpdateEvent, TaskIdParams, type TaskQueryParams, TaskSendParams, TaskState, type TaskStatusUpdateEvent } from './a2a-types.js';
|
|
2
2
|
import { BaseAgent, type BaseAgentOptions, type BaseAgentState } from './base-agent.js';
|
|
3
|
+
/** @internal */
|
|
3
4
|
type TaskEvent = TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for {@link BackgroundTaskAgent}.
|
|
7
|
+
*/
|
|
4
8
|
export interface BackgroundTaskAgentOptions<StateT extends BaseAgentState> extends BaseAgentOptions<StateT> {
|
|
9
|
+
/** Maximum number of events buffered per task. Defaults to 1000. */
|
|
5
10
|
maxQueueSize?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum task execution time in milliseconds.
|
|
13
|
+
* Tasks exceeding this limit are automatically canceled. Null disables the limit.
|
|
14
|
+
*/
|
|
6
15
|
maxTaskLifetimeMs?: number | null;
|
|
16
|
+
/** Maximum number of completed tasks to cache. Defaults to 100. */
|
|
7
17
|
completedCacheSize?: number;
|
|
18
|
+
/** Time-to-live for cached completed tasks in seconds. Defaults to 300. */
|
|
8
19
|
completedCacheTtlSec?: number;
|
|
9
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Base class for agents that execute long-running background tasks.
|
|
23
|
+
*
|
|
24
|
+
* Unlike {@link BaseAgent} which runs tasks synchronously in startTask,
|
|
25
|
+
* BackgroundTaskAgent starts tasks in the background and streams status
|
|
26
|
+
* updates to subscribers. This is ideal for work that takes significant time.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* Lifecycle:
|
|
30
|
+
* 1. Client calls startTask, which returns immediately with WORKING status
|
|
31
|
+
* 2. runBackgroundTask executes asynchronously
|
|
32
|
+
* 3. Status updates are queued and delivered to subscribers
|
|
33
|
+
* 4. Task completes with COMPLETED, FAILED, or CANCELED status
|
|
34
|
+
*
|
|
35
|
+
* Completed tasks are cached briefly for late subscribers to retrieve final status.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import {
|
|
40
|
+
* BackgroundTaskAgent,
|
|
41
|
+
* TaskSendParams,
|
|
42
|
+
* TaskState,
|
|
43
|
+
* } from '@naylence/agent-sdk';
|
|
44
|
+
*
|
|
45
|
+
* class SlowAgent extends BackgroundTaskAgent {
|
|
46
|
+
* protected async runBackgroundTask(params: TaskSendParams): Promise<string> {
|
|
47
|
+
* // Report progress
|
|
48
|
+
* await this.updateTaskState(params.id, TaskState.WORKING);
|
|
49
|
+
*
|
|
50
|
+
* // Do expensive work
|
|
51
|
+
* await someSlowOperation();
|
|
52
|
+
*
|
|
53
|
+
* // Return value becomes the task result
|
|
54
|
+
* return 'done';
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* const agent = new SlowAgent('slow-worker', {
|
|
59
|
+
* maxTaskLifetimeMs: 60_000, // 1 minute timeout
|
|
60
|
+
* });
|
|
61
|
+
* await agent.serve('fame://slow-worker');
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @typeParam StateT - The state model type, defaults to {@link BaseAgentState}.
|
|
65
|
+
*/
|
|
10
66
|
export declare abstract class BackgroundTaskAgent<StateT extends BaseAgentState = BaseAgentState> extends BaseAgent<StateT> {
|
|
67
|
+
/** @internal */
|
|
11
68
|
private readonly maxQueueSize;
|
|
69
|
+
/** @internal */
|
|
12
70
|
private readonly maxTaskLifetimeMs;
|
|
71
|
+
/** @internal */
|
|
13
72
|
private readonly taskStatuses;
|
|
73
|
+
/** @internal */
|
|
14
74
|
private readonly taskEventQueues;
|
|
75
|
+
/** @internal */
|
|
15
76
|
private readonly completed;
|
|
77
|
+
/** @internal */
|
|
16
78
|
private readonly completedCacheSize;
|
|
79
|
+
/** @internal */
|
|
17
80
|
private readonly completedCacheTtlSec;
|
|
81
|
+
/** @internal */
|
|
18
82
|
private readonly statusLock;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a new BackgroundTaskAgent.
|
|
85
|
+
*
|
|
86
|
+
* @param name - Agent name. Defaults to snake_case of the class name.
|
|
87
|
+
* @param options - Configuration options for task execution and caching.
|
|
88
|
+
*/
|
|
19
89
|
protected constructor(name?: string | null, options?: BackgroundTaskAgentOptions<StateT>);
|
|
90
|
+
/** @internal */
|
|
20
91
|
private isTerminalTaskState;
|
|
92
|
+
/**
|
|
93
|
+
* Starts a background task.
|
|
94
|
+
*
|
|
95
|
+
* Returns immediately with WORKING status. The task executes asynchronously
|
|
96
|
+
* via {@link BackgroundTaskAgent.runBackgroundTask}.
|
|
97
|
+
*
|
|
98
|
+
* @param params - Task parameters.
|
|
99
|
+
* @returns The task with initial WORKING status.
|
|
100
|
+
*/
|
|
21
101
|
startTask(params: TaskSendParams): Promise<Task>;
|
|
102
|
+
/** @internal */
|
|
22
103
|
private enforceMaxLifetime;
|
|
104
|
+
/** @internal */
|
|
23
105
|
private runBackgroundTaskInternal;
|
|
106
|
+
/**
|
|
107
|
+
* Override to implement background task execution logic.
|
|
108
|
+
*
|
|
109
|
+
* This method runs asynchronously after startTask returns. The return value
|
|
110
|
+
* becomes the task's completion message. Throwing an error fails the task.
|
|
111
|
+
*
|
|
112
|
+
* @remarks
|
|
113
|
+
* Call {@link BackgroundTaskAgent.updateTaskState} to report progress.
|
|
114
|
+
* Call {@link BackgroundTaskAgent.updateTaskArtifact} to emit artifacts.
|
|
115
|
+
*
|
|
116
|
+
* @param params - The original task parameters.
|
|
117
|
+
* @returns The task result, which is included in the completion message.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* protected async runBackgroundTask(params: TaskSendParams): Promise<unknown> {
|
|
122
|
+
* for (let i = 0; i < 10; i++) {
|
|
123
|
+
* await this.updateTaskState(params.id, TaskState.WORKING);
|
|
124
|
+
* await doStep(i);
|
|
125
|
+
* }
|
|
126
|
+
* return { steps: 10 };
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
24
130
|
protected abstract runBackgroundTask(params: TaskSendParams): Promise<unknown>;
|
|
131
|
+
/**
|
|
132
|
+
* Gets the current state of a task.
|
|
133
|
+
*
|
|
134
|
+
* @param taskId - The task identifier.
|
|
135
|
+
* @returns The current task state, or UNKNOWN if not found.
|
|
136
|
+
*/
|
|
25
137
|
getTaskState(taskId: string): Promise<TaskState>;
|
|
138
|
+
/**
|
|
139
|
+
* Retrieves the full status of a task.
|
|
140
|
+
*
|
|
141
|
+
* @param params - Query parameters including the task ID.
|
|
142
|
+
* @returns The task with current status.
|
|
143
|
+
* @throws Error if the task is unknown or expired.
|
|
144
|
+
*/
|
|
26
145
|
getTaskStatus(params: TaskQueryParams): Promise<Task>;
|
|
146
|
+
/**
|
|
147
|
+
* Updates the state of a running task.
|
|
148
|
+
*
|
|
149
|
+
* Call this from {@link BackgroundTaskAgent.runBackgroundTask} to report progress.
|
|
150
|
+
* Status updates are delivered to subscribers.
|
|
151
|
+
*
|
|
152
|
+
* @param taskId - The task identifier.
|
|
153
|
+
* @param state - The new task state.
|
|
154
|
+
* @param message - Optional message with details.
|
|
155
|
+
* @returns True if the update was applied, false if the task is already terminal.
|
|
156
|
+
*/
|
|
27
157
|
updateTaskState(taskId: string, state: TaskState, message?: Message | null): Promise<boolean>;
|
|
158
|
+
/** @internal */
|
|
28
159
|
private addToCompleted;
|
|
160
|
+
/**
|
|
161
|
+
* Emits an artifact for a running task.
|
|
162
|
+
*
|
|
163
|
+
* Artifacts are delivered to subscribers as TaskArtifactUpdateEvents.
|
|
164
|
+
*
|
|
165
|
+
* @param taskId - The task identifier.
|
|
166
|
+
* @param artifact - The artifact to emit.
|
|
167
|
+
*/
|
|
29
168
|
updateTaskArtifact(taskId: string, artifact: Artifact): Promise<void>;
|
|
169
|
+
/**
|
|
170
|
+
* Subscribes to updates for a task.
|
|
171
|
+
*
|
|
172
|
+
* Returns an async iterable that yields status and artifact events.
|
|
173
|
+
* For completed tasks, yields the cached final status if still available.
|
|
174
|
+
*
|
|
175
|
+
* @param params - Task parameters including the task ID.
|
|
176
|
+
* @returns Async iterable of task events.
|
|
177
|
+
*/
|
|
30
178
|
subscribeToTaskUpdates(params: TaskSendParams): AsyncIterable<TaskEvent>;
|
|
179
|
+
/**
|
|
180
|
+
* Cancels a task subscription.
|
|
181
|
+
*
|
|
182
|
+
* @param params - Parameters including the task ID.
|
|
183
|
+
*/
|
|
31
184
|
unsubscribeTask(params: TaskIdParams): Promise<void>;
|
|
185
|
+
/**
|
|
186
|
+
* Cancels a running task.
|
|
187
|
+
*
|
|
188
|
+
* Sets the task state to CANCELED. Does not interrupt runBackgroundTask,
|
|
189
|
+
* but prevents further state updates.
|
|
190
|
+
*
|
|
191
|
+
* @param params - Parameters including the task ID.
|
|
192
|
+
* @returns The task with CANCELED status.
|
|
193
|
+
*/
|
|
32
194
|
cancelTask(params: TaskIdParams): Promise<Task>;
|
|
195
|
+
/** @internal */
|
|
33
196
|
private purgeCompletedAfterTtl;
|
|
197
|
+
/** @internal */
|
|
34
198
|
private normalizeResultPayload;
|
|
35
199
|
}
|
|
36
200
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"background-task-agent.d.ts","sourceRoot":"","sources":["../../../../src/naylence/agent/background-task-agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"background-task-agent.d.ts","sourceRoot":"","sources":["../../../../src/naylence/agent/background-task-agent.ts"],"names":[],"mappings":"AAmBA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,OAAO,EACZ,IAAI,EACJ,KAAK,uBAAuB,EAC5B,YAAY,EACZ,KAAK,eAAe,EACpB,cAAc,EACd,SAAS,EAET,KAAK,qBAAqB,EAC3B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,SAAS,EACT,KAAK,gBAAgB,EACrB,KAAK,cAAc,EAEpB,MAAM,iBAAiB,CAAC;AAuBzB,gBAAgB;AAChB,KAAK,SAAS,GAAG,qBAAqB,GAAG,uBAAuB,CAAC;AAuGjE;;GAEG;AACH,MAAM,WAAW,0BAA0B,CAAC,MAAM,SAAS,cAAc,CACvE,SAAQ,gBAAgB,CAAC,MAAM,CAAC;IAChC,oEAAoE;IACpE,YAAY,CAAC,EAAE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