thinking-context.ts 1.1 KB

1234567891011121314151617181920212223
  1. /**
  2. * Async-context carrier for per-request ThinkingConfig.
  3. *
  4. * callLLM / streamLLM wrap each AI SDK call in thinkingContext.run()
  5. * so that the custom fetch wrapper in providers.ts can read the
  6. * current thinking preference and inject vendor-specific body params.
  7. *
  8. * IMPORTANT: This module uses node:async_hooks which is server-only.
  9. * providers.ts must NOT import this module directly (it's also used
  10. * on the client via settings.ts). Instead, providers.ts reads the
  11. * context via globalThis.__thinkingContext, which is set here at
  12. * module load time and guaranteed to be available before any fetch
  13. * wrapper runs.
  14. */
  15. import { AsyncLocalStorage } from 'node:async_hooks';
  16. import type { ThinkingConfig } from '@/lib/types/provider';
  17. export const thinkingContext = new AsyncLocalStorage<ThinkingConfig | undefined>();
  18. // Expose on globalThis so providers.ts can access the store without
  19. // importing this module (which would pull node:async_hooks into the
  20. // client bundle via the settings.ts → providers.ts import chain).
  21. (globalThis as Record<string, unknown>).__thinkingContext = thinkingContext;