harness.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, { ReactLoopAgent } 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 ToolTodo from '@deepseek-ai/dsh-tool-todo'
  12. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  13. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  14. /**
  15. * Shared harness for the coding-agent e2e suites: the full plugin stack
  16. * with the real DeepSeek adapter and the real bash + todo_write tools. Lives
  17. * outside the *.e2e.ts pattern so importing it never re-registers another
  18. * file's tests.
  19. */
  20. export const SYSTEM_PROMPT = 'You are a coding agent. Use bash for file operations '
  21. + 'with cat/grep/heredocs; check [exit code: N] markers, '
  22. + 'and report results briefly.'
  23. /** System prompt for the todo_write e2e: nudges the model to plan with the tool. */
  24. export const TODO_SYSTEM_PROMPT = 'You are a coding agent. For multi-step work, '
  25. + 'use the todo_write tool to track a task list: send the WHOLE list each call, '
  26. + 'keep at most one task in_progress (exactly one while work remains), and mark '
  27. + 'a task completed as soon as it is done.'
  28. export async function codingHarness(workdir: string, persistenceRoot?: string): Promise<Context> {
  29. const ctx = new Context()
  30. await ctx.plugin(LlmService)
  31. await ctx.plugin(SessionStore)
  32. await ctx.plugin(SystemPrompt)
  33. await ctx.plugin(ToolRegistry)
  34. await ctx.plugin(AgentRegistry)
  35. await ctx.plugin(AgentLoop, { agents: [] })
  36. await ctx.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  37. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  38. await ctx.plugin(ToolBash)
  39. await ctx.plugin(ToolTodo)
  40. // Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
  41. // other suites stay file-free. Loaded last so a resume's deferred
  42. // `ctx.inject(['sessionPersistence'])` resolves once this is present.
  43. if (persistenceRoot !== undefined) await ctx.plugin(SessionPersistenceJsonl, { root: persistenceRoot })
  44. return ctx
  45. }
  46. export function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise<void> {
  47. return new Promise((resolve) => {
  48. const dispose = ctx.on('agent/status', (subject, status) => {
  49. if (subject === agent && status === 'idle') {
  50. dispose()
  51. resolve()
  52. }
  53. })
  54. })
  55. }
  56. export function finalText(events: SessionEvent[]): string {
  57. const message = events.findLast(event => event.type === 'assistant/message')
  58. if (message?.type !== 'assistant/message') return ''
  59. return message.data.content
  60. .filter(block => block.type === 'text')
  61. .map(block => block.text)
  62. .join('')
  63. }