npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

factual-fox-sdk

v2.2.0

Published

Factual Fox SDK for metadata queries, business writes, single-patient profiles, and formal business dialogue replies

Readme

factual-fox-sdk

factual-fox-sdk 是面向业务后端 Node.js 服务的 Factual Fox SDK。

它当前提供三类正式能力:

  1. 查询标签定义与单患者画像
  2. 发起业务写入:ingestingestBatchdirectUpsertTrait
  3. 发起正式业务对话回复生成

不提供以下能力:

  • provider/config 管理
  • debug session / debug extraction / debug stream
  • reset、delete、replay 等运维或后台控制操作
  • scene 配置管理

安装

npm install factual-fox-sdk

运行环境建议:

  • Node.js 18+

说明:

  • SDK 使用 fetchReadableStream
  • 流式能力面向业务后端 Node.js 服务调用,不直接给浏览器前端使用

初始化

import { FactualFoxClient } from 'factual-fox-sdk';

const foxClient = new FactualFoxClient({
  baseURL: 'http://factual-fox-api:3040'
});

也可以通过环境变量初始化:

export FACTUAL_FOX_BASE_URL=http://factual-fox-api:3040
const foxClient = new FactualFoxClient();

API

listTraits(options?)

查询标签定义列表。

const response = await foxClient.listTraits({
  trait_kind: 'base',
  dimension: 'clinical',
  include_inactive: false
});

getTrait(traitKey)

查询单个标签定义。

const trait = await foxClient.getTrait('patient_level');

getPatientProfile(patientId, options?)

查询单个患者画像 JSON。

const profile = await foxClient.getPatientProfile('patient-001', {
  includeEvidence: true
});

ingest(request, idempotencyKey?)

提交一条证据并触发异步抽取。

const accepted = await foxClient.ingest(
  {
    patient_id: 'patient-001',
    source_type: 'followup',
    source_ref: 'followup-001',
    system_code: 'crm',
    event_time: '2026-03-29T08:00:00.000Z',
    raw_content: '患者反馈最近三天餐后血糖偏高。'
  },
  'idem-ingest-001'
);

ingestBatch(request)

批量提交多条证据。

const acceptedBatch = await foxClient.ingestBatch({
  items: [
    {
      patient_id: 'patient-001',
      source_type: 'followup',
      source_ref: 'followup-001',
      system_code: 'crm',
      event_time: '2026-03-29T08:00:00.000Z',
      raw_content: '患者反馈最近三天餐后血糖偏高。'
    }
  ]
});

directUpsertTrait(request, idempotencyKey?)

从可信业务系统直接写入基础标签。

推荐使用当前服务端 contract 中的 trait_code 字段:

const writeResult = await foxClient.directUpsertTrait(
  {
    patient_id: 'patient-001',
    source_type: 'crm',
    source_ref: 'visit-001',
    system_code: 'crm',
    event_time: '2026-03-29T08:00:00.000Z',
    traits: [
      { trait_code: 'patient_level', trait_value: 'tier_1' }
    ]
  },
  'idem-write-001'
);

appendTraits(request, idempotencyKey?)

appendTraits 作为历史兼容别名保留,内部行为等同于 directUpsertTrait

await foxClient.appendTraits({
  patient_id: 'patient-001',
  source_type: 'crm',
  source_ref: 'visit-001',
  system_code: 'crm',
  event_time: '2026-03-29T08:00:00.000Z',
  traits: [
    { trait_code: 'patient_level', trait_value: 'tier_1' }
  ]
});

generateDialogueReply(request)

按正式业务接口获取一次性完整回复。

const reply = await foxClient.generateDialogueReply({
  patient_id: 'patient-001',
  scene_code: 'followup_general',
  request_timestamp: '2026-03-23T10:30:00.000Z',
  caller_id: 'caregiver-001'
});

请求字段:

  • patient_id: 必填,业务患者 ID
  • scene_code: 必填,factual-fox 后台维护的 dialogue route key(不是业务方管理的 scene 资源 ID,也不是直接 agent ID)
  • request_timestamp: 必填,同一 patient_id + scene_code + request_timestamp 视为同一次回复请求
  • caller_id: 选填,用于统计调用方使用量
  • force_regenerate: 选填,默认 false,为 true 时跳过复用逻辑

streamDialogueReply(request)

以事件流方式获取回复,返回 AsyncIterable,适合业务后端直接 for await 消费。

for await (const event of foxClient.streamDialogueReply({
  patient_id: 'patient-001',
  scene_code: 'followup_general',
  request_timestamp: '2026-03-23T10:30:00.000Z'
})) {
  if (event.type === 'reply.delta' && event.field === 'content') {
    process.stdout.write(event.delta ?? '');
  }
}

对话能力边界

正式业务对话路径里,业务系统不需要自己管理 route 绑定的 agent/config、thinking level、prompt 拼装或 streaming 策略,这些都由 factual-fox 后台维护。

CLI

安装后会附带 factual-fox-sync 命令,用于同步基础标签定义。

npx factual-fox-sync http://factual-fox-api:3040

factual-fox-sync 只负责标签定义同步,不负责写入、provider/config、debug 或 reset。

错误处理

SDK 在 HTTP 非 2xx 时会直接抛错,错误信息中包含状态码和响应文本。

业务写接口在以下情况下也会直接在本地抛错:

  • 缺少 system_code
  • directUpsertTrait / appendTraits 既没有单标签字段,也没有非空 traits

正式业务对话常见失败原因:

  • scene_code 对应的 dialogue route key 不存在
  • scene_code 对应的 dialogue route key 已禁用
  • route 不允许流式回复但调用了流式接口
  • route 绑定的 dialogue agent 不可用

说明

如果需要项目内更完整的接入说明,可同时参考仓库文档:

  • docs/sdk-usage-guide.md