@builder.io/ai-utils 0.3.11 → 0.3.12

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/mapping.ts ADDED
@@ -0,0 +1,98 @@
1
+ import type { ESMImport } from "./codegen";
2
+
3
+ export interface RawFigmaJson {
4
+ documents: any[];
5
+ document?: any;
6
+ components?: Record<string, any>;
7
+ styles?: Record<string, any>;
8
+ componentSets?: Record<string, any>;
9
+ schemaVersion?: number;
10
+ }
11
+
12
+ export interface UserContext {
13
+ client: string;
14
+ clientVersion: string;
15
+ nodeVersion: string;
16
+ systemPlatform: string;
17
+ frameworks: string[];
18
+ systemEOL: string;
19
+ systemArch: string;
20
+ systemShell?: string;
21
+ inGitRepo?: boolean;
22
+ [key: string]: string | string[] | boolean | undefined;
23
+ }
24
+
25
+ export type ExportType = "default" | "named";
26
+
27
+ /**
28
+ * Gets the latest component mappings for a space
29
+ */
30
+ export interface FigmaMappingsData {
31
+ id: string;
32
+ figmaBuilderLinks: FigmaBuilderLink[];
33
+ framework: string;
34
+ version?: number;
35
+ createdDate?: string; // ISO string timestamp or unix timestamp
36
+ local: boolean;
37
+ userEmail?: string;
38
+ remoteUrl?: string;
39
+ }
40
+
41
+ export interface FigmaBuilderLink {
42
+ builderName: string;
43
+ figmaName: string;
44
+ figmaKey: string;
45
+ figmaUrl?: string;
46
+ inputMapper?: string;
47
+ originalInputMapper?: string;
48
+ exportType?: ExportType;
49
+ importName?: string;
50
+ importPath?: string;
51
+ source: string;
52
+ loc?: string;
53
+ imports?: ESMImport[];
54
+ }
55
+
56
+ export interface FigmaMapperFile {
57
+ filePath: string;
58
+ content: string;
59
+ }
60
+
61
+ export interface PublishedMapping {
62
+ figmaBuilderLinks: FigmaBuilderLink[];
63
+ mapperFiles: FigmaMapperFile[];
64
+
65
+ // Meta data
66
+ remoteUrl?: string;
67
+ defaultBranch?: string;
68
+ currentBranch?: string;
69
+ commit?: string;
70
+ spaceKind?: string;
71
+
72
+ userContext?: UserContext;
73
+ }
74
+
75
+ export interface FigmaComponentInfo {
76
+ documentName: string;
77
+ key: string;
78
+ tree?: string;
79
+ jsx?: string;
80
+ type: string;
81
+ name: string;
82
+ exportJson?: any;
83
+ inputs: FigmaComponentInput[];
84
+ description: string;
85
+ documentationLinks: string[];
86
+ instanceId: string;
87
+ }
88
+
89
+ export interface FigmaComponentInput {
90
+ id: string;
91
+ name: string;
92
+ value?: any;
93
+ type: string;
94
+ baseType: "text" | "variant" | "boolean" | "slot";
95
+ variantOptions?: string[];
96
+ isDefault: boolean;
97
+ ref?: string;
98
+ }
@@ -0,0 +1,345 @@
1
+ import type { ContentUpdatePatch } from "./events.js";
2
+
3
+ /**
4
+ * Message param does not know the id of the message.
5
+ * This is an input message.
6
+ */
7
+ export type MessageParam =
8
+ | SystemMessageParam
9
+ | UserMessageParam
10
+ | AssistantMessageParam;
11
+
12
+ export interface ContentMessageItemText {
13
+ type: "text";
14
+ text: string;
15
+ cache?: boolean;
16
+ citations?: TextCitationParam[] | null;
17
+ ephemeral?: boolean;
18
+ }
19
+
20
+ export interface ContentMessageItemImage {
21
+ type: "image";
22
+ source: ImageBase64Source | ImageUrlSource;
23
+ cache?: boolean;
24
+ ephemeral?: boolean;
25
+ }
26
+
27
+ export interface ContentMessageItemDocument {
28
+ type: "document";
29
+ source: DocumentBase64Source | DocumentUrlSource;
30
+ cache?: boolean;
31
+ ephemeral?: boolean;
32
+ }
33
+
34
+ export interface DocumentBase64Source {
35
+ type: "base64";
36
+ media_type: "application/pdf";
37
+ data: string;
38
+ }
39
+
40
+ export interface DocumentUrlSource {
41
+ type: "url";
42
+ url: string;
43
+ }
44
+
45
+ export interface ContentMessageItemToolResult {
46
+ type: "tool_result";
47
+ tool_use_id: string;
48
+ tool_name?: string;
49
+ tool_input?: string;
50
+ title?: string;
51
+ content: string;
52
+ is_error?: boolean;
53
+ cache?: boolean;
54
+ ephemeral?: boolean;
55
+ }
56
+
57
+ export interface ImageBase64Source {
58
+ type: "base64";
59
+ media_type: "image/webp" | "image/png" | "image/jpeg";
60
+ data: string;
61
+ }
62
+
63
+ export interface ImageUrlSource {
64
+ type: "url";
65
+ url: string;
66
+ }
67
+
68
+ export interface ContentMessageItemThinking {
69
+ type: "thinking";
70
+ thinking: string;
71
+ signature: string;
72
+ }
73
+
74
+ export interface ContentMessageItemRedactedThinking {
75
+ type: "redacted_thinking";
76
+ data: string;
77
+ }
78
+
79
+ export interface ContentMessageItemToolUse {
80
+ type: "tool_use";
81
+ id: string;
82
+ input: unknown;
83
+ completion?: string;
84
+ name: string;
85
+ }
86
+
87
+ export interface CitationCharLocationParam {
88
+ cited_text: string;
89
+
90
+ document_index: number;
91
+
92
+ document_title: string | null;
93
+
94
+ end_char_index: number;
95
+
96
+ start_char_index: number;
97
+
98
+ type: "char_location";
99
+ }
100
+
101
+ export interface CitationContentBlockLocationParam {
102
+ cited_text: string;
103
+
104
+ document_index: number;
105
+
106
+ document_title: string | null;
107
+
108
+ end_block_index: number;
109
+
110
+ start_block_index: number;
111
+
112
+ type: "content_block_location";
113
+ }
114
+
115
+ export interface CitationPageLocationParam {
116
+ cited_text: string;
117
+
118
+ document_index: number;
119
+
120
+ document_title: string | null;
121
+
122
+ end_page_number: number;
123
+
124
+ start_page_number: number;
125
+
126
+ type: "page_location";
127
+ }
128
+
129
+ export interface CitationWebSearchResultLocationParam {
130
+ cited_text: string;
131
+
132
+ encrypted_index: string;
133
+
134
+ title: string | null;
135
+
136
+ type: "web_search_result_location";
137
+
138
+ url: string;
139
+ }
140
+
141
+ export type TextCitationParam =
142
+ | CitationCharLocationParam
143
+ | CitationPageLocationParam
144
+ | CitationContentBlockLocationParam
145
+ | CitationWebSearchResultLocationParam;
146
+
147
+ export interface ServerToolUseContent {
148
+ type: "web_search_result";
149
+ title: string;
150
+ url: string;
151
+ page_age: string | null;
152
+ encrypted_content: string;
153
+ }
154
+
155
+ export interface ServerToolUseContentError {
156
+ error_code:
157
+ | "invalid_tool_input"
158
+ | "unavailable"
159
+ | "max_uses_exceeded"
160
+ | "too_many_requests"
161
+ | "query_too_long";
162
+
163
+ type: "web_search_tool_result_error";
164
+ }
165
+
166
+ export type ServerToolUseContentUnion =
167
+ | ServerToolUseContent[]
168
+ | ServerToolUseContentError;
169
+
170
+ export interface ContentMessageItemServerToolUse {
171
+ type: "server_tool_use";
172
+ id: string;
173
+ name: "web_search";
174
+ input: unknown;
175
+ completion?: string;
176
+ content?: ServerToolUseContentUnion;
177
+ }
178
+
179
+ export interface ContentMessageItemWebSearchToolResult {
180
+ content: ServerToolUseContent[] | ServerToolUseContentError;
181
+ tool_use_id: string;
182
+ type: "web_search_tool_result";
183
+ }
184
+
185
+ export type ContentMessageItem =
186
+ | ContentMessageItemText
187
+ | ContentMessageItemImage
188
+ | ContentMessageItemDocument
189
+ | ContentMessageItemThinking
190
+ | ContentMessageItemRedactedThinking
191
+ | ContentMessageItemToolUse
192
+ | ContentMessageItemToolResult
193
+ | ContentMessageItemWebSearchToolResult
194
+ | ContentMessageItemServerToolUse;
195
+
196
+ export type ContentMessage = ContentMessageItem[];
197
+
198
+ export interface SystemMessageParam {
199
+ /**
200
+ * The contents of the system message.
201
+ */
202
+ content: string | ContentMessageItemText[];
203
+
204
+ // an id to track messages across a response including multiple retries
205
+ responseId?: string;
206
+
207
+ /**
208
+ * The role of the messages author, in this case `system`.
209
+ */
210
+ role: "system";
211
+
212
+ id?: string;
213
+ }
214
+
215
+ export interface UserMessageParam {
216
+ /**
217
+ * The contents of the user message.
218
+ */
219
+ content:
220
+ | string
221
+ | (
222
+ | ContentMessageItemText
223
+ | ContentMessageItemImage
224
+ | ContentMessageItemDocument
225
+ | ContentMessageItemToolResult
226
+ )[];
227
+
228
+ // an id to track messages across a response including multiple retries
229
+ responseId?: string;
230
+
231
+ /**
232
+ * The role of the messages author, in this case `user`.
233
+ */
234
+ role: "user";
235
+ id?: string;
236
+ }
237
+
238
+ export interface AssistantMessageParam {
239
+ /**
240
+ * The contents of the assistant message.
241
+ */
242
+ content: string | ContentMessageItem[];
243
+ /**
244
+ * The role of the messages author, in this case `assistant`.
245
+ */
246
+ role: "assistant";
247
+
248
+ // an id to track messages across a response including multiple retries
249
+ responseId?: string;
250
+
251
+ /**
252
+ * The function call name and arguments
253
+ */
254
+ action?: {
255
+ /**
256
+ * The specific function name
257
+ */
258
+ name: string;
259
+ /** This is arbitrary JSON */
260
+ arguments: any;
261
+ };
262
+ id?: string;
263
+ skipDelta?: boolean;
264
+ /**
265
+ * A summary of the patches which the assistant has made.
266
+ * Useful for genai.
267
+ */
268
+ patches?: ContentUpdatePatch[];
269
+ state?: "error";
270
+ }
271
+
272
+ export interface SystemMessage extends SystemMessageParam {
273
+ id: string;
274
+ }
275
+
276
+ export interface UserMessage extends UserMessageParam {
277
+ id: string;
278
+ }
279
+
280
+ export interface AssistantMessage extends AssistantMessageParam {
281
+ id: string;
282
+ status?: "accepted" | "rejected" | "aborted";
283
+ // Optional summary from the LLM that explains why it did
284
+ summary?: string;
285
+ }
286
+
287
+ export interface AssistantActionMessage {
288
+ /**
289
+ * The role of the messages author, in this case `assistant`.
290
+ */
291
+ role: "assistant";
292
+
293
+ id: string;
294
+ }
295
+
296
+ /**
297
+ * Message DOES know the id of the message.
298
+ * This message is after an id has been assigned
299
+ * and is the output message.
300
+ */
301
+ export type Message = SystemMessage | UserMessage | AssistantMessage;
302
+
303
+ export type GeneratingMessage = null | Partial<
304
+ AssistantActionMessage | AssistantMessage
305
+ >;
306
+
307
+ export function getContentText(message: string | ContentMessage) {
308
+ if (typeof message === "string") {
309
+ return message;
310
+ }
311
+ return message
312
+ .map((item) => (item.type === "text" ? item.text : ""))
313
+ .join("");
314
+ }
315
+
316
+ export function getContentAttachments(message: string | ContentMessage) {
317
+ if (typeof message === "string") {
318
+ return [];
319
+ }
320
+ return message
321
+ .filter((item) => item.type === "image")
322
+ .map((item) => item.source);
323
+ }
324
+
325
+ export type Attachment = FileUpload | Template | URL;
326
+
327
+ export interface URL {
328
+ type: "url";
329
+ value: string;
330
+ }
331
+
332
+ export interface FileUpload {
333
+ type: "upload";
334
+ contentType: string;
335
+ name: string;
336
+ dataUrl: string;
337
+ size: number;
338
+ id: string;
339
+ }
340
+
341
+ export interface Template {
342
+ type: "template";
343
+ name: string;
344
+ id: number;
345
+ }
@@ -0,0 +1,59 @@
1
+ export interface AssistantSettings {
2
+ assistantType?: string;
3
+ viewId?: string;
4
+ theme?: "dark" | "light";
5
+ }
6
+
7
+ interface IframeSettings extends Partial<AssistantSettings> {
8
+ local?: boolean;
9
+ /**
10
+ * The URL of the assistant.
11
+ */
12
+ baseUrl?: string;
13
+ }
14
+
15
+ type AssistantSettingsKeys = keyof AssistantSettings;
16
+
17
+ const urlParamSettings: AssistantSettingsKeys[] = [
18
+ "assistantType",
19
+ "theme",
20
+ "viewId",
21
+ ];
22
+
23
+ export function getAssistantUrl(opts: IframeSettings = {}) {
24
+ const url = new URL(
25
+ opts.baseUrl ??
26
+ (opts.local ? "http://localhost:7242" : "https://assistant.builder.io"),
27
+ );
28
+
29
+ urlParamSettings.forEach((key) => {
30
+ const value = opts[key];
31
+ if (typeof value === "string" || typeof value === "boolean") {
32
+ url.searchParams.set(key, String(value));
33
+ }
34
+ });
35
+
36
+ return url.href;
37
+ }
38
+
39
+ export function parseAssistantUrlSettings(url: string) {
40
+ const parsed = new URL(url);
41
+ const settings: Record<string, string | boolean> = {};
42
+
43
+ urlParamSettings.forEach((key) => {
44
+ const value = parsed.searchParams.get(key);
45
+ if (value === "true" || value === "false") {
46
+ settings[key] = value === "true";
47
+ } else if (value) {
48
+ settings[key] = value;
49
+ }
50
+ });
51
+
52
+ return settings as Partial<AssistantSettings>;
53
+ }
54
+
55
+ export interface BuilderEditorAuth {
56
+ spaceId: string;
57
+ userId: string;
58
+ authToken: string;
59
+ }