@jonathangu/openclawbrain 0.3.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.
Files changed (113) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +412 -0
  3. package/bin/openclawbrain.js +15 -0
  4. package/docs/END_STATE.md +244 -0
  5. package/docs/EVIDENCE.md +128 -0
  6. package/docs/RELEASE_CONTRACT.md +91 -0
  7. package/docs/agent-tools.md +106 -0
  8. package/docs/architecture.md +224 -0
  9. package/docs/configuration.md +178 -0
  10. package/docs/evidence/2026-03-16/3188b50c4ed30f07dea111e35ce52aabefaced63/brain-teach-session-bound/status.json +87 -0
  11. package/docs/evidence/2026-03-16/3188b50c4ed30f07dea111e35ce52aabefaced63/brain-teach-session-bound/summary.md +16 -0
  12. package/docs/evidence/2026-03-16/3188b50c4ed30f07dea111e35ce52aabefaced63/brain-teach-session-bound/trace.json +273 -0
  13. package/docs/evidence/2026-03-16/3188b50c4ed30f07dea111e35ce52aabefaced63/brain-teach-session-bound/validation-report.json +652 -0
  14. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/channels-status.txt +31 -0
  15. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/config-snapshot.json +66 -0
  16. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/doctor.json +14 -0
  17. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/gateway-probe.txt +34 -0
  18. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/gateway-status.txt +41 -0
  19. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/logs.txt +428 -0
  20. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/status-all.txt +60 -0
  21. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/status.json +223 -0
  22. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/summary.md +13 -0
  23. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/trace.json +4 -0
  24. package/docs/evidence/2026-03-16/4941429588810da5d6f7ef1509f229f83fa08031/validation-report.json +334 -0
  25. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/channels-status.txt +25 -0
  26. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/config-snapshot.json +91 -0
  27. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/doctor.json +14 -0
  28. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/gateway-probe.txt +36 -0
  29. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/gateway-status.txt +44 -0
  30. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/logs.txt +428 -0
  31. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/short-static-classification/preflight-doctor.json +10 -0
  32. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/short-static-classification/preflight-sdk-probe.json +11 -0
  33. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/short-static-classification/preflight-setup-only.json +12 -0
  34. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/short-static-classification/summary.md +30 -0
  35. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/short-static-classification/validation-report.json +72 -0
  36. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/status-all.txt +63 -0
  37. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/status.json +200 -0
  38. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/summary.md +13 -0
  39. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/trace.json +4 -0
  40. package/docs/evidence/2026-03-16/7f8dbcb27e741abdeefd5656c210639d0acdd440/validation-report.json +311 -0
  41. package/docs/evidence/README.md +16 -0
  42. package/docs/fts5.md +161 -0
  43. package/docs/tui.md +506 -0
  44. package/index.ts +1372 -0
  45. package/openclaw.plugin.json +136 -0
  46. package/package.json +66 -0
  47. package/src/assembler.ts +804 -0
  48. package/src/brain-cli.ts +316 -0
  49. package/src/brain-core/decay.ts +35 -0
  50. package/src/brain-core/episode.ts +82 -0
  51. package/src/brain-core/graph.ts +321 -0
  52. package/src/brain-core/health.ts +116 -0
  53. package/src/brain-core/mutator.ts +281 -0
  54. package/src/brain-core/pack.ts +117 -0
  55. package/src/brain-core/policy.ts +153 -0
  56. package/src/brain-core/replay.ts +1 -0
  57. package/src/brain-core/teacher.ts +105 -0
  58. package/src/brain-core/trace.ts +40 -0
  59. package/src/brain-core/traverse.ts +230 -0
  60. package/src/brain-core/types.ts +405 -0
  61. package/src/brain-core/update.ts +123 -0
  62. package/src/brain-harvest/human.ts +46 -0
  63. package/src/brain-harvest/scanner.ts +98 -0
  64. package/src/brain-harvest/self.ts +147 -0
  65. package/src/brain-runtime/assembler-extension.ts +230 -0
  66. package/src/brain-runtime/evidence-detectors.ts +68 -0
  67. package/src/brain-runtime/graph-io.ts +72 -0
  68. package/src/brain-runtime/harvester-extension.ts +98 -0
  69. package/src/brain-runtime/service.ts +659 -0
  70. package/src/brain-runtime/tools.ts +109 -0
  71. package/src/brain-runtime/worker-state.ts +106 -0
  72. package/src/brain-runtime/worker-supervisor.ts +169 -0
  73. package/src/brain-store/embedding.ts +179 -0
  74. package/src/brain-store/init.ts +347 -0
  75. package/src/brain-store/migrations.ts +188 -0
  76. package/src/brain-store/store.ts +816 -0
  77. package/src/brain-worker/child-runner.ts +321 -0
  78. package/src/brain-worker/jobs.ts +12 -0
  79. package/src/brain-worker/mutation-job.ts +5 -0
  80. package/src/brain-worker/promotion-job.ts +5 -0
  81. package/src/brain-worker/protocol.ts +79 -0
  82. package/src/brain-worker/teacher-job.ts +5 -0
  83. package/src/brain-worker/update-job.ts +5 -0
  84. package/src/brain-worker/worker.ts +422 -0
  85. package/src/compaction.ts +1332 -0
  86. package/src/db/config.ts +265 -0
  87. package/src/db/connection.ts +72 -0
  88. package/src/db/features.ts +42 -0
  89. package/src/db/migration.ts +561 -0
  90. package/src/engine.ts +1995 -0
  91. package/src/expansion-auth.ts +351 -0
  92. package/src/expansion-policy.ts +303 -0
  93. package/src/expansion.ts +383 -0
  94. package/src/integrity.ts +600 -0
  95. package/src/large-files.ts +527 -0
  96. package/src/openclaw-bridge.ts +22 -0
  97. package/src/retrieval.ts +357 -0
  98. package/src/store/conversation-store.ts +748 -0
  99. package/src/store/fts5-sanitize.ts +29 -0
  100. package/src/store/full-text-fallback.ts +74 -0
  101. package/src/store/index.ts +29 -0
  102. package/src/store/summary-store.ts +918 -0
  103. package/src/summarize.ts +847 -0
  104. package/src/tools/common.ts +53 -0
  105. package/src/tools/lcm-conversation-scope.ts +76 -0
  106. package/src/tools/lcm-describe-tool.ts +234 -0
  107. package/src/tools/lcm-expand-query-tool.ts +594 -0
  108. package/src/tools/lcm-expand-tool.delegation.ts +556 -0
  109. package/src/tools/lcm-expand-tool.ts +448 -0
  110. package/src/tools/lcm-expansion-recursion-guard.ts +286 -0
  111. package/src/tools/lcm-grep-tool.ts +200 -0
  112. package/src/transcript-repair.ts +301 -0
  113. package/src/types.ts +149 -0
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Sanitize a user-provided query for use in an FTS5 MATCH expression.
3
+ *
4
+ * FTS5 treats certain characters as operators:
5
+ * - `-` (NOT), `+` (required), `*` (prefix), `^` (initial token)
6
+ * - `OR`, `AND`, `NOT` (boolean operators)
7
+ * - `:` (column filter — e.g. `agent:foo` means "search column agent")
8
+ * - `"` (phrase query), `(` `)` (grouping)
9
+ * - `NEAR` (proximity)
10
+ *
11
+ * If the query contains any of these, naive MATCH will either error
12
+ * ("no such column") or return unexpected results.
13
+ *
14
+ * Strategy: wrap each whitespace-delimited token in double quotes so FTS5
15
+ * treats it as a literal phrase token. Internal double quotes are stripped.
16
+ * Empty tokens are dropped. Tokens are joined with spaces (implicit AND).
17
+ *
18
+ * Examples:
19
+ * "sub-agent restrict" → '"sub-agent" "restrict"'
20
+ * "lcm_expand OR crash" → '"lcm_expand" "OR" "crash"'
21
+ * 'hello "world"' → '"hello" "world"'
22
+ */
23
+ export function sanitizeFts5Query(raw: string): string {
24
+ const tokens = raw.split(/\s+/).filter(Boolean);
25
+ if (tokens.length === 0) {
26
+ return '""';
27
+ }
28
+ return tokens.map((t) => `"${t.replace(/"/g, "")}"`).join(" ");
29
+ }
@@ -0,0 +1,74 @@
1
+ const RAW_TERM_RE = /"([^"]+)"|(\S+)/g;
2
+ const EDGE_PUNCTUATION_RE = /^[`'"()[\]{}<>.,:;!?*_+=|\\/-]+|[`'"()[\]{}<>.,:;!?*_+=|\\/-]+$/g;
3
+
4
+ export type LikeSearchPlan = {
5
+ terms: string[];
6
+ where: string[];
7
+ args: string[];
8
+ };
9
+
10
+ function normalizeFallbackTerm(raw: string): string {
11
+ return raw.trim().replace(EDGE_PUNCTUATION_RE, "").toLowerCase();
12
+ }
13
+
14
+ function escapeLike(term: string): string {
15
+ return term.replace(/([\\%_])/g, "\\$1");
16
+ }
17
+
18
+ /**
19
+ * Convert a free-text query into a conservative LIKE search plan.
20
+ *
21
+ * The fallback keeps phrase tokens when the query uses double quotes, and
22
+ * otherwise searches for all normalized tokens as case-insensitive substrings.
23
+ */
24
+ export function buildLikeSearchPlan(column: string, query: string): LikeSearchPlan {
25
+ const terms: string[] = [];
26
+ for (const match of query.matchAll(RAW_TERM_RE)) {
27
+ const raw = match[1] ?? match[2] ?? "";
28
+ const normalized = normalizeFallbackTerm(raw);
29
+ if (normalized.length > 0 && !terms.includes(normalized)) {
30
+ terms.push(normalized);
31
+ }
32
+ }
33
+
34
+ if (terms.length === 0) {
35
+ const fallback = normalizeFallbackTerm(query);
36
+ if (fallback.length > 0) {
37
+ terms.push(fallback);
38
+ }
39
+ }
40
+
41
+ return {
42
+ terms,
43
+ where: terms.map(() => `LOWER(${column}) LIKE ? ESCAPE '\\'`),
44
+ args: terms.map((term) => `%${escapeLike(term)}%`),
45
+ };
46
+ }
47
+
48
+ /**
49
+ * Build a compact snippet centered around the earliest matching term.
50
+ */
51
+ export function createFallbackSnippet(content: string, terms: string[]): string {
52
+ const haystack = content.toLowerCase();
53
+ let matchIndex = -1;
54
+ let matchLength = 0;
55
+
56
+ for (const term of terms) {
57
+ const idx = haystack.indexOf(term);
58
+ if (idx !== -1 && (matchIndex === -1 || idx < matchIndex)) {
59
+ matchIndex = idx;
60
+ matchLength = term.length;
61
+ }
62
+ }
63
+
64
+ if (matchIndex === -1) {
65
+ const head = content.trim();
66
+ return head.length <= 80 ? head : `${head.slice(0, 77).trimEnd()}...`;
67
+ }
68
+
69
+ const start = Math.max(0, matchIndex - 24);
70
+ const end = Math.min(content.length, matchIndex + Math.max(matchLength, 1) + 40);
71
+ const prefix = start > 0 ? "..." : "";
72
+ const suffix = end < content.length ? "..." : "";
73
+ return `${prefix}${content.slice(start, end).trim()}${suffix}`;
74
+ }
@@ -0,0 +1,29 @@
1
+ export { ConversationStore } from "./conversation-store.js";
2
+ export type {
3
+ ConversationId,
4
+ MessageId,
5
+ SummaryId,
6
+ MessageRole,
7
+ MessagePartType,
8
+ MessageRecord,
9
+ MessagePartRecord,
10
+ ConversationRecord,
11
+ CreateMessageInput,
12
+ CreateMessagePartInput,
13
+ CreateConversationInput,
14
+ MessageSearchInput,
15
+ MessageSearchResult,
16
+ } from "./conversation-store.js";
17
+
18
+ export { SummaryStore } from "./summary-store.js";
19
+ export type {
20
+ SummaryKind,
21
+ ContextItemType,
22
+ CreateSummaryInput,
23
+ SummaryRecord,
24
+ ContextItemRecord,
25
+ SummarySearchInput,
26
+ SummarySearchResult,
27
+ CreateLargeFileInput,
28
+ LargeFileRecord,
29
+ } from "./summary-store.js";