diff options
| author | Adam <[email protected]> | 2026-01-05 15:11:55 -0600 |
|---|---|---|
| committer | Adam <[email protected]> | 2026-01-05 15:12:02 -0600 |
| commit | 2ca0ae77557ae759f8463f82f67df4a132f5f749 (patch) | |
| tree | 3dbf26803d41c8140eeb2ce1f7d7076429fe59e6 /packages/app/src/utils | |
| parent | 19123b6803d1720d83dfd2388b0396971ddb179e (diff) | |
| download | opencode-2ca0ae77557ae759f8463f82f67df4a132f5f749.tar.gz opencode-2ca0ae77557ae759f8463f82f67df4a132f5f749.zip | |
fix(app): more defensive, handle no git
Diffstat (limited to 'packages/app/src/utils')
| -rw-r--r-- | packages/app/src/utils/prompt.ts | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/packages/app/src/utils/prompt.ts b/packages/app/src/utils/prompt.ts index 29a774c2a..5d9edfed1 100644 --- a/packages/app/src/utils/prompt.ts +++ b/packages/app/src/utils/prompt.ts @@ -53,9 +53,25 @@ function textPartValue(parts: Part[]) { * Extract prompt content from message parts for restoring into the prompt input. * This is used by undo to restore the original user prompt. */ -export function extractPromptFromParts(parts: Part[]): Prompt { +export function extractPromptFromParts(parts: Part[], opts?: { directory?: string }): Prompt { const textPart = textPartValue(parts) const text = textPart?.text ?? "" + const directory = opts?.directory + + const toRelative = (path: string) => { + if (!directory) return path + + const prefix = directory.endsWith("/") ? directory : directory + "/" + if (path.startsWith(prefix)) return path.slice(prefix.length) + + if (path.startsWith(directory)) { + const next = path.slice(directory.length) + if (next.startsWith("/")) return next.slice(1) + return next + } + + return path + } const inline: Inline[] = [] const images: ImageAttachmentPart[] = [] @@ -78,7 +94,7 @@ export function extractPromptFromParts(parts: Part[]): Prompt { start, end, value, - path, + path: toRelative(path), selection: selectionFromFileUrl(filePart.url), }) continue @@ -158,20 +174,21 @@ export function extractPromptFromParts(parts: Part[]): Prompt { for (const item of inline) { if (item.start < 0 || item.end < item.start) continue - if (item.end > text.length) continue - if (item.start < cursor) continue - pushText(text.slice(cursor, item.start)) + const expected = item.value + if (!expected) continue - if (item.type === "file") { - pushFile(item) - } + const mismatch = item.end > text.length || item.start < cursor || text.slice(item.start, item.end) !== expected + const start = mismatch ? text.indexOf(expected, cursor) : item.start + if (start === -1) continue + const end = mismatch ? start + expected.length : item.end - if (item.type === "agent") { - pushAgent(item) - } + pushText(text.slice(cursor, start)) + + if (item.type === "file") pushFile(item) + if (item.type === "agent") pushAgent(item) - cursor = item.end + cursor = end } pushText(text.slice(cursor)) |
