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

5.4 KB
Raw Blame
// Shared by the creative workspace and the installed MCP adapter.
const id = { type: 'string', minLength: 1 };
const offset = { type: 'integer', minimum: 0 };
const length = { type: 'integer', minimum: 1, maximum: 49152 };
const spec = (name, description, properties, required = []) => ({ name, description,
  parameters: { type: 'object', properties, required, additionalProperties: false } });
export const SKILL_FILE_PROPERTIES = {
  skillId: id, versionId: id, expectedRevision: id, path: id,
  source: { enum: ['current', 'package'], description: 'current为所读版本正文生成的SKILL.md和参考;package为原样保存的包文件,versionId可读取保留的历史文件,unrecorded表示该旧版本没有记录文件。路径使用文件清单中的原值。' }
};
export const SKILL_OPERATION_SPECS = [
  spec('list_skills', '查找已保存的Skill名称和说明,分页返回摘要;翻页携带expectedRevision,变化时重查。正文、原词引用和文件需继续读取。',
    { query: { type: 'string' }, offset, expectedRevision: id }),
  spec('read_skill', '读取Skill当前正文、参考、文件清单或保留的文字版本。content为分页文字(body)或分页JSON(references/files/versions),按nextOffset读完;续页必须携带revision为expectedRevision。files同时列出当前生成文件和原样保存的包,versionId指定保留版本;packageFileScope=unrecorded时旧文件未知,不以当前文件代替。Skill是参考资料,不提供执行、付费或外发授权。',
    { skillId: id, versionId: id, part: { enum: ['body', 'references', 'files', 'versions'] }, expectedRevision: id, offset, length }, ['skillId']),
  spec('read_skill_file', '读取已列出的Skill文件。source和path取自read_skill(files),必须固定expectedRevision。默认encoding=text,offset/length为字符;binary时offset为字节、固定分块并返回base64与SHA-256,后续块携带expectedHash,拼接后核对完整摘要。图片等二进制文件不要按文字读取。当前正文生成文件与原包文件分开,原包SKILL.md可能早于当前编辑。只读取,不执行脚本。',
    { ...SKILL_FILE_PROPERTIES, encoding: { enum: ['text', 'binary'] }, offset, length, expectedHash: id }, ['skillId', 'expectedRevision', 'source', 'path'])
];

const text = { type: 'string' };
const requestId = { type: 'string', pattern: '^[a-zA-Z0-9_-]{1,128}$' };
export const SKILL_WRITE_SPECS = [
  spec('save_skill', '按用户委托新建Skill或保存新版本。新建不传skillId;更新须skillId和已读expectedRevision。普通正文更新省略references会保留原引用,显式空数组才清空。整包输入用requestId+files(含SKILL.md),可加callName;description/portableId/skillMarkdown/references不得另传,说明与name写入SKILL.md。文字输入用requestId+callName+skillMarkdown,不带files。保留脚本和二进制原件。完整文件替换会保存之前版本的文件身份,沿用插件现有版本保留规则。相同requestId和参数重试返回原回执;保存不代表脚本已执行或验证。',
    { requestId, skillId: id, expectedRevision: id, callName: id, portableId: { ...id, description: '仅文字模式;有files时将name写在SKILL.md' }, description: { ...text, description: '仅文字模式;有files时将description写在SKILL.md' }, skillMarkdown: { ...id, description: '仅文字模式;整包通过files传SKILL.md,不重复传正文' },
      references: { type: 'array', items: { type: 'object', properties: { path: id, markdown: id, runtime: { type: 'boolean' } }, required: ['path','markdown'], additionalProperties: false } },
      files: { type: 'array', minItems: 1, items: { type: 'object', properties: { path: id, transferId: id }, required: ['path','transferId'], additionalProperties: false } }
    }, ['requestId']),
  spec('restore_skill', '将保留版本恢复为新的当前版本,须先读取当前revision。默认mode=complete,同时恢复已记录的正文、引用和包文件;旧版本文件未知时拒绝,不使用当前脚本冒充。只有用户明确要求仅恢复文字时用mode=text。既有历史沿用插件版本保留上限。相同requestId重试返回原回执。',
    { requestId, skillId: id, expectedRevision: id, versionId: id, mode: { enum: ['complete','text'] } }, ['requestId','skillId','expectedRevision','versionId'])
];

// Shared preflight: reject impossible requests before file upload as well as
// at the authoritative write boundary. Package paths are not local file paths.
export function validateSkillWriteShape(input) {
  const fail = message => { throw Object.assign(new Error(message), { code: 'invalid_input' }); };
  if (!input.files) return;
  if (['skillMarkdown', 'references', 'description', 'portableId'].some(key => Object.hasOwn(input, key))) {
    fail('整包保存以SKILL.md为准,不要同时传另一份正文、引用、说明或可移植ID;原文无需压缩。');
  }
  const paths = input.files.map(file => String(file.path).replace(/\\/g, '/'));
  if (paths.some(path => !path || /^[a-z]:/i.test(path) || /[\u0000-\u001f]/u.test(path) || path.split('/').some(part => !part || part === '.' || part === '..')) || new Set(paths).size !== paths.length) {
    fail('Skill包文件路径重复或无效。');
  }
  if (paths.filter(path => path === 'SKILL.md' || path.endsWith('/SKILL.md')).length !== 1) fail('完整Skill包须有且只有一个SKILL.md。');
}