agentschat-mcp 0.36.24 → 0.36.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +8 -0
- package/codex/README.md +1 -1
- package/codex/app-server.ts +79 -22
- package/codex/bots-config.ts +31 -11
- package/codex/bridge.ts +123 -25
- package/codex/config.ts +128 -21
- package/codex/gui-channel.ts +66 -9
- package/codex/inbox-sync.ts +65 -12
- package/codex/loop-grants.ts +41 -11
- package/codex/manager.ts +101 -25
- package/codex/owner.ts +81 -9
- package/codex/processes.ts +51 -7
- package/codex/run.ts +70 -17
- package/codex/runtime-home.ts +41 -11
- package/codex/thread-history.ts +77 -30
- package/codex/transport.ts +122 -19
- package/connector/auth.ts +57 -5
- package/connector/backfill.ts +92 -10
- package/connector/descriptor.ts +27 -1
- package/connector/identities.ts +82 -14
- package/connector/ingest.ts +11 -2
- package/connector/normalize.ts +32 -12
- package/connector/run.ts +314 -67
- package/connector/server.ts +95 -26
- package/dist/codex-bots.js +410 -73
- package/dist/codex-bridge.js +1996 -576
- package/dist/connector.js +1286 -493
- package/dist/dsh.js +1776 -299
- package/dist/muse.js +967 -221
- package/dist/server.js +7645 -1172
- package/dsh/client.js +7 -5
- package/dsh/identity-ui/client.js +7 -5
- package/dsh/identity-ui/index.js +43 -8
- package/dsh/inbound-bridge/index.js +595 -146
- package/dsh/src/cli.ts +430 -91
- package/dsh/src/patch.ts +295 -15
- package/dsh/tests/dsh-hardening-127.test.ts +207 -0
- package/dsh/tests/dsh.test.ts +990 -3
- package/muse/scripts/ensure-muse-wakes.mjs +5 -3
- package/muse/scripts/muse-wake-receiver.mjs +4 -1
- package/muse/src/bridge.ts +54 -13
- package/muse/src/cli.ts +226 -41
- package/muse/src/client.ts +249 -34
- package/muse/src/config.ts +128 -12
- package/muse/src/policy.ts +28 -8
- package/muse/src/types.ts +1 -0
- package/muse/src/typing.ts +18 -1
- package/muse/src/wake.ts +237 -35
- package/muse/tests/bridge.test.mjs +131 -0
- package/muse/tests/client.test.mjs +327 -0
- package/muse/tests/config.test.mjs +78 -1
- package/muse/tests/hardening.test.mjs +199 -0
- package/muse/tests/hardening126.test.mjs +292 -0
- package/muse/tests/policy.test.mjs +83 -1
- package/muse/tests/wake.test.mjs +134 -6
- package/package.json +3 -3
- package/scripts/ensure-grok-wakes.mjs +93 -8
- package/scripts/example-url-wake-receiver.mjs +254 -61
- package/scripts/grok-bind-register.sh +38 -8
- package/skills/agentschat-team-lead/SKILL.md +1 -1
- package/skills/onboarding.md +14 -10
- package/src/argcheck.ts +1890 -1
- package/src/cli.mjs +98 -14
- package/src/dedup.ts +57 -8
- package/src/grok-bind.ts +160 -33
- package/src/heartbeat.ts +39 -11
- package/src/identity-guard.ts +93 -20
- package/src/identity.ts +67 -11
- package/src/mentions.ts +26 -5
- package/src/onboarding-status.ts +134 -11
- package/src/profile-selection.ts +269 -18
- package/src/profile-store.ts +222 -28
- package/src/read-cursor.ts +256 -9
- package/src/reconnect.ts +98 -2
- package/src/redact.ts +34 -15
- package/src/rest-cli.ts +713 -159
- package/src/server.ts +2487 -342
- package/src/session-bind.ts +731 -132
- package/src/session-route.ts +183 -12
- package/src/team-lead-skill.ts +3 -0
- package/src/timestamps.ts +56 -23
- package/src/tool-annotations.ts +22 -1
- package/src/wake.ts +524 -90
package/src/argcheck.ts
CHANGED
|
@@ -10,8 +10,19 @@
|
|
|
10
10
|
*
|
|
11
11
|
* Returns null when the args are acceptable, or a human-readable reason string.
|
|
12
12
|
*/
|
|
13
|
+
const FORBIDDEN_ARG_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
14
|
+
|
|
15
|
+
function hasOwnProto(v: unknown): boolean {
|
|
16
|
+
return Boolean(v && typeof v === "object" && Object.prototype.hasOwnProperty.call(v, "__proto__"));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isReservedIdKey(id: string): boolean {
|
|
20
|
+
return id === "." || id.includes("..") || FORBIDDEN_ARG_KEYS.has(id.toLowerCase());
|
|
21
|
+
}
|
|
22
|
+
|
|
13
23
|
export function validateToolArgs(schema: any, args: any): string | null {
|
|
14
24
|
if (!schema || schema.type !== "object" || !schema.properties) return null;
|
|
25
|
+
if (hasOwnProto(args)) return 'disallowed argument "__proto__"';
|
|
15
26
|
const a = args && typeof args === "object" && !Array.isArray(args) ? args : {};
|
|
16
27
|
|
|
17
28
|
const required: string[] = Array.isArray(schema.required) ? schema.required : [];
|
|
@@ -55,7 +66,7 @@ function matchesJsonType(val: any, expected: string | string[]): boolean {
|
|
|
55
66
|
case "array":
|
|
56
67
|
return Array.isArray(val);
|
|
57
68
|
case "object":
|
|
58
|
-
return val !== null && typeof val === "object" && !Array.isArray(val);
|
|
69
|
+
return val !== null && typeof val === "object" && !Array.isArray(val) && !hasOwnProto(val);
|
|
59
70
|
case "null":
|
|
60
71
|
return val === null;
|
|
61
72
|
default:
|
|
@@ -63,3 +74,1881 @@ function matchesJsonType(val: any, expected: string | string[]): boolean {
|
|
|
63
74
|
}
|
|
64
75
|
});
|
|
65
76
|
}
|
|
77
|
+
|
|
78
|
+
export const TASK_EVIDENCE_KINDS = [
|
|
79
|
+
"commit",
|
|
80
|
+
"deploy",
|
|
81
|
+
"probe",
|
|
82
|
+
"doc",
|
|
83
|
+
"external",
|
|
84
|
+
"rollout",
|
|
85
|
+
] as const;
|
|
86
|
+
export type TaskEvidenceKind = (typeof TASK_EVIDENCE_KINDS)[number];
|
|
87
|
+
|
|
88
|
+
export interface TaskEvidenceInput {
|
|
89
|
+
kind: TaskEvidenceKind;
|
|
90
|
+
ref: string;
|
|
91
|
+
note?: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const TASK_EVIDENCE_CAP = 10;
|
|
95
|
+
const TASK_EVIDENCE_REF_MAX = 160;
|
|
96
|
+
const TASK_EVIDENCE_NOTE_MAX = 200;
|
|
97
|
+
const EVIDENCE_CTRL_RE = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;
|
|
98
|
+
const EVIDENCE_SECRET_RE =
|
|
99
|
+
/(?:(?:ac|agent_key|mcp_at|mcp_rt|mcp_code|whsec|mr)_[A-Za-z0-9_-]{8,}|(?:hf|npm)_[A-Za-z0-9]{16,}|glpat-[A-Za-z0-9_-]{12,}|xox[baprs]-[A-Za-z0-9-]{10,}|eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|(?:sk|rk)-(?:live-|test-|proj-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{16,}|github_pat_[A-Za-z0-9_]{16,}|AIza[0-9A-Za-z_-]{20,}|(?:AKIA|ASIA)[0-9A-Z]{16}|-----BEGIN [^-]*PRIVATE KEY-----)/;
|
|
100
|
+
|
|
101
|
+
function isTaskEvidenceKind(v: unknown): v is TaskEvidenceKind {
|
|
102
|
+
return typeof v === "string" && (TASK_EVIDENCE_KINDS as readonly string[]).includes(v);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Validate `okr_update_task` `evidence` client-side so invalid shapes or
|
|
107
|
+
* accidental credential leaks fail fast before leaving the local MCP runtime.
|
|
108
|
+
* Passing `[]` or `null` normalizes to `[]` so callers can clear existing evidence.
|
|
109
|
+
*/
|
|
110
|
+
export function validateTaskEvidenceInput(
|
|
111
|
+
raw: unknown,
|
|
112
|
+
): { ok: true; value: TaskEvidenceInput[] } | { ok: false; error: string } {
|
|
113
|
+
if (raw === null) return { ok: true, value: [] };
|
|
114
|
+
if (!Array.isArray(raw)) {
|
|
115
|
+
return { ok: false, error: "evidence must be an array (max 10 items; pass [] to clear)" };
|
|
116
|
+
}
|
|
117
|
+
if (raw.length === 0) return { ok: true, value: [] };
|
|
118
|
+
if (raw.length > TASK_EVIDENCE_CAP) {
|
|
119
|
+
return { ok: false, error: `evidence exceeds max ${TASK_EVIDENCE_CAP} items` };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const out: TaskEvidenceInput[] = [];
|
|
123
|
+
const seen = new Set<string>();
|
|
124
|
+
for (let i = 0; i < raw.length; i++) {
|
|
125
|
+
const item = raw[i];
|
|
126
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
127
|
+
return { ok: false, error: `evidence[${i}] must be an object` };
|
|
128
|
+
}
|
|
129
|
+
const rawKind = (item as Record<string, unknown>).kind;
|
|
130
|
+
const rawRef = (item as Record<string, unknown>).ref;
|
|
131
|
+
const rawNote = (item as Record<string, unknown>).note;
|
|
132
|
+
if (!isTaskEvidenceKind(rawKind)) {
|
|
133
|
+
return {
|
|
134
|
+
ok: false,
|
|
135
|
+
error: `evidence[${i}].kind must be one of ${TASK_EVIDENCE_KINDS.join("|")}`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (typeof rawRef !== "string") {
|
|
139
|
+
return { ok: false, error: `evidence[${i}].ref must be a non-empty string` };
|
|
140
|
+
}
|
|
141
|
+
const ref = rawRef.trim();
|
|
142
|
+
if (!ref) {
|
|
143
|
+
return { ok: false, error: `evidence[${i}].ref must be a non-empty string` };
|
|
144
|
+
}
|
|
145
|
+
if (ref.length > TASK_EVIDENCE_REF_MAX) {
|
|
146
|
+
return { ok: false, error: `evidence[${i}].ref exceeds ${TASK_EVIDENCE_REF_MAX} chars` };
|
|
147
|
+
}
|
|
148
|
+
if (EVIDENCE_CTRL_RE.test(ref)) {
|
|
149
|
+
return { ok: false, error: `evidence[${i}].ref contains disallowed control characters` };
|
|
150
|
+
}
|
|
151
|
+
if (EVIDENCE_SECRET_RE.test(ref)) {
|
|
152
|
+
return { ok: false, error: `evidence[${i}].ref rejected: appears to contain a secret or token` };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let note: string | undefined;
|
|
156
|
+
if (rawNote !== undefined && rawNote !== null) {
|
|
157
|
+
if (typeof rawNote !== "string") {
|
|
158
|
+
return { ok: false, error: `evidence[${i}].note must be a string` };
|
|
159
|
+
}
|
|
160
|
+
const normalizedNote = rawNote.replace(/[\r\n\t]+/g, " ").trim();
|
|
161
|
+
if (normalizedNote.length > TASK_EVIDENCE_NOTE_MAX) {
|
|
162
|
+
return { ok: false, error: `evidence[${i}].note exceeds ${TASK_EVIDENCE_NOTE_MAX} chars` };
|
|
163
|
+
}
|
|
164
|
+
if (EVIDENCE_CTRL_RE.test(normalizedNote)) {
|
|
165
|
+
return { ok: false, error: `evidence[${i}].note contains disallowed control characters` };
|
|
166
|
+
}
|
|
167
|
+
if (normalizedNote.length > 0) {
|
|
168
|
+
if (EVIDENCE_SECRET_RE.test(normalizedNote)) {
|
|
169
|
+
return { ok: false, error: `evidence[${i}].note rejected: appears to contain a secret or token` };
|
|
170
|
+
}
|
|
171
|
+
note = normalizedNote;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const dedupKey = `${rawKind}:${ref}`;
|
|
176
|
+
if (seen.has(dedupKey)) {
|
|
177
|
+
return { ok: false, error: `duplicate evidence entry "${dedupKey}"` };
|
|
178
|
+
}
|
|
179
|
+
seen.add(dedupKey);
|
|
180
|
+
out.push({
|
|
181
|
+
kind: rawKind,
|
|
182
|
+
ref,
|
|
183
|
+
...(note ? { note } : {}),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return { ok: true, value: out };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function validateAppendTaskEvidenceInput(
|
|
190
|
+
raw: unknown,
|
|
191
|
+
): { ok: true; value: TaskEvidenceInput[] } | { ok: false; error: string } {
|
|
192
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
193
|
+
return {
|
|
194
|
+
ok: false,
|
|
195
|
+
error: `append_evidence must be a non-empty array (1..${TASK_EVIDENCE_CAP} items)`,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return validateTaskEvidenceInput(raw);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function validateTaskVersionToken(
|
|
202
|
+
raw: unknown,
|
|
203
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
204
|
+
if (typeof raw !== "string") {
|
|
205
|
+
return { ok: false, error: "if_match_updated_at must be a non-empty string" };
|
|
206
|
+
}
|
|
207
|
+
const trimmed = raw.trim();
|
|
208
|
+
if (!trimmed || trimmed.length > 64 || EVIDENCE_CTRL_RE.test(trimmed)) {
|
|
209
|
+
return {
|
|
210
|
+
ok: false,
|
|
211
|
+
error: "if_match_updated_at must be a valid non-empty timestamp token (<=64 chars)",
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
if (EVIDENCE_SECRET_RE.test(trimmed)) {
|
|
215
|
+
return {
|
|
216
|
+
ok: false,
|
|
217
|
+
error: "if_match_updated_at rejected: appears to contain a secret or token",
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return { ok: true, value: trimmed };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function responseIncludesAppendedEvidence(
|
|
224
|
+
taskRaw: unknown,
|
|
225
|
+
appended: ReadonlyArray<TaskEvidenceInput>,
|
|
226
|
+
): boolean {
|
|
227
|
+
if (!taskRaw || typeof taskRaw !== "object") return false;
|
|
228
|
+
const evList = (taskRaw as Record<string, unknown>).evidence;
|
|
229
|
+
if (!Array.isArray(evList)) return false;
|
|
230
|
+
return appended.every((want) =>
|
|
231
|
+
evList.some((item) => {
|
|
232
|
+
if (!item || typeof item !== "object") return false;
|
|
233
|
+
const gotKind = (item as Record<string, unknown>).kind;
|
|
234
|
+
const gotRef = typeof (item as Record<string, unknown>).ref === "string"
|
|
235
|
+
? ((item as Record<string, unknown>).ref as string).trim()
|
|
236
|
+
: "";
|
|
237
|
+
const gotNote = typeof (item as Record<string, unknown>).note === "string"
|
|
238
|
+
? ((item as Record<string, unknown>).note as string).replace(/[\r\n\t]+/g, " ").trim()
|
|
239
|
+
: "";
|
|
240
|
+
return gotKind === want.kind && gotRef === want.ref && gotNote === (want.note ?? "");
|
|
241
|
+
}),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function mergeLegacyTaskEvidence(
|
|
246
|
+
existingRaw: unknown,
|
|
247
|
+
appended: ReadonlyArray<TaskEvidenceInput>,
|
|
248
|
+
): { ok: true; value: TaskEvidenceInput[] } | { ok: false; error: string } {
|
|
249
|
+
const merged: TaskEvidenceInput[] = [];
|
|
250
|
+
const indexByKey = new Map<string, number>();
|
|
251
|
+
if (Array.isArray(existingRaw)) {
|
|
252
|
+
for (const raw of existingRaw) {
|
|
253
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue;
|
|
254
|
+
const rawKind = (raw as Record<string, unknown>).kind;
|
|
255
|
+
const rawRef = (raw as Record<string, unknown>).ref;
|
|
256
|
+
const rawNote = (raw as Record<string, unknown>).note;
|
|
257
|
+
if (!isTaskEvidenceKind(rawKind) || typeof rawRef !== "string") continue;
|
|
258
|
+
const ref = rawRef.trim();
|
|
259
|
+
if (!ref || ref.length > TASK_EVIDENCE_REF_MAX || EVIDENCE_CTRL_RE.test(ref)) continue;
|
|
260
|
+
const note = typeof rawNote === "string"
|
|
261
|
+
? rawNote.replace(/[\r\n\t]+/g, " ").trim()
|
|
262
|
+
: "";
|
|
263
|
+
if (note.length > TASK_EVIDENCE_NOTE_MAX || EVIDENCE_CTRL_RE.test(note)) continue;
|
|
264
|
+
const key = `${rawKind}:${ref}`;
|
|
265
|
+
if (indexByKey.has(key)) continue;
|
|
266
|
+
if (merged.length >= TASK_EVIDENCE_CAP) break;
|
|
267
|
+
indexByKey.set(key, merged.length);
|
|
268
|
+
merged.push({ kind: rawKind, ref, ...(note ? { note } : {}) });
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
for (const item of appended) {
|
|
272
|
+
const key = `${item.kind}:${item.ref}`;
|
|
273
|
+
const existingIdx = indexByKey.get(key);
|
|
274
|
+
if (existingIdx !== undefined) {
|
|
275
|
+
merged[existingIdx] = { ...item };
|
|
276
|
+
} else {
|
|
277
|
+
if (merged.length >= TASK_EVIDENCE_CAP) {
|
|
278
|
+
return {
|
|
279
|
+
ok: false,
|
|
280
|
+
error: `evidence_cap_exceeded (task already at ${TASK_EVIDENCE_CAP} items; refused to overwrite history)`,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
indexByKey.set(key, merged.length);
|
|
284
|
+
merged.push({ ...item });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return { ok: true, value: merged };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export const CREATE_LOOP_MODES = ["okr_wake", "okr_watch", "prompt"] as const;
|
|
291
|
+
export type CreateLoopMode = (typeof CREATE_LOOP_MODES)[number];
|
|
292
|
+
|
|
293
|
+
export interface ValidatedCreateLoopBody {
|
|
294
|
+
channel_id: string;
|
|
295
|
+
mode: CreateLoopMode;
|
|
296
|
+
interval?: string;
|
|
297
|
+
interval_ms?: number;
|
|
298
|
+
objective_id?: string;
|
|
299
|
+
prompt?: string;
|
|
300
|
+
target_agents?: string[];
|
|
301
|
+
watch_idle?: boolean;
|
|
302
|
+
watch_supply?: boolean;
|
|
303
|
+
stall_minutes?: number;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const LOOP_MIN_INTERVAL_MS = 60_000;
|
|
307
|
+
const LOOP_MAX_INTERVAL_MS = 86_400_000;
|
|
308
|
+
const SAFE_CHANNEL_OR_LOOP_ID_RE = /^[A-Za-z0-9_.:-]{1,128}$/;
|
|
309
|
+
const SAFE_OBJECTIVE_ID_RE = /^[A-Za-z0-9_-]{6,80}$/;
|
|
310
|
+
const SAFE_AGENT_HANDLE_RE = /^[A-Za-z0-9_-]{2,64}$/;
|
|
311
|
+
const LOOP_PROMPT_CTRL_RE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f\u2028\u2029]/;
|
|
312
|
+
|
|
313
|
+
export function parseLoopIntervalShorthandMs(raw: string): number | null {
|
|
314
|
+
const m = raw.trim().match(/^(\d+)\s*(s|sec|m|min|h|hr|d)?$/i);
|
|
315
|
+
if (!m) return null;
|
|
316
|
+
const n = Number(m[1]);
|
|
317
|
+
if (!Number.isSafeInteger(n) || n <= 0) return null;
|
|
318
|
+
const unit = (m[2] || "m").toLowerCase();
|
|
319
|
+
const mult =
|
|
320
|
+
unit === "s" || unit === "sec" ? 1_000 :
|
|
321
|
+
unit === "m" || unit === "min" ? 60_000 :
|
|
322
|
+
unit === "h" || unit === "hr" ? 3_600_000 :
|
|
323
|
+
86_400_000;
|
|
324
|
+
const ms = n * mult;
|
|
325
|
+
if (!Number.isSafeInteger(ms) || ms < LOOP_MIN_INTERVAL_MS || ms > LOOP_MAX_INTERVAL_MS) {
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
return ms;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function validateCreateLoopInput(
|
|
332
|
+
args: unknown,
|
|
333
|
+
): { ok: true; value: ValidatedCreateLoopBody } | { ok: false; error: string } {
|
|
334
|
+
if (hasOwnProto(args)) {
|
|
335
|
+
return { ok: false, error: 'create_loop arguments cannot define own "__proto__".' };
|
|
336
|
+
}
|
|
337
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
338
|
+
? (args as Record<string, unknown>)
|
|
339
|
+
: {};
|
|
340
|
+
const channelId = typeof a.channel_id === "string" ? a.channel_id.trim() : "";
|
|
341
|
+
if (
|
|
342
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(channelId) ||
|
|
343
|
+
isReservedIdKey(channelId) ||
|
|
344
|
+
EVIDENCE_CTRL_RE.test(channelId) ||
|
|
345
|
+
EVIDENCE_SECRET_RE.test(channelId)
|
|
346
|
+
) {
|
|
347
|
+
return { ok: false, error: "create_loop needs a valid channel_id." };
|
|
348
|
+
}
|
|
349
|
+
const rawMode = typeof a.mode === "string" ? a.mode.trim() : "";
|
|
350
|
+
if (!(CREATE_LOOP_MODES as readonly string[]).includes(rawMode)) {
|
|
351
|
+
return { ok: false, error: "create_loop mode must be one of okr_wake|okr_watch|prompt." };
|
|
352
|
+
}
|
|
353
|
+
const mode = rawMode as CreateLoopMode;
|
|
354
|
+
|
|
355
|
+
let intervalMs: number | undefined;
|
|
356
|
+
let interval: string | undefined;
|
|
357
|
+
if (a.interval_ms !== undefined && a.interval_ms !== null) {
|
|
358
|
+
if (
|
|
359
|
+
typeof a.interval_ms !== "number" ||
|
|
360
|
+
!Number.isSafeInteger(a.interval_ms) ||
|
|
361
|
+
a.interval_ms < LOOP_MIN_INTERVAL_MS ||
|
|
362
|
+
a.interval_ms > LOOP_MAX_INTERVAL_MS
|
|
363
|
+
) {
|
|
364
|
+
return {
|
|
365
|
+
ok: false,
|
|
366
|
+
error: `create_loop interval_ms must be an integer in ${LOOP_MIN_INTERVAL_MS}..${LOOP_MAX_INTERVAL_MS} (1m..24h).`,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
intervalMs = a.interval_ms;
|
|
370
|
+
}
|
|
371
|
+
if (a.interval !== undefined && a.interval !== null) {
|
|
372
|
+
if (typeof a.interval !== "string" || !a.interval.trim()) {
|
|
373
|
+
return { ok: false, error: "create_loop interval must be a shorthand string such as 5m, 15m, 1h." };
|
|
374
|
+
}
|
|
375
|
+
const parsed = parseLoopIntervalShorthandMs(a.interval);
|
|
376
|
+
if (parsed === null) {
|
|
377
|
+
return { ok: false, error: "create_loop interval must resolve within 1m..24h (e.g. 5m, 15m, 30m, 1h)." };
|
|
378
|
+
}
|
|
379
|
+
interval = a.interval.trim();
|
|
380
|
+
if (intervalMs !== undefined && intervalMs !== parsed) {
|
|
381
|
+
return { ok: false, error: "create_loop interval and interval_ms disagree; pass only one or matching values." };
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let targetAgents: string[] | undefined;
|
|
386
|
+
if (a.target_agents !== undefined && a.target_agents !== null) {
|
|
387
|
+
if (!Array.isArray(a.target_agents)) {
|
|
388
|
+
return { ok: false, error: "create_loop target_agents must be an array of agent handles." };
|
|
389
|
+
}
|
|
390
|
+
if (a.target_agents.length > 12) {
|
|
391
|
+
return { ok: false, error: "create_loop target_agents exceeds max 12 targets." };
|
|
392
|
+
}
|
|
393
|
+
const dedup: string[] = [];
|
|
394
|
+
const seenTargets = new Set<string>();
|
|
395
|
+
for (const entry of a.target_agents) {
|
|
396
|
+
if (typeof entry !== "string") {
|
|
397
|
+
return { ok: false, error: "create_loop target_agents items must be strings." };
|
|
398
|
+
}
|
|
399
|
+
const handle = entry.trim().replace(/^@+/, "");
|
|
400
|
+
if (!handle) continue;
|
|
401
|
+
if (!SAFE_AGENT_HANDLE_RE.test(handle) || isReservedIdKey(handle) || EVIDENCE_SECRET_RE.test(handle)) {
|
|
402
|
+
return { ok: false, error: `create_loop invalid target agent handle "${handle}".` };
|
|
403
|
+
}
|
|
404
|
+
if (!seenTargets.has(handle)) {
|
|
405
|
+
seenTargets.add(handle);
|
|
406
|
+
dedup.push(handle);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (dedup.length > 0) targetAgents = dedup;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const out: ValidatedCreateLoopBody = {
|
|
413
|
+
channel_id: channelId,
|
|
414
|
+
mode,
|
|
415
|
+
...(intervalMs !== undefined ? { interval_ms: intervalMs } : interval ? { interval } : {}),
|
|
416
|
+
...(targetAgents ? { target_agents: targetAgents } : {}),
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
if (a.stall_minutes !== undefined && a.stall_minutes !== null) {
|
|
420
|
+
if (mode !== "okr_watch") {
|
|
421
|
+
return { ok: false, error: 'create_loop stall_minutes is only supported when mode="okr_watch".' };
|
|
422
|
+
}
|
|
423
|
+
if (
|
|
424
|
+
typeof a.stall_minutes !== "number" ||
|
|
425
|
+
!Number.isSafeInteger(a.stall_minutes) ||
|
|
426
|
+
a.stall_minutes < 5 ||
|
|
427
|
+
a.stall_minutes > 1_440
|
|
428
|
+
) {
|
|
429
|
+
return { ok: false, error: "create_loop stall_minutes must be an integer in 5..1440." };
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (mode === "okr_wake" || mode === "okr_watch") {
|
|
434
|
+
const objectiveId = typeof a.objective_id === "string" ? a.objective_id.trim() : "";
|
|
435
|
+
if (!SAFE_OBJECTIVE_ID_RE.test(objectiveId) || isReservedIdKey(objectiveId) || EVIDENCE_SECRET_RE.test(objectiveId)) {
|
|
436
|
+
return { ok: false, error: `create_loop mode="${mode}" requires a valid objective_id.` };
|
|
437
|
+
}
|
|
438
|
+
out.objective_id = objectiveId;
|
|
439
|
+
if (mode === "okr_watch") {
|
|
440
|
+
if (typeof a.watch_idle === "boolean") out.watch_idle = a.watch_idle;
|
|
441
|
+
if (typeof a.watch_supply === "boolean") out.watch_supply = a.watch_supply;
|
|
442
|
+
if (typeof a.stall_minutes === "number") out.stall_minutes = a.stall_minutes;
|
|
443
|
+
}
|
|
444
|
+
return { ok: true, value: out };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const prompt = typeof a.prompt === "string" ? a.prompt.trim() : "";
|
|
448
|
+
if (!prompt) {
|
|
449
|
+
return { ok: false, error: 'create_loop mode="prompt" requires a non-empty prompt.' };
|
|
450
|
+
}
|
|
451
|
+
if (prompt.length > 500) {
|
|
452
|
+
return { ok: false, error: "create_loop prompt exceeds max 500 characters." };
|
|
453
|
+
}
|
|
454
|
+
if (LOOP_PROMPT_CTRL_RE.test(prompt)) {
|
|
455
|
+
return { ok: false, error: "create_loop prompt contains disallowed control characters." };
|
|
456
|
+
}
|
|
457
|
+
if (EVIDENCE_SECRET_RE.test(prompt)) {
|
|
458
|
+
return { ok: false, error: "create_loop prompt rejected: appears to contain a secret or token." };
|
|
459
|
+
}
|
|
460
|
+
out.prompt = prompt;
|
|
461
|
+
return { ok: true, value: out };
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export const OKR_KR_METRIC_TYPES = ["count", "bool", "percent"] as const;
|
|
465
|
+
export type OkrKrMetricType = (typeof OKR_KR_METRIC_TYPES)[number];
|
|
466
|
+
export const OKR_KR_RISK_LEVELS = ["green", "yellow", "red"] as const;
|
|
467
|
+
export type OkrKrRiskLevel = (typeof OKR_KR_RISK_LEVELS)[number];
|
|
468
|
+
|
|
469
|
+
export interface ValidatedOkrAddKrInput {
|
|
470
|
+
objective_id: string;
|
|
471
|
+
body: {
|
|
472
|
+
title: string;
|
|
473
|
+
metric_type: OkrKrMetricType;
|
|
474
|
+
target: number;
|
|
475
|
+
current?: number;
|
|
476
|
+
risk_level?: OkrKrRiskLevel;
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export function validateOkrAddKrInput(
|
|
481
|
+
args: unknown,
|
|
482
|
+
): { ok: true; value: ValidatedOkrAddKrInput } | { ok: false; error: string } {
|
|
483
|
+
if (hasOwnProto(args)) {
|
|
484
|
+
return { ok: false, error: 'okr_add_kr arguments cannot define own "__proto__".' };
|
|
485
|
+
}
|
|
486
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
487
|
+
? (args as Record<string, unknown>)
|
|
488
|
+
: {};
|
|
489
|
+
const objectiveId = typeof a.objective_id === "string" ? a.objective_id.trim() : "";
|
|
490
|
+
if (
|
|
491
|
+
!objectiveId ||
|
|
492
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(objectiveId) ||
|
|
493
|
+
isReservedIdKey(objectiveId) ||
|
|
494
|
+
EVIDENCE_CTRL_RE.test(objectiveId) ||
|
|
495
|
+
EVIDENCE_SECRET_RE.test(objectiveId)
|
|
496
|
+
) {
|
|
497
|
+
return { ok: false, error: "okr_add_kr requires a valid objective_id." };
|
|
498
|
+
}
|
|
499
|
+
const rawTitle = typeof a.title === "string" ? a.title : "";
|
|
500
|
+
const title = rawTitle.replace(/[\r\n\t]+/g, " ").trim();
|
|
501
|
+
if (!title || title.length > 200 || EVIDENCE_CTRL_RE.test(title)) {
|
|
502
|
+
return { ok: false, error: "okr_add_kr title must be a non-empty string (<=200 chars) without control characters." };
|
|
503
|
+
}
|
|
504
|
+
if (EVIDENCE_SECRET_RE.test(title)) {
|
|
505
|
+
return { ok: false, error: "okr_add_kr title rejected: appears to contain a secret or token." };
|
|
506
|
+
}
|
|
507
|
+
const mt = a.metric_type === undefined ? "count" : a.metric_type;
|
|
508
|
+
if (typeof mt !== "string" || !(OKR_KR_METRIC_TYPES as readonly string[]).includes(mt)) {
|
|
509
|
+
return { ok: false, error: "okr_add_kr metric_type must be count|bool|percent." };
|
|
510
|
+
}
|
|
511
|
+
const metricType = mt as OkrKrMetricType;
|
|
512
|
+
if (typeof a.target !== "number" || !Number.isFinite(a.target) || a.target < 0 || a.target > 1_000_000) {
|
|
513
|
+
return { ok: false, error: "okr_add_kr target must be a finite number in 0..1000000." };
|
|
514
|
+
}
|
|
515
|
+
if (metricType === "bool" && a.target !== 0 && a.target !== 1) {
|
|
516
|
+
return { ok: false, error: "okr_add_kr bool target must be 0 or 1." };
|
|
517
|
+
}
|
|
518
|
+
if (metricType === "percent" && a.target > 100) {
|
|
519
|
+
return { ok: false, error: "okr_add_kr percent target must be <= 100." };
|
|
520
|
+
}
|
|
521
|
+
let current: number | undefined;
|
|
522
|
+
if (a.current !== undefined && a.current !== null) {
|
|
523
|
+
if (typeof a.current !== "number" || !Number.isFinite(a.current) || a.current < 0 || a.current > 1_000_000) {
|
|
524
|
+
return { ok: false, error: "okr_add_kr current must be a finite number in 0..1000000." };
|
|
525
|
+
}
|
|
526
|
+
if (metricType === "bool" && a.current !== 0 && a.current !== 1) {
|
|
527
|
+
return { ok: false, error: "okr_add_kr bool current must be 0 or 1." };
|
|
528
|
+
}
|
|
529
|
+
if (metricType === "percent" && a.current > 100) {
|
|
530
|
+
return { ok: false, error: "okr_add_kr percent current must be <= 100." };
|
|
531
|
+
}
|
|
532
|
+
current = a.current;
|
|
533
|
+
}
|
|
534
|
+
let riskLevel: OkrKrRiskLevel | undefined;
|
|
535
|
+
if (a.risk_level !== undefined && a.risk_level !== null) {
|
|
536
|
+
if (typeof a.risk_level !== "string" || !(OKR_KR_RISK_LEVELS as readonly string[]).includes(a.risk_level)) {
|
|
537
|
+
return { ok: false, error: "okr_add_kr risk_level must be green|yellow|red." };
|
|
538
|
+
}
|
|
539
|
+
riskLevel = a.risk_level as OkrKrRiskLevel;
|
|
540
|
+
}
|
|
541
|
+
return {
|
|
542
|
+
ok: true,
|
|
543
|
+
value: {
|
|
544
|
+
objective_id: objectiveId,
|
|
545
|
+
body: {
|
|
546
|
+
title,
|
|
547
|
+
metric_type: metricType,
|
|
548
|
+
target: a.target,
|
|
549
|
+
...(current !== undefined ? { current } : {}),
|
|
550
|
+
...(riskLevel !== undefined ? { risk_level: riskLevel } : {}),
|
|
551
|
+
},
|
|
552
|
+
},
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export interface ValidatedOkrKrUpdateBody {
|
|
557
|
+
title?: string;
|
|
558
|
+
metric_type?: OkrKrMetricType;
|
|
559
|
+
target?: number;
|
|
560
|
+
current?: number;
|
|
561
|
+
risk_level?: OkrKrRiskLevel;
|
|
562
|
+
if_match_updated_at?: string;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
export interface ValidatedOkrKrUpdateResult {
|
|
566
|
+
kr_id: string;
|
|
567
|
+
body: ValidatedOkrKrUpdateBody;
|
|
568
|
+
useMetadataEndpoint: boolean;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function validateOkrKrMutationInput(
|
|
572
|
+
args: unknown,
|
|
573
|
+
toolLabel: "okr_set_kr_progress" | "okr_update_kr",
|
|
574
|
+
forceMetadataEndpoint: boolean,
|
|
575
|
+
): { ok: true; value: ValidatedOkrKrUpdateResult } | { ok: false; error: string } {
|
|
576
|
+
if (hasOwnProto(args)) {
|
|
577
|
+
return { ok: false, error: `${toolLabel} arguments cannot define own "__proto__".` };
|
|
578
|
+
}
|
|
579
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
580
|
+
? (args as Record<string, unknown>)
|
|
581
|
+
: {};
|
|
582
|
+
const krId = typeof a.kr_id === "string" ? a.kr_id.trim() : "";
|
|
583
|
+
if (
|
|
584
|
+
!krId ||
|
|
585
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(krId) ||
|
|
586
|
+
isReservedIdKey(krId) ||
|
|
587
|
+
EVIDENCE_CTRL_RE.test(krId) ||
|
|
588
|
+
EVIDENCE_SECRET_RE.test(krId)
|
|
589
|
+
) {
|
|
590
|
+
return { ok: false, error: `${toolLabel} requires a valid kr_id.` };
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const body: ValidatedOkrKrUpdateBody = {};
|
|
594
|
+
|
|
595
|
+
if (a.title !== undefined && a.title !== null) {
|
|
596
|
+
if (typeof a.title !== "string") {
|
|
597
|
+
return { ok: false, error: `${toolLabel} title must be a non-empty string (1..200 chars).` };
|
|
598
|
+
}
|
|
599
|
+
if (EVIDENCE_CTRL_RE.test(a.title)) {
|
|
600
|
+
return { ok: false, error: `${toolLabel} title must not contain control characters.` };
|
|
601
|
+
}
|
|
602
|
+
if (EVIDENCE_SECRET_RE.test(a.title)) {
|
|
603
|
+
return { ok: false, error: `${toolLabel} title rejected: appears to contain a secret or token.` };
|
|
604
|
+
}
|
|
605
|
+
const trimmedTitle = a.title.trim();
|
|
606
|
+
if (!trimmedTitle || trimmedTitle.length > 200) {
|
|
607
|
+
return { ok: false, error: `${toolLabel} title must be 1..200 chars.` };
|
|
608
|
+
}
|
|
609
|
+
body.title = trimmedTitle;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
let metricType: OkrKrMetricType | undefined;
|
|
613
|
+
if (a.metric_type !== undefined && a.metric_type !== null) {
|
|
614
|
+
if (typeof a.metric_type !== "string" || !(OKR_KR_METRIC_TYPES as readonly string[]).includes(a.metric_type)) {
|
|
615
|
+
return { ok: false, error: `${toolLabel} metric_type must be count|bool|percent.` };
|
|
616
|
+
}
|
|
617
|
+
metricType = a.metric_type as OkrKrMetricType;
|
|
618
|
+
body.metric_type = metricType;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (a.target !== undefined && a.target !== null) {
|
|
622
|
+
if (typeof a.target !== "number" || !Number.isFinite(a.target) || a.target < 0 || a.target > 1_000_000) {
|
|
623
|
+
return { ok: false, error: `${toolLabel} target must be a finite number in 0..1000000.` };
|
|
624
|
+
}
|
|
625
|
+
if (metricType === "bool" && a.target !== 0 && a.target !== 1) {
|
|
626
|
+
return { ok: false, error: `${toolLabel} bool target must be 0 or 1.` };
|
|
627
|
+
}
|
|
628
|
+
if (metricType === "percent" && a.target > 100) {
|
|
629
|
+
return { ok: false, error: `${toolLabel} percent target must be <= 100.` };
|
|
630
|
+
}
|
|
631
|
+
body.target = a.target;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (a.current !== undefined && a.current !== null) {
|
|
635
|
+
if (typeof a.current !== "number" || !Number.isFinite(a.current) || a.current < 0 || a.current > 1_000_000) {
|
|
636
|
+
return { ok: false, error: `${toolLabel} current must be a finite number in 0..1000000.` };
|
|
637
|
+
}
|
|
638
|
+
if (metricType === "bool" && a.current !== 0 && a.current !== 1) {
|
|
639
|
+
return { ok: false, error: `${toolLabel} bool current must be 0 or 1.` };
|
|
640
|
+
}
|
|
641
|
+
if (metricType === "percent" && a.current > 100) {
|
|
642
|
+
return { ok: false, error: `${toolLabel} percent current must be <= 100.` };
|
|
643
|
+
}
|
|
644
|
+
body.current = a.current;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
if (a.risk_level !== undefined && a.risk_level !== null) {
|
|
648
|
+
if (typeof a.risk_level !== "string" || !(OKR_KR_RISK_LEVELS as readonly string[]).includes(a.risk_level)) {
|
|
649
|
+
return { ok: false, error: `${toolLabel} risk_level must be green|yellow|red.` };
|
|
650
|
+
}
|
|
651
|
+
body.risk_level = a.risk_level as OkrKrRiskLevel;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const hasMutableField =
|
|
655
|
+
body.title !== undefined ||
|
|
656
|
+
body.metric_type !== undefined ||
|
|
657
|
+
body.target !== undefined ||
|
|
658
|
+
body.current !== undefined ||
|
|
659
|
+
body.risk_level !== undefined;
|
|
660
|
+
if (!hasMutableField) {
|
|
661
|
+
return {
|
|
662
|
+
ok: false,
|
|
663
|
+
error: `${toolLabel} requires at least one of current, risk_level, title, metric_type, or target.`,
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (a.if_match_updated_at !== undefined && a.if_match_updated_at !== null) {
|
|
668
|
+
const verCheck = validateTaskVersionToken(a.if_match_updated_at);
|
|
669
|
+
if (!verCheck.ok) {
|
|
670
|
+
return { ok: false, error: `${toolLabel} invalid if_match_updated_at: ${verCheck.error}` };
|
|
671
|
+
}
|
|
672
|
+
if (EVIDENCE_SECRET_RE.test(verCheck.value)) {
|
|
673
|
+
return { ok: false, error: `${toolLabel} if_match_updated_at rejected: appears to contain a secret or token.` };
|
|
674
|
+
}
|
|
675
|
+
body.if_match_updated_at = verCheck.value;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const useMetadataEndpoint =
|
|
679
|
+
forceMetadataEndpoint ||
|
|
680
|
+
body.title !== undefined ||
|
|
681
|
+
body.metric_type !== undefined ||
|
|
682
|
+
body.target !== undefined ||
|
|
683
|
+
body.current === undefined;
|
|
684
|
+
|
|
685
|
+
return {
|
|
686
|
+
ok: true,
|
|
687
|
+
value: {
|
|
688
|
+
kr_id: krId,
|
|
689
|
+
body,
|
|
690
|
+
useMetadataEndpoint,
|
|
691
|
+
},
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
export function validateOkrSetKrProgressInput(
|
|
696
|
+
args: unknown,
|
|
697
|
+
): { ok: true; value: ValidatedOkrKrUpdateResult } | { ok: false; error: string } {
|
|
698
|
+
return validateOkrKrMutationInput(args, "okr_set_kr_progress", false);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
export function validateOkrUpdateKrInput(
|
|
702
|
+
args: unknown,
|
|
703
|
+
): { ok: true; value: ValidatedOkrKrUpdateResult } | { ok: false; error: string } {
|
|
704
|
+
return validateOkrKrMutationInput(args, "okr_update_kr", true);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
export function validateOkrTaskTitleInput(
|
|
708
|
+
raw: unknown,
|
|
709
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
710
|
+
if (typeof raw !== "string") {
|
|
711
|
+
return { ok: false, error: "title must be a non-empty string (1..200 chars)" };
|
|
712
|
+
}
|
|
713
|
+
if (EVIDENCE_CTRL_RE.test(raw)) {
|
|
714
|
+
return { ok: false, error: "title must not contain control characters" };
|
|
715
|
+
}
|
|
716
|
+
if (EVIDENCE_SECRET_RE.test(raw)) {
|
|
717
|
+
return { ok: false, error: "title rejected: appears to contain a secret or token" };
|
|
718
|
+
}
|
|
719
|
+
const trimmed = raw.trim();
|
|
720
|
+
if (!trimmed || trimmed.length > 200) {
|
|
721
|
+
return { ok: false, error: "title must be 1..200 chars" };
|
|
722
|
+
}
|
|
723
|
+
return { ok: true, value: trimmed };
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
export function validateOkrTaskDueInput(
|
|
727
|
+
raw: unknown,
|
|
728
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
729
|
+
if (raw === null || raw === "") {
|
|
730
|
+
return { ok: true, value: "" };
|
|
731
|
+
}
|
|
732
|
+
if (typeof raw !== "string") {
|
|
733
|
+
return { ok: false, error: "due must be an ISO-8601 date/timestamp string (<= 32 chars) or empty string to clear" };
|
|
734
|
+
}
|
|
735
|
+
if (EVIDENCE_CTRL_RE.test(raw)) {
|
|
736
|
+
return { ok: false, error: "due must not contain control characters" };
|
|
737
|
+
}
|
|
738
|
+
if (EVIDENCE_SECRET_RE.test(raw)) {
|
|
739
|
+
return { ok: false, error: "due rejected: appears to contain a secret or token" };
|
|
740
|
+
}
|
|
741
|
+
const trimmed = raw.trim();
|
|
742
|
+
if (!trimmed) {
|
|
743
|
+
return { ok: true, value: "" };
|
|
744
|
+
}
|
|
745
|
+
if (trimmed.length > 32 || !Number.isFinite(Date.parse(trimmed))) {
|
|
746
|
+
return { ok: false, error: "due must be a valid ISO-8601 date/timestamp (<= 32 chars) or empty string to clear" };
|
|
747
|
+
}
|
|
748
|
+
return { ok: true, value: trimmed };
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
const OKR_TASK_CONTRIBUTES_CAP = 5;
|
|
752
|
+
|
|
753
|
+
export function validateOkrTaskAssigneeInput(
|
|
754
|
+
raw: unknown,
|
|
755
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
756
|
+
if (typeof raw !== "string") {
|
|
757
|
+
return { ok: false, error: "assignee must be a non-empty agent handle string" };
|
|
758
|
+
}
|
|
759
|
+
const trimmed = raw.trim().replace(/^@+/, "");
|
|
760
|
+
if (!trimmed || trimmed.length > 128 || EVIDENCE_CTRL_RE.test(trimmed)) {
|
|
761
|
+
return { ok: false, error: "assignee must be a non-empty agent handle (<=128 chars)" };
|
|
762
|
+
}
|
|
763
|
+
if (trimmed.includes("@")) {
|
|
764
|
+
return { ok: false, error: "assignee rejected: must be an agent handle, never an email address" };
|
|
765
|
+
}
|
|
766
|
+
if (EVIDENCE_SECRET_RE.test(trimmed)) {
|
|
767
|
+
return { ok: false, error: "assignee rejected: appears to contain a secret or token" };
|
|
768
|
+
}
|
|
769
|
+
if (!SAFE_CHANNEL_OR_LOOP_ID_RE.test(trimmed) || isReservedIdKey(trimmed)) {
|
|
770
|
+
return { ok: false, error: `assignee "${trimmed}" contains invalid characters` };
|
|
771
|
+
}
|
|
772
|
+
return { ok: true, value: trimmed };
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
export function validateOkrTaskContributesToInput(
|
|
776
|
+
raw: unknown,
|
|
777
|
+
): { ok: true; value: string[] } | { ok: false; error: string } {
|
|
778
|
+
if (raw === null) return { ok: true, value: [] };
|
|
779
|
+
if (!Array.isArray(raw)) {
|
|
780
|
+
return {
|
|
781
|
+
ok: false,
|
|
782
|
+
error: `contributes_to must be an array of KR IDs (0..${OKR_TASK_CONTRIBUTES_CAP} items; pass [] to clear)`,
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
if (raw.length === 0) return { ok: true, value: [] };
|
|
786
|
+
if (raw.length > OKR_TASK_CONTRIBUTES_CAP) {
|
|
787
|
+
return { ok: false, error: `contributes_to exceeds max ${OKR_TASK_CONTRIBUTES_CAP} KR IDs` };
|
|
788
|
+
}
|
|
789
|
+
const out: string[] = [];
|
|
790
|
+
const seen = new Set<string>();
|
|
791
|
+
for (let i = 0; i < raw.length; i++) {
|
|
792
|
+
const item = raw[i];
|
|
793
|
+
if (typeof item !== "string") {
|
|
794
|
+
return { ok: false, error: `contributes_to[${i}] must be a non-empty KR ID string` };
|
|
795
|
+
}
|
|
796
|
+
const id = item.trim();
|
|
797
|
+
if (!id || id.length > 128 || EVIDENCE_CTRL_RE.test(id)) {
|
|
798
|
+
return { ok: false, error: `contributes_to[${i}] must be a non-empty KR ID (<=128 chars)` };
|
|
799
|
+
}
|
|
800
|
+
if (EVIDENCE_SECRET_RE.test(id)) {
|
|
801
|
+
return { ok: false, error: `contributes_to[${i}] rejected: appears to contain a secret or token` };
|
|
802
|
+
}
|
|
803
|
+
if (!SAFE_CHANNEL_OR_LOOP_ID_RE.test(id) || isReservedIdKey(id)) {
|
|
804
|
+
return { ok: false, error: `contributes_to[${i}] contains invalid KR ID characters` };
|
|
805
|
+
}
|
|
806
|
+
if (!seen.has(id)) {
|
|
807
|
+
seen.add(id);
|
|
808
|
+
out.push(id);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
return { ok: true, value: out };
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
const OKR_TASK_DEPENDS_ON_CAP = 20;
|
|
815
|
+
const OKR_LINKED_CHANNEL_DOCS_CAP = 10;
|
|
816
|
+
const OKR_DOC_CHANNEL_ID_RE = /^[A-Za-z0-9_-]{1,80}$/;
|
|
817
|
+
const OKR_DOC_ID_RE = /^[A-Za-z0-9_-]{1,120}$/;
|
|
818
|
+
const OKR_NARRATIVE_MAX = 2000;
|
|
819
|
+
|
|
820
|
+
export interface OkrLinkedChannelDocInput {
|
|
821
|
+
channel_id: string;
|
|
822
|
+
doc_id: string;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
export function validateOkrTaskDependsOnInput(
|
|
826
|
+
raw: unknown,
|
|
827
|
+
selfTaskId?: string,
|
|
828
|
+
): { ok: true; value: string[] } | { ok: false; error: string } {
|
|
829
|
+
if (raw === null) return { ok: true, value: [] };
|
|
830
|
+
if (!Array.isArray(raw)) {
|
|
831
|
+
return {
|
|
832
|
+
ok: false,
|
|
833
|
+
error: `depends_on must be an array of task IDs (0..${OKR_TASK_DEPENDS_ON_CAP} items; pass [] to clear)`,
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
if (raw.length === 0) return { ok: true, value: [] };
|
|
837
|
+
if (raw.length > OKR_TASK_DEPENDS_ON_CAP) {
|
|
838
|
+
return { ok: false, error: `depends_on exceeds max ${OKR_TASK_DEPENDS_ON_CAP} task IDs` };
|
|
839
|
+
}
|
|
840
|
+
const normalizedSelf = typeof selfTaskId === "string" ? selfTaskId.trim() : "";
|
|
841
|
+
const out: string[] = [];
|
|
842
|
+
const seen = new Set<string>();
|
|
843
|
+
for (let i = 0; i < raw.length; i++) {
|
|
844
|
+
const item = raw[i];
|
|
845
|
+
if (typeof item !== "string") {
|
|
846
|
+
return { ok: false, error: `depends_on[${i}] must be a non-empty task ID string` };
|
|
847
|
+
}
|
|
848
|
+
const id = item.trim();
|
|
849
|
+
if (!id || id.length > 128 || EVIDENCE_CTRL_RE.test(id)) {
|
|
850
|
+
return { ok: false, error: `depends_on[${i}] must be a non-empty task ID (<=128 chars)` };
|
|
851
|
+
}
|
|
852
|
+
if (EVIDENCE_SECRET_RE.test(id)) {
|
|
853
|
+
return { ok: false, error: `depends_on[${i}] rejected: appears to contain a secret or token` };
|
|
854
|
+
}
|
|
855
|
+
if (!SAFE_CHANNEL_OR_LOOP_ID_RE.test(id) || isReservedIdKey(id)) {
|
|
856
|
+
return { ok: false, error: `depends_on[${i}] contains invalid task ID characters` };
|
|
857
|
+
}
|
|
858
|
+
if (normalizedSelf && id === normalizedSelf) {
|
|
859
|
+
return { ok: false, error: `depends_on[${i}] cannot reference the task itself (${id})` };
|
|
860
|
+
}
|
|
861
|
+
if (!seen.has(id)) {
|
|
862
|
+
seen.add(id);
|
|
863
|
+
out.push(id);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
return { ok: true, value: out };
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
export function validateOkrLinkedChannelDocsInput(
|
|
870
|
+
raw: unknown,
|
|
871
|
+
expectedChannelId?: string,
|
|
872
|
+
): { ok: true; value: OkrLinkedChannelDocInput[] } | { ok: false; error: string } {
|
|
873
|
+
if (raw === null) return { ok: true, value: [] };
|
|
874
|
+
if (!Array.isArray(raw)) {
|
|
875
|
+
return {
|
|
876
|
+
ok: false,
|
|
877
|
+
error: `linked_channel_docs must be an array of {channel_id, doc_id} objects (0..${OKR_LINKED_CHANNEL_DOCS_CAP} items; pass [] to clear)`,
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
if (raw.length === 0) return { ok: true, value: [] };
|
|
881
|
+
if (raw.length > OKR_LINKED_CHANNEL_DOCS_CAP) {
|
|
882
|
+
return { ok: false, error: `linked_channel_docs exceeds max ${OKR_LINKED_CHANNEL_DOCS_CAP} items` };
|
|
883
|
+
}
|
|
884
|
+
const requiredChannel = typeof expectedChannelId === "string" ? expectedChannelId.trim() : "";
|
|
885
|
+
const out: OkrLinkedChannelDocInput[] = [];
|
|
886
|
+
const seen = new Set<string>();
|
|
887
|
+
for (let i = 0; i < raw.length; i++) {
|
|
888
|
+
const item = raw[i];
|
|
889
|
+
if (!item || typeof item !== "object" || Array.isArray(item) || hasOwnProto(item)) {
|
|
890
|
+
return { ok: false, error: `linked_channel_docs[${i}] must be an object with channel_id and doc_id` };
|
|
891
|
+
}
|
|
892
|
+
const rawChannelId = (item as Record<string, unknown>).channel_id;
|
|
893
|
+
const rawDocId = (item as Record<string, unknown>).doc_id;
|
|
894
|
+
if (typeof rawChannelId !== "string" || typeof rawDocId !== "string") {
|
|
895
|
+
return { ok: false, error: `linked_channel_docs[${i}] requires string channel_id and doc_id` };
|
|
896
|
+
}
|
|
897
|
+
const channelId = rawChannelId.trim();
|
|
898
|
+
const docId = rawDocId.trim();
|
|
899
|
+
if (
|
|
900
|
+
!channelId ||
|
|
901
|
+
!OKR_DOC_CHANNEL_ID_RE.test(channelId) ||
|
|
902
|
+
/^dm[-:]/i.test(channelId) ||
|
|
903
|
+
channelId === "direct" ||
|
|
904
|
+
isReservedIdKey(channelId)
|
|
905
|
+
) {
|
|
906
|
+
return {
|
|
907
|
+
ok: false,
|
|
908
|
+
error: `linked_channel_docs[${i}].channel_id must be a valid non-DM room ID (1..80 chars)`,
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
if (EVIDENCE_SECRET_RE.test(channelId) || EVIDENCE_SECRET_RE.test(docId)) {
|
|
912
|
+
return { ok: false, error: `linked_channel_docs[${i}] rejected: appears to contain a secret or token` };
|
|
913
|
+
}
|
|
914
|
+
if (!docId || !OKR_DOC_ID_RE.test(docId) || isReservedIdKey(docId)) {
|
|
915
|
+
return { ok: false, error: `linked_channel_docs[${i}].doc_id must match ^[A-Za-z0-9_-]{1,120}$` };
|
|
916
|
+
}
|
|
917
|
+
if (requiredChannel && channelId !== requiredChannel) {
|
|
918
|
+
return {
|
|
919
|
+
ok: false,
|
|
920
|
+
error: `linked_channel_docs[${i}].channel_id (${channelId}) must match discussion_channel_id (${requiredChannel})`,
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
const key = `${channelId}:${docId}`;
|
|
924
|
+
if (!seen.has(key)) {
|
|
925
|
+
seen.add(key);
|
|
926
|
+
out.push({ channel_id: channelId, doc_id: docId });
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
return { ok: true, value: out };
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
export function validateOkrObjectiveNarrativeInput(
|
|
933
|
+
raw: unknown,
|
|
934
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
935
|
+
if (raw === null || raw === "") return { ok: true, value: "" };
|
|
936
|
+
if (typeof raw !== "string") {
|
|
937
|
+
return { ok: false, error: "narrative must be a string" };
|
|
938
|
+
}
|
|
939
|
+
if (raw.length > OKR_NARRATIVE_MAX || EVIDENCE_CTRL_RE.test(raw)) {
|
|
940
|
+
return { ok: false, error: `narrative must be <= ${OKR_NARRATIVE_MAX} chars without control characters` };
|
|
941
|
+
}
|
|
942
|
+
if (EVIDENCE_SECRET_RE.test(raw)) {
|
|
943
|
+
return { ok: false, error: "narrative rejected: appears to contain a secret or token" };
|
|
944
|
+
}
|
|
945
|
+
return { ok: true, value: raw.trim() };
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
const OKR_OBJECTIVE_TITLE_MAX = 200;
|
|
949
|
+
const OKR_COMPLETION_SUMMARY_MAX = 280;
|
|
950
|
+
const OKR_OBJECTIVE_STATUSES = ["active", "done", "abandoned"] as const;
|
|
951
|
+
const OKR_OBJECTIVE_HORIZONS = ["week", "month", "Q"] as const;
|
|
952
|
+
|
|
953
|
+
export function validateOkrCompletionSummaryInput(
|
|
954
|
+
raw: unknown,
|
|
955
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
956
|
+
if (raw === null || raw === "") return { ok: true, value: "" };
|
|
957
|
+
if (typeof raw !== "string") {
|
|
958
|
+
return { ok: false, error: "completion_summary must be a string" };
|
|
959
|
+
}
|
|
960
|
+
if (EVIDENCE_CTRL_RE.test(raw)) {
|
|
961
|
+
return { ok: false, error: "completion_summary must not contain control characters" };
|
|
962
|
+
}
|
|
963
|
+
if (EVIDENCE_SECRET_RE.test(raw)) {
|
|
964
|
+
return { ok: false, error: "completion_summary rejected: appears to contain a secret or token" };
|
|
965
|
+
}
|
|
966
|
+
const trimmed = raw.trim();
|
|
967
|
+
if (trimmed.length > OKR_COMPLETION_SUMMARY_MAX) {
|
|
968
|
+
return { ok: false, error: `completion_summary exceeds max ${OKR_COMPLETION_SUMMARY_MAX} chars` };
|
|
969
|
+
}
|
|
970
|
+
return { ok: true, value: trimmed };
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
export interface ValidatedOkrUpdateObjectiveInput {
|
|
974
|
+
objective_id: string;
|
|
975
|
+
body: Record<string, unknown>;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
export function validateOkrUpdateObjectiveInput(
|
|
979
|
+
args: unknown,
|
|
980
|
+
): { ok: true; value: ValidatedOkrUpdateObjectiveInput } | { ok: false; error: string } {
|
|
981
|
+
if (hasOwnProto(args)) {
|
|
982
|
+
return { ok: false, error: 'okr_update_objective arguments cannot define own "__proto__"' };
|
|
983
|
+
}
|
|
984
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
985
|
+
? (args as Record<string, unknown>)
|
|
986
|
+
: {};
|
|
987
|
+
const objectiveId = typeof a.objective_id === "string" ? a.objective_id.trim() : "";
|
|
988
|
+
if (
|
|
989
|
+
!objectiveId ||
|
|
990
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(objectiveId) ||
|
|
991
|
+
isReservedIdKey(objectiveId) ||
|
|
992
|
+
EVIDENCE_CTRL_RE.test(objectiveId) ||
|
|
993
|
+
EVIDENCE_SECRET_RE.test(objectiveId)
|
|
994
|
+
) {
|
|
995
|
+
return { ok: false, error: "objective_id must be a valid non-empty identifier" };
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const body: Record<string, unknown> = {};
|
|
999
|
+
|
|
1000
|
+
if (a.title !== undefined) {
|
|
1001
|
+
if (typeof a.title !== "string") {
|
|
1002
|
+
return { ok: false, error: "title must be a non-empty string (<= 200 chars)" };
|
|
1003
|
+
}
|
|
1004
|
+
if (EVIDENCE_CTRL_RE.test(a.title)) {
|
|
1005
|
+
return { ok: false, error: "title must not contain control characters" };
|
|
1006
|
+
}
|
|
1007
|
+
if (EVIDENCE_SECRET_RE.test(a.title)) {
|
|
1008
|
+
return { ok: false, error: "title rejected: appears to contain a secret or token" };
|
|
1009
|
+
}
|
|
1010
|
+
const trimmedTitle = a.title.trim();
|
|
1011
|
+
if (!trimmedTitle || trimmedTitle.length > OKR_OBJECTIVE_TITLE_MAX) {
|
|
1012
|
+
return { ok: false, error: `title must be 1..${OKR_OBJECTIVE_TITLE_MAX} chars` };
|
|
1013
|
+
}
|
|
1014
|
+
body.title = trimmedTitle;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
if (a.status !== undefined) {
|
|
1018
|
+
if (
|
|
1019
|
+
typeof a.status !== "string" ||
|
|
1020
|
+
!(OKR_OBJECTIVE_STATUSES as readonly string[]).includes(a.status)
|
|
1021
|
+
) {
|
|
1022
|
+
return { ok: false, error: "status must be active, done, or abandoned" };
|
|
1023
|
+
}
|
|
1024
|
+
body.status = a.status;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
if (a.horizon !== undefined) {
|
|
1028
|
+
if (
|
|
1029
|
+
typeof a.horizon !== "string" ||
|
|
1030
|
+
!(OKR_OBJECTIVE_HORIZONS as readonly string[]).includes(a.horizon)
|
|
1031
|
+
) {
|
|
1032
|
+
return { ok: false, error: "horizon must be week, month, or Q" };
|
|
1033
|
+
}
|
|
1034
|
+
body.horizon = a.horizon;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
if (a.due !== undefined) {
|
|
1038
|
+
if (a.due === null || a.due === "") {
|
|
1039
|
+
body.due = "";
|
|
1040
|
+
} else if (typeof a.due === "string") {
|
|
1041
|
+
if (EVIDENCE_CTRL_RE.test(a.due)) {
|
|
1042
|
+
return { ok: false, error: "due must not contain control characters" };
|
|
1043
|
+
}
|
|
1044
|
+
if (EVIDENCE_SECRET_RE.test(a.due)) {
|
|
1045
|
+
return { ok: false, error: "due rejected: appears to contain a secret or token" };
|
|
1046
|
+
}
|
|
1047
|
+
const trimmedDue = a.due.trim();
|
|
1048
|
+
if (!trimmedDue) {
|
|
1049
|
+
body.due = "";
|
|
1050
|
+
} else if (trimmedDue.length > 32 || !Number.isFinite(Date.parse(trimmedDue))) {
|
|
1051
|
+
return { ok: false, error: "due must be a valid ISO-8601 date/timestamp (<= 32 chars) or empty string to clear" };
|
|
1052
|
+
} else {
|
|
1053
|
+
body.due = trimmedDue;
|
|
1054
|
+
}
|
|
1055
|
+
} else {
|
|
1056
|
+
return { ok: false, error: "due must be an ISO-8601 date string or empty string to clear" };
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
if (a.narrative !== undefined) {
|
|
1061
|
+
const narCheck = validateOkrObjectiveNarrativeInput(a.narrative);
|
|
1062
|
+
if (!narCheck.ok) return narCheck;
|
|
1063
|
+
body.narrative = narCheck.value;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
if (a.completion_summary !== undefined) {
|
|
1067
|
+
const sumCheck = validateOkrCompletionSummaryInput(a.completion_summary);
|
|
1068
|
+
if (!sumCheck.ok) return sumCheck;
|
|
1069
|
+
body.completion_summary = sumCheck.value;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
if (a.linked_channel_docs !== undefined) {
|
|
1073
|
+
const docsCheck = validateOkrLinkedChannelDocsInput(a.linked_channel_docs);
|
|
1074
|
+
if (!docsCheck.ok) return docsCheck;
|
|
1075
|
+
if (docsCheck.value.length > 1) {
|
|
1076
|
+
const firstChan = docsCheck.value[0].channel_id;
|
|
1077
|
+
if (docsCheck.value.some((d) => d.channel_id !== firstChan)) {
|
|
1078
|
+
return { ok: false, error: "linked_channel_docs items on an Objective must all reference the same channel_id" };
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
body.linked_channel_docs = docsCheck.value;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
if (Object.keys(body).length === 0) {
|
|
1085
|
+
return {
|
|
1086
|
+
ok: false,
|
|
1087
|
+
error: "at least one mutable field (title, status, horizon, due, narrative, completion_summary, linked_channel_docs) is required",
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
if (a.if_match_updated_at !== undefined && a.if_match_updated_at !== null) {
|
|
1092
|
+
const verCheck = validateTaskVersionToken(a.if_match_updated_at);
|
|
1093
|
+
if (!verCheck.ok) {
|
|
1094
|
+
return { ok: false, error: verCheck.error };
|
|
1095
|
+
}
|
|
1096
|
+
body.if_match_updated_at = verCheck.value;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
return { ok: true, value: { objective_id: objectiveId, body } };
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
export function validateOkrArchiveObjectiveInput(
|
|
1104
|
+
args: unknown,
|
|
1105
|
+
): { ok: true; value: { objective_id: string; body: Record<string, unknown> } } | { ok: false; error: string } {
|
|
1106
|
+
if (hasOwnProto(args)) {
|
|
1107
|
+
return { ok: false, error: 'archive_objective arguments cannot define own "__proto__"' };
|
|
1108
|
+
}
|
|
1109
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1110
|
+
? (args as Record<string, unknown>)
|
|
1111
|
+
: {};
|
|
1112
|
+
const objectiveId = typeof a.objective_id === "string" ? a.objective_id.trim() : "";
|
|
1113
|
+
if (
|
|
1114
|
+
!objectiveId ||
|
|
1115
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(objectiveId) ||
|
|
1116
|
+
isReservedIdKey(objectiveId) ||
|
|
1117
|
+
EVIDENCE_CTRL_RE.test(objectiveId) ||
|
|
1118
|
+
EVIDENCE_SECRET_RE.test(objectiveId)
|
|
1119
|
+
) {
|
|
1120
|
+
return { ok: false, error: "objective_id must be a valid non-empty identifier" };
|
|
1121
|
+
}
|
|
1122
|
+
const body: Record<string, unknown> = {};
|
|
1123
|
+
if (a.completion_summary !== undefined && a.completion_summary !== null) {
|
|
1124
|
+
const sumCheck = validateOkrCompletionSummaryInput(a.completion_summary);
|
|
1125
|
+
if (!sumCheck.ok) return sumCheck;
|
|
1126
|
+
if (sumCheck.value) {
|
|
1127
|
+
body.completion_summary = sumCheck.value;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
return { ok: true, value: { objective_id: objectiveId, body } };
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
export interface ValidatedListLoopsFilter {
|
|
1134
|
+
channel_id?: string;
|
|
1135
|
+
status?: "active" | "paused";
|
|
1136
|
+
mode?: CreateLoopMode;
|
|
1137
|
+
objective_id?: string;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
export function validateListLoopsInput(
|
|
1141
|
+
args: unknown,
|
|
1142
|
+
): { ok: true; value: ValidatedListLoopsFilter } | { ok: false; error: string } {
|
|
1143
|
+
if (hasOwnProto(args)) {
|
|
1144
|
+
return { ok: false, error: 'list_loops arguments cannot define own "__proto__".' };
|
|
1145
|
+
}
|
|
1146
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1147
|
+
? (args as Record<string, unknown>)
|
|
1148
|
+
: {};
|
|
1149
|
+
const out: ValidatedListLoopsFilter = {};
|
|
1150
|
+
if (a.channel_id !== undefined && a.channel_id !== null) {
|
|
1151
|
+
const cid = typeof a.channel_id === "string" ? a.channel_id.trim() : "";
|
|
1152
|
+
if (!SAFE_CHANNEL_OR_LOOP_ID_RE.test(cid) || isReservedIdKey(cid) || EVIDENCE_CTRL_RE.test(cid) || EVIDENCE_SECRET_RE.test(cid)) {
|
|
1153
|
+
return { ok: false, error: "list_loops channel_id must be a valid channel identifier." };
|
|
1154
|
+
}
|
|
1155
|
+
out.channel_id = cid;
|
|
1156
|
+
}
|
|
1157
|
+
if (a.status !== undefined && a.status !== null) {
|
|
1158
|
+
if (a.status !== "active" && a.status !== "paused") {
|
|
1159
|
+
return { ok: false, error: "list_loops status must be one of: active, paused." };
|
|
1160
|
+
}
|
|
1161
|
+
out.status = a.status;
|
|
1162
|
+
}
|
|
1163
|
+
if (a.mode !== undefined && a.mode !== null) {
|
|
1164
|
+
if (typeof a.mode !== "string" || !(CREATE_LOOP_MODES as readonly string[]).includes(a.mode)) {
|
|
1165
|
+
return { ok: false, error: "list_loops mode must be one of: okr_wake, okr_watch, prompt." };
|
|
1166
|
+
}
|
|
1167
|
+
out.mode = a.mode as CreateLoopMode;
|
|
1168
|
+
}
|
|
1169
|
+
if (a.objective_id !== undefined && a.objective_id !== null) {
|
|
1170
|
+
const oid = typeof a.objective_id === "string" ? a.objective_id.trim() : "";
|
|
1171
|
+
if (!SAFE_OBJECTIVE_ID_RE.test(oid) || isReservedIdKey(oid) || EVIDENCE_SECRET_RE.test(oid)) {
|
|
1172
|
+
return { ok: false, error: "list_loops objective_id must be a valid objective identifier." };
|
|
1173
|
+
}
|
|
1174
|
+
out.objective_id = oid;
|
|
1175
|
+
}
|
|
1176
|
+
return { ok: true, value: out };
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
const SAFE_MEMORY_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$/;
|
|
1180
|
+
|
|
1181
|
+
export interface ValidatedDeleteMemoryInput {
|
|
1182
|
+
name: string;
|
|
1183
|
+
if_match_version?: number;
|
|
1184
|
+
allow_missing?: boolean;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
export interface ValidatedLoadMemoryInput {
|
|
1188
|
+
name?: string;
|
|
1189
|
+
include_revisions?: boolean;
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
export function validateLoadMemoryInput(
|
|
1193
|
+
args: unknown,
|
|
1194
|
+
): { ok: true; value: ValidatedLoadMemoryInput } | { ok: false; error: string } {
|
|
1195
|
+
if (hasOwnProto(args)) {
|
|
1196
|
+
return { ok: false, error: 'load_memory arguments cannot define own "__proto__".' };
|
|
1197
|
+
}
|
|
1198
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1199
|
+
? (args as Record<string, unknown>)
|
|
1200
|
+
: {};
|
|
1201
|
+
const out: ValidatedLoadMemoryInput = {};
|
|
1202
|
+
if (a.name !== undefined && a.name !== null) {
|
|
1203
|
+
if (typeof a.name !== "string") {
|
|
1204
|
+
return { ok: false, error: "load_memory name must be a string." };
|
|
1205
|
+
}
|
|
1206
|
+
const trimmed = a.name.trim();
|
|
1207
|
+
if (trimmed) {
|
|
1208
|
+
if (
|
|
1209
|
+
!SAFE_MEMORY_NAME_RE.test(trimmed) ||
|
|
1210
|
+
isReservedIdKey(trimmed) ||
|
|
1211
|
+
EVIDENCE_CTRL_RE.test(trimmed) ||
|
|
1212
|
+
EVIDENCE_SECRET_RE.test(trimmed)
|
|
1213
|
+
) {
|
|
1214
|
+
return {
|
|
1215
|
+
ok: false,
|
|
1216
|
+
error: "load_memory name must be 1..64 alphanumeric/._- chars starting with alphanumeric.",
|
|
1217
|
+
};
|
|
1218
|
+
}
|
|
1219
|
+
out.name = trimmed;
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
if (a.include_revisions !== undefined && a.include_revisions !== null) {
|
|
1223
|
+
if (typeof a.include_revisions !== "boolean") {
|
|
1224
|
+
return { ok: false, error: "load_memory include_revisions must be a boolean." };
|
|
1225
|
+
}
|
|
1226
|
+
if (a.include_revisions && !out.name) {
|
|
1227
|
+
return { ok: false, error: "load_memory include_revisions requires a specific memory name." };
|
|
1228
|
+
}
|
|
1229
|
+
out.include_revisions = a.include_revisions;
|
|
1230
|
+
}
|
|
1231
|
+
return { ok: true, value: out };
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
export function validateDeleteMemoryInput(
|
|
1235
|
+
args: unknown,
|
|
1236
|
+
): { ok: true; value: ValidatedDeleteMemoryInput } | { ok: false; error: string } {
|
|
1237
|
+
if (hasOwnProto(args)) {
|
|
1238
|
+
return { ok: false, error: 'delete_memory arguments cannot define own "__proto__".' };
|
|
1239
|
+
}
|
|
1240
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1241
|
+
? (args as Record<string, unknown>)
|
|
1242
|
+
: {};
|
|
1243
|
+
const name = typeof a.name === "string" ? a.name.trim() : "";
|
|
1244
|
+
if (
|
|
1245
|
+
!name ||
|
|
1246
|
+
!SAFE_MEMORY_NAME_RE.test(name) ||
|
|
1247
|
+
isReservedIdKey(name) ||
|
|
1248
|
+
EVIDENCE_CTRL_RE.test(name) ||
|
|
1249
|
+
EVIDENCE_SECRET_RE.test(name)
|
|
1250
|
+
) {
|
|
1251
|
+
return {
|
|
1252
|
+
ok: false,
|
|
1253
|
+
error: "delete_memory requires a valid memory name (1..64 alphanumeric/._- chars starting with alphanumeric).",
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
const out: ValidatedDeleteMemoryInput = { name };
|
|
1257
|
+
if (a.if_match_version !== undefined && a.if_match_version !== null) {
|
|
1258
|
+
if (
|
|
1259
|
+
typeof a.if_match_version !== "number" ||
|
|
1260
|
+
!Number.isSafeInteger(a.if_match_version) ||
|
|
1261
|
+
a.if_match_version < 1 ||
|
|
1262
|
+
a.if_match_version > 1_000_000
|
|
1263
|
+
) {
|
|
1264
|
+
return { ok: false, error: "delete_memory if_match_version must be an integer in 1..1000000." };
|
|
1265
|
+
}
|
|
1266
|
+
out.if_match_version = a.if_match_version;
|
|
1267
|
+
}
|
|
1268
|
+
if (a.allow_missing !== undefined && a.allow_missing !== null) {
|
|
1269
|
+
if (typeof a.allow_missing !== "boolean") {
|
|
1270
|
+
return { ok: false, error: "delete_memory allow_missing must be a boolean." };
|
|
1271
|
+
}
|
|
1272
|
+
out.allow_missing = a.allow_missing;
|
|
1273
|
+
}
|
|
1274
|
+
return { ok: true, value: out };
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
export function validateOkrParentIdOptionalInput(
|
|
1278
|
+
parentId: unknown,
|
|
1279
|
+
): { ok: true; value: string | undefined } | { ok: false; error: string } {
|
|
1280
|
+
if (parentId === undefined || parentId === null || parentId === "") {
|
|
1281
|
+
return { ok: true, value: undefined };
|
|
1282
|
+
}
|
|
1283
|
+
if (typeof parentId !== "string") {
|
|
1284
|
+
return { ok: false, error: "parent_id must be a string objective identifier" };
|
|
1285
|
+
}
|
|
1286
|
+
const trimmed = parentId.trim();
|
|
1287
|
+
if (!trimmed) {
|
|
1288
|
+
return { ok: true, value: undefined };
|
|
1289
|
+
}
|
|
1290
|
+
if (EVIDENCE_SECRET_RE.test(trimmed)) {
|
|
1291
|
+
return { ok: false, error: "parent_id rejected: appears to contain a secret or token" };
|
|
1292
|
+
}
|
|
1293
|
+
if (
|
|
1294
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(trimmed) ||
|
|
1295
|
+
isReservedIdKey(trimmed) ||
|
|
1296
|
+
EVIDENCE_CTRL_RE.test(trimmed)
|
|
1297
|
+
) {
|
|
1298
|
+
return { ok: false, error: "parent_id must be a valid objective identifier" };
|
|
1299
|
+
}
|
|
1300
|
+
return { ok: true, value: trimmed };
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
export function validateOkrReparentObjectiveInput(
|
|
1304
|
+
args: unknown,
|
|
1305
|
+
): { ok: true; value: { objective_id: string; body: Record<string, unknown> } } | { ok: false; error: string } {
|
|
1306
|
+
if (!args || typeof args !== "object" || Array.isArray(args) || hasOwnProto(args)) {
|
|
1307
|
+
return { ok: false, error: "okr_reparent_objective requires objective_id and parent_id." };
|
|
1308
|
+
}
|
|
1309
|
+
const a = args as Record<string, unknown>;
|
|
1310
|
+
const objectiveId = typeof a.objective_id === "string" ? a.objective_id.trim() : "";
|
|
1311
|
+
if (
|
|
1312
|
+
!objectiveId ||
|
|
1313
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(objectiveId) ||
|
|
1314
|
+
isReservedIdKey(objectiveId) ||
|
|
1315
|
+
EVIDENCE_CTRL_RE.test(objectiveId) ||
|
|
1316
|
+
EVIDENCE_SECRET_RE.test(objectiveId)
|
|
1317
|
+
) {
|
|
1318
|
+
return { ok: false, error: "objective_id must be a valid non-empty identifier." };
|
|
1319
|
+
}
|
|
1320
|
+
if (!("parent_id" in a) || a.parent_id === undefined) {
|
|
1321
|
+
return {
|
|
1322
|
+
ok: false,
|
|
1323
|
+
error: "okr_reparent_objective needs parent_id (an objective id to attach under, or null to detach to a top-level root).",
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1326
|
+
let normalizedParent: string | null;
|
|
1327
|
+
if (a.parent_id === null || (typeof a.parent_id === "string" && a.parent_id.trim() === "")) {
|
|
1328
|
+
normalizedParent = null;
|
|
1329
|
+
} else if (typeof a.parent_id === "string") {
|
|
1330
|
+
const trimmedParent = a.parent_id.trim();
|
|
1331
|
+
if (EVIDENCE_SECRET_RE.test(trimmedParent)) {
|
|
1332
|
+
return { ok: false, error: "parent_id rejected: appears to contain a secret or token" };
|
|
1333
|
+
}
|
|
1334
|
+
if (
|
|
1335
|
+
!SAFE_CHANNEL_OR_LOOP_ID_RE.test(trimmedParent) ||
|
|
1336
|
+
isReservedIdKey(trimmedParent) ||
|
|
1337
|
+
EVIDENCE_CTRL_RE.test(trimmedParent)
|
|
1338
|
+
) {
|
|
1339
|
+
return { ok: false, error: "parent_id must be a valid objective identifier or null to detach" };
|
|
1340
|
+
}
|
|
1341
|
+
if (trimmedParent === objectiveId) {
|
|
1342
|
+
return { ok: false, error: "parent_id cannot equal objective_id (self-reparent cycle)" };
|
|
1343
|
+
}
|
|
1344
|
+
normalizedParent = trimmedParent;
|
|
1345
|
+
} else {
|
|
1346
|
+
return { ok: false, error: "parent_id must be a string objective identifier or null to detach" };
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
const body: Record<string, unknown> = { parent_id: normalizedParent };
|
|
1350
|
+
if (a.if_match_updated_at !== undefined && a.if_match_updated_at !== null) {
|
|
1351
|
+
const verCheck = validateTaskVersionToken(a.if_match_updated_at);
|
|
1352
|
+
if (!verCheck.ok) {
|
|
1353
|
+
return { ok: false, error: verCheck.error };
|
|
1354
|
+
}
|
|
1355
|
+
body.if_match_updated_at = verCheck.value;
|
|
1356
|
+
}
|
|
1357
|
+
return { ok: true, value: { objective_id: objectiveId, body } };
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
export const CHANNEL_DOC_KINDS = [
|
|
1361
|
+
"topic",
|
|
1362
|
+
"rules",
|
|
1363
|
+
"roles",
|
|
1364
|
+
"context",
|
|
1365
|
+
"deep_dive",
|
|
1366
|
+
"skill",
|
|
1367
|
+
"channel_skill",
|
|
1368
|
+
] as const;
|
|
1369
|
+
|
|
1370
|
+
export type ChannelDocKindInput = (typeof CHANNEL_DOC_KINDS)[number];
|
|
1371
|
+
|
|
1372
|
+
const SAFE_CHANNEL_DOC_ID_RE = /^[A-Za-z0-9_-]{1,64}$/;
|
|
1373
|
+
|
|
1374
|
+
function normalizeDocLevelValue(level: unknown): 1 | 2 | 3 | 4 | null {
|
|
1375
|
+
if (typeof level === "number" && Number.isInteger(level) && level >= 1 && level <= 4) {
|
|
1376
|
+
return level as 1 | 2 | 3 | 4;
|
|
1377
|
+
}
|
|
1378
|
+
if (typeof level === "string") {
|
|
1379
|
+
const m = level.trim().match(/^(?:L)?([1-4])$/i);
|
|
1380
|
+
if (m) return Number(m[1]) as 1 | 2 | 3 | 4;
|
|
1381
|
+
}
|
|
1382
|
+
return null;
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
function validateChannelDocRoomId(
|
|
1386
|
+
raw: unknown,
|
|
1387
|
+
toolName: string,
|
|
1388
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
1389
|
+
const chatId = typeof raw === "string" ? raw.trim() : "";
|
|
1390
|
+
if (
|
|
1391
|
+
!chatId ||
|
|
1392
|
+
!OKR_DOC_CHANNEL_ID_RE.test(chatId) ||
|
|
1393
|
+
/^dm[-:]/i.test(chatId) ||
|
|
1394
|
+
chatId === "direct" ||
|
|
1395
|
+
isReservedIdKey(chatId) ||
|
|
1396
|
+
EVIDENCE_CTRL_RE.test(chatId)
|
|
1397
|
+
) {
|
|
1398
|
+
return { ok: false, error: `${toolName} requires a valid non-DM room chat_id (1..80 chars).` };
|
|
1399
|
+
}
|
|
1400
|
+
if (EVIDENCE_SECRET_RE.test(chatId)) {
|
|
1401
|
+
return { ok: false, error: `${toolName} chat_id rejected: appears to contain a secret or token.` };
|
|
1402
|
+
}
|
|
1403
|
+
return { ok: true, value: chatId };
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
export function validateChannelDocIdInput(
|
|
1407
|
+
raw: unknown,
|
|
1408
|
+
toolName = "channel_doc",
|
|
1409
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
1410
|
+
const docId = typeof raw === "string" ? raw.trim() : "";
|
|
1411
|
+
if (!docId || !SAFE_CHANNEL_DOC_ID_RE.test(docId) || isReservedIdKey(docId) || EVIDENCE_CTRL_RE.test(docId)) {
|
|
1412
|
+
return { ok: false, error: `${toolName} doc_id must match ^[A-Za-z0-9_-]{1,64}$.` };
|
|
1413
|
+
}
|
|
1414
|
+
if (EVIDENCE_SECRET_RE.test(docId)) {
|
|
1415
|
+
return { ok: false, error: `${toolName} doc_id rejected: appears to contain a secret or token.` };
|
|
1416
|
+
}
|
|
1417
|
+
return { ok: true, value: docId };
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
export interface ValidatedListChannelDocsInput {
|
|
1421
|
+
chat_id: string;
|
|
1422
|
+
level?: 1 | 2 | 3 | 4;
|
|
1423
|
+
active_only?: boolean;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
export function validateListChannelDocsInput(
|
|
1427
|
+
args: unknown,
|
|
1428
|
+
): { ok: true; value: ValidatedListChannelDocsInput } | { ok: false; error: string } {
|
|
1429
|
+
if (hasOwnProto(args)) {
|
|
1430
|
+
return { ok: false, error: 'list_channel_docs arguments cannot define own "__proto__".' };
|
|
1431
|
+
}
|
|
1432
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1433
|
+
? (args as Record<string, unknown>)
|
|
1434
|
+
: {};
|
|
1435
|
+
const roomCheck = validateChannelDocRoomId(a.chat_id, "list_channel_docs");
|
|
1436
|
+
if (!roomCheck.ok) return roomCheck;
|
|
1437
|
+
const out: ValidatedListChannelDocsInput = { chat_id: roomCheck.value };
|
|
1438
|
+
if (a.level !== undefined && a.level !== null) {
|
|
1439
|
+
const lvl = normalizeDocLevelValue(a.level);
|
|
1440
|
+
if (lvl === null) {
|
|
1441
|
+
return { ok: false, error: "list_channel_docs failed: level must be 1|2|3|4" };
|
|
1442
|
+
}
|
|
1443
|
+
out.level = lvl;
|
|
1444
|
+
}
|
|
1445
|
+
if (a.active_only !== undefined && a.active_only !== null) {
|
|
1446
|
+
if (typeof a.active_only !== "boolean") {
|
|
1447
|
+
return { ok: false, error: "list_channel_docs active_only must be a boolean." };
|
|
1448
|
+
}
|
|
1449
|
+
out.active_only = a.active_only;
|
|
1450
|
+
}
|
|
1451
|
+
return { ok: true, value: out };
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
export interface ValidatedArchiveChannelDocInput {
|
|
1455
|
+
chat_id: string;
|
|
1456
|
+
doc_id: string;
|
|
1457
|
+
expected_version: number;
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
export function validateArchiveChannelDocInput(
|
|
1461
|
+
args: unknown,
|
|
1462
|
+
toolName = "archive_channel_doc",
|
|
1463
|
+
): { ok: true; value: ValidatedArchiveChannelDocInput } | { ok: false; error: string } {
|
|
1464
|
+
if (hasOwnProto(args)) {
|
|
1465
|
+
return { ok: false, error: `${toolName} arguments cannot define own "__proto__".` };
|
|
1466
|
+
}
|
|
1467
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1468
|
+
? (args as Record<string, unknown>)
|
|
1469
|
+
: {};
|
|
1470
|
+
const roomCheck = validateChannelDocRoomId(a.chat_id, toolName);
|
|
1471
|
+
if (!roomCheck.ok) return roomCheck;
|
|
1472
|
+
const docCheck = validateChannelDocIdInput(a.doc_id, toolName);
|
|
1473
|
+
if (!docCheck.ok) return docCheck;
|
|
1474
|
+
if (
|
|
1475
|
+
typeof a.expected_version !== "number" ||
|
|
1476
|
+
!Number.isSafeInteger(a.expected_version) ||
|
|
1477
|
+
a.expected_version < 1 ||
|
|
1478
|
+
a.expected_version > 999_999_999
|
|
1479
|
+
) {
|
|
1480
|
+
return {
|
|
1481
|
+
ok: false,
|
|
1482
|
+
error: `${toolName} requires expected_version as a positive integer (1..999999999) for If-Match CAS.`,
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
return {
|
|
1486
|
+
ok: true,
|
|
1487
|
+
value: {
|
|
1488
|
+
chat_id: roomCheck.value,
|
|
1489
|
+
doc_id: docCheck.value,
|
|
1490
|
+
expected_version: a.expected_version,
|
|
1491
|
+
},
|
|
1492
|
+
};
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
export type ValidatedUpsertChannelDocResult =
|
|
1496
|
+
| {
|
|
1497
|
+
mode: "archive";
|
|
1498
|
+
chat_id: string;
|
|
1499
|
+
doc_id: string;
|
|
1500
|
+
expected_version: number;
|
|
1501
|
+
}
|
|
1502
|
+
| {
|
|
1503
|
+
mode: "upsert";
|
|
1504
|
+
chat_id: string;
|
|
1505
|
+
doc_id: string;
|
|
1506
|
+
expected_version: number;
|
|
1507
|
+
body: {
|
|
1508
|
+
title: string;
|
|
1509
|
+
kind: ChannelDocKindInput;
|
|
1510
|
+
level: 1 | 2 | 3 | 4;
|
|
1511
|
+
body_markdown: string;
|
|
1512
|
+
};
|
|
1513
|
+
};
|
|
1514
|
+
|
|
1515
|
+
function channelDocBodyByteCap(level: 1 | 2 | 3 | 4): number {
|
|
1516
|
+
switch (level) {
|
|
1517
|
+
case 1:
|
|
1518
|
+
return 16_384;
|
|
1519
|
+
case 2:
|
|
1520
|
+
return 131_072;
|
|
1521
|
+
case 3:
|
|
1522
|
+
return 524_288;
|
|
1523
|
+
case 4:
|
|
1524
|
+
return 921_600;
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
function hasValidSkillFrontmatter(markdown: string): boolean {
|
|
1529
|
+
if (!markdown.startsWith("---\n") && !markdown.startsWith("---\r\n")) return false;
|
|
1530
|
+
const normalized = markdown.replace(/\r\n/g, "\n");
|
|
1531
|
+
const endIdx = normalized.indexOf("\n---", 4);
|
|
1532
|
+
if (endIdx < 0) return false;
|
|
1533
|
+
const header = normalized.slice(4, endIdx);
|
|
1534
|
+
let hasName = false;
|
|
1535
|
+
let hasDesc = false;
|
|
1536
|
+
for (const line of header.split("\n")) {
|
|
1537
|
+
const colon = line.indexOf(":");
|
|
1538
|
+
if (colon <= 0) continue;
|
|
1539
|
+
const key = line.slice(0, colon).trim().toLowerCase();
|
|
1540
|
+
let val = line.slice(colon + 1).trim();
|
|
1541
|
+
if (
|
|
1542
|
+
val.length >= 2 &&
|
|
1543
|
+
((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'")))
|
|
1544
|
+
) {
|
|
1545
|
+
val = val.slice(1, -1).trim();
|
|
1546
|
+
}
|
|
1547
|
+
if (key === "name" && val.length > 0 && val.length <= 160) hasName = true;
|
|
1548
|
+
if (key === "description" && val.length > 0 && val.length <= 500) hasDesc = true;
|
|
1549
|
+
}
|
|
1550
|
+
return hasName && hasDesc;
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
export function validateUpsertChannelDocInput(
|
|
1554
|
+
args: unknown,
|
|
1555
|
+
): { ok: true; value: ValidatedUpsertChannelDocResult } | { ok: false; error: string } {
|
|
1556
|
+
if (hasOwnProto(args)) {
|
|
1557
|
+
return { ok: false, error: 'upsert_channel_doc arguments cannot define own "__proto__".' };
|
|
1558
|
+
}
|
|
1559
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1560
|
+
? (args as Record<string, unknown>)
|
|
1561
|
+
: {};
|
|
1562
|
+
if (a.archived !== undefined && typeof a.archived !== "boolean") {
|
|
1563
|
+
return { ok: false, error: "upsert_channel_doc archived must be a boolean when provided." };
|
|
1564
|
+
}
|
|
1565
|
+
if (a.archived === true) {
|
|
1566
|
+
const archCheck = validateArchiveChannelDocInput(a, "upsert_channel_doc");
|
|
1567
|
+
if (!archCheck.ok) return archCheck;
|
|
1568
|
+
return { ok: true, value: { mode: "archive", ...archCheck.value } };
|
|
1569
|
+
}
|
|
1570
|
+
const roomCheck = validateChannelDocRoomId(a.chat_id, "upsert_channel_doc");
|
|
1571
|
+
if (!roomCheck.ok) return roomCheck;
|
|
1572
|
+
const docCheck = validateChannelDocIdInput(a.doc_id, "upsert_channel_doc");
|
|
1573
|
+
if (!docCheck.ok) return docCheck;
|
|
1574
|
+
if (
|
|
1575
|
+
typeof a.expected_version !== "number" ||
|
|
1576
|
+
!Number.isSafeInteger(a.expected_version) ||
|
|
1577
|
+
a.expected_version < 0 ||
|
|
1578
|
+
a.expected_version > 999_999_999
|
|
1579
|
+
) {
|
|
1580
|
+
return {
|
|
1581
|
+
ok: false,
|
|
1582
|
+
error: "upsert_channel_doc expected_version must be an integer in 0..999999999.",
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1585
|
+
const title = typeof a.title === "string" ? a.title.trim() : "";
|
|
1586
|
+
if (!title || title.length > 120 || EVIDENCE_CTRL_RE.test(title)) {
|
|
1587
|
+
return { ok: false, error: "upsert_channel_doc requires a non-empty title (<=120 chars)." };
|
|
1588
|
+
}
|
|
1589
|
+
if (EVIDENCE_SECRET_RE.test(title)) {
|
|
1590
|
+
return { ok: false, error: "upsert_channel_doc rejected: title appears to contain a secret or token." };
|
|
1591
|
+
}
|
|
1592
|
+
if (typeof a.kind !== "string" || !(CHANNEL_DOC_KINDS as readonly string[]).includes(a.kind)) {
|
|
1593
|
+
return {
|
|
1594
|
+
ok: false,
|
|
1595
|
+
error: `upsert_channel_doc kind must be one of ${CHANNEL_DOC_KINDS.join(" | ")}.`,
|
|
1596
|
+
};
|
|
1597
|
+
}
|
|
1598
|
+
const kind = a.kind as ChannelDocKindInput;
|
|
1599
|
+
const level = a.level === undefined ? 3 : normalizeDocLevelValue(a.level);
|
|
1600
|
+
if (level === null) {
|
|
1601
|
+
return { ok: false, error: "upsert_channel_doc failed: level must be 1|2|3|4" };
|
|
1602
|
+
}
|
|
1603
|
+
if (typeof a.body_markdown !== "string" || !a.body_markdown.trim()) {
|
|
1604
|
+
return { ok: false, error: "upsert_channel_doc requires a non-empty body_markdown." };
|
|
1605
|
+
}
|
|
1606
|
+
if (EVIDENCE_SECRET_RE.test(a.body_markdown)) {
|
|
1607
|
+
return {
|
|
1608
|
+
ok: false,
|
|
1609
|
+
error: "upsert_channel_doc rejected: body_markdown appears to contain an API key, token, or private key.",
|
|
1610
|
+
};
|
|
1611
|
+
}
|
|
1612
|
+
const cap = channelDocBodyByteCap(level);
|
|
1613
|
+
if (Buffer.byteLength(a.body_markdown, "utf8") > cap) {
|
|
1614
|
+
return {
|
|
1615
|
+
ok: false,
|
|
1616
|
+
error: `upsert_channel_doc body_markdown exceeds L${level} cap (${cap} UTF-8 bytes).`,
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
if ((kind === "skill" || kind === "channel_skill") && !hasValidSkillFrontmatter(a.body_markdown)) {
|
|
1620
|
+
return {
|
|
1621
|
+
ok: false,
|
|
1622
|
+
error: "upsert_channel_doc skill/channel_skill requires YAML frontmatter with single-line name and description.",
|
|
1623
|
+
};
|
|
1624
|
+
}
|
|
1625
|
+
return {
|
|
1626
|
+
ok: true,
|
|
1627
|
+
value: {
|
|
1628
|
+
mode: "upsert",
|
|
1629
|
+
chat_id: roomCheck.value,
|
|
1630
|
+
doc_id: docCheck.value,
|
|
1631
|
+
expected_version: a.expected_version,
|
|
1632
|
+
body: {
|
|
1633
|
+
title,
|
|
1634
|
+
kind,
|
|
1635
|
+
level,
|
|
1636
|
+
body_markdown: a.body_markdown,
|
|
1637
|
+
},
|
|
1638
|
+
},
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
const SAFE_PERSONAL_SKILL_SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$/;
|
|
1643
|
+
const PERSONAL_SKILL_BODY_MAX_CHARS = 100_000;
|
|
1644
|
+
|
|
1645
|
+
export function derivePersonalSkillSlug(rawDocId: unknown, rawName: string): string {
|
|
1646
|
+
if (typeof rawDocId === "string" && rawDocId.trim()) {
|
|
1647
|
+
const trimmed = rawDocId.trim();
|
|
1648
|
+
if (SAFE_PERSONAL_SKILL_SLUG_RE.test(trimmed) && !isReservedIdKey(trimmed)) {
|
|
1649
|
+
return trimmed;
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
const fallback =
|
|
1653
|
+
String(rawDocId || rawName)
|
|
1654
|
+
.toLowerCase()
|
|
1655
|
+
.replace(/[^a-z0-9._-]+/g, "-")
|
|
1656
|
+
.replace(/^[^a-z0-9]+/, "")
|
|
1657
|
+
.replace(/[^a-z0-9]+$/, "")
|
|
1658
|
+
.replace(/\.{2,}/g, "-")
|
|
1659
|
+
.slice(0, 64) || "skill";
|
|
1660
|
+
return isReservedIdKey(fallback) ? "skill" : fallback;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
export function validatePersonalSkillSlugInput(
|
|
1664
|
+
raw: unknown,
|
|
1665
|
+
toolName: string,
|
|
1666
|
+
): { ok: true; value: string } | { ok: false; error: string } {
|
|
1667
|
+
const slug = typeof raw === "string" ? raw.trim() : "";
|
|
1668
|
+
if (
|
|
1669
|
+
!slug ||
|
|
1670
|
+
!SAFE_PERSONAL_SKILL_SLUG_RE.test(slug) ||
|
|
1671
|
+
isReservedIdKey(slug) ||
|
|
1672
|
+
EVIDENCE_CTRL_RE.test(slug) ||
|
|
1673
|
+
EVIDENCE_SECRET_RE.test(slug)
|
|
1674
|
+
) {
|
|
1675
|
+
return {
|
|
1676
|
+
ok: false,
|
|
1677
|
+
error: `${toolName} requires a valid personal skill slug (1..64 alphanumeric/._- chars starting with alphanumeric).`,
|
|
1678
|
+
};
|
|
1679
|
+
}
|
|
1680
|
+
return { ok: true, value: slug };
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
export type ValidatedLoadSkillTarget =
|
|
1684
|
+
| { scope: "global"; skill_id?: string }
|
|
1685
|
+
| { scope: "personal"; name: string; include_revisions: boolean }
|
|
1686
|
+
| { scope: "channel"; chat_id: string; doc_id: string; include_revisions: boolean };
|
|
1687
|
+
|
|
1688
|
+
export function validateLoadSkillInput(
|
|
1689
|
+
args: unknown,
|
|
1690
|
+
): { ok: true; value: ValidatedLoadSkillTarget } | { ok: false; error: string } {
|
|
1691
|
+
if (hasOwnProto(args)) {
|
|
1692
|
+
return { ok: false, error: 'load_skill arguments cannot define own "__proto__".' };
|
|
1693
|
+
}
|
|
1694
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1695
|
+
? (args as Record<string, unknown>)
|
|
1696
|
+
: {};
|
|
1697
|
+
const includeRevisions = a.include_revisions === true;
|
|
1698
|
+
if (a.include_revisions !== undefined && typeof a.include_revisions !== "boolean") {
|
|
1699
|
+
return { ok: false, error: "load_skill include_revisions must be a boolean when provided." };
|
|
1700
|
+
}
|
|
1701
|
+
const hasChat = typeof a.chat_id === "string" && a.chat_id.trim().length > 0;
|
|
1702
|
+
const hasDoc = typeof a.doc_id === "string" && a.doc_id.trim().length > 0;
|
|
1703
|
+
const hasName = typeof a.name === "string" && a.name.trim().length > 0;
|
|
1704
|
+
const hasSkillId = typeof a.skill_id === "string" && a.skill_id.trim().length > 0;
|
|
1705
|
+
|
|
1706
|
+
if ((hasChat || hasDoc) && (hasName || hasSkillId)) {
|
|
1707
|
+
return {
|
|
1708
|
+
ok: false,
|
|
1709
|
+
error: "load_skill target is ambiguous: pass (chat_id + doc_id) for a channel skill, (name) for a personal skill, or (skill_id) for a global skill.",
|
|
1710
|
+
};
|
|
1711
|
+
}
|
|
1712
|
+
if (hasName && hasSkillId) {
|
|
1713
|
+
return {
|
|
1714
|
+
ok: false,
|
|
1715
|
+
error: "load_skill target is ambiguous: pass either name (personal skill) or skill_id (global skill), not both.",
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
if (hasChat || hasDoc) {
|
|
1719
|
+
if (!hasChat || !hasDoc) {
|
|
1720
|
+
return { ok: false, error: "load_skill for a channel skill requires both chat_id and doc_id." };
|
|
1721
|
+
}
|
|
1722
|
+
const roomCheck = validateChannelDocRoomId(a.chat_id, "load_skill");
|
|
1723
|
+
if (!roomCheck.ok) return roomCheck;
|
|
1724
|
+
const docCheck = validateChannelDocIdInput(a.doc_id, "load_skill");
|
|
1725
|
+
if (!docCheck.ok) return docCheck;
|
|
1726
|
+
return {
|
|
1727
|
+
ok: true,
|
|
1728
|
+
value: { scope: "channel", chat_id: roomCheck.value, doc_id: docCheck.value, include_revisions: includeRevisions },
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
if (hasName) {
|
|
1732
|
+
const slugCheck = validatePersonalSkillSlugInput(a.name, "load_skill");
|
|
1733
|
+
if (!slugCheck.ok) return slugCheck;
|
|
1734
|
+
return {
|
|
1735
|
+
ok: true,
|
|
1736
|
+
value: { scope: "personal", name: slugCheck.value, include_revisions: includeRevisions },
|
|
1737
|
+
};
|
|
1738
|
+
}
|
|
1739
|
+
if (includeRevisions) {
|
|
1740
|
+
return {
|
|
1741
|
+
ok: false,
|
|
1742
|
+
error: "load_skill include_revisions is supported for personal skills (name) or channel skills (chat_id + doc_id), not built-in global skills.",
|
|
1743
|
+
};
|
|
1744
|
+
}
|
|
1745
|
+
if (a.skill_id !== undefined && a.skill_id !== null && typeof a.skill_id !== "string") {
|
|
1746
|
+
return { ok: false, error: "load_skill skill_id must be a string when provided." };
|
|
1747
|
+
}
|
|
1748
|
+
const skillId = hasSkillId ? (a.skill_id as string).trim() : undefined;
|
|
1749
|
+
if (
|
|
1750
|
+
skillId !== undefined &&
|
|
1751
|
+
(!SAFE_CHANNEL_DOC_ID_RE.test(skillId) ||
|
|
1752
|
+
isReservedIdKey(skillId) ||
|
|
1753
|
+
skillId in Object.prototype ||
|
|
1754
|
+
EVIDENCE_CTRL_RE.test(skillId) ||
|
|
1755
|
+
EVIDENCE_SECRET_RE.test(skillId))
|
|
1756
|
+
) {
|
|
1757
|
+
return { ok: false, error: "load_skill skill_id must be a valid global skill identifier." };
|
|
1758
|
+
}
|
|
1759
|
+
return { ok: true, value: { scope: "global", ...(skillId ? { skill_id: skillId } : {}) } };
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
export type ValidatedSaveSkillResult =
|
|
1763
|
+
| {
|
|
1764
|
+
scope: "personal";
|
|
1765
|
+
mode: "archive";
|
|
1766
|
+
slug: string;
|
|
1767
|
+
if_match_version: number;
|
|
1768
|
+
}
|
|
1769
|
+
| {
|
|
1770
|
+
scope: "personal";
|
|
1771
|
+
mode: "save";
|
|
1772
|
+
slug: string;
|
|
1773
|
+
name: string;
|
|
1774
|
+
body_markdown: string;
|
|
1775
|
+
if_match_version?: number;
|
|
1776
|
+
}
|
|
1777
|
+
| {
|
|
1778
|
+
scope: "channel";
|
|
1779
|
+
mode: "archive";
|
|
1780
|
+
chat_id: string;
|
|
1781
|
+
doc_id: string;
|
|
1782
|
+
if_match_version: number;
|
|
1783
|
+
}
|
|
1784
|
+
| {
|
|
1785
|
+
scope: "channel";
|
|
1786
|
+
mode: "save";
|
|
1787
|
+
chat_id: string;
|
|
1788
|
+
doc_id: string;
|
|
1789
|
+
name: string;
|
|
1790
|
+
level: 1 | 2 | 3 | 4;
|
|
1791
|
+
body_markdown: string;
|
|
1792
|
+
if_match_version?: number;
|
|
1793
|
+
};
|
|
1794
|
+
|
|
1795
|
+
export function validateSaveSkillInput(
|
|
1796
|
+
args: unknown,
|
|
1797
|
+
): { ok: true; value: ValidatedSaveSkillResult } | { ok: false; error: string } {
|
|
1798
|
+
if (hasOwnProto(args)) {
|
|
1799
|
+
return { ok: false, error: 'save_skill arguments cannot define own "__proto__".' };
|
|
1800
|
+
}
|
|
1801
|
+
const a = args && typeof args === "object" && !Array.isArray(args)
|
|
1802
|
+
? (args as Record<string, unknown>)
|
|
1803
|
+
: {};
|
|
1804
|
+
const rawName = typeof a.name === "string" ? a.name.trim() : "";
|
|
1805
|
+
if (!rawName || rawName.length > 120 || EVIDENCE_CTRL_RE.test(rawName)) {
|
|
1806
|
+
return { ok: false, error: "save_skill requires name (1..120 chars)." };
|
|
1807
|
+
}
|
|
1808
|
+
if (EVIDENCE_SECRET_RE.test(rawName)) {
|
|
1809
|
+
return { ok: false, error: "save_skill rejected: name appears to contain a secret or token." };
|
|
1810
|
+
}
|
|
1811
|
+
if (a.archived !== undefined && typeof a.archived !== "boolean") {
|
|
1812
|
+
return { ok: false, error: "save_skill archived must be a boolean when provided." };
|
|
1813
|
+
}
|
|
1814
|
+
const archived = a.archived === true;
|
|
1815
|
+
let ifMatchVersion: number | undefined;
|
|
1816
|
+
if (a.if_match_version !== undefined && a.if_match_version !== null) {
|
|
1817
|
+
if (
|
|
1818
|
+
typeof a.if_match_version !== "number" ||
|
|
1819
|
+
!Number.isSafeInteger(a.if_match_version) ||
|
|
1820
|
+
a.if_match_version < 0 ||
|
|
1821
|
+
a.if_match_version > 999_999_999
|
|
1822
|
+
) {
|
|
1823
|
+
return { ok: false, error: "save_skill if_match_version must be an integer in 0..999999999." };
|
|
1824
|
+
}
|
|
1825
|
+
ifMatchVersion = a.if_match_version;
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
const hasChat = typeof a.chat_id === "string" && a.chat_id.trim().length > 0;
|
|
1829
|
+
if (archived) {
|
|
1830
|
+
if (ifMatchVersion === undefined || ifMatchVersion < 1) {
|
|
1831
|
+
return {
|
|
1832
|
+
ok: false,
|
|
1833
|
+
error: "save_skill archived=true requires a positive integer if_match_version (>= 1) for non-destructive CAS archiving.",
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
if (!hasChat) {
|
|
1837
|
+
const pslug =
|
|
1838
|
+
typeof a.doc_id === "string" && a.doc_id.trim()
|
|
1839
|
+
? validatePersonalSkillSlugInput(a.doc_id, "save_skill")
|
|
1840
|
+
: validatePersonalSkillSlugInput(derivePersonalSkillSlug(undefined, rawName), "save_skill");
|
|
1841
|
+
if (!pslug.ok) return pslug;
|
|
1842
|
+
return {
|
|
1843
|
+
ok: true,
|
|
1844
|
+
value: { scope: "personal", mode: "archive", slug: pslug.value, if_match_version: ifMatchVersion },
|
|
1845
|
+
};
|
|
1846
|
+
}
|
|
1847
|
+
const roomCheck = validateChannelDocRoomId(a.chat_id, "save_skill");
|
|
1848
|
+
if (!roomCheck.ok) return roomCheck;
|
|
1849
|
+
const defaultDocId = `skill-${rawName.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 48) || "skill"}`;
|
|
1850
|
+
const docCheck = validateChannelDocIdInput(
|
|
1851
|
+
typeof a.doc_id === "string" && a.doc_id.trim() ? a.doc_id : defaultDocId,
|
|
1852
|
+
"save_skill",
|
|
1853
|
+
);
|
|
1854
|
+
if (!docCheck.ok) return docCheck;
|
|
1855
|
+
return {
|
|
1856
|
+
ok: true,
|
|
1857
|
+
value: { scope: "channel", mode: "archive", chat_id: roomCheck.value, doc_id: docCheck.value, if_match_version: ifMatchVersion },
|
|
1858
|
+
};
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
const rawDesc = typeof a.description === "string" ? a.description.trim() : "";
|
|
1862
|
+
if (!rawDesc) {
|
|
1863
|
+
return {
|
|
1864
|
+
ok: false,
|
|
1865
|
+
error: "save_skill needs name + description (body is the skill markdown). Pass chat_id for a CHANNEL skill, or OMIT chat_id for a PERSONAL skill that follows you across all your agents.",
|
|
1866
|
+
};
|
|
1867
|
+
}
|
|
1868
|
+
if (EVIDENCE_SECRET_RE.test(rawDesc)) {
|
|
1869
|
+
return { ok: false, error: "save_skill rejected: description appears to contain a secret or token." };
|
|
1870
|
+
}
|
|
1871
|
+
const rawBody = a.body === undefined || a.body === null ? "" : typeof a.body === "string" ? a.body : null;
|
|
1872
|
+
if (rawBody === null) {
|
|
1873
|
+
return { ok: false, error: "save_skill body must be a markdown string when provided." };
|
|
1874
|
+
}
|
|
1875
|
+
if (EVIDENCE_SECRET_RE.test(rawBody)) {
|
|
1876
|
+
return {
|
|
1877
|
+
ok: false,
|
|
1878
|
+
error: "save_skill rejected: body appears to contain an API key, token, or private key.",
|
|
1879
|
+
};
|
|
1880
|
+
}
|
|
1881
|
+
const oneLine = (s: string, cap = 480) => String(s).replace(/\r?\n/g, " ").trim().slice(0, cap);
|
|
1882
|
+
const triggerLine =
|
|
1883
|
+
typeof a.trigger === "string" && a.trigger.trim()
|
|
1884
|
+
? `\ntrigger: ${oneLine(a.trigger, 80)}`
|
|
1885
|
+
: "";
|
|
1886
|
+
const argHintLine =
|
|
1887
|
+
typeof a.argument_hint === "string" && a.argument_hint.trim()
|
|
1888
|
+
? `\nargument-hint: ${JSON.stringify(oneLine(a.argument_hint, 160))}`
|
|
1889
|
+
: "";
|
|
1890
|
+
const md = `---\nname: ${oneLine(rawName, 120)}\ndescription: ${oneLine(rawDesc, 480)}${triggerLine}${argHintLine}\n---\n\n${rawBody}`;
|
|
1891
|
+
|
|
1892
|
+
if (!hasChat) {
|
|
1893
|
+
if (md.length > PERSONAL_SKILL_BODY_MAX_CHARS) {
|
|
1894
|
+
return { ok: false, error: `save_skill personal skill markdown exceeds ${PERSONAL_SKILL_BODY_MAX_CHARS} chars.` };
|
|
1895
|
+
}
|
|
1896
|
+
if (typeof a.doc_id === "string" && a.doc_id.trim()) {
|
|
1897
|
+
const explicitSlug = validatePersonalSkillSlugInput(a.doc_id, "save_skill");
|
|
1898
|
+
if (!explicitSlug.ok) return explicitSlug;
|
|
1899
|
+
return {
|
|
1900
|
+
ok: true,
|
|
1901
|
+
value: {
|
|
1902
|
+
scope: "personal",
|
|
1903
|
+
mode: "save",
|
|
1904
|
+
slug: explicitSlug.value,
|
|
1905
|
+
name: rawName,
|
|
1906
|
+
body_markdown: md,
|
|
1907
|
+
...(ifMatchVersion !== undefined ? { if_match_version: ifMatchVersion } : {}),
|
|
1908
|
+
},
|
|
1909
|
+
};
|
|
1910
|
+
}
|
|
1911
|
+
const derived = derivePersonalSkillSlug(undefined, rawName);
|
|
1912
|
+
return {
|
|
1913
|
+
ok: true,
|
|
1914
|
+
value: {
|
|
1915
|
+
scope: "personal",
|
|
1916
|
+
mode: "save",
|
|
1917
|
+
slug: derived,
|
|
1918
|
+
name: rawName,
|
|
1919
|
+
body_markdown: md,
|
|
1920
|
+
...(ifMatchVersion !== undefined ? { if_match_version: ifMatchVersion } : {}),
|
|
1921
|
+
},
|
|
1922
|
+
};
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
const roomCheck = validateChannelDocRoomId(a.chat_id, "save_skill");
|
|
1926
|
+
if (!roomCheck.ok) return roomCheck;
|
|
1927
|
+
const defaultDocId = `skill-${rawName.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 48) || "skill"}`;
|
|
1928
|
+
const docCheck = validateChannelDocIdInput(
|
|
1929
|
+
typeof a.doc_id === "string" && a.doc_id.trim() ? a.doc_id : defaultDocId,
|
|
1930
|
+
"save_skill",
|
|
1931
|
+
);
|
|
1932
|
+
if (!docCheck.ok) return docCheck;
|
|
1933
|
+
const level = a.level === undefined ? 3 : normalizeDocLevelValue(a.level);
|
|
1934
|
+
if (level === null) {
|
|
1935
|
+
return { ok: false, error: "save_skill level must be 1|2|3|4." };
|
|
1936
|
+
}
|
|
1937
|
+
const cap = channelDocBodyByteCap(level);
|
|
1938
|
+
if (Buffer.byteLength(md, "utf8") > cap) {
|
|
1939
|
+
return { ok: false, error: `save_skill markdown exceeds L${level} cap (${cap} UTF-8 bytes).` };
|
|
1940
|
+
}
|
|
1941
|
+
return {
|
|
1942
|
+
ok: true,
|
|
1943
|
+
value: {
|
|
1944
|
+
scope: "channel",
|
|
1945
|
+
mode: "save",
|
|
1946
|
+
chat_id: roomCheck.value,
|
|
1947
|
+
doc_id: docCheck.value,
|
|
1948
|
+
name: rawName,
|
|
1949
|
+
level,
|
|
1950
|
+
body_markdown: md,
|
|
1951
|
+
...(ifMatchVersion !== undefined ? { if_match_version: ifMatchVersion } : {}),
|
|
1952
|
+
},
|
|
1953
|
+
};
|
|
1954
|
+
}
|