Files
syops/frontend/src/features/chat/composables/useChatLeave.ts
T

139 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* features/chat/composables/useChatLeave.ts
* 请假(OA)编排层:组合 leave/* 子模块,对外暴露 ChatView 所需 API。
*
* 消息类型约定(写入 messages 列表):
* - leave-form:可编辑表单
* - leave-confirmation:提交前确认
* - leave-result:OA 提交结果
*/
import { computed, type ComputedRef, type Ref } from 'vue';
import { getUserProfile } from '@/features/auth/api/user.api';
import { canUseLeaveFeature as resolveLeaveFeatureEnabled } from '@/shared/utils/agent-registry.utils';
import type { AgentRegistryFields } from '@/shared/utils/agent-registry.utils';
import { LEAVE_TYPES } from '../constants/leave';
import {
createLeaveAgentPickerApi,
createLeaveFlowApi,
createLeaveFormApi,
createLeaveLocalMessageApi,
createLeaveOaLoginApi,
formatDurationLabel,
formatYesNoLabel,
getLeaveAgentDisplayValue,
getLeaveConfirmationPayload,
getLeaveFormPayload,
getLeaveResultPayload,
syncLeaveDuration,
} from '../leave';
import { getLeaveTypeLabel } from '../leave/leaveDisplay';
import type { ChatMessage, ChatSessionItem } from '../types/chat.types';
import type { ChatDeps } from './chat.deps';
export interface UseChatLeaveOptions {
deps: ChatDeps;
currentAgent: Ref<AgentRegistryFields | null>;
currentAgentId: ComputedRef<number>;
currentUser: Ref<Record<string, unknown> | null>;
currentUserDisplayName: ComputedRef<string>;
currentUserEmployeeId: ComputedRef<string>;
messages: Ref<ChatMessage[]>;
conversationId: Ref<string | null>;
isThinking: Ref<boolean>;
history: Ref<ChatSessionItem[]>;
loadHistory: () => Promise<void>;
}
export function useChatLeave(options: UseChatLeaveOptions) {
const {
deps: { t, msg },
currentAgent,
currentAgentId,
currentUser,
currentUserDisplayName,
currentUserEmployeeId,
messages,
conversationId,
isThinking,
history,
loadHistory,
} = options;
const canUseLeaveFeature = computed(() => resolveLeaveFeatureEnabled(currentAgent.value));
const { newLocalMessageId, saveLocalMessageToBackend } = createLeaveLocalMessageApi({
conversationId,
currentAgentId,
});
const oaLogin = createLeaveOaLoginApi({
currentUserEmployeeId,
msg,
t,
});
const agentPicker = createLeaveAgentPickerApi({
currentAgentId,
messages,
saveLocalMessageToBackend,
});
const formApi = createLeaveFormApi({
messages,
currentUserDisplayName,
currentUserEmployeeId,
newLocalMessageId,
saveLocalMessageToBackend,
msg,
t,
});
const flowApi = createLeaveFlowApi({
canUseLeaveFeature,
isThinking,
messages,
conversationId,
history,
currentAgentId,
loadHistory,
newLocalMessageId,
saveLocalMessageToBackend,
createLeaveFormData: formApi.createLeaveFormData,
checkLeaveBalance: formApi.checkLeaveBalance,
formatLeaveBalanceNumber: formApi.formatLeaveBalanceNumber,
msg,
t,
});
const syncCurrentUserProfile = async () => {
if (currentUser.value?.real_name && String(currentUser.value.real_name).trim()) return;
try {
const data = await getUserProfile();
currentUser.value = { ...currentUser.value, ...data };
} catch (e) {
console.error('Failed to load current user profile', e);
}
};
return {
LEAVE_TYPES,
canUseLeaveFeature,
...oaLogin,
...agentPicker,
getLeaveTypeLabel,
formatDurationLabel: (hours: number, leaveType?: string) => formatDurationLabel(hours, leaveType, t),
formatYesNoLabel,
syncLeaveDuration,
getLeaveFormPayload,
getLeaveConfirmationPayload,
getLeaveResultPayload,
getLeaveAgentDisplayValue,
submitLeaveForm: formApi.submitLeaveForm,
editLeaveConfirmation: formApi.editLeaveConfirmation,
confirmLeaveSubmission: formApi.confirmLeaveSubmission,
tryHandleLeaveSend: flowApi.tryHandleLeaveSend,
handleLeaveMarkdownClick: flowApi.handleLeaveMarkdownClick,
syncCurrentUserProfile,
};
}