@blinkk/root-cms 2.4.9 → 2.4.10
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 +17 -3
- package/dist/ai-DRQJXU4N.js +13 -0
- package/dist/altText-RDKJNVGH.js +7 -0
- package/dist/app.js +9 -269
- package/dist/chunk-377TGNX2.js +82 -0
- package/dist/chunk-62EVNFXB.js +1883 -0
- package/dist/chunk-MLKGABMK.js +9 -0
- package/dist/chunk-RYF3UTHQ.js +302 -0
- package/dist/chunk-SI44FG3H.js +136 -0
- package/dist/chunk-Y65VGJLE.js +193 -0
- package/dist/chunk-ZOEPOKGE.js +294 -0
- package/dist/cli.js +54 -362
- package/dist/{client-pSzji9ZN.d.ts → client-PhodvL2Q.d.ts} +32 -2
- package/dist/client.d.ts +2 -1
- package/dist/client.js +15 -1509
- package/dist/core.d.ts +2 -2
- package/dist/core.js +24 -1519
- package/dist/edit-XX3LAGK6.js +7 -0
- package/dist/functions.js +8 -1632
- package/dist/generate-types-TQBCE2SG.js +9 -0
- package/dist/plugin.d.ts +2 -1
- package/dist/plugin.js +62 -2521
- package/dist/project.js +6 -76
- package/dist/richtext.js +2 -0
- package/dist/ui/ui.css +1 -1
- package/dist/ui/ui.js +234 -154
- package/dist/ui/ui.js.LEGAL.txt +124 -102
- package/package.json +5 -5
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// core/ai.ts
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { vertexAI } from "@genkit-ai/google-genai";
|
|
6
|
+
import { Timestamp } from "firebase-admin/firestore";
|
|
7
|
+
import { genkit } from "genkit";
|
|
8
|
+
import { logger } from "genkit/logging";
|
|
9
|
+
logger.setLogLevel("warn");
|
|
10
|
+
var DEFAULT_MODEL = "gemini-3-flash-preview";
|
|
11
|
+
var DEFAULT_IMAGEGEN_MODEL = "gemini-2.5-flash-image";
|
|
12
|
+
var LEGACY_MODEL_RENAME = {
|
|
13
|
+
"vertexai/gemini-2.5-flash": "gemini-2.5-flash",
|
|
14
|
+
"vertexai/gemini-2.0-pro": "gemini-2.0-pro"
|
|
15
|
+
};
|
|
16
|
+
async function summarizeDiff(cmsClient, options) {
|
|
17
|
+
const cmsPluginOptions = cmsClient.cmsPlugin.getConfig();
|
|
18
|
+
const firebaseConfig = cmsPluginOptions.firebaseConfig;
|
|
19
|
+
const model = (typeof cmsPluginOptions.experiments?.ai === "object" ? cmsPluginOptions.experiments.ai.model : void 0) || DEFAULT_MODEL;
|
|
20
|
+
const ai = genkit({
|
|
21
|
+
plugins: [
|
|
22
|
+
vertexAI({
|
|
23
|
+
projectId: firebaseConfig.projectId,
|
|
24
|
+
location: firebaseConfig.location || "us-central1"
|
|
25
|
+
})
|
|
26
|
+
]
|
|
27
|
+
});
|
|
28
|
+
const beforeJson = JSON.stringify(options.before ?? null, null, 2);
|
|
29
|
+
const afterJson = JSON.stringify(options.after ?? null, null, 2);
|
|
30
|
+
const systemPrompt = [
|
|
31
|
+
"You are an assistant that summarizes changes made to CMS documents stored as JSON.",
|
|
32
|
+
"Provide a concise description of the most important updates using short bullet points.",
|
|
33
|
+
'If there are no meaningful differences, respond with "No significant changes."',
|
|
34
|
+
`Focus on just the content changes, ignore insignificant changes to richtext blocks and structure, such as updates to the richtext block's "timestamp" and "version" fields.`
|
|
35
|
+
].join("\n");
|
|
36
|
+
const diffPrompt = [
|
|
37
|
+
"Previous version JSON:",
|
|
38
|
+
"```json",
|
|
39
|
+
beforeJson,
|
|
40
|
+
"```",
|
|
41
|
+
"",
|
|
42
|
+
"Updated version JSON:",
|
|
43
|
+
"```json",
|
|
44
|
+
afterJson,
|
|
45
|
+
"```",
|
|
46
|
+
"",
|
|
47
|
+
"Summarize the differences between the two payloads."
|
|
48
|
+
].join("\n");
|
|
49
|
+
const res = await generate(ai, model, {
|
|
50
|
+
messages: [
|
|
51
|
+
{
|
|
52
|
+
role: "system",
|
|
53
|
+
content: [{ text: systemPrompt }]
|
|
54
|
+
}
|
|
55
|
+
],
|
|
56
|
+
prompt: [{ text: diffPrompt }]
|
|
57
|
+
});
|
|
58
|
+
return res.text?.trim() || "";
|
|
59
|
+
}
|
|
60
|
+
async function generateImage(cmsClient, options) {
|
|
61
|
+
const cmsPluginOptions = cmsClient.cmsPlugin.getConfig();
|
|
62
|
+
const firebaseConfig = cmsPluginOptions.firebaseConfig;
|
|
63
|
+
const model = options.model || DEFAULT_IMAGEGEN_MODEL;
|
|
64
|
+
const ai = genkit({
|
|
65
|
+
plugins: [
|
|
66
|
+
vertexAI({
|
|
67
|
+
projectId: firebaseConfig.projectId,
|
|
68
|
+
location: firebaseConfig.location || "us-central1"
|
|
69
|
+
})
|
|
70
|
+
]
|
|
71
|
+
});
|
|
72
|
+
const res = await generate(ai, model, {
|
|
73
|
+
prompt: [
|
|
74
|
+
{
|
|
75
|
+
text: options.prompt
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
config: {
|
|
79
|
+
imageConfig: {
|
|
80
|
+
aspectRatio: options.aspectRatio
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
output: {
|
|
84
|
+
format: "media"
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (!res.media) {
|
|
88
|
+
throw new Error("No image generated");
|
|
89
|
+
}
|
|
90
|
+
return res.media.url;
|
|
91
|
+
}
|
|
92
|
+
async function generate(ai, model, options) {
|
|
93
|
+
const generateOptions = {
|
|
94
|
+
...options,
|
|
95
|
+
model: vertexAI.model(model)
|
|
96
|
+
};
|
|
97
|
+
if (model.startsWith("gemini-3")) {
|
|
98
|
+
generateOptions.config = {
|
|
99
|
+
...generateOptions.config,
|
|
100
|
+
location: "global"
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return ai.generate(generateOptions);
|
|
104
|
+
}
|
|
105
|
+
var Chat = class {
|
|
106
|
+
constructor(chatClient, id, options) {
|
|
107
|
+
this.chatClient = chatClient;
|
|
108
|
+
this.cmsClient = chatClient.cmsClient;
|
|
109
|
+
this.cmsPluginOptions = this.cmsClient.cmsPlugin.getConfig();
|
|
110
|
+
this.id = id;
|
|
111
|
+
this.history = options?.history ?? [];
|
|
112
|
+
this.model = cleanModelName(
|
|
113
|
+
options?.model || (typeof this.cmsPluginOptions.experiments?.ai === "object" ? this.cmsPluginOptions.experiments.ai.model : void 0) || DEFAULT_MODEL
|
|
114
|
+
);
|
|
115
|
+
const firebaseConfig = this.cmsPluginOptions.firebaseConfig;
|
|
116
|
+
this.ai = genkit({
|
|
117
|
+
plugins: [
|
|
118
|
+
vertexAI({
|
|
119
|
+
projectId: firebaseConfig.projectId,
|
|
120
|
+
location: firebaseConfig.location || "us-central1"
|
|
121
|
+
})
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/** Builds the messages for the AI request. */
|
|
126
|
+
async buildMessages(options) {
|
|
127
|
+
const messages = this.history;
|
|
128
|
+
const hasSystemPrompt = messages.some(
|
|
129
|
+
(msg) => msg.role === "system" && msg.content.length > 0
|
|
130
|
+
);
|
|
131
|
+
if (!hasSystemPrompt) {
|
|
132
|
+
messages.push({
|
|
133
|
+
role: "system",
|
|
134
|
+
content: [{ text: await this.buildSystemPrompt(options) }]
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (options.mode === "edit") {
|
|
138
|
+
messages.push({
|
|
139
|
+
role: "user",
|
|
140
|
+
content: [
|
|
141
|
+
{
|
|
142
|
+
text: [
|
|
143
|
+
"The JSON you must edit is:",
|
|
144
|
+
"",
|
|
145
|
+
JSON.stringify(options.editData || {}, null, 2)
|
|
146
|
+
].join("")
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return messages;
|
|
152
|
+
}
|
|
153
|
+
/** Builds the request sent to the AI based on the `ChatMode`. */
|
|
154
|
+
async buildGenerateRequest(prompt, options) {
|
|
155
|
+
if (options.mode === "edit") {
|
|
156
|
+
return {
|
|
157
|
+
messages: await this.buildMessages(options),
|
|
158
|
+
model: this.model,
|
|
159
|
+
prompt
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
messages: await this.buildMessages(options),
|
|
164
|
+
model: this.model,
|
|
165
|
+
prompt
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** Sends the request to the AI and stores the history in the session and the database. */
|
|
169
|
+
async sendPrompt(prompt, options = {}) {
|
|
170
|
+
const chatRequest = await this.buildGenerateRequest(prompt, options);
|
|
171
|
+
const res = await generate(this.ai, chatRequest.model, {
|
|
172
|
+
messages: chatRequest.messages,
|
|
173
|
+
prompt: Array.isArray(prompt) ? prompt.flat() : prompt
|
|
174
|
+
});
|
|
175
|
+
this.history = res.messages;
|
|
176
|
+
await this.dbDoc().update({
|
|
177
|
+
history: this.history,
|
|
178
|
+
modifiedAt: Timestamp.now()
|
|
179
|
+
});
|
|
180
|
+
if (options.mode === "edit") {
|
|
181
|
+
const aiResponse = res.output;
|
|
182
|
+
if (aiResponse?.data && !aiResponse.message) {
|
|
183
|
+
aiResponse.message = "";
|
|
184
|
+
}
|
|
185
|
+
return aiResponse;
|
|
186
|
+
}
|
|
187
|
+
return { message: res.text, data: null };
|
|
188
|
+
}
|
|
189
|
+
dbDoc() {
|
|
190
|
+
return this.chatClient.dbCollection().doc(this.id);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Builds the system prompt sent to the AI, based on the `ChatMode` and
|
|
194
|
+
* supplied `SendPromptOptions`. `SendPromptOptions` may contain data or
|
|
195
|
+
* references to information needed to construct the prompt.
|
|
196
|
+
*/
|
|
197
|
+
async buildSystemPrompt(options) {
|
|
198
|
+
const serializedRootConfig = JSON.stringify(
|
|
199
|
+
this.cmsClient.rootConfig,
|
|
200
|
+
null,
|
|
201
|
+
2
|
|
202
|
+
);
|
|
203
|
+
if (options.mode === "edit") {
|
|
204
|
+
const rootDir = process.cwd();
|
|
205
|
+
const rootCmsDefsPath = path.resolve(rootDir, "root-cms.d.ts");
|
|
206
|
+
const rootCmsDefs = fs.existsSync(rootCmsDefsPath) ? fs.readFileSync(rootCmsDefsPath, {
|
|
207
|
+
encoding: "utf8"
|
|
208
|
+
}) : null;
|
|
209
|
+
const text = [(await import("./edit-XX3LAGK6.js")).default];
|
|
210
|
+
if (rootCmsDefs) {
|
|
211
|
+
text.push(
|
|
212
|
+
"Here is the `root-cms.d.ts` file for this project:",
|
|
213
|
+
"```",
|
|
214
|
+
rootCmsDefs,
|
|
215
|
+
"```"
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
return text.join("\n");
|
|
219
|
+
}
|
|
220
|
+
if (options.mode === "altText") {
|
|
221
|
+
return (await import("./altText-RDKJNVGH.js")).default;
|
|
222
|
+
}
|
|
223
|
+
const systemText = [
|
|
224
|
+
`You are an assistant for a headless CMS called Root CMS which is used on a website called ${this.cmsPluginOptions.name || this.cmsPluginOptions.id}. Your job is to answer questions about the docs in the system, and if requested, help suggest changes to the JSON data in the docs. If you don't know the answer, just say that you don't know, don't try to make up an answer. Be friendly and playful with your messaging.`,
|
|
225
|
+
"",
|
|
226
|
+
"Here is the root.config.ts file for the site:",
|
|
227
|
+
"```",
|
|
228
|
+
serializedRootConfig,
|
|
229
|
+
"```",
|
|
230
|
+
"",
|
|
231
|
+
"Here are the docs that exist in the system:"
|
|
232
|
+
];
|
|
233
|
+
const pages = await this.cmsClient.listDocs("Pages", { mode: "draft" });
|
|
234
|
+
pages.docs.forEach((doc) => {
|
|
235
|
+
systemText.push(JSON.stringify(doc));
|
|
236
|
+
});
|
|
237
|
+
return systemText.join("\n");
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
var ChatClient = class {
|
|
241
|
+
constructor(cmsClient, user) {
|
|
242
|
+
this.cmsClient = cmsClient;
|
|
243
|
+
this.user = user;
|
|
244
|
+
}
|
|
245
|
+
async createChat() {
|
|
246
|
+
const chatId = crypto.randomUUID();
|
|
247
|
+
const docRef = this.dbCollection().doc(chatId);
|
|
248
|
+
const chat = new Chat(this, chatId);
|
|
249
|
+
await docRef.set({
|
|
250
|
+
id: chatId,
|
|
251
|
+
createdBy: this.user,
|
|
252
|
+
createdAt: Timestamp.now(),
|
|
253
|
+
modifiedAt: Timestamp.now(),
|
|
254
|
+
model: chat.model
|
|
255
|
+
});
|
|
256
|
+
return chat;
|
|
257
|
+
}
|
|
258
|
+
async getOrCreateChat(chatId) {
|
|
259
|
+
return chatId ? this.getChat(chatId) : this.createChat();
|
|
260
|
+
}
|
|
261
|
+
async getChat(chatId) {
|
|
262
|
+
const docRef = this.dbCollection().doc(chatId);
|
|
263
|
+
const chatDoc = await docRef.get();
|
|
264
|
+
if (!chatDoc.exists) {
|
|
265
|
+
throw new Error(`${chatId} does not exist`);
|
|
266
|
+
}
|
|
267
|
+
const chatData = chatDoc.data() || {};
|
|
268
|
+
return new Chat(this, chatId, {
|
|
269
|
+
history: chatData.history,
|
|
270
|
+
model: chatData.model
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
async listChats(options) {
|
|
274
|
+
const limit = options?.limit || 20;
|
|
275
|
+
const query = this.dbCollection().where("createdBy", "==", this.user).limit(limit).orderBy("createdAt", "desc");
|
|
276
|
+
const res = await query.get();
|
|
277
|
+
return res.docs.map((doc) => doc.data());
|
|
278
|
+
}
|
|
279
|
+
dbCollection() {
|
|
280
|
+
return this.cmsClient.db.collection(
|
|
281
|
+
`Projects/${this.cmsClient.projectId}/Experiments/ai/Chat`
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
function cleanModelName(model) {
|
|
286
|
+
return LEGACY_MODEL_RENAME[model] || model;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export {
|
|
290
|
+
summarizeDiff,
|
|
291
|
+
generateImage,
|
|
292
|
+
Chat,
|
|
293
|
+
ChatClient
|
|
294
|
+
};
|