@agentscope-ai/chat 1.1.54 → 1.1.55-beta.1774332575807

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.zh-CN.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # Alibaba Cloud Spark Chat
2
2
 
3
- **注意**:`@agentscope-ai/design` 和 `@agentscope-ai/chat` 仍在加速推进开源中,我们预计在 2025 年年底完成开源。目前可以通过 npm 使用我们的项目。
4
3
 
5
4
  [![npm version](https://img.shields.io/npm/v/@ali/agentscope-ai-chat.svg)](https://www.npmjs.com/package/@ali/agentscope-ai-chat)
6
5
  [![license](https://img.shields.io/npm/l/@ali/agentscope-ai-chat.svg)](https://github.com/your-repo/spark-chat/blob/main/LICENSE)
@@ -39,6 +39,10 @@ export default createGlobalStyle`
39
39
  );
40
40
  }
41
41
 
42
+ &-content {
43
+ line-height: 1;
44
+ }
45
+
42
46
  /* 加号图标 */
43
47
  &-icon {
44
48
  font-size: 20px;
@@ -3,6 +3,7 @@ import { IAgentScopeRuntimeWebUIOptions } from "@agentscope-ai/chat";
3
3
  import { createContext, useContextSelector } from 'use-context-selector';
4
4
  import { useMemo } from "react";
5
5
  import { ConfigProvider, generateTheme, generateThemeByToken } from '@agentscope-ai/design';
6
+ import { createDefaultSessionApi } from "./defaultSessionApi";
6
7
 
7
8
 
8
9
  const ChatAnywhereOptionsContext = createContext<IAgentScopeRuntimeWebUIOptions>(undefined);
@@ -22,17 +23,29 @@ export function ChatAnywhereOptionsContextProvider(props: { children: React.Reac
22
23
  const { children } = props;
23
24
  const responsive = useResponsive();
24
25
 
26
+ const defaultSessionApi = useMemo(() => {
27
+ const multiple = !!props.options.session?.multiple;
28
+ return createDefaultSessionApi(multiple);
29
+ }, [props.options.session?.multiple]);
30
+
25
31
  const options = useMemo(() => {
26
32
  const theme = props.options.theme || {};
33
+ const session = props.options.session || {};
34
+ const multiple = !!session.multiple;
27
35
 
28
36
  return {
29
37
  ...props.options,
38
+ session: {
39
+ ...session,
40
+ multiple,
41
+ api: session.api || defaultSessionApi,
42
+ },
30
43
  theme: {
31
44
  ...theme,
32
45
  narrowMode: !responsive.lg || theme.narrowMode,
33
46
  }
34
47
  };
35
- }, [props.options, responsive.lg]);
48
+ }, [props.options, responsive.lg, defaultSessionApi]);
36
49
 
37
50
  const themeToken = useMemo(() => {
38
51
  const colorPrimary = options.theme.colorPrimary;
@@ -0,0 +1,115 @@
1
+ import {
2
+ IAgentScopeRuntimeWebUISession,
3
+ IAgentScopeRuntimeWebUISessionAPI,
4
+ } from '@agentscope-ai/chat';
5
+
6
+ const STORAGE_KEY_MULTIPLE = 'agent-scope-runtime-webui-sessions';
7
+ const STORAGE_KEY_SINGLE = 'agent-scope-runtime-webui-session';
8
+
9
+ function canUseLocalStorage() {
10
+ return typeof window !== 'undefined' && !!window.localStorage;
11
+ }
12
+
13
+ function createSessionId() {
14
+ return `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
15
+ }
16
+
17
+ function normalizeSession(
18
+ session: Partial<IAgentScopeRuntimeWebUISession>,
19
+ fallbackId?: string,
20
+ ) {
21
+ return {
22
+ id: session.id || fallbackId || createSessionId(),
23
+ name: session.name || '',
24
+ messages: session.messages || [],
25
+ generating: session.generating,
26
+ } as IAgentScopeRuntimeWebUISession;
27
+ }
28
+
29
+ function createStorageSessionStore(multiple: boolean) {
30
+ const storageKey = multiple ? STORAGE_KEY_MULTIPLE : STORAGE_KEY_SINGLE;
31
+ let sessionList: IAgentScopeRuntimeWebUISession[] = [];
32
+
33
+ const persist = () => {
34
+ if (!canUseLocalStorage()) {
35
+ return;
36
+ }
37
+ localStorage.setItem(storageKey, JSON.stringify(sessionList));
38
+ };
39
+
40
+ const load = () => {
41
+ if (!canUseLocalStorage()) {
42
+ return;
43
+ }
44
+ const raw = localStorage.getItem(storageKey);
45
+ sessionList = raw ? JSON.parse(raw) : [];
46
+ };
47
+
48
+ return {
49
+ async getSessionList() {
50
+ load();
51
+ if (!multiple && sessionList.length > 1) {
52
+ sessionList = sessionList.slice(0, 1);
53
+ persist();
54
+ }
55
+ return [...sessionList];
56
+ },
57
+ async getSession(sessionId: string) {
58
+ const list = await this.getSessionList();
59
+ if (!multiple) {
60
+ return list[0];
61
+ }
62
+ return list.find((item) => item.id === sessionId);
63
+ },
64
+ async updateSession(session: Partial<IAgentScopeRuntimeWebUISession>) {
65
+ if (!session.id) {
66
+ return this.createSession(session);
67
+ }
68
+
69
+ const list = await this.getSessionList();
70
+ const index = list.findIndex((item) => item.id === session.id);
71
+
72
+ if (index > -1) {
73
+ list[index] = normalizeSession(
74
+ {
75
+ ...list[index],
76
+ ...session,
77
+ },
78
+ session.id,
79
+ );
80
+ } else {
81
+ list.unshift(normalizeSession(session));
82
+ }
83
+
84
+ sessionList = multiple ? list : list.slice(0, 1);
85
+ persist();
86
+ return [...sessionList];
87
+ },
88
+ async createSession(session: Partial<IAgentScopeRuntimeWebUISession>) {
89
+ const list = await this.getSessionList();
90
+ const created = normalizeSession(session);
91
+
92
+ if (multiple) {
93
+ sessionList = [created, ...list];
94
+ } else {
95
+ sessionList = [created];
96
+ }
97
+
98
+ persist();
99
+ return [...sessionList];
100
+ },
101
+ async removeSession(session: Partial<IAgentScopeRuntimeWebUISession>) {
102
+ const list = await this.getSessionList();
103
+ if (!session.id) {
104
+ return [...list];
105
+ }
106
+ sessionList = list.filter((item) => item.id !== session.id);
107
+ persist();
108
+ return [...sessionList];
109
+ },
110
+ } as IAgentScopeRuntimeWebUISessionAPI;
111
+ }
112
+
113
+ export function createDefaultSessionApi(multiple: boolean) {
114
+ return createStorageSessionStore(multiple);
115
+ }
@@ -312,7 +312,7 @@ export interface IAgentScopeRuntimeWebUISessionOptions {
312
312
  * @description 会话 API 接口
313
313
  * @descriptionEn Session API interface
314
314
  */
315
- api: IAgentScopeRuntimeWebUISessionAPI;
315
+ api?: IAgentScopeRuntimeWebUISessionAPI;
316
316
  }
317
317
 
318
318
  /**
@@ -1,10 +1,8 @@
1
1
  import { AgentScopeRuntimeWebUI, IAgentScopeRuntimeWebUIRef, ChatInput } from '@agentscope-ai/chat';
2
2
  import OptionsPanel from './OptionsPanel';
3
- import { useMemo, useRef, useState } from 'react';
4
- import sessionApi from './sessionApi';
3
+ import { useMemo, useRef } from 'react';
5
4
  import defaultConfig from './OptionsPanel/defaultConfig';
6
5
  import { useLocalStorageState } from 'ahooks';
7
- import Weather from './Weather';
8
6
  import { Flex } from 'antd';
9
7
  import MessageImport from './MessageImport';
10
8
 
@@ -39,7 +37,6 @@ export default function () {
39
37
  },
40
38
  session: {
41
39
  multiple: true,
42
- api: sessionApi,
43
40
  },
44
41
  sender: {
45
42
  ...optionsConfig.sender,
@@ -1,7 +1,7 @@
1
1
  var _templateObject;
2
2
  function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
3
3
  import { createGlobalStyle } from 'antd-style';
4
- export default createGlobalStyle(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n.", "-media-upload {\n width: fit-content;\n\n .", "-upload-drag {\n border: none;\n }\n .", "-upload-drag .", "-upload-btn {\n padding: 0;\n }\n\n /* \u5DE6\u4FA7\u7F29\u7565\u56FE\u533A\u57DF */\n &-thumbnail {\n position: relative;\n width: 100px;\n height: 64px;\n display: flex;\n align-items: center;\n justify-content: center;\n border-radius: ", "px;\n border: 1px solid ", ";\n background-color: ", ";\n overflow: hidden;\n cursor: pointer;\n\n /* \u6E10\u53D8\u906E\u7F69 */\n &-gradient {\n position: absolute;\n top: 0;\n left: 0;\n width: 100px;\n height: 42px;\n background: linear-gradient(\n 174.5deg,\n rgba(205, 208, 220, 0.2) 0%,\n rgba(205, 208, 220, 0) 100%\n );\n }\n\n /* \u52A0\u53F7\u56FE\u6807 */\n &-icon {\n font-size: 20px;\n color: ", ";\n }\n\n &-count {\n color: ", ";\n text-align: center;\n font-size: 12px;\n line-height: 20px;\n }\n }\n}\n"])), function (p) {
4
+ export default createGlobalStyle(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n.", "-media-upload {\n width: fit-content;\n\n .", "-upload-drag {\n border: none;\n }\n .", "-upload-drag .", "-upload-btn {\n padding: 0;\n }\n\n /* \u5DE6\u4FA7\u7F29\u7565\u56FE\u533A\u57DF */\n &-thumbnail {\n position: relative;\n width: 100px;\n height: 64px;\n display: flex;\n align-items: center;\n justify-content: center;\n border-radius: ", "px;\n border: 1px solid ", ";\n background-color: ", ";\n overflow: hidden;\n cursor: pointer;\n\n /* \u6E10\u53D8\u906E\u7F69 */\n &-gradient {\n position: absolute;\n top: 0;\n left: 0;\n width: 100px;\n height: 42px;\n background: linear-gradient(\n 174.5deg,\n rgba(205, 208, 220, 0.2) 0%,\n rgba(205, 208, 220, 0) 100%\n );\n }\n\n &-content {\n line-height: 1;\n }\n\n /* \u52A0\u53F7\u56FE\u6807 */\n &-icon {\n font-size: 20px;\n color: ", ";\n }\n\n &-count {\n color: ", ";\n text-align: center;\n font-size: 12px;\n line-height: 20px;\n }\n }\n}\n"])), function (p) {
5
5
  return p.theme.prefixCls;
6
6
  }, function (p) {
7
7
  return p.theme.prefixCls;
@@ -8,6 +8,7 @@ import { useResponsive } from "ahooks";
8
8
  import { createContext, useContextSelector } from 'use-context-selector';
9
9
  import { useMemo } from "react";
10
10
  import { ConfigProvider, generateTheme, generateThemeByToken } from '@agentscope-ai/design';
11
+ import { createDefaultSessionApi } from "./defaultSessionApi";
11
12
  import { jsx as _jsx } from "react/jsx-runtime";
12
13
  var ChatAnywhereOptionsContext = createContext(undefined);
13
14
  export function useChatAnywhereOptions(selector) {
@@ -20,16 +21,28 @@ export function useChatAnywhereOptions(selector) {
20
21
  }
21
22
  ;
22
23
  export function ChatAnywhereOptionsContextProvider(props) {
24
+ var _props$options$sessio2;
23
25
  var children = props.children;
24
26
  var responsive = useResponsive();
27
+ var defaultSessionApi = useMemo(function () {
28
+ var _props$options$sessio;
29
+ var multiple = !!((_props$options$sessio = props.options.session) !== null && _props$options$sessio !== void 0 && _props$options$sessio.multiple);
30
+ return createDefaultSessionApi(multiple);
31
+ }, [(_props$options$sessio2 = props.options.session) === null || _props$options$sessio2 === void 0 ? void 0 : _props$options$sessio2.multiple]);
25
32
  var options = useMemo(function () {
26
33
  var theme = props.options.theme || {};
34
+ var session = props.options.session || {};
35
+ var multiple = !!session.multiple;
27
36
  return _objectSpread(_objectSpread({}, props.options), {}, {
37
+ session: _objectSpread(_objectSpread({}, session), {}, {
38
+ multiple: multiple,
39
+ api: session.api || defaultSessionApi
40
+ }),
28
41
  theme: _objectSpread(_objectSpread({}, theme), {}, {
29
42
  narrowMode: !responsive.lg || theme.narrowMode
30
43
  })
31
44
  });
32
- }, [props.options, responsive.lg]);
45
+ }, [props.options, responsive.lg, defaultSessionApi]);
33
46
  var themeToken = useMemo(function () {
34
47
  var colorPrimary = options.theme.colorPrimary;
35
48
  var colorBgBase = options.theme.colorBgBase;
@@ -0,0 +1,2 @@
1
+ import { IAgentScopeRuntimeWebUISessionAPI } from "../../..";
2
+ export declare function createDefaultSessionApi(multiple: boolean): IAgentScopeRuntimeWebUISessionAPI;
@@ -0,0 +1,188 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
8
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
9
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
10
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
11
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
12
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
13
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
14
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
15
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
16
+ var STORAGE_KEY_MULTIPLE = 'agent-scope-runtime-webui-sessions';
17
+ var STORAGE_KEY_SINGLE = 'agent-scope-runtime-webui-session';
18
+ function canUseLocalStorage() {
19
+ return typeof window !== 'undefined' && !!window.localStorage;
20
+ }
21
+ function createSessionId() {
22
+ return "".concat(Date.now(), "-").concat(Math.random().toString(36).slice(2, 8));
23
+ }
24
+ function normalizeSession(session, fallbackId) {
25
+ return {
26
+ id: session.id || fallbackId || createSessionId(),
27
+ name: session.name || '',
28
+ messages: session.messages || [],
29
+ generating: session.generating
30
+ };
31
+ }
32
+ function createStorageSessionStore(multiple) {
33
+ var storageKey = multiple ? STORAGE_KEY_MULTIPLE : STORAGE_KEY_SINGLE;
34
+ var sessionList = [];
35
+ var persist = function persist() {
36
+ if (!canUseLocalStorage()) {
37
+ return;
38
+ }
39
+ localStorage.setItem(storageKey, JSON.stringify(sessionList));
40
+ };
41
+ var load = function load() {
42
+ if (!canUseLocalStorage()) {
43
+ return;
44
+ }
45
+ var raw = localStorage.getItem(storageKey);
46
+ sessionList = raw ? JSON.parse(raw) : [];
47
+ };
48
+ return {
49
+ getSessionList: function getSessionList() {
50
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
51
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
52
+ while (1) switch (_context.prev = _context.next) {
53
+ case 0:
54
+ load();
55
+ if (!multiple && sessionList.length > 1) {
56
+ sessionList = sessionList.slice(0, 1);
57
+ persist();
58
+ }
59
+ return _context.abrupt("return", _toConsumableArray(sessionList));
60
+ case 3:
61
+ case "end":
62
+ return _context.stop();
63
+ }
64
+ }, _callee);
65
+ }))();
66
+ },
67
+ getSession: function getSession(sessionId) {
68
+ var _this = this;
69
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
70
+ var list;
71
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
72
+ while (1) switch (_context2.prev = _context2.next) {
73
+ case 0:
74
+ _context2.next = 2;
75
+ return _this.getSessionList();
76
+ case 2:
77
+ list = _context2.sent;
78
+ if (multiple) {
79
+ _context2.next = 5;
80
+ break;
81
+ }
82
+ return _context2.abrupt("return", list[0]);
83
+ case 5:
84
+ return _context2.abrupt("return", list.find(function (item) {
85
+ return item.id === sessionId;
86
+ }));
87
+ case 6:
88
+ case "end":
89
+ return _context2.stop();
90
+ }
91
+ }, _callee2);
92
+ }))();
93
+ },
94
+ updateSession: function updateSession(session) {
95
+ var _this2 = this;
96
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
97
+ var list, index;
98
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
99
+ while (1) switch (_context3.prev = _context3.next) {
100
+ case 0:
101
+ if (session.id) {
102
+ _context3.next = 2;
103
+ break;
104
+ }
105
+ return _context3.abrupt("return", _this2.createSession(session));
106
+ case 2:
107
+ _context3.next = 4;
108
+ return _this2.getSessionList();
109
+ case 4:
110
+ list = _context3.sent;
111
+ index = list.findIndex(function (item) {
112
+ return item.id === session.id;
113
+ });
114
+ if (index > -1) {
115
+ list[index] = normalizeSession(_objectSpread(_objectSpread({}, list[index]), session), session.id);
116
+ } else {
117
+ list.unshift(normalizeSession(session));
118
+ }
119
+ sessionList = multiple ? list : list.slice(0, 1);
120
+ persist();
121
+ return _context3.abrupt("return", _toConsumableArray(sessionList));
122
+ case 10:
123
+ case "end":
124
+ return _context3.stop();
125
+ }
126
+ }, _callee3);
127
+ }))();
128
+ },
129
+ createSession: function createSession(session) {
130
+ var _this3 = this;
131
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
132
+ var list, created;
133
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
134
+ while (1) switch (_context4.prev = _context4.next) {
135
+ case 0:
136
+ _context4.next = 2;
137
+ return _this3.getSessionList();
138
+ case 2:
139
+ list = _context4.sent;
140
+ created = normalizeSession(session);
141
+ if (multiple) {
142
+ sessionList = [created].concat(_toConsumableArray(list));
143
+ } else {
144
+ sessionList = [created];
145
+ }
146
+ persist();
147
+ return _context4.abrupt("return", _toConsumableArray(sessionList));
148
+ case 7:
149
+ case "end":
150
+ return _context4.stop();
151
+ }
152
+ }, _callee4);
153
+ }))();
154
+ },
155
+ removeSession: function removeSession(session) {
156
+ var _this4 = this;
157
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
158
+ var list;
159
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
160
+ while (1) switch (_context5.prev = _context5.next) {
161
+ case 0:
162
+ _context5.next = 2;
163
+ return _this4.getSessionList();
164
+ case 2:
165
+ list = _context5.sent;
166
+ if (session.id) {
167
+ _context5.next = 5;
168
+ break;
169
+ }
170
+ return _context5.abrupt("return", _toConsumableArray(list));
171
+ case 5:
172
+ sessionList = list.filter(function (item) {
173
+ return item.id !== session.id;
174
+ });
175
+ persist();
176
+ return _context5.abrupt("return", _toConsumableArray(sessionList));
177
+ case 8:
178
+ case "end":
179
+ return _context5.stop();
180
+ }
181
+ }, _callee5);
182
+ }))();
183
+ }
184
+ };
185
+ }
186
+ export function createDefaultSessionApi(multiple) {
187
+ return createStorageSessionStore(multiple);
188
+ }
@@ -291,7 +291,7 @@ export interface IAgentScopeRuntimeWebUISessionOptions {
291
291
  * @description 会话 API 接口
292
292
  * @descriptionEn Session API interface
293
293
  */
294
- api: IAgentScopeRuntimeWebUISessionAPI;
294
+ api?: IAgentScopeRuntimeWebUISessionAPI;
295
295
  }
296
296
  /**
297
297
  * @description 自定义卡片组件配置
@@ -13,7 +13,6 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
13
  import { AgentScopeRuntimeWebUI, ChatInput } from "../..";
14
14
  import OptionsPanel from "./OptionsPanel";
15
15
  import { useMemo, useRef } from 'react';
16
- import sessionApi from "./sessionApi";
17
16
  import defaultConfig from "./OptionsPanel/defaultConfig";
18
17
  import { useLocalStorageState } from 'ahooks';
19
18
  import { Flex } from 'antd';
@@ -48,8 +47,7 @@ export default function () {
48
47
  // 'weather search mock': Weather,
49
48
  },
50
49
  session: {
51
- multiple: true,
52
- api: sessionApi
50
+ multiple: true
53
51
  },
54
52
  sender: _objectSpread(_objectSpread({}, optionsConfig.sender), {}, {
55
53
  beforeUI: /*#__PURE__*/_jsx(ChatInput.BeforeUIContainer, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentscope-ai/chat",
3
- "version": "1.1.54",
3
+ "version": "1.1.55-beta.1774332575807",
4
4
  "description": "a free and open-source chat framework for building excellent LLM-powered chat experiences",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": [