Sessions & prompts
A session is one OpenCode session, kept in the Durable Object’s SQLite. You reach it by a name of your choosing, from the object whose harness owns it. A prompt sends it a message and returns OpenCode’s answer.
Name a session
Section titled “Name a session”harness.session(name) is the session with that name. It does nothing by itself:
const session = harness.session("pr-142");The first call on any session starts OpenCode in the Durable Object, which takes a few tens of milliseconds. Later calls, from this instance of the object or a later one, find the session by its name.
Prompt it
Section titled “Prompt it”prompt sends a message and waits for the answer, through every tool call OpenCode makes on the way:
const reply = yield* session.prompt("Why does the login test fail?");return reply.text;reply.text is the answer’s text. reply.messages is what the prompt added to the transcript, as OpenCode stores it: the prompt, each step of the answer with its text, reasoning and tool calls, and an idle entry that ends it.
Choose what a prompt runs with
Section titled “Choose what a prompt runs with”A prompt uses the harness’s first model and every listed tool unless it says otherwise:
const reply = yield* session.prompt("Run the tests and fix what fails.", { model: Smart, tools: [AI.Bash, AI.Read, AI.Edit, RunTests], instructions: "Use pnpm, never npm.",});tools is exact. The model gets these and nothing else, including OpenCode’s own tools. instructions are added to OpenCode’s system prompt for this prompt.
Set defaults for a session
Section titled “Set defaults for a session”The second argument of session is what each of its prompts runs with unless it says otherwise:
const session = harness.session("pr-142", { tools: [AI.CodingTools, RunTests], instructions: "Use pnpm, never npm.",});Make a prompt exactly-once
Section titled “Make a prompt exactly-once”A caller that retries after a timeout must not prompt twice. Give the prompt a requestId:
const reply = yield* session.prompt(text, { requestId: deliveryId });A second prompt with the same id waits for the first and returns its answer. OpenCode stores the id with the prompt, so this holds after the Durable Object restarts too.
Submit without waiting
Section titled “Submit without waiting”submit gives the session a prompt and returns once it is stored. wait returns when the session has nothing left to run:
yield* session.submit("Fix the failing test.");yield* session.wait;A submitted prompt runs on after the call returns. While it runs the harness keeps the Durable Object’s alarm armed, so an object evicted midway wakes up and the prompt resumes within about half a minute.
Prompts take minutes
Section titled “Prompts take minutes”A prompt returns when OpenCode is done, which for real work is minutes. A request that must answer quickly, such as a webhook, can’t wait for it. Submit the prompt, or store the job and run it from the Durable Object’s alarm when something must happen with the answer:
const answer = yield* Alchemy.makeCallback( "answer", Effect.fn(function* (job: { text: string; id: string }) { const reply = yield* session.prompt(job.text, { requestId: job.id }); yield* Effect.log(reply.text); }, Effect.orDie),);The tutorial builds this.
Stop a prompt
Section titled “Stop a prompt”abort stops the prompt that is running:
yield* session.abort;Read the transcript
Section titled “Read the transcript”messages is the session’s transcript, oldest first, as OpenCode stores it. Each entry has a type; an assistant entry is one step of an answer, and its content holds the step’s text and tool calls:
const messages = yield* session.messages;const calls = messages.flatMap((message) => message.type === "assistant" ? (message as OpenCode.AssistantMessage).content.flatMap((content) => content.type === "tool" ? [content.name] : [], ) : [],);A tool entry has the tool’s name as OpenCode knows it and its state: running, completed with its content, or error with its error. In code mode the call is the tool that ran the program: alchemy_codemode, or execute under OpenCode’s own code mode, where state.metadata.toolCalls lists the calls its program made.
Use one of OpenCode’s agents
Section titled “Use one of OpenCode’s agents”agent picks one of OpenCode’s own agents, such as build or plan, or one you define in config:
const reply = yield* session.prompt("Plan the migration.", { agent: "plan" });