Node.js SDK

The official Nascentist Node.js / TypeScript SDK provides fully-typed access to the REST API with zero dependencies.

Installation

1 lines
npm install nascentist

Quick Start

20 lines
import Nascentist from 'nascentist';

const client = new Nascentist({ apiKey: 'nsc_live_YOUR_KEY' });

// Simple completion
const response = await client.complete(
  'function bubbleSort(arr) {',
  { maxTokens: 200 }
);
console.log(response.choices[0].text);

// Agent fix
const fix = await client.agent.fix({
  code: 'function add(a, b) { return a - b }',
  language: 'javascript'
});

if (fix.status === 'fixed') {
  console.log(fix.fixedCode);
}

Project Memory

Set your project memory up with a convenient one-liner.

13 lines
// Store memory once using setup
await client.memory.setup({
  techStack: "Node.js 20, TS, PostgreSQL",
  conventions: "camelCase, single quotes",
  projectContext: "SaaS backend API"
});

// All future requests explicitly or implicitly use it
const response = await client.complete("Optimize this SQL query");

// List all project memory keys
const data = await client.memory.list();
data.memories.forEach(m => console.log(m.key));

Async Jobs

10 lines
// Wait for any asynchronous job seamlessly
const job = await client.jobs.wait('job_abc123', {
  timeout: 120000, // 2 minutes max
  onProgress: (j) => console.log('Job is', j.status)
});

// Once resolved as completed
if (job.status === 'completed') {
  console.log(job.result);
}

Streaming

We rely on AsyncIterables for modern async streaming natively in Node.js 18+.

8 lines
const stream = await client.complete('Write a react component', {
  stream: true,
  model: 'nascentist-coder'
});

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

Error Handling

14 lines
import Nascentist, { RateLimitError, JobTimeoutError } from 'nascentist';

try {
  await client.agent.test({ code: myCode, language: 'ts' });
} catch (error) {
  if (error instanceof RateLimitError) {
    console.log(`Rate limit! Retry after ${error.retryAfter}s`);
  } else if (error instanceof JobTimeoutError) {
    console.log('The requested job took too long and timed out');
  } else {
    // Other NascentistError exceptions or network failures...
    console.error(error);
  }
}