@builder.io/ai-utils 0.0.73 → 0.0.74

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,190 @@
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: ImageSource;
21
+ cache?: boolean;
22
+ }
23
+
24
+ export interface ImageSource {
25
+ type: "base64";
26
+ media_type: "image/webp" | "image/png" | "image/jpeg";
27
+ data: string;
28
+ }
29
+
30
+ export interface ContentMessageItemThinking {
31
+ type: "thinking";
32
+ thinking: string;
33
+ signature: string;
34
+ }
35
+
36
+ export interface ContentMessageItemRedactedThinking {
37
+ type: "redacted_thinking";
38
+ data: string;
39
+ }
40
+
41
+ export interface ContentMessageItemToolUse {
42
+ type: "tool_use";
43
+ id: string;
44
+ input: unknown;
45
+ name: string;
46
+ }
47
+
48
+ export type ContentMessageItem =
49
+ | ContentMessageItemText
50
+ | ContentMessageItemImage
51
+ | ContentMessageItemThinking
52
+ | ContentMessageItemRedactedThinking
53
+ | ContentMessageItemToolUse;
54
+
55
+ export type ContentMessage = ContentMessageItem[];
56
+
57
+ export interface SystemMessageParam {
58
+ /**
59
+ * The contents of the system message.
60
+ */
61
+ content: string | ContentMessageItemText[];
62
+
63
+ // an id to track messages across a response including multiple retries
64
+ responseId?: string;
65
+
66
+ /**
67
+ * The role of the messages author, in this case `system`.
68
+ */
69
+ role: "system";
70
+
71
+ id?: string;
72
+ }
73
+
74
+ export interface UserMessageParam {
75
+ /**
76
+ * The contents of the user message.
77
+ */
78
+ content: string | (ContentMessageItemText | ContentMessageItemImage)[];
79
+
80
+ // an id to track messages across a response including multiple retries
81
+ responseId?: string;
82
+
83
+ /**
84
+ * The role of the messages author, in this case `user`.
85
+ */
86
+ role: "user";
87
+ id?: string;
88
+ }
89
+
90
+ export interface AssistantMessageParam {
91
+ /**
92
+ * The contents of the assistant message.
93
+ */
94
+ content: string | ContentMessageItem[];
95
+ /**
96
+ * The role of the messages author, in this case `assistant`.
97
+ */
98
+ role: "assistant";
99
+
100
+ // an id to track messages across a response including multiple retries
101
+ responseId?: string;
102
+
103
+ /**
104
+ * The function call name and arguments
105
+ */
106
+ action?: {
107
+ /**
108
+ * The specific function name
109
+ */
110
+ name: string;
111
+ /** This is arbitrary JSON */
112
+ arguments: any;
113
+ };
114
+ id?: string;
115
+ skipDelta?: boolean;
116
+ /**
117
+ * A summary of the patches which the assistant has made.
118
+ * Useful for genai.
119
+ */
120
+ patches?: ContentUpdatePatch[];
121
+ state?: "error";
122
+ }
123
+
124
+ export interface SystemMessage extends SystemMessageParam {
125
+ id: string;
126
+ }
127
+
128
+ export interface UserMessage extends UserMessageParam {
129
+ id: string;
130
+ }
131
+
132
+ export interface AssistantMessage extends AssistantMessageParam {
133
+ id: string;
134
+ status?: "accepted" | "rejected" | "aborted";
135
+ }
136
+
137
+ export interface AssistantActionMessage {
138
+ /**
139
+ * The role of the messages author, in this case `assistant`.
140
+ */
141
+ role: "assistant";
142
+
143
+ id: string;
144
+ }
145
+
146
+ /**
147
+ * Message DOES know the id of the message.
148
+ * This message is after an id has been assigned
149
+ * and is the output message.
150
+ */
151
+ export type Message = SystemMessage | UserMessage | AssistantMessage;
152
+
153
+ export type GeneratingMessage = null | Partial<
154
+ AssistantActionMessage | AssistantMessage
155
+ >;
156
+
157
+ export function getContentText(message: string | ContentMessage) {
158
+ if (typeof message === "string") {
159
+ return message;
160
+ }
161
+ return message
162
+ .map((item) => (item.type === "text" ? item.text : ""))
163
+ .join("");
164
+ }
165
+
166
+ export function getContentAttachments(message: string | ContentMessage) {
167
+ if (typeof message === "string") {
168
+ return [];
169
+ }
170
+ return message
171
+ .filter((item) => item.type === "image")
172
+ .map((item) => item.source);
173
+ }
174
+
175
+ export type Attachment = FileUpload | Template;
176
+
177
+ export interface FileUpload {
178
+ type: "upload";
179
+ contentType: string;
180
+ name: string;
181
+ dataUrl: string;
182
+ size: number;
183
+ id: string;
184
+ }
185
+
186
+ export interface Template {
187
+ type: "template";
188
+ name: string;
189
+ id: number;
190
+ }
@@ -0,0 +1,63 @@
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
+
10
+ interface IframeSettings extends Partial<AssistantSettings> {
11
+ local?: boolean;
12
+ /**
13
+ * The URL of the assistant.
14
+ */
15
+ baseUrl?: string;
16
+ }
17
+
18
+ type AssistantSettingsKeys = keyof AssistantSettings;
19
+
20
+ const urlParamSettings: AssistantSettingsKeys[] = [
21
+ "assistantType",
22
+ "showPrompt",
23
+ "theme",
24
+ "viewId",
25
+ ];
26
+
27
+ export function getAssistantUrl(opts: IframeSettings = {}) {
28
+ const url = new URL(
29
+ opts.baseUrl ??
30
+ (opts.local ? "http://localhost:7242" : "https://assistant.builder.io"),
31
+ );
32
+
33
+ urlParamSettings.forEach((key) => {
34
+ const value = opts[key];
35
+ if (typeof value === "string" || typeof value === "boolean") {
36
+ url.searchParams.set(key, String(value));
37
+ }
38
+ });
39
+
40
+ return url.href;
41
+ }
42
+
43
+ export function parseAssistantUrlSettings(url: string) {
44
+ const parsed = new URL(url);
45
+ const settings: Record<string, string | boolean> = {};
46
+
47
+ urlParamSettings.forEach((key) => {
48
+ const value = parsed.searchParams.get(key);
49
+ if (value === "true" || value === "false") {
50
+ settings[key] = value === "true";
51
+ } else if (value) {
52
+ settings[key] = value;
53
+ }
54
+ });
55
+
56
+ return settings as Partial<AssistantSettings>;
57
+ }
58
+
59
+ export interface BuilderEditorAuth {
60
+ spaceId: string;
61
+ userId: string;
62
+ authToken: string;
63
+ }
package/src/thread.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { Message } from "./messages.js";
2
+
3
+ export interface Thread {
4
+ /**
5
+ * The unique LOCAL identifier of the thread.
6
+ */
7
+ id: string;
8
+ /**
9
+ * The unique OPENAI identifier of the thread. This is only
10
+ * set after openai creates the thread.
11
+ */
12
+ threadId?: string;
13
+ platformId?: string;
14
+ title?: string;
15
+ /**
16
+ * The type of assistant that the thread is associated with.
17
+ * For example, "content-editor" or "docs".
18
+ */
19
+ assistantType?: string;
20
+ /**
21
+ * The unique identifier of the view that the thread is associated with.
22
+ * For example, the "content-editor" assistant would use the
23
+ * builder content id as the viewId.
24
+ */
25
+ viewId?: string;
26
+ created: number;
27
+ messages: Message[];
28
+ }
package/src/vcp.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { ESMImport } from "./codegen";
2
+
3
+ export interface BuilderComponentInfo {
4
+ name: string;
5
+ inputs?: BuilderComponentInput[];
6
+ path: string;
7
+ imports: { [key: string]: string | undefined };
8
+ esmImports?: ESMImport[];
9
+ options?: any;
10
+ }
11
+
12
+ export interface BuilderComponentInput {
13
+ name: string;
14
+ type: string;
15
+ value: any;
16
+ }
17
+
18
+ export interface BuilderComponentSlot {
19
+ figmaId: string;
20
+ builderName: string;
21
+ }
22
+
23
+ export interface JSXNode {
24
+ "@type": "jsx";
25
+ name: string;
26
+ props: Record<string, any>;
27
+ tagName?: string;
28
+ includeAirLayoutStyles?: boolean;
29
+ path?: string;
30
+ imports?: { [key: string]: string | undefined };
31
+ esmImports?: ESMImport[];
32
+ }
@@ -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
- }
@@ -1,154 +0,0 @@
1
- import type { BuilderContent, BuilderElement, Component } from "@builder.io/sdk";
2
- import type { Message, MessageParam, Attachment } from "./messages.js";
3
- import type { BuilderModel } from "./events.js";
4
- export type BuilderContentData = BuilderContent["data"];
5
- export type { BuilderContent, BuilderElement, Component };
6
- export interface CompletionOptions {
7
- /**
8
- * How this assistant is being used. For example, "content-editor"
9
- * is used when the assistant is being used in the Builder.io content editor.
10
- */
11
- assistantType?: string;
12
- /**
13
- * LLM Model identifier to lookup which sdk and model to use.
14
- * If not provided the default model will be used.
15
- * Note: This is not the Builder.io data model.
16
- */
17
- modelId?: string;
18
- /**
19
- * The unique LOCAL identifier of the thread to submit the user message to.
20
- * This is used to identify the thread with our own id (openai assistant id is different)
21
- */
22
- id: string;
23
- /**
24
- * The unique OPENAI assistant id of the thread to submit the user message to.
25
- * If the assistantThreadId is not provided, a new thread will be created.
26
- */
27
- assistantThreadId?: string;
28
- /**
29
- * The message history of the conversation, user prompt then assistant response.
30
- * The messages are ordered from oldest to newest. The last message in the array
31
- * is the message that the user has just sent.
32
- */
33
- messages: (MessageParam | Message)[];
34
- /**
35
- * Which platform (framework) the the user has choosen to get help with.
36
- */
37
- platformId?: string;
38
- /**
39
- * The user id of the user sending the message.
40
- * This is used to track the user's progress in the conversation.
41
- * User id is stored in localStorage for this domain, its not the builder user id.
42
- */
43
- userId: string;
44
- /**
45
- * The state of the builder editor.
46
- */
47
- builderState?: BuilderEditorState;
48
- /**
49
- * Additional console logs
50
- */
51
- debug?: boolean;
52
- /**
53
- * Date.now() timestamp of when the assistant was started.
54
- * This is used to calculate the time taken between all events.
55
- */
56
- startMs?: number;
57
- /**
58
- * If events should be streamed back throughout the process.
59
- *
60
- * Setting to `false` will skip any intermediate processing and emitting
61
- * events while collecting LLM deltas, but will only emit the final result.
62
- *
63
- * Defaults to `true`.
64
- */
65
- stream?: boolean;
66
- /**
67
- * Provide a system prompt id to lookup a pre-defined system prompt
68
- * text sent to the LLM. Must be a pre-defined system prompt id!
69
- */
70
- systemPromptIds?: {
71
- "content-edit"?: string;
72
- };
73
- /**
74
- * Option on how this call should handle the conversation thread.
75
- *
76
- * `persist`:
77
- * - When there is no CompletionOptions `id`, it'll create a new thread id
78
- * - When given an existing `id`, it'll look up the past thread messages and prefix the messages
79
- *
80
- * `ephemeral`:
81
- * - Creates a new conversation each time, ignoring any existing thread history
82
- * - Will not save the conversation for future use
83
- *
84
- * Defaults to `persist`
85
- */
86
- thread?: "persist" | "ephemeral";
87
- attachments?: Attachment[];
88
- }
89
- export interface BuilderEditorState {
90
- /**
91
- * The active locale of the builder editor.
92
- */
93
- activeLocale?: string;
94
- /**
95
- * The locale of the provided builder content.
96
- */
97
- contentLocale?: string;
98
- /**
99
- * Top level Builder content. The data.blocks array contains the BuilderElement.
100
- */
101
- content?: BuilderContent;
102
- /**
103
- * Builder custom components.
104
- */
105
- components?: Component[];
106
- /**
107
- * Builder design tokens.
108
- */
109
- designTokens?: Record<string, string>;
110
- /**
111
- * Builder model. (not the LLM model)
112
- */
113
- model?: BuilderModel;
114
- /**
115
- * Other models in the Builder account
116
- */
117
- otherModels?: BuilderModel[];
118
- /**
119
- * The URL that the Builder content preview is pointing to.
120
- */
121
- previewUrl?: string;
122
- /**
123
- * Selected ids in the builder.
124
- */
125
- selectedIds?: string[];
126
- /**
127
- * Builder space id.
128
- */
129
- spaceId?: string;
130
- /**
131
- * Builder user id.
132
- */
133
- userId?: string;
134
- /**
135
- * Email of the user in the builder.
136
- */
137
- userEmail?: string;
138
- /**
139
- * User's jobs
140
- */
141
- userJobs?: string[];
142
- /**
143
- * Builder session id.
144
- */
145
- sessionId?: string;
146
- /**
147
- * Custom instructions that could be add into the prompt.
148
- */
149
- customInstructions?: Record<string, string>;
150
- /**
151
- * All targeting attributes of the content.
152
- */
153
- allTargetingAttributes?: Record<string, any>;
154
- }
package/src/completion.js DELETED
@@ -1 +0,0 @@
1
- export {};