loader-composition.spec.ts 5.4 KB

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