token-counter-adapter-routing.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, expect, it } from 'vitest';
  2. import { resolveRemoteTokenCounterAdapter } from '../../src/context/token-counter-adapters/registry.js';
  3. import { buildResponsesInputTokensUrl } from '../../src/context/token-counter-adapters/responses.js';
  4. import type { RemoteTokenCountContext } from '../../src/context/token-counter-adapters/types.js';
  5. function makeContext(baseUrl: string, api = 'openai-completions'): RemoteTokenCountContext {
  6. return {
  7. model: { id: 'glm-5.3', api, provider: 'custom_provider', baseUrl, input: ['text'] },
  8. apiKey: 'sk-test',
  9. systemPrompt: 'hi',
  10. messages: [],
  11. tools: [],
  12. } as unknown as RemoteTokenCountContext;
  13. }
  14. describe('remote token counter adapter routing (issue #258)', () => {
  15. it('routes Zhipu general API-plan base URLs to the zhipu tokenizer', () => {
  16. for (const baseUrl of [
  17. 'https://api.z.ai/api/paas/v4',
  18. 'https://open.bigmodel.cn/api/paas/v4/chat/completions',
  19. ]) {
  20. const ctx = makeContext(baseUrl);
  21. const adapter = resolveRemoteTokenCounterAdapter(ctx);
  22. expect(adapter?.id).toBe('zhipu-tokenizer');
  23. expect(adapter?.buildRequest(ctx)?.url).toMatch(/\/api\/paas\/v4\/tokenizer$/u);
  24. }
  25. });
  26. it('routes Zhipu coding-plan base URLs to the zhipu tokenizer on the coding-plan root', () => {
  27. for (const host of ['api.z.ai', 'open.bigmodel.cn']) {
  28. for (const path of ['/api/coding/paas/v4', '/api/coding/paas/v4/chat/completions']) {
  29. const ctx = makeContext(`https://${host}${path}`);
  30. const adapter = resolveRemoteTokenCounterAdapter(ctx);
  31. expect(adapter?.id).toBe('zhipu-tokenizer');
  32. expect(adapter?.buildRequest(ctx)?.url).toBe(
  33. `https://${host}/api/coding/paas/v4/tokenizer`,
  34. );
  35. }
  36. }
  37. });
  38. it('routes Kimi base URLs to the kimi estimator', () => {
  39. const adapter = resolveRemoteTokenCounterAdapter(makeContext('https://api.moonshot.cn/v1'));
  40. expect(adapter?.id).toBe('kimi-estimate-token-count');
  41. });
  42. it('keeps unrelated OpenAI-compatible providers on generic-responses', () => {
  43. const adapter = resolveRemoteTokenCounterAdapter(
  44. makeContext('https://gateway.example.com/v1'),
  45. );
  46. expect(adapter?.id).toBe('generic-responses');
  47. });
  48. });
  49. describe('buildResponsesInputTokensUrl version handling (issue #258)', () => {
  50. it('keeps /v1 base URLs unchanged', () => {
  51. expect(buildResponsesInputTokensUrl('https://api.example.com/v1')).toBe(
  52. 'https://api.example.com/v1/responses/input_tokens',
  53. );
  54. });
  55. it('appends /v1 when the base URL has no version segment', () => {
  56. expect(buildResponsesInputTokensUrl('https://api.example.com')).toBe(
  57. 'https://api.example.com/v1/responses/input_tokens',
  58. );
  59. expect(buildResponsesInputTokensUrl('https://api.example.com/openai')).toBe(
  60. 'https://api.example.com/openai/v1/responses/input_tokens',
  61. );
  62. });
  63. it('does not double the version for base URLs already ending in a version segment', () => {
  64. expect(buildResponsesInputTokensUrl('https://api.z.ai/api/coding/paas/v4')).toBe(
  65. 'https://api.z.ai/api/coding/paas/v4/responses/input_tokens',
  66. );
  67. expect(buildResponsesInputTokensUrl('https://gateway.example.com/v2')).toBe(
  68. 'https://gateway.example.com/v2/responses/input_tokens',
  69. );
  70. });
  71. it('strips chat/completions and responses suffixes before resolving the root', () => {
  72. expect(buildResponsesInputTokensUrl('https://api.example.com/v1/chat/completions')).toBe(
  73. 'https://api.example.com/v1/responses/input_tokens',
  74. );
  75. expect(
  76. buildResponsesInputTokensUrl('https://open.bigmodel.cn/api/paas/v4/chat/completions'),
  77. ).toBe('https://open.bigmodel.cn/api/paas/v4/responses/input_tokens');
  78. expect(buildResponsesInputTokensUrl('https://api.example.com/v1/responses')).toBe(
  79. 'https://api.example.com/v1/responses/input_tokens',
  80. );
  81. });
  82. it('keeps the MiniMax anthropic-compat stripping behavior stable', () => {
  83. expect(
  84. buildResponsesInputTokensUrl('https://api.minimaxi.com/anthropic', {
  85. stripMessagesCompatibilityPrefix: true,
  86. }),
  87. ).toBe('https://api.minimaxi.com/v1/responses/input_tokens');
  88. });
  89. it('falls back to string handling for non-URL base values', () => {
  90. expect(buildResponsesInputTokensUrl('not a url/api/paas/v4')).toBe(
  91. 'not a url/api/paas/v4/responses/input_tokens',
  92. );
  93. });
  94. });