docs: update portal for WRNexusJS 0.8.0

This commit is contained in:
2026-08-02 23:57:41 +05:30
parent 9366895f78
commit 8d5d4b8f12
183 changed files with 14433 additions and 2492 deletions
+21 -110
View File
@@ -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 &#123;
/* back off */
&#125;
&#125;</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 &#123; anthropicProvider, createAIClient &#125; from &quot;@wrnexus/ai&quot;;
const ai = createAIClient(&#123;
providers: [anthropicProvider()],
retry: &#123; attempts: 3, baseDelayMs: 100, maxDelayMs: 2_000 &#125;,
circuitBreaker: &#123; failureThreshold: 5, resetAfterMs: 30_000 &#125;,
&#125;);
const result = await ai.generateObject&lt;&#123; title: string &#125;&gt;(&quot;Return a JSON title&quot;, &#123;
validate: (value): value is &#123; title: string &#125; =&gt;
typeof value === &quot;object&quot; &amp;&amp; value !== null &amp;&amp; &quot;title&quot; in value,
&#125;);</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) =&gt; &#123;
<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>&#123; model &#125;</code> for a different model (e.g.</li>
<p><code>&quot;claude-sonnet-5&quot;</code> for speed/cost, <code>&quot;claude-haiku-4-5&quot;</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 &#123;
inputTokens?: number;
outputTokens?: number;
totalTokens?: number;
&#125;
interface AIResult&lt;T = string&gt; &#123;
value: T;
provider: string;
model?: string;
usage?: AIUsage;
finishReason?: string;
raw?: unknown;
&#125;
interface AIProvider &#123;
name: string;
generate(prompt: string | Message[], options?: GenerateOptions): Promise&lt;AIResult&lt;string&gt;&gt;;
stream?(prompt: string | Message[], options?: GenerateOptions): AsyncGenerator&lt;string, void, unknown&gt;;
&#125;
interface AIClientOptions &#123;
providers: AIProvider[];
fallback?: boolean;
onAttempt?: (provider: string, error?: unknown) =&gt; void | Promise&lt;void&gt;;
&#125;
interface AIClient &#123;
generate(prompt: string | Message[], options?: GenerateOptions &amp; &#123;
provider?: string;
&#125;): Promise&lt;AIResult&lt;string&gt;&gt;;
generateObject&lt;T&gt;(prompt: string | Message[], options?: GenerateOptions &amp; &#123;
provider?: string;
validate?: (value: unknown) =&gt; value is T;
&#125;): Promise&lt;AIResult&lt;T&gt;&gt;;
stream(prompt: string | Message[], options?: GenerateOptions &amp; &#123;
provider?: string;
&#125;): AsyncGenerator&lt;string, void, unknown&gt;;
&#125;
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 &#123; createAI &#125; from &quot;@wrnexus/ai&quot;;
* const ai = createAI(); // reads ANTHROPIC_API_KEY
* const text = await ai.generate(&quot;Write a haiku about Bun.&quot;);
*
* Streaming (great for API routes):
* export const POST = async (ctx) =&gt; ai.streamResponse(await ctx.req.text());
*/
type Role = &quot;user&quot; | &quot;assistant&quot;;
interface Message &#123;
role: Role;
content: string;
&#125;
/** Reasoning effort — higher means deeper thinking + more tokens. */
type Effort = &quot;low&quot; | &quot;medium&quot; | &quot;high&quot; | &quot;xhigh&quot; | &quot;max&quot;;
interface AIConfig &#123;
/** 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;
&#125;
interface GenerateOptions &#123;
/** 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;
&#125;
/** Thrown when the API returns a non-2xx response or refuses the request. */
declare class AIError extends Error &#123;
readonly status: number;
readonly type: string;
constructor(message: string, status?: number, type?: string);
&#125;
interface AI &#123;
/** Generate a full text response (non-streaming). */
generate(prompt: string | Message[], opts?: GenerateOptions): Promise&lt;string&gt;;
/** Stream the response as text deltas, as they arrive. */
stream(prompt: string | Message[], opts?: GenerateOptions): AsyncGenerator&lt;string, void, unknown&gt;;
/** Stream straight to a `Response` (text/plain) — drop-in for an API route return. */
streamResponse(prompt: string | Message[], opts?: GenerateOptions): Response;
&#125;
/** Create a Claude client. Reads `ANTHROPIC_API_KEY` from the environment by default. */
declare function createAI(config?: AIConfig): AI;
export &#123; 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 &#125;;
</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 &#123; 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 &#125; 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 &#123; createAI &#125; from &quot;@wrnexus/ai&quot;;
const ai = createAI();
@@ -238,9 +149,9 @@ export const POST = async (ctx) =&gt; &#123;
maxTokens: 1_500,
&#125;);
&#125;;</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&lt;string&gt;</a><a class="toc-level-4" href="#ai-stream-prompt-opts-asyncgenerator-string">ai.stream(prompt, opts?): AsyncGenerator&lt;string&gt;</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&lt;string&gt;</a><a class="toc-level-4" href="#ai-stream-prompt-opts-asyncgenerator-string">ai.stream(prompt, opts?): AsyncGenerator&lt;string&gt;</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>
}