@h-ai/kit 0.1.0-alpha9 → 1.0.0-beta.1
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 +430 -0
- package/README.ja.md +430 -0
- package/README.md +351 -40
- package/dist/adapter/index.d.ts +17 -2
- package/dist/adapter/index.js.map +1 -1
- package/dist/browser.d.ts +19 -8
- package/dist/browser.js +10 -7
- package/dist/browser.js.map +1 -1
- package/dist/chunk-3OHUSKHI.js +175 -0
- package/dist/chunk-3OHUSKHI.js.map +1 -0
- package/dist/chunk-5VWPH7DO.js +126 -0
- package/dist/chunk-5VWPH7DO.js.map +1 -0
- package/dist/chunk-7EH47MTW.js +141 -0
- package/dist/chunk-7EH47MTW.js.map +1 -0
- package/dist/chunk-JRQPMPQC.js +63 -0
- package/dist/chunk-JRQPMPQC.js.map +1 -0
- package/dist/chunk-USFJN7IO.js +187 -0
- package/dist/chunk-USFJN7IO.js.map +1 -0
- package/dist/chunk-YKJRQCWR.js +363 -0
- package/dist/chunk-YKJRQCWR.js.map +1 -0
- package/dist/client/index.d.ts +51 -2
- package/dist/client/index.js +21 -2
- package/dist/client/index.js.map +1 -1
- package/dist/index.d.ts +303 -409
- package/dist/index.js +598 -310
- package/dist/index.js.map +1 -1
- package/dist/kit-a2a-types-4dapztu2.d.ts +136 -0
- package/dist/kit-client-auth-BnhrkzZF.d.ts +422 -0
- package/dist/kit-crypto-types-CXr888wf.d.ts +200 -0
- package/dist/kit-i18n-EBzZKYN-.d.ts +502 -0
- package/dist/kit-types-BkNdyKj8.d.ts +822 -0
- package/dist/modules/a2a/index.d.ts +15 -113
- package/dist/modules/a2a/index.js +2 -55
- package/dist/modules/a2a/index.js.map +1 -1
- package/dist/modules/crypto/index.d.ts +31 -79
- package/dist/modules/crypto/index.js +3 -3
- package/dist/modules/crypto/index.js.map +1 -1
- package/package.json +36 -22
- package/dist/chunk-475WECWK.js +0 -110
- package/dist/chunk-475WECWK.js.map +0 -1
- package/dist/chunk-6TMSXG7J.js +0 -169
- package/dist/chunk-6TMSXG7J.js.map +0 -1
- package/dist/chunk-AD6S473Y.js +0 -137
- package/dist/chunk-AD6S473Y.js.map +0 -1
- package/dist/chunk-FRYJMRTP.js +0 -71
- package/dist/chunk-FRYJMRTP.js.map +0 -1
- package/dist/chunk-PHS7ZD5L.js +0 -274
- package/dist/chunk-PHS7ZD5L.js.map +0 -1
- package/dist/index-BotXLTck.d.ts +0 -203
- package/dist/kit-crypto-types-C-gEvx04.d.ts +0 -253
- package/dist/kit-i18n-CgpNTh9G.d.ts +0 -206
- package/dist/kit-types-BHlDNNpd.d.ts +0 -618
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import { f as HandleA2AOperations, h as HandleConfig } from './kit-types-BkNdyKj8.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 };
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { IamCreateCaptchaInputSchema, IamPublicConfig, IamLoginInput, IamRegisterInput } from '@h-ai/api-contract';
|
|
2
|
+
import { HaiResult } from '@h-ai/core';
|
|
3
|
+
import { CryptoFunctions } from '@h-ai/crypto';
|
|
4
|
+
import { PasswordCredentials, AuthRequestContext, AuthResult, OtpCredentials, LdapCredentials, ApiKeyCredentials, RegisterOptions } from '@h-ai/iam';
|
|
5
|
+
import { k as KitPublicConfig } from './kit-types-BkNdyKj8.js';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @h-ai/kit — Bearer 认证工具
|
|
10
|
+
*
|
|
11
|
+
* 提供服务端认证 Cookie 管理(login / logout)与浏览器端 Token 存储工具。
|
|
12
|
+
* @module kit-auth
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 服务端认证写入/删除 Cookie 依赖;由当前请求提供并执行安全属性。
|
|
17
|
+
* @example 模块内部上下文
|
|
18
|
+
* ```ts
|
|
19
|
+
* function signIn(cookies: CookieWriter, credentials: PasswordCredentials) {
|
|
20
|
+
* return login(cookies, credentials) // 业务调用者检查 success 后消费身份。
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
interface CookieWriter {
|
|
25
|
+
set: (name: string, value: string, options: {
|
|
26
|
+
path: string;
|
|
27
|
+
httpOnly: boolean;
|
|
28
|
+
sameSite: 'lax' | 'strict' | 'none';
|
|
29
|
+
secure: boolean;
|
|
30
|
+
maxAge?: number;
|
|
31
|
+
}) => void;
|
|
32
|
+
delete: (name: string, options: {
|
|
33
|
+
path: string;
|
|
34
|
+
}) => void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 浏览器 Token 存储器;使用页面内存或平台安全存储,禁止写入 Web Storage。
|
|
38
|
+
* @example 模块内部上下文
|
|
39
|
+
* ```ts
|
|
40
|
+
* const store: BrowserTokenStore = getDefaultBrowserTokenStore()
|
|
41
|
+
* store.clear() // 当前页面内存状态;不持有服务端会话真值。
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
interface BrowserTokenStore {
|
|
45
|
+
get: () => string | null;
|
|
46
|
+
set: (token: string) => void;
|
|
47
|
+
clear: () => void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 服务端登录(密码):内部调用 iam.auth.login,成功时自动写入 Token Cookie。
|
|
51
|
+
*
|
|
52
|
+
* @param cookies - SvelteKit cookies 对象
|
|
53
|
+
* @param credentials - 密码凭证(identifier + password)
|
|
54
|
+
* @param context - 宿主提供的可信登录元数据,不从凭据输入复制 IP。
|
|
55
|
+
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
56
|
+
* @example 模块内部上下文
|
|
57
|
+
* ```ts
|
|
58
|
+
* async function signIn(cookies: CookieWriter, credentials: PasswordCredentials) {
|
|
59
|
+
* const result = await login(cookies, credentials)
|
|
60
|
+
* if (!result.success) return result.error.message
|
|
61
|
+
* return result.data
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
declare function login(cookies: CookieWriter, credentials: PasswordCredentials, context?: AuthRequestContext): Promise<HaiResult<AuthResult>>;
|
|
66
|
+
/**
|
|
67
|
+
* 服务端登录(OTP 验证码):内部调用 iam.auth.loginWithOtp,成功时自动写入 Token Cookie。
|
|
68
|
+
*
|
|
69
|
+
* @param cookies - SvelteKit cookies 对象
|
|
70
|
+
* @param credentials - OTP 凭证(identifier + code)
|
|
71
|
+
* @param context - 宿主提供的可信登录元数据。
|
|
72
|
+
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
73
|
+
* @example 模块内部上下文
|
|
74
|
+
* ```ts
|
|
75
|
+
* async function signIn(cookies: CookieWriter, credentials: OtpCredentials) {
|
|
76
|
+
* const result = await loginWithOtp(cookies, credentials)
|
|
77
|
+
* if (!result.success) return result.error.message
|
|
78
|
+
* return result.data
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function loginWithOtp(cookies: CookieWriter, credentials: OtpCredentials, context?: AuthRequestContext): Promise<HaiResult<AuthResult>>;
|
|
83
|
+
/**
|
|
84
|
+
* 服务端登录(LDAP):内部调用 iam.auth.loginWithLdap,成功时自动写入 Token Cookie。
|
|
85
|
+
*
|
|
86
|
+
* @param cookies - SvelteKit cookies 对象
|
|
87
|
+
* @param credentials - LDAP 凭证(username + password)
|
|
88
|
+
* @param context - 宿主提供的可信登录元数据。
|
|
89
|
+
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
90
|
+
* @example 模块内部上下文
|
|
91
|
+
* ```ts
|
|
92
|
+
* async function signIn(cookies: CookieWriter, credentials: LdapCredentials) {
|
|
93
|
+
* const result = await loginWithLdap(cookies, credentials)
|
|
94
|
+
* if (!result.success) return result.error.message
|
|
95
|
+
* return result.data
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function loginWithLdap(cookies: CookieWriter, credentials: LdapCredentials, context?: AuthRequestContext): Promise<HaiResult<AuthResult>>;
|
|
100
|
+
/**
|
|
101
|
+
* 服务端登录(API Key):内部调用 iam.auth.loginWithApiKey,成功时自动写入 Token Cookie。
|
|
102
|
+
*
|
|
103
|
+
* @param cookies - SvelteKit cookies 对象
|
|
104
|
+
* @param credentials - API Key 凭证(key)
|
|
105
|
+
* @param context - 宿主提供的可信登录元数据。
|
|
106
|
+
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
107
|
+
* @example 模块内部上下文
|
|
108
|
+
* ```ts
|
|
109
|
+
* async function signIn(cookies: CookieWriter, credentials: ApiKeyCredentials) {
|
|
110
|
+
* const result = await loginWithApiKey(cookies, credentials)
|
|
111
|
+
* if (!result.success) return result.error.message
|
|
112
|
+
* return result.data
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function loginWithApiKey(cookies: CookieWriter, credentials: ApiKeyCredentials, context?: AuthRequestContext): Promise<HaiResult<AuthResult>>;
|
|
117
|
+
/**
|
|
118
|
+
* 服务端注册并登录:内部调用 iam.auth.registerAndLogin,成功时自动写入 Token Cookie。
|
|
119
|
+
*
|
|
120
|
+
* @param cookies - SvelteKit cookies 对象
|
|
121
|
+
* @param options - 注册选项(username、password、email 等)
|
|
122
|
+
* @returns 认证结果(成功时 Cookie 已自动写入)
|
|
123
|
+
* @example 模块内部上下文
|
|
124
|
+
* ```ts
|
|
125
|
+
* async function signIn(cookies: CookieWriter, credentials: RegisterOptions) {
|
|
126
|
+
* const result = await registerAndLogin(cookies, credentials)
|
|
127
|
+
* if (!result.success) return result.error.message
|
|
128
|
+
* return result.data
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare function registerAndLogin(cookies: CookieWriter, options: RegisterOptions, context?: AuthRequestContext): Promise<HaiResult<AuthResult>>;
|
|
133
|
+
/**
|
|
134
|
+
* 服务端登出:委托 iam.auth.logout 原子撤销凭据,并始终清除当前请求的 Token Cookie。
|
|
135
|
+
* Cookie 清理只代表本地退出;服务端撤销失败仍返回失败,不能由调用端改成成功响应。
|
|
136
|
+
*
|
|
137
|
+
* @param cookies - SvelteKit cookies 对象
|
|
138
|
+
* @param accessToken - 访问令牌(为 null/undefined 时仅清除 Cookie)
|
|
139
|
+
* @returns 服务端撤销结果;无令牌时仅清理 Cookie 并成功,有令牌但未配置认证操作时失败。
|
|
140
|
+
* @remarks 底层控制流异常仍以 Promise 拒绝传播;Cookie 清理不吞掉业务失败。
|
|
141
|
+
* @example 模块内部上下文
|
|
142
|
+
* ```ts
|
|
143
|
+
* async function signOut(cookies: CookieWriter, accessToken: string) {
|
|
144
|
+
* const result = await logout(cookies, accessToken)
|
|
145
|
+
* if (!result.success) return result.error.code
|
|
146
|
+
* return result.data
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
declare function logout(cookies: CookieWriter, accessToken?: string | null): Promise<HaiResult<void>>;
|
|
151
|
+
/**
|
|
152
|
+
* 写入默认浏览器端 Access Token(仅页面内存,不持久化到 localStorage)。
|
|
153
|
+
* @param token - 当前页面内存中使用的 access token,不写入 Web Storage。
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* import { kit } from '@h-ai/kit'
|
|
157
|
+
*
|
|
158
|
+
* kit.auth.setBrowserToken('example-only-token')
|
|
159
|
+
* // 页面注销时同步清除浏览器内存令牌。
|
|
160
|
+
* kit.auth.clearBrowserToken()
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
declare function setBrowserToken(token: string): void;
|
|
164
|
+
/**
|
|
165
|
+
* 清除默认浏览器端 Access Token(仅页面内存)。
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* import { kit } from '@h-ai/kit'
|
|
169
|
+
*
|
|
170
|
+
* kit.auth.clearBrowserToken()
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
declare function clearBrowserToken(): void;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @h-ai/kit — 统一客户端
|
|
177
|
+
*
|
|
178
|
+
* 将 CSRF Token 附加与可选传输加密封装为统一 `apiFetch`。
|
|
179
|
+
* 应用层只需调用一次 `createKitClient()`,后续 API 调用即可复用同一套安全能力。
|
|
180
|
+
* @module kit-client
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 传输加密客户端配置
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* import type { ClientTransportConfig } from '@h-ai/kit'
|
|
188
|
+
* import { crypto } from '@h-ai/crypto'
|
|
189
|
+
*
|
|
190
|
+
* function configAfterInit(): ClientTransportConfig { return { crypto, excludePaths: ['/api/health'] } }
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
interface ClientTransportConfig {
|
|
194
|
+
/** 宿主构建挂载目录,默认空字符串;SvelteKit 使用 $app/paths.base。 */
|
|
195
|
+
basePath?: string;
|
|
196
|
+
/** 可选本地明文上限;缺省跟随服务端握手。 */
|
|
197
|
+
maxPlaintextBytes?: number;
|
|
198
|
+
/** @h-ai/crypto 服务实例 */
|
|
199
|
+
crypto: CryptoFunctions;
|
|
200
|
+
/** 密钥交换端点 URL(默认 `'/api/_hai/key-exchange'`) */
|
|
201
|
+
keyExchangeUrl?: string;
|
|
202
|
+
/** 排除路径(命中后强制走明文),需与服务端 transport.excludePaths 保持一致 */
|
|
203
|
+
excludePaths?: readonly string[];
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Kit 客户端配置
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* import type { KitClientConfig } from '@h-ai/kit'
|
|
210
|
+
*
|
|
211
|
+
* const value: KitClientConfig = { auth: true }
|
|
212
|
+
* void value
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
interface KitClientConfig {
|
|
216
|
+
/** 标准鉴别明确失效时原地登录原用户;成功才对被拒绝的请求重试一次。 */
|
|
217
|
+
recoverSession?: () => Promise<boolean>;
|
|
218
|
+
/** 会话终端分组,默认web;服务端按终端与全局配额共同限制。 */
|
|
219
|
+
terminalType?: 'web' | 'app' | 'desktop' | 'api';
|
|
220
|
+
/**
|
|
221
|
+
* 传输加密配置。
|
|
222
|
+
* - 不提供 / undefined:不启用传输加密,仅 CSRF
|
|
223
|
+
* - 提供对象:启用传输加密
|
|
224
|
+
*/
|
|
225
|
+
transport?: ClientTransportConfig;
|
|
226
|
+
/** CSRF Cookie 名称(默认 `'hai_csrf'`) */
|
|
227
|
+
csrfCookieName?: string;
|
|
228
|
+
/** CSRF Header 名称(默认 `'X-CSRF-Token'`) */
|
|
229
|
+
csrfHeaderName?: string;
|
|
230
|
+
/**
|
|
231
|
+
* 认证配置:自动从浏览器存储读取 Access Token 并注入请求头。
|
|
232
|
+
* - `true`:使用默认页面内存存储(由 `kit.auth.setBrowserToken()` 写入,不读写 localStorage)
|
|
233
|
+
* - `BrowserTokenStore`:使用自定义存储
|
|
234
|
+
* - 不提供 / `false`:不自动注入
|
|
235
|
+
*/
|
|
236
|
+
auth?: boolean | BrowserTokenStore;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Kit 客户端实例
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* import type { KitClient } from '@h-ai/kit'
|
|
243
|
+
*
|
|
244
|
+
* async function read(client: KitClient) {
|
|
245
|
+
* const response = await client.apiFetch('/api/health')
|
|
246
|
+
* if (!response.ok) throw new Error('Request failed')
|
|
247
|
+
* return response.json()
|
|
248
|
+
* }
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
interface KitClient {
|
|
252
|
+
/** 统一 API fetch(写请求自动附加 CSRF,启用 transport 时自动加解密) */
|
|
253
|
+
apiFetch: (input: RequestInfo | URL, init?: RequestInit, options?: {
|
|
254
|
+
recoverSession?: boolean;
|
|
255
|
+
reauthorize?: () => Promise<string | null | undefined>;
|
|
256
|
+
}) => Promise<Response>;
|
|
257
|
+
/** 传输加密是否就绪(未启用传输加密时始终为 true) */
|
|
258
|
+
readonly ready: boolean;
|
|
259
|
+
/**
|
|
260
|
+
* 手动触发密钥交换(transport 启用时)。
|
|
261
|
+
*
|
|
262
|
+
* 注意:本方法是 Client 形态的合规例外,密钥协商失败时 `throw`(与 `@h-ai/api-client` 一致),
|
|
263
|
+
* 调用方需自行 `try/catch`,不返回 `HaiResult`。
|
|
264
|
+
*/
|
|
265
|
+
init: () => Promise<void>;
|
|
266
|
+
/** 销毁密钥状态 */
|
|
267
|
+
destroy: () => void;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* 在浏览器启动时安装同源 transport fetch 包装。
|
|
271
|
+
*
|
|
272
|
+
* 该包装作用于同源 `/api/*` 与 SvelteKit `__data.json` 请求,页面文档、
|
|
273
|
+
* 静态资源以及 multipart/form-data 等上传请求保持原样。应用只需安装一次,
|
|
274
|
+
* 后续 `fetch` / SvelteKit 内部 data fetch / `kit.client.create()` 明文发送路径
|
|
275
|
+
* 都会共用这层传输加密能力。
|
|
276
|
+
* @param transportConfig - 已初始化 crypto、同源密钥交换路径及排除路径;省略时不安装包装。
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* import { kit } from '@h-ai/kit'
|
|
280
|
+
*
|
|
281
|
+
* // 不传配置时不安装全局包装;启用时应传已初始化 crypto 与同源端点。
|
|
282
|
+
* kit.client.installBrowserTransportFetch()
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
declare function installBrowserTransportFetch(transportConfig?: ClientTransportConfig): void;
|
|
286
|
+
/**
|
|
287
|
+
* 用 `config/_kit.yml` 解析后的 {@link KitPublicConfig} 公共投影 一次性安装浏览器端传输加密。
|
|
288
|
+
*
|
|
289
|
+
* 行为:
|
|
290
|
+
* - `config.transport === false` 时直接返回(不启用传输加密);
|
|
291
|
+
* - 否则启动 {@link installBrowserTransportFetch},并预热 {@link CryptoFunctions} 模块。
|
|
292
|
+
*
|
|
293
|
+
* 应用层(SvelteKit `+layout.svelte` 或浏览器入口)只需调用此函数一次,
|
|
294
|
+
* 之后宿主 basePath 下的同源 `/api/*` 与 `__data.json` 请求都会自动走传输加密。
|
|
295
|
+
*
|
|
296
|
+
* @param config 已解析的 kit 公共传输配置
|
|
297
|
+
* @param deps 外部依赖
|
|
298
|
+
* @param deps.crypto `@h-ai/crypto` 模块实例
|
|
299
|
+
* @param deps.basePath 宿主构建挂载目录,默认空字符串;SvelteKit 使用 $app/paths.base。
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* import { kit } from '@h-ai/kit'
|
|
303
|
+
*
|
|
304
|
+
* import { crypto } from '@h-ai/crypto'
|
|
305
|
+
*
|
|
306
|
+
* function installAfterCryptoInit() {
|
|
307
|
+
* kit.client.installBrowserTransport({ transport: { keyExchangePath: '/api/_hai/key-exchange', excludePaths: ['/api/health'] } }, { crypto })
|
|
308
|
+
* }
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
declare function installBrowserTransport(config: KitPublicConfig, deps: {
|
|
312
|
+
crypto: CryptoFunctions;
|
|
313
|
+
basePath?: string;
|
|
314
|
+
}): void;
|
|
315
|
+
/**
|
|
316
|
+
* 创建 Kit 客户端
|
|
317
|
+
*
|
|
318
|
+
* 返回统一的 `apiFetch`,内部自动完成:
|
|
319
|
+
* 1. CSRF Token 附加(写方法自动读取 Cookie + 设置 Header)
|
|
320
|
+
* 2. 密钥交换(首次写请求时 lazy init,transport 启用时)
|
|
321
|
+
* 3. 请求体加密(transport 启用时)
|
|
322
|
+
* 4. 响应体解密(transport 启用时)
|
|
323
|
+
*
|
|
324
|
+
* @param config - 客户端配置
|
|
325
|
+
* @returns KitClient 实例
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* import { kit } from '@h-ai/kit'
|
|
329
|
+
*
|
|
330
|
+
* const client = kit.client.create({ auth: true })
|
|
331
|
+
* try {
|
|
332
|
+
* await client.init() // Client API 使用 Promise 拒绝;不是 HaiResult。
|
|
333
|
+
* const response = await client.apiFetch('/api/health')
|
|
334
|
+
* if (!response.ok) throw new Error('Request failed')
|
|
335
|
+
* } finally {
|
|
336
|
+
* client.destroy()
|
|
337
|
+
* }
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
declare function createKitClient(config?: KitClientConfig): KitClient;
|
|
341
|
+
|
|
342
|
+
/** 与标准 Kit 认证端点配套的表单动作;复用已有 CSRF/transport fetch。 */
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* 创建标准 Cookie 认证表单动作;复用可选 apiFetch 以保留 CSRF 和传输配置。
|
|
346
|
+
* @param options - 可复用的 apiFetch 和标准认证端点 basePath。
|
|
347
|
+
* @param options.apiFetch - 已装配 CSRF/transport 的 fetch;省略时使用默认 Kit 客户端。
|
|
348
|
+
* @param options.basePath - 同源认证端点前缀,默认 /api/auth。
|
|
349
|
+
* @returns 标准表单动作;业务返回 HaiResult,网络/格式错误会拒绝 Promise。
|
|
350
|
+
* @example
|
|
351
|
+
* ```ts
|
|
352
|
+
* import { kit } from '@h-ai/kit'
|
|
353
|
+
*
|
|
354
|
+
* const actions = kit.client.createAuthActions({ basePath: '/api/auth' })
|
|
355
|
+
* void actions
|
|
356
|
+
* ```
|
|
357
|
+
*/
|
|
358
|
+
declare function createKitAuthActions(options?: {
|
|
359
|
+
apiFetch?: KitClient['apiFetch'];
|
|
360
|
+
basePath?: string;
|
|
361
|
+
}): {
|
|
362
|
+
bindPageExit(options?: {
|
|
363
|
+
enabled?: boolean;
|
|
364
|
+
}): () => void;
|
|
365
|
+
createCaptcha(input: z.input<typeof IamCreateCaptchaInputSchema>): Promise<HaiResult<{
|
|
366
|
+
id: string;
|
|
367
|
+
challenge: {
|
|
368
|
+
parameters: {
|
|
369
|
+
algorithm: "PBKDF2/SHA-256";
|
|
370
|
+
nonce: string;
|
|
371
|
+
salt: string;
|
|
372
|
+
cost: number;
|
|
373
|
+
keyLength: number;
|
|
374
|
+
keyPrefix: string;
|
|
375
|
+
keySignature?: string | undefined;
|
|
376
|
+
data?: Record<string, string | number | boolean | null> | undefined;
|
|
377
|
+
};
|
|
378
|
+
signature?: string | undefined;
|
|
379
|
+
};
|
|
380
|
+
}>>;
|
|
381
|
+
publicConfig(): Promise<HaiResult<IamPublicConfig | null>>;
|
|
382
|
+
login(input: IamLoginInput & {
|
|
383
|
+
method?: "password" | "ldap" | "email" | "sms";
|
|
384
|
+
code?: string;
|
|
385
|
+
}): Promise<HaiResult<unknown>>;
|
|
386
|
+
register: (input: IamRegisterInput) => Promise<HaiResult<unknown>>;
|
|
387
|
+
sendOtp: (identifier: string, options?: {
|
|
388
|
+
captcha?: string;
|
|
389
|
+
}) => Promise<HaiResult<unknown>>;
|
|
390
|
+
requestPasswordReset: (identifier: string, options?: {
|
|
391
|
+
captcha?: string;
|
|
392
|
+
}) => Promise<HaiResult<unknown>>;
|
|
393
|
+
confirmPasswordReset: (token: string, newPassword: string) => Promise<HaiResult<unknown>>;
|
|
394
|
+
renewPassword: (input: {
|
|
395
|
+
identifier: string;
|
|
396
|
+
password: string;
|
|
397
|
+
newPassword: string;
|
|
398
|
+
captcha?: string;
|
|
399
|
+
}) => Promise<HaiResult<unknown>>;
|
|
400
|
+
reauthenticate(password: string, operation: string): Promise<{
|
|
401
|
+
success: false;
|
|
402
|
+
error: {
|
|
403
|
+
code: string | number;
|
|
404
|
+
message: string;
|
|
405
|
+
traceId?: string | undefined;
|
|
406
|
+
httpStatus?: number | undefined;
|
|
407
|
+
system?: string | undefined;
|
|
408
|
+
module?: string | undefined;
|
|
409
|
+
cause?: unknown;
|
|
410
|
+
suggestion?: string | undefined;
|
|
411
|
+
ext?: Record<string, unknown> | undefined;
|
|
412
|
+
};
|
|
413
|
+
} | {
|
|
414
|
+
success: true;
|
|
415
|
+
data: {
|
|
416
|
+
ticket: string;
|
|
417
|
+
expiresAt: number;
|
|
418
|
+
};
|
|
419
|
+
}>;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
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, loginWithOtp as f, loginWithLdap as g, loginWithApiKey as h, installBrowserTransport as i, logout as j, login as l, registerAndLogin as r, setBrowserToken as s };
|