harness.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Your only tool is bash; '
  21. + 'do file operations 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 exactly one task in_progress, and mark a task completed as soon as it is done.'
  27. export async function codingHarness(workdir: string, persistenceRoot?: string): Promise<Context> {
  28. const ctx = new Context()
  29. await ctx.plugin(LlmService)
  30. await ctx.plugin(SessionStore)
  31. await ctx.plugin(SystemPrompt)
  32. await ctx.plugin(ToolRegistry)
  33. await ctx.plugin(AgentRegistry)
  34. await ctx.plugin(AgentLoop, { agents: [] })
  35. await ctx.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  36. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  37. await ctx.plugin(ToolBash)
  38. await ctx.plugin(ToolTodo)
  39. // Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
  40. // other suites stay file-free. Loaded last so a resume's deferred
  41. // `ctx.inject(['sessionPersistence'])` resolves once this is present.
  42. if (persistenceRoot !== undefined) await ctx.plugin(SessionPersistenceJsonl, { root: persistenceRoot })
  43. return ctx
  44. }
  45. export function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise<void> {
  46. return new Promise((resolve) => {
  47. const dispose = ctx.on('agent/status', (subject, status) => {
  48. if (subject === agent && status === 'idle') {
  49. dispose()
  50. resolve()
  51. }
  52. })
  53. })
  54. }
  55. export function finalText(events: SessionEvent[]): string {
  56. const message = events.findLast(event => event.type === 'assistant/message')
  57. if (message?.type !== 'assistant/message') return ''
  58. return message.data.content
  59. .filter(block => block.type === 'text')
  60. .map(block => block.text)
  61. .join('')
  62. }