aegra-hono 0.11.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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://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,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (c) 2025 Muhammad Ibrahim (@ibbybuilds)
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ <p align="center">
2
+ <img src="docs/images/banner.png" alt="Aegra 横幅" />
3
+ </p>
4
+
5
+ <h1 align="center">Aegra</h1>
6
+
7
+ <p align="center"><strong>面向 TypeScript LangGraph 应用的自托管 Agent Protocol 服务端。</strong></p>
8
+
9
+ Aegra 运行在 Bun 与 Hono 之上,应用数据通过 Drizzle ORM 和 PostgreSQL 存储,并可选用 Redis 实现分布式 worker 与可续传的 SSE 流。
10
+
11
+ ## 快速开始
12
+
13
+ 环境要求:Bun 1.3.10+ 与 PostgreSQL。Redis 可选:本地执行不需要,分布式 worker 需要。
14
+
15
+ 使用已安装的 CLI:
16
+
17
+ ```bash
18
+ aegra init ./my-agent
19
+ cd my-agent
20
+ bun install
21
+ cp .env.example .env
22
+ bun run dev
23
+ ```
24
+
25
+ 直接运行本仓库:
26
+
27
+ ```bash
28
+ bun install --frozen-lockfile
29
+ cp .env.example .env
30
+ bun run dev
31
+ ```
32
+
33
+ 或启动完整技术栈:
34
+
35
+ ```bash
36
+ docker compose up --build
37
+ ```
38
+
39
+ 服务默认监听 `http://localhost:2026`,健康检查端点为 `GET /health`,接口文档位于 `/docs`:
40
+
41
+ | 端点 | 内容 |
42
+ |:--|:--|
43
+ | `GET /docs` | Agent Protocol 接口的 Swagger UI |
44
+ | `GET /redoc` | 同一份文档的 ReDoc 渲染 |
45
+ | `GET /openapi.json` | 原始 OpenAPI 3.1 文档(仓库内的契约文件) |
46
+
47
+ `src/openapi.json` 是 Agent Protocol 的权威契约,`docs/openapi.json` 是它的镜像。测试会校验「文档中的每个操作都有对应路由」以及「不存在未写入文档的协议路由」,因此文档不会与实现脱节。
48
+
49
+ ## 配置图
50
+
51
+ `aegra.json` 指向 TypeScript 导出:
52
+
53
+ ```json
54
+ {
55
+ "graphs": {
56
+ "agent": "./examples/react_agent/graph.ts:graph"
57
+ }
58
+ }
59
+ ```
60
+
61
+ Python 的 graph、auth 与自定义路由模块无法在 Bun 运行时执行,但 Agent Protocol 客户端仍可用任意语言编写。
62
+
63
+ ## 运行时支持的能力
64
+
65
+ - 助手 CRUD、版本历史、schema、图可视化与子图
66
+ - 线程 CRUD、PostgreSQL checkpoint、状态/历史接口与 TTL 清理
67
+ - 后台运行、等待、流式、无状态、中断、恢复与取消等运行流程
68
+ - Agent Protocol 的运行级与线程级 SSE,支持 Redis 跨实例重放
69
+ - Redis worker:租约、心跳、超时、取消与异常退出的运行恢复
70
+ - 定时运行:支持时区的 cron 表达式与多实例安全的抢占
71
+ - 带命名空间的 PostgreSQL 键值存储:支持共享 scope 与可选的 pgvector 语义检索
72
+ - TypeScript 鉴权、细粒度授权处理器与自定义 Hono 路由
73
+ - 可选的 Prometheus 指标
74
+
75
+ 当前限制见[功能支持](docs/feature-support.mdx)。
76
+
77
+ ## 常用命令
78
+
79
+ ```bash
80
+ bun run dev # 热重载开发服务器
81
+ bun run start # 生产服务器
82
+ bun run check # TypeScript 检查与测试
83
+ bun src/cli.ts db upgrade # 执行幂等的数据库初始化
84
+ bun src/cli.ts init ./agent # 创建 TypeScript 起步项目
85
+ bun src/cli.ts up --build # Docker Compose 启动
86
+ bun src/cli.ts down # Docker Compose 停止
87
+ bun src/cli.ts version # 输出版本号
88
+ ```
89
+
90
+ ## 架构
91
+
92
+ | 领域 | 实现 |
93
+ |:--|:--|
94
+ | HTTP | 原生 `Bun.serve` + Hono |
95
+ | 校验 | Zod |
96
+ | 持久化 | Drizzle ORM + `postgres.js` + PostgreSQL |
97
+ | 图状态 | LangGraph `PostgresSaver` |
98
+ | 分布式执行 | Redis 队列与发布订阅 |
99
+ | 图与扩展 | TypeScript 模块导出 |
100
+
101
+ Drizzle schema 位于 `src/core/schema.ts`,共享连接位于 `src/core/database.ts`。常规应用查询走 Drizzle,裸 SQL 仅用于初始化 DDL 和 PostgreSQL 特有的 checkpoint 清理。
102
+
103
+ ## 开发
104
+
105
+ ```bash
106
+ bun run typecheck
107
+ bun test
108
+ bun run check
109
+ ```
110
+
111
+ 仓库约定见 [CONTRIBUTING.md](CONTRIBUTING.md) 与 [CLAUDE.md](CLAUDE.md)。
112
+
113
+ ## 许可证
114
+
115
+ Apache 2.0,详见 [LICENSE](LICENSE)。
package/dist/auth.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { AuthUser, JsonObject } from "./types.js";
2
+ export interface AuthorizationContext {
3
+ user: AuthUser;
4
+ resource: string;
5
+ action: string;
6
+ permissions: string[];
7
+ }
8
+ export type AuthenticateHandler = (request: Request) => AuthUser | Promise<AuthUser>;
9
+ export type AuthorizationHandler = (context: AuthorizationContext, value: JsonObject) => boolean | JsonObject | void | Promise<boolean | JsonObject | void>;
10
+ export declare class Auth {
11
+ authenticateHandler?: AuthenticateHandler;
12
+ readonly handlers: Map<string, AuthorizationHandler>;
13
+ authenticate(handler: AuthenticateHandler): AuthenticateHandler;
14
+ on(resource: string, action: string, handler: AuthorizationHandler): AuthorizationHandler;
15
+ handler(resource: string, action: string): AuthorizationHandler | undefined;
16
+ }
package/dist/auth.js ADDED
@@ -0,0 +1,38 @@
1
+ // @bun
2
+ var __defProp = Object.defineProperty;
3
+ var __returnValue = (v) => v;
4
+ function __exportSetter(name, newValue) {
5
+ this[name] = __returnValue.bind(null, newValue);
6
+ }
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true,
12
+ configurable: true,
13
+ set: __exportSetter.bind(all, name)
14
+ });
15
+ };
16
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
17
+
18
+ // src/auth.ts
19
+ class Auth {
20
+ authenticateHandler;
21
+ handlers = new Map;
22
+ authenticate(handler) {
23
+ this.authenticateHandler = handler;
24
+ return handler;
25
+ }
26
+ on(resource, action, handler) {
27
+ this.handlers.set(`${resource}:${action}`, handler);
28
+ return handler;
29
+ }
30
+ handler(resource, action) {
31
+ return this.handlers.get(`${resource}:${action}`) ?? this.handlers.get(`${resource}:*`) ?? this.handlers.get(`*:${action}`) ?? this.handlers.get("*:*");
32
+ }
33
+ }
34
+ export {
35
+ Auth
36
+ };
37
+
38
+ //# debugId=3B0927EBA12BFB6E64756E2164756E21
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/auth.ts"],
4
+ "sourcesContent": [
5
+ "import type { AuthUser, JsonObject } from \"./types.ts\";\n\nexport interface AuthorizationContext {\n user: AuthUser;\n resource: string;\n action: string;\n permissions: string[];\n}\n\nexport type AuthenticateHandler = (request: Request) => AuthUser | Promise<AuthUser>;\nexport type AuthorizationHandler = (\n context: AuthorizationContext,\n value: JsonObject,\n) => boolean | JsonObject | void | Promise<boolean | JsonObject | void>;\n\nexport class Auth {\n authenticateHandler?: AuthenticateHandler;\n readonly handlers = new Map<string, AuthorizationHandler>();\n\n authenticate(handler: AuthenticateHandler): AuthenticateHandler {\n this.authenticateHandler = handler;\n return handler;\n }\n\n on(resource: string, action: string, handler: AuthorizationHandler): AuthorizationHandler {\n this.handlers.set(`${resource}:${action}`, handler);\n return handler;\n }\n\n handler(resource: string, action: string): AuthorizationHandler | undefined {\n return this.handlers.get(`${resource}:${action}`)\n ?? this.handlers.get(`${resource}:*`)\n ?? this.handlers.get(`*:${action}`)\n ?? this.handlers.get(\"*:*\");\n }\n}\n"
6
+ ],
7
+ "mappings": ";;;;;;;;;;;;;;;;;;AAeO,MAAM,KAAK;AAAA,EAChB;AAAA,EACS,WAAW,IAAI;AAAA,EAExB,YAAY,CAAC,SAAmD;AAAA,IAC9D,KAAK,sBAAsB;AAAA,IAC3B,OAAO;AAAA;AAAA,EAGT,EAAE,CAAC,UAAkB,QAAgB,SAAqD;AAAA,IACxF,KAAK,SAAS,IAAI,GAAG,YAAY,UAAU,OAAO;AAAA,IAClD,OAAO;AAAA;AAAA,EAGT,OAAO,CAAC,UAAkB,QAAkD;AAAA,IAC1E,OAAO,KAAK,SAAS,IAAI,GAAG,YAAY,QAAQ,KAC3C,KAAK,SAAS,IAAI,GAAG,YAAY,KACjC,KAAK,SAAS,IAAI,KAAK,QAAQ,KAC/B,KAAK,SAAS,IAAI,KAAK;AAAA;AAEhC;",
8
+ "debugId": "3B0927EBA12BFB6E64756E2164756E21",
9
+ "names": []
10
+ }