🎥 Make videos programmatically with React
`@remotion/studio`: Add skill installation to settings
JonnyBurger423deb1aacd9
diff --git a/packages/example/e2e/studio.test.mts b/packages/example/e2e/studio.test.mtsindex 3ee98f7732..5544e143e1 100644--- a/packages/example/e2e/studio.test.mts+++ b/packages/example/e2e/studio.test.mts@@ -1676,6 +1676,91 @@ test.describe('visual mode', () => {}});+ test('installs skills from settings and keeps progress when reopened', async ({+ page,+ }) => {+ const skillsInfo = {+ remotionUpgradeSkillAvailable: false,+ remotionInteractivitySkillAvailable: false,+ skills: ['remotion-captions', 'remotion-create'].map((name) => ({+ name,+ installedInProject: false,+ installedGlobally: false,+ })),+ };+ await page.route('**/api/remotion-skills-info', (route) =>+ route.fulfill({json: {success: true, data: skillsInfo}}),+ );+ // The server integration test covers the real installer boundary and filesystem.+ let installAttempts = 0;+ let finishInstall: () => void = () => undefined;+ const installFinished = new Promise<void>((resolve) => {+ finishInstall = resolve;+ });+ await page.route('**/api/install-remotion-skill', async (route) => {+ expect(route.request().postDataJSON()).toEqual({+ skill: 'remotion-captions',+ });+ if (++installAttempts === 1) {+ await route.fulfill({+ json: {success: false, error: 'Could not download skills'},+ });+ return;+ }+ await installFinished;+ skillsInfo.skills[0].installedInProject = true;+ await route.fulfill({json: {success: true, data: skillsInfo}});+ });+ await page.goto(`${STUDIO_URL}/schema-test`);+ const openSkills = async () => {+ await page.getByRole('button', {name: /Search\.\.\./}).click();+ const dialog = page.getByRole('dialog');+ await dialog.getByRole('textbox').fill('> Settings');+ await dialog.getByText('Settings...', {exact: true}).click();+ await dialog.getByText('Skills', {exact: true}).click();+ };+ await openSkills();+ const dialog = page.getByRole('dialog');+ const captionsSkill = dialog+ .getByRole('listitem')+ .filter({hasText: '/remotion-captions'});+ const installButton = captionsSkill.getByRole('button', {+ name: 'Install',+ exact: true,+ });+ await installButton.click();+ await expect(+ dialog.getByText('Could not download skills', {exact: true}),+ ).toBeVisible();+ await installButton.click();+ await expect(+ captionsSkill.getByRole('button', {name: 'Installing...'}),+ ).toBeDisabled();+ await expect(+ dialog+ .getByRole('listitem')+ .filter({hasText: '/remotion-create'})+ .getByRole('button'),+ ).toBeDisabled();+ await page.keyboard.press('Escape');+ await openSkills();+ await expect(+ captionsSkill.getByRole('button', {name: 'Installing...'}),+ ).toBeDisabled();+ finishInstall();+ await expect(+ captionsSkill.getByText('Project', {exact: true}),+ ).toBeVisible();+ await expect(+ dialog.getByText('Could not download skills', {exact: true}),+ ).toBeHidden();+ await page.keyboard.press('Escape');+ await openSkills();+ await expect(+ captionsSkill.getByText('Project', {exact: true}),+ ).toBeVisible();+ });+test('settings reuse reactive runtime config and license toggle', async ({page,}) => {@@ -2018,7 +2103,7 @@ test.describe('visual mode', () => {await dialog.getByText('Skills', {exact: true}).click();await expect(dialog.getByText(- 'Not all skills are installed. Run this command in the project directory, then reload Studio and restart your coding agent.',+ 'Install skills in this project to use them with your coding agent.',{exact: true},),).toBeVisible();@@ -2034,13 +2119,7 @@ test.describe('visual mode', () => {await expect(dialog.getByText('Project', {exact: true})).toBeVisible();await expect(dialog.getByText('Global', {exact: true})).toBeVisible();await expect(- dialog.getByText('Not installed', {exact: true}).first(),- ).toBeVisible();- await expect(- dialog.getByText('npx remotion skills add', {exact: true}),- ).toBeVisible();- await expect(- dialog.getByRole('button', {name: 'Copy install command'}),+ dialog.getByRole('button', {name: 'Install', exact: true}).first(),).toBeVisible();const skillsList = dialog.getByRole('list', {name: 'Remotion Agent Skills',@@ -2062,7 +2141,7 @@ test.describe('visual mode', () => {).toBeGreaterThanOrEqual(16);await expect(dialog.getByText('Changes save to', {exact: false}),- ).toBeVisible();+ ).toBeHidden();await dialog.getByText('Apps', {exact: true}).click();await expect(diff --git a/packages/studio-server/src/preview-server/api-routes.ts b/packages/studio-server/src/preview-server/api-routes.tsindex f47df69391..f21cc62a55 100644--- a/packages/studio-server/src/preview-server/api-routes.ts+++ b/packages/studio-server/src/preview-server/api-routes.ts@@ -28,6 +28,7 @@ import {findInFileHandler} from './routes/find-in-file';import {insertElementHandler} from './routes/insert-element';import {insertJsxElementHandler} from './routes/insert-jsx-element';import {handleInstallPackage} from './routes/install-dependency';+import {installRemotionSkillHandler} from './routes/install-remotion-skill';import {logStudioErrorHandler} from './routes/log-studio-error';import {moveKeyframesHandler} from './routes/move-keyframes';import {openInEditorHandler} from './routes/open-in-editor';@@ -123,6 +124,7 @@ export const allApiRoutes: {'/api/update-available': handleUpdate,'/api/release-notes': getReleaseNotesHandler,'/api/remotion-skills-info': remotionSkillsInfoHandler,+ '/api/install-remotion-skill': installRemotionSkillHandler,'/api/project-info': projectInfoHandler,'/api/delete-static-file': deleteStaticFileHandler,'/api/rename-static-file': renameStaticFileHandler,diff --git a/packages/studio-server/src/preview-server/routes/install-remotion-skill.ts b/packages/studio-server/src/preview-server/routes/install-remotion-skill.tsnew file mode 100644index 0000000000..5889d3c501--- /dev/null+++ b/packages/studio-server/src/preview-server/routes/install-remotion-skill.ts@@ -0,0 +1,80 @@+import {spawn} from 'node:child_process';+import type {+ GetRemotionSkillsInfoResponse,+ InstallRemotionSkillRequest,+} from '@remotion/studio-shared';+import {getPackageManagerSpawnOptions} from '../../helpers/package-manager-spawn-options';+import {remotionSkillNames} from '../../remotion-skill-names';+import type {ApiHandler} from '../api-types';+import {getRemotionSkillsInfo} from './remotion-skills-info';++const installingInProjects = new Set<string>();++export const installRemotionSkillHandler: ApiHandler<+ InstallRemotionSkillRequest,+ GetRemotionSkillsInfoResponse+> = async ({remotionRoot, input: {skill}}) => {+ if (!remotionSkillNames.some((name) => name === skill)) {+ throw new Error(`Unknown Remotion skill: ${JSON.stringify(skill)}`);+ }++ if (installingInProjects.has(remotionRoot)) {+ throw new Error(+ 'A skill is already being installed. Please try again once it finishes.',+ );+ }++ installingInProjects.add(remotionRoot);+ try {+ await new Promise<void>((resolve, reject) => {+ const child = spawn(+ process.platform === 'win32' ? 'npx.cmd' : 'npx',+ [+ '--yes',+ '--loglevel=error',+ '[email protected]',+ 'add',+ 'remotion-dev/skills',+ '--skill',+ skill,+ '--yes',+ ],+ {+ cwd: remotionRoot,+ stdio: ['ignore', 'pipe', 'pipe'],+ ...getPackageManagerSpawnOptions(),+ },+ );+ let output = '';+ const onData = (data: Buffer) => {+ output = (output + data.toString()).slice(-8000);+ };++ child.stdout.on('data', onData);+ child.stderr.on('data', onData);+ child.on('error', reject);+ child.on('close', (code, signal) => {+ if (code === 0) {+ resolve();+ } else {+ reject(+ new Error(+ `Could not install ${skill} (exit code ${code}, signal ${signal}). ${output.trim()}`,+ ),+ );+ }+ });+ });++ const info = getRemotionSkillsInfo({remotionRoot});+ if (!info.skills.find(({name}) => name === skill)?.installedInProject) {+ throw new Error(+ `The installer finished, but ${skill} was not found in the project. Please try again.`,+ );+ }++ return info;+ } finally {+ installingInProjects.delete(remotionRoot);+ }+};diff --git a/packages/studio-server/src/test/install-remotion-skill.test.ts b/packages/studio-server/src/test/install-remotion-skill.test.tsnew file mode 100644index 0000000000..33d6cc57e9--- /dev/null+++ b/packages/studio-server/src/test/install-remotion-skill.test.ts@@ -0,0 +1,121 @@+import {expect, test} from 'bun:test';+import {chmod, mkdir, mkdtemp, readFile, rm, writeFile} from 'node:fs/promises';+import {createServer} from 'node:http';+import {tmpdir} from 'node:os';+import path from 'node:path';+import {allApiRoutes} from '../preview-server/api-routes';+import {handleRequest} from '../preview-server/handler';++// Replace only the external skills installer; exercise the real HTTP handler and filesystem.+test.skipIf(process.platform === 'win32')(+ 'Studio installs a skill in the project, reports failures, and allows retrying',+ async () => {+ const root = await mkdtemp(path.join(tmpdir(), 'studio-install-skill-'));+ const previousPath = process.env.PATH;+ const bin = path.join(root, 'bin');+ await mkdir(bin);+ const npx = path.join(bin, 'npx');+ await writeFile(+ npx,+ `#!${process.execPath}+const fs = require('node:fs');+const path = require('node:path');+const args = process.argv.slice(2);+fs.writeFileSync('install-call.json', JSON.stringify(args));+if (fs.existsSync('fail')) {+ process.stderr.write('Could not download skills');+ process.stdout.end();+ setTimeout(() => process.exit(1), 100);+} else {+ const skill = args[args.indexOf('--skill') + 1];+ const directory = path.join('.agents', 'skills', skill);+ fs.mkdirSync(directory, {recursive: true});+ fs.writeFileSync(path.join(directory, 'SKILL.md'), '# Installed skill');+}+`,+ );+ await chmod(npx, 0o755);+ process.env.PATH = `${bin}${path.delimiter}${previousPath}`;+ const server = createServer((request, response) => {+ handleRequest({+ request,+ response,+ handler: allApiRoutes['/api/install-remotion-skill'],+ remotionRoot: root,+ entryPoint: '',+ logLevel: 'error',+ publicDir: root,+ binariesDirectory: null,+ configFile: null,+ getDefaultCodingAgent: () => null,+ getDefaultEditor: () => null,+ methods: {+ addJob: () => undefined,+ cancelJob: () => undefined,+ removeJob: () => undefined,+ },+ }).catch(() => response.end());+ });+ try {+ await new Promise<void>((resolve) =>+ server.listen(0, '127.0.0.1', resolve),+ );+ const address = server.address();+ if (!address || typeof address === 'string') throw new Error('No port');+ const origin = `http://127.0.0.1:${address.port}`;+ const request = async (skill: string) => {+ const response = await fetch(`${origin}/api/install-remotion-skill`, {+ method: 'POST',+ headers: {origin, 'content-type': 'application/json'},+ body: JSON.stringify({skill}),+ });+ return response.json();+ };++ expect(await request('../untrusted-skill')).toMatchObject({+ success: false,+ });+ await expect(+ readFile(path.join(root, 'install-call.json')),+ ).rejects.toThrow();+ await writeFile(path.join(root, 'fail'), '');+ const failed = await request('remotion-interactivity');+ expect(failed.success).toBe(false);+ expect(failed.error).toContain('Could not download skills');+ await rm(path.join(root, 'fail'));+ const installed = await request('remotion-interactivity');+ expect(installed.success).toBe(true);+ expect(installed.data.remotionInteractivitySkillAvailable).toBe(true);+ expect(+ installed.data.skills.find(+ ({name}: {name: string}) => name === 'remotion-interactivity',+ ),+ ).toMatchObject({installedInProject: true});+ expect(+ await readFile(+ path.join(root, '.agents/skills/remotion-interactivity/SKILL.md'),+ 'utf8',+ ),+ ).toBe('# Installed skill');+ expect(+ JSON.parse(+ await readFile(path.join(root, 'install-call.json'), 'utf8'),+ ),+ ).toEqual([+ '--yes',+ '--loglevel=error',+ '[email protected]',+ 'add',+ 'remotion-dev/skills',+ '--skill',+ 'remotion-interactivity',+ '--yes',+ ]);+ } finally {+ process.env.PATH = previousPath;+ server.closeAllConnections();+ await new Promise<void>((resolve) => server.close(() => resolve()));+ await rm(root, {recursive: true, force: true});+ }+ },+);diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.tsindex 472f2fae69..ea9dcf4183 100644--- a/packages/studio-shared/src/api-requests.ts+++ b/packages/studio-shared/src/api-requests.ts@@ -1135,6 +1135,9 @@ export type GetReleaseNotesResponse = {};export type GetRemotionSkillsInfoRequest = {};+export type InstallRemotionSkillRequest = {+ skill: string;+};export type GetRemotionSkillsInfoResponse = {remotionUpgradeSkillAvailable: boolean;remotionInteractivitySkillAvailable: boolean;@@ -1413,6 +1416,10 @@ export type ApiRoutes = {GetRemotionSkillsInfoRequest,GetRemotionSkillsInfoResponse>;+ '/api/install-remotion-skill': ReqAndRes<+ InstallRemotionSkillRequest,+ GetRemotionSkillsInfoResponse+ >;'/api/apply-codemod': ReqAndRes<ApplyCodemodRequest, ApplyCodemodResponse>;'/api/project-info': ReqAndRes<ProjectInfoRequest, ProjectInfoResponse>;'/api/delete-static-file': ReqAndRes<diff --git a/packages/studio-shared/src/index.ts b/packages/studio-shared/src/index.tsindex 77a5f2553e..9cb112bf1e 100644--- a/packages/studio-shared/src/index.ts+++ b/packages/studio-shared/src/index.ts@@ -67,6 +67,7 @@ export {GetDefaultEditorInfoResponse,GetRemotionSkillsInfoRequest,GetRemotionSkillsInfoResponse,+ InstallRemotionSkillRequest,GetReleaseNotesRequest,GetReleaseNotesResponse,GoogleFontSourceEdit,diff --git a/packages/studio/src/components/SettingsContext.tsx b/packages/studio/src/components/SettingsContext.tsxindex 5b667c3189..d233f1744a 100644--- a/packages/studio/src/components/SettingsContext.tsx+++ b/packages/studio/src/components/SettingsContext.tsx@@ -16,6 +16,7 @@ import React, {import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';import {StudioServerConnectionCtx} from '../helpers/client-id';import {callApi} from './call-api';+import {showNotification} from './Notifications/NotificationCenter';import {UpdateStatusProvider} from './UpdateStatusContext';type SettingsContextValue = {@@ -28,6 +29,9 @@ type SettingsContextValue = {readonly studioRuntimeConfig: StudioRuntimeConfig | null;readonly revision: number;readonly setPublicLicenseKey: (publicLicenseKey: string | null) => void;+ readonly installSkill: (skill: string) => Promise<void>;+ readonly installingSkill: string | null;+ readonly skillInstallError: string | null;};const SettingsContext = createContext<SettingsContextValue | null>(null);@@ -39,7 +43,7 @@ export const SettingsProvider: React.FC<{StudioServerConnectionCtx,);const [settings, setSettings] = useState<- Omit<SettingsContextValue, 'setPublicLicenseKey'>+ Omit<SettingsContextValue, 'setPublicLicenseKey' | 'installSkill'>>({codingAgentInfo: null,editorInfo: null,@@ -49,6 +53,8 @@ export const SettingsProvider: React.FC<{renderDefaults: window.remotion_renderDefaults ?? null,studioRuntimeConfig: window.remotion_studioConfig ?? null,revision: 0,+ installingSkill: null,+ skillInstallError: null,});useEffect(() => {@@ -147,9 +153,37 @@ export const SettingsProvider: React.FC<{};});}, []);+ const installSkill = useCallback(async (skill: string) => {+ setSettings((currentSettings) => ({+ ...currentSettings,+ installingSkill: skill,+ skillInstallError: null,+ }));+ try {+ const remotionSkillsInfo = await callApi('/api/install-remotion-skill', {+ skill,+ });+ setSettings((currentSettings) => ({+ ...currentSettings,+ remotionSkillsInfo,+ installingSkill: null,+ revision: currentSettings.revision + 1,+ }));+ showNotification(+ `Installed ${skill}. Restart your coding agent to use it.`,+ 5000,+ );+ } catch (err) {+ setSettings((currentSettings) => ({+ ...currentSettings,+ installingSkill: null,+ skillInstallError: (err as Error).message,+ }));+ }+ }, []);const value = useMemo<SettingsContextValue>(() => {- return {...settings, setPublicLicenseKey};- }, [setPublicLicenseKey, settings]);+ return {...settings, setPublicLicenseKey, installSkill};+ }, [installSkill, setPublicLicenseKey, settings]);return (<SettingsContext.Provider value={value}>diff --git a/packages/studio/src/components/SettingsModal.tsx b/packages/studio/src/components/SettingsModal.tsxindex f2c808c929..b07bf74b9f 100644--- a/packages/studio/src/components/SettingsModal.tsx+++ b/packages/studio/src/components/SettingsModal.tsx@@ -325,7 +325,7 @@ export const SettingsModal: React.FC<{</div>{tab === 'packages' ? (<div ref={setPackagesFooterContainer} />- ) : isBrowserStudio || tab === 'updates' ? null : (+ ) : isBrowserStudio || tab === 'updates' || tab === 'skills' ? null : (<SettingsModalFooter showLicenseFaq={tab === 'license'} />)}</>diff --git a/packages/studio/src/components/SkillsSettings.tsx b/packages/studio/src/components/SkillsSettings.tsxindex 92866f8e22..c5bb0b3970 100644--- a/packages/studio/src/components/SkillsSettings.tsx+++ b/packages/studio/src/components/SkillsSettings.tsx@@ -1,4 +1,5 @@-import React, {useCallback, useMemo} from 'react';+import React, {useCallback, useContext, useMemo} from 'react';+import {StudioServerConnectionCtx} from '../helpers/client-id';import {BLUE,BORDER_WHITE_ALPHA_12,@@ -6,10 +7,12 @@ import {WHITE,} from '../helpers/colors';import {copyText} from '../helpers/copy-text';+import {NO_HOVER_BACKGROUND_STYLE} from '../helpers/hoverable';import {useCopyFeedback} from '../helpers/use-copy-feedback';import {CheckCircleFilled} from '../icons/check-circle-filled';import {CopyIcon} from '../icons/copy';import {Minus} from '../icons/minus';+import {Button} from './Button';import type {RenderInlineAction} from './InlineAction';import {InlineAction} from './InlineAction';import {ValidationMessage} from './NewComposition/ValidationMessage';@@ -109,7 +112,17 @@ const loading: React.CSSProperties = {};export const SkillsSettings: React.FC = () => {- const {error, remotionSkillsInfo} = useSettings();+ const {+ error,+ remotionSkillsInfo,+ installSkill,+ installingSkill,+ skillInstallError,+ } = useSettings();+ const {previewServerState} = useContext(StudioServerConnectionCtx);+ const canInstall =+ !window.remotion_isReadOnlyStudio &&+ previewServerState.type === 'connected';const {copied, markCopied} = useCopyFeedback();const installedSkills = useMemo(() => {return (@@ -146,7 +159,21 @@ export const SkillsSettings: React.FC = () => {<ValidationMessage message={error} align="flex-start" type="error" /></div>) : null}- {missingSkills > 0 ? (+ {missingSkills > 0 && canInstall ? (+ <p style={description}>+ Install skills in this project to use them with your coding agent.+ </p>+ ) : null}+ {skillInstallError ? (+ <div style={{marginTop: 14}}>+ <ValidationMessage+ message={skillInstallError}+ align="flex-start"+ type="error"+ />+ </div>+ ) : null}+ {missingSkills > 0 && !canInstall ? (<div><p style={description}>Not all skills are installed. Run this command in the project@@ -196,7 +223,21 @@ export const SkillsSettings: React.FC = () => {<Minus aria-hidden color={LIGHT_TEXT} style={statusIcon} />)}<span style={skillName}>/{skill.name}</span>- <span style={status}>{installedLocation}</span>+ {!installed && canInstall ? (+ <Button+ size="compact"+ style={NO_HOVER_BACKGROUND_STYLE}+ title={`Install ${skill.name} in this project`}+ disabled={installingSkill !== null}+ onClick={() => installSkill(skill.name)}+ >+ {installingSkill === skill.name+ ? 'Installing...'+ : 'Install'}+ </Button>+ ) : (+ <span style={status}>{installedLocation}</span>+ )}</div>);})}