@builder.io/ai-utils 0.3.23 → 0.4.1

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,381 @@
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
+ data: {
67
+ hasChatMessages: boolean;
68
+ };
69
+ }
70
+ export interface AppAttachmentTemplateEvent {
71
+ type: "assistant.app.attachment.template";
72
+ data: {
73
+ id: number;
74
+ name: string;
75
+ };
76
+ }
77
+ export interface AppMessagesGenerationEvent {
78
+ type: "assistant.app.messages.generation";
79
+ data: {
80
+ state: GenerationState;
81
+ };
82
+ }
83
+ /**
84
+ * idle: no messages are being generated or queued to be generated
85
+ *
86
+ * queued: messages have been sent to the LLM, but no response has been received yet
87
+ *
88
+ * generating: messages are actively being generated and streaming back to the UI
89
+ */
90
+ export type GenerationState = "idle" | "queued" | "generating";
91
+ export interface AppPromptAbortEvent {
92
+ type: "assistant.app.prompt.abort";
93
+ }
94
+ export interface AppPromptFocusEvent {
95
+ type: "assistant.app.prompt.focus";
96
+ }
97
+ export interface AppPromptSubmitEvent {
98
+ type: "assistant.app.prompt.submit";
99
+ data: {
100
+ prompt: string;
101
+ attachments: any[];
102
+ };
103
+ }
104
+ export interface AppReadyEvent {
105
+ type: "assistant.app.ready";
106
+ }
107
+ export interface AppSettingsSetEvent {
108
+ type: "assistant.app.settings.set";
109
+ data: Partial<AssistantSettings>;
110
+ }
111
+ export interface AppThreadNewEvent {
112
+ type: "assistant.app.thread.new";
113
+ }
114
+ export interface AssistantEditorAuthMessage {
115
+ type: "assistant.editor.auth.update";
116
+ }
117
+ export interface BuilderEditorAuthEvent extends AwaitResultEvent {
118
+ type: "assistant.editor.auth";
119
+ }
120
+ export interface BuilderEditorStateEvent extends AwaitResultEvent {
121
+ type: "assistant.editor.state";
122
+ }
123
+ export interface AwaitResultEvent {
124
+ resolveId?: string;
125
+ }
126
+ export interface ResultEvent {
127
+ type: "assistant.result";
128
+ resolveId: string;
129
+ data: any;
130
+ }
131
+ export interface ContentCreatePatch {
132
+ parentId: string;
133
+ insertBeforeId: string;
134
+ element: BuilderElement;
135
+ }
136
+ export interface ContentApplySnapshot {
137
+ /**
138
+ * The id of the BuilderContent to apply the snapshot to
139
+ */
140
+ id: string;
141
+ /**
142
+ * Each snapshot can be either a full BuilderContent or individual BuilderElements.
143
+ * Order matters, as the snapshots will be applied in the order they are listed.
144
+ * The builder app will handle the logic of applying the snapshots in the correct order
145
+ * and to the right content/elements.
146
+ */
147
+ snapshots: (BuilderElement | BuilderContent)[];
148
+ }
149
+ export interface ContentApplySnapshotEvent {
150
+ type: "assistant.content.applysnapshot";
151
+ data: ContentApplySnapshot;
152
+ }
153
+ export interface ContentUpdateEvent {
154
+ type: "assistant.content.update";
155
+ data: ContentUpdatePatch[];
156
+ }
157
+ export type ContentUpdatePatch = ContentTsUpdateComponentPatch;
158
+ interface ContentPatchBase {
159
+ id: string;
160
+ nodeId?: string;
161
+ builderId?: string;
162
+ description?: string;
163
+ value: string;
164
+ displayValue?: string;
165
+ ts: number;
166
+ /**
167
+ * A change value is considered incomplete until we also parsed it's closing xml tag.
168
+ */
169
+ incomplete?: boolean;
170
+ /**
171
+ * If there was an error applying the patch, this will contain the error message.
172
+ */
173
+ error?: string;
174
+ /**
175
+ * If `true`, Editor AI attempted to process this patch. It could be that the patch
176
+ * had invalid syntax or that the LLM made its changes incorrectly. This can be used
177
+ * to differentiate between a patch that is actively being streamed in, as indicated by
178
+ * the "incomplete" property, and a patch that is has finished streaming and is being
179
+ * processed.
180
+ */
181
+ attemptedProcess?: boolean;
182
+ }
183
+ export interface ContentTsUpdateComponentPatch extends ContentPatchBase {
184
+ type: "update_component";
185
+ }
186
+ export interface AssistantStatsEvent {
187
+ type: "assistant.stats";
188
+ data: AssistantStats;
189
+ }
190
+ export interface AssistantStats {
191
+ /**
192
+ * The unique id of the thread (not the openai threadId)
193
+ */
194
+ threadId: string;
195
+ /**
196
+ * The unique id of the completion, which is a combination of the user's prompt and assistant's response.
197
+ */
198
+ completionId: string;
199
+ /**
200
+ * The model id used to generate this completion.
201
+ */
202
+ modelId: string;
203
+ /**
204
+ * The assistant's response message.
205
+ */
206
+ assistantMessage: string;
207
+ /**
208
+ * The assistant's summary of what it did. This appears after assistantMessage
209
+ * as well as any changes it made.
210
+ */
211
+ assistantSummary: string;
212
+ /**
213
+ * The user's prompt message.
214
+ */
215
+ userMessage: string;
216
+ /**
217
+ * The index within the thread the assistant message is.
218
+ * For a first assistant message, the index will be 1 (the user message is index 0).
219
+ * For a second assistant message, the index will be 3 (the user message is index 2), and so on.
220
+ */
221
+ assistantMessageIndex: number;
222
+ /**
223
+ * The timestamp (Date.now()) of when the user first submitted their prompt.
224
+ */
225
+ userPromptMs: number;
226
+ /**
227
+ * The timestamp of the first assistant chunk in the response.
228
+ */
229
+ firstChunkMs: number;
230
+ /**
231
+ * The timestamp of the last assistant chunk in the response.
232
+ */
233
+ lastChunkMs: number;
234
+ /**
235
+ * The total number of chunks in the assistant's streamed response.
236
+ */
237
+ chunkCount: number;
238
+ /**
239
+ * The total number of characters in the generated prompt sent to the LLM.
240
+ */
241
+ promptLength: number;
242
+ /**
243
+ * The total number of characters in the assistant's response.
244
+ */
245
+ completionLength: number;
246
+ /**
247
+ * If the user provided custom instructions for the prompt.
248
+ */
249
+ hasCustomInstructions: boolean;
250
+ /**
251
+ * The deployed version.
252
+ */
253
+ version: string;
254
+ /**
255
+ * Error message if there was one.
256
+ */
257
+ errorMessage?: string;
258
+ /**
259
+ * Input tokens
260
+ */
261
+ inputTokens?: number;
262
+ /**
263
+ * Output tokens
264
+ */
265
+ outputTokens?: number;
266
+ /**
267
+ * Output tokens
268
+ */
269
+ completionCost?: number;
270
+ /**
271
+ * Number of streamed snapshots
272
+ */
273
+ streamedSnapshots?: number;
274
+ /**
275
+ * Number of cached input tokens
276
+ */
277
+ cacheInputTokens?: number;
278
+ /**
279
+ * Number of cached created tokens
280
+ */
281
+ cacheCreatedTokens?: number;
282
+ }
283
+ export interface ModelUndoEvent {
284
+ type: "assistant.model.undo";
285
+ }
286
+ export interface ModelRedoEvent {
287
+ type: "assistant.model.redo";
288
+ }
289
+ export interface BuilderModel {
290
+ name?: string;
291
+ friendlyName?: string;
292
+ description?: string;
293
+ type?: string;
294
+ fields?: BuilderModelField[];
295
+ }
296
+ export interface BuilderModelField {
297
+ name?: string;
298
+ type?: string;
299
+ description?: string;
300
+ }
301
+ export interface ThreadMessageFeedbackEvent {
302
+ type: "assistant.thread.message.feedback";
303
+ data: CompletionResponseFeedback;
304
+ }
305
+ export interface CompletionResponseFeedback {
306
+ userId: string;
307
+ builderUserId?: string;
308
+ builderEmail?: string;
309
+ responseId?: string;
310
+ frontendUrl: string | undefined;
311
+ frontendCommitId: string | undefined;
312
+ backendDomain?: string;
313
+ backendCommitId: string | undefined;
314
+ feedbackText: string;
315
+ sentiment?: "positive" | "negative";
316
+ }
317
+ export interface ThreadCreatedEvent {
318
+ type: "assistant.thread.created";
319
+ data: ThreadCreated;
320
+ }
321
+ export interface ThreadCreated {
322
+ platformId: string;
323
+ threadId: string;
324
+ vectorStoreId: string;
325
+ }
326
+ export interface ThreadMessageCreatedEvent {
327
+ type: "assistant.thread.message.created";
328
+ data: ThreadMessageCreated;
329
+ }
330
+ export interface ThreadMessageCreated {
331
+ id: string;
332
+ responseId: string;
333
+ threadId: string;
334
+ }
335
+ export interface ThreadMessageDeltaEvent {
336
+ type: "assistant.thread.message.delta";
337
+ data: ThreadMessageDelta;
338
+ }
339
+ export interface AssistantHeartbeatEvent {
340
+ type: "assistant.heartbeat";
341
+ }
342
+ export interface ThreadMessageDelta {
343
+ id: string;
344
+ text: string;
345
+ }
346
+ export interface ThreadMessageCompletedEvent {
347
+ type: "assistant.thread.message.completed";
348
+ data: ThreadMessageCompleted;
349
+ }
350
+ export interface ThreadMessageRetryEvent {
351
+ type: "assistant.thread.message.retry";
352
+ }
353
+ export interface ThreadMessageSummaryEvent {
354
+ type: "assistant.thread.message.summary.delta";
355
+ data: ThreadMessageDelta;
356
+ }
357
+ export interface ThreadMessageCompleted extends AssistantMessage {
358
+ platformId: string;
359
+ threadId: string;
360
+ commitId?: string;
361
+ }
362
+ export interface ThreadRunStepDeltaEvent {
363
+ type: "assistant.thread.run.step.delta";
364
+ data: ThreadMessageStepDelta;
365
+ }
366
+ export interface ThreadMessageStepDelta {
367
+ delta: any;
368
+ }
369
+ export interface ThreadRunStepCreatedEvent {
370
+ type: "assistant.thread.run.step.created";
371
+ }
372
+ export type DeepPartial<T> = T extends object ? {
373
+ [P in keyof T]?: DeepPartial<T[P]>;
374
+ } : T;
375
+ export interface AssistantContentInitialEvent {
376
+ type: "assistant.content.initial";
377
+ data: {
378
+ code: string;
379
+ };
380
+ }
381
+ 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 {};