dsh-tiddlywiki 0.16.13 → 0.16.14
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/LICENSE +21 -21
- package/README.md +2 -0
- package/cordis.patch.yml +13 -13
- package/lib/client.bundle.js +32 -26
- package/lib/client.js +32 -26
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client/endpoints.ts +10 -10
- package/src/client/markdown-editor.ts +128 -128
- package/src/client/quick-note-dock.ts +133 -133
- package/src/client/session-summary.ts +223 -31
- package/src/client/state.ts +39 -39
- package/src/client/styles.ts +6 -0
- package/src/client/theme-sync.ts +312 -312
- package/src/client/toast.ts +22 -22
- package/src/client/tool-views.ts +604 -604
- package/src/client/ui-config.ts +44 -44
- package/src/host/http.ts +57 -57
- package/src/host/seed-render.ts +66 -66
- package/src/host/seed-send-to-agent.ts +63 -63
- package/src/host/seeds.ts +299 -299
- package/src/host/session-summary.ts +422 -422
- package/src/host/tw-api.ts +231 -231
- package/src/host/wiki.ts +299 -299
- package/src/sdk.ts +204 -204
package/src/host/seeds.ts
CHANGED
|
@@ -1,299 +1,299 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Unified seed registry (design: every one-time "与 dsh 联动需要 wiki 预置"
|
|
3
|
-
* item is a SeedDef here).
|
|
4
|
-
*
|
|
5
|
-
* Two tiers (v0.15.0):
|
|
6
|
-
* - CORE seeds (`core: true`) are functionally required by the plugin's
|
|
7
|
-
* own features — the startup path seeds exactly these (non-force).
|
|
8
|
-
* - OPTIONAL seeds (`core: false`) are nice-to-have content (说明笔记 /
|
|
9
|
-
* 首页 / 所有文章 / menubar 顶栏主题自适应) — they are NEVER auto-seeded;
|
|
10
|
-
* the user opts in from the settings page「初始化」section (重新初始化)
|
|
11
|
-
* and can opt out again with 反初始化 (remove).
|
|
12
|
-
*
|
|
13
|
-
* Each seed owns:
|
|
14
|
-
* - `check` — current state (present / missing / needs-update) for the UI;
|
|
15
|
-
* - `run(force)` — non-force keeps the ONE-SHOT / user-owned semantics
|
|
16
|
-
* (write only when missing, never overwrite), force (re)writes the
|
|
17
|
-
* built-in content and (re)records the marker;
|
|
18
|
-
* - `remove` — optional seeds only: delete the seeded tiddlers + markers,
|
|
19
|
-
* returning the wiki to the "never seeded" state.
|
|
20
|
-
*
|
|
21
|
-
* Registry: doc-note / send-to-agent / render-route / home-index /
|
|
22
|
-
* all-articles / menubar-theme / tw-web-host.
|
|
23
|
-
*
|
|
24
|
-
* @module dsh-tiddlywiki/host/seeds
|
|
25
|
-
*/
|
|
26
|
-
import type { TiddlyWebClient } from './tw-api.ts'
|
|
27
|
-
import { seedDocNote, unseedDocNote, DOC_NOTE_TITLE } from './seed-notes.ts'
|
|
28
|
-
import { seedSendToAgent, SEND_TO_AGENT_PLUGIN_TITLE } from './seed-send-to-agent.ts'
|
|
29
|
-
import { seedHomeIndex, unseedHomeIndex, HOME_INDEX_ITEMS } from './seed-home.ts'
|
|
30
|
-
import { seedAllArticles, unseedAllArticles, ALL_ARTICLES_TITLE } from './seed-all-articles.ts'
|
|
31
|
-
import { seedMenubarTheme, unseedMenubarTheme, MENUBAR_THEME_TIDDLER } from './seed-menubar-theme.ts'
|
|
32
|
-
import { seedRenderRoute, RENDER_PLUGIN_TITLE } from './seed-render.ts'
|
|
33
|
-
import { TW_WEB_HOST_TIDDLER, TW_WEB_HOST_DEFAULT } from './config.ts'
|
|
34
|
-
import { TW_PROXY_PATH } from './wiki.ts'
|
|
35
|
-
|
|
36
|
-
/** One seed's status as reported to the settings page. */
|
|
37
|
-
export interface SeedStatus {
|
|
38
|
-
id: string
|
|
39
|
-
title: string
|
|
40
|
-
description: string
|
|
41
|
-
/** true = target tiddler(s) present (or host value correct). */
|
|
42
|
-
present: boolean
|
|
43
|
-
/**
|
|
44
|
-
* true = the seed can be 反初始化 (removed). Core seeds that the plugin's
|
|
45
|
-
* own features depend on are never removable from the settings page.
|
|
46
|
-
*/
|
|
47
|
-
removable: boolean
|
|
48
|
-
/** Human detail, e.g. which tiddlers are missing. */
|
|
49
|
-
detail?: string
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Result of running one seed. */
|
|
53
|
-
export interface SeedRunResult {
|
|
54
|
-
id: string
|
|
55
|
-
ok: boolean
|
|
56
|
-
/** true = something was written this call. */
|
|
57
|
-
wrote: boolean
|
|
58
|
-
detail?: string
|
|
59
|
-
error?: string
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Context a seed needs (client to the live TW server). */
|
|
63
|
-
export interface SeedContext {
|
|
64
|
-
client: TiddlyWebClient
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** A registered seed: check current state + run (optionally force) + remove. */
|
|
68
|
-
export interface SeedDef {
|
|
69
|
-
id: string
|
|
70
|
-
title: string
|
|
71
|
-
description: string
|
|
72
|
-
/**
|
|
73
|
-
* Core seeds are seeded automatically on startup (功能必需). Optional
|
|
74
|
-
* seeds are manual-only from the settings page — never forced on users.
|
|
75
|
-
*/
|
|
76
|
-
core: boolean
|
|
77
|
-
check(ctx: SeedContext): Promise<SeedStatus>
|
|
78
|
-
run(ctx: SeedContext, force: boolean): Promise<SeedRunResult>
|
|
79
|
-
/** Optional seeds only: delete the seeded tiddlers + markers (反初始化). */
|
|
80
|
-
remove?(ctx: SeedContext): Promise<SeedRunResult>
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const presentOf = (ctx: SeedContext, title: string): Promise<boolean> =>
|
|
84
|
-
ctx.client.get(title).then((t) => t !== undefined).catch(() => false)
|
|
85
|
-
|
|
86
|
-
/** Build the per-item detail from an unseed result. */
|
|
87
|
-
const removedDetail = (id: string, removed: string[]): SeedRunResult => ({
|
|
88
|
-
id,
|
|
89
|
-
ok: true,
|
|
90
|
-
wrote: false,
|
|
91
|
-
detail: removed.length > 0
|
|
92
|
-
? `已移除:${removed.join('、')}`
|
|
93
|
-
: '本就不存在,无需移除',
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
/** Metadata shared by every seed and echoed in its status/result objects. */
|
|
97
|
-
interface SeedMeta {
|
|
98
|
-
id: string
|
|
99
|
-
title: string
|
|
100
|
-
description: string
|
|
101
|
-
core: boolean
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
type SeedWriter = (client: TiddlyWebClient, opts?: { force?: boolean }) => Promise<boolean>
|
|
105
|
-
type SeedUnseeder = (client: TiddlyWebClient) => Promise<{ removed: string[] }>
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Define a seed from a small spec, killing the id/title/description
|
|
109
|
-
* triplication and the try/catch boilerplate every registry entry used to
|
|
110
|
-
* repeat by hand:
|
|
111
|
-
* - `presentTitle` → default check: is that tiddler present?
|
|
112
|
-
* - `check` → custom presence check (overrides presentTitle).
|
|
113
|
-
* - `write` → built-in content writer; true = (re)written this call.
|
|
114
|
-
* - `run` → custom runner (overrides the standard write wrapper;
|
|
115
|
-
* e.g. tw-web-host honors a user-chosen host value).
|
|
116
|
-
* - `unseed` → remove the seeded tiddlers + markers (removable seeds).
|
|
117
|
-
*/
|
|
118
|
-
function defineSeed(meta: SeedMeta, impl: {
|
|
119
|
-
presentTitle?: string
|
|
120
|
-
check?: (ctx: SeedContext) => Promise<SeedStatus>
|
|
121
|
-
write?: SeedWriter
|
|
122
|
-
run?: (ctx: SeedContext, force: boolean) => Promise<SeedRunResult>
|
|
123
|
-
unseed?: SeedUnseeder
|
|
124
|
-
}): SeedDef {
|
|
125
|
-
const { id, title, description, core } = meta
|
|
126
|
-
const removable = !core
|
|
127
|
-
const check: SeedDef['check'] = impl.check ?? (async (ctx) => {
|
|
128
|
-
const present = await presentOf(ctx, impl.presentTitle ?? '')
|
|
129
|
-
return { id, title, description, present, removable, detail: present ? '已存在' : '缺失' }
|
|
130
|
-
})
|
|
131
|
-
const run: SeedDef['run'] = impl.run ?? (async (ctx, force) => {
|
|
132
|
-
try {
|
|
133
|
-
const wrote = await impl.write!(ctx.client, { force })
|
|
134
|
-
return { id, ok: true, wrote, detail: wrote ? (force ? '已重新初始化' : '已写入') : (force ? '内容已是最新(未重写)' : '已存在,跳过') }
|
|
135
|
-
} catch (err) {
|
|
136
|
-
return { id, ok: false, wrote: false, error: err instanceof Error ? err.message : String(err) }
|
|
137
|
-
}
|
|
138
|
-
})
|
|
139
|
-
const remove: SeedDef['remove'] = impl.unseed === undefined ? undefined : async (ctx) => {
|
|
140
|
-
try {
|
|
141
|
-
return removedDetail(id, (await impl.unseed!(ctx.client)).removed)
|
|
142
|
-
} catch (err) {
|
|
143
|
-
return { id, ok: false, wrote: false, error: err instanceof Error ? err.message : String(err) }
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return { id, title, description, core, check, run, ...(remove === undefined ? {} : { remove }) }
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** The full registry, in display order. */
|
|
150
|
-
export const SEED_DEFS: SeedDef[] = [
|
|
151
|
-
defineSeed(
|
|
152
|
-
{ id: 'doc-note', title: '插件说明笔记', description: '「dsh-tiddlywiki 插件说明」——新 wiki 首启自动写入的入门说明(ONE-SHOT,用户可改可删)。', core: false },
|
|
153
|
-
{ presentTitle: DOC_NOTE_TITLE, write: seedDocNote, unseed: unseedDocNote },
|
|
154
|
-
),
|
|
155
|
-
defineSeed(
|
|
156
|
-
{ id: 'send-to-agent', title: '「发送给 Agent」按钮', description: 'TW 笔记工具栏「发送给 Agent」按钮插件($:/plugins/dsh/send-to-agent)——把笔记一键注入 DSH 会话。', core: true },
|
|
157
|
-
{ presentTitle: SEND_TO_AGENT_PLUGIN_TITLE, write: seedSendToAgent },
|
|
158
|
-
),
|
|
159
|
-
defineSeed(
|
|
160
|
-
{ id: 'render-route', title: '原生渲染路由(/render)', description: 'TW 服务端路由插件($:/plugins/dsh/render,server-routes/render.js)——把 wiki 文本在运行中的 TW 里原生渲染成 HTML 片段,回复流工具卡与 wiki 链接跳转依赖它。seed 写入后需重启 TW 使路由生效。', core: true },
|
|
161
|
-
{ presentTitle: RENDER_PLUGIN_TITLE, write: seedRenderRoute },
|
|
162
|
-
),
|
|
163
|
-
defineSeed(
|
|
164
|
-
{ id: 'home-index', title: '首页(主页 / 所有标签 / 标签笔记)', description: '默认主页:四象限待办 + 「所有标签」「所有文章」入口;所有标签:标签统计 + Agent 区块(纯 Agent / Agent+人工);标签笔记:按标签浏览。系统提示承诺的首页由这里 seed,主页同时写入 $:/DefaultTiddlers。', core: false },
|
|
165
|
-
{
|
|
166
|
-
check: async (ctx) => {
|
|
167
|
-
const missing: string[] = []
|
|
168
|
-
for (const item of HOME_INDEX_ITEMS) {
|
|
169
|
-
if (!(await presentOf(ctx, item.title))) missing.push(item.title)
|
|
170
|
-
}
|
|
171
|
-
return { id: 'home-index', title: '首页(主页 / 所有标签 / 标签笔记)', description: '默认主页:四象限待办 + 「所有标签」「所有文章」入口;所有标签:标签统计 + Agent 区块(纯 Agent / Agent+人工);标签笔记:按标签浏览。系统提示承诺的首页由这里 seed,主页同时写入 $:/DefaultTiddlers。', present: missing.length === 0, removable: true, detail: missing.length === 0 ? '已存在' : `缺失:${missing.join('、')}` }
|
|
172
|
-
},
|
|
173
|
-
write: seedHomeIndex,
|
|
174
|
-
unseed: unseedHomeIndex,
|
|
175
|
-
},
|
|
176
|
-
),
|
|
177
|
-
defineSeed(
|
|
178
|
-
{ id: 'all-articles', title: '所有文章(两列分页总览)', description: '「所有文章」——全部条目分两列(🤖 Agent 撰写 / 👤 人工·人类)各自分页展示。每页条数取插件设置 ui.allArticles.pageSize(默认 10)。', core: false },
|
|
179
|
-
{ presentTitle: ALL_ARTICLES_TITLE, write: seedAllArticles, unseed: unseedAllArticles },
|
|
180
|
-
),
|
|
181
|
-
defineSeed(
|
|
182
|
-
{ id: 'menubar-theme', title: 'menubar 顶栏主题自适应', description: '样式表覆盖($:/plugins/dsh-tiddlywiki/menubar-theme,tag $:/tags/Stylesheet)——把 tiddlywiki/menubar 顶栏从「默认色映射的蓝色」改为跟随当前 palette 的 background/foreground,随 DSH 主题切换($:/palette 翻转)自动换色。', core: false },
|
|
183
|
-
{ presentTitle: MENUBAR_THEME_TIDDLER, write: seedMenubarTheme, unseed: unseedMenubarTheme },
|
|
184
|
-
),
|
|
185
|
-
defineSeed(
|
|
186
|
-
{ id: 'tw-web-host', title: 'TW 前端 API 基址(同源代理)', description: '把 $:/config/tiddlyweb/host 指向 DSH 同源代理,嵌入式 TW 才能经 DSH origin 访问(远程访问模式的前提)。', core: true },
|
|
187
|
-
{
|
|
188
|
-
check: async (ctx) => {
|
|
189
|
-
let current: string | undefined
|
|
190
|
-
try {
|
|
191
|
-
current = (await ctx.client.get(TW_WEB_HOST_TIDDLER))?.text?.trim()
|
|
192
|
-
} catch {
|
|
193
|
-
current = undefined
|
|
194
|
-
}
|
|
195
|
-
const ok = current === TW_PROXY_PATH
|
|
196
|
-
return { id: 'tw-web-host', title: 'TW 前端 API 基址(同源代理)', description: '把 $:/config/tiddlyweb/host 指向 DSH 同源代理,嵌入式 TW 才能经 DSH origin 访问(远程访问模式的前提)。', present: ok, removable: false, detail: ok ? `已指向 ${TW_PROXY_PATH}` : `当前:${current ?? '(缺失)'},应为 ${TW_PROXY_PATH}` }
|
|
197
|
-
},
|
|
198
|
-
run: async (ctx, force) => {
|
|
199
|
-
try {
|
|
200
|
-
let current: string | undefined
|
|
201
|
-
try {
|
|
202
|
-
current = (await ctx.client.get(TW_WEB_HOST_TIDDLER))?.text?.trim()
|
|
203
|
-
} catch {
|
|
204
|
-
current = undefined
|
|
205
|
-
}
|
|
206
|
-
// Non-force keeps the ensure semantics: write only when missing or still
|
|
207
|
-
// the legacy default (a user override pointing elsewhere is honored).
|
|
208
|
-
if (!force && current !== undefined && current !== TW_WEB_HOST_DEFAULT) {
|
|
209
|
-
return { id: 'tw-web-host', ok: true, wrote: false, detail: '已指向自定义基址,未覆盖' }
|
|
210
|
-
}
|
|
211
|
-
await ctx.client.put({ title: TW_WEB_HOST_TIDDLER, text: TW_PROXY_PATH, type: 'text/plain', tags: [] })
|
|
212
|
-
return { id: 'tw-web-host', ok: true, wrote: true, detail: force ? '已重新初始化(强制写回代理基址)' : '已写入代理基址' }
|
|
213
|
-
} catch (err) {
|
|
214
|
-
return { id: 'tw-web-host', ok: false, wrote: false, error: err instanceof Error ? err.message : String(err) }
|
|
215
|
-
}
|
|
216
|
-
},
|
|
217
|
-
},
|
|
218
|
-
),
|
|
219
|
-
]
|
|
220
|
-
|
|
221
|
-
/** Check every seed, returning statuses in registry order. */
|
|
222
|
-
export async function checkAllSeeds(ctx: SeedContext): Promise<SeedStatus[]> {
|
|
223
|
-
const out: SeedStatus[] = []
|
|
224
|
-
for (const def of SEED_DEFS) {
|
|
225
|
-
try {
|
|
226
|
-
out.push(await def.check(ctx))
|
|
227
|
-
} catch (err) {
|
|
228
|
-
out.push({
|
|
229
|
-
id: def.id,
|
|
230
|
-
title: def.title,
|
|
231
|
-
description: def.description,
|
|
232
|
-
present: false,
|
|
233
|
-
removable: !def.core,
|
|
234
|
-
detail: `检查失败:${err instanceof Error ? err.message : String(err)}`,
|
|
235
|
-
})
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
return out
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Run one seed (or all, when id is undefined). Non-force = one-shot semantics;
|
|
243
|
-
* force = manual "重新初始化" from the settings page. A manual run with no id
|
|
244
|
-
* (重新初始化 all) covers every registry item, core and optional alike.
|
|
245
|
-
*/
|
|
246
|
-
export async function runSeedById(ctx: SeedContext, id: string | undefined, force: boolean): Promise<SeedRunResult[]> {
|
|
247
|
-
const targets = id === undefined ? SEED_DEFS : SEED_DEFS.filter((d) => d.id === id)
|
|
248
|
-
if (targets.length === 0) {
|
|
249
|
-
return [{ id: id ?? '', ok: false, wrote: false, error: `unknown seed: ${id}` }]
|
|
250
|
-
}
|
|
251
|
-
const out: SeedRunResult[] = []
|
|
252
|
-
for (const def of targets) {
|
|
253
|
-
out.push(await def.run(ctx, force))
|
|
254
|
-
}
|
|
255
|
-
return out
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Startup path: seed ONLY the core items (功能必需:发送给 Agent 按钮 +
|
|
260
|
-
* TW 前端 API 基址), non-force (write only what is missing). Optional seeds
|
|
261
|
-
* (说明笔记 / 首页 / 所有文章 / menubar 顶栏主题自适应) are never forced on
|
|
262
|
-
* users — they opt in from the settings page「初始化」section.
|
|
263
|
-
*/
|
|
264
|
-
export async function runAllSeeds(ctx: SeedContext): Promise<SeedRunResult[]> {
|
|
265
|
-
const out: SeedRunResult[] = []
|
|
266
|
-
for (const def of SEED_DEFS) {
|
|
267
|
-
if (!def.core) continue
|
|
268
|
-
out.push(await def.run(ctx, false))
|
|
269
|
-
}
|
|
270
|
-
return out
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* 反初始化 (remove): delete one optional seed's seeded tiddlers + markers, or
|
|
275
|
-
* all optional seeds when `id` is undefined. Core seeds are functionally
|
|
276
|
-
* required and cannot be removed — a direct request for one returns an error
|
|
277
|
-
* result (and is skipped when removing all).
|
|
278
|
-
*/
|
|
279
|
-
export async function removeSeedById(ctx: SeedContext, id: string | undefined): Promise<SeedRunResult[]> {
|
|
280
|
-
if (id === undefined) {
|
|
281
|
-
const out: SeedRunResult[] = []
|
|
282
|
-
for (const def of SEED_DEFS) {
|
|
283
|
-
if (def.core || def.remove === undefined) continue
|
|
284
|
-
out.push(await def.remove(ctx))
|
|
285
|
-
}
|
|
286
|
-
return out
|
|
287
|
-
}
|
|
288
|
-
const def = SEED_DEFS.find((d) => d.id === id)
|
|
289
|
-
if (def === undefined) {
|
|
290
|
-
return [{ id, ok: false, wrote: false, error: `unknown seed: ${id}` }]
|
|
291
|
-
}
|
|
292
|
-
if (def.core) {
|
|
293
|
-
return [{ id, ok: false, wrote: false, error: '该 seed 为核心项(功能必需),不可反初始化' }]
|
|
294
|
-
}
|
|
295
|
-
if (def.remove === undefined) {
|
|
296
|
-
return [{ id, ok: false, wrote: false, error: '该 seed 不支持移除' }]
|
|
297
|
-
}
|
|
298
|
-
return [await def.remove(ctx)]
|
|
299
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Unified seed registry (design: every one-time "与 dsh 联动需要 wiki 预置"
|
|
3
|
+
* item is a SeedDef here).
|
|
4
|
+
*
|
|
5
|
+
* Two tiers (v0.15.0):
|
|
6
|
+
* - CORE seeds (`core: true`) are functionally required by the plugin's
|
|
7
|
+
* own features — the startup path seeds exactly these (non-force).
|
|
8
|
+
* - OPTIONAL seeds (`core: false`) are nice-to-have content (说明笔记 /
|
|
9
|
+
* 首页 / 所有文章 / menubar 顶栏主题自适应) — they are NEVER auto-seeded;
|
|
10
|
+
* the user opts in from the settings page「初始化」section (重新初始化)
|
|
11
|
+
* and can opt out again with 反初始化 (remove).
|
|
12
|
+
*
|
|
13
|
+
* Each seed owns:
|
|
14
|
+
* - `check` — current state (present / missing / needs-update) for the UI;
|
|
15
|
+
* - `run(force)` — non-force keeps the ONE-SHOT / user-owned semantics
|
|
16
|
+
* (write only when missing, never overwrite), force (re)writes the
|
|
17
|
+
* built-in content and (re)records the marker;
|
|
18
|
+
* - `remove` — optional seeds only: delete the seeded tiddlers + markers,
|
|
19
|
+
* returning the wiki to the "never seeded" state.
|
|
20
|
+
*
|
|
21
|
+
* Registry: doc-note / send-to-agent / render-route / home-index /
|
|
22
|
+
* all-articles / menubar-theme / tw-web-host.
|
|
23
|
+
*
|
|
24
|
+
* @module dsh-tiddlywiki/host/seeds
|
|
25
|
+
*/
|
|
26
|
+
import type { TiddlyWebClient } from './tw-api.ts'
|
|
27
|
+
import { seedDocNote, unseedDocNote, DOC_NOTE_TITLE } from './seed-notes.ts'
|
|
28
|
+
import { seedSendToAgent, SEND_TO_AGENT_PLUGIN_TITLE } from './seed-send-to-agent.ts'
|
|
29
|
+
import { seedHomeIndex, unseedHomeIndex, HOME_INDEX_ITEMS } from './seed-home.ts'
|
|
30
|
+
import { seedAllArticles, unseedAllArticles, ALL_ARTICLES_TITLE } from './seed-all-articles.ts'
|
|
31
|
+
import { seedMenubarTheme, unseedMenubarTheme, MENUBAR_THEME_TIDDLER } from './seed-menubar-theme.ts'
|
|
32
|
+
import { seedRenderRoute, RENDER_PLUGIN_TITLE } from './seed-render.ts'
|
|
33
|
+
import { TW_WEB_HOST_TIDDLER, TW_WEB_HOST_DEFAULT } from './config.ts'
|
|
34
|
+
import { TW_PROXY_PATH } from './wiki.ts'
|
|
35
|
+
|
|
36
|
+
/** One seed's status as reported to the settings page. */
|
|
37
|
+
export interface SeedStatus {
|
|
38
|
+
id: string
|
|
39
|
+
title: string
|
|
40
|
+
description: string
|
|
41
|
+
/** true = target tiddler(s) present (or host value correct). */
|
|
42
|
+
present: boolean
|
|
43
|
+
/**
|
|
44
|
+
* true = the seed can be 反初始化 (removed). Core seeds that the plugin's
|
|
45
|
+
* own features depend on are never removable from the settings page.
|
|
46
|
+
*/
|
|
47
|
+
removable: boolean
|
|
48
|
+
/** Human detail, e.g. which tiddlers are missing. */
|
|
49
|
+
detail?: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Result of running one seed. */
|
|
53
|
+
export interface SeedRunResult {
|
|
54
|
+
id: string
|
|
55
|
+
ok: boolean
|
|
56
|
+
/** true = something was written this call. */
|
|
57
|
+
wrote: boolean
|
|
58
|
+
detail?: string
|
|
59
|
+
error?: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Context a seed needs (client to the live TW server). */
|
|
63
|
+
export interface SeedContext {
|
|
64
|
+
client: TiddlyWebClient
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A registered seed: check current state + run (optionally force) + remove. */
|
|
68
|
+
export interface SeedDef {
|
|
69
|
+
id: string
|
|
70
|
+
title: string
|
|
71
|
+
description: string
|
|
72
|
+
/**
|
|
73
|
+
* Core seeds are seeded automatically on startup (功能必需). Optional
|
|
74
|
+
* seeds are manual-only from the settings page — never forced on users.
|
|
75
|
+
*/
|
|
76
|
+
core: boolean
|
|
77
|
+
check(ctx: SeedContext): Promise<SeedStatus>
|
|
78
|
+
run(ctx: SeedContext, force: boolean): Promise<SeedRunResult>
|
|
79
|
+
/** Optional seeds only: delete the seeded tiddlers + markers (反初始化). */
|
|
80
|
+
remove?(ctx: SeedContext): Promise<SeedRunResult>
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const presentOf = (ctx: SeedContext, title: string): Promise<boolean> =>
|
|
84
|
+
ctx.client.get(title).then((t) => t !== undefined).catch(() => false)
|
|
85
|
+
|
|
86
|
+
/** Build the per-item detail from an unseed result. */
|
|
87
|
+
const removedDetail = (id: string, removed: string[]): SeedRunResult => ({
|
|
88
|
+
id,
|
|
89
|
+
ok: true,
|
|
90
|
+
wrote: false,
|
|
91
|
+
detail: removed.length > 0
|
|
92
|
+
? `已移除:${removed.join('、')}`
|
|
93
|
+
: '本就不存在,无需移除',
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
/** Metadata shared by every seed and echoed in its status/result objects. */
|
|
97
|
+
interface SeedMeta {
|
|
98
|
+
id: string
|
|
99
|
+
title: string
|
|
100
|
+
description: string
|
|
101
|
+
core: boolean
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type SeedWriter = (client: TiddlyWebClient, opts?: { force?: boolean }) => Promise<boolean>
|
|
105
|
+
type SeedUnseeder = (client: TiddlyWebClient) => Promise<{ removed: string[] }>
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Define a seed from a small spec, killing the id/title/description
|
|
109
|
+
* triplication and the try/catch boilerplate every registry entry used to
|
|
110
|
+
* repeat by hand:
|
|
111
|
+
* - `presentTitle` → default check: is that tiddler present?
|
|
112
|
+
* - `check` → custom presence check (overrides presentTitle).
|
|
113
|
+
* - `write` → built-in content writer; true = (re)written this call.
|
|
114
|
+
* - `run` → custom runner (overrides the standard write wrapper;
|
|
115
|
+
* e.g. tw-web-host honors a user-chosen host value).
|
|
116
|
+
* - `unseed` → remove the seeded tiddlers + markers (removable seeds).
|
|
117
|
+
*/
|
|
118
|
+
function defineSeed(meta: SeedMeta, impl: {
|
|
119
|
+
presentTitle?: string
|
|
120
|
+
check?: (ctx: SeedContext) => Promise<SeedStatus>
|
|
121
|
+
write?: SeedWriter
|
|
122
|
+
run?: (ctx: SeedContext, force: boolean) => Promise<SeedRunResult>
|
|
123
|
+
unseed?: SeedUnseeder
|
|
124
|
+
}): SeedDef {
|
|
125
|
+
const { id, title, description, core } = meta
|
|
126
|
+
const removable = !core
|
|
127
|
+
const check: SeedDef['check'] = impl.check ?? (async (ctx) => {
|
|
128
|
+
const present = await presentOf(ctx, impl.presentTitle ?? '')
|
|
129
|
+
return { id, title, description, present, removable, detail: present ? '已存在' : '缺失' }
|
|
130
|
+
})
|
|
131
|
+
const run: SeedDef['run'] = impl.run ?? (async (ctx, force) => {
|
|
132
|
+
try {
|
|
133
|
+
const wrote = await impl.write!(ctx.client, { force })
|
|
134
|
+
return { id, ok: true, wrote, detail: wrote ? (force ? '已重新初始化' : '已写入') : (force ? '内容已是最新(未重写)' : '已存在,跳过') }
|
|
135
|
+
} catch (err) {
|
|
136
|
+
return { id, ok: false, wrote: false, error: err instanceof Error ? err.message : String(err) }
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
const remove: SeedDef['remove'] = impl.unseed === undefined ? undefined : async (ctx) => {
|
|
140
|
+
try {
|
|
141
|
+
return removedDetail(id, (await impl.unseed!(ctx.client)).removed)
|
|
142
|
+
} catch (err) {
|
|
143
|
+
return { id, ok: false, wrote: false, error: err instanceof Error ? err.message : String(err) }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { id, title, description, core, check, run, ...(remove === undefined ? {} : { remove }) }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The full registry, in display order. */
|
|
150
|
+
export const SEED_DEFS: SeedDef[] = [
|
|
151
|
+
defineSeed(
|
|
152
|
+
{ id: 'doc-note', title: '插件说明笔记', description: '「dsh-tiddlywiki 插件说明」——新 wiki 首启自动写入的入门说明(ONE-SHOT,用户可改可删)。', core: false },
|
|
153
|
+
{ presentTitle: DOC_NOTE_TITLE, write: seedDocNote, unseed: unseedDocNote },
|
|
154
|
+
),
|
|
155
|
+
defineSeed(
|
|
156
|
+
{ id: 'send-to-agent', title: '「发送给 Agent」按钮', description: 'TW 笔记工具栏「发送给 Agent」按钮插件($:/plugins/dsh/send-to-agent)——把笔记一键注入 DSH 会话。', core: true },
|
|
157
|
+
{ presentTitle: SEND_TO_AGENT_PLUGIN_TITLE, write: seedSendToAgent },
|
|
158
|
+
),
|
|
159
|
+
defineSeed(
|
|
160
|
+
{ id: 'render-route', title: '原生渲染路由(/render)', description: 'TW 服务端路由插件($:/plugins/dsh/render,server-routes/render.js)——把 wiki 文本在运行中的 TW 里原生渲染成 HTML 片段,回复流工具卡与 wiki 链接跳转依赖它。seed 写入后需重启 TW 使路由生效。', core: true },
|
|
161
|
+
{ presentTitle: RENDER_PLUGIN_TITLE, write: seedRenderRoute },
|
|
162
|
+
),
|
|
163
|
+
defineSeed(
|
|
164
|
+
{ id: 'home-index', title: '首页(主页 / 所有标签 / 标签笔记)', description: '默认主页:四象限待办 + 「所有标签」「所有文章」入口;所有标签:标签统计 + Agent 区块(纯 Agent / Agent+人工);标签笔记:按标签浏览。系统提示承诺的首页由这里 seed,主页同时写入 $:/DefaultTiddlers。', core: false },
|
|
165
|
+
{
|
|
166
|
+
check: async (ctx) => {
|
|
167
|
+
const missing: string[] = []
|
|
168
|
+
for (const item of HOME_INDEX_ITEMS) {
|
|
169
|
+
if (!(await presentOf(ctx, item.title))) missing.push(item.title)
|
|
170
|
+
}
|
|
171
|
+
return { id: 'home-index', title: '首页(主页 / 所有标签 / 标签笔记)', description: '默认主页:四象限待办 + 「所有标签」「所有文章」入口;所有标签:标签统计 + Agent 区块(纯 Agent / Agent+人工);标签笔记:按标签浏览。系统提示承诺的首页由这里 seed,主页同时写入 $:/DefaultTiddlers。', present: missing.length === 0, removable: true, detail: missing.length === 0 ? '已存在' : `缺失:${missing.join('、')}` }
|
|
172
|
+
},
|
|
173
|
+
write: seedHomeIndex,
|
|
174
|
+
unseed: unseedHomeIndex,
|
|
175
|
+
},
|
|
176
|
+
),
|
|
177
|
+
defineSeed(
|
|
178
|
+
{ id: 'all-articles', title: '所有文章(两列分页总览)', description: '「所有文章」——全部条目分两列(🤖 Agent 撰写 / 👤 人工·人类)各自分页展示。每页条数取插件设置 ui.allArticles.pageSize(默认 10)。', core: false },
|
|
179
|
+
{ presentTitle: ALL_ARTICLES_TITLE, write: seedAllArticles, unseed: unseedAllArticles },
|
|
180
|
+
),
|
|
181
|
+
defineSeed(
|
|
182
|
+
{ id: 'menubar-theme', title: 'menubar 顶栏主题自适应', description: '样式表覆盖($:/plugins/dsh-tiddlywiki/menubar-theme,tag $:/tags/Stylesheet)——把 tiddlywiki/menubar 顶栏从「默认色映射的蓝色」改为跟随当前 palette 的 background/foreground,随 DSH 主题切换($:/palette 翻转)自动换色。', core: false },
|
|
183
|
+
{ presentTitle: MENUBAR_THEME_TIDDLER, write: seedMenubarTheme, unseed: unseedMenubarTheme },
|
|
184
|
+
),
|
|
185
|
+
defineSeed(
|
|
186
|
+
{ id: 'tw-web-host', title: 'TW 前端 API 基址(同源代理)', description: '把 $:/config/tiddlyweb/host 指向 DSH 同源代理,嵌入式 TW 才能经 DSH origin 访问(远程访问模式的前提)。', core: true },
|
|
187
|
+
{
|
|
188
|
+
check: async (ctx) => {
|
|
189
|
+
let current: string | undefined
|
|
190
|
+
try {
|
|
191
|
+
current = (await ctx.client.get(TW_WEB_HOST_TIDDLER))?.text?.trim()
|
|
192
|
+
} catch {
|
|
193
|
+
current = undefined
|
|
194
|
+
}
|
|
195
|
+
const ok = current === TW_PROXY_PATH
|
|
196
|
+
return { id: 'tw-web-host', title: 'TW 前端 API 基址(同源代理)', description: '把 $:/config/tiddlyweb/host 指向 DSH 同源代理,嵌入式 TW 才能经 DSH origin 访问(远程访问模式的前提)。', present: ok, removable: false, detail: ok ? `已指向 ${TW_PROXY_PATH}` : `当前:${current ?? '(缺失)'},应为 ${TW_PROXY_PATH}` }
|
|
197
|
+
},
|
|
198
|
+
run: async (ctx, force) => {
|
|
199
|
+
try {
|
|
200
|
+
let current: string | undefined
|
|
201
|
+
try {
|
|
202
|
+
current = (await ctx.client.get(TW_WEB_HOST_TIDDLER))?.text?.trim()
|
|
203
|
+
} catch {
|
|
204
|
+
current = undefined
|
|
205
|
+
}
|
|
206
|
+
// Non-force keeps the ensure semantics: write only when missing or still
|
|
207
|
+
// the legacy default (a user override pointing elsewhere is honored).
|
|
208
|
+
if (!force && current !== undefined && current !== TW_WEB_HOST_DEFAULT) {
|
|
209
|
+
return { id: 'tw-web-host', ok: true, wrote: false, detail: '已指向自定义基址,未覆盖' }
|
|
210
|
+
}
|
|
211
|
+
await ctx.client.put({ title: TW_WEB_HOST_TIDDLER, text: TW_PROXY_PATH, type: 'text/plain', tags: [] })
|
|
212
|
+
return { id: 'tw-web-host', ok: true, wrote: true, detail: force ? '已重新初始化(强制写回代理基址)' : '已写入代理基址' }
|
|
213
|
+
} catch (err) {
|
|
214
|
+
return { id: 'tw-web-host', ok: false, wrote: false, error: err instanceof Error ? err.message : String(err) }
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
),
|
|
219
|
+
]
|
|
220
|
+
|
|
221
|
+
/** Check every seed, returning statuses in registry order. */
|
|
222
|
+
export async function checkAllSeeds(ctx: SeedContext): Promise<SeedStatus[]> {
|
|
223
|
+
const out: SeedStatus[] = []
|
|
224
|
+
for (const def of SEED_DEFS) {
|
|
225
|
+
try {
|
|
226
|
+
out.push(await def.check(ctx))
|
|
227
|
+
} catch (err) {
|
|
228
|
+
out.push({
|
|
229
|
+
id: def.id,
|
|
230
|
+
title: def.title,
|
|
231
|
+
description: def.description,
|
|
232
|
+
present: false,
|
|
233
|
+
removable: !def.core,
|
|
234
|
+
detail: `检查失败:${err instanceof Error ? err.message : String(err)}`,
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return out
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Run one seed (or all, when id is undefined). Non-force = one-shot semantics;
|
|
243
|
+
* force = manual "重新初始化" from the settings page. A manual run with no id
|
|
244
|
+
* (重新初始化 all) covers every registry item, core and optional alike.
|
|
245
|
+
*/
|
|
246
|
+
export async function runSeedById(ctx: SeedContext, id: string | undefined, force: boolean): Promise<SeedRunResult[]> {
|
|
247
|
+
const targets = id === undefined ? SEED_DEFS : SEED_DEFS.filter((d) => d.id === id)
|
|
248
|
+
if (targets.length === 0) {
|
|
249
|
+
return [{ id: id ?? '', ok: false, wrote: false, error: `unknown seed: ${id}` }]
|
|
250
|
+
}
|
|
251
|
+
const out: SeedRunResult[] = []
|
|
252
|
+
for (const def of targets) {
|
|
253
|
+
out.push(await def.run(ctx, force))
|
|
254
|
+
}
|
|
255
|
+
return out
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Startup path: seed ONLY the core items (功能必需:发送给 Agent 按钮 +
|
|
260
|
+
* TW 前端 API 基址), non-force (write only what is missing). Optional seeds
|
|
261
|
+
* (说明笔记 / 首页 / 所有文章 / menubar 顶栏主题自适应) are never forced on
|
|
262
|
+
* users — they opt in from the settings page「初始化」section.
|
|
263
|
+
*/
|
|
264
|
+
export async function runAllSeeds(ctx: SeedContext): Promise<SeedRunResult[]> {
|
|
265
|
+
const out: SeedRunResult[] = []
|
|
266
|
+
for (const def of SEED_DEFS) {
|
|
267
|
+
if (!def.core) continue
|
|
268
|
+
out.push(await def.run(ctx, false))
|
|
269
|
+
}
|
|
270
|
+
return out
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* 反初始化 (remove): delete one optional seed's seeded tiddlers + markers, or
|
|
275
|
+
* all optional seeds when `id` is undefined. Core seeds are functionally
|
|
276
|
+
* required and cannot be removed — a direct request for one returns an error
|
|
277
|
+
* result (and is skipped when removing all).
|
|
278
|
+
*/
|
|
279
|
+
export async function removeSeedById(ctx: SeedContext, id: string | undefined): Promise<SeedRunResult[]> {
|
|
280
|
+
if (id === undefined) {
|
|
281
|
+
const out: SeedRunResult[] = []
|
|
282
|
+
for (const def of SEED_DEFS) {
|
|
283
|
+
if (def.core || def.remove === undefined) continue
|
|
284
|
+
out.push(await def.remove(ctx))
|
|
285
|
+
}
|
|
286
|
+
return out
|
|
287
|
+
}
|
|
288
|
+
const def = SEED_DEFS.find((d) => d.id === id)
|
|
289
|
+
if (def === undefined) {
|
|
290
|
+
return [{ id, ok: false, wrote: false, error: `unknown seed: ${id}` }]
|
|
291
|
+
}
|
|
292
|
+
if (def.core) {
|
|
293
|
+
return [{ id, ok: false, wrote: false, error: '该 seed 为核心项(功能必需),不可反初始化' }]
|
|
294
|
+
}
|
|
295
|
+
if (def.remove === undefined) {
|
|
296
|
+
return [{ id, ok: false, wrote: false, error: '该 seed 不支持移除' }]
|
|
297
|
+
}
|
|
298
|
+
return [await def.remove(ctx)]
|
|
299
|
+
}
|