loader-composition.spec.ts 5.0 KB

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