ai 3.1.7 → 3.1.9

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.
@@ -42,6 +42,38 @@ interface ToolResult<NAME extends string, ARGS, RESULT> {
42
42
  result: RESULT;
43
43
  }
44
44
 
45
+ type AssistantStatus = 'in_progress' | 'awaiting_message';
46
+ type UseAssistantOptions = {
47
+ /**
48
+ * The API endpoint that accepts a `{ threadId: string | null; message: string; }` object and returns an `AssistantResponse` stream.
49
+ * The threadId refers to an existing thread with messages (or is `null` to create a new thread).
50
+ * The message is the next message that should be appended to the thread and sent to the assistant.
51
+ */
52
+ api: string;
53
+ /**
54
+ * An optional string that represents the ID of an existing thread.
55
+ * If not provided, a new thread will be created.
56
+ */
57
+ threadId?: string;
58
+ /**
59
+ * An optional literal that sets the mode of credentials to be used on the request.
60
+ * Defaults to "same-origin".
61
+ */
62
+ credentials?: RequestCredentials;
63
+ /**
64
+ * An optional object of headers to be passed to the API endpoint.
65
+ */
66
+ headers?: Record<string, string> | Headers;
67
+ /**
68
+ * An optional, additional body object to be passed to the API endpoint.
69
+ */
70
+ body?: object;
71
+ /**
72
+ * An optional callback that will be called when the assistant encounters an error.
73
+ */
74
+ onError?: (error: Error) => void;
75
+ };
76
+
45
77
  interface FunctionCall {
46
78
  /**
47
79
  * The arguments to call the function with, as generated by the model in JSON
@@ -402,4 +434,51 @@ type UseCompletionHelpers = {
402
434
  };
403
435
  declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
404
436
 
405
- export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, UseCompletionOptions, useChat, useCompletion };
437
+ type UseAssistantHelpers = {
438
+ /**
439
+ * The current array of chat messages.
440
+ */
441
+ messages: Readable<Message[]>;
442
+ /**
443
+ * Update the message store with a new array of messages.
444
+ */
445
+ setMessages: (messages: Message[]) => void;
446
+ /**
447
+ * The current thread ID.
448
+ */
449
+ threadId: Readable<string | undefined>;
450
+ /**
451
+ * The current value of the input field.
452
+ */
453
+ input: Writable<string>;
454
+ /**
455
+ * Append a user message to the chat list. This triggers the API call to fetch
456
+ * the assistant's response.
457
+ * @param message The message to append
458
+ * @param requestOptions Additional options to pass to the API call
459
+ */
460
+ append: (message: Message | CreateMessage, requestOptions?: {
461
+ data?: Record<string, string>;
462
+ }) => Promise<void>;
463
+ /**
464
+ Abort the current request immediately, keep the generated tokens if any.
465
+ */
466
+ stop: () => void;
467
+ /**
468
+ * Form submission handler that automatically resets the input field and appends a user message.
469
+ */
470
+ submitMessage: (e: any, requestOptions?: {
471
+ data?: Record<string, string>;
472
+ }) => Promise<void>;
473
+ /**
474
+ * The current status of the assistant. This can be used to show a loading indicator.
475
+ */
476
+ status: Readable<AssistantStatus>;
477
+ /**
478
+ * The error thrown during the assistant message processing, if any.
479
+ */
480
+ error: Readable<undefined | Error>;
481
+ };
482
+ declare function useAssistant({ api, threadId: threadIdParam, credentials, headers, body, onError, }: UseAssistantOptions): UseAssistantHelpers;
483
+
484
+ export { CreateMessage, Message, UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, UseCompletionOptions, useAssistant, useChat, useCompletion };
@@ -42,6 +42,38 @@ interface ToolResult<NAME extends string, ARGS, RESULT> {
42
42
  result: RESULT;
43
43
  }
44
44
 
45
+ type AssistantStatus = 'in_progress' | 'awaiting_message';
46
+ type UseAssistantOptions = {
47
+ /**
48
+ * The API endpoint that accepts a `{ threadId: string | null; message: string; }` object and returns an `AssistantResponse` stream.
49
+ * The threadId refers to an existing thread with messages (or is `null` to create a new thread).
50
+ * The message is the next message that should be appended to the thread and sent to the assistant.
51
+ */
52
+ api: string;
53
+ /**
54
+ * An optional string that represents the ID of an existing thread.
55
+ * If not provided, a new thread will be created.
56
+ */
57
+ threadId?: string;
58
+ /**
59
+ * An optional literal that sets the mode of credentials to be used on the request.
60
+ * Defaults to "same-origin".
61
+ */
62
+ credentials?: RequestCredentials;
63
+ /**
64
+ * An optional object of headers to be passed to the API endpoint.
65
+ */
66
+ headers?: Record<string, string> | Headers;
67
+ /**
68
+ * An optional, additional body object to be passed to the API endpoint.
69
+ */
70
+ body?: object;
71
+ /**
72
+ * An optional callback that will be called when the assistant encounters an error.
73
+ */
74
+ onError?: (error: Error) => void;
75
+ };
76
+
45
77
  interface FunctionCall {
46
78
  /**
47
79
  * The arguments to call the function with, as generated by the model in JSON
@@ -402,4 +434,51 @@ type UseCompletionHelpers = {
402
434
  };
403
435
  declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
404
436
 
405
- export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, UseCompletionOptions, useChat, useCompletion };
437
+ type UseAssistantHelpers = {
438
+ /**
439
+ * The current array of chat messages.
440
+ */
441
+ messages: Readable<Message[]>;
442
+ /**
443
+ * Update the message store with a new array of messages.
444
+ */
445
+ setMessages: (messages: Message[]) => void;
446
+ /**
447
+ * The current thread ID.
448
+ */
449
+ threadId: Readable<string | undefined>;
450
+ /**
451
+ * The current value of the input field.
452
+ */
453
+ input: Writable<string>;
454
+ /**
455
+ * Append a user message to the chat list. This triggers the API call to fetch
456
+ * the assistant's response.
457
+ * @param message The message to append
458
+ * @param requestOptions Additional options to pass to the API call
459
+ */
460
+ append: (message: Message | CreateMessage, requestOptions?: {
461
+ data?: Record<string, string>;
462
+ }) => Promise<void>;
463
+ /**
464
+ Abort the current request immediately, keep the generated tokens if any.
465
+ */
466
+ stop: () => void;
467
+ /**
468
+ * Form submission handler that automatically resets the input field and appends a user message.
469
+ */
470
+ submitMessage: (e: any, requestOptions?: {
471
+ data?: Record<string, string>;
472
+ }) => Promise<void>;
473
+ /**
474
+ * The current status of the assistant. This can be used to show a loading indicator.
475
+ */
476
+ status: Readable<AssistantStatus>;
477
+ /**
478
+ * The error thrown during the assistant message processing, if any.
479
+ */
480
+ error: Readable<undefined | Error>;
481
+ };
482
+ declare function useAssistant({ api, threadId: threadIdParam, credentials, headers, body, onError, }: UseAssistantOptions): UseAssistantHelpers;
483
+
484
+ export { CreateMessage, Message, UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, UseCompletionOptions, useAssistant, useChat, useCompletion };
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // svelte/index.ts
21
21
  var svelte_exports = {};
22
22
  __export(svelte_exports, {
23
+ useAssistant: () => useAssistant,
23
24
  useChat: () => useChat,
24
25
  useCompletion: () => useCompletion
25
26
  });
@@ -1618,8 +1619,159 @@ function useCompletion({
1618
1619
  data: streamData
1619
1620
  };
1620
1621
  }
1622
+
1623
+ // svelte/use-assistant.ts
1624
+ var import_provider_utils = require("@ai-sdk/provider-utils");
1625
+ var import_store3 = require("svelte/store");
1626
+ var uniqueId3 = 0;
1627
+ var store3 = {};
1628
+ function useAssistant({
1629
+ api,
1630
+ threadId: threadIdParam,
1631
+ credentials,
1632
+ headers,
1633
+ body,
1634
+ onError
1635
+ }) {
1636
+ const threadIdStore = (0, import_store3.writable)(threadIdParam);
1637
+ const key = `${api}|${threadIdParam != null ? threadIdParam : `completion-${uniqueId3++}`}`;
1638
+ const messages = (0, import_store3.writable)(store3[key] || []);
1639
+ const input = (0, import_store3.writable)("");
1640
+ const status = (0, import_store3.writable)("awaiting_message");
1641
+ const error = (0, import_store3.writable)(void 0);
1642
+ let abortController = null;
1643
+ const mutateMessages = (newMessages) => {
1644
+ store3[key] = newMessages;
1645
+ messages.set(newMessages);
1646
+ };
1647
+ async function append(message, requestOptions) {
1648
+ var _a, _b, _c, _d;
1649
+ status.set("in_progress");
1650
+ abortController = new AbortController();
1651
+ mutateMessages([
1652
+ ...(0, import_store3.get)(messages),
1653
+ { ...message, id: (_a = message.id) != null ? _a : generateId() }
1654
+ ]);
1655
+ input.set("");
1656
+ try {
1657
+ const result = await fetch(api, {
1658
+ method: "POST",
1659
+ credentials,
1660
+ signal: abortController.signal,
1661
+ headers: { "Content-Type": "application/json", ...headers },
1662
+ body: JSON.stringify({
1663
+ ...body,
1664
+ // always use user-provided threadId when available:
1665
+ threadId: (_b = threadIdParam != null ? threadIdParam : (0, import_store3.get)(threadIdStore)) != null ? _b : null,
1666
+ message: message.content,
1667
+ // optional request data:
1668
+ data: requestOptions == null ? void 0 : requestOptions.data
1669
+ })
1670
+ });
1671
+ if (result.body == null) {
1672
+ throw new Error("The response body is empty.");
1673
+ }
1674
+ for await (const { type, value } of readDataStream(
1675
+ result.body.getReader()
1676
+ )) {
1677
+ switch (type) {
1678
+ case "assistant_message": {
1679
+ mutateMessages([
1680
+ ...(0, import_store3.get)(messages),
1681
+ {
1682
+ id: value.id,
1683
+ role: value.role,
1684
+ content: value.content[0].text.value
1685
+ }
1686
+ ]);
1687
+ break;
1688
+ }
1689
+ case "text": {
1690
+ mutateMessages(
1691
+ (0, import_store3.get)(messages).map((msg, index, array) => {
1692
+ if (index === array.length - 1) {
1693
+ return { ...msg, content: msg.content + value };
1694
+ }
1695
+ return msg;
1696
+ })
1697
+ );
1698
+ break;
1699
+ }
1700
+ case "data_message": {
1701
+ mutateMessages([
1702
+ ...(0, import_store3.get)(messages),
1703
+ {
1704
+ id: (_c = value.id) != null ? _c : generateId(),
1705
+ role: "data",
1706
+ content: "",
1707
+ data: value.data
1708
+ }
1709
+ ]);
1710
+ break;
1711
+ }
1712
+ case "assistant_control_data": {
1713
+ threadIdStore.set(value.threadId);
1714
+ mutateMessages(
1715
+ (0, import_store3.get)(messages).map((msg, index, array) => {
1716
+ if (index === array.length - 1) {
1717
+ return { ...msg, id: value.messageId };
1718
+ }
1719
+ return msg;
1720
+ })
1721
+ );
1722
+ break;
1723
+ }
1724
+ case "error": {
1725
+ error.set(new Error(value));
1726
+ break;
1727
+ }
1728
+ }
1729
+ }
1730
+ } catch (err) {
1731
+ if ((0, import_provider_utils.isAbortError)(error) && ((_d = abortController == null ? void 0 : abortController.signal) == null ? void 0 : _d.aborted)) {
1732
+ abortController = null;
1733
+ return;
1734
+ }
1735
+ if (onError && err instanceof Error) {
1736
+ onError(err);
1737
+ }
1738
+ error.set(err);
1739
+ } finally {
1740
+ abortController = null;
1741
+ status.set("awaiting_message");
1742
+ }
1743
+ }
1744
+ function setMessages(messages2) {
1745
+ mutateMessages(messages2);
1746
+ }
1747
+ function stop() {
1748
+ if (abortController) {
1749
+ abortController.abort();
1750
+ abortController = null;
1751
+ }
1752
+ }
1753
+ async function submitMessage(e, requestOptions) {
1754
+ e.preventDefault();
1755
+ const inputValue = (0, import_store3.get)(input);
1756
+ if (!inputValue)
1757
+ return;
1758
+ await append({ role: "user", content: inputValue }, requestOptions);
1759
+ }
1760
+ return {
1761
+ messages,
1762
+ error,
1763
+ threadId: threadIdStore,
1764
+ input,
1765
+ append,
1766
+ submitMessage,
1767
+ status,
1768
+ setMessages,
1769
+ stop
1770
+ };
1771
+ }
1621
1772
  // Annotate the CommonJS export names for ESM import in node:
1622
1773
  0 && (module.exports = {
1774
+ useAssistant,
1623
1775
  useChat,
1624
1776
  useCompletion
1625
1777
  });