ai 2.2.28 → 2.2.30

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.
@@ -11,15 +11,27 @@ interface FunctionCall {
11
11
  */
12
12
  name?: string;
13
13
  }
14
+ /**
15
+ * The tool calls generated by the model, such as function calls.
16
+ */
17
+ interface ToolCall {
18
+ id: string;
19
+ type: string;
20
+ function: {
21
+ name: string;
22
+ arguments: string;
23
+ };
24
+ }
14
25
  /**
15
26
  * Shared types between the API and UI packages.
16
27
  */
17
28
  interface Message {
18
29
  id: string;
30
+ tool_call_id?: string;
19
31
  createdAt?: Date;
20
32
  content: string;
21
33
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
22
- role: 'system' | 'user' | 'assistant' | 'function' | 'data';
34
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
23
35
  /**
24
36
  * If the message has a role of `function`, the `name` field is the name of the function.
25
37
  * Otherwise, the name field should not be set.
@@ -28,10 +40,15 @@ interface Message {
28
40
  /**
29
41
  * If the assistant role makes a function call, the `function_call` field
30
42
  * contains the function call name and arguments. Otherwise, the field should
31
- * not be set.
43
+ * not be set. (Deprecated and replaced by tool_calls.)
32
44
  */
33
45
  function_call?: string | FunctionCall;
34
46
  data?: JSONValue;
47
+ /**
48
+ * If the assistant role makes a tool call, the `tool_calls` field contains
49
+ * the tool call name and arguments. Otherwise, the field should not be set.
50
+ */
51
+ tool_calls?: string | ToolCall[];
35
52
  }
36
53
  type JSONValue = null | string | number | boolean | {
37
54
  [x: string]: JSONValue;
@@ -63,4 +80,159 @@ declare function experimental_buildOpenAssistantPrompt(messages: Pick<Message, '
63
80
  */
64
81
  declare function experimental_buildLlama2Prompt(messages: Pick<Message, 'content' | 'role'>[]): string;
65
82
 
66
- export { experimental_buildAnthropicPrompt, experimental_buildLlama2Prompt, experimental_buildOpenAssistantPrompt, experimental_buildStarChatBetaPrompt };
83
+ declare function experimental_buildOpenAIMessages(messages: Message[]): ChatCompletionMessageParam[];
84
+ type ChatCompletionMessageParam = ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionToolMessageParam | ChatCompletionFunctionMessageParam;
85
+ interface ChatCompletionSystemMessageParam {
86
+ /**
87
+ * The contents of the system message.
88
+ */
89
+ content: string | null;
90
+ /**
91
+ * The role of the messages author, in this case `system`.
92
+ */
93
+ role: 'system';
94
+ }
95
+ interface ChatCompletionUserMessageParam {
96
+ /**
97
+ * The contents of the user message.
98
+ */
99
+ content: string | Array<ChatCompletionContentPart> | null;
100
+ /**
101
+ * The role of the messages author, in this case `user`.
102
+ */
103
+ role: 'user';
104
+ }
105
+ type ChatCompletionContentPart = ChatCompletionContentPartText | ChatCompletionContentPartImage;
106
+ interface ChatCompletionContentPartText {
107
+ /**
108
+ * The text content.
109
+ */
110
+ text: string;
111
+ /**
112
+ * The type of the content part.
113
+ */
114
+ type: 'text';
115
+ }
116
+ interface ChatCompletionContentPartImage {
117
+ image_url: ChatCompletionContentPartImage.ImageURL;
118
+ /**
119
+ * The type of the content part.
120
+ */
121
+ type: 'image_url';
122
+ }
123
+ declare namespace ChatCompletionContentPartImage {
124
+ interface ImageURL {
125
+ /**
126
+ * Specifies the detail level of the image.
127
+ */
128
+ detail?: 'auto' | 'low' | 'high';
129
+ /**
130
+ * Either a URL of the image or the base64 encoded image data.
131
+ */
132
+ url?: string;
133
+ }
134
+ }
135
+ interface ChatCompletionAssistantMessageParam {
136
+ /**
137
+ * The contents of the assistant message.
138
+ */
139
+ content: string | null;
140
+ /**
141
+ * The role of the messages author, in this case `assistant`.
142
+ */
143
+ role: 'assistant';
144
+ /**
145
+ * Deprecated and replaced by `tool_calls`. The name and arguments of a function
146
+ * that should be called, as generated by the model.
147
+ */
148
+ function_call?: ChatCompletionAssistantMessageParam.FunctionCall;
149
+ /**
150
+ * The tool calls generated by the model, such as function calls.
151
+ */
152
+ tool_calls?: Array<ChatCompletionMessageToolCall>;
153
+ }
154
+ declare namespace ChatCompletionAssistantMessageParam {
155
+ /**
156
+ * Deprecated and replaced by `tool_calls`. The name and arguments of a function
157
+ * that should be called, as generated by the model.
158
+ */
159
+ interface FunctionCall {
160
+ /**
161
+ * The arguments to call the function with, as generated by the model in JSON
162
+ * format. Note that the model does not always generate valid JSON, and may
163
+ * hallucinate parameters not defined by your function schema. Validate the
164
+ * arguments in your code before calling your function.
165
+ */
166
+ arguments: string;
167
+ /**
168
+ * The name of the function to call.
169
+ */
170
+ name: string;
171
+ }
172
+ }
173
+ interface ChatCompletionMessageToolCall {
174
+ /**
175
+ * The ID of the tool call.
176
+ */
177
+ id: string;
178
+ /**
179
+ * The function that the model called.
180
+ */
181
+ function: ChatCompletionMessageToolCall.Function;
182
+ /**
183
+ * The type of the tool. Currently, only `function` is supported.
184
+ */
185
+ type: 'function';
186
+ }
187
+ declare namespace ChatCompletionMessageToolCall {
188
+ /**
189
+ * The function that the model called.
190
+ */
191
+ interface Function {
192
+ /**
193
+ * The arguments to call the function with, as generated by the model in JSON
194
+ * format. Note that the model does not always generate valid JSON, and may
195
+ * hallucinate parameters not defined by your function schema. Validate the
196
+ * arguments in your code before calling your function.
197
+ */
198
+ arguments: string;
199
+ /**
200
+ * The name of the function to call.
201
+ */
202
+ name: string;
203
+ }
204
+ }
205
+ interface ChatCompletionToolMessageParam {
206
+ /**
207
+ * The contents of the tool message.
208
+ */
209
+ content: string | null;
210
+ /**
211
+ * The role of the messages author, in this case `tool`.
212
+ */
213
+ role: 'tool';
214
+ /**
215
+ * The name of the function that this message is responding to.
216
+ */
217
+ name: string;
218
+ /**
219
+ * Tool call that this message is responding to.
220
+ */
221
+ tool_call_id: string;
222
+ }
223
+ interface ChatCompletionFunctionMessageParam {
224
+ /**
225
+ * The return value from the function call, to return to the model.
226
+ */
227
+ content: string | null;
228
+ /**
229
+ * The name of the function to call.
230
+ */
231
+ name: string;
232
+ /**
233
+ * The role of the messages author, in this case `function`.
234
+ */
235
+ role: 'function';
236
+ }
237
+
238
+ export { ChatCompletionAssistantMessageParam, ChatCompletionContentPart, ChatCompletionContentPartImage, ChatCompletionContentPartText, ChatCompletionFunctionMessageParam, ChatCompletionMessageParam, ChatCompletionMessageToolCall, ChatCompletionSystemMessageParam, ChatCompletionToolMessageParam, ChatCompletionUserMessageParam, experimental_buildAnthropicPrompt, experimental_buildLlama2Prompt, experimental_buildOpenAIMessages, experimental_buildOpenAssistantPrompt, experimental_buildStarChatBetaPrompt };
@@ -22,6 +22,7 @@ var prompts_exports = {};
22
22
  __export(prompts_exports, {
23
23
  experimental_buildAnthropicPrompt: () => experimental_buildAnthropicPrompt,
24
24
  experimental_buildLlama2Prompt: () => experimental_buildLlama2Prompt,
25
+ experimental_buildOpenAIMessages: () => experimental_buildOpenAIMessages,
25
26
  experimental_buildOpenAssistantPrompt: () => experimental_buildOpenAssistantPrompt,
26
27
  experimental_buildStarChatBetaPrompt: () => experimental_buildStarChatBetaPrompt
27
28
  });
@@ -97,10 +98,68 @@ ${content}
97
98
  });
98
99
  return startPrompt + conversation.join("") + endPrompt;
99
100
  }
101
+
102
+ // prompts/openai.tsx
103
+ function experimental_buildOpenAIMessages(messages) {
104
+ return messages.map((message) => {
105
+ switch (message.role) {
106
+ case "system":
107
+ case "user":
108
+ return {
109
+ role: message.role,
110
+ content: message.content
111
+ };
112
+ case "assistant": {
113
+ const function_call = message.function_call;
114
+ if (function_call !== void 0 && (typeof function_call === "string" || function_call.arguments === void 0 || function_call.name === void 0)) {
115
+ throw new Error(
116
+ "Invalid function call in message. Expected a function call object"
117
+ );
118
+ }
119
+ return {
120
+ role: message.role,
121
+ content: message.content,
122
+ function_call: function_call === void 0 ? void 0 : {
123
+ name: function_call.name,
124
+ arguments: function_call.arguments
125
+ }
126
+ };
127
+ }
128
+ case "function": {
129
+ if (message.name === void 0) {
130
+ throw new Error("Invalid function call in message. Expected a name");
131
+ }
132
+ return {
133
+ role: message.role,
134
+ content: message.content,
135
+ name: message.name
136
+ };
137
+ }
138
+ case "data": {
139
+ throw "unsupported message role 'data'";
140
+ }
141
+ case "tool": {
142
+ if (message.name === void 0) {
143
+ throw new Error("Invalid tool message. Expected a name");
144
+ }
145
+ if (message.tool_call_id === void 0) {
146
+ throw new Error("Invalid tool message. Expected a tool_call_id");
147
+ }
148
+ return {
149
+ role: message.role,
150
+ content: message.content,
151
+ name: message.name,
152
+ tool_call_id: message.tool_call_id
153
+ };
154
+ }
155
+ }
156
+ });
157
+ }
100
158
  // Annotate the CommonJS export names for ESM import in node:
101
159
  0 && (module.exports = {
102
160
  experimental_buildAnthropicPrompt,
103
161
  experimental_buildLlama2Prompt,
162
+ experimental_buildOpenAIMessages,
104
163
  experimental_buildOpenAssistantPrompt,
105
164
  experimental_buildStarChatBetaPrompt
106
165
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../index.ts","../anthropic.ts","../huggingface.ts"],"sourcesContent":["export * from './anthropic';\nexport * from './huggingface';\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,SAAS,kCACd,UACA;AACA,SACE,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAClC,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA;AAAA,SAAc;AAAA,IACvB,OAAO;AACL,aAAO;AAAA;AAAA,aAAkB;AAA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,aAAa,KAAK,EAAE,IAAI;AAC/C;","names":[]}
1
+ {"version":3,"sources":["../index.ts","../anthropic.ts","../huggingface.ts","../openai.tsx"],"sourcesContent":["export * from './anthropic';\nexport * from './huggingface';\nexport * from './openai';\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n","import { Message } from '../shared/types';\n\nexport function experimental_buildOpenAIMessages(\n messages: Message[],\n): ChatCompletionMessageParam[] {\n return messages.map(message => {\n switch (message.role) {\n case 'system':\n case 'user':\n return {\n role: message.role,\n content: message.content,\n } satisfies ChatCompletionMessageParam;\n\n case 'assistant': {\n const function_call = message.function_call;\n\n if (\n function_call !== undefined &&\n (typeof function_call === 'string' ||\n function_call.arguments === undefined ||\n function_call.name === undefined)\n ) {\n throw new Error(\n 'Invalid function call in message. Expected a function call object',\n );\n }\n\n return {\n role: message.role,\n content: message.content,\n function_call:\n function_call === undefined\n ? undefined\n : {\n name: function_call.name!,\n arguments: function_call.arguments!,\n },\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'function': {\n if (message.name === undefined) {\n throw new Error('Invalid function call in message. Expected a name');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'data': {\n throw \"unsupported message role 'data'\";\n }\n\n case 'tool': {\n if (message.name === undefined) {\n throw new Error('Invalid tool message. Expected a name');\n }\n\n if (message.tool_call_id === undefined) {\n throw new Error('Invalid tool message. Expected a tool_call_id');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n tool_call_id: message.tool_call_id,\n } satisfies ChatCompletionMessageParam;\n }\n }\n });\n}\n\n// copy of open ai messages (so we don't have a dependency on the openai package)\nexport type ChatCompletionMessageParam =\n | ChatCompletionSystemMessageParam\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionToolMessageParam\n | ChatCompletionFunctionMessageParam;\n\nexport interface ChatCompletionSystemMessageParam {\n /**\n * The contents of the system message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `system`.\n */\n role: 'system';\n}\n\nexport interface ChatCompletionUserMessageParam {\n /**\n * The contents of the user message.\n */\n content: string | Array<ChatCompletionContentPart> | null;\n\n /**\n * The role of the messages author, in this case `user`.\n */\n role: 'user';\n}\n\nexport type ChatCompletionContentPart =\n | ChatCompletionContentPartText\n | ChatCompletionContentPartImage;\n\nexport interface ChatCompletionContentPartText {\n /**\n * The text content.\n */\n text: string;\n\n /**\n * The type of the content part.\n */\n type: 'text';\n}\n\nexport interface ChatCompletionContentPartImage {\n image_url: ChatCompletionContentPartImage.ImageURL;\n\n /**\n * The type of the content part.\n */\n type: 'image_url';\n}\n\nexport namespace ChatCompletionContentPartImage {\n export interface ImageURL {\n /**\n * Specifies the detail level of the image.\n */\n detail?: 'auto' | 'low' | 'high';\n\n /**\n * Either a URL of the image or the base64 encoded image data.\n */\n url?: string;\n }\n}\n\nexport interface ChatCompletionAssistantMessageParam {\n /**\n * The contents of the assistant message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `assistant`.\n */\n role: 'assistant';\n\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n function_call?: ChatCompletionAssistantMessageParam.FunctionCall;\n\n /**\n * The tool calls generated by the model, such as function calls.\n */\n tool_calls?: Array<ChatCompletionMessageToolCall>;\n}\n\nexport namespace ChatCompletionAssistantMessageParam {\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n export interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionMessageToolCall {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: ChatCompletionMessageToolCall.Function;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: 'function';\n}\n\nexport namespace ChatCompletionMessageToolCall {\n /**\n * The function that the model called.\n */\n export interface Function {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionToolMessageParam {\n /**\n * The contents of the tool message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `tool`.\n */\n role: 'tool';\n\n /**\n * The name of the function that this message is responding to.\n */\n\n name: string;\n /**\n * Tool call that this message is responding to.\n */\n tool_call_id: string;\n}\n\nexport interface ChatCompletionFunctionMessageParam {\n /**\n * The return value from the function call, to return to the model.\n */\n content: string | null;\n\n /**\n * The name of the function to call.\n */\n name: string;\n\n /**\n * The role of the messages author, in this case `function`.\n */\n role: 'function';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,SAAS,kCACd,UACA;AACA,SACE,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAClC,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA;AAAA,SAAc;AAAA,IACvB,OAAO;AACL,aAAO;AAAA;AAAA,aAAkB;AAAA,IAC3B;AAAA,EACF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ation.join("") + endPrompt;
70
70
  }
71
+
72
+ // prompts/openai.tsx
73
+ function experimental_buildOpenAIMessages(messages) {
74
+ return messages.map((message) => {
75
+ switch (message.role) {
76
+ case "system":
77
+ case "user":
78
+ return {
79
+ role: message.role,
80
+ content: message.content
81
+ };
82
+ case "assistant": {
83
+ const function_call = message.function_call;
84
+ if (function_call !== void 0 && (typeof function_call === "string" || function_call.arguments === void 0 || function_call.name === void 0)) {
85
+ throw new Error(
86
+ "Invalid function call in message. Expected a function call object"
87
+ );
88
+ }
89
+ return {
90
+ role: message.role,
91
+ content: message.content,
92
+ function_call: function_call === void 0 ? void 0 : {
93
+ name: function_call.name,
94
+ arguments: function_call.arguments
95
+ }
96
+ };
97
+ }
98
+ case "function": {
99
+ if (message.name === void 0) {
100
+ throw new Error("Invalid function call in message. Expected a name");
101
+ }
102
+ return {
103
+ role: message.role,
104
+ content: message.content,
105
+ name: message.name
106
+ };
107
+ }
108
+ case "data": {
109
+ throw "unsupported message role 'data'";
110
+ }
111
+ case "tool": {
112
+ if (message.name === void 0) {
113
+ throw new Error("Invalid tool message. Expected a name");
114
+ }
115
+ if (message.tool_call_id === void 0) {
116
+ throw new Error("Invalid tool message. Expected a tool_call_id");
117
+ }
118
+ return {
119
+ role: message.role,
120
+ content: message.content,
121
+ name: message.name,
122
+ tool_call_id: message.tool_call_id
123
+ };
124
+ }
125
+ }
126
+ });
127
+ }
71
128
  export {
72
129
  experimental_buildAnthropicPrompt,
73
130
  experimental_buildLlama2Prompt,
131
+ experimental_buildOpenAIMessages,
74
132
  experimental_buildOpenAssistantPrompt,
75
133
  experimental_buildStarChatBetaPrompt
76
134
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../anthropic.ts","../huggingface.ts"],"sourcesContent":["import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n"],"mappings":";AAOO,SAAS,kCACd,UACA;AACA,SACE,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAClC,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA;AAAA,SAAc;AAAA,IACvB,OAAO;AACL,aAAO;AAAA;AAAA,aAAkB;AAAA,IAC3B;AAAA,EACF,CAAC,IAAI;AAET;;;ACZO,SAAS,qCACd,UACA;AACA,SACE,SACG,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAC1B,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA,EAAa;AAAA;AAAA,IACtB,WAAW,SAAS,aAAa;AAC/B,aAAO;AAAA,EAAkB;AAAA;AAAA,IAC3B,WAAW,SAAS,UAAU;AAC5B,aAAO;AAAA,EAAe;AAAA;AAAA,IACxB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE;AAAA,EACF,CAAC,EACA,KAAK,EAAE,IAAI;AAElB;AAOO,SAAS,sCACd,UACA;AACA,SACE,SACG,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAC1B,QAAI,SAAS,QAAQ;AACnB,aAAO,eAAe;AAAA,IACxB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE,WAAW,SAAS,UAAU;AAC5B,YAAM,IAAI,MAAM,iDAAiD;AAAA,IACnE,OAAO;AACL,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF,CAAC,EACA,KAAK,EAAE,IAAI;AAElB;AAOO,SAAS,+BACd,UACA;AACA,QAAM,cAAc;AACpB,QAAM,YAAY;AAClB,QAAM,eAAe,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,GAAG,UAAU;AAC9D,QAAI,SAAS,QAAQ;AACnB,aAAO,QAAQ,KAAK;AAAA,IACtB,WAAW,SAAS,aAAa;AAC/B,aAAO,YAAY;AAAA,IACrB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC5D,WAAW,SAAS,YAAY,UAAU,GAAG;AAC3C,aAAO;AAAA,EAAY;AAAA;AAAA;AAAA;AAAA,IACrB,OAAO;AACL,YAAM,IAAI,MAAM,yBAAyB,MAAM;AAAA,IACjD;AAAA,EACF,CAAC;AAED,SAAO,cAAc,aAAa,KAAK,EAAE,IAAI;AAC/C;","names":[]}
1
+ {"version":3,"sources":["../anthropic.ts","../huggingface.ts","../openai.tsx"],"sourcesContent":["import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n","import { Message } from '../shared/types';\n\nexport function experimental_buildOpenAIMessages(\n messages: Message[],\n): ChatCompletionMessageParam[] {\n return messages.map(message => {\n switch (message.role) {\n case 'system':\n case 'user':\n return {\n role: message.role,\n content: message.content,\n } satisfies ChatCompletionMessageParam;\n\n case 'assistant': {\n const function_call = message.function_call;\n\n if (\n function_call !== undefined &&\n (typeof function_call === 'string' ||\n function_call.arguments === undefined ||\n function_call.name === undefined)\n ) {\n throw new Error(\n 'Invalid function call in message. Expected a function call object',\n );\n }\n\n return {\n role: message.role,\n content: message.content,\n function_call:\n function_call === undefined\n ? undefined\n : {\n name: function_call.name!,\n arguments: function_call.arguments!,\n },\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'function': {\n if (message.name === undefined) {\n throw new Error('Invalid function call in message. Expected a name');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'data': {\n throw \"unsupported message role 'data'\";\n }\n\n case 'tool': {\n if (message.name === undefined) {\n throw new Error('Invalid tool message. Expected a name');\n }\n\n if (message.tool_call_id === undefined) {\n throw new Error('Invalid tool message. Expected a tool_call_id');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n tool_call_id: message.tool_call_id,\n } satisfies ChatCompletionMessageParam;\n }\n }\n });\n}\n\n// copy of open ai messages (so we don't have a dependency on the openai package)\nexport type ChatCompletionMessageParam =\n | ChatCompletionSystemMessageParam\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionToolMessageParam\n | ChatCompletionFunctionMessageParam;\n\nexport interface ChatCompletionSystemMessageParam {\n /**\n * The contents of the system message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `system`.\n */\n role: 'system';\n}\n\nexport interface ChatCompletionUserMessageParam {\n /**\n * The contents of the user message.\n */\n content: string | Array<ChatCompletionContentPart> | null;\n\n /**\n * The role of the messages author, in this case `user`.\n */\n role: 'user';\n}\n\nexport type ChatCompletionContentPart =\n | ChatCompletionContentPartText\n | ChatCompletionContentPartImage;\n\nexport interface ChatCompletionContentPartText {\n /**\n * The text content.\n */\n text: string;\n\n /**\n * The type of the content part.\n */\n type: 'text';\n}\n\nexport interface ChatCompletionContentPartImage {\n image_url: ChatCompletionContentPartImage.ImageURL;\n\n /**\n * The type of the content part.\n */\n type: 'image_url';\n}\n\nexport namespace ChatCompletionContentPartImage {\n export interface ImageURL {\n /**\n * Specifies the detail level of the image.\n */\n detail?: 'auto' | 'low' | 'high';\n\n /**\n * Either a URL of the image or the base64 encoded image data.\n */\n url?: string;\n }\n}\n\nexport interface ChatCompletionAssistantMessageParam {\n /**\n * The contents of the assistant message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `assistant`.\n */\n role: 'assistant';\n\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n function_call?: ChatCompletionAssistantMessageParam.FunctionCall;\n\n /**\n * The tool calls generated by the model, such as function calls.\n */\n tool_calls?: Array<ChatCompletionMessageToolCall>;\n}\n\nexport namespace ChatCompletionAssistantMessageParam {\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n export interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionMessageToolCall {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: ChatCompletionMessageToolCall.Function;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: 'function';\n}\n\nexport namespace ChatCompletionMessageToolCall {\n /**\n * The function that the model called.\n */\n export interface Function {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionToolMessageParam {\n /**\n * The contents of the tool message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `tool`.\n */\n role: 'tool';\n\n /**\n * The name of the function that this message is responding to.\n */\n\n name: string;\n /**\n * Tool call that this message is responding to.\n */\n tool_call_id: string;\n}\n\nexport interface ChatCompletionFunctionMessageParam {\n /**\n * The return value from the function call, to return to the model.\n */\n content: string | null;\n\n /**\n * The name of the function to call.\n */\n name: string;\n\n /**\n * The role of the messages author, in this case `function`.\n */\n role: 'function';\n}\n"],"mappings":";AAOO,SAAS,kCACd,UACA;AACA,SACE,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAClC,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA;AAAA,SAAc;AAAA,IACvB,OAAO;AACL,aAAO;AAAA;AAAA,aAAkB;AAAA,IAC3B;AAAA,EACF,CAAC,IAAI;AAET;;;ACZO,SAAS,qCACd,UACA;AACA,SACE,SACG,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAC1B,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA,EAAa;AAAA;AAAA,IACtB,WAAW,SAAS,aAAa;AAC/B,aAAO;AAAA,EAAkB;AAAA;AAAA,IAC3B,WAAW,SAAS,UAAU;AAC5B,aAAO;AAAA,EAAe;AAAA;AAAA,IACxB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE;AAAA,EACF,CAAC,EACA,KAAK,EAAE,IAAI;AAElB;AAOO,SAAS,sCACd,UACA;AACA,SACE,SACG,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAC1B,QAAI,SAAS,QAAQ;AACnB,aAAO,eAAe;AAAA,IACxB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE,WAAW,SAAS,UAAU;AAC5B,YAAM,IAAI,MAAM,iDAAiD;AAAA,IACnE,OAAO;AACL,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF,CAAC,EACA,KAAK,EAAE,IAAI;AAElB;AAOO,SAAS,+BACd,UACA;AACA,QAAM,cAAc;AACpB,QAAM,YAAY;AAClB,QAAM,eAAe,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,GAAG,UAAU;AAC9D,QAAI,SAAS,QAAQ;AACnB,aAAO,QAAQ,KAAK;AAAA,I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A,QACzD;AAEA,YAAI,QAAQ,iBAAiB,QAAW;AACtC,gBAAM,IAAI,MAAM,+CAA+C;AAAA,QACjE;AAEA,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,UACjB,MAAM,QAAQ;AAAA,UACd,cAAc,QAAQ;AAAA,QACxB;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -11,6 +11,38 @@ interface FunctionCall {
11
11
  */
12
12
  name?: string;
13
13
  }
14
+ /**
15
+ * The tool calls generated by the model, such as function calls.
16
+ */
17
+ interface ToolCall {
18
+ id: string;
19
+ type: string;
20
+ function: {
21
+ name: string;
22
+ arguments: string;
23
+ };
24
+ }
25
+ /**
26
+ * Controls which (if any) function is called by the model.
27
+ * - none means the model will not call a function and instead generates a message.
28
+ * - auto means the model can pick between generating a message or calling a function.
29
+ * - Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function.
30
+ * none is the default when no functions are present. auto is the default if functions are present.
31
+ */
32
+ type ToolChoice = 'none' | 'auto' | {
33
+ type: 'function';
34
+ function: {
35
+ name: string;
36
+ };
37
+ };
38
+ /**
39
+ * A list of tools the model may call. Currently, only functions are supported as a tool.
40
+ * Use this to provide a list of functions the model may generate JSON inputs for.
41
+ */
42
+ interface Tool {
43
+ type: 'function';
44
+ function: Function;
45
+ }
14
46
  interface Function {
15
47
  /**
16
48
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
@@ -39,10 +71,11 @@ type IdGenerator = () => string;
39
71
  */
40
72
  interface Message {
41
73
  id: string;
74
+ tool_call_id?: string;
42
75
  createdAt?: Date;
43
76
  content: string;
44
77
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
45
- role: 'system' | 'user' | 'assistant' | 'function' | 'data';
78
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
46
79
  /**
47
80
  * If the message has a role of `function`, the `name` field is the name of the function.
48
81
  * Otherwise, the name field should not be set.
@@ -51,10 +84,15 @@ interface Message {
51
84
  /**
52
85
  * If the assistant role makes a function call, the `function_call` field
53
86
  * contains the function call name and arguments. Otherwise, the field should
54
- * not be set.
87
+ * not be set. (Deprecated and replaced by tool_calls.)
55
88
  */
56
89
  function_call?: string | FunctionCall;
57
90
  data?: JSONValue;
91
+ /**
92
+ * If the assistant role makes a tool call, the `tool_calls` field contains
93
+ * the tool call name and arguments. Otherwise, the field should not be set.
94
+ */
95
+ tool_calls?: string | ToolCall[];
58
96
  }
59
97
  type CreateMessage = Omit<Message, 'id'> & {
60
98
  id?: Message['id'];
@@ -65,8 +103,11 @@ type ChatRequest = {
65
103
  functions?: Array<Function>;
66
104
  function_call?: FunctionCall;
67
105
  data?: Record<string, string>;
106
+ tools?: Array<Tool>;
107
+ tool_choice?: ToolChoice;
68
108
  };
69
109
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
110
+ type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
70
111
  type RequestOptions = {
71
112
  headers?: Record<string, string> | Headers;
72
113
  body?: object;
@@ -75,6 +116,8 @@ type ChatRequestOptions = {
75
116
  options?: RequestOptions;
76
117
  functions?: Array<Function>;
77
118
  function_call?: FunctionCall;
119
+ tools?: Array<Tool>;
120
+ tool_choice?: ToolChoice;
78
121
  data?: Record<string, string>;
79
122
  };
80
123
  type UseChatOptions = {
@@ -103,6 +146,12 @@ type UseChatOptions = {
103
146
  * automatically to the API and will be used to update the chat.
104
147
  */
105
148
  experimental_onFunctionCall?: FunctionCallHandler;
149
+ /**
150
+ * Callback function to be called when a tool call is received.
151
+ * If the function returns a `ChatRequest` object, the request will be sent
152
+ * automatically to the API and will be used to update the chat.
153
+ */
154
+ experimental_onToolCall?: ToolCallHandler;
106
155
  /**
107
156
  * Callback function to be called when the API response is received.
108
157
  */
@@ -285,7 +334,7 @@ type UseChatHelpers = {
285
334
  setInput: React.Dispatch<React.SetStateAction<string>>;
286
335
  /** An input/textarea-ready onChange handler to control the value of the input */
287
336
  handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
288
- /** Form submission handler to automatically reset input and append a user message */
337
+ /** Form submission handler to automatically reset input and append a user message */
289
338
  handleSubmit: (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => void;
290
339
  metadata?: Object;
291
340
  /** Whether the API request is in progress */
@@ -350,28 +399,61 @@ declare function useCompletion({ api, id, initialCompletion, initialInput, crede
350
399
 
351
400
  type AssistantStatus = 'in_progress' | 'awaiting_message';
352
401
  type UseAssistantHelpers = {
353
- /** Current messages in the chat */
402
+ /**
403
+ * The current array of chat messages.
404
+ */
354
405
  messages: Message[];
355
- /** Current thread ID */
406
+ /**
407
+ * The current thread ID.
408
+ */
356
409
  threadId: string | undefined;
357
- /** The current value of the input */
410
+ /**
411
+ * The current value of the input field.
412
+ */
358
413
  input: string;
359
- /** An input/textarea-ready onChange handler to control the value of the input */
414
+ /**
415
+ * Handler for the `onChange` event of the input field to control the input's value.
416
+ */
360
417
  handleInputChange: (event: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
361
- /** Form submission handler to automatically reset input and append a user message */
418
+ /**
419
+ * Form submission handler that automatically resets the input field and appends a user message.
420
+ */
362
421
  submitMessage: (event?: React.FormEvent<HTMLFormElement>, requestOptions?: {
363
422
  data?: Record<string, string>;
364
423
  }) => Promise<void>;
365
- /** Current status of the assistant */
424
+ /**
425
+ * The current status of the assistant. This can be used to show a loading indicator.
426
+ */
366
427
  status: AssistantStatus;
367
- /** Current error, if any */
428
+ /**
429
+ * The error thrown during the assistant message processing, if any.
430
+ */
368
431
  error: undefined | unknown;
369
432
  };
370
433
  type UseAssistantOptions = {
434
+ /**
435
+ * The API endpoint that accepts a `{ threadId: string | null; message: string; }` object and returns an `AssistantResponse` stream.
436
+ * The threadId refers to an existing thread with messages (or is `null` to create a new thread).
437
+ * The message is the next message that should be appended to the thread and sent to the assistant.
438
+ */
371
439
  api: string;
440
+ /**
441
+ * An optional string that represents the ID of an existing thread.
442
+ * If not provided, a new thread will be created.
443
+ */
372
444
  threadId?: string | undefined;
445
+ /**
446
+ * An optional literal that sets the mode of credentials to be used on the request.
447
+ * Defaults to "same-origin".
448
+ */
373
449
  credentials?: RequestCredentials;
450
+ /**
451
+ * An optional object of headers to be passed to the API endpoint.
452
+ */
374
453
  headers?: Record<string, string> | Headers;
454
+ /**
455
+ * An optional, additional body object to be passed to the API endpoint.
456
+ */
375
457
  body?: object;
376
458
  };
377
459
  declare function experimental_useAssistant({ api, threadId: threadIdParam, credentials, headers, body, }: UseAssistantOptions): UseAssistantHelpers;