provider-prefix.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import assert from "node:assert/strict";
  2. import { isDeepStrictEqual } from "node:util";
  3. export interface CapturedPrompt {
  4. model?: unknown;
  5. system?: unknown;
  6. tools?: unknown;
  7. messages: Array<{ role: string; content: unknown }>;
  8. }
  9. /** 所有父请求逐一检查工具配对,且相邻请求只能追加历史并保持缓存标记。 */
  10. export function assertParentRequestHistory(requests: CapturedPrompt[]): void {
  11. for (const [index, request] of requests.entries()) {
  12. assertCompleteToolHistory(request);
  13. if (index) assertProviderPrefix(requests[index - 1]!, request);
  14. else assertRequestCacheBreakpoint(request);
  15. }
  16. }
  17. function withoutCache(value: unknown): unknown {
  18. if (Array.isArray(value)) return value.map(withoutCache);
  19. if (!value || typeof value !== "object") return value;
  20. return Object.fromEntries(
  21. Object.entries(value)
  22. .filter(([key]) => key !== "cache_control")
  23. .map(([key, nested]) => [key, withoutCache(nested)]),
  24. );
  25. }
  26. /** 历史与静态工具保持稳定;异步 MCP 目录允许在普通 step 更新。 */
  27. export function assertProviderPrefix(previous: CapturedPrompt, next: CapturedPrompt): void {
  28. assert.deepEqual(next.model, previous.model, "父模型不能因 subagent 配置变化而改变");
  29. assert.deepEqual(next.system, previous.system, "父 system 及其缓存标记必须稳定");
  30. assertProviderToolContracts(previous.tools, next.tools);
  31. assertProviderMessageHistory(previous, next);
  32. assertRequestCacheBreakpoint(previous);
  33. assertRequestCacheBreakpoint(next);
  34. }
  35. export function assertProviderToolContracts(previous: unknown, next: unknown): void {
  36. const before = withoutCache(previous);
  37. const after = withoutCache(next);
  38. assert.ok(Array.isArray(before) && Array.isArray(after), "请求必须包含工具列表");
  39. // MCP 后台连接及冷恢复会改变成员;其余定义和仍可见的同名工具不能借此被改写。
  40. const isStatic = (tool: { name: string }) =>
  41. !tool.name.startsWith("mcp__") && tool.name !== "WaitForMcpServers";
  42. assert.deepEqual(after.filter(isStatic), before.filter(isStatic), "内置工具定义必须稳定");
  43. for (const tool of before) {
  44. const current: unknown = after.find((candidate) => candidate.name === tool.name);
  45. if (current) assert.deepEqual(current, tool, `已存在工具 ${tool.name} 的定义必须稳定`);
  46. }
  47. }
  48. /** child 配置变化允许 headers 改变,已有对话消息仍须完整保留。 */
  49. export function assertProviderMessageHistory(previous: CapturedPrompt, next: CapturedPrompt): void {
  50. assert.ok(next.messages.length >= previous.messages.length, "历史消息不得减少");
  51. for (const [index, before] of previous.messages.entries()) {
  52. const after = next.messages[index]!;
  53. if (isDeepStrictEqual(withoutCache(before), withoutCache(after))) continue;
  54. // 相邻 user 会被 provider 合并,只允许在原有最后一条 user 的尾部追加 block。
  55. assert.equal(index, previous.messages.length - 1, `历史消息 ${index} 被改写`);
  56. assert.equal(before.role, "user", "只有最后一条 user 可以扩展");
  57. assert.equal(after.role, "user");
  58. assert.ok(Array.isArray(before.content) && Array.isArray(after.content));
  59. assert.ok(after.content.length > before.content.length, "只能追加 user block");
  60. assert.deepEqual(
  61. withoutCache(after.content.slice(0, before.content.length)),
  62. withoutCache(before.content),
  63. );
  64. }
  65. }
  66. export function assertRequestCacheBreakpoint(request: CapturedPrompt): void {
  67. const lastIndex = request.messages.findLastIndex((message) => message.role !== "system");
  68. assert.ok(lastIndex >= 0);
  69. const content = request.messages[lastIndex]!.content;
  70. assert.ok(Array.isArray(content) && content.length > 0);
  71. assert.deepEqual(
  72. content.at(-1)?.cache_control,
  73. { type: "ephemeral" },
  74. "最后非 system 消息末尾必须保留缓存 breakpoint",
  75. );
  76. }
  77. export function assertCompleteToolHistory(request: CapturedPrompt): void {
  78. const pending = new Set<string>();
  79. for (const message of request.messages) {
  80. if (message.role === "system") assert.equal(pending.size, 0, "MCS 不得插入未完成工具批次");
  81. if (!Array.isArray(message.content)) continue;
  82. for (const block of message.content) {
  83. if (block.type === "tool_use") {
  84. assert.ok(!pending.has(block.id), "重复 tool_use");
  85. pending.add(block.id);
  86. }
  87. if (block.type === "tool_result")
  88. assert.ok(pending.delete(block.tool_use_id), "tool_result 缺少 tool_use");
  89. }
  90. }
  91. assert.equal(pending.size, 0, "provider 请求存在缺失的 tool_result");
  92. }