Skip to content

Commit 030d134

Browse files
committed
add file/agent mentions to /tui/prompt-append
1 parent e088068 commit 030d134

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
@@ -250,8 +250,33 @@ export function Prompt(props: PromptProps) {
250250
})
251251

252252
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
253+
const oldLength = input.plainText.length
253254
input.gotoBufferEnd()
254255
input.insertText(evt.properties.text)
256+
257+
if (evt.properties.parts.length > 0) {
258+
for (const part of evt.properties.parts) {
259+
const extmarkId =
260+
part.source &&
261+
input.extmarks.create({
262+
start: oldLength + (part.type === "file" ? part.source.text.start : part.source.start),
263+
end: oldLength + (part.type === "file" ? part.source.text.end : part.source.end),
264+
virtual: true,
265+
styleId: part.type === "agent" ? agentStyleId : fileStyleId,
266+
typeId: promptPartTypeId,
267+
})
268+
269+
setStore(
270+
produce((draft) => {
271+
const partIndex = draft.prompt.parts.length
272+
draft.prompt.parts.push(part)
273+
if (extmarkId !== undefined) {
274+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
275+
}
276+
}),
277+
)
278+
}
279+
}
255280
})
256281

257282
createEffect(() => {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { Bus } from "@/bus"
2+
import { MessageV2 } from "@/session/message-v2"
23
import z from "zod"
34

45
export const TuiEvent = {
5-
PromptAppend: Bus.event("tui.prompt.append", z.object({ text: z.string() })),
6+
PromptAppend: Bus.event(
7+
"tui.prompt.append",
8+
z.object({
9+
text: z.string(),
10+
parts: z.array(
11+
z.union([
12+
MessageV2.AgentPart.omit({ id: true, sessionID: true, messageID: true }),
13+
MessageV2.FilePart.omit({ id: true, sessionID: true, messageID: true }),
14+
]),
15+
),
16+
}),
17+
),
618
CommandExecute: Bus.event(
719
"tui.command.execute",
820
z.object({

packages/opencode/src/server/server.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,10 +1489,25 @@ export namespace Server {
14891489
...errors(400),
14901490
},
14911491
}),
1492-
validator("json", TuiEvent.PromptAppend.properties),
1492+
validator("json", z.object({ text: z.string() })),
14931493
async (c) => {
1494-
await Bus.publish(TuiEvent.PromptAppend, c.req.valid("json"))
1495-
return c.json(true)
1494+
const { text } = c.req.valid("json")
1495+
1496+
try {
1497+
await Bus.publish(TuiEvent.PromptAppend, {
1498+
text,
1499+
parts: (await SessionPrompt.resolvePromptParts(text)).filter(
1500+
(part) => part.type === "agent" || part.type === "file",
1501+
),
1502+
})
1503+
return c.json(true)
1504+
} catch (error) {
1505+
log.error("Failed to process prompt append", {
1506+
text,
1507+
error: error instanceof Error ? error.message : String(error),
1508+
})
1509+
return c.json(false)
1510+
}
14961511
},
14971512
)
14981513
.post(

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,24 @@ export type EventTuiPromptAppend = {
535535
type: "tui.prompt.append"
536536
properties: {
537537
text: string
538+
parts: Array<
539+
| {
540+
type: "agent"
541+
name: string
542+
source?: {
543+
value: string
544+
start: number
545+
end: number
546+
}
547+
}
548+
| {
549+
type: "file"
550+
mime: string
551+
filename?: string
552+
url: string
553+
source?: FilePartSource
554+
}
555+
>
538556
}
539557
}
540558

0 commit comments

Comments
 (0)