-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce ValTown adapter with function registration and execution
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |