webhelm 1.0.48 → 1.0.50

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.
@@ -1,93 +0,0 @@
1
- import { waitUntilStable } from "./readiness.mjs";
2
- export const SEND_ENABLED_JS =
3
- `(() => { const buttons=[...document.querySelectorAll('button[data-testid="send-button"],button[aria-label="Send prompt"],button[aria-label="Send"],form button[type="submit"]')]; const b=buttons.find(e=>e.getClientRects().length>0); return !!b && !b.disabled && b.getAttribute('aria-disabled') !== 'true'; })()`;
4
- // Only post-click Stop visibility acknowledges queue submission.
5
- // The same selector family is used to detect ongoing generation before a send.
6
- export const STOP_VISIBLE_JS = `(() => {
7
- const visible = b => !!b?.getClientRects().length && getComputedStyle(b).visibility !== 'hidden';
8
- return [...document.querySelectorAll('button')].some(b => {
9
- if (!visible(b)) return false;
10
- const label=(b.getAttribute('aria-label')||'').trim().toLowerCase();
11
- const title=(b.getAttribute('title')||'').trim().toLowerCase();
12
- const testId=(b.getAttribute('data-testid')||'').trim().toLowerCase();
13
- return ['stop generating','stop answering','stop response','stop'].includes(label) ||
14
- ['stop generating','stop answering'].includes(title) ||
15
- ['stop-button','stop-generating-button'].includes(testId);
16
- });
17
- })()`;
18
- export const CLICK_SEND_JS =
19
- `(() => { const visible=e=>!!e&&!!e.getClientRects().length; const running=[...document.querySelectorAll('button')].some(b=>{ if(!visible(b))return false; const l=(b.getAttribute('aria-label')||'').trim().toLowerCase(),t=(b.getAttribute('title')||'').trim().toLowerCase(),d=(b.getAttribute('data-testid')||'').trim().toLowerCase(); return ['stop generating','stop answering','stop response','stop'].includes(l)||['stop generating','stop answering'].includes(t)||['stop-button','stop-generating-button'].includes(d); }); if(running)return false; const b=document.querySelector('button[data-testid="send-button"],button[aria-label="Send prompt"],button[aria-label="Send"],form button[type="submit"]'); if(!b||b.disabled||b.getAttribute('aria-disabled')==='true')return false; b.click(); return true; })()`;
20
- export function targetUrl(
21
- { project_id, thread_id, temporary = false, prompt } = {},
22
- ) {
23
- if (temporary && (project_id || thread_id)) {
24
- throw new Error(
25
- "temporary cannot be combined with project_id or thread_id",
26
- );
27
- }
28
- const enc = encodeURIComponent;
29
- const base = temporary
30
- ? "https://chatgpt.com/"
31
- : thread_id
32
- ? `https://chatgpt.com${project_id ? `/g/${enc(project_id)}` : ""}/c/${
33
- enc(thread_id)
34
- }`
35
- : project_id
36
- ? `https://chatgpt.com/g/${enc(project_id)}/project`
37
- : "https://chatgpt.com/";
38
- const params = new URLSearchParams();
39
- if (temporary) params.set("temporary-chat", "true");
40
- if (prompt !== undefined) params.set("prompt", prompt);
41
- return base + (params.size ? `?${params}` : "");
42
- }
43
- export function visibleDeliveryError(js) {
44
- return js(
45
- `(() => { const as=[...document.querySelectorAll('[role="alert"],[role="status"],[data-sonner-toast],[data-testid*="toast"],[class*="toast"],[class*="error-banner"]')]; for(const e of as){if(!e.getClientRects().length)continue; const t=(e.innerText||'').toLowerCase(); if(t.includes('this conversation is too long to continue')||t.includes('conversation is too long to continue'))return 'conversation_too_long'; if(t.includes('could not load this chatgpt conversation'))return 'thread_load_failed';} if((document.body?.innerText||'').toLowerCase().includes('could not load this chatgpt conversation'))return 'thread_load_failed'; return null; })()`,
46
- );
47
- }
48
- export async function prefilledSubmit(helpers, prompt, { send = true } = {}) {
49
- const { js } = helpers;
50
- const sendEnabled = async () => !!(await js(SEND_ENABLED_JS));
51
- try {
52
- await waitUntilStable(
53
- async () => ({ send: await sendEnabled() }),
54
- (s) => s.send,
55
- {
56
- timeout: 15,
57
- interval: 0.25,
58
- stableSamples: 3,
59
- phase: "prefilled Send readiness",
60
- sleep: helpers.sleep,
61
- },
62
- );
63
- } catch {
64
- return send
65
- ? Promise.reject(
66
- new Error("URL-prefilled Send button was not observed ready"),
67
- )
68
- : { send_enabled: false };
69
- }
70
- if (!send) return { send_enabled: true };
71
- if (!(await js(CLICK_SEND_JS))) {
72
- throw new Error("URL-prefilled Send became unavailable before click");
73
- }
74
- try {
75
- await waitUntilStable(
76
- async () => ({ send_disabled: !(await sendEnabled()) }),
77
- (s) => s.send_disabled,
78
- {
79
- timeout: 30,
80
- interval: 0.2,
81
- stableSamples: 2,
82
- phase: "post-click Send disabled",
83
- sleep: helpers.sleep,
84
- },
85
- );
86
- } catch {
87
- const e = new Error("Send did not become disabled after click");
88
- e.name = "SubmissionVerificationError";
89
- e.diagnostic = { reason: "post_submit_send_state_unverified" };
90
- throw e;
91
- }
92
- return { verified_by: "send-button-disabled", turn: { id: null } };
93
- }
@@ -1,120 +0,0 @@
1
- export class SubmissionVerificationError extends Error {
2
- constructor(message, diagnostic = {}) {
3
- super(`${message}: ${JSON.stringify(diagnostic)}`);
4
- this.name = "SubmissionVerificationError";
5
- this.diagnostic = diagnostic;
6
- }
7
- }
8
-
9
- export function waitUntilStable(
10
- readState,
11
- ready,
12
- {
13
- timeout = 15,
14
- interval = 0.25,
15
- stableSamples = 2,
16
- phase = "readiness",
17
- sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
18
- } = {},
19
- ) {
20
- return (async () => {
21
- const end = Date.now() + timeout * 1000;
22
- let stable = 0, previous;
23
- while (Date.now() < end) {
24
- let state;
25
- try {
26
- state = await readState();
27
- } catch {
28
- stable = 0;
29
- await sleep(interval * 1000);
30
- continue;
31
- }
32
- if (ready(state)) {
33
- const key = JSON.stringify(state);
34
- if (key === previous) stable++;
35
- else {
36
- previous = key;
37
- stable = 1;
38
- }
39
- if (stable >= stableSamples) return state;
40
- } else {
41
- stable = 0;
42
- previous = undefined;
43
- }
44
- await sleep(interval * 1000);
45
- }
46
- throw new Error(`ChatGPT ${phase} was not observed ready`);
47
- })();
48
- }
49
-
50
- export function promptTextMatches(observed, expected) {
51
- const norm = (value) =>
52
- String(value || "").replace(/\s+/g, " ").replace(/>\s+/g, ">").replace(
53
- /\s+</g,
54
- "<",
55
- ).trim();
56
- const a = norm(observed), b = norm(expected);
57
- if (a.includes(b)) return true;
58
- if (!b) return !a;
59
- const span = Math.min(256, Math.max(48, Math.floor(b.length / 8)));
60
- return a.length >= Math.floor(b.length * 0.9) &&
61
- a.includes(b.slice(0, span)) && a.includes(b.slice(-span));
62
- }
63
-
64
- export function sendReady(state, { prompt, attachments = [] } = {}) {
65
- const button = state?.send;
66
- const enabled = typeof button === "object" ? !!button.enabled : !!button;
67
- if (!enabled) return false;
68
- if (prompt !== undefined && !promptTextMatches(state.text || "", prompt)) {
69
- return false;
70
- }
71
- const names = Array.isArray(state.attachments?.names)
72
- ? state.attachments.names
73
- : [];
74
- return attachments.every((name) => names.includes(name));
75
- }
76
-
77
- export function submissionReceipt(
78
- turns,
79
- beforeCount,
80
- prompt,
81
- composerText,
82
- { allowComposerCleared = false } = {},
83
- ) {
84
- for (const turn of turns.slice(beforeCount)) {
85
- if (promptTextMatches(turn.text, prompt)) {
86
- return { verified_by: "user-turn", turn };
87
- }
88
- }
89
- if (allowComposerCleared && !String(composerText || "").trim()) {
90
- return {
91
- verified_by: "composer-cleared",
92
- turn: { id: null, text: prompt },
93
- };
94
- }
95
- return null;
96
- }
97
-
98
- export function submissionFailureDiagnostic(
99
- turns,
100
- beforeCount,
101
- prompt,
102
- composerText,
103
- uiBefore,
104
- uiAfter,
105
- ) {
106
- return {
107
- accepted: false,
108
- reason: "no_accepted_user_turn",
109
- user_turn_matched: false,
110
- before_user_turn_count: beforeCount,
111
- user_turn_count: turns.length,
112
- composer_populated: !!String(composerText || "").trim(),
113
- composer_text_length: String(composerText || "").trim().length,
114
- ui_changed: uiBefore == null || uiAfter == null
115
- ? null
116
- : JSON.stringify(uiBefore) !== JSON.stringify(uiAfter),
117
- ui_before: uiBefore,
118
- ui_after: uiAfter,
119
- };
120
- }
@@ -1,31 +0,0 @@
1
- import { PROBE_JS } from "./chatgpt-probe.mjs";
2
- import { SEND_ENABLED_JS, STOP_VISIBLE_JS } from "./lifecycle-common.mjs";
3
-
4
- /** Source executed inside a caller-owned WebHelm Session, never a separate tab. */
5
- export function submissionScript(item) {
6
- const selectorJS = "(() => { const selector=\'button[aria-label=\"Send\"],button[data-testid=\"send-button\"],button[aria-label=\"Send prompt\"],form button[type=\"submit\"]\'; for(const b of document.querySelectorAll(\'[data-webhelm-queue-send]\'))b.removeAttribute(\'data-webhelm-queue-send\'); for(const b of document.querySelectorAll(selector)){if(!b.getClientRects().length||b.disabled||b.getAttribute(\'aria-disabled\')===\'true\')continue;b.setAttribute(\'data-webhelm-queue-send\',\'1\');return \'[data-webhelm-queue-send=\"1\"]\';} return null; })()";
7
- return [
8
- "return await (async()=>{",
9
- "const gate=await js("+JSON.stringify(PROBE_JS)+");",
10
- "const actualId=await js("+JSON.stringify("location.pathname.split('/c/')[1]?.split('/')[0]||''")+");",
11
- "if(actualId!=="+JSON.stringify(item.thread_id)+
12
- ")return {diagnostic:'thread_redirect_mismatch'};",
13
- "if(gate?.state!=='READY'||!(await js("+JSON.stringify(SEND_ENABLED_JS)+")))",
14
- " return {diagnostic:'pre_send_gate_not_ready'};",
15
- "if((gate.composer_text||'').replace(/\\s+/g,' ').trim()!=="+
16
- JSON.stringify(item.message.replace(/\s+/g,' ').trim())+")",
17
-
18
- " return {diagnostic:'pre_send_gate_not_ready'};",
19
- "const selector=await js("+JSON.stringify(selectorJS)+");",
20
- "if(!selector)return {diagnostic:'send_control_not_found'};",
21
- "await click(selector);",
22
- // Stay below the WebHelm 30-second Session exec deadline on a missing Stop.
23
- "for(let i=0;i<60;i++){",
24
- " if(await js("+JSON.stringify(STOP_VISIBLE_JS)+"))",
25
- " return {verified_by:'stop-button-visible'};",
26
- " await sleep(250);",
27
- "}",
28
- "return {diagnostic:'post_submit_stop_not_observed_no_resend'};",
29
- "})()",
30
- ].join("\n");
31
- }