Skip to content
refs
123 lines · 5.0 KBRaw
1import type { TimelineItem } from "../contract/events.ts";
2import type { Repo, Task } from "../contract/handles.ts";
3import type { TaskStatus } from "../contract/views.ts";
4import { readStream, taskStream } from "../events.ts";
5import { decorateMentions, mentionedSessions } from "../mentions.ts";
6import { firstInvalid, invalid, ok, raise } from "../outcomes.ts";
7import { Policy } from "../policy.ts";
8import { type RepoRecord } from "../repos.ts";
9import * as tasks from "../tasks.ts";
10import type { Deps } from "./context.ts";
11
12export async function openTask(deps: Deps, policy: Policy, record: tasks.TaskRecord, visibleRepo: (name: string) => Promise<RepoRecord>, repo: () => Promise<Repo>): Promise<Task> {
13 const db = deps.env.DB;
14 const actor = policy.actor;
15 const person = policy.person!;
16 const extras = await tasks.taskExtras(db, record.id);
17
18 const commentAuthor = (item: TimelineItem) => item.by.onBehalfOf?.id ?? item.by.actor.id;
19 const detail = tasks.toDetail(record, extras.labels, extras.comments);
20
21 async function handOff(session: string, note: string) {
22 const handed = await (await repo()).handOff({ title: record.title, body: [record.body, note].filter(Boolean).join("\n\n"), session });
23 if (!handed.ok) return handed;
24 const { pull, agent } = handed.value;
25 await tasks.recordHandOff(db, actor, record, { agent: { session: agent.session, runtime: agent.runtime, onBehalfOf: agent.onBehalfOf }, repo: record.repo_name, pull });
26 return ok({ pull });
27 }
28
29 return {
30 detail,
31
32 async timeline() {
33 const moderator = policy.canModerateComments();
34 const events = await readStream(db, taskStream(record.id));
35 return Promise.all(
36 events.map(async (event) =>
37 event.kind === "task.commented"
38 ? { ...event, data: { ...event.data, html: await decorateMentions(db, event.data.html) }, can: { edit: event.by.actor.kind === "human" && event.by.actor.id === person.id, delete: moderator || commentAuthor(event) === person.id } }
39 : event,
40 ),
41 );
42 },
43
44 async comment(body, then) {
45 const text = body.trim();
46 const bodyProblem = firstInvalid({ body: tasks.requiredBodyError(text, "Comment", "Write a comment first.") });
47 if (bodyProblem) return bodyProblem;
48 const sessions = mentionedSessions(text);
49 if (sessions.length) {
50 const online = new Set((await (await repo()).agents()).map((agent) => agent.session));
51 const away = sessions.find((session) => !online.has(session));
52 if (away) return invalid(`Agent ${away.slice(0, 4)} is not online in this repository. Mention an online agent.`, "body");
53 }
54 await tasks.addComment(db, actor, record, text, then);
55 for (const session of sessions) {
56 const handed = await handOff(session, text);
57 if (!handed.ok) return handed;
58 }
59 return ok();
60 },
61
62 async setStatus(status: TaskStatus) {
63 await tasks.setStatus(db, actor, record, status);
64 return ok();
65 },
66
67 async rename(title) {
68 const text = title.trim();
69 const titleProblem = firstInvalid({ title: tasks.titleError(text) });
70 if (titleProblem) return titleProblem;
71 await tasks.rename(db, actor, record, text);
72 return ok();
73 },
74
75 async describe(body) {
76 const text = body.trim();
77 const bodyProblem = firstInvalid({ body: tasks.bodyError(text) });
78 if (bodyProblem) return bodyProblem;
79 await tasks.describe(db, record, text);
80 return ok();
81 },
82
83 async assign(id) {
84 if (id === null) {
85 await tasks.assign(db, actor, record, null);
86 return ok();
87 }
88 const name = await tasks.memberName(db, record.repo_id, id);
89 if (!name) return invalid("Choose a member to assign.", "assignee");
90 await tasks.assign(db, actor, record, { id, name });
91 return ok();
92 },
93
94 async handToAgent(session) {
95 return handOff(session, "");
96 },
97
98 async transfer(repoName) {
99 const target = await visibleRepo(repoName).catch(() => null);
100 if (!target) return invalid("Choose a repository.", "repo");
101 if (target.id === record.repo_id) return ok({ repo: record.repo_name, number: record.number });
102 return ok(await tasks.transfer(db, actor, record, { id: target.id, name: target.full_name }));
103 },
104
105 async setLabels(labels) {
106 await tasks.setLabels(db, actor, record, labels);
107 return ok();
108 },
109
110 async editComment(comment, body) {
111 const text = body.trim();
112 const bodyProblem = firstInvalid({ body: tasks.requiredBodyError(text, "Comment", "Comments can't be empty.") });
113 if (bodyProblem) return bodyProblem;
114 if (!(await tasks.editComment(db, record, Number(comment), person.id, text))) raise("not_found", "No such comment, or it isn't yours.");
115 return ok();
116 },
117
118 async deleteComment(comment) {
119 if (!(await tasks.deleteComment(db, record, Number(comment), person.id, policy.canModerateComments()))) raise("not_found", "No such comment, or you can't delete it.");
120 return ok();
121 },
122 };
123}