refactor(core): show selected content chip if needed (#13415)

> CLOSE AF-2784

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved reliability when handling AI chat actions by ensuring valid
context is present before proceeding.
* Enhanced error handling and logging for failed context extraction in
AI chat features.

* **New Features**
* Context extraction is now performed asynchronously before opening the
AI Chat, providing more accurate and relevant chat context.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
德布劳外 · 贾贵
2025-08-06 09:39:25 +08:00
committed by GitHub
parent f303ec14df
commit 40ccb7642c
2 changed files with 34 additions and 9 deletions

View File

@@ -211,13 +211,32 @@ export class AIChatComposer extends SignalWatcher(
if (!params) return;
const { context, host } = params;
if (this.host !== host) return;
if (this.host !== host || !context) return;
if (context) {
this.updateContext(context);
if (
context.attachments ||
context.snapshot ||
context.combinedElementsMarkdown
) {
// Wait for context value updated next frame
setTimeout(() => {
this.addSelectedContextChip().catch(console.error);
}, 0);
}
})
);
this._disposables.add(
AIProvider.slots.requestSendWithChat.subscribe(params => {
if (!params) return;
if (context?.attachments) {
const { context, host } = params;
if (this.host !== host || !context) return;
if (
context.attachments ||
context.snapshot ||
context.combinedElementsMarkdown
) {
// Wait for context value updated next frame
setTimeout(() => {
this.addSelectedContextChip().catch(console.error);

View File

@@ -52,6 +52,7 @@ import type { AIItemGroupConfig } from '../../components/ai-item/types';
import { AIProvider } from '../../provider';
import { getAIPanelWidget } from '../../utils/ai-widgets';
import { mindMapToMarkdown } from '../../utils/edgeless';
import { extractSelectedContent } from '../../utils/extract';
import { canvasToBlob, randomSeed } from '../../utils/image';
import {
getCopilotSelectedElems,
@@ -114,11 +115,16 @@ const othersGroup: AIItemGroupConfig = {
showWhen: () => true,
handler: host => {
const panel = getAIPanelWidget(host);
AIProvider.slots.requestOpenWithChat.next({
host,
mode: 'edgeless',
autoSelect: true,
});
extractSelectedContent(host)
.then(context => {
AIProvider.slots.requestOpenWithChat.next({
host,
mode: 'edgeless',
autoSelect: true,
context,
});
})
.catch(console.error);
panel.hide();
},
},