@stitchdb/cli 0.4.0 → 0.5.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 (2) hide show
  1. package/dist/cli.js +59 -11
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -455,18 +455,61 @@ async function cmdHook(args) {
455
455
  catch {
456
456
  return;
457
457
  }
458
- if (!raw)
459
- return;
460
- let event;
461
- try {
462
- event = JSON.parse(raw);
463
- }
464
- catch {
465
- return;
458
+ let event = {};
459
+ if (raw) {
460
+ try {
461
+ event = JSON.parse(raw);
462
+ }
463
+ catch { /* keep empty */ }
466
464
  }
465
+ // hook_event_name may be missing for SessionStart on some Claude Code
466
+ // versions. Allow the caller to pass it as the first positional arg too.
467
467
  const eventName = event?.hook_event_name || args[0] || '';
468
- const cwd = event?.cwd;
468
+ // SessionStart's payload doesn't include cwd fall back to process.cwd()
469
+ // (which is the directory `claude` was launched from).
470
+ const cwd = event?.cwd || process.cwd();
469
471
  const threadName = inferThreadFor(cwd) || 'default';
472
+ // ── SessionStart: inject prior context as additional context ──────────
473
+ if (eventName === 'SessionStart') {
474
+ try {
475
+ const stitch = client(cfg);
476
+ const projectTag = path.basename(cwd || process.cwd());
477
+ // Pull both: recent dialogue from the per-repo thread + relevant
478
+ // durable memories tagged with the project.
479
+ const [thread, memHits] = await Promise.all([
480
+ stitch.thread(threadName).recall({ last: 20 }).catch(() => ({ thread_id: '', recent: [], semantic: [] })),
481
+ stitch.recall(projectTag, { k: 5 }).catch(() => []),
482
+ ]);
483
+ const lines = [];
484
+ lines.push('## Stitch — prior memory for this project');
485
+ lines.push('');
486
+ lines.push(`Thread: \`${threadName}\` · Workspace recall available via the \`stitch\` MCP server.`);
487
+ lines.push('');
488
+ if (thread.recent && thread.recent.length > 0) {
489
+ lines.push('### Recent conversation (continue from here)');
490
+ for (const t of thread.recent.slice(-12)) {
491
+ const txt = String(t.content || '').replace(/\n+/g, ' ').slice(0, 400);
492
+ lines.push(`- **${t.role}**: ${txt}`);
493
+ }
494
+ lines.push('');
495
+ }
496
+ if (Array.isArray(memHits) && memHits.length > 0) {
497
+ lines.push('### Durable memories');
498
+ for (const m of memHits) {
499
+ const txt = String(m.content || '').replace(/\n+/g, ' ').slice(0, 320);
500
+ lines.push(`- **[${m.kind}]** ${txt}`);
501
+ }
502
+ lines.push('');
503
+ }
504
+ if (lines.length > 4) {
505
+ // Plain stdout is auto-injected as additionalContext for SessionStart.
506
+ process.stdout.write(lines.join('\n'));
507
+ }
508
+ }
509
+ catch { /* silent — never break a session start */ }
510
+ return;
511
+ }
512
+ // ── UserPromptSubmit / Stop: log a turn ───────────────────────────────
470
513
  let role = null;
471
514
  let content = '';
472
515
  if (eventName === 'UserPromptSubmit') {
@@ -846,15 +889,16 @@ async function cmdInstall(args) {
846
889
  else
847
890
  console.log(`failed (${stderr.trim().slice(0, 120)})`);
848
891
  }
849
- // 2. Hooks for auto-logging conversations
892
+ // 2. Hooks: auto-log every turn + auto-inject prior context at session start
850
893
  if (!noHooks) {
851
- process.stdout.write('• Wiring auto-log hooks (UserPromptSubmit + Stop)… ');
894
+ process.stdout.write('• Wiring hooks (SessionStart + UserPromptSubmit + Stop)… ');
852
895
  try {
853
896
  const settingsPath = path.join(os.homedir(), '.claude', 'settings.json');
854
897
  const existing = fs.existsSync(settingsPath)
855
898
  ? JSON.parse(fs.readFileSync(settingsPath, 'utf8'))
856
899
  : {};
857
900
  existing.hooks = existing.hooks || {};
901
+ existing.hooks.SessionStart = mergeHook(existing.hooks.SessionStart, STITCH_SESSION_START_HOOK);
858
902
  existing.hooks.UserPromptSubmit = mergeHook(existing.hooks.UserPromptSubmit, STITCH_USER_HOOK);
859
903
  existing.hooks.Stop = mergeHook(existing.hooks.Stop, STITCH_STOP_HOOK);
860
904
  fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
@@ -957,6 +1001,10 @@ const STITCH_STOP_HOOK = {
957
1001
  matcher: '*',
958
1002
  hooks: [{ type: 'command', command: 'stitch _hook' }],
959
1003
  };
1004
+ const STITCH_SESSION_START_HOOK = {
1005
+ matcher: '*',
1006
+ hooks: [{ type: 'command', command: 'stitch _hook SessionStart' }],
1007
+ };
960
1008
  function mergeHook(existing, entry) {
961
1009
  const arr = Array.isArray(existing) ? existing.slice() : [];
962
1010
  // Replace any earlier Stitch entry; identify by the marker.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stitchdb/cli",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Stitch CLI — manage memory + run agents from your terminal",
5
5
  "type": "module",
6
6
  "bin": {