@memoy-dev/sdk 0.0.0-stage → 0.0.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.
@@ -0,0 +1,99 @@
1
+ export declare const DEFAULT_BASE_URL = "https://memoy.dev";
2
+ export type Message = {
3
+ role: "user" | "agent";
4
+ content: string;
5
+ };
6
+ export type Fetch = (url: string, init: RequestInit) => Promise<Response>;
7
+ export type MemoyOptions = {
8
+ apiKey?: string;
9
+ baseUrl?: string;
10
+ fetch?: Fetch;
11
+ };
12
+ type RunTarget = {
13
+ agent: string;
14
+ model?: string;
15
+ key?: string;
16
+ };
17
+ export type RunInput = RunTarget & ({
18
+ messages: Message[];
19
+ input?: never;
20
+ } | {
21
+ input: string;
22
+ messages?: never;
23
+ });
24
+ export type RunReply = {
25
+ content: string;
26
+ images: string[];
27
+ };
28
+ export type Agent = {
29
+ slug: string;
30
+ name: string;
31
+ model: string;
32
+ instructions: string;
33
+ permissions: string[];
34
+ public: boolean;
35
+ createdAt: string;
36
+ updatedAt: string;
37
+ };
38
+ export type MemorySearch = {
39
+ agent?: string;
40
+ key?: string;
41
+ scope?: "agent" | "project";
42
+ query?: string;
43
+ };
44
+ export type Memory = {
45
+ id: string;
46
+ content: string;
47
+ key: string;
48
+ scope: "agent" | "project";
49
+ agent: string;
50
+ createdAt: string;
51
+ };
52
+ export type MessageSource = "api" | "playground" | "manual";
53
+ export type MessageList = {
54
+ agent?: string;
55
+ key?: string;
56
+ source?: MessageSource;
57
+ page?: number;
58
+ limit?: number;
59
+ };
60
+ export type StoredMessage = {
61
+ id: string;
62
+ agent: string;
63
+ key: string | null;
64
+ source: MessageSource;
65
+ role: "user" | "agent";
66
+ content: string;
67
+ createdAt: string;
68
+ };
69
+ export type MessagePage = {
70
+ messages: StoredMessage[];
71
+ total: number;
72
+ };
73
+ export type MessageRecord = {
74
+ agent: string;
75
+ key?: string;
76
+ messages: Message[];
77
+ };
78
+ export declare class MemoyError extends Error {
79
+ readonly status: number;
80
+ readonly body: unknown;
81
+ constructor(status: number, message: string, body: unknown);
82
+ }
83
+ export declare class Memoy {
84
+ #private;
85
+ readonly baseUrl: string;
86
+ constructor(options?: MemoyOptions);
87
+ readonly agents: {
88
+ list: () => Promise<Agent[]>;
89
+ };
90
+ readonly memories: {
91
+ search: (filter?: MemorySearch) => Promise<Memory[]>;
92
+ };
93
+ readonly messages: {
94
+ list: (filter?: MessageList) => Promise<MessagePage>;
95
+ record: (input: MessageRecord) => Promise<StoredMessage[]>;
96
+ };
97
+ run(input: RunInput): Promise<RunReply>;
98
+ }
99
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,132 @@
1
+ // index.ts
2
+ const DEFAULT_BASE_URL = "https://memoy.dev";
3
+
4
+ class MemoyError extends Error {
5
+ status;
6
+ body;
7
+ constructor(status, message, body) {
8
+ super(message);
9
+ this.name = "MemoyError";
10
+ this.status = status;
11
+ this.body = body;
12
+ }
13
+ }
14
+ const envApiKey = () => typeof process === "undefined" ? undefined : process.env?.MEMOY_API_KEY;
15
+ const readJson = async (response) => {
16
+ const text = await response.text();
17
+ try {
18
+ return JSON.parse(text);
19
+ } catch {
20
+ return text;
21
+ }
22
+ };
23
+ const errorMessage = (body, status) => {
24
+ const error = body?.error;
25
+ return typeof error === "string" ? error : `Request failed with ${status}.`;
26
+ };
27
+
28
+ class Memoy {
29
+ baseUrl;
30
+ #apiKey;
31
+ #fetch;
32
+ constructor(options = {}) {
33
+ const apiKey = options.apiKey ?? envApiKey();
34
+ if (!apiKey)
35
+ throw new Error("memoy: pass an apiKey or set MEMOY_API_KEY.");
36
+ this.#apiKey = apiKey;
37
+ this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
38
+ this.#fetch = options.fetch ?? ((url, init) => fetch(url, init));
39
+ }
40
+ agents = {
41
+ list: async () => {
42
+ const body = await this.#request("/api/v1/agents");
43
+ return (body.agents ?? []).map(({ created_at, updated_at, ...agent }) => ({
44
+ ...agent,
45
+ createdAt: created_at,
46
+ updatedAt: updated_at
47
+ }));
48
+ }
49
+ };
50
+ memories = {
51
+ search: async (filter = {}) => {
52
+ const params = new URLSearchParams;
53
+ for (const [name, value] of Object.entries(filter))
54
+ if (value)
55
+ params.set(name, value);
56
+ const body = await this.#request(`/api/v1/memories?${params}`);
57
+ return (body.memories ?? []).map(({ created_at, ...memory }) => ({
58
+ ...memory,
59
+ createdAt: created_at
60
+ }));
61
+ }
62
+ };
63
+ messages = {
64
+ list: async (filter = {}) => {
65
+ const params = new URLSearchParams;
66
+ for (const [name, value] of Object.entries(filter))
67
+ if (value !== undefined && value !== "")
68
+ params.set(name, String(value));
69
+ const { body, headers } = await this.#send(`/api/v1/messages?${params}`);
70
+ const messages = body.messages ?? [];
71
+ return {
72
+ messages: messages.map(({ created_at, ...message }) => ({
73
+ ...message,
74
+ createdAt: created_at
75
+ })),
76
+ total: Number(headers.get("x-total-count") ?? messages.length)
77
+ };
78
+ },
79
+ record: async (input) => {
80
+ const body = await this.#request("/api/v1/messages", {
81
+ method: "POST",
82
+ body: JSON.stringify({
83
+ agent: input.agent,
84
+ key: input.key,
85
+ messages: input.messages
86
+ })
87
+ });
88
+ return (body.messages ?? []).map(({ created_at, ...message }) => ({
89
+ ...message,
90
+ createdAt: created_at
91
+ }));
92
+ }
93
+ };
94
+ async#request(path, init = {}) {
95
+ const { body } = await this.#send(path, init);
96
+ return body;
97
+ }
98
+ async#send(path, init = {}) {
99
+ const response = await this.#fetch(`${this.baseUrl}${path}`, {
100
+ ...init,
101
+ headers: {
102
+ authorization: `Bearer ${this.#apiKey}`,
103
+ "content-type": "application/json"
104
+ }
105
+ });
106
+ const body = await readJson(response);
107
+ if (!response.ok)
108
+ throw new MemoyError(response.status, errorMessage(body, response.status), body);
109
+ return { body, headers: response.headers };
110
+ }
111
+ async run(input) {
112
+ const messages = input.messages ?? [
113
+ { role: "user", content: input.input }
114
+ ];
115
+ const body = await this.#request("/api/v1/run", {
116
+ method: "POST",
117
+ body: JSON.stringify({
118
+ agent: input.agent,
119
+ model: input.model,
120
+ messages,
121
+ key: input.key
122
+ })
123
+ });
124
+ const reply = body;
125
+ return { content: reply.content ?? "", images: reply.images ?? [] };
126
+ }
127
+ }
128
+ export {
129
+ DEFAULT_BASE_URL,
130
+ Memoy,
131
+ MemoyError
132
+ };
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "@memoy-dev/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "Run your memoy agents from any JavaScript runtime.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "files": [
19
+ "dist/index.js",
20
+ "dist/index.d.ts"
21
+ ],
22
+ "scripts": {
23
+ "build": "rm -rf dist && bun build ./index.ts --outdir dist --target browser --dts",
24
+ "typecheck": "bun check",
25
+ "prepublishOnly": "bun run build"
26
+ },
27
+ "devDependencies": {
28
+ "@types/bun": "1.4.4",
29
+ "typescript": "7.0.2"
30
+ }
31
+ }
package/readme.md ADDED
@@ -0,0 +1,84 @@
1
+ # memoy
2
+
3
+ Run your memoy agents from any JavaScript runtime (Node, Bun, Deno, edge, browser). No dependencies.
4
+
5
+ ```sh
6
+ npm install @memoy-dev/sdk
7
+ ```
8
+
9
+ ```ts
10
+ import { Memoy, MemoyError } from "@memoy-dev/sdk"
11
+
12
+ // apiKey defaults to process.env.MEMOY_API_KEY
13
+ const memoy = new Memoy({ apiKey: "db_live_..." })
14
+
15
+ // Runs on the agent's own model.
16
+ const reply = await memoy.run({ agent: "your-agent-slug", input: "Hi!" })
17
+ console.log(reply.content, reply.images)
18
+
19
+ // `model` overrides the agent's model for this run only.
20
+ await memoy.run({
21
+ agent: "your-agent-slug",
22
+ model: "openai/gpt-4o-mini",
23
+ input: "Hi!",
24
+ })
25
+
26
+ // Send the whole conversation, oldest first, ending on the user.
27
+ await memoy.run({
28
+ agent: "your-agent-slug",
29
+ messages: [
30
+ { role: "user", content: "Hi!" },
31
+ { role: "agent", content: "Hello! How can I help?" },
32
+ { role: "user", content: "Tell me a joke." },
33
+ ],
34
+ })
35
+ ```
36
+
37
+ Pass a `key` to give the agent memory: it sees and saves only the memories under that key, so you choose how to group them (your user's id, an IP, an account...). Any text without spaces, up to 200 characters. Without a `key` the run has no memory.
38
+
39
+ ```ts
40
+ await memoy.run({
41
+ agent: "your-agent-slug",
42
+ key: "user-42",
43
+ input: "I prefer email.",
44
+ })
45
+ ```
46
+
47
+ List your project's agents, newest first. Each `slug` is what `run`, `memories.search` and `messages.list` take as `agent`.
48
+
49
+ ```ts
50
+ const agents = await memoy.agents.list()
51
+ // [{ slug, name, model, instructions, permissions, public, createdAt, updatedAt }]
52
+ ```
53
+
54
+ Search your project's memories, newest first. Every filter is optional: `agent` (slug of the agent that saved it), `key`, `scope` (`"agent"` or `"project"`) and `query` (content containing the text, ignoring case).
55
+
56
+ ```ts
57
+ const memories = await memoy.memories.search({ key: "user-42", query: "email" })
58
+ // [{ id, content, key, scope, agent, createdAt }]
59
+ ```
60
+
61
+ List the messages sent to your agents and their replies (from the API and the Playground), newest first, one page at a time. Every parameter is optional: `agent`, `key`, `source` (`"api"`, `"playground"` or `"manual"`), `page` (from 1) and `limit` (1 to 100, default 20). `total` counts every match across all pages.
62
+
63
+ ```ts
64
+ const { messages, total } = await memoy.messages.list({ key: "user-42", page: 1, limit: 50 })
65
+ // messages: [{ id, agent, key, source, role, content, createdAt }]
66
+ ```
67
+
68
+ Record messages without running the agent nor spending credits, e.g. the replies a human sent while the agent was paused. They are stored in order with `source: "manual"`, so the next `messages.list` has the whole conversation.
69
+
70
+ ```ts
71
+ await memoy.messages.record({
72
+ agent: "your-agent-slug",
73
+ key: "user-42",
74
+ messages: [
75
+ { role: "user", content: "Can I talk to a person?" },
76
+ { role: "agent", content: "Hi, this is Ana from support." },
77
+ ],
78
+ })
79
+ // [{ id, agent, key, source: "manual", role, content, createdAt }]
80
+ ```
81
+
82
+ Errors from the API (400, 401, 402, 404, 502) throw a `MemoyError` with `status`, `message` and the response `body`.
83
+
84
+ Options: `apiKey`, `baseUrl` (defaults to `https://memoy.dev`) and `fetch`.
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.