taskwindow 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 +2 -2
- package/src/bridge.js +64 -7
- package/src/tools.js +28 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "taskwindow",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "TaskWindow local daemon: bridges the TaskWindow Chrome extension to any MCP client over Streamable HTTP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"start": "node src/index.js",
|
|
12
|
-
"test": "node --test test/pairing.test.mjs test/extension-bootstrap.test.mjs test/extension-tabs.test.mjs test/tool-contract.test.mjs test/updates.test.mjs && node test/run.mjs"
|
|
12
|
+
"test": "node --test test/pairing.test.mjs test/extension-bootstrap.test.mjs test/extension-tabs.test.mjs test/bridge.test.mjs test/tool-contract.test.mjs test/updates.test.mjs && node test/run.mjs"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=18"
|
package/src/bridge.js
CHANGED
|
@@ -10,14 +10,27 @@ const PROTOCOL_VERSION = 1;
|
|
|
10
10
|
* tool calls go out as {type:"tool_call", id, tool, params} and come back as
|
|
11
11
|
* {type:"tool_result", id, ok, ...}. Token is checked at handshake time.
|
|
12
12
|
*/
|
|
13
|
+
const SLOW_ANSWER_MS = 5_000; // log tool calls the extension took longer than this
|
|
14
|
+
const EXPIRED_TTL_MS = 10 * 60_000; // how long a timed-out call is remembered, to name a late answer
|
|
15
|
+
|
|
13
16
|
export class Bridge {
|
|
14
|
-
|
|
17
|
+
/**
|
|
18
|
+
* `pingIntervalMs` / `silenceMs`: the extension sends an application-level
|
|
19
|
+
* ping every 20s, so 65s without any message from it means its end of the
|
|
20
|
+
* socket is dead (a laptop sleep can leave it half-open, readyState still
|
|
21
|
+
* OPEN) — tests shrink both.
|
|
22
|
+
*/
|
|
23
|
+
constructor({ token, logger = console, pingIntervalMs = 25_000, silenceMs = 65_000 }) {
|
|
15
24
|
this.token = token;
|
|
16
25
|
this.logger = logger;
|
|
17
26
|
this.ext = null; // active extension WebSocket
|
|
18
|
-
this.pending = new Map(); // tool_call id -> {resolve, reject, timer}
|
|
27
|
+
this.pending = new Map(); // tool_call id -> {resolve, reject, timer, tool, sentAt}
|
|
28
|
+
this.expired = new Map(); // tool_call id -> {tool, sentAt, timeoutMs}: timed out, answer may still come
|
|
19
29
|
this.lastHello = null;
|
|
30
|
+
this.lastExtMessage = 0;
|
|
20
31
|
this.wss = null;
|
|
32
|
+
this.pingIntervalMs = pingIntervalMs;
|
|
33
|
+
this.silenceMs = silenceMs;
|
|
21
34
|
}
|
|
22
35
|
|
|
23
36
|
start(httpServer) {
|
|
@@ -39,6 +52,7 @@ export class Bridge {
|
|
|
39
52
|
} catch {}
|
|
40
53
|
}
|
|
41
54
|
this.ext = ws;
|
|
55
|
+
this.lastExtMessage = Date.now();
|
|
42
56
|
|
|
43
57
|
ws.on("message", (raw) => this.#onMessage(ws, raw));
|
|
44
58
|
ws.on("close", () => {
|
|
@@ -52,13 +66,25 @@ export class Bridge {
|
|
|
52
66
|
ws.on("error", () => {});
|
|
53
67
|
});
|
|
54
68
|
|
|
55
|
-
// Ping to keep the MV3 service worker alive and
|
|
69
|
+
// Ping to keep the MV3 service worker alive, and drop a socket whose
|
|
70
|
+
// extension has gone silent: otherwise `connected` stays true, every tool
|
|
71
|
+
// call waits out its full timeout, and status reports a healthy extension
|
|
72
|
+
// while the popover says "not connected".
|
|
56
73
|
this.pingTimer = setInterval(() => {
|
|
57
74
|
if (this.ext && this.ext.readyState === 0 /* CONNECTING */) return;
|
|
75
|
+
if (this.ext && this.ext.readyState === 1 && Date.now() - this.lastExtMessage > this.silenceMs) {
|
|
76
|
+
this.logger.log(
|
|
77
|
+
`[bridge] extension silent for ${Math.round((Date.now() - this.lastExtMessage) / 1000)}s — dropping the stale connection`
|
|
78
|
+
);
|
|
79
|
+
try {
|
|
80
|
+
this.ext.terminate();
|
|
81
|
+
} catch {}
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
58
84
|
for (const client of this.wss.clients) {
|
|
59
85
|
if (client.readyState === 1 /* OPEN */) client.ping();
|
|
60
86
|
}
|
|
61
|
-
},
|
|
87
|
+
}, this.pingIntervalMs);
|
|
62
88
|
}
|
|
63
89
|
|
|
64
90
|
stop() {
|
|
@@ -85,12 +111,17 @@ export class Bridge {
|
|
|
85
111
|
);
|
|
86
112
|
}
|
|
87
113
|
const id = randomUUID();
|
|
114
|
+
const sentAt = Date.now();
|
|
88
115
|
return new Promise((resolve, reject) => {
|
|
89
116
|
const timer = setTimeout(() => {
|
|
90
117
|
this.pending.delete(id);
|
|
91
|
-
|
|
118
|
+
for (const [k, v] of this.expired) if (Date.now() - v.sentAt > EXPIRED_TTL_MS) this.expired.delete(k);
|
|
119
|
+
this.expired.set(id, { tool, sentAt, timeoutMs });
|
|
120
|
+
const err = new Error(`TaskWindow extension did not answer "${tool}" within ${Math.round(timeoutMs / 1000)}s`);
|
|
121
|
+
err.code = "TIMEOUT";
|
|
122
|
+
reject(err);
|
|
92
123
|
}, timeoutMs);
|
|
93
|
-
this.pending.set(id, { resolve, reject, timer, tool });
|
|
124
|
+
this.pending.set(id, { resolve, reject, timer, tool, sentAt });
|
|
94
125
|
this.ext.send(JSON.stringify({ type: "tool_call", id, tool, params }));
|
|
95
126
|
});
|
|
96
127
|
}
|
|
@@ -102,6 +133,7 @@ export class Bridge {
|
|
|
102
133
|
} catch {
|
|
103
134
|
return;
|
|
104
135
|
}
|
|
136
|
+
if (ws === this.ext) this.lastExtMessage = Date.now();
|
|
105
137
|
switch (msg.type) {
|
|
106
138
|
case "hello":
|
|
107
139
|
this.lastHello = msg;
|
|
@@ -114,9 +146,26 @@ export class Bridge {
|
|
|
114
146
|
break;
|
|
115
147
|
case "tool_result": {
|
|
116
148
|
const entry = this.pending.get(msg.id);
|
|
117
|
-
if (!entry)
|
|
149
|
+
if (!entry) {
|
|
150
|
+
// The extension did the work; nobody is waiting any more. Say so —
|
|
151
|
+
// "did not answer" alone reads as a dead extension, and the tab or
|
|
152
|
+
// click this answer describes really happened.
|
|
153
|
+
const late = this.expired.get(msg.id);
|
|
154
|
+
if (late) {
|
|
155
|
+
this.expired.delete(msg.id);
|
|
156
|
+
this.logger.log(
|
|
157
|
+
`[bridge] ${late.tool} answered ${((Date.now() - late.sentAt) / 1000).toFixed(1)}s after it was sent — ` +
|
|
158
|
+
`past its ${Math.round(late.timeoutMs / 1000)}s timeout, so the caller already got an error${this.#timing(msg)}`
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
118
163
|
clearTimeout(entry.timer);
|
|
119
164
|
this.pending.delete(msg.id);
|
|
165
|
+
const elapsed = Date.now() - entry.sentAt;
|
|
166
|
+
if (elapsed > SLOW_ANSWER_MS) {
|
|
167
|
+
this.logger.log(`[bridge] ${entry.tool} answered after ${(elapsed / 1000).toFixed(1)}s${this.#timing(msg)}`);
|
|
168
|
+
}
|
|
120
169
|
if (msg.ok) entry.resolve(msg.result ?? {});
|
|
121
170
|
else entry.reject(new Error(msg.error || `extension tool "${entry.tool}" failed`));
|
|
122
171
|
break;
|
|
@@ -126,6 +175,14 @@ export class Bridge {
|
|
|
126
175
|
}
|
|
127
176
|
}
|
|
128
177
|
|
|
178
|
+
/** Per-phase timing a handler reported (tabs_create does), for the slow/late log lines. */
|
|
179
|
+
#timing(msg) {
|
|
180
|
+
const timing = msg.result?.data?.timing;
|
|
181
|
+
if (!timing || typeof timing !== "object") return "";
|
|
182
|
+
const parts = Object.entries(timing).map(([k, v]) => `${k.replace(/Ms$/, "")} ${(v / 1000).toFixed(1)}s`);
|
|
183
|
+
return parts.length ? ` (${parts.join(", ")})` : "";
|
|
184
|
+
}
|
|
185
|
+
|
|
129
186
|
#failAllPending(err) {
|
|
130
187
|
for (const [, entry] of this.pending) {
|
|
131
188
|
clearTimeout(entry.timer);
|
package/src/tools.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
4
|
import os from "node:os";
|
|
4
5
|
import path from "node:path";
|
|
@@ -341,7 +342,18 @@ export function registerTools(server, { bridge, version, updates = null, logger
|
|
|
341
342
|
// save_to_disk is a daemon-side concern: the extension has no disk
|
|
342
343
|
// access, so it is stripped here and applied to the returned image.
|
|
343
344
|
const { save_to_disk, ...forwardedArgs } = args;
|
|
344
|
-
|
|
345
|
+
// A first tabs_create has no token yet; mint it here rather than in the
|
|
346
|
+
// extension so a call that times out on the way back can still name
|
|
347
|
+
// it. The retry with that token (same task and url) then gets the tab
|
|
348
|
+
// the first call opened, not a second tab in a second group.
|
|
349
|
+
if (def.name === "tabs_create" && !forwardedArgs.sessionToken) forwardedArgs.sessionToken = randomUUID();
|
|
350
|
+
let result;
|
|
351
|
+
try {
|
|
352
|
+
result = await bridge.sendTool(def.name, forwardedArgs, def.timeoutMs);
|
|
353
|
+
} catch (err) {
|
|
354
|
+
if (err.code !== "TIMEOUT") throw err;
|
|
355
|
+
throw new Error(`${err.message}. ${timeoutAdvice(def.name, forwardedArgs)}`);
|
|
356
|
+
}
|
|
345
357
|
if (save_to_disk && result?.image) {
|
|
346
358
|
try {
|
|
347
359
|
const savedPath = await saveImageToDisk(result.image);
|
|
@@ -368,6 +380,21 @@ export function registerTools(server, { bridge, version, updates = null, logger
|
|
|
368
380
|
}
|
|
369
381
|
}
|
|
370
382
|
|
|
383
|
+
/**
|
|
384
|
+
* What to do after the extension missed a tool's deadline. Chrome is slow to
|
|
385
|
+
* wake, not dead: the call usually completes seconds later (the daemon logs
|
|
386
|
+
* it), so a blind repeat doubles whatever it did.
|
|
387
|
+
*/
|
|
388
|
+
function timeoutAdvice(tool, args) {
|
|
389
|
+
if (tool === "tabs_create") {
|
|
390
|
+
return (
|
|
391
|
+
`The tab may still be opening. Retry this exact call with sessionToken "${args.sessionToken}" ` +
|
|
392
|
+
"(same task and url): it returns the tab the first call opened instead of opening another."
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
return "Chrome may still complete the action; check the page (screenshot or read_page) before repeating anything that has side effects.";
|
|
396
|
+
}
|
|
397
|
+
|
|
371
398
|
/**
|
|
372
399
|
* Write an image result to the session temp dir (OS-cleaned, like Claude's
|
|
373
400
|
* chrome extension does). Timestamped naming so repeat shots never overwrite.
|