Skip to main content

ChuckyClient

The main client class for interacting with Chucky. Matches the official Claude Agent SDK V2 interface.

Installation

npm install @chucky.cloud/sdk

Import

import { ChuckyClient, createClient, getAssistantText } from '@chucky.cloud/sdk';

Constructor

const client = new ChuckyClient(options: ClientOptions);

ClientOptions

PropertyTypeRequiredDefaultDescription
tokenstringYes-JWT authentication token
baseUrlstringNo'wss://conjure.chucky.cloud/ws'WebSocket server URL
debugbooleanNofalseEnable debug logging
timeoutnumberNo60000Connection timeout (ms)
keepAliveIntervalnumberNo300000Keep-alive interval (ms)

Example

const client = new ChuckyClient({
  token: 'your-jwt-token',
  debug: true,
});

Methods

prompt()

Send a one-shot prompt (stateless). Creates a session, sends the message, and returns the result.
async prompt(message: string, options?: SessionOptions): Promise<SDKResultMessage>

Example

const result = await client.prompt(
  'What is 2 + 2?',
  { model: 'claude-sonnet-4-5-20250929' }
);

if (result.subtype === 'success') {
  console.log(result.result); // "4"
  console.log(result.total_cost_usd); // 0.0001
}

createSession()

Create a new conversation session. Returns the session immediately; connection happens on first send().
createSession(options?: SessionOptions): Session

SessionOptions

PropertyTypeDescription
modelstringClaude model to use
systemPromptstringSystem prompt
toolsTool[]Available tools
mcpServersMcpServer[]MCP servers
maxTurnsnumberMax conversation turns
maxBudgetUsdnumberBudget limit
outputFormatOutputFormatStructured output format
sessionIdstringResume existing session
forkSessionstringFork from session ID
continuebooleanContinue from last message

Example

const session = client.createSession({
  model: 'claude-sonnet-4-5-20250929',
  systemPrompt: 'You are a helpful assistant.',
});

await session.send('Hello!');
for await (const msg of session.stream()) {
  if (msg.type === 'assistant') {
    console.log(getAssistantText(msg));
  }
}

session.close();

resumeSession()

Resume an existing session by ID.
resumeSession(
  sessionId: string,
  options?: Omit<SessionOptions, 'sessionId'>
): Session

Example

const session = client.resumeSession('session-123');

await session.send('What did we discuss before?');
for await (const msg of session.stream()) {
  // handle messages
}

close()

Close all active sessions.
close(): void

Example

// Clean up when done
client.close();

on()

Register event handlers.
on(handlers: ClientEventHandlers): this

ClientEventHandlers

HandlerParametersDescription
onConnect()WebSocket connected
onDisconnect(reason: string)WebSocket disconnected
onReconnect(attempt: number)Reconnection attempt
onError(error: Error)Error occurred

Factory Function

createClient()

Alternative factory function for creating clients.
function createClient(options: ClientOptions): ChuckyClient

Example

const client = createClient({ token: 'your-token' });

Helper Functions

getAssistantText()

Extract text content from an assistant message.
import { getAssistantText } from '@chucky.cloud/sdk';

const text = getAssistantText(msg);

getResultText()

Extract the result text from a result message.
import { getResultText } from '@chucky.cloud/sdk';

const text = getResultText(msg);

Complete Example

import { ChuckyClient, tool, getAssistantText } from '@chucky.cloud/sdk';

// Create client
const client = new ChuckyClient({
  token: process.env.CHUCKY_TOKEN,
  debug: true,
});

// Define a tool
const calculatorTool = tool(
  'calculate',
  'Perform math calculations',
  {
    type: 'object',
    properties: {
      expression: { type: 'string' },
    },
    required: ['expression'],
  },
  async ({ expression }) => {
    const result = eval(expression); // Use a proper math lib in production
    return { content: [{ type: 'text', text: String(result) }] };
  }
);

// Create a session with tools
const session = client.createSession({
  model: 'claude-sonnet-4-5-20250929',
  systemPrompt: 'You are a math tutor.',
  tools: [calculatorTool],
});

// Send message and stream response
await session.send('What is 15% of 230?');
for await (const msg of session.stream()) {
  if (msg.type === 'assistant') {
    const text = getAssistantText(msg);
    if (text) process.stdout.write(text);
  }
  if (msg.type === 'result' && msg.subtype === 'success') {
    console.log('\nCost:', msg.total_cost_usd);
  }
}

// Multi-turn conversation
await session.send('Explain how you calculated that');
for await (const msg of session.stream()) {
  if (msg.type === 'assistant') {
    console.log(getAssistantText(msg));
  }
}

// Clean up
session.close();
client.close();