Skip to content

Commit

Permalink
feat: Update interrupt semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjcsmith committed Dec 19, 2024
1 parent 6befe89 commit e7574ce
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
25 changes: 24 additions & 1 deletion sdk-node/src/execute-fn.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { executeFn } from "./execute-fn";
import { Interrupt } from "./util";

describe("executeFn", () => {
it("should run a function with arguments", async () => {
const fn = (val: { [key: string]: string }) => Promise.resolve(val.foo);
const result = await fn({ foo: "bar" });
const result = await executeFn(fn, [{foo: "bar"}] as any);
expect(result).toBe("bar");
});

it("should extract interrupt from resolution", async () => {
const fn = (_: string) => Promise.resolve(Interrupt.approval());
const result = await executeFn(fn, [{}] as any);
expect(result).toEqual({
content: { type: "approval" },
type: "interrupt",
functionExecutionTime: expect.any(Number),
});
});

it("should extract interrupt from rejection", async () => {
const fn = () => Promise.reject(Interrupt.approval());
const result = await executeFn(fn, [{}] as any);
expect(result).toEqual({
content: { type: "approval" },
type: "interrupt",
functionExecutionTime: expect.any(Number),
});
});
});
8 changes: 8 additions & 0 deletions sdk-node/src/execute-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export const executeFn = async (
functionExecutionTime: Date.now() - start,
};
} catch (e) {
const interupt = extractInterrupt(e);
if (interupt) {
return {
content: interupt,
type: "interrupt",
functionExecutionTime: Date.now() - start,
};
}
const functionExecutionTime = Date.now() - start;
if (e instanceof Error) {
return {
Expand Down
17 changes: 17 additions & 0 deletions sdk-node/src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
ajvErrorToFailures,
blob,
extractBlobs,
extractInterrupt,
Interrupt,
validateFunctionSchema,
} from "./util";

Expand Down Expand Up @@ -64,6 +66,21 @@ describe("ajvErrorToFailures", () => {
});
});

describe("extractInterrupt", () => {
it("should extract extract interrupt", () => {
const interrupt = extractInterrupt(Interrupt.approval());
expect(interrupt).toEqual({
type: "approval",
})
})

it("should not extract interrupt from non-interrupt", () => {
const interrupt = extractInterrupt({ foo: "bar" });
expect(interrupt).toBeUndefined();
})

});

describe("extractBlobs", () => {
it("should extract blobs from content", () => {
const initialContent = {
Expand Down
20 changes: 20 additions & 0 deletions sdk-node/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ export const blob = ({


export const INTERRUPT_KEY = "__inferable_interrupt";
type VALID_INTERRUPT_TYPES = "approval";
const interruptResultSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("approval"),
Expand All @@ -297,6 +298,25 @@ export const extractInterrupt = (input: unknown): z.infer<typeof interruptResult
}
}

export class Interrupt {
[INTERRUPT_KEY]: {
type: VALID_INTERRUPT_TYPES
}

constructor(type: VALID_INTERRUPT_TYPES) {
this[INTERRUPT_KEY] = {
type
}
}

static approval() {
return new Interrupt("approval");
}
}

/**
* @deprecated Use Interrupt.approval() instea
*/
export const approvalRequest = () => {
return {
[INTERRUPT_KEY]: {
Expand Down

0 comments on commit e7574ce

Please sign in to comment.