@opperai/agents 0.1.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 ADDED
@@ -0,0 +1,210 @@
1
+ # Opper Agent SDK (TypeScript)
2
+
3
+ Type‑safe, composable agents for Opper AI in TypeScript using: generics, Zod schemas, modular tools, and modern DX.
4
+
5
+ - Async‑first
6
+ - Strong typing with Zod runtime validation
7
+ - Opper integration (spans, usage, retries)
8
+ - Hooks for observability and custom behavior
9
+ - Tools: function, decorator, MCP provider, or other agents via `.asTool()`
10
+
11
+ ## Install
12
+
13
+ Prerequisites: Node.js 20+ and pnpm (recommended).
14
+
15
+ ```bash
16
+ pnpm add @opperai/agents zod
17
+ ```
18
+
19
+ Set your Opper key:
20
+
21
+ ```bash
22
+ export OPPER_API_KEY="<your-key>"
23
+ ```
24
+
25
+ ## Use Locally
26
+
27
+ Two quick ways to consume this package from a local checkout:
28
+
29
+ 1. Live symlink (best for active development)
30
+
31
+ ```bash
32
+ # In the app that will consume the SDK
33
+ pnpm link ../opperai-agent-sdk-node
34
+ ```
35
+
36
+ ```ts
37
+ // Then import normally
38
+ import { Agent } from "@opperai/agents";
39
+ ```
40
+
41
+ 2. Local file dependency (copied on install)
42
+
43
+ ```json
44
+ // app/package.json
45
+ {
46
+ "dependencies": {
47
+ "@opperai/agents": "file:../opperai-agent-sdk-node"
48
+ }
49
+ }
50
+ ```
51
+
52
+ Notes:
53
+
54
+ - Build the SDK first (and optionally watch) so `dist/` is up to date: `pnpm build` or `pnpm dev`.
55
+ - With `link`, changes in this repo are reflected in your app after rebuild.
56
+
57
+ ## Quick Start
58
+
59
+ Minimal agent that returns structured JSON:
60
+
61
+ ```ts
62
+ import { Agent } from "@opperai/agents";
63
+ import { z } from "zod";
64
+
65
+ const Output = z.object({ message: z.string() });
66
+
67
+ type OutputType = z.infer<typeof Output>;
68
+
69
+ const agent = new Agent<string, OutputType>({
70
+ name: "HelloAgent",
71
+ instructions: "Greet the user briefly.",
72
+ outputSchema: Output,
73
+ });
74
+
75
+ const result = await agent.process("Say hi to Ada");
76
+ // => { message: "Hi Ada!" }
77
+ ```
78
+
79
+ ## Tools
80
+
81
+ Define tools with Zod schemas. Results are discriminated unions that never throw.
82
+
83
+ ```ts
84
+ import {
85
+ createFunctionTool,
86
+ ToolResultFactory,
87
+ Agent,
88
+ } from "@opperai/agents";
89
+ import { z } from "zod";
90
+
91
+ const add = createFunctionTool(
92
+ (input: { a: number; b: number }) => input.a + input.b,
93
+ { name: "add", schema: z.object({ a: z.number(), b: z.number() }) },
94
+ );
95
+
96
+ const agent = new Agent<string, { answer: number }>({
97
+ name: "MathAgent",
98
+ instructions: "Use tools to compute when needed.",
99
+ tools: [add],
100
+ outputSchema: z.object({ answer: z.number() }),
101
+ });
102
+
103
+ const res = await agent.process("What is (5 + 3)? Return JSON with answer.");
104
+ ```
105
+
106
+ Decorator tools and extraction:
107
+
108
+ ```ts
109
+ import { tool, extractTools } from "@opperai/agents";
110
+ import { z } from "zod";
111
+
112
+ class WeatherTools {
113
+ @tool({ schema: z.object({ city: z.string() }) })
114
+ async get_weather({ city }: { city: string }) {
115
+ return { city, tempC: 22 };
116
+ }
117
+ }
118
+
119
+ const tools = extractTools(new WeatherTools());
120
+ ```
121
+
122
+ ## Agent as Tool
123
+
124
+ Compose agents by reusing them as tools.
125
+
126
+ ```ts
127
+ import { Agent } from "@opperai/agents";
128
+ import { z } from "zod";
129
+
130
+ const Summarize = new Agent<string, { summary: string }>({
131
+ name: "Summarizer",
132
+ instructions: "Summarize in one sentence.",
133
+ outputSchema: z.object({ summary: z.string() }),
134
+ });
135
+
136
+ const Coordinator = new Agent<string, string>({
137
+ name: "Coordinator",
138
+ instructions: "Delegate to tools and answer.",
139
+ tools: [Summarize.asTool("summarize")],
140
+ });
141
+
142
+ const answer = await Coordinator.process(
143
+ "Summarize: Opper helps you build agents.",
144
+ );
145
+ ```
146
+
147
+ ## MCP Integration
148
+
149
+ Wrap MCP servers as tool providers in one line.
150
+
151
+ ```ts
152
+ import { mcp, MCPconfig, Agent } from "@opperai/agents";
153
+
154
+ const fsServer = MCPconfig({ name: "fs", transport: "stdio" });
155
+
156
+ const agent = new Agent<string, string>({
157
+ name: "FSReader",
158
+ instructions: "Use filesystem tools to answer questions.",
159
+ tools: [mcp(fsServer)],
160
+ });
161
+
162
+ const out = await agent.process("Read README.md and summarize it.");
163
+ ```
164
+
165
+ ## Hooks & Context
166
+
167
+ Observe the loop and collect metrics without breaking execution.
168
+
169
+ ```ts
170
+ import { HookEvents } from "@opperai/agents";
171
+
172
+ agent.registerHook(HookEvents.LoopEnd, ({ context }) => {
173
+ const last = context.executionHistory.at(-1);
174
+ if (last) console.log("iteration", last.iteration);
175
+ });
176
+ ```
177
+
178
+ `AgentContext` tracks usage automatically. Inspect it via hooks:
179
+
180
+ ```ts
181
+ agent.registerHook(HookEvents.AgentEnd, ({ context }) => {
182
+ console.log(context.usage); // { requests, inputTokens, outputTokens, totalTokens }
183
+ });
184
+ ```
185
+
186
+ ## Type Safety with Zod
187
+
188
+ - Provide `inputSchema`/`outputSchema` to validate at runtime and type the agent at compile time.
189
+ - Schemas with defaults/transforms are supported. If you use Zod defaults on fields (e.g., `z.string().default("")`), the agent will still deliver a fully‑typed output.
190
+
191
+ ## Testing & Tooling
192
+
193
+ ```bash
194
+ pnpm install
195
+ pnpm lint
196
+ pnpm format
197
+ pnpm test
198
+ pnpm build
199
+ ```
200
+
201
+ - Tests use `vitest` and avoid real network calls.
202
+ - Build bundles ESM/CJS and `.d.ts` into `dist/` via `tsup`.
203
+
204
+ ## Examples
205
+
206
+ See `examples/` for getting started, tools, composition, memory, and MCP usage. Run with `pnpm build` then `tsx <file>`.
207
+
208
+ ## License
209
+
210
+ MIT