geniffy 0.4.0 → 0.5.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 CHANGED
@@ -30,16 +30,21 @@ Ask about something that was never stored and it returns one sentence, `There is
30
30
  this yet. Say so rather than guessing.`, never an empty string: a model reads silence as permission to
31
31
  invent.
32
32
 
33
- ## Three ways to recall
33
+ ## Recall: fast reads every turn, a checked answer when it matters
34
34
 
35
35
  ```ts
36
- await mem.context("Who signs the renewal?"); // a block for your own prompt; the one most apps want
36
+ await mem.briefing({ cue: userMessage }); // what this moment needs, for your system prompt; read, not
37
+ // generated: a fraction of a second, fit for every turn
38
+ await mem.search("renewal", { limit: 5 }); // the memories, ranked, fast
39
+ await mem.searchFull("port 6543"); // the same, with the lessons, stories and open promises
37
40
  await mem.ask("Who signs the renewal?"); // { answer, memories }, or answer: null and a message
38
- await mem.search("renewal", { limit: 5 }); // the raw memories, ranked, to do with as you like
41
+ await mem.context("Who signs the renewal?"); // the memories an answer stands on, as a prompt block
39
42
  ```
40
43
 
41
- `context()` and `ask()` judge whether anything bears on the question. `search()` ranks and does not
42
- judge: it returns its best matches for any question at all.
44
+ `briefing()` and `search()` read: nothing is generated, so they are fast enough for every turn, voice calls included.
45
+ `ask()` and `context()` have a model judge whether anything bears on the question, which takes a couple of seconds
46
+ and never invents: use them when an answer must be checked, not on every turn of a call. `search()` ranks and does
47
+ not judge: it returns its best matches for any question at all.
43
48
 
44
49
  ## Agents: open with the briefing, save every turn
45
50
 
package/dist/index.d.ts CHANGED
@@ -10,8 +10,15 @@
10
10
  * Retries: reads, deletes and files.put (the same text twice is the same file) are retried on network errors,
11
11
  * 408, 429 and 5xx; adding is retried only on 429, so a retry never saves a note twice.
12
12
  */
13
- export declare const VERSION = "0.4.0";
13
+ export declare const VERSION = "0.5.0";
14
14
  export declare const DEFAULT_BASE_URL = "https://api.geniffy.com";
15
+ /** Each region's address, by name. An account's memory is stored in the region it was made in, and its key works only
16
+ * there, so a client calls that region; "in" is the default. */
17
+ export declare const REGIONS: {
18
+ readonly in: "https://api.geniffy.com";
19
+ readonly eu: "https://eu.api.geniffy.com";
20
+ };
21
+ export type Region = keyof typeof REGIONS;
15
22
  export type Kind = "all" | "people" | "plan" | "pref" | "detail";
16
23
  export interface SourceRef {
17
24
  id: string | null;
@@ -47,7 +54,7 @@ export interface MemoryDetail {
47
54
  /** This month's use, for the whole account, against the plan (usage()). Saving, briefings and search are free and
48
55
  * never counted. */
49
56
  export interface Usage {
50
- /** free, pro or max: the same plans for everyone, the API in every one */
57
+ /** free, pro or max: the same plans for everyone, the API in every one; startup or scale: a company's plan */
51
58
  plan: string;
52
59
  /** on the 14 days of Pro every account gets once */
53
60
  trial: boolean;
@@ -111,8 +118,11 @@ export interface Answer {
111
118
  export interface ClientOptions {
112
119
  /** Defaults to GENIFFY_API_KEY. */
113
120
  apiKey?: string;
114
- /** Defaults to GENIFFY_BASE_URL, then https://api.geniffy.com */
121
+ /** Defaults to GENIFFY_BASE_URL, then the address of `region`. */
115
122
  baseURL?: string;
123
+ /** The region your account was made in; leave it out. Defaults to GENIFFY_REGION. A baseURL wins over it, but one
124
+ * that is another region's address is refused. */
125
+ region?: Region;
116
126
  /** Milliseconds per attempt. Default 60000. */
117
127
  timeout?: number;
118
128
  /** Default 2. */
@@ -220,6 +230,14 @@ export declare class Geniffy {
220
230
  kind?: Kind;
221
231
  labels?: LabelFilter;
222
232
  }): Promise<Memory[]>;
233
+ /** The same search, with everything else the memory learned that bears on `q`: the memories, then the lessons
234
+ * (rules and how-tos), episodes (what happened) and intentions (open promises) that match it, by meaning and by
235
+ * the words themselves, each naming its source. With labels, or a kind other than all, only memories come back. */
236
+ searchFull(q: string, opts?: {
237
+ limit?: number;
238
+ kind?: Kind;
239
+ labels?: LabelFilter;
240
+ }): Promise<SearchFull>;
223
241
  /**
224
242
  * The memories that bear on a question, already written out for YOUR prompt.
225
243
  *
@@ -299,7 +317,8 @@ export declare class Geniffy {
299
317
  /** What the memory holds and what connects to what. Every line has a memory behind it. */
300
318
  graph(): Promise<Graph>;
301
319
  /** Everything held, as the user's own copy: every memory, current or not, with its status and the sentence it
302
- * came from, every source, and every file by its path (files.get(path) reads each one's text). On
320
+ * came from, every source, every file by its path (files.get(path) reads each one's text), and the stories,
321
+ * lessons and promises read from them, each with every source it names, and where things stand. On
303
322
  * client.space(id), for a user who asks what you hold about them. */
304
323
  export(): Promise<Export>;
305
324
  /** Whose key this is, and which space this client is reading. */
@@ -309,9 +328,8 @@ export declare class Geniffy {
309
328
  space: string | null;
310
329
  }>;
311
330
  /**
312
- * This month's use for the whole account: what it comes to in dollars ($4 per million tokens learned, $6 per
313
- * 1,000 answers), what the plan includes, the most the month can come to, and when it resets. Search and recall
314
- * are free and not counted.
331
+ * This month's use for the whole account, against the plan: tokens learned and waiting, answers, what the plan
332
+ * includes, extra learning, and when it resets. Saving, briefings and search are free and never counted.
315
333
  */
316
334
  usage(): Promise<Usage>;
317
335
  /**
@@ -378,6 +396,24 @@ export interface Export {
378
396
  /** every file kept, by path and without its text, which files.get(path) returns exactly as written (left out by
379
397
  * an API from before files) */
380
398
  files?: FileEntry[];
399
+ /** every story of what happened, oldest first, each with its status: closed, or open when it was still going as
400
+ * its source ended (left out by an older API) */
401
+ episodes?: Array<Episode & {
402
+ status: string | null;
403
+ }>;
404
+ /** every rule, how-to and lesson, oldest first, in use or not (its status: current, merged or struck), each with
405
+ * every source it was learned from, oldest first (left out by an older API) */
406
+ lessons?: Array<Omit<Lesson, "sources"> & {
407
+ sources: SourceRef[];
408
+ status: string | null;
409
+ }>;
410
+ /** every promise and plan, oldest first: open, done or dropped, each with every source it came from, oldest first
411
+ * (left out by an older API) */
412
+ intentions?: Array<Omit<Intention, "sources"> & {
413
+ sources: SourceRef[];
414
+ }>;
415
+ /** where things stand, per project (left out by an older API) */
416
+ now?: State[];
381
417
  }
382
418
  /** Where one project stands. */
383
419
  export interface State {
@@ -435,7 +471,10 @@ export interface Lesson {
435
471
  why: string | null;
436
472
  /** how many times it was learned again */
437
473
  strength: number;
474
+ /** the sources it was learned from last, five at most, the oldest of them first */
438
475
  sources: SourceRef[];
476
+ /** how many sources it was learned from in all (left out by an older API) */
477
+ sources_total?: number;
439
478
  }
440
479
  /** A promise or plan, with its trigger. */
441
480
  export interface Intention {
@@ -449,7 +488,23 @@ export interface Intention {
449
488
  /** its trigger in words: "when testing ends" */
450
489
  when: string | null;
451
490
  status: "open" | "done" | "dropped" | null;
491
+ /** the sources it came from last, five at most, the oldest of them first */
452
492
  sources: SourceRef[];
493
+ /** how many sources it came from in all (left out by an older API) */
494
+ sources_total?: number;
495
+ /** when it was due before a later conversation moved it, oldest first, named as at and when are; empty when it
496
+ * was never moved (left out by an older API) */
497
+ moved_from?: Array<{
498
+ at: string | null;
499
+ when: string | null;
500
+ }>;
501
+ }
502
+ /** What searchFull() found: the memories, then the lessons, episodes and open promises that bear on the query. */
503
+ export interface SearchFull {
504
+ memories: Memory[];
505
+ lessons: Lesson[];
506
+ episodes: Episode[];
507
+ intentions: Intention[];
453
508
  }
454
509
  /** How well the memory answers about its own work, from the questions it asks itself each night. */
455
510
  export interface MemoryHealth {
package/dist/index.js CHANGED
@@ -10,8 +10,11 @@
10
10
  * Retries: reads, deletes and files.put (the same text twice is the same file) are retried on network errors,
11
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.4.0";
13
+ export const VERSION = "0.5.0";
14
14
  export const DEFAULT_BASE_URL = "https://api.geniffy.com";
15
+ /** Each region's address, by name. An account's memory is stored in the region it was made in, and its key works only
16
+ * there, so a client calls that region; "in" is the default. */
17
+ export const REGIONS = { in: DEFAULT_BASE_URL, eu: "https://eu.api.geniffy.com" };
15
18
  /** A filter in a query string: label=name:value, once for each value. */
16
19
  function labelQuery(labels) {
17
20
  if (!labels)
@@ -105,6 +108,33 @@ function env(name) {
105
108
  const g = globalThis;
106
109
  return g.process?.env?.[name];
107
110
  }
111
+ // An option, else its environment variable. A blank one counts as not set: docker compose passes a variable the
112
+ // host never set as "", which means the default, not a region or an address named "".
113
+ function setting(given, name) {
114
+ return given?.trim() || env(name)?.trim() || undefined;
115
+ }
116
+ // Where calls go: baseURL, else GENIFFY_BASE_URL, else the region's address (region, else GENIFFY_REGION, else "in").
117
+ // A region is checked wherever it is named, a base URL or not. A base URL that is another region's address is refused
118
+ // rather than called: a GENIFFY_BASE_URL left over in an environment would otherwise send one region's data to the
119
+ // other, under a key that works only in its own. Any other address (a local server, a test double) wins as before.
120
+ function baseAddress(baseURL, region) {
121
+ const said = region?.trim() ? "region" : "GENIFFY_REGION";
122
+ const where = setting(region, "GENIFFY_REGION")?.toLowerCase();
123
+ if (where && !Object.prototype.hasOwnProperty.call(REGIONS, where)) {
124
+ throw new TypeError(`${said} is one of: ${Object.keys(REGIONS).join(", ")} (the region your account was made in).`);
125
+ }
126
+ const given = baseURL?.trim() ? "baseURL" : "GENIFFY_BASE_URL";
127
+ const named = setting(baseURL, "GENIFFY_BASE_URL");
128
+ if (!named)
129
+ return REGIONS[(where ?? "in")];
130
+ const url = named.replace(/\/+$/, "");
131
+ const other = Object.keys(REGIONS).find((name) => REGIONS[name] === url.toLowerCase());
132
+ if (where && other && other !== where) {
133
+ throw new TypeError(`${said} is '${where}', but ${given} is the address of region '${other}' (${url}). Keep one ` +
134
+ "of the two: the region your account was made in.");
135
+ }
136
+ return url;
137
+ }
108
138
  function delay(attempt, response) {
109
139
  const after = Number(response?.headers.get("retry-after"));
110
140
  if (response && Number.isFinite(after) && after >= 0 && after <= 60)
@@ -136,7 +166,7 @@ export class Geniffy {
136
166
  }
137
167
  this.#opts = opts;
138
168
  this.#key = key;
139
- this.#base = (opts.baseURL ?? env("GENIFFY_BASE_URL") ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
169
+ this.#base = baseAddress(opts.baseURL, opts.region);
140
170
  this.#fetch = opts.fetch ?? globalThis.fetch.bind(globalThis);
141
171
  this.timeout = opts.timeout ?? 60_000;
142
172
  this.maxRetries = Math.max(0, opts.maxRetries ?? 2);
@@ -239,6 +269,16 @@ export class Geniffy {
239
269
  });
240
270
  return out.memories;
241
271
  }
272
+ /** The same search, with everything else the memory learned that bears on `q`: the memories, then the lessons
273
+ * (rules and how-tos), episodes (what happened) and intentions (open promises) that match it, by meaning and by
274
+ * the words themselves, each naming its source. With labels, or a kind other than all, only memories come back. */
275
+ async searchFull(q, opts = {}) {
276
+ const out = await this.request("POST", "/v1/search", {
277
+ json: { q, limit: opts.limit ?? 10, kind: opts.kind, labels: opts.labels },
278
+ });
279
+ return { memories: out.memories ?? [], lessons: out.lessons ?? [], episodes: out.episodes ?? [],
280
+ intentions: out.intentions ?? [] };
281
+ }
242
282
  /**
243
283
  * The memories that bear on a question, already written out for YOUR prompt.
244
284
  *
@@ -321,7 +361,8 @@ export class Geniffy {
321
361
  return this.request("GET", "/v1/graph");
322
362
  }
323
363
  /** Everything held, as the user's own copy: every memory, current or not, with its status and the sentence it
324
- * came from, every source, and every file by its path (files.get(path) reads each one's text). On
364
+ * came from, every source, every file by its path (files.get(path) reads each one's text), and the stories,
365
+ * lessons and promises read from them, each with every source it names, and where things stand. On
325
366
  * client.space(id), for a user who asks what you hold about them. */
326
367
  export() {
327
368
  return this.request("GET", "/v1/export");
@@ -331,9 +372,8 @@ export class Geniffy {
331
372
  return this.request("GET", "/v1/me");
332
373
  }
333
374
  /**
334
- * This month's use for the whole account: what it comes to in dollars ($4 per million tokens learned, $6 per
335
- * 1,000 answers), what the plan includes, the most the month can come to, and when it resets. Search and recall
336
- * are free and not counted.
375
+ * This month's use for the whole account, against the plan: tokens learned and waiting, answers, what the plan
376
+ * includes, extra learning, and when it resets. Saving, briefings and search are free and never counted.
337
377
  */
338
378
  usage() {
339
379
  return this.request("GET", "/v1/usage");
@@ -669,6 +709,21 @@ function stable(value) {
669
709
  }
670
710
  return JSON.stringify(value) ?? "null";
671
711
  }
712
+ // Where the messages not sent yet begin: after the last one already sent. A conversation that only grew still has it
713
+ // where it was sent from (at, the count of messages up to it), so a message said again later ("Okay.") is never taken
714
+ // for it and the turns before it lost. When it has moved (the conversation was rewritten or compacted), after its
715
+ // latest copy; when it is no longer there, at the start: saving a turn twice is better than losing what changed.
716
+ function unsentFrom(messages, last, at) {
717
+ if (last === null)
718
+ return 0;
719
+ if (at > 0 && at <= messages.length && stable(messages[at - 1]) === last)
720
+ return at;
721
+ for (let i = messages.length - 1; i >= 0; i--) {
722
+ if (stable(messages[i]) === last)
723
+ return i + 1;
724
+ }
725
+ return 0;
726
+ }
672
727
  /**
673
728
  * An agent's or a chat's session, saved as it goes into one memory for the whole session. Hand save() the whole
674
729
  * conversation after each turn: only the messages after the last one it sent go to Geniffy, at most 500 a call, so a
@@ -678,6 +733,7 @@ function stable(value) {
678
733
  export class Session {
679
734
  client;
680
735
  #last = null;
736
+ #at = 0; // where the last message sent was: how many messages, up to and including it
681
737
  id;
682
738
  title;
683
739
  labels;
@@ -690,15 +746,7 @@ export class Session {
690
746
  /** Sends what is new; resolves to the session's source, or null when nothing was new. */
691
747
  async save(messages) {
692
748
  const all = [...messages];
693
- let from = 0;
694
- if (this.#last !== null) {
695
- for (let i = all.length - 1; i >= 0; i--) {
696
- if (stable(all[i]) === this.#last) {
697
- from = i + 1;
698
- break;
699
- }
700
- }
701
- }
749
+ const from = unsentFrom(all, this.#last, this.#at);
702
750
  const fresh = all.slice(from);
703
751
  let source = null;
704
752
  for (let i = 0; i < fresh.length; i += SESSION_CALL) {
@@ -710,6 +758,7 @@ export class Session {
710
758
  input.labels = this.labels;
711
759
  source = await this.client.memories.add(input);
712
760
  this.#last = stable(part[part.length - 1]);
761
+ this.#at = from + i + part.length;
713
762
  }
714
763
  return source;
715
764
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geniffy",
3
- "version": "0.4.0",
3
+ "version": "0.5.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",
@@ -20,7 +20,7 @@
20
20
  "engines": { "node": ">=18" },
21
21
  "scripts": {
22
22
  "build": "tsc -p tsconfig.json",
23
- "test": "npm run build && node --test test/client.test.mjs test/claude.test.mjs test/briefing.test.mjs test/session-usage.test.mjs"
23
+ "test": "npm run build && node --test test/client.test.mjs test/claude.test.mjs test/briefing.test.mjs test/session-usage.test.mjs test/regions-search.test.mjs"
24
24
  },
25
25
  "keywords": ["memory", "ai", "agents", "llm", "rag", "geniffy", "claude", "memory-tool"],
26
26
  "homepage": "https://docs.geniffy.com/sdks/typescript",