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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/api/channel/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { NextResponse } from "next/server";

import { getS3Bucket, s3Client } from "../../config";
import { isArchivedHtmlChannel } from "../shared/channelArchive";
import {
type ChannelRequestBody,
isChannelRequestBody,
Expand Down Expand Up @@ -52,9 +53,17 @@ export async function POST(req: Request) {
}

try {
if (!(await isArchivedHtmlChannel(requestBody.key, req.signal))) {
return NextResponse.json(
{ error: "This channel is not part of the archived HTML collection." },
{ status: 404, headers: { "Cache-Control": "no-store" } },
);
}

const bucketParams = {
Bucket: getS3Bucket(),
Key: requestBody.key,
ResponseCacheControl: "private, no-store",
};

const url = await getSignedUrl(
Expand Down
22 changes: 22 additions & 0 deletions app/api/chat/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "server-only";

import { createAnthropic } from "@ai-sdk/anthropic";
import { createGoogle } from "@ai-sdk/google";
import { createOpenAI } from "@ai-sdk/openai";

import type { InferenceProvider } from "../../chatConfig";

export function createChatLanguageModel(
provider: InferenceProvider,
model: string,
apiKey: string,
) {
switch (provider) {
case "anthropic":
return createAnthropic({ apiKey })(model);
case "google":
return createGoogle({ apiKey })(model);
case "openai":
return createOpenAI({ apiKey })(model);
}
}
115 changes: 115 additions & 0 deletions app/api/chat/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { isInferenceProvider, type InferenceProvider } from "../../chatConfig";

const API_KEY_MAX_LENGTH = 512;
const CHANNEL_KEY_MAX_LENGTH = 1_024;
const CHAT_HISTORY_MAX_CHARACTERS = 40_000;
const CHAT_MESSAGE_MAX_CHARACTERS = 6_000;
const CHAT_MESSAGE_MAX_COUNT = 24;
const MODEL_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$/;

type ChatHistoryMessage = {
role: "assistant" | "user";
text: string;
};

export type ChannelChatRequest = {
apiKey: string;
channelKey: string;
messages: ChatHistoryMessage[];
model: string;
provider: InferenceProvider;
};

function parseMessages(value: unknown): ChatHistoryMessage[] | null {
if (
!Array.isArray(value) ||
value.length === 0 ||
value.length > CHAT_MESSAGE_MAX_COUNT
) {
return null;
}

const messages: ChatHistoryMessage[] = [];
let totalCharacters = 0;

for (const item of value) {
if (!item || typeof item !== "object") return null;

const candidate = item as {
parts?: unknown;
role?: unknown;
};
if (candidate.role !== "assistant" && candidate.role !== "user") {
return null;
}
if (!Array.isArray(candidate.parts) || candidate.parts.length === 0) {
return null;
}

const textParts: string[] = [];
for (const part of candidate.parts) {
if (!part || typeof part !== "object") return null;
const typedPart = part as { text?: unknown; type?: unknown };

if (typedPart.type === "step-start") continue;
if (typedPart.type !== "text" || typeof typedPart.text !== "string") {
return null;
}
textParts.push(typedPart.text);
}

const text = textParts.join("\n").trim();
if (!text || text.length > CHAT_MESSAGE_MAX_CHARACTERS) return null;

totalCharacters += text.length;
if (totalCharacters > CHAT_HISTORY_MAX_CHARACTERS) return null;

messages.push({ role: candidate.role, text });
}

return messages.at(-1)?.role === "user" ? messages : null;
}

export function parseChannelChatRequest(
value: unknown,
): ChannelChatRequest | null {
if (!value || typeof value !== "object") return null;

const candidate = value as {
apiKey?: unknown;
channelKey?: unknown;
messages?: unknown;
model?: unknown;
provider?: unknown;
};
const apiKey =
typeof candidate.apiKey === "string" ? candidate.apiKey.trim() : "";
const channelKey =
typeof candidate.channelKey === "string" ? candidate.channelKey.trim() : "";
const model =
typeof candidate.model === "string" ? candidate.model.trim() : "";
const messages = parseMessages(candidate.messages);

if (
!isInferenceProvider(candidate.provider) ||
apiKey.length < 8 ||
apiKey.length > API_KEY_MAX_LENGTH ||
/[\r\n]/.test(apiKey) ||
!channelKey ||
channelKey.length > CHANNEL_KEY_MAX_LENGTH ||
channelKey.includes("\0") ||
!channelKey.toLowerCase().endsWith(".html") ||
!MODEL_ID_PATTERN.test(model) ||
!messages
) {
return null;
}

return {
apiKey,
channelKey,
messages,
model,
provider: candidate.provider,
};
}
Loading