@openserv-labs/sdk 2.0.1 → 2.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/dist/agent.d.ts +41 -16
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +90 -64
- package/package.json +1 -1
package/dist/agent.d.ts
CHANGED
|
@@ -19,6 +19,12 @@ export interface AgentOptions<T extends string> {
|
|
|
19
19
|
* Can also be provided via OPENSERV_API_KEY environment variable.
|
|
20
20
|
*/
|
|
21
21
|
apiKey?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The auth token for securing agent requests.
|
|
24
|
+
* Can also be provided via OPENSERV_AUTH_TOKEN environment variable.
|
|
25
|
+
* Generated by provision() and used to validate incoming requests.
|
|
26
|
+
*/
|
|
27
|
+
authToken?: string;
|
|
22
28
|
/**
|
|
23
29
|
* The system prompt that defines the agent's behavior and context.
|
|
24
30
|
* Used as the initial system message in OpenAI chat completions.
|
|
@@ -82,25 +88,46 @@ export declare class Agent<M extends string = string> {
|
|
|
82
88
|
protected tools: Array<Capability<M, z.ZodTypeAny>>;
|
|
83
89
|
/**
|
|
84
90
|
* The OpenServ API key used for authentication.
|
|
85
|
-
*
|
|
86
|
-
* @private
|
|
91
|
+
* Set via setCredentials() or resolved from options/env in start().
|
|
87
92
|
*/
|
|
88
|
-
|
|
93
|
+
apiKey?: string;
|
|
89
94
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
|
|
95
|
+
* The auth token used to validate incoming requests.
|
|
96
|
+
* Set via setCredentials() or resolved from options/env in start().
|
|
97
|
+
*/
|
|
98
|
+
authToken?: string;
|
|
99
|
+
/**
|
|
100
|
+
* Set credentials for this agent instance.
|
|
101
|
+
* This is typically called by provision() to bind the agent to its platform credentials.
|
|
102
|
+
*
|
|
103
|
+
* @param credentials - The credentials to set
|
|
104
|
+
* @param credentials.apiKey - The OpenServ API key for this agent
|
|
105
|
+
* @param credentials.authToken - Optional auth token for securing agent requests
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* // Typically called by provision() automatically:
|
|
110
|
+
* await provision({
|
|
111
|
+
* agent: { instance: myAgent, name: 'my-agent', ... }
|
|
112
|
+
* })
|
|
113
|
+
*
|
|
114
|
+
* // Or manually:
|
|
115
|
+
* agent.setCredentials({ apiKey: 'key', authToken: 'token' })
|
|
116
|
+
* ```
|
|
93
117
|
*/
|
|
94
|
-
|
|
118
|
+
setCredentials(credentials: {
|
|
119
|
+
apiKey: string;
|
|
120
|
+
authToken?: string;
|
|
121
|
+
}): void;
|
|
95
122
|
/**
|
|
96
123
|
* Axios instance for making requests to the OpenServ API.
|
|
97
|
-
*
|
|
124
|
+
* Initialized in start() after credentials are resolved.
|
|
98
125
|
* @private
|
|
99
126
|
*/
|
|
100
127
|
private apiClient;
|
|
101
128
|
/**
|
|
102
129
|
* Axios instance for making requests to the OpenServ Runtime API.
|
|
103
|
-
*
|
|
130
|
+
* Initialized in start() after credentials are resolved.
|
|
104
131
|
* @protected
|
|
105
132
|
*/
|
|
106
133
|
protected runtimeClient: AxiosInstance;
|
|
@@ -132,11 +159,9 @@ export declare class Agent<M extends string = string> {
|
|
|
132
159
|
private get openai();
|
|
133
160
|
/**
|
|
134
161
|
* Creates a new Agent instance.
|
|
135
|
-
*
|
|
136
|
-
* Initializes API clients with appropriate authentication.
|
|
162
|
+
* The agent is configured but not started until start() is called.
|
|
137
163
|
*
|
|
138
164
|
* @param {AgentOptions} options - Configuration options for the agent
|
|
139
|
-
* @throws {Error} If OpenServ API key is not provided in options or environment
|
|
140
165
|
*/
|
|
141
166
|
constructor(options: AgentOptions<M>);
|
|
142
167
|
private initializeMCPClients;
|
|
@@ -404,17 +429,17 @@ export declare class Agent<M extends string = string> {
|
|
|
404
429
|
body: unknown;
|
|
405
430
|
}): Promise<void>;
|
|
406
431
|
/**
|
|
407
|
-
*
|
|
408
|
-
*
|
|
432
|
+
* Defines routes on the router (called in constructor).
|
|
433
|
+
* Routes are attached to the app in start() after middleware is configured.
|
|
409
434
|
* @private
|
|
410
435
|
*/
|
|
411
|
-
private
|
|
436
|
+
private defineRoutes;
|
|
412
437
|
/**
|
|
413
438
|
* Starts the agent's HTTP server.
|
|
414
439
|
* If the preferred port is unavailable, it will find an open port.
|
|
415
440
|
*
|
|
416
441
|
* @returns {Promise<void>} Resolves when the server has started
|
|
417
|
-
* @throws {Error} If server fails to start
|
|
442
|
+
* @throws {Error} If server fails to start or if API key is missing
|
|
418
443
|
*/
|
|
419
444
|
start(): Promise<void>;
|
|
420
445
|
/**
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,KAAK,aAAa,EAAE,MAAM,OAAO,CAAA;AAUjD,OAAO,KAAK,EACV,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,uBAAuB,EACvB,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EAClB,4BAA4B,EAC5B,sBAAsB,EACtB,aAAa,EACb,sBAAsB,EACtB,qBAAqB,EACrB,yBAAyB,EACzB,gBAAgB,EAEhB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,qBAAqB,EACrB,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAOlB,YAAY,EACZ,kBAAkB,EAClB,8BAA8B,EAC/B,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EACV,0BAA0B,EAE1B,cAAc,EACf,MAAM,mCAAmC,CAAA;AAI1C,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACz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AAQ,EAAE,MAAM,CAAA;SAAE,CAAA;QAC5B,IAAI,EAAE;YACJ,IAAI,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,CAAA;YAC5B,MAAM,CAAC,EAAE,YAAY,CAAA;YACrB,QAAQ,CAAC,EAAE,0BAA0B,EAAE,CAAA;SACxC,CAAA;KACF;;;IAyBD;;;;;;;OAOG;IACG,eAAe,CAAC,GAAG,EAAE;QAAE,IAAI,EAAE,OAAO,CAAA;KAAE;IAgB5C;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAqBpB;;;;;;OAMG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAsJ5B;;;;OAIG;IACG,IAAI;IAQV;;;OAGG;IACH,OAAO,CAAC,WAAW;IAOnB;;;;;;;;;;;;;OAaG;IACG,eAAe,CAAC,WAAW,EAAE,sBAAsB;IASzD;;;;;;;;OAQG;IACH,OAAO,CAAC,yBAAyB;CAsClC"}
|
package/dist/agent.js
CHANGED
|
@@ -21,20 +21,6 @@ const mcp_1 = require("./mcp");
|
|
|
21
21
|
const PLATFORM_URL = process.env.OPENSERV_API_URL || 'https://api.openserv.ai';
|
|
22
22
|
const RUNTIME_URL = process.env.OPENSERV_RUNTIME_URL || 'https://agents.openserv.ai';
|
|
23
23
|
const DEFAULT_PORT = Number.parseInt(process.env.PORT || '') || 7378;
|
|
24
|
-
const AUTH_TOKEN = process.env.OPENSERV_AUTH_TOKEN;
|
|
25
|
-
const authTokenMiddleware = async (req, res, next) => {
|
|
26
|
-
const tokenHash = req.headers['x-openserv-auth-token'];
|
|
27
|
-
if (!tokenHash || typeof tokenHash !== 'string') {
|
|
28
|
-
res.status(401).json({ error: 'Unauthorized' });
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
const isTokenValid = await bcryptjs_1.default.compare(AUTH_TOKEN, tokenHash);
|
|
32
|
-
if (!isTokenValid) {
|
|
33
|
-
res.status(401).json({ error: 'Unauthorized' });
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
next();
|
|
37
|
-
};
|
|
38
24
|
class Agent {
|
|
39
25
|
options;
|
|
40
26
|
/**
|
|
@@ -75,27 +61,46 @@ class Agent {
|
|
|
75
61
|
tools = [];
|
|
76
62
|
/**
|
|
77
63
|
* The OpenServ API key used for authentication.
|
|
78
|
-
*
|
|
79
|
-
|
|
64
|
+
* Set via setCredentials() or resolved from options/env in start().
|
|
65
|
+
*/
|
|
66
|
+
apiKey;
|
|
67
|
+
/**
|
|
68
|
+
* The auth token used to validate incoming requests.
|
|
69
|
+
* Set via setCredentials() or resolved from options/env in start().
|
|
80
70
|
*/
|
|
81
|
-
|
|
71
|
+
authToken;
|
|
82
72
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
73
|
+
* Set credentials for this agent instance.
|
|
74
|
+
* This is typically called by provision() to bind the agent to its platform credentials.
|
|
75
|
+
*
|
|
76
|
+
* @param credentials - The credentials to set
|
|
77
|
+
* @param credentials.apiKey - The OpenServ API key for this agent
|
|
78
|
+
* @param credentials.authToken - Optional auth token for securing agent requests
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // Typically called by provision() automatically:
|
|
83
|
+
* await provision({
|
|
84
|
+
* agent: { instance: myAgent, name: 'my-agent', ... }
|
|
85
|
+
* })
|
|
86
|
+
*
|
|
87
|
+
* // Or manually:
|
|
88
|
+
* agent.setCredentials({ apiKey: 'key', authToken: 'token' })
|
|
89
|
+
* ```
|
|
86
90
|
*/
|
|
87
|
-
|
|
88
|
-
|
|
91
|
+
setCredentials(credentials) {
|
|
92
|
+
this.apiKey = credentials.apiKey;
|
|
93
|
+
this.authToken = credentials.authToken;
|
|
89
94
|
}
|
|
90
95
|
/**
|
|
91
96
|
* Axios instance for making requests to the OpenServ API.
|
|
92
|
-
*
|
|
97
|
+
* Initialized in start() after credentials are resolved.
|
|
93
98
|
* @private
|
|
94
99
|
*/
|
|
95
100
|
apiClient;
|
|
96
101
|
/**
|
|
97
102
|
* Axios instance for making requests to the OpenServ Runtime API.
|
|
98
|
-
*
|
|
103
|
+
* Initialized in start() after credentials are resolved.
|
|
99
104
|
* @protected
|
|
100
105
|
*/
|
|
101
106
|
runtimeClient;
|
|
@@ -145,11 +150,9 @@ class Agent {
|
|
|
145
150
|
}
|
|
146
151
|
/**
|
|
147
152
|
* Creates a new Agent instance.
|
|
148
|
-
*
|
|
149
|
-
* Initializes API clients with appropriate authentication.
|
|
153
|
+
* The agent is configured but not started until start() is called.
|
|
150
154
|
*
|
|
151
155
|
* @param {AgentOptions} options - Configuration options for the agent
|
|
152
|
-
* @throws {Error} If OpenServ API key is not provided in options or environment
|
|
153
156
|
*/
|
|
154
157
|
constructor(options) {
|
|
155
158
|
this.options = options;
|
|
@@ -157,39 +160,10 @@ class Agent {
|
|
|
157
160
|
this.router = (0, express_async_router_1.AsyncRouter)();
|
|
158
161
|
this.port = this.options.port || DEFAULT_PORT;
|
|
159
162
|
this.systemPrompt = this.options.systemPrompt;
|
|
160
|
-
|
|
161
|
-
if (!this._apiKey) {
|
|
162
|
-
throw new Error('OpenServ API key is required. Please provide it in options or set OPENSERV_API_KEY environment variable.');
|
|
163
|
-
}
|
|
164
|
-
// Initialize API client
|
|
165
|
-
this.apiClient = axios_1.default.create({
|
|
166
|
-
baseURL: PLATFORM_URL,
|
|
167
|
-
headers: {
|
|
168
|
-
'Content-Type': 'application/json',
|
|
169
|
-
'x-openserv-key': this.apiKey
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
// Initialize runtime client
|
|
173
|
-
this.runtimeClient = axios_1.default.create({
|
|
174
|
-
baseURL: `${RUNTIME_URL}/runtime`,
|
|
175
|
-
headers: {
|
|
176
|
-
'Content-Type': 'application/json',
|
|
177
|
-
'x-openserv-key': this.apiKey
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
this.app.use(express_1.default.json({ limit: '10mb' }));
|
|
181
|
-
this.app.use(express_1.default.urlencoded({ extended: false }));
|
|
182
|
-
this.app.use((0, hpp_1.default)());
|
|
183
|
-
this.app.use((0, helmet_1.default)());
|
|
184
|
-
this.app.use((0, compression_1.default)());
|
|
185
|
-
if (AUTH_TOKEN) {
|
|
186
|
-
this.app.use(authTokenMiddleware);
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
logger_1.logger.warn('OPENSERV_AUTH_TOKEN is not set. All requests will be allowed.');
|
|
190
|
-
}
|
|
191
|
-
this.setupRoutes();
|
|
163
|
+
// Initialize MCP clients (no credentials needed)
|
|
192
164
|
this.initializeMCPClients();
|
|
165
|
+
// Define routes (they'll be attached to app in start())
|
|
166
|
+
this.defineRoutes();
|
|
193
167
|
}
|
|
194
168
|
initializeMCPClients() {
|
|
195
169
|
if (this.options.mcpServers) {
|
|
@@ -720,11 +694,11 @@ class Agent {
|
|
|
720
694
|
}
|
|
721
695
|
}
|
|
722
696
|
/**
|
|
723
|
-
*
|
|
724
|
-
*
|
|
697
|
+
* Defines routes on the router (called in constructor).
|
|
698
|
+
* Routes are attached to the app in start() after middleware is configured.
|
|
725
699
|
* @private
|
|
726
700
|
*/
|
|
727
|
-
|
|
701
|
+
defineRoutes() {
|
|
728
702
|
this.router.get('/health', async (_req, res) => {
|
|
729
703
|
res.status(200).json({ status: 'ok', uptime: process.uptime() });
|
|
730
704
|
});
|
|
@@ -741,16 +715,68 @@ class Agent {
|
|
|
741
715
|
body: req.body
|
|
742
716
|
});
|
|
743
717
|
});
|
|
744
|
-
this.app.use('/', this.router);
|
|
745
718
|
}
|
|
746
719
|
/**
|
|
747
720
|
* Starts the agent's HTTP server.
|
|
748
721
|
* If the preferred port is unavailable, it will find an open port.
|
|
749
722
|
*
|
|
750
723
|
* @returns {Promise<void>} Resolves when the server has started
|
|
751
|
-
* @throws {Error} If server fails to start
|
|
724
|
+
* @throws {Error} If server fails to start or if API key is missing
|
|
752
725
|
*/
|
|
753
726
|
async start() {
|
|
727
|
+
// Resolve credentials from options or environment if not already set via setCredentials()
|
|
728
|
+
if (!this.apiKey) {
|
|
729
|
+
this.apiKey = this.options.apiKey || process.env.OPENSERV_API_KEY;
|
|
730
|
+
}
|
|
731
|
+
if (!this.authToken) {
|
|
732
|
+
this.authToken = this.options.authToken || process.env.OPENSERV_AUTH_TOKEN;
|
|
733
|
+
}
|
|
734
|
+
// Validate API key
|
|
735
|
+
if (!this.apiKey) {
|
|
736
|
+
throw new Error('OpenServ API key is required. Please provide it in options, set OPENSERV_API_KEY environment variable, or call provision() first.');
|
|
737
|
+
}
|
|
738
|
+
// Initialize API clients
|
|
739
|
+
this.apiClient = axios_1.default.create({
|
|
740
|
+
baseURL: PLATFORM_URL,
|
|
741
|
+
headers: {
|
|
742
|
+
'Content-Type': 'application/json',
|
|
743
|
+
'x-openserv-key': this.apiKey
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
this.runtimeClient = axios_1.default.create({
|
|
747
|
+
baseURL: `${RUNTIME_URL}/runtime`,
|
|
748
|
+
headers: {
|
|
749
|
+
'Content-Type': 'application/json',
|
|
750
|
+
'x-openserv-key': this.apiKey
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
// Set up middleware
|
|
754
|
+
this.app.use(express_1.default.json({ limit: '10mb' }));
|
|
755
|
+
this.app.use(express_1.default.urlencoded({ extended: false }));
|
|
756
|
+
this.app.use((0, hpp_1.default)());
|
|
757
|
+
this.app.use((0, helmet_1.default)());
|
|
758
|
+
this.app.use((0, compression_1.default)());
|
|
759
|
+
// Auth middleware
|
|
760
|
+
if (this.authToken) {
|
|
761
|
+
this.app.use(async (req, res, next) => {
|
|
762
|
+
const tokenHash = req.headers['x-openserv-auth-token'];
|
|
763
|
+
if (!tokenHash || typeof tokenHash !== 'string') {
|
|
764
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
const isTokenValid = await bcryptjs_1.default.compare(this.authToken, tokenHash);
|
|
768
|
+
if (!isTokenValid) {
|
|
769
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
next();
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
else {
|
|
776
|
+
logger_1.logger.warn('OPENSERV_AUTH_TOKEN is not set. All requests will be allowed.');
|
|
777
|
+
}
|
|
778
|
+
// Attach routes to app (routes are defined in constructor)
|
|
779
|
+
this.app.use('/', this.router);
|
|
754
780
|
const preferredPort = this.port;
|
|
755
781
|
// Try the preferred port first, fallback to an available port if it fails
|
|
756
782
|
await new Promise((resolve, reject) => {
|