docs: publish package usage examples for 0.2.24

This commit is contained in:
2026-07-13 20:58:28 +05:30
parent b07ef5058a
commit 379553bdf7
79 changed files with 1542 additions and 894 deletions
+41 -15
View File
@@ -10,12 +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="/language">Language</a><a href="/architecture">Architecture</a></nav>
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.2.23</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.2.24</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="/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="page package-page">
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">AI</span><h2>@wrnexus/ai</h2><p>Server-side Anthropic client with generation and streaming.</p><span class="status status-beta">Private preview · 0.2.23</span><nav><a href="#access">Access</a><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
<article id="main" class="documentation"><section class="doc-intro"><span class="eyebrow">AI · Preview</span><h1>@wrnexus/ai</h1><p>Server-side Anthropic client with generation and streaming.</p><section id="access" class="access-callout"><h2>Private registry access required</h2><p>This package is not available from the public npm registry. After WorkRoot approves access and supplies private registry instructions, install the release-aligned package:</p><pre><code>bun add @wrnexus/ai@0.2.23</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" class="prose"><blockquote>A tiny, zero-dependency Claude (Anthropic) client for WRNexusJS apps — generate and stream text with Claude from any server-side code.</blockquote>
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">AI</span><h2>@wrnexus/ai</h2><p>Server-side Anthropic client with generation and streaming.</p><span class="status status-beta">Private preview · 0.2.24</span><nav><a href="#access">Access</a><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
<article id="main" class="documentation"><section class="doc-intro"><span class="eyebrow">AI · Preview</span><h1>@wrnexus/ai</h1><p>Server-side Anthropic client with generation and streaming.</p><section id="access" class="access-callout"><h2>Private registry access required</h2><p>This package is not available from the public npm registry. After WorkRoot approves access and supplies private registry instructions, install the release-aligned package:</p><pre><code>bun add @wrnexus/ai@0.2.24</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" class="prose"><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>
@@ -77,6 +77,7 @@ try &#123;
&#125;
&#125;</code></pre>
<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
import &#123; createAI &#125; from &quot;@wrnexus/ai&quot;;
const ai = createAI();
@@ -89,6 +90,19 @@ export const POST = async (ctx) =&gt; &#123;
&#125;);
return Response.json(&#123; summary &#125;);
&#125;;</code></pre>
<h4 id="stream-a-chat-response-to-the-browser">Stream a chat response to the browser</h4>
<pre data-language="ts"><code>// app/api/chat.ts
import &#123; createAI &#125; from &quot;@wrnexus/ai&quot;;
const ai = createAI(&#123; model: &quot;claude-sonnet-5&quot; &#125;);
export const POST = async (ctx) =&gt; &#123;
const &#123; messages &#125; = await ctx.req.json();
return ai.streamResponse(messages, &#123;
system: &quot;Answer using concise Markdown.&quot;,
maxTokens: 1_500,
&#125;);
&#125;;</code></pre>
<h3 id="requirements-notes">Requirements / Notes</h3>
<ul>
<li><strong>Bun-only.</strong> Uses <code>fetch</code>, <code>ReadableStream</code>, <code>TextDecoder</code>/<code>TextEncoder</code>, and</li>
@@ -163,20 +177,32 @@ interface AI &#123;
declare function createAI(config?: AIConfig): AI;
export &#123; type AI, type AIConfig, AIError, type Effort, type GenerateOptions, type Message, type Role, createAI &#125;;
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Examples are taken from this package's installed documentation and must be evaluated with its requirements and stability notes.</p><div class="example-grid"><article class="example-card"><h3>Example 1</h3><pre data-language="bash"><code>bun add @wrnexus/ai</code></pre></article><article class="example-card"><h3>Example 2</h3><pre data-language="text"><code>ANTHROPIC_API_KEY=sk-ant-...</code></pre></article><article class="example-card"><h3>Example 3</h3><pre data-language="ts"><code>import &#123; createAI &#125; from &quot;@wrnexus/ai&quot;;
const ai = createAI(); // or createAI(&#123; apiKey, model, maxTokens, baseURL, version &#125;)</code></pre></article><article class="example-card"><h3>Example 4</h3><pre data-language="ts"><code>const text = await ai.generate(&quot;Write a haiku about Bun.&quot;);
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Examples are taken from this package's installed documentation and must be evaluated with its requirements and stability notes.</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();
const reply = await ai.generate(
[
&#123; role: &quot;user&quot;, content: &quot;My name is Ada.&quot; &#125;,
&#123; role: &quot;assistant&quot;, content: &quot;Hi Ada!&quot; &#125;,
&#123; role: &quot;user&quot;, content: &quot;What's my name?&quot; &#125;,
],
&#123; system: &quot;You are concise.&quot; &#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-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>
export const POST = async (ctx) =&gt; &#123;
const &#123; text &#125; = await ctx.req.json().catch(() =&gt; (&#123;&#125;));
if (!text) return Response.json(&#123; error: &quot;Provide 'text'.&quot; &#125;, &#123; status: 400 &#125;);
const summary = await ai.generate(`Summarize in one sentence:\n\n$&#123;text&#125;`, &#123;
system: &quot;You are a precise summarizer.&quot;,
&#125;);
return Response.json(&#123; summary &#125;);
&#125;;</code></pre></article><article class="example-card"><h3>Stream a chat response to the browser</h3><pre data-language="ts"><code>// app/api/chat.ts
import &#123; createAI &#125; from &quot;@wrnexus/ai&quot;;
const ai = createAI(&#123; model: &quot;claude-sonnet-5&quot; &#125;);
export const POST = async (ctx) =&gt; &#123;
const &#123; messages &#125; = await ctx.req.json();
return ai.streamResponse(messages, &#123;
system: &quot;Answer using concise Markdown.&quot;,
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>
</main>
<footer>WRNexusJS 0.2.23 · Private Developer Preview · Bun-native · Documentation generated from installed package APIs.</footer>
<footer>WRNexusJS 0.2.24 · Private Developer Preview · Bun-native · Documentation generated from installed package APIs.</footer>
</div>
}
}