| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "regexp" |
| 10 | "slices" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/go-chi/chi/v5" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 18 | "amadan.net/oficina/equipo/internal/equipo/identity" |
| 19 | "amadan.net/oficina/equipo/internal/wire" |
| 20 | ) |
| 21 | |
| 22 | // taskLine is a task as the task card, the Tasks page and the task page |
| 23 | // show it: the person API's Task with its words. |
| 24 | type taskLine struct { |
| 25 | wire.Task |
| 26 | // MergeDropped: an exploration whose merge history, from before it |
| 27 | // became one, the pages leave out (pageTask) and say so once. |
| 28 | MergeDropped bool |
| 29 | StageLine, Pushed, Short string |
| 30 | // ChecksLine and ReviewLine describe the judgements of the newest |
| 31 | // candidate (or that they are running); MergedLine says where and at |
| 32 | // which commit Equipo merged it, and RepoURL opens the repository. |
| 33 | ChecksLine, ReviewLine string |
| 34 | MergedLine, RepoURL string |
| 35 | // PreviewURL and PreviewLine: the task's live preview, if any. |
| 36 | PreviewURL, PreviewLine string |
| 37 | Step *wire.Step |
| 38 | StepLine string |
| 39 | Steps []stepLine |
| 40 | Finding *wire.Finding |
| 41 | Retry, Cancel bool |
| 42 | // Recover settles a step stopped when its machine was removed. |
| 43 | Recover bool |
| 44 | // Merge is the creator's quiet Merge on a task left at Ready to merge. |
| 45 | Merge bool |
| 46 | // Hold is why the scheduler holds the task's step, with what the |
| 47 | // creator can do about it; Priority the task's priority and deadline, |
| 48 | // with the creator's own change (connectors spec §6, §8). |
| 49 | Hold *holdLine |
| 50 | Priority *priorityLine |
| 51 | // Pause is why the work waits or stopped, and the one action |
| 52 | // (pause_view.go); nil while it moves. |
| 53 | Pause *pauseLine |
| 54 | // StageTag is the connection StageLine names (a hold's), for its |
| 55 | // mark. |
| 56 | StageTag *chat.ConnLabel |
| 57 | // The inner screens' pieces (work_views.go). |
| 58 | Mono monoLine |
| 59 | Creator personLine |
| 60 | Mine, Live bool |
| 61 | Group, Tone string |
| 62 | Track []trackStop |
| 63 | Changed, SourceAt time.Time |
| 64 | Ago string |
| 65 | Source, SourceHref string |
| 66 | Chip *chipLine |
| 67 | Badges []badgeLine |
| 68 | Dup *wire.TaskRef |
| 69 | Menu []menuItem |
| 70 | Card *wire.Card |
| 71 | Decision *decisionView |
| 72 | Running, Stopped *wire.Step |
| 73 | // WaitsOther: a teammate's task waiting on their decision. |
| 74 | WaitsOther bool |
| 75 | // The Tasks row's own words (rowOf): Say is the status, Note the |
| 76 | // detail line whole (its tooltip), StepLabel the stepper in words, |
| 77 | // SourceShort the From column's origin and Act a Needs you row's one |
| 78 | // action. |
| 79 | Say, Note, StepLabel, SourceShort string |
| 80 | Act *rowAction |
| 81 | // Summary and Timeline are the task page's; Spend what the task used |
| 82 | // and StepSpend each step's part, for the specialist panel. |
| 83 | Summary string |
| 84 | Timeline []timelineItem |
| 85 | Spend string |
| 86 | SpendNote string |
| 87 | StepSpend []spendLine |
| 88 | } |
| 89 | |
| 90 | // stepLine is one step on the task page. |
| 91 | type stepLine struct { |
| 92 | wire.Step |
| 93 | Line, Result string |
| 94 | } |
| 95 | |
| 96 | func specialistName(s string) string { |
| 97 | if n := copyText("work.specialist." + s); n != "" { |
| 98 | return n |
| 99 | } |
| 100 | return s |
| 101 | } |
| 102 | |
| 103 | // stepName names the specialist that makes step s: its maker (a |
| 104 | // data-defined specialist such as Design, which works on Engineering's |
| 105 | // step kind) by its profile's own name, else its kind's specialist. |
| 106 | func stepName(s wire.Step) string { |
| 107 | if s.Maker != nil { |
| 108 | if n := makerName(s.Maker.Profile); n != "" { |
| 109 | return n |
| 110 | } |
| 111 | } |
| 112 | return specialistName(s.Specialist) |
| 113 | } |
| 114 | |
| 115 | // stepKey is the sidebar key of the specialist that makes step s: its |
| 116 | // maker's id, else its kind. |
| 117 | func stepKey(s wire.Step) string { |
| 118 | if s.Maker != nil { |
| 119 | return s.Maker.ID |
| 120 | } |
| 121 | return s.Specialist |
| 122 | } |
| 123 | |
| 124 | // makerName is a data-defined specialist profile's own name (Design). |
| 125 | func makerName(profile string) string { return copyText(makerCopyKey(profile)) } |
| 126 | |
| 127 | func makerCopyKey(profile string) string { return "chat.specialist.profile." + profile } |
| 128 | |
| 129 | // makerIcon is a data-defined specialist profile's icon. |
| 130 | func makerIcon(profile string) string { |
| 131 | switch profile { |
| 132 | case "research-v1": |
| 133 | return "search" |
| 134 | case "design-v1": |
| 135 | return "image" |
| 136 | } |
| 137 | return "star" |
| 138 | } |
| 139 | |
| 140 | // taskStepName names who works on the task's step s: stepName, or the |
| 141 | // task's maker for an Engineering-kind step it has not bound yet. |
| 142 | func taskStepName(t wire.Task, s *wire.Step) string { |
| 143 | if s == nil { |
| 144 | if t.Maker != nil { |
| 145 | if n := makerName(t.Maker.Profile); n != "" { |
| 146 | return n |
| 147 | } |
| 148 | } |
| 149 | return specialistName(wire.SpecialistEngineering) |
| 150 | } |
| 151 | return stepName(*s) |
| 152 | } |
| 153 | |
| 154 | // checksLine is a checks result in words, honest about what did not run |
| 155 | // and about checks that ran outside the sandbox (sandbox none, a Mac |
| 156 | // worker whose probe P2 did not deny: Paul's ruling 2). |
| 157 | func checksLine(outcome, reason, gate string, code *int, sandbox string) string { |
| 158 | switch outcome { |
| 159 | case wire.ChecksPassed: |
| 160 | return outsideSandbox(fmt.Sprintf(copyText("work.checks.passed"), gate), sandbox) |
| 161 | case wire.ChecksFailed: |
| 162 | n := -1 |
| 163 | if code != nil { |
| 164 | n = *code |
| 165 | } |
| 166 | return outsideSandbox(fmt.Sprintf(copyText("work.checks.failed"), gate, n), sandbox) |
| 167 | case wire.ChecksNone: |
| 168 | return copyText("work.checks.none") |
| 169 | case wire.ChecksCannotRun: |
| 170 | if s := copyText("work.checks." + reason); s != "" { |
| 171 | return s |
| 172 | } |
| 173 | return copyText("work.checks.checks_unavailable") |
| 174 | } |
| 175 | return "" |
| 176 | } |
| 177 | |
| 178 | // outsideSandbox labels a checks result that ran outside the sandbox. |
| 179 | func outsideSandbox(line, sandbox string) string { |
| 180 | if sandbox != wire.SandboxNone { |
| 181 | return line |
| 182 | } |
| 183 | return fmt.Sprintf(copyText("work.checks.outside_sandbox"), line) |
| 184 | } |
| 185 | |
| 186 | func reviewLine(verdict, label string) string { |
| 187 | if verdict == "" { |
| 188 | return "" |
| 189 | } |
| 190 | return fmt.Sprintf(copyText("work.review."+verdict), copyText("work.review."+label)) |
| 191 | } |
| 192 | |
| 193 | // taskLineOf is t as viewer sees it: every member reads a team task; |
| 194 | // Retry and Cancel are offered only to the person who asked for it. |
| 195 | func taskLineOf(t wire.Task, viewer string) taskLine { |
| 196 | v := taskLine{Task: t, StageLine: copyText("work.stage." + t.Stage)} |
| 197 | if v.StageLine == "" { |
| 198 | v.StageLine = t.Stage |
| 199 | } |
| 200 | if n := len(t.Findings); n > 0 { |
| 201 | v.Finding = &t.Findings[n-1] |
| 202 | } |
| 203 | if len(t.CandidateSHA) >= 7 { |
| 204 | v.Short = t.CandidateSHA[:7] |
| 205 | v.Pushed = fmt.Sprintf(copyText("work.pushed"), t.Branch, v.Short) |
| 206 | } |
| 207 | for _, s := range t.Steps { |
| 208 | l := stepLine{Step: s, Line: fmt.Sprintf(copyText("work.step"), stepName(s), s.Attempt, copyText("work.step."+s.State))} |
| 209 | if s.Round > 0 && s.Specialist == wire.SpecialistEngineering && !s.Rebase { |
| 210 | l.Line += " (" + fmt.Sprintf(copyText("work.round"), s.Round) + ")" |
| 211 | } |
| 212 | if s.Rebase { |
| 213 | l.Line += " (" + copyText("work.rebase") + ")" |
| 214 | } |
| 215 | if s.Switched != nil { |
| 216 | // "Engineering on Codex (Claude was limited)": what ran it, and why. |
| 217 | l.Line += " (" + fmt.Sprintf(copyText("work.switched"), providerName(s.Switched.From)) + ")" |
| 218 | } |
| 219 | if s.Model != "" { |
| 220 | // The model a metered step ran on (§16.7). |
| 221 | l.Line += " · " + fmt.Sprintf(copyText("work.step.model"), s.Model) |
| 222 | } |
| 223 | if s.MadeOn != "" { |
| 224 | // Engineering on a worker, named (only then: house rule |
| 225 | // 2026-11-07.8); without Xcode access the Mac's checks build it. |
| 226 | l.Line += " · " + fmt.Sprintf(copyText("work.step.made_on"), s.MadeOn) |
| 227 | if s.XcodeAccess != wire.XcodeAccessSandboxed { |
| 228 | l.Result = copyText("work.step.no_xcode") |
| 229 | } |
| 230 | if s.Specialist == wire.SpecialistEngineering && s.Placement == wire.PlacementWaiting && s.CandidateSHA != "" { |
| 231 | // Its commit waits for a primary to publish it. |
| 232 | l.Line += " · " + copyText("work.step.waiting_publish") |
| 233 | } |
| 234 | } |
| 235 | if s.Specialist == wire.SpecialistEngineering && s.State == wire.StepQueued && s.Placement == wire.PlacementBase && s.RunnerLabel != "" { |
| 236 | // The main runner resolves the Mac's starting point first. |
| 237 | l.Line += " · " + fmt.Sprintf(copyText("work.step.waiting_base"), s.RunnerLabel) |
| 238 | } |
| 239 | if s.State == wire.StepDone { |
| 240 | switch s.Specialist { |
| 241 | case wire.SpecialistChecks: |
| 242 | l.Result = checksLine(s.Checks, s.Reason, s.Gate, s.ExitCode, s.ChecksSandbox) |
| 243 | case wire.SpecialistReview: |
| 244 | l.Result = reviewLine(s.Verdict, s.ReviewLabel) |
| 245 | case wire.SpecialistMerge: |
| 246 | l.Result = mergedLine(s.Into, s.MergeSHA, s.WithoutChecks) |
| 247 | } |
| 248 | } |
| 249 | v.Steps = append(v.Steps, l) |
| 250 | } |
| 251 | v.ChecksLine = checksLine(t.Checks, t.ChecksReason, "", nil, t.ChecksSandbox) |
| 252 | for _, s := range t.Steps { |
| 253 | if s.CandidateSHA == t.CandidateSHA && s.Specialist == wire.SpecialistChecks && s.State == wire.StepDone { |
| 254 | v.ChecksLine = checksLine(s.Checks, s.Reason, s.Gate, s.ExitCode, s.ChecksSandbox) |
| 255 | } |
| 256 | } |
| 257 | v.ReviewLine = reviewLine(t.Verdict, t.ReviewLabel) |
| 258 | n := len(t.Steps) |
| 259 | for n > 0 && (t.Steps[n-1].Specialist == wire.SpecialistPreview || t.Steps[n-1].Specialist == wire.SpecialistResearch || t.Steps[n-1].Specialist == wire.SpecialistPublish) { |
| 260 | // Previews and Research never drive the task's line. |
| 261 | n-- |
| 262 | } |
| 263 | if n > 0 { |
| 264 | s := t.Steps[n-1] |
| 265 | v.Step = &s |
| 266 | v.StepLine = v.Steps[n-1].Line |
| 267 | if s.State == wire.StepRunning || s.State == wire.StepQueued { |
| 268 | switch s.Specialist { |
| 269 | case wire.SpecialistChecks: |
| 270 | v.ChecksLine = copyText("work.checks.running") |
| 271 | if s.Placement == wire.PlacementSandbox { |
| 272 | v.ChecksLine = copyText("work.checks.waiting_sandbox") |
| 273 | } |
| 274 | if s.Placement == wire.PlacementMac { |
| 275 | v.ChecksLine = copyText("work.chip.waiting_mac") |
| 276 | } |
| 277 | case wire.SpecialistReview: |
| 278 | v.ReviewLine = copyText("work.review.running") |
| 279 | } |
| 280 | } |
| 281 | if s.State == wire.StepFailed && !wire.FinalStage(t.Stage) { |
| 282 | reason := copyText("work.reason." + stopReason(t, s)) |
| 283 | if reason == "" { |
| 284 | reason = copyText("work.reason.other") |
| 285 | } |
| 286 | v.StageLine = fmt.Sprintf(copyText("work.stopped"), reason) |
| 287 | v.Retry = s.Terminated |
| 288 | if s.Reason == "machine_removed" && !s.Terminated { |
| 289 | v.StageLine, v.Recover = copyText("work.needs.machine_removed"), true |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | if t.Merged != nil { |
| 294 | v.MergedLine = mergedLine(t.Merged.Into, t.Merged.SHA, t.Merged.WithoutChecks) |
| 295 | v.RepoURL = "https://" + t.Repo |
| 296 | } |
| 297 | v.PreviewURL, v.PreviewLine = previewCard(t, time.Now()) |
| 298 | switch t.NeedsYou { |
| 299 | case wire.NeedsFixRounds, wire.NeedsReviewBlocked, wire.NeedsMergeConflict, wire.NeedsDirection: |
| 300 | v.StageLine = copyText("work.needs." + t.NeedsYou) |
| 301 | case wire.NeedsMacSandbox: |
| 302 | v.StageLine = copyText("work.checks.waiting_sandbox") |
| 303 | case wire.NeedsMacRunner, wire.NeedsMacEngineering: |
| 304 | v.StageLine = copyText("work.needs." + t.NeedsYou) |
| 305 | case wire.NeedsMacConnection, wire.NeedsMacUpdate: |
| 306 | // The Mac (or the primary) it waits for, by name. |
| 307 | if s := lastStep(t); s != nil { |
| 308 | v.StageLine = fmt.Sprintf(copyText("work.needs."+t.NeedsYou), s.RunnerLabel) |
| 309 | } |
| 310 | case wire.NeedsBranchMoved: |
| 311 | // What was checked and reviewed is not what the branch holds now: |
| 312 | // none of it is shown beside the branch's link. |
| 313 | v.StageLine, v.ChecksLine, v.ReviewLine = copyText("work.needs.branch_moved"), "", "" |
| 314 | } |
| 315 | v.Cancel = !wire.FinalStage(t.Stage) && t.Stage != wire.StageMerging |
| 316 | v.Merge = t.MergeOffered |
| 317 | v.Priority = priorityOf(t, t.Creator == viewer) |
| 318 | if v.Step != nil && v.Step.Hold != nil && !wire.FinalStage(t.Stage) { |
| 319 | v.Hold = holdOf(*v.Step, t.Creator == viewer, chat.HoldLabel(nil, nil, "", "", v.Step.Hold.Provider), defaultZone()) |
| 320 | v.StageLine = v.Hold.Line |
| 321 | } |
| 322 | if t.Creator != viewer { |
| 323 | v.Retry, v.Cancel, v.Merge, v.Recover = false, false, false, false |
| 324 | } |
| 325 | if t.Moving || t.Stopping { |
| 326 | v.StageLine = placementPendingLine(t) |
| 327 | v.Retry, v.Cancel, v.Merge, v.Recover = false, false, false, false |
| 328 | v.Hold = nil |
| 329 | } |
| 330 | return v |
| 331 | } |
| 332 | |
| 333 | // holdLine is a held step's line (§8) and its actions: Run it now anyway, |
| 334 | // or for a spending bar Raise the budget or Resume (both on the |
| 335 | // Connections page). |
| 336 | type holdLine struct { |
| 337 | Line string |
| 338 | // Tag is the connection the hold names. |
| 339 | Tag *chat.ConnLabel |
| 340 | Step string |
| 341 | Revision int64 |
| 342 | RunNow, Budget, Resume bool |
| 343 | } |
| 344 | |
| 345 | // holdOf is the hold's words, naming its connection by tag, and, for |
| 346 | // the creator, its action. |
| 347 | func holdOf(s wire.Step, mine bool, tag chat.ConnLabel, loc *time.Location) *holdLine { |
| 348 | h := s.Hold |
| 349 | name := tag.Text |
| 350 | l := &holdLine{Step: s.ID, Revision: h.StepRevision} |
| 351 | if s.Specialist == wire.SpecialistCopy && (s.Reason == "copy_budget" || s.Reason == "copy_budget_unknown") { |
| 352 | l.Line = copyText("work.reason." + s.Reason) |
| 353 | return l |
| 354 | } |
| 355 | if h.Provider != "" { |
| 356 | l.Tag = &tag |
| 357 | } |
| 358 | switch h.Reason { |
| 359 | case wire.HoldHeadroom: |
| 360 | l.Line = fmt.Sprintf(copyText("work.hold.headroom"), name) |
| 361 | if h.ResetsAt != nil { |
| 362 | l.Line = fmt.Sprintf(copyText("work.hold.headroom_reset"), name, resetText(*h.ResetsAt, loc)) |
| 363 | } |
| 364 | case wire.HoldBudget: |
| 365 | l.Line, l.Budget = fmt.Sprintf(copyText("work.hold.budget"), name), mine |
| 366 | case wire.HoldSpendUnknown: |
| 367 | l.Line, l.Resume = fmt.Sprintf(copyText("work.hold.spend_unknown"), name), mine |
| 368 | case wire.HoldBusy: |
| 369 | l.Line = fmt.Sprintf(copyText("work.hold.busy"), name) |
| 370 | default: |
| 371 | l.Line = copyText("work.hold.no_connection") |
| 372 | } |
| 373 | l.RunNow = mine && h.Reason != wire.HoldBudget && h.Reason != wire.HoldSpendUnknown |
| 374 | return l |
| 375 | } |
| 376 | |
| 377 | // priorityLine is a task's priority and deadline in words, and the |
| 378 | // creator's change: Mark urgent, or back to normal. |
| 379 | type priorityLine struct { |
| 380 | Line string |
| 381 | Revision int64 |
| 382 | MarkUrgent, MakeNormal bool |
| 383 | } |
| 384 | |
| 385 | // priorityOf is nil for a normal task with no deadline that nobody set, |
| 386 | // unless its creator may mark it urgent. |
| 387 | func priorityOf(t wire.Task, mine bool) *priorityLine { |
| 388 | if wire.FinalStage(t.Stage) { |
| 389 | return nil |
| 390 | } |
| 391 | p, by, from := wire.PriorityNormal, (*time.Time)(nil), false |
| 392 | if t.Priority != nil { |
| 393 | p, by, from = t.Priority.Priority, t.Priority.By, t.Priority.SetBy == wire.PrioritySetByCoordinator |
| 394 | } |
| 395 | l := &priorityLine{Revision: t.Revision, MarkUrgent: mine && p != wire.PriorityUrgent, MakeNormal: mine && p == wire.PriorityUrgent} |
| 396 | if p != wire.PriorityNormal || by != nil { |
| 397 | parts := []string{copyText("work.priority." + p)} |
| 398 | if by != nil { |
| 399 | parts = append(parts, fmt.Sprintf(copyText("work.priority.by"), by.UTC().Format("Mon 2 Jan"))) |
| 400 | } |
| 401 | if from { |
| 402 | parts = append(parts, copyText("work.priority.from_message")) |
| 403 | } |
| 404 | l.Line = strings.Join(parts, " · ") |
| 405 | } |
| 406 | if l.Line == "" && !l.MarkUrgent { |
| 407 | return nil |
| 408 | } |
| 409 | return l |
| 410 | } |
| 411 | |
| 412 | // mergedLine is "Merged into main at abc1234", and says when it went in |
| 413 | // without checks. |
| 414 | func mergedLine(into, sha string, withoutChecks bool) string { |
| 415 | if into == "" || len(sha) < 7 { |
| 416 | return "" |
| 417 | } |
| 418 | key := "work.merged" |
| 419 | if withoutChecks { |
| 420 | key = "work.merged_without_checks" |
| 421 | } |
| 422 | return fmt.Sprintf(copyText(key), into, sha[:7]) |
| 423 | } |
| 424 | |
| 425 | // projectLine is a project as the Projects page shows it: its monogram, |
| 426 | // task count and spread, the last change and the facts Equipo knows. |
| 427 | type projectLine struct { |
| 428 | wire.Project |
| 429 | Tasks int |
| 430 | Mono monoLine |
| 431 | Spread []spreadPart |
| 432 | Words string |
| 433 | People []personLine |
| 434 | Last string |
| 435 | LastAt time.Time |
| 436 | Repos []repoLine |
| 437 | StackLine string |
| 438 | // Open is the tasks not done; NeedsYou those waiting on the viewer's |
| 439 | // own decision. Short is the row's description (DescLine the project |
| 440 | // page's, whole words). |
| 441 | Open, NeedsYou int |
| 442 | Short, DescLine string |
| 443 | // Star is the viewer's star; StarWhy an auto star's reason. |
| 444 | Star starLine |
| 445 | StarWhy string |
| 446 | } |
| 447 | |
| 448 | // threadView is a task page's thread: the creator's questions to the |
| 449 | // specialists with their answers, and notes, oldest first. Ask is set for |
| 450 | // the task's creator when a specialist can answer: the composer then |
| 451 | // asks (Asking Engineering · change); otherwise it adds a note. |
| 452 | type threadView struct { |
| 453 | Cursor int64 |
| 454 | Items []threadItem |
| 455 | Ask bool |
| 456 | // Choices are the specialists that can answer, the default first. |
| 457 | Choices []string |
| 458 | // Names are their names, for the ask line and the answers. |
| 459 | Names map[string]string |
| 460 | } |
| 461 | |
| 462 | // threadItem is a note, or a question with its answer, its waiting line |
| 463 | // or its ending. |
| 464 | type threadItem struct { |
| 465 | ID, Text, Author, Answer, Who, Ended, Waiting string |
| 466 | Note, Own, Answered bool |
| 467 | At time.Time |
| 468 | } |
| 469 | |
| 470 | // modelLookup says, for a step's connection (its runner's name for it) |
| 471 | // and the model it ran, the provider and whether the person still allows |
| 472 | // that model there. |
| 473 | type modelLookup func(account, model string) (provider string, allowed bool) |
| 474 | |
| 475 | // modelLookup reads the person's metered connections' allowed sets. |
| 476 | func (a *application) modelLookup(ctx context.Context, address string) modelLookup { |
| 477 | conns, err := a.store.Chat().Connections(ctx, address) |
| 478 | if err != nil { |
| 479 | return nil |
| 480 | } |
| 481 | return func(account, model string) (string, bool) { |
| 482 | for _, c := range conns { |
| 483 | if c.LocalID != account || c.Kind != wire.KindAPIKey { |
| 484 | continue |
| 485 | } |
| 486 | for _, m := range c.Models { |
| 487 | if m.ID == model { |
| 488 | return c.Provider, m.Allowed |
| 489 | } |
| 490 | } |
| 491 | return c.Provider, false |
| 492 | } |
| 493 | return "", true |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // modelUnavailable is a question's card when its session's model is |
| 498 | // unavailable: no longer allowed on its provider, or not answering. |
| 499 | func modelUnavailable(t wire.Task, q wire.Question, models []modelLookup) string { |
| 500 | for _, s := range t.Steps { |
| 501 | if s.ID != q.StepID || len(models) == 0 || models[0] == nil { |
| 502 | continue |
| 503 | } |
| 504 | if provider, allowed := models[0](s.Account, s.Model); !allowed && provider != "" { |
| 505 | return fmt.Sprintf(copyText("work.ask.model_removed"), providerName(provider)) |
| 506 | } |
| 507 | } |
| 508 | return copyText("work.ask.model_unhealthy") |
| 509 | } |
| 510 | |
| 511 | func threadOf(t wire.Task, viewer string, cursor int64, models ...modelLookup) threadView { |
| 512 | v := threadView{Cursor: cursor, Names: map[string]string{}} |
| 513 | for _, n := range t.Thread { |
| 514 | v.Items = append(v.Items, threadItem{ID: n.ID, Text: n.Text, Author: n.Author, Note: true, At: n.At}) |
| 515 | } |
| 516 | for _, q := range t.Questions { |
| 517 | who := specialistName(q.Specialist) |
| 518 | // A move retires a task's steps to EarlierSteps; their questions |
| 519 | // stay, still their makers'. |
| 520 | for _, st := range slices.Concat(t.EarlierSteps, t.Steps) { |
| 521 | if st.ID == q.StepID { |
| 522 | who = stepName(st) |
| 523 | } |
| 524 | } |
| 525 | it := threadItem{ID: q.ID, Text: q.Text, Author: q.Asker, Who: who, Own: q.Asker == viewer, At: q.CreatedAt} |
| 526 | switch q.State { |
| 527 | case wire.QuestionAnswered: |
| 528 | it.Answered, it.Answer = true, q.Answer |
| 529 | case wire.QuestionQueued: |
| 530 | it.Waiting = copyText("work.ask.queued") |
| 531 | if held := copyText("work.ask.held." + q.Reason); q.Reason != "" && held != "" { |
| 532 | // Held by its metered connection's guard, never moved. |
| 533 | it.Waiting = held |
| 534 | } |
| 535 | case wire.QuestionRunning: |
| 536 | it.Waiting = copyText("chat.activity.thinking") |
| 537 | case wire.QuestionCancelled: |
| 538 | it.Ended = copyText("work.ask.cancelled") |
| 539 | case wire.QuestionFailed: |
| 540 | if q.Reason == wire.ReasonModelUnavailable { |
| 541 | // Its session's model left the set, or is not answering: the |
| 542 | // question never moves (§16.3). |
| 543 | it.Ended = modelUnavailable(t, q, models) |
| 544 | break |
| 545 | } |
| 546 | fallthrough |
| 547 | default: |
| 548 | reason := copyText("work.ask.reason." + q.Reason) |
| 549 | if reason == "" { |
| 550 | reason = copyText("work.reason." + q.Reason) |
| 551 | } |
| 552 | if reason == "" { |
| 553 | reason = copyText("work.reason.other") |
| 554 | } |
| 555 | it.Ended = fmt.Sprintf(copyText("work.ask.failed"), who, reason) |
| 556 | } |
| 557 | v.Items = append(v.Items, it) |
| 558 | } |
| 559 | slices.SortStableFunc(v.Items, func(a, b threadItem) int { return a.At.Compare(b.At) }) |
| 560 | // Who answers as each kind (the page and its stream name them): the |
| 561 | // newest such step's maker, Design on a task Design makes. |
| 562 | for _, s := range t.Steps { |
| 563 | if s.Specialist == wire.SpecialistEngineering || s.Specialist == wire.SpecialistReview { |
| 564 | v.Names[s.Specialist] = stepName(s) |
| 565 | } |
| 566 | } |
| 567 | // Asking follows the API: until the task ends, and for 14 days after |
| 568 | // it is done (merged work). |
| 569 | if t.Creator != viewer || !t.AskOpen { |
| 570 | return v |
| 571 | } |
| 572 | for _, sp := range []string{wire.SpecialistEngineering, wire.SpecialistReview} { |
| 573 | for _, s := range t.Steps { |
| 574 | if s.Specialist == sp && s.Askable && s.State == wire.StepDone { |
| 575 | if !slices.Contains(v.Choices, sp) { |
| 576 | v.Choices = append(v.Choices, sp) |
| 577 | } |
| 578 | // Named by the newest such step's maker (Design). |
| 579 | v.Names[sp] = stepName(s) |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | v.Ask = len(v.Choices) > 0 |
| 584 | return v |
| 585 | } |
| 586 | |
| 587 | // workView is what the thin Tasks and Projects pages show. |
| 588 | type workView struct { |
| 589 | CanAdd []wire.ReachableProject |
| 590 | ProjectNames string |
| 591 | Tasks []taskLine |
| 592 | Task taskLine |
| 593 | Projects []projectLine |
| 594 | Project *projectLine |
| 595 | // Needs is the Needs you page: Decisions are the viewer's own, Others |
| 596 | // the team's tasks waiting on someone else. |
| 597 | Needs bool |
| 598 | Decisions []taskLine |
| 599 | Questions []chatCard |
| 600 | Others []taskLine |
| 601 | // Groups are the Tasks page's (or a project's) tasks by what matters. |
| 602 | Groups []taskGroup |
| 603 | // Filters: the project and person chosen (empty for all) and what can |
| 604 | // be chosen. |
| 605 | Filter workFilter |
| 606 | ProjectCount int |
| 607 | // Thread is the task page's thread. |
| 608 | Thread threadView |
| 609 | // Notes and Activity are a project page's, and MergePolicy its "How |
| 610 | // Equipo merges" section. |
| 611 | Notes []projectNote |
| 612 | Activity []projectEvent |
| 613 | MergePolicy *mergePolicyView |
| 614 | // List is the Projects page's list, in its groups. |
| 615 | List projectGroups |
| 616 | } |
| 617 | |
| 618 | // workFilter is the Tasks page's two quiet filters. |
| 619 | type workFilter struct { |
| 620 | Project, Person string |
| 621 | ProjectTitle, Person1 string |
| 622 | Projects []projectLine |
| 623 | People []personLine |
| 624 | } |
| 625 | |
| 626 | // projectNote is one of the coordinator's notes about a project. |
| 627 | type projectNote struct { |
| 628 | Text, From string |
| 629 | } |
| 630 | |
| 631 | // projectEvent is one line of a project's recent activity. |
| 632 | type projectEvent struct { |
| 633 | At time.Time |
| 634 | Ago string |
| 635 | Format string |
| 636 | Who, TaskID string |
| 637 | Title string |
| 638 | } |
| 639 | |
| 640 | // richTasks is every team task as viewer sees it, with the viewer's open |
| 641 | // merge cards attached to their tasks. |
| 642 | func (a *application) richTasks(ctx context.Context, viewer string, list []wire.Task) ([]taskLine, error) { |
| 643 | cards, err := a.store.Chat().OpenMergeCards(ctx, viewer) |
| 644 | if err != nil { |
| 645 | return nil, err |
| 646 | } |
| 647 | // The viewer's connections, for the age of the reading a hold for |
| 648 | // headroom was decided on (connectors spec rev 3.2 §6). Unread, the |
| 649 | // line says no age. |
| 650 | conns, labels, _ := a.store.Chat().LabelledConnections(ctx, viewer) |
| 651 | now, loc := time.Now(), a.store.Chat().Zone(viewer) |
| 652 | out := make([]taskLine, 0, len(list)) |
| 653 | for _, t := range list { |
| 654 | var card *wire.Card |
| 655 | if c, ok := cards[t.ID]; ok { |
| 656 | card = &c |
| 657 | } |
| 658 | t, dropped := pageTask(t) |
| 659 | v := taskLineOf(t, viewer) |
| 660 | v.MergeDropped = dropped |
| 661 | v.richOf(t, viewer, card, now) |
| 662 | if t.Creator == viewer { |
| 663 | holdNamed(&v, conns, labels, loc) |
| 664 | } else if v.Hold != nil && v.Step != nil && v.Step.Hold != nil { |
| 665 | // The viewer's own clock (review N9). |
| 666 | was := v.Hold.Line |
| 667 | v.Hold = holdOf(*v.Step, false, chat.HoldLabel(nil, nil, "", "", v.Step.Hold.Provider), loc) |
| 668 | if v.StageLine == was { |
| 669 | v.StageLine = v.Hold.Line |
| 670 | } |
| 671 | } |
| 672 | holdReading(&v, conns, now) |
| 673 | if v.Hold != nil && v.StageLine == v.Hold.Line { |
| 674 | v.StageTag = v.Hold.Tag |
| 675 | } |
| 676 | v.applyPause(viewer, now, loc) |
| 677 | pauseReading(&v, conns, now) |
| 678 | out = append(out, v) |
| 679 | } |
| 680 | return out, nil |
| 681 | } |
| 682 | |
| 683 | // holdNamed names the connection a hold waits on by its shared label: |
| 684 | // the creator's own connection, so only on the creator's pages. |
| 685 | func holdNamed(v *taskLine, conns []wire.Connection, labels map[string]chat.ConnLabel, loc *time.Location) { |
| 686 | if v.Hold == nil || v.Step == nil || v.Step.Hold == nil { |
| 687 | return |
| 688 | } |
| 689 | h := v.Step.Hold |
| 690 | was := v.Hold.Line |
| 691 | v.Hold = holdOf(*v.Step, true, chat.HoldLabel(conns, labels, v.Step.RunnerID, h.Connection, h.Provider), loc) |
| 692 | if v.StageLine == was { |
| 693 | v.StageLine = v.Hold.Line |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | // holdReading adds to a hold for headroom how old the reading it waits on |
| 698 | // is: "as of 2 h ago" for Codex, "as last reported 2 h ago" for Claude. |
| 699 | func holdReading(v *taskLine, conns []wire.Connection, now time.Time) { |
| 700 | if v.Hold == nil || v.Step == nil || v.Step.Hold == nil || v.Step.Hold.Reason != wire.HoldHeadroom { |
| 701 | return |
| 702 | } |
| 703 | h := v.Step.Hold |
| 704 | for _, c := range conns { |
| 705 | if c.LocalID != h.Connection || c.Provider != h.Provider { |
| 706 | continue |
| 707 | } |
| 708 | age := readingAge(c, now) |
| 709 | if age == "" { |
| 710 | return |
| 711 | } |
| 712 | line := fmt.Sprintf(copyText("work.hold.reading"), v.Hold.Line, age) |
| 713 | if v.StageLine == v.Hold.Line { |
| 714 | v.StageLine = line |
| 715 | } |
| 716 | v.Hold.Line = line |
| 717 | return |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | // workTasks is GET /tasks: every team task grouped by what matters |
| 722 | // (Needs you, In progress, Ready to merge, Done), optionally for one |
| 723 | // project (?project=) or person (?person=). ?needs=you is Needs you. |
| 724 | func (a *application) workTasks(w http.ResponseWriter, r *http.Request) { |
| 725 | w.Header().Set("Cache-Control", "no-store") |
| 726 | ctx, address, ok := sessionContext(r) |
| 727 | if !ok { |
| 728 | a.failure(w, r, ErrForbidden) |
| 729 | return |
| 730 | } |
| 731 | list, err := a.store.Chat().Tasks(ctx, address) |
| 732 | if err != nil { |
| 733 | a.failure(w, r, chatError(err)) |
| 734 | return |
| 735 | } |
| 736 | data, err := a.page(r, "tasks") |
| 737 | if err != nil { |
| 738 | a.failure(w, r, err) |
| 739 | return |
| 740 | } |
| 741 | all, err := a.richTasks(ctx, address, list.Items) |
| 742 | if err != nil { |
| 743 | a.failure(w, r, chatError(err)) |
| 744 | return |
| 745 | } |
| 746 | if r.URL.Query().Get("needs") == "you" { |
| 747 | // Needs you: the person's own decisions, then what waits on others. |
| 748 | data.Key, data.Title, data.Thin.Needs = "needs-you", "nav.needs-you", true |
| 749 | questions, err := a.store.Chat().OpenStandaloneCards(ctx, address) |
| 750 | if err != nil { |
| 751 | a.failure(w, r, chatError(err)) |
| 752 | return |
| 753 | } |
| 754 | for _, c := range questions { |
| 755 | v := cardView(c.Card, nil, nil) |
| 756 | v.Job = c.Job |
| 757 | data.Thin.Questions = append(data.Thin.Questions, v) |
| 758 | } |
| 759 | for _, t := range all { |
| 760 | switch { |
| 761 | case t.Group == "needs": |
| 762 | data.Thin.Decisions = append(data.Thin.Decisions, t) |
| 763 | case t.WaitsOther: |
| 764 | data.Thin.Others = append(data.Thin.Others, t) |
| 765 | } |
| 766 | } |
| 767 | sortGroup(data.Thin.Decisions) |
| 768 | sortGroup(data.Thin.Others) |
| 769 | if len(data.Thin.Others) > 0 { |
| 770 | data.Thin.Groups = []taskGroup{{Key: "others", Tasks: data.Thin.Others}} |
| 771 | } |
| 772 | render(w, "work_needs", data) |
| 773 | return |
| 774 | } |
| 775 | projects, err := a.store.Chat().Projects(ctx, address) |
| 776 | if err != nil { |
| 777 | a.failure(w, r, chatError(err)) |
| 778 | return |
| 779 | } |
| 780 | f := workFilter{Project: r.URL.Query().Get("project"), Person: r.URL.Query().Get("person")} |
| 781 | seen := map[string]bool{address: true} |
| 782 | f.People = append(f.People, personOf(address, address)) |
| 783 | for _, t := range all { |
| 784 | if !seen[t.Task.Creator] { |
| 785 | seen[t.Task.Creator] = true |
| 786 | f.People = append(f.People, t.Creator) |
| 787 | } |
| 788 | if (f.Project == "" || t.ProjectID == f.Project) && (f.Person == "" || t.Task.Creator == f.Person) { |
| 789 | data.Thin.Tasks = append(data.Thin.Tasks, t) |
| 790 | } |
| 791 | } |
| 792 | for _, p := range projects.Items { |
| 793 | line := projectLine{Project: p, Mono: monoOf(p.Title, p.ID)} |
| 794 | f.Projects = append(f.Projects, line) |
| 795 | if p.ID == f.Project { |
| 796 | f.ProjectTitle = p.Title |
| 797 | data.Thin.Project = &line |
| 798 | } |
| 799 | } |
| 800 | if f.Person != "" { |
| 801 | f.Person1 = personOf(f.Person, address).Name |
| 802 | } |
| 803 | data.Thin.Filter, data.Thin.ProjectCount = f, len(projects.Items) |
| 804 | data.Thin.Groups = groupTasks(data.Thin.Tasks) |
| 805 | foldDone(data.Thin.Groups, r.URL) |
| 806 | render(w, "work_tasks", data) |
| 807 | } |
| 808 | |
| 809 | // workTask is the thin task page for a chat task id ("tk..."); false |
| 810 | // for the older tasks' ids, which the caller serves as before. |
| 811 | func (a *application) workTask(w http.ResponseWriter, r *http.Request) bool { |
| 812 | id := chi.URLParam(r, "id") |
| 813 | if !strings.HasPrefix(id, "tk") { |
| 814 | return false |
| 815 | } |
| 816 | w.Header().Set("Cache-Control", "no-store") |
| 817 | ctx, address, ok := sessionContext(r) |
| 818 | if !ok { |
| 819 | a.failure(w, r, ErrForbidden) |
| 820 | return true |
| 821 | } |
| 822 | // The cursor first: the thread then follows every event after it. |
| 823 | cursor, err := a.store.Chat().Cursor(ctx) |
| 824 | if err != nil { |
| 825 | a.failure(w, r, chatError(err)) |
| 826 | return true |
| 827 | } |
| 828 | t, err := a.store.Chat().Task(ctx, address, id) |
| 829 | if err != nil { |
| 830 | a.failure(w, r, chatError(err)) |
| 831 | return true |
| 832 | } |
| 833 | data, err := a.page(r, "tasks") |
| 834 | if err != nil { |
| 835 | a.failure(w, r, err) |
| 836 | return true |
| 837 | } |
| 838 | lines, err := a.richTasks(ctx, address, []wire.Task{t}) |
| 839 | if err != nil { |
| 840 | a.failure(w, r, chatError(err)) |
| 841 | return true |
| 842 | } |
| 843 | v := lines[0] |
| 844 | t, _ = pageTask(t) |
| 845 | v.Summary, v.Timeline = summaryOf(t, v), timelineOf(t, v) |
| 846 | // What it used; an unreadable sum shows nothing, never zero. |
| 847 | if sp, err := a.store.Chat().TaskSpend(ctx, address, id); err == nil { |
| 848 | v.Spend, v.SpendNote, v.StepSpend = spendOf(t, sp) |
| 849 | } |
| 850 | data.Thin.Task, data.Title, data.RequestID = v, "nav.tasks", rand.Text() |
| 851 | data.Thin.Thread, data.ChatStream = threadOf(t, address, cursor, a.modelLookup(ctx, address)), true |
| 852 | data.ThinkingSet, data.ThinkingWords = a.thinkingFor(ctx, address) |
| 853 | render(w, "work_task", data) |
| 854 | return true |
| 855 | } |
| 856 | |
| 857 | // workProjects is GET /projects: the team's projects, each with what |
| 858 | // Equipo knows of it and how its tasks stand. Nothing to set up here. |
| 859 | func (a *application) workProjects(w http.ResponseWriter, r *http.Request) { |
| 860 | w.Header().Set("Cache-Control", "no-store") |
| 861 | ctx, address, ok := sessionContext(r) |
| 862 | if !ok { |
| 863 | a.failure(w, r, ErrForbidden) |
| 864 | return |
| 865 | } |
| 866 | data, err := a.page(r, "projects") |
| 867 | if err != nil { |
| 868 | a.failure(w, r, err) |
| 869 | return |
| 870 | } |
| 871 | if data.Thin, err = a.workProjectsView(ctx, address, r.URL.Query().Get("q")); err != nil { |
| 872 | a.failure(w, r, chatError(err)) |
| 873 | return |
| 874 | } |
| 875 | render(w, "work_projects", data) |
| 876 | } |
| 877 | |
| 878 | // workProjectsView is the Projects page: the auto-star rules applied for |
| 879 | // address first (their stars after it), then the projects in their groups, |
| 880 | // filtered by q once there are more than filterAbove. |
| 881 | func (a *application) workProjectsView(ctx context.Context, address, q string) (workView, error) { |
| 882 | var v workView |
| 883 | projects, err := a.store.Chat().Projects(ctx, address) |
| 884 | if err != nil { |
| 885 | return v, err |
| 886 | } |
| 887 | tasks, err := a.store.Chat().Tasks(ctx, address) |
| 888 | if err != nil { |
| 889 | return v, err |
| 890 | } |
| 891 | if v.Tasks, err = a.richTasks(ctx, address, tasks.Items); err != nil { |
| 892 | return v, err |
| 893 | } |
| 894 | now := time.Now() |
| 895 | stars, err := a.store.Chat().AutoStar(ctx, address, starSignals(v.Tasks, address, now)) |
| 896 | if err != nil { |
| 897 | return v, err |
| 898 | } |
| 899 | for _, p := range projects.Items { |
| 900 | line := projectLineOf(p, v.Tasks, address, now) |
| 901 | st, ok := stars[p.ID] |
| 902 | if ok && st.By == chat.StarByAuto { |
| 903 | line.StarWhy = starWhy(st) |
| 904 | } |
| 905 | line.Star = starLineOf(line, st, ok, now) |
| 906 | v.Projects = append(v.Projects, line) |
| 907 | } |
| 908 | for _, candidate := range projects.CanAdd { |
| 909 | if strings.Contains(strings.ToLower(candidate.Title+" "+candidate.Name+" "+candidate.Description), strings.ToLower(strings.TrimSpace(q))) { |
| 910 | v.CanAdd = append(v.CanAdd, candidate) |
| 911 | } |
| 912 | } |
| 913 | lines := v.Projects |
| 914 | filtering := true |
| 915 | if filtering { |
| 916 | lines = filterProjects(lines, q) |
| 917 | } |
| 918 | v.List = orderProjects(lines, now) |
| 919 | v.List.Filtering, v.List.Total = filtering, len(v.Projects) |
| 920 | if filtering { |
| 921 | v.List.Filter = strings.TrimSpace(q) |
| 922 | } |
| 923 | return v, nil |
| 924 | } |
| 925 | |
| 926 | // workStar is the person's own star or unstar on a project row. |
| 927 | func (a *application) workStar(w http.ResponseWriter, r *http.Request) { |
| 928 | id := chi.URLParam(r, "id") |
| 929 | back := "/projects" |
| 930 | if r.PostFormValue("from") == "project" && strings.HasPrefix(id, "p_") { |
| 931 | back = "/projects/" + id |
| 932 | } |
| 933 | a.chatActionTo(w, r, back, func(ctx context.Context, address string) error { |
| 934 | return a.store.Chat().SetStar(ctx, address, id, r.PostFormValue("starred") == "1") |
| 935 | }) |
| 936 | } |
| 937 | |
| 938 | // workProject is the thin project page for a chat project id. |
| 939 | func (a *application) workProject(w http.ResponseWriter, r *http.Request) bool { |
| 940 | id := chi.URLParam(r, "id") |
| 941 | if !strings.HasPrefix(id, "p_") { |
| 942 | return false |
| 943 | } |
| 944 | w.Header().Set("Cache-Control", "no-store") |
| 945 | ctx, address, ok := sessionContext(r) |
| 946 | if !ok { |
| 947 | a.failure(w, r, ErrForbidden) |
| 948 | return true |
| 949 | } |
| 950 | data, err := a.page(r, "projects") |
| 951 | if err != nil { |
| 952 | a.failure(w, r, err) |
| 953 | return true |
| 954 | } |
| 955 | all, err := a.workProjectsView(ctx, address, "") |
| 956 | if err != nil { |
| 957 | a.failure(w, r, chatError(err)) |
| 958 | return true |
| 959 | } |
| 960 | for i := range all.Projects { |
| 961 | if all.Projects[i].ID == id { |
| 962 | data.Thin.Project = &all.Projects[i] |
| 963 | data.Thin.ProjectNames = strings.Join(all.Projects[i].Names, ", ") |
| 964 | } |
| 965 | } |
| 966 | if data.Thin.Project == nil { |
| 967 | a.failure(w, r, ErrNotFound) |
| 968 | return true |
| 969 | } |
| 970 | for _, t := range all.Tasks { |
| 971 | if t.ProjectID == id { |
| 972 | data.Thin.Tasks = append(data.Thin.Tasks, t) |
| 973 | } |
| 974 | } |
| 975 | data.Thin.Groups = groupTasks(data.Thin.Tasks) |
| 976 | foldDone(data.Thin.Groups, r.URL) |
| 977 | data.Thin.Activity = projectActivity(data.Thin.Tasks, time.Now()) |
| 978 | notebook, err := a.store.Chat().Notebook(ctx, address) |
| 979 | if err != nil { |
| 980 | a.failure(w, r, chatError(err)) |
| 981 | return true |
| 982 | } |
| 983 | data.Thin.Notes = projectNotes(*data.Thin.Project, notebook.Entries) |
| 984 | policy, err := a.store.Chat().MergePolicy(ctx, address, id) |
| 985 | if err != nil { |
| 986 | a.failure(w, r, chatError(err)) |
| 987 | return true |
| 988 | } |
| 989 | data.Thin.MergePolicy = mergePolicyViewOf(policy) |
| 990 | data.Title = "nav.projects" |
| 991 | render(w, "work_project", data) |
| 992 | return true |
| 993 | } |
| 994 | |
| 995 | // workTaskMerge is the quiet Merge on a task left at Ready to merge. |
| 996 | func (a *application) workTaskMerge(w http.ResponseWriter, r *http.Request) { |
| 997 | id := chi.URLParam(r, "id") |
| 998 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 999 | _, err := a.store.Chat().MergeTask(ctx, address, id) |
| 1000 | return err |
| 1001 | }) |
| 1002 | } |
| 1003 | |
| 1004 | // workTaskAction is Retry or Cancel from the task card or the task page: |
| 1005 | // the same store calls a device makes, as the session's person. |
| 1006 | // workTaskRecover is Recover from the task card: the creator says the |
| 1007 | // removed machine is off. |
| 1008 | func (a *application) workTaskRecover(w http.ResponseWriter, r *http.Request) { |
| 1009 | id := chi.URLParam(r, "id") |
| 1010 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1011 | _, err := a.store.Chat().RecoverTask(ctx, address, id) |
| 1012 | return err |
| 1013 | }) |
| 1014 | } |
| 1015 | |
| 1016 | // workTaskUnsandboxed is the confirm of Run outside the sandbox: the |
| 1017 | // task's creator chooses to run the checks waiting for a Mac's sandbox |
| 1018 | // on that Mac outside it, for the attempt the page showed (step and |
| 1019 | // attempt); a session's own action, never a device's or a tool's. |
| 1020 | func (a *application) workTaskUnsandboxed(w http.ResponseWriter, r *http.Request) { |
| 1021 | id := chi.URLParam(r, "id") |
| 1022 | step := r.PostFormValue("step") |
| 1023 | attempt, perr := strconv.ParseInt(r.PostFormValue("attempt"), 10, 64) |
| 1024 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1025 | if perr != nil || step == "" || attempt < 1 { |
| 1026 | return chat.ErrInvalid |
| 1027 | } |
| 1028 | return a.store.Chat().RunUnsandboxed(ctx, address, id, step, attempt) |
| 1029 | }) |
| 1030 | } |
| 1031 | |
| 1032 | func (a *application) workTaskAction(retry bool) http.HandlerFunc { |
| 1033 | return func(w http.ResponseWriter, r *http.Request) { |
| 1034 | id := chi.URLParam(r, "id") |
| 1035 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1036 | var err error |
| 1037 | switch step := r.PostFormValue("step"); { |
| 1038 | case retry && step != "": |
| 1039 | // One step's Retry (a failed preview behind the merge). |
| 1040 | _, err = a.store.Chat().RetryStep(ctx, address, id, step) |
| 1041 | case retry: |
| 1042 | _, err = a.store.Chat().RetryTask(ctx, address, id) |
| 1043 | default: |
| 1044 | _, err = a.store.Chat().CancelTask(ctx, address, id) |
| 1045 | } |
| 1046 | return err |
| 1047 | }) |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // workTaskPriority is the creator's Mark urgent (or back to normal) on |
| 1052 | // the task, at the revision the page showed; the deadline stays. |
| 1053 | func (a *application) workTaskPriority(w http.ResponseWriter, r *http.Request) { |
| 1054 | id := chi.URLParam(r, "id") |
| 1055 | priority := r.PostFormValue("priority") |
| 1056 | revision, perr := strconv.ParseInt(r.PostFormValue("revision"), 10, 64) |
| 1057 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1058 | if perr != nil { |
| 1059 | return chat.ErrInvalid |
| 1060 | } |
| 1061 | k, err := a.store.Chat().Task(ctx, address, id) |
| 1062 | if err != nil { |
| 1063 | return err |
| 1064 | } |
| 1065 | var by *time.Time |
| 1066 | if k.Priority != nil { |
| 1067 | by = k.Priority.By |
| 1068 | } |
| 1069 | _, err = a.store.Chat().SetPriority(ctx, address, id, wire.SetPriority{Priority: priority, By: by, Revision: revision}) |
| 1070 | return err |
| 1071 | }) |
| 1072 | } |
| 1073 | |
| 1074 | // workTaskRunNow is the creator's Run it now anyway on a held step, at |
| 1075 | // the step revision its hold showed. |
| 1076 | func (a *application) workTaskRunNow(w http.ResponseWriter, r *http.Request) { |
| 1077 | id := chi.URLParam(r, "id") |
| 1078 | step := r.PostFormValue("step") |
| 1079 | revision, perr := strconv.ParseInt(r.PostFormValue("revision"), 10, 64) |
| 1080 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1081 | if perr != nil || step == "" { |
| 1082 | return chat.ErrInvalid |
| 1083 | } |
| 1084 | _, err := a.store.Chat().RunNow(ctx, address, id, wire.RunNow{Step: step, StepRevision: revision, Offer: r.PostFormValue("offer")}) |
| 1085 | if errors.Is(err, chat.ErrOfferChanged) { |
| 1086 | // What Run now would use changed since the page was drawn: back to |
| 1087 | // the page, which shows the offer as it is (review R1). |
| 1088 | return chat.ErrCardConflict |
| 1089 | } |
| 1090 | return err |
| 1091 | }) |
| 1092 | } |
| 1093 | |
| 1094 | // workTaskRound is Try another round from the task's decision. |
| 1095 | func (a *application) workTaskRound(w http.ResponseWriter, r *http.Request) { |
| 1096 | id := chi.URLParam(r, "id") |
| 1097 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1098 | _, err := a.store.Chat().RoundTask(ctx, address, id) |
| 1099 | return err |
| 1100 | }) |
| 1101 | } |
| 1102 | |
| 1103 | // workTaskKind is the task menu's switch between a change and an |
| 1104 | // exploration (the menu's value names the kind), at the task's revision |
| 1105 | // as the store reads it now. |
| 1106 | func (a *application) workTaskKind(w http.ResponseWriter, r *http.Request) { |
| 1107 | id := chi.URLParam(r, "id") |
| 1108 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1109 | t, err := a.store.Chat().Task(ctx, address, id) |
| 1110 | if err != nil { |
| 1111 | return err |
| 1112 | } |
| 1113 | _, err = a.store.Chat().SetKind(ctx, address, id, wire.SetKind{Kind: r.PostFormValue("of"), Revision: t.Revision}) |
| 1114 | return err |
| 1115 | }) |
| 1116 | } |
| 1117 | |
| 1118 | // workBack is where a work action returns: the page it came from, a |
| 1119 | // fixed path chosen here (the task id only when it is one), else the |
| 1120 | // conversation. |
| 1121 | func workBack(r *http.Request, id string) string { |
| 1122 | switch r.PostFormValue("from") { |
| 1123 | case "task": |
| 1124 | if taskIDPattern.MatchString(id) { |
| 1125 | return "/tasks/" + id |
| 1126 | } |
| 1127 | case "tasks": |
| 1128 | return "/tasks" |
| 1129 | case "needs": |
| 1130 | return "/tasks?needs=you" |
| 1131 | case "job": |
| 1132 | // A card answered on a job thread's page returns there. |
| 1133 | if job, ok := wire.JobOf(wire.JobLane(r.PostFormValue("job"))); ok { |
| 1134 | return "/jobs/" + job |
| 1135 | } |
| 1136 | } |
| 1137 | return "/conversation" |
| 1138 | } |
| 1139 | |
| 1140 | var taskIDPattern = regexp.MustCompile(`^tk[a-z0-9]{1,40}$`) |
| 1141 | |
| 1142 | // workUncombine is Undo on a task Equipo combined into another; it |
| 1143 | // returns to the task page it came from (the folded or the surviving one). |
| 1144 | func (a *application) workUncombine(w http.ResponseWriter, r *http.Request) { |
| 1145 | id := chi.URLParam(r, "id") |
| 1146 | back := r.PostFormValue("task") |
| 1147 | if back == "" { |
| 1148 | back = id |
| 1149 | } |
| 1150 | a.chatActionTo(w, r, workBack(r, back), func(ctx context.Context, address string) error { |
| 1151 | _, err := a.store.Chat().Uncombine(ctx, address, id) |
| 1152 | return err |
| 1153 | }) |
| 1154 | } |
| 1155 | |
| 1156 | // workDuplicate folds the task into the open task it duplicates: the |
| 1157 | // creator's one tap, the same MarkDuplicate a device makes. |
| 1158 | func (a *application) workDuplicate(w http.ResponseWriter, r *http.Request) { |
| 1159 | id := chi.URLParam(r, "id") |
| 1160 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1161 | _, err := a.store.Chat().MarkDuplicate(ctx, address, id, wire.MarkDuplicate{Of: r.PostFormValue("of")}) |
| 1162 | return err |
| 1163 | }) |
| 1164 | } |
| 1165 | |
| 1166 | // workThread adds the person's note to a task's thread. |
| 1167 | func (a *application) workThread(w http.ResponseWriter, r *http.Request) { |
| 1168 | id := chi.URLParam(r, "id") |
| 1169 | a.threadAction(w, r, id, func(ctx context.Context, address string) (string, error) { |
| 1170 | e, err := a.store.Chat().PostThread(ctx, address, id, wire.PostThread{Text: r.PostFormValue("body"), IdempotencyKey: r.PostFormValue("request_id")}) |
| 1171 | return e.ID, err |
| 1172 | }) |
| 1173 | } |
| 1174 | |
| 1175 | // workAsk is the task page's composer asking a specialist: the same Ask |
| 1176 | // a device makes, as the session's person. |
| 1177 | func (a *application) workAsk(w http.ResponseWriter, r *http.Request) { |
| 1178 | id := chi.URLParam(r, "id") |
| 1179 | a.threadAction(w, r, id, func(ctx context.Context, address string) (string, error) { |
| 1180 | q, err := a.store.Chat().Ask(ctx, address, id, wire.AskQuestion{Text: r.PostFormValue("body"), IdempotencyKey: r.PostFormValue("request_id"), Specialist: r.PostFormValue("specialist")}) |
| 1181 | return q.ID, err |
| 1182 | }) |
| 1183 | } |
| 1184 | |
| 1185 | // askOffline answers a question asked while the runner holding its |
| 1186 | // session is offline: which runner, and to ask again when it is back |
| 1187 | // (Mac runners slice F), in the composer's JSON or as a page. |
| 1188 | func (a *application) askOffline(w http.ResponseWriter, r *http.Request, id, label string) { |
| 1189 | line := copyText("api.runner_offline") |
| 1190 | if label != "" { |
| 1191 | line = fmt.Sprintf(copyText("work.ask.mac_offline"), label) |
| 1192 | } |
| 1193 | if strings.Contains(r.Header.Get("Accept"), "application/json") { |
| 1194 | runnerJSON(w, http.StatusConflict, map[string]any{"error": map[string]string{"code": "runner_offline", "message": line}}) |
| 1195 | return |
| 1196 | } |
| 1197 | idn, _ := identity.From(r) |
| 1198 | renderStatus(w, http.StatusConflict, "form_error", pageData{Nav: Navigation(), Key: "work", Address: idn.Address, Title: "form.conflict", ErrorKey: "form.conflict", ErrorText: line, Back: "/tasks/" + id, TeamName: copyBrand}) |
| 1199 | } |
| 1200 | |
| 1201 | // threadAction posts to a task's thread. chat-stream.js sends in the |
| 1202 | // background and reconciles its bubble by the returned id; without |
| 1203 | // script the form returns to the task page. |
| 1204 | func (a *application) threadAction(w http.ResponseWriter, r *http.Request, id string, act func(context.Context, string) (string, error)) { |
| 1205 | w.Header().Set("Cache-Control", "no-store") |
| 1206 | ctx, address, ok := sessionContext(r) |
| 1207 | if !ok { |
| 1208 | a.failure(w, r, ErrForbidden) |
| 1209 | return |
| 1210 | } |
| 1211 | posted, err := act(ctx, address) |
| 1212 | var offline *chat.RunnerOfflineError |
| 1213 | if errors.As(err, &offline) { |
| 1214 | a.askOffline(w, r, id, offline.Label) |
| 1215 | return |
| 1216 | } |
| 1217 | if err != nil { |
| 1218 | a.failure(w, r, chatError(err)) |
| 1219 | return |
| 1220 | } |
| 1221 | if strings.Contains(r.Header.Get("Accept"), "application/json") { |
| 1222 | runnerJSON(w, http.StatusOK, map[string]any{"message": map[string]string{"id": posted}}) |
| 1223 | return |
| 1224 | } |
| 1225 | http.Redirect(w, r, "/tasks/"+id, http.StatusSeeOther) |
| 1226 | } |
| 1227 | |
| 1228 | func (a *application) workTaskContinueCopy(w http.ResponseWriter, r *http.Request) { |
| 1229 | id := chi.URLParam(r, "id") |
| 1230 | a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| 1231 | return a.store.Chat().ContinueWithoutCopy(ctx, address, id) |
| 1232 | }) |
| 1233 | } |
| 1234 | |
| 1235 | func (v workView) DecisionCount() int { return len(v.Decisions) + len(v.Questions) } |
| 1236 | |