@openserv-labs/sdk 2.0.0 → 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 +43 -12
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +92 -58
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +3 -0
- 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,19 +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
|
-
|
|
91
|
+
* Set via setCredentials() or resolved from options/env in start().
|
|
92
|
+
*/
|
|
93
|
+
apiKey?: string;
|
|
94
|
+
/**
|
|
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
|
+
* ```
|
|
87
117
|
*/
|
|
88
|
-
|
|
118
|
+
setCredentials(credentials: {
|
|
119
|
+
apiKey: string;
|
|
120
|
+
authToken?: string;
|
|
121
|
+
}): void;
|
|
89
122
|
/**
|
|
90
123
|
* Axios instance for making requests to the OpenServ API.
|
|
91
|
-
*
|
|
124
|
+
* Initialized in start() after credentials are resolved.
|
|
92
125
|
* @private
|
|
93
126
|
*/
|
|
94
127
|
private apiClient;
|
|
95
128
|
/**
|
|
96
129
|
* Axios instance for making requests to the OpenServ Runtime API.
|
|
97
|
-
*
|
|
130
|
+
* Initialized in start() after credentials are resolved.
|
|
98
131
|
* @protected
|
|
99
132
|
*/
|
|
100
133
|
protected runtimeClient: AxiosInstance;
|
|
@@ -126,11 +159,9 @@ export declare class Agent<M extends string = string> {
|
|
|
126
159
|
private get openai();
|
|
127
160
|
/**
|
|
128
161
|
* Creates a new Agent instance.
|
|
129
|
-
*
|
|
130
|
-
* Initializes API clients with appropriate authentication.
|
|
162
|
+
* The agent is configured but not started until start() is called.
|
|
131
163
|
*
|
|
132
164
|
* @param {AgentOptions} options - Configuration options for the agent
|
|
133
|
-
* @throws {Error} If OpenServ API key is not provided in options or environment
|
|
134
165
|
*/
|
|
135
166
|
constructor(options: AgentOptions<M>);
|
|
136
167
|
private initializeMCPClients;
|
|
@@ -398,17 +429,17 @@ export declare class Agent<M extends string = string> {
|
|
|
398
429
|
body: unknown;
|
|
399
430
|
}): Promise<void>;
|
|
400
431
|
/**
|
|
401
|
-
*
|
|
402
|
-
*
|
|
432
|
+
* Defines routes on the router (called in constructor).
|
|
433
|
+
* Routes are attached to the app in start() after middleware is configured.
|
|
403
434
|
* @private
|
|
404
435
|
*/
|
|
405
|
-
private
|
|
436
|
+
private defineRoutes;
|
|
406
437
|
/**
|
|
407
438
|
* Starts the agent's HTTP server.
|
|
408
439
|
* If the preferred port is unavailable, it will find an open port.
|
|
409
440
|
*
|
|
410
441
|
* @returns {Promise<void>} Resolves when the server has started
|
|
411
|
-
* @throws {Error} If server fails to start
|
|
442
|
+
* @throws {Error} If server fails to start or if API key is missing
|
|
412
443
|
*/
|
|
413
444
|
start(): Promise<void>;
|
|
414
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;AACzC,OAAO,EAGL,K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M,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,19 +61,46 @@ class Agent {
|
|
|
75
61
|
tools = [];
|
|
76
62
|
/**
|
|
77
63
|
* The OpenServ API key used for authentication.
|
|
78
|
-
*
|
|
79
|
-
* @private
|
|
64
|
+
* Set via setCredentials() or resolved from options/env in start().
|
|
80
65
|
*/
|
|
81
66
|
apiKey;
|
|
67
|
+
/**
|
|
68
|
+
* The auth token used to validate incoming requests.
|
|
69
|
+
* Set via setCredentials() or resolved from options/env in start().
|
|
70
|
+
*/
|
|
71
|
+
authToken;
|
|
72
|
+
/**
|
|
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
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
setCredentials(credentials) {
|
|
92
|
+
this.apiKey = credentials.apiKey;
|
|
93
|
+
this.authToken = credentials.authToken;
|
|
94
|
+
}
|
|
82
95
|
/**
|
|
83
96
|
* Axios instance for making requests to the OpenServ API.
|
|
84
|
-
*
|
|
97
|
+
* Initialized in start() after credentials are resolved.
|
|
85
98
|
* @private
|
|
86
99
|
*/
|
|
87
100
|
apiClient;
|
|
88
101
|
/**
|
|
89
102
|
* Axios instance for making requests to the OpenServ Runtime API.
|
|
90
|
-
*
|
|
103
|
+
* Initialized in start() after credentials are resolved.
|
|
91
104
|
* @protected
|
|
92
105
|
*/
|
|
93
106
|
runtimeClient;
|
|
@@ -137,11 +150,9 @@ class Agent {
|
|
|
137
150
|
}
|
|
138
151
|
/**
|
|
139
152
|
* Creates a new Agent instance.
|
|
140
|
-
*
|
|
141
|
-
* Initializes API clients with appropriate authentication.
|
|
153
|
+
* The agent is configured but not started until start() is called.
|
|
142
154
|
*
|
|
143
155
|
* @param {AgentOptions} options - Configuration options for the agent
|
|
144
|
-
* @throws {Error} If OpenServ API key is not provided in options or environment
|
|
145
156
|
*/
|
|
146
157
|
constructor(options) {
|
|
147
158
|
this.options = options;
|
|
@@ -149,39 +160,10 @@ class Agent {
|
|
|
149
160
|
this.router = (0, express_async_router_1.AsyncRouter)();
|
|
150
161
|
this.port = this.options.port || DEFAULT_PORT;
|
|
151
162
|
this.systemPrompt = this.options.systemPrompt;
|
|
152
|
-
|
|
153
|
-
if (!this.apiKey) {
|
|
154
|
-
throw new Error('OpenServ API key is required. Please provide it in options or set OPENSERV_API_KEY environment variable.');
|
|
155
|
-
}
|
|
156
|
-
// Initialize API client
|
|
157
|
-
this.apiClient = axios_1.default.create({
|
|
158
|
-
baseURL: PLATFORM_URL,
|
|
159
|
-
headers: {
|
|
160
|
-
'Content-Type': 'application/json',
|
|
161
|
-
'x-openserv-key': this.apiKey
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
// Initialize runtime client
|
|
165
|
-
this.runtimeClient = axios_1.default.create({
|
|
166
|
-
baseURL: `${RUNTIME_URL}/runtime`,
|
|
167
|
-
headers: {
|
|
168
|
-
'Content-Type': 'application/json',
|
|
169
|
-
'x-openserv-key': this.apiKey
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
this.app.use(express_1.default.json({ limit: '10mb' }));
|
|
173
|
-
this.app.use(express_1.default.urlencoded({ extended: false }));
|
|
174
|
-
this.app.use((0, hpp_1.default)());
|
|
175
|
-
this.app.use((0, helmet_1.default)());
|
|
176
|
-
this.app.use((0, compression_1.default)());
|
|
177
|
-
if (AUTH_TOKEN) {
|
|
178
|
-
this.app.use(authTokenMiddleware);
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
logger_1.logger.warn('OPENSERV_AUTH_TOKEN is not set. All requests will be allowed.');
|
|
182
|
-
}
|
|
183
|
-
this.setupRoutes();
|
|
163
|
+
// Initialize MCP clients (no credentials needed)
|
|
184
164
|
this.initializeMCPClients();
|
|
165
|
+
// Define routes (they'll be attached to app in start())
|
|
166
|
+
this.defineRoutes();
|
|
185
167
|
}
|
|
186
168
|
initializeMCPClients() {
|
|
187
169
|
if (this.options.mcpServers) {
|
|
@@ -712,11 +694,11 @@ class Agent {
|
|
|
712
694
|
}
|
|
713
695
|
}
|
|
714
696
|
/**
|
|
715
|
-
*
|
|
716
|
-
*
|
|
697
|
+
* Defines routes on the router (called in constructor).
|
|
698
|
+
* Routes are attached to the app in start() after middleware is configured.
|
|
717
699
|
* @private
|
|
718
700
|
*/
|
|
719
|
-
|
|
701
|
+
defineRoutes() {
|
|
720
702
|
this.router.get('/health', async (_req, res) => {
|
|
721
703
|
res.status(200).json({ status: 'ok', uptime: process.uptime() });
|
|
722
704
|
});
|
|
@@ -733,16 +715,68 @@ class Agent {
|
|
|
733
715
|
body: req.body
|
|
734
716
|
});
|
|
735
717
|
});
|
|
736
|
-
this.app.use('/', this.router);
|
|
737
718
|
}
|
|
738
719
|
/**
|
|
739
720
|
* Starts the agent's HTTP server.
|
|
740
721
|
* If the preferred port is unavailable, it will find an open port.
|
|
741
722
|
*
|
|
742
723
|
* @returns {Promise<void>} Resolves when the server has started
|
|
743
|
-
* @throws {Error} If server fails to start
|
|
724
|
+
* @throws {Error} If server fails to start or if API key is missing
|
|
744
725
|
*/
|
|
745
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);
|
|
746
780
|
const preferredPort = this.port;
|
|
747
781
|
// Try the preferred port first, fallback to an available port if it fails
|
|
748
782
|
await new Promise((resolve, reject) => {
|
package/dist/run.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpC,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAMrE,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,MAAM,CAAC,EAAE,IAAI,CAAC,qBAAqB,EAAE,aAAa,GAAG,WAAW,GAAG,SAAS,CAAC,CAAA;IAE7E;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,MAAM,EAAE,cAAc,CAAA;IAEtB;;OAEG;IACH,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC1B;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAsB,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC,
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpC,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAMrE,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,MAAM,CAAC,EAAE,IAAI,CAAC,qBAAqB,EAAE,aAAa,GAAG,WAAW,GAAG,SAAS,CAAC,CAAA;IAE7E;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,MAAM,EAAE,cAAc,CAAA;IAEtB;;OAEG;IACH,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC1B;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAsB,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC,CA8EhF"}
|
package/dist/run.js
CHANGED
|
@@ -42,6 +42,9 @@ async function run(agent, options) {
|
|
|
42
42
|
await agent.start();
|
|
43
43
|
const tunnel = new tunnel_1.OpenServTunnel({
|
|
44
44
|
...options?.tunnel,
|
|
45
|
+
// Always use the agent's API key to ensure tunnel authenticates as the correct agent
|
|
46
|
+
// This prevents issues when running multiple agents with different API keys
|
|
47
|
+
apiKey: agent.apiKey,
|
|
45
48
|
onConnected: isReconnect => {
|
|
46
49
|
if (!isReconnect) {
|
|
47
50
|
logger_1.logger.info('Agent connected to OpenServ proxy');
|