@claiv/memory 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 +178 -0
- package/dist/client.d.ts +67 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +221 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +35 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +51 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +158 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# @claiv/memory
|
|
2
|
+
|
|
3
|
+
Official JavaScript/TypeScript SDK for the Claiv Memory API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @claiv/memory
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { ClaivClient } from '@claiv/memory';
|
|
15
|
+
|
|
16
|
+
const client = new ClaivClient({
|
|
17
|
+
apiKey: 'your-api-key',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Store a memory
|
|
21
|
+
const { event_id } = await client.ingest({
|
|
22
|
+
user_id: 'user-123',
|
|
23
|
+
type: 'message',
|
|
24
|
+
content: 'User prefers dark mode and uses VS Code',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Recall relevant memory
|
|
28
|
+
const { memory_blocks, citations, token_estimate } = await client.recall({
|
|
29
|
+
user_id: 'user-123',
|
|
30
|
+
task: 'Help the user configure their editor',
|
|
31
|
+
token_budget: 2000,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Forget memory for a user
|
|
35
|
+
const { receipt_id, deleted_counts } = await client.forget({
|
|
36
|
+
user_id: 'user-123',
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API Reference
|
|
41
|
+
|
|
42
|
+
### `new ClaivClient(options)`
|
|
43
|
+
|
|
44
|
+
| Option | Type | Default | Description |
|
|
45
|
+
|--------------|------------|--------------------------|------------------------------------|
|
|
46
|
+
| `apiKey` | `string` | *required* | API key (sent as Bearer token) |
|
|
47
|
+
| `baseUrl` | `string` | `https://api.claiv.com` | API base URL |
|
|
48
|
+
| `timeout` | `number` | `30000` | Request timeout in milliseconds |
|
|
49
|
+
| `maxRetries` | `number` | `2` | Retries on 429/5xx (0 to disable) |
|
|
50
|
+
| `fetch` | `function` | `globalThis.fetch` | Custom fetch implementation |
|
|
51
|
+
|
|
52
|
+
### Core Methods
|
|
53
|
+
|
|
54
|
+
#### `client.ingest(request): Promise<IngestResponse>`
|
|
55
|
+
|
|
56
|
+
Store a memory event.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
const result = await client.ingest({
|
|
60
|
+
user_id: 'user-123', // required
|
|
61
|
+
type: 'message', // required: 'message' | 'tool_call' | 'app_event'
|
|
62
|
+
content: 'The actual text', // required
|
|
63
|
+
thread_id: 'thread-456', // optional: scope to a conversation
|
|
64
|
+
metadata: { source: 'chat' },// optional: arbitrary metadata
|
|
65
|
+
event_time: '2025-01-01T00:00:00Z', // optional: ISO 8601
|
|
66
|
+
idempotency_key: 'unique-1', // optional: prevents duplicate ingestion
|
|
67
|
+
});
|
|
68
|
+
// result: { event_id: string, deduped: boolean }
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### `client.recall(request): Promise<RecallResponse>`
|
|
72
|
+
|
|
73
|
+
Retrieve relevant memory for a task.
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
const result = await client.recall({
|
|
77
|
+
user_id: 'user-123', // required
|
|
78
|
+
task: 'Help configure their editor', // required: what the AI is doing
|
|
79
|
+
token_budget: 2000, // required: 200–8000
|
|
80
|
+
thread_id: 'thread-456', // optional
|
|
81
|
+
scope: { project: 'claiv' }, // optional
|
|
82
|
+
});
|
|
83
|
+
// result: {
|
|
84
|
+
// system_context: string,
|
|
85
|
+
// memory_blocks: Array<{ type, content, source_ids, score }>,
|
|
86
|
+
// citations: string[],
|
|
87
|
+
// token_estimate: number,
|
|
88
|
+
// }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Memory block types: `open_loop`, `fact`, `claim`, `episode`, `chunk`.
|
|
92
|
+
|
|
93
|
+
#### `client.forget(request): Promise<ForgetResponse>`
|
|
94
|
+
|
|
95
|
+
Delete memory matching a scope.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
const result = await client.forget({
|
|
99
|
+
user_id: 'user-123', // required
|
|
100
|
+
thread_id: 'thread-456', // optional
|
|
101
|
+
from_time: '2025-01-01T00:00:00Z', // optional
|
|
102
|
+
to_time: '2025-06-01T00:00:00Z', // optional
|
|
103
|
+
});
|
|
104
|
+
// result: {
|
|
105
|
+
// receipt_id: string,
|
|
106
|
+
// deleted_counts: { events, chunks, episodes, facts, claims, open_loops },
|
|
107
|
+
// }
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Usage Methods
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
// Aggregated summary with daily breakdown
|
|
114
|
+
const summary = await client.getUsageSummary('30d'); // '7d' | '30d' | 'month' | 'today'
|
|
115
|
+
|
|
116
|
+
// Breakdown by endpoint
|
|
117
|
+
const breakdown = await client.getUsageBreakdown('today');
|
|
118
|
+
|
|
119
|
+
// Current plan limits and quota
|
|
120
|
+
const limits = await client.getUsageLimits();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Health Check
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
const { ok } = await client.healthCheck(); // no auth required
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Error Handling
|
|
130
|
+
|
|
131
|
+
All errors extend `ClaivError`.
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { ClaivApiError, ClaivTimeoutError, ClaivNetworkError } from '@claiv/memory';
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await client.ingest({ ... });
|
|
138
|
+
} catch (err) {
|
|
139
|
+
if (err instanceof ClaivApiError) {
|
|
140
|
+
console.log(err.status); // HTTP status code
|
|
141
|
+
console.log(err.code); // 'invalid_request' | 'unauthorized' | 'quota_exceeded' | ...
|
|
142
|
+
console.log(err.requestId); // server request ID for support
|
|
143
|
+
console.log(err.details); // validation errors, quota info, etc.
|
|
144
|
+
} else if (err instanceof ClaivTimeoutError) {
|
|
145
|
+
// request timed out
|
|
146
|
+
} else if (err instanceof ClaivNetworkError) {
|
|
147
|
+
// DNS failure, connection refused, etc.
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Retries
|
|
153
|
+
|
|
154
|
+
The SDK automatically retries on 429 (rate limited) and 5xx (server error) responses with exponential backoff and jitter. Client errors (400, 401, 403, 404) are never retried.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
// Default: 2 retries (3 total attempts)
|
|
158
|
+
const client = new ClaivClient({ apiKey: 'key' });
|
|
159
|
+
|
|
160
|
+
// Disable retries
|
|
161
|
+
const client = new ClaivClient({ apiKey: 'key', maxRetries: 0 });
|
|
162
|
+
|
|
163
|
+
// More retries for critical paths
|
|
164
|
+
const client = new ClaivClient({ apiKey: 'key', maxRetries: 5 });
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## TypeScript
|
|
168
|
+
|
|
169
|
+
All request/response types are exported:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import type {
|
|
173
|
+
IngestRequest, IngestResponse,
|
|
174
|
+
RecallRequest, RecallResponse, ContextPack, MemoryBlock,
|
|
175
|
+
ForgetRequest, ForgetResponse, DeletedCounts,
|
|
176
|
+
UsageSummaryResponse, UsageBreakdownResponse, UsageLimitsResponse,
|
|
177
|
+
} from '@claiv/memory';
|
|
178
|
+
```
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ClaivClientOptions, IngestRequest, IngestResponse, RecallRequest, RecallResponse, ForgetRequest, ForgetResponse, DeletionReceipt, ListDeletionReceiptsResponse, UsageRange, UsageSummaryResponse, UsageBreakdownResponse, UsageLimitsResponse } from './types.js';
|
|
2
|
+
export declare class ClaivClient {
|
|
3
|
+
private readonly baseUrl;
|
|
4
|
+
private readonly apiKey;
|
|
5
|
+
private readonly timeoutMs;
|
|
6
|
+
private readonly maxRetries;
|
|
7
|
+
private readonly _fetch;
|
|
8
|
+
constructor(options: ClaivClientOptions);
|
|
9
|
+
/**
|
|
10
|
+
* Ingest a memory event.
|
|
11
|
+
*
|
|
12
|
+
* Stores context your AI should remember — user messages, tool calls, or
|
|
13
|
+
* application events. Returns immediately; enrichment happens asynchronously.
|
|
14
|
+
*/
|
|
15
|
+
ingest(request: IngestRequest): Promise<IngestResponse>;
|
|
16
|
+
/**
|
|
17
|
+
* Recall relevant memory for a task.
|
|
18
|
+
*
|
|
19
|
+
* Returns ranked memory blocks that fit within the specified token budget,
|
|
20
|
+
* along with citations linking each block to its source event.
|
|
21
|
+
*/
|
|
22
|
+
recall(request: RecallRequest): Promise<RecallResponse>;
|
|
23
|
+
/**
|
|
24
|
+
* Forget (delete) memory matching the given scope.
|
|
25
|
+
*
|
|
26
|
+
* Deletes all memory for the specified user, optionally scoped to a thread
|
|
27
|
+
* and/or time range. Returns a receipt with deletion counts.
|
|
28
|
+
*/
|
|
29
|
+
forget(request: ForgetRequest): Promise<ForgetResponse>;
|
|
30
|
+
/**
|
|
31
|
+
* Get aggregated usage summary with daily breakdown.
|
|
32
|
+
*/
|
|
33
|
+
getUsageSummary(range?: UsageRange): Promise<UsageSummaryResponse>;
|
|
34
|
+
/**
|
|
35
|
+
* Get usage breakdown by endpoint.
|
|
36
|
+
*/
|
|
37
|
+
getUsageBreakdown(range?: UsageRange): Promise<UsageBreakdownResponse>;
|
|
38
|
+
/**
|
|
39
|
+
* Get current plan limits and quota status.
|
|
40
|
+
*/
|
|
41
|
+
getUsageLimits(): Promise<UsageLimitsResponse>;
|
|
42
|
+
/**
|
|
43
|
+
* List deletion receipts, newest first.
|
|
44
|
+
* Supports filtering by user_id and pagination via limit/offset.
|
|
45
|
+
*/
|
|
46
|
+
listDeletionReceipts(options?: {
|
|
47
|
+
user_id?: string;
|
|
48
|
+
limit?: number;
|
|
49
|
+
offset?: number;
|
|
50
|
+
}): Promise<ListDeletionReceiptsResponse>;
|
|
51
|
+
/**
|
|
52
|
+
* Get a single deletion receipt by ID.
|
|
53
|
+
*/
|
|
54
|
+
getDeletionReceipt(receiptId: string): Promise<DeletionReceipt>;
|
|
55
|
+
/**
|
|
56
|
+
* Check if the API is reachable and healthy.
|
|
57
|
+
* Does not require authentication.
|
|
58
|
+
*/
|
|
59
|
+
healthCheck(): Promise<{
|
|
60
|
+
ok: boolean;
|
|
61
|
+
}>;
|
|
62
|
+
private post;
|
|
63
|
+
private get;
|
|
64
|
+
private request;
|
|
65
|
+
private getBackoffMs;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,aAAa,EACb,cAAc,EACd,aAAa,EACb,cAAc,EACd,eAAe,EACf,4BAA4B,EAC5B,UAAU,EACV,oBAAoB,EACpB,sBAAsB,EACtB,mBAAmB,EAEpB,MAAM,YAAY,CAAC;AAepB,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,Q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MEOUT_MS = 30_000;
|
|
4
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
5
|
+
const INITIAL_BACKOFF_MS = 500;
|
|
6
|
+
const MAX_BACKOFF_MS = 30_000;
|
|
7
|
+
const RETRYABLE_STATUS_CODES = new Set([429, 500, 502, 503, 504]);
|
|
8
|
+
function sleep(ms) {
|
|
9
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
|
+
}
|
|
11
|
+
export class ClaivClient {
|
|
12
|
+
baseUrl;
|
|
13
|
+
apiKey;
|
|
14
|
+
timeoutMs;
|
|
15
|
+
maxRetries;
|
|
16
|
+
_fetch;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
if (!options.apiKey) {
|
|
19
|
+
throw new Error('apiKey is required');
|
|
20
|
+
}
|
|
21
|
+
if (options.timeout !== undefined && (!Number.isFinite(options.timeout) || options.timeout <= 0)) {
|
|
22
|
+
throw new Error('timeout must be a positive number');
|
|
23
|
+
}
|
|
24
|
+
if (options.maxRetries !== undefined &&
|
|
25
|
+
(!Number.isInteger(options.maxRetries) || options.maxRetries < 0)) {
|
|
26
|
+
throw new Error('maxRetries must be a non-negative integer');
|
|
27
|
+
}
|
|
28
|
+
if (options.fetch !== undefined && typeof options.fetch !== 'function') {
|
|
29
|
+
throw new Error('fetch must be a function');
|
|
30
|
+
}
|
|
31
|
+
this.apiKey = options.apiKey;
|
|
32
|
+
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '');
|
|
33
|
+
this.timeoutMs = options.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
34
|
+
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
35
|
+
if (options.fetch) {
|
|
36
|
+
this._fetch = options.fetch;
|
|
37
|
+
}
|
|
38
|
+
else if (typeof globalThis.fetch === 'function') {
|
|
39
|
+
this._fetch = globalThis.fetch;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error('No fetch implementation available. Provide options.fetch.');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// -------------------------------------------------------------------------
|
|
46
|
+
// Core Memory Endpoints
|
|
47
|
+
// -------------------------------------------------------------------------
|
|
48
|
+
/**
|
|
49
|
+
* Ingest a memory event.
|
|
50
|
+
*
|
|
51
|
+
* Stores context your AI should remember — user messages, tool calls, or
|
|
52
|
+
* application events. Returns immediately; enrichment happens asynchronously.
|
|
53
|
+
*/
|
|
54
|
+
async ingest(request) {
|
|
55
|
+
return this.post('/v1/ingest', request);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Recall relevant memory for a task.
|
|
59
|
+
*
|
|
60
|
+
* Returns ranked memory blocks that fit within the specified token budget,
|
|
61
|
+
* along with citations linking each block to its source event.
|
|
62
|
+
*/
|
|
63
|
+
async recall(request) {
|
|
64
|
+
return this.post('/v1/recall', request);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Forget (delete) memory matching the given scope.
|
|
68
|
+
*
|
|
69
|
+
* Deletes all memory for the specified user, optionally scoped to a thread
|
|
70
|
+
* and/or time range. Returns a receipt with deletion counts.
|
|
71
|
+
*/
|
|
72
|
+
async forget(request) {
|
|
73
|
+
return this.post('/v1/forget', request);
|
|
74
|
+
}
|
|
75
|
+
// -------------------------------------------------------------------------
|
|
76
|
+
// Usage Endpoints
|
|
77
|
+
// -------------------------------------------------------------------------
|
|
78
|
+
/**
|
|
79
|
+
* Get aggregated usage summary with daily breakdown.
|
|
80
|
+
*/
|
|
81
|
+
async getUsageSummary(range = '7d') {
|
|
82
|
+
return this.get('/v1/usage/summary', { range });
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get usage breakdown by endpoint.
|
|
86
|
+
*/
|
|
87
|
+
async getUsageBreakdown(range = '7d') {
|
|
88
|
+
return this.get('/v1/usage/breakdown', { range });
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get current plan limits and quota status.
|
|
92
|
+
*/
|
|
93
|
+
async getUsageLimits() {
|
|
94
|
+
return this.get('/v1/usage/limits');
|
|
95
|
+
}
|
|
96
|
+
// -------------------------------------------------------------------------
|
|
97
|
+
// Deletion Receipts
|
|
98
|
+
// -------------------------------------------------------------------------
|
|
99
|
+
/**
|
|
100
|
+
* List deletion receipts, newest first.
|
|
101
|
+
* Supports filtering by user_id and pagination via limit/offset.
|
|
102
|
+
*/
|
|
103
|
+
async listDeletionReceipts(options) {
|
|
104
|
+
const params = {};
|
|
105
|
+
if (options?.user_id)
|
|
106
|
+
params.user_id = options.user_id;
|
|
107
|
+
if (options?.limit !== undefined)
|
|
108
|
+
params.limit = String(options.limit);
|
|
109
|
+
if (options?.offset !== undefined)
|
|
110
|
+
params.offset = String(options.offset);
|
|
111
|
+
return this.get('/v1/deletion-receipts', params);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get a single deletion receipt by ID.
|
|
115
|
+
*/
|
|
116
|
+
async getDeletionReceipt(receiptId) {
|
|
117
|
+
return this.get(`/v1/deletion-receipts/${receiptId}`);
|
|
118
|
+
}
|
|
119
|
+
// -------------------------------------------------------------------------
|
|
120
|
+
// Health
|
|
121
|
+
// -------------------------------------------------------------------------
|
|
122
|
+
/**
|
|
123
|
+
* Check if the API is reachable and healthy.
|
|
124
|
+
* Does not require authentication.
|
|
125
|
+
*/
|
|
126
|
+
async healthCheck() {
|
|
127
|
+
return this.request('GET', '/healthz', undefined, false);
|
|
128
|
+
}
|
|
129
|
+
// -------------------------------------------------------------------------
|
|
130
|
+
// Internal HTTP Methods
|
|
131
|
+
// -------------------------------------------------------------------------
|
|
132
|
+
async post(path, body) {
|
|
133
|
+
return this.request('POST', path, body, true);
|
|
134
|
+
}
|
|
135
|
+
async get(path, params) {
|
|
136
|
+
let url = path;
|
|
137
|
+
if (params) {
|
|
138
|
+
const qs = new URLSearchParams(params).toString();
|
|
139
|
+
if (qs)
|
|
140
|
+
url = `${path}?${qs}`;
|
|
141
|
+
}
|
|
142
|
+
return this.request('GET', url, undefined, true);
|
|
143
|
+
}
|
|
144
|
+
async request(method, path, body, auth) {
|
|
145
|
+
const url = `${this.baseUrl}${path}`;
|
|
146
|
+
const headers = {
|
|
147
|
+
'Accept': 'application/json',
|
|
148
|
+
};
|
|
149
|
+
if (auth) {
|
|
150
|
+
headers['Authorization'] = `Bearer ${this.apiKey}`;
|
|
151
|
+
}
|
|
152
|
+
if (body !== undefined) {
|
|
153
|
+
headers['Content-Type'] = 'application/json';
|
|
154
|
+
}
|
|
155
|
+
let lastError;
|
|
156
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
157
|
+
if (attempt > 0 && lastError) {
|
|
158
|
+
const backoff = this.getBackoffMs(attempt, lastError);
|
|
159
|
+
await sleep(backoff);
|
|
160
|
+
}
|
|
161
|
+
const controller = new AbortController();
|
|
162
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
163
|
+
let response;
|
|
164
|
+
try {
|
|
165
|
+
response = await this._fetch(url, {
|
|
166
|
+
method,
|
|
167
|
+
headers,
|
|
168
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
169
|
+
signal: controller.signal,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
clearTimeout(timer);
|
|
174
|
+
if (err instanceof DOMException && err.name === 'AbortError') {
|
|
175
|
+
throw new ClaivTimeoutError(this.timeoutMs);
|
|
176
|
+
}
|
|
177
|
+
throw new ClaivNetworkError(err instanceof Error ? err : new Error(String(err)));
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
clearTimeout(timer);
|
|
181
|
+
}
|
|
182
|
+
if (response.ok) {
|
|
183
|
+
return (await response.json());
|
|
184
|
+
}
|
|
185
|
+
let errorBody;
|
|
186
|
+
try {
|
|
187
|
+
errorBody = (await response.json());
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
errorBody = {
|
|
191
|
+
error: {
|
|
192
|
+
code: 'unknown',
|
|
193
|
+
message: response.statusText || `HTTP ${response.status}`,
|
|
194
|
+
request_id: '00000000-0000-0000-0000-000000000000',
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
lastError = new ClaivApiError(response.status, errorBody);
|
|
199
|
+
if (!RETRYABLE_STATUS_CODES.has(response.status) || attempt === this.maxRetries) {
|
|
200
|
+
throw lastError;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// Unreachable, but satisfies TypeScript
|
|
204
|
+
throw lastError;
|
|
205
|
+
}
|
|
206
|
+
getBackoffMs(attempt, error) {
|
|
207
|
+
// Respect Retry-After header for 429 responses (stored in details if present)
|
|
208
|
+
const retryAfter = error.details && typeof error.details === 'object' &&
|
|
209
|
+
'retry_after' in error.details
|
|
210
|
+
? Number(error.details.retry_after)
|
|
211
|
+
: undefined;
|
|
212
|
+
if (retryAfter && retryAfter > 0) {
|
|
213
|
+
return Math.min(retryAfter * 1000, MAX_BACKOFF_MS);
|
|
214
|
+
}
|
|
215
|
+
// Exponential backoff with jitter
|
|
216
|
+
const base = INITIAL_BACKOFF_MS * Math.pow(2, attempt - 1);
|
|
217
|
+
const jitter = base * 0.5 * Math.random();
|
|
218
|
+
return Math.min(base + jitter, MAX_BACKOFF_MS);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAElF,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AACjD,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAC9B,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAC/B,MAAM,cAAc,GAAG,MAAM,CAAC;AAE9B,MAAM,sB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nD;iBACF,CAAC;YACJ,CAAC;YAED,SAAS,GAAG,IAAI,aAAa,CAAC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;YAE1D,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,OAAO,KAAK,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChF,MAAM,SAAS,CAAC;YAClB,CAAC;QACH,CAAC;QAED,wCAAwC;QACxC,MAAM,SAAS,CAAC;IAClB,CAAC;IAEO,YAAY,CAAC,OAAe,EAAE,KAAoB;QACxD,8EAA8E;QAC9E,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;YACnE,aAAa,IAAK,KAAK,CAAC,OAAmC;YAC3D,CAAC,CAAC,MAAM,CAAE,KAAK,CAAC,OAAmC,CAAC,WAAW,CAAC;YAChE,CAAC,CAAC,SAAS,CAAC;QAEd,IAAI,UAAU,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;YACjC,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,GAAG,IAAI,EAAE,cAAc,CAAC,CAAC;QACrD,CAAC;QAED,kCAAkC;QAClC,MAAM,IAAI,GAAG,kBAAkB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;QAC3D,MAAM,MAAM,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,MAAM,EAAE,cAAc,CAAC,CAAC;IACjD,CAAC;CACF"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ApiErrorBody } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base error for all Claiv SDK errors.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ClaivError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Thrown when the API returns an error response (4xx / 5xx).
|
|
10
|
+
*/
|
|
11
|
+
export declare class ClaivApiError extends ClaivError {
|
|
12
|
+
/** HTTP status code */
|
|
13
|
+
readonly status: number;
|
|
14
|
+
/** Machine-readable error code from the API */
|
|
15
|
+
readonly code: string;
|
|
16
|
+
/** Server-assigned request ID for support tickets */
|
|
17
|
+
readonly requestId: string;
|
|
18
|
+
/** Optional extra details (e.g. validation errors, quota info) */
|
|
19
|
+
readonly details: unknown;
|
|
20
|
+
constructor(status: number, body: ApiErrorBody);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Thrown when a request times out.
|
|
24
|
+
*/
|
|
25
|
+
export declare class ClaivTimeoutError extends ClaivError {
|
|
26
|
+
constructor(timeoutMs: number);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Thrown when a network-level error occurs (DNS failure, connection refused, etc.).
|
|
30
|
+
*/
|
|
31
|
+
export declare class ClaivNetworkError extends ClaivError {
|
|
32
|
+
readonly cause: Error;
|
|
33
|
+
constructor(cause: Error);
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,UAAU;IAC3C,uBAAuB;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,+CAA+C;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,qDAAqD;IACrD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,kEAAkE;IAClE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;gBAEd,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY;CAQ/C;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,UAAU;gBACnC,SAAS,EAAE,MAAM;CAI9B;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,UAAU;IAC/C,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;gBAEV,KAAK,EAAE,KAAK;CAKzB"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error for all Claiv SDK errors.
|
|
3
|
+
*/
|
|
4
|
+
export class ClaivError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'ClaivError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Thrown when the API returns an error response (4xx / 5xx).
|
|
12
|
+
*/
|
|
13
|
+
export class ClaivApiError extends ClaivError {
|
|
14
|
+
/** HTTP status code */
|
|
15
|
+
status;
|
|
16
|
+
/** Machine-readable error code from the API */
|
|
17
|
+
code;
|
|
18
|
+
/** Server-assigned request ID for support tickets */
|
|
19
|
+
requestId;
|
|
20
|
+
/** Optional extra details (e.g. validation errors, quota info) */
|
|
21
|
+
details;
|
|
22
|
+
constructor(status, body) {
|
|
23
|
+
super(body.error.message);
|
|
24
|
+
this.name = 'ClaivApiError';
|
|
25
|
+
this.status = status;
|
|
26
|
+
this.code = body.error.code;
|
|
27
|
+
this.requestId = body.error.request_id;
|
|
28
|
+
this.details = body.error.details;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Thrown when a request times out.
|
|
33
|
+
*/
|
|
34
|
+
export class ClaivTimeoutError extends ClaivError {
|
|
35
|
+
constructor(timeoutMs) {
|
|
36
|
+
super(`Request timed out after ${timeoutMs}ms`);
|
|
37
|
+
this.name = 'ClaivTimeoutError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Thrown when a network-level error occurs (DNS failure, connection refused, etc.).
|
|
42
|
+
*/
|
|
43
|
+
export class ClaivNetworkError extends ClaivError {
|
|
44
|
+
cause;
|
|
45
|
+
constructor(cause) {
|
|
46
|
+
super(`Network error: ${cause.message}`);
|
|
47
|
+
this.name = 'ClaivNetworkError';
|
|
48
|
+
this.cause = cause;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC3B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,UAAU;IAC3C,uBAAuB;IACd,MAAM,CAAS;IACxB,+CAA+C;IACtC,IAAI,CAAS;IACtB,qDAAqD;IAC5C,SAAS,CAAS;IAC3B,kEAAkE;IACzD,OAAO,CAAU;IAE1B,YAAY,MAAc,EAAE,IAAkB;QAC5C,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;QAC5B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC;QACvC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;IACpC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAC/C,YAAY,SAAiB;QAC3B,KAAK,CAAC,2BAA2B,SAAS,IAAI,CAAC,CAAC;QAChD,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IACtC,KAAK,CAAQ;IAEtB,YAAY,KAAY;QACtB,KAAK,CAAC,kBAAkB,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACzC,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ClaivClient } from './client.js';
|
|
2
|
+
export { ClaivError, ClaivApiError, ClaivTimeoutError, ClaivNetworkError } from './errors.js';
|
|
3
|
+
export type { ClaivClientOptions, EventType, MemoryBlockType, UsageRange, IngestRequest, IngestResponse, RecallRequest, RecallResponse, ContextPack, MemoryBlock, ForgetRequest, ForgetResponse, DeletedCounts, DeletionScope, DeletionReceipt, ListDeletionReceiptsResponse, UsageSummaryResponse, UsageTotals, UsageDailyEntry, UsageBreakdownResponse, EndpointBreakdownEntry, UsageLimitsResponse, UsageLimitMetric, ApiErrorBody, } from './types.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAC9F,YAAY,EACV,kBAAkB,EAClB,SAAS,EACT,eAAe,EACf,UAAU,EACV,aAAa,EACb,cAAc,EACd,aAAa,EACb,cAAc,EACd,WAAW,EACX,WAAW,EACX,aAAa,EACb,cAAc,EACd,aAAa,EACb,aAAa,EACb,eAAe,EACf,4BAA4B,EAC5B,oBAAoB,EACpB,WAAW,EACX,eAAe,EACf,sBAAsB,EACtB,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,EAChB,YAAY,GACb,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript types matching the Claiv Memory API contracts exactly.
|
|
3
|
+
*
|
|
4
|
+
* These mirror the Zod schemas in @claiv/shared without importing them,
|
|
5
|
+
* so the SDK has zero dependency on internal packages.
|
|
6
|
+
*/
|
|
7
|
+
export type EventType = 'message' | 'tool_call' | 'app_event';
|
|
8
|
+
export type MemoryBlockType = 'open_loop' | 'fact' | 'claim' | 'episode' | 'chunk';
|
|
9
|
+
export type UsageRange = '7d' | '30d' | 'month' | 'today';
|
|
10
|
+
export interface IngestRequest {
|
|
11
|
+
user_id: string;
|
|
12
|
+
thread_id?: string;
|
|
13
|
+
type: EventType;
|
|
14
|
+
content: string;
|
|
15
|
+
metadata?: Record<string, unknown>;
|
|
16
|
+
event_time?: string;
|
|
17
|
+
idempotency_key?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface IngestResponse {
|
|
20
|
+
event_id: string;
|
|
21
|
+
deduped: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface RecallRequest {
|
|
24
|
+
user_id: string;
|
|
25
|
+
thread_id?: string;
|
|
26
|
+
task: string;
|
|
27
|
+
token_budget: number;
|
|
28
|
+
scope?: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
export interface MemoryBlock {
|
|
31
|
+
type: MemoryBlockType;
|
|
32
|
+
content: string;
|
|
33
|
+
source_ids: string[];
|
|
34
|
+
score: number;
|
|
35
|
+
}
|
|
36
|
+
export interface RecallResponse {
|
|
37
|
+
system_context: string;
|
|
38
|
+
memory_blocks: MemoryBlock[];
|
|
39
|
+
citations: string[];
|
|
40
|
+
token_estimate: number;
|
|
41
|
+
}
|
|
42
|
+
export type ContextPack = RecallResponse;
|
|
43
|
+
export interface ForgetRequest {
|
|
44
|
+
user_id: string;
|
|
45
|
+
thread_id?: string;
|
|
46
|
+
from_time?: string;
|
|
47
|
+
to_time?: string;
|
|
48
|
+
}
|
|
49
|
+
export interface DeletedCounts {
|
|
50
|
+
events: number;
|
|
51
|
+
chunks: number;
|
|
52
|
+
episodes: number;
|
|
53
|
+
facts: number;
|
|
54
|
+
claims: number;
|
|
55
|
+
open_loops: number;
|
|
56
|
+
}
|
|
57
|
+
export interface ForgetResponse {
|
|
58
|
+
receipt_id: string;
|
|
59
|
+
deleted_counts: DeletedCounts;
|
|
60
|
+
}
|
|
61
|
+
export interface DeletionScope {
|
|
62
|
+
user_id: string;
|
|
63
|
+
thread_id?: string | null;
|
|
64
|
+
from_time?: string | null;
|
|
65
|
+
to_time?: string | null;
|
|
66
|
+
}
|
|
67
|
+
export interface DeletionReceipt {
|
|
68
|
+
receipt_id: string;
|
|
69
|
+
scope: DeletionScope;
|
|
70
|
+
requested_at: string;
|
|
71
|
+
completed_at: string | null;
|
|
72
|
+
deleted_counts: DeletedCounts;
|
|
73
|
+
}
|
|
74
|
+
export interface ListDeletionReceiptsResponse {
|
|
75
|
+
receipts: DeletionReceipt[];
|
|
76
|
+
total: number;
|
|
77
|
+
limit: number;
|
|
78
|
+
offset: number;
|
|
79
|
+
}
|
|
80
|
+
export interface UsageTotals {
|
|
81
|
+
requests: number;
|
|
82
|
+
ingest_requests: number;
|
|
83
|
+
recall_requests: number;
|
|
84
|
+
forget_requests: number;
|
|
85
|
+
ingest_events: number;
|
|
86
|
+
tokens: number;
|
|
87
|
+
work_units: number;
|
|
88
|
+
errors: number;
|
|
89
|
+
request_bytes: number;
|
|
90
|
+
response_bytes: number;
|
|
91
|
+
}
|
|
92
|
+
export interface UsageDailyEntry {
|
|
93
|
+
date: string;
|
|
94
|
+
requests: number;
|
|
95
|
+
ingest_events: number;
|
|
96
|
+
tokens: number;
|
|
97
|
+
work_units: number;
|
|
98
|
+
errors: number;
|
|
99
|
+
}
|
|
100
|
+
export interface UsageSummaryResponse {
|
|
101
|
+
range: string;
|
|
102
|
+
start_date: string;
|
|
103
|
+
end_date: string;
|
|
104
|
+
totals: UsageTotals;
|
|
105
|
+
daily: UsageDailyEntry[];
|
|
106
|
+
}
|
|
107
|
+
export interface EndpointBreakdownEntry {
|
|
108
|
+
endpoint: string;
|
|
109
|
+
requests: number;
|
|
110
|
+
errors: number;
|
|
111
|
+
error_rate: number;
|
|
112
|
+
avg_latency_ms: number;
|
|
113
|
+
}
|
|
114
|
+
export interface UsageBreakdownResponse {
|
|
115
|
+
range: string;
|
|
116
|
+
start_date: string;
|
|
117
|
+
end_date: string;
|
|
118
|
+
endpoints: EndpointBreakdownEntry[];
|
|
119
|
+
}
|
|
120
|
+
export interface UsageLimitMetric {
|
|
121
|
+
used: number;
|
|
122
|
+
limit: number | null;
|
|
123
|
+
remaining: number | null;
|
|
124
|
+
percentage_used: number;
|
|
125
|
+
}
|
|
126
|
+
export interface UsageLimitsResponse {
|
|
127
|
+
plan: string;
|
|
128
|
+
billing_cycle_day: number;
|
|
129
|
+
reset_date: string;
|
|
130
|
+
is_within_quota: boolean;
|
|
131
|
+
limits: {
|
|
132
|
+
requests: UsageLimitMetric;
|
|
133
|
+
tokens: UsageLimitMetric;
|
|
134
|
+
work_units: UsageLimitMetric;
|
|
135
|
+
ingest_events: UsageLimitMetric;
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
export interface ApiErrorBody {
|
|
139
|
+
error: {
|
|
140
|
+
code: string;
|
|
141
|
+
message: string;
|
|
142
|
+
request_id: string;
|
|
143
|
+
details?: unknown;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export interface ClaivClientOptions {
|
|
147
|
+
/** API key for authentication (sent as Bearer token). */
|
|
148
|
+
apiKey: string;
|
|
149
|
+
/** Base URL of the Claiv Memory API. Defaults to https://api.claiv.com */
|
|
150
|
+
baseUrl?: string;
|
|
151
|
+
/** Request timeout in milliseconds. Defaults to 30000. */
|
|
152
|
+
timeout?: number;
|
|
153
|
+
/** Maximum number of retries on 429 and 5xx errors. Set to 0 to disable. Defaults to 2. */
|
|
154
|
+
maxRetries?: number;
|
|
155
|
+
/** Custom fetch implementation. Defaults to global fetch. */
|
|
156
|
+
fetch?: typeof globalThis.fetch;
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,WAAW,GAAG,WAAW,CAAC;AAE9D,MAAM,MAAM,eAAe,GAAG,WAAW,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,OAAO,CAAC;AAEnF,MAAM,MAAM,UAAU,GAAG,IAAI,GAAG,KAAK,GAAG,OAAO,GAAG,OAAO,CAAC;AAM1D,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB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
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@claiv/memory",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official JavaScript/TypeScript SDK for the Claiv Memory API",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"test": "vitest run"
|
|
21
|
+
},
|
|
22
|
+
"keywords": ["claiv", "memory", "ai", "context", "llm", "sdk"],
|
|
23
|
+
"author": "Claiv",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/kinkaid2002/claiv-memory.git",
|
|
28
|
+
"directory": "packages/sdk-js"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://claiv.com",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^20.11.0",
|
|
33
|
+
"typescript": "^5.3.3",
|
|
34
|
+
"vitest": "^4.0.18"
|
|
35
|
+
}
|
|
36
|
+
}
|