@cloudbase/agent-server 0.0.8 → 0.0.11
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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +17 -5
- package/dist/index.js +62 -19
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @cloudbase/agent-server
|
|
2
2
|
|
|
3
|
+
## 1.0.1-alpha.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- alpha release 0.1.2-alpha.1
|
|
8
|
+
- Update all public packages to version 0.1.2-alpha.1
|
|
9
|
+
- Trigger automated alpha release workflow
|
|
10
|
+
- Includes latest features and improvements
|
|
11
|
+
|
|
12
|
+
- Updated dependencies
|
|
13
|
+
- @cloudbase/agent-shared@1.0.1-alpha.5
|
|
14
|
+
|
|
3
15
|
## 1.0.1-alpha.4
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CreateCopilotRuntimeServerOptions } from '@copilotkit/runtime';
|
|
2
2
|
import { CopilotRuntimeOptions } from '@copilotkit/runtime/v2';
|
|
3
|
-
import
|
|
3
|
+
import expressLib, { Express } from 'express';
|
|
4
4
|
import * as _ag_ui_client from '@ag-ui/client';
|
|
5
5
|
import { AbstractAgent, RunAgentInput } from '@ag-ui/client';
|
|
6
6
|
import cors from 'cors';
|
|
@@ -9,13 +9,25 @@ import { Repeater } from '@repeaterjs/repeater';
|
|
|
9
9
|
import * as _whatwg_node_server from '@whatwg-node/server';
|
|
10
10
|
import { OpenAI } from 'openai';
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Context passed to the agent factory function.
|
|
14
|
+
* Contains request information for per-request agent configuration.
|
|
15
|
+
*/
|
|
16
|
+
interface AgentCreatorContext {
|
|
17
|
+
/** The incoming HTTP request (Web Standard Request) */
|
|
18
|
+
request: Request;
|
|
19
|
+
}
|
|
12
20
|
type AgentCreatorRet = {
|
|
13
21
|
agent: AbstractAgent | {
|
|
14
22
|
toAGUIAgent(): AbstractAgent;
|
|
15
23
|
};
|
|
16
24
|
cleanup?: () => void;
|
|
17
25
|
};
|
|
18
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Factory function to create an agent.
|
|
28
|
+
* Can optionally receive request context for per-request configuration.
|
|
29
|
+
*/
|
|
30
|
+
type AgentCreator = ((context: AgentCreatorContext) => AgentCreatorRet | Promise<AgentCreatorRet>) | (() => AgentCreatorRet | Promise<AgentCreatorRet>);
|
|
19
31
|
interface ICreateServer {
|
|
20
32
|
createAgent: AgentCreator;
|
|
21
33
|
basePath?: `/${string}/`;
|
|
@@ -26,12 +38,12 @@ interface ICreateServer {
|
|
|
26
38
|
interface IRun extends ICreateServer {
|
|
27
39
|
port?: number | string;
|
|
28
40
|
}
|
|
29
|
-
interface ICreateExpressRoutes extends ICreateServer {
|
|
41
|
+
interface ICreateExpressRoutes extends Omit<ICreateServer, "cors"> {
|
|
30
42
|
express: Express;
|
|
31
43
|
}
|
|
32
44
|
declare function run(props: IRun): void;
|
|
33
45
|
declare function createExpressServer(props: ICreateServer): Express;
|
|
34
|
-
declare function createExpressRoutes({ createAgent, basePath: _basePath, express, useAGUI: _useAGUI, aguiOptions, }: ICreateExpressRoutes):
|
|
46
|
+
declare function createExpressRoutes({ createAgent, basePath: _basePath, express, useAGUI: _useAGUI, aguiOptions, }: ICreateExpressRoutes): expressLib.Express;
|
|
35
47
|
interface AGUIOptions {
|
|
36
48
|
runtimeOptions?: Partial<CopilotRuntimeOptions>;
|
|
37
49
|
endpointOptions?: Partial<CreateCopilotRuntimeServerOptions>;
|
|
@@ -107,4 +119,4 @@ declare namespace index {
|
|
|
107
119
|
export { index$2 as healthz, index$1 as openai, index$4 as sendMessage, index$3 as sendMessageAGUI };
|
|
108
120
|
}
|
|
109
121
|
|
|
110
|
-
export { type AgentCreator, index as agui, createExpressRoutes, createExpressServer, run };
|
|
122
|
+
export { type AgentCreator, type AgentCreatorContext, index as agui, createExpressRoutes, createExpressServer, run };
|
package/dist/index.js
CHANGED
|
@@ -166,7 +166,9 @@ var import_agent_shared = require("@cloudbase/agent-shared");
|
|
|
166
166
|
function createServerAdapter(createAgent) {
|
|
167
167
|
return (0, import_server.createServerAdapter)(async (request) => {
|
|
168
168
|
const input = import_agent_shared.sendMessageInputSchema.parse(await request.json());
|
|
169
|
-
const { agent: unknownAgent, cleanup } = await Promise.resolve(
|
|
169
|
+
const { agent: unknownAgent, cleanup } = await Promise.resolve(
|
|
170
|
+
createAgent({ request })
|
|
171
|
+
);
|
|
170
172
|
const agent = "toAGUIAgent" in unknownAgent ? unknownAgent.toAGUIAgent() : unknownAgent;
|
|
171
173
|
const events = handler(input, agent);
|
|
172
174
|
let heartbeat;
|
|
@@ -232,7 +234,9 @@ var import_client2 = require("@ag-ui/client");
|
|
|
232
234
|
function createServerAdapter2(createAgent) {
|
|
233
235
|
return (0, import_server3.createServerAdapter)(async (request) => {
|
|
234
236
|
const input = import_client2.RunAgentInputSchema.parse(await request.json());
|
|
235
|
-
const { agent: unknownAgent, cleanup } = await Promise.resolve(
|
|
237
|
+
const { agent: unknownAgent, cleanup } = await Promise.resolve(
|
|
238
|
+
createAgent({ request })
|
|
239
|
+
);
|
|
236
240
|
const agent = "toAGUIAgent" in unknownAgent ? unknownAgent.toAGUIAgent() : unknownAgent;
|
|
237
241
|
const events = handler2(input, agent);
|
|
238
242
|
let heartbeat;
|
|
@@ -430,7 +434,9 @@ var import_server6 = require("@whatwg-node/server");
|
|
|
430
434
|
function createServerAdapter3(createAgent) {
|
|
431
435
|
return (0, import_server6.createServerAdapter)(async (request) => {
|
|
432
436
|
const input = await request.json();
|
|
433
|
-
const { agent: unknownAgent, cleanup } = await Promise.resolve(
|
|
437
|
+
const { agent: unknownAgent, cleanup } = await Promise.resolve(
|
|
438
|
+
createAgent({ request })
|
|
439
|
+
);
|
|
434
440
|
const agent = "toAGUIAgent" in unknownAgent ? unknownAgent.toAGUIAgent() : unknownAgent;
|
|
435
441
|
const events = handler3(input, agent);
|
|
436
442
|
const stream = new ReadableStream({
|
|
@@ -460,6 +466,19 @@ function createServerAdapter3(createAgent) {
|
|
|
460
466
|
var import_runtime = require("@copilotkit/runtime");
|
|
461
467
|
var import_express = __toESM(require("express"));
|
|
462
468
|
var import_cors = __toESM(require("cors"));
|
|
469
|
+
var import_async_hooks = require("async_hooks");
|
|
470
|
+
var import_server8 = require("@whatwg-node/server");
|
|
471
|
+
var DefaultFetchAPI = __toESM(require("@whatwg-node/fetch"));
|
|
472
|
+
var preparedAgentStorage = new import_async_hooks.AsyncLocalStorage();
|
|
473
|
+
function agentCloneFn() {
|
|
474
|
+
const preparedAgent = preparedAgentStorage.getStore();
|
|
475
|
+
if (preparedAgent) {
|
|
476
|
+
return preparedAgent;
|
|
477
|
+
}
|
|
478
|
+
throw new Error(
|
|
479
|
+
"agent.clone() called outside of request context. This should not happen in normal operation."
|
|
480
|
+
);
|
|
481
|
+
}
|
|
463
482
|
function run(props) {
|
|
464
483
|
const { port, ...rest } = props;
|
|
465
484
|
createExpressServer(rest).listen(
|
|
@@ -481,42 +500,44 @@ function createExpressServer(props) {
|
|
|
481
500
|
function createExpressRoutes({
|
|
482
501
|
createAgent,
|
|
483
502
|
basePath: _basePath,
|
|
484
|
-
express
|
|
503
|
+
express,
|
|
485
504
|
useAGUI: _useAGUI,
|
|
486
505
|
aguiOptions
|
|
487
506
|
}) {
|
|
488
507
|
const useAGUI = _useAGUI ?? true;
|
|
489
|
-
const basePath = _basePath ?? (process.env.TENCENTCLOUD_RUNENV === "SCF" ? "/v1/aibot/bots/:agentId/" :
|
|
508
|
+
const basePath = _basePath ?? (process.env.TENCENTCLOUD_RUNENV === "SCF" ? "/v1/aibot/bots/:agentId/" : "/");
|
|
490
509
|
const sendMessageServerAdapter = useAGUI ? sendMessageAGUI_exports.createServerAdapter(createAgent) : sendMessage_exports.createServerAdapter(createAgent);
|
|
491
510
|
if (useAGUI) {
|
|
492
511
|
createAGUIRoute({
|
|
493
512
|
basePath,
|
|
494
|
-
express
|
|
513
|
+
express,
|
|
495
514
|
createAgent,
|
|
496
515
|
...aguiOptions || {}
|
|
497
516
|
});
|
|
498
517
|
}
|
|
499
518
|
const openaiServerAdapter = openai_exports.createServerAdapter(createAgent);
|
|
500
519
|
const healthzServerAdapter = healthz_exports.serverAdapter;
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
return
|
|
520
|
+
express.use(`${basePath}send-message`, sendMessageServerAdapter);
|
|
521
|
+
express.use(`${basePath}healthz`, healthzServerAdapter);
|
|
522
|
+
express.use(`${basePath}chat/completions`, openaiServerAdapter);
|
|
523
|
+
return express;
|
|
505
524
|
}
|
|
506
525
|
var AGUIRpcHandlerPromise = null;
|
|
507
526
|
function getAGUIRpcHandler({
|
|
508
527
|
createAgent,
|
|
509
528
|
runtimeOptions,
|
|
510
529
|
basePath,
|
|
511
|
-
endpointOptions
|
|
530
|
+
endpointOptions,
|
|
531
|
+
request
|
|
512
532
|
}) {
|
|
513
533
|
if (AGUIRpcHandlerPromise) return AGUIRpcHandlerPromise;
|
|
514
534
|
AGUIRpcHandlerPromise = (async () => {
|
|
515
|
-
const agent =
|
|
516
|
-
const
|
|
535
|
+
const { agent } = await createAgent({ request });
|
|
536
|
+
const templateAgent = "toAGUIAgent" in agent ? agent.toAGUIAgent() : agent;
|
|
537
|
+
templateAgent.clone = agentCloneFn;
|
|
517
538
|
const runtime = new import_runtime.CopilotRuntime({
|
|
518
539
|
agents: {
|
|
519
|
-
default:
|
|
540
|
+
default: templateAgent
|
|
520
541
|
},
|
|
521
542
|
...runtimeOptions || {}
|
|
522
543
|
});
|
|
@@ -529,11 +550,33 @@ function getAGUIRpcHandler({
|
|
|
529
550
|
})();
|
|
530
551
|
return AGUIRpcHandlerPromise;
|
|
531
552
|
}
|
|
532
|
-
function createAGUIRoute(
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
553
|
+
function createAGUIRoute({
|
|
554
|
+
express,
|
|
555
|
+
basePath,
|
|
556
|
+
createAgent,
|
|
557
|
+
runtimeOptions,
|
|
558
|
+
endpointOptions
|
|
559
|
+
}) {
|
|
560
|
+
express.post(`${basePath}agui`, import_express.default.json(), async (req, res) => {
|
|
561
|
+
const webRequest = (0, import_server8.normalizeNodeRequest)(req, DefaultFetchAPI);
|
|
562
|
+
const { agent: rawAgent, cleanup } = await createAgent({
|
|
563
|
+
request: webRequest
|
|
564
|
+
});
|
|
565
|
+
const preparedAgent = "toAGUIAgent" in rawAgent ? rawAgent.toAGUIAgent() : rawAgent;
|
|
566
|
+
preparedAgent.clone = agentCloneFn;
|
|
567
|
+
const rpcHandler = await getAGUIRpcHandler({
|
|
568
|
+
createAgent,
|
|
569
|
+
basePath,
|
|
570
|
+
runtimeOptions,
|
|
571
|
+
endpointOptions,
|
|
572
|
+
request: webRequest
|
|
573
|
+
});
|
|
574
|
+
preparedAgentStorage.run(preparedAgent, () => {
|
|
575
|
+
rpcHandler(req, res);
|
|
576
|
+
});
|
|
577
|
+
if (cleanup) {
|
|
578
|
+
res.on("close", cleanup);
|
|
579
|
+
}
|
|
537
580
|
});
|
|
538
581
|
}
|
|
539
582
|
function isCorsOptions(cors2) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudbase/agent-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist/",
|
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
"@ag-ui/client": "0.0.42",
|
|
15
15
|
"@copilotkit/runtime": "^1.50.1",
|
|
16
16
|
"@repeaterjs/repeater": "^3.0.6",
|
|
17
|
+
"@whatwg-node/fetch": "^0.10.13",
|
|
17
18
|
"@whatwg-node/server": "^0.10.12",
|
|
18
19
|
"cors": "^2.8.5",
|
|
19
20
|
"express": "^5.1.0",
|
|
20
21
|
"openai": "6.3.0",
|
|
21
22
|
"uuid": "^10.0.0",
|
|
22
23
|
"zod": "^4.1.12",
|
|
23
|
-
"@cloudbase/agent-shared": "^0.0.
|
|
24
|
+
"@cloudbase/agent-shared": "^0.0.11"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
27
|
"@types/cors": "^2.8.19",
|