TaskActions.vala 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using Inversion;
  2. using Spry;
  3. using Spry.Authentication;
  4. using Statum;
  5. using ClankerSaddle.Services;
  6. namespace ClankerSaddle {
  7. /** Sealed task id carried by the task row actions. */
  8. public class TaskActionPrivate : Object {
  9. public int64 task_id { get; set; }
  10. }
  11. /** Sealed block identity carried by {@link AnswerBlockAction}. */
  12. public class BlockAnswerPrivate : Object {
  13. public int64 task_id { get; set; }
  14. public int64 block_id { get; set; }
  15. }
  16. /**
  17. * Common base for actions sealed against a task id: materialises the
  18. * typed private blob and exposes the resolved id (falling back to the
  19. * hidden `task_id` form field).
  20. */
  21. public abstract class SealedTaskAction : TypedStatumAction<TaskActionPrivate> {
  22. protected override Type private_type { get { return typeof(TaskActionPrivate); } }
  23. /** The sealed task id, or 0 when neither blob nor form field carried one. */
  24. protected int64 sealed_task_id() {
  25. var ctx = request_private;
  26. return ctx != null ? ((!)ctx).task_id : Forms.integer(request, "task_id", 0);
  27. }
  28. }
  29. /**
  30. * Creates a task from the queue form (project and title required).
  31. * Priority clamps to 1–9; the design-task checkbox and the optional
  32. * parent id support design tasks and manual subtasks. Branch fields
  33. * are optional — `branch_from` prefills from the project's default
  34. * branch and `task_branch` is inherited from the parent task when
  35. * left blank.
  36. */
  37. public class CreateTaskAction : StatumAction {
  38. private TaskService tasks = inject<TaskService>();
  39. private UserService users = inject<UserService>();
  40. public override async DirectiveBuilder handle() throws GLib.Error {
  41. var guard = yield SpryAuth.require_login(directives(), request, users);
  42. if (guard != null) {
  43. return (!)guard;
  44. }
  45. int64 project_id = Forms.integer(request, "project_id", 0);
  46. string title = Forms.field(request, "title");
  47. if (project_id <= 0 || title.length == 0) {
  48. return directives().notify("error", "Project and title are required").navigate("/tasks");
  49. }
  50. int64? parent_id = null;
  51. int64 parsed_parent = Forms.integer(request, "parent_task_id", 0);
  52. if (parsed_parent > 0) {
  53. parent_id = parsed_parent;
  54. }
  55. try {
  56. var task = yield tasks.create_task(project_id, title,
  57. Forms.field_raw(request, "description"),
  58. Forms.integer(request, "priority", 5),
  59. Forms.field(request, "is_design_task") == "on", parent_id,
  60. Forms.optional(request, "task_branch"),
  61. Forms.optional(request, "branch_from"));
  62. if (Forms.field(request, "plan").length > 0) {
  63. yield tasks.update_plan(task.id, Forms.field_raw(request, "plan"));
  64. }
  65. return directives().notify("success", @"Task '$(task.title)' queued")
  66. .navigate(@"/tasks/$(task.id)");
  67. } catch (Error e) {
  68. return directives().notify("error", @"Could not create the task: $(e.message)").navigate("/tasks");
  69. }
  70. }
  71. }
  72. /**
  73. * Saves the editable fields of a task; the id comes from the sealed
  74. * {@link TaskActionPrivate} blob (the hidden form field is a
  75. * fallback).
  76. */
  77. public class UpdateTaskAction : SealedTaskAction {
  78. private TaskService tasks = inject<TaskService>();
  79. private UserService users = inject<UserService>();
  80. public override async DirectiveBuilder handle() throws GLib.Error {
  81. var guard = yield SpryAuth.require_login(directives(), request, users);
  82. if (guard != null) {
  83. return (!)guard;
  84. }
  85. int64 task_id = sealed_task_id();
  86. if (task_id <= 0) {
  87. return directives().navigate("/tasks");
  88. }
  89. try {
  90. int64? priority = null;
  91. if (Forms.field(request, "priority").length > 0) {
  92. priority = Forms.integer(request, "priority", 5);
  93. }
  94. yield tasks.update_task(task_id,
  95. Forms.optional(request, "title"),
  96. request.form != null ? Forms.field(request, "description") : null,
  97. priority,
  98. Forms.optional(request, "status"));
  99. yield tasks.set_branch_fields(task_id,
  100. Forms.optional(request, "task_branch"),
  101. Forms.optional(request, "branch_from"));
  102. return directives().notify("success", "Task saved").navigate(@"/tasks/$(task_id)");
  103. } catch (Error e) {
  104. return directives().notify("error", @"Could not save the task: $(e.message)")
  105. .navigate(@"/tasks/$(task_id)");
  106. }
  107. }
  108. }
  109. /**
  110. * Appends a comment to a task as the signed-in user. An optional
  111. * `quote` field carries text the user highlighted in the terminal log
  112. * or an RFC body; the quote is stored with the comment and delivered
  113. * to the saddle as a quote block.
  114. */
  115. public class AddCommentAction : SealedTaskAction {
  116. private TaskService tasks = inject<TaskService>();
  117. private UserService users = inject<UserService>();
  118. public override async DirectiveBuilder handle() throws GLib.Error {
  119. var guard = yield SpryAuth.require_login(directives(), request, users);
  120. if (guard != null) {
  121. return (!)guard;
  122. }
  123. int64 task_id = sealed_task_id();
  124. string body = Forms.field_raw(request, "body");
  125. string quote = Forms.field_raw(request, "quote");
  126. if (task_id <= 0 || body.length == 0) {
  127. return directives().notify("error", "A comment body is required").navigate(@"/tasks/$(task_id)");
  128. }
  129. var me = SpryAuth.current(request);
  130. try {
  131. yield tasks.add_comment(task_id, @"user:$(me.username)", body, quote);
  132. yield tasks.log_session_entry(task_id, "user-comment",
  133. @"comment by user:$(me.username)", quote.length > 0 ? "> " + quote.replace("\n", "\n> ") + "\n\n" + body : body);
  134. // Terminals post with a return_to so the live view survives
  135. // the comment; every other caller lands on the task page.
  136. string return_to = Forms.field(request, "return_to");
  137. string destination = return_to.length > 0 && return_to.has_prefix("/")
  138. ? return_to : @"/tasks/$(task_id)";
  139. return directives().notify("success", "Comment added").navigate(destination);
  140. } catch (Error e) {
  141. return directives().notify("error", @"Could not add the comment: $(e.message)")
  142. .navigate(@"/tasks/$(task_id)");
  143. }
  144. }
  145. }
  146. /**
  147. * Saves the task's markdown plan from the web UI's plan editor (the
  148. * human counterpart of the agent's `saddle plan`).
  149. */
  150. public class UpdatePlanAction : SealedTaskAction {
  151. private TaskService tasks = inject<TaskService>();
  152. private UserService users = inject<UserService>();
  153. public override async DirectiveBuilder handle() throws GLib.Error {
  154. var guard = yield SpryAuth.require_login(directives(), request, users);
  155. if (guard != null) {
  156. return (!)guard;
  157. }
  158. int64 task_id = sealed_task_id();
  159. if (task_id <= 0) {
  160. return directives().navigate("/tasks");
  161. }
  162. try {
  163. yield tasks.update_plan(task_id, Forms.field_raw(request, "plan"));
  164. return directives().notify("success", "Plan saved").navigate(@"/tasks/$(task_id)");
  165. } catch (Error e) {
  166. return directives().notify("error", @"Could not save the plan: $(e.message)")
  167. .navigate(@"/tasks/$(task_id)");
  168. }
  169. }
  170. }
  171. /**
  172. * Records the human's answer to a block; the task and block ids come
  173. * from the sealed {@link BlockAnswerPrivate} blob. Question blocks
  174. * submit either a chosen `option` or the free-text `other` field.
  175. * Answers are stored verbatim — the saddle daemon applies its own
  176. * "Human selected: …" formatting, so a stored prefix would wrap the
  177. * answer a second time.
  178. */
  179. public class AnswerBlockAction : TypedStatumAction<BlockAnswerPrivate> {
  180. private TaskService tasks = inject<TaskService>();
  181. private UserService users = inject<UserService>();
  182. protected override Type private_type { get { return typeof(BlockAnswerPrivate); } }
  183. public override async DirectiveBuilder handle() throws GLib.Error {
  184. var guard = yield SpryAuth.require_login(directives(), request, users);
  185. if (guard != null) {
  186. return (!)guard;
  187. }
  188. var ctx = request_private;
  189. if (ctx == null) {
  190. return directives().navigate("/tasks");
  191. }
  192. int64 task_id = ((!)ctx).task_id;
  193. string option = Forms.field(request, "option");
  194. string other = Forms.field_raw(request, "other");
  195. string answer = Forms.field_raw(request, "answer");
  196. if (option == "__other__") {
  197. answer = other.strip().length > 0 ? other.strip() : answer;
  198. } else if (option.length > 0) {
  199. answer = option;
  200. }
  201. if (answer.length == 0) {
  202. return directives().notify("error", "An answer is required").navigate(@"/tasks/$(task_id)");
  203. }
  204. try {
  205. yield tasks.answer_block(((!)ctx).block_id, answer);
  206. return directives().notify("success", "Answer recorded; the saddle will collect it")
  207. .navigate(@"/tasks/$(task_id)");
  208. } catch (Error e) {
  209. return directives().notify("error", @"Could not answer the block: $(e.message)")
  210. .navigate(@"/tasks/$(task_id)");
  211. }
  212. }
  213. }
  214. /** Creates a child task under the sealed parent task id. */
  215. public class CreateSubtaskAction : SealedTaskAction {
  216. private TaskService tasks = inject<TaskService>();
  217. private UserService users = inject<UserService>();
  218. public override async DirectiveBuilder handle() throws GLib.Error {
  219. var guard = yield SpryAuth.require_login(directives(), request, users);
  220. if (guard != null) {
  221. return (!)guard;
  222. }
  223. int64 task_id = sealed_task_id();
  224. string title = Forms.field(request, "title");
  225. if (task_id <= 0 || title.length == 0) {
  226. return directives().notify("error", "A subtask title is required").navigate(@"/tasks/$(task_id)");
  227. }
  228. try {
  229. yield tasks.create_subtask(task_id, title, Forms.field_raw(request, "description"));
  230. return directives().notify("success", "Subtask created").navigate(@"/tasks/$(task_id)");
  231. } catch (Error e) {
  232. return directives().notify("error", @"Could not create the subtask: $(e.message)")
  233. .navigate(@"/tasks/$(task_id)");
  234. }
  235. }
  236. }
  237. /** Adds a dependency edge from the sealed task to the form's target. */
  238. public class AddDependencyAction : SealedTaskAction {
  239. private TaskService tasks = inject<TaskService>();
  240. private UserService users = inject<UserService>();
  241. public override async DirectiveBuilder handle() throws GLib.Error {
  242. var guard = yield SpryAuth.require_login(directives(), request, users);
  243. if (guard != null) {
  244. return (!)guard;
  245. }
  246. int64 task_id = sealed_task_id();
  247. int64 depends_on = Forms.integer(request, "depends_on_task_id", 0);
  248. if (task_id <= 0 || depends_on <= 0) {
  249. return directives().notify("error", "Pick a task to depend on").navigate(@"/tasks/$(task_id)");
  250. }
  251. try {
  252. yield tasks.add_dependency(task_id, depends_on);
  253. return directives().notify("success", "Dependency added").navigate(@"/tasks/$(task_id)");
  254. } catch (Error e) {
  255. return directives().notify("error", @"Could not add the dependency: $(e.message)")
  256. .navigate(@"/tasks/$(task_id)");
  257. }
  258. }
  259. }
  260. /**
  261. * Completes a task (summary and working branch from the form); closes
  262. * the open session and frees the saddle running it.
  263. */
  264. public class CompleteTaskAction : SealedTaskAction {
  265. private TaskService tasks = inject<TaskService>();
  266. private UserService users = inject<UserService>();
  267. public override async DirectiveBuilder handle() throws GLib.Error {
  268. var guard = yield SpryAuth.require_login(directives(), request, users);
  269. if (guard != null) {
  270. return (!)guard;
  271. }
  272. int64 task_id = sealed_task_id();
  273. if (task_id <= 0) {
  274. return directives().navigate("/tasks");
  275. }
  276. try {
  277. yield tasks.complete_task(task_id, Forms.optional(request, "summary"),
  278. Forms.optional(request, "working_branch"), "done");
  279. return directives().notify("success", "Task completed").navigate(@"/tasks/$(task_id)");
  280. } catch (Error e) {
  281. return directives().notify("error", @"Could not complete the task: $(e.message)")
  282. .navigate(@"/tasks/$(task_id)");
  283. }
  284. }
  285. }
  286. /**
  287. * Cancels a task (status 'failed'); the sealed id comes from the task
  288. * page's cancel button.
  289. */
  290. public class CancelTaskAction : SealedTaskAction {
  291. private TaskService tasks = inject<TaskService>();
  292. private UserService users = inject<UserService>();
  293. public override async DirectiveBuilder handle() throws GLib.Error {
  294. var guard = yield SpryAuth.require_login(directives(), request, users);
  295. if (guard != null) {
  296. return (!)guard;
  297. }
  298. int64 task_id = sealed_task_id();
  299. if (task_id <= 0) {
  300. return directives().navigate("/tasks");
  301. }
  302. try {
  303. yield tasks.cancel_task(task_id);
  304. return directives().notify("success", "Task cancelled").navigate(@"/tasks/$(task_id)");
  305. } catch (Error e) {
  306. return directives().notify("error", @"Could not cancel the task: $(e.message)")
  307. .navigate(@"/tasks/$(task_id)");
  308. }
  309. }
  310. }
  311. /**
  312. * The task detail page's polling action: rebuilds the `task` slot from
  313. * current data so the three-second client poll shows live comments,
  314. * blocks, output-driven status changes and completions. Navigates
  315. * nowhere; a failed reload leaves the client's state untouched.
  316. */
  317. public class RefreshTaskDataAction : SealedTaskAction {
  318. private TaskService task_service = inject<TaskService>();
  319. private ProjectService project_service = inject<ProjectService>();
  320. private SaddleService saddle_service = inject<SaddleService>();
  321. private RuleService rule_service = inject<RuleService>();
  322. private UserService users = inject<UserService>();
  323. public override async DirectiveBuilder handle() throws GLib.Error {
  324. var guard = yield SpryAuth.require_login(directives(), request, users);
  325. if (guard != null) {
  326. return (!)guard;
  327. }
  328. int64 task_id = sealed_task_id();
  329. if (task_id <= 0) {
  330. return directives();
  331. }
  332. var state = yield TaskDetailState.build(state_service, task_service,
  333. project_service, saddle_service, rule_service, action_registry, task_id);
  334. return directives().set(state);
  335. }
  336. }
  337. }