@h-ai/kit 0.1.0-alpha.66 → 0.1.0-alpha.68

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.
Files changed (44) hide show
  1. package/README.en.md +80 -46
  2. package/README.ja.md +72 -40
  3. package/README.md +73 -41
  4. package/dist/adapter/index.d.ts +17 -2
  5. package/dist/adapter/index.js.map +1 -1
  6. package/dist/browser.d.ts +12 -6
  7. package/dist/browser.js +6 -7
  8. package/dist/browser.js.map +1 -1
  9. package/dist/{chunk-DVW7CQ3H.js → chunk-2PWGJCWX.js} +3 -3
  10. package/dist/chunk-2PWGJCWX.js.map +1 -0
  11. package/dist/{chunk-VOTDAVCB.js → chunk-2YYRP7HH.js} +17 -33
  12. package/dist/chunk-2YYRP7HH.js.map +1 -0
  13. package/dist/{chunk-MLSP67JF.js → chunk-333FGMGQ.js} +59 -111
  14. package/dist/chunk-333FGMGQ.js.map +1 -0
  15. package/dist/chunk-OVRZIDCD.js +175 -0
  16. package/dist/chunk-OVRZIDCD.js.map +1 -0
  17. package/dist/{chunk-L7BTMAKX.js → chunk-P6XH7G4N.js} +3 -6
  18. package/dist/chunk-P6XH7G4N.js.map +1 -0
  19. package/dist/{chunk-NVYIUBU7.js → chunk-XC7I2L3G.js} +2 -2
  20. package/dist/chunk-XC7I2L3G.js.map +1 -0
  21. package/dist/client/index.d.ts +47 -1
  22. package/dist/client/index.js +3 -3
  23. package/dist/client/index.js.map +1 -1
  24. package/dist/index.d.ts +217 -347
  25. package/dist/index.js +138 -83
  26. package/dist/index.js.map +1 -1
  27. package/dist/kit-a2a-types-CwT1AFQu.d.ts +136 -0
  28. package/dist/{index-B7s-0mZl.d.ts → kit-client-auth-HnZIYUia.d.ts} +220 -101
  29. package/dist/kit-crypto-types-Dd24aJ4m.d.ts +194 -0
  30. package/dist/{kit-i18n-D9Qqk-1k.d.ts → kit-i18n-DEzmzE5T.d.ts} +142 -29
  31. package/dist/{kit-types-ByWdbCeO.d.ts → kit-types-CnzhN-AS.d.ts} +240 -162
  32. package/dist/modules/a2a/index.d.ts +13 -111
  33. package/dist/modules/a2a/index.js +2 -2
  34. package/dist/modules/crypto/index.d.ts +28 -195
  35. package/dist/modules/crypto/index.js +3 -3
  36. package/dist/modules/crypto/index.js.map +1 -1
  37. package/package.json +10 -10
  38. package/dist/chunk-DVW7CQ3H.js.map +0 -1
  39. package/dist/chunk-L7BTMAKX.js.map +0 -1
  40. package/dist/chunk-MLSP67JF.js.map +0 -1
  41. package/dist/chunk-NVYIUBU7.js.map +0 -1
  42. package/dist/chunk-QHYNL24G.js +0 -117
  43. package/dist/chunk-QHYNL24G.js.map +0 -1
  44. 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-CnzhN-AS.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,40 +12,97 @@ import { z } from 'zod';
12
12
  * @module kit-config
13
13
  */
14
14
 
15
- /** 服务器监听配置(对应 `config/_kit.yml` 的 `server`)。 */
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
- /** `config/_kit.yml` 中的服务器监听输入类型。 */
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
- /** `config/_kit.yml` 中的 transport 配置。 */
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 {
24
51
  readonly keyExchangePath: `/${string}`;
25
52
  readonly excludePaths: readonly `/${string}`[];
26
- readonly requireEncryption: boolean;
27
- readonly encryptResponse: boolean;
28
- readonly maxClients: number;
29
53
  }
30
- /** `config/_kit.yml` 的顶层配置结构。 */
54
+ /**
55
+ * `config/_kit.yml` 的顶层配置结构。
56
+ * @example
57
+ * ```ts
58
+ * import type { KitConfig } from '@h-ai/kit'
59
+ *
60
+ * const value: KitConfig = { server: { host: '127.0.0.1', port: 3000 }, transport: false }
61
+ * void value
62
+ * ```
63
+ */
31
64
  interface KitConfig {
32
65
  readonly server: KitServerConfig;
33
66
  readonly transport: false | KitTransportConfig;
34
67
  }
35
- /** `config/_kit.yml` 中的 transport 输入类型。 */
68
+ /**
69
+ * `config/_kit.yml` 中的 transport 输入类型。
70
+ * @example
71
+ * ```ts
72
+ * import type { KitTransportConfigInput } from '@h-ai/kit'
73
+ *
74
+ * const value: KitTransportConfigInput = { excludePaths: ['/api/health'] }
75
+ * void value
76
+ * ```
77
+ */
36
78
  type KitTransportConfigInput = Partial<{
37
79
  readonly keyExchangePath: `/${string}`;
38
80
  readonly excludePaths: readonly `/${string}`[];
39
- readonly requireEncryption: boolean;
40
- readonly encryptResponse: boolean;
41
- readonly maxClients: number;
42
81
  }>;
43
- /** `config/_kit.yml` 的顶层输入类型。 */
82
+ /**
83
+ * `config/_kit.yml` 的顶层输入类型。
84
+ * @example
85
+ * ```ts
86
+ * import type { KitConfigInput } from '@h-ai/kit'
87
+ *
88
+ * const value: KitConfigInput = { transport: false }
89
+ * void value
90
+ * ```
91
+ */
44
92
  interface KitConfigInput {
45
93
  readonly server?: KitServerConfigInput;
46
94
  readonly transport?: false | KitTransportConfigInput;
47
95
  }
48
- /** `config/_kit.yml` 对应的配置 Schema。 */
96
+ /**
97
+ * `config/_kit.yml` 对应的配置 Schema。
98
+ * @example
99
+ * ```ts
100
+ * import { KitConfigSchema } from '@h-ai/kit'
101
+ *
102
+ * const config = KitConfigSchema.parse({ server: { port: 3000 }, transport: false })
103
+ * void config
104
+ * ```
105
+ */
49
106
  declare const KitConfigSchema: z.ZodPipe<z.ZodObject<{
50
107
  server: z.ZodOptional<z.ZodObject<{
51
108
  host: z.ZodDefault<z.ZodString>;
@@ -54,9 +111,6 @@ declare const KitConfigSchema: z.ZodPipe<z.ZodObject<{
54
111
  transport: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodObject<{
55
112
  keyExchangePath: z.ZodDefault<z.ZodCustom<`/${string}`, `/${string}`>>;
56
113
  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
114
  }, z.core.$strip>]>>;
61
115
  }, z.core.$strip>, z.ZodTransform<{
62
116
  server: KitServerConfig;
@@ -69,9 +123,6 @@ declare const KitConfigSchema: z.ZodPipe<z.ZodObject<{
69
123
  transport?: false | {
70
124
  keyExchangePath: `/${string}`;
71
125
  excludePaths: `/${string}`[];
72
- requireEncryption: boolean;
73
- encryptResponse: boolean;
74
- maxClients: number;
75
126
  } | undefined;
76
127
  }>>;
77
128
  /**
@@ -79,12 +130,28 @@ declare const KitConfigSchema: z.ZodPipe<z.ZodObject<{
79
130
  *
80
131
  * @param input - `config/_kit.yml` 中的 transport 配置
81
132
  * @returns 完整 transport 配置;`false` 表示关闭
133
+ * @example
134
+ * ```ts
135
+ * import { resolveKitTransportConfig } from '@h-ai/kit'
136
+ *
137
+ * const transport = resolveKitTransportConfig({ excludePaths: ['/api/health'] })
138
+ * if (transport) void transport.keyExchangePath
139
+ * ```
82
140
  */
83
141
  declare function resolveKitTransportConfig(input?: false | KitTransportConfigInput): false | KitTransportConfig;
84
142
  /**
85
143
  * 解析 `config/_kit.yml` 顶层配置并补齐默认值。
86
144
  *
87
145
  * 适合同一份配置同时供 SvelteKit 服务端 hook 与浏览器端 client 读取。
146
+ * @param input - Kit 监听和公开 transport 输入;省略字段按 Schema 补默认值。
147
+ * @returns 已补齐默认值的配置;不合法输入抛出 ZodError。
148
+ * @example
149
+ * ```ts
150
+ * import { resolveKitConfig } from '@h-ai/kit'
151
+ *
152
+ * const config = resolveKitConfig({ transport: false })
153
+ * void config.server.port
154
+ * ```
88
155
  */
89
156
  declare function resolveKitConfig(input?: KitConfigInput): KitConfig;
90
157
 
@@ -95,6 +162,15 @@ declare function resolveKitConfig(input?: KitConfigInput): KitConfig;
95
162
  * @module kit-auth
96
163
  */
97
164
 
165
+ /**
166
+ * 服务端认证写入/删除 Cookie 依赖;由当前请求提供并执行安全属性。
167
+ * @example 模块内部上下文
168
+ * ```ts
169
+ * function signIn(cookies: CookieWriter, credentials: PasswordCredentials) {
170
+ * return login(cookies, credentials) // 业务调用者检查 success 后消费身份。
171
+ * }
172
+ * ```
173
+ */
98
174
  interface CookieWriter {
99
175
  set: (name: string, value: string, options: {
100
176
  path: string;
@@ -107,7 +183,14 @@ interface CookieWriter {
107
183
  path: string;
108
184
  }) => void;
109
185
  }
110
- /** 浏览器 Token 存储器 */
186
+ /**
187
+ * 浏览器 Token 存储器;使用页面内存或平台安全存储,禁止写入 Web Storage。
188
+ * @example 模块内部上下文
189
+ * ```ts
190
+ * const store: BrowserTokenStore = getDefaultBrowserTokenStore()
191
+ * store.clear() // 当前页面内存状态;不持有服务端会话真值。
192
+ * ```
193
+ */
111
194
  interface BrowserTokenStore {
112
195
  get: () => string | null;
113
196
  set: (token: string) => void;
@@ -119,12 +202,13 @@ interface BrowserTokenStore {
119
202
  * @param cookies - SvelteKit cookies 对象
120
203
  * @param credentials - 密码凭证(identifier + password)
121
204
  * @returns 认证结果(成功时 Cookie 已自动写入)
122
- *
123
- * @example
205
+ * @example 模块内部上下文
124
206
  * ```ts
125
- * const result = await kit.auth.login(cookies, { identifier, password })
126
- * if (!result.success) return kit.response.fromError(result.error, IamErrorHttpStatus)
127
- * const { user, roles, permissions } = result.data
207
+ * async function signIn(cookies: CookieWriter, credentials: PasswordCredentials) {
208
+ * const result = await login(cookies, credentials)
209
+ * if (!result.success) return result.error.message
210
+ * return result.data
211
+ * }
128
212
  * ```
129
213
  */
130
214
  declare function login(cookies: CookieWriter, credentials: PasswordCredentials): Promise<HaiResult<AuthResult>>;
@@ -134,10 +218,13 @@ declare function login(cookies: CookieWriter, credentials: PasswordCredentials):
134
218
  * @param cookies - SvelteKit cookies 对象
135
219
  * @param credentials - OTP 凭证(identifier + code)
136
220
  * @returns 认证结果(成功时 Cookie 已自动写入)
137
- *
138
- * @example
221
+ * @example 模块内部上下文
139
222
  * ```ts
140
- * const result = await kit.auth.loginWithOtp(cookies, { identifier, code })
223
+ * async function signIn(cookies: CookieWriter, credentials: OtpCredentials) {
224
+ * const result = await loginWithOtp(cookies, credentials)
225
+ * if (!result.success) return result.error.message
226
+ * return result.data
227
+ * }
141
228
  * ```
142
229
  */
143
230
  declare function loginWithOtp(cookies: CookieWriter, credentials: OtpCredentials): Promise<HaiResult<AuthResult>>;
@@ -147,10 +234,13 @@ declare function loginWithOtp(cookies: CookieWriter, credentials: OtpCredentials
147
234
  * @param cookies - SvelteKit cookies 对象
148
235
  * @param credentials - LDAP 凭证(username + password)
149
236
  * @returns 认证结果(成功时 Cookie 已自动写入)
150
- *
151
- * @example
237
+ * @example 模块内部上下文
152
238
  * ```ts
153
- * const result = await kit.auth.loginWithLdap(cookies, { username, password })
239
+ * async function signIn(cookies: CookieWriter, credentials: LdapCredentials) {
240
+ * const result = await loginWithLdap(cookies, credentials)
241
+ * if (!result.success) return result.error.message
242
+ * return result.data
243
+ * }
154
244
  * ```
155
245
  */
156
246
  declare function loginWithLdap(cookies: CookieWriter, credentials: LdapCredentials): Promise<HaiResult<AuthResult>>;
@@ -160,11 +250,13 @@ declare function loginWithLdap(cookies: CookieWriter, credentials: LdapCredentia
160
250
  * @param cookies - SvelteKit cookies 对象
161
251
  * @param credentials - API Key 凭证(key)
162
252
  * @returns 认证结果(成功时 Cookie 已自动写入)
163
- *
164
- * @example
253
+ * @example 模块内部上下文
165
254
  * ```ts
166
- * const result = await kit.auth.loginWithApiKey(cookies, { key: apiKey })
167
- * if (!result.success) return kit.response.fromError(result.error, IamErrorHttpStatus)
255
+ * async function signIn(cookies: CookieWriter, credentials: ApiKeyCredentials) {
256
+ * const result = await loginWithApiKey(cookies, credentials)
257
+ * if (!result.success) return result.error.message
258
+ * return result.data
259
+ * }
168
260
  * ```
169
261
  */
170
262
  declare function loginWithApiKey(cookies: CookieWriter, credentials: ApiKeyCredentials): Promise<HaiResult<AuthResult>>;
@@ -174,42 +266,55 @@ declare function loginWithApiKey(cookies: CookieWriter, credentials: ApiKeyCrede
174
266
  * @param cookies - SvelteKit cookies 对象
175
267
  * @param options - 注册选项(username、password、email 等)
176
268
  * @returns 认证结果(成功时 Cookie 已自动写入)
177
- *
178
- * @example
269
+ * @example 模块内部上下文
179
270
  * ```ts
180
- * const result = await kit.auth.registerAndLogin(cookies, { username, email, password })
271
+ * async function signIn(cookies: CookieWriter, credentials: RegisterOptions) {
272
+ * const result = await registerAndLogin(cookies, credentials)
273
+ * if (!result.success) return result.error.message
274
+ * return result.data
275
+ * }
181
276
  * ```
182
277
  */
183
278
  declare function registerAndLogin(cookies: CookieWriter, options: RegisterOptions): Promise<HaiResult<AuthResult>>;
184
279
  /**
185
- * 服务端登出:内部调用 iam.auth.logout 使会话失效,并清除 Token Cookie。
280
+ * 服务端登出:委托 iam.auth.logout 原子撤销凭据,并始终清除当前请求的 Token Cookie。
281
+ * Cookie 清理只代表本地退出;服务端撤销失败仍返回失败,不能由调用端改成成功响应。
186
282
  *
187
283
  * @param cookies - SvelteKit cookies 对象
188
284
  * @param accessToken - 访问令牌(为 null/undefined 时仅清除 Cookie)
189
- *
190
- * @example
285
+ * @returns 服务端撤销结果;无令牌时仅清理 Cookie 并成功,有令牌但未配置认证操作时失败。
286
+ * @remarks 底层控制流异常仍以 Promise 拒绝传播;Cookie 清理不吞掉业务失败。
287
+ * @example 模块内部上下文
191
288
  * ```ts
192
- * await kit.auth.logout(cookies, locals.accessToken)
193
- * return kit.response.ok(null)
289
+ * async function signOut(cookies: CookieWriter, accessToken: string) {
290
+ * const result = await logout(cookies, accessToken)
291
+ * if (!result.success) return result.error.code
292
+ * return result.data
293
+ * }
194
294
  * ```
195
295
  */
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;
296
+ declare function logout(cookies: CookieWriter, accessToken?: string | null): Promise<HaiResult<void>>;
207
297
  /**
208
298
  * 写入默认浏览器端 Access Token(仅页面内存,不持久化到 localStorage)。
299
+ * @param token - 当前页面内存中使用的 access token,不写入 Web Storage。
300
+ * @example
301
+ * ```ts
302
+ * import { kit } from '@h-ai/kit'
303
+ *
304
+ * kit.auth.setBrowserToken('example-only-token')
305
+ * // 页面注销时同步清除浏览器内存令牌。
306
+ * kit.auth.clearBrowserToken()
307
+ * ```
209
308
  */
210
309
  declare function setBrowserToken(token: string): void;
211
310
  /**
212
311
  * 清除默认浏览器端 Access Token(仅页面内存)。
312
+ * @example
313
+ * ```ts
314
+ * import { kit } from '@h-ai/kit'
315
+ *
316
+ * kit.auth.clearBrowserToken()
317
+ * ```
213
318
  */
214
319
  declare function clearBrowserToken(): void;
215
320
 
@@ -223,6 +328,13 @@ declare function clearBrowserToken(): void;
223
328
 
224
329
  /**
225
330
  * 传输加密客户端配置
331
+ * @example
332
+ * ```ts
333
+ * import type { ClientTransportConfig } from '@h-ai/kit'
334
+ * import { crypto } from '@h-ai/crypto'
335
+ *
336
+ * function configAfterInit(): ClientTransportConfig { return { crypto, excludePaths: ['/api/health'] } }
337
+ * ```
226
338
  */
227
339
  interface ClientTransportConfig {
228
340
  /** @h-ai/crypto 服务实例 */
@@ -234,6 +346,13 @@ interface ClientTransportConfig {
234
346
  }
235
347
  /**
236
348
  * Kit 客户端配置
349
+ * @example
350
+ * ```ts
351
+ * import type { KitClientConfig } from '@h-ai/kit'
352
+ *
353
+ * const value: KitClientConfig = { auth: true }
354
+ * void value
355
+ * ```
237
356
  */
238
357
  interface KitClientConfig {
239
358
  /**
@@ -256,6 +375,16 @@ interface KitClientConfig {
256
375
  }
257
376
  /**
258
377
  * Kit 客户端实例
378
+ * @example
379
+ * ```ts
380
+ * import type { KitClient } from '@h-ai/kit'
381
+ *
382
+ * async function read(client: KitClient) {
383
+ * const response = await client.apiFetch('/api/health')
384
+ * if (!response.ok) throw new Error('Request failed')
385
+ * return response.json()
386
+ * }
387
+ * ```
259
388
  */
260
389
  interface KitClient {
261
390
  /** 统一 API fetch(写请求自动附加 CSRF,启用 transport 时自动加解密) */
@@ -279,6 +408,14 @@ interface KitClient {
279
408
  * 静态资源以及 multipart/form-data 等上传请求保持原样。应用只需安装一次,
280
409
  * 后续 `fetch` / SvelteKit 内部 data fetch / `kit.client.create()` 明文发送路径
281
410
  * 都会共用这层传输加密能力。
411
+ * @param transportConfig - 已初始化 crypto、同源密钥交换路径及排除路径;省略时不安装包装。
412
+ * @example
413
+ * ```ts
414
+ * import { kit } from '@h-ai/kit'
415
+ *
416
+ * // 不传配置时不安装全局包装;启用时应传已初始化 crypto 与同源端点。
417
+ * kit.client.installBrowserTransportFetch()
418
+ * ```
282
419
  */
283
420
  declare function installBrowserTransportFetch(transportConfig?: ClientTransportConfig): void;
284
421
  /**
@@ -294,16 +431,14 @@ declare function installBrowserTransportFetch(transportConfig?: ClientTransportC
294
431
  * @param config 已解析的 kit 顶层配置
295
432
  * @param deps 外部依赖
296
433
  * @param deps.crypto `@h-ai/crypto` 模块实例
297
- *
298
434
  * @example
299
435
  * ```ts
300
- * import { browser } from '$app/environment'
301
- * import { crypto } from '@h-ai/crypto'
302
436
  * import { kit } from '@h-ai/kit'
303
- * import { adminConsoleKitConfig } from '$lib/config/kit-config.js'
304
437
  *
305
- * if (browser) {
306
- * kit.client.installBrowserTransport(adminConsoleKitConfig, { crypto })
438
+ * import { crypto } from '@h-ai/crypto'
439
+ *
440
+ * function installAfterCryptoInit() {
441
+ * kit.client.installBrowserTransport({ server: { host: '127.0.0.1', port: 3000 }, transport: { keyExchangePath: '/api/_hai/key-exchange', excludePaths: ['/api/health'] } }, { crypto })
307
442
  * }
308
443
  * ```
309
444
  */
@@ -321,17 +456,38 @@ declare function installBrowserTransport(config: KitConfig, deps: {
321
456
  *
322
457
  * @param config - 客户端配置
323
458
  * @returns KitClient 实例
324
- *
325
459
  * @example
326
460
  * ```ts
327
- * const client = createKitClient({ transport: { crypto } })
328
- * const res = await client.apiFetch('/api/users', { method: 'POST', body: '{}' })
461
+ * import { kit } from '@h-ai/kit'
462
+ *
463
+ * const client = kit.client.create({ auth: true })
464
+ * try {
465
+ * await client.init() // Client API 使用 Promise 拒绝;不是 HaiResult。
466
+ * const response = await client.apiFetch('/api/health')
467
+ * if (!response.ok) throw new Error('Request failed')
468
+ * } finally {
469
+ * client.destroy()
470
+ * }
329
471
  * ```
330
472
  */
331
473
  declare function createKitClient(config?: KitClientConfig): KitClient;
332
474
 
333
475
  /** 与标准 Kit 认证端点配套的表单动作;复用已有 CSRF/transport fetch。 */
334
476
 
477
+ /**
478
+ * 创建标准 Cookie 认证表单动作;复用可选 apiFetch 以保留 CSRF 和传输配置。
479
+ * @param options - 可复用的 apiFetch 和标准认证端点 basePath。
480
+ * @param options.apiFetch - 已装配 CSRF/transport 的 fetch;省略时使用默认 Kit 客户端。
481
+ * @param options.basePath - 同源认证端点前缀,默认 /api/auth。
482
+ * @returns 标准表单动作;业务返回 HaiResult,网络/格式错误会拒绝 Promise。
483
+ * @example
484
+ * ```ts
485
+ * import { kit } from '@h-ai/kit'
486
+ *
487
+ * const actions = kit.client.createAuthActions({ basePath: '/api/auth' })
488
+ * void actions
489
+ * ```
490
+ */
335
491
  declare function createKitAuthActions(options?: {
336
492
  apiFetch?: KitClient['apiFetch'];
337
493
  basePath?: string;
@@ -347,41 +503,4 @@ declare function createKitAuthActions(options?: {
347
503
  confirmPasswordReset: (token: string, newPassword: string) => Promise<HaiResult<unknown>>;
348
504
  };
349
505
 
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 };
506
+ 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 };