workflow-worker-thread.e2e.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { SessionId } from '@deepseek-ai/dsh-session'
  4. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  5. import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
  6. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  7. import SubagentRuntime from '@deepseek-ai/dsh-subagent'
  8. import * as Spawn from '@deepseek-ai/dsh-subagent-spawn-in-process'
  9. import WorkerThreadWorkflowEngine from '../src/index.ts'
  10. /**
  11. * With-key e2e: a REAL script in a REAL worker thread
  12. * drives REAL spawn children against the live DeepSeek API — one plain child
  13. * and one schema'd child through the real structured-output runtime — and
  14. * the run's value, events, and child sessions are asserted from the outside
  15. * (never the script's self-report alone). Key-gated (self-skips without
  16. * DEEPSEEK_API_KEY).
  17. */
  18. let ctx: Context | undefined
  19. afterEach(async () => {
  20. await ctx?.fiber.dispose()
  21. ctx = undefined
  22. })
  23. async function harness(): Promise<Context> {
  24. const built = new Context()
  25. await mountAgentLoopTestDependencies(built)
  26. await built.plugin(AgentLoop, { agents: [] })
  27. await built.plugin(LlmDeepSeek)
  28. await built.plugin(SubagentRuntime)
  29. await built.plugin(Spawn, { providerName: 'spawn' })
  30. await built.plugin(WorkerThreadWorkflowEngine, { provider: 'spawn' })
  31. return built
  32. }
  33. const META = {
  34. name: 'e2e-worker-arithmetic',
  35. description: 'two real children through a worker thread: one prose, one structured',
  36. phases: [{ title: 'Ask' }, { title: 'Judge' }],
  37. }
  38. const SCRIPT = `phase('Ask')
  39. log('asking the prose child')
  40. const prose = await agent('Reply with exactly one short sentence: what is 2 + 2?')
  41. phase('Judge')
  42. const judged = await agent(
  43. 'Here is an answer to the question "what is 2+2": ' + prose
  44. + ' — report whether it contains the number 4 and your confidence between 0 and 1.',
  45. { schema: { type: 'object', properties: { containsFour: { type: 'boolean' }, confidence: { type: 'number' } }, required: ['containsFour'] } },
  46. )
  47. return { prose, containsFour: judged === null ? null : judged.containsFour }`
  48. describe.skipIf(!process.env.DEEPSEEK_API_KEY)('worker workflow engine with-key e2e', () => {
  49. it('runs a two-phase script in a worker thread over real children, one through the structured runtime', async () => {
  50. ctx = await harness()
  51. const parentHandle = await ctx.agents.create({
  52. sessionId: 'wf-worker-e2e-session' as never,
  53. agentOptions: { provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  54. })
  55. const events: string[] = []
  56. const childIds: string[] = []
  57. for (const name of ['workflow/start', 'workflow/phase', 'workflow/log', 'workflow/agent-start', 'workflow/agent-end', 'workflow/end'] as const) {
  58. ctx.on(name, (...payload: unknown[]) => {
  59. events.push(name)
  60. if (name === 'workflow/agent-start') childIds.push((payload[1] as { childId: string }).childId)
  61. })
  62. }
  63. const run = ctx.workflowEngine.start({ script: SCRIPT, meta: META, parent: parentHandle.agent })
  64. const result = await run.result
  65. await run.dispose()
  66. expect(result.stopReason).toBe('completed')
  67. expect(result.agentsStarted).toBe(2)
  68. const value = result.value as { prose: string; containsFour: boolean | null }
  69. // World checks: the prose child really answered (a real completion), and
  70. // the structured child judged it against the REAL schema-forced tool.
  71. expect(value.prose.length).toBeGreaterThan(0)
  72. expect(value.containsFour).toBe(true)
  73. expect(events[0]).toBe('workflow/start')
  74. expect(events.at(-1)).toBe('workflow/end')
  75. expect(events.filter(name => name === 'workflow/phase').length).toBe(2)
  76. expect(events.filter(name => name === 'workflow/agent-start').length).toBe(2)
  77. expect(childIds.length).toBe(2)
  78. // The children were disposed to quiescence after collection.
  79. for (const childId of childIds) {
  80. expect(ctx.agents.get(SessionId(childId))).toBeUndefined()
  81. }
  82. await parentHandle.dispose()
  83. }, 240_000)
  84. })