@zero-library/chat-agent 2.1.20 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- import { createRequest, RenderControl } from '@zero-library/common';
1
+ import { createRequest, RequestConfig, RenderControl } from '@zero-library/common';
2
2
  import { ButtonProps, ThemeConfig } from 'antd';
3
3
  import * as react from 'react';
4
4
  import { Conversation, BubbleProps } from '@ant-design/x';
5
- export * from '@ant-design/x';
6
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
7
6
 
8
7
  /**
@@ -16,27 +15,13 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
16
15
  */
17
16
  interface UserInfo {
18
17
  /** 用户ID */
19
- id: number;
20
- /** 机构名称 */
21
- orgName?: string;
18
+ id?: string | number;
22
19
  /** 用户姓名 */
23
- name: string;
24
- /** 用户名 */
25
- username?: string;
26
- /** 手机号 */
27
- mobile?: string;
20
+ name?: string;
28
21
  /** 头像地址 */
29
- avatar: string;
30
- /** 我的邀请码 */
31
- myInviteCode?: string;
32
- /** 邮箱地址 */
33
- email?: string;
22
+ avatar?: string;
34
23
  /** 备注信息 */
35
24
  remark?: string;
36
- /** 角色ID列表 */
37
- roles?: number[];
38
- /** 权限代码列表 */
39
- permissions?: string[];
40
25
  }
41
26
  /**
42
27
  * 会话列表查询参数接口
@@ -467,13 +452,23 @@ declare const createFileService: (request: ReturnType<typeof createRequest>) =>
467
452
 
468
453
  /**
469
454
  * 会话策略类型
470
- * 1-最近会话:获取用户最近的会话继续对话
471
- * 2-新会话:创建全新的会话开始对话
455
+ * 定义创建或获取会话的策略
456
+ *
457
+ * 取值说明:
458
+ * - 1: 最近会话策略 - 获取用户最近的会话继续对话
459
+ * - 2: 新会话策略 - 创建全新的会话开始对话
472
460
  */
473
461
  type ConversationStrategy = 1 | 2;
474
462
  /**
475
463
  * 引用内容接口
476
464
  * 扩展消息引用信息,支持更多引用格式
465
+ *
466
+ * @example
467
+ * // 引用文档片段
468
+ * const quote: ReferencesContent = {
469
+ * name: '法规条款',
470
+ * markdown: '> 根据《企业所得税法》第XX条规定...',
471
+ * }
477
472
  */
478
473
  interface ReferencesContent extends MessageQuoteMsg {
479
474
  /** 引用名称 */
@@ -486,73 +481,418 @@ interface ReferencesContent extends MessageQuoteMsg {
486
481
  /**
487
482
  * 引用类型接口
488
483
  * 定义消息引用的各种类型和参数
484
+ *
485
+ * @example
486
+ * // Markdown引用(拼接到正文发送)
487
+ * const markdownRef: ReferencesType = {
488
+ * type: 1,
489
+ * content: {
490
+ * name: '法规摘要',
491
+ * markdown: '> 根据相关规定...'
492
+ * },
493
+ * params: {
494
+ * source: 'law-001'
495
+ * }
496
+ * }
497
+ *
498
+ * // 文案引用(不发送正文)
499
+ * const textRef: ReferencesType = {
500
+ * type: 2,
501
+ * content: {
502
+ * name: '参考答案',
503
+ * markdown: '建议按以下方式处理...'
504
+ * }
505
+ * }
506
+ *
507
+ * // 消息引用
508
+ * const messageRef: ReferencesType = {
509
+ * type: 3,
510
+ * content: {
511
+ * id: 'msg-001',
512
+ * msgContent: '之前的回答内容'
513
+ * }
514
+ * }
489
515
  */
490
516
  interface ReferencesType {
491
517
  /** 引用类型:1-markdown引用(拼正文发) 2-文案引用(不发正文) 3-消息引用 */
492
518
  type: number;
493
519
  /** 引用内容 */
494
520
  content: ReferencesContent;
495
- /** 引用参数 */
521
+ /** 引用参数,最终透传大模型 */
496
522
  params?: ObjectType<any>;
497
523
  }
498
524
  /**
499
525
  * 聊天组件生命周期钩子接口
500
526
  * 定义聊天组件在各种事件发生时的回调函数
527
+ * @example
528
+ * // 完整的钩子配置示例
529
+ * const hooks: ChatHooks = {
530
+ * onBeforeInit: () => {
531
+ * console.log('聊天组件初始化前')
532
+ * },
533
+ * onAfterInit: () => {
534
+ * console.log('聊天组件初始化后')
535
+ * },
536
+ * onBeforeSend: (message, files) => {
537
+ * // 验证消息内容
538
+ * if (!message?.trim()) {
539
+ * message.error('请输入消息内容')
540
+ * return false // 阻止发送
541
+ * }
542
+ * return true // 允许发送
543
+ * },
544
+ * onAfterSend: () => {
545
+ * console.log('消息发送完成')
546
+ * },
547
+ * onRequestInterceptor: [
548
+ * (config) => {
549
+ * // 添加认证头
550
+ * config.headers.Authorization = `Bearer ${getToken()}`
551
+ * return config
552
+ * },
553
+ * (error) => {
554
+ * console.error('请求错误:', error)
555
+ * return Promise.reject(error)
556
+ * }
557
+ * ],
558
+ * ...
559
+ * }
501
560
  */
502
561
  interface ChatHooks {
503
- /** 初始化前回调 */
562
+ /**
563
+ * 初始化前回调
564
+ * 在聊天组件初始化之前调用
565
+ *
566
+ * @example
567
+ * onBeforeInit: () => {
568
+ * console.log('准备初始化聊天组件')
569
+ * }
570
+ */
504
571
  onBeforeInit?: () => void;
505
- /** 初始化后回调 */
572
+ /**
573
+ * 初始化后回调
574
+ * 在聊天组件初始化完成后调用
575
+ *
576
+ * @example
577
+ * onAfterInit: () => {
578
+ * console.log('聊天组件初始化完成')
579
+ * }
580
+ */
506
581
  onAfterInit?: () => void;
507
- /** 输入框聚焦回调 */
582
+ /**
583
+ * 输入框聚焦回调
584
+ * 当输入框获得焦点时调用
585
+ *
586
+ * @example
587
+ * onSenderFocus: () => {
588
+ * console.log('输入框获得焦点')
589
+ * }
590
+ */
508
591
  onSenderFocus?: () => void;
509
- /** 头部关闭回调 */
592
+ /**
593
+ * 头部关闭回调
594
+ * 当聊天窗口头部关闭按钮被点击时调用
595
+ *
596
+ * @example
597
+ * onHeaderClose: () => {
598
+ * console.log('聊天窗口关闭')
599
+ * // 可以在这里执行清理操作
600
+ * }
601
+ */
510
602
  onHeaderClose?: () => void;
511
- /** 发送消息前回调,可阻止发送 */
603
+ /**
604
+ * 发送消息前回调
605
+ * 在消息发送前调用,返回false可阻止发送
606
+ *
607
+ * @param message - 要发送的消息内容
608
+ * @param files - 要发送的文件列表
609
+ * @returns boolean | Promise<boolean> | void - 返回false阻止发送
610
+ *
611
+ * @example
612
+ * onBeforeSend: (message, files) => {
613
+ * // 检查消息长度
614
+ * if (message.length > 1000) {
615
+ * message.error('消息内容过长')
616
+ * return false
617
+ * }
618
+ * return true
619
+ * }
620
+ */
512
621
  onBeforeSend?: (message: ConversationMessageSend['msgContent'], files: InputFile[]) => boolean | Promise<boolean> | void;
513
- /** 发送消息后回调 */
622
+ /**
623
+ * 发送消息后回调
624
+ * 在消息发送完成后调用
625
+ *
626
+ * @example
627
+ * onAfterSend: () => {
628
+ * console.log('消息发送完成')
629
+ * // 可以在这里执行后续操作,如滚动到底部
630
+ * }
631
+ */
514
632
  onAfterSend?: () => void;
515
- /** 切换智能体前回调,可阻止切换 */
633
+ /**
634
+ * 切换智能体前回调
635
+ * 在切换智能体前调用,返回false可阻止切换
636
+ *
637
+ * @param agentId - 目标智能体ID
638
+ * @returns boolean | Promise<boolean> | void - 返回false阻止切换
639
+ *
640
+ * @example
641
+ * onBeforeSwitchAgent: (agentId) => {
642
+ * // 检查智能体权益
643
+ * }
644
+ */
516
645
  onBeforeSwitchAgent?: (agentId: string) => boolean | Promise<boolean> | void;
517
- /** 切换智能体后回调 */
646
+ /**
647
+ * 切换智能体后回调
648
+ * 在切换智能体完成后调用
649
+ *
650
+ * @param agent - 新的智能体信息
651
+ *
652
+ * @example
653
+ * onAfterSwitchAgent: (agent) => {
654
+ * console.log(`已切换到智能体: ${agent.agentName}`)
655
+ * // 可以在这里获取最新的智能体
656
+ * }
657
+ */
518
658
  onAfterSwitchAgent?: (agent: AgentInfo) => void;
519
- /** 切换会话前回调,可阻止切换 */
659
+ /**
660
+ * 切换会话前回调
661
+ * 在切换会话前调用,返回false可阻止切换
662
+ *
663
+ * @param conversationId - 目标会话ID
664
+ * @returns boolean | Promise<boolean> | void - 返回false阻止切换
665
+ *
666
+ * @example
667
+ * onBeforeSwitchConversation: (conversationId) => {
668
+ * // 检查当前会话状态
669
+ * if (isSendingMessage) {
670
+ * message.warning('正在发送消息,请稍后再切换会话')
671
+ * return false
672
+ * }
673
+ * return true
674
+ * }
675
+ */
520
676
  onBeforeSwitchConversation?: (conversationId: string) => boolean | Promise<boolean> | void;
521
- /** 切换会话后回调 */
677
+ /**
678
+ * 在切换会话完成后调用
679
+ *
680
+ * @param conversationId - 新的会话ID
681
+ *
682
+ * @example
683
+ * onAfterSwitchConversation: (conversationId) => {
684
+ * console.log(`已切换到会话: ${conversationId}`)
685
+ * // 可以在这里更新界面状态
686
+ * }
687
+ */
522
688
  onAfterSwitchConversation?: (conversationId: string) => void;
523
- /** 初始化消息前回调,可阻止初始化 */
689
+ /**
690
+ * 初始化消息前回调
691
+ * 在初始化消息列表前调用,返回false可阻止初始化
692
+ *
693
+ * @param conversationId - 会话ID
694
+ * @returns boolean | Promise<boolean> | void - 返回false阻止初始化
695
+ *
696
+ * @example
697
+ * onBeforeInitMessages: (conversationId) => {
698
+ * // 检查会话权限
699
+ * if (!hasPermission(conversationId)) {
700
+ * message.error('无权访问此会话')
701
+ * return false
702
+ * }
703
+ * return true
704
+ * }
705
+ */
524
706
  onBeforeInitMessages?: (conversationId: string) => boolean | Promise<boolean> | void;
525
- /** 初始化消息后回调 */
707
+ /**
708
+ * 初始化消息后回调
709
+ * 在初始化消息列表完成后调用
710
+ *
711
+ * @param messages - 初始化的消息列表
712
+ *
713
+ * @example
714
+ * onAfterInitMessages: (messages) => {
715
+ * console.log(`加载了 ${messages.length} 条消息`)
716
+ * // 可以在这里执行消息分析等操作
717
+ * }
718
+ */
526
719
  onAfterInitMessages?: (messages: ConversationMessage[]) => void;
527
- /** 删除会话前回调,可阻止删除 */
720
+ /**
721
+ * 删除会话前回调
722
+ * 在删除会话前调用,返回false可阻止删除
723
+ *
724
+ * @param conversationId - 要删除的会话ID
725
+ * @returns boolean | Promise<boolean> | void - 返回false阻止删除
726
+ *
727
+ * @example
728
+ * onBeforeDelConversation: (conversationId) => {
729
+ * window.confirm('确定要删除此会话吗?删除后无法恢复。')
730
+ * return confirm
731
+ * }
732
+ */
528
733
  onBeforeDelConversation?: (conversationId: string) => boolean | Promise<boolean> | void;
529
- /** 删除会话后回调 */
734
+ /**
735
+ * 在删除会话完成后调用
736
+ *
737
+ * @param conversationId - 已删除的会话ID
738
+ * @param isCurrentConversation - 是否为当前会话
739
+ *
740
+ * @example
741
+ * onAfterDelConversation: (conversationId, isCurrentConversation) => {
742
+ * console.log(`会话 ${conversationId} 已删除`)
743
+ * if (isCurrentConversation) {
744
+ * // 当前会话被删除,可能需要切换到其他会话
745
+ * }
746
+ * }
747
+ */
530
748
  onAfterDelConversation?: (conversationId: string, isCurrentConversation: boolean) => void;
749
+ /**
750
+ * 接收消息前回调
751
+ * 在接收到WebSocket消息后调用,返回false可阻止渲染
752
+ *
753
+ * @param message - 接收到的消息
754
+ * @returns boolean | Promise<boolean> | void - 返回false阻止渲染
755
+ *
756
+ * @example
757
+ * onBeforeAcceptMessage: (message) => {
758
+ * // 过滤特定类型的消息
759
+ * if (message.type === 'SYSTEM_NOTIFICATION') {
760
+ * handleSystemNotification(message)
761
+ * return false // 不在聊天界面显示
762
+ * }
763
+ * return true
764
+ * }
765
+ */
766
+ onBeforeAcceptMessage?: (message: ConversationMessage) => boolean | Promise<boolean> | void;
767
+ /**
768
+ * 在接收到WebSocket消息并处理完成后调用
769
+ *
770
+ * @param message - 处理完成的消息
771
+ *
772
+ * @example
773
+ * onAfterAcceptMessage: (message) => {
774
+ * console.log('收到新消息:', message)
775
+ * // 可以在这里执行消息通知等操作
776
+ * }
777
+ */
778
+ onAfterAcceptMessage?: (message: ConversationMessage) => void;
779
+ /**
780
+ * HTTP 请求拦截器
781
+ * 在每个HTTP请求发送前调用,可用于修改请求配置、添加认证头等
782
+ *
783
+ * 拦截器数组包含两个函数:
784
+ * 1. 第一个函数:处理成功的请求配置
785
+ * - 参数:当前请求配置对象
786
+ * - 返回:修改后的请求配置对象或Promise
787
+ * 2. 第二个函数:处理请求错误
788
+ * - 参数:错误对象
789
+ * - 返回:错误处理结果或Promise
790
+ *
791
+ * @example
792
+ * // 添加认证头和日志
793
+ * onRequestInterceptor: [
794
+ * (config) => {
795
+ * // 添加认证头
796
+ * config.headers.Authorization = `Bearer ${getToken()}`
797
+ * console.log('发送请求:', config)
798
+ * return config
799
+ * },
800
+ * (error) => {
801
+ * // 处理请求错误
802
+ * console.error('请求错误:', error)
803
+ * return Promise.reject(error)
804
+ * }
805
+ * ]
806
+ */
807
+ onRequestInterceptor?: [(config: RequestConfig) => RequestConfig | Promise<RequestConfig>, (error: any) => any];
808
+ /**
809
+ * HTTP 响应拦截器
810
+ * 在每个HTTP响应返回后调用,可用于处理响应数据或错误
811
+ *
812
+ * 拦截器数组包含两个函数:
813
+ * 1. 第一个函数:处理成功的响应
814
+ * - 参数:响应对象
815
+ * - 返回:处理后的响应数据或Promise
816
+ * 2. 第二个函数:处理响应错误
817
+ * - 参数:错误对象
818
+ * - 返回:错误处理结果或Promise
819
+ *
820
+ * @example
821
+ * // 统一处理响应和错误
822
+ * onResponseInterceptor: [
823
+ * (response) => {
824
+ * // 统一处理业务错误
825
+ * if (response.data.code !== 200) {
826
+ * message.error(response.data.message)
827
+ * }
828
+ * return response.data
829
+ * },
830
+ * (error) => {
831
+ * // 统一处理HTTP错误
832
+ * if (error?.response?.status === 401) {
833
+ * // 未授权,跳转登录
834
+ * onRedirectLogin?.()
835
+ * }
836
+ * return Promise.reject(error)
837
+ * }
838
+ * ]
839
+ */
840
+ onResponseInterceptor?: [(response: any) => any | Promise<any>, (error: any) => any];
841
+ /**
842
+ * 重定向到登录页面回调函数
843
+ * 当用户未授权或认证失败时调用,用于执行跳转登录逻辑
844
+ *
845
+ * 使用场景:
846
+ * - HTTP 401 未授权响应
847
+ *
848
+ * @example
849
+ * // 自定义登录跳转逻辑
850
+ * onRedirectLogin: () => {
851
+ * // 清除认证信息
852
+ * tokenManager.clear()
853
+ * userInfoManager.clear()
854
+ * // 跳转到登录页
855
+ * window.location.href = '/login'
856
+ * }
857
+ */
858
+ onRedirectLogin?: () => void;
531
859
  }
532
860
  /**
533
861
  * 聊天参数接口
534
862
  * 定义聊天组件的运行时参数
535
863
  */
536
864
  interface ChatParams {
537
- /** 会话来源 */
538
- source?: number;
539
- /** 业务扩展数据,JSON字符串格式 */
865
+ /** 会话来源设置:1-罗拉-APP 2-罗拉-pc端 3-合作伙伴端 4-税协 5-罗拉-cube 6-api */
866
+ source: number;
867
+ /** 业务扩展数据,非必填 JSON字符串格式 */
540
868
  businessData?: string;
541
- /** 业务数据唯一ID */
869
+ /** 业务数据唯一ID 非必填 */
542
870
  businessId?: string;
543
- /** 业务类型 */
871
+ /** 业务类型 非必填 1-风控;2-弹窗 */
544
872
  businessType?: number;
545
- /** 扩展参数对象 */
873
+ /** 扩展参数对象 透传大模型 */
546
874
  params?: ObjectType<any>;
547
875
  }
548
876
  /**
549
877
  * 聊天布局头部配置接口
550
878
  * 控制聊天头部区域的显示和功能
551
- * 默认 title = true, closeBtn = false, newConversationBtn = true, conversationListBtn = true
879
+ * ChatHeader组件默认 title = true, avatar = true, closeBtn = false, newConversationBtn = true, conversationListBtn = true
880
+ * @example
881
+ * // 自定义头部配置
882
+ * const header: ChatLayoutHeader = {
883
+ * title: true, // 显示标题
884
+ * avatar: true, // 显示头像
885
+ * closeBtn: true, // 显示关闭按钮
886
+ * newConversationBtn: false, // 隐藏新建会话按钮
887
+ * agentCharacter: true, // 显示智能体性格选择
888
+ * conversationListBtn: true // 显示会话列表按钮
889
+ * }
552
890
  */
553
891
  interface ChatLayoutHeader {
554
- /** 头部标题渲染控制 */
892
+ /** 标题渲染控制 */
555
893
  title?: RenderControl<void, void>;
894
+ /** 头像渲染控制 */
895
+ avatar?: RenderControl<void, void>;
556
896
  /** 是否显示关闭按钮 */
557
897
  closeBtn?: boolean;
558
898
  /** 新建会话按钮渲染控制 */
@@ -565,7 +905,12 @@ interface ChatLayoutHeader {
565
905
  /**
566
906
  * 聊天布局会话列表配置接口
567
907
  * 控制会话列表区域的显示和功能
568
- * 默认 header = true
908
+ * ConversationList组件默认 header = true
909
+ * @example
910
+ * // 自定义会话列表配置
911
+ * const conversationList: ChatLayoutConversationList = {
912
+ * header: true // 显示会话列表头部
913
+ * }
569
914
  */
570
915
  interface ChatLayoutConversationList {
571
916
  /** 会话列表头部渲染控制 */
@@ -574,7 +919,19 @@ interface ChatLayoutConversationList {
574
919
  /**
575
920
  * 聊天布局输入框配置接口
576
921
  * 控制输入框区域的功能和样式
577
- * 默认 placeholder, extraBtn = false, referencesBtn = false
922
+ * ChatSender组件默认 placeholder, extraBtn = false, referencesBtn = false
923
+ * @example
924
+ * // 自定义输入框配置
925
+ * const sender: ChatLayoutSender = {
926
+ * placeholder: '请输入您的问题...',
927
+ * extraBtn: true, // 显示额外按钮
928
+ * referencesBtn: true, // 显示引用按钮
929
+ * prompts: true, // 显示推荐问题
930
+ * sendBtnProps: () => ({
931
+ * type: 'primary',
932
+ * children: '发送'
933
+ * })
934
+ * }
578
935
  */
579
936
  interface ChatLayoutSender {
580
937
  /** 输入框占位符文本 */
@@ -590,19 +947,67 @@ interface ChatLayoutSender {
590
947
  /** 是否显示推荐问题 */
591
948
  prompts?: boolean;
592
949
  }
950
+ /**
951
+ * 消息列表欢迎信息配置
952
+ * 控制聊天界面初始欢迎信息的显示内容和自定义渲染
953
+ * @example
954
+ * // 自定义欢迎信息
955
+ * const welcomeMessage: ChatLayoutWelcomeMessage = {
956
+ * icon: true, // 显示默认图标
957
+ * title: {render: () => <>欢迎使用AI助手</>}, // 修改标题
958
+ * description: false, // 不显示详情
959
+ * prompts: true // 显示推荐问题
960
+ * }
961
+ */
962
+ interface ChatLayoutWelcomeMessage {
963
+ icon?: RenderControl<void, void>;
964
+ title?: RenderControl<void, void>;
965
+ description?: RenderControl<void, void>;
966
+ prompts?: RenderControl<void, void>;
967
+ }
593
968
  /**
594
969
  * 聊天布局消息列表配置接口
595
970
  * 控制消息列表区域的显示和功能
971
+ * @example
972
+ * // 自定义消息列表配置
973
+ * const messageList: ChatLayoutMessageList = {
974
+ * avatar: {
975
+ * user: true,
976
+ * agent: true,
977
+ * other: false
978
+ * },
979
+ * firstMessage: false // 不显示首条消息
980
+ * welcomeMessage: false // 不显示欢迎消息
981
+ * }
596
982
  */
597
983
  interface ChatLayoutMessageList {
598
984
  /** 头像显示控制:可以是全局布尔值或按成员类型分别控制 */
599
985
  avatar?: boolean | Partial<Record<ConversationMemberEnum, boolean>>;
600
986
  /** 首条消息渲染控制 */
601
987
  firstMessage?: RenderControl<void, void>;
988
+ /** 欢迎消息渲染控制, 要渲染时如果列表有内容则不渲染 */
989
+ welcomeMessage?: RenderControl<void, ChatLayoutWelcomeMessage>;
602
990
  }
603
991
  /**
604
- * 聊天布局配置接口
992
+ * 聊天布局配置接口(专家和智能体各有一套默认布局)
605
993
  * 控制聊天组件整体布局的显示和功能
994
+ *
995
+ * @example
996
+ * const layout: ChatLayout = {
997
+ * leftPanel: false,
998
+ * conversationList: false,
999
+ * preview: false,
1000
+ * messageList: true,
1001
+ * sender: {
1002
+ * props: {
1003
+ * prompts: false
1004
+ * }
1005
+ * },
1006
+ * chatHeader: {
1007
+ * render: (props) => <div>{props.children}</div>:
1008
+ * },
1009
+ * disclaimerNotice: false
1010
+ * }
606
1011
  */
607
1012
  interface ChatLayout {
608
1013
  /** 左侧面板渲染控制 */
@@ -626,17 +1031,90 @@ interface ChatLayout {
626
1031
  /** 免责声明渲染控制 */
627
1032
  disclaimerNotice?: RenderControl<void, void>;
628
1033
  }
1034
+ type RequestInstance = ReturnType<typeof createChatService> & ReturnType<typeof createFileService>;
1035
+ /**
1036
+ * 聊天HTTP服务配置接口
1037
+ * 定义聊天组件所需的HTTP服务配置,用于API请求
1038
+ */
1039
+ interface ChatHttpServices {
1040
+ /**
1041
+ * 基于axios扩展的配置参数
1042
+ * 支持所有axios配置选项,如baseURL、timeout、headers等
1043
+ * @see {@link RequestConfig}
1044
+ */
1045
+ config?: RequestConfig;
1046
+ /**
1047
+ * 聊天服务和文件服务的合并请求对象(没有测试,暂时不使用)
1048
+ * 可以提供自定义的请求实例,用于替代默认创建的实例
1049
+ */
1050
+ request?: RequestInstance;
1051
+ }
1052
+ /**
1053
+ * 聊天WebSocket服务配置接口
1054
+ * 定义聊天组件所需的WebSocket服务配置,用于实时消息通信
1055
+ */
1056
+ interface ChatWebsocketServices {
1057
+ /**
1058
+ * WebSocket连接基础URL列表,支持多地址容错(目前只支持两个地址)
1059
+ *
1060
+ * 使用方式:
1061
+ * - undefined: 继承http.config.baseURL的值
1062
+ * - []: 空数组表示关闭WebSocket功能
1063
+ * - [url]: 单个URL用于WebSocket连接
1064
+ * - [url1, url2]: 两个URL用于WebSocket连接,支持多端容错
1065
+ */
1066
+ baseURLs?: (string | undefined)[];
1067
+ /**
1068
+ * WebSocket连接参数
1069
+ * - undefined: 继承http.config.headers的值
1070
+ * 附加到WebSocket URL上的查询参数
1071
+ */
1072
+ params?: Record<string, string | number>;
1073
+ }
629
1074
  /**
630
- * 聊天服务接口
631
- * 定义聊天组件所需的服务配置
1075
+ * 聊天服务配置接口
1076
+ * 定义聊天组件所需的所有服务配置,包括HTTP和WebSocket
1077
+ * * @example
1078
+ * // 基本配置,使用默认继承
1079
+ * const services: ChatServices = {
1080
+ * http: {
1081
+ * config: {
1082
+ * baseURL: '/api',
1083
+ * timeout: 10000,
1084
+ * headers: { 'NS-TOKEN': '123456' }
1085
+ * }
1086
+ * },
1087
+ * websocket: {} // 继承http的baseURL和headers参数
1088
+ * }
1089
+ *
1090
+ * @example
1091
+ * // 自定义WebSocket配置
1092
+ * const services: ChatServices = {
1093
+ * http: {
1094
+ * config: { baseURL: '/api' }
1095
+ * },
1096
+ * websocket: {
1097
+ * baseURLs: ['/api', '/api/saas'],
1098
+ * params: { token: 'abc123' }
1099
+ * }
1100
+ * }
1101
+ *
1102
+ * @example
1103
+ * // 关闭WebSocket功能
1104
+ * const services: ChatServices = {
1105
+ * http: {
1106
+ * config: { baseURL: '/api' }
1107
+ * },
1108
+ * websocket: {
1109
+ * baseURLs: [] // 空数组表示关闭WebSocket
1110
+ * }
1111
+ * }
632
1112
  */
633
1113
  interface ChatServices {
634
- /** WebSocket连接基础URL列表,支持多地址容错 */
635
- websocketBaseUrls?: string[];
636
- /** API基础URL */
637
- baseUrl?: string;
638
- /** 聊天服务和文件服务的合并请求对象 */
639
- request?: ReturnType<typeof createChatService> & ReturnType<typeof createFileService>;
1114
+ /** HTTP服务配置 */
1115
+ http?: ChatHttpServices;
1116
+ /** WebSocket服务配置 */
1117
+ websocket?: ChatWebsocketServices;
640
1118
  }
641
1119
 
642
1120
  /**
@@ -722,9 +1200,23 @@ declare const _default$2: react.ForwardRefExoticComponent<SenderProps & react.Re
722
1200
  /**
723
1201
  * 聊天组件配置接口
724
1202
  * 定义聊天组件的初始化配置选项
1203
+ *
1204
+ * @example
1205
+ * // 基本配置 - 指定智能体
1206
+ * const config: ChatConfig = {
1207
+ * receiverType: 3, // 智能体类型
1208
+ * receiverId: 'agent-001', // 智能体ID
1209
+ * conversationStrategy: 2 // 创建新会话
1210
+ * }
1211
+ *
1212
+ * @example
1213
+ * // 指定已有会话
1214
+ * const config: ChatConfig = {
1215
+ * conversationId: 'conv-001' // 直接打开指定会话
1216
+ * }
725
1217
  */
726
1218
  interface ChatConfig {
727
- /** 接收者类型:1-用户 2-专家 3-Agent */
1219
+ /** 接收者类型:1-用户 2-专家 3-Agent; 控制默认布局显示 */
728
1220
  receiverType?: number;
729
1221
  /** 接收者ID:专家或智能体ID */
730
1222
  receiverId?: string;
@@ -734,29 +1226,209 @@ interface ChatConfig {
734
1226
  conversationStrategy?: ConversationStrategy;
735
1227
  }
736
1228
  /**
737
- * 聊天组件操作句柄接口
738
1229
  * 提供外部操作聊天组件的方法
1230
+ *
1231
+ * @example
1232
+ * // 使用ref调用组件方法
1233
+ * const chatRef = useRef<ChatHandle>(null)
1234
+ * // 在组件中使用
1235
+ * <Chat ref={chatRef} config={chatConfig} />
1236
+ *
1237
+ * // 发送消息
1238
+ * chatRef.current?.sendMessage('你好,帮我分析一下这份报表')
1239
+ *
1240
+ * // 切换智能体会话
1241
+ * chatRef.current?.switchAgentConversation('agent-002')
1242
+ *
1243
+ * // 创建新会话
1244
+ * chatRef.current?.createConversation()
1245
+ *
1246
+ * // 设置引用内容
1247
+ * chatRef.current?.setReferences({
1248
+ * type: 1,
1249
+ * content: {
1250
+ * name: '法规条款',
1251
+ * markdown: '> 根据《企业所得税法》第XX条规定...'
1252
+ * }
1253
+ * })
1254
+ *
1255
+ * // 聚焦输入框
1256
+ * chatRef.current?.senderFocus()
739
1257
  */
740
1258
  interface ChatHandle {
741
- /** 发送消息 */
742
- sendMessage: (message?: string, files?: InputFile[]) => void;
743
- /** 切换智能体会话 */
1259
+ /** 发送消息
1260
+ * @param message - 消息内容,如果提供则直接发送该内容,否则使用输入框内容, 且两者必须存在一个
1261
+ * @param files - 文件列表,与消息一起发送的文件
1262
+ * @param params - 透传给大模型的参数,用于控制模型行为
1263
+ *
1264
+ * @remarks
1265
+ * 1. 传参调用则只会将参数直接传入接口,不会影响当前输入框数据
1266
+ * 2. 参数为空则触发默认逻辑,收集输入框数据、引用等内容发送
1267
+ * 3. 如果基于1想发送引用等内容,可以调用setMessage(message)-->sendMessage()
1268
+ *
1269
+ * @example
1270
+ * // 直接发送消息(不影响输入框)
1271
+ * chatRef.current?.sendMessage('你好,帮我分析一下这份报表')
1272
+ *
1273
+ * @example
1274
+ * // 触发默认逻辑(收集输入框内容发送)
1275
+ * chatRef.current?.sendMessage()
1276
+ */
1277
+ sendMessage: (message?: string, files?: InputFile[], params?: ObjectType<any>) => void;
1278
+ /**
1279
+ * 切换智能体会话
1280
+ * 切换到指定的智能体并创建或获取会话
1281
+ *
1282
+ * @param agentId - 目标智能体ID
1283
+ * @param strategy - 会话策略,1-使用最近会话,2-创建新会话
1284
+ *
1285
+ * @example
1286
+ * // 切换到指定智能体并创建新会话
1287
+ * chatRef.current?.switchAgentConversation('agent-001')
1288
+ *
1289
+ * @example
1290
+ * // 切换到指定智能体并使用最近会话
1291
+ * chatRef.current?.switchAgentConversation('agent-001', 1)
1292
+ */
744
1293
  switchAgentConversation: (agentId: AgentInfo['id'], strategy?: ConversationStrategy) => Promise<void>;
745
- /** 切换会话 */
746
- switchConversation: (id?: Conversation['id']) => Promise<void>;
747
- /** 创建新会话 */
1294
+ /**
1295
+ * 切换到指定的会话
1296
+ *
1297
+ * @param id - 目标会话ID
1298
+ *
1299
+ * @example
1300
+ * // 切换到指定会话
1301
+ * chatRef.current?.switchConversation('conv-001')
1302
+ */
1303
+ switchConversation: (id: Conversation['id']) => Promise<void>;
1304
+ /**
1305
+ * 基于当前接收者创建新的会话
1306
+ *
1307
+ * @example
1308
+ * // 创建新会话
1309
+ * chatRef.current?.createConversation()
1310
+ */
748
1311
  createConversation: () => Promise<void>;
749
- /** 设置引用消息 */
1312
+ /**
1313
+ * 设置引用消息
1314
+ * 在输入框中设置引用内容,将在下次发送消息时包含引用信息
1315
+ *
1316
+ * @param references - 引用内容对象
1317
+ *
1318
+ * @example
1319
+ * // 设置Markdown引用
1320
+ * chatRef.current?.setReferences({
1321
+ * type: 1,
1322
+ * content: {
1323
+ * name: '法规条款',
1324
+ * markdown: '> 根据《企业所得税法》第XX条规定...'
1325
+ * },
1326
+ * params: {
1327
+ * source: 'law-001'
1328
+ * }
1329
+ * })
1330
+ *
1331
+ * @example
1332
+ * // 清除引用
1333
+ * chatRef.current?.setReferences(undefined)
1334
+ */
750
1335
  setReferences: (references?: ReferencesType) => void;
751
- /** 设置输入框消息内容 */
1336
+ /**
1337
+ * 直接设置输入框中的文本内容
1338
+ *
1339
+ * @param message - 要设置的消息内容
1340
+ *
1341
+ * @example
1342
+ * // 设置输入框内容
1343
+ * chatRef.current?.setMessage('请帮我分析以下问题...')
1344
+ *
1345
+ * @example
1346
+ * // 清空输入框
1347
+ * chatRef.current?.setMessage('')
1348
+ */
752
1349
  setMessage: (message?: string) => void;
753
- /** 设置上传文件列表 */
1350
+ /**
1351
+ * 设置输入框中要上传的文件列表
1352
+ *
1353
+ * @param files - 文件列表
1354
+ *
1355
+ * @example
1356
+ * // 设置文件列表
1357
+ * chatRef.current?.setFiles([
1358
+ * {
1359
+ * fileId: 'file-001',
1360
+ * fileName: '财务报表.xlsx',
1361
+ * fileUrl: '/files/report.xlsx'
1362
+ * }
1363
+ * ])
1364
+ *
1365
+ * @example
1366
+ * // 清空文件列表
1367
+ * chatRef.current?.setFiles([])
1368
+ */
754
1369
  setFiles: (files?: InputFile[]) => void;
755
- /** 设置聊天参数 */
1370
+ /**
1371
+ * 设置全局参数
1372
+ * 更新聊天组件运行时需要的参数
1373
+ *
1374
+ * @param params - 聊天参数对象
1375
+ *
1376
+ * @example
1377
+ * // 设置业务参数
1378
+ * chatRef.current?.setParams({
1379
+ * businessId: 'case-001',
1380
+ * businessType: 2,
1381
+ * source: 1
1382
+ * })
1383
+ *
1384
+ * @example
1385
+ * // 设置扩展参数, 透传大模型
1386
+ * chatRef.current?.setParams({
1387
+ * params: {
1388
+ * textInfo: '税务局',
1389
+ * }
1390
+ * })
1391
+ */
756
1392
  setParams: (params?: ChatParams) => void;
757
- /** 设置服务配置 */
1393
+ /**
1394
+ * 设置服务配置
1395
+ * 动态更新聊天组件的服务配置
1396
+ *
1397
+ * @param services - 服务配置对象
1398
+ *
1399
+ * @example
1400
+ * // 更新HTTP配置,WebSocket继承
1401
+ * chatRef.current?.setServices({
1402
+ * http: {
1403
+ * config: {
1404
+ * baseURL: '/new-api',
1405
+ * headers: {
1406
+ * token: 'new-token'
1407
+ * }
1408
+ * }
1409
+ * }
1410
+ * })
1411
+ *
1412
+ * @example
1413
+ * // 更新WebSocket配置,http使用默认
1414
+ * chatRef.current?.setServices({
1415
+ * websocket: {
1416
+ * baseURLs: ['/api', '/api/backup'],
1417
+ * params: {
1418
+ * token: 'new-token'
1419
+ * }
1420
+ * }
1421
+ * })
1422
+ */
758
1423
  setServices: (services?: ChatServices) => void;
759
- /** 输入框聚焦 */
1424
+ /**
1425
+ * 输入框聚焦
1426
+ * 使聊天输入框获得焦点
1427
+ *
1428
+ * @example
1429
+ * // 聚焦输入框
1430
+ * chatRef.current?.senderFocus()
1431
+ */
760
1432
  senderFocus: ChatSenderHandle['focus'];
761
1433
  }
762
1434
  /**
@@ -774,15 +1446,76 @@ interface ChatProps {
774
1446
  hooks?: ChatHooks;
775
1447
  /** 布局配置 */
776
1448
  layout?: ChatLayout;
777
- /** 用户信息 */
1449
+ /** 用户信息, 如果会话成员类型可以区分当前用户则不需要传入此参数;否则必传id */
778
1450
  userInfo?: UserInfo;
779
1451
  /** 服务配置 */
780
1452
  services?: ChatServices;
781
1453
  }
1454
+
782
1455
  /**
783
1456
  * 聊天组件
784
1457
  * 使用 React.forwardRef 实现的聊天界面组件
785
1458
  * 集成了消息收发、会话管理、文件预览等功能
1459
+ *
1460
+ * @example
1461
+ * // 基本使用
1462
+ * <Chat
1463
+ * config={{
1464
+ * receiverType: 3,
1465
+ * receiverId: 'agent-001'
1466
+ * }}
1467
+ * params={{
1468
+ * businessId: 'case-001',
1469
+ * businessType: 2
1470
+ * }}
1471
+ * />
1472
+ *
1473
+ * @example
1474
+ * <Chat
1475
+ * config={{
1476
+ * receiverType: 3,
1477
+ * receiverId: 'agent-001'
1478
+ * }}
1479
+ * params={{
1480
+ * businessId: 'case-001',
1481
+ * businessType: 2
1482
+ * }}
1483
+ * // 钩子配置
1484
+ * hooks={{
1485
+ * onBeforeSend: (message, files) => {
1486
+ * if (!message?.trim()) {
1487
+ * message.error('请输入消息内容')
1488
+ * return false
1489
+ * }
1490
+ * return true
1491
+ * },
1492
+ * onAfterSend: () => {
1493
+ * console.log('消息发送完成')
1494
+ * }
1495
+ * }}
1496
+ * // 布局配置
1497
+ * layout={{
1498
+ * preview: true,
1499
+ * sender: {
1500
+ * props: {
1501
+ * placeholder: '请输入您的问题...'
1502
+ * }
1503
+ * }
1504
+ * }}
1505
+ * // 服务配置
1506
+ * services={{
1507
+ * http: {
1508
+ * config: {
1509
+ * baseURL: '/api',
1510
+ * timeout: 30000
1511
+ * }
1512
+ * },
1513
+ * websocket: {
1514
+ * baseURLs: ['/api', '/api/backup']
1515
+ * }
1516
+ * }}
1517
+ * />
1518
+ *
786
1519
  */
787
1520
  declare const _default$1: react.ForwardRefExoticComponent<ChatProps & react.RefAttributes<ChatHandle>>;
788
1521
 
@@ -801,4 +1534,4 @@ interface MessageRenderProps {
801
1534
  */
802
1535
  declare const _default: ({ message, placement }: MessageRenderProps) => react_jsx_runtime.JSX.Element;
803
1536
 
804
- export { type AgentInfo, _default$3 as Attachments, type AttachmentsProps, type ChatConfig, _default$1 as ChatCopilot, type ChatHandle, type ChatHooks, type ChatLayout, type ChatParams, type ChatProps, _default$2 as ChatSender, type ChatServices, type ConversationStrategy, type InputFile, _default as MessageRender, type MessageRenderProps, type ReferencesType, type SenderProps, type UserInfo };
1537
+ export { type AgentInfo, _default$3 as Attachments, type AttachmentsProps, type ChatConfig, _default$1 as ChatCopilot, type ChatHandle, type ChatHooks, type ChatLayout, type ChatParams, type ChatProps, _default$2 as ChatSender, type ChatServices, type ConversationStrategy, type InputFile, _default as MessageRender, type MessageRenderProps, type ReferencesType, type RequestInstance, type SenderProps, type UserInfo };