dsh-tiddlywiki 0.34.0 → 0.34.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-tiddlywiki",
3
3
  "description": "把 TiddlyWiki 5 变成你和 Agent 共用的本地文档中心(纯文本 + git):你在 DSH 界面里阅读、写作、记日记,Agent 用 17 个 tiddlywiki_* 工具读写同一批笔记;每条笔记都是你能读、能改、能审计、能随时带走的文件——不是自动记忆插件,不做后台抓取。Turn TiddlyWiki 5 into a document center you and your agent share, in plain text under git — a knowledge base you can read, edit, audit and take with you, not an auto-memory plugin. Mounts via the official dsh plugin system; no DSH source changes.",
4
- "version": "0.34.0",
4
+ "version": "0.34.1",
5
5
  "type": "module",
6
6
  "keywords": [
7
7
  "dsh",
@@ -47,9 +47,9 @@ export function searchTool(env: ToolEnv) {
47
47
  // ── tiddlywiki_search ────────────────────────────────────────────────────
48
48
  return defineTool({
49
49
  name: 'tiddlywiki_search',
50
- description: '检索 TiddlyWiki 持久知识库:按关键词(可选 tags 数组 / since 修改时间 / type / field+value / limit)搜索非系统 tiddler,按相关度排序返回标题、标签、修改时间与命中处上下文片段。二进制 tiddler(图片等附件,正文为 base64)不参与检索。查询按空白切词、**所有词都必须命中**(AND)。若当前会话属于某个工作区,会**先在该工作区内检索、命中为空才自动扩大到全库**,回执会写明用了哪个范围(传了 tags / tag / field / value 时视为调用方已给出明确范围,不再自动收窄)。',
50
+ description: '检索 TiddlyWiki 持久知识库:按关键词(可选 tags 数组 / since 修改时间 / type / field+value / limit)搜索非系统 tiddler,按相关度排序返回标题、标签、修改时间与命中处上下文片段。**query 可省略**——只传 tags/tag/field/since/type 时就是「按条件列出」(v0.34.1),此时按修改时间倒序。二进制 tiddler(图片等附件,正文为 base64)不参与检索。查询按空白切词、**所有词都必须命中**(AND)。若当前会话属于某个工作区,会**先在该工作区内检索、命中为空才自动扩大到全库**,回执会写明用了哪个范围(传了 tags / tag / field / value 时视为调用方已给出明确范围,不再自动收窄)。',
51
51
  parameters: {
52
- query: { type: 'string', description: '搜索关键词(大小写不敏感,子串匹配;命中标题/标签/正文,标题命中权重最高)。多词按空白切分,**全部词都要命中**才算(AND)——例如「部署 步骤」只返回同时含这两词的笔记' },
52
+ query: { type: 'string', description: '搜索关键词(大小写不敏感,子串匹配;命中标题/标签/正文,标题命中权重最高)。多词按空白切分,**全部词都要命中**才算(AND)——例如「部署 步骤」只返回同时含这两词的笔记。⚠️ 省略 query 且给了 tags/tag/field/since/type 时=按这些条件列出(不是空结果)' },
53
53
  tags: { type: 'array', items: { type: 'string' }, description: '可选:要求同时包含的标签(AND)' },
54
54
  tag: { type: 'string', description: '可选:单个精确标签(与 tags 同为 AND)' },
55
55
  since: { type: 'string', description: '可选:ISO 时间(如 2026-09-01 或 2026-09-01T00:00:00Z),只返回修改时间不早于它的 tiddler' },
@@ -75,7 +75,12 @@ export function searchTool(env: ToolEnv) {
75
75
  } else if (value.workspace !== null && value.fellBack) {
76
76
  scope = ` · 工作区 ${WORKSPACE_TAG_PREFIX}${value.workspace} 内 0 条,已扩大到全库`
77
77
  }
78
- const head = `TiddlyWiki 搜索「${value.query}」${filters.length > 0 ? ` (${filters.join(' · ')})` : ''}:命中 ${value.total} 条${scope}(按相关度排序)。`
78
+ // v0.34.1:query 可为空(纯标签/字段列出)。用 ?? '' 兜底,免得渲染路径
79
+ // 因为「回执里没有关键词」而崩掉整张工具卡。
80
+ const query = value.query ?? ''
81
+ const head = query.length > 0
82
+ ? `TiddlyWiki 搜索「${query}」${filters.length > 0 ? ` (${filters.join(' · ')})` : ''}:命中 ${value.total} 条${scope}(按相关度排序)。`
83
+ : `TiddlyWiki 按条件列出${filters.length > 0 ? ` (${filters.join(' · ')})` : ''}:命中 ${value.total} 条${scope}(按修改时间倒序)。`
79
84
  const lines = [head]
80
85
  if (value.results.length === 0) lines.push('没有匹配的 tiddler。')
81
86
  // `limit` 管"查多少条",这里管"回执多大"(见 RENDER_TEXT_BUDGET_CHARS)。
@@ -100,8 +105,12 @@ export function searchTool(env: ToolEnv) {
100
105
  return [{ type: 'text', text: lines.join('\n') }]
101
106
  },
102
107
  },
103
- execute: async (args: { query: string; tags?: string[]; tag?: string; since?: string; type?: string; field?: string; value?: string; limit?: number }, exec: unknown): Promise<SearchResult> => {
108
+ execute: async (args: { query?: string; tags?: string[]; tag?: string; since?: string; type?: string; field?: string; value?: string; limit?: number }, exec: unknown): Promise<SearchResult> => {
104
109
  const wiki = requireWiki(sessionIdOf(exec))
110
+ // v0.34.1(issue #1):`query` 在 schema 里是可选的,纯标签筛选时调用方不传它。
111
+ // 这里归一成 '',下游(search / snippetAround / 回执)就不用各自判空 ——
112
+ // 尤其是回执里塞 undefined 会让 DSH 的 lossless-JSON 校验整次判 invalid。
113
+ const query = args.query ?? ''
105
114
  const options = {
106
115
  tags: args.tags,
107
116
  tag: args.tag,
@@ -120,12 +129,12 @@ export function searchTool(env: ToolEnv) {
120
129
  const mark = explicitScope ? undefined : workspaceMarkFor(deps, exec)
121
130
  let narrowed: { items: Tiddler[]; total: number } | undefined
122
131
  if (mark !== undefined) {
123
- narrowed = await wiki.search(args.query, { ...options, tags: [mark.tag] })
132
+ narrowed = await wiki.search(query, { ...options, tags: [mark.tag] })
124
133
  }
125
134
  const usedWorkspace = narrowed !== undefined && narrowed.total > 0
126
- const { items, total } = usedWorkspace ? narrowed as { items: Tiddler[]; total: number } : await wiki.search(args.query, options)
135
+ const { items, total } = usedWorkspace ? narrowed as { items: Tiddler[]; total: number } : await wiki.search(query, options)
127
136
  return {
128
- query: args.query,
137
+ query,
129
138
  tags: args.tags ?? [],
130
139
  since: args.since ?? null,
131
140
  type: args.type ?? null,
@@ -135,7 +144,7 @@ export function searchTool(env: ToolEnv) {
135
144
  workspace: mark?.id ?? null,
136
145
  scope: usedWorkspace ? 'workspace' : 'all',
137
146
  fellBack: mark !== undefined && !usedWorkspace,
138
- results: items.map((t) => ({ title: t.title, tags: t.tags ?? [], modified: toIsoDateString(t.modified), snippet: snippetAround(t.text ?? '', args.query) })),
147
+ results: items.map((t) => ({ title: t.title, tags: t.tags ?? [], modified: toIsoDateString(t.modified), snippet: snippetAround(t.text ?? '', query) })),
139
148
  }
140
149
  },
141
150
  })
@@ -351,7 +351,7 @@ export function withWorkspaceMark(
351
351
  * literally. Fall back to the earliest individual term before giving up, or a
352
352
  * perfectly good hit would still render the head of the note.
353
353
  */
354
- export function snippetAround(text: string, query: string, max = 160): string {
354
+ export function snippetAround(text: string, query = '', max = 160): string {
355
355
  const flat = text.replace(/\s+/g, ' ').trim()
356
356
  if (flat.length <= max) return flat
357
357
  const lower = flat.toLowerCase()
@@ -467,18 +467,37 @@ export class TiddlyWebClient {
467
467
  * `书籍 索引` (with the space) and returned 0 hits while each word matched
468
468
  * dozens — a query shape every model produces naturally, and one that reads
469
469
  * as "the knowledge base is empty" rather than as a syntax error.
470
+ *
471
+ * v0.34.1 — TAGS-ONLY (issue #1): `query` is OPTIONAL in the tool schema, but
472
+ * this signature said `string` and the body called `query.split(…)` — so
473
+ * `tiddlywiki_search({ tags: ['x'] })` died with
474
+ * `Cannot read properties of undefined (reading 'split')`. Worse, even with
475
+ * the crash gone, the empty-query short-circuit sat ABOVE `wantedTags`, so a
476
+ * "list by tag" call could never reach the tag filter and returned 0 rows.
477
+ * The rule now is: an empty query matches nothing **only when it is the only
478
+ * filter** — with tags / since / type / field present, "list by condition" is
479
+ * the whole point (and `for (const term of terms)` simply leaves allTerms
480
+ * true, so ranking falls back to newest-first).
470
481
  */
471
- async search(query: string, options: SearchOptions = {}): Promise<{ items: Tiddler[]; total: number }> {
482
+ async search(query: string | undefined, options: SearchOptions = {}): Promise<{ items: Tiddler[]; total: number }> {
472
483
  const items = await this.list(undefined, true)
473
- const terms = query.split(/\s+/).map((s) => s.trim().toLowerCase()).filter((s) => s.length > 0)
474
- // An empty query matched nothing before (every hit flag was gated on a
475
- // non-empty needle) — keep that, rather than "match everything".
476
- if (terms.length === 0) return { items: [], total: 0 }
484
+ const terms = (query ?? '').split(/\s+/).map((s) => s.trim().toLowerCase()).filter((s) => s.length > 0)
477
485
  const sinceTime = parseSince(options.since)
478
486
  const wantedTags = [...(options.tags ?? []), options.tag].filter((t): t is string => typeof t === 'string' && t.trim().length > 0)
479
487
  const limit = clampLimit(options.limit, 30)
488
+ const typeFilter = typeof options.type === 'string' && options.type.length > 0 ? options.type : undefined
480
489
  const fieldName = typeof options.field === 'string' && options.field.length > 0 ? options.field : undefined
481
490
  const fieldValue = typeof options.value === 'string' ? options.value : undefined
491
+ // An empty query matched nothing before (every hit flag was gated on a
492
+ // non-empty needle) — keep that, rather than "match everything" — but only
493
+ // while there is no other condition the caller asked for.
494
+ if (
495
+ terms.length === 0
496
+ && wantedTags.length === 0
497
+ && sinceTime === undefined
498
+ && typeFilter === undefined
499
+ && fieldName === undefined
500
+ ) return { items: [], total: 0 }
482
501
  const matched: Array<{ t: Tiddler; score: number }> = []
483
502
  for (const t of items) {
484
503
  if (t.title.startsWith('$:/')) continue
@@ -501,7 +520,7 @@ export class TiddlyWebClient {
501
520
  const modified = parseTiddlerDate(t.modified)
502
521
  if (modified === undefined || modified < sinceTime) continue
503
522
  }
504
- if (options.type !== undefined && options.type.length > 0 && (t.type ?? 'text/vnd.tiddlywiki') !== options.type) continue
523
+ if (typeFilter !== undefined && (t.type ?? 'text/vnd.tiddlywiki') !== typeFilter) continue
505
524
  if (wantedTags.length > 0) {
506
525
  if (!wantedTags.every((w) => lowerTags.includes(w.toLowerCase()))) continue
507
526
  }