Skip to content

🎥 Make videos programmatically with React

Share app selection rules between editors and coding agents

Jonny Burgerd7e8bb376bf1
diff --git a/packages/studio-server/src/helpers/coding-agent-registry.ts b/packages/studio-server/src/helpers/coding-agent-registry.ts
index c6b2b65dab..2820d48aab 100644
--- a/packages/studio-server/src/helpers/coding-agent-registry.ts
+++ b/packages/studio-server/src/helpers/coding-agent-registry.ts
@@ -5,7 +5,7 @@ import path from 'node:path';
import {promisify} from 'node:util';
import type {DefaultCodingAgent, LogLevel} from '@remotion/renderer';
import {defaultCodingAgentIds, RenderInternals} from '@remotion/renderer';
-import {getRunningProcesses} from './running-processes';
+import {getRunningProcesses, isAppRunning} from './running-processes';
const execFilePromise = promisify(execFile);
@@ -448,36 +448,19 @@ export const getRunningCodingAgents = async (
}
return installedCodingAgents
- .filter((agent) => {
- if (agent.platform === 'darwin') {
- return processes.some(({executable: processPath}) =>
- processPath?.startsWith(`${agent.applicationPath}/Contents/MacOS/`),
- );
- }
-
- const commands = codingAgentDefinitions[agent.id][agent.platform]
- .flatMap((variant) => variant.commands)
- .map((command) => command.replace(/\.(exe|cmd)$/i, ''));
- const escapedCommands = commands.map((command) =>
- command.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
- );
- const escapedPath = agent.applicationPath.replace(
- /[.*+?^${}()|[\]\\]/g,
- '\\$&',
- );
- const executable = new RegExp(
- `^"?(?:${escapedPath}|(?:[^"\\s]*[/\\\\])?(?:${escapedCommands.join('|')})(?:\\.(?:exe|cmd|js))?)"?(?=\\s|$)`,
- agent.platform === 'win32' ? 'i' : '',
- );
- return processes.some(({commandLine}) => {
- // Node-based CLIs put the script after the interpreter path.
- const withoutNode = (commandLine ?? '').replace(
- /^(?:"[^"\r\n]*[/\\]node(?:\.exe)?"|(?:[^\s"]*[/\\])?node(?:\.exe)?)\s+/i,
- '',
- );
- return executable.test(withoutNode);
- });
- })
+ .filter((agent) =>
+ isAppRunning({
+ processes,
+ executablePath: agent.applicationPath,
+ commands:
+ agent.platform === 'darwin'
+ ? []
+ : codingAgentDefinitions[agent.id][agent.platform].flatMap(
+ (variant) => variant.commands,
+ ),
+ platform: agent.platform,
+ }),
+ )
.map((agent) => agent.id);
};
diff --git a/packages/studio-server/src/helpers/editor-registry.ts b/packages/studio-server/src/helpers/editor-registry.ts
index babc963168..64f5521501 100644
--- a/packages/studio-server/src/helpers/editor-registry.ts
+++ b/packages/studio-server/src/helpers/editor-registry.ts
@@ -5,6 +5,7 @@ import path from 'node:path';
import {promisify} from 'node:util';
import type {BuiltInEditor} from '@remotion/renderer';
import {defaultEditorIds} from '@remotion/renderer';
+import {getRunningProcesses, isAppRunning} from './running-processes';
const execFilePromise = promisify(execFile);
@@ -669,3 +670,35 @@ export const getAvailableEditors = () => {
availableEditors ??= discoverAvailableEditors(defaultDiscoveryContext);
return availableEditors;
};
+
+export const getRunningEditors = async (
+ installedEditors: readonly InstalledEditor[],
+): Promise<readonly BuiltInEditor[]> => {
+ const {platform} = process;
+ if (
+ installedEditors.length === 0 ||
+ (platform !== 'darwin' && platform !== 'linux' && platform !== 'win32')
+ ) {
+ return [];
+ }
+
+ const processes = await getRunningProcesses();
+ if (processes === null) {
+ return [];
+ }
+
+ return installedEditors
+ .filter((editor) =>
+ isAppRunning({
+ processes,
+ executablePath: editor.process,
+ commands: editorDefinitions[editor.id][platform]
+ .map((variant) =>
+ 'commands' in variant ? variant.commands : [variant.command],
+ )
+ .flat(),
+ platform,
+ }),
+ )
+ .map(({id}) => id);
+};
diff --git a/packages/studio-server/src/helpers/running-processes.ts b/packages/studio-server/src/helpers/running-processes.ts
index 33668a6937..7717af64dd 100644
--- a/packages/studio-server/src/helpers/running-processes.ts
+++ b/packages/studio-server/src/helpers/running-processes.ts
@@ -13,6 +13,47 @@ let processSnapshot: {
expiresAt: number;
} | null = null;
+export const isAppRunning = ({
+ processes,
+ executablePath,
+ commands,
+ platform,
+}: {
+ processes: readonly RunningProcess[];
+ executablePath: string;
+ commands: readonly string[];
+ platform: NodeJS.Platform;
+}) => {
+ if (platform === 'darwin') {
+ const applicationPath = executablePath.split('/Contents/')[0];
+ if (applicationPath.endsWith('.app')) {
+ return processes.some(({executable}) =>
+ executable?.startsWith(`${applicationPath}/Contents/MacOS/`),
+ );
+ }
+ }
+
+ const escapedCommands = commands.map((command) =>
+ command.replace(/\.(exe|cmd)$/i, '').replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
+ );
+ const escapedPath = executablePath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ const executablePattern = new RegExp(
+ `^"?(?:${escapedPath}|(?:[^"\\s]*[/\\\\])?(?:${escapedCommands.join('|')})(?:\\.(?:exe|cmd|js))?)"?(?=\\s|$)`,
+ platform === 'win32' ? 'i' : '',
+ );
+ return processes.some(({executable, commandLine}) => {
+ // Node-based CLIs put the script after the interpreter path.
+ const withoutNode = (commandLine ?? '').replace(
+ /^(?:"[^"\r\n]*[/\\]node(?:\.exe)?"|(?:[^\s"]*[/\\])?node(?:\.exe)?)\s+/i,
+ '',
+ );
+ return (
+ executablePattern.test(executable ?? '') ||
+ executablePattern.test(withoutNode)
+ );
+ });
+};
+
export const getRunningProcesses = (): Promise<
readonly RunningProcess[] | null
> => {
diff --git a/packages/studio-server/src/preview-server/routes/default-coding-agent.ts b/packages/studio-server/src/preview-server/routes/default-coding-agent.ts
index 3e7edafcf2..402c51dc34 100644
--- a/packages/studio-server/src/preview-server/routes/default-coding-agent.ts
+++ b/packages/studio-server/src/preview-server/routes/default-coding-agent.ts
@@ -16,37 +16,23 @@ import type {ApiHandler} from '../api-types';
export const getDefaultCodingAgentInfoHandler: ApiHandler<
GetDefaultCodingAgentInfoRequest,
GetDefaultCodingAgentInfoResponse
-> = async ({getDefaultCodingAgent, input}) => {
+> = async ({getDefaultCodingAgent}) => {
const installedCodingAgents = await getAvailableCodingAgents();
const runningCodingAgents = await getRunningCodingAgents(
installedCodingAgents,
);
const installedTerminals = await getAvailableTerminals();
const installedGitClients = await getAvailableGitClients();
- const recentlyUsedCodingAgents = input.recentlyUsedCodingAgents ?? [];
return {
defaultCodingAgent: getDefaultCodingAgent(),
- installedCodingAgents: [...installedCodingAgents]
- .sort((a, b) => {
- const runningDifference =
- Number(runningCodingAgents.includes(b.id)) -
- Number(runningCodingAgents.includes(a.id));
- if (runningDifference !== 0) {
- return runningDifference;
- }
-
- const aIndex = recentlyUsedCodingAgents.indexOf(a.id);
- const bIndex = recentlyUsedCodingAgents.indexOf(b.id);
- return (
- (aIndex === -1 ? recentlyUsedCodingAgents.length : aIndex) -
- (bIndex === -1 ? recentlyUsedCodingAgents.length : bIndex)
- );
- })
- .map(({id, name, nameWithType}) => ({
+ runningCodingAgents,
+ installedCodingAgents: installedCodingAgents.map(
+ ({id, name, nameWithType}) => ({
id,
name,
nameWithType,
- })),
+ }),
+ ),
installedTerminals: installedTerminals.map(({id, name}) => ({id, name})),
installedGitClients: installedGitClients.map(({id, name}) => ({id, name})),
};
diff --git a/packages/studio-server/src/preview-server/routes/default-editor.ts b/packages/studio-server/src/preview-server/routes/default-editor.ts
index eae5e3e207..b724917efb 100644
--- a/packages/studio-server/src/preview-server/routes/default-editor.ts
+++ b/packages/studio-server/src/preview-server/routes/default-editor.ts
@@ -3,7 +3,10 @@ import type {
GetDefaultEditorInfoResponse,
} from '@remotion/studio-shared';
import {resolveCustomEditorExecutable} from '../../helpers/custom-editor';
-import {getAvailableEditors} from '../../helpers/editor-registry';
+import {
+ getAvailableEditors,
+ getRunningEditors,
+} from '../../helpers/editor-registry';
import type {ApiHandler} from '../api-types';
export const getDefaultEditorInfoHandler: ApiHandler<
@@ -11,6 +14,7 @@ export const getDefaultEditorInfoHandler: ApiHandler<
GetDefaultEditorInfoResponse
> = async ({getDefaultEditor}) => {
const installedEditors = await getAvailableEditors();
+ const runningEditors = await getRunningEditors(installedEditors);
const configuredEditor = getDefaultEditor();
const customEditor =
configuredEditor &&
@@ -20,7 +24,10 @@ export const getDefaultEditorInfoHandler: ApiHandler<
: null;
return {
defaultEditor:
- typeof configuredEditor === 'object' ? 'custom' : configuredEditor,
+ configuredEditor && typeof configuredEditor === 'object'
+ ? 'custom'
+ : configuredEditor,
+ runningEditors,
installedEditors: [
...installedEditors.map(({id, name, nameWithType}) => ({
id,
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index 26fc945c57..98c746caa8 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -1397,6 +1397,7 @@ export type GetDefaultEditorInfoRequest = {};
export type EditorPickerId = BuiltInEditor | 'custom';
export type GetDefaultEditorInfoResponse = {
defaultEditor: EditorPickerId | null;
+ runningEditors?: readonly EditorPickerId[] | null;
installedEditors: {
id: EditorPickerId;
name: string;
@@ -1404,11 +1405,10 @@ export type GetDefaultEditorInfoResponse = {
}[];
};
-export type GetDefaultCodingAgentInfoRequest = {
- recentlyUsedCodingAgents: readonly DefaultCodingAgent[];
-};
+export type GetDefaultCodingAgentInfoRequest = {};
export type GetDefaultCodingAgentInfoResponse = {
defaultCodingAgent: DefaultCodingAgent | null;
+ runningCodingAgents?: readonly DefaultCodingAgent[] | null;
installedCodingAgents: {
id: DefaultCodingAgent;
name: string;
diff --git a/packages/studio/src/components/CodingAgentButton.tsx b/packages/studio/src/components/CodingAgentButton.tsx
index a81c99c952..24581213ef 100644
--- a/packages/studio/src/components/CodingAgentButton.tsx
+++ b/packages/studio/src/components/CodingAgentButton.tsx
@@ -7,6 +7,7 @@ import type {ComboboxValue} from './NewComposition/ComboBox';
import {showNotification} from './Notifications/NotificationCenter';
import {useSettings} from './SettingsContext';
import {useConfigureDefaultApps} from './use-configure-default-apps';
+import {getPreferredCodingAgent} from './use-default-editor-info';
const menuLabel: React.CSSProperties = {
color: 'inherit',
@@ -24,10 +25,7 @@ export const CodingAgentButton: React.FC<{
const {codingAgentInfo} = useSettings();
const configureDefaultApps = useConfigureDefaultApps();
const installedCodingAgents = codingAgentInfo?.installedCodingAgents ?? [];
- const defaultCodingAgent =
- installedCodingAgents.find(
- (agent) => agent.id === codingAgentInfo?.defaultCodingAgent,
- ) ?? installedCodingAgents[0];
+ const defaultCodingAgent = getPreferredCodingAgent(codingAgentInfo);
const alternativeCodingAgents = installedCodingAgents.filter(
(agent) => agent.id !== defaultCodingAgent?.id,
);
diff --git a/packages/studio/src/components/SettingsContext.tsx b/packages/studio/src/components/SettingsContext.tsx
index 2286c7bde5..18ac1d11a6 100644
--- a/packages/studio/src/components/SettingsContext.tsx
+++ b/packages/studio/src/components/SettingsContext.tsx
@@ -15,10 +15,7 @@ import React, {
} from 'react';
import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
import {StudioServerConnectionCtx} from '../helpers/client-id';
-import {
- codingAgentSelectedEvent,
- getRecentlyUsedCodingAgents,
-} from '../state/recently-used-coding-agents';
+import {appSelectedEvent} from '../state/recently-used-apps';
import {callApi} from './call-api';
import {showNotification} from './Notifications/NotificationCenter';
import {UpdateStatusProvider} from './UpdateStatusContext';
@@ -86,20 +83,10 @@ export const SettingsProvider: React.FC<{
error: null,
}));
- Promise.all([
- callApi('/api/default-editor-info', {}, controller.signal),
- callApi('/api/remotion-skills-info', {}, controller.signal),
- ])
- .then(([editorInfo, remotionSkillsInfo]) => {
- const runtimeConfig = window.remotion_studioConfig;
+ callApi('/api/remotion-skills-info', {}, controller.signal)
+ .then((remotionSkillsInfo) => {
setSettings((currentSettings) => ({
...currentSettings,
- editorInfo: {
- ...editorInfo,
- defaultEditor: runtimeConfig
- ? runtimeConfig.defaultEditor
- : editorInfo.defaultEditor,
- },
remotionSkillsInfo,
error: null,
revision: currentSettings.revision + 1,
@@ -128,16 +115,15 @@ export const SettingsProvider: React.FC<{
}
let controller: AbortController | null = null;
- const refreshCodingAgents = () => {
+ const refreshApps = () => {
controller?.abort();
const requestController = new AbortController();
controller = requestController;
- callApi(
- '/api/default-coding-agent-info',
- {recentlyUsedCodingAgents: getRecentlyUsedCodingAgents()},
- requestController.signal,
- )
- .then((codingAgentInfo) => {
+ Promise.all([
+ callApi('/api/default-coding-agent-info', {}, requestController.signal),
+ callApi('/api/default-editor-info', {}, requestController.signal),
+ ])
+ .then(([codingAgentInfo, editorInfo]) => {
if (requestController.signal.aborted) {
return;
}
@@ -151,6 +137,12 @@ export const SettingsProvider: React.FC<{
? runtimeConfig.defaultCodingAgent
: codingAgentInfo.defaultCodingAgent,
},
+ editorInfo: {
+ ...editorInfo,
+ defaultEditor: runtimeConfig
+ ? runtimeConfig.defaultEditor
+ : editorInfo.defaultEditor,
+ },
}));
})
.catch((err) => {
@@ -165,16 +157,16 @@ export const SettingsProvider: React.FC<{
});
};
- refreshCodingAgents();
- window.addEventListener('focus', refreshCodingAgents);
- window.addEventListener(codingAgentSelectedEvent, refreshCodingAgents);
- window.addEventListener('storage', refreshCodingAgents);
+ refreshApps();
+ window.addEventListener('focus', refreshApps);
+ window.addEventListener(appSelectedEvent, refreshApps);
+ window.addEventListener('storage', refreshApps);
return () => {
controller?.abort();
- window.removeEventListener('focus', refreshCodingAgents);
- window.removeEventListener(codingAgentSelectedEvent, refreshCodingAgents);
- window.removeEventListener('storage', refreshCodingAgents);
+ window.removeEventListener('focus', refreshApps);
+ window.removeEventListener(appSelectedEvent, refreshApps);
+ window.removeEventListener('storage', refreshApps);
};
}, [previewServerState.type]);
diff --git a/packages/studio/src/components/use-default-editor-info.ts b/packages/studio/src/components/use-default-editor-info.ts
index 6ba0f6d794..b96f0d35ca 100644
--- a/packages/studio/src/components/use-default-editor-info.ts
+++ b/packages/studio/src/components/use-default-editor-info.ts
@@ -1,8 +1,11 @@
import type {
EditorPickerId,
+ GetDefaultCodingAgentInfoResponse,
GetDefaultEditorInfoResponse,
} from '@remotion/studio-shared';
import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
+import {getPreferredApp} from '../helpers/get-preferred-app';
+import {codingAgentHistory, editorHistory} from '../state/recently-used-apps';
import {useSettings} from './SettingsContext';
export const canUseEditorPicker = (previewServerConnected: boolean) => {
@@ -26,41 +29,36 @@ const preferredFallbackEditorIds: EditorPickerId[] = [
export const getPreferredEditorId = (
editorInfo: GetDefaultEditorInfoResponse | null,
): EditorPickerId | null => {
- const configuredEditorId = editorInfo?.installedEditors.find(
- (editor) => editor.id === editorInfo.defaultEditor,
- )?.id;
- if (configuredEditorId) {
- return configuredEditorId;
- }
-
- const runningEditorId = editorInfo?.installedEditors.find(
- (editor) => editor.nameWithType === window.remotion_editorName,
- )?.id;
- if (runningEditorId) {
- return runningEditorId;
- }
-
return (
- editorInfo?.installedEditors
- .slice()
- .sort((a, b) => {
- const aPriority = preferredFallbackEditorIds.indexOf(a.id);
- const bPriority = preferredFallbackEditorIds.indexOf(b.id);
- const normalizedAPriority =
- aPriority === -1 ? Number.POSITIVE_INFINITY : aPriority;
- const normalizedBPriority =
- bPriority === -1 ? Number.POSITIVE_INFINITY : bPriority;
-
- if (normalizedAPriority !== normalizedBPriority) {
- return normalizedAPriority - normalizedBPriority;
- }
-
- return a.nameWithType.localeCompare(b.nameWithType);
- })
- .at(0)?.id ?? null
+ getPreferredApp({
+ installedApps: editorInfo?.installedEditors ?? [],
+ configuredId: editorInfo?.defaultEditor ?? null,
+ runningIds:
+ editorInfo?.runningEditors ??
+ editorInfo?.installedEditors
+ .filter(
+ (editor) => editor.nameWithType === window.remotion_editorName,
+ )
+ .map((editor) => editor.id) ??
+ [],
+ recentlyUsedIds: editorHistory.getRecentlyUsed(),
+ fallbackIds: preferredFallbackEditorIds,
+ })?.id ?? null
);
};
+export const getPreferredCodingAgent = (
+ codingAgentInfo: GetDefaultCodingAgentInfoResponse | null,
+) => {
+ return getPreferredApp({
+ installedApps: codingAgentInfo?.installedCodingAgents ?? [],
+ configuredId: codingAgentInfo?.defaultCodingAgent ?? null,
+ runningIds: codingAgentInfo?.runningCodingAgents ?? [],
+ recentlyUsedIds: codingAgentHistory.getRecentlyUsed(),
+ fallbackIds: codingAgentInfo?.installedCodingAgents.map(({id}) => id) ?? [],
+ });
+};
+
export const useEditorOpening = (previewServerConnected: boolean) => {
const editorConnectionAvailable = canOpenInEditor(previewServerConnected);
const editorInfo = useDefaultEditorInfo(editorConnectionAvailable);
diff --git a/packages/studio/src/helpers/get-preferred-app.ts b/packages/studio/src/helpers/get-preferred-app.ts
new file mode 100644
index 0000000000..0b02c6e758
--- /dev/null
+++ b/packages/studio/src/helpers/get-preferred-app.ts
@@ -0,0 +1,45 @@
+export const getPreferredApp = <
+ App extends {id: string; nameWithType: string},
+>({
+ installedApps,
+ configuredId,
+ runningIds,
+ recentlyUsedIds,
+ fallbackIds,
+}: {
+ installedApps: readonly App[];
+ configuredId: string | null;
+ runningIds: readonly string[];
+ recentlyUsedIds: readonly string[];
+ fallbackIds: readonly string[];
+}): App | null => {
+ const configuredApp = installedApps.find((app) => app.id === configuredId);
+ if (configuredApp) {
+ return configuredApp;
+ }
+
+ return (
+ [...installedApps]
+ .sort((a, b) => {
+ const runningDifference =
+ Number(runningIds.includes(b.id)) - Number(runningIds.includes(a.id));
+ if (runningDifference !== 0) {
+ return runningDifference;
+ }
+
+ for (const ids of [recentlyUsedIds, fallbackIds]) {
+ const aIndex = ids.indexOf(a.id);
+ const bIndex = ids.indexOf(b.id);
+ const difference =
+ (aIndex === -1 ? ids.length : aIndex) -
+ (bIndex === -1 ? ids.length : bIndex);
+ if (difference !== 0) {
+ return difference;
+ }
+ }
+
+ return a.nameWithType.localeCompare(b.nameWithType);
+ })
+ .at(0) ?? null
+ );
+};
diff --git a/packages/studio/src/helpers/open-in-editor.ts b/packages/studio/src/helpers/open-in-editor.ts
index cb518db4bf..adcbc25f6b 100644
--- a/packages/studio/src/helpers/open-in-editor.ts
+++ b/packages/studio/src/helpers/open-in-editor.ts
@@ -14,11 +14,11 @@ import type {
CodePosition,
OriginalPosition,
} from '../error-overlay/react-overlay/utils/get-source-map';
-import {rememberCodingAgent} from '../state/recently-used-coding-agents';
+import {codingAgentHistory, editorHistory} from '../state/recently-used-apps';
import {getBrowserStudioOperations} from './browser-studio-operations';
import {useSyncExternalStore} from './use-sync-external-store';
-export const openInEditor = (
+export const openInEditor = async (
stack: SymbolicatedStackFrame,
editorId: EditorPickerId,
) => {
@@ -30,7 +30,7 @@ export const openInEditor = (
originalScriptCode,
} = stack;
- return callApi('/api/open-in-editor', {
+ const response = await callApi('/api/open-in-editor', {
editorId,
stack: {
originalFileName,
@@ -40,6 +40,11 @@ export const openInEditor = (
originalScriptCode,
},
});
+ if (response.success) {
+ editorHistory.remember(editorId);
+ }
+
+ return response;
};
export const openInCodingAgent = async (
@@ -51,7 +56,7 @@ export const openInCodingAgent = async (
prompt,
});
if (response.success) {
- rememberCodingAgent(codingAgentId);
+ codingAgentHistory.remember(codingAgentId);
}
return response;
diff --git a/packages/studio/src/state/recently-used-apps.ts b/packages/studio/src/state/recently-used-apps.ts
new file mode 100644
index 0000000000..450804e050
--- /dev/null
+++ b/packages/studio/src/state/recently-used-apps.ts
@@ -0,0 +1,58 @@
+import type {DefaultCodingAgent} from '@remotion/renderer';
+import {
+ defaultCodingAgentIds,
+ defaultEditorIds,
+} from '@remotion/renderer/client';
+import type {EditorPickerId} from '@remotion/studio-shared';
+
+export const appSelectedEvent = 'remotion-app-selected';
+
+const createAppHistory = <Id extends string>(
+ key: string,
+ appIds: readonly Id[],
+) => {
+ let inMemoryChoices: readonly Id[] | null = null;
+
+ const getRecentlyUsed = (): readonly Id[] => {
+ if (inMemoryChoices !== null) {
+ return inMemoryChoices;
+ }
+
+ try {
+ const stored: unknown = JSON.parse(
+ window.localStorage.getItem(key) ?? '[]',
+ );
+ if (Array.isArray(stored)) {
+ return stored.filter((id): id is Id => appIds.includes(id));
+ }
+ } catch {
+ // Keep the choice in memory when browser storage is unavailable.
+ }
+
+ return [];
+ };
+
+ const remember = (appId: Id) => {
+ const choices = [appId, ...getRecentlyUsed().filter((id) => id !== appId)];
+ try {
+ window.localStorage.setItem(key, JSON.stringify(choices));
+ inMemoryChoices = null;
+ } catch {
+ inMemoryChoices = choices;
+ }
+
+ window.dispatchEvent(new Event(appSelectedEvent));
+ };
+
+ return {getRecentlyUsed, remember};
+};
+
+export const codingAgentHistory = createAppHistory<DefaultCodingAgent>(
+ 'remotion.recentlyUsedCodingAgents',
+ defaultCodingAgentIds,
+);
+
+export const editorHistory = createAppHistory<EditorPickerId>(
+ 'remotion.recentlyUsedEditors',
+ [...defaultEditorIds, 'custom'],
+);
diff --git a/packages/studio/src/state/recently-used-coding-agents.ts b/packages/studio/src/state/recently-used-coding-agents.ts
deleted file mode 100644
index 778e0610b7..0000000000
--- a/packages/studio/src/state/recently-used-coding-agents.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-import type {DefaultCodingAgent} from '@remotion/renderer';
-import {defaultCodingAgentIds} from '@remotion/renderer/client';
-
-const key = 'remotion.recentlyUsedCodingAgents';
-export const codingAgentSelectedEvent = 'remotion-coding-agent-selected';
-let inMemoryChoices: readonly DefaultCodingAgent[] | null = null;
-
-export const getRecentlyUsedCodingAgents =
- (): readonly DefaultCodingAgent[] => {
- if (inMemoryChoices !== null) {
- return inMemoryChoices;
- }
-
- try {
- const stored: unknown = JSON.parse(
- window.localStorage.getItem(key) ?? '[]',
- );
- if (Array.isArray(stored)) {
- return stored.filter((id): id is DefaultCodingAgent =>
- defaultCodingAgentIds.includes(id),
- );
- }
- } catch {
- // Keep the choice in memory when browser storage is unavailable.
- }
-
- return [];
- };
-
-export const rememberCodingAgent = (codingAgentId: DefaultCodingAgent) => {
- const choices = [
- codingAgentId,
- ...getRecentlyUsedCodingAgents().filter((id) => id !== codingAgentId),
- ];
- try {
- window.localStorage.setItem(key, JSON.stringify(choices));
- inMemoryChoices = null;
- } catch {
- inMemoryChoices = choices;
- }
-
- window.dispatchEvent(new Event(codingAgentSelectedEvent));
-};