@andrioid/paseo-attention-span 0.1.2 → 0.1.3

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.
package/server/service.ts CHANGED
@@ -5,8 +5,8 @@ import { classify, classifyHistory } from "./classify";
5
5
  import { FIXTURE, graphql } from "./gh";
6
6
  import { fixture, FIXTURE_AGENTS } from "./fixture";
7
7
  import type { Details, HistoryResult, LiveResult } from "./github-types";
8
- import { detailsQuery, HISTORY_SEARCHES, LIVE_SEARCHES, type SearchName, searchQuery, searchVariables, unclaimedScope } from "./queries";
9
- import { loadConfig, loadState, moveOldFiles, saveCodeRepo, saveCoverage, saveState, State } from "./store";
8
+ import { detailsQuery, HISTORY_SEARCHES, mergeUnclaimed, type SearchName, searchQuery, searchVariables, type UnclaimedSearch, unclaimedSearches } from "./queries";
9
+ import { loadConfig, loadState, moveOldFiles, saveCodeRepo, saveCoverage, saveNextUpSources, saveState, State } from "./store";
10
10
  import { buildView } from "./view";
11
11
 
12
12
  // History changes only when I finish something, so it's fetched daily, on ⟳ in its tab and after a merge.
@@ -93,11 +93,22 @@ export class AttentionService {
93
93
  await this.refresh("live").catch(() => {});
94
94
  }
95
95
 
96
+ // Where Next up looks, chosen in settings [NXT-04]. Only the live feed has Next up.
97
+ async setNextUpSources(sources: Pick<AttentionConfig["nextUp"], "mine" | "orgs" | "repos">): Promise<void> {
98
+ this.config = await saveNextUpSources(sources);
99
+ await this.refresh("live").catch(() => {});
100
+ }
101
+
96
102
  async setScopes(scopes: string[]): Promise<void> {
97
103
  this.state.scopes = scopes;
98
104
  await this.save();
99
105
  }
100
106
 
107
+ async setSources(sources: string[]): Promise<void> {
108
+ this.state.sources = sources;
109
+ await this.save();
110
+ }
111
+
101
112
  // Concurrent callers share one refresh per feed.
102
113
  refresh(id: FeedId): Promise<void> {
103
114
  const feed = this.feeds[id];
@@ -133,8 +144,15 @@ export class AttentionService {
133
144
 
134
145
  private async fetchLive(now: Date): Promise<Attention> {
135
146
  const projectRepos = FIXTURE ? [] : await this.projectRepos();
136
- const { data, details } = FIXTURE ? fixture(now) : await this.searchLive(now, projectRepos);
137
- return { ...classify({ data, details, config: this.config, now }), unclaimedFrom: unclaimedScope(this.config, projectRepos).from };
147
+ const login = FIXTURE ? "me" : this.config.nextUp.mine ? await this.login() : null;
148
+ const unclaimed = unclaimedSearches(this.config, projectRepos, login);
149
+ const { data, details } = FIXTURE ? fixture(now) : await this.searchLive(now, unclaimed.searches);
150
+ return { ...classify({ data, details, config: this.config, now }), unclaimedFrom: unclaimed.from };
151
+ }
152
+
153
+ // The account gh is signed in as, for My repos [NXT-04]. Asked once, unless the last fetch already says.
154
+ private async login(): Promise<string> {
155
+ return (this.state.live?.attention.login || (await graphql<{ viewer: { login: string } }>("{ viewer { login } }")).viewer.login);
138
156
  }
139
157
 
140
158
  // My Paseo projects' GitHub repos, where Next up looks when no repos are chosen [NXT-08].
@@ -157,19 +175,25 @@ export class AttentionService {
157
175
  }
158
176
 
159
177
  // One request per search, in parallel. The details request starts once the searches it needs are back [GH-01].
160
- private async searchLive(now: Date, projectRepos: string[]): Promise<{ data: LiveResult; details: Details }> {
161
- const all = searchVariables(this.config, now, projectRepos);
162
- const search = async (name: (typeof LIVE_SEARCHES)[number]): Promise<Partial<LiveResult>> =>
163
- all[name] === null ? { [name]: { nodes: [] } } : graphql<Partial<LiveResult>>(searchQuery([name], name === "mine"), { [name]: all[name] });
178
+ private async searchLive(now: Date, unclaimed: UnclaimedSearch[]): Promise<{ data: LiveResult; details: Details }> {
179
+ const all = searchVariables(this.config, now);
180
+ const search = (name: keyof typeof all): Promise<Partial<LiveResult>> => graphql<Partial<LiveResult>>(searchQuery([name], name === "mine"), { [name]: all[name] });
181
+ // One per Next up source [NXT-09].
182
+ const searchUnclaimed = async (): Promise<Partial<LiveResult>> => {
183
+ const results = await Promise.all(
184
+ unclaimed.map(async ({ source, query }) => ({ source, nodes: (await graphql<Pick<LiveResult, "unclaimed">>(searchQuery(["unclaimed"]), { unclaimed: query })).unclaimed.nodes })),
185
+ );
186
+ return { unclaimed: { nodes: mergeUnclaimed(results) } };
187
+ };
164
188
  const merge = (parts: Partial<LiveResult>[]) => Object.assign({}, ...parts) as LiveResult;
165
- const forDetails = Promise.all([search("mine"), search("assigned"), search("unclaimed")]);
189
+ const forDetails = Promise.all([search("mine"), search("assigned"), searchUnclaimed()]);
166
190
  const others = Promise.all([search("reviews"), search("mentions"), search("commented")]);
167
191
  const afterThem = forDetails.then((parts) => this.details(merge(parts)));
168
192
  const [first, rest, details] = await Promise.all([forDetails, others, afterThem]);
169
193
  return { data: merge([...first, ...rest]), details };
170
194
  }
171
195
 
172
- private variables(names: readonly SearchName[], now: Date): Record<string, string | null> {
196
+ private variables(names: readonly Exclude<SearchName, "unclaimed">[], now: Date): Record<string, string> {
173
197
  const all = searchVariables(this.config, now);
174
198
  return Object.fromEntries(names.map((name) => [name, all[name]]));
175
199
  }
@@ -241,6 +265,7 @@ export class AttentionService {
241
265
  return buildView(this.state.live?.attention ?? null, this.state.history?.items ?? [], this.state.dismissed, {
242
266
  feeds: { live: status("live"), history: status("history") },
243
267
  scopes: this.state.scopes,
268
+ sources: this.state.sources,
244
269
  config: this.config,
245
270
  ...(FIXTURE ? { fixtureAgents: FIXTURE_AGENTS } : {}),
246
271
  });
package/server/setup.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { ConfigStatus, CoverageCheck, CoverageKind, SetupStatus } from "../shared/setup";
2
- import { FIXTURE_ORGS, FIXTURE_REPOS } from "./fixture";
3
- import { FIXTURE, runGh } from "./gh";
2
+ import { FIXTURE_MY_ORGS, FIXTURE_ORGS, FIXTURE_REPOS } from "./fixture";
3
+ import { FIXTURE, graphql, runGh } from "./gh";
4
4
  import { loadConfig } from "./store";
5
5
 
6
6
  // What the setup checklist needs [SET-01]. Asked only when Attention Span can't load.
@@ -21,13 +21,20 @@ async function ghStatus(): Promise<SetupStatus["gh"]> {
21
21
 
22
22
  export async function configStatus(): Promise<ConfigStatus> {
23
23
  try {
24
- const { orgs, repos } = await loadConfig();
25
- return { error: null, orgs, repos };
24
+ const { orgs, repos, nextUp } = await loadConfig();
25
+ return { error: null, orgs, repos, nextUp: { mine: nextUp.mine, orgs: nextUp.orgs, repos: nextUp.repos } };
26
26
  } catch (error) {
27
- return { error: (error as Error).message, orgs: [], repos: [] };
27
+ return { error: (error as Error).message, orgs: [], repos: [], nextUp: { mine: false, orgs: [], repos: [] } };
28
28
  }
29
29
  }
30
30
 
31
+ // The orgs I belong to, for Next up's org picker [NXT-04]. Only these can be picked.
32
+ export async function myOrgs(): Promise<string[]> {
33
+ if (FIXTURE) return FIXTURE_MY_ORGS;
34
+ const { viewer } = await graphql<{ viewer: { organizations: { nodes: { login: string }[] } } }>("{ viewer { organizations(first: 100) { nodes { login } } } }");
35
+ return viewer.organizations.nodes.map((org) => org.login).sort((a, b) => a.localeCompare(b));
36
+ }
37
+
31
38
  // An org or repo is checked on GitHub before it's saved, so a typo shows right away [SRC-02].
32
39
  // Pasted GitHub URLs are accepted. The exact name GitHub returns is what gets saved.
33
40
  export async function checkCoverage(kind: CoverageKind, input: string): Promise<CoverageCheck> {
package/server/store.ts CHANGED
@@ -16,12 +16,17 @@ const STATE = join(DIR, "state.json");
16
16
  export const State = z.object({
17
17
  // Item key -> others' activity and section when dismissed [ATT-08].
18
18
  dismissed: z.record(z.string(), z.object({ at: z.string(), section: z.custom<SectionId>() })).default({}),
19
- // Scope chips picked on Next up; none means all [NXT-03].
19
+ // Scopes ticked in Next up's filter; none means all [NXT-03].
20
20
  scopes: z.array(z.string()).default([]),
21
+ // Sources ticked in Next up's filter; none means all [NXT-09].
22
+ sources: z.array(z.string()).default([]),
21
23
  // My Paseo projects' GitHub repos when last listed, for refreshes before the app has connected [NXT-08].
22
24
  projectRepos: z.array(z.string()).default([]),
23
25
  // The last fetch of each feed. A cache from an older version is dropped rather than shown wrong.
24
- live: z.object({ fetchedAt: z.string(), attention: z.custom<Attention>((value) => Array.isArray((value as Attention)?.unclaimed)) }).nullable().catch(null),
26
+ live: z
27
+ .object({ fetchedAt: z.string(), attention: z.custom<Attention>((value) => (value as Attention)?.unclaimed?.every?.((issue) => Array.isArray(issue.sources) && Array.isArray(issue.openPrs))) })
28
+ .nullable()
29
+ .catch(null),
25
30
  history: z.object({ fetchedAt: z.string(), items: z.custom<Item[]>(Array.isArray) }).nullable().catch(null),
26
31
  });
27
32
  export type State = z.infer<typeof State>;
@@ -68,6 +73,14 @@ export function saveCoverage(orgs: string[], repos: string[]): Promise<Attention
68
73
  return updateConfig((file) => ({ ...file, orgs, repos }));
69
74
  }
70
75
 
76
+ // Where Next up looks, chosen in settings [NXT-04]. The fields and the rest of nextUp are kept.
77
+ export function saveNextUpSources(sources: Pick<AttentionConfig["nextUp"], "mine" | "orgs" | "repos">): Promise<AttentionConfig> {
78
+ return updateConfig((file) => {
79
+ const nextUp = file.nextUp && typeof file.nextUp === "object" ? (file.nextUp as Record<string, unknown>) : {};
80
+ return { ...file, nextUp: { ...nextUp, ...sources } };
81
+ });
82
+ }
83
+
71
84
  // What the screen writes changes only its own keys; everything else in the file is kept as it is.
72
85
  // A file that doesn't read or parse is left alone.
73
86
  async function updateConfig(change: (file: Record<string, unknown>) => Record<string, unknown>): Promise<AttentionConfig> {
package/server/view.ts CHANGED
@@ -5,6 +5,7 @@ import type { State } from "./store";
5
5
  export interface ViewMeta {
6
6
  feeds: Record<FeedId, FeedStatus>;
7
7
  scopes: string[];
8
+ sources: string[];
8
9
  config: AttentionConfig;
9
10
  }
10
11
 
@@ -73,6 +73,9 @@ export interface WaitingComment {
73
73
  url?: string;
74
74
  }
75
75
 
76
+ // GitHub's reasons for closing an issue that Next up offers [NXT-11].
77
+ export type IssueCloseReason = "completed" | "not_planned";
78
+
76
79
  // What a session or GitHub action works on.
77
80
  export interface Target {
78
81
  kind: "pr" | "issue";
@@ -136,6 +139,12 @@ export interface NextUpIssue {
136
139
  effort?: string;
137
140
  scope?: string;
138
141
  defaultBranch?: string;
142
+ // Ids of the Next up sources that found it; empty without sources of its own [NXT-09].
143
+ sources: string[];
144
+ // Its priority's place in the field's order, 0 first; missing when unknown [NXT-02].
145
+ priorityRank?: number;
146
+ // Open PRs that close it: someone may already be on it [NXT-10].
147
+ openPrs: { number: number; url: string; author: string | null }[];
139
148
  }
140
149
 
141
150
  // More about an item, fetched when its sheet opens rather than in every refresh [GH-01].
@@ -200,8 +209,10 @@ export interface AttentionView {
200
209
  unclaimed: NextUpIssue[];
201
210
  unclaimedFrom: UnclaimedSource;
202
211
  dismissedCount: number;
203
- // Scope chips picked on Next up; none means all [NXT-03].
212
+ // Scopes ticked in Next up's filter; none means all [NXT-03].
204
213
  scopes: string[];
214
+ // Sources ticked in Next up's filter; none means all [NXT-09].
215
+ sources: string[];
205
216
  config: AttentionConfig;
206
217
  // Fixture mode only: made-up sessions, since the preview daemon has no agents.
207
218
  fixtureAgents?: Record<string, AgentSummary>;
package/shared/config.ts CHANGED
@@ -38,18 +38,35 @@ export const AttentionConfig = z.object({
38
38
  mentionDays: z.number().positive().default(30),
39
39
  // How many weeks History shows [HIS-01].
40
40
  historyWeeks: z.number().int().positive().default(4),
41
- // Unclaimed issues to pick from [NXT-04]. Repos default to `orgs` and `repos`.
41
+ // Unclaimed issues to pick from [NXT-04]. With no sources chosen, Next up looks in `orgs` and `repos` [NXT-08].
42
42
  nextUp: z
43
43
  .object({
44
- repos: z.array(z.string()).optional(),
44
+ // Sources: my own repos (not forks), whole orgs, and single "owner/name" repos.
45
+ mine: z.boolean().default(false),
46
+ orgs: z.array(z.string()).default([]),
47
+ repos: z.array(z.string()).default([]),
45
48
  priority: RankedField.optional(),
46
49
  effort: RankedField.optional(),
47
50
  scope: z.object({ field: z.string() }).optional(),
48
51
  })
49
- .default({}),
52
+ .default({ mine: false, orgs: [], repos: [] }),
50
53
  });
51
54
  export type AttentionConfig = z.infer<typeof AttentionConfig>;
52
55
 
56
+ // Where Next up looks, one search each; `id` is what the chips I pick remember [NXT-09].
57
+ export interface NextUpSource {
58
+ id: string;
59
+ label: string;
60
+ }
61
+
62
+ export function nextUpSources({ nextUp }: Pick<AttentionConfig, "nextUp">): NextUpSource[] {
63
+ return [
64
+ ...(nextUp.mine ? [{ id: "mine", label: "My repos" }] : []),
65
+ ...nextUp.orgs.map((org) => ({ id: `org:${org}`, label: org })),
66
+ ...nextUp.repos.map((repo) => ({ id: `repo:${repo}`, label: repo })),
67
+ ];
68
+ }
69
+
53
70
  // "owner/name" of a Paseo project with a GitHub remote, from its project key.
54
71
  export function githubRepoOf(projectKey: string | null | undefined): string | null {
55
72
  return /^remote:github\.com\/(.+)$/i.exec(projectKey ?? "")?.[1] ?? null;
package/shared/format.ts CHANGED
@@ -19,3 +19,13 @@ export function daysSince(iso: string, now: Date): number {
19
19
  export function linkList(items: { title: string; url: string }[]): string {
20
20
  return items.map(({ title, url }) => `- [${title.replace(/[[\]\\]/g, "\\$&")}](${url})`).join("\n");
21
21
  }
22
+
23
+ // An issue field value for a card: "🐟 Tiny (< 4h)" -> "Tiny", "🏕P2 Medium" -> "P2 Medium" [NXT-10]. The sheet keeps the full value.
24
+ export function shortValue(value: string): string {
25
+ return (
26
+ value
27
+ .replace(/^[^\p{L}\p{N}]+/u, "")
28
+ .replace(/\s*\([^)]*\)\s*$/, "")
29
+ .trim() || value
30
+ );
31
+ }
package/shared/rpc.ts CHANGED
@@ -23,8 +23,12 @@ export const setupStatus = defineRpc({ name: "setup.status", input: z.object({})
23
23
  export const getCoverage = defineRpc({ name: "config.coverage", input: z.object({}), output: z.custom<ConfigStatus>() });
24
24
  export const checkCoverage = defineRpc({ name: "config.check-coverage", input: z.object({ kind: z.enum(["org", "repo"]), name: z.string() }), output: z.custom<CoverageCheck>() });
25
25
  export const setCoverage = defineRpc({ name: "config.set-coverage", input: z.object({ orgs: z.array(z.string()), repos: z.array(z.string()) }), output: done });
26
- // Remember the scope chips picked on Next up [NXT-03].
26
+ // Where Next up looks, chosen in settings [NXT-04]; orgs are picked from the ones I belong to.
27
+ export const setNextUpSources = defineRpc({ name: "config.set-next-up", input: z.object({ mine: z.boolean(), orgs: z.array(z.string()), repos: z.array(z.string()) }), output: done });
28
+ export const myOrgs = defineRpc({ name: "config.my-orgs", input: z.object({}), output: z.object({ orgs: z.array(z.string()) }) });
29
+ // Remember the scopes and sources ticked in Next up's filter [NXT-03, NXT-09].
27
30
  export const setScopes = defineRpc({ name: "next-up.scopes", input: z.object({ scopes: z.array(z.string()) }), output: done });
31
+ export const setSources = defineRpc({ name: "next-up.sources", input: z.object({ sources: z.array(z.string()) }), output: done });
28
32
 
29
33
  // GitHub changes, each from an explicit tap [SAFE-01].
30
34
  export const updateBranch = defineRpc({ name: "pr.update-branch", input: z.object({ target }), output: done });
@@ -32,6 +36,9 @@ export const mergePullRequest = defineRpc({ name: "pr.merge", input: z.object({
32
36
  export const closePullRequest = defineRpc({ name: "pr.close", input: z.object({ target, comment: z.string().optional() }), output: done });
33
37
  export const claimIssue = defineRpc({ name: "issue.claim", input: z.object({ target }), output: done });
34
38
  export const unclaimIssue = defineRpc({ name: "issue.unclaim", input: z.object({ target }), output: done });
39
+ // Close an issue from Next up; reopen is its Undo [NXT-11].
40
+ export const closeIssue = defineRpc({ name: "issue.close", input: z.object({ target, reason: z.enum(["completed", "not_planned"]), comment: z.string().optional() }), output: done });
41
+ export const reopenIssue = defineRpc({ name: "issue.reopen", input: z.object({ target }), output: done });
35
42
  // `ready: false` and `on: false` are the Undo of each.
36
43
  export const setReady = defineRpc({ name: "pr.ready", input: z.object({ target, ready: z.boolean() }), output: done });
37
44
  export const setAutoMerge = defineRpc({ name: "pr.auto-merge", input: z.object({ target, on: z.boolean() }), output: done });
package/shared/setup.ts CHANGED
@@ -5,6 +5,8 @@ export interface ConfigStatus {
5
5
  error: string | null;
6
6
  orgs: string[];
7
7
  repos: string[];
8
+ // Where Next up looks; none chosen falls back to `orgs` and `repos`, then my Paseo projects [NXT-04, NXT-08].
9
+ nextUp: { mine: boolean; orgs: string[]; repos: string[] };
8
10
  }
9
11
 
10
12
  export interface SetupStatus {