Skip to content

Commit c8e214a

Browse files
committed
add file/agent mentions to /tui/prompt-append
1 parent cdc1cbf commit c8e214a

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,33 @@ export function Prompt(props: PromptProps) {
267267
})
268268

269269
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
270+
const oldLength = input.plainText.length
270271
input.gotoBufferEnd()
271272
input.insertText(evt.properties.text)
273+
274+
if (evt.properties.parts.length > 0) {
275+
for (const part of evt.properties.parts) {
276+
const extmarkId =
277+
part.source &&
278+
input.extmarks.create({
279+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
280+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
281+
virtual: true,
282+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
283+
typeId: promptPartTypeId,
284+
})
285+
286+
setStore(
287+
produce((draft) => {
288+
const partIndex = draft.prompt.parts.length
289+
draft.prompt.parts.push(part)
290+
if (extmarkId !== undefined) {
291+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
292+
}
293+
}),
294+
)
295+
}
296+
}
272297
})
273298

274299
createEffect(() => {

packages/opencode/src/cli/cmd/tui/event.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { Bus } from "@/bus"
2+
import { MessageV2 } from "@/session/message-v2"
23
import z from "zod"
34

45
export const TuiEvent = {
5-
PromptAppend: Bus.event("tui.prompt.append", z.object({ text: z.string() })),
6+
PromptAppend: Bus.event(
7+
"tui.prompt.append",
8+
z.object({
9+
text: z.string(),
10+
parts: z.array(
11+
z.union([
12+
MessageV2.AgentPart.omit({ id: true, sessionID: true, messageID: true }),
13+
MessageV2.FilePart.omit({ id: true, sessionID: true, messageID: true }),
14+
]),
15+
),
16+
}),
17+
),
618
CommandExecute: Bus.event(
719
"tui.command.execute",
820
z.object({

packages/opencode/src/server/server.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,10 +1861,25 @@ export namespace Server {
18611861
...errors(400),
18621862
},
18631863
}),
1864-
validator("json", TuiEvent.PromptAppend.properties),
1864+
validator("json", z.object({ text: z.string() })),
18651865
async (c) => {
1866-
await Bus.publish(TuiEvent.PromptAppend, c.req.valid("json"))
1867-
return c.json(true)
1866+
const { text } = c.req.valid("json")
1867+
1868+
try {
1869+
await Bus.publish(TuiEvent.PromptAppend, {
1870+
text,
1871+
parts: (await SessionPrompt.resolvePromptParts(text)).filter(
1872+
(part) => part.type === "agent" || part.type === "file",
1873+
),
1874+
})
1875+
return c.json(true)
1876+
} catch (error) {
1877+
log.error("Failed to process prompt append", {
1878+
text,
1879+
error: error instanceof Error ? error.message : String(error),
1880+
})
1881+
return c.json(false)
1882+
}
18681883
},
18691884
)
18701885
.post(

packages/sdk/js/src/gen/types.gen.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,24 @@ export type EventTuiPromptAppend = {
615615
type: "tui.prompt.append"
616616
properties: {
617617
text: string
618+
parts: Array<
619+
| {
620+
type: "agent"
621+
name: string
622+
source?: {
623+
value: string
624+
start: number
625+
end: number
626+
}
627+
}
628+
| {
629+
type: "file"
630+
mime: string
631+
filename?: string
632+
url: string
633+
source?: FilePartSource
634+
}
635+
>
618636
}
619637
}
620638

0 commit comments

Comments
 (0)