Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/tui/src/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,7 @@ export function Prompt(props: PromptProps) {
dialog.clear()

// replace summarized text parts with the actual text
const text = store.prompt.parts
.filter((p) => p.type === "text")
.reduce((acc, p) => {
if (!p.source) return acc
return acc.replace(p.source.text.value, p.text)
}, store.prompt.input)
const text = expandPastedTextPlaceholders(store.prompt.input, store.prompt.parts)

const nonTextParts = store.prompt.parts.filter((p) => p.type !== "text")

Expand Down
2 changes: 1 addition & 1 deletion packages/tui/src/prompt/part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function stripPromptPartIDs<Part extends { id: string; messageID: string;
export function expandPastedTextPlaceholders(text: string, parts: readonly unknown[]) {
return parts.reduce<string>((result, part) => {
if (!isPastedTextPart(part)) return result
return result.replace(part.source.text.value, part.text)
return result.replace(part.source.text.value, () => part.text)
}, text)
}

Expand Down
35 changes: 34 additions & 1 deletion packages/tui/test/prompt/part.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { describe, expect, test } from "bun:test"
import { expandTrackedPastedText, stripPromptPartIDs } from "../../src/prompt/part"
import { expandPastedTextPlaceholders, expandTrackedPastedText, stripPromptPartIDs } from "../../src/prompt/part"

function pastedTextPart(marker: string, text: string) {
return { type: "text" as const, text, source: { text: { value: marker } } }
}

describe("prompt part", () => {
test("strips persisted IDs from reused parts", () => {
Expand Down Expand Up @@ -50,4 +54,33 @@ describe("prompt part", () => {
]),
).toBe(`keep ${marker} then alpha\nbeta\ngamma tail`)
})

test("expands placeholders with dollar sequences literally", () => {
const marker = "[Pasted ~1 lines]"
for (const pasted of ["const cost = ${price}$$", "echo $&foo", "a $` b", "x $' y", "price $1 each"]) {
expect(expandPastedTextPlaceholders(`see ${marker} here`, [pastedTextPart(marker, pasted)])).toBe(
`see ${pasted} here`,
)
}
})

test("expands repeated same-line-count placeholders in order", () => {
const marker = "[Pasted ~2 lines]"
expect(
expandPastedTextPlaceholders(`${marker} and ${marker}`, [
pastedTextPart(marker, "first"),
pastedTextPart(marker, "second"),
]),
).toBe("first and second")
})

test("leaves non pasted-text parts untouched", () => {
const marker = "[Pasted ~1 lines]"
expect(
expandPastedTextPlaceholders(`see ${marker} here`, [
{ type: "file", url: "data:," },
pastedTextPart(marker, "$$$"),
]),
).toBe("see $$$ here")
})
})
Loading