@tomflow/chatloop-tools 0.9.21 → 0.9.23
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/README.md +3 -1
- package/node_modules/@tomflow/chatloop-context/package.json +3 -3
- package/node_modules/@tomflow/chatloop-extension/README.md +94 -0
- package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +11 -0
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +141 -0
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +109 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +120 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +180 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +322 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +378 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +54 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +20 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +69 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +287 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +23 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +16 -0
- package/node_modules/@tomflow/chatloop-extension/extension/clean.html +5 -0
- package/node_modules/@tomflow/chatloop-extension/extension/content.js +40 -0
- package/node_modules/@tomflow/chatloop-extension/extension/continue.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/dom.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +275 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +76 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +107 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +36 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +227 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +144 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +171 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +154 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +254 -0
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +42 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.js +93 -0
- package/node_modules/@tomflow/chatloop-extension/extension/reload.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/start.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/status.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/text.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +136 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +220 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +122 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +155 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +185 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +120 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +172 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +165 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +244 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +181 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +147 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +197 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +200 -0
- package/node_modules/@tomflow/chatloop-extension/index.mjs +71 -0
- package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +18 -0
- package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +74 -0
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +208 -0
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +40 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +111 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +67 -0
- package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +120 -0
- package/node_modules/@tomflow/chatloop-extension/package.json +38 -0
- package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +133 -0
- package/node_modules/@tomflow/chatloop-trace/README.md +10 -3
- package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +4 -2
- package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +109 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +12 -5
- package/node_modules/@tomflow/chatloop-trace/package.json +1 -1
- package/node_modules/ps-list/index.d.ts +76 -0
- package/node_modules/ps-list/index.js +366 -0
- package/node_modules/ps-list/license +9 -0
- package/node_modules/ps-list/package.json +44 -0
- package/node_modules/ps-list/readme.md +56 -0
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
- package/package.json +9 -5
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
// Dedicated first-Turn Action. Only this page creates and sends the initial Web Chat.
|
|
2
|
+
// All subsequent turns are owned by the existing Observer / Continue / Handoff flow.
|
|
3
|
+
const info=document.getElementById('details'),label=document.getElementById('status');
|
|
4
|
+
const pause=ms=>new Promise(resolve=>setTimeout(resolve,ms));
|
|
5
|
+
// Timeout only read-only Chrome probes. Never race/retry a composer write or click.
|
|
6
|
+
const boundedRead=(call,ms)=>Promise.race([
|
|
7
|
+
Promise.resolve().then(call).catch(()=>null),
|
|
8
|
+
pause(Math.max(1,ms)).then(()=>null)
|
|
9
|
+
]);
|
|
10
|
+
const show=(s,d)=>{label.textContent=s;info.textContent=d;document.body.dataset.result=s==='SUCCESS'?'success':'error';};
|
|
11
|
+
const host='com.tomflow.chatloop.extension';
|
|
12
|
+
async function waitForHomeUrl(id){
|
|
13
|
+
const deadline=Date.now()+12_000;
|
|
14
|
+
for(let i=0;i<40 && Date.now()<deadline;i++){
|
|
15
|
+
const tab=await boundedRead(()=>chrome.tabs.get(id),
|
|
16
|
+
Math.min(1200,deadline-Date.now()));
|
|
17
|
+
if(tab?.url==='https://chatgpt.com/')return;
|
|
18
|
+
await pause(Math.min(250,Math.max(1,deadline-Date.now())));
|
|
19
|
+
}
|
|
20
|
+
throw Error('START_HOME_URL_UNVERIFIED');
|
|
21
|
+
}
|
|
22
|
+
async function newChatReady(id){
|
|
23
|
+
// Preserve 35s+ cold hydration but return well before the Worker watchdog.
|
|
24
|
+
// A never-settling Chrome read must not leave Start PENDING indefinitely.
|
|
25
|
+
const deadline=Date.now()+90_000;
|
|
26
|
+
for(let i=0;i<200 && Date.now()<deadline;i++){
|
|
27
|
+
const tab=await boundedRead(()=>chrome.tabs.get(id),
|
|
28
|
+
Math.min(1500,deadline-Date.now()));
|
|
29
|
+
if(tab?.url==='https://chatgpt.com/'){
|
|
30
|
+
const result=await boundedRead(()=>chrome.scripting.executeScript({target:{tabId:id},func:()=>{
|
|
31
|
+
const visible=e=>Boolean(e?.getClientRects?.().length);
|
|
32
|
+
const mains=[...document.querySelectorAll('main')].filter(visible);
|
|
33
|
+
const editor=document.querySelector('[contenteditable="true"][data-composer-markdown]')
|
|
34
|
+
||document.querySelector('#prompt-textarea[contenteditable="true"]');
|
|
35
|
+
if(location.origin!=='https://chatgpt.com'||location.pathname!=='/'
|
|
36
|
+
||mains.length!==1||!visible(editor)
|
|
37
|
+
||mains[0].querySelector('[data-chatgpt-search-message-ids],[data-message-author-role]'))
|
|
38
|
+
return false;
|
|
39
|
+
// A visible account-switch notice invalidates the first-send
|
|
40
|
+
// environment. Catch it before any editor mutation or send click.
|
|
41
|
+
const switched=/你的账户已切换[。.!!\s]*请重新发送消息|your account (?:has been |was )?switched[.!\s]*please (?:re-?send|send again) (?:your )?message/i;
|
|
42
|
+
if(switched.test(mains[0].innerText||mains[0].textContent||''))
|
|
43
|
+
return 'START_ACCOUNT_SWITCH_VISIBLE';
|
|
44
|
+
return (editor.innerText||editor.textContent||'').trim()
|
|
45
|
+
?'START_COMPOSER_NOT_EMPTY':true;
|
|
46
|
+
}}),Math.min(1500,deadline-Date.now()));
|
|
47
|
+
if(result?.[0]?.result==='START_COMPOSER_NOT_EMPTY')
|
|
48
|
+
throw Error('START_COMPOSER_NOT_EMPTY');
|
|
49
|
+
if(result?.[0]?.result==='START_ACCOUNT_SWITCH_VISIBLE')
|
|
50
|
+
throw Error('START_ACCOUNT_SWITCH_VISIBLE');
|
|
51
|
+
if(result?.[0]?.result===true)return;
|
|
52
|
+
}
|
|
53
|
+
await pause(Math.min(420,Math.max(1,deadline-Date.now())));
|
|
54
|
+
}
|
|
55
|
+
throw Error('NEW_CHAT_NOT_READY');
|
|
56
|
+
}
|
|
57
|
+
async function observerReady(id){
|
|
58
|
+
let proof=await boundedRead(()=>chrome.tabs.sendMessage(id,
|
|
59
|
+
{type:'CHAT_LOOP_TURN_OBSERVE_V1'}),3000);
|
|
60
|
+
if(proof?.contract!=='chat-loop.turn-observer.v1'){
|
|
61
|
+
const injected=await boundedRead(()=>chrome.scripting.executeScript({
|
|
62
|
+
target:{tabId:id},files:['content.js', 'loop/observer-inspect.js', 'loop/observer-poll.js', 'loop/observer-recovery.js', 'loop/observer-approval.js', 'loop/observer.js']}),3000);
|
|
63
|
+
if(!injected)throw Error('FIRST_TURN_OBSERVER_UNAVAILABLE');
|
|
64
|
+
proof=await boundedRead(()=>chrome.tabs.sendMessage(id,
|
|
65
|
+
{type:'CHAT_LOOP_TURN_OBSERVE_V1'}),3000);
|
|
66
|
+
}
|
|
67
|
+
if(proof?.contract!=='chat-loop.turn-observer.v1')
|
|
68
|
+
throw Error('FIRST_TURN_OBSERVER_UNAVAILABLE');
|
|
69
|
+
}
|
|
70
|
+
async function sendFirst(id,text){
|
|
71
|
+
const [clicked]=await chrome.scripting.executeScript({target:{tabId:id},
|
|
72
|
+
func:async instruction=>{
|
|
73
|
+
try{
|
|
74
|
+
const visible=e=>Boolean(e?.getClientRects?.().length);
|
|
75
|
+
if(location.origin!=='https://chatgpt.com'||location.pathname!=='/')throw Error('START_PAGE_CHANGED');
|
|
76
|
+
const mains=[...document.querySelectorAll('main')].filter(visible);
|
|
77
|
+
const editor=document.querySelector('[contenteditable="true"][data-composer-markdown]')
|
|
78
|
+
||document.querySelector('#prompt-textarea[contenteditable="true"]');
|
|
79
|
+
if(mains.length!==1||!visible(editor)||!(editor.isContentEditable)||!document.contains(editor)
|
|
80
|
+
||(editor.innerText||editor.textContent||'').trim()
|
|
81
|
+
||mains[0].querySelector('[data-chatgpt-search-message-ids],[data-message-author-role]'))
|
|
82
|
+
throw Error('START_COMPOSER_UNVERIFIED');
|
|
83
|
+
// Recheck immediately before changing the composer; account
|
|
84
|
+
// identity may have switched after the initial readiness probe.
|
|
85
|
+
const switched=/你的账户已切换[。.!!\s]*请重新发送消息|your account (?:has been |was )?switched[.!\s]*please (?:re-?send|send again) (?:your )?message/i;
|
|
86
|
+
if(switched.test(mains[0].innerText||mains[0].textContent||''))
|
|
87
|
+
throw Error('START_ACCOUNT_SWITCH_VISIBLE');
|
|
88
|
+
const canonical=v=>String(v||'').replace(/\s+/g,'');
|
|
89
|
+
editor.focus();
|
|
90
|
+
if(!document.execCommand('insertText',false,instruction))throw Error('START_INPUT_REJECTED');
|
|
91
|
+
let verified=false;
|
|
92
|
+
for(let i=0;i<25;i++){
|
|
93
|
+
if(location.pathname!=='/')throw Error('START_PAGE_CHANGED_DURING_INPUT');
|
|
94
|
+
if(canonical(editor.innerText)===canonical(instruction)
|
|
95
|
+
||canonical(editor.textContent)===canonical(instruction)){verified=true;break;}
|
|
96
|
+
await new Promise(r=>setTimeout(r,120));
|
|
97
|
+
}
|
|
98
|
+
if(!verified)throw Error('START_INPUT_UNVERIFIED');
|
|
99
|
+
let send;
|
|
100
|
+
for(let i=0;i<16;i++){
|
|
101
|
+
send=document.querySelector('button[data-testid="send-button"]')
|
|
102
|
+
||document.querySelector('button[data-testid="composer-send-button"]')
|
|
103
|
+
||document.querySelector('button[type="submit"][aria-label="发送"]')
|
|
104
|
+
||document.querySelector('button[aria-label="Send message"]');
|
|
105
|
+
if(send&&!send.disabled&&visible(send))break;
|
|
106
|
+
await new Promise(r=>setTimeout(r,120));
|
|
107
|
+
}
|
|
108
|
+
if(!send||send.disabled||!visible(send))throw Error('START_SEND_BUTTON_UNAVAILABLE');
|
|
109
|
+
if(location.pathname!=='/'||(canonical(editor.innerText)!==canonical(instruction)
|
|
110
|
+
&&canonical(editor.textContent)!==canonical(instruction)))
|
|
111
|
+
throw Error('START_INPUT_CHANGED');
|
|
112
|
+
send.click(); // Once only. Worker persisted PENDING before this point.
|
|
113
|
+
return {clickedOnce:true};
|
|
114
|
+
}catch(e){return {clickedOnce:false,reason:e.message||'START_INPUT_UNKNOWN'};}
|
|
115
|
+
},args:[text]});
|
|
116
|
+
if(clicked?.result?.clickedOnce!==true)
|
|
117
|
+
throw Error(clicked?.result?.reason||'START_CLICK_UNVERIFIED');
|
|
118
|
+
// Once clicked: ONLY bounded read-only receipt probes. A provisional
|
|
119
|
+
// local-chatgpt URL can settle late; never retry typing or clicking.
|
|
120
|
+
// Keep even the worst-case read budget below the 330s Worker watchdog,
|
|
121
|
+
// including the preceding 90s cold-page readiness window.
|
|
122
|
+
const deadline=Date.now()+120_000;
|
|
123
|
+
// E15 must verify a single, stable server-backed first user Turn.
|
|
124
|
+
// One transient bubble is not proof that ChatGPT emitted exactly one Turn.
|
|
125
|
+
let candidatePage=null,candidateUserKey=null,stableProbes=0;
|
|
126
|
+
for(let i=0;i<200&&Date.now()<deadline;i++){
|
|
127
|
+
const tab=await boundedRead(()=>chrome.tabs.get(id),
|
|
128
|
+
Math.min(1200,Math.max(1,deadline-Date.now())));
|
|
129
|
+
const page=tab?.url&&new URL(tab.url).pathname;
|
|
130
|
+
if(/^\/c\/[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(page||'')){
|
|
131
|
+
const rows=await boundedRead(()=>chrome.scripting.executeScript({target:{tabId:id},func:expected=>{
|
|
132
|
+
const visible=e=>Boolean(e?.getClientRects?.().length);
|
|
133
|
+
const mains=[...document.querySelectorAll('main')].filter(visible);
|
|
134
|
+
if(mains.length!==1)return null;
|
|
135
|
+
const modern=[...mains[0].querySelectorAll('[data-chatgpt-search-unit-key][data-chatgpt-search-message-ids]')];
|
|
136
|
+
const rows=modern.length?modern:[...mains[0].querySelectorAll('[data-message-author-role]')];
|
|
137
|
+
const role=n=>n.getAttribute('data-message-author-role')
|
|
138
|
+
||n.getAttribute('data-chatgpt-search-unit-key')?.split(':').at(-1);
|
|
139
|
+
const users=rows.filter(n=>role(n)==='user');
|
|
140
|
+
const user=users.at(-1);
|
|
141
|
+
const idOf=node=>node?.getAttribute('data-message-id')
|
|
142
|
+
||node?.getAttribute('data-chatgpt-search-message-ids')?.trim().split(/\s+/)[0];
|
|
143
|
+
const ids=users.map(idOf);
|
|
144
|
+
const id=idOf(user);
|
|
145
|
+
const bubble=user?.querySelector('[data-search-result-target]')
|
|
146
|
+
||user?.querySelector('[data-user-message-bubble="true"]')||user;
|
|
147
|
+
const canonical=v=>String(v||'').replace(/\s+/g,'');
|
|
148
|
+
return {observedPage:location.pathname,userMessageCount:users.length,
|
|
149
|
+
distinctUserIds:new Set(ids).size,
|
|
150
|
+
userKey:id&&canonical(bubble?.innerText||bubble?.textContent)===canonical(expected)
|
|
151
|
+
?'user:'+id:null};
|
|
152
|
+
},args:[text]}),Math.min(1200,Math.max(1,deadline-Date.now())));
|
|
153
|
+
const evidence=rows?.[0]?.result;
|
|
154
|
+
if(evidence?.observedPage&&evidence.observedPage!==page)
|
|
155
|
+
throw Error('FIRST_SEND_PAGE_DRIFT_UNVERIFIED');
|
|
156
|
+
if(evidence?.userMessageCount>1||evidence?.distinctUserIds>1)
|
|
157
|
+
throw Error('FIRST_SEND_DUPLICATE_UNVERIFIED');
|
|
158
|
+
const exactOne=evidence?.userMessageCount===1
|
|
159
|
+
&&evidence.distinctUserIds===1&&typeof evidence.userKey==='string';
|
|
160
|
+
if(exactOne){
|
|
161
|
+
if(candidatePage===page&&candidateUserKey===evidence.userKey)stableProbes++;
|
|
162
|
+
else {candidatePage=page;candidateUserKey=evidence.userKey;stableProbes=1;}
|
|
163
|
+
// Four independent bounded DOM probes (~1.2s in Chrome). Never
|
|
164
|
+
// claim SENT_CONFIRMED from the first transient matching bubble.
|
|
165
|
+
if(stableProbes>=4)return {status:'SENT_CONFIRMED',
|
|
166
|
+
newUserKey:candidateUserKey,newPage:page,newTabId:id};
|
|
167
|
+
}else if(evidence){candidatePage=null;candidateUserKey=null;stableProbes=0;}
|
|
168
|
+
}
|
|
169
|
+
await pause(Math.min(420,Math.max(1,deadline-Date.now())));
|
|
170
|
+
}
|
|
171
|
+
throw Error('FIRST_SEND_RECEIPT_UNKNOWN');
|
|
172
|
+
}
|
|
173
|
+
async function run(){
|
|
174
|
+
const own=await chrome.tabs.getCurrent();
|
|
175
|
+
const q=new URLSearchParams(location.search),id=q.get('requestId');
|
|
176
|
+
if(!own?.id||own.url!==location.href||[...q.keys()].length!==1
|
|
177
|
+
||!/^[a-f0-9]{32}$/.test(id||''))throw Error('START_ACTION_SOURCE_INVALID');
|
|
178
|
+
const proof=await chrome.runtime.sendNativeMessage(host,{op:'acquire',id,action:'start'});
|
|
179
|
+
if(proof?.status!=='OK'||proof.action!=='start'||proof.id!==id
|
|
180
|
+
||typeof proof.nonce!=='string')throw Error(proof?.code||'START_NATIVE_INVALID');
|
|
181
|
+
let prepared,taken,newTab,committed;
|
|
182
|
+
try{
|
|
183
|
+
prepared=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_PREPARE_V1',
|
|
184
|
+
requestId:id,...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
185
|
+
if(prepared?.status!=='PREPARED')throw Error(prepared?.code||'START_PREPARE_DENIED');
|
|
186
|
+
taken=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_TAKE_V1',
|
|
187
|
+
requestId:id,token:prepared.token,
|
|
188
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
189
|
+
if(taken?.status!=='OK'||taken.sessionId!==prepared.sessionId
|
|
190
|
+
||(taken.mode==='adopt'
|
|
191
|
+
? !Number.isSafeInteger(taken.tabId)||!/^\/c\/[0-9a-f-]{12,}$/i.test(taken.page||'')
|
|
192
|
+
&& !/^\/g\/[^/]+\/c\/[0-9a-f-]{12,}$/i.test(taken.page||'')
|
|
193
|
+
||typeof taken.userKey!=='string'
|
|
194
|
+
: typeof taken.prompt!=='string'))
|
|
195
|
+
throw Error(taken?.code||'START_TAKE_DENIED');
|
|
196
|
+
if(taken.mode==='adopt'){
|
|
197
|
+
const existing=await chrome.tabs.get(taken.tabId).catch(()=>null);
|
|
198
|
+
if(!existing || new URL(existing.url).pathname!==taken.page)
|
|
199
|
+
throw Error('ADOPTION_TAB_UNVERIFIED');
|
|
200
|
+
await observerReady(existing.id);
|
|
201
|
+
const repair=taken.purpose==='REPAIR_SAME_SCOPE';
|
|
202
|
+
const evidence=await chrome.tabs.sendMessage(existing.id,
|
|
203
|
+
{type:repair?'CHAT_LOOP_REPAIR_EVIDENCE_V1':'CHAT_LOOP_APPROVAL_EVIDENCE_V1',
|
|
204
|
+
...(taken.explicitChatId
|
|
205
|
+
?{explicitChatId:taken.explicitChatId}:{}),
|
|
206
|
+
...(taken.expectedTaskId
|
|
207
|
+
?{expectedTaskId:taken.expectedTaskId}:{})}).catch(()=>null);
|
|
208
|
+
if(evidence?.contract!=='chat-loop.turn-observer.v1'
|
|
209
|
+
||evidence.status!=='VERIFIED'||evidence.page!==taken.page
|
|
210
|
+
||evidence.userKey!==taken.userKey
|
|
211
|
+
||(repair
|
|
212
|
+
? evidence.userText!==taken.repairUserText
|
|
213
|
+
||evidence.priorDoneAssistantKey!==taken.priorDoneAssistantKey
|
|
214
|
+
: evidence.rVersion!==taken.rVersion
|
|
215
|
+
||evidence.pVersion!==taken.pVersion
|
|
216
|
+
||(evidence.priorAssistantKey||null)!==(taken.priorAssistantKey||null)
|
|
217
|
+
||(evidence.postApprovalAssistantKey||null)!==(taken.postApprovalAssistantKey||null)))
|
|
218
|
+
throw Error('ADOPTION_APPROVAL_CHANGED');
|
|
219
|
+
const registered=await chrome.runtime.sendMessage({type:'CHAT_LOOP_RESOURCE_CHAT_V1',
|
|
220
|
+
action:'start',stage:'adopt',tabId:existing.id});
|
|
221
|
+
if(registered?.status!=='OK')throw Error(registered?.code||'ADOPTION_RESOURCE_UNVERIFIED');
|
|
222
|
+
// No tabs.create, no composer input, no send-button click in this branch.
|
|
223
|
+
committed=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
|
|
224
|
+
requestId:id,token:prepared.token,result:'ADOPTED_CONFIRMED',
|
|
225
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{}),
|
|
226
|
+
newTabId:existing.id,newPage:taken.page,newUserKey:taken.userKey});
|
|
227
|
+
if(committed?.status!=='OK')throw Error(committed?.code||'ADOPTION_COMMIT_UNVERIFIED');
|
|
228
|
+
await chrome.tabs.sendMessage(existing.id,{type:'CHAT_LOOP_TURN_RESUME_V1',
|
|
229
|
+
page:taken.page,userKey:taken.userKey,
|
|
230
|
+
assistantKey:taken.postApprovalAssistantKey||taken.priorAssistantKey||null,
|
|
231
|
+
...(taken.postApprovalAssistantKey?{baselineOnly:true}:{}),
|
|
232
|
+
startedAt:Date.now()}).catch(()=>null);
|
|
233
|
+
const stored=await chrome.runtime.sendNativeMessage(host,{op:'complete',
|
|
234
|
+
id,nonce:proof.nonce,action:'start',status:'OK',payload:JSON.stringify(committed)});
|
|
235
|
+
if(stored?.status!=='SAVED')throw Error(stored?.code||'ADOPTION_RECEIPT_UNVERIFIED');
|
|
236
|
+
show('SUCCESS','已接管完成 R/P 确认的原 Chat;没有新建会话或重新发送消息。');
|
|
237
|
+
await chrome.tabs.remove(own.id);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const quota=await chrome.runtime.sendMessage({type:'CHAT_LOOP_RESOURCE_CHAT_V1',
|
|
241
|
+
action:'start',stage:'preflight'});
|
|
242
|
+
if(quota?.status!=='OK')throw Error(quota?.code||'RESOURCE_QUOTA_UNVERIFIED');
|
|
243
|
+
show('RUNNING','已独占会话,正在创建唯一网页 Chat 并确认首轮发送。');
|
|
244
|
+
newTab=await chrome.tabs.create({url:'https://chatgpt.com/',active:true,
|
|
245
|
+
windowId:own.windowId,openerTabId:own.id});
|
|
246
|
+
await waitForHomeUrl(newTab.id);
|
|
247
|
+
const registered=await chrome.runtime.sendMessage({type:'CHAT_LOOP_RESOURCE_CHAT_V1',
|
|
248
|
+
action:'start',stage:'register',tabId:newTab.id});
|
|
249
|
+
if(registered?.status!=='OK')throw Error(registered?.code||'RESOURCE_REGISTER_UNVERIFIED');
|
|
250
|
+
await newChatReady(newTab.id);
|
|
251
|
+
await observerReady(newTab.id);
|
|
252
|
+
const receipt=await sendFirst(newTab.id,taken.prompt);
|
|
253
|
+
committed=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
|
|
254
|
+
requestId:id,token:prepared.token,...receipt,result:receipt.status,
|
|
255
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
256
|
+
if(committed?.status!=='OK')throw Error(committed?.code||'START_COMMIT_UNVERIFIED');
|
|
257
|
+
const stored=await chrome.runtime.sendNativeMessage(host,{op:'complete',
|
|
258
|
+
id,nonce:proof.nonce,action:'start',status:'OK',payload:JSON.stringify(committed)});
|
|
259
|
+
if(stored?.status!=='SAVED')throw Error(stored?.code||'START_RECEIPT_NOT_STORED');
|
|
260
|
+
show('SUCCESS','唯一网页 Chat 已确认;执行由 Observer 和 Continue/Handoff 接管。');
|
|
261
|
+
await chrome.tabs.remove(own.id);
|
|
262
|
+
}catch(e){
|
|
263
|
+
if(taken&&committed?.status!=='OK') {
|
|
264
|
+
// This is diagnostics only; it does NOT authorize a retry or prove a
|
|
265
|
+
// message was never sent. Keep the existing fail-closed Owner semantics.
|
|
266
|
+
const failureCode=['START_HOME_URL_UNVERIFIED','NEW_CHAT_NOT_READY',
|
|
267
|
+
'START_COMPOSER_NOT_EMPTY','START_ACCOUNT_SWITCH_VISIBLE',
|
|
268
|
+
'FIRST_TURN_OBSERVER_UNAVAILABLE','START_SEND_BUTTON_UNAVAILABLE','START_INPUT_CHANGED',
|
|
269
|
+
'START_INPUT_UNVERIFIED','START_INPUT_REJECTED',
|
|
270
|
+
'START_CLICK_UNVERIFIED','FIRST_SEND_RECEIPT_UNKNOWN',
|
|
271
|
+
'FIRST_SEND_DUPLICATE_UNVERIFIED','FIRST_SEND_PAGE_DRIFT_UNVERIFIED']
|
|
272
|
+
.includes(e.message) ? e.message : null;
|
|
273
|
+
await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
|
|
274
|
+
requestId:id,token:prepared.token,result:'UNVERIFIED',newTabId:newTab?.id,
|
|
275
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{}),
|
|
276
|
+
...(failureCode?{failureCode}:{})}).catch(()=>{});
|
|
277
|
+
}
|
|
278
|
+
const failed=await chrome.runtime.sendNativeMessage(host,{op:'complete',id,nonce:proof.nonce,
|
|
279
|
+
action:'start',status:'FAILED',code:String(e.message||'START_UNVERIFIED').slice(0,100)}).catch(()=>null);
|
|
280
|
+
// Only the Action page closes after its durable FAILED receipt; the
|
|
281
|
+
// possibly-sent Chat and any unverified draft remain untouched.
|
|
282
|
+
if(failed?.status==='SAVED' && failed.id===id)
|
|
283
|
+
await chrome.tabs.remove(own.id).catch(()=>{});
|
|
284
|
+
throw e; // Never auto-retry a possibly clicked first user Turn.
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
void run().catch(e=>show('BLOCKED',e.message||'START_UNVERIFIED'));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Read-only machine Status. Does not create a Chat or modify the browser Owner.
|
|
2
|
+
const host='com.tomflow.chatloop.extension';
|
|
3
|
+
async function run(){
|
|
4
|
+
const own=await chrome.tabs.getCurrent(),q=new URLSearchParams(location.search);
|
|
5
|
+
const id=q.get('requestId');
|
|
6
|
+
if(!own?.id||own.url!==location.href||[...q.keys()].length!==1
|
|
7
|
+
||!/^[a-f0-9]{32}$/.test(id||''))throw Error('INVALID_STATUS_ACTION');
|
|
8
|
+
const proof=await chrome.runtime.sendNativeMessage(host,{op:'acquire',id,action:'status'});
|
|
9
|
+
if(proof?.status!=='OK'||proof.action!=='status'||proof.id!==id)
|
|
10
|
+
throw Error(proof?.code||'STATUS_NATIVE_INVALID');
|
|
11
|
+
const state=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_STATUS_V1',
|
|
12
|
+
requestId:id,...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
13
|
+
const receipt=await chrome.runtime.sendNativeMessage(host,{op:'complete',id,
|
|
14
|
+
nonce:proof.nonce,action:'status',status:'OK',payload:JSON.stringify(state)});
|
|
15
|
+
if(receipt?.status!=='SAVED')throw Error(receipt?.code||'STATUS_NOT_STORED');
|
|
16
|
+
document.getElementById('status').textContent='SUCCESS';
|
|
17
|
+
document.getElementById('details').textContent='只读状态已返回本机请求;未修改执行 Chat。';
|
|
18
|
+
await chrome.tabs.remove(own.id);
|
|
19
|
+
}
|
|
20
|
+
void run().catch(e=>{
|
|
21
|
+
document.getElementById('status').textContent='UNVERIFIED';
|
|
22
|
+
document.getElementById('details').textContent=e.message||'STATUS_UNKNOWN';
|
|
23
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Chrome MV3 classic service Worker. Shared lexical context; exact order
|
|
2
|
+
// preserves the original single-Worker Owner and serialized Action lifecycle.
|
|
3
|
+
importScripts("loop/index.js");
|
|
4
|
+
importScripts("worker/bootstrap.js");
|
|
5
|
+
importScripts("worker/admission.js");
|
|
6
|
+
importScripts("worker/adopt-prepare.js");
|
|
7
|
+
importScripts("worker/adopt-result.js");
|
|
8
|
+
importScripts("worker/readiness.js");
|
|
9
|
+
importScripts("worker/turn-failure.js");
|
|
10
|
+
importScripts("worker/turn-finished.js");
|
|
11
|
+
importScripts("worker/adopt-recovery.js");
|
|
12
|
+
importScripts("worker/manual.js");
|
|
13
|
+
importScripts("worker/action-take.js");
|
|
14
|
+
importScripts("worker/action-result.js");
|
|
15
|
+
importScripts("worker/scheduler.js");
|
|
16
|
+
importScripts("worker/router.js");
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN"><head><meta charset="utf-8"><title>Chat Loop · Clean</title>
|
|
3
|
+
<style>body{font:14px system-ui;padding:22px;color:#242424}#status{font-weight:bold}#details{color:#666}</style></head>
|
|
4
|
+
<body><h1>Clean · 清理输入框</h1><p id="status">VERIFYING</p><p id="details">正在核实目标 Chat,不发送消息。</p>
|
|
5
|
+
<script src="actions/clean.js"></script></body></html>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Read-only ChatGPT page agent. Reinjection after extension reload is idempotent.
|
|
2
|
+
(() => {
|
|
3
|
+
if (globalThis.__CHAT_LOOP_EYE_V1__) return;
|
|
4
|
+
globalThis.__CHAT_LOOP_EYE_V1__ = true;
|
|
5
|
+
const CONTRACT = "chat-loop.web-environment.v1";
|
|
6
|
+
const READ_CONTRACT = "chat-loop.eye.v1";
|
|
7
|
+
const onChatGPT = () => location.protocol === "https:" && location.hostname === "chatgpt.com";
|
|
8
|
+
|
|
9
|
+
function inspectChatPage() {
|
|
10
|
+
const brands = navigator.userAgentData?.brands;
|
|
11
|
+
const chromeBrowser = Array.isArray(brands)
|
|
12
|
+
&& brands.some(entry => entry?.brand === "Google Chrome");
|
|
13
|
+
return {
|
|
14
|
+
contract: CONTRACT,
|
|
15
|
+
status: !onChatGPT() ? "UNSUPPORTED_PAGE" : !chromeBrowser
|
|
16
|
+
? "BROWSER_UNVERIFIED" : "WEB_READY",
|
|
17
|
+
pageOrigin: onChatGPT() ? "https://chatgpt.com" : null,
|
|
18
|
+
browser: chromeBrowser ? "GOOGLE_CHROME" : "UNVERIFIED",
|
|
19
|
+
extensionVersion: chrome.runtime.getManifest().version
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|
24
|
+
if (message?.type === "CHAT_LOOP_WEB_CHECK_V1") {
|
|
25
|
+
sendResponse(inspectChatPage());
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
if (message?.type !== "CHAT_LOOP_EYE_READ_V1") return false;
|
|
29
|
+
if (!onChatGPT() || !["dom", "text"].includes(message.mode)) {
|
|
30
|
+
sendResponse({contract: READ_CONTRACT, status: "UNSUPPORTED"});
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const value = message.mode === "dom"
|
|
34
|
+
? document.documentElement.outerHTML
|
|
35
|
+
: (document.querySelector("main")?.innerText ?? document.body?.innerText ?? "");
|
|
36
|
+
sendResponse({contract: READ_CONTRACT, status: "OK",
|
|
37
|
+
mode: message.mode, url: location.href, value, length: value.length});
|
|
38
|
+
return false;
|
|
39
|
+
});
|
|
40
|
+
})();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN"><head><meta charset="utf-8"><title>Chat Loop Extension · Continue Action</title>
|
|
3
|
+
<style>body{font:15px system-ui,sans-serif;padding:24px;max-width:620px;color:#222}#status{font-weight:bold}#details{white-space:pre-wrap;overflow-wrap:anywhere}body[data-result="error"] #status{color:#b00020}</style>
|
|
4
|
+
</head><body>
|
|
5
|
+
<h1>Chat Loop · Continue</h1>
|
|
6
|
+
<p id="status" role="status">STARTING</p>
|
|
7
|
+
<p id="details">仅执行一次已授权的固定续接。失败时保留本页,不重新发送。</p>
|
|
8
|
+
<script src="actions/continue.js"></script>
|
|
9
|
+
</body></html>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>Chat Loop Extension · DOM</title>
|
|
6
|
+
<style>body{font:15px system-ui,sans-serif;padding:24px;max-width:620px;color:#222}
|
|
7
|
+
#status{font-weight:700}#details{white-space:pre-wrap;overflow-wrap:anywhere;color:#555}
|
|
8
|
+
body[data-result="error"] #status{color:#b00020}</style>
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<h1>Chat Loop · DOM</h1>
|
|
12
|
+
<p id="status" role="status">STARTING</p>
|
|
13
|
+
<p id="details">正在执行,成功后关闭此页面;失败则保留错误以供诊断。</p>
|
|
14
|
+
<script src="actions/common.js"></script>
|
|
15
|
+
<script src="actions/dom.js"></script>
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN"><head><meta charset="utf-8"><title>Chat Loop · Handoff Action</title>
|
|
3
|
+
<style>body{font:15px system-ui,sans-serif;padding:24px;max-width:620px}#status{font-weight:700}#details{white-space:pre-wrap;overflow-wrap:anywhere}body[data-result="error"] #status{color:#b00020}</style>
|
|
4
|
+
</head><body>
|
|
5
|
+
<h1>Chat Loop · Handoff</h1>
|
|
6
|
+
<p id="status" role="status">STARTING</p>
|
|
7
|
+
<p id="details">仅在旧 Chat 已完成并给出明确交接包时,创建一次新 Chat 并发送一次。未知结果禁止重试。</p>
|
|
8
|
+
<script src="actions/handoff.js"></script>
|
|
9
|
+
</body></html>
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// GENERATED by scripts/build-actions.mjs — do not edit.
|
|
2
|
+
globalThis.ChatLoopEyeCatalogV1=Object.freeze({"chat_text":{"page":"text.html","kind":"eye-read","readMode":"text"},"chat_dom":{"page":"dom.html","kind":"eye-read","readMode":"dom"},"chat_screenshot":{"page":"screenshot.html","kind":"eye-screenshot","readMode":null},"reload":{"page":"reload.html","kind":"eye-reload","readMode":null}});
|