Skip to content
Merged
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
10 changes: 4 additions & 6 deletions packages/opencode/src/altimate/enhance-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { Agent } from "@/agent/agent"
import { Config } from "@/config/config"
import { Log } from "@/util/log"
import { MessageV2 } from "@/session/message-v2"
import { MessageID, SessionID } from "@/session/schema"

const ENHANCE_NAME = "enhance-prompt"
const ENHANCE_TIMEOUT_MS = 15_000
// MessageV2.User requires branded MessageID/SessionID types, but this is a
// synthetic message that never enters the session store — cast is safe here.
const ENHANCE_ID = ENHANCE_NAME as any

const log = Log.create({ service: ENHANCE_NAME })

Expand Down Expand Up @@ -105,8 +103,8 @@ export async function enhancePrompt(text: string): Promise<string> {
}

const user: MessageV2.User = {
id: ENHANCE_ID,
sessionID: ENHANCE_ID,
id: MessageID.ascending(),
sessionID: SessionID.descending(),
role: "user",
time: { created: Date.now() },
agent: ENHANCE_NAME,
Expand All @@ -124,7 +122,7 @@ export async function enhancePrompt(text: string): Promise<string> {
tools: {},
model,
abort: controller.signal,
sessionID: ENHANCE_ID,
sessionID: user.sessionID,
retries: 2,
messages: [
{
Expand Down
12 changes: 9 additions & 3 deletions packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ export function Prompt(props: PromptProps) {
enabled: !!store.prompt.input,
onSelect: async (dialog) => {
if (!store.prompt.input.trim()) return
if (enhancingInProgress) return
dialog.clear()
const original = store.prompt.input
enhancingInProgress = true
toast.show({
message: "Enhancing prompt...",
variant: "info",
Expand Down Expand Up @@ -241,6 +243,8 @@ export function Prompt(props: PromptProps) {
variant: "error",
duration: 3000,
})
} finally {
enhancingInProgress = false
}
},
},
Expand Down Expand Up @@ -625,9 +629,11 @@ export function Prompt(props: PromptProps) {
enhancingInProgress = true
toast.show({ message: "Enhancing prompt...", variant: "info", duration: 2000 })
const enhanced = await enhancePrompt(inputText)
// Discard if user changed the prompt during enhancement
if (store.prompt.input !== inputText) return
if (enhanced !== inputText) {
// If user changed the prompt during enhancement, skip enhancement
// but continue submission with the original text (don't abandon it)
if (store.prompt.input !== inputText) {
inputText = store.prompt.input
} else if (enhanced !== inputText) {
inputText = enhanced
setStore("prompt", "input", enhanced)
}
Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/test/altimate/enhance-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ mock.module("@/session/message-v2", () => ({
MessageV2: {},
}))

let idCounter = 0
mock.module("@/session/schema", () => ({
MessageID: {
ascending: () => `msg-${++idCounter}`,
},
SessionID: {
descending: () => `session-${++idCounter}`,
},
}))

// Import after mocking
const { enhancePrompt, isAutoEnhanceEnabled } = await import("../../src/altimate/enhance-prompt")

Expand Down
Loading