Ainative Svelte Sdk
/install ainative-svelte-sdk
@ainative/svelte-sdk
Svelte stores and utilities for AINative chat completions.
Install
npm install @ainative/svelte-sdk
Configure
// src/lib/ainative.ts
import { setAINativeConfig } from '@ainative/svelte-sdk';
setAINativeConfig({
apiKey: import.meta.env.VITE_AINATIVE_API_KEY,
baseUrl: 'https://api.ainative.studio',
});
Call this once in your app root (+layout.svelte or App.svelte).
createChatStore
\x3C!-- src/lib/Chat.svelte -->
\x3Cscript lang="ts">
import { createChatStore } from '@ainative/svelte-sdk';
const chat = createChatStore({
model: 'claude-3-5-sonnet-20241022',
});
let input = '';
async function send() {
if (!input.trim()) return;
const userMsg = { role: 'user' as const, content: input };
input = '';
await chat.sendMessage([...$chat.messages, userMsg]);
}
\x3C/script>
{#each $chat.messages as msg}
\x3Cdiv class="message {msg.role}">
\x3Cstrong>{msg.role}:\x3C/strong> {msg.content}
\x3C/div>
{/each}
{#if $chat.isLoading}
\x3Cp>Thinking...\x3C/p>
{/if}
{#if $chat.error}
\x3Cp class="error">Error: {$chat.error.message}\x3C/p>
{/if}
\x3Cinput bind:value={input} on:keydown={(e) => e.key === 'Enter' && send()} />
\x3Cbutton on:click={send} disabled={$chat.isLoading}>Send\x3C/button>
Store Shape
$chat is a reactive store with this shape:
| Field | Type | Description |
|---|---|---|
messages |
Message[] |
Full conversation history |
isLoading |
boolean |
True while request in flight |
error |
AINativeError | null |
Last error |
createChatStore Options
| Option | Type | Default | Description |
|---|---|---|---|
model |
string | — | Model ID |
initialMessages |
Message[] |
[] |
Seed conversation |
SvelteKit — Server Route
For server-side calls, use the raw API directly (no browser auth exposure):
// src/routes/api/chat/+server.ts
import { AINATIVE_API_KEY } from '$env/static/private';
import type { RequestHandler } from './$types';
export const POST: RequestHandler = async ({ request }) => {
const { messages } = await request.json();
const resp = await fetch('https://api.ainative.studio/v1/public/chat/completions', {
method: 'POST',
headers: {
'X-API-Key': AINATIVE_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'claude-3-5-sonnet-20241022',
messages,
}),
});
return new Response(resp.body, {
headers: { 'Content-Type': 'application/json' },
});
};
Environment Variables
# .env
VITE_AINATIVE_API_KEY=ak_your_key # Client-safe (public key only)
AINATIVE_API_KEY=ak_your_key # Server-side (SvelteKit $env/static/private)
Use server routes for production — never expose API keys in client bundles.
Exports
import {
createChatStore,
setAINativeConfig,
ainativeConfig,
type Message,
type ChatState,
type AINativeError,
} from '@ainative/svelte-sdk';
References
packages/sdks/svelte/src/stores/chat.ts— Chat store implementationpackages/sdks/svelte/src/stores/config.ts— Config storepackages/sdks/svelte/src/index.ts— Package exports
- 确保已安装 OpenClaw(本地或 Docker 部署)
- 在对话框中输入安装命令:
/install ainative-svelte-sdk - 安装完成后,直接呼叫该 Skill 的名称或使用
/ainative-svelte-sdk触发 - 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
Ainative Svelte Sdk 是什么?
Use @ainative/svelte-sdk to add AI chat to Svelte/SvelteKit apps. Use when (1) Installing @ainative/svelte-sdk, (2) Using Svelte stores for chat state, (3) C... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 118 次。
如何安装 Ainative Svelte Sdk?
在 OpenClaw 或 Claude Code 对话框中运行命令「/install ainative-svelte-sdk」即可一键安装,无需额外配置。
Ainative Svelte Sdk 是免费的吗?
是的,Ainative Svelte Sdk 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。
Ainative Svelte Sdk 支持哪些平台?
Ainative Svelte Sdk 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。
谁开发了 Ainative Svelte Sdk?
由 Toby Morning(@urbantech)开发并维护,当前版本 v1.0.0。