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. 提示词导演

6.1 KB
Raw Blame
import { WORKSPACE_OPERATION_SPECS } from './workspace-operation-specs.js';
import { validate } from './case-operation-specs.js';
import { agentError, requireInteger } from "./agent-protocol.js";

export function createAgentWorkspace({ chromeApi, readCase, readSelection, readTemporary }) {
  const libraryUrl = chromeApi.runtime.getURL("library.html");
  const composerUrl = chromeApi.runtime.getURL("composer.html");
  async function contexts() {
    const values = await chromeApi.runtime.getContexts({ contextTypes: ["TAB"] });
    return values.filter(item => [libraryUrl, composerUrl].includes(item.documentUrl?.split(/[?#]/u)[0]))
      .map(item => ({ tabId: item.tabId, surface: item.documentUrl.split(/[?#]/u)[0] === libraryUrl ? "library" : "composer" }));
  }
  const api = {
    async open(input) {
      validate(WORKSPACE_OPERATION_SPECS.find(spec => spec.name === 'open_workspace').parameters, input, 'open_workspace');
      const available = (await contexts()).filter(item => item.surface === input.surface);
      if (input.tabId === undefined && available.length > 1) return { state: 'choose_workspace', contexts: available };
      const existing = input.tabId === undefined ? available[0] : available.find(item => item.tabId === input.tabId);
      if (input.tabId !== undefined && !existing) throw agentError('workspace_closed', '该工作页面已关闭,请重新发现现场。');
      const tab = existing ? await chromeApi.tabs.update(existing.tabId, { active: true })
        : await chromeApi.tabs.create({ url: input.surface === 'library' ? libraryUrl : composerUrl, active: true });
      await chromeApi.windows.update(tab.windowId, { focused: true });
      return waitForReady(tab.id, { surface: input.surface });
    },
    async live(operation, input = {}) {
      validate(WORKSPACE_OPERATION_SPECS.find(spec => spec.name === operation).parameters, input, operation);
      if (input.tabId !== undefined) requireInteger(input.tabId);
      const available = (await contexts()).filter(item => item.surface === 'library');
      if (!available.length) return { state: 'no_workspace', contexts: [] };
      if (input.tabId === undefined && available.length > 1) return { state: 'choose_workspace', contexts: available };
      const context = input.tabId === undefined ? available[0] : available.find(item => item.tabId === input.tabId);
      if (!context) throw agentError('workspace_closed', '页面已关闭,请重新发现现场');
      const temporary = operation === 'control_workspace' && input.action === 'open_temporary'
        ? await readTemporary(input.transferId) : undefined;
      let response;
      try { response = await chromeApi.runtime.sendMessage({ type: 'AGENT_LIVE_WORKSPACE', operation, tabId: context.tabId, input, temporary }); }
      catch { throw agentError('workspace_unavailable', '页面尚未加载协作功能,请保存编辑后刷新案例库页面'); }
      if (!response?.ok) throw agentError(response?.code || 'workspace_unavailable', response?.message || '页面尚未就绪');
      return { state: 'ready', tabId: context.tabId, ...response.result };
    },
    async read(input = {}) {
      if (input.tabId === undefined && input.source !== "page") return readSelection(input);
      if (input.tabId !== undefined) requireInteger(input.tabId);
      const available = await contexts();
      if (!available.length) return { state: "no_workspace", contexts: [], message: "请打开案例库或创作台,再读取当前选择。" };
      if (input.tabId === undefined && available.length > 1) {
        return { state: "choose_workspace", contexts: available, message: "多个工作页面已打开,请明确要接续的页面;不要合并各页选择。" };
      }
      const context = input.tabId === undefined ? available[0] : available.find(item => item.tabId === input.tabId);
      if (!context) throw agentError("workspace_closed", "该工作页面已关闭,请重新发现工作现场。");
      let response;
      try { response = await chromeApi.runtime.sendMessage({ type: "AGENT_READ_WORKSPACE", tabId: context.tabId, input }); }
      catch { throw agentError("workspace_unavailable", "工作页面尚未就绪,请稍后重读;旧页面需刷新后接续。"); }
      if (!response) throw agentError("workspace_unavailable", "工作页面尚未就绪,请刷新该页面后重读。");
      if (!response.ok) throw agentError(response.code, response.message);
      return { state: "ready", tabId: context.tabId, ...response.result };
    },
    async show({ caseId }) {
      // Check existence through the same case service used by the MCP reader.
      await readCase({ caseId });
      const openUrl = `${libraryUrl}?case=${encodeURIComponent(caseId)}`;
      const tab = await chromeApi.tabs.create({ url: openUrl, active: true });
      await chromeApi.windows.update(tab.windowId, { focused: true });
      return waitForReady(tab.id, { caseId, openUrl });
    }
  };
  async function waitForReady(tabId, details) {
    // Match the existing 15s foreground task wait budget; loading can continue
    // after timeout without pretending that an opening page is usable.
    const deadline = Date.now() + 15000;
    do {
      let snapshot;
      try {
        snapshot = await api.read({ tabId, source: 'page' });
      } catch (error) {
        if (!['workspace_unavailable', 'workspace_closed'].includes(error.code)) throw error;
        if (error.code === 'workspace_closed') await assertTabOpen();
      }
      if (snapshot?.state === 'ready' && (!details.caseId || snapshot.viewedCaseId === details.caseId)) return { ...details, ...snapshot };
      if (snapshot?.state === 'no_workspace') await assertTabOpen();
      await new Promise(resolve => setTimeout(resolve, 100));
    } while (Date.now() < deadline);
    return { state: 'opening', tabId, ...details, next: '用 read_workspace_context(tabId) 继续读取;ready 才表示现场就绪。' };
    async function assertTabOpen() {
      try { if (await chromeApi.tabs.get(tabId)) return; } catch { /* A closed tab cannot finish loading. */ }
      throw agentError('workspace_closed', '该工作页面已关闭,请重新发现现场。');
    }
  }
  return api;
}