跳转到主要内容

Documentation Index

Fetch the complete documentation index at: https://docs.qoder.com/llms.txt

Use this file to discover all available pages before exploring further.

query() 需要认证配置才能启动会话。推荐使用 Personal Access Token (PAT) 并通过环境变量注入,适合脚本、CI 和宿主应用集成。 如果你已经在本机通过 qodercli 登录,也可以复用本机登录态。

获取 PAT

qoder.com/account/integrations 生成一个 Personal Access Token:
  1. 登录 Qoder 账号
  2. 打开 Account → Integrations 页面
  3. 创建新的 PAT,按需选择有效期和权限范围
  4. 生成后立即复制——页面关闭后无法再次查看,丢失只能重新生成
同一个账号可以生成多个 PAT,建议给不同环境(本地脚本、CI、生产服务)发独立的 token,方便单独吊销。

推荐用法

从默认环境变量读取 PAT

默认环境变量名是 QODER_PERSONAL_ACCESS_TOKEN
export QODER_PERSONAL_ACCESS_TOKEN="<your-qoder-personal-access-token>"
node agent.mjs
import { accessTokenFromEnv, query } from '@qoder-ai/qoder-agent-sdk';

const q = query({
  prompt: 'Summarize the purpose of this project in one sentence.',
  options: {
    auth: accessTokenFromEnv(),
  },
});

try {
  for await (const message of q) {
    if (message.type === 'result') {
      console.log(message.subtype);
    }
  }
} finally {
  await q.close?.();
}

从自定义环境变量读取 PAT

import { accessTokenFromEnv, query } from '@qoder-ai/qoder-agent-sdk';

const q = query({
  prompt: 'Check whether the README contains installation steps.',
  options: {
    auth: accessTokenFromEnv('MY_QODER_PAT'),
  },
});
等价的显式写法:
auth: {
  type: 'accessToken',
  accessToken: { envVar: 'MY_QODER_PAT' },
}
如果同时设置了 options.envprocess.env 中的同名变量,SDK 会优先读取 options.env 中的值。

直接传入 PAT

如果宿主应用已经从密钥管理服务、登录态或后端接口拿到了 PAT,可以直接传入。不要把 token 字面量写进源码。
import { accessToken, query } from '@qoder-ai/qoder-agent-sdk';

const token = await readTokenFromSecretManager();

const q = query({
  prompt: 'List the most recently modified files.',
  options: {
    auth: accessToken(token),
  },
});

复用 qodercli 登录态

如果本机已经通过 qodercli 完成登录,可以让 SDK 交给 CLI 读取本地登录态。该方式适合交互式本机环境,不建议用于无状态 CI。
import { qodercliAuth, query } from '@qoder-ai/qoder-agent-sdk';

const q = query({
  prompt: 'Summarize the current workspace.',
  options: {
    auth: qodercliAuth(),
  },
});

API 速览

选项用途
accessTokenFromEnv()QODER_PERSONAL_ACCESS_TOKEN 读取 PAT。
accessTokenFromEnv(envVar)从指定环境变量读取 PAT。
accessToken(token)使用调用方已经取得的 PAT。
qodercliAuth()复用本机 qodercli 登录状态。
onAuthExpiredtoken 失效、认证失败或 CLI 认证错误退出时回调。每个 query 会话最多触发一次。

错误处理

缺少 auth 配置

query() 会在启动前抛出 auth_not_configured
try {
  query({ prompt: 'hello' });
} catch (error) {
  if (error?.code === 'auth_not_configured') {
    console.error('Please configure options.auth');
  }
}

环境变量未设置

当使用 { envVar } 但变量为空时,SDK 会抛出 auth_access_token_env_var_not_configured
try {
  const q = query({
    prompt: 'hello',
    options: {
      auth: accessTokenFromEnv('MY_QODER_PAT'),
    },
  });

  for await (const _message of q) {
    // Initialization failures do not reach this loop.
  }
} catch (error) {
  if (error?.code === 'auth_access_token_env_var_not_configured') {
    console.error('MY_QODER_PAT is not set');
  }
}

运行中认证失败

远端拒绝 token、token 过期或 CLI 以认证错误退出时,使用 onAuthExpired 触发重新登录或换 token 流程:
const q = query({
  prompt: 'Continue with the current task.',
  options: {
    auth: accessTokenFromEnv(),
    onAuthExpired() {
      showSignInRequired();
    },
  },
});

for await (const message of q) {
  if (message.type === 'result' && message.subtype !== 'success') {
    console.error(message.errors?.join('\n') ?? message.subtype);
  }
}
SDK 不会自动刷新 PAT。宿主应用应在拿到新 token 后创建新的 query() 会话并传入新的 auth 配置。

最佳实践

  • 生产和 CI 中优先使用环境变量或密钥管理服务,不要硬编码 token。
  • 不要把 token 写入日志、错误对象或调试输出。
  • 自动化环境中建议使用 PAT,不建议依赖本机 qodercli 登录态。
  • 对用户可见应用注册 onAuthExpired,把认证失败转成明确的登录提示。
  • 需要轮换 token 时,新建 query 会话;不要复用已经认证失败的会话。