@blueking/chat-helper 0.0.12-beta.10 → 0.0.12-beta.12

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.
@@ -22,7 +22,7 @@
22
22
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23
23
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
24
  * IN THE SOFTWARE.
25
- */ import { transferAgentInfoApi2AgentInfo } from '../transform/agent.ts.js';
25
+ */ import { transferAgentInfoApi2AgentInfo, transferLlmListApi2LlmItems } from '../transform/agent.ts.js';
26
26
  /**
27
27
  * agent 相关 http 接口
28
28
  * @param fetchClient - 请求客户端
@@ -30,7 +30,24 @@
30
30
  */ export const useAgent = (fetchClient)=>{
31
31
  // 获取 agent 信息
32
32
  const getAgentInfo = (config)=>fetchClient.get('agent/info/', undefined, config).then(transferAgentInfoApi2AgentInfo);
33
+ /**
34
+ * 获取当前空间可用模型列表(公开 + 空间授权 + 用户权限交集)
35
+ * @param params 查询参数;未传 llm_type 时默认 chat.completion
36
+ */ const getLlms = (params, config)=>{
37
+ var _params_llm_type;
38
+ const query = {
39
+ llm_type: (_params_llm_type = params === null || params === void 0 ? void 0 : params.llm_type) !== null && _params_llm_type !== void 0 ? _params_llm_type : 'chat.completion'
40
+ };
41
+ if (params === null || params === void 0 ? void 0 : params.fuzzy) {
42
+ query.fuzzy = params.fuzzy;
43
+ }
44
+ if (params === null || params === void 0 ? void 0 : params.supports) {
45
+ query.supports = params.supports;
46
+ }
47
+ return fetchClient.get('llms/', query, config).then(transferLlmListApi2LlmItems);
48
+ };
33
49
  return {
34
- getAgentInfo
50
+ getAgentInfo,
51
+ getLlms
35
52
  };
36
53
  };
@@ -7,6 +7,7 @@ import type { FetchClient } from '../fetch';
7
7
  export declare const useModule: (fetchClient: FetchClient) => {
8
8
  agent: {
9
9
  getAgentInfo: (config?: import("../fetch").IRequestConfig) => Promise<import("../..").IAgentInfo>;
10
+ getLlms: (params?: import("../..").ILlmListQuery, config?: import("../fetch").IRequestConfig) => Promise<import("../..").ILlmItem[]>;
10
11
  };
11
12
  session: {
12
13
  clearSession: (sessionCode: string, config?: import("../fetch").IRequestConfig) => Promise<unknown>;
@@ -33,6 +34,7 @@ export declare const useModule: (fetchClient: FetchClient) => {
33
34
  uploadFile: (sessionCode: string, file: File, config?: import("../fetch").IRequestConfig) => Promise<{
34
35
  download_url?: string;
35
36
  }>;
37
+ getPvFileDownloadUrl: (sessionCode: string, path: string, options?: import("../..").GetPvFileDownloadUrlOptions, config?: import("../fetch").IRequestConfig) => Promise<import("../..").IPvFileDownloadUrlResult>;
36
38
  isResumeSession: (sessionCode: string, config?: import("../fetch").IRequestConfig) => Promise<boolean>;
37
39
  };
38
40
  message: {
@@ -50,7 +50,7 @@ function _object_spread(target) {
50
50
  }
51
51
  return target;
52
52
  }
53
- import { transferMessage2MessageApi, transferMessageApi2Message } from '../transform/message.ts.js';
53
+ import { transferMessage2MessageApi, transferMessageApi2Message, mergeArtifactsActivityIntoMessages } from '../transform/message.ts.js';
54
54
  /**
55
55
  * message 相关 http 接口
56
56
  * @param fetchClient - 请求客户端
@@ -60,7 +60,7 @@ import { transferMessage2MessageApi, transferMessageApi2Message } from '../trans
60
60
  const getMessages = (sessionCode, limit, config)=>fetchClient.get(`session_content/content/`, {
61
61
  session_code: sessionCode,
62
62
  limit
63
- }, config).then((res)=>res.map(transferMessageApi2Message));
63
+ }, config).then((res)=>mergeArtifactsActivityIntoMessages(res.map(transferMessageApi2Message)));
64
64
  // 新增会话内容
65
65
  const plusMessage = (data, config)=>fetchClient.post(`session_content/`, transferMessage2MessageApi(data), config).then(transferMessageApi2Message);
66
66
  // 修改会话内容
@@ -1,4 +1,4 @@
1
- import type { ISession, ISessionFeedback, ISessionListParams } from '../../session/type';
1
+ import type { GetPvFileDownloadUrlOptions, IPvFileDownloadUrlResult, ISession, ISessionFeedback, ISessionListParams } from '../../session/type';
2
2
  import type { FetchClient, IRequestConfig } from '../fetch';
3
3
  /**
4
4
  * session 相关 http 接口
@@ -30,5 +30,6 @@ export declare const useSession: (fetchClient: FetchClient) => {
30
30
  uploadFile: (sessionCode: string, file: File, config?: IRequestConfig) => Promise<{
31
31
  download_url?: string;
32
32
  }>;
33
+ getPvFileDownloadUrl: (sessionCode: string, path: string, options?: GetPvFileDownloadUrlOptions, config?: IRequestConfig) => Promise<IPvFileDownloadUrlResult>;
33
34
  isResumeSession: (sessionCode: string, config?: IRequestConfig) => Promise<boolean>;
34
35
  };
@@ -78,6 +78,31 @@ import { transferSession2SessionApi, transferSessionApi2Session, transferSession
78
78
  import { resolveRequestValue } from '../fetch/index.ts.js';
79
79
  /** 上传接口传输用文件名前缀,与原始展示名解耦 */ const UPLOAD_FILE_NAME_PREFIX = 'upload_file';
80
80
  /** 会话列表默认每页条数 */ const DEFAULT_PAGE_SIZE = 20;
81
+ const DEFAULT_PV_FILE_DOWNLOAD_EXPIRES_IN = 600;
82
+ const DEFAULT_PV_FILE_DOWNLOAD_TIMEOUT = 20000;
83
+ /**
84
+ * 将会话列表接口响应统一为分页结构。
85
+ * 兼容两类后台:
86
+ * 1. 分页:`{ page, num_pages, count, results }`
87
+ * 2. 未分页(旧接口):直接返回 `ISessionApi[]`
88
+ */ const normalizeSessionListResponse = (res)=>{
89
+ if (Array.isArray(res)) {
90
+ return {
91
+ page: 1,
92
+ numPages: 1,
93
+ count: res.length,
94
+ results: res.map(transferSessionApi2Session)
95
+ };
96
+ }
97
+ const results = Array.isArray(res === null || res === void 0 ? void 0 : res.results) ? res.results : [];
98
+ var _res_page, _res_num_pages, _res_count;
99
+ return {
100
+ page: (_res_page = res === null || res === void 0 ? void 0 : res.page) !== null && _res_page !== void 0 ? _res_page : 1,
101
+ numPages: (_res_num_pages = res === null || res === void 0 ? void 0 : res.num_pages) !== null && _res_num_pages !== void 0 ? _res_num_pages : 1,
102
+ count: (_res_count = res === null || res === void 0 ? void 0 : res.count) !== null && _res_count !== void 0 ? _res_count : results.length,
103
+ results: results.map(transferSessionApi2Session)
104
+ };
105
+ };
81
106
  /** 从原始文件名提取 ASCII 安全扩展名,用于保留文件类型 */ const getSafeFileExtension = (fileName)=>{
82
107
  const extension = fileName.split('.').pop();
83
108
  if (!extension || extension === fileName) {
@@ -104,18 +129,13 @@ import { resolveRequestValue } from '../fetch/index.ts.js';
104
129
  */ export const useSession = (fetchClient)=>{
105
130
  // 清除聊天上下文
106
131
  const clearSession = (sessionCode, config)=>fetchClient.post(`chat_completion/${sessionCode}/clear/`, undefined, config);
107
- // 获取会话列表(分页)
132
+ // 获取会话列表(分页;兼容未支持分页的后台返回数组)
108
133
  const getSessions = (params, config)=>{
109
134
  var _params_page, _params_page_size;
110
135
  return fetchClient.get(`session/`, {
111
136
  page: (_params_page = params === null || params === void 0 ? void 0 : params.page) !== null && _params_page !== void 0 ? _params_page : 1,
112
137
  page_size: (_params_page_size = params === null || params === void 0 ? void 0 : params.page_size) !== null && _params_page_size !== void 0 ? _params_page_size : DEFAULT_PAGE_SIZE
113
- }, config).then((res)=>({
114
- page: res.page,
115
- numPages: res.num_pages,
116
- count: res.count,
117
- results: res.results.map(transferSessionApi2Session)
118
- }));
138
+ }, config).then(normalizeSessionListResponse);
119
139
  };
120
140
  // 新增会话
121
141
  const plusSession = (data, config)=>fetchClient.post(`session/`, transferSession2SessionApi(data), config).then((res)=>transferSessionApi2Session(res));
@@ -149,6 +169,22 @@ import { resolveRequestValue } from '../fetch/index.ts.js';
149
169
  })
150
170
  })));
151
171
  };
172
+ /**
173
+ * 签发会话 PV 文件的临时 download_url / preview_url。
174
+ * path 为文件相对路径(artifacts 的 outputId);URLSearchParams 会自动编码,勿二次 encode。
175
+ */ const getPvFileDownloadUrl = (sessionCode, path, options, config)=>{
176
+ var _options_expiresIn;
177
+ const expiresIn = (_options_expiresIn = options === null || options === void 0 ? void 0 : options.expiresIn) !== null && _options_expiresIn !== void 0 ? _options_expiresIn : DEFAULT_PV_FILE_DOWNLOAD_EXPIRES_IN;
178
+ var _options_timeout;
179
+ const timeout = (_options_timeout = options === null || options === void 0 ? void 0 : options.timeout) !== null && _options_timeout !== void 0 ? _options_timeout : DEFAULT_PV_FILE_DOWNLOAD_TIMEOUT;
180
+ var _config_timeout;
181
+ return fetchClient.get(`session/${sessionCode}/pv_files/download_url/`, {
182
+ expires_in: expiresIn,
183
+ path
184
+ }, _object_spread_props(_object_spread({}, config), {
185
+ timeout: (_config_timeout = config === null || config === void 0 ? void 0 : config.timeout) !== null && _config_timeout !== void 0 ? _config_timeout : timeout
186
+ }));
187
+ };
152
188
  // 轮询接口,判断是否可以继续聊天
153
189
  const isResumeSession = (sessionCode, config)=>fetchClient.get(`session/${sessionCode}/is_resume/`, undefined, config);
154
190
  return {
@@ -163,6 +199,7 @@ import { resolveRequestValue } from '../fetch/index.ts.js';
163
199
  getSessionFeedbackReasons,
164
200
  renameSession,
165
201
  uploadFile,
202
+ getPvFileDownloadUrl,
166
203
  isResumeSession
167
204
  };
168
205
  };
@@ -1,7 +1,15 @@
1
- import type { IAgentInfo, IAgentInfoApi } from '../../agent/type';
1
+ import type { IAgentInfo, IAgentInfoApi, ILlmItem, ILlmItemApi } from '../../agent/type';
2
2
  /**
3
3
  * 将 API 返回的 agent 信息数据转换为前端使用的 agent 信息数据
4
4
  * @param data API 返回的 agent 信息数据
5
5
  * @returns 前端使用的 agent 信息数据
6
6
  */
7
7
  export declare const transferAgentInfoApi2AgentInfo: (data: IAgentInfoApi) => IAgentInfo;
8
+ /**
9
+ * 将 llms/ 接口单项规范化为前端 ILlmItem(补齐缺省字段,避免 UI 崩溃)
10
+ */
11
+ export declare const transferLlmApi2LlmItem: (data: ILlmItemApi) => ILlmItem;
12
+ /**
13
+ * 将 llms/ 列表响应规范化为 ILlmItem[]
14
+ */
15
+ export declare const transferLlmListApi2LlmItems: (data: ILlmItemApi[] | null | undefined) => ILlmItem[];
@@ -79,3 +79,30 @@
79
79
  resources: data === null || data === void 0 ? void 0 : data.resources
80
80
  };
81
81
  };
82
+ /**
83
+ * 将 llms/ 接口单项规范化为前端 ILlmItem(补齐缺省字段,避免 UI 崩溃)
84
+ */ export const transferLlmApi2LlmItem = (data)=>{
85
+ var _data_id, _data_llm_type, _data_icon, _data_description, _data_max_token_size, _data_property, _data_space_auth_mode, _data_user_auth_mode;
86
+ return {
87
+ id: (_data_id = data.id) !== null && _data_id !== void 0 ? _data_id : 0,
88
+ llm_code: data.llm_code,
89
+ llm_name: data.llm_name,
90
+ llm_type: (_data_llm_type = data.llm_type) !== null && _data_llm_type !== void 0 ? _data_llm_type : 'chat.completion',
91
+ icon: (_data_icon = data.icon) !== null && _data_icon !== void 0 ? _data_icon : '',
92
+ description: (_data_description = data.description) !== null && _data_description !== void 0 ? _data_description : '',
93
+ base_model: data.base_model,
94
+ max_token_size: (_data_max_token_size = data.max_token_size) !== null && _data_max_token_size !== void 0 ? _data_max_token_size : 0,
95
+ property: (_data_property = data.property) !== null && _data_property !== void 0 ? _data_property : {},
96
+ space_auth_mode: (_data_space_auth_mode = data.space_auth_mode) !== null && _data_space_auth_mode !== void 0 ? _data_space_auth_mode : '',
97
+ user_auth_mode: (_data_user_auth_mode = data.user_auth_mode) !== null && _data_user_auth_mode !== void 0 ? _data_user_auth_mode : '',
98
+ tag_names: data.tag_names
99
+ };
100
+ };
101
+ /**
102
+ * 将 llms/ 列表响应规范化为 ILlmItem[]
103
+ */ export const transferLlmListApi2LlmItems = (data)=>{
104
+ if (!Array.isArray(data)) {
105
+ return [];
106
+ }
107
+ return data.map(transferLlmApi2LlmItem);
108
+ };
@@ -11,3 +11,9 @@ export declare const transferMessageApi2Message: (data: IMessageApi) => IMessage
11
11
  * @returns API 使用的消息数据
12
12
  */
13
13
  export declare const transferMessage2MessageApi: (data: IMessage) => IMessageApi;
14
+ /**
15
+ * 将历史/快照中的 artifacts_generated activity 合并到前一条 Assistant 的 property.artifacts,
16
+ * 并过滤掉该 activity(UI 只从 Assistant.property.artifacts 渲染)。
17
+ * 实时 SSE 已写过 property.artifacts;历史回填依赖此归一化。
18
+ */
19
+ export declare const mergeArtifactsActivityIntoMessages: (messages: IMessage[]) => IMessage[];
@@ -113,6 +113,7 @@ import { ActivityType, MessageRole, MessageType } from '../../message/type.ts.js
113
113
  break;
114
114
  }
115
115
  case ActivityType.FlowAgent:
116
+ case ActivityType.ArtifactsGenerated:
116
117
  default:
117
118
  content = activityData.content;
118
119
  break;
@@ -120,6 +121,7 @@ import { ActivityType, MessageRole, MessageType } from '../../message/type.ts.js
120
121
  const result = _object_spread_props(_object_spread({}, baseMessage), {
121
122
  activityType: activityData.activity_type,
122
123
  content,
124
+ property: activityData.property,
123
125
  role: activityData.role
124
126
  });
125
127
  return result;
@@ -382,6 +384,7 @@ import { ActivityType, MessageRole, MessageType } from '../../message/type.ts.js
382
384
  break;
383
385
  }
384
386
  case ActivityType.FlowAgent:
387
+ case ActivityType.ArtifactsGenerated:
385
388
  default:
386
389
  content = activityData.content;
387
390
  break;
@@ -389,6 +392,7 @@ import { ActivityType, MessageRole, MessageType } from '../../message/type.ts.js
389
392
  const result = _object_spread_props(_object_spread({}, baseMessage), {
390
393
  activity_type: activityData.activityType,
391
394
  content,
395
+ property: activityData.property,
392
396
  role: activityData.role
393
397
  });
394
398
  return result;
@@ -613,3 +617,55 @@ import { ActivityType, MessageRole, MessageType } from '../../message/type.ts.js
613
617
  }
614
618
  }
615
619
  };
620
+ /**
621
+ * 将历史/快照中的 artifacts_generated activity 合并到前一条 Assistant 的 property.artifacts,
622
+ * 并过滤掉该 activity(UI 只从 Assistant.property.artifacts 渲染)。
623
+ * 实时 SSE 已写过 property.artifacts;历史回填依赖此归一化。
624
+ */ export const mergeArtifactsActivityIntoMessages = (messages)=>{
625
+ const result = [];
626
+ for (const message of messages){
627
+ var _activity_content, _activity_property;
628
+ if (message.role !== MessageRole.Activity) {
629
+ result.push(message);
630
+ continue;
631
+ }
632
+ const activity = message;
633
+ if (activity.activityType !== ActivityType.ArtifactsGenerated) {
634
+ result.push(message);
635
+ continue;
636
+ }
637
+ const artifacts = (_activity_content = activity.content) === null || _activity_content === void 0 ? void 0 : _activity_content.artifacts;
638
+ // status=empty 或无产物:丢弃 activity,不渲染
639
+ if (!(artifacts === null || artifacts === void 0 ? void 0 : artifacts.length)) {
640
+ continue;
641
+ }
642
+ const activityTurnId = (_activity_property = activity.property) === null || _activity_property === void 0 ? void 0 : _activity_property.turn_id;
643
+ let matchedByTurn = -1;
644
+ let nearestAssistant = -1;
645
+ for(let i = result.length - 1; i >= 0; i -= 1){
646
+ var _candidate_property;
647
+ if (result[i].role !== MessageRole.Assistant) {
648
+ continue;
649
+ }
650
+ const candidate = result[i];
651
+ if (nearestAssistant < 0) {
652
+ nearestAssistant = i;
653
+ }
654
+ if (activityTurnId && ((_candidate_property = candidate.property) === null || _candidate_property === void 0 ? void 0 : _candidate_property.turn_id) === activityTurnId) {
655
+ matchedByTurn = i;
656
+ break;
657
+ }
658
+ }
659
+ const targetIndex = matchedByTurn >= 0 ? matchedByTurn : nearestAssistant;
660
+ if (targetIndex >= 0) {
661
+ const assistant = result[targetIndex];
662
+ result[targetIndex] = _object_spread_props(_object_spread({}, assistant), {
663
+ property: _object_spread_props(_object_spread({}, assistant.property), {
664
+ artifacts
665
+ })
666
+ });
667
+ }
668
+ // 始终丢弃 artifacts_generated activity
669
+ }
670
+ return result;
671
+ };