|
6 | 6 |
|
7 | 7 | (set! *warn-on-reflection* true) |
8 | 8 |
|
9 | | -;; --------------------------------------------------------------------------- |
10 | | -;; Action handlers |
11 | | -;; --------------------------------------------------------------------------- |
| 9 | +(defn ^:private job-prefix |
| 10 | + "Render `\"<name>\" (job-id)` when the job has a `:summary`, otherwise just the id." |
| 11 | + [{:keys [id summary]}] |
| 12 | + (if-let [s (not-empty summary)] |
| 13 | + (str "\"" s "\" (" id ")") |
| 14 | + id)) |
12 | 15 |
|
13 | 16 | (defn ^:private action-list [_arguments] |
14 | 17 | (let [jobs (remove :notified (bg/list-jobs))] |
15 | 18 | (if (seq jobs) |
16 | 19 | (tools.util/single-text-content |
17 | 20 | (string/join |
18 | 21 | "\n" |
19 | | - (map (fn [{:keys [id type label summary status exit-code] :as job}] |
20 | | - (str "- " id |
21 | | - (when summary (str " (" summary ")")) |
| 22 | + (map (fn [{:keys [type label status exit-code] :as job}] |
| 23 | + (str "- " (job-prefix job) |
22 | 24 | " [" (name type) "] " |
23 | 25 | (name status) |
24 | 26 | (when exit-code (str " (exit " exit-code ")")) |
|
30 | 32 | (defn ^:private action-read-output [{:keys [job-id]}] |
31 | 33 | (if-let [result (bg/read-output! job-id)] |
32 | 34 | (let [{:keys [lines dropped status exit-code]} result |
| 35 | + prefix (if-let [job (bg/get-job job-id)] |
| 36 | + (job-prefix job) |
| 37 | + job-id) |
33 | 38 | output (if (seq lines) |
34 | 39 | (string/join "\n" (map bg/format-output-line lines)) |
35 | 40 | "(no new output)")] |
36 | 41 | (tools.util/single-text-content |
37 | | - (str "Job " job-id " — " (name status) |
| 42 | + (str "Job " prefix " — " (name status) |
38 | 43 | (when exit-code (str " (exit " exit-code ")")) |
39 | 44 | (when (pos? dropped) (str "\n[" dropped " lines dropped from buffer]")) |
40 | 45 | "\n\n" output))) |
41 | 46 | (tools.util/single-text-content (str "Background job " job-id " not found.") true))) |
42 | 47 |
|
43 | 48 | (defn ^:private action-kill [{:keys [job-id]}] |
44 | | - (if (bg/get-job job-id) |
| 49 | + (if-let [job (bg/get-job job-id)] |
45 | 50 | (if (bg/kill-job! job-id) |
46 | | - (tools.util/single-text-content (str "Background job " job-id " killed.")) |
| 51 | + (tools.util/single-text-content (str "Background job " (job-prefix job) " killed.")) |
47 | 52 | (let [current-status (:status (bg/get-job job-id))] |
48 | 53 | (tools.util/single-text-content |
49 | | - (str "Background job " job-id " is not running" |
| 54 | + (str "Background job " (job-prefix job) " is not running" |
50 | 55 | (when current-status (str " (status: " (name current-status) ")")) ".") |
51 | 56 | true))) |
52 | 57 | (tools.util/single-text-content (str "Background job " job-id " not found.") true))) |
53 | 58 |
|
54 | | -;; --------------------------------------------------------------------------- |
55 | | -;; Tool handler |
56 | | -;; --------------------------------------------------------------------------- |
57 | | - |
58 | 59 | (defn ^:private bg-job [arguments _ctx] |
59 | 60 | (let [action (get arguments "action") |
60 | 61 | job-id (get arguments "job_id")] |
|
68 | 69 | (tools.util/single-text-content "job_id is required for kill action." true)) |
69 | 70 | (tools.util/single-text-content (str "Unknown action: " action ". Use list, read_output, or kill.") true)))) |
70 | 71 |
|
71 | | -;; --------------------------------------------------------------------------- |
72 | | -;; Tool definition |
73 | | -;; --------------------------------------------------------------------------- |
74 | | - |
75 | 72 | (def definitions |
76 | 73 | {"bg_job" |
77 | 74 | {:description (tools.util/read-tool-description "bg_job") |
|
84 | 81 | :required ["action"]} |
85 | 82 | :summary-fn (fn [{:keys [args]}] |
86 | 83 | (let [action (get args "action") |
87 | | - job-id (get args "job_id")] |
| 84 | + job-id (get args "job_id") |
| 85 | + label (when job-id |
| 86 | + (if-let [job (bg/get-job job-id)] |
| 87 | + (job-prefix job) |
| 88 | + job-id))] |
88 | 89 | (case action |
89 | 90 | "list" "Listing background jobs" |
90 | | - "read_output" (str "Reading output of " job-id) |
91 | | - "kill" (str "Killing " job-id) |
| 91 | + "read_output" (str "Reading output of " label) |
| 92 | + "kill" (str "Killing " label) |
92 | 93 | "Managing background job"))) |
93 | 94 | :handler #'bg-job}}) |
0 commit comments