@zhin.js/host-http 1.0.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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 凉菜
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @zhin.js/host-http
2
+
3
+ Minimal HTTP and WebSocket host for Plugin Runtime. Provides `httpHostToken` so Adapters and
4
+ Console can register path-scoped WebSocket / HTTP routes without importing Koa or the legacy
5
+ `@zhin.js/host-router`.
6
+
7
+ Root installs the host via `installResources` (see `basic/cli/src/plugin-runtime/http-host-installer.ts`
8
+ and `console-host-installer.ts`).
9
+
10
+ ## Capabilities (this slice)
11
+
12
+ - Path-scoped WebSocket upgrades (`ws(path)`)
13
+ - HTTP `route(method, path, handler, meta?)` (exact + `/*` prefix)
14
+ - Built-in `GET /pub/health` and `GET /pub/openapi.json`
15
+ - CORS allowlist (always includes `https://console.zhin.dev`)
16
+ - Optional Bearer auth for `/api/*` when `http.token` / `http.tokens` are set (ADR 0016 scopes)
17
+ - WebSocket upgrade auth (Authorization or `?token=`); demo scope limited to `/sandbox`
18
+ - `readJsonBody()` helper for JSON POST/PUT bodies (no Koa)
19
+
20
+ ```ts
21
+ import { httpHostToken, type HttpHost } from '@zhin.js/host-http';
22
+
23
+ const http = context.use(httpHostToken);
24
+ const handle = http.ws('/sandbox');
25
+ handle.onConnection(({ socket, authScope }) => { /* ... */ });
26
+
27
+ http.route('GET', '/api/secure', (_req, res, _url, scope) => {
28
+ res.writeHead(200, { 'content-type': 'application/json' });
29
+ res.end(JSON.stringify({ scope }));
30
+ });
31
+ ```
32
+
33
+ ## Config (`http` in Root YAML)
34
+
35
+ ```yaml
36
+ http:
37
+ host: 127.0.0.1
38
+ port: 8086
39
+ token: ${HTTP_TOKEN} # full scope
40
+ tokens:
41
+ - token: ${DEMO_TOKEN}
42
+ scope: demo
43
+ corsOrigins:
44
+ - https://example.test
45
+ base: /api # auth prefix
46
+ ```
47
+
48
+ Still deferred to later host migration: full `@zhin.js/host-api` management-plane REST/RPC/SSE
49
+ and retiring the legacy Koa `@zhin.js/host-router` stack.
@@ -0,0 +1,70 @@
1
+ import type { HttpHost } from './http-host.js';
2
+ /** 与 basic/cli 接线方对齐的上下文约定。 */
3
+ export interface ConsoleRestCtx {
4
+ /** false = demo 部署:只读 GET 放行,写操作一律 403。 */
5
+ readonly fullScope: boolean;
6
+ readonly projectRoot: string;
7
+ readonly getEndpoints?: () => readonly {
8
+ name: string;
9
+ adapter: string;
10
+ connected: boolean;
11
+ status: string;
12
+ }[];
13
+ /**
14
+ * 可选:返回新 Runtime 的 agent 门面(见 {@link ConsoleAgentRuntime})。
15
+ * 未接线时 introspection 降级为空列表 + note,session tree 返回 503。
16
+ */
17
+ readonly getAgentRuntime?: () => unknown;
18
+ /** Database host(logs 页数据源:SystemLog 模型)。 */
19
+ readonly databaseHost?: {
20
+ readonly dialect: string;
21
+ readonly started: boolean;
22
+ readonly models: {
23
+ get(name: string): unknown;
24
+ };
25
+ };
26
+ }
27
+ /** 内省数据门面 — 由 basic/cli 用 agent 包 collectIntrospection* 装配。 */
28
+ export interface ConsoleAgentIntrospection {
29
+ commands?(): readonly unknown[];
30
+ bindings?(): readonly unknown[];
31
+ tools?(): readonly unknown[];
32
+ mcp?(): {
33
+ rows: readonly unknown[];
34
+ note?: string;
35
+ } | readonly unknown[];
36
+ }
37
+ /** Session tree 门面 — 形状对齐 agent 包 SessionTreeRuntimeHandle。 */
38
+ export interface ConsoleAgentSessionTree {
39
+ resolveActiveSessionId(sessionKey: string): Promise<string | null>;
40
+ agentSessionStore: {
41
+ getBySessionId(sessionId: string): Promise<{
42
+ active_leaf_message_id?: number | null;
43
+ } | null | undefined>;
44
+ };
45
+ listBranchPoints(sessionId: string): Promise<readonly unknown[]>;
46
+ switchActiveLeaf(sessionId: string, messageId: number): Promise<boolean>;
47
+ jumpToBranchIndex(sessionId: string, index: number): Promise<{
48
+ ok: boolean;
49
+ message: string;
50
+ }>;
51
+ }
52
+ /** `ctx.getAgentRuntime()` 返回值的最小结构约定(全部可选,逐项降级)。 */
53
+ export interface ConsoleAgentRuntime {
54
+ readonly introspection?: ConsoleAgentIntrospection;
55
+ readonly sessionTree?: ConsoleAgentSessionTree;
56
+ }
57
+ export interface ConsoleRestPagesOptions {
58
+ /** 默认 `/api`。 */
59
+ readonly apiBase?: string;
60
+ /** 测试注入用;默认全局 fetch。 */
61
+ readonly fetchFn?: typeof fetch;
62
+ /** 默认 `https://zhin.js.org/plugins.json`(legacy 同源)。 */
63
+ readonly pluginRegistryUrl?: string;
64
+ /** 默认 `https://registry.npmmirror.com`(legacy 同源)。 */
65
+ readonly npmRegistryUrl?: string;
66
+ }
67
+ /**
68
+ * 注册 Console 页面 REST 路由。返回注销函数(注销全部已注册路由)。
69
+ */
70
+ export declare function registerConsoleRestPages(host: HttpHost, ctx: ConsoleRestCtx, options?: ConsoleRestPagesOptions): () => void;