Skip to content

Commit 267ffc4

Browse files
clawsweeperroboclaw-botTakhoffman
authored
feat(ui): manage automation condition triggers (#126534)
* feat(ui): manage cron condition triggers * test(models): isolate ambient CLI availability Co-authored-by: Tak Hoffman <[email protected]> --------- Co-authored-by: RoboClaw <[email protected]> Co-authored-by: Tak Hoffman <[email protected]>
1 parent fdac5c5 commit 267ffc4

20 files changed

Lines changed: 497 additions & 50 deletions

File tree

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16877,6 +16877,7 @@ public struct CronListParams: Codable, Sendable {
1687716877
public let enabled: AnyCodable?
1687816878
public let schedulekind: AnyCodable?
1687916879
public let lastrunstatus: AnyCodable?
16880+
public let trigger: AnyCodable?
1688016881
public let sortby: AnyCodable?
1688116882
public let sortdir: AnyCodable?
1688216883
public let agentid: String?
@@ -16891,6 +16892,7 @@ public struct CronListParams: Codable, Sendable {
1689116892
enabled: AnyCodable? = nil,
1689216893
schedulekind: AnyCodable? = nil,
1689316894
lastrunstatus: AnyCodable? = nil,
16895+
trigger: AnyCodable? = nil,
1689416896
sortby: AnyCodable? = nil,
1689516897
sortdir: AnyCodable? = nil,
1689616898
agentid: String? = nil,
@@ -16904,6 +16906,7 @@ public struct CronListParams: Codable, Sendable {
1690416906
self.enabled = enabled
1690516907
self.schedulekind = schedulekind
1690616908
self.lastrunstatus = lastrunstatus
16909+
self.trigger = trigger
1690716910
self.sortby = sortby
1690816911
self.sortdir = sortdir
1690916912
self.agentid = agentid
@@ -16919,6 +16922,7 @@ public struct CronListParams: Codable, Sendable {
1691916922
case enabled
1692016923
case schedulekind = "scheduleKind"
1692116924
case lastrunstatus = "lastRunStatus"
16925+
case trigger
1692216926
case sortby = "sortBy"
1692316927
case sortdir = "sortDir"
1692416928
case agentid = "agentId"

‎packages/gateway-protocol/src/cron-validators.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ describe("cron protocol validators", () => {
397397
enabled: "all",
398398
scheduleKind: "cron",
399399
lastRunStatus: "unknown",
400+
trigger: "conditional",
400401
sortBy: "nextRunAtMs",
401402
sortDir: "asc",
402403
agentId: "ops",
@@ -409,6 +410,7 @@ describe("cron protocol validators", () => {
409410
{ agentId: "" },
410411
{ scheduleKind: "yearly" },
411412
{ lastRunStatus: "pending" },
413+
{ trigger: "configured" },
412414
]);
413415
});
414416

‎packages/gateway-protocol/src/schema/cron.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ const CronJobsLastRunStatusFilterSchema = Type.Union([
128128
Type.Literal("skipped"),
129129
Type.Literal("unknown"),
130130
]);
131+
const CronJobsTriggerFilterSchema = Type.Union([
132+
Type.Literal("all"),
133+
Type.Literal("conditional"),
134+
Type.Literal("unconditional"),
135+
]);
131136
const CronJobsSortBySchema = Type.Union([
132137
Type.Literal("nextRunAtMs"),
133138
Type.Literal("updatedAtMs"),
@@ -622,6 +627,7 @@ export const CronListParamsSchema = closedObject({
622627
enabled: Type.Optional(CronJobsEnabledFilterSchema),
623628
scheduleKind: Type.Optional(CronJobsScheduleKindFilterSchema),
624629
lastRunStatus: Type.Optional(CronJobsLastRunStatusFilterSchema),
630+
trigger: Type.Optional(CronJobsTriggerFilterSchema),
625631
sortBy: Type.Optional(CronJobsSortBySchema),
626632
sortDir: Type.Optional(CronSortDirSchema),
627633
agentId: Type.Optional(NonEmptyString),

‎src/cron/service.list-page-sort-guards.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ describe("cron listPage sort guards", () => {
283283
expect(page.jobs.map((job) => job.id)).toEqual(["tax-digest"]);
284284
});
285285

286-
it("applies schedule and last-run status filters before paging", async () => {
286+
it("applies schedule, status, and trigger filters before paging", async () => {
287287
const nextRunAtMs = Date.parse("2030-02-27T15:30:00.000Z");
288288
const jobs = [
289289
createBaseJob({
@@ -299,6 +299,12 @@ describe("cron listPage sort guards", () => {
299299
createBaseJob({
300300
id: "cron-unknown",
301301
schedule: { kind: "cron", expr: "0 10 * * *" },
302+
trigger: { script: "json({ fire: true })" },
303+
state: { nextRunAtMs },
304+
}),
305+
createBaseJob({
306+
id: "cron-unknown-plain",
307+
schedule: { kind: "cron", expr: "0 11 * * *" },
302308
state: { nextRunAtMs },
303309
}),
304310
];
@@ -310,6 +316,7 @@ describe("cron listPage sort guards", () => {
310316
const page = await listPage(state, {
311317
scheduleKind: "cron",
312318
lastRunStatus: "unknown",
319+
trigger: "conditional",
313320
limit: 1,
314321
});
315322

‎src/cron/service/list-page-types.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export type CronJobsScheduleKindFilter = "all" | "at" | "every" | "cron" | "on-e
1010
/** Last-run status filter, including jobs that have not produced a status yet. */
1111
export type CronJobsLastRunStatusFilter = "all" | CronRunStatus | "unknown";
1212

13+
/** Condition-trigger filter accepted by paginated cron listing. */
14+
export type CronJobsTriggerFilter = "all" | "conditional" | "unconditional";
15+
1316
/** Stable sort keys supported by paginated cron listing. */
1417
export type CronJobsSortBy = "nextRunAtMs" | "updatedAtMs" | "name";
1518

@@ -25,6 +28,7 @@ export type CronListPageOptions = {
2528
enabled?: CronJobsEnabledFilter;
2629
scheduleKind?: CronJobsScheduleKindFilter;
2730
lastRunStatus?: CronJobsLastRunStatusFilter;
31+
trigger?: CronJobsTriggerFilter;
2832
sortBy?: CronJobsSortBy;
2933
sortDir?: CronSortDir;
3034
agentId?: string;

‎src/cron/service/ops-read.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
CronJobsEnabledFilter,
1818
CronJobsLastRunStatusFilter,
1919
CronJobsScheduleKindFilter,
20+
CronJobsTriggerFilter,
2021
CronListPageOptions,
2122
CronListPageResult,
2223
} from "./list-page-types.js";
@@ -306,6 +307,17 @@ function resolveLastRunStatusFilter(opts?: CronListPageOptions): CronJobsLastRun
306307
return "all";
307308
}
308309

310+
function resolveTriggerFilter(opts?: CronListPageOptions): CronJobsTriggerFilter {
311+
if (
312+
opts?.trigger === "all" ||
313+
opts?.trigger === "conditional" ||
314+
opts?.trigger === "unconditional"
315+
) {
316+
return opts.trigger;
317+
}
318+
return "all";
319+
}
320+
309321
/** Lists a filtered, sorted, bounded page of cron jobs for CLI/RPC callers. */
310322
export async function listPage(state: CronServiceState, opts?: CronListPageOptions) {
311323
return await locked(state, async () => {
@@ -314,6 +326,7 @@ export async function listPage(state: CronServiceState, opts?: CronListPageOptio
314326
const enabledFilter = resolveEnabledFilter(opts);
315327
const scheduleKindFilter = resolveScheduleKindFilter(opts);
316328
const lastRunStatusFilter = resolveLastRunStatusFilter(opts);
329+
const triggerFilter = resolveTriggerFilter(opts);
317330
const sortBy = opts?.sortBy ?? "nextRunAtMs";
318331
const sortDir = opts?.sortDir ?? "asc";
319332
const requestedAgentId = normalizeOptionalAgentId(opts?.agentId);
@@ -340,6 +353,12 @@ export async function listPage(state: CronServiceState, opts?: CronListPageOptio
340353
) {
341354
return false;
342355
}
356+
if (triggerFilter === "conditional" && !job.trigger) {
357+
return false;
358+
}
359+
if (triggerFilter === "unconditional" && job.trigger) {
360+
return false;
361+
}
343362
if (!query) {
344363
return true;
345364
}

‎src/gateway/server-methods/cron.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ export const cronHandlers: GatewayRequestHandlers = {
496496
enabled?: "all" | "enabled" | "disabled";
497497
scheduleKind?: "all" | "at" | "every" | "cron";
498498
lastRunStatus?: "all" | "ok" | "error" | "skipped" | "unknown";
499+
trigger?: "all" | "conditional" | "unconditional";
499500
sortBy?: "nextRunAtMs" | "updatedAtMs" | "name";
500501
sortDir?: "asc" | "desc";
501502
agentId?: string;
@@ -516,6 +517,7 @@ export const cronHandlers: GatewayRequestHandlers = {
516517
enabled: p.enabled,
517518
scheduleKind: p.scheduleKind,
518519
lastRunStatus: p.lastRunStatus,
520+
trigger: p.trigger,
519521
sortBy: p.sortBy,
520522
sortDir: p.sortDir,
521523
agentId: callerScope?.agentId ?? p.agentId,

‎src/gateway/server-methods/cron.validation.test.ts‎

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,36 @@ function createCronContext(currentJobs?: CronJob | CronJob[]) {
226226
},
227227
),
228228
list: vi.fn(async () => jobs),
229-
listPage: vi.fn(async (opts?: { agentId?: string; limit?: number; offset?: number }) => {
230-
const requestedAgentId = opts?.agentId?.trim().toLowerCase();
231-
const filteredJobs = requestedAgentId
232-
? jobs.filter((job) => (job.agentId ?? "main").trim().toLowerCase() === requestedAgentId)
233-
: jobs;
234-
const total = filteredJobs.length;
235-
const offset = Math.max(0, Math.min(total, Math.floor(opts?.offset ?? 0)));
236-
const defaultLimit = total === 0 ? 50 : total;
237-
const limit = Math.max(1, Math.min(200, Math.floor(opts?.limit ?? defaultLimit)));
238-
const pageJobs = filteredJobs.slice(offset, offset + limit);
239-
const nextOffset = offset + pageJobs.length;
240-
return {
241-
jobs: pageJobs,
242-
snapshotRevision: `fixture:${filteredJobs.map((job) => job.id).join(",")}`,
243-
total,
244-
offset,
245-
limit,
246-
hasMore: nextOffset < total,
247-
nextOffset: nextOffset < total ? nextOffset : null,
248-
};
249-
}),
229+
listPage: vi.fn(
230+
async (opts?: {
231+
agentId?: string;
232+
limit?: number;
233+
offset?: number;
234+
trigger?: "all" | "conditional" | "unconditional";
235+
}) => {
236+
const requestedAgentId = opts?.agentId?.trim().toLowerCase();
237+
const filteredJobs = requestedAgentId
238+
? jobs.filter(
239+
(job) => (job.agentId ?? "main").trim().toLowerCase() === requestedAgentId,
240+
)
241+
: jobs;
242+
const total = filteredJobs.length;
243+
const offset = Math.max(0, Math.min(total, Math.floor(opts?.offset ?? 0)));
244+
const defaultLimit = total === 0 ? 50 : total;
245+
const limit = Math.max(1, Math.min(200, Math.floor(opts?.limit ?? defaultLimit)));
246+
const pageJobs = filteredJobs.slice(offset, offset + limit);
247+
const nextOffset = offset + pageJobs.length;
248+
return {
249+
jobs: pageJobs,
250+
snapshotRevision: `fixture:${filteredJobs.map((job) => job.id).join(",")}`,
251+
total,
252+
offset,
253+
limit,
254+
hasMore: nextOffset < total,
255+
nextOffset: nextOffset < total ? nextOffset : null,
256+
};
257+
},
258+
),
250259
},
251260
logGateway: {
252261
info: vi.fn(),
@@ -925,13 +934,17 @@ describe("cron method validation", () => {
925934
});
926935
});
927936

928-
it("keeps unscoped cron.list agentId filtering global for operator callers", async () => {
937+
it("forwards unscoped cron.list filters for operator callers", async () => {
929938
const context = createCronContext(createCronJob({ agentId: "worker" }));
930939

931-
const { respond } = await invokeCron("cron.list", { agentId: "worker" }, { context });
940+
const { respond } = await invokeCron(
941+
"cron.list",
942+
{ agentId: "worker", trigger: "conditional" },
943+
{ context },
944+
);
932945

933946
expect(context.cron.listPage).toHaveBeenCalledWith(
934-
expect.objectContaining({ agentId: "worker" }),
947+
expect.objectContaining({ agentId: "worker", trigger: "conditional" }),
935948
);
936949
expect(respond).toHaveBeenCalledWith(
937950
true,

‎src/gateway/server-methods/models-list-result.cli-runtime-availability.test.ts‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
13
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js";
25
import type { OpenClawConfig } from "../../config/types.openclaw.js";
36
import {
47
listModels,
58
providerCatalogEntry,
69
} from "./models-list-result.openai-routes.test-support.js";
710

8-
const mocks = vi.hoisted(() => ({
9-
readClaudeCliCredentialsCached: vi.fn<() => unknown>(() => null),
10-
readCodexCliCredentialsCached: vi.fn<() => unknown>(() => null),
11-
readMiniMaxCliCredentialsCached: vi.fn<() => unknown>(() => null),
12-
}));
13-
14-
vi.mock("../../agents/cli-credentials.js", () => mocks);
11+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
1512

1613
const config = {
1714
agents: {
@@ -40,26 +37,28 @@ async function listClaudeCliModel() {
4037
describe("models.list CLI runtime availability", () => {
4138
beforeEach(() => {
4239
vi.stubEnv("ANTHROPIC_API_KEY", "");
43-
mocks.readClaudeCliCredentialsCached.mockReset();
44-
mocks.readClaudeCliCredentialsCached.mockReturnValue(null);
45-
mocks.readCodexCliCredentialsCached.mockReset();
46-
mocks.readCodexCliCredentialsCached.mockReturnValue(null);
47-
mocks.readMiniMaxCliCredentialsCached.mockReset();
48-
mocks.readMiniMaxCliCredentialsCached.mockReturnValue(null);
4940
});
5041

5142
afterEach(() => {
5243
vi.unstubAllEnvs();
5344
});
5445

5546
it("marks a Claude CLI runtime model available with ambient CLI OAuth", async () => {
56-
mocks.readClaudeCliCredentialsCached.mockReturnValue({
57-
type: "oauth",
58-
provider: "anthropic",
59-
access: "test-access",
60-
refresh: "test-refresh",
61-
expires: Date.now() + 3_600_000,
62-
});
47+
const homeDir = tempDirs.make("models-list-claude-cli-");
48+
const credentialDir = path.join(homeDir, ".claude");
49+
fs.mkdirSync(credentialDir, { recursive: true, mode: 0o700 });
50+
fs.writeFileSync(
51+
path.join(credentialDir, ".credentials.json"),
52+
JSON.stringify({
53+
claudeAiOauth: {
54+
accessToken: "test-access",
55+
refreshToken: "test-refresh",
56+
expiresAt: Date.now() + 3_600_000,
57+
},
58+
}),
59+
{ mode: 0o600 },
60+
);
61+
vi.stubEnv("HOME", homeDir);
6362

6463
await expect(listClaudeCliModel()).resolves.toEqual({
6564
models: [expect.objectContaining({ id: "claude-opus-5", available: true })],

‎ui/src/api/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ export type {
555555
export type CronRunStatus = NonNullable<ProtocolCronRunLogEntry["status"]>;
556556
export type CronDeliveryStatus = NonNullable<ProtocolCronRunLogEntry["deliveryStatus"]>;
557557
export type CronJobsEnabledFilter = NonNullable<CronListParams["enabled"]>;
558+
export type CronJobsTriggerFilter = NonNullable<CronListParams["trigger"]>;
558559
export type CronJobsSortBy = NonNullable<CronListParams["sortBy"]>;
559560
export type CronRunScope = NonNullable<CronRunsParams["scope"]>;
560561
export type CronRunsStatusValue = NonNullable<CronRunsParams["statuses"]>[number];

0 commit comments

Comments
 (0)