The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import type { CronJob, CronJobUpdates } from '@/types/hermes'
import { truncateText } from './job-state'
const asText = (value: unknown): string => (typeof value === 'string' ? value : '')
/** Script-only cron jobs run a shell script on schedule with no LLM prompt. */
export function jobIsScriptOnly(job: Pick<CronJob, 'no_agent' | 'script'>): boolean {
return Boolean(job.no_agent) && Boolean(asText(job.script).trim())
}
/** Description shown for a cron job: the prompt, or the script when script-only. */
export function jobDescription(job: Pick<CronJob, 'prompt' | 'script'>): string {
return asText(job.prompt) || asText(job.script)
}
export type CronEditorValidationError = 'prompt' | 'prompt_and_schedule' | 'schedule'
export interface CronEditorValidationInput {
prompt: string
schedule: string
scriptOnlyJob: boolean
}
export function validateCronEditor(input: CronEditorValidationInput): CronEditorValidationError | null {
const trimmedPrompt = input.prompt.trim()
const trimmedSchedule = input.schedule.trim()
if (!trimmedSchedule && !trimmedPrompt && !input.scriptOnlyJob) {
return 'prompt_and_schedule'
}
if (!trimmedSchedule) {
return 'schedule'
}
if (!input.scriptOnlyJob && !trimmedPrompt) {
return 'prompt'
}
return null
}
export interface CronEditorSaveValues {
/** Missed-run policy (true run once, false skip, null default); undefined = unchanged, not sent. */
catchUp?: boolean | null
deliver: string
/** Per-job model override ('' = follow the global default at fire time). */
model: string
name: string
prompt: string
/** Provider for the model override ('' = none). Always paired with model. */
provider: string
schedule: string
}
export interface CronModelChoice {
model: string
provider: string
}
/** Encode the provider/model pair as an opaque Select value without delimiter ambiguity. */
export function cronModelChoiceValue(provider: string, model: string): string {
return JSON.stringify([provider, model])
}
export function parseCronModelChoiceValue(value: string): CronModelChoice | null {
try {
const parsed: unknown = JSON.parse(value)
if (
!Array.isArray(parsed) ||
parsed.length !== 2 ||
typeof parsed[0] !== 'string' ||
typeof parsed[1] !== 'string'
) {
return null
}
return { provider: parsed[0], model: parsed[1] }
} catch {
return null
}
}
export function parseCronDeliveryTargets(value: string): string[] {
const targets = value
.split(',')
.map(target => target.trim())
.filter(Boolean)
return targets.length > 0 ? [...new Set(targets)] : ['local']
}
export function toggleCronDeliveryTarget(value: string, target: string, checked: boolean): string {
const targets = parseCronDeliveryTargets(value)
if (checked) {
return targets.includes(target) ? targets.join(',') : [...targets, target].join(',')
}
if (!targets.includes(target) || targets.length === 1) {
return targets.join(',')
}
return targets.filter(candidate => candidate !== target).join(',')
}
// The scheduler stores `last_error` as the raw exception text, e.g.
// "RuntimeError: Cron job 'x' has no model configured (job.model=None, …). Set a
// per-job model via `hermes cron edit …`". Users need the first plain sentence,
// not the Python wrapper; the full text stays reachable via a hover title.
const ERROR_PREFIX_RE = /^(?:[A-Za-z_][\w.]*(?:Error|Exception)|Exception):\s*/
const ERROR_MARKER_RE = /^\[[a-z_]+(?::[a-z_]+)?\]\s*/
const ERROR_EMOJI_RE = /^(?:\u26A0\uFE0F?|\uD83D\uDED1|\u274C|\u{1F6AB})\s*/u
const ERROR_SUMMARY_MAX = 200
export function lastErrorSummary(lastError: string | null | undefined): string {
let text = (lastError ?? '').trim()
// Wrappers can nest (marker, then emoji, then exception class); peel until stable.
for (let previous = ''; previous !== text;) {
previous = text
text = text.replace(ERROR_MARKER_RE, '').replace(ERROR_EMOJI_RE, '').replace(ERROR_PREFIX_RE, '').trimStart()
}
const sentenceEnd = text.search(/\. |\n/)
const sentence = (sentenceEnd === -1 ? text : text.slice(0, sentenceEnd + 1)).trim()
// Grapheme-aware cap (review follow-up): slice()'s UTF-16 unit limit could split a surrogate
// pair in astral error text. truncateText appends the ellipsis itself, so the old form's
// reserved slot (`max - 1`) is expressed by the max passed here.
return sentence.length > ERROR_SUMMARY_MAX ? truncateText(sentence, ERROR_SUMMARY_MAX - 1) : sentence
}
/** Build the API update payload, preserving an empty prompt on script-only jobs. */
export function cronEditorUpdates(values: CronEditorSaveValues, options: { scriptOnlyJob: boolean }): CronJobUpdates {
const updates: CronJobUpdates = {
deliver: values.deliver,
name: values.name,
schedule: values.schedule.trim()
}
const trimmedPrompt = values.prompt.trim()
if (!options.scriptOnlyJob || trimmedPrompt) {
updates.prompt = trimmedPrompt
}
// Script-only jobs never run an agent, so the scheduler ignores model
// overrides — leave whatever is stored untouched. For agent jobs, always
// write both axes so resetting to "default" clears a previous pin (the
// backend normalizes null/'' to "no override").
if (!options.scriptOnlyJob) {
updates.model = values.model.trim() || null
updates.provider = values.provider.trim() || null
}
// Sent only when the user changed it, so an edit never rewrites a policy it did not touch.
if (values.catchUp !== undefined) {
updates.catch_up = values.catchUp
}
return updates
}