@vettvangur/ai-sdk 0.7.3 → 0.7.5

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.
@@ -136,6 +136,20 @@ function filledFormFields(form, answers) {
136
136
  }
137
137
  return filled;
138
138
  }
139
+ /**
140
+ * A rich message as the markdown an agent message renders with: each card's title and subtitle,
141
+ * and its link buttons as links. A webview button, such as an agent app's sign-in card, opens in a
142
+ * new tab, since the window has no webview; buttons that post back to the agent are left out.
143
+ */
144
+ function richMessageText(event) {
145
+ const parts = [];
146
+ for (const element of event.elements) {
147
+ if (element.title?.trim()) parts.push(`**${element.title.trim()}**`);
148
+ if (element.subtitle?.trim()) parts.push(element.subtitle.trim());
149
+ for (const button of element.buttons ?? []) if ((button.type === "url" || button.type === "open_external" || button.type === "webview") && button.text.trim()) parts.push(`[${button.text.trim()}](${button.value})`);
150
+ }
151
+ return parts.join("\n\n");
152
+ }
139
153
  function errorCode(caught) {
140
154
  return caught && typeof caught === "object" && "code" in caught ? String(caught.code) : null;
141
155
  }
@@ -162,8 +176,9 @@ function createLiveChatProvider(config) {
162
176
  for (const listener of agentListeners) listener({ name: user.name.trim() || null });
163
177
  }
164
178
  function receive(event) {
165
- if (!event || event.authorId === customerId || event.type !== "message" && event.type !== "system_message") return;
166
- const text = event.text.trim();
179
+ if (!event || event.authorId === customerId) return;
180
+ if (event.type !== "message" && event.type !== "system_message" && event.type !== "rich_message") return;
181
+ const text = event.type === "rich_message" ? richMessageText(event) : event.text.trim();
167
182
  if (!text) return;
168
183
  const system = event.type === "system_message";
169
184
  const item = {
@@ -286,14 +301,21 @@ function createLiveChatProvider(config) {
286
301
  thread: { events }
287
302
  };
288
303
  try {
289
- const started = await sdk.startChat({ chat });
304
+ const { chat: started } = await sdk.startChat({ chat });
290
305
  adopt(started.id, started.thread, started.users, false);
291
306
  } catch (caught) {
292
307
  if (errorCode(caught) !== "CHAT_LIMIT_REACHED") throw caught;
293
308
  const { chatsSummary } = await sdk.listChats({ limit: 1 });
294
309
  const earlier = chatsSummary[0];
295
310
  if (!earlier) throw caught;
296
- const resumed = await sdk.resumeChat({ chat: {
311
+ if (earlier.active) {
312
+ const open = await sdk.getChat({ chatId: earlier.id });
313
+ if (open.thread?.active) {
314
+ adopt(open.id, open.thread, open.users, false);
315
+ return;
316
+ }
317
+ }
318
+ const { chat: resumed } = await sdk.resumeChat({ chat: {
297
319
  id: earlier.id,
298
320
  ...chat
299
321
  } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vettvangur/ai-sdk",
3
- "version": "0.7.3",
3
+ "version": "0.7.5",
4
4
  "description": "Framework-agnostic AI chat client for server-rendered shells",
5
5
  "type": "module",
6
6
  "access": "public",