360 Secure SkillHub
360 安全技能中心

Ainative React Sdk

该 Skill 提供 @ainative/react-sdk 的安装与使用说明,涵盖 AINativeProvider、useChat 聊天补全、useCredits 额度显示、环境变量配置及加载/错误处理示例;仅含文档,不包含 SDK 源码、后端服务或测试部署配置。

urbantech
urbantech
热度 54.76
最近更新 2026-05-11 17:35:38
分类:开发依赖:第三方 API应用场景:代码生成 / API 集成

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add ainative-react-sdk
Metadata
name
ainative-react-sdk
description
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.

@ainative/react-sdk

React hooks and components for AINative — chat completions, credit tracking, and managed sessions.

Install

npm install @ainative/react-sdk

Setup: AINativeProvider

Wrap your app (or a subtree) with the provider:

import { AINativeProvider } from '@ainative/react-sdk';

function App() {
  return (
    <AINativeProvider config={{ apiKey: 'ak_your_key' }}>
      <YourApp />
    </AINativeProvider>
  );
}

useChat Hook

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

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

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)

REACT_APP_AINATIVE_API_KEY=ak_your_key   # CRA
VITE_AINATIVE_API_KEY=ak_your_key        # Vite
<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