Files
wehub-resource-sync 9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

69 lines
2.9 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { CommandExecutionError } from '@jackwener/opencli/errors';
import {
GROK_DOMAIN,
ensureOnGrok,
authRequired,
isLoggedIn,
parseGrokSessionId,
clickConversationMenuItem,
getPinStateFromMenuLabels,
readConversationMenuLabels,
waitForConversationPinState,
} from './utils.js';
const SESSION_HINT = 'Likely login/auth/challenge/session issue in the existing grok.com browser session.';
function defineToggle(name, accessLabels) {
cli({
site: 'grok',
name,
access: 'write',
description: `${name === 'pin' ? 'Pin' : 'Unpin'} a Grok conversation by ID`,
domain: GROK_DOMAIN,
strategy: Strategy.COOKIE,
browser: true,
siteSession: 'persistent',
args: [
{ name: 'id', positional: true, type: 'string', required: true, help: 'Conversation UUID or grok.com/c/<uuid> URL' },
],
columns: ['status', 'id'],
func: async (page, kwargs) => {
const id = parseGrokSessionId(kwargs.id);
await ensureOnGrok(page);
if (!(await isLoggedIn(page))) throw authRequired();
const expectedState = name === 'pin' ? 'pinned' : 'unpinned';
const before = await readConversationMenuLabels(page, id);
if (!before.ok) {
const detail = before.detail ? ` ${before.detail}` : '';
throw new CommandExecutionError(`${before.reason || `Failed to inspect ${name} state.`}${detail}`, SESSION_HINT);
}
if (getPinStateFromMenuLabels(before.labels) === expectedState) {
return [{ status: `already-${expectedState}`, id }];
}
const result = await clickConversationMenuItem(page, id, accessLabels);
if (!result || !result.ok) {
const detail = result?.detail ? ` ${result.detail}` : '';
throw new CommandExecutionError(`${result?.reason || `Failed to ${name} conversation.`}${detail}`, SESSION_HINT);
}
const verified = await waitForConversationPinState(page, id, expectedState);
if (!verified.ok) {
const labels = verified.labels?.length ? ` labels=${JSON.stringify(verified.labels)}` : '';
throw new CommandExecutionError(
`${name} menu item was clicked, but the conversation did not verify as ${expectedState}.${labels}`,
SESSION_HINT,
);
}
return [{ status: name === 'pin' ? 'pinned' : 'unpinned', id }];
},
});
}
// Grok's context menu shows EITHER "置顶" OR "取消置顶" depending on the
// current pin state, never both. We register two commands that bind to
// the matching label so callers can use whichever they want.
defineToggle('pin', ['置顶', 'pin']);
defineToggle('unpin', ['取消置顶', 'unpin']);