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

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
@@ -479,10 +479,10 @@ __webpack_require__.r(__webpack_exports__);
479
479
 
480
480
 
481
481
 
482
- var ___CSS_LOADER_URL_IMPORT_0___ = new URL(/* asset import */ __webpack_require__(705), __webpack_require__.b);
483
- var ___CSS_LOADER_URL_IMPORT_1___ = new URL(/* asset import */ __webpack_require__(106), __webpack_require__.b);
484
- var ___CSS_LOADER_URL_IMPORT_2___ = new URL(/* asset import */ __webpack_require__(802), __webpack_require__.b);
485
- var ___CSS_LOADER_URL_IMPORT_3___ = new URL(/* asset import */ __webpack_require__(912), __webpack_require__.b);
482
+ var ___CSS_LOADER_URL_IMPORT_0___ = /* asset import */ new __webpack_require__.U(__webpack_require__(705));
483
+ var ___CSS_LOADER_URL_IMPORT_1___ = /* asset import */ new __webpack_require__.U(__webpack_require__(106));
484
+ var ___CSS_LOADER_URL_IMPORT_2___ = /* asset import */ new __webpack_require__.U(__webpack_require__(802));
485
+ var ___CSS_LOADER_URL_IMPORT_3___ = /* asset import */ new __webpack_require__.U(__webpack_require__(912));
486
486
  var ___CSS_LOADER_EXPORT___ = _node_modules_css_loader_dist_runtime_api_js__WEBPACK_IMPORTED_MODULE_1___default()((_node_modules_css_loader_dist_runtime_noSourceMaps_js__WEBPACK_IMPORTED_MODULE_0___default()));
487
487
  var ___CSS_LOADER_URL_REPLACEMENT_0___ = _node_modules_css_loader_dist_runtime_getUrl_js__WEBPACK_IMPORTED_MODULE_2___default()(___CSS_LOADER_URL_IMPORT_0___, { hash: "#iconcool" });
488
488
  var ___CSS_LOADER_URL_REPLACEMENT_1___ = _node_modules_css_loader_dist_runtime_getUrl_js__WEBPACK_IMPORTED_MODULE_2___default()(___CSS_LOADER_URL_IMPORT_1___);
@@ -565,9 +565,6 @@ url(${___CSS_LOADER_URL_REPLACEMENT_3___}) format("embedded-opentype");
565
565
  /******/ return module.exports;
566
566
  /******/ }
567
567
  /******/
568
- /******/ // expose the modules object (__webpack_modules__)
569
- /******/ __webpack_require__.m = __webpack_modules__;
570
- /******/
571
568
  /************************************************************************/
572
569
  /******/ /* webpack/runtime/compat get default export */
573
570
  /******/ (() => {
@@ -609,33 +606,24 @@ url(${___CSS_LOADER_URL_REPLACEMENT_3___}) format("embedded-opentype");
609
606
  /******/ };
610
607
  /******/ })();
611
608
  /******/
612
- /******/ /* webpack/runtime/publicPath */
609
+ /******/ /* webpack/runtime/relative url */
613
610
  /******/ (() => {
614
- /******/ __webpack_require__.p = "/";
611
+ /******/ __webpack_require__.U = function RelativeURL(url) {
612
+ /******/ var realUrl = new URL(url, "x:/");
613
+ /******/ var values = {};
614
+ /******/ for (var key in realUrl) values[key] = realUrl[key];
615
+ /******/ values.href = url;
616
+ /******/ values.pathname = url.replace(/[?#].*/, "");
617
+ /******/ values.origin = values.protocol = "";
618
+ /******/ values.toString = values.toJSON = () => (url);
619
+ /******/ for (var key in values) Object.defineProperty(this, key, { enumerable: true, configurable: true, value: values[key] });
620
+ /******/ };
621
+ /******/ __webpack_require__.U.prototype = URL.prototype;
615
622
  /******/ })();
616
623
  /******/
617
- /******/ /* webpack/runtime/import chunk loading */
624
+ /******/ /* webpack/runtime/publicPath */
618
625
  /******/ (() => {
619
- /******/ __webpack_require__.b = new URL("./", import.meta.url);
620
- /******/
621
- /******/ // object to store loaded and loading chunks
622
- /******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched
623
- /******/ // [resolve, Promise] = chunk loading, 0 = chunk loaded
624
- /******/ var installedChunks = {
625
- /******/ 792: 0
626
- /******/ };
627
- /******/
628
- /******/ // no install chunk
629
- /******/
630
- /******/ // no chunk on demand loading
631
- /******/
632
- /******/ // no prefetching
633
- /******/
634
- /******/ // no preloaded
635
- /******/
636
- /******/ // no external install chunk
637
- /******/
638
- /******/ // no on chunks loaded
626
+ /******/ __webpack_require__.p = "/";
639
627
  /******/ })();
640
628
  /******/
641
629
  /************************************************************************/
@@ -844,6 +832,14 @@ const handleCopy = (text)=>{
844
832
  });
845
833
  document.body.removeChild(textarea);
846
834
  };
835
+ /**
836
+ * 处理提示词模板,替换模板中的变量
837
+ * @param prompt 提示词模板
838
+ * @param selectedText 选中的文本
839
+ * @returns 处理后的提示词
840
+ */ const processPromptTemplate = (prompt, selectedText)=>{
841
+ return prompt.replace(/\{\{\s*SELECTED_TEXT\s*\}\}/g, selectedText || '');
842
+ };
847
843
 
848
844
  ;// ./src/hooks/use-click-proxy.ts
849
845
 
@@ -1434,8 +1430,9 @@ class ChatHelper {
1434
1430
 
1435
1431
 
1436
1432
 
1433
+
1437
1434
  // ai 聊天
1438
- const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError } = {})=>{
1435
+ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError, requestOptions } = {})=>{
1439
1436
  const startMessage = '内容正在生成中...';
1440
1437
  // 聊天上下文
1441
1438
  const currentSession = (0,external_vue_namespaceObject.ref)();
@@ -1477,6 +1474,12 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1477
1474
  }
1478
1475
  return calculatedSessionContents;
1479
1476
  });
1477
+ // 当前会话是否正在加载
1478
+ const currentSessionLoading = (0,external_vue_namespaceObject.computed)(()=>{
1479
+ var _currentSession_value;
1480
+ const sessionCode = (_currentSession_value = currentSession.value) === null || _currentSession_value === void 0 ? void 0 : _currentSession_value.sessionCode;
1481
+ return sessionCode ? sessionLoadingMap.value[sessionCode] : false;
1482
+ });
1480
1483
  // 计算当前的 prompt
1481
1484
  const prompts = (0,external_vue_namespaceObject.computed)(()=>{
1482
1485
  var _currentSession_value_roleInfo_content, _currentSession_value_roleInfo, _currentSession_value;
@@ -1738,16 +1741,90 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1738
1741
  // 调用cb
1739
1742
  return handleError === null || handleError === void 0 ? void 0 : handleError(sessionCode, sessionContent, code);
1740
1743
  }
1744
+ // 重新生成对话 仅支持重新生成 ai 的回复
1745
+ function reGenerateChat(chatIndex) {
1746
+ const chat = sessionContents.value[chatIndex];
1747
+ if (chat.role !== SessionContentRole.Ai) {
1748
+ return;
1749
+ }
1750
+ const message = sessionContents.value[chatIndex - 1].content;
1751
+ const cite = sessionContents.value[chatIndex - 1].cite;
1752
+ sessionContents.value.splice(chatIndex - 1, 2);
1753
+ sendChat({
1754
+ message,
1755
+ cite
1756
+ });
1757
+ }
1741
1758
  // 聊天
1742
1759
  function chat({ sessionCode, data, url, headers }) {
1743
1760
  // 发送请求
1744
1761
  chatHelper.stream({
1745
1762
  sessionCode,
1746
- url,
1763
+ url: url,
1747
1764
  data,
1748
- headers
1765
+ headers: headers || (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers)
1749
1766
  });
1750
1767
  }
1768
+ // 重新发送聊天, 需要将当前聊天内容以及下方的所有内容删除
1769
+ function reSendChat(index, { message, cite }, callback) {
1770
+ const chat = sessionContents.value[index];
1771
+ if (chat.role !== SessionContentRole.User) {
1772
+ return;
1773
+ }
1774
+ sessionContents.value.splice(index, sessionContents.value.length - index);
1775
+ sendChat({
1776
+ message,
1777
+ cite
1778
+ }, callback);
1779
+ }
1780
+ // 发送聊天
1781
+ function sendChat(content, callback) {
1782
+ var _currentSession_value, _currentSession_value1, _currentSession_value2;
1783
+ 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) {
1784
+ return;
1785
+ }
1786
+ const { message, cite, shortcut } = content;
1787
+ let input = '';
1788
+ if (shortcut) {
1789
+ input = processPromptTemplate(shortcut.prompt, cite);
1790
+ } else {
1791
+ input = cite ? `${message}: "${cite}"` : message;
1792
+ }
1793
+ sessionContents.value.push({
1794
+ sessionCode: (_currentSession_value1 = currentSession.value) === null || _currentSession_value1 === void 0 ? void 0 : _currentSession_value1.sessionCode,
1795
+ content: message,
1796
+ role: SessionContentRole.User,
1797
+ status: SessionContentStatus.Success,
1798
+ cite
1799
+ });
1800
+ // 发送请求
1801
+ chatHelper.stream({
1802
+ sessionCode: (_currentSession_value2 = currentSession.value) === null || _currentSession_value2 === void 0 ? void 0 : _currentSession_value2.sessionCode,
1803
+ url: requestOptions.url,
1804
+ headers: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers,
1805
+ data: {
1806
+ inputs: {
1807
+ chat_history: prompts.value.slice(0, prompts.value.length - 1),
1808
+ input
1809
+ }
1810
+ }
1811
+ });
1812
+ callback === null || callback === void 0 ? void 0 : callback();
1813
+ }
1814
+ /**
1815
+ * 删除聊天内容
1816
+ * @param index 要删除的聊天内容索引
1817
+ * @description
1818
+ * - 如果删除的是用户消息,则同时删除该消息及其对应的AI回复(共2条)
1819
+ * - 如果删除的是其他类型消息(如AI回复),则只删除该条消息
1820
+ */ function deleteChat(index) {
1821
+ const chat = sessionContents.value[index];
1822
+ if (chat.role === SessionContentRole.User) {
1823
+ sessionContents.value.splice(index, 2);
1824
+ return;
1825
+ }
1826
+ sessionContents.value.splice(index - 1, 2);
1827
+ }
1751
1828
  // 停止聊天
1752
1829
  function stopChat(sessionCode) {
1753
1830
  chatHelper.stop(sessionCode);
@@ -1758,7 +1835,9 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1758
1835
  sessionContentsMap,
1759
1836
  sessionLoadingMap,
1760
1837
  prompts,
1838
+ currentSessionLoading,
1761
1839
  chat,
1840
+ sendChat,
1762
1841
  stopChat,
1763
1842
  plusSessionContent,
1764
1843
  updateSessionContent,
@@ -1770,7 +1849,10 @@ const useChat = ({ handleStart, handleText, handleReferenceDoc, handleThink, han
1770
1849
  deleteSessionContent,
1771
1850
  deleteSessionContents,
1772
1851
  handleStartChat,
1773
- handleErrorChat
1852
+ handleErrorChat,
1853
+ reGenerateChat,
1854
+ reSendChat,
1855
+ deleteChat
1774
1856
  };
1775
1857
  };
1776
1858
 
@@ -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.9",
4
4
  "description": "蓝鲸AI UI SDK",
5
5
  "module": "dist/main.js",
6
6
  "types": "dist/main.d.ts",