@builder.io/ai-utils 0.1.4 → 0.2.0

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,238 @@
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
+ }
17
+
18
+ export interface ContentMessageItemImage {
19
+ type: "image";
20
+ source: ImageBase64Source | ImageUrlSource;
21
+ cache?: boolean;
22
+ }
23
+
24
+ export interface ContentMessageItemDocument {
25
+ type: "document";
26
+ source: DocumentBase64Source | DocumentUrlSource;
27
+ cache?: boolean;
28
+ }
29
+
30
+ export interface DocumentBase64Source {
31
+ type: "base64";
32
+ media_type: "application/pdf";
33
+ data: string;
34
+ }
35
+
36
+ export interface DocumentUrlSource {
37
+ type: "url";
38
+ url: string;
39
+ }
40
+
41
+ export interface ContentMessageItemToolResult {
42
+ type: "tool_result";
43
+ tool_use_id: string;
44
+ tool_name?: string;
45
+ tool_input?: string;
46
+ title?: string;
47
+ content: string;
48
+ is_error: boolean;
49
+ cache?: boolean;
50
+ }
51
+
52
+ export interface ImageBase64Source {
53
+ type: "base64";
54
+ media_type: "image/webp" | "image/png" | "image/jpeg";
55
+ data: string;
56
+ }
57
+
58
+ export interface ImageUrlSource {
59
+ type: "url";
60
+ url: string;
61
+ }
62
+
63
+ export interface ContentMessageItemThinking {
64
+ type: "thinking";
65
+ thinking: string;
66
+ signature: string;
67
+ }
68
+
69
+ export interface ContentMessageItemRedactedThinking {
70
+ type: "redacted_thinking";
71
+ data: string;
72
+ }
73
+
74
+ export interface ContentMessageItemToolUse {
75
+ type: "tool_use";
76
+ id: string;
77
+ input: unknown;
78
+ completion?: string;
79
+ name: string;
80
+ }
81
+
82
+ export type ContentMessageItem =
83
+ | ContentMessageItemText
84
+ | ContentMessageItemImage
85
+ | ContentMessageItemDocument
86
+ | ContentMessageItemThinking
87
+ | ContentMessageItemRedactedThinking
88
+ | ContentMessageItemToolUse
89
+ | ContentMessageItemToolResult;
90
+
91
+ export type ContentMessage = ContentMessageItem[];
92
+
93
+ export interface SystemMessageParam {
94
+ /**
95
+ * The contents of the system message.
96
+ */
97
+ content: string | ContentMessageItemText[];
98
+
99
+ // an id to track messages across a response including multiple retries
100
+ responseId?: string;
101
+
102
+ /**
103
+ * The role of the messages author, in this case `system`.
104
+ */
105
+ role: "system";
106
+
107
+ id?: string;
108
+ }
109
+
110
+ export interface UserMessageParam {
111
+ /**
112
+ * The contents of the user message.
113
+ */
114
+ content:
115
+ | string
116
+ | (
117
+ | ContentMessageItemText
118
+ | ContentMessageItemImage
119
+ | ContentMessageItemDocument
120
+ | ContentMessageItemToolResult
121
+ )[];
122
+
123
+ // an id to track messages across a response including multiple retries
124
+ responseId?: string;
125
+
126
+ /**
127
+ * The role of the messages author, in this case `user`.
128
+ */
129
+ role: "user";
130
+ id?: string;
131
+ }
132
+
133
+ export interface AssistantMessageParam {
134
+ /**
135
+ * The contents of the assistant message.
136
+ */
137
+ content: string | ContentMessageItem[];
138
+ /**
139
+ * The role of the messages author, in this case `assistant`.
140
+ */
141
+ role: "assistant";
142
+
143
+ // an id to track messages across a response including multiple retries
144
+ responseId?: string;
145
+
146
+ /**
147
+ * The function call name and arguments
148
+ */
149
+ action?: {
150
+ /**
151
+ * The specific function name
152
+ */
153
+ name: string;
154
+ /** This is arbitrary JSON */
155
+ arguments: any;
156
+ };
157
+ id?: string;
158
+ skipDelta?: boolean;
159
+ /**
160
+ * A summary of the patches which the assistant has made.
161
+ * Useful for genai.
162
+ */
163
+ patches?: ContentUpdatePatch[];
164
+ state?: "error";
165
+ }
166
+
167
+ export interface SystemMessage extends SystemMessageParam {
168
+ id: string;
169
+ }
170
+
171
+ export interface UserMessage extends UserMessageParam {
172
+ id: string;
173
+ }
174
+
175
+ export interface AssistantMessage extends AssistantMessageParam {
176
+ id: string;
177
+ status?: "accepted" | "rejected" | "aborted";
178
+ }
179
+
180
+ export interface AssistantActionMessage {
181
+ /**
182
+ * The role of the messages author, in this case `assistant`.
183
+ */
184
+ role: "assistant";
185
+
186
+ id: string;
187
+ }
188
+
189
+ /**
190
+ * Message DOES know the id of the message.
191
+ * This message is after an id has been assigned
192
+ * and is the output message.
193
+ */
194
+ export type Message = SystemMessage | UserMessage | AssistantMessage;
195
+
196
+ export type GeneratingMessage = null | Partial<
197
+ AssistantActionMessage | AssistantMessage
198
+ >;
199
+
200
+ export function getContentText(message: string | ContentMessage) {
201
+ if (typeof message === "string") {
202
+ return message;
203
+ }
204
+ return message
205
+ .map((item) => (item.type === "text" ? item.text : ""))
206
+ .join("");
207
+ }
208
+
209
+ export function getContentAttachments(message: string | ContentMessage) {
210
+ if (typeof message === "string") {
211
+ return [];
212
+ }
213
+ return message
214
+ .filter((item) => item.type === "image")
215
+ .map((item) => item.source);
216
+ }
217
+
218
+ export type Attachment = FileUpload | Template | URL;
219
+
220
+ export interface URL {
221
+ type: "url";
222
+ value: string;
223
+ }
224
+
225
+ export interface FileUpload {
226
+ type: "upload";
227
+ contentType: string;
228
+ name: string;
229
+ dataUrl: string;
230
+ size: number;
231
+ id: string;
232
+ }
233
+
234
+ export interface Template {
235
+ type: "template";
236
+ name: string;
237
+ id: number;
238
+ }
@@ -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
+ }
package/src/codegen.d.ts DELETED
@@ -1,346 +0,0 @@
1
- import type { Attachment, ContentMessageItemToolResult, UserMessageParam } from "./messages";
2
- import type { BuilderContent } from "./completion";
3
- import type { Options as PrettierOptions } from "prettier";
4
- import type { UserContext } from "./mapping";
5
- import type { AssistantMessageParam } from "./messages";
6
- export type ImportType = "named" | "default";
7
- export interface ESMImport {
8
- importName: string;
9
- importPath: string;
10
- importType: ImportType;
11
- }
12
- export interface ProjectFile {
13
- filePath: string;
14
- content?: string;
15
- importance?: number;
16
- dropReason?: string;
17
- wasIncluded?: boolean;
18
- }
19
- export interface CustomInstruction {
20
- id: string;
21
- name: string;
22
- content: string;
23
- type?: "always" | "agent-mode" | string;
24
- glob?: string;
25
- description?: string;
26
- }
27
- export type CodeGenFramework = "react" | "html" | "mitosis" | "react-native" | "angular" | "vue" | "svelte" | "qwik" | "solid" | "marko" | "swiftui" | "jetpack-compose" | "flutter";
28
- export type CodeGenStyleLibrary = "tailwind" | "tailwind-precise" | "emotion" | "styled-components" | "styled-jsx" | "react-native" | undefined;
29
- export type CompletionStopReason = "max_tokens" | "stop_sequence" | "tool_use" | "end_turn" | "content_filter" | null;
30
- export interface ViewPathToolInput {
31
- filePath: string;
32
- viewRange?: [number, number];
33
- }
34
- export interface GlobSearchToolInput {
35
- pattern: string;
36
- }
37
- export interface GrepSearchToolInput {
38
- query: string;
39
- includeGlob?: string;
40
- excludeGlob?: string;
41
- }
42
- export interface GetRuleToolInput {
43
- name: string;
44
- type?: "always" | "agent-mode" | string;
45
- glob?: string;
46
- description?: string;
47
- }
48
- export interface CodeGenToolMap {
49
- view_path: ViewPathToolInput;
50
- glob_search: GlobSearchToolInput;
51
- grep_search: GrepSearchToolInput;
52
- get_rule: GetRuleToolInput;
53
- }
54
- export type CodeGenTools = keyof CodeGenToolMap;
55
- export type CodeGenMode = "exact" | "precise" | "precise_vision" | "creative" | "creative_vision" | "creative_only_vision";
56
- export interface CodeGenInputOptions {
57
- position: string;
58
- eventName?: string;
59
- sessionId: string;
60
- codeGenMode?: "fast" | "quality" | "quality-v3";
61
- url?: string;
62
- diffActions?: boolean;
63
- planningPrompt?: boolean;
64
- customInstructions?: CustomInstruction[];
65
- userPrompt?: string;
66
- displayUserPrompt?: string;
67
- files?: ProjectFile[];
68
- rerankFiles?: number;
69
- toolResults?: ContentMessageItemToolResult[];
70
- attachments?: Attachment[];
71
- beforeCommit?: string;
72
- builderContent?: BuilderContent;
73
- framework?: CodeGenFramework;
74
- styleLibrary?: CodeGenStyleLibrary;
75
- typescript?: boolean;
76
- userContext?: UserContext;
77
- enabledTools?: CodeGenTools[];
78
- maxTokens?: number;
79
- maxPages?: number;
80
- autoContinue?: number;
81
- promptCaching?: boolean;
82
- isAutoContinue?: boolean;
83
- llmSuggestions?: boolean;
84
- conclusionText?: boolean;
85
- searchResponse?: any | null;
86
- prettierConfig?: PrettierOptions;
87
- /** @deprecated */
88
- history?: (UserMessageParam | AssistantMessageParam)[];
89
- /** @deprecated */
90
- prevId?: string;
91
- /** @deprecated */
92
- nextPage?: boolean;
93
- /** @deprecated */
94
- vcpId?: string;
95
- }
96
- export type Feature = "component-mapping";
97
- export interface CodegenUsage {
98
- total: number;
99
- fast: number;
100
- quality: number;
101
- features: Feature[];
102
- limits: {
103
- aiGeneration: number;
104
- aiGenerationContextWindow: number;
105
- };
106
- }
107
- export interface PromptSuggestion {
108
- type: "missing-imports" | "lazy-code" | "syntax-error" | "llm-suggested" | "diff-apply";
109
- filePath?: string;
110
- line?: number;
111
- importance: "high" | "medium" | "low";
112
- column?: number;
113
- summary: string;
114
- prompt: string;
115
- }
116
- export interface ActionItem {
117
- type: "file" | "text" | "diff" | "thinking" | "tool" | "suggestion" | "tool_result";
118
- id?: string;
119
- content: string;
120
- filePath?: string;
121
- artifactTitle?: string;
122
- actionTitle?: string;
123
- synthetic?: boolean;
124
- incomplete?: boolean;
125
- suggestions?: PromptSuggestion[];
126
- errors?: string[];
127
- }
128
- export interface RepoInfo {
129
- remoteUrl: string;
130
- defaultBranch: string;
131
- currentBranch: string;
132
- commit: string;
133
- }
134
- export interface CodebaseSearchOptions {
135
- repoInfo?: RepoInfo;
136
- query: string;
137
- selectedFiles?: string[];
138
- sessionId: string;
139
- files?: string[];
140
- packageJson?: string;
141
- limit?: number;
142
- includeContent?: boolean;
143
- }
144
- export interface CodebaseSearchResponse {
145
- id: string;
146
- relevantPaths: string[];
147
- grepQueries: string[];
148
- streamMeta: any;
149
- ranked: RankedResult[];
150
- }
151
- export interface RankedResult {
152
- index: number;
153
- filePath: string;
154
- startIndex: number;
155
- endIndex: number;
156
- score: number;
157
- id: string;
158
- content?: string;
159
- }
160
- export interface GenerateCompletionStepThinking {
161
- type: "thinking";
162
- }
163
- export interface FileInfo {
164
- filePath: string;
165
- size: number;
166
- }
167
- export interface GenerateCompletionStepUserInput {
168
- type: "user-input";
169
- prompt: string;
170
- files: FileInfo[];
171
- }
172
- export interface GenerateCompletionStepToolResult {
173
- type: "agent-input";
174
- toolResults: ContentMessageItemToolResult[];
175
- }
176
- export interface GenerateCompletionStepPlanning {
177
- type: "planning";
178
- content: string;
179
- }
180
- export interface GenerateCompletionStepUser {
181
- type: "user";
182
- displayPrompt: string | undefined;
183
- id: string;
184
- role: "user" | "agent";
185
- }
186
- export interface GenerateCompletionStepFile {
187
- type: "file";
188
- filePath: string;
189
- content: string;
190
- title: string;
191
- id: string;
192
- }
193
- export interface GenerateCompletionStepDiff {
194
- type: "diff";
195
- filePath: string;
196
- content: string;
197
- title: string;
198
- id: string;
199
- }
200
- export interface GenerateCompletionStepTool {
201
- type: "tool";
202
- name: string;
203
- id: string;
204
- content: string;
205
- }
206
- export interface GenerateCompletionStepText {
207
- type: "text";
208
- content: string;
209
- }
210
- export interface GenerateCompletionStepDone {
211
- type: "done";
212
- id: string;
213
- applyResults: ApplyActionsResult[];
214
- actions: ActionItem[];
215
- usage: CodegenUsage | undefined;
216
- url?: string;
217
- stopReason: CompletionStopReason;
218
- }
219
- export interface GenerateCompletionStepStart {
220
- type: "start";
221
- name: string;
222
- id: string | undefined;
223
- title: string;
224
- content: string;
225
- }
226
- export interface GenerateCompletionStepDelta {
227
- type: "delta";
228
- name: string;
229
- delta: string;
230
- }
231
- export interface GenerateCompletionStepError {
232
- type: "error";
233
- id?: string;
234
- stopReason?: "error" | "limit";
235
- metadata?: any;
236
- message: string;
237
- usage?: CodegenUsage;
238
- }
239
- export interface GenerateCompletionStepContinue {
240
- type: "continue";
241
- id: string;
242
- url: string;
243
- }
244
- export interface GenerateCompletionStepWaitForInput {
245
- type: "wait-for-input";
246
- }
247
- export interface GenerateCompletionStepAbort {
248
- type: "user-abort";
249
- }
250
- export type GenerateCompletionStep = {
251
- timestamp?: number;
252
- } & (GenerateCompletionStepPlanning | GenerateCompletionStepStart | GenerateCompletionStepDelta | GenerateCompletionStepUser | GenerateCompletionStepFile | GenerateCompletionStepDiff | GenerateCompletionStepTool | GenerateCompletionStepError | GenerateCompletionStepContinue | GenerateCompletionStepWaitForInput | GenerateCompletionStepAbort | GenerateCompletionStepDone | GenerateCompletionStepUserInput | GenerateCompletionStepText | GenerateCompletionStepToolResult);
253
- export interface ApplyActionsResult {
254
- filePath: string;
255
- addedLines: number;
256
- removedLines: number;
257
- action: "create" | "update" | "delete";
258
- content?: string;
259
- oldContent?: string;
260
- }
261
- export interface GenerateUserMessage {
262
- userPrompt: string;
263
- displayPrompt?: string;
264
- files?: string[];
265
- includeBaseFiles?: boolean;
266
- skipSearch?: boolean;
267
- attachments?: Attachment[];
268
- }
269
- export interface UserInput {
270
- userMessage: GenerateUserMessage | undefined;
271
- userPrompt: string;
272
- attachments: Attachment[];
273
- files: ProjectFile[];
274
- searchResponse: CodebaseSearchResponse | null;
275
- rerankFiles?: number;
276
- mostRelevantFile: string | null;
277
- toolResults: ContentMessageItemToolResult[];
278
- role: "user" | "agent";
279
- }
280
- export interface CodegenTurn {
281
- state: "running" | "done" | "error" | "aborted" | "reverted";
282
- unixTime: number;
283
- completionId: string;
284
- title: string;
285
- nextUrl: string | undefined;
286
- isUserMessage: boolean;
287
- actions: ActionItem[];
288
- sentiment: "positive" | "negative" | "undo" | undefined;
289
- applyResults: ApplyActionsResult[];
290
- userMessage: GenerateUserMessage | undefined;
291
- beforeCommit: string | undefined;
292
- afterCommit: string | undefined;
293
- }
294
- export interface CodegenFeedback {
295
- id: string;
296
- feedbackText?: string;
297
- feedbackSentiment?: string;
298
- framework?: string;
299
- acceptedLines?: number;
300
- afterCommit?: string;
301
- }
302
- export interface GenerateCodeEventDone {
303
- type: "done";
304
- unixTime: number;
305
- stopReason: CompletionStopReason;
306
- id: string;
307
- actionTitle: string;
308
- content?: string;
309
- needsPagination: boolean;
310
- actions?: ActionItem[];
311
- promptSuggestions: PromptSuggestion[];
312
- usage?: CodegenUsage;
313
- messageIndex: number;
314
- willAutoContinue: boolean;
315
- sessionUsage: number;
316
- nextUrl: string;
317
- }
318
- export interface GenerateCodeEventError {
319
- type: "error";
320
- stopReason: "error" | "limit";
321
- id: string;
322
- message: string;
323
- usage?: CodegenUsage;
324
- }
325
- export interface GenerateCodeEventPagination {
326
- type: "pagination";
327
- pop: number;
328
- page: number;
329
- id: string;
330
- }
331
- export interface GenerateCodeEventContinue {
332
- type: "continue";
333
- id: string;
334
- nextUrl: string;
335
- }
336
- export interface GenerateCodeEventDelta {
337
- type: "delta";
338
- content: string;
339
- }
340
- export interface GenerateCodeEventUser {
341
- type: "user";
342
- id: string;
343
- displayPrompt: string | undefined;
344
- role: "user" | "agent";
345
- }
346
- export type GenerateCodeEvent = ActionItem | GenerateCodeEventDone | GenerateCodeEventContinue | GenerateCodeEventPagination | GenerateCodeEventDelta | GenerateCodeEventError | GenerateCodeEventUser;
package/src/codegen.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- /**
2
- * Complete a partial JSON string with the necessary closing brackets and braces
3
- */
4
- export declare function completeJson(input: string): string;
@@ -1,52 +0,0 @@
1
- /**
2
- * Complete a partial JSON string with the necessary closing brackets and braces
3
- */
4
- export function completeJson(input) {
5
- const stack = [];
6
- let isInString = false;
7
- let lastQuote = "";
8
- // Scan the string to determine the necessary closures
9
- for (const char of input) {
10
- switch (char) {
11
- case '"':
12
- case "'":
13
- if (isInString && char === lastQuote) {
14
- // Check for escaping
15
- if (input.charAt(input.indexOf(char) - 1) !== "\\") {
16
- isInString = false;
17
- }
18
- }
19
- else if (!isInString) {
20
- isInString = true;
21
- lastQuote = char;
22
- }
23
- break;
24
- case "{":
25
- case "[":
26
- if (!isInString) {
27
- stack.push(char === "{" ? "}" : "]");
28
- }
29
- break;
30
- case "}":
31
- case "]":
32
- if (!isInString) {
33
- if (stack.length > 0 && stack[stack.length - 1] === char) {
34
- stack.pop();
35
- }
36
- }
37
- break;
38
- default:
39
- break;
40
- }
41
- }
42
- // Close any unclosed string
43
- if (isInString) {
44
- input += lastQuote;
45
- }
46
- // Close all remaining open brackets and braces
47
- while (stack.length > 0) {
48
- const charToClose = stack.pop();
49
- input += charToClose;
50
- }
51
- return input;
52
- }