umpirix/examples/official-sdk.mjs
caelum0x 26becee66a feat(api): official TypeSafe SDKs work unchanged against OpenJev
Found by running typesafe-sdk 0.7.5 and @typesafe-ai/sdk 0.7.0 against a
local server and fixed:
- zstd (and gzip/deflate/br) request bodies are decoded; the SDKs compress
  bodies of 1 KiB or more, which used to fail with a 500
- GET /v1/models also returns models: [{ name, description, release_date }]
- every systemone, batch and models response carries x-typesafe-request-id
- a base URL ending in /v1 (or /api/v1) resolves to the same handlers
- request errors carry a detail list; a non-JSON body is a 400, not a 500
- multipart (image asset) requests get a clear 415

Adds examples/official-sdk.py and .mjs, a /docs/typesafe-sdk page, a README
block and a COMPARISON.md section, limited to what was observed to pass.
2026-11-11 05:41:06 +03:00

86 lines
3.4 KiB
JavaScript

#!/usr/bin/env node
// openjev example: the official TypeSafe JavaScript SDK, pointed at OpenJev.
//
// Nothing here imports OpenJev code. It is the unmodified `@typesafe-ai/sdk`
// package from npm with its base URL set to a local OpenJev server. Request
// bodies of 1 KiB or more are sent zstd-compressed by the SDK; the last call
// below sends one.
//
// npm install @typesafe-ai/sdk
// npm run dev # or: docker compose up
// TYPESAFE_BASE_URL=http://localhost:8080 node examples/official-sdk.mjs
//
// The SDK insists on an API key, so any placeholder value works against a
// self-hosted server that does not issue keys.
import { BadRequestError, choice, noul, score, TypeSafeClient } from "@typesafe-ai/sdk";
const baseURL = process.env.TYPESAFE_BASE_URL ?? "http://localhost:8080";
const apiKey = process.env.TYPESAFE_API_KEY ?? "not-needed-for-self-host";
const TICKET = "I was charged twice for order A-104, please refund the duplicate.";
const client = new TypeSafeClient({ baseURL, apiKey });
console.log(`Server: ${baseURL}`);
const picked = await client.systemOne({
state: TICKET,
questions: {
department: choice("Which team should handle this?", {
billing: "Payments, refunds, invoices.",
technical: "Bugs, crashes, outages.",
sales: "Pricing, plans, upgrades.",
}),
},
});
const { department } = picked.answers;
console.log(`choice ${department.choice} confidence=${department.confidence}`, department.probabilities);
const rated = await client.systemOne({
state: TICKET,
questions: { urgency: score("How urgent is this ticket?", ["low", "medium", "high", "critical"]) },
});
console.log(`score ${rated.answers.urgency.score}`, rated.answers.urgency.probabilities);
const flagged = await client.systemOne({
state: TICKET,
questions: { isRefund: noul("Is the customer asking for a refund?") },
});
console.log(`noul ${flagged.answers.isRefund.noul}`);
// Several typed questions over a structured state, answered in one request.
const { data: multi, requestId } = await client
.systemOne({
state: { ticket: TICKET, order: { id: "A-104", amount: 49 } },
questions: {
department: choice("Which team should handle this?", { billing: null, technical: null, sales: null }),
urgency: score("How urgent is this ticket?", ["low", "medium", "high"]),
isRefund: noul("Is the customer asking for a refund?"),
},
})
.withResponse();
console.log(
`multi department=${multi.answers.department.choice} urgency=${multi.answers.urgency.score} ` +
`isRefund=${multi.answers.isRefund.noul} model=${multi.model} requestId=${requestId}`,
);
const models = await client.models.list();
console.log(`models ${models.map((model) => model.name).join(", ")}`);
// A request the server rejects: a Choice needs at least two options.
try {
await client.systemOne({
state: TICKET,
questions: { department: choice("Which team?", { billing: null }) },
});
} catch (error) {
if (!(error instanceof BadRequestError)) throw error;
console.log(`invalid ${error.status} ${error.body.error.message} requestId=${error.requestId}`);
}
// A state over 1 KiB, which the SDK compresses with zstd before sending.
const long = await client.systemOne({
state: Array.from({ length: 40 }, (_, i) => `Message ${i + 1}: ${TICKET}`).join("\n"),
questions: { isRefund: noul("Is the customer asking for a refund?") },
});
console.log(`zstd isRefund=${long.answers.isRefund.noul}`);