opencode-go-headers.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. /** Go routes inference by conversation, independently of prompt cache settings. */
  2. export function withOpenCodeGoHeaders(
  3. baseUrl: string | undefined,
  4. headers?: Readonly<Record<string, string>>,
  5. sessionId?: string,
  6. ): Record<string, string> | undefined {
  7. if (!isOpenCodeGoUrl(baseUrl)) return headers ? { ...headers } : undefined;
  8. const merged = { ...headers };
  9. for (const name of Object.keys(merged)) {
  10. if (name.toLowerCase() === 'x-opencode-session' || name.toLowerCase() === 'user-agent') {
  11. delete merged[name];
  12. }
  13. }
  14. // Runtime identity wins case-insensitively over stale, static config headers.
  15. // Connectivity requests have no conversation and receive their own identity.
  16. merged['x-opencode-session'] = sessionId || crypto.randomUUID();
  17. merged['user-agent'] = 'MiniMaxCode';
  18. return merged;
  19. }
  20. function isOpenCodeGoUrl(value: string | undefined): boolean {
  21. if (!value) return false;
  22. try {
  23. const url = new URL(value);
  24. return (
  25. url.protocol === 'https:' &&
  26. url.hostname === 'opencode.ai' &&
  27. (url.pathname === '/zen/go' || url.pathname.startsWith('/zen/go/'))
  28. );
  29. } catch {
  30. return false;
  31. }
  32. }