返回顶部
a

ainative-react-sdk

Use @ainative/react-sdk to add AI chat and credits to React apps. Use when (1) Installing @ainative/react-sdk, (2) Using the useChat hook for chat completions, (3) Displaying credit balance with useCredits, (4) Setting up AINativeProvider, (5) Handling loading/error states in chat UI. Published npm package v1.0.1.

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

ainative-react-sdk

# @ainative/react-sdk React hooks and components for AINative — chat completions, credit tracking, and managed sessions. ## Install ```bash npm install @ainative/react-sdk ``` ## Setup: AINativeProvider Wrap your app (or a subtree) with the provider: ```tsx import { AINativeProvider } from '@ainative/react-sdk'; function App() { return ( <AINativeProvider config={{ apiKey: 'ak_your_key' }}> <YourApp /> </AINativeProvider> ); } ``` ## useChat Hook ```tsx import { useChat } from '@ainative/react-sdk'; import type { Message } from '@ainative/react-sdk'; function ChatUI() { const { messages, isLoading, error, sendMessage } = useChat({ model: 'claude-3-5-sonnet-20241022', temperature: 0.7, max_tokens: 1024, }); const handleSend = async (input: string) => { await sendMessage([ ...messages, { role: 'user', content: input } ]); }; return ( <div> {messages.map((msg, i) => ( <div key={i} className={`message ${msg.role}`}> <strong>{msg.role}:</strong> {msg.content} </div> ))} {isLoading && <div>Thinking...</div>} {error && <div className="error">Error: {error.message}</div>} <input onKeyDown={(e) => e.key === 'Enter' && handleSend(e.currentTarget.value)} placeholder="Type a message..." /> </div> ); } ``` ### useChat Options | Option | Type | Default | Description | |--------|------|---------|-------------| | `model` | string | — | Model ID (e.g. `claude-3-5-sonnet-20241022`) | | `temperature` | number | 0.7 | Randomness (0–1) | | `max_tokens` | number | 1024 | Max response tokens | ### useChat Return | Field | Type | Description | |-------|------|-------------| | `messages` | `Message[]` | Full conversation history | | `isLoading` | `boolean` | True while request in flight | | `error` | `AINativeError \| null` | Last error, if any | | `sendMessage` | `(msgs: Message[]) => Promise<...>` | Send next message | ## useCredits Hook ```tsx import { useCredits } from '@ainative/react-sdk'; function CreditsBar() { const { balance, isLoading, error, refetch } = useCredits(); if (isLoading) return <div>Loading credits...</div>; if (error) return <div>Error: {error.message}</div>; return ( <div> <span>{balance?.remaining_credits} credits remaining</span> <span> | Plan: {balance?.plan}</span> <button onClick={refetch}>Refresh</button> </div> ); } ``` ### useCredits Return | Field | Type | Description | |-------|------|-------------| | `balance` | `CreditBalance \| null` | Balance data | | `isLoading` | `boolean` | Fetching state | | `error` | `AINativeError \| null` | Error state | | `refetch` | `() => void` | Manually refresh | `CreditBalance` shape: `{ remaining_credits: number, plan: string, ... }` ## Exports ```typescript import { AINativeProvider, useChat, useCredits, useAINativeContext, // access raw config/baseUrl type Message, type ChatCompletionResponse, type CreditBalance, type AINativeError, type UseChatOptions, } from '@ainative/react-sdk'; ``` ## Environment Variable (CRA / Vite) ```bash REACT_APP_AINATIVE_API_KEY=ak_your_key # CRA VITE_AINATIVE_API_KEY=ak_your_key # Vite ``` ```tsx <AINativeProvider config={{ apiKey: process.env.REACT_APP_AINATIVE_API_KEY! }}> ``` ## References - `packages/sdks/react/src/hooks/useChat.ts` — Hook implementation - `packages/sdks/react/src/hooks/useCredits.ts` — Credits hook - `packages/sdks/react/src/AINativeProvider.tsx` — Provider context - `packages/sdks/react/src/index.ts` — Package exports

标签

skill ai

通过对话安装

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

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 ainative-react-sdk-1776063677 技能

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

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

通过命令行安装

skillhub install ainative-react-sdk-1776063677

下载 Zip 包

⬇ 下载 ainative-react-sdk v1.0.0

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

v1.0.0 最新 2026-4-14 10:02
- Initial public release of ainative-react-sdk.
- Provides React hooks for AI chat (useChat), credit tracking (useCredits), and managed sessions via AINativeProvider.
- Includes TypeScript types and context access (useAINativeContext).
- Documentation covers installation, hook usage, provider setup, handling loading/error states, and environment variable configuration.
- Published as @ainative/react-sdk version 1.0.1 on npm.

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

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

p2p_official_large
返回顶部