synabun 2.2.0 → 2.2.2
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/CHANGELOG.md +45 -0
- package/README.md +1 -1
- package/hooks/claude-code/pre-websearch.mjs +4 -1
- package/hooks/claude-code/prompt-submit.mjs +7 -0
- package/lib/release-scan.mjs +23 -10
- package/lib/virustotal.mjs +143 -39
- package/mcp-server/dist/http.d.ts +19 -0
- package/mcp-server/dist/http.d.ts.map +1 -1
- package/mcp-server/dist/http.js +55 -13
- package/mcp-server/dist/http.js.map +1 -1
- package/mcp-server/dist/index.d.ts.map +1 -1
- package/mcp-server/dist/index.js +14 -9
- package/mcp-server/dist/index.js.map +1 -1
- package/mcp-server/dist/services/browser-route-flow.d.ts +118 -0
- package/mcp-server/dist/services/browser-route-flow.d.ts.map +1 -0
- package/mcp-server/dist/services/browser-route-flow.js +339 -0
- package/mcp-server/dist/services/browser-route-flow.js.map +1 -0
- package/mcp-server/dist/services/desktop-client.js +2 -2
- package/mcp-server/dist/services/desktop-client.js.map +1 -1
- package/mcp-server/dist/services/identity.d.ts +66 -3
- package/mcp-server/dist/services/identity.d.ts.map +1 -1
- package/mcp-server/dist/services/identity.js +61 -4
- package/mcp-server/dist/services/identity.js.map +1 -1
- package/mcp-server/dist/services/neural-interface.d.ts +112 -1
- package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
- package/mcp-server/dist/services/neural-interface.js +555 -61
- package/mcp-server/dist/services/neural-interface.js.map +1 -1
- package/mcp-server/dist/tools/bluesky.d.ts +20 -7
- package/mcp-server/dist/tools/bluesky.d.ts.map +1 -1
- package/mcp-server/dist/tools/bluesky.js +147 -45
- package/mcp-server/dist/tools/bluesky.js.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts +11 -2
- package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.js +131 -18
- package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
- package/mcp-server/dist/tools/browser-cheatsheet.d.ts +2 -0
- package/mcp-server/dist/tools/browser-cheatsheet.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-cheatsheet.js +3 -0
- package/mcp-server/dist/tools/browser-cheatsheet.js.map +1 -1
- package/mcp-server/dist/tools/morelogin.d.ts.map +1 -1
- package/mcp-server/dist/tools/morelogin.js +4 -2
- package/mcp-server/dist/tools/morelogin.js.map +1 -1
- package/mcp-server/dist/tools/skill-tools.d.ts +4 -4
- package/mcp-server/dist/tools/youtube/helpers.d.ts +1 -0
- package/mcp-server/dist/tools/youtube/helpers.d.ts.map +1 -1
- package/mcp-server/dist/tools/youtube/helpers.js +1 -1
- package/mcp-server/dist/tools/youtube/helpers.js.map +1 -1
- package/mcp-server/package-lock.json +2 -2
- package/mcp-server/package.json +1 -1
- package/mcp-server/src/http.ts +58 -12
- package/mcp-server/src/index.ts +15 -9
- package/mcp-server/src/services/browser-route-flow.ts +402 -0
- package/mcp-server/src/services/desktop-client.ts +2 -2
- package/mcp-server/src/services/identity.ts +80 -4
- package/mcp-server/src/services/neural-interface.ts +572 -70
- package/mcp-server/src/tools/bluesky.ts +164 -51
- package/mcp-server/src/tools/browser-advanced.ts +121 -17
- package/mcp-server/src/tools/browser-cheatsheet.ts +3 -0
- package/mcp-server/src/tools/morelogin.ts +4 -2
- package/mcp-server/src/tools/youtube/helpers.ts +2 -2
- package/neural-interface/i18n/en.json +27 -0
- package/neural-interface/i18n/pt-BR.json +27 -0
- package/neural-interface/lib/assistant-api.js +64 -23
- package/neural-interface/lib/assistant-brains/codex.js +6 -1
- package/neural-interface/lib/assistant-catalog.js +20 -2
- package/neural-interface/lib/assistant-dispatch.js +31 -2
- package/neural-interface/lib/assistant-persona.js +1 -0
- package/neural-interface/lib/assistant-runtime.js +15 -1
- package/neural-interface/lib/assistant-tools.js +10 -1
- package/neural-interface/lib/browser-automation-routes.js +84 -0
- package/neural-interface/lib/browser-execution.js +16 -3
- package/neural-interface/lib/browser-owner.js +48 -0
- package/neural-interface/lib/browser-session-close.js +84 -0
- package/neural-interface/lib/browser-start-handoff.js +202 -0
- package/neural-interface/lib/browser-tool-policy.js +21 -4
- package/neural-interface/lib/browser-wedge-retire.js +64 -0
- package/neural-interface/lib/claude-agent-bridge.js +36 -1
- package/neural-interface/lib/claude-background-hold.js +239 -0
- package/neural-interface/lib/exec-loop-command.js +8 -0
- package/neural-interface/lib/http-guards.js +11 -6
- package/neural-interface/lib/loop-logger.js +6 -1
- package/neural-interface/lib/mcp-external-servers.js +64 -13
- package/neural-interface/lib/native-loop-providers.js +122 -10
- package/neural-interface/lib/native-loop-runtime.js +112 -11
- package/neural-interface/lib/opencode-native-boundary.js +155 -21
- package/neural-interface/lib/owner-authority.js +113 -37
- package/neural-interface/lib/page-session.js +81 -23
- package/neural-interface/lib/peer-identity.js +311 -47
- package/neural-interface/lib/provider-panel-api-key-auth.js +7 -2
- package/neural-interface/lib/pty-host/core.js +36 -0
- package/neural-interface/lib/pty-host/protocol.js +2 -0
- package/neural-interface/lib/skills/api-v2.js +13 -2
- package/neural-interface/lib/skills/creation-api.js +3 -1
- package/neural-interface/lib/skills/evals-api.js +5 -1
- package/neural-interface/lib/social-write-pacing.js +151 -0
- package/neural-interface/lib/terminal-token-redaction.js +293 -0
- package/neural-interface/lib/terminal-tokens.js +359 -0
- package/neural-interface/lib/worker-mcp-channel.js +45 -10
- package/neural-interface/package-lock.json +2 -2
- package/neural-interface/package.json +1 -1
- package/neural-interface/public/shared/agy/agy-render.js +41 -1
- package/neural-interface/public/shared/agy/agy-styles.js +5 -0
- package/neural-interface/public/shared/api.js +1 -0
- package/neural-interface/public/shared/assistant/asst-api.js +14 -2
- package/neural-interface/public/shared/assistant/asst-computer.js +73 -4
- package/neural-interface/public/shared/assistant/asst-limit-wait.js +3 -1
- package/neural-interface/public/shared/assistant/asst-motion-gate.js +80 -0
- package/neural-interface/public/shared/assistant/asst-panel.js +8 -4
- package/neural-interface/public/shared/assistant/asst-render.js +72 -11
- package/neural-interface/public/shared/assistant/asst-scene-styles.js +55 -23
- package/neural-interface/public/shared/assistant/asst-scenes.js +225 -79
- package/neural-interface/public/shared/assistant/asst-sidepanel-styles.js +4 -4
- package/neural-interface/public/shared/assistant/asst-style-cache.js +44 -0
- package/neural-interface/public/shared/assistant/asst-styles.js +26 -5
- package/neural-interface/public/shared/cdx/cdx-render.js +73 -7
- package/neural-interface/public/shared/cdx/cdx-tabs.js +3 -3
- package/neural-interface/public/shared/cp/cp-agents.js +8 -0
- package/neural-interface/public/shared/cp/cp-permissions.js +18 -2
- package/neural-interface/public/shared/cp/cp-rehydrate.js +25 -1
- package/neural-interface/public/shared/cp/cp-tasks-model.js +33 -0
- package/neural-interface/public/shared/judgments-log-origins.js +29 -0
- package/neural-interface/public/shared/loading-teardown.js +34 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-automation-ownership.js +23 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-childpanel.js +6 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-events.js +13 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-icons.js +3 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +5 -2
- package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +16 -86
- package/neural-interface/public/shared/ocp-v2/ocp-v2-send.js +6 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-styles.js +88 -72
- package/neural-interface/public/shared/ocp-v2/ocp-v2-ws.js +5 -2
- package/neural-interface/public/shared/owner-access.js +134 -0
- package/neural-interface/public/shared/owner-authority-view.js +92 -0
- package/neural-interface/public/shared/page-session.js +143 -14
- package/neural-interface/public/shared/poll-backoff.js +59 -0
- package/neural-interface/public/shared/settings/settings-data.js +6 -1
- package/neural-interface/public/shared/settings/settings-kit.js +11 -3
- package/neural-interface/public/shared/settings/settings-shell.js +7 -0
- package/neural-interface/public/shared/shown-children.js +72 -0
- package/neural-interface/public/shared/static-bg-state.js +22 -0
- package/neural-interface/public/shared/storage-retry.js +47 -0
- package/neural-interface/public/shared/storage.js +25 -4
- package/neural-interface/public/shared/styles.css +109 -8
- package/neural-interface/public/shared/synabun-mascot.js +27 -8
- package/neural-interface/public/shared/synabun-ticker.js +147 -12
- package/neural-interface/public/shared/synabun-tools.js +173 -0
- package/neural-interface/public/shared/tool-heartbeat.js +43 -0
- package/neural-interface/public/shared/tool-list-picker.js +3 -1
- package/neural-interface/public/shared/tool-list-view.js +23 -4
- package/neural-interface/public/shared/ui-automation-studio.js +8 -1
- package/neural-interface/public/shared/ui-claude-panel.js +238 -90
- package/neural-interface/public/shared/ui-judgments.js +37 -10
- package/neural-interface/public/shared/ui-loading.js +5 -4
- package/neural-interface/public/shared/ui-navbar.js +6 -0
- package/neural-interface/public/shared/ui-owner-access.js +191 -0
- package/neural-interface/public/shared/ui-owner-authority.js +162 -0
- package/neural-interface/public/shared/ui-sessions.js +45 -16
- package/neural-interface/public/shared/ui-settings.js +86 -15
- package/neural-interface/public/shared/ui-terminal.js +15 -7
- package/neural-interface/public/shared/ui-tutorial-preview.js +4 -0
- package/neural-interface/public/shared/ui-update.js +3 -0
- package/neural-interface/public/shared/ui-whiteboard.js +4 -0
- package/neural-interface/public/shared/window-keys.js +38 -0
- package/neural-interface/server.js +1240 -238
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -207,7 +207,10 @@ const HELPERS = [BSKY_SESSION_JS, BSKY_XRPC_JS, BSKY_NORMURI_JS, BSKY_UPLOAD_JS,
|
|
|
207
207
|
interface SessionArgs { sessionId?: string; tabId?: string }
|
|
208
208
|
interface CursorArgs extends SessionArgs { limit?: number; cursor?: string; maxItems?: number }
|
|
209
209
|
/** `expired`: the server rejected the token; `wrote`: a write had already landed in that script. */
|
|
210
|
-
type
|
|
210
|
+
type PacedWrite = { retryAfterMs: number; waitedMs: number; gapMinMs: number; gapMaxMs: number };
|
|
211
|
+
/** `missingRoute`: the script did not run, because its session or tab was gone. */
|
|
212
|
+
type BskyRead = { data: any } | { error: string; expired?: boolean; wrote?: boolean; missingRoute?: true };
|
|
213
|
+
type BskyResult = BskyRead | { paced: PacedWrite };
|
|
211
214
|
|
|
212
215
|
const BSKY_APP_URL = 'https://bsky.app/';
|
|
213
216
|
const BSKY_TARGET_PROBE_JS = `(() => {
|
|
@@ -241,9 +244,34 @@ function isSafeBlankTarget(target: BskyPageTarget): boolean {
|
|
|
241
244
|
return href === '' || /^about:blank(?:[?#].*)?$/.test(href);
|
|
242
245
|
}
|
|
243
246
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
+
/**
|
|
248
|
+
* The session and tab one tool call works in. When the session or tab a step names is gone, the Neural Interface
|
|
249
|
+
* client may run that step on the run's replacement tab instead and say so (`rerouted`); every later step of the
|
|
250
|
+
* call then follows it. Keeping the old id is what failed on 2026-11-04: the probe read the replacement tab
|
|
251
|
+
* (about:blank), the navigation went to the closed session and answered "Session not found".
|
|
252
|
+
*/
|
|
253
|
+
type PageRoute = { sessionId: string; tabId?: string };
|
|
254
|
+
/** A step that failed because its session or tab is gone, before anything ran: the call may find its tab again once. */
|
|
255
|
+
type StepError = { error: string; missingRoute?: true };
|
|
256
|
+
|
|
257
|
+
function follow(route: PageRoute, res: ni.NiResponse | null | undefined): void {
|
|
258
|
+
const next = res?.rerouted;
|
|
259
|
+
if (next && typeof next.sessionId === 'string' && next.sessionId) {
|
|
260
|
+
route.sessionId = next.sessionId;
|
|
261
|
+
route.tabId = next.tabId || undefined;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Same test as the client's isMissingRoute: the server refused before starting anything (SESSION_NOT_FOUND / TAB_NOT_FOUND). */
|
|
266
|
+
function missingRoute(res: ni.NiResponse | null | undefined): boolean {
|
|
267
|
+
return !!res && res.actionStarted === false && (res.code === 'SESSION_NOT_FOUND' || res.code === 'TAB_NOT_FOUND');
|
|
268
|
+
}
|
|
269
|
+
const stepError = (error: string, res?: ni.NiResponse | null): StepError => (missingRoute(res) ? { error, missingRoute: true } : { error });
|
|
270
|
+
|
|
271
|
+
async function inspectBlueskyTarget(route: PageRoute): Promise<BskyPageTarget | StepError> {
|
|
272
|
+
const inspected = await ni.evaluate(route.sessionId, BSKY_TARGET_PROBE_JS, route.tabId);
|
|
273
|
+
follow(route, inspected);
|
|
274
|
+
if (inspected.error) return stepError(`Could not inspect the assigned BlueSky tab: ${inspected.error}`, inspected);
|
|
247
275
|
const target = inspected.result as Partial<BskyPageTarget> | null;
|
|
248
276
|
if (!target || typeof target.href !== 'string') {
|
|
249
277
|
return { error: 'Could not inspect the assigned BlueSky tab: invalid page context response.' };
|
|
@@ -265,10 +293,11 @@ async function inspectBlueskyTarget(sessionId: string, tabId?: string): Promise<
|
|
|
265
293
|
export async function ensureBlueskyPageTarget(
|
|
266
294
|
sessionId: string,
|
|
267
295
|
tabId?: string,
|
|
268
|
-
|
|
269
|
-
|
|
296
|
+
route: PageRoute = { sessionId, tabId },
|
|
297
|
+
): Promise<{ ok: true; target: BskyPageTarget; route: PageRoute } | StepError> {
|
|
298
|
+
const inspected = await inspectBlueskyTarget(route);
|
|
270
299
|
if ('error' in inspected) return inspected;
|
|
271
|
-
if (isBlueskyTarget(inspected)) return { ok: true, target: inspected };
|
|
300
|
+
if (isBlueskyTarget(inspected)) return { ok: true, target: inspected, route };
|
|
272
301
|
|
|
273
302
|
const pageLabel = inspected.href || '(empty URL)';
|
|
274
303
|
if (!inspected.topLevel) {
|
|
@@ -280,17 +309,18 @@ export async function ensureBlueskyPageTarget(
|
|
|
280
309
|
};
|
|
281
310
|
}
|
|
282
311
|
|
|
283
|
-
const navigated = await ni.navigate(sessionId, BSKY_APP_URL, tabId, undefined, 'none');
|
|
312
|
+
const navigated = await ni.navigate(route.sessionId, BSKY_APP_URL, route.tabId, undefined, 'none');
|
|
313
|
+
follow(route, navigated);
|
|
284
314
|
if (navigated.error) {
|
|
285
|
-
return
|
|
315
|
+
return stepError(`Could not initialize the blank BlueSky tab at ${BSKY_APP_URL}: ${navigated.error}`, navigated);
|
|
286
316
|
}
|
|
287
317
|
|
|
288
|
-
const verified = await inspectBlueskyTarget(
|
|
318
|
+
const verified = await inspectBlueskyTarget(route);
|
|
289
319
|
if ('error' in verified) return verified;
|
|
290
320
|
if (!isBlueskyTarget(verified)) {
|
|
291
321
|
return { error: `BlueSky tab initialization ended at ${verified.href || '(empty URL)'} instead of ${BSKY_APP_URL}.` };
|
|
292
322
|
}
|
|
293
|
-
return { ok: true, target: verified };
|
|
323
|
+
return { ok: true, target: verified, route };
|
|
294
324
|
}
|
|
295
325
|
|
|
296
326
|
// JSON literal safe to embed inside JS source (escape the two line separators
|
|
@@ -306,14 +336,22 @@ function clampLimit(n?: number, def = 50, max = 100): number {
|
|
|
306
336
|
return Math.min(n, max);
|
|
307
337
|
}
|
|
308
338
|
|
|
309
|
-
async function evalBsky(sessionId: string, tabId: string | undefined, body: string): Promise<BskyResult> {
|
|
339
|
+
async function evalBsky(sessionId: string, tabId: string | undefined, body: string, pace?: ni.SocialWritePace): Promise<BskyResult> {
|
|
310
340
|
// The Neural Interface runs this via page.evaluate(string), which compiles the
|
|
311
341
|
// whole string as ONE expression — so the helper function declarations must
|
|
312
342
|
// live INSIDE the IIFE, not before it (a leading `function ...` is not an
|
|
313
343
|
// expression and throws "Unexpected token").
|
|
314
344
|
const script = `(async () => {\n${HELPERS}\n${body}\n})()`;
|
|
315
|
-
|
|
316
|
-
|
|
345
|
+
// Never run on a replacement tab the client picks by itself: that tab is about:blank, not the bsky.app page this
|
|
346
|
+
// call prepared. A gone session or tab comes back as missingRoute, and runPrepared prepares the run's new tab first.
|
|
347
|
+
const res = await ni.evaluate(sessionId, script, tabId, { recover: false, ...(pace && { pace }) });
|
|
348
|
+
if (res.error) return { error: `BlueSky call failed: ${res.error}`, ...(missingRoute(res) && { missingRoute: true as const }) };
|
|
349
|
+
// An automation's write waits out the account's gap inside the Neural Interface; when more of it is left than
|
|
350
|
+
// this call may wait, nothing ran and the answer says how much is left (the next call waits the rest).
|
|
351
|
+
const paced = res.paced as (Partial<PacedWrite> & { pending?: boolean }) | undefined;
|
|
352
|
+
if (paced?.pending) {
|
|
353
|
+
return { paced: { retryAfterMs: Number(paced.retryAfterMs) || 0, waitedMs: Number(paced.waitedMs) || 0, gapMinMs: Number(paced.gapMinMs) || 0, gapMaxMs: Number(paced.gapMaxMs) || 0 } };
|
|
354
|
+
}
|
|
317
355
|
const r = res.result as any;
|
|
318
356
|
if (r && r.__bskyError) return { error: r.__bskyError, expired: r.__bskyExpired === true, wrote: r.__bskyWrote === true };
|
|
319
357
|
return { data: r };
|
|
@@ -341,8 +379,9 @@ type TokenState = {
|
|
|
341
379
|
error?: string;
|
|
342
380
|
expiresAt?: number;
|
|
343
381
|
pageAgeMs?: number | null;
|
|
382
|
+
missingRoute?: true;
|
|
344
383
|
};
|
|
345
|
-
type FreshSession = { ok: true; reloaded: boolean } |
|
|
384
|
+
type FreshSession = { ok: true; reloaded: boolean } | StepError;
|
|
346
385
|
type OutOfTime = { error: string; timedOut: true };
|
|
347
386
|
|
|
348
387
|
const NOT_LOGGED_IN = 'Not logged in to BlueSky (no session token found on this page). Open a bsky.app tab, sign in, then retry.';
|
|
@@ -360,10 +399,11 @@ function beforeDeadline<T>(work: Promise<T>, deadlineAt: number, what: string):
|
|
|
360
399
|
}
|
|
361
400
|
const timedOut = (value: unknown): value is OutOfTime => !!value && typeof value === 'object' && (value as OutOfTime).timedOut === true;
|
|
362
401
|
|
|
363
|
-
async function readTokenState(
|
|
364
|
-
const res = await beforeDeadline(ni.evaluate(sessionId, TOKEN_STATE_SCRIPT, tabId), deadlineAt, 'reading the BlueSky session token');
|
|
402
|
+
async function readTokenState(route: PageRoute, deadlineAt: number): Promise<TokenState> {
|
|
403
|
+
const res = await beforeDeadline(ni.evaluate(route.sessionId, TOKEN_STATE_SCRIPT, route.tabId), deadlineAt, 'reading the BlueSky session token');
|
|
365
404
|
if (timedOut(res)) return { state: 'error', error: res.error };
|
|
366
|
-
|
|
405
|
+
follow(route, res);
|
|
406
|
+
if (res.error) return { state: 'error', error: `BlueSky call failed: ${res.error}`, ...(missingRoute(res) && { missingRoute: true as const }) };
|
|
367
407
|
const r = res.result as TokenState | null;
|
|
368
408
|
if (!r || typeof r.state !== 'string') return { state: 'error', error: 'BlueSky call failed: invalid session probe response.' };
|
|
369
409
|
return r;
|
|
@@ -371,10 +411,12 @@ async function readTokenState(sessionId: string, tabId: string | undefined, dead
|
|
|
371
411
|
|
|
372
412
|
/** Poll until the page holds a usable token (or none at all), the wait ends, or the
|
|
373
413
|
* call's deadline passes. `staleExpiresAt` names a token the server already rejected. */
|
|
374
|
-
async function waitForToken(
|
|
414
|
+
async function waitForToken(route: PageRoute, timeoutMs: number, deadlineAt: number, staleExpiresAt?: number): Promise<TokenState> {
|
|
375
415
|
const waitUntil = Math.min(Date.now() + timeoutMs, deadlineAt);
|
|
376
416
|
for (;;) {
|
|
377
|
-
const st = await readTokenState(
|
|
417
|
+
const st = await readTokenState(route, deadlineAt);
|
|
418
|
+
// The tab is gone: polling it again cannot help (the caller finds it again).
|
|
419
|
+
if (st.missingRoute) return st;
|
|
378
420
|
const renewed = staleExpiresAt === undefined || st.expiresAt !== staleExpiresAt;
|
|
379
421
|
// A page mid-reload can fail to evaluate for a moment; keep polling.
|
|
380
422
|
if (st.state === 'none' || (tokenUsable(st) && renewed)) return st;
|
|
@@ -392,52 +434,90 @@ async function waitForToken(sessionId: string, tabId: string | undefined, timeou
|
|
|
392
434
|
export async function ensureFreshBlueskySession(
|
|
393
435
|
sessionId: string,
|
|
394
436
|
tabId: string | undefined,
|
|
395
|
-
opts: { forceReload?: boolean; deadlineAt?: number } = {},
|
|
437
|
+
opts: { forceReload?: boolean; deadlineAt?: number; route?: PageRoute } = {},
|
|
396
438
|
): Promise<FreshSession> {
|
|
397
439
|
const by = opts.deadlineAt ?? Date.now() + BSKY_CALL_BUDGET_MS - BSKY_SCRIPT_RESERVE_MS;
|
|
398
|
-
const
|
|
440
|
+
const route = opts.route || { sessionId, tabId };
|
|
441
|
+
const first = await readTokenState(route, by);
|
|
399
442
|
if (first.state === 'none') return { error: first.error || NOT_LOGGED_IN };
|
|
400
|
-
if (first.state === 'error') return { error: first.error || 'BlueSky call failed.' };
|
|
443
|
+
if (first.state === 'error') return { error: first.error || 'BlueSky call failed.', ...(first.missingRoute && { missingRoute: true as const }) };
|
|
401
444
|
if (!opts.forceReload) {
|
|
402
445
|
if (tokenUsable(first)) return { ok: true, reloaded: false };
|
|
403
446
|
const pageAge = typeof first.pageAgeMs === 'number' ? first.pageAgeMs : Infinity;
|
|
404
447
|
if (pageAge < TOKEN_BOOT_WINDOW_MS) {
|
|
405
|
-
const booted = await waitForToken(
|
|
448
|
+
const booted = await waitForToken(route, TOKEN_BOOT_WAIT_MS, by);
|
|
406
449
|
if (booted.state === 'none') return { error: booted.error || NOT_LOGGED_IN };
|
|
450
|
+
if (booted.missingRoute) return { error: booted.error || 'BlueSky call failed.', missingRoute: true };
|
|
407
451
|
if (tokenUsable(booted)) return { ok: true, reloaded: false };
|
|
408
452
|
}
|
|
409
453
|
}
|
|
410
454
|
// Reload this bsky.app tab once so the app resumes its session and renews the token.
|
|
411
|
-
const reloaded = await beforeDeadline(ni.reload(sessionId, tabId), by, 'reloading the bsky.app tab to renew the session');
|
|
455
|
+
const reloaded = await beforeDeadline(ni.reload(route.sessionId, route.tabId), by, 'reloading the bsky.app tab to renew the session');
|
|
412
456
|
if (timedOut(reloaded)) return { error: `BlueSky token expired, and this call ran out of its ${BSKY_CALL_BUDGET_MS / 1000} s time limit while reloading the bsky.app tab to renew it. Try again; the next call starts with a fresh time limit.` };
|
|
413
|
-
|
|
414
|
-
|
|
457
|
+
follow(route, reloaded);
|
|
458
|
+
if (reloaded.error) return stepError(`BlueSky token expired, and reloading the bsky.app tab to renew it failed: ${reloaded.error}`, reloaded);
|
|
459
|
+
const after = await waitForToken(route, TOKEN_RELOAD_WAIT_MS, by, opts.forceReload ? first.expiresAt : undefined);
|
|
415
460
|
if (after.state === 'none') {
|
|
416
461
|
return { error: 'Not logged in to BlueSky: after reloading the bsky.app tab the page holds no session (the account was signed out). Sign in again in this browser profile, then retry.' };
|
|
417
462
|
}
|
|
418
463
|
if (tokenUsable(after)) return { ok: true, reloaded: true };
|
|
419
|
-
if (after.state === 'error') return { error: `BlueSky token expired, and the bsky.app tab could not be read after reloading it: ${after.error}
|
|
464
|
+
if (after.state === 'error') return { error: `BlueSky token expired, and the bsky.app tab could not be read after reloading it: ${after.error}`, ...(after.missingRoute && { missingRoute: true as const }) };
|
|
420
465
|
if (Date.now() >= by) return { error: 'BlueSky token was still expired when this call\'s time limit ran out (the bsky.app tab was reloaded once). Try again; the next call starts with a fresh time limit.' };
|
|
421
466
|
return { error: 'BlueSky token is still expired after waiting for bsky.app to renew it and reloading the tab once, so the saved session could not be renewed. Open bsky.app in this browser profile and sign in again if it asks.' };
|
|
422
467
|
}
|
|
423
468
|
|
|
424
|
-
type BskyRoute = { sessionId: string; tabId?: string; reloaded: boolean; deadlineAt: number };
|
|
469
|
+
type BskyRoute = { sessionId: string; tabId?: string; reloaded: boolean; deadlineAt: number; args: SessionArgs };
|
|
425
470
|
|
|
426
471
|
/** Resolve the caller's tab in the default browser, make sure it is a
|
|
427
472
|
* top-level bsky.app page, and make sure it holds a usable session token — all
|
|
428
|
-
* before the call's deadline minus the time kept for the tool's own request.
|
|
429
|
-
|
|
473
|
+
* before the call's deadline minus the time kept for the tool's own request.
|
|
474
|
+
* `refresh`: the route this caller had is gone; resolve it again without the cache (a pinned run gets its
|
|
475
|
+
* replacement tab, the one the next calls use too). */
|
|
476
|
+
async function prepareOnce(args: SessionArgs, deadlineAt: number, refresh: boolean): Promise<BskyRoute | StepError> {
|
|
430
477
|
const prepBy = deadlineAt - BSKY_SCRIPT_RESERVE_MS;
|
|
431
|
-
const
|
|
478
|
+
const find = refresh ? ni.refreshSession : ni.resolveSession;
|
|
479
|
+
const resolved = await beforeDeadline(find(args.sessionId, undefined, args.tabId, { platform: true }), prepBy, 'finding the bsky.app tab');
|
|
432
480
|
if ('error' in resolved) return { error: resolved.error };
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
481
|
+
const route: PageRoute = { sessionId: resolved.sessionId, tabId: resolved.tabId };
|
|
482
|
+
const ready = await beforeDeadline(ensureBlueskyPageTarget(route.sessionId, route.tabId, route), prepBy, 'opening bsky.app');
|
|
483
|
+
if ('error' in ready) return ready;
|
|
484
|
+
const fresh = await ensureFreshBlueskySession(route.sessionId, route.tabId, { deadlineAt: prepBy, route });
|
|
436
485
|
if ('error' in fresh) return fresh;
|
|
437
|
-
return { sessionId:
|
|
486
|
+
return { sessionId: route.sessionId, tabId: route.tabId, reloaded: fresh.reloaded, deadlineAt, args };
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** Time enough before the script's reserve to find the run's tab again and prepare it. */
|
|
490
|
+
const canFindAgain = (deadlineAt: number) => deadlineAt - BSKY_SCRIPT_RESERVE_MS - Date.now() >= BSKY_RENEW_MIN_MS;
|
|
491
|
+
|
|
492
|
+
async function prepareBsky(args: SessionArgs, deadlineAt = Date.now() + BSKY_CALL_BUDGET_MS): Promise<BskyRoute | StepError> {
|
|
493
|
+
const first = await prepareOnce(args, deadlineAt, false);
|
|
494
|
+
// The session or tab this caller had is gone (another automation closed the browser, a wedged session was
|
|
495
|
+
// retired) and the client could not move the step by itself: find the run's tab again, once.
|
|
496
|
+
if ('error' in first && first.missingRoute && canFindAgain(deadlineAt)) {
|
|
497
|
+
const again = await prepareOnce(args, deadlineAt, true);
|
|
498
|
+
if ('error' in again) return { error: `${ROUTE_GONE} Finding this run's tab again failed: ${again.error}` };
|
|
499
|
+
return again;
|
|
500
|
+
}
|
|
501
|
+
return first;
|
|
438
502
|
}
|
|
439
503
|
|
|
504
|
+
/** What a call says when the browser tab it used went away under it. */
|
|
505
|
+
const ROUTE_GONE = 'The browser tab this BlueSky call used was closed (its browser session ended or was replaced).';
|
|
506
|
+
|
|
440
507
|
const UNCERTAIN = 'It may still finish in the page, so check the account before repeating a post, like, follow or message.';
|
|
508
|
+
const UNCERTAIN_PACED = 'It may still be waiting for its pacing gap and then finish in the page, so check the account before repeating it.';
|
|
509
|
+
|
|
510
|
+
/** Bluesky writes of an automation are spaced by SynaBun: what a write tool's description says about it. */
|
|
511
|
+
const PACING_NOTE = ' In a scheduled automation SynaBun spaces Bluesky writes (posts, replies, likes, reposts, follows and their undos, DMs) a minute or two apart inside this call: never sleep, start a background shell or a Monitor, or end your turn to wait between writes. When the gap needs longer than one call, the call writes nothing, says so, and the next identical call waits the rest.';
|
|
512
|
+
|
|
513
|
+
function pacedText(tool: string, paced: PacedWrite) {
|
|
514
|
+
const secs = (ms: number) => Math.max(1, Math.ceil((Number(ms) || 0) / 1000));
|
|
515
|
+
const range = paced.gapMinMs && paced.gapMaxMs ? ` ${secs(paced.gapMinMs)}-${secs(paced.gapMaxMs)} s apart` : '';
|
|
516
|
+
return text(`Not written yet: SynaBun spaces this automation's Bluesky writes${range}`
|
|
517
|
+
+ `${paced.waitedMs > 0 ? `, and this call waited ${secs(paced.waitedMs)} s of the gap` : ''}; about ${secs(paced.retryAfterMs)} s are left. `
|
|
518
|
+
+ `Call ${tool} again now with the same arguments: the call itself waits out the rest, then writes. `
|
|
519
|
+
+ 'Do not sleep, start a background shell or a Monitor, or end your turn to wait.');
|
|
520
|
+
}
|
|
441
521
|
|
|
442
522
|
/**
|
|
443
523
|
* Run a script on a prepared tab, within the call's deadline. When the server still
|
|
@@ -445,30 +525,58 @@ const UNCERTAIN = 'It may still finish in the page, so check the account before
|
|
|
445
525
|
* once more — only if nothing was written yet, the tab was not already reloaded
|
|
446
526
|
* for this call, and there is time left — so no post, like or message can repeat.
|
|
447
527
|
*/
|
|
448
|
-
async function runPrepared(route: BskyRoute, body: string): Promise<BskyResult> {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
528
|
+
async function runPrepared(route: BskyRoute, body: string, opts: { write?: boolean } = {}): Promise<BskyResult> {
|
|
529
|
+
// A write may wait for the account's pacing gap with whatever this call's budget leaves after the script's own
|
|
530
|
+
// reserve; the Neural Interface paces only an automation's writes (an interactive call never waits).
|
|
531
|
+
const paceNow = () => (opts.write ? { platform: 'bluesky' as const, maxWaitMs: Math.max(0, route.deadlineAt - BSKY_SCRIPT_RESERVE_MS - Date.now()) } : undefined);
|
|
532
|
+
let pace = paceNow();
|
|
533
|
+
let first = await beforeDeadline(evalBsky(route.sessionId, route.tabId, body, pace), route.deadlineAt, 'waiting for BlueSky to answer');
|
|
534
|
+
if (timedOut(first)) return { error: `${first.error} ${pace ? UNCERTAIN_PACED : UNCERTAIN}` };
|
|
535
|
+
if ('error' in first && first.missingRoute) {
|
|
536
|
+
// The script never ran (and took no pacing slot): its tab was closed before it started, or while the write
|
|
537
|
+
// waited for its gap. Find the run's tab again (a replacement starts blank: it is opened at bsky.app and its
|
|
538
|
+
// token checked), then run the script there once. Never more than once, so nothing can repeat.
|
|
539
|
+
const nothing = opts.write ? 'Nothing was written' : 'Nothing was read';
|
|
540
|
+
if (!canFindAgain(route.deadlineAt)) return { error: `${ROUTE_GONE} ${nothing}; call it again now: the next call finds the run's tab first.` };
|
|
541
|
+
const again = await prepareOnce(route.args, route.deadlineAt, true);
|
|
542
|
+
if ('error' in again) return { error: `${ROUTE_GONE} ${nothing}, and finding this run's tab again failed: ${again.error}` };
|
|
543
|
+
Object.assign(route, again);
|
|
544
|
+
pace = paceNow();
|
|
545
|
+
first = await beforeDeadline(evalBsky(route.sessionId, route.tabId, body, pace), route.deadlineAt, 'waiting for BlueSky to answer');
|
|
546
|
+
if (timedOut(first)) return { error: `${first.error} ${pace ? UNCERTAIN_PACED : UNCERTAIN}` };
|
|
547
|
+
if ('error' in first && first.missingRoute) return { error: `${ROUTE_GONE} ${nothing}; the replacement tab closed too. ${first.error}` };
|
|
548
|
+
}
|
|
549
|
+
if ('paced' in first || !('error' in first) || !first.expired) return first;
|
|
452
550
|
if (first.wrote || route.reloaded) {
|
|
453
551
|
return { error: `${first.error} The bsky.app tab was ${route.reloaded ? 'already reloaded once for this call' : 'not retried because part of this action was already written'}; check the account before trying again.`, expired: true, wrote: first.wrote };
|
|
454
552
|
}
|
|
455
553
|
if (route.deadlineAt - Date.now() < BSKY_SCRIPT_RESERVE_MS + BSKY_RENEW_MIN_MS) {
|
|
456
554
|
return { error: `${first.error} Not enough of this call's time limit is left to renew the session and try again; the next BlueSky call renews it first.`, expired: true, wrote: false };
|
|
457
555
|
}
|
|
458
|
-
const renewed = await ensureFreshBlueskySession(route.sessionId, route.tabId, { forceReload: true, deadlineAt: route.deadlineAt - BSKY_SCRIPT_RESERVE_MS });
|
|
459
|
-
if ('error' in renewed) return renewed;
|
|
460
|
-
|
|
556
|
+
const renewed = await ensureFreshBlueskySession(route.sessionId, route.tabId, { forceReload: true, deadlineAt: route.deadlineAt - BSKY_SCRIPT_RESERVE_MS, route });
|
|
557
|
+
if ('error' in renewed) return { error: renewed.error };
|
|
558
|
+
// The first attempt wrote nothing (it took no pacing slot): the retry goes through the same queue, at once.
|
|
559
|
+
const second = await beforeDeadline(evalBsky(route.sessionId, route.tabId, body, pace ? { platform: 'bluesky', maxWaitMs: 0 } : undefined), route.deadlineAt, 'waiting for BlueSky to answer the retry');
|
|
461
560
|
if (timedOut(second)) return { error: `${second.error} ${UNCERTAIN}` };
|
|
561
|
+
if ('paced' in second) return second;
|
|
462
562
|
if ('error' in second && second.expired) {
|
|
463
563
|
return { error: `${second.error} It was still rejected after the bsky.app tab renewed its session; sign in again in this browser profile if bsky.app asks.`, expired: true, wrote: second.wrote };
|
|
464
564
|
}
|
|
465
565
|
return second;
|
|
466
566
|
}
|
|
467
567
|
|
|
468
|
-
|
|
568
|
+
/** A read: never paced. */
|
|
569
|
+
async function runBsky(args: SessionArgs, body: string): Promise<BskyRead> {
|
|
570
|
+
const route = await prepareBsky(args);
|
|
571
|
+
if ('error' in route) return route;
|
|
572
|
+
return runPrepared(route, body) as Promise<BskyRead>;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/** A write (post, reply, like, follow, DM…): an automation's waits out the account's pacing gap. */
|
|
576
|
+
async function runBskyWrite(args: SessionArgs, body: string): Promise<BskyResult> {
|
|
469
577
|
const route = await prepareBsky(args);
|
|
470
578
|
if ('error' in route) return route;
|
|
471
|
-
return runPrepared(route, body);
|
|
579
|
+
return runPrepared(route, body, { write: true });
|
|
472
580
|
}
|
|
473
581
|
|
|
474
582
|
// ── Trimmers: shrink verbose AT Proto views to the useful, token-cheap fields ──
|
|
@@ -849,7 +957,7 @@ const blueskyPostSchema = {
|
|
|
849
957
|
...sessionFields,
|
|
850
958
|
};
|
|
851
959
|
const blueskyPostDescription =
|
|
852
|
-
'BlueSky: publish a post. Supports plain text with auto-facets (@mentions, links, #hashtags), replies (replyTo), quote-posts (quote), and up to 4 images (by url or local path, each with alt text). A link card (OG preview with thumbnail) is auto-generated for the first URL when the post has no images and no quote. Returns the new post uri+cid.';
|
|
960
|
+
'BlueSky: publish a post. Supports plain text with auto-facets (@mentions, links, #hashtags), replies (replyTo), quote-posts (quote), and up to 4 images (by url or local path, each with alt text). A link card (OG preview with thumbnail) is auto-generated for the first URL when the post has no images and no quote. Returns the new post uri+cid.' + PACING_NOTE;
|
|
853
961
|
async function handleBlueskyPost(args: {
|
|
854
962
|
text: string; replyTo?: string; quote?: string;
|
|
855
963
|
images?: Array<{ url?: string; path?: string; alt?: string }>; langs?: string[];
|
|
@@ -971,7 +1079,8 @@ async function handleBlueskyPost(args: {
|
|
|
971
1079
|
if (cr.__bskyError) return cr;
|
|
972
1080
|
return { uri: cr.uri, cid: cr.cid };`;
|
|
973
1081
|
|
|
974
|
-
const r = await runPrepared(resolved, body);
|
|
1082
|
+
const r = await runPrepared(resolved, body, { write: true });
|
|
1083
|
+
if ('paced' in r) return pacedText('bluesky_post', r.paced);
|
|
975
1084
|
if ('error' in r) return text(r.error);
|
|
976
1085
|
return text(`Posted to BlueSky: ${JSON.stringify({ uri: r.data.uri, cid: r.data.cid })}`);
|
|
977
1086
|
}
|
|
@@ -987,7 +1096,8 @@ const blueskyActionSchema = {
|
|
|
987
1096
|
...sessionFields,
|
|
988
1097
|
};
|
|
989
1098
|
const blueskyActionDescription =
|
|
990
|
-
'BlueSky: act on a post or actor. Post: like/unlike, repost/unrepost, delete (your own). Actor: follow/unfollow, mute/unmute, block/unblock. Undo actions resolve the record key automatically — never pass rkeys.';
|
|
1099
|
+
'BlueSky: act on a post or actor. Post: like/unlike, repost/unrepost, delete (your own). Actor: follow/unfollow, mute/unmute, block/unblock. Undo actions resolve the record key automatically — never pass rkeys.' + PACING_NOTE;
|
|
1100
|
+
const PACED_ACTIONS = new Set(['like', 'unlike', 'repost', 'unrepost', 'follow', 'unfollow']);
|
|
991
1101
|
async function handleBlueskyAction(args: { action: string; target: string } & SessionArgs) {
|
|
992
1102
|
const body = `
|
|
993
1103
|
var ACTION = ${jsLit(args.action)};
|
|
@@ -1064,7 +1174,9 @@ async function handleBlueskyAction(args: { action: string; target: string } & Se
|
|
|
1064
1174
|
return { ok:true, action:'delete', uri: uri };
|
|
1065
1175
|
}
|
|
1066
1176
|
return { __bskyError: 'Unknown action: ' + ACTION };`;
|
|
1067
|
-
|
|
1177
|
+
// Engagement and its undos are spaced like posts; moderation and deleting one's own record are not.
|
|
1178
|
+
const r = PACED_ACTIONS.has(args.action) ? await runBskyWrite(args, body) : await runBsky(args, body);
|
|
1179
|
+
if ('paced' in r) return pacedText('bluesky_action', r.paced);
|
|
1068
1180
|
if ('error' in r) return text(r.error);
|
|
1069
1181
|
return text(`BlueSky action: ${JSON.stringify(r.data)}`);
|
|
1070
1182
|
}
|
|
@@ -1104,7 +1216,7 @@ const blueskyDmSchema = {
|
|
|
1104
1216
|
...cursorFields, ...sessionFields,
|
|
1105
1217
|
};
|
|
1106
1218
|
const blueskyDmDescription =
|
|
1107
|
-
'BlueSky DMs (chat.bsky.*): list_convos, read messages in a convo, send a message (by convoId or members), or mark_read. Returns compact JSON.';
|
|
1219
|
+
'BlueSky DMs (chat.bsky.*): list_convos, read messages in a convo, send a message (by convoId or members), or mark_read. Returns compact JSON.' + PACING_NOTE;
|
|
1108
1220
|
async function handleBlueskyDm(args: { action: string; convoId?: string; members?: string[]; text?: string } & CursorArgs) {
|
|
1109
1221
|
const proxy = jsLit(CHAT_PROXY);
|
|
1110
1222
|
if (args.action === 'list_convos') {
|
|
@@ -1145,7 +1257,8 @@ async function handleBlueskyDm(args: { action: string; convoId?: string; members
|
|
|
1145
1257
|
var sm = await __bskyXrpc('chat.bsky.convo.sendMessage', { verb:'POST', proxy: ${proxy}, body:{ convoId: convoId, message: { text: ${jsLit(args.text)} } } });
|
|
1146
1258
|
if (sm.__bskyError) return sm;
|
|
1147
1259
|
return { ok:true, convoId: convoId, messageId: sm.id, sentAt: sm.sentAt };`;
|
|
1148
|
-
const r = await
|
|
1260
|
+
const r = await runBskyWrite(args, body);
|
|
1261
|
+
if ('paced' in r) return pacedText('bluesky_dm', r.paced);
|
|
1149
1262
|
if ('error' in r) return text(r.error);
|
|
1150
1263
|
return text(`DM sent: ${JSON.stringify(r.data)}`);
|
|
1151
1264
|
}
|
|
@@ -87,46 +87,150 @@ export async function handleBrowserWait(args: {
|
|
|
87
87
|
// ── browser_session ──
|
|
88
88
|
|
|
89
89
|
export const browserSessionSchema = {
|
|
90
|
-
action: z.enum(['list', 'create', 'close']).describe('"list" =
|
|
90
|
+
action: z.enum(['list', 'status', 'create', 'close']).describe('"list" = the open sessions with your own tabs (id, address, title; your current tab marked), "status" = the session and tab your browser calls use now, with its address and whether SynaBun replaced a lost tab, "create" = open a new browser tab of your own (starting the browser if needed) and continue there, "close" = close a session.'),
|
|
91
91
|
url: z.string().optional().describe('Starting URL for "create" action (default: about:blank).'),
|
|
92
|
-
sessionId: z.string().optional().describe('Session ID for "close" action. Required for close when multiple sessions exist.'),
|
|
92
|
+
sessionId: z.string().optional().describe('Session ID for "close" action. Required for close when multiple sessions exist (or pass your tabId).'),
|
|
93
|
+
tabId: z.string().optional().describe('Your tab id, as the other browser tools take it. "status" / "list": says whether it is still your current tab (a lost tab names the tab that replaced it). "close": closes only this tab of yours; a tab that is not yours is refused and nothing is closed. Ignored by "create", which always opens a new tab of your own.'),
|
|
93
94
|
};
|
|
94
95
|
|
|
95
96
|
export const browserSessionDescription =
|
|
96
|
-
'Manage browser sessions — list open sessions, create a new
|
|
97
|
+
'Manage browser sessions — list open sessions and your tabs in them, see which tab you work in ("status"), create a new one, or close one (pass your tabId to close only that tab of yours). Use "status" to find your current tab id. When a browser tool\'s answer starts with "Your tab … was lost; a replacement tab … is open", continue in that tab (no create). Use "create" when your browser session or tab was lost (closed, disconnected, "Session not found", "Tab … not found"): it opens a new tab of your own in the SynaBun browser, starting the browser when it is not running, and in a loop or a dispatched run your next browser calls go to it. Only if create fails too, stop and report it; never switch to another browser. Closing a session other automations still use closes only your own tab in it; the session stays open for them.';
|
|
97
98
|
|
|
98
99
|
export async function handleBrowserSession(args: {
|
|
99
100
|
action: string;
|
|
100
101
|
url?: string;
|
|
101
102
|
sessionId?: string;
|
|
103
|
+
tabId?: string;
|
|
102
104
|
}) {
|
|
103
|
-
if (args.action === 'list') {
|
|
104
|
-
const
|
|
105
|
-
if (
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
const lines = sessions.map(s => ` ${s.id} — ${s.title || s.url} (opened ${new Date(s.createdAt).toLocaleTimeString()})`);
|
|
111
|
-
return text(`${sessions.length} session(s):\n${lines.join('\n')}`);
|
|
105
|
+
if (args.action === 'list' || args.action === 'status') {
|
|
106
|
+
const status = await ni.browserRouteStatus();
|
|
107
|
+
if (status.error) return text(`${args.action === 'list' ? 'List' : 'Status'} failed: ${status.error}`);
|
|
108
|
+
const body = args.action === 'list' ? formatSessionList(status) : formatRouteStatus(status);
|
|
109
|
+
const note = args.tabId ? namedTabLine(status, args.tabId) : null;
|
|
110
|
+
return text(note ? `${body}\n${note}` : body);
|
|
112
111
|
}
|
|
113
112
|
|
|
114
113
|
if (args.action === 'create') {
|
|
115
114
|
const result = await ni.createSession(args.url);
|
|
116
|
-
|
|
115
|
+
// A cold browser start still running: nothing failed, and the next create returns that same start.
|
|
116
|
+
if (ni.isBrowserStarting(result)) return text(String(result.error || 'The browser is still starting. Call browser_session with action "create" again in a moment.'));
|
|
117
|
+
if (result.error) return text(`Create failed: ${result.error}\nThe SynaBun browser could not open a new session: stop and report this. Never switch to another browser or tool.`);
|
|
118
|
+
const sessionId = String(result.sessionId);
|
|
119
|
+
const tabId = typeof result.tabId === 'string' ? result.tabId : null;
|
|
120
|
+
const ids = `sessionId "${sessionId}"${tabId ? ` and tabId "${tabId}"` : ''}`;
|
|
121
|
+
const lines = [`Created session ${sessionId}${tabId ? `, tab ${tabId}` : ''} at ${typeof result.url === 'string' ? result.url : args.url || 'about:blank'}.`];
|
|
122
|
+
lines.push(result.repinned === true
|
|
123
|
+
? `This run's browser now works there: your next browser calls go to it with or without ids (pass ${ids} if you pass ids; never the old ones).`
|
|
124
|
+
: `Continue there: pass ${ids} to your next browser calls.`);
|
|
117
125
|
// Created, but MoreLogin refused the start URL (e.g. a localhost port missing from Port scan protection).
|
|
118
|
-
|
|
119
|
-
|
|
126
|
+
if (result.moreLoginBlocked === true && typeof result.notice === 'string') lines.push(result.notice);
|
|
127
|
+
if (typeof result.navigateError === 'string') lines.push(`Opening ${args.url} in it failed: ${result.navigateError}. Open it with browser_navigate.`);
|
|
128
|
+
return text(lines.join('\n'));
|
|
120
129
|
}
|
|
121
130
|
|
|
122
131
|
if (args.action === 'close') {
|
|
123
|
-
|
|
132
|
+
let named = args.sessionId;
|
|
133
|
+
if (args.tabId) {
|
|
134
|
+
// A tab named: only one of the caller's own (browserRouteStatus lists nobody else's), and it picks the session.
|
|
135
|
+
const status = await ni.browserRouteStatus();
|
|
136
|
+
if (status.error) return text(`Close failed: ${status.error}`);
|
|
137
|
+
const owner = status.sessions.find(s => s.tabs.some(t => t.id === args.tabId));
|
|
138
|
+
if (!owner) {
|
|
139
|
+
if (isGoneTab(status, args.tabId)) return text(`Tab ${args.tabId} is already gone; nothing was closed.${status.current?.open && status.current.tabId ? ` Your current tab is ${status.current.tabId}.` : ''}`);
|
|
140
|
+
return text(`Close refused: tab ${args.tabId} is not one of your tabs. You can close only your own tab; nothing was closed. Call browser_session with action "status" to see your tab id.`);
|
|
141
|
+
}
|
|
142
|
+
if (named && named !== owner.id) return text(`Close refused: your tab ${args.tabId} is in session ${owner.id}, not ${named}; nothing was closed.`);
|
|
143
|
+
named = owner.id;
|
|
144
|
+
}
|
|
145
|
+
const resolved = await ni.resolveSession(named);
|
|
124
146
|
if ('error' in resolved) return text(resolved.error);
|
|
125
147
|
|
|
126
148
|
const result = await ni.closeSession(resolved.sessionId);
|
|
127
149
|
if (result.error) return text(`Close failed: ${result.error}`);
|
|
150
|
+
if (result.sessionKept === true) {
|
|
151
|
+
// Other automations (scheduled runs) work in tabs of this browser: the server closed only this caller's tab.
|
|
152
|
+
const others = Number(result.otherOwners) || 0;
|
|
153
|
+
const released = typeof result.releasedTabId === 'string' ? `Only your tab ${result.releasedTabId} was closed` : 'You had no tab of your own in it, so nothing was closed';
|
|
154
|
+
return text(`Session ${resolved.sessionId} stays open: ${others} other automation${others === 1 ? '' : 's'} still use${others === 1 ? 's' : ''} tabs in it. ${released}. Do not close it again. Your next browser call opens a new tab of your own, and its answer starts with that tab's id ("a replacement tab … is open"): continue there, without browser_session create.`);
|
|
155
|
+
}
|
|
128
156
|
return text(`Closed session ${resolved.sessionId}`);
|
|
129
157
|
}
|
|
130
158
|
|
|
131
|
-
return text(`Unknown action "${args.action}". Use "list", "create", or "close".`);
|
|
159
|
+
return text(`Unknown action "${args.action}". Use "list", "status", "create", or "close".`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Lost tabs of the caller's that the route status knows about. */
|
|
163
|
+
function isGoneTab(status: ni.BrowserRouteStatus, tabId: string): boolean {
|
|
164
|
+
return status.replacedTabs.includes(tabId)
|
|
165
|
+
|| status.lost?.tabId === tabId
|
|
166
|
+
|| status.movedFrom?.tabId === tabId
|
|
167
|
+
|| (!!status.current && !status.current.open && status.current.tabId === tabId);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** What a tabId passed to list / status means for the caller: only its own tabs are ever described. */
|
|
171
|
+
export function namedTabLine(status: ni.BrowserRouteStatus, tabId: string): string | null {
|
|
172
|
+
const current = status.current;
|
|
173
|
+
if (current?.open && current.tabId === tabId) return null;
|
|
174
|
+
if (isGoneTab(status, tabId)) {
|
|
175
|
+
return current?.open && current.tabId
|
|
176
|
+
? `Tab ${tabId} you passed is gone; tab ${current.tabId} (session ${current.sessionId}) replaced it. Use tabId ${current.tabId}.`
|
|
177
|
+
: `Tab ${tabId} you passed is gone. Your next browser call opens a new tab of your own and names it.`;
|
|
178
|
+
}
|
|
179
|
+
if (status.sessions.some(s => s.tabs.some(t => t.id === tabId))) {
|
|
180
|
+
return `Tab ${tabId} you passed is one of your tabs, but not the current one${current?.open && current.tabId ? ` (${current.tabId})` : ''}.`;
|
|
181
|
+
}
|
|
182
|
+
return `Tab ${tabId} you passed is not one of your tabs; it was ignored.`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const quoteTitle = (title: string | undefined) => (title ? ` "${title}"` : '');
|
|
186
|
+
const otherTabsText = (n: number) => `${n} tab${n === 1 ? '' : 's'} of other automations or the owner`;
|
|
187
|
+
|
|
188
|
+
/** browser_session list: every session the caller can see, with its own tabs only. */
|
|
189
|
+
export function formatSessionList(status: ni.BrowserRouteStatus): string {
|
|
190
|
+
if (status.sessions.length === 0) return 'No browser sessions open. browser_navigate opens one by itself.';
|
|
191
|
+
const lines = [`${status.sessions.length} session(s); only your own tabs are listed:`];
|
|
192
|
+
for (const session of status.sessions) {
|
|
193
|
+
lines.push(` ${session.id}${session.servesDefault ? '' : ` — not the default browser (${session.profile}); SynaBun's browser tools do not use it`}`);
|
|
194
|
+
for (const tab of session.tabs) {
|
|
195
|
+
const isCurrent = status.current?.open && status.current.sessionId === session.id && status.current.tabId === tab.id;
|
|
196
|
+
lines.push(` tab ${tab.id} — ${tab.url || 'about:blank'}${quoteTitle(tab.title)}${isCurrent ? ' ← your current tab' : ''}`);
|
|
197
|
+
}
|
|
198
|
+
if (!session.tabs.length) lines.push(` no tab of yours${session.otherTabs ? ` (+${otherTabsText(session.otherTabs)})` : ''}`);
|
|
199
|
+
else if (session.otherTabs) lines.push(` +${otherTabsText(session.otherTabs)}`);
|
|
200
|
+
}
|
|
201
|
+
if (status.current && !status.current.open) lines.push(lostTabLine(status.current));
|
|
202
|
+
else if (!status.current) lines.push('You have no tab of your own yet: browser_navigate opens one.');
|
|
203
|
+
return lines.join('\n');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function lostTabLine(current: NonNullable<ni.BrowserRouteStatus['current']>): string {
|
|
207
|
+
return `Your tab ${current.tabId || '(none)'} of session ${current.sessionId} is gone. Your next browser call opens a new tab of your own, and its answer starts with that tab's id: continue there. Call browser_session create only if a browser call fails because the tab is gone and announces no replacement.`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** browser_session status: the route the caller's next browser call uses, its page, and what replaced what. */
|
|
211
|
+
export function formatRouteStatus(status: ni.BrowserRouteStatus): string {
|
|
212
|
+
const current = status.current;
|
|
213
|
+
if (!current) {
|
|
214
|
+
if (status.lost) return `Your tab ${status.lost.tabId || '(none)'} of session ${status.lost.sessionId} is gone and no tab replaced it yet. Your next browser call opens a new tab of your own, and its answer starts with that tab's id.`;
|
|
215
|
+
return 'You have no browser tab yet: browser_navigate opens one (no browser_session create needed).';
|
|
216
|
+
}
|
|
217
|
+
if (!current.open) return lostTabLine(current);
|
|
218
|
+
const lines = [
|
|
219
|
+
`Current session: ${current.sessionId}`,
|
|
220
|
+
`Current tab: ${current.tabId}`,
|
|
221
|
+
`Page: ${current.url || 'about:blank'}${quoteTitle(current.title)}`,
|
|
222
|
+
];
|
|
223
|
+
if (status.replacedTabs.length) {
|
|
224
|
+
lines.push(`Replaced: yes. This tab replaced your lost tab${status.replacedTabs.length === 1 ? '' : 's'} ${status.replacedTabs.join(', ')}; keep using tabId ${current.tabId}.`);
|
|
225
|
+
} else if (status.movedFromPin) {
|
|
226
|
+
// Only a move from a tab the run knew: the route the server recorded for a fresh run is its launch route, not one.
|
|
227
|
+
const from = status.movedFrom?.tabId && status.movedFrom.tabId !== current.tabId ? ` from tab ${status.movedFrom.tabId}` : '';
|
|
228
|
+
lines.push(`Replaced: yes. SynaBun moved your run's browser to this tab${from} (a recovery or your own create); keep using tabId ${current.tabId}.`);
|
|
229
|
+
} else {
|
|
230
|
+
lines.push('Replaced: no.');
|
|
231
|
+
}
|
|
232
|
+
if (status.lost && !(status.lost.sessionId === current.sessionId && status.lost.tabId === current.tabId)) {
|
|
233
|
+
lines.push(`Your tab ${status.lost.tabId || '(none)'} of session ${status.lost.sessionId} went away; your next browser call may open a new tab and will say so.`);
|
|
234
|
+
}
|
|
235
|
+
return lines.join('\n');
|
|
132
236
|
}
|
|
@@ -124,6 +124,9 @@ PRECONDITION: a logged-in bsky.app tab must be the target (the session token is
|
|
|
124
124
|
export const browserCheatsheetSchema = {
|
|
125
125
|
platform: z.enum(PLATFORMS).describe('Target platform to fetch selectors for.'),
|
|
126
126
|
action: z.enum(ACTIONS).optional().describe('Optional: filter to a single action (click/type/fill/scroll/snapshot/extract/evaluate). If omitted, returns every action known for the platform.'),
|
|
127
|
+
// Clients that pass their route to every browser tool (Codex) send these too; a lookup opens no page, so they are accepted and ignored.
|
|
128
|
+
sessionId: z.string().optional().describe('Ignored: the cheatsheet opens no page. Accepted so a call that names your browser route is not flagged.'),
|
|
129
|
+
tabId: z.string().optional().describe('Ignored: the cheatsheet opens no page. Accepted so a call that names your browser route is not flagged.'),
|
|
127
130
|
};
|
|
128
131
|
|
|
129
132
|
export const browserCheatsheetDescription =
|
|
@@ -9,7 +9,7 @@ import { text } from './response.js';
|
|
|
9
9
|
|
|
10
10
|
export const moreloginSchema = {
|
|
11
11
|
action: z.enum(['status', 'list', 'create', 'start', 'stop', 'use_default']).describe(
|
|
12
|
-
'MoreLogin action. "status" = installed/running + current default. "list" = browser profiles (environments). "create" = new profile. "start"/"stop" = launch/close a profile. "use_default" = make a profile the default AI browser so every browser_* tool drives it.'
|
|
12
|
+
'MoreLogin action. "status" = installed/running + current default. "list" = browser profiles (environments). "create" = new profile. "start"/"stop" = launch/close a profile (stop closes every tab in that browser: you may stop only a profile you started or have a tab in, and never while other automations have a tab in it). "use_default" = make a profile the default AI browser so every browser_* tool drives it (refused while other automations work in the browser). "start" and "use_default" are for a run SynaBun started; the owner does the rest in Settings → Browser.'
|
|
13
13
|
),
|
|
14
14
|
envId: z.string().optional().describe(
|
|
15
15
|
'MoreLogin environment (profile) id — required for start, stop, and use_default. Get ids from the "list" action.'
|
|
@@ -53,7 +53,9 @@ export async function handleMoreLogin(args: { action: string; envId?: string; na
|
|
|
53
53
|
if (!args.envId) return text('envId is required for the "start" action. Use "list" to get ids.');
|
|
54
54
|
const r = await ni.moreloginStart(args.envId);
|
|
55
55
|
if (r.error) return text(`Start failed: ${r.error}`);
|
|
56
|
-
|
|
56
|
+
// The owner's page gets the profile's debugging port; an automation does not (a raw CDP connection would drive
|
|
57
|
+
// the profile past every check SynaBun makes), and needs none: the browser tools drive the default profile.
|
|
58
|
+
return text(`Started MoreLogin env ${args.envId}${r.debugPort ? ` (debug port ${r.debugPort})` : ''}.`);
|
|
57
59
|
}
|
|
58
60
|
case 'stop': {
|
|
59
61
|
if (!args.envId) return text('envId is required for the "stop" action.');
|
|
@@ -69,9 +69,9 @@ export async function pollFor<T = unknown>(
|
|
|
69
69
|
export async function safeNavigate(
|
|
70
70
|
r: Resolved,
|
|
71
71
|
url: string,
|
|
72
|
-
): Promise<{ error?: string; sessionInvalidated?: boolean }> {
|
|
72
|
+
): Promise<{ error?: string; sessionInvalidated?: boolean; wedgedTabRetired?: boolean }> {
|
|
73
73
|
let res = await ni.navigate(r.sessionId, url, r.tabId);
|
|
74
|
-
if (res.error && !res.sessionInvalidated && /Timeout/i.test(res.error)) {
|
|
74
|
+
if (res.error && !res.sessionInvalidated && /Timeout/i.test(res.error) && !res.wedgedTabRetired) {
|
|
75
75
|
await wait(600);
|
|
76
76
|
res = await ni.navigate(r.sessionId, url, r.tabId);
|
|
77
77
|
}
|