@nxuss/lemma 1.18.1 → 1.18.2
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/dist/cjs/mcp/tools/context-tools.d.ts.map +1 -1
- package/dist/cjs/mcp/tools/context-tools.js +11 -2
- package/dist/cjs/mcp/tools/context-tools.js.map +1 -1
- package/dist/cjs/mcp/tools/squeeze-cache.d.ts.map +1 -1
- package/dist/cjs/mcp/tools/squeeze-cache.js +2 -0
- package/dist/cjs/mcp/tools/squeeze-cache.js.map +1 -1
- package/dist/cjs/mcp/tools/workspace.d.ts.map +1 -1
- package/dist/cjs/mcp/tools/workspace.js +13 -1
- package/dist/cjs/mcp/tools/workspace.js.map +1 -1
- package/dist/cjs/mcp/tools.js +10 -0
- package/dist/cjs/mcp/tools.js.map +1 -1
- package/dist/cjs/mcp/utils.d.ts +22 -2
- package/dist/cjs/mcp/utils.d.ts.map +1 -1
- package/dist/cjs/mcp/utils.js +98 -10
- package/dist/cjs/mcp/utils.js.map +1 -1
- package/dist/cjs/utils/ExecAuditLog.d.ts +27 -0
- package/dist/cjs/utils/ExecAuditLog.d.ts.map +1 -0
- package/dist/cjs/utils/ExecAuditLog.js +53 -0
- package/dist/cjs/utils/ExecAuditLog.js.map +1 -0
- package/dist/esm/mcp/tools/context-tools.d.ts.map +1 -1
- package/dist/esm/mcp/tools/context-tools.js +11 -2
- package/dist/esm/mcp/tools/context-tools.js.map +1 -1
- package/dist/esm/mcp/tools/squeeze-cache.d.ts.map +1 -1
- package/dist/esm/mcp/tools/squeeze-cache.js +3 -1
- package/dist/esm/mcp/tools/squeeze-cache.js.map +1 -1
- package/dist/esm/mcp/tools/workspace.d.ts.map +1 -1
- package/dist/esm/mcp/tools/workspace.js +13 -1
- package/dist/esm/mcp/tools/workspace.js.map +1 -1
- package/dist/esm/mcp/tools.js +11 -1
- package/dist/esm/mcp/tools.js.map +1 -1
- package/dist/esm/mcp/utils.d.ts +22 -2
- package/dist/esm/mcp/utils.d.ts.map +1 -1
- package/dist/esm/mcp/utils.js +97 -10
- package/dist/esm/mcp/utils.js.map +1 -1
- package/dist/esm/utils/ExecAuditLog.d.ts +27 -0
- package/dist/esm/utils/ExecAuditLog.d.ts.map +1 -0
- package/dist/esm/utils/ExecAuditLog.js +45 -0
- package/dist/esm/utils/ExecAuditLog.js.map +1 -0
- package/package.json +3 -1
- package/sdks/crewai/dist/crewai/src/index.d.ts +131 -0
- package/sdks/crewai/dist/crewai/src/index.js +325 -0
- package/sdks/crewai/dist/crewai/src/memory.d.ts +36 -0
- package/sdks/crewai/dist/crewai/src/memory.js +61 -0
- package/sdks/crewai/dist/ts/src/client.d.ts +39 -0
- package/sdks/crewai/dist/ts/src/client.js +124 -0
- package/sdks/crewai/dist/ts/src/errors.d.ts +19 -0
- package/sdks/crewai/dist/ts/src/errors.js +18 -0
- package/sdks/crewai/dist/ts/src/evidence.d.ts +30 -0
- package/sdks/crewai/dist/ts/src/evidence.js +175 -0
- package/sdks/crewai/dist/ts/src/index.d.ts +4 -0
- package/sdks/crewai/dist/ts/src/index.js +27 -0
- package/sdks/crewai/dist/ts/src/types.d.ts +112 -0
- package/sdks/crewai/dist/ts/src/types.js +11 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LemmaClient = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
const DEFAULT_BASE_URL = 'https://api.lemma.dev/v2';
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 10000;
|
|
7
|
+
const DEFAULT_MAX_RETRIES = 3;
|
|
8
|
+
/** Cap for exponential backoff on transient (network/5xx) failures. */
|
|
9
|
+
const MAX_BACKOFF_MS = 8000;
|
|
10
|
+
function defaultSleep(ms) {
|
|
11
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
12
|
+
}
|
|
13
|
+
function isRetryableStatus(status) {
|
|
14
|
+
return status === 429 || status >= 500;
|
|
15
|
+
}
|
|
16
|
+
/** LemmaClient never logs or exposes the raw key past construction. */
|
|
17
|
+
function maskKey(key) {
|
|
18
|
+
if (key.length <= 12)
|
|
19
|
+
return '***';
|
|
20
|
+
return `${key.slice(0, 8)}...${key.slice(-4)}`;
|
|
21
|
+
}
|
|
22
|
+
class LemmaClient {
|
|
23
|
+
constructor(options) {
|
|
24
|
+
if (!options.apiKey) {
|
|
25
|
+
throw new errors_1.LemmaSdkError({ code: 'invalid_request', message: 'apiKey is required' });
|
|
26
|
+
}
|
|
27
|
+
this.apiKey = options.apiKey;
|
|
28
|
+
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '');
|
|
29
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
30
|
+
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
31
|
+
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
32
|
+
this.sleep = options.sleep ?? defaultSleep;
|
|
33
|
+
}
|
|
34
|
+
/** Store a memory: claims + the evidence they're grounded in (see `collectEvidence`). */
|
|
35
|
+
async remember(request) {
|
|
36
|
+
return this.request('POST', '/memories', request);
|
|
37
|
+
}
|
|
38
|
+
/** Search; every result comes back with a verified `state`, never a guess. */
|
|
39
|
+
async recall(request) {
|
|
40
|
+
return this.request('POST', '/recall', request);
|
|
41
|
+
}
|
|
42
|
+
/** Batch-resolve freshness for known ids without a search round-trip. */
|
|
43
|
+
async verify(request) {
|
|
44
|
+
return this.request('POST', '/verify', request);
|
|
45
|
+
}
|
|
46
|
+
/** Permanently forget a memory, scoped to your tenant. */
|
|
47
|
+
async forget(id) {
|
|
48
|
+
return this.request('DELETE', `/memories/${encodeURIComponent(id)}`);
|
|
49
|
+
}
|
|
50
|
+
/** Stateless secret-scrubbing pass over text or a serialized tool-call. */
|
|
51
|
+
async scrub(request) {
|
|
52
|
+
return this.request('POST', '/scrub', request);
|
|
53
|
+
}
|
|
54
|
+
/** Real, counted usage for your tenant — never an estimate. */
|
|
55
|
+
async usage() {
|
|
56
|
+
return this.request('GET', '/usage');
|
|
57
|
+
}
|
|
58
|
+
/** Masked — never prints the raw key, including via `console.log`. */
|
|
59
|
+
toString() {
|
|
60
|
+
return `LemmaClient(baseUrl=${this.baseUrl}, apiKey=${maskKey(this.apiKey)})`;
|
|
61
|
+
}
|
|
62
|
+
/** Node's console.log/util.inspect path — masked for the same reason as toString(). */
|
|
63
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
64
|
+
return this.toString();
|
|
65
|
+
}
|
|
66
|
+
async request(method, path, body) {
|
|
67
|
+
let attempt = 0;
|
|
68
|
+
while (true) {
|
|
69
|
+
const controller = new AbortController();
|
|
70
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
71
|
+
let res;
|
|
72
|
+
try {
|
|
73
|
+
res = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
74
|
+
method,
|
|
75
|
+
headers: {
|
|
76
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
},
|
|
79
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
80
|
+
signal: controller.signal,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
const isAbort = err instanceof Error && err.name === 'AbortError';
|
|
86
|
+
if (attempt < this.maxRetries) {
|
|
87
|
+
await this.sleep(backoffMs(attempt));
|
|
88
|
+
attempt++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
throw new errors_1.LemmaSdkError({
|
|
92
|
+
code: isAbort ? 'timeout' : 'network_error',
|
|
93
|
+
message: isAbort ? `request timed out after ${this.timeoutMs}ms` : `network error: ${err.message}`,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
clearTimeout(timer);
|
|
97
|
+
if (res.ok) {
|
|
98
|
+
return (await res.json());
|
|
99
|
+
}
|
|
100
|
+
const bodyJson = (await res.json().catch(() => null));
|
|
101
|
+
const apiError = bodyJson?.error;
|
|
102
|
+
if (isRetryableStatus(res.status) && attempt < this.maxRetries) {
|
|
103
|
+
const retryAfterHeader = res.headers.get('Retry-After');
|
|
104
|
+
const waitMs = retryAfterHeader ? Number(retryAfterHeader) * 1000 : backoffMs(attempt);
|
|
105
|
+
await this.sleep(Number.isFinite(waitMs) && waitMs > 0 ? waitMs : backoffMs(attempt));
|
|
106
|
+
attempt++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
throw new errors_1.LemmaSdkError({
|
|
110
|
+
code: apiError?.code ?? 'internal',
|
|
111
|
+
message: apiError?.message ?? `request failed with status ${res.status}`,
|
|
112
|
+
status: res.status,
|
|
113
|
+
field: apiError?.field,
|
|
114
|
+
retryAfter: res.headers.get('Retry-After') ? Number(res.headers.get('Retry-After')) : undefined,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.LemmaClient = LemmaClient;
|
|
120
|
+
/** Exponential backoff (200ms, 400ms, 800ms, ...) capped at MAX_BACKOFF_MS. */
|
|
121
|
+
function backoffMs(attempt) {
|
|
122
|
+
return Math.min(200 * 2 ** attempt, MAX_BACKOFF_MS);
|
|
123
|
+
}
|
|
124
|
+
exports.default = LemmaClient;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { LemmaErrorCode } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Typed error thrown by every LemmaClient method. Carries the API's stable
|
|
4
|
+
* `code` so callers can branch on it instead of parsing `message`.
|
|
5
|
+
*/
|
|
6
|
+
export declare class LemmaSdkError extends Error {
|
|
7
|
+
readonly code: LemmaErrorCode | 'network_error' | 'timeout';
|
|
8
|
+
readonly status?: number;
|
|
9
|
+
readonly field?: string;
|
|
10
|
+
/** Present on `rate_limited` — seconds the server asked the client to wait. */
|
|
11
|
+
readonly retryAfter?: number;
|
|
12
|
+
constructor(opts: {
|
|
13
|
+
code: LemmaErrorCode | 'network_error' | 'timeout';
|
|
14
|
+
message: string;
|
|
15
|
+
status?: number;
|
|
16
|
+
field?: string;
|
|
17
|
+
retryAfter?: number;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LemmaSdkError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Typed error thrown by every LemmaClient method. Carries the API's stable
|
|
6
|
+
* `code` so callers can branch on it instead of parsing `message`.
|
|
7
|
+
*/
|
|
8
|
+
class LemmaSdkError extends Error {
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
super(opts.message);
|
|
11
|
+
this.name = 'LemmaSdkError';
|
|
12
|
+
this.code = opts.code;
|
|
13
|
+
this.status = opts.status;
|
|
14
|
+
this.field = opts.field;
|
|
15
|
+
this.retryAfter = opts.retryAfter;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.LemmaSdkError = LemmaSdkError;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Evidence } from './types';
|
|
2
|
+
/** sha256 of a whole file's utf8 content. `'MISSING'` if it can't be read. */
|
|
3
|
+
export declare function hashFile(rootDir: string, relPath: string): string;
|
|
4
|
+
/** Token-stream normalization: strips comments/whitespace, keeps semantics. */
|
|
5
|
+
export declare function normalizeForFreshness(source: string): string;
|
|
6
|
+
/** Raw + normalized sha256 pair for one symbol. `'MISSING'`/`'MISSING'` if unreadable or not found. */
|
|
7
|
+
export declare function hashSymbol(rootDir: string, relPath: string, symbolName: string): {
|
|
8
|
+
raw: string;
|
|
9
|
+
normalized: string;
|
|
10
|
+
};
|
|
11
|
+
export interface CollectEvidenceOptions {
|
|
12
|
+
/** Directory relative paths are resolved against. Defaults to `process.cwd()`. */
|
|
13
|
+
rootDir?: string;
|
|
14
|
+
/** Relative file paths to hash whole. */
|
|
15
|
+
files?: string[];
|
|
16
|
+
/** Symbols to hash (raw + normalized), one file per symbol. */
|
|
17
|
+
symbols?: Array<{
|
|
18
|
+
filePath: string;
|
|
19
|
+
symbolName: string;
|
|
20
|
+
}>;
|
|
21
|
+
branch?: string;
|
|
22
|
+
commit?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Builds an `Evidence` bundle the way a client is expected to: hash the files
|
|
26
|
+
* and symbols it's about to make claims about, right before sending them.
|
|
27
|
+
* Identical algorithm to the local MCP Brain's `LocalFsResolver` — see the
|
|
28
|
+
* module doc.
|
|
29
|
+
*/
|
|
30
|
+
export declare function collectEvidence(options: CollectEvidenceOptions): Evidence;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.hashFile = hashFile;
|
|
40
|
+
exports.normalizeForFreshness = normalizeForFreshness;
|
|
41
|
+
exports.hashSymbol = hashSymbol;
|
|
42
|
+
exports.collectEvidence = collectEvidence;
|
|
43
|
+
/**
|
|
44
|
+
* Client-side evidence hashing.
|
|
45
|
+
*
|
|
46
|
+
* Ports the exact algorithm `LocalFsResolver` and `getSymbolSurgicalContext`
|
|
47
|
+
* use in the main repo (`src/subconscious/freshness/LocalFsResolver.ts`,
|
|
48
|
+
* `src/utils/SymbolSurgicalContext.ts`), so a memory stored via this SDK and
|
|
49
|
+
* one stored via the local MCP Brain hash identically. Kept honest by
|
|
50
|
+
* `tests/unit/lane-g-evidence.test.ts`, which imports both implementations on
|
|
51
|
+
* the same fixture file and asserts the hashes match byte for byte — this is
|
|
52
|
+
* not a claim taken on faith.
|
|
53
|
+
*
|
|
54
|
+
* A file that can't be read, or a symbol that can't be found, hashes to the
|
|
55
|
+
* literal string `'MISSING'` — same fail-loud convention as `LocalFsResolver`
|
|
56
|
+
* — rather than silently omitting the claim.
|
|
57
|
+
*/
|
|
58
|
+
const fs_1 = __importDefault(require("fs"));
|
|
59
|
+
const path_1 = __importDefault(require("path"));
|
|
60
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
61
|
+
const ts = __importStar(require("typescript"));
|
|
62
|
+
function sha256(content) {
|
|
63
|
+
return crypto_1.default.createHash('sha256').update(content, 'utf8').digest('hex');
|
|
64
|
+
}
|
|
65
|
+
function resolvePath(rootDir, relPath) {
|
|
66
|
+
return path_1.default.isAbsolute(relPath) ? relPath : path_1.default.resolve(rootDir, relPath);
|
|
67
|
+
}
|
|
68
|
+
/** sha256 of a whole file's utf8 content. `'MISSING'` if it can't be read. */
|
|
69
|
+
function hashFile(rootDir, relPath) {
|
|
70
|
+
const abs = resolvePath(rootDir, relPath);
|
|
71
|
+
try {
|
|
72
|
+
return sha256(fs_1.default.readFileSync(abs, 'utf8'));
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return 'MISSING';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Same traversal `SymbolSurgicalContext.findSymbolNode` uses: a top-level
|
|
80
|
+
* function/class/interface/type-alias/enum declaration named `name`, or the
|
|
81
|
+
* variable statement declaring an identifier named `name`.
|
|
82
|
+
*/
|
|
83
|
+
function findSymbolNode(sourceFile, name) {
|
|
84
|
+
let targetNode = null;
|
|
85
|
+
function walk(node) {
|
|
86
|
+
if (targetNode)
|
|
87
|
+
return;
|
|
88
|
+
if (ts.isFunctionDeclaration(node) && node.name && node.name.text === name) {
|
|
89
|
+
targetNode = node;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (ts.isClassDeclaration(node) && node.name && node.name.text === name) {
|
|
93
|
+
targetNode = node;
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (ts.isInterfaceDeclaration(node) && node.name && node.name.text === name) {
|
|
97
|
+
targetNode = node;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (ts.isTypeAliasDeclaration(node) && node.name && node.name.text === name) {
|
|
101
|
+
targetNode = node;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (ts.isEnumDeclaration(node) && node.name && node.name.text === name) {
|
|
105
|
+
targetNode = node;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (ts.isVariableStatement(node)) {
|
|
109
|
+
for (const decl of node.declarationList.declarations) {
|
|
110
|
+
if (ts.isIdentifier(decl.name) && decl.name.text === name) {
|
|
111
|
+
targetNode = node;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
ts.forEachChild(node, walk);
|
|
117
|
+
}
|
|
118
|
+
walk(sourceFile);
|
|
119
|
+
return targetNode;
|
|
120
|
+
}
|
|
121
|
+
/** Token-stream normalization: strips comments/whitespace, keeps semantics. */
|
|
122
|
+
function normalizeForFreshness(source) {
|
|
123
|
+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, /* skipTrivia */ true, ts.LanguageVariant.Standard, source);
|
|
124
|
+
const tokens = [];
|
|
125
|
+
let kind = scanner.scan();
|
|
126
|
+
while (kind !== ts.SyntaxKind.EndOfFileToken) {
|
|
127
|
+
tokens.push(scanner.getTokenText());
|
|
128
|
+
kind = scanner.scan();
|
|
129
|
+
}
|
|
130
|
+
return tokens.join(' ');
|
|
131
|
+
}
|
|
132
|
+
/** Raw + normalized sha256 pair for one symbol. `'MISSING'`/`'MISSING'` if unreadable or not found. */
|
|
133
|
+
function hashSymbol(rootDir, relPath, symbolName) {
|
|
134
|
+
const abs = resolvePath(rootDir, relPath);
|
|
135
|
+
try {
|
|
136
|
+
const src = fs_1.default.readFileSync(abs, 'utf8');
|
|
137
|
+
const sf = ts.createSourceFile(abs, src, ts.ScriptTarget.Latest, true);
|
|
138
|
+
const node = findSymbolNode(sf, symbolName);
|
|
139
|
+
if (!node)
|
|
140
|
+
return { raw: 'MISSING', normalized: 'MISSING' };
|
|
141
|
+
const implementation = node.getText();
|
|
142
|
+
return { raw: sha256(implementation), normalized: sha256(normalizeForFreshness(implementation)) };
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return { raw: 'MISSING', normalized: 'MISSING' };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Builds an `Evidence` bundle the way a client is expected to: hash the files
|
|
150
|
+
* and symbols it's about to make claims about, right before sending them.
|
|
151
|
+
* Identical algorithm to the local MCP Brain's `LocalFsResolver` — see the
|
|
152
|
+
* module doc.
|
|
153
|
+
*/
|
|
154
|
+
function collectEvidence(options) {
|
|
155
|
+
const rootDir = options.rootDir ?? process.cwd();
|
|
156
|
+
const files = {};
|
|
157
|
+
const symbols = {};
|
|
158
|
+
const symbolsNormalized = {};
|
|
159
|
+
for (const relPath of options.files ?? []) {
|
|
160
|
+
files[relPath] = hashFile(rootDir, relPath);
|
|
161
|
+
}
|
|
162
|
+
for (const { filePath, symbolName } of options.symbols ?? []) {
|
|
163
|
+
const key = `${filePath}::${symbolName}`;
|
|
164
|
+
const { raw, normalized } = hashSymbol(rootDir, filePath, symbolName);
|
|
165
|
+
symbols[key] = raw;
|
|
166
|
+
symbolsNormalized[key] = normalized;
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
files,
|
|
170
|
+
symbols,
|
|
171
|
+
symbolsNormalized,
|
|
172
|
+
...(options.branch ? { branch: options.branch } : {}),
|
|
173
|
+
...(options.commit ? { commit: options.commit } : {}),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.LemmaSdkError = exports.normalizeForFreshness = exports.hashSymbol = exports.hashFile = exports.collectEvidence = exports.LemmaClient = void 0;
|
|
18
|
+
var client_1 = require("./client");
|
|
19
|
+
Object.defineProperty(exports, "LemmaClient", { enumerable: true, get: function () { return client_1.LemmaClient; } });
|
|
20
|
+
var evidence_1 = require("./evidence");
|
|
21
|
+
Object.defineProperty(exports, "collectEvidence", { enumerable: true, get: function () { return evidence_1.collectEvidence; } });
|
|
22
|
+
Object.defineProperty(exports, "hashFile", { enumerable: true, get: function () { return evidence_1.hashFile; } });
|
|
23
|
+
Object.defineProperty(exports, "hashSymbol", { enumerable: true, get: function () { return evidence_1.hashSymbol; } });
|
|
24
|
+
Object.defineProperty(exports, "normalizeForFreshness", { enumerable: true, get: function () { return evidence_1.normalizeForFreshness; } });
|
|
25
|
+
var errors_1 = require("./errors");
|
|
26
|
+
Object.defineProperty(exports, "LemmaSdkError", { enumerable: true, get: function () { return errors_1.LemmaSdkError; } });
|
|
27
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire types for the Lemma /v2 API.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `src/contracts/api-v2.types.ts` and `src/contracts/evidence.ts` in the
|
|
5
|
+
* main repo (the SDK is published standalone, so it can't import from `src/`
|
|
6
|
+
* directly — this file is the SDK's own copy of the same shapes). Kept in sync
|
|
7
|
+
* by `tests/unit/lane-g-evidence.test.ts`, which imports the real contract
|
|
8
|
+
* types from the main repo and structurally checks this file against them.
|
|
9
|
+
*/
|
|
10
|
+
/** Hash of a whole file: path (as the client refers to it) → sha256 hex. */
|
|
11
|
+
export type FileHashes = Record<string, string>;
|
|
12
|
+
/** Hash pair for one symbol: key is `<path>::<symbolName>`. */
|
|
13
|
+
export type SymbolHashes = Record<string, string>;
|
|
14
|
+
export interface Evidence {
|
|
15
|
+
files: FileHashes;
|
|
16
|
+
symbols: SymbolHashes;
|
|
17
|
+
/** Symbols hashed after stripping comments/whitespace (semantic diff). */
|
|
18
|
+
symbolsNormalized: SymbolHashes;
|
|
19
|
+
branch?: string;
|
|
20
|
+
commit?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Verifiability state of a memory or claim.
|
|
24
|
+
*
|
|
25
|
+
* fresh — every tracked path hashes identical to what was stored.
|
|
26
|
+
* stale — a tracked path hashed differently (staleFiles names which).
|
|
27
|
+
* unverified — the verifier cannot prove validity (no current evidence, or a
|
|
28
|
+
* tracked path was omitted). NEVER `fresh` by guess.
|
|
29
|
+
* contradicted — two stored claims about the same subject now diverge.
|
|
30
|
+
*/
|
|
31
|
+
export type MemoryState = 'fresh' | 'stale' | 'unverified' | 'contradicted';
|
|
32
|
+
export interface MemoryClaim {
|
|
33
|
+
id: string;
|
|
34
|
+
text: string;
|
|
35
|
+
fileHashes?: FileHashes;
|
|
36
|
+
symbolHashes?: SymbolHashes;
|
|
37
|
+
symbolNormalizedHashes?: SymbolHashes;
|
|
38
|
+
}
|
|
39
|
+
export interface VerifiedResult {
|
|
40
|
+
id: string;
|
|
41
|
+
state: MemoryState;
|
|
42
|
+
staleFiles?: string[];
|
|
43
|
+
cosmeticChanges?: string[];
|
|
44
|
+
memory?: unknown;
|
|
45
|
+
}
|
|
46
|
+
export interface CreateMemoryRequest {
|
|
47
|
+
claims: Array<{
|
|
48
|
+
text: string;
|
|
49
|
+
filePaths?: string[];
|
|
50
|
+
symbols?: Array<{
|
|
51
|
+
filePath: string;
|
|
52
|
+
symbolName: string;
|
|
53
|
+
}>;
|
|
54
|
+
}>;
|
|
55
|
+
evidence: Evidence;
|
|
56
|
+
derivedFrom?: string[];
|
|
57
|
+
}
|
|
58
|
+
export interface CreateMemoryResponse {
|
|
59
|
+
id: string;
|
|
60
|
+
claims: MemoryClaim[];
|
|
61
|
+
}
|
|
62
|
+
export interface RecallRequest {
|
|
63
|
+
query: string;
|
|
64
|
+
currentEvidence?: Evidence;
|
|
65
|
+
limit?: number;
|
|
66
|
+
}
|
|
67
|
+
export interface RecallResponse {
|
|
68
|
+
results: VerifiedResult[];
|
|
69
|
+
query: string;
|
|
70
|
+
}
|
|
71
|
+
export interface VerifyRequest {
|
|
72
|
+
ids: string[];
|
|
73
|
+
currentEvidence: Evidence;
|
|
74
|
+
}
|
|
75
|
+
export interface VerifyResponse {
|
|
76
|
+
results: VerifiedResult[];
|
|
77
|
+
}
|
|
78
|
+
export interface ForgetResponse {
|
|
79
|
+
forged: string[];
|
|
80
|
+
}
|
|
81
|
+
export interface ScrubFinding {
|
|
82
|
+
type: string;
|
|
83
|
+
offset: number;
|
|
84
|
+
length: number;
|
|
85
|
+
}
|
|
86
|
+
export interface ScrubRequest {
|
|
87
|
+
text?: string;
|
|
88
|
+
toolCallJson?: string;
|
|
89
|
+
mode?: 'mask' | 'detect';
|
|
90
|
+
}
|
|
91
|
+
export interface ScrubResponse {
|
|
92
|
+
text?: string;
|
|
93
|
+
toolCallJson?: string;
|
|
94
|
+
findings: ScrubFinding[];
|
|
95
|
+
}
|
|
96
|
+
export interface UsageStat {
|
|
97
|
+
measurement: 'counted';
|
|
98
|
+
value: number;
|
|
99
|
+
}
|
|
100
|
+
export interface UsageResponse {
|
|
101
|
+
metrics: Record<string, UsageStat>;
|
|
102
|
+
excludedEvents: number;
|
|
103
|
+
}
|
|
104
|
+
export type LemmaErrorCode = 'unauthenticated' | 'invalid_request' | 'quota_exceeded' | 'not_found' | 'forbidden' | 'rate_limited' | 'not_implemented' | 'internal';
|
|
105
|
+
export interface LemmaApiErrorBody {
|
|
106
|
+
error: {
|
|
107
|
+
code: LemmaErrorCode;
|
|
108
|
+
status: number;
|
|
109
|
+
message: string;
|
|
110
|
+
field?: string;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Wire types for the Lemma /v2 API.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors `src/contracts/api-v2.types.ts` and `src/contracts/evidence.ts` in the
|
|
6
|
+
* main repo (the SDK is published standalone, so it can't import from `src/`
|
|
7
|
+
* directly — this file is the SDK's own copy of the same shapes). Kept in sync
|
|
8
|
+
* by `tests/unit/lane-g-evidence.test.ts`, which imports the real contract
|
|
9
|
+
* types from the main repo and structurally checks this file against them.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|