@pantheon.ai/agents 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +321 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { createCommand } from "commander";
4
4
  import expandTilde from "expand-tilde";
5
5
  import path from "node:path";
6
6
  import * as process$1 from "node:process";
7
+ import readline from "node:readline/promises";
7
8
  import { multistream, pino, transport } from "pino";
8
9
  import * as fs$1 from "node:fs";
9
10
  import { Kysely, MysqlDialect } from "kysely";
@@ -15,7 +16,7 @@ var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).export
15
16
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
16
17
 
17
18
  //#endregion
18
- //#region schemas/task-list.ts
19
+ //#region src/schemas/task-list.ts
19
20
  const taskItemSchema = z.discriminatedUnion("status", [
20
21
  z.object({
21
22
  status: z.literal("pending"),
@@ -70,13 +71,13 @@ const taskItemSchema = z.discriminatedUnion("status", [
70
71
  ]);
71
72
 
72
73
  //#endregion
73
- //#region utils/get-error-message.ts
74
+ //#region src/utils/get-error-message.ts
74
75
  function getErrorMessage(error) {
75
76
  return String(error?.message ?? error);
76
77
  }
77
78
 
78
79
  //#endregion
79
- //#region task-list/task-list-provider.ts
80
+ //#region src/providers/task-list-provider.ts
80
81
  var TaskListProvider = class {
81
82
  logger;
82
83
  agentName;
@@ -156,7 +157,7 @@ var TaskListProvider = class {
156
157
  };
157
158
 
158
159
  //#endregion
159
- //#region task-list/task-list-tidb-provider.ts
160
+ //#region src/providers/task-list-tidb-provider.ts
160
161
  var TaskListTidbProvider = class extends TaskListProvider {
161
162
  db;
162
163
  constructor(agentName, logger) {
@@ -178,6 +179,9 @@ var TaskListTidbProvider = class extends TaskListProvider {
178
179
  if (config == null) return null;
179
180
  return config;
180
181
  }
182
+ async getAgentConfigs() {
183
+ return await this.db.selectFrom("agent_project_config").selectAll().where("agent", "=", this.agentName).execute();
184
+ }
181
185
  async setAgentConfig({ skills, ...config }) {
182
186
  await this.db.insertInto("agent_project_config").values({
183
187
  agent: this.agentName,
@@ -196,6 +200,9 @@ var TaskListTidbProvider = class extends TaskListProvider {
196
200
  if (order_by) builder = builder.orderBy(order_by, order_direction);
197
201
  return (await builder.execute()).map((item) => taskItemSchema.parse(item));
198
202
  }
203
+ async listAgentNames() {
204
+ return (await this.db.selectFrom("task").select("agent").distinct().execute()).map((item) => item.agent);
205
+ }
199
206
  async updateTask(taskItem) {
200
207
  const { id, started_at, ended_at, queued_at, ...rest } = taskItem;
201
208
  await this.db.updateTable("task").set({
@@ -219,6 +226,10 @@ var TaskListTidbProvider = class extends TaskListProvider {
219
226
  id: String(insertId)
220
227
  };
221
228
  }
229
+ async deleteTask(taskId) {
230
+ const result = await this.db.deleteFrom("task").where("id", "=", taskId).where("agent", "=", this.agentName).executeTakeFirst();
231
+ return Number(result.numDeletedRows ?? 0) > 0;
232
+ }
222
233
  };
223
234
 
224
235
  //#endregion
@@ -1326,7 +1337,7 @@ function getServerErrorMessage(error) {
1326
1337
  }
1327
1338
 
1328
1339
  //#endregion
1329
- //#region core/pantheon.ts
1340
+ //#region src/core/pantheon.ts
1330
1341
  const executor = createApiExecutor({
1331
1342
  baseUrl: "https://pantheon-ai.tidb.ai",
1332
1343
  headers: { Authorization: `Bearer ${process.env.PANTHEON_API_KEY}` },
@@ -1376,7 +1387,7 @@ async function executeOnPantheon({ projectId, branchId, prompt, agent }) {
1376
1387
  }
1377
1388
 
1378
1389
  //#endregion
1379
- //#region core/task-list.ts
1390
+ //#region src/core/task-list.ts
1380
1391
  async function startTaskListLoop(agentName, { loopInterval = 5 }, logger) {
1381
1392
  logger.info("Starting task list loop...");
1382
1393
  let i = 0;
@@ -1458,7 +1469,7 @@ async function startPendingTask(provider, task, logger) {
1458
1469
  }
1459
1470
 
1460
1471
  //#endregion
1461
- //#region core/index.ts
1472
+ //#region src/core/index.ts
1462
1473
  async function runAgent(name, options, logger) {
1463
1474
  const agentDir = path.join(options.dataDir, "agents", name);
1464
1475
  const pidFile = path.join(agentDir, "pid");
@@ -1504,7 +1515,7 @@ async function configAgent(name, options) {
1504
1515
  1. Clone the main branch from https://github.com/pingcap-inc/pantheon-agents to a temporary directory.
1505
1516
  2. Copy the pantheon-agents/roles/${options.role}/AGENTS.md to \`<workspace>/AGENTS.md\`.
1506
1517
  3. Copy the pantheon-agents/roles/${options.role}/skills directory to \`<workspace>/.codex/skills\` if exists.
1507
- ${options.skills.length > 0 ? `4. Copy the pantheon-agents/skills/\{${options.skills.join(",")}} directory to \`<workspace>/.codex/\`` : ""}
1518
+ ${options.skills.length > 0 ? `4. Copy the pantheon-agents/skills/\{${options.skills.join(",")}} directory to \`<workspace>/.codex/skills/\`` : ""}
1508
1519
 
1509
1520
  Validate <workspace>: check if files and directorys exists:
1510
1521
  - AGENTS.md
@@ -1577,6 +1588,145 @@ async function addTask(name, options) {
1577
1588
  console.log(`Task ${task.task} queued successfully.`);
1578
1589
  await provider.close();
1579
1590
  }
1591
+ async function deleteTask(agentName, taskId) {
1592
+ const provider = new TaskListTidbProvider(agentName, pino());
1593
+ try {
1594
+ const task = await provider.getTask(taskId);
1595
+ if (!task) return null;
1596
+ if (!await provider.deleteTask(taskId)) return null;
1597
+ return task;
1598
+ } finally {
1599
+ await provider.close();
1600
+ }
1601
+ }
1602
+ async function showAgentConfig(agentName, projectId) {
1603
+ const provider = new TaskListTidbProvider(agentName, pino());
1604
+ try {
1605
+ return await provider.getAgentConfig(projectId);
1606
+ } finally {
1607
+ await provider.close();
1608
+ }
1609
+ }
1610
+ async function showAgentConfigs(agentName) {
1611
+ const provider = new TaskListTidbProvider(agentName, pino());
1612
+ try {
1613
+ return await provider.getAgentConfigs();
1614
+ } finally {
1615
+ await provider.close();
1616
+ }
1617
+ }
1618
+ async function showTasks(name, options) {
1619
+ const provider = new TaskListTidbProvider(name, pino());
1620
+ try {
1621
+ return await provider.getTasks({
1622
+ status: options.status,
1623
+ order_by: options.orderBy,
1624
+ order_direction: options.orderDirection
1625
+ });
1626
+ } finally {
1627
+ await provider.close();
1628
+ }
1629
+ }
1630
+ async function listAgentNames() {
1631
+ const provider = new TaskListTidbProvider("list-agents", pino());
1632
+ try {
1633
+ return await provider.listAgentNames();
1634
+ } finally {
1635
+ await provider.close();
1636
+ }
1637
+ }
1638
+ async function showTasksForAgents(options) {
1639
+ const selectedAgents = options.allAgents ? await listAgentNames() : options.agents;
1640
+ const tasksWithAgent = await Promise.all(selectedAgents.map(async (agent) => {
1641
+ return (await showTasks(agent, {
1642
+ status: options.status,
1643
+ orderBy: options.orderBy,
1644
+ orderDirection: options.orderDirection
1645
+ })).map((task) => ({
1646
+ agent,
1647
+ task
1648
+ }));
1649
+ })).then((groups) => groups.flat());
1650
+ const orderKey = options.orderBy ?? "queued_at";
1651
+ const orderDirection = options.orderDirection ?? "desc";
1652
+ tasksWithAgent.sort((a, b) => {
1653
+ const aValue = a.task[orderKey];
1654
+ const bValue = b.task[orderKey];
1655
+ const aTime = aValue instanceof Date ? aValue.getTime() : 0;
1656
+ const bTime = bValue instanceof Date ? bValue.getTime() : 0;
1657
+ return orderDirection === "asc" ? aTime - bTime : bTime - aTime;
1658
+ });
1659
+ if (options.limit != null) return tasksWithAgent.slice(0, options.limit);
1660
+ return tasksWithAgent;
1661
+ }
1662
+ function formatRelativeTime(value) {
1663
+ if (!value) return "";
1664
+ const diffMs = Date.now() - value.getTime();
1665
+ const absMs = Math.abs(diffMs);
1666
+ const tense = diffMs >= 0 ? "ago" : "from now";
1667
+ const seconds = Math.floor(absMs / 1e3);
1668
+ const minutes = Math.floor(seconds / 60);
1669
+ const hours = Math.floor(minutes / 60);
1670
+ const days = Math.floor(hours / 24);
1671
+ if (days > 0) return `${days}d ${tense}`;
1672
+ if (hours > 0) return `${hours}h ${tense}`;
1673
+ if (minutes > 0) return `${minutes}m ${tense}`;
1674
+ return `${seconds}s ${tense}`;
1675
+ }
1676
+ function formatDate(value) {
1677
+ return value ? value.toISOString() : "";
1678
+ }
1679
+ function truncateText(value, maxLength) {
1680
+ if (value.length <= maxLength) return value;
1681
+ return `${value.slice(0, Math.max(0, maxLength - 1))}…`;
1682
+ }
1683
+ function colorText(value, colorCode) {
1684
+ return `\u001b[${colorCode}m${value}\u001b[0m`;
1685
+ }
1686
+ function formatStatus(status, useColor) {
1687
+ if (!useColor) return status;
1688
+ switch (status) {
1689
+ case "pending": return colorText(status, 33);
1690
+ case "running": return colorText(status, 36);
1691
+ case "completed": return colorText(status, 32);
1692
+ case "failed": return colorText(status, 31);
1693
+ case "cancelled": return colorText(status, 90);
1694
+ }
1695
+ }
1696
+ function formatConciseTaskLine(options) {
1697
+ const { agent, task, maxTaskLength, useColor } = options;
1698
+ let timestamp = task.queued_at;
1699
+ if (task.status === "running") timestamp = task.started_at;
1700
+ if (task.status === "completed" || task.status === "failed") timestamp = task.ended_at;
1701
+ if (task.status === "cancelled") timestamp = task.cancelled_at ?? task.queued_at;
1702
+ const statusText = formatStatus(task.status, useColor);
1703
+ const timeText = formatRelativeTime(timestamp);
1704
+ const taskText = truncateText(task.task, maxTaskLength);
1705
+ return [
1706
+ agent,
1707
+ statusText,
1708
+ task.id,
1709
+ timeText,
1710
+ taskText
1711
+ ].filter((part) => part !== "").join(" ");
1712
+ }
1713
+ function formatTaskTableRow(options) {
1714
+ const { agent, task, maxTaskLength } = options;
1715
+ const branchId = "branch_id" in task ? task.branch_id ?? "" : "";
1716
+ const startedAt = "started_at" in task ? task.started_at : void 0;
1717
+ const endedAt = "ended_at" in task ? task.ended_at : void 0;
1718
+ return {
1719
+ agent,
1720
+ id: task.id,
1721
+ status: task.status,
1722
+ project_id: task.project_id,
1723
+ branch_id: branchId,
1724
+ queued_at: formatDate(task.queued_at),
1725
+ started_at: formatDate(startedAt),
1726
+ ended_at: formatDate(endedAt),
1727
+ task: truncateText(task.task, maxTaskLength)
1728
+ };
1729
+ }
1580
1730
  async function assertsSingleton(logger, pidFile) {
1581
1731
  try {
1582
1732
  const pid = await fs$1.promises.readFile(pidFile, "utf-8");
@@ -1969,10 +2119,26 @@ var require_cli_options = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1969
2119
 
1970
2120
  //#endregion
1971
2121
  //#region package.json
1972
- var version = "0.0.3";
2122
+ var version = "0.0.5";
1973
2123
 
1974
2124
  //#endregion
1975
- //#region index.ts
2125
+ //#region src/cli/index.ts
2126
+ const taskStatuses = [
2127
+ "pending",
2128
+ "running",
2129
+ "completed",
2130
+ "failed",
2131
+ "cancelled"
2132
+ ];
2133
+ const orderByFields = [
2134
+ "queued_at",
2135
+ "started_at",
2136
+ "ended_at"
2137
+ ];
2138
+ const orderDirections = ["asc", "desc"];
2139
+ function parseCommaList(value) {
2140
+ return value.split(",").map((item) => item.trim()).filter((item) => item !== "");
2141
+ }
1976
2142
  if (!process$1.env.PANTHEON_API_KEY) console.error("PANTHEON_API_KEY environment variable is not set.");
1977
2143
  if (!process$1.env.DATABASE_URL) console.error("DATABASE_URL environment variable is not set.");
1978
2144
  const configAgentCommand = createCommand("pantheon-agents config").version(version).description("Configure agent for pantheon project").argument("<name>", "The name of the agent.").argument("<role>", "The role of the agent.").argument("<project-id>", "The project id of the agent.").option("--skills <skills>", "The skills of the agent. Multiple values are separated by comma.", (val) => val.split(",").map((s) => s.trim()).filter((s) => s !== ""), []).option("--execute-agent <agent>", "The execute agent of the agent.", "codex").option("--root-branch-id <branchId>", "The root branch id of the agent. Default to project root branch id.").option("--no-bootstrap", "Prevent bootstrap base branch for agent. Use the root branch as base branch.").action(async function() {
@@ -1990,6 +2156,74 @@ const addTaskCommand = createCommand("pantheon-agents add-task").version(version
1990
2156
  prompt: taskPrompt
1991
2157
  });
1992
2158
  });
2159
+ const deleteTaskCommand = createCommand("pantheon-agents delete-task").version(version).description("Delete a task for an agent").argument("<name>", "The name of the agent.").argument("<task-id>", "The id of the task.").action(async function() {
2160
+ const [name, taskId] = this.args;
2161
+ const rl = readline.createInterface({
2162
+ input: process$1.stdin,
2163
+ output: process$1.stdout
2164
+ });
2165
+ try {
2166
+ if ((await rl.question(`Type the task id (${taskId}) to confirm deletion: `)).trim() !== taskId) {
2167
+ console.error("Confirmation failed. Task id did not match.");
2168
+ process$1.exit(1);
2169
+ }
2170
+ if ((await rl.question("Type DELETE to permanently remove this task: ")).trim() !== "DELETE") {
2171
+ console.error("Confirmation failed. Aborting deletion.");
2172
+ process$1.exit(1);
2173
+ }
2174
+ const deletedTask = await deleteTask(name, taskId);
2175
+ if (!deletedTask) {
2176
+ console.error(`Task ${taskId} not found for agent ${name}.`);
2177
+ process$1.exit(1);
2178
+ }
2179
+ console.log(`Deleted task ${taskId} for agent ${name}. Status was ${deletedTask.status}.`);
2180
+ } finally {
2181
+ rl.close();
2182
+ }
2183
+ });
2184
+ const showConfigCommand = createCommand("pantheon-agents show-config").version(version).description("Show agent config for a project").argument("<name>", "The name of the agent.").argument("[project-id]", "The project id.").option("--json", "Output config as JSON.").action(async function() {
2185
+ const [name, projectId] = this.args;
2186
+ const options = this.opts();
2187
+ if (projectId) {
2188
+ const config = await showAgentConfig(name, projectId);
2189
+ if (!config) {
2190
+ console.error(`No config found for agent ${name} and project ${projectId}.`);
2191
+ process$1.exit(1);
2192
+ }
2193
+ if (options.json) {
2194
+ console.log(JSON.stringify(config, null, 2));
2195
+ return;
2196
+ }
2197
+ console.table([{
2198
+ agent: config.agent,
2199
+ project_id: config.project_id,
2200
+ base_branch_id: config.base_branch_id,
2201
+ role: config.role,
2202
+ execute_agent: config.execute_agent,
2203
+ prototype_url: config.prototype_url,
2204
+ skills: Array.isArray(config.skills) ? config.skills.join(", ") : String(config.skills)
2205
+ }]);
2206
+ return;
2207
+ }
2208
+ const configs = await showAgentConfigs(name);
2209
+ if (!configs.length) {
2210
+ console.error(`No configs found for agent ${name}.`);
2211
+ process$1.exit(1);
2212
+ }
2213
+ if (options.json) {
2214
+ console.log(JSON.stringify(configs, null, 2));
2215
+ return;
2216
+ }
2217
+ console.table(configs.map((config) => ({
2218
+ agent: config.agent,
2219
+ project_id: config.project_id,
2220
+ base_branch_id: config.base_branch_id,
2221
+ role: config.role,
2222
+ execute_agent: config.execute_agent,
2223
+ prototype_url: config.prototype_url,
2224
+ skills: Array.isArray(config.skills) ? config.skills.join(", ") : String(config.skills)
2225
+ })));
2226
+ });
1993
2227
  const runAgentCommand = createCommand("pantheon-agents run").version(version).description("Start a pantheon agents").argument("<name>", "The name of the agent.").option("--data-dir [dir]", "Data directory.", expandTilde("~/.pantheon-agents")).option("--mcp-port", "The port of the MCP server. Defaults to a random port.").option("--loop-interval <seconds>", "The interval of the loop in seconds. Defaults to 5.", (val) => parseInt(val, 10), 5).action(async function() {
1994
2228
  const [name] = this.args;
1995
2229
  const options = this.opts();
@@ -2015,6 +2249,79 @@ const runAgentCommand = createCommand("pantheon-agents run").version(version).de
2015
2249
  } }
2016
2250
  }, multistream([{ stream: logFileTransport }, { stream: prettyTransport }])));
2017
2251
  });
2252
+ const showTasksCommand = createCommand("pantheon-agents show-tasks").version(version).description("Show tasks for an agent").argument("[name]", "The name of the agent.").option("--agents <names>", "Comma-separated agent names to query in one call.", parseCommaList, []).option("--all", "Show tasks for all agents.").option("--status <status>", "Filter tasks by status. Multiple values are separated by comma.", parseCommaList, []).option("--order-by <field>", "Order by queued_at, started_at, or ended_at.", "queued_at").option("--order-direction <direction>", "Order direction: asc or desc.", "desc").option("--limit <number>", "Limit the number of tasks shown.", (val) => parseInt(val, 10)).option("--max-task-length <number>", "Maximum task length for table output.", (val) => parseInt(val, 10), 120).option("--full-task", "Do not truncate task text in output.").option("--concise", "Output concise one-line entries.", true).option("--no-color", "Disable colored output.").option("--json", "Output tasks as JSON.").action(async function() {
2253
+ const [name] = this.args;
2254
+ const options = this.opts();
2255
+ const agentNames = /* @__PURE__ */ new Set();
2256
+ if (name) agentNames.add(name);
2257
+ options.agents.forEach((agent) => agentNames.add(agent));
2258
+ if (options.all && agentNames.size > 0) {
2259
+ console.error("Use either a specific agent name or --all, not both.");
2260
+ process$1.exit(1);
2261
+ }
2262
+ if (!options.all && agentNames.size === 0) {
2263
+ console.error("Provide an agent name, --agents, or --all.");
2264
+ process$1.exit(1);
2265
+ }
2266
+ const invalidStatuses = options.status.filter((status) => !taskStatuses.includes(status));
2267
+ if (invalidStatuses.length > 0) {
2268
+ console.error(`Invalid status values: ${invalidStatuses.join(", ")}. Allowed: ${taskStatuses.join(", ")}.`);
2269
+ process$1.exit(1);
2270
+ }
2271
+ if (!orderByFields.includes(options.orderBy)) {
2272
+ console.error(`Invalid order-by value: ${options.orderBy}. Allowed: ${orderByFields.join(", ")}.`);
2273
+ process$1.exit(1);
2274
+ }
2275
+ if (!orderDirections.includes(options.orderDirection)) {
2276
+ console.error(`Invalid order-direction value: ${options.orderDirection}. Allowed: ${orderDirections.join(", ")}.`);
2277
+ process$1.exit(1);
2278
+ }
2279
+ if (options.limit != null && Number.isNaN(options.limit)) {
2280
+ console.error("Invalid limit value. Must be a number.");
2281
+ process$1.exit(1);
2282
+ }
2283
+ if (Number.isNaN(options.maxTaskLength) || options.maxTaskLength <= 0) {
2284
+ console.error("Invalid max-task-length value. Must be a positive number.");
2285
+ process$1.exit(1);
2286
+ }
2287
+ if (options.fullTask && options.maxTaskLength) options.maxTaskLength = Number.POSITIVE_INFINITY;
2288
+ if (options.json && options.concise) {
2289
+ console.error("Use either --json or --concise, not both.");
2290
+ process$1.exit(1);
2291
+ }
2292
+ const limitedTasks = await showTasksForAgents({
2293
+ agents: Array.from(agentNames),
2294
+ allAgents: options.all,
2295
+ status: options.status,
2296
+ orderBy: options.orderBy,
2297
+ orderDirection: options.orderDirection,
2298
+ limit: options.limit
2299
+ });
2300
+ if (options.json) {
2301
+ const payload = limitedTasks.map(({ agent, task }) => ({
2302
+ agent,
2303
+ ...task
2304
+ }));
2305
+ console.log(JSON.stringify(payload, null, 2));
2306
+ return;
2307
+ }
2308
+ if (options.concise) {
2309
+ for (const { agent, task } of limitedTasks) console.log(formatConciseTaskLine({
2310
+ agent,
2311
+ task,
2312
+ maxTaskLength: options.maxTaskLength,
2313
+ useColor: options.color
2314
+ }));
2315
+ return;
2316
+ }
2317
+ const rows = limitedTasks.map(({ agent, task }) => formatTaskTableRow({
2318
+ agent,
2319
+ task,
2320
+ maxTaskLength: options.maxTaskLength
2321
+ }));
2322
+ console.table(rows);
2323
+ console.log(`${limitedTasks.length} task(s) shown.`);
2324
+ });
2018
2325
  function printCommandHelpAndExit(command) {
2019
2326
  console.error(`Invalid command: ${command}. Supported commands: ${Object.keys(commands).join(", ")}.`);
2020
2327
  console.error(` Run pantheon-agents help <command> for more information.`);
@@ -2023,7 +2330,10 @@ function printCommandHelpAndExit(command) {
2023
2330
  const commands = {
2024
2331
  "add-task": addTaskCommand,
2025
2332
  config: configAgentCommand,
2026
- run: runAgentCommand
2333
+ "delete-task": deleteTaskCommand,
2334
+ run: runAgentCommand,
2335
+ "show-config": showConfigCommand,
2336
+ "show-tasks": showTasksCommand
2027
2337
  };
2028
2338
  if (process$1.argv[2] === "help") {
2029
2339
  const command = process$1.argv[3];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pantheon.ai/agents",
3
3
  "type": "module",
4
- "version": "0.0.3",
4
+ "version": "0.0.5",
5
5
  "bin": {
6
6
  "pantheon-agents": "dist/index.js"
7
7
  },