Skip to content

Commit 80c1a12

Browse files
luoxuanzaoQoder-AI
andauthored
feat(chat): accept OS file-manager drops as external context (#64)
Drag files and folders from the OS file manager onto the composer: directories and non-image files become external context roots (paths resolve through Electron webUtils.getPathForFile with a text/uri-list fallback), images keep attaching, and OS drags are always claimed so the dropped content is never pasted as text. External roots behave like vault folders for @ mentions — `@root/` inserts a folder chip and resolves to the root's absolute path at send time — the same chip renders in sent user bubbles (carrying the absolute path) and clicking it reveals the item in Finder/Explorer. Single-file contexts can be persisted across sessions, and message render failures now log the underlying error. Adds a protocol-driven real-app e2e for file drops. Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com>
1 parent 7aadd26 commit 80c1a12

34 files changed

Lines changed: 1057 additions & 134 deletions

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ version with its date and start a fresh empty `[Unreleased]` above it.
1111

1212
## [Unreleased]
1313

14+
### Added
15+
16+
- Show the added external context path after dropping a folder or file, surface
17+
rejected additions, and show the context count even for a single path.
18+
- Drag files and folders from your operating system (e.g. macOS Finder) onto
19+
the composer to add them as external context. Dropped folders join the
20+
external context as before, dropped images still attach to the message, and
21+
a dropped file's content is no longer pasted into the input as plain text.
22+
1423
### Fixed
1524

1625
- Hover dropdowns no longer lose their leading characters when their icon

‎CONTRIBUTING.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,27 @@ stream events should also be exercised manually against a real qodercli:
4242
npm run smoke:qoder
4343
```
4444

45-
This initialization-only check follows the official TypeScript model-selection
45+
The initialization-only SDK check follows the official TypeScript model-selection
4646
sample and does not send a model turn. Set `QODER_CLI_PATH` when qodercli is not
4747
available on PATH.
4848

49+
Folder-drop changes have a protocol-driven test in a running Obsidian/Electron app. Set
50+
`OBSIDIAN_VAULT` in `.env.local`, launch Obsidian with remote debugging enabled,
51+
open that vault, and run:
52+
53+
```bash
54+
/Applications/Obsidian.app/Contents/MacOS/Obsidian --remote-debugging-port=9222
55+
npm run test:e2e:file-drop
56+
```
57+
58+
The test builds and deploys the plugin into the configured test vault, reloads
59+
it, sends a folder drag through Chromium's input protocol, and verifies that
60+
the path becomes external context with visible feedback and no composer text
61+
change. It also checks dragover acceptance with a simulated host interceptor.
62+
This does not automate the Finder/Explorer mouse gesture; verify that separately.
63+
Set `QODERIAN_E2E_DROP_KIND=file` for the file case, or `OBSIDIAN_DEBUG_PORT` when
64+
using a port other than `9222`.
65+
4966
## Naming conventions
5067

5168
- TypeScript, TSX, and script file names use `kebab-case`, including

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"lint:names": "node scripts/check-file-names.mjs",
1818
"lint:fix": "npm run lint -- --fix",
1919
"test": "node scripts/run-jest.js",
20+
"test:e2e:file-drop": "node scripts/e2e-external-file-drop.mjs",
2021
"test:watch": "node scripts/run-jest.js --watch",
2122
"test:coverage": "node scripts/run-jest.js --coverage",
2223
"smoke:qoder": "node scripts/smoke-qoder-sdk.mjs",

‎scripts/e2e-external-file-drop.mjs‎

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
#!/usr/bin/env node
2+
3+
import { execFileSync } from 'child_process';
4+
import {
5+
copyFileSync,
6+
existsSync,
7+
mkdirSync,
8+
mkdtempSync,
9+
readFileSync,
10+
rmSync,
11+
writeFileSync,
12+
} from 'fs';
13+
import os from 'os';
14+
import path from 'path';
15+
import process from 'process';
16+
import { fileURLToPath } from 'url';
17+
18+
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
19+
const DEBUG_PORT = Number(process.env.OBSIDIAN_DEBUG_PORT || 9222);
20+
const DEBUG_ORIGIN = `http://127.0.0.1:${DEBUG_PORT}`;
21+
const PLUGIN_ID = 'qoderian';
22+
const DROP_MARKER = 'QODERIAN_E2E_FILE_CONTENT_MUST_NOT_BE_PASTED';
23+
24+
function loadLocalEnvironment() {
25+
const envPath = path.join(ROOT, '.env.local');
26+
if (!existsSync(envPath)) return;
27+
28+
for (const line of readFileSync(envPath, 'utf8').split(/\r?\n/)) {
29+
const match = line.match(/^([^#=]+)=["']?(.+?)["']?$/);
30+
if (match && !process.env[match[1].trim()]) {
31+
process.env[match[1].trim()] = match[2].trim();
32+
}
33+
}
34+
}
35+
36+
function deployPlugin(vaultPath) {
37+
execFileSync(process.execPath, ['scripts/build.mjs', 'production'], {
38+
cwd: ROOT,
39+
stdio: 'inherit',
40+
});
41+
42+
const pluginDir = path.join(vaultPath, '.obsidian', 'plugins', PLUGIN_ID);
43+
if (!existsSync(pluginDir)) {
44+
throw new Error(`Qoderian plugin directory does not exist: ${pluginDir}`);
45+
}
46+
for (const name of ['main.js', 'manifest.json', 'styles.css']) {
47+
copyFileSync(path.join(ROOT, name), path.join(pluginDir, name));
48+
}
49+
}
50+
51+
class CdpSession {
52+
constructor(url) {
53+
this.url = url;
54+
this.nextId = 1;
55+
this.pending = new Map();
56+
}
57+
58+
async connect() {
59+
this.socket = new WebSocket(this.url);
60+
await new Promise((resolve, reject) => {
61+
this.socket.addEventListener('open', resolve, { once: true });
62+
this.socket.addEventListener('error', reject, { once: true });
63+
});
64+
this.socket.addEventListener('message', (event) => {
65+
const message = JSON.parse(event.data);
66+
if (!message.id) return;
67+
const pending = this.pending.get(message.id);
68+
if (!pending) return;
69+
this.pending.delete(message.id);
70+
if (message.error) {
71+
pending.reject(new Error(`${pending.method}: ${message.error.message}`));
72+
} else {
73+
pending.resolve(message.result);
74+
}
75+
});
76+
}
77+
78+
send(method, params = {}) {
79+
const id = this.nextId++;
80+
return new Promise((resolve, reject) => {
81+
this.pending.set(id, { method, resolve, reject });
82+
this.socket.send(JSON.stringify({ id, method, params }));
83+
});
84+
}
85+
86+
async evaluate(expression, awaitPromise = false) {
87+
const result = await this.send('Runtime.evaluate', {
88+
expression,
89+
awaitPromise,
90+
returnByValue: true,
91+
});
92+
if (result.exceptionDetails) {
93+
throw new Error(result.exceptionDetails.exception?.description || 'Renderer evaluation failed');
94+
}
95+
return result.result.value;
96+
}
97+
98+
close() {
99+
this.socket.close();
100+
}
101+
}
102+
103+
async function retry(description, action, timeoutMs = 15_000) {
104+
const deadline = Date.now() + timeoutMs;
105+
let lastError;
106+
while (Date.now() < deadline) {
107+
try {
108+
const value = await action();
109+
if (value) return value;
110+
} catch (error) {
111+
lastError = error;
112+
}
113+
await new Promise(resolve => setTimeout(resolve, 150));
114+
}
115+
throw new Error(`${description} timed out${lastError ? `: ${lastError.message}` : ''}`);
116+
}
117+
118+
async function findVaultTarget(vaultName) {
119+
const targets = await retry('Obsidian DevTools endpoint', async () => {
120+
const response = await fetch(`${DEBUG_ORIGIN}/json/list`);
121+
if (!response.ok) return null;
122+
return response.json();
123+
});
124+
const target = targets.find(candidate =>
125+
candidate.type === 'page'
126+
&& candidate.url === 'app://obsidian.md/index.html'
127+
&& candidate.title.includes(vaultName)
128+
);
129+
if (!target) {
130+
throw new Error(`No Obsidian window for vault "${vaultName}" was found on ${DEBUG_ORIGIN}`);
131+
}
132+
return target;
133+
}
134+
135+
function visibleComposerStateExpression() {
136+
return `(() => {
137+
const wrapper = [...document.querySelectorAll('.qoderian-input-wrapper')]
138+
.find(element => element.getBoundingClientRect().width > 0);
139+
if (!wrapper) return null;
140+
const rect = wrapper.getBoundingClientRect();
141+
return {
142+
x: Math.round(rect.x + rect.width / 2),
143+
y: Math.round(rect.y + rect.height / 2),
144+
sourceValue: wrapper.querySelector('textarea.qoderian-input')?.value ?? '',
145+
editorText: wrapper.querySelector('.cm-content')?.textContent ?? '',
146+
};
147+
})()`;
148+
}
149+
150+
async function run() {
151+
loadLocalEnvironment();
152+
const vaultPath = process.env.OBSIDIAN_VAULT;
153+
if (!vaultPath || !existsSync(vaultPath)) {
154+
throw new Error('Set OBSIDIAN_VAULT in .env.local to an existing test vault.');
155+
}
156+
157+
deployPlugin(vaultPath);
158+
159+
const target = await findVaultTarget(path.basename(vaultPath));
160+
const cdp = new CdpSession(target.webSocketDebuggerUrl);
161+
await cdp.connect();
162+
163+
const tempDir = mkdtempSync(path.join(os.tmpdir(), 'qoderian-file-drop-e2e-'));
164+
const isFolder = process.env.QODERIAN_E2E_DROP_KIND !== 'file';
165+
const droppedPath = path.join(tempDir, isFolder ? '外部资料 folder' : 'external-context.txt');
166+
if (isFolder) mkdirSync(droppedPath);
167+
else writeFileSync(droppedPath, DROP_MARKER, 'utf8');
168+
169+
try {
170+
await cdp.evaluate(`(async () => {
171+
const plugin = app.plugins.getPlugin(${JSON.stringify(PLUGIN_ID)});
172+
const contexts = new Map([...(plugin?.getView()?.getTabManager()?.tabs ?? [])]
173+
.map(([id, tab]) => [id, tab.ui.externalContextSelector?.getExternalContexts() ?? []]));
174+
await app.plugins.disablePlugin(${JSON.stringify(PLUGIN_ID)});
175+
if (window.__qoderianE2EDragOverObserver) {
176+
window.removeEventListener('dragover', window.__qoderianE2EDragOverObserver, true);
177+
}
178+
if (window.__qoderianE2EHostBlocker) {
179+
document.removeEventListener('dragover', window.__qoderianE2EHostBlocker, true);
180+
}
181+
window.__qoderianE2EDragOverResults = [];
182+
window.__qoderianE2EDragOverObserver = event => {
183+
const reachesComposer = event.composedPath()
184+
.some(element => element?.classList?.contains('qoderian-input-wrapper'));
185+
const result = {
186+
accepted: false,
187+
reachesComposer,
188+
types: [...(event.dataTransfer?.types ?? [])],
189+
hostBlocked: false,
190+
};
191+
window.__qoderianE2EDragOverResults.push(result);
192+
const preventDefault = event.preventDefault.bind(event);
193+
event.preventDefault = () => {
194+
result.accepted = true;
195+
preventDefault();
196+
};
197+
};
198+
window.__qoderianE2EHostBlocker = event => {
199+
if (event.composedPath().some(element => element?.classList?.contains('qoderian-input-wrapper'))) {
200+
window.__qoderianE2EDragOverResults.push({
201+
accepted: event.defaultPrevented,
202+
reachesComposer: true,
203+
types: [...(event.dataTransfer?.types ?? [])],
204+
hostBlocked: true,
205+
});
206+
event.stopImmediatePropagation();
207+
}
208+
};
209+
window.addEventListener('dragover', window.__qoderianE2EDragOverObserver, true);
210+
await app.plugins.enablePlugin(${JSON.stringify(PLUGIN_ID)});
211+
await app.commands.executeCommandById(${JSON.stringify(`${PLUGIN_ID}:open-view`)});
212+
for (const [id, tab] of app.plugins.getPlugin(${JSON.stringify(PLUGIN_ID)}).getView().getTabManager().tabs) {
213+
if (contexts.has(id)) tab.ui.externalContextSelector?.setExternalContexts(contexts.get(id));
214+
}
215+
document.addEventListener('dragover', window.__qoderianE2EHostBlocker, true);
216+
return true;
217+
})()`, true);
218+
219+
const before = await retry('visible Qoderian composer', () =>
220+
cdp.evaluate(visibleComposerStateExpression()));
221+
222+
const dragData = {
223+
items: [{ mimeType: 'text/plain', data: DROP_MARKER }],
224+
files: [droppedPath],
225+
dragOperationsMask: 1,
226+
};
227+
for (const type of ['dragEnter', 'dragOver', 'drop']) {
228+
await cdp.send('Input.dispatchDragEvent', {
229+
type,
230+
x: before.x,
231+
y: before.y,
232+
data: dragData,
233+
});
234+
}
235+
236+
const after = await retry('dropped file to appear as external context', async () => {
237+
const state = await cdp.evaluate(`(() => {
238+
const path = ${JSON.stringify(droppedPath)};
239+
const context = [...document.querySelectorAll('.qoderian-external-context-text')]
240+
.find(element => element.getAttribute('title') === path);
241+
const composer = (${visibleComposerStateExpression()});
242+
const notice = [...document.querySelectorAll('.notice')]
243+
.some(element => element.textContent.includes(path));
244+
const badge = context?.closest('.qoderian-external-context-selector')
245+
?.querySelector('.qoderian-external-context-badge.visible');
246+
return context && composer && notice && badge
247+
? { composer, contextTitle: context.getAttribute('title') } : null;
248+
})()`);
249+
return state;
250+
});
251+
252+
const dragOverResults = await cdp.evaluate(
253+
`window.__qoderianE2EDragOverResults ?? []`,
254+
);
255+
const dragOverWasAccepted = dragOverResults.some(result => result.accepted);
256+
if (!dragOverWasAccepted) {
257+
throw new Error(
258+
`The window capture handler did not accept native dragover before the host intercepted it: ${JSON.stringify(dragOverResults)}`,
259+
);
260+
}
261+
262+
if (after.composer.sourceValue !== before.sourceValue
263+
|| after.composer.editorText !== before.editorText
264+
|| after.composer.sourceValue.includes(DROP_MARKER)
265+
|| after.composer.editorText.includes(DROP_MARKER)) {
266+
throw new Error('The dropped file content was inserted into the composer.');
267+
}
268+
269+
console.log(`PASS protocol-injected ${isFolder ? 'folder' : 'file'} drop -> external context: ${after.contextTitle}`);
270+
console.log('PASS added path shown in a notice and context count badge');
271+
console.log('PASS dragover accepted before simulated host interception');
272+
console.log('PASS dropped file content was not pasted into the composer');
273+
} finally {
274+
try {
275+
await cdp.evaluate(`(() => {
276+
const path = ${JSON.stringify(droppedPath)};
277+
const text = [...document.querySelectorAll('.qoderian-external-context-text')]
278+
.find(element => element.getAttribute('title') === path);
279+
text?.closest('.qoderian-external-context-item')
280+
?.querySelector('.qoderian-external-context-remove')?.click();
281+
if (window.__qoderianE2EDragOverObserver) {
282+
window.removeEventListener('dragover', window.__qoderianE2EDragOverObserver, true);
283+
}
284+
if (window.__qoderianE2EHostBlocker) {
285+
document.removeEventListener('dragover', window.__qoderianE2EHostBlocker, true);
286+
}
287+
delete window.__qoderianE2EDragOverObserver;
288+
delete window.__qoderianE2EHostBlocker;
289+
delete window.__qoderianE2EDragOverResults;
290+
return true;
291+
})()`);
292+
} catch {
293+
// Best-effort UI cleanup; the temporary file is always removed below.
294+
}
295+
cdp.close();
296+
rmSync(tempDir, { recursive: true, force: true });
297+
}
298+
}
299+
300+
run().catch((error) => {
301+
console.error(`E2E FAILED: ${error.message}`);
302+
console.error(`Launch Obsidian with --remote-debugging-port=${DEBUG_PORT} and open the OBSIDIAN_VAULT test vault.`);
303+
process.exit(1);
304+
});

‎src/core/context/context-mention-resolver.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,25 @@ export function resolveExternalMentionAtIndex(
8585
const separator = text[displayNameEnd];
8686
if (separator !== '/' && separator !== '\\') continue;
8787

88+
const pathStart = displayNameEnd + 1;
89+
// A bare `@root/` targets the external context root itself, mirroring how
90+
// a vault folder mention references the whole folder.
91+
if (pathStart >= text.length || isWhitespace(text[pathStart])) {
92+
const folderMatch: MentionLookupMatch = {
93+
resolvedPath: entry.contextRoot,
94+
endIndex: pathStart,
95+
trailingPunctuation: '',
96+
};
97+
if (!bestMatch || folderMatch.endIndex > bestMatch.endIndex) {
98+
bestMatch = folderMatch;
99+
}
100+
continue;
101+
}
102+
88103
const lookup = getContextLookup(entry.contextRoot);
89104
const match = findBestMentionLookupMatch(
90105
text,
91-
displayNameEnd + 1,
106+
pathStart,
92107
lookup,
93108
normalizeMentionPath,
94109
normalizeForPlatformLookup

0 commit comments

Comments
 (0)