borderless-agent 0.0.1-alpha.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.
- package/README.md +274 -0
- package/dist/agentBuilder.d.ts +51 -0
- package/dist/agentBuilder.d.ts.map +1 -0
- package/dist/agentBuilder.js +130 -0
- package/dist/agentBuilder.js.map +1 -0
- package/dist/agentInstance.d.ts +45 -0
- package/dist/agentInstance.d.ts.map +1 -0
- package/dist/agentInstance.js +501 -0
- package/dist/agentInstance.js.map +1 -0
- package/dist/agentsCore.d.ts +16 -0
- package/dist/agentsCore.d.ts.map +1 -0
- package/dist/agentsCore.js +50 -0
- package/dist/agentsCore.js.map +1 -0
- package/dist/cli/index.d.ts +5 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +11 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/main.d.ts +36 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +306 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +98 -0
- package/dist/config.js.map +1 -0
- package/dist/contextCore.d.ts +97 -0
- package/dist/contextCore.d.ts.map +1 -0
- package/dist/contextCore.js +533 -0
- package/dist/contextCore.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +46 -0
- package/dist/index.js.map +1 -0
- package/dist/llmProtocol.d.ts +60 -0
- package/dist/llmProtocol.d.ts.map +1 -0
- package/dist/llmProtocol.js +202 -0
- package/dist/llmProtocol.js.map +1 -0
- package/dist/loopCore.d.ts +22 -0
- package/dist/loopCore.d.ts.map +1 -0
- package/dist/loopCore.js +288 -0
- package/dist/loopCore.js.map +1 -0
- package/dist/main.d.ts +9 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +15 -0
- package/dist/main.js.map +1 -0
- package/dist/memoryCore.d.ts +37 -0
- package/dist/memoryCore.d.ts.map +1 -0
- package/dist/memoryCore.js +346 -0
- package/dist/memoryCore.js.map +1 -0
- package/dist/server/app.d.ts +13 -0
- package/dist/server/app.d.ts.map +1 -0
- package/dist/server/app.js +200 -0
- package/dist/server/app.js.map +1 -0
- package/dist/sessionCore.d.ts +54 -0
- package/dist/sessionCore.d.ts.map +1 -0
- package/dist/sessionCore.js +312 -0
- package/dist/sessionCore.js.map +1 -0
- package/dist/skillsCore.d.ts +23 -0
- package/dist/skillsCore.d.ts.map +1 -0
- package/dist/skillsCore.js +202 -0
- package/dist/skillsCore.js.map +1 -0
- package/dist/storage/cloudBackend.d.ts +48 -0
- package/dist/storage/cloudBackend.d.ts.map +1 -0
- package/dist/storage/cloudBackend.js +301 -0
- package/dist/storage/cloudBackend.js.map +1 -0
- package/dist/storage/fileBackend.d.ts +41 -0
- package/dist/storage/fileBackend.d.ts.map +1 -0
- package/dist/storage/fileBackend.js +240 -0
- package/dist/storage/fileBackend.js.map +1 -0
- package/dist/storage/index.d.ts +21 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +58 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/protocols.d.ts +29 -0
- package/dist/storage/protocols.d.ts.map +1 -0
- package/dist/storage/protocols.js +20 -0
- package/dist/storage/protocols.js.map +1 -0
- package/dist/todoCore.d.ts +15 -0
- package/dist/todoCore.d.ts.map +1 -0
- package/dist/todoCore.js +52 -0
- package/dist/todoCore.js.map +1 -0
- package/dist/toolsCore.d.ts +31 -0
- package/dist/toolsCore.d.ts.map +1 -0
- package/dist/toolsCore.js +609 -0
- package/dist/toolsCore.js.map +1 -0
- package/dist/types.d.ts +144 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
# borderless-agent
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/borderless-agent)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
A portable, framework-agnostic **agentic AI toolkit** for TypeScript/Node.js. Build production-ready AI agents with custom tools, skills, system prompts, and persistent sessions — in just a few lines of code.
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
npm install borderless-agent
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## ✨ Highlights
|
|
13
|
+
|
|
14
|
+
- 🔧 **Custom Tools** — Give your agent any capability with typed tool definitions
|
|
15
|
+
- 🧠 **Skills** — Hot-load domain knowledge on demand
|
|
16
|
+
- 💬 **Sessions** — Persistent, resumable conversation history
|
|
17
|
+
- 🌊 **Streaming** — Token-by-token SSE-compatible streaming
|
|
18
|
+
- 🧩 **Framework-agnostic** — Works with Express, Next.js, Hono, standalone scripts
|
|
19
|
+
- 🔒 **Approval Flow** — Gate dangerous tool calls behind user confirmation
|
|
20
|
+
- 📦 **Zero config** — Works out of the box with OpenAI-compatible APIs
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { AgentBuilder } from 'borderless-agent';
|
|
28
|
+
|
|
29
|
+
const agent = new AgentBuilder()
|
|
30
|
+
.setLLM({ apiKey: process.env.OPENAI_API_KEY! })
|
|
31
|
+
.setSystemPrompt('You are a concise, helpful assistant.')
|
|
32
|
+
.addTool({
|
|
33
|
+
name: 'get_weather',
|
|
34
|
+
description: 'Get the current weather for a city',
|
|
35
|
+
parameters: { city: { type: 'string', description: 'City name' } },
|
|
36
|
+
required: ['city'],
|
|
37
|
+
execute: async (args) => {
|
|
38
|
+
// Your logic here
|
|
39
|
+
return JSON.stringify({ city: args.city, temp: '22°C', sky: 'sunny' });
|
|
40
|
+
},
|
|
41
|
+
})
|
|
42
|
+
.build();
|
|
43
|
+
|
|
44
|
+
const result = await agent.chat('What is the weather in Tokyo?');
|
|
45
|
+
console.log(result.reply);
|
|
46
|
+
// → "The weather in Tokyo is 22°C and sunny."
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Core Concepts
|
|
52
|
+
|
|
53
|
+
### Tools
|
|
54
|
+
|
|
55
|
+
Tools let your agent take actions. Define a name, description, parameters, and an `execute` function — the agent decides when to call them.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
agent.addTool({
|
|
59
|
+
name: 'search_docs',
|
|
60
|
+
description: 'Search internal documentation',
|
|
61
|
+
parameters: {
|
|
62
|
+
query: { type: 'string', description: 'Search query' },
|
|
63
|
+
limit: { type: 'integer', description: 'Max results' },
|
|
64
|
+
},
|
|
65
|
+
required: ['query'],
|
|
66
|
+
execute: async (args) => {
|
|
67
|
+
const results = await mySearchEngine.search(args.query, args.limit ?? 5);
|
|
68
|
+
return JSON.stringify(results);
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Skills
|
|
74
|
+
|
|
75
|
+
Skills inject specialized knowledge into the agent's context when loaded via the `Skill` tool. Use them for domain-specific instructions, API docs, or workflow guides.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
builder.addSkill({
|
|
79
|
+
name: 'sql-expert',
|
|
80
|
+
description: 'Expert knowledge for writing SQL queries',
|
|
81
|
+
body: `
|
|
82
|
+
## SQL Best Practices
|
|
83
|
+
- Always use parameterized queries
|
|
84
|
+
- Prefer JOINs over subqueries for readability
|
|
85
|
+
- Add indexes on frequently filtered columns
|
|
86
|
+
...
|
|
87
|
+
`,
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Sessions
|
|
92
|
+
|
|
93
|
+
Sessions maintain conversation history across multiple turns and persist to disk (or cloud storage).
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// Create a new session
|
|
97
|
+
const session = agent.createSession();
|
|
98
|
+
await session.chat('My name is Alice and I work on Project X.');
|
|
99
|
+
const r = await session.chat('What project do I work on?');
|
|
100
|
+
console.log(r.reply); // → "You work on Project X."
|
|
101
|
+
|
|
102
|
+
// Persist
|
|
103
|
+
await session.save();
|
|
104
|
+
|
|
105
|
+
// Restore later (even after restart)
|
|
106
|
+
const restored = agent.restoreSession(session.id);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Streaming
|
|
110
|
+
|
|
111
|
+
Stream responses token-by-token — perfect for chat UIs and SSE endpoints.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
for await (const chunk of agent.stream('Explain quantum computing')) {
|
|
115
|
+
if (chunk.delta) process.stdout.write(chunk.delta);
|
|
116
|
+
if (chunk.done) console.log('\n[done]');
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Builder API
|
|
123
|
+
|
|
124
|
+
| Method | Description |
|
|
125
|
+
|--------|-------------|
|
|
126
|
+
| `.setLLM({ apiKey, model?, baseUrl?, timeout? })` | Configure OpenAI-compatible LLM |
|
|
127
|
+
| `.setLLMProvider(provider)` | Supply a custom `LLMProvider` implementation |
|
|
128
|
+
| `.setSystemPrompt(prompt)` | Set the base system prompt |
|
|
129
|
+
| `.addTool(tool)` / `.addTools(tools)` | Register custom tools |
|
|
130
|
+
| `.addSkill(skill)` / `.addSkills(skills)` | Register skills |
|
|
131
|
+
| `.setIncludeBuiltinTools(bool)` | Include bash, file read/write/edit, grep (`true` by default) |
|
|
132
|
+
| `.setStorage({ backend, dir? })` | Configure persistence (`'file'` or `'cloud'`) |
|
|
133
|
+
| `.enableMemory()` | Enable long-term episodic + semantic memory |
|
|
134
|
+
| `.enableStreaming()` | Enable streaming by default |
|
|
135
|
+
| `.enableContext()` | Enable token budgeting & history trimming |
|
|
136
|
+
| `.setMaxToolRounds(n)` | Safety limit for tool-use loops (default: 20) |
|
|
137
|
+
| `.setApprovalCallback(fn)` | Gate mutating tool calls behind user approval |
|
|
138
|
+
| `.build()` | → `AgentInstance` |
|
|
139
|
+
|
|
140
|
+
## Agent API
|
|
141
|
+
|
|
142
|
+
| Method | Returns | Description |
|
|
143
|
+
|--------|---------|-------------|
|
|
144
|
+
| `agent.chat(message, history?)` | `Promise<ChatResult>` | Single stateless turn |
|
|
145
|
+
| `agent.stream(message, history?)` | `AsyncGenerator<StreamChunk>` | Streaming turn |
|
|
146
|
+
| `agent.createSession()` | `AgentSession` | New persistent session |
|
|
147
|
+
| `agent.restoreSession(id)` | `AgentSession \| null` | Restore by ID |
|
|
148
|
+
| `agent.listSessions()` | `string[]` | All saved session IDs |
|
|
149
|
+
| `agent.listSessionSummaries(limit?)` | `object[]` | Session metadata |
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Integration Examples
|
|
154
|
+
|
|
155
|
+
### Next.js (App Router)
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// app/api/chat/route.ts
|
|
159
|
+
import { AgentBuilder } from 'borderless-agent';
|
|
160
|
+
|
|
161
|
+
const agent = new AgentBuilder()
|
|
162
|
+
.setLLM({ apiKey: process.env.OPENAI_API_KEY! })
|
|
163
|
+
.setSystemPrompt('You are a helpful assistant.')
|
|
164
|
+
.setIncludeBuiltinTools(false) // no bash/fs in serverless
|
|
165
|
+
.addTool({ name: 'lookup', description: '...', execute: async (args) => '...' })
|
|
166
|
+
.build();
|
|
167
|
+
|
|
168
|
+
export async function POST(req: Request) {
|
|
169
|
+
const { message, sessionId } = await req.json();
|
|
170
|
+
const session = agent.restoreSession(sessionId) ?? agent.createSession();
|
|
171
|
+
const result = await session.chat(message);
|
|
172
|
+
return Response.json({ reply: result.reply, sessionId: session.id });
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Next.js (Streaming SSE)
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
export async function POST(req: Request) {
|
|
180
|
+
const { message } = await req.json();
|
|
181
|
+
|
|
182
|
+
const stream = new ReadableStream({
|
|
183
|
+
async start(controller) {
|
|
184
|
+
const encoder = new TextEncoder();
|
|
185
|
+
for await (const chunk of agent.stream(message)) {
|
|
186
|
+
if (chunk.delta) {
|
|
187
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ delta: chunk.delta })}\n\n`));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
controller.close();
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return new Response(stream, {
|
|
195
|
+
headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache' },
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Express
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import express from 'express';
|
|
204
|
+
import { AgentBuilder } from 'borderless-agent';
|
|
205
|
+
|
|
206
|
+
const app = express();
|
|
207
|
+
app.use(express.json());
|
|
208
|
+
|
|
209
|
+
const agent = new AgentBuilder()
|
|
210
|
+
.setLLM({ apiKey: process.env.OPENAI_API_KEY! })
|
|
211
|
+
.build();
|
|
212
|
+
|
|
213
|
+
app.post('/chat', async (req, res) => {
|
|
214
|
+
const result = await agent.chat(req.body.message);
|
|
215
|
+
res.json({ reply: result.reply });
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
app.listen(3000);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Approval Callback
|
|
224
|
+
|
|
225
|
+
Gate dangerous tool calls behind user confirmation:
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
const agent = new AgentBuilder()
|
|
229
|
+
.setLLM({ apiKey: '...' })
|
|
230
|
+
.setApprovalCallback(async (toolName, args) => {
|
|
231
|
+
console.log(`🔐 Tool "${toolName}" wants to run with:`, args);
|
|
232
|
+
// Return true to approve, false to deny
|
|
233
|
+
return toolName !== 'bash'; // e.g., allow everything except bash
|
|
234
|
+
})
|
|
235
|
+
.build();
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Types
|
|
241
|
+
|
|
242
|
+
All types are fully exported for TypeScript consumers:
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
import type {
|
|
246
|
+
ToolDefinition,
|
|
247
|
+
SkillDefinition,
|
|
248
|
+
AgentConfig,
|
|
249
|
+
ChatResult,
|
|
250
|
+
StreamChunk,
|
|
251
|
+
AgentSession,
|
|
252
|
+
LLMConfig,
|
|
253
|
+
LLMProvider,
|
|
254
|
+
} from 'borderless-agent';
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Configuration
|
|
260
|
+
|
|
261
|
+
| Environment Variable | Default | Description |
|
|
262
|
+
|---------------------|---------|-------------|
|
|
263
|
+
| `OPENAI_API_KEY` | — | OpenAI API key |
|
|
264
|
+
| `MODEL_ID` | `gpt-4o` | Model identifier |
|
|
265
|
+
| `AGENT_STREAM` | `0` | Enable streaming (`1` to enable) |
|
|
266
|
+
| `AGENT_MEMORY` | `0` | Enable long-term memory |
|
|
267
|
+
| `AGENT_CONTEXT` | `1` | Enable context management |
|
|
268
|
+
| `AGENT_STORAGE_BACKEND` | `file` | `file` or `cloud` |
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agentBuilder.ts — Fluent builder for creating portable agent instances.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* ```ts
|
|
6
|
+
* const agent = new AgentBuilder()
|
|
7
|
+
* .setLLM({ apiKey: 'sk-...', model: 'gpt-4o' })
|
|
8
|
+
* .setSystemPrompt('You are a helpful assistant.')
|
|
9
|
+
* .addTool({ name: 'greet', description: 'Say hi', execute: () => 'Hi!' })
|
|
10
|
+
* .addSkill({ name: 'ts', description: 'TypeScript', body: '...' })
|
|
11
|
+
* .enableMemory()
|
|
12
|
+
* .build();
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
import { LLMProvider } from './llmProtocol';
|
|
16
|
+
import { ToolDefinition, SkillDefinition, LLMConfig, StorageConfig } from './types';
|
|
17
|
+
import { AgentInstance } from './agentInstance';
|
|
18
|
+
export declare class AgentBuilder {
|
|
19
|
+
private _config;
|
|
20
|
+
/** Provide an LLM config (creates an OpenAIProvider automatically). */
|
|
21
|
+
setLLM(config: LLMConfig): this;
|
|
22
|
+
/** Provide a custom LLMProvider instance directly. */
|
|
23
|
+
setLLMProvider(provider: LLMProvider): this;
|
|
24
|
+
/** Set the base system prompt the agent uses. */
|
|
25
|
+
setSystemPrompt(prompt: string): this;
|
|
26
|
+
/** Add a single user-defined tool. */
|
|
27
|
+
addTool(tool: ToolDefinition): this;
|
|
28
|
+
/** Add multiple user-defined tools at once. */
|
|
29
|
+
addTools(tools: ToolDefinition[]): this;
|
|
30
|
+
/** Whether to include built-in tools (bash, read_file, etc.). Default: true. */
|
|
31
|
+
setIncludeBuiltinTools(include: boolean): this;
|
|
32
|
+
/** Add a single skill. */
|
|
33
|
+
addSkill(skill: SkillDefinition): this;
|
|
34
|
+
/** Add multiple skills at once. */
|
|
35
|
+
addSkills(skills: SkillDefinition[]): this;
|
|
36
|
+
/** Configure storage backend. */
|
|
37
|
+
setStorage(config: StorageConfig): this;
|
|
38
|
+
/** Enable long-term memory (episodic + semantic). */
|
|
39
|
+
enableMemory(enable?: boolean): this;
|
|
40
|
+
/** Enable streaming responses by default. */
|
|
41
|
+
enableStreaming(enable?: boolean): this;
|
|
42
|
+
/** Enable context management (token budgeting, history trimming). */
|
|
43
|
+
enableContext(enable?: boolean): this;
|
|
44
|
+
/** Max tool rounds per turn (safety limit). */
|
|
45
|
+
setMaxToolRounds(max: number): this;
|
|
46
|
+
/** Set approval callback for mutating tools. */
|
|
47
|
+
setApprovalCallback(cb: (toolName: string, args: Record<string, any>) => Promise<boolean> | boolean): this;
|
|
48
|
+
/** Validate config and build the agent instance. */
|
|
49
|
+
build(): AgentInstance;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=agentBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agentBuilder.d.ts","sourceRoot":"","sources":["../src/agentBuilder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAkB,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EACH,cAAc,EACd,eAAe,EAEf,SAAS,EACT,aAAa,EAChB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,qBAAa,YAAY;IACrB,OAAO,CAAC,OAAO,CAQb;IAIF,uEAAuE;IACvE,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI;IAK/B,sDAAsD;IACtD,cAAc,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI;IAO3C,iDAAiD;IACjD,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAOrC,sCAAsC;IACtC,OAAO,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI;IAMnC,+CAA+C;IAC/C,QAAQ,CAAC,KAAK,EAAE,cAAc,EAAE,GAAG,IAAI;IAMvC,gFAAgF;IAChF,sBAAsB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAO9C,0BAA0B;IAC1B,QAAQ,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI;IAMtC,mCAAmC;IACnC,SAAS,CAAC,MAAM,EAAE,eAAe,EAAE,GAAG,IAAI;IAQ1C,iCAAiC;IACjC,UAAU,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI;IAOvC,qDAAqD;IACrD,YAAY,CAAC,MAAM,GAAE,OAAc,GAAG,IAAI;IAK1C,6CAA6C;IAC7C,eAAe,CAAC,MAAM,GAAE,OAAc,GAAG,IAAI;IAK7C,qEAAqE;IACrE,aAAa,CAAC,MAAM,GAAE,OAAc,GAAG,IAAI;IAK3C,+CAA+C;IAC/C,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAKnC,gDAAgD;IAChD,mBAAmB,CACf,EAAE,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,GAChF,IAAI;IAOP,oDAAoD;IACpD,KAAK,IAAI,aAAa;CAmBzB"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* agentBuilder.ts — Fluent builder for creating portable agent instances.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* ```ts
|
|
7
|
+
* const agent = new AgentBuilder()
|
|
8
|
+
* .setLLM({ apiKey: 'sk-...', model: 'gpt-4o' })
|
|
9
|
+
* .setSystemPrompt('You are a helpful assistant.')
|
|
10
|
+
* .addTool({ name: 'greet', description: 'Say hi', execute: () => 'Hi!' })
|
|
11
|
+
* .addSkill({ name: 'ts', description: 'TypeScript', body: '...' })
|
|
12
|
+
* .enableMemory()
|
|
13
|
+
* .build();
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.AgentBuilder = void 0;
|
|
18
|
+
const llmProtocol_1 = require("./llmProtocol");
|
|
19
|
+
const agentInstance_1 = require("./agentInstance");
|
|
20
|
+
class AgentBuilder {
|
|
21
|
+
_config = {
|
|
22
|
+
includeBuiltinTools: true,
|
|
23
|
+
enableMemory: false,
|
|
24
|
+
enableStreaming: false,
|
|
25
|
+
enableContext: true,
|
|
26
|
+
maxToolRounds: 20,
|
|
27
|
+
tools: [],
|
|
28
|
+
skills: [],
|
|
29
|
+
};
|
|
30
|
+
// ---- LLM ----
|
|
31
|
+
/** Provide an LLM config (creates an OpenAIProvider automatically). */
|
|
32
|
+
setLLM(config) {
|
|
33
|
+
this._config.llmConfig = config;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/** Provide a custom LLMProvider instance directly. */
|
|
37
|
+
setLLMProvider(provider) {
|
|
38
|
+
this._config.llm = provider;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
// ---- System prompt ----
|
|
42
|
+
/** Set the base system prompt the agent uses. */
|
|
43
|
+
setSystemPrompt(prompt) {
|
|
44
|
+
this._config.systemPrompt = prompt;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
// ---- Tools ----
|
|
48
|
+
/** Add a single user-defined tool. */
|
|
49
|
+
addTool(tool) {
|
|
50
|
+
this._config.tools = this._config.tools ?? [];
|
|
51
|
+
this._config.tools.push(tool);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/** Add multiple user-defined tools at once. */
|
|
55
|
+
addTools(tools) {
|
|
56
|
+
this._config.tools = this._config.tools ?? [];
|
|
57
|
+
this._config.tools.push(...tools);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/** Whether to include built-in tools (bash, read_file, etc.). Default: true. */
|
|
61
|
+
setIncludeBuiltinTools(include) {
|
|
62
|
+
this._config.includeBuiltinTools = include;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
// ---- Skills ----
|
|
66
|
+
/** Add a single skill. */
|
|
67
|
+
addSkill(skill) {
|
|
68
|
+
this._config.skills = this._config.skills ?? [];
|
|
69
|
+
this._config.skills.push(skill);
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
/** Add multiple skills at once. */
|
|
73
|
+
addSkills(skills) {
|
|
74
|
+
this._config.skills = this._config.skills ?? [];
|
|
75
|
+
this._config.skills.push(...skills);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
// ---- Storage ----
|
|
79
|
+
/** Configure storage backend. */
|
|
80
|
+
setStorage(config) {
|
|
81
|
+
this._config.storage = config;
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
// ---- Feature toggles ----
|
|
85
|
+
/** Enable long-term memory (episodic + semantic). */
|
|
86
|
+
enableMemory(enable = true) {
|
|
87
|
+
this._config.enableMemory = enable;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
/** Enable streaming responses by default. */
|
|
91
|
+
enableStreaming(enable = true) {
|
|
92
|
+
this._config.enableStreaming = enable;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
/** Enable context management (token budgeting, history trimming). */
|
|
96
|
+
enableContext(enable = true) {
|
|
97
|
+
this._config.enableContext = enable;
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
/** Max tool rounds per turn (safety limit). */
|
|
101
|
+
setMaxToolRounds(max) {
|
|
102
|
+
this._config.maxToolRounds = Math.max(1, Math.min(max, 100));
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
/** Set approval callback for mutating tools. */
|
|
106
|
+
setApprovalCallback(cb) {
|
|
107
|
+
this._config.approvalCallback = cb;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
// ---- Build ----
|
|
111
|
+
/** Validate config and build the agent instance. */
|
|
112
|
+
build() {
|
|
113
|
+
// Resolve LLM provider
|
|
114
|
+
if (!this._config.llm) {
|
|
115
|
+
const cfg = this._config.llmConfig;
|
|
116
|
+
if (!cfg?.apiKey) {
|
|
117
|
+
throw new Error('AgentBuilder: must call .setLLM({ apiKey }) or .setLLMProvider() before .build()');
|
|
118
|
+
}
|
|
119
|
+
this._config.llm = new llmProtocol_1.OpenAIProvider({
|
|
120
|
+
apiKey: cfg.apiKey,
|
|
121
|
+
model: cfg.model ?? 'gpt-4o',
|
|
122
|
+
baseUrl: cfg.baseUrl,
|
|
123
|
+
timeout: cfg.timeout ?? 120,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return new agentInstance_1.AgentInstance({ ...this._config });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.AgentBuilder = AgentBuilder;
|
|
130
|
+
//# sourceMappingURL=agentBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agentBuilder.js","sourceRoot":"","sources":["../src/agentBuilder.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;AAEH,+CAA4D;AAQ5D,mDAAgD;AAEhD,MAAa,YAAY;IACb,OAAO,GAAgB;QAC3B,mBAAmB,EAAE,IAAI;QACzB,YAAY,EAAE,KAAK;QACnB,eAAe,EAAE,KAAK;QACtB,aAAa,EAAE,IAAI;QACnB,aAAa,EAAE,EAAE;QACjB,KAAK,EAAE,EAAE;QACT,MAAM,EAAE,EAAE;KACb,CAAC;IAEF,gBAAgB;IAEhB,uEAAuE;IACvE,MAAM,CAAC,MAAiB;QACpB,IAAI,CAAC,OAAO,CAAC,SAAS,GAAG,MAAM,CAAC;QAChC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,sDAAsD;IACtD,cAAc,CAAC,QAAqB;QAChC,IAAI,CAAC,OAAO,CAAC,GAAG,GAAG,QAAQ,CAAC;QAC5B,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,0BAA0B;IAE1B,iDAAiD;IACjD,eAAe,CAAC,MAAc;QAC1B,IAAI,CAAC,OAAO,CAAC,YAAY,GAAG,MAAM,CAAC;QACnC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,kBAAkB;IAElB,sCAAsC;IACtC,OAAO,CAAC,IAAoB;QACxB,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;QAC9C,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9B,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,+CAA+C;IAC/C,QAAQ,CAAC,KAAuB;QAC5B,IAAI,CAAC,OAAO,CAAC,KAAK,G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Builder.build().
|
|
3
|
+
*
|
|
4
|
+
* Provides: chat(), stream(), createSession(), restoreSession().
|
|
5
|
+
* All internal modules are wired through dependency injection — no globals.
|
|
6
|
+
*/
|
|
7
|
+
import { AgentConfig, ToolDefinition, ChatResult, StreamChunk, AgentSession } from './types';
|
|
8
|
+
import { LLMProvider } from './llmProtocol';
|
|
9
|
+
export declare class AgentInstance {
|
|
10
|
+
private _llm;
|
|
11
|
+
private _systemPrompt;
|
|
12
|
+
private _tools;
|
|
13
|
+
private _skills;
|
|
14
|
+
private _openaiTools;
|
|
15
|
+
private _toolMap;
|
|
16
|
+
private _sessionMgr;
|
|
17
|
+
private _maxToolRounds;
|
|
18
|
+
private _memoryEnabled;
|
|
19
|
+
private _streamingEnabled;
|
|
20
|
+
private _contextEnabled;
|
|
21
|
+
private _approvalCallback?;
|
|
22
|
+
constructor(config: AgentConfig);
|
|
23
|
+
/** Send a single message (no session, stateless). */
|
|
24
|
+
chat(message: string, history?: Record<string, any>[]): Promise<ChatResult>;
|
|
25
|
+
/** Stream a single message (no session, stateless). */
|
|
26
|
+
stream(message: string, history?: Record<string, any>[]): AsyncGenerator<StreamChunk>;
|
|
27
|
+
/** Create a new session (persisted, maintains conversation history). */
|
|
28
|
+
createSession(): AgentSession;
|
|
29
|
+
/** Restore a previously saved session by ID. */
|
|
30
|
+
restoreSession(sessionId: string): AgentSession | null;
|
|
31
|
+
/** List saved session IDs. */
|
|
32
|
+
listSessions(): string[];
|
|
33
|
+
/** List saved session summaries. */
|
|
34
|
+
listSessionSummaries(limit?: number): Record<string, any>[];
|
|
35
|
+
/** Get the underlying LLM provider. */
|
|
36
|
+
get llm(): LLMProvider;
|
|
37
|
+
/** Get the list of registered tools. */
|
|
38
|
+
get tools(): ToolDefinition[];
|
|
39
|
+
private _wrapSession;
|
|
40
|
+
private _executeTool;
|
|
41
|
+
private _buildSystemForTurn;
|
|
42
|
+
private _runLoop;
|
|
43
|
+
private _runLoopStream;
|
|
44
|
+
}
|
|
45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