Effect CommunityEC
Effect Community9mo ago
1 reply
Peter

Routing HTTP path strings...without HTTP?

bit of a weird one, hear me out! i'm working on an application which uses llms + tools to hit some "endpoints" like /api/v1/posts /api/v1/posts/1234. i've put endpoints in quotes because, while they are currently implemented as endpoints, that is really just an abstraction i've found useful for language models - they are extremely reliable at generating well formed URLs. in reality, these endpoints do not require a full HTTP server, they just need to map from string -> function.

for clarity, you can imagine my LLM tool to be defined as:

tool({
  name: "getDataFromAPI",
  schema: z.string().describe("... my formal spec of my fake 'api' routes ....",
  execute: (endpoint: string) => return fetch(endpoint).then(r => r.json) // error handling omitted.
})



the issue here is the fetch. there is no reason perform a real fetch request to a real endpoint when the API itself is just a construct for the llm. in reality, it would be much more efficient to:

tool({
  name: "getDataFromAPI",
  schema: z.string().describe("... my formal spec of my 'api' routes ....",
  execute: (endpoint: string) => match(endpoint, {
    "/api/v1/posts": () => getPosts(),
    "/api/v1/posts/:id", ({id}) => getPostByID(id)
  })
})


of couse, we need the match function. basically a router. i could build my own, but i'm wondering if it's possible to use the internal HTTPRouter to just match strings -> functions and skip the HTTP transport entirely.


LMK!
Was this page helpful?