Skip to content

Commit ab8f9c1

Browse files
committed
add file/agent mentions to /tui/prompt-append
1 parent 8a039e2 commit ab8f9c1

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
@@ -126,7 +126,33 @@ export function Prompt(props: PromptProps) {
126126

127127
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
128128
if (!input || input.isDestroyed) return
129+
const oldLength = input.plainText.length
129130
input.insertText(evt.properties.text)
131+
132+
if (evt.properties.parts.length > 0) {
133+
for (const part of evt.properties.parts) {
134+
const extmarkId =
135+
part.source &&
136+
input.extmarks.create({
137+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
138+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
139+
virtual: true,
140+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
141+
typeId: promptPartTypeId,
142+
})
143+
144+
setStore(
145+
produce((draft) => {
146+
const partIndex = draft.prompt.parts.length
147+
draft.prompt.parts.push(part)
148+
if (extmarkId !== undefined) {
149+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
150+
}
151+
}),
152+
)
153+
}
154+
}
155+
130156
setTimeout(() => {
131157
// setTimeout is a workaround and needs to be addressed properly
132158
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
@@ -272,6 +272,24 @@ export type EventTuiPromptAppend = {
272272
type: "tui.prompt.append"
273273
properties: {
274274
text: string
275+
parts: Array<
276+
| {
277+
type: "agent"
278+
name: string
279+
source?: {
280+
value: string
281+
start: number
282+
end: number
283+
}
284+
}
285+
| {
286+
type: "file"
287+
mime: string
288+
filename?: string
289+
url: string
290+
source?: FilePartSource
291+
}
292+
>
275293
}
276294
}
277295

0 commit comments

Comments
 (0)