| 1 | import { DurableObject } from "cloudflare:workers"; |
| 2 | import { OpenCodeWorkerd } from "@opencode-ai/sdk/workerd"; |
| 3 | |
| 4 | const PROMPT = "hello world"; |
| 5 | |
| 6 | export class OpenCodeDO extends DurableObject { |
| 7 | async fetch(_request) { |
| 8 | const checks = []; |
| 9 | const check = async (name, fn) => { |
| 10 | const started = Date.now(); |
| 11 | try { |
| 12 | const value = await fn(); |
| 13 | checks.push({ name, ok: true, ms: Date.now() - started }); |
| 14 | return value; |
| 15 | } catch (error) { |
| 16 | checks.push({ |
| 17 | name, |
| 18 | ok: false, |
| 19 | ms: Date.now() - started, |
| 20 | error: serializeError(error), |
| 21 | }); |
| 22 | throw error; |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | try { |
| 27 | const opencode = await OpenCodeWorkerd.create({ |
| 28 | storage: this.ctx.storage, |
| 29 | }); |
| 30 | |
| 31 | // The SDK resolves the model from its own provider registry. Report the |
| 32 | // resolution instead of a model this example names: a request for an |
| 33 | // unregistered provider falls back without an error, so a hardcoded name |
| 34 | // in the response would claim a model that never ran. |
| 35 | const model = await check( |
| 36 | "model.default", |
| 37 | () => opencode.model.default(), |
| 38 | ); |
| 39 | const providers = await check( |
| 40 | "provider.list", |
| 41 | () => opencode.provider.list(), |
| 42 | ); |
| 43 | |
| 44 | await check("health.get", () => opencode.health.get()); |
| 45 | const session = await check( |
| 46 | "session.create", |
| 47 | () => opencode.session.create(), |
| 48 | ); |
| 49 | await check( |
| 50 | "session.get", |
| 51 | () => opencode.session.get({ sessionID: session.id }), |
| 52 | ); |
| 53 | |
| 54 | // `prompt` only enqueues the message and returns before the model runs. |
| 55 | // `wait` blocks until the run completes, so a probe that stops at |
| 56 | // `prompt` reports success even when no model answers. |
| 57 | await check( |
| 58 | "session.prompt", |
| 59 | () => opencode.session.prompt({ sessionID: session.id, text: PROMPT }), |
| 60 | ); |
| 61 | await check( |
| 62 | "session.wait", |
| 63 | () => opencode.session.wait({ sessionID: session.id }), |
| 64 | ); |
| 65 | |
| 66 | const messages = await check( |
| 67 | "message.list", |
| 68 | () => opencode.message.list({ sessionID: session.id }), |
| 69 | ); |
| 70 | |
| 71 | const reply = assistantText(messages); |
| 72 | if (!reply) { |
| 73 | return Response.json({ |
| 74 | ok: false, |
| 75 | error: "the session completed without an assistant reply", |
| 76 | model: modelRef(model), |
| 77 | checks, |
| 78 | }, { status: 502 }); |
| 79 | } |
| 80 | |
| 81 | return Response.json({ |
| 82 | ok: true, |
| 83 | model: modelRef(model), |
| 84 | providers: (providers?.data ?? providers ?? []).map((entry) => |
| 85 | entry.id |
| 86 | ), |
| 87 | prompt: PROMPT, |
| 88 | reply, |
| 89 | checks, |
| 90 | }); |
| 91 | } catch (error) { |
| 92 | return Response.json({ |
| 93 | ok: false, |
| 94 | error: serializeError(error), |
| 95 | checks, |
| 96 | }, { status: 500 }); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | function modelRef(model) { |
| 102 | const data = model?.data ?? model; |
| 103 | return { |
| 104 | providerID: data?.providerID ?? null, |
| 105 | modelID: data?.modelID ?? null, |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | // An assistant reply is one entry of a `content` array that also carries |
| 110 | // reasoning and tool parts. Join the text parts only, because a reasoning part |
| 111 | // is present even when the model produced no answer. |
| 112 | function assistantText(messages) { |
| 113 | const rows = messages?.data ?? messages ?? []; |
| 114 | for (const message of rows) { |
| 115 | if (message?.type !== "assistant") continue; |
| 116 | const text = (message.content ?? []) |
| 117 | .filter((part) => part?.type === "text") |
| 118 | .map((part) => part.text) |
| 119 | .join("") |
| 120 | .trim(); |
| 121 | if (text) return text; |
| 122 | } |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | function serializeError(value, seen = new Set()) { |
| 127 | if (value === null || typeof value !== "object") return value; |
| 128 | if (seen.has(value)) return "[circular]"; |
| 129 | seen.add(value); |
| 130 | return { |
| 131 | name: value.name, |
| 132 | message: value.message, |
| 133 | stack: value.stack, |
| 134 | cause: serializeError(value.cause, seen), |
| 135 | ...Object.fromEntries(Object.entries(value)), |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | export default { |
| 140 | fetch(request, env) { |
| 141 | const id = env.OPENCODE.idFromName("reporter-main-f4e19cd2"); |
| 142 | return env.OPENCODE.get(id).fetch(request); |
| 143 | }, |
| 144 | }; |