Skip to content

Commit

Permalink
feat: Introduce ValTown adapter with function registration and execution
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeesha committed Dec 31, 2024
1 parent ffde4a8 commit 9f927b0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
6 changes: 6 additions & 0 deletions adapters/valtown-adapter/jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@inferable/valtown-adapter",
"version": "0.0.1",
"license": "MIT",
"exports": "./mod.ts"
}
119 changes: 119 additions & 0 deletions adapters/valtown-adapter/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
export class InferableService {
private functions: Parameters<InferableService["registerFunction"]>[0][] = [];

constructor(
private options: {
description: string;
},
) {}

registerFunction(options: {
name: string;
description: string;
handler: (input: any) => Promise<any>;
input: {
type: "object";
properties: {
[key: string]: any;
};
required: string[];
};
}): void {
this.functions.push(options);
}

getServer(): (request: Request) => Promise<Response> {
const server = async (request: Request): Promise<Response> => {
const url = new URL(request.url);
const path = url.pathname;

// Metadata route
if (path === "/meta") {
return new Response(
JSON.stringify({
description: this.options.description,
functions: Object.values(this.functions).map(func => ({
name: func.name,
description: func.description,
input: func.input,
})),
}),
{
headers: { "content-type": "application/json" },
},
);
}

// Execution route
if (path.startsWith("/exec/functions/")) {
const functionName = path.split("/")[3];

if (request.method !== "POST") {
return new Response(JSON.stringify({ error: "Method not allowed" }), {
status: 405,
headers: { "content-type": "application/json" },
});
}

try {
const input = await request.json();

const handler = this.functions.find(func => func.name === functionName);

if (!handler) {
return new Response(JSON.stringify({ error: `Function ${functionName} not found` }), {
status: 404,
headers: { "content-type": "application/json" },
});
}

return new Response(JSON.stringify({ result: await handler.handler(input) }));
} catch (error) {
return new Response(JSON.stringify({ error: `Error occurred during execution: ${error}` }), {
status: 400,
headers: { "content-type": "application/json" },
});
}
}

// Default route
return new Response(
JSON.stringify({
error: `Route not found: ${path}`,
}),
{
status: 404,
headers: { "content-type": "application/json" },
},
);
};

return server;
}
}

//
// Usage
//

// const service = new InferableService({
// description: "My functions",
// });

// service.registerFunction({
// name: "sum",
// description: "Sum two numbers",
// handler: (input: { a: number; b: number }) => Promise.resolve(input.a + input.b),
// input: {
// type: "object",
// properties: {
// a: { type: "number" },
// b: { type: "number" },
// },
// required: ["a", "b"],
// },
// });

// const server = service.getServer();

// export default server;

0 comments on commit 9f927b0

Please sign in to comment.