<!-- 来源: https://hostanagent.com/docs/integration/javascript/ | 协议: 1.0 | 更新: 2026-09-30 -->

# JavaScript / TypeScript Client Core

`@hostanagent/client-core@0.2.5` 的包根不依赖 DOM，可在浏览器消费产品代理；`/bridge` 是 Node 端产品代理入口。Node 需要 22.12+。使用前先完成[产品身份和受限 Key](/docs/integration/backend-auth/)。

```bash
npm install --save-exact @hostanagent/client-core@0.2.5
```

浏览器只访问自己的同源 `/api/agent`。发布 Agent 并接好产品代理后：

```ts
import { AgentClient, bootstrap } from '@hostanagent/client-core';

const metadata = await bootstrap('/api/agent');
const client = new AgentClient({
  endpoint: '/api/agent',
  agentVersion: '替换为已发布的固定版本 ID',
  identityId: metadata.identity_id,
});
client.subscribe(state => {
  // 渲染 state.run.messages；这里只观察状态，不执行产品工具。
});
await client.send('整理我的任务', {
  reasoning_effort: 'high',
});
```

发送前从模型目录确认 `high` 是否支持，检查账号可用余额。请求失联后 Client Core 保留原发送参数与幂等键；同内容重试先核对旧 Run。`client.connect(runId)` 观察原 Run，`client.restore(sessionId)` 使用旧 Session 的固定版本。收起或卸载视图不停止 Run；需要明确停止才调用 `client.cancel()`。[恢复详情](/docs/integration/recovery/)。

Node 产品代理可用 `@hostanagent/client-core/bridge` 的 `createProductBridge`；产品要提供 `authenticate`、`authorizeAgent`，业务工具再额外提供 `authorizeTool` 和 `authorizeExecutor`。[产品身份与代理](/docs/integration/backend-auth/)直接给出完整代理工厂代码和回调形状，无需访问源码仓库。身份回调必须接入产品真实登录验证。

如需收紧本轮输入窗口，可给 `send` 传 `limits: { max_context_tokens: 128000 }`，且不能超过 Agent 发布配置。工具的 `max_result_context_bytes` 在工具集配置，不是 `send` 参数。已发布 0.2.5 的配置类型仍含已删除字段、尚无新字节字段；使用当前 OpenAPI、控制台或 MCP 配置，不能依赖旧类型生成旧字段。[版本与迁移](/docs/releases/compatibility/)。
