@h-ai/ai 0.1.0-alpha.57 → 0.1.0-alpha.59

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@h-ai/ai",
3
3
  "type": "module",
4
- "version": "0.1.0-alpha.57",
4
+ "version": "0.1.0-alpha.59",
5
5
  "description": "Hai Framework AI module (LLM, MCP, tools, and clients).",
6
6
  "license": "Apache-2.0",
7
7
  "exports": {
@@ -26,38 +26,38 @@
26
26
  "access": "public"
27
27
  },
28
28
  "engines": {
29
- "node": ">=22.12.0"
29
+ "node": "^22.22.2 || ^24.15.0 || >=26.0.0"
30
30
  },
31
31
  "dependencies": {
32
32
  "@a2a-js/sdk": "^0.3.12",
33
- "@modelcontextprotocol/sdk": "^1.26.0",
34
- "mem0ai": "^3.0.13",
35
- "nanoid": "^5.0.9",
36
- "openai": "^6.17.0",
37
- "undici": "^7.24.7",
38
- "ws": "^8.18.3",
39
- "zod": "^4.4.3",
40
- "@h-ai/core": "0.1.0-alpha.57",
41
- "@h-ai/api-contract": "0.1.0-alpha.57",
42
- "@h-ai/datapipe": "0.1.0-alpha.57",
43
- "@h-ai/vecdb": "0.1.0-alpha.57",
44
- "@h-ai/reldb": "0.1.0-alpha.57"
33
+ "@h-ai/api-contract": "0.1.0-alpha.59",
34
+ "@h-ai/core": "0.1.0-alpha.59",
35
+ "@h-ai/datapipe": "0.1.0-alpha.59",
36
+ "@h-ai/reldb": "0.1.0-alpha.59",
37
+ "@h-ai/vecdb": "0.1.0-alpha.59",
38
+ "@modelcontextprotocol/sdk": "^1.32.1",
39
+ "mem0ai": "^3.3.1",
40
+ "nanoid": "^6.0.2",
41
+ "openai": "^7.32.0",
42
+ "undici": "^8.11.2",
43
+ "ws": "^8.22.0",
44
+ "zod": "^4.6.5"
45
45
  },
46
46
  "optionalDependencies": {
47
- "better-sqlite3": "^12.6.2"
47
+ "better-sqlite3": "^12.11.1"
48
48
  },
49
49
  "devDependencies": {
50
- "@types/node": "^25.9.1",
51
- "@types/ws": "^8.18.1",
50
+ "@types/node": "^26.6.5",
51
+ "@types/ws": "^8.18.2",
52
52
  "rimraf": "^6.1.3",
53
53
  "tsup": "^8.5.1",
54
54
  "typescript": "^6.0.3",
55
- "vitest": "^4.1.7"
55
+ "vitest": "^4.1.11"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "tsup",
59
59
  "dev": "tsup --watch",
60
- "typecheck": "tsc --noEmit",
60
+ "typecheck": "node ../../scripts/typecheck.mjs --noEmit",
61
61
  "lint": "eslint .",
62
62
  "lint:fix": "eslint . --fix",
63
63
  "test": "vitest run",
@@ -1,8 +1,8 @@
1
- import { AgentCard } from '@a2a-js/sdk';
2
- import { AgentExecutor } from '@a2a-js/sdk/server';
3
1
  import { HaiResult, HaiError } from '@h-ai/core';
4
2
  import { CleanOptionsInput, ChunkOptionsInput } from '@h-ai/datapipe';
5
3
  import { ZodType, z } from 'zod';
4
+ import { AgentCard } from '@a2a-js/sdk';
5
+ import { AgentExecutor } from '@a2a-js/sdk/server';
6
6
  import OpenAI from 'openai';
7
7
 
8
8
  /**
@@ -466,195 +466,6 @@ interface AIStoreProvider {
466
466
  close?: () => Promise<void>;
467
467
  }
468
468
 
469
- /**
470
- * @h-ai/ai — A2A(Agent-to-Agent)子功能类型
471
- *
472
- * 定义 A2A 协议集成所需的类型、SDK 类型再导出、操作接口。
473
- * @module ai-a2a-types
474
- */
475
-
476
- /** A2A 调用方身份信息 */
477
- interface A2ACallerIdentity {
478
- /** 调用方 Agent ID(如 AgentCard.name 或自定义标识) */
479
- agentId: string;
480
- /** 调用方显示名 */
481
- name?: string;
482
- /** 调用方 URL(AgentCard 地址) */
483
- url?: string;
484
- }
485
- /** A2A 消息记录(存储每条 A2A 交互) */
486
- interface A2AMessageRecord {
487
- /** 记录 ID(自动生成) */
488
- id: string;
489
- /** 关联的 A2A Task ID */
490
- taskId: string;
491
- /** 所属会话上下文 ID(历史记录可能缺失) */
492
- contextId?: string;
493
- /** 消息角色:user(入站请求)/ agent(出站响应) */
494
- role: 'user' | 'agent';
495
- /** 消息内容(JSON 序列化的 Part[] 数组) */
496
- parts: unknown[];
497
- /** 调用方身份信息(仅 role=user 时) */
498
- caller?: A2ACallerIdentity;
499
- /** 创建时间戳(毫秒) */
500
- createdAt: number;
501
- }
502
- /** A2A 上下文信息(对话/会话级别) */
503
- interface A2AContextInfo {
504
- /** 上下文 ID(对应 SDK 的 contextId) */
505
- id: string;
506
- /** 关联的 Agent 标识 */
507
- agentId?: string;
508
- /** 上下文标题(可选) */
509
- title?: string;
510
- /** 创建时间戳 */
511
- createdAt: number;
512
- /** 最后更新时间戳 */
513
- updatedAt: number;
514
- }
515
- /**
516
- * A2A 认证器接口
517
- *
518
- * 纯接口设计,由应用层实现,Kit 模块做胶水集成。
519
- * 用于验证入站 A2A 请求的身份。
520
- */
521
- interface A2AAuthenticator {
522
- /**
523
- * 验证入站请求
524
- *
525
- * @param headers - HTTP 请求头
526
- * @returns 成功返回调用方身份信息,失败返回错误
527
- */
528
- authenticate: (headers: Record<string, string | undefined>) => Promise<HaiResult<A2ACallerIdentity>>;
529
- }
530
- /** A2A 任务查询过滤器 */
531
- interface A2ATaskFilter {
532
- /** 按消息角色过滤(user / agent),不是任务生命周期状态 */
533
- status?: string | string[];
534
- /** 按上下文 ID 过滤 */
535
- contextId?: string;
536
- /** 按调用方 Agent ID 过滤 */
537
- callerId?: string;
538
- /** 时间范围起始(毫秒) */
539
- since?: number;
540
- /** 最大返回数 */
541
- limit?: number;
542
- /** 偏移量 */
543
- offset?: number;
544
- }
545
- /**
546
- * A2A 操作接口
547
- *
548
- * 提供 Agent-to-Agent 协议能力:Agent Card 管理、请求处理、客户端调用。
549
- */
550
- interface A2AOperations {
551
- /**
552
- * 注册 Agent 执行器
553
- *
554
- * 需要先调用 `ai.init()` 并配置 `a2a` 后才能注册。
555
- * 注册后 `handleRequest()`、`listMessages()` 等方法即可使用。
556
- *
557
- * @param executor - Agent 执行器(由应用层实现)
558
- * @returns 注册成功返回 `ok(undefined)`;未配置/未初始化返回 `err(HaiAIError.*)`
559
- */
560
- registerExecutor: (executor: AgentExecutor) => HaiResult<void>;
561
- /**
562
- * 获取当前 Agent Card
563
- *
564
- * 配置了 a2a 后即可使用,无需注册 executor。
565
- *
566
- * @returns 符合 A2A 0.3 的完整 Agent Card(含能力与安全方案)
567
- */
568
- getAgentCard: () => HaiResult<AgentCard>;
569
- /**
570
- * 处理入站 JSON-RPC 请求
571
- *
572
- * 将 HTTP 请求体路由到 SDK 的 JsonRpcTransportHandler。
573
- *
574
- * @param requestBody - JSON-RPC 请求体
575
- * @param context - 可信 HTTP 层认证上下文;agentId 映射为 SDK user.userName
576
- * @returns JSON-RPC 响应(单条或流式);未初始化也返回 JSON-RPC 错误,不混入 HaiResult
577
- */
578
- handleRequest: (requestBody: unknown, context?: Record<string, unknown>) => Promise<A2AHandleResult>;
579
- /**
580
- * 查询 A2A 消息列表(服务端管理接口,不自动推断调用方权限)
581
- *
582
- * @param filter - 查询过滤器
583
- * @returns 分页的消息记录列表
584
- */
585
- listMessages: (filter: A2ATaskFilter) => Promise<HaiResult<StorePage<A2AMessageRecord>>>;
586
- /**
587
- * 作为客户端调用远端 Agent
588
- *
589
- * remoteUrl 只应来自受信配置。框架校验协议和内嵌凭据,应用仍须按部署环境实施来源白名单与出口策略。
590
- *
591
- * @param remoteUrl - 远端 Agent 的 A2A 端点 URL
592
- * @param message - 发送的消息文本
593
- * @param options - 调用选项
594
- * @returns 远端响应
595
- */
596
- callRemoteAgent: (remoteUrl: string, message: string, options?: A2ACallOptions) => Promise<HaiResult<A2ACallResult>>;
597
- }
598
- /** A2A API Key 安全配置 */
599
- interface A2AApiKeySecurity {
600
- /** API Key 的传递位置 */
601
- in: 'header' | 'query';
602
- /** 参数名 */
603
- name: string;
604
- }
605
- /** A2A 安全认证配置 */
606
- interface A2ASecurityConfig {
607
- /** API Key 认证配置 */
608
- apiKey?: A2AApiKeySecurity;
609
- }
610
- /** Agent Card 配置(应用层提供) */
611
- interface A2AAgentCardConfig {
612
- /** Agent 名称 */
613
- name: string;
614
- /** Agent 描述 */
615
- description?: string;
616
- /** Agent URL(对外可访问的 base URL) */
617
- url: string;
618
- /** Agent 版本 */
619
- version?: string;
620
- /** Agent 能力声明 */
621
- skills?: Array<{
622
- id: string;
623
- name: string;
624
- description?: string;
625
- tags?: string[];
626
- }>;
627
- /** 安全认证配置(体现在 Agent Card 的 securitySchemes / security 字段) */
628
- security?: A2ASecurityConfig;
629
- }
630
- /** 处理结果(单条或流式) */
631
- interface A2AHandleResult {
632
- /** 是否为流式响应 */
633
- streaming: boolean;
634
- /** 单条 JSON-RPC 响应体(非流式时) */
635
- body?: unknown;
636
- /** 流式 JSON-RPC 响应迭代器(流式时) */
637
- stream?: AsyncGenerator<unknown, void, undefined>;
638
- }
639
- /** 远端调用选项 */
640
- interface A2ACallOptions {
641
- /** 超时时间(毫秒),默认 60 秒。 */
642
- timeout?: number;
643
- /** 额外请求头 */
644
- headers?: Record<string, string>;
645
- }
646
- /** 远端调用结果 */
647
- interface A2ACallResult {
648
- /** 远端任务 ID */
649
- taskId?: string;
650
- /** 任务最终状态 */
651
- taskState?: string;
652
- /** 响应消息文本 */
653
- responseText?: string;
654
- /** 响应消息 Part 数组 */
655
- responseParts?: unknown[];
656
- }
657
-
658
469
  /**
659
470
  * @h-ai/ai — LLM 子功能类型
660
471
  *
@@ -3267,6 +3078,195 @@ type AIConfig = z.infer<typeof AIConfigSchema>;
3267
3078
  /** AI 配置输入类型(允许部分字段) */
3268
3079
  type AIConfigInput = z.input<typeof AIConfigSchema>;
3269
3080
 
3081
+ /**
3082
+ * @h-ai/ai — A2A(Agent-to-Agent)子功能类型
3083
+ *
3084
+ * 定义 A2A 协议集成所需的类型、SDK 类型再导出、操作接口。
3085
+ * @module ai-a2a-types
3086
+ */
3087
+
3088
+ /** A2A 调用方身份信息 */
3089
+ interface A2ACallerIdentity {
3090
+ /** 调用方 Agent ID(如 AgentCard.name 或自定义标识) */
3091
+ agentId: string;
3092
+ /** 调用方显示名 */
3093
+ name?: string;
3094
+ /** 调用方 URL(AgentCard 地址) */
3095
+ url?: string;
3096
+ }
3097
+ /** A2A 消息记录(存储每条 A2A 交互) */
3098
+ interface A2AMessageRecord {
3099
+ /** 记录 ID(自动生成) */
3100
+ id: string;
3101
+ /** 关联的 A2A Task ID */
3102
+ taskId: string;
3103
+ /** 所属会话上下文 ID(历史记录可能缺失) */
3104
+ contextId?: string;
3105
+ /** 消息角色:user(入站请求)/ agent(出站响应) */
3106
+ role: 'user' | 'agent';
3107
+ /** 消息内容(JSON 序列化的 Part[] 数组) */
3108
+ parts: unknown[];
3109
+ /** 调用方身份信息(仅 role=user 时) */
3110
+ caller?: A2ACallerIdentity;
3111
+ /** 创建时间戳(毫秒) */
3112
+ createdAt: number;
3113
+ }
3114
+ /** A2A 上下文信息(对话/会话级别) */
3115
+ interface A2AContextInfo {
3116
+ /** 上下文 ID(对应 SDK 的 contextId) */
3117
+ id: string;
3118
+ /** 关联的 Agent 标识 */
3119
+ agentId?: string;
3120
+ /** 上下文标题(可选) */
3121
+ title?: string;
3122
+ /** 创建时间戳 */
3123
+ createdAt: number;
3124
+ /** 最后更新时间戳 */
3125
+ updatedAt: number;
3126
+ }
3127
+ /**
3128
+ * A2A 认证器接口
3129
+ *
3130
+ * 纯接口设计,由应用层实现,Kit 模块做胶水集成。
3131
+ * 用于验证入站 A2A 请求的身份。
3132
+ */
3133
+ interface A2AAuthenticator {
3134
+ /**
3135
+ * 验证入站请求
3136
+ *
3137
+ * @param headers - HTTP 请求头
3138
+ * @returns 成功返回调用方身份信息,失败返回错误
3139
+ */
3140
+ authenticate: (headers: Record<string, string | undefined>) => Promise<HaiResult<A2ACallerIdentity>>;
3141
+ }
3142
+ /** A2A 任务查询过滤器 */
3143
+ interface A2ATaskFilter {
3144
+ /** 按消息角色过滤(user / agent),不是任务生命周期状态 */
3145
+ status?: string | string[];
3146
+ /** 按上下文 ID 过滤 */
3147
+ contextId?: string;
3148
+ /** 按调用方 Agent ID 过滤 */
3149
+ callerId?: string;
3150
+ /** 时间范围起始(毫秒) */
3151
+ since?: number;
3152
+ /** 最大返回数 */
3153
+ limit?: number;
3154
+ /** 偏移量 */
3155
+ offset?: number;
3156
+ }
3157
+ /**
3158
+ * A2A 操作接口
3159
+ *
3160
+ * 提供 Agent-to-Agent 协议能力:Agent Card 管理、请求处理、客户端调用。
3161
+ */
3162
+ interface A2AOperations {
3163
+ /**
3164
+ * 注册 Agent 执行器
3165
+ *
3166
+ * 需要先调用 `ai.init()` 并配置 `a2a` 后才能注册。
3167
+ * 注册后 `handleRequest()`、`listMessages()` 等方法即可使用。
3168
+ *
3169
+ * @param executor - Agent 执行器(由应用层实现)
3170
+ * @returns 注册成功返回 `ok(undefined)`;未配置/未初始化返回 `err(HaiAIError.*)`
3171
+ */
3172
+ registerExecutor: (executor: AgentExecutor) => HaiResult<void>;
3173
+ /**
3174
+ * 获取当前 Agent Card
3175
+ *
3176
+ * 配置了 a2a 后即可使用,无需注册 executor。
3177
+ *
3178
+ * @returns 符合 A2A 0.3 的完整 Agent Card(含能力与安全方案)
3179
+ */
3180
+ getAgentCard: () => HaiResult<AgentCard>;
3181
+ /**
3182
+ * 处理入站 JSON-RPC 请求
3183
+ *
3184
+ * 将 HTTP 请求体路由到 SDK 的 JsonRpcTransportHandler。
3185
+ *
3186
+ * @param requestBody - JSON-RPC 请求体
3187
+ * @param context - 可信 HTTP 层认证上下文;agentId 映射为 SDK user.userName
3188
+ * @returns JSON-RPC 响应(单条或流式);未初始化也返回 JSON-RPC 错误,不混入 HaiResult
3189
+ */
3190
+ handleRequest: (requestBody: unknown, context?: Record<string, unknown>) => Promise<A2AHandleResult>;
3191
+ /**
3192
+ * 查询 A2A 消息列表(服务端管理接口,不自动推断调用方权限)
3193
+ *
3194
+ * @param filter - 查询过滤器
3195
+ * @returns 分页的消息记录列表
3196
+ */
3197
+ listMessages: (filter: A2ATaskFilter) => Promise<HaiResult<StorePage<A2AMessageRecord>>>;
3198
+ /**
3199
+ * 作为客户端调用远端 Agent
3200
+ *
3201
+ * remoteUrl 只应来自受信配置。框架校验协议和内嵌凭据,应用仍须按部署环境实施来源白名单与出口策略。
3202
+ *
3203
+ * @param remoteUrl - 远端 Agent 的 A2A 端点 URL
3204
+ * @param message - 发送的消息文本
3205
+ * @param options - 调用选项
3206
+ * @returns 远端响应
3207
+ */
3208
+ callRemoteAgent: (remoteUrl: string, message: string, options?: A2ACallOptions) => Promise<HaiResult<A2ACallResult>>;
3209
+ }
3210
+ /** A2A API Key 安全配置 */
3211
+ interface A2AApiKeySecurity {
3212
+ /** API Key 的传递位置 */
3213
+ in: 'header' | 'query';
3214
+ /** 参数名 */
3215
+ name: string;
3216
+ }
3217
+ /** A2A 安全认证配置 */
3218
+ interface A2ASecurityConfig {
3219
+ /** API Key 认证配置 */
3220
+ apiKey?: A2AApiKeySecurity;
3221
+ }
3222
+ /** Agent Card 配置(应用层提供) */
3223
+ interface A2AAgentCardConfig {
3224
+ /** Agent 名称 */
3225
+ name: string;
3226
+ /** Agent 描述 */
3227
+ description?: string;
3228
+ /** Agent URL(对外可访问的 base URL) */
3229
+ url: string;
3230
+ /** Agent 版本 */
3231
+ version?: string;
3232
+ /** Agent 能力声明 */
3233
+ skills?: Array<{
3234
+ id: string;
3235
+ name: string;
3236
+ description?: string;
3237
+ tags?: string[];
3238
+ }>;
3239
+ /** 安全认证配置(体现在 Agent Card 的 securitySchemes / security 字段) */
3240
+ security?: A2ASecurityConfig;
3241
+ }
3242
+ /** 处理结果(单条或流式) */
3243
+ interface A2AHandleResult {
3244
+ /** 是否为流式响应 */
3245
+ streaming: boolean;
3246
+ /** 单条 JSON-RPC 响应体(非流式时) */
3247
+ body?: unknown;
3248
+ /** 流式 JSON-RPC 响应迭代器(流式时) */
3249
+ stream?: AsyncGenerator<unknown, void, undefined>;
3250
+ }
3251
+ /** 远端调用选项 */
3252
+ interface A2ACallOptions {
3253
+ /** 超时时间(毫秒),默认 60 秒。 */
3254
+ timeout?: number;
3255
+ /** 额外请求头 */
3256
+ headers?: Record<string, string>;
3257
+ }
3258
+ /** 远端调用结果 */
3259
+ interface A2ACallResult {
3260
+ /** 远端任务 ID */
3261
+ taskId?: string;
3262
+ /** 任务最终状态 */
3263
+ taskState?: string;
3264
+ /** 响应消息文本 */
3265
+ responseText?: string;
3266
+ /** 响应消息 Part 数组 */
3267
+ responseParts?: unknown[];
3268
+ }
3269
+
3270
3270
  /**
3271
3271
  * @h-ai/ai — Audio 子功能公共类型
3272
3272
  *