control-flow-optional-fn-call.ts 436 B

12345678
  1. // Optional call on a maybe-undefined script-local function: when defined at
  2. // runtime, helper's ask consumes the secret and its result flows onward.
  3. const scout = agent("scout");
  4. const secret = await scout.ask("find the secret");
  5. const helper = async (s: string): Promise<string> => agent("h").ask(`handle ${s}`);
  6. const f = secret.length > 3 ? helper : undefined;
  7. const out = await f?.(secret);
  8. return agent("act").ask(`out ${out}`);