tool-search.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { TOOL_SEARCH_BETA, type Model, type ToolSearchCapability } from "@zcode/contracts";
  2. const ANTHROPIC_MESSAGES_API_TYPE = "anthropic-messages";
  3. export function resolveToolSearchCapability(
  4. apiType: string,
  5. access: { type: string; mode?: string },
  6. ): ToolSearchCapability {
  7. const protocolSupported = apiType === ANTHROPIC_MESSAGES_API_TYPE;
  8. const codingPlan =
  9. access.type === "zhipu-coding-plan-api-key" ||
  10. (access.type === "zhipu-account" &&
  11. (access.mode === "individual-coding-plan" || access.mode === "team-coding-plan"));
  12. return Object.freeze({ protocolSupported, defaultEnabled: protocolSupported && codingPlan });
  13. }
  14. export function toolSearchHeaders(headers: Record<string, string> | undefined, enabled?: boolean) {
  15. if (!enabled) return headers;
  16. const result = { ...headers };
  17. const key =
  18. Object.keys(result).find((name) => name.toLowerCase() === "anthropic-beta") ?? "anthropic-beta";
  19. result[key] = [
  20. ...new Set([
  21. ...(result[key]
  22. ?.split(",")
  23. .map((value) => value.trim())
  24. .filter(Boolean) ?? []),
  25. TOOL_SEARCH_BETA,
  26. ]),
  27. ].join(",");
  28. return result;
  29. }
  30. /** 模型请求能力只由协议与接入方式决定;core 投影据此选择请求形态。 */
  31. export function resolveModelRequestCapabilities(
  32. apiType: string,
  33. access: { type: string; mode?: string },
  34. ): NonNullable<Model["requestCapabilities"]> {
  35. return {
  36. toolSearch: resolveToolSearchCapability(apiType, access),
  37. // 只在 Anthropic Messages 协议把提醒并入 tool result;OpenAI 兼容协议保持独立的 user 消息。
  38. foldRemindersIntoToolResults: apiType === ANTHROPIC_MESSAGES_API_TYPE,
  39. // Anthropic Messages 的请求不带没有可见内容(只有思考和空白文字)的回复,由 core 投影第一步删除。
  40. dropReasoningOnlyOrBlankAssistants: apiType === ANTHROPIC_MESSAGES_API_TYPE,
  41. };
  42. }