@artinet/sdk 0.6.11 → 0.6.12

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.
@@ -85,7 +85,7 @@ class Messenger {
85
85
  interceptors: [...(config?.interceptors ?? []), new HeaderInterceptor(() => this.headers)],
86
86
  },
87
87
  }));
88
- //RAII
88
+ //RAII: to try and maintain backwards compatibility with the old client?
89
89
  this.clientPromise = this.reset(this._baseUrl, this._fallbackPath);
90
90
  }
91
91
  async reset(baseUrl = this._baseUrl, fallbackPath = this._fallbackPath) {
@@ -1,7 +1,9 @@
1
1
  /**
2
- * Copyright 2025 The Artinet Project
3
- * SPDX-License-Identifier: Apache-2.0
2
+ * Sanitizes a string by escaping HTML characters to prevent XSS attacks.
3
+ * @param str - The string to sanitize.
4
+ * @returns The sanitized string.
4
5
  */
6
+ export declare function sanitizeString(str: string): string;
5
7
  /**
6
8
  * Generates a timestamp in ISO 8601 format.
7
9
  * @returns The current timestamp as a string.
@@ -2,6 +2,16 @@
2
2
  * Copyright 2025 The Artinet Project
3
3
  * SPDX-License-Identifier: Apache-2.0
4
4
  */
5
+ import escapeHtml from 'escape-html';
6
+ import { Buffer } from 'node:buffer';
7
+ /**
8
+ * Sanitizes a string by escaping HTML characters to prevent XSS attacks.
9
+ * @param str - The string to sanitize.
10
+ * @returns The sanitized string.
11
+ */
12
+ export function sanitizeString(str) {
13
+ return escapeHtml(str).trim();
14
+ }
5
15
  /**
6
16
  * Generates a timestamp in ISO 8601 format.
7
17
  * @returns The current timestamp as a string.
@@ -28,18 +28,16 @@
28
28
  * ```
29
29
  *
30
30
  * @module A2ABuilder
31
- * @version 0.6.0-preview
31
+ * @version 0.6
32
32
  * @since 0.5.6
33
33
  * @author The Artinet Project
34
34
  */
35
35
  import { A2A } from "../types/index.js";
36
- import * as A from "./agent-builder.js";
37
- import { ServiceParams } from "../services/a2a/factory/service.js";
38
- import { describe } from "./index.js";
39
- import { Service } from "../services/a2a/service.js";
40
- import { MessageParams } from "./message-builder.js";
41
- import { StatusUpdateParams, ArtifactUpdateParams, TaskParams } from "./task-builder.js";
42
- import { ServerParams } from "../server/express/server.js";
36
+ import * as A from './agent-builder.js';
37
+ import { ServiceParams } from '../services/a2a/factory/service.js';
38
+ import { MessageParams } from './message-builder.js';
39
+ import { StatusUpdateParams, ArtifactUpdateParams, TaskParams } from './task-builder.js';
40
+ import { ServerParams as BaseServerParams } from "../server/params.js";
43
41
  export interface MessageSender {
44
42
  sendMessage(params: A2A.MessageSendParams): Promise<A2A.SendMessageSuccessResult>;
45
43
  }
@@ -73,7 +71,7 @@ export interface MessageSender {
73
71
  * @public
74
72
  * @since 0.5.6
75
73
  */
76
- export type textStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.empty> = A.Step<A2A.TextPart["text"], Input, Carry>;
74
+ export type textStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.empty> = A.Step<A2A.TextPart['text'], Input, Carry>;
77
75
  /**
78
76
  * Type alias for file-based workflow steps.
79
77
  *
@@ -112,7 +110,7 @@ export type textStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.
112
110
  * @public
113
111
  * @since 0.5.6
114
112
  */
115
- export type fileStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.empty> = A.Step<A2A.FilePart["file"], Input, Carry>;
113
+ export type fileStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.empty> = A.Step<A2A.FilePart['file'], Input, Carry>;
116
114
  /**
117
115
  * Type alias for data-based workflow steps.
118
116
  *
@@ -147,7 +145,7 @@ export type fileStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.
147
145
  * @public
148
146
  * @since 0.5.6
149
147
  */
150
- export type dataStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.empty> = A.Step<A2A.DataPart["data"], Input, Carry>;
148
+ export type dataStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.empty> = A.Step<A2A.DataPart['data'], Input, Carry>;
151
149
  /**
152
150
  * Type alias for message-based workflow steps.
153
151
  *
@@ -292,435 +290,7 @@ export type statusStep<Input extends A.bargs = A.empty, Carry extends A.bargs =
292
290
  * @since 0.6.0
293
291
  */
294
292
  export type taskStep<Input extends A.bargs = A.empty, Carry extends A.bargs = A.empty> = A.Step<A.Stateless<TaskParams>, Input, Carry>;
295
- export type FactoryParams = Omit<ServiceParams, "engine" | "agentCard"> & Omit<ServerParams, "agent">;
296
- /**
297
- * Fluent builder for constructing A2A agent execution engines.
298
- *
299
- * AgentFactory provides a type-safe, fluent API for composing multi-step
300
- * agent workflows. It supports method chaining to build complex agent behaviors
301
- * from individual processing steps, with automatic type inference for carried
302
- * arguments between steps.
303
- *
304
- * @template I - The arguments type received from previous steps (inferred automatically)
305
- *
306
- * @example
307
- * ```typescript
308
- * // Basic agent with text steps
309
- * const agent = cr8("MyAgent")
310
- * .text(({ content }) => `You said: ${content}`)
311
- * .agent;
312
- *
313
- * // Agent with carried args between steps
314
- * const agent = cr8("AnalysisAgent")
315
- * .text(({ content }) => ({
316
- * reply: `Analyzing: ${content}`,
317
- * args: { originalContent: content }
318
- * }))
319
- * .data(({ args }) => ({
320
- * wordCount: args?.originalContent?.split(' ').length,
321
- * timestamp: Date.now()
322
- * }))
323
- * .text(({ args }) => `Analysis complete: ${args?.wordCount} words`)
324
- * .agent;
325
- *
326
- * // Agent-to-agent orchestration
327
- * const orchestrator = cr8("Orchestrator")
328
- * .text("Starting multi-agent workflow...")
329
- * .sendMessage({ agent: otherAgent, message: "Process this" })
330
- * .text(({ args }) => `Got result: ${args?.task?.status.state}`)
331
- * .agent;
332
- * ```
333
- *
334
- * @public
335
- * @since 0.5.6
336
- */
337
- export declare class AgentFactory<I extends A.bargs = A.empty> implements A.AgentBuilder<I> {
338
- private readonly _agentCard;
339
- private readonly _params?;
340
- private readonly _steps;
341
- /**
342
- * Protected constructor to enforce factory method usage.
343
- * @param agentCard - The agent card to use
344
- * @param params - The parameters to use
345
- * @param steps - Initial steps array
346
- */
347
- protected constructor(_agentCard: A2A.AgentCard, _params?: FactoryParams | undefined, _steps?: Array<A.Resolved<any, any, any, any, any>>);
348
- /**
349
- * Builds the step list for the workflow.
350
- *
351
- * @returns Array of workflow steps
352
- * @throws Error if no steps have been added
353
- *
354
- * @example
355
- * ```typescript
356
- * const steps = cr8.steps;
357
- * ```
358
- */
359
- get steps(): Array<A.Resolved<any, any, any, any, any>>;
360
- /**
361
- * The {@link A2A.AgentCard} to use
362
- * @returns The {@link A2A.AgentCard}
363
- */
364
- get agentCard(): A2A.AgentCard;
365
- /**
366
- * The {@link FactoryParams} to use
367
- * @returns The {@link FactoryParams}
368
- */
369
- get params(): FactoryParams | undefined;
370
- /**
371
- * Creates an agent execution engine from the built workflow.
372
- *
373
- * @returns The {@link A2A.Engine}
374
- *
375
- * @example
376
- * ```typescript
377
- * const engine = builder.engine;
378
- * // Use engine with service execution
379
- * ```
380
- */
381
- get engine(): A2A.Engine;
382
- /**
383
- * Creates a complete A2A agent using the built workflow.
384
- *
385
- * @param params - The {@link ServiceParams} to use
386
- * @returns The {@link Service}
387
- *
388
- * @example
389
- * ```typescript
390
- * const agent = cr8({
391
- * id: 'my-agent',
392
- * name: 'Assistant Agent',
393
- * capabilities: ['text-processing']
394
- * }).agent;
395
- * ```
396
- */
397
- get agent(): Service;
398
- get server(): {
399
- app: import("express").Express;
400
- agent: Service;
401
- start: (_port?: number) => import("node:http").Server<typeof import("node:http").IncomingMessage, typeof import("node:http").ServerResponse>;
402
- };
403
- from(engine?: A2A.Engine): Service;
404
- serve(engine?: A2A.Engine): {
405
- app: import("express").Express;
406
- agent: Service;
407
- start: (_port?: number) => import("node:http").Server<typeof import("node:http").IncomingMessage, typeof import("node:http").ServerResponse>;
408
- };
409
- addStep<Ret extends A.AcceptedReturnValues = A.text, C extends A.bargs = A.empty, R extends A.rep<Ret, C> = A.rep<Ret, C>, Kind extends A.AcceptedKinds = "text">(step: A.Resolved<Ret, I, C, R, Kind>): AgentFactory<A.inferCarry<R>>;
410
- /**
411
- * Adds a text processing step to the workflow.
412
- *
413
- * Text steps are the most common step type, producing text content that
414
- * becomes a TextPart in the agent's response message.
415
- *
416
- * @param step - A text step function or static string value
417
- * @returns New builder instance with updated type parameters
418
- *
419
- * @example
420
- * ```typescript
421
- * // Static text
422
- * builder.text("Hello, world!")
423
- *
424
- * // Dynamic text from content
425
- * builder.text(({ content }) => `You said: ${content}`)
426
- *
427
- * // With carried args
428
- * builder.text(({ args }) => ({
429
- * reply: `Processing ${args?.itemCount} items`,
430
- * args: { processedAt: Date.now() }
431
- * }))
432
- * ```
433
- */
434
- text<C extends A.bargs = A.empty>(text: A.text): AgentFactory<A.inC<A.rep<A.text, C>>>;
435
- text<C extends A.bargs = A.empty>(step: textStep<I, C>): AgentFactory<A.inC<A.rep<A.text, C>>>;
436
- /**
437
- * Adds a file processing step to the workflow.
438
- *
439
- * File steps produce file content that becomes a FilePart in the agent's
440
- * response. Files can be specified by URI or inline bytes/base64 content.
441
- *
442
- * @param step - A file step function or static file object
443
- * @returns New builder instance with updated type parameters
444
- *
445
- * @example
446
- * ```typescript
447
- * // Static file by URI
448
- * builder.file({ uri: "https://example.com/doc.pdf" })
449
- *
450
- * // Dynamic file generation
451
- * builder.file(async ({ args }) => ({
452
- * name: 'report.pdf',
453
- * mimeType: 'application/pdf',
454
- * bytes: await generatePDF(args?.data)
455
- * }))
456
- *
457
- * // Multiple files
458
- * builder.file(() => [
459
- * { uri: "https://example.com/file1.pdf" },
460
- * { uri: "https://example.com/file2.pdf" }
461
- * ])
462
- * ```
463
- */
464
- file<C extends A.bargs = A.empty, R extends A.rep<A.file, C> = A.rep<A.file, C>>(file: A.file): AgentFactory<A.inC<A.rep<A.file, C>>>;
465
- file<C extends A.bargs = A.empty, R extends A.rep<A.file, C> = A.rep<A.file, C>>(step: fileStep<I, C>): AgentFactory<A.inC<A.rep<A.file, C>>>;
466
- /**
467
- * Adds a data processing step to the workflow.
468
- *
469
- * Data steps produce structured JSON data that becomes a DataPart in the
470
- * agent's response. Useful for returning complex objects, API responses,
471
- * or any structured data.
472
- *
473
- * @param step - A data step function or static data object
474
- * @returns New builder instance with updated type parameters
475
- *
476
- * @example
477
- * ```typescript
478
- * // Static data
479
- * builder.data({ status: "ok", version: "1.0.0" })
480
- *
481
- * // Dynamic data
482
- * builder.data(async ({ content }) => ({
483
- * analysis: await analyzeText(content),
484
- * timestamp: Date.now()
485
- * }))
486
- *
487
- * // With carried args
488
- * builder.data(({ args }) => ({
489
- * reply: { result: args?.computedValue * 2 },
490
- * args: { doubled: true }
491
- * }))
492
- * ```
493
- */
494
- data<C extends A.bargs = A.empty>(data: A.data): AgentFactory<A.inC<A.rep<A.data, C>>>;
495
- data<C extends A.bargs = A.empty>(step: dataStep<I, C>): AgentFactory<A.inC<A.rep<A.data, C>>>;
496
- /**
497
- * Adds a message step to the workflow.
498
- *
499
- * Message steps yield complete A2A messages with full control over role,
500
- * parts, and metadata. Use when you need to construct complex multi-part
501
- * messages or control the message structure directly.
502
- *
503
- * @param step - A message step function or static message/string
504
- * @returns New builder instance with updated type parameters
505
- *
506
- * @example
507
- * ```typescript
508
- * // Simple string message
509
- * builder.message("Hello from the agent!")
510
- *
511
- * // Full message with parts
512
- * builder.message(({ context }) => ({
513
- * role: "agent",
514
- * parts: [
515
- * { kind: "text", text: "Here are your files:" },
516
- * { kind: "file", file: { uri: "https://example.com/doc.pdf" } }
517
- * ]
518
- * }))
519
- *
520
- * // Using describe helper
521
- * builder.message(({ args }) => describe.message({
522
- * role: "agent",
523
- * parts: [{ kind: "text", text: args?.greeting }]
524
- * }))
525
- * ```
526
- */
527
- message<C extends A.bargs = A.empty>(message: A.sMessage): AgentFactory<A.inC<A.rep<A.sMessage, C>>>;
528
- message<C extends A.bargs = A.empty>(step: messageStep<I, C>): AgentFactory<A.inC<A.rep<A.sMessage, C>>>;
529
- /**
530
- * Adds an artifact step to the workflow.
531
- *
532
- * Artifact steps create persistent, versioned outputs that can be referenced
533
- * across task sessions. Use for documents, generated files, or content that
534
- * clients may need to retrieve later.
535
- *
536
- * @param step - An artifact step function or static artifact object
537
- * @returns New builder instance with updated type parameters
538
- *
539
- * @example
540
- * ```typescript
541
- * // Static artifact
542
- * builder.artifact(describe.artifact({
543
- * artifactId: "report-001",
544
- * parts: [{ kind: "text", text: "Report content" }]
545
- * }))
546
- *
547
- * // Dynamic artifact
548
- * builder.artifact(async ({ context, args }) => ({
549
- * artifactId: `analysis-${context.taskId}`,
550
- * name: "Analysis Results",
551
- * parts: [{ kind: "data", data: args?.analysisData }]
552
- * }))
553
- * ```
554
- */
555
- artifact<C extends A.bargs = A.empty>(step: artifactStep<I, C>): AgentFactory<A.inC<A.rep<A.sArtifact, C>>>;
556
- artifact<C extends A.bargs = A.empty>(artifact: A.sArtifact): AgentFactory<A.inC<A.rep<A.sArtifact, C>>>;
557
- /**
558
- * Adds a status update step to the workflow.
559
- *
560
- * Status steps emit task state updates during execution. Use to communicate
561
- * progress, intermediate states, or completion to clients. Supports simple
562
- * state strings or full status objects with messages.
563
- *
564
- * @param step - A status step function, status object, or state string
565
- * @returns New builder instance with updated type parameters
566
- *
567
- * @example
568
- * ```typescript
569
- * // Simple state string
570
- * builder.status("working")
571
- *
572
- * // Status with message
573
- * builder.status(({ args }) => ({
574
- * status: {
575
- * state: A2A.TaskState.working,
576
- * message: describe.message(`Step ${args?.step} of 5 complete`)
577
- * }
578
- * }))
579
- *
580
- * // Mark completion
581
- * builder.status(() => ({
582
- * status: { state: A2A.TaskState.completed }
583
- * }))
584
- * ```
585
- */
586
- status<C extends A.bargs = A.empty>(status: A.sUpdate): AgentFactory<A.inC<A.rep<A.sUpdate, C>>>;
587
- status<C extends A.bargs = A.empty>(step: statusStep<I, C>): AgentFactory<A.inC<A.rep<A.sUpdate, C>>>;
588
- /**
589
- * Adds a task step to the workflow.
590
- *
591
- * Task steps yield complete A2A task objects. Use when you need full control
592
- * over the task representation, including status, artifacts, and history.
593
- * Particularly useful for orchestration scenarios or final task construction.
594
- *
595
- * @param step - A task step function, task object, or string
596
- * @returns New builder instance with updated type parameters
597
- *
598
- * @example
599
- * ```typescript
600
- * // Simple string (auto-converted to task)
601
- * builder.task("Operation completed")
602
- *
603
- * // Full task object
604
- * builder.task(({ context }) => describe.task({
605
- * id: context.taskId,
606
- * contextId: context.contextId,
607
- * status: { state: A2A.TaskState.completed }
608
- * }))
609
- *
610
- * // With carried args
611
- * builder.task(({ context }) => ({
612
- * reply: describe.task({ id: context.taskId }),
613
- * args: { completedAt: Date.now() }
614
- * }))
615
- * ```
616
- */
617
- task<C extends A.bargs = A.empty>(task: A.sTask): AgentFactory<A.inC<A.rep<A.sTask, C>>>;
618
- task<C extends A.bargs = A.empty>(step: taskStep<I, C>): AgentFactory<A.inC<A.rep<A.sTask, C>>>;
619
- /**
620
- * Adds an agent-to-agent orchestration step to the workflow.
621
- *
622
- * This step sends a message to another agent (local Service or remote A2A Server)
623
- * and yields the response as a task. Enables multi-agent workflows where one
624
- * agent delegates work to others.
625
- *
626
- * **Note:** This is currently a blocking call. Streaming responses are not
627
- * yet supported in orchestration steps.
628
- * @note Args passed from the previous step are inserted, by default,
629
- * (`unshift`) as `DataPart`s onto the forwarded `Message`.`Parts`.
630
- *
631
- * @param agent - The target agent (Agent or AgentMessenger)
632
- * @param message - Message to send (defaults to context.userMessage)
633
- * @returns New builder instance with task carry args (args.task)
634
- *
635
- * @example
636
- * ```typescript
637
- * // Delegate to another agent
638
- * const orchestrator = cr8("Orchestrator")
639
- * .text("Starting workflow...")
640
- * .sendMessage({ agent: analysisAgent, message: "Analyze this data" })
641
- * .text(({ args }) => `Analysis result: ${args?.task?.status.state}`)
642
- * .agent;
643
- *
644
- * // Chain multiple agents
645
- * const pipeline = cr8("Pipeline")
646
- * .sendMessage({ agent: preprocessor })
647
- * .sendMessage({ agent: analyzer })
648
- * .sendMessage({ agent: postprocessor })
649
- * .text(({ args }) => `Final result: ${args?.task?.status.message}`)
650
- * .agent;
651
- *
652
- * // Forward user's message to another agent
653
- * const proxy = cr8("Proxy")
654
- * .sendMessage({ agent: targetAgent }) // uses context.userMessage
655
- * .agent;
656
- * ```
657
- */
658
- sendMessage<Carry extends A.BaseArgs = {
659
- task?: A2A.Task;
660
- }>(agent_and_message: {
661
- agent: MessageSender;
662
- message?: A.sMessage | string;
663
- }): AgentFactory<A.inferCarry<A.Reply<A.Stateless<TaskParams>, Carry>>>;
664
- /**
665
- * Creates a new AgentFactory instance.
666
- *
667
- * @template Input - The initial arguments type
668
- * @returns A new AgentFactory instance
669
- *
670
- * @example
671
- * ```typescript
672
- * const factory = AgentFactory.create(myCard, { params });
673
- * ```
674
- */
675
- static create(agentCard: describe.AgentCardParams, params?: FactoryParams): AgentFactory<A.EmptyArgs>;
676
- }
677
- /**
678
- * Creates a new AgentFactory instance for building agent workflows.
679
- *
680
- * This is the primary entry point for the fluent builder API. Accepts an
681
- * agent card (or name string) and optional factory parameters.
682
- *
683
- * @param agentCard - Agent card object or name string
684
- * @param params - Optional factory parameters (basePath, port, etc.)
685
- * @returns New AgentFactory instance
686
- *
687
- * @example
688
- * ```typescript
689
- * // Simple agent with name string
690
- * const agent = cr8("MyAgent")
691
- * .text(({ content }) => `Echo: ${content}`)
692
- * .agent;
693
- *
694
- * // Agent with full card and params
695
- * const agent = cr8(myAgentCard, { basePath: "/api" })
696
- * .text("Hello!")
697
- * .data(({ content }) => analyzeContent(content))
698
- * .agent;
699
- *
700
- * // Get the engine directly
701
- * const engine = cr8("Processor")
702
- * .text("Processing...")
703
- * .engine;
704
- *
705
- * // Create and start server
706
- * const server = cr8("ServerAgent", { port: 3000 })
707
- * .text("Ready to serve!")
708
- * .server.start();
709
- * ```
710
- *
711
- * @public
712
- * @since 0.6.0
713
- */
714
- export declare const cr8: typeof AgentFactory.create;
715
- /**
716
- * @deprecated Use {@link cr8} instead
717
- * @note This export exists only to alert users that `AgentBuilder` is deprecated.
718
- * `AgentBuilder` no longer comes with the `createAgent` method.
719
- * @since 0.6.0
720
- */
721
- export declare class AgentBuilder extends AgentFactory {
722
- constructor(agentCard?: describe.AgentCardParams | string, params?: FactoryParams);
723
- }
293
+ export type FactoryParams<ServerParams extends BaseServerParams = BaseServerParams> = Omit<ServiceParams, 'engine' | 'agentCard'> & Omit<ServerParams, 'agent'>;
724
294
  /**
725
295
  * Creates an agent execution engine from a list of workflow steps.
726
296
  *