快速开始
连接 Client
用 User City 从产品侧调用 Federation 的 Service 和 Action。
User City 有两条常用通路:
city.ai.*调内置 AIServicecity.service(id).action(name).invoke()调自定义 Service 或服务 Service
import { City } from "@downcity/federation/legacy";
const city = new City({
federation_url: "http://127.0.0.1:43127",
user_token: "ub_xxx",
});
// AI Service — SDK 通路
const msg = await city.ai.text({ prompt: "hello", model: "deepseek-v4-flash" });
const stream = await city.ai.stream({ prompt: "hello", model: "deepseek-v4-flash" });
// AI Service — OpenAI 通路(用 OpenAI SDK 直接调)
import OpenAI from "openai";
const openai = new OpenAI({ baseURL: "http://127.0.0.1:43127/v1/ai", apiKey: "ub_xxx" });
await openai.chat.completions.create({ model: "deepseek-v4-flash", messages: [...] });
// 自定义 Service
const result = await city.service("translate").action("zh2en").invoke({ text: "你好" });
// GET action(如读取登录结果)
const login = await city.service("accounts").get("login/result", { login_id: "login_xxx" });创建 Bureau、暂停 Bureau、请求 Federation 签发 user_token 和维护 Runtime env 都属于可信环境动作,用 FederationAdmin 发起,不放到产品前端。