@ray-js/t-agent-plugin-aistream 0.2.0-beta-1 → 0.2.0-beta-2

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.
@@ -0,0 +1,4 @@
1
+ type TryCatchResult<T> = [Error | null, T | null];
2
+ export declare function tryCatch<T>(fn: () => Promise<T>): Promise<TryCatchResult<T>>;
3
+ export declare function tryCatch<T>(fn: () => T): TryCatchResult<T>;
4
+ export {};
@@ -0,0 +1,16 @@
1
+ // 函数重载定义
2
+
3
+ // 函数实现
4
+ export function tryCatch(fn) {
5
+ try {
6
+ const result = fn();
7
+
8
+ // 判断是不是 Promise
9
+ if (result instanceof Promise) {
10
+ return result.then(res => [null, res]).catch(err => [err instanceof Error ? err : new Error(String(err)), null]);
11
+ }
12
+ return [null, result];
13
+ } catch (err) {
14
+ return [err instanceof Error ? err : new Error(String(err)), null];
15
+ }
16
+ }
@@ -5,6 +5,7 @@ import '../polyfill';
5
5
  import { ReadableStream } from 'web-streams-polyfill';
6
6
  import { AIStreamAttributePayloadType, AIStreamAttributeType, AIStreamChatSysWorkflow, ConnectState, FileFormat, ReceivedTextPacketEof, ReceivedTextPacketType, SessionState, StreamFlag } from '../AIStreamTypes';
7
7
  import { EmitterEvent, generateId, safeParseJSON, StreamResponse } from '@ray-js/t-agent';
8
+ import { tryCatch } from './misc';
8
9
  const mimeTypeToFormatMap = {
9
10
  'video/mp4': FileFormat.MP4,
10
11
  'text/json': FileFormat.JSON,
@@ -60,13 +61,19 @@ export function sendBlocksToAIStream(params) {
60
61
  }
61
62
  controller.enqueue(part);
62
63
  };
63
- event = await session.startEvent({
64
+ let error;
65
+ [error, event] = await tryCatch(() => session.startEvent({
64
66
  userData: [{
65
67
  type: AIStreamAttributeType.AI_CHAT,
66
68
  payloadType: AIStreamAttributePayloadType.STRING,
67
69
  value: JSON.stringify(attribute)
68
70
  }]
69
- });
71
+ }));
72
+ if (error) {
73
+ controller.error(error);
74
+ controller.close();
75
+ return;
76
+ }
70
77
  const meta = {
71
78
  sessionId: event.sessionId,
72
79
  eventId: event.eventId
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-plugin-aistream",
3
- "version": "0.2.0-beta-1",
3
+ "version": "0.2.0-beta-2",
4
4
  "author": "Tuya.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -35,5 +35,5 @@
35
35
  "devDependencies": {
36
36
  "@types/url-parse": "^1.4.11"
37
37
  },
38
- "gitHead": "98229f8a9cb090a6fa0d9b47fa70b1f44ec2b34a"
38
+ "gitHead": "0784a0f440b67cc1f766a440dbad5350f633dfb1"
39
39
  }