ms-vite-plugin 1.4.14 → 1.4.16
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/dist/cli.js +1002 -9
- package/dist/mcp/device-log.d.ts +7 -22
- package/dist/mcp/device-log.js +20 -383
- package/dist/mcp/doc-tools.d.ts +0 -8
- package/dist/mcp/doc-tools.js +40 -93
- package/dist/mcp/docs-service.d.ts +0 -23
- package/dist/mcp/docs-service.js +0 -40
- package/dist/mcp/httpapi-docs-service.d.ts +9 -0
- package/dist/mcp/httpapi-docs-service.js +3 -7
- package/dist/mcp/httpapi-tools.js +3 -1
- package/dist/mcp/image-tools.d.ts +69 -0
- package/dist/mcp/image-tools.js +163 -0
- package/dist/mcp/ocr-tools.d.ts +50 -0
- package/dist/mcp/ocr-tools.js +59 -48
- package/dist/mcp/runtime-tools.js +16 -23
- package/dist/mcp/tool-utils.d.ts +3 -2
- package/dist/mcp/tool-utils.js +9 -3
- package/dist/mcp/tools.js +0 -1
- package/dist/project.d.ts +15 -40
- package/dist/project.js +36 -186
- package/docs/AGENTS.md +46 -36
- package/docs/SKILL.md +46 -29
- package/docs/api/paddleocr.md +11 -33
- package/docs/apicn/paddleocr.md +5 -29
- package/docs/apipython/paddleocr.md +34 -55
- package/docs/mcp-agent-description.md +53 -43
- package/package.json +1 -1
package/dist/mcp/device-log.d.ts
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
|
+
import type { RuntimeHttpTarget } from "./tool-utils";
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 确保后台日志订阅处于目标设备上
|
|
8
|
-
* - 若目标未变化且订阅仍在运行,则直接复用已有连接
|
|
9
|
-
* - 若目标发生变化,则重置缓存并重新建立订阅
|
|
10
|
-
* @param ip 设备 IP
|
|
11
|
-
* @param port 设备端口
|
|
12
|
-
* @returns 返回是否复用了现有订阅
|
|
13
|
-
* @example
|
|
14
|
-
* const reused = ensureDeviceLogSubscription("192.168.1.10", 9800)
|
|
15
|
-
*/
|
|
16
|
-
export declare function ensureDeviceLogSubscription(ip: string, port: number): boolean;
|
|
17
|
-
/**
|
|
18
|
-
* 生成日志缓存快照文本
|
|
19
|
-
* @param limit 需要返回的日志条数
|
|
20
|
-
* @param runtimeStatusLimit 需要返回的 runtime_status 条数
|
|
21
|
-
* @returns 返回用于 MCP 文本响应的快照内容
|
|
3
|
+
* 构建当前设备日志文本快照
|
|
4
|
+
* @param target 当前默认 HTTP 设备
|
|
5
|
+
* @param limit 需要返回的最新日志行数
|
|
6
|
+
* @returns 返回可直接作为 MCP 文本响应的日志内容
|
|
22
7
|
* @example
|
|
23
|
-
* const text = buildDeviceLogSnapshotText(
|
|
8
|
+
* const text = await buildDeviceLogSnapshotText({ ip: "192.168.1.10", port: "9800", label: "192.168.1.10:9800" }, 200)
|
|
24
9
|
*/
|
|
25
|
-
export declare function buildDeviceLogSnapshotText(
|
|
10
|
+
export declare function buildDeviceLogSnapshotText(target: RuntimeHttpTarget, limit: number): Promise<string>;
|
package/dist/mcp/device-log.js
CHANGED
|
@@ -1,390 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEVICE_LOG_MEMORY_LIMIT = void 0;
|
|
4
|
-
exports.ensureDeviceLogSubscription = ensureDeviceLogSubscription;
|
|
5
3
|
exports.buildDeviceLogSnapshotText = buildDeviceLogSnapshotText;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"connected",
|
|
23
|
-
"reconnecting",
|
|
24
|
-
]);
|
|
25
|
-
/**
|
|
26
|
-
* 进程级设备日志订阅状态
|
|
27
|
-
* - 当前 MCP 仅维护单设备日志缓存
|
|
28
|
-
* - MCP Tool 处理函数会复用同一份内存缓存
|
|
29
|
-
*/
|
|
30
|
-
const deviceLogSubscriptionState = {
|
|
31
|
-
target: null,
|
|
32
|
-
status: "idle",
|
|
33
|
-
lastError: null,
|
|
34
|
-
lastEventAt: null,
|
|
35
|
-
startedAt: null,
|
|
36
|
-
logs: [],
|
|
37
|
-
runtimeStatus: [],
|
|
38
|
-
generation: 0,
|
|
39
|
-
controller: null,
|
|
40
|
-
reconnectTimer: null,
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* 向环形缓冲区追加数据
|
|
44
|
-
* @param buffer 目标数组
|
|
45
|
-
* @param entry 待写入的数据
|
|
46
|
-
* @param limit 数组最大长度
|
|
47
|
-
* @returns 无返回值
|
|
48
|
-
* @example
|
|
49
|
-
* pushRingBuffer([1, 2], 3, 2)
|
|
50
|
-
*/
|
|
51
|
-
function pushRingBuffer(buffer, entry, limit) {
|
|
52
|
-
if (buffer.length >= limit) {
|
|
53
|
-
buffer.splice(0, buffer.length - limit + 1);
|
|
54
|
-
}
|
|
55
|
-
buffer.push(entry);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* 重置当前日志缓存快照
|
|
59
|
-
* @returns 无返回值
|
|
60
|
-
* @example
|
|
61
|
-
* resetDeviceLogSnapshotState()
|
|
62
|
-
*/
|
|
63
|
-
function resetDeviceLogSnapshotState() {
|
|
64
|
-
deviceLogSubscriptionState.lastError = null;
|
|
65
|
-
deviceLogSubscriptionState.lastEventAt = null;
|
|
66
|
-
deviceLogSubscriptionState.startedAt = new Date().toISOString();
|
|
67
|
-
deviceLogSubscriptionState.logs = [];
|
|
68
|
-
deviceLogSubscriptionState.runtimeStatus = [];
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* 判断日志订阅是否仍然活跃
|
|
72
|
-
* @returns 活跃返回 true,否则返回 false
|
|
73
|
-
* @example
|
|
74
|
-
* const active = isDeviceLogSubscriptionActive()
|
|
75
|
-
*/
|
|
76
|
-
function isDeviceLogSubscriptionActive() {
|
|
77
|
-
return (ACTIVE_DEVICE_LOG_SUBSCRIPTION_STATUSES.has(deviceLogSubscriptionState.status) || deviceLogSubscriptionState.reconnectTimer !== null);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* 解析单个 SSE 文本块
|
|
81
|
-
* @param rawBlock 原始 SSE 文本块
|
|
82
|
-
* @returns 返回事件名与 data 字符串
|
|
83
|
-
* @example
|
|
84
|
-
* parseSseEventBlock("event: log\ndata: {\"message\":\"ok\"}")
|
|
85
|
-
*/
|
|
86
|
-
function parseSseEventBlock(rawBlock) {
|
|
87
|
-
const lines = rawBlock.split(/\r?\n/);
|
|
88
|
-
let eventName = "message";
|
|
89
|
-
const dataLines = [];
|
|
90
|
-
for (const line of lines) {
|
|
91
|
-
if (line.startsWith("event:")) {
|
|
92
|
-
eventName = line.slice(6).trim() || "message";
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
if (line.startsWith("data:")) {
|
|
96
|
-
dataLines.push(line.slice(5).trimStart());
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return {
|
|
100
|
-
event: eventName,
|
|
101
|
-
data: dataLines.join("\n"),
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* 将原始日志对象标准化为统一结构
|
|
106
|
-
* @param payload 日志原始对象
|
|
107
|
-
* @returns 返回标准化后的日志
|
|
108
|
-
* @example
|
|
109
|
-
* normalizeBufferedLogEntry({ level: "info", message: "started" })
|
|
110
|
-
*/
|
|
111
|
-
function normalizeBufferedLogEntry(payload) {
|
|
112
|
-
const levelText = String(payload.level ?? "info").toLowerCase();
|
|
113
|
-
const messageText = String(payload.message ?? "");
|
|
114
|
-
const timestampText = typeof payload.timestamp === "string" && payload.timestamp
|
|
115
|
-
? payload.timestamp
|
|
116
|
-
: new Date().toISOString();
|
|
117
|
-
return {
|
|
118
|
-
level: levelText,
|
|
119
|
-
message: messageText,
|
|
120
|
-
timestamp: timestampText,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* 清理当前后台日志订阅
|
|
125
|
-
* @returns 无返回值
|
|
126
|
-
* @example
|
|
127
|
-
* stopDeviceLogSubscription()
|
|
128
|
-
*/
|
|
129
|
-
function stopDeviceLogSubscription() {
|
|
130
|
-
if (deviceLogSubscriptionState.reconnectTimer) {
|
|
131
|
-
clearTimeout(deviceLogSubscriptionState.reconnectTimer);
|
|
132
|
-
deviceLogSubscriptionState.reconnectTimer = null;
|
|
133
|
-
}
|
|
134
|
-
if (deviceLogSubscriptionState.controller) {
|
|
135
|
-
deviceLogSubscriptionState.controller.abort();
|
|
136
|
-
deviceLogSubscriptionState.controller = null;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* 安排后台重连
|
|
141
|
-
* @param generation 当前订阅代次
|
|
142
|
-
* @returns 无返回值
|
|
143
|
-
* @example
|
|
144
|
-
* scheduleDeviceLogReconnect(1)
|
|
145
|
-
*/
|
|
146
|
-
function scheduleDeviceLogReconnect(generation) {
|
|
147
|
-
if (generation !== deviceLogSubscriptionState.generation ||
|
|
148
|
-
!deviceLogSubscriptionState.target) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
if (deviceLogSubscriptionState.reconnectTimer) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
deviceLogSubscriptionState.status = "reconnecting";
|
|
155
|
-
deviceLogSubscriptionState.reconnectTimer = setTimeout(() => {
|
|
156
|
-
deviceLogSubscriptionState.reconnectTimer = null;
|
|
157
|
-
void startDeviceLogSubscriptionLoop(generation);
|
|
158
|
-
}, DEVICE_LOG_RECONNECT_DELAY_MS);
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* 启动一次 SSE 读取循环
|
|
162
|
-
* - 该函数仅负责单次连接;若连接中断,会由外层自动调度重连
|
|
163
|
-
* @param generation 当前订阅代次
|
|
164
|
-
* @returns 读取流程结束后返回 Promise<void>
|
|
165
|
-
* @example
|
|
166
|
-
* await startDeviceLogSubscriptionLoop(1)
|
|
167
|
-
*/
|
|
168
|
-
async function startDeviceLogSubscriptionLoop(generation) {
|
|
169
|
-
if (generation !== deviceLogSubscriptionState.generation ||
|
|
170
|
-
!deviceLogSubscriptionState.target) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
const { ip, port } = deviceLogSubscriptionState.target;
|
|
174
|
-
const controller = new AbortController();
|
|
175
|
-
const url = `http://${ip}:${port}/logger/sse`;
|
|
176
|
-
deviceLogSubscriptionState.controller = controller;
|
|
177
|
-
deviceLogSubscriptionState.status =
|
|
178
|
-
deviceLogSubscriptionState.lastEventAt === null
|
|
179
|
-
? "connecting"
|
|
180
|
-
: "reconnecting";
|
|
181
|
-
try {
|
|
182
|
-
const response = await fetch(url, {
|
|
183
|
-
method: "GET",
|
|
184
|
-
signal: controller.signal,
|
|
185
|
-
headers: {
|
|
186
|
-
Accept: "text/event-stream",
|
|
187
|
-
},
|
|
188
|
-
});
|
|
189
|
-
if (!response.ok || !response.body) {
|
|
190
|
-
throw new Error(`连接日志流失败: ${response.status} ${response.statusText || "unknown"}`);
|
|
191
|
-
}
|
|
192
|
-
deviceLogSubscriptionState.status = "connected";
|
|
193
|
-
deviceLogSubscriptionState.lastError = null;
|
|
194
|
-
const reader = response.body.getReader();
|
|
195
|
-
const decoder = new TextDecoder("utf-8");
|
|
196
|
-
let buffer = "";
|
|
197
|
-
while (generation === deviceLogSubscriptionState.generation) {
|
|
198
|
-
const { value, done } = await reader.read();
|
|
199
|
-
if (done) {
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
|
-
buffer += decoder.decode(value, { stream: true });
|
|
203
|
-
let splitIndex = buffer.search(/\r?\n\r?\n/);
|
|
204
|
-
while (splitIndex >= 0) {
|
|
205
|
-
const rawBlock = buffer.slice(0, splitIndex);
|
|
206
|
-
buffer = buffer.slice(splitIndex + (buffer[splitIndex] === "\r" ? 4 : 2));
|
|
207
|
-
const event = parseSseEventBlock(rawBlock);
|
|
208
|
-
if (event.data) {
|
|
209
|
-
try {
|
|
210
|
-
const parsed = JSON.parse(event.data);
|
|
211
|
-
deviceLogSubscriptionState.lastEventAt = new Date().toISOString();
|
|
212
|
-
if (event.event === "log") {
|
|
213
|
-
pushRingBuffer(deviceLogSubscriptionState.logs, normalizeBufferedLogEntry(parsed), exports.DEVICE_LOG_MEMORY_LIMIT);
|
|
214
|
-
}
|
|
215
|
-
else if (event.event === "runtime_status") {
|
|
216
|
-
pushRingBuffer(deviceLogSubscriptionState.runtimeStatus, {
|
|
217
|
-
raw: parsed,
|
|
218
|
-
timestamp: new Date().toISOString(),
|
|
219
|
-
}, exports.DEVICE_LOG_MEMORY_LIMIT);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
catch {
|
|
223
|
-
// 忽略单条非法 JSON,避免因为异常日志阻塞整个后台连接
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
splitIndex = buffer.search(/\r?\n\r?\n/);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
if (generation === deviceLogSubscriptionState.generation) {
|
|
230
|
-
scheduleDeviceLogReconnect(generation);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
catch (error) {
|
|
234
|
-
if (error instanceof Error &&
|
|
235
|
-
(error.name === "AbortError" ||
|
|
236
|
-
error.message === "The operation was aborted.")) {
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
if (generation === deviceLogSubscriptionState.generation) {
|
|
240
|
-
deviceLogSubscriptionState.status = "error";
|
|
241
|
-
deviceLogSubscriptionState.lastError =
|
|
242
|
-
error instanceof Error ? error.message : String(error);
|
|
243
|
-
scheduleDeviceLogReconnect(generation);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
finally {
|
|
247
|
-
if (deviceLogSubscriptionState.controller === controller) {
|
|
248
|
-
deviceLogSubscriptionState.controller = null;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* 确保后台日志订阅处于目标设备上
|
|
254
|
-
* - 若目标未变化且订阅仍在运行,则直接复用已有连接
|
|
255
|
-
* - 若目标发生变化,则重置缓存并重新建立订阅
|
|
256
|
-
* @param ip 设备 IP
|
|
257
|
-
* @param port 设备端口
|
|
258
|
-
* @returns 返回是否复用了现有订阅
|
|
259
|
-
* @example
|
|
260
|
-
* const reused = ensureDeviceLogSubscription("192.168.1.10", 9800)
|
|
261
|
-
*/
|
|
262
|
-
function ensureDeviceLogSubscription(ip, port) {
|
|
263
|
-
const sameTarget = deviceLogSubscriptionState.target?.ip === ip &&
|
|
264
|
-
deviceLogSubscriptionState.target?.port === port;
|
|
265
|
-
const isActive = isDeviceLogSubscriptionActive();
|
|
266
|
-
if (sameTarget && isActive) {
|
|
267
|
-
return true;
|
|
268
|
-
}
|
|
269
|
-
stopDeviceLogSubscription();
|
|
270
|
-
deviceLogSubscriptionState.generation += 1;
|
|
271
|
-
deviceLogSubscriptionState.target = { ip, port };
|
|
272
|
-
deviceLogSubscriptionState.status = "connecting";
|
|
273
|
-
resetDeviceLogSnapshotState();
|
|
274
|
-
void startDeviceLogSubscriptionLoop(deviceLogSubscriptionState.generation);
|
|
275
|
-
return false;
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* 按日志页面风格格式化时间
|
|
279
|
-
* @param value ISO 时间字符串
|
|
280
|
-
* @returns 返回 `yyyy/mm/dd hh:mm:ss.xxx` 风格文本,异常时回退原值
|
|
281
|
-
* @example
|
|
282
|
-
* formatDisplayTimestamp("2026-05-07T11:34:48.776Z")
|
|
283
|
-
*/
|
|
284
|
-
function formatDisplayTimestamp(value) {
|
|
285
|
-
const date = new Date(value);
|
|
286
|
-
if (Number.isNaN(date.getTime())) {
|
|
287
|
-
return value;
|
|
288
|
-
}
|
|
289
|
-
const pad = (number, length = 2) => String(number).padStart(length, "0");
|
|
290
|
-
return `${date.getFullYear()}/${pad(date.getMonth() + 1)}/${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}`;
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* 将日志订阅连接状态转为中文文本
|
|
294
|
-
* @param status 原始连接状态
|
|
295
|
-
* @returns 返回中文状态说明
|
|
296
|
-
* @example
|
|
297
|
-
* formatDeviceLogSubscriptionStatus("connected")
|
|
298
|
-
*/
|
|
299
|
-
function formatDeviceLogSubscriptionStatus(status) {
|
|
300
|
-
switch (status) {
|
|
301
|
-
case "idle":
|
|
302
|
-
return "未订阅";
|
|
303
|
-
case "connecting":
|
|
304
|
-
return "连接中";
|
|
305
|
-
case "connected":
|
|
306
|
-
return "已连接";
|
|
307
|
-
case "reconnecting":
|
|
308
|
-
return "重连中";
|
|
309
|
-
case "error":
|
|
310
|
-
return "连接异常";
|
|
311
|
-
default:
|
|
312
|
-
return status;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* 将 runtime_status 中的内存数值格式化为可读文本
|
|
317
|
-
* @param value 原始数值
|
|
318
|
-
* @param suffix 数值单位后缀
|
|
319
|
-
* @returns 数值存在时返回格式化结果,否则返回未知
|
|
320
|
-
* @example
|
|
321
|
-
* formatRuntimeMetricText(12.3456, " MB")
|
|
322
|
-
*/
|
|
323
|
-
function formatRuntimeMetricText(value, suffix = "") {
|
|
324
|
-
if (typeof value === "number" && Number.isFinite(value)) {
|
|
325
|
-
return `${value.toFixed(2)}${suffix}`;
|
|
326
|
-
}
|
|
327
|
-
return "未知";
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* 将单条 runtime_status 事件格式化为中文运行状态摘要
|
|
331
|
-
* @param entry runtime_status 条目
|
|
332
|
-
* @returns 返回更易读的状态文本
|
|
333
|
-
* @example
|
|
334
|
-
* formatRuntimeStatusEntry({ raw: { isRunning: false }, timestamp: "2026-01-01T00:00:00.000Z" })
|
|
335
|
-
*/
|
|
336
|
-
function formatRuntimeStatusEntry(entry) {
|
|
337
|
-
const raw = entry.raw;
|
|
338
|
-
const memory = raw.memory && typeof raw.memory === "object"
|
|
339
|
-
? raw.memory
|
|
340
|
-
: {};
|
|
341
|
-
return [
|
|
342
|
-
`[${formatDisplayTimestamp(entry.timestamp)}]`,
|
|
343
|
-
`UI: ${typeof raw.isUIShowing === "boolean"
|
|
344
|
-
? raw.isUIShowing
|
|
345
|
-
? "显示中"
|
|
346
|
-
: "未显示"
|
|
347
|
-
: "未知"}`,
|
|
348
|
-
`脚本: ${typeof raw.isRunning === "boolean"
|
|
349
|
-
? raw.isRunning
|
|
350
|
-
? "运行中"
|
|
351
|
-
: "未运行"
|
|
352
|
-
: "未知"}`,
|
|
353
|
-
`系统: ${formatRuntimeMetricText(memory.systemUsed, " MB")}/${formatRuntimeMetricText(memory.total, " MB")}`,
|
|
354
|
-
`系统占用: ${formatRuntimeMetricText(memory.usagePercentage, "%")}`,
|
|
355
|
-
`应用: ${formatRuntimeMetricText(memory.used, " MB")}`,
|
|
356
|
-
].join(" | ");
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* 生成日志缓存快照文本
|
|
360
|
-
* @param limit 需要返回的日志条数
|
|
361
|
-
* @param runtimeStatusLimit 需要返回的 runtime_status 条数
|
|
362
|
-
* @returns 返回用于 MCP 文本响应的快照内容
|
|
363
|
-
* @example
|
|
364
|
-
* const text = buildDeviceLogSnapshotText(100, 20)
|
|
365
|
-
*/
|
|
366
|
-
function buildDeviceLogSnapshotText(limit, runtimeStatusLimit) {
|
|
367
|
-
const recentLogs = deviceLogSubscriptionState.logs
|
|
368
|
-
.slice(-limit)
|
|
369
|
-
.map((log) => `[${formatDisplayTimestamp(log.timestamp)}] [${log.level.toUpperCase()}] ${log.message}`);
|
|
370
|
-
const recentRuntimeStatus = deviceLogSubscriptionState.runtimeStatus
|
|
371
|
-
.slice(-runtimeStatusLimit)
|
|
372
|
-
.map((entry) => formatRuntimeStatusEntry(entry));
|
|
4
|
+
const project_1 = require("../project");
|
|
5
|
+
/**
|
|
6
|
+
* 构建当前设备日志文本快照
|
|
7
|
+
* @param target 当前默认 HTTP 设备
|
|
8
|
+
* @param limit 需要返回的最新日志行数
|
|
9
|
+
* @returns 返回可直接作为 MCP 文本响应的日志内容
|
|
10
|
+
* @example
|
|
11
|
+
* const text = await buildDeviceLogSnapshotText({ ip: "192.168.1.10", port: "9800", label: "192.168.1.10:9800" }, 200)
|
|
12
|
+
*/
|
|
13
|
+
async function buildDeviceLogSnapshotText(target, limit) {
|
|
14
|
+
const result = await (0, project_1.getCurrentLogLinesOnDevice)({
|
|
15
|
+
ip: target.ip,
|
|
16
|
+
port: target.port,
|
|
17
|
+
transport: "http",
|
|
18
|
+
}, limit);
|
|
19
|
+
const lines = result.lines.length > 0 ? result.lines.join("\n") : "无日志";
|
|
373
20
|
return [
|
|
374
|
-
`设备: ${
|
|
375
|
-
|
|
376
|
-
: "未订阅"}`,
|
|
377
|
-
`状态: ${formatDeviceLogSubscriptionStatus(deviceLogSubscriptionState.status)}`,
|
|
378
|
-
...(deviceLogSubscriptionState.lastError
|
|
379
|
-
? [`最近错误: ${deviceLogSubscriptionState.lastError}`]
|
|
380
|
-
: []),
|
|
381
|
-
"",
|
|
382
|
-
`最新日志(最多 ${limit} 条):`,
|
|
383
|
-
recentLogs.length > 0 ? recentLogs.join("\n") : "无日志",
|
|
21
|
+
`设备: ${target.label}`,
|
|
22
|
+
`还有更早日志: ${result.hasMore ? "是" : "否"}`,
|
|
384
23
|
"",
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
? recentRuntimeStatus.join("\n")
|
|
388
|
-
: "无运行状态",
|
|
24
|
+
`最新日志(最多 ${limit} 行):`,
|
|
25
|
+
lines,
|
|
389
26
|
].join("\n");
|
|
390
27
|
}
|
package/dist/mcp/doc-tools.d.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
/**
|
|
3
|
-
* 注册文档资源
|
|
4
|
-
* @param server MCP 服务实例
|
|
5
|
-
* @returns 无返回值
|
|
6
|
-
* @example
|
|
7
|
-
* registerDocResources(server)
|
|
8
|
-
*/
|
|
9
|
-
export declare function registerDocResources(server: McpServer): void;
|
|
10
2
|
/**
|
|
11
3
|
* 注册文档工具
|
|
12
4
|
* @param server MCP 服务实例
|
package/dist/mcp/doc-tools.js
CHANGED
|
@@ -33,73 +33,27 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.registerDocResources = registerDocResources;
|
|
37
36
|
exports.registerDocTools = registerDocTools;
|
|
38
|
-
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
39
37
|
const z = __importStar(require("zod/v4"));
|
|
40
38
|
const docs_service_1 = require("./docs-service");
|
|
39
|
+
const httpapi_docs_service_1 = require("./httpapi-docs-service");
|
|
41
40
|
const tool_utils_1 = require("./tool-utils");
|
|
42
41
|
const types_1 = require("./types");
|
|
43
42
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @
|
|
46
|
-
* @returns 无返回值
|
|
43
|
+
* 格式化 KuaiJS 文档路径清单
|
|
44
|
+
* @returns 返回可直接交给 AI 读取的文档路径文本
|
|
47
45
|
* @example
|
|
48
|
-
*
|
|
46
|
+
* const text = formatDocsPathsText()
|
|
49
47
|
*/
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
uri: "kuaijs://docs/current-language",
|
|
60
|
-
mimeType: "application/json",
|
|
61
|
-
text: JSON.stringify({
|
|
62
|
-
currentLanguage: language,
|
|
63
|
-
label: types_1.DOC_LANGUAGE_LABELS[language],
|
|
64
|
-
}, null, 2),
|
|
65
|
-
},
|
|
66
|
-
],
|
|
67
|
-
};
|
|
68
|
-
});
|
|
69
|
-
server.registerResource("api-doc", new mcp_js_1.ResourceTemplate("kuaijs://docs/current/{slug}", {
|
|
70
|
-
list: async () => {
|
|
71
|
-
const language = (0, docs_service_1.getCurrentDocsLanguage)();
|
|
72
|
-
const docs = await (0, docs_service_1.getApiDocsByLanguage)(language);
|
|
73
|
-
return {
|
|
74
|
-
resources: docs.map((doc) => ({
|
|
75
|
-
uri: `kuaijs://docs/current/${doc.slug}`,
|
|
76
|
-
name: doc.title,
|
|
77
|
-
description: `[${types_1.DOC_LANGUAGE_LABELS[language]}] ${doc.slug}`,
|
|
78
|
-
mimeType: "text/markdown",
|
|
79
|
-
})),
|
|
80
|
-
};
|
|
81
|
-
},
|
|
82
|
-
}), {
|
|
83
|
-
title: "KuaiJS API documents",
|
|
84
|
-
mimeType: "text/markdown",
|
|
85
|
-
}, async (_uri, variables) => {
|
|
86
|
-
const language = (0, docs_service_1.getCurrentDocsLanguage)();
|
|
87
|
-
const docs = await (0, docs_service_1.getApiDocsByLanguage)(language);
|
|
88
|
-
const slug = String(variables.slug || "");
|
|
89
|
-
const target = docs.find((doc) => doc.slug === slug);
|
|
90
|
-
if (!target) {
|
|
91
|
-
throw new Error(`文档不存在: ${slug}`);
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
contents: [
|
|
95
|
-
{
|
|
96
|
-
uri: `kuaijs://docs/current/${target.slug}`,
|
|
97
|
-
mimeType: "text/markdown",
|
|
98
|
-
text: target.content,
|
|
99
|
-
},
|
|
100
|
-
],
|
|
101
|
-
};
|
|
102
|
-
});
|
|
48
|
+
function formatDocsPathsText() {
|
|
49
|
+
const languages = ["js", "js_zh", "python"];
|
|
50
|
+
return [
|
|
51
|
+
"KuaiJS 文档路径:",
|
|
52
|
+
`docsRoot: ${(0, docs_service_1.getDocsRootDir)()}`,
|
|
53
|
+
...languages.map((language) => `${language}: ${(0, docs_service_1.getDocsDirByLanguage)(language)} (${types_1.DOC_LANGUAGE_LABELS[language]})`),
|
|
54
|
+
`httpApi: ${(0, httpapi_docs_service_1.getHttpApiDocPath)()}`,
|
|
55
|
+
"说明: 直接读取上述 markdown 路径;search/read 工具用于定位具体文档。",
|
|
56
|
+
].join("\n");
|
|
103
57
|
}
|
|
104
58
|
/**
|
|
105
59
|
* 注册文档工具
|
|
@@ -109,51 +63,37 @@ function registerDocResources(server) {
|
|
|
109
63
|
* registerDocTools(server)
|
|
110
64
|
*/
|
|
111
65
|
function registerDocTools(server) {
|
|
112
|
-
server.registerTool("
|
|
113
|
-
title: "Get Docs
|
|
114
|
-
description: "
|
|
66
|
+
server.registerTool("get_docs_paths", {
|
|
67
|
+
title: "Get Docs Paths",
|
|
68
|
+
description: "获取 KuaiJS 本地文档路径,便于 AI 直接读取 markdown 文件而不是猜测位置。",
|
|
115
69
|
inputSchema: {},
|
|
116
70
|
}, async () => {
|
|
117
|
-
|
|
118
|
-
return (0, tool_utils_1.createTextToolResult)(`当前文档语言: ${language} (${types_1.DOC_LANGUAGE_LABELS[language]})`);
|
|
119
|
-
});
|
|
120
|
-
server.registerTool("set_docs_language", {
|
|
121
|
-
title: "Set Docs Language",
|
|
122
|
-
description: "设置当前 KuaiJS API 文档语言,影响后续文档查询与读取。",
|
|
123
|
-
inputSchema: {
|
|
124
|
-
language: z
|
|
125
|
-
.enum(["js", "js_zh", "python"])
|
|
126
|
-
.describe("文档语言:js | js_zh | python"),
|
|
127
|
-
},
|
|
128
|
-
}, async ({ language }) => {
|
|
129
|
-
const active = (0, docs_service_1.setCurrentDocsLanguage)(language);
|
|
130
|
-
const docsDir = (0, docs_service_1.getDocsDirByLanguage)(active);
|
|
131
|
-
return (0, tool_utils_1.createTextToolResult)(`文档语言已切换为 ${active} (${types_1.DOC_LANGUAGE_LABELS[active]})\n目录: ${docsDir}`);
|
|
71
|
+
return (0, tool_utils_1.createTextToolResult)(formatDocsPathsText());
|
|
132
72
|
});
|
|
133
73
|
server.registerTool("list_api_docs", {
|
|
134
74
|
title: "List API Docs",
|
|
135
|
-
description: "
|
|
75
|
+
description: "列出指定语言下可用的 API 文档;不传 language 时默认 js_zh。",
|
|
136
76
|
inputSchema: {
|
|
137
77
|
language: z
|
|
138
78
|
.enum(["js", "js_zh", "python"])
|
|
139
79
|
.optional()
|
|
140
|
-
.describe("
|
|
80
|
+
.describe("可选语言,不传默认 js_zh"),
|
|
141
81
|
},
|
|
142
82
|
}, async ({ language }) => {
|
|
143
|
-
const activeLanguage =
|
|
83
|
+
const activeLanguage = language ?? "js_zh";
|
|
144
84
|
const docs = await (0, docs_service_1.getApiDocsByLanguage)(activeLanguage);
|
|
145
|
-
const lines = docs.map((doc, index) => (0, tool_utils_1.formatApiDocSummary)(activeLanguage, doc.title, doc.slug, index));
|
|
85
|
+
const lines = docs.map((doc, index) => (0, tool_utils_1.formatApiDocSummary)(activeLanguage, doc.title, doc.slug, index, doc.filePath));
|
|
146
86
|
return (0, tool_utils_1.createTextToolResult)(lines.length === 0
|
|
147
|
-
?
|
|
87
|
+
? `文档语言 ${activeLanguage} 下没有可用文档。`
|
|
148
88
|
: [
|
|
149
|
-
|
|
89
|
+
`文档语言: ${activeLanguage} (${types_1.DOC_LANGUAGE_LABELS[activeLanguage]})`,
|
|
150
90
|
"",
|
|
151
91
|
...lines,
|
|
152
92
|
].join("\n"));
|
|
153
93
|
});
|
|
154
94
|
server.registerTool("search_api_docs", {
|
|
155
95
|
title: "Search API Docs",
|
|
156
|
-
description: "
|
|
96
|
+
description: "在指定语言文档中按关键字搜索;不传 language 时默认 js_zh。",
|
|
157
97
|
inputSchema: {
|
|
158
98
|
query: z.string().min(1).describe("搜索关键字"),
|
|
159
99
|
limit: z
|
|
@@ -167,10 +107,10 @@ function registerDocTools(server) {
|
|
|
167
107
|
language: z
|
|
168
108
|
.enum(["js", "js_zh", "python"])
|
|
169
109
|
.optional()
|
|
170
|
-
.describe("
|
|
110
|
+
.describe("可选语言,不传默认 js_zh"),
|
|
171
111
|
},
|
|
172
112
|
}, async ({ query, limit, language }) => {
|
|
173
|
-
const activeLanguage =
|
|
113
|
+
const activeLanguage = language ?? "js_zh";
|
|
174
114
|
const docs = await (0, docs_service_1.getApiDocsByLanguage)(activeLanguage);
|
|
175
115
|
const normalizedQuery = query.toLowerCase();
|
|
176
116
|
const matches = docs
|
|
@@ -180,31 +120,38 @@ function registerDocTools(server) {
|
|
|
180
120
|
.slice(0, limit)
|
|
181
121
|
.map((item) => item.doc);
|
|
182
122
|
const text = matches.length === 0
|
|
183
|
-
?
|
|
123
|
+
? `文档语言: ${activeLanguage} (${types_1.DOC_LANGUAGE_LABELS[activeLanguage]})\n未找到与 "${query}" 匹配的文档。`
|
|
184
124
|
: [
|
|
185
|
-
|
|
125
|
+
`文档语言: ${activeLanguage} (${types_1.DOC_LANGUAGE_LABELS[activeLanguage]})`,
|
|
186
126
|
"",
|
|
187
|
-
...matches.map((doc, index) => (0, tool_utils_1.formatApiDocSummary)(activeLanguage, doc.title, doc.slug, index)),
|
|
127
|
+
...matches.map((doc, index) => (0, tool_utils_1.formatApiDocSummary)(activeLanguage, doc.title, doc.slug, index, doc.filePath)),
|
|
188
128
|
].join("\n");
|
|
189
129
|
return (0, tool_utils_1.createTextToolResult)(text);
|
|
190
130
|
});
|
|
191
131
|
server.registerTool("read_api_doc", {
|
|
192
132
|
title: "Read API Doc",
|
|
193
|
-
description: "
|
|
133
|
+
description: "读取指定语言下某个文档的完整 markdown 内容;不传 language 时默认 js_zh。",
|
|
194
134
|
inputSchema: {
|
|
195
135
|
slug: z.string().min(1).describe("文档 slug(文件名,不含 .md)"),
|
|
196
136
|
language: z
|
|
197
137
|
.enum(["js", "js_zh", "python"])
|
|
198
138
|
.optional()
|
|
199
|
-
.describe("
|
|
139
|
+
.describe("可选语言,不传默认 js_zh"),
|
|
200
140
|
},
|
|
201
141
|
}, async ({ slug, language }) => {
|
|
202
|
-
const activeLanguage =
|
|
142
|
+
const activeLanguage = language ?? "js_zh";
|
|
203
143
|
const docs = await (0, docs_service_1.getApiDocsByLanguage)(activeLanguage);
|
|
204
144
|
const target = docs.find((doc) => doc.slug === slug);
|
|
205
145
|
if (!target) {
|
|
206
146
|
return (0, tool_utils_1.createTextToolResult)(`未找到文档 ${slug}.md(语言: ${activeLanguage})。`, true);
|
|
207
147
|
}
|
|
208
|
-
return (0, tool_utils_1.createTextToolResult)(
|
|
148
|
+
return (0, tool_utils_1.createTextToolResult)([
|
|
149
|
+
`标题: ${target.title}`,
|
|
150
|
+
`语言: ${activeLanguage}`,
|
|
151
|
+
`URI: ${(0, docs_service_1.getDocUri)(activeLanguage, target.slug)}`,
|
|
152
|
+
`path: ${target.filePath}`,
|
|
153
|
+
"",
|
|
154
|
+
target.content,
|
|
155
|
+
].join("\n"));
|
|
209
156
|
});
|
|
210
157
|
}
|