@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,109 @@
|
|
|
1
|
+
// Read-only Observer helper: no composer writes, Chrome Owner or sends.
|
|
2
|
+
(() => {
|
|
3
|
+
globalThis.ChatLoopObserverRecoveryV1=({CONTRACT,session,inspect,poll,
|
|
4
|
+
resetCandidate,activeEvidenceConversation,messageId})=>
|
|
5
|
+
(message,reply)=>{
|
|
6
|
+
if (message?.type === "CHAT_LOOP_TURN_RETRY_MONITOR_V1") {
|
|
7
|
+
const snap = inspect();
|
|
8
|
+
if (snap.path !== message.page || snap.userKey !== message.userKey
|
|
9
|
+
|| session.reportedErrorUser !== message.userKey
|
|
10
|
+
|| session.completedUser === message.userKey) {
|
|
11
|
+
reply({contract: CONTRACT, status: "UNVERIFIED", reason: "RETRY_OWNER_CHANGED"});
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
session.reportedErrorUser = null;
|
|
15
|
+
reply({contract: CONTRACT, status: "REARMED", userKey: snap.userKey});
|
|
16
|
+
// Re-read after the Worker has committed WAITING_REPLY; if the same red
|
|
17
|
+
// error is still visible this is the second failure, not a new Continue.
|
|
18
|
+
void Promise.resolve().then(poll);
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (message?.type === "CHAT_LOOP_TURN_RESUME_V1") {
|
|
22
|
+
// Rebind only the exact Worker-owned Turn after Extension reload.
|
|
23
|
+
// A dedicated baseline-only mode never reports an already-completed
|
|
24
|
+
// pre-adoption assistant as a new execution Turn.
|
|
25
|
+
const snap = inspect();
|
|
26
|
+
if (!snap.path || snap.unavailable || snap.path !== message.page
|
|
27
|
+
|| !/^user:[^\s]{8,}$/.test(message.userKey || "")
|
|
28
|
+
|| snap.userKey !== message.userKey
|
|
29
|
+
|| message.assistantKey && !/^assistant:[^\s]{8,}$/.test(message.assistantKey)) {
|
|
30
|
+
reply({contract: CONTRACT, status: "UNVERIFIED", reason: "RESUME_IDENTITY_MISMATCH"});
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
// The Worker can still be WAITING_REPLY after our completion report was
|
|
34
|
+
// lost (e.g. MV3 worker suspension). Only that Worker-owned exact Turn
|
|
35
|
+
// may request a new stable read. Never replay on ordinary DOM polling.
|
|
36
|
+
if (session.pendingUser === message.userKey && !session.attaching
|
|
37
|
+
&& session.completedUser !== message.userKey && message.baselineOnly!==true) {
|
|
38
|
+
reply({contract: CONTRACT, status: "ALREADY_MONITORING"});
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if(message.baselineOnly===true &&
|
|
42
|
+
(!message.assistantKey || snap.assistantKey!==message.assistantKey
|
|
43
|
+
|| snap.stop||snap.busy||snap.pendingConfirmation||!snap.copyAction)){
|
|
44
|
+
reply({contract:CONTRACT,status:"UNVERIFIED",reason:"ADOPT_BASELINE_UNVERIFIED"});
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
session.path = snap.path;
|
|
48
|
+
session.sawHomepage = false;
|
|
49
|
+
session.attaching = false;
|
|
50
|
+
session.attachingFirstTurn = false;
|
|
51
|
+
session.firstSendSeen = false;
|
|
52
|
+
session.priorUser = null;
|
|
53
|
+
session.baselineUser = message.baselineOnly===true ? message.userKey : null;
|
|
54
|
+
session.baselineAssistant = message.assistantKey || null;
|
|
55
|
+
session.pendingUser = message.baselineOnly===true ? null : message.userKey;
|
|
56
|
+
session.completedUser = message.baselineOnly===true ? message.userKey : null;
|
|
57
|
+
session.firstTurnAt = Number.isSafeInteger(message.startedAt)
|
|
58
|
+
&& message.startedAt > 0 ? message.startedAt : Date.now();
|
|
59
|
+
session.attachSignature = "";
|
|
60
|
+
resetCandidate();
|
|
61
|
+
const observation = poll();
|
|
62
|
+
reply({contract: CONTRACT, status: "RESUMED",
|
|
63
|
+
observation: observation.status, reason: observation.reason,
|
|
64
|
+
userKey: observation.userKey,
|
|
65
|
+
...(message.baselineOnly===true?{baselineOnly:true}:{})});
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (message?.type === "CHAT_LOOP_REPAIR_EVIDENCE_V1") {
|
|
69
|
+
// Read-only proof of a NEW repair request in the EXACT Chat whose
|
|
70
|
+
// previous assistant finished with machine DONE. Not a new R/P approval.
|
|
71
|
+
const view=activeEvidenceConversation();
|
|
72
|
+
const fail=reason=>reply({contract:CONTRACT,status:"UNVERIFIED",reason});
|
|
73
|
+
if(!view){fail("REPAIR_CHAT_UNVERIFIED");return false;}
|
|
74
|
+
const {path,nodes,role}=view;
|
|
75
|
+
const userIdx=nodes.findLastIndex(el=>role(el)==="user");
|
|
76
|
+
if(userIdx<1||role(nodes[userIdx-1])!=="assistant"){
|
|
77
|
+
fail("REPAIR_SEQUENCE_UNVERIFIED");return false;
|
|
78
|
+
}
|
|
79
|
+
const text=el=>(el.querySelector?.(".markdown")?.innerText
|
|
80
|
+
||el.innerText||el.textContent||"").trim();
|
|
81
|
+
const lastUser=nodes[userIdx],priorAssistant=nodes[userIdx-1];
|
|
82
|
+
const userText=text(lastUser).replace(/\s+/g," ");
|
|
83
|
+
if(userText.length<4||userText.length>2000
|
|
84
|
+
||!/(?:修复|修正|返工|修改|补完|不通过|重新做|继续完成|改好|fix|repair|rework|correct)/i.test(userText)
|
|
85
|
+
||/(?:不要|不用|无需|拒绝).{0,8}(?:修复|修改|继续|fix|repair)/i.test(userText)
|
|
86
|
+
||!/\[CHAT_LOOP_STATUS:DONE\]\s*$/.test(text(priorAssistant))){
|
|
87
|
+
fail("REPAIR_EXPLICIT_INTENT_NOT_FOUND");return false;
|
|
88
|
+
}
|
|
89
|
+
const snap=inspect(),userKey=messageId(lastUser,"user"),
|
|
90
|
+
doneAssistantKey=messageId(priorAssistant,"assistant");
|
|
91
|
+
const requestedId=typeof message.explicitChatId==="string"
|
|
92
|
+
?message.explicitChatId.toLowerCase():null;
|
|
93
|
+
const currentId=path.match(/\/c\/([0-9a-f-]{12,})$/i)?.[1].toLowerCase();
|
|
94
|
+
if(requestedId&&requestedId!==currentId){
|
|
95
|
+
fail("REPAIR_CHAT_ID_CHANGED");return false;
|
|
96
|
+
}
|
|
97
|
+
if(!userKey||snap.path!==path||snap.userKey!==userKey
|
|
98
|
+
||snap.fatal||snap.dialog||snap.toolUnknown
|
|
99
|
+
||!(snap.stop||snap.busy||requestedId
|
|
100
|
+
&&!snap.pendingConfirmation&&snap.composerReady)){
|
|
101
|
+
fail("REPAIR_LAST_USER_UNVERIFIED");return false;
|
|
102
|
+
}
|
|
103
|
+
reply({contract:CONTRACT,status:"VERIFIED",page:path,
|
|
104
|
+
userKey,userText,priorDoneAssistantKey:doneAssistantKey});
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
};
|
|
109
|
+
})();
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Read-only ChatGPT Turn observation. No composer writes, clicks, or task decisions.
|
|
2
|
+
// Candidate DOM selectors must be verified against an authorized real ChatGPT tab.
|
|
3
|
+
(() => {
|
|
4
|
+
if (globalThis.__CHAT_LOOP_OBSERVER_V1__) return;
|
|
5
|
+
globalThis.__CHAT_LOOP_OBSERVER_V1__ = true;
|
|
6
|
+
const CONTRACT = "chat-loop.turn-observer.v1";
|
|
7
|
+
const STABLE_MS = 2200;
|
|
8
|
+
const POLL_MS = 60_000; // Temporary: check Turn status once per minute, independent of Handoff clock.
|
|
9
|
+
const session = {
|
|
10
|
+
path: "", baselineUser: null, baselineAssistant: null,
|
|
11
|
+
pendingUser: null, completedUser: null,
|
|
12
|
+
candidate: "", stableSince: 0, firstSendSeen: false, firstSendAt: 0,
|
|
13
|
+
sawHomepage: false, attaching: false, attachingFirstTurn: false,
|
|
14
|
+
priorUser: null, attachSignature: "", attachSince: 0,
|
|
15
|
+
emittedCount: 0, firstTurnAt: 0, reportedErrorUser: null
|
|
16
|
+
};
|
|
17
|
+
let state = {contract: CONTRACT, status: "UNKNOWN", reason: "INITIALIZING", eventCount: 0};
|
|
18
|
+
|
|
19
|
+
const conversationPath = () => {
|
|
20
|
+
if (location.protocol !== "https:" || location.hostname !== "chatgpt.com") return "";
|
|
21
|
+
const path = location.pathname.replace(/\/$/, "");
|
|
22
|
+
return /^\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i.test(path)
|
|
23
|
+
? path : "";
|
|
24
|
+
};
|
|
25
|
+
function visible(el) {
|
|
26
|
+
if (!el || !el.getClientRects?.().length) return false;
|
|
27
|
+
if (el.closest?.('[hidden],[aria-hidden="true"]')) return false;
|
|
28
|
+
const style = globalThis.getComputedStyle?.(el);
|
|
29
|
+
return style?.display !== "none" && style?.visibility !== "hidden";
|
|
30
|
+
}
|
|
31
|
+
function messageId(node, role) {
|
|
32
|
+
if (!node) return null;
|
|
33
|
+
const container = node.closest?.('[data-testid^="conversation-turn-"]');
|
|
34
|
+
const ids = node.getAttribute?.("data-chatgpt-search-message-ids") || "";
|
|
35
|
+
const id = node.getAttribute?.("data-message-id")
|
|
36
|
+
|| ids.trim().split(/\s+/)[0] || container?.getAttribute?.("data-testid");
|
|
37
|
+
return id ? role + ":" + id : null;
|
|
38
|
+
}
|
|
39
|
+
function fingerprint(text) {
|
|
40
|
+
let hash = 2166136261;
|
|
41
|
+
// Hash every UTF-16 code unit: for...of exposes whole Unicode code points
|
|
42
|
+
// and charCodeAt(0) would ignore the LOW surrogate of different emoji.
|
|
43
|
+
for (let i=0;i<text.length;i++) {
|
|
44
|
+
hash = Math.imul(hash ^ text.charCodeAt(i), 16777619);
|
|
45
|
+
}
|
|
46
|
+
return (hash >>> 0).toString(16) + ":" + text.length;
|
|
47
|
+
}
|
|
48
|
+
// Formal approval and repair are two independent evidence consumers, but
|
|
49
|
+
// both must read the ONE visible Chat, not merge hidden/virtualized mains.
|
|
50
|
+
// Keep this read-only snapshot separate from the Turn polling state machine.
|
|
51
|
+
const {activeEvidenceConversation,inspect}=
|
|
52
|
+
globalThis.ChatLoopObserverInspectV1({conversationPath,visible,messageId});
|
|
53
|
+
|
|
54
|
+
function emit(status, reason, snap, event = null) {
|
|
55
|
+
if (event) session.emittedCount += 1;
|
|
56
|
+
state = {contract: CONTRACT, status, reason, page: snap.path || null,
|
|
57
|
+
userKey: snap.userKey || null, assistantKey: snap.assistantKey || null,
|
|
58
|
+
retryAvailable: status === "BLOCKED" && snap.retryAvailable === true,
|
|
59
|
+
eventCount: session.emittedCount, observedAt: Date.now(),
|
|
60
|
+
pendingConfirmation: snap.pendingConfirmation === true,
|
|
61
|
+
priorAssistantKey: snap.priorAssistantKey || null,
|
|
62
|
+
priorAssistantUserKey:snap.priorAssistantUserKey || null,
|
|
63
|
+
directReplyAfterPriorAssistant:snap.directReplyAfterPriorAssistant===true};
|
|
64
|
+
if (event) {
|
|
65
|
+
document.dispatchEvent(new CustomEvent("CHAT_LOOP_TURN_FINISHED_V1", {
|
|
66
|
+
detail: {contract: CONTRACT, page: snap.path,
|
|
67
|
+
userKey: snap.userKey, assistantKey: snap.assistantKey}
|
|
68
|
+
}));
|
|
69
|
+
// The V2 route is read-only and selects a Task from the real sender
|
|
70
|
+
// Tab, never from DOM text or a global latest-Task pointer. Only a
|
|
71
|
+
// verified UNSCOPED reply (or an old Worker without the V2 handler)
|
|
72
|
+
// falls back to the original V1 Turn delivery. A V2 BLOCKED/UNKNOWN
|
|
73
|
+
// never authorizes replay on the legacy sender.
|
|
74
|
+
if (typeof chrome.runtime.sendMessage === "function") {
|
|
75
|
+
const legacy=()=>chrome.runtime.sendMessage({
|
|
76
|
+
type: "CHAT_LOOP_TURN_FINISHED_V1", page: snap.path,
|
|
77
|
+
userKey: snap.userKey, assistantKey: snap.assistantKey,
|
|
78
|
+
replyText: snap.text, startedAt: session.firstTurnAt || Date.now(),
|
|
79
|
+
pendingConfirmation: snap.pendingConfirmation === true,
|
|
80
|
+
priorAssistantKey: snap.priorAssistantKey || null,
|
|
81
|
+
priorAssistantUserKey:snap.priorAssistantUserKey || null,
|
|
82
|
+
directReplyAfterPriorAssistant:snap.directReplyAfterPriorAssistant===true
|
|
83
|
+
});
|
|
84
|
+
void Promise.resolve().then(()=>chrome.runtime.sendMessage({
|
|
85
|
+
type:"CHAT_LOOP_TASK_TURN_OBSERVED_V2",page:snap.path,
|
|
86
|
+
userKey:snap.userKey,assistantKey:snap.assistantKey,
|
|
87
|
+
// Core is the SAME canonical status parser used in the Worker.
|
|
88
|
+
// Only the marker classification crosses this boundary; never
|
|
89
|
+
// forward the full private user reply into Task Store.
|
|
90
|
+
outcome:!snap.pendingConfirmation
|
|
91
|
+
?globalThis.ChatLoopCoreV1?.parseStatus(snap.text)??'UNKNOWN'
|
|
92
|
+
:'UNKNOWN',
|
|
93
|
+
pendingConfirmation:snap.pendingConfirmation===true
|
|
94
|
+
})).then(result=>{
|
|
95
|
+
if(result?.status==='OBSERVED'
|
|
96
|
+
&&['TASK_SCOPED_OBSERVATION_ONLY',
|
|
97
|
+
'TASK_SCOPED_TERMINAL_OBSERVATION'].includes(result.scope))
|
|
98
|
+
return;
|
|
99
|
+
// A genuine scoped rejection, including UNKNOWN/UNVERIFIED,
|
|
100
|
+
// never becomes a legacy V1 sender. Only a proven unowned Tab
|
|
101
|
+
// or an old Worker without this response may use V1 routing.
|
|
102
|
+
if(result?.status==='UNSCOPED'
|
|
103
|
+
&&result.code==='SCOPED_OBSERVER_TASK_NOT_FOUND'||!result)
|
|
104
|
+
return legacy();
|
|
105
|
+
},()=>legacy()).catch(()=>{});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return state;
|
|
109
|
+
}
|
|
110
|
+
function resetCandidate() {
|
|
111
|
+
session.candidate = "";
|
|
112
|
+
session.stableSince = 0;
|
|
113
|
+
}
|
|
114
|
+
const reportBlocked=detail=>{
|
|
115
|
+
const legacy=()=>chrome.runtime.sendMessage({
|
|
116
|
+
type:'CHAT_LOOP_TURN_BLOCKED_V1',...detail});
|
|
117
|
+
// A Task fault is only an observation, not a Handoff permit. Do not
|
|
118
|
+
// downgrade an explicit scoped denial into a legacy V1 side effect.
|
|
119
|
+
return Promise.resolve(chrome.runtime.sendMessage({
|
|
120
|
+
type:'CHAT_LOOP_TASK_TURN_BLOCKED_V2',...detail
|
|
121
|
+
})).then(result=>{
|
|
122
|
+
if(result?.status==='UNSCOPED'
|
|
123
|
+
&&result.code==='SCOPED_OBSERVER_TASK_NOT_FOUND'||!result)
|
|
124
|
+
return legacy();
|
|
125
|
+
},()=>legacy()).catch(()=>{});
|
|
126
|
+
};
|
|
127
|
+
const poll=globalThis.ChatLoopObserverPollV1({session,inspect,emit,
|
|
128
|
+
resetCandidate,fingerprint,STABLE_MS,getState:()=>state,reportBlocked});
|
|
129
|
+
function recordFirstChatSubmission(event) {
|
|
130
|
+
if (!event?.target?.closest) return;
|
|
131
|
+
const fromHome = !conversationPath() && !session.path;
|
|
132
|
+
const fromEmptyChat = Boolean(conversationPath()) && session.attaching
|
|
133
|
+
&& !session.baselineUser && !session.attachSignature;
|
|
134
|
+
if (!fromHome && !fromEmptyChat) return;
|
|
135
|
+
const sendClick = event.type === "click" && event.target.closest(
|
|
136
|
+
'button[type="submit"][aria-label="发送"],button[data-testid="send-button"],' +
|
|
137
|
+
'button[data-testid="composer-send-button"],button[aria-label="Send message"]');
|
|
138
|
+
const sendEnter = event.type === "keydown" && event.key === "Enter"
|
|
139
|
+
&& !event.shiftKey && !event.isComposing && event.target.closest(
|
|
140
|
+
'[contenteditable="true"][data-composer-markdown],#prompt-textarea');
|
|
141
|
+
if (!sendClick && !sendEnter) return;
|
|
142
|
+
const editor = document.querySelector('[contenteditable="true"][data-composer-markdown]')
|
|
143
|
+
|| document.querySelector('#prompt-textarea[contenteditable="true"]');
|
|
144
|
+
if (!visible(editor) || !(editor.innerText ?? editor.textContent ?? "").trim()) return;
|
|
145
|
+
session.firstSendSeen = true;
|
|
146
|
+
session.firstSendAt = Date.now();
|
|
147
|
+
if (!session.firstTurnAt) session.firstTurnAt = session.firstSendAt;
|
|
148
|
+
if (fromEmptyChat) {
|
|
149
|
+
session.attaching = false;
|
|
150
|
+
session.attachingFirstTurn = true;
|
|
151
|
+
session.baselineUser = null;
|
|
152
|
+
session.baselineAssistant = null;
|
|
153
|
+
session.pendingUser = null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
document.addEventListener?.("click", recordFirstChatSubmission, true);
|
|
157
|
+
document.addEventListener?.("keydown", recordFirstChatSubmission, true);
|
|
158
|
+
globalThis.ChatLoopTurnObserverV1 = Object.freeze({
|
|
159
|
+
poll, read: () => ({...state})
|
|
160
|
+
});
|
|
161
|
+
const onRecovery=globalThis.ChatLoopObserverRecoveryV1({
|
|
162
|
+
CONTRACT,session,inspect,poll,resetCandidate,activeEvidenceConversation,messageId});
|
|
163
|
+
const onApproval=globalThis.ChatLoopObserverApprovalV1({
|
|
164
|
+
CONTRACT,inspect,activeEvidenceConversation,messageId});
|
|
165
|
+
chrome.runtime.onMessage.addListener((message,_sender,reply)=>{
|
|
166
|
+
if(['CHAT_LOOP_TURN_RETRY_MONITOR_V1','CHAT_LOOP_TURN_RESUME_V1',
|
|
167
|
+
'CHAT_LOOP_REPAIR_EVIDENCE_V1'].includes(message?.type))
|
|
168
|
+
return onRecovery(message,reply);
|
|
169
|
+
if(message?.type==='CHAT_LOOP_APPROVAL_EVIDENCE_V1')
|
|
170
|
+
return onApproval(message,reply);
|
|
171
|
+
if(message?.type==='CHAT_LOOP_TURN_PROGRESS_V2'){
|
|
172
|
+
// Model Intent is issued BEFORE the assistant Turn finishes. A
|
|
173
|
+
// completed historical user Turn never authorizes a fresh Action.
|
|
174
|
+
// Report only top-level current DOM identity and active generation
|
|
175
|
+
// evidence; neither message text nor a send permit crosses here.
|
|
176
|
+
if(Object.keys(message).length!==1)return false;
|
|
177
|
+
const snap=inspect();
|
|
178
|
+
reply({contract:CONTRACT,page:snap.path||null,
|
|
179
|
+
userKey:snap.userKey||null,
|
|
180
|
+
active:Boolean(snap.path&&snap.userKey
|
|
181
|
+
&&(snap.stop||snap.busy
|
|
182
|
+
||snap.hasAssistant&&snap.turnState
|
|
183
|
+
&&snap.turnState!=='complete')
|
|
184
|
+
&&!snap.pendingConfirmation&&!snap.fatal&&!snap.dialog),
|
|
185
|
+
pendingConfirmation:snap.pendingConfirmation===true});
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
if (message?.type === "CHAT_LOOP_TURN_VERIFY_V1") {
|
|
189
|
+
// Explicit manual Action: only a fresh read-only completed-Turn candidate.
|
|
190
|
+
// This does not emit a completion event or write anything to the page.
|
|
191
|
+
const snap = inspect();
|
|
192
|
+
const ready = Boolean(snap.path && snap.userKey && snap.assistantKey
|
|
193
|
+
&& snap.text && snap.composerReady && snap.copyAction
|
|
194
|
+
&& !snap.stop && !snap.busy && !snap.pendingConfirmation
|
|
195
|
+
&& !snap.toolUnknown && !snap.fatal
|
|
196
|
+
&& !snap.dialog && (!snap.turnState || snap.turnState === "complete"));
|
|
197
|
+
reply({contract: CONTRACT, status: ready ? "READY" : "UNVERIFIED",
|
|
198
|
+
page: snap.path || null, userKey: snap.userKey || null,
|
|
199
|
+
assistantKey: snap.assistantKey || null,
|
|
200
|
+
pendingConfirmation:snap.pendingConfirmation===true,
|
|
201
|
+
replyText: ready ? snap.text : null});
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
if (message?.type !== "CHAT_LOOP_TURN_OBSERVE_V1") return false;
|
|
205
|
+
if (message.poll === true) poll();
|
|
206
|
+
reply({...state});
|
|
207
|
+
return false;
|
|
208
|
+
});
|
|
209
|
+
setInterval(poll, POLL_MS);
|
|
210
|
+
poll();
|
|
211
|
+
})();
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Pure support functions for the existing single Resource Ledger Owner.
|
|
2
|
+
// No storage or independent background Worker/Alarm is introduced.
|
|
3
|
+
(() => {
|
|
4
|
+
const safeRemove = async (tab,entry,state,reason) => {
|
|
5
|
+
const live=await chrome.tabs.get(tab.id).catch(()=>null);
|
|
6
|
+
if (!live || live.url !== entry.url || live.active || live.pinned) return false;
|
|
7
|
+
try {await chrome.tabs.remove(tab.id);}
|
|
8
|
+
catch (error) {
|
|
9
|
+
// A user can close this tab after the final ownership check. Treat an
|
|
10
|
+
// absent tab as unconfirmed cleanup and reconcile it on the next scan;
|
|
11
|
+
// a still-live tab with a remove error must remain an explicit failure.
|
|
12
|
+
if (await chrome.tabs.get(tab.id).catch(()=>null)) throw error;
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
state.closed++;
|
|
16
|
+
state.lastReaped={id:tab.id, reason, at:Date.now()};
|
|
17
|
+
state.entries=state.entries.filter(e=>e.id!==tab.id);
|
|
18
|
+
return true;
|
|
19
|
+
};
|
|
20
|
+
const noDraft = async tabId => {
|
|
21
|
+
try {
|
|
22
|
+
const [p]=await chrome.scripting.executeScript({target:{tabId},func:()=>{
|
|
23
|
+
if (location.origin !== 'https://chatgpt.com' || !/^\/c\/[0-9a-f-]{12,}$/i.test(location.pathname)) return false;
|
|
24
|
+
const editor=document.querySelector('[contenteditable="true"][data-composer-markdown]')
|
|
25
|
+
||document.querySelector('#prompt-textarea[contenteditable="true"]');
|
|
26
|
+
return !!editor && !(editor.innerText || editor.textContent || '').trim()
|
|
27
|
+
&& !document.querySelector('[data-testid*="attachment-preview"], [data-testid*="image-preview"], [data-testid="composer-attachment"]');
|
|
28
|
+
}});
|
|
29
|
+
return p?.result===true;
|
|
30
|
+
} catch {return false;}
|
|
31
|
+
};
|
|
32
|
+
const actionTabMatches=async tab=>{
|
|
33
|
+
const live=await chrome.tabs.get(tab.id).catch(()=>null);
|
|
34
|
+
return !!live&&(live.url===tab.url
|
|
35
|
+
||live.url==='about:blank'&&live.pendingUrl===tab.url);
|
|
36
|
+
};
|
|
37
|
+
// Opener absence is meaningful only while the exact original Action
|
|
38
|
+
// remains alive: closing it can erase a successor's opener attribution.
|
|
39
|
+
// Read the original before and after querying all tabs; any uncertainty
|
|
40
|
+
// retains the original Resource Ledger hold.
|
|
41
|
+
const noSuccessorForAbortedHandoff=async(run,actionTabId)=>{
|
|
42
|
+
const action={id:actionTabId,url:chrome.runtime.getURL('handoff.html')
|
|
43
|
+
+'?tabId='+run.tabId+'&token='+run.turnToken+'&taskFirst=1'};
|
|
44
|
+
if(!await actionTabMatches(action))return false;
|
|
45
|
+
const tabs=await chrome.tabs.query({});
|
|
46
|
+
if(tabs.some(tab=>tab.openerTabId===actionTabId))return false;
|
|
47
|
+
return actionTabMatches(action);
|
|
48
|
+
};
|
|
49
|
+
// Only a completed Native + Context transfer, already committed to
|
|
50
|
+
// the Worker Task Store, can retire the old physical Owner seat.
|
|
51
|
+
// Retain the old Chat entry and tab; it is historical provenance.
|
|
52
|
+
const confirmTransfer=async(state,entry,run,sameSession,url)=>{
|
|
53
|
+
// resourceTransfer is committed with the proven generation switch
|
|
54
|
+
// by the Task Store CAS, not supplied by an Action or a Chat page.
|
|
55
|
+
const r=run?.resourceTransfer;
|
|
56
|
+
if(!r||!run.taskId||run.generation<1
|
|
57
|
+
||r.sourceTabId===run.tabId||r.sourcePage===run.page
|
|
58
|
+
||entry.id!==run.tabId||!sameSession(entry,run)
|
|
59
|
+
||entry.supersededBy!==undefined
|
|
60
|
+
||(entry.url!=='https://chatgpt.com/'&&entry.url!==url)
|
|
61
|
+
||!Number.isSafeInteger(r.sourceTabId)||r.sourceTabId<=0
|
|
62
|
+
||!/^\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i.test(r.sourcePage)
|
|
63
|
+
||!/^\/c\/[0-9a-f-]{12,}$/i.test(run.page)
|
|
64
|
+
||!/^[a-f0-9]{32}$/.test(r.attemptId)
|
|
65
|
+
||url!=='https://chatgpt.com'+run.page)return false;
|
|
66
|
+
const tab=await chrome.tabs.get(run.tabId).catch(()=>null);
|
|
67
|
+
try{
|
|
68
|
+
const live=new URL(tab?.url);
|
|
69
|
+
if(live.origin+live.pathname!==url)return false;
|
|
70
|
+
}catch{return false;}
|
|
71
|
+
const old=state.entries.find(e=>e.id===r.sourceTabId);
|
|
72
|
+
if(old&&(!old.confirmed||old.kind!=='chat'||!sameSession(old,run)
|
|
73
|
+
||old.url!=='https://chatgpt.com'+r.sourcePage
|
|
74
|
+
||old.supersededBy!==undefined&&old.supersededBy!==entry.id))
|
|
75
|
+
return false;
|
|
76
|
+
if(old)old.supersededBy=entry.id;
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
79
|
+
const activeChat=(entry,runs,sameSession)=>{
|
|
80
|
+
// Never use a historical seat as an active Owner, except when a
|
|
81
|
+
// conflicting current projection still points to its exact tab.
|
|
82
|
+
if(Number.isSafeInteger(entry.supersededBy)&&entry.supersededBy>0
|
|
83
|
+
&&entry.supersededBy!==entry.id&&entry.confirmed&&entry.taskId)
|
|
84
|
+
return runs.some(r=>sameSession(entry,r)&&r.tabId===entry.id
|
|
85
|
+
&&!['DONE','BLOCKED','CANCELLED'].includes(r.phase));
|
|
86
|
+
return !['DONE','BLOCKED','CANCELLED'].includes(entry.terminal)
|
|
87
|
+
||runs.some(r=>sameSession(entry,r)
|
|
88
|
+
&&!['DONE','BLOCKED','CANCELLED'].includes(r.phase));
|
|
89
|
+
};
|
|
90
|
+
// Only binds to the caller's existing Resource Ledger. This helper
|
|
91
|
+
// owns no storage, queue, browser sender, clock or independent authority.
|
|
92
|
+
const createHandoffReservation=({load,reserveChat,actionTabMatches,validScope})=>{
|
|
93
|
+
const same=(h,run,actionTabId)=>typeof run.turnToken==='string'
|
|
94
|
+
&&typeof run.sessionId==='string'
|
|
95
|
+
&&h.token.toLowerCase()===run.turnToken.toLowerCase()
|
|
96
|
+
&&h.sessionId.toLowerCase()===run.sessionId.toLowerCase()
|
|
97
|
+
&&h.projectId===run.projectId&&h.taskId===run.taskId
|
|
98
|
+
&&h.actionTabId===actionTabId;
|
|
99
|
+
const reserve=async(run,actionTabId)=>{
|
|
100
|
+
if(run?.phase!=='WAITING_REPLY'
|
|
101
|
+
||!validScope(run.projectId)||!validScope(run.taskId)
|
|
102
|
+
||run.modelIntent?.action!=='HANDOFF'
|
|
103
|
+
||run.modelIntent.state!=='QUEUED'
|
|
104
|
+
||run.modelIntent.actionTabId!==actionTabId
|
|
105
|
+
||run.modelIntent.turnToken!==run.turnToken
|
|
106
|
+
||run.lastObservedTurn?.outcome!=='HANDOFF'
|
|
107
|
+
||run.lastObservedTurn.pendingConfirmation!==false
|
|
108
|
+
||run.lastObservedTurn.userKey!==run.modelIntent.targetUserKey
|
|
109
|
+
||!Number.isSafeInteger(actionTabId)||actionTabId<=0
|
|
110
|
+
||!/^[0-9a-f-]{36}$/i.test(run.turnToken||''))
|
|
111
|
+
throw Error('RESOURCE_HANDOFF_SOURCE_INVALID');
|
|
112
|
+
const url=chrome.runtime.getURL('handoff.html')+
|
|
113
|
+
'?tabId='+run.tabId+'&token='+run.turnToken+'&taskFirst=1';
|
|
114
|
+
if(!await actionTabMatches({id:actionTabId,url}))
|
|
115
|
+
throw Error('RESOURCE_HANDOFF_ACTION_CHANGED');
|
|
116
|
+
const state=await load(),owned=state.pendingChats.filter(h=>
|
|
117
|
+
same(h,run,actionTabId));
|
|
118
|
+
if(owned.length===1)return {ok:true,code:'RESOURCE_CHAT_RESERVED_SAME_ATTEMPT'};
|
|
119
|
+
if(state.pendingChats.some(h=>
|
|
120
|
+
h.token.toLowerCase()===run.turnToken.toLowerCase()
|
|
121
|
+
||h.sessionId.toLowerCase()===run.sessionId.toLowerCase()
|
|
122
|
+
||h.actionTabId===actionTabId))
|
|
123
|
+
return {ok:false,code:'RESOURCE_HANDOFF_HOLD_CONFLICT'};
|
|
124
|
+
return reserveChat({...run,phase:'PENDING',token:run.turnToken},actionTabId);
|
|
125
|
+
};
|
|
126
|
+
const verify=async(run,actionTabId)=>{
|
|
127
|
+
if(run?.phase!=='PENDING'||run.modelIntent?.action!=='HANDOFF'
|
|
128
|
+
||run.modelIntent.state!=='TAKEN'
|
|
129
|
+
||run.modelIntent.actionTabId!==actionTabId
|
|
130
|
+
||run.nextAttempt?.id!==run.modelIntent.id
|
|
131
|
+
||!validScope(run.projectId)||!validScope(run.taskId))
|
|
132
|
+
return false;
|
|
133
|
+
const url=chrome.runtime.getURL('handoff.html')+
|
|
134
|
+
'?tabId='+run.tabId+'&token='+run.turnToken+'&taskFirst=1';
|
|
135
|
+
if(!await actionTabMatches({id:actionTabId,url}))return false;
|
|
136
|
+
const state=await load();
|
|
137
|
+
// load() already rejects duplicate token/session/action-tab holds.
|
|
138
|
+
return state.pendingChats.some(h=>same(h,run,actionTabId));
|
|
139
|
+
};
|
|
140
|
+
return {reserve,verify};
|
|
141
|
+
};
|
|
142
|
+
const finishedSendAction=(info,entry,runs)=>runs.some(run=>{
|
|
143
|
+
const legacy=['DONE','BLOCKED'].includes(run.phase)
|
|
144
|
+
&&run.token===info.token
|
|
145
|
+
&&[run.tabId,run.sourceTabId].includes(info.targetId);
|
|
146
|
+
const receipt=run.lastIntentReceipt;
|
|
147
|
+
const scoped=entry.owned===true
|
|
148
|
+
&&['continue','handoff'].includes(info.name)
|
|
149
|
+
&&receipt?.turnToken===info.token
|
|
150
|
+
&&receipt.action===info.name.toUpperCase()
|
|
151
|
+
&&receipt.status==='SENT_CONFIRMED'
|
|
152
|
+
&&(info.name==='continue'
|
|
153
|
+
?run.tabId===info.targetId:run.generation>=1);
|
|
154
|
+
return legacy||scoped;
|
|
155
|
+
});
|
|
156
|
+
globalThis.ChatLoopResourceMaintenanceV1=Object.freeze({
|
|
157
|
+
safeRemove,noDraft,actionTabMatches,noSuccessorForAbortedHandoff,
|
|
158
|
+
confirmTransfer,activeChat,createHandoffReservation,finishedSendAction});
|
|
159
|
+
})();
|