@aiti-ai/aiti-codex-agent 0.1.13 → 0.1.14
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/client/src/controlled-login-dialog.mjs +53 -53
- package/dist/controlled-login-dialog.mjs +53 -53
- package/package.json +2 -2
- package/tools/deploy.mjs +3 -3
- package/dev-pack.mjs +0 -259
- package/reconcile-deps.mjs +0 -41
- package/reload-instances.mjs +0 -60
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* controlled-login —
|
|
2
|
+
* controlled-login — External agent controlled login standard · unified frontend dialog (native h() rendering, zero build step).
|
|
3
3
|
*
|
|
4
|
-
* ⚠
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* ⚠ Copy sync (byte-level copies; changes must be synced to all copies; test `_shared/controlled-login.test.mjs` pins consistency).
|
|
5
|
+
* Authoritative source: pi-web-ext/_shared/controlled-login-dialog.mjs
|
|
6
|
+
* Deployed copies (client side imports relative to the deployment tree — a zero-build client cannot inline ../../_shared
|
|
7
|
+
* at build time the way a vite client can; the asset route rejects traversal above the tree; same convention as draft-keeper's ./core.mjs):
|
|
8
8
|
* pi-web-ext/aiti-{zcode,cursor,qoder,claude-code,codex}-agent/client/controlled-login-dialog.mjs
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* +
|
|
13
|
-
* + B
|
|
14
|
-
*
|
|
15
|
-
*
|
|
10
|
+
* Contract (docs/ext-controlled-login-standard.md §3.5):
|
|
11
|
+
* 🔗 "Open authorization page" button (target=_blank) + copyable URL text + mode explanation line
|
|
12
|
+
* + status area (waiting spinner / success ✅ username / failed / timeout / cancelled, all with retry)
|
|
13
|
+
* + B mode (paste)-only code input + submit.
|
|
14
|
+
* Status refresh: while the dialog is open, poll GET /login/status every 2s; stops on close
|
|
15
|
+
* (the polling exception argued and decided in plan §3.6: login is a minutes-scale one-shot interaction).
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
17
|
+
* Float contract: panel root node data-ext-float="1" + data-ext-float-w/-h + data-ext-float-full="1"
|
|
18
|
+
* (aiti-float takes over dragging/resizing/persistence/small-screen fullscreen; without it, its own style centers as a fallback).
|
|
19
19
|
*
|
|
20
|
-
*
|
|
20
|
+
* Wiring (each extension's client/controlled-login-client.mjs, two lines):
|
|
21
21
|
* import { registerControlledLoginClient } from "./controlled-login-dialog.mjs";
|
|
22
22
|
* export default function register(ctx) { registerControlledLoginClient(ctx, { … }); }
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
// ----------
|
|
25
|
+
// ---------- Module-scoped open state (shared by the toolbar entry and the dialogs entry within one module; never a window global) ----------
|
|
26
26
|
let moduleSetOpen = null;
|
|
27
|
-
//
|
|
28
|
-
//
|
|
27
|
+
// Under lazy loading (clientImmediately:false) the dialog component may not be mounted yet when the toolbar static entry is clicked:
|
|
28
|
+
// record pending; the mount effect consumes it — avoids a dead first click.
|
|
29
29
|
let pendingOpen = false;
|
|
30
30
|
|
|
31
|
-
/**
|
|
31
|
+
/** Opens this extension's controlled login dialog (for the toolbar 🔑 button / static entry meta.onClick). */
|
|
32
32
|
export function openControlledLogin() {
|
|
33
33
|
if (moduleSetOpen) moduleSetOpen(true);
|
|
34
34
|
else pendingOpen = true;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
// ----------
|
|
37
|
+
// ---------- Self-contained style layer (idempotent injection; clg- prefix + var(--x, fallback), zero host coupling) ----------
|
|
38
38
|
const STYLE_ID = "aiti-controlled-login-styles";
|
|
39
39
|
const STYLES = `
|
|
40
40
|
.clg-mask { position: fixed; inset: 0; z-index: 2147483000; background: rgba(0,0,0,.32); display: flex; align-items: center; justify-content: center; }
|
|
@@ -93,16 +93,16 @@ function ensureStyles() {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
|
-
*
|
|
96
|
+
* Unified dialog registration (called once from each extension client's thin shell).
|
|
97
97
|
*
|
|
98
|
-
* @param {*} ctx
|
|
98
|
+
* @param {*} ctx host client context (h/useState/useEffect/apiUrl/registerItems)
|
|
99
99
|
* @param {object} config
|
|
100
|
-
* @param {string} config.extId
|
|
101
|
-
* @param {string} config.toolbarLabel
|
|
102
|
-
* @param {string} config.title
|
|
103
|
-
* @param {string} [config.urlHint] A url-only
|
|
104
|
-
* @param {string} [config.pasteHint] B paste-relay
|
|
105
|
-
* @param {string} [config.successHint]
|
|
100
|
+
* @param {string} config.extId extension id (used for API prefix / toolbar item id)
|
|
101
|
+
* @param {string} config.toolbarLabel toolbar button title/label (e.g. "Sign in to ZCode")
|
|
102
|
+
* @param {string} config.title dialog title
|
|
103
|
+
* @param {string} [config.urlHint] A url-only mode explanation line
|
|
104
|
+
* @param {string} [config.pasteHint] B paste-relay mode explanation line
|
|
105
|
+
* @param {string} [config.successHint] supplementary note after success (e.g. credentials-to-key hint)
|
|
106
106
|
*/
|
|
107
107
|
export function registerControlledLoginClient(ctx, config) {
|
|
108
108
|
ensureStyles();
|
|
@@ -112,7 +112,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
112
112
|
const api = (p, init) => fetch(ctx.apiUrl("/api/extensions/" + config.extId + p), { cache: "no-store", ...init });
|
|
113
113
|
const jsonInit = (body) => ({ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
114
114
|
|
|
115
|
-
// ----
|
|
115
|
+
// ---- Toolbar entry: declarative icon button (meta.icon + meta.onClick, no render — the host uniformly renders 36×36 line-icon buttons, emoji forbidden) ----
|
|
116
116
|
ctx.registerItems("toolbar", [{
|
|
117
117
|
id: `${config.extId}-controlled-login`,
|
|
118
118
|
meta: {
|
|
@@ -122,7 +122,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
122
122
|
onClick: () => openControlledLogin(),
|
|
123
123
|
},
|
|
124
124
|
}]);
|
|
125
|
-
// ----
|
|
125
|
+
// ---- Dialog entry (dialogs slot; the host wraps it in a display:contents shell, the float persistence key keys on it) ----
|
|
126
126
|
ctx.registerItems("dialogs", [{
|
|
127
127
|
id: `${config.extId}-controlled-login-dialog`,
|
|
128
128
|
render: () => h(ControlledLoginDialog),
|
|
@@ -130,9 +130,9 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
130
130
|
|
|
131
131
|
function ControlledLoginDialog() {
|
|
132
132
|
const [open, setOpen] = useState(false);
|
|
133
|
-
const [status, setStatus] = useState(null); // /login/status
|
|
133
|
+
const [status, setStatus] = useState(null); // /login/status response ({state,...})
|
|
134
134
|
const [busy, setBusy] = useState(false);
|
|
135
|
-
const [reqError, setReqError] = useState(null); //
|
|
135
|
+
const [reqError, setReqError] = useState(null); // request-layer error (network/400)
|
|
136
136
|
const [code, setCode] = useState("");
|
|
137
137
|
const [copied, setCopied] = useState(false);
|
|
138
138
|
|
|
@@ -142,7 +142,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
142
142
|
return () => { if (moduleSetOpen === setOpen) moduleSetOpen = null; };
|
|
143
143
|
}, []);
|
|
144
144
|
|
|
145
|
-
//
|
|
145
|
+
// While open, poll /login/status every 2s (§3.6 decided exception); stops on close
|
|
146
146
|
useEffect(() => {
|
|
147
147
|
if (!open) return;
|
|
148
148
|
let stopped = false;
|
|
@@ -152,7 +152,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
152
152
|
const r = await api("/login/status");
|
|
153
153
|
const d = await r.json();
|
|
154
154
|
if (!stopped) { setStatus(d); setReqError(null); }
|
|
155
|
-
} catch { /*
|
|
155
|
+
} catch { /* ignore network jitter; retry next round */ }
|
|
156
156
|
if (!stopped) timer = setTimeout(tick, 2000);
|
|
157
157
|
};
|
|
158
158
|
tick();
|
|
@@ -184,26 +184,26 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
184
184
|
async function copyUrl() {
|
|
185
185
|
const url = status?.url || "";
|
|
186
186
|
if (!url) return;
|
|
187
|
-
try { await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 1500); } catch { /*
|
|
187
|
+
try { await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 1500); } catch { /* clipboard unavailable: the URL text itself stays selectable/copyable */ }
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
// ----
|
|
190
|
+
// ---- Status area ----
|
|
191
191
|
let statusLine = null;
|
|
192
192
|
if (st === "idle") {
|
|
193
|
-
statusLine = h("div", { className: "clg-status waiting" }, "
|
|
193
|
+
statusLine = h("div", { className: "clg-status waiting" }, "No login started yet");
|
|
194
194
|
} else if (st === "waiting") {
|
|
195
195
|
statusLine = h("div", { className: "clg-status waiting" },
|
|
196
196
|
h("span", { className: "clg-spin" }),
|
|
197
|
-
status?.url ? "
|
|
198
|
-
h("button", { className: "clg-btn", disabled: busy, style: { marginLeft: "auto" }, onClick: cancelLogin }, "
|
|
197
|
+
status?.url ? "Waiting for you to complete authorization in the browser… (auto-detected on this page, within ~2s)" : "Starting the official login process, waiting for the authorization link…",
|
|
198
|
+
h("button", { className: "clg-btn", disabled: busy, style: { marginLeft: "auto" }, onClick: cancelLogin }, "Cancel"),
|
|
199
199
|
);
|
|
200
200
|
} else if (st === "success") {
|
|
201
201
|
statusLine = h("div", { className: "clg-status success" },
|
|
202
|
-
"
|
|
202
|
+
"Login successful", status?.user ? h("span", { style: { fontWeight: 700 } }, status.user) : null,
|
|
203
203
|
config.successHint ? h("span", { className: "clg-hint", style: { color: "inherit" } }, config.successHint) : null,
|
|
204
204
|
);
|
|
205
205
|
} else if (st === "failed" || st === "timeout" || st === "cancelled") {
|
|
206
|
-
const label = st === "failed" ? "
|
|
206
|
+
const label = st === "failed" ? "Login failed" : st === "timeout" ? "Login timed out" : "Cancelled";
|
|
207
207
|
statusLine = h("div", { className: "clg-status " + st },
|
|
208
208
|
h("span", null, (st === "timeout" ? "⏱ " : st === "cancelled" ? "⚪ " : "❌ ") + label),
|
|
209
209
|
status?.error ? h("span", { className: "clg-errline" }, status.error) : null,
|
|
@@ -216,52 +216,52 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
216
216
|
"data-ext-float": "1", "data-ext-float-w": 520, "data-ext-float-h": 380, "data-ext-float-full": "1",
|
|
217
217
|
onClick: (e) => e.stopPropagation(),
|
|
218
218
|
},
|
|
219
|
-
// ──
|
|
219
|
+
// ── Title bar (first child = float drag handle) ──
|
|
220
220
|
h("div", { className: "clg-head" },
|
|
221
221
|
h("span", { className: "clg-title" }, "🔑 " + config.title),
|
|
222
|
-
h("button", { className: "clg-x", title: "
|
|
222
|
+
h("button", { className: "clg-x", title: "Close", onClick: () => setOpen(false) }, "✕"),
|
|
223
223
|
),
|
|
224
224
|
h("div", { className: "clg-body" },
|
|
225
225
|
config.urlHint && !pasteMode ? h("div", { className: "clg-intro" }, config.urlHint) : null,
|
|
226
226
|
pasteMode && config.pasteHint ? h("div", { className: "clg-intro" }, config.pasteHint) : null,
|
|
227
227
|
|
|
228
|
-
// ── URL
|
|
228
|
+
// ── URL area (waiting and link captured) ──
|
|
229
229
|
st === "waiting" && status?.url
|
|
230
230
|
? h("div", { className: "clg-urlrow" },
|
|
231
|
-
h("a", { className: "clg-open", href: status.url, target: "_blank", rel: "noopener noreferrer" }, "
|
|
232
|
-
h("span", { className: "clg-url", title: "
|
|
233
|
-
copied ? h("span", { className: "clg-copied" }, "
|
|
231
|
+
h("a", { className: "clg-open", href: status.url, target: "_blank", rel: "noopener noreferrer" }, "Open authorization page"),
|
|
232
|
+
h("span", { className: "clg-url", title: "Click to copy link", onClick: copyUrl }, status.url),
|
|
233
|
+
copied ? h("span", { className: "clg-copied" }, "Copied") : null,
|
|
234
234
|
)
|
|
235
235
|
: null,
|
|
236
236
|
|
|
237
|
-
// ── B
|
|
237
|
+
// ── B mode code input (paste and waiting) ──
|
|
238
238
|
pasteMode && st === "waiting"
|
|
239
239
|
? h("div", { className: "clg-coderow" },
|
|
240
240
|
h("input", {
|
|
241
241
|
className: "clg-input",
|
|
242
|
-
placeholder: "
|
|
242
|
+
placeholder: "Paste the code shown on the authorization-completed page (or the full callback link)",
|
|
243
243
|
value: code,
|
|
244
244
|
onChange: (e) => setCode(e.target.value),
|
|
245
245
|
onKeyDown: (e) => { if (e.key === "Enter") submitCode(); },
|
|
246
246
|
}),
|
|
247
|
-
h("button", { className: "clg-btn primary", disabled: busy || !code.trim(), onClick: submitCode }, busy ? "…" : "
|
|
247
|
+
h("button", { className: "clg-btn primary", disabled: busy || !code.trim(), onClick: submitCode }, busy ? "…" : "Submit code"),
|
|
248
248
|
)
|
|
249
249
|
: null,
|
|
250
250
|
|
|
251
251
|
statusLine,
|
|
252
252
|
reqError ? h("div", { className: "clg-errline" }, reqError) : null,
|
|
253
253
|
|
|
254
|
-
// CLI
|
|
254
|
+
// When a CLI upgrade changes the output format and URL capture fails: surface the raw text (human-readable and copyable, plan §7)
|
|
255
255
|
(st === "failed" || st === "timeout") && status?.outputTail
|
|
256
256
|
? h("div", { className: "clg-tail" }, status.outputTail)
|
|
257
257
|
: null,
|
|
258
258
|
|
|
259
|
-
// ──
|
|
259
|
+
// ── Footer actions: idle/terminal states get a start/(re)try button ──
|
|
260
260
|
h("div", { className: "clg-foot" },
|
|
261
261
|
st === "idle" || st === "failed" || st === "timeout" || st === "cancelled" || st === "success"
|
|
262
|
-
? h("button", { className: "clg-btn primary", disabled: busy, onClick: startLogin }, busy ? "…" : st === "idle" ? "
|
|
262
|
+
? h("button", { className: "clg-btn primary", disabled: busy, onClick: startLogin }, busy ? "…" : st === "idle" ? "Start login" : "Retry")
|
|
263
263
|
: null,
|
|
264
|
-
h("span", { className: "clg-hint" }, "
|
|
264
|
+
h("span", { className: "clg-hint" }, "Authorization completes in the browser; credentials are persisted by the official CLI. Closing this page does not affect a login in progress."),
|
|
265
265
|
),
|
|
266
266
|
),
|
|
267
267
|
),
|
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* controlled-login —
|
|
2
|
+
* controlled-login — External agent controlled login standard · unified frontend dialog (native h() rendering, zero build step).
|
|
3
3
|
*
|
|
4
|
-
* ⚠
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* ⚠ Copy sync (byte-level copies; changes must be synced to all copies; test `_shared/controlled-login.test.mjs` pins consistency).
|
|
5
|
+
* Authoritative source: pi-web-ext/_shared/controlled-login-dialog.mjs
|
|
6
|
+
* Deployed copies (client side imports relative to the deployment tree — a zero-build client cannot inline ../../_shared
|
|
7
|
+
* at build time the way a vite client can; the asset route rejects traversal above the tree; same convention as draft-keeper's ./core.mjs):
|
|
8
8
|
* pi-web-ext/aiti-{zcode,cursor,qoder,claude-code,codex}-agent/client/controlled-login-dialog.mjs
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* +
|
|
13
|
-
* + B
|
|
14
|
-
*
|
|
15
|
-
*
|
|
10
|
+
* Contract (docs/ext-controlled-login-standard.md §3.5):
|
|
11
|
+
* 🔗 "Open authorization page" button (target=_blank) + copyable URL text + mode explanation line
|
|
12
|
+
* + status area (waiting spinner / success ✅ username / failed / timeout / cancelled, all with retry)
|
|
13
|
+
* + B mode (paste)-only code input + submit.
|
|
14
|
+
* Status refresh: while the dialog is open, poll GET /login/status every 2s; stops on close
|
|
15
|
+
* (the polling exception argued and decided in plan §3.6: login is a minutes-scale one-shot interaction).
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
17
|
+
* Float contract: panel root node data-ext-float="1" + data-ext-float-w/-h + data-ext-float-full="1"
|
|
18
|
+
* (aiti-float takes over dragging/resizing/persistence/small-screen fullscreen; without it, its own style centers as a fallback).
|
|
19
19
|
*
|
|
20
|
-
*
|
|
20
|
+
* Wiring (each extension's client/controlled-login-client.mjs, two lines):
|
|
21
21
|
* import { registerControlledLoginClient } from "./controlled-login-dialog.mjs";
|
|
22
22
|
* export default function register(ctx) { registerControlledLoginClient(ctx, { … }); }
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
// ----------
|
|
25
|
+
// ---------- Module-scoped open state (shared by the toolbar entry and the dialogs entry within one module; never a window global) ----------
|
|
26
26
|
let moduleSetOpen = null;
|
|
27
|
-
//
|
|
28
|
-
//
|
|
27
|
+
// Under lazy loading (clientImmediately:false) the dialog component may not be mounted yet when the toolbar static entry is clicked:
|
|
28
|
+
// record pending; the mount effect consumes it — avoids a dead first click.
|
|
29
29
|
let pendingOpen = false;
|
|
30
30
|
|
|
31
|
-
/**
|
|
31
|
+
/** Opens this extension's controlled login dialog (for the toolbar 🔑 button / static entry meta.onClick). */
|
|
32
32
|
export function openControlledLogin() {
|
|
33
33
|
if (moduleSetOpen) moduleSetOpen(true);
|
|
34
34
|
else pendingOpen = true;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
// ----------
|
|
37
|
+
// ---------- Self-contained style layer (idempotent injection; clg- prefix + var(--x, fallback), zero host coupling) ----------
|
|
38
38
|
const STYLE_ID = "aiti-controlled-login-styles";
|
|
39
39
|
const STYLES = `
|
|
40
40
|
.clg-mask { position: fixed; inset: 0; z-index: 2147483000; background: rgba(0,0,0,.32); display: flex; align-items: center; justify-content: center; }
|
|
@@ -93,16 +93,16 @@ function ensureStyles() {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
|
-
*
|
|
96
|
+
* Unified dialog registration (called once from each extension client's thin shell).
|
|
97
97
|
*
|
|
98
|
-
* @param {*} ctx
|
|
98
|
+
* @param {*} ctx host client context (h/useState/useEffect/apiUrl/registerItems)
|
|
99
99
|
* @param {object} config
|
|
100
|
-
* @param {string} config.extId
|
|
101
|
-
* @param {string} config.toolbarLabel
|
|
102
|
-
* @param {string} config.title
|
|
103
|
-
* @param {string} [config.urlHint] A url-only
|
|
104
|
-
* @param {string} [config.pasteHint] B paste-relay
|
|
105
|
-
* @param {string} [config.successHint]
|
|
100
|
+
* @param {string} config.extId extension id (used for API prefix / toolbar item id)
|
|
101
|
+
* @param {string} config.toolbarLabel toolbar button title/label (e.g. "Sign in to ZCode")
|
|
102
|
+
* @param {string} config.title dialog title
|
|
103
|
+
* @param {string} [config.urlHint] A url-only mode explanation line
|
|
104
|
+
* @param {string} [config.pasteHint] B paste-relay mode explanation line
|
|
105
|
+
* @param {string} [config.successHint] supplementary note after success (e.g. credentials-to-key hint)
|
|
106
106
|
*/
|
|
107
107
|
export function registerControlledLoginClient(ctx, config) {
|
|
108
108
|
ensureStyles();
|
|
@@ -112,7 +112,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
112
112
|
const api = (p, init) => fetch(ctx.apiUrl("/api/extensions/" + config.extId + p), { cache: "no-store", ...init });
|
|
113
113
|
const jsonInit = (body) => ({ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
114
114
|
|
|
115
|
-
// ----
|
|
115
|
+
// ---- Toolbar entry: declarative icon button (meta.icon + meta.onClick, no render — the host uniformly renders 36×36 line-icon buttons, emoji forbidden) ----
|
|
116
116
|
ctx.registerItems("toolbar", [{
|
|
117
117
|
id: `${config.extId}-controlled-login`,
|
|
118
118
|
meta: {
|
|
@@ -122,7 +122,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
122
122
|
onClick: () => openControlledLogin(),
|
|
123
123
|
},
|
|
124
124
|
}]);
|
|
125
|
-
// ----
|
|
125
|
+
// ---- Dialog entry (dialogs slot; the host wraps it in a display:contents shell, the float persistence key keys on it) ----
|
|
126
126
|
ctx.registerItems("dialogs", [{
|
|
127
127
|
id: `${config.extId}-controlled-login-dialog`,
|
|
128
128
|
render: () => h(ControlledLoginDialog),
|
|
@@ -130,9 +130,9 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
130
130
|
|
|
131
131
|
function ControlledLoginDialog() {
|
|
132
132
|
const [open, setOpen] = useState(false);
|
|
133
|
-
const [status, setStatus] = useState(null); // /login/status
|
|
133
|
+
const [status, setStatus] = useState(null); // /login/status response ({state,...})
|
|
134
134
|
const [busy, setBusy] = useState(false);
|
|
135
|
-
const [reqError, setReqError] = useState(null); //
|
|
135
|
+
const [reqError, setReqError] = useState(null); // request-layer error (network/400)
|
|
136
136
|
const [code, setCode] = useState("");
|
|
137
137
|
const [copied, setCopied] = useState(false);
|
|
138
138
|
|
|
@@ -142,7 +142,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
142
142
|
return () => { if (moduleSetOpen === setOpen) moduleSetOpen = null; };
|
|
143
143
|
}, []);
|
|
144
144
|
|
|
145
|
-
//
|
|
145
|
+
// While open, poll /login/status every 2s (§3.6 decided exception); stops on close
|
|
146
146
|
useEffect(() => {
|
|
147
147
|
if (!open) return;
|
|
148
148
|
let stopped = false;
|
|
@@ -152,7 +152,7 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
152
152
|
const r = await api("/login/status");
|
|
153
153
|
const d = await r.json();
|
|
154
154
|
if (!stopped) { setStatus(d); setReqError(null); }
|
|
155
|
-
} catch { /*
|
|
155
|
+
} catch { /* ignore network jitter; retry next round */ }
|
|
156
156
|
if (!stopped) timer = setTimeout(tick, 2000);
|
|
157
157
|
};
|
|
158
158
|
tick();
|
|
@@ -184,26 +184,26 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
184
184
|
async function copyUrl() {
|
|
185
185
|
const url = status?.url || "";
|
|
186
186
|
if (!url) return;
|
|
187
|
-
try { await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 1500); } catch { /*
|
|
187
|
+
try { await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 1500); } catch { /* clipboard unavailable: the URL text itself stays selectable/copyable */ }
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
// ----
|
|
190
|
+
// ---- Status area ----
|
|
191
191
|
let statusLine = null;
|
|
192
192
|
if (st === "idle") {
|
|
193
|
-
statusLine = h("div", { className: "clg-status waiting" }, "
|
|
193
|
+
statusLine = h("div", { className: "clg-status waiting" }, "No login started yet");
|
|
194
194
|
} else if (st === "waiting") {
|
|
195
195
|
statusLine = h("div", { className: "clg-status waiting" },
|
|
196
196
|
h("span", { className: "clg-spin" }),
|
|
197
|
-
status?.url ? "
|
|
198
|
-
h("button", { className: "clg-btn", disabled: busy, style: { marginLeft: "auto" }, onClick: cancelLogin }, "
|
|
197
|
+
status?.url ? "Waiting for you to complete authorization in the browser… (auto-detected on this page, within ~2s)" : "Starting the official login process, waiting for the authorization link…",
|
|
198
|
+
h("button", { className: "clg-btn", disabled: busy, style: { marginLeft: "auto" }, onClick: cancelLogin }, "Cancel"),
|
|
199
199
|
);
|
|
200
200
|
} else if (st === "success") {
|
|
201
201
|
statusLine = h("div", { className: "clg-status success" },
|
|
202
|
-
"
|
|
202
|
+
"Login successful", status?.user ? h("span", { style: { fontWeight: 700 } }, status.user) : null,
|
|
203
203
|
config.successHint ? h("span", { className: "clg-hint", style: { color: "inherit" } }, config.successHint) : null,
|
|
204
204
|
);
|
|
205
205
|
} else if (st === "failed" || st === "timeout" || st === "cancelled") {
|
|
206
|
-
const label = st === "failed" ? "
|
|
206
|
+
const label = st === "failed" ? "Login failed" : st === "timeout" ? "Login timed out" : "Cancelled";
|
|
207
207
|
statusLine = h("div", { className: "clg-status " + st },
|
|
208
208
|
h("span", null, (st === "timeout" ? "⏱ " : st === "cancelled" ? "⚪ " : "❌ ") + label),
|
|
209
209
|
status?.error ? h("span", { className: "clg-errline" }, status.error) : null,
|
|
@@ -216,52 +216,52 @@ export function registerControlledLoginClient(ctx, config) {
|
|
|
216
216
|
"data-ext-float": "1", "data-ext-float-w": 520, "data-ext-float-h": 380, "data-ext-float-full": "1",
|
|
217
217
|
onClick: (e) => e.stopPropagation(),
|
|
218
218
|
},
|
|
219
|
-
// ──
|
|
219
|
+
// ── Title bar (first child = float drag handle) ──
|
|
220
220
|
h("div", { className: "clg-head" },
|
|
221
221
|
h("span", { className: "clg-title" }, "🔑 " + config.title),
|
|
222
|
-
h("button", { className: "clg-x", title: "
|
|
222
|
+
h("button", { className: "clg-x", title: "Close", onClick: () => setOpen(false) }, "✕"),
|
|
223
223
|
),
|
|
224
224
|
h("div", { className: "clg-body" },
|
|
225
225
|
config.urlHint && !pasteMode ? h("div", { className: "clg-intro" }, config.urlHint) : null,
|
|
226
226
|
pasteMode && config.pasteHint ? h("div", { className: "clg-intro" }, config.pasteHint) : null,
|
|
227
227
|
|
|
228
|
-
// ── URL
|
|
228
|
+
// ── URL area (waiting and link captured) ──
|
|
229
229
|
st === "waiting" && status?.url
|
|
230
230
|
? h("div", { className: "clg-urlrow" },
|
|
231
|
-
h("a", { className: "clg-open", href: status.url, target: "_blank", rel: "noopener noreferrer" }, "
|
|
232
|
-
h("span", { className: "clg-url", title: "
|
|
233
|
-
copied ? h("span", { className: "clg-copied" }, "
|
|
231
|
+
h("a", { className: "clg-open", href: status.url, target: "_blank", rel: "noopener noreferrer" }, "Open authorization page"),
|
|
232
|
+
h("span", { className: "clg-url", title: "Click to copy link", onClick: copyUrl }, status.url),
|
|
233
|
+
copied ? h("span", { className: "clg-copied" }, "Copied") : null,
|
|
234
234
|
)
|
|
235
235
|
: null,
|
|
236
236
|
|
|
237
|
-
// ── B
|
|
237
|
+
// ── B mode code input (paste and waiting) ──
|
|
238
238
|
pasteMode && st === "waiting"
|
|
239
239
|
? h("div", { className: "clg-coderow" },
|
|
240
240
|
h("input", {
|
|
241
241
|
className: "clg-input",
|
|
242
|
-
placeholder: "
|
|
242
|
+
placeholder: "Paste the code shown on the authorization-completed page (or the full callback link)",
|
|
243
243
|
value: code,
|
|
244
244
|
onChange: (e) => setCode(e.target.value),
|
|
245
245
|
onKeyDown: (e) => { if (e.key === "Enter") submitCode(); },
|
|
246
246
|
}),
|
|
247
|
-
h("button", { className: "clg-btn primary", disabled: busy || !code.trim(), onClick: submitCode }, busy ? "…" : "
|
|
247
|
+
h("button", { className: "clg-btn primary", disabled: busy || !code.trim(), onClick: submitCode }, busy ? "…" : "Submit code"),
|
|
248
248
|
)
|
|
249
249
|
: null,
|
|
250
250
|
|
|
251
251
|
statusLine,
|
|
252
252
|
reqError ? h("div", { className: "clg-errline" }, reqError) : null,
|
|
253
253
|
|
|
254
|
-
// CLI
|
|
254
|
+
// When a CLI upgrade changes the output format and URL capture fails: surface the raw text (human-readable and copyable, plan §7)
|
|
255
255
|
(st === "failed" || st === "timeout") && status?.outputTail
|
|
256
256
|
? h("div", { className: "clg-tail" }, status.outputTail)
|
|
257
257
|
: null,
|
|
258
258
|
|
|
259
|
-
// ──
|
|
259
|
+
// ── Footer actions: idle/terminal states get a start/(re)try button ──
|
|
260
260
|
h("div", { className: "clg-foot" },
|
|
261
261
|
st === "idle" || st === "failed" || st === "timeout" || st === "cancelled" || st === "success"
|
|
262
|
-
? h("button", { className: "clg-btn primary", disabled: busy, onClick: startLogin }, busy ? "…" : st === "idle" ? "
|
|
262
|
+
? h("button", { className: "clg-btn primary", disabled: busy, onClick: startLogin }, busy ? "…" : st === "idle" ? "Start login" : "Retry")
|
|
263
263
|
: null,
|
|
264
|
-
h("span", { className: "clg-hint" }, "
|
|
264
|
+
h("span", { className: "clg-hint" }, "Authorization completes in the browser; credentials are persisted by the official CLI. Closing this page does not affect a login in progress."),
|
|
265
265
|
),
|
|
266
266
|
),
|
|
267
267
|
),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiti-ai/aiti-codex-agent",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "OpenAI Codex CLI driver — exec --json + resume integrated into All Sessions; dual-track auth (auth.json OAuth / openrouter env), live streaming and cold replay render identically-shaped native tool cards",
|
|
5
5
|
"displayNameI18n": {
|
|
6
6
|
"en": "Codex Session Backend"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@aiti-ai/ext-shared": "^0.1.12"
|
|
27
27
|
},
|
|
28
28
|
"license": "MIT",
|
|
29
|
-
"contentHash": "
|
|
29
|
+
"contentHash": "afbd46584f3305aa",
|
|
30
30
|
"repository": {
|
|
31
31
|
"type": "git",
|
|
32
32
|
"url": "git+https://github.com/aiti-ai/aiti-ai.git",
|
package/tools/deploy.mjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { cpSync, mkdirSync, existsSync } from "node:fs";
|
|
4
4
|
import { join, dirname, resolve } from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { notifyReconcile } from "
|
|
6
|
+
import { notifyReconcile } from "@aiti-ai/ext-shared/reconcile-deps";
|
|
7
7
|
|
|
8
8
|
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
9
9
|
// 目标:默认双写 web + web-dev;--prod 仅 prod、--dev 仅 dev(向后兼容旧单边用法)
|
|
@@ -30,7 +30,7 @@ await notifyReconcile();
|
|
|
30
30
|
|
|
31
31
|
// ── 双实例热重载(basePath 双 env 按机器设值)——允许失败:服务未启动/共享模块缺失仅提示,不影响 deploy 退出码 ──
|
|
32
32
|
try {
|
|
33
|
-
const { reloadInstances } = await import("
|
|
33
|
+
const { reloadInstances } = await import("@aiti-ai/ext-shared/reload-instances");
|
|
34
34
|
await reloadInstances();
|
|
35
35
|
} catch (e) {
|
|
36
36
|
console.log("[deploy] 自动 reload 未执行(" + String((e && e.message) || e) + ")— 需手动 POST /api/extensions/reload");
|
|
@@ -38,7 +38,7 @@ try {
|
|
|
38
38
|
|
|
39
39
|
// ── dev-pack(方案 §3.2:源码+构建工具+AGENTS.md 直达部署目录,AI 可现场直改)——允许失败,不影响 deploy 退出码 ──
|
|
40
40
|
try {
|
|
41
|
-
const { packDevPack } = await import("
|
|
41
|
+
const { packDevPack } = await import("@aiti-ai/ext-shared/dev-pack");
|
|
42
42
|
packDevPack({ extRoot: resolve(import.meta.dirname, ".."), log: (m) => console.log("[deploy] " + m) });
|
|
43
43
|
} catch (e) {
|
|
44
44
|
console.log("[deploy] dev-pack 未执行(" + String((e && e.message) || e) + ")");
|
package/dev-pack.mjs
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* pi-web-ext · dev-pack 共享实现(方案 §3.2 / M5:扩展 dev-pack——src + 构建工具 + AGENTS.md 直达部署目录)
|
|
3
|
-
*
|
|
4
|
-
* 目标:用户在 AI 窗对 AI 说「帮我改一下 xxx 扩展」时,AI 能在**部署目录**直接:
|
|
5
|
-
* 读 AGENTS.md → 改 src/ → node tools/build.mjs → curl reload → 浏览器生效,不等回开发机。
|
|
6
|
-
*
|
|
7
|
-
* 各扩展 tools/deploy.mjs 末尾统一追加一段(见本文件同级各 deploy.mjs 模式):
|
|
8
|
-
* const { packDevPack } = await import("../../_shared/dev-pack.mjs");
|
|
9
|
-
* packDevPack({ extRoot: <扩展源码根>, log: console.log });
|
|
10
|
-
*
|
|
11
|
-
* packDevPack 向每个已存在的部署目录(~/.pi/web{,-dev}/extensions/<extId>)写入:
|
|
12
|
-
* AGENTS.md 源码目录有则拷,无则由模板生成(§3.4)
|
|
13
|
-
* src/ client/src/**、根级服务端源码 *.ts/*.cjs/*.mjs、lib/(跳 vendor)、server/、sched/、worker-src/
|
|
14
|
-
* tools/build.mjs 现场构建入口(扩展自带则拷真身,否则生成 esbuild 默认版)
|
|
15
|
-
* tools/deploy.mjs 部署脚本本身(大改后回源码仓重部署的入口)
|
|
16
|
-
* package.json 含 aitiConfigs 声明与 build 脚本
|
|
17
|
-
* vite.config.ts 有则拷
|
|
18
|
-
*
|
|
19
|
-
* 幂等:重复部署整目录覆盖;允许失败:任何异常只 log 不影响 deploy 退出码(调用方 try/catch)。
|
|
20
|
-
*/
|
|
21
|
-
"use strict";
|
|
22
|
-
import { homedir } from "node:os";
|
|
23
|
-
import { join, resolve, dirname, basename, sep as pathSep } from "node:path";
|
|
24
|
-
import {
|
|
25
|
-
existsSync, readdirSync, statSync, mkdirSync, cpSync, rmSync, writeFileSync, readFileSync,
|
|
26
|
-
} from "node:fs";
|
|
27
|
-
|
|
28
|
-
// 根级服务端源码文件:拷进 src/(运行时平铺文件在部署根不动,宿主 loader 路径不变)
|
|
29
|
-
const ROOT_SRC_EXTS = new Set([".ts", ".tsx", ".cjs", ".mjs"]);
|
|
30
|
-
const ROOT_SRC_SKIP = new Set(["dist", "node_modules"]);
|
|
31
|
-
// 源码子目录:整目录拷(vendor 类大资产跳过——部署目录本就有运行时副本)
|
|
32
|
-
const SRC_DIRS = ["client/src", "server", "lib", "sched", "worker-src"];
|
|
33
|
-
const DIR_SKIP = new Set(["vendor", "__pycache__", "node_modules"]);
|
|
34
|
-
|
|
35
|
-
function copyTree(src, dest, skip) {
|
|
36
|
-
mkdirSync(dest, { recursive: true });
|
|
37
|
-
for (const en of readdirSync(src, { withFileTypes: true })) {
|
|
38
|
-
if (skip && skip.has(en.name)) continue;
|
|
39
|
-
const s = join(src, en.name), d = join(dest, en.name);
|
|
40
|
-
if (en.isDirectory()) copyTree(s, d, skip);
|
|
41
|
-
else if (en.isFile()) cpSync(s, d);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** AGENTS.md 默认模板(§3.4;扩展自带 AGENTS.md 则不走到这里)。供 CLI 批量生成源码目录版复用。 */
|
|
46
|
-
export function agentsMdTemplate(extId) {
|
|
47
|
-
return `# ${extId} 扩展(部署目录直改指南)
|
|
48
|
-
|
|
49
|
-
## 结构
|
|
50
|
-
- 运行时文件平铺在本目录根(index.ts / core.cjs …,宿主 loader 按相对路径加载——**禁手改运行时平铺文件**,改 src/ 再 build)
|
|
51
|
-
- \`src/\` 源码(client/src + 服务端 ts/cjs)· \`dist/\` 构建产物(禁手改)· \`tools/\` 构建/部署脚本
|
|
52
|
-
- npm 包形态的扩展:本目录 package.json 含 version 与 aitiConfigs 声明
|
|
53
|
-
|
|
54
|
-
## 小改现场流程(改文案/样式/小逻辑)
|
|
55
|
-
1. 改 \`src/\` 下对应源码
|
|
56
|
-
2. \`node tools/build.mjs\`(client 有构建产物的扩展;纯服务端改动跳过)
|
|
57
|
-
3. \`curl -X POST http://127.0.0.1:3001/api/extensions/reload\`(dev;prod 3002;外部经 nginx 带 basePath:/dev、/mac、/mdev、/win、/wdev → \`/xxx/api/extensions/reload\`)
|
|
58
|
-
4. 浏览器刷新页面验证
|
|
59
|
-
|
|
60
|
-
## 大改回源码仓
|
|
61
|
-
- 源码仓:\`extensions/${extId}/\`(git 管理,deploy 同步回本目录)
|
|
62
|
-
- 改完在源码仓跑 \`node tools/deploy.mjs\`(或仓库根 \`node scripts/deploy-all.mjs --only ${extId}\`)
|
|
63
|
-
|
|
64
|
-
## 数据目录(数据分层:config / cache)
|
|
65
|
-
- \`ctx.configDir\` 根级 = 配置与关键小状态(aiti-configs 备份范围;小、珍贵、人可读)
|
|
66
|
-
- \`ctx.cacheDir\`(= \`<configDir>/cache/\`)= 可删缓存:db/venv/日志/大文件;rm -rf 后扩展下次访问自动重建
|
|
67
|
-
- \`ctx.dataDir\` 已移除(访问即抛)——新代码禁用
|
|
68
|
-
- 本扩展白名单(永不为 cache 的配置文件)见 package.json \`aitiConfigs.configFiles\`
|
|
69
|
-
|
|
70
|
-
## 红线
|
|
71
|
-
- 禁轮询(setInterval 拉取);需要等待用事件/回调/SSE(桥的 SSE、aiti-event-bus topics)
|
|
72
|
-
- 禁重启生产服务(pi-web 3002 等 restart 须用户明确同意)
|
|
73
|
-
- 禁手改 dist/ 与运行时平铺文件;跨扩展通信走 ctx.services(禁 globalThis.__ext 直穿)
|
|
74
|
-
- 服务端:SQLite 一律 WAL + 分批小事务(批间 setImmediate 让步);禁 execFileSync 调 >1s 外部程序;>8MB 文件禁同步读
|
|
75
|
-
`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** 默认 tools/build.mjs(esbuild 单包零配置;扩展自带 tools/build.mjs 则不走到这里)。 */
|
|
79
|
-
function buildMjsTemplate(extId, extRoot, clientOut, entryRel) {
|
|
80
|
-
// No hardcoded checkout path: candidates are resolved at run time (see repoCandidates below).
|
|
81
|
-
return `/**
|
|
82
|
-
* ${extId} · 现场构建入口(dev-pack 默认模板,esbuild 单包零配置)
|
|
83
|
-
* 用法:node tools/build.mjs
|
|
84
|
-
* 本文件由 dev-pack 生成;复杂构建(多入口/插件链)请参照 src/vite.config.ts 改写本文件或回源码仓构建。
|
|
85
|
-
*/
|
|
86
|
-
"use strict";
|
|
87
|
-
import { existsSync, readdirSync, constants as fsConstants, accessSync } from "node:fs";
|
|
88
|
-
import { join, resolve, dirname, basename, sep as pathSep } from "node:path";
|
|
89
|
-
import { fileURLToPath } from "node:url";
|
|
90
|
-
import { spawnSync } from "node:child_process";
|
|
91
|
-
|
|
92
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
93
|
-
const extRoot = resolve(here, "..");
|
|
94
|
-
const clientOut = ${JSON.stringify(clientOut || "dist/client-bundle.mjs")};
|
|
95
|
-
const entryRel = ${JSON.stringify(entryRel || "")};
|
|
96
|
-
|
|
97
|
-
// esbuild 解析:部署目录 node_modules → 源码仓 node_modules(本机开发场景)→ PATH 上的 esbuild → npm exec 现拉。
|
|
98
|
-
// 注意:node_modules/esbuild/bin/esbuild 是原生二进制(ELF/Mach-O),必须直接执行(不能当 JS 跑)。
|
|
99
|
-
// Checkout roots are discovered by walking up from the extension dir (a root has node_modules/),
|
|
100
|
-
// plus AITI_REPO_ROOT when set — no machine-specific absolute paths baked into generated files.
|
|
101
|
-
function repoCandidates() {
|
|
102
|
-
const out = [];
|
|
103
|
-
if (process.env.AITI_REPO_ROOT) out.push(process.env.AITI_REPO_ROOT.split(pathSep).join("/") + "/node_modules/esbuild/bin/esbuild");
|
|
104
|
-
let d = extRoot;
|
|
105
|
-
for (let i = 0; i < 8; i++) {
|
|
106
|
-
out.push(d.split(pathSep).join("/") + "/node_modules/esbuild/bin/esbuild");
|
|
107
|
-
const up = dirname(d);
|
|
108
|
-
if (up === d) break;
|
|
109
|
-
d = up;
|
|
110
|
-
}
|
|
111
|
-
return out;
|
|
112
|
-
}
|
|
113
|
-
function findEsbuild() {
|
|
114
|
-
const home = (process.env.HOME || process.env.USERPROFILE || "").split(pathSep).join("/");
|
|
115
|
-
const candidates = [
|
|
116
|
-
extRoot + "/node_modules/esbuild/bin/esbuild",
|
|
117
|
-
...repoCandidates(),
|
|
118
|
-
];
|
|
119
|
-
for (const c of candidates) {
|
|
120
|
-
if (!existsSync(c)) continue;
|
|
121
|
-
try { accessSync(c, fsConstants.X_OK); return [c]; } catch (e) { /* 无执行位 → 下一个来源 */ }
|
|
122
|
-
}
|
|
123
|
-
const r = spawnSync("esbuild", ["--version"], { encoding: "utf8" });
|
|
124
|
-
if (r.status === 0) return ["esbuild"];
|
|
125
|
-
return ["npm", "exec", "--yes", "esbuild", "--"];
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function pickEntry() {
|
|
129
|
-
if (entryRel) return entryRel;
|
|
130
|
-
const srcDir = join(extRoot, "src", "client", "src");
|
|
131
|
-
if (!existsSync(srcDir)) throw new Error("找不到 client 源码目录 src/client/src");
|
|
132
|
-
for (const name of ["entry.tsx", "entry.ts", "entry.jsx", "entry.js", "index.tsx", "index.ts"]) {
|
|
133
|
-
if (existsSync(join(srcDir, name))) return "client/src/" + name;
|
|
134
|
-
}
|
|
135
|
-
const first = readdirSync(srcDir).find((f) => /\\.(tsx?|jsx?)$/.test(f));
|
|
136
|
-
if (!first) throw new Error("client/src 下没有可构建入口");
|
|
137
|
-
return "client/src/" + first;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// 服务端源码同步:src/ 根级文件(index.ts/core.cjs/package.json/vite.config.ts 等)→ 部署根
|
|
141
|
-
// (服务端没有构建产物——jiti 直接加载部署根文件;不 sync 则「改 src → reload」永远不生效)
|
|
142
|
-
import { readdirSync as _rd, copyFileSync as _cp, statSync as _st } from "node:fs";
|
|
143
|
-
const srcRoot = join(extRoot, "src");
|
|
144
|
-
if (existsSync(srcRoot)) {
|
|
145
|
-
for (const f of _rd(srcRoot)) {
|
|
146
|
-
const fp = join(srcRoot, f);
|
|
147
|
-
if (!_st(fp).isFile()) continue;
|
|
148
|
-
_cp(fp, join(extRoot, f));
|
|
149
|
-
}
|
|
150
|
-
console.log("[build] 服务端源码已从 src/ 同步到部署根");
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const entry = pickEntry();
|
|
154
|
-
const args = [join(extRoot, "src", entry), "--bundle", "--format=esm",
|
|
155
|
-
"--outfile=" + join(extRoot, clientOut), "--jsx=automatic"];
|
|
156
|
-
// pi-web host-shim 委托(react → 本地 shim;jsx-runtime 同理)——与各扩展 vite.config 同款
|
|
157
|
-
const shim = join(extRoot, "src", "client", "src", "host-shim.ts");
|
|
158
|
-
const jsxRuntime = ["jsx-runtime.ts", "jsx-runtime.tsx", "jsx-dev-runtime.ts"].map((f) => join(extRoot, "src", "client", "src", f)).find(existsSync);
|
|
159
|
-
if (existsSync(shim)) args.push("--alias:react=" + shim);
|
|
160
|
-
if (jsxRuntime) args.push("--alias:react/jsx-runtime=" + jsxRuntime);
|
|
161
|
-
args.push("--log-level=info");
|
|
162
|
-
|
|
163
|
-
const [cmd, ...pre] = findEsbuild();
|
|
164
|
-
console.log("[build] esbuild:", cmd, ...pre);
|
|
165
|
-
const r = spawnSync(cmd, [...pre, ...args], { stdio: "inherit", cwd: extRoot });
|
|
166
|
-
if (r.status !== 0) { console.error("[build] 失败——可参照 src/vite.config.ts 调整本文件后重试,或回源码仓构建"); process.exit(r.status ?? 1); }
|
|
167
|
-
console.log("[build] ✓ " + clientOut + "(reload 后浏览器刷新生效)");
|
|
168
|
-
`;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* 主入口:向已存在的部署目录写 dev-pack。
|
|
173
|
-
* @param {object} opts
|
|
174
|
-
* @param {string} opts.extRoot 扩展源码根(pi-web-ext/<extId>)
|
|
175
|
-
* @param {string[]} [opts.targets] 显式部署目录;缺省 = ~/.pi/web{,-dev}/extensions/<extId>(存在的才写)
|
|
176
|
-
* @param {(m: string) => void} [opts.log]
|
|
177
|
-
*/
|
|
178
|
-
export function packDevPack(opts) {
|
|
179
|
-
const extRoot = resolve(opts.extRoot);
|
|
180
|
-
const extId = basename(extRoot);
|
|
181
|
-
const log = opts.log || (() => {});
|
|
182
|
-
const targets = (opts.targets || [
|
|
183
|
-
join(homedir(), ".pi", "web", "extensions", extId),
|
|
184
|
-
join(homedir(), ".pi", "web-dev", "extensions", extId),
|
|
185
|
-
]).filter((t) => existsSync(t));
|
|
186
|
-
if (!targets.length) return;
|
|
187
|
-
|
|
188
|
-
// 1) 打包源码到临时区(staging)→ 逐目标 cpSync(多部署目录零重复扫描)
|
|
189
|
-
const stage = join(extRoot, "dist", ".dev-pack-staging");
|
|
190
|
-
rmSync(stage, { recursive: true, force: true });
|
|
191
|
-
mkdirSync(stage, { recursive: true });
|
|
192
|
-
try {
|
|
193
|
-
// src/:根级源码文件 + 源码子目录
|
|
194
|
-
const srcDir = join(stage, "src");
|
|
195
|
-
mkdirSync(srcDir, { recursive: true });
|
|
196
|
-
for (const en of readdirSync(extRoot, { withFileTypes: true })) {
|
|
197
|
-
if (en.isDirectory()) continue;
|
|
198
|
-
if (ROOT_SRC_SKIP.has(en.name)) continue;
|
|
199
|
-
if (!ROOT_SRC_EXTS.has(en.name.slice(en.name.lastIndexOf(".") || 0)) && en.name !== "package.json") continue;
|
|
200
|
-
cpSync(join(extRoot, en.name), join(srcDir, en.name));
|
|
201
|
-
}
|
|
202
|
-
for (const d of SRC_DIRS) {
|
|
203
|
-
const s = join(extRoot, d);
|
|
204
|
-
if (!existsSync(s)) continue;
|
|
205
|
-
copyTree(s, join(srcDir, ...d.split("/")), DIR_SKIP);
|
|
206
|
-
}
|
|
207
|
-
for (const f of ["vite.config.ts", "package.json"]) {
|
|
208
|
-
if (existsSync(join(extRoot, f))) cpSync(join(extRoot, f), join(srcDir, f));
|
|
209
|
-
}
|
|
210
|
-
// tools/build.mjs:扩展自带拷真身,否则生成默认 esbuild 版
|
|
211
|
-
mkdirSync(join(stage, "tools"), { recursive: true });
|
|
212
|
-
if (existsSync(join(extRoot, "tools", "build.mjs"))) {
|
|
213
|
-
cpSync(join(extRoot, "tools", "build.mjs"), join(stage, "tools", "build.mjs"));
|
|
214
|
-
} else {
|
|
215
|
-
let clientOut = "";
|
|
216
|
-
try {
|
|
217
|
-
const pkg = JSON.parse(readFileSync(join(extRoot, "package.json"), "utf8"));
|
|
218
|
-
if (pkg.client && String(pkg.client).startsWith("dist/")) clientOut = pkg.client;
|
|
219
|
-
} catch (e) {}
|
|
220
|
-
if (!clientOut) {
|
|
221
|
-
// 回退:client 产物名常声明在 entry 的 client: "dist/<id>-client.mjs"(index.ts)——从 staging 的 src 里解析
|
|
222
|
-
try {
|
|
223
|
-
for (const f of readdirSync(srcDir)) {
|
|
224
|
-
if (!/\.tsx?$/.test(f)) continue;
|
|
225
|
-
const m = readFileSync(join(srcDir, f), "utf8").match(/client\s*:\s*["'`](dist\/[\w.-]+\.mjs)["'`]/);
|
|
226
|
-
if (m) { clientOut = m[1]; break; }
|
|
227
|
-
}
|
|
228
|
-
} catch (e) {}
|
|
229
|
-
}
|
|
230
|
-
writeFileSync(join(stage, "tools", "build.mjs"), buildMjsTemplate(extId, extRoot, clientOut, ""));
|
|
231
|
-
}
|
|
232
|
-
// tools/deploy.mjs 本身
|
|
233
|
-
if (existsSync(join(extRoot, "tools", "deploy.mjs"))) {
|
|
234
|
-
cpSync(join(extRoot, "tools", "deploy.mjs"), join(stage, "tools", "deploy.mjs"));
|
|
235
|
-
}
|
|
236
|
-
// AGENTS.md:自带优先,否则模板生成
|
|
237
|
-
if (existsSync(join(extRoot, "AGENTS.md"))) cpSync(join(extRoot, "AGENTS.md"), join(stage, "AGENTS.md"));
|
|
238
|
-
else writeFileSync(join(stage, "AGENTS.md"), agentsMdTemplate(extId));
|
|
239
|
-
|
|
240
|
-
// 1.5) _shared 依赖:部署 src 里的源码可能相对引用 ../../../_shared/*(源仓出仓路径)——
|
|
241
|
-
// 部署根放一份 _shared(小体量、纯源码),让现场 build 的模块解析与源仓一致
|
|
242
|
-
const sharedSrc = resolve(extRoot, "..", "_shared");
|
|
243
|
-
if (existsSync(sharedSrc)) {
|
|
244
|
-
const sharedStage = join(stage, "_shared");
|
|
245
|
-
copyTree(sharedSrc, sharedStage, DIR_SKIP);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// 2) 落位(staging → 部署目录;package.json/vite.config.ts 双放:根级按 §3.2 结构,src 内供现场阅读)
|
|
249
|
-
for (const t of targets) {
|
|
250
|
-
cpSync(stage, t, { recursive: true });
|
|
251
|
-
for (const f of ["package.json", "vite.config.ts"]) {
|
|
252
|
-
if (existsSync(join(stage, f))) cpSync(join(stage, f), join(t, f));
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
log(`dev-pack → ${targets.map((t) => t.replace(homedir(), "~")).join(", ")}(AGENTS.md + src/ + tools/)`);
|
|
256
|
-
} finally {
|
|
257
|
-
rmSync(stage, { recursive: true, force: true });
|
|
258
|
-
}
|
|
259
|
-
}
|
package/reconcile-deps.mjs
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* _shared/reconcile-deps.mjs · 通知 pi-web 双实例对齐扩展共享依赖(各扩展 deploy.mjs 共用)
|
|
3
|
-
*
|
|
4
|
-
* 背景:扩展运行时依赖统一装进共享根 <webRoot>/node_modules(jiti 从扩展目录向上解析
|
|
5
|
-
* 自动命中;机制仿 pi 本体 npm: 包安装)。共享根的唯一写入方是 aiti-extman 的
|
|
6
|
-
* shared-deps reconcile——deploy.mjs 拷完文件后 POST 本通知即可,不在部署脚本里跑 npm。
|
|
7
|
-
*
|
|
8
|
-
* 用法(与 reload-instances 同款:允许失败,永不 throw、不影响 deploy 退出码):
|
|
9
|
-
* import { notifyReconcile } from "../../_shared/reconcile-deps.mjs";
|
|
10
|
-
* await notifyReconcile(); // 默认 "[deploy] " 前缀;建议在 reloadInstances() 之前调
|
|
11
|
-
*
|
|
12
|
-
* 失败无害:extman 在下一次 install/remove/reconcile-deps 时会自愈对齐;双实例各自
|
|
13
|
-
* reconcile 各自的 webRoot(~/.pi/web 与 ~/.pi/web-dev),互不相干。
|
|
14
|
-
*/
|
|
15
|
-
import { buildInstances } from "./reload-instances.mjs";
|
|
16
|
-
|
|
17
|
-
export async function notifyReconcile(prefix = "[deploy] ") {
|
|
18
|
-
for (const inst of buildInstances()) {
|
|
19
|
-
if (inst.base === null) {
|
|
20
|
-
console.log(`${prefix}reconcile ${inst.name} 跳过:basePath 非法`);
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
try {
|
|
24
|
-
const r = await fetch(inst.base + "/api/extensions/aiti-extman/ops", {
|
|
25
|
-
method: "POST",
|
|
26
|
-
headers: { "content-type": "application/json" },
|
|
27
|
-
body: JSON.stringify({ action: "reconcile-deps", source: "deploy" }),
|
|
28
|
-
});
|
|
29
|
-
const body = await r.json().catch(() => null);
|
|
30
|
-
const err = body && body.deps && body.deps.error;
|
|
31
|
-
if (r.ok && body && body.ok) {
|
|
32
|
-
console.log(`${prefix}reconcile ${inst.name} → ✓(${Object.keys(body.deps?.deps || {}).length} 个共享依赖)`);
|
|
33
|
-
} else {
|
|
34
|
-
console.log(`${prefix}reconcile ${inst.name} → HTTP ${r.status} ⚠${err ? " " + err : "(extman 未就绪?下次安装时自动对齐)"}`);
|
|
35
|
-
}
|
|
36
|
-
} catch (e) {
|
|
37
|
-
const cause = e && e.cause ? String(e.cause.code || e.cause.message || e.cause) : String((e && e.message) || e);
|
|
38
|
-
console.log(`${prefix}reconcile ${inst.name} 不可达(${cause})— 下次安装时自动对齐`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
package/reload-instances.mjs
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* _shared/reload-instances.mjs · pi-web 双实例热重载(各扩展 deploy.mjs 共用)
|
|
3
|
-
*
|
|
4
|
-
* dev 3001 / 生产 3002;端口可经 AITI_DEV_PORT / AITI_PROD_PORT 覆盖;
|
|
5
|
-
* basePath 双 env(AITI_DEV_BASE_PATH / AITI_PROD_BASE_PATH)按机器设值,
|
|
6
|
-
* 推荐值不带斜杠(48:dev 与 空;155:系统级 wdev 与 win)——git-bash 启动
|
|
7
|
-
* 原生程序时会把 / 开头的 env 值做 MSYS 路径转换(/wdev → D:/apps/Git/wdev)
|
|
8
|
-
* 致 URL 非法;不带斜杠则不触发转换。此处自动补 /(旧 /xxx 格式仍兼容),
|
|
9
|
-
* 兼容不了时提示 MSYS_NO_PATHCONV=1。
|
|
10
|
-
* 避免「server 端代码改了、实例没 reload 导致跑旧逻辑」。
|
|
11
|
-
*
|
|
12
|
-
* 用法(静态 import 或顶层 await import 均可):
|
|
13
|
-
* import { reloadInstances } from "../../_shared/reload-instances.mjs";
|
|
14
|
-
* await reloadInstances(); // 默认 "[deploy] " 前缀
|
|
15
|
-
*/
|
|
16
|
-
const BASE_PATH_RE = /^\/[A-Za-z0-9._-]*$/;
|
|
17
|
-
|
|
18
|
-
function buildInstance(name, portEnv, portDefault, pathEnv, pathDefault) {
|
|
19
|
-
const origin = `http://127.0.0.1:${process.env[portEnv] || portDefault}`;
|
|
20
|
-
const raw = process.env[pathEnv];
|
|
21
|
-
if (raw === undefined || raw === null || String(raw).trim() === "") return { name, base: origin + pathDefault };
|
|
22
|
-
// 自动补 /:wdev → /wdev(推荐无斜杠格式,MSYS 不转换);/wdev → /wdev(旧格式兼容)
|
|
23
|
-
const p = "/" + String(raw).trim().replace(/^\/+|\/+$/g, "");
|
|
24
|
-
if (BASE_PATH_RE.test(p)) return { name, base: origin + p };
|
|
25
|
-
return { name, base: null, raw };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** 双实例清单(dev/prod 各 {name, base});reload-instances 与 reconcile-deps 等共用。 */
|
|
29
|
-
export function buildInstances() {
|
|
30
|
-
return [
|
|
31
|
-
buildInstance("dev", "AITI_DEV_PORT", 3001, "AITI_DEV_BASE_PATH", "/dev"),
|
|
32
|
-
buildInstance("prod", "AITI_PROD_PORT", 3002, "AITI_PROD_BASE_PATH", ""),
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function reloadInstances(prefix = "[deploy] ") {
|
|
37
|
-
// 批次合并(deploy-all 场景):子条目部署脚本被注入 PI_WEB_DEFER_RELOAD=1 时不各自 reload,
|
|
38
|
-
// 由 deploy-all 在全部条目完成后统一执行一轮(N 条目 × 2 实例 → 2 次 POST)。
|
|
39
|
-
// 避免批量部署时的 reload 风暴(2026-09-15 实测:65 条目逐个 reload 把 dev 实例打满)。
|
|
40
|
-
// 单独跑某个扩展 deploy.mjs 不带该 env → 行为不变(部署完立即 reload)。
|
|
41
|
-
if (process.env.PI_WEB_DEFER_RELOAD === "1") {
|
|
42
|
-
console.log(`${prefix}reload 已 defer(PI_WEB_DEFER_RELOAD=1,由 deploy-all 收尾统一执行)`);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
// 允许失败:网络异常/非 2xx/服务未启动/env 非法一律只打提示,永不 throw——不影响 deploy 退出码
|
|
46
|
-
for (const inst of buildInstances()) {
|
|
47
|
-
if (inst.base === null) {
|
|
48
|
-
const msysHint = /[\\:]/.test(inst.raw) ? "——疑似 git-bash MSYS 路径转换,用 MSYS_NO_PATHCONV=1 前缀重跑" : "";
|
|
49
|
-
console.log(`${prefix}reload ${inst.name} 跳过:AITI_${inst.name.toUpperCase()}_BASE_PATH="${inst.raw}" 非法(须形如 /xxx 或留空)${msysHint}`);
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
try {
|
|
53
|
-
const r = await fetch(inst.base + "/api/extensions/reload", { method: "POST" });
|
|
54
|
-
console.log(`${prefix}reload ${inst.name} → HTTP ${r.status} ${r.ok ? "✓" : "⚠ 失败(请手动 POST /api/extensions/reload)"}`);
|
|
55
|
-
} catch (e) {
|
|
56
|
-
const cause = e && e.cause ? String(e.cause.code || e.cause.message || e.cause) : String((e && e.message) || e);
|
|
57
|
-
console.log(`${prefix}reload ${inst.name} 不可达(${cause})— 需手动 POST /api/extensions/reload`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|