@ray-js/t-agent-ui-ray 0.0.9-beta-1 → 0.0.9-beta-3

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.
@@ -29,6 +29,26 @@ export default function ChatContainer(props) {
29
29
  }
30
30
  return agent;
31
31
  });
32
+ const uiAgent = useMemo(() => {
33
+ const session = {
34
+ get: agent.session.get,
35
+ set: agent.session.set,
36
+ sessionId: agent.session.sessionId,
37
+ getData: agent.session.getData
38
+ };
39
+ Object.defineProperty(session, 'sessionId', {
40
+ get() {
41
+ return agent.session.sessionId;
42
+ }
43
+ });
44
+ return {
45
+ session,
46
+ plugins: agent.plugins,
47
+ pushInputBlocks: agent.pushInputBlocks,
48
+ emitTileEvent: agent.emitTileEvent,
49
+ removeMessage: agent.removeMessage
50
+ };
51
+ }, [agent]);
32
52
  useEffect(() => {
33
53
  const {
34
54
  onEvent,
@@ -138,7 +158,7 @@ export default function ChatContainer(props) {
138
158
  't-agent-chat-container-keyboard-show': keyboardHeight > 0
139
159
  })
140
160
  }, /*#__PURE__*/React.createElement(ChatAgentContext.Provider, {
141
- value: agent
161
+ value: uiAgent
142
162
  }, /*#__PURE__*/React.createElement(RenderContext.Provider, {
143
163
  value: renderOptions
144
164
  }, /*#__PURE__*/React.createElement(MessageContext.Provider, {
@@ -1,7 +1,7 @@
1
1
  import './index.less';
2
2
  import { View } from '@ray-js/components';
3
- import React from 'react';
4
- import { useRenderOptions } from '../hooks';
3
+ import React, { useCallback, useEffect, useRef } from 'react';
4
+ import { useRenderOptions, useTileProps } from '../hooks';
5
5
  export default function CustomCardRender(_ref) {
6
6
  let {
7
7
  card
@@ -9,6 +9,23 @@ export default function CustomCardRender(_ref) {
9
9
  const {
10
10
  customCardMap
11
11
  } = useRenderOptions();
12
+ const {
13
+ emitEvent
14
+ } = useTileProps();
15
+ const ref = useRef(card);
16
+ useEffect(() => {
17
+ ref.current = card;
18
+ });
19
+ const setCardState = useCallback((state, options) => {
20
+ if (typeof state === 'function') {
21
+ state = state(ref.current.cardState);
22
+ }
23
+ emitEvent({
24
+ type: '@buildIn/setCardState',
25
+ state,
26
+ options: options || {}
27
+ });
28
+ }, [emitEvent]);
12
29
  const Card = customCardMap[card.cardCode];
13
30
  if (!Card) {
14
31
  return null;
@@ -16,6 +33,7 @@ export default function CustomCardRender(_ref) {
16
33
  return /*#__PURE__*/React.createElement(View, {
17
34
  className: "t-agent-custom-card-render"
18
35
  }, /*#__PURE__*/React.createElement(Card, {
19
- card: card
36
+ card: card,
37
+ setCardState: setCardState
20
38
  }));
21
39
  }
@@ -70,5 +70,7 @@ export default function MessageList(props) {
70
70
  isLatestMessage: index === 0,
71
71
  side: side
72
72
  });
73
+ }), /*#__PURE__*/React.createElement(View, {
74
+ className: "t-agent-message-list-padding-start"
73
75
  })));
74
76
  }
@@ -17,3 +17,5 @@
17
17
  .t-agent-message-list-padding {
18
18
  flex: 1
19
19
  }
20
+
21
+ .t-agent-message-list-padding-start {}
@@ -10,7 +10,7 @@ const TileRender = _ref => {
10
10
  side
11
11
  } = _ref;
12
12
  const agent = useChatAgent();
13
- const emitEvent = useCallback(async payload => agent.emitTileEvent(tile.id, payload), [tile.id]);
13
+ const emitEvent = useCallback(async payload => agent.emitTileEvent(tile.id, payload), [agent, tile.id]);
14
14
  const {
15
15
  renderTileAs
16
16
  } = useRenderOptions();
@@ -2,10 +2,15 @@
2
2
  import { ChatAgent, ChatMessageObject, UIPlugin } from '@ray-js/t-agent';
3
3
  import { AssistantPlugin } from '@ray-js/t-agent-plugin-assistant';
4
4
  import { RenderOptions, TileProps } from './types';
5
+ import { ChatSession } from '@ray-js/t-agent';
5
6
  export declare const MessageContext: import("react").Context<{
6
7
  messages: ChatMessageObject[];
7
8
  keyboardHeight: number;
8
9
  }>;
9
10
  export declare const RenderContext: import("react").Context<RenderOptions>;
10
- export declare const ChatAgentContext: import("react").Context<ChatAgent<UIPlugin & AssistantPlugin>>;
11
+ export type UIChatSession = Pick<ChatSession, 'get' | 'getData' | 'set' | 'sessionId'>;
12
+ export type UIChatAgent = Pick<ChatAgent<UIPlugin & AssistantPlugin>, 'pushInputBlocks' | 'plugins' | 'emitTileEvent' | 'removeMessage'> & {
13
+ session: UIChatSession;
14
+ };
15
+ export declare const ChatAgentContext: import("react").Context<UIChatAgent>;
11
16
  export declare const TilePropsContext: import("react").Context<TileProps<any, any>>;
package/dist/contexts.js CHANGED
@@ -9,7 +9,8 @@ export const RenderContext = /*#__PURE__*/createContext({
9
9
  renderCardAs: () => null,
10
10
  customBlockTypes: [],
11
11
  customCardMap: {},
12
- getStaticResourceBizType: () => ''
12
+ getStaticResourceBizType: () => '',
13
+ i18nTranslate: key => key
13
14
  });
14
15
  export const ChatAgentContext = /*#__PURE__*/createContext({});
15
16
  export const TilePropsContext = /*#__PURE__*/createContext({});
@@ -1,5 +1,5 @@
1
1
  import { TTTAction } from '@ray-js/t-agent-plugin-assistant';
2
- export declare const useChatAgent: () => import("@ray-js/t-agent").ChatAgent<import("@ray-js/t-agent").UIPlugin & import("@ray-js/t-agent-plugin-assistant").AssistantPlugin>;
2
+ export declare const useChatAgent: () => import("../contexts").UIChatAgent;
3
3
  export declare const useAgentMessage: () => {
4
4
  messages: import("@ray-js/t-agent").ChatMessageObject[];
5
5
  keyboardHeight: number;
@@ -85,6 +85,7 @@ const BubbleTile = props => {
85
85
  }
86
86
  return /*#__PURE__*/React.createElement(View, {
87
87
  className: "t-agent-bubble-tile t-agent-bubble-tile-".concat(side),
88
+ "data-testid": "t-agent-bubble-tile",
88
89
  onLongPress: () => {}
89
90
  }, side === 'end' && errorNode, /*#__PURE__*/React.createElement(View, {
90
91
  className: "t-agent-bubble-tile-bubble ".concat(loading ? 't-agent-bubble-tile-bubble-loading' : '')
@@ -14,7 +14,8 @@ const CardTile = props => {
14
14
  return null;
15
15
  }
16
16
  return /*#__PURE__*/React.createElement(View, {
17
- className: "t-agent-card-tile"
17
+ className: "t-agent-card-tile",
18
+ "data-testid": "t-agent-bubble-tile"
18
19
  }, node);
19
20
  };
20
21
  export default /*#__PURE__*/React.memo(CardTile);
package/dist/types.d.ts CHANGED
@@ -14,14 +14,20 @@ export interface MarkdownBlock {
14
14
  children: string;
15
15
  map: [number, number];
16
16
  }
17
+ export type ChatCardComponent<T = any, S = any> = React.ComponentType<{
18
+ card: ChatCardObject<T, S>;
19
+ setCardState: ChatCardComponentSetCardState<S>;
20
+ }>;
21
+ export interface ChatCardComponentSetCardStateOptions {
22
+ persist?: boolean;
23
+ }
24
+ export type ChatCardComponentSetCardState<S = any> = (state: S | ((prev: S | undefined) => S), options?: ChatCardComponentSetCardStateOptions) => void;
17
25
  export interface RenderOptions {
18
26
  renderTileAs: (props: TileProps) => React.ReactNode;
19
27
  renderCustomBlockAs: (block: MarkdownBlock) => React.ReactNode;
20
28
  renderCardAs: (card: ChatCardObject) => React.ReactNode;
21
29
  customBlockTypes: string[];
22
- customCardMap: Record<string, React.ComponentType<{
23
- card: ChatCardObject;
24
- }>>;
30
+ customCardMap: Record<string, ChatCardComponent>;
25
31
  getStaticResourceBizType: (src: string, scene?: string, data?: any) => string;
26
32
  i18nTranslate: (key: string) => string;
27
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-ui-ray",
3
- "version": "0.0.9-beta-1",
3
+ "version": "0.0.9-beta-3",
4
4
  "author": "Tuya.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -41,5 +41,5 @@
41
41
  "@types/echarts": "^4.9.22",
42
42
  "@types/markdown-it": "^14.1.1"
43
43
  },
44
- "gitHead": "df6f4f35caa6e62408450f14d9dcdd26f068283c"
44
+ "gitHead": "7330e1a07e32629668a6edf933d4682f286d957a"
45
45
  }