import { apiFetch } from "@/core/api"; import { OptType, writeYLog } from "@/core/log/y-log"; import { urlOfArtifact } from "./utils"; async function errorMessageFromResponse(res: Response): Promise { try { const data: unknown = await res.json(); if (data && typeof data === "object" && "detail" in data) { const detail = (data as { detail: unknown }).detail; if (typeof detail === "string") return detail; } } catch { // ignore non-json errors } return res.statusText || "Request failed"; } export async function updateArtifactContent({ filepath, threadId, content, isMock, }: { filepath: string; threadId: string; content: string; isMock?: boolean; }) { const url = urlOfArtifact({ filepath, threadId, isMock }); const res = await apiFetch(url, { method: "PATCH", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ content }), }); if (!res.ok) { throw new Error(await errorMessageFromResponse(res)); } writeYLog({ optType: OptType.edit, optDetail: "CM助手-全量-产物-编辑产物内容", url, reqParam: { filepath, threadId }, }); return res.text(); }