harness.ts 2.3 KB

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