Skip to content

🎥 Make videos programmatically with React

feat(studio): copy composition context for agents

YUHAO-corn90403086df54
diff --git a/packages/studio/src/components/composition-menu-items.ts b/packages/studio/src/components/composition-menu-items.ts
index 303a3e9863..58e5960719 100644
--- a/packages/studio/src/components/composition-menu-items.ts
+++ b/packages/studio/src/components/composition-menu-items.ts
@@ -2,7 +2,10 @@ import type {EditorPickerId} from '@remotion/studio-shared';
import type {SetStateAction} from 'react';
import type {ResolvedStackLocation, _InternalTypes} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
-import {formatFileLocation} from '../helpers/format-file-location';
+import {
+ formatContextForAgents,
+ formatFileLocation,
+} from '../helpers/format-file-location';
import {
openCompositionComponentInEditor,
openOriginalPositionInEditor,
@@ -38,6 +41,11 @@ export const getCompositionMenuItems = ({
location: resolvedLocation,
root: window.remotion_cwd,
});
+ const contextForAgents = formatContextForAgents({
+ location: resolvedLocation,
+ name: composition?.id ?? null,
+ root: window.remotion_cwd,
+ });
const showInEditorDisabled =
!editorId ||
!composition ||
@@ -188,6 +196,30 @@ export const getCompositionMenuItems = ({
id: 'copy-actions-divider',
}
: null,
+ {
+ id: 'copy-context-for-agents',
+ keyHint: null,
+ label: `Copy context for agents`,
+ leftItem: null,
+ onClick: () => {
+ closeMenu();
+ if (!contextForAgents) {
+ return;
+ }
+
+ navigator.clipboard.writeText(contextForAgents).catch((err) => {
+ showNotification(
+ `Could not copy to clipboard: ${(err as Error).message}`,
+ 1000,
+ );
+ });
+ },
+ quickSwitcherLabel: null,
+ subMenu: null,
+ type: 'item' as const,
+ value: 'copy-context-for-agents',
+ disabled: !contextForAgents,
+ },
{
id: 'copy-file-location',
keyHint: null,
diff --git a/packages/studio/src/test/composition-menu-items.test.ts b/packages/studio/src/test/composition-menu-items.test.ts
index 5cdc12dfcc..fc4ef3aa1a 100644
--- a/packages/studio/src/test/composition-menu-items.test.ts
+++ b/packages/studio/src/test/composition-menu-items.test.ts
@@ -1,6 +1,5 @@
-import {afterEach, expect, test} from 'bun:test';
-import type {_InternalTypes} from 'remotion';
-import type {ResolvedStackLocation} from 'remotion';
+import {afterEach, expect, mock, test} from 'bun:test';
+import type {_InternalTypes, ResolvedStackLocation} from 'remotion';
import {
getCompositionContextMenuItems,
getCompositionMenuItems,
@@ -10,6 +9,10 @@ const originalWindowDescriptor = Object.getOwnPropertyDescriptor(
globalThis,
'window',
);
+const originalNavigatorDescriptor = Object.getOwnPropertyDescriptor(
+ globalThis,
+ 'navigator',
+);
afterEach(() => {
if (originalWindowDescriptor) {
@@ -17,6 +20,12 @@ afterEach(() => {
} else {
Reflect.deleteProperty(globalThis, 'window');
}
+
+ if (originalNavigatorDescriptor) {
+ Object.defineProperty(globalThis, 'navigator', originalNavigatorDescriptor);
+ } else {
+ Reflect.deleteProperty(globalThis, 'navigator');
+ }
});
const installTestWindow = () => {
@@ -95,9 +104,18 @@ test('connected composition context menus omit management actions', () => {
expect(ids(items)).toEqual([
'open-in-new-window',
'open-in-new-window-divider',
+ 'copy-context-for-agents',
'copy-file-location',
'copy-id',
]);
+ const copyContext = items.find(
+ (item) => item.id === 'copy-context-for-agents',
+ );
+ if (copyContext?.type !== 'item') {
+ throw new Error('Expected copy context to be a menu item');
+ }
+
+ expect(copyContext.disabled).toBe(true);
});
test('copy actions are adjacent', () => {
@@ -107,14 +125,45 @@ test('copy actions are adjacent', () => {
...commonArgs,
includeCompositionManagementItems: true,
});
+ const copyContextIndex = items.findIndex(
+ (item) => item.id === 'copy-context-for-agents',
+ );
const copyFileLocationIndex = items.findIndex(
(item) => item.id === 'copy-file-location',
);
const copyIdIndex = items.findIndex((item) => item.id === 'copy-id');
+ expect(copyFileLocationIndex).toBe(copyContextIndex + 1);
expect(copyIdIndex).toBe(copyFileLocationIndex + 1);
});
+test('copies composition context for agents', async () => {
+ installTestWindow();
+ const writeText = mock(() => Promise.resolve());
+ Object.defineProperty(globalThis, 'navigator', {
+ configurable: true,
+ value: {clipboard: {writeText}},
+ });
+
+ const items = getCompositionMenuItems({
+ ...commonArgs,
+ includeCompositionManagementItems: false,
+ resolvedLocation,
+ });
+ const copyContext = items.find(
+ (item) => item.id === 'copy-context-for-agents',
+ );
+ if (copyContext?.type !== 'item') {
+ throw new Error('Expected copy context to be a menu item');
+ }
+
+ copyContext.onClick(copyContext.id, null);
+ await Promise.resolve();
+ expect(writeText).toHaveBeenCalledWith(
+ 'ConnectedComposition in src/Composition.tsx:10',
+ );
+});
+
test('editor actions use Open labels and are adjacent', () => {
installTestWindowWithEditor();
@@ -161,6 +210,7 @@ test('read-only composition menus keep navigation and copy actions enabled', ()
expect(itemById('open-in-new-window').disabled).not.toBe(true);
expect(itemById('show-in-editor').disabled).toBe(false);
expect(itemById('open-component-in-editor').disabled).toBe(false);
+ expect(itemById('copy-context-for-agents').disabled).toBe(false);
expect(itemById('copy-file-location').disabled).toBe(false);
expect(itemById('copy-id').disabled).toBe(false);
expect(itemById('rename').disabled).toBe(true);