import {getAccessToken} from '@/api'; /** 사장님 에이전트 — 카카오톡 채널 연결. */ export type KakaoLinkState = { connection_enabled: boolean; channel_url: string; status: 'PENDING' | 'LINKED' | 'REVOKED' | null; linked_at: string | null; code_expires_at: string | null; }; export type KakaoLinkCode = {code: string; expires_at: string; channel_url: string}; const base = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:9800'; const messages: Record = { KAKAO_LINK_DISABLED: '카카오톡 채널을 준비하고 있습니다. 준비되면 여기서 연결할 수 있습니다.', KAKAO_LINK_ALREADY: '이미 연결되어 있습니다.', KAKAO_LINK_CODE_INVALID: '코드가 맞지 않거나 시간이 지났습니다. 새 코드를 받아 주세요.', KAKAO_LINK_NOT_FOUND: '연결된 카카오톡 계정이 없습니다.', KAKAO_LINK_TAKEN: '그 카카오톡 계정은 다른 계정에 이미 연결되어 있습니다.', }; export async function agentApi(path: string, data?: unknown): Promise { const token = getAccessToken(); const response = await fetch(`${base}/v1/agent${path}`, { method: data === undefined ? 'GET' : 'POST', credentials: 'include', // 연결 코드가 오가는 요청이다 — 리퍼러로 새거나 캐시에 남지 않게 한다. referrerPolicy: 'no-referrer', cache: 'no-store', headers: {'Content-Type': 'application/json', ...(token ? {Authorization: `Bearer ${token}`} : {})}, body: data === undefined ? undefined : JSON.stringify(data), }); const result = await response.json(); if (!response.ok) { throw new Error(messages[result.detail] ?? '요청을 처리하지 못했습니다. 잠시 후 다시 확인해 주세요.'); } return result as T; }