@usestratus/sdk 0.12.0 → 0.12.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.
Files changed (2) hide show
  1. package/README.md +541 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,541 @@
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset=".github/logo-dark.svg">
4
+ <source media="(prefers-color-scheme: light)" srcset=".github/logo.svg">
5
+ <img src=".github/logo.svg" alt="Stratus" width="80" height="80">
6
+ </picture>
7
+ </p>
8
+
9
+ # Stratus
10
+
11
+ [usestratus.dev](https://usestratus.dev)
12
+
13
+ [![npm version](https://img.shields.io/npm/v/@usestratus/sdk)](https://www.npmjs.com/package/@usestratus/sdk)
14
+ [![CI](https://github.com/tylergibbs1/stratus/actions/workflows/ci.yml/badge.svg)](https://github.com/tylergibbs1/stratus/actions/workflows/ci.yml)
15
+
16
+ A better TypeScript agent SDK for Azure OpenAI. Build multi-agent systems with tools, handoffs, guardrails, streaming, structured output, and more.
17
+
18
+ - **Built for Azure, not bolted on** — auto-endpoint detection, Entra ID auth, content filter errors as typed exceptions, and built-in retry. No 404 config spirals, no "which SDK do I use" confusion.
19
+ - **Two API backends, one interface** — Chat Completions and Responses API through the same agent, tool, and session code. Start with one, switch with a single line.
20
+ - **Multi-agent orchestration** — handoffs, subagents, guardrails, and hooks compose through a single run loop. Hooks can deny or modify tool calls at runtime.
21
+ - **Client-side state you control** — save, resume, and fork conversations as portable JSON snapshots. No server-side threads, no opaque session IDs.
22
+ - **Type-safe from schema to output** — Zod schemas drive tool parameters, structured output, and validation. Context types flow through agents, hooks, and guardrails at compile time.
23
+ - **Zero dependencies** — only Zod as a peer dep. No transitive dependency sprawl, no framework lock-in.
24
+
25
+ `agents` `tools` `streaming` `structured output` `handoffs` `subagents` `guardrails` `hooks` `tracing` `sessions` `abort signals` `code mode` `todo tracking` `cost tracking`
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ bun add @usestratus/sdk
31
+ ```
32
+
33
+ Stratus requires [Zod](https://zod.dev) as a peer dependency:
34
+
35
+ ```bash
36
+ bun add zod
37
+ ```
38
+
39
+ ## Quick Start
40
+
41
+ ```ts
42
+ import { z } from "zod";
43
+ import { Agent, AzureResponsesModel, run, tool } from "@usestratus/sdk";
44
+
45
+ const model = new AzureResponsesModel({
46
+ endpoint: process.env.AZURE_OPENAI_ENDPOINT!,
47
+ apiKey: process.env.AZURE_OPENAI_API_KEY!,
48
+ deployment: "gpt-5.2",
49
+ });
50
+
51
+ const getWeather = tool({
52
+ name: "get_weather",
53
+ description: "Get the current weather for a city",
54
+ parameters: z.object({
55
+ city: z.string().describe("The city name"),
56
+ }),
57
+ execute: async (_ctx, { city }) => {
58
+ return `72°F and sunny in ${city}`;
59
+ },
60
+ });
61
+
62
+ const agent = new Agent({
63
+ name: "weather-assistant",
64
+ instructions: "You are a helpful weather assistant.",
65
+ model,
66
+ tools: [getWeather],
67
+ });
68
+
69
+ const result = await run(agent, "What's the weather in New York?");
70
+ console.log(result.output);
71
+ ```
72
+
73
+ ## Core Concepts
74
+
75
+ ### Agents
76
+
77
+ Agents are the primary building block. Each agent has a name, instructions, a model, and optional tools, handoffs, guardrails, and hooks.
78
+
79
+ ```ts
80
+ const agent = new Agent({
81
+ name: "my-agent",
82
+ instructions: "You are a helpful assistant.",
83
+ model,
84
+ tools: [myTool],
85
+ });
86
+
87
+ // Dynamic instructions based on context
88
+ const agent = new Agent({
89
+ name: "my-agent",
90
+ instructions: (ctx) => `You are helping ${ctx.userName}.`,
91
+ model,
92
+ });
93
+ ```
94
+
95
+ ### Tools
96
+
97
+ Define tools with Zod schemas for type-safe parameter validation:
98
+
99
+ ```ts
100
+ const searchTool = tool({
101
+ name: "search",
102
+ description: "Search for information",
103
+ parameters: z.object({
104
+ query: z.string().describe("Search query"),
105
+ limit: z.number().optional().describe("Max results"),
106
+ }),
107
+ execute: async (context, { query, limit }) => {
108
+ // Tool logic here
109
+ return "search results";
110
+ },
111
+ });
112
+ ```
113
+
114
+ ### Streaming
115
+
116
+ Stream responses token-by-token:
117
+
118
+ ```ts
119
+ const { stream: s, result } = stream(agent, "Tell me a story");
120
+
121
+ for await (const event of s) {
122
+ if (event.type === "content_delta") {
123
+ process.stdout.write(event.content);
124
+ } else if (event.type === "tool_call_start") {
125
+ console.log(`Calling: ${event.toolCall.name}`);
126
+ }
127
+ }
128
+
129
+ const finalResult = await result;
130
+ ```
131
+
132
+ ### Structured Output
133
+
134
+ Use Zod schemas to get typed, validated output:
135
+
136
+ ```ts
137
+ const PersonSchema = z.object({
138
+ name: z.string(),
139
+ age: z.number(),
140
+ occupation: z.string(),
141
+ });
142
+
143
+ const agent = new Agent({
144
+ name: "extractor",
145
+ instructions: "Extract person information.",
146
+ model,
147
+ outputType: PersonSchema,
148
+ });
149
+
150
+ const result = await run(agent, "Marie Curie was a 66-year-old physicist.");
151
+ console.log(result.finalOutput); // { name: "Marie Curie", age: 66, occupation: "physicist" }
152
+ ```
153
+
154
+ ### Sessions
155
+
156
+ Sessions maintain conversation history across multiple interactions:
157
+
158
+ ```ts
159
+ import { createSession } from "@usestratus/sdk";
160
+
161
+ const session = createSession({ model, tools: [myTool] });
162
+
163
+ session.send("Hello!");
164
+ for await (const event of session.stream()) {
165
+ // handle events
166
+ }
167
+
168
+ session.send("Follow-up question");
169
+ for await (const event of session.stream()) {
170
+ // handle events
171
+ }
172
+
173
+ // Save and resume sessions
174
+ const snapshot = session.save();
175
+ const resumed = resumeSession(snapshot, { model });
176
+
177
+ // Fork a session (new ID, same history)
178
+ const forked = forkSession(snapshot, { model });
179
+
180
+ // Cleanup
181
+ session.close();
182
+ // Or use Symbol.asyncDispose:
183
+ await using session = createSession({ model });
184
+ ```
185
+
186
+ ### Handoffs
187
+
188
+ Transfer control between specialized agents:
189
+
190
+ ```ts
191
+ import { handoff } from "@usestratus/sdk";
192
+
193
+ const orderAgent = new Agent({
194
+ name: "order_specialist",
195
+ instructions: "Help with order inquiries.",
196
+ model,
197
+ tools: [lookupOrder],
198
+ handoffDescription: "Transfer for order questions",
199
+ });
200
+
201
+ const triageAgent = new Agent({
202
+ name: "triage",
203
+ instructions: "Route to the right specialist.",
204
+ model,
205
+ handoffs: [
206
+ orderAgent, // shorthand
207
+ handoff({ // with options
208
+ agent: refundAgent,
209
+ onHandoff: () => console.log("Transferring..."),
210
+ }),
211
+ ],
212
+ });
213
+
214
+ const result = await run(triageAgent, "Where is my order?");
215
+ console.log(result.lastAgent.name); // "order_specialist"
216
+ ```
217
+
218
+ ### Subagents
219
+
220
+ Delegate subtasks to child agents that run independently:
221
+
222
+ ```ts
223
+ import { subagent } from "@usestratus/sdk";
224
+
225
+ const researcher = new Agent({
226
+ name: "researcher",
227
+ instructions: "Research topics thoroughly.",
228
+ model,
229
+ });
230
+
231
+ const parentAgent = new Agent({
232
+ name: "parent",
233
+ instructions: "Use the researcher for deep dives.",
234
+ model,
235
+ subagents: [
236
+ subagent({
237
+ agent: researcher,
238
+ inputSchema: z.object({ topic: z.string() }),
239
+ mapInput: ({ topic }) => `Research: ${topic}`,
240
+ }),
241
+ ],
242
+ });
243
+ ```
244
+
245
+ ### Guardrails
246
+
247
+ Validate inputs and outputs with guardrails:
248
+
249
+ ```ts
250
+ import type { InputGuardrail, OutputGuardrail } from "@usestratus/sdk";
251
+
252
+ const profanityFilter: InputGuardrail = {
253
+ name: "profanity_filter",
254
+ execute: (input) => ({
255
+ tripwireTriggered: containsProfanity(input),
256
+ outputInfo: "Blocked by profanity filter",
257
+ }),
258
+ };
259
+
260
+ const piiFilter: OutputGuardrail = {
261
+ name: "pii_filter",
262
+ execute: (output) => ({
263
+ tripwireTriggered: /\d{3}-\d{2}-\d{4}/.test(output),
264
+ outputInfo: "Output contained PII",
265
+ }),
266
+ };
267
+
268
+ const agent = new Agent({
269
+ name: "guarded",
270
+ model,
271
+ inputGuardrails: [profanityFilter],
272
+ outputGuardrails: [piiFilter],
273
+ });
274
+ ```
275
+
276
+ Guardrails run in parallel. When a tripwire is triggered, an `InputGuardrailTripwireTriggered` or `OutputGuardrailTripwireTriggered` error is thrown.
277
+
278
+ ### Hooks
279
+
280
+ Lifecycle hooks for observability and control:
281
+
282
+ ```ts
283
+ import type { AgentHooks } from "@usestratus/sdk";
284
+
285
+ const hooks: AgentHooks = {
286
+ beforeRun: ({ agent, input }) => { /* ... */ },
287
+ afterRun: ({ agent, result }) => { /* ... */ },
288
+
289
+ // Return a decision to allow, deny, or modify tool calls
290
+ beforeToolCall: ({ toolCall }) => {
291
+ if (toolCall.function.name === "dangerous_tool") {
292
+ return { decision: "deny", reason: "Not allowed" };
293
+ }
294
+ return { decision: "allow" };
295
+ },
296
+ afterToolCall: ({ toolCall, result }) => { /* ... */ },
297
+
298
+ // Allow or deny handoffs
299
+ beforeHandoff: ({ fromAgent, toAgent }) => {
300
+ return { decision: "allow" };
301
+ },
302
+ };
303
+ ```
304
+
305
+ ### Tracing
306
+
307
+ Opt-in tracing with zero overhead when inactive:
308
+
309
+ ```ts
310
+ import { withTrace } from "@usestratus/sdk";
311
+
312
+ const { result, trace } = await withTrace("my-workflow", () =>
313
+ run(agent, "Hello"),
314
+ );
315
+
316
+ console.log(trace.id);
317
+ console.log(trace.duration);
318
+ for (const span of trace.spans) {
319
+ console.log(`[${span.type}] ${span.name} (${span.duration}ms)`);
320
+ // span.type: "model_call" | "tool_execution" | "handoff" | "guardrail" | "subagent" | "custom"
321
+ }
322
+ ```
323
+
324
+ ### Abort Signals
325
+
326
+ Cancel runs with `AbortSignal`:
327
+
328
+ ```ts
329
+ const controller = new AbortController();
330
+
331
+ setTimeout(() => controller.abort(), 5000);
332
+
333
+ try {
334
+ const result = await run(agent, "Long task...", {
335
+ signal: controller.signal,
336
+ });
337
+ } catch (error) {
338
+ if (error instanceof RunAbortedError) {
339
+ console.log("Run was cancelled");
340
+ }
341
+ }
342
+ ```
343
+
344
+ ### Todo Tracking
345
+
346
+ Track task progress during agent execution:
347
+
348
+ ```ts
349
+ import { todoTool, TodoList } from "@usestratus/sdk";
350
+
351
+ const todos = new TodoList();
352
+ todos.onUpdate((items) => {
353
+ for (const item of items) {
354
+ const icon = item.status === "completed" ? "+" : item.status === "in_progress" ? ">" : "-";
355
+ console.log(`${icon} ${item.content}`);
356
+ }
357
+ });
358
+
359
+ const agent = new Agent({
360
+ name: "planner",
361
+ instructions: "Break tasks into steps and track progress with todo_write.",
362
+ model,
363
+ tools: [todoTool(todos)],
364
+ });
365
+
366
+ await run(agent, "Set up a new TypeScript project");
367
+ ```
368
+
369
+ ### Usage & Cost Tracking
370
+
371
+ Track token usage and estimate costs:
372
+
373
+ ```ts
374
+ import { createCostEstimator } from "@usestratus/sdk";
375
+
376
+ const estimator = createCostEstimator({
377
+ inputTokenCostPer1k: 0.01,
378
+ outputTokenCostPer1k: 0.03,
379
+ });
380
+
381
+ const result = await run(agent, "Hello", { costEstimator: estimator });
382
+ console.log(result.usage.totalTokens); // token counts
383
+ console.log(result.totalCostUsd); // estimated cost
384
+ console.log(result.numTurns); // model call count
385
+
386
+ // Set budget limits
387
+ const result = await run(agent, "Hello", {
388
+ costEstimator: estimator,
389
+ maxBudgetUsd: 0.50, // throws MaxBudgetExceededError if exceeded
390
+ });
391
+ ```
392
+
393
+ ### Tool Choice & Tool Use Behavior
394
+
395
+ Control how the model uses tools:
396
+
397
+ ```ts
398
+ const agent = new Agent({
399
+ name: "my-agent",
400
+ model,
401
+ tools: [myTool],
402
+ modelSettings: {
403
+ // "auto" | "none" | "required" | { type: "function", function: { name: "..." } }
404
+ toolChoice: "required",
405
+ },
406
+ // "run_llm_again" (default) | "stop_on_first_tool" | { stopAtToolNames: ["..."] }
407
+ toolUseBehavior: "stop_on_first_tool",
408
+ });
409
+ ```
410
+
411
+ ### Code Mode (Experimental)
412
+
413
+ Let LLMs write code that orchestrates multiple tools instead of calling them one at a time. Inspired by [Cloudflare's Code Mode](https://blog.cloudflare.com/code-mode-the-better-way-to-use-mcp) — LLMs are better at writing code than making individual tool calls.
414
+
415
+ ```ts
416
+ import { createCodeModeTool, FunctionExecutor } from "@usestratus/sdk/core";
417
+
418
+ const executor = new FunctionExecutor({ timeout: 30_000 });
419
+ const codemode = createCodeModeTool({
420
+ tools: [getWeather, sendEmail, lookupOrder],
421
+ executor,
422
+ });
423
+
424
+ const agent = new Agent({
425
+ name: "assistant",
426
+ model,
427
+ tools: [codemode],
428
+ });
429
+
430
+ // The LLM writes code like:
431
+ // async () => {
432
+ // const weather = await codemode.get_weather({ location: "London" });
433
+ // if (weather.temp > 60) {
434
+ // await codemode.send_email({ to: "team@co.com", subject: "Nice day!", body: ... });
435
+ // }
436
+ // return { weather, notified: true };
437
+ // }
438
+ ```
439
+
440
+ `createCodeModeTool` generates TypeScript types from your tools, presents the LLM with a single `execute_code` tool, and runs the generated code in an executor. All tool calls happen within one invocation — no round-trips through the model between calls.
441
+
442
+ Two built-in executors:
443
+ - **`FunctionExecutor`** — fast, same-process (NOT sandboxed)
444
+ - **`WorkerExecutor`** — isolated via `worker_threads` (separate V8 context, no host access)
445
+
446
+ Implement the `Executor` interface for custom sandboxes (containers, Cloudflare Workers, etc.).
447
+
448
+ ## Imports
449
+
450
+ Stratus provides three export paths:
451
+
452
+ ```ts
453
+ // Everything (core + Azure)
454
+ import { Agent, run, tool, AzureChatCompletionsModel, AzureResponsesModel } from "@usestratus/sdk";
455
+
456
+ // Core only (provider-agnostic)
457
+ import { Agent, run, tool } from "@usestratus/sdk/core";
458
+
459
+ // Azure provider only
460
+ import { AzureChatCompletionsModel, AzureResponsesModel } from "@usestratus/sdk/azure";
461
+ ```
462
+
463
+ ## Configuration
464
+
465
+ ### Azure OpenAI
466
+
467
+ Stratus includes two interchangeable Azure model implementations:
468
+
469
+ ```ts
470
+ // Chat Completions API
471
+ const model = new AzureChatCompletionsModel({
472
+ endpoint: process.env.AZURE_OPENAI_ENDPOINT!,
473
+ apiKey: process.env.AZURE_OPENAI_API_KEY!,
474
+ deployment: "gpt-5.2",
475
+ apiVersion: "2025-03-01-preview", // optional, this is the default
476
+ });
477
+
478
+ // Responses API
479
+ const model = new AzureResponsesModel({
480
+ endpoint: process.env.AZURE_OPENAI_ENDPOINT!,
481
+ apiKey: process.env.AZURE_OPENAI_API_KEY!,
482
+ deployment: "gpt-5.2",
483
+ apiVersion: "2025-04-01-preview", // optional, this is the default
484
+ });
485
+ ```
486
+
487
+ Both implement the same `Model` interface — swap one for the other without changing any agent, tool, or session code.
488
+
489
+ ### Environment Variables
490
+
491
+ ```
492
+ AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
493
+ AZURE_OPENAI_API_KEY=your-api-key
494
+ ```
495
+
496
+ ## Error Handling
497
+
498
+ All errors extend `StratusError`:
499
+
500
+ | Error | Description |
501
+ |---|---|
502
+ | `StratusError` | Base error class |
503
+ | `ModelError` | API call failures (includes `status` and `code`) |
504
+ | `ContentFilterError` | Content filtered by Azure's content management policy |
505
+ | `MaxTurnsExceededError` | Agent exceeded the `maxTurns` limit |
506
+ | `OutputParseError` | Structured output failed Zod validation |
507
+ | `RunAbortedError` | Run cancelled via `AbortSignal` |
508
+ | `InputGuardrailTripwireTriggered` | Input guardrail blocked the request |
509
+ | `OutputGuardrailTripwireTriggered` | Output guardrail blocked the response |
510
+
511
+ ```ts
512
+ import { ModelError, MaxTurnsExceededError, RunAbortedError } from "@usestratus/sdk";
513
+
514
+ try {
515
+ await run(agent, input);
516
+ } catch (error) {
517
+ if (error instanceof MaxTurnsExceededError) {
518
+ // Agent ran too many turns
519
+ } else if (error instanceof ModelError) {
520
+ console.log(error.status, error.code);
521
+ }
522
+ }
523
+ ```
524
+
525
+ ## Packages
526
+
527
+ Stratus is a monorepo with two packages:
528
+
529
+ | Package | Description |
530
+ |---|---|
531
+ | [`@usestratus/sdk`](packages/stratus-sdk/) | Agent SDK for Azure OpenAI (this README) |
532
+ | [`@usestratus/mcp-aws`](packages/mcp-aws/) | MCP server framework for AWS — deploy MCP servers to Lambda with progressive disclosure, tool gating, and code mode |
533
+
534
+ ## Development
535
+
536
+ ```bash
537
+ bun install # Install all workspace dependencies
538
+ bun test # Run tests (all packages)
539
+ bun run lint # Lint with Biome
540
+ bun run typecheck # TypeScript type checking
541
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usestratus/sdk",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",