npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cloudtrain/sdk

v0.9.0

Published

Official SDK for the CloudTrain API — build and interact with AI agents trained on your data.

Readme

CloudTrain SDK

The official CloudTrain SDK for JavaScript and TypeScript — interact with AI agents trained on your data through a small, typed client. Works in Node.js, Bun, Deno, browsers, and edge runtimes.

Users must create an AI model on CloudTrain AI and generate an API key to use this SDK.


🚀 Features

  • Typed chat() and chatStream() methods.
  • Streaming responses via async iterators.
  • Universal: runs in Node, Bun, browsers, and edge runtimes.
  • Zero dependencies.

📖 Getting Started

1️⃣ Installation

npm i @cloudtrain/sdk

2️⃣ Usage

import { CloudTrain } from "@cloudtrain/sdk";

const client = new CloudTrain({
  apiKey: "YOUR_API_KEY_HERE",
});

const completion = await client.chat({
  messages: [
    { role: "user", content: "What is CloudTrain?" },
  ],
});

console.log(completion.choices[0].message.content);

Replace YOUR_API_KEY_HERE with the API key generated at CloudTrain AI.


3️⃣ Streaming Responses

Use chatStream() to receive the response as an async iterable of text chunks:

const stream = client.chatStream({
  messages: [
    { role: "user", content: "Write a short poem about the sea." },
  ],
});

for await (const chunk of stream) {
  process.stdout.write(chunk);
}

4️⃣ Structured Output (JSON Schema)

Constrain the model to a JSON Schema and get a typed parsed object back — no manual JSON.parse needed.

type MarketingEmail = {
  subject: string;
  preview: string;
  body: string;
  cta_label: string;
};

const completion = await client.chat<MarketingEmail>({
  messages: [{ role: "user", content: "Draft a launch email for our new live chat feature." }],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "MarketingEmail",
      schema: {
        type: "object",
        properties: {
          subject:   { type: "string" },
          preview:   { type: "string" },
          body:      { type: "string" },
          cta_label: { type: "string" },
        },
        required: ["subject", "preview", "body", "cta_label"],
      },
      strict: true,
    },
  },
});

const email = completion.choices[0].message.content;
console.log(email.subject);    // typed as string
console.log(email.cta_label);  // typed as string

Only type: "text" (default) and type: "json_schema" are supported. json_object is rejected by the API.

Streaming JSON — buffer-then-parse

chatStream() delivers raw text chunks. For structured output, buffer the chunks and parse once the stream completes:

let buffer = "";
await client.chatStream({
  messages,
  response_format: { type: "json_schema", json_schema: { name: "Email", schema } },
  onChunk: (chunk) => { buffer += chunk; },
  onComplete: () => {
    const email: MarketingEmail = JSON.parse(buffer);
  },
});

Streaming JSON — progressive partial objects

For generative-UI patterns (form fields filling in live, lists rendering as they're produced), use chatStreamPartial<T>(). The SDK parses CloudTrain's partial-json stream and emits a Partial<T> snapshot per event, then a final fully-formed T on completion:

await client.chatStreamPartial<MarketingEmail>({
  messages,
  response_format: {
    type: "json_schema",
    json_schema: { name: "MarketingEmail", schema },
  },
  onPartial: (partial) => {
    // Re-render with whatever fields the model has produced so far.
    setDraft(partial);
  },
  onComplete: (final) => {
    // Fully-formed object — all required fields present.
    saveDraft(final);
  },
});

Each partial may be missing fields the model hasn't reached yet — the parameter is typed Partial<T> to reflect this. onComplete receives the last snapshot as T.


5️⃣ Passing Meta Data

Pass a custom meta object to provide additional context to the AI:

await client.chat({
  messages: [{ role: "user", content: "Hi" }],
  meta: { name: "John" },
});

6️⃣ Custom Base URL

Override the API base URL (useful for self-hosted deployments or testing):

const client = new CloudTrain({
  apiKey: "YOUR_API_KEY_HERE",
  baseUrl: "https://your-domain.com",
});

7️⃣ Error Handling

Failed requests throw a CloudTrainAPIError with the response status and error type:

import { CloudTrain, CloudTrainAPIError } from "@cloudtrain/sdk";

try {
  await client.chat({ messages: [{ role: "user", content: "Hi" }] });
} catch (error) {
  if (error instanceof CloudTrainAPIError) {
    console.error(`[${error.status}] ${error.type}: ${error.message}`);
  } else {
    throw error;
  }
}

📌 API Reference

🔹 new CloudTrain(config)

| Option | Type | Required | Description | |------------|--------|----------|--------------------------------------------------------------| | apiKey | String | ✅ Yes | The API key generated on CloudTrain AI. | | baseUrl | String | ❌ No | Custom API base URL. Defaults to https://cloudtrain.ai. |

🔹 client.chat<T>(options)

Sends a chat completion request and resolves with the full response. When response_format.type === "json_schema", the SDK auto-parses message.content into the typed object T.

| Option | Type | Required | Description | |-------------------|------------------|----------|-----------------------------------------------------------------------------------| | messages | Message[] | ✅ Yes | The conversation history. | | meta | Object | ❌ No | Custom metadata passed to the AI model. | | response_format | ResponseFormat | ❌ No | { type: "text" } (default) or { type: "json_schema", json_schema: { name, schema, strict? } }. | | signal | AbortSignal | ❌ No | Abort the request. | | timeoutMs | Number | ❌ No | Per-request timeout. Defaults to client timeoutMs. |

Returns: Promise<ChatCompletion<T>> (with T = string by default).

🔹 client.chatStream(options)

Same options as chat(), plus onChunk / onComplete / onError callbacks for streaming text chunks. response_format is forwarded but chunks are delivered as raw strings — buffer them and parse at onComplete if using json_schema.

🔹 client.chatStreamPartial<T>(options)

CloudTrain-specific structured streaming. Requires response_format.type === "json_schema". Emits a Partial<T> snapshot on each event and a final T on completion.

| Option | Type | Required | Description | |-------------------|-------------------------------------|----------|----------------------------------------------------------------------| | messages | Message[] | ✅ Yes | The conversation history. | | response_format | { type: "json_schema", ... } | ✅ Yes | Required — partial streaming only makes sense for JSON Schema. | | onPartial | (partial: Partial<T>) => void | ✅ Yes | Called with the latest snapshot on every event. | | onComplete | (final: T) => void | ❌ No | Called once with the final fully-formed object. | | onError | (err: unknown) => void | ❌ No | Called on stream errors. | | meta | Object | ❌ No | Custom metadata forwarded to the AI model. | | signal | AbortSignal | ❌ No | Abort the stream. | | timeoutMs | Number | ❌ No | Time-to-first-byte timeout. |

🔹 Types

type Message<T = string> = {
  role: "system" | "user" | "assistant";
  content: T;
};

type ResponseFormat =
  | { type: "text" }
  | {
      type: "json_schema";
      json_schema: {
        name: string;
        schema: Record<string, unknown>;
        strict?: boolean;
      };
    };

type ChatCompletion<T = string> = {
  id: string;
  object: "chat.completion";
  choices: {
    index: number;
    message: Message<T>;
    finish_reason: string;
  }[];
};

🔑 How to Generate API Key

  1. Go to CloudTrain AI.
  2. Create an AI model for your chatbot.
  3. Generate an API key for the model.
  4. Use the API key in the apiKey option when constructing the client.

📜 License

This project is licensed under the MIT License.


💡 Need help? Contact support at CloudTrain AI. 🚀