dsh-apis-plugin 0.2.3 → 0.3.1
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 +32 -74
- package/api-tools.js +67 -38
- package/client.js +53 -71
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -1,87 +1,58 @@
|
|
|
1
1
|
# dsh-apis-plugin
|
|
2
2
|
|
|
3
|
-
通用接口管理插件(
|
|
3
|
+
通用接口管理插件(DSH 插件):把任意一套 HTTP 接口注册成 agent 可用的**文档查询 + 请求调用**双工具。
|
|
4
4
|
|
|
5
|
-
核心约定:**`api.cfg`
|
|
5
|
+
核心约定:**`api.cfg` 是接口的唯一权威范围**——范围外的请求一律拒绝。
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## 版本兼容
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
dsh plugin --profile web add dsh-apis-plugin
|
|
11
|
-
```
|
|
9
|
+
需要 **DSH >=0.1.7-rc.2**(详情页徽章据此显示)。DSH 0.1.5 / 0.1.6 请用旧版 `dsh-apis-plugin@0.2.3`。
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
## 安装
|
|
14
12
|
|
|
15
13
|
```sh
|
|
16
|
-
dsh plugin --profile web add dsh-apis-plugin@0.1
|
|
14
|
+
dsh plugin --profile web add dsh-apis-plugin@0.3.1
|
|
17
15
|
```
|
|
18
16
|
|
|
19
17
|
## 配置
|
|
20
18
|
|
|
21
|
-
「API
|
|
19
|
+
「API配置」指向一个**集合父目录**,插件递归扫描其下所有 `*.cfg`,**每个 cfg 是一个集合**(集合名取 cfg 的 `host` 字段):
|
|
22
20
|
|
|
23
21
|
```
|
|
24
22
|
config/
|
|
25
|
-
├── site-a.example.com/
|
|
26
|
-
│ ├── api.cfg #
|
|
23
|
+
├── site-a.example.com/
|
|
24
|
+
│ ├── api.cfg # host=site-a.example.com
|
|
27
25
|
│ └── api_doc.md # 该集合的接口文档
|
|
28
26
|
└── site-b.example.com/
|
|
29
|
-
├── api.cfg
|
|
27
|
+
├── api.cfg
|
|
30
28
|
└── api_doc.md
|
|
31
29
|
```
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
### 1. xxx.cfg(每个集合一份,必需)
|
|
36
|
-
|
|
37
|
-
key=value 行格式,`#` 注释,UTF-8 编码:
|
|
31
|
+
### api.cfg(每个集合一份,必需)
|
|
38
32
|
|
|
39
33
|
```ini
|
|
40
|
-
#
|
|
34
|
+
# 集合标识(如域名),用于展示与「集合名/路径」消歧
|
|
41
35
|
host=site-a.example.com
|
|
42
36
|
|
|
43
|
-
#
|
|
37
|
+
# 集合用途说明(可选),展示在接口目录与手风琴中
|
|
44
38
|
title=xxx
|
|
45
39
|
|
|
46
|
-
#
|
|
40
|
+
# 该集合的接口服务前缀
|
|
47
41
|
baseUrl=http://your-host/api
|
|
48
42
|
|
|
49
|
-
#
|
|
43
|
+
# 接口白名单:逐行一个,支持 {param} 模板段
|
|
50
44
|
apis=
|
|
51
45
|
/users/page
|
|
52
46
|
/users/{id}
|
|
53
47
|
```
|
|
54
48
|
|
|
55
|
-
|
|
56
|
-
- `title`:集合用途说明(可选),AI 查目录与 UI 手风琴均会展示
|
|
57
|
-
- `baseUrl`:请求时拼接在该集合的接口路径前(改文件即生效,无需重启)
|
|
58
|
-
- `apis`:该集合的接口白名单,支持 `{param}` 模板段(如 `/users/{id}`),agent 请求时填实际值
|
|
59
|
-
- **增删接口只改这个文件**,改完点插件配置页的「扫描」(或重启)生效
|
|
60
|
-
|
|
61
|
-
### 2. api_doc.md(接口文档,建议提供)
|
|
62
|
-
|
|
63
|
-
与 `api.cfg` 放在一起。用 `---` 分节,每节描述一个接口,节内用反引号标注接口路径:
|
|
64
|
-
|
|
65
|
-
```markdown
|
|
66
|
-
## 通用说明
|
|
67
|
-
|
|
68
|
-
所有接口返回 JSON,需携带 token。
|
|
49
|
+
改 cfg 后点插件详情页的「扫描」(或重启)生效。
|
|
69
50
|
|
|
70
|
-
|
|
51
|
+
### api_doc.md(接口文档,建议提供)
|
|
71
52
|
|
|
72
|
-
|
|
73
|
-
- 参数:keyword(可选)、page、pageSize
|
|
53
|
+
与 `api.cfg` 放在一起,用 `---` 分节,每节描述一个接口,节内用反引号标注路径(如 `` `/users/page` ``)。agent 调用 `{prefix}_api_doc` 时按路径命中对应小节返回。
|
|
74
54
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
- 路径:`/users/{id}`(GET)
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
agent 调用 `{prefix}_api_doc` 时按路径命中对应小节返回。
|
|
81
|
-
|
|
82
|
-
💡 建议:在「通用说明」里注明 *“本文档与线上服务一一对应,以此为准”*,并写清字段语义(如 `status=0 编辑中,1 待审核`)——工具描述已声明文档为权威依据,写清楚后 agent 会直接按文档调用,不会再去翻后端源码核实。
|
|
83
|
-
|
|
84
|
-
### 3. 部署侧 cordis.patch.yml(可选覆盖)
|
|
55
|
+
### 部署侧 cordis.patch.yml(可选)
|
|
85
56
|
|
|
86
57
|
```yaml
|
|
87
58
|
- insert:
|
|
@@ -90,54 +61,41 @@ agent 调用 `{prefix}_api_doc` 时按路径命中对应小节返回。
|
|
|
90
61
|
config:
|
|
91
62
|
domain: "用户中心" # 领域名:拼进工具描述 prompt
|
|
92
63
|
toolPrefix: api # 工具名前缀:生成 api_api_doc / api_api_request
|
|
93
|
-
apiDir: "" #
|
|
94
|
-
endpoints: [] #
|
|
64
|
+
apiDir: "" # 集合父目录默认值(留空则在 UI 里填)
|
|
65
|
+
endpoints: [] # 额外固定接口(不随扫描回写)
|
|
95
66
|
baseUrl: "" # 服务前缀兜底(api.cfg 优先)
|
|
96
67
|
```
|
|
97
68
|
|
|
98
69
|
## 使用
|
|
99
70
|
|
|
100
|
-
### Web UI(设置 →
|
|
71
|
+
### Web UI(设置 → 接口管理)
|
|
101
72
|
|
|
102
73
|
| 操作 | 说明 |
|
|
103
74
|
|---|---|
|
|
104
|
-
| API
|
|
105
|
-
| 扫描 |
|
|
106
|
-
| 集合手风琴 |
|
|
75
|
+
| API配置 | 填集合父目录,失焦或回车自动保存 |
|
|
76
|
+
| 扫描 | 重扫目录;**目录留空点击则清空列表** |
|
|
77
|
+
| 集合手风琴 | 展示集合名、用途、接口数、baseUrl 与接口列表(只读,增删请在 cfg 中操作) |
|
|
107
78
|
|
|
108
79
|
### Agent 工具
|
|
109
80
|
|
|
110
|
-
插件向 agent 注册两个工具(`{prefix}` 默认 `api`):
|
|
111
|
-
|
|
112
81
|
| 工具 | 用途 |
|
|
113
82
|
|---|---|
|
|
114
|
-
| `api_api_doc` | 查接口文档:传 `path`
|
|
115
|
-
| `api_api_request` |
|
|
116
|
-
|
|
117
|
-
多集合规则:
|
|
118
|
-
|
|
119
|
-
- path 默认写接口路径(如 `/users/page`),在全部集合中查找;**跨集合同名接口需用「集合名/路径」消歧**(如 `site-a.example.com/users/page`)
|
|
120
|
-
- 每个集合使用自己 api.cfg 里的 baseUrl 发请求,互不混淆
|
|
121
|
-
- 接口文档也按集合独立查找(各集合自己的 api_doc.md)
|
|
122
|
-
|
|
123
|
-
超出 api.cfg 范围的请求(无论查文档还是发请求)会被直接拒绝,并提示可用接口列表。
|
|
124
|
-
|
|
125
|
-
安全机制:
|
|
83
|
+
| `api_api_doc` | 查接口文档:传 `path` 返回该接口的参数与响应说明;不传返回全部接口目录 |
|
|
84
|
+
| `api_api_request` | 调用接口:`path` 必须在白名单内,GET 用 `query`,POST 用 `body` |
|
|
126
85
|
|
|
127
|
-
-
|
|
128
|
-
-
|
|
86
|
+
- path 默认写接口路径(如 `/users/page`);**跨集合同名接口用「集合名/路径」消歧**(如 `site-a.example.com/users/page`)
|
|
87
|
+
- 安全机制:`DELETE` 需二次确认(首次只返回确认提示,确认后带 `confirm=true` 再调);写操作返回结果附带警告块
|
|
129
88
|
|
|
130
89
|
## 排查
|
|
131
90
|
|
|
132
91
|
dsh 控制台日志:
|
|
133
92
|
|
|
134
93
|
```
|
|
135
|
-
[dsh-apis-plugin] loaded; endpoints=N, collections=a.com,b.com
|
|
94
|
+
[dsh-apis-plugin] loaded; apiDir=/path/to/config, endpoints=N, collections=a.com,b.com
|
|
136
95
|
```
|
|
137
96
|
|
|
138
|
-
- `endpoints=0`:检查
|
|
139
|
-
-
|
|
140
|
-
- 提示未配置 baseUrl:在对应集合的 cfg 文件写入 `baseUrl=http://...`
|
|
97
|
+
- `endpoints=0`:检查 apiDir 是否正确、其下是否有含 apis 的 `*.cfg`
|
|
98
|
+
- 列表与 cfg 不一致:点「扫描」或重启
|
|
141
99
|
|
|
142
100
|
## License
|
|
143
101
|
|
package/api-tools.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// 可复用的「接口工具插件」工厂:把任意一套 HTTP 接口注册成 agent 的 {prefix}_api_doc / {prefix}_api_request 双工具
|
|
2
2
|
// 领域差异(settings 命名空间、工具名前缀、领域名、文档目录、baseUrl、默认接口列表)由调用方注入,
|
|
3
3
|
// 其中 domain / toolPrefix / apiDir 也可在部署侧 cordis.patch.yml 的 config 里覆盖
|
|
4
|
+
// DSH 0.1.7+:插件 Config 即设置表单,apiDir / scanToken / collections 声明为 volatile 字段;
|
|
5
|
+
// 服务端扫描结果经 ctx.settings.update 写回,客户端卡片经 configForms 表单读写
|
|
4
6
|
import { readFileSync, writeFileSync, readdirSync } from 'node:fs'
|
|
5
7
|
import { join, dirname, basename } from 'node:path'
|
|
6
8
|
import Schema from '@deepseek-ai/schemastery'
|
|
@@ -103,6 +105,11 @@ export function syncClientNs(dir, ns) {
|
|
|
103
105
|
if (updated !== code) writeFileSync(file, updated)
|
|
104
106
|
}
|
|
105
107
|
|
|
108
|
+
/** 取 volatile 配置引用的当前快照;非引用原样返回 */
|
|
109
|
+
function val(v) {
|
|
110
|
+
return v !== null && typeof v === 'object' && typeof v.get === 'function' ? v.get() : v
|
|
111
|
+
}
|
|
112
|
+
|
|
106
113
|
/**
|
|
107
114
|
* 构造一个可被 cordis 直接加载的插件模块:{ Config, inject, apply }
|
|
108
115
|
* @param {object} options
|
|
@@ -116,60 +123,82 @@ export function syncClientNs(dir, ns) {
|
|
|
116
123
|
export function defineApiPlugin(options) {
|
|
117
124
|
const { ns, toolPrefix = 'api', domain = '', apiDir = '', defaultBaseUrl = '', defaultEndpoints = [] } = options
|
|
118
125
|
|
|
126
|
+
// DSH 0.1.7 写回通道:扫描结果要写回 settings(进 profile patch)供客户端卡片展示,
|
|
127
|
+
// 但 HMR 事务期间(loader/volatile-update 事件、live 重载触发的重应用都在事务内)禁止再开
|
|
128
|
+
// 写事务("HMR transactions cannot be nested"),且事务的 AsyncLocalStorage 会沿定时器等异步链
|
|
129
|
+
// 传播、无法用 defer 逃逸。启动时的首次 apply 不在事务内,故写回定时器只在首次 apply 时创建,
|
|
130
|
+
// 其回调永远不带事务上下文;apply / 事件回调只置 pending 标记与待写内容,由定时器落盘。
|
|
131
|
+
let flushState = null // { ctx, config, serialized }
|
|
132
|
+
let flushPending = false
|
|
133
|
+
let flushStarted = false
|
|
134
|
+
|
|
119
135
|
const Config = Schema.object({
|
|
120
136
|
domain: Schema.string().default(domain), // 领域名:拼进工具描述 prompt
|
|
121
137
|
toolPrefix: Schema.string().default(toolPrefix), // 工具名前缀:生成 {prefix}_api_doc / {prefix}_api_request
|
|
122
|
-
apiDir: Schema.string().default(apiDir), // cfg 与 api_doc.md 所在目录(留空则只用 endpoints
|
|
123
|
-
endpoints: Schema.array(Schema.string()).default([]),
|
|
138
|
+
apiDir: Schema.string().default(apiDir).volatile(), // cfg 与 api_doc.md 所在目录(留空则只用 endpoints);客户端卡片可写
|
|
139
|
+
endpoints: Schema.array(Schema.string()).default([]), // 部署侧固定接口(yml config 注入,不随扫描回写)
|
|
124
140
|
baseUrl: Schema.string().default(defaultBaseUrl), // 接口服务前缀(路径本身已含服务前缀)
|
|
125
|
-
scanToken: Schema.string().default(''), //
|
|
126
|
-
collections: Schema.string().default(''), // 内部字段:扫描到的集合 JSON [{name, baseUrl, apis}],客户端只读展示
|
|
141
|
+
scanToken: Schema.string().default('').volatile(), // 内部字段:客户端「扫描」按钮改写它触发配置变更重应用
|
|
142
|
+
collections: Schema.string().default('').volatile(), // 内部字段:扫描到的集合 JSON [{name, baseUrl, apis}],客户端只读展示
|
|
127
143
|
})
|
|
128
144
|
|
|
129
|
-
const inject = ['
|
|
145
|
+
const inject = ['tools', 'settings']
|
|
130
146
|
|
|
131
147
|
function apply(ctx, config) {
|
|
132
148
|
const prefix = config.toolPrefix || toolPrefix
|
|
133
149
|
const domainName = config.domain || domain
|
|
134
|
-
|
|
135
|
-
//
|
|
150
|
+
// DSH 0.1.7:volatile 字段(apiDir/scanToken/collections)在 config 里是稳定 Volatile 引用,
|
|
151
|
+
// 值更新不重跑 apply,而是原地写入引用并发出 loader/volatile-update 事件;
|
|
152
|
+
// .get() 取当前快照,事件回调里重扫即可让「扫描」与目录修改实时生效
|
|
153
|
+
// 部署侧固定接口(yml config.endpoints + 工厂默认);cfg 扫描结果只落在 collections,二者运行时取并集
|
|
136
154
|
const staticEndpoints = [...new Set([...defaultEndpoints, ...(config.endpoints ?? [])])]
|
|
137
155
|
// 集合持久化为 JSON(客户端只读展示;文档分节不落盘)
|
|
138
156
|
const plain = (cs) => JSON.stringify(cs.map(({ name, title, baseUrl, apis }) => ({ name, title, baseUrl, apis })))
|
|
139
157
|
const mergedEndpoints = (cs) => [...new Set([...cs.flatMap((c) => c.apis), ...staticEndpoints])]
|
|
140
158
|
|
|
141
|
-
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
159
|
+
// 按当前 apiDir 重扫 cfg;结果与配置不一致时经 settings.update 写回 profile patch(写回会触发
|
|
160
|
+
// 一次 volatile 更新,下一轮扫描结果与配置一致即收敛,不会循环)。
|
|
161
|
+
// 写回不在当前调用栈直接执行:loader/volatile-update 事件与 live 重载的重应用都在 HMR 事务内,
|
|
162
|
+
// 事务内再开写事务会被拒绝,故只置 pending 标记,由首次 apply 创建的定时器(事务外)落盘
|
|
163
|
+
function rescan() {
|
|
164
|
+
const dir = val(config.apiDir) || apiDir
|
|
165
|
+
const cs = loadCollections(dir)
|
|
166
|
+
const serialized = plain(cs)
|
|
167
|
+
if (serialized !== val(config.collections)) {
|
|
168
|
+
flushState = { ctx, config, serialized }
|
|
169
|
+
flushPending = true
|
|
170
|
+
}
|
|
171
|
+
return cs
|
|
172
|
+
}
|
|
149
173
|
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
|
|
174
|
+
// 写回定时器只在首次 apply 创建(boot 的 apply 不在 HMR 事务内,其回调永无事务上下文);
|
|
175
|
+
// flushing 锁保证写回串行——上一笔 update 完成前不放行下一笔,避免乱序覆盖
|
|
176
|
+
if (!flushStarted) {
|
|
177
|
+
flushStarted = true
|
|
178
|
+
let flushing = false
|
|
179
|
+
setInterval(async () => {
|
|
180
|
+
if (!flushPending || flushing) return
|
|
181
|
+
flushPending = false
|
|
182
|
+
flushing = true
|
|
183
|
+
const { ctx, config, serialized } = flushState
|
|
153
184
|
try {
|
|
154
|
-
|
|
155
|
-
if (dir !== docDir || next.scanToken !== lastScanToken) {
|
|
156
|
-
docDir = dir
|
|
157
|
-
collections = loadCollections(dir)
|
|
158
|
-
}
|
|
159
|
-
if (next.scanToken !== lastScanToken) {
|
|
160
|
-
lastScanToken = next.scanToken
|
|
161
|
-
const patch = {}
|
|
162
|
-
const merged = mergedEndpoints(collections)
|
|
163
|
-
if (merged.join('\n') !== (Array.isArray(next.endpoints) ? next.endpoints : []).join('\n')) patch.endpoints = merged
|
|
164
|
-
if (plain(collections) !== next.collections) patch.collections = plain(collections)
|
|
165
|
-
if (Object.keys(patch).length) scope.update(patch)
|
|
166
|
-
}
|
|
185
|
+
if (serialized !== val(config.collections)) await ctx.settings.update(ns, { collections: serialized })
|
|
167
186
|
} catch (err) {
|
|
168
|
-
console.error(`[${ns}]
|
|
187
|
+
console.error(`[${ns}] settings sync error:`, err)
|
|
188
|
+
} finally {
|
|
189
|
+
flushing = false
|
|
169
190
|
}
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
|
|
191
|
+
}, 50)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
let collections = rescan()
|
|
195
|
+
ctx.effect(() => ctx.on('loader/volatile-update', () => {
|
|
196
|
+
try {
|
|
197
|
+
collections = rescan()
|
|
198
|
+
} catch (err) {
|
|
199
|
+
console.error(`[${ns}] rescan error:`, err)
|
|
200
|
+
}
|
|
201
|
+
}), `${ns}: volatile rescan`)
|
|
173
202
|
|
|
174
203
|
// 已配置接口目录(按集合分组展示,title 说明集合用途;部署侧静态接口不在任何集合时单独一组)
|
|
175
204
|
function catalogText() {
|
|
@@ -197,13 +226,13 @@ export function defineApiPlugin(options) {
|
|
|
197
226
|
if (hits.length > 1) return { ambiguous: hits }
|
|
198
227
|
if (hits.length === 1) return { c: hits[0], path: norm }
|
|
199
228
|
if (staticEndpoints.some((e) => matchEndpoint(norm, e))) {
|
|
200
|
-
return { c: { name: '_static', baseUrl:
|
|
229
|
+
return { c: { name: '_static', baseUrl: config.baseUrl || '', apis: staticEndpoints, sections: [] }, path: norm }
|
|
201
230
|
}
|
|
202
231
|
return null
|
|
203
232
|
}
|
|
204
233
|
|
|
205
234
|
// 集合的服务前缀:实时重读该集合的 cfg 文件(改文件即生效,无需重启),回退扫描快照
|
|
206
|
-
const baseUrlOf = (c) => c.name === '_static' ?
|
|
235
|
+
const baseUrlOf = (c) => c.name === '_static' ? config.baseUrl || '' : loadCfgFile(c.file).baseUrl || c.baseUrl || ''
|
|
207
236
|
const ambiguousMsg = (norm, hits) => `接口 ${norm} 在多个集合中命中(${hits.map((c) => c.name).join('、')}),请用「集合名/路径」精确指定,如:${hits[0].name}${norm}`
|
|
208
237
|
const normPath = (p) => ('/' + p.trim().replace(/^\/+/, '')).split('?')[0]
|
|
209
238
|
|
|
@@ -280,7 +309,7 @@ export function defineApiPlugin(options) {
|
|
|
280
309
|
},
|
|
281
310
|
})
|
|
282
311
|
|
|
283
|
-
console.log(`[${ns}] loaded; endpoints=${mergedEndpoints(
|
|
312
|
+
console.log(`[${ns}] loaded; apiDir=${val(config.apiDir) || '(未配置)'}, endpoints=${mergedEndpoints(collections).length}, collections=${collections.map((c) => c.name).join(',') || '(无)'}`)
|
|
284
313
|
}
|
|
285
314
|
|
|
286
315
|
return { Config, inject, apply }
|
package/client.js
CHANGED
|
@@ -1,29 +1,26 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 浏览器半侧:在设置弹窗注册「接口管理」菜单项(DSH 0.1.7+ 的 settings.section 槽位)
|
|
2
2
|
// 接口的增删以 cfg 文件为唯一来源:这里只读展示集合手风琴,编辑请改 cfg 后点「扫描」重扫
|
|
3
|
-
// API
|
|
3
|
+
// API 目录失焦或点「扫描」时经 configForms 表单写入,由宿主持久化到 profile 的 cordis.patch.yml
|
|
4
4
|
window.__ModuleLoader__.load({
|
|
5
5
|
id: "dsh-apis-plugin",
|
|
6
6
|
factory: (require) => {
|
|
7
7
|
const { jsx, jsxs } = require("react/jsx-runtime");
|
|
8
8
|
const react = require("react");
|
|
9
9
|
|
|
10
|
-
/**
|
|
10
|
+
/** 本设置页读写的 settings 命名空间,须与宿主侧 index.js 按 cordis.patch.yml 的 id 回写一致(亦即 Host 插件条目 id) */
|
|
11
11
|
const NS = "dsh-apis-plugin";
|
|
12
12
|
|
|
13
|
-
/** bind
|
|
14
|
-
let
|
|
13
|
+
/** bind 后的配置表单与快照 store(apply 时赋值):读快照、写字段都经由它 */
|
|
14
|
+
let form;
|
|
15
|
+
let store;
|
|
15
16
|
|
|
16
17
|
// 精简样式(类名自持,不依赖原生卡片哈希类)
|
|
17
18
|
const css = `
|
|
18
|
-
.apis-
|
|
19
|
-
.apis-card:hover{border-color:var(--dsw-alias-label-dimmed)}
|
|
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}
|
|
19
|
+
.apis-section{display:grid;gap:12px;align-content:start;padding:4px 2px}
|
|
21
20
|
.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
21
|
.apis-desc,.apis-count,.apis-empty{color:var(--dsw-alias-label-tertiary);font-size:13px}
|
|
24
22
|
.apis-count{font-size:12px;flex:none;white-space:nowrap}
|
|
25
23
|
.apis-chevron{flex:none;color:var(--dsw-alias-label-tertiary);transition:transform .16s}
|
|
26
|
-
.apis-body{border-top:.5px solid var(--dsw-alias-border-l2);margin:0 16px;padding:12px 0 8px;display:grid;gap:10px}
|
|
27
24
|
.apis-row,.apis-item{display:flex;align-items:center;gap:8px;min-width:0}
|
|
28
25
|
.apis-label{flex:none;font-size:13px;color:var(--dsw-alias-label-primary);white-space:nowrap}
|
|
29
26
|
.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}
|
|
@@ -74,12 +71,11 @@ window.__ModuleLoader__.load({
|
|
|
74
71
|
});
|
|
75
72
|
}
|
|
76
73
|
|
|
77
|
-
function
|
|
78
|
-
const state =
|
|
74
|
+
function ApisSection() {
|
|
75
|
+
const state = react.useSyncExternalStore(store.subscribe, store.getSnapshot);
|
|
79
76
|
const [dirDraft, setDirDraft] = react.useState(null); // 文档目录,null 表示未编辑
|
|
80
77
|
const [loading, setLoading] = react.useState(false); // 扫描进行中
|
|
81
78
|
const [loaded, setLoaded] = react.useState(false); // 扫描完成后短暂提示
|
|
82
|
-
const [open, setOpen] = react.useState(false); // 卡片折叠态
|
|
83
79
|
const [openGroups, setOpenGroups] = react.useState({}); // 集合手风琴展开态(key: 集合名)
|
|
84
80
|
const ready = state.status === "ready" && state.writable;
|
|
85
81
|
|
|
@@ -131,81 +127,67 @@ window.__ModuleLoader__.load({
|
|
|
131
127
|
] });
|
|
132
128
|
};
|
|
133
129
|
|
|
134
|
-
/**
|
|
130
|
+
/** 扫描:目录有改动先落盘,再写 scanToken 触发配置变更重应用,服务端重扫 cfg 并把集合写回;目录为空则清空列表 */
|
|
135
131
|
async function scan() {
|
|
136
|
-
if (loading || !ready) return;
|
|
132
|
+
if (loading || !ready || !form) return;
|
|
137
133
|
setLoading(true);
|
|
138
134
|
try {
|
|
139
|
-
if (dirDirty) await
|
|
140
|
-
await
|
|
135
|
+
if (dirDirty) await form.set("apiDir", shownDir.trim());
|
|
136
|
+
await form.set("scanToken", String(Date.now()));
|
|
141
137
|
setDirDraft(null);
|
|
142
138
|
setLoaded(true);
|
|
143
139
|
setTimeout(() => setLoaded(false), 2000);
|
|
140
|
+
} catch {
|
|
141
|
+
// 写入被拒(如 revision 冲突恢复失败)时静默结束,避免 unhandled rejection
|
|
144
142
|
} finally {
|
|
145
143
|
setLoading(false);
|
|
146
144
|
}
|
|
147
145
|
}
|
|
148
146
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
jsxs("
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
onBlur: () => {
|
|
170
|
-
if (dirDirty && ready) scope.set("apiDir", dirDraft);
|
|
171
|
-
setDirDraft(null);
|
|
172
|
-
},
|
|
173
|
-
onKeyDown: (e) => e.key === "Enter" && e.currentTarget.blur(),
|
|
174
|
-
placeholder: "集合父目录(扫描各子文件夹下的 *.cfg,以其中 host 为集合名),留空扫描即清空列表",
|
|
175
|
-
className: "apis-input",
|
|
176
|
-
}),
|
|
177
|
-
jsx("button", {
|
|
178
|
-
type: "button", disabled: !ready || loading, onClick: scan,
|
|
179
|
-
className: "apis-btn", children: loading ? "扫描中…" : "扫描",
|
|
180
|
-
}),
|
|
181
|
-
loaded && jsx("span", { className: "apis-count", children: "已扫描" }),
|
|
182
|
-
] }),
|
|
183
|
-
!state.writable && jsx("span", { className: "apis-desc", children: "只读" }),
|
|
184
|
-
groups.length
|
|
185
|
-
? groups.map(group)
|
|
186
|
-
: list.length
|
|
187
|
-
? list.map(row)
|
|
188
|
-
: jsx("span", { className: "apis-empty", children: "暂无接口:请在 API 目录下的各集合 cfg 文件中配置 host、baseUrl 与 apis,然后点击「扫描」" }),
|
|
147
|
+
// 设置页主体:API配置目录行 + 集合手风琴(无集合时回退平铺列表)
|
|
148
|
+
return jsxs("div", { className: "apis-section", children: [
|
|
149
|
+
jsxs("div", { className: "apis-row", children: [
|
|
150
|
+
jsx("div", { className: "apis-label", children: "API配置" }),
|
|
151
|
+
jsx("input", {
|
|
152
|
+
value: shownDir, disabled: !ready || loading,
|
|
153
|
+
onChange: (e) => setDirDraft(e.target.value),
|
|
154
|
+
onBlur: () => {
|
|
155
|
+
if (dirDirty && ready && form) form.set("apiDir", dirDraft).catch(() => {});
|
|
156
|
+
setDirDraft(null);
|
|
157
|
+
},
|
|
158
|
+
onKeyDown: (e) => e.key === "Enter" && e.currentTarget.blur(),
|
|
159
|
+
placeholder: "集合父目录(扫描各子文件夹下的 *.cfg,以其中 host 为集合名),留空扫描即清空列表",
|
|
160
|
+
className: "apis-input",
|
|
161
|
+
}),
|
|
162
|
+
jsx("button", {
|
|
163
|
+
type: "button", disabled: !ready || loading, onClick: scan,
|
|
164
|
+
className: "apis-btn", children: loading ? "扫描中…" : "扫描",
|
|
165
|
+
}),
|
|
166
|
+
loaded && jsx("span", { className: "apis-count", children: "已扫描" }),
|
|
189
167
|
] }),
|
|
168
|
+
!state.writable && jsx("span", { className: "apis-desc", children: "只读" }),
|
|
169
|
+
groups.length
|
|
170
|
+
? groups.map(group)
|
|
171
|
+
: list.length
|
|
172
|
+
? list.map(row)
|
|
173
|
+
: jsx("span", { className: "apis-empty", children: "暂无接口:请在 API 目录下的各集合 cfg 文件中配置 host、baseUrl 与 apis,然后点击「扫描」" }),
|
|
190
174
|
] });
|
|
191
175
|
}
|
|
192
176
|
|
|
193
|
-
/**
|
|
194
|
-
const inject = ["slots", "
|
|
177
|
+
/** 需要的浏览器侧服务:configForms 由 ui-settings 提供,封装 Host settings 的读快照与写字段 */
|
|
178
|
+
const inject = ["slots", "configForms"];
|
|
195
179
|
|
|
196
|
-
/**
|
|
197
|
-
* 以命名空间为 key 注册到 settings.plugin.item 槽位:
|
|
198
|
-
* 「插件配置」标签页会为每个被服务且被认领的命名空间渲染一张卡片
|
|
199
|
-
*/
|
|
180
|
+
/** 注册设置弹窗的「接口管理」菜单项:settings.section 列表槽位,菜单取 {id, order, label},选中后渲染组件 */
|
|
200
181
|
function apply(ctx) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
ctx.effect(() =>
|
|
204
|
-
ctx.effect(() => ctx.slots.inject("settings.
|
|
205
|
-
name: "settings.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
182
|
+
form = ctx.configForms.get(NS);
|
|
183
|
+
store = createStore(form.getSnapshot());
|
|
184
|
+
ctx.effect(() => form.subscribe(() => store.set(form.getSnapshot())));
|
|
185
|
+
ctx.effect(() => ctx.slots.inject("settings.section", () => ctx.slots.register({
|
|
186
|
+
name: "settings.section",
|
|
187
|
+
id: "dsh-apis-plugin",
|
|
188
|
+
order: 30,
|
|
189
|
+
label: "接口管理",
|
|
190
|
+
}, ApisSection)));
|
|
209
191
|
}
|
|
210
192
|
|
|
211
193
|
return { apply, inject };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-apis-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "通用接口管理插件:把任意一套 HTTP 接口注册成 agent 可用的文档查询与请求双工具",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,6 +18,9 @@
|
|
|
18
18
|
"tools"
|
|
19
19
|
],
|
|
20
20
|
"main": "index.js",
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@deepseek-ai/dsh": ">=0.1.7-rc.2"
|
|
23
|
+
},
|
|
21
24
|
"exports": {
|
|
22
25
|
".": "./index.js",
|
|
23
26
|
"./client": "./client.js"
|
|
@@ -37,6 +40,6 @@
|
|
|
37
40
|
}
|
|
38
41
|
},
|
|
39
42
|
"dependencies": {
|
|
40
|
-
"@deepseek-ai/schemastery": "^3.18.
|
|
43
|
+
"@deepseek-ai/schemastery": "^3.18.4"
|
|
41
44
|
}
|
|
42
45
|
}
|