@chalk-ai/client

中文文档
1.31.13发布于 2026/08/06Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 1万

Chark 的脚本客户端

中文使用文档
文档目录9 节
  1. 安装
  2. 生成类型
  3. 使用量
  4. 现代 JavaScript ES6
  5. 常见JS
  6. 构造器选项( gRPC)
  7. 迁移到 gRPC 客户端
  8. 支持的环境变量
  9. 构造器选项( Legacy)

@ chalk-ai/ 客户端

npm version CI

Chalk的TypeScript客户端.

安装

$ yarn add @chalk-ai/client

生成类型

从您的 Chark 项目目录开始, 运行 :

$ chalk codegen typescript --out=generated_types.ts

使用量

现代 JavaScript ES6

import { ChalkGRPCClient } from "@chalk-ai/client"
// import { ChalkClient } from "@chalk-ai/client";

// Import your generated types (recommended)
import { FeaturesType } from "local/generated_types";

// Alternatively, define features you want to pull using the client
// as an interface with the feature type and feature name.
interface FeaturesType {
  "user.id": string;
  "user.fraud_score": number;
}

const client = new ChalkGRPCClient<FeaturesType>();

const result = await client.query({
  inputs: {
    "user.id": "1",
  },
  outputs: ["user.fraud_score"],
});

// The property `.data` has auto-complete based on the
// list provided in `output` above. So if you try to pull
// a feature that wasn't requested, you will see an error
// in type checking.
console.log(result.data["user.fraud_score"].value);

常见JS

var ChalkGRPCClient = require("@chalk-ai/client").ChalkGRPCClient;
// var ChalkClient = require("@chalk-ai/client").ChalkClient; 

interface FeaturesType {
  "user.id": string;
  "user.fraud_score": number;
}

var client = new ChalkGRPCClient<FeaturesType>();

client
  .query({
    inputs: {
      "user.id": "1",
    },
    outputs: ["user.fraud_score"],
  })
  .then((result) => {
    console.log(result.data["user.fraud_score"].value);
  });

构造器选项( gRPC)

见 hTTP 选项 如果您正在使用 HTTP 客户端, 但我们鼓励 读取 移徙指南。 。 。 .

import { ChalkGRPCClient } from "@chalk-ai/client"

const options: ChalkGRPCClientOpts = {/* ... */ };
const chalkClient = new ChalkGRPCClient(options);

export interface ChalkGRPCClientOpts {
  /**
   * Your Chalk Client ID. This value will be read from the _CHALK_CLIENT_ID environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientId?: string;

  /**
   * Your Chalk Client Secret. This value will be read from the _CHALK_CLIENT_SECRET environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientSecret?: string;

  /**
   * The URL of your chalk API server. Defaults to https://api.chalk.ai
   */
  apiServer?: string;

  /**
   * The environment that your client will run against. This value will be read from the _CHALK_ACTIVE_ENVIRONMENT environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  activeEnvironment?: string;

  /**
   * A custom fetch client that will replace the fetch polyfill used by default. Used by both the HTTP and gRPC
   * clients - the HTTP client for all calls, and the gRPC client for fetching credentials from the API server.
   *
   * If not provided, the client will use the default fetch polyfill (native fetch with node-fetch as a fallback).
   */
  fetch?: CustomFetchClient;

  /**
   * A custom fetch headers object that will replace the fetch Headers polyfill used by default. This is primarily for use
   * with a custom fetch client, and is not the preferred way to add additional headers to requests.
   *
   * If not provided, the client will use the default fetch Headers polyfill (native fetch with node-fetch as a fallback).
   */
  fetchHeaders?: typeof Headers;

  /**
   * The format to use for date-type data.
   *
   * Defaults to "ISO_8601" (in UTC), also supports "EPOCH_MILLIS" as number of milliseconds since epoch
   */
  timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;

  /**
   * By default, the client will try to directly connect to the query client using the metadata provided during
   * credentials exchange, which returns a mapping between environment and query server. If this option is
   * truthy, the client will skip this step.
   *
   * The queryServer option will take precedence over this option (i.e. this option is essentially useless).
   * *
   * Defaults to false.
   */
  skipQueryServerFromCredentialExchange?: boolean;

  /**
   * Passed to the internal grpc client upon initialization. Under the hood, Chalk uses the @grpc/grpc-js library
   * and this allows users finer control of their gRPC usage.
   *
   * See https://grpc.github.io/grpc/node/grpc.Client.html for more information on what is supported here.
   */
  grpcClientOptions?: Partial<ClientOptions>;
}

迁移到 gRPC 客户端

Chalk既支持HTTP客户端,也支持gRPC客户端与Chark接口,但我们大力鼓励用户使用 gRPC 客户端, 以更好的性能, 因为它的目标是一个更能表现的 gRPC 查询服务器 。 成立GRPC后 在您的 Chark 环境中查询服务器, 更改为 gRPC 客户端可以在几个步骤中完成 :

  • 使用导入 ChalkGRPCClient 改为 ChalkClient
  • 检查是否需要更改以下初始化选项 :
    • 选项 useQueryServerFromCredentialExchange 已改为 skipQueryServerFromCredentialExchange 财务报告和审定财务报表 已否定 要更好地反映gRPC查询服务器的默认行为.
    • 检查您的 Chark 环境, 查看任何自定义的路由规则 。 如果您不确定, 请向 Chalk 求救 .
      • 如果您的前一个 SDK 客户端明确设置 engine-type: engine 请求时的标题, 需要更改 改为 engine-grpc (或省略,gRPC客户端会自动设置此头但会尊重所有覆盖).
      • 很可能没有必要更改规定 QueryServer 因为大多数路由是通过 SDK 设置头来完成的,但是 取决于您的设置, 如果使用非标准端口, 这可能需要直接指定 - gRPC 查询服务器监听 在端口时 443 默认.
    • 没错 没有 需要删除 fetch 或者说 fetchHeaders 因为这些仍在一些 HTTP 常规中使用 例如获取证书.

初始化修改后,无需更改实际呼叫 - gRPC 客户端使用 相同的函数签名.

此时此刻 query(), (中文(简体) ), queryBulk(),以及 multiQuery() 支持 - 如果您的用法需要其中之一 函数whoami(), (中文(简体) ), triggerResolverRun(), (中文(简体) ), uploadSingle(), (中文(简体) ), getRunStatus()),遗产 ChalkClient 应使用 我们仍强烈建议将部分查询电话转移到gRPC客户端.

支持的环境变量

变量仁爱说明
process.env._CHALK_CLIENT_ID需求你的粉笔客户身份证 您必须指定此环境变量或通过一个明确 clientId 构建 CharkClient 时的值
process.env._CHALK_CLIENT_SECRET需求你的粉笔客户秘密 您必须指定此环境变量或通过一个明确 clientSecret 构建 CharkClient 时的值
process.env._CHALK_ACTIVE_ENVIRONMENT可选您的客户端应该连接到的环境 。 如果未指定, 客户端会查询您的项目的默认环境
process.env._CHALK_API_SERVER可选客户端将与之通信的API服务器. 此默认为 https://api.calk.ai (英语) 对大多数消费者来说都足够

您可以通过 Chark 客户端找到要使用的相关变量 运行 chalk config 带有 Chark 命令行工具.

$ chalk config

Path:          ~~~
Name:          Organization Token
Client Id:     <client-id>
Client Secret: <client-secret>
Environment:   <active-environment>
API Server:    https://api.chalk.ai
Valid Until:   2023-11-10T06:11:17.516000

构造器选项( Legacy)

import { ChalkCLient } from "@chalk-ai/client"

const options: ChalkClientOpts = {/* ... */};
const chalkClient = new ChalkClient(opts);

export interface ChalkClientOpts {
  /**
   * Your Chalk Client ID. This value will be read from the _CHALK_CLIENT_ID environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientId?: string;

  /**
   * Your Chalk Client Secret. This value will be read from the _CHALK_CLIENT_SECRET environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  clientSecret?: string;

  /**
   * The URL of your chalk API server. Defaults to https://api.chalk.ai
   */
  apiServer?: string;

  /**
   * The environment that your client will run against. This value will be read from the _CHALK_ACTIVE_ENVIRONMENT environment variable if not set explicitly.
   *
   * If not specified and unset by your environment, an error will be thrown on client creation
   */
  activeEnvironment?: string;

  /**
   * A custom fetch client that will replace the fetch polyfill used by default.
   *
   * If not provided, the client will use the default fetch polyfill (native fetch with node-fetch as a fallback).
   */
  fetch?: CustomFetchClient;

  /**
   * A custom fetch headers object that will replace the fetch Headers polyfill used by default.
   *
   * If not provided, the client will use the default fetch Headers polyfill (native fetch with node-fetch as a fallback).
   */
  fetchHeaders?: typeof Headers;

  /**
   * The format to use for date-type data.
   *
   * Defaults to "ISO_8601" (in UTC), also supports "EPOCH_MILLIS" as number of milliseconds since epoch
   */
  timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;

  /**
   * If true, will try to directly connect to the query client using the metadata provided during
   * credentials exchange, which returns a mapping between environment and query server.
   *
   * The queryServer option will take precedence over this option (i.e. this option is essentially useless).
   *
   * Defaults to false, the legacy behavior of this client. This will change at the next major release.
   */
  useQueryServerFromCredentialExchange?: boolean;
}

版本与发布记录

@chalk-ai/client 共发布 47 个版本,首次发布于 2022/11/11;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.31.13latest2026/08/063 个月前
1.31.122026/03/108 个月前
1.31.82025/07/311.3 年前
1.31.72025/07/251.3 年前
1.31.62025/07/231.3 年前
1.31.42025/07/111.3 年前

关于 @chalk-ai/client 的常见问题

@chalk-ai/client 是什么?

Chark 的脚本客户端

如何安装 @chalk-ai/client?

在项目目录运行 npm install @chalk-ai/client,或使用 pnpm add / yarn add / bun add。

@chalk-ai/client 支持 TypeScript 吗?

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

@chalk-ai/client 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@chalk-ai/client 可以商用吗?

@chalk-ai/client 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@chalk-ai/client 的下载量有多少?

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

@chalk-ai/client 还在维护吗?

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