ts-agent-lib 0.1.0 → 0.1.1

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,416 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const StepStatus: {
4
+ readonly PENDING: "pending";
5
+ readonly RUNNING: "running";
6
+ readonly COMPLETED: "completed";
7
+ readonly FAILED: "failed";
8
+ readonly BLOCKED: "blocked";
9
+ readonly CANCELLED: "cancelled";
10
+ };
11
+ type StepStatus = (typeof StepStatus)[keyof typeof StepStatus];
12
+ declare const StepStatusSchema: z.ZodEnum<{
13
+ pending: "pending";
14
+ running: "running";
15
+ completed: "completed";
16
+ failed: "failed";
17
+ blocked: "blocked";
18
+ cancelled: "cancelled";
19
+ }>;
20
+ declare const ExecutionStatus: {
21
+ readonly PENDING: "pending";
22
+ readonly RUNNING: "running";
23
+ readonly COMPLETED: "completed";
24
+ readonly FAILED: "failed";
25
+ readonly CANCELLED: "cancelled";
26
+ };
27
+ type ExecutionStatus = (typeof ExecutionStatus)[keyof typeof ExecutionStatus];
28
+ declare const ExecutionStatusSchema: z.ZodEnum<{
29
+ pending: "pending";
30
+ running: "running";
31
+ completed: "completed";
32
+ failed: "failed";
33
+ cancelled: "cancelled";
34
+ }>;
35
+ declare const StepSchema: z.ZodObject<{
36
+ id: z.ZodString;
37
+ action: z.ZodString;
38
+ deps: z.ZodPipe<z.ZodOptional<z.ZodArray<z.ZodString>>, z.ZodTransform<string[], string[] | undefined>>;
39
+ payload: z.ZodOptional<z.ZodUnknown>;
40
+ }, z.core.$strip>;
41
+ type Step = z.infer<typeof StepSchema>;
42
+ declare function utcNow(): Date;
43
+ declare const ErrorInfoSchema: z.ZodObject<{
44
+ message: z.ZodString;
45
+ type: z.ZodOptional<z.ZodString>;
46
+ traceback: z.ZodOptional<z.ZodString>;
47
+ }, z.core.$strip>;
48
+ type ErrorInfo = z.infer<typeof ErrorInfoSchema>;
49
+ declare const StepResultSchema: z.ZodObject<{
50
+ stepId: z.ZodString;
51
+ status: z.ZodDefault<z.ZodEnum<{
52
+ pending: "pending";
53
+ running: "running";
54
+ completed: "completed";
55
+ failed: "failed";
56
+ blocked: "blocked";
57
+ cancelled: "cancelled";
58
+ }>>;
59
+ output: z.ZodOptional<z.ZodUnknown>;
60
+ error: z.ZodOptional<z.ZodObject<{
61
+ message: z.ZodString;
62
+ type: z.ZodOptional<z.ZodString>;
63
+ traceback: z.ZodOptional<z.ZodString>;
64
+ }, z.core.$strip>>;
65
+ startedAt: z.ZodOptional<z.ZodCoercedDate<unknown>>;
66
+ finishedAt: z.ZodOptional<z.ZodCoercedDate<unknown>>;
67
+ }, z.core.$strip>;
68
+ type StepResult = z.infer<typeof StepResultSchema>;
69
+ declare const ExecutionStateSchema: z.ZodObject<{
70
+ executionId: z.ZodString;
71
+ status: z.ZodDefault<z.ZodEnum<{
72
+ pending: "pending";
73
+ running: "running";
74
+ completed: "completed";
75
+ failed: "failed";
76
+ cancelled: "cancelled";
77
+ }>>;
78
+ steps: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodMap<z.ZodString, z.ZodObject<{
79
+ stepId: z.ZodString;
80
+ status: z.ZodDefault<z.ZodEnum<{
81
+ pending: "pending";
82
+ running: "running";
83
+ completed: "completed";
84
+ failed: "failed";
85
+ blocked: "blocked";
86
+ cancelled: "cancelled";
87
+ }>>;
88
+ output: z.ZodOptional<z.ZodUnknown>;
89
+ error: z.ZodOptional<z.ZodObject<{
90
+ message: z.ZodString;
91
+ type: z.ZodOptional<z.ZodString>;
92
+ traceback: z.ZodOptional<z.ZodString>;
93
+ }, z.core.$strip>>;
94
+ startedAt: z.ZodOptional<z.ZodCoercedDate<unknown>>;
95
+ finishedAt: z.ZodOptional<z.ZodCoercedDate<unknown>>;
96
+ }, z.core.$strip>>>;
97
+ startedAt: z.ZodOptional<z.ZodCoercedDate<unknown>>;
98
+ finishedAt: z.ZodOptional<z.ZodCoercedDate<unknown>>;
99
+ }, z.core.$strip>;
100
+ type ExecutionState = z.infer<typeof ExecutionStateSchema>;
101
+ declare const ExecutionEventSchema: z.ZodObject<{
102
+ executionId: z.ZodString;
103
+ ts: z.ZodCoercedDate<unknown>;
104
+ type: z.ZodString;
105
+ }, z.core.$strip>;
106
+ type ExecutionEvent = z.infer<typeof ExecutionEventSchema>;
107
+ declare const ExecutionStartedSchema: z.ZodObject<{
108
+ executionId: z.ZodString;
109
+ ts: z.ZodCoercedDate<unknown>;
110
+ type: z.ZodLiteral<"execution_started">;
111
+ startedAt: z.ZodCoercedDate<unknown>;
112
+ }, z.core.$strip>;
113
+ type ExecutionStarted = z.infer<typeof ExecutionStartedSchema>;
114
+ declare const ExecutionCompletedSchema: z.ZodObject<{
115
+ executionId: z.ZodString;
116
+ ts: z.ZodCoercedDate<unknown>;
117
+ type: z.ZodLiteral<"execution_completed">;
118
+ status: z.ZodEnum<{
119
+ pending: "pending";
120
+ running: "running";
121
+ completed: "completed";
122
+ failed: "failed";
123
+ cancelled: "cancelled";
124
+ }>;
125
+ finishedAt: z.ZodCoercedDate<unknown>;
126
+ }, z.core.$strip>;
127
+ type ExecutionCompleted = z.infer<typeof ExecutionCompletedSchema>;
128
+ declare const ExecutionCancelledSchema: z.ZodObject<{
129
+ executionId: z.ZodString;
130
+ ts: z.ZodCoercedDate<unknown>;
131
+ type: z.ZodLiteral<"execution_cancelled">;
132
+ finishedAt: z.ZodCoercedDate<unknown>;
133
+ }, z.core.$strip>;
134
+ type ExecutionCancelled = z.infer<typeof ExecutionCancelledSchema>;
135
+ declare const StepScheduledSchema: z.ZodObject<{
136
+ executionId: z.ZodString;
137
+ ts: z.ZodCoercedDate<unknown>;
138
+ type: z.ZodLiteral<"step_scheduled">;
139
+ stepId: z.ZodString;
140
+ }, z.core.$strip>;
141
+ type StepScheduled = z.infer<typeof StepScheduledSchema>;
142
+ declare const StepStartedSchema: z.ZodObject<{
143
+ executionId: z.ZodString;
144
+ ts: z.ZodCoercedDate<unknown>;
145
+ type: z.ZodLiteral<"step_started">;
146
+ stepId: z.ZodString;
147
+ }, z.core.$strip>;
148
+ type StepStarted = z.infer<typeof StepStartedSchema>;
149
+ declare const StepCompletedSchema: z.ZodObject<{
150
+ executionId: z.ZodString;
151
+ ts: z.ZodCoercedDate<unknown>;
152
+ type: z.ZodLiteral<"step_completed">;
153
+ stepId: z.ZodString;
154
+ }, z.core.$strip>;
155
+ type StepCompleted = z.infer<typeof StepCompletedSchema>;
156
+ declare const StepFailedSchema: z.ZodObject<{
157
+ executionId: z.ZodString;
158
+ ts: z.ZodCoercedDate<unknown>;
159
+ type: z.ZodLiteral<"step_failed">;
160
+ stepId: z.ZodString;
161
+ error: z.ZodObject<{
162
+ message: z.ZodString;
163
+ type: z.ZodOptional<z.ZodString>;
164
+ traceback: z.ZodOptional<z.ZodString>;
165
+ }, z.core.$strip>;
166
+ }, z.core.$strip>;
167
+ type StepFailed = z.infer<typeof StepFailedSchema>;
168
+ declare const StepBlockedSchema: z.ZodObject<{
169
+ executionId: z.ZodString;
170
+ ts: z.ZodCoercedDate<unknown>;
171
+ type: z.ZodLiteral<"step_blocked">;
172
+ stepId: z.ZodString;
173
+ }, z.core.$strip>;
174
+ type StepBlocked = z.infer<typeof StepBlockedSchema>;
175
+ declare const StepCancelledSchema: z.ZodObject<{
176
+ executionId: z.ZodString;
177
+ ts: z.ZodCoercedDate<unknown>;
178
+ type: z.ZodLiteral<"step_cancelled">;
179
+ stepId: z.ZodString;
180
+ }, z.core.$strip>;
181
+ type StepCancelled = z.infer<typeof StepCancelledSchema>;
182
+ declare const ExecutionEventTypeSchema: z.ZodUnion<readonly [z.ZodObject<{
183
+ executionId: z.ZodString;
184
+ ts: z.ZodCoercedDate<unknown>;
185
+ type: z.ZodLiteral<"execution_started">;
186
+ startedAt: z.ZodCoercedDate<unknown>;
187
+ }, z.core.$strip>, z.ZodObject<{
188
+ executionId: z.ZodString;
189
+ ts: z.ZodCoercedDate<unknown>;
190
+ type: z.ZodLiteral<"execution_completed">;
191
+ status: z.ZodEnum<{
192
+ pending: "pending";
193
+ running: "running";
194
+ completed: "completed";
195
+ failed: "failed";
196
+ cancelled: "cancelled";
197
+ }>;
198
+ finishedAt: z.ZodCoercedDate<unknown>;
199
+ }, z.core.$strip>, z.ZodObject<{
200
+ executionId: z.ZodString;
201
+ ts: z.ZodCoercedDate<unknown>;
202
+ type: z.ZodLiteral<"execution_cancelled">;
203
+ finishedAt: z.ZodCoercedDate<unknown>;
204
+ }, z.core.$strip>, z.ZodObject<{
205
+ executionId: z.ZodString;
206
+ ts: z.ZodCoercedDate<unknown>;
207
+ type: z.ZodLiteral<"step_scheduled">;
208
+ stepId: z.ZodString;
209
+ }, z.core.$strip>, z.ZodObject<{
210
+ executionId: z.ZodString;
211
+ ts: z.ZodCoercedDate<unknown>;
212
+ type: z.ZodLiteral<"step_started">;
213
+ stepId: z.ZodString;
214
+ }, z.core.$strip>, z.ZodObject<{
215
+ executionId: z.ZodString;
216
+ ts: z.ZodCoercedDate<unknown>;
217
+ type: z.ZodLiteral<"step_completed">;
218
+ stepId: z.ZodString;
219
+ }, z.core.$strip>, z.ZodObject<{
220
+ executionId: z.ZodString;
221
+ ts: z.ZodCoercedDate<unknown>;
222
+ type: z.ZodLiteral<"step_failed">;
223
+ stepId: z.ZodString;
224
+ error: z.ZodObject<{
225
+ message: z.ZodString;
226
+ type: z.ZodOptional<z.ZodString>;
227
+ traceback: z.ZodOptional<z.ZodString>;
228
+ }, z.core.$strip>;
229
+ }, z.core.$strip>, z.ZodObject<{
230
+ executionId: z.ZodString;
231
+ ts: z.ZodCoercedDate<unknown>;
232
+ type: z.ZodLiteral<"step_blocked">;
233
+ stepId: z.ZodString;
234
+ }, z.core.$strip>, z.ZodObject<{
235
+ executionId: z.ZodString;
236
+ ts: z.ZodCoercedDate<unknown>;
237
+ type: z.ZodLiteral<"step_cancelled">;
238
+ stepId: z.ZodString;
239
+ }, z.core.$strip>]>;
240
+ type ExecutionEventType = z.infer<typeof ExecutionEventTypeSchema>;
241
+ type StepsInput = Map<string, Step> | Record<string, Step>;
242
+ declare const PlanSchema: z.ZodObject<{
243
+ steps: z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodMap<z.ZodString, z.ZodObject<{
244
+ id: z.ZodString;
245
+ action: z.ZodString;
246
+ deps: z.ZodPipe<z.ZodOptional<z.ZodArray<z.ZodString>>, z.ZodTransform<string[], string[] | undefined>>;
247
+ payload: z.ZodOptional<z.ZodUnknown>;
248
+ }, z.core.$strip>>>;
249
+ }, z.core.$strip>;
250
+ type PlanInput = z.infer<typeof PlanSchema>;
251
+ declare class Plan implements Iterable<Step> {
252
+ private readonly _steps;
253
+ constructor(steps: StepsInput);
254
+ [Symbol.iterator](): Iterator<Step>;
255
+ get steps(): ReadonlyMap<string, Step>;
256
+ getStep(stepId: string): Step;
257
+ }
258
+
259
+ type StepHandler = (step: Step, ctx: StepContext) => Promise<StepResult>;
260
+ type EventHandler = (event: ExecutionEventType) => void | Promise<void>;
261
+ interface ExecuteOptions {
262
+ onEvent?: EventHandler;
263
+ cancelSignal?: AbortSignal;
264
+ executionId?: string;
265
+ }
266
+ declare class StepContext {
267
+ private readonly _executionId;
268
+ private readonly _stepId;
269
+ private readonly _signal?;
270
+ private readonly _plan;
271
+ private readonly _getResult;
272
+ constructor(params: {
273
+ executionId: string;
274
+ stepId: string;
275
+ signal?: AbortSignal;
276
+ plan: Plan;
277
+ getResult: (stepId: string) => StepResult | undefined;
278
+ });
279
+ get executionId(): string;
280
+ get stepId(): string;
281
+ get signal(): AbortSignal | undefined;
282
+ isCancelled(): boolean;
283
+ getStep(stepId: string): Step;
284
+ getResult(stepId: string): Readonly<StepResult> | undefined;
285
+ requireResult(stepId: string): Readonly<StepResult>;
286
+ getDependencyResults(): Readonly<Record<string, StepResult>>;
287
+ getDependencyOutputs<T = unknown>(): Readonly<Record<string, T>>;
288
+ }
289
+ declare class DagExecutor {
290
+ private readonly maxParallelSteps;
291
+ private readonly failFast;
292
+ constructor(params?: {
293
+ maxParallelSteps?: number;
294
+ failFast?: boolean;
295
+ });
296
+ executeAsync(plan: Plan, handlers: Record<string, StepHandler>, options?: ExecuteOptions): Promise<ExecutionState>;
297
+ execute(plan: Plan, handlers: Record<string, StepHandler>, options?: ExecuteOptions): Promise<ExecutionState>;
298
+ streamExecute(plan: Plan, handlers: Record<string, StepHandler>, options?: ExecuteOptions): AsyncIterable<ExecutionEventType>;
299
+ }
300
+
301
+ declare class ExecutionError extends Error {
302
+ readonly executionId: string;
303
+ readonly status: ExecutionStatus;
304
+ readonly errors: ErrorInfo[];
305
+ constructor(params: {
306
+ executionId: string;
307
+ status: ExecutionStatus;
308
+ errors: ErrorInfo[];
309
+ });
310
+ static fromState(state: ExecutionState): ExecutionError;
311
+ }
312
+ declare function raiseIfFailed(state: ExecutionState): void;
313
+
314
+ declare class PlanBuilder {
315
+ private readonly steps;
316
+ addStep(params: {
317
+ id: string;
318
+ action: string;
319
+ deps?: Iterable<string>;
320
+ payload?: unknown;
321
+ }): this;
322
+ addStepObj(step: Step): this;
323
+ build(): Plan;
324
+ private validateDependencies;
325
+ private validateAcyclic;
326
+ }
327
+
328
+ interface ExecutionObserver {
329
+ onEvent(event: ExecutionEventType): void | Promise<void>;
330
+ }
331
+ type ObserverErrorHandler = (observer: ExecutionObserver, event: ExecutionEventType, error: Error) => void | Promise<void>;
332
+ declare const ObserverFailurePolicy: {
333
+ readonly LENIENT: "lenient";
334
+ readonly STRICT: "strict";
335
+ };
336
+ type ObserverFailurePolicy = (typeof ObserverFailurePolicy)[keyof typeof ObserverFailurePolicy];
337
+ declare function composeObservers(observers: Iterable<ExecutionObserver>, options?: {
338
+ onError?: ObserverErrorHandler;
339
+ failurePolicy?: ObserverFailurePolicy;
340
+ }): EventHandler;
341
+ declare function composeObserversParallel(observers: Iterable<ExecutionObserver>, options?: {
342
+ onError?: ObserverErrorHandler;
343
+ failurePolicy?: ObserverFailurePolicy;
344
+ }): EventHandler;
345
+ declare const OverflowPolicy: {
346
+ readonly BLOCK: "block";
347
+ readonly DROP_NEW: "drop_new";
348
+ readonly DROP_OLDEST: "drop_oldest";
349
+ };
350
+ type OverflowPolicy = (typeof OverflowPolicy)[keyof typeof OverflowPolicy];
351
+ declare abstract class BufferedObserver implements ExecutionObserver {
352
+ readonly maxQueueSize: number;
353
+ readonly overflowPolicy: OverflowPolicy;
354
+ readonly onError?: (event: ExecutionEventType, error: Error) => void | Promise<void>;
355
+ private readonly queue;
356
+ private task;
357
+ private started;
358
+ private abortController;
359
+ dropped: number;
360
+ failed: boolean;
361
+ lastError: Error | null;
362
+ constructor(params?: {
363
+ maxQueueSize?: number;
364
+ overflowPolicy?: OverflowPolicy;
365
+ onError?: (event: ExecutionEventType, error: Error) => void | Promise<void>;
366
+ });
367
+ start(): Promise<void>;
368
+ stop(): Promise<void>;
369
+ onEvent(event: ExecutionEventType): Promise<void>;
370
+ private run;
371
+ protected abstract process(event: ExecutionEventType): void | Promise<void>;
372
+ }
373
+
374
+ type NumberLike = number;
375
+ declare class Usage {
376
+ readonly provider?: string;
377
+ readonly model?: string;
378
+ readonly metrics: Record<string, NumberLike>;
379
+ constructor(params?: {
380
+ provider?: string;
381
+ model?: string;
382
+ metrics?: Record<string, NumberLike>;
383
+ });
384
+ add(other: Usage): Usage;
385
+ }
386
+ declare function aggregateUsage(state: ExecutionState, extract: (result: StepResult) => Usage | undefined): Usage;
387
+ interface PricingRule {
388
+ metric: string;
389
+ perUnit: number;
390
+ scale?: number;
391
+ }
392
+ interface CostBreakdown {
393
+ byMetric: Record<string, number>;
394
+ total: number;
395
+ }
396
+ declare function estimateCost(usage: Usage, rules: PricingRule[]): CostBreakdown;
397
+
398
+ declare class TaskOutput<T> {
399
+ readonly value: T;
400
+ readonly usage?: Usage;
401
+ readonly meta: Record<string, unknown>;
402
+ constructor(params: {
403
+ value: T;
404
+ usage?: Usage;
405
+ meta?: Record<string, unknown>;
406
+ });
407
+ }
408
+ declare function defaultUsageExtractor(res: StepResult): Usage | undefined;
409
+ declare function runPlan(plan: Plan, handlers: Record<string, StepHandler>, options?: {
410
+ failOnError?: boolean;
411
+ usageExtractor?: ((res: StepResult) => Usage | undefined) | null;
412
+ pricingRules?: PricingRule[];
413
+ executor?: DagExecutor;
414
+ }): Promise<[ExecutionState, Usage | undefined, CostBreakdown | undefined]>;
415
+
416
+ export { BufferedObserver, type CostBreakdown, DagExecutor, type ErrorInfo, ErrorInfoSchema, type EventHandler, type ExecuteOptions, type ExecutionCancelled, ExecutionCancelledSchema, type ExecutionCompleted, ExecutionCompletedSchema, ExecutionError, type ExecutionEvent, ExecutionEventSchema, type ExecutionEventType, ExecutionEventTypeSchema, type ExecutionObserver, type ExecutionStarted, ExecutionStartedSchema, type ExecutionState, ExecutionStateSchema, ExecutionStatus, ExecutionStatusSchema, type ObserverErrorHandler, ObserverFailurePolicy, OverflowPolicy, Plan, PlanBuilder, type PlanInput, PlanSchema, type PricingRule, type Step, type StepBlocked, StepBlockedSchema, type StepCancelled, StepCancelledSchema, type StepCompleted, StepCompletedSchema, StepContext, type StepFailed, StepFailedSchema, type StepHandler, type StepResult, StepResultSchema, type StepScheduled, StepScheduledSchema, StepSchema, type StepStarted, StepStartedSchema, StepStatus, StepStatusSchema, type StepsInput, TaskOutput, Usage, aggregateUsage, composeObservers, composeObserversParallel, defaultUsageExtractor, estimateCost, raiseIfFailed, runPlan, utcNow };