harness.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Context } from 'cordis'
  2. import type { Agent } from '@deepseek-ai/dsh-agent'
  3. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  4. import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
  5. import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
  6. import LocalSubprocessService from '@deepseek-ai/dsh-subprocess-local'
  7. import * as ToolBash from '@deepseek-ai/dsh-tool-bash'
  8. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  9. import SubagentService from '@deepseek-ai/dsh-subagent'
  10. import * as Spawn from '../src/index.ts'
  11. import * as ToolSubagent from '@deepseek-ai/dsh-tool-subagent'
  12. /**
  13. * Shared harness for the spawn-backend e2e: the full real stack (DeepSeek
  14. * adapter + real bash tool + the subagent tool bound to the spawn backend), so
  15. * a real parent agent can delegate to a real in-process child that does real
  16. * work (writes a file). Lives outside the *.e2e.ts pattern so importing it never
  17. * re-registers another file's tests.
  18. */
  19. export async function spawnHarness(workdir: string): Promise<Context> {
  20. const ctx = new Context()
  21. // This harness installs only the global default persona, so both parent and
  22. // spawned children render it. It stays neutral for both roles; the
  23. // delegation nudge lives in the e2e's user prompt and the subagent tool's
  24. // own description.
  25. await mountAgentLoopTestDependencies(ctx, {
  26. systemPrompt: { persona: 'You are a coding agent. Report only when the requested work is done.' },
  27. })
  28. await ctx.plugin(AgentLoop, { agents: [] })
  29. await ctx.plugin(LlmDeepSeek)
  30. await ctx.plugin(LocalSubprocessService)
  31. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  32. await ctx.plugin(ToolBash)
  33. await ctx.plugin(SubagentService)
  34. await ctx.plugin(Spawn, { providerName: 'spawn' })
  35. // The model-facing subagent tool, bound to the spawn backend.
  36. await ctx.plugin(ToolSubagent, { provider: 'spawn' })
  37. return ctx
  38. }
  39. export function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  40. return new Promise((resolve) => {
  41. const dispose = ctx.on('agent/status', (subject, status) => {
  42. if (subject === agent && status === 'idle') {
  43. dispose()
  44. resolve()
  45. }
  46. })
  47. })
  48. }