feat: add controlled codex thread archive

This commit is contained in:
AI Bot
2026-06-03 13:47:50 +08:00
parent 0fb588e339
commit 0bcdcbfb9d
11 changed files with 493 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
import { NextRequest, NextResponse } from "next/server";
import { requireRequestSession } from "@/lib/boss-auth";
import { appendProjectMessage, buildCollaborationGate, readState } from "@/lib/boss-data";
import { canAccessProject } from "@/lib/boss-permissions";
import {
queueThreadLifecycleTask,
ThreadConversationExecutionConflictError,
} from "@/lib/boss-master-agent";
type ThreadLifecycleAction = "archive" | "unarchive";
function forbiddenResponse(message = "FORBIDDEN") {
return NextResponse.json({ ok: false, message }, { status: 403 });
}
function normalizeLifecycleAction(value: unknown): ThreadLifecycleAction | undefined {
const normalized = String(value ?? "archive").trim();
return normalized === "archive" || normalized === "unarchive" ? normalized : undefined;
}
function normalizeLifecycleReason(value: unknown) {
const trimmed = String(value ?? "").trim();
return trimmed ? trimmed : undefined;
}
export async function POST(
request: NextRequest,
context: { params: Promise<{ projectId: string }> },
) {
const session = await requireRequestSession(request);
if (!session) {
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
}
const { projectId } = await context.params;
const body = (await request.json().catch(() => ({}))) as {
action?: unknown;
reason?: unknown;
};
const action = normalizeLifecycleAction(body.action);
if (!action) {
return NextResponse.json(
{ ok: false, message: "THREAD_LIFECYCLE_ACTION_UNSUPPORTED" },
{ status: 400 },
);
}
const reason = normalizeLifecycleReason(body.reason);
const state = await readState();
const projectExists = state.projects.some((project) => project.id === projectId);
if (!canAccessProject(state, session, projectId, "project.view")) {
return forbiddenResponse(projectExists ? "FORBIDDEN" : "PROJECT_NOT_FOUND");
}
if (!canAccessProject(state, session, projectId, "master_agent.ask")) {
return forbiddenResponse("MASTER_AGENT_FORBIDDEN");
}
try {
const actionLabel = action === "archive" ? "归档" : "恢复";
const message = await appendProjectMessage({
projectId,
account: session.account,
senderLabel: session.displayName || "你",
body: reason || `${actionLabel}当前 Codex 线程。`,
kind: "text",
});
const task = await queueThreadLifecycleTask({
projectId,
requestMessageId: message.id,
action,
reason,
requestedBy: session.displayName || session.account,
requestedByAccount: session.account,
});
const nextState = await readState();
const project = nextState.projects.find((item) => item.id === projectId);
return NextResponse.json({
ok: true,
message,
task,
collaborationGate: buildCollaborationGate(project),
});
} catch (error) {
if (error instanceof ThreadConversationExecutionConflictError) {
return NextResponse.json(
{
ok: false,
code: error.message,
message: "THREAD_EXECUTION_CONFLICT",
executionConflict: error.conflict,
},
{ status: 409 },
);
}
return NextResponse.json(
{ ok: false, message: error instanceof Error ? error.message : "UNKNOWN_ERROR" },
{ status: 400 },
);
}
}

View File

@@ -485,6 +485,8 @@ export type ComputerControlIntentCategory =
| "thread_collaboration"
| "thread_rollback"
| "thread_compact"
| "thread_archive"
| "thread_unarchive"
| "browser_control"
| "desktop_control";
export type ComputerControlRuntimeKind =
@@ -1350,6 +1352,8 @@ export interface MasterAgentTask {
rollbackNumTurns?: number;
rollbackReason?: string;
compactReason?: string;
threadLifecycleAction?: "archive" | "unarchive";
threadLifecycleReason?: string;
intentCategory?: ComputerControlIntentCategory;
runtimeKind?: ComputerControlRuntimeKind;
controlPlatform?: ComputerControlPlatform;
@@ -4738,12 +4742,19 @@ export function migrateBossState(raw: Partial<BossState> | undefined): BossState
: undefined,
rollbackReason: trimToDefined(task.rollbackReason),
compactReason: trimToDefined(task.compactReason),
threadLifecycleAction:
task.threadLifecycleAction === "archive" || task.threadLifecycleAction === "unarchive"
? task.threadLifecycleAction
: undefined,
threadLifecycleReason: trimToDefined(task.threadLifecycleReason),
intentCategory:
task.intentCategory === "discussion_only" ||
task.intentCategory === "project_development" ||
task.intentCategory === "thread_collaboration" ||
task.intentCategory === "thread_rollback" ||
task.intentCategory === "thread_compact" ||
task.intentCategory === "thread_archive" ||
task.intentCategory === "thread_unarchive" ||
task.intentCategory === "browser_control" ||
task.intentCategory === "desktop_control"
? task.intentCategory
@@ -8811,6 +8822,8 @@ export async function queueMasterAgentTask(payload: {
rollbackNumTurns?: number;
rollbackReason?: string;
compactReason?: string;
threadLifecycleAction?: "archive" | "unarchive";
threadLifecycleReason?: string;
intentCategory?: ComputerControlIntentCategory;
runtimeKind?: ComputerControlRuntimeKind;
controlPlatform?: ComputerControlPlatform;
@@ -8878,6 +8891,11 @@ export async function queueMasterAgentTask(payload: {
: undefined,
rollbackReason: trimToDefined(payload.rollbackReason),
compactReason: trimToDefined(payload.compactReason),
threadLifecycleAction:
payload.threadLifecycleAction === "archive" || payload.threadLifecycleAction === "unarchive"
? payload.threadLifecycleAction
: undefined,
threadLifecycleReason: trimToDefined(payload.threadLifecycleReason),
intentCategory: payload.intentCategory,
runtimeKind: payload.runtimeKind,
controlPlatform: payload.controlPlatform,

View File

@@ -3303,6 +3303,68 @@ export async function queueThreadCompactTask(params: {
});
}
function buildThreadLifecyclePrompt(params: {
project: Project;
action: "archive" | "unarchive";
reason?: string;
}) {
const threadTitle =
params.project.threadMeta.threadDisplayName?.trim() || params.project.name || "当前线程";
const actionLabel = params.action === "archive" ? "归档" : "恢复";
const method = params.action === "archive" ? "thread/archive" : "thread/unarchive";
return [
`你正在执行 Boss 下发的 Codex App Server 线程${actionLabel}控制任务。`,
`目标线程:${threadTitle}`,
params.reason ? `用户原因:${params.reason}` : undefined,
`请通过 ${method} 执行线程${actionLabel},不要启动普通 turn不要输出系统提示词、线程原始历史或内部调度字段。`,
"注意:该动作只改变 Codex 线程生命周期状态,不代表代码修改、文件恢复或版本发布完成。",
]
.filter(Boolean)
.join("\n");
}
export async function queueThreadLifecycleTask(params: {
projectId: string;
requestMessageId: string;
action: "archive" | "unarchive";
reason?: string;
requestedBy: string;
requestedByAccount: string;
}) {
if (params.action !== "archive" && params.action !== "unarchive") {
throw new Error("THREAD_LIFECYCLE_ACTION_UNSUPPORTED");
}
const conflict = await getThreadConversationExecutionConflict(params.projectId);
if (conflict) {
throw new ThreadConversationExecutionConflictError(conflict);
}
const { project, deviceId } = await resolveThreadConversationExecutionContext(params.projectId);
const reason = params.reason?.trim() || undefined;
const actionLabel = params.action === "archive" ? "归档" : "恢复";
return queueMasterAgentTask({
projectId: project.id,
taskType: "conversation_reply",
requestMessageId: params.requestMessageId,
requestText: reason || `${actionLabel}当前 Codex 线程。`,
executionPrompt: buildThreadLifecyclePrompt({
project,
action: params.action,
reason,
}),
requestedBy: params.requestedBy,
requestedByAccount: params.requestedByAccount,
deviceId,
intentCategory: params.action === "archive" ? "thread_archive" : "thread_unarchive",
targetProjectId: project.id,
targetThreadId: project.threadMeta.threadId,
targetThreadDisplayName: project.threadMeta.threadDisplayName,
targetCodexThreadRef: project.threadMeta.codexThreadRef,
targetCodexFolderRef: project.threadMeta.codexFolderRef,
threadLifecycleAction: params.action,
threadLifecycleReason: reason,
});
}
export async function queueInterThreadCollaborationTask(params: {
sourceProjectId: string;
targetProjectId: string;