@artinet/sdk 0.6.0-preview.1 → 0.6.0-preview.3
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 +8 -17
- package/dist/browser/client/a2a-client.js +4 -4
- package/dist/browser/config/index.d.ts +1 -1
- package/dist/browser/config/index.js +1 -1
- package/dist/browser/create/agentcard-builder.d.ts +47 -0
- package/dist/browser/create/agentcard-builder.js +65 -0
- package/dist/browser/create/base.d.ts +4 -0
- package/dist/browser/create/base.js +1 -0
- package/dist/browser/create/describe.d.ts +8 -0
- package/dist/browser/create/describe.js +8 -0
- package/dist/browser/create/message-builder.d.ts +78 -0
- package/dist/browser/create/message-builder.js +108 -0
- package/dist/browser/create/part-builder.d.ts +60 -0
- package/dist/browser/create/part-builder.js +81 -0
- package/dist/browser/create/task-builder.d.ts +251 -0
- package/dist/browser/create/task-builder.js +379 -0
- package/dist/browser/transport/rpc/parser.d.ts +1 -1
- package/dist/browser/transport/rpc/parser.js +2 -1
- package/dist/browser/types/a2a/a2a.d.ts +7 -3
- package/dist/browser/types/a2a/index.d.ts +3 -1
- package/dist/browser/types/a2a/index.js +0 -1
- package/dist/browser/utils/common/constants.d.ts +0 -5
- package/dist/browser/utils/common/constants.js +0 -27
- package/dist/browser/utils/common/errors.d.ts +57 -1
- package/dist/browser/utils/common/errors.js +68 -15
- package/dist/client/a2a-client.js +4 -4
- package/dist/config/default.d.ts +1 -1
- package/dist/config/default.js +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.js +1 -1
- package/dist/create/agent-builder.d.ts +77 -0
- package/dist/create/agent-builder.js +20 -0
- package/dist/create/agentcard-builder.d.ts +47 -0
- package/dist/create/agentcard-builder.js +66 -0
- package/dist/create/base.d.ts +4 -0
- package/dist/create/base.js +1 -0
- package/dist/create/create.d.ts +762 -0
- package/dist/create/create.js +556 -0
- package/dist/create/describe.d.ts +8 -0
- package/dist/create/describe.js +8 -0
- package/dist/create/index.d.ts +4 -0
- package/dist/create/index.js +4 -0
- package/dist/create/message-builder.d.ts +78 -0
- package/dist/create/message-builder.js +110 -0
- package/dist/create/part-builder.d.ts +60 -0
- package/dist/create/part-builder.js +84 -0
- package/dist/create/status-builder.d.ts +30 -0
- package/dist/create/status-builder.js +46 -0
- package/dist/create/task-builder.d.ts +251 -0
- package/dist/create/task-builder.js +384 -0
- package/dist/create/transform.d.ts +16 -0
- package/dist/create/transform.js +106 -0
- package/dist/extensions/otel.d.ts +3 -0
- package/dist/extensions/otel.js +3 -0
- package/dist/extensions/pino.d.ts +3 -0
- package/dist/extensions/pino.js +3 -0
- package/dist/extensions/winston.d.ts +3 -0
- package/dist/extensions/winston.js +3 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/server/express/errors.js +4 -8
- package/dist/server/express/middeware.js +17 -38
- package/dist/server/express/server.d.ts +20 -1
- package/dist/server/express/server.js +44 -12
- package/dist/services/a2a/execute.d.ts +5 -0
- package/dist/services/a2a/execute.js +7 -0
- package/dist/services/a2a/factory/context.d.ts +1 -1
- package/dist/services/a2a/factory/context.js +2 -3
- package/dist/services/a2a/factory/handler.js +3 -3
- package/dist/services/a2a/factory/index.d.ts +0 -1
- package/dist/services/a2a/factory/index.js +0 -1
- package/dist/services/a2a/factory/service.d.ts +2 -2
- package/dist/services/a2a/factory/service.js +2 -2
- package/dist/services/a2a/factory/state-machine.d.ts +1 -1
- package/dist/services/a2a/factory/state-machine.js +30 -8
- package/dist/services/a2a/handlers/artifact.d.ts +2 -5
- package/dist/services/a2a/handlers/artifact.js +21 -32
- package/dist/services/a2a/handlers/cancel-task.js +7 -5
- package/dist/services/a2a/handlers/resubscribe-task.d.ts +10 -2
- package/dist/services/a2a/handlers/resubscribe-task.js +21 -18
- package/dist/services/a2a/handlers/send-message.js +6 -10
- package/dist/services/a2a/handlers/stream-message.d.ts +10 -2
- package/dist/services/a2a/handlers/stream-message.js +5 -1
- package/dist/services/a2a/handlers/update.js +15 -12
- package/dist/services/a2a/helpers/content.d.ts +5 -1
- package/dist/services/a2a/helpers/content.js +5 -1
- package/dist/services/a2a/helpers/index.d.ts +2 -2
- package/dist/services/a2a/helpers/index.js +2 -2
- package/dist/services/a2a/index.d.ts +1 -1
- package/dist/services/a2a/index.js +1 -1
- package/dist/services/a2a/managers.js +7 -1
- package/dist/services/a2a/service.d.ts +6 -2
- package/dist/services/a2a/service.js +54 -61
- package/dist/services/a2a/state-machine.d.ts +3 -3
- package/dist/services/a2a/state-machine.js +2 -0
- package/dist/transport/rpc/parser.d.ts +1 -1
- package/dist/transport/rpc/parser.js +2 -1
- package/dist/transport/trpc/a2a/routes/message/route.js +2 -1
- package/dist/transport/trpc/a2a/routes/tasks/route.js +2 -1
- package/dist/types/a2a/a2a.d.ts +7 -3
- package/dist/types/a2a/index.d.ts +3 -1
- package/dist/types/a2a/index.js +0 -1
- package/dist/utils/common/constants.d.ts +0 -5
- package/dist/utils/common/constants.js +0 -27
- package/dist/utils/common/errors.d.ts +57 -1
- package/dist/utils/common/errors.js +68 -15
- package/dist/utils/common/parse.d.ts +1 -1
- package/dist/utils/common/schema-validation.d.ts +1 -1
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/package.json +8 -5
- package/dist/browser/services/a2a/helpers/message-builder.d.ts +0 -17
- package/dist/browser/services/a2a/helpers/message-builder.js +0 -66
- package/dist/browser/types/a2a/builder.d.ts +0 -43
- package/dist/browser/types/a2a/builder.js +0 -5
- package/dist/services/a2a/factory/builder.d.ts +0 -292
- package/dist/services/a2a/factory/builder.js +0 -370
- package/dist/services/a2a/helpers/agentcard-builder.d.ts +0 -11
- package/dist/services/a2a/helpers/agentcard-builder.js +0 -27
- package/dist/services/a2a/helpers/message-builder.d.ts +0 -17
- package/dist/services/a2a/helpers/message-builder.js +0 -66
- package/dist/types/a2a/builder.d.ts +0 -43
- package/dist/types/a2a/builder.js +0 -5
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2025 The Artinet Project
|
|
3
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @fileoverview A2A Agent Builder and Execution Engine Factory
|
|
7
|
-
*
|
|
8
|
-
* This module provides a fluent builder API for constructing A2A agents and
|
|
9
|
-
* execution engines. It enables declarative definition of multi-step agent
|
|
10
|
-
* workflows with type-safe step composition and automatic execution orchestration.
|
|
11
|
-
*
|
|
12
|
-
* @module A2ABuilder
|
|
13
|
-
* @version 0.6.0-preview
|
|
14
|
-
* @since 0.5.6
|
|
15
|
-
* @author The Artinet Project
|
|
16
|
-
*/
|
|
17
|
-
import { StepBuilder, Step, A2A, StepWithKind, OutputType, BaseArgs, EmptyArgs, OutArgsOf, PartContent } from "../../../types/index.js";
|
|
18
|
-
import { ServiceParams } from "./service.js";
|
|
19
|
-
/**
|
|
20
|
-
* Type alias for text-based workflow steps.
|
|
21
|
-
*
|
|
22
|
-
* This type represents a step that processes or generates text content
|
|
23
|
-
* within an agent workflow. Text steps are the most common type of step
|
|
24
|
-
* and are used for message processing, content generation, and text-based
|
|
25
|
-
* decision making.
|
|
26
|
-
*
|
|
27
|
-
* @template TCommand - The command type, defaults to MessageSendParams
|
|
28
|
-
* @template TPart - The text part type, defaults to A2A.TextPart["text"]
|
|
29
|
-
* @template TInboundArgs - Arguments received from previous step
|
|
30
|
-
* @template TForwardArgs - Arguments passed to next step
|
|
31
|
-
* @template TOutput - The output type of the step
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* ```typescript
|
|
35
|
-
* const greetingStep: textStep = async ({ command, context, content, args }) => {
|
|
36
|
-
* const userName = command.message.metadata?.userName || 'there';
|
|
37
|
-
* return `Hello, ${userName}! How can I help you today?`;
|
|
38
|
-
* };
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @public
|
|
42
|
-
* @since 0.5.6
|
|
43
|
-
*/
|
|
44
|
-
export type textStep<TInboundArgs extends BaseArgs = EmptyArgs, TForwardArgs extends BaseArgs = EmptyArgs, TOutput extends OutputType<A2A.TextPart["text"], TForwardArgs> = OutputType<A2A.TextPart["text"], TForwardArgs>> = Step<A2A.TextPart["text"], TInboundArgs, TForwardArgs, TOutput>;
|
|
45
|
-
/**
|
|
46
|
-
* Type alias for file-based workflow steps.
|
|
47
|
-
*
|
|
48
|
-
* This type represents a step that processes or generates file content
|
|
49
|
-
* within an agent workflow. File steps handle document processing,
|
|
50
|
-
* file generation, and file-based data operations.
|
|
51
|
-
*
|
|
52
|
-
* @template TCommand - The command type, defaults to MessageSendParams
|
|
53
|
-
* @template TPart - The file part type, defaults to A2A.FilePart["file"]
|
|
54
|
-
* @template TInboundArgs - Arguments received from previous step
|
|
55
|
-
* @template TForwardArgs - Arguments passed to next step
|
|
56
|
-
* @template TOutput - The output type of the step
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* const documentStep: fileStep = async ({ command, context, args }) => {
|
|
61
|
-
* const content = generateDocument(command.message.content);
|
|
62
|
-
* return {
|
|
63
|
-
* name: 'report.pdf',
|
|
64
|
-
* mimeType: 'application/pdf',
|
|
65
|
-
* bytes: content
|
|
66
|
-
* };
|
|
67
|
-
* };
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* @public
|
|
71
|
-
* @since 0.5.6
|
|
72
|
-
*/
|
|
73
|
-
export type fileStep<TInboundArgs extends BaseArgs = EmptyArgs, TForwardArgs extends BaseArgs = EmptyArgs, TOutput extends OutputType<A2A.FilePart["file"], TForwardArgs> = OutputType<A2A.FilePart["file"], TForwardArgs>> = Step<A2A.FilePart["file"], TInboundArgs, TForwardArgs, TOutput>;
|
|
74
|
-
/**
|
|
75
|
-
* Type alias for data-based workflow steps.
|
|
76
|
-
*
|
|
77
|
-
* This type represents a step that processes or generates structured data
|
|
78
|
-
* within an agent workflow. Data steps handle JSON processing, API responses,
|
|
79
|
-
* and structured data transformations.
|
|
80
|
-
*
|
|
81
|
-
* @template TCommand - The command type, defaults to MessageSendParams
|
|
82
|
-
* @template TPart - The data part type, defaults to A2A.DataPart["data"]
|
|
83
|
-
* @template TInboundArgs - Arguments received from previous step
|
|
84
|
-
* @template TForwardArgs - Arguments passed to next step
|
|
85
|
-
* @template TOutput - The output type of the step
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* ```typescript
|
|
89
|
-
* const analyzeStep: dataStep = async ({ command, context, args }) => {
|
|
90
|
-
* const analysis = await analyzeMessage(command.message.content);
|
|
91
|
-
* return {
|
|
92
|
-
* sentiment: analysis.sentiment,
|
|
93
|
-
* entities: analysis.entities,
|
|
94
|
-
* confidence: analysis.confidence
|
|
95
|
-
* };
|
|
96
|
-
* };
|
|
97
|
-
* ```
|
|
98
|
-
*
|
|
99
|
-
* @public
|
|
100
|
-
* @since 0.5.6
|
|
101
|
-
*/
|
|
102
|
-
export type dataStep<TInboundArgs extends BaseArgs = EmptyArgs, TForwardArgs extends BaseArgs = EmptyArgs, TOutput extends OutputType<A2A.DataPart["data"], TForwardArgs> = OutputType<A2A.DataPart["data"], TForwardArgs>> = Step<A2A.DataPart["data"], TInboundArgs, TForwardArgs, TOutput>;
|
|
103
|
-
/**
|
|
104
|
-
* Fluent builder for constructing A2A agent execution engines.
|
|
105
|
-
*
|
|
106
|
-
* The EngineBuilder provides a type-safe, fluent API for composing multi-step
|
|
107
|
-
* agent workflows. It supports method chaining to build complex agent behaviors
|
|
108
|
-
* from individual processing steps, with automatic type inference and validation.
|
|
109
|
-
*
|
|
110
|
-
* @template TCommand - The command type, defaults to MessageSendParams
|
|
111
|
-
* @template TInboundArgs - The arguments type received from previous steps
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
* ```typescript
|
|
115
|
-
* const agent = EngineBuilder.create()
|
|
116
|
-
* .text(async ({ command }) => {
|
|
117
|
-
* return `Processing: ${command.message.content}`;
|
|
118
|
-
* })
|
|
119
|
-
* .data(async ({ args }) => {
|
|
120
|
-
* return { processed: true, timestamp: Date.now() };
|
|
121
|
-
* })
|
|
122
|
-
* .text(async ({ args }) => {
|
|
123
|
-
* return `Completed at ${new Date(args[0].timestamp)}`;
|
|
124
|
-
* })
|
|
125
|
-
* .createAgent({ agentCard: myAgentCard });
|
|
126
|
-
* ```
|
|
127
|
-
*
|
|
128
|
-
* @public
|
|
129
|
-
* @since 0.5.6
|
|
130
|
-
*/
|
|
131
|
-
export declare class EngineBuilder<TInboundArgs extends BaseArgs = EmptyArgs> implements StepBuilder<TInboundArgs> {
|
|
132
|
-
private steps;
|
|
133
|
-
/**
|
|
134
|
-
* Protected constructor to enforce factory method usage.
|
|
135
|
-
*
|
|
136
|
-
* @param steps - Initial steps array
|
|
137
|
-
*/
|
|
138
|
-
protected constructor(steps?: Array<StepWithKind<any, any, any, any, any>>);
|
|
139
|
-
/**
|
|
140
|
-
* Creates a new EngineBuilder instance.
|
|
141
|
-
*
|
|
142
|
-
* @template TCommand - The command type for the builder
|
|
143
|
-
* @template TInboundArgs - The initial arguments type
|
|
144
|
-
* @returns A new EngineBuilder instance
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
* ```typescript
|
|
148
|
-
* const builder = EngineBuilder.create<MyCommand, [string, number]>();
|
|
149
|
-
* ```
|
|
150
|
-
*/
|
|
151
|
-
static create<TInboundArgs extends BaseArgs = EmptyArgs>(): EngineBuilder<TInboundArgs>;
|
|
152
|
-
addStep<TPart extends PartContent = A2A.TextPart["text"], TForwardArgs extends BaseArgs = EmptyArgs, TOutput extends OutputType<TPart, TForwardArgs> = OutputType<TPart, TForwardArgs>, TKind extends "text" | "file" | "data" = "text">(step: StepWithKind<TPart, TInboundArgs, TForwardArgs, TOutput, TKind>): EngineBuilder<OutArgsOf<TOutput>>;
|
|
153
|
-
/**
|
|
154
|
-
* Adds a text processing step to the workflow.
|
|
155
|
-
*
|
|
156
|
-
* @template TPart - The text part type
|
|
157
|
-
* @template TForwardArgs - Arguments to forward to next step
|
|
158
|
-
* @template TOutput - The output type of the step
|
|
159
|
-
* @param step - The text step implementation
|
|
160
|
-
* @returns New builder instance with updated type parameters
|
|
161
|
-
*
|
|
162
|
-
* @example
|
|
163
|
-
* ```typescript
|
|
164
|
-
* builder.text(async ({ command, args }) => {
|
|
165
|
-
* return `Hello, ${command.message.content}!`;
|
|
166
|
-
* });
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
text<TForwardArgs extends BaseArgs = EmptyArgs, TOutput extends OutputType<A2A.TextPart["text"], TForwardArgs> = OutputType<A2A.TextPart["text"], TForwardArgs>>(step: textStep<TInboundArgs, TForwardArgs, TOutput> | string): EngineBuilder<OutArgsOf<TOutput>>;
|
|
170
|
-
/**
|
|
171
|
-
* Adds a file processing step to the workflow.
|
|
172
|
-
*
|
|
173
|
-
* @template TPart - The file part type
|
|
174
|
-
* @template TForwardArgs - Arguments to forward to next step
|
|
175
|
-
* @template TOutput - The output type of the step
|
|
176
|
-
* @param step - The file step implementation
|
|
177
|
-
* @returns New builder instance with updated type parameters
|
|
178
|
-
*
|
|
179
|
-
* @example
|
|
180
|
-
* ```typescript
|
|
181
|
-
* builder.file(async ({ command, args }) => {
|
|
182
|
-
* return {
|
|
183
|
-
* name: 'output.txt',
|
|
184
|
-
* mimeType: 'text/plain',
|
|
185
|
-
* bytes: Buffer.from(command.message.content)
|
|
186
|
-
* };
|
|
187
|
-
* });
|
|
188
|
-
* ```
|
|
189
|
-
*/
|
|
190
|
-
file<TForwardArgs extends BaseArgs = EmptyArgs, TOutput extends OutputType<A2A.FilePart["file"], TForwardArgs> = OutputType<A2A.FilePart["file"], TForwardArgs>>(step: fileStep<TInboundArgs, TForwardArgs, TOutput>): EngineBuilder<OutArgsOf<TOutput>>;
|
|
191
|
-
/**
|
|
192
|
-
* Adds a data processing step to the workflow.
|
|
193
|
-
*
|
|
194
|
-
* @template TPart - The data part type
|
|
195
|
-
* @template TForwardArgs - Arguments to forward to next step
|
|
196
|
-
* @template TOutput - The output type of the step
|
|
197
|
-
* @param step - The data step implementation
|
|
198
|
-
* @returns New builder instance with updated type parameters
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
* ```typescript
|
|
202
|
-
* builder.data(async ({ command, args }) => {
|
|
203
|
-
* return {
|
|
204
|
-
* analysis: analyzeText(command.message.content),
|
|
205
|
-
* timestamp: Date.now()
|
|
206
|
-
* };
|
|
207
|
-
* });
|
|
208
|
-
* ```
|
|
209
|
-
*/
|
|
210
|
-
data<TForwardArgs extends BaseArgs = EmptyArgs, TOutput extends OutputType<A2A.DataPart["data"], TForwardArgs> = OutputType<A2A.DataPart["data"], TForwardArgs>>(step: dataStep<TInboundArgs, TForwardArgs, TOutput>): EngineBuilder<OutArgsOf<TOutput>>;
|
|
211
|
-
/**
|
|
212
|
-
* Creates a complete A2A agent using the built workflow.
|
|
213
|
-
*
|
|
214
|
-
* @param params - Agent factory parameters (excluding engine)
|
|
215
|
-
* @returns Configured A2A agent instance
|
|
216
|
-
*
|
|
217
|
-
* @example
|
|
218
|
-
* ```typescript
|
|
219
|
-
* const agent = builder.createAgent({
|
|
220
|
-
* agentCard: {
|
|
221
|
-
* id: 'my-agent',
|
|
222
|
-
* name: 'Assistant Agent',
|
|
223
|
-
* capabilities: ['text-processing']
|
|
224
|
-
* }
|
|
225
|
-
* });
|
|
226
|
-
* ```
|
|
227
|
-
*/
|
|
228
|
-
createAgent(params: Omit<ServiceParams, "engine">): import("../service.js").Service;
|
|
229
|
-
/**
|
|
230
|
-
* Creates an agent execution engine from the built workflow.
|
|
231
|
-
*
|
|
232
|
-
* @returns Agent execution engine function
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* ```typescript
|
|
236
|
-
* const engine = builder.createAgentEngine();
|
|
237
|
-
* // Use engine with service execution
|
|
238
|
-
* ```
|
|
239
|
-
*/
|
|
240
|
-
createAgentEngine(): A2A.Engine;
|
|
241
|
-
/**
|
|
242
|
-
* Builds the step list for the workflow.
|
|
243
|
-
*
|
|
244
|
-
* @returns Array of workflow steps
|
|
245
|
-
* @throws Error if no steps have been added
|
|
246
|
-
*
|
|
247
|
-
* @example
|
|
248
|
-
* ```typescript
|
|
249
|
-
* const steps = builder.build();
|
|
250
|
-
* ```
|
|
251
|
-
*/
|
|
252
|
-
build(): StepWithKind<any, any, any, any, any>[];
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Convenience factory function for creating an agent builder with default parameters.
|
|
256
|
-
*
|
|
257
|
-
* @returns New EngineBuilder instance with MessageSendParams command type
|
|
258
|
-
*
|
|
259
|
-
* @example
|
|
260
|
-
* ```typescript
|
|
261
|
-
* const agent = AgentBuilder()
|
|
262
|
-
* .text(async ({ command }) => `Hello ${command.message.content}!`)
|
|
263
|
-
* .createAgent({ agentCard: myCard });
|
|
264
|
-
* ```
|
|
265
|
-
*
|
|
266
|
-
* @public
|
|
267
|
-
* @since 0.5.6
|
|
268
|
-
*/
|
|
269
|
-
export declare const AgentBuilder: () => EngineBuilder<EmptyArgs>;
|
|
270
|
-
/**
|
|
271
|
-
* Creates an agent execution engine from a list of workflow steps.
|
|
272
|
-
*
|
|
273
|
-
* This function transforms a list of step definitions into an executable
|
|
274
|
-
* agent engine that processes commands through the defined workflow,
|
|
275
|
-
* yielding updates as each step completes.
|
|
276
|
-
*
|
|
277
|
-
* @param stepsList - Array of workflow steps to execute
|
|
278
|
-
* @returns Agent execution engine function
|
|
279
|
-
*
|
|
280
|
-
* @example
|
|
281
|
-
* ```typescript
|
|
282
|
-
* const steps = [
|
|
283
|
-
* { kind: 'text', step: greetingStep },
|
|
284
|
-
* { kind: 'data', step: analysisStep }
|
|
285
|
-
* ];
|
|
286
|
-
* const engine = createAgentExecutor(steps);
|
|
287
|
-
* ```
|
|
288
|
-
*
|
|
289
|
-
* @public
|
|
290
|
-
* @since 0.5.6
|
|
291
|
-
*/
|
|
292
|
-
export declare function createAgentExecutor(stepsList: StepWithKind[]): A2A.Engine;
|
|
@@ -1,370 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2025 The Artinet Project
|
|
3
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @fileoverview A2A Agent Builder and Execution Engine Factory
|
|
7
|
-
*
|
|
8
|
-
* This module provides a fluent builder API for constructing A2A agents and
|
|
9
|
-
* execution engines. It enables declarative definition of multi-step agent
|
|
10
|
-
* workflows with type-safe step composition and automatic execution orchestration.
|
|
11
|
-
*
|
|
12
|
-
* @module A2ABuilder
|
|
13
|
-
* @version 0.6.0-preview
|
|
14
|
-
* @since 0.5.6
|
|
15
|
-
* @author The Artinet Project
|
|
16
|
-
*/
|
|
17
|
-
import { A2A, } from "../../../types/index.js";
|
|
18
|
-
import { createAgent } from "./service.js";
|
|
19
|
-
import { v4 as uuidv4 } from "uuid";
|
|
20
|
-
import { getContent } from "../helpers/content.js";
|
|
21
|
-
import { SUBMITTED_UPDATE, WORKING_UPDATE } from "../../../utils/index.js";
|
|
22
|
-
/**
|
|
23
|
-
* Fluent builder for constructing A2A agent execution engines.
|
|
24
|
-
*
|
|
25
|
-
* The EngineBuilder provides a type-safe, fluent API for composing multi-step
|
|
26
|
-
* agent workflows. It supports method chaining to build complex agent behaviors
|
|
27
|
-
* from individual processing steps, with automatic type inference and validation.
|
|
28
|
-
*
|
|
29
|
-
* @template TCommand - The command type, defaults to MessageSendParams
|
|
30
|
-
* @template TInboundArgs - The arguments type received from previous steps
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```typescript
|
|
34
|
-
* const agent = EngineBuilder.create()
|
|
35
|
-
* .text(async ({ command }) => {
|
|
36
|
-
* return `Processing: ${command.message.content}`;
|
|
37
|
-
* })
|
|
38
|
-
* .data(async ({ args }) => {
|
|
39
|
-
* return { processed: true, timestamp: Date.now() };
|
|
40
|
-
* })
|
|
41
|
-
* .text(async ({ args }) => {
|
|
42
|
-
* return `Completed at ${new Date(args[0].timestamp)}`;
|
|
43
|
-
* })
|
|
44
|
-
* .createAgent({ agentCard: myAgentCard });
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* @public
|
|
48
|
-
* @since 0.5.6
|
|
49
|
-
*/
|
|
50
|
-
export class EngineBuilder {
|
|
51
|
-
//@typescript-eslint/no-explicit-any
|
|
52
|
-
steps = [];
|
|
53
|
-
/**
|
|
54
|
-
* Protected constructor to enforce factory method usage.
|
|
55
|
-
*
|
|
56
|
-
* @param steps - Initial steps array
|
|
57
|
-
*/
|
|
58
|
-
constructor(
|
|
59
|
-
//@typescript-eslint/no-explicit-any
|
|
60
|
-
steps = []) {
|
|
61
|
-
this.steps = steps;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Creates a new EngineBuilder instance.
|
|
65
|
-
*
|
|
66
|
-
* @template TCommand - The command type for the builder
|
|
67
|
-
* @template TInboundArgs - The initial arguments type
|
|
68
|
-
* @returns A new EngineBuilder instance
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```typescript
|
|
72
|
-
* const builder = EngineBuilder.create<MyCommand, [string, number]>();
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
static create() {
|
|
76
|
-
return new EngineBuilder();
|
|
77
|
-
}
|
|
78
|
-
addStep(step) {
|
|
79
|
-
return new EngineBuilder([...this.steps, step]);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Adds a text processing step to the workflow.
|
|
83
|
-
*
|
|
84
|
-
* @template TPart - The text part type
|
|
85
|
-
* @template TForwardArgs - Arguments to forward to next step
|
|
86
|
-
* @template TOutput - The output type of the step
|
|
87
|
-
* @param step - The text step implementation
|
|
88
|
-
* @returns New builder instance with updated type parameters
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```typescript
|
|
92
|
-
* builder.text(async ({ command, args }) => {
|
|
93
|
-
* return `Hello, ${command.message.content}!`;
|
|
94
|
-
* });
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
text(step) {
|
|
98
|
-
return this.addStep({
|
|
99
|
-
step: typeof step === "string" ? () => step : step,
|
|
100
|
-
kind: "text",
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Adds a file processing step to the workflow.
|
|
105
|
-
*
|
|
106
|
-
* @template TPart - The file part type
|
|
107
|
-
* @template TForwardArgs - Arguments to forward to next step
|
|
108
|
-
* @template TOutput - The output type of the step
|
|
109
|
-
* @param step - The file step implementation
|
|
110
|
-
* @returns New builder instance with updated type parameters
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```typescript
|
|
114
|
-
* builder.file(async ({ command, args }) => {
|
|
115
|
-
* return {
|
|
116
|
-
* name: 'output.txt',
|
|
117
|
-
* mimeType: 'text/plain',
|
|
118
|
-
* bytes: Buffer.from(command.message.content)
|
|
119
|
-
* };
|
|
120
|
-
* });
|
|
121
|
-
* ```
|
|
122
|
-
*/
|
|
123
|
-
file(step) {
|
|
124
|
-
return this.addStep({
|
|
125
|
-
step: step,
|
|
126
|
-
kind: "file",
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Adds a data processing step to the workflow.
|
|
131
|
-
*
|
|
132
|
-
* @template TPart - The data part type
|
|
133
|
-
* @template TForwardArgs - Arguments to forward to next step
|
|
134
|
-
* @template TOutput - The output type of the step
|
|
135
|
-
* @param step - The data step implementation
|
|
136
|
-
* @returns New builder instance with updated type parameters
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* ```typescript
|
|
140
|
-
* builder.data(async ({ command, args }) => {
|
|
141
|
-
* return {
|
|
142
|
-
* analysis: analyzeText(command.message.content),
|
|
143
|
-
* timestamp: Date.now()
|
|
144
|
-
* };
|
|
145
|
-
* });
|
|
146
|
-
* ```
|
|
147
|
-
*/
|
|
148
|
-
data(step) {
|
|
149
|
-
return this.addStep({
|
|
150
|
-
step: step,
|
|
151
|
-
kind: "data",
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Creates a complete A2A agent using the built workflow.
|
|
156
|
-
*
|
|
157
|
-
* @param params - Agent factory parameters (excluding engine)
|
|
158
|
-
* @returns Configured A2A agent instance
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* const agent = builder.createAgent({
|
|
163
|
-
* agentCard: {
|
|
164
|
-
* id: 'my-agent',
|
|
165
|
-
* name: 'Assistant Agent',
|
|
166
|
-
* capabilities: ['text-processing']
|
|
167
|
-
* }
|
|
168
|
-
* });
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
createAgent(params) {
|
|
172
|
-
return createAgent({
|
|
173
|
-
...params,
|
|
174
|
-
engine: this.createAgentEngine(),
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Creates an agent execution engine from the built workflow.
|
|
179
|
-
*
|
|
180
|
-
* @returns Agent execution engine function
|
|
181
|
-
*
|
|
182
|
-
* @example
|
|
183
|
-
* ```typescript
|
|
184
|
-
* const engine = builder.createAgentEngine();
|
|
185
|
-
* // Use engine with service execution
|
|
186
|
-
* ```
|
|
187
|
-
*/
|
|
188
|
-
createAgentEngine() {
|
|
189
|
-
return createAgentExecutor(this.build());
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Builds the step list for the workflow.
|
|
193
|
-
*
|
|
194
|
-
* @returns Array of workflow steps
|
|
195
|
-
* @throws Error if no steps have been added
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```typescript
|
|
199
|
-
* const steps = builder.build();
|
|
200
|
-
* ```
|
|
201
|
-
*/
|
|
202
|
-
build() {
|
|
203
|
-
if (this.steps.length === 0) {
|
|
204
|
-
throw new Error("No steps provided");
|
|
205
|
-
}
|
|
206
|
-
return this.steps;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Convenience factory function for creating an agent builder with default parameters.
|
|
211
|
-
*
|
|
212
|
-
* @returns New EngineBuilder instance with MessageSendParams command type
|
|
213
|
-
*
|
|
214
|
-
* @example
|
|
215
|
-
* ```typescript
|
|
216
|
-
* const agent = AgentBuilder()
|
|
217
|
-
* .text(async ({ command }) => `Hello ${command.message.content}!`)
|
|
218
|
-
* .createAgent({ agentCard: myCard });
|
|
219
|
-
* ```
|
|
220
|
-
*
|
|
221
|
-
* @public
|
|
222
|
-
* @since 0.5.6
|
|
223
|
-
*/
|
|
224
|
-
export const AgentBuilder = () => EngineBuilder.create();
|
|
225
|
-
const partToMessagePart = (kind, part) => {
|
|
226
|
-
switch (kind) {
|
|
227
|
-
case "text": {
|
|
228
|
-
return { kind: "text", text: part };
|
|
229
|
-
}
|
|
230
|
-
case "file": {
|
|
231
|
-
const filePart = part;
|
|
232
|
-
if (filePart.uri) {
|
|
233
|
-
return {
|
|
234
|
-
kind: "file",
|
|
235
|
-
file: {
|
|
236
|
-
uri: filePart.uri,
|
|
237
|
-
name: filePart.name,
|
|
238
|
-
mimeType: filePart.mimeType,
|
|
239
|
-
},
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
if (!filePart.bytes) {
|
|
243
|
-
throw new Error("File bytes are required when uri is not provided");
|
|
244
|
-
}
|
|
245
|
-
return {
|
|
246
|
-
kind: "file",
|
|
247
|
-
file: {
|
|
248
|
-
bytes: filePart.bytes,
|
|
249
|
-
name: filePart.name,
|
|
250
|
-
mimeType: filePart.mimeType,
|
|
251
|
-
},
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
case "data": {
|
|
255
|
-
return { kind: "data", data: part };
|
|
256
|
-
}
|
|
257
|
-
default:
|
|
258
|
-
throw new Error("Invalid part kind");
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
/**
|
|
262
|
-
* Creates an agent execution engine from a list of workflow steps.
|
|
263
|
-
*
|
|
264
|
-
* This function transforms a list of step definitions into an executable
|
|
265
|
-
* agent engine that processes commands through the defined workflow,
|
|
266
|
-
* yielding updates as each step completes.
|
|
267
|
-
*
|
|
268
|
-
* @param stepsList - Array of workflow steps to execute
|
|
269
|
-
* @returns Agent execution engine function
|
|
270
|
-
*
|
|
271
|
-
* @example
|
|
272
|
-
* ```typescript
|
|
273
|
-
* const steps = [
|
|
274
|
-
* { kind: 'text', step: greetingStep },
|
|
275
|
-
* { kind: 'data', step: analysisStep }
|
|
276
|
-
* ];
|
|
277
|
-
* const engine = createAgentExecutor(steps);
|
|
278
|
-
* ```
|
|
279
|
-
*
|
|
280
|
-
* @public
|
|
281
|
-
* @since 0.5.6
|
|
282
|
-
*/
|
|
283
|
-
export function createAgentExecutor(stepsList) {
|
|
284
|
-
return async function* (context) {
|
|
285
|
-
const content = getContent(context.userMessage);
|
|
286
|
-
let _skipStep = false;
|
|
287
|
-
const stepArgs = {
|
|
288
|
-
message: context.messages,
|
|
289
|
-
context: context,
|
|
290
|
-
content: content,
|
|
291
|
-
skip: () => {
|
|
292
|
-
_skipStep = true;
|
|
293
|
-
return;
|
|
294
|
-
},
|
|
295
|
-
};
|
|
296
|
-
const contextId = context.contextId;
|
|
297
|
-
const taskId = context.taskId;
|
|
298
|
-
if (!contextId || !taskId) {
|
|
299
|
-
throw new Error("Context ID and task ID are required");
|
|
300
|
-
}
|
|
301
|
-
const taskStarted = SUBMITTED_UPDATE(taskId, contextId);
|
|
302
|
-
yield taskStarted;
|
|
303
|
-
let finalState = A2A.TaskState.completed;
|
|
304
|
-
const finalMessage = {
|
|
305
|
-
taskId: taskId,
|
|
306
|
-
contextId: contextId,
|
|
307
|
-
messageId: uuidv4(),
|
|
308
|
-
kind: "message",
|
|
309
|
-
role: "agent",
|
|
310
|
-
parts: [],
|
|
311
|
-
};
|
|
312
|
-
for (const step of stepsList) {
|
|
313
|
-
if (await context.isCancelled()) {
|
|
314
|
-
finalState = A2A.TaskState.canceled;
|
|
315
|
-
break;
|
|
316
|
-
}
|
|
317
|
-
const ret = await step.step({ ...stepArgs });
|
|
318
|
-
if (_skipStep) {
|
|
319
|
-
_skipStep = false;
|
|
320
|
-
continue;
|
|
321
|
-
}
|
|
322
|
-
let parts = [];
|
|
323
|
-
if (Array.isArray(ret)) {
|
|
324
|
-
parts = ret.map((part) => partToMessagePart(step.kind, part));
|
|
325
|
-
const taskStatusUpdate = WORKING_UPDATE(taskId, contextId, {
|
|
326
|
-
messageId: uuidv4(),
|
|
327
|
-
kind: "message",
|
|
328
|
-
role: "agent",
|
|
329
|
-
parts: parts,
|
|
330
|
-
});
|
|
331
|
-
yield taskStatusUpdate;
|
|
332
|
-
}
|
|
333
|
-
else if (ret !== null && typeof ret === "object" && "parts" in ret) {
|
|
334
|
-
parts = Array.isArray(ret.parts)
|
|
335
|
-
? ret.parts.map((part) => partToMessagePart(step.kind, part))
|
|
336
|
-
: [partToMessagePart(step.kind, ret.parts)];
|
|
337
|
-
const taskStatusUpdate = WORKING_UPDATE(taskId, contextId, {
|
|
338
|
-
messageId: uuidv4(),
|
|
339
|
-
kind: "message",
|
|
340
|
-
role: "agent",
|
|
341
|
-
parts: parts,
|
|
342
|
-
});
|
|
343
|
-
yield taskStatusUpdate;
|
|
344
|
-
stepArgs.args = ret.args;
|
|
345
|
-
}
|
|
346
|
-
else {
|
|
347
|
-
parts = [partToMessagePart(step.kind, ret)];
|
|
348
|
-
const taskStatusUpdate = WORKING_UPDATE(taskId, contextId, {
|
|
349
|
-
messageId: uuidv4(),
|
|
350
|
-
kind: "message",
|
|
351
|
-
role: "agent",
|
|
352
|
-
parts: parts,
|
|
353
|
-
});
|
|
354
|
-
yield taskStatusUpdate;
|
|
355
|
-
}
|
|
356
|
-
finalMessage.parts.push(...parts);
|
|
357
|
-
}
|
|
358
|
-
const task = {
|
|
359
|
-
kind: "task",
|
|
360
|
-
id: taskId,
|
|
361
|
-
contextId: contextId,
|
|
362
|
-
status: {
|
|
363
|
-
state: finalState,
|
|
364
|
-
timestamp: new Date().toISOString(),
|
|
365
|
-
message: finalMessage,
|
|
366
|
-
},
|
|
367
|
-
};
|
|
368
|
-
yield task;
|
|
369
|
-
};
|
|
370
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2025 The Artinet Project
|
|
3
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
*/
|
|
5
|
-
import { A2A } from "../../../types/index.js";
|
|
6
|
-
export declare class AgentCardBuilder {
|
|
7
|
-
agentCard: A2A.AgentCard;
|
|
8
|
-
constructor(agentCard: Partial<A2A.AgentCard> & Required<Pick<A2A.AgentCard, "name">>);
|
|
9
|
-
valueOf(): A2A.AgentCard;
|
|
10
|
-
}
|
|
11
|
-
export declare function createAgentCard(agentCard: A2A.AgentCardParams): A2A.AgentCard;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2025 The Artinet Project
|
|
3
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
*/
|
|
5
|
-
export class AgentCardBuilder {
|
|
6
|
-
agentCard;
|
|
7
|
-
constructor(agentCard) {
|
|
8
|
-
this.agentCard = {
|
|
9
|
-
...agentCard,
|
|
10
|
-
protocolVersion: agentCard.protocolVersion ?? "0.3.0",
|
|
11
|
-
description: agentCard.description ?? "An agent that can use the A2A protocol.",
|
|
12
|
-
url: agentCard.url ?? "https://localhost:3000/a2a",
|
|
13
|
-
version: agentCard.version ?? "0.0.0",
|
|
14
|
-
capabilities: agentCard.capabilities ?? {},
|
|
15
|
-
defaultInputModes: agentCard.defaultInputModes ?? [],
|
|
16
|
-
defaultOutputModes: agentCard.defaultOutputModes ?? [],
|
|
17
|
-
skills: agentCard.skills ?? [],
|
|
18
|
-
preferredTransport: agentCard.preferredTransport ?? "JSONRPC",
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
valueOf() {
|
|
22
|
-
return this.agentCard;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
export function createAgentCard(agentCard) {
|
|
26
|
-
return new AgentCardBuilder(typeof agentCard === "string" ? { name: agentCard } : agentCard).valueOf();
|
|
27
|
-
}
|