Quickstart

Try It Now

Run a complete call chain with the default database, FederationAdmin, and User City.

1. Start City

import {
  Federation,
  AIService,
  AIChannel,
  type AIChannelStreamInput,
  type LanguageModelV3StreamResult,
} from "@downcity/federation";
import { Database } from "@downcity/database-sqlite";

const database = new Database({ filename: "./data.sqlite" });
const base = new Federation({ database });

class EchoChannel extends AIChannel {
  constructor() {
    super({ id: "echo" });
  }

  protected async stream(
    input: AIChannelStreamInput,
  ): Promise<LanguageModelV3StreamResult> {
    const input_text = input.call.prompt
      .flatMap((message) => Array.isArray(message.content) ? message.content : [])
      .filter((part) => part.type === "text")
      .map((part) => part.text)
      .join("\n");
    return {
      stream: new ReadableStream({
        start(controller) {
          controller.enqueue({ type: "stream-start", warnings: [] });
          controller.enqueue({ type: "text-start", id: "echo_text" });
          controller.enqueue({ type: "text-delta", id: "echo_text", delta: `Echo: ${input_text}` });
          controller.enqueue({ type: "text-end", id: "echo_text" });
          controller.enqueue({
            type: "finish",
            finishReason: { unified: "stop", raw: "stop" },
            usage: {
              inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
              outputTokens: { total: 0, text: 0, reasoning: 0 },
            },
          });
          controller.close();
        },
      }),
    };
  }
}

const echo = new EchoChannel();

const ai = new AIService();
ai.use(echo.model({ id: "local-echo", upstream_model: "local-echo", name: "Local Echo" }));
base.use(ai);

await base.health();
serve({ fetch: (request) => base.fetch(request), port: 43127, hostname: "127.0.0.1" });

2. Create Bureau & Request Token

const admin = new FederationAdmin({
  base_url: "http://127.0.0.1:43127",
  credential: administrator_session_token,
});
const token = await admin.service("accounts").action("tokens/issue").invoke({
  bureau_id: "demo",
  user_id: "user_123",
  ttl: "7d",
});

3. Client Call

const city = new City({
  federation_url: "http://127.0.0.1:43127",
  user_token: token.user_token,
});

// SDK pathway
const result = await city.ai.text({ model: "local-echo", prompt: "Hello" });

Next Steps