@h-ai/kit 0.1.0-alpha.67 → 0.1.0-alpha.69
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/README.en.md +67 -13
- package/README.ja.md +67 -13
- package/README.md +67 -13
- package/dist/adapter/index.d.ts +17 -2
- package/dist/adapter/index.js.map +1 -1
- package/dist/browser.d.ts +12 -6
- package/dist/browser.js +6 -7
- package/dist/browser.js.map +1 -1
- package/dist/{chunk-DVW7CQ3H.js → chunk-2PWGJCWX.js} +3 -3
- package/dist/chunk-2PWGJCWX.js.map +1 -0
- package/dist/chunk-OVRZIDCD.js +175 -0
- package/dist/chunk-OVRZIDCD.js.map +1 -0
- package/dist/{chunk-VOTDAVCB.js → chunk-QNLZLTVC.js} +29 -42
- package/dist/chunk-QNLZLTVC.js.map +1 -0
- package/dist/{chunk-MLSP67JF.js → chunk-UTCS6FR7.js} +67 -110
- package/dist/chunk-UTCS6FR7.js.map +1 -0
- package/dist/{chunk-NVYIUBU7.js → chunk-XC7I2L3G.js} +2 -2
- package/dist/chunk-XC7I2L3G.js.map +1 -0
- package/dist/{chunk-L7BTMAKX.js → chunk-Y2YUA3VR.js} +5 -6
- package/dist/chunk-Y2YUA3VR.js.map +1 -0
- package/dist/client/index.d.ts +47 -1
- package/dist/client/index.js +3 -3
- package/dist/client/index.js.map +1 -1
- package/dist/index.d.ts +251 -347
- package/dist/index.js +347 -171
- package/dist/index.js.map +1 -1
- package/dist/kit-a2a-types-DwcmE668.d.ts +136 -0
- package/dist/{index-B7s-0mZl.d.ts → kit-client-auth-B5y1g5_A.d.ts} +228 -102
- package/dist/kit-crypto-types-Cwj9bprV.d.ts +196 -0
- package/dist/{kit-i18n-D9Qqk-1k.d.ts → kit-i18n-DEzmzE5T.d.ts} +142 -29
- package/dist/{kit-types-ByWdbCeO.d.ts → kit-types-DQYqP1Cn.d.ts} +242 -162
- package/dist/modules/a2a/index.d.ts +13 -111
- package/dist/modules/a2a/index.js +2 -2
- package/dist/modules/crypto/index.d.ts +28 -195
- package/dist/modules/crypto/index.js +3 -3
- package/dist/modules/crypto/index.js.map +1 -1
- package/package.json +16 -11
- package/dist/chunk-DVW7CQ3H.js.map +0 -1
- package/dist/chunk-L7BTMAKX.js.map +0 -1
- package/dist/chunk-MLSP67JF.js.map +0 -1
- package/dist/chunk-NVYIUBU7.js.map +0 -1
- package/dist/chunk-QHYNL24G.js +0 -117
- package/dist/chunk-QHYNL24G.js.map +0 -1
- package/dist/chunk-VOTDAVCB.js.map +0 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import { f as HandleA2AOperations, h as HandleConfig } from './kit-types-DQYqP1Cn.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @h-ai/kit — A2A API Key 认证器
|
|
6
|
+
*
|
|
7
|
+
* 从请求头或 query 参数中提取 API Key,调用 IAM apiKey.verifyApiKey 验证。
|
|
8
|
+
* @module kit-a2a-auth
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A2A API Key 认证器配置
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { A2AApiKeyAuthConfig } from '@h-ai/kit'
|
|
16
|
+
*
|
|
17
|
+
* const value: A2AApiKeyAuthConfig = { in: 'header', name: 'X-Agent-Key' }
|
|
18
|
+
* void value
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
interface A2AApiKeyAuthConfig {
|
|
22
|
+
/** API Key 的传递位置 */
|
|
23
|
+
in: 'header' | 'query';
|
|
24
|
+
/** 参数名 */
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 创建 A2A API Key 认证函数
|
|
29
|
+
*
|
|
30
|
+
* 根据配置从请求头或 query 参数中提取 API Key,
|
|
31
|
+
* 通过 `iam.apiKey.verifyApiKey()` 验证后返回调用方上下文。
|
|
32
|
+
*
|
|
33
|
+
* - 无 API Key → 返回 `null`(由调用方拒绝请求)
|
|
34
|
+
* - 验证失败 → 返回 `null`(拒绝)
|
|
35
|
+
* - 验证成功 → 返回 `{ agentId, apiKeyId, scopes }`
|
|
36
|
+
*
|
|
37
|
+
* @param config - API Key 提取配置
|
|
38
|
+
* @returns authenticate 函数,可直接赋给 HandleA2AConfig.authenticate
|
|
39
|
+
* @example 模块内部上下文
|
|
40
|
+
* ```ts
|
|
41
|
+
* const authenticate = createA2AApiKeyAuthenticator({ in: 'header', name: 'X-Agent-Key' })
|
|
42
|
+
* // 应用先初始化 IAM;拒绝返回 null,交给路由转为 401。
|
|
43
|
+
* void authenticate
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function createA2AApiKeyAuthenticator(config: A2AApiKeyAuthConfig): (event: RequestEvent) => Promise<Record<string, unknown> | null>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @h-ai/kit — A2A Handle 拦截器
|
|
50
|
+
*
|
|
51
|
+
* 在 SvelteKit Handle Hook 中拦截 A2A 端点(Agent Card 发现 + JSON-RPC 处理),
|
|
52
|
+
* 自动处理请求并返回 Response,不匹配时返回 null 由 Handle 继续后续流程。
|
|
53
|
+
* @module kit-a2a-handle
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A2A 解析后的内部配置
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import type { ResolvedA2AConfig } from '@h-ai/kit'
|
|
61
|
+
*
|
|
62
|
+
* function paths(config: ResolvedA2AConfig) { return [config.cardPath, config.rpcPath] }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
interface ResolvedA2AConfig {
|
|
66
|
+
operations: HandleA2AOperations;
|
|
67
|
+
cardPath: string;
|
|
68
|
+
rpcPath: string;
|
|
69
|
+
authenticate?: (event: RequestEvent) => Promise<Record<string, unknown> | null | undefined>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 解析 A2A 配置(简单模式 / 配置模式)
|
|
73
|
+
*
|
|
74
|
+
* - 简单模式:直接传入 `ai.a2a` 操作对象,使用默认路径
|
|
75
|
+
* - 配置模式:传入 `{ operations, rpcPath?, cardPath?, authenticate? }`
|
|
76
|
+
*
|
|
77
|
+
* @param input - HandleConfig.a2a 配置
|
|
78
|
+
* @returns 解析后的配置,或 `null` 表示未配置
|
|
79
|
+
* @example 模块内部上下文
|
|
80
|
+
* ```ts
|
|
81
|
+
* function resolve(operations: HandleA2AOperations) {
|
|
82
|
+
* return resolveA2AConfig({ operations, rpcPath: '/a2a', authenticate: async () => null })
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare function resolveA2AConfig(input: HandleConfig['a2a']): ResolvedA2AConfig | null;
|
|
87
|
+
/**
|
|
88
|
+
* 处理 A2A 端点请求
|
|
89
|
+
*
|
|
90
|
+
* 匹配 Agent Card GET 和 JSON-RPC POST 端点,返回 Response。
|
|
91
|
+
* 不匹配时返回 `null`,由 Handle 继续后续流程。
|
|
92
|
+
*
|
|
93
|
+
* @param event - SvelteKit RequestEvent
|
|
94
|
+
* @param requestId - 当前请求 ID(用于响应头)
|
|
95
|
+
* @param config - 解析后的 A2A 配置
|
|
96
|
+
* @returns 匹配时返回 Response,不匹配返回 null
|
|
97
|
+
* @example 模块内部上下文
|
|
98
|
+
* ```ts
|
|
99
|
+
* async function handle(event: RequestEvent, config: ResolvedA2AConfig) {
|
|
100
|
+
* return handleA2ARequest(event, 'req_1', config) // null 表示路径未匹配,交给后续 handle。
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
declare function handleA2ARequest(event: RequestEvent, requestId: string, config: ResolvedA2AConfig): Promise<Response | null>;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @h-ai/kit — A2A 类型定义
|
|
108
|
+
*
|
|
109
|
+
* Kit A2A 路由处理器相关类型
|
|
110
|
+
* @module kit-a2a-types
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A2A 路由处理器配置
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* import type { KitA2AHandlerConfig } from '@h-ai/kit'
|
|
118
|
+
*
|
|
119
|
+
* const value: KitA2AHandlerConfig = { authenticate: async () => null }
|
|
120
|
+
* void value
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
interface KitA2AHandlerConfig {
|
|
124
|
+
/**
|
|
125
|
+
* 可选的认证回调
|
|
126
|
+
*
|
|
127
|
+
* 验证入站 A2A 请求的身份。返回 null/undefined 表示认证失败(401)。
|
|
128
|
+
* 未配置此项才允许匿名请求;回调可抛出 SvelteKit HTTP 错误。
|
|
129
|
+
*
|
|
130
|
+
* - `'apiKey'`:自动使用 IAM API Key 认证
|
|
131
|
+
* - 函数:自定义认证回调
|
|
132
|
+
*/
|
|
133
|
+
authenticate?: 'apiKey' | ((event: RequestEvent) => Promise<Record<string, unknown> | null | undefined>);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export { type A2AApiKeyAuthConfig as A, type KitA2AHandlerConfig as K, type ResolvedA2AConfig as R, createA2AApiKeyAuthenticator as c, handleA2ARequest as h, resolveA2AConfig as r };
|
|
@@ -12,51 +12,109 @@ import { z } from 'zod';
|
|
|
12
12
|
* @module kit-config
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* 服务器监听配置(对应 `config/_kit.yml` 的 `server`)。
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import type { KitServerConfig } from '@h-ai/kit'
|
|
20
|
+
*
|
|
21
|
+
* const value: KitServerConfig = { host: '127.0.0.1', port: 3000 }
|
|
22
|
+
* void value
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
16
25
|
interface KitServerConfig {
|
|
17
26
|
readonly host: string;
|
|
18
27
|
readonly port: number;
|
|
19
28
|
}
|
|
20
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* `config/_kit.yml` 中的服务器监听输入类型。
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import type { KitServerConfigInput } from '@h-ai/kit'
|
|
34
|
+
*
|
|
35
|
+
* const value: KitServerConfigInput = { port: 4000 }
|
|
36
|
+
* void value
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
21
39
|
type KitServerConfigInput = Partial<KitServerConfig>;
|
|
22
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* `config/_kit.yml` 中的 transport 配置。
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import type { KitTransportConfig } from '@h-ai/kit'
|
|
45
|
+
*
|
|
46
|
+
* const value: KitTransportConfig = { keyExchangePath: '/api/_hai/key-exchange', excludePaths: ['/api/health'] }
|
|
47
|
+
* void value
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
23
50
|
interface KitTransportConfig {
|
|
51
|
+
/** 双向明文 UTF-8 字节上限;省略使用 1 MiB,范围 1 KiB~64 MiB。 */
|
|
52
|
+
readonly maxPlaintextBytes?: number;
|
|
24
53
|
readonly keyExchangePath: `/${string}`;
|
|
25
54
|
readonly excludePaths: readonly `/${string}`[];
|
|
26
|
-
readonly requireEncryption: boolean;
|
|
27
|
-
readonly encryptResponse: boolean;
|
|
28
|
-
readonly maxClients: number;
|
|
29
55
|
}
|
|
30
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* `config/_kit.yml` 的顶层配置结构。
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import type { KitConfig } from '@h-ai/kit'
|
|
61
|
+
*
|
|
62
|
+
* const value: KitConfig = { server: { host: '127.0.0.1', port: 3000 }, transport: false }
|
|
63
|
+
* void value
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
31
66
|
interface KitConfig {
|
|
32
67
|
readonly server: KitServerConfig;
|
|
33
68
|
readonly transport: false | KitTransportConfig;
|
|
34
69
|
}
|
|
35
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* `config/_kit.yml` 中的 transport 输入类型。
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* import type { KitTransportConfigInput } from '@h-ai/kit'
|
|
75
|
+
*
|
|
76
|
+
* const value: KitTransportConfigInput = { excludePaths: ['/api/health'] }
|
|
77
|
+
* void value
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
36
80
|
type KitTransportConfigInput = Partial<{
|
|
81
|
+
readonly maxPlaintextBytes: number;
|
|
37
82
|
readonly keyExchangePath: `/${string}`;
|
|
38
83
|
readonly excludePaths: readonly `/${string}`[];
|
|
39
|
-
readonly requireEncryption: boolean;
|
|
40
|
-
readonly encryptResponse: boolean;
|
|
41
|
-
readonly maxClients: number;
|
|
42
84
|
}>;
|
|
43
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* `config/_kit.yml` 的顶层输入类型。
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import type { KitConfigInput } from '@h-ai/kit'
|
|
90
|
+
*
|
|
91
|
+
* const value: KitConfigInput = { transport: false }
|
|
92
|
+
* void value
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
44
95
|
interface KitConfigInput {
|
|
45
96
|
readonly server?: KitServerConfigInput;
|
|
46
97
|
readonly transport?: false | KitTransportConfigInput;
|
|
47
98
|
}
|
|
48
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* `config/_kit.yml` 对应的配置 Schema。
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { KitConfigSchema } from '@h-ai/kit'
|
|
104
|
+
*
|
|
105
|
+
* const config = KitConfigSchema.parse({ server: { port: 3000 }, transport: false })
|
|
106
|
+
* void config
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
49
109
|
declare const KitConfigSchema: z.ZodPipe<z.ZodObject<{
|
|
50
110
|
server: z.ZodOptional<z.ZodObject<{
|
|
51
111
|
host: z.ZodDefault<z.ZodString>;
|
|
52
112
|
port: z.ZodDefault<z.ZodNumber>;
|
|
53
113
|
}, z.core.$strip>>;
|
|
54
114
|
transport: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodObject<{
|
|
115
|
+
maxPlaintextBytes: z.ZodOptional<z.ZodNumber>;
|
|
55
116
|
keyExchangePath: z.ZodDefault<z.ZodCustom<`/${string}`, `/${string}`>>;
|
|
56
117
|
excludePaths: z.ZodDefault<z.ZodArray<z.ZodCustom<`/${string}`, `/${string}`>>>;
|
|
57
|
-
requireEncryption: z.ZodDefault<z.ZodBoolean>;
|
|
58
|
-
encryptResponse: z.ZodDefault<z.ZodBoolean>;
|
|
59
|
-
maxClients: z.ZodDefault<z.ZodNumber>;
|
|
60
118
|
}, z.core.$strip>]>>;
|
|
61
119
|
}, z.core.$strip>, z.ZodTransform<{
|
|
62
120
|
server: KitServerConfig;
|
|
@@ -69,9 +127,7 @@ declare const KitConfigSchema: z.ZodPipe<z.ZodObject<{
|
|
|
69
127
|
transport?: false | {
|
|
70
128
|
keyExchangePath: `/${string}`;
|
|
71
129
|
excludePaths: `/${string}`[];
|
|
72
|
-
|
|
73
|
-
encryptResponse: boolean;
|
|
74
|
-
maxClients: number;
|
|
130
|
+
maxPlaintextBytes?: number | undefined;
|
|
75
131
|
} | undefined;
|
|
76
132
|
}>>;
|
|
77
133
|
/**
|
|
@@ -79,12 +135,28 @@ declare const KitConfigSchema: z.ZodPipe<z.ZodObject<{
|
|
|
79
135
|
*
|
|
80
136
|
* @param input - `config/_kit.yml` 中的 transport 配置
|
|
81
137
|
* @returns 完整 transport 配置;`false` 表示关闭
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* import { resolveKitTransportConfig } from '@h-ai/kit'
|
|
141
|
+
*
|
|
142
|
+
* const transport = resolveKitTransportConfig({ excludePaths: ['/api/health'] })
|
|
143
|
+
* if (transport) void transport.keyExchangePath
|
|
144
|
+
* ```
|
|
82
145
|
*/
|
|
83
146
|
declare function resolveKitTransportConfig(input?: false | KitTransportConfigInput): false | KitTransportConfig;
|
|
84
147
|
/**
|
|
85
148
|
* 解析 `config/_kit.yml` 顶层配置并补齐默认值。
|
|
86
149
|
*
|
|
87
150
|
* 适合同一份配置同时供 SvelteKit 服务端 hook 与浏览器端 client 读取。
|
|
151
|
+
* @param input - Kit 监听和公开 transport 输入;省略字段按 Schema 补默认值。
|
|
152
|
+
* @returns 已补齐默认值的配置;不合法输入抛出 ZodError。
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { resolveKitConfig } from '@h-ai/kit'
|
|
156
|
+
*
|
|
157
|
+
* const config = resolveKitConfig({ transport: false })
|
|
158
|
+
* void config.server.port
|
|
159
|
+
* ```
|
|
88
160
|
*/
|
|
89
161
|
declare function resolveKitConfig(input?: KitConfigInput): KitConfig;
|
|
90
162
|
|
|
@@ -95,6 +167,15 @@ declare function resolveKitConfig(input?: KitConfigInput): KitConfig;
|
|
|
95
167
|
* @module kit-auth
|
|
96
168
|
*/
|
|
97
169
|
|
|
170
|
+
/**
|
|
171
|
+
* 服务端认证写入/删除 Cookie 依赖;由当前请求提供并执行安全属性。
|
|
172
|
+
* @example 模块内部上下文
|
|
173
|
+
* ```ts
|
|
174
|
+
* function signIn(cookies: CookieWriter, credentials: PasswordCredentials) {
|
|
175
|
+
* return login(cookies, credentials) // 业务调用者检查 success 后消费身份。
|
|
176
|
+
* }
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
98
179
|
interface CookieWriter {
|
|
99
180
|
set: (name: string, value: string, options: {
|
|
100
181
|
path: string;
|
|
@@ -107,7 +188,14 @@ interface CookieWriter {
|
|
|
107
188
|
path: string;
|
|
108
189
|
}) => void;
|
|
109
190
|
}
|
|
110
|
-
/**
|
|
191
|
+
/**
|
|
192
|
+
* 浏览器 Token 存储器;使用页面内存或平台安全存储,禁止写入 Web Storage。
|
|
193
|
+
* @example 模块内部上下文
|
|
194
|
+
* ```ts
|
|
195
|
+
* const store: BrowserTokenStore = getDefaultBrowserTokenStore()
|
|
196
|
+
* store.clear() // 当前页面内存状态;不持有服务端会话真值。
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
111
199
|
interface BrowserTokenStore {
|
|
112
200
|
get: () => string | null;
|
|
113
201
|
set: (token: string) => void;
|
|
@@ -119,12 +207,13 @@ interface BrowserTokenStore {
|
|
|
119
207
|
* @param cookies - SvelteKit cookies 对象
|
|
120
208
|
* @param credentials - 密码凭证(identifier + password)
|
|
121
209
|
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
210
|
+
* @example 模块内部上下文
|
|
124
211
|
* ```ts
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
212
|
+
* async function signIn(cookies: CookieWriter, credentials: PasswordCredentials) {
|
|
213
|
+
* const result = await login(cookies, credentials)
|
|
214
|
+
* if (!result.success) return result.error.message
|
|
215
|
+
* return result.data
|
|
216
|
+
* }
|
|
128
217
|
* ```
|
|
129
218
|
*/
|
|
130
219
|
declare function login(cookies: CookieWriter, credentials: PasswordCredentials): Promise<HaiResult<AuthResult>>;
|
|
@@ -134,10 +223,13 @@ declare function login(cookies: CookieWriter, credentials: PasswordCredentials):
|
|
|
134
223
|
* @param cookies - SvelteKit cookies 对象
|
|
135
224
|
* @param credentials - OTP 凭证(identifier + code)
|
|
136
225
|
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
226
|
+
* @example 模块内部上下文
|
|
139
227
|
* ```ts
|
|
140
|
-
*
|
|
228
|
+
* async function signIn(cookies: CookieWriter, credentials: OtpCredentials) {
|
|
229
|
+
* const result = await loginWithOtp(cookies, credentials)
|
|
230
|
+
* if (!result.success) return result.error.message
|
|
231
|
+
* return result.data
|
|
232
|
+
* }
|
|
141
233
|
* ```
|
|
142
234
|
*/
|
|
143
235
|
declare function loginWithOtp(cookies: CookieWriter, credentials: OtpCredentials): Promise<HaiResult<AuthResult>>;
|
|
@@ -147,10 +239,13 @@ declare function loginWithOtp(cookies: CookieWriter, credentials: OtpCredentials
|
|
|
147
239
|
* @param cookies - SvelteKit cookies 对象
|
|
148
240
|
* @param credentials - LDAP 凭证(username + password)
|
|
149
241
|
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
242
|
+
* @example 模块内部上下文
|
|
152
243
|
* ```ts
|
|
153
|
-
*
|
|
244
|
+
* async function signIn(cookies: CookieWriter, credentials: LdapCredentials) {
|
|
245
|
+
* const result = await loginWithLdap(cookies, credentials)
|
|
246
|
+
* if (!result.success) return result.error.message
|
|
247
|
+
* return result.data
|
|
248
|
+
* }
|
|
154
249
|
* ```
|
|
155
250
|
*/
|
|
156
251
|
declare function loginWithLdap(cookies: CookieWriter, credentials: LdapCredentials): Promise<HaiResult<AuthResult>>;
|
|
@@ -160,11 +255,13 @@ declare function loginWithLdap(cookies: CookieWriter, credentials: LdapCredentia
|
|
|
160
255
|
* @param cookies - SvelteKit cookies 对象
|
|
161
256
|
* @param credentials - API Key 凭证(key)
|
|
162
257
|
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
258
|
+
* @example 模块内部上下文
|
|
165
259
|
* ```ts
|
|
166
|
-
*
|
|
167
|
-
*
|
|
260
|
+
* async function signIn(cookies: CookieWriter, credentials: ApiKeyCredentials) {
|
|
261
|
+
* const result = await loginWithApiKey(cookies, credentials)
|
|
262
|
+
* if (!result.success) return result.error.message
|
|
263
|
+
* return result.data
|
|
264
|
+
* }
|
|
168
265
|
* ```
|
|
169
266
|
*/
|
|
170
267
|
declare function loginWithApiKey(cookies: CookieWriter, credentials: ApiKeyCredentials): Promise<HaiResult<AuthResult>>;
|
|
@@ -174,42 +271,55 @@ declare function loginWithApiKey(cookies: CookieWriter, credentials: ApiKeyCrede
|
|
|
174
271
|
* @param cookies - SvelteKit cookies 对象
|
|
175
272
|
* @param options - 注册选项(username、password、email 等)
|
|
176
273
|
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
177
|
-
*
|
|
178
|
-
* @example
|
|
274
|
+
* @example 模块内部上下文
|
|
179
275
|
* ```ts
|
|
180
|
-
*
|
|
276
|
+
* async function signIn(cookies: CookieWriter, credentials: RegisterOptions) {
|
|
277
|
+
* const result = await registerAndLogin(cookies, credentials)
|
|
278
|
+
* if (!result.success) return result.error.message
|
|
279
|
+
* return result.data
|
|
280
|
+
* }
|
|
181
281
|
* ```
|
|
182
282
|
*/
|
|
183
283
|
declare function registerAndLogin(cookies: CookieWriter, options: RegisterOptions): Promise<HaiResult<AuthResult>>;
|
|
184
284
|
/**
|
|
185
|
-
*
|
|
285
|
+
* 服务端登出:委托 iam.auth.logout 原子撤销凭据,并始终清除当前请求的 Token Cookie。
|
|
286
|
+
* Cookie 清理只代表本地退出;服务端撤销失败仍返回失败,不能由调用端改成成功响应。
|
|
186
287
|
*
|
|
187
288
|
* @param cookies - SvelteKit cookies 对象
|
|
188
289
|
* @param accessToken - 访问令牌(为 null/undefined 时仅清除 Cookie)
|
|
189
|
-
*
|
|
190
|
-
* @
|
|
290
|
+
* @returns 服务端撤销结果;无令牌时仅清理 Cookie 并成功,有令牌但未配置认证操作时失败。
|
|
291
|
+
* @remarks 底层控制流异常仍以 Promise 拒绝传播;Cookie 清理不吞掉业务失败。
|
|
292
|
+
* @example 模块内部上下文
|
|
191
293
|
* ```ts
|
|
192
|
-
*
|
|
193
|
-
*
|
|
294
|
+
* async function signOut(cookies: CookieWriter, accessToken: string) {
|
|
295
|
+
* const result = await logout(cookies, accessToken)
|
|
296
|
+
* if (!result.success) return result.error.code
|
|
297
|
+
* return result.data
|
|
298
|
+
* }
|
|
194
299
|
* ```
|
|
195
300
|
*/
|
|
196
|
-
declare function logout(cookies: CookieWriter, accessToken?: string | null): Promise<void
|
|
197
|
-
/**
|
|
198
|
-
* 创建浏览器端 Token 存储器(localStorage)。
|
|
199
|
-
*
|
|
200
|
-
* ⚠️ 安全提示:localStorage 对同源脚本可读,任意 XSS 都可能窃取 Token。
|
|
201
|
-
* 本函数仅供调用方显式接受风险时使用;`kit.client.create({ auth: true })`
|
|
202
|
-
* 不会默认调用它。
|
|
203
|
-
*
|
|
204
|
-
* @param key - localStorage 键名,默认 `'hai_access_token'`
|
|
205
|
-
*/
|
|
206
|
-
declare function createTokenStore(key?: string): BrowserTokenStore;
|
|
301
|
+
declare function logout(cookies: CookieWriter, accessToken?: string | null): Promise<HaiResult<void>>;
|
|
207
302
|
/**
|
|
208
303
|
* 写入默认浏览器端 Access Token(仅页面内存,不持久化到 localStorage)。
|
|
304
|
+
* @param token - 当前页面内存中使用的 access token,不写入 Web Storage。
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* import { kit } from '@h-ai/kit'
|
|
308
|
+
*
|
|
309
|
+
* kit.auth.setBrowserToken('example-only-token')
|
|
310
|
+
* // 页面注销时同步清除浏览器内存令牌。
|
|
311
|
+
* kit.auth.clearBrowserToken()
|
|
312
|
+
* ```
|
|
209
313
|
*/
|
|
210
314
|
declare function setBrowserToken(token: string): void;
|
|
211
315
|
/**
|
|
212
316
|
* 清除默认浏览器端 Access Token(仅页面内存)。
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* import { kit } from '@h-ai/kit'
|
|
320
|
+
*
|
|
321
|
+
* kit.auth.clearBrowserToken()
|
|
322
|
+
* ```
|
|
213
323
|
*/
|
|
214
324
|
declare function clearBrowserToken(): void;
|
|
215
325
|
|
|
@@ -223,8 +333,17 @@ declare function clearBrowserToken(): void;
|
|
|
223
333
|
|
|
224
334
|
/**
|
|
225
335
|
* 传输加密客户端配置
|
|
336
|
+
* @example
|
|
337
|
+
* ```ts
|
|
338
|
+
* import type { ClientTransportConfig } from '@h-ai/kit'
|
|
339
|
+
* import { crypto } from '@h-ai/crypto'
|
|
340
|
+
*
|
|
341
|
+
* function configAfterInit(): ClientTransportConfig { return { crypto, excludePaths: ['/api/health'] } }
|
|
342
|
+
* ```
|
|
226
343
|
*/
|
|
227
344
|
interface ClientTransportConfig {
|
|
345
|
+
/** 可选本地明文上限;缺省跟随服务端握手。 */
|
|
346
|
+
maxPlaintextBytes?: number;
|
|
228
347
|
/** @h-ai/crypto 服务实例 */
|
|
229
348
|
crypto: CryptoFunctions;
|
|
230
349
|
/** 密钥交换端点 URL(默认 `'/api/_hai/key-exchange'`) */
|
|
@@ -234,6 +353,13 @@ interface ClientTransportConfig {
|
|
|
234
353
|
}
|
|
235
354
|
/**
|
|
236
355
|
* Kit 客户端配置
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* import type { KitClientConfig } from '@h-ai/kit'
|
|
359
|
+
*
|
|
360
|
+
* const value: KitClientConfig = { auth: true }
|
|
361
|
+
* void value
|
|
362
|
+
* ```
|
|
237
363
|
*/
|
|
238
364
|
interface KitClientConfig {
|
|
239
365
|
/**
|
|
@@ -256,10 +382,20 @@ interface KitClientConfig {
|
|
|
256
382
|
}
|
|
257
383
|
/**
|
|
258
384
|
* Kit 客户端实例
|
|
385
|
+
* @example
|
|
386
|
+
* ```ts
|
|
387
|
+
* import type { KitClient } from '@h-ai/kit'
|
|
388
|
+
*
|
|
389
|
+
* async function read(client: KitClient) {
|
|
390
|
+
* const response = await client.apiFetch('/api/health')
|
|
391
|
+
* if (!response.ok) throw new Error('Request failed')
|
|
392
|
+
* return response.json()
|
|
393
|
+
* }
|
|
394
|
+
* ```
|
|
259
395
|
*/
|
|
260
396
|
interface KitClient {
|
|
261
397
|
/** 统一 API fetch(写请求自动附加 CSRF,启用 transport 时自动加解密) */
|
|
262
|
-
apiFetch: (
|
|
398
|
+
apiFetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
263
399
|
/** 传输加密是否就绪(未启用传输加密时始终为 true) */
|
|
264
400
|
readonly ready: boolean;
|
|
265
401
|
/**
|
|
@@ -279,6 +415,14 @@ interface KitClient {
|
|
|
279
415
|
* 静态资源以及 multipart/form-data 等上传请求保持原样。应用只需安装一次,
|
|
280
416
|
* 后续 `fetch` / SvelteKit 内部 data fetch / `kit.client.create()` 明文发送路径
|
|
281
417
|
* 都会共用这层传输加密能力。
|
|
418
|
+
* @param transportConfig - 已初始化 crypto、同源密钥交换路径及排除路径;省略时不安装包装。
|
|
419
|
+
* @example
|
|
420
|
+
* ```ts
|
|
421
|
+
* import { kit } from '@h-ai/kit'
|
|
422
|
+
*
|
|
423
|
+
* // 不传配置时不安装全局包装;启用时应传已初始化 crypto 与同源端点。
|
|
424
|
+
* kit.client.installBrowserTransportFetch()
|
|
425
|
+
* ```
|
|
282
426
|
*/
|
|
283
427
|
declare function installBrowserTransportFetch(transportConfig?: ClientTransportConfig): void;
|
|
284
428
|
/**
|
|
@@ -294,16 +438,14 @@ declare function installBrowserTransportFetch(transportConfig?: ClientTransportC
|
|
|
294
438
|
* @param config 已解析的 kit 顶层配置
|
|
295
439
|
* @param deps 外部依赖
|
|
296
440
|
* @param deps.crypto `@h-ai/crypto` 模块实例
|
|
297
|
-
*
|
|
298
441
|
* @example
|
|
299
442
|
* ```ts
|
|
300
|
-
* import { browser } from '$app/environment'
|
|
301
|
-
* import { crypto } from '@h-ai/crypto'
|
|
302
443
|
* import { kit } from '@h-ai/kit'
|
|
303
|
-
* import { adminConsoleKitConfig } from '$lib/config/kit-config.js'
|
|
304
444
|
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
445
|
+
* import { crypto } from '@h-ai/crypto'
|
|
446
|
+
*
|
|
447
|
+
* function installAfterCryptoInit() {
|
|
448
|
+
* kit.client.installBrowserTransport({ server: { host: '127.0.0.1', port: 3000 }, transport: { keyExchangePath: '/api/_hai/key-exchange', excludePaths: ['/api/health'] } }, { crypto })
|
|
307
449
|
* }
|
|
308
450
|
* ```
|
|
309
451
|
*/
|
|
@@ -321,17 +463,38 @@ declare function installBrowserTransport(config: KitConfig, deps: {
|
|
|
321
463
|
*
|
|
322
464
|
* @param config - 客户端配置
|
|
323
465
|
* @returns KitClient 实例
|
|
324
|
-
*
|
|
325
466
|
* @example
|
|
326
467
|
* ```ts
|
|
327
|
-
*
|
|
328
|
-
*
|
|
468
|
+
* import { kit } from '@h-ai/kit'
|
|
469
|
+
*
|
|
470
|
+
* const client = kit.client.create({ auth: true })
|
|
471
|
+
* try {
|
|
472
|
+
* await client.init() // Client API 使用 Promise 拒绝;不是 HaiResult。
|
|
473
|
+
* const response = await client.apiFetch('/api/health')
|
|
474
|
+
* if (!response.ok) throw new Error('Request failed')
|
|
475
|
+
* } finally {
|
|
476
|
+
* client.destroy()
|
|
477
|
+
* }
|
|
329
478
|
* ```
|
|
330
479
|
*/
|
|
331
480
|
declare function createKitClient(config?: KitClientConfig): KitClient;
|
|
332
481
|
|
|
333
482
|
/** 与标准 Kit 认证端点配套的表单动作;复用已有 CSRF/transport fetch。 */
|
|
334
483
|
|
|
484
|
+
/**
|
|
485
|
+
* 创建标准 Cookie 认证表单动作;复用可选 apiFetch 以保留 CSRF 和传输配置。
|
|
486
|
+
* @param options - 可复用的 apiFetch 和标准认证端点 basePath。
|
|
487
|
+
* @param options.apiFetch - 已装配 CSRF/transport 的 fetch;省略时使用默认 Kit 客户端。
|
|
488
|
+
* @param options.basePath - 同源认证端点前缀,默认 /api/auth。
|
|
489
|
+
* @returns 标准表单动作;业务返回 HaiResult,网络/格式错误会拒绝 Promise。
|
|
490
|
+
* @example
|
|
491
|
+
* ```ts
|
|
492
|
+
* import { kit } from '@h-ai/kit'
|
|
493
|
+
*
|
|
494
|
+
* const actions = kit.client.createAuthActions({ basePath: '/api/auth' })
|
|
495
|
+
* void actions
|
|
496
|
+
* ```
|
|
497
|
+
*/
|
|
335
498
|
declare function createKitAuthActions(options?: {
|
|
336
499
|
apiFetch?: KitClient['apiFetch'];
|
|
337
500
|
basePath?: string;
|
|
@@ -347,41 +510,4 @@ declare function createKitAuthActions(options?: {
|
|
|
347
510
|
confirmPasswordReset: (token: string, newPassword: string) => Promise<HaiResult<unknown>>;
|
|
348
511
|
};
|
|
349
512
|
|
|
350
|
-
|
|
351
|
-
* @h-ai/kit — SvelteKit 路由适配器
|
|
352
|
-
*
|
|
353
|
-
* 此前位于 `@h-ai/ui/sveltekit`,为避免 `@h-ai/ui` 耦合 SvelteKit,迁移至本模块。
|
|
354
|
-
*
|
|
355
|
-
* 为 `@h-ai/ui` 的 `CrudPage` 等需要“宿主路由”的场景组件提供 SvelteKit 实现。
|
|
356
|
-
* 返回值结构与 `@h-ai/ui` 的 `NavAdapter` 完全一致;TS 通过结构类型自动匹配,
|
|
357
|
-
* 无需在 `@h-ai/kit` 中显式依赖 `@h-ai/ui`。
|
|
358
|
-
*
|
|
359
|
-
* @example
|
|
360
|
-
* ```svelte
|
|
361
|
-
* <script lang='ts'>
|
|
362
|
-
* import { CrudPage } from '@h-ai/ui'
|
|
363
|
-
* import { createSvelteKitNavAdapter } from '@h-ai/kit/client'
|
|
364
|
-
*
|
|
365
|
-
* const nav = createSvelteKitNavAdapter()
|
|
366
|
-
* </script>
|
|
367
|
-
*
|
|
368
|
-
* <CrudPage {crud} {data} {nav} />
|
|
369
|
-
* ```
|
|
370
|
-
*
|
|
371
|
-
* @module kit-nav-adapter
|
|
372
|
-
*/
|
|
373
|
-
/**
|
|
374
|
-
* SvelteKit `NavAdapter`:
|
|
375
|
-
*
|
|
376
|
-
* - `pathname` 来自 `$app/state` 的 `page.url.pathname`(响应式)。
|
|
377
|
-
* - `navigate` 使用 `$app/navigation` 的 `goto`,并自动 `invalidateAll`,
|
|
378
|
-
* 触发 `+page.server.ts` 的 `load` 重新执行。
|
|
379
|
-
* - `refresh` 调用 `invalidateAll()`,常用于写操作后刷新列表数据。
|
|
380
|
-
*/
|
|
381
|
-
declare function createSvelteKitNavAdapter(): {
|
|
382
|
-
readonly pathname: string;
|
|
383
|
-
readonly navigate: (url: string) => Promise<void>;
|
|
384
|
-
readonly refresh: () => Promise<void>;
|
|
385
|
-
};
|
|
386
|
-
|
|
387
|
-
export { type ClientTransportConfig as C, type KitClient as K, createKitAuthActions as a, installBrowserTransportFetch as b, createKitClient as c, clearBrowserToken as d, createTokenStore as e, type KitClientConfig as f, type KitConfig as g, type KitConfigInput as h, installBrowserTransport as i, KitConfigSchema as j, type KitServerConfig as k, type KitServerConfigInput as l, type KitTransportConfig as m, type KitTransportConfigInput as n, createSvelteKitNavAdapter as o, resolveKitTransportConfig as p, login as q, resolveKitConfig as r, setBrowserToken as s, loginWithOtp as t, loginWithLdap as u, loginWithApiKey as v, registerAndLogin as w, logout as x };
|
|
513
|
+
export { type ClientTransportConfig as C, type KitClient as K, createKitAuthActions as a, installBrowserTransportFetch as b, createKitClient as c, clearBrowserToken as d, type KitClientConfig as e, type KitConfig as f, type KitConfigInput as g, KitConfigSchema as h, installBrowserTransport as i, type KitServerConfig as j, type KitServerConfigInput as k, type KitTransportConfig as l, type KitTransportConfigInput as m, resolveKitTransportConfig as n, login as o, loginWithOtp as p, loginWithLdap as q, resolveKitConfig as r, setBrowserToken as s, loginWithApiKey as t, registerAndLogin as u, logout as v };
|