geniffy 0.3.0 → 0.4.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
@@ -41,6 +41,31 @@ await mem.search("renewal", { limit: 5 }); // the raw memories, ranke
41
41
  `context()` and `ask()` judge whether anything bears on the question. `search()` ranks and does not
42
42
  judge: it returns its best matches for any question at all.
43
43
 
44
+ ## Agents: open with the briefing, save every turn
45
+
46
+ An agent, or any chat that runs over many turns, does two things: it opens each session with the briefing, and it
47
+ saves every turn as it goes.
48
+
49
+ ```ts
50
+ const system = await mem.briefing({ project: "checkout", cue: task }); // where things stand, what is due, the
51
+ // rules that apply, what happened, then
52
+ // the memories, each dated
53
+ const run = mem.session(runId, { title: "Refund agent" });
54
+ // ...
55
+ await run.save(messages); // after every turn: only what is new is sent
56
+ ```
57
+
58
+ `briefing()` is written to go straight into a system prompt. `briefingFull()` has its parts, and each part can be
59
+ read on its own: `now()` (where each project stands), `episodes()`, `lessons()` and `intentions()`;
60
+ `setIntention(id, "done")` marks a promise kept, and `memoryHealth()` says how well the memory answers about its
61
+ own work.
62
+
63
+ A session's turns go into one memory for the whole session, tool calls and what came back included, in the shapes
64
+ OpenAI, Anthropic, the Vercel AI SDK, Gemini and LangChain hold them. `save()` sends only the messages after the
65
+ last one it sent, 500 at a time, so a session of any length is never sent twice; if your agent rewrites its
66
+ history, what it holds now is saved again rather than lost. Sending the new turns yourself instead:
67
+ `mem.memories.add({ messages: newTurns, session: runId })`.
68
+
44
69
  ## Spaces: one memory per user
45
70
 
46
71
  A space is your own name for one of your users. Bind a client to it and every call stays inside it.
@@ -152,11 +177,21 @@ Claude wrote, and `clearAllMemory(mem)` deletes it all (both from `geniffy/claud
152
177
  calls of one reply at the same time, so the handlers take them one after another: two edits to one file both land.
153
178
  `geniffy/claude` needs `@anthropic-ai/sdk` 0.72 or later, an optional peer dependency; `geniffy` itself still has none.
154
179
 
180
+ ## This month's use
181
+
182
+ ```ts
183
+ await client.usage(); // tokens learned and waiting, answers, what the plan includes, extra learning, when it resets
184
+ ```
185
+
186
+ Saving, briefings, search and recall are free. Past your plan, a source comes back with status `"waiting"`: kept,
187
+ found by search, and learned next month or at once with extra learning on. Only at the fair-use limit does adding
188
+ throw `UsageLimitError`.
189
+
155
190
  ## Errors, retries and request ids
156
191
 
157
192
  Every error carries the API's own sentence: `AuthenticationError` (a wrong or revoked key),
158
- `NotFoundError`, `BadRequestError`, `UnreadableError`, `RateLimitError`, `InternalServerError`,
159
- `APIConnectionError`; all extend `GeniffyError`. Reads, and putting a file (the same text twice is the same
193
+ `NotFoundError`, `BadRequestError`, `UnreadableError`, `RateLimitError`, `UsageLimitError` (this month's use is
194
+ up; not retried), `InternalServerError`, `APIConnectionError`; all extend `GeniffyError`. Reads, and putting a file (the same text twice is the same
160
195
  file), are retried twice on network errors, 408, 429 and 5xx; adding is retried only on 429, so a retry never
161
196
  saves a note twice. Pass `{ maxRetries, timeout }` to the constructor to change that.
162
197
 
package/dist/index.d.ts CHANGED
@@ -10,7 +10,7 @@
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.3.0";
13
+ export declare const VERSION = "0.4.0";
14
14
  export declare const DEFAULT_BASE_URL = "https://api.geniffy.com";
15
15
  export type Kind = "all" | "people" | "plan" | "pref" | "detail";
16
16
  export interface SourceRef {
@@ -44,11 +44,38 @@ export interface MemoryDetail {
44
44
  memory: Memory;
45
45
  history: Memory[];
46
46
  }
47
+ /** This month's use, for the whole account, against the plan (usage()). Saving, briefings and search are free and
48
+ * never counted. */
49
+ export interface Usage {
50
+ /** free, pro or max: the same plans for everyone, the API in every one */
51
+ plan: string;
52
+ /** on the 14 days of Pro every account gets once */
53
+ trial: boolean;
54
+ period_start: string;
55
+ /** when it resets */
56
+ period_end: string;
57
+ /** tokens learned this month from everything saved, every way in, your app's users included */
58
+ learned_tokens: number;
59
+ /** the tokens the plan learns in a month */
60
+ included_tokens: number;
61
+ /** saved and searchable, waiting to be learned next month (or at once with extra learning on) */
62
+ waiting_tokens: number;
63
+ /** written answers and context blocks */
64
+ answers: number;
65
+ included_answers: number;
66
+ /** extra learning: past the plan, learned and answered at once, $15 per million tokens, $15 per 1,000 answers */
67
+ extra_on: boolean;
68
+ extra_cap_usd: number;
69
+ extra_used_usd: number;
70
+ /** learning against the plan: near at 80%; over: new saves wait; extra: extra learning carries on */
71
+ state: "ok" | "near" | "over" | "extra" | "paused";
72
+ }
47
73
  export interface Source {
48
74
  id: string;
49
75
  kind: "note" | "file" | "link";
50
76
  title: string;
51
- status: "reading" | "learned" | "failed";
77
+ /** waiting: saved past this month's use, kept, and learned once there is room */
78
+ status: "reading" | "learned" | "failed" | "waiting";
52
79
  /** why it failed, in plain words */
53
80
  error?: string | null;
54
81
  facts?: number | null;
@@ -136,6 +163,13 @@ export declare class UnreadableError extends BadRequestError {
136
163
  }
137
164
  export declare class RateLimitError extends GeniffyError {
138
165
  }
166
+ /**
167
+ * This month's use is up (402, code allowance_used): past the plan, what waits to be learned has reached its fair-use
168
+ * limit (ten months of the plan's learning). Search and recall keep working; saving resumes when the month resets
169
+ * (usage() says when), at once with extra learning on, or on a bigger plan. Not retried.
170
+ */
171
+ export declare class UsageLimitError extends GeniffyError {
172
+ }
139
173
  export declare class InternalServerError extends GeniffyError {
140
174
  }
141
175
  type Body = {
@@ -217,6 +251,51 @@ export declare class Geniffy {
217
251
  limit?: number;
218
252
  labels?: LabelFilter;
219
253
  }): Promise<Brief>;
254
+ /**
255
+ * What this moment needs, written out for YOUR prompt within `budgetChars`: where the project stands (goal,
256
+ * focus, open items, decisions, next steps), what is due or was promised, the rules and lessons that apply,
257
+ * what happened (the latest and the most related to `cue` first), then the memories, each dated. Open a session
258
+ * with it, and call it again when the task changes:
259
+ *
260
+ * const system = `${await mem.briefing({ project: "checkout", cue: userMessage })}\n\n${instructions}`;
261
+ *
262
+ * `project` is the label your sources carry (a project, context, thread or repo label); leave it out for every
263
+ * project. Read, not written by a model, so it is fast enough for every turn.
264
+ */
265
+ briefing(opts?: {
266
+ project?: string;
267
+ cue?: string;
268
+ budgetChars?: number;
269
+ }): Promise<string>;
270
+ /** The same, with its parts: now, due, lessons, episodes and memories, each with its source. */
271
+ briefingFull(opts?: {
272
+ project?: string;
273
+ cue?: string;
274
+ budgetChars?: number;
275
+ }): Promise<Briefing>;
276
+ /** Where things stand, per project: goal, focus, what is open (and since when), decisions with their reasons,
277
+ * next steps, blockers and what was finished. */
278
+ now(project?: string): Promise<State[]>;
279
+ /** What happened, as stories, newest first: what happened, how it ended, what it led to, its source. */
280
+ episodes(opts?: {
281
+ project?: string;
282
+ limit?: number;
283
+ }): Promise<Episode[]>;
284
+ /** Rules, how-tos and lessons, strongest first, each with when it applies and why. */
285
+ lessons(opts?: {
286
+ project?: string;
287
+ limit?: number;
288
+ }): Promise<Lesson[]>;
289
+ /** Promises and plans, yours and those made to you, with their triggers: open (the default), done or dropped. */
290
+ intentions(opts?: {
291
+ status?: "open" | "done" | "dropped";
292
+ limit?: number;
293
+ }): Promise<Intention[]>;
294
+ /** How well the memory answers about its own work: each night it writes questions from what it read, answers them
295
+ * and marks the answers. `health` is the share right (0 to 1), with the questions; null before its first night. */
296
+ memoryHealth(): Promise<MemoryHealth>;
297
+ /** Mark a promise or plan done, dropped, or open again. */
298
+ setIntention(id: number, status?: "done" | "dropped" | "open"): Promise<Intention>;
220
299
  /** What the memory holds and what connects to what. Every line has a memory behind it. */
221
300
  graph(): Promise<Graph>;
222
301
  /** Everything held, as the user's own copy: every memory, current or not, with its status and the sentence it
@@ -229,6 +308,22 @@ export declare class Geniffy {
229
308
  memory: string;
230
309
  space: string | null;
231
310
  }>;
311
+ /**
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.
315
+ */
316
+ usage(): Promise<Usage>;
317
+ /**
318
+ * An agent's or a chat's session, saved as it goes into one memory: hand save() the whole conversation after each
319
+ * turn, and only the messages it has not sent yet go to Geniffy.
320
+ *
321
+ * const run = mem.session("run-42", { title: "Refund agent" });
322
+ * await run.save(messages); // after every turn
323
+ */
324
+ session(id: string, opts?: {
325
+ title?: string;
326
+ } & WithLabels): Session;
232
327
  /** Which of your users have memory, busiest first. */
233
328
  spaces(): Promise<SpaceRow[]>;
234
329
  /**
@@ -284,6 +379,102 @@ export interface Export {
284
379
  * an API from before files) */
285
380
  files?: FileEntry[];
286
381
  }
382
+ /** Where one project stands. */
383
+ export interface State {
384
+ project: string | null;
385
+ goal: string | null;
386
+ focus: string | null;
387
+ open: Array<{
388
+ text: string;
389
+ since: string | null;
390
+ }>;
391
+ decisions: Array<{
392
+ what: string;
393
+ why: string | null;
394
+ when: string | null;
395
+ }>;
396
+ next_steps: string[];
397
+ blockers: string[];
398
+ done: Array<{
399
+ text: string;
400
+ when: string | null;
401
+ }>;
402
+ updated_at: string | null;
403
+ }
404
+ /** Something that happened, as a story. */
405
+ export interface Episode {
406
+ id: number;
407
+ project: string | null;
408
+ title: string | null;
409
+ what_happened: string | null;
410
+ /** how it ended */
411
+ outcome: string | null;
412
+ led_to: string | null;
413
+ people: string[];
414
+ decisions: Array<{
415
+ what: string;
416
+ why: string | null;
417
+ }>;
418
+ /** endings a later conversation corrected, oldest first: what it first seemed, and what showed otherwise */
419
+ revised?: Array<{
420
+ was: string | null;
421
+ why: string | null;
422
+ at: string | null;
423
+ }>;
424
+ started_at: string | null;
425
+ ended_at: string | null;
426
+ source: SourceRef;
427
+ }
428
+ /** A rule, how-to or lesson, with when it applies and why. */
429
+ export interface Lesson {
430
+ id: number;
431
+ project: string | null;
432
+ kind: "rule" | "how_to" | "lesson" | null;
433
+ statement: string | null;
434
+ applies_when: string | null;
435
+ why: string | null;
436
+ /** how many times it was learned again */
437
+ strength: number;
438
+ sources: SourceRef[];
439
+ }
440
+ /** A promise or plan, with its trigger. */
441
+ export interface Intention {
442
+ id: number;
443
+ project: string | null;
444
+ what: string | null;
445
+ owner: string | null;
446
+ trigger: "time" | "context" | "person" | "event" | null;
447
+ /** when it is due, for a time */
448
+ at: string | null;
449
+ /** its trigger in words: "when testing ends" */
450
+ when: string | null;
451
+ status: "open" | "done" | "dropped" | null;
452
+ sources: SourceRef[];
453
+ }
454
+ /** How well the memory answers about its own work, from the questions it asks itself each night. */
455
+ export interface MemoryHealth {
456
+ health: number | null;
457
+ tested_at: string | null;
458
+ asked: number;
459
+ score: number | null;
460
+ items: Array<{
461
+ question: string | null;
462
+ expected: string | null;
463
+ answer: string | null;
464
+ mark: "correct" | "partial" | "wrong" | null;
465
+ }>;
466
+ }
467
+ /** What a session opens with. */
468
+ export interface Briefing {
469
+ project: string | null;
470
+ /** Put this straight into your prompt. */
471
+ briefing: string;
472
+ now: State[];
473
+ due: Intention[];
474
+ lessons: Lesson[];
475
+ episodes: Episode[];
476
+ memories: Memory[];
477
+ }
287
478
  export interface Graph {
288
479
  nodes: Array<{
289
480
  id: string;
@@ -309,13 +500,20 @@ export interface SpaceRow {
309
500
  }
310
501
  export interface ChatMessage {
311
502
  role: string;
312
- /** The words: a string, or the blocks or parts your framework holds (Anthropic, OpenAI). Only text is
313
- * kept; null is a turn that said nothing in words, such as one that only called a tool. */
503
+ /** The words: a string, or the blocks or parts your framework holds (Anthropic, OpenAI), tool calls and tool
504
+ * results included. Facts come only from what was said; a tool's call and output tell Geniffy what
505
+ * happened. */
314
506
  content?: string | unknown[] | null;
315
507
  /** Or the parts, as the Vercel AI SDK and Gemini hold a message. */
316
508
  parts?: unknown[];
317
509
  /** Who, when the role does not say: a tool name, a participant. */
318
510
  name?: string;
511
+ /** The tools an assistant called, as OpenAI and LangChain hold them. */
512
+ tool_calls?: unknown[];
513
+ /** For a tool result, the call it answers. */
514
+ tool_call_id?: string;
515
+ /** When it was said, ISO 8601, if your app keeps it. */
516
+ time?: string;
319
517
  }
320
518
  /** When a note or conversation from the past was said: a Date, or an ISO 8601 string (2026-03-04,
321
519
  * 2026-03-04T09:30:00Z). What it teaches is dated by it. Left out, now. */
@@ -347,7 +545,14 @@ export type AddInput = string | ({
347
545
  | ({
348
546
  messages: ChatMessage[];
349
547
  title?: string;
350
- } & SaidAt & ExternalId & WithLabels);
548
+ } & SaidAt & ExternalId & WithLabels)
549
+ /** The next turns of an agent's or a chat's session: each call adds its messages to one memory for the whole
550
+ * session, however long it gets, so send only what is new since the last call (session().save() works that out). */
551
+ | ({
552
+ messages: ChatMessage[];
553
+ session: string;
554
+ title?: string;
555
+ } & WithLabels);
351
556
  /** A source named by Geniffy's id, or by the external id you added it under. */
352
557
  export type SourceLookup = string | {
353
558
  externalId: string;
@@ -539,4 +744,22 @@ export declare class Files {
539
744
  * already taken, and then nothing moves. Resolves to how many files moved. */
540
745
  move(from: string, to: string): Promise<number>;
541
746
  }
747
+ /**
748
+ * An agent's or a chat's session, saved as it goes into one memory for the whole session. Hand save() the whole
749
+ * conversation after each turn: only the messages after the last one it sent go to Geniffy, at most 500 a call, so a
750
+ * session of any length is never sent twice. When that message is no longer there (the agent rewrote or compacted
751
+ * its history), what it holds now is saved again rather than lost. Made by client.session(id).
752
+ */
753
+ export declare class Session {
754
+ #private;
755
+ private readonly client;
756
+ readonly id: string;
757
+ readonly title?: string;
758
+ readonly labels?: Labels;
759
+ constructor(client: Geniffy, id: string, opts?: {
760
+ title?: string;
761
+ } & WithLabels);
762
+ /** Sends what is new; resolves to the session's source, or null when nothing was new. */
763
+ save(messages: ChatMessage[]): Promise<Source | null>;
764
+ }
542
765
  export default Geniffy;
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@
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.3.0";
13
+ export const VERSION = "0.4.0";
14
14
  export const DEFAULT_BASE_URL = "https://api.geniffy.com";
15
15
  /** A filter in a query string: label=name:value, once for each value. */
16
16
  function labelQuery(labels) {
@@ -70,6 +70,13 @@ export class UnreadableError extends BadRequestError {
70
70
  }
71
71
  export class RateLimitError extends GeniffyError {
72
72
  }
73
+ /**
74
+ * This month's use is up (402, code allowance_used): past the plan, what waits to be learned has reached its fair-use
75
+ * limit (ten months of the plan's learning). Search and recall keep working; saving resumes when the month resets
76
+ * (usage() says when), at once with extra learning on, or on a bigger plan. Not retried.
77
+ */
78
+ export class UsageLimitError extends GeniffyError {
79
+ }
73
80
  export class InternalServerError extends GeniffyError {
74
81
  }
75
82
  function errorFrom(status, body, requestId) {
@@ -84,6 +91,8 @@ function errorFrom(status, body, requestId) {
84
91
  return new UnreadableError(message, opts);
85
92
  if ([400, 409, 413, 422].includes(status))
86
93
  return new BadRequestError(message, opts);
94
+ if (status === 402)
95
+ return new UsageLimitError(message, opts);
87
96
  if (status === 429)
88
97
  return new RateLimitError(message, opts);
89
98
  if (status >= 500)
@@ -260,6 +269,53 @@ export class Geniffy {
260
269
  brief(subject, opts = {}) {
261
270
  return this.request("GET", "/v1/brief", { query: { subject, limit: opts.limit, label: labelQuery(opts.labels) } });
262
271
  }
272
+ // ── what a session opens with: where things stand, what happened, what was learned and promised
273
+ /**
274
+ * What this moment needs, written out for YOUR prompt within `budgetChars`: where the project stands (goal,
275
+ * focus, open items, decisions, next steps), what is due or was promised, the rules and lessons that apply,
276
+ * what happened (the latest and the most related to `cue` first), then the memories, each dated. Open a session
277
+ * with it, and call it again when the task changes:
278
+ *
279
+ * const system = `${await mem.briefing({ project: "checkout", cue: userMessage })}\n\n${instructions}`;
280
+ *
281
+ * `project` is the label your sources carry (a project, context, thread or repo label); leave it out for every
282
+ * project. Read, not written by a model, so it is fast enough for every turn.
283
+ */
284
+ async briefing(opts = {}) {
285
+ return (await this.briefingFull(opts)).briefing;
286
+ }
287
+ /** The same, with its parts: now, due, lessons, episodes and memories, each with its source. */
288
+ briefingFull(opts = {}) {
289
+ return this.request("POST", "/v1/briefing", {
290
+ json: { project: opts.project, cue: opts.cue ?? "", budget_chars: opts.budgetChars ?? 6000 },
291
+ });
292
+ }
293
+ /** Where things stand, per project: goal, focus, what is open (and since when), decisions with their reasons,
294
+ * next steps, blockers and what was finished. */
295
+ async now(project) {
296
+ return (await this.request("GET", "/v1/now", { query: { project } })).now;
297
+ }
298
+ /** What happened, as stories, newest first: what happened, how it ended, what it led to, its source. */
299
+ async episodes(opts = {}) {
300
+ return (await this.request("GET", "/v1/episodes", { query: { project: opts.project, limit: opts.limit ?? 20 } })).episodes;
301
+ }
302
+ /** Rules, how-tos and lessons, strongest first, each with when it applies and why. */
303
+ async lessons(opts = {}) {
304
+ return (await this.request("GET", "/v1/lessons", { query: { project: opts.project, limit: opts.limit ?? 50 } })).lessons;
305
+ }
306
+ /** Promises and plans, yours and those made to you, with their triggers: open (the default), done or dropped. */
307
+ async intentions(opts = {}) {
308
+ return (await this.request("GET", "/v1/intentions", { query: { status: opts.status ?? "open", limit: opts.limit ?? 50 } })).intentions;
309
+ }
310
+ /** How well the memory answers about its own work: each night it writes questions from what it read, answers them
311
+ * and marks the answers. `health` is the share right (0 to 1), with the questions; null before its first night. */
312
+ memoryHealth() {
313
+ return this.request("GET", "/v1/memory-health");
314
+ }
315
+ /** Mark a promise or plan done, dropped, or open again. */
316
+ async setIntention(id, status = "done") {
317
+ return (await this.request("POST", `/v1/intentions/${Number(id)}`, { json: { status } })).intention;
318
+ }
263
319
  /** What the memory holds and what connects to what. Every line has a memory behind it. */
264
320
  graph() {
265
321
  return this.request("GET", "/v1/graph");
@@ -274,6 +330,24 @@ export class Geniffy {
274
330
  me() {
275
331
  return this.request("GET", "/v1/me");
276
332
  }
333
+ /**
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.
337
+ */
338
+ usage() {
339
+ return this.request("GET", "/v1/usage");
340
+ }
341
+ /**
342
+ * An agent's or a chat's session, saved as it goes into one memory: hand save() the whole conversation after each
343
+ * turn, and only the messages it has not sent yet go to Geniffy.
344
+ *
345
+ * const run = mem.session("run-42", { title: "Refund agent" });
346
+ * await run.save(messages); // after every turn
347
+ */
348
+ session(id, opts = {}) {
349
+ return new Session(this, id, opts);
350
+ }
277
351
  /** Which of your users have memory, busiest first. */
278
352
  async spaces() {
279
353
  const out = await this.request("GET", "/v1/spaces");
@@ -291,6 +365,14 @@ export class Geniffy {
291
365
  function addBody(input) {
292
366
  const given = typeof input === "string" ? { text: input } : input;
293
367
  const { saidAt, externalId, ...rest } = given;
368
+ if (rest.session !== undefined) { // the API says the same: refused before anything is sent
369
+ if (!("messages" in rest))
370
+ throw new TypeError("session goes with messages: the turns of the session.");
371
+ if (externalId !== undefined)
372
+ throw new TypeError("Send session or externalId, not both: the session is the source's id.");
373
+ if (saidAt !== undefined)
374
+ throw new TypeError("saidAt does not go with session: each turn is dated as it arrives.");
375
+ }
294
376
  const body = { ...rest };
295
377
  if (saidAt !== undefined)
296
378
  body.said_at = saidAt instanceof Date ? saidAt.toISOString() : String(saidAt);
@@ -575,4 +657,61 @@ export class Files {
575
657
  return out.moved ?? 0;
576
658
  }
577
659
  }
660
+ // ── sessions saved as they go ─────────────────────────────────────────────────────────────────────────────
661
+ const SESSION_CALL = 500; // the most messages one call takes
662
+ // A message written out with its keys in order, so the same message reads the same however it was built.
663
+ function stable(value) {
664
+ if (Array.isArray(value))
665
+ return `[${value.map(stable).join(",")}]`;
666
+ if (value && typeof value === "object") {
667
+ const o = value;
668
+ return `{${Object.keys(o).sort().filter((k) => o[k] !== undefined).map((k) => `${JSON.stringify(k)}:${stable(o[k])}`).join(",")}}`;
669
+ }
670
+ return JSON.stringify(value) ?? "null";
671
+ }
672
+ /**
673
+ * An agent's or a chat's session, saved as it goes into one memory for the whole session. Hand save() the whole
674
+ * conversation after each turn: only the messages after the last one it sent go to Geniffy, at most 500 a call, so a
675
+ * session of any length is never sent twice. When that message is no longer there (the agent rewrote or compacted
676
+ * its history), what it holds now is saved again rather than lost. Made by client.session(id).
677
+ */
678
+ export class Session {
679
+ client;
680
+ #last = null;
681
+ id;
682
+ title;
683
+ labels;
684
+ constructor(client, id, opts = {}) {
685
+ this.client = client;
686
+ this.id = String(id);
687
+ this.title = opts.title;
688
+ this.labels = opts.labels;
689
+ }
690
+ /** Sends what is new; resolves to the session's source, or null when nothing was new. */
691
+ async save(messages) {
692
+ 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
+ }
702
+ const fresh = all.slice(from);
703
+ let source = null;
704
+ for (let i = 0; i < fresh.length; i += SESSION_CALL) {
705
+ const part = fresh.slice(i, i + SESSION_CALL);
706
+ const input = { messages: part, session: this.id };
707
+ if (this.title)
708
+ input.title = this.title;
709
+ if (this.labels)
710
+ input.labels = this.labels;
711
+ source = await this.client.memories.add(input);
712
+ this.#last = stable(part[part.length - 1]);
713
+ }
714
+ return source;
715
+ }
716
+ }
578
717
  export default Geniffy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geniffy",
3
- "version": "0.3.0",
3
+ "version": "0.4.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"
23
+ "test": "npm run build && node --test test/client.test.mjs test/claude.test.mjs test/briefing.test.mjs test/session-usage.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",