@sanity/cli 8.22.1 → 8.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +313 -0
- package/dist/actions/codemods/dashboard.js +136 -8
- package/dist/actions/codemods/dashboard.js.map +1 -1
- package/dist/actions/codemods/types.js.map +1 -1
- package/dist/actions/context/watchJob.js +9 -3
- package/dist/actions/context/watchJob.js.map +1 -1
- package/dist/actions/deploy/blueprintManaged.js +59 -0
- package/dist/actions/deploy/blueprintManaged.js.map +1 -0
- package/dist/actions/deploy/deployApp.js +117 -1
- package/dist/actions/deploy/deployApp.js.map +1 -1
- package/dist/actions/deploy/deployRunner.js.map +1 -1
- package/dist/actions/deploy/deployStudio.js +6 -31
- package/dist/actions/deploy/deployStudio.js.map +1 -1
- package/dist/actions/exec/runScript.worker.js +6 -2
- package/dist/actions/exec/runScript.worker.js.map +1 -1
- package/dist/actions/init/bootstrapLocalTemplate.js +35 -11
- package/dist/actions/init/bootstrapLocalTemplate.js.map +1 -1
- package/dist/actions/init/createMediaLibraryCliConfig.js +12 -0
- package/dist/actions/init/createMediaLibraryCliConfig.js.map +1 -0
- package/dist/actions/init/determineMediaLibraryTemplate.js +14 -0
- package/dist/actions/init/determineMediaLibraryTemplate.js.map +1 -0
- package/dist/actions/init/initAction.js +35 -6
- package/dist/actions/init/initAction.js.map +1 -1
- package/dist/actions/init/initApp.js +81 -6
- package/dist/actions/init/initApp.js.map +1 -1
- package/dist/actions/init/offerAspectPull.js +81 -0
- package/dist/actions/init/offerAspectPull.js.map +1 -0
- package/dist/actions/init/project/getOrCreateDataset.js +8 -5
- package/dist/actions/init/project/getOrCreateDataset.js.map +1 -1
- package/dist/actions/init/project/getOrCreateProject.js +7 -5
- package/dist/actions/init/project/getOrCreateProject.js.map +1 -1
- package/dist/actions/init/project/getProjectDetails.js +25 -2
- package/dist/actions/init/project/getProjectDetails.js.map +1 -1
- package/dist/actions/init/project/promptUserForOrganization.js +6 -4
- package/dist/actions/init/project/promptUserForOrganization.js.map +1 -1
- package/dist/actions/init/templates/index.js +3 -0
- package/dist/actions/init/templates/index.js.map +1 -1
- package/dist/actions/init/templates/mediaLibraryConfig.js +51 -0
- package/dist/actions/init/templates/mediaLibraryConfig.js.map +1 -0
- package/dist/actions/media/aspectName.js +8 -0
- package/dist/actions/media/aspectName.js.map +1 -0
- package/dist/actions/media/aspectSync/contract.js +163 -0
- package/dist/actions/media/aspectSync/contract.js.map +1 -0
- package/dist/actions/media/aspectSync/syncAspects.js +115 -0
- package/dist/actions/media/aspectSync/syncAspects.js.map +1 -0
- package/dist/actions/media/cliConfigFields.js +192 -0
- package/dist/actions/media/cliConfigFields.js.map +1 -0
- package/dist/actions/media/pullAspects.js +225 -0
- package/dist/actions/media/pullAspects.js.map +1 -0
- package/dist/actions/media/resolveMediaLibraryId.js +83 -0
- package/dist/actions/media/resolveMediaLibraryId.js.map +1 -0
- package/dist/commands/codemod.js +1 -0
- package/dist/commands/codemod.js.map +1 -1
- package/dist/commands/context/build.js +2 -2
- package/dist/commands/context/build.js.map +1 -1
- package/dist/commands/context/entries/get.js +83 -0
- package/dist/commands/context/entries/get.js.map +1 -0
- package/dist/commands/context/entries/list.js +82 -0
- package/dist/commands/context/entries/list.js.map +1 -0
- package/dist/commands/context/entries/rebuild.js +95 -0
- package/dist/commands/context/entries/rebuild.js.map +1 -0
- package/dist/commands/context/issues/apply.js +86 -0
- package/dist/commands/context/issues/apply.js.map +1 -0
- package/dist/commands/context/issues/dismiss.js +52 -0
- package/dist/commands/context/issues/dismiss.js.map +1 -0
- package/dist/commands/context/issues/get.js +105 -0
- package/dist/commands/context/issues/get.js.map +1 -0
- package/dist/commands/context/issues/list.js +128 -0
- package/dist/commands/context/issues/list.js.map +1 -0
- package/dist/commands/context/issues/reopen.js +53 -0
- package/dist/commands/context/issues/reopen.js.map +1 -0
- package/dist/commands/context/issues/resolve.js +137 -0
- package/dist/commands/context/issues/resolve.js.map +1 -0
- package/dist/commands/context/jobs/get.js +2 -2
- package/dist/commands/context/jobs/get.js.map +1 -1
- package/dist/commands/context/query.js +92 -0
- package/dist/commands/context/query.js.map +1 -0
- package/dist/commands/deploy.js +13 -0
- package/dist/commands/deploy.js.map +1 -1
- package/dist/commands/media/prune-aspect-data.js +170 -0
- package/dist/commands/media/prune-aspect-data.js.map +1 -0
- package/dist/commands/media/pull-aspects.js +309 -0
- package/dist/commands/media/pull-aspects.js.map +1 -0
- package/dist/exports/invokeSanityCli/commandPolicies/mcpPolicy.js +17 -0
- package/dist/exports/invokeSanityCli/commandPolicies/mcpPolicy.js.map +1 -1
- package/dist/services/context.js +96 -3
- package/dist/services/context.js.map +1 -1
- package/dist/services/mediaLibraries.js +175 -8
- package/dist/services/mediaLibraries.js.map +1 -1
- package/dist/util/searchChoices.js +52 -0
- package/dist/util/searchChoices.js.map +1 -0
- package/oclif.manifest.json +2134 -1445
- package/package.json +9 -8
- package/templates/media-library-config/README.md +52 -0
- package/templates/media-library-config/eslint.config.txt +52 -0
- package/templates/media-library-config/src/fields/caption.tsx +29 -0
- package/templates/media-library-config/src/fields/notes.tsx +54 -0
- package/templates/shopify/.gitignore +0 -26
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/media/pull-aspects.ts"],"sourcesContent":["import {access, mkdir, readFile, writeFile} from 'node:fs/promises'\nimport path from 'node:path'\nimport {styleText} from 'node:util'\n\nimport {Flags} from '@oclif/core'\nimport {\n type CliConfig,\n exitCodes,\n ProjectRootNotFoundError,\n SanityCommand,\n subdebug,\n} from '@sanity/cli-core'\nimport {confirm, logSymbols, spinner} from '@sanity/cli-core/ux'\nimport {resolveWorkbenchConfig} from '@sanity/workbench-cli/deploy'\n\nimport {appendFieldEntries} from '../../actions/media/cliConfigFields.js'\nimport {\n convertAspectDocuments,\n formatFieldEntries,\n type GeneratedFieldModule,\n type SkippedAspect,\n} from '../../actions/media/pullAspects.js'\nimport {resolveMediaLibraryId} from '../../actions/media/resolveMediaLibraryId.js'\nimport {getAspects} from '../../services/mediaLibraries.js'\nimport {pluralize} from '../../util/pluralize.js'\nimport {defineCommandTelemetry} from '../../util/telemetry/commandTelemetry.js'\n\nconst pullAspectsDebug = subdebug('media:pull-aspects')\n\nconst NOT_A_CONFIG_APP =\n 'This command must be run inside a Media Library config app — a project whose ' +\n '`sanity.cli.ts` declares `unstable_defineMediaLibrary(...)`. Scaffold one with ' +\n '`sanity init --template media-library-config`.'\n\nconst flags = {\n 'media-library-id': Flags.string({\n description:\n 'The id of the media library to pull from (defaults to the single active library of the configured organization)',\n }),\n 'out-dir': Flags.string({\n default: 'src/fields',\n description: 'Directory to write the generated field modules to, relative to the project root',\n }),\n overwrite: Flags.boolean({\n description:\n 'Overwrite existing field modules and re-generate fields already declared in the config',\n }),\n 'update-config': Flags.boolean({\n allowNo: true,\n description:\n 'Declare the generated fields in sanity.cli.ts (--no-update-config to skip). Prompted when omitted; unattended runs only print the entries',\n }),\n}\n\n/**\n * The outcome of offering to declare the generated fields in `sanity.cli.ts`:\n * written, turned down (flag, prompt, or unattended), or not writable (the\n * config shape could not be edited safely — the `reason` says why).\n */\ntype Registration = 'added' | 'declined' | {reason: string}\n\nexport class MediaPullAspectsCommand extends SanityCommand<typeof MediaPullAspectsCommand> {\n static override description =\n \"Generate field modules from a media library's stored aspect documents\"\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'Generate src/fields/<name>.tsx modules from the stored aspects',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --media-library-id ml123 --overwrite',\n description: 'Pull from a specific media library, overwriting existing modules',\n },\n ]\n\n static override flags = flags\n\n // Part of the beta config-app flow: only usable inside a config app (like\n // the unlisted media-library-config init template, which points to it), and\n // listing it next to the legacy aspect commands would mix the two\n // paradigms. Unhide when the config app leaves beta.\n static override hidden = true\n\n // --out-dir is a local filesystem path.\n static telemetry = defineCommandTelemetry(flags, {redact: ['out-dir']})\n\n public async run(): Promise<void> {\n const {\n 'media-library-id': mediaLibraryIdFlag,\n 'out-dir': outDir,\n overwrite = false,\n } = this.flags\n\n let cliConfig: CliConfig\n try {\n cliConfig = await this.getCliConfig()\n } catch (err) {\n if (err instanceof ProjectRootNotFoundError) {\n this.error(NOT_A_CONFIG_APP, {exit: exitCodes.RUNTIME_ERROR})\n }\n throw err\n }\n\n const config = resolveWorkbenchConfig(cliConfig)\n if (!config) {\n this.error(NOT_A_CONFIG_APP, {exit: exitCodes.RUNTIME_ERROR})\n }\n\n const root = await this.getProjectRoot()\n // The generated `src:` entries and the ownership story both assume the\n // modules live inside the project; a path escaping the root is a mistake.\n // `outDirRelative` (not the raw flag) feeds the report so an absolute\n // --out-dir inside the root still prints proper relative `src:` entries.\n const outDirAbsolute = path.resolve(root.directory, outDir)\n const outDirRelative = path.relative(root.directory, outDirAbsolute)\n if (\n path.isAbsolute(outDirRelative) ||\n outDirRelative === '..' ||\n outDirRelative.startsWith(`..${path.sep}`)\n ) {\n this.error(`--out-dir must stay inside the project root (got \"${outDir}\")`, {\n exit: exitCodes.USAGE_ERROR,\n })\n }\n\n try {\n const mediaLibraryId = await resolveMediaLibraryId({\n mediaLibraryIdFlag,\n organizationId: config.organizationId,\n unattended: this.isUnattended(),\n })\n\n const spin = spinner('Fetching stored aspect documents').start()\n let documents: Awaited<ReturnType<typeof getAspects>>\n try {\n documents = await getAspects(mediaLibraryId)\n } finally {\n // Also on failure — a spinner left running garbles the error output.\n spin.stop()\n }\n\n if (documents.length === 0) {\n this.log(\n `Media library ${styleText('bold', mediaLibraryId)} has no stored aspects — nothing to pull.`,\n )\n return\n }\n\n // Names already declared in the config are owned by existing field\n // modules; pulling the stored mirror back over them would regress local\n // edits, so they are skipped unless --overwrite says otherwise.\n const configuredNames = new Set(config.fields.map((field) => field.name))\n const configuredSrcByName = new Map(config.fields.map((field) => [field.name, field.src]))\n\n // One module per field name; name collisions and invalid documents are skipped.\n const {modules: winners, skipped} = convertAspectDocuments(documents)\n\n const generated: GeneratedFieldModule[] = []\n // Module name -> written path relative to the project root, so the\n // report shows where each module actually landed.\n const writtenPaths = new Map<string, string>()\n // Module name -> path of a module skipped because it already exists on\n // disk. Those fields may still be undeclared and need registration.\n const existingPaths = new Map<string, string>()\n for (const module of winners) {\n if (!overwrite && configuredNames.has(module.name)) {\n skipped.push({\n id: module.name,\n reasons: ['Already declared in sanity.cli.ts (pass --overwrite to re-generate)'],\n status: 'skipped',\n validationErrors: [],\n })\n continue\n }\n\n const defaultDestination = path.join(outDirAbsolute, module.filename)\n // The pull generates `.tsx`, but a hand-written module for the same\n // field may be plain `.ts` — treat both as taken.\n const existing = await firstExisting([\n defaultDestination,\n defaultDestination.replace(/\\.tsx$/, '.ts'),\n ])\n if (!overwrite && existing) {\n // The file is left alone, but the field is undeclared (declared\n // names were skipped above) — so it still joins the registration\n // offer below. An on-disk module without a config entry is exactly\n // the state the next deploy punishes, however the file got there.\n existingPaths.set(module.name, path.relative(root.directory, existing))\n skipped.push({\n id: module.name,\n reasons: [\n `A file already exists at ${path.relative(root.directory, existing)} (pass --overwrite to replace it)`,\n ],\n status: 'skipped',\n validationErrors: [],\n })\n continue\n }\n\n // When overwriting a field the config already declares, the module\n // must land at the declared `src` path — writing <out-dir>/<name>.tsx\n // instead would orphan the new file while deploy keeps evaluating the\n // old one. Failing that, replace the module found on disk (which may\n // be the .ts variant), and only then fall back to the default.\n const declaredSrc = overwrite ? configuredSrcByName.get(module.name) : undefined\n const destination = declaredSrc\n ? await resolveDeclaredSrc(path.resolve(root.directory, declaredSrc), module.filename)\n : (existing ?? defaultDestination)\n\n await mkdir(path.dirname(destination), {recursive: true})\n await writeFile(destination, module.source)\n writtenPaths.set(module.name, path.relative(root.directory, destination))\n generated.push(module)\n }\n\n // Fields present locally — written this run or already on disk — but not\n // declared get offered for registration: the deploy mirrors config to\n // stored documents, so an undeclared stored aspect is deleted by the\n // next deploy — leaving the entries unpasted is a data hazard, not just\n // friction. Existing files count because a prior pull may have been\n // declined (or edited by hand): the file being there does not make the\n // field declared.\n const entryPaths = new Map([...writtenPaths, ...existingPaths])\n const newEntries = winners.filter(\n (module) => !configuredNames.has(module.name) && entryPaths.has(module.name),\n )\n const entryLines =\n newEntries.length > 0\n ? formatFieldEntries(newEntries, outDirRelative, entryPaths).split('\\n')\n : []\n const registration =\n newEntries.length > 0\n ? await this.maybeRegisterFields({configPath: root.path, entryLines})\n : null\n\n this.report({\n configuredNames,\n entryLines,\n generated,\n newEntries,\n outDir: outDirRelative,\n registration,\n skipped,\n writtenPaths,\n })\n } catch (error) {\n // Ctrl+C on the library prompt and errors that already carry their exit\n // code (`this.error` inside helpers) pass through to SanityCommand.catch.\n if (error instanceof Error && error.name === 'ExitPromptError') throw error\n if (error && typeof error === 'object' && 'oclif' in error) throw error\n const err = error as Error\n pullAspectsDebug('Failed to pull aspects', {error: err, mediaLibraryId: mediaLibraryIdFlag})\n this.error(\n styleText('bold', 'Failed to pull aspects') + `\\n\\n${styleText('red', err.message)}`,\n {exit: exitCodes.RUNTIME_ERROR},\n )\n }\n }\n\n /**\n * Offer to declare the generated fields in `sanity.cli.ts`. `--update-config`\n * / `--no-update-config` decide outright; otherwise an interactive run\n * prompts (default yes) and an unattended run leaves the config alone.\n */\n private async maybeRegisterFields({\n configPath,\n entryLines,\n }: {\n configPath: string\n entryLines: string[]\n }): Promise<Registration> {\n const flag = this.flags['update-config']\n const shouldUpdate =\n flag ??\n (this.isUnattended()\n ? false\n : await confirm({\n default: true,\n message: `Declare the ${entryLines.length} pulled ${pluralize('field', entryLines.length)} in sanity.cli.ts?`,\n }))\n if (!shouldUpdate) return 'declined'\n\n const source = await readFile(configPath, 'utf8')\n const result = appendFieldEntries(source, entryLines)\n if (!result.ok) return {reason: result.reason}\n await writeFile(configPath, result.updated)\n return 'added'\n }\n\n private report({\n configuredNames,\n entryLines,\n generated,\n newEntries,\n outDir,\n registration,\n skipped,\n writtenPaths,\n }: {\n configuredNames: Set<string>\n entryLines: string[]\n generated: GeneratedFieldModule[]\n newEntries: GeneratedFieldModule[]\n outDir: string\n registration: Registration | null\n skipped: SkippedAspect[]\n writtenPaths: Map<string, string>\n }): void {\n if (skipped.length > 0) {\n this.logToStderr('')\n this.warn(\n styleText('bold', `Skipped ${skipped.length} ${pluralize('aspect', skipped.length)}`),\n )\n for (const aspect of skipped) {\n const errors = aspect.validationErrors.flatMap((group) => group.map(({message}) => message))\n const reasons = [...aspect.reasons, ...errors]\n this.logToStderr(` - ${aspect.id} ${styleText('dim', reasons.join('; '))}`)\n }\n }\n\n // No modules written and nothing to declare: done. Skipped-but-undeclared\n // fields keep the report alive — their registration outcome still matters.\n if (generated.length === 0 && newEntries.length === 0) {\n this.log('')\n this.log('No field modules generated.')\n return\n }\n\n if (generated.length === 0) {\n this.log('')\n this.log('No field modules generated.')\n } else {\n this.log('')\n this.log(\n `${logSymbols.success} ${styleText('bold', `Generated ${generated.length} field ${pluralize('module', generated.length)}`)}`,\n )\n for (const module of generated) {\n const location = writtenPaths.get(module.name) ?? path.join(outDir, module.filename)\n this.log(` - ${module.name} ${styleText('dim', location)}`)\n for (const warning of module.warnings) {\n this.log(` ${logSymbols.warning} ${warning}`)\n }\n }\n }\n\n if (newEntries.length > 0 && registration === 'added') {\n this.log('')\n this.log(\n `${logSymbols.success} Declared ${newEntries.length} ${pluralize('field', newEntries.length)} in ${styleText('bold', 'sanity.cli.ts')}`,\n )\n } else if (newEntries.length > 0) {\n if (registration !== null && typeof registration === 'object') {\n this.warn(`Could not update sanity.cli.ts automatically: ${registration.reason}`)\n }\n this.log('')\n this.log(\n configuredNames.size > 0\n ? `Add the pulled ${pluralize('field', newEntries.length)} to the \\`fields\\` array in ${styleText('bold', 'sanity.cli.ts')}:`\n : `Declare the pulled ${pluralize('field', newEntries.length)} in ${styleText('bold', 'sanity.cli.ts')}:`,\n )\n this.log('')\n this.log(entryLines.map((line) => ` ${line}`).join('\\n'))\n // The deploy mirrors config into stored documents; a pulled-but-undeclared\n // field reads as stale there, so leaving the entries out is destructive.\n this.warn(\n '`sanity deploy` deletes the stored definition of any field not declared in sanity.cli.ts.',\n )\n }\n\n this.log('')\n this.log(\n 'Once declared in sanity.cli.ts, the config app owns these definitions — deploying the',\n )\n this.log('config overwrites the stored documents this pull read from. Stored definitions')\n this.log('not declared in sanity.cli.ts are deleted on deploy.')\n }\n}\n\n/**\n * The write target for a declared `src` path. A declared path may omit the\n * extension — the bundler and jiti both resolve `./src/fields/notes` to\n * `notes.ts(x)` — so writing it literally would create an extensionless file\n * next to the module deploy actually evaluates. Resolve to the existing\n * module file; if none exists, append the generated module's extension.\n */\nasync function resolveDeclaredSrc(declaredPath: string, moduleFilename: string): Promise<string> {\n if (path.extname(declaredPath) !== '') return declaredPath\n const existing = await firstExisting([`${declaredPath}.ts`, `${declaredPath}.tsx`])\n return existing ?? `${declaredPath}${path.extname(moduleFilename)}`\n}\n\n/** The first path in `paths` that exists, or `undefined`. */\nasync function firstExisting(paths: string[]): Promise<string | undefined> {\n for (const filePath of paths) {\n const exists = await access(filePath).then(\n () => true,\n () => false,\n )\n if (exists) return filePath\n }\n return undefined\n}\n"],"names":["access","mkdir","readFile","writeFile","path","styleText","Flags","exitCodes","ProjectRootNotFoundError","SanityCommand","subdebug","confirm","logSymbols","spinner","resolveWorkbenchConfig","appendFieldEntries","convertAspectDocuments","formatFieldEntries","resolveMediaLibraryId","getAspects","pluralize","defineCommandTelemetry","pullAspectsDebug","NOT_A_CONFIG_APP","flags","string","description","default","overwrite","boolean","allowNo","MediaPullAspectsCommand","examples","command","hidden","telemetry","redact","run","mediaLibraryIdFlag","outDir","cliConfig","getCliConfig","err","error","exit","RUNTIME_ERROR","config","root","getProjectRoot","outDirAbsolute","resolve","directory","outDirRelative","relative","isAbsolute","startsWith","sep","USAGE_ERROR","mediaLibraryId","organizationId","unattended","isUnattended","spin","start","documents","stop","length","log","configuredNames","Set","fields","map","field","name","configuredSrcByName","Map","src","modules","winners","skipped","generated","writtenPaths","existingPaths","module","has","push","id","reasons","status","validationErrors","defaultDestination","join","filename","existing","firstExisting","replace","set","declaredSrc","get","undefined","destination","resolveDeclaredSrc","dirname","recursive","source","entryPaths","newEntries","filter","entryLines","split","registration","maybeRegisterFields","configPath","report","Error","message","flag","shouldUpdate","result","ok","reason","updated","logToStderr","warn","aspect","errors","flatMap","group","success","location","warning","warnings","size","line","declaredPath","moduleFilename","extname","paths","filePath","exists","then"],"mappings":"AAAA,SAAQA,MAAM,EAAEC,KAAK,EAAEC,QAAQ,EAAEC,SAAS,QAAO,mBAAkB;AACnE,OAAOC,UAAU,YAAW;AAC5B,SAAQC,SAAS,QAAO,YAAW;AAEnC,SAAQC,KAAK,QAAO,cAAa;AACjC,SAEEC,SAAS,EACTC,wBAAwB,EACxBC,aAAa,EACbC,QAAQ,QACH,mBAAkB;AACzB,SAAQC,OAAO,EAAEC,UAAU,EAAEC,OAAO,QAAO,sBAAqB;AAChE,SAAQC,sBAAsB,QAAO,+BAA8B;AAEnE,SAAQC,kBAAkB,QAAO,yCAAwC;AACzE,SACEC,sBAAsB,EACtBC,kBAAkB,QAGb,qCAAoC;AAC3C,SAAQC,qBAAqB,QAAO,+CAA8C;AAClF,SAAQC,UAAU,QAAO,mCAAkC;AAC3D,SAAQC,SAAS,QAAO,0BAAyB;AACjD,SAAQC,sBAAsB,QAAO,2CAA0C;AAE/E,MAAMC,mBAAmBZ,SAAS;AAElC,MAAMa,mBACJ,kFACA,oFACA;AAEF,MAAMC,QAAQ;IACZ,oBAAoBlB,MAAMmB,MAAM,CAAC;QAC/BC,aACE;IACJ;IACA,WAAWpB,MAAMmB,MAAM,CAAC;QACtBE,SAAS;QACTD,aAAa;IACf;IACAE,WAAWtB,MAAMuB,OAAO,CAAC;QACvBH,aACE;IACJ;IACA,iBAAiBpB,MAAMuB,OAAO,CAAC;QAC7BC,SAAS;QACTJ,aACE;IACJ;AACF;AASA,OAAO,MAAMK,gCAAgCtB;IAC3C,OAAgBiB,cACd,wEAAuE;IAEzE,OAAgBM,WAAW;QACzB;YACEC,SAAS;YACTP,aAAa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AE,4BAA4B,EAAE7D,UAAU,QAAQ,iBAAiB,CAAC,CAAC,GAC3H,CAAC,mBAAmB,EAAEe,UAAU,SAASsF,WAAWxC,MAAM,EAAE,IAAI,EAAE7D,UAAU,QAAQ,iBAAiB,CAAC,CAAC;YAE7G,IAAI,CAAC8D,GAAG,CAAC;YACT,IAAI,CAACA,GAAG,CAACyC,WAAWrC,GAAG,CAAC,CAAC8D,OAAS,CAAC,EAAE,EAAEA,MAAM,EAAE1C,IAAI,CAAC;YACpD,2EAA2E;YAC3E,yEAAyE;YACzE,IAAI,CAACgC,IAAI,CACP;QAEJ;QAEA,IAAI,CAACxD,GAAG,CAAC;QACT,IAAI,CAACA,GAAG,CACN;QAEF,IAAI,CAACA,GAAG,CAAC;QACT,IAAI,CAACA,GAAG,CAAC;IACX;AACF;AAEA;;;;;;CAMC,GACD,eAAekC,mBAAmBiC,YAAoB,EAAEC,cAAsB;IAC5E,IAAInI,KAAKoI,OAAO,CAACF,kBAAkB,IAAI,OAAOA;IAC9C,MAAMzC,WAAW,MAAMC,cAAc;QAAC,GAAGwC,aAAa,GAAG,CAAC;QAAE,GAAGA,aAAa,IAAI,CAAC;KAAC;IAClF,OAAOzC,YAAY,GAAGyC,eAAelI,KAAKoI,OAAO,CAACD,iBAAiB;AACrE;AAEA,2DAA2D,GAC3D,eAAezC,cAAc2C,KAAe;IAC1C,KAAK,MAAMC,YAAYD,MAAO;QAC5B,MAAME,SAAS,MAAM3I,OAAO0I,UAAUE,IAAI,CACxC,IAAM,MACN,IAAM;QAER,IAAID,QAAQ,OAAOD;IACrB;IACA,OAAOvC;AACT"}
|
|
@@ -71,6 +71,10 @@ function apiValidator({ args, flags }) {
|
|
|
71
71
|
'context:build': conditionalDenyFlags('watch'),
|
|
72
72
|
'context:create': allow,
|
|
73
73
|
'context:delete': allow,
|
|
74
|
+
'context:entries:get': allow,
|
|
75
|
+
'context:entries:list': allow,
|
|
76
|
+
// See context:build — --watch has no deadline and would hang the MCP tool call.
|
|
77
|
+
'context:entries:rebuild': conditionalDenyFlags('watch'),
|
|
74
78
|
'context:get': allow,
|
|
75
79
|
// --file reads the import payload from the local filesystem; text, URL and
|
|
76
80
|
// dataset imports are remote-only.
|
|
@@ -80,8 +84,17 @@ function apiValidator({ args, flags }) {
|
|
|
80
84
|
'context:imports:get': allow,
|
|
81
85
|
'context:imports:list': allow,
|
|
82
86
|
// See context:build — --watch has no deadline and would hang the MCP tool call.
|
|
87
|
+
'context:issues:apply': conditionalDenyFlags('watch'),
|
|
88
|
+
'context:issues:dismiss': allow,
|
|
89
|
+
'context:issues:get': allow,
|
|
90
|
+
'context:issues:list': allow,
|
|
91
|
+
'context:issues:reopen': allow,
|
|
92
|
+
// See context:build — --watch has no deadline and would hang the MCP tool call.
|
|
93
|
+
'context:issues:resolve': conditionalDenyFlags('watch'),
|
|
94
|
+
// See context:build — --watch has no deadline and would hang the MCP tool call.
|
|
83
95
|
'context:jobs:get': conditionalDenyFlags('watch'),
|
|
84
96
|
'context:list': allow,
|
|
97
|
+
'context:query': allow,
|
|
85
98
|
'context:refresh': allow,
|
|
86
99
|
'context:update': allow,
|
|
87
100
|
'cors:add': allow,
|
|
@@ -170,6 +183,10 @@ function apiValidator({ args, flags }) {
|
|
|
170
183
|
],
|
|
171
184
|
validate: ({ args })=>typeof args.source === 'string' && isRemoteAssetSource(args.source)
|
|
172
185
|
}),
|
|
186
|
+
// media:pull-aspects and media:prune-aspect-data are hidden while the
|
|
187
|
+
// config app is in beta, so they take the deny-by-default path like any
|
|
188
|
+
// unlisted command. The exhaustiveness test forces explicit entries when
|
|
189
|
+
// they are unhidden.
|
|
173
190
|
// Creates migration source files in the local project.
|
|
174
191
|
'migrations:create': deny,
|
|
175
192
|
// Reads and loads migration definitions from the local project.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/exports/invokeSanityCli/commandPolicies/mcpPolicy.ts"],"sourcesContent":["import {\n allow,\n type CommandPolicySet,\n conditionalDenyFlags,\n conditionalPolicy,\n deny,\n} from '@sanity/cli-core/commandPolicy'\n\nimport {isRemoteAssetSource} from '../../../util/isRemoteAssetSource.js'\n\nfunction apiValidator({\n args,\n flags,\n}: {\n args: Readonly<Record<string, unknown>>\n flags: Readonly<Record<string, unknown>>\n}): boolean {\n const fields: unknown[] = Array.isArray(flags.field) ? flags.field : []\n\n const fieldReadsFromHost = fields.some((field) => {\n if (typeof field !== 'string') return false\n const separatorIndex = field.indexOf('=')\n return separatorIndex > 0 && field[separatorIndex + 1] === '@'\n })\n if (fieldReadsFromHost) return false\n\n const headers: unknown[] = Array.isArray(flags.header) ? flags.header : []\n\n const headerOverridesAuthentication = headers.some((header) => {\n if (typeof header !== 'string') return false\n const separatorIndex = header.indexOf(':')\n if (separatorIndex <= 0) return false\n\n const name = header.slice(0, separatorIndex).trim().toLowerCase()\n return name === 'authorization' || name === 'cookie'\n })\n\n if (headerOverridesAuthentication) return false\n\n const endpoint = typeof args.endpoint === 'string' ? URL.parse(args.endpoint) : null\n const urlEmbedsCredentials =\n endpoint !== null && (endpoint.username !== '' || endpoint.password !== '')\n\n return !urlEmbedsCredentials\n}\n\n/**\n * MCP programmatic mode disables local project/config discovery (see the CLI\n * execution context). Missing project or dataset values may therefore produce\n * a usage error, but cannot cause local filesystem access. Destructive remote\n * operations are allowed and do not by themselves make a command unsafe.\n *\n * Every command this package contributes must have exactly one policy here:\n * - allow: every valid invocation is safe\n * - conditional: safety depends on parsed arguments or flags\n * - deny: no invocation is safe\n *\n * Commands contributed by plugins are not listed here. Each plugin declares\n * policies for its own commands (see `PluginInvocationPolicies` in\n * `@sanity/cli-core/commandPolicy`).\n */\nexport const mcpPolicy: CommandPolicySet = {\n // Special exception, this can be very dangerous but is also super useful\n // to expose. Refuse authentication overrides and host input channels:\n // `--token`, Authorization, and Cookie headers replace the MCP user's authentication,\n // URL-embedded credentials replace it with Basic authentication,\n // `--input` reads the request body from the host's filesystem or stdin, and\n // `-F key=@<file>` / `-F key=@-` field values do the same.\n api: conditionalPolicy({\n deniedFlags: ['input', 'token'],\n validate: apiValidator,\n }),\n\n // --file reads from the machine running the command and must only run locally.\n 'assets:upload': conditionalPolicy({\n deniedFlags: ['content-type', 'file'],\n validate: ({flags}) => typeof flags['from-url'] === 'string' && flags['from-url'].length > 0,\n }),\n\n 'backups:disable': allow,\n // Writes a downloaded backup to the local filesystem.\n 'backups:download': deny,\n 'backups:enable': allow,\n 'backups:list': allow,\n\n // Requires a local Studio project and writes build output to disk.\n build: deny,\n\n // Reads and rewrites local source code.\n codemod: deny,\n\n // --watch polls a job with no deadline; a stuck job would hang the MCP tool\n // call indefinitely. Non-watch invocations are single remote requests.\n 'context:build': conditionalDenyFlags('watch'),\n 'context:create': allow,\n 'context:delete': allow,\n 'context:get': allow,\n // --file reads the import payload from the local filesystem; text, URL and\n // dataset imports are remote-only.\n 'context:imports:create': conditionalDenyFlags('file'),\n 'context:imports:delete': allow,\n 'context:imports:download': allow,\n 'context:imports:get': allow,\n 'context:imports:list': allow,\n // See context:build — --watch has no deadline and would hang the MCP tool call.\n 'context:jobs:get': conditionalDenyFlags('watch'),\n 'context:list': allow,\n 'context:refresh': allow,\n 'context:update': allow,\n\n 'cors:add': allow,\n 'cors:delete': allow,\n 'cors:list': allow,\n\n 'datasets:alias:create': allow,\n 'datasets:alias:delete': allow,\n 'datasets:alias:link': allow,\n 'datasets:alias:unlink': allow,\n 'datasets:copy': allow,\n 'datasets:create': allow,\n 'datasets:delete': allow,\n 'datasets:embeddings:disable': allow,\n 'datasets:embeddings:enable': allow,\n 'datasets:embeddings:status': allow,\n // Writes dataset contents and assets to the local filesystem.\n 'datasets:export': deny,\n // Reads import data from the local filesystem and may replace documents.\n 'datasets:import': deny,\n 'datasets:list': allow,\n 'datasets:visibility:get': allow,\n 'datasets:visibility:set': allow,\n\n // Inspects local project files and can print authentication secrets.\n debug: deny,\n\n // Reads, builds, and deploys a local Studio project.\n deploy: deny,\n\n // Loads a local Studio project and starts a development server.\n dev: deny,\n\n // Opens a browser on the machine running the MCP server.\n 'docs:browse': deny,\n // --web opens a browser on the machine running the MCP server.\n 'docs:read': conditionalDenyFlags('web'),\n 'docs:search': allow,\n\n // Reads and executes local project configuration for diagnostics.\n doctor: deny,\n\n // Reads document input from disk or launches a local editor.\n 'documents:create': deny,\n 'documents:delete': allow,\n 'documents:get': allow,\n 'documents:query': allow,\n // Loads a local Studio schema to validate documents.\n 'documents:validate': deny,\n\n // Executes arbitrary code in the local Studio context.\n exec: deny,\n\n // Loads a local schema and deploys a GraphQL API.\n 'graphql:deploy': deny,\n 'graphql:list': allow,\n // --api loads GraphQL definitions from the local project; explicit project/dataset flags are safe.\n 'graphql:undeploy': conditionalDenyFlags('api'),\n\n 'hooks:attempt': allow,\n 'hooks:create': allow,\n 'hooks:delete': allow,\n 'hooks:list': allow,\n 'hooks:logs': allow,\n\n // Creates or modifies a local project and may install dependencies.\n init: deny,\n\n // Installs packages into the local project.\n install: deny,\n\n // Opens a browser on the machine running the MCP server.\n learn: deny,\n\n // Performs an authentication flow.\n login: deny,\n\n // Performs an authentication operation and clears local credentials.\n logout: deny,\n\n // Reads local project configuration and opens a browser.\n manage: deny,\n\n // Loads local Studio configuration and writes manifest files.\n 'manifest:extract': deny,\n\n // Reads and writes local MCP client configuration.\n 'mcp:configure': deny,\n\n // Writes an aspect definition to the local filesystem.\n 'media:create-aspect': deny,\n 'media:delete-aspect': allow,\n // Reads a local aspect definition before deploying it.\n 'media:deploy-aspect': deny,\n // Writes media assets to the local filesystem.\n 'media:export': deny,\n // A directory or archive source reads media assets from the local\n // filesystem. A URL source does not: Sanity fetches the asset itself, so the\n // only local input is the URL. `--replace-aspects` is meaningless for a URL\n // source (the command rejects the combination), so it is hidden here rather\n // than advertised as usable surface.\n 'media:import': conditionalPolicy({\n deniedFlags: ['replace-aspects'],\n validate: ({args}) => typeof args.source === 'string' && isRemoteAssetSource(args.source),\n }),\n\n // Creates migration source files in the local project.\n 'migrations:create': deny,\n // Reads and loads migration definitions from the local project.\n 'migrations:list': deny,\n // Executes local migration code that may perform arbitrary document mutations.\n 'migrations:run': deny,\n\n // Writes project setup values and scaffolds local applications.\n new: deny,\n\n // --web opens a browser on the machine running the MCP server.\n 'openapi:get': conditionalDenyFlags('web'),\n 'openapi:list': conditionalDenyFlags('web'),\n\n 'organizations:create': allow,\n 'organizations:delete': allow,\n 'organizations:get': allow,\n 'organizations:list': allow,\n 'organizations:update': allow,\n\n // Serves a local production build.\n preview: deny,\n\n 'projects:create': allow,\n 'projects:list': allow,\n // Reads locally stored claim URLs and robot tokens.\n 'projects:unclaimed': deny,\n\n // Loads the local Studio configuration to resolve the datasets containing each schema.\n 'schemas:delete': deny,\n // Loads local schema files before deploying them.\n 'schemas:deploy': deny,\n // Loads local Studio configuration and writes an extracted schema to disk.\n 'schemas:extract': deny,\n // Requires a local project and loads its schema configuration.\n 'schemas:list': deny,\n // Loads and executes a local Studio schema.\n 'schemas:validate': deny,\n\n // Installs skills into local editor configuration directories.\n 'skills:install': deny,\n\n // Changes account telemetry preferences and mutates local cached configuration.\n 'telemetry:disable': deny,\n // Changes account telemetry preferences and mutates local cached configuration.\n 'telemetry:enable': deny,\n 'telemetry:status': allow,\n\n // Creates authentication credentials.\n 'tokens:create': deny,\n // Deletes authentication credentials.\n 'tokens:delete': deny,\n // Exposes authentication credential metadata.\n 'tokens:list': deny,\n // Replaces authentication credentials and exposes the new secret.\n 'tokens:rotate': deny,\n\n // Reads the local schema and source files and writes generated types to the project.\n 'typegen:generate': deny,\n\n // Loads local CLI and workbench configuration to identify the deployed Studio or application.\n undeploy: deny,\n\n // Grants a user access to a project.\n 'users:invite': deny,\n 'users:list': allow,\n\n // Reads the local project and installed package tree.\n versions: deny,\n\n // Reads the authenticated user from the API using the execution context token.\n whoami: allow,\n}\n"],"names":["allow","conditionalDenyFlags","conditionalPolicy","deny","isRemoteAssetSource","apiValidator","args","flags","fields","Array","isArray","field","fieldReadsFromHost","some","separatorIndex","indexOf","headers","header","headerOverridesAuthentication","name","slice","trim","toLowerCase","endpoint","URL","parse","urlEmbedsCredentials","username","password","mcpPolicy","api","deniedFlags","validate","length","build","codemod","debug","deploy","dev","doctor","exec","init","install","learn","login","logout","manage","source","new","preview","undeploy","versions","whoami"],"mappings":"AAAA,SACEA,KAAK,EAELC,oBAAoB,EACpBC,iBAAiB,EACjBC,IAAI,QACC,iCAAgC;AAEvC,SAAQC,mBAAmB,QAAO,uCAAsC;AAExE,SAASC,aAAa,EACpBC,IAAI,EACJC,KAAK,EAIN;IACC,MAAMC,SAAoBC,MAAMC,OAAO,CAACH,MAAMI,KAAK,IAAIJ,MAAMI,KAAK,GAAG,EAAE;IAEvE,MAAMC,qBAAqBJ,OAAOK,IAAI,CAAC,CAACF;QACtC,IAAI,OAAOA,UAAU,UAAU,OAAO;QACtC,MAAMG,iBAAiBH,MAAMI,OAAO,CAAC;QACrC,OAAOD,iBAAiB,KAAKH,KAAK,CAACG,iBAAiB,EAAE,KAAK;IAC7D;IACA,IAAIF,oBAAoB,OAAO;IAE/B,MAAMI,UAAqBP,MAAMC,OAAO,CAACH,MAAMU,MAAM,IAAIV,MAAMU,MAAM,GAAG,EAAE;IAE1E,MAAMC,gCAAgCF,QAAQH,IAAI,CAAC,CAACI;QAClD,IAAI,OAAOA,WAAW,UAAU,OAAO;QACvC,MAAMH,iBAAiBG,OAAOF,OAAO,CAAC;QACtC,IAAID,kBAAkB,GAAG,OAAO;QAEhC,MAAMK,OAAOF,OAAOG,KAAK,CAAC,GAAGN,gBAAgBO,IAAI,GAAGC,WAAW;QAC/D,OAAOH,SAAS,mBAAmBA,SAAS;IAC9C;IAEA,IAAID,+BAA+B,OAAO;IAE1C,MAAMK,WAAW,OAAOjB,KAAKiB,QAAQ,KAAK,WAAWC,IAAIC,KAAK,CAACnB,KAAKiB,QAAQ,IAAI;IAChF,MAAMG,uBACJH,aAAa,QAASA,CAAAA,SAASI,QAAQ,KAAK,MAAMJ,SAASK,QAAQ,KAAK,EAAC;IAE3E,OAAO,CAACF;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AErC,wBAAwBD;IACxB,wBAAwBA;IACxB,qBAAqBA;IACrB,sBAAsBA;IACtB,wBAAwBA;IAExB,mCAAmC;IACnCiD,SAAS9C;IAET,mBAAmBH;IACnB,iBAAiBA;IACjB,oDAAoD;IACpD,sBAAsBG;IAEtB,uFAAuF;IACvF,kBAAkBA;IAClB,kDAAkD;IAClD,kBAAkBA;IAClB,2EAA2E;IAC3E,mBAAmBA;IACnB,+DAA+D;IAC/D,gBAAgBA;IAChB,4CAA4C;IAC5C,oBAAoBA;IAEpB,+DAA+D;IAC/D,kBAAkBA;IAElB,gFAAgF;IAChF,qBAAqBA;IACrB,gFAAgF;IAChF,oBAAoBA;IACpB,oBAAoBH;IAEpB,sCAAsC;IACtC,iBAAiBG;IACjB,sCAAsC;IACtC,iBAAiBA;IACjB,8CAA8C;IAC9C,eAAeA;IACf,kEAAkE;IAClE,iBAAiBA;IAEjB,qFAAqF;IACrF,oBAAoBA;IAEpB,8FAA8F;IAC9F+C,UAAU/C;IAEV,qCAAqC;IACrC,gBAAgBA;IAChB,cAAcH;IAEd,sDAAsD;IACtDmD,UAAUhD;IAEV,+EAA+E;IAC/EiD,QAAQpD;AACV,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/exports/invokeSanityCli/commandPolicies/mcpPolicy.ts"],"sourcesContent":["import {\n allow,\n type CommandPolicySet,\n conditionalDenyFlags,\n conditionalPolicy,\n deny,\n} from '@sanity/cli-core/commandPolicy'\n\nimport {isRemoteAssetSource} from '../../../util/isRemoteAssetSource.js'\n\nfunction apiValidator({\n args,\n flags,\n}: {\n args: Readonly<Record<string, unknown>>\n flags: Readonly<Record<string, unknown>>\n}): boolean {\n const fields: unknown[] = Array.isArray(flags.field) ? flags.field : []\n\n const fieldReadsFromHost = fields.some((field) => {\n if (typeof field !== 'string') return false\n const separatorIndex = field.indexOf('=')\n return separatorIndex > 0 && field[separatorIndex + 1] === '@'\n })\n if (fieldReadsFromHost) return false\n\n const headers: unknown[] = Array.isArray(flags.header) ? flags.header : []\n\n const headerOverridesAuthentication = headers.some((header) => {\n if (typeof header !== 'string') return false\n const separatorIndex = header.indexOf(':')\n if (separatorIndex <= 0) return false\n\n const name = header.slice(0, separatorIndex).trim().toLowerCase()\n return name === 'authorization' || name === 'cookie'\n })\n\n if (headerOverridesAuthentication) return false\n\n const endpoint = typeof args.endpoint === 'string' ? URL.parse(args.endpoint) : null\n const urlEmbedsCredentials =\n endpoint !== null && (endpoint.username !== '' || endpoint.password !== '')\n\n return !urlEmbedsCredentials\n}\n\n/**\n * MCP programmatic mode disables local project/config discovery (see the CLI\n * execution context). Missing project or dataset values may therefore produce\n * a usage error, but cannot cause local filesystem access. Destructive remote\n * operations are allowed and do not by themselves make a command unsafe.\n *\n * Every command this package contributes must have exactly one policy here:\n * - allow: every valid invocation is safe\n * - conditional: safety depends on parsed arguments or flags\n * - deny: no invocation is safe\n *\n * Commands contributed by plugins are not listed here. Each plugin declares\n * policies for its own commands (see `PluginInvocationPolicies` in\n * `@sanity/cli-core/commandPolicy`).\n */\nexport const mcpPolicy: CommandPolicySet = {\n // Special exception, this can be very dangerous but is also super useful\n // to expose. Refuse authentication overrides and host input channels:\n // `--token`, Authorization, and Cookie headers replace the MCP user's authentication,\n // URL-embedded credentials replace it with Basic authentication,\n // `--input` reads the request body from the host's filesystem or stdin, and\n // `-F key=@<file>` / `-F key=@-` field values do the same.\n api: conditionalPolicy({\n deniedFlags: ['input', 'token'],\n validate: apiValidator,\n }),\n\n // --file reads from the machine running the command and must only run locally.\n 'assets:upload': conditionalPolicy({\n deniedFlags: ['content-type', 'file'],\n validate: ({flags}) => typeof flags['from-url'] === 'string' && flags['from-url'].length > 0,\n }),\n\n 'backups:disable': allow,\n // Writes a downloaded backup to the local filesystem.\n 'backups:download': deny,\n 'backups:enable': allow,\n 'backups:list': allow,\n\n // Requires a local Studio project and writes build output to disk.\n build: deny,\n\n // Reads and rewrites local source code.\n codemod: deny,\n\n // --watch polls a job with no deadline; a stuck job would hang the MCP tool\n // call indefinitely. Non-watch invocations are single remote requests.\n 'context:build': conditionalDenyFlags('watch'),\n 'context:create': allow,\n 'context:delete': allow,\n 'context:entries:get': allow,\n 'context:entries:list': allow,\n // See context:build — --watch has no deadline and would hang the MCP tool call.\n 'context:entries:rebuild': conditionalDenyFlags('watch'),\n 'context:get': allow,\n // --file reads the import payload from the local filesystem; text, URL and\n // dataset imports are remote-only.\n 'context:imports:create': conditionalDenyFlags('file'),\n 'context:imports:delete': allow,\n 'context:imports:download': allow,\n 'context:imports:get': allow,\n 'context:imports:list': allow,\n // See context:build — --watch has no deadline and would hang the MCP tool call.\n 'context:issues:apply': conditionalDenyFlags('watch'),\n 'context:issues:dismiss': allow,\n 'context:issues:get': allow,\n 'context:issues:list': allow,\n 'context:issues:reopen': allow,\n // See context:build — --watch has no deadline and would hang the MCP tool call.\n 'context:issues:resolve': conditionalDenyFlags('watch'),\n // See context:build — --watch has no deadline and would hang the MCP tool call.\n 'context:jobs:get': conditionalDenyFlags('watch'),\n 'context:list': allow,\n 'context:query': allow,\n 'context:refresh': allow,\n 'context:update': allow,\n\n 'cors:add': allow,\n 'cors:delete': allow,\n 'cors:list': allow,\n\n 'datasets:alias:create': allow,\n 'datasets:alias:delete': allow,\n 'datasets:alias:link': allow,\n 'datasets:alias:unlink': allow,\n 'datasets:copy': allow,\n 'datasets:create': allow,\n 'datasets:delete': allow,\n 'datasets:embeddings:disable': allow,\n 'datasets:embeddings:enable': allow,\n 'datasets:embeddings:status': allow,\n // Writes dataset contents and assets to the local filesystem.\n 'datasets:export': deny,\n // Reads import data from the local filesystem and may replace documents.\n 'datasets:import': deny,\n 'datasets:list': allow,\n 'datasets:visibility:get': allow,\n 'datasets:visibility:set': allow,\n\n // Inspects local project files and can print authentication secrets.\n debug: deny,\n\n // Reads, builds, and deploys a local Studio project.\n deploy: deny,\n\n // Loads a local Studio project and starts a development server.\n dev: deny,\n\n // Opens a browser on the machine running the MCP server.\n 'docs:browse': deny,\n // --web opens a browser on the machine running the MCP server.\n 'docs:read': conditionalDenyFlags('web'),\n 'docs:search': allow,\n\n // Reads and executes local project configuration for diagnostics.\n doctor: deny,\n\n // Reads document input from disk or launches a local editor.\n 'documents:create': deny,\n 'documents:delete': allow,\n 'documents:get': allow,\n 'documents:query': allow,\n // Loads a local Studio schema to validate documents.\n 'documents:validate': deny,\n\n // Executes arbitrary code in the local Studio context.\n exec: deny,\n\n // Loads a local schema and deploys a GraphQL API.\n 'graphql:deploy': deny,\n 'graphql:list': allow,\n // --api loads GraphQL definitions from the local project; explicit project/dataset flags are safe.\n 'graphql:undeploy': conditionalDenyFlags('api'),\n\n 'hooks:attempt': allow,\n 'hooks:create': allow,\n 'hooks:delete': allow,\n 'hooks:list': allow,\n 'hooks:logs': allow,\n\n // Creates or modifies a local project and may install dependencies.\n init: deny,\n\n // Installs packages into the local project.\n install: deny,\n\n // Opens a browser on the machine running the MCP server.\n learn: deny,\n\n // Performs an authentication flow.\n login: deny,\n\n // Performs an authentication operation and clears local credentials.\n logout: deny,\n\n // Reads local project configuration and opens a browser.\n manage: deny,\n\n // Loads local Studio configuration and writes manifest files.\n 'manifest:extract': deny,\n\n // Reads and writes local MCP client configuration.\n 'mcp:configure': deny,\n\n // Writes an aspect definition to the local filesystem.\n 'media:create-aspect': deny,\n 'media:delete-aspect': allow,\n // Reads a local aspect definition before deploying it.\n 'media:deploy-aspect': deny,\n // Writes media assets to the local filesystem.\n 'media:export': deny,\n // A directory or archive source reads media assets from the local\n // filesystem. A URL source does not: Sanity fetches the asset itself, so the\n // only local input is the URL. `--replace-aspects` is meaningless for a URL\n // source (the command rejects the combination), so it is hidden here rather\n // than advertised as usable surface.\n 'media:import': conditionalPolicy({\n deniedFlags: ['replace-aspects'],\n validate: ({args}) => typeof args.source === 'string' && isRemoteAssetSource(args.source),\n }),\n // media:pull-aspects and media:prune-aspect-data are hidden while the\n // config app is in beta, so they take the deny-by-default path like any\n // unlisted command. The exhaustiveness test forces explicit entries when\n // they are unhidden.\n\n // Creates migration source files in the local project.\n 'migrations:create': deny,\n // Reads and loads migration definitions from the local project.\n 'migrations:list': deny,\n // Executes local migration code that may perform arbitrary document mutations.\n 'migrations:run': deny,\n\n // Writes project setup values and scaffolds local applications.\n new: deny,\n\n // --web opens a browser on the machine running the MCP server.\n 'openapi:get': conditionalDenyFlags('web'),\n 'openapi:list': conditionalDenyFlags('web'),\n\n 'organizations:create': allow,\n 'organizations:delete': allow,\n 'organizations:get': allow,\n 'organizations:list': allow,\n 'organizations:update': allow,\n\n // Serves a local production build.\n preview: deny,\n\n 'projects:create': allow,\n 'projects:list': allow,\n // Reads locally stored claim URLs and robot tokens.\n 'projects:unclaimed': deny,\n\n // Loads the local Studio configuration to resolve the datasets containing each schema.\n 'schemas:delete': deny,\n // Loads local schema files before deploying them.\n 'schemas:deploy': deny,\n // Loads local Studio configuration and writes an extracted schema to disk.\n 'schemas:extract': deny,\n // Requires a local project and loads its schema configuration.\n 'schemas:list': deny,\n // Loads and executes a local Studio schema.\n 'schemas:validate': deny,\n\n // Installs skills into local editor configuration directories.\n 'skills:install': deny,\n\n // Changes account telemetry preferences and mutates local cached configuration.\n 'telemetry:disable': deny,\n // Changes account telemetry preferences and mutates local cached configuration.\n 'telemetry:enable': deny,\n 'telemetry:status': allow,\n\n // Creates authentication credentials.\n 'tokens:create': deny,\n // Deletes authentication credentials.\n 'tokens:delete': deny,\n // Exposes authentication credential metadata.\n 'tokens:list': deny,\n // Replaces authentication credentials and exposes the new secret.\n 'tokens:rotate': deny,\n\n // Reads the local schema and source files and writes generated types to the project.\n 'typegen:generate': deny,\n\n // Loads local CLI and workbench configuration to identify the deployed Studio or application.\n undeploy: deny,\n\n // Grants a user access to a project.\n 'users:invite': deny,\n 'users:list': allow,\n\n // Reads the local project and installed package tree.\n versions: deny,\n\n // Reads the authenticated user from the API using the execution context token.\n whoami: allow,\n}\n"],"names":["allow","conditionalDenyFlags","conditionalPolicy","deny","isRemoteAssetSource","apiValidator","args","flags","fields","Array","isArray","field","fieldReadsFromHost","some","separatorIndex","indexOf","headers","header","headerOverridesAuthentication","name","slice","trim","toLowerCase","endpoint","URL","parse","urlEmbedsCredentials","username","password","mcpPolicy","api","deniedFlags","validate","length","build","codemod","debug","deploy","dev","doctor","exec","init","install","learn","login","logout","manage","source","new","preview","undeploy","versions","whoami"],"mappings":"AAAA,SACEA,KAAK,EAELC,oBAAoB,EACpBC,iBAAiB,EACjBC,IAAI,QACC,iCAAgC;AAEvC,SAAQC,mBAAmB,QAAO,uCAAsC;AAExE,SAASC,aAAa,EACpBC,IAAI,EACJC,KAAK,EAIN;IACC,MAAMC,SAAoBC,MAAMC,OAAO,CAACH,MAAMI,KAAK,IAAIJ,MAAMI,KAAK,GAAG,EAAE;IAEvE,MAAMC,qBAAqBJ,OAAOK,IAAI,CAAC,CAACF;QACtC,IAAI,OAAOA,UAAU,UAAU,OAAO;QACtC,MAAMG,iBAAiBH,MAAMI,OAAO,CAAC;QACrC,OAAOD,iBAAiB,KAAKH,KAAK,CAACG,iBAAiB,EAAE,KAAK;IAC7D;IACA,IAAIF,oBAAoB,OAAO;IAE/B,MAAMI,UAAqBP,MAAMC,OAAO,CAACH,MAAMU,MAAM,IAAIV,MAAMU,MAAM,GAAG,EAAE;IAE1E,MAAMC,gCAAgCF,QAAQH,IAAI,CAAC,CAACI;QAClD,IAAI,OAAOA,WAAW,UAAU,OAAO;QACvC,MAAMH,iBAAiBG,OAAOF,OAAO,CAAC;QACtC,IAAID,kBAAkB,GAAG,OAAO;QAEhC,MAAMK,OAAOF,OAAOG,KAAK,CAAC,GAAGN,gBAAgBO,IAAI,GAAGC,WAAW;QAC/D,OAAOH,SAAS,mBAAmBA,SAAS;IAC9C;IAEA,IAAID,+BAA+B,OAAO;IAE1C,MAAMK,WAAW,OAAOjB,KAAKiB,QAAQ,KAAK,WAAWC,IAAIC,KAAK,CAACnB,KAAKiB,QAAQ,IAAI;IAChF,MAAMG,uBACJH,aAAa,QAASA,CAAAA,SAASI,QAAQ,KAAK,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,EAAC1B,IAAI,EAAC,GAAK,OAAOA,KAAKyC,MAAM,KAAK,YAAY3C,oBAAoBE,KAAKyC,MAAM;IAC1F;IACA,sEAAsE;IACtE,wEAAwE;IACxE,yEAAyE;IACzE,qBAAqB;IAErB,uDAAuD;IACvD,qBAAqB5C;IACrB,gEAAgE;IAChE,mBAAmBA;IACnB,+EAA+E;IAC/E,kBAAkBA;IAElB,gEAAgE;IAChE6C,KAAK7C;IAEL,+DAA+D;IAC/D,eAAeF,qBAAqB;IACpC,gBAAgBA,qBAAqB;IAErC,wBAAwBD;IACxB,wBAAwBA;IACxB,qBAAqBA;IACrB,sBAAsBA;IACtB,wBAAwBA;IAExB,mCAAmC;IACnCiD,SAAS9C;IAET,mBAAmBH;IACnB,iBAAiBA;IACjB,oDAAoD;IACpD,sBAAsBG;IAEtB,uFAAuF;IACvF,kBAAkBA;IAClB,kDAAkD;IAClD,kBAAkBA;IAClB,2EAA2E;IAC3E,mBAAmBA;IACnB,+DAA+D;IAC/D,gBAAgBA;IAChB,4CAA4C;IAC5C,oBAAoBA;IAEpB,+DAA+D;IAC/D,kBAAkBA;IAElB,gFAAgF;IAChF,qBAAqBA;IACrB,gFAAgF;IAChF,oBAAoBA;IACpB,oBAAoBH;IAEpB,sCAAsC;IACtC,iBAAiBG;IACjB,sCAAsC;IACtC,iBAAiBA;IACjB,8CAA8C;IAC9C,eAAeA;IACf,kEAAkE;IAClE,iBAAiBA;IAEjB,qFAAqF;IACrF,oBAAoBA;IAEpB,8FAA8F;IAC9F+C,UAAU/C;IAEV,qCAAqC;IACrC,gBAAgBA;IAChB,cAAcH;IAEd,sDAAsD;IACtDmD,UAAUhD;IAEV,+EAA+E;IAC/EiD,QAAQpD;AACV,EAAC"}
|
package/dist/services/context.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { getGlobalCliClient } from '@sanity/cli-core';
|
|
2
2
|
const CONTEXT_API_VERSION = 'v2026-08-25';
|
|
3
3
|
/**
|
|
4
|
-
* Knowledge-base-scoped methods on `client.context`
|
|
5
|
-
*
|
|
4
|
+
* Knowledge-base-scoped methods on `client.context` operate on the client's
|
|
5
|
+
* configured `resource`; collection-level methods
|
|
6
6
|
* (`client.context.knowledgeBases.*`) are addressed per call and need none.
|
|
7
|
-
|
|
7
|
+
* GROQ-backed reads also need `context.organizationId`.
|
|
8
|
+
*/ async function getContextClient(knowledgeBaseId, organizationId) {
|
|
8
9
|
return getGlobalCliClient({
|
|
9
10
|
apiVersion: CONTEXT_API_VERSION,
|
|
10
11
|
requireUser: true,
|
|
@@ -13,9 +14,22 @@ const CONTEXT_API_VERSION = 'v2026-08-25';
|
|
|
13
14
|
id: knowledgeBaseId,
|
|
14
15
|
type: 'knowledge-base'
|
|
15
16
|
}
|
|
17
|
+
},
|
|
18
|
+
...organizationId === undefined ? {} : {
|
|
19
|
+
context: {
|
|
20
|
+
organizationId
|
|
21
|
+
}
|
|
16
22
|
}
|
|
17
23
|
});
|
|
18
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* GROQ-backed reads (issues, entries) query the organization's
|
|
27
|
+
* document store, where documents are keyed by the knowledge base's public ID.
|
|
28
|
+
* Fetching the knowledge base resolves both, whichever of its IDs was passed.
|
|
29
|
+
*/ async function getContextStoreClient(knowledgeBaseId) {
|
|
30
|
+
const { organizationId, publicId } = await getKnowledgeBase(knowledgeBaseId);
|
|
31
|
+
return getContextClient(publicId, organizationId);
|
|
32
|
+
}
|
|
19
33
|
/**
|
|
20
34
|
* Create a knowledge base in an organization
|
|
21
35
|
*/ export async function createKnowledgeBase(params) {
|
|
@@ -128,5 +142,84 @@ const CONTEXT_API_VERSION = 'v2026-08-25';
|
|
|
128
142
|
jobId
|
|
129
143
|
});
|
|
130
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* List all issues for a knowledge base, optionally narrowed to one status
|
|
147
|
+
*/ export async function listIssues(knowledgeBaseId, status) {
|
|
148
|
+
const client = await getContextStoreClient(knowledgeBaseId);
|
|
149
|
+
return client.context.issues.list({
|
|
150
|
+
status
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get a single issue by ID, or `null` when it does not exist
|
|
155
|
+
*/ export async function getIssue(knowledgeBaseId, issueId) {
|
|
156
|
+
const client = await getContextStoreClient(knowledgeBaseId);
|
|
157
|
+
return client.context.issues.get({
|
|
158
|
+
issueId
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Resolve a conflict issue by choosing one of its sides (by index). Choosing
|
|
163
|
+
* a side other than the entry's current content starts a rewrite job.
|
|
164
|
+
*/ export async function resolveIssue(knowledgeBaseId, issueId, resolution) {
|
|
165
|
+
const client = await getContextClient(knowledgeBaseId);
|
|
166
|
+
return client.context.issues.resolve({
|
|
167
|
+
issueId,
|
|
168
|
+
resolution
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Dismiss an issue
|
|
173
|
+
*/ export async function dismissIssue(knowledgeBaseId, issueId) {
|
|
174
|
+
const client = await getContextClient(knowledgeBaseId);
|
|
175
|
+
return client.context.issues.dismiss({
|
|
176
|
+
issueId
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Reopen a resolved conflict issue
|
|
181
|
+
*/ export async function reopenIssue(knowledgeBaseId, issueId) {
|
|
182
|
+
const client = await getContextClient(knowledgeBaseId);
|
|
183
|
+
return client.context.issues.reopen({
|
|
184
|
+
issueId
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Apply accepted issues to a knowledge base in one batch
|
|
189
|
+
*/ export async function applyIssues(knowledgeBaseId, issueIds) {
|
|
190
|
+
const client = await getContextClient(knowledgeBaseId);
|
|
191
|
+
return client.context.issues.apply({
|
|
192
|
+
issueIds
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* List all entries for a knowledge base, path-ordered, without bodies
|
|
197
|
+
*/ export async function listEntries(knowledgeBaseId) {
|
|
198
|
+
const client = await getContextStoreClient(knowledgeBaseId);
|
|
199
|
+
return client.context.entries.list();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Get a single entry with its body and citations by outline path, or `null`
|
|
203
|
+
* when no entry sits at that path
|
|
204
|
+
*/ export async function getEntry(knowledgeBaseId, path) {
|
|
205
|
+
const client = await getContextStoreClient(knowledgeBaseId);
|
|
206
|
+
return client.context.entries.get({
|
|
207
|
+
path
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Rebuild one entry from its sources by outline path
|
|
212
|
+
*/ export async function rebuildEntry(knowledgeBaseId, path) {
|
|
213
|
+
const client = await getContextClient(knowledgeBaseId);
|
|
214
|
+
return client.context.entries.rebuild({
|
|
215
|
+
path
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Run a GROQ query against an organization's Context documents
|
|
220
|
+
*/ export async function queryContext(organizationId, query, params) {
|
|
221
|
+
const client = await getContextClient(undefined, organizationId);
|
|
222
|
+
return client.context.fetch(query, params);
|
|
223
|
+
}
|
|
131
224
|
|
|
132
225
|
//# sourceMappingURL=context.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/services/context.ts"],"sourcesContent":["import {getGlobalCliClient} from '@sanity/cli-core'\nimport {type Context} from '@sanity/client'\n\nconst CONTEXT_API_VERSION = 'v2026-08-25'\n\n/**\n * Knowledge-base-scoped methods on `client.context` (imports, builds, jobs)\n * operate on the client's configured `resource`; collection-level methods\n * (`client.context.knowledgeBases.*`) are addressed per call and need none.\n */\nasync function getContextClient(knowledgeBaseId?: string) {\n return getGlobalCliClient({\n apiVersion: CONTEXT_API_VERSION,\n requireUser: true,\n ...(knowledgeBaseId === undefined\n ? {}\n : {resource: {id: knowledgeBaseId, type: 'knowledge-base' as const}}),\n })\n}\n\n/**\n * Create a knowledge base in an organization\n */\nexport async function createKnowledgeBase(\n params: Context.CreateKnowledgeBaseParams,\n): Promise<Context.KnowledgeBase> {\n const client = await getContextClient()\n return client.context.knowledgeBases.create(params)\n}\n\n/**\n * List all knowledge bases in an organization (drains pagination)\n */\nexport async function listKnowledgeBases(organizationId: string): Promise<Context.KnowledgeBase[]> {\n const client = await getContextClient()\n\n const knowledgeBases: Context.KnowledgeBase[] = []\n let cursor: string | undefined\n do {\n const page = await client.context.knowledgeBases.list({cursor, organizationId})\n knowledgeBases.push(...page.data)\n cursor = page.nextCursor ?? undefined\n } while (cursor !== undefined)\n\n return knowledgeBases\n}\n\n/**\n * Get a single knowledge base by ID\n */\nexport async function getKnowledgeBase(knowledgeBaseId: string): Promise<Context.KnowledgeBase> {\n const client = await getContextClient()\n return client.context.knowledgeBases.get(knowledgeBaseId)\n}\n\n/**\n * Update a knowledge base's configuration\n */\nexport async function updateKnowledgeBase(\n knowledgeBaseId: string,\n params: Context.EditKnowledgeBaseParams,\n): Promise<Context.KnowledgeBase> {\n const client = await getContextClient()\n return client.context.knowledgeBases.edit(knowledgeBaseId, params)\n}\n\n/**\n * Delete a knowledge base and its generated content\n */\nexport async function deleteKnowledgeBase(knowledgeBaseId: string): Promise<void> {\n const client = await getContextClient()\n await client.context.knowledgeBases.delete(knowledgeBaseId)\n}\n\n/**\n * Import content into a knowledge base. File imports are staged and uploaded\n * to signed storage by the client.\n */\nexport async function createImport(\n knowledgeBaseId: string,\n params: Context.CreateFileImportParams | Context.CreateImportParams,\n): Promise<Context.JobAccepted> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.imports.create(params)\n}\n\n/**\n * List all imports for a knowledge base (drains pagination)\n */\nexport async function listImports(knowledgeBaseId: string): Promise<Context.Import[]> {\n const client = await getContextClient(knowledgeBaseId)\n\n const imports: Context.Import[] = []\n let cursor: string | undefined\n do {\n const page = await client.context.imports.list({cursor})\n imports.push(...page.data)\n cursor = page.nextCursor ?? undefined\n } while (cursor !== undefined)\n\n return imports\n}\n\n/**\n * Get a single import by ID\n */\nexport async function getImport(\n knowledgeBaseId: string,\n importId: string,\n): Promise<Context.ImportDetail> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.imports.get({importId})\n}\n\n/**\n * Get a short-lived signed URL for an import's original uploaded bytes\n */\nexport async function downloadImport(\n knowledgeBaseId: string,\n importId: string,\n): Promise<Context.ImportDownloadResponse> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.imports.download({importId})\n}\n\n/**\n * Delete an import from a knowledge base\n */\nexport async function deleteImport(knowledgeBaseId: string, importId: string): Promise<void> {\n const client = await getContextClient(knowledgeBaseId)\n await client.context.imports.delete({importId})\n}\n\n/**\n * Start a build of a knowledge base\n */\nexport async function buildKnowledgeBase(knowledgeBaseId: string): Promise<Context.JobAccepted> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.build()\n}\n\n/**\n * Cancel the running build of a knowledge base, if any\n */\nexport async function cancelKnowledgeBaseBuild(\n knowledgeBaseId: string,\n): Promise<{cancelled: boolean}> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.cancelBuild()\n}\n\n/**\n * Run an incremental refresh: re-check sources and apply what changed\n */\nexport async function refreshKnowledgeBase(\n knowledgeBaseId: string,\n): Promise<{jobId: string; started: boolean}> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.refresh()\n}\n\n/**\n * Get an async job (build, import) by ID\n */\nexport async function getJob(knowledgeBaseId: string, jobId: string): Promise<Context.Job> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.jobs.get({jobId})\n}\n"],"names":["getGlobalCliClient","CONTEXT_API_VERSION","getContextClient","knowledgeBaseId","apiVersion","requireUser","undefined","resource","id","type","createKnowledgeBase","params","client","context","knowledgeBases","create","listKnowledgeBases","organizationId","cursor","page","list","push","data","nextCursor","getKnowledgeBase","get","updateKnowledgeBase","edit","deleteKnowledgeBase","delete","createImport","imports","listImports","getImport","importId","downloadImport","download","deleteImport","buildKnowledgeBase","build","cancelKnowledgeBaseBuild","cancelBuild","refreshKnowled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;IAEvB,MAAMS,SAAS,MAAMV,iBAAiBC;IACtC,OAAOS,OAAOC,OAAO,CAAC4B,WAAW;AACnC;AAEA;;CAEC,GACD,OAAO,eAAeC,qBACpBvC,eAAuB;IAEvB,MAAMS,SAAS,MAAMV,iBAAiBC;IACtC,OAAOS,OAAOC,OAAO,CAAC8B,OAAO;AAC/B;AAEA;;CAEC,GACD,OAAO,eAAeC,OAAOzC,eAAuB,EAAE0C,KAAa;IACjE,MAAMjC,SAAS,MAAMV,iBAAiBC;IACtC,OAAOS,OAAOC,OAAO,CAACiC,IAAI,CAACrB,GAAG,CAAC;QAACoB;IAAK;AACvC"}
|
|
1
|
+
{"version":3,"sources":["../../src/services/context.ts"],"sourcesContent":["import {getGlobalCliClient} from '@sanity/cli-core'\nimport {type Context} from '@sanity/client'\n\nconst CONTEXT_API_VERSION = 'v2026-08-25'\n\n/**\n * Knowledge-base-scoped methods on `client.context` operate on the client's\n * configured `resource`; collection-level methods\n * (`client.context.knowledgeBases.*`) are addressed per call and need none.\n * GROQ-backed reads also need `context.organizationId`.\n */\nasync function getContextClient(knowledgeBaseId?: string, organizationId?: string) {\n return getGlobalCliClient({\n apiVersion: CONTEXT_API_VERSION,\n requireUser: true,\n ...(knowledgeBaseId === undefined\n ? {}\n : {resource: {id: knowledgeBaseId, type: 'knowledge-base' as const}}),\n ...(organizationId === undefined ? {} : {context: {organizationId}}),\n })\n}\n\n/**\n * GROQ-backed reads (issues, entries) query the organization's\n * document store, where documents are keyed by the knowledge base's public ID.\n * Fetching the knowledge base resolves both, whichever of its IDs was passed.\n */\nasync function getContextStoreClient(knowledgeBaseId: string) {\n const {organizationId, publicId} = await getKnowledgeBase(knowledgeBaseId)\n return getContextClient(publicId, organizationId)\n}\n\n/**\n * Create a knowledge base in an organization\n */\nexport async function createKnowledgeBase(\n params: Context.CreateKnowledgeBaseParams,\n): Promise<Context.KnowledgeBase> {\n const client = await getContextClient()\n return client.context.knowledgeBases.create(params)\n}\n\n/**\n * List all knowledge bases in an organization (drains pagination)\n */\nexport async function listKnowledgeBases(organizationId: string): Promise<Context.KnowledgeBase[]> {\n const client = await getContextClient()\n\n const knowledgeBases: Context.KnowledgeBase[] = []\n let cursor: string | undefined\n do {\n const page = await client.context.knowledgeBases.list({cursor, organizationId})\n knowledgeBases.push(...page.data)\n cursor = page.nextCursor ?? undefined\n } while (cursor !== undefined)\n\n return knowledgeBases\n}\n\n/**\n * Get a single knowledge base by ID\n */\nexport async function getKnowledgeBase(knowledgeBaseId: string): Promise<Context.KnowledgeBase> {\n const client = await getContextClient()\n return client.context.knowledgeBases.get(knowledgeBaseId)\n}\n\n/**\n * Update a knowledge base's configuration\n */\nexport async function updateKnowledgeBase(\n knowledgeBaseId: string,\n params: Context.EditKnowledgeBaseParams,\n): Promise<Context.KnowledgeBase> {\n const client = await getContextClient()\n return client.context.knowledgeBases.edit(knowledgeBaseId, params)\n}\n\n/**\n * Delete a knowledge base and its generated content\n */\nexport async function deleteKnowledgeBase(knowledgeBaseId: string): Promise<void> {\n const client = await getContextClient()\n await client.context.knowledgeBases.delete(knowledgeBaseId)\n}\n\n/**\n * Import content into a knowledge base. File imports are staged and uploaded\n * to signed storage by the client.\n */\nexport async function createImport(\n knowledgeBaseId: string,\n params: Context.CreateFileImportParams | Context.CreateImportParams,\n): Promise<Context.JobAccepted> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.imports.create(params)\n}\n\n/**\n * List all imports for a knowledge base (drains pagination)\n */\nexport async function listImports(knowledgeBaseId: string): Promise<Context.Import[]> {\n const client = await getContextClient(knowledgeBaseId)\n\n const imports: Context.Import[] = []\n let cursor: string | undefined\n do {\n const page = await client.context.imports.list({cursor})\n imports.push(...page.data)\n cursor = page.nextCursor ?? undefined\n } while (cursor !== undefined)\n\n return imports\n}\n\n/**\n * Get a single import by ID\n */\nexport async function getImport(\n knowledgeBaseId: string,\n importId: string,\n): Promise<Context.ImportDetail> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.imports.get({importId})\n}\n\n/**\n * Get a short-lived signed URL for an import's original uploaded bytes\n */\nexport async function downloadImport(\n knowledgeBaseId: string,\n importId: string,\n): Promise<Context.ImportDownloadResponse> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.imports.download({importId})\n}\n\n/**\n * Delete an import from a knowledge base\n */\nexport async function deleteImport(knowledgeBaseId: string, importId: string): Promise<void> {\n const client = await getContextClient(knowledgeBaseId)\n await client.context.imports.delete({importId})\n}\n\n/**\n * Start a build of a knowledge base\n */\nexport async function buildKnowledgeBase(knowledgeBaseId: string): Promise<Context.JobAccepted> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.build()\n}\n\n/**\n * Cancel the running build of a knowledge base, if any\n */\nexport async function cancelKnowledgeBaseBuild(\n knowledgeBaseId: string,\n): Promise<{cancelled: boolean}> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.cancelBuild()\n}\n\n/**\n * Run an incremental refresh: re-check sources and apply what changed\n */\nexport async function refreshKnowledgeBase(\n knowledgeBaseId: string,\n): Promise<{jobId: string; started: boolean}> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.refresh()\n}\n\n/**\n * Get an async job (build, import) by ID\n */\nexport async function getJob(knowledgeBaseId: string, jobId: string): Promise<Context.Job> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.jobs.get({jobId})\n}\n\n/**\n * List all issues for a knowledge base, optionally narrowed to one status\n */\nexport async function listIssues(\n knowledgeBaseId: string,\n status?: Context.IssueDoc['status'],\n): Promise<Context.IssueDoc[]> {\n const client = await getContextStoreClient(knowledgeBaseId)\n return client.context.issues.list({status})\n}\n\n/**\n * Get a single issue by ID, or `null` when it does not exist\n */\nexport async function getIssue(\n knowledgeBaseId: string,\n issueId: string,\n): Promise<Context.IssueDoc | null> {\n const client = await getContextStoreClient(knowledgeBaseId)\n return client.context.issues.get({issueId})\n}\n\n/**\n * Resolve a conflict issue by choosing one of its sides (by index). Choosing\n * a side other than the entry's current content starts a rewrite job.\n */\nexport async function resolveIssue(\n knowledgeBaseId: string,\n issueId: string,\n resolution: number,\n): Promise<Context.ResolveIssueResponse> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.issues.resolve({issueId, resolution})\n}\n\n/**\n * Dismiss an issue\n */\nexport async function dismissIssue(\n knowledgeBaseId: string,\n issueId: string,\n): Promise<Context.DismissIssueResponse> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.issues.dismiss({issueId})\n}\n\n/**\n * Reopen a resolved conflict issue\n */\nexport async function reopenIssue(\n knowledgeBaseId: string,\n issueId: string,\n): Promise<Context.ReopenIssueResponse> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.issues.reopen({issueId})\n}\n\n/**\n * Apply accepted issues to a knowledge base in one batch\n */\nexport async function applyIssues(\n knowledgeBaseId: string,\n issueIds: string[],\n): Promise<Context.ApplyIssuesResponse> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.issues.apply({issueIds})\n}\n\n/**\n * List all entries for a knowledge base, path-ordered, without bodies\n */\nexport async function listEntries(knowledgeBaseId: string): Promise<Context.Entry[]> {\n const client = await getContextStoreClient(knowledgeBaseId)\n return client.context.entries.list()\n}\n\n/**\n * Get a single entry with its body and citations by outline path, or `null`\n * when no entry sits at that path\n */\nexport async function getEntry(\n knowledgeBaseId: string,\n path: string,\n): Promise<Context.EntryDoc | null> {\n const client = await getContextStoreClient(knowledgeBaseId)\n return client.context.entries.get({path})\n}\n\n/**\n * Rebuild one entry from its sources by outline path\n */\nexport async function rebuildEntry(\n knowledgeBaseId: string,\n path: string,\n): Promise<Context.RebuildEntryResponse> {\n const client = await getContextClient(knowledgeBaseId)\n return client.context.entries.rebuild({path})\n}\n\n/**\n * Run a GROQ query against an organization's Context documents\n */\nexport async function queryContext(\n organizationId: string,\n query: string,\n params?: Record<string, unknown>,\n): Promise<unknown> {\n const client = await getContextClient(undefined, organizationId)\n return client.context.fetch(query, params)\n}\n"],"names":["getGlobalCliClient","CONTEXT_API_VERSION","getContextClient","knowledgeBaseId","organizationId","apiVersion","requireUser","undefined","resource","id","type","context","getContextStoreClient","publicId","getKnowledgeBase","createKnowledgeBase","params","client","knowledgeBases","create","listKnowledgeBases","cursor","page","list","push","data","nextCursor","get","updateKnowledgeBase","edit","deleteKnowledgeBase","delete","createImport","imports","listImports","getImport","importId","downloadImport","download","deleteImport","buildKnowledgeBase","build","cancelKnowledgeBaseBuild","cancelBuild","refreshKnowledgeBase","refresh","getJob","jobId","jobs","listIssues","status","issues","getIssue","issueId","resolveIssue","resolution","resolve","dismissIssue","dismiss","reopenIssue","reopen","applyIssues","issueIds","apply","listEntries","entries","getEntry","path","rebuildEntry","rebuild","queryContext","query","fetch"],"mappings":"AAAA,SAAQA,kBAAkB,QAAO,mBAAkB;AAGnD,MAAMC,sBAAsB;AAE5B;;;;;CAKC,GACD,eAAeC,iBAAiBC,eAAwB,EAAEC,cAAuB;IAC/E,OAAOJ,mBAAmB;QACxBK,YAAYJ;QACZK,aAAa;QACb,GAAIH,oBAAoBI,YACpB,CAAC,IACD;YAACC,UAAU;gBAACC,IAAIN;gBAAiBO,MAAM;YAAyB;QAAC,CAAC;QACtE,GAAIN,mBAAmBG,YAAY,CAAC,IAAI;YAACI,SAAS;gBAACP;YAAc;QAAC,CAAC;IACrE;AACF;AAEA;;;;CAIC,GACD,eAAeQ,sBAAsBT,eAAuB;IAC1D,MAAM,EAACC,cAAc,EAAES,QAAQ,EAAC,GAAG,MAAMC,iBAAiBX;IAC1D,OAAOD,iBAAiBW,UAAUT;AACpC;AAEA;;CAEC,GACD,OAAO,eAAeW,oBACpBC,MAAyC;IAEzC,MAAMC,SAAS,MAAMf;IACrB,OAAOe,OAAON,OAAO,CAACO,cAAc,CAACC,MAAM,CAACH;AAC9C;AAEA;;CAEC,GACD,OAAO,eAAeI,mBAAmBhB,cAAsB;IAC7D,MAAMa,SAAS,MAAMf;IAErB,MAAMgB,iBAA0C,EAAE;IAClD,IAAIG;IACJ,GAAG;QACD,MAAMC,OAAO,MAAML,OAAON,OAA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IAEZ,MAAMlD,SAAS,MAAMf,iBAAiBC;IACtC,OAAOc,OAAON,OAAO,CAACsD,OAAO,CAACI,OAAO,CAAC;QAACF;IAAI;AAC7C;AAEA;;CAEC,GACD,OAAO,eAAeG,aACpBlE,cAAsB,EACtBmE,KAAa,EACbvD,MAAgC;IAEhC,MAAMC,SAAS,MAAMf,iBAAiBK,WAAWH;IACjD,OAAOa,OAAON,OAAO,CAAC6D,KAAK,CAACD,OAAOvD;AACrC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getGlobalCliClient } from '@sanity/cli-core';
|
|
2
2
|
import { MEDIA_LIBRARY_ASSET_ASPECT_TYPE_NAME } from '@sanity/types';
|
|
3
|
+
import { VALID_ASPECT_NAME } from '../actions/media/aspectName.js';
|
|
3
4
|
import { ASSET_UPLOAD_REQUEST_TAG_PREFIX, URL_INGEST_TIMEOUT_MS } from './assets.js';
|
|
4
5
|
export const MEDIA_LIBRARY_API_VERSION = 'v2025-02-19';
|
|
5
6
|
async function getMediaLibraryClient(options = {}) {
|
|
@@ -39,22 +40,188 @@ async function getMediaLibraryClient(options = {}) {
|
|
|
39
40
|
url: `/media-libraries/${mediaLibraryId}/mutate`
|
|
40
41
|
});
|
|
41
42
|
}
|
|
43
|
+
/** One listing endpoint, scoped by either owner — only the query param differs. */ async function listActiveMediaLibraries(query) {
|
|
44
|
+
const client = await getMediaLibraryClient();
|
|
45
|
+
const response = await client.request({
|
|
46
|
+
method: 'GET',
|
|
47
|
+
query,
|
|
48
|
+
url: `/media-libraries`
|
|
49
|
+
});
|
|
50
|
+
return response.data.filter((library)=>library.status === 'active');
|
|
51
|
+
}
|
|
42
52
|
/**
|
|
43
|
-
* Get a list of media libraries for a project
|
|
53
|
+
* Get a list of active media libraries for a project
|
|
44
54
|
* @param projectId - The project ID
|
|
45
55
|
* @returns A promise that resolves to the media libraries
|
|
46
56
|
*
|
|
47
57
|
* @internal
|
|
48
|
-
*/ export
|
|
58
|
+
*/ export function getMediaLibraries(projectId) {
|
|
59
|
+
return listActiveMediaLibraries({
|
|
60
|
+
projectId
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get a list of active media libraries for an organization
|
|
65
|
+
* @param organizationId - The organization ID
|
|
66
|
+
* @returns A promise that resolves to the active media libraries
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/ export function getMediaLibrariesForOrganization(organizationId) {
|
|
70
|
+
return listActiveMediaLibraries({
|
|
71
|
+
organizationId
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/** Query page size — see the cursor loop in `queryAllPages`. */ const QUERY_PAGE_SIZE = 1000;
|
|
75
|
+
/**
|
|
76
|
+
* Run a media-library GROQ query to exhaustion, paging on an `_id` cursor:
|
|
77
|
+
* the query API caps single-response sizes, and a truncated read would
|
|
78
|
+
* silently drop documents. Only an *empty* page ends the loop — a short page
|
|
79
|
+
* is not trusted as exhaustion, in case the endpoint ever returns fewer rows
|
|
80
|
+
* than the requested slice; the cursor advances past the last returned id
|
|
81
|
+
* either way, so partial slices just mean extra round-trips.
|
|
82
|
+
*/ async function queryAllPages(options) {
|
|
83
|
+
const { filter, mediaLibraryId, params, perspective = 'published', projection = '', tag } = options;
|
|
49
84
|
const client = await getMediaLibraryClient();
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
85
|
+
const documents = [];
|
|
86
|
+
let lastId = '';
|
|
87
|
+
for(;;){
|
|
88
|
+
const response = await client.request({
|
|
89
|
+
body: {
|
|
90
|
+
params: {
|
|
91
|
+
...params,
|
|
92
|
+
lastId
|
|
93
|
+
},
|
|
94
|
+
query: `*[${filter} && _id > $lastId] | order(_id) [0...${QUERY_PAGE_SIZE}]${projection}`
|
|
95
|
+
},
|
|
96
|
+
method: 'POST',
|
|
97
|
+
// `published` is this API version's default; passed explicitly so a
|
|
98
|
+
// future default change can never silently surface `drafts.*`
|
|
99
|
+
// duplicates of published documents.
|
|
100
|
+
query: {
|
|
101
|
+
perspective
|
|
102
|
+
},
|
|
103
|
+
tag,
|
|
104
|
+
url: `/media-libraries/${mediaLibraryId}/query`
|
|
105
|
+
});
|
|
106
|
+
const page = response?.result ?? [];
|
|
107
|
+
documents.push(...page);
|
|
108
|
+
const last = page.at(-1);
|
|
109
|
+
if (!last) break;
|
|
110
|
+
lastId = last._id;
|
|
111
|
+
}
|
|
112
|
+
return documents;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Fetch the stored aspect documents of a media library
|
|
116
|
+
* @param mediaLibraryId - The media library ID
|
|
117
|
+
* @returns A promise that resolves to the aspect documents, ordered by id
|
|
118
|
+
*
|
|
119
|
+
* @internal
|
|
120
|
+
*/ export function getAspects(mediaLibraryId) {
|
|
121
|
+
// An incomplete read here is destructive downstream: a pull would generate
|
|
122
|
+
// a config missing the dropped aspects, and its deploy deletes them.
|
|
123
|
+
return queryAllPages({
|
|
124
|
+
filter: '_type == $type',
|
|
125
|
+
mediaLibraryId,
|
|
126
|
+
params: {
|
|
127
|
+
type: MEDIA_LIBRARY_ASSET_ASPECT_TYPE_NAME
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Fetch every stored aspect document `_id` of a media library. The consumer
|
|
133
|
+
* acts destructively on the result (mirror deletes), where a silently
|
|
134
|
+
* truncated list means reporting success while stale documents are left
|
|
135
|
+
* behind — `queryAllPages` reads to exhaustion.
|
|
136
|
+
*
|
|
137
|
+
* The `published` perspective (the helper's default) matters doubly here:
|
|
138
|
+
* stale detection must never see draft ids. The sync's upserts write
|
|
139
|
+
* published documents and its deletes match ids exactly, so drafts (not
|
|
140
|
+
* normally used for aspect docs) are out of scope either way — a
|
|
141
|
+
* default-perspective read could flag a `drafts.*` id as stale and target it
|
|
142
|
+
* for deletion.
|
|
143
|
+
*
|
|
144
|
+
* `tag` names the calling operation in request logs; it defaults to the
|
|
145
|
+
* deploy-time sync, the original consumer.
|
|
146
|
+
*
|
|
147
|
+
* @internal
|
|
148
|
+
*/ export async function getStoredAspectIds(mediaLibraryId, tag = 'aspect-sync') {
|
|
149
|
+
const documents = await queryAllPages({
|
|
150
|
+
filter: '_type == $type',
|
|
151
|
+
mediaLibraryId,
|
|
152
|
+
params: {
|
|
153
|
+
type: MEDIA_LIBRARY_ASSET_ASPECT_TYPE_NAME
|
|
54
154
|
},
|
|
55
|
-
|
|
155
|
+
projection: '{_id}',
|
|
156
|
+
tag
|
|
157
|
+
});
|
|
158
|
+
return documents.map((doc)=>doc._id);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Fetch the `_id` of every asset document carrying a value under
|
|
162
|
+
* `aspects.<aspectName>`.
|
|
163
|
+
*
|
|
164
|
+
* Queried under the `raw` perspective so drafts are included alongside
|
|
165
|
+
* published documents — a prune that missed drafts would resurrect the data
|
|
166
|
+
* on publish.
|
|
167
|
+
*
|
|
168
|
+
* @internal
|
|
169
|
+
*/ export async function getAspectDataCarrierIds(mediaLibraryId, aspectName) {
|
|
170
|
+
// Defense in depth: callers validate the name against the aspect-name rule,
|
|
171
|
+
// but this string is interpolated into GROQ, so re-guard here.
|
|
172
|
+
if (!VALID_ASPECT_NAME.test(aspectName)) {
|
|
173
|
+
throw new Error(`"${aspectName}" is not a valid aspect name`);
|
|
174
|
+
}
|
|
175
|
+
const documents = await queryAllPages({
|
|
176
|
+
filter: `_type == "sanity.asset" && defined(aspects["${aspectName}"])`,
|
|
177
|
+
mediaLibraryId,
|
|
178
|
+
params: {},
|
|
179
|
+
perspective: 'raw',
|
|
180
|
+
projection: '{_id}',
|
|
181
|
+
tag: 'prune-aspect-data'
|
|
182
|
+
});
|
|
183
|
+
return documents.map((doc)=>doc._id);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Remove `aspects.<aspectName>` from the given asset documents in one mutate
|
|
187
|
+
* request. Irreversible — this deletes aspect data, not a definition.
|
|
188
|
+
*
|
|
189
|
+
* @internal
|
|
190
|
+
*/ export async function unsetAspectData(options) {
|
|
191
|
+
const { aspectName, assetIds, mediaLibraryId } = options;
|
|
192
|
+
const client = await getMediaLibraryClient();
|
|
193
|
+
await client.request({
|
|
194
|
+
body: {
|
|
195
|
+
mutations: assetIds.map((id)=>({
|
|
196
|
+
patch: {
|
|
197
|
+
id,
|
|
198
|
+
unset: [
|
|
199
|
+
`aspects.${aspectName}`
|
|
200
|
+
]
|
|
201
|
+
}
|
|
202
|
+
}))
|
|
203
|
+
},
|
|
204
|
+
method: 'POST',
|
|
205
|
+
tag: 'prune-aspect-data',
|
|
206
|
+
url: `/media-libraries/${mediaLibraryId}/mutate`
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Apply one aspect-sync transaction — the upsert/delete mutation list built by
|
|
211
|
+
* `buildSyncMutations` — to a media library in a single request.
|
|
212
|
+
*
|
|
213
|
+
* @internal
|
|
214
|
+
*/ export async function mutateAspects(options) {
|
|
215
|
+
const { mediaLibraryId, mutations } = options;
|
|
216
|
+
const client = await getMediaLibraryClient();
|
|
217
|
+
return client.request({
|
|
218
|
+
body: {
|
|
219
|
+
mutations
|
|
220
|
+
},
|
|
221
|
+
method: 'POST',
|
|
222
|
+
tag: 'aspect-sync',
|
|
223
|
+
url: `/media-libraries/${mediaLibraryId}/mutate`
|
|
56
224
|
});
|
|
57
|
-
return response.data.filter((library)=>library.status === 'active');
|
|
58
225
|
}
|
|
59
226
|
/**
|
|
60
227
|
* Deploy one or more aspects to a media library
|