@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.
package/src/events.d.ts DELETED
@@ -1,402 +0,0 @@
1
- import type { BuilderContent, BuilderElement } from "@builder.io/sdk";
2
- import type { AssistantMessage } 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 | AppPromptTextEvent | AppReadyEvent | AppSettingsResetEvent | AppSettingsSetEvent | AppThreadNewEvent | AssistantStatsEvent | BuilderEditorAuthEvent | BuilderEditorStateEvent | ContentUpdateEvent | ContentApplySnapshotEvent | ModelCreateEvent | ModelUpdateEvent | ModelUndoEvent | ModelRedoEvent | ResultEvent | ThreadCreatedEvent | ThreadMessageCompletedEvent | ThreadMessageCreatedEvent | ThreadMessageDeltaEvent | ThreadMessageFeedbackEvent | ThreadRunRequiresActionEvent | ThreadRunStepCreatedEvent | ThreadRunStepDeltaEvent | AppAcceptChangeEvent | AppAcceptRejectEvent | AssistantTrackEvent | AssistantEditorAuthMessage | AppAttachmentTemplateEvent;
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
- }
18
- export interface AssistantErrorEvent {
19
- type: "assistant.error";
20
- data: AssistantError;
21
- }
22
- export interface AssistantStreamErrorEvent {
23
- type: "assistant.stream.error";
24
- data: AssistantError;
25
- }
26
- export interface AssistantTrackEvent {
27
- type: "assistant.track";
28
- data: {
29
- name: string;
30
- properties: Record<string, any>;
31
- };
32
- }
33
- export interface AppCloseEvent {
34
- type: "assistant.app.close";
35
- }
36
- export interface AppMessagesClickEvent {
37
- type: "assistant.app.messages.click";
38
- }
39
- export interface AppAcceptChangeEvent {
40
- type: "assistant.app.change.accept";
41
- }
42
- export interface AppAcceptRejectEvent {
43
- type: "assistant.app.change.reject";
44
- }
45
- export interface AppMessageEditCustomInstructionsEvent {
46
- type: "assistant.app.messages.editCustomInstructions";
47
- }
48
- export interface AppAttachmentTemplateEvent {
49
- type: "assistant.app.attachment.template";
50
- data: {
51
- id: number;
52
- name: string;
53
- };
54
- }
55
- export interface AppMessagesGenerationEvent {
56
- type: "assistant.app.messages.generation";
57
- data: {
58
- state: GenerationState;
59
- };
60
- }
61
- /**
62
- * idle: no messages are being generated or queued to be generated
63
- *
64
- * queued: messages have been sent to the LLM, but no response has been received yet
65
- *
66
- * generating: messages are actively being generated and streaming back to the UI
67
- */
68
- export type GenerationState = "idle" | "queued" | "generating";
69
- export interface AppPromptAbortEvent {
70
- type: "assistant.app.prompt.abort";
71
- }
72
- export interface AppPromptFocusEvent {
73
- type: "assistant.app.prompt.focus";
74
- }
75
- export interface AppPromptTextEvent {
76
- type: "assistant.app.prompt.text";
77
- data: {
78
- text: string;
79
- };
80
- }
81
- export interface AppReadyEvent {
82
- type: "assistant.app.ready";
83
- }
84
- export interface AppSettingsResetEvent {
85
- type: "assistant.app.settings.reset";
86
- }
87
- export interface AppSettingsSetEvent {
88
- type: "assistant.app.settings.set";
89
- data: Partial<AssistantSettings>;
90
- }
91
- export interface AppThreadNewEvent {
92
- type: "assistant.app.thread.new";
93
- }
94
- export interface AssistantEditorAuthMessage {
95
- type: "assistant.editor.auth.update";
96
- }
97
- export interface BuilderEditorAuthEvent extends AwaitResultEvent {
98
- type: "assistant.editor.auth";
99
- }
100
- export interface BuilderEditorStateEvent extends AwaitResultEvent {
101
- type: "assistant.editor.state";
102
- }
103
- export interface AwaitResultEvent {
104
- resolveId?: string;
105
- }
106
- export interface ResultEvent {
107
- type: "assistant.result";
108
- resolveId: string;
109
- data: any;
110
- }
111
- export interface ContentCreatePatch {
112
- parentId: string;
113
- insertBeforeId: string;
114
- element: BuilderElement;
115
- }
116
- export interface ContentApplySnapshot {
117
- /**
118
- * The id of the BuilderContent to apply the snapshot to
119
- */
120
- id: string;
121
- /**
122
- * Each snapshot can be either a full BuilderContent or individual BuilderElements.
123
- * Order matters, as the snapshots will be applied in the order they are listed.
124
- * The builder app will handle the logic of applying the snapshots in the correct order
125
- * and to the right content/elements.
126
- */
127
- snapshots: (BuilderElement | BuilderContent)[];
128
- }
129
- export interface ContentApplySnapshotEvent {
130
- type: "assistant.content.applysnapshot";
131
- data: ContentApplySnapshot;
132
- }
133
- export interface ContentUpdateEvent {
134
- type: "assistant.content.update";
135
- data: ContentUpdatePatch[];
136
- }
137
- export type ContentUpdatePatch = ContentTsCodePatch | ContentInsertBeforePatch | ContentInsertAfterPatch | ContentPropsUpdatePatch | ContentSetChildrenPatch | ContentStyleUpdatePatch | ContentDeletePatch | ContentSetStorePatch | ContentSetMountPatch | ContentMovePatch | ContentMoveAfterPatch | ContentMoveBeforePatch | ContentSetTagNamePatch | ContentTsUpdateComponentPatch;
138
- interface ContentPatchBase {
139
- id: string;
140
- nodeId?: string;
141
- builderId?: string;
142
- description?: string;
143
- value: string;
144
- displayValue?: string;
145
- ts: number;
146
- /**
147
- * A change value is considered incomplete until we also parsed it's closing xml tag.
148
- */
149
- incomplete?: boolean;
150
- /**
151
- * If there was an error applying the patch, this will contain the error message.
152
- */
153
- error?: string;
154
- }
155
- export interface ContentTsUpdateComponentPatch extends ContentPatchBase {
156
- type: "update_component";
157
- }
158
- export interface ContentTsCodePatch extends ContentPatchBase {
159
- type: "ts_code";
160
- }
161
- export interface ContentSetTagNamePatch extends ContentPatchBase {
162
- type: "set_tag_name";
163
- }
164
- export interface ContentInsertBeforePatch extends ContentPatchBase {
165
- type: "insert_before";
166
- snapshotBuilderId?: string;
167
- }
168
- export interface ContentInsertAfterPatch extends ContentPatchBase {
169
- type: "insert_after";
170
- snapshotBuilderId?: string;
171
- }
172
- export interface ContentPropsUpdatePatch extends ContentPatchBase {
173
- type: "props";
174
- }
175
- export interface ContentStyleUpdatePatch extends ContentPatchBase {
176
- type: "style";
177
- }
178
- export interface ContentSetChildrenPatch extends ContentPatchBase {
179
- type: "children";
180
- snapshotBuilderId?: string;
181
- }
182
- export interface ContentDeletePatch extends ContentPatchBase {
183
- type: "delete";
184
- }
185
- export interface ContentSetStorePatch extends ContentPatchBase {
186
- type: "store";
187
- }
188
- export interface ContentSetMountPatch extends ContentPatchBase {
189
- type: "mount";
190
- }
191
- export interface ContentMovePatch extends ContentPatchBase {
192
- type: "move";
193
- }
194
- export interface ContentMoveAfterPatch extends ContentPatchBase {
195
- type: "move_after";
196
- }
197
- export interface ContentMoveBeforePatch extends ContentPatchBase {
198
- type: "move_before";
199
- }
200
- export interface AssistantStatsEvent {
201
- type: "assistant.stats";
202
- data: AssistantStats;
203
- }
204
- export interface AssistantStats {
205
- /**
206
- * The unique id of the thread (not the openai threadId)
207
- */
208
- threadId: string;
209
- /**
210
- * The unique id of the completion, which is a combination of the user's prompt and assistant's response.
211
- */
212
- completionId: string;
213
- /**
214
- * The model id used to generate this completion.
215
- */
216
- modelId: string;
217
- /**
218
- * The assistant's response message.
219
- */
220
- assistantMessage: string;
221
- /**
222
- * The user's prompt message.
223
- */
224
- userMessage: string;
225
- /**
226
- * The index within the thread the assistant message is.
227
- * For a first assistant message, the index will be 1 (the user message is index 0).
228
- * For a second assistant message, the index will be 3 (the user message is index 2), and so on.
229
- */
230
- assistantMessageIndex: number;
231
- /**
232
- * The timestamp (Date.now()) of when the user first submitted their prompt.
233
- */
234
- userPromptMs: number;
235
- /**
236
- * The timestamp of the first assistant chunk in the response.
237
- */
238
- firstChunkMs: number;
239
- /**
240
- * The timestamp of the last assistant chunk in the response.
241
- */
242
- lastChunkMs: number;
243
- /**
244
- * The total number of chunks in the assistant's streamed response.
245
- */
246
- chunkCount: number;
247
- /**
248
- * The total number of characters in the generated prompt sent to the LLM.
249
- */
250
- promptLength: number;
251
- /**
252
- * The total number of characters in the assistant's response.
253
- */
254
- completionLength: number;
255
- /**
256
- * If the user provided custom instructions for the prompt.
257
- */
258
- hasCustomInstructions: boolean;
259
- /**
260
- * The deployed version.
261
- */
262
- version: string;
263
- /**
264
- * Error message if there was one.
265
- */
266
- errorMessage?: string;
267
- /**
268
- * Input tokens
269
- */
270
- inputTokens?: number;
271
- /**
272
- * Output tokens
273
- */
274
- outputTokens?: number;
275
- /**
276
- * Output tokens
277
- */
278
- completionCost?: number;
279
- /**
280
- * Number of streamed snapshots
281
- */
282
- streamedSnapshots?: number;
283
- /**
284
- * Number of cached input tokens
285
- */
286
- cacheInputTokens?: number;
287
- /**
288
- * Number of cached created tokens
289
- */
290
- cacheCreatedTokens?: number;
291
- }
292
- export interface ModelCreateEvent {
293
- type: "assistant.model.create";
294
- data: BuilderModel;
295
- }
296
- export interface ModelUpdateEvent {
297
- type: "assistant.model.update";
298
- data: ModelUpdate;
299
- }
300
- export interface ModelUndoEvent {
301
- type: "assistant.model.undo";
302
- }
303
- export interface ModelRedoEvent {
304
- type: "assistant.model.redo";
305
- }
306
- export interface ModelUpdate {
307
- patches: ModelPatch[];
308
- }
309
- export interface BuilderModel {
310
- name?: string;
311
- friendlyName?: string;
312
- description?: string;
313
- type?: string;
314
- fields?: BuilderModelField[];
315
- }
316
- export interface BuilderModelField {
317
- name?: string;
318
- type?: string;
319
- description?: string;
320
- }
321
- export type ModelPatch = {
322
- op: "add" | "remove" | "replace";
323
- path: string;
324
- value?: any;
325
- };
326
- export interface ThreadMessageFeedbackEvent {
327
- type: "assistant.thread.message.feedback";
328
- data: CompletionResponseFeedback;
329
- }
330
- export interface CompletionResponseFeedback {
331
- userId: string;
332
- builderUserId?: string;
333
- builderEmail?: string;
334
- responseId?: string;
335
- frontendUrl: string | undefined;
336
- frontendCommitId: string | undefined;
337
- backendDomain?: string;
338
- backendCommitId: string | undefined;
339
- feedbackText: string;
340
- sentiment?: "positive" | "negative";
341
- }
342
- export interface ThreadCreatedEvent {
343
- type: "assistant.thread.created";
344
- data: ThreadCreated;
345
- }
346
- export interface ThreadCreated {
347
- platformId: string;
348
- threadId: string;
349
- vectorStoreId: string;
350
- }
351
- export interface ThreadMessageCreatedEvent {
352
- type: "assistant.thread.message.created";
353
- data: ThreadMessageCreated;
354
- }
355
- export interface ThreadMessageCreated {
356
- id: string;
357
- responseId: string;
358
- threadId: string;
359
- }
360
- export interface ThreadMessageDeltaEvent {
361
- type: "assistant.thread.message.delta";
362
- data: ThreadMessageDelta;
363
- }
364
- export interface ThreadMessageDelta {
365
- id: string;
366
- text: string;
367
- }
368
- export interface ThreadMessageCompletedEvent {
369
- type: "assistant.thread.message.completed";
370
- data: ThreadMessageCompleted;
371
- }
372
- export interface ThreadMessageCompleted extends AssistantMessage {
373
- platformId: string;
374
- threadId: string;
375
- commitId?: string;
376
- }
377
- export interface ThreadRunRequiresActionEvent {
378
- type: "assistant.thread.run.requires_action";
379
- data: ThreadRunRequiredAction;
380
- }
381
- export interface ThreadRunRequiredAction {
382
- platformId: string;
383
- threadId: string;
384
- action: {
385
- name: string;
386
- arguments: string;
387
- };
388
- }
389
- export interface ThreadRunStepDeltaEvent {
390
- type: "assistant.thread.run.step.delta";
391
- data: ThreadMessageStepDelta;
392
- }
393
- export interface ThreadMessageStepDelta {
394
- delta: any;
395
- }
396
- export interface ThreadRunStepCreatedEvent {
397
- type: "assistant.thread.run.step.created";
398
- }
399
- export type DeepPartial<T> = T extends object ? {
400
- [P in keyof T]?: DeepPartial<T[P]>;
401
- } : T;
402
- export {};
package/src/events.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/src/fetch.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function fetchJsonl<T>(input: string | URL, init: RequestInit, onData: (ev: T) => void): Promise<Response>;
package/src/fetch.js DELETED
@@ -1,83 +0,0 @@
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 DELETED
@@ -1,7 +0,0 @@
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";
package/src/messages.d.ts DELETED
@@ -1,130 +0,0 @@
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 | (ContentMessageItemThinking | ContentMessageItemRedactedThinking | ContentMessageItemText | ContentMessageItemToolUse)[];
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 type Attachment = FileUpload | Template;
119
- export interface FileUpload {
120
- type: string;
121
- name: string;
122
- dataUrl: string;
123
- size: number;
124
- id: number;
125
- }
126
- export interface Template {
127
- type: "template";
128
- name: string;
129
- id: number;
130
- }
package/src/messages.js DELETED
@@ -1,8 +0,0 @@
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
- }
package/src/settings.d.ts DELETED
@@ -1,23 +0,0 @@
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 {};
package/src/settings.js DELETED
@@ -1,31 +0,0 @@
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
- }