@aigne/transport 0.1.0 → 0.2.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 CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.0](https://github.com/AIGNE-io/aigne-framework/compare/transport-v0.1.0...transport-v0.2.0) (2025-05-25)
4
+
5
+
6
+ ### Features
7
+
8
+ * add user context support ([#131](https://github.com/AIGNE-io/aigne-framework/issues/131)) ([4dd9d20](https://github.com/AIGNE-io/aigne-framework/commit/4dd9d20953f6ac33933723db56efd9b44bafeb02))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @aigne/openai bumped to 0.2.0
16
+ * devDependencies
17
+ * @aigne/core bumped to 1.17.0
18
+ * @aigne/test-utils bumped to 0.3.1
19
+
3
20
  ## [0.1.0](https://github.com/AIGNE-io/aigne-framework/compare/transport-v0.0.1...transport-v0.1.0) (2025-05-23)
4
21
 
5
22
 
@@ -16,7 +16,7 @@ export interface AIGNEHTTPClientOptions {
16
16
  * Options for invoking an agent through the AIGNEHTTPClient.
17
17
  * Extends the standard AgentInvokeOptions with client-specific options.
18
18
  */
19
- export interface AIGNEHTTPClientInvokeOptions extends AgentInvokeOptions {
19
+ export interface AIGNEHTTPClientInvokeOptions extends Omit<AgentInvokeOptions, "context"> {
20
20
  /**
21
21
  * Additional fetch API options to customize the HTTP request.
22
22
  * These options will be merged with the default options used by the client.
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from "node:http";
2
- import type { AIGNE } from "@aigne/core";
2
+ import type { AIGNE, UserContext } from "@aigne/core";
3
3
  import { z } from "zod";
4
4
  /**
5
5
  * Schema for validating agent invocation payloads.
@@ -10,19 +10,23 @@ import { z } from "zod";
10
10
  export declare const invokePayloadSchema: z.ZodObject<{
11
11
  agent: z.ZodString;
12
12
  input: z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
13
- options: z.ZodOptional<z.ZodNullable<z.ZodObject<{
13
+ options: z.ZodEffects<z.ZodOptional<z.ZodNullable<z.ZodObject<{
14
14
  streaming: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
15
15
  }, "strip", z.ZodTypeAny, {
16
16
  streaming?: boolean | null | undefined;
17
17
  }, {
18
18
  streaming?: boolean | null | undefined;
19
- }>>>;
19
+ }>>>, {
20
+ streaming?: boolean | null | undefined;
21
+ } | undefined, {
22
+ streaming?: boolean | null | undefined;
23
+ } | null | undefined>;
20
24
  }, "strip", z.ZodTypeAny, {
21
25
  agent: string;
22
26
  input: string | Record<string, unknown>;
23
27
  options?: {
24
28
  streaming?: boolean | null | undefined;
25
- } | null | undefined;
29
+ } | undefined;
26
30
  }, {
27
31
  agent: string;
28
32
  input: string | Record<string, unknown>;
@@ -46,6 +50,9 @@ export interface AIGNEHTTPServerOptions {
46
50
  */
47
51
  maximumBodySize?: string;
48
52
  }
53
+ export interface AIGNEHTTPServerInvokeOptions<U extends UserContext = UserContext> {
54
+ userContext?: U;
55
+ }
49
56
  /**
50
57
  * AIGNEHTTPServer provides HTTP API access to AIGNE capabilities.
51
58
  * It handles requests to invoke agents, manages response streaming,
@@ -87,7 +94,7 @@ export declare class AIGNEHTTPServer {
87
94
  * Here's a simple example of how to use AIGNEServer with Hono:
88
95
  * {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-hono}
89
96
  */
90
- invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
97
+ invoke(request: Record<string, unknown> | Request | IncomingMessage, options?: ServerResponse | AIGNEHTTPServerInvokeOptions): Promise<Response>;
91
98
  /**
92
99
  * Invokes an agent with the provided input and streams the response to a Node.js ServerResponse.
93
100
  * This overload is specifically designed for Node.js HTTP server environments.
@@ -102,7 +109,7 @@ export declare class AIGNEHTTPServer {
102
109
  * Here's a simple example of how to use AIGNEServer with express:
103
110
  * {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-express}
104
111
  */
105
- invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse): Promise<void>;
112
+ invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse, options?: AIGNEHTTPServerInvokeOptions): Promise<void>;
106
113
  /**
107
114
  * Internal method that handles the core logic of processing an agent invocation request.
108
115
  * Validates the request payload, finds the requested agent, and processes the invocation
@@ -112,7 +119,7 @@ export declare class AIGNEHTTPServer {
112
119
  * @returns A standard Response object with the invocation result
113
120
  * @private
114
121
  */
115
- _invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
122
+ _invoke(request: Record<string, unknown> | Request | IncomingMessage, { userContext }?: AIGNEHTTPServerInvokeOptions): Promise<Response>;
116
123
  /**
117
124
  * Prepares and normalizes the input from various request types.
118
125
  * Handles different request formats (Node.js IncomingMessage, Fetch API Request,
@@ -28,10 +28,9 @@ exports.invokePayloadSchema = zod_1.z.object({
28
28
  agent: zod_1.z.string(),
29
29
  input: zod_1.z.union([zod_1.z.string(), zod_1.z.record(zod_1.z.string(), zod_1.z.unknown())]),
30
30
  options: zod_1.z
31
- .object({
32
- streaming: zod_1.z.boolean().nullish(),
33
- })
34
- .nullish(),
31
+ .object({ streaming: zod_1.z.boolean().nullish() })
32
+ .nullish()
33
+ .transform((v) => v ?? undefined),
35
34
  });
36
35
  /**
37
36
  * AIGNEHTTPServer provides HTTP API access to AIGNE capabilities.
@@ -60,8 +59,9 @@ class AIGNEHTTPServer {
60
59
  this.engine = engine;
61
60
  this.options = options;
62
61
  }
63
- async invoke(request, response) {
64
- const result = await this._invoke(request);
62
+ async invoke(request, response, options) {
63
+ const opts = !(response instanceof node_http_1.ServerResponse) ? options || response : options;
64
+ const result = await this._invoke(request, { userContext: opts?.userContext });
65
65
  if (response instanceof node_http_1.ServerResponse) {
66
66
  await this._writeResponse(result, response);
67
67
  return;
@@ -77,21 +77,21 @@ class AIGNEHTTPServer {
77
77
  * @returns A standard Response object with the invocation result
78
78
  * @private
79
79
  */
80
- async _invoke(request) {
80
+ async _invoke(request, { userContext } = {}) {
81
81
  const { engine } = this;
82
82
  try {
83
83
  const payload = await this._prepareInput(request);
84
- const { agent: agentName, input, options, } = (0, type_utils_js_1.tryOrThrow)(() => (0, type_utils_js_1.checkArguments)(`Invoke agent ${payload.agent}`, exports.invokePayloadSchema, payload), (error) => new error_js_1.ServerError(400, error.message));
84
+ const { agent: agentName, input, options: { streaming } = {}, } = (0, type_utils_js_1.tryOrThrow)(() => (0, type_utils_js_1.checkArguments)(`Invoke agent ${payload.agent}`, exports.invokePayloadSchema, payload), (error) => new error_js_1.ServerError(400, error.message));
85
85
  const agent = engine.agents[agentName];
86
86
  if (!agent)
87
87
  throw new error_js_1.ServerError(404, `Agent ${agentName} not found`);
88
- if (!options?.streaming) {
89
- const result = await engine.invoke(agent, input);
88
+ if (!streaming) {
89
+ const result = await engine.invoke(agent, input, { userContext });
90
90
  return new Response(JSON.stringify(result), {
91
91
  headers: { "Content-Type": "application/json" },
92
92
  });
93
93
  }
94
- const stream = await engine.invoke(agent, input, { streaming: true });
94
+ const stream = await engine.invoke(agent, input, { userContext, streaming: true });
95
95
  return new Response(new event_stream_js_1.AgentResponseStreamSSE(stream), {
96
96
  headers: {
97
97
  "Content-Type": "text/event-stream",
@@ -16,7 +16,7 @@ export interface AIGNEHTTPClientOptions {
16
16
  * Options for invoking an agent through the AIGNEHTTPClient.
17
17
  * Extends the standard AgentInvokeOptions with client-specific options.
18
18
  */
19
- export interface AIGNEHTTPClientInvokeOptions extends AgentInvokeOptions {
19
+ export interface AIGNEHTTPClientInvokeOptions extends Omit<AgentInvokeOptions, "context"> {
20
20
  /**
21
21
  * Additional fetch API options to customize the HTTP request.
22
22
  * These options will be merged with the default options used by the client.
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from "node:http";
2
- import type { AIGNE } from "@aigne/core";
2
+ import type { AIGNE, UserContext } from "@aigne/core";
3
3
  import { z } from "zod";
4
4
  /**
5
5
  * Schema for validating agent invocation payloads.
@@ -10,19 +10,23 @@ import { z } from "zod";
10
10
  export declare const invokePayloadSchema: z.ZodObject<{
11
11
  agent: z.ZodString;
12
12
  input: z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
13
- options: z.ZodOptional<z.ZodNullable<z.ZodObject<{
13
+ options: z.ZodEffects<z.ZodOptional<z.ZodNullable<z.ZodObject<{
14
14
  streaming: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
15
15
  }, "strip", z.ZodTypeAny, {
16
16
  streaming?: boolean | null | undefined;
17
17
  }, {
18
18
  streaming?: boolean | null | undefined;
19
- }>>>;
19
+ }>>>, {
20
+ streaming?: boolean | null | undefined;
21
+ } | undefined, {
22
+ streaming?: boolean | null | undefined;
23
+ } | null | undefined>;
20
24
  }, "strip", z.ZodTypeAny, {
21
25
  agent: string;
22
26
  input: string | Record<string, unknown>;
23
27
  options?: {
24
28
  streaming?: boolean | null | undefined;
25
- } | null | undefined;
29
+ } | undefined;
26
30
  }, {
27
31
  agent: string;
28
32
  input: string | Record<string, unknown>;
@@ -46,6 +50,9 @@ export interface AIGNEHTTPServerOptions {
46
50
  */
47
51
  maximumBodySize?: string;
48
52
  }
53
+ export interface AIGNEHTTPServerInvokeOptions<U extends UserContext = UserContext> {
54
+ userContext?: U;
55
+ }
49
56
  /**
50
57
  * AIGNEHTTPServer provides HTTP API access to AIGNE capabilities.
51
58
  * It handles requests to invoke agents, manages response streaming,
@@ -87,7 +94,7 @@ export declare class AIGNEHTTPServer {
87
94
  * Here's a simple example of how to use AIGNEServer with Hono:
88
95
  * {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-hono}
89
96
  */
90
- invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
97
+ invoke(request: Record<string, unknown> | Request | IncomingMessage, options?: ServerResponse | AIGNEHTTPServerInvokeOptions): Promise<Response>;
91
98
  /**
92
99
  * Invokes an agent with the provided input and streams the response to a Node.js ServerResponse.
93
100
  * This overload is specifically designed for Node.js HTTP server environments.
@@ -102,7 +109,7 @@ export declare class AIGNEHTTPServer {
102
109
  * Here's a simple example of how to use AIGNEServer with express:
103
110
  * {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-express}
104
111
  */
105
- invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse): Promise<void>;
112
+ invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse, options?: AIGNEHTTPServerInvokeOptions): Promise<void>;
106
113
  /**
107
114
  * Internal method that handles the core logic of processing an agent invocation request.
108
115
  * Validates the request payload, finds the requested agent, and processes the invocation
@@ -112,7 +119,7 @@ export declare class AIGNEHTTPServer {
112
119
  * @returns A standard Response object with the invocation result
113
120
  * @private
114
121
  */
115
- _invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
122
+ _invoke(request: Record<string, unknown> | Request | IncomingMessage, { userContext }?: AIGNEHTTPServerInvokeOptions): Promise<Response>;
116
123
  /**
117
124
  * Prepares and normalizes the input from various request types.
118
125
  * Handles different request formats (Node.js IncomingMessage, Fetch API Request,
@@ -16,7 +16,7 @@ export interface AIGNEHTTPClientOptions {
16
16
  * Options for invoking an agent through the AIGNEHTTPClient.
17
17
  * Extends the standard AgentInvokeOptions with client-specific options.
18
18
  */
19
- export interface AIGNEHTTPClientInvokeOptions extends AgentInvokeOptions {
19
+ export interface AIGNEHTTPClientInvokeOptions extends Omit<AgentInvokeOptions, "context"> {
20
20
  /**
21
21
  * Additional fetch API options to customize the HTTP request.
22
22
  * These options will be merged with the default options used by the client.
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from "node:http";
2
- import type { AIGNE } from "@aigne/core";
2
+ import type { AIGNE, UserContext } from "@aigne/core";
3
3
  import { z } from "zod";
4
4
  /**
5
5
  * Schema for validating agent invocation payloads.
@@ -10,19 +10,23 @@ import { z } from "zod";
10
10
  export declare const invokePayloadSchema: z.ZodObject<{
11
11
  agent: z.ZodString;
12
12
  input: z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
13
- options: z.ZodOptional<z.ZodNullable<z.ZodObject<{
13
+ options: z.ZodEffects<z.ZodOptional<z.ZodNullable<z.ZodObject<{
14
14
  streaming: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
15
15
  }, "strip", z.ZodTypeAny, {
16
16
  streaming?: boolean | null | undefined;
17
17
  }, {
18
18
  streaming?: boolean | null | undefined;
19
- }>>>;
19
+ }>>>, {
20
+ streaming?: boolean | null | undefined;
21
+ } | undefined, {
22
+ streaming?: boolean | null | undefined;
23
+ } | null | undefined>;
20
24
  }, "strip", z.ZodTypeAny, {
21
25
  agent: string;
22
26
  input: string | Record<string, unknown>;
23
27
  options?: {
24
28
  streaming?: boolean | null | undefined;
25
- } | null | undefined;
29
+ } | undefined;
26
30
  }, {
27
31
  agent: string;
28
32
  input: string | Record<string, unknown>;
@@ -46,6 +50,9 @@ export interface AIGNEHTTPServerOptions {
46
50
  */
47
51
  maximumBodySize?: string;
48
52
  }
53
+ export interface AIGNEHTTPServerInvokeOptions<U extends UserContext = UserContext> {
54
+ userContext?: U;
55
+ }
49
56
  /**
50
57
  * AIGNEHTTPServer provides HTTP API access to AIGNE capabilities.
51
58
  * It handles requests to invoke agents, manages response streaming,
@@ -87,7 +94,7 @@ export declare class AIGNEHTTPServer {
87
94
  * Here's a simple example of how to use AIGNEServer with Hono:
88
95
  * {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-hono}
89
96
  */
90
- invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
97
+ invoke(request: Record<string, unknown> | Request | IncomingMessage, options?: ServerResponse | AIGNEHTTPServerInvokeOptions): Promise<Response>;
91
98
  /**
92
99
  * Invokes an agent with the provided input and streams the response to a Node.js ServerResponse.
93
100
  * This overload is specifically designed for Node.js HTTP server environments.
@@ -102,7 +109,7 @@ export declare class AIGNEHTTPServer {
102
109
  * Here's a simple example of how to use AIGNEServer with express:
103
110
  * {@includeCode ../../test/http-server/http-server.test.ts#example-aigne-server-express}
104
111
  */
105
- invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse): Promise<void>;
112
+ invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse, options?: AIGNEHTTPServerInvokeOptions): Promise<void>;
106
113
  /**
107
114
  * Internal method that handles the core logic of processing an agent invocation request.
108
115
  * Validates the request payload, finds the requested agent, and processes the invocation
@@ -112,7 +119,7 @@ export declare class AIGNEHTTPServer {
112
119
  * @returns A standard Response object with the invocation result
113
120
  * @private
114
121
  */
115
- _invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
122
+ _invoke(request: Record<string, unknown> | Request | IncomingMessage, { userContext }?: AIGNEHTTPServerInvokeOptions): Promise<Response>;
116
123
  /**
117
124
  * Prepares and normalizes the input from various request types.
118
125
  * Handles different request formats (Node.js IncomingMessage, Fetch API Request,
@@ -22,10 +22,9 @@ export const invokePayloadSchema = z.object({
22
22
  agent: z.string(),
23
23
  input: z.union([z.string(), z.record(z.string(), z.unknown())]),
24
24
  options: z
25
- .object({
26
- streaming: z.boolean().nullish(),
27
- })
28
- .nullish(),
25
+ .object({ streaming: z.boolean().nullish() })
26
+ .nullish()
27
+ .transform((v) => v ?? undefined),
29
28
  });
30
29
  /**
31
30
  * AIGNEHTTPServer provides HTTP API access to AIGNE capabilities.
@@ -54,8 +53,9 @@ export class AIGNEHTTPServer {
54
53
  this.engine = engine;
55
54
  this.options = options;
56
55
  }
57
- async invoke(request, response) {
58
- const result = await this._invoke(request);
56
+ async invoke(request, response, options) {
57
+ const opts = !(response instanceof ServerResponse) ? options || response : options;
58
+ const result = await this._invoke(request, { userContext: opts?.userContext });
59
59
  if (response instanceof ServerResponse) {
60
60
  await this._writeResponse(result, response);
61
61
  return;
@@ -71,21 +71,21 @@ export class AIGNEHTTPServer {
71
71
  * @returns A standard Response object with the invocation result
72
72
  * @private
73
73
  */
74
- async _invoke(request) {
74
+ async _invoke(request, { userContext } = {}) {
75
75
  const { engine } = this;
76
76
  try {
77
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));
78
+ const { agent: agentName, input, options: { streaming } = {}, } = tryOrThrow(() => checkArguments(`Invoke agent ${payload.agent}`, invokePayloadSchema, payload), (error) => new ServerError(400, error.message));
79
79
  const agent = engine.agents[agentName];
80
80
  if (!agent)
81
81
  throw new ServerError(404, `Agent ${agentName} not found`);
82
- if (!options?.streaming) {
83
- const result = await engine.invoke(agent, input);
82
+ if (!streaming) {
83
+ const result = await engine.invoke(agent, input, { userContext });
84
84
  return new Response(JSON.stringify(result), {
85
85
  headers: { "Content-Type": "application/json" },
86
86
  });
87
87
  }
88
- const stream = await engine.invoke(agent, input, { streaming: true });
88
+ const stream = await engine.invoke(agent, input, { userContext, streaming: true });
89
89
  return new Response(new AgentResponseStreamSSE(stream), {
90
90
  headers: {
91
91
  "Content-Type": "text/event-stream",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/transport",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "AIGNE Transport SDK providing HTTP client and server implementations for communication between AIGNE components",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -39,7 +39,7 @@
39
39
  }
40
40
  },
41
41
  "dependencies": {
42
- "@aigne/openai": "^0.1.0"
42
+ "@aigne/openai": "^0.2.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/bun": "^1.2.12",
@@ -52,8 +52,8 @@
52
52
  "npm-run-all": "^4.1.5",
53
53
  "rimraf": "^6.0.1",
54
54
  "typescript": "^5.8.3",
55
- "@aigne/core": "^1.16.0",
56
- "@aigne/test-utils": "^0.3.0"
55
+ "@aigne/core": "^1.17.0",
56
+ "@aigne/test-utils": "^0.3.1"
57
57
  },
58
58
  "scripts": {
59
59
  "lint": "tsc --noEmit",