Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: Rename StreamingTextResponse to RemixStreamingTextResponse (#2434)
Giuseppe309d178ae56d
diff --git a/packages/ai/src/chains/copywriter/chain.server.ts b/packages/ai/src/chains/copywriter/chain.server.tsdeleted file mode 100644index f9b5a9f9f2..0000000000--- a/packages/ai/src/chains/copywriter/chain.server.ts+++ /dev/null@@ -1,166 +0,0 @@-import { z } from "zod";-import type { Model as BaseModel, ModelMessage, Chain } from "../../types";-import { formatPrompt } from "../../utils/format-prompt";-import { prompt as promptSystemTemplate } from "./__generated__/copy.system.prompt";-import { prompt as promptUserTemplate } from "./__generated__/copy.user.prompt";-import type { Instance, Instances } from "@webstudio-is/sdk";-import { createErrorResponse } from "../../utils/create-error-response";-import type { StreamingTextResponseType } from "../../utils/streaming-text-response";--/**- * Copywriter chain.- *- * Given a description and an instance id,- * this chain generates copy for the instance and all its descendant text nodes.- */--export const TextInstanceSchema = z.object({- instanceId: z.string(),- index: z.number(),- type: z.union([- z.literal("Heading"),- z.literal("Paragraph"),- z.literal("Text"),- ]),- text: z.string(),-});--export type TextInstance = z.infer<typeof TextInstanceSchema>;--export const ContextSchema = z.object({- // The prompt provides context about the copy to generate and comes from the user.- prompt: z.string(),- // An array of text nodes to generate copy for.- textInstances: z.array(TextInstanceSchema),-});-export type Context = z.infer<typeof ContextSchema>;--export const ResponseSchema = z.array(TextInstanceSchema);-export type Response = z.infer<typeof ResponseSchema>;--export const createChain = <ModelMessageFormat>(): Chain<- BaseModel<ModelMessageFormat>,- Context,- StreamingTextResponseType-> =>- async function chain({ model, context }) {- const { prompt, textInstances } = context;-- const id = "copywriter";-- const llmMessages: ModelMessage[] = [- ["system", promptSystemTemplate],- [- "user",- formatPrompt(- {- prompt,- text_nodes: JSON.stringify(textInstances),- },- promptUserTemplate- ),- ],- ];-- if (textInstances.length === 0) {- const message = "No text nodes found for the instance";- return {- id,- ...createErrorResponse({- status: 404,- error: "ai.copywriter.textNodesNotFound",- message,- debug: message,- }),- llmMessages,- };- }-- try {- z.array(TextInstanceSchema).parse(textInstances);- } catch (error) {- const message = "Invalid nodes list";- return {- id,- ...createErrorResponse({- status: 404,- error: "ai.parseError",- message,- debug: message,- }),- llmMessages,- };- }-- const messages = model.generateMessages(llmMessages);-- const response = await model.completionStream({- id,- messages,- });-- return {- ...response,- llmMessages,- };- };--export const collectTextInstances = ({- instances,- rootInstanceId,- textComponents = new Set(["Heading", "Paragraph", "Text"]),-}: {- instances: Instances;- rootInstanceId: Instance["id"];- textComponents?: Set<string>;-}) => {- const textInstances: TextInstance[] = [];-- const rootInstance = instances.get(rootInstanceId);-- if (rootInstance === undefined) {- return textInstances;- }-- const nodeType =- rootInstance.component === "Heading" ||- rootInstance.component === "Paragraph"- ? rootInstance.component- : "Text";-- // Instances can have a number of text child nodes without interleaving components.- // When this is the case we treat the child nodes as a single text node,- // otherwise the AI would generate children.length chunks of separate text.- // To signal that a textInstance is "joint" we set the index to -1.- if (rootInstance.children.every((child) => child.type === "text")) {- textInstances.push({- instanceId: rootInstanceId,- index: -1,- type: nodeType,- text: rootInstance.children.map((child) => child.value).join(" "),- });- } else {- rootInstance.children.forEach((child, index) => {- if (child.type === "text") {- if (textComponents.has(rootInstance.component)) {- textInstances.push({- instanceId: rootInstanceId,- index,- type: nodeType,- text: child.value,- });- }- } else if (child.type === "id") {- textInstances.push(- ...collectTextInstances({- instances,- rootInstanceId: child.value,- textComponents,- })- );- }- });- }-- return textInstances;-};diff --git a/packages/ai/src/chains/copywriter/chain.ts b/packages/ai/src/chains/copywriter/chain.tsindex f9b5a9f9f2..74eefec2b8 100644--- a/packages/ai/src/chains/copywriter/chain.ts+++ b/packages/ai/src/chains/copywriter/chain.ts@@ -5,7 +5,7 @@ import { prompt as promptSystemTemplate } from "./__generated__/copy.system.promimport { prompt as promptUserTemplate } from "./__generated__/copy.user.prompt";import type { Instance, Instances } from "@webstudio-is/sdk";import { createErrorResponse } from "../../utils/create-error-response";-import type { StreamingTextResponseType } from "../../utils/streaming-text-response";+import type { RemixStreamingTextResponse } from "../../utils/remix-streaming-text-response";/*** Copywriter chain.@@ -41,7 +41,7 @@ export type Response = z.infer<typeof ResponseSchema>;export const createChain = <ModelMessageFormat>(): Chain<BaseModel<ModelMessageFormat>,Context,- StreamingTextResponseType+ RemixStreamingTextResponse> =>async function chain({ model, context }) {const { prompt, textInstances } = context;diff --git a/packages/ai/src/index.server.ts b/packages/ai/src/index.server.tsindex bbad01b1fb..3224348835 100644--- a/packages/ai/src/index.server.ts+++ b/packages/ai/src/index.server.ts@@ -15,7 +15,7 @@ export {/** Utils */export * from "./utils/handle-ai-request";export * from "./utils/create-error-response";-export * from "./utils/streaming-text-response";+export * from "./utils/remix-streaming-text-response";/** Types */export * from "./types";diff --git a/packages/ai/src/index.ts b/packages/ai/src/index.tsindex 0ee7c9370f..0249e8c629 100644--- a/packages/ai/src/index.ts+++ b/packages/ai/src/index.ts@@ -7,7 +7,7 @@ export * as templateGenerator from "./chains/template-generator/index";/** Utils */export * from "./utils/handle-ai-request";export * from "./utils/create-error-response";-export * from "./utils/streaming-text-response";+export * from "./utils/remix-streaming-text-response";/** Types */export * from "./types";diff --git a/packages/ai/src/models/README.md b/packages/ai/src/models/README.mdindex 6de68676fd..11168a85a0 100644--- a/packages/ai/src/models/README.md+++ b/packages/ai/src/models/README.md@@ -8,10 +8,10 @@ Each vendor (eg. OpenAI) implementation follows the same API and is passed to [cexport type Model<ModelMessageFormat> = {// Turns ModelMessages into a model-specific messages format.generateMessages: ModelGenerateMessages<ModelMessageFormat>;- // Regular request.- request: ModelRequest<ModelMessageFormat>;- // Streaming.- requestStream: ModelRequestStream<ModelMessageFormat>;+ // JSON completion.+ completion: ModelCompletion<ModelMessageFormat>;+ // Streaming completion.+ completionStream: ModelCompletionStream<ModelMessageFormat>;};```diff --git a/packages/ai/src/models/gpt.ts b/packages/ai/src/models/gpt.tsindex 6b418b9445..c5b32198c7 100644--- a/packages/ai/src/models/gpt.ts+++ b/packages/ai/src/models/gpt.ts@@ -7,7 +7,7 @@ import type {ModelGenerateMessages,} from "../types";import { createErrorResponse } from "../utils/create-error-response";-import { StreamingTextResponse } from "../utils/streaming-text-response";+import { RemixStreamingTextResponse } from "../utils/remix-streaming-text-response";export type Model = BaseModel<ModelMessageFormat>;export type ModelMessageFormat = OpenAI.Chat.Completions.ChatCompletionMessage;@@ -117,7 +117,7 @@ export const createCompletionStream = (id,type: "stream",success: true,- data: new StreamingTextResponse(stream),+ data: new RemixStreamingTextResponse(stream),tokens: {prompt: -1,completion: -1,diff --git a/packages/ai/src/types.ts b/packages/ai/src/types.tsindex ad1a6af88e..885998430c 100644--- a/packages/ai/src/types.ts+++ b/packages/ai/src/types.ts@@ -1,4 +1,4 @@-import type { StreamingTextResponseType } from "./utils/streaming-text-response";+import { type RemixStreamingTextResponse } from "./utils/remix-streaming-text-response";/*** Generic Response types used both by Models and Chains.@@ -13,7 +13,7 @@ export type Tokens = {};export type SuccessResponse<ResponseData> = {- type: ResponseData extends StreamingTextResponseType ? "stream" : "json";+ type: ResponseData extends RemixStreamingTextResponse ? "stream" : "json";success: true;tokens: Tokens;data: ResponseData;@@ -63,7 +63,7 @@ export type ModelCompletion<ModelMessageFormat> = (args: {export type ModelCompletionStream<ModelMessageFormat> = (args: {id: string;messages: ReturnType<ModelGenerateMessages<ModelMessageFormat>>;-}) => Promise<Response<StreamingTextResponseType>>;+}) => Promise<Response<RemixStreamingTextResponse>>;/*** Chains types.diff --git a/packages/ai/src/utils/handle-ai-request.ts b/packages/ai/src/utils/handle-ai-request.tsindex 0c7ec6764d..6e13e5d266 100644--- a/packages/ai/src/utils/handle-ai-request.ts+++ b/packages/ai/src/utils/handle-ai-request.ts@@ -1,7 +1,7 @@import { createChunkDecoder } from "ai";import type { ModelResponse } from "../types";import { createErrorResponse } from "./create-error-response";-import { StreamingTextResponse } from "./streaming-text-response";+import { RemixStreamingTextResponse } from "./remix-streaming-text-response";type RequestOptions = {signal: AbortSignal;@@ -75,7 +75,7 @@ export const handleAiRequest = <ResponseData>(id: operationId,type: "stream",success: true,- data: new StreamingTextResponse(+ data: new RemixStreamingTextResponse(new Blob([completion], { type: "text/plain" }).stream()),tokens: { prompt: -1, completion: -1 },diff --git a/packages/ai/src/utils/streaming-text-response.ts b/packages/ai/src/utils/remix-streaming-text-response.tssimilarity index 66%rename from packages/ai/src/utils/streaming-text-response.tsrename to packages/ai/src/utils/remix-streaming-text-response.tsindex 58c02bf657..0c8350cdb2 100644--- a/packages/ai/src/utils/streaming-text-response.ts+++ b/packages/ai/src/utils/remix-streaming-text-response.ts@@ -1,11 +1,9 @@-import { StreamingTextResponse as _StreamingTextResponse } from "ai";--export type StreamingTextResponseType = StreamingTextResponse;+import { StreamingTextResponse } from "ai";// vercel/ai's StreamingTextResponse does not include request.headers.raw()// which @vercel/remix uses when deployed on vercel.// Therefore we use a custom one.-export class StreamingTextResponse extends _StreamingTextResponse {+export class RemixStreamingTextResponse extends StreamingTextResponse {constructor(res: ReadableStream, init?: ResponseInit) {super(res, init);this.getRequestHeaders();@@ -16,16 +14,15 @@ export class StreamingTextResponse extends _StreamingTextResponse {}}-const addRawHeaders = function addRawHeaders(headers: Headers) {+const addRawHeaders = (headers: Headers) => {// eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-ignore- headers.raw = function () {+ headers.raw = () => {const rawHeaders: { [k in string]: string[] } = {};const headerEntries = headers.entries();for (const [key, value] of headerEntries) {const headerKey = key.toLowerCase();- // eslint-disable-next-line no-prototype-builtins- if (rawHeaders.hasOwnProperty(headerKey)) {+ if (headerKey in rawHeaders) {rawHeaders[headerKey].push(value);} else {rawHeaders[headerKey] = [value];