llmasaservice-ui 0.3.2 → 0.3.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.
package/dist/index.d.mts CHANGED
@@ -35,6 +35,12 @@ interface ChatPanelProps {
35
35
  };
36
36
  }) => void;
37
37
  promptTemplate?: string;
38
+ actions?: {
39
+ pattern: string;
40
+ type: string;
41
+ htmlAction: string;
42
+ callbackAction: (capture: string) => void;
43
+ }[];
38
44
  }
39
45
  interface ExtraProps extends React.HTMLAttributes<HTMLElement> {
40
46
  inline?: boolean;
package/dist/index.d.ts CHANGED
@@ -35,6 +35,12 @@ interface ChatPanelProps {
35
35
  };
36
36
  }) => void;
37
37
  promptTemplate?: string;
38
+ actions?: {
39
+ pattern: string;
40
+ type: string;
41
+ htmlAction: string;
42
+ callbackAction: (capture: string) => void;
43
+ }[];
38
44
  }
39
45
  interface ExtraProps extends React.HTMLAttributes<HTMLElement> {
40
46
  inline?: boolean;
package/dist/index.js CHANGED
@@ -92,7 +92,8 @@ var ChatPanel = ({
92
92
  prismStyle = theme === "light" ? import_material_light.default : import_material_dark.default,
93
93
  service = null,
94
94
  historyChangedCallback = null,
95
- promptTemplate = ""
95
+ promptTemplate = "",
96
+ actions = []
96
97
  }) => {
97
98
  const { send, response, idle, stop, lastCallId } = (0, import_llmasaservice_client.useLLM)({
98
99
  project_id,
@@ -111,9 +112,22 @@ var ChatPanel = ({
111
112
  (0, import_react.useEffect)(() => {
112
113
  if (response && response.length > 0) {
113
114
  setIsLoading(false);
115
+ let newResponse = response;
116
+ if (actions && actions.length > 0) {
117
+ actions.forEach((action) => {
118
+ const regex = new RegExp(action.pattern, "g");
119
+ newResponse = newResponse.replace(regex, (match, ...groups) => {
120
+ let html = action.htmlAction;
121
+ groups.slice(0, -2).forEach((group, index) => {
122
+ html = html.replace(`$${index + 1}`, group);
123
+ });
124
+ return html;
125
+ });
126
+ });
127
+ }
114
128
  setHistory((prevHistory) => {
115
129
  return __spreadProps(__spreadValues({}, prevHistory), {
116
- [lastPrompt != null ? lastPrompt : ""]: { content: response, callId: lastCallId }
130
+ [lastPrompt != null ? lastPrompt : ""]: { content: newResponse, callId: lastCallId }
117
131
  });
118
132
  });
119
133
  }
@@ -198,13 +212,15 @@ var ChatPanel = ({
198
212
  });
199
213
  });
200
214
  let promptToSend = nextPrompt;
201
- if (Object.keys(history).length === 0 && promptTemplate && promptTemplate !== "") {
202
- promptToSend = promptTemplate.replace("{{prompt}}", nextPrompt);
203
- for (let i = 0; i < data.length; i++) {
204
- promptToSend = promptToSend.replace(
205
- "{{" + ((_a = data[i]) == null ? void 0 : _a.key) + "}}",
206
- (_c = (_b = data[i]) == null ? void 0 : _b.data) != null ? _c : ""
207
- );
215
+ if (initialPrompt && initialPrompt !== "" && Object.keys(history).length === 1 || (!initialPrompt || initialPrompt === "") && Object.keys(history).length === 0) {
216
+ if (promptTemplate && promptTemplate !== "") {
217
+ promptToSend = promptTemplate.replace("{{prompt}}", nextPrompt);
218
+ for (let i = 0; i < data.length; i++) {
219
+ promptToSend = promptToSend.replace(
220
+ "{{" + ((_a = data[i]) == null ? void 0 : _a.key) + "}}",
221
+ (_c = (_b = data[i]) == null ? void 0 : _b.data) != null ? _c : ""
222
+ );
223
+ }
208
224
  }
209
225
  }
210
226
  const controller = new AbortController();
package/dist/index.mjs CHANGED
@@ -59,7 +59,8 @@ var ChatPanel = ({
59
59
  prismStyle = theme === "light" ? materialLight : materialDark,
60
60
  service = null,
61
61
  historyChangedCallback = null,
62
- promptTemplate = ""
62
+ promptTemplate = "",
63
+ actions = []
63
64
  }) => {
64
65
  const { send, response, idle, stop, lastCallId } = useLLM({
65
66
  project_id,
@@ -78,9 +79,22 @@ var ChatPanel = ({
78
79
  useEffect(() => {
79
80
  if (response && response.length > 0) {
80
81
  setIsLoading(false);
82
+ let newResponse = response;
83
+ if (actions && actions.length > 0) {
84
+ actions.forEach((action) => {
85
+ const regex = new RegExp(action.pattern, "g");
86
+ newResponse = newResponse.replace(regex, (match, ...groups) => {
87
+ let html = action.htmlAction;
88
+ groups.slice(0, -2).forEach((group, index) => {
89
+ html = html.replace(`$${index + 1}`, group);
90
+ });
91
+ return html;
92
+ });
93
+ });
94
+ }
81
95
  setHistory((prevHistory) => {
82
96
  return __spreadProps(__spreadValues({}, prevHistory), {
83
- [lastPrompt != null ? lastPrompt : ""]: { content: response, callId: lastCallId }
97
+ [lastPrompt != null ? lastPrompt : ""]: { content: newResponse, callId: lastCallId }
84
98
  });
85
99
  });
86
100
  }
@@ -165,13 +179,15 @@ var ChatPanel = ({
165
179
  });
166
180
  });
167
181
  let promptToSend = nextPrompt;
168
- if (Object.keys(history).length === 0 && promptTemplate && promptTemplate !== "") {
169
- promptToSend = promptTemplate.replace("{{prompt}}", nextPrompt);
170
- for (let i = 0; i < data.length; i++) {
171
- promptToSend = promptToSend.replace(
172
- "{{" + ((_a = data[i]) == null ? void 0 : _a.key) + "}}",
173
- (_c = (_b = data[i]) == null ? void 0 : _b.data) != null ? _c : ""
174
- );
182
+ if (initialPrompt && initialPrompt !== "" && Object.keys(history).length === 1 || (!initialPrompt || initialPrompt === "") && Object.keys(history).length === 0) {
183
+ if (promptTemplate && promptTemplate !== "") {
184
+ promptToSend = promptTemplate.replace("{{prompt}}", nextPrompt);
185
+ for (let i = 0; i < data.length; i++) {
186
+ promptToSend = promptToSend.replace(
187
+ "{{" + ((_a = data[i]) == null ? void 0 : _a.key) + "}}",
188
+ (_c = (_b = data[i]) == null ? void 0 : _b.data) != null ? _c : ""
189
+ );
190
+ }
175
191
  }
176
192
  }
177
193
  const controller = new AbortController();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llmasaservice-ui",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Prebuilt UI components for LLMAsAService.io",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/ChatPanel.tsx CHANGED
@@ -32,6 +32,12 @@ export interface ChatPanelProps {
32
32
  [key: string]: { content: string; callId: string };
33
33
  }) => void;
34
34
  promptTemplate?: string;
35
+ actions?: {
36
+ pattern: string;
37
+ type: string;
38
+ htmlAction: string;
39
+ callbackAction: (capture: string) => void;
40
+ }[];
35
41
  }
36
42
 
37
43
  interface ExtraProps extends React.HTMLAttributes<HTMLElement> {
@@ -60,6 +66,7 @@ const ChatPanel: React.FC<ChatPanelProps & ExtraProps> = ({
60
66
  service = null,
61
67
  historyChangedCallback = null,
62
68
  promptTemplate = "",
69
+ actions = [],
63
70
  }) => {
64
71
  const { send, response, idle, stop, lastCallId } = useLLM({
65
72
  project_id: project_id,
@@ -85,10 +92,26 @@ const ChatPanel: React.FC<ChatPanelProps & ExtraProps> = ({
85
92
  if (response && response.length > 0) {
86
93
  setIsLoading(false);
87
94
 
95
+ let newResponse = response;
96
+
97
+ // replace actions with links
98
+ if (actions && actions.length > 0) {
99
+ actions.forEach((action) => {
100
+ const regex = new RegExp(action.pattern, "g");
101
+ newResponse = newResponse.replace(regex, (match, ...groups) => {
102
+ let html = action.htmlAction;
103
+ groups.slice(0, -2).forEach((group, index) => {
104
+ html = html.replace(`$${index + 1}`, group);
105
+ });
106
+ return html;
107
+ });
108
+ });
109
+ }
110
+
88
111
  setHistory((prevHistory) => {
89
112
  return {
90
113
  ...prevHistory,
91
- [lastPrompt ?? ""]: { content: response, callId: lastCallId },
114
+ [lastPrompt ?? ""]: { content: newResponse, callId: lastCallId },
92
115
  };
93
116
  });
94
117
  }
@@ -193,17 +216,22 @@ const ChatPanel: React.FC<ChatPanelProps & ExtraProps> = ({
193
216
  let promptToSend = nextPrompt;
194
217
 
195
218
  // if this is the first user message, use the template. otherwise it is a follow-on question(s)
219
+
196
220
  if (
197
- Object.keys(history).length === 0 &&
198
- promptTemplate &&
199
- promptTemplate !== ""
221
+ (initialPrompt &&
222
+ initialPrompt !== "" &&
223
+ Object.keys(history).length === 1) ||
224
+ ((!initialPrompt || initialPrompt === "") &&
225
+ Object.keys(history).length === 0)
200
226
  ) {
201
- promptToSend = promptTemplate.replace("{{prompt}}", nextPrompt);
202
- for (let i = 0; i < data.length; i++) {
203
- promptToSend = promptToSend.replace(
204
- "{{" + data[i]?.key + "}}",
205
- data[i]?.data ?? ""
206
- );
227
+ if (promptTemplate && promptTemplate !== "") {
228
+ promptToSend = promptTemplate.replace("{{prompt}}", nextPrompt);
229
+ for (let i = 0; i < data.length; i++) {
230
+ promptToSend = promptToSend.replace(
231
+ "{{" + data[i]?.key + "}}",
232
+ data[i]?.data ?? ""
233
+ );
234
+ }
207
235
  }
208
236
  }
209
237