memorylens 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/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # MemoryLens JS/TS SDK
2
+
3
+ **Observability and debugging for AI agent memory systems.**
4
+
5
+ TypeScript SDK for instrumenting memory operations in JavaScript/TypeScript AI agents. Sends traces to the [MemoryLens](https://github.com/memorylens/memorylens) dashboard via OTLP.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install memorylens
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```typescript
16
+ import { init, instrumentWrite, instrumentRead, context } from 'memorylens';
17
+
18
+ // Initialize — sends traces to MemoryLens UI by default
19
+ init();
20
+
21
+ // Wrap your memory functions
22
+ const store = instrumentWrite(async (content: string) => {
23
+ return await db.save(content);
24
+ }, { backend: 'my_db' });
25
+
26
+ const search = instrumentRead(async (query: string) => {
27
+ return await db.search(query);
28
+ }, { backend: 'my_db' });
29
+
30
+ // Add context for session tracking
31
+ await context({ agentId: 'support-bot', sessionId: 'sess-123' }, async () => {
32
+ await store('user prefers dark mode');
33
+ const results = await search('user preferences');
34
+ });
35
+ ```
36
+
37
+ ## Auto-Instrumentation (LangChain.js)
38
+
39
+ ```typescript
40
+ import { init } from 'memorylens';
41
+ import { LangChainInstrumentor } from 'memorylens/integrations/langchain';
42
+
43
+ init();
44
+ new LangChainInstrumentor().instrument();
45
+ // All LangChain memory operations are now traced
46
+ ```
47
+
48
+ ## Configuration
49
+
50
+ ```typescript
51
+ init({
52
+ serviceName: 'my-agent',
53
+ exporter: 'otlp', // 'otlp' or 'console'
54
+ endpoint: 'http://localhost:8000/v1/traces', // MemoryLens UI ingest
55
+ captureContent: true,
56
+ sampleRate: 1.0,
57
+ });
58
+ ```
59
+
60
+ Or via environment variables:
61
+
62
+ ```bash
63
+ export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8000/v1/traces
64
+ export OTEL_SERVICE_NAME=my-agent
65
+ ```
66
+
67
+ ## View Traces
68
+
69
+ Start the MemoryLens dashboard (Python):
70
+
71
+ ```bash
72
+ pip install memorylens[ui]
73
+ memorylens ui --ingest
74
+ # Open http://localhost:8000
75
+ ```
76
+
77
+ ## License
78
+
79
+ Apache 2.0
@@ -0,0 +1,6 @@
1
+ interface Instrumentor {
2
+ instrument(options?: Record<string, unknown>): void;
3
+ uninstrument(): void;
4
+ }
5
+
6
+ export type { Instrumentor as I };
@@ -0,0 +1,6 @@
1
+ interface Instrumentor {
2
+ instrument(options?: Record<string, unknown>): void;
3
+ uninstrument(): void;
4
+ }
5
+
6
+ export type { Instrumentor as I };
@@ -0,0 +1,171 @@
1
+ export { I as Instrumentor } from './index-BZqWj0_R.mjs';
2
+
3
+ declare class Sampler {
4
+ private rate;
5
+ constructor(rate?: number);
6
+ shouldSample(): boolean;
7
+ getRate(): number;
8
+ }
9
+
10
+ type MemoryOperation = 'memory.write' | 'memory.read' | 'memory.compress' | 'memory.update';
11
+ type SpanStatus = 'ok' | 'error' | 'dropped';
12
+ declare enum ExportResult {
13
+ SUCCESS = 0,
14
+ FAILURE = 1
15
+ }
16
+ interface MemorySpan {
17
+ spanId: string;
18
+ traceId: string;
19
+ parentSpanId: string | null;
20
+ operation: MemoryOperation;
21
+ status: SpanStatus;
22
+ startTime: number;
23
+ endTime: number;
24
+ durationMs: number;
25
+ agentId: string | null;
26
+ sessionId: string | null;
27
+ userId: string | null;
28
+ inputContent: string | null;
29
+ outputContent: string | null;
30
+ attributes: Record<string, unknown>;
31
+ }
32
+ interface SpanProcessor {
33
+ onStart(span: MemorySpan): void;
34
+ onEnd(span: MemorySpan): void;
35
+ shutdown(): Promise<void>;
36
+ forceFlush(timeoutMs?: number): Promise<boolean>;
37
+ }
38
+ interface SpanExporter {
39
+ export(spans: MemorySpan[]): Promise<ExportResult>;
40
+ shutdown(): Promise<void>;
41
+ }
42
+ interface MemoryContext {
43
+ agentId?: string | null;
44
+ sessionId?: string | null;
45
+ userId?: string | null;
46
+ }
47
+
48
+ declare class MutableSpan {
49
+ readonly traceId: string;
50
+ readonly spanId: string;
51
+ readonly operation: MemoryOperation;
52
+ readonly parentSpanId: string | null;
53
+ status: SpanStatus;
54
+ readonly startTime: number;
55
+ endTime: number;
56
+ agentId: string | null;
57
+ sessionId: string | null;
58
+ userId: string | null;
59
+ inputContent: string | null;
60
+ outputContent: string | null;
61
+ attributes: Record<string, unknown>;
62
+ constructor(options: {
63
+ operation: MemoryOperation;
64
+ parentSpanId?: string | null;
65
+ agentId?: string | null;
66
+ sessionId?: string | null;
67
+ userId?: string | null;
68
+ attributes?: Record<string, unknown>;
69
+ });
70
+ setAttribute(key: string, value: unknown): void;
71
+ setContent(input?: string | null, output?: string | null): void;
72
+ setStatus(status: SpanStatus): void;
73
+ setError(error: unknown): void;
74
+ end(): MemorySpan;
75
+ }
76
+ declare class Tracer {
77
+ private name;
78
+ private provider;
79
+ constructor(name: string, provider: TracerProvider);
80
+ getName(): string;
81
+ startSpan(operation: MemoryOperation, attributes?: Record<string, unknown>): MutableSpan;
82
+ endSpan(span: MutableSpan): void;
83
+ }
84
+ declare class TracerProvider {
85
+ private static instance;
86
+ processors: SpanProcessor[];
87
+ sampler: Sampler;
88
+ serviceName: string;
89
+ static get(): TracerProvider;
90
+ static reset(): void;
91
+ addProcessor(processor: SpanProcessor): void;
92
+ getTracer(name: string): Tracer;
93
+ shutdown(): Promise<void>;
94
+ }
95
+
96
+ declare function runWithContext<T>(ctx: MemoryContext, fn: () => T): T;
97
+ declare function getCurrentContext(): MemoryContext | undefined;
98
+
99
+ declare class SimpleSpanProcessor implements SpanProcessor {
100
+ private exporter;
101
+ constructor(exporter: SpanExporter);
102
+ onStart(_span: MemorySpan): void;
103
+ onEnd(span: MemorySpan): void;
104
+ shutdown(): Promise<void>;
105
+ forceFlush(_timeoutMs?: number): Promise<boolean>;
106
+ }
107
+ declare class BatchSpanProcessor implements SpanProcessor {
108
+ private exporter;
109
+ private queue;
110
+ private maxBatchSize;
111
+ private scheduleDelayMs;
112
+ private maxQueueSize;
113
+ private timer;
114
+ private isShutdown;
115
+ constructor(exporter: SpanExporter, options?: {
116
+ maxBatchSize?: number;
117
+ scheduleDelayMs?: number;
118
+ maxQueueSize?: number;
119
+ });
120
+ onStart(_span: MemorySpan): void;
121
+ onEnd(span: MemorySpan): void;
122
+ shutdown(): Promise<void>;
123
+ forceFlush(_timeoutMs?: number): Promise<boolean>;
124
+ private flush;
125
+ private scheduleFlush;
126
+ }
127
+
128
+ declare class ConsoleExporter implements SpanExporter {
129
+ export(spans: MemorySpan[]): Promise<ExportResult>;
130
+ shutdown(): Promise<void>;
131
+ }
132
+
133
+ /**
134
+ * OTLP HTTP exporter — maps MemorySpan to OpenTelemetry protobuf-JSON format
135
+ * and ships to the MemoryLens ingest endpoint (or any OTLP-compatible backend).
136
+ */
137
+ declare class OTLPExporter implements SpanExporter {
138
+ private endpoint;
139
+ constructor(options?: {
140
+ endpoint?: string;
141
+ });
142
+ export(spans: MemorySpan[]): Promise<ExportResult>;
143
+ shutdown(): Promise<void>;
144
+ private buildOTLPPayload;
145
+ private spanToOTLP;
146
+ }
147
+
148
+ interface WrapperOptions {
149
+ captureContent?: boolean;
150
+ [key: string]: unknown;
151
+ }
152
+ type AnyAsyncFn<TArgs extends unknown[], TReturn> = (...args: TArgs) => Promise<TReturn>;
153
+ declare function instrumentWrite<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
154
+ declare function instrumentRead<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
155
+ declare function instrumentCompress<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
156
+ declare function instrumentUpdate<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
157
+
158
+ interface InitOptions {
159
+ serviceName?: string;
160
+ exporter?: 'otlp' | 'console';
161
+ endpoint?: string;
162
+ captureContent?: boolean;
163
+ sampleRate?: number;
164
+ batch?: boolean;
165
+ }
166
+ declare function init(options?: InitOptions): void;
167
+ declare function shutdown(): Promise<void>;
168
+ declare function context<T>(ctx: MemoryContext, fn: () => Promise<T>): Promise<T>;
169
+ declare function getTracer(name: string): Tracer;
170
+
171
+ export { BatchSpanProcessor, ConsoleExporter, ExportResult, type InitOptions, type MemoryContext, type MemoryOperation, type MemorySpan, MutableSpan, OTLPExporter, Sampler, SimpleSpanProcessor, type SpanExporter, type SpanProcessor, type SpanStatus, Tracer, TracerProvider, context, getCurrentContext, getTracer, init, instrumentCompress, instrumentRead, instrumentUpdate, instrumentWrite, runWithContext, shutdown };
@@ -0,0 +1,171 @@
1
+ export { I as Instrumentor } from './index-BZqWj0_R.js';
2
+
3
+ declare class Sampler {
4
+ private rate;
5
+ constructor(rate?: number);
6
+ shouldSample(): boolean;
7
+ getRate(): number;
8
+ }
9
+
10
+ type MemoryOperation = 'memory.write' | 'memory.read' | 'memory.compress' | 'memory.update';
11
+ type SpanStatus = 'ok' | 'error' | 'dropped';
12
+ declare enum ExportResult {
13
+ SUCCESS = 0,
14
+ FAILURE = 1
15
+ }
16
+ interface MemorySpan {
17
+ spanId: string;
18
+ traceId: string;
19
+ parentSpanId: string | null;
20
+ operation: MemoryOperation;
21
+ status: SpanStatus;
22
+ startTime: number;
23
+ endTime: number;
24
+ durationMs: number;
25
+ agentId: string | null;
26
+ sessionId: string | null;
27
+ userId: string | null;
28
+ inputContent: string | null;
29
+ outputContent: string | null;
30
+ attributes: Record<string, unknown>;
31
+ }
32
+ interface SpanProcessor {
33
+ onStart(span: MemorySpan): void;
34
+ onEnd(span: MemorySpan): void;
35
+ shutdown(): Promise<void>;
36
+ forceFlush(timeoutMs?: number): Promise<boolean>;
37
+ }
38
+ interface SpanExporter {
39
+ export(spans: MemorySpan[]): Promise<ExportResult>;
40
+ shutdown(): Promise<void>;
41
+ }
42
+ interface MemoryContext {
43
+ agentId?: string | null;
44
+ sessionId?: string | null;
45
+ userId?: string | null;
46
+ }
47
+
48
+ declare class MutableSpan {
49
+ readonly traceId: string;
50
+ readonly spanId: string;
51
+ readonly operation: MemoryOperation;
52
+ readonly parentSpanId: string | null;
53
+ status: SpanStatus;
54
+ readonly startTime: number;
55
+ endTime: number;
56
+ agentId: string | null;
57
+ sessionId: string | null;
58
+ userId: string | null;
59
+ inputContent: string | null;
60
+ outputContent: string | null;
61
+ attributes: Record<string, unknown>;
62
+ constructor(options: {
63
+ operation: MemoryOperation;
64
+ parentSpanId?: string | null;
65
+ agentId?: string | null;
66
+ sessionId?: string | null;
67
+ userId?: string | null;
68
+ attributes?: Record<string, unknown>;
69
+ });
70
+ setAttribute(key: string, value: unknown): void;
71
+ setContent(input?: string | null, output?: string | null): void;
72
+ setStatus(status: SpanStatus): void;
73
+ setError(error: unknown): void;
74
+ end(): MemorySpan;
75
+ }
76
+ declare class Tracer {
77
+ private name;
78
+ private provider;
79
+ constructor(name: string, provider: TracerProvider);
80
+ getName(): string;
81
+ startSpan(operation: MemoryOperation, attributes?: Record<string, unknown>): MutableSpan;
82
+ endSpan(span: MutableSpan): void;
83
+ }
84
+ declare class TracerProvider {
85
+ private static instance;
86
+ processors: SpanProcessor[];
87
+ sampler: Sampler;
88
+ serviceName: string;
89
+ static get(): TracerProvider;
90
+ static reset(): void;
91
+ addProcessor(processor: SpanProcessor): void;
92
+ getTracer(name: string): Tracer;
93
+ shutdown(): Promise<void>;
94
+ }
95
+
96
+ declare function runWithContext<T>(ctx: MemoryContext, fn: () => T): T;
97
+ declare function getCurrentContext(): MemoryContext | undefined;
98
+
99
+ declare class SimpleSpanProcessor implements SpanProcessor {
100
+ private exporter;
101
+ constructor(exporter: SpanExporter);
102
+ onStart(_span: MemorySpan): void;
103
+ onEnd(span: MemorySpan): void;
104
+ shutdown(): Promise<void>;
105
+ forceFlush(_timeoutMs?: number): Promise<boolean>;
106
+ }
107
+ declare class BatchSpanProcessor implements SpanProcessor {
108
+ private exporter;
109
+ private queue;
110
+ private maxBatchSize;
111
+ private scheduleDelayMs;
112
+ private maxQueueSize;
113
+ private timer;
114
+ private isShutdown;
115
+ constructor(exporter: SpanExporter, options?: {
116
+ maxBatchSize?: number;
117
+ scheduleDelayMs?: number;
118
+ maxQueueSize?: number;
119
+ });
120
+ onStart(_span: MemorySpan): void;
121
+ onEnd(span: MemorySpan): void;
122
+ shutdown(): Promise<void>;
123
+ forceFlush(_timeoutMs?: number): Promise<boolean>;
124
+ private flush;
125
+ private scheduleFlush;
126
+ }
127
+
128
+ declare class ConsoleExporter implements SpanExporter {
129
+ export(spans: MemorySpan[]): Promise<ExportResult>;
130
+ shutdown(): Promise<void>;
131
+ }
132
+
133
+ /**
134
+ * OTLP HTTP exporter — maps MemorySpan to OpenTelemetry protobuf-JSON format
135
+ * and ships to the MemoryLens ingest endpoint (or any OTLP-compatible backend).
136
+ */
137
+ declare class OTLPExporter implements SpanExporter {
138
+ private endpoint;
139
+ constructor(options?: {
140
+ endpoint?: string;
141
+ });
142
+ export(spans: MemorySpan[]): Promise<ExportResult>;
143
+ shutdown(): Promise<void>;
144
+ private buildOTLPPayload;
145
+ private spanToOTLP;
146
+ }
147
+
148
+ interface WrapperOptions {
149
+ captureContent?: boolean;
150
+ [key: string]: unknown;
151
+ }
152
+ type AnyAsyncFn<TArgs extends unknown[], TReturn> = (...args: TArgs) => Promise<TReturn>;
153
+ declare function instrumentWrite<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
154
+ declare function instrumentRead<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
155
+ declare function instrumentCompress<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
156
+ declare function instrumentUpdate<TArgs extends unknown[], TReturn>(fn: AnyAsyncFn<TArgs, TReturn>, options?: WrapperOptions): AnyAsyncFn<TArgs, TReturn>;
157
+
158
+ interface InitOptions {
159
+ serviceName?: string;
160
+ exporter?: 'otlp' | 'console';
161
+ endpoint?: string;
162
+ captureContent?: boolean;
163
+ sampleRate?: number;
164
+ batch?: boolean;
165
+ }
166
+ declare function init(options?: InitOptions): void;
167
+ declare function shutdown(): Promise<void>;
168
+ declare function context<T>(ctx: MemoryContext, fn: () => Promise<T>): Promise<T>;
169
+ declare function getTracer(name: string): Tracer;
170
+
171
+ export { BatchSpanProcessor, ConsoleExporter, ExportResult, type InitOptions, type MemoryContext, type MemoryOperation, type MemorySpan, MutableSpan, OTLPExporter, Sampler, SimpleSpanProcessor, type SpanExporter, type SpanProcessor, type SpanStatus, Tracer, TracerProvider, context, getCurrentContext, getTracer, init, instrumentCompress, instrumentRead, instrumentUpdate, instrumentWrite, runWithContext, shutdown };