v0.1.0 Edit on GitHub

Quick Start

Add Coign to any HTML page in two lines:

HTML
<script>
  window.Coign = window.Coign || function() {
    (window.Coign.q = window.Coign.q || []).push(arguments);
  };
  Coign('init'); // shell first — model loads on ask
</script>
<!-- Prefer the ES loader (code-split). IIFE is ~2.1 MB gzip. -->
<script type="module" src="./dist/coign-loader.es.js"></script>
<!-- or: <script src="./dist/coign-sdk.iife.js"></script> -->

The window.Coign stub queues calls before the SDK loads. Prefer coign-loader.es.js so WebLLM stays deferred; use the IIFE only when you need a single classic script tag (~2.1 MB gzip).

Edit on GitHub

Installation

CDN (Recommended)

Use coign-loader.es.js with the queue stub above (ES modules). Fall back to coign-sdk.iife.js for classic script tags — that bundle includes WebLLM (~2.1 MB gzip).

ESM (npm)

Shell
npm install coign-sdk
JavaScript
import { init, config } from 'coign-sdk';

init(); // or init({ preset: 'coign-lite', engage: 'ask' });
config({ theme: { accent: '#6366f1' } });

Edit on GitHub

Configuration

The config function accepts a flat object of options. Coign is client-side only: no cloud API calls are made during inference, so no API key or remote endpoint is required.

OptionTypeDescription
modelstringModel identifier or preset name
themeobjectAccent, text, bg, radius colors
positionstringWidget position: bottom-right or inline
inlineMountstringCSS selector for inline mount target
systemPromptstringCustom system prompt override
contextWindowSizenumberMax tokens in context
temperaturenumberSampling temperature (0–1)
maxTokensnumberMax response tokens
onDownloadProgressfunctionCallback on model download progress
onDownloadCompletefunctionCallback when model download finishes
onDownloadErrorfunctionCallback when model download fails
maxRetriesnumberMax retry attempts for init (default 0)
retryDelayMsnumberDelay between retry attempts in ms (default 1000)

Edit on GitHub

API Reference

init(options)

Initializes the SDK with a model preset or custom configuration. Must be called before other API methods.

JavaScript
init(); // or init({ preset: 'coign-lite', engage: 'ask' });
init({ model: 'Llama-3.2-3B-Instruct-q4f16_1-MLC' });

config(options)

Updates configuration after init. Merges with existing config.

JavaScript
config({ theme: { accent: '#10b981' } });

ask(message)

Sends a message to the agent and returns a Promise that resolves with the response string.

JavaScript
const answer = await Coign.ask('Summarize this page');

createWidget()

Creates and mounts the chat widget into the page. The widget is a singleton — calling this again returns the same instance.

JavaScript
Coign.createWidget();
Coign.showWidget();   // open panel
Coign.hideWidget();   // close panel
Coign.destroyWidget(); // remove from DOM

showConfirmDialog(message, risk, confirmationValue?)

Shows a native <dialog> for risky operations. Returns Promise<boolean>.

JavaScript
const allowed = await Coign.showConfirmDialog(
  'Delete all user data?',
  'destructive',
  'DELETE'
);

Edit on GitHub

Browser Check

Use checkSupport() to verify WebGPU and estimate available VRAM before calling init():

JavaScript
const check = await Coign.checkSupport();
if (!check.supported) {
  alert('Coign requires WebGPU. Reason: ' + check.reason);
}

Returns an object with:

Edit on GitHub

Download Progress

Model downloads are large (400 MB–4.5 GB). You can track progress with callbacks or events:

JavaScript
init({
  preset: 'coign-balanced',
  onDownloadProgress: (p) => {
    console.log(p.stage, p.progress); // 'downloading', 0.42
  },
  onDownloadComplete: () => console.log('Done!'),
  onDownloadError: (err) => console.error(err.message),
});

The built-in widget also shows a progress overlay automatically. You can cancel a slow download:

JavaScript
Coign.cancelEngineInit(); // or click Cancel in the widget overlay

Edit on GitHub

Lifecycle

Check SDK state at any time:

JavaScript
Coign.isInitialized(); // true after init() resolves
Coign.isReady();       // true when model is loaded and engine is ready

Retry a failed init with exponential backoff:

JavaScript
Coign.retryInit({ preset: 'coign-balanced', maxRetries: 3, retryDelayMs: 2000 });

Swap models without destroying history or tools:

JavaScript
await Coign.swapModel('coign-code');

Edit on GitHub

Architecture

Coign is designed as a set of layered modules:

All modules are pure ESM with .js extensions. Browser-only code guards with typeof window !== 'undefined' so it can be imported in Node/Vitest contexts.

Edit on GitHub

Model Presets

Preset names resolve to WebLLM model IDs. Models are downloaded once and cached for offline inference — no cloud calls.

PresetModel IDSizeTool supportRecommended for
coign-quickSmolLM2-135M-Instruct-q4f16_1-MLC~79 MBnoneFirst-answer progressive path
coign-tinySmolLM2-360M-Instruct-q4f16_1-MLC~207 MBnoneQuick fallback
coign-liteLlama-3.2-1B-Instruct-q4f16_1-MLC~700 MBmanualDefault full model
coign-balancedLlama-3.2-3B-Instruct-q4f16_1-MLC~1800 MBmanualStronger reasoning
coign-qualityLlama-3.1-8B-Instruct-q4f16_1-MLC~4500 MBmanualLong-form reasoning
coign-toolsHermes-2-Pro-Llama-3-8B-q4f16_1-MLC~4500 MBnativeTool-call reliability

Edit on GitHub

Custom Tools

Register custom tools with Coign.registerTool:

JavaScript
Coign.registerTool({
  name: 'sendAnalytics',
  description: 'Send a page-view event to analytics',
  parameters: {
    type: 'object',
    properties: {
      page: { type: 'string', description: 'Page path' }
    },
    required: ['page']
  },
  execute: async ({ page }) => {
    gtag('event', 'page_view', { page });
    return { sent: true };
  }
});

Tools are called via an XML-based manual loop by default. The LLM generates <tool> tags, the SDK parses them, executes the matching tool, and feeds the result back into the conversation.

Edit on GitHub

Theming

Pass a theme object to config():

JavaScript
Coign.config({
  theme: {
    accent: '#6366f1',
    text: '#1e293b',
    bg: '#ffffff',
    radius: '0.5rem'
  }
});

Edit on GitHub

Risk Tiers

Operations that modify state or access sensitive data require user confirmation:

TierBehaviorExample
readSilentReading page content
writeModal confirmationClicking a submit button
destructiveModal + typed confirmationDeleting data

Edit on GitHub