@downcity/server 0.1.3
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/LICENSE +183 -0
- package/README.md +51 -0
- package/bin/http/AgentHTTP.d.ts +53 -0
- package/bin/http/AgentHTTP.d.ts.map +1 -0
- package/bin/http/AgentHTTP.js +118 -0
- package/bin/http/AgentHTTP.js.map +1 -0
- package/bin/http/NodeHttpAdapter.d.ts +21 -0
- package/bin/http/NodeHttpAdapter.d.ts.map +1 -0
- package/bin/http/NodeHttpAdapter.js +56 -0
- package/bin/http/NodeHttpAdapter.js.map +1 -0
- package/bin/http/routes/SessionRoutes.d.ts +15 -0
- package/bin/http/routes/SessionRoutes.d.ts.map +1 -0
- package/bin/http/routes/SessionRoutes.js +249 -0
- package/bin/http/routes/SessionRoutes.js.map +1 -0
- package/bin/index.d.ts +13 -0
- package/bin/index.d.ts.map +1 -0
- package/bin/index.js +10 -0
- package/bin/index.js.map +1 -0
- package/bin/rpc/AgentRPC.d.ts +37 -0
- package/bin/rpc/AgentRPC.d.ts.map +1 -0
- package/bin/rpc/AgentRPC.js +81 -0
- package/bin/rpc/AgentRPC.js.map +1 -0
- package/bin/rpc/RpcServer.d.ts +30 -0
- package/bin/rpc/RpcServer.d.ts.map +1 -0
- package/bin/rpc/RpcServer.js +106 -0
- package/bin/rpc/RpcServer.js.map +1 -0
- package/bin/rpc/server/InternalHandlers.d.ts +22 -0
- package/bin/rpc/server/InternalHandlers.d.ts.map +1 -0
- package/bin/rpc/server/InternalHandlers.js +248 -0
- package/bin/rpc/server/InternalHandlers.js.map +1 -0
- package/bin/rpc/server/RequestDispatcher.d.ts +27 -0
- package/bin/rpc/server/RequestDispatcher.d.ts.map +1 -0
- package/bin/rpc/server/RequestDispatcher.js +38 -0
- package/bin/rpc/server/RequestDispatcher.js.map +1 -0
- package/bin/rpc/server/SdkSessionHandlers.d.ts +25 -0
- package/bin/rpc/server/SdkSessionHandlers.d.ts.map +1 -0
- package/bin/rpc/server/SdkSessionHandlers.js +86 -0
- package/bin/rpc/server/SdkSessionHandlers.js.map +1 -0
- package/bin/rpc/server/ServerTypes.d.ts +58 -0
- package/bin/rpc/server/ServerTypes.d.ts.map +1 -0
- package/bin/rpc/server/ServerTypes.js +9 -0
- package/bin/rpc/server/ServerTypes.js.map +1 -0
- package/bin/types/AgentHttpBinding.d.ts +24 -0
- package/bin/types/AgentHttpBinding.d.ts.map +1 -0
- package/bin/types/AgentHttpBinding.js +5 -0
- package/bin/types/AgentHttpBinding.js.map +1 -0
- package/bin/types/AgentRpcBinding.d.ts +27 -0
- package/bin/types/AgentRpcBinding.d.ts.map +1 -0
- package/bin/types/AgentRpcBinding.js +8 -0
- package/bin/types/AgentRpcBinding.js.map +1 -0
- package/bin/types/RpcProtocol.d.ts +9 -0
- package/bin/types/RpcProtocol.d.ts.map +1 -0
- package/bin/types/RpcProtocol.js +9 -0
- package/bin/types/RpcProtocol.js.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
https://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
36
|
+
below).
|
|
37
|
+
|
|
38
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
39
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
40
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
41
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
42
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
43
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
44
|
+
|
|
45
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
46
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
47
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
48
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
49
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
50
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
51
|
+
to the Licensor or its representatives, including but not limited to
|
|
52
|
+
communication on electronic mailing lists, source code control systems, and
|
|
53
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
54
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
55
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
56
|
+
owner as "Not a Contribution."
|
|
57
|
+
|
|
58
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
59
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
60
|
+
incorporated within the Work.
|
|
61
|
+
|
|
62
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
63
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
64
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
65
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
66
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
67
|
+
Object form.
|
|
68
|
+
|
|
69
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
70
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
71
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
72
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
73
|
+
transfer the Work, where such license applies only to those patent claims
|
|
74
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
75
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work
|
|
76
|
+
to which such Contribution(s) was submitted. If You institute patent litigation
|
|
77
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
78
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
79
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
80
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
81
|
+
such litigation is filed.
|
|
82
|
+
|
|
83
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
84
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
85
|
+
Source or Object form, provided that You meet the following conditions:
|
|
86
|
+
|
|
87
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
88
|
+
this License; and
|
|
89
|
+
|
|
90
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
91
|
+
You changed the files; and
|
|
92
|
+
|
|
93
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
94
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
95
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
96
|
+
of the Derivative Works; and
|
|
97
|
+
|
|
98
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
99
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
100
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
101
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
102
|
+
following places: within a NOTICE text file distributed as part of the
|
|
103
|
+
Derivative Works; within the Source form or documentation, if provided along
|
|
104
|
+
with the Derivative Works; or, within a display generated by the Derivative
|
|
105
|
+
Works, if and wherever such third-party notices normally appear. The contents of
|
|
106
|
+
the NOTICE file are for informational purposes only and do not modify the
|
|
107
|
+
License. You may add Your own attribution notices within Derivative Works that
|
|
108
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
|
109
|
+
provided that such additional attribution notices cannot be construed as
|
|
110
|
+
modifying the License.
|
|
111
|
+
|
|
112
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
113
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
114
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
115
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
116
|
+
with the conditions stated in this License.
|
|
117
|
+
|
|
118
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
119
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
120
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
121
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
122
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
123
|
+
executed with Licensor regarding such Contributions.
|
|
124
|
+
|
|
125
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
126
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
127
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
128
|
+
reproducing the content of the NOTICE file.
|
|
129
|
+
|
|
130
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
131
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
132
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
133
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
134
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
135
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
136
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
137
|
+
associated with Your exercise of permissions under this License.
|
|
138
|
+
|
|
139
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
140
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
141
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
142
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
143
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
144
|
+
arising as a result of this License or out of the use or inability to use the
|
|
145
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
146
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
147
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
148
|
+
damages.
|
|
149
|
+
|
|
150
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
151
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
152
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
153
|
+
and/or rights consistent with this License. However, in accepting such
|
|
154
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
155
|
+
responsibility, not on behalf of any other Contributor, and only if You agree to
|
|
156
|
+
indemnify, defend, and hold each Contributor harmless for any liability incurred
|
|
157
|
+
by, or claims asserted against, such Contributor by reason of your accepting any
|
|
158
|
+
such warranty or additional liability.
|
|
159
|
+
|
|
160
|
+
END OF TERMS AND CONDITIONS
|
|
161
|
+
|
|
162
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
163
|
+
|
|
164
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
165
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
166
|
+
identifying information. (Do not include the brackets!) The text should be
|
|
167
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
168
|
+
recommend that a file or class name and description of purpose be included on
|
|
169
|
+
the same "printed page" as the copyright notice for easier identification within
|
|
170
|
+
third-party archives.
|
|
171
|
+
|
|
172
|
+
Copyright [yyyy] [name of copyright owner]
|
|
173
|
+
|
|
174
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
175
|
+
you may not use this file except in compliance with the License.
|
|
176
|
+
You may obtain a copy of the License at
|
|
177
|
+
|
|
178
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
179
|
+
|
|
180
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
181
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
182
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
183
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# @downcity/server
|
|
2
|
+
|
|
3
|
+
`@downcity/server` 是 Downcity Agent 的传输适配层。
|
|
4
|
+
|
|
5
|
+
包内只做两件事:
|
|
6
|
+
|
|
7
|
+
- `AgentRPC`:把一个本地 `Agent` 通过 NDJSON over TCP 暴露成本机 RPC 服务,配合 `RemoteAgent("rpc://...")` 使用。
|
|
8
|
+
- `AgentHTTP`:把 `Agent` 暴露成最小 SDK HTTP 面(`/api/sdk/sessions/*`),可作为独立 HTTP 服务启动,也可作为 Hono 子路由挂到调用方自己的服务器上。
|
|
9
|
+
|
|
10
|
+
`@downcity/server` 不承载 Agent 业务实现,所有 session、plugin、executor 能力仍由 `@downcity/agent` 提供。
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @downcity/agent @downcity/server
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 用法
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { Agent } from "@downcity/agent";
|
|
22
|
+
import { AgentRPC, AgentHTTP } from "@downcity/server";
|
|
23
|
+
|
|
24
|
+
const agent = new Agent({ id, path, model });
|
|
25
|
+
await agent.ready();
|
|
26
|
+
|
|
27
|
+
const rpc = new AgentRPC(agent);
|
|
28
|
+
await rpc.listen({ host: "127.0.0.1", port: 15314 });
|
|
29
|
+
|
|
30
|
+
const http = new AgentHTTP(agent);
|
|
31
|
+
await http.server().listen({ host: "127.0.0.1", port: 5314 });
|
|
32
|
+
|
|
33
|
+
// 或者把 router 挂到自己的 Hono 上:
|
|
34
|
+
import { Hono } from "hono";
|
|
35
|
+
const app = new Hono();
|
|
36
|
+
app.route("/", http.router());
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
停机:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
await rpc.close();
|
|
43
|
+
await http.close();
|
|
44
|
+
await agent.dispose();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 设计要点
|
|
48
|
+
|
|
49
|
+
- AgentRPC 仅封装 NDJSON over TCP,不带 auth;本机使用。
|
|
50
|
+
- AgentHTTP 仅承载 RemoteAgent SDK transport;平台级路由请由调用方自行装配。
|
|
51
|
+
- AgentRPC / AgentHTTP 都是按需启动,互不依赖。
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentHTTP:把本地 Agent 暴露为最小 SDK HTTP 面的对外类。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 仅承载 RemoteAgent 对应的 SDK transport(`/api/sdk/sessions/*`)。
|
|
6
|
+
* - `router()` 返回一个 `Hono` 子路由,调用方 `app.route("/", agent_http.router())` 即可挂载。
|
|
7
|
+
* - `server()` 返回一个独立 HTTP server 句柄,按需自启、自停。
|
|
8
|
+
* - 同一个实例多次取 router/server 都返回缓存对象,避免重复注册。
|
|
9
|
+
*/
|
|
10
|
+
import { Hono } from "hono";
|
|
11
|
+
import type { Agent } from "@downcity/agent";
|
|
12
|
+
import type { AgentHttpBinding, AgentHttpListenOptions } from "../types/AgentHttpBinding.js";
|
|
13
|
+
/**
|
|
14
|
+
* AgentHTTP server 句柄。
|
|
15
|
+
*/
|
|
16
|
+
export interface AgentHttpServerHandle {
|
|
17
|
+
/** 监听 HTTP 端口。 */
|
|
18
|
+
listen(options: AgentHttpListenOptions): Promise<AgentHttpBinding>;
|
|
19
|
+
/** 关闭当前 HTTP server。 */
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
/** 当前监听绑定信息,未 listen 时为 `null`。 */
|
|
22
|
+
binding(): AgentHttpBinding | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 把一个 `Agent` 暴露为最小 SDK HTTP 面。
|
|
26
|
+
*/
|
|
27
|
+
export declare class AgentHTTP {
|
|
28
|
+
private readonly agent;
|
|
29
|
+
private cached_router;
|
|
30
|
+
private cached_server;
|
|
31
|
+
constructor(agent: Agent);
|
|
32
|
+
/**
|
|
33
|
+
* 返回一个挂载到外部 hono server 用的子路由。
|
|
34
|
+
*
|
|
35
|
+
* 说明(中文)
|
|
36
|
+
* - 多次调用返回同一个 hono 实例,避免重复注册。
|
|
37
|
+
* - 不在这里附加 CORS / logger,由调用方按需在自己的入口 hono 上挂中间件。
|
|
38
|
+
*/
|
|
39
|
+
router(): Hono;
|
|
40
|
+
/**
|
|
41
|
+
* 返回独立 HTTP server 句柄。
|
|
42
|
+
*/
|
|
43
|
+
server(): AgentHttpServerHandle;
|
|
44
|
+
/**
|
|
45
|
+
* 关闭通过本实例 `server()` 启动的 HTTP server。
|
|
46
|
+
*
|
|
47
|
+
* 说明(中文)
|
|
48
|
+
* - 仅作用于 `server()` 创建的独立 HTTP server。
|
|
49
|
+
* - `router()` 挂到外部 hono 的场景由调用方自己管理生命周期。
|
|
50
|
+
*/
|
|
51
|
+
close(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=AgentHTTP.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AgentHTTP.d.ts","sourceRoot":"","sources":["../../src/http/AgentHTTP.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAG7C,OAAO,KAAK,EACV,gBAAgB,EAChB,sBAAsB,EACvB,MAAM,6BAA6B,CAAC;AAIrC;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,kBAAkB;IAClB,MAAM,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IACnE,wBAAwB;IACxB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,mCAAmC;IACnC,OAAO,IAAI,gBAAgB,GAAG,IAAI,CAAC;CACpC;AAED;;GAEG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAC9B,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,aAAa,CAAsC;gBAE/C,KAAK,EAAE,KAAK;IAIxB;;;;;;OAMG;IACH,MAAM,IAAI,IAAI;IAQd;;OAEG;IACH,MAAM,IAAI,qBAAqB;IAO/B;;;;;;OAMG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAM7B"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentHTTP:把本地 Agent 暴露为最小 SDK HTTP 面的对外类。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 仅承载 RemoteAgent 对应的 SDK transport(`/api/sdk/sessions/*`)。
|
|
6
|
+
* - `router()` 返回一个 `Hono` 子路由,调用方 `app.route("/", agent_http.router())` 即可挂载。
|
|
7
|
+
* - `server()` 返回一个独立 HTTP server 句柄,按需自启、自停。
|
|
8
|
+
* - 同一个实例多次取 router/server 都返回缓存对象,避免重复注册。
|
|
9
|
+
*/
|
|
10
|
+
import { Hono } from "hono";
|
|
11
|
+
import { registerSdkSessionRoutes } from "../http/routes/SessionRoutes.js";
|
|
12
|
+
import { createNodeHttpServer } from "../http/NodeHttpAdapter.js";
|
|
13
|
+
const DEFAULT_HTTP_HOST = "127.0.0.1";
|
|
14
|
+
/**
|
|
15
|
+
* 把一个 `Agent` 暴露为最小 SDK HTTP 面。
|
|
16
|
+
*/
|
|
17
|
+
export class AgentHTTP {
|
|
18
|
+
agent;
|
|
19
|
+
cached_router = null;
|
|
20
|
+
cached_server = null;
|
|
21
|
+
constructor(agent) {
|
|
22
|
+
this.agent = agent;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 返回一个挂载到外部 hono server 用的子路由。
|
|
26
|
+
*
|
|
27
|
+
* 说明(中文)
|
|
28
|
+
* - 多次调用返回同一个 hono 实例,避免重复注册。
|
|
29
|
+
* - 不在这里附加 CORS / logger,由调用方按需在自己的入口 hono 上挂中间件。
|
|
30
|
+
*/
|
|
31
|
+
router() {
|
|
32
|
+
if (this.cached_router)
|
|
33
|
+
return this.cached_router;
|
|
34
|
+
const router = new Hono();
|
|
35
|
+
registerSdkSessionRoutes(router, this.agent.getSessionCollection());
|
|
36
|
+
this.cached_router = router;
|
|
37
|
+
return router;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 返回独立 HTTP server 句柄。
|
|
41
|
+
*/
|
|
42
|
+
server() {
|
|
43
|
+
if (this.cached_server)
|
|
44
|
+
return this.cached_server;
|
|
45
|
+
const handle = createAgentHttpServerHandle(this.router());
|
|
46
|
+
this.cached_server = handle;
|
|
47
|
+
return handle;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 关闭通过本实例 `server()` 启动的 HTTP server。
|
|
51
|
+
*
|
|
52
|
+
* 说明(中文)
|
|
53
|
+
* - 仅作用于 `server()` 创建的独立 HTTP server。
|
|
54
|
+
* - `router()` 挂到外部 hono 的场景由调用方自己管理生命周期。
|
|
55
|
+
*/
|
|
56
|
+
async close() {
|
|
57
|
+
const handle = this.cached_server;
|
|
58
|
+
this.cached_server = null;
|
|
59
|
+
if (!handle)
|
|
60
|
+
return;
|
|
61
|
+
await handle.close();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function createAgentHttpServerHandle(app) {
|
|
65
|
+
let current_server = null;
|
|
66
|
+
let current_binding = null;
|
|
67
|
+
let start_promise = null;
|
|
68
|
+
return {
|
|
69
|
+
async listen(options) {
|
|
70
|
+
if (start_promise)
|
|
71
|
+
return await start_promise;
|
|
72
|
+
if (current_binding)
|
|
73
|
+
return current_binding;
|
|
74
|
+
const host = String(options.host || DEFAULT_HTTP_HOST).trim() || DEFAULT_HTTP_HOST;
|
|
75
|
+
const port = options.port;
|
|
76
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
|
77
|
+
throw new Error("AgentHTTP server requires a valid TCP port");
|
|
78
|
+
}
|
|
79
|
+
start_promise = (async () => {
|
|
80
|
+
const server = createNodeHttpServer({ app, host, port });
|
|
81
|
+
await new Promise((resolve, reject) => {
|
|
82
|
+
server.once("error", reject);
|
|
83
|
+
server.listen(port, host, () => {
|
|
84
|
+
server.off("error", reject);
|
|
85
|
+
resolve();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
current_server = server;
|
|
89
|
+
current_binding = {
|
|
90
|
+
url: `http://${host}:${port}`,
|
|
91
|
+
host,
|
|
92
|
+
port,
|
|
93
|
+
};
|
|
94
|
+
return current_binding;
|
|
95
|
+
})();
|
|
96
|
+
try {
|
|
97
|
+
return await start_promise;
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
start_promise = null;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
async close() {
|
|
104
|
+
const server = current_server;
|
|
105
|
+
current_server = null;
|
|
106
|
+
current_binding = null;
|
|
107
|
+
if (!server)
|
|
108
|
+
return;
|
|
109
|
+
await new Promise((resolve) => {
|
|
110
|
+
server.close(() => resolve());
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
binding() {
|
|
114
|
+
return current_binding;
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=AgentHTTP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AgentHTTP.js","sourceRoot":"","sources":["../../src/http/AgentHTTP.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAMjE,MAAM,iBAAiB,GAAG,WAAW,CAAC;AActC;;GAEG;AACH,MAAM,OAAO,SAAS;IACH,KAAK,CAAQ;IACtB,aAAa,GAAgB,IAAI,CAAC;IAClC,aAAa,GAAiC,IAAI,CAAC;IAE3D,YAAY,KAAY;QACtB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED;;;;;;OAMG;IACH,MAAM;QACJ,IAAI,IAAI,CAAC,aAAa;YAAE,OAAO,IAAI,CAAC,aAAa,CAAC;QAClD,MAAM,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QAC1B,wBAAwB,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,oBAAoB,EAAE,CAAC,CAAC;QACpE,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC;QAC5B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,IAAI,CAAC,aAAa;YAAE,OAAO,IAAI,CAAC,aAAa,CAAC;QAClD,MA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CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;oBAC7B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE;wBAC7B,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;wBAC5B,OAAO,EAAE,CAAC;oBACZ,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;gBACH,cAAc,GAAG,MAAM,CAAC;gBACxB,eAAe,GAAG;oBAChB,GAAG,EAAE,UAAU,IAAI,IAAI,IAAI,EAAE;oBAC7B,IAAI;oBACJ,IAAI;iBACL,CAAC;gBACF,OAAO,eAAe,CAAC;YACzB,CAAC,CAAC,EAAE,CAAC;YACL,IAAI,CAAC;gBACH,OAAO,MAAM,aAAa,CAAC;YAC7B,CAAC;oBAAS,CAAC;gBACT,aAAa,GAAG,IAAI,CAAC;YACvB,CAAC;QACH,CAAC;QACD,KAAK,CAAC,KAAK;YACT,MAAM,MAAM,GAAG,cAAc,CAAC;YAC9B,cAAc,GAAG,IAAI,CAAC;YACtB,eAAe,GAAG,IAAI,CAAC;YACvB,IAAI,CAAC,MAAM;gBAAE,OAAO;YACpB,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;gBAClC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;YAChC,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAO;YACL,OAAO,eAAe,CAAC;QACzB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hono 与 Node http server 的最小适配层。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 该适配层只承担 fetch <-> Node http 的桥接。
|
|
6
|
+
* - AgentHTTP 直接用它把 hono router 跑成独立 Node HTTP server。
|
|
7
|
+
*/
|
|
8
|
+
import http from "node:http";
|
|
9
|
+
import type { Hono } from "hono";
|
|
10
|
+
/**
|
|
11
|
+
* 创建一个把 hono fetch 接到 node http 的 server。
|
|
12
|
+
*/
|
|
13
|
+
export declare function createNodeHttpServer(params: {
|
|
14
|
+
/** 入口 Hono 应用。 */
|
|
15
|
+
app: Hono;
|
|
16
|
+
/** 监听主机。 */
|
|
17
|
+
host: string;
|
|
18
|
+
/** 监听端口。 */
|
|
19
|
+
port: number;
|
|
20
|
+
}): http.Server;
|
|
21
|
+
//# sourceMappingURL=NodeHttpAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NodeHttpAdapter.d.ts","sourceRoot":"","sources":["../../src/http/NodeHttpAdapter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAEjC;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IAC3C,kBAAkB;IAClB,GAAG,EAAE,IAAI,CAAC;IACV,YAAY;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,YAAY;IACZ,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,IAAI,CAAC,MAAM,CAgCd"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hono 与 Node http server 的最小适配层。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 该适配层只承担 fetch <-> Node http 的桥接。
|
|
6
|
+
* - AgentHTTP 直接用它把 hono router 跑成独立 Node HTTP server。
|
|
7
|
+
*/
|
|
8
|
+
import http from "node:http";
|
|
9
|
+
import { Readable } from "node:stream";
|
|
10
|
+
import { finished } from "node:stream/promises";
|
|
11
|
+
/**
|
|
12
|
+
* 创建一个把 hono fetch 接到 node http 的 server。
|
|
13
|
+
*/
|
|
14
|
+
export function createNodeHttpServer(params) {
|
|
15
|
+
const { app, host, port } = params;
|
|
16
|
+
return http.createServer(async (req, res) => {
|
|
17
|
+
try {
|
|
18
|
+
const url = new URL(req.url || "/", `http://${host}:${port}`);
|
|
19
|
+
const method = req.method || "GET";
|
|
20
|
+
const body_buffer = await read_request_body(req);
|
|
21
|
+
const request = new Request(url.toString(), {
|
|
22
|
+
method,
|
|
23
|
+
headers: new Headers(req.headers),
|
|
24
|
+
body: body_buffer.length > 0 ? body_buffer : undefined,
|
|
25
|
+
});
|
|
26
|
+
const response = await app.fetch(request);
|
|
27
|
+
res.statusCode = response.status;
|
|
28
|
+
for (const [key, value] of response.headers.entries()) {
|
|
29
|
+
res.setHeader(key, value);
|
|
30
|
+
}
|
|
31
|
+
if (!response.body) {
|
|
32
|
+
res.end();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const body_stream = Readable.fromWeb(response.body);
|
|
36
|
+
body_stream.pipe(res);
|
|
37
|
+
await finished(body_stream).catch(() => undefined);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
res.statusCode = 500;
|
|
41
|
+
res.end("Internal Server Error");
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 读取 Node 原生请求体。
|
|
47
|
+
*/
|
|
48
|
+
async function read_request_body(req) {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
const chunks = [];
|
|
51
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
52
|
+
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
53
|
+
req.on("error", reject);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=NodeHttpAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NodeHttpAdapter.js","sourceRoot":"","sources":["../../src/http/NodeHttpAdapter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC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AC,OAAO,CAClC,QAAQ,CAAC,IAAwD,CAClE,CAAC;YACF,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACtB,MAAM,QAAQ,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QACrD,CAAC;QAAC,MAAM,CAAC;YACP,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;YACrB,GAAG,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;QACnC,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,iBAAiB,CAAC,GAAyB;IACxD,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC7C,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QAC9C,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACpD,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK HTTP session 路由。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 这组路由面向 `RemoteAgent`,只暴露最小 Session actor 使用面。
|
|
6
|
+
* - 当前公开输入收口到 `prompt()`,公开输出收口到 `events` 长连接。
|
|
7
|
+
* - 不复用 control API 的控制台语义,避免 transport 面混入非 SDK 约束。
|
|
8
|
+
*/
|
|
9
|
+
import type { Hono } from "hono";
|
|
10
|
+
import type { AgentSessionCollection } from "@downcity/agent";
|
|
11
|
+
/**
|
|
12
|
+
* 注册 SDK session 路由。
|
|
13
|
+
*/
|
|
14
|
+
export declare function registerSdkSessionRoutes(app: Hono, sessionCollection: AgentSessionCollection): void;
|
|
15
|
+
//# sourceMappingURL=SessionRoutes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SessionRoutes.d.ts","sourceRoot":"","sources":["../../../src/http/routes/SessionRoutes.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,KAAK,EAEV,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AAQzB;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,IAAI,EACT,iBAAiB,EAAE,sBAAsB,GACxC,IAAI,CA+QN"}
|