@builder.io/ai-utils 0.0.74 → 0.0.75

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/fetch.js ADDED
@@ -0,0 +1,83 @@
1
+ export async function fetchJsonl(input, init, onData) {
2
+ return new Promise(async (resolve, reject) => {
3
+ try {
4
+ init = {
5
+ ...init,
6
+ headers: {
7
+ accept: "application/jsonl",
8
+ "content-type": "application/json",
9
+ ...init.headers,
10
+ },
11
+ };
12
+ const rsp = await (async () => {
13
+ try {
14
+ return await fetch(input, init);
15
+ }
16
+ catch (err) {
17
+ reject(new NetworkError(`${input}, status: network failure`));
18
+ }
19
+ })();
20
+ if (!rsp) {
21
+ return;
22
+ }
23
+ if (!rsp.ok) {
24
+ reject(new APIError(`${input}, status: ${rsp.status}`));
25
+ return;
26
+ }
27
+ if (!rsp.body) {
28
+ reject(new APIError(`${input}, missing body`));
29
+ return;
30
+ }
31
+ const reader = rsp.body.getReader();
32
+ const decoder = new TextDecoder();
33
+ let data = "";
34
+ const processStream = async () => {
35
+ const { done, value } = await reader.read();
36
+ data += decoder.decode(value, { stream: !done });
37
+ if (!done) {
38
+ let lines = data.split("\n");
39
+ if (!done) {
40
+ data = lines.pop() || "";
41
+ }
42
+ for (let line of lines) {
43
+ if (line) {
44
+ try {
45
+ line = line.trim();
46
+ if (line.startsWith("{") && line.endsWith("}")) {
47
+ onData(JSON.parse(line));
48
+ }
49
+ else {
50
+ reject(`Invalid JSONL line: ${line}`);
51
+ return;
52
+ }
53
+ }
54
+ catch (e) {
55
+ console.error(`Failed to parse JSONL: ${line}`);
56
+ reject(e);
57
+ return;
58
+ }
59
+ }
60
+ }
61
+ await processStream();
62
+ }
63
+ };
64
+ await processStream();
65
+ resolve(rsp);
66
+ }
67
+ catch (e) {
68
+ reject(e);
69
+ }
70
+ });
71
+ }
72
+ class NetworkError extends Error {
73
+ constructor(message) {
74
+ super(message);
75
+ this.name = "NetworkError";
76
+ }
77
+ }
78
+ class APIError extends Error {
79
+ constructor(message) {
80
+ super(message);
81
+ this.name = "APIError";
82
+ }
83
+ }
package/src/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export * from "./complete-json.js";
2
+ export * from "./completion.js";
3
+ export * from "./events.js";
4
+ export * from "./fetch.js";
5
+ export * from "./messages.js";
6
+ export * from "./settings.js";
7
+ export * from "./thread.js";
8
+ export * from "./mapping.js";
9
+ export * from "./codegen.js";
10
+ export * from "./vcp.js";
@@ -0,0 +1,77 @@
1
+ import type { ESMImport } from "./codegen";
2
+ export interface UserContext {
3
+ client: string;
4
+ clientVersion: string;
5
+ nodeVersion: string;
6
+ systemPlatform: string;
7
+ frameworks: string[];
8
+ systemEOL: string;
9
+ systemArch: string;
10
+ systemShell?: string;
11
+ inGitRepo?: boolean;
12
+ [key: string]: string | string[] | boolean | undefined;
13
+ }
14
+ export type ExportType = "default" | "named";
15
+ /**
16
+ * Gets the latest component mappings for a space
17
+ */
18
+ export interface FigmaMappingsData {
19
+ id: string;
20
+ figmaBuilderLinks: FigmaBuilderLink[];
21
+ version?: number;
22
+ createdDate?: string;
23
+ local: boolean;
24
+ userEmail?: string;
25
+ remoteUrl?: string;
26
+ }
27
+ export interface FigmaBuilderLink {
28
+ builderName: string;
29
+ figmaName: string;
30
+ figmaKey: string;
31
+ figmaUrl?: string;
32
+ inputMapper?: string;
33
+ originalInputMapper?: string;
34
+ exportType?: ExportType;
35
+ importName?: string;
36
+ importPath?: string;
37
+ source: string;
38
+ loc?: string;
39
+ imports?: ESMImport[];
40
+ }
41
+ export interface FigmaMapperFile {
42
+ filePath: string;
43
+ content: string;
44
+ }
45
+ export interface PublishedMapping {
46
+ figmaBuilderLinks: FigmaBuilderLink[];
47
+ mapperFiles: FigmaMapperFile[];
48
+ remoteUrl?: string;
49
+ defaultBranch?: string;
50
+ currentBranch?: string;
51
+ commit?: string;
52
+ spaceKind?: string;
53
+ userContext?: UserContext;
54
+ }
55
+ export interface FigmaComponentInfo {
56
+ documentName: string;
57
+ key: string;
58
+ tree?: string;
59
+ jsx?: string;
60
+ type: string;
61
+ name: string;
62
+ exportJson?: any;
63
+ inputs: FigmaComponentInput[];
64
+ description: string;
65
+ documentationLinks: string[];
66
+ instanceId: string;
67
+ }
68
+ export interface FigmaComponentInput {
69
+ id: string;
70
+ name: string;
71
+ value?: any;
72
+ type: string;
73
+ baseType: "text" | "variant" | "boolean" | "slot";
74
+ variantOptions?: string[];
75
+ isDefault: boolean;
76
+ ref?: string;
77
+ }
package/src/mapping.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,132 @@
1
+ import type { ContentUpdatePatch } from "./events.js";
2
+ /**
3
+ * Message param does not know the id of the message.
4
+ * This is an input message.
5
+ */
6
+ export type MessageParam = SystemMessageParam | UserMessageParam | AssistantMessageParam;
7
+ export interface ContentMessageItemText {
8
+ type: "text";
9
+ text: string;
10
+ cache?: boolean;
11
+ }
12
+ export interface ContentMessageItemImage {
13
+ type: "image";
14
+ source: ImageSource;
15
+ cache?: boolean;
16
+ }
17
+ export interface ImageSource {
18
+ type: "base64";
19
+ media_type: "image/webp" | "image/png" | "image/jpeg";
20
+ data: string;
21
+ }
22
+ export interface ContentMessageItemThinking {
23
+ type: "thinking";
24
+ thinking: string;
25
+ signature: string;
26
+ }
27
+ export interface ContentMessageItemRedactedThinking {
28
+ type: "redacted_thinking";
29
+ data: string;
30
+ }
31
+ export interface ContentMessageItemToolUse {
32
+ type: "tool_use";
33
+ id: string;
34
+ input: unknown;
35
+ name: string;
36
+ }
37
+ export type ContentMessageItem = ContentMessageItemText | ContentMessageItemImage | ContentMessageItemThinking | ContentMessageItemRedactedThinking | ContentMessageItemToolUse;
38
+ export type ContentMessage = ContentMessageItem[];
39
+ export interface SystemMessageParam {
40
+ /**
41
+ * The contents of the system message.
42
+ */
43
+ content: string | ContentMessageItemText[];
44
+ responseId?: string;
45
+ /**
46
+ * The role of the messages author, in this case `system`.
47
+ */
48
+ role: "system";
49
+ id?: string;
50
+ }
51
+ export interface UserMessageParam {
52
+ /**
53
+ * The contents of the user message.
54
+ */
55
+ content: string | (ContentMessageItemText | ContentMessageItemImage)[];
56
+ responseId?: string;
57
+ /**
58
+ * The role of the messages author, in this case `user`.
59
+ */
60
+ role: "user";
61
+ id?: string;
62
+ }
63
+ export interface AssistantMessageParam {
64
+ /**
65
+ * The contents of the assistant message.
66
+ */
67
+ content: string | ContentMessageItem[];
68
+ /**
69
+ * The role of the messages author, in this case `assistant`.
70
+ */
71
+ role: "assistant";
72
+ responseId?: string;
73
+ /**
74
+ * The function call name and arguments
75
+ */
76
+ action?: {
77
+ /**
78
+ * The specific function name
79
+ */
80
+ name: string;
81
+ /** This is arbitrary JSON */
82
+ arguments: any;
83
+ };
84
+ id?: string;
85
+ skipDelta?: boolean;
86
+ /**
87
+ * A summary of the patches which the assistant has made.
88
+ * Useful for genai.
89
+ */
90
+ patches?: ContentUpdatePatch[];
91
+ state?: "error";
92
+ }
93
+ export interface SystemMessage extends SystemMessageParam {
94
+ id: string;
95
+ }
96
+ export interface UserMessage extends UserMessageParam {
97
+ id: string;
98
+ }
99
+ export interface AssistantMessage extends AssistantMessageParam {
100
+ id: string;
101
+ status?: "accepted" | "rejected" | "aborted";
102
+ }
103
+ export interface AssistantActionMessage {
104
+ /**
105
+ * The role of the messages author, in this case `assistant`.
106
+ */
107
+ role: "assistant";
108
+ id: string;
109
+ }
110
+ /**
111
+ * Message DOES know the id of the message.
112
+ * This message is after an id has been assigned
113
+ * and is the output message.
114
+ */
115
+ export type Message = SystemMessage | UserMessage | AssistantMessage;
116
+ export type GeneratingMessage = null | Partial<AssistantActionMessage | AssistantMessage>;
117
+ export declare function getContentText(message: string | ContentMessage): string;
118
+ export declare function getContentAttachments(message: string | ContentMessage): ImageSource[];
119
+ export type Attachment = FileUpload | Template;
120
+ export interface FileUpload {
121
+ type: "upload";
122
+ contentType: string;
123
+ name: string;
124
+ dataUrl: string;
125
+ size: number;
126
+ id: string;
127
+ }
128
+ export interface Template {
129
+ type: "template";
130
+ name: string;
131
+ id: number;
132
+ }
@@ -0,0 +1,16 @@
1
+ export function getContentText(message) {
2
+ if (typeof message === "string") {
3
+ return message;
4
+ }
5
+ return message
6
+ .map((item) => (item.type === "text" ? item.text : ""))
7
+ .join("");
8
+ }
9
+ export function getContentAttachments(message) {
10
+ if (typeof message === "string") {
11
+ return [];
12
+ }
13
+ return message
14
+ .filter((item) => item.type === "image")
15
+ .map((item) => item.source);
16
+ }
@@ -0,0 +1,23 @@
1
+ export interface AssistantSettings {
2
+ assistantType?: string;
3
+ viewId?: string;
4
+ cssOverrides?: string;
5
+ messagePlaceholder: string;
6
+ showPrompt?: boolean;
7
+ theme?: "dark" | "light";
8
+ }
9
+ interface IframeSettings extends Partial<AssistantSettings> {
10
+ local?: boolean;
11
+ /**
12
+ * The URL of the assistant.
13
+ */
14
+ baseUrl?: string;
15
+ }
16
+ export declare function getAssistantUrl(opts?: IframeSettings): string;
17
+ export declare function parseAssistantUrlSettings(url: string): Partial<AssistantSettings>;
18
+ export interface BuilderEditorAuth {
19
+ spaceId: string;
20
+ userId: string;
21
+ authToken: string;
22
+ }
23
+ export {};
@@ -0,0 +1,31 @@
1
+ const urlParamSettings = [
2
+ "assistantType",
3
+ "showPrompt",
4
+ "theme",
5
+ "viewId",
6
+ ];
7
+ export function getAssistantUrl(opts = {}) {
8
+ var _a;
9
+ const url = new URL((_a = opts.baseUrl) !== null && _a !== void 0 ? _a : (opts.local ? "http://localhost:7242" : "https://assistant.builder.io"));
10
+ urlParamSettings.forEach((key) => {
11
+ const value = opts[key];
12
+ if (typeof value === "string" || typeof value === "boolean") {
13
+ url.searchParams.set(key, String(value));
14
+ }
15
+ });
16
+ return url.href;
17
+ }
18
+ export function parseAssistantUrlSettings(url) {
19
+ const parsed = new URL(url);
20
+ const settings = {};
21
+ urlParamSettings.forEach((key) => {
22
+ const value = parsed.searchParams.get(key);
23
+ if (value === "true" || value === "false") {
24
+ settings[key] = value === "true";
25
+ }
26
+ else if (value) {
27
+ settings[key] = value;
28
+ }
29
+ });
30
+ return settings;
31
+ }
@@ -0,0 +1,27 @@
1
+ import type { Message } from "./messages.js";
2
+ export interface Thread {
3
+ /**
4
+ * The unique LOCAL identifier of the thread.
5
+ */
6
+ id: string;
7
+ /**
8
+ * The unique OPENAI identifier of the thread. This is only
9
+ * set after openai creates the thread.
10
+ */
11
+ threadId?: string;
12
+ platformId?: string;
13
+ title?: string;
14
+ /**
15
+ * The type of assistant that the thread is associated with.
16
+ * For example, "content-editor" or "docs".
17
+ */
18
+ assistantType?: string;
19
+ /**
20
+ * The unique identifier of the view that the thread is associated with.
21
+ * For example, the "content-editor" assistant would use the
22
+ * builder content id as the viewId.
23
+ */
24
+ viewId?: string;
25
+ created: number;
26
+ messages: Message[];
27
+ }
package/src/thread.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/src/vcp.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { ESMImport } from "./codegen";
2
+ export interface BuilderComponentInfo {
3
+ name: string;
4
+ inputs?: BuilderComponentInput[];
5
+ path: string;
6
+ imports: {
7
+ [key: string]: string | undefined;
8
+ };
9
+ esmImports?: ESMImport[];
10
+ options?: any;
11
+ }
12
+ export interface BuilderComponentInput {
13
+ name: string;
14
+ type: string;
15
+ value: any;
16
+ }
17
+ export interface BuilderComponentSlot {
18
+ figmaId: string;
19
+ builderName: string;
20
+ }
21
+ export interface JSXNode {
22
+ "@type": "jsx";
23
+ name: string;
24
+ props: Record<string, any>;
25
+ tagName?: string;
26
+ includeAirLayoutStyles?: boolean;
27
+ path?: string;
28
+ imports?: {
29
+ [key: string]: string | undefined;
30
+ };
31
+ esmImports?: ESMImport[];
32
+ }
package/src/vcp.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Builder.io AI types and utilities
2
-
3
- Shared types between AI systems, threads, servers, etc.
package/src/codegen.ts DELETED
@@ -1,111 +0,0 @@
1
- import type { UserMessageParam } from "./messages";
2
-
3
- import type { BuilderContent } from "./completion";
4
-
5
- import type { Options as PrettierOptions } from "prettier";
6
- import type { UserContext } from "./mapping";
7
- import type { AssistantMessageParam } from "./messages";
8
-
9
- // Define the import type as a union of literal types.
10
- export type ImportType = "named" | "default";
11
-
12
- // Define an interface for the import descriptor.
13
- export interface ESMImport {
14
- importName: string; // e.g. "Button"
15
- importPath: string; // e.g. "path"
16
- importType: ImportType; // Either 'named' or 'default'
17
- }
18
-
19
- export interface ProjectFile {
20
- filePath: string;
21
- content?: string;
22
- importance?: number;
23
- dropReason?: string;
24
- wasIncluded?: boolean;
25
- }
26
-
27
- export interface CustomInstruction {
28
- id: string;
29
- name: string;
30
- content: string;
31
- }
32
-
33
- export type CodeGenFramework =
34
- | "react"
35
- | "html"
36
- | "mitosis"
37
- | "react-native"
38
- | "angular"
39
- | "vue"
40
- | "svelte"
41
- | "qwik"
42
- | "solid"
43
- | "marko"
44
- | "swiftui"
45
- | "jetpack-compose"
46
- | "flutter";
47
-
48
- export type CodeGenStyleLibrary =
49
- | "tailwind"
50
- | "tailwind-precise"
51
- | "emotion"
52
- | "styled-components"
53
- | "styled-jsx"
54
- | "react-native"
55
- | undefined;
56
-
57
- export type CodeGenStopReason = "error" | "max_tokens" | "end" | "limit";
58
-
59
- export type CodeGenMode =
60
- | "exact" // @deprecated
61
- | "precise" // tries to match the design as close
62
- | "precise_vision" // tries to match the design as close, also uses vision to match
63
- | "creative" // adapts the design to some generic design language
64
- | "creative_vision" // adapts the design to some generic design language, also uses vision to match
65
- | "creative_only_vision"; // adapts the design to some generic design language, but only uses vision to match
66
-
67
- export interface CodeGenInputOptions {
68
- position: string;
69
- eventName?: string;
70
- sessionId: string;
71
-
72
- codeGenMode?: "fast" | "quality-v2";
73
- url?: string;
74
- diffActions?: boolean;
75
- planningPrompt?: boolean;
76
- customInstructions?: CustomInstruction[];
77
- userPrompt?: string;
78
- files?: ProjectFile[];
79
- rerankFiles?: number;
80
-
81
- // Code options
82
- builderContent?: BuilderContent;
83
- framework?: CodeGenFramework;
84
- styleLibrary?: CodeGenStyleLibrary;
85
- typescript?: boolean;
86
- userContext?: UserContext;
87
-
88
- // Options
89
- maxTokens?: number;
90
- maxPages?: number;
91
- autoContinue?: number;
92
- promptCaching?: boolean;
93
- isAutoContinue?: boolean;
94
- llmSuggestions?: boolean;
95
- requestActions?: boolean;
96
- conclusionText?: boolean;
97
-
98
- searchResponse?: any | null;
99
-
100
- // Prettier options
101
- prettierConfig?: PrettierOptions;
102
-
103
- /** @deprecated */
104
- history?: (UserMessageParam | AssistantMessageParam)[];
105
- /** @deprecated */
106
- prevId?: string;
107
- /** @deprecated */
108
- nextPage?: boolean;
109
- /** @deprecated */
110
- vcpId?: string;
111
- }
@@ -1,55 +0,0 @@
1
- /**
2
- * Complete a partial JSON string with the necessary closing brackets and braces
3
- */
4
- export function completeJson(input: string): string {
5
- const stack = [];
6
- let isInString = false;
7
- let lastQuote = "";
8
-
9
- // Scan the string to determine the necessary closures
10
- for (const char of input) {
11
- switch (char) {
12
- case '"':
13
- case "'":
14
- if (isInString && char === lastQuote) {
15
- // Check for escaping
16
- if (input.charAt(input.indexOf(char) - 1) !== "\\") {
17
- isInString = false;
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
-
43
- // Close any unclosed string
44
- if (isInString) {
45
- input += lastQuote;
46
- }
47
-
48
- // Close all remaining open brackets and braces
49
- while (stack.length > 0) {
50
- const charToClose = stack.pop();
51
- input += charToClose;
52
- }
53
-
54
- return input;
55
- }