dsh-apis-plugin 0.1.10 → 0.2.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/README.md +31 -15
- package/api-tools.js +153 -130
- package/client.js +109 -93
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,20 +18,29 @@ dsh plugin --profile web add dsh-apis-plugin@0.1.6
|
|
|
18
18
|
|
|
19
19
|
## 配置
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
「API配置」指向一个**集合父目录**,扫描其下(含各子文件夹)的所有 `*.cfg` 文件,**每个 cfg 是一个集合,集合名取 cfg 里的 `host` 字段**(缺失时回退所在文件夹名),各集合的接口、baseUrl、文档相互独立:
|
|
22
22
|
|
|
23
23
|
```
|
|
24
24
|
config/
|
|
25
|
-
├──
|
|
26
|
-
|
|
25
|
+
├── site-a.example.com/ # 子文件夹 1
|
|
26
|
+
│ ├── api.cfg # 集合 1:host=site-a.example.com
|
|
27
|
+
│ └── api_doc.md # 该集合的接口文档
|
|
28
|
+
└── site-b.example.com/
|
|
29
|
+
├── api.cfg # 集合 2:host=site-b.example.com
|
|
30
|
+
└── api_doc.md
|
|
27
31
|
```
|
|
28
32
|
|
|
29
|
-
|
|
33
|
+
> cfg 文件名任意(`api.cfg`、`config.cfg`、`<host>.cfg` 均可),扫描只认 `*.cfg` 文件(根目录直放的也会扫到),集合身份只看里面的 `host` 字段;同一个子文件夹放多个 cfg 即为多个集合。
|
|
34
|
+
|
|
35
|
+
### 1. xxx.cfg(每个集合一份,必需)
|
|
30
36
|
|
|
31
37
|
key=value 行格式,`#` 注释,UTF-8 编码:
|
|
32
38
|
|
|
33
39
|
```ini
|
|
34
|
-
#
|
|
40
|
+
# 集合标识(如网站域名),用于展示与「集合名/路径」消歧
|
|
41
|
+
host=site-a.example.com
|
|
42
|
+
|
|
43
|
+
# 该集合的接口服务前缀:域名 + nginx 前缀 + 网关路由段
|
|
35
44
|
baseUrl=http://your-host/prod-api
|
|
36
45
|
|
|
37
46
|
# 接口列表:apis= 下面逐行一个接口
|
|
@@ -42,8 +51,9 @@ apis=
|
|
|
42
51
|
/outline/detailByNos
|
|
43
52
|
```
|
|
44
53
|
|
|
45
|
-
- `
|
|
46
|
-
- `
|
|
54
|
+
- `host`:集合名,缺失时回退文件所在文件夹名
|
|
55
|
+
- `baseUrl`:请求时拼接在该集合的接口路径前(改文件即生效,无需重启)
|
|
56
|
+
- `apis`:该集合的接口白名单,支持 `{param}` 模板段(如 `/users/{id}`),agent 请求时填实际值
|
|
47
57
|
- **增删接口只改这个文件**,改完点插件配置页的「扫描」(或重启)生效
|
|
48
58
|
|
|
49
59
|
### 2. api_doc.md(接口文档,建议提供)
|
|
@@ -89,9 +99,9 @@ agent 调用 `{prefix}_api_doc` 时按路径命中对应小节返回。
|
|
|
89
99
|
|
|
90
100
|
| 操作 | 说明 |
|
|
91
101
|
|---|---|
|
|
92
|
-
| API 配置 |
|
|
93
|
-
| 扫描 |
|
|
94
|
-
|
|
|
102
|
+
| API 配置 | 填集合父目录(扫描其下所有 `*.cfg`,以 host 为集合名),**失焦或回车自动保存** |
|
|
103
|
+
| 扫描 | 重扫目录:接口列表严格等于当前各集合 cfg 内容;**目录留空点击则清空列表** |
|
|
104
|
+
| 集合手风琴 | 每个集合一个可折叠分组,展示集合名、接口数、baseUrl 与接口列表(只读,增删请在 cfg 中操作) |
|
|
95
105
|
|
|
96
106
|
### Agent 工具
|
|
97
107
|
|
|
@@ -99,9 +109,15 @@ agent 调用 `{prefix}_api_doc` 时按路径命中对应小节返回。
|
|
|
99
109
|
|
|
100
110
|
| 工具 | 用途 |
|
|
101
111
|
|---|---|
|
|
102
|
-
| `api_api_doc` | 查接口文档:传 `path`
|
|
112
|
+
| `api_api_doc` | 查接口文档:传 `path` 返回该接口的参数与响应说明;不传返回通用说明和各集合的接口目录 |
|
|
103
113
|
| `api_api_request` | 真正调用接口:`path` 必须在白名单内,GET 用 `query`,POST 用 `body` |
|
|
104
114
|
|
|
115
|
+
多集合规则:
|
|
116
|
+
|
|
117
|
+
- path 默认写接口路径(如 `/users/page`),在全部集合中查找;**跨集合同名接口需用「集合名/路径」消歧**(如 `site-a.example.com/users/page`)
|
|
118
|
+
- 每个集合使用自己 api.cfg 里的 baseUrl 发请求,互不混淆
|
|
119
|
+
- 接口文档也按集合独立查找(各集合自己的 api_doc.md)
|
|
120
|
+
|
|
105
121
|
超出 api.cfg 范围的请求(无论查文档还是发请求)会被直接拒绝,并提示可用接口列表。
|
|
106
122
|
|
|
107
123
|
安全机制:
|
|
@@ -114,12 +130,12 @@ agent 调用 `{prefix}_api_doc` 时按路径命中对应小节返回。
|
|
|
114
130
|
dsh 控制台日志:
|
|
115
131
|
|
|
116
132
|
```
|
|
117
|
-
[dsh-apis-plugin] loaded; endpoints=N,
|
|
133
|
+
[dsh-apis-plugin] loaded; endpoints=N, collections=a.com,b.com
|
|
118
134
|
```
|
|
119
135
|
|
|
120
|
-
- `endpoints=0`:检查 API
|
|
121
|
-
- 加载后列表与
|
|
122
|
-
- 提示未配置 baseUrl
|
|
136
|
+
- `endpoints=0`:检查 API 目录路径是否正确、各子文件夹下是否有 `*.cfg`(且含 apis)
|
|
137
|
+
- 加载后列表与 cfg 不一致:确认已在配置页点过「扫描」
|
|
138
|
+
- 提示未配置 baseUrl:在对应集合的 cfg 文件写入 `baseUrl=http://...`
|
|
123
139
|
|
|
124
140
|
## License
|
|
125
141
|
|
package/api-tools.js
CHANGED
|
@@ -1,42 +1,60 @@
|
|
|
1
1
|
// 可复用的「接口工具插件」工厂:把任意一套 HTTP 接口注册成 agent 的 {prefix}_api_doc / {prefix}_api_request 双工具
|
|
2
2
|
// 领域差异(settings 命名空间、工具名前缀、领域名、文档目录、baseUrl、默认接口列表)由调用方注入,
|
|
3
|
-
// 其中 domain / toolPrefix / apiDir 也可在部署侧 cordis.patch.yml 的 config
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import { join } from 'node:path'
|
|
3
|
+
// 其中 domain / toolPrefix / apiDir 也可在部署侧 cordis.patch.yml 的 config 里覆盖
|
|
4
|
+
import { readFileSync, writeFileSync, readdirSync } from 'node:fs'
|
|
5
|
+
import { join, dirname, basename } from 'node:path'
|
|
7
6
|
import Schema from '@deepseek-ai/schemastery'
|
|
8
7
|
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
if (!
|
|
8
|
+
// 读取 cfg 文件(key=value 行,# 注释):host 为集合标识;baseUrl 为服务前缀;
|
|
9
|
+
// apis 为接口列表,支持逗号分隔同行书写,或 apis= 下面逐行一个接口
|
|
10
|
+
function loadCfgFile(file) {
|
|
11
|
+
const cfg = { apis: [] }
|
|
12
|
+
if (!file) return cfg
|
|
14
13
|
try {
|
|
15
|
-
|
|
16
|
-
let current = null
|
|
14
|
+
let current
|
|
17
15
|
// 去掉 UTF-8 BOM(Windows 记事本默认会加),否则首个 key 解析失败
|
|
18
|
-
const
|
|
19
|
-
for (const raw of text.split(/\r?\n/)) {
|
|
16
|
+
for (const raw of readFileSync(file, 'utf8').replace(/^\uFEFF/, '').split(/\r?\n/)) {
|
|
20
17
|
const line = raw.trim()
|
|
21
18
|
if (!line || line.startsWith('#')) continue
|
|
22
19
|
const m = line.match(/^([\w-]+)\s*=\s*(.*)$/)
|
|
23
20
|
if (m) {
|
|
24
21
|
current = m[1]
|
|
25
22
|
if (m[2]) cfg[current] = current === 'apis' ? m[2].split(',').map((s) => s.trim()).filter(Boolean) : m[2]
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
// 无 = 的行归属上一个 key(apis 的逐行列举形式)
|
|
29
|
-
if (current === 'apis') cfg.apis.push(line)
|
|
23
|
+
} else if (current === 'apis') cfg.apis.push(line) // 无 = 的行归属上一个 key(apis 逐行列举)
|
|
30
24
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
25
|
+
} catch { /* 文件不可读时按空处理 */ }
|
|
26
|
+
return cfg
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 扫描目录下的「接口集合」:单次遍历根目录与各子文件夹下的 *.cfg(*.cfg 即集合,天然覆盖旧单文件布局),
|
|
30
|
+
// 集合名取 cfg 的 host 字段(缺失时回退文件所在文件夹名)
|
|
31
|
+
function scanCollections(dir) {
|
|
32
|
+
if (!dir) return []
|
|
33
|
+
const out = []
|
|
34
|
+
const push = (file) => {
|
|
35
|
+
const cfg = loadCfgFile(file)
|
|
36
|
+
if (cfg.apis.length) out.push({ name: cfg.host || basename(dirname(file)), file, dir: dirname(file), baseUrl: cfg.baseUrl || '', apis: cfg.apis })
|
|
34
37
|
}
|
|
38
|
+
try {
|
|
39
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
40
|
+
if (entry.isDirectory()) {
|
|
41
|
+
const sub = join(dir, entry.name)
|
|
42
|
+
for (const f of readdirSync(sub)) if (f.endsWith('.cfg')) push(join(sub, f))
|
|
43
|
+
} else if (entry.isFile() && entry.name.endsWith('.cfg')) {
|
|
44
|
+
push(join(dir, entry.name))
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
} catch { /* 目录不可读时按空处理 */ }
|
|
48
|
+
return out
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 扫描集合并附带各自目录下的 api_doc.md 分节
|
|
52
|
+
function loadCollections(dir) {
|
|
53
|
+
return scanCollections(dir).map((c) => ({ ...c, sections: splitDocSections(loadApiDoc(c.dir)) }))
|
|
35
54
|
}
|
|
36
55
|
|
|
37
56
|
// 读取目录下 api_doc.md 作为接口文档
|
|
38
57
|
function loadApiDoc(dir) {
|
|
39
|
-
if (!dir) return ''
|
|
40
58
|
try {
|
|
41
59
|
return readFileSync(join(dir, 'api_doc.md'), 'utf8').replace(/^\uFEFF/, '')
|
|
42
60
|
} catch {
|
|
@@ -49,16 +67,20 @@ function splitDocSections(doc) {
|
|
|
49
67
|
return doc.split(/\r?\n-{3,}\r?\n/).map((s) => s.trim()).filter(Boolean)
|
|
50
68
|
}
|
|
51
69
|
|
|
52
|
-
// 判断实际请求路径是否命中配置的接口(支持 {param}
|
|
70
|
+
// 判断实际请求路径是否命中配置的接口(支持 {param} 模板段);
|
|
71
|
+
// 其余字符按字面量转义,避免 cfg 里误写正则特殊字符导致构造 RegExp 抛错
|
|
53
72
|
function matchEndpoint(path, configured) {
|
|
54
|
-
const pattern =
|
|
55
|
-
|
|
73
|
+
const pattern = configured.split(/\{[^}]+\}/).map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('[^/]+')
|
|
74
|
+
try {
|
|
75
|
+
return new RegExp('^' + pattern + '/?$').test(path)
|
|
76
|
+
} catch {
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
56
79
|
}
|
|
57
80
|
|
|
58
81
|
/** 从 cordis.patch.yml 提取插件 id(insert 列表第一项),作为命名空间唯一来源 */
|
|
59
82
|
export function readPatchId(dir) {
|
|
60
|
-
const
|
|
61
|
-
const m = yml.match(/^\s*-\s*id:\s*(\S+)\s*$/m)
|
|
83
|
+
const m = readFileSync(join(dir, 'cordis.patch.yml'), 'utf8').match(/^\s*-\s*id:\s*(\S+)\s*$/m)
|
|
62
84
|
if (!m) throw new Error('cordis.patch.yml 中未找到 insert 的 id')
|
|
63
85
|
return m[1]
|
|
64
86
|
}
|
|
@@ -77,7 +99,7 @@ export function syncClientNs(dir, ns) {
|
|
|
77
99
|
* @param {string} options.ns settings 命名空间,须与前端卡片的 NS 一致
|
|
78
100
|
* @param {string} [options.toolPrefix] 工具名前缀默认值,生成 {prefix}_api_doc / {prefix}_api_request,可被 config 覆盖
|
|
79
101
|
* @param {string} [options.domain] 领域名默认值,用于拼接工具描述 prompt,可被 config 覆盖
|
|
80
|
-
* @param {string} [options.apiDir]
|
|
102
|
+
* @param {string} [options.apiDir] cfg 文件与 api_doc.md 所在目录默认值,可被 config 覆盖
|
|
81
103
|
* @param {string} [options.defaultBaseUrl] 接口服务前缀默认值(域名 + nginx 前缀 + 网关路由段)
|
|
82
104
|
* @param {string[]} [options.defaultEndpoints] yml 之外的额外默认接口
|
|
83
105
|
*/
|
|
@@ -85,150 +107,156 @@ export function defineApiPlugin(options) {
|
|
|
85
107
|
const { ns, toolPrefix = 'api', domain = '', apiDir = '', defaultBaseUrl = '', defaultEndpoints = [] } = options
|
|
86
108
|
|
|
87
109
|
const Config = Schema.object({
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
//
|
|
91
|
-
toolPrefix: Schema.string().default(toolPrefix),
|
|
92
|
-
// api.cfg 与 api_doc.md 所在目录(留空则只用 endpoints)
|
|
93
|
-
apiDir: Schema.string().default(apiDir),
|
|
110
|
+
domain: Schema.string().default(domain), // 领域名:拼进工具描述 prompt
|
|
111
|
+
toolPrefix: Schema.string().default(toolPrefix), // 工具名前缀:生成 {prefix}_api_doc / {prefix}_api_request
|
|
112
|
+
apiDir: Schema.string().default(apiDir), // cfg 与 api_doc.md 所在目录(留空则只用 endpoints)
|
|
94
113
|
endpoints: Schema.array(Schema.string()).default([]),
|
|
95
|
-
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
// 内部字段:客户端「加载API」按钮改写它触发服务端重扫目录
|
|
99
|
-
scanToken: Schema.string().default(''),
|
|
114
|
+
baseUrl: Schema.string().default(defaultBaseUrl), // 接口服务前缀(路径本身已含服务前缀)
|
|
115
|
+
scanToken: Schema.string().default(''), // 内部字段:客户端「扫描」按钮改写它触发服务端重扫
|
|
116
|
+
collections: Schema.string().default(''), // 内部字段:扫描到的集合 JSON [{name, baseUrl, apis}],客户端只读展示
|
|
100
117
|
})
|
|
101
118
|
|
|
102
|
-
// 依赖部署挂载的设置服务与工具服务
|
|
103
119
|
const inject = ['settings', 'tools']
|
|
104
120
|
|
|
105
121
|
function apply(ctx, config) {
|
|
106
|
-
// config 覆盖优先,回退工厂默认值(兼容旧用户层保存值缺字段的情况)
|
|
107
122
|
const prefix = config.toolPrefix || toolPrefix
|
|
108
123
|
const domainName = config.domain || domain
|
|
109
|
-
// 部署侧静态接口(yml config.endpoints + 工厂默认),与 api.cfg 动态读取分离,
|
|
110
|
-
// 避免「加载API」重扫时把启动时的旧 api.cfg 列表拼回去(导致删掉的接口仍可请求)
|
|
111
124
|
const ymlApiDir = config.apiDir || apiDir
|
|
125
|
+
// 部署侧静态接口(yml config.endpoints + 工厂默认),与 cfg 动态读取分离,避免重扫时把旧列表拼回去
|
|
112
126
|
const staticEndpoints = [...new Set([...defaultEndpoints, ...(config.endpoints ?? [])])]
|
|
113
|
-
//
|
|
114
|
-
const
|
|
127
|
+
// 集合持久化为 JSON(客户端只读展示;文档分节不落盘)
|
|
128
|
+
const plain = (cs) => JSON.stringify(cs.map(({ name, baseUrl, apis }) => ({ name, baseUrl, apis })))
|
|
129
|
+
const mergedEndpoints = (cs) => [...new Set([...cs.flatMap((c) => c.apis), ...staticEndpoints])]
|
|
115
130
|
|
|
131
|
+
const bootCollections = loadCollections(ymlApiDir)
|
|
116
132
|
// 以组合配置为 base 层注册 settings 命名空间,卸载插件时注册随 fiber 一并清理
|
|
117
|
-
const scope = ctx.settings.register(ns, Config, { base: { ...config, endpoints:
|
|
133
|
+
const scope = ctx.settings.register(ns, Config, { base: { ...config, endpoints: mergedEndpoints(bootCollections), collections: plain(bootCollections) } })
|
|
118
134
|
|
|
119
|
-
//
|
|
135
|
+
// 当前生效目录与集合:用户层保存值优先(重启后仍生效),回退部署侧配置
|
|
120
136
|
let docDir = scope.get()?.apiDir || ymlApiDir
|
|
121
|
-
|
|
122
|
-
// 文档按当前生效目录读取;用户层改写 apiDir 后随 watch 刷新
|
|
123
|
-
let docSections = splitDocSections(loadApiDoc(docDir))
|
|
124
|
-
|
|
125
|
-
// 每次已提交变更后收到通知;「加载API」以 scanToken 变化标记,触发目录重扫
|
|
137
|
+
let collections = loadCollections(docDir)
|
|
126
138
|
let lastScanToken = scope.get()?.scanToken ?? ''
|
|
139
|
+
|
|
140
|
+
// 每次已提交变更后收到通知;「扫描」以 scanToken 变化标记,触发目录重扫并整体接管 endpoints / collections
|
|
127
141
|
ctx.effect(
|
|
128
|
-
() => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
// 目录变化或显式重扫时重读文档
|
|
135
|
-
if (dir !== docDir || next.scanToken !== lastScanToken) {
|
|
136
|
-
docDir = dir
|
|
137
|
-
docSections = splitDocSections(loadApiDoc(dir))
|
|
138
|
-
}
|
|
139
|
-
if (next.scanToken !== lastScanToken) {
|
|
140
|
-
lastScanToken = next.scanToken
|
|
141
|
-
// 以最新 api.cfg 为权威范围 ∪ 静态接口,整体接管用户层 endpoints(去重;值未变时不提交,避免循环)
|
|
142
|
-
// 用最新读取结果而非启动时的快照,这样从 api.cfg 删掉的接口重扫后即失效
|
|
143
|
-
const merged = [...new Set([...loadApiCfg(dir).apis, ...staticEndpoints])]
|
|
144
|
-
const cur = Array.isArray(next.endpoints) ? next.endpoints : []
|
|
145
|
-
if (merged.join('\n') !== cur.join('\n')) scope.update({ endpoints: merged })
|
|
146
|
-
}
|
|
147
|
-
console.log(`${ns}: settings updated`)
|
|
148
|
-
} catch (err) {
|
|
149
|
-
console.error(`[${ns}] watch handler error:`, err)
|
|
142
|
+
() => scope.watch((next) => {
|
|
143
|
+
try {
|
|
144
|
+
const dir = next.apiDir || apiDir
|
|
145
|
+
if (dir !== docDir || next.scanToken !== lastScanToken) {
|
|
146
|
+
docDir = dir
|
|
147
|
+
collections = loadCollections(dir)
|
|
150
148
|
}
|
|
151
|
-
|
|
152
|
-
|
|
149
|
+
if (next.scanToken !== lastScanToken) {
|
|
150
|
+
lastScanToken = next.scanToken
|
|
151
|
+
const patch = {}
|
|
152
|
+
const merged = mergedEndpoints(collections)
|
|
153
|
+
if (merged.join('\n') !== (Array.isArray(next.endpoints) ? next.endpoints : []).join('\n')) patch.endpoints = merged
|
|
154
|
+
if (plain(collections) !== next.collections) patch.collections = plain(collections)
|
|
155
|
+
if (Object.keys(patch).length) scope.update(patch)
|
|
156
|
+
}
|
|
157
|
+
} catch (err) {
|
|
158
|
+
console.error(`[${ns}] watch handler error:`, err)
|
|
159
|
+
}
|
|
160
|
+
}),
|
|
153
161
|
`${ns}: settings watch`,
|
|
154
|
-
|
|
162
|
+
)
|
|
155
163
|
|
|
156
|
-
//
|
|
164
|
+
// 已配置接口目录(按集合分组展示;部署侧静态接口不在任何集合时单独一组)
|
|
165
|
+
function catalogText() {
|
|
166
|
+
const groups = collections.map((c) =>
|
|
167
|
+
`【${c.name}】${c.baseUrl ? `(baseUrl: ${c.baseUrl})` : ''}\n` + c.apis.map((e) => ' ' + e).join('\n'))
|
|
168
|
+
const dyn = new Set(collections.flatMap((c) => c.apis))
|
|
169
|
+
const extra = staticEndpoints.filter((e) => !dyn.has(e))
|
|
170
|
+
if (extra.length) groups.push('【部署侧固定接口】\n' + extra.map((e) => ' ' + e).join('\n'))
|
|
171
|
+
return groups.join('\n\n')
|
|
172
|
+
}
|
|
157
173
|
|
|
158
|
-
//
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
174
|
+
// 把规范化 path 解析到具体集合:① 支持「集合名/路径」前缀;② 无前缀全集合查找,
|
|
175
|
+
// 命中多个返回 { ambiguous } 要求消歧;③ 集合外回退部署侧静态接口
|
|
176
|
+
function resolveTarget(norm) {
|
|
177
|
+
const bare = norm.replace(/^\//, '') // 剥掉前导 / 再找「集合名/路径」前缀
|
|
178
|
+
const slash = bare.indexOf('/')
|
|
179
|
+
if (slash > 0) {
|
|
180
|
+
const c = collections.find((x) => x.name === bare.slice(0, slash))
|
|
181
|
+
if (c) {
|
|
182
|
+
const rest = bare.slice(slash)
|
|
183
|
+
return c.apis.some((e) => matchEndpoint(rest, e)) ? { c, path: rest } : null
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const hits = collections.filter((c) => c.apis.some((e) => matchEndpoint(norm, e)))
|
|
187
|
+
if (hits.length > 1) return { ambiguous: hits }
|
|
188
|
+
if (hits.length === 1) return { c: hits[0], path: norm }
|
|
189
|
+
if (staticEndpoints.some((e) => matchEndpoint(norm, e))) {
|
|
190
|
+
return { c: { name: '_static', baseUrl: scope.get()?.baseUrl || '', apis: staticEndpoints, sections: [] }, path: norm }
|
|
191
|
+
}
|
|
192
|
+
return null
|
|
165
193
|
}
|
|
166
194
|
|
|
195
|
+
// 集合的服务前缀:实时重读该集合的 cfg 文件(改文件即生效,无需重启),回退扫描快照
|
|
196
|
+
const baseUrlOf = (c) => c.name === '_static' ? scope.get()?.baseUrl || '' : loadCfgFile(c.file).baseUrl || c.baseUrl || ''
|
|
197
|
+
const ambiguousMsg = (norm, hits) => `接口 ${norm} 在多个集合中命中(${hits.map((c) => c.name).join('、')}),请用「集合名/路径」精确指定,如:${hits[0].name}${norm}`
|
|
198
|
+
const normPath = (p) => ('/' + p.trim().replace(/^\/+/, '')).split('?')[0]
|
|
199
|
+
|
|
200
|
+
// 删除操作的待确认记录(key: "DELETE <完整地址>"):只有先收到过确认提示,confirm=true 才会放行
|
|
201
|
+
const pendingDeletes = new Set()
|
|
202
|
+
|
|
167
203
|
// 工具 1:查接口文档,让模型了解每个接口的参数与响应(prompt 按领域名拼接)
|
|
168
204
|
ctx.tools.register({
|
|
169
205
|
name: `${prefix}_api_doc`,
|
|
170
|
-
description: `查询${domainName}
|
|
206
|
+
description: `查询${domainName}接口文档。接口按集合组织(集合名取各 cfg 文件的 host 字段),api_doc.md 与部署的服务一一对应,是这些接口的权威依据。传 path(如 /users/page,跨集合同名接口用「集合名/路径」如 site-a.com/users/page)返回该接口的详细参数与响应说明;不传 path 返回通用说明和全部集合的接口目录。发起请求前先用它确认参数,按文档直接调用即可,不要再去查阅后端源码或其他材料核实接口行为。`,
|
|
171
207
|
parameters: {
|
|
172
|
-
path: { type: 'string', description: '接口路径,如 /users/{id}' },
|
|
208
|
+
path: { type: 'string', description: '接口路径,如 /users/{id};跨集合同名时用 集合名/路径' },
|
|
173
209
|
},
|
|
174
210
|
output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] },
|
|
175
211
|
async execute(args) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return `接口 ${norm} 不在已配置列表中,已拒绝查询。可用接口:\n` + list.join('\n')
|
|
184
|
-
}
|
|
185
|
-
const hit = docSections.filter((s) => s.includes('`' + norm + '`'))
|
|
186
|
-
if (hit.length) return hit.join('\n\n---\n\n')
|
|
187
|
-
return `文档中未找到 ${norm}。已配置接口目录:\n` + list.join('\n')
|
|
212
|
+
if (!args.path) return '【通用说明】接口按集合组织,path 可用「集合名/路径」精确指定。\n\n【已配置接口目录】\n' + catalogText()
|
|
213
|
+
const norm = normPath(args.path)
|
|
214
|
+
const r = resolveTarget(norm)
|
|
215
|
+
if (!r) return `接口 ${norm} 不在已配置列表中,已拒绝查询。可用接口:\n` + catalogText()
|
|
216
|
+
if (r.ambiguous) return ambiguousMsg(norm, r.ambiguous)
|
|
217
|
+
const hit = (r.c.sections || []).filter((s) => s.includes('`' + r.path + '`'))
|
|
218
|
+
return hit.length ? hit.join('\n\n---\n\n') : `文档中未找到 ${r.path}(集合 ${r.c.name})。已配置接口目录:\n` + catalogText()
|
|
188
219
|
},
|
|
189
220
|
})
|
|
190
221
|
|
|
191
222
|
// 工具 2:真正调用接口拿数据(prompt 按领域名拼接)
|
|
192
223
|
ctx.tools.register({
|
|
193
224
|
name: `${prefix}_api_request`,
|
|
194
|
-
description: `调用${domainName}接口并返回 JSON 数据。path
|
|
225
|
+
description: `调用${domainName}接口并返回 JSON 数据。path 必须是已配置集合中的接口(支持 /orders/{id} 这类模板路径,模板段填实际值;跨集合同名接口用「集合名/路径」如 site-a.com/users/detail);GET 用 query 传参,POST 用 body 传 JSON。安全规则:① DELETE 属于危险操作,首次调用只返回确认提示不会执行,必须先向用户二次确认,用户明确同意后才能携带 confirm=true 再次调用;② 返回结果若以 ⚠️ 开头(写操作警告/危险操作确认),必须在回复中原样向用户展示警告块内容(含接口、方法、完整地址与参数),不得省略、转述或只报结果。请依据 ${prefix}_api_doc 返回的文档构造请求,文档即权威,无需从源码核实接口行为。`,
|
|
195
226
|
parameters: {
|
|
196
|
-
path: { type: 'string', required: true, description: '接口路径,如 /users/detail 或 /orders/123' },
|
|
227
|
+
path: { type: 'string', required: true, description: '接口路径,如 /users/detail 或 /orders/123;跨集合同名时用 集合名/路径' },
|
|
197
228
|
method: { type: 'string', description: 'HTTP 方法,默认 GET' },
|
|
198
229
|
query: { type: 'json', description: 'query 参数对象,如 { "keyword": "abc" }' },
|
|
199
230
|
body: { type: 'json', description: 'POST 请求体(JSON 对象)' },
|
|
200
|
-
confirm: { type: 'boolean', description: '
|
|
231
|
+
confirm: { type: 'boolean', description: '删除操作的二次确认标记:仅在与用户确认且收到过确认提示后才能传 true;未经确认提示不得传' },
|
|
201
232
|
},
|
|
202
233
|
output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] },
|
|
203
234
|
async execute(args) {
|
|
204
|
-
const list = currentEndpoints()
|
|
205
235
|
const method = (args.method ?? 'GET').toUpperCase()
|
|
206
|
-
const norm = (
|
|
236
|
+
const norm = normPath(args.path)
|
|
237
|
+
const r = resolveTarget(norm)
|
|
238
|
+
if (!r) return `接口 ${norm} 不在已配置列表中。可用接口:\n` + catalogText()
|
|
239
|
+
if (r.ambiguous) return ambiguousMsg(norm, r.ambiguous)
|
|
207
240
|
|
|
208
|
-
const
|
|
209
|
-
if (!
|
|
210
|
-
|
|
211
|
-
}
|
|
241
|
+
const base = baseUrlOf(r.c)
|
|
242
|
+
if (!base) return `未配置接口服务前缀 baseUrl。请在集合 ${r.c.name} 的 cfg 文件写入 baseUrl=http://... ,或在部署配置中设置。`
|
|
243
|
+
const url = new URL(base.replace(/\/+$/, '') + r.path)
|
|
244
|
+
for (const [k, v] of Object.entries(args.query ?? {})) url.searchParams.set(k, String(v))
|
|
212
245
|
|
|
213
|
-
//
|
|
214
|
-
|
|
215
|
-
if (
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
// 删除操作:二次确认,首次调用不执行,仅返回确认提示
|
|
224
|
-
if (method === 'DELETE' && args.confirm !== true) {
|
|
225
|
-
return `⚠️【危险操作确认】本次将执行删除接口:\n ${method} ${url.toString()}\n` +
|
|
226
|
-
'该操作可能不可恢复。请先向用户二次确认;用户同意后,携带 confirm=true 重新调用本工具才会真正执行。'
|
|
246
|
+
// 删除操作:两段式确认。首次调用只返回确认提示并记录待确认项;
|
|
247
|
+
// 即使模型首次就带 confirm=true,只要没有对应的待确认记录也一律拒绝执行
|
|
248
|
+
if (method === 'DELETE') {
|
|
249
|
+
const key = method + ' ' + url
|
|
250
|
+
if (args.confirm === true && pendingDeletes.has(key)) pendingDeletes.delete(key)
|
|
251
|
+
else {
|
|
252
|
+
pendingDeletes.add(key)
|
|
253
|
+
return `⚠️【危险操作确认】本次将执行删除接口:\n ${method} ${url}\n该操作可能不可恢复。请先向用户二次确认;用户同意后,携带 confirm=true 重新调用本工具才会真正执行。`
|
|
254
|
+
}
|
|
227
255
|
}
|
|
228
256
|
|
|
229
257
|
// 写操作:在结果前向用户明示将执行哪个接口
|
|
230
258
|
const writeWarn = ['POST', 'PUT', 'PATCH'].includes(method)
|
|
231
|
-
? `⚠️【写操作警告】本次请求将执行接口:${method} ${url
|
|
259
|
+
? `⚠️【写操作警告】本次请求将执行接口:${method} ${url}(query: ${JSON.stringify(args.query ?? {})};body: ${JSON.stringify(args.body ?? null)})\n\n`
|
|
232
260
|
: ''
|
|
233
261
|
|
|
234
262
|
const response = await fetch(url, {
|
|
@@ -237,17 +265,12 @@ export function defineApiPlugin(options) {
|
|
|
237
265
|
...(method !== 'GET' && args.body != null ? { body: JSON.stringify(args.body) } : {}),
|
|
238
266
|
})
|
|
239
267
|
const text = await response.text()
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return writeWarn + `HTTP ${response.status} ${response.statusText}\n` + text.slice(0, 4000)
|
|
243
|
-
}
|
|
244
|
-
// 非文件流接口直接返回 JSON 文本,超长截断
|
|
245
|
-
const result = text.length > 30000 ? text.slice(0, 30000) + '\n…(超长截断)' : text
|
|
246
|
-
return writeWarn + result
|
|
268
|
+
if (!response.ok) return writeWarn + `HTTP ${response.status} ${response.statusText}\n` + text.slice(0, 4000)
|
|
269
|
+
return writeWarn + (text.length > 30000 ? text.slice(0, 30000) + '\n…(超长截断)' : text)
|
|
247
270
|
},
|
|
248
271
|
})
|
|
249
272
|
|
|
250
|
-
console.log(`[${ns}] loaded; endpoints=${
|
|
273
|
+
console.log(`[${ns}] loaded; endpoints=${mergedEndpoints(bootCollections).length}, collections=${bootCollections.map((c) => c.name).join(',') || '(无)'}`)
|
|
251
274
|
}
|
|
252
275
|
|
|
253
276
|
return { Config, inject, apply }
|
package/client.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
// 浏览器半侧:在「插件配置」标签页为 dsh-apis-plugin 命名空间注册一张展示卡片
|
|
2
|
-
// 接口的增删以
|
|
2
|
+
// 接口的增删以 cfg 文件为唯一来源:这里只读展示集合手风琴,编辑请改 cfg 后点「扫描」重扫
|
|
3
3
|
// API 目录失焦或点「扫描」时自动保存,无页脚按钮
|
|
4
|
-
// 样式与 DOM 结构对齐原生 PluginCard(li 卡片 + SVG 箭头)
|
|
5
4
|
window.__ModuleLoader__.load({
|
|
6
5
|
id: "dsh-apis-plugin",
|
|
7
6
|
factory: (require) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
11
|
-
let react = require("react");
|
|
7
|
+
const { jsx, jsxs } = require("react/jsx-runtime");
|
|
8
|
+
const react = require("react");
|
|
12
9
|
|
|
13
10
|
/** 本卡片编辑的 settings 命名空间,由宿主侧 index.js 按 cordis.patch.yml 的 id 自动回写 */
|
|
14
11
|
const NS = "dsh-apis-plugin";
|
|
@@ -16,30 +13,29 @@ window.__ModuleLoader__.load({
|
|
|
16
13
|
/** bind 后的命名空间 scope,写入经由它进行(apply 时赋值) */
|
|
17
14
|
let scope;
|
|
18
15
|
|
|
19
|
-
//
|
|
16
|
+
// 精简样式(类名自持,不依赖原生卡片哈希类)
|
|
20
17
|
const css = `
|
|
21
|
-
.apis-card{border:.5px solid var(--dsw-alias-border-l4);background:var(--dsw-alias-bg-layer-3);border-radius:16px;
|
|
18
|
+
.apis-card{list-style:none;border:.5px solid var(--dsw-alias-border-l4);background:var(--dsw-alias-bg-layer-3);border-radius:16px;transition:border-color .16s}
|
|
22
19
|
.apis-card:hover{border-color:var(--dsw-alias-label-dimmed)}
|
|
23
|
-
.apis-
|
|
24
|
-
.apis-
|
|
25
|
-
.apis-
|
|
26
|
-
.apis-
|
|
27
|
-
.apis-
|
|
28
|
-
.apis-chevron{color:var(--dsw-alias-label-tertiary);
|
|
20
|
+
.apis-header{width:100%;font:inherit;color:inherit;text-align:left;cursor:pointer;background:0 0;border:0;display:flex;align-items:center;gap:12px;padding:14px 16px}
|
|
21
|
+
.apis-headText{flex:1;display:flex;flex-direction:column;gap:4px;min-width:0}
|
|
22
|
+
.apis-title{color:var(--dsw-alias-label-primary);font-size:15px;font-weight:600;line-height:1.4}
|
|
23
|
+
.apis-desc,.apis-count,.apis-empty{color:var(--dsw-alias-label-tertiary);font-size:13px}
|
|
24
|
+
.apis-count{font-size:12px;flex:none;white-space:nowrap}
|
|
25
|
+
.apis-chevron{flex:none;color:var(--dsw-alias-label-tertiary);transition:transform .16s}
|
|
29
26
|
.apis-body{border-top:.5px solid var(--dsw-alias-border-l2);margin:0 16px;padding:12px 0 8px;display:grid;gap:10px}
|
|
30
|
-
.apis-
|
|
31
|
-
.apis-
|
|
27
|
+
.apis-row,.apis-item{display:flex;align-items:center;gap:8px;min-width:0}
|
|
28
|
+
.apis-label{flex:none;font-size:13px;color:var(--dsw-alias-label-primary);white-space:nowrap}
|
|
29
|
+
.apis-input{flex:1;min-width:0;box-sizing:border-box;font:inherit;color:var(--dsw-alias-label-primary);background:transparent;border:.5px solid var(--dsw-alias-border-l4);border-radius:8px;padding:6px 10px}
|
|
30
|
+
.apis-btn{font:inherit;cursor:pointer;border:1px solid var(--dsw-alias-border-l2);border-radius:8px;padding:5px 14px;font-size:13px;line-height:1.5;color:var(--dsw-alias-label-secondary);background:0 0}
|
|
31
|
+
.apis-btn:hover:not(:disabled){color:var(--dsw-alias-label-primary)}
|
|
32
32
|
.apis-btn:disabled{opacity:.4;cursor:default}
|
|
33
|
-
.apis-
|
|
34
|
-
.apis-
|
|
35
|
-
.apis-
|
|
36
|
-
.apis-
|
|
37
|
-
.apis-
|
|
38
|
-
.apis-
|
|
39
|
-
.apis-input{box-sizing:border-box;flex:1;min-width:0;font:inherit;color:var(--dsw-alias-label-primary);background:transparent;border:.5px solid var(--dsw-alias-border-l4);border-radius:8px;padding:6px 10px}
|
|
40
|
-
.apis-item{display:flex;align-items:center;gap:8px;min-width:0}
|
|
41
|
-
.apis-itemText{flex:1;min-width:0;font-size:13px;color:var(--dsw-alias-label-primary);font-family:ui-monospace,SFMono-Regular,Consolas,monospace;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding:6px 10px;border:.5px solid var(--dsw-alias-border-l2);border-radius:8px;background:var(--dsw-alias-bg-layer-3)}
|
|
42
|
-
.apis-empty{color:var(--dsw-alias-label-tertiary);font-size:13px;padding:4px 0}
|
|
33
|
+
.apis-text{flex:1;min-width:0;font-family:ui-monospace,Consolas,monospace;font-size:13px;color:var(--dsw-alias-label-primary);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;border:.5px solid var(--dsw-alias-border-l2);border-radius:8px;padding:6px 10px}
|
|
34
|
+
.apis-group{border:.5px solid var(--dsw-alias-border-l2);border-radius:10px;overflow:hidden}
|
|
35
|
+
.apis-groupHead{width:100%;font:inherit;color:inherit;text-align:left;cursor:pointer;background:0 0;border:0;display:flex;align-items:center;gap:8px;padding:8px 10px}
|
|
36
|
+
.apis-groupHead:hover{background:var(--dsw-alias-bg-layer-2)}
|
|
37
|
+
.apis-groupName{flex:1;min-width:0;font-size:13px;font-weight:600;color:var(--dsw-alias-label-primary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
38
|
+
.apis-groupBody{border-top:.5px solid var(--dsw-alias-border-l2);padding:8px 10px;display:grid;gap:8px}
|
|
43
39
|
`;
|
|
44
40
|
if (typeof document !== "undefined" && document.querySelector("style[data-plugin-css=dsh-apis-plugin]") === null) {
|
|
45
41
|
const tag = document.createElement("style");
|
|
@@ -65,13 +61,13 @@ window.__ModuleLoader__.load({
|
|
|
65
61
|
};
|
|
66
62
|
}
|
|
67
63
|
|
|
68
|
-
/**
|
|
64
|
+
/** 下拉箭头(14px 线性 chevron) */
|
|
69
65
|
function Chevron({ open }) {
|
|
70
|
-
return
|
|
66
|
+
return jsx("svg", {
|
|
71
67
|
width: 14, height: 14, viewBox: "0 0 14 14", fill: "none", "aria-hidden": true,
|
|
72
68
|
className: "apis-chevron",
|
|
73
69
|
style: { transform: open ? "rotate(180deg)" : "none" },
|
|
74
|
-
children:
|
|
70
|
+
children: jsx("path", {
|
|
75
71
|
d: "M3.5 5.25 7 8.75l3.5-3.5",
|
|
76
72
|
stroke: "currentColor", strokeWidth: 1.2, strokeLinecap: "round", strokeLinejoin: "round",
|
|
77
73
|
}),
|
|
@@ -83,24 +79,57 @@ window.__ModuleLoader__.load({
|
|
|
83
79
|
const [dirDraft, setDirDraft] = react.useState(null); // 文档目录,null 表示未编辑
|
|
84
80
|
const [loading, setLoading] = react.useState(false); // 扫描进行中
|
|
85
81
|
const [loaded, setLoaded] = react.useState(false); // 扫描完成后短暂提示
|
|
86
|
-
const [open, setOpen] = react.useState(false); //
|
|
82
|
+
const [open, setOpen] = react.useState(false); // 卡片折叠态
|
|
83
|
+
const [openGroups, setOpenGroups] = react.useState({}); // 集合手风琴展开态(key: 集合名)
|
|
87
84
|
const ready = state.status === "ready" && state.writable;
|
|
88
85
|
|
|
89
|
-
/**
|
|
86
|
+
/** 扫描得到的接口集合(服务端回写 JSON):[{ name, baseUrl, apis }] */
|
|
87
|
+
const groups = (() => {
|
|
88
|
+
try {
|
|
89
|
+
const v = JSON.parse(state.value?.collections ?? "[]");
|
|
90
|
+
return Array.isArray(v) ? v : [];
|
|
91
|
+
} catch {
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
})();
|
|
95
|
+
/** 兜底平铺列表:集合为空时展示(如仅部署侧固定接口) */
|
|
90
96
|
const list = (state.value?.endpoints ?? []).map(String);
|
|
91
|
-
/** 当前展示的文档目录:编辑中取 dirDraft,否则取已保存值 */
|
|
92
97
|
const savedDir = state.value?.apiDir ?? "";
|
|
93
98
|
const shownDir = dirDraft ?? savedDir;
|
|
94
99
|
const dirDirty = dirDraft !== null && dirDraft !== savedDir;
|
|
95
100
|
|
|
96
|
-
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
+
/** 只读单行:接口N 标签 + 路径文本 */
|
|
102
|
+
const row = (v, i) => jsxs("div", { className: "apis-item", children: [
|
|
103
|
+
jsx("div", { className: "apis-label", children: `接口${i + 1}` }),
|
|
104
|
+
jsx("span", { className: "apis-text", title: v, children: v }),
|
|
105
|
+
] });
|
|
101
106
|
|
|
102
|
-
/**
|
|
103
|
-
|
|
107
|
+
/** 集合手风琴:头行为 集合名 + 接口数 + 箭头,展开体含 baseUrl 与接口列表(只读) */
|
|
108
|
+
const group = (g) => {
|
|
109
|
+
const isOpen = !!openGroups[g.name];
|
|
110
|
+
const apis = (g.apis ?? []).map(String);
|
|
111
|
+
return jsxs("div", { className: "apis-group", children: [
|
|
112
|
+
jsxs("button", {
|
|
113
|
+
type: "button", className: "apis-groupHead", "aria-expanded": isOpen,
|
|
114
|
+
onClick: () => setOpenGroups((m) => ({ ...m, [g.name]: !m[g.name] })),
|
|
115
|
+
children: [
|
|
116
|
+
jsx(Chevron, { open: isOpen }),
|
|
117
|
+
jsx("span", { className: "apis-groupName", title: g.name, children: g.name }),
|
|
118
|
+
jsx("span", { className: "apis-count", children: `${apis.length} 个接口` }),
|
|
119
|
+
],
|
|
120
|
+
}),
|
|
121
|
+
isOpen && jsxs("div", { className: "apis-groupBody", children: [
|
|
122
|
+
g.baseUrl && jsxs("div", { className: "apis-item", children: [
|
|
123
|
+
jsx("div", { className: "apis-label", children: "baseUrl" }),
|
|
124
|
+
jsx("span", { className: "apis-text", title: g.baseUrl, children: g.baseUrl }),
|
|
125
|
+
] }),
|
|
126
|
+
apis.length ? apis.map(row) : jsx("span", { className: "apis-empty", children: "该集合暂无接口" }),
|
|
127
|
+
] }),
|
|
128
|
+
] });
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** 扫描:目录有改动先落盘,再翻转 scanToken 让服务端重扫 cfg 并回写集合;目录为空则清空列表 */
|
|
132
|
+
async function scan() {
|
|
104
133
|
if (loading || !ready) return;
|
|
105
134
|
setLoading(true);
|
|
106
135
|
try {
|
|
@@ -114,59 +143,48 @@ window.__ModuleLoader__.load({
|
|
|
114
143
|
}
|
|
115
144
|
}
|
|
116
145
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return react_jsx_runtime.jsxs("li", {
|
|
127
|
-
className: "apis-card",
|
|
128
|
-
"data-open": open,
|
|
129
|
-
children: [
|
|
130
|
-
// 折叠头:标题 + 描述 + 箭头,点击切换展开
|
|
131
|
-
react_jsx_runtime.jsxs("button", {
|
|
132
|
-
type: "button",
|
|
133
|
-
className: "apis-header",
|
|
134
|
-
"aria-expanded": open,
|
|
135
|
-
onClick: () => setOpen((v) => !v),
|
|
136
|
-
children: [
|
|
137
|
-
react_jsx_runtime.jsxs("div", { className: "apis-headText", children: [
|
|
138
|
-
react_jsx_runtime.jsx("span", { className: "apis-name", children: "通用接口管理插件" }),
|
|
139
|
-
react_jsx_runtime.jsx("span", { className: "apis-description", children: "接口列表(来源:api.cfg)" }),
|
|
140
|
-
] }),
|
|
141
|
-
react_jsx_runtime.jsx(Chevron, { open }),
|
|
142
|
-
],
|
|
143
|
-
}),
|
|
144
|
-
// 展开体:API配置目录行 + 分隔线 + 接口行列表(只读)
|
|
145
|
-
open && react_jsx_runtime.jsxs("div", { className: "apis-body", children: [
|
|
146
|
-
// API配置:文档目录(api.cfg / api_doc.md 所在目录,失焦自动保存)+ 扫描按钮(目录为空则清空列表)
|
|
147
|
-
react_jsx_runtime.jsxs("div", { className: "apis-row", children: [
|
|
148
|
-
react_jsx_runtime.jsx("div", { className: "apis-rowLabel apis-rowLabelWide", children: "API配置" }),
|
|
149
|
-
react_jsx_runtime.jsx("input", {
|
|
150
|
-
value: shownDir, disabled: !ready || loading,
|
|
151
|
-
onChange: (e) => setDirDraft(e.target.value),
|
|
152
|
-
onBlur: commitDir,
|
|
153
|
-
onKeyDown: (e) => { if (e.key === "Enter") e.currentTarget.blur(); },
|
|
154
|
-
placeholder: "api.cfg / api_doc.md 所在目录,留空扫描即清空列表",
|
|
155
|
-
className: "apis-input",
|
|
156
|
-
}),
|
|
157
|
-
react_jsx_runtime.jsx("button", {
|
|
158
|
-
type: "button", disabled: !ready || loading,
|
|
159
|
-
onClick: loadApis,
|
|
160
|
-
className: "apis-btn apis-discard", children: loading ? "扫描中…" : "扫描",
|
|
161
|
-
}),
|
|
162
|
-
loaded && react_jsx_runtime.jsx("span", { className: "apis-saved", children: "已扫描" }),
|
|
146
|
+
return jsxs("li", { className: "apis-card", children: [
|
|
147
|
+
// 折叠头:标题 + 描述 + 箭头,点击切换展开
|
|
148
|
+
jsxs("button", {
|
|
149
|
+
type: "button", className: "apis-header", "aria-expanded": open,
|
|
150
|
+
onClick: () => setOpen((v) => !v),
|
|
151
|
+
children: [
|
|
152
|
+
jsxs("div", { className: "apis-headText", children: [
|
|
153
|
+
jsx("span", { className: "apis-title", children: "通用接口管理插件" }),
|
|
154
|
+
jsx("span", { className: "apis-desc", children: "接口集合(来源:cfg 的 host 字段)" }),
|
|
163
155
|
] }),
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
156
|
+
jsx(Chevron, { open }),
|
|
157
|
+
],
|
|
158
|
+
}),
|
|
159
|
+
// 展开体:API配置目录行 + 集合手风琴(无集合时回退平铺列表)
|
|
160
|
+
open && jsxs("div", { className: "apis-body", children: [
|
|
161
|
+
jsxs("div", { className: "apis-row", children: [
|
|
162
|
+
jsx("div", { className: "apis-label", children: "API配置" }),
|
|
163
|
+
jsx("input", {
|
|
164
|
+
value: shownDir, disabled: !ready || loading,
|
|
165
|
+
onChange: (e) => setDirDraft(e.target.value),
|
|
166
|
+
onBlur: () => {
|
|
167
|
+
if (dirDirty && ready) scope.set("apiDir", dirDraft);
|
|
168
|
+
setDirDraft(null);
|
|
169
|
+
},
|
|
170
|
+
onKeyDown: (e) => e.key === "Enter" && e.currentTarget.blur(),
|
|
171
|
+
placeholder: "集合父目录(扫描各子文件夹下的 *.cfg,以其中 host 为集合名),留空扫描即清空列表",
|
|
172
|
+
className: "apis-input",
|
|
173
|
+
}),
|
|
174
|
+
jsx("button", {
|
|
175
|
+
type: "button", disabled: !ready || loading, onClick: scan,
|
|
176
|
+
className: "apis-btn", children: loading ? "扫描中…" : "扫描",
|
|
177
|
+
}),
|
|
178
|
+
loaded && jsx("span", { className: "apis-count", children: "已扫描" }),
|
|
167
179
|
] }),
|
|
168
|
-
|
|
169
|
-
|
|
180
|
+
!state.writable && jsx("span", { className: "apis-desc", children: "只读" }),
|
|
181
|
+
groups.length
|
|
182
|
+
? groups.map(group)
|
|
183
|
+
: list.length
|
|
184
|
+
? list.map(row)
|
|
185
|
+
: jsx("span", { className: "apis-empty", children: "暂无接口:请在 API 目录下的各集合 cfg 文件中配置 host、baseUrl 与 apis,然后点击「扫描」" }),
|
|
186
|
+
] }),
|
|
187
|
+
] });
|
|
170
188
|
}
|
|
171
189
|
|
|
172
190
|
/** 需要的浏览器侧服务 */
|
|
@@ -187,8 +205,6 @@ window.__ModuleLoader__.load({
|
|
|
187
205
|
}, ApisCard)));
|
|
188
206
|
}
|
|
189
207
|
|
|
190
|
-
|
|
191
|
-
exports.inject = inject;
|
|
192
|
-
return module.exports;
|
|
208
|
+
return { apply, inject };
|
|
193
209
|
}
|
|
194
210
|
});
|