formagent-sdk 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/LICENSE +21 -0
- package/README.md +280 -0
- package/dist/cli/index.js +4539 -0
- package/dist/index.js +5705 -0
- package/package.json +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 FormAgent Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# FormAgent SDK
|
|
2
|
+
|
|
3
|
+
A powerful AI Agent framework for building intelligent assistants with streaming support, tool execution, skills, hooks, and MCP integration.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/formagent-sdk)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Session-Based API**: Multi-turn conversations with state management
|
|
11
|
+
- **Streaming Support**: Real-time streaming of LLM responses with event-based notifications
|
|
12
|
+
- **Built-in Tools**: File operations, bash execution, web fetch, and task management
|
|
13
|
+
- **Tool System**: Flexible tool registration with Zod schema support
|
|
14
|
+
- **Skills System**: Extend agent capabilities with discoverable skills
|
|
15
|
+
- **Hooks System**: Intercept and control agent behavior (PreToolUse, PostToolUse)
|
|
16
|
+
- **Structured Outputs**: JSON Schema validated responses
|
|
17
|
+
- **MCP Integration**: Model Context Protocol server support
|
|
18
|
+
- **Multi-Provider**: Support for Anthropic Claude and OpenAI models
|
|
19
|
+
- **Cost Tracking**: Token usage and cost estimation
|
|
20
|
+
- **Type-Safe**: Full TypeScript support with strict typing
|
|
21
|
+
- **Zero Config**: Auto-reads API keys from environment variables
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install formagent-sdk
|
|
27
|
+
# or
|
|
28
|
+
bun add formagent-sdk
|
|
29
|
+
# or
|
|
30
|
+
yarn add formagent-sdk
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
### One-Shot Prompts
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { prompt } from "formagent-sdk"
|
|
39
|
+
|
|
40
|
+
const response = await prompt("What is 2+2?")
|
|
41
|
+
console.log(response) // "4"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Sessions with Tools
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { createSession, builtinTools } from "formagent-sdk"
|
|
48
|
+
|
|
49
|
+
const session = await createSession({
|
|
50
|
+
model: "claude-sonnet-4-20250514",
|
|
51
|
+
tools: builtinTools,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
await session.send("List all TypeScript files in the current directory")
|
|
55
|
+
|
|
56
|
+
for await (const event of session.receive()) {
|
|
57
|
+
if (event.type === "text") {
|
|
58
|
+
process.stdout.write(event.text)
|
|
59
|
+
} else if (event.type === "tool_use") {
|
|
60
|
+
console.log(`Using tool: ${event.name}`)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
await session.close()
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Environment Variables
|
|
68
|
+
|
|
69
|
+
The SDK automatically reads configuration from environment variables:
|
|
70
|
+
|
|
71
|
+
| Variable | Description |
|
|
72
|
+
|----------|-------------|
|
|
73
|
+
| `ANTHROPIC_API_KEY` | Anthropic API key (required for Claude models) |
|
|
74
|
+
| `ANTHROPIC_BASE_URL` | Custom Anthropic API endpoint |
|
|
75
|
+
| `OPENAI_API_KEY` | OpenAI API key (for GPT models) |
|
|
76
|
+
| `OPENAI_BASE_URL` | Custom OpenAI API endpoint |
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
export ANTHROPIC_API_KEY=your-api-key
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Custom Tools
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { tool } from "formagent-sdk"
|
|
86
|
+
import { z } from "zod"
|
|
87
|
+
|
|
88
|
+
const weatherTool = tool({
|
|
89
|
+
name: "get_weather",
|
|
90
|
+
description: "Get the current weather for a location",
|
|
91
|
+
schema: z.object({
|
|
92
|
+
location: z.string().describe("City name"),
|
|
93
|
+
unit: z.enum(["celsius", "fahrenheit"]).optional(),
|
|
94
|
+
}),
|
|
95
|
+
execute: async ({ location, unit = "celsius" }) => {
|
|
96
|
+
return `Weather in ${location}: 22°${unit === "celsius" ? "C" : "F"}`
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
const session = await createSession({
|
|
101
|
+
model: "claude-sonnet-4-20250514",
|
|
102
|
+
tools: [weatherTool],
|
|
103
|
+
})
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Built-in Tools
|
|
107
|
+
|
|
108
|
+
| Tool | Description |
|
|
109
|
+
|------|-------------|
|
|
110
|
+
| **Bash** | Execute bash commands with timeout support |
|
|
111
|
+
| **Read** | Read file contents with optional line range |
|
|
112
|
+
| **Write** | Write content to files, creates directories |
|
|
113
|
+
| **Edit** | Find/replace text in files |
|
|
114
|
+
| **Glob** | Find files matching glob patterns |
|
|
115
|
+
| **Grep** | Search file contents with regex |
|
|
116
|
+
| **WebFetch** | Fetch URL content, converts HTML to markdown |
|
|
117
|
+
| **TodoWrite** | Manage task lists for progress tracking |
|
|
118
|
+
| **Skill** | Discover and invoke specialized skills |
|
|
119
|
+
|
|
120
|
+
**Security defaults (important):**
|
|
121
|
+
- File tools (`Read`/`Write`/`Edit`/`Glob`/`Grep`) are restricted to `process.cwd()` by default; configure `allowedPaths` to widen access.
|
|
122
|
+
- `WebFetch` blocks localhost/private network targets by default; set `allowPrivateNetwork: true` to override.
|
|
123
|
+
- `Bash` blocks a small set of high-risk command patterns by default; set `allowDangerous: true` to disable the denylist.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { builtinTools, fileTools, createBuiltinTools } from "formagent-sdk"
|
|
127
|
+
|
|
128
|
+
// Use all built-in tools
|
|
129
|
+
const session = await createSession({
|
|
130
|
+
tools: builtinTools,
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// Or use just file tools (Read, Write, Edit, Glob, Grep)
|
|
134
|
+
const session = await createSession({
|
|
135
|
+
tools: fileTools,
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
// Or configure access boundaries explicitly
|
|
139
|
+
const tools = createBuiltinTools({
|
|
140
|
+
cwd: process.cwd(),
|
|
141
|
+
allowedPaths: [process.cwd()],
|
|
142
|
+
allowPrivateNetwork: false,
|
|
143
|
+
allowDangerous: false,
|
|
144
|
+
})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Skills System
|
|
148
|
+
|
|
149
|
+
Load skills from directories to extend agent capabilities:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { createSession, DEFAULT_USER_SKILLS_PATH } from "formagent-sdk"
|
|
153
|
+
|
|
154
|
+
const session = await createSession({
|
|
155
|
+
model: "claude-sonnet-4-20250514",
|
|
156
|
+
settingSources: [
|
|
157
|
+
DEFAULT_USER_SKILLS_PATH, // ~/.claude/skills
|
|
158
|
+
"/path/to/project/skills",
|
|
159
|
+
],
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// Claude can now use the Skill tool to discover and invoke skills
|
|
163
|
+
await session.send("What skills are available?")
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Hooks System
|
|
167
|
+
|
|
168
|
+
Intercept and control tool execution:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { createSession, builtinTools, type HookCallback } from "formagent-sdk"
|
|
172
|
+
|
|
173
|
+
const protectEnvFiles: HookCallback = async (input, toolUseId, context) => {
|
|
174
|
+
const filePath = input.tool_input?.file_path as string
|
|
175
|
+
|
|
176
|
+
if (filePath?.endsWith(".env")) {
|
|
177
|
+
return {
|
|
178
|
+
hookSpecificOutput: {
|
|
179
|
+
hookEventName: "PreToolUse",
|
|
180
|
+
permissionDecision: "deny",
|
|
181
|
+
permissionDecisionReason: "Cannot modify .env files",
|
|
182
|
+
},
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return {}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const session = await createSession({
|
|
189
|
+
model: "claude-sonnet-4-20250514",
|
|
190
|
+
tools: builtinTools,
|
|
191
|
+
hooks: {
|
|
192
|
+
PreToolUse: [
|
|
193
|
+
{ matcher: "Write|Edit", hooks: [protectEnvFiles] },
|
|
194
|
+
],
|
|
195
|
+
},
|
|
196
|
+
})
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Structured Outputs
|
|
200
|
+
|
|
201
|
+
Get validated JSON responses:
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import { createSession } from "formagent-sdk"
|
|
205
|
+
|
|
206
|
+
const session = await createSession({
|
|
207
|
+
model: "claude-sonnet-4-20250514",
|
|
208
|
+
outputFormat: {
|
|
209
|
+
type: "json_schema",
|
|
210
|
+
schema: {
|
|
211
|
+
type: "object",
|
|
212
|
+
properties: {
|
|
213
|
+
summary: { type: "string" },
|
|
214
|
+
score: { type: "number" },
|
|
215
|
+
},
|
|
216
|
+
required: ["summary"],
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
for await (const event of session.receive()) {
|
|
222
|
+
if (event.type === "result" && event.structured_output) {
|
|
223
|
+
console.log(event.structured_output)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Session Management
|
|
229
|
+
|
|
230
|
+
Resume and fork sessions:
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
import { createSession, resumeSession, forkSession } from "formagent-sdk"
|
|
234
|
+
|
|
235
|
+
// Create a session
|
|
236
|
+
const session = await createSession({ model: "claude-sonnet-4-20250514" })
|
|
237
|
+
const sessionId = session.id
|
|
238
|
+
|
|
239
|
+
// Later: Resume the session
|
|
240
|
+
const resumed = await resumeSession(sessionId)
|
|
241
|
+
|
|
242
|
+
// Or: Fork the session (create a branch)
|
|
243
|
+
const forked = await forkSession(sessionId)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Event Types
|
|
247
|
+
|
|
248
|
+
The `receive()` method yields different event types:
|
|
249
|
+
|
|
250
|
+
| Type | Properties | Description |
|
|
251
|
+
|------|------------|-------------|
|
|
252
|
+
| `text` | `text: string` | Text content chunk |
|
|
253
|
+
| `tool_use` | `id, name, input` | Tool invocation |
|
|
254
|
+
| `tool_result` | `tool_use_id, content, is_error` | Tool execution result |
|
|
255
|
+
| `message` | `message: SDKMessage` | Complete message |
|
|
256
|
+
| `result` | `structured_output` | Structured output (when configured) |
|
|
257
|
+
| `stop` | `stop_reason, usage` | Generation complete |
|
|
258
|
+
| `error` | `error: Error` | Error occurred |
|
|
259
|
+
|
|
260
|
+
## Examples
|
|
261
|
+
|
|
262
|
+
See the [examples](./examples) directory for complete examples:
|
|
263
|
+
|
|
264
|
+
- Basic sessions and prompts
|
|
265
|
+
- Streaming responses
|
|
266
|
+
- Custom tools and MCP servers
|
|
267
|
+
- Skills and hooks
|
|
268
|
+
- Structured outputs
|
|
269
|
+
- CLI agent implementation
|
|
270
|
+
|
|
271
|
+
## Documentation
|
|
272
|
+
|
|
273
|
+
- [Getting Started](./docs/getting-started.md)
|
|
274
|
+
- [API Reference](./docs/api-reference.md)
|
|
275
|
+
- [Built-in Tools](./docs/tools.md)
|
|
276
|
+
- [MCP Servers](./docs/mcp-servers.md)
|
|
277
|
+
|
|
278
|
+
## License
|
|
279
|
+
|
|
280
|
+
MIT
|