# Cron Job

> Schedule jobs; fire events for other plugins (IPC) or run AI Chat agents on a schedule.

| | |
|---|---|
| Version | 1.0.0 |
| Category | Productivity |
| Permissions | process.control |
| Depends on | agent-role, ai-chat |

# Cron Job

Schedule jobs; fire events for other plugins (IPC) or run AI Chat agents on a schedule.

**Type:** `backend` — see `.claude/skills/kazuo-create-plugin/references/plugin-examples.md`

## Files

- `manifest.json` — metadata, `defaultSettings`, `defaultWidgetConfig`
- `widget.html` / `js/widget.js` / `css/widget.css` — main UI
  - has its own "⚙" button + settings overlay built into widget.html/widget.js (host no longer provides a per-widget gear icon — see references/plugin-runtime-api.md section 1b)
- `settings.html` / `js/settings.js` / `css/settings.css` — same fields as the widget overlay (intentionally duplicated), used by the Plugin Manager screen and the pop-out window
- `locales/en.json5` / `locales/vi.json5` — UI strings, `widget.*`/`settings.*` namespaces
- `main/index.ts` (+ compiled `main/index.js`) — runs in the Electron main process; `activate`/`deactivate` + optional `getMcpTools` (exposed to ai-chat's AI agent, Dev Mode only)

## Job actions

Mỗi job có 1 `action` chạy khi tới giờ:

- `event` — `broadcast` tín hiệu tới event channel của 1 plugin đích (renderer/widget nhận).
- `agent` — chạy 1 lượt AI Chat agent headless qua `globalThis.__kazuoAiChat.runAgent`.
- `service` — gọi headless 1 service trong registry global `globalThis.__kazuoPluginServices[pluginId][method](payload)`.
  Dùng khi plugin đích cần chạy nền mà không mở widget (vd backup theo lịch của `backup-manager`).
  Spec: `{ type:'service', pluginId, method, payload? }`. Nếu service chưa đăng ký (plugin tắt) → job ghi
  `lastRun` lỗi rõ, tick không crash. Service phải **ném lỗi** thì lượt chạy mới bị ghi là lỗi.
  Xem `references/plugin-runtime-api.md` mục 4d.

Ngược lại, plugin khác (ở tiến trình nền) tạo/liệt kê/xoá job qua dịch vụ
`globalThis.__kazuoPluginServices['cron-job']` — `createJob(spec, pluginId)`, `listJobs()`,
`deleteJob(id)`, `setEnabled(id, enabled)`. KChat dùng nó để model tự đặt lịch.

## Default settings

- `jobs` — default: `[]`

## Test

Open Kazuo AI → Plugin Manager — this plugin appears automatically (the host scans `plugins-library/` on its own, no registration needed) → Install → Enable.

After editing `main/index.ts`, recompile before testing: `npx tsc -p tsconfig.plugins.json` (run from the project root).
