MCPcopy Create free account
hub / github.com/cloudflare/computer / fetch

Function fetch

examples/tutorial/src/index.ts:121–139  ·  view source on GitHub ↗
(request: Request, env: Env)

Source from the content-addressed store, hash-verified

119
120export default {
121 async fetch(request: Request, env: Env): Promise<Response> {
122 const url = new URL(request.url);
123
124 if (request.method === "POST" && url.pathname === "/prompt") {
125 const { prompt } = (await request.json()) as { prompt?: string };
126 if (typeof prompt !== "string" || prompt.trim() === "") {
127 return Response.json({ error: "prompt must be a non-empty string" }, { status: 400 });
128 }
129 // A fresh durable object per request, so every card starts from
130 // an empty workspace and an empty conversation.
131 const agent = await getAgentByName<Env, RecipeAgent>(env.RecipeAgent, crypto.randomUUID());
132 return Response.json(await agent.card(prompt));
133 }
134
135 return new Response('recipe card example\n\nPOST /prompt {"prompt":"chili con carne"}\n', {
136 status: 404,
137 headers: { "content-type": "text/plain; charset=utf-8" },
138 });
139 },
140} satisfies ExportedHandler<Env>;
141
142function lastAssistantText(messages: RecipeAgent["messages"]): string {

Callers

nothing calls this directly

Calls 2

cardMethod · 0.80
jsonMethod · 0.65

Tested by

no test coverage detected