@builder.io/ai-utils 0.3.16 → 0.3.17

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Builder.io AI types and utilities
2
+
3
+ Shared types between AI systems, threads, servers, etc.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder.io/ai-utils",
3
- "version": "0.3.16",
3
+ "version": "0.3.17",
4
4
  "description": "Builder.io AI utils",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/codegen.ts ADDED
@@ -0,0 +1,686 @@
1
+ import type {
2
+ Attachment,
3
+ ContentMessageItemToolResult,
4
+ UserMessageParam,
5
+ } from "./messages";
6
+
7
+ import type { BuilderContent } from "./completion";
8
+
9
+ import type { Options as PrettierOptions } from "prettier";
10
+ import type { UserContext } from "./mapping";
11
+ import type { AssistantMessageParam } from "./messages";
12
+
13
+ // Define the import type as a union of literal types.
14
+ export type ImportType = "named" | "default";
15
+
16
+ // Define an interface for the import descriptor.
17
+ export interface ESMImport {
18
+ importName: string; // e.g. "Button"
19
+ importPath: string; // e.g. "path"
20
+ importType: ImportType; // Either 'named' or 'default'
21
+ }
22
+
23
+ export interface ProjectFile {
24
+ filePath: string;
25
+ content?: string;
26
+ importance?: number;
27
+ dropReason?: string;
28
+ wasIncluded?: boolean;
29
+ }
30
+
31
+ export interface CustomInstruction {
32
+ id: string;
33
+ name: string;
34
+ content: string;
35
+ type?: "always" | "agent-mode";
36
+ filePath?: string;
37
+ glob?: string;
38
+ description?: string;
39
+ }
40
+
41
+ export type CodeGenFramework =
42
+ | "react"
43
+ | "html"
44
+ | "mitosis"
45
+ | "react-native"
46
+ | "angular"
47
+ | "vue"
48
+ | "svelte"
49
+ | "qwik"
50
+ | "solid"
51
+ | "marko"
52
+ | "swiftui"
53
+ | "jetpack-compose"
54
+ | "flutter";
55
+
56
+ export type CodeGenStyleLibrary =
57
+ | "tailwind"
58
+ | "tailwind-precise"
59
+ | "emotion"
60
+ | "styled-components"
61
+ | "styled-jsx"
62
+ | "react-native"
63
+ | undefined;
64
+
65
+ export type CompletionStopReason =
66
+ | "max_tokens"
67
+ | "stop_sequence"
68
+ | "tool_use"
69
+ | "end_turn"
70
+ | "content_filter"
71
+ | "error"
72
+ | "aborted"
73
+ | "pause_turn"
74
+ | "refusal"
75
+ | null;
76
+
77
+ export interface ViewPathToolInput {
78
+ file_path: string;
79
+ view_range?: [number, number];
80
+ }
81
+
82
+ export interface GlobSearchToolInput {
83
+ pattern: string;
84
+ }
85
+
86
+ export interface GrepSearchToolInput {
87
+ query: string;
88
+ include_glob?: string;
89
+ exclude_glob?: string;
90
+ }
91
+
92
+ export interface GetRuleToolInput {
93
+ name: string;
94
+ }
95
+
96
+ export interface GetStyleInspirationToolInput {
97
+ url: string;
98
+ }
99
+
100
+ export interface GetBuildOutputToolInput {}
101
+ export interface DevServerControlInput {
102
+ restart?: boolean;
103
+ get_logs?: boolean;
104
+ }
105
+
106
+ export interface BashToolInput {
107
+ command?: string;
108
+ restart?: boolean;
109
+ }
110
+
111
+ export interface WebSearchToolInput {
112
+ query: string;
113
+ }
114
+
115
+ export interface WriteFileInput {
116
+ title: string;
117
+ file_path: string;
118
+ content: string;
119
+ }
120
+
121
+ export interface SearchReplaceInput {
122
+ title: string;
123
+ file_path: string;
124
+ old_str: string;
125
+ new_str: string;
126
+ }
127
+
128
+ export interface CodeGenToolMap {
129
+ view_path: ViewPathToolInput;
130
+ glob_search: GlobSearchToolInput;
131
+ grep_search: GrepSearchToolInput;
132
+ get_rule: GetRuleToolInput;
133
+ get_style_inspiration: GetStyleInspirationToolInput;
134
+ get_build_output: GetBuildOutputToolInput;
135
+ dev_server_control: DevServerControlInput;
136
+ bash: BashToolInput;
137
+ web_search: WebSearchToolInput;
138
+ write_file: WriteFileInput;
139
+ search_replace_file: SearchReplaceInput;
140
+ }
141
+
142
+ export type CodeGenTools = keyof CodeGenToolMap;
143
+
144
+ export type AllCodeGenTools =
145
+ | CodeGenTools
146
+ | "str_replace_editor"
147
+ | "web_search";
148
+
149
+ export type CodeGenMode =
150
+ | "exact" // @deprecated
151
+ | "precise" // tries to match the design as close
152
+ | "precise_vision" // tries to match the design as close, also uses vision to match
153
+ | "creative" // adapts the design to some generic design language
154
+ | "creative_vision" // adapts the design to some generic design language, also uses vision to match
155
+ | "creative_only_vision"; // adapts the design to some generic design language, but only uses vision to match
156
+
157
+ export interface CodeGenInputOptions {
158
+ position: string;
159
+ eventName?: string;
160
+ sessionId: string;
161
+
162
+ codeGenMode?: "fast" | "quality" | "quality-v3";
163
+ url?: string;
164
+ diffActions?: boolean;
165
+ planningPrompt?: boolean;
166
+ customInstructions?: CustomInstruction[];
167
+ userPrompt?: string;
168
+ ephemeralUserPrompt?: string;
169
+ displayUserPrompt?: string;
170
+ files?: ProjectFile[];
171
+ rerankFiles?: number;
172
+ toolResults?: ContentMessageItemToolResult[];
173
+ attachments?: Attachment[];
174
+ beforeCommit?: string;
175
+ workingDirectory?: string;
176
+
177
+ // Code options
178
+ builderContent?: BuilderContent;
179
+ framework?: CodeGenFramework;
180
+ styleLibrary?: CodeGenStyleLibrary;
181
+ typescript?: boolean;
182
+ userContext?: UserContext;
183
+
184
+ enabledTools?: CodeGenTools[];
185
+
186
+ // Options
187
+ maxTokens?: number;
188
+ maxPages?: number;
189
+ autoContinue?: number;
190
+ promptCaching?: boolean;
191
+ llmSuggestions?: boolean;
192
+ conclusionText?: boolean;
193
+
194
+ searchResponse?: any | null;
195
+
196
+ // Prettier options
197
+ prettierConfig?: PrettierOptions;
198
+
199
+ // Role
200
+ role?: "user" | "agent";
201
+ user?: UserSource;
202
+
203
+ /** @deprecated */
204
+ history?: (UserMessageParam | AssistantMessageParam)[];
205
+ /** @deprecated */
206
+ prevId?: string;
207
+ /** @deprecated */
208
+ nextPage?: boolean;
209
+ /** @deprecated */
210
+ vcpId?: string;
211
+ }
212
+
213
+ export type Feature = "component-mapping";
214
+
215
+ export interface CodegenUsage {
216
+ total: number;
217
+ fast: number;
218
+ quality: number;
219
+ features: Feature[];
220
+ limits: {
221
+ aiGeneration: number;
222
+ aiGenerationContextWindow: number;
223
+ };
224
+ }
225
+
226
+ export interface PromptSuggestion {
227
+ type:
228
+ | "missing-imports"
229
+ | "lazy-code"
230
+ | "no-file-for-diff"
231
+ | "syntax-error"
232
+ | "llm-suggested"
233
+ | "diff-apply";
234
+
235
+ filePath?: string;
236
+ line?: number;
237
+ importance: "high" | "medium" | "low";
238
+ column?: number;
239
+ summary: string;
240
+ prompt: string;
241
+ }
242
+
243
+ export interface ActionItem {
244
+ type:
245
+ | "file"
246
+ | "text"
247
+ | "diff"
248
+ | "thinking"
249
+ | "tool"
250
+ | "suggestion"
251
+ | "tool_result"
252
+ | "server_tool_result";
253
+ id?: string;
254
+ content: string;
255
+ filePath?: string;
256
+ artifactTitle?: string;
257
+ actionTitle?: string;
258
+ synthetic?: boolean;
259
+ incomplete?: boolean;
260
+ suggestions?: PromptSuggestion[];
261
+ errors?: string[];
262
+ }
263
+
264
+ export interface RepoInfo {
265
+ remoteUrl: string;
266
+ defaultBranch: string;
267
+ currentBranch: string;
268
+ commit: string;
269
+ }
270
+
271
+ export interface CodebaseSearchOptions {
272
+ repoInfo?: RepoInfo;
273
+ query: string;
274
+ selectedFiles?: string[];
275
+ sessionId: string;
276
+ files?: string[];
277
+ packageJson?: string;
278
+ limit?: number;
279
+ includeContent?: boolean;
280
+ }
281
+
282
+ export interface CodebaseSearchResponse {
283
+ id: string;
284
+ relevantPaths: string[];
285
+ grepQueries: string[];
286
+ streamMeta: any;
287
+ ranked: RankedResult[];
288
+ }
289
+
290
+ export interface RankedResult {
291
+ index: number;
292
+ filePath: string;
293
+ startIndex: number;
294
+ endIndex: number;
295
+ score: number;
296
+ id: string;
297
+ content?: string;
298
+ }
299
+
300
+ export interface GenerateCompletionStepThinking {
301
+ type: "thinking";
302
+ }
303
+
304
+ export interface FileInfo {
305
+ filePath: string;
306
+ size: number;
307
+ isCustomInstruction: boolean;
308
+ }
309
+
310
+ export interface GenerateCompletionStepUserInput {
311
+ type: "user-input";
312
+ prompt: string;
313
+ files: FileInfo[];
314
+ }
315
+
316
+ export interface GenerateCompletionStepToolResult {
317
+ type: "agent-input";
318
+ toolResults: ContentMessageItemToolResult[];
319
+ }
320
+
321
+ export interface GenerateCompletionStepPlanning {
322
+ type: "planning";
323
+ content: string;
324
+ }
325
+
326
+ export interface GenerateCompletionStepUser {
327
+ type: "user";
328
+ displayPrompt: string | undefined;
329
+ attachments?: Array<Attachment>;
330
+ id: string;
331
+ user: UserSource;
332
+ role: "user" | "agent";
333
+ }
334
+
335
+ export interface GenerateCompletionStepFile {
336
+ type: "file";
337
+ filePath: string;
338
+ content: string;
339
+ title: string;
340
+ id: string;
341
+ }
342
+
343
+ export interface GenerateCompletionStepDiff {
344
+ type: "diff";
345
+ filePath: string;
346
+ content: string;
347
+ title: string;
348
+ id: string;
349
+ }
350
+
351
+ export interface GenerateCompletionStepTool {
352
+ type: "tool";
353
+ name: string;
354
+ id: string;
355
+ content: string;
356
+ }
357
+
358
+ export interface GenerateCompletionStepText {
359
+ type: "text";
360
+ content: string;
361
+ }
362
+
363
+ export interface GenerateCompletionStepDone {
364
+ type: "done";
365
+ id: string;
366
+ applyResults: ApplyActionsResult[];
367
+ actions: ActionItem[];
368
+ usage: CodegenUsage | undefined;
369
+ url?: string;
370
+ stopReason: CompletionStopReason;
371
+ hasChanges: boolean;
372
+ }
373
+
374
+ export interface GenerateCompletionStepStart {
375
+ type: "start";
376
+ name: string;
377
+ id: string | undefined;
378
+ title: string;
379
+ content: string;
380
+ filePath?: string;
381
+ }
382
+
383
+ export interface GenerateCompletionStepDelta {
384
+ type: "delta";
385
+ name: string;
386
+ delta: string;
387
+ }
388
+
389
+ export interface GenerateCompletionStepError {
390
+ type: "error";
391
+ id?: string;
392
+ stopReason?: "error" | "limit";
393
+ metadata?: any;
394
+ message: string;
395
+ usage?: CodegenUsage;
396
+ contextPrompt?: string;
397
+ }
398
+
399
+ export interface GenerateCompletionStepContinue {
400
+ type: "continue";
401
+ id: string;
402
+ url: string;
403
+ }
404
+
405
+ export interface CodeGenHealthStatus {
406
+ devServerRunning: boolean;
407
+ devServerResponding: boolean;
408
+ logs: string | undefined;
409
+ lastInstallFailed: boolean;
410
+ checkLogs: string;
411
+ checkPassed: boolean;
412
+ prompt: string;
413
+ message: string;
414
+ }
415
+
416
+ export interface SuggestedActionBase {
417
+ level: "info" | "warning" | "error";
418
+ canAutoApply: boolean;
419
+ actionButtonText: string;
420
+ message: string;
421
+ userMessage: GenerateUserMessage;
422
+ }
423
+
424
+ export interface SuggestedActionBuildError extends SuggestedActionBase {
425
+ type: "build-error";
426
+ healthStatus: CodeGenHealthStatus;
427
+ }
428
+
429
+ export type SuggestedAction = SuggestedActionBuildError;
430
+
431
+ export interface GenerateCompletionStepWaitForInput {
432
+ type: "wait-for-input";
433
+ state: GenerateCompletionState;
434
+ suggestion?: SuggestedAction;
435
+ }
436
+
437
+ export interface GenerateCompletionStepAbort {
438
+ type: "user-abort";
439
+ }
440
+
441
+ export interface GenerateCompletionStepRestore {
442
+ type: "restore";
443
+ location: "before" | "after";
444
+ files: string[];
445
+ lastCompletionId: string | undefined;
446
+ commitHash: string | undefined;
447
+ }
448
+
449
+ export type GenerateCompletionState =
450
+ | "unknown"
451
+ | "initial-with-url"
452
+ | "initial-without-url"
453
+ | "generating"
454
+ | "success"
455
+ | "abort"
456
+ | "error"
457
+ | "close"
458
+ | "replay";
459
+
460
+ export interface GenerateCompletionStepState {
461
+ type: "state";
462
+ previousState: GenerateCompletionState;
463
+ newState: GenerateCompletionState;
464
+ }
465
+
466
+ export interface GenerateCompletionStepGit {
467
+ type: "git";
468
+ isSessionDirty: boolean;
469
+ remoteBranchExists: boolean;
470
+ canPush: boolean;
471
+ canPull: boolean;
472
+ canSync: boolean;
473
+ ahead: number;
474
+ behind: number;
475
+ currentCommit: string;
476
+ currentBranch: string;
477
+ remoteBranch: string;
478
+ }
479
+
480
+ export interface GenerateCompletionStepClose {
481
+ type: "close";
482
+ }
483
+
484
+ export interface GenerateCompletionStepStdio {
485
+ type: "stdio";
486
+ command: string;
487
+ stream: "stdout" | "stderr";
488
+ source: "run-command" | "tool-command";
489
+ content: string;
490
+ }
491
+
492
+ export interface GenerateCompletionStepSession {
493
+ type: "session";
494
+ title: string | undefined;
495
+ beforeCommit: string | undefined;
496
+ createdUnixTime: number;
497
+ updatedUnixTime: number;
498
+ id: string;
499
+ hasChanges: boolean;
500
+ }
501
+
502
+ export interface GenerateCompletionStepServerToolResult {
503
+ type: "server_tool_result";
504
+ content: any;
505
+ title: string;
506
+ id: string;
507
+ }
508
+
509
+ export type GenerateCompletionStep = { timestamp?: number } & (
510
+ | GenerateCompletionStepPlanning
511
+ | GenerateCompletionStepStart
512
+ | GenerateCompletionStepDelta
513
+ | GenerateCompletionStepUser
514
+ | GenerateCompletionStepFile
515
+ | GenerateCompletionStepDiff
516
+ | GenerateCompletionStepTool
517
+ | GenerateCompletionStepError
518
+ | GenerateCompletionStepContinue
519
+ | GenerateCompletionStepWaitForInput
520
+ | GenerateCompletionStepAbort
521
+ | GenerateCompletionStepDone
522
+ | GenerateCompletionStepUserInput
523
+ | GenerateCompletionStepText
524
+ | GenerateCompletionStepRestore
525
+ | GenerateCompletionStepState
526
+ | GenerateCompletionStepStdio
527
+ | GenerateCompletionStepSession
528
+ | GenerateCompletionStepServerToolResult
529
+ | GenerateCompletionStepGit
530
+ | GenerateCompletionStepToolResult
531
+ );
532
+
533
+ export interface ApplyActionsResult {
534
+ filePath: string;
535
+ addedLines: number;
536
+ removedLines: number;
537
+ action: "create" | "update" | "delete";
538
+ content?: string;
539
+ oldContent?: string;
540
+ }
541
+
542
+ export interface UserSourceBuilder {
543
+ source: "builder.io";
544
+ userId?: string;
545
+ userName?: string;
546
+ userEmail?: string;
547
+ role: "user";
548
+ }
549
+
550
+ export interface UserSourceGithub {
551
+ source: "github";
552
+ userName: string;
553
+ link?: string;
554
+ role: "user" | "agent";
555
+ }
556
+
557
+ export interface UserSourceAgent {
558
+ source: "agent";
559
+ role: "agent";
560
+ }
561
+
562
+ export type UserSource = UserSourceBuilder | UserSourceGithub | UserSourceAgent;
563
+
564
+ export interface GenerateUserMessage {
565
+ user?: UserSource;
566
+ userPrompt: string;
567
+ ephemeralUserPrompt?: string;
568
+ displayPrompt?: string;
569
+ files?: string[];
570
+ includeBaseFiles?: boolean;
571
+ skipSearch?: boolean;
572
+ attachments?: Attachment[];
573
+ debugMode?: boolean;
574
+ logsCheckpoint?: boolean;
575
+ dropAbortedPrompt?: boolean;
576
+ }
577
+
578
+ export interface UserInput {
579
+ userMessage: GenerateUserMessage | undefined;
580
+ userPrompt: string;
581
+ attachments: Attachment[];
582
+ files: ProjectFile[];
583
+ searchResponse: CodebaseSearchResponse | null;
584
+ rerankFiles?: number;
585
+ mostRelevantFile: string | null;
586
+ toolResults: ContentMessageItemToolResult[];
587
+ role: "user" | "agent";
588
+ }
589
+
590
+ export interface CodegenTurn {
591
+ state: "running" | "done" | "error" | "aborted" | "reverted";
592
+ unixTime: number;
593
+ completionId: string;
594
+ title: string;
595
+ nextUrl: string | undefined;
596
+ user: UserSource;
597
+ actions: ActionItem[];
598
+ sentiment: "positive" | "negative" | "undo" | undefined;
599
+ applyResults: ApplyActionsResult[];
600
+ userMessage: GenerateUserMessage | undefined;
601
+ beforeCommit: string | undefined;
602
+ afterCommit: string | undefined;
603
+ }
604
+
605
+ export interface GetSessionTurnsResult {
606
+ turns: CodegenTurn[];
607
+ sessionId: string;
608
+ initialUrl: string | undefined;
609
+ beforeCommit: string | undefined;
610
+ title: string | undefined;
611
+ createdUnixTime: number | undefined;
612
+ updatedUnixTime: number | undefined;
613
+ }
614
+
615
+ export interface CodegenFeedback {
616
+ id: string;
617
+ feedbackText?: string;
618
+ feedbackSentiment?: string;
619
+ framework?: string;
620
+ acceptedLines?: number;
621
+ afterCommit?: string;
622
+ }
623
+
624
+ export interface CodegenSetLastCompletion {
625
+ sessionId: string;
626
+ lastCompletionId: string | undefined;
627
+ }
628
+
629
+ export interface GenerateCodeEventDone {
630
+ type: "done";
631
+ unixTime: number;
632
+ stopReason: CompletionStopReason;
633
+ id: string;
634
+ actionTitle: string;
635
+ content?: string;
636
+ needsPagination: boolean;
637
+ actions?: ActionItem[];
638
+ suggestions: PromptSuggestion[];
639
+ usage?: CodegenUsage;
640
+ messageIndex: number;
641
+ sessionUsage: number;
642
+ nextUrl: string;
643
+ }
644
+
645
+ export interface GenerateCodeEventError {
646
+ type: "error";
647
+ stopReason: "error" | "limit";
648
+ id: string;
649
+ message: string;
650
+ usage?: CodegenUsage;
651
+ }
652
+ export interface GenerateCodeEventPagination {
653
+ type: "pagination";
654
+ pop: number;
655
+ page: number;
656
+ id: string;
657
+ }
658
+
659
+ export interface GenerateCodeEventContinue {
660
+ type: "continue";
661
+ id: string;
662
+ nextUrl: string;
663
+ }
664
+
665
+ export interface GenerateCodeEventDelta {
666
+ type: "delta";
667
+ content: string;
668
+ }
669
+
670
+ export interface GenerateCodeEventUser {
671
+ type: "user";
672
+ id: string;
673
+ displayPrompt: string | undefined;
674
+ user: UserSource;
675
+ /** @deprecated */
676
+ role: "user" | "agent";
677
+ }
678
+
679
+ export type GenerateCodeEvent =
680
+ | ActionItem
681
+ | GenerateCodeEventDone
682
+ | GenerateCodeEventContinue
683
+ | GenerateCodeEventPagination
684
+ | GenerateCodeEventDelta
685
+ | GenerateCodeEventError
686
+ | GenerateCodeEventUser;