Skip to content
SuperInstancePublic

About

AI cells for Quilt — 4 providers, 8 cell kinds, one uniform interface

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@superinstance/quilt-ai — AI cells for Quilt

4 providers. 8 cell kinds. 1 uniform interface.

AI as a first-class cell kind in Quilt. Every LLM call, embedding, image, translation, sentiment, and code generation is a reactive cell that propagates with the rest of your sheet.

flowchart LR
    t["input.text"] --> e["ai.embed<br/>(BGE)"]
    e --> v["vector.search<br/>(top-K)"]
    v --> l["ai.llm<br/>(z.ai)"]
    l --> a["answer"]
    z["ai.zai<br/>(expensive)"] --> r["router"]
    d["ai.deepseek<br/>(cheap)"] --> r
    b["budget.over"] --> r
    r --> a
    classDef provider fill:#1b2a4a,stroke:#e8a54b,color:#f5ead6
    class z,d provider
Loading

Install

npm install @superinstance/quilt-ai

Quick start

import { AIEngine, createEngine } from '@superinstance/quilt-ai';

// Option 1: read keys from environment
const ai = createEngine();

// Option 2: pass keys explicitly
const ai = new AIEngine({
  zaiKey: process.env.ZAI_TOKEN,
  kimiKey: process.env.KIMI_TOKEN,
  deepseekKey: process.env.DEEPSEEK_TOKEN,
  cloudflareKey: process.env.CLOUDFLARE_TOKEN,
  cloudflareAccountId: process.env.CLOUDFLARE_ACCOUNT_ID,
});

// Use it directly
const answer = await ai.call({
  id: 'answer',
  kind: 'ai.llm',
  provider: 'zai',
  model: 'glm-4.5',
  prompt: 'What is Quilt?',
});
console.log(answer);

Or wire it into a Quilt sheet:

import { Quilt } from '@superinstance/quilt-core';
import { AIEngine } from '@superinstance/quilt-ai';

const sheet = new Quilt({ ai: new AIEngine() });

sheet.load({
  cells: [
    { id: 'question', kind: 'value', value: 'What is Quilt?' },
    { id: 'answer', kind: 'ai.llm', provider: 'zai', model: 'glm-4.5', prompt: '{{question}}' },
  ],
});

await sheet.tick();
console.log(sheet.get('answer'));

The 4 providers

Provider Best for Models Cost
z.ai Coding, reasoning GLM 4.5, AirX, Flash, 9B $0.001-$0.002 / 1k
Kimi Long context (128k) v1 8k/32k/128k, K2 $0.0001-$0.001 / 1k
DeepSeek Cheap parallel workers V3 Flash, R1 Reasoner $0.0001-$0.002 / 1k
Cloudflare Free tier, on-edge Llama 3.3, Mistral, BGE, SDXL Free tier 10k neurons/day

The 8 cell kinds

Kind Input Output Example
ai.llm prompt text Chat completion
ai.embed text vector Semantic search
ai.image prompt image URL Text → image
ai.translate text + target text Multi-language
ai.sentiment text {label, score} Classify mood
ai.summarize long text short text TL;DR
ai.code description code Code gen
ai.vision image + prompt text Caption an image

The 6 patterns

1. Cascade

- id: extract
  kind: ai.llm
  prompt: "Extract entities from: {{text}}"

- id: classify
  kind: ai.llm
  prompt: "Classify sentiment of: {{extract}}"

2. Fan-out (parallel)

- id: draft.zai:     { kind: ai.llm, provider: zai, ... }
- id: draft.kimi:    { kind: ai.llm, provider: kimi, ... }
- id: draft.deepseek: { kind: ai.llm, provider: deepseek, ... }
- id: best:
    kind: ai.llm
    prompt: "Pick best of: {{draft.zai}} {{draft.kimi}} {{draft.deepseek}}"

3. RAG

- id: q.embed:  { kind: ai.embed, input: question }
- id: matches:  { kind: vectorize.search, vector: q.embed, top_k: 5 }
- id: answer:   { kind: ai.llm, prompt: "Context: {{matches}}\nQ: {{question}}" }

4. Agent loop

- id: thought:    { kind: ai.llm, prompt: "What's next?" }
- id: action:     { kind: router, when: thought }
- id: step.advance: { kind: listener, when: "step < 10" }

5. Cost control

- id: answer.premium: { kind: ai.llm, provider: zai, ... }
- id: answer.cheap:   { kind: ai.llm, provider: deepseek, ... }
- id: answer:
    kind: router
    routes:
      - when: "total.cost >= budget"
        then: "answer.cheap"
      - then: "answer.premium"

6. Memoize

Cells cache by input hash automatically. Re-setting a cell with the same value is a no-op.

API

AIEngine

const engine = new AIEngine({
  zaiKey?: string,
  kimiKey?: string,
  deepseekKey?: string,
  cloudflareKey?: string,
  cloudflareAccountId?: string,
});

engine.registerProvider(name, provider, apiKey?);
engine.setKey(provider, apiKey);

const result = await engine.call(config, {
  useCache?: boolean,  // default: true
  signal?: AbortSignal,
});

engine.getCost();      // total USD spent
engine.getTokens();    // { prompt, completion, total }
engine.getCacheStats();
engine.clearCache();

createEngine()

Reads keys from process.env:

  • ZAI_TOKEN
  • KIMI_TOKEN
  • DEEPSEEK_TOKEN
  • CLOUDFLARE_TOKEN
  • CLOUDFLARE_ACCOUNT_ID

Security

Never put API keys in YAML sheets. Sheets are typically committed to git. Quilt refuses to load sheets that contain keys.

The 3 layers:

  1. Browser (Quilt Live): Calls a proxy URL. Keys never reach the browser.
  2. Cloudflare Worker: Keys set as Wrangler secrets. Only the Worker has access.
  3. Server (TS / Rust): Keys read from environment variables.

Examples

See the examples/ directory for complete sheets:

  • 01-basic-llm.yaml — single LLM call
  • 02-fanout-3models.yaml — 3 models in parallel
  • 03-rag.yaml — RAG over a vector store
  • 04-agent-loop.yaml — multi-step agent
  • 05-cost-control.yaml — budget-aware routing
  • 06-image-pipeline.yaml — text → enhanced prompt → image → caption

Test

npm test

The test suite runs 7 unit tests. To run real API tests, set the env vars first.

Part of Quilt

@superinstance/quilt-ai is one of 13 packages in the Quilt ecosystem:

Package Language Description
@superinstance/quilt-core TS Reactive engine
@superinstance/quilt-cli TS CLI + MCP server
quilt-rust Rust Sync + async engine
quilt-live HTML Single-file, offline
quilt-cloudflare Workers Edge-native with D1, Vectorize
@superinstance/quilt-ai (this) TS AI cells: 4 providers, 8 kinds
quilt-esp32 Rust Microcontroller
quilt-mesh TS CRDT peer-to-peer
quilt-agent TS LLM agent sheet
quilt-time TS Time travel
quilt-vault TS Encryption
quilt-vision TS Computer vision
quilt-zk TS Zero-knowledge proofs
quilt-flow TS Visual editor

Related Quilt repos

Quilt is an ecosystem of 15 repos, 5 deployment tiers, 3 languages. This repo is part of:

Tier Repo What it is
Canonical quilt TypeScript core (this ecosystem's home base)
Compiled quilt-rust Rust port — single static binary, axum, crossterm
Browser quilt-live Single 70KB HTML file that runs anywhere
IoT quilt-esp32 no_std Rust for ESP32, sensors + actuators
Edge quilt-cloudflare Cloudflare Workers + D1 + Vectorize + R2
Codespace quilt-codespace GitHub Codespace as a live Quilt runtime
AI quilt-ai LLM cells across 4 providers (z.ai, Kimi, DeepSeek, Cloudflare)
Evolution quilt-evolve Self-improvement loops, 4 components, 5 scopes
Mesh quilt-mesh CRDT-backed cross-tab / cross-device sync
Agent quilt-agent LLM agent as a sheet — memory, tools, reasoning
Time quilt-time Time-series cells with rolling windows
Vault quilt-vault Secrets cells with per-cell ACLs
Vision quilt-vision Computer-vision cells (camera → scene → caption)
ZK quilt-zk Zero-knowledge cell verification primitives
Flow quilt-flow Workflow cells — DAG execution, retry, rollback

See the Federation landing page for the architecture and the Engineering Bar for what "done right" means across all 15 repos.


License

MIT

About

AI cells for Quilt — 4 providers, 8 cell kinds, one uniform interface

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages