Quick Start

Add Coign to any HTML page in two lines:

<script>
  window.Coign = window.Coign || function() {
    (window.Coign.q = window.Coign.q || []).push(arguments);
  };
  Coign('init', { preset: 'coign-balanced' });
</script>
<script src="https://your-cdn.com/coign-sdk.iife.js"></script>

The window.Coign stub queues calls before the SDK loads. Once loaded, the real API replays the queue automatically.

Installation

CDN (Recommended)

Use the IIFE bundle with the queue-loader pattern shown above. This works on any plain HTML site with no build step.

ESM (npm)

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

init({ preset: 'coign-balanced' });
config({ theme: { accent: '#6366f1' } });

Configuration

The config function accepts a flat object of options:

OptionTypeDescription
apiKeystringAPI key if using a cloud proxy (optional)
endpointstringCustom LLM endpoint (optional)
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)

API Reference

init(options)

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

init({ preset: 'coign-balanced' });
init({ model: 'Llama-3.1-8B-Instruct-q4f32_1-MLC' });

config(options)

Updates configuration after init. Merges with existing config.

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

ask(message)

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

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.

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>.

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

Browser Check

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

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

Returns an object with:

Download Progress

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

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:

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

Lifecycle

Check SDK state at any time:

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

Retry a failed init with exponential backoff:

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

Swap models without destroying history or tools:

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

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.

Model Presets

PresetModelDescription
coign-balancedLlama-3.1-8B Instruct q4f32_1Fast, capable, good default
coign-creativeQwen2.5-7B Instruct q4f16_1Best for creative writing
coign-codeDeepSeek-Coder-V2-Lite q4f16_1Code generation and review
coign-localPhi-3.5-mini-instruct q4f16_1Smallest, fastest on low-end devices

Custom Tools

Register custom tools with Coign.registerTool:

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.

Theming

Pass a theme object to config():

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

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