29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
import { displaySlice } from "./display"
|
|
|
|
export function stripPromptPartIDs<Part extends { id: string; messageID: string; sessionID: string }>(part: Part) {
|
|
const { id: _id, messageID: _messageID, sessionID: _sessionID, ...rest } = part
|
|
return rest
|
|
}
|
|
|
|
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)
|
|
}, text)
|
|
}
|
|
|
|
function isPastedTextPart(part: unknown): part is { type: "text"; text: string; source: { text: { value: string } } } {
|
|
if (!part || typeof part !== "object" || !("type" in part) || part.type !== "text") return false
|
|
if (!("text" in part) || typeof part.text !== "string" || !("source" in part)) return false
|
|
const source = part.source
|
|
if (!source || typeof source !== "object" || !("text" in source)) return false
|
|
const text = source.text
|
|
return Boolean(text && typeof text === "object" && "value" in text && typeof text.value === "string")
|
|
}
|
|
|
|
export function expandTrackedPastedText(text: string, ranges: { start: number; end: number; text: string }[]) {
|
|
return ranges
|
|
.slice()
|
|
.sort((a, b) => b.start - a.start)
|
|
.reduce((result, part) => displaySlice(result, 0, part.start) + part.text + displaySlice(result, part.end), text)
|
|
}
|