Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ dist
build
coverage
package-lock.json
yarn.lock
yarn.lock
# Generated by scripts/api-report.mjs
api-report
2 changes: 1 addition & 1 deletion .sync-ref
Original file line number Diff line number Diff line change
@@ -1 +1 @@
chaibuilder/pro@55058ebe503eb924ba1b9a9829f0a15989989ede
chaibuilder/pro@a4127ad93f51d4f04e4945a035f5845739418ca3
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@
"@rjsf/core": "5.24.13",
"@rjsf/utils": "5.24.13",
"@rjsf/validator-ajv8": "5.24.13",
"@tailwindcss/container-queries": "0.1.1",
"@tailwindcss/forms": "0.5.11",
"@tailwindcss/typography": "0.5.19",
"@tanstack/react-query": "5.90.16",
Expand Down
12 changes: 0 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-sync.exclude
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Read by scripts/sync-src.mjs and scripts/sync-check.mjs. Edit in both repos in the same change.
/edition/
/payload/
/plugins/ai-advanced/
/plugins/ai-pro/
/plugins/animation/
/plugins/bulk-publish/
Expand Down
12 changes: 12 additions & 0 deletions src/ai/openai-compatible.deprecated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Deprecated names of `<pkg>/ai/openai-compatible`, re-exported by `./openai-compatible.ts`.
*
* chaipro makes no breaking changes: a renamed or retired export keeps working from its original
* import path, with a `@deprecated` notice that names its replacement. Never delete an entry here.
* See API-CONSISTENCY.md.
*/

export {
/** @deprecated Use `openAiCompatiblePlugin` from `<pkg>/ai/openai-compatible`. Kept for compatibility. */
openAiCompatiblePlugin as openAICompatiblePlugin,
} from "~/ai/openai-compatible";
20 changes: 10 additions & 10 deletions src/ai/openai-compatible.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { openAICompatiblePlugin } from "./openai-compatible";
import { openAiCompatiblePlugin } from "./openai-compatible";

const createOpenAICompatible = vi.hoisted(() =>
vi.fn((_options: Record<string, unknown>) => ({ languageModel: (id: string) => ({ id }) })),
Expand All @@ -14,33 +14,33 @@ afterEach(() => {

describe("openAICompatiblePlugin (static SDK import)", () => {
it("matches the built-in plugin's identity, so error messages stay the same", () => {
expect(openAICompatiblePlugin.id).toBe("openai-compatible");
expect(openAICompatiblePlugin.label).toBe("OpenAI-compatible");
expect(openAICompatiblePlugin.packageName).toBe("@ai-sdk/openai-compatible");
expect(openAiCompatiblePlugin.id).toBe("openai-compatible");
expect(openAiCompatiblePlugin.label).toBe("OpenAI-compatible");
expect(openAiCompatiblePlugin.packageName).toBe("@ai-sdk/openai-compatible");
});

it("activates on the base URL — without it the AI gateway keeps serving requests", () => {
vi.stubEnv("OPENAI_COMPATIBLE_BASE_URL", "https://router.huggingface.co/v1");
expect(openAICompatiblePlugin.isConfigured()).toBe(true);
expect(openAiCompatiblePlugin.isConfigured()).toBe(true);

vi.stubEnv("OPENAI_COMPATIBLE_BASE_URL", "");
expect(openAICompatiblePlugin.isConfigured()).toBe(false);
expect(openAiCompatiblePlugin.isConfigured()).toBe(false);
});

it("rebuilds when the endpoint or key changes", () => {
vi.stubEnv("OPENAI_COMPATIBLE_BASE_URL", "http://localhost:11434/v1");
const first = openAICompatiblePlugin.fingerprint?.();
const first = openAiCompatiblePlugin.fingerprint?.();

vi.stubEnv("OPENAI_COMPATIBLE_API_KEY", "sk-1");

expect(openAICompatiblePlugin.fingerprint?.()).not.toBe(first);
expect(openAiCompatiblePlugin.fingerprint?.()).not.toBe(first);
});

it("builds the provider from the endpoint, with the default label", async () => {
vi.stubEnv("OPENAI_COMPATIBLE_BASE_URL", "https://api.groq.com/openai/v1");
vi.stubEnv("OPENAI_COMPATIBLE_API_KEY", "sk-1");

const provider = await openAICompatiblePlugin.createProvider();
const provider = await openAiCompatiblePlugin.createProvider();

expect(createOpenAICompatible).toHaveBeenCalledWith({
name: "openai-compatible",
Expand All @@ -55,7 +55,7 @@ describe("openAICompatiblePlugin (static SDK import)", () => {
vi.stubEnv("OPENAI_COMPATIBLE_API_KEY", "");
vi.stubEnv("OPENAI_COMPATIBLE_NAME", "ollama");

await openAICompatiblePlugin.createProvider();
await openAiCompatiblePlugin.createProvider();

const options = createOpenAICompatible.mock.calls[0][0];
expect(options.name).toBe("ollama");
Expand Down
5 changes: 4 additions & 1 deletion src/ai/openai-compatible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ import {
} from "~/server/chai-actions/ai-providers/openai-compatible-options";
import type { ChaiAiProviderPlugin, ChaiAiSdkProvider } from "~/types/server-config";

export const openAICompatiblePlugin: ChaiAiProviderPlugin = {
export const openAiCompatiblePlugin: ChaiAiProviderPlugin = {
id: OPENAI_COMPATIBLE_PLUGIN_ID,
label: OPENAI_COMPATIBLE_PLUGIN_LABEL,
packageName: OPENAI_COMPATIBLE_PACKAGE,
isConfigured: isOpenAICompatibleConfigured,
fingerprint: openAICompatibleFingerprint,
createProvider: (): ChaiAiSdkProvider => createOpenAICompatible(openAICompatibleOptions()) as ChaiAiSdkProvider,
};

// Deprecated names, kept for compatibility. See ./openai-compatible.deprecated.ts.
export * from "./openai-compatible.deprecated";
8 changes: 4 additions & 4 deletions src/builder/core/components/QuickPrompts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useTranslation } from "react-i18next";
import { useAskAi } from "~/builder/hooks/use-ask-ai";
import { useLanguages } from "~/builder/hooks/use-languages";
import { Popover, PopoverContent, PopoverTrigger } from "~/components/ui/popover";
import { LANGUAGES } from "~/constants/LANGUAGES";
import { CHAI_LANGUAGES } from "~/constants/LANGUAGES";

export function QuickPrompts({ onClick }: { onClick: (prompt: string) => void }) {
const { loading } = useAskAi();
Expand Down Expand Up @@ -64,11 +64,11 @@ export function QuickPrompts({ onClick }: { onClick: (prompt: string) => void })
if (selectedLang && selectedLang !== fallbackLang) {
quickPrompts.splice(0, 0, {
name: t(`Translate to {{lang}}`, {
lang: get(LANGUAGES, selectedLang, selectedLang),
lang: get(CHAI_LANGUAGES, selectedLang, selectedLang),
}),
icon: ChatBubbleIcon,
prompt: t(`Translate the content to {{lang}}. Maintain same tone, style and length.`, {
lang: get(LANGUAGES, selectedLang, selectedLang),
lang: get(CHAI_LANGUAGES, selectedLang, selectedLang),
}),
});
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export function QuickPrompts({ onClick }: { onClick: (prompt: string) => void })
) : (
<li
onClick={() => onClick(prompt)}
className="flex cursor-pointer items-center space-x-2 rounded p-1 text-xs hover:bg-primary/10 hover:text-primary dark:hover:bg-gray-800"
className="hover:bg-primary/10 hover:text-primary flex cursor-pointer items-center space-x-2 rounded p-1 text-xs dark:hover:bg-gray-800"
key={name}>
<Icon className="h-3.5 w-3.5" />
<span>{t(name)}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useBuilderProp } from "~/builder/hooks/use-builder-prop";
import { Button } from "~/components/ui/button";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "~/components/ui/command";
import { COLLECTION_PREFIX } from "~/constants/STRINGS";
import type { ChaiCollection } from "~/types/collections";
import type { ChaiCollection } from "~/types/deprecated";
import { BindingOptionRow, type BindingOptionIcon } from "./binding-option-row";
import {
compareBindingFieldNames,
Expand Down
57 changes: 17 additions & 40 deletions src/builder/core/components/canvas/IframeInitialContent.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
import { describe, expect, it } from "vitest";
import { getIframeInitialContent, TAILWIND_CDN_URLS, TAILWIND_THEME_STYLE_ID } from "./IframeInitialContent";
import { getIframeInitialContent, TAILWIND_CDN_URL, TAILWIND_THEME_STYLE_ID } from "./IframeInitialContent";

describe("getIframeInitialContent", () => {
it("defaults to Tailwind v4 CDN", () => {
it("loads the Tailwind v4 browser build", () => {
const html = getIframeInitialContent();

expect(html).toContain(TAILWIND_CDN_URLS["4"]);
expect(html).toContain(`src="${TAILWIND_CDN_URLS["4"]}"`);
expect(TAILWIND_CDN_URL).toContain("@tailwindcss/browser@4");
expect(html).toContain(`src="${TAILWIND_CDN_URL}"`);
expect(html).not.toContain("cdn.tailwindcss.com");
expect(html).not.toContain("__TAILWIND_CDN__");
expect(html).not.toContain("__HTML_DIR__");
});

it("uses the v3 CDN when tailwindCSS is 3", () => {
const html = getIframeInitialContent({ tailwindCSS: "3" });

expect(html).toContain(TAILWIND_CDN_URLS["3"]);
expect(html).toContain("cdn.tailwindcss.com");
expect(html).not.toContain(TAILWIND_CDN_URLS["4"]);
});

it("sets html dir for rtl layouts", () => {
const html = getIframeInitialContent({ htmlDir: "rtl", tailwindCSS: "4" });
const html = getIframeInitialContent({ htmlDir: "rtl" });

expect(html).toContain('dir="rtl"');
expect(html).toContain(TAILWIND_CDN_URLS["4"]);
});

it("exposes distinct CDN URLs for v3 and v4", () => {
expect(TAILWIND_CDN_URLS["3"]).toContain("cdn.tailwindcss.com");
expect(TAILWIND_CDN_URLS["4"]).toContain("@tailwindcss/browser");
expect(TAILWIND_CDN_URLS["3"]).not.toBe(TAILWIND_CDN_URLS["4"]);
});

it("keeps the config-driven @apply base rule on v3", () => {
const html = getIframeInitialContent({ tailwindCSS: "3" });

expect(html).toContain("@apply border-border;");
expect(html).not.toContain("__TAILWIND_STYLE__");
expect(html).toContain(TAILWIND_CDN_URL);
});

// The v4 browser build drops the whole sheet when one utility is unknown.
it("compiles standalone on v4 without theme-dependent utilities in base", () => {
const html = getIframeInitialContent({ tailwindCSS: "4" });
it("compiles standalone without theme-dependent utilities in base", () => {
const html = getIframeInitialContent();

expect(html).toContain("@custom-variant dark (&:where(.dark, .dark *));");
expect(html).toContain("border-color: var(--color-border, currentColor);");
Expand All @@ -51,23 +31,20 @@ describe("getIframeInitialContent", () => {

// Spelling out the import makes the browser resolve "tailwindcss" as a relative URL and 404;
// the build injects it for us when the concatenated CSS has no @import of its own.
it("leaves the tailwindcss import to the v4 browser build", () => {
expect(getIframeInitialContent({ tailwindCSS: "4" })).not.toContain("@import");
it("leaves the tailwindcss import to the browser build", () => {
expect(getIframeInitialContent()).not.toContain("@import");
});

it("ships an empty theme placeholder on v4 for TailwindV4Theme to fill", () => {
const html = getIframeInitialContent({ tailwindCSS: "4" });
it("ships an empty theme placeholder for TailwindV4Theme to fill", () => {
const html = getIframeInitialContent();

expect(html).toContain(`<style type="text/tailwindcss" id="${TAILWIND_THEME_STYLE_ID}"></style>`);
expect(getIframeInitialContent({ tailwindCSS: "3" })).not.toContain(TAILWIND_THEME_STYLE_ID);
});

it("ships the rte utilities on both versions", () => {
for (const tailwindCSS of ["3", "4"] as const) {
const html = getIframeInitialContent({ tailwindCSS });
it("ships the rte utilities", () => {
const html = getIframeInitialContent();

expect(html).toContain('<style type="text/tailwindcss">');
expect(html).toContain("@apply list-disc pl-6 text-base;");
}
expect(html).toContain('<style type="text/tailwindcss">');
expect(html).toContain("@apply list-disc pl-6 text-base;");
});
});
56 changes: 8 additions & 48 deletions src/builder/core/components/canvas/IframeInitialContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ export const IframeInitialContent: string = `<!doctype html>
</body>
</html>`;

export type TailwindCSSVersion = "3" | "4";

export const TAILWIND_THEME_STYLE_ID = "chai-tailwind-theme";

const RTE_UTILITIES = `
Expand Down Expand Up @@ -147,46 +145,22 @@ const screenHeightValue = (unit: (typeof SCREEN_UNITS)[number]) =>
`var(${SCREEN_HEIGHT_VAR}, ${unit === "screen" ? "100vh" : `100${unit}`})`;
const SCREEN_HEIGHT_PROPERTIES = { h: "height", "min-h": "min-height", "max-h": "max-height" } as const;

// v4: an @utility named like a core utility appends its declaration after the core one, so it wins.
const V4_SCREEN_HEIGHT_UTILITIES = Object.entries(SCREEN_HEIGHT_PROPERTIES)
// An @utility named like a core utility appends its declaration after the core one, so it wins.
const SCREEN_HEIGHT_UTILITIES = Object.entries(SCREEN_HEIGHT_PROPERTIES)
.flatMap(([prefix, property]) =>
SCREEN_UNITS.map((unit) => `@utility ${prefix}-${unit} { ${property}: ${screenHeightValue(unit)}; }`),
)
.join("\n ");

// v3: the same values as theme keys, merged into the Play CDN config by HeadTags.
export const V3_SCREEN_HEIGHT_THEME = Object.fromEntries(
(["height", "minHeight", "maxHeight"] as const).map((key) => [
key,
Object.fromEntries(SCREEN_UNITS.map((unit) => [unit, screenHeightValue(unit)])),
]),
);

// v3 Play CDN resolves `border-border` from the config pushed via `window.tailwind.config`.
const TAILWIND_V3_STYLE = `<style type="text/tailwindcss">
@layer base {
* {
@apply border-border;
}
/* Seed the canvas default text/background from the theme (mirrors the v4
sheet below), so that after .typeset switched to currentColor, unstyled
Paragraph/Heading text still matches the published page in the v3 canvas
instead of falling back to browser default black-on-white. */
body {
@apply bg-background text-foreground;
}
}${RTE_UTILITIES}
</style>`;

// The v4 browser build concatenates every `style[type="text/tailwindcss"]` into a single
// compile. One unknown utility fails the whole sheet, so base styles here stay on plain CSS
// vars; theme-dependent utilities come from the `@theme` block written into the placeholder
// below. The typography and forms plugins the page CSS compiles with arrive pre-rewritten as
// `@utility` / `@custom-variant` rules (the browser build cannot load npm plugins). No `@import "tailwindcss"` here on purpose: the build prepends it when no @import is
// present, and spelling it out makes the browser fetch the bare specifier as a relative URL.
const TAILWIND_V4_STYLE = `<style type="text/tailwindcss">
const TAILWIND_STYLE = `<style type="text/tailwindcss">
@custom-variant dark (&:where(.dark, .dark *));
${V4_SCREEN_HEIGHT_UTILITIES}
${SCREEN_HEIGHT_UTILITIES}
@layer base {
*,
::after,
Expand All @@ -212,23 +186,9 @@ const TAILWIND_V4_STYLE = `<style type="text/tailwindcss">
-->
<style type="text/tailwindcss" id="${TAILWIND_THEME_STYLE_ID}"></style>`;

export const TAILWIND_CDN_URLS: Record<TailwindCSSVersion, string> = {
"3": "https://cdn.tailwindcss.com/3.4.17?plugins=forms@0.5.9,typography@0.5.15,aspect-ratio@0.4.2",
"4": "https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4",
};

const TAILWIND_STYLES: Record<TailwindCSSVersion, string> = {
"3": TAILWIND_V3_STYLE,
"4": TAILWIND_V4_STYLE,
};
export const TAILWIND_CDN_URL = "https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4";

export const getIframeInitialContent = ({
htmlDir = "ltr",
tailwindCSS = "4",
}: {
htmlDir?: "ltr" | "rtl" | string;
tailwindCSS?: TailwindCSSVersion;
} = {}): string =>
export const getIframeInitialContent = ({ htmlDir = "ltr" }: { htmlDir?: "ltr" | "rtl" | string } = {}): string =>
IframeInitialContent.replace("__HTML_DIR__", htmlDir)
.replace("__TAILWIND_CDN__", TAILWIND_CDN_URLS[tailwindCSS])
.replace("__TAILWIND_STYLE__", TAILWIND_STYLES[tailwindCSS]);
.replace("__TAILWIND_CDN__", TAILWIND_CDN_URL)
.replace("__TAILWIND_STYLE__", TAILWIND_STYLE);
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { useCheckPartialCanAdd } from "~/builder/hooks/use-partial-blocks-store"
import { useSelectedBlockIds } from "~/builder/hooks/use-selected-blockIds";
import { useSelectedStylingBlocks } from "~/builder/hooks/use-selected-styling-blocks";
import { useUpdateBlocksProps } from "~/builder/hooks/use-update-blocks-props";
import { syncBlocksWithDefaultProps } from "~/registry";
import { syncChaiBlocksWithDefaultProps } from "~/registry";
import { ChaiBlock } from "~/types/common";

/**
Expand Down Expand Up @@ -203,7 +203,7 @@ export const useBlockDrop = () => {
const preBlocks = !dropBlock?.blocks
? null
: isFunction(dropBlock?.blocks)
? syncBlocksWithDefaultProps(dropBlock?.blocks())
? syncChaiBlocksWithDefaultProps(dropBlock?.blocks())
: dropBlock?.blocks;

if (draggedBlockType === "PartialBlock") {
Expand Down
Loading
Loading