ms-vite-plugin 1.1.16 → 1.1.18

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.
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTextToolResult = createTextToolResult;
4
+ exports.createRuntimeHttpRequestOptions = createRuntimeHttpRequestOptions;
5
+ exports.resolveRuntimeHttpTarget = resolveRuntimeHttpTarget;
6
+ exports.requestRuntimeJsonApi = requestRuntimeJsonApi;
7
+ exports.requestRuntimePathJson = requestRuntimePathJson;
8
+ exports.unwrapRuntimeData = unwrapRuntimeData;
9
+ exports.formatRuntimeJsonText = formatRuntimeJsonText;
10
+ exports.formatApiDocSummary = formatApiDocSummary;
11
+ const docs_service_1 = require("./docs-service");
12
+ const device_config_1 = require("./device-config");
13
+ /**
14
+ * 创建标准 MCP 文本内容对象
15
+ * @param text 返回文本
16
+ * @returns 返回 `{ type: "text", text }` 结构
17
+ * @example
18
+ * createTextContent("ok")
19
+ */
20
+ function createTextContent(text) {
21
+ return {
22
+ type: "text",
23
+ text,
24
+ };
25
+ }
26
+ /**
27
+ * 创建标准 MCP 文本工具返回值
28
+ * @param text 返回文本
29
+ * @param isError 是否标记为错误响应
30
+ * @returns 返回包含单条文本内容的 MCP Tool 结果
31
+ * @example
32
+ * createTextToolResult("done")
33
+ */
34
+ function createTextToolResult(text, isError = false) {
35
+ return {
36
+ content: [createTextContent(text)],
37
+ ...(isError ? { isError: true } : {}),
38
+ };
39
+ }
40
+ /**
41
+ * 构建运行时 HTTP 请求参数
42
+ * @param target 已解析的设备目标
43
+ * @returns 返回项目内部设备请求参数
44
+ * @example
45
+ * const options = createRuntimeHttpRequestOptions(target)
46
+ */
47
+ function createRuntimeHttpRequestOptions(target) {
48
+ return {
49
+ ip: target.ip,
50
+ port: target.port,
51
+ transport: "http",
52
+ };
53
+ }
54
+ /**
55
+ * 解析当前默认 HTTP 设备
56
+ * @returns 返回标准化后的 HTTP 请求目标
57
+ * @example
58
+ * const target = await resolveRuntimeHttpTarget()
59
+ */
60
+ async function resolveRuntimeHttpTarget() {
61
+ let device;
62
+ try {
63
+ device = await (0, device_config_1.resolveDeviceConfig)();
64
+ }
65
+ catch {
66
+ throw new Error("未设置设备:请先调用 set_device。");
67
+ }
68
+ return {
69
+ ip: device.ip,
70
+ port: (0, device_config_1.normalizePort)(device.port),
71
+ label: `${device.ip}:${device.port}`,
72
+ };
73
+ }
74
+ /**
75
+ * 请求设备通用 JSON API
76
+ * @param target 当前默认设备目标
77
+ * @param endpoint API 路径(不含前导 `/api/`)
78
+ * @param method 请求方法
79
+ * @param body 可选 JSON 请求体
80
+ * @returns 返回解析后的 JSON 数据
81
+ * @example
82
+ * const payload = await requestRuntimeJsonApi(target, "activeAppInfo")
83
+ */
84
+ async function requestRuntimeJsonApi(target, endpoint, method = "GET", body) {
85
+ const url = `http://${target.ip}:${target.port}/api/${endpoint}`;
86
+ const response = await fetch(url, {
87
+ method,
88
+ headers: body ? { "Content-Type": "application/json" } : undefined,
89
+ body: body ? JSON.stringify(body) : undefined,
90
+ });
91
+ if (!response.ok) {
92
+ throw new Error(`${endpoint} 请求失败,状态码: ${response.status}`);
93
+ }
94
+ const payload = (await response.json());
95
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
96
+ throw new Error(`${endpoint} 返回了无效响应`);
97
+ }
98
+ return payload;
99
+ }
100
+ /**
101
+ * 请求任意相对路径 JSON API,并可附带 query 参数
102
+ * @param target 当前默认设备目标
103
+ * @param routePath 相对路径,例如 `/api/control/click`
104
+ * @param query 可选查询参数
105
+ * @returns 返回解析后的 JSON 数据
106
+ * @example
107
+ * const payload = await requestRuntimePathJson(target, "/api/ime/getText")
108
+ */
109
+ async function requestRuntimePathJson(target, routePath, query) {
110
+ const url = new URL(`http://${target.ip}:${target.port}${routePath}`);
111
+ if (query) {
112
+ for (const [key, value] of Object.entries(query)) {
113
+ if (value === undefined || value === null) {
114
+ continue;
115
+ }
116
+ url.searchParams.set(key, String(value));
117
+ }
118
+ }
119
+ const response = await fetch(url.toString(), {
120
+ method: "GET",
121
+ });
122
+ if (!response.ok) {
123
+ throw new Error(`${routePath} 请求失败,状态码: ${response.status}`);
124
+ }
125
+ const payload = (await response.json());
126
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
127
+ throw new Error(`${routePath} 返回了无效响应`);
128
+ }
129
+ return payload;
130
+ }
131
+ /**
132
+ * 从通用 JSON 响应中读取 `data` 字段
133
+ * @param payload API 返回 JSON
134
+ * @param endpoint API 路径名,用于错误提示
135
+ * @returns 返回 `data` 字段内容
136
+ * @example
137
+ * const data = unwrapRuntimeData(payload, "getAllConfig")
138
+ */
139
+ function unwrapRuntimeData(payload, endpoint) {
140
+ if (payload.success !== true) {
141
+ throw new Error(String(payload.message ?? `${endpoint} 返回失败`));
142
+ }
143
+ return payload.data;
144
+ }
145
+ /**
146
+ * 将任意 JSON 兼容值格式化为文本
147
+ * @param value 任意待展示值
148
+ * @returns 返回便于 MCP 文本输出的内容
149
+ * @example
150
+ * formatRuntimeJsonText({ success: true })
151
+ */
152
+ function formatRuntimeJsonText(value) {
153
+ if (typeof value === "string") {
154
+ return value;
155
+ }
156
+ return JSON.stringify(value, null, 2);
157
+ }
158
+ /**
159
+ * 格式化单条 API 文档摘要
160
+ * @param language 文档语言
161
+ * @param title 文档标题
162
+ * @param slug 文档 slug
163
+ * @param index 列表序号
164
+ * @returns 返回用于列表展示的文本块
165
+ * @example
166
+ * formatApiDocSummary("js", "Tap", "tap", 0)
167
+ */
168
+ function formatApiDocSummary(language, title, slug, index) {
169
+ return `${index + 1}. ${title}\nslug: ${slug}\nuri: ${(0, docs_service_1.getDocUri)(language, slug)}`;
170
+ }
@@ -1,6 +1,6 @@
1
1
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
2
  import { DEFAULT_DEVICE_PORT } from "./device-config";
3
- import { type ApiDocsLanguage } from "./types";
3
+ import type { ApiDocsLanguage } from "./types";
4
4
  /**
5
5
  * 创建并注册 MCP 工具
6
6
  * @returns 返回已注册工具的 MCP Server 实例