-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathtasks.ts
More file actions
156 lines (129 loc) · 5.4 KB
/
Copy pathtasks.ts
File metadata and controls
156 lines (129 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type { TaskSettings as TaskSettingsOnDisk } from "@/common/config/schemas/taskSettings";
import { TASK_SETTINGS_LIMITS } from "@/common/config/schemas/taskSettings";
import type {
SubagentAiDefaults,
SubagentAiDefaultsEntry,
} from "@/common/config/schemas/appConfigOnDisk";
import { AgentIdSchema } from "@/common/orpc/schemas";
import assert from "@/common/utils/assert";
import { normalizeAgentId } from "@/common/utils/agentIds";
import { coerceThinkingLevel, type ThinkingLevel } from "./thinking";
export type { SubagentAiDefaults, SubagentAiDefaultsEntry };
export { TASK_SETTINGS_LIMITS } from "@/common/config/schemas/taskSettings";
// Normalized runtime settings always include numeric task limits.
export type TaskSettings = Required<
Pick<TaskSettingsOnDisk, "maxParallelAgentTasks" | "maxTaskNestingDepth">
> &
Omit<TaskSettingsOnDisk, "maxParallelAgentTasks" | "maxTaskNestingDepth">;
export const DEFAULT_TASK_SETTINGS: TaskSettings = {
maxParallelAgentTasks: TASK_SETTINGS_LIMITS.maxParallelAgentTasks.default,
maxTaskNestingDepth: TASK_SETTINGS_LIMITS.maxTaskNestingDepth.default,
proposePlanImplementReplacesChatHistory: false,
preserveSubagentsUntilArchive: false,
};
export {
BACKGROUND_WORK_ATTENTION_POLICIES,
BackgroundWorkAttentionPolicySchema,
DEFAULT_BACKGROUND_WORK_ATTENTION_POLICY,
resolveBackgroundWorkAttentionPolicy,
type BackgroundWorkAttentionPolicy,
} from "./backgroundWorkAttention";
const AGENT_DEFAULT_IDS_EXCLUDED_FROM_LEGACY_SUBAGENTS: ReadonlySet<string> = new Set([
"plan",
"exec",
"compact",
]);
export function shouldMirrorAgentDefaultToLegacySubagent(agentId: string): boolean {
return !AGENT_DEFAULT_IDS_EXCLUDED_FROM_LEGACY_SUBAGENTS.has(agentId);
}
export function normalizeSubagentAiDefaults(raw: unknown): SubagentAiDefaults {
const record = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : ({} as const);
const result: SubagentAiDefaults = {};
for (const [agentTypeRaw, entryRaw] of Object.entries(record)) {
const agentType = normalizeAgentId(agentTypeRaw, "");
if (!agentType) continue;
if (!AgentIdSchema.safeParse(agentType).success) continue;
if (!entryRaw || typeof entryRaw !== "object") continue;
const entry = entryRaw as Record<string, unknown>;
const modelString =
typeof entry.modelString === "string" && entry.modelString.trim().length > 0
? entry.modelString.trim()
: undefined;
const thinkingLevel: ThinkingLevel | undefined = coerceThinkingLevel(entry.thinkingLevel);
if (!modelString && !thinkingLevel) {
continue;
}
result[agentType] = { modelString, thinkingLevel };
}
return result;
}
export function deriveLegacySubagentAiDefaultsFromAgentDefaults(params: {
agentAiDefaults: Record<string, unknown>;
preservedExec?: SubagentAiDefaultsEntry;
}): SubagentAiDefaults {
const legacySubagentDefaultsRaw: Record<string, unknown> = {};
for (const [agentId, entry] of Object.entries(params.agentAiDefaults)) {
if (!shouldMirrorAgentDefaultToLegacySubagent(agentId)) continue;
legacySubagentDefaultsRaw[agentId] = entry;
}
const legacySubagentDefaults = normalizeSubagentAiDefaults(legacySubagentDefaultsRaw);
if (params.preservedExec) {
legacySubagentDefaults.exec = params.preservedExec;
}
return legacySubagentDefaults;
}
function clampInt(value: unknown, fallback: number, min: number, max: number): number {
if (typeof value !== "number" || !Number.isFinite(value)) {
return fallback;
}
const rounded = Math.floor(value);
if (rounded < min) return min;
if (rounded > max) return max;
return rounded;
}
export function normalizeTaskSettings(raw: unknown): TaskSettings {
const record = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : ({} as const);
const maxParallelAgentTasks = clampInt(
record.maxParallelAgentTasks,
DEFAULT_TASK_SETTINGS.maxParallelAgentTasks,
TASK_SETTINGS_LIMITS.maxParallelAgentTasks.min,
TASK_SETTINGS_LIMITS.maxParallelAgentTasks.max
);
const maxTaskNestingDepth = clampInt(
record.maxTaskNestingDepth,
DEFAULT_TASK_SETTINGS.maxTaskNestingDepth,
TASK_SETTINGS_LIMITS.maxTaskNestingDepth.min,
TASK_SETTINGS_LIMITS.maxTaskNestingDepth.max
);
const proposePlanImplementReplacesChatHistory =
typeof record.proposePlanImplementReplacesChatHistory === "boolean"
? record.proposePlanImplementReplacesChatHistory
: (DEFAULT_TASK_SETTINGS.proposePlanImplementReplacesChatHistory ?? false);
const preserveSubagentsUntilArchive =
typeof record.preserveSubagentsUntilArchive === "boolean"
? record.preserveSubagentsUntilArchive
: DEFAULT_TASK_SETTINGS.preserveSubagentsUntilArchive;
const result: TaskSettings = {
maxParallelAgentTasks,
maxTaskNestingDepth,
proposePlanImplementReplacesChatHistory,
preserveSubagentsUntilArchive,
};
assert(
Number.isInteger(maxParallelAgentTasks),
"normalizeTaskSettings: maxParallelAgentTasks must be an integer"
);
assert(
Number.isInteger(maxTaskNestingDepth),
"normalizeTaskSettings: maxTaskNestingDepth must be an integer"
);
assert(
typeof proposePlanImplementReplacesChatHistory === "boolean",
"normalizeTaskSettings: proposePlanImplementReplacesChatHistory must be a boolean"
);
assert(
typeof preserveSubagentsUntilArchive === "boolean",
"normalizeTaskSettings: preserveSubagentsUntilArchive must be a boolean"
);
return result;
}