@agentxjs/core 1.9.5-dev → 1.9.7-dev

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.
Files changed (80) hide show
  1. package/dist/Processor-DT0N1qI6.d.ts +64 -0
  2. package/dist/agent/engine/internal/index.d.ts +223 -0
  3. package/dist/agent/engine/internal/index.js +24 -0
  4. package/dist/agent/engine/internal/index.js.map +1 -0
  5. package/dist/agent/engine/mealy/index.d.ts +157 -0
  6. package/dist/agent/engine/mealy/index.js +26 -0
  7. package/dist/agent/engine/mealy/index.js.map +1 -0
  8. package/dist/agent/index.d.ts +244 -0
  9. package/dist/agent/index.js +66 -0
  10. package/dist/agent/index.js.map +1 -0
  11. package/dist/agent/types/index.d.ts +322 -0
  12. package/dist/agent/types/index.js +12 -0
  13. package/dist/agent/types/index.js.map +1 -0
  14. package/dist/base-m40r3Qgu.d.ts +157 -0
  15. package/dist/bus-uF1DM2ox.d.ts +906 -0
  16. package/dist/chunk-7D4SUZUM.js +38 -0
  17. package/dist/chunk-7D4SUZUM.js.map +1 -0
  18. package/dist/chunk-7ZDX3O6I.js +173 -0
  19. package/dist/chunk-7ZDX3O6I.js.map +1 -0
  20. package/dist/chunk-E5FPOAPO.js +123 -0
  21. package/dist/chunk-E5FPOAPO.js.map +1 -0
  22. package/dist/chunk-EKHT54KN.js +272 -0
  23. package/dist/chunk-EKHT54KN.js.map +1 -0
  24. package/dist/chunk-I7GYR3MN.js +502 -0
  25. package/dist/chunk-I7GYR3MN.js.map +1 -0
  26. package/dist/chunk-K6WXQ2RW.js +38 -0
  27. package/dist/chunk-K6WXQ2RW.js.map +1 -0
  28. package/dist/chunk-RL3JRNXM.js +3 -0
  29. package/dist/chunk-RL3JRNXM.js.map +1 -0
  30. package/dist/chunk-TBU7FFZT.js +543 -0
  31. package/dist/chunk-TBU7FFZT.js.map +1 -0
  32. package/dist/combinators-nEa5dD0T.d.ts +271 -0
  33. package/dist/common/index.d.ts +1 -0
  34. package/dist/common/index.js +2 -0
  35. package/dist/common/index.js.map +1 -0
  36. package/dist/common/logger/index.d.ts +163 -0
  37. package/dist/common/logger/index.js +184 -0
  38. package/dist/common/logger/index.js.map +1 -0
  39. package/dist/container/index.d.ts +110 -0
  40. package/dist/container/index.js +127 -0
  41. package/dist/container/index.js.map +1 -0
  42. package/dist/driver/index.d.ts +266 -0
  43. package/dist/driver/index.js +1 -0
  44. package/dist/driver/index.js.map +1 -0
  45. package/dist/event/index.d.ts +55 -0
  46. package/dist/event/index.js +60 -0
  47. package/dist/event/index.js.map +1 -0
  48. package/dist/event/types/index.d.ts +1149 -0
  49. package/dist/event/types/index.js +56 -0
  50. package/dist/event/types/index.js.map +1 -0
  51. package/dist/event-CDuTzs__.d.ts +296 -0
  52. package/dist/image/index.d.ts +112 -0
  53. package/dist/image/index.js +151 -0
  54. package/dist/image/index.js.map +1 -0
  55. package/dist/index.d.ts +8 -0
  56. package/dist/index.js +67 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/message-BMrMm1pq.d.ts +305 -0
  59. package/dist/mq/index.d.ts +165 -0
  60. package/dist/mq/index.js +37 -0
  61. package/dist/mq/index.js.map +1 -0
  62. package/dist/network/index.d.ts +567 -0
  63. package/dist/network/index.js +435 -0
  64. package/dist/network/index.js.map +1 -0
  65. package/dist/persistence/index.d.ts +155 -0
  66. package/dist/persistence/index.js +1 -0
  67. package/dist/persistence/index.js.map +1 -0
  68. package/dist/runtime/index.d.ts +240 -0
  69. package/dist/runtime/index.js +347 -0
  70. package/dist/runtime/index.js.map +1 -0
  71. package/dist/session/index.d.ts +92 -0
  72. package/dist/session/index.js +56 -0
  73. package/dist/session/index.js.map +1 -0
  74. package/dist/workspace/index.d.ts +111 -0
  75. package/dist/workspace/index.js +1 -0
  76. package/dist/workspace/index.js.map +1 -0
  77. package/dist/wrapper-Y3UTVU2E.js +3635 -0
  78. package/dist/wrapper-Y3UTVU2E.js.map +1 -0
  79. package/package.json +73 -14
  80. package/tsconfig.json +0 -10
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ import {
2
+ AgentStateMachine,
3
+ MealyMachine,
4
+ agentProcessor,
5
+ createAgent,
6
+ createInitialAgentEngineState,
7
+ createMealyMachine
8
+ } from "./chunk-TBU7FFZT.js";
9
+ import {
10
+ createInitialMessageAssemblerState,
11
+ createInitialStateEventProcessorContext,
12
+ createInitialTurnTrackerState,
13
+ messageAssemblerProcessor,
14
+ messageAssemblerProcessorDef,
15
+ stateEventProcessor,
16
+ stateEventProcessorDef,
17
+ turnTrackerProcessor,
18
+ turnTrackerProcessorDef
19
+ } from "./chunk-I7GYR3MN.js";
20
+ import {
21
+ MemoryStore,
22
+ chainProcessors,
23
+ combineInitialStates,
24
+ combineProcessors,
25
+ filterProcessor,
26
+ identityProcessor,
27
+ mapOutput,
28
+ withLogging
29
+ } from "./chunk-EKHT54KN.js";
30
+ import {
31
+ isMessageEvent,
32
+ isStateEvent,
33
+ isTurnEvent
34
+ } from "./chunk-K6WXQ2RW.js";
35
+ import "./chunk-7ZDX3O6I.js";
36
+ import "./chunk-E5FPOAPO.js";
37
+ import "./chunk-7D4SUZUM.js";
38
+ import "./chunk-RL3JRNXM.js";
39
+ export {
40
+ AgentStateMachine,
41
+ MealyMachine,
42
+ MemoryStore,
43
+ agentProcessor,
44
+ chainProcessors,
45
+ combineInitialStates,
46
+ combineProcessors,
47
+ createAgent,
48
+ createInitialAgentEngineState,
49
+ createInitialMessageAssemblerState,
50
+ createInitialStateEventProcessorContext,
51
+ createInitialTurnTrackerState,
52
+ createMealyMachine,
53
+ filterProcessor,
54
+ identityProcessor,
55
+ isMessageEvent,
56
+ isStateEvent,
57
+ isTurnEvent,
58
+ mapOutput,
59
+ messageAssemblerProcessor,
60
+ messageAssemblerProcessorDef,
61
+ stateEventProcessor,
62
+ stateEventProcessorDef,
63
+ turnTrackerProcessor,
64
+ turnTrackerProcessorDef,
65
+ withLogging
66
+ };
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,305 @@
1
+ /**
2
+ * Message Types - Content Parts and Message Types
3
+ *
4
+ * This file defines:
5
+ * - Content Parts: TextPart, ThinkingPart, ImagePart, FilePart, ToolCallPart, ToolResultPart
6
+ * - Message Types: UserMessage, AssistantMessage, ToolCallMessage, ToolResultMessage, ErrorMessage
7
+ * - Message Role and Subtype discriminators
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+ /**
12
+ * Text Part
13
+ *
14
+ * Plain text content in a message.
15
+ */
16
+ interface TextPart {
17
+ /** Content type discriminator */
18
+ type: "text";
19
+ /** The text content (supports Markdown) */
20
+ text: string;
21
+ }
22
+ /**
23
+ * Thinking Part
24
+ *
25
+ * AI's reasoning/thinking process (extended thinking).
26
+ */
27
+ interface ThinkingPart {
28
+ /** Content type discriminator */
29
+ type: "thinking";
30
+ /** The reasoning text */
31
+ reasoning: string;
32
+ /** Tokens used for thinking (optional) */
33
+ tokenCount?: number;
34
+ }
35
+ /**
36
+ * Image Part
37
+ *
38
+ * Image content in a message.
39
+ */
40
+ interface ImagePart {
41
+ /** Content type discriminator */
42
+ type: "image";
43
+ /** Image data (base64-encoded string or URL) */
44
+ data: string;
45
+ /** Image MIME type */
46
+ mediaType: "image/png" | "image/jpeg" | "image/gif" | "image/webp";
47
+ /** Optional image name/filename */
48
+ name?: string;
49
+ }
50
+ /**
51
+ * File Part
52
+ *
53
+ * File attachment in a message (PDF, documents, etc.).
54
+ */
55
+ interface FilePart {
56
+ /** Content type discriminator */
57
+ type: "file";
58
+ /** File data (base64-encoded string or URL) */
59
+ data: string;
60
+ /** File MIME type (IANA media type) */
61
+ mediaType: string;
62
+ /** Optional filename */
63
+ filename?: string;
64
+ }
65
+ /**
66
+ * Tool Call Part
67
+ *
68
+ * AI's request to invoke a tool.
69
+ */
70
+ interface ToolCallPart {
71
+ /** Content type discriminator */
72
+ type: "tool-call";
73
+ /** Unique identifier for this tool call */
74
+ id: string;
75
+ /** Tool name */
76
+ name: string;
77
+ /** Tool input parameters */
78
+ input: Record<string, unknown>;
79
+ }
80
+ /**
81
+ * Tool Result Output
82
+ *
83
+ * Enhanced tool result format supporting multiple output types.
84
+ * Based on Vercel AI SDK and industry best practices.
85
+ */
86
+ type ToolResultOutput =
87
+ /**
88
+ * Plain text result
89
+ */
90
+ {
91
+ type: "text";
92
+ value: string;
93
+ }
94
+ /**
95
+ * JSON result
96
+ */
97
+ | {
98
+ type: "json";
99
+ value: unknown;
100
+ }
101
+ /**
102
+ * Text error
103
+ */
104
+ | {
105
+ type: "error-text";
106
+ value: string;
107
+ }
108
+ /**
109
+ * JSON error
110
+ */
111
+ | {
112
+ type: "error-json";
113
+ value: unknown;
114
+ }
115
+ /**
116
+ * User denied tool execution
117
+ */
118
+ | {
119
+ type: "execution-denied";
120
+ reason?: string;
121
+ }
122
+ /**
123
+ * Rich content (multiple parts)
124
+ */
125
+ | {
126
+ type: "content";
127
+ value: Array<TextPart | ImagePart | FilePart>;
128
+ };
129
+ /**
130
+ * Tool Result Part
131
+ *
132
+ * Result of tool execution.
133
+ */
134
+ interface ToolResultPart {
135
+ /** Content type discriminator */
136
+ type: "tool-result";
137
+ /** Tool call ID this result corresponds to */
138
+ id: string;
139
+ /** Tool name */
140
+ name: string;
141
+ /** Tool execution output */
142
+ output: ToolResultOutput;
143
+ }
144
+ /**
145
+ * Content Part
146
+ *
147
+ * Discriminated union of all content part types.
148
+ * Used in messages to support multi-modal and complex content.
149
+ */
150
+ type ContentPart = TextPart | ThinkingPart | ImagePart | FilePart | ToolCallPart | ToolResultPart;
151
+ /**
152
+ * User Content Part
153
+ *
154
+ * Subset of ContentPart types that users can send in messages.
155
+ * - TextPart: Plain text content
156
+ * - ImagePart: Image attachments (JPEG, PNG, GIF, WebP)
157
+ * - FilePart: File attachments (PDF, documents, etc.)
158
+ */
159
+ type UserContentPart = TextPart | ImagePart | FilePart;
160
+ /**
161
+ * Message Role
162
+ *
163
+ * Represents who sent the message in the conversation.
164
+ * Five fundamental roles in the conversation.
165
+ */
166
+ type MessageRole = "user" | "assistant" | "tool" | "system" | "error";
167
+ /**
168
+ * Message Subtype
169
+ *
170
+ * Represents the specific type/category of the message.
171
+ * Used together with role for serialization and type discrimination.
172
+ */
173
+ type MessageSubtype = "user" | "assistant" | "tool-call" | "tool-result" | "error";
174
+ /**
175
+ * Token usage information
176
+ */
177
+ interface TokenUsage {
178
+ inputTokens: number;
179
+ outputTokens: number;
180
+ totalTokens?: number;
181
+ }
182
+ /**
183
+ * User Message
184
+ *
185
+ * Message sent by the user.
186
+ * Can contain simple text or rich content (text, images, files).
187
+ */
188
+ interface UserMessage {
189
+ /** Unique identifier */
190
+ id: string;
191
+ /** Message role */
192
+ role: "user";
193
+ /** Message subtype for serialization */
194
+ subtype: "user";
195
+ /** Message content - can be simple string or array of parts */
196
+ content: string | UserContentPart[];
197
+ /** When this message was created (Unix timestamp in milliseconds) */
198
+ timestamp: number;
199
+ /** Parent message ID for threading (optional) */
200
+ parentId?: string;
201
+ }
202
+ /**
203
+ * Assistant Message
204
+ *
205
+ * Message generated by the AI assistant.
206
+ * Contains text, thinking process, or files.
207
+ *
208
+ * Note: Tool calls are separate - use ToolCallMessage for tool invocations.
209
+ */
210
+ interface AssistantMessage {
211
+ /** Unique identifier */
212
+ id: string;
213
+ /** Message role */
214
+ role: "assistant";
215
+ /** Message subtype for serialization */
216
+ subtype: "assistant";
217
+ /** Message content - can be simple string or array of parts */
218
+ content: string | Array<TextPart | ThinkingPart | FilePart>;
219
+ /** When this message was created (Unix timestamp in milliseconds) */
220
+ timestamp: number;
221
+ /** Parent message ID for threading (optional) */
222
+ parentId?: string;
223
+ /** Token usage for this AI response (optional) */
224
+ usage?: TokenUsage;
225
+ }
226
+ /**
227
+ * Tool Call Message
228
+ *
229
+ * Represents AI's request to invoke a tool.
230
+ * Emitted when tool call parameters are fully assembled.
231
+ *
232
+ * Subject: Assistant (AI decided to call a tool)
233
+ * Timing: At tool_use_content_block_stop
234
+ */
235
+ interface ToolCallMessage {
236
+ /** Unique message identifier */
237
+ id: string;
238
+ /** Message role - assistant initiates tool calls */
239
+ role: "assistant";
240
+ /** Message subtype for serialization */
241
+ subtype: "tool-call";
242
+ /** Tool call details */
243
+ toolCall: ToolCallPart;
244
+ /** When this message was created (Unix timestamp in milliseconds) */
245
+ timestamp: number;
246
+ /** Parent message ID (the assistant message that triggered this) */
247
+ parentId?: string;
248
+ }
249
+ /**
250
+ * Tool Result Message
251
+ *
252
+ * Represents the result of tool execution.
253
+ * Emitted after tool execution completes.
254
+ *
255
+ * Subject: Tool (execution completed with result)
256
+ * Timing: At tool_result event
257
+ */
258
+ interface ToolResultMessage {
259
+ /** Unique message identifier */
260
+ id: string;
261
+ /** Message role - tool returns results */
262
+ role: "tool";
263
+ /** Message subtype for serialization */
264
+ subtype: "tool-result";
265
+ /** Tool result details */
266
+ toolResult: ToolResultPart;
267
+ /** ID of the corresponding tool call */
268
+ toolCallId: string;
269
+ /** When this message was created (Unix timestamp in milliseconds) */
270
+ timestamp: number;
271
+ }
272
+ /**
273
+ * Error Message
274
+ *
275
+ * Message representing an error that occurred during conversation.
276
+ * Displayed in the chat history so users can see what went wrong.
277
+ */
278
+ interface ErrorMessage {
279
+ /** Unique identifier */
280
+ id: string;
281
+ /** Message role */
282
+ role: "error";
283
+ /** Message subtype for serialization */
284
+ subtype: "error";
285
+ /** Error message content (human-readable) */
286
+ content: string;
287
+ /** Error code (e.g., "rate_limit_error", "api_error") */
288
+ errorCode?: string;
289
+ /** When this error occurred (Unix timestamp in milliseconds) */
290
+ timestamp: number;
291
+ /** Parent message ID for threading (optional) */
292
+ parentId?: string;
293
+ }
294
+ /**
295
+ * Message
296
+ *
297
+ * Discriminated union of all message types.
298
+ * Use `subtype` field for precise type discrimination.
299
+ *
300
+ * Role: Who sent it (user, assistant, tool, system, error)
301
+ * Subtype: What type of message (user, assistant, tool-call, tool-result, error)
302
+ */
303
+ type Message = UserMessage | AssistantMessage | ToolCallMessage | ToolResultMessage | ErrorMessage;
304
+
305
+ export type { AssistantMessage as A, ContentPart as C, ErrorMessage as E, FilePart as F, ImagePart as I, Message as M, ToolCallMessage as T, UserMessage as U, ToolResultMessage as a, TokenUsage as b, UserContentPart as c, TextPart as d, ThinkingPart as e, ToolCallPart as f, ToolResultOutput as g, ToolResultPart as h, MessageRole as i, MessageSubtype as j };
@@ -0,0 +1,165 @@
1
+ /**
2
+ * MessageQueue - Reliable message delivery with persistence guarantee
3
+ *
4
+ * Standard interface for message queue implementations:
5
+ * - In-memory pub/sub for real-time delivery (RxJS)
6
+ * - Persistence for recovery guarantee (platform-specific)
7
+ * - Consumer offset tracking for at-least-once delivery
8
+ *
9
+ * Platform implementations:
10
+ * - Node.js: SQLite
11
+ * - Cloudflare Workers: Durable Objects Storage
12
+ * - Browser: IndexedDB
13
+ */
14
+ /**
15
+ * QueueEntry - A single entry in the message queue
16
+ */
17
+ interface QueueEntry {
18
+ /**
19
+ * Unique offset for this entry (monotonically increasing)
20
+ * Format: "{timestamp_base36}-{sequence}"
21
+ * Example: "lq5x4g2-0001"
22
+ */
23
+ readonly offset: string;
24
+ /**
25
+ * Topic this entry belongs to (e.g., sessionId, agentId)
26
+ */
27
+ readonly topic: string;
28
+ /**
29
+ * The actual event payload
30
+ */
31
+ readonly event: unknown;
32
+ /**
33
+ * Timestamp when the event was published (Unix milliseconds)
34
+ */
35
+ readonly timestamp: number;
36
+ }
37
+ /**
38
+ * QueueConfig - Platform-agnostic configuration
39
+ *
40
+ * Platform-specific options (path, namespace, etc.) are handled by Provider.
41
+ */
42
+ interface QueueConfig {
43
+ /**
44
+ * Message retention timeout (milliseconds)
45
+ * Messages older than this may be deleted during cleanup
46
+ * @default 86400000 (24 hours)
47
+ */
48
+ retentionMs?: number;
49
+ /**
50
+ * Maximum entries per topic (optional)
51
+ * When exceeded, oldest entries may be removed
52
+ */
53
+ maxEntriesPerTopic?: number;
54
+ }
55
+ /**
56
+ * Unsubscribe function - call to stop receiving messages
57
+ */
58
+ type Unsubscribe = () => void;
59
+ /**
60
+ * MessageQueue - Standard interface for message queue
61
+ *
62
+ * All methods that involve storage are async to support all platforms.
63
+ */
64
+ interface MessageQueue {
65
+ /**
66
+ * Publish an event to a topic
67
+ *
68
+ * - Persists to storage (async)
69
+ * - Broadcasts to all subscribers (in-memory, sync)
70
+ *
71
+ * @param topic - Topic identifier (e.g., sessionId)
72
+ * @param event - Event payload
73
+ * @returns Offset of the published entry
74
+ */
75
+ publish(topic: string, event: unknown): Promise<string>;
76
+ /**
77
+ * Subscribe to real-time events on a topic
78
+ *
79
+ * Receives events published after subscription (in-memory pub/sub).
80
+ * For historical events, use recover() first.
81
+ *
82
+ * @param topic - Topic identifier
83
+ * @param handler - Callback for each event entry
84
+ * @returns Unsubscribe function
85
+ */
86
+ subscribe(topic: string, handler: (entry: QueueEntry) => void): Unsubscribe;
87
+ /**
88
+ * Acknowledge consumption (update consumer offset)
89
+ *
90
+ * Call this after successfully processing an event.
91
+ * The offset is persisted for recovery.
92
+ *
93
+ * @param consumerId - Consumer identifier (e.g., connectionId)
94
+ * @param topic - Topic identifier
95
+ * @param offset - Offset of the consumed entry
96
+ */
97
+ ack(consumerId: string, topic: string, offset: string): Promise<void>;
98
+ /**
99
+ * Get consumer's current offset
100
+ *
101
+ * @param consumerId - Consumer identifier
102
+ * @param topic - Topic identifier
103
+ * @returns Offset or null if consumer not found
104
+ */
105
+ getOffset(consumerId: string, topic: string): Promise<string | null>;
106
+ /**
107
+ * Recover historical events from persistence
108
+ *
109
+ * Used for reconnection recovery - fetches events after an offset.
110
+ *
111
+ * @param topic - Topic identifier
112
+ * @param afterOffset - Start offset (exclusive), omit for all history
113
+ * @param limit - Maximum entries to return (default: 1000)
114
+ * @returns Array of event entries
115
+ */
116
+ recover(topic: string, afterOffset?: string, limit?: number): Promise<QueueEntry[]>;
117
+ /**
118
+ * Close the message queue and release resources
119
+ */
120
+ close(): Promise<void>;
121
+ }
122
+ /**
123
+ * MessageQueueProvider - Factory for creating MessageQueue instances
124
+ *
125
+ * Each platform implements its own provider:
126
+ * - SqliteMessageQueueProvider (Node.js)
127
+ * - DurableMessageQueueProvider (Cloudflare Workers)
128
+ * - IndexedDBMessageQueueProvider (Browser)
129
+ */
130
+ interface MessageQueueProvider {
131
+ /**
132
+ * Create a MessageQueue instance
133
+ *
134
+ * @param config - Platform-agnostic configuration
135
+ * @returns MessageQueue instance
136
+ */
137
+ createQueue(config?: QueueConfig): Promise<MessageQueue>;
138
+ }
139
+
140
+ /**
141
+ * OffsetGenerator - Generates monotonically increasing offsets
142
+ *
143
+ * Format: "{timestamp_base36}-{sequence_padded}"
144
+ * Example: "lq5x4g2-0001"
145
+ *
146
+ * This format ensures:
147
+ * - Lexicographic ordering matches temporal ordering
148
+ * - Multiple events in same millisecond get unique offsets
149
+ * - Human-readable and compact
150
+ */
151
+ declare class OffsetGenerator {
152
+ private lastTimestamp;
153
+ private sequence;
154
+ /**
155
+ * Generate a new offset
156
+ */
157
+ generate(): string;
158
+ /**
159
+ * Compare two offsets
160
+ * @returns negative if a < b, 0 if a == b, positive if a > b
161
+ */
162
+ static compare(a: string, b: string): number;
163
+ }
164
+
165
+ export { type MessageQueue, type MessageQueueProvider, OffsetGenerator, type QueueConfig, type QueueEntry, type Unsubscribe };
@@ -0,0 +1,37 @@
1
+ import "../chunk-7D4SUZUM.js";
2
+
3
+ // src/mq/OffsetGenerator.ts
4
+ var OffsetGenerator = class {
5
+ lastTimestamp = 0;
6
+ sequence = 0;
7
+ /**
8
+ * Generate a new offset
9
+ */
10
+ generate() {
11
+ const now = Date.now();
12
+ if (now === this.lastTimestamp) {
13
+ this.sequence++;
14
+ } else {
15
+ this.lastTimestamp = now;
16
+ this.sequence = 0;
17
+ }
18
+ const timestampPart = now.toString(36);
19
+ const sequencePart = this.sequence.toString().padStart(4, "0");
20
+ return `${timestampPart}-${sequencePart}`;
21
+ }
22
+ /**
23
+ * Compare two offsets
24
+ * @returns negative if a < b, 0 if a == b, positive if a > b
25
+ */
26
+ static compare(a, b) {
27
+ const [aTime, aSeq] = a.split("-");
28
+ const [bTime, bSeq] = b.split("-");
29
+ const timeDiff = parseInt(aTime, 36) - parseInt(bTime, 36);
30
+ if (timeDiff !== 0) return timeDiff;
31
+ return parseInt(aSeq) - parseInt(bSeq);
32
+ }
33
+ };
34
+ export {
35
+ OffsetGenerator
36
+ };
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/mq/OffsetGenerator.ts"],"sourcesContent":["/**\n * OffsetGenerator - Generates monotonically increasing offsets\n *\n * Format: \"{timestamp_base36}-{sequence_padded}\"\n * Example: \"lq5x4g2-0001\"\n *\n * This format ensures:\n * - Lexicographic ordering matches temporal ordering\n * - Multiple events in same millisecond get unique offsets\n * - Human-readable and compact\n */\nexport class OffsetGenerator {\n private lastTimestamp = 0;\n private sequence = 0;\n\n /**\n * Generate a new offset\n */\n generate(): string {\n const now = Date.now();\n\n if (now === this.lastTimestamp) {\n this.sequence++;\n } else {\n this.lastTimestamp = now;\n this.sequence = 0;\n }\n\n const timestampPart = now.toString(36);\n const sequencePart = this.sequence.toString().padStart(4, \"0\");\n\n return `${timestampPart}-${sequencePart}`;\n }\n\n /**\n * Compare two offsets\n * @returns negative if a < b, 0 if a == b, positive if a > b\n */\n static compare(a: string, b: string): number {\n const [aTime, aSeq] = a.split(\"-\");\n const [bTime, bSeq] = b.split(\"-\");\n\n const timeDiff = parseInt(aTime, 36) - parseInt(bTime, 36);\n if (timeDiff !== 0) return timeDiff;\n\n return parseInt(aSeq) - parseInt(bSeq);\n }\n}\n"],"mappings":";;;AAWO,IAAM,kBAAN,MAAsB;AAAA,EACnB,gBAAgB;AAAA,EAChB,WAAW;AAAA;AAAA;AAAA;AAAA,EAKnB,WAAmB;AACjB,UAAM,MAAM,KAAK,IAAI;AAErB,QAAI,QAAQ,KAAK,eAAe;AAC9B,WAAK;AAAA,IACP,OAAO;AACL,WAAK,gBAAgB;AACrB,WAAK,WAAW;AAAA,IAClB;AAEA,UAAM,gBAAgB,IAAI,SAAS,EAAE;AACrC,UAAM,eAAe,KAAK,SAAS,SAAS,EAAE,SAAS,GAAG,GAAG;AAE7D,WAAO,GAAG,aAAa,IAAI,YAAY;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,QAAQ,GAAW,GAAmB;AAC3C,UAAM,CAAC,OAAO,IAAI,IAAI,EAAE,MAAM,GAAG;AACjC,UAAM,CAAC,OAAO,IAAI,IAAI,EAAE,MAAM,GAAG;AAEjC,UAAM,WAAW,SAAS,OAAO,EAAE,IAAI,SAAS,OAAO,EAAE;AACzD,QAAI,aAAa,EAAG,QAAO;AAE3B,WAAO,SAAS,IAAI,IAAI,SAAS,IAAI;AAAA,EACvC;AACF;","names":[]}