geniffy 0.1.1 → 0.3.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 +78 -4
- package/dist/claude.d.ts +95 -0
- package/dist/claude.js +399 -0
- package/dist/index.d.ts +223 -23
- package/dist/index.js +276 -30
- package/package.json +14 -3
package/dist/index.js
CHANGED
|
@@ -7,11 +7,33 @@
|
|
|
7
7
|
* const { answer } = await g.ask("Who signs the Lumen renewal?");
|
|
8
8
|
*
|
|
9
9
|
* No dependencies: the platform's fetch, FormData and Blob (Node 18+, Deno, Bun, edge runtimes).
|
|
10
|
-
* Retries: reads
|
|
11
|
-
* 429, so a retry never saves a note twice.
|
|
10
|
+
* Retries: reads, deletes and files.put (the same text twice is the same file) are retried on network errors,
|
|
11
|
+
* 408, 429 and 5xx; adding is retried only on 429, so a retry never saves a note twice.
|
|
12
12
|
*/
|
|
13
|
-
export const VERSION = "0.
|
|
13
|
+
export const VERSION = "0.3.0";
|
|
14
14
|
export const DEFAULT_BASE_URL = "https://api.geniffy.com";
|
|
15
|
+
/** A filter in a query string: label=name:value, once for each value. */
|
|
16
|
+
function labelQuery(labels) {
|
|
17
|
+
if (!labels)
|
|
18
|
+
return undefined;
|
|
19
|
+
return Object.entries(labels).flatMap(([k, v]) => (Array.isArray(v) ? v : [v]).map((x) => `${k}:${x}`));
|
|
20
|
+
}
|
|
21
|
+
const INTEGRATION = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}\/[A-Za-z0-9][A-Za-z0-9._+-]{0,31}$/;
|
|
22
|
+
// One of your users, by your own name for them: a string, or an integer id. A blank one is refused, not
|
|
23
|
+
// read as no space: a client with no space reads YOUR memory, so a user with no id would land in it.
|
|
24
|
+
function spaceName(space, what) {
|
|
25
|
+
const named = typeof space === "number" && Number.isSafeInteger(space) ? String(space) : space;
|
|
26
|
+
if (typeof named !== "string") {
|
|
27
|
+
throw new TypeError(`${what} takes your name for one of your users, a string or an integer id, not ` +
|
|
28
|
+
`${named === null ? "null" : typeof named}.`);
|
|
29
|
+
}
|
|
30
|
+
const name = named.trim();
|
|
31
|
+
if (!name) {
|
|
32
|
+
throw new TypeError(`${what} got a blank space. A client with no space reads your own memory, so a user ` +
|
|
33
|
+
"with no id would land in it. Pass the user's id, or use the client itself for your own memory.");
|
|
34
|
+
}
|
|
35
|
+
return name;
|
|
36
|
+
}
|
|
15
37
|
// ── errors ────────────────────────────────────────────────────────────────────
|
|
16
38
|
export class GeniffyError extends Error {
|
|
17
39
|
status;
|
|
@@ -84,6 +106,12 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
84
106
|
export class Geniffy {
|
|
85
107
|
memories;
|
|
86
108
|
sources;
|
|
109
|
+
/** Files kept with their exact text, such as the ones Claude's memory tool writes (see `geniffy/claude`) */
|
|
110
|
+
files;
|
|
111
|
+
/** Keys limited to one of your users, on a client bound to them: client.space(id).keys */
|
|
112
|
+
keys;
|
|
113
|
+
/** The sections profiles are grouped into: every user's on the plain client, one user's on client.space(id) */
|
|
114
|
+
sections;
|
|
87
115
|
maxRetries;
|
|
88
116
|
timeout;
|
|
89
117
|
/** The one of your users this client is bound to, or "" for your own memory. */
|
|
@@ -103,9 +131,15 @@ export class Geniffy {
|
|
|
103
131
|
this.#fetch = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
104
132
|
this.timeout = opts.timeout ?? 60_000;
|
|
105
133
|
this.maxRetries = Math.max(0, opts.maxRetries ?? 2);
|
|
106
|
-
this.boundSpace =
|
|
134
|
+
this.boundSpace = opts.space === undefined || opts.space === null ? "" : spaceName(opts.space, "space:");
|
|
135
|
+
if (opts.integration !== undefined && !INTEGRATION.test(opts.integration)) {
|
|
136
|
+
throw new TypeError('integration is a name and a version, such as "geniffy-ai-sdk/0.1.0".');
|
|
137
|
+
}
|
|
107
138
|
this.memories = new Memories(this);
|
|
108
139
|
this.sources = new Sources(this);
|
|
140
|
+
this.files = new Files(this);
|
|
141
|
+
this.keys = new Keys(this);
|
|
142
|
+
this.sections = new Sections(this);
|
|
109
143
|
}
|
|
110
144
|
/**
|
|
111
145
|
* The same client, pointed at one of YOUR users. Everything it reads and writes is that user's
|
|
@@ -116,22 +150,27 @@ export class Geniffy {
|
|
|
116
150
|
* await mem.memories.add("Prefers WhatsApp.");
|
|
117
151
|
* const answer = await mem.ask("How should we reach them?");
|
|
118
152
|
*
|
|
119
|
-
* `space` is your own name for that user and nothing is read into it:
|
|
120
|
-
*
|
|
153
|
+
* `space` is your own name for that user and nothing is read into it: the id you already give them,
|
|
154
|
+
* up to 128 letters, digits, dots, dashes or underscores, so an id rather than an email. A blank one
|
|
155
|
+
* (or undefined, or null) throws rather than reading your own memory.
|
|
121
156
|
*/
|
|
122
157
|
space(space) {
|
|
123
|
-
return new Geniffy({ ...this.#opts, apiKey: this.#key, space });
|
|
158
|
+
return new Geniffy({ ...this.#opts, apiKey: this.#key, space: spaceName(space, "space()") });
|
|
124
159
|
}
|
|
125
160
|
/** @internal */
|
|
126
161
|
async request(method, path, body = {}) {
|
|
127
162
|
const url = new URL(this.#base + path);
|
|
128
|
-
for (const [k, v] of Object.entries(body.query ?? {}))
|
|
129
|
-
if (v
|
|
163
|
+
for (const [k, v] of Object.entries(body.query ?? {})) {
|
|
164
|
+
if (Array.isArray(v))
|
|
165
|
+
for (const each of v)
|
|
166
|
+
url.searchParams.append(k, each);
|
|
167
|
+
else if (v !== undefined)
|
|
130
168
|
url.searchParams.set(k, String(v));
|
|
169
|
+
}
|
|
131
170
|
const headers = {
|
|
132
171
|
Authorization: `Bearer ${this.#key}`,
|
|
133
172
|
Accept: "application/json",
|
|
134
|
-
"X-Geniffy-Client": `geniffy-js/${VERSION}`,
|
|
173
|
+
"X-Geniffy-Client": `geniffy-js/${VERSION}${this.#opts.integration ? ` ${this.#opts.integration}` : ""}`,
|
|
135
174
|
};
|
|
136
175
|
// One header carries the space, so every call a bound client makes is scoped without any
|
|
137
176
|
// method having to take it, and a space can never be dropped by forgetting an argument.
|
|
@@ -178,13 +217,17 @@ export class Geniffy {
|
|
|
178
217
|
return data;
|
|
179
218
|
}
|
|
180
219
|
}
|
|
181
|
-
/** An answer from your memory only. `answer` is null when nothing you added supports one.
|
|
182
|
-
|
|
183
|
-
|
|
220
|
+
/** An answer from your memory only. `answer` is null when nothing you added supports one. With labels, only
|
|
221
|
+
* from what the sources carrying them said. */
|
|
222
|
+
ask(question, opts = {}) {
|
|
223
|
+
return this.request("POST", "/v1/ask", { json: { question, labels: opts.labels } });
|
|
184
224
|
}
|
|
185
|
-
/** The memories that best match `q`, best first.
|
|
225
|
+
/** The memories that best match `q`, best first. `labels: { channel: "email" }` keeps to the sources
|
|
226
|
+
* carrying them: every name must match, and a list of values is any one of them. */
|
|
186
227
|
async search(q, opts = {}) {
|
|
187
|
-
const out = await this.request("POST", "/v1/search", {
|
|
228
|
+
const out = await this.request("POST", "/v1/search", {
|
|
229
|
+
json: { q, limit: opts.limit ?? 10, kind: opts.kind, labels: opts.labels },
|
|
230
|
+
});
|
|
188
231
|
return out.memories;
|
|
189
232
|
}
|
|
190
233
|
/**
|
|
@@ -205,7 +248,8 @@ export class Geniffy {
|
|
|
205
248
|
/** The same, with the memories behind it and whether anything was found. */
|
|
206
249
|
contextFull(question, opts = {}) {
|
|
207
250
|
return this.request("POST", "/v1/context", {
|
|
208
|
-
json: { question, limit: opts.limit ?? 12, kind: opts.kind, with_sources: opts.withSources ?? true
|
|
251
|
+
json: { question, limit: opts.limit ?? 12, kind: opts.kind, with_sources: opts.withSources ?? true,
|
|
252
|
+
labels: opts.labels },
|
|
209
253
|
});
|
|
210
254
|
}
|
|
211
255
|
/** What stays true about someone, and what is going on with them now. */
|
|
@@ -214,12 +258,18 @@ export class Geniffy {
|
|
|
214
258
|
}
|
|
215
259
|
/** What to read before dealing with someone. */
|
|
216
260
|
brief(subject, opts = {}) {
|
|
217
|
-
return this.request("GET", "/v1/brief", { query: { subject, limit: opts.limit } });
|
|
261
|
+
return this.request("GET", "/v1/brief", { query: { subject, limit: opts.limit, label: labelQuery(opts.labels) } });
|
|
218
262
|
}
|
|
219
263
|
/** What the memory holds and what connects to what. Every line has a memory behind it. */
|
|
220
264
|
graph() {
|
|
221
265
|
return this.request("GET", "/v1/graph");
|
|
222
266
|
}
|
|
267
|
+
/** Everything held, as the user's own copy: every memory, current or not, with its status and the sentence it
|
|
268
|
+
* came from, every source, and every file by its path (files.get(path) reads each one's text). On
|
|
269
|
+
* client.space(id), for a user who asks what you hold about them. */
|
|
270
|
+
export() {
|
|
271
|
+
return this.request("GET", "/v1/export");
|
|
272
|
+
}
|
|
223
273
|
/** Whose key this is, and which space this client is reading. */
|
|
224
274
|
me() {
|
|
225
275
|
return this.request("GET", "/v1/me");
|
|
@@ -234,9 +284,27 @@ export class Geniffy {
|
|
|
234
284
|
* make when they ask to be forgotten. It cannot be undone.
|
|
235
285
|
*/
|
|
236
286
|
async forgetSpace(space) {
|
|
237
|
-
await this.request("DELETE", `/v1/spaces/${encodeURIComponent(space)}`);
|
|
287
|
+
await this.request("DELETE", `/v1/spaces/${encodeURIComponent(spaceName(space, "forgetSpace()"))}`);
|
|
238
288
|
}
|
|
239
289
|
}
|
|
290
|
+
// The body the API takes: saidAt goes as said_at, a Date as its moment in UTC; externalId as external_id.
|
|
291
|
+
function addBody(input) {
|
|
292
|
+
const given = typeof input === "string" ? { text: input } : input;
|
|
293
|
+
const { saidAt, externalId, ...rest } = given;
|
|
294
|
+
const body = { ...rest };
|
|
295
|
+
if (saidAt !== undefined)
|
|
296
|
+
body.said_at = saidAt instanceof Date ? saidAt.toISOString() : String(saidAt);
|
|
297
|
+
if (externalId !== undefined)
|
|
298
|
+
body.external_id = String(externalId);
|
|
299
|
+
return body;
|
|
300
|
+
}
|
|
301
|
+
function lookup(ref) {
|
|
302
|
+
if (typeof ref === "string")
|
|
303
|
+
return { id: ref };
|
|
304
|
+
if (ref && typeof ref.externalId === "string")
|
|
305
|
+
return { externalId: ref.externalId };
|
|
306
|
+
throw new TypeError("Name the source by its id, or as { externalId }.");
|
|
307
|
+
}
|
|
240
308
|
export class Memories {
|
|
241
309
|
client;
|
|
242
310
|
constructor(client) {
|
|
@@ -244,11 +312,12 @@ export class Memories {
|
|
|
244
312
|
}
|
|
245
313
|
/** Add a note (a string or { text }), or a web page ({ url }) that Geniffy reads once. */
|
|
246
314
|
async add(input) {
|
|
247
|
-
const json =
|
|
315
|
+
const json = addBody(input);
|
|
248
316
|
const out = await this.client.request("POST", "/v1/memories", { json });
|
|
249
317
|
return out.source;
|
|
250
318
|
}
|
|
251
|
-
/** Add a PDF
|
|
319
|
+
/** Add a file: PDF, Word (.docx), PowerPoint (.pptx), Excel (.xlsx), or text (.txt, .md, .csv, .html). Under an
|
|
320
|
+
* externalId, a new version updates the source that id names. */
|
|
252
321
|
async addFile(file, opts = {}) {
|
|
253
322
|
const blob = file instanceof Blob ? file : new Blob([file]);
|
|
254
323
|
const named = file.name;
|
|
@@ -257,6 +326,10 @@ export class Memories {
|
|
|
257
326
|
form.append("file", blob, name);
|
|
258
327
|
if (opts.title)
|
|
259
328
|
form.append("title", opts.title);
|
|
329
|
+
if (opts.externalId !== undefined)
|
|
330
|
+
form.append("external_id", String(opts.externalId));
|
|
331
|
+
if (opts.labels !== undefined)
|
|
332
|
+
form.append("labels", JSON.stringify(opts.labels));
|
|
260
333
|
const out = await this.client.request("POST", "/v1/memories/file", { form });
|
|
261
334
|
return out.source;
|
|
262
335
|
}
|
|
@@ -265,7 +338,7 @@ export class Memories {
|
|
|
265
338
|
* the order you sent it, with either its source or why it was refused.
|
|
266
339
|
*/
|
|
267
340
|
async addMany(items) {
|
|
268
|
-
const json = { items: items.map(
|
|
341
|
+
const json = { items: items.map(addBody) };
|
|
269
342
|
return this.client.request("POST", "/v1/memories/batch", { json });
|
|
270
343
|
}
|
|
271
344
|
/** Say what we got wrong and what is right. The wrong one is marked and never used again; what
|
|
@@ -273,17 +346,17 @@ export class Memories {
|
|
|
273
346
|
async correct(id, text) {
|
|
274
347
|
return this.client.request("PATCH", `/v1/memories/${id}`, { json: { text } });
|
|
275
348
|
}
|
|
276
|
-
/** One page of memories, newest first. */
|
|
349
|
+
/** One page of memories, newest first; with labels, only what the sources carrying them said. */
|
|
277
350
|
list(opts = {}) {
|
|
278
351
|
return this.client.request("GET", "/v1/memories", {
|
|
279
|
-
query: { kind: opts.kind, limit: opts.limit ?? 50, cursor: opts.cursor ?? 0 },
|
|
352
|
+
query: { kind: opts.kind, limit: opts.limit ?? 50, cursor: opts.cursor ?? 0, label: labelQuery(opts.labels) },
|
|
280
353
|
});
|
|
281
354
|
}
|
|
282
355
|
/** Every memory, newest first, a page at a time: for await (const m of g.memories.iterate()) */
|
|
283
356
|
async *iterate(opts = {}) {
|
|
284
357
|
let cursor = 0;
|
|
285
358
|
while (cursor !== null) {
|
|
286
|
-
const page = await this.list({ kind: opts.kind, limit: opts.pageSize ?? 100, cursor });
|
|
359
|
+
const page = await this.list({ kind: opts.kind, limit: opts.pageSize ?? 100, cursor, labels: opts.labels });
|
|
287
360
|
yield* page.memories;
|
|
288
361
|
cursor = page.next;
|
|
289
362
|
}
|
|
@@ -297,20 +370,139 @@ export class Memories {
|
|
|
297
370
|
await this.client.request("DELETE", `/v1/memories/${Math.trunc(id)}`);
|
|
298
371
|
}
|
|
299
372
|
}
|
|
373
|
+
/** Keys limited to one of your users, on a client bound to that user: `client.space(id).keys`. Such a key is
|
|
374
|
+
* safe to hand to that user's own app or device, since it reaches their memory and nothing else. */
|
|
375
|
+
export class Keys {
|
|
376
|
+
client;
|
|
377
|
+
constructor(client) {
|
|
378
|
+
this.client = client;
|
|
379
|
+
}
|
|
380
|
+
/** A new key limited to this client's user. `key` is shown once. rpm: requests a minute (up to 600, the default).
|
|
381
|
+
* expiresAt: when it stops working by itself (a Date, or an ISO date it works through); left out, never. */
|
|
382
|
+
create(opts = {}) {
|
|
383
|
+
const json = {};
|
|
384
|
+
if (opts.name)
|
|
385
|
+
json.name = opts.name;
|
|
386
|
+
if (opts.rpm !== undefined)
|
|
387
|
+
json.rpm = opts.rpm;
|
|
388
|
+
if (opts.expiresAt !== undefined) {
|
|
389
|
+
json.expires_at = opts.expiresAt instanceof Date ? opts.expiresAt.toISOString() : String(opts.expiresAt);
|
|
390
|
+
}
|
|
391
|
+
return this.client.request("POST", "/v1/keys", { json });
|
|
392
|
+
}
|
|
393
|
+
/** The keys limited to this client's user that still work. */
|
|
394
|
+
async list() {
|
|
395
|
+
return (await this.client.request("GET", "/v1/keys")).keys;
|
|
396
|
+
}
|
|
397
|
+
/** One of this user's keys stops at once. */
|
|
398
|
+
async revoke(id) {
|
|
399
|
+
await this.client.request("DELETE", `/v1/keys/${Math.trunc(id)}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/** The sections profiles are grouped into. On the plain client, for every one of your users; on a client bound
|
|
403
|
+
* to one user (`client.space(id).sections`), for that user only. A memory goes in a section when one of its
|
|
404
|
+
* keywords appears in it, or its topic is one of the section's. */
|
|
405
|
+
export class Sections {
|
|
406
|
+
client;
|
|
407
|
+
constructor(client) {
|
|
408
|
+
this.client = client;
|
|
409
|
+
}
|
|
410
|
+
/** The sections, the app's own first, then the built-in ones. */
|
|
411
|
+
async list() {
|
|
412
|
+
return (await this.client.request("GET", "/v1/profile/sections")).sections;
|
|
413
|
+
}
|
|
414
|
+
/** Add a section (or, under a name it already has, update it). Profiles regroup within a minute or so. */
|
|
415
|
+
create(section) {
|
|
416
|
+
const json = { name: section.name, description: section.description ?? "", keywords: section.keywords ?? [],
|
|
417
|
+
topics: section.topics ?? [] };
|
|
418
|
+
return this.client.request("POST", "/v1/profile/sections", { json });
|
|
419
|
+
}
|
|
420
|
+
async delete(id) {
|
|
421
|
+
await this.client.request("DELETE", `/v1/profile/sections/${Math.trunc(id)}`);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// Each call deletes up to 100 sources (or files) and says whether more are left; this many calls is the most one
|
|
425
|
+
// deleteLabelled() or files.deletePrefix() makes, so a filter that somehow keeps matching cannot loop for ever.
|
|
426
|
+
const DELETE_CALLS = 1000;
|
|
300
427
|
export class Sources {
|
|
301
428
|
client;
|
|
302
429
|
constructor(client) {
|
|
303
430
|
this.client = client;
|
|
304
431
|
}
|
|
432
|
+
/** What was added, newest first; with labels, only the sources carrying them. */
|
|
305
433
|
list(opts = {}) {
|
|
306
|
-
return this.client.request("GET", "/v1/sources", {
|
|
434
|
+
return this.client.request("GET", "/v1/sources", {
|
|
435
|
+
query: { limit: opts.limit ?? 100, cursor: opts.cursor ?? 0, label: labelQuery(opts.labels) },
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
/** Delete every source carrying these labels, and every memory learned only from them: the call when your
|
|
439
|
+
* user disconnects a data source whose things you added under its label. Resolves to how many.
|
|
440
|
+
*
|
|
441
|
+
* `keep`: the external ids to leave, for the end of a sync that read everything: every other source with
|
|
442
|
+
* these labels, such as what is gone from the data source, is deleted. */
|
|
443
|
+
async deleteLabelled(labels, opts = {}) {
|
|
444
|
+
if (!labels || !Object.keys(labels).length) {
|
|
445
|
+
throw new TypeError('Name the labels whose sources to delete, such as { channel: "gmail" }.');
|
|
446
|
+
}
|
|
447
|
+
if (opts.keep !== undefined)
|
|
448
|
+
return this.deleteAllBut(labels, opts.keep);
|
|
449
|
+
let total = 0;
|
|
450
|
+
for (let i = 0; i < DELETE_CALLS; i++) {
|
|
451
|
+
const out = await this.client.request("DELETE", "/v1/sources", {
|
|
452
|
+
query: { label: labelQuery(labels) },
|
|
453
|
+
});
|
|
454
|
+
total += out.sources_deleted ?? 0;
|
|
455
|
+
if (!out.more)
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
return total;
|
|
307
459
|
}
|
|
308
|
-
async
|
|
309
|
-
|
|
460
|
+
async deleteAllBut(labels, keep) {
|
|
461
|
+
// one id on its own would read as its letters, and keep nothing
|
|
462
|
+
if (typeof keep === "string")
|
|
463
|
+
throw new TypeError("keep is a list or set of external ids, not one id.");
|
|
464
|
+
const kept = new Set(Array.from(keep, String));
|
|
465
|
+
const gone = [];
|
|
466
|
+
for (let cursor = 0; cursor !== null;) { // the whole list first: deleting moves the pages
|
|
467
|
+
const page = await this.list({ labels, cursor });
|
|
468
|
+
for (const s of page.sources)
|
|
469
|
+
if (!(s.external_id != null && kept.has(s.external_id)))
|
|
470
|
+
gone.push(s.id);
|
|
471
|
+
cursor = page.next;
|
|
472
|
+
}
|
|
473
|
+
let deleted = 0;
|
|
474
|
+
for (const id of gone) {
|
|
475
|
+
try {
|
|
476
|
+
await this.delete(id);
|
|
477
|
+
deleted++;
|
|
478
|
+
}
|
|
479
|
+
catch (e) {
|
|
480
|
+
if (!(e instanceof NotFoundError))
|
|
481
|
+
throw e; // deleted meanwhile: gone either way
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return deleted;
|
|
310
485
|
}
|
|
311
|
-
/**
|
|
312
|
-
async
|
|
313
|
-
|
|
486
|
+
/** A source, by its id or as { externalId } (a NotFoundError when no source has that external id). */
|
|
487
|
+
async get(ref) {
|
|
488
|
+
const { id, externalId } = lookup(ref);
|
|
489
|
+
if (id !== undefined) {
|
|
490
|
+
return (await this.client.request("GET", `/v1/sources/${encodeURIComponent(id)}`)).source;
|
|
491
|
+
}
|
|
492
|
+
const { sources } = await this.client.request("GET", "/v1/sources", { query: { external_id: externalId } });
|
|
493
|
+
if (!sources.length) {
|
|
494
|
+
throw new NotFoundError("No source in this space has that external_id.", { status: 404, code: "not_found" });
|
|
495
|
+
}
|
|
496
|
+
return sources[0];
|
|
497
|
+
}
|
|
498
|
+
/** Delete a source and every memory learned only from it, by its id or as { externalId }: the call for a
|
|
499
|
+
* record your app deleted. */
|
|
500
|
+
async delete(ref) {
|
|
501
|
+
const { id, externalId } = lookup(ref);
|
|
502
|
+
if (id !== undefined)
|
|
503
|
+
await this.client.request("DELETE", `/v1/sources/${encodeURIComponent(id)}`);
|
|
504
|
+
else
|
|
505
|
+
await this.client.request("DELETE", "/v1/sources", { query: { external_id: externalId } });
|
|
314
506
|
}
|
|
315
507
|
/** Wait until Geniffy has learned from a source (or could not), then return it. Geniffy holds the
|
|
316
508
|
* call open until the source is done, so this returns the moment it is, usually in one call. */
|
|
@@ -329,4 +521,58 @@ export class Sources {
|
|
|
329
521
|
}
|
|
330
522
|
}
|
|
331
523
|
}
|
|
524
|
+
/**
|
|
525
|
+
* Files kept exactly as they were written, each under a path such as /memories/notes.md: what an agent keeps for
|
|
526
|
+
* itself, like the notes Claude's memory tool writes (see `geniffy/claude`). Geniffy also learns from each file like
|
|
527
|
+
* a note titled by its path, so context() and ask() recall what it says, and deleting it takes back what only it
|
|
528
|
+
* taught.
|
|
529
|
+
*
|
|
530
|
+
* A path starts with "/" and has at most 255 characters, with no empty, "." or ".." parts and no backslash.
|
|
531
|
+
*/
|
|
532
|
+
export class Files {
|
|
533
|
+
client;
|
|
534
|
+
constructor(client) {
|
|
535
|
+
this.client = client;
|
|
536
|
+
}
|
|
537
|
+
/** Create a file, or replace its text. The text is kept exactly as sent, whitespace and line endings included,
|
|
538
|
+
* and learned like a note; a replace learns only what changed. An empty file is kept too, with nothing learned
|
|
539
|
+
* from it. labels: as on memories.add. */
|
|
540
|
+
put(path, text, opts = {}) {
|
|
541
|
+
return this.client.request("PUT", "/v1/files", { json: { path, text, labels: opts.labels } });
|
|
542
|
+
}
|
|
543
|
+
/** A file and its exact text (a NotFoundError when no file has that path). */
|
|
544
|
+
get(path) {
|
|
545
|
+
return this.client.request("GET", "/v1/files", { query: { path } });
|
|
546
|
+
}
|
|
547
|
+
/** The files whose paths start with `prefix`, by path, without their text ("/", the default, for all of them).
|
|
548
|
+
* Up to 200 a page; `next` is the cursor of the page after, null at the end. */
|
|
549
|
+
list(opts = {}) {
|
|
550
|
+
return this.client.request("GET", "/v1/files", {
|
|
551
|
+
query: { prefix: opts.prefix ?? "/", limit: opts.limit ?? 100, cursor: opts.cursor ?? 0 },
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
/** Delete a file, its text and what only it taught (a NotFoundError when no file has that path). */
|
|
555
|
+
async delete(path) {
|
|
556
|
+
await this.client.request("DELETE", "/v1/files", { query: { path } });
|
|
557
|
+
}
|
|
558
|
+
/** Delete every file whose path starts with `prefix` ("/memories/" for everything in that folder), with what
|
|
559
|
+
* only they taught. Resolves to how many. */
|
|
560
|
+
async deletePrefix(prefix) {
|
|
561
|
+
let total = 0;
|
|
562
|
+
for (let i = 0; i < DELETE_CALLS; i++) {
|
|
563
|
+
const out = await this.client.request("DELETE", "/v1/files", { query: { prefix } });
|
|
564
|
+
total += out.deleted ?? 0;
|
|
565
|
+
if (!out.more)
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
return total;
|
|
569
|
+
}
|
|
570
|
+
/** Move a file, or, when `from` is a folder, every file in it, keeping the text and what was learned. A
|
|
571
|
+
* NotFoundError when there is nothing to move; a BadRequestError with code "conflict" when a destination is
|
|
572
|
+
* already taken, and then nothing moves. Resolves to how many files moved. */
|
|
573
|
+
async move(from, to) {
|
|
574
|
+
const out = await this.client.request("POST", "/v1/files/move", { json: { from, to } });
|
|
575
|
+
return out.moved ?? 0;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
332
578
|
export default Geniffy;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "geniffy",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Geniffy for TypeScript and JavaScript: add notes, files and links to a memory, then search it and ask it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./claude": {
|
|
14
|
+
"types": "./dist/claude.d.ts",
|
|
15
|
+
"import": "./dist/claude.js"
|
|
12
16
|
}
|
|
13
17
|
},
|
|
14
18
|
"files": ["dist", "README.md"],
|
|
@@ -16,9 +20,9 @@
|
|
|
16
20
|
"engines": { "node": ">=18" },
|
|
17
21
|
"scripts": {
|
|
18
22
|
"build": "tsc -p tsconfig.json",
|
|
19
|
-
"test": "npm run build && node --test test/client.test.mjs"
|
|
23
|
+
"test": "npm run build && node --test test/client.test.mjs test/claude.test.mjs"
|
|
20
24
|
},
|
|
21
|
-
"keywords": ["memory", "ai", "agents", "llm", "rag", "geniffy"],
|
|
25
|
+
"keywords": ["memory", "ai", "agents", "llm", "rag", "geniffy", "claude", "memory-tool"],
|
|
22
26
|
"homepage": "https://docs.geniffy.com/sdks/typescript",
|
|
23
27
|
"repository": {
|
|
24
28
|
"type": "git",
|
|
@@ -29,7 +33,14 @@
|
|
|
29
33
|
},
|
|
30
34
|
"author": "Geniffy <ops@geniffy.com>",
|
|
31
35
|
"license": "MIT",
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@anthropic-ai/sdk": ">=0.72.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"@anthropic-ai/sdk": { "optional": true }
|
|
41
|
+
},
|
|
32
42
|
"devDependencies": {
|
|
43
|
+
"@anthropic-ai/sdk": "^0.131.0",
|
|
33
44
|
"typescript": "^5.6.0"
|
|
34
45
|
}
|
|
35
46
|
}
|