@aexhq/sdk 0.85.0 → 0.87.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
@@ -8,14 +8,14 @@ tools, send messages, and read saved conversations without operating a Brain ser
8
8
  Create a key in the [dashboard](https://aex.dev/dashboard). With Node.js 22 or newer, install:
9
9
 
10
10
  ```sh
11
- npm install @aexhq/sdk@0.85.0 @aexhq/agentloop-pi@7.2.4 zod@4.4.3
11
+ npm install @aexhq/sdk@0.87.0 @aexhq/agentloop-pi@8.0.0 zod@4.4.3
12
12
  ```
13
13
 
14
14
  Set `AEX_API_KEY` and `OPENAI_API_KEY` in your server environment. Save as `order.mjs` and
15
15
  run `node order.mjs`:
16
16
 
17
17
  ```js
18
- import { Aex, brainEnv, tool } from "@aexhq/sdk";
18
+ import { Aex, tool } from "@aexhq/sdk";
19
19
  import { pi } from "@aexhq/agentloop-pi";
20
20
  import { z } from "zod";
21
21
 
@@ -27,22 +27,22 @@ const lookupOrder = tool({
27
27
  });
28
28
 
29
29
  const aex = new Aex({ apiKey: process.env.AEX_API_KEY });
30
- try {
31
- const session = await aex.sessions.create({
32
- model: { provider: "openai", name: "gpt-4.1-mini", apiKey: process.env.OPENAI_API_KEY },
33
- agentloop: pi({ env: brainEnv({ name: "brain" }) }),
34
- tools: [lookupOrder()],
35
- });
36
- try {
37
- await session.send("Look up order A-1001. Has it shipped?");
38
- console.log(JSON.stringify(await session.transcript(), null, 2));
39
- console.log("Session:", session.id);
40
- } finally {
41
- await session.end();
30
+ aex.sessions.create({
31
+ model: { provider: "openai", name: "gpt-4.1-mini", apiKey: process.env.OPENAI_API_KEY },
32
+ agentloop: pi(),
33
+ tools: [lookupOrder()],
34
+ }).then(async session => {
35
+ const after = session.state.lastSequence;
36
+ await session.send("Look up order A-1001. Has it shipped?");
37
+ for await (const event of session.events(after)) {
38
+ if (event.type === "turn_failed") throw new Error(JSON.stringify(event.data));
42
39
  }
43
- } finally {
44
- await aex.close();
45
- }
40
+ console.log(JSON.stringify(await session.transcript(), null, 2));
41
+ console.log("Session:", session.id);
42
+ }).catch(error => {
43
+ console.error(error);
44
+ process.exitCode = 1;
45
+ });
46
46
  ```
47
47
 
48
48
  The transcript includes an order lookup and the agent's answer. The lookup function runs in your
@@ -52,7 +52,7 @@ explains setup and where other tools run.
52
52
  ## Sessions and tools
53
53
 
54
54
  The SDK uses Brain's session API and extension helpers. You can import `tool`, `agentloop`,
55
- `brainEnv`, `hostEnv`, `component` and `environment` from `@aexhq/sdk`.
55
+ `brainEnv`, `hostEnv`, `component`, `program` and `environment` from `@aexhq/sdk`.
56
56
 
57
57
  - [Sessions](https://aex.dev/brain/docs/concepts/sessions): send, submit, stream, reconnect and stop.
58
58
  - [Tools](https://aex.dev/brain/docs/guides/write-a-tool): application functions and packaged tools.
@@ -60,13 +60,16 @@ The SDK uses Brain's session API and extension helpers. You can import `tool`, `
60
60
  - [Managed environments](https://github.com/aexhq/aex/blob/main/docs/environments.md): hosted tools and workspaces.
61
61
  - [Attachments](https://github.com/aexhq/aex/blob/main/docs/attachments.md): upload images and PDFs.
62
62
 
63
- Use `ctx.finish(value)` to complete a tool. Use `aex.close()` in `finally` to close client
64
- connections. Closing the client keeps stored sessions; tools in your process still require it.
63
+ Use `ctx.finish(value)` to complete a tool. The shared tool connection suspends after five
64
+ idle seconds and reconnects before the same live client starts more work. Configure
65
+ `connectionIdleTimeoutMs` on `new Aex(...)`; use `0` when other callers or future autonomous
66
+ events must reach these tools. History reads do not reconnect, and event subscriptions remain
67
+ caller-owned. Explicit `aex.close()` permanently disposes of the client while keeping stored sessions.
65
68
 
66
69
  ## Structured output
67
70
 
68
- Ask for a typed application answer on one send. Before ending the session, pass a Zod
69
- schema alongside the message:
71
+ Ask for a typed application answer on one send. Inside the callback, pass a Zod schema
72
+ alongside the message:
70
73
 
71
74
  ```ts
72
75
  const answer = await session.send("Return the order status", {
@@ -119,7 +122,7 @@ correction. It supports JSON Schema, not arbitrary local Zod refinements or tran
119
122
  Brain SDK 0.34 removes typed sends and the corresponding types and errors. Aex SDK 0.84
120
123
  owns them. Existing Aex `send(..., { output })` calls keep the same syntax and behavior.
121
124
  Import `StructuredSendOptions` and `StructuredOutputError` from `@aexhq/sdk`.
122
- Use Pi/Codex 7.2.4 and extension packages pinned to Brain 0.35.1 with this SDK.
125
+ Use Pi 8.0.0, Codex 8.0.0 and extension packages pinned to Brain 0.37.0 with this SDK.
123
126
  Mixing exact Brain dependency versions creates different TypeScript extension brands.
124
127
 
125
128
  An existing standalone Brain handle can use Aex's policy without changing servers:
@@ -165,3 +168,7 @@ request and read its committed outcome later. Desktop apps can use
165
168
  without receiving an account credential.
166
169
 
167
170
  Choose [clientBrowser](https://aex.dev/docs/client-browser) for the user's connected tab, or [Application](https://aex.dev/docs/application) for a deployed backend. Both use `aex.sessions.create()` and ordinary Tool declarations.
171
+
172
+ The SDK prepares hosted code during session setup. To do that earlier and reuse it across
173
+ sessions, call `const loop = await aex.prepare(pi())` during startup, then use `agentloop: loop`.
174
+ See [Environment preparation](https://github.com/aexhq/aex/blob/main/docs/environments.md#prepare-hosted-code).
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export { AexSessionHandle } from "./session.js";
4
4
  export { StructuredOutputError } from "./structured-output.js";
5
5
  export type { StructuredSendOptions } from "./structured-output.js";
6
6
  import { application } from "@aexhq/env-http";
7
- import { Brain, type BrainOptions, type CreateSessionOptions, type OperationOptions, type ModelSelection } from "@aexhq/brain";
7
+ import { Brain, type PlacedAgentloop, type PlacedTool, type BrainOptions, type CreateSessionOptions, type OperationOptions, type ModelSelection } from "@aexhq/brain";
8
8
  import { AexSessionHandle } from "./session.js";
9
9
  import type { ClientAccess, EnvironmentCatalog, TokenUsage } from "./generated.js";
10
10
  import type { Attachment, Account, Usage, ApiKey, IssuedKey, KeyInput, LoginGrantInput, LoginGrant, LoginExchange, AccountSession, Wallet, BillingSettings, LedgerPage, Topup, TopupInput, Refund, RefundInput, SyncPayment } from "./generated.js";
@@ -34,11 +34,12 @@ export declare class Aex {
34
34
  private readonly clientAccess?;
35
35
  constructor({ apiKey, accountToken, clientAccess, maxCostMicroUsd, ...options }: AexOptions);
36
36
  private composition;
37
+ private preparedComposition;
37
38
  get baseUrl(): string;
38
39
  readonly sessions: Readonly<{
39
40
  create: (options: AexSessionOptions, operation?: OperationOptions) => Promise<AexSessionHandle>;
40
41
  get: (sessionId: string, options?: {
41
- tools?: readonly import("@aexhq/brain").PlacedTool[];
42
+ tools?: readonly PlacedTool[];
42
43
  } | undefined) => Promise<AexSessionHandle>;
43
44
  list: () => ReturnType<Brain["sessions"]["list"]>;
44
45
  }>;
@@ -48,6 +49,9 @@ export declare class Aex {
48
49
  models(...args: Parameters<Brain["models"]>): ReturnType<Brain["models"]>;
49
50
  stream(...args: Parameters<Brain["stream"]>): ReturnType<Brain["stream"]>;
50
51
  streamPath(...args: Parameters<Brain["streamPath"]>): ReturnType<Brain["streamPath"]>;
52
+ prepare<T extends PlacedAgentloop | PlacedTool>(extension: T): Promise<T>;
53
+ prepareEnvironment(...args: Parameters<Brain["prepareEnvironment"]>): ReturnType<Brain["prepareEnvironment"]>;
54
+ admitProgram(...args: Parameters<Brain["admitProgram"]>): ReturnType<Brain["admitProgram"]>;
51
55
  admit(...args: Parameters<Brain["admit"]>): ReturnType<Brain["admit"]>;
52
56
  admitAgentloop(...args: Parameters<Brain["admitAgentloop"]>): ReturnType<Brain["admitAgentloop"]>;
53
57
  admitTool(...args: Parameters<Brain["admitTool"]>): ReturnType<Brain["admitTool"]>;
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ export { AexSessionHandle } from "./session.js";
3
3
  export { StructuredOutputError } from "./structured-output.js";
4
4
  import { application } from "@aexhq/env-http";
5
5
  import { composeApplication } from "./application.js";
6
- import { Brain } from "@aexhq/brain";
6
+ import { Brain, inspectAgentloop, inspectTool, inspectEnvironment } from "@aexhq/brain";
7
7
  import { AexSessionHandle } from "./session.js";
8
8
  function nonnegativeInteger(value, name) {
9
9
  if (!Number.isSafeInteger(value) || value < 0)
@@ -43,9 +43,20 @@ export class Aex {
43
43
  throw new TypeError("server-side sessions require a model apiKey");
44
44
  return composeApplication({ ...options, model: { ...model, apiKey: model.apiKey } }, this.baseUrl);
45
45
  }
46
+ async preparedComposition(options) {
47
+ const composition = this.composition(options);
48
+ const agentloop = inspectEnvironment(inspectAgentloop(composition.agentloop).environment).driver.driver === "brain"
49
+ ? await this.client.prepare(composition.agentloop) : composition.agentloop;
50
+ const tools = [];
51
+ for (const tool of composition.tools ?? []) {
52
+ tools.push(inspectEnvironment(inspectTool(tool).environment).driver.driver === "brain"
53
+ ? await this.client.prepare(tool) : tool);
54
+ }
55
+ return { ...composition, agentloop, tools };
56
+ }
46
57
  get baseUrl() { return this.client.baseUrl; }
47
58
  sessions = Object.freeze({
48
- create: async (options, operation) => new AexSessionHandle(await this.client.sessions.create(this.composition(options), operation)),
59
+ create: async (options, operation) => new AexSessionHandle(await this.client.sessions.create(await this.preparedComposition(options), operation)),
49
60
  get: async (...args) => new AexSessionHandle(await this.client.sessions.get(...args)),
50
61
  list: () => this.client.sessions.list(),
51
62
  });
@@ -55,6 +66,9 @@ export class Aex {
55
66
  models(...args) { return this.client.models(...args); }
56
67
  stream(...args) { return this.client.stream(...args); }
57
68
  streamPath(...args) { return this.client.streamPath(...args); }
69
+ prepare(extension) { return this.client.prepare(extension); }
70
+ prepareEnvironment(...args) { return this.client.prepareEnvironment(...args); }
71
+ admitProgram(...args) { return this.client.admitProgram(...args); }
58
72
  admit(...args) { return this.client.admit(...args); }
59
73
  admitAgentloop(...args) { return this.client.admitAgentloop(...args); }
60
74
  admitTool(...args) { return this.client.admitTool(...args); }
@@ -121,7 +135,7 @@ export class Aex {
121
135
  grant: async ({ session, origin, expiresAt = Math.floor(Date.now() / 1000) + 3600 }) => {
122
136
  if (this.clientAccess)
123
137
  throw new TypeError("client grants require a backend API key");
124
- const prepared = await this.client.sessions.prepare(this.composition(session), "authorized-client");
138
+ const prepared = await this.client.sessions.prepare(await this.preparedComposition(session), "authorized-client");
125
139
  return this.request("POST", "/v1/clients", { origin, expires_at: expiresAt, session: prepared });
126
140
  },
127
141
  revoke: (id) => this.request("DELETE", `/v1/clients/${encodeURIComponent(id)}`),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aexhq/sdk",
3
- "version": "0.85.0",
3
+ "version": "0.87.0",
4
4
  "description": "Aex accounts and hosted sessions using the Brain SDK",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -28,9 +28,9 @@
28
28
  "tag": "next"
29
29
  },
30
30
  "dependencies": {
31
- "@aexhq/brain": "0.35.1",
31
+ "@aexhq/brain": "0.37.0",
32
32
  "zod": "4.4.3",
33
- "@aexhq/env-http": "0.3.0"
33
+ "@aexhq/env-http": "0.3.2"
34
34
  },
35
35
  "scripts": {
36
36
  "gen": "node ../../tools/sdk-contracts.mjs",