@axiom-lattice/protocols 2.1.6 → 2.1.8

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.
@@ -0,0 +1,394 @@
1
+ /**
2
+ * ScheduleLatticeProtocol
3
+ *
4
+ * Schedule Lattice protocol for delayed and recurring task execution management
5
+ * Supports persistence and recovery after service restart
6
+ * Supports both one-time delayed tasks and cron-style recurring tasks
7
+ */
8
+
9
+ import { BaseLatticeProtocol } from "./BaseLatticeProtocol";
10
+
11
+ /**
12
+ * Schedule service type enumeration
13
+ */
14
+ export enum ScheduleType {
15
+ MEMORY = "memory",
16
+ POSTGRES = "postgres",
17
+ REDIS = "redis",
18
+ }
19
+
20
+ /**
21
+ * Schedule execution type - one-time or recurring
22
+ */
23
+ export enum ScheduleExecutionType {
24
+ ONCE = "once", // Execute once at specified time
25
+ CRON = "cron", // Recurring based on cron expression
26
+ }
27
+
28
+ /**
29
+ * Task status enumeration
30
+ */
31
+ export enum ScheduledTaskStatus {
32
+ PENDING = "pending", // Waiting to be executed
33
+ RUNNING = "running", // Currently executing
34
+ COMPLETED = "completed", // Successfully completed (for ONCE type)
35
+ FAILED = "failed", // Execution failed
36
+ CANCELLED = "cancelled", // Manually cancelled
37
+ PAUSED = "paused", // Paused (for CRON type)
38
+ }
39
+
40
+ /**
41
+ * Schedule configuration interface
42
+ */
43
+ export interface ScheduleConfig {
44
+ name: string;
45
+ description: string;
46
+ type: ScheduleType;
47
+ storage?: ScheduleStorage; // Optional storage for persistence
48
+ options?: Record<string, any>;
49
+ }
50
+
51
+ /**
52
+ * Scheduled task definition - fully serializable
53
+ * Supports both one-time and cron-style recurring tasks
54
+ */
55
+ export interface ScheduledTaskDefinition {
56
+ taskId: string;
57
+ taskType: string; // Maps to a registered handler
58
+ payload: Record<string, any>; // JSON-serializable data passed to handler
59
+
60
+ // Context fields for querying
61
+ assistantId?: string; // Which assistant created/owns this task
62
+ threadId?: string; // Which thread this task belongs to
63
+
64
+ // Execution configuration
65
+ executionType: ScheduleExecutionType;
66
+
67
+ // For ONCE type - execute at specific time or after delay
68
+ executeAt?: number; // Timestamp when to execute
69
+ delayMs?: number; // Original delay in milliseconds (for reference)
70
+
71
+ // For CRON type - recurring schedule
72
+ cronExpression?: string; // Cron format: "0 9 * * *" (min hour day month weekday)
73
+ timezone?: string; // Timezone: "Asia/Shanghai", defaults to system timezone
74
+ nextRunAt?: number; // Next calculated execution time
75
+ lastRunAt?: number; // Last execution time
76
+
77
+ // Execution tracking
78
+ status: ScheduledTaskStatus;
79
+ runCount: number; // How many times executed
80
+ maxRuns?: number; // Max executions (null/undefined = infinite for cron, 1 for once)
81
+
82
+ // Error handling
83
+ retryCount: number; // Current retry count
84
+ maxRetries: number; // Maximum retry attempts
85
+ lastError?: string; // Last error message if failed
86
+
87
+ // Timestamps
88
+ createdAt: number;
89
+ updatedAt: number;
90
+ expiresAt?: number; // When to stop (for cron, optional)
91
+
92
+ metadata?: Record<string, any>; // Additional metadata
93
+ }
94
+
95
+ /**
96
+ * Task handler function type
97
+ */
98
+ export type TaskHandler = (
99
+ payload: Record<string, any>,
100
+ taskInfo: ScheduledTaskDefinition
101
+ ) => void | Promise<void>;
102
+
103
+ /**
104
+ * Options for scheduling a one-time task
105
+ */
106
+ export interface ScheduleOnceOptions {
107
+ executeAt?: number; // Absolute timestamp to execute
108
+ delayMs?: number; // OR relative delay from now
109
+ maxRetries?: number; // Max retry attempts (default: 0)
110
+ assistantId?: string; // Which assistant created/owns this task
111
+ threadId?: string; // Which thread this task belongs to
112
+ metadata?: Record<string, any>;
113
+ }
114
+
115
+ /**
116
+ * Options for scheduling a cron task
117
+ */
118
+ export interface ScheduleCronOptions {
119
+ cronExpression: string; // Cron expression: "0 9 * * *"
120
+ timezone?: string; // Timezone: "Asia/Shanghai"
121
+ maxRuns?: number; // Max executions (undefined = infinite)
122
+ expiresAt?: number; // Stop after this timestamp
123
+ maxRetries?: number; // Max retry attempts per run (default: 0)
124
+ assistantId?: string; // Which assistant created/owns this task
125
+ threadId?: string; // Which thread this task belongs to
126
+ metadata?: Record<string, any>;
127
+ }
128
+
129
+ /**
130
+ * Schedule storage interface for persistence
131
+ */
132
+ export interface ScheduleStorage {
133
+ /**
134
+ * Save a new task
135
+ */
136
+ save(task: ScheduledTaskDefinition): Promise<void>;
137
+
138
+ /**
139
+ * Get task by ID
140
+ */
141
+ get(taskId: string): Promise<ScheduledTaskDefinition | null>;
142
+
143
+ /**
144
+ * Update task
145
+ */
146
+ update(
147
+ taskId: string,
148
+ updates: Partial<ScheduledTaskDefinition>
149
+ ): Promise<void>;
150
+
151
+ /**
152
+ * Delete task
153
+ */
154
+ delete(taskId: string): Promise<void>;
155
+
156
+ /**
157
+ * Get all pending/active tasks (for recovery)
158
+ * Returns tasks with status: PENDING or PAUSED
159
+ */
160
+ getActiveTasks(): Promise<ScheduledTaskDefinition[]>;
161
+
162
+ /**
163
+ * Get tasks by type
164
+ */
165
+ getTasksByType(taskType: string): Promise<ScheduledTaskDefinition[]>;
166
+
167
+ /**
168
+ * Get tasks by status
169
+ */
170
+ getTasksByStatus(
171
+ status: ScheduledTaskStatus
172
+ ): Promise<ScheduledTaskDefinition[]>;
173
+
174
+ /**
175
+ * Get tasks by execution type
176
+ */
177
+ getTasksByExecutionType(
178
+ executionType: ScheduleExecutionType
179
+ ): Promise<ScheduledTaskDefinition[]>;
180
+
181
+ /**
182
+ * Get tasks by assistant ID
183
+ */
184
+ getTasksByAssistantId(
185
+ assistantId: string
186
+ ): Promise<ScheduledTaskDefinition[]>;
187
+
188
+ /**
189
+ * Get tasks by thread ID
190
+ */
191
+ getTasksByThreadId(threadId: string): Promise<ScheduledTaskDefinition[]>;
192
+
193
+ /**
194
+ * Get all tasks (with optional filters)
195
+ */
196
+ getAllTasks(filters?: {
197
+ status?: ScheduledTaskStatus;
198
+ executionType?: ScheduleExecutionType;
199
+ taskType?: string;
200
+ assistantId?: string;
201
+ threadId?: string;
202
+ limit?: number;
203
+ offset?: number;
204
+ }): Promise<ScheduledTaskDefinition[]>;
205
+
206
+ /**
207
+ * Count tasks (with optional filters)
208
+ */
209
+ countTasks(filters?: {
210
+ status?: ScheduledTaskStatus;
211
+ executionType?: ScheduleExecutionType;
212
+ taskType?: string;
213
+ assistantId?: string;
214
+ threadId?: string;
215
+ }): Promise<number>;
216
+
217
+ /**
218
+ * Delete completed/cancelled tasks older than specified time
219
+ * Useful for cleanup
220
+ */
221
+ deleteOldTasks(olderThanMs: number): Promise<number>;
222
+ }
223
+
224
+ /**
225
+ * Schedule client interface
226
+ */
227
+ export interface ScheduleClient {
228
+ // ===== Handler Registration =====
229
+
230
+ /**
231
+ * Register a handler for a task type
232
+ * Must be called before scheduling tasks of this type
233
+ */
234
+ registerHandler(taskType: string, handler: TaskHandler): void;
235
+
236
+ /**
237
+ * Unregister a handler
238
+ */
239
+ unregisterHandler(taskType: string): boolean;
240
+
241
+ /**
242
+ * Check if a handler is registered
243
+ */
244
+ hasHandler(taskType: string): boolean;
245
+
246
+ /**
247
+ * Get all registered handler types
248
+ */
249
+ getHandlerTypes(): string[];
250
+
251
+ // ===== One-time Task Scheduling =====
252
+
253
+ /**
254
+ * Schedule a one-time task
255
+ * @param taskId - Unique identifier for the task
256
+ * @param taskType - Type of task (must have a registered handler)
257
+ * @param payload - Data to pass to the handler (must be JSON-serializable)
258
+ * @param options - Execution options (executeAt or delayMs required)
259
+ */
260
+ scheduleOnce(
261
+ taskId: string,
262
+ taskType: string,
263
+ payload: Record<string, any>,
264
+ options: ScheduleOnceOptions
265
+ ): Promise<boolean>;
266
+
267
+ // ===== Cron Task Scheduling =====
268
+
269
+ /**
270
+ * Schedule a recurring cron task
271
+ * @param taskId - Unique identifier for the task
272
+ * @param taskType - Type of task (must have a registered handler)
273
+ * @param payload - Data to pass to the handler (must be JSON-serializable)
274
+ * @param options - Cron options (cronExpression required)
275
+ */
276
+ scheduleCron(
277
+ taskId: string,
278
+ taskType: string,
279
+ payload: Record<string, any>,
280
+ options: ScheduleCronOptions
281
+ ): Promise<boolean>;
282
+
283
+ // ===== Task Management =====
284
+
285
+ /**
286
+ * Cancel a scheduled task
287
+ */
288
+ cancel(taskId: string): Promise<boolean>;
289
+
290
+ /**
291
+ * Pause a cron task (only for CRON type)
292
+ */
293
+ pause(taskId: string): Promise<boolean>;
294
+
295
+ /**
296
+ * Resume a paused cron task (only for CRON type)
297
+ */
298
+ resume(taskId: string): Promise<boolean>;
299
+
300
+ /**
301
+ * Check if a task exists
302
+ */
303
+ has(taskId: string): Promise<boolean>;
304
+
305
+ /**
306
+ * Get task information
307
+ */
308
+ getTask(taskId: string): Promise<ScheduledTaskDefinition | null>;
309
+
310
+ /**
311
+ * Get remaining time until next execution
312
+ * Returns -1 if task not found or already executed
313
+ */
314
+ getRemainingTime(taskId: string): Promise<number>;
315
+
316
+ /**
317
+ * Get count of active tasks (pending + paused)
318
+ */
319
+ getActiveTaskCount(): Promise<number>;
320
+
321
+ /**
322
+ * Get all active task IDs
323
+ */
324
+ getActiveTaskIds(): Promise<string[]>;
325
+
326
+ /**
327
+ * Cancel all active tasks
328
+ */
329
+ cancelAll(): Promise<void>;
330
+
331
+ // ===== Recovery =====
332
+
333
+ /**
334
+ * Restore active tasks from storage (call on service startup)
335
+ * Re-schedules all pending tasks with their remaining time
336
+ * Re-schedules all cron tasks for their next run
337
+ * @returns Number of tasks restored
338
+ */
339
+ restore(): Promise<number>;
340
+
341
+ // ===== Storage =====
342
+
343
+ /**
344
+ * Set the storage backend
345
+ */
346
+ setStorage(storage: ScheduleStorage): void;
347
+
348
+ /**
349
+ * Get current storage backend
350
+ */
351
+ getStorage(): ScheduleStorage | null;
352
+ }
353
+
354
+ /**
355
+ * Schedule Lattice protocol interface
356
+ */
357
+ export interface ScheduleLatticeProtocol
358
+ extends BaseLatticeProtocol<ScheduleConfig, ScheduleClient> {
359
+ // Handler registration
360
+ registerHandler: (taskType: string, handler: TaskHandler) => void;
361
+ unregisterHandler: (taskType: string) => boolean;
362
+ hasHandler: (taskType: string) => boolean;
363
+ getHandlerTypes: () => string[];
364
+
365
+ // One-time task scheduling
366
+ scheduleOnce: (
367
+ taskId: string,
368
+ taskType: string,
369
+ payload: Record<string, any>,
370
+ options: ScheduleOnceOptions
371
+ ) => Promise<boolean>;
372
+
373
+ // Cron task scheduling
374
+ scheduleCron: (
375
+ taskId: string,
376
+ taskType: string,
377
+ payload: Record<string, any>,
378
+ options: ScheduleCronOptions
379
+ ) => Promise<boolean>;
380
+
381
+ // Task management
382
+ cancel: (taskId: string) => Promise<boolean>;
383
+ pause: (taskId: string) => Promise<boolean>;
384
+ resume: (taskId: string) => Promise<boolean>;
385
+ has: (taskId: string) => Promise<boolean>;
386
+ getTask: (taskId: string) => Promise<ScheduledTaskDefinition | null>;
387
+ getRemainingTime: (taskId: string) => Promise<number>;
388
+ getActiveTaskCount: () => Promise<number>;
389
+ getActiveTaskIds: () => Promise<string[]>;
390
+ cancelAll: () => Promise<void>;
391
+
392
+ // Recovery
393
+ restore: () => Promise<number>;
394
+ }
package/src/index.ts CHANGED
@@ -11,6 +11,7 @@ export * from "./AgentLatticeProtocol";
11
11
  export * from "./MemoryLatticeProtocol";
12
12
  export * from "./UILatticeProtocol";
13
13
  export * from "./QueueLatticeProtocol";
14
+ export * from "./ScheduleLatticeProtocol";
14
15
  export * from "./EmbeddingsLatticeProtocol";
15
16
  export * from "./VectorStoreLatticeProtocol";
16
17
  export * from "./MessageProtocol";