Resolve pasted placeholders before sending prompts
This commit is contained in:
@@ -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<string, string>()
|
||||||
|
|
||||||
|
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(
|
async function sendMessage(
|
||||||
instanceId: string,
|
instanceId: string,
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
@@ -1256,11 +1291,13 @@ async function sendMessage(
|
|||||||
const messageId = createId("msg")
|
const messageId = createId("msg")
|
||||||
const textPartId = createId("part")
|
const textPartId = createId("part")
|
||||||
|
|
||||||
|
const resolvedPrompt = resolvePastedPlaceholders(prompt, attachments)
|
||||||
|
|
||||||
const optimisticParts: any[] = [
|
const optimisticParts: any[] = [
|
||||||
{
|
{
|
||||||
id: textPartId,
|
id: textPartId,
|
||||||
type: "text" as const,
|
type: "text" as const,
|
||||||
text: prompt,
|
text: resolvedPrompt,
|
||||||
synthetic: true,
|
synthetic: true,
|
||||||
renderCache: undefined,
|
renderCache: undefined,
|
||||||
},
|
},
|
||||||
@@ -1288,7 +1325,7 @@ async function sendMessage(
|
|||||||
{
|
{
|
||||||
id: textPartId,
|
id: textPartId,
|
||||||
type: "text" as const,
|
type: "text" as const,
|
||||||
text: prompt,
|
text: resolvedPrompt,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1313,16 +1350,24 @@ async function sendMessage(
|
|||||||
synthetic: true,
|
synthetic: true,
|
||||||
})
|
})
|
||||||
} else if (source.type === "text") {
|
} 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")
|
const partId = createId("part")
|
||||||
requestParts.push({
|
requestParts.push({
|
||||||
id: partId,
|
id: partId,
|
||||||
type: "text" as const,
|
type: "text" as const,
|
||||||
text: source.value,
|
text: value,
|
||||||
})
|
})
|
||||||
optimisticParts.push({
|
optimisticParts.push({
|
||||||
id: partId,
|
id: partId,
|
||||||
type: "text" as const,
|
type: "text" as const,
|
||||||
text: source.value,
|
text: value,
|
||||||
synthetic: true,
|
synthetic: true,
|
||||||
renderCache: undefined,
|
renderCache: undefined,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user