ai 2.1.8 → 2.1.10

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.
@@ -37,7 +37,7 @@ var __async = (__this, __arguments, generator) => {
37
37
  };
38
38
 
39
39
  // react/use-chat.ts
40
- import { useCallback, useId, useRef, useEffect, useState } from "react";
40
+ import { useCallback, useEffect, useId, useRef, useState } from "react";
41
41
  import useSWRMutation from "swr/mutation";
42
42
  import useSWR from "swr";
43
43
 
@@ -57,15 +57,97 @@ function createChunkDecoder() {
57
57
  }
58
58
 
59
59
  // react/use-chat.ts
60
+ var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => __async(void 0, null, function* () {
61
+ var _a, _b;
62
+ const previousMessages = messagesRef.current;
63
+ mutate(chatRequest.messages, false);
64
+ const res = yield fetch(api, __spreadValues({
65
+ method: "POST",
66
+ body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
67
+ messages: sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
68
+ ({ role, content, name, function_call }) => __spreadValues(__spreadValues({
69
+ role,
70
+ content
71
+ }, name !== void 0 && { name }), function_call !== void 0 && {
72
+ function_call
73
+ })
74
+ )
75
+ }, extraMetadataRef.current.body), (_a = chatRequest.options) == null ? void 0 : _a.body), chatRequest.functions !== void 0 && {
76
+ functions: chatRequest.functions
77
+ }), chatRequest.function_call !== void 0 && {
78
+ function_call: chatRequest.function_call
79
+ })),
80
+ credentials: extraMetadataRef.current.credentials,
81
+ headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), (_b = chatRequest.options) == null ? void 0 : _b.headers)
82
+ }, abortControllerRef.current !== null && {
83
+ signal: abortControllerRef.current.signal
84
+ })).catch((err) => {
85
+ mutate(previousMessages, false);
86
+ throw err;
87
+ });
88
+ if (onResponse) {
89
+ try {
90
+ yield onResponse(res);
91
+ } catch (err) {
92
+ throw err;
93
+ }
94
+ }
95
+ if (!res.ok) {
96
+ mutate(previousMessages, false);
97
+ throw new Error((yield res.text()) || "Failed to fetch the chat response.");
98
+ }
99
+ if (!res.body) {
100
+ throw new Error("The response body is empty.");
101
+ }
102
+ let streamedResponse = "";
103
+ const createdAt = /* @__PURE__ */ new Date();
104
+ const replyId = nanoid();
105
+ const reader = res.body.getReader();
106
+ const decode = createChunkDecoder();
107
+ let responseMessage = {
108
+ id: replyId,
109
+ createdAt,
110
+ content: "",
111
+ role: "assistant"
112
+ };
113
+ while (true) {
114
+ const { done, value } = yield reader.read();
115
+ if (done) {
116
+ break;
117
+ }
118
+ streamedResponse += decode(value);
119
+ if (streamedResponse.startsWith('{"function_call":')) {
120
+ responseMessage["function_call"] = streamedResponse;
121
+ } else {
122
+ responseMessage["content"] = streamedResponse;
123
+ }
124
+ mutate([...chatRequest.messages, __spreadValues({}, responseMessage)], false);
125
+ if (abortControllerRef.current === null) {
126
+ reader.cancel();
127
+ break;
128
+ }
129
+ }
130
+ if (streamedResponse.startsWith('{"function_call":')) {
131
+ const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
132
+ responseMessage["function_call"] = parsedFunctionCall;
133
+ mutate([...chatRequest.messages, __spreadValues({}, responseMessage)]);
134
+ }
135
+ if (onFinish) {
136
+ onFinish(responseMessage);
137
+ }
138
+ return responseMessage;
139
+ });
60
140
  function useChat({
61
141
  api = "/api/chat",
62
142
  id,
63
143
  initialMessages = [],
64
144
  initialInput = "",
65
145
  sendExtraMessageFields,
146
+ experimental_onFunctionCall,
66
147
  onResponse,
67
148
  onFinish,
68
149
  onError,
150
+ credentials,
69
151
  headers,
70
152
  body
71
153
  } = {}) {
@@ -81,92 +163,52 @@ function useChat({
81
163
  }, [messages]);
82
164
  const abortControllerRef = useRef(null);
83
165
  const extraMetadataRef = useRef({
166
+ credentials,
84
167
  headers,
85
168
  body
86
169
  });
87
170
  useEffect(() => {
88
171
  extraMetadataRef.current = {
172
+ credentials,
89
173
  headers,
90
174
  body
91
175
  };
92
- }, [headers, body]);
176
+ }, [credentials, headers, body]);
93
177
  const { error, trigger, isMutating } = useSWRMutation(
94
178
  [api, chatId],
95
- (_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
179
+ (_0, _1) => __async(this, [_0, _1], function* (_, { arg: initialChatRequest }) {
96
180
  try {
97
- const { messages: messagesSnapshot, options } = arg;
98
181
  const abortController = new AbortController();
99
182
  abortControllerRef.current = abortController;
100
- const previousMessages = messagesRef.current;
101
- mutate(messagesSnapshot, false);
102
- const res = yield fetch(api, {
103
- method: "POST",
104
- body: JSON.stringify(__spreadValues(__spreadValues({
105
- messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
106
- role,
107
- content
108
- }))
109
- }, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
110
- headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
111
- signal: abortController.signal
112
- }).catch((err) => {
113
- mutate(previousMessages, false);
114
- throw err;
115
- });
116
- if (onResponse) {
117
- try {
118
- yield onResponse(res);
119
- } catch (err) {
120
- throw err;
121
- }
122
- }
123
- if (!res.ok) {
124
- mutate(previousMessages, false);
125
- throw new Error(
126
- (yield res.text()) || "Failed to fetch the chat response."
127
- );
128
- }
129
- if (!res.body) {
130
- throw new Error("The response body is empty.");
131
- }
132
- let result = "";
133
- const createdAt = /* @__PURE__ */ new Date();
134
- const replyId = nanoid();
135
- const reader = res.body.getReader();
136
- const decode = createChunkDecoder();
183
+ let chatRequest = initialChatRequest;
137
184
  while (true) {
138
- const { done, value } = yield reader.read();
139
- if (done) {
140
- break;
141
- }
142
- result += decode(value);
143
- mutate(
144
- [
145
- ...messagesSnapshot,
146
- {
147
- id: replyId,
148
- createdAt,
149
- content: result,
150
- role: "assistant"
151
- }
152
- ],
153
- false
185
+ const streamedResponseMessage = yield getStreamedResponse(
186
+ api,
187
+ chatRequest,
188
+ mutate,
189
+ extraMetadataRef,
190
+ messagesRef,
191
+ abortControllerRef,
192
+ onFinish,
193
+ onResponse,
194
+ sendExtraMessageFields
154
195
  );
155
- if (abortControllerRef.current === null) {
156
- reader.cancel();
196
+ if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
157
197
  break;
158
198
  }
159
- }
160
- if (onFinish) {
161
- onFinish({
162
- id: replyId,
163
- createdAt,
164
- content: result,
165
- role: "assistant"
166
- });
199
+ if (experimental_onFunctionCall) {
200
+ const functionCall = streamedResponseMessage.function_call;
201
+ const functionCallResponse = yield experimental_onFunctionCall(
202
+ messagesRef.current,
203
+ functionCall
204
+ );
205
+ if (functionCallResponse === void 0)
206
+ break;
207
+ chatRequest = functionCallResponse;
208
+ }
167
209
  }
168
210
  abortControllerRef.current = null;
169
- return result;
211
+ return null;
170
212
  } catch (err) {
171
213
  if (err.name === "AbortError") {
172
214
  abortControllerRef.current = null;
@@ -184,32 +226,35 @@ function useChat({
184
226
  }
185
227
  );
186
228
  const append = useCallback(
187
- (message, options) => __async(this, null, function* () {
229
+ (_0, ..._1) => __async(this, [_0, ..._1], function* (message, { options, functions, function_call } = {}) {
188
230
  if (!message.id) {
189
231
  message.id = nanoid();
190
232
  }
191
- return trigger({
233
+ const chatRequest = __spreadValues(__spreadValues({
192
234
  messages: messagesRef.current.concat(message),
193
235
  options
194
- });
236
+ }, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
237
+ return trigger(chatRequest);
195
238
  }),
196
239
  [trigger]
197
240
  );
198
241
  const reload = useCallback(
199
- (options) => __async(this, null, function* () {
242
+ (..._0) => __async(this, [..._0], function* ({ options, functions, function_call } = {}) {
200
243
  if (messagesRef.current.length === 0)
201
244
  return null;
202
245
  const lastMessage = messagesRef.current[messagesRef.current.length - 1];
203
246
  if (lastMessage.role === "assistant") {
204
- return trigger({
247
+ const chatRequest2 = __spreadValues(__spreadValues({
205
248
  messages: messagesRef.current.slice(0, -1),
206
249
  options
207
- });
250
+ }, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
251
+ return trigger(chatRequest2);
208
252
  }
209
- return trigger({
253
+ const chatRequest = __spreadValues(__spreadValues({
210
254
  messages: messagesRef.current,
211
255
  options
212
- });
256
+ }, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
257
+ return trigger(chatRequest);
213
258
  }),
214
259
  [trigger]
215
260
  );
@@ -228,18 +273,21 @@ function useChat({
228
273
  );
229
274
  const [input, setInput] = useState(initialInput);
230
275
  const handleSubmit = useCallback(
231
- (e, metadata) => {
276
+ (e, { options, functions, function_call } = {}, metadata) => {
232
277
  if (metadata) {
233
278
  extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata);
234
279
  }
235
280
  e.preventDefault();
236
281
  if (!input)
237
282
  return;
238
- append({
239
- content: input,
240
- role: "user",
241
- createdAt: /* @__PURE__ */ new Date()
242
- });
283
+ append(
284
+ {
285
+ content: input,
286
+ role: "user",
287
+ createdAt: /* @__PURE__ */ new Date()
288
+ },
289
+ { options, functions, function_call }
290
+ );
243
291
  setInput("");
244
292
  },
245
293
  [input, append]
@@ -271,6 +319,7 @@ function useCompletion({
271
319
  id,
272
320
  initialCompletion = "",
273
321
  initialInput = "",
322
+ credentials,
274
323
  headers,
275
324
  body,
276
325
  onResponse,
@@ -285,15 +334,17 @@ function useCompletion({
285
334
  const completion = data;
286
335
  const [abortController, setAbortController] = useState2(null);
287
336
  const extraMetadataRef = useRef2({
337
+ credentials,
288
338
  headers,
289
339
  body
290
340
  });
291
341
  useEffect2(() => {
292
342
  extraMetadataRef.current = {
343
+ credentials,
293
344
  headers,
294
345
  body
295
346
  };
296
- }, [headers, body]);
347
+ }, [credentials, headers, body]);
297
348
  const { error, trigger, isMutating } = useSWRMutation2(
298
349
  [api, completionId],
299
350
  (_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
@@ -307,6 +358,7 @@ function useCompletion({
307
358
  body: JSON.stringify(__spreadValues(__spreadValues({
308
359
  prompt
309
360
  }, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
361
+ credentials: extraMetadataRef.current.credentials,
310
362
  headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
311
363
  signal: abortController2.signal
312
364
  }).catch((err) => {
@@ -410,7 +462,32 @@ function useCompletion({
410
462
  isLoading: isMutating
411
463
  };
412
464
  }
465
+
466
+ // react/tokens.tsx
467
+ import { Suspense } from "react";
468
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
469
+ function Tokens(props) {
470
+ return __async(this, null, function* () {
471
+ const { stream } = props;
472
+ const reader = stream.getReader();
473
+ return /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) });
474
+ });
475
+ }
476
+ function RecursiveTokens(_0) {
477
+ return __async(this, arguments, function* ({ reader }) {
478
+ const { done, value } = yield reader.read();
479
+ if (done) {
480
+ return null;
481
+ }
482
+ const text = new TextDecoder().decode(value);
483
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
484
+ text,
485
+ /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(RecursiveTokens, { reader }) })
486
+ ] });
487
+ });
488
+ }
413
489
  export {
490
+ Tokens,
414
491
  useChat,
415
492
  useCompletion
416
493
  };
@@ -1,32 +1,49 @@
1
1
  import { Readable, Writable } from 'svelte/store';
2
+ import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge';
3
+ import { ChatCompletionFunctions } from 'openai-edge/types/api';
2
4
 
3
5
  /**
4
6
  * Shared types between the API and UI packages.
5
7
  */
6
- declare type Message = {
8
+ type Message = {
7
9
  id: string;
8
10
  createdAt?: Date;
9
11
  content: string;
10
- role: 'system' | 'user' | 'assistant';
12
+ role: 'system' | 'user' | 'assistant' | 'function';
13
+ /**
14
+ * If the message has a role of `function`, the `name` field is the name of the function.
15
+ * Otherwise, the name field should not be set.
16
+ */
17
+ name?: string;
18
+ /**
19
+ * If the assistant role makes a function call, the `function_call` field
20
+ * contains the function call name and arguments. Otherwise, the field should
21
+ * not be set.
22
+ */
23
+ function_call?: string | ChatCompletionRequestMessageFunctionCall;
11
24
  };
12
- declare type CreateMessage = {
13
- id?: string;
14
- createdAt?: Date;
15
- content: string;
16
- role: 'system' | 'user' | 'assistant';
25
+ type CreateMessage = Omit<Message, 'id'> & {
26
+ id?: Message['id'];
27
+ };
28
+ type ChatRequest = {
29
+ messages: Message[];
30
+ options?: RequestOptions;
31
+ functions?: Array<ChatCompletionFunctions>;
32
+ function_call?: CreateChatCompletionRequestFunctionCall;
17
33
  };
18
- declare type RequestOptions = {
34
+ type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>;
35
+ type RequestOptions = {
19
36
  headers?: Record<string, string> | Headers;
20
37
  body?: object;
21
38
  };
22
- declare type UseChatOptions = {
39
+ type UseChatOptions = {
23
40
  /**
24
41
  * The API endpoint that accepts a `{ messages: Message[] }` object and returns
25
42
  * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
26
43
  */
27
44
  api?: string;
28
45
  /**
29
- * An unique identifier for the chat. If not provided, a random one will be
46
+ * A unique identifier for the chat. If not provided, a random one will be
30
47
  * generated. When provided, the `useChat` hook with the same `id` will
31
48
  * have shared states across components.
32
49
  */
@@ -39,6 +56,12 @@ declare type UseChatOptions = {
39
56
  * Initial input of the chat.
40
57
  */
41
58
  initialInput?: string;
59
+ /**
60
+ * Callback function to be called when a function call is received.
61
+ * If the function returns a `ChatRequest` object, the request will be sent
62
+ * automatically to the API and will be used to update the chat.
63
+ */
64
+ experimental_onFunctionCall?: FunctionCallHandler;
42
65
  /**
43
66
  * Callback function to be called when the API response is received.
44
67
  */
@@ -51,6 +74,12 @@ declare type UseChatOptions = {
51
74
  * Callback function to be called when an error is encountered.
52
75
  */
53
76
  onError?: (error: Error) => void;
77
+ /**
78
+ * The credentials mode to be used for the fetch request.
79
+ * Possible values are: 'omit', 'same-origin', 'include'.
80
+ * Defaults to 'same-origin'.
81
+ */
82
+ credentials?: RequestCredentials;
54
83
  /**
55
84
  * HTTP headers to be sent with the API request.
56
85
  */
@@ -75,7 +104,7 @@ declare type UseChatOptions = {
75
104
  */
76
105
  sendExtraMessageFields?: boolean;
77
106
  };
78
- declare type UseCompletionOptions = {
107
+ type UseCompletionOptions = {
79
108
  /**
80
109
  * The API endpoint that accepts a `{ prompt: string }` object and returns
81
110
  * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
@@ -107,6 +136,12 @@ declare type UseCompletionOptions = {
107
136
  * Callback function to be called when an error is encountered.
108
137
  */
109
138
  onError?: (error: Error) => void;
139
+ /**
140
+ * The credentials mode to be used for the fetch request.
141
+ * Possible values are: 'omit', 'same-origin', 'include'.
142
+ * Defaults to 'same-origin'.
143
+ */
144
+ credentials?: RequestCredentials;
110
145
  /**
111
146
  * HTTP headers to be sent with the API request.
112
147
  */
@@ -126,7 +161,7 @@ declare type UseCompletionOptions = {
126
161
  body?: object;
127
162
  };
128
163
 
129
- declare type UseChatHelpers = {
164
+ type UseChatHelpers = {
130
165
  /** Current messages in the chat */
131
166
  messages: Readable<Message[]>;
132
167
  /** The error object of the API request */
@@ -159,9 +194,9 @@ declare type UseChatHelpers = {
159
194
  /** Whether the API request is in progress */
160
195
  isLoading: Writable<boolean>;
161
196
  };
162
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
197
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers;
163
198
 
164
- declare type UseCompletionHelpers = {
199
+ type UseCompletionHelpers = {
165
200
  /** The current completion result */
166
201
  completion: Readable<string>;
167
202
  /** The error object of the API request */
@@ -193,6 +228,6 @@ declare type UseCompletionHelpers = {
193
228
  /** Whether the API request is in progress */
194
229
  isLoading: Writable<boolean>;
195
230
  };
196
- declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
231
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
197
232
 
198
233
  export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
@@ -395,7 +395,7 @@ var SWR = class {
395
395
  }
396
396
  };
397
397
 
398
- // ../../node_modules/.pnpm/sswr@1.10.0_svelte@3.54.0/node_modules/sswr/dist/sswr.mjs
398
+ // ../../node_modules/.pnpm/sswr@1.10.0_svelte@4.0.0/node_modules/sswr/dist/sswr.mjs
399
399
  var import_svelte = require("svelte");
400
400
  function h() {
401
401
  }
@@ -527,6 +527,7 @@ function useChat({
527
527
  onResponse,
528
528
  onFinish,
529
529
  onError,
530
+ credentials,
530
531
  headers,
531
532
  body
532
533
  } = {}) {
@@ -561,7 +562,8 @@ function useChat({
561
562
  }))
562
563
  }, body), options == null ? void 0 : options.body)),
563
564
  headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
564
- signal: abortController.signal
565
+ signal: abortController.signal,
566
+ credentials
565
567
  }).catch((err) => {
566
568
  mutate(previousMessages);
567
569
  throw err;
@@ -691,6 +693,7 @@ function useCompletion({
691
693
  id,
692
694
  initialCompletion = "",
693
695
  initialInput = "",
696
+ credentials,
694
697
  headers,
695
698
  body,
696
699
  onResponse,
@@ -724,7 +727,8 @@ function useCompletion({
724
727
  prompt
725
728
  }, body), options == null ? void 0 : options.body)),
726
729
  headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
727
- signal: abortController.signal
730
+ signal: abortController.signal,
731
+ credentials
728
732
  }).catch((err) => {
729
733
  throw err;
730
734
  });
@@ -371,7 +371,7 @@ var SWR = class {
371
371
  }
372
372
  };
373
373
 
374
- // ../../node_modules/.pnpm/sswr@1.10.0_svelte@3.54.0/node_modules/sswr/dist/sswr.mjs
374
+ // ../../node_modules/.pnpm/sswr@1.10.0_svelte@4.0.0/node_modules/sswr/dist/sswr.mjs
375
375
  import { beforeUpdate as _, onDestroy as D } from "svelte";
376
376
  function h() {
377
377
  }
@@ -503,6 +503,7 @@ function useChat({
503
503
  onResponse,
504
504
  onFinish,
505
505
  onError,
506
+ credentials,
506
507
  headers,
507
508
  body
508
509
  } = {}) {
@@ -537,7 +538,8 @@ function useChat({
537
538
  }))
538
539
  }, body), options == null ? void 0 : options.body)),
539
540
  headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
540
- signal: abortController.signal
541
+ signal: abortController.signal,
542
+ credentials
541
543
  }).catch((err) => {
542
544
  mutate(previousMessages);
543
545
  throw err;
@@ -667,6 +669,7 @@ function useCompletion({
667
669
  id,
668
670
  initialCompletion = "",
669
671
  initialInput = "",
672
+ credentials,
670
673
  headers,
671
674
  body,
672
675
  onResponse,
@@ -700,7 +703,8 @@ function useCompletion({
700
703
  prompt
701
704
  }, body), options == null ? void 0 : options.body)),
702
705
  headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
703
- signal: abortController.signal
706
+ signal: abortController.signal,
707
+ credentials
704
708
  }).catch((err) => {
705
709
  throw err;
706
710
  });