@artinet/sdk 0.5.16 → 0.5.18
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 +108 -58
- package/dist/browser/browser.d.ts +9 -0
- package/dist/browser/browser.js +10 -0
- package/dist/browser/client/a2a-client.d.ts +126 -0
- package/dist/browser/client/a2a-client.js +221 -0
- package/dist/browser/client/index.d.ts +1 -0
- package/dist/browser/client/index.js +1 -0
- package/dist/browser/services/a2a/helpers/message-builder.d.ts +12 -0
- package/dist/browser/services/a2a/helpers/message-builder.js +61 -0
- package/dist/browser/transport/rpc/parser.d.ts +15 -0
- package/dist/browser/transport/rpc/parser.js +48 -0
- package/dist/browser/transport/rpc/rpc-client.d.ts +80 -0
- package/dist/browser/transport/rpc/rpc-client.js +189 -0
- package/dist/browser/transport/streaming/event-stream.d.ts +25 -0
- package/dist/browser/transport/streaming/event-stream.js +99 -0
- package/dist/browser/types/ext.d.ts +13 -0
- package/dist/browser/types/ext.js +10 -0
- package/dist/browser/types/index.d.ts +4 -0
- package/dist/browser/types/index.js +4 -0
- package/dist/browser/types/interfaces/client.d.ts +135 -0
- package/dist/browser/types/interfaces/client.js +5 -0
- package/dist/browser/types/interfaces/index.d.ts +3 -0
- package/dist/browser/types/interfaces/index.js +3 -0
- package/dist/browser/types/interfaces/services/a2a/builder.d.ts +37 -0
- package/dist/browser/types/interfaces/services/a2a/builder.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/context.d.ts +162 -0
- package/dist/browser/types/interfaces/services/a2a/context.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/engine.d.ts +7 -0
- package/dist/browser/types/interfaces/services/a2a/engine.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/index.d.ts +5 -0
- package/dist/browser/types/interfaces/services/a2a/index.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/legacy.d.ts +93 -0
- package/dist/browser/types/interfaces/services/a2a/legacy.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/service.d.ts +413 -0
- package/dist/browser/types/interfaces/services/a2a/service.js +5 -0
- package/dist/browser/types/interfaces/services/core/context/command.d.ts +25 -0
- package/dist/browser/types/interfaces/services/core/context/command.js +5 -0
- package/dist/browser/types/interfaces/services/core/context/context.d.ts +207 -0
- package/dist/browser/types/interfaces/services/core/context/context.js +5 -0
- package/dist/browser/types/interfaces/services/core/context/index.d.ts +3 -0
- package/dist/browser/types/interfaces/services/core/context/index.js +3 -0
- package/dist/browser/types/interfaces/services/core/context/types.d.ts +11 -0
- package/dist/browser/types/interfaces/services/core/context/types.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/engine.d.ts +106 -0
- package/dist/browser/types/interfaces/services/core/execution/engine.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/environment.d.ts +11 -0
- package/dist/browser/types/interfaces/services/core/execution/environment.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/execute.d.ts +7 -0
- package/dist/browser/types/interfaces/services/core/execution/execute.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/index.d.ts +3 -0
- package/dist/browser/types/interfaces/services/core/execution/index.js +3 -0
- package/dist/browser/types/interfaces/services/core/index.d.ts +4 -0
- package/dist/browser/types/interfaces/services/core/index.js +4 -0
- package/dist/browser/types/interfaces/services/core/managers/cancellation.d.ts +9 -0
- package/dist/browser/types/interfaces/services/core/managers/cancellation.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/connection.d.ts +9 -0
- package/dist/browser/types/interfaces/services/core/managers/connection.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/context.d.ts +17 -0
- package/dist/browser/types/interfaces/services/core/managers/context.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/event.d.ts +328 -0
- package/dist/browser/types/interfaces/services/core/managers/event.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/index.d.ts +6 -0
- package/dist/browser/types/interfaces/services/core/managers/index.js +6 -0
- package/dist/browser/types/interfaces/services/core/managers/stream.d.ts +217 -0
- package/dist/browser/types/interfaces/services/core/managers/stream.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/task.d.ts +9 -0
- package/dist/browser/types/interfaces/services/core/managers/task.js +1 -0
- package/dist/browser/types/interfaces/services/core/service.d.ts +115 -0
- package/dist/browser/types/interfaces/services/core/service.js +5 -0
- package/dist/browser/types/interfaces/services/index.d.ts +4 -0
- package/dist/browser/types/interfaces/services/index.js +4 -0
- package/dist/browser/types/interfaces/services/mcp/index.d.ts +1 -0
- package/dist/browser/types/interfaces/services/mcp/index.js +1 -0
- package/dist/browser/types/interfaces/services/mcp/service.d.ts +49 -0
- package/dist/browser/types/interfaces/services/mcp/service.js +5 -0
- package/dist/browser/types/interfaces/services/protocol.d.ts +33 -0
- package/dist/browser/types/interfaces/services/protocol.js +34 -0
- package/dist/browser/types/interfaces/storage.d.ts +8 -0
- package/dist/browser/types/interfaces/storage.js +5 -0
- package/dist/browser/types/schemas/a2a/agent.d.ts +2583 -0
- package/dist/browser/types/schemas/a2a/agent.js +323 -0
- package/dist/browser/types/schemas/a2a/auth.d.ts +908 -0
- package/dist/browser/types/schemas/a2a/auth.js +283 -0
- package/dist/browser/types/schemas/a2a/error.d.ts +396 -0
- package/dist/browser/types/schemas/a2a/error.js +163 -0
- package/dist/browser/types/schemas/a2a/index.d.ts +11 -0
- package/dist/browser/types/schemas/a2a/index.js +11 -0
- package/dist/browser/types/schemas/a2a/kind.d.ts +11 -0
- package/dist/browser/types/schemas/a2a/kind.js +20 -0
- package/dist/browser/types/schemas/a2a/message.d.ts +10343 -0
- package/dist/browser/types/schemas/a2a/message.js +130 -0
- package/dist/browser/types/schemas/a2a/notification.d.ts +1517 -0
- package/dist/browser/types/schemas/a2a/notification.js +203 -0
- package/dist/browser/types/schemas/a2a/parameters.d.ts +956 -0
- package/dist/browser/types/schemas/a2a/parameters.js +241 -0
- package/dist/browser/types/schemas/a2a/protocol.d.ts +14363 -0
- package/dist/browser/types/schemas/a2a/protocol.js +59 -0
- package/dist/browser/types/schemas/a2a/rpc.d.ts +182 -0
- package/dist/browser/types/schemas/a2a/rpc.js +126 -0
- package/dist/browser/types/schemas/a2a/task.d.ts +5886 -0
- package/dist/browser/types/schemas/a2a/task.js +134 -0
- package/dist/browser/types/schemas/a2a/transport.d.ts +31 -0
- package/dist/browser/types/schemas/a2a/transport.js +28 -0
- package/dist/browser/types/schemas/index.d.ts +1 -0
- package/dist/browser/types/schemas/index.js +1 -0
- package/dist/browser/types/utils/index.d.ts +1 -0
- package/dist/browser/types/utils/index.js +1 -0
- package/dist/browser/types/utils/transform.d.ts +64 -0
- package/dist/browser/types/utils/transform.js +35 -0
- package/dist/browser/utils/common/constants.d.ts +11 -0
- package/dist/browser/utils/common/constants.js +38 -0
- package/dist/browser/utils/common/errors.d.ts +24 -0
- package/dist/browser/utils/common/errors.js +42 -0
- package/dist/browser/utils/common/utils.d.ts +9 -0
- package/dist/browser/utils/common/utils.js +11 -0
- package/dist/browser/utils/logging/index.d.ts +2 -0
- package/dist/browser/utils/logging/index.js +2 -0
- package/dist/browser/utils/logging/log.d.ts +33 -0
- package/dist/browser/utils/logging/log.js +75 -0
- package/dist/browser/utils/logging/logger.d.ts +18 -0
- package/dist/browser/utils/logging/logger.js +18 -0
- package/dist/client/a2a-client.d.ts +2 -1
- package/dist/client/a2a-client.js +13 -4
- package/dist/server/express/errors.js +1 -1
- package/dist/server/express/middeware.d.ts +2 -2
- package/dist/server/express/middeware.js +26 -6
- package/dist/server/express/server.d.ts +26 -25
- package/dist/server/express/server.js +32 -6
- package/dist/services/a2a/factory/builder.d.ts +24 -24
- package/dist/services/a2a/factory/builder.js +6 -1
- package/dist/services/a2a/factory/service.js +2 -1
- package/dist/services/a2a/helpers/agentcard-builder.d.ts +7 -0
- package/dist/services/a2a/helpers/agentcard-builder.js +23 -0
- package/dist/services/a2a/helpers/history.d.ts +2 -0
- package/dist/services/a2a/helpers/history.js +3 -0
- package/dist/services/a2a/helpers/index.d.ts +3 -0
- package/dist/services/a2a/helpers/index.js +3 -0
- package/dist/services/a2a/methods/get-task.d.ts +46 -46
- package/dist/services/a2a/methods/get-task.js +2 -0
- package/dist/services/a2a/methods/send-message.js +18 -1
- package/dist/services/a2a/service.d.ts +154 -153
- package/dist/services/a2a/service.js +20 -6
- package/dist/services/mcp/service.js +0 -1
- package/dist/transport/rpc/parser.js +2 -2
- package/dist/transport/rpc/rpc-client.js +2 -2
- package/dist/transport/trpc/a2a/factory/router.d.ts +1084 -1084
- package/dist/transport/trpc/a2a/routes/info.d.ts +36 -36
- package/dist/transport/trpc/a2a/routes/message/route.d.ts +225 -225
- package/dist/transport/trpc/a2a/routes/tasks/route.d.ts +257 -257
- package/dist/transport/trpc/a2a/trpc.d.ts +120 -120
- package/dist/types/interfaces/services/a2a/service.d.ts +6 -1
- package/dist/types/schemas/a2a/agent.d.ts +818 -818
- package/dist/types/schemas/a2a/agent.js +1 -23
- package/dist/types/schemas/a2a/auth.d.ts +197 -197
- package/dist/types/schemas/a2a/auth.js +4 -19
- package/dist/types/schemas/a2a/error.d.ts +24 -24
- package/dist/types/schemas/a2a/message.d.ts +4499 -4499
- package/dist/types/schemas/a2a/message.js +2 -10
- package/dist/types/schemas/a2a/notification.d.ts +403 -403
- package/dist/types/schemas/a2a/notification.js +3 -7
- package/dist/types/schemas/a2a/parameters.d.ts +264 -264
- package/dist/types/schemas/a2a/parameters.js +1 -14
- package/dist/types/schemas/a2a/protocol.d.ts +5988 -5988
- package/dist/types/schemas/a2a/rpc.d.ts +20 -20
- package/dist/types/schemas/a2a/rpc.js +0 -5
- package/dist/types/schemas/a2a/task.d.ts +2513 -2513
- package/dist/types/schemas/a2a/task.js +10 -11
- package/dist/utils/common/constants.js +1 -1
- package/dist/utils/common/errors.d.ts +2 -1
- package/dist/utils/common/errors.js +2 -1
- package/dist/utils/common/schema-validation.d.ts +2 -0
- package/dist/utils/common/schema-validation.js +12 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/package.json +20 -17
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025 The Artinet Project
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @fileoverview Core Execution Engine Type Definition
|
|
7
|
+
*
|
|
8
|
+
* This module defines the fundamental execution engine type that serves as the
|
|
9
|
+
* foundation for all command processing in the core service framework. Execution
|
|
10
|
+
* engines are responsible for transforming commands into streams of updates.
|
|
11
|
+
*
|
|
12
|
+
* @module CoreExecutionEngine
|
|
13
|
+
* @version 0.5.7
|
|
14
|
+
* @since 0.5.6
|
|
15
|
+
* @author The Artinet Project
|
|
16
|
+
*/
|
|
17
|
+
import { CoreCommand, CoreContext, CoreState, CoreUpdate } from "../context/index.js";
|
|
18
|
+
/**
|
|
19
|
+
* Core execution engine function signature.
|
|
20
|
+
*
|
|
21
|
+
* An ExecutionEngine is a function that accepts a CoreContext and returns an
|
|
22
|
+
* AsyncGenerator yielding updates. This pattern enables streaming execution
|
|
23
|
+
* where commands can produce multiple updates over time, supporting both
|
|
24
|
+
* real-time processing and long-running operations.
|
|
25
|
+
*
|
|
26
|
+
* The engine receives a complete execution context containing the command to
|
|
27
|
+
* execute, current state, event management, and cancellation signals. It
|
|
28
|
+
* processes the command and yields updates as execution progresses, allowing
|
|
29
|
+
* consumers to receive real-time feedback about execution status.
|
|
30
|
+
*
|
|
31
|
+
* @template TCommand - The command type, must extend CoreCommand
|
|
32
|
+
* @template TState - The state type, must extend CoreState
|
|
33
|
+
* @template TUpdate - The update type, must extend CoreUpdate
|
|
34
|
+
*
|
|
35
|
+
* @param context - The execution context containing command, state, and environment
|
|
36
|
+
* @returns AsyncGenerator that yields updates during execution
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Simple execution engine that processes a command
|
|
41
|
+
* const simpleEngine: ExecutionEngine<MyCommand, MyState, MyUpdate> = async function* (context) {
|
|
42
|
+
* const { command, State } = context;
|
|
43
|
+
*
|
|
44
|
+
* // Yield progress update
|
|
45
|
+
* yield {
|
|
46
|
+
* type: 'progress',
|
|
47
|
+
* message: 'Starting execution...',
|
|
48
|
+
* progress: 0
|
|
49
|
+
* };
|
|
50
|
+
*
|
|
51
|
+
* // Process command
|
|
52
|
+
* const result = await processCommand(command);
|
|
53
|
+
*
|
|
54
|
+
* // Yield intermediate results
|
|
55
|
+
* yield {
|
|
56
|
+
* type: 'data',
|
|
57
|
+
* data: result.intermediateData,
|
|
58
|
+
* progress: 0.5
|
|
59
|
+
* };
|
|
60
|
+
*
|
|
61
|
+
* // Complete processing
|
|
62
|
+
* const finalResult = await finalizeResult(result);
|
|
63
|
+
*
|
|
64
|
+
* // Yield final update
|
|
65
|
+
* yield {
|
|
66
|
+
* type: 'complete',
|
|
67
|
+
* data: finalResult,
|
|
68
|
+
* progress: 1.0
|
|
69
|
+
* };
|
|
70
|
+
* };
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // Execution engine with error handling and cancellation
|
|
76
|
+
* const robustEngine: ExecutionEngine = async function* (context) {
|
|
77
|
+
* const { command, signal, isCancelled } = context;
|
|
78
|
+
*
|
|
79
|
+
* try {
|
|
80
|
+
* for (let i = 0; i < command.steps.length; i++) {
|
|
81
|
+
* // Check for cancellation
|
|
82
|
+
* if (isCancelled() || signal.aborted) {
|
|
83
|
+
* yield { type: 'cancelled', step: i };
|
|
84
|
+
* return;
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* // Process step
|
|
88
|
+
* const stepResult = await processStep(command.steps[i]);
|
|
89
|
+
*
|
|
90
|
+
* yield {
|
|
91
|
+
* type: 'step-complete',
|
|
92
|
+
* step: i,
|
|
93
|
+
* result: stepResult,
|
|
94
|
+
* progress: (i + 1) / command.steps.length
|
|
95
|
+
* };
|
|
96
|
+
* }
|
|
97
|
+
* } catch (error) {
|
|
98
|
+
* yield { type: 'error', error: error.message };
|
|
99
|
+
* }
|
|
100
|
+
* };
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @public
|
|
104
|
+
* @since 0.5.6
|
|
105
|
+
*/
|
|
106
|
+
export type ExecutionEngine<TCommand extends CoreCommand = CoreCommand, TState extends CoreState = CoreState, TUpdate extends CoreUpdate = CoreUpdate> = (context: CoreContext<TCommand, TState, TUpdate>) => AsyncGenerator<TUpdate, void, unknown>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025 The Artinet Project
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { CoreCommand, CoreState, CoreUpdate } from "../context/index.js";
|
|
6
|
+
import { ServiceInterface } from "../service.js";
|
|
7
|
+
import { ExecutionEngine } from "./engine.js";
|
|
8
|
+
export interface ExecutionEnvironment<TCommand extends CoreCommand = CoreCommand, TState extends CoreState = CoreState, TUpdate extends CoreUpdate = CoreUpdate> {
|
|
9
|
+
service: ServiceInterface<TCommand, TState, TUpdate>;
|
|
10
|
+
engine?: ExecutionEngine<TCommand, TState, TUpdate>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025 The Artinet Project
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { CoreContext, CoreCommand, CoreState, CoreUpdate } from "../context/index.js";
|
|
6
|
+
import { ExecutionEngine } from "./engine.js";
|
|
7
|
+
export type ExecuteFunction<TCommand extends CoreCommand = CoreCommand, TState extends CoreState = CoreState, TUpdate extends CoreUpdate = CoreUpdate> = (engine: ExecutionEngine<TCommand, TState, TUpdate>, context: CoreContext<TCommand, TState, TUpdate>) => Promise<void>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025 The Artinet Project
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { CoreCommand, CoreState, CoreContext, CoreUpdate } from "../context/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* @description The context manager interface.
|
|
8
|
+
* @type {ContextManagerInterface<TCommand, TState>}
|
|
9
|
+
* @note This will become an increasingly important part of the system as Context Engineering becomes more important.
|
|
10
|
+
* currently it is used to store & retrieve contexts from storage but greater degrees of control may be needed.
|
|
11
|
+
* ex. forking/merging contexts, forwarding contexts to other services, nesting contexts, etc.
|
|
12
|
+
*/
|
|
13
|
+
export interface ContextManagerInterface<TCommand extends CoreCommand = CoreCommand, TState extends CoreState = CoreState, TUpdate extends CoreUpdate = CoreUpdate> {
|
|
14
|
+
getContext: (id: string) => CoreContext<TCommand, TState, TUpdate> | undefined;
|
|
15
|
+
setContext: (id: string, context: CoreContext<TCommand, TState, TUpdate>) => void;
|
|
16
|
+
deleteContext: (id: string) => void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025 The Artinet Project
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @fileoverview Core Event Manager Interface Definitions
|
|
7
|
+
*
|
|
8
|
+
* This module defines interfaces for managing events throughout the execution lifecycle
|
|
9
|
+
* of core services. It provides type-safe event handling, lifecycle management, and
|
|
10
|
+
* state transitions for command execution contexts.
|
|
11
|
+
*
|
|
12
|
+
* @module CoreEventManager
|
|
13
|
+
* @version 0.5.7
|
|
14
|
+
* @since 0.5.6
|
|
15
|
+
* @author The Artinet Project
|
|
16
|
+
*/
|
|
17
|
+
import { EventEmitter } from "events";
|
|
18
|
+
import { CoreCommand, CoreState, CoreUpdate, CoreContext } from "../context/index.js";
|
|
19
|
+
/**
|
|
20
|
+
* Configuration options for EventManager lifecycle callbacks.
|
|
21
|
+
*
|
|
22
|
+
* This interface defines optional callback functions that are invoked during
|
|
23
|
+
* different phases of command execution, providing hooks for custom logic
|
|
24
|
+
* and state management throughout the execution lifecycle.
|
|
25
|
+
*
|
|
26
|
+
* @template TCommand - The command type, must extend CoreCommand
|
|
27
|
+
* @template TState - The state type, must extend CoreState
|
|
28
|
+
* @template TUpdate - The update type, must extend CoreUpdate
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const eventOptions: EventManagerOptions<MyCommand, MyState, MyUpdate> = {
|
|
33
|
+
* onStart: async (context) => {
|
|
34
|
+
* console.log('Execution started for context:', context.contextId);
|
|
35
|
+
* return initialState;
|
|
36
|
+
* },
|
|
37
|
+
* onUpdate: async (current, update) => {
|
|
38
|
+
* console.log('Processing update:', update);
|
|
39
|
+
* return { ...current, lastUpdate: update };
|
|
40
|
+
* },
|
|
41
|
+
* onComplete: async (finalState) => {
|
|
42
|
+
* console.log('Execution completed successfully');
|
|
43
|
+
* }
|
|
44
|
+
* };
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
48
|
+
* @since 0.5.6
|
|
49
|
+
*/
|
|
50
|
+
export interface EventManagerOptions<TCommand extends CoreCommand = CoreCommand, TState extends CoreState = CoreState, TUpdate extends CoreUpdate = CoreUpdate> {
|
|
51
|
+
/**
|
|
52
|
+
* Called when execution starts for a new context.
|
|
53
|
+
*
|
|
54
|
+
* This callback is invoked at the beginning of command execution,
|
|
55
|
+
* allowing for initialization of state and setup of execution context.
|
|
56
|
+
*
|
|
57
|
+
* @param context - The execution context containing command and environment data
|
|
58
|
+
* @returns Promise resolving to the initial state for this execution
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* onStart: async (context) => {
|
|
63
|
+
* const userId = context.command.metadata?.userId;
|
|
64
|
+
* return {
|
|
65
|
+
* userId,
|
|
66
|
+
* startTime: new Date(),
|
|
67
|
+
* status: 'initializing'
|
|
68
|
+
* };
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
readonly onStart?: (context: CoreContext<TCommand, TState, TUpdate>) => Promise<TState>;
|
|
73
|
+
/**
|
|
74
|
+
* Called when execution is cancelled.
|
|
75
|
+
*
|
|
76
|
+
* This callback handles cleanup and state updates when an execution
|
|
77
|
+
* is cancelled before completion, ensuring proper resource cleanup.
|
|
78
|
+
*
|
|
79
|
+
* @param current - The current state at the time of cancellation
|
|
80
|
+
* @param update - The update that triggered the cancellation
|
|
81
|
+
* @returns Promise that resolves when cancellation handling is complete
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* onCancel: async (current, update) => {
|
|
86
|
+
* await cleanupResources(current.resourceIds);
|
|
87
|
+
* await logCancellation(current.userId, update.reason);
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
readonly onCancel?: (current: TState, update: TUpdate) => Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Called for each update during execution.
|
|
94
|
+
*
|
|
95
|
+
* This is the primary callback for processing updates and evolving state
|
|
96
|
+
* throughout the execution lifecycle. It receives the current state and
|
|
97
|
+
* an update, returning the new state.
|
|
98
|
+
*
|
|
99
|
+
* @param current - The current state before applying the update
|
|
100
|
+
* @param update - The update to be processed
|
|
101
|
+
* @returns Promise resolving to the new state after applying the update
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* onUpdate: async (current, update) => {
|
|
106
|
+
* switch (update.type) {
|
|
107
|
+
* case 'progress':
|
|
108
|
+
* return { ...current, progress: update.progress };
|
|
109
|
+
* case 'data':
|
|
110
|
+
* return { ...current, data: [...current.data, update.data] };
|
|
111
|
+
* default:
|
|
112
|
+
* return current;
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
readonly onUpdate?: (current: TState, update: TUpdate) => Promise<TState>;
|
|
118
|
+
/**
|
|
119
|
+
* Called when an error occurs during execution.
|
|
120
|
+
*
|
|
121
|
+
* This callback handles error scenarios, allowing for custom error processing,
|
|
122
|
+
* logging, and recovery strategies.
|
|
123
|
+
*
|
|
124
|
+
* @param current - The current state when the error occurred
|
|
125
|
+
* @param error - The error that occurred (can be any type)
|
|
126
|
+
* @returns Promise that resolves when error handling is complete
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* onError: async (current, error) => {
|
|
131
|
+
* console.error('Execution error:', error);
|
|
132
|
+
* await notifyAdministrators(error, current);
|
|
133
|
+
* await saveErrorLog(current.contextId, error);
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
readonly onError?: (current: TState, error: any) => Promise<void>;
|
|
138
|
+
/**
|
|
139
|
+
* Called when execution completes successfully.
|
|
140
|
+
*
|
|
141
|
+
* This callback is invoked upon successful completion of command execution,
|
|
142
|
+
* allowing for cleanup, final state processing, and completion notifications.
|
|
143
|
+
*
|
|
144
|
+
* @param current - The final state after successful completion
|
|
145
|
+
* @returns Promise that resolves when completion handling is done
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* onComplete: async (finalState) => {
|
|
150
|
+
* await saveResults(finalState.results);
|
|
151
|
+
* await notifyCompletion(finalState.userId);
|
|
152
|
+
* await cleanupTemporaryResources();
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
readonly onComplete?: (current: TState) => Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Optional state getter function.
|
|
159
|
+
*
|
|
160
|
+
* @deprecated This property may be removed in future versions.
|
|
161
|
+
* Use the EventManagerInterface.getState method instead.
|
|
162
|
+
*
|
|
163
|
+
* @returns The current state
|
|
164
|
+
*/
|
|
165
|
+
readonly getState?: () => TState;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Event map defining the structure of events emitted by EventManager.
|
|
169
|
+
*
|
|
170
|
+
* This interface maps event names to their corresponding argument tuples,
|
|
171
|
+
* providing type safety for event emission and subscription.
|
|
172
|
+
*
|
|
173
|
+
* @template TCommand - The command type, must extend CoreCommand
|
|
174
|
+
* @template TState - The state type, must extend CoreState
|
|
175
|
+
* @template TUpdate - The update type, must extend CoreUpdate
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* // Event manager will emit these events with the specified argument types
|
|
180
|
+
* eventManager.on('start', (command: TCommand, state: TState) => {
|
|
181
|
+
* console.log('Execution started');
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* eventManager.on('update', (state: TState, update: TUpdate) => {
|
|
185
|
+
* console.log('State updated');
|
|
186
|
+
* });
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @public
|
|
190
|
+
* @since 0.5.6
|
|
191
|
+
*/
|
|
192
|
+
export interface EventManagerMap<TCommand extends CoreCommand = CoreCommand, TState extends CoreState = CoreState, TUpdate extends CoreUpdate = CoreUpdate> {
|
|
193
|
+
/** Emitted when execution starts - [command, initialState] */
|
|
194
|
+
start: [TCommand, TState];
|
|
195
|
+
/** Emitted when execution is cancelled - [cancelUpdate] */
|
|
196
|
+
cancel: [TUpdate];
|
|
197
|
+
/** Emitted when state is updated - [currentState, update] */
|
|
198
|
+
update: [TState, TUpdate];
|
|
199
|
+
/** Emitted when an error occurs - [error, currentState] */
|
|
200
|
+
error: [any, TState];
|
|
201
|
+
/** Emitted when execution completes - [finalState] */
|
|
202
|
+
complete: [TState];
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Main interface for EventManager implementations.
|
|
206
|
+
*
|
|
207
|
+
* This interface extends EventEmitter to provide typed event handling
|
|
208
|
+
* and defines the contract for managing execution lifecycle events.
|
|
209
|
+
* Implementations must handle state transitions, error scenarios, and
|
|
210
|
+
* provide access to current execution state.
|
|
211
|
+
*
|
|
212
|
+
* @template TCommand - The command type, must extend CoreCommand
|
|
213
|
+
* @template TState - The state type, must extend CoreState
|
|
214
|
+
* @template TUpdate - The update type, must extend CoreUpdate
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* class MyEventManager implements EventManagerInterface<MyCommand, MyState, MyUpdate> {
|
|
219
|
+
* constructor(
|
|
220
|
+
* public readonly contextId: string,
|
|
221
|
+
* private options: EventManagerOptions<MyCommand, MyState, MyUpdate>
|
|
222
|
+
* ) {
|
|
223
|
+
* super();
|
|
224
|
+
* }
|
|
225
|
+
*
|
|
226
|
+
* async onUpdate(update: MyUpdate): Promise<MyState> {
|
|
227
|
+
* const current = this.getState();
|
|
228
|
+
* const newState = await this.options.onUpdate?.(current, update) ?? current;
|
|
229
|
+
* this.emit('update', newState, update);
|
|
230
|
+
* return newState;
|
|
231
|
+
* }
|
|
232
|
+
*
|
|
233
|
+
* // ... other method implementations
|
|
234
|
+
* }
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* @public
|
|
238
|
+
* @since 0.5.6
|
|
239
|
+
*/
|
|
240
|
+
export interface EventManagerInterface<TCommand extends CoreCommand = CoreCommand, TState extends CoreState = CoreState, TUpdate extends CoreUpdate = CoreUpdate> extends EventEmitter<EventManagerMap<TCommand, TState, TUpdate>> {
|
|
241
|
+
/**
|
|
242
|
+
* Unique identifier for this execution context.
|
|
243
|
+
*
|
|
244
|
+
* This ID is used to track and manage the specific execution instance
|
|
245
|
+
* throughout its lifecycle.
|
|
246
|
+
*/
|
|
247
|
+
readonly contextId: string;
|
|
248
|
+
/**
|
|
249
|
+
* Optional callback for handling execution start.
|
|
250
|
+
*
|
|
251
|
+
* When present, this function is called to initialize state for a new execution.
|
|
252
|
+
*
|
|
253
|
+
* @param context - The execution context
|
|
254
|
+
* @returns Promise resolving to initial state
|
|
255
|
+
*/
|
|
256
|
+
readonly onStart?: (context: CoreContext<TCommand, TState, TUpdate>) => Promise<TState>;
|
|
257
|
+
/**
|
|
258
|
+
* Handles execution cancellation.
|
|
259
|
+
*
|
|
260
|
+
* This method processes cancellation requests and performs necessary cleanup.
|
|
261
|
+
*
|
|
262
|
+
* @param update - The update that triggered cancellation
|
|
263
|
+
* @returns Promise that resolves when cancellation is complete
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* await eventManager.onCancel(cancellationUpdate);
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
readonly onCancel: (update: TUpdate) => Promise<void>;
|
|
271
|
+
/**
|
|
272
|
+
* Processes state updates during execution.
|
|
273
|
+
*
|
|
274
|
+
* This is the primary method for handling state evolution throughout
|
|
275
|
+
* the execution lifecycle.
|
|
276
|
+
*
|
|
277
|
+
* @param update - The update to process
|
|
278
|
+
* @returns Promise resolving to the new state
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* const newState = await eventManager.onUpdate(progressUpdate);
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
readonly onUpdate: (update: TUpdate) => Promise<TState>;
|
|
286
|
+
/**
|
|
287
|
+
* Handles errors that occur during execution.
|
|
288
|
+
*
|
|
289
|
+
* This method processes error scenarios and performs error recovery logic.
|
|
290
|
+
*
|
|
291
|
+
* @param error - The error that occurred
|
|
292
|
+
* @returns Promise that resolves when error handling is complete
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* await eventManager.onError(new Error('Something went wrong'));
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
readonly onError: (error: any) => Promise<void>;
|
|
300
|
+
/**
|
|
301
|
+
* Handles successful completion of execution.
|
|
302
|
+
*
|
|
303
|
+
* This method is called when execution completes successfully and
|
|
304
|
+
* performs final cleanup and notification tasks.
|
|
305
|
+
*
|
|
306
|
+
* @returns Promise that resolves when completion handling is done
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* await eventManager.onComplete();
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
onComplete: () => Promise<void>;
|
|
314
|
+
/**
|
|
315
|
+
* Gets the current execution state.
|
|
316
|
+
*
|
|
317
|
+
* @deprecated This method may be removed in future versions.
|
|
318
|
+
* Consider using state management through the context instead.
|
|
319
|
+
*
|
|
320
|
+
* @returns The current state
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
* ```typescript
|
|
324
|
+
* const currentState = eventManager.getState();
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
readonly getState: () => TState;
|
|
328
|
+
}
|