Run the agent.
(prompt: string)
| 86 | |
| 87 | /** Run the agent. */ |
| 88 | async card(prompt: string): Promise<{ url: string; summary: string }> { |
| 89 | try { |
| 90 | await this.workspace.fs.mkdir("/workspace", { recursive: true }); |
| 91 | const turn = await this.saveMessages([ |
| 92 | { id: crypto.randomUUID(), role: "user", parts: [{ type: "text", text: prompt }] }, |
| 93 | ]); |
| 94 | if (turn.status !== "completed") { |
| 95 | throw new Error(`Agent turn ${turn.status}: ${turn.error ?? "no detail"}`); |
| 96 | } |
| 97 | const written = await this.workspace.fs.stat("/workspace/card.pdf").catch(() => null); |
| 98 | if (!written?.isFile) throw new Error("The agent produced no PDF at /workspace/card.pdf"); |
| 99 | const assets = createAssets({ |
| 100 | ws: this.workspace, |
| 101 | bucket: this.env.CARDS, |
| 102 | s3: { |
| 103 | bucket: this.env.CARDS_BUCKET_NAME, |
| 104 | accountId: this.env.CLOUDFLARE_ACCOUNT_ID, |
| 105 | accessKeyId: this.env.R2_ACCESS_KEY_ID, |
| 106 | secretAccessKey: this.env.R2_SECRET_ACCESS_KEY, |
| 107 | }, |
| 108 | }); |
| 109 | const url = await assets.share("/workspace/card.pdf", { |
| 110 | expiresAfter: 24 * 60 * 60 * 1000, |
| 111 | prefix: "cards", |
| 112 | }); |
| 113 | return { url, summary: lastAssistantText(this.messages) }; |
| 114 | } finally { |
| 115 | await this.workspace.close(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | export default { |
no test coverage detected