dsh-plugin-browser4agent 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +54 -2
- package/cordis.patch.yml +3 -0
- package/icon.svg +9 -0
- package/lib/client.js +888 -0
- package/lib/index.js +218 -0
- package/lib/setup.js +902 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +67 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getBrowser4AgentStatus,
|
|
3
|
+
runBrowser4AgentSetup,
|
|
4
|
+
} from './setup.js';
|
|
5
|
+
|
|
6
|
+
export const name = 'browser4agent';
|
|
7
|
+
export const inject = ['webServer'];
|
|
8
|
+
|
|
9
|
+
const STATUS_PATH = '/api/browser4agent/status';
|
|
10
|
+
const SETUP_PATH = '/api/browser4agent/setup';
|
|
11
|
+
const MAX_BODY_BYTES = 16 * 1024;
|
|
12
|
+
|
|
13
|
+
function sendJson(res, statusCode, payload) {
|
|
14
|
+
const body = JSON.stringify(payload);
|
|
15
|
+
res.statusCode = statusCode;
|
|
16
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
17
|
+
res.setHeader('cache-control', 'no-store');
|
|
18
|
+
res.end(body);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function sendMethodNotAllowed(res, allow) {
|
|
22
|
+
res.statusCode = 405;
|
|
23
|
+
res.setHeader('allow', allow);
|
|
24
|
+
res.setHeader('cache-control', 'no-store');
|
|
25
|
+
res.end();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function readBoundedBody(req, maxBytes = MAX_BODY_BYTES) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const chunks = [];
|
|
31
|
+
let size = 0;
|
|
32
|
+
let exceeded = false;
|
|
33
|
+
req.on('data', (chunk) => {
|
|
34
|
+
size += chunk.length;
|
|
35
|
+
if (size > maxBytes) {
|
|
36
|
+
exceeded = true;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
chunks.push(chunk);
|
|
40
|
+
});
|
|
41
|
+
req.on('end', () => {
|
|
42
|
+
if (exceeded) {
|
|
43
|
+
resolve(null);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
resolve(Buffer.concat(chunks).toString('utf8'));
|
|
47
|
+
});
|
|
48
|
+
req.on('error', reject);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function parseJsonRequest(req, res) {
|
|
53
|
+
const contentType = String(req.headers['content-type'] || '')
|
|
54
|
+
.split(';', 1)[0]
|
|
55
|
+
.trim()
|
|
56
|
+
.toLowerCase();
|
|
57
|
+
if (contentType !== 'application/json') {
|
|
58
|
+
sendJson(res, 415, {
|
|
59
|
+
ok: false,
|
|
60
|
+
error: 'content-type must be application/json',
|
|
61
|
+
});
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let rawBody = '';
|
|
66
|
+
try {
|
|
67
|
+
rawBody = await readBoundedBody(req);
|
|
68
|
+
} catch {
|
|
69
|
+
sendJson(res, 400, { ok: false, error: 'request body unreadable' });
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
if (rawBody === null) {
|
|
73
|
+
sendJson(res, 413, { ok: false, error: 'request body too large' });
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!rawBody.trim()) return {};
|
|
78
|
+
try {
|
|
79
|
+
return JSON.parse(rawBody);
|
|
80
|
+
} catch {
|
|
81
|
+
sendJson(res, 400, { ok: false, error: 'invalid JSON body' });
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function apply(ctx) {
|
|
87
|
+
let opInFlight = null;
|
|
88
|
+
let disposed = false;
|
|
89
|
+
let provisioning = { state: 'idle', step: null, error: null };
|
|
90
|
+
|
|
91
|
+
const startProvision = ({ force = false, forceDownload = false } = {}) => {
|
|
92
|
+
if (opInFlight || disposed) return opInFlight;
|
|
93
|
+
provisioning = { state: 'running', step: 'checking', error: null };
|
|
94
|
+
const task = (async () => {
|
|
95
|
+
try {
|
|
96
|
+
if (!force && !forceDownload) {
|
|
97
|
+
const current = await getBrowser4AgentStatus({ probeTabs: false });
|
|
98
|
+
if (current.setupComplete || disposed) {
|
|
99
|
+
provisioning = { state: 'idle', step: null, error: null };
|
|
100
|
+
return { ok: true, status: current };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const result = await runBrowser4AgentSetup({
|
|
104
|
+
forceDownload,
|
|
105
|
+
onStep: (step) => {
|
|
106
|
+
if (!disposed) {
|
|
107
|
+
provisioning = { state: 'running', step, error: null };
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
provisioning = { state: 'idle', step: null, error: null };
|
|
112
|
+
return result;
|
|
113
|
+
} catch (err) {
|
|
114
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
115
|
+
provisioning = { state: 'error', step: null, error: message };
|
|
116
|
+
throw err;
|
|
117
|
+
} finally {
|
|
118
|
+
if (opInFlight === task) opInFlight = null;
|
|
119
|
+
}
|
|
120
|
+
})();
|
|
121
|
+
opInFlight = task;
|
|
122
|
+
return task;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// Automatically install browser4agent CLI and run `--setup skills` in the
|
|
126
|
+
// background when the plugin activates so Native Messaging Host + DSH Skill
|
|
127
|
+
// are ready without manual terminal steps.
|
|
128
|
+
ctx.effect(() => {
|
|
129
|
+
disposed = false;
|
|
130
|
+
startProvision().catch(() => {
|
|
131
|
+
// Error is captured in `provisioning` and surfaced in the Settings UI
|
|
132
|
+
});
|
|
133
|
+
return () => {
|
|
134
|
+
disposed = true;
|
|
135
|
+
};
|
|
136
|
+
}, 'browser4agent: auto-provision');
|
|
137
|
+
|
|
138
|
+
const isRejected = (req, res) => {
|
|
139
|
+
const connection = ctx.get?.('connection') ?? Reflect.get(ctx, 'connection');
|
|
140
|
+
const rejection = connection?.requestRejection?.(req);
|
|
141
|
+
if (rejection === undefined) return false;
|
|
142
|
+
res.statusCode = rejection;
|
|
143
|
+
res.end();
|
|
144
|
+
return true;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const buildStatusPayload = async () => {
|
|
148
|
+
const status = await getBrowser4AgentStatus({ probeTabs: true });
|
|
149
|
+
return { ...status, provisioning };
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
ctx.effect(
|
|
153
|
+
() =>
|
|
154
|
+
ctx.webServer.register({
|
|
155
|
+
kind: 'exact',
|
|
156
|
+
path: STATUS_PATH,
|
|
157
|
+
handler: async (req, res) => {
|
|
158
|
+
if (isRejected(req, res)) return;
|
|
159
|
+
if (req.method !== 'GET') {
|
|
160
|
+
sendMethodNotAllowed(res, 'GET');
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
sendJson(res, 200, await buildStatusPayload());
|
|
165
|
+
} catch (err) {
|
|
166
|
+
sendJson(res, 500, {
|
|
167
|
+
ok: false,
|
|
168
|
+
error: err instanceof Error ? err.message : String(err),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
}),
|
|
173
|
+
`browser4agent: GET ${STATUS_PATH}`,
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
ctx.effect(
|
|
177
|
+
() =>
|
|
178
|
+
ctx.webServer.register({
|
|
179
|
+
kind: 'exact',
|
|
180
|
+
path: SETUP_PATH,
|
|
181
|
+
handler: async (req, res) => {
|
|
182
|
+
if (isRejected(req, res)) return;
|
|
183
|
+
if (req.method !== 'POST') {
|
|
184
|
+
sendMethodNotAllowed(res, 'POST');
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const parsed = await parseJsonRequest(req, res);
|
|
188
|
+
if (parsed === null) return;
|
|
189
|
+
|
|
190
|
+
if (opInFlight) {
|
|
191
|
+
sendJson(res, 202, {
|
|
192
|
+
ok: true,
|
|
193
|
+
status: await buildStatusPayload(),
|
|
194
|
+
});
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
const result = await startProvision({
|
|
200
|
+
force: true,
|
|
201
|
+
forceDownload: Boolean(parsed?.forceDownload),
|
|
202
|
+
});
|
|
203
|
+
sendJson(res, 200, {
|
|
204
|
+
ok: true,
|
|
205
|
+
status: { ...result.status, provisioning },
|
|
206
|
+
});
|
|
207
|
+
} catch (err) {
|
|
208
|
+
sendJson(res, 500, {
|
|
209
|
+
ok: false,
|
|
210
|
+
error: err instanceof Error ? err.message : String(err),
|
|
211
|
+
status: await buildStatusPayload(),
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
}),
|
|
216
|
+
`browser4agent: POST ${SETUP_PATH}`,
|
|
217
|
+
);
|
|
218
|
+
}
|