@blueking/ai-ui-sdk 0.0.7-beta.7 → 0.0.7-beta.8

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.
@@ -17,3 +17,10 @@ export declare function durationFormatter(val: number): string;
17
17
  */
18
18
  export declare function handleDownLoad(source: string, filename?: string): void;
19
19
  export declare const handleCopy: (text: string) => void;
20
+ /**
21
+ * 处理提示词模板,替换模板中的变量
22
+ * @param prompt 提示词模板
23
+ * @param selectedText 选中的文本
24
+ * @returns 处理后的提示词
25
+ */
26
+ export declare const processPromptTemplate: (prompt: string, selectedText: string) => string;
@@ -1,4 +1,4 @@
1
- import type { ISessionContent, ISession, ISessionPrompt, ChatCallbacks } from '../types/type.ts';
1
+ import type { ISessionContent, ISession, ISessionPrompt, ChatCallbacks, BasicChatContent, ShortcutChatContent } from '../types/type.ts';
2
2
  import { SessionContentRole, SessionContentStatus } from '../types/enum';
3
3
  type SessionContentsMap = {
4
4
  [key: string]: ISessionContent[];
@@ -6,7 +6,7 @@ type SessionContentsMap = {
6
6
  type SessionLoadingMap = {
7
7
  [key: string]: boolean;
8
8
  };
9
- export declare const useChat: <T extends ISession = ISession>({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError, }?: ChatCallbacks) => {
9
+ export declare const useChat: <T extends ISession = ISession>({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError, requestOptions, }?: ChatCallbacks) => {
10
10
  currentSession: import("vue").Ref<T | undefined, T | undefined>;
11
11
  sessionContents: import("vue").Ref<{
12
12
  id?: number | undefined;
@@ -21,6 +21,8 @@ export declare const useChat: <T extends ISession = ISession>({ handleStart, han
21
21
  rate?: number | undefined;
22
22
  status?: SessionContentStatus | undefined;
23
23
  sessionCode: string;
24
+ cite?: string | undefined;
25
+ time?: string | undefined;
24
26
  }[], ISessionContent[] | {
25
27
  id?: number | undefined;
26
28
  createdAt?: string | undefined;
@@ -34,16 +36,20 @@ export declare const useChat: <T extends ISession = ISession>({ handleStart, han
34
36
  rate?: number | undefined;
35
37
  status?: SessionContentStatus | undefined;
36
38
  sessionCode: string;
39
+ cite?: string | undefined;
40
+ time?: string | undefined;
37
41
  }[]>;
38
42
  sessionContentsMap: SessionContentsMap;
39
43
  sessionLoadingMap: import("vue").Ref<SessionLoadingMap, SessionLoadingMap>;
40
44
  prompts: import("vue").ComputedRef<ISessionPrompt[]>;
45
+ currentSessionLoading: import("vue").ComputedRef<boolean>;
41
46
  chat: ({ sessionCode, data, url, headers, }: {
42
47
  sessionCode: string;
43
48
  data?: Record<string, any>;
44
49
  url: string;
45
50
  headers?: Record<string, string>;
46
51
  }) => void;
52
+ sendChat: (content: BasicChatContent | ShortcutChatContent, callback?: () => void) => void;
47
53
  stopChat: (sessionCode: string) => void;
48
54
  plusSessionContent: (sessionCode: string, sessionContent: ISessionContent) => void;
49
55
  updateSessionContent: (sessionContent: ISessionContent) => void;
@@ -56,5 +62,11 @@ export declare const useChat: <T extends ISession = ISession>({ handleStart, han
56
62
  deleteSessionContents: (sessionCode: string, contentIds: number[]) => number[];
57
63
  handleStartChat: (sessionCode: string) => void | undefined;
58
64
  handleErrorChat: (sessionCode: string, message: string, code?: string) => void | undefined;
65
+ reGenerateChat: (chatIndex: number) => void;
66
+ reSendChat: (index: number, { message, cite }: {
67
+ message: string;
68
+ cite?: string;
69
+ }, callback?: () => void) => void;
70
+ deleteChat: (index: number) => void;
59
71
  };
60
72
  export {};
package/dist/main.js CHANGED
@@ -844,6 +844,14 @@ const handleCopy = (text)=>{
844
844
  });
845
845
  document.body.removeChild(textarea);
846
846
  };
847
+ /**
848
+ * 处理提示词模板,替换模板中的变量
849
+ * @param prompt 提示词模板
850
+ * @param selectedText 选中的文本
851
+ * @returns 处理后的提示词
852
+ */ const processPromptTemplate = (prompt, selectedText)=>{
853
+ return prompt.replace(/\{\{\s*SELECTED_TEXT\s*\}\}/g, selectedText || '');
854
+ };
847
855
 
848
856
  ;// ./src/hooks/use-click-proxy.ts
849
857
 
@@ -1434,8 +1442,9 @@ class ChatHelper {
1434
1442
 
1435
1443
 
1436
1444
 
1445
+
1437
1446
  // ai 聊天
1438
- const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError } = {})=>{
1447
+ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError, requestOptions } = {})=>{
1439
1448
  const startMessage = '内容正在生成中...';
1440
1449
  // 聊天上下文
1441
1450
  const currentSession = (0,external_vue_namespaceObject.ref)();
@@ -1477,6 +1486,12 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1477
1486
  }
1478
1487
  return calculatedSessionContents;
1479
1488
  });
1489
+ // 当前会话是否正在加载
1490
+ const currentSessionLoading = (0,external_vue_namespaceObject.computed)(()=>{
1491
+ var _currentSession_value;
1492
+ const sessionCode = (_currentSession_value = currentSession.value) === null || _currentSession_value === void 0 ? void 0 : _currentSession_value.sessionCode;
1493
+ return sessionCode ? sessionLoadingMap.value[sessionCode] : false;
1494
+ });
1480
1495
  // 计算当前的 prompt
1481
1496
  const prompts = (0,external_vue_namespaceObject.computed)(()=>{
1482
1497
  var _currentSession_value_roleInfo_content, _currentSession_value_roleInfo, _currentSession_value;
@@ -1738,16 +1753,90 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1738
1753
  // 调用cb
1739
1754
  return handleError === null || handleError === void 0 ? void 0 : handleError(sessionCode, sessionContent, code);
1740
1755
  }
1756
+ // 重新生成对话 仅支持重新生成 ai 的回复
1757
+ function reGenerateChat(chatIndex) {
1758
+ const chat = sessionContents.value[chatIndex];
1759
+ if (chat.role !== SessionContentRole.Ai) {
1760
+ return;
1761
+ }
1762
+ const message = sessionContents.value[chatIndex - 1].content;
1763
+ const cite = sessionContents.value[chatIndex - 1].cite;
1764
+ sessionContents.value.splice(chatIndex - 1, 2);
1765
+ sendChat({
1766
+ message,
1767
+ cite
1768
+ });
1769
+ }
1741
1770
  // 聊天
1742
1771
  function chat({ sessionCode, data, url, headers }) {
1743
1772
  // 发送请求
1744
1773
  chatHelper.stream({
1745
1774
  sessionCode,
1746
- url,
1775
+ url: url,
1747
1776
  data,
1748
- headers
1777
+ headers: headers || (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers)
1749
1778
  });
1750
1779
  }
1780
+ // 重新发送聊天, 需要将当前聊天内容以及下方的所有内容删除
1781
+ function reSendChat(index, { message, cite }, callback) {
1782
+ const chat = sessionContents.value[index];
1783
+ if (chat.role !== SessionContentRole.User) {
1784
+ return;
1785
+ }
1786
+ sessionContents.value.splice(index, sessionContents.value.length - index);
1787
+ sendChat({
1788
+ message,
1789
+ cite
1790
+ }, callback);
1791
+ }
1792
+ // 发送聊天
1793
+ function sendChat(content, callback) {
1794
+ var _currentSession_value, _currentSession_value1, _currentSession_value2;
1795
+ if (!((_currentSession_value = currentSession.value) === null || _currentSession_value === void 0 ? void 0 : _currentSession_value.sessionCode) || !(requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.url) || currentSessionLoading.value) {
1796
+ return;
1797
+ }
1798
+ const { message, cite, shortcut } = content;
1799
+ let input = '';
1800
+ if (shortcut) {
1801
+ input = processPromptTemplate(shortcut.prompt, cite);
1802
+ } else {
1803
+ input = cite ? `${message}: "${cite}"` : message;
1804
+ }
1805
+ sessionContents.value.push({
1806
+ sessionCode: (_currentSession_value1 = currentSession.value) === null || _currentSession_value1 === void 0 ? void 0 : _currentSession_value1.sessionCode,
1807
+ content: message,
1808
+ role: SessionContentRole.User,
1809
+ status: SessionContentStatus.Success,
1810
+ cite
1811
+ });
1812
+ // 发送请求
1813
+ chatHelper.stream({
1814
+ sessionCode: (_currentSession_value2 = currentSession.value) === null || _currentSession_value2 === void 0 ? void 0 : _currentSession_value2.sessionCode,
1815
+ url: requestOptions.url,
1816
+ headers: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers,
1817
+ data: {
1818
+ inputs: {
1819
+ chat_history: prompts.value.slice(0, prompts.value.length - 1),
1820
+ input
1821
+ }
1822
+ }
1823
+ });
1824
+ callback === null || callback === void 0 ? void 0 : callback();
1825
+ }
1826
+ /**
1827
+ * 删除聊天内容
1828
+ * @param index 要删除的聊天内容索引
1829
+ * @description
1830
+ * - 如果删除的是用户消息,则同时删除该消息及其对应的AI回复(共2条)
1831
+ * - 如果删除的是其他类型消息(如AI回复),则只删除该条消息
1832
+ */ function deleteChat(index) {
1833
+ const chat = sessionContents.value[index];
1834
+ if (chat.role === SessionContentRole.User) {
1835
+ sessionContents.value.splice(index, 2);
1836
+ return;
1837
+ }
1838
+ sessionContents.value.splice(index - 1, 2);
1839
+ }
1751
1840
  // 停止聊天
1752
1841
  function stopChat(sessionCode) {
1753
1842
  chatHelper.stop(sessionCode);
@@ -1758,7 +1847,9 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1758
1847
  sessionContentsMap,
1759
1848
  sessionLoadingMap,
1760
1849
  prompts,
1850
+ currentSessionLoading,
1761
1851
  chat,
1852
+ sendChat,
1762
1853
  stopChat,
1763
1854
  plusSessionContent,
1764
1855
  updateSessionContent,
@@ -1770,7 +1861,10 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1770
1861
  deleteSessionContent,
1771
1862
  deleteSessionContents,
1772
1863
  handleStartChat,
1773
- handleErrorChat
1864
+ handleErrorChat,
1865
+ reGenerateChat,
1866
+ reSendChat,
1867
+ deleteChat
1774
1868
  };
1775
1869
  };
1776
1870
 
@@ -34,6 +34,8 @@ export interface ISessionContent {
34
34
  rate?: number;
35
35
  status?: SessionContentStatus;
36
36
  sessionCode: string;
37
+ cite?: string;
38
+ time?: string;
37
39
  }
38
40
  export interface ISessionPrompt {
39
41
  content: string;
@@ -46,9 +48,33 @@ export interface ChatCallbacks {
46
48
  handleThink?: (sessionCode: string, sessionContent: ISessionContent) => void;
47
49
  handleEnd?: (sessionCode: string, sessionContent: ISessionContent) => void;
48
50
  handleError?: (sessionCode: string, sessionContent: ISessionContent, code: string | undefined) => void;
51
+ requestOptions?: {
52
+ url: string;
53
+ headers?: Record<string, string>;
54
+ };
49
55
  }
50
56
  export interface SummaryCallbacks {
51
57
  handleStart?: () => void;
52
58
  handleEnd?: (text: string) => void;
53
59
  handleError?: (message: string, code?: string) => void;
54
60
  }
61
+ export type BasicChatContent = {
62
+ message: string;
63
+ cite?: string;
64
+ shortcut?: never;
65
+ };
66
+ export type ShortcutChatContent = {
67
+ message: string;
68
+ cite: string;
69
+ shortcut: ShortCut;
70
+ };
71
+ export interface ShortCut {
72
+ label: string;
73
+ key: string;
74
+ prompt: string;
75
+ icon?: string;
76
+ }
77
+ export type ISendChat = {
78
+ content: BasicChatContent | ShortcutChatContent;
79
+ callback?: () => void;
80
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/ai-ui-sdk",
3
- "version": "0.0.7-beta.7",
3
+ "version": "0.0.7-beta.8",
4
4
  "description": "蓝鲸AI UI SDK",
5
5
  "module": "dist/main.js",
6
6
  "types": "dist/main.d.ts",