forked from EduCraft/curriculum-project-hub
feat: redesign Feishu project console
This commit is contained in:
@@ -89,6 +89,7 @@ export class OrganizationAgentConfiguration {
|
||||
readonly systemPrompt?: string | null | undefined;
|
||||
readonly tools?: readonly string[] | null | undefined;
|
||||
readonly sortOrder?: number | undefined;
|
||||
readonly isDefault?: boolean | undefined;
|
||||
}): Promise<{ readonly id: string; readonly roleId: string }> {
|
||||
await this.requireActiveOrganization(input.organizationId);
|
||||
if (!ROLE_ID_PATTERN.test(input.roleId)) throw new Error(`invalid role id: ${input.roleId}`);
|
||||
@@ -107,8 +108,22 @@ export class OrganizationAgentConfiguration {
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const previous = await tx.organizationAgentRole.findUnique({
|
||||
where: { organizationId_roleId: { organizationId: input.organizationId, roleId: input.roleId } },
|
||||
select: { defaultModel: true, systemPrompt: true, tools: true },
|
||||
select: { defaultModel: true, systemPrompt: true, tools: true, isDefault: true },
|
||||
});
|
||||
const currentDefault = await tx.organizationAgentRole.findFirst({
|
||||
where: { organizationId: input.organizationId, isDefault: true, disabledAt: null },
|
||||
select: { id: true },
|
||||
});
|
||||
const effectiveIsDefault = input.isDefault ?? previous?.isDefault ?? (currentDefault === null);
|
||||
if (input.isDefault === false && previous?.isDefault === true) {
|
||||
throw new Error("cannot unset the active default role without selecting a replacement");
|
||||
}
|
||||
if (effectiveIsDefault) {
|
||||
await tx.organizationAgentRole.updateMany({
|
||||
where: { organizationId: input.organizationId, isDefault: true },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
}
|
||||
const role = await tx.organizationAgentRole.upsert({
|
||||
where: {
|
||||
organizationId_roleId: {
|
||||
@@ -124,6 +139,7 @@ export class OrganizationAgentConfiguration {
|
||||
systemPrompt: normalizeOptionalText(input.systemPrompt),
|
||||
tools: createTools,
|
||||
sortOrder,
|
||||
isDefault: effectiveIsDefault,
|
||||
},
|
||||
update: {
|
||||
label,
|
||||
@@ -131,6 +147,7 @@ export class OrganizationAgentConfiguration {
|
||||
...(input.systemPrompt !== undefined ? { systemPrompt: normalizeOptionalText(input.systemPrompt) } : {}),
|
||||
...(updateTools !== undefined ? { tools: updateTools } : {}),
|
||||
sortOrder,
|
||||
isDefault: effectiveIsDefault,
|
||||
disabledAt: null,
|
||||
},
|
||||
select: { id: true, roleId: true },
|
||||
@@ -140,6 +157,12 @@ export class OrganizationAgentConfiguration {
|
||||
(input.systemPrompt !== undefined && normalizeOptionalText(input.systemPrompt) !== previous.systemPrompt) ||
|
||||
(input.tools !== undefined && JSON.stringify(input.tools) !== JSON.stringify(previous.tools))
|
||||
);
|
||||
const activeDefaultCount = await tx.organizationAgentRole.count({
|
||||
where: { organizationId: input.organizationId, isDefault: true, disabledAt: null },
|
||||
});
|
||||
if (activeDefaultCount !== 1) {
|
||||
throw new Error(`organization ${input.organizationId} must have exactly one active default role`);
|
||||
}
|
||||
if (executionSurfaceChanged) await archiveRoleSessions(tx, input.organizationId, [input.roleId]);
|
||||
await tx.auditEntry.create({
|
||||
data: {
|
||||
@@ -154,6 +177,8 @@ export class OrganizationAgentConfiguration {
|
||||
: normalizeOptionalText(input.systemPrompt) !== null,
|
||||
tools: input.tools === undefined ? "unchanged" : input.tools === null ? "all" : [...input.tools],
|
||||
sortOrder,
|
||||
isDefault: effectiveIsDefault,
|
||||
defaultExplicit: input.isDefault !== undefined,
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -237,14 +262,26 @@ async function archiveRoleSessions(
|
||||
roleIds: readonly string[],
|
||||
): Promise<void> {
|
||||
if (roleIds.length === 0) return;
|
||||
await tx.agentSession.updateMany({
|
||||
const sessions = await tx.agentSession.findMany({
|
||||
where: {
|
||||
roleId: { in: [...new Set(roleIds)] },
|
||||
archivedAt: null,
|
||||
project: { organizationId },
|
||||
},
|
||||
data: { archivedAt: new Date() },
|
||||
select: { id: true, archivedAt: true, metadata: true },
|
||||
});
|
||||
const archivedAt = new Date();
|
||||
for (const session of sessions) {
|
||||
const metadata = typeof session.metadata === "object" && session.metadata !== null && !Array.isArray(session.metadata)
|
||||
? session.metadata as Prisma.JsonObject
|
||||
: {};
|
||||
await tx.agentSession.update({
|
||||
where: { id: session.id },
|
||||
data: {
|
||||
...(session.archivedAt === null ? { archivedAt } : {}),
|
||||
metadata: { ...metadata, userResumable: false },
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function nonEmpty(value: string, label: string): string {
|
||||
|
||||
Reference in New Issue
Block a user