@player-ui/reference-assets-plugin 0.15.3-next.3 → 0.15.4--canary.881.37421

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.
@@ -4,10 +4,25 @@ import {
4
4
  ExtendedPlayerPlugin,
5
5
  NodeType,
6
6
  Player,
7
+ Node,
7
8
  } from "@player-ui/player";
8
9
  import { ExpressionPlugin } from "@player-ui/expression-plugin";
9
10
  import { send } from "./send";
10
11
 
12
+ const isInChatDemo = (node: Node.Node) => {
13
+ if (
14
+ node.parent?.parent?.type === NodeType.View &&
15
+ node.parent.parent.value.id === "chat-view"
16
+ ) {
17
+ return true;
18
+ }
19
+
20
+ return (
21
+ node.parent?.parent?.type === NodeType.Asset &&
22
+ node.parent.parent.value.id.startsWith("collection-async-chat-demo")
23
+ );
24
+ };
25
+
11
26
  const createContentFromMessage = (message: string, id: string): any => ({
12
27
  asset: {
13
28
  type: "chat-message",
@@ -22,6 +37,27 @@ const createContentFromMessage = (message: string, id: string): any => ({
22
37
  },
23
38
  });
24
39
 
40
+ /** This content will fail to display its label since it isn't a valid asset */
41
+ const createBrokenRenderContent = (id: string): any => ({
42
+ asset: {
43
+ id,
44
+ type: "input",
45
+ binding: "binding",
46
+ label: 100,
47
+ },
48
+ });
49
+
50
+ /** this content will fail to fetch from the data model since the binding is an object */
51
+ const createBrokenTransformContent = (id: string): any => ({
52
+ asset: {
53
+ id,
54
+ type: "input",
55
+ binding: {
56
+ prop: "value",
57
+ },
58
+ },
59
+ });
60
+
25
61
  export class ChatUiDemoPlugin implements ExtendedPlayerPlugin<[], [], [send]> {
26
62
  public readonly name = "chat-ui-demo-plugin";
27
63
 
@@ -41,10 +77,10 @@ export class ChatUiDemoPlugin implements ExtendedPlayerPlugin<[], [], [send]> {
41
77
  let allPromiseKeys: string[] = [];
42
78
  let counter = 0;
43
79
 
44
- const sendMessage: send = (
80
+ const sendMessage = (
45
81
  context: ExpressionContext,
46
- message: string,
47
82
  nodeId?: string,
83
+ getContent?: () => any,
48
84
  ): void => {
49
85
  if (nodeId && !(nodeId in deferredPromises)) {
50
86
  context.logger?.warn(
@@ -62,12 +98,8 @@ export class ChatUiDemoPlugin implements ExtendedPlayerPlugin<[], [], [send]> {
62
98
  const keys = nodeId ? [nodeId] : allPromiseKeys;
63
99
 
64
100
  for (const id of keys) {
65
- const content = createContentFromMessage(
66
- message,
67
- `chat-demo-${counter++}`,
68
- );
69
101
  const resolveFunction = deferredPromises[id];
70
- resolveFunction?.(content);
102
+ resolveFunction?.(getContent?.());
71
103
  delete deferredPromises[id];
72
104
  }
73
105
 
@@ -81,11 +113,7 @@ export class ChatUiDemoPlugin implements ExtendedPlayerPlugin<[], [], [send]> {
81
113
 
82
114
  asyncNodePlugin.hooks.onAsyncNode.tap(this.name, (node) => {
83
115
  // Ensure this is only used on the chat-ui.tsx mock to prevent the promise from setting up during tests.
84
- if (
85
- (node.parent?.parent?.type !== NodeType.Asset &&
86
- node.parent?.parent?.type !== NodeType.View) ||
87
- !node.parent.parent.value.id.startsWith("collection-async-chat-demo")
88
- ) {
116
+ if (!isInChatDemo(node)) {
89
117
  return Promise.resolve(undefined);
90
118
  }
91
119
 
@@ -95,6 +123,37 @@ export class ChatUiDemoPlugin implements ExtendedPlayerPlugin<[], [], [send]> {
95
123
  });
96
124
  });
97
125
 
126
+ const sendRealMessage: send = (
127
+ context: ExpressionContext,
128
+ message: string,
129
+ nodeId?: string,
130
+ ) => {
131
+ return sendMessage(context, nodeId, () =>
132
+ createContentFromMessage(message, `chat-demo-${counter++}`),
133
+ );
134
+ };
135
+
136
+ /** These expressions are used as examples in the storybook to allow broken content through and show the error recovery fallback pattern. */
137
+ const sendBrokenMessage: send = (
138
+ context: ExpressionContext,
139
+ _: string,
140
+ nodeId?: string,
141
+ ) => {
142
+ return sendMessage(context, nodeId, () =>
143
+ createBrokenRenderContent(`chat-demo-${counter++}`),
144
+ );
145
+ };
146
+
147
+ const sendBrokenTransformMessage: send = (
148
+ context: ExpressionContext,
149
+ _: string,
150
+ nodeId?: string,
151
+ ) => {
152
+ return sendMessage(context, nodeId, () =>
153
+ createBrokenTransformContent(`chat-demo-${counter++}`),
154
+ );
155
+ };
156
+
98
157
  // Reset at the start of a new view.
99
158
  player.hooks.view.tap(this.name, (_) => {
100
159
  deferredPromises = {};
@@ -104,7 +163,11 @@ export class ChatUiDemoPlugin implements ExtendedPlayerPlugin<[], [], [send]> {
104
163
 
105
164
  // Register 'send' expression
106
165
  const expressionPlugin = new ExpressionPlugin(
107
- new Map([["send", sendMessage]]),
166
+ new Map([
167
+ ["send", sendRealMessage],
168
+ ["sendBroken", sendBrokenMessage],
169
+ ["sendBrokenTransform", sendBrokenTransformMessage],
170
+ ]),
108
171
  );
109
172
  player.registerPlugin(expressionPlugin);
110
173
  }
@@ -0,0 +1,37 @@
1
+ import { AsyncNodePlugin } from "@player-ui/async-node-plugin";
2
+ import { Player, PlayerPlugin } from "@player-ui/player";
3
+
4
+ export class ErrorRecoveryPlugin implements PlayerPlugin {
5
+ readonly name = "ErrorRecoveryPlugin";
6
+ /** */
7
+ apply(player: Player): void {
8
+ player.applyTo<AsyncNodePlugin>(AsyncNodePlugin.Symbol, (plugin) => {
9
+ plugin.hooks.onAsyncNodeError.tap(this.name, (err, node) => {
10
+ const playerState = player.getState();
11
+ if (playerState.status !== "in-progress") {
12
+ return;
13
+ }
14
+
15
+ // Limit error recovery to chat-ui view example to avoid breaking tests.
16
+ const viewId = playerState.controllers.view.currentView?.initialView.id;
17
+ if (viewId !== "chat-view") {
18
+ return;
19
+ }
20
+
21
+ return {
22
+ asset: {
23
+ type: "chat-message",
24
+ id: `${node.id}-recovery`,
25
+ value: {
26
+ asset: {
27
+ id: `${node.id}-recovery-text`,
28
+ type: "text",
29
+ value: "Something went wrong, please try again.",
30
+ },
31
+ },
32
+ },
33
+ };
34
+ });
35
+ });
36
+ }
37
+ }
@@ -19,18 +19,21 @@ import type {
19
19
  TextAsset,
20
20
  } from "../assets";
21
21
 
22
- export class ReferenceAssetsTransformPlugin implements ExtendedPlayerPlugin<
23
- [
24
- ActionAsset,
25
- InputAsset,
26
- ImageAsset,
27
- TextAsset,
28
- CollectionAsset,
29
- ChoiceAsset,
30
- ChatMessageAsset,
31
- ],
32
- [InfoAsset]
33
- > {
22
+ export class ReferenceAssetsTransformPlugin
23
+ implements
24
+ ExtendedPlayerPlugin<
25
+ [
26
+ ActionAsset,
27
+ InputAsset,
28
+ ImageAsset,
29
+ TextAsset,
30
+ CollectionAsset,
31
+ ChoiceAsset,
32
+ ChatMessageAsset,
33
+ ],
34
+ [InfoAsset]
35
+ >
36
+ {
34
37
  name = "reference-assets-transforms";
35
38
 
36
39
  apply(player: Player): void {
@@ -0,0 +1,7 @@
1
+ import { Player, PlayerPlugin } from "@player-ui/player";
2
+ export declare class ErrorRecoveryPlugin implements PlayerPlugin {
3
+ readonly name = "ErrorRecoveryPlugin";
4
+ /** */
5
+ apply(player: Player): void;
6
+ }
7
+ //# sourceMappingURL=error-recovery-plugin.d.ts.map