| 119 | |
| 120 | export 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 | |
| 142 | function lastAssistantText(messages: RecipeAgent["messages"]): string { |