Skip to content

Commit 4e3274d

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

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

9797
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
9898
if (!input || input.isDestroyed) return
99+
const oldLength = input.plainText.length
99100
input.insertText(evt.properties.text)
101+
102+
if (evt.properties.parts.length > 0) {
103+
for (const part of evt.properties.parts) {
104+
const extmarkId =
105+
part.source &&
106+
input.extmarks.create({
107+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
108+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
109+
virtual: true,
110+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
111+
typeId: promptPartTypeId,
112+
})
113+
114+
setStore(
115+
produce((draft) => {
116+
const partIndex = draft.prompt.parts.length
117+
draft.prompt.parts.push(part)
118+
if (extmarkId !== undefined) {
119+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
120+
}
121+
}),
122+
)
123+
}
124+
}
125+
100126
setTimeout(() => {
101127
// setTimeout is a workaround and needs to be addressed properly
102128
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)