Skip to content

DX-1797: Add WorkflowMiddleware #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/context/auto-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type BaseLazyStep } from "./steps";
import type { WorkflowLogger } from "../logger";
import { QstashError } from "@upstash/qstash";
import { submitParallelSteps, submitSingleStep } from "../qstash/submit-steps";
import { WorkflowMiddleware } from "../middleware";

export class AutoExecutor {
private context: WorkflowContext;
Expand All @@ -15,8 +16,9 @@ export class AutoExecutor {
private readonly nonPlanStepCount: number;
private readonly steps: Step[];
private indexInCurrentList = 0;
private invokeCount: number;
private telemetry?: Telemetry;
private readonly invokeCount: number;
private readonly telemetry?: Telemetry;
private readonly middlewares?: WorkflowMiddleware[];

public stepCount = 0;
public planStepCount = 0;
Expand All @@ -28,13 +30,15 @@ export class AutoExecutor {
steps: Step[],
telemetry?: Telemetry,
invokeCount?: number,
debug?: WorkflowLogger
debug?: WorkflowLogger,
middlewares?: WorkflowMiddleware[]
) {
this.context = context;
this.steps = steps;
this.telemetry = telemetry;
this.invokeCount = invokeCount ?? 0;
this.debug = debug;
this.middlewares = middlewares;

this.nonPlanStepCount = this.steps.filter((step) => !step.targetStep).length;
}
Expand Down Expand Up @@ -133,7 +137,9 @@ export class AutoExecutor {
step,
stepCount: this.stepCount,
});
return lazyStep.parseOut(step.out);
const parsedOut = lazyStep.parseOut(step.out);

return parsedOut;
}

const resultStep = await submitSingleStep({
Expand All @@ -144,6 +150,7 @@ export class AutoExecutor {
concurrency: 1,
telemetry: this.telemetry,
debug: this.debug,
middlewares: this.middlewares,
});
throw new WorkflowAbort(lazyStep.stepName, resultStep);
}
Expand Down Expand Up @@ -232,6 +239,7 @@ export class AutoExecutor {
concurrency: parallelSteps.length,
telemetry: this.telemetry,
debug: this.debug,
middlewares: this.middlewares,
});
throw new WorkflowAbort(parallelStep.stepName, resultStep);
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { WorkflowApi } from "./api";
import { WorkflowAgents } from "../agents";
import { FlowControl } from "@upstash/qstash";
import { getNewUrlFromWorkflowId } from "../serve/serve-many";
import { WorkflowMiddleware } from "../middleware";

/**
* Upstash Workflow context
Expand Down Expand Up @@ -176,6 +177,7 @@ export class WorkflowContext<TInitialPayload = unknown> {
telemetry,
invokeCount,
flowControl,
middlewares,
}: {
qstashClient: WorkflowClient;
workflowRunId: string;
Expand All @@ -190,6 +192,7 @@ export class WorkflowContext<TInitialPayload = unknown> {
telemetry?: Telemetry;
invokeCount?: number;
flowControl?: FlowControl;
middlewares?: WorkflowMiddleware[];
}) {
this.qstashClient = qstashClient;
this.workflowRunId = workflowRunId;
Expand All @@ -202,7 +205,7 @@ export class WorkflowContext<TInitialPayload = unknown> {
this.retries = retries ?? DEFAULT_RETRIES;
this.flowControl = flowControl;

this.executor = new AutoExecutor(this, this.steps, telemetry, invokeCount, debug);
this.executor = new AutoExecutor(this, this.steps, telemetry, invokeCount, debug, middlewares);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./logger";
export * from "./client";
export { WorkflowError, WorkflowAbort } from "./error";
export { WorkflowTool } from "./agents/adapters";
export { WorkflowMiddleware, loggingMiddleware } from "./middleware";
2 changes: 2 additions & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { WorkflowMiddleware } from "./middleware";
export { loggingMiddleware } from "./logging";
23 changes: 23 additions & 0 deletions src/middleware/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { WorkflowMiddleware } from "./middleware";

export const loggingMiddleware = new WorkflowMiddleware({
name: "logging",
init: () => {
console.log("Logging middleware initialized");

return {
afterExecution(params) {
console.log("Step executed:", params);
},
beforeExecution(params) {
console.log("Step execution started:", params);
},
runStarted(params) {
console.log("Workflow run started:", params);
},
runCompleted(params) {
console.log("Workflow run completed:", params);
},
};
},
});
Loading
Loading