fix: ai generation of mindmap fail from note text context (#8952)

Fix issue [AF-1632](https://linear.app/affine-design/issue/AF-1632).

### What Changed?
- Keep the note selection and do not set the widget tool to copilot if asking AI from selection in a note in edgeless mode.
- Remove `content` from `AIImageActionOptions`, using `input` as AI context like other actions.
This commit is contained in:
akumatus
2024-11-28 02:35:05 +00:00
parent 3c4e70a8c3
commit d2eef768ba
6 changed files with 16 additions and 29 deletions

View File

@@ -6,14 +6,12 @@ import type {
import { import {
type AIItemGroupConfig, type AIItemGroupConfig,
type AISubItemConfig, type AISubItemConfig,
type CopilotTool,
EDGELESS_ELEMENT_TOOLBAR_WIDGET, EDGELESS_ELEMENT_TOOLBAR_WIDGET,
type EdgelessElementToolbarWidget, type EdgelessElementToolbarWidget,
matchFlavours, matchFlavours,
} from '@blocksuite/affine/blocks'; } from '@blocksuite/affine/blocks';
import type { TemplateResult } from 'lit'; import type { TemplateResult } from 'lit';
import { TOGGLE_EMPTY_INPUT_ACTIONS } from '../actions/consts';
import { actionToHandler } from '../actions/doc-handler'; import { actionToHandler } from '../actions/doc-handler';
import { actionToHandler as edgelessActionToHandler } from '../actions/edgeless-handler'; import { actionToHandler as edgelessActionToHandler } from '../actions/edgeless-handler';
import { import {
@@ -255,17 +253,6 @@ function edgelessHandler<T extends keyof BlockSuitePresets.AIActions>(
const selectedElements = edgeless.service.selection.selectedElements; const selectedElements = edgeless.service.selection.selectedElements;
if (!selectedElements.length) return; if (!selectedElements.length) return;
edgeless.gfx.tool.setTool({ type: 'copilot' });
const currentController =
edgeless.gfx.tool.currentTool$.peek() as CopilotTool;
if (!currentController) {
edgeless.gfx.tool.setTool({ type: 'default' });
return;
}
currentController.updateDragPointsWith(selectedElements, 10);
currentController.draggingAreaUpdated.emit(false); // do not show edgeless panel
return edgelessActionToHandler( return edgelessActionToHandler(
id, id,
generatingIcon, generatingIcon,
@@ -281,11 +268,10 @@ function edgelessHandler<T extends keyof BlockSuitePresets.AIActions>(
selections?.selectedBlocks?.length === 1 && attachments.length > 0; selections?.selectedBlocks?.length === 1 && attachments.length > 0;
return { return {
attachments: sendAttachments ? attachments : undefined, attachments: sendAttachments ? attachments : undefined,
content: sendAttachments ? '' : markdown, input: sendAttachments ? '' : markdown,
}; };
}, },
trackerOptions, trackerOptions
TOGGLE_EMPTY_INPUT_ACTIONS.includes(id)
)(host); )(host);
} }
}; };

View File

@@ -27,4 +27,3 @@ export const generatingStages: {
}; };
export const INSERT_ABOVE_ACTIONS = ['createHeadings']; export const INSERT_ABOVE_ACTIONS = ['createHeadings'];
export const TOGGLE_EMPTY_INPUT_ACTIONS = ['createImage', 'makeItReal'];

View File

@@ -67,7 +67,8 @@ function actionToRenderer<T extends keyof BlockSuitePresets.AIActions>(
] as BlockSuite.EdgelessModel[]; ] as BlockSuite.EdgelessModel[];
if ( if (
isMindMapRoot(selectedElements[0] || isMindmapChild(selectedElements[0])) isMindMapRoot(selectedElements[0]) ||
isMindmapChild(selectedElements[0])
) { ) {
const mindmap = selectedElements[0].group as MindmapElementModel; const mindmap = selectedElements[0].group as MindmapElementModel;
@@ -426,8 +427,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
attachments?: (string | Blob)[]; attachments?: (string | Blob)[];
seed?: string; seed?: string;
} | void>, } | void>,
trackerOptions?: BlockSuitePresets.TrackerOptions, trackerOptions?: BlockSuitePresets.TrackerOptions
toggleEmptyInput?: boolean
) { ) {
return (host: EditorHost) => { return (host: EditorHost) => {
const aiPanel = getAIPanel(host); const aiPanel = getAIPanel(host);
@@ -490,7 +490,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
if (isCreateImageAction || isMakeItRealAction) { if (isCreateImageAction || isMakeItRealAction) {
togglePanel = async () => { togglePanel = async () => {
if (isEmpty || toggleEmptyInput) return true; if (isEmpty) return true;
const { notes, shapes, images, edgelessTexts, embedSyncedDocs } = const { notes, shapes, images, edgelessTexts, embedSyncedDocs } =
BlocksUtils.splitElements(selectedElements); BlocksUtils.splitElements(selectedElements);
const blocks = [ const blocks = [

View File

@@ -87,7 +87,6 @@ declare global {
} }
interface AIImageActionOptions extends AITextActionOptions { interface AIImageActionOptions extends AITextActionOptions {
content?: string;
seed?: string; seed?: string;
} }

View File

@@ -288,7 +288,7 @@ const generateGroup: AIItemGroupConfig = {
const content = aiPanel.inputText?.trim(); const content = aiPanel.inputText?.trim();
if (!content) return; if (!content) return;
return { return {
content, input: content,
}; };
} }
@@ -317,7 +317,7 @@ const generateGroup: AIItemGroupConfig = {
// text to image // text to image
if (content.length && images.length + pureShapes.length === 0) { if (content.length && images.length + pureShapes.length === 0) {
return { return {
content, input: content,
}; };
} }
@@ -337,7 +337,7 @@ const generateGroup: AIItemGroupConfig = {
const png = await canvasToBlob(canvas); const png = await canvasToBlob(canvas);
if (!png) return; if (!png) return;
return { return {
content, input: content,
attachments: [png], attachments: [png],
seed: String(randomSeed()), seed: String(randomSeed()),
}; };
@@ -418,7 +418,7 @@ const generateGroup: AIItemGroupConfig = {
const content = aiPanel.inputText?.trim(); const content = aiPanel.inputText?.trim();
if (!content) return; if (!content) return;
return { return {
content, input: content,
}; };
} }
@@ -444,7 +444,7 @@ const generateGroup: AIItemGroupConfig = {
(s === 1 && shapes[0] instanceof TextElementModel)) (s === 1 && shapes[0] instanceof TextElementModel))
) { ) {
return { return {
content, input: content,
}; };
} }
@@ -472,7 +472,7 @@ const generateGroup: AIItemGroupConfig = {
}); });
return { return {
content, input: content,
attachments: [png], attachments: [png],
}; };
} }

View File

@@ -296,7 +296,7 @@ export function setupAIProvider(client: CopilotClient) {
AIProvider.provide('makeItReal', options => { AIProvider.provide('makeItReal', options => {
let promptName: PromptKey = 'Make it real'; let promptName: PromptKey = 'Make it real';
let content = options.content || ''; let content = options.input || '';
// wireframes // wireframes
if (options.attachments?.length) { if (options.attachments?.length) {
@@ -374,6 +374,7 @@ Could you make a new website based on these notes and send back just the html fi
return toImage({ return toImage({
...options, ...options,
client, client,
content: options.input,
promptName, promptName,
}); });
}); });
@@ -384,6 +385,7 @@ Could you make a new website based on these notes and send back just the html fi
return toImage({ return toImage({
...options, ...options,
client, client,
content: options.input,
timeout: 120000, timeout: 120000,
promptName: promptName as PromptKey, promptName: promptName as PromptKey,
workflow: !!promptName?.startsWith('workflow:'), workflow: !!promptName?.startsWith('workflow:'),
@@ -398,6 +400,7 @@ Could you make a new website based on these notes and send back just the html fi
return toImage({ return toImage({
...options, ...options,
client, client,
content: options.input,
timeout: 120000, timeout: 120000,
promptName, promptName,
}); });