agents-library 1.1.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.

Potentially problematic release.


This version of agents-library might be problematic. Click here for more details.

Files changed (89) hide show
  1. package/dist/base-agent.d.ts +172 -0
  2. package/dist/base-agent.d.ts.map +1 -0
  3. package/dist/base-agent.js +255 -0
  4. package/dist/base-agent.js.map +1 -0
  5. package/dist/base-bot.d.ts +282 -0
  6. package/dist/base-bot.d.ts.map +1 -0
  7. package/dist/base-bot.js +375 -0
  8. package/dist/base-bot.js.map +1 -0
  9. package/dist/common/result.d.ts +51 -0
  10. package/dist/common/result.d.ts.map +1 -0
  11. package/dist/common/result.js +45 -0
  12. package/dist/common/result.js.map +1 -0
  13. package/dist/common/types.d.ts +57 -0
  14. package/dist/common/types.d.ts.map +1 -0
  15. package/dist/common/types.js +42 -0
  16. package/dist/common/types.js.map +1 -0
  17. package/dist/index.d.ts +94 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +108 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/kadi-event-publisher.d.ts +163 -0
  22. package/dist/kadi-event-publisher.d.ts.map +1 -0
  23. package/dist/kadi-event-publisher.js +286 -0
  24. package/dist/kadi-event-publisher.js.map +1 -0
  25. package/dist/memory/arcadedb-adapter.d.ts +159 -0
  26. package/dist/memory/arcadedb-adapter.d.ts.map +1 -0
  27. package/dist/memory/arcadedb-adapter.js +314 -0
  28. package/dist/memory/arcadedb-adapter.js.map +1 -0
  29. package/dist/memory/file-storage-adapter.d.ts +122 -0
  30. package/dist/memory/file-storage-adapter.d.ts.map +1 -0
  31. package/dist/memory/file-storage-adapter.js +352 -0
  32. package/dist/memory/file-storage-adapter.js.map +1 -0
  33. package/dist/memory/memory-service.d.ts +208 -0
  34. package/dist/memory/memory-service.d.ts.map +1 -0
  35. package/dist/memory/memory-service.js +410 -0
  36. package/dist/memory/memory-service.js.map +1 -0
  37. package/dist/memory/types.d.ts +126 -0
  38. package/dist/memory/types.d.ts.map +1 -0
  39. package/dist/memory/types.js +41 -0
  40. package/dist/memory/types.js.map +1 -0
  41. package/dist/producer-tool-utils.d.ts +474 -0
  42. package/dist/producer-tool-utils.d.ts.map +1 -0
  43. package/dist/producer-tool-utils.js +664 -0
  44. package/dist/producer-tool-utils.js.map +1 -0
  45. package/dist/providers/anthropic-provider.d.ts +160 -0
  46. package/dist/providers/anthropic-provider.d.ts.map +1 -0
  47. package/dist/providers/anthropic-provider.js +527 -0
  48. package/dist/providers/anthropic-provider.js.map +1 -0
  49. package/dist/providers/model-manager-provider.d.ts +91 -0
  50. package/dist/providers/model-manager-provider.d.ts.map +1 -0
  51. package/dist/providers/model-manager-provider.js +355 -0
  52. package/dist/providers/model-manager-provider.js.map +1 -0
  53. package/dist/providers/provider-manager.d.ts +111 -0
  54. package/dist/providers/provider-manager.d.ts.map +1 -0
  55. package/dist/providers/provider-manager.js +337 -0
  56. package/dist/providers/provider-manager.js.map +1 -0
  57. package/dist/providers/types.d.ts +145 -0
  58. package/dist/providers/types.d.ts.map +1 -0
  59. package/dist/providers/types.js +23 -0
  60. package/dist/providers/types.js.map +1 -0
  61. package/dist/shadow-agent-factory.d.ts +623 -0
  62. package/dist/shadow-agent-factory.d.ts.map +1 -0
  63. package/dist/shadow-agent-factory.js +1117 -0
  64. package/dist/shadow-agent-factory.js.map +1 -0
  65. package/dist/types/agent-config.d.ts +307 -0
  66. package/dist/types/agent-config.d.ts.map +1 -0
  67. package/dist/types/agent-config.js +15 -0
  68. package/dist/types/agent-config.js.map +1 -0
  69. package/dist/types/event-schemas.d.ts +358 -0
  70. package/dist/types/event-schemas.d.ts.map +1 -0
  71. package/dist/types/event-schemas.js +188 -0
  72. package/dist/types/event-schemas.js.map +1 -0
  73. package/dist/types/tool-schemas.d.ts +498 -0
  74. package/dist/types/tool-schemas.d.ts.map +1 -0
  75. package/dist/types/tool-schemas.js +457 -0
  76. package/dist/types/tool-schemas.js.map +1 -0
  77. package/dist/utils/logger.d.ts +135 -0
  78. package/dist/utils/logger.d.ts.map +1 -0
  79. package/dist/utils/logger.js +205 -0
  80. package/dist/utils/logger.js.map +1 -0
  81. package/dist/utils/timer.d.ts +186 -0
  82. package/dist/utils/timer.d.ts.map +1 -0
  83. package/dist/utils/timer.js +211 -0
  84. package/dist/utils/timer.js.map +1 -0
  85. package/dist/worker-agent-factory.d.ts +688 -0
  86. package/dist/worker-agent-factory.d.ts.map +1 -0
  87. package/dist/worker-agent-factory.js +1517 -0
  88. package/dist/worker-agent-factory.js.map +1 -0
  89. package/package.json +38 -0
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Unreal Engine-Style Logger Utility
3
+ * ==================================
4
+ *
5
+ * Provides structured logging with Unreal Engine format:
6
+ * [MODULE_NAME] [HH:MM:SS.mmm] LogLevel: Message
7
+ *
8
+ * This logger is shared across all KĀDI agents (template-agent-typescript,
9
+ * agent-producer, agent-artist, etc.) for consistent log formatting.
10
+ *
11
+ * Usage:
12
+ * ```typescript
13
+ * import { logger, MODULE_AGENT } from 'agents-library';
14
+ *
15
+ * logger.info(MODULE_AGENT, 'Application started');
16
+ * logger.warn(MODULE_SLACK_BOT, 'Circuit breaker open', { timeout: 5000 });
17
+ * logger.error(MODULE_AGENT, 'Connection failed', error);
18
+ * logger.debug(MODULE_TOOLS, 'Tool invocation', { toolName: 'echo' });
19
+ * ```
20
+ *
21
+ * @module logger
22
+ */
23
+ /**
24
+ * Module name constants for structured logging
25
+ * All use kebab-case for consistency with KĀDI naming conventions
26
+ */
27
+ export const MODULE_AGENT = 'template-agent';
28
+ export const MODULE_SLACK_BOT = 'slack-bot';
29
+ export const MODULE_DISCORD_BOT = 'discord-bot';
30
+ export const MODULE_TASK_HANDLER = 'task-handler';
31
+ export const MODULE_TOOLS = 'tools';
32
+ /**
33
+ * ANSI Color Codes for Terminal Output
34
+ *
35
+ * These color codes are applied only in TTY (interactive terminal) environments.
36
+ * Non-TTY environments (pipes, redirects, log files) automatically receive plain text.
37
+ *
38
+ * Respects the NO_COLOR environment variable (https://no-color.org/)
39
+ * Set NO_COLOR=1 to disable colors globally.
40
+ */
41
+ const COLORS = {
42
+ RESET: '\x1b[0m',
43
+ CYAN: '\x1b[36m', // Info level
44
+ YELLOW: '\x1b[33m', // Warning level
45
+ RED: '\x1b[31m', // Error level
46
+ GRAY: '\x1b[90m', // Debug level
47
+ WHITE: '\x1b[37m' // Message text color
48
+ };
49
+ /**
50
+ * UnrealLogger class for Unreal Engine-style structured logging
51
+ *
52
+ * Formats all log output as: [MODULE_NAME] [HH:MM:SS.mmm] LogLevel: Message
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const logger = new UnrealLogger();
57
+ * logger.info('my-module', 'Initialization complete');
58
+ * // Output: [my-module] [14:32:45.123] Info: Initialization complete
59
+ * ```
60
+ */
61
+ class Logger {
62
+ /**
63
+ * Get current timestamp in HH:MM:SS.mmm format
64
+ *
65
+ * @returns Formatted timestamp string with millisecond precision
66
+ */
67
+ getTimestamp() {
68
+ const now = new Date();
69
+ const hours = String(now.getHours()).padStart(2, '0');
70
+ const minutes = String(now.getMinutes()).padStart(2, '0');
71
+ const seconds = String(now.getSeconds()).padStart(2, '0');
72
+ const milliseconds = String(now.getMilliseconds()).padStart(3, '0');
73
+ return `${hours}:${minutes}:${seconds}.${milliseconds}`;
74
+ }
75
+ /**
76
+ * Determine if colors should be used in log output
77
+ *
78
+ * Colors are enabled only when:
79
+ * 1. stdout is a TTY (interactive terminal)
80
+ * 2. NO_COLOR environment variable is not set
81
+ *
82
+ * This respects the NO_COLOR standard (https://no-color.org/)
83
+ * which provides a universal way to disable colorized output.
84
+ *
85
+ * @returns true if colors should be applied, false for plain text
86
+ */
87
+ shouldUseColors() {
88
+ return process.stdout.isTTY && process.env.NO_COLOR === undefined;
89
+ }
90
+ /**
91
+ * Format log message with module name and timestamp
92
+ *
93
+ * @param module - Module name (e.g., 'template-agent', 'slack-bot')
94
+ * @param level - Log level (Info, Warning, Error, Debug)
95
+ * @param message - Log message
96
+ * @param elapsedTime - Elapsed time suffix (e.g., '+5s', '+3m') - always included
97
+ * @param color - Optional ANSI color code to apply to module bracket, level indicator, and elapsed time
98
+ * @returns Formatted log string with selective coloring: <color>[module]<reset> [timestamp] <color>level<reset>: <white>message<reset> <color>elapsedTime<reset>
99
+ */
100
+ formatMessage(module, level, message, elapsedTime, color) {
101
+ const timestamp = this.getTimestamp();
102
+ if (this.shouldUseColors() && color) {
103
+ // Apply selective coloring with module name and elapsed time both colored to match level:
104
+ // <color>[module]<reset> [timestamp] <color>level<reset>: <white>message<reset> <color>elapsedTime<reset>
105
+ // Module bracket colored to match level (cyan/yellow/red/gray)
106
+ // Timestamp in terminal default color (no color codes)
107
+ // Level indicator colored to match level
108
+ // Message text in WHITE
109
+ // Elapsed time suffix always colored to match level
110
+ return `${color}[${module}]${COLORS.RESET} [${timestamp}] ${color}${level}${COLORS.RESET}: ${COLORS.WHITE}${message}${COLORS.RESET} ${color}${elapsedTime}${COLORS.RESET}`;
111
+ }
112
+ // Plain text for non-TTY environments
113
+ return `[${module}] [${timestamp}] ${level}: ${message} ${elapsedTime}`;
114
+ }
115
+ /**
116
+ * Log informational message
117
+ *
118
+ * Used for normal operational events (startup, connections, completions)
119
+ *
120
+ * @param module - Module name
121
+ * @param message - Log message
122
+ * @param elapsedTime - Elapsed time suffix (e.g., '+5s', '+3m')
123
+ * @param data - Optional data to log (will be logged on separate line)
124
+ */
125
+ info(module, message, elapsedTime, data) {
126
+ console.log(this.formatMessage(module, 'Info', message, elapsedTime, COLORS.CYAN));
127
+ if (data) {
128
+ console.log(data);
129
+ }
130
+ }
131
+ /**
132
+ * Log warning message
133
+ *
134
+ * Used for potentially problematic situations (circuit breaker open, retries, timeouts)
135
+ *
136
+ * @param module - Module name
137
+ * @param message - Log message
138
+ * @param elapsedTime - Elapsed time suffix (e.g., '+5s', '+3m')
139
+ * @param data - Optional data to log
140
+ */
141
+ warn(module, message, elapsedTime, data) {
142
+ console.warn(this.formatMessage(module, 'Warning', message, elapsedTime, COLORS.YELLOW));
143
+ if (data) {
144
+ console.warn(data);
145
+ }
146
+ }
147
+ /**
148
+ * Log error message
149
+ *
150
+ * Used for error conditions and exceptions
151
+ *
152
+ * @param module - Module name
153
+ * @param message - Log message
154
+ * @param elapsedTime - Elapsed time suffix (e.g., '+5s', '+3m')
155
+ * @param error - Optional error object or error message string
156
+ * @param data - Optional additional data to log
157
+ */
158
+ error(module, message, elapsedTime, error, data) {
159
+ console.error(this.formatMessage(module, 'Error', message, elapsedTime, COLORS.RED));
160
+ if (error instanceof Error) {
161
+ if (error.stack) {
162
+ console.error(error.stack);
163
+ }
164
+ else {
165
+ console.error(error.message);
166
+ }
167
+ }
168
+ else if (error) {
169
+ console.error(error);
170
+ }
171
+ if (data) {
172
+ console.error(data);
173
+ }
174
+ }
175
+ /**
176
+ * Log debug message
177
+ *
178
+ * Used for detailed diagnostic information during development
179
+ *
180
+ * @param module - Module name
181
+ * @param message - Log message
182
+ * @param elapsedTime - Elapsed time suffix (e.g., '+5s', '+3m')
183
+ * @param data - Optional data to log
184
+ */
185
+ debug(module, message, elapsedTime, data) {
186
+ console.log(this.formatMessage(module, 'Debug', message, elapsedTime, COLORS.GRAY));
187
+ if (data) {
188
+ console.log(data);
189
+ }
190
+ }
191
+ }
192
+ /**
193
+ * Singleton logger instance
194
+ *
195
+ * Export as singleton to ensure consistent timestamp generation and
196
+ * single source of truth for logging configuration across all agents.
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * import { logger, MODULE_AGENT } from 'agents-library';
201
+ * logger.info(MODULE_AGENT, 'Application started');
202
+ * ```
203
+ */
204
+ export const logger = new Logger();
205
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,gBAAyB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1,186 @@
1
+ /**
2
+ * Timer Utility for Performance Tracking
3
+ * =====================================
4
+ *
5
+ * Provides named timer tracking with human-readable elapsed time formatting.
6
+ * Tracks multiple concurrent timers and formats output as: +Nms, +Ns, +Nm
7
+ *
8
+ * This timer is shared across all KĀDI agents (template-agent-typescript,
9
+ * agent-producer, agent-artist, etc.) for consistent performance tracking
10
+ * and elapsed time display in logs.
11
+ *
12
+ * Usage:
13
+ * ```typescript
14
+ * import { timer } from 'agents-library';
15
+ *
16
+ * // Start a timer
17
+ * timer.start('main');
18
+ *
19
+ * // Later, get elapsed time formatted string
20
+ * logger.info(MODULE_AGENT, 'Application started', timer.elapsed('main'));
21
+ * // Output: [template-agent] [14:32:45.123] Info: Application started +150ms
22
+ *
23
+ * // Even later, elapsed time shows progression
24
+ * logger.info(MODULE_AGENT, 'Connected to broker', timer.elapsed('main'));
25
+ * // Output: [template-agent] [14:32:48.234] Info: Connected to broker +3.1s
26
+ *
27
+ * // Start nested timer for operation-specific tracking
28
+ * timer.start('file-write');
29
+ * // ... perform file operation ...
30
+ * logger.info(MODULE_AGENT, 'File written', timer.elapsed('file-write'));
31
+ *
32
+ * // Reset timer to zero
33
+ * timer.reset('main');
34
+ *
35
+ * // Stop and remove timer from tracking
36
+ * timer.stop('main');
37
+ * ```
38
+ *
39
+ * @module timer
40
+ */
41
+ /**
42
+ * Timer class for tracking named elapsed times with human-readable formatting.
43
+ *
44
+ * Maintains a Map of named timers, each storing a start timestamp.
45
+ * Provides methods to start, check elapsed time, reset, and stop timers.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const timer = new Timer();
50
+ * timer.start('operation');
51
+ * // ... do work ...
52
+ * const elapsed = timer.elapsed('operation'); // Returns '+150ms', '+2.4s', etc.
53
+ * ```
54
+ */
55
+ declare class Timer {
56
+ /**
57
+ * Map of timer names to start timestamps (milliseconds since epoch)
58
+ *
59
+ * Each named timer stores the timestamp when it was started.
60
+ * Elapsed time is calculated as: now - startTime
61
+ *
62
+ * @private
63
+ */
64
+ private timers;
65
+ /**
66
+ * Start a named timer
67
+ *
68
+ * If a timer with the same name already exists, it is reset to the current time.
69
+ * This allows restarting a timer without stopping it first.
70
+ *
71
+ * Uses performance.now() for high-resolution timing when available,
72
+ * falls back to Date.now() for millisecond precision.
73
+ *
74
+ * @param name - Unique name for this timer (e.g., 'main', 'file-write', 'api-call')
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * timer.start('database-query');
79
+ * // Timer is now tracking
80
+ * ```
81
+ */
82
+ start(name: string): void;
83
+ /**
84
+ * Get elapsed time for a named timer as a human-readable formatted string
85
+ *
86
+ * Formats the elapsed time automatically:
87
+ * - Less than 1 second: '+Nms' (milliseconds)
88
+ * - 1 second to 59.9 seconds: '+Ns' (seconds with 1 decimal)
89
+ * - 1 minute or more: '+Nm' (minutes with 1 decimal)
90
+ *
91
+ * If the timer doesn't exist, returns '+0ms' as a safe fallback.
92
+ *
93
+ * @param name - Name of the timer to check
94
+ * @returns Formatted elapsed time string (e.g., '+150ms', '+2.4s', '+1.2m')
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * timer.start('process');
99
+ * setTimeout(() => {
100
+ * console.log(timer.elapsed('process')); // After 1.5s: '+1.5s'
101
+ * }, 1500);
102
+ * ```
103
+ */
104
+ elapsed(name: string): string;
105
+ /**
106
+ * Reset a named timer to the current time
107
+ *
108
+ * This is equivalent to stopping and immediately restarting the timer.
109
+ * The elapsed time will be zero after reset.
110
+ *
111
+ * If the timer doesn't exist, it is created with the current time.
112
+ *
113
+ * @param name - Name of the timer to reset
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * timer.start('attempt');
118
+ * // ... some work ...
119
+ * timer.reset('attempt'); // Restart the timer
120
+ * // ... more work ...
121
+ * console.log(timer.elapsed('attempt')); // Only counts time after reset
122
+ * ```
123
+ */
124
+ reset(name: string): void;
125
+ /**
126
+ * Stop a named timer and remove it from tracking
127
+ *
128
+ * After stopping, calling elapsed() for this timer will return '+0ms'.
129
+ * To resume tracking, call start() again.
130
+ *
131
+ * @param name - Name of the timer to stop
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * timer.start('temp-operation');
136
+ * // ... work ...
137
+ * timer.stop('temp-operation'); // Stops tracking
138
+ * console.log(timer.elapsed('temp-operation')); // Returns '+0ms'
139
+ * ```
140
+ */
141
+ stop(name: string): void;
142
+ /**
143
+ * Get current time in milliseconds
144
+ *
145
+ * Uses performance.now() for high-resolution timing when available
146
+ * (typical in Node.js 8.5.0+), falls back to Date.now() for millisecond precision.
147
+ *
148
+ * @private
149
+ * @returns Current time in milliseconds since timer epoch started
150
+ */
151
+ private getCurrentTime;
152
+ }
153
+ /**
154
+ * Singleton timer instance
155
+ *
156
+ * Export as singleton to ensure:
157
+ * 1. Single source of truth for all timers across the application
158
+ * 2. Consistent time reference points for all agents
159
+ * 3. Ability to track global and operation-specific timers
160
+ *
161
+ * This follows the same pattern as logger.ts for consistency.
162
+ *
163
+ * @example
164
+ * ```typescript
165
+ * import { timer } from 'agents-library';
166
+ *
167
+ * timer.start('app');
168
+ * // ... application runs ...
169
+ * logger.info(MODULE_AGENT, 'Status', timer.elapsed('app'));
170
+ * ```
171
+ */
172
+ export declare const timer: Timer;
173
+ /**
174
+ * Timer class export for TypeScript type annotations
175
+ *
176
+ * Use this type when you need to pass the timer or its type as a parameter.
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * function logWithTiming(timerInstance: Timer, name: string) {
181
+ * console.log(timerInstance.elapsed(name));
182
+ * }
183
+ * ```
184
+ */
185
+ export type { Timer };
186
+ //# sourceMappingURL=timer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../../src/utils/timer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH;;;;;;;;;;;;;GAaG;AACH,cAAM,KAAK;IACT;;;;;;;OAOG;IACH,OAAO,CAAC,MAAM,CAAkC;IAEhD;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKzB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IA0B7B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKzB;;;;;;;;;;;;;;;OAeG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAIxB;;;;;;;;OAQG;IACH,OAAO,CAAC,cAAc;CAQvB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,KAAK,OAAc,CAAC;AAEjC;;;;;;;;;;;GAWG;AACH,YAAY,EAAE,KAAK,EAAE,CAAC"}
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Timer Utility for Performance Tracking
3
+ * =====================================
4
+ *
5
+ * Provides named timer tracking with human-readable elapsed time formatting.
6
+ * Tracks multiple concurrent timers and formats output as: +Nms, +Ns, +Nm
7
+ *
8
+ * This timer is shared across all KĀDI agents (template-agent-typescript,
9
+ * agent-producer, agent-artist, etc.) for consistent performance tracking
10
+ * and elapsed time display in logs.
11
+ *
12
+ * Usage:
13
+ * ```typescript
14
+ * import { timer } from 'agents-library';
15
+ *
16
+ * // Start a timer
17
+ * timer.start('main');
18
+ *
19
+ * // Later, get elapsed time formatted string
20
+ * logger.info(MODULE_AGENT, 'Application started', timer.elapsed('main'));
21
+ * // Output: [template-agent] [14:32:45.123] Info: Application started +150ms
22
+ *
23
+ * // Even later, elapsed time shows progression
24
+ * logger.info(MODULE_AGENT, 'Connected to broker', timer.elapsed('main'));
25
+ * // Output: [template-agent] [14:32:48.234] Info: Connected to broker +3.1s
26
+ *
27
+ * // Start nested timer for operation-specific tracking
28
+ * timer.start('file-write');
29
+ * // ... perform file operation ...
30
+ * logger.info(MODULE_AGENT, 'File written', timer.elapsed('file-write'));
31
+ *
32
+ * // Reset timer to zero
33
+ * timer.reset('main');
34
+ *
35
+ * // Stop and remove timer from tracking
36
+ * timer.stop('main');
37
+ * ```
38
+ *
39
+ * @module timer
40
+ */
41
+ /**
42
+ * Timer class for tracking named elapsed times with human-readable formatting.
43
+ *
44
+ * Maintains a Map of named timers, each storing a start timestamp.
45
+ * Provides methods to start, check elapsed time, reset, and stop timers.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const timer = new Timer();
50
+ * timer.start('operation');
51
+ * // ... do work ...
52
+ * const elapsed = timer.elapsed('operation'); // Returns '+150ms', '+2.4s', etc.
53
+ * ```
54
+ */
55
+ class Timer {
56
+ /**
57
+ * Map of timer names to start timestamps (milliseconds since epoch)
58
+ *
59
+ * Each named timer stores the timestamp when it was started.
60
+ * Elapsed time is calculated as: now - startTime
61
+ *
62
+ * @private
63
+ */
64
+ timers = new Map();
65
+ /**
66
+ * Start a named timer
67
+ *
68
+ * If a timer with the same name already exists, it is reset to the current time.
69
+ * This allows restarting a timer without stopping it first.
70
+ *
71
+ * Uses performance.now() for high-resolution timing when available,
72
+ * falls back to Date.now() for millisecond precision.
73
+ *
74
+ * @param name - Unique name for this timer (e.g., 'main', 'file-write', 'api-call')
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * timer.start('database-query');
79
+ * // Timer is now tracking
80
+ * ```
81
+ */
82
+ start(name) {
83
+ const now = this.getCurrentTime();
84
+ this.timers.set(name, now);
85
+ }
86
+ /**
87
+ * Get elapsed time for a named timer as a human-readable formatted string
88
+ *
89
+ * Formats the elapsed time automatically:
90
+ * - Less than 1 second: '+Nms' (milliseconds)
91
+ * - 1 second to 59.9 seconds: '+Ns' (seconds with 1 decimal)
92
+ * - 1 minute or more: '+Nm' (minutes with 1 decimal)
93
+ *
94
+ * If the timer doesn't exist, returns '+0ms' as a safe fallback.
95
+ *
96
+ * @param name - Name of the timer to check
97
+ * @returns Formatted elapsed time string (e.g., '+150ms', '+2.4s', '+1.2m')
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * timer.start('process');
102
+ * setTimeout(() => {
103
+ * console.log(timer.elapsed('process')); // After 1.5s: '+1.5s'
104
+ * }, 1500);
105
+ * ```
106
+ */
107
+ elapsed(name) {
108
+ const startTime = this.timers.get(name);
109
+ // Timer doesn't exist - return safe fallback
110
+ if (startTime === undefined) {
111
+ return '+0ms';
112
+ }
113
+ const now = this.getCurrentTime();
114
+ const elapsedMs = now - startTime;
115
+ // Format based on magnitude
116
+ if (elapsedMs < 1000) {
117
+ // Less than 1 second: show milliseconds
118
+ return `+${Math.round(elapsedMs)}ms`;
119
+ }
120
+ else if (elapsedMs < 60000) {
121
+ // Less than 1 minute: show seconds with 1 decimal place
122
+ const seconds = elapsedMs / 1000;
123
+ return `+${seconds.toFixed(1)}s`;
124
+ }
125
+ else {
126
+ // 1 minute or more: show minutes with 1 decimal place
127
+ const minutes = elapsedMs / 60000;
128
+ return `+${minutes.toFixed(1)}m`;
129
+ }
130
+ }
131
+ /**
132
+ * Reset a named timer to the current time
133
+ *
134
+ * This is equivalent to stopping and immediately restarting the timer.
135
+ * The elapsed time will be zero after reset.
136
+ *
137
+ * If the timer doesn't exist, it is created with the current time.
138
+ *
139
+ * @param name - Name of the timer to reset
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * timer.start('attempt');
144
+ * // ... some work ...
145
+ * timer.reset('attempt'); // Restart the timer
146
+ * // ... more work ...
147
+ * console.log(timer.elapsed('attempt')); // Only counts time after reset
148
+ * ```
149
+ */
150
+ reset(name) {
151
+ const now = this.getCurrentTime();
152
+ this.timers.set(name, now);
153
+ }
154
+ /**
155
+ * Stop a named timer and remove it from tracking
156
+ *
157
+ * After stopping, calling elapsed() for this timer will return '+0ms'.
158
+ * To resume tracking, call start() again.
159
+ *
160
+ * @param name - Name of the timer to stop
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * timer.start('temp-operation');
165
+ * // ... work ...
166
+ * timer.stop('temp-operation'); // Stops tracking
167
+ * console.log(timer.elapsed('temp-operation')); // Returns '+0ms'
168
+ * ```
169
+ */
170
+ stop(name) {
171
+ this.timers.delete(name);
172
+ }
173
+ /**
174
+ * Get current time in milliseconds
175
+ *
176
+ * Uses performance.now() for high-resolution timing when available
177
+ * (typical in Node.js 8.5.0+), falls back to Date.now() for millisecond precision.
178
+ *
179
+ * @private
180
+ * @returns Current time in milliseconds since timer epoch started
181
+ */
182
+ getCurrentTime() {
183
+ // Try to use performance.now() for higher resolution timing
184
+ if (typeof performance !== 'undefined' && performance.now) {
185
+ return performance.now();
186
+ }
187
+ // Fallback to Date.now() for millisecond precision
188
+ return Date.now();
189
+ }
190
+ }
191
+ /**
192
+ * Singleton timer instance
193
+ *
194
+ * Export as singleton to ensure:
195
+ * 1. Single source of truth for all timers across the application
196
+ * 2. Consistent time reference points for all agents
197
+ * 3. Ability to track global and operation-specific timers
198
+ *
199
+ * This follows the same pattern as logger.ts for consistency.
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * import { timer } from 'agents-library';
204
+ *
205
+ * timer.start('app');
206
+ * // ... application runs ...
207
+ * logger.info(MODULE_AGENT, 'Status', timer.elapsed('app'));
208
+ * ```
209
+ */
210
+ export const timer = new Timer();
211
+ //# sourceMappingURL=timer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timer.js","sourceRoot":"","sources":["../../src/utils/timer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH;;;;;;;;;;;;;GAaG;AACH,MAAM,KAAK;IACT;;;;;;;OAOG;IACK,MAAM,GAAwB,IAAI,GAAG,EAAE,CAAC;IAEhD;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,IAAY;QAChB,MAAM,GAAG,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;QAClC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH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