@builder.io/ai-utils 0.17.2 → 0.18.0

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/src/messages.d.ts CHANGED
@@ -3,266 +3,317 @@ import type { ContentUpdatePatch } from "./events.js";
3
3
  * Message param does not know the id of the message.
4
4
  * This is an input message.
5
5
  */
6
- export type MessageParam = SystemMessageParam | UserMessageParam | AssistantMessageParam;
6
+ export type MessageParam =
7
+ | SystemMessageParam
8
+ | UserMessageParam
9
+ | AssistantMessageParam;
7
10
  export interface ContentMessageItemText {
8
- type: "text";
9
- text: string;
10
- cache?: boolean;
11
- citations?: TextCitationParam[] | null;
12
- ephemeral?: boolean;
13
- thoughtSignature?: string;
14
- tag?: string;
11
+ type: "text";
12
+ text: string;
13
+ cache?: boolean;
14
+ citations?: TextCitationParam[] | null;
15
+ ephemeral?: boolean;
16
+ thoughtSignature?: string;
17
+ tag?: string;
15
18
  }
16
19
  export interface ContentMessageItemImage {
17
- type: "image";
18
- source: ImageBase64Source | ImageUrlSource;
19
- cache?: boolean;
20
- ephemeral?: boolean;
20
+ type: "image";
21
+ source: ImageBase64Source | ImageUrlSource;
22
+ cache?: boolean;
23
+ ephemeral?: boolean;
21
24
  }
22
25
  export interface ContentMessageItemDocument {
23
- type: "document";
24
- source: DocumentBase64Source | DocumentUrlSource | DocumentTextSource;
25
- cache?: boolean;
26
- ephemeral?: boolean;
27
- title?: string;
26
+ type: "document";
27
+ source: DocumentBase64Source | DocumentUrlSource | DocumentTextSource;
28
+ cache?: boolean;
29
+ ephemeral?: boolean;
30
+ title?: string;
28
31
  }
29
32
  export interface ContentMessageItemVideo {
30
- type: "video";
31
- source: VideoUrlSource;
32
- cache?: boolean;
33
- ephemeral?: boolean;
33
+ type: "video";
34
+ source: VideoUrlSource;
35
+ cache?: boolean;
36
+ ephemeral?: boolean;
34
37
  }
35
38
  export interface DocumentBase64Source {
36
- type: "base64";
37
- media_type: "application/pdf";
38
- data: string;
39
- original_url?: string;
39
+ type: "base64";
40
+ media_type: "application/pdf";
41
+ data: string;
42
+ original_url?: string;
40
43
  }
41
44
  export interface DocumentUrlSource {
42
- type: "url";
43
- url: string;
45
+ type: "url";
46
+ url: string;
44
47
  }
45
48
  export interface DocumentTextSource {
46
- type: "text";
47
- media_type: "text/plain";
48
- data: string;
49
+ type: "text";
50
+ media_type: "text/plain";
51
+ data: string;
49
52
  }
50
53
  export interface ContentMessageItemToolResult {
51
- type: "tool_result";
52
- tool_use_id: string;
53
- tool_name?: string;
54
- tool_input?: string;
55
- title?: string;
56
- content: string | (ContentMessageItemText | ContentMessageItemImage)[];
57
- is_error?: boolean;
58
- cache?: boolean;
59
- ephemeral?: boolean;
60
- structured_result?: Record<string, any>;
54
+ type: "tool_result";
55
+ tool_use_id: string;
56
+ tool_name?: string;
57
+ tool_input?: string;
58
+ title?: string;
59
+ content: string | (ContentMessageItemText | ContentMessageItemImage)[];
60
+ is_error?: boolean;
61
+ cache?: boolean;
62
+ ephemeral?: boolean;
63
+ structured_result?: Record<string, any>;
61
64
  }
62
65
  export interface ImageBase64Source {
63
- type: "base64";
64
- media_type: "image/webp" | "image/png" | "image/jpeg" | "image/gif";
65
- data: string;
66
- original_url?: string;
66
+ type: "base64";
67
+ media_type: "image/webp" | "image/png" | "image/jpeg" | "image/gif";
68
+ data: string;
69
+ original_url?: string;
67
70
  }
68
71
  export interface ImageUrlSource {
69
- type: "url";
70
- url: string;
72
+ type: "url";
73
+ url: string;
71
74
  }
72
75
  export interface VideoUrlSource {
73
- type: "url";
74
- url: string;
75
- media_type?: "video/mp4" | "video/mov" | "video/mpeg" | "video/mpg" | "video/avi" | "video/wmv" | "video/webm" | "video/flv";
76
+ type: "url";
77
+ url: string;
78
+ media_type?:
79
+ | "video/mp4"
80
+ | "video/mov"
81
+ | "video/mpeg"
82
+ | "video/mpg"
83
+ | "video/avi"
84
+ | "video/wmv"
85
+ | "video/webm"
86
+ | "video/flv";
76
87
  }
77
88
  export interface ContentMessageItemThinking {
78
- type: "thinking";
79
- thinking: string;
80
- signature: string;
81
- source?: "openai" | "gemini";
82
- id?: string;
89
+ type: "thinking";
90
+ thinking: string;
91
+ signature: string;
92
+ source?: "openai" | "gemini";
93
+ id?: string;
83
94
  }
84
95
  export interface ContentMessageItemRedactedThinking {
85
- type: "redacted_thinking";
86
- data: string;
87
- source?: "openai" | "gemini";
88
- id?: string;
96
+ type: "redacted_thinking";
97
+ data: string;
98
+ source?: "openai" | "gemini";
99
+ id?: string;
89
100
  }
90
101
  export interface ContentMessageItemToolUse {
91
- type: "tool_use";
92
- id: string;
93
- provider_id?: string;
94
- input: unknown;
95
- completion?: string;
96
- thoughtSignature?: string;
97
- name: string;
102
+ type: "tool_use";
103
+ id: string;
104
+ provider_id?: string;
105
+ input: unknown;
106
+ completion?: string;
107
+ thoughtSignature?: string;
108
+ name: string;
98
109
  }
99
110
  export interface CitationCharLocationParam {
100
- cited_text: string;
101
- document_index: number;
102
- document_title: string | null;
103
- end_char_index: number;
104
- start_char_index: number;
105
- type: "char_location";
111
+ cited_text: string;
112
+ document_index: number;
113
+ document_title: string | null;
114
+ end_char_index: number;
115
+ start_char_index: number;
116
+ type: "char_location";
106
117
  }
107
118
  export interface CitationContentBlockLocationParam {
108
- cited_text: string;
109
- document_index: number;
110
- document_title: string | null;
111
- end_block_index: number;
112
- start_block_index: number;
113
- type: "content_block_location";
119
+ cited_text: string;
120
+ document_index: number;
121
+ document_title: string | null;
122
+ end_block_index: number;
123
+ start_block_index: number;
124
+ type: "content_block_location";
114
125
  }
115
126
  export interface CitationPageLocationParam {
116
- cited_text: string;
117
- document_index: number;
118
- document_title: string | null;
119
- end_page_number: number;
120
- start_page_number: number;
121
- type: "page_location";
127
+ cited_text: string;
128
+ document_index: number;
129
+ document_title: string | null;
130
+ end_page_number: number;
131
+ start_page_number: number;
132
+ type: "page_location";
122
133
  }
123
134
  export interface CitationWebSearchResultLocationParam {
124
- cited_text: string;
125
- encrypted_index: string;
126
- title: string | null;
127
- type: "web_search_result_location";
128
- url: string;
129
- }
130
- export type TextCitationParam = CitationCharLocationParam | CitationPageLocationParam | CitationContentBlockLocationParam | CitationWebSearchResultLocationParam;
135
+ cited_text: string;
136
+ encrypted_index: string;
137
+ title: string | null;
138
+ type: "web_search_result_location";
139
+ url: string;
140
+ }
141
+ export type TextCitationParam =
142
+ | CitationCharLocationParam
143
+ | CitationPageLocationParam
144
+ | CitationContentBlockLocationParam
145
+ | CitationWebSearchResultLocationParam;
131
146
  export interface ServerToolUseContent {
132
- type: "web_search_result";
133
- title: string;
134
- url: string;
135
- page_age: string | null;
136
- encrypted_content: string;
147
+ type: "web_search_result";
148
+ title: string;
149
+ url: string;
150
+ page_age: string | null;
151
+ encrypted_content: string;
137
152
  }
138
153
  export interface ServerToolUseContentError {
139
- error_code: "invalid_tool_input" | "unavailable" | "max_uses_exceeded" | "too_many_requests" | "query_too_long";
140
- type: "web_search_tool_result_error";
141
- }
142
- export type ServerToolUseContentUnion = ServerToolUseContent[] | ServerToolUseContentError;
154
+ error_code:
155
+ | "invalid_tool_input"
156
+ | "unavailable"
157
+ | "max_uses_exceeded"
158
+ | "too_many_requests"
159
+ | "query_too_long";
160
+ type: "web_search_tool_result_error";
161
+ }
162
+ export type ServerToolUseContentUnion =
163
+ | ServerToolUseContent[]
164
+ | ServerToolUseContentError;
143
165
  export interface ContentMessageItemServerToolUse {
144
- type: "server_tool_use";
145
- id: string;
146
- name: "web_search" | "code_execution" | "web_fetch" | "bash_code_execution" | "text_editor_code_execution" | "tool_search_tool_regex" | "tool_search_tool_bm25";
147
- input: unknown;
148
- completion?: string;
149
- content?: any;
150
- cache?: boolean;
166
+ type: "server_tool_use";
167
+ id: string;
168
+ name:
169
+ | "web_search"
170
+ | "code_execution"
171
+ | "web_fetch"
172
+ | "bash_code_execution"
173
+ | "text_editor_code_execution"
174
+ | "tool_search_tool_regex"
175
+ | "tool_search_tool_bm25";
176
+ input: unknown;
177
+ completion?: string;
178
+ content?: any;
179
+ cache?: boolean;
151
180
  }
152
181
  export interface MCPServerURLDefinition {
153
- name: string;
154
- type: "url";
155
- url: string;
156
- authorization_token?: string | null;
157
- tool_configuration?: MCPServerToolConfiguration | null;
158
- token_expires_at?: number | null;
159
- create_date?: number | null;
160
- serverId: string;
161
- disabled: boolean;
182
+ name: string;
183
+ type: "url";
184
+ url: string;
185
+ authorization_token?: string | null;
186
+ tool_configuration?: MCPServerToolConfiguration | null;
187
+ token_expires_at?: number | null;
188
+ create_date?: number | null;
189
+ serverId: string;
190
+ disabled: boolean;
162
191
  }
163
192
  export interface MCPServerToolConfiguration {
164
- allowed_tools?: Array<string> | null;
165
- enabled?: boolean | null;
193
+ allowed_tools?: Array<string> | null;
194
+ enabled?: boolean | null;
166
195
  }
167
196
  export interface ContentMessageItemWebSearchToolResult {
168
- content: ServerToolUseContent[] | ServerToolUseContentError;
169
- tool_use_id: string;
170
- type: "web_search_tool_result";
171
- cache?: boolean;
197
+ content: ServerToolUseContent[] | ServerToolUseContentError;
198
+ tool_use_id: string;
199
+ type: "web_search_tool_result";
200
+ cache?: boolean;
172
201
  }
173
202
  export interface ContentMessageItemMCPToolUse {
174
- type: "mcp_tool_use";
175
- id: string;
176
- name: string;
177
- input: unknown;
178
- server_name: string;
179
- cache?: boolean;
203
+ type: "mcp_tool_use";
204
+ id: string;
205
+ name: string;
206
+ input: unknown;
207
+ server_name: string;
208
+ cache?: boolean;
180
209
  }
181
210
  export interface ContentMessageItemContainerUpload {
182
- type: "container_upload";
183
- file_id: string;
211
+ type: "container_upload";
212
+ file_id: string;
184
213
  }
185
214
  export interface ContentMessageItemMCPToolResult {
186
- type: "mcp_tool_result";
187
- tool_use_id: string;
188
- content: string | ContentMessageItemText[];
189
- is_error: boolean;
190
- cache?: boolean;
215
+ type: "mcp_tool_result";
216
+ tool_use_id: string;
217
+ content: string | ContentMessageItemText[];
218
+ is_error: boolean;
219
+ cache?: boolean;
191
220
  }
192
221
  export interface ContentMessageItemCodeExecutionToolResult {
193
- type: "code_execution_tool_result";
194
- tool_use_id: string;
195
- content: any;
196
- cache?: boolean;
197
- }
198
- export type ContentMessageItem = ContentMessageItemText | ContentMessageItemImage | ContentMessageItemDocument | ContentMessageItemVideo | ContentMessageItemThinking | ContentMessageItemRedactedThinking | ContentMessageItemToolUse | ContentMessageItemToolResult | ContentMessageItemWebSearchToolResult | ContentMessageItemServerToolUse | ContentMessageItemMCPToolUse | ContentMessageItemMCPToolResult | ContentMessageItemContainerUpload | ContentMessageItemCodeExecutionToolResult;
222
+ type: "code_execution_tool_result";
223
+ tool_use_id: string;
224
+ content: any;
225
+ cache?: boolean;
226
+ }
227
+ export type ContentMessageItem =
228
+ | ContentMessageItemText
229
+ | ContentMessageItemImage
230
+ | ContentMessageItemDocument
231
+ | ContentMessageItemVideo
232
+ | ContentMessageItemThinking
233
+ | ContentMessageItemRedactedThinking
234
+ | ContentMessageItemToolUse
235
+ | ContentMessageItemToolResult
236
+ | ContentMessageItemWebSearchToolResult
237
+ | ContentMessageItemServerToolUse
238
+ | ContentMessageItemMCPToolUse
239
+ | ContentMessageItemMCPToolResult
240
+ | ContentMessageItemContainerUpload
241
+ | ContentMessageItemCodeExecutionToolResult;
199
242
  export type ContentMessage = ContentMessageItem[];
200
243
  export interface SystemMessageParam {
201
- /**
202
- * The contents of the system message.
203
- */
204
- content: string | ContentMessageItemText[];
205
- responseId?: string;
206
- /**
207
- * The role of the messages author, in this case `system`.
208
- */
209
- role: "system";
210
- id?: string;
244
+ /**
245
+ * The contents of the system message.
246
+ */
247
+ content: string | ContentMessageItemText[];
248
+ responseId?: string;
249
+ /**
250
+ * The role of the messages author, in this case `system`.
251
+ */
252
+ role: "system";
253
+ id?: string;
211
254
  }
212
255
  export interface UserMessageParam {
213
- /**
214
- * The contents of the user message.
215
- */
216
- content: string | (ContentMessageItemText | ContentMessageItemImage | ContentMessageItemDocument | ContentMessageItemVideo | ContentMessageItemToolResult)[];
217
- responseId?: string;
218
- /**
219
- * The role of the messages author, in this case `user`.
220
- */
221
- role: "user";
222
- id?: string;
256
+ /**
257
+ * The contents of the user message.
258
+ */
259
+ content:
260
+ | string
261
+ | (
262
+ | ContentMessageItemText
263
+ | ContentMessageItemImage
264
+ | ContentMessageItemDocument
265
+ | ContentMessageItemVideo
266
+ | ContentMessageItemToolResult
267
+ )[];
268
+ responseId?: string;
269
+ /**
270
+ * The role of the messages author, in this case `user`.
271
+ */
272
+ role: "user";
273
+ id?: string;
223
274
  }
224
275
  export type AssistantMessageAction = "new-chat";
225
276
  export interface AssistantMessageParam {
226
- /**
227
- * The contents of the assistant message.
228
- */
229
- content: string | ContentMessageItem[];
230
- /**
231
- * The role of the messages author, in this case `assistant`.
232
- */
233
- role: "assistant";
234
- responseId?: string;
235
- id?: string;
236
- skipDelta?: boolean;
237
- /**
238
- * A summary of the patches which the assistant has made.
239
- * Useful for genai.
240
- */
241
- patches?: ContentUpdatePatch[];
242
- state?: "error";
243
- /**
244
- * Any actions associated with the error such as a "new chat" button.
245
- * typically used when there is an error.
246
- */
247
- actions?: AssistantMessageAction[];
277
+ /**
278
+ * The contents of the assistant message.
279
+ */
280
+ content: string | ContentMessageItem[];
281
+ /**
282
+ * The role of the messages author, in this case `assistant`.
283
+ */
284
+ role: "assistant";
285
+ responseId?: string;
286
+ id?: string;
287
+ skipDelta?: boolean;
288
+ /**
289
+ * A summary of the patches which the assistant has made.
290
+ * Useful for genai.
291
+ */
292
+ patches?: ContentUpdatePatch[];
293
+ state?: "error";
294
+ /**
295
+ * Any actions associated with the error such as a "new chat" button.
296
+ * typically used when there is an error.
297
+ */
298
+ actions?: AssistantMessageAction[];
248
299
  }
249
300
  export interface SystemMessage extends SystemMessageParam {
250
- id: string;
301
+ id: string;
251
302
  }
252
303
  export interface UserMessage extends UserMessageParam {
253
- id: string;
304
+ id: string;
254
305
  }
255
306
  export interface AssistantMessage extends AssistantMessageParam {
256
- id: string;
257
- status?: "accepted" | "rejected" | "aborted";
258
- summary?: string;
307
+ id: string;
308
+ status?: "accepted" | "rejected" | "aborted";
309
+ summary?: string;
259
310
  }
260
311
  export interface AssistantActionMessage {
261
- /**
262
- * The role of the messages author, in this case `assistant`.
263
- */
264
- role: "assistant";
265
- id: string;
312
+ /**
313
+ * The role of the messages author, in this case `assistant`.
314
+ */
315
+ role: "assistant";
316
+ id: string;
266
317
  }
267
318
  /**
268
319
  * Message DOES know the id of the message.
@@ -270,35 +321,53 @@ export interface AssistantActionMessage {
270
321
  * and is the output message.
271
322
  */
272
323
  export type Message = SystemMessage | UserMessage | AssistantMessage;
273
- export type GeneratingMessage = null | Partial<AssistantActionMessage | AssistantMessage>;
274
- export declare function getContentText(message: string | ContentMessage): string;
275
- export declare function getContentAttachments(message: string | ContentMessage): (ImageBase64Source | ImageUrlSource)[];
276
- export type Attachment = FileUpload | Template | URL | BuilderContentAttachment | FigmaContentAttachment;
324
+ export type GeneratingMessage = null | Partial<
325
+ AssistantActionMessage | AssistantMessage
326
+ >;
327
+ export declare function getContentText(
328
+ message: string | ContentMessage,
329
+ ): string;
330
+ export declare function getContentAttachments(
331
+ message: string | ContentMessage,
332
+ ): (ImageBase64Source | ImageUrlSource)[];
333
+ export type Attachment =
334
+ | FileUpload
335
+ | Template
336
+ | URL
337
+ | BuilderContentAttachment
338
+ | FigmaContentAttachment;
277
339
  export interface URL {
278
- type: "url";
279
- value: string;
340
+ type: "url";
341
+ value: string;
280
342
  }
281
343
  export interface FileUpload {
282
- type: "upload";
283
- contentType: "image/webp" | "image/png" | "image/jpeg" | "image/gif" | "application/pdf" | "application/json" | "text/plain";
284
- name: string;
285
- dataUrl: string;
286
- text?: string;
287
- size: number;
288
- id: string;
289
- originalUrl?: string;
290
- ephemeral?: boolean;
344
+ type: "upload";
345
+ contentType:
346
+ | "image/webp"
347
+ | "image/png"
348
+ | "image/jpeg"
349
+ | "image/gif"
350
+ | "application/pdf"
351
+ | "application/json"
352
+ | "text/plain";
353
+ name: string;
354
+ dataUrl: string;
355
+ text?: string;
356
+ size: number;
357
+ id: string;
358
+ originalUrl?: string;
359
+ ephemeral?: boolean;
291
360
  }
292
361
  export interface Template {
293
- type: "template";
294
- name: string;
295
- id: number;
362
+ type: "template";
363
+ name: string;
364
+ id: number;
296
365
  }
297
366
  export interface BuilderContentAttachment {
298
- type: "builder";
299
- html: string;
367
+ type: "builder";
368
+ html: string;
300
369
  }
301
370
  export interface FigmaContentAttachment {
302
- type: "figma";
303
- html: string;
371
+ type: "figma";
372
+ html: string;
304
373
  }
package/src/messages.js CHANGED
@@ -1,16 +1,16 @@
1
1
  export function getContentText(message) {
2
- if (typeof message === "string") {
3
- return message;
4
- }
5
- return message
6
- .map((item) => (item.type === "text" ? item.text : ""))
7
- .join("");
2
+ if (typeof message === "string") {
3
+ return message;
4
+ }
5
+ return message
6
+ .map((item) => (item.type === "text" ? item.text : ""))
7
+ .join("");
8
8
  }
9
9
  export function getContentAttachments(message) {
10
- if (typeof message === "string") {
11
- return [];
12
- }
13
- return message
14
- .filter((item) => item.type === "image")
15
- .map((item) => item.source);
10
+ if (typeof message === "string") {
11
+ return [];
12
+ }
13
+ return message
14
+ .filter((item) => item.type === "image")
15
+ .map((item) => item.source);
16
16
  }