workflow-workerthread.e2e.ts 4.3 KB

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