loader-composition.spec.ts 4.8 KB

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