docs: update portal for WRNexusJS 0.8.0
This commit is contained in:
+21
-110
@@ -10,11 +10,12 @@ page wrnexusai {
|
||||
<header class="topbar">
|
||||
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
|
||||
<nav aria-label="Primary"><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="https://component.wrnexusjs.dev/">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
|
||||
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.7.0</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
|
||||
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.8.0</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
|
||||
</header>
|
||||
<div class="mobile-doc-nav"><details><summary>Browse documentation</summary><nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="https://component.wrnexusjs.dev/">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a><a href="/tutorial">Tutorial</a><a href="/guides/project-structure">Guides</a><a href="/examples">Examples</a><a href="/search">Search</a></nav></details></div>
|
||||
<main class="portal-main docs-layout">
|
||||
<article id="main" class="documentation prose standalone package-document"><nav class="breadcrumbs" aria-label="Breadcrumb"><a href="/">Home</a><span>/</span><a href="/packages">Packages</a><span>/</span><span aria-current="page">@wrnexus/ai</span></nav><section class="doc-intro"><span class="eyebrow">AI · Package reference</span><h1>@wrnexus/ai</h1><p>Server-side Anthropic client with generation and streaming.</p><div class="doc-meta"><span>v0.7.0</span><span>Private registry</span><span>AI</span></div><section id="access" class="access-callout"><h2>Install the package</h2><p>After WorkRoot approves private registry access, install the release-aligned package:</p><pre><code>bun add @wrnexus/ai@0.7.0</code><button type="button" class="copy-button" aria-label="Copy installation command">Copy</button></pre><p><a href="/access">Request preview access</a>. Never put registry tokens in source control.</p></section></section><section id="guide"><blockquote>A tiny, zero-dependency Claude (Anthropic) client for WRNexusJS apps — generate and stream text with Claude from any server-side code.</blockquote>
|
||||
<article id="main" class="documentation prose standalone package-document"><nav class="breadcrumbs" aria-label="Breadcrumb"><a href="/">Home</a><span>/</span><a href="/packages">Packages</a><span>/</span><span aria-current="page">@wrnexus/ai</span></nav><section class="doc-intro"><span class="eyebrow">AI · Package reference</span><h1>@wrnexus/ai</h1><p>Server-side Anthropic client with generation and streaming.</p><div class="doc-meta"><span>v0.8.0</span><span>Private registry</span><span>AI</span></div><section id="access" class="access-callout"><h2>Install the package</h2><p>After WorkRoot approves private registry access, install the release-aligned package:</p><pre><code>bun add @wrnexus/ai@0.8.0</code><button type="button" class="copy-button" aria-label="Copy installation command">Copy</button></pre><p><a href="/access">Request preview access</a>. Never put registry tokens in source control.</p></section></section><section id="guide"><p>Provider-neutral AI orchestration for OpenAI, Anthropic, Google and local OpenAI-compatible models, with streaming, structured output, tools, embeddings, vector search/RAG, conversation persistence, templates, guardrails, usage events, fallback, rate limits and evaluation reports.</p>
|
||||
<blockquote>A tiny, zero-dependency Claude (Anthropic) client for WRNexusJS apps — generate and stream text with Claude from any server-side code.</blockquote>
|
||||
<p>Part of the <strong>WRNexusJS</strong> framework — an SSR-first, Bun-native full-stack web framework.</p>
|
||||
<h3 id="overview">Overview</h3>
|
||||
<p><code>@wrnexus/ai</code> is a thin, dependency-free wrapper over the Anthropic <strong>Messages API</strong>, built on <code>fetch</code> (Bun-native, no SDK). Use it in API routes, jobs, or middleware to call Claude. It defaults to the most capable model, <strong><code>claude-opus-4-8</code></strong>, reads your key from <code>ANTHROPIC_API_KEY</code>, and supports both one-shot generation and streaming.</p>
|
||||
@@ -75,6 +76,21 @@ try {
|
||||
/* back off */
|
||||
}
|
||||
}</code></pre>
|
||||
<h4 id="multi-provider-client">Multi-provider client</h4>
|
||||
<p><code>createAIClient</code> adds named-provider selection and fallback, capability discovery, validated JSON output, validated tool execution, abort-aware exponential retries, and per-provider circuit breakers. Attempt events intentionally contain metadata only: prompts, credentials, and raw model responses are never passed to telemetry.</p>
|
||||
<pre data-language="ts"><code>import { anthropicProvider, createAIClient } from "@wrnexus/ai";
|
||||
|
||||
const ai = createAIClient({
|
||||
providers: [anthropicProvider()],
|
||||
retry: { attempts: 3, baseDelayMs: 100, maxDelayMs: 2_000 },
|
||||
circuitBreaker: { failureThreshold: 5, resetAfterMs: 30_000 },
|
||||
});
|
||||
|
||||
const result = await ai.generateObject<{ title: string }>("Return a JSON title", {
|
||||
validate: (value): value is { title: string } =>
|
||||
typeof value === "object" && value !== null && "title" in value,
|
||||
});</code></pre>
|
||||
<p>Providers can return normalized <code>usage</code> (<code>inputTokens</code>, <code>outputTokens</code>, <code>totalTokens</code>, and <code>costUsd</code>) and <code>toolCalls</code>. Use <code>executeTools</code> with a named, validated tool registry; unknown tools and invalid arguments are rejected before application code runs. <code>deterministicAIProvider</code> supplies ordered or computed offline responses for tests and examples without API keys or network calls.</p>
|
||||
<h3 id="usage">Usage</h3>
|
||||
<h4 id="return-generated-json-from-an-api-route">Return generated JSON from an API route</h4>
|
||||
<pre data-language="ts"><code>// app/api/summarize.ts — summarize posted text
|
||||
@@ -109,112 +125,7 @@ export const POST = async (ctx) => {
|
||||
<li><strong>Zero dependencies</strong> — no <code>@anthropic-ai/sdk</code>; talks to the Messages API directly.</li>
|
||||
<li>Defaults to <code>claude-opus-4-8</code>. Pass <code>{ model }</code> for a different model (e.g.</li>
|
||||
<p><code>"claude-sonnet-5"</code> for speed/cost, <code>"claude-haiku-4-5"</code> for the fastest).</p>
|
||||
</ul></section><section id="api" class="api"><h2>Complete TypeScript API</h2><p>Generated from the exact installed package declarations.</p><pre data-language="typescript"><code>interface AIUsage {
|
||||
inputTokens?: number;
|
||||
outputTokens?: number;
|
||||
totalTokens?: number;
|
||||
}
|
||||
interface AIResult<T = string> {
|
||||
value: T;
|
||||
provider: string;
|
||||
model?: string;
|
||||
usage?: AIUsage;
|
||||
finishReason?: string;
|
||||
raw?: unknown;
|
||||
}
|
||||
interface AIProvider {
|
||||
name: string;
|
||||
generate(prompt: string | Message[], options?: GenerateOptions): Promise<AIResult<string>>;
|
||||
stream?(prompt: string | Message[], options?: GenerateOptions): AsyncGenerator<string, void, unknown>;
|
||||
}
|
||||
interface AIClientOptions {
|
||||
providers: AIProvider[];
|
||||
fallback?: boolean;
|
||||
onAttempt?: (provider: string, error?: unknown) => void | Promise<void>;
|
||||
}
|
||||
interface AIClient {
|
||||
generate(prompt: string | Message[], options?: GenerateOptions & {
|
||||
provider?: string;
|
||||
}): Promise<AIResult<string>>;
|
||||
generateObject<T>(prompt: string | Message[], options?: GenerateOptions & {
|
||||
provider?: string;
|
||||
validate?: (value: unknown) => value is T;
|
||||
}): Promise<AIResult<T>>;
|
||||
stream(prompt: string | Message[], options?: GenerateOptions & {
|
||||
provider?: string;
|
||||
}): AsyncGenerator<string, void, unknown>;
|
||||
}
|
||||
declare function anthropicProvider(config?: AIConfig): AIProvider;
|
||||
declare function aiProvider(name: string, client: AI): AIProvider;
|
||||
declare function createAIClient(options: AIClientOptions): AIClient;
|
||||
|
||||
/**
|
||||
* @wrnexus/ai — a tiny, zero-dependency Claude (Anthropic) client for WRNexusJS apps.
|
||||
*
|
||||
* Use it in API routes, jobs, or anywhere server-side to generate text with Claude.
|
||||
* It talks to the Anthropic Messages API over `fetch` (no SDK dependency, Bun-native),
|
||||
* and defaults to the most capable model, `claude-opus-4-8`.
|
||||
*
|
||||
* import { createAI } from "@wrnexus/ai";
|
||||
* const ai = createAI(); // reads ANTHROPIC_API_KEY
|
||||
* const text = await ai.generate("Write a haiku about Bun.");
|
||||
*
|
||||
* Streaming (great for API routes):
|
||||
* export const POST = async (ctx) => ai.streamResponse(await ctx.req.text());
|
||||
*/
|
||||
type Role = "user" | "assistant";
|
||||
interface Message {
|
||||
role: Role;
|
||||
content: string;
|
||||
}
|
||||
/** Reasoning effort — higher means deeper thinking + more tokens. */
|
||||
type Effort = "low" | "medium" | "high" | "xhigh" | "max";
|
||||
interface AIConfig {
|
||||
/** Anthropic API key. Default: `ANTHROPIC_API_KEY` from the environment. */
|
||||
apiKey?: string;
|
||||
/** Model id. Default: `claude-opus-4-8` (the most capable Claude model). */
|
||||
model?: string;
|
||||
/** Default max output tokens. Default: 4096. */
|
||||
maxTokens?: number;
|
||||
/** API base URL. Default: `https://api.anthropic.com`. */
|
||||
baseURL?: string;
|
||||
/** `anthropic-version` header. Default: `2023-06-01`. */
|
||||
version?: string;
|
||||
}
|
||||
interface GenerateOptions {
|
||||
/** System prompt — sets the assistant's role/behavior. */
|
||||
system?: string;
|
||||
/** Override the model for this call. */
|
||||
model?: string;
|
||||
/** Override max output tokens for this call. */
|
||||
maxTokens?: number;
|
||||
/** Enable adaptive extended thinking (slower, deeper reasoning). */
|
||||
thinking?: boolean;
|
||||
/** Reasoning effort / token spend (`output_config.effort`). */
|
||||
effort?: Effort;
|
||||
/** Full message history — supersedes the `prompt` argument when provided. */
|
||||
messages?: Message[];
|
||||
/** Abort the request. */
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
/** Thrown when the API returns a non-2xx response or refuses the request. */
|
||||
declare class AIError extends Error {
|
||||
readonly status: number;
|
||||
readonly type: string;
|
||||
constructor(message: string, status?: number, type?: string);
|
||||
}
|
||||
interface AI {
|
||||
/** Generate a full text response (non-streaming). */
|
||||
generate(prompt: string | Message[], opts?: GenerateOptions): Promise<string>;
|
||||
/** Stream the response as text deltas, as they arrive. */
|
||||
stream(prompt: string | Message[], opts?: GenerateOptions): AsyncGenerator<string, void, unknown>;
|
||||
/** Stream straight to a `Response` (text/plain) — drop-in for an API route return. */
|
||||
streamResponse(prompt: string | Message[], opts?: GenerateOptions): Response;
|
||||
}
|
||||
/** Create a Claude client. Reads `ANTHROPIC_API_KEY` from the environment by default. */
|
||||
declare function createAI(config?: AIConfig): AI;
|
||||
|
||||
export { type AI, type AIClient, type AIClientOptions, type AIConfig, AIError, type AIProvider, type AIResult, type AIUsage, type Effort, type GenerateOptions, type Message, type Role, aiProvider, anthropicProvider, createAI, createAIClient };
|
||||
</ul></section><section id="api" class="api"><h2>Complete TypeScript API</h2><p>Generated from the exact installed package declarations.</p><pre data-language="typescript"><code>export { A as AI, a as AIAttemptEvent, b as AICircuitBreakerOptions, c as AIClient, d as AIClientOptions, e as AIConfig, f as AIError, AIGuardrail, g as AIProvider, h as AIProviderCapabilities, i as AIResult, j as AIRetryOptions, k as AITool, l as AIToolCall, m as AIUsage, ConversationStore, D as DeterministicAIProviderOptions, E as Effort, EmbeddingProvider, G as GenerateOptions, HttpAIProviderOptions, M as Message, R as Role, VectorMatch, VectorRecord, VectorStore, n as aiProvider, aiRateLimiter, o as anthropicProvider, p as createAI, q as createAIClient, createRagPipeline, r as deterministicAIProvider, evaluateAI, googleAIProvider, guardedProvider, localAIProvider, maxPromptLength, memoryConversationStore, memoryVectorStore, openAIEmbeddings, openAIProvider, promptTemplate } from './platform.js';
|
||||
</code></pre></section><section id="examples" class="examples"><h2>Examples</h2><p>Copy-ready examples from the installed package documentation.</p><div class="example-grid"><article class="example-card"><h3>Return generated JSON from an API route</h3><pre data-language="ts"><code>// app/api/summarize.ts — summarize posted text
|
||||
import { createAI } from "@wrnexus/ai";
|
||||
const ai = createAI();
|
||||
@@ -238,9 +149,9 @@ export const POST = async (ctx) => {
|
||||
maxTokens: 1_500,
|
||||
});
|
||||
};</code></pre></article></div></section></article>
|
||||
<aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#createai-config">createAI(config?)</a><a class="toc-level-4" href="#ai-generate-prompt-opts-promise-string">ai.generate(prompt, opts?): Promise<string></a><a class="toc-level-4" href="#ai-stream-prompt-opts-asyncgenerator-string">ai.stream(prompt, opts?): AsyncGenerator<string></a><a class="toc-level-4" href="#ai-streamresponse-prompt-opts-response">ai.streamResponse(prompt, opts?): Response</a><a class="toc-level-4" href="#generateoptions">GenerateOptions</a><a class="toc-level-4" href="#aierror">AIError</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-4" href="#return-generated-json-from-an-api-route">Return generated JSON from an API route</a><a class="toc-level-4" href="#stream-a-chat-response-to-the-browser">Stream a chat response to the browser</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
|
||||
<aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#createai-config">createAI(config?)</a><a class="toc-level-4" href="#ai-generate-prompt-opts-promise-string">ai.generate(prompt, opts?): Promise<string></a><a class="toc-level-4" href="#ai-stream-prompt-opts-asyncgenerator-string">ai.stream(prompt, opts?): AsyncGenerator<string></a><a class="toc-level-4" href="#ai-streamresponse-prompt-opts-response">ai.streamResponse(prompt, opts?): Response</a><a class="toc-level-4" href="#generateoptions">GenerateOptions</a><a class="toc-level-4" href="#aierror">AIError</a><a class="toc-level-4" href="#multi-provider-client">Multi-provider client</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-4" href="#return-generated-json-from-an-api-route">Return generated JSON from an API route</a><a class="toc-level-4" href="#stream-a-chat-response-to-the-browser">Stream a chat response to the browser</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
|
||||
</main>
|
||||
<footer><div class="footer-brand"><span class="footer-mark" aria-hidden="true">W</span><p><strong>WRNexusJS 0.7.0</strong><span>Complete API documentation generated from installed package declarations.</span></p></div><nav aria-label="Footer"><a href="/packages">All packages</a><a href="/getting-started">Get started</a><a href="/security">Security</a><a href="/support">Support</a><a href="/llms.txt">AI guide</a></nav><p class="footer-meta">Private Developer Preview · Bun-native</p></footer>
|
||||
<footer><div class="footer-brand"><span class="footer-mark" aria-hidden="true">W</span><p><strong>WRNexusJS 0.8.0</strong><span>Complete API documentation generated from installed package declarations.</span></p></div><nav aria-label="Footer"><a href="/packages">All packages</a><a href="/getting-started">Get started</a><a href="/security">Security</a><a href="/support">Support</a><a href="/llms.txt">AI guide</a></nav><p class="footer-meta">Private Developer Preview · Bun-native</p></footer>
|
||||
<BackToTop />
|
||||
</div>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user