dsh-plugin-agentdeck 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 +91 -2
- package/cordis.patch.yml +3 -0
- package/icon.svg +7 -0
- package/lib/client.js +1019 -0
- package/lib/index.js +233 -0
- package/lib/setup.js +1053 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +66 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
controlAgentDeckDaemon,
|
|
5
|
+
getAgentDeckStatus,
|
|
6
|
+
runAgentDeckSetup,
|
|
7
|
+
} from './setup.js';
|
|
8
|
+
|
|
9
|
+
export const name = 'agentdeck';
|
|
10
|
+
export const inject = ['webServer'];
|
|
11
|
+
|
|
12
|
+
const STATUS_PATH = '/api/agentdeck/status';
|
|
13
|
+
const SETUP_PATH = '/api/agentdeck/setup';
|
|
14
|
+
const DAEMON_PATH = '/api/agentdeck/daemon';
|
|
15
|
+
const MAX_BODY_BYTES = 16 * 1024;
|
|
16
|
+
|
|
17
|
+
function sendJson(res, statusCode, payload) {
|
|
18
|
+
const body = JSON.stringify(payload);
|
|
19
|
+
res.statusCode = statusCode;
|
|
20
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
21
|
+
res.setHeader('cache-control', 'no-store');
|
|
22
|
+
res.end(body);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function sendMethodNotAllowed(res, allow) {
|
|
26
|
+
res.statusCode = 405;
|
|
27
|
+
res.setHeader('allow', allow);
|
|
28
|
+
res.setHeader('cache-control', 'no-store');
|
|
29
|
+
res.end();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readBoundedBody(req, maxBytes = MAX_BODY_BYTES) {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const chunks = [];
|
|
35
|
+
let size = 0;
|
|
36
|
+
let exceeded = false;
|
|
37
|
+
req.on('data', (chunk) => {
|
|
38
|
+
size += chunk.length;
|
|
39
|
+
if (size > maxBytes) {
|
|
40
|
+
exceeded = true;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
chunks.push(chunk);
|
|
44
|
+
});
|
|
45
|
+
req.on('end', () => {
|
|
46
|
+
if (exceeded) {
|
|
47
|
+
resolve(null);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
resolve(Buffer.concat(chunks).toString('utf8'));
|
|
51
|
+
});
|
|
52
|
+
req.on('error', reject);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function parseJsonRequest(req, res) {
|
|
57
|
+
const contentType = String(req.headers['content-type'] || '')
|
|
58
|
+
.split(';', 1)[0]
|
|
59
|
+
.trim()
|
|
60
|
+
.toLowerCase();
|
|
61
|
+
if (contentType && contentType !== 'application/json') {
|
|
62
|
+
sendJson(res, 415, {
|
|
63
|
+
ok: false,
|
|
64
|
+
error: 'content-type must be application/json',
|
|
65
|
+
});
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let rawBody = '';
|
|
70
|
+
try {
|
|
71
|
+
rawBody = await readBoundedBody(req);
|
|
72
|
+
} catch {
|
|
73
|
+
sendJson(res, 400, { ok: false, error: 'request body unreadable' });
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
if (rawBody === null) {
|
|
77
|
+
sendJson(res, 413, { ok: false, error: 'request body too large' });
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!rawBody.trim()) return {};
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(rawBody);
|
|
84
|
+
} catch {
|
|
85
|
+
sendJson(res, 400, { ok: false, error: 'invalid JSON body' });
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getProfileInfo(ctx) {
|
|
91
|
+
const profile = ctx.get?.('profileContext');
|
|
92
|
+
const dshHome =
|
|
93
|
+
profile?.home || process.env.DSH_HOME || join(homedir(), '.dsh');
|
|
94
|
+
const profileName = profile?.name || 'web';
|
|
95
|
+
const sourcePatchPath =
|
|
96
|
+
profile?.patchPath ||
|
|
97
|
+
join(dshHome, 'profiles', profileName, 'cordis.patch.yml');
|
|
98
|
+
const packageManager = profile?.packageManager || null;
|
|
99
|
+
return { dshHome, profileName, sourcePatchPath, packageManager };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function apply(ctx) {
|
|
103
|
+
let opInFlight = null;
|
|
104
|
+
|
|
105
|
+
const isRejected = (req, res) => {
|
|
106
|
+
const connection = ctx.get?.('connection') ?? Reflect.get(ctx, 'connection');
|
|
107
|
+
const rejection = connection?.requestRejection?.(req);
|
|
108
|
+
if (rejection === undefined) return false;
|
|
109
|
+
res.statusCode = rejection;
|
|
110
|
+
res.end();
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
ctx.effect(
|
|
115
|
+
() =>
|
|
116
|
+
ctx.webServer.register({
|
|
117
|
+
kind: 'exact',
|
|
118
|
+
path: STATUS_PATH,
|
|
119
|
+
handler: async (req, res) => {
|
|
120
|
+
if (isRejected(req, res)) return;
|
|
121
|
+
if (req.method !== 'GET') {
|
|
122
|
+
sendMethodNotAllowed(res, 'GET');
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
const info = getProfileInfo(ctx);
|
|
127
|
+
const status = await getAgentDeckStatus(info);
|
|
128
|
+
sendJson(res, 200, status);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
sendJson(res, 500, {
|
|
131
|
+
ok: false,
|
|
132
|
+
error: err instanceof Error ? err.message : String(err),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
}),
|
|
137
|
+
`agentdeck: GET ${STATUS_PATH}`,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
ctx.effect(
|
|
141
|
+
() =>
|
|
142
|
+
ctx.webServer.register({
|
|
143
|
+
kind: 'exact',
|
|
144
|
+
path: SETUP_PATH,
|
|
145
|
+
handler: async (req, res) => {
|
|
146
|
+
if (isRejected(req, res)) return;
|
|
147
|
+
if (req.method !== 'POST') {
|
|
148
|
+
sendMethodNotAllowed(res, 'POST');
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const parsed = await parseJsonRequest(req, res);
|
|
152
|
+
if (parsed === null) return;
|
|
153
|
+
|
|
154
|
+
if (opInFlight) {
|
|
155
|
+
sendJson(res, 409, {
|
|
156
|
+
ok: false,
|
|
157
|
+
error: 'Another AgentDeck operation is already in progress',
|
|
158
|
+
});
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const info = getProfileInfo(ctx);
|
|
163
|
+
const task = runAgentDeckSetup({
|
|
164
|
+
...info,
|
|
165
|
+
syncModels: Boolean(parsed?.syncModels),
|
|
166
|
+
});
|
|
167
|
+
opInFlight = task;
|
|
168
|
+
try {
|
|
169
|
+
const result = await task;
|
|
170
|
+
sendJson(res, 200, result);
|
|
171
|
+
} catch (err) {
|
|
172
|
+
sendJson(res, 500, {
|
|
173
|
+
ok: false,
|
|
174
|
+
error: err instanceof Error ? err.message : String(err),
|
|
175
|
+
});
|
|
176
|
+
} finally {
|
|
177
|
+
if (opInFlight === task) opInFlight = null;
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
`agentdeck: POST ${SETUP_PATH}`,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
ctx.effect(
|
|
185
|
+
() =>
|
|
186
|
+
ctx.webServer.register({
|
|
187
|
+
kind: 'exact',
|
|
188
|
+
path: DAEMON_PATH,
|
|
189
|
+
handler: async (req, res) => {
|
|
190
|
+
if (isRejected(req, res)) return;
|
|
191
|
+
if (req.method !== 'POST') {
|
|
192
|
+
sendMethodNotAllowed(res, 'POST');
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const parsed = await parseJsonRequest(req, res);
|
|
196
|
+
if (parsed === null) return;
|
|
197
|
+
|
|
198
|
+
const action = parsed?.action;
|
|
199
|
+
if (!['start', 'stop', 'restart'].includes(action)) {
|
|
200
|
+
sendJson(res, 400, {
|
|
201
|
+
ok: false,
|
|
202
|
+
error: 'action must be "start", "stop", or "restart"',
|
|
203
|
+
});
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (opInFlight) {
|
|
208
|
+
sendJson(res, 409, {
|
|
209
|
+
ok: false,
|
|
210
|
+
error: 'Another AgentDeck operation is already in progress',
|
|
211
|
+
});
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const info = getProfileInfo(ctx);
|
|
216
|
+
const task = controlAgentDeckDaemon({ ...info, action });
|
|
217
|
+
opInFlight = task;
|
|
218
|
+
try {
|
|
219
|
+
const result = await task;
|
|
220
|
+
sendJson(res, 200, result);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
sendJson(res, 500, {
|
|
223
|
+
ok: false,
|
|
224
|
+
error: err instanceof Error ? err.message : String(err),
|
|
225
|
+
});
|
|
226
|
+
} finally {
|
|
227
|
+
if (opInFlight === task) opInFlight = null;
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
}),
|
|
231
|
+
`agentdeck: POST ${DAEMON_PATH}`,
|
|
232
|
+
);
|
|
233
|
+
}
|