dia-bridge 0.2.6 → 0.2.7
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 +1 -1
- package/skills/dia/SKILL.md +1 -0
- package/src/bridge.mjs +39 -10
- package/src/errors.mjs +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dia-bridge",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
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/skills/dia/SKILL.md
CHANGED
|
@@ -12,3 +12,4 @@ description: 直接调用本地 Dia 浏览器内置 AI(dia_ask)回答用户
|
|
|
12
12
|
3. 如果用户只输入了 `/dia status` 或询问连接状态,调用 `dia_status`。
|
|
13
13
|
4. 如果用户输入了 `/dia history` 或想看最近回答,调用 `dia_get_chat_history`。
|
|
14
14
|
5. 若调用报权限或未运行错误,先调用一次 `dia_status` 并将具体原因与修复提示简洁告诉用户。
|
|
15
|
+
6. 若 `dia_ask` 返回超时(TIMEOUT),说明 Dia 可能仍在生成长回答或交互式内容,不要反复重复提问;直接告知用户 Dia 仍在生成中,或稍等片刻后调用 `dia_get_chat_history` 读取结果。
|
package/src/bridge.mjs
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
export const DEFAULT_TIMEOUT_MS = 50000
|
|
24
24
|
export const MIN_TIMEOUT_MS = 1000
|
|
25
25
|
export const MAX_TIMEOUT_MS = 300000
|
|
26
|
+
export const DEFAULT_DEDUP_WINDOW_MS = 180000
|
|
26
27
|
|
|
27
28
|
export function normalizeTimeoutMs(value, fallback = DEFAULT_TIMEOUT_MS) {
|
|
28
29
|
if (value === undefined || value === null) return fallback
|
|
@@ -72,6 +73,10 @@ export function createDiaBridge(config = {}) {
|
|
|
72
73
|
const pollIntervalMs = config.pollIntervalMs ?? 350
|
|
73
74
|
const settleMs = config.settleMs ?? 400
|
|
74
75
|
const workFallbackSettleMs = config.workFallbackSettleMs ?? 2000
|
|
76
|
+
const dedupWindowMs =
|
|
77
|
+
typeof config.dedupWindowMs === "number" && config.dedupWindowMs >= 0
|
|
78
|
+
? config.dedupWindowMs
|
|
79
|
+
: DEFAULT_DEDUP_WINDOW_MS
|
|
75
80
|
|
|
76
81
|
const deps = {
|
|
77
82
|
checkEnv: config.deps?.checkEnv ?? backend.checkEnv,
|
|
@@ -84,6 +89,7 @@ export function createDiaBridge(config = {}) {
|
|
|
84
89
|
(() => config.contextsDir ?? getDefaultContextsDir({ platform })),
|
|
85
90
|
}
|
|
86
91
|
const log = createLogger("dia-bridge")
|
|
92
|
+
let pendingSubmission = null
|
|
87
93
|
|
|
88
94
|
async function ask(prompt, options = {}) {
|
|
89
95
|
if (typeof prompt !== "string" || prompt.trim().length === 0) {
|
|
@@ -120,17 +126,39 @@ export function createDiaBridge(config = {}) {
|
|
|
120
126
|
platform,
|
|
121
127
|
})
|
|
122
128
|
|
|
129
|
+
const promptKey = prompt.trim()
|
|
123
130
|
const contextsDir = deps.getContextsDir()
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
const canReusePending =
|
|
132
|
+
dedupWindowMs > 0 &&
|
|
133
|
+
pendingSubmission !== null &&
|
|
134
|
+
pendingSubmission.promptKey === promptKey &&
|
|
135
|
+
pendingSubmission.contextsDir === contextsDir &&
|
|
136
|
+
Date.now() - pendingSubmission.submittedAt < dedupWindowMs
|
|
137
|
+
|
|
138
|
+
let snapshot
|
|
139
|
+
if (canReusePending) {
|
|
140
|
+
snapshot = pendingSubmission.snapshot
|
|
141
|
+
log.debug("reusing pending submission after prior timeout/abort; skipping duplicate submit", {
|
|
142
|
+
length: prompt.length,
|
|
143
|
+
})
|
|
144
|
+
} else {
|
|
145
|
+
snapshot = await deps.captureSnapshot(contextsDir)
|
|
146
|
+
log.debug("captured snapshot", { contexts: snapshot.contexts.size })
|
|
147
|
+
|
|
148
|
+
const submitRes = await deps.submitPrompt(prompt, {
|
|
149
|
+
signal,
|
|
150
|
+
timeoutMs: Math.min(25000, remainingMs(backend.submitStage)),
|
|
151
|
+
bundleId,
|
|
152
|
+
processName,
|
|
153
|
+
})
|
|
154
|
+
pendingSubmission = {
|
|
155
|
+
promptKey,
|
|
156
|
+
snapshot,
|
|
157
|
+
contextsDir,
|
|
158
|
+
submittedAt: Date.now(),
|
|
159
|
+
}
|
|
160
|
+
log.debug("submitted prompt", { method: submitRes?.method, length: prompt.length })
|
|
161
|
+
}
|
|
134
162
|
|
|
135
163
|
const result = await deps.pollForNewAnswer(contextsDir, snapshot, {
|
|
136
164
|
signal,
|
|
@@ -141,6 +169,7 @@ export function createDiaBridge(config = {}) {
|
|
|
141
169
|
workFallbackSettleMs,
|
|
142
170
|
})
|
|
143
171
|
|
|
172
|
+
pendingSubmission = null
|
|
144
173
|
log.debug("received answer", { contextId: result.contextId, source: result.source })
|
|
145
174
|
return result.answer
|
|
146
175
|
} finally {
|
package/src/errors.mjs
CHANGED
|
@@ -57,7 +57,7 @@ export class DiaTimeoutError extends DiaBridgeError {
|
|
|
57
57
|
constructor(timeoutMs, stage = "waiting for Dia response") {
|
|
58
58
|
super(
|
|
59
59
|
"TIMEOUT",
|
|
60
|
-
`dia_ask timed out after ${timeoutMs}ms while ${stage}.
|
|
60
|
+
`dia_ask timed out after ${timeoutMs}ms while ${stage}. Dia may still be generating the reply — do not spam duplicate retries (use dia_get_chat_history after a short wait; retrying the exact same prompt within 3 minutes resumes waiting without re-submitting).`,
|
|
61
61
|
)
|
|
62
62
|
this.timeoutMs = timeoutMs
|
|
63
63
|
this.stage = stage
|