Skip to content

Commit f1596f5

Browse files
committed
add file/agent mentions to /tui/prompt-append
1 parent 4ca11da commit f1596f5

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
@@ -309,8 +309,33 @@ export function Prompt(props: PromptProps) {
309309
})
310310

311311
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
312+
const oldLength = input.plainText.length
312313
input.gotoBufferEnd()
313314
input.insertText(evt.properties.text)
315+
316+
if (evt.properties.parts.length > 0) {
317+
for (const part of evt.properties.parts) {
318+
const extmarkId =
319+
part.source &&
320+
input.extmarks.create({
321+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
322+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
323+
virtual: true,
324+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
325+
typeId: promptPartTypeId,
326+
})
327+
328+
setStore(
329+
produce((draft) => {
330+
const partIndex = draft.prompt.parts.length
331+
draft.prompt.parts.push(part)
332+
if (extmarkId !== undefined) {
333+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
334+
}
335+
}),
336+
)
337+
}
338+
}
314339
})
315340

316341
createEffect(() => {

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
@@ -2164,10 +2164,25 @@ export namespace Server {
21642164
...errors(400),
21652165
},
21662166
}),
2167-
validator("json", TuiEvent.PromptAppend.properties),
2167+
validator("json", z.object({ text: z.string() })),
21682168
async (c) => {
2169-
await Bus.publish(TuiEvent.PromptAppend, c.req.valid("json"))
2170-
return c.json(true)
2169+
const { text } = c.req.valid("json")
2170+
2171+
try {
2172+
await Bus.publish(TuiEvent.PromptAppend, {
2173+
text,
2174+
parts: (await SessionPrompt.resolvePromptParts(text)).filter(
2175+
(part) => part.type === "agent" || part.type === "file",
2176+
),
2177+
})
2178+
return c.json(true)
2179+
} catch (error) {
2180+
log.error("Failed to process prompt append", {
2181+
text,
2182+
error: error instanceof Error ? error.message : String(error),
2183+
})
2184+
return c.json(false)
2185+
}
21712186
},
21722187
)
21732188
.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)