@slashfi/agents-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 +274 -0
- package/dist/auth.d.ts +109 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +329 -0
- package/dist/auth.js.map +1 -0
- package/dist/build.d.ts +68 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +159 -0
- package/dist/build.js.map +1 -0
- package/dist/define.d.ts +87 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +71 -0
- package/dist/define.js.map +1 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/registry.d.ts +48 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +274 -0
- package/dist/registry.js.map +1 -0
- package/dist/server.d.ts +66 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +308 -0
- package/dist/server.js.map +1 -0
- package/dist/types.d.ts +389 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +54 -0
- package/src/auth.ts +493 -0
- package/src/build.ts +238 -0
- package/src/define.ts +153 -0
- package/src/index.ts +111 -0
- package/src/registry.ts +403 -0
- package/src/server.ts +460 -0
- package/src/types.ts +524 -0
package/dist/auth.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth Agent
|
|
3
|
+
*
|
|
4
|
+
* Built-in agent that provides OAuth2 client_credentials authentication.
|
|
5
|
+
* Register it into any agent registry to enable auth.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Client credentials management (create, rotate, revoke)
|
|
9
|
+
* - OAuth2 client_credentials token exchange
|
|
10
|
+
* - JWT access tokens with scopes
|
|
11
|
+
* - Pluggable AuthStore interface (in-memory default)
|
|
12
|
+
* - Root key for admin operations
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { createAgentRegistry, createAgentServer, createAuthAgent } from '@slashfi/agents-sdk';
|
|
17
|
+
*
|
|
18
|
+
* const registry = createAgentRegistry();
|
|
19
|
+
* registry.register(createAuthAgent({ rootKey: process.env.ROOT_KEY }));
|
|
20
|
+
* registry.register(myAgent);
|
|
21
|
+
*
|
|
22
|
+
* const server = createAgentServer(registry, { port: 3000 });
|
|
23
|
+
* await server.start();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
import { defineAgent, defineTool } from "./define.js";
|
|
27
|
+
// ============================================
|
|
28
|
+
// In-Memory Auth Store
|
|
29
|
+
// ============================================
|
|
30
|
+
function generateId(prefix) {
|
|
31
|
+
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
32
|
+
let id = prefix;
|
|
33
|
+
for (let i = 0; i < 24; i++) {
|
|
34
|
+
id += chars[Math.floor(Math.random() * chars.length)];
|
|
35
|
+
}
|
|
36
|
+
return id;
|
|
37
|
+
}
|
|
38
|
+
function generateSecret() {
|
|
39
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
40
|
+
let secret = "sk_";
|
|
41
|
+
for (let i = 0; i < 40; i++) {
|
|
42
|
+
secret += chars[Math.floor(Math.random() * chars.length)];
|
|
43
|
+
}
|
|
44
|
+
return secret;
|
|
45
|
+
}
|
|
46
|
+
function generateToken() {
|
|
47
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
48
|
+
let token = "at_";
|
|
49
|
+
for (let i = 0; i < 48; i++) {
|
|
50
|
+
token += chars[Math.floor(Math.random() * chars.length)];
|
|
51
|
+
}
|
|
52
|
+
return token;
|
|
53
|
+
}
|
|
54
|
+
/** Simple hash for storing secrets (not for production - use bcrypt/argon2) */
|
|
55
|
+
async function hashSecret(secret) {
|
|
56
|
+
const encoder = new TextEncoder();
|
|
57
|
+
const data = encoder.encode(secret);
|
|
58
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
59
|
+
return Array.from(new Uint8Array(hash))
|
|
60
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
61
|
+
.join("");
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create an in-memory auth store.
|
|
65
|
+
* Suitable for development and testing. Use a persistent store for production.
|
|
66
|
+
*/
|
|
67
|
+
export function createMemoryAuthStore() {
|
|
68
|
+
const clients = new Map();
|
|
69
|
+
const tokens = new Map();
|
|
70
|
+
return {
|
|
71
|
+
async createClient(name, scopes, selfRegistered) {
|
|
72
|
+
const clientId = generateId("ag_");
|
|
73
|
+
const clientSecret = generateSecret();
|
|
74
|
+
const secretHash = await hashSecret(clientSecret);
|
|
75
|
+
clients.set(clientId, {
|
|
76
|
+
clientId,
|
|
77
|
+
clientSecretHash: secretHash,
|
|
78
|
+
name,
|
|
79
|
+
scopes,
|
|
80
|
+
createdAt: Date.now(),
|
|
81
|
+
selfRegistered,
|
|
82
|
+
});
|
|
83
|
+
return { clientId, clientSecret };
|
|
84
|
+
},
|
|
85
|
+
async validateClient(clientId, clientSecret) {
|
|
86
|
+
const client = clients.get(clientId);
|
|
87
|
+
if (!client)
|
|
88
|
+
return null;
|
|
89
|
+
const hash = await hashSecret(clientSecret);
|
|
90
|
+
return hash === client.clientSecretHash ? client : null;
|
|
91
|
+
},
|
|
92
|
+
async getClient(clientId) {
|
|
93
|
+
return clients.get(clientId) ?? null;
|
|
94
|
+
},
|
|
95
|
+
async listClients() {
|
|
96
|
+
return Array.from(clients.values());
|
|
97
|
+
},
|
|
98
|
+
async revokeClient(clientId) {
|
|
99
|
+
// Also revoke all tokens for this client
|
|
100
|
+
for (const [tokenStr, token] of tokens) {
|
|
101
|
+
if (token.clientId === clientId) {
|
|
102
|
+
tokens.delete(tokenStr);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return clients.delete(clientId);
|
|
106
|
+
},
|
|
107
|
+
async rotateSecret(clientId) {
|
|
108
|
+
const client = clients.get(clientId);
|
|
109
|
+
if (!client)
|
|
110
|
+
return null;
|
|
111
|
+
const clientSecret = generateSecret();
|
|
112
|
+
client.clientSecretHash = await hashSecret(clientSecret);
|
|
113
|
+
return { clientSecret };
|
|
114
|
+
},
|
|
115
|
+
async storeToken(token) {
|
|
116
|
+
tokens.set(token.token, token);
|
|
117
|
+
},
|
|
118
|
+
async validateToken(tokenString) {
|
|
119
|
+
const token = tokens.get(tokenString);
|
|
120
|
+
if (!token)
|
|
121
|
+
return null;
|
|
122
|
+
if (Date.now() > token.expiresAt) {
|
|
123
|
+
tokens.delete(tokenString);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
return token;
|
|
127
|
+
},
|
|
128
|
+
async revokeToken(tokenString) {
|
|
129
|
+
return tokens.delete(tokenString);
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// ============================================
|
|
134
|
+
// Create Auth Agent
|
|
135
|
+
// ============================================
|
|
136
|
+
/**
|
|
137
|
+
* Create the built-in `@auth` agent.
|
|
138
|
+
*
|
|
139
|
+
* Provides OAuth2 client_credentials authentication as agent tools.
|
|
140
|
+
* The server auto-detects this agent and wires up token validation.
|
|
141
|
+
*/
|
|
142
|
+
export function createAuthAgent(options) {
|
|
143
|
+
const { rootKey, allowRegistration = false, registrationScopes, tokenTtl = 3600, store = createMemoryAuthStore(), } = options;
|
|
144
|
+
// --- Public Tools ---
|
|
145
|
+
const tokenTool = defineTool({
|
|
146
|
+
name: "token",
|
|
147
|
+
description: "Exchange client credentials for an access token (OAuth2 client_credentials grant)",
|
|
148
|
+
visibility: "public",
|
|
149
|
+
inputSchema: {
|
|
150
|
+
type: "object",
|
|
151
|
+
properties: {
|
|
152
|
+
grantType: {
|
|
153
|
+
type: "string",
|
|
154
|
+
enum: ["client_credentials"],
|
|
155
|
+
description: "Grant type. Must be 'client_credentials'.",
|
|
156
|
+
},
|
|
157
|
+
clientId: { type: "string", description: "Client ID" },
|
|
158
|
+
clientSecret: { type: "string", description: "Client secret" },
|
|
159
|
+
},
|
|
160
|
+
required: ["grantType", "clientId", "clientSecret"],
|
|
161
|
+
},
|
|
162
|
+
execute: async (input) => {
|
|
163
|
+
if (input.grantType !== "client_credentials") {
|
|
164
|
+
throw new Error("Unsupported grant type. Use 'client_credentials'.");
|
|
165
|
+
}
|
|
166
|
+
const client = await store.validateClient(input.clientId, input.clientSecret);
|
|
167
|
+
if (!client) {
|
|
168
|
+
throw new Error("Invalid client credentials");
|
|
169
|
+
}
|
|
170
|
+
const token = {
|
|
171
|
+
token: generateToken(),
|
|
172
|
+
clientId: client.clientId,
|
|
173
|
+
scopes: client.scopes,
|
|
174
|
+
issuedAt: Date.now(),
|
|
175
|
+
expiresAt: Date.now() + tokenTtl * 1000,
|
|
176
|
+
};
|
|
177
|
+
await store.storeToken(token);
|
|
178
|
+
return {
|
|
179
|
+
accessToken: token.token,
|
|
180
|
+
tokenType: "bearer",
|
|
181
|
+
expiresIn: tokenTtl,
|
|
182
|
+
scopes: client.scopes,
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
const whoamiTool = defineTool({
|
|
187
|
+
name: "whoami",
|
|
188
|
+
description: "Introspect the current authentication context",
|
|
189
|
+
visibility: "public",
|
|
190
|
+
inputSchema: { type: "object", properties: {} },
|
|
191
|
+
execute: async (_input, ctx) => {
|
|
192
|
+
return {
|
|
193
|
+
callerId: ctx.callerId,
|
|
194
|
+
callerType: ctx.callerType,
|
|
195
|
+
scopes: ctx.scopes ?? [],
|
|
196
|
+
isRoot: ctx.callerId === "root",
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
// --- Optional Public Tool ---
|
|
201
|
+
const registerTool = defineTool({
|
|
202
|
+
name: "register",
|
|
203
|
+
description: "Register a new agent client (self-service)",
|
|
204
|
+
visibility: "public",
|
|
205
|
+
inputSchema: {
|
|
206
|
+
type: "object",
|
|
207
|
+
properties: {
|
|
208
|
+
name: { type: "string", description: "Name for this client" },
|
|
209
|
+
scopes: {
|
|
210
|
+
type: "array",
|
|
211
|
+
items: { type: "string" },
|
|
212
|
+
description: "Requested scopes",
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
required: ["name"],
|
|
216
|
+
},
|
|
217
|
+
execute: async (input) => {
|
|
218
|
+
let scopes = input.scopes ?? [];
|
|
219
|
+
// If registration scopes are restricted, filter
|
|
220
|
+
if (registrationScopes && registrationScopes.length > 0) {
|
|
221
|
+
scopes = scopes.filter((s) => registrationScopes.includes(s));
|
|
222
|
+
}
|
|
223
|
+
const { clientId, clientSecret } = await store.createClient(input.name, scopes, true);
|
|
224
|
+
return { clientId, clientSecret, scopes };
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
// --- Private Tools (root key only) ---
|
|
228
|
+
const createClientTool = defineTool({
|
|
229
|
+
name: "create_client",
|
|
230
|
+
description: "Create a new client with specific scopes (admin only)",
|
|
231
|
+
visibility: "private",
|
|
232
|
+
inputSchema: {
|
|
233
|
+
type: "object",
|
|
234
|
+
properties: {
|
|
235
|
+
name: { type: "string", description: "Client name" },
|
|
236
|
+
scopes: {
|
|
237
|
+
type: "array",
|
|
238
|
+
items: { type: "string" },
|
|
239
|
+
description: "Scopes to grant",
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
required: ["name", "scopes"],
|
|
243
|
+
},
|
|
244
|
+
execute: async (input) => {
|
|
245
|
+
const { clientId, clientSecret } = await store.createClient(input.name, input.scopes);
|
|
246
|
+
return { clientId, clientSecret, scopes: input.scopes };
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
const listClientsTool = defineTool({
|
|
250
|
+
name: "list_clients",
|
|
251
|
+
description: "List all registered clients (admin only)",
|
|
252
|
+
visibility: "private",
|
|
253
|
+
inputSchema: { type: "object", properties: {} },
|
|
254
|
+
execute: async () => {
|
|
255
|
+
const clients = await store.listClients();
|
|
256
|
+
return {
|
|
257
|
+
clients: clients.map((c) => ({
|
|
258
|
+
clientId: c.clientId,
|
|
259
|
+
name: c.name,
|
|
260
|
+
scopes: c.scopes,
|
|
261
|
+
createdAt: c.createdAt,
|
|
262
|
+
selfRegistered: c.selfRegistered ?? false,
|
|
263
|
+
})),
|
|
264
|
+
};
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
const revokeClientTool = defineTool({
|
|
268
|
+
name: "revoke_client",
|
|
269
|
+
description: "Revoke a client and all its tokens (admin only)",
|
|
270
|
+
visibility: "private",
|
|
271
|
+
inputSchema: {
|
|
272
|
+
type: "object",
|
|
273
|
+
properties: {
|
|
274
|
+
clientId: { type: "string", description: "Client ID to revoke" },
|
|
275
|
+
},
|
|
276
|
+
required: ["clientId"],
|
|
277
|
+
},
|
|
278
|
+
execute: async (input) => {
|
|
279
|
+
const revoked = await store.revokeClient(input.clientId);
|
|
280
|
+
return { revoked };
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
const rotateSecretTool = defineTool({
|
|
284
|
+
name: "rotate_secret",
|
|
285
|
+
description: "Rotate a client's secret (admin only)",
|
|
286
|
+
visibility: "private",
|
|
287
|
+
inputSchema: {
|
|
288
|
+
type: "object",
|
|
289
|
+
properties: {
|
|
290
|
+
clientId: { type: "string", description: "Client ID to rotate" },
|
|
291
|
+
},
|
|
292
|
+
required: ["clientId"],
|
|
293
|
+
},
|
|
294
|
+
execute: async (input) => {
|
|
295
|
+
const result = await store.rotateSecret(input.clientId);
|
|
296
|
+
if (!result)
|
|
297
|
+
throw new Error(`Client not found: ${input.clientId}`);
|
|
298
|
+
return { clientId: input.clientId, clientSecret: result.clientSecret };
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
// --- Assemble tools ---
|
|
302
|
+
const tools = [
|
|
303
|
+
tokenTool,
|
|
304
|
+
whoamiTool,
|
|
305
|
+
...(allowRegistration ? [registerTool] : []),
|
|
306
|
+
createClientTool,
|
|
307
|
+
listClientsTool,
|
|
308
|
+
revokeClientTool,
|
|
309
|
+
rotateSecretTool,
|
|
310
|
+
];
|
|
311
|
+
const agent = defineAgent({
|
|
312
|
+
path: "@auth",
|
|
313
|
+
entrypoint: "Authentication agent. Provides OAuth2 client_credentials authentication for the agent network.",
|
|
314
|
+
config: {
|
|
315
|
+
name: "Auth",
|
|
316
|
+
description: "Built-in authentication agent",
|
|
317
|
+
supportedActions: ["execute_tool", "describe_tools", "load"],
|
|
318
|
+
},
|
|
319
|
+
tools: tools,
|
|
320
|
+
visibility: "public",
|
|
321
|
+
});
|
|
322
|
+
// Attach store and config for server integration
|
|
323
|
+
return Object.assign(agent, {
|
|
324
|
+
__authStore: store,
|
|
325
|
+
__rootKey: rootKey,
|
|
326
|
+
__tokenTtl: tokenTtl,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA+EtD,+CAA+C;AAC/C,uBAAuB;AACvB,+CAA+C;AAE/C,SAAS,UAAU,CAAC,MAAc;IAChC,MAAM,KAAK,GAAG,sCAAsC,CAAC;IACrD,IAAI,EAAE,GAAG,MAAM,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5B,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACxD,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,C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AAW,EAAE,KAAK;QAClB,SAAS,EAAE,OAAO;QAClB,UAAU,EAAE,QAAQ;KACrB,CAAC,CAAC;AACL,CAAC"}
|
package/dist/build.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build Agents
|
|
3
|
+
*
|
|
4
|
+
* Scans a directory for agent definitions and generates a registry file.
|
|
5
|
+
*
|
|
6
|
+
* Convention:
|
|
7
|
+
* - Agent directories start with `@` (e.g., `@my-agent`)
|
|
8
|
+
* - Each agent has:
|
|
9
|
+
* - `entrypoint.md` - System prompt
|
|
10
|
+
* - `agent.config.ts` - Configuration (exports default AgentConfig)
|
|
11
|
+
* - `*.tool.ts` - Tool definitions (exports `{toolName}Tool`)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // scripts/build-agents.ts
|
|
16
|
+
* import { buildAgents } from '@slashfi/agents-sdk';
|
|
17
|
+
*
|
|
18
|
+
* await buildAgents({
|
|
19
|
+
* agentsDir: './src/agents',
|
|
20
|
+
* outFile: './src/agents/_generated-registry.ts',
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Options for building agents.
|
|
26
|
+
*/
|
|
27
|
+
export interface BuildAgentsOptions {
|
|
28
|
+
/** Directory containing agent folders (e.g., './src/agents') */
|
|
29
|
+
agentsDir: string;
|
|
30
|
+
/** Output file path for the generated registry */
|
|
31
|
+
outFile: string;
|
|
32
|
+
/**
|
|
33
|
+
* Import path for the SDK.
|
|
34
|
+
* @default '@slashfi/agents-sdk'
|
|
35
|
+
*/
|
|
36
|
+
sdkImport?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Default visibility for agents.
|
|
39
|
+
* @default 'internal'
|
|
40
|
+
*/
|
|
41
|
+
defaultVisibility?: "public" | "internal" | "private";
|
|
42
|
+
/**
|
|
43
|
+
* Whether to use double quotes for strings.
|
|
44
|
+
* @default false (single quotes)
|
|
45
|
+
*/
|
|
46
|
+
doubleQuotes?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Result of building agents.
|
|
50
|
+
*/
|
|
51
|
+
export interface BuildAgentsResult {
|
|
52
|
+
/** Number of agents processed */
|
|
53
|
+
agentCount: number;
|
|
54
|
+
/** Paths of agents that were processed */
|
|
55
|
+
agents: string[];
|
|
56
|
+
/** Agents that were skipped (missing required files) */
|
|
57
|
+
skipped: string[];
|
|
58
|
+
/** Path to the generated output file */
|
|
59
|
+
outFile: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Build agents from a directory.
|
|
63
|
+
*
|
|
64
|
+
* Scans the given directory for agent folders (starting with `@`) and generates
|
|
65
|
+
* a TypeScript file that creates and populates an agent registry.
|
|
66
|
+
*/
|
|
67
|
+
export declare function buildAgents(options: BuildAgentsOptions): Promise<BuildAgentsResult>;
|
|
68
|
+
//# sourceMappingURL=build.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAKH;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAElB,kDAAkD;IAClD,OAAO,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,QAAQ,GAAG,UAAU,GAAG,SAAS,CAAC;IAEtD;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,iCAAiC;IACjC,UAAU,EAAE,MAAM,CAAC;IAEnB,0CAA0C;IAC1C,MAAM,EAAE,MAAM,EAAE,CAAC;IAEjB,wDAAwD;IACxD,OAAO,EAAE,MAAM,EAAE,CAAC;IAElB,wCAAwC;IACxC,OAAO,EAAE,MAAM,CAAC;CACjB;AA0BD;;;;;GAKG;AACH,wBAAsB,WAAW,CAC/B,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,iBAAiB,CAAC,CAoI5B"}
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build Agents
|
|
3
|
+
*
|
|
4
|
+
* Scans a directory for agent definitions and generates a registry file.
|
|
5
|
+
*
|
|
6
|
+
* Convention:
|
|
7
|
+
* - Agent directories start with `@` (e.g., `@my-agent`)
|
|
8
|
+
* - Each agent has:
|
|
9
|
+
* - `entrypoint.md` - System prompt
|
|
10
|
+
* - `agent.config.ts` - Configuration (exports default AgentConfig)
|
|
11
|
+
* - `*.tool.ts` - Tool definitions (exports `{toolName}Tool`)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // scripts/build-agents.ts
|
|
16
|
+
* import { buildAgents } from '@slashfi/agents-sdk';
|
|
17
|
+
*
|
|
18
|
+
* await buildAgents({
|
|
19
|
+
* agentsDir: './src/agents',
|
|
20
|
+
* outFile: './src/agents/_generated-registry.ts',
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
import { readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
25
|
+
import { join } from "node:path";
|
|
26
|
+
/**
|
|
27
|
+
* Convert kebab-case to camelCase.
|
|
28
|
+
*/
|
|
29
|
+
function toCamelCase(str) {
|
|
30
|
+
return str
|
|
31
|
+
.split("-")
|
|
32
|
+
.map((part, i) => i === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1))
|
|
33
|
+
.join("");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Escape a string for use in a JavaScript string literal.
|
|
37
|
+
*/
|
|
38
|
+
function escapeString(str, quote) {
|
|
39
|
+
return str
|
|
40
|
+
.replace(/\\/g, "\\\\")
|
|
41
|
+
.replace(new RegExp(quote, "g"), `\\${quote}`)
|
|
42
|
+
.replace(/\n/g, "\\n")
|
|
43
|
+
.replace(/\r/g, "\\r")
|
|
44
|
+
.replace(/\t/g, "\\t");
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Build agents from a directory.
|
|
48
|
+
*
|
|
49
|
+
* Scans the given directory for agent folders (starting with `@`) and generates
|
|
50
|
+
* a TypeScript file that creates and populates an agent registry.
|
|
51
|
+
*/
|
|
52
|
+
export async function buildAgents(options) {
|
|
53
|
+
const { agentsDir, outFile, sdkImport = "@slashfi/agents-sdk", defaultVisibility = "internal", doubleQuotes = false, } = options;
|
|
54
|
+
const q = doubleQuotes ? '"' : "'";
|
|
55
|
+
const agents = [];
|
|
56
|
+
const skipped = [];
|
|
57
|
+
// Find all agent directories (start with @)
|
|
58
|
+
const agentDirs = readdirSync(agentsDir).filter((name) => {
|
|
59
|
+
if (!name.startsWith("@"))
|
|
60
|
+
return false;
|
|
61
|
+
try {
|
|
62
|
+
const stat = statSync(join(agentsDir, name));
|
|
63
|
+
return stat.isDirectory();
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
const imports = [];
|
|
70
|
+
const registrations = [];
|
|
71
|
+
const exportItems = [];
|
|
72
|
+
for (const agentDir of agentDirs) {
|
|
73
|
+
const agentPath = join(agentsDir, agentDir);
|
|
74
|
+
const entrypointPath = join(agentPath, "entrypoint.md");
|
|
75
|
+
const configPath = join(agentPath, "agent.config.ts");
|
|
76
|
+
// Check if required files exist
|
|
77
|
+
try {
|
|
78
|
+
statSync(entrypointPath);
|
|
79
|
+
statSync(configPath);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
skipped.push(agentDir);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
// Read entrypoint content
|
|
86
|
+
const entrypoint = readFileSync(entrypointPath, "utf-8");
|
|
87
|
+
// Find tool files
|
|
88
|
+
const toolFiles = readdirSync(agentPath).filter((f) => f.endsWith(".tool.ts"));
|
|
89
|
+
// Generate variable name from directory (e.g., @product-applications -> productApplications)
|
|
90
|
+
const varName = toCamelCase(agentDir.slice(1));
|
|
91
|
+
// Add config import
|
|
92
|
+
imports.push(`import ${varName}Config from ${q}./${agentDir}/agent.config.js${q};`);
|
|
93
|
+
// Add tool imports and collect tool info
|
|
94
|
+
const toolEntries = [];
|
|
95
|
+
for (const toolFile of toolFiles) {
|
|
96
|
+
const baseName = toolFile.replace(".tool.ts", "");
|
|
97
|
+
// Convert kebab-case to camelCase for JS variable: get-status -> getStatusTool
|
|
98
|
+
const toolVarName = `${toCamelCase(baseName)}Tool`;
|
|
99
|
+
// Tool name is just the filename (kebab-case): get-status
|
|
100
|
+
const toolName = baseName;
|
|
101
|
+
imports.push(`import { ${toolVarName} } from ${q}./${agentDir}/${toolFile.replace(".ts", ".js")}${q};`);
|
|
102
|
+
toolEntries.push({ varName: toolVarName, name: toolName });
|
|
103
|
+
}
|
|
104
|
+
// Read config to get the path override if specified
|
|
105
|
+
const configContent = readFileSync(configPath, "utf-8");
|
|
106
|
+
const pathMatch = configContent.match(/path:\s*['"]([^'"]+)['"]/);
|
|
107
|
+
const registeredPath = pathMatch ? pathMatch[1] : agentDir;
|
|
108
|
+
// Generate tools array with name override
|
|
109
|
+
const toolsArray = toolEntries
|
|
110
|
+
.map((t) => `{ ...${t.varName}, name: ${q}${t.name}${q} }`)
|
|
111
|
+
.join(", ");
|
|
112
|
+
// Generate agent definition
|
|
113
|
+
const entrypointEscaped = escapeString(entrypoint, q);
|
|
114
|
+
registrations.push(`
|
|
115
|
+
const ${varName}Agent = defineAgent({
|
|
116
|
+
path: ${q}${registeredPath}${q},
|
|
117
|
+
entrypoint: ${q}${entrypointEscaped}${q},
|
|
118
|
+
config: ${varName}Config,
|
|
119
|
+
tools: [${toolsArray}],
|
|
120
|
+
visibility: ${q}${defaultVisibility}${q},
|
|
121
|
+
});
|
|
122
|
+
agentRegistry.register(${varName}Agent);`);
|
|
123
|
+
exportItems.push(` ${varName}: ${varName}Agent,`);
|
|
124
|
+
agents.push(registeredPath);
|
|
125
|
+
}
|
|
126
|
+
const output = `/**
|
|
127
|
+
* AUTO-GENERATED FILE - DO NOT EDIT
|
|
128
|
+
* Generated by buildAgents from ${sdkImport}
|
|
129
|
+
*
|
|
130
|
+
* This file bundles agent entrypoints at build time.
|
|
131
|
+
* Tool names are derived from filenames (kebab-case).
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
import { createAgentRegistry, defineAgent } from ${q}${sdkImport}${q};
|
|
135
|
+
|
|
136
|
+
${imports.join("\n")}
|
|
137
|
+
|
|
138
|
+
// Create registry
|
|
139
|
+
export const agentRegistry = createAgentRegistry({
|
|
140
|
+
defaultVisibility: ${q}${defaultVisibility}${q},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Register agents
|
|
144
|
+
${registrations.join("\n")}
|
|
145
|
+
|
|
146
|
+
// Export agents
|
|
147
|
+
export const agents = {
|
|
148
|
+
${exportItems.join("\n")}
|
|
149
|
+
};
|
|
150
|
+
`;
|
|
151
|
+
writeFileSync(outFile, output);
|
|
152
|
+
return {
|
|
153
|
+
agentCount: agents.length,
|
|
154
|
+
agents,
|
|
155
|
+
skipped,
|
|
156
|
+
outFile,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=build.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAgDjC;;GAEG;AACH,SAAS,WAAW,CAAC,GAAW;IAC9B,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G,iBAAiB,GAAG,CAAC;YAC7B,OAAO;YACP,UAAU;gBACN,CAAC,GAAG,iBAAiB,GAAG,CAAC;;yBAEhB,OAAO,SAAS,CAAC,CAAC;QAEvC,WAAW,CAAC,IAAI,CAAC,KAAK,OAAO,KAAK,OAAO,QAAQ,CAAC,CAAC;QACnD,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAC9B,CAAC;IAED,MAAM,MAAM,GAAG;;mCAEkB,SAAS;;;;;;mDAMO,CAAC,GAAG,SAAS,GAAG,CAAC;;EAElE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;;;;uBAIG,CAAC,GAAG,iBAAiB,GAAG,CAAC;;;;EAI9C,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC;;;;EAIxB,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC;;CAEvB,CAAC;IAEA,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAE/B,OAAO;QACL,UAAU,EAAE,MAAM,CAAC,MAAM;QACzB,MAAM;QACN,OAAO;QACP,OAAO;KACR,CAAC;AACJ,CAAC"}
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Define Agent and Tool Functions
|
|
3
|
+
*
|
|
4
|
+
* Factory functions for creating agent and tool definitions.
|
|
5
|
+
*/
|
|
6
|
+
import type { AgentConfig, AgentDefinition, AgentRuntime, JsonSchema, ToolContext, ToolDefinition, Visibility } from "./types.js";
|
|
7
|
+
export interface DefineToolOptions<TContext extends ToolContext = ToolContext, TInput = unknown, TOutput = unknown> {
|
|
8
|
+
/** Tool name (unique within agent) */
|
|
9
|
+
name: string;
|
|
10
|
+
/** Short description for tool discovery */
|
|
11
|
+
description: string;
|
|
12
|
+
/** JSON Schema for input parameters */
|
|
13
|
+
inputSchema: JsonSchema;
|
|
14
|
+
/** JSON Schema for output (optional) */
|
|
15
|
+
outputSchema?: JsonSchema;
|
|
16
|
+
/** Visibility level */
|
|
17
|
+
visibility?: Visibility;
|
|
18
|
+
/** Explicit allowed callers */
|
|
19
|
+
allowedCallers?: string[];
|
|
20
|
+
/** Execute function */
|
|
21
|
+
execute: (input: TInput, ctx: TContext) => Promise<TOutput>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create a tool definition.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const greet = defineTool({
|
|
29
|
+
* name: 'greet',
|
|
30
|
+
* description: 'Greet a user',
|
|
31
|
+
* inputSchema: {
|
|
32
|
+
* type: 'object',
|
|
33
|
+
* properties: {
|
|
34
|
+
* name: { type: 'string', description: 'Name to greet' }
|
|
35
|
+
* },
|
|
36
|
+
* required: ['name']
|
|
37
|
+
* },
|
|
38
|
+
* execute: async (input) => ({ message: `Hello, ${input.name}!` })
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function defineTool<TContext extends ToolContext = ToolContext, TInput = unknown, TOutput = unknown>(options: DefineToolOptions<TContext, TInput, TOutput>): ToolDefinition<TContext, TInput, TOutput>;
|
|
43
|
+
export interface DefineAgentOptions<TContext extends ToolContext = ToolContext> {
|
|
44
|
+
/** Agent path (e.g., '@example', '/agents/my-agent') */
|
|
45
|
+
path: string;
|
|
46
|
+
/** System prompt / entrypoint content */
|
|
47
|
+
entrypoint: string;
|
|
48
|
+
/** Agent configuration */
|
|
49
|
+
config?: AgentConfig;
|
|
50
|
+
/** Tools provided by this agent */
|
|
51
|
+
tools?: ToolDefinition<TContext, unknown, unknown>[];
|
|
52
|
+
/**
|
|
53
|
+
* Runtime hooks factory.
|
|
54
|
+
* Called once to create the runtime for this agent.
|
|
55
|
+
*/
|
|
56
|
+
runtime?: () => AgentRuntime;
|
|
57
|
+
/** Visibility level */
|
|
58
|
+
visibility?: Visibility;
|
|
59
|
+
/** Explicit allowed callers */
|
|
60
|
+
allowedCallers?: string[];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create an agent definition.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const agent = defineAgent({
|
|
68
|
+
* path: '@my-agent',
|
|
69
|
+
* entrypoint: 'You are a helpful assistant.',
|
|
70
|
+
* config: {
|
|
71
|
+
* name: 'My Agent',
|
|
72
|
+
* description: 'A helpful agent'
|
|
73
|
+
* },
|
|
74
|
+
* tools: [greet, echo],
|
|
75
|
+
* runtime: () => ({
|
|
76
|
+
* onInvoke: async (ctx) => {
|
|
77
|
+
* console.log(`Invoked with: ${ctx.prompt}`);
|
|
78
|
+
* },
|
|
79
|
+
* onTick: async (ctx) => {
|
|
80
|
+
* console.log(`Tick at ${ctx.timestamp}`);
|
|
81
|
+
* }
|
|
82
|
+
* })
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function defineAgent<TContext extends ToolContext = ToolContext>(options: DefineAgentOptions<TContext>): AgentDefinition<TContext>;
|
|
87
|
+
//# sourceMappingURL=define.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,YAAY,EACZ,UAAU,EACV,WAAW,EACX,cAAc,EACd,UAAU,EACX,MAAM,YAAY,CAAC;AAMpB,MAAM,WAAW,iBAAiB,CAChC,QAAQ,SAAS,WAAW,GAAG,WAAW,EAC1C,MAAM,GAAG,OAAO,EAChB,OAAO,GAAG,OAAO;IAEjB,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;IAEb,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IAEpB,uCAAuC;IACvC,WAAW,EAAE,UAAU,CAAC;IAExB,wCAAwC;IACxC,YAAY,CAAC,EAAE,UAAU,CAAC;IAE1B,uBAAuB;IACvB,UAAU,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