@fungi.computer/bonsai 0.4.7 → 0.5.1

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.
@@ -0,0 +1,166 @@
1
+ import { useState } from "react";
2
+ import { useQueryClient } from "@tanstack/react-query";
3
+ import { Alert } from "@fungi.computer/caps/components/alert";
4
+ import { Button } from "@fungi.computer/caps/components/button";
5
+ import { Checkbox } from "@fungi.computer/caps/components/checkbox";
6
+ import {
7
+ Dialog,
8
+ DialogContent,
9
+ DialogDescription,
10
+ DialogFooter,
11
+ DialogHeader,
12
+ DialogTitle,
13
+ } from "@fungi.computer/caps/components/dialog";
14
+ import { Field } from "@fungi.computer/caps/components/field";
15
+ import { Input } from "@fungi.computer/caps/components/input";
16
+ import { SetupNotice } from "@fungi.computer/caps/blocks/setup-notice";
17
+ import { repoNameProblem } from "../shared/repo-name.js";
18
+ import { createRepository } from "./api.js";
19
+ import { href, useRouter } from "./router.js";
20
+ import { isPermissionDenied, requestGitAccess } from "./transport.js";
21
+
22
+ /** Name a repository, optionally commit a README, and open it. */
23
+ export function NewRepositoryDialog({
24
+ open,
25
+ onOpenChange,
26
+ }: {
27
+ open: boolean;
28
+ onOpenChange: (open: boolean) => void;
29
+ }) {
30
+ const client = useQueryClient();
31
+ const { go } = useRouter();
32
+ const [name, setName] = useState("");
33
+ const [readme, setReadme] = useState(true);
34
+ const [touched, setTouched] = useState(false);
35
+ const [creating, setCreating] = useState(false);
36
+ const [needsAccess, setNeedsAccess] = useState(false);
37
+ const [waiting, setWaiting] = useState(false);
38
+ const [failure, setFailure] = useState<string | null>(null);
39
+ const problem = repoNameProblem(name);
40
+ const shownProblem = touched ? problem : null;
41
+
42
+ async function create(): Promise<"created" | "refused" | "failed"> {
43
+ setTouched(true);
44
+ if (problem !== null) return "refused";
45
+ setCreating(true);
46
+ setFailure(null);
47
+ try {
48
+ const created = await createRepository({ name, readme });
49
+ await client.invalidateQueries({ queryKey: ["repos"] });
50
+ onOpenChange(false);
51
+ setName("");
52
+ setNeedsAccess(false);
53
+ go(
54
+ href({
55
+ page: "repo",
56
+ repo: created.name,
57
+ tab: "overview",
58
+ ref: null,
59
+ file: null,
60
+ path: "",
61
+ }),
62
+ );
63
+ return "created";
64
+ } catch (error) {
65
+ if (error instanceof Error && isPermissionDenied(error))
66
+ setNeedsAccess(true);
67
+ else
68
+ setFailure(
69
+ error instanceof Error ? error.message : "Couldn't create it.",
70
+ );
71
+ return "failed";
72
+ } finally {
73
+ setCreating(false);
74
+ }
75
+ }
76
+
77
+ async function approve(): Promise<void> {
78
+ setWaiting(true);
79
+ try {
80
+ const outcome = await requestGitAccess();
81
+ if (outcome === "granted") {
82
+ setNeedsAccess(false);
83
+ await create();
84
+ } else if (outcome === "declined") {
85
+ setFailure("Bonsai needs Git access to create a repository.");
86
+ } else if (outcome === "unavailable") {
87
+ setFailure("Open this Oyster's App settings and allow Git for Bonsai.");
88
+ }
89
+ } finally {
90
+ setWaiting(false);
91
+ }
92
+ }
93
+
94
+ return (
95
+ <Dialog open={open} onOpenChange={onOpenChange}>
96
+ <DialogContent>
97
+ <DialogHeader className="mb-4">
98
+ <DialogTitle>New repository</DialogTitle>
99
+ <DialogDescription>
100
+ Pick a short name. Lowercase letters, digits and dashes work.
101
+ </DialogDescription>
102
+ </DialogHeader>
103
+ <form
104
+ className="space-y-4"
105
+ onSubmit={(event) => {
106
+ event.preventDefault();
107
+ void create();
108
+ }}
109
+ >
110
+ <Field label="Name" error={shownProblem} required>
111
+ <Input
112
+ autoFocus
113
+ value={name}
114
+ placeholder="koi-pond"
115
+ onChange={(event) => {
116
+ setName(event.target.value);
117
+ }}
118
+ onBlur={() => {
119
+ setTouched(true);
120
+ }}
121
+ />
122
+ </Field>
123
+ <Field label="Start with a README" orientation="inline">
124
+ <Checkbox
125
+ checked={readme}
126
+ onChange={(event) => {
127
+ setReadme(event.target.checked);
128
+ }}
129
+ />
130
+ </Field>
131
+ {needsAccess ? (
132
+ <SetupNotice
133
+ variant="inline"
134
+ need="Bonsai needs Git access to save your repositories"
135
+ cause="Approve once and Bonsai can save your repositories."
136
+ fix={{
137
+ label: "Approve",
138
+ onSelect: () => void approve(),
139
+ pending: waiting,
140
+ }}
141
+ waitingFor="your answer"
142
+ />
143
+ ) : null}
144
+ {failure !== null ? (
145
+ <Alert tone="error" description={failure} />
146
+ ) : null}
147
+ <DialogFooter className="mt-2">
148
+ <Button
149
+ type="button"
150
+ variant="ghost"
151
+ disabled={creating}
152
+ onClick={() => {
153
+ onOpenChange(false);
154
+ }}
155
+ >
156
+ Cancel
157
+ </Button>
158
+ <Button type="submit" disabled={creating || needsAccess}>
159
+ {creating ? "Creating…" : "Create repository"}
160
+ </Button>
161
+ </DialogFooter>
162
+ </form>
163
+ </DialogContent>
164
+ </Dialog>
165
+ );
166
+ }
package/web/overview.tsx CHANGED
@@ -220,7 +220,7 @@ export function Overview({
220
220
  variant="quiet"
221
221
  headingLevel={3}
222
222
  title="No changes yet"
223
- description="Proposed updates will appear here."
223
+ description="When an Agent pushes to its fork, its change shows up here to merge."
224
224
  />,
225
225
  );
226
226
  // Nothing real to show yet: render nothing rather than a stuck loader (a
@@ -287,7 +287,10 @@ export function Overview({
287
287
  />
288
288
  ) : (
289
289
  <div className="line-clamp-6">
290
- <Markdown text={readme.data.text ?? "README is not a text file."} />
290
+ <Markdown
291
+ tile
292
+ text={readme.data.text ?? "README is not a text file."}
293
+ />
291
294
  </div>
292
295
  ),
293
296
  <Link to={repoLink("code", readmePath)}>Open in Code ›</Link>,
@@ -334,6 +337,7 @@ export function Overview({
334
337
  repo={route.repo}
335
338
  definitions={tiles}
336
339
  controlsTarget={controlsTarget}
340
+ compactHero={(graph.data?.nodes.length ?? Infinity) <= 3}
337
341
  />
338
342
  );
339
343
  }
package/web/transport.ts CHANGED
@@ -24,13 +24,38 @@ export class ApiError extends Error {
24
24
  }
25
25
  }
26
26
 
27
- let connection: Pick<AppRoot, "capabilities"> | null = null;
27
+ export type Connection = Readonly<{
28
+ capabilities: AppRoot["capabilities"];
29
+ /** The host's permission request, when this host can ask on Bonsai's behalf. */
30
+ ui?: Pick<AppRoot["ui"], "requestAppPermissions"> | undefined;
31
+ }>;
32
+
33
+ let connection: Connection | null = null;
28
34
 
29
35
  /** Remember the host connection mountApp handed over (null outside a host). */
30
- export function connect(app: Pick<AppRoot, "capabilities"> | null): void {
36
+ export function connect(app: Connection | null): void {
31
37
  connection = app;
32
38
  }
33
39
 
40
+ /** What the person answered when Bonsai asked the host for Git access. */
41
+ export type GitAccessOutcome =
42
+ "granted" | "declined" | "customize" | "unavailable";
43
+
44
+ /** Ask the host's consent sheet for the rights Bonsai's writes need. */
45
+ export async function requestGitAccess(): Promise<GitAccessOutcome> {
46
+ const ui = connection?.ui;
47
+ if (ui === undefined) return "unavailable";
48
+ return ui.requestAppPermissions({
49
+ git: ["git.repository"],
50
+ events: ["app.events.emit"],
51
+ });
52
+ }
53
+
54
+ /** A write the host refused because Bonsai has not been given that permission. */
55
+ export function isPermissionDenied(error: Error): boolean {
56
+ return error instanceof ApiError && error.status === 403;
57
+ }
58
+
34
59
  /** The Team's files-role App as the shell's role routing names it; null hides the entry. */
35
60
  export async function filesEditor(): Promise<AppRoleChoice> {
36
61
  if (!connection?.capabilities.has(appsOpen)) return null;
package/web/views.tsx CHANGED
@@ -146,6 +146,21 @@ export function RepoTree({ paths, active, onOpen }: TreeProps) {
146
146
  }
147
147
 
148
148
  /** Markdown from a repository, rendered by the shared Caps component. */
149
- export function Markdown({ text }: { text: string }) {
150
- return <CapsMarkdown className="bonsai-markdown">{text}</CapsMarkdown>;
149
+ export function Markdown({
150
+ text,
151
+ tile = false,
152
+ }: {
153
+ text: string;
154
+ /** Inside an overview tile, which already pads its content. */
155
+ tile?: boolean;
156
+ }) {
157
+ return (
158
+ <CapsMarkdown
159
+ className={
160
+ tile ? "bonsai-markdown bonsai-markdown-tile" : "bonsai-markdown"
161
+ }
162
+ >
163
+ {text}
164
+ </CapsMarkdown>
165
+ );
151
166
  }
@@ -16,7 +16,9 @@ export function workingNow(events: readonly Activity[]) {
16
16
  { repo: string; ref: string; at: number; paths: Set<string> }
17
17
  >();
18
18
  for (const event of events) {
19
- if (event.type !== "push" || event.at < since) continue;
19
+ // Working now means an Agent on its own fork, not a finished commit to main.
20
+ if (event.type !== "push" || event.at < since || !event.repo.includes("--"))
21
+ continue;
20
22
  const current = byAgent.get(event.actor);
21
23
  const ref = refLabel(event.data.updates[0]?.ref ?? "");
22
24
  if (!current)