261006_01_ENH: 전화를 걸지 않았는데 거는 중으로 남는 CTI 상태 오류 수정 및 AI Agent용 하네스 변경 적용

This commit is contained in:
2026-10-06 13:56:56 +09:00
parent b86ab58767
commit 746f709e92
18 changed files with 508 additions and 93 deletions

View File

@@ -10,3 +10,10 @@ export const CTI_CALL_PHASE: CtiCallPhase[] = [
// WS 서버가 보내는 CTIAgentStatus 값 중 통화 중 또는 통화 중 재로그인 대기로 간주할 값들
export const CTI_ON_CALL_STATUSES = ["0", "2", "3"] as const;
// localStorage에 저장한 CTI 상태의 만료 시간. 마지막 저장(updatedAt) 기준이며 복원 시점에만 검사한다.
export const CTI_STATE_TTL_MS = 2 * 60 * 60 * 1000;
// 상대가 받기 전(dialing) 상태의 최대 유지 시간. 이 시간 안에 StartSessionRecord/CallClosed가 없으면
// 종료 신호를 놓친 것으로 보고 idle로 되돌린다.
export const CTI_DIALING_TIMEOUT_MS = 30 * 1000;

View File

@@ -1,7 +1,12 @@
import { clientEnv } from "@/lib/env.client";
import { nextApi } from "@/lib/http-client";
import { HttpError } from "@kri/common/http";
import { CTI_AGENT_GROUP, CTI_ON_CALL_STATUSES } from "@/const/ctiConst";
import {
CTI_AGENT_GROUP,
CTI_DIALING_TIMEOUT_MS,
CTI_ON_CALL_STATUSES,
CTI_STATE_TTL_MS,
} from "@/const/ctiConst";
import type {
ListCallCloseRequest,
ListCallHist,
@@ -40,6 +45,8 @@ type CtiCallContext = {
type CtiCall = {
context: CtiCallContext;
agentSessionId: string | null;
// makeCall 시각. 이 값이 없는 dialing 상태(이전 버전 저장분)는 복원 시 만료된 것으로 본다.
dialStartedAt?: number;
};
type PersistedCtiState = {
@@ -74,7 +81,6 @@ type LegacyPersistedCtiState = Omit<
};
const CTI_STATE_VERSION = 3;
const CTI_STATE_TTL_MS = 24 * 60 * 60 * 1000;
let socket: WebSocket | null = null;
let connectionState: CtiConnectionState = "idle";
@@ -89,6 +95,7 @@ let lastCallHistId: number | null = null;
let hangupRequested = false;
let callClosedReceived = false;
let reconnectIntervalId: ReturnType<typeof setInterval> | null = null;
let dialingTimeoutId: ReturnType<typeof setTimeout> | null = null;
const listeners = new Set<() => void>();
@@ -108,6 +115,34 @@ const isCallInProgress = () =>
(agentStatus !== null &&
(CTI_ON_CALL_STATUSES as readonly string[]).includes(agentStatus));
const getDialingRemainingMs = (call: CtiCall) =>
call.dialStartedAt === undefined
? 0
: Math.max(CTI_DIALING_TIMEOUT_MS - (Date.now() - call.dialStartedAt), 0);
const clearDialingTimeout = () => {
if (dialingTimeoutId === null) return;
clearTimeout(dialingTimeoutId);
dialingTimeoutId = null;
};
// dialing 상태일 때만 남은 시간만큼 타이머를 건다. StartSessionRecord로 activeCall이 교체되거나
// 비워지면 타이머가 실행돼도 아무것도 하지 않는다.
const scheduleDialingTimeout = () => {
clearDialingTimeout();
const dialingCall = activeCall;
if (!dialingCall || dialingCall.agentSessionId != null) return;
dialingTimeoutId = setTimeout(() => {
dialingTimeoutId = null;
if (activeCall !== dialingCall) return;
console.warn("[CTI][TEMP_LOG] dialing timed out, resetting to idle", dialingCall); // TEMP_CTI_LOG
activeCall = null;
persistCtiState();
notify();
}, getDialingRemainingMs(dialingCall));
};
const isCallContext = (value: unknown): value is CtiCallContext => {
if (typeof value !== "object" || value === null) return false;
const context = value as Partial<CtiCallContext>;
@@ -139,7 +174,8 @@ const isCtiCall = (value: unknown): value is CtiCall => {
const call = value as Partial<CtiCall>;
return (
isCallContext(call.context) &&
(call.agentSessionId === null || typeof call.agentSessionId === "string")
(call.agentSessionId === null || typeof call.agentSessionId === "string") &&
(call.dialStartedAt === undefined || typeof call.dialStartedAt === "number")
);
};
@@ -246,7 +282,12 @@ const restoreCtiState = (agentLoginId: string) => {
lastCallHistId = persistedState.lastCallHistId;
hangupRequested = persistedState.hangupRequested;
callClosedReceived = persistedState.callClosedReceived;
if (persistedState !== stored) persistCtiState();
// 거는 중 새로고침은 dialing을 유지해 재발신을 막고, 타임아웃이 지난 dialing만 정리한다.
const isDialingExpired =
activeCall !== null && activeCall.agentSessionId == null && getDialingRemainingMs(activeCall) === 0;
if (isDialingExpired) activeCall = null;
if (persistedState !== stored || isDialingExpired) persistCtiState();
scheduleDialingTimeout();
notify();
return true;
};
@@ -594,6 +635,7 @@ const logout = () => {
hangupRequested = false;
callClosedReceived = false;
stopReconnectLoop();
clearDialingTimeout();
setAgentStatus(null);
setLastCallHistId(null);
if (shouldClearPersistedState) removeLocalStorage(StorageKeys.CtiState);
@@ -740,7 +782,9 @@ export const ctiClient = {
listHderId: params.listHderId,
},
agentSessionId: null,
dialStartedAt: Date.now(),
};
scheduleDialingTimeout();
lastCallContext = null;
hangupRequested = false;
callClosedReceived = false;