| 940 | ) |
| 941 | |
| 942 | const parseFromDOM = (): Prompt => { |
| 943 | const parts: Prompt = [] |
| 944 | let position = 0 |
| 945 | let buffer = "" |
| 946 | |
| 947 | const flushText = () => { |
| 948 | let content = buffer |
| 949 | if (content.includes("\r")) content = content.replace(/\r\n?/g, "\n") |
| 950 | if (content.includes("\u200B")) content = content.replace(/\u200B/g, "") |
| 951 | buffer = "" |
| 952 | if (!content) return |
| 953 | parts.push({ type: "text", content, start: position, end: position + content.length }) |
| 954 | position += content.length |
| 955 | } |
| 956 | |
| 957 | const pushFile = (file: HTMLElement) => { |
| 958 | const content = file.textContent ?? "" |
| 959 | const source = |
| 960 | file.dataset.sourceType === "resource" && file.dataset.sourceClientName && file.dataset.sourceUri |
| 961 | ? { |
| 962 | type: "resource" as const, |
| 963 | text: { |
| 964 | value: content, |
| 965 | start: position, |
| 966 | end: position + content.length, |
| 967 | }, |
| 968 | clientName: file.dataset.sourceClientName, |
| 969 | uri: file.dataset.sourceUri, |
| 970 | } |
| 971 | : undefined |
| 972 | parts.push({ |
| 973 | type: "file", |
| 974 | path: file.dataset.path!, |
| 975 | content, |
| 976 | start: position, |
| 977 | end: position + content.length, |
| 978 | ...(file.dataset.mime ? { mime: file.dataset.mime } : {}), |
| 979 | ...(file.dataset.filename ? { filename: file.dataset.filename } : {}), |
| 980 | ...(file.dataset.url ? { url: file.dataset.url } : {}), |
| 981 | ...(source ? { source } : {}), |
| 982 | }) |
| 983 | position += content.length |
| 984 | } |
| 985 | |
| 986 | const pushAgent = (agent: HTMLElement) => { |
| 987 | const content = agent.textContent ?? "" |
| 988 | parts.push({ |
| 989 | type: "agent", |
| 990 | name: agent.dataset.name!, |
| 991 | content, |
| 992 | start: position, |
| 993 | end: position + content.length, |
| 994 | }) |
| 995 | position += content.length |
| 996 | } |
| 997 | |
| 998 | const visit = (node: Node) => { |
| 999 | if (node.nodeType === Node.TEXT_NODE) { |