@artinet/fleet 0.1.5 → 0.1.7
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/README.md +109 -230
- package/dist/default.d.ts +1 -0
- package/dist/default.js +1 -0
- package/dist/routes/create/index.d.ts +47 -46
- package/dist/routes/create/index.js +1 -1
- package/dist/routes/request/implementation/load.d.ts +1 -1
- package/dist/routes/request/implementation/load.js +17 -20
- package/dist/routes/request/types/definitions.d.ts +55 -53
- package/dist/routes/request/types/definitions.js +5 -7
- package/dist/server/express/agent-request.d.ts +6 -15
- package/dist/server/express/agent-request.js +33 -29
- package/dist/server/express/deploy-request.d.ts +6 -14
- package/dist/server/express/deploy-request.js +18 -18
- package/dist/server/express/server.d.ts +14 -13
- package/dist/server/express/server.js +23 -49
- package/dist/server/express/test-request.d.ts +16 -14
- package/dist/server/express/test-request.js +25 -25
- package/dist/server/express/types.d.ts +10 -0
- package/dist/server/express/types.js +5 -0
- package/dist/server/hono/agent-request.d.ts +6 -14
- package/dist/server/hono/agent-request.js +25 -21
- package/dist/server/hono/deploy-request.d.ts +6 -13
- package/dist/server/hono/deploy-request.js +14 -13
- package/dist/server/hono/rpc.d.ts +9 -11
- package/dist/server/hono/rpc.js +19 -20
- package/dist/server/hono/server.d.ts +15 -14
- package/dist/server/hono/server.js +20 -44
- package/dist/server/hono/test-request.d.ts +6 -13
- package/dist/server/hono/test-request.js +26 -26
- package/dist/server/hono/types.d.ts +9 -0
- package/dist/server/hono/types.js +5 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +13 -0
- package/package.json +108 -107
|
@@ -2,12 +2,8 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
export declare const AGENT_FIELD_NAME = "agentId";
|
|
8
|
-
export type handler = (req: express.Request, res: express.Response, next: express.NextFunction, context: RequestContext, request?: RequestAgentRoute["implementation"], intercepts?: RequestAgentRoute["intercept"][]) => Promise<void>;
|
|
9
|
-
export declare function handle(req: express.Request, res: express.Response, _next: express.NextFunction, context: RequestContext, request?: RequestAgentRoute["implementation"], intercepts?: RequestAgentRoute["intercept"][]): Promise<void>;
|
|
10
|
-
export declare const factory: (request?: RequestAgentRoute["implementation"], intercepts?: RequestAgentRoute["intercept"][]) => handler;
|
|
5
|
+
import { RequestAgentMount } from '../../routes/request/index.js';
|
|
6
|
+
import { Session } from './types.js';
|
|
11
7
|
/**
|
|
12
8
|
* Handler utilities for agent HTTP requests.
|
|
13
9
|
*
|
|
@@ -19,12 +15,7 @@ export declare const factory: (request?: RequestAgentRoute["implementation"], in
|
|
|
19
15
|
*
|
|
20
16
|
* @module server/handlers/agent
|
|
21
17
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
context: Omit<RequestContext, "agentId">;
|
|
27
|
-
handler: handler;
|
|
28
|
-
user: (request: express.Request) => Promise<string>;
|
|
29
|
-
}
|
|
30
|
-
export declare function request({ request: req, response: res, next, context, handler, user, }: Params): Promise<void>;
|
|
18
|
+
export type Mount = RequestAgentMount<Session>;
|
|
19
|
+
export declare const factory: Mount['factory'];
|
|
20
|
+
export declare const handle: Mount['handler'];
|
|
21
|
+
export declare const request: Mount['request'];
|
|
@@ -2,50 +2,54 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import { INVALID_REQUEST } from
|
|
6
|
-
import { RequestAgent
|
|
7
|
-
import * as sdk from
|
|
8
|
-
import { handleJSONRPCResponse } from
|
|
9
|
-
import { generateRequestId } from
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
|
|
5
|
+
import { INVALID_REQUEST } from '@artinet/sdk';
|
|
6
|
+
import { RequestAgent } from '../../routes/request/index.js';
|
|
7
|
+
import * as sdk from '@artinet/sdk';
|
|
8
|
+
import { handleJSONRPCResponse } from './rpc.js';
|
|
9
|
+
import { generateRequestId } from './utils.js';
|
|
10
|
+
import { AGENT_FIELD_NAME } from '../../default.js';
|
|
11
|
+
export const factory = ({ implementation = RequestAgent, }) => {
|
|
12
|
+
return async (params) => {
|
|
13
|
+
return await handle(params, implementation);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export const handle = async ({ session: { request, response }, context, intercepts }, implementation = RequestAgent) => {
|
|
17
|
+
const requestId = generateRequestId(context, request);
|
|
13
18
|
let parsed;
|
|
14
|
-
if (
|
|
15
|
-
req?.path?.endsWith("agent.json")) {
|
|
19
|
+
if (request?.path?.endsWith('agent-card.json') || request?.path?.endsWith('agent.json')) {
|
|
16
20
|
parsed = {
|
|
17
|
-
jsonrpc:
|
|
21
|
+
jsonrpc: '2.0',
|
|
18
22
|
id: requestId,
|
|
19
|
-
method:
|
|
23
|
+
method: 'agentcard/get',
|
|
20
24
|
params: null,
|
|
21
25
|
};
|
|
22
26
|
}
|
|
23
27
|
else {
|
|
24
|
-
parsed = await sdk.validateSchema(sdk.A2A.A2ARequestSchema,
|
|
28
|
+
parsed = await sdk.validateSchema(sdk.A2A.A2ARequestSchema, request?.body ?? {});
|
|
25
29
|
}
|
|
26
30
|
const params = await sdk.validateSchema(sdk.A2A.RequestParamSchema, parsed.params);
|
|
27
|
-
const
|
|
31
|
+
const req = {
|
|
28
32
|
method: parsed.method,
|
|
29
33
|
params: params,
|
|
30
34
|
};
|
|
31
|
-
sdk.logger.info(`handle agent request received:${parsed.method}:params:${sdk.formatJson(
|
|
32
|
-
const
|
|
33
|
-
sdk.logger.info(`handle agent request completed:${parsed.method}:response:${sdk.formatJson(
|
|
34
|
-
await handleJSONRPCResponse(
|
|
35
|
-
}
|
|
36
|
-
export const
|
|
37
|
-
|
|
38
|
-
const agentId = Array.isArray(
|
|
39
|
-
?
|
|
40
|
-
:
|
|
35
|
+
sdk.logger.info(`handle agent request received:${parsed.method}:params:${sdk.formatJson(req)}`);
|
|
36
|
+
const res = await implementation(req, context, intercepts);
|
|
37
|
+
sdk.logger.info(`handle agent request completed:${parsed.method}:response:${sdk.formatJson(res)}`);
|
|
38
|
+
await handleJSONRPCResponse(response, requestId, parsed.method, res);
|
|
39
|
+
};
|
|
40
|
+
export const request = async ({ session, context, handler = handle, user, intercepts }, implementation = RequestAgent) => {
|
|
41
|
+
const { request, next } = session;
|
|
42
|
+
const agentId = Array.isArray(request?.params?.[AGENT_FIELD_NAME])
|
|
43
|
+
? request?.params?.[AGENT_FIELD_NAME][0]
|
|
44
|
+
: request?.params?.[AGENT_FIELD_NAME];
|
|
41
45
|
if (!agentId) {
|
|
42
46
|
return next(INVALID_REQUEST({ message: `${AGENT_FIELD_NAME} is required` }));
|
|
43
47
|
}
|
|
44
|
-
const
|
|
48
|
+
const _context = {
|
|
45
49
|
...context,
|
|
46
50
|
agentId,
|
|
47
|
-
requestId: generateRequestId(context,
|
|
48
|
-
userId: await user?.(
|
|
51
|
+
requestId: generateRequestId(context, request),
|
|
52
|
+
userId: await user?.(session),
|
|
49
53
|
};
|
|
50
|
-
await handler(
|
|
51
|
-
}
|
|
54
|
+
return await handler({ session, context: _context, intercepts }, implementation);
|
|
55
|
+
};
|
|
@@ -2,17 +2,9 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
export type
|
|
8
|
-
export declare
|
|
9
|
-
export declare const factory:
|
|
10
|
-
export
|
|
11
|
-
request: express.Request;
|
|
12
|
-
response: express.Response;
|
|
13
|
-
next: express.NextFunction;
|
|
14
|
-
context: CreateAgentRoute["context"];
|
|
15
|
-
handler: handler;
|
|
16
|
-
user: (req: express.Request) => Promise<string>;
|
|
17
|
-
}
|
|
18
|
-
export declare function request({ request: req, response: res, next, context, handler, user, }: Params): Promise<void>;
|
|
5
|
+
import { CreateAgentMount } from '../../routes/create/index.js';
|
|
6
|
+
import { Session } from './types.js';
|
|
7
|
+
export type Mount = CreateAgentMount<Session>;
|
|
8
|
+
export declare const handle: Mount['handler'];
|
|
9
|
+
export declare const factory: Mount['factory'];
|
|
10
|
+
export declare const request: Mount['request'];
|
|
@@ -2,23 +2,23 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import { CreateAgent, CreateAgentRequestSchema, } from
|
|
6
|
-
import { generateRequestId, generateRegistrationId } from
|
|
7
|
-
import { logger, validateSchema, formatJson } from
|
|
8
|
-
export async
|
|
9
|
-
const
|
|
10
|
-
logger.info(`deploying agent: ${
|
|
11
|
-
logger.debug(`deploying agent: ${formatJson(
|
|
12
|
-
context.registrationId = generateRegistrationId(
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
export const factory = (
|
|
17
|
-
export
|
|
18
|
-
const
|
|
5
|
+
import { CreateAgent, CreateAgentRequestSchema, } from '../../routes/create/index.js';
|
|
6
|
+
import { generateRequestId, generateRegistrationId } from './utils.js';
|
|
7
|
+
import { logger, validateSchema, formatJson } from '@artinet/sdk';
|
|
8
|
+
export const handle = async ({ session: { request, response }, context, intercepts }, implementation = CreateAgent) => {
|
|
9
|
+
const req = await validateSchema(CreateAgentRequestSchema, request?.body ?? {});
|
|
10
|
+
logger.info(`deploying agent: ${req.config.name}`);
|
|
11
|
+
logger.debug(`deploying agent: ${formatJson(req)}`);
|
|
12
|
+
context.registrationId = generateRegistrationId(req.config.uri);
|
|
13
|
+
const res = await implementation(req, context, intercepts);
|
|
14
|
+
response.json(res);
|
|
15
|
+
};
|
|
16
|
+
export const factory = ({ implementation = CreateAgent }) => async (params) => await handle(params, implementation);
|
|
17
|
+
export const request = async ({ session, context, handler = handle, user, intercepts }, implementation = CreateAgent) => {
|
|
18
|
+
const _context = {
|
|
19
19
|
...context,
|
|
20
|
-
requestId: generateRequestId(context,
|
|
21
|
-
userId: await user?.(
|
|
20
|
+
requestId: generateRequestId(context, session.request),
|
|
21
|
+
userId: await user?.(session),
|
|
22
22
|
};
|
|
23
|
-
return await handler(
|
|
24
|
-
}
|
|
23
|
+
return await handler({ session, context: _context, intercepts }, implementation);
|
|
24
|
+
};
|
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import { Server } from
|
|
6
|
-
import express from
|
|
7
|
-
import { CreateAgentRoute } from
|
|
8
|
-
import { Settings as FleetSettings } from
|
|
9
|
-
import * as agent from
|
|
10
|
-
import * as testing from
|
|
11
|
-
import * as deployment from
|
|
5
|
+
import { Server } from 'http';
|
|
6
|
+
import express from 'express';
|
|
7
|
+
import { CreateAgentRoute } from '../../routes/create/index.js';
|
|
8
|
+
import { Settings as FleetSettings } from '../../settings.js';
|
|
9
|
+
import * as agent from './agent-request.js';
|
|
10
|
+
import * as testing from './test-request.js';
|
|
11
|
+
import * as deployment from './deploy-request.js';
|
|
12
|
+
import { Session } from './types.js';
|
|
12
13
|
/**
|
|
13
14
|
* Extended settings for the Express Fleet server.
|
|
14
15
|
*
|
|
@@ -19,13 +20,13 @@ import * as deployment from "./deploy-request.js";
|
|
|
19
20
|
*/
|
|
20
21
|
export type Settings = FleetSettings & {
|
|
21
22
|
/** Extracts the user ID from an incoming request. Used for multi-tenant agent isolation. */
|
|
22
|
-
user?: (
|
|
23
|
+
user?: (session: Session) => Promise<string>;
|
|
23
24
|
/** Handler for agent retrieval requests. Generated via {@link agent.factory}. */
|
|
24
|
-
retrieve?: agent.handler;
|
|
25
|
+
retrieve?: agent.Mount['handler'];
|
|
25
26
|
/** Handler for agent deployment requests. Generated via {@link deployment.factory}. */
|
|
26
|
-
deploy?: deployment.handler;
|
|
27
|
+
deploy?: deployment.Mount['handler'];
|
|
27
28
|
/** Handler for agent test/evaluation requests. Generated via {@link testing.factory}. */
|
|
28
|
-
evaluate?: testing.handler;
|
|
29
|
+
evaluate?: testing.Mount['handler'];
|
|
29
30
|
/**
|
|
30
31
|
* Authentication middleware applied to protected routes.
|
|
31
32
|
* @see {@link https://expressjs.com/en/guide/writing-middleware.html Writing Middleware}
|
|
@@ -80,10 +81,10 @@ export interface Options {
|
|
|
80
81
|
* server.launch(8080);
|
|
81
82
|
* ```
|
|
82
83
|
*/
|
|
83
|
-
export declare function fleet(settings?: Partial<Settings>, { app, authOnRetrieve, enableTesting
|
|
84
|
+
export declare function fleet(settings?: Partial<Settings>, { app, authOnRetrieve, enableTesting }?: Options): {
|
|
84
85
|
app: express.Application;
|
|
85
86
|
launch: (port: number) => Server;
|
|
86
|
-
ship: (agents: CreateAgentRoute[
|
|
87
|
+
ship: (agents: CreateAgentRoute['request'][], userId?: string) => Promise<{
|
|
87
88
|
launch: (port?: number) => Server;
|
|
88
89
|
}>;
|
|
89
90
|
};
|
|
@@ -2,25 +2,21 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import * as sdk from
|
|
6
|
-
import express from
|
|
7
|
-
import { DEFAULTS } from
|
|
8
|
-
import * as agent from
|
|
9
|
-
import * as testing from
|
|
10
|
-
import * as deployment from
|
|
11
|
-
import { AGENT_FIELD_NAME } from
|
|
5
|
+
import * as sdk from '@artinet/sdk';
|
|
6
|
+
import express from 'express';
|
|
7
|
+
import { DEFAULTS } from '../../default.js';
|
|
8
|
+
import * as agent from './agent-request.js';
|
|
9
|
+
import * as testing from './test-request.js';
|
|
10
|
+
import * as deployment from './deploy-request.js';
|
|
11
|
+
import { AGENT_FIELD_NAME } from '../../default.js';
|
|
12
12
|
const createContext = (settings) => {
|
|
13
13
|
const _settings = {
|
|
14
14
|
...DEFAULTS,
|
|
15
15
|
...settings,
|
|
16
|
-
retrieve: agent.factory(settings.get ?? DEFAULTS.get,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
evaluate: testing.factory(settings.test ?? DEFAULTS.test),
|
|
21
|
-
user: settings.user
|
|
22
|
-
? settings.user
|
|
23
|
-
: (_req) => Promise.resolve(settings.userId ?? "default"),
|
|
16
|
+
retrieve: agent.factory({ implementation: settings.get ?? DEFAULTS.get }),
|
|
17
|
+
deploy: deployment.factory({ implementation: settings.set ?? DEFAULTS.set }),
|
|
18
|
+
evaluate: testing.factory({ implementation: settings.test ?? DEFAULTS.test }),
|
|
19
|
+
user: settings.user ? settings.user : (_session) => Promise.resolve(settings.userId ?? 'default'),
|
|
24
20
|
};
|
|
25
21
|
return _settings;
|
|
26
22
|
};
|
|
@@ -74,9 +70,9 @@ const createRequestContext = (context) => {
|
|
|
74
70
|
* server.launch(8080);
|
|
75
71
|
* ```
|
|
76
72
|
*/
|
|
77
|
-
export function fleet(settings = DEFAULTS, { app = express(), authOnRetrieve = false, enableTesting = true
|
|
73
|
+
export function fleet(settings = DEFAULTS, { app = express(), authOnRetrieve = false, enableTesting = true } = {}) {
|
|
78
74
|
const context = createContext(settings);
|
|
79
|
-
const { basePath, agentPath, fallbackPath, deploymentPath, testPath, auth, user, evaluate, deploy, retrieve, set
|
|
75
|
+
const { basePath, agentPath, fallbackPath, deploymentPath, testPath, auth, user, evaluate, deploy, retrieve, set } = context;
|
|
80
76
|
const router = express.Router();
|
|
81
77
|
router.use(express.json());
|
|
82
78
|
if (auth) {
|
|
@@ -88,38 +84,32 @@ export function fleet(settings = DEFAULTS, { app = express(), authOnRetrieve = f
|
|
|
88
84
|
}
|
|
89
85
|
}
|
|
90
86
|
if (enableTesting === true && evaluate !== undefined) {
|
|
91
|
-
router.post(testPath, async (
|
|
92
|
-
|
|
93
|
-
response: res,
|
|
94
|
-
next,
|
|
87
|
+
router.post(testPath, async (request, response, next) => await testing.request({
|
|
88
|
+
session: { request, response, next },
|
|
95
89
|
context: createRequestContext(context),
|
|
96
90
|
handler: evaluate,
|
|
97
91
|
user,
|
|
98
92
|
}), sdk.errorHandler);
|
|
99
93
|
}
|
|
100
|
-
router.post(deploymentPath, async (
|
|
101
|
-
|
|
102
|
-
response: res,
|
|
103
|
-
next,
|
|
94
|
+
router.post(deploymentPath, async (request, response, next) => await deployment.request({
|
|
95
|
+
session: { request, response, next },
|
|
104
96
|
context: createRequestContext(context),
|
|
105
97
|
handler: deploy,
|
|
106
98
|
user,
|
|
107
99
|
}));
|
|
108
|
-
router.use(`${agentPath}/:${AGENT_FIELD_NAME}`, async (
|
|
109
|
-
|
|
110
|
-
response: res,
|
|
111
|
-
next,
|
|
100
|
+
router.use(`${agentPath}/:${AGENT_FIELD_NAME}`, async (request, response, next) => await agent.request({
|
|
101
|
+
session: { request, response, next },
|
|
112
102
|
context: createRequestContext(context),
|
|
113
103
|
handler: retrieve,
|
|
114
104
|
user,
|
|
105
|
+
intercepts: settings.middleware?.build() ?? [],
|
|
115
106
|
}), sdk.errorHandler);
|
|
116
|
-
router.use(`${fallbackPath}/:${AGENT_FIELD_NAME}`, async (
|
|
117
|
-
|
|
118
|
-
response: res,
|
|
119
|
-
next,
|
|
107
|
+
router.use(`${fallbackPath}/:${AGENT_FIELD_NAME}`, async (request, response, next) => await agent.request({
|
|
108
|
+
session: { request, response, next },
|
|
120
109
|
context: createRequestContext(context),
|
|
121
110
|
handler: retrieve,
|
|
122
111
|
user,
|
|
112
|
+
intercepts: settings.middleware?.build() ?? [],
|
|
123
113
|
}), sdk.errorHandler);
|
|
124
114
|
app.use(basePath, router);
|
|
125
115
|
const launch = (port = 3000) => {
|
|
@@ -141,19 +131,3 @@ export function fleet(settings = DEFAULTS, { app = express(), authOnRetrieve = f
|
|
|
141
131
|
};
|
|
142
132
|
return { app, launch, ship };
|
|
143
133
|
}
|
|
144
|
-
// const swarm = await fleet().ship([
|
|
145
|
-
// {
|
|
146
|
-
// config: {
|
|
147
|
-
// uri: "my-agent",
|
|
148
|
-
// name: "my-agent",
|
|
149
|
-
// description: "A helpful assistant",
|
|
150
|
-
// modelId: "gpt-4",
|
|
151
|
-
// instructions: "You are a helpful assistant.",
|
|
152
|
-
// version: "1.0.0",
|
|
153
|
-
// skills: [],
|
|
154
|
-
// capabilities: {},
|
|
155
|
-
// services: [],
|
|
156
|
-
// },
|
|
157
|
-
// },
|
|
158
|
-
// ]);
|
|
159
|
-
// swarm.launch(3000);
|
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export declare
|
|
5
|
+
import { TestAgentMount } from '../../routes/request/index.js';
|
|
6
|
+
import { Session } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Handler utilities for agent test/evaluation requests.
|
|
9
|
+
*
|
|
10
|
+
* Exports a reusable handler function for routing test/evaluation requests to agent instances,
|
|
11
|
+
* validating the request body, and returning the result as a JSON-RPC response.
|
|
12
|
+
*
|
|
13
|
+
* Used by the deployment server to provide a standard agent test/evaluation endpoint interface.
|
|
14
|
+
*
|
|
15
|
+
* @module server/handlers/test
|
|
16
|
+
*/
|
|
17
|
+
export type Mount = TestAgentMount<Session>;
|
|
18
|
+
export declare const factory: Mount['factory'];
|
|
19
|
+
export declare const handle: Mount['handler'];
|
|
20
|
+
export declare const request: Mount['request'];
|
|
@@ -2,25 +2,13 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import * as sdk from
|
|
6
|
-
import { TestAgent, TestRequestSchema, } from
|
|
7
|
-
import { v4 as uuidv4 } from
|
|
8
|
-
import { handleJSONRPCResponse } from
|
|
9
|
-
import { generateRequestId } from
|
|
10
|
-
export async function handle(req, res, _next, context, test = TestAgent) {
|
|
11
|
-
let parsed = await sdk.validateSchema(TestRequestSchema, req?.body ?? {});
|
|
12
|
-
let id = parsed.id ?? uuidv4();
|
|
13
|
-
parsed.id = id;
|
|
14
|
-
let request = parsed;
|
|
15
|
-
context.target = parsed.config;
|
|
16
|
-
request.method = "test/invoke";
|
|
17
|
-
request.params = null;
|
|
18
|
-
const response = await test(request, context);
|
|
19
|
-
await handleJSONRPCResponse(res, String(id), request.method, response);
|
|
20
|
-
}
|
|
21
|
-
export const factory = (test = TestAgent) => async (req, res, next, context) => await handle(req, res, next, context, test);
|
|
5
|
+
import * as sdk from '@artinet/sdk';
|
|
6
|
+
import { TestAgent, TestRequestSchema, } from '../../routes/request/index.js';
|
|
7
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
8
|
+
import { handleJSONRPCResponse } from './rpc.js';
|
|
9
|
+
import { generateRequestId } from './utils.js';
|
|
22
10
|
const MAX_TEST_ID_ATTEMPTS = 10;
|
|
23
|
-
const
|
|
11
|
+
const generateTestId = async (context) => {
|
|
24
12
|
let testId = uuidv4();
|
|
25
13
|
let free = false;
|
|
26
14
|
for (let i = 0; i < MAX_TEST_ID_ATTEMPTS; i++) {
|
|
@@ -40,12 +28,24 @@ const getTestId = async (context) => {
|
|
|
40
28
|
}
|
|
41
29
|
return testId;
|
|
42
30
|
};
|
|
43
|
-
export
|
|
44
|
-
|
|
31
|
+
export const factory = ({ implementation = TestAgent }) => async (params) => await handle(params, implementation);
|
|
32
|
+
export const handle = async ({ session: { request, response }, context }, implementation = TestAgent) => {
|
|
33
|
+
let parsed = await sdk.validateSchema(TestRequestSchema, request?.body ?? {});
|
|
34
|
+
let id = parsed.id ?? uuidv4();
|
|
35
|
+
parsed.id = id;
|
|
36
|
+
let req = parsed;
|
|
37
|
+
context.target = parsed.config;
|
|
38
|
+
req.method = 'test/invoke';
|
|
39
|
+
req.params = null;
|
|
40
|
+
const res = await implementation(req, context);
|
|
41
|
+
return await handleJSONRPCResponse(response, String(id), req.method, res);
|
|
42
|
+
};
|
|
43
|
+
export const request = async ({ session, context, handler = handle, user, intercepts }, implementation = TestAgent) => {
|
|
44
|
+
const _context = {
|
|
45
45
|
...context,
|
|
46
|
-
agentId: await
|
|
47
|
-
requestId: generateRequestId(context,
|
|
48
|
-
userId: await user?.(
|
|
46
|
+
agentId: await generateTestId(context),
|
|
47
|
+
requestId: generateRequestId(context, session.request),
|
|
48
|
+
userId: await user?.(session),
|
|
49
49
|
};
|
|
50
|
-
return await handler(
|
|
51
|
-
}
|
|
50
|
+
return await handler({ session, context: _context, intercepts }, implementation);
|
|
51
|
+
};
|
|
@@ -2,12 +2,8 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
export declare const AGENT_FIELD_NAME = "agentId";
|
|
8
|
-
export type handler = (ctx: hono.Context, next: hono.Next, context: RequestContext, request?: RequestAgentRoute["implementation"], intercepts?: RequestAgentRoute["intercept"][]) => Promise<void>;
|
|
9
|
-
export declare function handle(ctx: hono.Context, _next: hono.Next, context: RequestContext, request?: RequestAgentRoute["implementation"], intercepts?: RequestAgentRoute["intercept"][]): Promise<void>;
|
|
10
|
-
export declare const factory: (request?: RequestAgentRoute["implementation"], intercepts?: RequestAgentRoute["intercept"][]) => handler;
|
|
5
|
+
import { RequestAgentMount } from '../../routes/request/index.js';
|
|
6
|
+
import { Session } from './types.js';
|
|
11
7
|
/**
|
|
12
8
|
* Handler utilities for agent HTTP requests.
|
|
13
9
|
*
|
|
@@ -19,11 +15,7 @@ export declare const factory: (request?: RequestAgentRoute["implementation"], in
|
|
|
19
15
|
*
|
|
20
16
|
* @module server/handlers/agent
|
|
21
17
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
handler: handler;
|
|
27
|
-
user: (ctx: hono.Context) => Promise<string>;
|
|
28
|
-
}
|
|
29
|
-
export declare function request({ ctx, next, context, handler, user, }: Params): Promise<void>;
|
|
18
|
+
export type Mount = RequestAgentMount<Session>;
|
|
19
|
+
export declare const handle: Mount['handler'];
|
|
20
|
+
export declare const factory: Mount['factory'];
|
|
21
|
+
export declare const request: Mount['request'];
|
|
@@ -2,23 +2,22 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import { INVALID_REQUEST } from
|
|
6
|
-
import { RequestAgent
|
|
7
|
-
import * as sdk from
|
|
8
|
-
import { handleJSONRPCResponse } from
|
|
9
|
-
import { generateRequestId } from
|
|
10
|
-
|
|
11
|
-
export async
|
|
5
|
+
import { INVALID_REQUEST } from '@artinet/sdk';
|
|
6
|
+
import { RequestAgent } from '../../routes/request/index.js';
|
|
7
|
+
import * as sdk from '@artinet/sdk';
|
|
8
|
+
import { handleJSONRPCResponse } from './rpc.js';
|
|
9
|
+
import { generateRequestId } from './utils.js';
|
|
10
|
+
import { AGENT_FIELD_NAME } from '../../default.js';
|
|
11
|
+
export const handle = async ({ session: { ctx }, context, intercepts }, implementation = RequestAgent) => {
|
|
12
12
|
/* hono.Context.req uses a raw JSON.parse() so we prefer to use the text() and our own safeParse() */
|
|
13
13
|
const body = sdk.safeParse(await ctx.req.text());
|
|
14
|
-
const requestId = generateRequestId(context, ctx.req.header(
|
|
14
|
+
const requestId = generateRequestId(context, ctx.req.header('x-request-id') ?? body?.id);
|
|
15
15
|
let parsed;
|
|
16
|
-
if (ctx.req.path.endsWith(
|
|
17
|
-
ctx.req.path.endsWith("agent.json")) {
|
|
16
|
+
if (ctx.req.path.endsWith('agent-card.json') || ctx.req.path.endsWith('agent.json')) {
|
|
18
17
|
parsed = {
|
|
19
|
-
jsonrpc:
|
|
18
|
+
jsonrpc: '2.0',
|
|
20
19
|
id: requestId,
|
|
21
|
-
method:
|
|
20
|
+
method: 'agentcard/get',
|
|
22
21
|
params: null,
|
|
23
22
|
};
|
|
24
23
|
}
|
|
@@ -31,23 +30,28 @@ export async function handle(ctx, _next, context, request = RequestAgent, interc
|
|
|
31
30
|
params: params,
|
|
32
31
|
};
|
|
33
32
|
sdk.logger.info(`handle agent request received:${parsed.method}:params:${sdk.formatJson(agentRequest)}`);
|
|
34
|
-
const response = await
|
|
33
|
+
const response = await implementation(agentRequest, context, intercepts);
|
|
35
34
|
sdk.logger.info(`handle agent request completed:${parsed.method}:response:${sdk.formatJson(response)}`);
|
|
36
35
|
await handleJSONRPCResponse(ctx, requestId, parsed.method, response);
|
|
37
|
-
}
|
|
38
|
-
export const factory = (
|
|
39
|
-
|
|
36
|
+
};
|
|
37
|
+
export const factory = ({ implementation = RequestAgent, }) => {
|
|
38
|
+
return async (params) => {
|
|
39
|
+
return await handle(params, implementation);
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export const request = async ({ session, context, handler = handle, user, intercepts }, implementation = RequestAgent) => {
|
|
43
|
+
const { ctx } = session;
|
|
40
44
|
const agentId = ctx.req.param(AGENT_FIELD_NAME);
|
|
41
45
|
if (!agentId) {
|
|
42
46
|
throw INVALID_REQUEST({ message: `${AGENT_FIELD_NAME} is required` });
|
|
43
47
|
}
|
|
44
|
-
/* hono.Context.req uses a raw JSON.parse() so we prefer to use
|
|
45
|
-
const reqId = ctx.req.header(
|
|
48
|
+
/* hono.Context.req uses a raw JSON.parse() so we prefer to use text() and our own safeParse() */
|
|
49
|
+
const reqId = ctx.req.header('x-request-id') ?? sdk.safeParse(await ctx.req.text())?.id;
|
|
46
50
|
const requestContext = {
|
|
47
51
|
...context,
|
|
48
52
|
agentId,
|
|
49
53
|
requestId: generateRequestId(context, reqId),
|
|
50
|
-
userId: await user?.(
|
|
54
|
+
userId: await user?.(session),
|
|
51
55
|
};
|
|
52
|
-
await handler(
|
|
53
|
-
}
|
|
56
|
+
return await handler({ session, context: requestContext, intercepts }, implementation);
|
|
57
|
+
};
|
|
@@ -2,16 +2,9 @@
|
|
|
2
2
|
* Copyright 2025 The Artinet Project
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
export type
|
|
8
|
-
export declare
|
|
9
|
-
export declare const
|
|
10
|
-
export
|
|
11
|
-
ctx: hono.Context;
|
|
12
|
-
next: hono.Next;
|
|
13
|
-
context: CreateAgentRoute["context"];
|
|
14
|
-
handler: handler;
|
|
15
|
-
user: (ctx: hono.Context) => Promise<string>;
|
|
16
|
-
}
|
|
17
|
-
export declare function request({ ctx, next, context, handler, user, }: Params): Promise<void>;
|
|
5
|
+
import { CreateAgentMount } from '../../routes/create/index.js';
|
|
6
|
+
import { Session } from './types.js';
|
|
7
|
+
export type Mount = CreateAgentMount<Session>;
|
|
8
|
+
export declare const factory: Mount['factory'];
|
|
9
|
+
export declare const handle: Mount['handler'];
|
|
10
|
+
export declare const request: Mount['request'];
|