oortmem 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +21 -2
- package/dist/commands/connect.d.ts +14 -0
- package/dist/commands/connect.js +99 -0
- package/dist/commands/consolidate.d.ts +3 -0
- package/dist/commands/consolidate.js +21 -0
- package/dist/commands/doctor.d.ts +15 -0
- package/dist/commands/doctor.js +410 -0
- package/dist/commands/explain.d.ts +5 -0
- package/dist/commands/explain.js +67 -0
- package/dist/commands/export.d.ts +19 -0
- package/dist/commands/export.js +69 -0
- package/dist/commands/forget.d.ts +5 -0
- package/dist/commands/forget.js +17 -0
- package/dist/commands/hook.d.ts +12 -0
- package/dist/commands/hook.js +165 -0
- package/dist/commands/import.d.ts +23 -0
- package/dist/commands/import.js +67 -0
- package/dist/commands/init.d.ts +9 -0
- package/dist/commands/init.js +86 -0
- package/dist/commands/inspect.d.ts +26 -0
- package/dist/commands/inspect.js +126 -0
- package/dist/commands/install.d.ts +28 -0
- package/dist/commands/install.js +122 -0
- package/dist/commands/keys.d.ts +16 -0
- package/dist/commands/keys.js +69 -0
- package/dist/commands/login.d.ts +74 -0
- package/dist/commands/login.js +326 -0
- package/dist/commands/mcp.d.ts +14 -0
- package/dist/commands/mcp.js +42 -0
- package/dist/commands/observe.d.ts +15 -0
- package/dist/commands/observe.js +73 -0
- package/dist/commands/pin.d.ts +3 -0
- package/dist/commands/pin.js +16 -0
- package/dist/commands/project.d.ts +15 -0
- package/dist/commands/project.js +60 -0
- package/dist/commands/recall.d.ts +11 -0
- package/dist/commands/recall.js +46 -0
- package/dist/commands/setup.d.ts +50 -0
- package/dist/commands/setup.js +532 -0
- package/dist/commands/stats.d.ts +25 -0
- package/dist/commands/stats.js +113 -0
- package/dist/connectors.d.ts +83 -0
- package/dist/connectors.js +615 -0
- package/dist/credentials-file.d.ts +37 -0
- package/dist/credentials-file.js +94 -0
- package/dist/credentials.d.ts +47 -0
- package/dist/credentials.js +46 -0
- package/dist/embed-cache-disk.d.ts +13 -0
- package/dist/embed-cache-disk.js +81 -0
- package/dist/io.d.ts +36 -0
- package/dist/io.js +117 -0
- package/dist/keychain.d.ts +70 -0
- package/dist/keychain.js +218 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +28 -0
- package/dist/mcp.d.ts +17 -0
- package/dist/mcp.js +168 -0
- package/dist/program.d.ts +14 -0
- package/dist/program.js +224 -0
- package/dist/relay.d.ts +57 -0
- package/dist/relay.js +119 -0
- package/dist/remember.d.ts +27 -0
- package/dist/remember.js +42 -0
- package/dist/ui.d.ts +42 -0
- package/dist/ui.js +63 -0
- package/dist/util.d.ts +78 -0
- package/dist/util.js +202 -0
- package/dist/wire.d.ts +65 -0
- package/dist/wire.js +257 -0
- package/package.json +67 -3
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { HOOK_TIMEOUTS, createHookLogger, runHook } from '@oortmem/claude-code';
|
|
2
|
+
import { resolveHome } from '@oortmem/core';
|
|
3
|
+
import { echo } from '../util.js';
|
|
4
|
+
export const HOOK_COMMAND_EVENTS = [
|
|
5
|
+
'UserPromptSubmit',
|
|
6
|
+
'Stop',
|
|
7
|
+
'SessionStart',
|
|
8
|
+
'drain',
|
|
9
|
+
];
|
|
10
|
+
/** Stdin is read for at most this long; Claude Code writes it at once and closes it. */
|
|
11
|
+
export const HOOK_STDIN_CAP_MS = 1000;
|
|
12
|
+
/** Exit this long before Claude Code's own hook timeout would discard the output. */
|
|
13
|
+
const DEADLINE_MARGIN_MS = 1000;
|
|
14
|
+
/**
|
|
15
|
+
* A hook waits at most this long for the store's write lock (held, say, by a
|
|
16
|
+
* drain). SQLite's busy wait is synchronous: it freezes the event loop, the
|
|
17
|
+
* deadline timer included, so it must stay short. The drain keeps the default.
|
|
18
|
+
*/
|
|
19
|
+
export const HOOK_BUSY_TIMEOUT_MS = 250;
|
|
20
|
+
/** SQLite's "another connection holds the lock" errors. */
|
|
21
|
+
function isBusy(e) {
|
|
22
|
+
const code = e?.code;
|
|
23
|
+
return (typeof code === 'string' && (code.startsWith('SQLITE_BUSY') || code.startsWith('SQLITE_LOCKED')));
|
|
24
|
+
}
|
|
25
|
+
/** `store`, noting through `onBusy` any call that failed on the lock (the handlers swallow errors). */
|
|
26
|
+
function busyAware(store, onBusy) {
|
|
27
|
+
return new Proxy(store, {
|
|
28
|
+
get(target, prop, receiver) {
|
|
29
|
+
const value = Reflect.get(target, prop, receiver);
|
|
30
|
+
if (typeof value !== 'function')
|
|
31
|
+
return value;
|
|
32
|
+
return async (...args) => {
|
|
33
|
+
try {
|
|
34
|
+
return await value.apply(target, args);
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
if (isBusy(e))
|
|
38
|
+
onBusy();
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const isEvent = (e) => HOOK_COMMAND_EVENTS.includes(e);
|
|
46
|
+
/** The session's cwd from the hook input, so config and scope follow the session, not our cwd. */
|
|
47
|
+
function inputCwd(stdin) {
|
|
48
|
+
try {
|
|
49
|
+
const raw = JSON.parse(stdin);
|
|
50
|
+
if (typeof raw === 'object' && raw !== null && 'cwd' in raw) {
|
|
51
|
+
const cwd = raw.cwd;
|
|
52
|
+
if (typeof cwd === 'string' && cwd.length > 0)
|
|
53
|
+
return cwd;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// runHook logs the invalid input.
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* `oort hook <event>`. Prints exactly the hook JSON (or nothing) on stdout and
|
|
63
|
+
* always exits 0, through `io.hookExit`, which exits the moment the write is
|
|
64
|
+
* flushed: after a recall timeout the losing full recall is still pending and
|
|
65
|
+
* would otherwise hold the process past Claude Code's timeout. Every failure
|
|
66
|
+
* (bad config, a store that will not open, a throwing handler) is logged to
|
|
67
|
+
* the hook log and swallowed. A store locked by another writer (a drain) makes
|
|
68
|
+
* the open or a write fail after 250 ms: stdout is then empty and the log
|
|
69
|
+
* records `degraded:busy`. A deadline exits 0 with empty stdout should
|
|
70
|
+
* anything hang.
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* The adapters each event needs. UserPromptSubmit only recalls; Stop and SessionStart call no
|
|
74
|
+
* model; the drain writes memories. Loading fewer adapters skips their key reads and SDK imports.
|
|
75
|
+
*/
|
|
76
|
+
const HOOK_ROLES = {
|
|
77
|
+
UserPromptSubmit: ['embed', 'rerank'],
|
|
78
|
+
SessionStart: [],
|
|
79
|
+
Stop: [],
|
|
80
|
+
drain: ['embed', 'rerank', 'decide', 'write'],
|
|
81
|
+
};
|
|
82
|
+
export async function hookCommand(event, ctx) {
|
|
83
|
+
const { io } = ctx;
|
|
84
|
+
let exited = false;
|
|
85
|
+
const exit = (stdout) => {
|
|
86
|
+
if (exited)
|
|
87
|
+
return;
|
|
88
|
+
exited = true;
|
|
89
|
+
clearTimeout(deadline);
|
|
90
|
+
io.hookExit(stdout);
|
|
91
|
+
};
|
|
92
|
+
const budget = event in HOOK_TIMEOUTS ? HOOK_TIMEOUTS[event] : 0;
|
|
93
|
+
const deadline = budget > 0 ? setTimeout(() => exit(''), budget * 1000 - DEADLINE_MARGIN_MS) : undefined;
|
|
94
|
+
deadline?.unref();
|
|
95
|
+
let w;
|
|
96
|
+
let stdout = '';
|
|
97
|
+
let busy = false;
|
|
98
|
+
try {
|
|
99
|
+
if (!isEvent(event)) {
|
|
100
|
+
io.err(`oort hook: unknown event ${echo(event)}; expected one of ${HOOK_COMMAND_EVENTS.join(', ')}.\n`);
|
|
101
|
+
log(ctx, undefined, event, 'unknown-event');
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const stdin = event === 'drain' ? '' : await io.readStdin(HOOK_STDIN_CAP_MS);
|
|
105
|
+
const wireStart = performance.now();
|
|
106
|
+
w = await ctx.wire(inputCwd(stdin) ?? io.cwd, {
|
|
107
|
+
env: io.env,
|
|
108
|
+
// The drain serves every project's queue: home config only, whatever lies above OORT_HOME.
|
|
109
|
+
...(event === 'drain' ? { projectConfig: false } : { busyTimeoutMs: HOOK_BUSY_TIMEOUT_MS }),
|
|
110
|
+
roles: HOOK_ROLES[event],
|
|
111
|
+
});
|
|
112
|
+
// Config, store open and key lookup (env, then the keychain): the hook log records it (CON-57).
|
|
113
|
+
const wireMs = performance.now() - wireStart;
|
|
114
|
+
for (const warning of w.warnings)
|
|
115
|
+
log(ctx, w, event, `config:${warning.code}`);
|
|
116
|
+
const home = w.config.home;
|
|
117
|
+
const { harnessLog, unavailable: _u, close: _c, scope: _s, warnings: _w, store, ...engine } = w;
|
|
118
|
+
// The scope is left out: each handler derives it from the session's cwd.
|
|
119
|
+
const out = await runHook(event, stdin, {
|
|
120
|
+
...engine,
|
|
121
|
+
store: busyAware(store, () => {
|
|
122
|
+
busy = true;
|
|
123
|
+
}),
|
|
124
|
+
log: { write: (e) => harnessLog.write({ ...e, timings: { ...e.timings, wireMs } }) },
|
|
125
|
+
// The drain runs in the home directory, so it resolves the home config only:
|
|
126
|
+
// project config never affects the write path.
|
|
127
|
+
spawnDrain: () => io.spawnDrain(home),
|
|
128
|
+
});
|
|
129
|
+
stdout = out.stdout;
|
|
130
|
+
// The drain is detached and nothing waits on it, so it closes the store cleanly.
|
|
131
|
+
if (event === 'drain')
|
|
132
|
+
await w.close();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
if (isBusy(e))
|
|
137
|
+
busy = true;
|
|
138
|
+
else
|
|
139
|
+
log(ctx, w, event, 'cli-error');
|
|
140
|
+
}
|
|
141
|
+
if (busy) {
|
|
142
|
+
// The store was locked: whatever the handler made of it, inject nothing.
|
|
143
|
+
stdout = '';
|
|
144
|
+
log(ctx, w, event, 'degraded:busy', ['busy']);
|
|
145
|
+
}
|
|
146
|
+
exit(stdout);
|
|
147
|
+
}
|
|
148
|
+
function log(ctx, w, event, outcome, degraded) {
|
|
149
|
+
try {
|
|
150
|
+
const logger = w?.harnessLog ?? createHookLogger(resolveHome(ctx.io.env));
|
|
151
|
+
logger.write({
|
|
152
|
+
event,
|
|
153
|
+
session: null,
|
|
154
|
+
project: null,
|
|
155
|
+
outcome,
|
|
156
|
+
counts: {},
|
|
157
|
+
timings: {},
|
|
158
|
+
...(degraded !== undefined && { degraded }),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// Logging is best effort; the session must go on.
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=hook.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Ctx } from '../util.js';
|
|
2
|
+
export interface ImportCommandOptions {
|
|
3
|
+
since?: string;
|
|
4
|
+
yes?: boolean;
|
|
5
|
+
estimateOnly?: boolean;
|
|
6
|
+
includeUnresolved?: boolean;
|
|
7
|
+
projectsDir?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Asks before importing, given the estimate (the setup wizard's prompt);
|
|
10
|
+
* by default a y/N question on the terminal, or `--yes`.
|
|
11
|
+
*/
|
|
12
|
+
confirmWith?: (estimate: {
|
|
13
|
+
observations: number;
|
|
14
|
+
estimatedTokens: number;
|
|
15
|
+
}) => Promise<boolean>;
|
|
16
|
+
}
|
|
17
|
+
/** `30d`, `12h`, `2w` → the instant that long before `now`. */
|
|
18
|
+
export declare function parseSince(raw: string, now: Date): Date;
|
|
19
|
+
/**
|
|
20
|
+
* `oort import claude-code`: prints the estimate (sessions, observations,
|
|
21
|
+
* writer tokens, skipped sessions) first, then asks before observing anything.
|
|
22
|
+
*/
|
|
23
|
+
export declare function importCommand(opts: ImportCommandOptions, ctx: Ctx): Promise<number>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as os from 'node:os';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { importTranscripts } from '@oortmem/claude-code';
|
|
4
|
+
import { uiFor } from '../ui.js';
|
|
5
|
+
import { Refusal, confirm, echo, writePath, withWired } from '../util.js';
|
|
6
|
+
const UNIT_MS = { h: 3_600_000, d: 86_400_000, w: 7 * 86_400_000 };
|
|
7
|
+
/** `30d`, `12h`, `2w` → the instant that long before `now`. */
|
|
8
|
+
export function parseSince(raw, now) {
|
|
9
|
+
const m = /^(\d+)([hdw])$/.exec(raw.trim());
|
|
10
|
+
if (m === null)
|
|
11
|
+
throw new Refusal(`--since ${echo(raw)} must look like 12h, 30d or 2w.`);
|
|
12
|
+
return new Date(now.getTime() - Number(m[1]) * UNIT_MS[m[2]]);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* `oort import claude-code`: prints the estimate (sessions, observations,
|
|
16
|
+
* writer tokens, skipped sessions) first, then asks before observing anything.
|
|
17
|
+
*/
|
|
18
|
+
export async function importCommand(opts, ctx) {
|
|
19
|
+
const since = opts.since === undefined ? undefined : parseSince(opts.since, new Date());
|
|
20
|
+
const home = ctx.io.env['HOME'] || os.homedir();
|
|
21
|
+
const projectsDir = path.resolve(ctx.io.cwd, opts.projectsDir ?? path.join(home, '.claude', 'projects'));
|
|
22
|
+
return withWired(ctx, async (w) => {
|
|
23
|
+
const deps = { ...w, log: w.harnessLog };
|
|
24
|
+
const base = {
|
|
25
|
+
projectsDir,
|
|
26
|
+
...(since !== undefined && { since }),
|
|
27
|
+
includeUnresolved: opts.includeUnresolved === true,
|
|
28
|
+
};
|
|
29
|
+
const est = await importTranscripts({ ...base, estimateOnly: true }, deps);
|
|
30
|
+
ctx.io.out(uiFor(ctx.io).header('import claude-code', 'estimate first, then asks'));
|
|
31
|
+
ctx.io.out([
|
|
32
|
+
`Claude Code transcripts in ${projectsDir}${since !== undefined ? ` since ${since.toISOString()}` : ''}:`,
|
|
33
|
+
` sessions ${est.sessions}`,
|
|
34
|
+
` observations ${est.observations}`,
|
|
35
|
+
` estimated tokens ${est.estimatedTokens} (writer, approximate)`,
|
|
36
|
+
` skipped (unresolved) ${est.skippedUnresolved}${est.skippedUnresolved > 0 && opts.includeUnresolved !== true ? ' (--include-unresolved imports them as global)' : ''}`,
|
|
37
|
+
` skipped (unreadable) ${est.skippedUnreadable}`,
|
|
38
|
+
].join('\n') + '\n');
|
|
39
|
+
if (opts.estimateOnly === true || est.observations === 0)
|
|
40
|
+
return 0;
|
|
41
|
+
writePath(w, 'oort import claude-code');
|
|
42
|
+
const go = opts.confirmWith !== undefined
|
|
43
|
+
? await opts.confirmWith(est)
|
|
44
|
+
: await confirm(ctx.io, 'Proceed?', opts.yes === true, `import ${est.observations} observations`);
|
|
45
|
+
if (!go) {
|
|
46
|
+
ctx.io.out('Nothing imported.\n');
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
const r = await importTranscripts({ ...base, estimateOnly: false }, deps);
|
|
50
|
+
const outcomes = Object.entries(r.outcomes ?? {})
|
|
51
|
+
.map(([k, n]) => `${k} ${n}`)
|
|
52
|
+
.join(', ');
|
|
53
|
+
ctx.io.out(`Imported ${r.observations} observations: ${outcomes || 'nothing new'}.\n`);
|
|
54
|
+
const skippedWhy = Object.entries(r.unwritable ?? {});
|
|
55
|
+
if (skippedWhy.length > 0) {
|
|
56
|
+
ctx.io.out(`Skipped as unwritable (a retry would fail the same way): ${skippedWhy.map(([k, n]) => `${k} x${n}`).join('; ')}\n`);
|
|
57
|
+
}
|
|
58
|
+
const failed = Object.keys(r.failedSessions ?? {}).length;
|
|
59
|
+
if (failed > 0) {
|
|
60
|
+
ctx.io.out(`${failed} session(s) stopped at a failed observation; re-run the import to retry them.\n`);
|
|
61
|
+
const reasons = new Set(Object.values(r.failedSessions ?? {}).map((f) => f.reason));
|
|
62
|
+
ctx.io.out(`Reason: ${[...reasons].join('; ')}\n`);
|
|
63
|
+
}
|
|
64
|
+
return 0;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=import.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Ctx } from '../util.js';
|
|
2
|
+
/**
|
|
3
|
+
* `oort init [--project]`: writes `$OORT_HOME/config.json` with the defaults
|
|
4
|
+
* when absent (never overwrites), and with `--project` an `oort.config.json`
|
|
5
|
+
* at the git root. Reports which keys are missing and how to set each.
|
|
6
|
+
*/
|
|
7
|
+
export declare function initCommand(opts: {
|
|
8
|
+
project?: boolean;
|
|
9
|
+
}, ctx: Ctx): Promise<number>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { OortConfig, ensurePrivateDir, findGitRoot, resolveHome } from '@oortmem/core';
|
|
4
|
+
import { uiFor } from '../ui.js';
|
|
5
|
+
import { Refusal } from '../util.js';
|
|
6
|
+
/** Each key and what needs it. */
|
|
7
|
+
const KEYS = [
|
|
8
|
+
{
|
|
9
|
+
provider: 'voyage',
|
|
10
|
+
envVar: 'VOYAGE_API_KEY',
|
|
11
|
+
needs: 'embed and rerank (dense recall; observe, import)',
|
|
12
|
+
},
|
|
13
|
+
{ provider: 'typesafe', envVar: 'TYPESAFE_API_KEY', needs: 'decide (observe, import)' },
|
|
14
|
+
{ provider: 'anthropic', envVar: 'ANTHROPIC_API_KEY', needs: 'write (observe, import)' },
|
|
15
|
+
];
|
|
16
|
+
/** Creates `file` with `data` and `mode`; returns false, leaving it untouched, when it exists. */
|
|
17
|
+
function createExclusive(file, data, mode) {
|
|
18
|
+
let fd;
|
|
19
|
+
try {
|
|
20
|
+
fd = fs.openSync(file, 'wx', mode);
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
if (e.code === 'EEXIST')
|
|
24
|
+
return false;
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
fs.writeSync(fd, data);
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
fs.closeSync(fd);
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* `oort init [--project]`: writes `$OORT_HOME/config.json` with the defaults
|
|
37
|
+
* when absent (never overwrites), and with `--project` an `oort.config.json`
|
|
38
|
+
* at the git root. Reports which keys are missing and how to set each.
|
|
39
|
+
*/
|
|
40
|
+
export async function initCommand(opts, ctx) {
|
|
41
|
+
const { io } = ctx;
|
|
42
|
+
const home = resolveHome(io.env);
|
|
43
|
+
ensurePrivateDir(home);
|
|
44
|
+
const file = path.join(home, 'config.json');
|
|
45
|
+
io.out(uiFor(io).header('init', home));
|
|
46
|
+
const defaults = `${JSON.stringify(OortConfig.parse({}), null, 2)}\n`;
|
|
47
|
+
io.out(createExclusive(file, defaults, 0o600) ? `Created ${file}\n` : `Kept existing ${file}\n`);
|
|
48
|
+
if (opts.project === true) {
|
|
49
|
+
const root = findGitRoot(io.cwd);
|
|
50
|
+
if (root === undefined)
|
|
51
|
+
throw new Refusal('--project needs a git repository; none found above the cwd.');
|
|
52
|
+
const projectFile = path.join(root, 'oort.config.json');
|
|
53
|
+
const body = `${JSON.stringify({ project: { private: false } }, null, 2)}\n`;
|
|
54
|
+
io.out(
|
|
55
|
+
// 0644: the project file is meant to be committed and holds no secrets.
|
|
56
|
+
createExclusive(projectFile, body, 0o644)
|
|
57
|
+
? `Created ${projectFile}\n`
|
|
58
|
+
: `Kept existing ${projectFile}\n`);
|
|
59
|
+
}
|
|
60
|
+
// Same order as `wire`: the environment, then the Keychain. Presence only; values are dropped.
|
|
61
|
+
const missing = [];
|
|
62
|
+
for (const k of KEYS) {
|
|
63
|
+
if (io.env[k.envVar])
|
|
64
|
+
continue;
|
|
65
|
+
if ((await ctx.keychain.read(k.provider)) === undefined)
|
|
66
|
+
missing.push(k);
|
|
67
|
+
}
|
|
68
|
+
const oort = Boolean(io.env['OORT_API_KEY']) ||
|
|
69
|
+
(missing.length > 0 && (await ctx.keychain.read('oort')) !== undefined);
|
|
70
|
+
if (missing.length === 0) {
|
|
71
|
+
io.out('All keys are set.\n');
|
|
72
|
+
}
|
|
73
|
+
else if (oort) {
|
|
74
|
+
io.out(`Oort Pro key found: ${missing.map((k) => k.provider).join(', ')} go through the Oort Pro relay; your own keys win where set.\n`);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
io.out('Missing keys (recall works lexical-only without them):\n');
|
|
78
|
+
for (const k of missing) {
|
|
79
|
+
io.out(` ${k.envVar}, for ${k.needs}. Set it with: oort keys set ${k.provider} (or export ${k.envVar}=...)\n`);
|
|
80
|
+
}
|
|
81
|
+
io.out('Do not export ANTHROPIC_API_KEY globally if you also use Claude Code on a subscription: it would bill the API instead.\n');
|
|
82
|
+
io.out('Or skip vendor keys with Oort Pro: run `oort login`.\n');
|
|
83
|
+
}
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Memory, type MemoryStore, type ScopeFilter } from '@oortmem/core';
|
|
2
|
+
import { type Ctx } from '../util.js';
|
|
3
|
+
export interface InspectOptions {
|
|
4
|
+
json?: boolean;
|
|
5
|
+
limit?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface ChainLink {
|
|
8
|
+
id: string;
|
|
9
|
+
validFrom: string;
|
|
10
|
+
validTo: string | null;
|
|
11
|
+
expiredAt: string | null;
|
|
12
|
+
endReason: Memory['endReason'];
|
|
13
|
+
text: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The supersede chain through `m`, oldest first: `supersedes` back,
|
|
17
|
+
* `supersededBy` forward. Links outside `filter` (another project's memory,
|
|
18
|
+
* from before observe kept chains in one scope) are walked through but not
|
|
19
|
+
* returned; `hidden` counts them.
|
|
20
|
+
*/
|
|
21
|
+
export declare function supersedeChain(store: MemoryStore, m: Memory, filter: ScopeFilter): Promise<{
|
|
22
|
+
links: ChainLink[];
|
|
23
|
+
hidden: number;
|
|
24
|
+
}>;
|
|
25
|
+
/** `oort inspect [<id>] [--json] [--limit n]` */
|
|
26
|
+
export declare function inspectCommand(id: string | undefined, opts: InspectOptions, ctx: Ctx): Promise<number>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { filterFor } from '@oortmem/claude-code';
|
|
2
|
+
import { activation, localGrant, } from '@oortmem/core';
|
|
3
|
+
import { uiFor } from '../ui.js';
|
|
4
|
+
import { Refusal, echo, age, clip, findMemory, fixed, scopeLabel, shapeLinks, short, withoutEmbedding, withWired, } from '../util.js';
|
|
5
|
+
const link = (m) => ({
|
|
6
|
+
id: m.id,
|
|
7
|
+
validFrom: m.validFrom,
|
|
8
|
+
validTo: m.validTo,
|
|
9
|
+
expiredAt: m.expiredAt,
|
|
10
|
+
endReason: m.endReason,
|
|
11
|
+
text: m.text,
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* The supersede chain through `m`, oldest first: `supersedes` back,
|
|
15
|
+
* `supersededBy` forward. Links outside `filter` (another project's memory,
|
|
16
|
+
* from before observe kept chains in one scope) are walked through but not
|
|
17
|
+
* returned; `hidden` counts them.
|
|
18
|
+
*/
|
|
19
|
+
export async function supersedeChain(store, m, filter) {
|
|
20
|
+
const grant = localGrant(filter);
|
|
21
|
+
const seen = new Set([m.id]);
|
|
22
|
+
let hidden = 0;
|
|
23
|
+
const walk = async (start, next, add) => {
|
|
24
|
+
for (let id = start; id !== null && !seen.has(id);) {
|
|
25
|
+
seen.add(id);
|
|
26
|
+
// The store answers an unknown and an out-of-scope id alike: the chain
|
|
27
|
+
// continues where this view cannot follow, so the walk stops there.
|
|
28
|
+
const x = await store.get(grant, id);
|
|
29
|
+
if (x === null) {
|
|
30
|
+
hidden++;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
add(link(x));
|
|
34
|
+
id = next(x);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const back = [];
|
|
38
|
+
const forward = [];
|
|
39
|
+
await walk(m.supersedes, (x) => x.supersedes, (l) => back.unshift(l));
|
|
40
|
+
await walk(m.supersededBy, (x) => x.supersededBy, (l) => forward.push(l));
|
|
41
|
+
return { links: [...back, link(m), ...forward], hidden };
|
|
42
|
+
}
|
|
43
|
+
function limitOf(raw) {
|
|
44
|
+
if (raw === undefined)
|
|
45
|
+
return 50;
|
|
46
|
+
const n = Number(raw);
|
|
47
|
+
if (!Number.isInteger(n) || n <= 0)
|
|
48
|
+
throw new Refusal(`--limit ${echo(raw)} is not a positive integer.`);
|
|
49
|
+
return n;
|
|
50
|
+
}
|
|
51
|
+
/** `oort inspect [<id>] [--json] [--limit n]` */
|
|
52
|
+
export async function inspectCommand(id, opts, ctx) {
|
|
53
|
+
const limit = limitOf(opts.limit);
|
|
54
|
+
return withWired(ctx, async (w) => {
|
|
55
|
+
const now = new Date();
|
|
56
|
+
const filter = filterFor(w.scope);
|
|
57
|
+
if (id === undefined) {
|
|
58
|
+
const current = (await w.store.list(localGrant(filter))).slice(0, limit);
|
|
59
|
+
if (opts.json === true) {
|
|
60
|
+
const shaped = await Promise.all(current.map((m) => shapeLinks(w.store, filter, m)));
|
|
61
|
+
ctx.io.out(`${JSON.stringify(shaped.map(withoutEmbedding), null, 2)}\n`);
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
const ui = uiFor(ctx.io);
|
|
65
|
+
const rows = current.map((m) => [
|
|
66
|
+
short(m.id),
|
|
67
|
+
m.type,
|
|
68
|
+
ui.tier(m.tier),
|
|
69
|
+
fixed(activation(m.accessLog, m.salience, now, w.config.activation)),
|
|
70
|
+
age(m.validFrom, now),
|
|
71
|
+
clip(m.text, 60),
|
|
72
|
+
]);
|
|
73
|
+
const n = current.length;
|
|
74
|
+
ctx.io.out(ui.header('inspect', `${n} current ${n === 1 ? 'memory' : 'memories'}`, scopeLabel(w)));
|
|
75
|
+
ctx.io.out(ui.table(['id', 'type', 'tier', 'activation', 'age', 'text'], rows));
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
const m = await findMemory(w.store, filter, id);
|
|
79
|
+
const { links: chain, hidden } = await supersedeChain(w.store, m, filter);
|
|
80
|
+
if (opts.json === true) {
|
|
81
|
+
const memory = withoutEmbedding(await shapeLinks(w.store, filter, m));
|
|
82
|
+
const out = { memory, chain, hiddenLinks: hidden };
|
|
83
|
+
ctx.io.out(`${JSON.stringify(out, null, 2)}\n`);
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
const a = activation(m.accessLog, m.salience, now, w.config.activation);
|
|
87
|
+
const ui = uiFor(ctx.io);
|
|
88
|
+
ctx.io.out(ui.header('inspect', short(m.id), scopeLabel(w)));
|
|
89
|
+
ctx.io.out(ui.kv([
|
|
90
|
+
['id', m.id],
|
|
91
|
+
['type', m.type],
|
|
92
|
+
['tier', `${ui.tier(m.tier)}${m.pinned ? ' (pinned)' : ''}`],
|
|
93
|
+
['scope', JSON.stringify(m.scope)],
|
|
94
|
+
['salience', fixed(m.salience)],
|
|
95
|
+
['activation', `${fixed(a)} (${m.accessLog.count} accesses)`],
|
|
96
|
+
['valid', `${m.validFrom} → ${m.validTo ?? 'open'}`],
|
|
97
|
+
['expired', `${m.expiredAt ?? 'no'}${m.endReason !== null ? ` (${m.endReason})` : ''}`],
|
|
98
|
+
['confirmed', m.lastConfirmedAt ?? 'never'],
|
|
99
|
+
['created', m.createdAt],
|
|
100
|
+
['updated', m.updatedAt],
|
|
101
|
+
[
|
|
102
|
+
'source',
|
|
103
|
+
`${m.source.harness}${m.source.sessionId !== undefined ? ` session ${m.source.sessionId}` : ''}${m.source.turnRef !== undefined ? ` turn ${m.source.turnRef}` : ''}`,
|
|
104
|
+
],
|
|
105
|
+
['embedding', m.embedding !== undefined ? `${m.embedding.length} dims` : 'none'],
|
|
106
|
+
['stub', m.stub],
|
|
107
|
+
['entities', m.entities.join(', ') || '-'],
|
|
108
|
+
]));
|
|
109
|
+
ctx.io.out(`\n${m.text}\n\n${ui.bold('decisions')}\n`);
|
|
110
|
+
ctx.io.out(ui.table(['stage', 'question', 'answer', 'p'], m.source.decisions.map((d) => [d.stage, d.question, d.answer, fixed(d.probability, 3)])));
|
|
111
|
+
ctx.io.out(`\n${ui.bold('supersede chain')} ${ui.dim('(oldest first)')}\n`);
|
|
112
|
+
ctx.io.out(ui.table(['', 'id', 'valid from', 'valid to', 'end', 'text'], chain.map((c) => [
|
|
113
|
+
c.id === m.id ? '*' : '',
|
|
114
|
+
short(c.id),
|
|
115
|
+
c.validFrom,
|
|
116
|
+
c.validTo ?? 'open',
|
|
117
|
+
c.endReason ?? '-',
|
|
118
|
+
clip(c.text, 60),
|
|
119
|
+
])));
|
|
120
|
+
if (hidden > 0) {
|
|
121
|
+
ctx.io.out(`${hidden} linked ${hidden === 1 ? 'memory' : 'memories'} in another scope (not shown)\n`);
|
|
122
|
+
}
|
|
123
|
+
return 0;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=inspect.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Ctx } from '../util.js';
|
|
2
|
+
export declare const HOW_TO_INSTALL: string;
|
|
3
|
+
/** True when `p` lies in a directory a package runner uses for throwaway installs. */
|
|
4
|
+
export declare function ephemeral(p: string): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* The command written into settings.json: the realpath of this package's
|
|
7
|
+
* executable `dist/main.js`, and only when the CLI was started as that file.
|
|
8
|
+
* Anything else (`node src/...`, `node -e`, a package runner's cache) would
|
|
9
|
+
* write a hook that breaks once the process is gone, so it is refused.
|
|
10
|
+
*/
|
|
11
|
+
export declare function hookCommandPath(entry: string | undefined, expected?: string): string;
|
|
12
|
+
/** The Claude Code settings file the hooks go in: user scope, or `<cwd>/.claude` with `project`. */
|
|
13
|
+
export declare function settingsFile(project: boolean, ctx: Ctx): string;
|
|
14
|
+
/** `oort install claude-code [--project]` / `oort uninstall claude-code [--project]` */
|
|
15
|
+
export declare function installCommand(action: 'install' | 'uninstall', harness: string, opts: {
|
|
16
|
+
project?: boolean;
|
|
17
|
+
}, ctx: Ctx): Promise<number>;
|
|
18
|
+
/** This process runs from a package runner's cache (`npx oortmem`, `pnpm dlx`). */
|
|
19
|
+
export declare function fromPackageCache(): boolean;
|
|
20
|
+
/** The global `oortmem` install `oort setup` offers when it runs under npx. */
|
|
21
|
+
export interface GlobalInstall {
|
|
22
|
+
/** The realpath of the globally installed bin, when there is a usable one. */
|
|
23
|
+
bin(): string | undefined;
|
|
24
|
+
/** Runs `npm i -g oortmem` with the terminal attached; resolves its exit code. */
|
|
25
|
+
install(): Promise<number>;
|
|
26
|
+
}
|
|
27
|
+
/** `npm i -g oortmem`, and `<npm prefix -g>/bin/oort` as the stable path (not on Windows yet). */
|
|
28
|
+
export declare function npmGlobalInstall(env: Record<string, string | undefined>): GlobalInstall;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as os from 'node:os';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import { HOOK_EVENTS, installHooks, uninstallHooks } from '@oortmem/claude-code';
|
|
6
|
+
import { binPath } from '../io.js';
|
|
7
|
+
import { uiFor } from '../ui.js';
|
|
8
|
+
import { Refusal, echo } from '../util.js';
|
|
9
|
+
// pnpm 11 removed `pnpm link --global`; `pnpm add -g .` links the package directory instead.
|
|
10
|
+
export const HOW_TO_INSTALL = 'Install the CLI globally first, with `npm i -g oortmem` (from the oort repo: ' +
|
|
11
|
+
'`pnpm build && cd packages/cli && pnpm add -g .`), then run this again as `oort`.';
|
|
12
|
+
/** True when `p` lies in a directory a package runner uses for throwaway installs. */
|
|
13
|
+
export function ephemeral(p) {
|
|
14
|
+
const segments = p.split(path.sep);
|
|
15
|
+
if (segments.includes('_npx') || segments.includes('dlx'))
|
|
16
|
+
return true;
|
|
17
|
+
const tmp = fs.realpathSync(os.tmpdir());
|
|
18
|
+
return p === tmp || p.startsWith(tmp + path.sep);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The command written into settings.json: the realpath of this package's
|
|
22
|
+
* executable `dist/main.js`, and only when the CLI was started as that file.
|
|
23
|
+
* Anything else (`node src/...`, `node -e`, a package runner's cache) would
|
|
24
|
+
* write a hook that breaks once the process is gone, so it is refused.
|
|
25
|
+
*/
|
|
26
|
+
export function hookCommandPath(entry, expected = binPath()) {
|
|
27
|
+
if (entry === undefined)
|
|
28
|
+
throw new Refusal(`oort is not running as its bin. ${HOW_TO_INSTALL}`);
|
|
29
|
+
let real;
|
|
30
|
+
let bin;
|
|
31
|
+
try {
|
|
32
|
+
real = fs.realpathSync(entry);
|
|
33
|
+
bin = fs.realpathSync(expected);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
throw new Refusal(`oort is not running as its bin. ${HOW_TO_INSTALL}`);
|
|
37
|
+
}
|
|
38
|
+
if (real !== bin)
|
|
39
|
+
throw new Refusal(`oort is not running as its bin (${real}). ${HOW_TO_INSTALL}`);
|
|
40
|
+
if (ephemeral(real)) {
|
|
41
|
+
throw new Refusal(`oort is running from a temporary package cache (${real}). ${HOW_TO_INSTALL}`);
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
fs.accessSync(real, fs.constants.X_OK);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
throw new Refusal(`${real} is not executable. ${HOW_TO_INSTALL}`);
|
|
48
|
+
}
|
|
49
|
+
return real;
|
|
50
|
+
}
|
|
51
|
+
/** The Claude Code settings file the hooks go in: user scope, or `<cwd>/.claude` with `project`. */
|
|
52
|
+
export function settingsFile(project, ctx) {
|
|
53
|
+
if (project)
|
|
54
|
+
return path.join(ctx.io.cwd, '.claude', 'settings.json');
|
|
55
|
+
return path.join(ctx.io.env['HOME'] || os.homedir(), '.claude', 'settings.json');
|
|
56
|
+
}
|
|
57
|
+
/** `oort install claude-code [--project]` / `oort uninstall claude-code [--project]` */
|
|
58
|
+
export async function installCommand(action, harness, opts, ctx) {
|
|
59
|
+
if (harness !== 'claude-code') {
|
|
60
|
+
throw new Refusal(`unknown harness ${echo(harness)}; Phase 1 supports claude-code.`);
|
|
61
|
+
}
|
|
62
|
+
const cmd = hookCommandPath(ctx.io.entry);
|
|
63
|
+
const file = settingsFile(opts.project === true, ctx);
|
|
64
|
+
const events = HOOK_EVENTS.join(', ');
|
|
65
|
+
ctx.io.out(uiFor(ctx.io).header(`${action} claude-code`, 'hooks'));
|
|
66
|
+
if (action === 'install') {
|
|
67
|
+
const { changed } = installHooks(file, cmd);
|
|
68
|
+
ctx.io.out(changed
|
|
69
|
+
? `Installed Oort hooks (${events}) in ${file}:\n${HOOK_EVENTS.map((e) => ` ${cmd} hook ${e}`).join('\n')}\n`
|
|
70
|
+
: `Oort hooks are already installed in ${file}; nothing changed.\n`);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
const { changed } = uninstallHooks(file, cmd);
|
|
74
|
+
ctx.io.out(changed
|
|
75
|
+
? `Removed Oort hooks (${events}) from ${file}.\n`
|
|
76
|
+
: `No Oort hooks found in ${file}; nothing changed.\n`);
|
|
77
|
+
}
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
/** This process runs from a package runner's cache (`npx oortmem`, `pnpm dlx`). */
|
|
81
|
+
export function fromPackageCache() {
|
|
82
|
+
try {
|
|
83
|
+
return ephemeral(fs.realpathSync(binPath()));
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** `npm i -g oortmem`, and `<npm prefix -g>/bin/oort` as the stable path (not on Windows yet). */
|
|
90
|
+
export function npmGlobalInstall(env) {
|
|
91
|
+
const childEnv = env;
|
|
92
|
+
return {
|
|
93
|
+
bin() {
|
|
94
|
+
if (process.platform === 'win32')
|
|
95
|
+
return undefined;
|
|
96
|
+
const r = spawnSync('npm', ['prefix', '-g'], {
|
|
97
|
+
encoding: 'utf8',
|
|
98
|
+
env: childEnv,
|
|
99
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
100
|
+
timeout: 30_000,
|
|
101
|
+
});
|
|
102
|
+
if (r.status !== 0 || typeof r.stdout !== 'string')
|
|
103
|
+
return undefined;
|
|
104
|
+
const link = path.join(r.stdout.trim(), 'bin', 'oort');
|
|
105
|
+
try {
|
|
106
|
+
const real = hookCommandPath(link, link);
|
|
107
|
+
return /[\\/]oortmem[\\/]dist[\\/]main\.js$/.test(real) ? real : undefined;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
install() {
|
|
114
|
+
return new Promise((resolve) => {
|
|
115
|
+
spawn('npm', ['i', '-g', 'oortmem'], { stdio: 'inherit', env: childEnv })
|
|
116
|
+
.once('error', () => resolve(1))
|
|
117
|
+
.once('close', (code) => resolve(code ?? 1));
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=install.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Provider } from '../keychain.js';
|
|
2
|
+
import { type Ctx } from '../util.js';
|
|
3
|
+
/**
|
|
4
|
+
* `oort keys set <provider>`: the value is prompted for with the input hidden (by `security` on
|
|
5
|
+
* macOS, by us for credentials.json), so it never touches argv or the terminal history.
|
|
6
|
+
*/
|
|
7
|
+
export declare function keysSetCommand(arg: string, ctx: Ctx): Promise<number>;
|
|
8
|
+
/** `oort keys rm <provider>`. */
|
|
9
|
+
export declare function keysRmCommand(arg: string, ctx: Ctx): Promise<number>;
|
|
10
|
+
export type KeySource = 'env' | 'keychain' | 'missing';
|
|
11
|
+
/** Where `provider`'s key would come from (env first, then the Keychain); never the value. */
|
|
12
|
+
export declare function keySource(provider: Provider, ctx: Ctx): Promise<KeySource>;
|
|
13
|
+
/** `oort keys list [--json]`: where each key would come from; presence only, never a value or prefix. */
|
|
14
|
+
export declare function keysListCommand(opts: {
|
|
15
|
+
json?: boolean;
|
|
16
|
+
}, ctx: Ctx): Promise<number>;
|