@mochi-fish/fish-bridge 0.0.0-stage → 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/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ // @mochi-fish/fish-bridge 的入口:现在整份插件是 dist/fish-bridge.mjs(由 src/parts 按 src/manifest.json 拼成,见 scripts/build.mjs)。
2
+ // 以后把纯函数一块块拆成真模块时,在这里接回去;包的 exports 只有 .、./client、./package.json,别依赖内部路径。
3
+ export * from './dist/fish-bridge.mjs'
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@mochi-fish/fish-bridge",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "DSH(DeepSeek Harness)插件:让 dsh-fish 养的那一只在 QQ、群、桌面之间只有一条会话,管谁的话怎么送进来、她说的话怎么发出去、哪一轮能用哪些工具。主人、QQ 号、功能开关都从身份配置(@mochi-fish/config schema 2)读;没配置时没有主人、功能开关全关、工具的门按 enforce。",
5
+ "keywords": [
6
+ "dsh",
7
+ "deepseek-harness",
8
+ "dsh-fish",
9
+ "onebot",
10
+ "companion-ai",
11
+ "mochi-fish"
12
+ ],
13
+ "homepage": "https://github.com/mochi-fish/fish-sessionless/tree/main/packages/fish-bridge#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/mochi-fish/fish-sessionless/issues"
16
+ },
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/mochi-fish/fish-sessionless.git",
21
+ "directory": "packages/fish-bridge"
22
+ },
23
+ "type": "module",
24
+ "exports": {
25
+ ".": "./index.mjs",
26
+ "./client": "./ui/client.js",
27
+ "./package.json": "./package.json"
28
+ },
29
+ "files": [
30
+ "index.mjs",
31
+ "dist/",
32
+ "ui/",
33
+ "README.md",
34
+ "README_en.md",
35
+ "LICENSE",
36
+ "THIRD-PARTY-NOTICES"
37
+ ],
38
+ "engines": {
39
+ "node": ">=22",
40
+ "dsh": ">=0.2.2-rc.5 <0.3.0-0"
41
+ },
42
+ "scripts": {
43
+ "build": "node scripts/build.mjs src/manifest.json --out dist/fish-bridge.mjs --force --verify --node-check",
44
+ "verify": "node scripts/build.mjs src/manifest.json --verify",
45
+ "prepack": "node scripts/build.mjs src/manifest.json --out dist/fish-bridge.mjs --force --verify --node-check",
46
+ "test": "node test/run.mjs"
47
+ },
48
+ "dependencies": {
49
+ "@mochi-fish/config": "0.2.0",
50
+ "@mochi-fish/audience": "1.4.4",
51
+ "@mochi-fish/reldate": "0.3.0",
52
+ "@mochi-fish/ledger-core": "0.2.0"
53
+ },
54
+ "dsh": {
55
+ "client": {
56
+ "platform": "web",
57
+ "inject": []
58
+ }
59
+ },
60
+ "publishConfig": {
61
+ "access": "public"
62
+ }
63
+ }
package/ui/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # 审批面板(`ui/client.js`)
2
+
3
+ 教学模式(桥第 35 条)开着时,大肥鱼每次 `im_send` 都先进一个队列,主人在她会话顶上的「审批」标签里决定:放行(照她写的发)、写一句建议送给她(她自己选怎么发)、不发了。这个标签就是 `client.js`。
4
+
5
+ ## 怎么挂上
6
+
7
+ 面板和桥在同一个包里,不用另装:
8
+
9
+ - `package.json` 里有 `"dsh": { "client": { "platform": "web", "inject": [] } }` 和 `"exports": { "./client": "./ui/client.js" }`,DSH 的网页端照这个载入它。
10
+ - 包在 profile 的 `cordis.patch.yml` 里插的那一行(`name: '@mochi-fish/fish-bridge'`,见包的 README)同时管桥和面板:同一个带 `dsh.client` 的包只能被一行 insert 引用,所以不分两个包。
11
+ - `client.js` 不是 ESM,是自注册脚本:`window.__ModuleLoader__.load({ id: '@mochi-fish/fish-bridge', factory })`。**id 必须和 `package.json` 的 `name` 一样**,改包名要一起改。
12
+
13
+ 要它真的出现,还要两样:
14
+
15
+ 1. 身份配置(`@mochi-fish/config` 0.2.0 的 schema 2)里 `bridge.features.teach` 是 `true`,而且整份桥那边认(`bridgeView` 是 ok:主人的 qq 号正好一个、叫法合规矩)。关着或者不认时桥不挂接口,面板问不到桥,标签不出现。
16
+ 2. 在面板里把「教学模式」打开(开关存在桥的状态目录里,`local-bridge.teach.json`)。功能开关决定「能不能开」,面板上的开关决定「现在开没开」。
17
+
18
+ ## 标签什么时候出现
19
+
20
+ `conversation.view` 这个槽没有按会话隐藏的开关,所以面板往 `conversation.input.left` 塞一个看不见的探针,拿当前会话的 id 问桥 `GET /api/fish-bridge/teach/state?session=<id>`:
21
+
22
+ | 桥怎么答 | 标签 |
23
+ | --- | --- |
24
+ | `{ ok: true, mine: true }`(或者没说 `mine: false`) | 挂上 |
25
+ | `{ ok: true, mine: false }`:不是她的会话 | 不挂 |
26
+ | 404:桥没挂这个接口(功能关着、桥没装上) | 不挂 |
27
+ | 200 但不是桥的 JSON(宿主把没挂的路径交给了页面) | 不挂 |
28
+ | 401 / 403 / 503 / 连不上 | 挂上,标签里写是哪一种错,让主人看到为什么 |
29
+
30
+ 问过的会话 10 分钟内不再问;挂着的标签每 30 秒在后台再确认一次,桥明确说「不是她的会话」才撤(免得把开着的标签打回「对话」、清掉没写完的建议)。
31
+
32
+ ## 接口
33
+
34
+ 都在 `/api/fish-bridge/teach` 下面,由桥挂(`webServer.register({ kind: 'prefix', auth: 'required', … })`):
35
+
36
+ | 请求 | 回 |
37
+ | --- | --- |
38
+ | `GET /state[?session=<id>]` | `{ ok, on, now, mine, items, lastError, lastErrorAt, fullName, label }`;不带 `session` 时 `mine` 是 `null` |
39
+ | `POST /mode` `{ on: true \| false }` | `{ ok, on }` |
40
+ | `POST /act` `{ id, action, text? }` | `action` 是 `approve` / `suggest`(要 `text`)/ `drop` / `resend`;成了 200,这一条已经处理过之类 409 |
41
+
42
+ - 鉴权在宿主:DSH 认出来的 peer 的 `principal.kind` 是 `owner` 才放;宿主没交 peer 时桥自己用 `connection.admit` 再认一遍;都拿不到 = 503(失败即关)。断开的 401、不是主人的 403。
43
+ - 桥另外只认本机同源(`Host` 是 `127.0.0.1` / `localhost` / `[::1]`,`Origin` / `Referer` 要对得上),请求体只收 JSON、64 KB 封顶。
44
+ - 面板发请求只带同源 cookie(`credentials: 'same-origin'`)、`content-type: application/json`。
45
+ - 面板上称呼主人用 `/state` 的 `fullName`,没有就用叫法 `label`,都没有写「主人」。桥送给她的建议标的是同一个名字(「X觉得这样表达更好」),两边对得上。`fullName` 来自身份配置的 `bridge.ownerLabel.fullName`(要先写 `owner.displayName`),`label` 是桥现在给她看的叫法(写死的就是 `owner.displayName`)。
46
+
47
+ 条目的状态:`pending` 等你审、`sending` 正在发、`sent` 放行了已发、`dropped` 没让发、`suggested` 建议还没送到她、`asked` 等她选、`picking` 她选了正在发、`done` 她选完了、`unsure` 不知道发出去没有、`expired` 她没选过期了。她选的(`pick`):`owner` 发了主人的那句、`rewrite` 她重写了一句、`mine` 还是发她原来那句、`none` 没发。
48
+
49
+ ## 停用
50
+
51
+ 先在面板里把教学模式关掉,再把身份配置里的 `bridge.features.teach` 改成 `false`、重启 DSH。面板打不开时:停 DSH,把状态目录里 `local-bridge.teach.json` 的 `"on"` 改成 `false`。
52
+
53
+ ## 离线预览
54
+
55
+ `test/ui-harness/`(不进 npm 包):起一份桥的拷贝(假 onebot、假会话、编的身份配置),把接口接到桥自己挂的路由上,页面用假的 `__ModuleLoader__` 和 slots 渲染这个面板。编的身份配置照 `@mochi-fish/config` 0.2.0 写(主人叫「兔」、全名「兔兔」,功能只开 `teach`),起桥前先核一遍,桥那边不认就退 2 并说是哪一处。桥 import 的 `@mochi-fish/*` 从仓库的 `packages/` 找,别处跑设 `FISH_DEPS_DIR`。
56
+
57
+ ```bash
58
+ node test/ui-harness/server.mjs --bridge dist/fish-bridge.mjs # 然后浏览器开它打印的地址
59
+ node --test test/ui-harness/test-ui-harness.mjs # 不开浏览器的测试
60
+ ```
61
+
62
+ 预览页的 React 从 cdnjs 拿,只有在浏览器里打开时才用到。
package/ui/client.js ADDED
@@ -0,0 +1,294 @@
1
+ /**
2
+ * fish-bridge 的审批面板:教学模式(桥第 35 条)开着时,大肥鱼会话顶上的「审批」标签。
3
+ *
4
+ * - 不是 ESM,是自注册脚本:window.__ModuleLoader__.load({ id: <包名>, factory })。id 必须和 package.json 的 name 一样。
5
+ * - 标签挂在 conversation.view 上(和 对话 / 轨迹 / 上下文 这些标签一排)。这个槽没有按会话隐藏的开关,
6
+ * 所以往 conversation.input.left 塞一个看不见的探针,拿 sessionId 问桥是不是她的会话,是才挂上。
7
+ * - 数据都走桥挂的 /api/fish-bridge/teach:GET /state、POST /mode(开关)、POST /act(放行 / 建议 / 不发了 / 再送一次)。
8
+ * 接口要 DSH 的主人身份(auth: 'required'),面板只带同源的 cookie、只发 JSON。标签开着时每 2 秒刷一次;没有推送。
9
+ * - 面板上称呼主人用 /state 回来的 fullName,没有就用叫法 label,都没有写「主人」:和桥送给她的那句「X觉得这样表达更好」对得上。
10
+ */
11
+ window.__ModuleLoader__.load({
12
+ id: '@mochi-fish/fish-bridge',
13
+ factory: (require) => {
14
+ const React = require('react')
15
+ const h = React.createElement
16
+ const { useState, useEffect, useCallback, useRef } = React
17
+
18
+ // 和桥里挂接口的路径是同一个值;改一边要改另一边
19
+ const API = '/api/fish-bridge/teach'
20
+ // 面板上怎么称呼主人:全名 → 叫法 → 「主人」;只认短的单行字,别的一律当没有
21
+ const ownerName = (st) => {
22
+ for (const v of [st && st.fullName, st && st.label]) {
23
+ if (typeof v === 'string' && v.trim() !== '' && v.length <= 32 && !/[\r\n]/.test(v)) return v.trim()
24
+ }
25
+ return '主人'
26
+ }
27
+ const asJson = async (res) => {
28
+ let body = null
29
+ try { body = await res.json() } catch { /* 不是 JSON */ }
30
+ if (res.ok) return body
31
+ const why = body && typeof body.error === 'string' ? body.error
32
+ : res.status === 401 ? '没登录'
33
+ : res.status === 403 ? '宿主认的不是主人的身份'
34
+ : res.status === 503 ? '桥拿不到宿主的鉴权,接口没开' : ''
35
+ const err = new Error('HTTP ' + res.status + (why ? ':' + why : ''))
36
+ err.status = res.status
37
+ throw err
38
+ }
39
+ const jget = async (p) => asJson(await fetch(API + p, { credentials: 'same-origin', cache: 'no-store' }))
40
+ const jpost = async (p, body) => asJson(await fetch(API + p, {
41
+ method: 'POST',
42
+ credentials: 'same-origin',
43
+ headers: { 'content-type': 'application/json' },
44
+ body: JSON.stringify(body || {}),
45
+ }))
46
+
47
+ const pad = (n) => String(n).padStart(2, '0')
48
+ const clock = (at) => { const d = new Date(at); return (d.getMonth() + 1) + '-' + pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) }
49
+ const ago = (now, at) => {
50
+ const s = Math.max(0, Math.round((now - at) / 1000))
51
+ if (s < 60) return s + ' 秒前'
52
+ if (s < 3600) return Math.floor(s / 60) + ' 分钟前'
53
+ if (s < 86400) return Math.floor(s / 3600) + ' 小时前'
54
+ return Math.floor(s / 86400) + ' 天前'
55
+ }
56
+ // 「润:」是她给回复器的记号;你写的那句不交回复器,预填和比较时都去掉它
57
+ const stripRun = (s) => String(s || '').replace(/^\s*润[::]\s*/, '').trim()
58
+ // 没写完的建议按条目存在这里:标签被宿主重挂、换会话再回来都不丢
59
+ const drafts = new Map()
60
+ const STATE = {
61
+ pending: '等你审', sending: '正在发', sent: '放行了,已发', dropped: '没让发', suggested: '建议还没送到她',
62
+ asked: '等她选', picking: '她选了,正在发', done: '她选完了', unsure: '不知道发出去没有', expired: '她没选,过期了',
63
+ }
64
+ const PICK = { owner: '发了你的那句', rewrite: '她重写了一句发的', mine: '还是发了她原来那句', none: '没发' }
65
+
66
+ const CSS = `
67
+ [data-lbt] { height: 100%; overflow: auto; padding: 14px 18px 40px; box-sizing: border-box; font-size: 13px; line-height: 1.6; color: inherit; }
68
+ [data-lbt] * { box-sizing: border-box; }
69
+ [data-lbt-top] { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 4px; }
70
+ [data-lbt-title] { font-size: 15px; font-weight: 600; }
71
+ [data-lbt-hint] { opacity: .65; font-size: 12px; margin: 2px 0 12px; }
72
+ [data-lbt-h] { font-weight: 600; margin: 18px 0 8px; opacity: .85; }
73
+ [data-lbt-card] { border: 1px solid rgba(127,127,127,.35); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; }
74
+ [data-lbt-meta] { display: flex; gap: 8px; flex-wrap: wrap; font-size: 12px; opacity: .7; margin-bottom: 6px; }
75
+ [data-lbt-tag] { border: 1px solid rgba(127,127,127,.45); border-radius: 999px; padding: 0 7px; font-size: 11px; }
76
+ [data-lbt-tag="warn"] { border-color: rgba(220,140,40,.8); }
77
+ [data-lbt-text] { white-space: pre-wrap; word-break: break-word; margin: 4px 0; padding: 6px 8px; border-radius: 6px; background: rgba(127,127,127,.10); }
78
+ [data-lbt-label] { font-size: 12px; opacity: .7; margin-top: 6px; }
79
+ [data-lbt-ctx] { font-size: 12px; opacity: .75; margin: 4px 0; }
80
+ [data-lbt-ctx] div { white-space: pre-wrap; word-break: break-word; }
81
+ [data-lbt] textarea { width: 100%; min-height: 64px; margin-top: 6px; padding: 6px 8px; border-radius: 6px; border: 1px solid rgba(127,127,127,.45);
82
+ background: transparent; color: inherit; font: inherit; resize: vertical; }
83
+ [data-lbt-row] { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
84
+ [data-lbt] button { font: inherit; font-size: 12px; padding: 4px 12px; border-radius: 6px; border: 1px solid rgba(127,127,127,.5); background: transparent; color: inherit; cursor: pointer; }
85
+ [data-lbt] button:disabled { opacity: .4; cursor: default; }
86
+ [data-lbt] button[data-main] { border-color: rgba(60,140,240,.9); }
87
+ [data-lbt-err] { color: rgb(220,80,60); font-size: 12px; margin-top: 6px; white-space: pre-wrap; }
88
+ [data-lbt-empty] { opacity: .6; font-size: 12px; }
89
+ [data-lbt] details summary { cursor: pointer; font-size: 12px; opacity: .7; }
90
+ `
91
+
92
+ const Text = ({ label, text }) => h(React.Fragment, null,
93
+ label ? h('div', { 'data-lbt-label': '' }, label) : null,
94
+ h('div', { 'data-lbt-text': '' }, text))
95
+
96
+ const Meta = ({ it, now }) => h('div', { 'data-lbt-meta': '' },
97
+ h('span', null, it.id),
98
+ h('span', null, it.where + '(' + it.address + ')'),
99
+ h('span', { title: clock(it.at) }, '她写于 ' + ago(now, it.at)),
100
+ h('span', { 'data-lbt-tag': '' }, STATE[it.state] || it.state),
101
+ it.replier ? h('span', { 'data-lbt-tag': '', title: '她在开头写了「润:」,放行后照样交给回复器顺一句' }, '润') : null,
102
+ it.others ? h('span', { 'data-lbt-tag': 'warn', title: '引起她这一轮的有别人的话(群友、游戏里的人、别的 AI…)' }, '别人的话引起的') : null)
103
+
104
+ const Context = ({ it }) => (it.context && it.context.length > 0)
105
+ ? h('details', null, h('summary', null, '她写之前群里的 ' + it.context.length + ' 句'),
106
+ h('div', { 'data-lbt-ctx': '' }, it.context.map((l, i) => h('div', { key: i }, clock(l.at).slice(-5) + ' ' + l.who + ':' + l.text))))
107
+ : null
108
+
109
+ function Pending({ it, now, reload, owner }) {
110
+ const [draft, setDraftState] = useState(() => (drafts.has(it.id) ? drafts.get(it.id) : stripRun(it.text)))
111
+ const setDraft = (v) => { drafts.set(it.id, v); setDraftState(v) }
112
+ const [busy, setBusy] = useState('')
113
+ const [err, setErr] = useState('')
114
+ const act = (action) => {
115
+ setBusy(action); setErr('')
116
+ jpost('/act', { id: it.id, action, text: action === 'suggest' ? draft : undefined })
117
+ .then(() => { drafts.delete(it.id); reload() }, (e) => setErr(e.message))
118
+ .finally(() => setBusy(''))
119
+ }
120
+ const same = stripRun(draft) === stripRun(it.text)
121
+ if (it.state === 'sending') {
122
+ return h('div', { 'data-lbt-card': '' }, h(Meta, { it, now }), h(Text, { label: '她要发的', text: it.text }),
123
+ h('div', { 'data-lbt-label': '' }, '正在发…'))
124
+ }
125
+ return h('div', { 'data-lbt-card': '' },
126
+ h(Meta, { it, now }),
127
+ h(Context, { it }),
128
+ h(Text, { label: '她要发的', text: it.text }),
129
+ it.ja ? h('details', null, h('summary', null, '她写的日语'), h('div', { 'data-lbt-text': '' }, it.ja)) : null,
130
+ h('div', { 'data-lbt-label': '' }, '你觉得怎么说更好(改了才能送给她;会标「' + owner + '觉得这样表达更好」)'),
131
+ h('textarea', { value: draft, onChange: (e) => setDraft(e.target.value), disabled: busy !== '' }),
132
+ h('div', { 'data-lbt-row': '' },
133
+ h('button', { 'data-main': '', disabled: busy !== '', onClick: () => act('approve') }, busy === 'approve' ? '发送中…' : '放行(照她写的发)'),
134
+ h('button', { disabled: busy !== '' || draft.trim() === '' || same, onClick: () => act('suggest') }, busy === 'suggest' ? '送去中…' : '把建议给她'),
135
+ h('button', { disabled: busy !== '', onClick: () => { if (window.confirm('这句不发了?下一条消息送进她会话时,她会知道你没让发。')) act('drop') } }, '不发了')),
136
+ it.error ? h('div', { 'data-lbt-err': '' }, it.error) : null,
137
+ err ? h('div', { 'data-lbt-err': '' }, err) : null)
138
+ }
139
+
140
+ function Waiting({ it, now, reload }) {
141
+ const [busy, setBusy] = useState(false)
142
+ const [err, setErr] = useState('')
143
+ const resend = () => {
144
+ setBusy(true); setErr('')
145
+ jpost('/act', { id: it.id, action: 'resend' }).then(() => reload(), (e) => setErr(e.message)).finally(() => setBusy(false))
146
+ }
147
+ return h('div', { 'data-lbt-card': '' },
148
+ h(Meta, { it, now }),
149
+ h(Text, { label: '她原来写的', text: it.text }),
150
+ it.suggestion ? h(Text, { label: '你的写法', text: it.suggestion }) : null,
151
+ it.state === 'suggested' ? h('div', { 'data-lbt-row': '' }, h('button', { disabled: busy, onClick: resend }, busy ? '送去中…' : '再送给她一次')) : null,
152
+ it.pairError ? h('div', { 'data-lbt-err': '' }, '表达对照.md:' + it.pairError) : null,
153
+ it.error ? h('div', { 'data-lbt-err': '' }, it.error) : null,
154
+ err ? h('div', { 'data-lbt-err': '' }, err) : null)
155
+ }
156
+
157
+ function Done({ it, now }) {
158
+ return h('div', { 'data-lbt-card': '' },
159
+ h(Meta, { it, now }),
160
+ h(Text, { label: '她原来写的', text: it.text }),
161
+ it.suggestion ? h(Text, { label: '你的写法', text: it.suggestion }) : null,
162
+ it.pick ? h('div', { 'data-lbt-label': '' }, '她选的:' + (PICK[it.pick] || it.pick) + (it.pickedAt ? '(' + ago(now, it.pickedAt) + ')' : '')) : null,
163
+ it.pick === 'rewrite' && it.rewrite ? h(Text, { label: '她重写的', text: it.rewrite }) : null,
164
+ it.reason ? h(Text, { label: it.pick === 'mine' ? '她为什么还是发原来那句' : '她写的理由', text: it.reason }) : null,
165
+ it.diff ? h(Text, { label: '她写的差在哪儿', text: it.diff }) : null,
166
+ (() => {
167
+ // 实际发出去的那句:只在跟该发的那句(去掉「润:」)不一样时显示;回复器真改过才说「回复器顺过」
168
+ const base = it.pick === 'owner' ? it.suggestion : it.pick === 'rewrite' ? it.rewrite : it.text
169
+ if (!it.sentText || stripRun(base) === it.sentText) return null
170
+ return h(Text, { label: it.replierUsed ? '实际发出去的(回复器顺过)' : '实际发出去的', text: it.sentText })
171
+ })(),
172
+ it.pairError ? h('div', { 'data-lbt-err': '' }, '表达对照.md:' + it.pairError) : null,
173
+ it.error ? h('div', { 'data-lbt-err': '' }, it.error) : null)
174
+ }
175
+
176
+ function TeachTab() {
177
+ const [st, setSt] = useState(null)
178
+ const [err, setErr] = useState('')
179
+ const [busy, setBusy] = useState(false)
180
+ const alive = useRef(true)
181
+ const load = useCallback(() => jget('/state').then(
182
+ (r) => { if (alive.current) { setSt(r); setErr('') } },
183
+ (e) => { if (alive.current) setErr(e.message) }), [])
184
+ useEffect(() => {
185
+ alive.current = true
186
+ load()
187
+ const t = setInterval(load, 2000)
188
+ return () => { alive.current = false; clearInterval(t) }
189
+ }, [load])
190
+ const toggle = () => {
191
+ if (!st) return
192
+ setBusy(true)
193
+ jpost('/mode', { on: !st.on }).then(() => load(), (e) => setErr(e.message)).finally(() => setBusy(false))
194
+ }
195
+ const items = st && Array.isArray(st.items) ? st.items : []
196
+ const now = st && st.now ? st.now : Date.now()
197
+ const pending = items.filter((it) => it.state === 'pending' || it.state === 'sending')
198
+ const waiting = items.filter((it) => it.state === 'suggested' || it.state === 'asked' || it.state === 'picking')
199
+ const when = (it) => it.pickedAt || it.decidedAt || it.at
200
+ const done = items.filter((it) => !pending.includes(it) && !waiting.includes(it)).sort((a, b) => when(b) - when(a)).slice(0, 30)
201
+ return h('div', { 'data-lbt': '' },
202
+ h('div', { 'data-lbt-top': '' },
203
+ h('span', { 'data-lbt-title': '' }, '教学模式'),
204
+ h('button', { 'data-main': st && st.on ? '' : undefined, disabled: !st || busy, onClick: toggle },
205
+ !st ? '…' : st.on ? '开着(点一下关掉)' : '关着(点一下打开)')),
206
+ h('div', { 'data-lbt-hint': '' },
207
+ '开着时她每次 im_send 都先到这里,不等你,这一轮照常往下走。放行就照她写的发;写了建议就送给她,她自己选怎么发(你的、她重写的、她原来的、不发),两句都记进她的 表达对照.md。' +
208
+ '放行和不发了的结果,等下一条消息送进她会话时带给她。不经过这里的:戳一戳、发到杀戮尖塔的、表情包和图、QQ 空间、我的世界里 mc 程序自己说的话(act.say 之类)、转到你私聊的卡片。'),
209
+ err ? h('div', { 'data-lbt-err': '' }, err) : null,
210
+ st && st.lastError ? h('div', { 'data-lbt-err': '' }, '桥那边' + (st.lastErrorAt ? '(' + clock(st.lastErrorAt) + ')' : '') + ':' + st.lastError) : null,
211
+ h('div', { 'data-lbt-h': '' }, '等你审(' + pending.length + ')'),
212
+ pending.length ? pending.map((it) => h(Pending, { key: it.id, it, now, reload: load, owner: ownerName(st) })) : h('div', { 'data-lbt-empty': '' }, st ? '没有' : '读取中…'),
213
+ waiting.length ? h('div', { 'data-lbt-h': '' }, '等她选(' + waiting.length + ')') : null,
214
+ waiting.map((it) => h(Waiting, { key: it.id, it, now, reload: load })),
215
+ done.length ? h('div', { 'data-lbt-h': '' }, '最近') : null,
216
+ done.map((it) => h(Done, { key: it.id, it, now })))
217
+ }
218
+
219
+ return {
220
+ inject: ['slots'],
221
+ apply(ctx) {
222
+ const style = document.createElement('style')
223
+ style.setAttribute('data-plugin-css', 'fish-bridge')
224
+ style.textContent = CSS
225
+ document.head.appendChild(style)
226
+ ctx.effect(() => () => style.remove(), 'fish-bridge: 样式')
227
+
228
+ const slots = (typeof ctx.get === 'function' ? ctx.get('slots') : undefined) || ctx.slots
229
+ if (!slots) return
230
+ try {
231
+ let viewOff = null
232
+ let viewSlotReady = false
233
+ let lastMine = false
234
+ const dropView = () => {
235
+ if (!viewOff) return
236
+ try { viewOff() } catch { /* 已经撤过了 */ }
237
+ viewOff = null
238
+ }
239
+ const ensureView = () => {
240
+ if (viewOff || !viewSlotReady) return
241
+ try {
242
+ viewOff = slots.register({ name: 'conversation.view', id: 'fish-bridge-teach', order: 45, label: '审批' }, () => h(TeachTab))
243
+ } catch { viewOff = null }
244
+ }
245
+ slots.inject('conversation.view', () => {
246
+ viewSlotReady = true
247
+ if (lastMine) ensureView()
248
+ return () => { viewSlotReady = false; dropView() }
249
+ })
250
+ const cache = new Map() // sessionId → { mine, at }
251
+ const ScopeProbe = ({ sessionId }) => {
252
+ const [nonce, setNonce] = useState(0)
253
+ useEffect(() => {
254
+ const t = setInterval(() => setNonce((n) => n + 1), 30_000)
255
+ return () => clearInterval(t)
256
+ }, [])
257
+ useEffect(() => {
258
+ const sid = String(sessionId || '')
259
+ // 拿不到 sessionId(槽位没给 / 宿主改了)时不带 session 问:桥答得上来(功能开着)就挂,别把标签弄没了
260
+ const hit = cache.get(sid)
261
+ if (hit) { lastMine = hit.mine; if (hit.mine) ensureView(); else dropView() }
262
+ if (hit && Date.now() - hit.at < 10 * 60_000) return () => {}
263
+ // 没问过的会话:先收起来再问(宁可晚一帧出现,也别在别的会话上闪)。
264
+ // 问过的照旧挂着,在后台再问一次,明确说不是她的会话才撤:免得把开着的标签打回「对话」、清掉没写完的建议
265
+ if (!hit) dropView()
266
+ let live = true
267
+ const settle = (mine) => {
268
+ if (!live) return
269
+ lastMine = mine
270
+ cache.set(sid, { mine, at: Date.now() })
271
+ if (mine) ensureView(); else dropView()
272
+ }
273
+ jget(sid ? '/state?session=' + encodeURIComponent(sid) : '/state').then(
274
+ // 只有桥正经答了(ok: true)而且没说「不是她的会话」才挂。答的不是桥的 JSON(宿主把没挂的路径交给了页面)
275
+ // 也不挂:教学模式这个功能关着时桥根本不挂接口
276
+ (r) => settle(!!r && typeof r === 'object' && r.ok === true && r.mine !== false),
277
+ // 404:桥没挂接口(功能关着、或者桥没装上),不挂。别的错(没登录、不是主人、宿主的鉴权拿不到、桥没起来):
278
+ // 挂上,让主人在标签里看到为什么
279
+ (e) => settle(!(e && e.status === 404)))
280
+ return () => { live = false }
281
+ }, [sessionId, nonce])
282
+ useEffect(() => () => { dropView() }, [])
283
+ return h('span', { 'data-lbt-probe': '', style: { display: 'none' } })
284
+ }
285
+ slots.inject('conversation.input.left', () => slots.register({
286
+ name: 'conversation.input.left',
287
+ id: 'fish-bridge-teach-scope',
288
+ order: 6,
289
+ }, (props) => h(ScopeProbe, props || {})))
290
+ } catch { /* 没有这些槽位就算了 */ }
291
+ },
292
+ }
293
+ },
294
+ })