@kidecms/core 0.1.10 → 0.13.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.
- package/LICENSE +21 -0
- package/admin/components/AssetsGrid.tsx +366 -173
- package/admin/components/BlockEditor.tsx +260 -679
- package/admin/components/CheckboxField.tsx +2 -2
- package/admin/components/CollaborationReviewBar.tsx +384 -0
- package/admin/components/ColorField.tsx +87 -0
- package/admin/components/CommandPalette.tsx +169 -0
- package/admin/components/ContentEditor.tsx +687 -0
- package/admin/components/DocumentActions.tsx +4 -2
- package/admin/components/DocumentsDataTable.tsx +91 -10
- package/admin/components/FieldControl.astro +151 -26
- package/admin/components/FocalPointSelector.tsx +27 -1
- package/admin/components/InternalLinkPicker.tsx +4 -3
- package/admin/components/LinkField.tsx +126 -0
- package/admin/components/RelationField.tsx +105 -26
- package/admin/components/RepeaterInput.tsx +46 -0
- package/admin/components/RichTextEditor.tsx +44 -9
- package/admin/components/SharedSectionBlockField.tsx +93 -0
- package/admin/components/SidebarUserMenu.tsx +1 -1
- package/admin/components/Toast.astro +1 -1
- package/admin/components/TreeItemsEditor.tsx +20 -19
- package/admin/components/TreeSelect.tsx +2 -2
- package/admin/components/YoutubeField.tsx +48 -0
- package/admin/components/block-fields.tsx +777 -0
- package/admin/components/content-block-spec.ts +114 -0
- package/admin/components/slash-command.tsx +242 -0
- package/admin/components/tree-utils.ts +14 -2
- package/admin/components/ui/badge.tsx +2 -3
- package/admin/components/ui/button.tsx +5 -4
- package/admin/components/ui/checkbox.tsx +1 -1
- package/admin/components/ui/input.tsx +1 -1
- package/admin/components/ui/select.tsx +1 -1
- package/admin/components/ui/table.tsx +1 -1
- package/admin/components/ui/textarea.tsx +1 -1
- package/admin/layouts/AdminLayout.astro +323 -123
- package/admin/lib/access.ts +54 -0
- package/admin/lib/collaboration.ts +178 -0
- package/admin/lib/edit-data.ts +448 -0
- package/admin/views/AccessDeniedView.astro +8 -0
- package/admin/views/EditView.astro +706 -0
- package/admin/views/ListView.astro +202 -0
- package/admin/views/RecentView.astro +76 -0
- package/admin/views/SinglesView.astro +65 -0
- package/admin/views/edit/EditScripts.astro +145 -0
- package/admin/views/edit/ReverseReferencesList.astro +68 -0
- package/core/admin.ts +193 -0
- package/core/ai.ts +87 -0
- package/core/api.ts +1392 -0
- package/core/assets.ts +336 -0
- package/core/audit.ts +84 -0
- package/core/auth-config.ts +44 -0
- package/core/auth.ts +303 -0
- package/core/collaboration.ts +276 -0
- package/core/content.ts +27 -0
- package/core/create-admin.ts +28 -0
- package/core/define.ts +625 -0
- package/core/field-model.ts +172 -0
- package/core/generator.ts +519 -0
- package/core/http.ts +42 -0
- package/core/image.ts +219 -0
- package/core/index.ts +211 -0
- package/core/locks.ts +52 -0
- package/core/log.ts +37 -0
- package/core/migrate.ts +278 -0
- package/core/rate-limit.ts +110 -0
- package/core/request-scope.ts +33 -0
- package/core/richtext.ts +1 -0
- package/core/runtime.ts +63 -0
- package/core/schema.ts +32 -0
- package/core/search.ts +489 -0
- package/core/seed.ts +101 -0
- package/core/shared-sections.ts +96 -0
- package/core/tasks.ts +176 -0
- package/core/values.ts +433 -0
- package/core/webhooks.ts +85 -0
- package/core/youtube.ts +39 -0
- package/internals/cli.mjs +34 -0
- package/internals/context.ts +67 -0
- package/internals/create-admin.ts +40 -0
- package/internals/describe.ts +25 -0
- package/internals/eject.ts +144 -0
- package/internals/generator.ts +11 -0
- package/internals/integration.ts +481 -0
- package/internals/mcp.ts +397 -0
- package/internals/project.ts +26 -0
- package/internals/push.ts +160 -0
- package/internals/reindex.ts +11 -0
- package/internals/restore.ts +257 -0
- package/internals/seed.data.ts +7 -0
- package/internals/seed.ts +11 -0
- package/internals/upgrade.ts +776 -0
- package/{virtual.d.ts → internals/virtual.d.ts} +39 -5
- package/middleware/auth.ts +130 -17
- package/middleware/push.ts +121 -0
- package/package.json +55 -67
- package/platform/cloudflare/cf-env.ts +60 -0
- package/platform/cloudflare/database.ts +28 -0
- package/platform/cloudflare/globals.d.ts +5 -0
- package/platform/cloudflare/storage.ts +33 -0
- package/platform/cloudflare/uploads-route.ts +37 -0
- package/platform/node/database.ts +39 -0
- package/platform/node/storage.ts +52 -0
- package/routes/admin/[...path].astro +71 -0
- package/routes/{pages/admin → admin}/assets/[id].astro +25 -19
- package/routes/admin/assets/index.astro +90 -0
- package/routes/admin/forgot-password.astro +60 -0
- package/routes/{pages/admin → admin}/invite.astro +7 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +32 -8
- package/routes/api/{cms/[collection] → [collection]}/[...path].ts +66 -14
- package/routes/api/admin/search.ts +89 -0
- package/routes/api/{cms/assets → assets}/[id].ts +24 -14
- package/routes/api/{cms/assets → assets}/index.ts +4 -2
- package/routes/api/assets/upload.ts +147 -0
- package/routes/api/auth/forgot-password.ts +77 -0
- package/routes/api/{cms/auth → auth}/invite.ts +41 -9
- package/routes/api/auth/login.ts +167 -0
- package/routes/api/{cms/auth → auth}/logout.ts +11 -2
- package/routes/api/auth/reset-password.ts +85 -0
- package/routes/api/auth/setup.ts +110 -0
- package/routes/api/auth/sso/[provider]/start.ts +34 -0
- package/routes/api/collaboration/index.ts +106 -0
- package/routes/api/cron/_authorize.ts +25 -0
- package/routes/api/{cms/cron → cron}/publish.ts +3 -9
- package/routes/api/cron/tasks.ts +33 -0
- package/routes/api/forms/submit/[slug].ts +143 -0
- package/routes/api/img/[...path].ts +65 -0
- package/routes/api/{cms/preview → preview}/render.ts +8 -3
- package/routes/api/{cms/references → references}/[collection]/[id].ts +31 -1
- package/routes/api/webhooks/[provider].ts +54 -0
- package/README.md +0 -28
- package/dist/admin.d.ts +0 -33
- package/dist/admin.js +0 -92
- package/dist/ai.d.ts +0 -15
- package/dist/ai.js +0 -70
- package/dist/api.d.ts +0 -14
- package/dist/api.js +0 -833
- package/dist/assets.d.ts +0 -51
- package/dist/assets.js +0 -163
- package/dist/auth.d.ts +0 -30
- package/dist/auth.js +0 -132
- package/dist/content.d.ts +0 -3
- package/dist/content.js +0 -29
- package/dist/create-admin.d.ts +0 -5
- package/dist/create-admin.js +0 -23
- package/dist/define.d.ts +0 -254
- package/dist/define.js +0 -36
- package/dist/generator.d.ts +0 -9
- package/dist/generator.js +0 -370
- package/dist/image.d.ts +0 -9
- package/dist/image.js +0 -69
- package/dist/index.d.ts +0 -28
- package/dist/index.js +0 -16
- package/dist/integration.d.ts +0 -14
- package/dist/integration.js +0 -327
- package/dist/locks.d.ts +0 -7
- package/dist/locks.js +0 -37
- package/dist/richtext.d.ts +0 -1
- package/dist/richtext.js +0 -1
- package/dist/runtime.d.ts +0 -24
- package/dist/runtime.js +0 -26
- package/dist/schema.d.ts +0 -17
- package/dist/schema.js +0 -13
- package/dist/seed.d.ts +0 -2
- package/dist/seed.js +0 -84
- package/dist/values.d.ts +0 -8
- package/dist/values.js +0 -102
- package/dist/webhooks.d.ts +0 -2
- package/dist/webhooks.js +0 -73
- package/routes/api/cms/assets/upload.ts +0 -112
- package/routes/api/cms/auth/login.ts +0 -124
- package/routes/api/cms/auth/setup.ts +0 -77
- package/routes/api/cms/img/[...path].ts +0 -24
- package/routes/pages/admin/[...path].astro +0 -1107
- package/routes/pages/admin/assets/index.astro +0 -63
- package/routes/pages/admin/login.astro +0 -57
- /package/routes/api/{cms/ai → ai}/alt-text.ts +0 -0
- /package/routes/api/{cms/ai → ai}/seo.ts +0 -0
- /package/routes/api/{cms/ai → ai}/translate.ts +0 -0
- /package/routes/api/{cms/assets → assets}/folders.ts +0 -0
- /package/routes/api/{cms/locks → locks}/[...path].ts +0 -0
package/core/admin.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { CMSConfig, CollectionConfig, FieldConfig } from "./define";
|
|
2
|
+
import { contentToPlainText, richTextToPlainText } from "./values";
|
|
3
|
+
|
|
4
|
+
const DEFAULT_DATE_FORMAT = "en-US";
|
|
5
|
+
const DEFAULT_DATE_OPTIONS: Intl.DateTimeFormatOptions = {
|
|
6
|
+
year: "numeric",
|
|
7
|
+
month: "numeric",
|
|
8
|
+
day: "numeric",
|
|
9
|
+
hour: "2-digit",
|
|
10
|
+
minute: "2-digit",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
let dateLocale = DEFAULT_DATE_FORMAT;
|
|
14
|
+
let timeZone: string | undefined;
|
|
15
|
+
let formatOptions: Intl.DateTimeFormatOptions = DEFAULT_DATE_OPTIONS;
|
|
16
|
+
let datePattern: string | undefined;
|
|
17
|
+
|
|
18
|
+
export const initDateFormat = (config: CMSConfig, nextTimeZone?: string) => {
|
|
19
|
+
dateLocale = config.admin?.dateFormat ?? DEFAULT_DATE_FORMAT;
|
|
20
|
+
// A configured zone wins; otherwise fall back to the browser's zone (passed in per request).
|
|
21
|
+
timeZone = config.admin?.timeZone ?? nextTimeZone;
|
|
22
|
+
formatOptions = { ...DEFAULT_DATE_OPTIONS, ...config.admin?.dateTimeFormat };
|
|
23
|
+
datePattern = config.admin?.dateTimePattern;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Extract zone-correct numeric parts once, then substitute pattern tokens against them.
|
|
27
|
+
type DateParts = { year: string; month: string; day: string; hour: string; minute: string; second: string };
|
|
28
|
+
|
|
29
|
+
const zoneParts = (date: Date, zone: string | undefined): DateParts => {
|
|
30
|
+
const parts: Record<string, string> = {};
|
|
31
|
+
for (const p of new Intl.DateTimeFormat("en-US", {
|
|
32
|
+
timeZone: zone,
|
|
33
|
+
year: "numeric",
|
|
34
|
+
month: "2-digit",
|
|
35
|
+
day: "2-digit",
|
|
36
|
+
hour: "2-digit",
|
|
37
|
+
minute: "2-digit",
|
|
38
|
+
second: "2-digit",
|
|
39
|
+
hourCycle: "h23",
|
|
40
|
+
}).formatToParts(date)) {
|
|
41
|
+
if (p.type !== "literal") parts[p.type] = p.value;
|
|
42
|
+
}
|
|
43
|
+
return parts as unknown as DateParts;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const TOKEN_FNS: Record<string, (p: DateParts) => string> = {
|
|
47
|
+
yyyy: (p) => p.year,
|
|
48
|
+
yy: (p) => p.year.slice(-2),
|
|
49
|
+
MM: (p) => p.month,
|
|
50
|
+
M: (p) => String(Number(p.month)),
|
|
51
|
+
dd: (p) => p.day,
|
|
52
|
+
d: (p) => String(Number(p.day)),
|
|
53
|
+
HH: (p) => p.hour,
|
|
54
|
+
H: (p) => String(Number(p.hour)),
|
|
55
|
+
hh: (p) => String(((Number(p.hour) + 11) % 12) + 1).padStart(2, "0"),
|
|
56
|
+
h: (p) => String(((Number(p.hour) + 11) % 12) + 1),
|
|
57
|
+
mm: (p) => p.minute,
|
|
58
|
+
m: (p) => String(Number(p.minute)),
|
|
59
|
+
ss: (p) => p.second,
|
|
60
|
+
s: (p) => String(Number(p.second)),
|
|
61
|
+
a: (p) => (Number(p.hour) < 12 ? "AM" : "PM"),
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const TOKEN_RE = /'([^']*)'|yyyy|yy|MM|M|dd|d|HH|H|hh|h|mm|m|ss|s|a/g;
|
|
65
|
+
|
|
66
|
+
const formatWithPattern = (date: Date, pattern: string, zone: string | undefined): string => {
|
|
67
|
+
const p = zoneParts(date, zone);
|
|
68
|
+
return pattern.replace(TOKEN_RE, (match, quoted?: string) => {
|
|
69
|
+
if (quoted !== undefined) return quoted === "" ? "'" : quoted; // '' → literal apostrophe
|
|
70
|
+
return TOKEN_FNS[match]?.(p) ?? match;
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const formatDate = (value: unknown): string => {
|
|
75
|
+
if (!value) return "—";
|
|
76
|
+
const date = new Date(String(value));
|
|
77
|
+
if (Number.isNaN(date.getTime())) return String(value);
|
|
78
|
+
if (datePattern) return formatWithPattern(date, datePattern, timeZone);
|
|
79
|
+
return date.toLocaleString(dateLocale, { ...formatOptions, timeZone });
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type AdminRoute =
|
|
83
|
+
| { kind: "dashboard" }
|
|
84
|
+
| { kind: "recent" }
|
|
85
|
+
| { kind: "singles" }
|
|
86
|
+
| { kind: "list"; collectionSlug: string }
|
|
87
|
+
| { kind: "new"; collectionSlug: string }
|
|
88
|
+
| { kind: "edit"; collectionSlug: string; documentId: string };
|
|
89
|
+
|
|
90
|
+
export const resolveAdminRoute = (path: string | undefined): AdminRoute => {
|
|
91
|
+
const segments = (path ?? "").split("/").filter(Boolean);
|
|
92
|
+
|
|
93
|
+
if (segments.length === 0) return { kind: "dashboard" };
|
|
94
|
+
if (segments[0] === "recent" && segments.length === 1) return { kind: "recent" };
|
|
95
|
+
if (segments[0] === "singles" && segments.length === 1) return { kind: "singles" };
|
|
96
|
+
if (segments.length === 1) return { kind: "list", collectionSlug: segments[0] };
|
|
97
|
+
if (segments[1] === "new") return { kind: "new", collectionSlug: segments[0] };
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
kind: "edit",
|
|
101
|
+
collectionSlug: segments[0],
|
|
102
|
+
documentId: segments[1],
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const humanize = (value: string) =>
|
|
107
|
+
value
|
|
108
|
+
.replace(/^_+/, "")
|
|
109
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
110
|
+
.replace(/[-_]/g, " ")
|
|
111
|
+
.replace(/\s+/g, " ")
|
|
112
|
+
.trim()
|
|
113
|
+
.replace(/^\w/, (char) => char.toUpperCase());
|
|
114
|
+
|
|
115
|
+
export const formatFieldValue = (
|
|
116
|
+
field: FieldConfig | undefined,
|
|
117
|
+
value: unknown,
|
|
118
|
+
relationLabels: Record<string, string> = {},
|
|
119
|
+
) => {
|
|
120
|
+
if (value === undefined || value === null || value === "") return "—";
|
|
121
|
+
if (!field) return String(value);
|
|
122
|
+
|
|
123
|
+
if (field.type === "richText" || field.type === "content") {
|
|
124
|
+
const text = field.type === "content" ? contentToPlainText(value as never) : richTextToPlainText(value as never);
|
|
125
|
+
return text.length > 120 ? `${text.slice(0, 117)}...` : text;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (field.type === "array") {
|
|
129
|
+
return Array.isArray(value) ? value.join(", ") : "—";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (field.type === "json" || field.type === "blocks") {
|
|
133
|
+
return Array.isArray(value) ? `${value.length} items` : "JSON";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (field.type === "boolean") return value ? "Yes" : "No";
|
|
137
|
+
|
|
138
|
+
if (field.type === "relation") {
|
|
139
|
+
if (Array.isArray(value)) {
|
|
140
|
+
return value.map((entry) => relationLabels[String(entry)] ?? String(entry)).join(", ");
|
|
141
|
+
}
|
|
142
|
+
return relationLabels[String(value)] ?? String(value);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return String(value);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export const getListColumns = (collection: CollectionConfig, viewConfig?: { columns?: string[] }) => {
|
|
149
|
+
if (viewConfig?.columns?.length) {
|
|
150
|
+
return collection.drafts ? viewConfig.columns : viewConfig.columns.filter((column) => column !== "_status");
|
|
151
|
+
}
|
|
152
|
+
const firstField = "title" in collection.fields ? "title" : Object.keys(collection.fields)[0];
|
|
153
|
+
return collection.drafts ? [firstField, "_status", "_updatedAt"] : [firstField, "_updatedAt"];
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Partition an ordered field list into consecutive runs sharing the same
|
|
158
|
+
* `admin.group` label. Runs with a group render as a titled panel in the edit
|
|
159
|
+
* form (collapsible when any field in the run declares it); ungrouped runs
|
|
160
|
+
* render as loose fields. Order is preserved, so grouping is purely
|
|
161
|
+
* presentational and opt-in per field.
|
|
162
|
+
*/
|
|
163
|
+
export const getFieldGroups = (collection: CollectionConfig, fieldNames: string[]) => {
|
|
164
|
+
const runs: Array<{ group?: string; collapsible?: boolean | "collapsed"; fields: string[] }> = [];
|
|
165
|
+
for (const fieldName of fieldNames) {
|
|
166
|
+
const raw = collection.fields[fieldName]?.admin?.group;
|
|
167
|
+
const group = typeof raw === "object" ? raw.label : raw;
|
|
168
|
+
const collapsible = typeof raw === "object" ? raw.collapsible : undefined;
|
|
169
|
+
const last = runs[runs.length - 1];
|
|
170
|
+
if (last && last.group === group) {
|
|
171
|
+
last.fields.push(fieldName);
|
|
172
|
+
last.collapsible ??= collapsible;
|
|
173
|
+
} else {
|
|
174
|
+
runs.push({ group, collapsible, fields: [fieldName] });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return runs;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export const getFieldSets = (collection: CollectionConfig) => {
|
|
181
|
+
const allFields = Object.keys(collection.fields).filter((fieldName) => !collection.fields[fieldName].admin?.hidden);
|
|
182
|
+
const contentFields = allFields.filter((fieldName) => collection.fields[fieldName].admin?.position !== "sidebar");
|
|
183
|
+
const sidebarFields = allFields.filter((fieldName) => collection.fields[fieldName].admin?.position === "sidebar");
|
|
184
|
+
|
|
185
|
+
if (sidebarFields.length > 0) {
|
|
186
|
+
return [
|
|
187
|
+
{ fields: contentFields, position: "content" as const },
|
|
188
|
+
{ fields: sidebarFields, position: "sidebar" as const },
|
|
189
|
+
];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return [{ fields: contentFields, position: "content" as const }];
|
|
193
|
+
};
|
package/core/ai.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { getStorage, readEnv } from "./runtime";
|
|
2
|
+
|
|
3
|
+
const getStreamText = async () => (await import("ai")).streamText;
|
|
4
|
+
|
|
5
|
+
export function isAiEnabled(): boolean {
|
|
6
|
+
return !!(readEnv("AI_PROVIDER") && readEnv("AI_API_KEY"));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function getAiModel(): Promise<any> {
|
|
10
|
+
const provider = readEnv("AI_PROVIDER") || "openai";
|
|
11
|
+
const modelName = readEnv("AI_MODEL") || "gpt-4o-mini";
|
|
12
|
+
|
|
13
|
+
if (provider === "openai") {
|
|
14
|
+
const { createOpenAI } = await import("@ai-sdk/openai");
|
|
15
|
+
const openai = createOpenAI({ apiKey: readEnv("AI_API_KEY") });
|
|
16
|
+
return openai(modelName);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
throw new Error(`Unsupported AI provider: ${provider}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function streamAltText(imageUrl: string, filename: string): Promise<any> {
|
|
23
|
+
const model = await getAiModel();
|
|
24
|
+
const image = await getStorage().getFile(imageUrl);
|
|
25
|
+
if (!image) {
|
|
26
|
+
throw new Error(`Image not found: ${imageUrl}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const streamText = await getStreamText();
|
|
30
|
+
return streamText({
|
|
31
|
+
model,
|
|
32
|
+
messages: [
|
|
33
|
+
{
|
|
34
|
+
role: "user",
|
|
35
|
+
content: [
|
|
36
|
+
{
|
|
37
|
+
type: "image",
|
|
38
|
+
image: new Uint8Array(image),
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
type: "text",
|
|
42
|
+
text: `Generate a concise, descriptive alt text for this image (filename: ${filename}). The alt text should be suitable for accessibility purposes. Return only the alt text, no quotes or extra formatting.`,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function streamSeoDescription(content: { title: string; excerpt?: string; body?: string }): Promise<any> {
|
|
51
|
+
const model = await getAiModel();
|
|
52
|
+
const prompt = `Generate an SEO-optimized meta description (max 155 characters) for a page with the following content. Return only the description, no quotes or extra formatting.
|
|
53
|
+
|
|
54
|
+
Title: ${content.title}
|
|
55
|
+
${content.excerpt ? `Excerpt: ${content.excerpt}` : ""}
|
|
56
|
+
${content.body ? `Body preview: ${content.body.substring(0, 500)}` : ""}`;
|
|
57
|
+
|
|
58
|
+
const streamText = await getStreamText();
|
|
59
|
+
return streamText({ model, prompt });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function streamTranslation(content: {
|
|
63
|
+
text: string;
|
|
64
|
+
sourceLocale: string;
|
|
65
|
+
targetLocale: string;
|
|
66
|
+
fieldName: string;
|
|
67
|
+
fieldType: "text" | "richText" | "slug";
|
|
68
|
+
}): Promise<any> {
|
|
69
|
+
const model = await getAiModel();
|
|
70
|
+
|
|
71
|
+
let prompt: string;
|
|
72
|
+
|
|
73
|
+
if (content.fieldType === "richText") {
|
|
74
|
+
prompt = `Translate the following rich text JSON AST from ${content.sourceLocale} to ${content.targetLocale}. Keep the exact same JSON structure, only translate the text values. Return only the valid JSON, no extra formatting or code blocks.
|
|
75
|
+
|
|
76
|
+
${content.text}`;
|
|
77
|
+
} else if (content.fieldType === "slug") {
|
|
78
|
+
prompt = `Translate and create a URL-friendly slug in ${content.targetLocale} based on this ${content.sourceLocale} text: "${content.text}". Return only the slug (lowercase, hyphens instead of spaces, no special characters).`;
|
|
79
|
+
} else {
|
|
80
|
+
prompt = `Translate the following text from ${content.sourceLocale} to ${content.targetLocale}. Return only the translated text, no quotes or extra formatting.
|
|
81
|
+
|
|
82
|
+
${content.text}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const streamText = await getStreamText();
|
|
86
|
+
return streamText({ model, prompt });
|
|
87
|
+
}
|