返回顶部
a

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) Configuring AINative in a Svelte app, (4) Displaying chat messages reactively, (5) Handling loading and error states with Svelte stores. Published npm package v1.0.0.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
72
下载量
0
收藏
概述
安装方式
版本历史

ainative-svelte-sdk

# @ainative/svelte-sdk Svelte stores and utilities for AINative chat completions. ## Install ```bash npm install @ainative/svelte-sdk ``` ## Configure ```typescript // 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 ```svelte <!-- src/lib/Chat.svelte --> <script 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]); } </script> {#each $chat.messages as msg} <div class="message {msg.role}"> <strong>{msg.role}:</strong> {msg.content} </div> {/each} {#if $chat.isLoading} <p>Thinking...</p> {/if} {#if $chat.error} <p class="error">Error: {$chat.error.message}</p> {/if} <input bind:value={input} on:keydown={(e) => e.key === 'Enter' && send()} /> <button on:click={send} disabled={$chat.isLoading}>Send</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): ```typescript // 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 ```bash # .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 ```typescript import { createChatStore, setAINativeConfig, ainativeConfig, type Message, type ChatState, type AINativeError, } from '@ainative/svelte-sdk'; ``` ## References - `packages/sdks/svelte/src/stores/chat.ts` — Chat store implementation - `packages/sdks/svelte/src/stores/config.ts` — Config store - `packages/sdks/svelte/src/index.ts` — Package exports

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 ainative-svelte-sdk-1776060301 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 ainative-svelte-sdk-1776060301 技能

通过命令行安装

skillhub install ainative-svelte-sdk-1776060301

下载 Zip 包

⬇ 下载 ainative-svelte-sdk v1.0.0

文件大小: 2.22 KB | 发布时间: 2026-4-14 10:42

v1.0.0 最新 2026-4-14 10:42
ainative-svelte-sdk v1.0.0 initial release

- Adds Svelte store utilities for integrating AINative chat into Svelte/SvelteKit apps.
- Provides createChatStore for reactive chat state, handling messages, loading, and errors.
- Includes setAINativeConfig for global API setup.
- Documents recommended setup for both client and secure server-side usage.
- Type exports for Message, ChatState, and AINativeError.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部