harness.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { Context } from 'cordis'
  2. import LlmService from '@deepseek-ai/dsh-llm'
  3. import SessionStore from '@deepseek-ai/dsh-session'
  4. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  5. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  6. import ToolRegistry from '@deepseek-ai/dsh-tools'
  7. import AgentRegistry from '@deepseek-ai/dsh-agent'
  8. import AgentLoop, { LoopAgent } from '@deepseek-ai/dsh-agent-loop'
  9. import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
  10. import * as ToolBash from '@deepseek-ai/dsh-tool-bash'
  11. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  12. /**
  13. * Shared harness for the coding-agent e2e suites: the full plugin stack
  14. * with the real DeepSeek adapter and the real bash tool. Lives outside the
  15. * *.e2e.ts pattern so importing it never re-registers another file's tests.
  16. */
  17. export const SYSTEM_PROMPT = 'You are a coding agent. Your only tool is bash; '
  18. + 'do file operations with cat/grep/heredocs, check [exit code: N] markers, '
  19. + 'and report results briefly.'
  20. export async function codingHarness(workdir: string): Promise<Context> {
  21. const ctx = new Context()
  22. await ctx.plugin(LlmService)
  23. await ctx.plugin(SessionStore)
  24. await ctx.plugin(SystemPrompt)
  25. await ctx.plugin(ToolRegistry)
  26. await ctx.plugin(AgentRegistry)
  27. await ctx.plugin(AgentLoop, { agents: [] })
  28. await ctx.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  29. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  30. await ctx.plugin(ToolBash)
  31. return ctx
  32. }
  33. export function waitForIdle(ctx: Context, agent: LoopAgent): Promise<void> {
  34. return new Promise((resolve) => {
  35. const dispose = ctx.on('agent/status', (subject, status) => {
  36. if (subject === agent && status === 'idle') {
  37. dispose()
  38. resolve()
  39. }
  40. })
  41. })
  42. }
  43. export function finalText(events: SessionEvent[]): string {
  44. const message = events.findLast(event => event.type === 'assistant/message')
  45. if (message?.type !== 'assistant/message') return ''
  46. return message.data.content
  47. .filter(block => block.type === 'text')
  48. .map(block => block.text)
  49. .join('')
  50. }