feat: finish master-agent prompt and memory runtime
This commit is contained in:
@@ -370,6 +370,12 @@ export interface ProjectAgentControls {
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface UserProjectAgentControls {
|
||||
account: string;
|
||||
projectId: string;
|
||||
controls: ProjectAgentControls;
|
||||
}
|
||||
|
||||
export interface DeviceImportCandidate {
|
||||
candidateId: string;
|
||||
deviceId: string;
|
||||
@@ -851,6 +857,7 @@ export interface BossState {
|
||||
masterAgentPromptPolicy: MasterAgentPromptPolicy | null;
|
||||
userMasterPrompts: UserMasterPrompt[];
|
||||
masterAgentMemories: MasterAgentMemory[];
|
||||
userProjectAgentControls: UserProjectAgentControls[];
|
||||
threadContextSnapshots: ThreadContextSnapshot[];
|
||||
threadHandoffPackages: ThreadHandoffPackage[];
|
||||
threadContextAlerts: ThreadContextAlert[];
|
||||
@@ -1268,6 +1275,7 @@ const initialState: BossState = {
|
||||
masterAgentPromptPolicy: null,
|
||||
userMasterPrompts: [],
|
||||
masterAgentMemories: [],
|
||||
userProjectAgentControls: [],
|
||||
masterAgentTasks: [],
|
||||
dispatchPlans: [],
|
||||
dispatchExecutions: [],
|
||||
@@ -2589,6 +2597,23 @@ function normalizeUserMasterPrompt(
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeUserProjectAgentControls(
|
||||
raw: Partial<UserProjectAgentControls>,
|
||||
fallback?: UserProjectAgentControls,
|
||||
): UserProjectAgentControls | null {
|
||||
const account = trimToDefined(raw.account) ?? trimToDefined(fallback?.account);
|
||||
const projectId = trimToDefined(raw.projectId) ?? trimToDefined(fallback?.projectId);
|
||||
const controls = normalizeProjectAgentControls(raw.controls ?? fallback?.controls);
|
||||
if (!account || !projectId || !controls) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
account,
|
||||
projectId,
|
||||
controls,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeMasterMemoryTags(values: string[] | undefined) {
|
||||
return dedupeStrings(
|
||||
(values ?? [])
|
||||
@@ -2871,6 +2896,17 @@ function normalizeState(raw: Partial<BossState> | undefined): BossState {
|
||||
base.masterAgentMemories[index % Math.max(1, base.masterAgentMemories.length)],
|
||||
),
|
||||
),
|
||||
userProjectAgentControls: ensureArray(
|
||||
raw.userProjectAgentControls,
|
||||
base.userProjectAgentControls,
|
||||
)
|
||||
.map((controls, index) =>
|
||||
normalizeUserProjectAgentControls(
|
||||
controls,
|
||||
base.userProjectAgentControls[index % Math.max(1, base.userProjectAgentControls.length)],
|
||||
),
|
||||
)
|
||||
.filter((item): item is UserProjectAgentControls => Boolean(item)),
|
||||
threadContextSnapshots: ensureArray(raw.threadContextSnapshots, base.threadContextSnapshots).map(
|
||||
(snapshot, index) => ({
|
||||
...base.threadContextSnapshots[index % base.threadContextSnapshots.length],
|
||||
@@ -3523,11 +3559,31 @@ export async function hasPersistedProject(projectId: string) {
|
||||
return Array.isArray(rawState.projects) && rawState.projects.some((project) => project?.id === projectId);
|
||||
}
|
||||
|
||||
export async function getProjectAgentControls(projectId: string) {
|
||||
function findUserProjectAgentControls(
|
||||
state: BossState,
|
||||
projectId: string,
|
||||
account?: string,
|
||||
) {
|
||||
const normalizedAccount = trimToDefined(account);
|
||||
if (!normalizedAccount) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
state.userProjectAgentControls.find(
|
||||
(item) => item.projectId === projectId && item.account === normalizedAccount,
|
||||
) ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export async function getProjectAgentControls(projectId: string, account?: string) {
|
||||
if (projectId !== "master-agent") {
|
||||
return null;
|
||||
}
|
||||
const state = await readState();
|
||||
const scopedControls = findUserProjectAgentControls(state, projectId, account);
|
||||
if (scopedControls?.controls) {
|
||||
return scopedControls.controls;
|
||||
}
|
||||
return state.projects.find((project) => project.id === projectId)?.agentControls ?? null;
|
||||
}
|
||||
|
||||
@@ -3538,6 +3594,7 @@ export async function updateProjectAgentControls(
|
||||
reasoningEffortOverride?: unknown;
|
||||
promptOverride?: unknown;
|
||||
},
|
||||
account?: string,
|
||||
) {
|
||||
if (projectId !== "master-agent") {
|
||||
throw new Error("MASTER_AGENT_CONTROLS_SCOPE_RESTRICTED");
|
||||
@@ -3566,7 +3623,9 @@ export async function updateProjectAgentControls(
|
||||
const project = state.projects.find((item) => item.id === projectId);
|
||||
if (!project) throw new Error("PROJECT_NOT_FOUND");
|
||||
|
||||
const currentControls = project.agentControls;
|
||||
const normalizedAccount = trimToDefined(account);
|
||||
const currentEntry = findUserProjectAgentControls(state, projectId, normalizedAccount ?? undefined);
|
||||
const currentControls = currentEntry?.controls ?? project.agentControls;
|
||||
const modelOverride =
|
||||
modelOverrideInput.kind === "set"
|
||||
? modelOverrideInput.value
|
||||
@@ -3603,11 +3662,26 @@ export async function updateProjectAgentControls(
|
||||
promptOverride,
|
||||
updatedAt: nowIso(),
|
||||
} satisfies ProjectAgentControls;
|
||||
const normalizedControls = normalizeProjectAgentControls(nextControls) ?? null;
|
||||
|
||||
if (normalizedAccount) {
|
||||
state.userProjectAgentControls = state.userProjectAgentControls.filter(
|
||||
(item) => !(item.projectId === projectId && item.account === normalizedAccount),
|
||||
);
|
||||
if (normalizedControls) {
|
||||
state.userProjectAgentControls.unshift({
|
||||
account: normalizedAccount,
|
||||
projectId,
|
||||
controls: normalizedControls,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
project.agentControls = normalizedControls ?? undefined;
|
||||
}
|
||||
|
||||
project.agentControls = normalizeProjectAgentControls(nextControls);
|
||||
project.threadMeta.updatedAt = nextControls.updatedAt;
|
||||
project.updatedAt = nextControls.updatedAt;
|
||||
return { result: project.agentControls, changed: true };
|
||||
return { result: normalizedControls, changed: true };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3837,6 +3911,24 @@ export async function archiveUserMasterMemory(memoryId: string, account: string)
|
||||
});
|
||||
}
|
||||
|
||||
export async function touchUserMasterMemories(memoryIds: string[], account: string) {
|
||||
const normalizedIds = Array.from(new Set(memoryIds.map((value) => value.trim()).filter(Boolean)));
|
||||
if (normalizedIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return mutateState((state) => {
|
||||
const now = nowIso();
|
||||
const touched: MasterAgentMemory[] = [];
|
||||
for (const memory of state.masterAgentMemories) {
|
||||
if (memory.account !== account) continue;
|
||||
if (!normalizedIds.includes(memory.memoryId)) continue;
|
||||
memory.lastUsedAt = now;
|
||||
touched.push(memory);
|
||||
}
|
||||
return touched;
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeAutoMemoryText(value: string | undefined) {
|
||||
return (value ?? "")
|
||||
.replace(/\s+/g, " ")
|
||||
|
||||
Reference in New Issue
Block a user