@a2a-wrapper/core 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +186 -0
- package/dist/cli/scaffold.d.ts +237 -0
- package/dist/cli/scaffold.d.ts.map +1 -0
- package/dist/cli/scaffold.js +241 -0
- package/dist/cli/scaffold.js.map +1 -0
- package/dist/config/loader.d.ts +100 -0
- package/dist/config/loader.d.ts.map +1 -0
- package/dist/config/loader.js +130 -0
- package/dist/config/loader.js.map +1 -0
- package/dist/config/types.d.ts +317 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +17 -0
- package/dist/config/types.js.map +1 -0
- package/dist/events/event-publisher.d.ts +205 -0
- package/dist/events/event-publisher.d.ts.map +1 -0
- package/dist/events/event-publisher.js +317 -0
- package/dist/events/event-publisher.js.map +1 -0
- package/dist/executor/types.d.ts +164 -0
- package/dist/executor/types.d.ts.map +1 -0
- package/dist/executor/types.js +30 -0
- package/dist/executor/types.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/server/agent-card.d.ts +66 -0
- package/dist/server/agent-card.d.ts.map +1 -0
- package/dist/server/agent-card.js +114 -0
- package/dist/server/agent-card.js.map +1 -0
- package/dist/server/factory.d.ts +159 -0
- package/dist/server/factory.d.ts.map +1 -0
- package/dist/server/factory.js +167 -0
- package/dist/server/factory.js.map +1 -0
- package/dist/session/base-session-manager.d.ts +218 -0
- package/dist/session/base-session-manager.d.ts.map +1 -0
- package/dist/session/base-session-manager.js +222 -0
- package/dist/session/base-session-manager.js.map +1 -0
- package/dist/utils/deep-merge.d.ts +83 -0
- package/dist/utils/deep-merge.d.ts.map +1 -0
- package/dist/utils/deep-merge.js +108 -0
- package/dist/utils/deep-merge.js.map +1 -0
- package/dist/utils/deferred.d.ts +97 -0
- package/dist/utils/deferred.d.ts.map +1 -0
- package/dist/utils/deferred.js +83 -0
- package/dist/utils/deferred.js.map +1 -0
- package/dist/utils/logger.d.ts +186 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +244 -0
- package/dist/utils/logger.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep Merge and Environment Token Substitution Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides two core utility functions used throughout the
|
|
5
|
+
* configuration loading pipeline:
|
|
6
|
+
*
|
|
7
|
+
* 1. `deepMerge` — Recursively merges a source object into a target object,
|
|
8
|
+
* producing a new object without mutating either input. Used by the config
|
|
9
|
+
* loader to layer defaults ← file ← env ← CLI overrides.
|
|
10
|
+
*
|
|
11
|
+
* 2. `substituteEnvTokens` — Replaces `$VAR_NAME` tokens in string arrays
|
|
12
|
+
* with matching environment variable values. Used to resolve environment
|
|
13
|
+
* references in MCP server arguments and other configuration arrays.
|
|
14
|
+
*
|
|
15
|
+
* Both functions are pure (no side effects beyond reading `process.env`) and
|
|
16
|
+
* return new data structures rather than mutating inputs.
|
|
17
|
+
*
|
|
18
|
+
* @module utils/deep-merge
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Recursively merge `source` into `target`, producing a new object.
|
|
22
|
+
*
|
|
23
|
+
* This function implements a deterministic, recursive merge strategy designed
|
|
24
|
+
* for layered configuration loading. The merge follows these rules:
|
|
25
|
+
*
|
|
26
|
+
* **Merge Rules:**
|
|
27
|
+
* - **Arrays are replaced** — If the source value for a key is an array, it
|
|
28
|
+
* completely replaces the target's array (no concatenation).
|
|
29
|
+
* - **Neither input is mutated** — A new object is always returned. Both
|
|
30
|
+
* `target` and `source` remain unchanged after the call.
|
|
31
|
+
* - **`undefined` values in source are skipped** — If a source key has the
|
|
32
|
+
* value `undefined`, the corresponding target value is preserved.
|
|
33
|
+
* - **`null` values in source replace the target value** — An explicit `null`
|
|
34
|
+
* in the source overwrites whatever the target had for that key.
|
|
35
|
+
* - **Nested objects are recursively merged** — When both the target and
|
|
36
|
+
* source values for a key are plain objects (non-null, non-array), the
|
|
37
|
+
* merge recurses into them.
|
|
38
|
+
*
|
|
39
|
+
* @typeParam T - The shape of the target object. The return type preserves
|
|
40
|
+
* this shape so that downstream consumers retain full type information.
|
|
41
|
+
*
|
|
42
|
+
* @param target - The base object providing default values. Not mutated.
|
|
43
|
+
* @param source - The override object whose defined, non-undefined values
|
|
44
|
+
* take precedence over `target`. Not mutated.
|
|
45
|
+
* @returns A new object containing the merged result of `target` and `source`.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const base = { server: { port: 3000, host: "localhost" }, tags: ["a"] };
|
|
50
|
+
* const overrides = { server: { port: 8080 }, tags: ["b", "c"] };
|
|
51
|
+
* const result = deepMerge(base, overrides);
|
|
52
|
+
* // result = { server: { port: 8080, host: "localhost" }, tags: ["b", "c"] }
|
|
53
|
+
* // base and overrides are unchanged
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function deepMerge<T extends Record<string, unknown>>(target: T, source: Partial<T>): T;
|
|
57
|
+
/**
|
|
58
|
+
* Replace `$VAR_NAME` tokens in a string array with matching environment
|
|
59
|
+
* variable values from `process.env`.
|
|
60
|
+
*
|
|
61
|
+
* Each string in the input array is scanned for tokens matching the pattern
|
|
62
|
+
* `$WORD_CHARS` (i.e. `$` followed by one or more `[A-Za-z0-9_]` characters).
|
|
63
|
+
* When a matching environment variable exists, the token is replaced with its
|
|
64
|
+
* value. Unmatched tokens (no corresponding env var) are left unchanged.
|
|
65
|
+
*
|
|
66
|
+
* The function returns a **new array** — the input array is not mutated.
|
|
67
|
+
*
|
|
68
|
+
* @param args - Array of strings potentially containing `$VAR_NAME` tokens.
|
|
69
|
+
* Each element is independently processed for token substitution.
|
|
70
|
+
* @returns A new array with environment variable tokens substituted where
|
|
71
|
+
* matching values exist. Unmatched tokens remain as-is.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // Given: process.env.HOME = "/home/user"
|
|
76
|
+
* // Given: process.env.WORKSPACE_DIR is not set
|
|
77
|
+
*
|
|
78
|
+
* substituteEnvTokens(["--dir", "$HOME/projects", "$WORKSPACE_DIR"])
|
|
79
|
+
* // Returns: ["--dir", "/home/user/projects", "$WORKSPACE_DIR"]
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function substituteEnvTokens(args: string[]): string[];
|
|
83
|
+
//# sourceMappingURL=deep-merge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep-merge.d.ts","sourceRoot":"","sources":["../../src/utils/deep-merge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACzD,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,GACjB,CAAC,CA+BH;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAI5D"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep Merge and Environment Token Substitution Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides two core utility functions used throughout the
|
|
5
|
+
* configuration loading pipeline:
|
|
6
|
+
*
|
|
7
|
+
* 1. `deepMerge` — Recursively merges a source object into a target object,
|
|
8
|
+
* producing a new object without mutating either input. Used by the config
|
|
9
|
+
* loader to layer defaults ← file ← env ← CLI overrides.
|
|
10
|
+
*
|
|
11
|
+
* 2. `substituteEnvTokens` — Replaces `$VAR_NAME` tokens in string arrays
|
|
12
|
+
* with matching environment variable values. Used to resolve environment
|
|
13
|
+
* references in MCP server arguments and other configuration arrays.
|
|
14
|
+
*
|
|
15
|
+
* Both functions are pure (no side effects beyond reading `process.env`) and
|
|
16
|
+
* return new data structures rather than mutating inputs.
|
|
17
|
+
*
|
|
18
|
+
* @module utils/deep-merge
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Recursively merge `source` into `target`, producing a new object.
|
|
22
|
+
*
|
|
23
|
+
* This function implements a deterministic, recursive merge strategy designed
|
|
24
|
+
* for layered configuration loading. The merge follows these rules:
|
|
25
|
+
*
|
|
26
|
+
* **Merge Rules:**
|
|
27
|
+
* - **Arrays are replaced** — If the source value for a key is an array, it
|
|
28
|
+
* completely replaces the target's array (no concatenation).
|
|
29
|
+
* - **Neither input is mutated** — A new object is always returned. Both
|
|
30
|
+
* `target` and `source` remain unchanged after the call.
|
|
31
|
+
* - **`undefined` values in source are skipped** — If a source key has the
|
|
32
|
+
* value `undefined`, the corresponding target value is preserved.
|
|
33
|
+
* - **`null` values in source replace the target value** — An explicit `null`
|
|
34
|
+
* in the source overwrites whatever the target had for that key.
|
|
35
|
+
* - **Nested objects are recursively merged** — When both the target and
|
|
36
|
+
* source values for a key are plain objects (non-null, non-array), the
|
|
37
|
+
* merge recurses into them.
|
|
38
|
+
*
|
|
39
|
+
* @typeParam T - The shape of the target object. The return type preserves
|
|
40
|
+
* this shape so that downstream consumers retain full type information.
|
|
41
|
+
*
|
|
42
|
+
* @param target - The base object providing default values. Not mutated.
|
|
43
|
+
* @param source - The override object whose defined, non-undefined values
|
|
44
|
+
* take precedence over `target`. Not mutated.
|
|
45
|
+
* @returns A new object containing the merged result of `target` and `source`.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const base = { server: { port: 3000, host: "localhost" }, tags: ["a"] };
|
|
50
|
+
* const overrides = { server: { port: 8080 }, tags: ["b", "c"] };
|
|
51
|
+
* const result = deepMerge(base, overrides);
|
|
52
|
+
* // result = { server: { port: 8080, host: "localhost" }, tags: ["b", "c"] }
|
|
53
|
+
* // base and overrides are unchanged
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export function deepMerge(target, source) {
|
|
57
|
+
const result = { ...target };
|
|
58
|
+
for (const key of Object.keys(source)) {
|
|
59
|
+
const srcVal = source[key];
|
|
60
|
+
// Skip undefined values — preserve the target's value
|
|
61
|
+
if (srcVal === undefined)
|
|
62
|
+
continue;
|
|
63
|
+
const tgtVal = result[key];
|
|
64
|
+
// Recursively merge when both sides are plain objects (non-null, non-array)
|
|
65
|
+
if (tgtVal !== null &&
|
|
66
|
+
srcVal !== null &&
|
|
67
|
+
typeof tgtVal === "object" &&
|
|
68
|
+
typeof srcVal === "object" &&
|
|
69
|
+
!Array.isArray(tgtVal) &&
|
|
70
|
+
!Array.isArray(srcVal)) {
|
|
71
|
+
result[key] = deepMerge(tgtVal, srcVal);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// Arrays, primitives, and null — direct replacement
|
|
75
|
+
result[key] = srcVal;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Replace `$VAR_NAME` tokens in a string array with matching environment
|
|
82
|
+
* variable values from `process.env`.
|
|
83
|
+
*
|
|
84
|
+
* Each string in the input array is scanned for tokens matching the pattern
|
|
85
|
+
* `$WORD_CHARS` (i.e. `$` followed by one or more `[A-Za-z0-9_]` characters).
|
|
86
|
+
* When a matching environment variable exists, the token is replaced with its
|
|
87
|
+
* value. Unmatched tokens (no corresponding env var) are left unchanged.
|
|
88
|
+
*
|
|
89
|
+
* The function returns a **new array** — the input array is not mutated.
|
|
90
|
+
*
|
|
91
|
+
* @param args - Array of strings potentially containing `$VAR_NAME` tokens.
|
|
92
|
+
* Each element is independently processed for token substitution.
|
|
93
|
+
* @returns A new array with environment variable tokens substituted where
|
|
94
|
+
* matching values exist. Unmatched tokens remain as-is.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* // Given: process.env.HOME = "/home/user"
|
|
99
|
+
* // Given: process.env.WORKSPACE_DIR is not set
|
|
100
|
+
*
|
|
101
|
+
* substituteEnvTokens(["--dir", "$HOME/projects", "$WORKSPACE_DIR"])
|
|
102
|
+
* // Returns: ["--dir", "/home/user/projects", "$WORKSPACE_DIR"]
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export function substituteEnvTokens(args) {
|
|
106
|
+
return args.map((arg) => arg.replace(/\$(\w+)/g, (_match, name) => process.env[name] ?? _match));
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=deep-merge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep-merge.js","sourceRoot":"","sources":["../../src/utils/deep-merge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,SAAS,CACvB,MAAS,EACT,MAAkB;IAElB,MAAM,MAAM,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;IAE7B,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QACtC,MAAM,MAAM,GAAI,MAAkC,CAAC,GAAG,CAAC,CAAC;QAExD,sDAAsD;QACtD,IAAI,MAAM,KAAK,SAAS;YAAE,SAAS;QAEnC,MAAM,MAAM,GAAI,MAAkC,CAAC,GAAG,CAAC,CAAC;QAExD,4EAA4E;QAC5E,IACE,MAAM,KAAK,IAAI;YACf,MAAM,KAAK,IAAI;YACf,OAAO,MAAM,KAAK,QAAQ;YAC1B,OAAO,MAAM,KAAK,QAAQ;YAC1B,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;YACtB,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EACtB,CAAC;YACA,MAAkC,CAAC,GAAG,CAAC,GAAG,SAAS,CAClD,MAAiC,EACjC,MAAiC,CAClC,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,oDAAoD;YACnD,MAAkC,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;QACpD,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAc;IAChD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CACtB,GAAG,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,MAAM,EAAE,IAAY,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,CAC/E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module utils/deferred
|
|
3
|
+
*
|
|
4
|
+
* Promise utilities for externally-controlled resolution and timed delays.
|
|
5
|
+
*
|
|
6
|
+
* This module provides two primitives used extensively across A2A wrapper
|
|
7
|
+
* projects:
|
|
8
|
+
*
|
|
9
|
+
* - {@link Deferred} / {@link createDeferred} — a promise whose `resolve` and
|
|
10
|
+
* `reject` callbacks are exposed as properties, enabling completion from a
|
|
11
|
+
* different code path than the one that created the promise. This pattern is
|
|
12
|
+
* common in streaming event handlers where the completion signal arrives
|
|
13
|
+
* asynchronously.
|
|
14
|
+
*
|
|
15
|
+
* - {@link sleep} — a simple timer-based delay that returns a promise,
|
|
16
|
+
* useful for polling loops and retry back-off.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { createDeferred, sleep } from '@a2a-wrapper/core';
|
|
21
|
+
*
|
|
22
|
+
* // Deferred usage — resolve from an event handler
|
|
23
|
+
* const deferred = createDeferred<string>();
|
|
24
|
+
* eventBus.on('done', (result) => deferred.resolve(result));
|
|
25
|
+
* const value = await deferred.promise;
|
|
26
|
+
*
|
|
27
|
+
* // Sleep usage — wait 500 ms between retries
|
|
28
|
+
* await sleep(500);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* A deferred promise whose `resolve` and `reject` callbacks are accessible
|
|
33
|
+
* as properties, allowing external code to settle the promise.
|
|
34
|
+
*
|
|
35
|
+
* Created via {@link createDeferred}. The `promise` property is a standard
|
|
36
|
+
* `Promise<T>` that settles when either `resolve` or `reject` is called.
|
|
37
|
+
*
|
|
38
|
+
* @typeParam T - The type of the value the promise resolves with.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const d: Deferred<number> = createDeferred<number>();
|
|
43
|
+
* d.resolve(42);
|
|
44
|
+
* const result = await d.promise; // 42
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export interface Deferred<T> {
|
|
48
|
+
/** The underlying promise that settles when {@link resolve} or {@link reject} is called. */
|
|
49
|
+
promise: Promise<T>;
|
|
50
|
+
/** Resolves the deferred promise with the given value. */
|
|
51
|
+
resolve: (value: T) => void;
|
|
52
|
+
/** Rejects the deferred promise with an optional reason. */
|
|
53
|
+
reject: (reason?: unknown) => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Creates a new {@link Deferred} instance.
|
|
57
|
+
*
|
|
58
|
+
* The returned object exposes `resolve` and `reject` callbacks alongside the
|
|
59
|
+
* `promise` they control. This is equivalent to extracting the executor
|
|
60
|
+
* arguments from `new Promise()` and storing them externally.
|
|
61
|
+
*
|
|
62
|
+
* @typeParam T - The type of the value the promise resolves with.
|
|
63
|
+
* @returns A new {@link Deferred} with an unsettled promise.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const deferred = createDeferred<string>();
|
|
68
|
+
*
|
|
69
|
+
* // Later, in a callback or event handler:
|
|
70
|
+
* deferred.resolve('done');
|
|
71
|
+
*
|
|
72
|
+
* // The consumer awaits the promise:
|
|
73
|
+
* const result = await deferred.promise; // 'done'
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function createDeferred<T>(): Deferred<T>;
|
|
77
|
+
/**
|
|
78
|
+
* Returns a promise that resolves after the specified number of milliseconds.
|
|
79
|
+
*
|
|
80
|
+
* Useful for introducing delays in polling loops, retry back-off strategies,
|
|
81
|
+
* and test utilities. The returned promise always resolves (never rejects).
|
|
82
|
+
*
|
|
83
|
+
* @param ms - The delay duration in milliseconds. A value of `0` defers to
|
|
84
|
+
* the next event-loop tick via `setTimeout(..., 0)`.
|
|
85
|
+
* @returns A promise that resolves with `void` after `ms` milliseconds.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* // Wait 1 second between retries
|
|
90
|
+
* for (let attempt = 0; attempt < 3; attempt++) {
|
|
91
|
+
* try { return await fetchData(); }
|
|
92
|
+
* catch { await sleep(1000); }
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
97
|
+
//# sourceMappingURL=deferred.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deferred.d.ts","sourceRoot":"","sources":["../../src/utils/deferred.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC;IACzB,4FAA4F;IAC5F,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAEpB,0DAA0D;IAC1D,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IAE5B,4DAA4D;IAC5D,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACpC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAQ/C;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module utils/deferred
|
|
3
|
+
*
|
|
4
|
+
* Promise utilities for externally-controlled resolution and timed delays.
|
|
5
|
+
*
|
|
6
|
+
* This module provides two primitives used extensively across A2A wrapper
|
|
7
|
+
* projects:
|
|
8
|
+
*
|
|
9
|
+
* - {@link Deferred} / {@link createDeferred} — a promise whose `resolve` and
|
|
10
|
+
* `reject` callbacks are exposed as properties, enabling completion from a
|
|
11
|
+
* different code path than the one that created the promise. This pattern is
|
|
12
|
+
* common in streaming event handlers where the completion signal arrives
|
|
13
|
+
* asynchronously.
|
|
14
|
+
*
|
|
15
|
+
* - {@link sleep} — a simple timer-based delay that returns a promise,
|
|
16
|
+
* useful for polling loops and retry back-off.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { createDeferred, sleep } from '@a2a-wrapper/core';
|
|
21
|
+
*
|
|
22
|
+
* // Deferred usage — resolve from an event handler
|
|
23
|
+
* const deferred = createDeferred<string>();
|
|
24
|
+
* eventBus.on('done', (result) => deferred.resolve(result));
|
|
25
|
+
* const value = await deferred.promise;
|
|
26
|
+
*
|
|
27
|
+
* // Sleep usage — wait 500 ms between retries
|
|
28
|
+
* await sleep(500);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new {@link Deferred} instance.
|
|
33
|
+
*
|
|
34
|
+
* The returned object exposes `resolve` and `reject` callbacks alongside the
|
|
35
|
+
* `promise` they control. This is equivalent to extracting the executor
|
|
36
|
+
* arguments from `new Promise()` and storing them externally.
|
|
37
|
+
*
|
|
38
|
+
* @typeParam T - The type of the value the promise resolves with.
|
|
39
|
+
* @returns A new {@link Deferred} with an unsettled promise.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const deferred = createDeferred<string>();
|
|
44
|
+
*
|
|
45
|
+
* // Later, in a callback or event handler:
|
|
46
|
+
* deferred.resolve('done');
|
|
47
|
+
*
|
|
48
|
+
* // The consumer awaits the promise:
|
|
49
|
+
* const result = await deferred.promise; // 'done'
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function createDeferred() {
|
|
53
|
+
let resolve;
|
|
54
|
+
let reject;
|
|
55
|
+
const promise = new Promise((res, rej) => {
|
|
56
|
+
resolve = res;
|
|
57
|
+
reject = rej;
|
|
58
|
+
});
|
|
59
|
+
return { promise, resolve, reject };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns a promise that resolves after the specified number of milliseconds.
|
|
63
|
+
*
|
|
64
|
+
* Useful for introducing delays in polling loops, retry back-off strategies,
|
|
65
|
+
* and test utilities. The returned promise always resolves (never rejects).
|
|
66
|
+
*
|
|
67
|
+
* @param ms - The delay duration in milliseconds. A value of `0` defers to
|
|
68
|
+
* the next event-loop tick via `setTimeout(..., 0)`.
|
|
69
|
+
* @returns A promise that resolves with `void` after `ms` milliseconds.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* // Wait 1 second between retries
|
|
74
|
+
* for (let attempt = 0; attempt < 3; attempt++) {
|
|
75
|
+
* try { return await fetchData(); }
|
|
76
|
+
* catch { await sleep(1000); }
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function sleep(ms) {
|
|
81
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=deferred.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deferred.js","sourceRoot":"","sources":["../../src/utils/deferred.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AA6BH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc;IAC5B,IAAI,OAA4B,CAAC;IACjC,IAAI,MAAmC,CAAC;IACxC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC1C,OAAO,GAAG,GAAG,CAAC;QACd,MAAM,GAAG,GAAG,CAAC;IACf,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACtC,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module utils/logger
|
|
3
|
+
*
|
|
4
|
+
* Structured, leveled logging with child-logger support and configurable root names.
|
|
5
|
+
*
|
|
6
|
+
* This module provides the logging infrastructure for all A2A wrapper projects.
|
|
7
|
+
* Each wrapper creates its own root logger via {@link createLogger}, avoiding
|
|
8
|
+
* hardcoded singletons and enabling independent log hierarchies per project.
|
|
9
|
+
*
|
|
10
|
+
* Output format: `[ISO_timestamp] [LEVEL] [name] message {data}`
|
|
11
|
+
*
|
|
12
|
+
* - ERROR messages route to `console.error`
|
|
13
|
+
* - WARN messages route to `console.warn`
|
|
14
|
+
* - DEBUG and INFO messages route to `console.log`
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { createLogger, LogLevel } from '@a2a-wrapper/core';
|
|
19
|
+
*
|
|
20
|
+
* const logger = createLogger('a2a-copilot');
|
|
21
|
+
* logger.setLevel(LogLevel.DEBUG);
|
|
22
|
+
*
|
|
23
|
+
* const child = logger.child('session');
|
|
24
|
+
* child.info('session started', { contextId: 'abc-123' });
|
|
25
|
+
* // => [2024-01-15T10:30:00.000Z] [INFO] [a2a-copilot:session] session started {"contextId":"abc-123"}
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Numeric log levels controlling message suppression.
|
|
30
|
+
*
|
|
31
|
+
* Messages are emitted only when their level is greater than or equal to the
|
|
32
|
+
* logger's configured minimum level. Lower numeric values represent more
|
|
33
|
+
* verbose output.
|
|
34
|
+
*/
|
|
35
|
+
export declare enum LogLevel {
|
|
36
|
+
/** Verbose diagnostic output, typically disabled in production. */
|
|
37
|
+
DEBUG = 0,
|
|
38
|
+
/** General operational messages indicating normal behavior. */
|
|
39
|
+
INFO = 1,
|
|
40
|
+
/** Potentially harmful situations that deserve attention. */
|
|
41
|
+
WARN = 2,
|
|
42
|
+
/** Failures requiring immediate investigation. */
|
|
43
|
+
ERROR = 3
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Structured logger with hierarchical naming and runtime level control.
|
|
47
|
+
*
|
|
48
|
+
* Each Logger instance has a `name` (used as a prefix in output) and a minimum
|
|
49
|
+
* {@link LogLevel}. Child loggers inherit the parent's level at creation time
|
|
50
|
+
* and format their name as `{parent}:{child}`.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const root = new Logger('myApp', LogLevel.DEBUG);
|
|
55
|
+
* const child = root.child('http');
|
|
56
|
+
* child.info('request received', { method: 'GET', path: '/' });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare class Logger {
|
|
60
|
+
/**
|
|
61
|
+
* Dot-colon-separated name identifying this logger in output.
|
|
62
|
+
*
|
|
63
|
+
* @readonly
|
|
64
|
+
*/
|
|
65
|
+
private readonly name;
|
|
66
|
+
/**
|
|
67
|
+
* Current minimum log level. Messages below this level are suppressed.
|
|
68
|
+
*/
|
|
69
|
+
private level;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a new Logger instance.
|
|
72
|
+
*
|
|
73
|
+
* @param name - Identifier included in every log line (e.g. `"a2a-copilot"` or `"a2a-copilot:session"`).
|
|
74
|
+
* @param level - Minimum log level; defaults to {@link LogLevel.INFO}.
|
|
75
|
+
*/
|
|
76
|
+
constructor(name: string, level?: LogLevel);
|
|
77
|
+
/**
|
|
78
|
+
* Changes the minimum log level at runtime.
|
|
79
|
+
*
|
|
80
|
+
* All subsequent calls to {@link debug}, {@link info}, {@link warn}, and
|
|
81
|
+
* {@link error} will be filtered against the new level.
|
|
82
|
+
*
|
|
83
|
+
* @param level - The new minimum {@link LogLevel}.
|
|
84
|
+
*/
|
|
85
|
+
setLevel(level: LogLevel): void;
|
|
86
|
+
/**
|
|
87
|
+
* Parses a string into a {@link LogLevel}.
|
|
88
|
+
*
|
|
89
|
+
* Matching is case-insensitive. The string `"warning"` is accepted as an
|
|
90
|
+
* alias for {@link LogLevel.WARN}. Unrecognized strings default to
|
|
91
|
+
* {@link LogLevel.INFO}.
|
|
92
|
+
*
|
|
93
|
+
* @param str - The string to parse (e.g. `"debug"`, `"WARN"`, `"error"`).
|
|
94
|
+
* @returns The corresponding {@link LogLevel} value.
|
|
95
|
+
*/
|
|
96
|
+
static parseLevel(str: string): LogLevel;
|
|
97
|
+
/**
|
|
98
|
+
* Creates a child logger that inherits this logger's current level.
|
|
99
|
+
*
|
|
100
|
+
* The child's name is formatted as `{parentName}:{childName}`, producing
|
|
101
|
+
* a colon-separated hierarchy visible in log output.
|
|
102
|
+
*
|
|
103
|
+
* @param childName - Short identifier appended to the parent name.
|
|
104
|
+
* @returns A new {@link Logger} instance with the composite name.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const root = createLogger('app');
|
|
109
|
+
* const child = root.child('db');
|
|
110
|
+
* const grandchild = child.child('query');
|
|
111
|
+
* // grandchild.name === 'app:db:query'
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
child(childName: string): Logger;
|
|
115
|
+
/**
|
|
116
|
+
* Logs a message at {@link LogLevel.DEBUG}.
|
|
117
|
+
*
|
|
118
|
+
* @param msg - Human-readable log message.
|
|
119
|
+
* @param data - Optional structured data appended as JSON.
|
|
120
|
+
*/
|
|
121
|
+
debug(msg: string, data?: Record<string, unknown>): void;
|
|
122
|
+
/**
|
|
123
|
+
* Logs a message at {@link LogLevel.INFO}.
|
|
124
|
+
*
|
|
125
|
+
* @param msg - Human-readable log message.
|
|
126
|
+
* @param data - Optional structured data appended as JSON.
|
|
127
|
+
*/
|
|
128
|
+
info(msg: string, data?: Record<string, unknown>): void;
|
|
129
|
+
/**
|
|
130
|
+
* Logs a message at {@link LogLevel.WARN}.
|
|
131
|
+
*
|
|
132
|
+
* @param msg - Human-readable log message.
|
|
133
|
+
* @param data - Optional structured data appended as JSON.
|
|
134
|
+
*/
|
|
135
|
+
warn(msg: string, data?: Record<string, unknown>): void;
|
|
136
|
+
/**
|
|
137
|
+
* Logs a message at {@link LogLevel.ERROR}.
|
|
138
|
+
*
|
|
139
|
+
* @param msg - Human-readable log message.
|
|
140
|
+
* @param data - Optional structured data appended as JSON.
|
|
141
|
+
*/
|
|
142
|
+
error(msg: string, data?: Record<string, unknown>): void;
|
|
143
|
+
/**
|
|
144
|
+
* Internal method that formats and emits a log line if the message level
|
|
145
|
+
* meets or exceeds the configured minimum.
|
|
146
|
+
*
|
|
147
|
+
* Output format: `[ISO_timestamp] [LEVEL] [name] message {data}`
|
|
148
|
+
*
|
|
149
|
+
* Routing:
|
|
150
|
+
* - {@link LogLevel.ERROR} → `console.error`
|
|
151
|
+
* - {@link LogLevel.WARN} → `console.warn`
|
|
152
|
+
* - All others → `console.log`
|
|
153
|
+
*
|
|
154
|
+
* @param level - The severity level of this message.
|
|
155
|
+
* @param msg - Human-readable log message.
|
|
156
|
+
* @param data - Optional structured data serialized as JSON.
|
|
157
|
+
*
|
|
158
|
+
* @internal
|
|
159
|
+
*/
|
|
160
|
+
private write;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Creates a new root {@link Logger} instance with the given name.
|
|
164
|
+
*
|
|
165
|
+
* This is the recommended entry point for obtaining a logger. Each wrapper
|
|
166
|
+
* project should call this once with its own root name, then use
|
|
167
|
+
* {@link Logger.child} to create scoped loggers for subsystems.
|
|
168
|
+
*
|
|
169
|
+
* Unlike a singleton, this factory allows multiple independent logger
|
|
170
|
+
* hierarchies to coexist — one per wrapper project or test suite.
|
|
171
|
+
*
|
|
172
|
+
* @param rootName - The root identifier for the logger hierarchy
|
|
173
|
+
* (e.g. `"a2a-copilot"`, `"a2a-opencode"`).
|
|
174
|
+
* @returns A new {@link Logger} instance with the default level {@link LogLevel.INFO}.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* import { createLogger } from '@a2a-wrapper/core';
|
|
179
|
+
*
|
|
180
|
+
* const logger = createLogger('a2a-copilot');
|
|
181
|
+
* const sessionLog = logger.child('session');
|
|
182
|
+
* sessionLog.info('ready');
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export declare function createLogger(rootName: string): Logger;
|
|
186
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;;;;;GAMG;AACH,oBAAY,QAAQ;IAClB,mEAAmE;IACnE,KAAK,IAAI;IACT,+DAA+D;IAC/D,IAAI,IAAI;IACR,6DAA6D;IAC7D,IAAI,IAAI;IACR,kDAAkD;IAClD,KAAK,IAAI;CACV;AAcD;;;;;;;;;;;;;GAaG;AACH,qBAAa,MAAM;IACjB;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAE9B;;OAEG;IACH,OAAO,CAAC,KAAK,CAAW;IAExB;;;;;OAKG;gBACS,IAAI,EAAE,MAAM,EAAE,KAAK,GAAE,QAAwB;IAKzD;;;;;;;OAOG;IACH,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAI/B;;;;;;;;;OASG;IACH,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ;IAcxC;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAIhC;;;;;OAKG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAIxD;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAIvD;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAIvD;;;;;OAKG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAIxD;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,KAAK;CAqBd;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAErD"}
|