@builder.io/ai-utils 0.18.0 → 0.18.2

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,317 +3,266 @@ 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 =
7
- | SystemMessageParam
8
- | UserMessageParam
9
- | AssistantMessageParam;
6
+ export type MessageParam = SystemMessageParam | UserMessageParam | AssistantMessageParam;
10
7
  export interface ContentMessageItemText {
11
- type: "text";
12
- text: string;
13
- cache?: boolean;
14
- citations?: TextCitationParam[] | null;
15
- ephemeral?: boolean;
16
- thoughtSignature?: string;
17
- tag?: string;
8
+ type: "text";
9
+ text: string;
10
+ cache?: boolean;
11
+ citations?: TextCitationParam[] | null;
12
+ ephemeral?: boolean;
13
+ thoughtSignature?: string;
14
+ tag?: string;
18
15
  }
19
16
  export interface ContentMessageItemImage {
20
- type: "image";
21
- source: ImageBase64Source | ImageUrlSource;
22
- cache?: boolean;
23
- ephemeral?: boolean;
17
+ type: "image";
18
+ source: ImageBase64Source | ImageUrlSource;
19
+ cache?: boolean;
20
+ ephemeral?: boolean;
24
21
  }
25
22
  export interface ContentMessageItemDocument {
26
- type: "document";
27
- source: DocumentBase64Source | DocumentUrlSource | DocumentTextSource;
28
- cache?: boolean;
29
- ephemeral?: boolean;
30
- title?: string;
23
+ type: "document";
24
+ source: DocumentBase64Source | DocumentUrlSource | DocumentTextSource;
25
+ cache?: boolean;
26
+ ephemeral?: boolean;
27
+ title?: string;
31
28
  }
32
29
  export interface ContentMessageItemVideo {
33
- type: "video";
34
- source: VideoUrlSource;
35
- cache?: boolean;
36
- ephemeral?: boolean;
30
+ type: "video";
31
+ source: VideoUrlSource;
32
+ cache?: boolean;
33
+ ephemeral?: boolean;
37
34
  }
38
35
  export interface DocumentBase64Source {
39
- type: "base64";
40
- media_type: "application/pdf";
41
- data: string;
42
- original_url?: string;
36
+ type: "base64";
37
+ media_type: "application/pdf";
38
+ data: string;
39
+ original_url?: string;
43
40
  }
44
41
  export interface DocumentUrlSource {
45
- type: "url";
46
- url: string;
42
+ type: "url";
43
+ url: string;
47
44
  }
48
45
  export interface DocumentTextSource {
49
- type: "text";
50
- media_type: "text/plain";
51
- data: string;
46
+ type: "text";
47
+ media_type: "text/plain";
48
+ data: string;
52
49
  }
53
50
  export interface ContentMessageItemToolResult {
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>;
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>;
64
61
  }
65
62
  export interface ImageBase64Source {
66
- type: "base64";
67
- media_type: "image/webp" | "image/png" | "image/jpeg" | "image/gif";
68
- data: string;
69
- original_url?: string;
63
+ type: "base64";
64
+ media_type: "image/webp" | "image/png" | "image/jpeg" | "image/gif";
65
+ data: string;
66
+ original_url?: string;
70
67
  }
71
68
  export interface ImageUrlSource {
72
- type: "url";
73
- url: string;
69
+ type: "url";
70
+ url: string;
74
71
  }
75
72
  export interface VideoUrlSource {
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";
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";
87
76
  }
88
77
  export interface ContentMessageItemThinking {
89
- type: "thinking";
90
- thinking: string;
91
- signature: string;
92
- source?: "openai" | "gemini";
93
- id?: string;
78
+ type: "thinking";
79
+ thinking: string;
80
+ signature: string;
81
+ source?: "openai" | "gemini";
82
+ id?: string;
94
83
  }
95
84
  export interface ContentMessageItemRedactedThinking {
96
- type: "redacted_thinking";
97
- data: string;
98
- source?: "openai" | "gemini";
99
- id?: string;
85
+ type: "redacted_thinking";
86
+ data: string;
87
+ source?: "openai" | "gemini";
88
+ id?: string;
100
89
  }
101
90
  export interface ContentMessageItemToolUse {
102
- type: "tool_use";
103
- id: string;
104
- provider_id?: string;
105
- input: unknown;
106
- completion?: string;
107
- thoughtSignature?: string;
108
- name: string;
91
+ type: "tool_use";
92
+ id: string;
93
+ provider_id?: string;
94
+ input: unknown;
95
+ completion?: string;
96
+ thoughtSignature?: string;
97
+ name: string;
109
98
  }
110
99
  export interface CitationCharLocationParam {
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";
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";
117
106
  }
118
107
  export interface CitationContentBlockLocationParam {
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";
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";
125
114
  }
126
115
  export interface CitationPageLocationParam {
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";
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";
133
122
  }
134
123
  export interface 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;
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;
146
131
  export interface ServerToolUseContent {
147
- type: "web_search_result";
148
- title: string;
149
- url: string;
150
- page_age: string | null;
151
- encrypted_content: string;
132
+ type: "web_search_result";
133
+ title: string;
134
+ url: string;
135
+ page_age: string | null;
136
+ encrypted_content: string;
152
137
  }
153
138
  export interface 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;
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;
165
143
  export interface ContentMessageItemServerToolUse {
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;
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;
180
151
  }
181
152
  export interface MCPServerURLDefinition {
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;
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;
191
162
  }
192
163
  export interface MCPServerToolConfiguration {
193
- allowed_tools?: Array<string> | null;
194
- enabled?: boolean | null;
164
+ allowed_tools?: Array<string> | null;
165
+ enabled?: boolean | null;
195
166
  }
196
167
  export interface ContentMessageItemWebSearchToolResult {
197
- content: ServerToolUseContent[] | ServerToolUseContentError;
198
- tool_use_id: string;
199
- type: "web_search_tool_result";
200
- cache?: boolean;
168
+ content: ServerToolUseContent[] | ServerToolUseContentError;
169
+ tool_use_id: string;
170
+ type: "web_search_tool_result";
171
+ cache?: boolean;
201
172
  }
202
173
  export interface ContentMessageItemMCPToolUse {
203
- type: "mcp_tool_use";
204
- id: string;
205
- name: string;
206
- input: unknown;
207
- server_name: string;
208
- cache?: boolean;
174
+ type: "mcp_tool_use";
175
+ id: string;
176
+ name: string;
177
+ input: unknown;
178
+ server_name: string;
179
+ cache?: boolean;
209
180
  }
210
181
  export interface ContentMessageItemContainerUpload {
211
- type: "container_upload";
212
- file_id: string;
182
+ type: "container_upload";
183
+ file_id: string;
213
184
  }
214
185
  export interface ContentMessageItemMCPToolResult {
215
- type: "mcp_tool_result";
216
- tool_use_id: string;
217
- content: string | ContentMessageItemText[];
218
- is_error: boolean;
219
- cache?: boolean;
186
+ type: "mcp_tool_result";
187
+ tool_use_id: string;
188
+ content: string | ContentMessageItemText[];
189
+ is_error: boolean;
190
+ cache?: boolean;
220
191
  }
221
192
  export interface 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;
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;
242
199
  export type ContentMessage = ContentMessageItem[];
243
200
  export interface SystemMessageParam {
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;
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;
254
211
  }
255
212
  export interface UserMessageParam {
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;
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;
274
223
  }
275
224
  export type AssistantMessageAction = "new-chat";
276
225
  export interface AssistantMessageParam {
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[];
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[];
299
248
  }
300
249
  export interface SystemMessage extends SystemMessageParam {
301
- id: string;
250
+ id: string;
302
251
  }
303
252
  export interface UserMessage extends UserMessageParam {
304
- id: string;
253
+ id: string;
305
254
  }
306
255
  export interface AssistantMessage extends AssistantMessageParam {
307
- id: string;
308
- status?: "accepted" | "rejected" | "aborted";
309
- summary?: string;
256
+ id: string;
257
+ status?: "accepted" | "rejected" | "aborted";
258
+ summary?: string;
310
259
  }
311
260
  export interface AssistantActionMessage {
312
- /**
313
- * The role of the messages author, in this case `assistant`.
314
- */
315
- role: "assistant";
316
- id: string;
261
+ /**
262
+ * The role of the messages author, in this case `assistant`.
263
+ */
264
+ role: "assistant";
265
+ id: string;
317
266
  }
318
267
  /**
319
268
  * Message DOES know the id of the message.
@@ -321,53 +270,35 @@ export interface AssistantActionMessage {
321
270
  * and is the output message.
322
271
  */
323
272
  export type Message = SystemMessage | UserMessage | AssistantMessage;
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;
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;
339
277
  export interface URL {
340
- type: "url";
341
- value: string;
278
+ type: "url";
279
+ value: string;
342
280
  }
343
281
  export interface FileUpload {
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;
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;
360
291
  }
361
292
  export interface Template {
362
- type: "template";
363
- name: string;
364
- id: number;
293
+ type: "template";
294
+ name: string;
295
+ id: number;
365
296
  }
366
297
  export interface BuilderContentAttachment {
367
- type: "builder";
368
- html: string;
298
+ type: "builder";
299
+ html: string;
369
300
  }
370
301
  export interface FigmaContentAttachment {
371
- type: "figma";
372
- html: string;
302
+ type: "figma";
303
+ html: string;
373
304
  }
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
  }