Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136import type { FileSelection } from "@/workspaces/files/model"
export type PromptComment = { path: string selection?: FileSelection comment: string preview?: string origin?: "review" | "file"}
/** An attachment the model receives as a path on the server rather than inline bytes. */export type PromptAttachmentReference = { name: string mime: string path: string}
function selection(selection: unknown) { if (!selection || typeof selection !== "object") return undefined const startLine = Number((selection as FileSelection).startLine) const startChar = Number((selection as FileSelection).startChar) const endLine = Number((selection as FileSelection).endLine) const endChar = Number((selection as FileSelection).endChar) if (![startLine, startChar, endLine, endChar].every(Number.isFinite)) return undefined return { startLine, startChar, endLine, endChar, } satisfies FileSelection}
export function createCommentMetadata(input: PromptComment) { return { opencodeComment: { path: input.path, selection: input.selection, comment: input.comment, preview: input.preview, origin: input.origin, }, }}
export function readCommentMetadata(value: unknown) { if (!value || typeof value !== "object") return const meta = (value as { opencodeComment?: unknown }).opencodeComment if (!meta || typeof meta !== "object") return const path = (meta as { path?: unknown }).path const comment = (meta as { comment?: unknown }).comment if (typeof path !== "string" || typeof comment !== "string") return const preview = (meta as { preview?: unknown }).preview const origin = (meta as { origin?: unknown }).origin return { path, selection: selection((meta as { selection?: unknown }).selection), comment, preview: typeof preview === "string" ? preview : undefined, origin: origin === "review" || origin === "file" ? origin : undefined, } satisfies PromptComment}
export function readPromptPresentation(value: unknown) { if (!value || typeof value !== "object") return const displayText = (value as { displayText?: unknown }).displayText const comments = (value as { comments?: unknown }).comments if (typeof displayText !== "string" || !Array.isArray(comments)) return const attachments = (value as { attachments?: unknown }).attachments return { displayText, attachments: (Array.isArray(attachments) ? attachments : []).flatMap((item): PromptAttachmentReference[] => { if (!item || typeof item !== "object") return [] const name = (item as { name?: unknown }).name const mime = (item as { mime?: unknown }).mime const path = (item as { path?: unknown }).path if (typeof name !== "string" || typeof mime !== "string" || typeof path !== "string") return [] return [{ name, mime, path }] }), comments: comments.flatMap((item): PromptComment[] => { if (!item || typeof item !== "object") return [] const path = (item as { path?: unknown }).path const comment = (item as { comment?: unknown }).comment if (typeof path !== "string" || typeof comment !== "string") return [] const preview = (item as { preview?: unknown }).preview const origin = (item as { origin?: unknown }).origin return [ { path, comment, selection: selection((item as { selection?: unknown }).selection), preview: typeof preview === "string" ? preview : undefined, origin: origin === "review" || origin === "file" ? origin : undefined, }, ] }), }}
export function formatAttachmentReference(input: PromptAttachmentReference) { return `Attached file: \`${input.path}\``}
export function formatCommentNote(input: { path: string; selection?: FileSelection; comment: string }) { const start = input.selection ? Math.min(input.selection.startLine, input.selection.endLine) : undefined const end = input.selection ? Math.max(input.selection.startLine, input.selection.endLine) : undefined const range = start === undefined || end === undefined ? "this file" : start === end ? `line ${start}` : `lines ${start} through ${end}` return `The user made the following comment regarding ${range} of ${input.path}: ${input.comment}`}
export function parseCommentNote(text: string) { const match = text.match( /^The user made the following comment regarding (this file|line (\d+)|lines (\d+) through (\d+)) of (.+?): ([\s\S]+)$/, ) if (!match) return const start = match[2] ? Number(match[2]) : match[3] ? Number(match[3]) : undefined const end = match[2] ? Number(match[2]) : match[4] ? Number(match[4]) : undefined return { path: match[5], selection: start !== undefined && end !== undefined ? { startLine: start, startChar: 0, endLine: end, endChar: 0, } : undefined, comment: match[6], } satisfies PromptComment}