Jobs are scheduled tasks that run automatically at specified times or intervals, without requiring user interaction. They enable your agent to perform automated operations like sending reports, cleaning up data, or monitoring systems.
Think of it as:
A scheduled assistant - tasks that run on their own schedule, like daily reports or weekly cleanup
New in v3.0.0: Two ways to create jobs - pre-defined with LuaJob or dynamically with Jobs API.
import { LuaJob, User } from 'lua-cli';const dailyReport = new LuaJob({ name: 'daily-sales-report', schedule: { type: 'cron', expression: '0 9 * * *' // Every day at 9 AM }, metadata: { userId: 'user_abc123' // Store user ID in metadata }, execute: async (job) => { // ⚠️ Pre-defined jobs have NO conversational context // Get user by ID from metadata const user = await User.get(job.metadata.userId); await user.send([{ type: 'text', text: 'Your daily report is ready!' }]); }});
Best for:
Daily/weekly reports
Cleanup tasks
System monitoring
Recurring notifications
No Context: LuaJob executes outside conversations. Use User.get(userId) with ID from metadata.
Jobs created on-demand from tools (has user context!)
Copy
import { Jobs } from 'lua-cli';// Inside a tool's execute functionconst job = await Jobs.create({ name: 'user-reminder', metadata: { message: 'Meeting in 10 minutes' }, schedule: { type: 'once', executeAt: new Date(Date.now() + 600000) }, execute: async (jobInstance) => { // ✅ Dynamic jobs automatically have user context const user = await jobInstance.user(); // No userId needed! await user.send([{ type: 'text', text: jobInstance.metadata.message }]); }});
Best for:
User-requested reminders
Follow-up messages
One-time notifications
Context-specific tasks
Has Context: Dynamic jobs created in tools automatically know which user triggered them. Use jobInstance.user() - no userId needed!