loader-composition.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { pathToFileURL } from 'node:url'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { Context } from 'cordis'
  7. import Loader from '@cordisjs/plugin-loader'
  8. import Include from '@cordisjs/plugin-include'
  9. import LlmService from '@deepseek-ai/dsh-llm'
  10. import TokenMeterService from '@deepseek-ai/dsh-token-meter'
  11. import BasicCompactService from '@deepseek-ai/dsh-compact-basic'
  12. import ToolResultPruneService from '@deepseek-ai/dsh-compact-tool-result-prune'
  13. let root: string | undefined
  14. let context: Context | undefined
  15. afterEach(async () => {
  16. await context?.fiber.dispose()
  17. context = undefined
  18. if (root !== undefined) await rm(root, { recursive: true, force: true })
  19. root = undefined
  20. })
  21. async function loadYaml(lines: readonly string[]): Promise<Context> {
  22. root = await mkdtemp(join(tmpdir(), 'dsh-token-meter-loader-'))
  23. const configPath = join(root, 'cordis.yml')
  24. await writeFile(configPath, [...lines, ''].join('\n'))
  25. context = new Context()
  26. context.baseUrl = pathToFileURL(root).href + '/'
  27. await context.plugin(Loader)
  28. context.loader.builtins.include = Include
  29. const modules = new Map<string, unknown>([
  30. ['@deepseek-ai/dsh-llm', LlmService],
  31. ['@deepseek-ai/dsh-token-meter', TokenMeterService],
  32. ['@deepseek-ai/dsh-compact-tool-result-prune', ToolResultPruneService],
  33. ['@deepseek-ai/dsh-compact-basic', BasicCompactService],
  34. ])
  35. context.loader.internal = {
  36. version: 'v2',
  37. async import(specifier: string) {
  38. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  39. return modules.get(specifier)
  40. },
  41. } as unknown as NonNullable<typeof context.loader.internal>
  42. await context.loader.create({
  43. name: 'cordis:include',
  44. config: { path: pathToFileURL(configPath).href },
  45. })
  46. await context.loader.await()
  47. return context
  48. }
  49. describe('real Loader composition', () => {
  50. it('loads the shipped token-meter, pruning, and compact-basic YAML order', async () => {
  51. const loaded = await loadYaml([
  52. "- name: '@deepseek-ai/dsh-llm'",
  53. "- name: '@deepseek-ai/dsh-token-meter'",
  54. "- name: '@deepseek-ai/dsh-compact-tool-result-prune'",
  55. ' config:',
  56. ' thresholdChars: 100',
  57. ' headChars: 20',
  58. ' tailChars: 10',
  59. "- name: '@deepseek-ai/dsh-compact-basic'",
  60. ' config:',
  61. ' thresholdRatio: 0.5',
  62. ' retainRatio: 0.125',
  63. ' auto: false',
  64. ])
  65. const unloaded = [...loaded.loader.entries()]
  66. .filter(entry => entry.fiber === undefined && !entry.disabled)
  67. .map(entry => entry.options.name)
  68. expect(unloaded).toEqual([])
  69. expect(loaded.get('toolResultPrune')).toBeInstanceOf(ToolResultPruneService)
  70. expect(loaded.get('compact')).toBeInstanceOf(BasicCompactService)
  71. expect((loaded.compact as BasicCompactService).config).toMatchObject({
  72. thresholdRatio: 0.5,
  73. retainRatio: 0.125,
  74. auto: false,
  75. })
  76. })
  77. it('rejects stale token-meter config after Schemastery normalization', async () => {
  78. context = new Context()
  79. await expect(context.plugin(TokenMeterService, {
  80. contextWindow: 4096,
  81. } as never)).rejects.toThrow(/TokenMeterConfig: unknown key "contextWindow"/)
  82. })
  83. it('rejects stale compact-basic config after Schemastery normalization', async () => {
  84. context = new Context()
  85. await context.plugin(LlmService)
  86. await context.plugin(TokenMeterService)
  87. await expect(context.plugin(BasicCompactService, {
  88. models: { legacy: { thresholdRatio: 0.5 } },
  89. } as never)).rejects.toThrow(/BasicCompactConfig: unknown key "models"/)
  90. })
  91. it('rejects a capacity-independent merged ratio conflict during plugin load', async () => {
  92. context = new Context()
  93. await context.plugin(LlmService)
  94. await context.plugin(TokenMeterService)
  95. await expect(context.plugin(BasicCompactService, {
  96. retainRatio: 0.2,
  97. modelPolicies: [{
  98. provider: 'test-provider',
  99. model: 'test-model',
  100. thresholdRatio: 0.1,
  101. }],
  102. })).rejects.toThrow(/modelPolicies\[0\]: retainRatio \(0.2\).*thresholdRatio \(0.1\)/)
  103. })
  104. it('rejects an incomplete model-policy summarization pair during plugin load', async () => {
  105. context = new Context()
  106. await context.plugin(LlmService)
  107. await context.plugin(TokenMeterService)
  108. await expect(context.plugin(BasicCompactService, {
  109. summarizationProvider: 'default-provider',
  110. summarizationModel: 'default-model',
  111. modelPolicies: [{
  112. provider: 'test-provider',
  113. model: 'test-model',
  114. summarizationModel: '',
  115. }],
  116. })).rejects.toThrow(/modelPolicies\[0\].*must be set together/)
  117. })
  118. })