// Great Work Labs client for TypeScript/JavaScript. No dependencies; works in Node 18+, Bun, Deno and browsers (server-side keys only). // Source: https://labs.greatwork.company/sdk/labs.ts Docs: https://labs.greatwork.company/docs // // import { Labs } from "./labs"; // const labs = new Labs(process.env.GWL_API_KEY!); // const { output } = await labs.run("veil-1", { input: "Email dana@example.com", redact: "type" }); export interface Usage { units: number; unit: string; cost_usd: string; cost_micros: number } export interface RunResult { id: string; object: "run"; model: string; created: number; output: O; usage: Usage; latency_ms: number } export class LabsError extends Error { constructor(public status: number, public code: string, message: string, public body: any) { super(message); } } /** Language detection. $0.02 per 1,000 texts. https://labs.greatwork.company/models/lingo-1 */ export interface LingoRequest { input: string | string[]; top_k?: number; only?: string[]; } export interface LingoOutput { language: string; name: string; script: string; code: string; score: number; candidates: Record[]; } /** Sentiment analysis. $0.02 per 1,000 texts. https://labs.greatwork.company/models/tone-1 */ export interface ToneRequest { input: string | string[]; } export interface ToneOutput { label: string; score: number; polarity: number; labels: Record[]; } /** Emotion detection. $0.02 per 1,000 texts. https://labs.greatwork.company/models/mood-1 */ export interface MoodRequest { input: string | string[]; all?: boolean; } export interface MoodOutput { emotions: Record[]; top: string; basic: Record; scores?: Record; } /** Named entity recognition. $0.02 per 1,000 texts. https://labs.greatwork.company/models/spot-1 */ export interface SpotRequest { input: string | string[]; types?: ("person" | "organization" | "location" | "date")[]; threshold?: number; } export interface SpotOutput { spans: Record[]; counts: Record; } /** Zero-shot classification. $0.01 per 1,000 label checks. https://labs.greatwork.company/models/sort-1 */ export interface SortRequest { input: string | string[]; labels: string[]; multi_label?: boolean; template?: string; } export interface SortOutput { label: string; score: number; labels: Record[]; } /** Topic and format classification. $0.02 per 1,000 texts. https://labs.greatwork.company/models/topic-1 */ export interface TopicRequest { input: string | string[]; } export interface TopicOutput { topic: Record; format: Record; topics: Record[]; formats: Record[]; } /** Prompt task and complexity. $0.02 per 1,000 texts. https://labs.greatwork.company/models/route-1 */ export interface RouteRequest { input: string | string[]; } export interface RouteOutput { task: string; task_score: number; tasks: Record[]; complexity: number; dimensions: Record; } /** Toxicity detection. $0.02 per 1,000 texts. https://labs.greatwork.company/models/sieve-1 */ export interface SieveRequest { input: string | string[]; threshold?: number; } export interface SieveOutput { flagged: boolean; labels: string[]; scores?: Record; } /** Content safety policy check. $0.03 per 1,000 texts. https://labs.greatwork.company/models/warden-1 */ export interface WardenRequest { input: string | string[]; response?: string; threshold?: number; } export interface WardenOutput { flagged: boolean; unsafe: number; categories: string[]; scores?: Record; } /** Prompt injection detection. $0.02 per 1,000 texts. https://labs.greatwork.company/models/moat-1 */ export interface MoatRequest { input: string | string[]; threshold?: number; } export interface MoatOutput { injection: boolean; score: number; } /** PII detection and redaction. $0.02 per 1,000 texts. https://labs.greatwork.company/models/veil-1 */ export interface VeilRequest { input: string | string[]; redact?: "none" | "type" | "mask" | "numbered"; types?: string[]; threshold?: number; } export interface VeilOutput { redacted?: string; spans: Record[]; counts: Record; } /** Phishing, scam and spam detection. $0.04 per 1,000 texts. https://labs.greatwork.company/models/bait-1 */ export interface BaitRequest { input: string | string[]; threshold?: number; } export interface BaitOutput { label: string; unwanted: number; flagged: boolean; scores?: Record; } /** Grounding check (hallucination detection). $0.04 per 1,000 sentences. https://labs.greatwork.company/models/ground-1 */ export interface GroundRequest { input: string; context: string; threshold?: number; } export interface GroundOutput { supported: boolean; score: number; sentences: Record[]; } /** Text embeddings. $0.005 per 1,000 texts. https://labs.greatwork.company/models/atlas-1 */ export interface AtlasRequest { input: string | string[]; kind?: "passage" | "query"; } export interface AtlasOutput { embedding?: number[]; dims: number; } /** Reranking. $0.01 per 1,000 documents. https://labs.greatwork.company/models/ladder-1 */ export interface LadderRequest { input: string; documents: string[]; top_n?: number; return_documents?: boolean; } export interface LadderOutput { results: Record[]; } /** Keyphrase extraction. $0.03 per 1,000 texts. https://labs.greatwork.company/models/pluck-1 */ export interface PluckRequest { input: string | string[]; top_k?: number; diversity?: number; } export interface PluckOutput { keyphrases: Record[]; } /** Emoji suggestions. $0.01 per 1,000 texts. https://labs.greatwork.company/models/glyph-1 */ export interface GlyphRequest { input: string | string[]; top_k?: number; } export interface GlyphOutput { emojis: Record[]; } /** Punctuation and capitalization. $0.02 per 1,000 texts. https://labs.greatwork.company/models/comma-1 */ export interface CommaRequest { input: string | string[]; } export interface CommaOutput { text: string; changes: number; } /** Summarization. $0.30 per 1,000 summaries. https://labs.greatwork.company/models/brief-1 */ export interface BriefRequest { input: string | string[]; length?: "short" | "medium" | "long"; } export interface BriefOutput { summary: string; } /** Structured extraction (JSON from text). $0.40 per 1,000 texts. https://labs.greatwork.company/models/mold-1 */ export interface MoldRequest { input: string | string[]; schema: Record; instructions?: string; } export interface MoldOutput { data: Record; } /** Title and description writing. $0.20 per 1,000 texts. https://labs.greatwork.company/models/headline-1 */ export interface HeadlineRequest { input: string | string[]; style?: string; } export interface HeadlineOutput { title: string; description: string; slug: string; } /** Speech to text. $0.001 per minute. https://labs.greatwork.company/models/scribe-1 */ export interface ScribeRequest { input: string /* https URL or data URL */ | Blob; language?: string; task?: "transcribe" | "translate"; timestamps?: "word" | "none"; } export interface ScribeOutput { text: string; language: string; language_score?: number; duration: number; words?: Record[]; segments?: Record[]; } /** Image tagging and image embeddings. $0.10 per 1,000 images. https://labs.greatwork.company/models/prism-1 */ export interface PrismRequest { input: string /* https URL or data URL */ | Blob; labels?: string[]; template?: string; embed?: boolean; texts?: string[]; } export interface PrismOutput { label?: string; score?: number; labels?: Record[]; embedding?: number[]; text_embeddings?: number[]; } /** Text recognition (OCR). $0.50 per 1,000 images. https://labs.greatwork.company/models/ink-1 */ export interface InkRequest { input: string /* https URL or data URL */ | Blob; min_confidence?: number; } export interface InkOutput { text: string; lines: Record[]; } /** Object detection. $0.30 per 1,000 images. https://labs.greatwork.company/models/scout-1 */ export interface ScoutRequest { input: string /* https URL or data URL */ | Blob; threshold?: number; labels?: string[]; } export interface ScoutOutput { objects: Record[]; counts: Record; } /** Background removal. $2.00 per 1,000 images. https://labs.greatwork.company/models/cutout-1 */ export interface CutoutRequest { input: string /* https URL or data URL */ | Blob; format?: "png" | "mask"; } export interface CutoutOutput { image: string; foreground: number; width: number; height: number; } /** NSFW image detection. $0.10 per 1,000 images. https://labs.greatwork.company/models/curtain-1 */ export interface CurtainRequest { input: string /* https URL or data URL */ | Blob; threshold?: number; } export interface CurtainOutput { nsfw: boolean; score: number; } export interface Models { "lingo-1": [LingoRequest, LingoOutput]; "lingo": [LingoRequest, LingoOutput]; "tone-1": [ToneRequest, ToneOutput]; "tone": [ToneRequest, ToneOutput]; "mood-1": [MoodRequest, MoodOutput]; "mood": [MoodRequest, MoodOutput]; "spot-1": [SpotRequest, SpotOutput]; "spot": [SpotRequest, SpotOutput]; "sort-1": [SortRequest, SortOutput]; "sort": [SortRequest, SortOutput]; "topic-1": [TopicRequest, TopicOutput]; "topic": [TopicRequest, TopicOutput]; "route-1": [RouteRequest, RouteOutput]; "route": [RouteRequest, RouteOutput]; "sieve-1": [SieveRequest, SieveOutput]; "sieve": [SieveRequest, SieveOutput]; "warden-1": [WardenRequest, WardenOutput]; "warden": [WardenRequest, WardenOutput]; "moat-1": [MoatRequest, MoatOutput]; "moat": [MoatRequest, MoatOutput]; "veil-1": [VeilRequest, VeilOutput]; "veil": [VeilRequest, VeilOutput]; "bait-1": [BaitRequest, BaitOutput]; "bait": [BaitRequest, BaitOutput]; "ground-1": [GroundRequest, GroundOutput]; "ground": [GroundRequest, GroundOutput]; "atlas-1": [AtlasRequest, AtlasOutput]; "atlas": [AtlasRequest, AtlasOutput]; "ladder-1": [LadderRequest, LadderOutput]; "ladder": [LadderRequest, LadderOutput]; "pluck-1": [PluckRequest, PluckOutput]; "pluck": [PluckRequest, PluckOutput]; "glyph-1": [GlyphRequest, GlyphOutput]; "glyph": [GlyphRequest, GlyphOutput]; "comma-1": [CommaRequest, CommaOutput]; "comma": [CommaRequest, CommaOutput]; "brief-1": [BriefRequest, BriefOutput]; "brief": [BriefRequest, BriefOutput]; "mold-1": [MoldRequest, MoldOutput]; "mold": [MoldRequest, MoldOutput]; "headline-1": [HeadlineRequest, HeadlineOutput]; "headline": [HeadlineRequest, HeadlineOutput]; "scribe-1": [ScribeRequest, ScribeOutput]; "scribe": [ScribeRequest, ScribeOutput]; "prism-1": [PrismRequest, PrismOutput]; "prism": [PrismRequest, PrismOutput]; "ink-1": [InkRequest, InkOutput]; "ink": [InkRequest, InkOutput]; "scout-1": [ScoutRequest, ScoutOutput]; "scout": [ScoutRequest, ScoutOutput]; "cutout-1": [CutoutRequest, CutoutOutput]; "cutout": [CutoutRequest, CutoutOutput]; "curtain-1": [CurtainRequest, CurtainOutput]; "curtain": [CurtainRequest, CurtainOutput]; } type Out = R extends { input: any[] } ? Models[M][1][] : Models[M][1]; export class Labs { constructor(private key: string, private base = "https://labs.greatwork.company/v1", private opts: { retries?: number } = {}) { if (!key) throw new Error("Missing API key (GWL_API_KEY)"); } /** Run any model. Batched (array) inputs return an array of outputs in the same order. */ async run(model: M, request: R, init: { idempotencyKey?: string; signal?: AbortSignal } = {}): Promise>> { const file = typeof Blob !== "undefined" && request.input instanceof Blob ? request.input : null; let body: BodyInit; const headers: Record = { Authorization: `Bearer ${this.key}` }; if (file) { const form = new FormData(); form.set("model", model as string); form.set("input", file); for (const [k, v] of Object.entries(request)) if (k !== "input" && v !== undefined) form.set(k, typeof v === "string" ? v : JSON.stringify(v)); body = form; } else { headers["Content-Type"] = "application/json"; body = JSON.stringify({ model, ...request }); } if (init.idempotencyKey) headers["Idempotency-Key"] = init.idempotencyKey; for (let attempt = 0; ; attempt++) { const res = await fetch(`${this.base}/run`, { method: "POST", headers, body, signal: init.signal }); const data: any = await res.json().catch(() => ({})); if (res.ok) return data; const retryable = res.status === 429 || res.status === 503; if (retryable && attempt < (this.opts.retries ?? 3)) { await new Promise((r) => setTimeout(r, 500 * 2 ** attempt)); continue; } throw new LabsError(res.status, data?.error?.code ?? "http_" + res.status, data?.error?.message ?? res.statusText, data); } } /** Most a call can cost, without running it. */ async estimate(model: M, request: Models[M][0]) { const res = await fetch(`${this.base}/estimate`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ model, ...request }) }); return res.json() as Promise<{ units: number; max_cost_usd: string }>; } async account() { const res = await fetch(`${this.base}/account`, { headers: { Authorization: `Bearer ${this.key}` } }); return res.json(); } }