@yeaft/webchat-agent 0.1.409 → 0.1.411

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.1.409",
3
+ "version": "0.1.411",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/unify/config.js CHANGED
@@ -267,3 +267,39 @@ export function loadConfig(overrides = {}) {
267
267
 
268
268
  return config;
269
269
  }
270
+
271
+ /**
272
+ * Load MCP server configuration from ~/.yeaft/mcp.json.
273
+ *
274
+ * JSON format (frontmatter parser can't handle nested objects):
275
+ * {
276
+ * "servers": [
277
+ * {
278
+ * "name": "github",
279
+ * "command": "npx",
280
+ * "args": ["@mcp/github"],
281
+ * "env": { "GITHUB_TOKEN": "ghp_..." }
282
+ * }
283
+ * ]
284
+ * }
285
+ *
286
+ * @param {string} yeaftDir — e.g. ~/.yeaft
287
+ * @returns {{ servers: object[] }}
288
+ */
289
+ export function loadMCPConfig(yeaftDir) {
290
+ const mcpPath = join(yeaftDir, 'mcp.json');
291
+ if (!existsSync(mcpPath)) return { servers: [] };
292
+
293
+ try {
294
+ const raw = readFileSync(mcpPath, 'utf8');
295
+ const parsed = JSON.parse(raw);
296
+ if (!parsed.servers || !Array.isArray(parsed.servers)) {
297
+ return { servers: [] };
298
+ }
299
+ // Each server must have at least name + command
300
+ const valid = parsed.servers.filter(s => s.name && s.command);
301
+ return { servers: valid };
302
+ } catch {
303
+ return { servers: [] };
304
+ }
305
+ }
package/unify/engine.js CHANGED
@@ -22,6 +22,7 @@ import { buildSystemPrompt } from './prompts.js';
22
22
  import { LLMContextError } from './llm/adapter.js';
23
23
  import { recall } from './memory/recall.js';
24
24
  import { shouldConsolidate, consolidate } from './memory/consolidate.js';
25
+ import { runStopHooks } from './stop-hooks.js';
25
26
 
26
27
  /** Maximum number of turns before the engine stops to prevent infinite loops. */
27
28
  const MAX_TURNS = 25;
@@ -67,16 +68,32 @@ export class Engine {
67
68
  /** @type {import('./memory/store.js').MemoryStore|null} */
68
69
  #memoryStore;
69
70
 
71
+ /** @type {import('./tools/registry.js').ToolRegistry|null} */
72
+ #toolRegistry;
73
+
74
+ /** @type {import('./skills.js').SkillManager|null} */
75
+ #skillManager;
76
+
77
+ /** @type {import('./mcp.js').MCPManager|null} */
78
+ #mcpManager;
79
+
80
+ /** @type {string|null} */
81
+ #yeaftDir;
82
+
70
83
  /**
71
84
  * @param {{
72
85
  * adapter: import('./llm/adapter.js').LLMAdapter,
73
86
  * trace: object,
74
87
  * config: object,
75
88
  * conversationStore?: import('./conversation/persist.js').ConversationStore,
76
- * memoryStore?: import('./memory/store.js').MemoryStore
89
+ * memoryStore?: import('./memory/store.js').MemoryStore,
90
+ * toolRegistry?: import('./tools/registry.js').ToolRegistry,
91
+ * skillManager?: import('./skills.js').SkillManager,
92
+ * mcpManager?: import('./mcp.js').MCPManager,
93
+ * yeaftDir?: string,
77
94
  * }} params
78
95
  */
79
- constructor({ adapter, trace, config, conversationStore, memoryStore }) {
96
+ constructor({ adapter, trace, config, conversationStore, memoryStore, toolRegistry, skillManager, mcpManager, yeaftDir }) {
80
97
  this.#adapter = adapter;
81
98
  this.#trace = trace;
82
99
  this.#config = config;
@@ -84,6 +101,10 @@ export class Engine {
84
101
  this.#traceId = randomUUID();
85
102
  this.#conversationStore = conversationStore || null;
86
103
  this.#memoryStore = memoryStore || null;
104
+ this.#toolRegistry = toolRegistry || null;
105
+ this.#skillManager = skillManager || null;
106
+ this.#mcpManager = mcpManager || null;
107
+ this.#yeaftDir = yeaftDir || null;
87
108
  }
88
109
 
89
110
  /**
@@ -106,10 +127,16 @@ export class Engine {
106
127
 
107
128
  /**
108
129
  * Get the list of registered tool definitions (for passing to the adapter).
130
+ * Prefers ToolRegistry (mode-aware) when available, falls back to legacy #tools Map.
109
131
  *
132
+ * @param {string} [mode]
110
133
  * @returns {import('./llm/adapter.js').UnifiedToolDef[]}
111
134
  */
112
- #getToolDefs() {
135
+ #getToolDefs(mode) {
136
+ if (this.#toolRegistry) {
137
+ return this.#toolRegistry.getToolDefs(mode || 'chat');
138
+ }
139
+ // Legacy path: no mode filtering
113
140
  const defs = [];
114
141
  for (const [, tool] of this.#tools) {
115
142
  defs.push({
@@ -122,23 +149,58 @@ export class Engine {
122
149
  }
123
150
 
124
151
  /**
125
- * Build the system prompt with memory and compact summary.
152
+ * Build the system prompt with memory, compact summary, and skill content.
126
153
  *
127
154
  * @param {string} mode
128
155
  * @param {{ profile?: string, entries?: object[] }} [memory]
129
156
  * @param {string} [compactSummary]
157
+ * @param {string} [prompt] — user prompt (for skill relevance matching)
130
158
  * @returns {string}
131
159
  */
132
- #buildSystemPrompt(mode, memory, compactSummary) {
160
+ #buildSystemPrompt(mode, memory, compactSummary, prompt) {
161
+ // Get relevant skill content if SkillManager is wired
162
+ let skillContent = '';
163
+ if (this.#skillManager && prompt) {
164
+ skillContent = this.#skillManager.getRelevantPromptContent(prompt, mode);
165
+ }
166
+
167
+ // Get tool names from the appropriate source
168
+ const toolNames = this.#toolRegistry
169
+ ? this.#toolRegistry.getToolNames(mode || 'chat')
170
+ : Array.from(this.#tools.keys());
171
+
133
172
  return buildSystemPrompt({
134
173
  language: this.#config.language || 'en',
135
174
  mode,
136
- toolNames: Array.from(this.#tools.keys()),
175
+ toolNames,
137
176
  memory,
138
177
  compactSummary,
178
+ skillContent,
139
179
  });
140
180
  }
141
181
 
182
+ /**
183
+ * Build the full tool context for Phase 5 tools.
184
+ *
185
+ * @param {AbortSignal} [signal]
186
+ * @param {string} [mode]
187
+ * @returns {object}
188
+ */
189
+ #buildToolContext(signal, mode) {
190
+ return {
191
+ signal,
192
+ yeaftDir: this.#yeaftDir,
193
+ cwd: process.cwd(),
194
+ mcpManager: this.#mcpManager,
195
+ skillManager: this.#skillManager,
196
+ memoryStore: this.#memoryStore,
197
+ conversationStore: this.#conversationStore,
198
+ adapter: this.#adapter,
199
+ config: this.#config,
200
+ mode,
201
+ };
202
+ }
203
+
142
204
  /**
143
205
  * Perform memory recall for a given prompt.
144
206
  *
@@ -262,7 +324,7 @@ export class Engine {
262
324
  }
263
325
 
264
326
  const compactSummary = this.#getCompactSummary();
265
- const systemPrompt = this.#buildSystemPrompt(mode, memory, compactSummary);
327
+ const systemPrompt = this.#buildSystemPrompt(mode, memory, compactSummary, prompt);
266
328
 
267
329
  // Build conversation: existing messages + new user message
268
330
  const conversationMessages = [
@@ -270,7 +332,7 @@ export class Engine {
270
332
  { role: 'user', content: prompt },
271
333
  ];
272
334
 
273
- const toolDefs = this.#getToolDefs();
335
+ const toolDefs = this.#getToolDefs(mode);
274
336
  let turnNumber = 0;
275
337
  let continueTurns = 0; // auto-continue counter
276
338
  let fullResponseText = '';
@@ -416,33 +478,66 @@ export class Engine {
416
478
  if (stopReason !== 'tool_use' || toolCalls.length === 0) {
417
479
  yield { type: 'turn_end', turnNumber, stopReason };
418
480
 
419
- // ─── Post-query: Persist + Consolidate ────────────
420
- this.#persistMessages(prompt, fullResponseText, mode, assistantMsg.toolCalls);
481
+ // ─── Post-query: StopHooks or Legacy ─────────────
482
+ if (this.#yeaftDir && this.#conversationStore) {
483
+ // Full pipeline: persist + consolidate + dream gate
484
+ const hookResult = await runStopHooks({
485
+ yeaftDir: this.#yeaftDir,
486
+ mode,
487
+ conversationStore: this.#conversationStore,
488
+ memoryStore: this.#memoryStore,
489
+ adapter: this.#adapter,
490
+ config: this.#config,
491
+ messages: conversationMessages,
492
+ trace: this.#trace,
493
+ });
494
+
495
+ if (hookResult.consolidated) {
496
+ yield { type: 'consolidate', archivedCount: 0, extractedCount: 0 };
497
+ }
498
+ if (hookResult.dreamTriggered) {
499
+ yield { type: 'dream_triggered' };
500
+ }
501
+ } else {
502
+ // Legacy path (no yeaftDir → use old behavior)
503
+ this.#persistMessages(prompt, fullResponseText, mode, assistantMsg.toolCalls);
421
504
 
422
- const consolidated = await this.#maybeConsolidate();
423
- if (consolidated && consolidated.archivedCount > 0) {
424
- yield { type: 'consolidate', archivedCount: consolidated.archivedCount, extractedCount: consolidated.extractedCount };
505
+ const consolidated = await this.#maybeConsolidate();
506
+ if (consolidated && consolidated.archivedCount > 0) {
507
+ yield { type: 'consolidate', archivedCount: consolidated.archivedCount, extractedCount: consolidated.extractedCount };
508
+ }
425
509
  }
426
510
 
427
511
  break;
428
512
  }
429
513
 
430
514
  // Execute tool calls and feed results back
515
+ const toolCtx = this.#buildToolContext(signal, mode);
516
+
431
517
  for (const tc of toolCalls) {
432
- const tool = this.#tools.get(tc.name);
433
518
  const toolStartTime = Date.now();
434
519
 
435
520
  let output;
436
521
  let isError = false;
437
522
 
438
- if (!tool) {
523
+ // Resolve tool: prefer ToolRegistry, fallback to legacy #tools Map
524
+ const hasTool = this.#toolRegistry
525
+ ? this.#toolRegistry.has(tc.name)
526
+ : this.#tools.has(tc.name);
527
+
528
+ if (!hasTool) {
439
529
  output = `Error: unknown tool "${tc.name}"`;
440
530
  isError = true;
441
531
  yield { type: 'tool_end', id: tc.id, name: tc.name, output, isError: true };
442
532
  } else {
443
533
  try {
444
534
  yield { type: 'tool_start', id: tc.id, name: tc.name, input: tc.input };
445
- output = await tool.execute(tc.input, { signal });
535
+ if (this.#toolRegistry) {
536
+ output = await this.#toolRegistry.execute(tc.name, tc.input, toolCtx);
537
+ } else {
538
+ const tool = this.#tools.get(tc.name);
539
+ output = await tool.execute(tc.input, { signal });
540
+ }
446
541
  yield { type: 'tool_end', id: tc.id, name: tc.name, output, isError: false };
447
542
  } catch (err) {
448
543
  output = `Error: ${err.message}`;
@@ -490,6 +585,7 @@ export class Engine {
490
585
  * @returns {string[]}
491
586
  */
492
587
  get toolNames() {
588
+ if (this.#toolRegistry) return this.#toolRegistry.names;
493
589
  return Array.from(this.#tools.keys());
494
590
  }
495
591
 
@@ -508,4 +604,16 @@ export class Engine {
508
604
  get memoryStore() {
509
605
  return this.#memoryStore;
510
606
  }
607
+
608
+ /** @returns {import('./tools/registry.js').ToolRegistry|null} */
609
+ get toolRegistry() { return this.#toolRegistry; }
610
+
611
+ /** @returns {import('./skills.js').SkillManager|null} */
612
+ get skillManager() { return this.#skillManager; }
613
+
614
+ /** @returns {import('./mcp.js').MCPManager|null} */
615
+ get mcpManager() { return this.#mcpManager; }
616
+
617
+ /** @returns {string|null} */
618
+ get yeaftDir() { return this.#yeaftDir; }
511
619
  }
package/unify/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  export { initYeaftDir, DEFAULT_YEAFT_DIR } from './init.js';
8
- export { loadConfig, parseFrontmatter } from './config.js';
8
+ export { loadConfig, parseFrontmatter, loadMCPConfig } from './config.js';
9
9
  export { DebugTrace, NullTrace, createTrace } from './debug-trace.js';
10
10
  export {
11
11
  LLMAdapter,
@@ -25,3 +25,16 @@ export { MemoryStore, parseEntry, serializeEntry, MEMORY_KINDS } from './memory/
25
25
  export { recall, extractKeywords, computeFingerprint, clearRecallCache } from './memory/recall.js';
26
26
  export { extractMemories } from './memory/extract.js';
27
27
  export { consolidate, shouldConsolidate } from './memory/consolidate.js';
28
+
29
+ // Phase 5: Advanced features
30
+ export { KINDS, KIND_PRIORITY, KIND_DESCRIPTIONS, IMPORTANCE_LEVELS, validateEntry, parseScopePath, getAncestorScopes, areScopesRelated } from './memory/types.js';
31
+ export { scanEntries, scoreEntry, findStaleEntries, findDuplicateGroups, summarizeScan } from './memory/scan.js';
32
+ export { dream, checkDreamGate, readDreamState, writeDreamState, incrementQueryCount } from './memory/dream.js';
33
+ export { buildOrientPrompt, buildGatherPrompt, buildMergePrompt, buildPrunePrompt, buildPromotePrompt } from './memory/dream-prompt.js';
34
+ export { runStopHooks } from './stop-hooks.js';
35
+ export { MCPManager, createMCPManager } from './mcp.js';
36
+ export { SkillManager, createSkillManager, parseSkill, serializeSkill } from './skills.js';
37
+ export { defineTool } from './tools/types.js';
38
+ export { ToolRegistry, createEmptyRegistry } from './tools/registry.js';
39
+ export { loadSession } from './session.js';
40
+