@tomflow/chatloop-tools 0.9.63 → 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/README.md +13 -1
- package/index.mjs +7 -7
- package/lib/context-control-run.mjs +23 -5
- package/lib/extension-proxy.mjs +16 -7
- package/lib/native-owner-v2.mjs +128 -0
- package/lib/release-health-checks.mjs +29 -5
- package/lib/trace-gateway.mjs +3 -1
- package/node_modules/@tomflow/chatloop-context/README.md +56 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +24 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +1 -1
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +2 -2
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +69 -0
- package/node_modules/@tomflow/chatloop-context/index.mjs +3 -1
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +50 -3
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +54 -1
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +2 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +61 -2
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +33 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +14 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +31 -39
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +6 -5
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +84 -3
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +52 -7
- package/node_modules/@tomflow/chatloop-context/package.json +2 -4
- package/node_modules/@tomflow/chatloop-extension/README.md +66 -0
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +19 -4
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +40 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +1 -1
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +54 -4
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +9 -3
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +9 -5
- package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +53 -10
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +34 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +16 -2
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +16 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +1 -1
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +7 -6
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +64 -7
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +235 -95
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/session.html +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +3 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +73 -16
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +23 -6
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +237 -13
- 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/index.mjs +7 -0
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +94 -19
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +3 -3
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +9 -2
- 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 +50 -7
- package/node_modules/@tomflow/chatloop-extension/package.json +1 -3
- package/node_modules/@tomflow/chatloop-trace/package.json +1 -3
- package/package.json +9 -11
- package/providers/extension.mjs +59 -16
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// V2 Task-first Handoff on the pre-existing formal handoff.html sender.
|
|
2
|
+
// Delegates the exact DOM interaction to handoff.js; never creates another
|
|
3
|
+
// browser sender or derives a Task from a user-supplied URL.
|
|
4
|
+
const sameOriginalChat=(tab,tabId,page)=>{
|
|
5
|
+
if(tab?.id!==tabId)return false;
|
|
6
|
+
try{
|
|
7
|
+
const url=new URL(tab.url);
|
|
8
|
+
return url.origin==='https://chatgpt.com'&&url.pathname===page;
|
|
9
|
+
}catch{return false;}
|
|
10
|
+
};
|
|
11
|
+
export async function runScopedHandoff(ports){
|
|
12
|
+
const {verifyOldChat,waitForNewChat,sendHandoffOnce,show,
|
|
13
|
+
actionErrorCode}=ports;
|
|
14
|
+
const own=await chrome.tabs.getCurrent(),q=new URLSearchParams(location.search);
|
|
15
|
+
const raw=q.get('tabId'),token=q.get('token');
|
|
16
|
+
if(!own?.id||own.url!==location.href||q.size!==3
|
|
17
|
+
||q.get('taskFirst')!=='1'
|
|
18
|
+
||!/^[1-9][0-9]*$/.test(raw||'')
|
|
19
|
+
||!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f-]{12}$/i.test(token||''))
|
|
20
|
+
throw Error('SCOPED_HANDOFF_PARAMETERS_INVALID');
|
|
21
|
+
const request={tabId:Number(raw),token};
|
|
22
|
+
const ask=(type,fields={})=>chrome.runtime.sendMessage({
|
|
23
|
+
type,...request,...fields});
|
|
24
|
+
let permit=null,registrationAttempts=0;
|
|
25
|
+
const capacityDeadline=Date.now()+60*60*1000;
|
|
26
|
+
for(;;){
|
|
27
|
+
permit=await ask('CHAT_LOOP_TASK_HANDOFF_TAKE_V2').catch(()=>null);
|
|
28
|
+
if(permit?.code==='SCOPED_ACTION_REGISTRATION_PENDING'
|
|
29
|
+
&®istrationAttempts++<12){
|
|
30
|
+
await new Promise(resolve=>setTimeout(resolve,180));
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
// RESOURCE_CHAT_LIMIT is decided in the Resource Ledger BEFORE
|
|
34
|
+
// Native/Context Permit consumption. Retry only that determinate
|
|
35
|
+
// no-side-effect denial on the SAME Action tab and original request.
|
|
36
|
+
// Never retry UNKNOWN, a partial Permit, or a lost Action ACK.
|
|
37
|
+
if(permit?.status==='DENIED'&&permit.code==='RESOURCE_CHAT_LIMIT'
|
|
38
|
+
&&Date.now()<capacityDeadline){
|
|
39
|
+
show('WAITING','当前 Task 的 Chat 名额不足,保留原 Writer,等待可用名额。');
|
|
40
|
+
await new Promise(resolve=>setTimeout(resolve,60_000));
|
|
41
|
+
// Worker rechecks the exact sender Tab URL and original Task/Turn
|
|
42
|
+
// on every TAKE; no duplicate check or second browser query here.
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
if(permit?.status!=='OK'||permit.scope!=='TASK_SCOPED_HANDOFF_TAKEN'
|
|
48
|
+
||permit.handoffMode!=='EXPLICIT'
|
|
49
|
+
||permit.target?.tabId!==request.tabId
|
|
50
|
+
||typeof permit.text!=='string'||!permit.contextHandoff)
|
|
51
|
+
throw Error(permit?.code||'SCOPED_HANDOFF_PERMIT_UNVERIFIED');
|
|
52
|
+
const h=permit.contextHandoff;
|
|
53
|
+
let newTab,started=false,result=null;
|
|
54
|
+
try{
|
|
55
|
+
const summary=await verifyOldChat(permit.target,permit.text,'EXPLICIT');
|
|
56
|
+
const source=await chrome.tabs.get(request.tabId);
|
|
57
|
+
if(!sameOriginalChat(source,request.tabId,permit.target.page))
|
|
58
|
+
throw Error('SCOPED_HANDOFF_OLD_CHAT_CHANGED');
|
|
59
|
+
// TAKE already reserved the original Action's successor seat before
|
|
60
|
+
// consuming the Permit. This check must not allocate another seat.
|
|
61
|
+
const hold=await ask('CHAT_LOOP_TASK_HANDOFF_CHAT_V2',{
|
|
62
|
+
stage:'preflight'}).catch(()=>null);
|
|
63
|
+
if(hold?.status!=='OK'||hold.scope!=='TASK_SCOPED_CHAT_RESERVED')
|
|
64
|
+
throw Error(hold?.code||'SCOPED_HANDOFF_HOLD_UNVERIFIED');
|
|
65
|
+
// A new Chat is an irreversible browser side effect: create once, never
|
|
66
|
+
// regenerate it if the tab ID, URL, page or ACK is missing.
|
|
67
|
+
newTab=await chrome.tabs.create({url:'https://chatgpt.com/',
|
|
68
|
+
active:true,windowId:source.windowId,openerTabId:own.id});
|
|
69
|
+
let ready=false;
|
|
70
|
+
for(let i=0;i<40;i++){
|
|
71
|
+
const current=await chrome.tabs.get(newTab.id).catch(()=>null);
|
|
72
|
+
if(current?.url==='https://chatgpt.com/'){ready=true;break;}
|
|
73
|
+
await new Promise(resolve=>setTimeout(resolve,250));
|
|
74
|
+
}
|
|
75
|
+
if(!ready)throw Error('SCOPED_HANDOFF_HOME_UNKNOWN');
|
|
76
|
+
const registered=await ask('CHAT_LOOP_TASK_HANDOFF_CHAT_V2',{
|
|
77
|
+
stage:'register',newTabId:newTab.id}).catch(()=>null);
|
|
78
|
+
if(registered?.status!=='OK'
|
|
79
|
+
||registered.scope!=='TASK_SCOPED_CHAT_REGISTERED')
|
|
80
|
+
throw Error(registered?.code||'SCOPED_HANDOFF_CHAT_NOT_REGISTERED');
|
|
81
|
+
await waitForNewChat(newTab.id);
|
|
82
|
+
let observer=await chrome.tabs.sendMessage(newTab.id,{
|
|
83
|
+
type:'CHAT_LOOP_TURN_OBSERVE_V1'}).catch(()=>null);
|
|
84
|
+
if(observer?.contract!=='chat-loop.turn-observer.v1'){
|
|
85
|
+
await chrome.scripting.executeScript({target:{tabId:newTab.id},
|
|
86
|
+
files:['content.js','loop/core.js','loop/observer-inspect.js',
|
|
87
|
+
'loop/observer-poll.js','loop/observer-recovery.js',
|
|
88
|
+
'loop/observer-approval.js','loop/observer.js']});
|
|
89
|
+
observer=await chrome.tabs.sendMessage(newTab.id,{
|
|
90
|
+
type:'CHAT_LOOP_TURN_OBSERVE_V1'}).catch(()=>null);
|
|
91
|
+
}
|
|
92
|
+
if(observer?.contract!=='chat-loop.turn-observer.v1')
|
|
93
|
+
throw Error('SCOPED_HANDOFF_OBSERVER_UNVERIFIED');
|
|
94
|
+
const context={projectId:h.projectId,taskId:h.taskId,
|
|
95
|
+
sessionId:h.sessionId,checkpointId:h.checkpointId,
|
|
96
|
+
attemptId:h.attemptId,storeEpoch:h.contextStoreEpoch,
|
|
97
|
+
targetGeneration:h.targetGeneration,
|
|
98
|
+
taskRevision:h.taskRevision,
|
|
99
|
+
bindingRevision:h.fencedBindingRevision+1,
|
|
100
|
+
runRef:h.nextRunRef,runCredential:h.nextRunCredential};
|
|
101
|
+
const handoff='你正在接手同一已授权 Task 的下一个 Chat。'
|
|
102
|
+
+'先核实原 Task 的 R/P、最新 Context Checkpoint、Native Owner、'
|
|
103
|
+
+'已发生的副作用;不要重复未知发送或新建另一任务。'
|
|
104
|
+
+'\n\n【旧 Chat 交接摘要】\n'+summary
|
|
105
|
+
+'\n\n【当前任务 Context 受控恢复凭据|仅本次新 Chat】\n'
|
|
106
|
+
+JSON.stringify(context)
|
|
107
|
+
+'\n验证新 generation、绑定修订与原 Handoff attempt 后,'
|
|
108
|
+
+'只按本 Task 原计划继续;阻断则报告 BLOCKED。'
|
|
109
|
+
+'\n\n最后一行必须是 [CHAT_LOOP_STATUS:CONTINUE]、'
|
|
110
|
+
+'[CHAT_LOOP_STATUS:DONE] 或 [CHAT_LOOP_STATUS:BLOCKED]。'
|
|
111
|
+
+(permit.modelTurnNote||'');
|
|
112
|
+
const begun=await ask('CHAT_LOOP_TASK_HANDOFF_SEND_BEGIN_V2')
|
|
113
|
+
.catch(()=>null);
|
|
114
|
+
if(begun?.status!=='RECORDED'
|
|
115
|
+
||begun.scope!=='TASK_SCOPED_HANDOFF_SEND_BEGIN')
|
|
116
|
+
throw Error(begun?.code||'SCOPED_HANDOFF_SEND_BEGIN_UNKNOWN');
|
|
117
|
+
started=true;
|
|
118
|
+
show('RUNNING','当前 Task 已围栏旧 Writer,正在对新 Chat 发送一次交接。');
|
|
119
|
+
result=await sendHandoffOnce(newTab.id,handoff);
|
|
120
|
+
}catch(error){
|
|
121
|
+
result={status:'UNVERIFIED',reason:actionErrorCode(error)};
|
|
122
|
+
if(!started){
|
|
123
|
+
await ask('CHAT_LOOP_TASK_HANDOFF_ABORT_V2',{
|
|
124
|
+
reason:result.reason}).catch(()=>null);
|
|
125
|
+
throw Error(result.reason);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const done=await ask('CHAT_LOOP_TASK_HANDOFF_RESULT_V2',{
|
|
129
|
+
result:result.status,newTabId:result.newTabId??newTab?.id??null,
|
|
130
|
+
newUserKey:result.newUserKey??null,newPage:result.newPage??null,
|
|
131
|
+
nextRunCredential:h.nextRunCredential
|
|
132
|
+
}).catch(()=>null);
|
|
133
|
+
if(result.status!=='SENT_CONFIRMED'||done?.status!=='OK'
|
|
134
|
+
||done.scope!=='TASK_SCOPED_HANDOFF_CONFIRMED')
|
|
135
|
+
throw Error(done?.code||result.reason||'SCOPED_HANDOFF_RECEIPT_UNKNOWN');
|
|
136
|
+
// Close the old tab only after Native + Context have promoted the exact
|
|
137
|
+
// new Chat. History remains available for audit; no send is retried.
|
|
138
|
+
const old=await chrome.tabs.get(request.tabId).catch(()=>null);
|
|
139
|
+
if(sameOriginalChat(old,request.tabId,permit.target.page))
|
|
140
|
+
await chrome.tabs.remove(old.id).catch(()=>{});
|
|
141
|
+
show('SUCCESS','同一 Task 已确认新 Chat 的交接和唯一 Owner。');
|
|
142
|
+
await chrome.tabs.remove(own.id).catch(()=>{});
|
|
143
|
+
}
|
|
@@ -204,7 +204,9 @@ async function sendHandoffOnce(tabId, text) {
|
|
|
204
204
|
if (mains.length !== 1) return null;
|
|
205
205
|
const modern = [...mains[0].querySelectorAll(
|
|
206
206
|
'[data-chatgpt-search-unit-key][data-chatgpt-search-message-ids]')];
|
|
207
|
-
const
|
|
207
|
+
const legacy = [...mains[0].querySelectorAll(
|
|
208
|
+
'[data-message-author-role="user"],[data-message-author-role="assistant"]')];
|
|
209
|
+
const rows = legacy.length ? legacy : modern;
|
|
208
210
|
const role = n => n.getAttribute('data-message-author-role')
|
|
209
211
|
|| n.getAttribute('data-chatgpt-search-unit-key')?.split(':').at(-1);
|
|
210
212
|
const idOf = n => n.getAttribute('data-message-id')
|
|
@@ -312,7 +314,7 @@ async function run() {
|
|
|
312
314
|
{type: "CHAT_LOOP_TURN_OBSERVE_V1"}); } catch { /* Reconnect below. */ }
|
|
313
315
|
if (observed?.contract !== "chat-loop.turn-observer.v1") {
|
|
314
316
|
await chrome.scripting.executeScript({target: {tabId: newTab.id},
|
|
315
|
-
files: ["content.js", "loop/observer-inspect.js", "loop/observer-poll.js", "loop/observer-recovery.js", "loop/observer-approval.js", "loop/observer.js"]});
|
|
317
|
+
files: ["content.js", "loop/core.js", "loop/observer-inspect.js", "loop/observer-poll.js", "loop/observer-recovery.js", "loop/observer-approval.js", "loop/observer.js"]});
|
|
316
318
|
observed = await chrome.tabs.sendMessage(newTab.id,
|
|
317
319
|
{type: "CHAT_LOOP_TURN_OBSERVE_V1"});
|
|
318
320
|
}
|
|
@@ -388,6 +390,10 @@ async function run() {
|
|
|
388
390
|
await chrome.tabs.remove(own.id);
|
|
389
391
|
}
|
|
390
392
|
(new URLSearchParams(location.search).has("requestId")
|
|
391
|
-
? import(chrome.runtime.getURL("actions/index.js")).then(m => m.queueModelIntent("handoff"))
|
|
393
|
+
? import(chrome.runtime.getURL("actions/index.js")).then(m => m.queueModelIntent("handoff"))
|
|
394
|
+
: new URLSearchParams(location.search).get('taskFirst')==='1'
|
|
395
|
+
?import(chrome.runtime.getURL('actions/handoff-scoped.js'))
|
|
396
|
+
.then(m=>m.runScopedHandoff({verifyOldChat,waitForNewChat,
|
|
397
|
+
sendHandoffOnce,show,actionErrorCode})):run())
|
|
392
398
|
.catch(error => show("BLOCKED",
|
|
393
399
|
actionErrorCode(error,"HANDOFF_ACTION_FAILED")));
|
|
@@ -16,12 +16,16 @@ export async function queueModelIntent(action) {
|
|
|
16
16
|
let outcome;
|
|
17
17
|
try {
|
|
18
18
|
const query=typeof auth.intent.intentId==='string'||auth.intent.lookup===true;
|
|
19
|
-
outcome = await chrome.runtime.sendMessage(
|
|
20
|
-
type:
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
outcome = await chrome.runtime.sendMessage(auth.intent.taskFirst===true
|
|
20
|
+
?{type:'CHAT_LOOP_TASK_MODEL_INTENT_V2',id,
|
|
21
|
+
action:action.toUpperCase()}
|
|
22
|
+
:{type:query?'CHAT_LOOP_MODEL_INTENT_STATUS_V1':'CHAT_LOOP_MODEL_INTENT_V1',
|
|
23
|
+
id,action:action.toUpperCase(),intent:auth.intent,
|
|
24
|
+
...(auth.traceParent?{traceParent:auth.traceParent}:{})});
|
|
24
25
|
if(outcome?.status!==(query?'STATUS':'QUEUED')
|
|
26
|
+
|| (auth.intent.taskFirst===true
|
|
27
|
+
&&!['TASK_SCOPED_INTENT_STATUS','TASK_SCOPED_INTENT_PENDING']
|
|
28
|
+
.includes(outcome.scope))
|
|
25
29
|
|| !/^[a-f0-9]{32}$/.test(outcome.intentId||'')
|
|
26
30
|
|| (query&&auth.intent.intentId&&outcome.intentId!==auth.intent.intentId)
|
|
27
31
|
|| (!query&&outcome.intentId!==id)
|
|
@@ -4,41 +4,84 @@ const host='com.tomflow.chatloop.extension';
|
|
|
4
4
|
const statusNode=document.getElementById('status');
|
|
5
5
|
const detailsNode=document.getElementById('details');
|
|
6
6
|
const show=(status,text)=>{statusNode.textContent=status;
|
|
7
|
-
detailsNode.textContent=text;
|
|
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
|
+
}
|
|
8
33
|
async function run(){
|
|
9
34
|
const own=await chrome.tabs.getCurrent(),params=new URLSearchParams(location.search);
|
|
10
|
-
const id=params.get('requestId');
|
|
35
|
+
const id=params.get('requestId'),operation=params.get('operation');
|
|
11
36
|
if(!own?.id||own.url!==location.href||[...params.keys()].join(',')!=='requestId,operation'
|
|
12
|
-
|
|
37
|
+
||!['cancel','recover-handoff'].includes(operation)
|
|
38
|
+
||!/^[a-f0-9]{32}$/.test(id||''))
|
|
13
39
|
throw Error('INVALID_SESSION_ACTION');
|
|
14
40
|
const acquired=await chrome.runtime.sendNativeMessage(host,{
|
|
15
41
|
op:'acquire',id,action:'session'
|
|
16
42
|
});
|
|
17
43
|
if(acquired?.status!=='OK'||acquired.id!==id
|
|
18
44
|
||acquired.action!=='session'||typeof acquired.nonce!=='string'
|
|
19
|
-
||acquired.intent?.operation!==
|
|
45
|
+
||acquired.intent?.operation!==operation)
|
|
20
46
|
throw Error(acquired?.code||'SESSION_INTENT_UNVERIFIED');
|
|
21
|
-
show('RUNNING','核验 Session、停止调度并撤销 Context 写权…');
|
|
22
47
|
let result;
|
|
23
48
|
try{
|
|
49
|
+
if(operation==='recover-handoff')await confirmRecoveryClick(acquired.intent);
|
|
50
|
+
show('RUNNING',operation==='cancel'
|
|
51
|
+
?'核验 Session、停止调度并撤销 Context 写权…'
|
|
52
|
+
:'核验原 Handoff 未发送证据及一次性恢复许可;不会发送消息…');
|
|
24
53
|
result=await chrome.runtime.sendMessage({
|
|
25
|
-
type:
|
|
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
|
|
26
59
|
});
|
|
27
|
-
if(result?.status!=='OK'||result.state!=='
|
|
28
|
-
|
|
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');
|
|
29
63
|
const receipt=await chrome.runtime.sendNativeMessage(host,{
|
|
30
64
|
op:'complete',id,nonce:acquired.nonce,action:'session',
|
|
31
65
|
status:'OK',payload:JSON.stringify(result)
|
|
32
66
|
});
|
|
33
67
|
if(receipt?.status!=='SAVED'||receipt.id!==id)
|
|
34
68
|
throw Error(receipt?.code||'SESSION_RESULT_UNVERIFIED');
|
|
35
|
-
show('SUCCESS','
|
|
69
|
+
show('SUCCESS',operation==='cancel'
|
|
70
|
+
?'Session 已安全取消;业务任务未交付。'
|
|
71
|
+
:'原 Task Writer 已恢复;本次没有发送 Chat 消息。');
|
|
36
72
|
await chrome.tabs.remove(own.id);
|
|
37
73
|
}catch(error){
|
|
38
74
|
// The Worker may have fenced Context before a transport failure. Never
|
|
39
75
|
// retry a mutation; the original request and persisted receipt are proof.
|
|
40
76
|
const raw=error?.message;
|
|
41
|
-
const code=/^[A-Z][A-Z0-9_]{1,95}$/.test(raw||'')?raw:'
|
|
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
|
+
}
|
|
42
85
|
const receipt=await chrome.runtime.sendNativeMessage(host,{
|
|
43
86
|
op:'complete',id,nonce:acquired.nonce,action:'session',
|
|
44
87
|
status:'FAILED',code
|
|
@@ -184,6 +184,40 @@ async function run(){
|
|
|
184
184
|
||typeof proof.nonce!=='string')throw Error(proof?.code||'START_NATIVE_INVALID');
|
|
185
185
|
let prepared,taken,newTab,committed;
|
|
186
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
|
+
}
|
|
187
221
|
prepared=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_PREPARE_V1',
|
|
188
222
|
requestId:id,...(proof.traceParent?{traceParent:proof.traceParent}:{})});
|
|
189
223
|
if(prepared?.status!=='PREPARED')throw Error(prepared?.code||'START_PREPARE_DENIED');
|
|
@@ -8,8 +8,22 @@ async function run(){
|
|
|
8
8
|
const proof=await chrome.runtime.sendNativeMessage(host,{op:'acquire',id,action:'status'});
|
|
9
9
|
if(proof?.status!=='OK'||proof.action!=='status'||proof.id!==id)
|
|
10
10
|
throw Error(proof?.code||'STATUS_NATIVE_INVALID');
|
|
11
|
-
const
|
|
12
|
-
|
|
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
|
+
}
|
|
13
27
|
const receipt=await chrome.runtime.sendNativeMessage(host,{op:'complete',id,
|
|
14
28
|
nonce:proof.nonce,action:'status',status:'OK',payload:JSON.stringify(state)});
|
|
15
29
|
if(receipt?.status!=='SAVED')throw Error(receipt?.code||'STATUS_NOT_STORED');
|
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
// Chrome MV3 classic service Worker. Shared lexical context; exact order
|
|
2
2
|
// preserves the original single-Worker Owner and serialized Action lifecycle.
|
|
3
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");
|
|
4
7
|
importScripts("worker/bootstrap.js");
|
|
5
8
|
importScripts("worker/admission.js");
|
|
6
9
|
importScripts("worker/adopt-prepare.js");
|
|
7
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");
|
|
8
23
|
importScripts("worker/session-control.js");
|
|
9
24
|
importScripts("worker/readiness.js");
|
|
10
25
|
importScripts("worker/turn-failure.js");
|
|
@@ -14,4 +29,5 @@ importScripts("worker/manual.js");
|
|
|
14
29
|
importScripts("worker/action-take.js");
|
|
15
30
|
importScripts("worker/action-result.js");
|
|
16
31
|
importScripts("worker/scheduler.js");
|
|
32
|
+
importScripts("worker/task-routes-v2.js");
|
|
17
33
|
importScripts("worker/router.js");
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Browser Worker API root; npm index.mjs is for Node only.
|
|
2
|
-
importScripts('loop/core.js','loop/resources.js');
|
|
2
|
+
importScripts('loop/core.js','loop/resource-maintenance.js','loop/resources.js');
|
|
3
3
|
importScripts('loop/action-catalog.generated.js');
|
|
4
4
|
importScripts('loop/api-aspect.generated.js');
|
|
5
5
|
importScripts('loop/api-observation-queue.generated.js');
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Read-only Observer helper: no composer writes, Chrome Owner or sends.
|
|
2
2
|
(() => {
|
|
3
3
|
globalThis.ChatLoopObserverPollV1=({session,inspect,emit,resetCandidate,
|
|
4
|
-
fingerprint,STABLE_MS,getState})=>{
|
|
4
|
+
fingerprint,STABLE_MS,getState,reportBlocked})=>{
|
|
5
5
|
function poll() {
|
|
6
6
|
try {
|
|
7
7
|
const snap = inspect();
|
|
@@ -109,11 +109,12 @@ function poll() {
|
|
|
109
109
|
|| session.baselineUser && snap.userKey !== session.baselineUser)
|
|
110
110
|
&& session.reportedErrorUser !== snap.userKey) {
|
|
111
111
|
session.reportedErrorUser = snap.userKey;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
const detail={page:snap.path,userKey:snap.userKey,
|
|
113
|
+
reason:snap.errorReason,retryAvailable:snap.retryAvailable===true};
|
|
114
|
+
void Promise.resolve().then(()=>reportBlocked
|
|
115
|
+
?reportBlocked(detail)
|
|
116
|
+
:chrome.runtime.sendMessage({
|
|
117
|
+
type:'CHAT_LOOP_TURN_BLOCKED_V1',...detail})).catch(()=>{});
|
|
117
118
|
}
|
|
118
119
|
return emit(snap.fatal ? "BLOCKED" : "UNKNOWN",
|
|
119
120
|
snap.fatal ? snap.errorReason : "MODAL_PRESENT", snap);
|
|
@@ -66,10 +66,13 @@
|
|
|
66
66
|
detail: {contract: CONTRACT, page: snap.path,
|
|
67
67
|
userKey: snap.userKey, assistantKey: snap.assistantKey}
|
|
68
68
|
}));
|
|
69
|
-
//
|
|
70
|
-
//
|
|
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.
|
|
71
74
|
if (typeof chrome.runtime.sendMessage === "function") {
|
|
72
|
-
|
|
75
|
+
const legacy=()=>chrome.runtime.sendMessage({
|
|
73
76
|
type: "CHAT_LOOP_TURN_FINISHED_V1", page: snap.path,
|
|
74
77
|
userKey: snap.userKey, assistantKey: snap.assistantKey,
|
|
75
78
|
replyText: snap.text, startedAt: session.firstTurnAt || Date.now(),
|
|
@@ -77,7 +80,29 @@
|
|
|
77
80
|
priorAssistantKey: snap.priorAssistantKey || null,
|
|
78
81
|
priorAssistantUserKey:snap.priorAssistantUserKey || null,
|
|
79
82
|
directReplyAfterPriorAssistant:snap.directReplyAfterPriorAssistant===true
|
|
80
|
-
})
|
|
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(()=>{});
|
|
81
106
|
}
|
|
82
107
|
}
|
|
83
108
|
return state;
|
|
@@ -86,8 +111,21 @@
|
|
|
86
111
|
session.candidate = "";
|
|
87
112
|
session.stableSince = 0;
|
|
88
113
|
}
|
|
89
|
-
|
|
90
|
-
|
|
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});
|
|
91
129
|
function recordFirstChatSubmission(event) {
|
|
92
130
|
if (!event?.target?.closest) return;
|
|
93
131
|
const fromHome = !conversationPath() && !session.path;
|
|
@@ -130,17 +168,36 @@
|
|
|
130
168
|
return onRecovery(message,reply);
|
|
131
169
|
if(message?.type==='CHAT_LOOP_APPROVAL_EVIDENCE_V1')
|
|
132
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
|
+
}
|
|
133
188
|
if (message?.type === "CHAT_LOOP_TURN_VERIFY_V1") {
|
|
134
189
|
// Explicit manual Action: only a fresh read-only completed-Turn candidate.
|
|
135
190
|
// This does not emit a completion event or write anything to the page.
|
|
136
191
|
const snap = inspect();
|
|
137
192
|
const ready = Boolean(snap.path && snap.userKey && snap.assistantKey
|
|
138
193
|
&& snap.text && snap.composerReady && snap.copyAction
|
|
139
|
-
&& !snap.stop && !snap.busy && !snap.
|
|
194
|
+
&& !snap.stop && !snap.busy && !snap.pendingConfirmation
|
|
195
|
+
&& !snap.toolUnknown && !snap.fatal
|
|
140
196
|
&& !snap.dialog && (!snap.turnState || snap.turnState === "complete"));
|
|
141
197
|
reply({contract: CONTRACT, status: ready ? "READY" : "UNVERIFIED",
|
|
142
198
|
page: snap.path || null, userKey: snap.userKey || null,
|
|
143
199
|
assistantKey: snap.assistantKey || null,
|
|
200
|
+
pendingConfirmation:snap.pendingConfirmation===true,
|
|
144
201
|
replyText: ready ? snap.text : null});
|
|
145
202
|
return false;
|
|
146
203
|
}
|