@alexein/sdk 0.3.3 → 0.3.5

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/dist/wrap.js ADDED
@@ -0,0 +1,318 @@
1
+ import {
2
+ AutoMemory,
3
+ HEADER,
4
+ Reply,
5
+ latestAsked,
6
+ memoryBlock,
7
+ textOf
8
+ } from "./chunk-EGH35T3L.js";
9
+ import "./chunk-NFVCTK7P.js";
10
+
11
+ // src/wrap/index.ts
12
+ var PAUSED = ["tool_use", "pause_turn"];
13
+ var anthropicMessages = {
14
+ asked: (params) => latestAsked(params.messages),
15
+ inject(params, text) {
16
+ const system = params.system;
17
+ if (Array.isArray(system)) return { ...params, system: [...system, { type: "text", text }] };
18
+ if (typeof system === "string" && system.trim())
19
+ return { ...params, system: `${system}
20
+
21
+ ${text}` };
22
+ return { ...params, system: text };
23
+ },
24
+ reply(result) {
25
+ const reply = new Reply();
26
+ for (const part of Array.isArray(result?.content) ? result.content : []) {
27
+ if (part?.type === "text") reply.parts.push(part.text ?? "");
28
+ else if (part?.type === "tool_use") reply.callsTool = true;
29
+ }
30
+ reply.callsTool ||= PAUSED.includes(result?.stop_reason);
31
+ reply.done = true;
32
+ return reply;
33
+ },
34
+ feed(reply, event) {
35
+ if (event?.type === "content_block_start" && event.content_block?.type === "tool_use") {
36
+ reply.callsTool = true;
37
+ } else if (event?.type === "content_block_delta" && event.delta?.type === "text_delta") {
38
+ reply.parts.push(event.delta.text ?? "");
39
+ } else if (event?.type === "message_delta") {
40
+ reply.callsTool ||= PAUSED.includes(event.delta?.stop_reason);
41
+ } else if (event?.type === "message_stop") {
42
+ reply.done = true;
43
+ }
44
+ }
45
+ };
46
+ var INSTRUCTIONS = ["system", "developer"];
47
+ var TOOL_FINISH = ["tool_calls", "function_call"];
48
+ var chatCompletions = {
49
+ asked: (params) => latestAsked(params.messages),
50
+ inject(params, text) {
51
+ const messages = Array.isArray(params.messages) ? [...params.messages] : [];
52
+ let lead = 0;
53
+ while (lead < messages.length && INSTRUCTIONS.includes(messages[lead]?.role)) lead++;
54
+ const role = lead ? messages[0].role : "system";
55
+ messages.splice(lead, 0, { role, content: text });
56
+ return { ...params, messages };
57
+ },
58
+ reply(result) {
59
+ const reply = new Reply();
60
+ const choice = result?.choices?.[0];
61
+ if (choice) {
62
+ const content = choice.message?.content;
63
+ reply.parts.push(typeof content === "string" ? content : textOf(content));
64
+ reply.callsTool = Boolean(choice.message?.tool_calls?.length || choice.message?.function_call) || TOOL_FINISH.includes(choice.finish_reason);
65
+ }
66
+ reply.done = true;
67
+ return reply;
68
+ },
69
+ feed(reply, event) {
70
+ const choice = event?.choices?.[0];
71
+ if (!choice) return;
72
+ if (typeof choice.delta?.content === "string") reply.parts.push(choice.delta.content);
73
+ if (choice.delta?.tool_calls?.length || choice.delta?.function_call) reply.callsTool = true;
74
+ if (choice.finish_reason) {
75
+ reply.done = true;
76
+ reply.callsTool ||= TOOL_FINISH.includes(choice.finish_reason);
77
+ }
78
+ }
79
+ };
80
+ var CALLS = /* @__PURE__ */ new Set([
81
+ "function_call",
82
+ "custom_tool_call",
83
+ "computer_call",
84
+ "local_shell_call",
85
+ "shell_call",
86
+ "apply_patch_call",
87
+ "mcp_approval_request"
88
+ ]);
89
+ var responsesApi = {
90
+ asked(params) {
91
+ const input = params.input;
92
+ if (typeof input === "string")
93
+ return input.trim() ? { text: input.trim(), before: "", at: 0 } : null;
94
+ return latestAsked(input);
95
+ },
96
+ inject(params, text) {
97
+ const given = typeof params.instructions === "string" ? params.instructions.trim() : "";
98
+ return { ...params, instructions: given ? `${params.instructions}
99
+
100
+ ${text}` : text };
101
+ },
102
+ reply(result) {
103
+ const reply = new Reply();
104
+ for (const item of Array.isArray(result?.output) ? result.output : []) {
105
+ if (CALLS.has(item?.type)) reply.callsTool = true;
106
+ else if (item?.type === "message") reply.parts.push(textOf(item.content, ["output_text"]));
107
+ }
108
+ reply.done = result?.status === void 0 || result?.status === null || result?.status === "completed";
109
+ return reply;
110
+ },
111
+ feed(reply, event) {
112
+ if (event?.type === "response.output_text.delta") reply.parts.push(event.delta ?? "");
113
+ else if (event?.type === "response.output_item.added" || event?.type === "response.output_item.done") {
114
+ reply.callsTool ||= CALLS.has(event.item?.type);
115
+ } else if (event?.type === "response.completed") reply.done = true;
116
+ }
117
+ };
118
+ function observed(stream, provider, done) {
119
+ const reply = new Reply();
120
+ let ended = false;
121
+ const end = () => {
122
+ if (!ended) {
123
+ ended = true;
124
+ done(reply);
125
+ }
126
+ };
127
+ return new Proxy(stream, {
128
+ get(target, prop) {
129
+ if (prop === Symbol.asyncIterator) {
130
+ return () => {
131
+ const inner = target[Symbol.asyncIterator]();
132
+ const iterator = {
133
+ async next() {
134
+ const step = await inner.next();
135
+ if (step.done) end();
136
+ else {
137
+ try {
138
+ provider.feed(reply, step.value);
139
+ } catch {
140
+ }
141
+ }
142
+ return step;
143
+ },
144
+ return: (value2) => inner.return?.(value2) ?? Promise.resolve({ done: true, value: value2 }),
145
+ throw: (error) => inner.throw?.(error) ?? Promise.reject(error),
146
+ [Symbol.asyncIterator]() {
147
+ return iterator;
148
+ }
149
+ };
150
+ return iterator;
151
+ };
152
+ }
153
+ const value = Reflect.get(target, prop, target);
154
+ return typeof value === "function" ? value.bind(target) : value;
155
+ }
156
+ });
157
+ }
158
+ var Deferred = class {
159
+ constructor(start, seen) {
160
+ this.start = start;
161
+ this.seen = seen;
162
+ this.#answer().catch(() => {
163
+ });
164
+ }
165
+ start;
166
+ seen;
167
+ #request;
168
+ #result;
169
+ #call() {
170
+ this.#request ??= this.start();
171
+ return this.#request;
172
+ }
173
+ #answer() {
174
+ this.#result ??= this.#call().then(
175
+ ({ call }) => Promise.resolve(call).then((data) => this.seen(data))
176
+ );
177
+ return this.#result;
178
+ }
179
+ then(onfulfilled, onrejected) {
180
+ return this.#answer().then(onfulfilled, onrejected);
181
+ }
182
+ catch(onrejected) {
183
+ return this.#answer().catch(onrejected);
184
+ }
185
+ finally(onfinally) {
186
+ return this.#answer().finally(onfinally);
187
+ }
188
+ async withResponse() {
189
+ const { call } = await this.#call();
190
+ const answered = await call.withResponse();
191
+ return { ...answered, data: this.seen(answered.data) };
192
+ }
193
+ async asResponse() {
194
+ const { call } = await this.#call();
195
+ return call.asResponse();
196
+ }
197
+ get [Symbol.toStringTag]() {
198
+ return "Promise";
199
+ }
200
+ };
201
+ function resource(target, provider, auto, client) {
202
+ function call(method) {
203
+ return (params, ...rest) => {
204
+ const asked = provider.asked(params ?? {});
205
+ let saved = false;
206
+ const remember = (reply) => {
207
+ if (!saved) {
208
+ saved = true;
209
+ auto.remember(asked, reply);
210
+ }
211
+ };
212
+ const seen = (data) => {
213
+ if (params?.stream && data && typeof data === "object") {
214
+ return observed(data, provider, remember);
215
+ }
216
+ try {
217
+ remember(provider.reply(data));
218
+ } catch {
219
+ }
220
+ return data;
221
+ };
222
+ return new Deferred(async () => {
223
+ const text = await auto.lookup(asked);
224
+ return { call: target[method](text ? provider.inject(params, text) : params, ...rest) };
225
+ }, seen);
226
+ };
227
+ }
228
+ const wrapped2 = {
229
+ create: call("create"),
230
+ parse: call("parse")
231
+ };
232
+ const proxy = new Proxy(target, {
233
+ get(t, prop) {
234
+ if (prop in wrapped2) return wrapped2[prop];
235
+ if (prop === "_client") return client();
236
+ const value = Reflect.get(t, prop, t);
237
+ return typeof value === "function" ? (...args) => value.apply(proxy, args) : value;
238
+ }
239
+ });
240
+ return proxy;
241
+ }
242
+ function wrapped(target, resources, auto, rewrap) {
243
+ const made = /* @__PURE__ */ new Map();
244
+ const self = new Proxy(target, {
245
+ get(t, prop) {
246
+ if (prop === "alexein") return auto;
247
+ if (prop === "withOptions") {
248
+ return (...args) => rewrap(t.withOptions(...args));
249
+ }
250
+ const make = typeof prop === "string" ? resources[prop] : void 0;
251
+ if (make) {
252
+ if (!made.has(prop))
253
+ made.set(
254
+ prop,
255
+ make(Reflect.get(t, prop, t), () => self)
256
+ );
257
+ return made.get(prop);
258
+ }
259
+ const value = Reflect.get(t, prop, t);
260
+ return typeof value === "function" ? value.bind(t) : value;
261
+ }
262
+ });
263
+ return self;
264
+ }
265
+ function wrapAnthropic(client, memory, options) {
266
+ const auto = autoMemory(memory, options);
267
+ const rewrap = (target) => wrapped(
268
+ target,
269
+ { messages: (value, self) => resource(value, anthropicMessages, auto, self) },
270
+ auto,
271
+ rewrap
272
+ );
273
+ return rewrap(client);
274
+ }
275
+ function wrapOpenAI(client, memory, options) {
276
+ const auto = autoMemory(memory, options);
277
+ const rewrap = (target) => wrapped(
278
+ target,
279
+ {
280
+ chat: (value, self) => {
281
+ let completions;
282
+ return new Proxy(value, {
283
+ get(t, prop) {
284
+ if (prop === "completions") {
285
+ completions ??= resource(Reflect.get(t, prop, t), chatCompletions, auto, self);
286
+ return completions;
287
+ }
288
+ const inner = Reflect.get(t, prop, t);
289
+ return typeof inner === "function" ? inner.bind(t) : inner;
290
+ }
291
+ });
292
+ },
293
+ responses: (value, self) => resource(value, responsesApi, auto, self)
294
+ },
295
+ auto,
296
+ rewrap
297
+ );
298
+ return rewrap(client);
299
+ }
300
+ function autoMemory(memory, options) {
301
+ if (options) return new AutoMemory(memory, options);
302
+ if (memory && "userId" in memory) return new AutoMemory(void 0, memory);
303
+ throw new TypeError("Alexein needs to know whose memory this is: pass { userId }");
304
+ }
305
+ function memoryOf(client) {
306
+ const auto = client?.alexein;
307
+ if (!(auto instanceof AutoMemory))
308
+ throw new TypeError("not a client wrapAnthropic or wrapOpenAI answered");
309
+ return auto;
310
+ }
311
+ export {
312
+ AutoMemory,
313
+ HEADER,
314
+ memoryBlock,
315
+ memoryOf,
316
+ wrapAnthropic,
317
+ wrapOpenAI
318
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexein/sdk",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "JavaScript and TypeScript client for Alexein: long-term memory for your users and agents, with LangGraph.js and OpenAI Agents SDK integrations.",
5
5
  "license": "MIT",
6
6
  "author": "Alexein AI",
@@ -20,7 +20,10 @@
20
20
  "agents",
21
21
  "ai-agents",
22
22
  "langgraph",
23
- "openai-agents"
23
+ "openai-agents",
24
+ "anthropic",
25
+ "openai",
26
+ "ai-sdk"
24
27
  ],
25
28
  "type": "module",
26
29
  "engines": {
@@ -61,6 +64,26 @@
61
64
  "default": "./dist/openai-agents.cjs"
62
65
  }
63
66
  },
67
+ "./wrap": {
68
+ "import": {
69
+ "types": "./dist/wrap.d.ts",
70
+ "default": "./dist/wrap.js"
71
+ },
72
+ "require": {
73
+ "types": "./dist/wrap.d.cts",
74
+ "default": "./dist/wrap.cjs"
75
+ }
76
+ },
77
+ "./ai-sdk": {
78
+ "import": {
79
+ "types": "./dist/ai-sdk.d.ts",
80
+ "default": "./dist/ai-sdk.js"
81
+ },
82
+ "require": {
83
+ "types": "./dist/ai-sdk.d.cts",
84
+ "default": "./dist/ai-sdk.cjs"
85
+ }
86
+ },
64
87
  "./package.json": "./package.json"
65
88
  },
66
89
  "typesVersions": {
@@ -70,6 +93,12 @@
70
93
  ],
71
94
  "openai-agents": [
72
95
  "./dist/openai-agents.d.ts"
96
+ ],
97
+ "wrap": [
98
+ "./dist/wrap.d.ts"
99
+ ],
100
+ "ai-sdk": [
101
+ "./dist/ai-sdk.d.ts"
73
102
  ]
74
103
  }
75
104
  },
@@ -95,12 +124,16 @@
95
124
  "prepublishOnly": "npm run check && npm run build"
96
125
  },
97
126
  "peerDependencies": {
127
+ "@ai-sdk/provider": ">=4.0.0 <5.0.0",
98
128
  "@langchain/core": ">=1.0.0 <2.0.0",
99
129
  "@langchain/langgraph": ">=1.0.0 <2.0.0",
100
130
  "@openai/agents": ">=0.21.0 <1.0.0",
101
131
  "zod": "^3.25.0 || ^4.0.0"
102
132
  },
103
133
  "peerDependenciesMeta": {
134
+ "@ai-sdk/provider": {
135
+ "optional": true
136
+ },
104
137
  "@langchain/core": {
105
138
  "optional": true
106
139
  },
@@ -115,11 +148,15 @@
115
148
  }
116
149
  },
117
150
  "devDependencies": {
151
+ "@ai-sdk/provider": "^4.0.39",
152
+ "@anthropic-ai/sdk": "^1.14.0",
118
153
  "@langchain/core": "^1.3.0",
119
154
  "@langchain/langgraph": "^1.6.0",
120
155
  "@openai/agents": "^0.21.0",
121
156
  "@types/node": "^22.10.0",
157
+ "ai": "^7.0.183",
122
158
  "langchain": "^1.6.0",
159
+ "openai": "^7.64.0",
123
160
  "prettier": "^3.4.0",
124
161
  "tsup": "^8.3.0",
125
162
  "typescript": "^5.7.0",