Files
openclaw/src/tasks/task-owner-access.ts
T
Peter Steinberger 00d8d7ead0 refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
2026-05-31 01:33:00 +01:00

133 lines
3.7 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import {
findTaskByRunId,
getTaskById,
listTasksForRelatedSessionKey,
markTaskTerminalById as markTaskTerminalRecordById,
resolveTaskForLookupToken,
updateTaskNotifyPolicyById,
} from "./task-registry.js";
import type { TaskNotifyPolicy, TaskRecord } from "./task-registry.types.js";
import { buildTaskStatusSnapshot } from "./task-status.js";
function canOwnerAccessTask(task: TaskRecord, callerOwnerKey: string): boolean {
return (
task.scopeKind === "session" &&
normalizeOptionalString(task.ownerKey) === normalizeOptionalString(callerOwnerKey)
);
}
export function getTaskByIdForOwner(params: {
taskId: string;
callerOwnerKey: string;
}): TaskRecord | undefined {
const task = getTaskById(params.taskId);
return task && canOwnerAccessTask(task, params.callerOwnerKey) ? task : undefined;
}
export function findTaskByRunIdForOwner(params: {
runId: string;
callerOwnerKey: string;
}): TaskRecord | undefined {
const task = findTaskByRunId(params.runId);
return task && canOwnerAccessTask(task, params.callerOwnerKey) ? task : undefined;
}
/** Update an owner-visible task's notification policy. */
export function updateTaskNotifyPolicyForOwner(params: {
taskId: string;
callerOwnerKey: string;
notifyPolicy: TaskNotifyPolicy;
}): TaskRecord | null {
const task = getTaskByIdForOwner({
taskId: params.taskId,
callerOwnerKey: params.callerOwnerKey,
});
if (!task) {
return null;
}
return updateTaskNotifyPolicyById({
taskId: task.taskId,
notifyPolicy: params.notifyPolicy,
});
}
/** Mark an owner-visible task as cancelled with a caller-provided summary. */
export function cancelTaskByIdForOwner(params: {
taskId: string;
callerOwnerKey: string;
endedAt: number;
terminalSummary?: string | null;
}): TaskRecord | null {
const task = getTaskByIdForOwner({
taskId: params.taskId,
callerOwnerKey: params.callerOwnerKey,
});
if (!task) {
return null;
}
return markTaskTerminalRecordById({
taskId: task.taskId,
status: "cancelled",
endedAt: params.endedAt,
terminalSummary: params.terminalSummary,
});
}
export function listTasksForRelatedSessionKeyForOwner(params: {
relatedSessionKey: string;
callerOwnerKey: string;
}): TaskRecord[] {
return listTasksForRelatedSessionKey(params.relatedSessionKey).filter((task) =>
canOwnerAccessTask(task, params.callerOwnerKey),
);
}
export function buildTaskStatusSnapshotForRelatedSessionKeyForOwner(params: {
relatedSessionKey: string;
callerOwnerKey: string;
}) {
return buildTaskStatusSnapshot(
listTasksForRelatedSessionKeyForOwner({
relatedSessionKey: params.relatedSessionKey,
callerOwnerKey: params.callerOwnerKey,
}),
);
}
export function findLatestTaskForRelatedSessionKeyForOwner(params: {
relatedSessionKey: string;
callerOwnerKey: string;
}): TaskRecord | undefined {
return listTasksForRelatedSessionKeyForOwner(params)[0];
}
export function resolveTaskForLookupTokenForOwner(params: {
token: string;
callerOwnerKey: string;
}): TaskRecord | undefined {
const direct = getTaskByIdForOwner({
taskId: params.token,
callerOwnerKey: params.callerOwnerKey,
});
if (direct) {
return direct;
}
const byRun = findTaskByRunIdForOwner({
runId: params.token,
callerOwnerKey: params.callerOwnerKey,
});
if (byRun) {
return byRun;
}
const related = findLatestTaskForRelatedSessionKeyForOwner({
relatedSessionKey: params.token,
callerOwnerKey: params.callerOwnerKey,
});
if (related) {
return related;
}
const raw = resolveTaskForLookupToken(params.token);
return raw && canOwnerAccessTask(raw, params.callerOwnerKey) ? raw : undefined;
}