@blackbelt-technology/pi-dashboard-gmail-plugin 0.9.0 → 0.11.0
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/package.json +9 -4
- package/src/bridge/attachments.ts +4 -1
- package/src/bridge/gmail-api.ts +35 -4
- package/src/bridge/lease-client.ts +9 -2
- package/src/bridge/tools.ts +217 -9
- package/src/client/AutoSetup.tsx +787 -0
- package/src/client/GmailSettings.tsx +702 -176
- package/src/client/auto-setup.ts +124 -0
- package/src/client/ui.ts +24 -0
- package/src/client/wizard.ts +80 -20
- package/src/configSchema.json +14 -1
- package/src/i18n.ts +439 -9
- package/src/server/accounts.ts +37 -2
- package/src/server/google-oauth.ts +10 -2
- package/src/server/index.ts +7 -0
- package/src/server/lease.ts +9 -2
- package/src/server/routes.ts +132 -25
- package/src/server/setup/browser-service.ts +55 -0
- package/src/server/setup/capture.ts +96 -0
- package/src/server/setup/cdp-page.ts +308 -0
- package/src/server/setup/errors.ts +36 -0
- package/src/server/setup/local.ts +27 -0
- package/src/server/setup/mask.ts +11 -0
- package/src/server/setup/run.ts +723 -0
- package/src/server/setup/setup-routes.ts +123 -0
- package/src/server/setup/steps.ts +458 -0
- package/src/server/store-client.ts +30 -0
- package/src/shared/auto-setup.ts +132 -0
- package/src/shared/client-json.ts +3 -1
- package/src/shared/protocol.ts +2 -0
- package/src/shared/scopes.ts +20 -4
- package/src/shared/setup.ts +43 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatic Google Cloud setup — wire types shared by the gmail server run engine
|
|
3
|
+
* and the settings client. Pure types + two constants; no I/O.
|
|
4
|
+
* The run view is secret-free by construction: it carries no client secret and no
|
|
5
|
+
* dedicated-browser `instanceId`. See change: add-gmail-auto-setup.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Recipe step ids, in execution order. */
|
|
9
|
+
export const SETUP_STEP_IDS = ["project", "api", "branding", "publish", "client", "capture"] as const;
|
|
10
|
+
export type SetupStepId = (typeof SETUP_STEP_IDS)[number];
|
|
11
|
+
|
|
12
|
+
export type SetupStepStatus = "pending" | "running" | "done" | "skipped" | "handoff";
|
|
13
|
+
|
|
14
|
+
/** Manual wizard step (1–4 in `client/wizard.ts`) a recipe step maps to. */
|
|
15
|
+
export type WizardStepNumber = 1 | 2 | 3 | 4;
|
|
16
|
+
|
|
17
|
+
/** `project`,`api` → 1; `branding` → 2; `publish` → 3; `client`,`capture` → 4. */
|
|
18
|
+
export const WIZARD_STEP_OF: Record<SetupStepId, WizardStepNumber> = {
|
|
19
|
+
project: 1,
|
|
20
|
+
api: 1,
|
|
21
|
+
branding: 2,
|
|
22
|
+
publish: 3,
|
|
23
|
+
client: 4,
|
|
24
|
+
capture: 4,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type SetupRunState =
|
|
28
|
+
| "starting"
|
|
29
|
+
| "running"
|
|
30
|
+
| "waiting_user"
|
|
31
|
+
| "waiting_decision"
|
|
32
|
+
| "busy"
|
|
33
|
+
| "done"
|
|
34
|
+
| "handoff"
|
|
35
|
+
| "cancelled";
|
|
36
|
+
|
|
37
|
+
/** Human checkpoints the run waits on; it never performs these clicks itself. */
|
|
38
|
+
export type CheckpointKind = "sign_in" | "console_terms" | "policy" | "publish";
|
|
39
|
+
|
|
40
|
+
/** Dedicated-browser refusals that hand off at `project`. */
|
|
41
|
+
export type BrowserRefusalReason =
|
|
42
|
+
| "disabled"
|
|
43
|
+
| "unloaded"
|
|
44
|
+
| "no-chrome"
|
|
45
|
+
| "no-display"
|
|
46
|
+
| "owned-by-other-server"
|
|
47
|
+
| "profile-in-use"
|
|
48
|
+
| "launch-failed";
|
|
49
|
+
|
|
50
|
+
export type HandoffReason =
|
|
51
|
+
| "unexpected_page"
|
|
52
|
+
| "locale"
|
|
53
|
+
| "domain_blocked"
|
|
54
|
+
| "publish_cancelled"
|
|
55
|
+
| "timeout"
|
|
56
|
+
| "secret_capture_failed"
|
|
57
|
+
| "client_in_use"
|
|
58
|
+
| "browser_closed"
|
|
59
|
+
| BrowserRefusalReason;
|
|
60
|
+
|
|
61
|
+
export type Audience = "internal" | "external";
|
|
62
|
+
|
|
63
|
+
export interface PublishLinks {
|
|
64
|
+
homePage: string;
|
|
65
|
+
privacyPolicy: string;
|
|
66
|
+
authorizedDomain: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type SetupDecision =
|
|
70
|
+
| { kind: "audience"; detected: Audience; org?: string }
|
|
71
|
+
| { kind: "links"; links: PublishLinks };
|
|
72
|
+
|
|
73
|
+
export interface SetupRunView {
|
|
74
|
+
runId: string;
|
|
75
|
+
state: SetupRunState;
|
|
76
|
+
/** Step being worked on (or the one that stopped the run). */
|
|
77
|
+
step: SetupStepId;
|
|
78
|
+
steps: Array<{ id: SetupStepId; status: SetupStepStatus }>;
|
|
79
|
+
checkpoint?: { kind: CheckpointKind };
|
|
80
|
+
decision?: SetupDecision;
|
|
81
|
+
/** Holder label (session id or client label) of the dedicated profile; never an instance id. */
|
|
82
|
+
busy?: { holder?: string };
|
|
83
|
+
handoff?: {
|
|
84
|
+
step: SetupStepId;
|
|
85
|
+
wizardStep: WizardStepNumber;
|
|
86
|
+
reason: HandoffReason;
|
|
87
|
+
/** `domain_blocked` only: the domains the dedicated browser must allow. */
|
|
88
|
+
domains?: string[];
|
|
89
|
+
};
|
|
90
|
+
projectId?: string;
|
|
91
|
+
/** Resources this run made on Google's side, so a hand-off can name them. */
|
|
92
|
+
created: { projectId?: string; clients: string[] };
|
|
93
|
+
/** Set once the client is stored (done). */
|
|
94
|
+
clientId?: string;
|
|
95
|
+
ownerEmail?: string;
|
|
96
|
+
/** Audience the run chose, or the one already configured on a re-run (reported, not asked). */
|
|
97
|
+
audience?: Audience;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `GET /state` → `autoSetup`. */
|
|
101
|
+
export interface AutoSetupState {
|
|
102
|
+
available: boolean;
|
|
103
|
+
/** Why unavailable: a `BrowserRefusalReason`, `service_missing`, `remote_dashboard`, `run_active`. */
|
|
104
|
+
reason?: string;
|
|
105
|
+
/** The request came from a genuinely local peer. */
|
|
106
|
+
local: boolean;
|
|
107
|
+
/** A configured client without accounts would be replaced by the new one. */
|
|
108
|
+
replacesClient?: boolean;
|
|
109
|
+
/** Id of the live (or last, unacknowledged) run, so a reloaded settings page can re-attach to it. */
|
|
110
|
+
runId?: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** `POST setup/run` body. */
|
|
114
|
+
export interface SetupRunRequest {
|
|
115
|
+
mode: "new" | "existing";
|
|
116
|
+
projectId?: string;
|
|
117
|
+
ownerEmail: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** `POST setup/run/:id/continue` body (answers a decision or acknowledges a checkpoint). */
|
|
121
|
+
export interface SetupContinueRequest {
|
|
122
|
+
audience?: Audience;
|
|
123
|
+
links?: PublishLinks;
|
|
124
|
+
confirmLinks?: boolean;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Defaults for External publishing (design D7). */
|
|
128
|
+
export const DEFAULT_PUBLISH_LINKS: PublishLinks = {
|
|
129
|
+
homePage: "https://github.com/BlackBeltTechnology/pi-agent-dashboard",
|
|
130
|
+
privacyPolicy: "https://github.com/BlackBeltTechnology/pi-agent-dashboard",
|
|
131
|
+
authorizedDomain: "github.com",
|
|
132
|
+
};
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
* route (authoritative). See change: add-gmail-plugin.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { PROJECT_ID_RE } from "./setup.js";
|
|
9
|
+
|
|
8
10
|
interface ParsedClient {
|
|
9
11
|
clientId: string;
|
|
10
12
|
clientSecret: string;
|
|
@@ -48,6 +50,6 @@ export function validateClientJson(input: string | unknown): ClientJsonResult {
|
|
|
48
50
|
return { ok: false, error: { code: "missing_secret", step: 5 } };
|
|
49
51
|
}
|
|
50
52
|
const client: ParsedClient = { clientId: i.client_id, clientSecret: i.client_secret };
|
|
51
|
-
if (typeof i.project_id === "string" &&
|
|
53
|
+
if (typeof i.project_id === "string" && PROJECT_ID_RE.test(i.project_id)) client.projectId = i.project_id;
|
|
52
54
|
return { ok: true, client };
|
|
53
55
|
}
|
package/src/shared/protocol.ts
CHANGED
package/src/shared/scopes.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
export type Tier = "readonly" | "draft" | "send";
|
|
8
|
-
export type Op = "read" | "draft" | "send" | "modify" | "trash";
|
|
8
|
+
export type Op = "read" | "draft" | "send" | "modify" | "trash" | "filters_read" | "filters_write";
|
|
9
9
|
|
|
10
10
|
export const TIERS: readonly Tier[] = ["readonly", "draft", "send"];
|
|
11
11
|
|
|
@@ -14,6 +14,7 @@ export const SCOPE = {
|
|
|
14
14
|
readonly: `${SCOPE_BASE}gmail.readonly`,
|
|
15
15
|
compose: `${SCOPE_BASE}gmail.compose`,
|
|
16
16
|
modify: `${SCOPE_BASE}gmail.modify`,
|
|
17
|
+
settingsBasic: `${SCOPE_BASE}gmail.settings.basic`,
|
|
17
18
|
} as const;
|
|
18
19
|
|
|
19
20
|
/** Requested Gmail scopes per tier (full set — no incremental auth for installed apps). */
|
|
@@ -32,6 +33,8 @@ const OP_TIER: Record<Op, Tier> = {
|
|
|
32
33
|
send: "send",
|
|
33
34
|
modify: "send",
|
|
34
35
|
trash: "send",
|
|
36
|
+
filters_read: "readonly",
|
|
37
|
+
filters_write: "send",
|
|
35
38
|
};
|
|
36
39
|
|
|
37
40
|
/** Scope(s) that cover an op — any one suffices. */
|
|
@@ -41,6 +44,8 @@ const OP_SCOPE: Record<Op, string> = {
|
|
|
41
44
|
send: SCOPE.modify,
|
|
42
45
|
modify: SCOPE.modify,
|
|
43
46
|
trash: SCOPE.modify,
|
|
47
|
+
filters_read: SCOPE.settingsBasic,
|
|
48
|
+
filters_write: SCOPE.settingsBasic,
|
|
44
49
|
};
|
|
45
50
|
|
|
46
51
|
/** Implication table: a granted scope also covers these. `modify ⇒ readonly, compose`. */
|
|
@@ -52,8 +57,13 @@ export function isTier(v: unknown): v is Tier {
|
|
|
52
57
|
return v === "readonly" || v === "draft" || v === "send";
|
|
53
58
|
}
|
|
54
59
|
|
|
60
|
+
/** True for the filter ops (gated by the per-account filters option). */
|
|
61
|
+
export function isFilterOp(op: Op): boolean {
|
|
62
|
+
return op === "filters_read" || op === "filters_write";
|
|
63
|
+
}
|
|
64
|
+
|
|
55
65
|
export function isOp(v: unknown): v is Op {
|
|
56
|
-
return v === "read" || v === "draft" || v === "send" || v === "modify" || v === "trash";
|
|
66
|
+
return v === "read" || v === "draft" || v === "send" || v === "modify" || v === "trash" || v === "filters_read" || v === "filters_write";
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
/** True when `tier` is high enough for `op`. */
|
|
@@ -82,7 +92,13 @@ export function scopesCoverTier(granted: readonly string[], tier: Tier): boolean
|
|
|
82
92
|
return TIER_SCOPES[tier].every((s) => eff.has(s));
|
|
83
93
|
}
|
|
84
94
|
|
|
95
|
+
/** True when the granted scopes cover the filter ops (`gmail.settings.basic`; `modify` does not imply it). */
|
|
96
|
+
export function scopesCoverFilters(granted: readonly string[]): boolean {
|
|
97
|
+
return effectiveScopes(granted).has(SCOPE.settingsBasic);
|
|
98
|
+
}
|
|
99
|
+
|
|
85
100
|
/** Space-separated `scope` parameter for an authorization request. */
|
|
86
|
-
export function authScopeParam(tier: Tier): string {
|
|
87
|
-
|
|
101
|
+
export function authScopeParam(tier: Tier, opts?: { filters?: boolean }): string {
|
|
102
|
+
const filters = opts?.filters ? [SCOPE.settingsBasic] : [];
|
|
103
|
+
return ["openid", "email", ...TIER_SCOPES[tier], ...filters].join(" ");
|
|
88
104
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Setup-field validators shared by the wizard (instant feedback) and the
|
|
3
|
+
* server route (authoritative): project id and owner account. Pure.
|
|
4
|
+
* See change: improve-gmail-plugin-setup-ux.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** A Google Cloud project id: 6–30 chars, lower-case letters, digits, hyphens. */
|
|
8
|
+
export const PROJECT_ID_RE = /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/;
|
|
9
|
+
|
|
10
|
+
/** RFC 5321 maximum address length. */
|
|
11
|
+
const EMAIL_MAX = 254;
|
|
12
|
+
|
|
13
|
+
export type ProjectIdClass = "ok" | "empty" | "looks_like_name" | "invalid";
|
|
14
|
+
|
|
15
|
+
export function normalizeProjectId(v: string): string {
|
|
16
|
+
return v.trim();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function normalizeEmail(v: string): string {
|
|
20
|
+
return v.trim().toLowerCase();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `looks_like_name`: the console's display name (uppercase or whitespace) typed instead of the id. */
|
|
24
|
+
export function classifyProjectId(v: string): ProjectIdClass {
|
|
25
|
+
const id = normalizeProjectId(v);
|
|
26
|
+
if (!id) return "empty";
|
|
27
|
+
if (/[A-Z\s]/.test(id)) return "looks_like_name";
|
|
28
|
+
return PROJECT_ID_RE.test(id) ? "ok" : "invalid";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One `@`, a non-empty local part, and a domain with an interior dot; no whitespace.
|
|
33
|
+
* Linear time: a single `[^\s@]+\.[^\s@]+` regex backtracks polynomially because `.` also
|
|
34
|
+
* matches the class (CodeQL js/polynomial-redos).
|
|
35
|
+
*/
|
|
36
|
+
export function isEmail(v: string): boolean {
|
|
37
|
+
if (v.length > EMAIL_MAX || /\s/.test(v)) return false;
|
|
38
|
+
const at = v.indexOf("@");
|
|
39
|
+
if (at < 1 || at !== v.lastIndexOf("@")) return false;
|
|
40
|
+
const domain = v.slice(at + 1);
|
|
41
|
+
const dot = domain.indexOf(".", 1);
|
|
42
|
+
return dot !== -1 && dot < domain.length - 1;
|
|
43
|
+
}
|