Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,13 @@ const layer = Layer.effect(
})

const createUserMessage = Effect.fn("SessionPrompt.createUserMessage")(function* (input: PromptInput) {
const agentName = input.agent
const current = yield* db
.select({ agent: SessionTable.agent, model: SessionTable.model })
.from(SessionTable)
.where(eq(SessionTable.id, input.sessionID))
.get()
.pipe(Effect.orDie)
const agentName = input.agent ?? current?.agent
const ag = agentName ? yield* agents.get(agentName) : yield* agents.defaultInfo()
if (!ag) {
const available = (yield* agents.list()).filter((a) => !a.hidden).map((a) => a.name)
Expand All @@ -651,7 +657,13 @@ const layer = Layer.effect(
.getModel(model.providerID, model.modelID)
.pipe(Effect.catchIf(Provider.ModelNotFoundError.isInstance, () => Effect.succeed(undefined)))
: undefined
const variant = input.variant ?? (ag.variant && full?.variants?.[ag.variant] ? ag.variant : undefined)
const sessionVariant =
current?.model?.providerID === model.providerID &&
current.model.id === model.modelID &&
current.model.variant !== "default"
? current.model.variant
: undefined
const variant = input.variant ?? (ag.variant && full?.variants?.[ag.variant] ? ag.variant : sessionVariant)

const info: SessionV1.User = {
id: input.messageID ?? MessageID.ascending(),
Expand All @@ -669,9 +681,8 @@ const layer = Layer.effect(
format: input.format,
}

const current = yield* sessions.get(input.sessionID).pipe(Effect.orDie)
if (
current.agent !== info.agent ||
current?.agent !== info.agent ||
current.model?.providerID !== info.model.providerID ||
current.model?.id !== info.model.modelID ||
(current.model?.variant === "default" ? undefined : current.model?.variant) !== info.model.variant
Expand Down
59 changes: 59 additions & 0 deletions packages/opencode/test/server/httpapi-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,65 @@ describe("session HttpApi", () => {
}).pipe(Effect.provide(TestLLMServer.layer), Effect.provide(AppNodeBuilder.build(CrossSpawnSpawner.node))),
)

it.instance(
"preserves session routing when prompt_async omits agent, model, and variant",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const session = yield* createSession({ title: "prompt routing" })
const headers = { "x-opencode-directory": test.directory, "content-type": "application/json" }
const path = pathFor(SessionPaths.promptAsync, { sessionID: session.id })

const first = yield* request(path, {
method: "POST",
headers,
body: JSON.stringify({
agent: "plan",
model: { providerID: "test", modelID: "test-model" },
variant: "medium",
parts: [{ type: "text", text: "first" }],
noReply: true,
}),
})
expect(first.status).toBe(204)
yield* pollWithTimeout(
requestJson<SessionV1.WithParts[]>(pathFor(SessionPaths.messages, { sessionID: session.id }), {
headers,
}).pipe(
Effect.map((messages) => {
const prompts = messages.filter((message) => message.info.role === "user")
return prompts.length === 1 ? prompts : undefined
}),
),
"timed out waiting for the first prompt_async message",
)

const second = yield* request(path, {
method: "POST",
headers,
body: JSON.stringify({ parts: [{ type: "text", text: "second" }], noReply: true }),
})
expect(second.status).toBe(204)

const users = yield* pollWithTimeout(
requestJson<SessionV1.WithParts[]>(pathFor(SessionPaths.messages, { sessionID: session.id }), {
headers,
}).pipe(
Effect.map((messages) => {
const prompts = messages.filter((message) => message.info.role === "user")
return prompts.length === 2 ? prompts : undefined
}),
),
"timed out waiting for both prompt_async messages",
)
expect(users.at(-1)?.info).toMatchObject({
agent: "plan",
model: { providerID: "test", modelID: "test-model", variant: "medium" },
})
}),
{ git: true, config: { formatter: false, lsp: false } },
)

it.instance(
"returns v2 public request errors for cursor and workspace query failures",
() =>
Expand Down
Loading