Skip to content

Commit

Permalink
feat: Initial function initiated approvals
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjcsmith committed Nov 19, 2024
1 parent db2c3e3 commit 30fa570
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 3 deletions.
70 changes: 68 additions & 2 deletions sdk-node/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ export const blobSchema = z.object({
workflowId: z.string().nullable(),
});

export const interruptSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("adhoc"),
}),
z.object({
type: z.literal("request"),
}),
z.object({
type: z.literal("sleep"),
seconds: z.number(),
})
])

export const VersionedTextsSchema = z.object({
current: z.object({
version: z.string(),
Expand All @@ -46,6 +59,24 @@ export const VersionedTextsSchema = z.object({
),
});

export const integrationSchema = z.object({
toolhouse: z
.object({
apiKey: z.string(),
})
.optional()
.nullable(),
langfuse: z
.object({
publicKey: z.string(),
secretKey: z.string(),
baseUrl: z.string(),
sendMessagePayloads: z.boolean(),
})
.optional()
.nullable(),
});

export const genericMessageDataSchema = z
.object({
message: z.string(),
Expand Down Expand Up @@ -233,6 +264,37 @@ export const definition = {
.describe("Human readable description of the cluster"),
}),
},
upsertIntegrations: {
method: "PUT",
path: "/clusters/:clusterId/integrations",
headers: z.object({
authorization: z.string(),
}),
responses: {
200: z.undefined(),
401: z.undefined(),
400: z.object({
issues: z.array(z.any()),
}),
},
pathParams: z.object({
clusterId: z.string(),
}),
body: integrationSchema,
},
getIntegrations: {
method: "GET",
path: "/clusters/:clusterId/integrations",
headers: z.object({
authorization: z.string(),
}),
responses: {
200: integrationSchema,
},
pathParams: z.object({
clusterId: z.string(),
}),
},
deleteCluster: {
method: "DELETE",
path: "/clusters/:clusterId",
Expand Down Expand Up @@ -266,6 +328,8 @@ export const definition = {
.describe(
"Enable additional logging (Including prompts and results) for use by Inferable support",
),
enableRunConfigs: z.boolean().optional(),
enableKnowledgebase: z.boolean().optional(),
}),
},
getCluster: {
Expand All @@ -282,6 +346,8 @@ export const definition = {
additionalContext: VersionedTextsSchema.nullable(),
createdAt: z.date(),
debug: z.boolean(),
enableRunConfigs: z.boolean(),
enableKnowledgebase: z.boolean(),
lastPingAt: z.date().nullable(),
}),
401: z.undefined(),
Expand Down Expand Up @@ -1344,7 +1410,7 @@ export const definition = {
200: z.object({
id: z.string(),
result: z.any().nullable(),
resultType: z.enum(["resolution", "rejection"]).nullable(),
resultType: z.enum(["resolution", "rejection", "interrupt"]).nullable(),
status: z.enum(["pending", "running", "success", "failure", "stalled"]),
}),
},
Expand All @@ -1366,7 +1432,7 @@ export const definition = {
},
body: z.object({
result: z.any(),
resultType: z.enum(["resolution", "rejection"]),
resultType: z.enum(["resolution", "rejection", "interrupt"]),
meta: z.object({
functionExecutionTime: z.number().optional(),
}),
Expand Down
13 changes: 12 additions & 1 deletion sdk-node/src/execute-fn.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { serializeError } from "./serialize-error";
import { FunctionRegistration } from "./types";
import { extractInterrupt } from "./util";

export type Result<T = unknown> = {
content: T;
type: "resolution" | "rejection";
type: "resolution" | "rejection" | "interrupt";
functionExecutionTime?: number;
};

Expand All @@ -15,6 +16,16 @@ export const executeFn = async (
try {
const result = await fn(...args);

const interupt = extractInterrupt(result);

if (interupt) {
return {
content: interupt,
type: "interrupt",
functionExecutionTime: Date.now() - start,
};
}

return {
content: result,
type: "resolution",
Expand Down
3 changes: 3 additions & 0 deletions sdk-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export {
validateFunctionSchema,
validateFunctionArgs,
blob,
interrupt,
sleep,
requestApproval
} from "./util";

export { createApiClient } from "./create-client";
54 changes: 54 additions & 0 deletions sdk-node/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,57 @@ export const blob = ({
},
};
};


export const INTERRUPT_KEY = "__inferable_interrupt";
const interruptResultSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("adhoc"),
}),
z.object({
type: z.literal("approval"),
}),
z.object({
type: z.literal("sleep"),
seconds: z.number(),
})
])

export const extractInterrupt = (input: unknown): z.infer<typeof interruptResultSchema> | undefined => {
if (input && typeof input === "object" && INTERRUPT_KEY in input) {
const parsedInterrupt = interruptResultSchema.safeParse(input[INTERRUPT_KEY]);

if (!parsedInterrupt.success) {
throw new InferableError("Found invalid Interrupt data");
}

return parsedInterrupt.data;
}
}


export const interrupt = () => {
return {
[INTERRUPT_KEY]: {
type: "adhoc",
},
};
};

export const sleep = (seconds: number) => {
return {
[INTERRUPT_KEY]: {
type: "sleep",
seconds,
},
};
};

export const requestApproval = () => {
return {
[INTERRUPT_KEY]: {
type: "approval",
},
};
};

0 comments on commit 30fa570

Please sign in to comment.