From e7957fd67d334c8477b364c7615d19ef0ee4272a Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Thu, 6 Nov 2025 09:42:52 +0000 Subject: [PATCH] Resolve pasted placeholders before sending prompts --- src/stores/sessions.ts | 53 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/src/stores/sessions.ts b/src/stores/sessions.ts index 186e39db..28f51797 100644 --- a/src/stores/sessions.ts +++ b/src/stores/sessions.ts @@ -1236,6 +1236,41 @@ function handleSessionUpdate(instanceId: string, event: any): void { } } +function resolvePastedPlaceholders(prompt: string, attachments: any[] = []): string { + if (!prompt || !prompt.includes("[pasted #")) { + return prompt + } + + if (!attachments || attachments.length === 0) { + return prompt + } + + const lookup = new Map() + + for (const attachment of attachments) { + const source = attachment?.source + if (!source || source.type !== "text") continue + const display: string | undefined = attachment?.display + const value: unknown = source.value + if (typeof display !== "string" || typeof value !== "string") continue + const match = display.match(/pasted #(\d+)/) + if (!match) continue + const placeholder = `[pasted #${match[1]}]` + if (!lookup.has(placeholder)) { + lookup.set(placeholder, value) + } + } + + if (lookup.size === 0) { + return prompt + } + + return prompt.replace(/\[pasted #(\d+)\]/g, (fullMatch) => { + const replacement = lookup.get(fullMatch) + return typeof replacement === "string" ? replacement : fullMatch + }) +} + async function sendMessage( instanceId: string, sessionId: string, @@ -1256,11 +1291,13 @@ async function sendMessage( const messageId = createId("msg") const textPartId = createId("part") + const resolvedPrompt = resolvePastedPlaceholders(prompt, attachments) + const optimisticParts: any[] = [ { id: textPartId, type: "text" as const, - text: prompt, + text: resolvedPrompt, synthetic: true, renderCache: undefined, }, @@ -1288,7 +1325,7 @@ async function sendMessage( { id: textPartId, type: "text" as const, - text: prompt, + text: resolvedPrompt, }, ] @@ -1313,16 +1350,24 @@ async function sendMessage( synthetic: true, }) } else if (source.type === "text") { + const display: string | undefined = att.display + const value: unknown = source.value + const isPastedPlaceholder = typeof display === "string" && /^pasted #\d+/.test(display) + + if (isPastedPlaceholder || typeof value !== "string") { + continue + } + const partId = createId("part") requestParts.push({ id: partId, type: "text" as const, - text: source.value, + text: value, }) optimisticParts.push({ id: partId, type: "text" as const, - text: source.value, + text: value, synthetic: true, renderCache: undefined, })