@cloudbase/agent-server 1.0.1-alpha.8 → 1.0.1-alpha.9
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 +24 -0
- package/dist/index.d.ts +55 -3
- package/dist/index.js +272 -10
- package/package.json +10 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @cloudbase/agent-server
|
|
2
2
|
|
|
3
|
+
## 1.0.1-alpha.8
|
|
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.8
|
|
14
|
+
|
|
15
|
+
## 1.0.1-alpha.7
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- alpha release 0.1.2-alpha.1
|
|
20
|
+
- Update all public packages to version 0.1.2-alpha.1
|
|
21
|
+
- Trigger automated alpha release workflow
|
|
22
|
+
- Includes latest features and improvements
|
|
23
|
+
|
|
24
|
+
- Updated dependencies
|
|
25
|
+
- @cloudbase/agent-shared@1.0.1-alpha.7
|
|
26
|
+
|
|
3
27
|
## 1.0.1-alpha.6
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as _cloudbase_agent_observability_server from '@cloudbase/agent-observability/server';
|
|
1
2
|
import { CreateCopilotRuntimeServerOptions } from '@copilotkit/runtime';
|
|
2
3
|
import { CopilotRuntimeOptions } from '@copilotkit/runtime/v2';
|
|
3
4
|
import expressLib, { Express } from 'express';
|
|
@@ -5,7 +6,7 @@ import * as _ag_ui_client from '@ag-ui/client';
|
|
|
5
6
|
import { AbstractAgent, RunAgentInput } from '@ag-ui/client';
|
|
6
7
|
import cors from 'cors';
|
|
7
8
|
import { Logger, SendMessageInput } from '@cloudbase/agent-shared';
|
|
8
|
-
export { LogFn, Logger, createConsoleLogger, noopLogger } from '@cloudbase/agent-shared';
|
|
9
|
+
export { LogFn, Logger, createConsoleLogger, isErrorWithCode, noopLogger } from '@cloudbase/agent-shared';
|
|
9
10
|
import { Repeater } from '@repeaterjs/repeater';
|
|
10
11
|
import * as _whatwg_node_server from '@whatwg-node/server';
|
|
11
12
|
import { OpenAI } from 'openai';
|
|
@@ -53,16 +54,47 @@ interface ICreateServer {
|
|
|
53
54
|
* createExpressServer({ createAgent, logger: pino({ level: 'info' }) });
|
|
54
55
|
*/
|
|
55
56
|
logger?: Logger;
|
|
57
|
+
/**
|
|
58
|
+
* Observability configuration for trace exporters.
|
|
59
|
+
*
|
|
60
|
+
* Requires @cloudbase/agent-observability package to be installed.
|
|
61
|
+
* If the package is not installed, this option is silently ignored.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // Console exporter (from env AUTO_TRACES_STDOUT)
|
|
65
|
+
* createExpressServer({ createAgent, observability: { type: 'console' } });
|
|
66
|
+
*
|
|
67
|
+
* // OTLP exporter (Langfuse, Jaeger, etc.)
|
|
68
|
+
* createExpressServer({
|
|
69
|
+
* createAgent,
|
|
70
|
+
* observability: {
|
|
71
|
+
* type: 'otlp',
|
|
72
|
+
* url: 'https://cloud.langfuse.com/api/public/otlp/v1/traces',
|
|
73
|
+
* headers: { 'Authorization': 'Basic xxx' }
|
|
74
|
+
* }
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* // Multiple exporters
|
|
78
|
+
* createExpressServer({
|
|
79
|
+
* createAgent,
|
|
80
|
+
* observability: [
|
|
81
|
+
* { type: 'console' },
|
|
82
|
+
* { type: 'otlp', url: 'http://localhost:4318/v1/traces' }
|
|
83
|
+
* ]
|
|
84
|
+
* });
|
|
85
|
+
*/
|
|
86
|
+
observability?: _cloudbase_agent_observability_server.ObservabilityConfig | _cloudbase_agent_observability_server.ObservabilityConfig[];
|
|
56
87
|
}
|
|
57
88
|
interface IRun extends ICreateServer {
|
|
58
89
|
port?: number | string;
|
|
59
90
|
}
|
|
60
91
|
interface ICreateExpressRoutes extends Omit<ICreateServer, "cors"> {
|
|
61
92
|
express: Express;
|
|
93
|
+
observability?: ICreateServer['observability'];
|
|
62
94
|
}
|
|
63
95
|
declare function run(props: IRun): void;
|
|
64
96
|
declare function createExpressServer(props: ICreateServer): Express;
|
|
65
|
-
declare function createExpressRoutes({ createAgent, basePath: _basePath, express, useAGUI: _useAGUI, aguiOptions, logger: _logger, }: ICreateExpressRoutes): expressLib.Express;
|
|
97
|
+
declare function createExpressRoutes({ createAgent, basePath: _basePath, express, useAGUI: _useAGUI, aguiOptions, logger: _logger, observability, }: ICreateExpressRoutes): expressLib.Express;
|
|
66
98
|
interface AGUIOptions {
|
|
67
99
|
runtimeOptions?: Partial<CopilotRuntimeOptions>;
|
|
68
100
|
endpointOptions?: Partial<CreateCopilotRuntimeServerOptions>;
|
|
@@ -188,4 +220,24 @@ declare function extractRequestId(headers: Headers | Record<string, string | str
|
|
|
188
220
|
*/
|
|
189
221
|
declare function getOrGenerateRequestId(headers: Headers | Record<string, string | string[] | undefined>, prefix?: string): string;
|
|
190
222
|
|
|
191
|
-
|
|
223
|
+
/**
|
|
224
|
+
* AG-Kit Error Handling
|
|
225
|
+
*
|
|
226
|
+
* Error handling follows these rules:
|
|
227
|
+
* 1. Pre-stream errors: Return JSON `{error: {code, message}, requestId}`
|
|
228
|
+
* 2. In-stream errors: Emit RunError event `{type: "RUN_ERROR", code, message}`
|
|
229
|
+
*
|
|
230
|
+
* Adapters can:
|
|
231
|
+
* - Directly emit RunError events into the stream
|
|
232
|
+
* - Throw errors with a `code` property (server extracts and converts to RunError)
|
|
233
|
+
* - Throw other errors (server uses fallback INTERNAL_ERROR code)
|
|
234
|
+
*/
|
|
235
|
+
declare const ErrorCode: {
|
|
236
|
+
/** Invalid request format or parameters (400) */
|
|
237
|
+
readonly INVALID_REQUEST: "INVALID_REQUEST";
|
|
238
|
+
/** Internal server error (500) */
|
|
239
|
+
readonly INTERNAL_ERROR: "INTERNAL_ERROR";
|
|
240
|
+
};
|
|
241
|
+
type ErrorCodeType = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
242
|
+
|
|
243
|
+
export { type AgentCreator, type AgentCreatorContext, ErrorCode, type ErrorCodeType, index as agui, createExpressRoutes, createExpressServer, extractRequestId, generateRequestId, getOrGenerateRequestId, run };
|
package/dist/index.js
CHANGED
|
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// src/index.ts
|
|
30
30
|
var index_exports = {};
|
|
31
31
|
__export(index_exports, {
|
|
32
|
+
ErrorCode: () => ErrorCode,
|
|
32
33
|
agui: () => agui_exports,
|
|
33
34
|
createConsoleLogger: () => import_agent_shared2.createConsoleLogger,
|
|
34
35
|
createExpressRoutes: () => createExpressRoutes,
|
|
@@ -36,6 +37,7 @@ __export(index_exports, {
|
|
|
36
37
|
extractRequestId: () => extractRequestId,
|
|
37
38
|
generateRequestId: () => generateRequestId,
|
|
38
39
|
getOrGenerateRequestId: () => getOrGenerateRequestId,
|
|
40
|
+
isErrorWithCode: () => import_agent_shared3.isErrorWithCode,
|
|
39
41
|
noopLogger: () => import_agent_shared2.noopLogger,
|
|
40
42
|
run: () => run
|
|
41
43
|
});
|
|
@@ -266,13 +268,166 @@ function getOrGenerateRequestId(headers, prefix = "req") {
|
|
|
266
268
|
return extractRequestId(headers) || generateRequestId(prefix);
|
|
267
269
|
}
|
|
268
270
|
|
|
271
|
+
// src/agui/sendMessageAGUI/schemas.ts
|
|
272
|
+
var import_v4 = require("zod/v4");
|
|
273
|
+
var FunctionCallSchema = import_v4.z.object({
|
|
274
|
+
name: import_v4.z.string(),
|
|
275
|
+
arguments: import_v4.z.string()
|
|
276
|
+
});
|
|
277
|
+
var ToolCallSchema = import_v4.z.object({
|
|
278
|
+
id: import_v4.z.string(),
|
|
279
|
+
type: import_v4.z.literal("function"),
|
|
280
|
+
function: FunctionCallSchema
|
|
281
|
+
});
|
|
282
|
+
var ToolSchema = import_v4.z.object({
|
|
283
|
+
name: import_v4.z.string(),
|
|
284
|
+
description: import_v4.z.string(),
|
|
285
|
+
parameters: import_v4.z.any()
|
|
286
|
+
});
|
|
287
|
+
var TextInputContentSchema = import_v4.z.object({
|
|
288
|
+
type: import_v4.z.literal("text"),
|
|
289
|
+
text: import_v4.z.string()
|
|
290
|
+
});
|
|
291
|
+
var BinaryInputContentObjectSchema = import_v4.z.object({
|
|
292
|
+
type: import_v4.z.literal("binary"),
|
|
293
|
+
mimeType: import_v4.z.string(),
|
|
294
|
+
id: import_v4.z.string().optional(),
|
|
295
|
+
url: import_v4.z.string().optional(),
|
|
296
|
+
data: import_v4.z.string().optional(),
|
|
297
|
+
filename: import_v4.z.string().optional()
|
|
298
|
+
});
|
|
299
|
+
var ensureBinaryPayload = (value, ctx) => {
|
|
300
|
+
if (!value.id && !value.url && !value.data) {
|
|
301
|
+
ctx.addIssue({
|
|
302
|
+
code: import_v4.z.ZodIssueCode.custom,
|
|
303
|
+
message: "BinaryInputContent requires at least one of id, url, or data.",
|
|
304
|
+
path: ["id"]
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
var BinaryInputContentSchema = BinaryInputContentObjectSchema.superRefine((value, ctx) => {
|
|
309
|
+
ensureBinaryPayload(value, ctx);
|
|
310
|
+
});
|
|
311
|
+
var InputContentBaseSchema = import_v4.z.discriminatedUnion("type", [
|
|
312
|
+
TextInputContentSchema,
|
|
313
|
+
BinaryInputContentObjectSchema
|
|
314
|
+
]);
|
|
315
|
+
var InputContentSchema = InputContentBaseSchema.superRefine(
|
|
316
|
+
(value, ctx) => {
|
|
317
|
+
if (value.type === "binary") {
|
|
318
|
+
ensureBinaryPayload(value, ctx);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
);
|
|
322
|
+
var BaseMessageSchema = import_v4.z.object({
|
|
323
|
+
id: import_v4.z.string(),
|
|
324
|
+
role: import_v4.z.string(),
|
|
325
|
+
content: import_v4.z.string().optional(),
|
|
326
|
+
name: import_v4.z.string().optional()
|
|
327
|
+
});
|
|
328
|
+
var DeveloperMessageSchema = BaseMessageSchema.extend({
|
|
329
|
+
role: import_v4.z.literal("developer"),
|
|
330
|
+
content: import_v4.z.string()
|
|
331
|
+
});
|
|
332
|
+
var SystemMessageSchema = BaseMessageSchema.extend({
|
|
333
|
+
role: import_v4.z.literal("system"),
|
|
334
|
+
content: import_v4.z.string()
|
|
335
|
+
});
|
|
336
|
+
var AssistantMessageSchema = BaseMessageSchema.extend({
|
|
337
|
+
role: import_v4.z.literal("assistant"),
|
|
338
|
+
content: import_v4.z.string().optional(),
|
|
339
|
+
toolCalls: import_v4.z.array(ToolCallSchema).optional()
|
|
340
|
+
});
|
|
341
|
+
var UserMessageSchema = BaseMessageSchema.extend({
|
|
342
|
+
role: import_v4.z.literal("user"),
|
|
343
|
+
content: import_v4.z.union([import_v4.z.string(), import_v4.z.array(InputContentSchema)])
|
|
344
|
+
});
|
|
345
|
+
var ToolMessageSchema = import_v4.z.object({
|
|
346
|
+
id: import_v4.z.string(),
|
|
347
|
+
content: import_v4.z.string(),
|
|
348
|
+
role: import_v4.z.literal("tool"),
|
|
349
|
+
toolCallId: import_v4.z.string(),
|
|
350
|
+
error: import_v4.z.string().optional()
|
|
351
|
+
});
|
|
352
|
+
var ActivityMessageSchema = import_v4.z.object({
|
|
353
|
+
id: import_v4.z.string(),
|
|
354
|
+
role: import_v4.z.literal("activity"),
|
|
355
|
+
activityType: import_v4.z.string(),
|
|
356
|
+
content: import_v4.z.record(import_v4.z.string(), import_v4.z.any())
|
|
357
|
+
});
|
|
358
|
+
var MessageSchema = import_v4.z.discriminatedUnion("role", [
|
|
359
|
+
DeveloperMessageSchema,
|
|
360
|
+
SystemMessageSchema,
|
|
361
|
+
AssistantMessageSchema,
|
|
362
|
+
UserMessageSchema,
|
|
363
|
+
ToolMessageSchema,
|
|
364
|
+
ActivityMessageSchema
|
|
365
|
+
]);
|
|
366
|
+
var ContextSchema = import_v4.z.object({
|
|
367
|
+
description: import_v4.z.string(),
|
|
368
|
+
value: import_v4.z.string()
|
|
369
|
+
});
|
|
370
|
+
var ServerRunAgentInputSchema = import_v4.z.object({
|
|
371
|
+
threadId: import_v4.z.string().optional(),
|
|
372
|
+
// Modified: optional instead of required
|
|
373
|
+
runId: import_v4.z.string(),
|
|
374
|
+
parentRunId: import_v4.z.string().optional(),
|
|
375
|
+
state: import_v4.z.any(),
|
|
376
|
+
messages: import_v4.z.array(MessageSchema),
|
|
377
|
+
tools: import_v4.z.array(ToolSchema),
|
|
378
|
+
context: import_v4.z.array(ContextSchema),
|
|
379
|
+
forwardedProps: import_v4.z.any()
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// src/errors/index.ts
|
|
383
|
+
var import_agent_shared3 = require("@cloudbase/agent-shared");
|
|
384
|
+
var ErrorCode = {
|
|
385
|
+
/** Invalid request format or parameters (400) */
|
|
386
|
+
INVALID_REQUEST: "INVALID_REQUEST",
|
|
387
|
+
/** Internal server error (500) */
|
|
388
|
+
INTERNAL_ERROR: "INTERNAL_ERROR"
|
|
389
|
+
};
|
|
390
|
+
|
|
269
391
|
// src/agui/sendMessageAGUI/server.ts
|
|
392
|
+
var startObservation;
|
|
393
|
+
var setupObservability;
|
|
394
|
+
var observabilityLoadAttempted = false;
|
|
395
|
+
async function loadObservability() {
|
|
396
|
+
if (!observabilityLoadAttempted) {
|
|
397
|
+
observabilityLoadAttempted = true;
|
|
398
|
+
try {
|
|
399
|
+
const obs = await import("@cloudbase/agent-observability");
|
|
400
|
+
const obsServer = await import("@cloudbase/agent-observability/server");
|
|
401
|
+
startObservation = obs.startObservation;
|
|
402
|
+
setupObservability = obsServer.setupObservability;
|
|
403
|
+
return true;
|
|
404
|
+
} catch (e) {
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return !!startObservation;
|
|
409
|
+
}
|
|
410
|
+
async function ensureObservabilityReady() {
|
|
411
|
+
if (!setupObservability) return false;
|
|
412
|
+
try {
|
|
413
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
414
|
+
setTimeout(() => reject(new Error("Observability setup timeout")), 2e3);
|
|
415
|
+
});
|
|
416
|
+
await Promise.race([
|
|
417
|
+
setupObservability(),
|
|
418
|
+
timeoutPromise
|
|
419
|
+
]);
|
|
420
|
+
return true;
|
|
421
|
+
} catch (e) {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
270
425
|
function createServerAdapter2(createAgent, options) {
|
|
271
426
|
var _a;
|
|
272
427
|
const { logger: parentLogger = import_agent_shared2.noopLogger } = options ?? {};
|
|
273
428
|
const adapterLogger = ((_a = parentLogger.child) == null ? void 0 : _a.call(parentLogger, { component: "sendMessageAGUI" })) ?? parentLogger;
|
|
274
429
|
return (0, import_server3.createServerAdapter)(async (request) => {
|
|
275
|
-
var _a2, _b, _c, _d, _e, _f, _g, _h;
|
|
430
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
276
431
|
const requestId = getOrGenerateRequestId(request.headers, "agui");
|
|
277
432
|
const logger = ((_a2 = adapterLogger.child) == null ? void 0 : _a2.call(adapterLogger, { requestId })) ?? adapterLogger;
|
|
278
433
|
(_b = logger.info) == null ? void 0 : _b.call(logger, "Request received");
|
|
@@ -286,7 +441,7 @@ function createServerAdapter2(createAgent, options) {
|
|
|
286
441
|
...rawInput,
|
|
287
442
|
runId: typeof rawInput.runId === "string" && rawInput.runId ? rawInput.runId : (0, import_uuid3.v4)()
|
|
288
443
|
};
|
|
289
|
-
return
|
|
444
|
+
return ServerRunAgentInputSchema.parse(inputWithDefaults);
|
|
290
445
|
});
|
|
291
446
|
if ("error" in inputRes) {
|
|
292
447
|
const { error } = inputRes;
|
|
@@ -297,7 +452,10 @@ function createServerAdapter2(createAgent, options) {
|
|
|
297
452
|
);
|
|
298
453
|
return new Response(
|
|
299
454
|
JSON.stringify({
|
|
300
|
-
error:
|
|
455
|
+
error: {
|
|
456
|
+
code: ErrorCode.INVALID_REQUEST,
|
|
457
|
+
message: error instanceof Error ? error.message : String(error)
|
|
458
|
+
},
|
|
301
459
|
requestId
|
|
302
460
|
}),
|
|
303
461
|
{
|
|
@@ -333,9 +491,14 @@ function createServerAdapter2(createAgent, options) {
|
|
|
333
491
|
if ("error" in createAgentRes) {
|
|
334
492
|
const { error } = createAgentRes;
|
|
335
493
|
(_h = logger.error) == null ? void 0 : _h.call(logger, { err: error }, "Agent creation failed");
|
|
494
|
+
const errorCode = (0, import_agent_shared3.isErrorWithCode)(error) ? error.code : ErrorCode.INTERNAL_ERROR;
|
|
495
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
336
496
|
return new Response(
|
|
337
497
|
JSON.stringify({
|
|
338
|
-
error:
|
|
498
|
+
error: {
|
|
499
|
+
code: errorCode,
|
|
500
|
+
message: errorMessage
|
|
501
|
+
},
|
|
339
502
|
requestId
|
|
340
503
|
}),
|
|
341
504
|
{
|
|
@@ -344,8 +507,46 @@ function createServerAdapter2(createAgent, options) {
|
|
|
344
507
|
}
|
|
345
508
|
);
|
|
346
509
|
}
|
|
347
|
-
const
|
|
348
|
-
let
|
|
510
|
+
const hasObservability = await loadObservability();
|
|
511
|
+
let serverSpan = null;
|
|
512
|
+
let serverContextData = null;
|
|
513
|
+
if (hasObservability && startObservation) {
|
|
514
|
+
try {
|
|
515
|
+
const isReady = await ensureObservabilityReady();
|
|
516
|
+
if (isReady) {
|
|
517
|
+
serverSpan = startObservation(
|
|
518
|
+
"AG-UI.Server",
|
|
519
|
+
{
|
|
520
|
+
"http.method": request.method,
|
|
521
|
+
"http.url": request.url,
|
|
522
|
+
"http.host": request.headers.get("host") || "unknown",
|
|
523
|
+
"http.user_agent": request.headers.get("user-agent") || "unknown",
|
|
524
|
+
"agui.thread_id": inputRes.result.threadId,
|
|
525
|
+
"agui.run_id": inputRes.result.runId
|
|
526
|
+
},
|
|
527
|
+
{ asType: "span" }
|
|
528
|
+
);
|
|
529
|
+
const spanContext = serverSpan.otelSpan.spanContext();
|
|
530
|
+
serverContextData = {
|
|
531
|
+
traceId: spanContext.traceId,
|
|
532
|
+
spanId: spanContext.spanId,
|
|
533
|
+
traceFlags: spanContext.traceFlags
|
|
534
|
+
};
|
|
535
|
+
inputRes.result.forwardedProps = {
|
|
536
|
+
...inputRes.result.forwardedProps,
|
|
537
|
+
__agui_server_context: serverContextData
|
|
538
|
+
};
|
|
539
|
+
(_i = logger.debug) == null ? void 0 : _i.call(logger, "\u2713 Server span created:", {
|
|
540
|
+
traceId: serverContextData.traceId,
|
|
541
|
+
spanId: serverContextData.spanId
|
|
542
|
+
});
|
|
543
|
+
} else {
|
|
544
|
+
(_j = logger.debug) == null ? void 0 : _j.call(logger, "Observability not ready, skipping span creation");
|
|
545
|
+
}
|
|
546
|
+
} catch (e) {
|
|
547
|
+
(_k = logger.debug) == null ? void 0 : _k.call(logger, "Failed to create server span:", e);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
349
550
|
let cleanupCalled = false;
|
|
350
551
|
const safeCleanup = () => {
|
|
351
552
|
var _a3, _b2, _c2;
|
|
@@ -358,9 +559,36 @@ function createServerAdapter2(createAgent, options) {
|
|
|
358
559
|
}
|
|
359
560
|
}
|
|
360
561
|
};
|
|
562
|
+
const eventsResult = safe(
|
|
563
|
+
() => handler2(
|
|
564
|
+
inputRes.result,
|
|
565
|
+
createAgentRes.result.agent
|
|
566
|
+
)
|
|
567
|
+
);
|
|
568
|
+
if ("error" in eventsResult) {
|
|
569
|
+
const { error } = eventsResult;
|
|
570
|
+
(_l = logger.error) == null ? void 0 : _l.call(logger, { err: error }, "Run agent failed");
|
|
571
|
+
const errorCode = (0, import_agent_shared3.isErrorWithCode)(error) ? error.code : ErrorCode.INTERNAL_ERROR;
|
|
572
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
573
|
+
return new Response(
|
|
574
|
+
JSON.stringify({
|
|
575
|
+
error: {
|
|
576
|
+
code: errorCode,
|
|
577
|
+
message: errorMessage
|
|
578
|
+
},
|
|
579
|
+
requestId
|
|
580
|
+
}),
|
|
581
|
+
{
|
|
582
|
+
status: 500,
|
|
583
|
+
headers: { "Content-Type": "application/json" }
|
|
584
|
+
}
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
const { result: events } = eventsResult;
|
|
588
|
+
let heartbeat;
|
|
361
589
|
const stream = new ReadableStream({
|
|
362
590
|
async start(controller) {
|
|
363
|
-
var _a3, _b2, _c2, _d2;
|
|
591
|
+
var _a3, _b2, _c2, _d2, _e2;
|
|
364
592
|
const encoder = new TextEncoder();
|
|
365
593
|
heartbeat = setInterval(() => {
|
|
366
594
|
controller.enqueue(encoder.encode(":ping\n\n"));
|
|
@@ -383,9 +611,12 @@ function createServerAdapter2(createAgent, options) {
|
|
|
383
611
|
(_c2 = logger.info) == null ? void 0 : _c2.call(logger, { eventCount }, "Request completed");
|
|
384
612
|
} catch (error) {
|
|
385
613
|
(_d2 = logger.error) == null ? void 0 : _d2.call(logger, { err: error }, "Stream error");
|
|
614
|
+
const errorCode = (0, import_agent_shared3.isErrorWithCode)(error) ? error.code : ErrorCode.INTERNAL_ERROR;
|
|
615
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
386
616
|
const errorEvent = {
|
|
387
617
|
type: import_client2.EventType.RUN_ERROR,
|
|
388
|
-
|
|
618
|
+
code: errorCode,
|
|
619
|
+
message: errorMessage
|
|
389
620
|
};
|
|
390
621
|
controller.enqueue(
|
|
391
622
|
encoder.encode(`data: ${JSON.stringify(errorEvent)}
|
|
@@ -396,13 +627,21 @@ function createServerAdapter2(createAgent, options) {
|
|
|
396
627
|
if (heartbeat) clearInterval(heartbeat);
|
|
397
628
|
controller.close();
|
|
398
629
|
safeCleanup();
|
|
630
|
+
if (serverSpan) {
|
|
631
|
+
serverSpan.end();
|
|
632
|
+
(_e2 = logger.debug) == null ? void 0 : _e2.call(logger, "\u2713 Server span ended");
|
|
633
|
+
}
|
|
399
634
|
}
|
|
400
635
|
},
|
|
401
636
|
cancel() {
|
|
402
|
-
var _a3;
|
|
637
|
+
var _a3, _b2;
|
|
403
638
|
(_a3 = logger.info) == null ? void 0 : _a3.call(logger, "Request cancelled by client");
|
|
404
639
|
if (heartbeat) clearInterval(heartbeat);
|
|
405
640
|
safeCleanup();
|
|
641
|
+
if (serverSpan) {
|
|
642
|
+
serverSpan.end();
|
|
643
|
+
(_b2 = logger.debug) == null ? void 0 : _b2.call(logger, "\u2713 Server span ended (cancelled)");
|
|
644
|
+
}
|
|
406
645
|
}
|
|
407
646
|
});
|
|
408
647
|
const headers = new Headers({
|
|
@@ -422,6 +661,15 @@ async function safeAsync(fn) {
|
|
|
422
661
|
return { error };
|
|
423
662
|
}
|
|
424
663
|
}
|
|
664
|
+
function safe(fn) {
|
|
665
|
+
try {
|
|
666
|
+
return {
|
|
667
|
+
result: fn()
|
|
668
|
+
};
|
|
669
|
+
} catch (error) {
|
|
670
|
+
return { error };
|
|
671
|
+
}
|
|
672
|
+
}
|
|
425
673
|
|
|
426
674
|
// src/agui/healthz/index.ts
|
|
427
675
|
var healthz_exports = {};
|
|
@@ -620,6 +868,13 @@ var import_cors = __toESM(require("cors"));
|
|
|
620
868
|
var import_async_hooks = require("async_hooks");
|
|
621
869
|
var import_server8 = require("@whatwg-node/server");
|
|
622
870
|
var DefaultFetchAPI = __toESM(require("@whatwg-node/fetch"));
|
|
871
|
+
async function setupObservabilityIfAvailable(configs) {
|
|
872
|
+
try {
|
|
873
|
+
const { setupObservability: setupObservability2 } = await import("@cloudbase/agent-observability/server");
|
|
874
|
+
await setupObservability2(configs);
|
|
875
|
+
} catch (error) {
|
|
876
|
+
}
|
|
877
|
+
}
|
|
623
878
|
var preparedAgentStorage = new import_async_hooks.AsyncLocalStorage();
|
|
624
879
|
function agentCloneFn() {
|
|
625
880
|
const preparedAgent = preparedAgentStorage.getStore();
|
|
@@ -654,7 +909,8 @@ function createExpressRoutes({
|
|
|
654
909
|
express,
|
|
655
910
|
useAGUI: _useAGUI,
|
|
656
911
|
aguiOptions,
|
|
657
|
-
logger: _logger
|
|
912
|
+
logger: _logger,
|
|
913
|
+
observability
|
|
658
914
|
}) {
|
|
659
915
|
var _a, _b, _c;
|
|
660
916
|
const useAGUI = _useAGUI ?? true;
|
|
@@ -662,6 +918,10 @@ function createExpressRoutes({
|
|
|
662
918
|
const basePath = _basePath ?? (process.env.TENCENTCLOUD_RUNENV === "SCF" ? "/v1/aibot/bots/:agentId/" : "/");
|
|
663
919
|
const serverLogger = ((_a = logger.child) == null ? void 0 : _a.call(logger, { component: "server" })) ?? logger;
|
|
664
920
|
(_b = serverLogger.debug) == null ? void 0 : _b.call(serverLogger, { basePath, useAGUI }, "Initializing server routes");
|
|
921
|
+
if (observability) {
|
|
922
|
+
setupObservabilityIfAvailable(observability).catch(() => {
|
|
923
|
+
});
|
|
924
|
+
}
|
|
665
925
|
const sendMessageServerAdapter = useAGUI ? sendMessageAGUI_exports.createServerAdapter(createAgent, { logger: serverLogger }) : sendMessage_exports.createServerAdapter(createAgent);
|
|
666
926
|
if (useAGUI) {
|
|
667
927
|
createAGUIRoute({
|
|
@@ -755,6 +1015,7 @@ function isCorsOptions(cors2) {
|
|
|
755
1015
|
}
|
|
756
1016
|
// Annotate the CommonJS export names for ESM import in node:
|
|
757
1017
|
0 && (module.exports = {
|
|
1018
|
+
ErrorCode,
|
|
758
1019
|
agui,
|
|
759
1020
|
createConsoleLogger,
|
|
760
1021
|
createExpressRoutes,
|
|
@@ -762,6 +1023,7 @@ function isCorsOptions(cors2) {
|
|
|
762
1023
|
extractRequestId,
|
|
763
1024
|
generateRequestId,
|
|
764
1025
|
getOrGenerateRequestId,
|
|
1026
|
+
isErrorWithCode,
|
|
765
1027
|
noopLogger,
|
|
766
1028
|
run
|
|
767
1029
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudbase/agent-server",
|
|
3
|
-
"version": "1.0.1-alpha.
|
|
3
|
+
"version": "1.0.1-alpha.9",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist/",
|
|
@@ -20,13 +20,19 @@
|
|
|
20
20
|
"express": "^5.1.0",
|
|
21
21
|
"openai": "6.3.0",
|
|
22
22
|
"uuid": "^10.0.0",
|
|
23
|
-
"
|
|
24
|
-
|
|
23
|
+
"@cloudbase/agent-shared": "^1.0.1-alpha.9"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
27
|
+
},
|
|
28
|
+
"optionalDependencies": {
|
|
29
|
+
"@cloudbase/agent-observability": "1.0.1-alpha.9"
|
|
25
30
|
},
|
|
26
31
|
"devDependencies": {
|
|
27
32
|
"@types/cors": "^2.8.19",
|
|
28
33
|
"@types/express": "^5.0.3",
|
|
29
|
-
"tsup": "^8.5.0"
|
|
34
|
+
"tsup": "^8.5.0",
|
|
35
|
+
"zod": "^4.0.0"
|
|
30
36
|
},
|
|
31
37
|
"scripts": {
|
|
32
38
|
"test": "echo \"Error: no test specified\" && exit 1",
|