wtfai 1.6.9 → 1.7.1

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.
@@ -35,15 +35,15 @@ class IframeBridgeHost {
35
35
  const [targetMethod] = params;
36
36
  result = this.hasMethod(targetMethod);
37
37
  } else if ('saveData' === method) {
38
- const [key, value] = params;
38
+ const [key, value, threadId] = params;
39
39
  const safeKey = `iframe_data_${key}`;
40
40
  await this.session.setWorkflowVariables({
41
41
  [safeKey]: value
42
- });
42
+ }, threadId);
43
43
  result = true;
44
44
  } else if ('loadData' === method) {
45
- const [key] = params;
46
- const variables = await this.session.getWorkflowVariables();
45
+ const [key, threadId] = params;
46
+ const variables = await this.session.getWorkflowVariables(threadId);
47
47
  result = variables[`iframe_data_${key}`];
48
48
  } else if ('executeWorkflow' === method) {
49
49
  const [workflowId, input] = params;
@@ -62,20 +62,23 @@ class IframeBridgeHost {
62
62
  const [collection, dataList, options] = params;
63
63
  result = await this.session.createRecords(collection, dataList, options);
64
64
  } else if ('getRecord' === method) {
65
- const [collection, id] = params;
66
- result = await this.session.getRecord(collection, id);
65
+ const [collection, id, threadId] = params;
66
+ result = await this.session.getRecord(collection, id, threadId);
67
67
  } else if ('updateRecord' === method) {
68
- const [collection, id, data] = params;
69
- result = await this.session.updateRecord(collection, id, data);
68
+ const [collection, id, data, threadId] = params;
69
+ result = await this.session.updateRecord(collection, id, data, threadId);
70
70
  } else if ('deleteRecord' === method) {
71
- const [collection, id] = params;
72
- result = await this.session.deleteRecord(collection, id);
71
+ const [collection, id, threadId] = params;
72
+ result = await this.session.deleteRecord(collection, id, threadId);
73
73
  } else if ('listRecords' === method) {
74
74
  const [collection, options] = params;
75
75
  result = await this.session.listRecords(collection, options);
76
76
  } else if ('clearCollection' === method) {
77
- const [collection] = params;
78
- result = await this.session.clearCollection(collection);
77
+ const [collection, threadId] = params;
78
+ result = await this.session.clearCollection(collection, threadId);
79
+ } else if ('fetch' === method) {
80
+ const [url, options] = params;
81
+ result = await this.session.fetch(url, options);
79
82
  } else {
80
83
  const userMethods = this.session.getIframeMethods();
81
84
  const fn = userMethods[method];
@@ -107,7 +110,8 @@ class IframeBridgeHost {
107
110
  'updateRecord',
108
111
  'deleteRecord',
109
112
  'listRecords',
110
- 'clearCollection'
113
+ 'clearCollection',
114
+ 'fetch'
111
115
  ];
112
116
  if (builtIns.includes(methodName)) return true;
113
117
  const userMethods = this.session.getIframeMethods();
package/dist/session.d.ts CHANGED
@@ -65,12 +65,12 @@ export declare class WorkflowSession {
65
65
  * 恢复历史会话
66
66
  */
67
67
  restore(): Promise<void>;
68
- getWorkflowVariables(): Promise<Record<string, any>>;
68
+ getWorkflowVariables(threadId?: string): Promise<Record<string, any>>;
69
69
  /**
70
70
  * 更新会话状态(服务端变量)
71
71
  * @param values 需要更新的变量键值对
72
72
  */
73
- setWorkflowVariables(values: Record<string, unknown>): Promise<void>;
73
+ setWorkflowVariables(values: Record<string, unknown>, threadId?: string): Promise<void>;
74
74
  /**
75
75
  * 更新会话标题
76
76
  * @param title 新的标题
@@ -84,6 +84,7 @@ export declare class WorkflowSession {
84
84
  */
85
85
  createRecord(collection: string, data: Record<string, unknown>, options?: {
86
86
  entityId?: string;
87
+ threadId?: string;
87
88
  }): Promise<string>;
88
89
  /**
89
90
  * 批量创建数据记录
@@ -93,6 +94,7 @@ export declare class WorkflowSession {
93
94
  */
94
95
  createRecords(collection: string, dataList: Array<Record<string, unknown>>, options?: {
95
96
  entityId?: string;
97
+ threadId?: string;
96
98
  }): Promise<string[]>;
97
99
  /**
98
100
  * 获取数据记录
@@ -100,27 +102,27 @@ export declare class WorkflowSession {
100
102
  * @param id 记录 ID
101
103
  * @returns 记录数据,不存在时返回 null
102
104
  */
103
- getRecord(collection: string, id: string): Promise<Record<string, unknown> | null>;
105
+ getRecord(collection: string, id: string, threadId?: string): Promise<Record<string, unknown> | null>;
104
106
  /**
105
107
  * 更新数据记录
106
108
  * @param collection 集合名称
107
109
  * @param id 记录 ID
108
110
  * @param data 新的数据内容
109
111
  */
110
- updateRecord(collection: string, id: string, data: Record<string, unknown>): Promise<void>;
112
+ updateRecord(collection: string, id: string, data: Record<string, unknown>, threadId?: string): Promise<void>;
111
113
  /**
112
114
  * 删除数据记录
113
115
  * @param collection 集合名称
114
116
  * @param id 记录 ID
115
117
  */
116
- deleteRecord(collection: string, id: string): Promise<void>;
118
+ deleteRecord(collection: string, id: string, threadId?: string): Promise<void>;
117
119
  listRecords(collection: string, options?: ListRecordsOptions): Promise<ListRecordsResult>;
118
120
  /**
119
121
  * 清空集合中的所有数据
120
122
  * @param collection 集合名称
121
123
  * @returns 被删除的记录数量
122
124
  */
123
- clearCollection(collection: string): Promise<number>;
125
+ clearCollection(collection: string, threadId?: string): Promise<number>;
124
126
  /**
125
127
  * 执行指定的工作流(临时/嵌套执行),工作流需要使用消息生成组件来生成消息供收集
126
128
  * 不会影响当前会话的状态(messages 等)
@@ -129,6 +131,12 @@ export declare class WorkflowSession {
129
131
  * @returns 执行结果的所有消息
130
132
  */
131
133
  executeWorkflow(workflowId: string, input: SendInput): Promise<SimpleMessage[]>;
134
+ /**
135
+ * 通用的 fetch 方法,支持调用后端 API
136
+ * @param url 请求地址,如果是相对路径则会自动拼接 baseUrl
137
+ * @param options 请求配置
138
+ */
139
+ fetch(url: string, options?: RequestInit): Promise<any>;
132
140
  /**
133
141
  * 发送消息执行工作流
134
142
  */
package/dist/session.js CHANGED
@@ -100,19 +100,21 @@ class WorkflowSession {
100
100
  title: variables.sessionTitle
101
101
  });
102
102
  }
103
- async getWorkflowVariables() {
103
+ async getWorkflowVariables(threadId) {
104
104
  this.assertNotDisposed();
105
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
106
- const response = await fetch(`${this.baseUrl}/workflows/${this.workflowId}/state?threadId=${this.state.threadId}`, {
105
+ const finalThreadId = threadId || this.state.threadId;
106
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
107
+ const response = await fetch(`${this.baseUrl}/workflows/${this.workflowId}/state?threadId=${finalThreadId}`, {
107
108
  headers: this.headers
108
109
  });
109
110
  if (!response.ok) throw new Error('Failed to get workflow variables');
110
111
  const data = await response.json();
111
112
  return data.variables || {};
112
113
  }
113
- async setWorkflowVariables(values) {
114
+ async setWorkflowVariables(values, threadId) {
114
115
  this.assertNotDisposed();
115
- if (!this.state.threadId) throw new Error('No active session (missing valid threadId)');
116
+ const finalThreadId = threadId || this.state.threadId;
117
+ if (!finalThreadId) throw new Error('No active session (missing valid threadId)');
116
118
  const response = await fetch(`${this.baseUrl}/workflows/${this.workflowId}/variables`, {
117
119
  method: 'POST',
118
120
  headers: {
@@ -120,7 +122,7 @@ class WorkflowSession {
120
122
  ...this.headers
121
123
  },
122
124
  body: JSON.stringify({
123
- threadId: this.state.threadId,
125
+ threadId: finalThreadId,
124
126
  values
125
127
  })
126
128
  });
@@ -163,7 +165,8 @@ class WorkflowSession {
163
165
  }
164
166
  async createRecord(collection, data, options = {}) {
165
167
  this.assertNotDisposed();
166
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
168
+ const finalThreadId = options.threadId || this.state.threadId;
169
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
167
170
  const response = await fetch(`${this.baseUrl}/workflows/${this.workflowId}/data/${encodeURIComponent(collection)}`, {
168
171
  method: 'POST',
169
172
  headers: {
@@ -171,7 +174,7 @@ class WorkflowSession {
171
174
  ...this.headers
172
175
  },
173
176
  body: JSON.stringify({
174
- threadId: this.state.threadId,
177
+ threadId: finalThreadId,
175
178
  data,
176
179
  entityId: options.entityId
177
180
  })
@@ -182,7 +185,8 @@ class WorkflowSession {
182
185
  }
183
186
  async createRecords(collection, dataList, options = {}) {
184
187
  this.assertNotDisposed();
185
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
188
+ const finalThreadId = options.threadId || this.state.threadId;
189
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
186
190
  const response = await fetch(`${this.baseUrl}/workflows/${this.workflowId}/data/${encodeURIComponent(collection)}/batch`, {
187
191
  method: 'POST',
188
192
  headers: {
@@ -190,7 +194,7 @@ class WorkflowSession {
190
194
  ...this.headers
191
195
  },
192
196
  body: JSON.stringify({
193
- threadId: this.state.threadId,
197
+ threadId: finalThreadId,
194
198
  dataList,
195
199
  entityId: options.entityId
196
200
  })
@@ -199,11 +203,12 @@ class WorkflowSession {
199
203
  const result = await response.json();
200
204
  return result.ids;
201
205
  }
202
- async getRecord(collection, id) {
206
+ async getRecord(collection, id, threadId) {
203
207
  this.assertNotDisposed();
204
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
208
+ const finalThreadId = threadId || this.state.threadId;
209
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
205
210
  const url = new URL(`${this.baseUrl}/workflows/${this.workflowId}/data/${encodeURIComponent(collection)}/${encodeURIComponent(id)}`);
206
- url.searchParams.set('threadId', this.state.threadId);
211
+ url.searchParams.set('threadId', finalThreadId);
207
212
  const response = await fetch(url.toString(), {
208
213
  headers: this.headers
209
214
  });
@@ -212,9 +217,10 @@ class WorkflowSession {
212
217
  const result = await response.json();
213
218
  return result.data;
214
219
  }
215
- async updateRecord(collection, id, data) {
220
+ async updateRecord(collection, id, data, threadId) {
216
221
  this.assertNotDisposed();
217
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
222
+ const finalThreadId = threadId || this.state.threadId;
223
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
218
224
  const response = await fetch(`${this.baseUrl}/workflows/${this.workflowId}/data/${encodeURIComponent(collection)}/${encodeURIComponent(id)}`, {
219
225
  method: 'PUT',
220
226
  headers: {
@@ -222,17 +228,18 @@ class WorkflowSession {
222
228
  ...this.headers
223
229
  },
224
230
  body: JSON.stringify({
225
- threadId: this.state.threadId,
231
+ threadId: finalThreadId,
226
232
  data
227
233
  })
228
234
  });
229
235
  if (!response.ok) throw new Error('Failed to update record');
230
236
  }
231
- async deleteRecord(collection, id) {
237
+ async deleteRecord(collection, id, threadId) {
232
238
  this.assertNotDisposed();
233
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
239
+ const finalThreadId = threadId || this.state.threadId;
240
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
234
241
  const url = new URL(`${this.baseUrl}/workflows/${this.workflowId}/data/${encodeURIComponent(collection)}/${encodeURIComponent(id)}`);
235
- url.searchParams.set('threadId', this.state.threadId);
242
+ url.searchParams.set('threadId', finalThreadId);
236
243
  const response = await fetch(url.toString(), {
237
244
  method: 'DELETE',
238
245
  headers: this.headers
@@ -241,9 +248,10 @@ class WorkflowSession {
241
248
  }
242
249
  async listRecords(collection, options) {
243
250
  this.assertNotDisposed();
244
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
251
+ const finalThreadId = (null == options ? void 0 : options.threadId) || this.state.threadId;
252
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
245
253
  const url = new URL(`${this.baseUrl}/workflows/${this.workflowId}/data/${encodeURIComponent(collection)}`);
246
- url.searchParams.set('threadId', this.state.threadId);
254
+ url.searchParams.set('threadId', finalThreadId);
247
255
  Object.entries(options || {}).forEach(([key, value])=>{
248
256
  if (void 0 !== value) url.searchParams.set(key, String(value));
249
257
  });
@@ -253,11 +261,12 @@ class WorkflowSession {
253
261
  if (!response.ok) throw new Error('Failed to list records');
254
262
  return response.json();
255
263
  }
256
- async clearCollection(collection) {
264
+ async clearCollection(collection, threadId) {
257
265
  this.assertNotDisposed();
258
- if (!this.state.threadId) throw new Error('No active session (missing threadId)');
266
+ const finalThreadId = threadId || this.state.threadId;
267
+ if (!finalThreadId) throw new Error('No active session (missing threadId)');
259
268
  const url = new URL(`${this.baseUrl}/workflows/${this.workflowId}/data/${encodeURIComponent(collection)}`);
260
- url.searchParams.set('threadId', this.state.threadId);
269
+ url.searchParams.set('threadId', finalThreadId);
261
270
  const response = await fetch(url.toString(), {
262
271
  method: 'DELETE',
263
272
  headers: this.headers
@@ -346,6 +355,22 @@ class WorkflowSession {
346
355
  }, this.headers).catch(reject);
347
356
  });
348
357
  }
358
+ async fetch(url, options = {}) {
359
+ this.assertNotDisposed();
360
+ const fullUrl = url.startsWith('http') ? url : `${this.baseUrl}${url}`;
361
+ const response = await fetch(fullUrl, {
362
+ ...options,
363
+ headers: {
364
+ ...this.headers,
365
+ ...options.headers
366
+ }
367
+ });
368
+ if (!response.ok) {
369
+ const errorData = await response.json().catch(()=>({}));
370
+ throw new Error(errorData.message || errorData.error || `Fetch failed with status ${response.status}`);
371
+ }
372
+ return response.json();
373
+ }
349
374
  async send({ parts: inputParts, ...rest }) {
350
375
  this.assertNotDisposed();
351
376
  if (this.state.isExecuting) throw new Error('工作流正在执行中');
package/dist/types.d.ts CHANGED
@@ -201,6 +201,7 @@ export interface ListRecordsOptions {
201
201
  pageSize?: number;
202
202
  order?: 'ASC' | 'DESC';
203
203
  entityId?: string;
204
+ threadId?: string;
204
205
  }
205
206
  /**
206
207
  * 数据记录基类
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wtfai",
3
- "version": "1.6.9",
3
+ "version": "1.7.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {