@ray-js/t-agent 0.0.8 → 0.0.9-beta-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.
package/README-zh_CN.md CHANGED
@@ -1388,3 +1388,4 @@ const renderOptions = {
1388
1388
  | t-agent.input.voice.require-permission | MessageInput 切换语音输入 | 需要授权录音权限 |
1389
1389
  | t-agent.input.upload.failed | MessageInput 上传文件 | 文件上传失败 |
1390
1390
  | t-agent.message.feedback.success | BubbleTile 消息评价 | 反馈成功 |
1391
+ | t-agent-bubble-tile-aborted | BubbleTile 消息 | 用户中断 |
package/README.md CHANGED
@@ -1383,3 +1383,4 @@ Below are the built-in multilingual keys:
1383
1383
  | t-agent.input.voice.require-permission | MessageInput switch to voice input | Need to grant recording permission |
1384
1384
  | t-agent.input.upload.failed | MessageInput file upload | File upload failed |
1385
1385
  | t-agent.message.feedback.success | BubbleTile message feedback | Feedback successful |
1386
+ | t-agent-bubble-tile-aborted | BubbleTile message | User aborted |
@@ -213,8 +213,7 @@ export default class ChatAgent {
213
213
  await message.set({
214
214
  status: ChatMessageStatus.UPDATING
215
215
  }).bubble.setText(text).update();
216
- }
217
- if (part.type === 'attachment' && attachmentCompose) {
216
+ } else if (part.type === 'attachment' && attachmentCompose) {
218
217
  const list = (await attachmentCompose(message, part)) || [];
219
218
  for (const m of list) {
220
219
  m.set({
@@ -223,12 +222,23 @@ export default class ChatAgent {
223
222
  await m.show();
224
223
  messages.push(m);
225
224
  }
226
- }
227
- if (part.type === 'error') {
225
+ } else if (part.type === 'error') {
228
226
  if (!message.bubble.text) {
229
227
  message.bubble.setText(part.error.message);
230
228
  }
231
229
  await message.bubble.setStatus(part.level === 'error' ? BubbleTileStatus.ERROR : BubbleTileStatus.WARNING).setInfo(part.error.message).update();
230
+ } else if (part.type === 'abort') {
231
+ // 如果只有一条响应消息,则直接设置为中断状态
232
+ if (messages.length === 1) {
233
+ if (message.status === ChatMessageStatus.UPDATING) {
234
+ await message.bubble.setStatus(BubbleTileStatus.ABORTED).update();
235
+ } else {
236
+ await message.remove();
237
+ messages.pop();
238
+ }
239
+ }
240
+ // stop update
241
+ break;
232
242
  }
233
243
  }
234
244
  }
@@ -1,6 +1,8 @@
1
1
  import { MessagePart, StreamResponseInstance } from './types';
2
2
  export default class StreamResponse implements StreamResponseInstance {
3
3
  private partStream;
4
+ private reader;
5
+ private aborted;
4
6
  /**
5
7
  * Create a new StreamResponse instance.
6
8
  *
@@ -1,3 +1,4 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
1
2
  import _awaitAsyncGenerator from "@babel/runtime/helpers/esm/awaitAsyncGenerator";
2
3
  import _wrapAsyncGenerator from "@babel/runtime/helpers/esm/wrapAsyncGenerator";
3
4
  import "core-js/modules/es.symbol.async-iterator.js";
@@ -8,6 +9,8 @@ export default class StreamResponse {
8
9
  * @param partStream
9
10
  */
10
11
  constructor(partStream) {
12
+ _defineProperty(this, "reader", null);
13
+ _defineProperty(this, "aborted", false);
11
14
  this.partStream = partStream;
12
15
  }
13
16
 
@@ -15,16 +18,41 @@ export default class StreamResponse {
15
18
  * Get all parts of the response as an async iterator.
16
19
  */
17
20
  parts() {
21
+ if (this.reader) {
22
+ throw new Error('parts() can only be called once');
23
+ }
18
24
  const reader = this.partStream.getReader();
25
+ this.reader = reader;
26
+
27
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
28
+ const resp = this;
19
29
  return {
20
30
  [Symbol.asyncIterator]() {
21
31
  return _wrapAsyncGenerator(function* () {
22
32
  try {
23
33
  while (true) {
34
+ if (resp.aborted) {
35
+ yield {
36
+ type: 'abort',
37
+ level: 'warn',
38
+ error: new Error('User aborted'),
39
+ meta: {}
40
+ };
41
+ break;
42
+ }
24
43
  const {
25
44
  done,
26
45
  value
27
46
  } = yield _awaitAsyncGenerator(reader.read());
47
+ if (resp.aborted) {
48
+ yield {
49
+ type: 'abort',
50
+ level: 'warn',
51
+ error: new Error('User aborted'),
52
+ meta: {}
53
+ };
54
+ break;
55
+ }
28
56
  if (done) break;
29
57
  yield value;
30
58
  }
@@ -42,6 +70,10 @@ export default class StreamResponse {
42
70
  * @param reason
43
71
  */
44
72
  cancel(reason) {
45
- return this.partStream.cancel(reason);
73
+ this.aborted = true;
74
+ if (this.reader) {
75
+ return this.reader.cancel(reason);
76
+ }
77
+ throw new Error('cancel() can only be called after parts()');
46
78
  }
47
79
  }
@@ -97,7 +97,8 @@ export interface InputBlock {
97
97
  export declare enum BubbleTileStatus {
98
98
  NORMAL = "normal",
99
99
  WARNING = "warning",
100
- ERROR = "error"
100
+ ERROR = "error",
101
+ ABORTED = "aborted"
101
102
  }
102
103
  export interface AbortSignalObject {
103
104
  aborted: boolean;
@@ -143,7 +144,7 @@ export type ChatAgentPluginHandler = {
143
144
  export type ChatAgentPlugin<T extends ChatAgentPluginHandler = Record<string, any>> = (agent: ChatAgent) => T;
144
145
  export type AddHookCallback<H, T extends keyof H> = (fn: H[T], type?: 'before' | 'after' | undefined) => () => void;
145
146
  export interface ErrorPart {
146
- type: 'error';
147
+ type: 'error' | 'abort';
147
148
  level: 'error' | 'warn';
148
149
  error: Error;
149
150
  meta: Record<string, any>;
@@ -19,6 +19,7 @@ export let BubbleTileStatus = /*#__PURE__*/function (BubbleTileStatus) {
19
19
  BubbleTileStatus["NORMAL"] = "normal";
20
20
  BubbleTileStatus["WARNING"] = "warning";
21
21
  BubbleTileStatus["ERROR"] = "error";
22
+ BubbleTileStatus["ABORTED"] = "aborted";
22
23
  return BubbleTileStatus;
23
24
  }({});
24
25
  export let FinishReason = /*#__PURE__*/function (FinishReason) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent",
3
- "version": "0.0.8",
3
+ "version": "0.0.9-beta-1",
4
4
  "author": "Tuya.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -26,5 +26,5 @@
26
26
  "build": "ray build --type=component --output dist",
27
27
  "clean": "rimraf ./dist"
28
28
  },
29
- "gitHead": "686e9d17bd1a96074d3ac16ac2a3d73e1aa17f8f"
29
+ "gitHead": "df6f4f35caa6e62408450f14d9dcdd26f068283c"
30
30
  }