Skip to content

Commit e66b1d5

Browse files
committed
add file/agent mentions to /tui/prompt-append
1 parent 023ef5d commit e66b1d5

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,33 @@ export function Prompt(props: PromptProps) {
9292
let promptPartTypeId: number
9393

9494
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
95+
const oldLength = input.plainText.length
9596
input.insertText(evt.properties.text)
97+
98+
if (evt.properties.parts.length > 0) {
99+
for (const part of evt.properties.parts) {
100+
const extmarkId =
101+
part.source &&
102+
input.extmarks.create({
103+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
104+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
105+
virtual: true,
106+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
107+
typeId: promptPartTypeId,
108+
})
109+
110+
setStore(
111+
produce((draft) => {
112+
const partIndex = draft.prompt.parts.length
113+
draft.prompt.parts.push(part)
114+
if (extmarkId !== undefined) {
115+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
116+
}
117+
}),
118+
)
119+
}
120+
}
121+
96122
setTimeout(() => {
97123
input.getLayoutNode().markDirty()
98124
input.gotoBufferEnd()

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import { BusEvent } from "@/bus/bus-event"
22
import { Bus } from "@/bus"
3+
import { MessageV2 } from "@/session/message-v2"
34
import z from "zod"
45

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

packages/opencode/src/server/server.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,10 +2437,25 @@ export namespace Server {
24372437
...errors(400),
24382438
},
24392439
}),
2440-
validator("json", TuiEvent.PromptAppend.properties),
2440+
validator("json", z.object({ text: z.string() })),
24412441
async (c) => {
2442-
await Bus.publish(TuiEvent.PromptAppend, c.req.valid("json"))
2443-
return c.json(true)
2442+
const { text } = c.req.valid("json")
2443+
2444+
try {
2445+
await Bus.publish(TuiEvent.PromptAppend, {
2446+
text,
2447+
parts: (await SessionPrompt.resolvePromptParts(text)).filter(
2448+
(part) => part.type === "agent" || part.type === "file",
2449+
),
2450+
})
2451+
return c.json(true)
2452+
} catch (error) {
2453+
log.error("Failed to process prompt append", {
2454+
text,
2455+
error: error instanceof Error ? error.message : String(error),
2456+
})
2457+
return c.json(false)
2458+
}
24442459
},
24452460
)
24462461
.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)