channel-future.ts 970 B

1234567891011121314151617181920212223242526272829
  1. // channel / future lower to host passthroughs (docs/dynamic-workflow/authoring.md, "Streams"):
  2. // `channel<T>(name)` -> `__host.channel(name)` with the type argument dropped, `future(body)` ->
  3. // `__host.future(body)`. Neither has a site id; `.send` / `.close` / `for await` are ordinary
  4. // member calls on the cell's object and survive untouched.
  5. interface Fact {
  6. claim: string;
  7. }
  8. const facts = channel<Fact>("facts");
  9. const anonymous = channel<string>();
  10. phase("gather");
  11. const research = future(async () => {
  12. try {
  13. const found = await agent("researcher").ask<Fact[]>("find facts");
  14. for (const fact of found) facts.send(fact);
  15. } finally {
  16. facts.close();
  17. }
  18. });
  19. phase("verify");
  20. const verify = future(async () => {
  21. const verdicts: string[] = [];
  22. for await (const fact of facts) {
  23. verdicts.push(await agent(`verifier-${fact.claim}`).ask(`verify ${fact.claim}`));
  24. }
  25. anonymous.close();
  26. return verdicts;
  27. });
  28. await research;
  29. return verify;