@tomflow/chatloop-tools 0.9.64 → 0.9.70
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/node_modules/@tomflow/chatloop-context/README.md +296 -0
- package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +290 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
- package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +261 -0
- package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
- package/node_modules/@tomflow/chatloop-context/index.mjs +238 -0
- package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
- package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +77 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +2036 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +69 -0
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +338 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +286 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +180 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +68 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +367 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +398 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +264 -0
- package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
- package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
- package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
- package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +118 -0
- package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +316 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +142 -0
- package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
- package/node_modules/@tomflow/chatloop-context/package.json +38 -0
- package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
- package/node_modules/@tomflow/chatloop-extension/README.md +190 -0
- package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +13 -0
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +168 -0
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +246 -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/entry_test.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/session.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 +123 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +183 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +390 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +241 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +399 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +60 -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 +77 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +95 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +329 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +38 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +33 -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/entry_test.html +6 -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 +84 -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 +299 -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 +172 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +211 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +399 -0
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +43 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.js +99 -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/session.html +7 -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 +218 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +127 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +296 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +186 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +126 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +287 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +163 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +345 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +209 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +385 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +75 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-action-v2.js +278 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-recovery-v2.js +52 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-v2.js +93 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-continue-recovery-v2.js +78 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-recovery-v2.js +148 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-v2.js +325 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-hold-recovery-v2.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-dispatch-v2.js +220 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-v2.js +106 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-owner-resolver.js +110 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-recover-v2.js +209 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-routes-v2.js +357 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-run-store.js +400 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-scheduler.js +78 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-start-v2.js +361 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-terminal-v2.js +235 -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 +205 -0
- package/node_modules/@tomflow/chatloop-extension/index.mjs +78 -0
- package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +19 -0
- package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +82 -0
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +378 -0
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +89 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +122 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +75 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-kernel-lock.mjs +106 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-ledger.mjs +316 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-locked-write.mjs +30 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-cancel-v2.mjs +190 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-continue-v2.mjs +214 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-handoff-v2.mjs +338 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-native-control.mjs +398 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-recovery-v2.mjs +234 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-retire-v2.mjs +70 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-terminal-v2.mjs +143 -0
- package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +228 -0
- package/node_modules/@tomflow/chatloop-extension/package.json +36 -0
- package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +138 -0
- package/node_modules/@tomflow/chatloop-trace/README.md +118 -0
- package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
- package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
- package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
- package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
- package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +308 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +140 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +236 -0
- package/node_modules/@tomflow/chatloop-trace/package.json +28 -0
- package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
- 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 +12 -4
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Screenshot Action: capture only the selected ChatGPT tab; wait for download completion.
|
|
2
|
+
(() => {
|
|
3
|
+
const {show, fail, findTarget, done, requireOwnTab,
|
|
4
|
+
machineBinding, machineTarget, machineResult, machineFailure} = globalThis.ChatLoopActionCommonV1;
|
|
5
|
+
async function saveScreenshot(tab,machine) {
|
|
6
|
+
await chrome.windows.update(tab.windowId, {focused: true});
|
|
7
|
+
await chrome.tabs.update(tab.id, {active: true});
|
|
8
|
+
// captureVisibleTab may otherwise return a stale compositor frame from an
|
|
9
|
+
// unfocused window even when tabs.query already reports the requested tab.
|
|
10
|
+
const [paint] = await chrome.scripting.executeScript({target: {tabId: tab.id},
|
|
11
|
+
func: async () => {
|
|
12
|
+
await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
|
|
13
|
+
return {visible: document.visibilityState === "visible", url: location.href};
|
|
14
|
+
}});
|
|
15
|
+
if (paint?.result?.visible !== true || paint.result.url !== tab.url)
|
|
16
|
+
throw Error("TARGET_PAINT_UNVERIFIED");
|
|
17
|
+
await new Promise(resolve => setTimeout(resolve, 600));
|
|
18
|
+
const [active] = await chrome.tabs.query({windowId: tab.windowId, active: true});
|
|
19
|
+
if (active?.id !== tab.id) throw Error("TARGET_NOT_VISIBLE");
|
|
20
|
+
const current = await chrome.tabs.get(tab.id);
|
|
21
|
+
if (current.url !== tab.url) throw Error("TARGET_CHANGED_BEFORE_CAPTURE");
|
|
22
|
+
let dataUrl;
|
|
23
|
+
try { dataUrl = await chrome.tabs.captureVisibleTab(tab.windowId, {format: "png"}); }
|
|
24
|
+
catch (error) {
|
|
25
|
+
const message = String(error?.message || '');
|
|
26
|
+
if (/activeTab|<all_urls>|permission|not allowed to capture/i.test(message))
|
|
27
|
+
throw Error('CAPTURE_PERMISSION_REQUIRED');
|
|
28
|
+
if (/^[A-Z][A-Z0-9_]{1,95}$/.test(message)) throw error;
|
|
29
|
+
throw Error('CAPTURE_API_FAILED');
|
|
30
|
+
}
|
|
31
|
+
if (!dataUrl?.startsWith("data:image/png;base64,")) throw Error("CAPTURE_FAILED");
|
|
32
|
+
if(machine) { await machineResult(machine,dataUrl.slice("data:image/png;base64,".length)); return; }
|
|
33
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
34
|
+
const filename = "ChatLoop/eye/chat-" + tab.id + "-" + stamp + ".png";
|
|
35
|
+
const downloadId = await chrome.downloads.download({
|
|
36
|
+
url: dataUrl, filename, saveAs: false, conflictAction: "uniquify"
|
|
37
|
+
});
|
|
38
|
+
if (!Number.isInteger(downloadId)) throw Error("DOWNLOAD_NOT_STARTED");
|
|
39
|
+
await new Promise((resolve, reject) => {
|
|
40
|
+
let settled = false;
|
|
41
|
+
const timer = setTimeout(() => finish("DOWNLOAD_TIMEOUT"), 30000);
|
|
42
|
+
function finish(error) {
|
|
43
|
+
if (settled) return;
|
|
44
|
+
settled = true;
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
chrome.downloads.onChanged.removeListener(onChanged);
|
|
47
|
+
if (error) reject(Error(error));
|
|
48
|
+
else resolve();
|
|
49
|
+
}
|
|
50
|
+
function onChanged(delta) {
|
|
51
|
+
if (delta.id !== downloadId) return;
|
|
52
|
+
if (delta.error?.current) return finish("DOWNLOAD_FAILED: " + delta.error.current);
|
|
53
|
+
if (delta.state?.current === "complete") return finish();
|
|
54
|
+
if (delta.state?.current === "interrupted") return finish("DOWNLOAD_INTERRUPTED");
|
|
55
|
+
}
|
|
56
|
+
chrome.downloads.onChanged.addListener(onChanged);
|
|
57
|
+
void chrome.downloads.search({id: downloadId}).then(items => {
|
|
58
|
+
if (items[0]?.state === "complete") finish();
|
|
59
|
+
if (items[0]?.state === "interrupted") finish("DOWNLOAD_INTERRUPTED");
|
|
60
|
+
}).catch(() => finish("DOWNLOAD_STATUS_UNVERIFIED"));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
let machine;
|
|
64
|
+
async function run() {
|
|
65
|
+
machine = await machineBinding("chat_screenshot");
|
|
66
|
+
const own = requireOwnTab(await chrome.tabs.getCurrent());
|
|
67
|
+
const target = machine ? await machineTarget(machine) : await findTarget(own);
|
|
68
|
+
show("RUNNING", "screenshot → ChatGPT tab " + target.id);
|
|
69
|
+
await saveScreenshot(target,machine);
|
|
70
|
+
await done(own, "screenshot", target);
|
|
71
|
+
}
|
|
72
|
+
run().catch(async error => {
|
|
73
|
+
await machineFailure("chat_screenshot",error);
|
|
74
|
+
fail(error.message || "UNEXPECTED_ERROR",
|
|
75
|
+
"操作未完成;动作页已保留,未对其他 ChatGPT 标签页进行操作。");
|
|
76
|
+
});
|
|
77
|
+
})();
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// One built-in lifecycle Action page. Query string locates a private CLI
|
|
2
|
+
// request; its operation and authorization are read from Native acquire.
|
|
3
|
+
const host='com.tomflow.chatloop.extension';
|
|
4
|
+
const statusNode=document.getElementById('status');
|
|
5
|
+
const detailsNode=document.getElementById('details');
|
|
6
|
+
const show=(status,text)=>{statusNode.textContent=status;
|
|
7
|
+
detailsNode.textContent=text;
|
|
8
|
+
document.body.dataset.result=status==='SUCCESS'?'success'
|
|
9
|
+
:['BLOCKED','UNVERIFIED'].includes(status)?'error':'pending'};
|
|
10
|
+
async function confirmRecoveryClick(intent){
|
|
11
|
+
const button=document.getElementById('confirm-recovery');
|
|
12
|
+
if(!button)throw Error('SCOPED_RECOVERY_CONFIRM_UI_MISSING');
|
|
13
|
+
show('CONFIRMATION_REQUIRED',
|
|
14
|
+
'项目:'+intent.projectId+'\n任务:'+intent.taskId
|
|
15
|
+
+'\n仅恢复原 Chat 的写权;不会创建 Chat 或发送消息。请在本页确认。');
|
|
16
|
+
button.hidden=false;
|
|
17
|
+
// Bound the real-click window below the CLI's 90-second wait.
|
|
18
|
+
// A later Native/Context ACK can still be UNKNOWN: reconcile by request ID.
|
|
19
|
+
const clicked=await new Promise(resolve=>{
|
|
20
|
+
let settled=false;
|
|
21
|
+
const timer=setTimeout(()=>{
|
|
22
|
+
if(settled)return;
|
|
23
|
+
settled=true;button.disabled=true;resolve(false);
|
|
24
|
+
},60_000);
|
|
25
|
+
button.addEventListener('click',event=>{
|
|
26
|
+
if(!event.isTrusted||settled)return;
|
|
27
|
+
settled=true;clearTimeout(timer);button.disabled=true;resolve(true);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
button.hidden=true;
|
|
31
|
+
if(!clicked)throw Error('SCOPED_RECOVERY_CLICK_NOT_RECEIVED');
|
|
32
|
+
}
|
|
33
|
+
async function run(){
|
|
34
|
+
const own=await chrome.tabs.getCurrent(),params=new URLSearchParams(location.search);
|
|
35
|
+
const id=params.get('requestId'),operation=params.get('operation');
|
|
36
|
+
if(!own?.id||own.url!==location.href||[...params.keys()].join(',')!=='requestId,operation'
|
|
37
|
+
||!['cancel','recover-handoff'].includes(operation)
|
|
38
|
+
||!/^[a-f0-9]{32}$/.test(id||''))
|
|
39
|
+
throw Error('INVALID_SESSION_ACTION');
|
|
40
|
+
const acquired=await chrome.runtime.sendNativeMessage(host,{
|
|
41
|
+
op:'acquire',id,action:'session'
|
|
42
|
+
});
|
|
43
|
+
if(acquired?.status!=='OK'||acquired.id!==id
|
|
44
|
+
||acquired.action!=='session'||typeof acquired.nonce!=='string'
|
|
45
|
+
||acquired.intent?.operation!==operation)
|
|
46
|
+
throw Error(acquired?.code||'SESSION_INTENT_UNVERIFIED');
|
|
47
|
+
let result;
|
|
48
|
+
try{
|
|
49
|
+
if(operation==='recover-handoff')await confirmRecoveryClick(acquired.intent);
|
|
50
|
+
show('RUNNING',operation==='cancel'
|
|
51
|
+
?'核验 Session、停止调度并撤销 Context 写权…'
|
|
52
|
+
:'核验原 Handoff 未发送证据及一次性恢复许可;不会发送消息…');
|
|
53
|
+
result=await chrome.runtime.sendMessage({
|
|
54
|
+
type:operation==='recover-handoff'
|
|
55
|
+
?'CHAT_LOOP_TASK_HANDOFF_RECOVER_V2'
|
|
56
|
+
:acquired.intent?.taskFirst===true
|
|
57
|
+
?'CHAT_LOOP_TASK_SESSION_CANCEL_V2':'CHAT_LOOP_SESSION_CONTROL_V1',
|
|
58
|
+
requestId:id,operation
|
|
59
|
+
});
|
|
60
|
+
if(result?.status!=='OK'||result.state!==(operation==='cancel'
|
|
61
|
+
?'CANCELLED_NOT_DELIVERED':'WRITER_RESTORED_NO_SEND'))
|
|
62
|
+
throw Error(result?.code||'SESSION_OPERATION_UNVERIFIED');
|
|
63
|
+
const receipt=await chrome.runtime.sendNativeMessage(host,{
|
|
64
|
+
op:'complete',id,nonce:acquired.nonce,action:'session',
|
|
65
|
+
status:'OK',payload:JSON.stringify(result)
|
|
66
|
+
});
|
|
67
|
+
if(receipt?.status!=='SAVED'||receipt.id!==id)
|
|
68
|
+
throw Error(receipt?.code||'SESSION_RESULT_UNVERIFIED');
|
|
69
|
+
show('SUCCESS',operation==='cancel'
|
|
70
|
+
?'Session 已安全取消;业务任务未交付。'
|
|
71
|
+
:'原 Task Writer 已恢复;本次没有发送 Chat 消息。');
|
|
72
|
+
await chrome.tabs.remove(own.id);
|
|
73
|
+
}catch(error){
|
|
74
|
+
// The Worker may have fenced Context before a transport failure. Never
|
|
75
|
+
// retry a mutation; the original request and persisted receipt are proof.
|
|
76
|
+
const raw=error?.message;
|
|
77
|
+
const code=/^[A-Z][A-Z0-9_]{1,95}$/.test(raw||'')?raw:'SESSION_ACTION_UNVERIFIED';
|
|
78
|
+
if(operation==='recover-handoff'){
|
|
79
|
+
// A permit/bind/Native CAS may already have committed. Never seal
|
|
80
|
+
// an ambiguous recovery as a definitive FAILED Action receipt:
|
|
81
|
+
// preserve this exact signed request for read-only reconciliation.
|
|
82
|
+
show('BLOCKED',code+';仅核查本次原请求,不要重新签发恢复许可。');
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const receipt=await chrome.runtime.sendNativeMessage(host,{
|
|
86
|
+
op:'complete',id,nonce:acquired.nonce,action:'session',
|
|
87
|
+
status:'FAILED',code
|
|
88
|
+
}).catch(()=>null);
|
|
89
|
+
show('BLOCKED',code);
|
|
90
|
+
if(receipt?.status==='SAVED'&&receipt.id===id)
|
|
91
|
+
await chrome.tabs.remove(own.id).catch(()=>{});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
void run().catch(e=>show('UNVERIFIED',
|
|
95
|
+
/^[A-Z][A-Z0-9_]{1,95}$/.test(e?.message||'')?e.message:'SESSION_ACTION_UNVERIFIED'));
|
|
@@ -0,0 +1,329 @@
|
|
|
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){
|
|
115
|
+
const code=e?.code||e?.message;
|
|
116
|
+
return {clickedOnce:false,reason:
|
|
117
|
+
/^[A-Z][A-Z0-9_]{1,95}$/.test(code||'')?code:'START_INPUT_UNKNOWN'};
|
|
118
|
+
}
|
|
119
|
+
},args:[text]});
|
|
120
|
+
if(clicked?.result?.clickedOnce!==true)
|
|
121
|
+
throw Error(clicked?.result?.reason||'START_CLICK_UNVERIFIED');
|
|
122
|
+
// Once clicked: ONLY bounded read-only receipt probes. A provisional
|
|
123
|
+
// local-chatgpt URL can settle late; never retry typing or clicking.
|
|
124
|
+
// Keep even the worst-case read budget below the 330s Worker watchdog,
|
|
125
|
+
// including the preceding 90s cold-page readiness window.
|
|
126
|
+
const deadline=Date.now()+120_000;
|
|
127
|
+
// E15 must verify a single, stable server-backed first user Turn.
|
|
128
|
+
// One transient bubble is not proof that ChatGPT emitted exactly one Turn.
|
|
129
|
+
let candidatePage=null,candidateUserKey=null,stableProbes=0;
|
|
130
|
+
for(let i=0;i<200&&Date.now()<deadline;i++){
|
|
131
|
+
const tab=await boundedRead(()=>chrome.tabs.get(id),
|
|
132
|
+
Math.min(1200,Math.max(1,deadline-Date.now())));
|
|
133
|
+
const page=tab?.url&&new URL(tab.url).pathname;
|
|
134
|
+
if(/^\/c\/[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(page||'')){
|
|
135
|
+
const rows=await boundedRead(()=>chrome.scripting.executeScript({target:{tabId:id},func:expected=>{
|
|
136
|
+
const visible=e=>Boolean(e?.getClientRects?.().length);
|
|
137
|
+
const mains=[...document.querySelectorAll('main')].filter(visible);
|
|
138
|
+
if(mains.length!==1)return null;
|
|
139
|
+
const modern=[...mains[0].querySelectorAll('[data-chatgpt-search-unit-key][data-chatgpt-search-message-ids]')];
|
|
140
|
+
const rows=modern.length?modern:[...mains[0].querySelectorAll('[data-message-author-role]')];
|
|
141
|
+
const role=n=>n.getAttribute('data-message-author-role')
|
|
142
|
+
||n.getAttribute('data-chatgpt-search-unit-key')?.split(':').at(-1);
|
|
143
|
+
const users=rows.filter(n=>role(n)==='user');
|
|
144
|
+
const user=users.at(-1);
|
|
145
|
+
const idOf=node=>node?.getAttribute('data-message-id')
|
|
146
|
+
||node?.getAttribute('data-chatgpt-search-message-ids')?.trim().split(/\s+/)[0];
|
|
147
|
+
const ids=users.map(idOf);
|
|
148
|
+
const id=idOf(user);
|
|
149
|
+
const bubble=user?.querySelector('[data-search-result-target]')
|
|
150
|
+
||user?.querySelector('[data-user-message-bubble="true"]')||user;
|
|
151
|
+
const canonical=v=>String(v||'').replace(/\s+/g,'');
|
|
152
|
+
return {observedPage:location.pathname,userMessageCount:users.length,
|
|
153
|
+
distinctUserIds:new Set(ids).size,
|
|
154
|
+
userKey:id&&canonical(bubble?.innerText||bubble?.textContent)===canonical(expected)
|
|
155
|
+
?'user:'+id:null};
|
|
156
|
+
},args:[text]}),Math.min(1200,Math.max(1,deadline-Date.now())));
|
|
157
|
+
const evidence=rows?.[0]?.result;
|
|
158
|
+
if(evidence?.observedPage&&evidence.observedPage!==page)
|
|
159
|
+
throw Error('FIRST_SEND_PAGE_DRIFT_UNVERIFIED');
|
|
160
|
+
if(evidence?.userMessageCount>1||evidence?.distinctUserIds>1)
|
|
161
|
+
throw Error('FIRST_SEND_DUPLICATE_UNVERIFIED');
|
|
162
|
+
const exactOne=evidence?.userMessageCount===1
|
|
163
|
+
&&evidence.distinctUserIds===1&&typeof evidence.userKey==='string';
|
|
164
|
+
if(exactOne){
|
|
165
|
+
if(candidatePage===page&&candidateUserKey===evidence.userKey)stableProbes++;
|
|
166
|
+
else {candidatePage=page;candidateUserKey=evidence.userKey;stableProbes=1;}
|
|
167
|
+
// Four independent bounded DOM probes (~1.2s in Chrome). Never
|
|
168
|
+
// claim SENT_CONFIRMED from the first transient matching bubble.
|
|
169
|
+
if(stableProbes>=4)return {status:'SENT_CONFIRMED',
|
|
170
|
+
newUserKey:candidateUserKey,newPage:page,newTabId:id};
|
|
171
|
+
}else if(evidence){candidatePage=null;candidateUserKey=null;stableProbes=0;}
|
|
172
|
+
}
|
|
173
|
+
await pause(Math.min(420,Math.max(1,deadline-Date.now())));
|
|
174
|
+
}
|
|
175
|
+
throw Error('FIRST_SEND_RECEIPT_UNKNOWN');
|
|
176
|
+
}
|
|
177
|
+
async function run(){
|
|
178
|
+
const own=await chrome.tabs.getCurrent();
|
|
179
|
+
const q=new URLSearchParams(location.search),id=q.get('requestId');
|
|
180
|
+
if(!own?.id||own.url!==location.href||[...q.keys()].length!==1
|
|
181
|
+
||!/^[a-f0-9]{32}$/.test(id||''))throw Error('START_ACTION_SOURCE_INVALID');
|
|
182
|
+
const proof=await chrome.runtime.sendNativeMessage(host,{op:'acquire',id,action:'start'});
|
|
183
|
+
if(proof?.status!=='OK'||proof.action!=='start'||proof.id!==id
|
|
184
|
+
||typeof proof.nonce!=='string')throw Error(proof?.code||'START_NATIVE_INVALID');
|
|
185
|
+
let prepared,taken,newTab,committed;
|
|
186
|
+
try{
|
|
187
|
+
if(proof.intent?.taskFirst===true){
|
|
188
|
+
// Versioned task-first original Chat Adopt. There is NO composer write,
|
|
189
|
+
// send click, new Chat or V1 global Run in this path.
|
|
190
|
+
prepared=await chrome.runtime.sendMessage({
|
|
191
|
+
type:'CHAT_LOOP_TASK_START_PREPARE_V2',requestId:id});
|
|
192
|
+
if(prepared?.status!=='PREPARED'
|
|
193
|
+
||prepared.scope!=='TASK_SCOPED_ADOPT_NO_SEND'
|
|
194
|
+
||!Number.isSafeInteger(prepared.tabId)
|
|
195
|
+
||typeof prepared.sessionId!=='string')
|
|
196
|
+
throw Error(prepared?.code||'SCOPED_START_PREPARE_UNVERIFIED');
|
|
197
|
+
await observerReady(prepared.tabId);
|
|
198
|
+
committed=await chrome.runtime.sendMessage({
|
|
199
|
+
type:'CHAT_LOOP_TASK_START_COMMIT_V2',requestId:id});
|
|
200
|
+
if(committed?.status!=='OK'
|
|
201
|
+
||committed.scope!=='TASK_SCOPED_ADOPTED_NO_SEND'
|
|
202
|
+
||committed.sessionId!==prepared.sessionId
|
|
203
|
+
||committed.tabId!==prepared.tabId)
|
|
204
|
+
throw Error(committed?.code||'SCOPED_START_COMMIT_UNVERIFIED');
|
|
205
|
+
// Resume only the existing Observer's baseline. It cannot grant a
|
|
206
|
+
// second sender; future new Turn events use the V2 Task route.
|
|
207
|
+
await chrome.tabs.sendMessage(prepared.tabId,{
|
|
208
|
+
type:'CHAT_LOOP_TURN_RESUME_V1',page:committed.page,
|
|
209
|
+
userKey:committed.userKey,assistantKey:committed.assistantKey,
|
|
210
|
+
...(committed.assistantKey?{baselineOnly:true}:{}),
|
|
211
|
+
startedAt:Date.now()}).catch(()=>null);
|
|
212
|
+
const receipt=await chrome.runtime.sendNativeMessage(host,{
|
|
213
|
+
op:'complete',id,nonce:proof.nonce,action:'start',
|
|
214
|
+
status:'OK',payload:JSON.stringify(committed)});
|
|
215
|
+
if(receipt?.status!=='SAVED'||receipt.id!==id)
|
|
216
|
+
throw Error('SCOPED_START_RECEIPT_UNKNOWN');
|
|
217
|
+
show('SUCCESS','当前 Task 已接管获批的原 Chat;未新建或发送消息。');
|
|
218
|
+
await chrome.tabs.remove(own.id).catch(()=>{});
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
prepared=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_PREPARE_V1',
|
|
222
|
+
requestId:id,...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
223
|
+
if(prepared?.status!=='PREPARED')throw Error(prepared?.code||'START_PREPARE_DENIED');
|
|
224
|
+
taken=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_TAKE_V1',
|
|
225
|
+
requestId:id,token:prepared.token,
|
|
226
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
227
|
+
if(taken?.status!=='OK'||taken.sessionId!==prepared.sessionId
|
|
228
|
+
||(taken.mode==='adopt'
|
|
229
|
+
? !Number.isSafeInteger(taken.tabId)||!/^\/c\/[0-9a-f-]{12,}$/i.test(taken.page||'')
|
|
230
|
+
&& !/^\/g\/[^/]+\/c\/[0-9a-f-]{12,}$/i.test(taken.page||'')
|
|
231
|
+
||typeof taken.userKey!=='string'
|
|
232
|
+
: typeof taken.prompt!=='string'))
|
|
233
|
+
throw Error(taken?.code||'START_TAKE_DENIED');
|
|
234
|
+
if(taken.mode==='adopt'){
|
|
235
|
+
const existing=await chrome.tabs.get(taken.tabId).catch(()=>null);
|
|
236
|
+
if(!existing || new URL(existing.url).pathname!==taken.page)
|
|
237
|
+
throw Error('ADOPTION_TAB_UNVERIFIED');
|
|
238
|
+
await observerReady(existing.id);
|
|
239
|
+
const repair=taken.purpose==='REPAIR_SAME_SCOPE';
|
|
240
|
+
const evidence=await chrome.tabs.sendMessage(existing.id,
|
|
241
|
+
{type:repair?'CHAT_LOOP_REPAIR_EVIDENCE_V1':'CHAT_LOOP_APPROVAL_EVIDENCE_V1',
|
|
242
|
+
...(taken.explicitChatId
|
|
243
|
+
?{explicitChatId:taken.explicitChatId}:{}),
|
|
244
|
+
...(taken.expectedTaskId
|
|
245
|
+
?{expectedTaskId:taken.expectedTaskId}:{})}).catch(()=>null);
|
|
246
|
+
if(evidence?.contract!=='chat-loop.turn-observer.v1'
|
|
247
|
+
||evidence.status!=='VERIFIED'||evidence.page!==taken.page
|
|
248
|
+
||evidence.userKey!==taken.userKey
|
|
249
|
+
||(repair
|
|
250
|
+
? evidence.userText!==taken.repairUserText
|
|
251
|
+
||evidence.priorDoneAssistantKey!==taken.priorDoneAssistantKey
|
|
252
|
+
: evidence.rVersion!==taken.rVersion
|
|
253
|
+
||evidence.pVersion!==taken.pVersion
|
|
254
|
+
||(evidence.priorAssistantKey||null)!==(taken.priorAssistantKey||null)
|
|
255
|
+
||(evidence.postApprovalAssistantKey||null)!==(taken.postApprovalAssistantKey||null)))
|
|
256
|
+
throw Error('ADOPTION_APPROVAL_CHANGED');
|
|
257
|
+
const registered=await chrome.runtime.sendMessage({type:'CHAT_LOOP_RESOURCE_CHAT_V1',
|
|
258
|
+
action:'start',stage:'adopt',tabId:existing.id});
|
|
259
|
+
if(registered?.status!=='OK')throw Error(registered?.code||'ADOPTION_RESOURCE_UNVERIFIED');
|
|
260
|
+
// No tabs.create, no composer input, no send-button click in this branch.
|
|
261
|
+
committed=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
|
|
262
|
+
requestId:id,token:prepared.token,result:'ADOPTED_CONFIRMED',
|
|
263
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{}),
|
|
264
|
+
newTabId:existing.id,newPage:taken.page,newUserKey:taken.userKey});
|
|
265
|
+
if(committed?.status!=='OK')throw Error(committed?.code||'ADOPTION_COMMIT_UNVERIFIED');
|
|
266
|
+
await chrome.tabs.sendMessage(existing.id,{type:'CHAT_LOOP_TURN_RESUME_V1',
|
|
267
|
+
page:taken.page,userKey:taken.userKey,
|
|
268
|
+
assistantKey:taken.postApprovalAssistantKey||taken.priorAssistantKey||null,
|
|
269
|
+
...(taken.postApprovalAssistantKey?{baselineOnly:true}:{}),
|
|
270
|
+
startedAt:Date.now()}).catch(()=>null);
|
|
271
|
+
const stored=await chrome.runtime.sendNativeMessage(host,{op:'complete',
|
|
272
|
+
id,nonce:proof.nonce,action:'start',status:'OK',payload:JSON.stringify(committed)});
|
|
273
|
+
if(stored?.status!=='SAVED')throw Error(stored?.code||'ADOPTION_RECEIPT_UNVERIFIED');
|
|
274
|
+
show('SUCCESS','已接管完成 R/P 确认的原 Chat;没有新建会话或重新发送消息。');
|
|
275
|
+
await chrome.tabs.remove(own.id);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const quota=await chrome.runtime.sendMessage({type:'CHAT_LOOP_RESOURCE_CHAT_V1',
|
|
279
|
+
action:'start',stage:'preflight'});
|
|
280
|
+
if(quota?.status!=='OK')throw Error(quota?.code||'RESOURCE_QUOTA_UNVERIFIED');
|
|
281
|
+
show('RUNNING','已独占会话,正在创建唯一网页 Chat 并确认首轮发送。');
|
|
282
|
+
newTab=await chrome.tabs.create({url:'https://chatgpt.com/',active:true,
|
|
283
|
+
windowId:own.windowId,openerTabId:own.id});
|
|
284
|
+
await waitForHomeUrl(newTab.id);
|
|
285
|
+
const registered=await chrome.runtime.sendMessage({type:'CHAT_LOOP_RESOURCE_CHAT_V1',
|
|
286
|
+
action:'start',stage:'register',tabId:newTab.id});
|
|
287
|
+
if(registered?.status!=='OK')throw Error(registered?.code||'RESOURCE_REGISTER_UNVERIFIED');
|
|
288
|
+
await newChatReady(newTab.id);
|
|
289
|
+
await observerReady(newTab.id);
|
|
290
|
+
const receipt=await sendFirst(newTab.id,taken.prompt);
|
|
291
|
+
committed=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
|
|
292
|
+
requestId:id,token:prepared.token,...receipt,result:receipt.status,
|
|
293
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
294
|
+
if(committed?.status!=='OK')throw Error(committed?.code||'START_COMMIT_UNVERIFIED');
|
|
295
|
+
const stored=await chrome.runtime.sendNativeMessage(host,{op:'complete',
|
|
296
|
+
id,nonce:proof.nonce,action:'start',status:'OK',payload:JSON.stringify(committed)});
|
|
297
|
+
if(stored?.status!=='SAVED')throw Error(stored?.code||'START_RECEIPT_NOT_STORED');
|
|
298
|
+
show('SUCCESS','唯一网页 Chat 已确认;执行由 Observer 和 Continue/Handoff 接管。');
|
|
299
|
+
await chrome.tabs.remove(own.id);
|
|
300
|
+
}catch(e){
|
|
301
|
+
if(taken&&committed?.status!=='OK') {
|
|
302
|
+
// This is diagnostics only; it does NOT authorize a retry or prove a
|
|
303
|
+
// message was never sent. Keep the existing fail-closed Owner semantics.
|
|
304
|
+
const failureCode=['START_HOME_URL_UNVERIFIED','NEW_CHAT_NOT_READY',
|
|
305
|
+
'START_COMPOSER_NOT_EMPTY','START_ACCOUNT_SWITCH_VISIBLE',
|
|
306
|
+
'FIRST_TURN_OBSERVER_UNAVAILABLE','START_SEND_BUTTON_UNAVAILABLE','START_INPUT_CHANGED',
|
|
307
|
+
'START_INPUT_UNVERIFIED','START_INPUT_REJECTED',
|
|
308
|
+
'START_CLICK_UNVERIFIED','FIRST_SEND_RECEIPT_UNKNOWN',
|
|
309
|
+
'FIRST_SEND_DUPLICATE_UNVERIFIED','FIRST_SEND_PAGE_DRIFT_UNVERIFIED']
|
|
310
|
+
.includes(e.message) ? e.message : null;
|
|
311
|
+
await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
|
|
312
|
+
requestId:id,token:prepared.token,result:'UNVERIFIED',newTabId:newTab?.id,
|
|
313
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{}),
|
|
314
|
+
...(failureCode?{failureCode}:{})}).catch(()=>{});
|
|
315
|
+
}
|
|
316
|
+
const rawCode=e?.code||e?.message;
|
|
317
|
+
const code=typeof rawCode==='string'&&/^[A-Z][A-Z0-9_]{1,95}$/.test(rawCode)
|
|
318
|
+
?rawCode:'START_UNVERIFIED';
|
|
319
|
+
const failed=await chrome.runtime.sendNativeMessage(host,{op:'complete',id,nonce:proof.nonce,
|
|
320
|
+
action:'start',status:'FAILED',code}).catch(()=>null);
|
|
321
|
+
// Only the Action page closes after its durable FAILED receipt; the
|
|
322
|
+
// possibly-sent Chat and any unverified draft remain untouched.
|
|
323
|
+
if(failed?.status==='SAVED' && failed.id===id)
|
|
324
|
+
await chrome.tabs.remove(own.id).catch(()=>{});
|
|
325
|
+
throw e; // Never auto-retry a possibly clicked first user Turn.
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
void run().catch(e=>show('BLOCKED',
|
|
329
|
+
/^[A-Z][A-Z0-9_]{1,95}$/.test(e?.message||'')?e.message:'START_UNVERIFIED'));
|
|
@@ -0,0 +1,38 @@
|
|
|
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 scoped=Boolean(proof.intent?.projectId&&proof.intent?.taskId);
|
|
12
|
+
const state=await chrome.runtime.sendMessage(scoped
|
|
13
|
+
?{type:'CHAT_LOOP_TASK_STATUS_V2',requestId:id}
|
|
14
|
+
:{type:'CHAT_LOOP_SESSION_STATUS_V1',requestId:id,
|
|
15
|
+
...(proof.traceParent?{traceParent:proof.traceParent}:{})})
|
|
16
|
+
.catch(()=>null);
|
|
17
|
+
if(scoped&&(state?.status!=='OK'
|
|
18
|
+
||state.scope!=='TASK_SCOPED_STATUS_READ_ONLY')){
|
|
19
|
+
const code=/^[A-Z][A-Z0-9_]{1,95}$/.test(state?.code||'')
|
|
20
|
+
?state.code:'SCOPED_TASK_STATUS_UNVERIFIED';
|
|
21
|
+
// Failure is an actual read-only result, not an absent ACK that makes
|
|
22
|
+
// the caller wait or retry an unrelated Status Action.
|
|
23
|
+
await chrome.runtime.sendNativeMessage(host,{op:'complete',id,
|
|
24
|
+
nonce:proof.nonce,action:'status',status:'FAILED',code}).catch(()=>null);
|
|
25
|
+
throw Error(code);
|
|
26
|
+
}
|
|
27
|
+
const receipt=await chrome.runtime.sendNativeMessage(host,{op:'complete',id,
|
|
28
|
+
nonce:proof.nonce,action:'status',status:'OK',payload:JSON.stringify(state)});
|
|
29
|
+
if(receipt?.status!=='SAVED')throw Error(receipt?.code||'STATUS_NOT_STORED');
|
|
30
|
+
document.getElementById('status').textContent='SUCCESS';
|
|
31
|
+
document.getElementById('details').textContent='只读状态已返回本机请求;未修改执行 Chat。';
|
|
32
|
+
await chrome.tabs.remove(own.id);
|
|
33
|
+
}
|
|
34
|
+
void run().catch(e=>{
|
|
35
|
+
document.getElementById('status').textContent='UNVERIFIED';
|
|
36
|
+
document.getElementById('details').textContent=
|
|
37
|
+
/^[A-Z][A-Z0-9_]{1,95}$/.test(e?.message||'')?e.message:'STATUS_UNKNOWN';
|
|
38
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
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/task-run-store.js");
|
|
5
|
+
importScripts("worker/task-scheduler.js");
|
|
6
|
+
importScripts("worker/task-owner-resolver.js");
|
|
7
|
+
importScripts("worker/bootstrap.js");
|
|
8
|
+
importScripts("worker/admission.js");
|
|
9
|
+
importScripts("worker/adopt-prepare.js");
|
|
10
|
+
importScripts("worker/adopt-result.js");
|
|
11
|
+
importScripts("worker/task-start-v2.js");
|
|
12
|
+
importScripts("worker/task-intent-v2.js");
|
|
13
|
+
importScripts("worker/task-intent-dispatch-v2.js");
|
|
14
|
+
importScripts("worker/task-action-v2.js");
|
|
15
|
+
importScripts("worker/task-continue-recovery-v2.js");
|
|
16
|
+
importScripts("worker/task-handoff-v2.js");
|
|
17
|
+
importScripts("worker/task-handoff-recovery-v2.js");
|
|
18
|
+
importScripts("worker/task-hold-recovery-v2.js");
|
|
19
|
+
importScripts("worker/task-terminal-v2.js");
|
|
20
|
+
importScripts("worker/task-cancel-v2.js");
|
|
21
|
+
importScripts("worker/task-recover-v2.js");
|
|
22
|
+
importScripts("worker/task-cancel-recovery-v2.js");
|
|
23
|
+
importScripts("worker/session-control.js");
|
|
24
|
+
importScripts("worker/readiness.js");
|
|
25
|
+
importScripts("worker/turn-failure.js");
|
|
26
|
+
importScripts("worker/turn-finished.js");
|
|
27
|
+
importScripts("worker/adopt-recovery.js");
|
|
28
|
+
importScripts("worker/manual.js");
|
|
29
|
+
importScripts("worker/action-take.js");
|
|
30
|
+
importScripts("worker/action-result.js");
|
|
31
|
+
importScripts("worker/scheduler.js");
|
|
32
|
+
importScripts("worker/task-routes-v2.js");
|
|
33
|
+
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>
|