prompt.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 }): Prompt {
  53. const textPart = textPartValue(parts)
  54. const text = textPart?.text ?? ""
  55. const directory = opts?.directory
  56. const toRelative = (path: string) => {
  57. if (!directory) return path
  58. const prefix = directory.endsWith("/") ? directory : directory + "/"
  59. if (path.startsWith(prefix)) return path.slice(prefix.length)
  60. if (path.startsWith(directory)) {
  61. const next = path.slice(directory.length)
  62. if (next.startsWith("/")) return next.slice(1)
  63. return next
  64. }
  65. return path
  66. }
  67. const inline: Inline[] = []
  68. const images: ImageAttachmentPart[] = []
  69. for (const part of parts) {
  70. if (part.type === "file") {
  71. const filePart = part as FilePart
  72. const sourceText = filePart.source?.text
  73. if (sourceText) {
  74. const value = sourceText.value
  75. const start = sourceText.start
  76. const end = sourceText.end
  77. let path = value
  78. if (value.startsWith("@")) path = value.slice(1)
  79. if (!value.startsWith("@") && filePart.source && "path" in filePart.source) {
  80. path = filePart.source.path
  81. }
  82. inline.push({
  83. type: "file",
  84. start,
  85. end,
  86. value,
  87. path: toRelative(path),
  88. selection: selectionFromFileUrl(filePart.url),
  89. })
  90. continue
  91. }
  92. if (filePart.url.startsWith("data:")) {
  93. images.push({
  94. type: "image",
  95. id: filePart.id,
  96. filename: filePart.filename ?? "attachment",
  97. mime: filePart.mime,
  98. dataUrl: filePart.url,
  99. })
  100. }
  101. }
  102. if (part.type === "agent") {
  103. const agentPart = part as MessageAgentPart
  104. const source = agentPart.source
  105. if (!source) continue
  106. inline.push({
  107. type: "agent",
  108. start: source.start,
  109. end: source.end,
  110. value: source.value,
  111. name: agentPart.name,
  112. })
  113. }
  114. }
  115. inline.sort((a, b) => {
  116. if (a.start !== b.start) return a.start - b.start
  117. return a.end - b.end
  118. })
  119. const result: Prompt = []
  120. let position = 0
  121. let cursor = 0
  122. const pushText = (content: string) => {
  123. if (!content) return
  124. result.push({
  125. type: "text",
  126. content,
  127. start: position,
  128. end: position + content.length,
  129. })
  130. position += content.length
  131. }
  132. const pushFile = (item: Extract<Inline, { type: "file" }>) => {
  133. const content = item.value
  134. const attachment: FileAttachmentPart = {
  135. type: "file",
  136. path: item.path,
  137. content,
  138. start: position,
  139. end: position + content.length,
  140. selection: item.selection,
  141. }
  142. result.push(attachment)
  143. position += content.length
  144. }
  145. const pushAgent = (item: Extract<Inline, { type: "agent" }>) => {
  146. const content = item.value
  147. const mention: AgentPart = {
  148. type: "agent",
  149. name: item.name,
  150. content,
  151. start: position,
  152. end: position + content.length,
  153. }
  154. result.push(mention)
  155. position += content.length
  156. }
  157. for (const item of inline) {
  158. if (item.start < 0 || item.end < item.start) continue
  159. const expected = item.value
  160. if (!expected) continue
  161. const mismatch = item.end > text.length || item.start < cursor || text.slice(item.start, item.end) !== expected
  162. const start = mismatch ? text.indexOf(expected, cursor) : item.start
  163. if (start === -1) continue
  164. const end = mismatch ? start + expected.length : item.end
  165. pushText(text.slice(cursor, start))
  166. if (item.type === "file") pushFile(item)
  167. if (item.type === "agent") pushAgent(item)
  168. cursor = end
  169. }
  170. pushText(text.slice(cursor))
  171. if (result.length === 0) {
  172. result.push({ type: "text", content: "", start: 0, end: 0 })
  173. }
  174. if (images.length === 0) return result
  175. return [...result, ...images]
  176. }