dia-bridge 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dia-bridge",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "把 Dia 浏览器内置 AI 变成 MCP 工具,给 Claude Code / Magpie 用(支持 macOS / Windows,Linux 可远程连)| Bridge Dia browser AI into MCP tools for Claude Code & Magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",
package/src/ax.mjs CHANGED
@@ -160,90 +160,86 @@ function isWritableTextArea(el) {
160
160
  }
161
161
 
162
162
  function inspectWindowTextAreas(win) {
163
+ var chatTextArea = null;
164
+ var fallbackTextArea = null;
165
+ var sendBtn = null;
166
+ var readOnlyBubbles = 0;
167
+ var btnPattern = /(send|submit|ask|arrow\\.up|发送|提交|傳送)/i;
168
+
169
+ // Native-only BFS skipping AXWebArea avoids traversing webpage DOMs (such as
170
+ // Google search textareas) and cuts AX scanning from ~16s down to ~1-2s.
163
171
  var queue = [{ el: win, depth: 0 }];
164
172
  var visited = 0;
165
- var maxNodes = 1500;
166
- var maxDepth = 18;
167
- var writable = null;
168
- var readOnlyBubbles = 0;
173
+ var maxNodes = 600;
174
+ var maxDepth = 24;
169
175
 
170
176
  while (queue.length > 0 && visited < maxNodes) {
171
177
  var item = queue.shift();
172
178
  visited += 1;
173
- var el = item.el;
174
- try {
175
- if (el.role() === "AXTextArea") {
176
- if (!writable && isWritableTextArea(el)) {
177
- writable = el;
178
- } else {
179
- var val = unwrapStr(el.value ? el.value() : "");
180
- if (val.trim().length > 0) {
181
- readOnlyBubbles += 1;
182
- }
183
- }
184
- }
185
- } catch (e) {}
186
- if (item.depth >= maxDepth) continue;
187
- var children = [];
179
+ var node = item.el;
180
+ var role = "";
188
181
  try {
189
- children = el.uiElements();
182
+ role = node.role();
190
183
  } catch (e) {
191
- children = [];
184
+ continue;
192
185
  }
193
- for (var i = 0; i < children.length; i++) {
194
- queue.push({ el: children[i], depth: item.depth + 1 });
186
+
187
+ // Never descend into webpage content (AXWebArea) so we never mistake a
188
+ // webpage's <textarea> for Dia's native AI chat input.
189
+ if (role === "AXWebArea") {
190
+ continue;
195
191
  }
196
- }
197
- return { textArea: writable, bubbles: readOnlyBubbles };
198
- }
199
192
 
200
- function findSendButton(textArea, win) {
201
- var roots = [];
202
- try {
203
- var p1 = textArea.attributes.byName("AXParent").value();
204
- if (p1) {
205
- roots.push(p1);
206
- try {
207
- var p2 = p1.attributes.byName("AXParent").value();
208
- if (p2) roots.push(p2);
209
- } catch (e) {}
193
+ if (role === "AXTextArea") {
194
+ if (isWritableTextArea(node)) {
195
+ var desc = "";
196
+ try { desc = unwrapStr(node.description ? node.description() : ""); } catch (e) {}
197
+ if (desc === "text entry area") {
198
+ chatTextArea = node;
199
+ // Active chat panel input found at shallow depth; stop scanning immediately
200
+ // instead of traversing dozens of read-only history bubbles.
201
+ break;
202
+ }
203
+ fallbackTextArea = node;
204
+ } else {
205
+ readOnlyBubbles += 1;
206
+ }
207
+ continue;
210
208
  }
211
- } catch (e) {}
212
- roots.push(win);
213
-
214
- var pattern = /(send|submit|ask|arrow\\.up|发送|提交)/i;
215
- for (var r = 0; r < roots.length; r++) {
216
- var queue = [{ el: roots[r], depth: 0 }];
217
- var visited = 0;
218
- while (queue.length > 0 && visited < 400) {
219
- var item = queue.shift();
220
- visited += 1;
221
- var el = item.el;
209
+
210
+ if (!sendBtn && role === "AXButton") {
222
211
  try {
223
- if (el.role() === "AXButton" && el.enabled() !== false) {
212
+ if (node.enabled() !== false) {
224
213
  var label = [
225
- unwrapStr(el.title ? el.title() : ""),
226
- unwrapStr(el.description ? el.description() : ""),
227
- unwrapStr(el.help ? el.help() : ""),
214
+ unwrapStr(node.title ? node.title() : ""),
215
+ unwrapStr(node.description ? node.description() : ""),
216
+ unwrapStr(node.help ? node.help() : ""),
228
217
  ].join(" ");
229
- if (pattern.test(label)) {
230
- return el;
218
+ if (btnPattern.test(label)) {
219
+ sendBtn = node;
231
220
  }
232
221
  }
233
222
  } catch (e) {}
234
- if (item.depth >= 6) continue;
235
- var children = [];
236
- try {
237
- children = el.uiElements();
238
- } catch (e) {
239
- children = [];
240
- }
241
- for (var i = 0; i < children.length; i++) {
242
- queue.push({ el: children[i], depth: item.depth + 1 });
243
- }
223
+ }
224
+
225
+ if (item.depth >= maxDepth) continue;
226
+ var children = [];
227
+ try {
228
+ children = node.uiElements();
229
+ } catch (e) {
230
+ children = [];
231
+ }
232
+ for (var j = 0; j < children.length; j++) {
233
+ queue.push({ el: children[j], depth: item.depth + 1 });
244
234
  }
245
235
  }
246
- return null;
236
+
237
+ return {
238
+ textArea: chatTextArea || fallbackTextArea,
239
+ isChatPanel: Boolean(chatTextArea),
240
+ sendBtn: sendBtn,
241
+ bubbles: readOnlyBubbles,
242
+ };
247
243
  }
248
244
 
249
245
  function postKeyCodeToPid(pid, keyCode) {
@@ -331,15 +327,23 @@ function run() {
331
327
  }
332
328
 
333
329
  var textArea = null;
334
- var targetWindow = null;
330
+ var isChatPanel = false;
331
+ var sendBtn = null;
335
332
  var bubbles = 0;
336
- for (var w = 0; w < windows.length; w++) {
337
- var inspected = inspectWindowTextAreas(windows[w]);
338
- if (inspected.textArea) {
339
- textArea = inspected.textArea;
340
- bubbles = inspected.bubbles;
341
- targetWindow = windows[w];
342
- break;
333
+ for (var attempt = 0; attempt < 3 && !textArea; attempt++) {
334
+ if (attempt > 0) {
335
+ delay(0.4);
336
+ try { windows = proc.windows(); } catch (e) {}
337
+ }
338
+ for (var w = 0; w < windows.length; w++) {
339
+ var inspected = inspectWindowTextAreas(windows[w]);
340
+ if (inspected.textArea) {
341
+ textArea = inspected.textArea;
342
+ isChatPanel = inspected.isChatPanel;
343
+ sendBtn = inspected.sendBtn || null;
344
+ bubbles = inspected.bubbles;
345
+ break;
346
+ }
343
347
  }
344
348
  }
345
349
  if (!textArea) {
@@ -362,8 +366,11 @@ function run() {
362
366
  focusTextArea(textArea);
363
367
 
364
368
  var submitMethod = "cg_return";
365
- var sendBtn = findSendButton(textArea, targetWindow);
366
- if (sendBtn) {
369
+ if (isChatPanel) {
370
+ // Active Dia AI chat panel submits directly on Return.
371
+ postReturnKeyToPid(pid);
372
+ submitMethod = "cg_return";
373
+ } else if (sendBtn) {
367
374
  try {
368
375
  sendBtn.actions.byName("AXPress").perform();
369
376
  submitMethod = "ax_send_button";
@@ -407,6 +414,7 @@ function run() {
407
414
  return JSON.stringify({
408
415
  ok: true,
409
416
  method: submitMethod,
417
+ isChatPanel: isChatPanel,
410
418
  bubbles: bubbles,
411
419
  pid: pid,
412
420
  length: promptText.length,
@@ -467,7 +475,7 @@ export async function submitPromptViaAX(
467
475
  prompt,
468
476
  {
469
477
  signal,
470
- timeoutMs = 15000,
478
+ timeoutMs = 25000,
471
479
  bundleId = DIA_BUNDLE_ID,
472
480
  execOsascript = runOsascript,
473
481
  } = {},
package/src/bridge.mjs CHANGED
@@ -126,7 +126,7 @@ export function createDiaBridge(config = {}) {
126
126
 
127
127
  const submitRes = await deps.submitPrompt(prompt, {
128
128
  signal,
129
- timeoutMs: Math.min(15000, remainingMs(backend.submitStage)),
129
+ timeoutMs: Math.min(25000, remainingMs(backend.submitStage)),
130
130
  bundleId,
131
131
  processName,
132
132
  })
package/src/store.mjs CHANGED
@@ -209,7 +209,13 @@ export function extractAnswerFromFeedbackEntry(entry) {
209
209
  const item = list[i]
210
210
  if (!item || typeof item !== "object") continue
211
211
  const role = String(item.role || item.author || item.sender || item.type || "").toLowerCase()
212
- if (role === "assistant" || role === "model" || role === "ai" || role === "final") {
212
+ const isAssistantTurn =
213
+ role === "assistant" ||
214
+ role === "model" ||
215
+ role === "ai" ||
216
+ role === "final" ||
217
+ (!role && key === "turns")
218
+ if (isAssistantTurn) {
213
219
  const text =
214
220
  extractTextFromContent(item.content) ||
215
221
  extractTextFromContent(item.text) ||