Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(md): offer Copy as prompt on markdown documents (#7126)

Eric Hayesf75ca4c22d1e
diff --git a/apps/web/src/features/block-md/component/DispatchAgentMenu.tsx b/apps/web/src/features/block-md/component/DispatchAgentMenu.tsx
index de5f32c004..072772afb4 100644
--- a/apps/web/src/features/block-md/component/DispatchAgentMenu.tsx
+++ b/apps/web/src/features/block-md/component/DispatchAgentMenu.tsx
@@ -1,4 +1,5 @@
import { openMacroMcpSetupModal } from '@app/features/integrations/mcp-setup/MacroMcpSetupModal';
+import type { CustomFileOperation } from '@components/app/split-layout/components/SplitFileMenu';
import type { SplitFileMenuAction } from '@components/app/split-layout/context';
import { editorStateAsMarkdown } from '@core/component/LexicalMarkdown/utils';
import { toast } from '@core/component/Toast/Toast';
@@ -81,19 +82,86 @@ async function fetchMessagePromptThreads(
return messagePromptThreads(threads);
}
-async function generateTaskPrompt(
+function promptThreadLines(threads: PromptThread[]): string[] {
+ const lines: string[] = [];
+ for (const thread of threads) {
+ lines.push(`<comment-thread thread-id="${thread.threadId}">`);
+ for (const comment of thread.comments) {
+ const macroId = tryMacroId(comment.author);
+ const author = macroId ? macroIdToEmail(macroId) : comment.author;
+ const createdAt = comment.createdAt
+ ? ` created-at="${comment.createdAt}"`
+ : '';
+ lines.push(
+ `<comment author="${author}"${createdAt}>${comment.text}</comment>`
+ );
+ }
+ lines.push('</comment-thread>');
+ }
+ return lines;
+}
+
+async function fetchBranchName(documentId: string) {
+ const result = await storageServiceClient.getDocumentBranchName({
+ documentId,
+ });
+ if (!result.isOk()) {
+ throw new Error('Failed to fetch branch name');
+ }
+ return result.value;
+}
+
+/**
+ * Plain documents carry no branch (the branch endpoint rejects non-tasks), so
+ * the prompt frames them as context to read.
+ */
+async function generateDocumentPrompt(
documentId: string,
documentName: string,
content: string,
threads: PromptThread[]
): Promise<string> {
- const result = await storageServiceClient.getDocumentBranchName({
- documentId,
- });
+ const result = await storageServiceClient.getDocumentShortId({ documentId });
if (!result.isOk()) {
- throw new Error('Failed to fetch branch name');
+ throw new Error('Failed to fetch short id');
}
- const { shortId, branchName } = result.value;
+ const shortId = result.value;
+
+ const lines: string[] = [];
+
+ lines.push(`Read the Macro document ${documentName}:`);
+ lines.push('');
+ lines.push(`<document identifier="${shortId}">`);
+ lines.push(`<title>${documentName}</title>`);
+ lines.push('</document>');
+
+ if (content) {
+ lines.push('');
+ lines.push('<document-content>');
+ lines.push(content);
+ lines.push('</document-content>');
+ }
+
+ if (threads.length > 0) {
+ lines.push('');
+ lines.push(...promptThreadLines(threads));
+ }
+
+ lines.push('');
+ lines.push(
+ 'If you have the Macro MCP server enabled, use it to gather additional context about this document.'
+ );
+
+ return lines.join('\n');
+}
+
+async function generateTaskPrompt(
+ documentId: string,
+ documentName: string,
+ content: string,
+ threads: PromptThread[]
+): Promise<string> {
+ const { shortId, branchName } = await fetchBranchName(documentId);
const lines: string[] = [];
@@ -113,20 +181,7 @@ async function generateTaskPrompt(
if (threads.length > 0) {
lines.push('');
- for (const thread of threads) {
- lines.push(`<comment-thread thread-id="${thread.threadId}">`);
- for (const comment of thread.comments) {
- const macroId = tryMacroId(comment.author);
- const author = macroId ? macroIdToEmail(macroId) : comment.author;
- const createdAt = comment.createdAt
- ? ` created-at="${comment.createdAt}"`
- : '';
- lines.push(
- `<comment author="${author}"${createdAt}>${comment.text}</comment>`
- );
- }
- lines.push('</comment-thread>');
- }
+ lines.push(...promptThreadLines(threads));
}
lines.push('');
@@ -162,7 +217,7 @@ const COPY_ACTION: AgentAction = {
buttonIcon: TerminalWindowIcon,
execute: (prompt) => {
navigator.clipboard.writeText(prompt);
- toast.success('Task prompt copied to clipboard');
+ toast.success('Prompt copied to clipboard');
},
};
@@ -211,10 +266,11 @@ const [lastUsedKey, setLastUsedKey] = makePersisted(
);
export function useDispatchAgentAction() {
- const { documentId, state } = useMarkdownDocument();
+ const { documentId, kind, state } = useMarkdownDocument();
const blockId = documentId();
const { displayName: name } = useMarkdownName();
const discussionThreads = useDiscussionThreads();
+ const isTask = () => kind() === 'task';
const lastUsed = () =>
ALL_ACTIONS.find((a) => a.key === lastUsedKey()) ?? COPY_ACTION;
@@ -226,7 +282,8 @@ export function useDispatchAgentAction() {
const threads = isFeatureEnabled(enableUnifiedDocumentDiscussions)
? await fetchMessagePromptThreads(blockId)
: legacyPromptThreads(discussionThreads() ?? []);
- return generateTaskPrompt(blockId, docName, content, threads);
+ const generate = isTask() ? generateTaskPrompt : generateDocumentPrompt;
+ return generate(blockId, docName, content, threads);
});
const executeAction = async (action: AgentAction) => {
@@ -235,50 +292,59 @@ export function useDispatchAgentAction() {
action.execute(prompt);
setLastUsedKey(action.key);
} catch (e) {
- console.error('Failed to generate task prompt', e);
- toast.failure('Failed to generate task prompt');
+ console.error('Failed to generate prompt', e);
+ toast.failure('Failed to generate prompt');
}
};
return {
blockId,
+ isTask,
lastUsed,
executeAction,
executeLastUsed: () => executeAction(lastUsed()),
};
}
-export function useDispatchAgentSplitFileActions(): SplitFileMenuAction[] {
+export function useDispatchAgentSplitFileActions(): {
+ copyAsPrompt: CustomFileOperation;
+ all: SplitFileMenuAction[];
+} {
const { executeAction } = useDispatchAgentAction();
- return [
- {
- label: COPY_ACTION.name,
- icon: COPY_ACTION.icon,
- action: () => {
- void executeAction(COPY_ACTION);
- },
+ const copyAsPrompt: CustomFileOperation = {
+ label: COPY_ACTION.name,
+ icon: COPY_ACTION.icon,
+ action: () => {
+ void executeAction(COPY_ACTION);
},
- ...PLATFORM_ACTIONS.map((action) => ({
- label: action.name,
- icon: action.icon,
- action: () => {
- void executeAction(action);
+ };
+
+ return {
+ copyAsPrompt,
+ all: [
+ copyAsPrompt,
+ ...PLATFORM_ACTIONS.map((action) => ({
+ label: action.name,
+ icon: action.icon,
+ action: () => {
+ void executeAction(action);
+ },
+ })),
+ {
+ label: 'MCP setup instructions',
+ icon: PlugIcon,
+ action: openMacroMcpSetupModal,
},
- })),
- {
- label: 'MCP setup instructions',
- icon: PlugIcon,
- action: openMacroMcpSetupModal,
- },
- ];
+ ],
+ };
}
export function DispatchAgentButton(
props: { showPrimaryLabel?: boolean } = {}
) {
const [open, setOpen] = createSignal(false);
- const { blockId, lastUsed, executeAction, executeLastUsed } =
+ const { blockId, isTask, lastUsed, executeAction, executeLastUsed } =
useDispatchAgentAction();
return (
@@ -323,15 +389,17 @@ export function DispatchAgentButton(
<Dynamic component={COPY_ACTION.icon} class="size-4 shrink-0" />
<span class="flex-1 truncate">{COPY_ACTION.name}</span>
</Dropdown.Item>
- <Dropdown.Item
- onSelect={() => {
- copyBranchNameToClipboard(blockId);
- setOpen(false);
- }}
- >
- <GitBranch class="size-4 shrink-0" />
- <span class="flex-1 truncate">Copy branch name</span>
- </Dropdown.Item>
+ <Show when={isTask()}>
+ <Dropdown.Item
+ onSelect={() => {
+ copyBranchNameToClipboard(blockId);
+ setOpen(false);
+ }}
+ >
+ <GitBranch class="size-4 shrink-0" />
+ <span class="flex-1 truncate">Copy branch name</span>
+ </Dropdown.Item>
+ </Show>
<Dropdown.Item
onSelect={() => {
openMacroMcpSetupModal();
diff --git a/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx b/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx
index 406e3794dd..0e8fa604f1 100644
--- a/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx
+++ b/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx
@@ -82,6 +82,7 @@ export function MarkdownSidePanelSections() {
const { displayName } = useMarkdownName();
const isTask = () => kind() === 'task';
const isSnippet = () => kind() === 'snippet';
+ const canDispatchToAgent = () => isTask() || kind() === 'document';
const entity = (): Entity => ({
id: documentId(),
type: 'document',
@@ -106,7 +107,7 @@ export function MarkdownSidePanelSections() {
fileType: 'md',
}}
/>
- <Show when={isTask() && !isMobile()}>
+ <Show when={canDispatchToAgent() && !isMobile()}>
<DispatchAgentButton showPrimaryLabel />
</Show>
</div>
diff --git a/apps/web/src/features/block-md/component/useMarkdownDocumentTools.tsx b/apps/web/src/features/block-md/component/useMarkdownDocumentTools.tsx
index f8fdc7406e..2227dea459 100644
--- a/apps/web/src/features/block-md/component/useMarkdownDocumentTools.tsx
+++ b/apps/web/src/features/block-md/component/useMarkdownDocumentTools.tsx
@@ -61,6 +61,7 @@ export function useMarkdownDocumentTools() {
const openShare = useMarkdownShareModal();
const dispatchAgentActions = useDispatchAgentSplitFileActions();
const isTask = kind() === 'task';
+ const isDocument = kind() === 'document';
const chatEntity = () => ({
type: 'document' as const,
@@ -84,6 +85,11 @@ export function useMarkdownDocumentTools() {
},
] satisfies FileOperation[])
: []),
+ ...(isDocument
+ ? ([
+ { ...dispatchAgentActions.copyAsPrompt, group: 'file' as const },
+ ] satisfies FileOperation[])
+ : []),
{
group: 'file',
label: 'Download',
@@ -129,7 +135,7 @@ export function useMarkdownDocumentTools() {
label: 'Code Actions',
icon: TerminalWindowIcon,
action: () => {},
- children: dispatchAgentActions,
+ children: dispatchAgentActions.all,
},
] satisfies BlockTool[])
: []),
diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.md
index df341f86f5..447a833b72 100644
--- a/docs/AGENT_GUIDE/documents.md
+++ b/docs/AGENT_GUIDE/documents.md
@@ -638,7 +638,15 @@ With the flag off, documents behave exactly as described above this section.
Right side of a doc (toggle with `Hide/Show Side Panel`):
-- `Actions` → `Ask Macro` (opens a doc-scoped AI chat, see ai-chat.md).
+- `Actions` → `Ask Macro` (opens a doc-scoped AI chat, see ai-chat.md), and on
+ desktop a `Copy as prompt` pill for documents and tasks. Its primary button
+ runs the last-used agent action; the `Agent options` caret lists `Copy as
+ prompt`, `MCP setup instructions`, and an `Open in` group (Claude Code Web,
+ Codex Desktop, Cursor, Zed). Tasks add `Copy branch name`. A document prompt
+ wraps the title and markdown in `<document>` / `<document-content>` tags with
+ no branch instructions; the toast reads `Prompt copied to clipboard`. The
+ Files-view title menu (three dots beside the breadcrumb) also lists `Copy as
+ prompt` directly above `Download` for plain documents.
- `Details` → Owner, Created, Last updated.
- `Tags` → `Add tags` (dialog). Click a tag's label to toggle and save; Shift-click
keeps the picker open for multiple selections. Reopen it to remove a tag.