Skip to content

Commit a385fd6

Browse files
committed
add file/agent mentions to /tui/prompt-append
1 parent 52e986e commit a385fd6

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
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
@@ -120,7 +120,33 @@ export function Prompt(props: PromptProps) {
120120

121121
event.on(TuiEvent.PromptAppend.type, (evt) => {
122122
if (!input || input.isDestroyed) return
123+
const oldLength = input.plainText.length
123124
input.insertText(evt.properties.text)
125+
126+
if (evt.properties.parts.length > 0) {
127+
for (const part of evt.properties.parts) {
128+
const extmarkId =
129+
part.source &&
130+
input.extmarks.create({
131+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
132+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
133+
virtual: true,
134+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
135+
typeId: promptPartTypeId,
136+
})
137+
138+
setStore(
139+
produce((draft) => {
140+
const partIndex = draft.prompt.parts.length
141+
draft.prompt.parts.push(part)
142+
if (extmarkId !== undefined) {
143+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
144+
}
145+
}),
146+
)
147+
}
148+
}
149+
124150
setTimeout(() => {
125151
// setTimeout is a workaround and needs to be addressed properly
126152
if (!input || input.isDestroyed) return

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import { BusEvent } from "@/bus/bus-event"
22
import { Bus } from "@/bus"
33
import { SessionID } from "@/session/schema"
4+
import { MessageV2 } from "@/session/message-v2"
45
import z from "zod"
56

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

packages/opencode/src/server/routes/tui.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describeRoute, validator, resolver } from "hono-openapi"
33
import z from "zod"
44
import { Bus } from "../../bus"
55
import { Session } from "../../session"
6+
import { SessionPrompt } from "../../session/prompt"
67
import { TuiEvent } from "@/cli/cmd/tui/event"
78
import { AsyncQueue } from "../../util/queue"
89
import { errors } from "../error"
@@ -95,9 +96,15 @@ export const TuiRoutes = lazy(() =>
9596
...errors(400),
9697
},
9798
}),
98-
validator("json", TuiEvent.PromptAppend.properties),
99+
validator("json", z.object({ text: z.string() })),
99100
async (c) => {
100-
await Bus.publish(TuiEvent.PromptAppend, c.req.valid("json"))
101+
const { text } = c.req.valid("json")
102+
await Bus.publish(TuiEvent.PromptAppend, {
103+
text,
104+
parts: (await SessionPrompt.resolvePromptParts(text)).filter(
105+
(part) => part.type === "agent" || part.type === "file",
106+
),
107+
})
101108
return c.json(true)
102109
},
103110
)

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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,24 @@ export type EventTuiPromptAppend = {
241241
type: "tui.prompt.append"
242242
properties: {
243243
text: string
244+
parts: Array<
245+
| {
246+
type: "agent"
247+
name: string
248+
source?: {
249+
value: string
250+
start: number
251+
end: number
252+
}
253+
}
254+
| {
255+
type: "file"
256+
mime: string
257+
filename?: string
258+
url: string
259+
source?: FilePartSource
260+
}
261+
>
244262
}
245263
}
246264

0 commit comments

Comments
 (0)