Skip to content

Commit 8578c54

Browse files
committed
add file/agent mentions to /tui/prompt-append
1 parent bd6f0d3 commit 8578c54

5 files changed

Lines changed: 84 additions & 3 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,33 @@ export function Prompt(props: PromptProps) {
9898

9999
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
100100
if (!input || input.isDestroyed) return
101+
const oldLength = input.plainText.length
101102
input.insertText(evt.properties.text)
103+
104+
if (evt.properties.parts.length > 0) {
105+
for (const part of evt.properties.parts) {
106+
const extmarkId =
107+
part.source &&
108+
input.extmarks.create({
109+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
110+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
111+
virtual: true,
112+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
113+
typeId: promptPartTypeId,
114+
})
115+
116+
setStore(
117+
produce((draft) => {
118+
const partIndex = draft.prompt.parts.length
119+
draft.prompt.parts.push(part)
120+
if (extmarkId !== undefined) {
121+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
122+
}
123+
}),
124+
)
125+
}
126+
}
127+
102128
setTimeout(() => {
103129
// setTimeout is a workaround and needs to be addressed properly
104130
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,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/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
@@ -719,6 +719,24 @@ export type EventTuiPromptAppend = {
719719
type: "tui.prompt.append"
720720
properties: {
721721
text: string
722+
parts: Array<
723+
| {
724+
type: "agent"
725+
name: string
726+
source?: {
727+
value: string
728+
start: number
729+
end: number
730+
}
731+
}
732+
| {
733+
type: "file"
734+
mime: string
735+
filename?: string
736+
url: string
737+
source?: FilePartSource
738+
}
739+
>
722740
}
723741
}
724742

0 commit comments

Comments
 (0)