@codespar/types

中文文档
0.11.7发布于 2026/10/30MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 5042

Shared session interface contract for codespar runtimes

原始使用文档
文档目录9 节
  1. Install
  2. Interfaces
  3. SessionBase
  4. Session
  5. isCodesparSession
  6. Wire types
  7. Conformance testing
  8. Need more?
  9. License

@codespar/types

Zero-dependency TypeScript package defining the shared session interface hierarchy for CodeSpar runtimes.

Any runtime that implements SessionBase — the managed CodeSpar backend, Anthropic Managed Agents, a self-hosted server, or a test double — is a first-class citizen in the SDK ecosystem. The contract is the only coupling between runtimes and the tools that run on top of them.

Install

npm install @codespar/types

Interfaces

SessionBase

The minimal interface any runtime must implement.

import type { SessionBase } from "@codespar/types";

interface SessionBase {
  readonly id: string;
  readonly status: "active" | "closed" | "error";

  execute(toolName: string, params: Record<string, unknown>): Promise<ToolResult>;
  send(message: string): Promise<SendResult>;
  sendStream(message: string): AsyncIterable<StreamEvent>;
  connections(): Promise<BaseConnection[]>;
  close(): Promise<void>;
}

Session

The codespar-specific extension of SessionBase, used when a session was created through the managed CodeSpar API.

import type { Session } from "@codespar/types";

interface Session extends SessionBase {
  proxyExecute(request: ProxyRequest): Promise<ProxyResult>;
  authorize(serverId: string, config: AuthConfig): Promise<AuthResult>;

  // Tool discovery + connection wizard (codespar_discover / codespar_manage_connections)
  discover(useCase: string, options?: DiscoverOptions): Promise<DiscoverResult>;
  connectionWizard(options: ConnectionWizardOptions): Promise<ConnectionWizardResult>;

  // Meta-tool wrappers — typed payloads, same wire as session.execute(...)
  charge(args: ChargeArgs): Promise<ChargeResult>;
  ship(args: ShipArgs): Promise<ShipResult>;
  shop(args: ShopArgs): Promise<ShopResult>;

  // Async settlement (codespar_charge / codespar_pay)
  paymentStatus(toolCallId: string): Promise<PaymentStatusResult>;
  paymentStatusStream(
    toolCallId: string,
    options: PaymentStatusStreamOptions,
  ): Promise<PaymentStatusResult>;

  // Async verification (codespar_kyc)
  verificationStatus(toolCallId: string): Promise<VerificationStatusResult>;
  verificationStatusStream(
    toolCallId: string,
    options: VerificationStatusStreamOptions,
  ): Promise<VerificationStatusResult>;

  mcp?: {
    url: string;
    headers: Record<string, string>;
  };
}

isCodesparSession

Type guard to narrow a SessionBase to Session when you need codespar-specific methods.

import { isCodesparSession } from "@codespar/types";

function useSession(session: SessionBase) {
  if (isCodesparSession(session)) {
    // session is Session here — proxyExecute, authorize, mcp available
    const config = session.mcp;
  }
}

Wire types

TypeDescription
ToolResultReturn value of execute() — success, data, error, duration, server, tool
SendResultReturn value of send() — message, tool_calls, iterations
StreamEventUnion of events yielded by sendStream() — user_message, assistant_text, tool_use, tool_result, done, error
BaseConnectionEntry from connections() — id, connected
ServerConnectionExtended connection with name and provider metadata
ProxyRequestInput to proxyExecute() — server, endpoint, method, body, headers
ProxyResultReturn value of proxyExecute() — status, data, headers, duration, proxy_call_id
AuthConfigInput to authorize() — redirectUri, scopes?
AuthResultReturn value of authorize() — linkToken, authorizeUrl, expiresAt
DiscoverOptions / DiscoverResult / DiscoverToolMatch / DiscoverPlanStepWire shapes for session.discover(...) (codespar_discover)
ConnectionWizardOptions / ConnectionWizardResult / ConnectionStatusRowWire shapes for session.connectionWizard(...) (codespar_manage_connections)
ChargeArgs / ChargeBuyer / ChargeResultWire shapes for session.charge(...) (codespar_charge, inbound)
ShipArgs / ShipAddress / ShipItem / ShipResultWire shapes for session.ship(...) (codespar_ship)
ShopArgs / ShopSearchArgs / ShopCheckoutArgs / ShopStatusArgs / ShopOffer / ShopVariant / ShopResult / ShopSearchResult / ShopCheckoutResult / ShopStatusResult / ShopCheckoutStatusWire shapes for session.shop(...) (codespar_shop) — discriminated on action. Canonical spec: docs/codespar-shop-contract.md
PaymentStatus / PaymentStatusResult / PaymentStatusEventWire shapes for session.paymentStatus(toolCallId) — async webhook correlation
PaymentStatusStreamOptionsOptions for session.paymentStatusStream(...) — onUpdate?, signal?
VerificationStatus / VerificationStatusResult / VerificationStatusEventWire shapes for session.verificationStatus(toolCallId) — async KYC poll
VerificationStatusStreamOptionsOptions for session.verificationStatusStream(...) — onUpdate?, signal?

The typed meta-tool facades (shop(), charge(), ship(), …) require a runtime that implements the meta-tool. These wire types are the contract; the facade in @codespar/sdk is a client over execute("codespar_<tool>", args). Against a self-hosted runtime with no registered implementation for that meta-tool, the call returns Tool not registered. The types + facade are MIT and self-hostable; the implementation is registered by the runtime the SDK points at.

Conformance testing

@codespar/types ships a runContractSuite helper under a /testing subpath export. It registers a Vitest test suite that exercises the five SessionBase methods against a live HTTP endpoint.

// my-runtime.test.ts
import { runContractSuite } from "@codespar/types/testing";

// Skip unless the env vars are present
const apiKey = process.env["MY_RUNTIME_API_KEY"];
const baseUrl = process.env["MY_RUNTIME_BASE_URL"] ?? "http://localhost:3000";

if (apiKey) {
  runContractSuite(baseUrl, apiKey);
}

The suite opens a session via POST /v1/sessions with Authorization: Bearer <apiKey> and body { servers: [], user_id: "contract-suite" }. Every leg asserts the 201 body carries id, status: "active", the posted user_id, servers (a string[] that includes every posted id) and a parseable created_at — the fields @codespar/sdk builds its session from. Then it validates:

  • execute() calls a registered tool and returns a ToolResult
  • send() returns a SendResult with a message field
  • sendStream() yields well-typed StreamEvents including a done event
  • connections() answers 2xx with { servers }, each entry carrying id and connected (tools, when present, must be an array)
  • close() transitions session.status to "closed"

See docs/custom-session-runtime.md for a full implementation guide.

Need more?

Need governance, budget limits, and audit trails for agent payments? CodeSpar Enterprise adds policy engine, payment routing, and compliance templates on top of these MCP servers.

License

MIT — codespar.dev

版本与发布记录

@codespar/types 共发布 23 个版本,首次发布于 2026/04/22;过去 12 个月发布了 23 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.11.7latest2026/10/3010 天前
0.11.62026/10/3010 天前
0.11.52026/10/2516 天前
0.11.42026/09/271 个月前
0.11.32026/09/241 个月前
0.11.22026/09/231 个月前

关于 @codespar/types 的常见问题

@codespar/types 是什么?

Shared session interface contract for codespar runtimes

如何安装 @codespar/types?

在项目目录运行 npm install @codespar/types,或使用 pnpm add / yarn add / bun add。

@codespar/types 支持 TypeScript 吗?

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

@codespar/types 支持 ESM 还是 CommonJS?

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

@codespar/types 需要什么版本的 Node.js?

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

@codespar/types 可以商用吗?

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

@codespar/types 的下载量有多少?

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

@codespar/types 还在维护吗?

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