@builder.io/ai-utils 0.3.18 → 0.3.20

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.
@@ -0,0 +1,378 @@
1
+ import type { BuilderContent, BuilderElement } from "@builder.io/sdk";
2
+ import type { AssistantMessage, AssistantMessageAction } from "./messages.js";
3
+ import type { AssistantSettings } from "./settings.js";
4
+ export type BuilderAssistantEventHandler = (ev: BuilderAssistantEvent) => void;
5
+ export type BuilderAssistantEvent = AssistantCompletionResultEvent | AssistantErrorEvent | AssistantStreamErrorEvent | AppCloseEvent | AppMessagesClickEvent | AppMessagesGenerationEvent | AppMessageEditCustomInstructionsEvent | AppPromptAbortEvent | AppPromptFocusEvent | AppPromptSubmitEvent | AppReadyEvent | AppSettingsSetEvent | AppThreadNewEvent | AssistantStatsEvent | AssistantThemeEvent | BuilderEditorAuthEvent | BuilderEditorStateEvent | ContentUpdateEvent | ContentApplySnapshotEvent | ModelUndoEvent | ModelRedoEvent | ResultEvent | ThreadCreatedEvent | ThreadMessageCompletedEvent | ThreadMessageCreatedEvent | ThreadMessageDeltaEvent | ThreadMessageFeedbackEvent | ThreadRunStepCreatedEvent | ThreadRunStepDeltaEvent | AppAcceptChangeEvent | AppAcceptRejectEvent | AssistantTrackEvent | AssistantEditorAuthMessage | AppAttachmentTemplateEvent | ThreadMessageRetryEvent | AppFigmaImportEvent | AppWebImportEvent | AppAiTemplatesEvent | AssistantContentInitialEvent | ThreadMessageSummaryEvent | AssistantHeartbeatEvent | ShowUpgradeDialogEvent;
6
+ export interface AssistantCompletionResultEvent {
7
+ type: "assistant.result";
8
+ data: {
9
+ content?: BuilderContent;
10
+ stats?: AssistantStats;
11
+ };
12
+ resolveId?: string;
13
+ }
14
+ export interface AssistantError {
15
+ message: string;
16
+ status?: number;
17
+ actions?: AssistantMessageAction[];
18
+ }
19
+ export interface AssistantErrorEvent {
20
+ type: "assistant.error";
21
+ data: AssistantError;
22
+ }
23
+ export interface AssistantStreamErrorEvent {
24
+ type: "assistant.stream.error";
25
+ data: AssistantError;
26
+ }
27
+ export interface AppFigmaImportEvent {
28
+ type: "assistant.app.figmaImport";
29
+ }
30
+ export interface AppWebImportEvent {
31
+ type: "assistant.app.webImport";
32
+ }
33
+ export interface AppAiTemplatesEvent {
34
+ type: "assistant.app.aiTemplates";
35
+ }
36
+ export interface AssistantTrackEvent {
37
+ type: "assistant.track";
38
+ data: {
39
+ name: string;
40
+ properties: Record<string, any>;
41
+ };
42
+ }
43
+ export interface AssistantThemeEvent {
44
+ type: "assistant.app.theme.update";
45
+ data: {
46
+ theme: string;
47
+ };
48
+ }
49
+ export interface AppCloseEvent {
50
+ type: "assistant.app.close";
51
+ }
52
+ export interface AppMessagesClickEvent {
53
+ type: "assistant.app.messages.click";
54
+ }
55
+ export interface ShowUpgradeDialogEvent {
56
+ type: "assistant.app.showUpgradeDialog";
57
+ }
58
+ export interface AppAcceptChangeEvent {
59
+ type: "assistant.app.change.accept";
60
+ }
61
+ export interface AppAcceptRejectEvent {
62
+ type: "assistant.app.change.reject";
63
+ }
64
+ export interface AppMessageEditCustomInstructionsEvent {
65
+ type: "assistant.app.messages.editCustomInstructions";
66
+ }
67
+ export interface AppAttachmentTemplateEvent {
68
+ type: "assistant.app.attachment.template";
69
+ data: {
70
+ id: number;
71
+ name: string;
72
+ };
73
+ }
74
+ export interface AppMessagesGenerationEvent {
75
+ type: "assistant.app.messages.generation";
76
+ data: {
77
+ state: GenerationState;
78
+ };
79
+ }
80
+ /**
81
+ * idle: no messages are being generated or queued to be generated
82
+ *
83
+ * queued: messages have been sent to the LLM, but no response has been received yet
84
+ *
85
+ * generating: messages are actively being generated and streaming back to the UI
86
+ */
87
+ export type GenerationState = "idle" | "queued" | "generating";
88
+ export interface AppPromptAbortEvent {
89
+ type: "assistant.app.prompt.abort";
90
+ }
91
+ export interface AppPromptFocusEvent {
92
+ type: "assistant.app.prompt.focus";
93
+ }
94
+ export interface AppPromptSubmitEvent {
95
+ type: "assistant.app.prompt.submit";
96
+ data: {
97
+ prompt: string;
98
+ attachments: any[];
99
+ };
100
+ }
101
+ export interface AppReadyEvent {
102
+ type: "assistant.app.ready";
103
+ }
104
+ export interface AppSettingsSetEvent {
105
+ type: "assistant.app.settings.set";
106
+ data: Partial<AssistantSettings>;
107
+ }
108
+ export interface AppThreadNewEvent {
109
+ type: "assistant.app.thread.new";
110
+ }
111
+ export interface AssistantEditorAuthMessage {
112
+ type: "assistant.editor.auth.update";
113
+ }
114
+ export interface BuilderEditorAuthEvent extends AwaitResultEvent {
115
+ type: "assistant.editor.auth";
116
+ }
117
+ export interface BuilderEditorStateEvent extends AwaitResultEvent {
118
+ type: "assistant.editor.state";
119
+ }
120
+ export interface AwaitResultEvent {
121
+ resolveId?: string;
122
+ }
123
+ export interface ResultEvent {
124
+ type: "assistant.result";
125
+ resolveId: string;
126
+ data: any;
127
+ }
128
+ export interface ContentCreatePatch {
129
+ parentId: string;
130
+ insertBeforeId: string;
131
+ element: BuilderElement;
132
+ }
133
+ export interface ContentApplySnapshot {
134
+ /**
135
+ * The id of the BuilderContent to apply the snapshot to
136
+ */
137
+ id: string;
138
+ /**
139
+ * Each snapshot can be either a full BuilderContent or individual BuilderElements.
140
+ * Order matters, as the snapshots will be applied in the order they are listed.
141
+ * The builder app will handle the logic of applying the snapshots in the correct order
142
+ * and to the right content/elements.
143
+ */
144
+ snapshots: (BuilderElement | BuilderContent)[];
145
+ }
146
+ export interface ContentApplySnapshotEvent {
147
+ type: "assistant.content.applysnapshot";
148
+ data: ContentApplySnapshot;
149
+ }
150
+ export interface ContentUpdateEvent {
151
+ type: "assistant.content.update";
152
+ data: ContentUpdatePatch[];
153
+ }
154
+ export type ContentUpdatePatch = ContentTsUpdateComponentPatch;
155
+ interface ContentPatchBase {
156
+ id: string;
157
+ nodeId?: string;
158
+ builderId?: string;
159
+ description?: string;
160
+ value: string;
161
+ displayValue?: string;
162
+ ts: number;
163
+ /**
164
+ * A change value is considered incomplete until we also parsed it's closing xml tag.
165
+ */
166
+ incomplete?: boolean;
167
+ /**
168
+ * If there was an error applying the patch, this will contain the error message.
169
+ */
170
+ error?: string;
171
+ /**
172
+ * If `true`, Editor AI attempted to process this patch. It could be that the patch
173
+ * had invalid syntax or that the LLM made its changes incorrectly. This can be used
174
+ * to differentiate between a patch that is actively being streamed in, as indicated by
175
+ * the "incomplete" property, and a patch that is has finished streaming and is being
176
+ * processed.
177
+ */
178
+ attemptedProcess?: boolean;
179
+ }
180
+ export interface ContentTsUpdateComponentPatch extends ContentPatchBase {
181
+ type: "update_component";
182
+ }
183
+ export interface AssistantStatsEvent {
184
+ type: "assistant.stats";
185
+ data: AssistantStats;
186
+ }
187
+ export interface AssistantStats {
188
+ /**
189
+ * The unique id of the thread (not the openai threadId)
190
+ */
191
+ threadId: string;
192
+ /**
193
+ * The unique id of the completion, which is a combination of the user's prompt and assistant's response.
194
+ */
195
+ completionId: string;
196
+ /**
197
+ * The model id used to generate this completion.
198
+ */
199
+ modelId: string;
200
+ /**
201
+ * The assistant's response message.
202
+ */
203
+ assistantMessage: string;
204
+ /**
205
+ * The assistant's summary of what it did. This appears after assistantMessage
206
+ * as well as any changes it made.
207
+ */
208
+ assistantSummary: string;
209
+ /**
210
+ * The user's prompt message.
211
+ */
212
+ userMessage: string;
213
+ /**
214
+ * The index within the thread the assistant message is.
215
+ * For a first assistant message, the index will be 1 (the user message is index 0).
216
+ * For a second assistant message, the index will be 3 (the user message is index 2), and so on.
217
+ */
218
+ assistantMessageIndex: number;
219
+ /**
220
+ * The timestamp (Date.now()) of when the user first submitted their prompt.
221
+ */
222
+ userPromptMs: number;
223
+ /**
224
+ * The timestamp of the first assistant chunk in the response.
225
+ */
226
+ firstChunkMs: number;
227
+ /**
228
+ * The timestamp of the last assistant chunk in the response.
229
+ */
230
+ lastChunkMs: number;
231
+ /**
232
+ * The total number of chunks in the assistant's streamed response.
233
+ */
234
+ chunkCount: number;
235
+ /**
236
+ * The total number of characters in the generated prompt sent to the LLM.
237
+ */
238
+ promptLength: number;
239
+ /**
240
+ * The total number of characters in the assistant's response.
241
+ */
242
+ completionLength: number;
243
+ /**
244
+ * If the user provided custom instructions for the prompt.
245
+ */
246
+ hasCustomInstructions: boolean;
247
+ /**
248
+ * The deployed version.
249
+ */
250
+ version: string;
251
+ /**
252
+ * Error message if there was one.
253
+ */
254
+ errorMessage?: string;
255
+ /**
256
+ * Input tokens
257
+ */
258
+ inputTokens?: number;
259
+ /**
260
+ * Output tokens
261
+ */
262
+ outputTokens?: number;
263
+ /**
264
+ * Output tokens
265
+ */
266
+ completionCost?: number;
267
+ /**
268
+ * Number of streamed snapshots
269
+ */
270
+ streamedSnapshots?: number;
271
+ /**
272
+ * Number of cached input tokens
273
+ */
274
+ cacheInputTokens?: number;
275
+ /**
276
+ * Number of cached created tokens
277
+ */
278
+ cacheCreatedTokens?: number;
279
+ }
280
+ export interface ModelUndoEvent {
281
+ type: "assistant.model.undo";
282
+ }
283
+ export interface ModelRedoEvent {
284
+ type: "assistant.model.redo";
285
+ }
286
+ export interface BuilderModel {
287
+ name?: string;
288
+ friendlyName?: string;
289
+ description?: string;
290
+ type?: string;
291
+ fields?: BuilderModelField[];
292
+ }
293
+ export interface BuilderModelField {
294
+ name?: string;
295
+ type?: string;
296
+ description?: string;
297
+ }
298
+ export interface ThreadMessageFeedbackEvent {
299
+ type: "assistant.thread.message.feedback";
300
+ data: CompletionResponseFeedback;
301
+ }
302
+ export interface CompletionResponseFeedback {
303
+ userId: string;
304
+ builderUserId?: string;
305
+ builderEmail?: string;
306
+ responseId?: string;
307
+ frontendUrl: string | undefined;
308
+ frontendCommitId: string | undefined;
309
+ backendDomain?: string;
310
+ backendCommitId: string | undefined;
311
+ feedbackText: string;
312
+ sentiment?: "positive" | "negative";
313
+ }
314
+ export interface ThreadCreatedEvent {
315
+ type: "assistant.thread.created";
316
+ data: ThreadCreated;
317
+ }
318
+ export interface ThreadCreated {
319
+ platformId: string;
320
+ threadId: string;
321
+ vectorStoreId: string;
322
+ }
323
+ export interface ThreadMessageCreatedEvent {
324
+ type: "assistant.thread.message.created";
325
+ data: ThreadMessageCreated;
326
+ }
327
+ export interface ThreadMessageCreated {
328
+ id: string;
329
+ responseId: string;
330
+ threadId: string;
331
+ }
332
+ export interface ThreadMessageDeltaEvent {
333
+ type: "assistant.thread.message.delta";
334
+ data: ThreadMessageDelta;
335
+ }
336
+ export interface AssistantHeartbeatEvent {
337
+ type: "assistant.heartbeat";
338
+ }
339
+ export interface ThreadMessageDelta {
340
+ id: string;
341
+ text: string;
342
+ }
343
+ export interface ThreadMessageCompletedEvent {
344
+ type: "assistant.thread.message.completed";
345
+ data: ThreadMessageCompleted;
346
+ }
347
+ export interface ThreadMessageRetryEvent {
348
+ type: "assistant.thread.message.retry";
349
+ }
350
+ export interface ThreadMessageSummaryEvent {
351
+ type: "assistant.thread.message.summary.delta";
352
+ data: ThreadMessageDelta;
353
+ }
354
+ export interface ThreadMessageCompleted extends AssistantMessage {
355
+ platformId: string;
356
+ threadId: string;
357
+ commitId?: string;
358
+ }
359
+ export interface ThreadRunStepDeltaEvent {
360
+ type: "assistant.thread.run.step.delta";
361
+ data: ThreadMessageStepDelta;
362
+ }
363
+ export interface ThreadMessageStepDelta {
364
+ delta: any;
365
+ }
366
+ export interface ThreadRunStepCreatedEvent {
367
+ type: "assistant.thread.run.step.created";
368
+ }
369
+ export type DeepPartial<T> = T extends object ? {
370
+ [P in keyof T]?: DeepPartial<T[P]>;
371
+ } : T;
372
+ export interface AssistantContentInitialEvent {
373
+ type: "assistant.content.initial";
374
+ data: {
375
+ code: string;
376
+ };
377
+ }
378
+ export {};
package/src/events.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./completion.js";
2
+ export * from "./events.js";
3
+ export * from "./messages.js";
4
+ export * from "./settings.js";
5
+ export * from "./mapping.js";
6
+ export * from "./codegen.js";
@@ -0,0 +1,86 @@
1
+ import type { ESMImport } from "./codegen";
2
+ export interface RawFigmaJson {
3
+ documents: any[];
4
+ document?: any;
5
+ components?: Record<string, any>;
6
+ styles?: Record<string, any>;
7
+ componentSets?: Record<string, any>;
8
+ schemaVersion?: number;
9
+ }
10
+ export interface UserContext {
11
+ client: string;
12
+ clientVersion: string;
13
+ nodeVersion: string;
14
+ systemPlatform: string;
15
+ frameworks: string[];
16
+ systemEOL: string;
17
+ systemArch: string;
18
+ systemShell?: string;
19
+ inGitRepo?: boolean;
20
+ [key: string]: string | string[] | boolean | undefined;
21
+ }
22
+ export type ExportType = "default" | "named";
23
+ /**
24
+ * Gets the latest component mappings for a space
25
+ */
26
+ export interface FigmaMappingsData {
27
+ id: string;
28
+ figmaBuilderLinks: FigmaBuilderLink[];
29
+ framework: string;
30
+ version?: number;
31
+ createdDate?: string;
32
+ local: boolean;
33
+ userEmail?: string;
34
+ remoteUrl?: string;
35
+ }
36
+ export interface FigmaBuilderLink {
37
+ builderName: string;
38
+ figmaName: string;
39
+ figmaKey: string;
40
+ figmaUrl?: string;
41
+ inputMapper?: string;
42
+ originalInputMapper?: string;
43
+ exportType?: ExportType;
44
+ importName?: string;
45
+ importPath?: string;
46
+ source: string;
47
+ loc?: string;
48
+ imports?: ESMImport[];
49
+ }
50
+ export interface FigmaMapperFile {
51
+ filePath: string;
52
+ content: string;
53
+ }
54
+ export interface PublishedMapping {
55
+ figmaBuilderLinks: FigmaBuilderLink[];
56
+ mapperFiles: FigmaMapperFile[];
57
+ remoteUrl?: string;
58
+ defaultBranch?: string;
59
+ currentBranch?: string;
60
+ commit?: string;
61
+ spaceKind?: string;
62
+ userContext?: UserContext;
63
+ }
64
+ export interface FigmaComponentInfo {
65
+ documentName: string;
66
+ key: string;
67
+ tree?: string;
68
+ jsx?: string;
69
+ type: string;
70
+ name: string;
71
+ exportJson?: any;
72
+ inputs: FigmaComponentInput[];
73
+ description: string;
74
+ documentationLinks: string[];
75
+ instanceId: string;
76
+ }
77
+ export interface FigmaComponentInput {
78
+ id: string;
79
+ name: string;
80
+ value?: any;
81
+ type: string;
82
+ baseType: "text" | "variant" | "boolean" | "slot";
83
+ variantOptions?: string[];
84
+ isDefault: boolean;
85
+ ref?: string;
86
+ }
package/src/mapping.js ADDED
@@ -0,0 +1 @@
1
+ export {};