mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(core): gate scheduled prompts with shell checks (#60137)
This commit is contained in:
committed by
GitHub
Unverified
parent
756f832902
commit
c784d17b83
@@ -69,10 +69,49 @@ export const Action = Schema.Union([PromptAction]).pipe(
|
||||
)
|
||||
export type Action = typeof Action.Type
|
||||
|
||||
/**
|
||||
* Runs `run` in the Session's directory before firing, with no model call. With none of `is`, `matches` or `changes`
|
||||
* set, exit 0 wakes and any non-zero exit is "not yet". With `is`, `matches` or `changes`, non-zero exit is a check
|
||||
* error, and exit 0 wakes when trimmed stdout equals `is`, stdout matches `matches`, or stdout differs from the
|
||||
* previous run (`changes`).
|
||||
*/
|
||||
export const ShellCondition = Schema.Struct({
|
||||
type: Schema.tag("shell"),
|
||||
run: Schema.String,
|
||||
is: Schema.String.pipe(optional),
|
||||
matches: Schema.String.pipe(optional),
|
||||
changes: Schema.Boolean.pipe(optional),
|
||||
}).annotate({ identifier: "Schedule.Condition.Shell" })
|
||||
export type ShellCondition = typeof ShellCondition.Type
|
||||
|
||||
/** Checked before firing; the schedule wakes the Session only when the condition passes. */
|
||||
export const Condition = Schema.Union([ShellCondition]).pipe(
|
||||
Schema.toTaggedUnion("type"),
|
||||
Schema.annotate({ identifier: "Schedule.Condition" }),
|
||||
)
|
||||
export type Condition = typeof Condition.Type
|
||||
|
||||
export const CheckMode = Schema.Literals(["if", "until"]).annotate({ identifier: "Schedule.CheckMode" })
|
||||
export type CheckMode = typeof CheckMode.Type
|
||||
|
||||
export const StoredCheck = Schema.Struct({
|
||||
mode: CheckMode,
|
||||
condition: Condition,
|
||||
}).annotate({ identifier: "Schedule.StoredCheck" })
|
||||
export type StoredCheck = typeof StoredCheck.Type
|
||||
|
||||
export const Status = Schema.Literals(["active", "paused", "done"]).annotate({ identifier: "Schedule.Status" })
|
||||
export type Status = typeof Status.Type
|
||||
|
||||
export const Outcome = Schema.Literals(["fired", "late", "skipped", "disabled", "failed"]).annotate({
|
||||
export const Outcome = Schema.Literals([
|
||||
"fired",
|
||||
"late",
|
||||
"checked",
|
||||
"expired",
|
||||
"skipped",
|
||||
"disabled",
|
||||
"failed",
|
||||
]).annotate({
|
||||
identifier: "Schedule.Outcome",
|
||||
})
|
||||
export type Outcome = typeof Outcome.Type
|
||||
@@ -83,6 +122,8 @@ export const Info = Schema.Struct({
|
||||
name: Schema.String.pipe(optional),
|
||||
when: When,
|
||||
tz: Schema.String,
|
||||
if: Condition.pipe(optional),
|
||||
until: Condition.pipe(optional),
|
||||
action: Action,
|
||||
status: Status,
|
||||
nextDueAt: DateTimeUtcFromMillis.pipe(optional),
|
||||
@@ -91,6 +132,7 @@ export const Info = Schema.Struct({
|
||||
runCount: NonNegativeInt,
|
||||
lastFiredAt: DateTimeUtcFromMillis.pipe(optional),
|
||||
lastOutcome: Outcome.pipe(optional),
|
||||
lastOutput: Schema.String.pipe(optional),
|
||||
time: Schema.Struct({ created: DateTimeUtcFromMillis, updated: DateTimeUtcFromMillis }),
|
||||
}).annotate({ identifier: "Schedule.Info" })
|
||||
export type Info = typeof Info.Type
|
||||
@@ -98,7 +140,9 @@ export type Info = typeof Info.Type
|
||||
export const CreateInput = Schema.Struct({
|
||||
sessionID: SessionID,
|
||||
name: Schema.String.pipe(optional),
|
||||
when: When,
|
||||
when: When.pipe(optional),
|
||||
if: Condition.pipe(optional),
|
||||
until: Condition.pipe(optional),
|
||||
ends: Ends.pipe(optional),
|
||||
action: Action,
|
||||
}).annotate({ identifier: "Schedule.CreateInput" })
|
||||
@@ -116,18 +160,42 @@ export const WhenInput = Schema.Union([
|
||||
]).annotate({ identifier: "Schedule.WhenInput" })
|
||||
export type WhenInput = typeof WhenInput.Type
|
||||
|
||||
/** A schedule as callers describe it: when it fires, when it stops, and the prompt it wakes the Session with. */
|
||||
export const ShellConditionInput = Schema.Struct({
|
||||
run: Schema.String.check(Schema.isMinLength(1)).annotate({
|
||||
description: "Shell command to run in the Session's directory. Alone, exit 0 wakes and stdout is appended.",
|
||||
}),
|
||||
is: Schema.String.pipe(optional).annotate({
|
||||
description: "Wake when exit is 0 and trimmed stdout equals this value.",
|
||||
}),
|
||||
matches: Schema.String.pipe(optional).annotate({
|
||||
description: "Wake when exit is 0 and stdout matches this regular expression.",
|
||||
}),
|
||||
changes: Schema.Boolean.pipe(optional).annotate({
|
||||
description: "Wake when exit is 0 and stdout differs from the previous check.",
|
||||
}),
|
||||
}).annotate({ identifier: "Schedule.ConditionInput" })
|
||||
export type ConditionInput = typeof ShellConditionInput.Type
|
||||
export const ConditionInput = ShellConditionInput
|
||||
|
||||
/** A schedule as callers describe it: when it fires, optional condition, when it stops, and the prompt it sends. */
|
||||
export const Request = Schema.Struct({
|
||||
name: Schema.String.pipe(optional).annotate({ description: "A short label for the schedule." }),
|
||||
when: WhenInput.annotate({
|
||||
when: WhenInput.pipe(optional).annotate({
|
||||
description:
|
||||
'When it fires: { in: "20 minutes" }, { at: "2026-10-27T09:00" }, { every: "1 hour" } or { cron: "0 9 * * 1-5" }. Wall-clock times use tz.',
|
||||
'When it fires: { in: "20 minutes" }, { at: "2026-10-27T09:00" }, { every: "1 hour" } or { cron: "0 9 * * 1-5" }. Optional with `until` (defaults to { every: "30 seconds" }).',
|
||||
}),
|
||||
if: ConditionInput.pipe(optional).annotate({
|
||||
description: "Recurring gate: check on each slot with no model call, and wake on every slot where it passes.",
|
||||
}),
|
||||
until: ConditionInput.pipe(optional).annotate({
|
||||
description:
|
||||
'One-shot gate: poll with no model call, wake once when it passes, and stop. Defaults ends.after to "24 hours".',
|
||||
}),
|
||||
ends: Ends.pipe(optional).annotate({
|
||||
description: 'Stops a repeating schedule at the first limit reached: { after: "8 hours" }, { at }, { times: 3 }.',
|
||||
}),
|
||||
prompt: Schema.String.check(Schema.isMinLength(1)).annotate({
|
||||
description: "The message the Session receives each time the schedule fires.",
|
||||
description: "The message the Session receives when the schedule wakes.",
|
||||
}),
|
||||
}).annotate({ identifier: "Schedule.Request" })
|
||||
export type Request = typeof Request.Type
|
||||
@@ -135,11 +203,21 @@ export type Request = typeof Request.Type
|
||||
export const toCreateInput = (sessionID: SessionID, request: Request): CreateInput => ({
|
||||
sessionID,
|
||||
name: request.name,
|
||||
when: toWhen(request.when),
|
||||
when: request.when === undefined ? undefined : toWhen(request.when),
|
||||
if: request.if === undefined ? undefined : toCondition(request.if),
|
||||
until: request.until === undefined ? undefined : toCondition(request.until),
|
||||
ends: request.ends,
|
||||
action: { type: "prompt", prompt: request.prompt },
|
||||
})
|
||||
|
||||
const toCondition = (condition: ConditionInput): Condition => ({
|
||||
type: "shell",
|
||||
run: condition.run,
|
||||
is: condition.is,
|
||||
matches: condition.matches,
|
||||
changes: condition.changes,
|
||||
})
|
||||
|
||||
const toWhen = (when: WhenInput): When => {
|
||||
if (when.in !== undefined) return { type: "in", in: when.in, tz: when.tz }
|
||||
if (when.at !== undefined) return { type: "at", at: when.at, tz: when.tz }
|
||||
|
||||
Reference in New Issue
Block a user