harness.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  13. /**
  14. * Shared harness for the coding-agent e2e suites: the full plugin stack
  15. * with the real DeepSeek adapter and the real bash tool. Lives outside the
  16. * *.e2e.ts pattern so importing it never re-registers another file's tests.
  17. */
  18. export const SYSTEM_PROMPT = 'You are a coding agent. Your only tool is bash; '
  19. + 'do file operations with cat/grep/heredocs, check [exit code: N] markers, '
  20. + 'and report results briefly.'
  21. export async function codingHarness(workdir: string, persistenceRoot?: string): Promise<Context> {
  22. const ctx = new Context()
  23. await ctx.plugin(LlmService)
  24. await ctx.plugin(SessionStore)
  25. await ctx.plugin(SystemPrompt)
  26. await ctx.plugin(ToolRegistry)
  27. await ctx.plugin(AgentRegistry)
  28. await ctx.plugin(AgentLoop, { agents: [] })
  29. await ctx.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  30. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  31. await ctx.plugin(ToolBash)
  32. // Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
  33. // other suites stay file-free. Loaded last so a resume's deferred
  34. // `ctx.inject(['sessionPersistence'])` resolves once this is present.
  35. if (persistenceRoot !== undefined) await ctx.plugin(SessionPersistenceJsonl, { root: persistenceRoot })
  36. return ctx
  37. }
  38. export function waitForIdle(ctx: Context, agent: LoopAgent): Promise<void> {
  39. return new Promise((resolve) => {
  40. const dispose = ctx.on('agent/status', (subject, status) => {
  41. if (subject === agent && status === 'idle') {
  42. dispose()
  43. resolve()
  44. }
  45. })
  46. })
  47. }
  48. export function finalText(events: SessionEvent[]): string {
  49. const message = events.findLast(event => event.type === 'assistant/message')
  50. if (message?.type !== 'assistant/message') return ''
  51. return message.data.content
  52. .filter(block => block.type === 'text')
  53. .map(block => block.text)
  54. .join('')
  55. }