geniffy 0.1.1
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/LICENSE +21 -0
- package/README.md +93 -0
- package/dist/index.d.ts +342 -0
- package/dist/index.js +332 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Geniffy Innovations Private Limited
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Geniffy for TypeScript and JavaScript
|
|
2
|
+
|
|
3
|
+
[](https://github.com/Geniffy/geniffy-typescript/actions/workflows/ci.yml) [](https://www.npmjs.com/package/geniffy) [](LICENSE) [](https://docs.geniffy.com/sdks/typescript)
|
|
4
|
+
|
|
5
|
+
Give your app a memory. Write what each of your users tells you, and put what is known about them
|
|
6
|
+
in front of your model, with where every line came from. When nothing is known, it says so instead
|
|
7
|
+
of guessing. No dependencies: it uses the platform's `fetch` (Node 18+, Deno, Bun, edge runtimes).
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install geniffy
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Make a key in the Geniffy app under **API keys** and set it as `GENIFFY_API_KEY`.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Geniffy } from "geniffy";
|
|
17
|
+
|
|
18
|
+
const client = new Geniffy(); // reads GENIFFY_API_KEY
|
|
19
|
+
const mem = client.space("customer_1042"); // one of your users; nothing else can read it
|
|
20
|
+
|
|
21
|
+
const source = await mem.memories.add("Priya Nair signs the Lumen renewal, and it comes up in March.");
|
|
22
|
+
await mem.sources.wait(source.id); // learning usually takes a few seconds
|
|
23
|
+
|
|
24
|
+
const context = await mem.context("Who signs the Lumen renewal?");
|
|
25
|
+
const prompt = `${context}\n\nUser: Who signs the Lumen renewal?`;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`context()` returns the memories that bear on the question, one per line, each with where it came from.
|
|
29
|
+
Ask about something that was never stored and it returns one sentence, `There is nothing stored about
|
|
30
|
+
this yet. Say so rather than guessing.`, never an empty string: a model reads silence as permission to
|
|
31
|
+
invent.
|
|
32
|
+
|
|
33
|
+
## Three ways to recall
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
await mem.context("Who signs the renewal?"); // a block for your own prompt; the one most apps want
|
|
37
|
+
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
|
|
39
|
+
```
|
|
40
|
+
|
|
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.
|
|
43
|
+
|
|
44
|
+
## Spaces: one memory per user
|
|
45
|
+
|
|
46
|
+
A space is your own name for one of your users. Bind a client to it and every call stays inside it.
|
|
47
|
+
A space exists from the first time you write to it; there is nothing to create.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const mem = client.space(`user_${user.id}`); // per request
|
|
51
|
+
await client.memories.add("..."); // no space: your own memory, the one the Geniffy app shows
|
|
52
|
+
await client.spaces(); // which spaces hold anything, most recently written first
|
|
53
|
+
await client.forgetSpace("user_8841"); // everything held for that user, gone, when they ask
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Add
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
await mem.memories.add({ text: "Pilots run for 6 weeks.", title: "GTM plan" });
|
|
60
|
+
await mem.memories.add({ url: "https://example.com" }); // a web page, read once
|
|
61
|
+
await mem.memories.add({ messages: chatHistory }); // a conversation, as your framework holds it
|
|
62
|
+
await mem.memories.addFile(fileOrBlob, { filename: "Pricing.pdf" }); // PDF or Word (.docx)
|
|
63
|
+
await mem.memories.addMany([{ text: "..." }, { url: "https://..." }]);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
A file or page that can't be read rejects with `UnreadableError`; `error.source` is the row it left, with the reason.
|
|
67
|
+
|
|
68
|
+
## Read and correct
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
const page = await mem.memories.list({ kind: "people" }); // all, people, plan, pref or detail; newest first
|
|
72
|
+
for await (const m of mem.memories.iterate()) { /* every memory */ }
|
|
73
|
+
const { memory, history } = await mem.memories.get(42); // memory.quote is the sentence it came from
|
|
74
|
+
await mem.memories.delete(42); // forget it for good
|
|
75
|
+
await mem.profile("Priya Nair"); // what is lastingly true about someone, and what is going on now
|
|
76
|
+
await mem.brief("Priya Nair"); // what to read before talking to them
|
|
77
|
+
await mem.sources.list();
|
|
78
|
+
await mem.sources.delete(source.id); // a source, and what only it taught
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Errors, retries and request ids
|
|
82
|
+
|
|
83
|
+
Every error carries the API's own sentence: `AuthenticationError` (a wrong or revoked key),
|
|
84
|
+
`NotFoundError`, `BadRequestError`, `UnreadableError`, `RateLimitError`, `InternalServerError`,
|
|
85
|
+
`APIConnectionError`; all extend `GeniffyError`. Reads are retried twice on network errors, 408, 429 and
|
|
86
|
+
5xx; adding is retried only on 429, so a retry never saves a note twice. Pass `{ maxRetries, timeout }`
|
|
87
|
+
to the constructor to change that.
|
|
88
|
+
|
|
89
|
+
Every response carries an `X-Request-ID`, and every error carries it as `error.requestId`. Paste it into
|
|
90
|
+
**Requests** in the Geniffy app to see that exact call: what was asked, what came back, and how long it took.
|
|
91
|
+
|
|
92
|
+
A key reaches its owner's memory and every space beneath it. Keep it on your server: anyone with it can
|
|
93
|
+
read that memory. Docs: https://docs.geniffy.com
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geniffy for TypeScript and JavaScript: add notes, files and links to a memory, then search it and ask it.
|
|
3
|
+
*
|
|
4
|
+
* import { Geniffy } from "geniffy";
|
|
5
|
+
* const g = new Geniffy(); // reads GENIFFY_API_KEY
|
|
6
|
+
* await g.memories.add({ text: "Priya Nair signs the Lumen renewal, and it comes up in March." });
|
|
7
|
+
* const { answer } = await g.ask("Who signs the Lumen renewal?");
|
|
8
|
+
*
|
|
9
|
+
* No dependencies: the platform's fetch, FormData and Blob (Node 18+, Deno, Bun, edge runtimes).
|
|
10
|
+
* Retries: reads and deletes are retried on network errors, 408, 429 and 5xx; adding is retried only on
|
|
11
|
+
* 429, so a retry never saves a note twice.
|
|
12
|
+
*/
|
|
13
|
+
export declare const VERSION = "0.1.1";
|
|
14
|
+
export declare const DEFAULT_BASE_URL = "https://api.geniffy.com";
|
|
15
|
+
export type Kind = "all" | "people" | "plan" | "pref" | "detail";
|
|
16
|
+
export interface SourceRef {
|
|
17
|
+
id: string | null;
|
|
18
|
+
kind: string;
|
|
19
|
+
title: string;
|
|
20
|
+
}
|
|
21
|
+
export interface Memory {
|
|
22
|
+
id: number;
|
|
23
|
+
text: string;
|
|
24
|
+
/** people, plan, pref or detail */
|
|
25
|
+
kind: string;
|
|
26
|
+
about?: string | null;
|
|
27
|
+
/** current, or clash when two memories disagree */
|
|
28
|
+
status: string;
|
|
29
|
+
learned_at?: string | null;
|
|
30
|
+
said_at?: string | null;
|
|
31
|
+
source?: SourceRef | null;
|
|
32
|
+
/** the sentence it came from (on memories.get) */
|
|
33
|
+
quote?: string | null;
|
|
34
|
+
}
|
|
35
|
+
export interface MemoryPage {
|
|
36
|
+
memories: Memory[];
|
|
37
|
+
counts: Record<string, number>;
|
|
38
|
+
total: number;
|
|
39
|
+
next: number | null;
|
|
40
|
+
}
|
|
41
|
+
export interface MemoryDetail {
|
|
42
|
+
memory: Memory;
|
|
43
|
+
history: Memory[];
|
|
44
|
+
}
|
|
45
|
+
export interface Source {
|
|
46
|
+
id: string;
|
|
47
|
+
kind: "note" | "file" | "link";
|
|
48
|
+
title: string;
|
|
49
|
+
status: "reading" | "learned" | "failed";
|
|
50
|
+
/** why it failed, in plain words */
|
|
51
|
+
error?: string | null;
|
|
52
|
+
facts?: number | null;
|
|
53
|
+
url?: string | null;
|
|
54
|
+
file_name?: string | null;
|
|
55
|
+
file_type?: string | null;
|
|
56
|
+
size_bytes?: number | null;
|
|
57
|
+
added_by?: string | null;
|
|
58
|
+
added_at?: string | null;
|
|
59
|
+
}
|
|
60
|
+
export interface SourcePage {
|
|
61
|
+
sources: Source[];
|
|
62
|
+
total: number;
|
|
63
|
+
next: number | null;
|
|
64
|
+
}
|
|
65
|
+
export interface Answer {
|
|
66
|
+
question: string;
|
|
67
|
+
/** null when nothing you added supports an answer; `message` then says so */
|
|
68
|
+
answer: string | null;
|
|
69
|
+
message: string | null;
|
|
70
|
+
memories: Memory[];
|
|
71
|
+
clash: boolean;
|
|
72
|
+
}
|
|
73
|
+
export interface ClientOptions {
|
|
74
|
+
/** Defaults to GENIFFY_API_KEY. */
|
|
75
|
+
apiKey?: string;
|
|
76
|
+
/** Defaults to GENIFFY_BASE_URL, then https://api.geniffy.com */
|
|
77
|
+
baseURL?: string;
|
|
78
|
+
/** Milliseconds per attempt. Default 60000. */
|
|
79
|
+
timeout?: number;
|
|
80
|
+
/** Default 2. */
|
|
81
|
+
maxRetries?: number;
|
|
82
|
+
/** Your own fetch (tests, proxies). */
|
|
83
|
+
fetch?: typeof fetch;
|
|
84
|
+
/**
|
|
85
|
+
* One of YOUR users. Every call this client makes then reaches that user's memory alone, and no
|
|
86
|
+
* other space can read it. Leave it out and the client reaches your own memory, the one the
|
|
87
|
+
* Geniffy app shows you, which is what a personal script wants.
|
|
88
|
+
*
|
|
89
|
+
* You usually want `client.space(id)` instead: one client for the process, one bound client per
|
|
90
|
+
* request, so a space can never be forgotten on a call.
|
|
91
|
+
*/
|
|
92
|
+
space?: string;
|
|
93
|
+
}
|
|
94
|
+
export declare class GeniffyError extends Error {
|
|
95
|
+
readonly status?: number;
|
|
96
|
+
readonly code?: string;
|
|
97
|
+
readonly body?: Record<string, unknown>;
|
|
98
|
+
/** The id Geniffy gave the call (its X-Request-ID): log it, and paste it into Requests in the Geniffy app
|
|
99
|
+
* to see exactly what was sent and what came back. Undefined when the call never arrived. */
|
|
100
|
+
readonly requestId?: string;
|
|
101
|
+
constructor(message: string, opts?: {
|
|
102
|
+
status?: number;
|
|
103
|
+
code?: string;
|
|
104
|
+
body?: Record<string, unknown>;
|
|
105
|
+
cause?: unknown;
|
|
106
|
+
requestId?: string;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/** Geniffy could not be reached, after the retries. */
|
|
110
|
+
export declare class APIConnectionError extends GeniffyError {
|
|
111
|
+
}
|
|
112
|
+
/** The key is missing, wrong or revoked. */
|
|
113
|
+
export declare class AuthenticationError extends GeniffyError {
|
|
114
|
+
}
|
|
115
|
+
export declare class NotFoundError extends GeniffyError {
|
|
116
|
+
}
|
|
117
|
+
export declare class BadRequestError extends GeniffyError {
|
|
118
|
+
}
|
|
119
|
+
/** A file or link could not be read; `source` is the row it left, with the reason. */
|
|
120
|
+
export declare class UnreadableError extends BadRequestError {
|
|
121
|
+
get source(): Source | undefined;
|
|
122
|
+
}
|
|
123
|
+
export declare class RateLimitError extends GeniffyError {
|
|
124
|
+
}
|
|
125
|
+
export declare class InternalServerError extends GeniffyError {
|
|
126
|
+
}
|
|
127
|
+
type Body = {
|
|
128
|
+
json?: unknown;
|
|
129
|
+
form?: FormData;
|
|
130
|
+
query?: Record<string, string | number | undefined>;
|
|
131
|
+
};
|
|
132
|
+
export declare class Geniffy {
|
|
133
|
+
#private;
|
|
134
|
+
readonly memories: Memories;
|
|
135
|
+
readonly sources: Sources;
|
|
136
|
+
readonly maxRetries: number;
|
|
137
|
+
readonly timeout: number;
|
|
138
|
+
/** The one of your users this client is bound to, or "" for your own memory. */
|
|
139
|
+
readonly boundSpace: string;
|
|
140
|
+
constructor(opts?: ClientOptions);
|
|
141
|
+
/**
|
|
142
|
+
* The same client, pointed at one of YOUR users. Everything it reads and writes is that user's
|
|
143
|
+
* memory alone; nothing else can see it.
|
|
144
|
+
*
|
|
145
|
+
* const client = new Geniffy({ apiKey }); // once, for the process
|
|
146
|
+
* const mem = client.space(req.user.id); // per request
|
|
147
|
+
* await mem.memories.add("Prefers WhatsApp.");
|
|
148
|
+
* const answer = await mem.ask("How should we reach them?");
|
|
149
|
+
*
|
|
150
|
+
* `space` is your own name for that user and nothing is read into it: an id, an email, whatever
|
|
151
|
+
* you already call them. Up to 128 letters, digits, dots, dashes or underscores.
|
|
152
|
+
*/
|
|
153
|
+
space(space: string): Geniffy;
|
|
154
|
+
/** @internal */
|
|
155
|
+
request<T>(method: "GET" | "POST" | "DELETE" | "PATCH", path: string, body?: Body): Promise<T>;
|
|
156
|
+
/** An answer from your memory only. `answer` is null when nothing you added supports one. */
|
|
157
|
+
ask(question: string): Promise<Answer>;
|
|
158
|
+
/** The memories that best match `q`, best first. */
|
|
159
|
+
search(q: string, opts?: {
|
|
160
|
+
limit?: number;
|
|
161
|
+
kind?: Kind;
|
|
162
|
+
}): Promise<Memory[]>;
|
|
163
|
+
/**
|
|
164
|
+
* The memories that bear on a question, already written out for YOUR prompt.
|
|
165
|
+
*
|
|
166
|
+
* const prompt = `${await mem.context(question)}
|
|
167
|
+
|
|
168
|
+
User: ${question}`;
|
|
169
|
+
*
|
|
170
|
+
* This is the ten lines of formatting every integration writes after calling search, so it is
|
|
171
|
+
* written here once. It is never empty: when nothing is held it says so in words, because an
|
|
172
|
+
* empty block reads to a model as permission to invent.
|
|
173
|
+
*/
|
|
174
|
+
context(question: string, opts?: {
|
|
175
|
+
limit?: number;
|
|
176
|
+
kind?: Kind;
|
|
177
|
+
withSources?: boolean;
|
|
178
|
+
}): Promise<string>;
|
|
179
|
+
/** The same, with the memories behind it and whether anything was found. */
|
|
180
|
+
contextFull(question: string, opts?: {
|
|
181
|
+
limit?: number;
|
|
182
|
+
kind?: Kind;
|
|
183
|
+
withSources?: boolean;
|
|
184
|
+
}): Promise<ContextResult>;
|
|
185
|
+
/** What stays true about someone, and what is going on with them now. */
|
|
186
|
+
profile(subject?: string): Promise<Profile>;
|
|
187
|
+
/** What to read before dealing with someone. */
|
|
188
|
+
brief(subject?: string, opts?: {
|
|
189
|
+
limit?: number;
|
|
190
|
+
}): Promise<Brief>;
|
|
191
|
+
/** What the memory holds and what connects to what. Every line has a memory behind it. */
|
|
192
|
+
graph(): Promise<Graph>;
|
|
193
|
+
/** Whose key this is, and which space this client is reading. */
|
|
194
|
+
me(): Promise<{
|
|
195
|
+
name: string | null;
|
|
196
|
+
memory: string;
|
|
197
|
+
space: string | null;
|
|
198
|
+
}>;
|
|
199
|
+
/** Which of your users have memory, busiest first. */
|
|
200
|
+
spaces(): Promise<SpaceRow[]>;
|
|
201
|
+
/**
|
|
202
|
+
* Everything one of your users ever said, gone: facts, sources, all of it. This is the call to
|
|
203
|
+
* make when they ask to be forgotten. It cannot be undone.
|
|
204
|
+
*/
|
|
205
|
+
forgetSpace(space: string): Promise<void>;
|
|
206
|
+
}
|
|
207
|
+
export interface BatchResult {
|
|
208
|
+
results: Array<{
|
|
209
|
+
index: number;
|
|
210
|
+
source: Source | null;
|
|
211
|
+
error: {
|
|
212
|
+
code: string;
|
|
213
|
+
message: string;
|
|
214
|
+
} | null;
|
|
215
|
+
}>;
|
|
216
|
+
added: number;
|
|
217
|
+
failed: number;
|
|
218
|
+
}
|
|
219
|
+
export interface ContextResult {
|
|
220
|
+
question: string;
|
|
221
|
+
/** Put this straight into your prompt. Never empty. */
|
|
222
|
+
context: string;
|
|
223
|
+
memories: Memory[];
|
|
224
|
+
used: number;
|
|
225
|
+
/** true when nothing stored bears on the question. */
|
|
226
|
+
empty: boolean;
|
|
227
|
+
}
|
|
228
|
+
export interface Profile {
|
|
229
|
+
subject: string | null;
|
|
230
|
+
lasting: unknown[];
|
|
231
|
+
current: unknown[];
|
|
232
|
+
grouped: Record<string, unknown>;
|
|
233
|
+
memories: number;
|
|
234
|
+
}
|
|
235
|
+
export interface Brief {
|
|
236
|
+
subject: string | null;
|
|
237
|
+
summary: string | null;
|
|
238
|
+
memories: Memory[];
|
|
239
|
+
total: number;
|
|
240
|
+
}
|
|
241
|
+
export interface Graph {
|
|
242
|
+
nodes: Array<{
|
|
243
|
+
id: string;
|
|
244
|
+
label: string;
|
|
245
|
+
type: string;
|
|
246
|
+
memories: number;
|
|
247
|
+
}>;
|
|
248
|
+
edges: Array<{
|
|
249
|
+
from: string;
|
|
250
|
+
to: string;
|
|
251
|
+
label: string;
|
|
252
|
+
memory_ids: number[];
|
|
253
|
+
}>;
|
|
254
|
+
}
|
|
255
|
+
export interface SpaceRow {
|
|
256
|
+
/** Your own name for that user. */
|
|
257
|
+
space: string;
|
|
258
|
+
/** Notes, files and links added for them. */
|
|
259
|
+
sources: number;
|
|
260
|
+
/** Facts learned from those. */
|
|
261
|
+
memories: number;
|
|
262
|
+
last_added_at: string | null;
|
|
263
|
+
}
|
|
264
|
+
export interface ChatMessage {
|
|
265
|
+
role: string;
|
|
266
|
+
/** The words: a string, or the blocks or parts your framework holds (Anthropic, OpenAI). Only text is
|
|
267
|
+
* kept; null is a turn that said nothing in words, such as one that only called a tool. */
|
|
268
|
+
content?: string | unknown[] | null;
|
|
269
|
+
/** Or the parts, as the Vercel AI SDK and Gemini hold a message. */
|
|
270
|
+
parts?: unknown[];
|
|
271
|
+
/** Who, when the role does not say: a tool name, a participant. */
|
|
272
|
+
name?: string;
|
|
273
|
+
}
|
|
274
|
+
export type AddInput = string | {
|
|
275
|
+
text: string;
|
|
276
|
+
title?: string;
|
|
277
|
+
} | {
|
|
278
|
+
url: string;
|
|
279
|
+
title?: string;
|
|
280
|
+
}
|
|
281
|
+
/** A conversation as your framework already holds it. System and developer messages are skipped, and
|
|
282
|
+
* who said what is kept, so the user's words become facts about the user and not about your assistant. */
|
|
283
|
+
| {
|
|
284
|
+
messages: ChatMessage[];
|
|
285
|
+
title?: string;
|
|
286
|
+
};
|
|
287
|
+
export declare class Memories {
|
|
288
|
+
private readonly client;
|
|
289
|
+
constructor(client: Geniffy);
|
|
290
|
+
/** Add a note (a string or { text }), or a web page ({ url }) that Geniffy reads once. */
|
|
291
|
+
add(input: AddInput): Promise<Source>;
|
|
292
|
+
/** Add a PDF or Word (.docx) file. */
|
|
293
|
+
addFile(file: Blob | ArrayBuffer | Uint8Array, opts?: {
|
|
294
|
+
filename?: string;
|
|
295
|
+
title?: string;
|
|
296
|
+
}): Promise<Source>;
|
|
297
|
+
/**
|
|
298
|
+
* Up to a hundred at once. One bad item does not take the rest down: every item comes back in
|
|
299
|
+
* the order you sent it, with either its source or why it was refused.
|
|
300
|
+
*/
|
|
301
|
+
addMany(items: AddInput[]): Promise<BatchResult>;
|
|
302
|
+
/** Say what we got wrong and what is right. The wrong one is marked and never used again; what
|
|
303
|
+
* is right is learned as a new memory with its own source. Nothing is quietly overwritten. */
|
|
304
|
+
correct(id: number, text: string): Promise<{
|
|
305
|
+
id: number;
|
|
306
|
+
corrected: boolean;
|
|
307
|
+
source: Source;
|
|
308
|
+
}>;
|
|
309
|
+
/** One page of memories, newest first. */
|
|
310
|
+
list(opts?: {
|
|
311
|
+
kind?: Kind;
|
|
312
|
+
limit?: number;
|
|
313
|
+
cursor?: number;
|
|
314
|
+
}): Promise<MemoryPage>;
|
|
315
|
+
/** Every memory, newest first, a page at a time: for await (const m of g.memories.iterate()) */
|
|
316
|
+
iterate(opts?: {
|
|
317
|
+
kind?: Kind;
|
|
318
|
+
pageSize?: number;
|
|
319
|
+
}): AsyncGenerator<Memory>;
|
|
320
|
+
/** A memory, the sentence it came from, and the values it held before. */
|
|
321
|
+
get(id: number): Promise<MemoryDetail>;
|
|
322
|
+
/** Forget a memory for good. */
|
|
323
|
+
delete(id: number): Promise<void>;
|
|
324
|
+
}
|
|
325
|
+
export declare class Sources {
|
|
326
|
+
private readonly client;
|
|
327
|
+
constructor(client: Geniffy);
|
|
328
|
+
list(opts?: {
|
|
329
|
+
limit?: number;
|
|
330
|
+
cursor?: number;
|
|
331
|
+
}): Promise<SourcePage>;
|
|
332
|
+
get(id: string): Promise<Source>;
|
|
333
|
+
/** Delete a source and every memory learned only from it. */
|
|
334
|
+
delete(id: string): Promise<void>;
|
|
335
|
+
/** Wait until Geniffy has learned from a source (or could not), then return it. Geniffy holds the
|
|
336
|
+
* call open until the source is done, so this returns the moment it is, usually in one call. */
|
|
337
|
+
wait(id: string, opts?: {
|
|
338
|
+
timeoutMs?: number;
|
|
339
|
+
intervalMs?: number;
|
|
340
|
+
}): Promise<Source>;
|
|
341
|
+
}
|
|
342
|
+
export default Geniffy;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geniffy for TypeScript and JavaScript: add notes, files and links to a memory, then search it and ask it.
|
|
3
|
+
*
|
|
4
|
+
* import { Geniffy } from "geniffy";
|
|
5
|
+
* const g = new Geniffy(); // reads GENIFFY_API_KEY
|
|
6
|
+
* await g.memories.add({ text: "Priya Nair signs the Lumen renewal, and it comes up in March." });
|
|
7
|
+
* const { answer } = await g.ask("Who signs the Lumen renewal?");
|
|
8
|
+
*
|
|
9
|
+
* No dependencies: the platform's fetch, FormData and Blob (Node 18+, Deno, Bun, edge runtimes).
|
|
10
|
+
* Retries: reads and deletes are retried on network errors, 408, 429 and 5xx; adding is retried only on
|
|
11
|
+
* 429, so a retry never saves a note twice.
|
|
12
|
+
*/
|
|
13
|
+
export const VERSION = "0.1.1";
|
|
14
|
+
export const DEFAULT_BASE_URL = "https://api.geniffy.com";
|
|
15
|
+
// ── errors ────────────────────────────────────────────────────────────────────
|
|
16
|
+
export class GeniffyError extends Error {
|
|
17
|
+
status;
|
|
18
|
+
code;
|
|
19
|
+
body;
|
|
20
|
+
/** The id Geniffy gave the call (its X-Request-ID): log it, and paste it into Requests in the Geniffy app
|
|
21
|
+
* to see exactly what was sent and what came back. Undefined when the call never arrived. */
|
|
22
|
+
requestId;
|
|
23
|
+
constructor(message, opts = {}) {
|
|
24
|
+
super(message, opts.cause === undefined ? undefined : { cause: opts.cause });
|
|
25
|
+
this.name = new.target.name;
|
|
26
|
+
this.status = opts.status;
|
|
27
|
+
this.code = opts.code;
|
|
28
|
+
this.body = opts.body;
|
|
29
|
+
this.requestId = opts.requestId;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Geniffy could not be reached, after the retries. */
|
|
33
|
+
export class APIConnectionError extends GeniffyError {
|
|
34
|
+
}
|
|
35
|
+
/** The key is missing, wrong or revoked. */
|
|
36
|
+
export class AuthenticationError extends GeniffyError {
|
|
37
|
+
}
|
|
38
|
+
export class NotFoundError extends GeniffyError {
|
|
39
|
+
}
|
|
40
|
+
export class BadRequestError extends GeniffyError {
|
|
41
|
+
}
|
|
42
|
+
/** A file or link could not be read; `source` is the row it left, with the reason. */
|
|
43
|
+
export class UnreadableError extends BadRequestError {
|
|
44
|
+
get source() {
|
|
45
|
+
const err = (this.body?.error ?? {});
|
|
46
|
+
return err.source;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export class RateLimitError extends GeniffyError {
|
|
50
|
+
}
|
|
51
|
+
export class InternalServerError extends GeniffyError {
|
|
52
|
+
}
|
|
53
|
+
function errorFrom(status, body, requestId) {
|
|
54
|
+
const err = (typeof body.error === "object" && body.error !== null ? body.error : {});
|
|
55
|
+
const message = err.message ?? (typeof body.detail === "string" ? body.detail : `Geniffy answered ${status}.`);
|
|
56
|
+
const opts = { status, code: err.code, body, requestId };
|
|
57
|
+
if (status === 401 || status === 403)
|
|
58
|
+
return new AuthenticationError(message, opts);
|
|
59
|
+
if (status === 404)
|
|
60
|
+
return new NotFoundError(message, opts);
|
|
61
|
+
if (status === 422 && err.code === "unreadable")
|
|
62
|
+
return new UnreadableError(message, opts);
|
|
63
|
+
if ([400, 409, 413, 422].includes(status))
|
|
64
|
+
return new BadRequestError(message, opts);
|
|
65
|
+
if (status === 429)
|
|
66
|
+
return new RateLimitError(message, opts);
|
|
67
|
+
if (status >= 500)
|
|
68
|
+
return new InternalServerError(message, opts);
|
|
69
|
+
return new GeniffyError(message, opts);
|
|
70
|
+
}
|
|
71
|
+
// ── the client ────────────────────────────────────────────────────────────────
|
|
72
|
+
const RETRY_STATUS = new Set([408, 429, 500, 502, 503, 504]);
|
|
73
|
+
function env(name) {
|
|
74
|
+
const g = globalThis;
|
|
75
|
+
return g.process?.env?.[name];
|
|
76
|
+
}
|
|
77
|
+
function delay(attempt, response) {
|
|
78
|
+
const after = Number(response?.headers.get("retry-after"));
|
|
79
|
+
if (response && Number.isFinite(after) && after >= 0 && after <= 60)
|
|
80
|
+
return after * 1000;
|
|
81
|
+
return Math.min(8000, 500 * 2 ** attempt) * (0.75 + Math.random() / 2);
|
|
82
|
+
}
|
|
83
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
84
|
+
export class Geniffy {
|
|
85
|
+
memories;
|
|
86
|
+
sources;
|
|
87
|
+
maxRetries;
|
|
88
|
+
timeout;
|
|
89
|
+
/** The one of your users this client is bound to, or "" for your own memory. */
|
|
90
|
+
boundSpace;
|
|
91
|
+
#opts;
|
|
92
|
+
#key;
|
|
93
|
+
#base;
|
|
94
|
+
#fetch;
|
|
95
|
+
constructor(opts = {}) {
|
|
96
|
+
const key = (opts.apiKey ?? env("GENIFFY_API_KEY") ?? "").trim();
|
|
97
|
+
if (!key) {
|
|
98
|
+
throw new GeniffyError("No API key. Pass { apiKey } or set GENIFFY_API_KEY. Make one in the Geniffy app under Connect, API keys.");
|
|
99
|
+
}
|
|
100
|
+
this.#opts = opts;
|
|
101
|
+
this.#key = key;
|
|
102
|
+
this.#base = (opts.baseURL ?? env("GENIFFY_BASE_URL") ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
103
|
+
this.#fetch = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
104
|
+
this.timeout = opts.timeout ?? 60_000;
|
|
105
|
+
this.maxRetries = Math.max(0, opts.maxRetries ?? 2);
|
|
106
|
+
this.boundSpace = (opts.space ?? "").trim();
|
|
107
|
+
this.memories = new Memories(this);
|
|
108
|
+
this.sources = new Sources(this);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The same client, pointed at one of YOUR users. Everything it reads and writes is that user's
|
|
112
|
+
* memory alone; nothing else can see it.
|
|
113
|
+
*
|
|
114
|
+
* const client = new Geniffy({ apiKey }); // once, for the process
|
|
115
|
+
* const mem = client.space(req.user.id); // per request
|
|
116
|
+
* await mem.memories.add("Prefers WhatsApp.");
|
|
117
|
+
* const answer = await mem.ask("How should we reach them?");
|
|
118
|
+
*
|
|
119
|
+
* `space` is your own name for that user and nothing is read into it: an id, an email, whatever
|
|
120
|
+
* you already call them. Up to 128 letters, digits, dots, dashes or underscores.
|
|
121
|
+
*/
|
|
122
|
+
space(space) {
|
|
123
|
+
return new Geniffy({ ...this.#opts, apiKey: this.#key, space });
|
|
124
|
+
}
|
|
125
|
+
/** @internal */
|
|
126
|
+
async request(method, path, body = {}) {
|
|
127
|
+
const url = new URL(this.#base + path);
|
|
128
|
+
for (const [k, v] of Object.entries(body.query ?? {}))
|
|
129
|
+
if (v !== undefined)
|
|
130
|
+
url.searchParams.set(k, String(v));
|
|
131
|
+
const headers = {
|
|
132
|
+
Authorization: `Bearer ${this.#key}`,
|
|
133
|
+
Accept: "application/json",
|
|
134
|
+
"X-Geniffy-Client": `geniffy-js/${VERSION}`,
|
|
135
|
+
};
|
|
136
|
+
// One header carries the space, so every call a bound client makes is scoped without any
|
|
137
|
+
// method having to take it, and a space can never be dropped by forgetting an argument.
|
|
138
|
+
if (this.boundSpace)
|
|
139
|
+
headers["X-Geniffy-Space"] = this.boundSpace;
|
|
140
|
+
if (body.json !== undefined)
|
|
141
|
+
headers["Content-Type"] = "application/json";
|
|
142
|
+
const payload = body.form ?? (body.json !== undefined ? JSON.stringify(body.json) : undefined);
|
|
143
|
+
for (let attempt = 0;; attempt++) {
|
|
144
|
+
const last = attempt >= this.maxRetries;
|
|
145
|
+
const ctrl = new AbortController();
|
|
146
|
+
const timer = setTimeout(() => ctrl.abort(), this.timeout);
|
|
147
|
+
let response;
|
|
148
|
+
try {
|
|
149
|
+
response = await this.#fetch(url, { method, headers, body: payload, signal: ctrl.signal });
|
|
150
|
+
}
|
|
151
|
+
catch (cause) {
|
|
152
|
+
clearTimeout(timer);
|
|
153
|
+
// a POST that failed in flight may have arrived: never sent twice
|
|
154
|
+
if (!last && method !== "POST" && method !== "PATCH") {
|
|
155
|
+
await sleep(delay(attempt));
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
throw new APIConnectionError(`Couldn't reach Geniffy: ${cause?.name ?? "network error"}.`, { cause });
|
|
159
|
+
}
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
const retry = response.status === 429 || (method !== "POST" && method !== "PATCH" && RETRY_STATUS.has(response.status));
|
|
162
|
+
if (!last && retry) {
|
|
163
|
+
await sleep(delay(attempt, response));
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const text = await response.text();
|
|
167
|
+
let data = {};
|
|
168
|
+
if (text) {
|
|
169
|
+
try {
|
|
170
|
+
data = JSON.parse(text);
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
data = { error: { code: "bad_response", message: text.slice(0, 300) } };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!response.ok)
|
|
177
|
+
throw errorFrom(response.status, data, response.headers.get("x-request-id") ?? undefined);
|
|
178
|
+
return data;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/** An answer from your memory only. `answer` is null when nothing you added supports one. */
|
|
182
|
+
ask(question) {
|
|
183
|
+
return this.request("POST", "/v1/ask", { json: { question } });
|
|
184
|
+
}
|
|
185
|
+
/** The memories that best match `q`, best first. */
|
|
186
|
+
async search(q, opts = {}) {
|
|
187
|
+
const out = await this.request("POST", "/v1/search", { json: { q, limit: opts.limit ?? 10, kind: opts.kind } });
|
|
188
|
+
return out.memories;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The memories that bear on a question, already written out for YOUR prompt.
|
|
192
|
+
*
|
|
193
|
+
* const prompt = `${await mem.context(question)}
|
|
194
|
+
|
|
195
|
+
User: ${question}`;
|
|
196
|
+
*
|
|
197
|
+
* This is the ten lines of formatting every integration writes after calling search, so it is
|
|
198
|
+
* written here once. It is never empty: when nothing is held it says so in words, because an
|
|
199
|
+
* empty block reads to a model as permission to invent.
|
|
200
|
+
*/
|
|
201
|
+
async context(question, opts = {}) {
|
|
202
|
+
const out = await this.contextFull(question, opts);
|
|
203
|
+
return out.context;
|
|
204
|
+
}
|
|
205
|
+
/** The same, with the memories behind it and whether anything was found. */
|
|
206
|
+
contextFull(question, opts = {}) {
|
|
207
|
+
return this.request("POST", "/v1/context", {
|
|
208
|
+
json: { question, limit: opts.limit ?? 12, kind: opts.kind, with_sources: opts.withSources ?? true },
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/** What stays true about someone, and what is going on with them now. */
|
|
212
|
+
profile(subject) {
|
|
213
|
+
return this.request("GET", "/v1/profile", { query: { subject } });
|
|
214
|
+
}
|
|
215
|
+
/** What to read before dealing with someone. */
|
|
216
|
+
brief(subject, opts = {}) {
|
|
217
|
+
return this.request("GET", "/v1/brief", { query: { subject, limit: opts.limit } });
|
|
218
|
+
}
|
|
219
|
+
/** What the memory holds and what connects to what. Every line has a memory behind it. */
|
|
220
|
+
graph() {
|
|
221
|
+
return this.request("GET", "/v1/graph");
|
|
222
|
+
}
|
|
223
|
+
/** Whose key this is, and which space this client is reading. */
|
|
224
|
+
me() {
|
|
225
|
+
return this.request("GET", "/v1/me");
|
|
226
|
+
}
|
|
227
|
+
/** Which of your users have memory, busiest first. */
|
|
228
|
+
async spaces() {
|
|
229
|
+
const out = await this.request("GET", "/v1/spaces");
|
|
230
|
+
return out.spaces;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Everything one of your users ever said, gone: facts, sources, all of it. This is the call to
|
|
234
|
+
* make when they ask to be forgotten. It cannot be undone.
|
|
235
|
+
*/
|
|
236
|
+
async forgetSpace(space) {
|
|
237
|
+
await this.request("DELETE", `/v1/spaces/${encodeURIComponent(space)}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
export class Memories {
|
|
241
|
+
client;
|
|
242
|
+
constructor(client) {
|
|
243
|
+
this.client = client;
|
|
244
|
+
}
|
|
245
|
+
/** Add a note (a string or { text }), or a web page ({ url }) that Geniffy reads once. */
|
|
246
|
+
async add(input) {
|
|
247
|
+
const json = typeof input === "string" ? { text: input } : input;
|
|
248
|
+
const out = await this.client.request("POST", "/v1/memories", { json });
|
|
249
|
+
return out.source;
|
|
250
|
+
}
|
|
251
|
+
/** Add a PDF or Word (.docx) file. */
|
|
252
|
+
async addFile(file, opts = {}) {
|
|
253
|
+
const blob = file instanceof Blob ? file : new Blob([file]);
|
|
254
|
+
const named = file.name;
|
|
255
|
+
const name = opts.filename ?? (typeof named === "string" && named ? named : "file");
|
|
256
|
+
const form = new FormData();
|
|
257
|
+
form.append("file", blob, name);
|
|
258
|
+
if (opts.title)
|
|
259
|
+
form.append("title", opts.title);
|
|
260
|
+
const out = await this.client.request("POST", "/v1/memories/file", { form });
|
|
261
|
+
return out.source;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Up to a hundred at once. One bad item does not take the rest down: every item comes back in
|
|
265
|
+
* the order you sent it, with either its source or why it was refused.
|
|
266
|
+
*/
|
|
267
|
+
async addMany(items) {
|
|
268
|
+
const json = { items: items.map((i) => (typeof i === "string" ? { text: i } : i)) };
|
|
269
|
+
return this.client.request("POST", "/v1/memories/batch", { json });
|
|
270
|
+
}
|
|
271
|
+
/** Say what we got wrong and what is right. The wrong one is marked and never used again; what
|
|
272
|
+
* is right is learned as a new memory with its own source. Nothing is quietly overwritten. */
|
|
273
|
+
async correct(id, text) {
|
|
274
|
+
return this.client.request("PATCH", `/v1/memories/${id}`, { json: { text } });
|
|
275
|
+
}
|
|
276
|
+
/** One page of memories, newest first. */
|
|
277
|
+
list(opts = {}) {
|
|
278
|
+
return this.client.request("GET", "/v1/memories", {
|
|
279
|
+
query: { kind: opts.kind, limit: opts.limit ?? 50, cursor: opts.cursor ?? 0 },
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
/** Every memory, newest first, a page at a time: for await (const m of g.memories.iterate()) */
|
|
283
|
+
async *iterate(opts = {}) {
|
|
284
|
+
let cursor = 0;
|
|
285
|
+
while (cursor !== null) {
|
|
286
|
+
const page = await this.list({ kind: opts.kind, limit: opts.pageSize ?? 100, cursor });
|
|
287
|
+
yield* page.memories;
|
|
288
|
+
cursor = page.next;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** A memory, the sentence it came from, and the values it held before. */
|
|
292
|
+
get(id) {
|
|
293
|
+
return this.client.request("GET", `/v1/memories/${Math.trunc(id)}`);
|
|
294
|
+
}
|
|
295
|
+
/** Forget a memory for good. */
|
|
296
|
+
async delete(id) {
|
|
297
|
+
await this.client.request("DELETE", `/v1/memories/${Math.trunc(id)}`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
export class Sources {
|
|
301
|
+
client;
|
|
302
|
+
constructor(client) {
|
|
303
|
+
this.client = client;
|
|
304
|
+
}
|
|
305
|
+
list(opts = {}) {
|
|
306
|
+
return this.client.request("GET", "/v1/sources", { query: { limit: opts.limit ?? 100, cursor: opts.cursor ?? 0 } });
|
|
307
|
+
}
|
|
308
|
+
async get(id) {
|
|
309
|
+
return (await this.client.request("GET", `/v1/sources/${encodeURIComponent(id)}`)).source;
|
|
310
|
+
}
|
|
311
|
+
/** Delete a source and every memory learned only from it. */
|
|
312
|
+
async delete(id) {
|
|
313
|
+
await this.client.request("DELETE", `/v1/sources/${encodeURIComponent(id)}`);
|
|
314
|
+
}
|
|
315
|
+
/** Wait until Geniffy has learned from a source (or could not), then return it. Geniffy holds the
|
|
316
|
+
* call open until the source is done, so this returns the moment it is, usually in one call. */
|
|
317
|
+
async wait(id, opts = {}) {
|
|
318
|
+
const deadline = Date.now() + (opts.timeoutMs ?? 120_000);
|
|
319
|
+
for (;;) {
|
|
320
|
+
const asked = Date.now();
|
|
321
|
+
// held up to 30 s, never past the deadline, and never so long that this client's own timeout fires first
|
|
322
|
+
const hold = Math.max(0, Math.min(deadline - asked, this.client.timeout - 5_000, 30_000));
|
|
323
|
+
const { source } = await this.client.request("GET", `/v1/sources/${encodeURIComponent(id)}`, { query: { wait: (hold / 1000).toFixed(1) } });
|
|
324
|
+
const now = Date.now();
|
|
325
|
+
if (source.status !== "reading" || now >= deadline)
|
|
326
|
+
return source;
|
|
327
|
+
if (now - asked < 1_000)
|
|
328
|
+
await sleep(Math.min(opts.intervalMs ?? 2000, deadline - now)); // answered at once: don't spin
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
export default Geniffy;
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "geniffy",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Geniffy for TypeScript and JavaScript: add notes, files and links to a memory, then search it and ask it.",
|
|
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": ["dist", "README.md"],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"engines": { "node": ">=18" },
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"test": "npm run build && node --test test/client.test.mjs"
|
|
20
|
+
},
|
|
21
|
+
"keywords": ["memory", "ai", "agents", "llm", "rag", "geniffy"],
|
|
22
|
+
"homepage": "https://docs.geniffy.com/sdks/typescript",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/Geniffy/geniffy-typescript.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/Geniffy/geniffy-typescript/issues"
|
|
29
|
+
},
|
|
30
|
+
"author": "Geniffy <ops@geniffy.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"typescript": "^5.6.0"
|
|
34
|
+
}
|
|
35
|
+
}
|