chore: adjusting the types of the checkpoint interface slightly

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-11-14 09:20:05 -07:00
parent f5a9a718ad
commit d674371495
3 changed files with 47 additions and 18 deletions
@@ -152,19 +152,41 @@ describe('NunjucksWorkflowRunner', () => {
id: 'checkpoints-action',
description: 'Mock action with checkpoints',
handler: async ctx => {
const key1 = await ctx.checkpoint('key1', async () => {
return 'updated';
const key1 = await ctx.checkpoint({
key: 'key1',
fn: async () => {
return 'updated';
},
});
const key2 = await ctx.checkpoint('key2', async () => {
return 'updated';
const key2 = await ctx.checkpoint({
key: 'key2',
fn: async () => {
return 'updated';
},
});
const key3 = await ctx.checkpoint('key3', async () => {
return 'updated';
const key3 = await ctx.checkpoint({
key: 'key3',
fn: async () => {
return 'updated';
},
});
const key4 = await ctx.checkpoint({
fn: () => {},
});
const key5 = await ctx.checkpoint({
fn: async () => {},
});
ctx.output('key1', key1);
ctx.output('key2', key2);
ctx.output('key3', key3);
// @ts-expect-error - this is void return
ctx.output('key4', key4);
// @ts-expect-error - this is void return
ctx.output('key5', key5);
},
});
@@ -364,6 +364,8 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
)}`,
);
}
let checkpointCount = 0;
await action.handler({
input: iteration.input,
secrets: task.secrets ?? {},
@@ -371,21 +373,26 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
logger: loggerToWinstonLogger(taskLogger),
logStream: streamLogger,
workspacePath,
async checkpoint<U extends JsonValue>(
keySuffix: string,
fn: () => Promise<U>,
) {
const key = `v1.task.checkpoint.${step.id}.${keySuffix}`;
async checkpoint<T extends JsonValue | void>(opts: {
key?: string;
fn: () => Promise<T> | T;
}) {
const { key: checkpointKey = checkpointCount++, fn } = opts;
// default the task checkpoint to stepId plus count if there's no explict key provided.
const key = `v1.task.checkpoint.${step.id}.${checkpointKey}`;
try {
let prevValue: U | undefined;
let prevValue: T | undefined;
if (prevTaskState) {
const prevState = (
prevTaskState.state?.checkpoints as {
[key: string]: CheckpointState;
}
)?.[key];
if (prevState && prevState.status === 'success') {
prevValue = prevState.value as U;
prevValue = prevState.value as T;
}
}
@@ -395,7 +402,7 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
task.updateCheckpoint?.({
key,
status: 'success',
value,
value: value ?? {},
});
}
return value;
+4 -4
View File
@@ -38,10 +38,10 @@ export type ActionContext<
secrets?: TaskSecrets;
workspacePath: string;
input: TActionInput;
checkpoint<U extends JsonValue>(
key: string,
fn: () => Promise<U>,
): Promise<U>;
checkpoint<U extends JsonValue | void>(opts: {
key?: string;
fn: () => Promise<U> | U;
}): Promise<U>;
output(
name: keyof TActionOutput,
value: TActionOutput[keyof TActionOutput],