@ray-js/t-agent 0.2.0-beta-2 → 0.2.0-beta-4

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.
@@ -107,7 +107,7 @@ export default class ChatMessage {
107
107
  await this.agent.hooks.callHook('onMessagePersist:before', payload, this);
108
108
  await this.agent.hooks.callHook('onMessagePersist', payload, this);
109
109
  await this.agent.hooks.callHook('onMessagePersist:after', payload, this);
110
- logger.debug('%onMessagePersist', 'background: gray; color: white', payload, this);
110
+ logger.debug('%conMessagePersist', 'background: gray; color: white', payload, this);
111
111
  });
112
112
  }
113
113
  setTilesLocked(locked) {
@@ -12,6 +12,7 @@ export default class ChatSession {
12
12
  sessionId: string | null;
13
13
  readonly messages: Map<string, ChatMessage>;
14
14
  constructor(agent: ChatAgent);
15
+ initValue: (key: string, value: any) => void;
15
16
  set: (key: string, value: any) => Promise<void>;
16
17
  get: <T = any>(key: string, defaultValue?: T | undefined) => any;
17
18
  getData: () => Record<string, any>;
@@ -11,6 +11,9 @@ export default class ChatSession {
11
11
  _defineProperty(this, "isNewChat", true);
12
12
  _defineProperty(this, "sessionId", null);
13
13
  _defineProperty(this, "messages", new Map());
14
+ _defineProperty(this, "initValue", (key, value) => {
15
+ this.data.set(key, value);
16
+ });
14
17
  _defineProperty(this, "set", async (key, value) => {
15
18
  const oldValue = this.data.get(key);
16
19
  if (oldValue === value) {
@@ -0,0 +1,14 @@
1
+ import { InputBlock, StreamRequestInstance } from './types';
2
+ export default class StreamRequest implements StreamRequestInstance {
3
+ readonly blocks: InputBlock[];
4
+ private aborted;
5
+ constructor(blocks: InputBlock[]);
6
+ sent(): void;
7
+ parts(): void;
8
+ /**
9
+ * Cancel the request stream.
10
+ *
11
+ * @param reason
12
+ */
13
+ cancel(reason: any): Promise<void>;
14
+ }
@@ -0,0 +1,19 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ export default class StreamRequest {
3
+ constructor(blocks) {
4
+ _defineProperty(this, "aborted", false);
5
+ this.blocks = blocks;
6
+ }
7
+ sent() {}
8
+ parts() {}
9
+
10
+ /**
11
+ * Cancel the request stream.
12
+ *
13
+ * @param reason
14
+ */
15
+ cancel() {
16
+ this.aborted = true;
17
+ throw new Error('cancel() can only be called after parts()');
18
+ }
19
+ }
@@ -177,6 +177,9 @@ export declare enum FinishReason {
177
177
  ABORT = "abort",
178
178
  ERROR = "error"
179
179
  }
180
+ export type StreamRequestInstance = {
181
+ blocks: InputBlock[];
182
+ };
180
183
  export type StreamResponseInstance = {
181
184
  parts: () => AsyncIterable<MessagePart>;
182
185
  cancel: (reason: any) => Promise<void>;
@@ -5,13 +5,6 @@ export function withDebug(options) {
5
5
  throw new Error('Debug plugin already exists');
6
6
  }
7
7
  const start = () => {
8
- // debugs.push(
9
- // createDebugger(agent.hooks, { tag: 'ChatAgent', filter: options?.filter }),
10
- // createDebugger(agent.session.hooks, { tag: 'ChatSession', filter: options?.filter }),
11
- // )
12
- // for (const hook of agent.pluginHooks) {
13
- // debugs.push(createDebugger(hook, { tag: 'ChatPlugin', filter: options?.filter }))
14
- // }
15
8
  Logger.setLogLevel('debug');
16
9
  };
17
10
  if ((options === null || options === void 0 ? void 0 : options.autoStart) !== false) {
@@ -23,8 +23,26 @@ export interface UIEventMap {
23
23
  };
24
24
  [key: string]: any;
25
25
  }
26
+ export interface UIHooksContext<P = any, R = any> {
27
+ payload: P;
28
+ result: R | null;
29
+ }
30
+ export interface UIHooks {
31
+ onMessageFeedback: (context: UIHooksContext<{
32
+ messageId: string;
33
+ content?: string;
34
+ rate?: string;
35
+ [key: string]: any;
36
+ }, {
37
+ success: boolean;
38
+ }>) => void | Promise<void>;
39
+ [key: string]: (context: UIHooksContext) => void;
40
+ }
26
41
  export declare function withUI(): (agent: ChatAgent) => {
42
+ hooks: import("hookable").Hookable<UIHooks, string>;
27
43
  ui: {
44
+ callHook: <K extends keyof UIHooks>(hookName: K, payload: UIHooks[K] extends (context: UIHooksContext<infer P, any>) => void ? P : never) => Promise<UIHooks[K] extends (context: UIHooksContext<any, infer R>) => void ? R : null>;
45
+ hook: <K_1 extends keyof UIHooks>(hookName: K_1, fn: UIHooks[K_1]) => () => void;
28
46
  emitter: Emitter;
29
47
  emitEvent: <T extends keyof UIEventMap>(eventName: T, detail: UIEventMap[T]) => void;
30
48
  onEvent: <T_1 extends keyof UIEventMap>(eventName: T_1, handler: (detail: UIEventMap[T_1]) => void) => () => void;
@@ -1,7 +1,10 @@
1
1
  import "core-js/modules/esnext.iterator.constructor.js";
2
2
  import "core-js/modules/esnext.iterator.map.js";
3
+ import "core-js/modules/web.dom-collections.iterator.js";
3
4
  import { Emitter, EmitterEvent } from '../chat';
5
+ import { createHooks } from 'hookable';
4
6
  export function withUI() {
7
+ const hooks = createHooks();
5
8
  return agent => {
6
9
  if (agent.plugins.ui) {
7
10
  throw new Error('UI plugin already exists');
@@ -28,6 +31,12 @@ export function withUI() {
28
31
  };
29
32
  emitEvent('messageChange', detail);
30
33
  }, 'after');
34
+ agent.onAgentDispose(() => {
35
+ const types = Object.keys(emitter.listeners);
36
+ for (const type of types) {
37
+ delete emitter.listeners[type];
38
+ }
39
+ });
31
40
  agent.session.onChange((key, value, oldValue) => {
32
41
  emitEvent('sessionChange', {
33
42
  key,
@@ -36,7 +45,19 @@ export function withUI() {
36
45
  });
37
46
  });
38
47
  return {
48
+ hooks,
39
49
  ui: {
50
+ callHook: async (hookName, payload) => {
51
+ const context = {
52
+ payload,
53
+ result: null
54
+ };
55
+ await hooks.callHook(hookName, context);
56
+ return context.result;
57
+ },
58
+ hook: (hookName, fn) => {
59
+ return hooks.hook(hookName, fn);
60
+ },
40
61
  emitter,
41
62
  emitEvent,
42
63
  onEvent: (eventName, handler) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent",
3
- "version": "0.2.0-beta-2",
3
+ "version": "0.2.0-beta-4",
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": "0784a0f440b67cc1f766a440dbad5350f633dfb1"
29
+ "gitHead": "1728f8a1cac6b2ca53e506e06171808fbf38efdc"
30
30
  }