Type Safe
返回模板目录
typescript · 2026-09-23

REST Jev 人工复核门控模板

不依赖 SDK 调用 Jev System One 接口,并使用有限重试和明确的响应校验。

适用场景

  • 边缘服务
  • 小型服务端处理器
  • SDK 评估

依赖与版本

  • Node.js 20+

预期行为

请求最多尝试三次,并返回经过校验的选择结果或 human_review。

失败处理

永久性 HTTP 错误、耗尽重试预算后的超时和异常响应都会停止工作流。

代码模板
const API_URL = 'https://api.typesafe.ai/v1/systemone';
const MAX_RETRIES = 2;
const apiKey = process.env.TYPESAFE_API_KEY;

if (!apiKey) throw new Error('TYPESAFE_API_KEY is required');

const requestBody = {
  model: 'jev-latest',
  state: { ticket: 'I was charged twice. Please fix this ASAP.' },
  questions: {
    route: {
      type: 'choice',
      instructions: 'Choose the support queue.',
      criteria: { billing: null, technical: null, other: null },
    },
  },
};

const retryable = (status) =>
  status === 408 || status === 429 || status >= 500;

let response;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt += 1) {
  response = await fetch(API_URL, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${apiKey}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(requestBody),
    signal: AbortSignal.timeout(10_000),
  });

  if (response.ok || !retryable(response.status) || attempt === MAX_RETRIES) {
    break;
  }
  await new Promise((resolve) => setTimeout(resolve, 500 * 2 ** attempt));
}

if (!response?.ok) {
  throw new Error(`TypeSafe request failed with HTTP ${response?.status}`);
}

const payload = await response.json();
const decision = payload?.answers?.route;
if (
  typeof decision?.choice !== 'string' ||
  !Number.isFinite(decision?.confidence) ||
  decision.confidence < 0 ||
  decision.confidence > 1
) {
  throw new Error('Unexpected decision response');
}

const action = decision.confidence < 0.8 ? 'human_review' : decision.choice;
console.log({ action, confidence: decision.confidence, model: payload.model });

来源

示例会调用第三方服务。复制前请核对当前 API、依赖版本、费用、隐私要求和服务条款。