@mastra/inngest 0.10.4 → 0.10.5-alpha.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.
- package/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +24 -0
- package/dist/_tsup-dts-rollup.d.cts +41 -18
- package/dist/_tsup-dts-rollup.d.ts +41 -18
- package/dist/index.cjs +143 -19
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +144 -21
- package/package.json +12 -12
- package/src/index.test.ts +180 -1
- package/src/index.ts +270 -53
package/src/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
2
|
import { subscribe } from '@inngest/realtime';
|
|
3
|
-
import
|
|
3
|
+
import { Agent, Tool } from '@mastra/core';
|
|
4
|
+
import type { Mastra, ToolExecutionContext, WorkflowRun, WorkflowRuns } from '@mastra/core';
|
|
4
5
|
import { RuntimeContext } from '@mastra/core/di';
|
|
5
|
-
import { Workflow,
|
|
6
|
+
import { Workflow, Run, DefaultExecutionEngine } from '@mastra/core/workflows';
|
|
6
7
|
import type {
|
|
7
8
|
ExecuteFunction,
|
|
8
9
|
ExecutionContext,
|
|
@@ -15,12 +16,13 @@ import type {
|
|
|
15
16
|
WorkflowResult,
|
|
16
17
|
SerializedStepFlowEntry,
|
|
17
18
|
StepFailure,
|
|
19
|
+
WatchEvent,
|
|
18
20
|
} from '@mastra/core/workflows';
|
|
19
21
|
import { EMITTER_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
20
22
|
import type { Span } from '@opentelemetry/api';
|
|
21
23
|
import type { Inngest, BaseContext } from 'inngest';
|
|
22
24
|
import { serve as inngestServe } from 'inngest/hono';
|
|
23
|
-
import
|
|
25
|
+
import { z } from 'zod';
|
|
24
26
|
|
|
25
27
|
export type InngestEngineType = {
|
|
26
28
|
step: any;
|
|
@@ -42,10 +44,11 @@ export function serve({ mastra, inngest }: { mastra: Mastra; inngest: Inngest })
|
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
export class InngestRun<
|
|
47
|
+
TEngineType = InngestEngineType,
|
|
45
48
|
TSteps extends Step<string, any, any>[] = Step<string, any, any>[],
|
|
46
49
|
TInput extends z.ZodType<any> = z.ZodType<any>,
|
|
47
50
|
TOutput extends z.ZodType<any> = z.ZodType<any>,
|
|
48
|
-
> extends Run<TSteps, TInput, TOutput> {
|
|
51
|
+
> extends Run<TEngineType, TSteps, TInput, TOutput> {
|
|
49
52
|
private inngest: Inngest;
|
|
50
53
|
serializedStepGraph: SerializedStepFlowEntry[];
|
|
51
54
|
#mastra: Mastra;
|
|
@@ -207,12 +210,13 @@ export class InngestRun<
|
|
|
207
210
|
}
|
|
208
211
|
|
|
209
212
|
export class InngestWorkflow<
|
|
213
|
+
TEngineType = InngestEngineType,
|
|
210
214
|
TSteps extends Step<string, any, any>[] = Step<string, any, any>[],
|
|
211
215
|
TWorkflowId extends string = string,
|
|
212
216
|
TInput extends z.ZodType<any> = z.ZodType<any>,
|
|
213
217
|
TOutput extends z.ZodType<any> = z.ZodType<any>,
|
|
214
218
|
TPrevSchema extends z.ZodType<any> = TInput,
|
|
215
|
-
> extends Workflow<TSteps, TWorkflowId, TInput, TOutput,
|
|
219
|
+
> extends Workflow<TEngineType, TSteps, TWorkflowId, TInput, TOutput, TPrevSchema> {
|
|
216
220
|
#mastra: Mastra;
|
|
217
221
|
public inngest: Inngest;
|
|
218
222
|
|
|
@@ -244,7 +248,10 @@ export class InngestWorkflow<
|
|
|
244
248
|
const storage = this.#mastra?.getStorage();
|
|
245
249
|
if (!storage) {
|
|
246
250
|
this.logger.debug('Cannot get workflow runs. Mastra engine is not initialized');
|
|
247
|
-
|
|
251
|
+
//returning in memory run if no storage is initialized
|
|
252
|
+
return this.runs.get(runId)
|
|
253
|
+
? ({ ...this.runs.get(runId), workflowName: this.id } as unknown as WorkflowRun)
|
|
254
|
+
: null;
|
|
248
255
|
}
|
|
249
256
|
const run = (await storage.getWorkflowRunById({ runId, workflowName: this.id })) as unknown as WorkflowRun;
|
|
250
257
|
|
|
@@ -254,6 +261,32 @@ export class InngestWorkflow<
|
|
|
254
261
|
);
|
|
255
262
|
}
|
|
256
263
|
|
|
264
|
+
async getWorkflowRunExecutionResult(runId: string): Promise<WatchEvent['payload']['workflowState'] | null> {
|
|
265
|
+
const storage = this.#mastra?.getStorage();
|
|
266
|
+
if (!storage) {
|
|
267
|
+
this.logger.debug('Cannot get workflow run execution result. Mastra storage is not initialized');
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
|
|
272
|
+
|
|
273
|
+
if (!run?.snapshot) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (typeof run.snapshot === 'string') {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return {
|
|
282
|
+
status: run.snapshot.status,
|
|
283
|
+
result: run.snapshot.result,
|
|
284
|
+
error: run.snapshot.error,
|
|
285
|
+
payload: run.snapshot.context?.input,
|
|
286
|
+
steps: run.snapshot.context as any,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
257
290
|
__registerMastra(mastra: Mastra) {
|
|
258
291
|
this.#mastra = mastra;
|
|
259
292
|
this.executionEngine.__registerMastra(mastra);
|
|
@@ -277,11 +310,11 @@ export class InngestWorkflow<
|
|
|
277
310
|
}
|
|
278
311
|
}
|
|
279
312
|
|
|
280
|
-
createRun(options?: { runId?: string }): Run<TSteps, TInput, TOutput> {
|
|
313
|
+
createRun(options?: { runId?: string }): Run<TEngineType, TSteps, TInput, TOutput> {
|
|
281
314
|
const runIdToUse = options?.runId || randomUUID();
|
|
282
315
|
|
|
283
316
|
// Return a new Run instance with object parameters
|
|
284
|
-
const run: Run<TSteps, TInput, TOutput> =
|
|
317
|
+
const run: Run<TEngineType, TSteps, TInput, TOutput> =
|
|
285
318
|
this.runs.get(runIdToUse) ??
|
|
286
319
|
new InngestRun(
|
|
287
320
|
{
|
|
@@ -375,29 +408,185 @@ export class InngestWorkflow<
|
|
|
375
408
|
}
|
|
376
409
|
}
|
|
377
410
|
|
|
378
|
-
function
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
411
|
+
export function createStep<
|
|
412
|
+
TStepId extends string,
|
|
413
|
+
TStepInput extends z.ZodType<any>,
|
|
414
|
+
TStepOutput extends z.ZodType<any>,
|
|
415
|
+
TResumeSchema extends z.ZodType<any>,
|
|
416
|
+
TSuspendSchema extends z.ZodType<any>,
|
|
417
|
+
>(params: {
|
|
418
|
+
id: TStepId;
|
|
419
|
+
description?: string;
|
|
420
|
+
inputSchema: TStepInput;
|
|
421
|
+
outputSchema: TStepOutput;
|
|
422
|
+
resumeSchema?: TResumeSchema;
|
|
423
|
+
suspendSchema?: TSuspendSchema;
|
|
424
|
+
execute: ExecuteFunction<
|
|
425
|
+
z.infer<TStepInput>,
|
|
426
|
+
z.infer<TStepOutput>,
|
|
427
|
+
z.infer<TResumeSchema>,
|
|
428
|
+
z.infer<TSuspendSchema>,
|
|
429
|
+
InngestEngineType
|
|
430
|
+
>;
|
|
431
|
+
}): Step<TStepId, TStepInput, TStepOutput, TResumeSchema, TSuspendSchema, InngestEngineType>;
|
|
432
|
+
|
|
433
|
+
export function createStep<
|
|
434
|
+
TStepId extends string,
|
|
435
|
+
TStepInput extends z.ZodObject<{ prompt: z.ZodString }>,
|
|
436
|
+
TStepOutput extends z.ZodObject<{ text: z.ZodString }>,
|
|
437
|
+
TResumeSchema extends z.ZodType<any>,
|
|
438
|
+
TSuspendSchema extends z.ZodType<any>,
|
|
383
439
|
>(
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
440
|
+
agent: Agent<TStepId, any, any>,
|
|
441
|
+
): Step<TStepId, TStepInput, TStepOutput, TResumeSchema, TSuspendSchema, InngestEngineType>;
|
|
442
|
+
|
|
443
|
+
export function createStep<
|
|
444
|
+
TSchemaIn extends z.ZodType<any>,
|
|
445
|
+
TSchemaOut extends z.ZodType<any>,
|
|
446
|
+
TContext extends ToolExecutionContext<TSchemaIn>,
|
|
447
|
+
>(
|
|
448
|
+
tool: Tool<TSchemaIn, TSchemaOut, TContext> & {
|
|
449
|
+
inputSchema: TSchemaIn;
|
|
450
|
+
outputSchema: TSchemaOut;
|
|
451
|
+
execute: (context: TContext) => Promise<any>;
|
|
452
|
+
},
|
|
453
|
+
): Step<string, TSchemaIn, TSchemaOut, z.ZodType<any>, z.ZodType<any>, InngestEngineType>;
|
|
454
|
+
|
|
455
|
+
export function createStep<
|
|
456
|
+
TStepId extends string,
|
|
457
|
+
TStepInput extends z.ZodType<any>,
|
|
458
|
+
TStepOutput extends z.ZodType<any>,
|
|
459
|
+
TResumeSchema extends z.ZodType<any>,
|
|
460
|
+
TSuspendSchema extends z.ZodType<any>,
|
|
461
|
+
>(
|
|
462
|
+
params:
|
|
463
|
+
| {
|
|
464
|
+
id: TStepId;
|
|
465
|
+
description?: string;
|
|
466
|
+
inputSchema: TStepInput;
|
|
467
|
+
outputSchema: TStepOutput;
|
|
468
|
+
resumeSchema?: TResumeSchema;
|
|
469
|
+
suspendSchema?: TSuspendSchema;
|
|
470
|
+
execute: ExecuteFunction<
|
|
471
|
+
z.infer<TStepInput>,
|
|
472
|
+
z.infer<TStepOutput>,
|
|
473
|
+
z.infer<TResumeSchema>,
|
|
474
|
+
z.infer<TSuspendSchema>,
|
|
475
|
+
InngestEngineType
|
|
476
|
+
>;
|
|
477
|
+
}
|
|
478
|
+
| Agent<any, any, any>
|
|
479
|
+
| (Tool<TStepInput, TStepOutput, any> & {
|
|
480
|
+
inputSchema: TStepInput;
|
|
481
|
+
outputSchema: TStepOutput;
|
|
482
|
+
execute: (context: ToolExecutionContext<TStepInput>) => Promise<any>;
|
|
483
|
+
}),
|
|
484
|
+
): Step<TStepId, TStepInput, TStepOutput, TResumeSchema, TSuspendSchema, InngestEngineType> {
|
|
485
|
+
if (params instanceof Agent) {
|
|
486
|
+
return {
|
|
487
|
+
id: params.name,
|
|
488
|
+
// @ts-ignore
|
|
489
|
+
inputSchema: z.object({
|
|
490
|
+
prompt: z.string(),
|
|
491
|
+
// resourceId: z.string().optional(),
|
|
492
|
+
// threadId: z.string().optional(),
|
|
493
|
+
}),
|
|
494
|
+
// @ts-ignore
|
|
495
|
+
outputSchema: z.object({
|
|
496
|
+
text: z.string(),
|
|
497
|
+
}),
|
|
498
|
+
execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext }) => {
|
|
499
|
+
let streamPromise = {} as {
|
|
500
|
+
promise: Promise<string>;
|
|
501
|
+
resolve: (value: string) => void;
|
|
502
|
+
reject: (reason?: any) => void;
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
streamPromise.promise = new Promise((resolve, reject) => {
|
|
506
|
+
streamPromise.resolve = resolve;
|
|
507
|
+
streamPromise.reject = reject;
|
|
508
|
+
});
|
|
509
|
+
const toolData = {
|
|
510
|
+
name: params.name,
|
|
511
|
+
args: inputData,
|
|
512
|
+
};
|
|
513
|
+
await emitter.emit('watch-v2', {
|
|
514
|
+
type: 'tool-call-streaming-start',
|
|
515
|
+
...toolData,
|
|
516
|
+
});
|
|
517
|
+
const { fullStream } = await params.stream(inputData.prompt, {
|
|
518
|
+
// resourceId: inputData.resourceId,
|
|
519
|
+
// threadId: inputData.threadId,
|
|
520
|
+
runtimeContext,
|
|
521
|
+
onFinish: result => {
|
|
522
|
+
streamPromise.resolve(result.text);
|
|
523
|
+
},
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
for await (const chunk of fullStream) {
|
|
527
|
+
switch (chunk.type) {
|
|
528
|
+
case 'text-delta':
|
|
529
|
+
await emitter.emit('watch-v2', {
|
|
530
|
+
type: 'tool-call-delta',
|
|
531
|
+
...toolData,
|
|
532
|
+
argsTextDelta: chunk.textDelta,
|
|
533
|
+
});
|
|
534
|
+
break;
|
|
535
|
+
|
|
536
|
+
case 'step-start':
|
|
537
|
+
case 'step-finish':
|
|
538
|
+
case 'finish':
|
|
539
|
+
break;
|
|
540
|
+
|
|
541
|
+
case 'tool-call':
|
|
542
|
+
case 'tool-result':
|
|
543
|
+
case 'tool-call-streaming-start':
|
|
544
|
+
case 'tool-call-delta':
|
|
545
|
+
case 'source':
|
|
546
|
+
case 'file':
|
|
547
|
+
default:
|
|
548
|
+
await emitter.emit('watch-v2', chunk);
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return {
|
|
554
|
+
text: await streamPromise.promise,
|
|
555
|
+
};
|
|
556
|
+
},
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (params instanceof Tool) {
|
|
561
|
+
if (!params.inputSchema || !params.outputSchema) {
|
|
562
|
+
throw new Error('Tool must have input and output schemas defined');
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
return {
|
|
566
|
+
// TODO: tool probably should have strong id type
|
|
567
|
+
// @ts-ignore
|
|
568
|
+
id: params.id,
|
|
569
|
+
inputSchema: params.inputSchema,
|
|
570
|
+
outputSchema: params.outputSchema,
|
|
571
|
+
execute: async ({ inputData, mastra, runtimeContext }) => {
|
|
572
|
+
return params.execute({
|
|
573
|
+
context: inputData,
|
|
574
|
+
mastra,
|
|
575
|
+
runtimeContext,
|
|
576
|
+
});
|
|
577
|
+
},
|
|
578
|
+
};
|
|
579
|
+
}
|
|
397
580
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
581
|
+
return {
|
|
582
|
+
id: params.id,
|
|
583
|
+
description: params.description,
|
|
584
|
+
inputSchema: params.inputSchema,
|
|
585
|
+
outputSchema: params.outputSchema,
|
|
586
|
+
resumeSchema: params.resumeSchema,
|
|
587
|
+
suspendSchema: params.suspendSchema,
|
|
588
|
+
execute: params.execute,
|
|
589
|
+
};
|
|
401
590
|
}
|
|
402
591
|
|
|
403
592
|
export function init(inngest: Inngest) {
|
|
@@ -406,34 +595,58 @@ export function init(inngest: Inngest) {
|
|
|
406
595
|
TWorkflowId extends string = string,
|
|
407
596
|
TInput extends z.ZodType<any> = z.ZodType<any>,
|
|
408
597
|
TOutput extends z.ZodType<any> = z.ZodType<any>,
|
|
409
|
-
TSteps extends Step<string, any, any
|
|
598
|
+
TSteps extends Step<string, any, any, any, any, InngestEngineType>[] = Step<
|
|
599
|
+
string,
|
|
600
|
+
any,
|
|
601
|
+
any,
|
|
602
|
+
any,
|
|
603
|
+
any,
|
|
604
|
+
InngestEngineType
|
|
605
|
+
>[],
|
|
410
606
|
>(params: WorkflowConfig<TWorkflowId, TInput, TOutput, TSteps>) {
|
|
411
|
-
return new InngestWorkflow(params, inngest);
|
|
607
|
+
return new InngestWorkflow<InngestEngineType, TSteps, TWorkflowId, TInput, TOutput, TInput>(params, inngest);
|
|
608
|
+
},
|
|
609
|
+
createStep,
|
|
610
|
+
cloneStep<TStepId extends string>(
|
|
611
|
+
step: Step<string, any, any, any, any, InngestEngineType>,
|
|
612
|
+
opts: { id: TStepId },
|
|
613
|
+
): Step<TStepId, any, any, any, any, InngestEngineType> {
|
|
614
|
+
return {
|
|
615
|
+
id: opts.id,
|
|
616
|
+
description: step.description,
|
|
617
|
+
inputSchema: step.inputSchema,
|
|
618
|
+
outputSchema: step.outputSchema,
|
|
619
|
+
execute: step.execute,
|
|
620
|
+
};
|
|
412
621
|
},
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
resumeSchema?: TResumeSchema;
|
|
424
|
-
suspendSchema?: TSuspendSchema;
|
|
425
|
-
execute: ExecuteFunction<
|
|
426
|
-
z.infer<TStepInput>,
|
|
427
|
-
z.infer<TStepOutput>,
|
|
428
|
-
z.infer<TResumeSchema>,
|
|
429
|
-
z.infer<TSuspendSchema>,
|
|
622
|
+
cloneWorkflow<
|
|
623
|
+
TWorkflowId extends string = string,
|
|
624
|
+
TInput extends z.ZodType<any> = z.ZodType<any>,
|
|
625
|
+
TOutput extends z.ZodType<any> = z.ZodType<any>,
|
|
626
|
+
TSteps extends Step<string, any, any, any, any, InngestEngineType>[] = Step<
|
|
627
|
+
string,
|
|
628
|
+
any,
|
|
629
|
+
any,
|
|
630
|
+
any,
|
|
631
|
+
any,
|
|
430
632
|
InngestEngineType
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
633
|
+
>[],
|
|
634
|
+
>(
|
|
635
|
+
workflow: Workflow<InngestEngineType, TSteps, string, TInput, TOutput, TInput>,
|
|
636
|
+
opts: { id: TWorkflowId },
|
|
637
|
+
): Workflow<InngestEngineType, TSteps, TWorkflowId, TInput, TOutput, TInput> {
|
|
638
|
+
const wf = new Workflow({
|
|
639
|
+
id: opts.id,
|
|
640
|
+
inputSchema: workflow.inputSchema,
|
|
641
|
+
outputSchema: workflow.outputSchema,
|
|
642
|
+
steps: workflow.stepDefs,
|
|
643
|
+
mastra: workflow.mastra,
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
wf.setStepFlow(workflow.stepGraph);
|
|
647
|
+
wf.commit();
|
|
648
|
+
return wf;
|
|
434
649
|
},
|
|
435
|
-
cloneStep,
|
|
436
|
-
cloneWorkflow,
|
|
437
650
|
};
|
|
438
651
|
}
|
|
439
652
|
|
|
@@ -558,6 +771,10 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
558
771
|
});
|
|
559
772
|
}
|
|
560
773
|
|
|
774
|
+
async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
775
|
+
await this.inngestStep.sleep(id, duration);
|
|
776
|
+
}
|
|
777
|
+
|
|
561
778
|
async executeStep({
|
|
562
779
|
step,
|
|
563
780
|
stepResults,
|