Open source CapCut alternative.
TypeScript
site/donkeyvision: rebuild API section as an interactive API reference
David Le4aad171224ba
diff --git a/site/src/app/donkeyvision/ApiReference.tsx b/site/src/app/donkeyvision/ApiReference.tsxnew file mode 100644index 00000000..21a6ea25--- /dev/null+++ b/site/src/app/donkeyvision/ApiReference.tsx@@ -0,0 +1,203 @@+"use client";++import { Check, Copy } from "lucide-react";+import { useState } from "react";++import {+ API_ENDPOINT,+ BODY_PARAMS,+ CODE_SAMPLES,+ RESPONSE_FIELDS,+ RESPONSE_SAMPLE,+ type ApiField,+} from "@/app/donkeyvision/apiContract";+import { HighlightedCode } from "@/app/donkeyvision/codeHighlight";++function CopyButton({ value, label }: { value: string; label: string }) {+ const [copied, setCopied] = useState(false);++ return (+ <button+ type="button"+ aria-label={`Copy ${label}`}+ onClick={() => {+ void navigator.clipboard.writeText(value).then(() => {+ setCopied(true);+ setTimeout(() => setCopied(false), 1500);+ });+ }}+ className="inline-flex items-center justify-center rounded-md border border-white/20 p-1.5 text-white/70 transition hover:bg-white/10 hover:text-white"+ >+ {copied ? (+ <Check size={14} aria-hidden="true" />+ ) : (+ <Copy size={14} aria-hidden="true" />+ )}+ </button>+ );+}++function CodePanel({ children }: { children: string }) {+ return (+ <pre className="max-w-full overflow-x-auto px-4 py-4 text-xs leading-6 text-white md:text-[13px]">+ <code>+ <HighlightedCode code={children} />+ </code>+ </pre>+ );+}++function RequestExamples() {+ const [active, setActive] = useState(CODE_SAMPLES[0].key);+ const sample =+ CODE_SAMPLES.find((s) => s.key === active) ?? CODE_SAMPLES[0];++ return (+ <div className="overflow-hidden rounded-lg border-2 border-[#0F0E0D] bg-[#0F0E0D]">+ <div className="flex items-center justify-between gap-2 border-b border-white/15 pl-2 pr-3">+ <div className="flex items-center">+ {CODE_SAMPLES.map((s) => {+ const isActive = s.key === active;+ return (+ <button+ key={s.key}+ type="button"+ onClick={() => setActive(s.key)}+ aria-pressed={isActive}+ className={`relative px-3 py-3 text-sm font-semibold transition-colors ${+ isActive+ ? "text-white"+ : "text-white/45 hover:text-white/75"+ }`}+ >+ {s.label}+ {isActive ? (+ <span className="absolute inset-x-2 -bottom-px h-0.5 rounded-full bg-[#F5D875]" />+ ) : null}+ </button>+ );+ })}+ </div>+ <CopyButton value={sample.code} label={`${sample.label} request`} />+ </div>+ <CodePanel>{sample.code}</CodePanel>+ </div>+ );+}++function ResponseExample() {+ return (+ <div className="overflow-hidden rounded-lg border-2 border-[#0F0E0D] bg-[#0F0E0D]">+ <div className="flex items-center justify-between border-b border-white/15 px-4 py-2.5">+ <div className="flex items-center gap-2 text-sm font-semibold text-white">+ <span className="inline-flex items-center rounded-full bg-[#34C759]/15 px-2 py-0.5 font-mono text-xs text-[#34C759]">+ 200+ </span>+ Response+ </div>+ <CopyButton value={RESPONSE_SAMPLE} label="response" />+ </div>+ <CodePanel>{RESPONSE_SAMPLE}</CodePanel>+ </div>+ );+}++function FieldList({ fields }: { fields: ApiField[] }) {+ return (+ <dl className="divide-y divide-[#0F0E0D]/10 rounded-lg border-2 border-[#0F0E0D] bg-white">+ {fields.map((field) => (+ <div key={field.name} className="px-4 py-3.5">+ <dt className="flex flex-wrap items-center gap-2">+ <span className="font-mono text-sm font-semibold text-[#0F0E0D]">+ {field.name}+ </span>+ <span className="rounded bg-[#0F0E0D]/8 px-1.5 py-0.5 font-mono text-[11px] text-[#0F0E0D]/70">+ {field.type}+ </span>+ <span+ className={`rounded px-1.5 py-0.5 text-[11px] font-semibold ${+ field.required+ ? "bg-[#EC7868]/20 text-[#C0432F]"+ : "bg-[#0F0E0D]/5 text-[#0F0E0D]/45"+ }`}+ >+ {field.required ? "required" : "optional"}+ </span>+ </dt>+ <dd className="mt-1 text-sm leading-6 text-[#555]">+ {field.description}+ </dd>+ </div>+ ))}+ </dl>+ );+}++export function ApiReference() {+ return (+ <div className="mt-16">+ {/* Endpoint */}+ <div className="flex flex-col gap-3 rounded-lg border-2 border-[#0F0E0D] bg-white p-4 sm:flex-row sm:items-center sm:justify-between">+ <div className="flex min-w-0 items-center gap-3">+ <span className="inline-flex shrink-0 items-center rounded-md border-2 border-[#0F0E0D] bg-[#F5D875] px-2.5 py-1 text-xs font-bold uppercase tracking-[0.08em]">+ {API_ENDPOINT.method}+ </span>+ <code className="truncate font-mono text-sm font-semibold text-[#0F0E0D] md:text-base">+ {API_ENDPOINT.path}+ </code>+ </div>+ <p className="shrink-0 text-sm text-[#555]">+ Authenticate with{" "}+ <code className="rounded bg-[#0F0E0D]/8 px-1.5 py-0.5 font-mono text-[0.85em] text-[#0F0E0D]">+ Authorization: Bearer dk_live_…+ </code>+ </p>+ </div>++ <div className="mt-6 grid gap-8 lg:grid-cols-2">+ {/* Request side */}+ <div className="min-w-0">+ <h3 className="text-sm font-bold uppercase tracking-[0.12em] text-[#0F0E0D]">+ Request+ </h3>+ <p className="mt-2 text-sm leading-6 text-[#555]">+ Send your screenshot as base64. Add an{" "}+ <code className="rounded bg-[#0F0E0D]/8 px-1 py-0.5 font-mono text-[0.85em] text-[#0F0E0D]">+ instruction+ </code>{" "}+ and you also get back the one spot to click.+ </p>+ <div className="mt-4">+ <RequestExamples />+ </div>+ <h4 className="mt-8 text-sm font-bold uppercase tracking-[0.12em] text-[#0F0E0D]">+ Body parameters+ </h4>+ <div className="mt-4">+ <FieldList fields={BODY_PARAMS} />+ </div>+ </div>++ {/* Response side */}+ <div className="min-w-0">+ <h3 className="text-sm font-bold uppercase tracking-[0.12em] text-[#0F0E0D]">+ Response+ </h3>+ <p className="mt-2 text-sm leading-6 text-[#555]">+ The numbers are pixels in the screenshot you sent, counted from the+ top-left corner. You can click them right away.+ </p>+ <div className="mt-4">+ <ResponseExample />+ </div>+ <h4 className="mt-8 text-sm font-bold uppercase tracking-[0.12em] text-[#0F0E0D]">+ Response fields+ </h4>+ <div className="mt-4">+ <FieldList fields={RESPONSE_FIELDS} />+ </div>+ </div>+ </div>+ </div>+ );+}diff --git a/site/src/app/donkeyvision/ApiSection.tsx b/site/src/app/donkeyvision/ApiSection.tsxindex 64e3c048..3ba843d6 100644--- a/site/src/app/donkeyvision/ApiSection.tsx+++ b/site/src/app/donkeyvision/ApiSection.tsx@@ -1,94 +1,44 @@-import { ArrowDown, Braces } from "lucide-react";--import {- elementResponseExample,- features,- requestExample,-} from "@/app/donkeyvision/data";+import { ApiReference } from "@/app/donkeyvision/ApiReference";+import { features } from "@/app/donkeyvision/data";import type { Feature } from "@/app/donkeyvision/types";type FeatureLineProps = {feature: Feature;};-type ExampleBlockProps = {- code: string;- title: string;-};--type ExamplePairProps = {- badge: string;- caption: string;- request: string;- requestTitle: string;- response: string;- responseTitle: string;-};-export function ApiSection() {return (<section- className="border-y-2 border-[#0F0E0D] bg-[#FAF6EC] px-6 py-20 md:px-12"+ className="border-b-2 border-[#0F0E0D] bg-[#FAF6EC] px-6 py-20 md:px-12"id="api"><div className="mx-auto max-w-[1400px]">- <div className="grid gap-10 lg:grid-cols-[0.85fr_1.15fr]">- <div className="min-w-0">- <h2 className="max-w-2xl text-4xl font-semibold leading-none md:text-6xl">- One endpoint, one job.- </h2>- <p className="mt-6 max-w-2xl text-lg leading-8 text-[#454545]">- Send a screenshot and every request returns the same compact- element catalog: IDs, labels, kinds, boxes, center points, and- interactivity flags. Coordinates come straight from the parser, in- the image space you sent.- </p>- <div className="mt-8 grid gap-4">- {features.map((feature) => (- <FeatureLine feature={feature} key={feature.title} />- ))}- </div>- </div>- <div className="grid min-w-0 gap-8">- <ExamplePair- badge="Parse"- caption="Detect every interactable element."- request={requestExample}- requestTitle="Request"- response={elementResponseExample}- responseTitle="Response"- />- </div>+ <div className="max-w-3xl">+ <h2 className="text-4xl font-semibold leading-none md:text-6xl">+ Here’s how the API works.+ </h2>+ <p className="mt-6 text-lg leading-8 text-[#454545]">+ Send a screenshot to <Code>/parse</Code>. The response is a list of+ detected UI elements with IDs, labels, types, boxes, center points,+ and confidence scores.+ </p>+ <p className="mt-4 text-lg leading-8 text-[#454545]">+ You can also send a text prompt, like{" "}+ <Code>click the play button</Code>, and get back the exact region to+ click. Use your choice of LLM: ChatGPT, Claude, Gemini, or a custom+ model.+ </p></div>- </div>- </section>- );-}-function ExamplePair({- badge,- caption,- request,- requestTitle,- response,- responseTitle,-}: ExamplePairProps) {- return (- <div className="min-w-0">- <div className="mb-3 flex items-center gap-3">- <span className="inline-flex items-center rounded-full border-2 border-[#0F0E0D] bg-[#F5D875] px-3 py-1 text-xs font-semibold uppercase tracking-[0.08em]">- {badge}- </span>- <span className="text-sm font-semibold text-[#0F0E0D]">{caption}</span>- </div>- <div className="grid gap-2">- <ExampleBlock code={request} title={requestTitle} />- <div className="flex justify-center">- <ArrowDown size={18} className="text-[#0F0E0D]" aria-hidden="true" />+ <div className="mt-10 grid gap-x-10 gap-y-8 sm:grid-cols-2 lg:grid-cols-3">+ {features.map((feature) => (+ <FeatureLine feature={feature} key={feature.title} />+ ))}</div>- <ExampleBlock code={response} title={responseTitle} />++ <ApiReference /></div>- </div>+ </section>);}@@ -96,28 +46,26 @@ function FeatureLine({ feature }: FeatureLineProps) {const Icon = feature.icon;return (- <div className="flex gap-4 rounded-lg border-2 border-[#0F0E0D] bg-white p-4">- <div className="flex h-11 w-11 shrink-0 items-center justify-center rounded-md border-2 border-[#0F0E0D] bg-[#A8D5E8]">- <Icon size={20} aria-hidden="true" />- </div>+ <div className="flex gap-3">+ <Icon+ size={20}+ aria-hidden="true"+ className="mt-0.5 shrink-0 text-[#0F0E0D]"+ /><div>- <h3 className="text-lg font-semibold">{feature.title}</h3>- <p className="mt-1 text-sm leading-6 text-[#555]">{feature.description}</p>+ <h3 className="text-base font-semibold">{feature.title}</h3>+ <p className="mt-1 text-sm leading-6 text-[#555]">+ {feature.description}+ </p></div></div>);}-function ExampleBlock({ code, title }: ExampleBlockProps) {+function Code({ children }: { children: string }) {return (- <div className="min-w-0 max-w-full overflow-hidden rounded-lg border-2 border-[#0F0E0D] bg-[#0F0E0D]">- <div className="flex items-center justify-between border-b-2 border-white/15 px-4 py-3 text-sm font-semibold text-white">- <span>{title}</span>- <Braces size={16} aria-hidden="true" />- </div>- <pre className="max-w-full overflow-x-auto p-4 text-xs leading-6 text-white md:text-sm">- <code>{code}</code>- </pre>- </div>+ <code className="rounded bg-[#0F0E0D]/8 px-1.5 py-0.5 font-mono text-[0.85em] text-[#0F0E0D]">+ {children}+ </code>);}diff --git a/site/src/app/donkeyvision/apiContract.ts b/site/src/app/donkeyvision/apiContract.tsnew file mode 100644index 00000000..5af5f286--- /dev/null+++ b/site/src/app/donkeyvision/apiContract.ts@@ -0,0 +1,166 @@+// Public Donkey Vision API contract for POST /api/inference/vision.+// Keep this in sync with src/lib/inference/vision/schema.ts.++export const API_ENDPOINT = {+ method: "POST",+ path: "/api/inference/vision",+ host: "https://donkeyuse.com",+} as const;++export type CodeLanguage = {+ key: string;+ label: string;+ code: string;+};++const url = `${API_ENDPOINT.host}${API_ENDPOINT.path}`;++export const CODE_SAMPLES: CodeLanguage[] = [+ {+ key: "typescript",+ label: "TypeScript",+ code: `const res = await fetch("${url}", {+ method: "POST",+ headers: {+ Authorization: \`Bearer \${process.env.DONKEY_API_KEY}\`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ // base64 png/jpeg/webp, no "data:" prefix+ image: screenshotBase64,+ instruction: "click the play button",+ returnElements: true,+ }),+});++const { target } = await res.json();+console.log(target.point); // { x, y } — ready to click`,+ },+ {+ key: "python",+ label: "Python",+ code: `import os, requests++res = requests.post(+ "${url}",+ headers={"Authorization": f"Bearer {os.environ['DONKEY_API_KEY']}"},+ json={+ # base64 png/jpeg/webp, no "data:" prefix+ "image": screenshot_base64,+ "instruction": "click the play button",+ "returnElements": True,+ },+)++target = res.json()["target"]+print(target["point"]) # { x, y } — ready to click`,+ },+ {+ key: "swift",+ label: "Swift",+ code: `var request = URLRequest(url: URL(string: "${url}")!)+request.httpMethod = "POST"+request.setValue("Bearer \\(apiKey)", forHTTPHeaderField: "Authorization")+request.setValue("application/json", forHTTPHeaderField: "Content-Type")+request.httpBody = try JSONSerialization.data(withJSONObject: [+ // base64 png/jpeg/webp, no "data:" prefix+ "image": screenshotBase64,+ "instruction": "click the play button",+ "returnElements": true,+])++let (data, _) = try await URLSession.shared.data(for: request)+let result = try JSONDecoder().decode(VisionResponse.self, from: data)+print(result.target?.point) // { x, y } — ready to click`,+ },+];++export const RESPONSE_SAMPLE = `{+ "image": { "width": 1440, "height": 900 },+ "elements": [+ {+ "id": "a92kfq",+ "label": "Play",+ "kind": "button",+ "interactive": true,+ "box": { "x": 618, "y": 816, "width": 42, "height": 42 },+ "point": { "x": 639, "y": 837 },+ "confidence": 0.82+ }+ ],+ "target": {+ "elementId": "a92kfq",+ "label": "Play",+ "kind": "button",+ "box": { "x": 618, "y": 816, "width": 42, "height": 42 },+ "point": { "x": 639, "y": 837 },+ "confidence": 0.91+ },+ "alternates": [],+ "model": "gemini-3.1-flash-lite"+}`;++export type ApiField = {+ name: string;+ type: string;+ required?: boolean;+ description: string;+};++export const BODY_PARAMS: ApiField[] = [+ {+ name: "image",+ type: "string",+ required: true,+ description:+ "Your screenshot as base64 text (PNG, JPEG, or WebP). For the best results, make it a JPEG at quality 0.8 and shrink it so the longest side is 1568 pixels.",+ },+ {+ name: "instruction",+ type: "string",+ description:+ "Tell it what to click in plain words, like “click the play button”. Then you also get back the one spot to click.",+ },+ {+ name: "model",+ type: "string",+ description:+ "Which model picks the spot to click. Use gemini-3.5-flash or gemini-3.1-flash-lite. If you don’t pick, it uses gemini-3.1-flash-lite.",+ },+ {+ name: "returnElements",+ type: "boolean",+ description:+ "Set to true to get the full list of things it found. It’s on by default, and off when you send an instruction.",+ },+];++export const RESPONSE_FIELDS: ApiField[] = [+ {+ name: "image",+ type: "object",+ description: "How big your screenshot is, in pixels: { width, height }.",+ },+ {+ name: "elements",+ type: "array",+ description:+ "Everything it found. Each one has an id, label, kind, interactive, box, point, and confidence. Left out when returnElements is false.",+ },+ {+ name: "target",+ type: "object | null",+ description:+ "The one thing to click for your instruction, with its box and click point. Only here when you send an instruction.",+ },+ {+ name: "alternates",+ type: "array",+ description: "Other things that might match your instruction, best ones first.",+ },+ {+ name: "model",+ type: "string",+ description: "The model that picked the spot to click.",+ },+];diff --git a/site/src/app/donkeyvision/codeHighlight.tsx b/site/src/app/donkeyvision/codeHighlight.tsxnew file mode 100644index 00000000..276e1a31--- /dev/null+++ b/site/src/app/donkeyvision/codeHighlight.tsx@@ -0,0 +1,81 @@+import type { ReactNode } from "react";++// Lightweight, language-agnostic syntax highlighter for the API reference code+// samples (TypeScript, Swift, Python) and the JSON response. It is not a full+// grammar — it tokenizes the shapes these snippets share (comments, strings,+// numbers, keywords, JSON keys, calls) and paints them with editor-style+// colors, which is enough to read like a code editor without a dependency.++// VS Code Dark+ palette, tuned for the near-black (#0F0E0D) panel background.+const COLOR = {+ comment: "#6A9955",+ string: "#CE9178",+ number: "#B5CEA8",+ keyword: "#569CD6",+ property: "#9CDCFE",+ func: "#DCDCAA",+};++const KEYWORDS = new Set([+ // shared across the three languages used in the samples+ "const", "let", "var", "await", "async", "function", "return", "try",+ "import", "from", "new", "def", "print", "true", "false", "null",+ "True", "False", "None", "as", "in",+]);++const TOKENIZER_SOURCE =+ "(\\/\\/[^\\n]*|#[^\\n]*|\\/\\*[\\s\\S]*?\\*\\/)|(\"(?:\\\\.|[^\"\\\\])*\"(?=\\s*:))|(`(?:\\\\.|[^`\\\\])*`|\"(?:\\\\.|[^\"\\\\])*\"|'(?:\\\\.|[^'\\\\])*')|(\\b\\d+(?:\\.\\d+)?\\b)|([A-Za-z_$][\\w$]*)";++export function HighlightedCode({ code }: { code: string }): ReactNode {+ const tokenizer = new RegExp(TOKENIZER_SOURCE, "g");+ const nodes: ReactNode[] = [];+ let lastIndex = 0;+ let key = 0;+ let match: RegExpExecArray | null;++ while ((match = tokenizer.exec(code)) !== null) {+ if (match.index > lastIndex) {+ nodes.push(code.slice(lastIndex, match.index));+ }++ const [full, comment, property, string, number, ident] = match;+ let color: string | null = null;++ if (comment) {+ color = COLOR.comment;+ } else if (property) {+ color = COLOR.property;+ } else if (string) {+ color = COLOR.string;+ } else if (number) {+ color = COLOR.number;+ } else if (ident) {+ if (KEYWORDS.has(ident)) {+ color = COLOR.keyword;+ } else {+ // A trailing "(" makes it read as a call; otherwise leave it default.+ let j = tokenizer.lastIndex;+ while (j < code.length && /\s/.test(code[j])) j += 1;+ if (code[j] === "(") color = COLOR.func;+ }+ }++ nodes.push(+ color ? (+ <span key={key} style={{ color }}>+ {full}+ </span>+ ) : (+ full+ ),+ );+ key += 1;+ lastIndex = tokenizer.lastIndex;+ }++ if (lastIndex < code.length) {+ nodes.push(code.slice(lastIndex));+ }++ return nodes;+}diff --git a/site/src/app/donkeyvision/data.ts b/site/src/app/donkeyvision/data.tsindex e865e183..e15452c4 100644--- a/site/src/app/donkeyvision/data.ts+++ b/site/src/app/donkeyvision/data.ts@@ -3,8 +3,10 @@ import {Crosshair,ImageIcon,ListChecks,- Monitor,+ MousePointerClick,SlidersHorizontal,+ Sparkles,+ Type,Video,Zap,} from "lucide-react";@@ -15,7 +17,7 @@ export const stats: Stat[] = [{eyebrow: "Server-side",label:- "Full time on our side to process a screenshot — detect, OCR, and label every element, ready to return. The part we control.",+ "Process a screenshot into structured UI elements — boxes, labels, OCR, and click coordinates — in under a second.",value: "~0.7s",},];@@ -23,27 +25,39 @@ export const stats: Stat[] = [export const features: Feature[] = [{description:- "Return every detected button, icon, input, row, link, and text target from a screenshot with boxes and center points.",+ "Buttons, icons, inputs, links, rows, text targets, and other visible UI regions.",icon: Boxes,- title: "All interactable elements",+ title: "Detected elements",},{description:- "Each element comes back with a pixel-space bounding box and a center point, ready to click without any coordinate conversion.",+ "Each element includes a bounding box and center point in the same image coordinate space.",icon: Crosshair,- title: "Click-ready coordinates",+ title: "Coordinates",},{description:- "Works on native Mac apps, web apps, Electron shells, VNC sessions, enterprise tools, games, and remote desktops.",- icon: Monitor,- title: "Any software surface",+ "Each element includes a readable label and kind, such as button, input, icon, or text.",+ icon: Type,+ title: "Labels and types",},{description:- "Tune box and IoU thresholds per request to control detection sensitivity and how aggressively overlapping elements are merged.",+ "Send a natural language instruction and get back the matching element, click point, and region.",+ icon: MousePointerClick,+ title: "Prompt-to-click",+ },+ {+ description:+ "Adjust thresholds per request to control sensitivity and element merging.",icon: SlidersHorizontal,- title: "Tunable detection",+ title: "Detection options",+ },+ {+ description:+ "Use ChatGPT, Claude, Gemini, or bring your own custom model for prompt-based targeting.",+ icon: Sparkles,+ title: "Model choice",},];@@ -82,26 +96,3 @@ export const surfaces: string[] = ["Enterprise and internal tools","Games and media players",];--export const requestExample = `{- "image": "<base64 png/jpeg/webp screenshot>",- "options": {- "boxThreshold": 0.05,- "iouThreshold": 0.1- }-}`;--export const elementResponseExample = `{- "image": { "width": 1440, "height": 900 },- "elements": [- {- "id": "a92kfq",- "label": "Play",- "kind": "button",- "interactive": true,- "box": { "x": 618, "y": 816, "width": 42, "height": 42 },- "point": { "x": 639, "y": 837 },- "confidence": 0.5- }- ]-}`;