@aigne/transport 0.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/CHANGELOG.md +23 -0
- package/LICENSE.md +93 -0
- package/README.md +162 -0
- package/README.zh.md +162 -0
- package/lib/cjs/http-client/client.d.ts +97 -0
- package/lib/cjs/http-client/client.js +87 -0
- package/lib/cjs/http-client/index.d.ts +1 -0
- package/lib/cjs/http-client/index.js +17 -0
- package/lib/cjs/http-server/error.d.ts +15 -0
- package/lib/cjs/http-server/error.js +22 -0
- package/lib/cjs/http-server/index.d.ts +2 -0
- package/lib/cjs/http-server/index.js +18 -0
- package/lib/cjs/http-server/server.d.ts +135 -0
- package/lib/cjs/http-server/server.js +187 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +2 -0
- package/lib/cjs/package.json +1 -0
- package/lib/dts/http-client/client.d.ts +97 -0
- package/lib/dts/http-client/index.d.ts +1 -0
- package/lib/dts/http-server/error.d.ts +15 -0
- package/lib/dts/http-server/index.d.ts +2 -0
- package/lib/dts/http-server/server.d.ts +135 -0
- package/lib/dts/index.d.ts +1 -0
- package/lib/esm/http-client/client.d.ts +97 -0
- package/lib/esm/http-client/client.js +83 -0
- package/lib/esm/http-client/index.d.ts +1 -0
- package/lib/esm/http-client/index.js +1 -0
- package/lib/esm/http-server/error.d.ts +15 -0
- package/lib/esm/http-server/error.js +18 -0
- package/lib/esm/http-server/index.d.ts +2 -0
- package/lib/esm/http-server/index.js +2 -0
- package/lib/esm/http-server/server.d.ts +135 -0
- package/lib/esm/http-server/server.js +180 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/package.json +1 -0
- package/package.json +66 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client module used to interact with the AIGNE framework.
|
|
3
|
+
*/
|
|
4
|
+
import type { AgentInvokeOptions, AgentResponse, AgentResponseStream, Message } from "@aigne/core";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the AIGNEHTTPClient.
|
|
7
|
+
*/
|
|
8
|
+
export interface AIGNEHTTPClientOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The URL of the AIGNE server to connect to.
|
|
11
|
+
* This should point to the base endpoint where the AIGNEServer is hosted.
|
|
12
|
+
*/
|
|
13
|
+
url: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Options for invoking an agent through the AIGNEHTTPClient.
|
|
17
|
+
* Extends the standard AgentInvokeOptions with client-specific options.
|
|
18
|
+
*/
|
|
19
|
+
export interface AIGNEHTTPClientInvokeOptions extends AgentInvokeOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Additional fetch API options to customize the HTTP request.
|
|
22
|
+
* These options will be merged with the default options used by the client.
|
|
23
|
+
*/
|
|
24
|
+
fetchOptions?: Partial<RequestInit>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Http client for interacting with a remote AIGNE server.
|
|
28
|
+
* AIGNEHTTPClient provides a client-side interface that matches the AIGNE API,
|
|
29
|
+
* allowing applications to invoke agents and receive responses from a remote AIGNE instance.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* Here's a simple example of how to use AIGNEClient:
|
|
33
|
+
* {@includeCode ../../test/http-client/http-client.test.ts#example-aigne-client-simple}
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* Here's an example of how to use AIGNEClient with streaming response:
|
|
37
|
+
* {@includeCode ../../test/http-client/http-client.test.ts#example-aigne-client-streaming}
|
|
38
|
+
*/
|
|
39
|
+
export declare class AIGNEHTTPClient {
|
|
40
|
+
options: AIGNEHTTPClientOptions;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a new AIGNEClient instance.
|
|
43
|
+
*
|
|
44
|
+
* @param options - Configuration options for connecting to the AIGNE server
|
|
45
|
+
*/
|
|
46
|
+
constructor(options: AIGNEHTTPClientOptions);
|
|
47
|
+
/**
|
|
48
|
+
* Invokes an agent in non-streaming mode and returns the complete response.
|
|
49
|
+
*
|
|
50
|
+
* @param agent - Name of the agent to invoke
|
|
51
|
+
* @param input - Input message for the agent
|
|
52
|
+
* @param options - Options with streaming mode explicitly set to false or omitted
|
|
53
|
+
* @returns The complete agent response
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* Here's a simple example of how to use AIGNEClient:
|
|
57
|
+
* {@includeCode ../../test/http-client/http-client.test.ts#example-aigne-client-simple}
|
|
58
|
+
*/
|
|
59
|
+
invoke<I extends Message, O extends Message>(agent: string, input: string | I, options?: AIGNEHTTPClientInvokeOptions & {
|
|
60
|
+
streaming?: false;
|
|
61
|
+
}): Promise<O>;
|
|
62
|
+
/**
|
|
63
|
+
* Invokes an agent with streaming mode enabled and returns a stream of response chunks.
|
|
64
|
+
*
|
|
65
|
+
* @param agent - Name of the agent to invoke
|
|
66
|
+
* @param input - Input message for the agent
|
|
67
|
+
* @param options - Options with streaming mode explicitly set to true
|
|
68
|
+
* @returns A stream of agent response chunks
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* Here's an example of how to use AIGNEClient with streaming response:
|
|
72
|
+
* {@includeCode ../../test/http-client/http-client.test.ts#example-aigne-client-streaming}
|
|
73
|
+
*/
|
|
74
|
+
invoke<I extends Message, O extends Message>(agent: string, input: string | I, options: AIGNEHTTPClientInvokeOptions & {
|
|
75
|
+
streaming: true;
|
|
76
|
+
}): Promise<AgentResponseStream<O>>;
|
|
77
|
+
/**
|
|
78
|
+
* Invokes an agent with the given input and options.
|
|
79
|
+
*
|
|
80
|
+
* @param agent - Name of the agent to invoke
|
|
81
|
+
* @param input - Input message for the agent
|
|
82
|
+
* @param options - Options for the invocation
|
|
83
|
+
* @returns Either a complete response or a response stream depending on the streaming option
|
|
84
|
+
*/
|
|
85
|
+
invoke<I extends Message, O extends Message>(agent: string, input: string | I, options?: AIGNEHTTPClientInvokeOptions): Promise<AgentResponse<O>>;
|
|
86
|
+
/**
|
|
87
|
+
* Enhanced fetch method that handles error responses from the AIGNE server.
|
|
88
|
+
* This method wraps the standard fetch API to provide better error handling and reporting.
|
|
89
|
+
*
|
|
90
|
+
* @param args - Standard fetch API arguments (url and options)
|
|
91
|
+
* @returns A Response object if the request was successful
|
|
92
|
+
* @throws Error with detailed information if the request failed
|
|
93
|
+
*
|
|
94
|
+
* @private
|
|
95
|
+
*/
|
|
96
|
+
fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response>;
|
|
97
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client module used to interact with the AIGNE framework.
|
|
3
|
+
*/
|
|
4
|
+
import { AgentResponseStreamParser, EventStreamParser } from "@aigne/core/utils/event-stream.js";
|
|
5
|
+
import { tryOrThrow } from "@aigne/core/utils/type-utils.js";
|
|
6
|
+
/**
|
|
7
|
+
* Http client for interacting with a remote AIGNE server.
|
|
8
|
+
* AIGNEHTTPClient provides a client-side interface that matches the AIGNE API,
|
|
9
|
+
* allowing applications to invoke agents and receive responses from a remote AIGNE instance.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* Here's a simple example of how to use AIGNEClient:
|
|
13
|
+
* {@includeCode ../../test/http-client/http-client.test.ts#example-aigne-client-simple}
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* Here's an example of how to use AIGNEClient with streaming response:
|
|
17
|
+
* {@includeCode ../../test/http-client/http-client.test.ts#example-aigne-client-streaming}
|
|
18
|
+
*/
|
|
19
|
+
export class AIGNEHTTPClient {
|
|
20
|
+
options;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new AIGNEClient instance.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Configuration options for connecting to the AIGNE server
|
|
25
|
+
*/
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.options = options;
|
|
28
|
+
}
|
|
29
|
+
async invoke(agent, input, options) {
|
|
30
|
+
// Send the agent invocation request to the AIGNE server
|
|
31
|
+
const response = await this.fetch(this.options.url, {
|
|
32
|
+
...options?.fetchOptions,
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: {
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
...options?.fetchOptions?.headers,
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify({ agent, input, options }),
|
|
39
|
+
});
|
|
40
|
+
// For non-streaming responses, simply parse the JSON response and return it
|
|
41
|
+
if (!options?.streaming) {
|
|
42
|
+
return await response.json();
|
|
43
|
+
}
|
|
44
|
+
// For streaming responses, set up the streaming pipeline
|
|
45
|
+
const stream = response.body;
|
|
46
|
+
if (!stream)
|
|
47
|
+
throw new Error("Response body is not a stream");
|
|
48
|
+
// Process the stream through a series of transforms:
|
|
49
|
+
// 1. Convert bytes to text
|
|
50
|
+
// 2. Parse SSE format into structured events
|
|
51
|
+
// 3. Convert events into a standardized agent response stream
|
|
52
|
+
return stream
|
|
53
|
+
.pipeThrough(new TextDecoderStream())
|
|
54
|
+
.pipeThrough(new EventStreamParser())
|
|
55
|
+
.pipeThrough(new AgentResponseStreamParser());
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Enhanced fetch method that handles error responses from the AIGNE server.
|
|
59
|
+
* This method wraps the standard fetch API to provide better error handling and reporting.
|
|
60
|
+
*
|
|
61
|
+
* @param args - Standard fetch API arguments (url and options)
|
|
62
|
+
* @returns A Response object if the request was successful
|
|
63
|
+
* @throws Error with detailed information if the request failed
|
|
64
|
+
*
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
async fetch(...args) {
|
|
68
|
+
const result = await globalThis.fetch(...args);
|
|
69
|
+
if (!result.ok) {
|
|
70
|
+
let message;
|
|
71
|
+
try {
|
|
72
|
+
const text = await result.text();
|
|
73
|
+
const json = tryOrThrow(() => JSON.parse(text));
|
|
74
|
+
message = json?.error?.message || text;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// ignore
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Failed to fetch url ${args[0]} with status ${result.status}: ${message}`);
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./client.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./client.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for AIGNEServer HTTP-related errors.
|
|
3
|
+
* Extends the standard Error class with an HTTP status code property.
|
|
4
|
+
* This allows error responses to include appropriate HTTP status codes.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ServerError extends Error {
|
|
7
|
+
status: number;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new ServerError instance.
|
|
10
|
+
*
|
|
11
|
+
* @param status - The HTTP status code for this error (e.g., 400, 404, 500)
|
|
12
|
+
* @param message - The error message describing what went wrong
|
|
13
|
+
*/
|
|
14
|
+
constructor(status: number, message: string);
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for AIGNEServer HTTP-related errors.
|
|
3
|
+
* Extends the standard Error class with an HTTP status code property.
|
|
4
|
+
* This allows error responses to include appropriate HTTP status codes.
|
|
5
|
+
*/
|
|
6
|
+
export class ServerError extends Error {
|
|
7
|
+
status;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new ServerError instance.
|
|
10
|
+
*
|
|
11
|
+
* @param status - The HTTP status code for this error (e.g., 400, 404, 500)
|
|
12
|
+
* @param message - The error message describing what went wrong
|
|
13
|
+
*/
|
|
14
|
+
constructor(status, message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.status = status;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { AIGNE } from "@aigne/core";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/**
|
|
5
|
+
* Schema for validating agent invocation payloads.
|
|
6
|
+
* Defines the expected structure for requests to invoke an agent.
|
|
7
|
+
*
|
|
8
|
+
* @hidden
|
|
9
|
+
*/
|
|
10
|
+
export declare const invokePayloadSchema: z.ZodObject<{
|
|
11
|
+
agent: z.ZodString;
|
|
12
|
+
input: z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
|
|
13
|
+
options: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
14
|
+
streaming: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
15
|
+
}, "strip", z.ZodTypeAny, {
|
|
16
|
+
streaming?: boolean | null | undefined;
|
|
17
|
+
}, {
|
|
18
|
+
streaming?: boolean | null | undefined;
|
|
19
|
+
}>>>;
|
|
20
|
+
}, "strip", z.ZodTypeAny, {
|
|
21
|
+
agent: string;
|
|
22
|
+
input: string | Record<string, unknown>;
|
|
23
|
+
options?: {
|
|
24
|
+
streaming?: boolean | null | undefined;
|
|
25
|
+
} | null | undefined;
|
|
26
|
+
}, {
|
|
27
|
+
agent: string;
|
|
28
|
+
input: string | Record<string, unknown>;
|
|
29
|
+
options?: {
|
|
30
|
+
streaming?: boolean | null | undefined;
|
|
31
|
+
} | null | undefined;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Configuration options for the AIGNEHTTPServer.
|
|
35
|
+
* These options control various aspects of server behavior including
|
|
36
|
+
* request parsing, payload limits, and response handling.
|
|
37
|
+
*/
|
|
38
|
+
export interface AIGNEHTTPServerOptions {
|
|
39
|
+
/**
|
|
40
|
+
* Maximum body size for incoming HTTP requests.
|
|
41
|
+
* This controls the upper limit of request payload size when parsing raw HTTP requests.
|
|
42
|
+
* Only used when working with Node.js IncomingMessage objects that don't already have
|
|
43
|
+
* a pre-parsed body property (e.g., when not using Express middleware).
|
|
44
|
+
*
|
|
45
|
+
* @default "4mb"
|
|
46
|
+
*/
|
|
47
|
+
maximumBodySize?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* AIGNEHTTPServer provides HTTP API access to AIGNE capabilities.
|
|
51
|
+
* It handles requests to invoke agents, manages response streaming,
|
|
52
|
+
* and supports multiple HTTP server frameworks including Node.js http,
|
|
53
|
+
* Express, and Fetch API compatible environments.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* Here's a simple example of how to use AIGNEServer with express:
|
|
57
|
+
* {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-express}
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* Here's an example of how to use AIGNEServer with Hono:
|
|
61
|
+
* {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-hono}
|
|
62
|
+
*/
|
|
63
|
+
export declare class AIGNEHTTPServer {
|
|
64
|
+
engine: AIGNE;
|
|
65
|
+
options?: AIGNEHTTPServerOptions | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Creates a new AIGNEServer instance.
|
|
68
|
+
*
|
|
69
|
+
* @param engine - The AIGNE engine instance that will process agent invocations
|
|
70
|
+
* @param options - Configuration options for the server
|
|
71
|
+
*/
|
|
72
|
+
constructor(engine: AIGNE, options?: AIGNEHTTPServerOptions | undefined);
|
|
73
|
+
/**
|
|
74
|
+
* Invokes an agent with the provided input and returns a standard web Response.
|
|
75
|
+
* This method serves as the primary API endpoint for agent invocation.
|
|
76
|
+
*
|
|
77
|
+
* The request can be provided in various formats to support different integration scenarios:
|
|
78
|
+
* - As a pre-parsed JavaScript object
|
|
79
|
+
* - As a Fetch API Request instance (for modern web frameworks)
|
|
80
|
+
* - As a Node.js IncomingMessage (for Express, Fastify, etc.)
|
|
81
|
+
*
|
|
82
|
+
* @param request - The agent invocation request in any supported format
|
|
83
|
+
* @returns A web standard Response object that can be returned directly in frameworks
|
|
84
|
+
* like Hono, Next.js, or any Fetch API compatible environment
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* Here's a simple example of how to use AIGNEServer with Hono:
|
|
88
|
+
* {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-hono}
|
|
89
|
+
*/
|
|
90
|
+
invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
|
|
91
|
+
/**
|
|
92
|
+
* Invokes an agent with the provided input and streams the response to a Node.js ServerResponse.
|
|
93
|
+
* This overload is specifically designed for Node.js HTTP server environments.
|
|
94
|
+
*
|
|
95
|
+
* The method handles both regular JSON responses and streaming Server-Sent Events (SSE)
|
|
96
|
+
* responses based on the options specified in the request.
|
|
97
|
+
*
|
|
98
|
+
* @param request - The agent invocation request in any supported format
|
|
99
|
+
* @param response - The Node.js ServerResponse object to write the response to
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* Here's a simple example of how to use AIGNEServer with express:
|
|
103
|
+
* {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-express}
|
|
104
|
+
*/
|
|
105
|
+
invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Internal method that handles the core logic of processing an agent invocation request.
|
|
108
|
+
* Validates the request payload, finds the requested agent, and processes the invocation
|
|
109
|
+
* with either streaming or non-streaming response handling.
|
|
110
|
+
*
|
|
111
|
+
* @param request - The parsed or raw request to process
|
|
112
|
+
* @returns A standard Response object with the invocation result
|
|
113
|
+
* @private
|
|
114
|
+
*/
|
|
115
|
+
_invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
|
|
116
|
+
/**
|
|
117
|
+
* Prepares and normalizes the input from various request types.
|
|
118
|
+
* Handles different request formats (Node.js IncomingMessage, Fetch API Request,
|
|
119
|
+
* or already parsed object) and extracts the JSON payload.
|
|
120
|
+
*
|
|
121
|
+
* @param request - The request object in any supported format
|
|
122
|
+
* @returns The normalized payload as a JavaScript object
|
|
123
|
+
* @private
|
|
124
|
+
*/
|
|
125
|
+
_prepareInput(request: Record<string, unknown> | Request | IncomingMessage): Promise<Record<string, unknown>>;
|
|
126
|
+
/**
|
|
127
|
+
* Writes a web standard Response object to a Node.js ServerResponse.
|
|
128
|
+
* Handles streaming responses and error conditions appropriately.
|
|
129
|
+
*
|
|
130
|
+
* @param response - The web standard Response object to write
|
|
131
|
+
* @param res - The Node.js ServerResponse to write to
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
_writeResponse(response: Response, res: ServerResponse): Promise<void>;
|
|
135
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { AgentResponseStreamSSE } from "@aigne/core/utils/event-stream.js";
|
|
3
|
+
import { checkArguments, isRecord, tryOrThrow } from "@aigne/core/utils/type-utils.js";
|
|
4
|
+
import contentType from "content-type";
|
|
5
|
+
import getRawBody from "raw-body";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { ServerError } from "./error.js";
|
|
8
|
+
/**
|
|
9
|
+
* Default maximum allowed size for request bodies when parsing raw HTTP requests.
|
|
10
|
+
* This limits the amount of data that can be uploaded to protect against denial of service attacks.
|
|
11
|
+
* Can be overridden via AIGNEServerOptions.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
const DEFAULT_MAXIMUM_BODY_SIZE = "4mb";
|
|
15
|
+
/**
|
|
16
|
+
* Schema for validating agent invocation payloads.
|
|
17
|
+
* Defines the expected structure for requests to invoke an agent.
|
|
18
|
+
*
|
|
19
|
+
* @hidden
|
|
20
|
+
*/
|
|
21
|
+
export const invokePayloadSchema = z.object({
|
|
22
|
+
agent: z.string(),
|
|
23
|
+
input: z.union([z.string(), z.record(z.string(), z.unknown())]),
|
|
24
|
+
options: z
|
|
25
|
+
.object({
|
|
26
|
+
streaming: z.boolean().nullish(),
|
|
27
|
+
})
|
|
28
|
+
.nullish(),
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* AIGNEHTTPServer provides HTTP API access to AIGNE capabilities.
|
|
32
|
+
* It handles requests to invoke agents, manages response streaming,
|
|
33
|
+
* and supports multiple HTTP server frameworks including Node.js http,
|
|
34
|
+
* Express, and Fetch API compatible environments.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* Here's a simple example of how to use AIGNEServer with express:
|
|
38
|
+
* {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-express}
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* Here's an example of how to use AIGNEServer with Hono:
|
|
42
|
+
* {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-hono}
|
|
43
|
+
*/
|
|
44
|
+
export class AIGNEHTTPServer {
|
|
45
|
+
engine;
|
|
46
|
+
options;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a new AIGNEServer instance.
|
|
49
|
+
*
|
|
50
|
+
* @param engine - The AIGNE engine instance that will process agent invocations
|
|
51
|
+
* @param options - Configuration options for the server
|
|
52
|
+
*/
|
|
53
|
+
constructor(engine, options) {
|
|
54
|
+
this.engine = engine;
|
|
55
|
+
this.options = options;
|
|
56
|
+
}
|
|
57
|
+
async invoke(request, response) {
|
|
58
|
+
const result = await this._invoke(request);
|
|
59
|
+
if (response instanceof ServerResponse) {
|
|
60
|
+
await this._writeResponse(result, response);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Internal method that handles the core logic of processing an agent invocation request.
|
|
67
|
+
* Validates the request payload, finds the requested agent, and processes the invocation
|
|
68
|
+
* with either streaming or non-streaming response handling.
|
|
69
|
+
*
|
|
70
|
+
* @param request - The parsed or raw request to process
|
|
71
|
+
* @returns A standard Response object with the invocation result
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
async _invoke(request) {
|
|
75
|
+
const { engine } = this;
|
|
76
|
+
try {
|
|
77
|
+
const payload = await this._prepareInput(request);
|
|
78
|
+
const { agent: agentName, input, options, } = tryOrThrow(() => checkArguments(`Invoke agent ${payload.agent}`, invokePayloadSchema, payload), (error) => new ServerError(400, error.message));
|
|
79
|
+
const agent = engine.agents[agentName];
|
|
80
|
+
if (!agent)
|
|
81
|
+
throw new ServerError(404, `Agent ${agentName} not found`);
|
|
82
|
+
if (!options?.streaming) {
|
|
83
|
+
const result = await engine.invoke(agent, input);
|
|
84
|
+
return new Response(JSON.stringify(result), {
|
|
85
|
+
headers: { "Content-Type": "application/json" },
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const stream = await engine.invoke(agent, input, { streaming: true });
|
|
89
|
+
return new Response(new AgentResponseStreamSSE(stream), {
|
|
90
|
+
headers: {
|
|
91
|
+
"Content-Type": "text/event-stream",
|
|
92
|
+
"Cache-Control": "no-cache",
|
|
93
|
+
"X-Accel-Buffering": "no",
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
return new Response(JSON.stringify({ error: { message: error.message } }), {
|
|
99
|
+
status: error instanceof ServerError ? error.status : 500,
|
|
100
|
+
headers: { "Content-Type": "application/json" },
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Prepares and normalizes the input from various request types.
|
|
106
|
+
* Handles different request formats (Node.js IncomingMessage, Fetch API Request,
|
|
107
|
+
* or already parsed object) and extracts the JSON payload.
|
|
108
|
+
*
|
|
109
|
+
* @param request - The request object in any supported format
|
|
110
|
+
* @returns The normalized payload as a JavaScript object
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
async _prepareInput(request) {
|
|
114
|
+
const contentTypeError = new ServerError(415, "Unsupported Media Type: Content-Type must be application/json");
|
|
115
|
+
if (request instanceof IncomingMessage) {
|
|
116
|
+
// Support for express with json() middleware
|
|
117
|
+
if ("body" in request && typeof request.body === "object") {
|
|
118
|
+
if (!isRecord(request.body))
|
|
119
|
+
throw contentTypeError;
|
|
120
|
+
return request.body;
|
|
121
|
+
}
|
|
122
|
+
// Support vanilla nodejs http server
|
|
123
|
+
const maximumBodySize = this.options?.maximumBodySize || DEFAULT_MAXIMUM_BODY_SIZE;
|
|
124
|
+
const ct = request.headers["content-type"];
|
|
125
|
+
if (!ct || !ct.includes("application/json"))
|
|
126
|
+
throw contentTypeError;
|
|
127
|
+
const parsedCt = contentType.parse(ct);
|
|
128
|
+
const raw = await getRawBody(request, {
|
|
129
|
+
limit: maximumBodySize,
|
|
130
|
+
encoding: parsedCt.parameters.charset ?? "utf-8",
|
|
131
|
+
});
|
|
132
|
+
return tryOrThrow(() => JSON.parse(raw.toString()), (error) => new ServerError(400, `Parse request body to json error: ${error.message}`));
|
|
133
|
+
}
|
|
134
|
+
if (request instanceof Request) {
|
|
135
|
+
if (!request.headers.get("content-type")?.includes("application/json")) {
|
|
136
|
+
throw contentTypeError;
|
|
137
|
+
}
|
|
138
|
+
return await request.json();
|
|
139
|
+
}
|
|
140
|
+
if (!isRecord(request))
|
|
141
|
+
throw contentTypeError;
|
|
142
|
+
return request;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Writes a web standard Response object to a Node.js ServerResponse.
|
|
146
|
+
* Handles streaming responses and error conditions appropriately.
|
|
147
|
+
*
|
|
148
|
+
* @param response - The web standard Response object to write
|
|
149
|
+
* @param res - The Node.js ServerResponse to write to
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
152
|
+
async _writeResponse(response, res) {
|
|
153
|
+
try {
|
|
154
|
+
res.writeHead(response.status, Object.fromEntries(response.headers.entries()));
|
|
155
|
+
res.flushHeaders();
|
|
156
|
+
if (!response.body)
|
|
157
|
+
throw new Error("Response body is empty");
|
|
158
|
+
for await (const chunk of response.body) {
|
|
159
|
+
res.write(chunk);
|
|
160
|
+
// Support for express with compression middleware
|
|
161
|
+
if ("flush" in res && typeof res.flush === "function") {
|
|
162
|
+
res.flush();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
if (!res.headersSent) {
|
|
168
|
+
res.writeHead(error instanceof ServerError ? error.status : 500, {
|
|
169
|
+
"Content-Type": "application/json",
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (res.writable) {
|
|
173
|
+
res.write(JSON.stringify({ error: { message: error.message } }));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
res.end();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module"}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aigne/transport",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "AIGNE Transport SDK providing HTTP client and server implementations for communication between AIGNE components",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
|
|
9
|
+
"homepage": "https://github.com/AIGNE-io/aigne-framework",
|
|
10
|
+
"license": "Elastic-2.0",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/AIGNE-io/aigne-framework"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"lib/cjs",
|
|
17
|
+
"lib/dts",
|
|
18
|
+
"lib/esm",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"README.md",
|
|
21
|
+
"CHANGELOG.md"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./lib/cjs/index.js",
|
|
25
|
+
"module": "./lib/esm/index.js",
|
|
26
|
+
"types": "./lib/dts/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
"./*": {
|
|
29
|
+
"import": "./lib/esm/*",
|
|
30
|
+
"require": "./lib/cjs/*",
|
|
31
|
+
"types": "./lib/dts/*"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"typesVersions": {
|
|
35
|
+
"*": {
|
|
36
|
+
"*": [
|
|
37
|
+
"./lib/dts/*"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@aigne/openai": "^0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/bun": "^1.2.12",
|
|
46
|
+
"@types/express": "^5.0.1",
|
|
47
|
+
"@types/node": "^22.15.15",
|
|
48
|
+
"compression": "^1.8.0",
|
|
49
|
+
"detect-port": "^2.1.0",
|
|
50
|
+
"express": "^5.1.0",
|
|
51
|
+
"hono": "^4.7.10",
|
|
52
|
+
"npm-run-all": "^4.1.5",
|
|
53
|
+
"rimraf": "^6.0.1",
|
|
54
|
+
"typescript": "^5.8.3",
|
|
55
|
+
"@aigne/core": "^1.16.0",
|
|
56
|
+
"@aigne/test-utils": "^0.3.0"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"lint": "tsc --noEmit",
|
|
60
|
+
"build": "tsc --build scripts/tsconfig.build.json",
|
|
61
|
+
"clean": "rimraf lib test/coverage",
|
|
62
|
+
"test": "bun test",
|
|
63
|
+
"test:coverage": "bun test --coverage --coverage-reporter=lcov --coverage-reporter=text",
|
|
64
|
+
"postbuild": "echo '{\"type\": \"module\"}' > lib/esm/package.json && echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json"
|
|
65
|
+
}
|
|
66
|
+
}
|