stream-pipeline.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Streams (docs/dynamic-workflow/authoring.md, "Streams"): two stages written as futures, each
  2. // opening with its own phase marker, joined by a channel. The channel is a container the taint
  3. // analysis sees through: `facts.send(r)` writes the researcher's result into the channel's place
  4. // and `for await (const fact of facts)` reads it back, so every producer draws a data edge to
  5. // every consumer. The `future` bodies inline at the call as entered strands.
  6. interface Fact {
  7. id: string;
  8. claim: string;
  9. }
  10. interface Verdict {
  11. holds: boolean;
  12. }
  13. const facts = channel<Fact>("facts");
  14. const verified: Fact[] = [];
  15. const research = future(async () => {
  16. phase("Gather facts from three angles");
  17. try {
  18. await Promise.all(
  19. ["a", "b", "c"].map(async (angle) => {
  20. const found = await agent(`researcher-${angle}`).ask<Fact[]>(`Find facts about ${angle}`);
  21. for (const fact of found) facts.send(fact);
  22. }),
  23. );
  24. } finally {
  25. facts.close();
  26. }
  27. });
  28. const verify = future(async () => {
  29. phase("Verify each fact as it arrives");
  30. await Promise.all(
  31. [1, 2].map(async () => {
  32. for await (const fact of facts) {
  33. const v = await agent(`verifier-${fact.id}`).ask<Verdict>(`Verify ${fact.claim}`);
  34. if (v.holds) verified.push(fact);
  35. }
  36. }),
  37. );
  38. });
  39. await Promise.all([research, verify]);
  40. phase("Write the report");
  41. return agent("writer").ask<string>(`Write up ${JSON.stringify(verified)}`);