loader-composition.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 AgentRegistry from '@deepseek-ai/dsh-agent'
  10. import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent'
  11. import CommandService from '@deepseek-ai/dsh-commands'
  12. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  13. import * as CommandFeedback from '@deepseek-ai/dsh-command-feedback'
  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. /** Register one idle agent over a store-owned session, as an app's spine does. */
  23. function agent(ctx: Context): Agent {
  24. const scope = ctx.plugin(() => {})
  25. const id = SessionId('feedback-loader-agent')
  26. const session = ctx.sessions.create(id)
  27. let status: AgentStatus = 'idle'
  28. const value: Agent = {
  29. id,
  30. options: {},
  31. session,
  32. ctx: scope.ctx,
  33. get status() { return status },
  34. get acceptsNextStep() { return status === 'running' },
  35. send: () => {},
  36. followup: () => {},
  37. steer: () => {},
  38. inject: () => {},
  39. cancel() { status = 'idle' },
  40. whenIdle: () => Promise.resolve(),
  41. }
  42. ctx.agents.register(value)
  43. return value
  44. }
  45. describe('/feedback real Loader composition through cordis.yml', () => {
  46. it('boots cordis.yml and records feedback without model-visible output', async () => {
  47. root = await mkdtemp(join(tmpdir(), 'dsh-command-feedback-loader-'))
  48. const configPath = join(root, 'cordis.yml')
  49. await writeFile(configPath, [
  50. "- name: '@deepseek-ai/dsh-agent'",
  51. "- name: '@deepseek-ai/dsh-session'",
  52. "- name: '@deepseek-ai/dsh-commands'",
  53. "- name: '@deepseek-ai/dsh-command-feedback'",
  54. '',
  55. ].join('\n'))
  56. context = new Context()
  57. context.baseUrl = pathToFileURL(root).href + '/'
  58. await context.plugin(Loader)
  59. context.loader.builtins.include = Include
  60. const modules = new Map<string, unknown>([
  61. ['@deepseek-ai/dsh-agent', AgentRegistry],
  62. ['@deepseek-ai/dsh-session', SessionStore],
  63. ['@deepseek-ai/dsh-commands', CommandService],
  64. ['@deepseek-ai/dsh-command-feedback', CommandFeedback],
  65. ])
  66. context.loader.internal = {
  67. version: 'v2',
  68. async import(specifier: string) {
  69. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  70. return modules.get(specifier)
  71. },
  72. } as unknown as NonNullable<typeof context.loader.internal>
  73. await context.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(configPath).href } })
  74. await context.loader.await()
  75. const owner = agent(context)
  76. const signal = new AbortController().signal
  77. // Discoverable through the composed registry, as a UI adapter finds it.
  78. expect(context.commands.list(owner).map(command => command.name)).toContain('feedback')
  79. const accepted = await context.commands.execute(owner, '/feedback the diff view is unreadable', signal)
  80. expect(accepted?.result).toEqual({ kind: 'success', text: 'Feedback recorded.' })
  81. const rejected = await context.commands.execute(owner, '/feedback', signal)
  82. expect(rejected?.result).toEqual({
  83. kind: 'error',
  84. text: 'Feedback text is required. Usage: /feedback <text>',
  85. })
  86. // The domain event owns the payload; generic command bookkeeping omits it.
  87. expect(owner.session.events.map(event => event.type))
  88. .toEqual(['command/run', 'feedback/record', 'command/done', 'command/run', 'command/done'])
  89. const run = owner.session.events.find(event => event.type === 'command/run')
  90. expect(run?.type === 'command/run' && Object.hasOwn(run.data, 'args')).toBe(false)
  91. const feedback = owner.session.events.find(event => event.type === 'feedback/record')
  92. expect(feedback?.type === 'feedback/record' && feedback.data.text).toBe('the diff view is unreadable')
  93. expect(JSON.stringify(owner.session.events).match(/the diff view is unreadable/gu)).toHaveLength(1)
  94. // Nothing reached the model.
  95. expect(owner.session.deriveMessages()).toEqual([])
  96. expect(owner.session.surface.nodes).toEqual([])
  97. })
  98. })