fix(ai): 收紧AI任务类型入库规则
This commit is contained in:
@@ -123,6 +123,15 @@ export function findCategoryByCode(categories: TaskCategory[], code?: string): T
|
||||
return categories.find((c) => c.code === code);
|
||||
}
|
||||
|
||||
function findCategoryByCodeInGroup(
|
||||
categories: TaskCategory[],
|
||||
code: string | undefined,
|
||||
group: CategoryGroup,
|
||||
): TaskCategory | undefined {
|
||||
if (!code) return undefined;
|
||||
return categories.find((category) => category.group === group && category.code === code);
|
||||
}
|
||||
|
||||
export function getDefaultCategoryByGroup(categories: TaskCategory[], group: CategoryGroup): TaskCategory {
|
||||
return getCategoriesByGroup(categories, group)[0] ?? categories[0] ?? PRESET_CATEGORIES[0];
|
||||
}
|
||||
@@ -135,13 +144,24 @@ export function ensureTaskCategoryByName(
|
||||
categories: TaskCategory[],
|
||||
name: string | undefined,
|
||||
group: CategoryGroup,
|
||||
): { categories: TaskCategory[]; category: TaskCategory; created: boolean } {
|
||||
return resolveAiTaskCategoryByName(categories, name, group, { allowCreate: true });
|
||||
}
|
||||
|
||||
export function resolveAiTaskCategoryByName(
|
||||
categories: TaskCategory[],
|
||||
name: string | undefined,
|
||||
group: CategoryGroup,
|
||||
options: { allowCreate?: boolean; fallbackCode?: string } = {},
|
||||
): { categories: TaskCategory[]; category: TaskCategory; created: boolean } {
|
||||
const normalizedCategories = normalizeTaskCategories(categories);
|
||||
const trimmedName = name?.trim() ?? '';
|
||||
if (!trimmedName) {
|
||||
return {
|
||||
categories: normalizedCategories,
|
||||
category: getDefaultCategoryByGroup(normalizedCategories, group),
|
||||
category:
|
||||
findCategoryByCodeInGroup(normalizedCategories, options.fallbackCode, group) ??
|
||||
getDefaultCategoryByGroup(normalizedCategories, group),
|
||||
created: false,
|
||||
};
|
||||
}
|
||||
@@ -153,6 +173,15 @@ export function ensureTaskCategoryByName(
|
||||
if (existing) {
|
||||
return { categories: normalizedCategories, category: existing, created: false };
|
||||
}
|
||||
if (options.allowCreate === false) {
|
||||
return {
|
||||
categories: normalizedCategories,
|
||||
category:
|
||||
findCategoryByCodeInGroup(normalizedCategories, options.fallbackCode, group) ??
|
||||
getDefaultCategoryByGroup(normalizedCategories, group),
|
||||
created: false,
|
||||
};
|
||||
}
|
||||
|
||||
const codeBase = slugifyCategoryName(trimmedName, normalizedCategories.length);
|
||||
const category: TaskCategory = {
|
||||
|
||||
Reference in New Issue
Block a user