dsh-skill-harbor 0.5.3 → 0.5.4

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,31 @@
1
+ /**
2
+ * 槽位挂载件。
3
+ *
4
+ * 宿主槽位的 render 必须返回 ReactNode(`packages/client/ui-slots/src/index.ts:16`
5
+ * 直接 import type { ReactNode })——裸 DOM 节点不能当返回值。所以这里套一层
6
+ * 极薄组件:React 负责生命周期,panel 的裸 DOM 塞进 ref 容器。
7
+ */
8
+ import { React } from './react.js'
9
+ import { createPanel } from './panel.js'
10
+ import { detectLang } from './lang.js'
11
+
12
+ export function HarborPanel() {
13
+ const host = React.useRef(null)
14
+
15
+ React.useEffect(() => {
16
+ const node = host.current
17
+ if (node === null || node === undefined) return undefined
18
+ // panel 自带 destroy():清定时器 / 摘事件监听 / 保留 style 标签
19
+ const panel = createPanel({ lang: detectLang() })
20
+ node.append(panel.node)
21
+ return () => {
22
+ try {
23
+ panel.destroy()
24
+ } finally {
25
+ node.replaceChildren()
26
+ }
27
+ }
28
+ }, [])
29
+
30
+ return React.createElement('div', { className: 'dshh-mount' })
31
+ }
@@ -7,7 +7,11 @@
7
7
  import { api, dict } from './api.js'
8
8
  import { FILE_STATE_LABEL } from './labels.js'
9
9
 
10
- const CSS_ID = 'dsh-skill-harbor-style'
10
+ // 分号必须有:下一行以 (function () { 开头,没有分号时 ASI 不会断句,
11
+ // 解析成 'dsh-skill-harbor-style'(function(){...})() —— 直接
12
+ // TypeError: "dsh-skill-harbor-style" is not a function(0.5.3 实测:
13
+ // 本文件在浏览器里从来没能跑起来,这个分号是根因之一)。
14
+ const CSS_ID = 'dsh-skill-harbor-style';
11
15
 
12
16
  /* __STAR_INLINE_BEGIN__ — 手写内联 star 面板(v1→v2→v3→v4 阶梯,_tools/star-row 计划)。
13
17
  * 本仓浏览器半边是手写 JS/无 TS 构建,插不进 star-row-copy.ts,故内联等价实现
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 宿主共享模块表里的 React 单点。
3
+ *
4
+ * 每个内联模块都直接 `require('react')` 会产生多个同名 `const React`,
5
+ * 内联后同处一个 IIFE 作用域 → 重复声明 SyntaxError。所以只在这里取一次,
6
+ * 其余模块从本模块 import。
7
+ *
8
+ * 不得写进 `dsh.client.external`:`react` 是宿主共享模块表的静态项,
9
+ * 声明成 external 会被判「自答自身依赖」而抛错(system.ts:563-575)。
10
+ */
11
+ import React from 'react'
12
+
13
+ export { React }
package/src/client.js CHANGED
@@ -3,26 +3,50 @@
3
3
  *
4
4
  * 注册 id **必须**等于 npm 包名,否则 DSH 静默忽略整个客户端半。
5
5
  * 注册必须发生在 apply 期顶层,关在任何 inject 回调里 = 失活。
6
+ *
7
+ * 本文件是 ESM 源:测试可以直接 import,宿主**不能**直接跑。
8
+ * 宿主把一批插件的源码拼进同一个 classic `<script>`,顶层 import/export 会
9
+ * 直接 SyntaxError。因此发布产物由 `scripts/build-client.mjs` 把本文件及其
10
+ * 相对依赖拓扑内联进一个 IIFE,产出 `lib/client.js`。改这里之后必须
11
+ * `npm run build`,别改 lib/。
6
12
  */
7
- import { createPanel } from './panel.js'
8
- import { LOCALES, LOCALE_IDS } from './locales.js'
13
+ import { React } from './client/react.js'
14
+ import { createPanel } from './client/panel.js'
15
+ import { LOCALES, LOCALE_IDS } from './client/locales.js'
16
+ import { HarborPanel } from './client/mount.js'
9
17
 
10
18
  const id = 'dsh-skill-harbor'
11
19
 
20
+ const inject = ['slots']
21
+
22
+ /**
23
+ * 把面板挂到插件详情页的配置区。
24
+ *
25
+ * `plugins.bundle.config` 是 keyed 槽,key = **包名**:
26
+ * `PluginManagerPage.tsx` 用 `pkg.name` 派发,写别的 key 永远不渲染。
27
+ * 该区只在宿主 `configForms.describe()` 认到这个 entry 时出现 —— 前提是
28
+ * host 半导出了 schemastery `Config`(见 src/index.js)。
29
+ */
30
+ function apply(ctx) {
31
+ const slots = ctx?.slots ?? ctx?.get?.('slots')
32
+ if (slots === undefined || slots === null) return
33
+
34
+ slots.inject('plugins.bundle.config', () => slots.register(
35
+ { name: 'plugins.bundle.config', key: id },
36
+ () => React.createElement(HarborPanel),
37
+ ))
38
+ }
39
+
12
40
  function factory(_require) {
13
41
  return {
14
42
  id,
43
+ inject,
44
+ apply,
15
45
  locales: LOCALES,
16
46
  localeIds: LOCALE_IDS,
17
- /** 宿主调用此方法把面板挂到槽位。 */
18
- createPanel(options = {}) {
19
- return createPanel(options)
20
- },
47
+ /** 对外 helper:宿主之外(回归测试 / 手工嵌入)仍可直接拿裸面板。 */
48
+ createPanel,
21
49
  }
22
50
  }
23
51
 
24
- if (typeof window !== 'undefined' && window.__ModuleLoader__?.load) {
25
- window.__ModuleLoader__.load({ id, factory })
26
- }
27
-
28
- export { id, factory, createPanel }
52
+ export { id, inject, apply, factory, createPanel }
@@ -1,169 +1,169 @@
1
- /**
2
- * 零依赖 frontmatter 解析。
3
- *
4
- * 不引 js-yaml:技能 frontmatter 的实际形态极窄(一层标量 + 少量布尔 + 两种块标量),
5
- * 而 yaml 全量解析器会带进 ~100KB 与大量边界语义(锚点/多文档/类型推断),
6
- * 我们只需要「读到正确字符串」。
7
- *
8
- * 支持:
9
- * - `key: value` 标量(去引号)
10
- * - `key: |` 字面块 / `key: >` 折叠块(含缩进剥离)
11
- * - `disable-model-invocation` / `user-invocable` 布尔:
12
- * true/false、1/0、yes/no、on/off(大小写不敏感)
13
- */
14
-
15
- /** 六种布尔写法的唯一收敛点。 */
16
- const BOOL_TRUE = new Set(['true', '1', 'yes', 'on'])
17
- const BOOL_FALSE = new Set(['false', '0', 'no', 'off'])
18
-
19
- /** 解析布尔;不是布尔写法返回 undefined(区别于 false)。 */
20
- export function parseBool(raw) {
21
- if (typeof raw !== 'string') return undefined
22
- const v = raw.trim().toLowerCase()
23
- if (BOOL_TRUE.has(v)) return true
24
- if (BOOL_FALSE.has(v)) return false
25
- return undefined
26
- }
27
-
28
- /** 去掉成对引号(单双),并还原常见转义。 */
29
- function unquote(raw) {
30
- const v = raw.trim()
31
- if (v.length >= 2) {
32
- const first = v[0]
33
- const last = v[v.length - 1]
34
- if ((first === '"' || first === "'") && first === last) {
35
- const inner = v.slice(1, -1)
36
- return first === '"' ? inner.replace(/\\"/g, '"').replace(/\\n/g, '\n') : inner.replace(/''/g, "'")
37
- }
38
- }
39
- return v
40
- }
41
-
42
- /**
43
- * 从 Markdown 文本中切出 frontmatter 原文与正文。
44
- * 只认文件最开头的 `---`(允许前置 BOM),与宿主一致。
45
- */
46
- export function splitFrontmatter(text) {
47
- const src = text.charCodeAt(0) === 0xfeff ? text.slice(1) : text
48
- if (!src.startsWith('---')) return { raw: null, body: src }
49
-
50
- // 找结束分隔行:独占一行的 --- 或 ...
51
- const lines = src.split(/\r?\n/)
52
- let end = -1
53
- for (let i = 1; i < lines.length; i += 1) {
54
- const line = lines[i]
55
- if (line === '---' || line === '...') {
56
- end = i
57
- break
58
- }
59
- }
60
- if (end === -1) return { raw: null, body: src }
61
-
62
- return {
63
- raw: lines.slice(1, end).join('\n'),
64
- body: lines.slice(end + 1).join('\n'),
65
- }
66
- }
67
-
68
- /**
69
- * 解析 frontmatter 原文为对象。
70
- * 值一律是 string 或 string[](块标量),布尔交给调用方用 parseBool 收敛,
71
- * 避免「看起来是字符串的 false」被当成真值。
72
- */
73
- export function parseFrontmatter(raw) {
74
- const out = Object.create(null)
75
- if (typeof raw !== 'string' || raw.trim() === '') return out
76
-
77
- const lines = raw.split(/\r?\n/)
78
- let i = 0
79
- while (i < lines.length) {
80
- const line = lines[i]
81
- if (line.trim() === '' || line.trimStart().startsWith('#')) {
82
- i += 1
83
- continue
84
- }
85
-
86
- const match = /^([A-Za-z0-9_.-]+):(.*)$/.exec(line)
87
- if (match === null) {
88
- i += 1
89
- continue
90
- }
91
-
92
- const key = match[1]
93
- const rest = match[2]
94
-
95
- // 块标量:`key: |` 或 `key: >`(可带修饰符)
96
- const blockMark = /^\s*([|>])[+-]?\s*$/.exec(rest)
97
- if (blockMark !== null) {
98
- const style = blockMark[1]
99
- const body = []
100
- i += 1
101
- while (i < lines.length) {
102
- const next = lines[i]
103
- if (next.trim() !== '' && !/^\s/.test(next)) break
104
- body.push(next)
105
- i += 1
106
- }
107
- // 去掉公共缩进
108
- const indents = body.filter(l => l.trim() !== '').map(l => l.match(/^\s*/)[0].length)
109
- const cut = indents.length > 0 ? Math.min(...indents) : 0
110
- const dedented = body.map(l => l.slice(cut))
111
- // 去掉尾部空行
112
- while (dedented.length > 0 && dedented[dedented.length - 1].trim() === '') dedented.pop()
113
- out[key] = style === '|' ? dedented.join('\n') : dedented.join(' ').replace(/\s+/g, ' ').trim()
114
- continue
115
- }
116
-
117
- // 普通标量
118
- const value = rest.trim()
119
- if (value !== '') {
120
- out[key] = unquote(value)
121
- i += 1
122
- continue
123
- }
124
-
125
- // 空值 → 可能是紧随其后的 `- item` 列表
126
- const items = []
127
- i += 1
128
- while (i < lines.length) {
129
- const next = lines[i]
130
- const item = /^\s*-\s+(.*)$/.exec(next)
131
- if (item === null) break
132
- items.push(unquote(item[1]))
133
- i += 1
134
- }
135
- out[key] = items.length > 0 ? items : ''
136
- }
137
-
138
- return out
139
- }
140
-
141
- /** 一站式:文本 → { data, body }。 */
142
- export function readSkillDocument(text) {
143
- const { raw, body } = splitFrontmatter(text)
144
- return { data: parseFrontmatter(raw), body, hasFrontmatter: raw !== null }
145
- }
146
-
147
- /**
148
- * 归一化成 SkillObservation 需要的三字段。
149
- * `whenToUse` 兼容 whenToUse / when_to_use 两种写法。
150
- */
151
- export function extractSkillMeta(text) {
152
- const { data, body } = readSkillDocument(text)
153
- const name = typeof data.name === 'string' ? data.name : ''
154
- const description = typeof data.description === 'string' ? data.description : ''
155
- const whenToUseRaw = data.whenToUse ?? data.when_to_use
156
- const whenToUse = typeof whenToUseRaw === 'string' && whenToUseRaw !== '' ? whenToUseRaw : undefined
157
-
158
- const disableRaw = data['disable-model-invocation']
159
- const userRaw = data['user-invocable']
160
-
161
- return {
162
- name,
163
- description,
164
- ...whenToUse === undefined ? {} : { whenToUse },
165
- disableModelInvocation: parseBool(disableRaw) ?? false,
166
- userInvocable: parseBool(userRaw) ?? true,
167
- body,
168
- }
169
- }
1
+ /**
2
+ * 零依赖 frontmatter 解析。
3
+ *
4
+ * 不引 js-yaml:技能 frontmatter 的实际形态极窄(一层标量 + 少量布尔 + 两种块标量),
5
+ * 而 yaml 全量解析器会带进 ~100KB 与大量边界语义(锚点/多文档/类型推断),
6
+ * 我们只需要「读到正确字符串」。
7
+ *
8
+ * 支持:
9
+ * - `key: value` 标量(去引号)
10
+ * - `key: |` 字面块 / `key: >` 折叠块(含缩进剥离)
11
+ * - `disable-model-invocation` / `user-invocable` 布尔:
12
+ * true/false、1/0、yes/no、on/off(大小写不敏感)
13
+ */
14
+
15
+ /** 六种布尔写法的唯一收敛点。 */
16
+ const BOOL_TRUE = new Set(['true', '1', 'yes', 'on'])
17
+ const BOOL_FALSE = new Set(['false', '0', 'no', 'off'])
18
+
19
+ /** 解析布尔;不是布尔写法返回 undefined(区别于 false)。 */
20
+ export function parseBool(raw) {
21
+ if (typeof raw !== 'string') return undefined
22
+ const v = raw.trim().toLowerCase()
23
+ if (BOOL_TRUE.has(v)) return true
24
+ if (BOOL_FALSE.has(v)) return false
25
+ return undefined
26
+ }
27
+
28
+ /** 去掉成对引号(单双),并还原常见转义。 */
29
+ function unquote(raw) {
30
+ const v = raw.trim()
31
+ if (v.length >= 2) {
32
+ const first = v[0]
33
+ const last = v[v.length - 1]
34
+ if ((first === '"' || first === "'") && first === last) {
35
+ const inner = v.slice(1, -1)
36
+ return first === '"' ? inner.replace(/\\"/g, '"').replace(/\\n/g, '\n') : inner.replace(/''/g, "'")
37
+ }
38
+ }
39
+ return v
40
+ }
41
+
42
+ /**
43
+ * 从 Markdown 文本中切出 frontmatter 原文与正文。
44
+ * 只认文件最开头的 `---`(允许前置 BOM),与宿主一致。
45
+ */
46
+ export function splitFrontmatter(text) {
47
+ const src = text.charCodeAt(0) === 0xfeff ? text.slice(1) : text
48
+ if (!src.startsWith('---')) return { raw: null, body: src }
49
+
50
+ // 找结束分隔行:独占一行的 --- 或 ...
51
+ const lines = src.split(/\r?\n/)
52
+ let end = -1
53
+ for (let i = 1; i < lines.length; i += 1) {
54
+ const line = lines[i]
55
+ if (line === '---' || line === '...') {
56
+ end = i
57
+ break
58
+ }
59
+ }
60
+ if (end === -1) return { raw: null, body: src }
61
+
62
+ return {
63
+ raw: lines.slice(1, end).join('\n'),
64
+ body: lines.slice(end + 1).join('\n'),
65
+ }
66
+ }
67
+
68
+ /**
69
+ * 解析 frontmatter 原文为对象。
70
+ * 值一律是 string 或 string[](块标量),布尔交给调用方用 parseBool 收敛,
71
+ * 避免「看起来是字符串的 false」被当成真值。
72
+ */
73
+ export function parseFrontmatter(raw) {
74
+ const out = Object.create(null)
75
+ if (typeof raw !== 'string' || raw.trim() === '') return out
76
+
77
+ const lines = raw.split(/\r?\n/)
78
+ let i = 0
79
+ while (i < lines.length) {
80
+ const line = lines[i]
81
+ if (line.trim() === '' || line.trimStart().startsWith('#')) {
82
+ i += 1
83
+ continue
84
+ }
85
+
86
+ const match = /^([A-Za-z0-9_.-]+):(.*)$/.exec(line)
87
+ if (match === null) {
88
+ i += 1
89
+ continue
90
+ }
91
+
92
+ const key = match[1]
93
+ const rest = match[2]
94
+
95
+ // 块标量:`key: |` 或 `key: >`(可带修饰符)
96
+ const blockMark = /^\s*([|>])[+-]?\s*$/.exec(rest)
97
+ if (blockMark !== null) {
98
+ const style = blockMark[1]
99
+ const body = []
100
+ i += 1
101
+ while (i < lines.length) {
102
+ const next = lines[i]
103
+ if (next.trim() !== '' && !/^\s/.test(next)) break
104
+ body.push(next)
105
+ i += 1
106
+ }
107
+ // 去掉公共缩进
108
+ const indents = body.filter(l => l.trim() !== '').map(l => l.match(/^\s*/)[0].length)
109
+ const cut = indents.length > 0 ? Math.min(...indents) : 0
110
+ const dedented = body.map(l => l.slice(cut))
111
+ // 去掉尾部空行
112
+ while (dedented.length > 0 && dedented[dedented.length - 1].trim() === '') dedented.pop()
113
+ out[key] = style === '|' ? dedented.join('\n') : dedented.join(' ').replace(/\s+/g, ' ').trim()
114
+ continue
115
+ }
116
+
117
+ // 普通标量
118
+ const value = rest.trim()
119
+ if (value !== '') {
120
+ out[key] = unquote(value)
121
+ i += 1
122
+ continue
123
+ }
124
+
125
+ // 空值 → 可能是紧随其后的 `- item` 列表
126
+ const items = []
127
+ i += 1
128
+ while (i < lines.length) {
129
+ const next = lines[i]
130
+ const item = /^\s*-\s+(.*)$/.exec(next)
131
+ if (item === null) break
132
+ items.push(unquote(item[1]))
133
+ i += 1
134
+ }
135
+ out[key] = items.length > 0 ? items : ''
136
+ }
137
+
138
+ return out
139
+ }
140
+
141
+ /** 一站式:文本 → { data, body }。 */
142
+ export function readSkillDocument(text) {
143
+ const { raw, body } = splitFrontmatter(text)
144
+ return { data: parseFrontmatter(raw), body, hasFrontmatter: raw !== null }
145
+ }
146
+
147
+ /**
148
+ * 归一化成 SkillObservation 需要的三字段。
149
+ * `whenToUse` 兼容 whenToUse / when_to_use 两种写法。
150
+ */
151
+ export function extractSkillMeta(text) {
152
+ const { data, body } = readSkillDocument(text)
153
+ const name = typeof data.name === 'string' ? data.name : ''
154
+ const description = typeof data.description === 'string' ? data.description : ''
155
+ const whenToUseRaw = data.whenToUse ?? data.when_to_use
156
+ const whenToUse = typeof whenToUseRaw === 'string' && whenToUseRaw !== '' ? whenToUseRaw : undefined
157
+
158
+ const disableRaw = data['disable-model-invocation']
159
+ const userRaw = data['user-invocable']
160
+
161
+ return {
162
+ name,
163
+ description,
164
+ ...whenToUse === undefined ? {} : { whenToUse },
165
+ disableModelInvocation: parseBool(disableRaw) ?? false,
166
+ userInvocable: parseBool(userRaw) ?? true,
167
+ body,
168
+ }
169
+ }