Skip to content

Spot it. Save it. Free, local-first Chrome extension for images, videos and prompts. Explore 2,000+ curated cases and connect your library to AI Agents via MCP. 提示词导演

18.7 KB
Raw Blame
import { defaultSkillExtractionInstruction } from './skill-extraction-instruction.js';
export { defaultSkillExtractionInstruction } from './skill-extraction-instruction.js';
import {
  createComposerSession,
  normalizeComposerSettings,
  referenceSourcePartsForAsset
} from "./composer.js";
import { executeComposerTurnWithService } from "./composer-service.js";
import { entryMediaAssets } from "./media.js";

export function defaultSkillVisualInstruction(localeValue = "zh-CN") {
  return localeValue === "en"
    ? "Describe reusable visual choices that serve the goal. Distinguish transferable patterns from source-specific details, do not judge completeness, and ignore contact-sheet borders and labels."
    : "描述服务于目标的可复用视觉选择,区分可迁移规律与案例专属内容,不评价完整性;忽略联系表边框和编号本身。";
}

export function buildSkillExtractionRequest(input = {}) {
  const locale = input.locale === "en" ? "en" : "zh-CN";
  const goal = clean(input.goal);
  if (!goal) throw new Error(locale === "en" ? "Describe what you want to extract" : "请先说明希望提炼什么");
  const sources = (Array.isArray(input.sources) ? input.sources : []).flatMap((source, index) => {
    const prompt = multiline(source?.prompt);
    const analysis = multiline(source?.analysis);
    const parts = normalizeAnonymousSourceParts(source?.parts);
    return prompt || analysis || parts.length ? [{ number: index + 1, prompt, analysis, parts }] : [];
  });
  if (!sources.length) throw new Error(locale === "en" ? "The selected cases have no usable text" : "所选案例没有可用于提炼的文字资料");
  const vision = (Array.isArray(input.visualAnalyses) ? input.visualAnalyses : []).map(multiline).filter(Boolean);
  const sourceText = sources.map((source) => [
    locale === "en" ? `Source ${source.number}` : `匿名来源 ${source.number}`,
    ...(source.parts.length
      ? exactAnonymousSourceGroups(source.parts).map((group) => `[${group.labels.map((kind) => skillSourceLabel(kind, locale)).join(" + ")}]\n${group.text}`)
      : [
          source.prompt ? `${locale === "en" ? "Original prompt" : "原提示词"}:\n${source.prompt}` : "",
          source.analysis ? `${locale === "en" ? "Existing visual analysis" : "已有画面分析"}:\n${source.analysis}` : ""
        ])
  ].filter(Boolean).join("\n")).join("\n\n");
  const visualText = vision.length ? `\n\n${locale === "en" ? "Confirmed visual contact-sheet analyses" : "已确认的视觉联系表分析"}:\n${vision.map((item, index) => `${index + 1}. ${item}`).join("\n")}` : "";
  const extractionInstruction = multiline(input.instructionOverride) || defaultSkillExtractionInstruction(locale);
  if (locale === "en") return [
    "Create the executable Markdown body of a portable creative Skill.",
    `Extraction goal: ${goal}`,
    extractionInstruction,
    "Do not include YAML frontmatter, provenance, local identifiers, project names, URLs, or claims about unseen images.",
    sourceText + visualText
  ].join("\n\n");
  return [
    "请生成一份可移植创作 Skill 的可执行 Markdown 正文。",
    `本次提炼目标:${goal}`,
    extractionInstruction,
    "不要输出 YAML frontmatter、来源证据、本地编号、项目名、网址,也不要声称看过未提供的图片。",
    sourceText + visualText
  ].join("\n\n");
}

// Existing extraction batch target only: every fragment is processed and then
// synthesized. This is never an input quota or a reason to discard source text.
export const SKILL_EXTRACTION_BATCH_TARGET_CHARACTERS = 337_500;

export function skillExtractionWorkload(input = {}) {
  const sources = normalizeSkillSources(input.sources);
  const batches = partitionSkillSources(sources, input.maxBatchCharacters);
  const textCharacters = sources.reduce((sum, source) => sum + skillSourceCharacters(source), 0);
  return {
    sourceCount: sources.length,
    textCharacters,
    batches,
    textBatchCount: batches.length,
    synthesisRequestCount: batches.length > 1 ? 1 : 0,
    requestCount: batches.length + (batches.length > 1 ? 1 : 0),
    overSingleRequest: batches.length > 1,
    tokenEstimate: estimateSkillTokens([
      clean(input.goal),
      multiline(input.instructionOverride) || defaultSkillExtractionInstruction(input.locale),
      ...sources.flatMap((source) => source.parts?.length ? source.parts.map((part) => part.text) : [source.prompt, source.analysis])
    ].join("\n"))
  };
}

export function estimateSkillTokens(value = "") {
  const text = String(value ?? "");
  const cjk = (text.match(/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/gu) ?? []).length;
  const other = text.replace(/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\s]/gu, "").length;
  return {
    min: cjk + Math.ceil(other / 4),
    max: cjk * 2 + Math.ceil(other / 2),
    kind: "local-range"
  };
}

export async function extractCreativeSkillDraftBatched(input = {}, settingsValue = {}, options = {}) {
  const workload = skillExtractionWorkload(input);
  if (!workload.batches.length) return extractCreativeSkillDraft(input, settingsValue, options);
  const partials = [];
  let usage = {};
  let lastModel = "";
  for (const [index, sources] of workload.batches.entries()) {
    options.onProgress?.({ phase: "text-batch", current: index + 1, total: workload.batches.length });
    const result = await extractCreativeSkillDraft({ ...input, sources }, settingsValue, options);
    partials.push(result.markdown);
    lastModel = result.model;
    usage = addUsage(usage, result.usage);
  }
  if (partials.length === 1) return { ...skillDraftMetadata(partials[0], input.goal), markdown: partials[0], usage, model: lastModel };
  options.onProgress?.({ phase: "synthesis", current: 1, total: 1 });
  const synthesized = await extractCreativeSkillDraft({
    ...input,
    sources: partials.map((markdown) => ({ prompt: markdown, analysis: "" })),
    visualAnalyses: [],
    goal: input.locale === "en"
      ? `${clean(input.goal)}. The following intermediate drafts cover the complete source set. Merge repeated rules, retain useful differences and boundaries, and produce one executable method.`
      : `${clean(input.goal)}。以下内容是完整来源经分批提炼得到的中间草稿;合并重复规则、保留有用差异与边界,形成一份统一可执行方法。`
  }, settingsValue, options);
  return { ...synthesized, usage: addUsage(usage, synthesized.usage) };
}

export async function extractCreativeSkillDraft(input = {}, settingsValue = {}, options = {}) {
  const request = buildSkillExtractionRequest(input);
  const composerSettings = normalizeComposerSettings(settingsValue.composer);
  const session = createComposerSession({
    targetType: input.targetType === "video" ? "video" : "image",
    outputLanguage: input.locale === "en" ? "en" : "zh-CN",
    routeMode: "chat",
    aiProfile: input.aiProfile ?? composerSettings.lastAiProfile,
    productionReviewEnabled: false
  });
  const result = await executeComposerTurnWithService({
    session,
    userMessage: request,
    route: "chat",
    instruction: request,
    composerSettings
  }, { ai: settingsValue.ai, vision: settingsValue.vision }, [], options);
  const markdown = stripFence(result.text);
  if (!markdown) throw new Error("模型没有返回可编辑的 Skill 草稿");
  return { ...skillDraftMetadata(markdown, input.goal), markdown, usage: result.usage, model: result.model };
}

export async function analyzeCreativeSkillVisualBatch(input = {}, settingsValue = {}, options = {}) {
  const locale = input.locale === "en" ? "en" : "zh-CN";
  const goal = clean(input.goal);
  const dataUrl = String(input.dataUrl ?? "").trim();
  if (!goal || !dataUrl.startsWith("data:image/")) throw new Error(locale === "en" ? "The visual batch is incomplete" : "视觉批次数据不完整");
  const labels = (Array.isArray(input.items) ? input.items : []).map((item) =>
    `C${Math.max(1, Number(item.caseNumber) || 1)}.${Math.max(1, Number(item.imageNumber) || 1)}`
  ).join(", ");
  const instruction = multiline(input.instructionOverride) || defaultSkillVisualInstruction(locale);
  const request = locale === "en"
    ? `Analyze only the visible creative content for this Skill extraction goal: ${goal}. Labels ${labels || "C1.1"} preserve case and image order. ${instruction}`
    : `只围绕这个 Skill 提炼目标分析可见创作内容:${goal}。标记 ${labels || "C1.1"} 用于保留案例归属和图片顺序。${instruction}`;
  const composerSettings = normalizeComposerSettings(settingsValue.composer);
  const profile = input.aiProfile;
  if (!profile || profile.serviceId === "deepseek") throw new Error(locale === "en" ? "Select a configured vision model" : "请先配置并选择视觉能力模型");
  const visualId = `skill-contact-sheet:${crypto.randomUUID()}`;
  const session = createComposerSession({
    targetType: "image",
    outputLanguage: locale,
    routeMode: "chat",
    aiProfile: profile,
    productionReviewEnabled: false,
    referenceSnapshots: [{
      entryId: visualId,
      alias: "@VISUAL",
      referenceKind: "vision",
      referenceText: request,
      imageRefs: [{ visualId, mimeType: String(input.mimeType ?? "image/jpeg") }]
    }]
  });
  const result = await executeComposerTurnWithService({
    session,
    userMessage: request,
    route: "chat",
    instruction: request,
    composerSettings
  }, { ai: settingsValue.ai, vision: settingsValue.vision }, [{ visualId, dataUrl }], options);
  const description = multiline(result.text);
  if (!description) throw new Error(locale === "en" ? "The vision model returned no usable analysis" : "视觉模型没有返回可用分析");
  return { description, usage: result.usage, model: result.model };
}

export function anonymousSkillSources(entriesValue = [], selectionsValue = [], options = {}) {
  const selections = normalizeSourceSelections(selectionsValue);
  const documents = options.documentTextByAsset instanceof Map ? options.documentTextByAsset : new Map();
  return (Array.isArray(entriesValue) ? entriesValue : []).flatMap((entry) => {
    const selection = selections.get(String(entry?.id ?? ""));
    if (!selection) return [];
    const selectedAssets = entryMediaAssets(entry).filter((asset) => asset.usage !== "poster" && (selection.assetIds === null || selection.assetIds.has(String(asset.id))));
    const mediaPrompts = new Map((Array.isArray(entry?.mediaPrompts) ? entry.mediaPrompts : [])
      .map((item) => [String(item?.assetId ?? ""), multiline(item?.text)]));
    const hasSelectedVideo = selectedAssets.some((asset) => asset.kind === "video");
    const parts = [
      selection.includeEntryText && multiline(entry?.text)
        ? { kind: "original_prompt", text: multiline(entry.text) } : null,
      ...selectedAssets.flatMap((asset) => {
        if (asset.kind === "video") {
          return referenceSourcePartsForAsset(entry, asset.id, {
            sourceIds: selection.sourceIds,
            analysisIds: selection.analysisIds
          }).map(({ kind, text }) => ({ kind, text }));
        }
        const promptText = mediaPrompts.get(String(asset.id));
        const documentText = asset.kind === "document" ? multiline(documents.get(String(asset.id))) : "";
        const visualText = !asset?.visionAnalysis?.invalidated
          ? multiline(asset?.visionAnalysis?.reconstructionPrompt ?? asset?.visionAnalysis?.description) : "";
        return [
          promptText ? { kind: "original_prompt", text: promptText } : null,
          documentText ? { kind: "document_text", text: documentText } : null,
          visualText ? { kind: "image_analysis", text: visualText } : null
        ].filter(Boolean);
      })
    ].filter(Boolean);
    const normalizedParts = normalizeAnonymousSourceParts(parts);
    const prompt = normalizedParts.filter((part) => ["original_prompt", "document_text"].includes(part.kind)).map((part) => part.text).join("\n\n");
    const analysis = normalizedParts.filter((part) => !["original_prompt", "document_text"].includes(part.kind)).map((part) => part.text).join("\n");
    return normalizedParts.length ? [{
      prompt,
      analysis,
      ...(hasSelectedVideo ? { parts: normalizedParts } : {})
    }] : [];
  });
}

function normalizeSourceSelections(values) {
  const source = Array.isArray(values) ? values : [];
  if (source.every((item) => typeof item === "string" || typeof item === "number")) {
    return new Map(source.map((entryId) => [String(entryId), { includeEntryText: true, assetIds: null }]));
  }
  return new Map(source.flatMap((selection) => {
    const entryId = clean(selection?.entryId);
    if (!entryId) return [];
    return [[entryId, {
      includeEntryText: selection?.includeEntryText !== false,
      assetIds: new Set((Array.isArray(selection?.assetIds) ? selection.assetIds : []).map(String)),
      sourceIds: Array.isArray(selection?.sourceIds) ? selection.sourceIds.map(String) : null,
      analysisIds: (Array.isArray(selection?.analysisIds) ? selection.analysisIds : []).map(String)
    }]];
  }));
}

function skillDraftMetadata(markdown, goalValue) {
  const heading = String(markdown ?? "").match(/^#\s+(.+)$/mu)?.[1]?.trim() || "";
  return {
    callName: heading,
    description: clean(goalValue)
  };
}

export function creativeRunEvidenceCandidates(runsValue = [], skillIdValue = "") {
  const skillId = clean(skillIdValue);
  return (Array.isArray(runsValue) ? runsValue : []).flatMap((run) => {
    const usedSkill = (Array.isArray(run?.appliedSkills) ? run.appliedSkills : [])
      .some((skill) => clean(skill?.skillId) === skillId);
    if (!skillId || !usedSkill) return [];
    return (Array.isArray(run?.outputs) ? run.outputs : []).flatMap((output) => {
      const keep = multiline(output?.judgment?.keep);
      const improve = multiline(output?.judgment?.improve);
      const visualId = clean(output?.visual?.id);
      if (!visualId || (!keep && !improve)) return [];
      return [{
        id: `${clean(run.id)}:${visualId}`,
        runId: clean(run.id),
        visualId,
        title: clean(run.title) || clean(run.promptText).slice(0, 80),
        prompt: multiline(run.promptText),
        keep,
        improve,
        capturedAt: clean(output.capturedAt || run.createdAt)
      }];
    });
  });
}

export function selectedCreativeRunEvidenceSources(runsValue = [], evidenceIdsValue = [], skillIdValue = "") {
  const selected = new Set((Array.isArray(evidenceIdsValue) ? evidenceIdsValue : []).map(clean));
  return creativeRunEvidenceCandidates(runsValue, skillIdValue)
    .filter((item) => selected.has(item.id))
    .map((item) => ({
      prompt: item.prompt,
      analysis: [
        item.keep ? `值得保留:${item.keep}` : "",
        item.improve ? `需要改进:${item.improve}` : ""
      ].filter(Boolean).join("\n")
    }));
}

function stripFence(value) {
  return multiline(value).replace(/^```(?:markdown|md)?\s*/i, "").replace(/\s*```$/, "").trim();
}

function normalizeSkillSources(values) {
  return (Array.isArray(values) ? values : []).flatMap((source) => {
    const prompt = multiline(source?.prompt);
    const analysis = multiline(source?.analysis);
    const parts = normalizeAnonymousSourceParts(source?.parts);
    return prompt || analysis || parts.length ? [{ prompt, analysis, parts }] : [];
  });
}

function partitionSkillSources(values, maxCharactersValue) {
  const requested = Number(maxCharactersValue);
  const maxCharacters = Number.isSafeInteger(requested) && requested > 0 ? requested : SKILL_EXTRACTION_BATCH_TARGET_CHARACTERS;
  const fragments = values.flatMap((source) => splitSkillSource(source, maxCharacters));
  const batches = [];
  let current = [];
  let characters = 0;
  for (const source of fragments) {
    const size = skillSourceCharacters(source);
    if (current.length && characters + size > maxCharacters) {
      batches.push(current);
      current = [];
      characters = 0;
    }
    current.push(source);
    characters += size;
  }
  if (current.length) batches.push(current);
  return batches;
}

function splitSkillSource(source, maxCharacters) {
  if (skillSourceCharacters(source) <= maxCharacters) return [source];
  const chunks = [];
  for (const part of source.parts ?? []) {
    for (let offset = 0; offset < part.text.length; offset += maxCharacters) {
      chunks.push({ prompt: "", analysis: "", parts: [{ ...part, text: part.text.slice(offset, offset + maxCharacters) }] });
    }
  }
  if (source.parts?.length) return chunks;
  for (let offset = 0; offset < source.prompt.length; offset += maxCharacters) {
    chunks.push({ prompt: source.prompt.slice(offset, offset + maxCharacters), analysis: "", parts: [] });
  }
  for (let offset = 0; offset < source.analysis.length; offset += maxCharacters) {
    chunks.push({ prompt: "", analysis: source.analysis.slice(offset, offset + maxCharacters), parts: [] });
  }
  return chunks;
}

function skillSourceCharacters(source) {
  return (source.parts ?? []).reduce((sum, part) => sum + part.text.length, 0)
    || source.prompt.length + source.analysis.length;
}

function normalizeAnonymousSourceParts(values) {
  const allowed = new Set(["original_prompt", "video_reconstruction", "time_notes", "other_analysis", "image_analysis", "document_text"]);
  return (Array.isArray(values) ? values : []).flatMap((part) => {
    const kind = String(part?.kind ?? "").trim();
    const text = multiline(part?.text);
    return allowed.has(kind) && text ? [{ kind, text }] : [];
  });
}

function exactAnonymousSourceGroups(parts) {
  const groups = [];
  const byText = new Map();
  for (const part of parts) {
    const key = multiline(part.text);
    let group = byText.get(key);
    if (!group) {
      group = { text: part.text, labels: [] };
      byText.set(key, group);
      groups.push(group);
    }
    if (!group.labels.includes(part.kind)) group.labels.push(part.kind);
  }
  return groups;
}

function skillSourceLabel(kind, locale) {
  const labels = locale === "en" ? {
    original_prompt: "Original prompt",
    video_reconstruction: "AI visual reconstruction",
    time_notes: "Human time notes",
    other_analysis: "Other analysis",
    image_analysis: "Image analysis",
    document_text: "Selected document"
  } : {
    original_prompt: "原始提示词",
    video_reconstruction: "AI 视觉逆推",
    time_notes: "人工时间点笔记",
    other_analysis: "其他分析",
    image_analysis: "图片分析",
    document_text: "所选文档"
  };
  return labels[kind] || (locale === "en" ? "Source" : "来源");
}

function addUsage(left = {}, right = {}) {
  const keys = new Set([...Object.keys(left || {}), ...Object.keys(right || {})]);
  return Object.fromEntries([...keys].map((key) => [key, (Number(left?.[key]) || 0) + (Number(right?.[key]) || 0)]));
}

function multiline(value) {
  return String(value ?? "").replace(/\r\n?/g, "\n").trim();
}

function clean(value) {
  return multiline(value).replace(/\s+/g, " ");
}