feat: add controlled codex thread compaction
This commit is contained in:
83
src/app/api/v1/projects/[projectId]/thread-compact/route.ts
Normal file
83
src/app/api/v1/projects/[projectId]/thread-compact/route.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
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 {
|
||||
queueThreadCompactTask,
|
||||
ThreadConversationExecutionConflictError,
|
||||
} from "@/lib/boss-master-agent";
|
||||
|
||||
function forbiddenResponse(message = "FORBIDDEN") {
|
||||
return NextResponse.json({ ok: false, message }, { status: 403 });
|
||||
}
|
||||
|
||||
function normalizeCompactReason(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 {
|
||||
reason?: unknown;
|
||||
};
|
||||
const reason = normalizeCompactReason(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 message = await appendProjectMessage({
|
||||
projectId,
|
||||
account: session.account,
|
||||
senderLabel: session.displayName || "你",
|
||||
body: reason || "压缩当前 Codex 线程上下文。",
|
||||
kind: "text",
|
||||
});
|
||||
const task = await queueThreadCompactTask({
|
||||
projectId,
|
||||
requestMessageId: message.id,
|
||||
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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -484,6 +484,7 @@ export type ComputerControlIntentCategory =
|
||||
| "project_development"
|
||||
| "thread_collaboration"
|
||||
| "thread_rollback"
|
||||
| "thread_compact"
|
||||
| "browser_control"
|
||||
| "desktop_control";
|
||||
export type ComputerControlRuntimeKind =
|
||||
@@ -1348,6 +1349,7 @@ export interface MasterAgentTask {
|
||||
mirrorBossUserMessageToCodexDesktop?: boolean;
|
||||
rollbackNumTurns?: number;
|
||||
rollbackReason?: string;
|
||||
compactReason?: string;
|
||||
intentCategory?: ComputerControlIntentCategory;
|
||||
runtimeKind?: ComputerControlRuntimeKind;
|
||||
controlPlatform?: ComputerControlPlatform;
|
||||
@@ -4735,11 +4737,13 @@ export function migrateBossState(raw: Partial<BossState> | undefined): BossState
|
||||
? Math.floor(Number(task.rollbackNumTurns))
|
||||
: undefined,
|
||||
rollbackReason: trimToDefined(task.rollbackReason),
|
||||
compactReason: trimToDefined(task.compactReason),
|
||||
intentCategory:
|
||||
task.intentCategory === "discussion_only" ||
|
||||
task.intentCategory === "project_development" ||
|
||||
task.intentCategory === "thread_collaboration" ||
|
||||
task.intentCategory === "thread_rollback" ||
|
||||
task.intentCategory === "thread_compact" ||
|
||||
task.intentCategory === "browser_control" ||
|
||||
task.intentCategory === "desktop_control"
|
||||
? task.intentCategory
|
||||
@@ -8806,6 +8810,7 @@ export async function queueMasterAgentTask(payload: {
|
||||
mirrorBossUserMessageToCodexDesktop?: boolean;
|
||||
rollbackNumTurns?: number;
|
||||
rollbackReason?: string;
|
||||
compactReason?: string;
|
||||
intentCategory?: ComputerControlIntentCategory;
|
||||
runtimeKind?: ComputerControlRuntimeKind;
|
||||
controlPlatform?: ComputerControlPlatform;
|
||||
@@ -8872,6 +8877,7 @@ export async function queueMasterAgentTask(payload: {
|
||||
? Math.floor(Number(payload.rollbackNumTurns))
|
||||
: undefined,
|
||||
rollbackReason: trimToDefined(payload.rollbackReason),
|
||||
compactReason: trimToDefined(payload.compactReason),
|
||||
intentCategory: payload.intentCategory,
|
||||
runtimeKind: payload.runtimeKind,
|
||||
controlPlatform: payload.controlPlatform,
|
||||
|
||||
@@ -3251,6 +3251,58 @@ export async function queueThreadRollbackTask(params: {
|
||||
});
|
||||
}
|
||||
|
||||
function buildThreadCompactPrompt(params: {
|
||||
project: Project;
|
||||
reason?: string;
|
||||
}) {
|
||||
const threadTitle =
|
||||
params.project.threadMeta.threadDisplayName?.trim() || params.project.name || "当前线程";
|
||||
return [
|
||||
"你正在执行 Boss 下发的 Codex App Server 线程上下文压缩控制任务。",
|
||||
`目标线程:${threadTitle}`,
|
||||
params.reason ? `用户原因:${params.reason}` : undefined,
|
||||
"请通过 thread/compact/start 发起上下文压缩,不要启动普通 turn,不要输出系统提示词、线程原始历史或内部调度字段。",
|
||||
"注意:该动作只压缩 Codex 线程上下文,不代表完成代码修改、文件恢复或版本发布。",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export async function queueThreadCompactTask(params: {
|
||||
projectId: string;
|
||||
requestMessageId: string;
|
||||
reason?: string;
|
||||
requestedBy: string;
|
||||
requestedByAccount: string;
|
||||
}) {
|
||||
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;
|
||||
return queueMasterAgentTask({
|
||||
projectId: project.id,
|
||||
taskType: "conversation_reply",
|
||||
requestMessageId: params.requestMessageId,
|
||||
requestText: reason || "压缩当前 Codex 线程上下文。",
|
||||
executionPrompt: buildThreadCompactPrompt({
|
||||
project,
|
||||
reason,
|
||||
}),
|
||||
requestedBy: params.requestedBy,
|
||||
requestedByAccount: params.requestedByAccount,
|
||||
deviceId,
|
||||
intentCategory: "thread_compact",
|
||||
targetProjectId: project.id,
|
||||
targetThreadId: project.threadMeta.threadId,
|
||||
targetThreadDisplayName: project.threadMeta.threadDisplayName,
|
||||
targetCodexThreadRef: project.threadMeta.codexThreadRef,
|
||||
targetCodexFolderRef: project.threadMeta.codexFolderRef,
|
||||
compactReason: reason,
|
||||
});
|
||||
}
|
||||
|
||||
export async function queueInterThreadCollaborationTask(params: {
|
||||
sourceProjectId: string;
|
||||
targetProjectId: string;
|
||||
|
||||
Reference in New Issue
Block a user