@cloudgrid-io/runtime

中文文档
1.0.4发布于 2026/10/26MITTypeScriptES Module零依赖近 30 天下载 3874

Cloud Grid Runtime SDK — zero-config access to platform services (runtime.ai.*)

原始使用文档
文档目录14 节
  1. Install
  2. Prerequisite
  3. Use
  4. Platform vs bring-your-own
  5. Managed substrate — don't reach for a personal key
  6. Methods
  7. Models
  8. App-to-app calls
  9. Declare the relationship
  10. Make the call
  11. Calling from a non-JS runtime
  12. Errors
  13. Compatibility
  14. More

@cloudgrid-io/runtime

The Runtime SDK for CloudGrid. Zero-config access to platform services from inside any entity on the grid — no API keys in your code. Two capabilities today: runtime.ai.* for platform AI, and runtime.call for app-to-app calls.

This package supersedes @cloudgrid-io/ai. AI now lives under runtime.ai. The old package name still works (it re-exports this one), so existing entities keep running with no code change.

Install

npm install @cloudgrid-io/runtime

Prerequisite

Add ai to the requires: block of your cloudgrid.yaml:

name: my-app
services:
  api:
    type: node
    path: /
requires:
  - ai

The platform injects RUNTIME_GATEWAY_URL at runtime — the SDK's only gateway env var. The SDK resolves identity internally — your code does not see a token, and never sees or handles a provider key.

Use

import { runtime } from '@cloudgrid-io/runtime';

// Chat
const { text } = await runtime.ai.chat({
  prompt: 'Summarize this report: ...',
  model: 'claude-haiku',
});

// Generate (single prompt)
const out = await runtime.ai.generate({ prompt: 'Write a haiku' });

// Vision
const image = await readFile('./screenshot.png');
const desc = await runtime.ai.vision({ image, prompt: 'What is in this image?' });

// Embeddings — for search and RAG
const vec = await runtime.ai.embeddings({ text: 'hello world' });
// vec.values : number[] (1536 for the default model)
// vec.model, vec.dim, vec.tokens

// Batch embeddings — preserves input order
const vecs = await runtime.ai.embeddings({ text: ['a', 'b', 'c'] });

// Streaming chat
for await (const chunk of runtime.ai.chat({ prompt, stream: true })) {
  if (chunk.type === 'text') process.stdout.write(chunk.text);
}

The SDK takes no identity argument. It reads identity from one of two places:

  • inside a CloudGrid pod, the platform-mounted identity file at /var/run/cloudgrid/identity/token
  • on a developer machine, ~/.cloudgrid/credentials plus <cwd>/.cloudgrid/link.json

Outside both, the first call throws IdentityNotFoundError.

Platform vs bring-your-own

Every AI service (chat, image, embeddings) carries a per-Grid provider decision, set by a Grid admin in the Console under Grid settings → Services:

  • Platform (default) — the call runs on the platform's key; usage is metered to the Grid.
  • Bring your own — the call runs on the Grid's own key (stored encrypted in the Grid Vault); the platform meters usage but does not bill for it.

Your code does not change. The same line runs on platform tokens or the Grid's key depending on one switch in the Grid's settings. The key never leaves the server.

Managed substrate — don't reach for a personal key

If you need embeddings, use runtime.ai.embeddings. Do not install a personal OpenAI key or a third-party vector service — the platform provides the model and (when the Grid opts in) meters the spend. The default embedding model is 1536-dimensional, matching the platform's managed pgvector.

Methods

runtime.ai.chat(req): Promise<AIResponse>
runtime.ai.chat({ ...req, stream: true }): AsyncGenerator<StreamChunk>
runtime.ai.generate(req): Promise<AIResponse>
runtime.ai.vision(req): Promise<AIResponse>
runtime.ai.embeddings({ text: string }): Promise<EmbeddingVector>
runtime.ai.embeddings({ text: string[] }): Promise<EmbeddingVector[]>
runtime.ai.image(req): Promise<ImageResponse>
runtime.call(address, path?, init?): Promise<Response>

interface AIResponse {
  text: string;
  model: string;
  usage: { input_tokens: number; output_tokens: number };
}

interface EmbeddingVector {
  values: number[];
  model: string;
  dim: number;
  tokens: number;
}

Models

Pass model: as one of the gateway aliases — claude-haiku (fast), claude-sonnet (default), claude-opus (highest capability) — and the gateway resolves it server-side. Embeddings default to text-embedding-3-small. The gateway enforces a server-side model whitelist.

App-to-app calls

runtime.call reaches another app on the grid. The model (Decision 051): an app is callable by another app only if the callee has granted the caller. The grant is enforced by the callee's network policy, so there is no token to carry and nothing injected into your environment — the address resolves at call time and the request is a plain fetch. RUNTIME_GATEWAY_URL is the only env var this uses, and the platform injects it into every service; runtime.call needs no requires:/needs: entry.

Declare the relationship

The caller lists what it calls; the callee lists who may call it.

# caller's cloudgrid.yaml
calls:
  - todo-api            # an app in this grid (the common case)
  - qa-todo/todo-api    # <grid-slug>/<app-slug> (same-grid only in this release)
  - 42f6e1c3-...        # or the entity id, stable across renames
# callee's cloudgrid.yaml
callers:
  scope: explicit
  allowed_apps:
    - todo-viewer   # named callers are granted at the caller's plug
  # policy: auto    # or: grant every same-grid caller (a deliberate opt-in)
  # policy: manual  # or: every grant waits for admin approval in Console

The default is deny. A caller not named in allowed_apps (and not covered by policy: auto) is born requested and waits for the callee admin's approval in Console. Approve or revoke takes effect live — the caller does not re-plug. Membership in a grid grants nothing on its own; only an explicit grant does.

Make the call

import { runtime } from '@cloudgrid-io/runtime';

const res = await runtime.call('todo-api', '/todos');
if (res.ok) {
  const todos = await res.json();
}

runtime.call(address, path?, init?) returns a standard Response:

  • address — the same three forms calls: accepts (app slug, <grid-slug>/<app-slug>, or entity id).
  • path — appended to the callee's internal URL; omit it to hit the root.
  • init — a standard RequestInit, passed to fetch untouched. The SDK sets no header of its own, so Authorization stays free for your app's own protocol.

The Response comes back as-is: an HTTP error status is the callee's answer, not an SDK failure. runtime.call throws only when the call cannot be made — see Errors below. It works inside a deployed app; on a developer machine it throws GridError CALLER_NOT_CONFIGURED, so plug the app with grid plug and call other apps from there.

Calling from a non-JS runtime

runtime.call is a thin wrapper over one platform request plus a plain fetch. A Python or Go app does the same two steps directly.

  1. Resolve the address. The platform injects RUNTIME_GATEWAY_URL into every service and mounts the pod identity token at /var/run/cloudgrid/identity/token.

    GET ${RUNTIME_GATEWAY_URL}/app-connection/resolve?address=todo-api
    Authorization: Bearer <contents of /var/run/cloudgrid/identity/token>
    

    On success the response is 200 with { "url": "http://<svc>-app.<namespace>.svc.cluster.local", "entity_id": "<uuid>" }. Cache the url — the SDK reuses it for about 5 minutes, and the in-cluster name is stable across re-plugs. The endpoint requires the signed pod token; the legacy X-CloudGrid-* identity headers are rejected. Failure statuses:

    StatuscodeMeaning
    401MISSING_IDENTITY / IDENTITY_TOKEN_REQUIREDNo bearer token, or the legacy header path was used.
    401IDENTITY_EXPIREDToken rotated; wait (Retry-After: 60) and retry.
    403CALL_NOT_GRANTEDThis app is not granted, or the grant is still pending. fix names the next step.
    404CALLER_TARGET_NOT_FOUND / CALLEE_NOT_ROUTABLEThe target does not exist in this grid, or has no callable service.
    400CALLER_INVALID_ADDRESS / CROSS_GRID_NOT_SUPPORTEDMalformed address, or a cross-grid target (not supported this release).
  2. Call the resolved URL with a plain request — no auth header from the platform. The callee's network policy already decided you may connect.

    GET <url>/todos
    

Errors

import {
  GridError,
  GridAIError,
  IdentityNotFoundError,
  MalformedIdentityError,
} from '@cloudgrid-io/runtime';

GridError is the base grid error for the runtime surface; it carries code (string), status (HTTP status), and an optional fix (an actionable next step). GridAIError is the subclass thrown by runtime.ai.* calls. CloudGridAIError is still exported as a deprecated alias of GridAIError, so existing instanceof CloudGridAIError catches keep working. IdentityNotFoundError is thrown when neither in-grid nor dev identity is available; MalformedIdentityError is thrown when the identity file or link.json exists but cannot be parsed.

runtime.call throws GridError with one of two codes when the call cannot be made: CALLER_NOT_CONFIGURED when the address cannot be resolved (this app is not granted, the target does not exist, or the address is malformed), and CALLEE_UNREACHABLE when the address resolved but the connection failed (the callee is down, or its admin removed this app from its callers). A non-2xx HTTP status is not an error — it is returned on the Response.

Compatibility

@cloudgrid-io/[email protected] re-homes the AI SDK under runtime.ai. The top-level createClient(), the legacy ai singleton, and createLegacyClient() are still exported for back-compat. The deprecated @cloudgrid-io/ai package re-exports everything here, so existing imports keep working.

More

版本与发布记录

@cloudgrid-io/runtime 共发布 5 个版本,首次发布于 2026/07/07;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.0.4latest2026/10/2614 天前
1.0.32026/07/123 个月前
1.0.22026/07/123 个月前
1.0.12026/07/123 个月前
1.0.02026/07/074 个月前

关于 @cloudgrid-io/runtime 的常见问题

@cloudgrid-io/runtime 是什么?

Cloud Grid Runtime SDK — zero-config access to platform services (runtime.ai.*)

如何安装 @cloudgrid-io/runtime?

在项目目录运行 npm install @cloudgrid-io/runtime,或使用 pnpm add / yarn add / bun add。

@cloudgrid-io/runtime 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cloudgrid-io/runtime 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cloudgrid-io/runtime 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cloudgrid-io/runtime 可以商用吗?

@cloudgrid-io/runtime 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cloudgrid-io/runtime 的下载量有多少?

根据 npm 官方下载统计,@cloudgrid-io/runtime 在 2026/10/10 – 2026/11/08 这 30 天内被下载 3,874 次,日均约 129 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cloudgrid-io/runtime 还在维护吗?

最新版本 1.0.4 发布于 2026/10/26(14 天前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/07/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。