@cachly-dev/mcp-server 0.9.24 → 0.10.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/.github/workflows/publish.yml +42 -0
- package/LICENSE +202 -0
- package/dist/handlers/brain.js +93 -33
- package/dist/handlers/brain.js.map +1 -1
- package/dist/handlers/fedbrain.js +55 -38
- package/dist/handlers/fedbrain.js.map +1 -1
- package/dist/handlers/team.js +62 -50
- package/dist/handlers/team.js.map +1 -1
- package/dist/index.js +147 -28
- package/dist/index.js.map +1 -1
- package/dist/tools.js +5 -4
- package/dist/tools.js.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +13 -0
- package/dist/utils.js.map +1 -0
- package/glama.json +6 -0
- package/package.json +1 -1
- package/src/handlers/brain.ts +89 -27
- package/src/handlers/fedbrain.ts +43 -31
- package/src/handlers/team.ts +57 -33
- package/src/index.ts +150 -31
- package/src/tools.ts +5 -4
- package/src/utils.ts +10 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
|
|
14
|
+
- uses: actions/setup-node@v4
|
|
15
|
+
with:
|
|
16
|
+
node-version: '20'
|
|
17
|
+
registry-url: 'https://registry.npmjs.org'
|
|
18
|
+
|
|
19
|
+
- name: Install dependencies
|
|
20
|
+
run: npm install --ignore-scripts
|
|
21
|
+
|
|
22
|
+
- name: Build
|
|
23
|
+
run: npm run build
|
|
24
|
+
|
|
25
|
+
- name: Check if version is already published
|
|
26
|
+
id: version_check
|
|
27
|
+
run: |
|
|
28
|
+
PKG_VERSION=$(node -p "require('./package.json').version")
|
|
29
|
+
echo "version=$PKG_VERSION" >> $GITHUB_OUTPUT
|
|
30
|
+
if npm view "@cachly-dev/mcp-server@$PKG_VERSION" version &>/dev/null; then
|
|
31
|
+
echo "skip=true" >> $GITHUB_OUTPUT
|
|
32
|
+
echo "Version $PKG_VERSION already published — skipping."
|
|
33
|
+
else
|
|
34
|
+
echo "skip=false" >> $GITHUB_OUTPUT
|
|
35
|
+
echo "Version $PKG_VERSION is new — will publish."
|
|
36
|
+
fi
|
|
37
|
+
|
|
38
|
+
- name: Publish
|
|
39
|
+
if: steps.version_check.outputs.skip == 'false'
|
|
40
|
+
run: npm publish --access public
|
|
41
|
+
env:
|
|
42
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/dist/handlers/brain.js
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import { execSync } from 'node:child_process';
|
|
2
2
|
import { calculateConfidence, confidenceBadge, STRUCTURED_TEMPLATES, CONFIDENCE_WARN_VALUE, CONFIDENCE_STALE_VALUE, CONFIDENCE_WARN_DAYS } from '../confidence.js';
|
|
3
3
|
import { ckgSlug, extractProblemConcept, ckgUpsertNode, ckgUpdateEdge } from '../ckg.js';
|
|
4
|
+
import { safeJsonParse } from '../utils.js';
|
|
4
5
|
import { keywordSearch, tokenize, splitMultiQuery, levenshtein, indexVocab as _indexVocab } from '../search.js';
|
|
5
6
|
import { computeEmbedding, hasEmbedProvider } from '../embeddings.js';
|
|
7
|
+
// ── Changelog (shown once per version in session_start) ──────────────────────
|
|
8
|
+
const MCP_VERSION = '0.10.0';
|
|
9
|
+
const WHATS_NEW = {
|
|
10
|
+
'0.10.0': [
|
|
11
|
+
`🆕 **What's new in v${MCP_VERSION}:**`,
|
|
12
|
+
` ✅ Zero-friction setup — auto-provisions your Brain, no manual config needed`,
|
|
13
|
+
` ✅ 10× faster briefings — batch Redis fetches replace per-key loops`,
|
|
14
|
+
` ✅ Crash-proof data reads — corrupted cache entries no longer break tools`,
|
|
15
|
+
` ✅ \`session_end\` warns when called without an active session`,
|
|
16
|
+
` ✅ \`get_api_status\` now shows live Redis ping + all instance statuses`,
|
|
17
|
+
],
|
|
18
|
+
};
|
|
6
19
|
export const BRAIN_TOOL_NAMES = new Set([
|
|
7
20
|
'learn_from_attempts', 'recall_best_solution', 'smart_recall',
|
|
8
21
|
'session_start', 'session_end', 'session_ping', 'session_handoff', 'auto_learn_session',
|
|
@@ -98,7 +111,7 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
98
111
|
for (const dep of depends_on) {
|
|
99
112
|
const depKey = `cachly:dep:${dep}`;
|
|
100
113
|
const existing = await redis.get(depKey);
|
|
101
|
-
const depTopics =
|
|
114
|
+
const depTopics = safeJsonParse(existing, []);
|
|
102
115
|
if (!depTopics.includes(topic))
|
|
103
116
|
depTopics.push(topic);
|
|
104
117
|
await redis.set(depKey, JSON.stringify(depTopics));
|
|
@@ -190,7 +203,9 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
190
203
|
const er = await redis.get(ek);
|
|
191
204
|
if (!er)
|
|
192
205
|
continue;
|
|
193
|
-
const existEdge =
|
|
206
|
+
const existEdge = safeJsonParse(er, null);
|
|
207
|
+
if (!existEdge)
|
|
208
|
+
continue;
|
|
194
209
|
if (existEdge.edgeType === 'fixes' && existEdge.confidence > 0.7 && existEdge.trials >= 3) {
|
|
195
210
|
beliefConflict = `⚠️ **belief_conflict** on \`${topic}\`: previously confirmed fix (confidence ${existEdge.confidence.toFixed(2)}, n=${existEdge.trials}) now reports failure. Both beliefs retained as \`contested\`. Use \`ckg_inspect(concept="${conceptId}")\` to review.`;
|
|
196
211
|
// Store conflict marker
|
|
@@ -210,7 +225,9 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
210
225
|
const er = await redis.get(ek);
|
|
211
226
|
if (!er)
|
|
212
227
|
continue;
|
|
213
|
-
const e2 =
|
|
228
|
+
const e2 = safeJsonParse(er, null);
|
|
229
|
+
if (!e2)
|
|
230
|
+
continue;
|
|
214
231
|
if (e2.edgeType !== 'fixes')
|
|
215
232
|
continue;
|
|
216
233
|
// Boost second-degree: edges from e2.to get a small fractional success
|
|
@@ -219,7 +236,9 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
219
236
|
const sr = await redis.get(sk);
|
|
220
237
|
if (!sr)
|
|
221
238
|
continue;
|
|
222
|
-
const se =
|
|
239
|
+
const se = safeJsonParse(sr, null);
|
|
240
|
+
if (!se)
|
|
241
|
+
continue;
|
|
223
242
|
if (se.edgeType !== 'co-occurs')
|
|
224
243
|
continue;
|
|
225
244
|
// Add 0.1 fractional success (second-degree signal)
|
|
@@ -241,7 +260,9 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
241
260
|
const er = await redis.get(ek);
|
|
242
261
|
if (!er)
|
|
243
262
|
continue;
|
|
244
|
-
const de =
|
|
263
|
+
const de = safeJsonParse(er, null);
|
|
264
|
+
if (!de)
|
|
265
|
+
continue;
|
|
245
266
|
if (de.trials < 3 && de.last_updated && new Date(de.last_updated).getTime() < decayCutoff) {
|
|
246
267
|
de.confidence = de.confidence * 0.9;
|
|
247
268
|
de.last_updated = new Date().toISOString();
|
|
@@ -281,7 +302,9 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
281
302
|
// Try exact best-solution key first
|
|
282
303
|
const best = await redis.get(`cachly:lesson:best:${topic}`);
|
|
283
304
|
if (best) {
|
|
284
|
-
const lesson =
|
|
305
|
+
const lesson = safeJsonParse(best, null);
|
|
306
|
+
if (!lesson)
|
|
307
|
+
return `⚠️ Lesson data for \`${topic}\` is corrupted. Re-store it with \`learn_from_attempts\`.`;
|
|
285
308
|
// ── Confidence decay check ───────────────────────────────────────────
|
|
286
309
|
const confidence = calculateConfidence(lesson);
|
|
287
310
|
const ref = lesson.verified_at ?? lesson.ts;
|
|
@@ -336,7 +359,7 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
336
359
|
const histKey = `cachly:lessons:${topic}`;
|
|
337
360
|
const all = await redis.lrange(histKey, -3, -1);
|
|
338
361
|
if (all.length > 0) {
|
|
339
|
-
const parsed = all.map(e =>
|
|
362
|
+
const parsed = all.map(e => safeJsonParse(e, null)).filter(Boolean);
|
|
340
363
|
const lines = parsed.map(p => `- ${p.outcome === 'success' ? '✅' : '❌'} ${p.what_worked.slice(0, 120)} (${new Date(p.ts).toLocaleDateString('de-DE')})`);
|
|
341
364
|
return `⚠️ No successful solution for \`${topic}\` yet. Last attempts:\n\n${lines.join('\n')}`;
|
|
342
365
|
}
|
|
@@ -348,7 +371,9 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
348
371
|
const raw = await redis.get(k);
|
|
349
372
|
if (!raw)
|
|
350
373
|
continue;
|
|
351
|
-
const lesson =
|
|
374
|
+
const lesson = safeJsonParse(raw, null);
|
|
375
|
+
if (!lesson)
|
|
376
|
+
continue;
|
|
352
377
|
results.push(`**\`${lesson.topic}\`** — ${lesson.what_worked.slice(0, 200)}`);
|
|
353
378
|
}
|
|
354
379
|
return `🔍 **Partial matches for \`${topic}\`:**\n\n${results.join('\n\n')}`;
|
|
@@ -485,14 +510,13 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
485
510
|
lStream.on('error', reject);
|
|
486
511
|
});
|
|
487
512
|
const lessons = [];
|
|
488
|
-
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
513
|
+
if (lessonKeys.length > 0) {
|
|
514
|
+
const raws = await redis.mget(...lessonKeys);
|
|
515
|
+
for (const raw of raws) {
|
|
516
|
+
const l = safeJsonParse(raw ?? null, null);
|
|
517
|
+
if (l)
|
|
518
|
+
lessons.push(l);
|
|
494
519
|
}
|
|
495
|
-
catch { /* skip corrupt */ }
|
|
496
520
|
}
|
|
497
521
|
lessons.sort((a, b) => new Date(b.ts).getTime() - new Date(a.ts).getTime());
|
|
498
522
|
// 3. Count context entries (filter :meta keys)
|
|
@@ -507,13 +531,7 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
507
531
|
});
|
|
508
532
|
// 4. Last session
|
|
509
533
|
const lastSessionRaw = await redis.get('cachly:session:last');
|
|
510
|
-
|
|
511
|
-
if (lastSessionRaw) {
|
|
512
|
-
try {
|
|
513
|
-
lastSession = JSON.parse(lastSessionRaw);
|
|
514
|
-
}
|
|
515
|
-
catch { /* ignore */ }
|
|
516
|
-
}
|
|
534
|
+
const lastSession = safeJsonParse(lastSessionRaw, null);
|
|
517
535
|
// 5. Focus filtering
|
|
518
536
|
const focusTerms = focus.toLowerCase().split(/\s+/).filter(Boolean);
|
|
519
537
|
const focusLessons = focusTerms.length > 0
|
|
@@ -526,7 +544,7 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
526
544
|
let streakMessage = '';
|
|
527
545
|
try {
|
|
528
546
|
const streakRaw = await redis.get('cachly:streak:current');
|
|
529
|
-
const streak =
|
|
547
|
+
const streak = safeJsonParse(streakRaw, null);
|
|
530
548
|
const today = new Date().toISOString().slice(0, 10);
|
|
531
549
|
if (streak) {
|
|
532
550
|
const lastDate = streak.last_date;
|
|
@@ -572,6 +590,35 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
572
590
|
const lines = [`🧠 **Session Briefing**${providerLabel}`, ''];
|
|
573
591
|
if (streakMessage)
|
|
574
592
|
lines.push(streakMessage, '');
|
|
593
|
+
// ── What's New (shown once per version update) ──────────────────────────
|
|
594
|
+
try {
|
|
595
|
+
const seenVersion = await redis.get('cachly:mcp:version:last_seen');
|
|
596
|
+
if (seenVersion !== MCP_VERSION) {
|
|
597
|
+
await redis.set('cachly:mcp:version:last_seen', MCP_VERSION, 'EX', 365 * 86400);
|
|
598
|
+
const changelog = WHATS_NEW[MCP_VERSION];
|
|
599
|
+
if (changelog) {
|
|
600
|
+
lines.push(...changelog, '');
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
catch { /* non-critical */ }
|
|
605
|
+
// ── First-time welcome (empty brain) ───────────────────────────────────
|
|
606
|
+
const isFirstSession = !lastSession && lessons.length === 0 && ctxCount === 0;
|
|
607
|
+
if (isFirstSession) {
|
|
608
|
+
lines.push('🎉 **Welcome! Your AI Brain is live.**', '');
|
|
609
|
+
lines.push('It learns from your work automatically. After your first session it will look like this:', '');
|
|
610
|
+
lines.push(' ✅ `api:auth` — Bearer token in header, not cookie; 401 on missing scope');
|
|
611
|
+
lines.push(' ✅ `database:migrations` — always run migrations before deploy');
|
|
612
|
+
lines.push(' ⚠️ `docker:build` — ARG changes bust all subsequent cache layers');
|
|
613
|
+
lines.push('');
|
|
614
|
+
lines.push('**3 steps to grow your brain:**');
|
|
615
|
+
lines.push(' 1. Fix something → `learn_from_attempts(topic="bug:name", outcome="success", what_worked="...")`');
|
|
616
|
+
lines.push(' 2. Save context → `remember_context(key="arch", value="...")`');
|
|
617
|
+
lines.push(' 3. End session → `session_end(summary="What I did")`');
|
|
618
|
+
lines.push('');
|
|
619
|
+
lines.push('💡 Run `brain_doctor` for a setup health-check and personalised tips.');
|
|
620
|
+
lines.push('');
|
|
621
|
+
}
|
|
575
622
|
// Handoff from previous window (if any)
|
|
576
623
|
const handoffRaw = await redis.get('cachly:session:handoff');
|
|
577
624
|
if (handoffRaw) {
|
|
@@ -1011,8 +1058,6 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
1011
1058
|
}
|
|
1012
1059
|
catch { /* non-critical */ }
|
|
1013
1060
|
// ── 🌍 Knowledge Commons — community stats banner ───────────────────────
|
|
1014
|
-
// Fetch syndication stats and show a 1-liner: total lessons + confirms + weekly growth.
|
|
1015
|
-
// Non-fatal: never blocks session start if the API call fails.
|
|
1016
1061
|
try {
|
|
1017
1062
|
const commonsStats = await apiFetch('/api/v1/syndication/stats');
|
|
1018
1063
|
if (commonsStats.total_lessons > 0) {
|
|
@@ -1023,6 +1068,19 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
1023
1068
|
}
|
|
1024
1069
|
}
|
|
1025
1070
|
catch { /* non-critical — never block session start */ }
|
|
1071
|
+
// ── Brain Doctor hint (Punkt 6) — surface when brain needs attention ───
|
|
1072
|
+
const hasOpenFailures = lessons.filter(l => l.outcome === 'failure' || l.outcome === 'partial').length > 0;
|
|
1073
|
+
const hasStaleLessons = lessons.some(l => l.outcome === 'success' && calculateConfidence(l) < CONFIDENCE_WARN_VALUE);
|
|
1074
|
+
if (lessons.length === 0 || hasOpenFailures || hasStaleLessons) {
|
|
1075
|
+
const reasons = [];
|
|
1076
|
+
if (lessons.length === 0)
|
|
1077
|
+
reasons.push('brain is empty');
|
|
1078
|
+
if (hasOpenFailures)
|
|
1079
|
+
reasons.push('open failures');
|
|
1080
|
+
if (hasStaleLessons)
|
|
1081
|
+
reasons.push('stale lessons');
|
|
1082
|
+
lines.push(`🩺 _Run \`brain_doctor\` to fix: ${reasons.join(', ')}._`);
|
|
1083
|
+
}
|
|
1026
1084
|
return lines.join('\n');
|
|
1027
1085
|
}
|
|
1028
1086
|
// ── session_end ───────────────────────────────────────────────────────────
|
|
@@ -1033,18 +1091,20 @@ export async function handleBrainTool(name, args, getConnection, apiFetch) {
|
|
|
1033
1091
|
// Calculate duration from session_start marker
|
|
1034
1092
|
let durationMin;
|
|
1035
1093
|
const currentRaw = await redis.get('cachly:session:current');
|
|
1036
|
-
if (currentRaw) {
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1094
|
+
if (!currentRaw) {
|
|
1095
|
+
return `⚠️ No active session found.\n\nRun \`session_start\` first to begin tracking a session, then call \`session_end\` when you're done.`;
|
|
1096
|
+
}
|
|
1097
|
+
const current = safeJsonParse(currentRaw, null);
|
|
1098
|
+
if (current?.started) {
|
|
1099
|
+
durationMin = Math.round((now.getTime() - new Date(current.started).getTime()) / 60000);
|
|
1042
1100
|
}
|
|
1043
|
-
// ── Session Replay: capture decision log ─────────────────────────────
|
|
1044
1101
|
let decisionLog = [];
|
|
1045
1102
|
try {
|
|
1046
1103
|
const dlEntries = await redis.lrange('cachly:session:decision-log', 0, -1);
|
|
1047
|
-
decisionLog = dlEntries.
|
|
1104
|
+
decisionLog = dlEntries.flatMap(e => {
|
|
1105
|
+
const entry = safeJsonParse(e, null);
|
|
1106
|
+
return entry ? [entry] : [];
|
|
1107
|
+
});
|
|
1048
1108
|
await redis.del('cachly:session:decision-log');
|
|
1049
1109
|
}
|
|
1050
1110
|
catch { /* non-critical */ }
|