aboutsummaryrefslogtreecommitdiff
path: root/castle-search/src/http.ts
diff options
context:
space:
mode:
Diffstat (limited to 'castle-search/src/http.ts')
-rw-r--r--castle-search/src/http.ts99
1 files changed, 96 insertions, 3 deletions
diff --git a/castle-search/src/http.ts b/castle-search/src/http.ts
index 076c624..d19b9f7 100644
--- a/castle-search/src/http.ts
+++ b/castle-search/src/http.ts
@@ -24,8 +24,9 @@
* through the browser's signed-in X session, and GET /openapi.json describes
* them, so that Open WebUI can offer them as tools ("X.com"). POST
* /youtube-transcript files a YouTube video's transcript and returns it
- * (youtube.ts), described at GET /youtube/openapi.json ("YouTube"). Each
- * answers 502 with `{ "error" }` on failure.
+ * (youtube.ts), described at GET /youtube/openapi.json ("YouTube"). POST
+ * /video makes a short video clip and files it (video.ts), described at GET
+ * /video/openapi.json ("Video"). Each answers 502 with `{ "error" }` on failure.
*/
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
@@ -49,6 +50,7 @@ import {
type XUserOutcome,
type XUserRequest,
} from "./x-search.ts";
+import { CLIPS_PER_DAY, LONG_SECONDS, makeVideo, VIDEO_KINDS, type VideoKind, type VideoOutcome, type VideoRequest } from "./video.ts";
import { DEFAULT_MAX_CHARS, type TranscriptOutcome, type TranscriptRequest, videoIdOf, youtubeTranscript } from "./youtube.ts";
export const DEFAULT_LISTEN = "127.0.0.1:11158";
@@ -193,6 +195,49 @@ export const handleTranscript = async (raw: string, run: TranscriptFetcher, sign
return answer(() => run({ url, language: language as string | undefined, maxChars }, signal));
};
+export type VideoMaker = (request: VideoRequest) => Promise<VideoOutcome>;
+
+const MAX_PROMPT_CHARS = 2000;
+
+/** POST /video: `{ prompt, kind?, orientation?, seconds?, image_url? }` in, where the clip was filed out. */
+export const handleVideo = async (raw: string, run: VideoMaker): Promise<Reply> => {
+ const body = jsonBody(raw);
+ if (isReply(body)) return body;
+ const { prompt, orientation, seconds } = body;
+ const kind = body["kind"] ?? "fast";
+ const imageUrl = body["image_url"];
+ if (typeof prompt !== "string" || prompt.trim() === "" || prompt.length > MAX_PROMPT_CHARS) {
+ return { status: 400, body: { error: `prompt must be a description of the clip, at most ${MAX_PROMPT_CHARS} characters` } };
+ }
+ if (!VIDEO_KINDS.includes(kind as VideoKind)) {
+ return { status: 400, body: { error: `kind must be one of ${VIDEO_KINDS.join(", ")}` } };
+ }
+ if (orientation !== undefined && orientation !== null && orientation !== "landscape" && orientation !== "portrait") {
+ return { status: 400, body: { error: 'orientation must be "landscape" or "portrait"' } };
+ }
+ if (seconds !== undefined && seconds !== null) {
+ if (kind !== "long") return { status: 400, body: { error: "seconds can only be chosen for kind long; the others have a fixed length" } };
+ if (!LONG_SECONDS.includes(seconds as (typeof LONG_SECONDS)[number])) {
+ return { status: 400, body: { error: `seconds must be one of ${LONG_SECONDS.join(", ")}` } };
+ }
+ }
+ if (imageUrl !== undefined && imageUrl !== null) {
+ if (kind !== "long") return { status: 400, body: { error: "image_url (animating a picture) needs kind long" } };
+ if (typeof imageUrl !== "string" || !/^(https:\/\/|data:image\/)/.test(imageUrl)) {
+ return { status: 400, body: { error: "image_url must be an https link to the picture or a data:image URI" } };
+ }
+ }
+ return answer(() =>
+ run({
+ prompt: prompt.trim(),
+ kind: kind as VideoKind,
+ portrait: orientation === "portrait",
+ seconds: typeof seconds === "number" ? seconds : undefined,
+ imageUrl: typeof imageUrl === "string" ? imageUrl : undefined,
+ }),
+ );
+};
+
const jsonSchema = (properties: Record<string, unknown>, required: string[]) => ({
required: true,
content: { "application/json": { schema: { type: "object", required, properties } } },
@@ -326,6 +371,49 @@ export const YOUTUBE_OPENAPI = {
},
} as const;
+/** The video tool's own description, so Open WebUI lists it on its own and it can stay off by default. */
+export const VIDEO_OPENAPI = {
+ openapi: "3.1.0",
+ info: {
+ title: "Video",
+ version: "1.0.0",
+ description: "Makes a short video clip from a description and files it in Igor's Nextcloud video folder. Costs money per clip.",
+ },
+ paths: {
+ "/video": {
+ post: {
+ operationId: "make_video",
+ summary: "Make a short video clip",
+ description:
+ "Make a video clip from a text description, only when asked for a video. It takes from under a " +
+ "minute to about three, costs money for every clip, and is limited to " +
+ `${CLIPS_PER_DAY} clips a day, so never make one unasked or retry one that failed. Pick kind by what ` +
+ "is asked: fast (5 s, no sound, about $0.02) unless the request calls for another; quality (5 s, " +
+ "no sound, sharper and smoother, about $0.17) when quality is asked for or a fast clip was not " +
+ "good enough; long (6 to 20 s with sound, $0.03 a second) for a clip longer than 5 s or to animate " +
+ "a picture; sound (8 s, the best quality, with realistic sound and speech, about $1.20) when the " +
+ "clip needs dialogue, voices, music or sound that matters. Describe the scene densely: the " +
+ "subject, the action, the setting, the light and the camera. The clip is filed in Igor's " +
+ "Nextcloud video folder: tell him the file name and give the folder link.",
+ requestBody: jsonSchema(
+ {
+ prompt: { type: "string", description: "What the clip shows: subject, action, setting, light, camera movement; for sound, what is heard or said" },
+ kind: { type: "string", enum: [...VIDEO_KINDS], default: "fast", description: "fast, quality, long or sound: see the description" },
+ orientation: { type: "string", enum: ["landscape", "portrait"], default: "landscape", description: "landscape 16:9 or portrait 9:16" },
+ seconds: { type: "integer", enum: [...LONG_SECONDS], default: 6, description: "Length, for kind long only" },
+ image_url: { type: "string", description: "For kind long only: a picture to start the clip from, as an https link or a data:image URI" },
+ },
+ ["prompt"],
+ ),
+ responses: {
+ "200": { description: "Where the clip was filed, and what it cost", content: { "application/json": { schema: { type: "object" } } } },
+ "502": { description: "No clip; error says why", content: { "application/json": { schema: { type: "object" } } } },
+ },
+ },
+ },
+ },
+} as const;
+
const readBody = (req: IncomingMessage): Promise<string> =>
new Promise((resolve, reject) => {
let size = 0;
@@ -376,16 +464,21 @@ const main = (): void => {
send(res, { status: 200, body: YOUTUBE_OPENAPI });
return;
}
+ if (req.method === "GET" && path === "/video/openapi.json") {
+ send(res, { status: 200, body: VIDEO_OPENAPI });
+ return;
+ }
const xRoutes: Record<string, (raw: string, signal: AbortSignal) => Promise<Reply>> = {
"/x-search": (raw, signal) => handleXSearch(raw, xSearch, signal),
"/x-post": (raw, signal) => handleXPost(raw, xPost, signal),
"/x-user": (raw, signal) => handleXUser(raw, xUser, signal),
"/x-trends": (raw, signal) => handleXTrends(raw, xTrends, signal),
"/youtube-transcript": (raw, signal) => handleTranscript(raw, youtubeTranscript, signal),
+ "/video": (raw) => handleVideo(raw, makeVideo),
};
const xRoute = xRoutes[path ?? ""];
if (req.method !== "POST" || (path !== "/" && path !== "/search" && !xRoute)) {
- send(res, { status: 404, body: { error: "POST a search to / or /search, an X read to /x-search, /x-post, /x-user or /x-trends, or a YouTube link to /youtube-transcript" } });
+ send(res, { status: 404, body: { error: "POST a search to / or /search, an X read to /x-search, /x-post, /x-user or /x-trends, a YouTube link to /youtube-transcript, or a clip description to /video" } });
return;
}
// Stop the search, and free its tab, if the client goes away first.