dsh-apis-plugin 0.1.2 → 0.1.3

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.
Files changed (2) hide show
  1. package/api-tools.js +15 -9
  2. package/package.json +1 -1
package/api-tools.js CHANGED
@@ -14,7 +14,9 @@ function loadApiCfg(dir) {
14
14
  try {
15
15
  const cfg = { apis: [] }
16
16
  let current = null
17
- for (const raw of readFileSync(join(dir, 'api.cfg'), 'utf8').split(/\r?\n/)) {
17
+ // 去掉 UTF-8 BOM(Windows 记事本默认会加),否则首个 key 解析失败
18
+ const text = readFileSync(join(dir, 'api.cfg'), 'utf8').replace(/^\uFEFF/, '')
19
+ for (const raw of text.split(/\r?\n/)) {
18
20
  const line = raw.trim()
19
21
  if (!line || line.startsWith('#')) continue
20
22
  const m = line.match(/^([\w-]+)\s*=\s*(.*)$/)
@@ -36,7 +38,7 @@ function loadApiCfg(dir) {
36
38
  function loadApiDoc(dir) {
37
39
  if (!dir) return ''
38
40
  try {
39
- return readFileSync(join(dir, 'api_doc.md'), 'utf8')
41
+ return readFileSync(join(dir, 'api_doc.md'), 'utf8').replace(/^\uFEFF/, '')
40
42
  } catch {
41
43
  return ''
42
44
  }
@@ -104,12 +106,15 @@ export function defineApiPlugin(options) {
104
106
  // config 覆盖优先,回退工厂默认值(兼容旧用户层保存值缺字段的情况)
105
107
  const prefix = config.toolPrefix || toolPrefix
106
108
  const domainName = config.domain || domain
107
- // yml 侧固定默认接口(扫描 yml 目录 + 工厂默认 + yml 配置),「加载API」重扫时始终保留这部分
109
+ // 部署侧静态接口(yml config.endpoints + 工厂默认),与 api.cfg 动态读取分离,
110
+ // 避免「加载API」重扫时把启动时的旧 api.cfg 列表拼回去(导致删掉的接口仍可请求)
108
111
  const ymlApiDir = config.apiDir || apiDir
109
- const ymlEndpoints = [...new Set([...loadApiCfg(ymlApiDir).apis, ...defaultEndpoints, ...(config.endpoints ?? [])])]
112
+ const staticEndpoints = [...new Set([...defaultEndpoints, ...(config.endpoints ?? [])])]
113
+ // 启动时以当前目录 api.cfg 为权威范围,∪ 静态接口作为 base
114
+ const bootEndpoints = [...new Set([...loadApiCfg(ymlApiDir).apis, ...staticEndpoints])]
110
115
 
111
116
  // 以组合配置为 base 层注册 settings 命名空间,卸载插件时注册随 fiber 一并清理
112
- const scope = ctx.settings.register(ns, Config, { base: { ...config, endpoints: ymlEndpoints } })
117
+ const scope = ctx.settings.register(ns, Config, { base: { ...config, endpoints: bootEndpoints } })
113
118
 
114
119
  // 当前生效目录:用户层保存值优先(重启后仍生效),回退部署侧配置
115
120
  let docDir = scope.get()?.apiDir || ymlApiDir
@@ -133,8 +138,9 @@ export function defineApiPlugin(options) {
133
138
  }
134
139
  if (next.scanToken !== lastScanToken) {
135
140
  lastScanToken = next.scanToken
136
- // 扫描结果yml 默认接口,整体接管用户层 endpoints(去重;值未变时不提交,避免循环)
137
- const merged = [...new Set([...loadApiCfg(dir).apis, ...ymlEndpoints])]
141
+ // 以最新 api.cfg 为权威范围 静态接口,整体接管用户层 endpoints(去重;值未变时不提交,避免循环)
142
+ // 用最新读取结果而非启动时的快照,这样从 api.cfg 删掉的接口重扫后即失效
143
+ const merged = [...new Set([...loadApiCfg(dir).apis, ...staticEndpoints])]
138
144
  const cur = Array.isArray(next.endpoints) ? next.endpoints : []
139
145
  if (merged.join('\n') !== cur.join('\n')) scope.update({ endpoints: merged })
140
146
  }
@@ -155,7 +161,7 @@ export function defineApiPlugin(options) {
155
161
  const value = scope.get()
156
162
  if (Array.isArray(value?.endpoints) && value.endpoints.length) return value.endpoints
157
163
  } catch { /* scope.get 不可用时回退 */ }
158
- return ymlEndpoints
164
+ return bootEndpoints
159
165
  }
160
166
 
161
167
  // 工具 1:查接口文档,让模型了解每个接口的参数与响应(prompt 按领域名拼接)
@@ -224,7 +230,7 @@ export function defineApiPlugin(options) {
224
230
  },
225
231
  })
226
232
 
227
- console.log(`[${ns}] loaded; endpoints=${ymlEndpoints.length}, doc sections=${docSections.length}`)
233
+ console.log(`[${ns}] loaded; endpoints=${bootEndpoints.length}, doc sections=${docSections.length}`)
228
234
  }
229
235
 
230
236
  return { Config, inject, apply }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-apis-plugin",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "通用接口管理插件:把任意一套 HTTP 接口注册成 agent 可用的文档查询与请求双工具",
5
5
  "type": "module",
6
6
  "license": "MIT",