prompt.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import type { AgentPart as MessageAgentPart, FilePart, Part, TextPart } from "@opencode-ai/sdk/v2"
  2. import type { AgentPart, FileAttachmentPart, ImageAttachmentPart, Prompt } from "@/context/prompt"
  3. type Inline =
  4. | {
  5. type: "file"
  6. start: number
  7. end: number
  8. value: string
  9. path: string
  10. selection?: {
  11. startLine: number
  12. endLine: number
  13. startChar: number
  14. endChar: number
  15. }
  16. }
  17. | {
  18. type: "agent"
  19. start: number
  20. end: number
  21. value: string
  22. name: string
  23. }
  24. function selectionFromFileUrl(url: string): Extract<Inline, { type: "file" }>["selection"] {
  25. const queryIndex = url.indexOf("?")
  26. if (queryIndex === -1) return undefined
  27. const params = new URLSearchParams(url.slice(queryIndex + 1))
  28. const startLine = Number(params.get("start"))
  29. const endLine = Number(params.get("end"))
  30. if (!Number.isFinite(startLine) || !Number.isFinite(endLine)) return undefined
  31. return {
  32. startLine,
  33. endLine,
  34. startChar: 0,
  35. endChar: 0,
  36. }
  37. }
  38. function textPartValue(parts: Part[]) {
  39. const candidates = parts
  40. .filter((part): part is TextPart => part.type === "text")
  41. .filter((part) => !part.synthetic && !part.ignored)
  42. return candidates.reduce((best: TextPart | undefined, part) => {
  43. if (!best) return part
  44. if (part.text.length > best.text.length) return part
  45. return best
  46. }, undefined)
  47. }
  48. /**
  49. * Extract prompt content from message parts for restoring into the prompt input.
  50. * This is used by undo to restore the original user prompt.
  51. */
  52. export function extractPromptFromParts(parts: Part[], opts?: { directory?: string; attachmentName?: string }): Prompt {
  53. const textPart = textPartValue(parts)
  54. const text = textPart?.text ?? ""
  55. const directory = opts?.directory
  56. const attachmentName = opts?.attachmentName ?? "attachment"
  57. const toRelative = (path: string) => {
  58. if (!directory) return path
  59. const prefix = directory.endsWith("/") ? directory : directory + "/"
  60. if (path.startsWith(prefix)) return path.slice(prefix.length)
  61. if (path.startsWith(directory)) {
  62. const next = path.slice(directory.length)
  63. if (next.startsWith("/")) return next.slice(1)
  64. return next
  65. }
  66. return path
  67. }
  68. const inline: Inline[] = []
  69. const images: ImageAttachmentPart[] = []
  70. for (const part of parts) {
  71. if (part.type === "file") {
  72. const filePart = part as FilePart
  73. const sourceText = filePart.source?.text
  74. if (sourceText) {
  75. const value = sourceText.value
  76. const start = sourceText.start
  77. const end = sourceText.end
  78. let path = value
  79. if (value.startsWith("@")) path = value.slice(1)
  80. if (!value.startsWith("@") && filePart.source && "path" in filePart.source) {
  81. path = filePart.source.path
  82. }
  83. inline.push({
  84. type: "file",
  85. start,
  86. end,
  87. value,
  88. path: toRelative(path),
  89. selection: selectionFromFileUrl(filePart.url),
  90. })
  91. continue
  92. }
  93. if (filePart.url.startsWith("data:")) {
  94. images.push({
  95. type: "image",
  96. id: filePart.id,
  97. filename: filePart.filename ?? attachmentName,
  98. mime: filePart.mime,
  99. dataUrl: filePart.url,
  100. })
  101. }
  102. }
  103. if (part.type === "agent") {
  104. const agentPart = part as MessageAgentPart
  105. const source = agentPart.source
  106. if (!source) continue
  107. inline.push({
  108. type: "agent",
  109. start: source.start,
  110. end: source.end,
  111. value: source.value,
  112. name: agentPart.name,
  113. })
  114. }
  115. }
  116. inline.sort((a, b) => {
  117. if (a.start !== b.start) return a.start - b.start
  118. return a.end - b.end
  119. })
  120. const result: Prompt = []
  121. let position = 0
  122. let cursor = 0
  123. const pushText = (content: string) => {
  124. if (!content) return
  125. result.push({
  126. type: "text",
  127. content,
  128. start: position,
  129. end: position + content.length,
  130. })
  131. position += content.length
  132. }
  133. const pushFile = (item: Extract<Inline, { type: "file" }>) => {
  134. const content = item.value
  135. const attachment: FileAttachmentPart = {
  136. type: "file",
  137. path: item.path,
  138. content,
  139. start: position,
  140. end: position + content.length,
  141. selection: item.selection,
  142. }
  143. result.push(attachment)
  144. position += content.length
  145. }
  146. const pushAgent = (item: Extract<Inline, { type: "agent" }>) => {
  147. const content = item.value
  148. const mention: AgentPart = {
  149. type: "agent",
  150. name: item.name,
  151. content,
  152. start: position,
  153. end: position + content.length,
  154. }
  155. result.push(mention)
  156. position += content.length
  157. }
  158. for (const item of inline) {
  159. if (item.start < 0 || item.end < item.start) continue
  160. const expected = item.value
  161. if (!expected) continue
  162. const mismatch = item.end > text.length || item.start < cursor || text.slice(item.start, item.end) !== expected
  163. const start = mismatch ? text.indexOf(expected, cursor) : item.start
  164. if (start === -1) continue
  165. const end = mismatch ? start + expected.length : item.end
  166. pushText(text.slice(cursor, start))
  167. if (item.type === "file") pushFile(item)
  168. if (item.type === "agent") pushAgent(item)
  169. cursor = end
  170. }
  171. pushText(text.slice(cursor))
  172. if (result.length === 0) {
  173. result.push({ type: "text", content: "", start: 0, end: 0 })
  174. }
  175. if (images.length === 0) return result
  176. return [...result, ...images]
  177. }