@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.
Files changed (175) hide show
  1. package/node_modules/@tomflow/chatloop-context/README.md +296 -0
  2. package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
  3. package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
  4. package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
  5. package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +290 -0
  6. package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
  7. package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
  8. package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
  9. package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
  10. package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
  11. package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
  12. package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
  13. package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +261 -0
  14. package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
  15. package/node_modules/@tomflow/chatloop-context/index.mjs +238 -0
  16. package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
  17. package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +77 -0
  18. package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +2036 -0
  19. package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +69 -0
  20. package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +338 -0
  21. package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
  22. package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +286 -0
  23. package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +117 -0
  24. package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
  25. package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +134 -0
  26. package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
  27. package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
  28. package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +180 -0
  29. package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +68 -0
  30. package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
  31. package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +367 -0
  32. package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +398 -0
  33. package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +264 -0
  34. package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
  35. package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
  36. package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
  37. package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +118 -0
  38. package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
  39. package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +316 -0
  40. package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
  41. package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
  42. package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +117 -0
  43. package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +142 -0
  44. package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
  45. package/node_modules/@tomflow/chatloop-context/package.json +38 -0
  46. package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
  47. package/node_modules/@tomflow/chatloop-extension/README.md +190 -0
  48. package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +13 -0
  49. package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +168 -0
  50. package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +246 -0
  51. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +1 -0
  52. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +1 -0
  53. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +1 -0
  54. package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +1 -0
  55. package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +1 -0
  56. package/node_modules/@tomflow/chatloop-extension/capabilities/entry_test.mjs +1 -0
  57. package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +1 -0
  58. package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +1 -0
  59. package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +1 -0
  60. package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +1 -0
  61. package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +1 -0
  62. package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +123 -0
  63. package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +183 -0
  64. package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +390 -0
  65. package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
  66. package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +241 -0
  67. package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
  68. package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +399 -0
  69. package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
  70. package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +60 -0
  71. package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +9 -0
  72. package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +20 -0
  73. package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +77 -0
  74. package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +95 -0
  75. package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +329 -0
  76. package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +38 -0
  77. package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
  78. package/node_modules/@tomflow/chatloop-extension/extension/background.js +33 -0
  79. package/node_modules/@tomflow/chatloop-extension/extension/clean.html +5 -0
  80. package/node_modules/@tomflow/chatloop-extension/extension/content.js +40 -0
  81. package/node_modules/@tomflow/chatloop-extension/extension/continue.html +9 -0
  82. package/node_modules/@tomflow/chatloop-extension/extension/dom.html +17 -0
  83. package/node_modules/@tomflow/chatloop-extension/extension/entry_test.html +6 -0
  84. package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +9 -0
  85. package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +2 -0
  86. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +275 -0
  87. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +84 -0
  88. package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +107 -0
  89. package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +36 -0
  90. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +299 -0
  91. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +144 -0
  92. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +172 -0
  93. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
  94. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +211 -0
  95. package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
  96. package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +399 -0
  97. package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +43 -0
  98. package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
  99. package/node_modules/@tomflow/chatloop-extension/extension/popup.js +99 -0
  100. package/node_modules/@tomflow/chatloop-extension/extension/reload.html +17 -0
  101. package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +17 -0
  102. package/node_modules/@tomflow/chatloop-extension/extension/session.html +7 -0
  103. package/node_modules/@tomflow/chatloop-extension/extension/start.html +6 -0
  104. package/node_modules/@tomflow/chatloop-extension/extension/status.html +6 -0
  105. package/node_modules/@tomflow/chatloop-extension/extension/text.html +17 -0
  106. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +136 -0
  107. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +218 -0
  108. package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +127 -0
  109. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +296 -0
  110. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +186 -0
  111. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +126 -0
  112. package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +287 -0
  113. package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +163 -0
  114. package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +345 -0
  115. package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +209 -0
  116. package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +385 -0
  117. package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +75 -0
  118. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-action-v2.js +278 -0
  119. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-recovery-v2.js +52 -0
  120. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-v2.js +93 -0
  121. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-continue-recovery-v2.js +78 -0
  122. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-recovery-v2.js +148 -0
  123. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-v2.js +325 -0
  124. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-hold-recovery-v2.js +109 -0
  125. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-dispatch-v2.js +220 -0
  126. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-v2.js +106 -0
  127. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-owner-resolver.js +110 -0
  128. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-recover-v2.js +209 -0
  129. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-routes-v2.js +357 -0
  130. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-run-store.js +400 -0
  131. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-scheduler.js +78 -0
  132. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-start-v2.js +361 -0
  133. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-terminal-v2.js +235 -0
  134. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +197 -0
  135. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +205 -0
  136. package/node_modules/@tomflow/chatloop-extension/index.mjs +78 -0
  137. package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +19 -0
  138. package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +82 -0
  139. package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +378 -0
  140. package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +89 -0
  141. package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
  142. package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +122 -0
  143. package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +75 -0
  144. package/node_modules/@tomflow/chatloop-extension/lib/owner-kernel-lock.mjs +106 -0
  145. package/node_modules/@tomflow/chatloop-extension/lib/owner-ledger.mjs +316 -0
  146. package/node_modules/@tomflow/chatloop-extension/lib/owner-locked-write.mjs +30 -0
  147. package/node_modules/@tomflow/chatloop-extension/lib/scoped-cancel-v2.mjs +190 -0
  148. package/node_modules/@tomflow/chatloop-extension/lib/scoped-continue-v2.mjs +214 -0
  149. package/node_modules/@tomflow/chatloop-extension/lib/scoped-handoff-v2.mjs +338 -0
  150. package/node_modules/@tomflow/chatloop-extension/lib/scoped-native-control.mjs +398 -0
  151. package/node_modules/@tomflow/chatloop-extension/lib/scoped-recovery-v2.mjs +234 -0
  152. package/node_modules/@tomflow/chatloop-extension/lib/scoped-retire-v2.mjs +70 -0
  153. package/node_modules/@tomflow/chatloop-extension/lib/scoped-terminal-v2.mjs +143 -0
  154. package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +228 -0
  155. package/node_modules/@tomflow/chatloop-extension/package.json +36 -0
  156. package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +138 -0
  157. package/node_modules/@tomflow/chatloop-trace/README.md +118 -0
  158. package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
  159. package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
  160. package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
  161. package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
  162. package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
  163. package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +308 -0
  164. package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +140 -0
  165. package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +236 -0
  166. package/node_modules/@tomflow/chatloop-trace/package.json +28 -0
  167. package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
  168. package/node_modules/ps-list/index.d.ts +76 -0
  169. package/node_modules/ps-list/index.js +366 -0
  170. package/node_modules/ps-list/license +9 -0
  171. package/node_modules/ps-list/package.json +44 -0
  172. package/node_modules/ps-list/readme.md +56 -0
  173. package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
  174. package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
  175. package/package.json +12 -4
@@ -0,0 +1,399 @@
1
+ // One bounded inventory for the Worker; no ChatGPT page sends or input.
2
+ (() => {
3
+ const KEY = 'chat-loop.resources.v1';
4
+ const ACTION_LIMIT = 3; // Includes machine Eye, Start, manual and timed Actions.
5
+ const CHAT_LIMIT = 2; // Shared physical capacity, NOT a global Task/Owner count.
6
+ const AUDIT_TTL = 60_000; // Reap obsolete audit pages from installed older Extension versions.
7
+ const READ_TTL = 180_000;
8
+ const TERMINAL_TTL = 30_000;
9
+ const READ_ONLY = new Set(['status','text','dom','screenshot','reload']);
10
+ const ACTIONS = new Set(['start','continue','handoff','status','text','dom','screenshot','reload','clean','session']);
11
+ const validId = s => /^[a-f0-9]{32}$/.test(s || '');
12
+ const validTabId = s => /^[1-9][0-9]*$/.test(s || '');
13
+ const validScope = s => /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(s||'');
14
+ const scopeOf = run => validScope(run?.projectId)&&validScope(run?.taskId)?{projectId:run.projectId,taskId:run.taskId}:{};
15
+ const sameSession = (entry,run)=>{
16
+ if(entry.sessionId!==(run.sessionId||run.token))return false;
17
+ // V2 TaskKey cannot inherit a V1 entry without an exact scope.
18
+ if(run.taskId!==undefined||entry.taskId!==undefined){
19
+ return validScope(run.projectId)&&validScope(run.taskId)
20
+ &&entry.projectId===run.projectId&&entry.taskId===run.taskId;
21
+ }
22
+ // Restrict legacy V1 unscoped entries until its fence retires.
23
+ return entry.projectId===undefined||run.projectId===undefined
24
+ ||entry.projectId===run.projectId;
25
+ };
26
+ const extensionBase = () => chrome.runtime.getURL('');
27
+ function classify(tab) {
28
+ if (!Number.isInteger(tab?.id) || !tab.url) return null;
29
+ let u;
30
+ try {u = new URL(tab.url);} catch {return null;}
31
+ if (u.protocol !== 'chrome-extension:' || !tab.url.startsWith(extensionBase())
32
+ || u.host !== new URL(extensionBase()).host) return null;
33
+ if (u.pathname === '/popup.html' && u.search === '?audit=1')
34
+ return {kind:'audit'}; // Legacy page reaping only; old audit UI is not executable.
35
+ const name = u.pathname.match(/^\/([a-z]+)\.html$/)?.[1];
36
+ if (!ACTIONS.has(name)) return null;
37
+ if (name==='session' && u.searchParams.size===2
38
+ && validId(u.searchParams.get('requestId'))
39
+ && ['cancel','recover-handoff'].includes(u.searchParams.get('operation')))
40
+ return {kind:'machine',name,requestId:u.searchParams.get('requestId')};
41
+ if (u.searchParams.size === 1 && validId(u.searchParams.get('requestId')))
42
+ return {kind:READ_ONLY.has(name) || ['continue','handoff'].includes(name)
43
+ ? 'read-only' : 'machine', name, requestId:u.searchParams.get('requestId')};
44
+ if (['continue','handoff'].includes(name)
45
+ && (u.searchParams.size === 2
46
+ ||u.searchParams.size===3&&u.searchParams.get('taskFirst')==='1')
47
+ && validTabId(u.searchParams.get('tabId'))
48
+ && /^[0-9a-f-]{36}$/i.test(u.searchParams.get('token') || ''))
49
+ return {kind:'send', name, token:u.searchParams.get('token'),
50
+ targetId:Number(u.searchParams.get('tabId'))};
51
+ return {kind:'manual', name};
52
+ }
53
+ const initial = () => ({created:0, observed:0, closed:0, denied:0,
54
+ peakActions:0, peakChats:0, lastDenial:null, entries:[],pendingChats:[]});
55
+ const validHold = h=>h&&typeof h==='object'&&!Array.isArray(h)
56
+ &&typeof h.token==='string'&&/^[0-9a-f-]{36}$/i.test(h.token)
57
+ &&typeof h.sessionId==='string'&&h.sessionId.length>=8
58
+ &&validScope(h.projectId)
59
+ &&(h.taskId===null||validScope(h.taskId))
60
+ &&Number.isSafeInteger(h.actionTabId)&&h.actionTabId>0
61
+ &&Number.isSafeInteger(h.createdAt)&&h.createdAt>0
62
+ &&Object.keys(h).length===6;
63
+ const load = async () => {
64
+ const value=(await chrome.storage.local.get(KEY))[KEY];
65
+ if(value===undefined)return initial();
66
+ // Only an ABSENT key is fresh; corrupt quota or holds fail closed.
67
+ if(!value||typeof value!=='object'||Array.isArray(value)
68
+ ||!Array.isArray(value.entries))throw Error('RESOURCE_LEDGER_INVALID');
69
+ const pendingChats=value.pendingChats===undefined?[]:value.pendingChats;
70
+ if(!Array.isArray(pendingChats)||pendingChats.length>32
71
+ ||pendingChats.some(h=>!validHold(h))
72
+ ||['token','sessionId','actionTabId'].some(k=>
73
+ new Set(pendingChats.map(h=>String(h[k]).toLowerCase())).size!==pendingChats.length))
74
+ throw Error('RESOURCE_HOLDS_INVALID');
75
+ return {...value,pendingChats};
76
+ };
77
+ const save = state => chrome.storage.local.set({[KEY]:state});
78
+ const cleanEntry = x => x && Number.isInteger(x.id) && typeof x.url === 'string';
79
+ const {safeRemove,noDraft,actionTabMatches,confirmTransfer,activeChat,
80
+ finishedSendAction,noSuccessorForAbortedHandoff}=globalThis.ChatLoopResourceMaintenanceV1||{};
81
+ if([safeRemove,noDraft,actionTabMatches,confirmTransfer,activeChat,finishedSendAction,noSuccessorForAbortedHandoff].some(x=>typeof x!=='function'))
82
+ throw Error('RESOURCE_MAINTENANCE_UNAVAILABLE');
83
+ async function reconcile(run, {reap=true}={}) {
84
+ // Explicit Task snapshots only; never infer the latest Task.
85
+ const runs=(Array.isArray(run)?run:[run]).filter(Boolean);
86
+ if(runs.length>32||runs.some(r=>!r||typeof r!=='object'
87
+ ||Array.isArray(r)||!(typeof r.sessionId==='string'
88
+ ||typeof r.token==='string')))
89
+ throw Error('RESOURCE_TASK_SNAPSHOT_INVALID');
90
+ const state=await load(),now=Date.now();
91
+ const all=await chrome.tabs.query({});
92
+ const live=new Map(all.filter(t=>Number.isInteger(t.id)).map(t=>[t.id,t]));
93
+ const kept=[];
94
+ for (const entry of state.entries.filter(cleanEntry)) {
95
+ const tab=live.get(entry.id);
96
+ const pendingAction=entry.kind==='action'&&tab?.url==='about:blank'&&tab.pendingUrl===entry.url;
97
+ if (!tab || tab.url!==entry.url && entry.kind!=='chat'
98
+ &&!pendingAction) {
99
+ if (!tab) state.closed++;
100
+ continue;
101
+ }
102
+ // A Chat URL may change from blank to canonical only via markConfirmed.
103
+ if (entry.kind==='chat' && tab.url!==entry.url) {
104
+ kept.push(entry);continue; // Preserve provenance; never delete on unknown navigation.
105
+ }
106
+ kept.push(entry);
107
+ }
108
+ state.entries=kept;
109
+ for (const tab of all) {
110
+ const info=classify(tab);
111
+ if (!info || state.entries.some(e=>e.id===tab.id)) continue;
112
+ state.entries.push({id:tab.id,url:tab.url,kind:'action',type:info.kind,
113
+ name:info.name||null,firstSeenAt:now,owned:false});
114
+ state.observed++;
115
+ }
116
+ // Recover exact opener provenance after MV3 crash; others stay unowned.
117
+ for (const tab of all) {
118
+ if (!Number.isInteger(tab.id)||!Number.isInteger(tab.openerTabId)
119
+ || !/^https:\/\/chatgpt\.com\/(?:|c\/[0-9a-f-]{12,})$/i.test(tab.url||'')
120
+ || state.entries.some(e=>e.id===tab.id)) continue;
121
+ const opener=live.get(tab.openerTabId), proof=classify(opener);
122
+ const matched=runs.filter(r=>{
123
+ const start=proof?.name==='start'&&proof.kind==='machine'
124
+ &&[r.machineActionTabId,r.lastStartActionTabId].includes(opener?.id);
125
+ const handoff=proof?.name==='handoff'&&proof.kind==='send'
126
+ &&(proof.token===r.token||r.taskId&&r.phase==='PENDING'
127
+ &&r.modelIntent?.action==='HANDOFF'
128
+ &&r.modelIntent.actionTabId===opener?.id&&proof.token===r.turnToken)
129
+ &&[r.tabId,r.sourceTabId].includes(proof.targetId);
130
+ return (start||handoff)&&(r.sessionId||r.token);
131
+ });
132
+ // Ambiguous source stays unowned and consumes capacity.
133
+ if(matched.length!==1)continue;
134
+ const owner=matched[0];
135
+ state.entries.push({id:tab.id,url:tab.url,kind:'chat',
136
+ sessionId:owner.sessionId||owner.token,...scopeOf(owner),
137
+ firstSeenAt:now,confirmed:false,
138
+ testOwned:/^cl-intent-e2e-[a-z0-9-]+$/i.test(owner.projectId||'')});
139
+ state.observed++;state.created++;
140
+ }
141
+ if (reap) {
142
+ for (const entry of [...state.entries]) {
143
+ const tab=live.get(entry.id);
144
+ if (!tab || tab.active || tab.pinned) continue;
145
+ const age=now-(entry.firstSeenAt||now);
146
+ if (entry.kind==='action') {
147
+ const info=classify(tab);
148
+ if (!info || tab.url!==entry.url) continue;
149
+ if (info.kind==='audit' && age>=AUDIT_TTL)
150
+ await safeRemove(tab,entry,state,'STALE_AUDIT');
151
+ else if (info.kind==='read-only' && age>=READ_TTL)
152
+ await safeRemove(tab,entry,state,'STALE_READ_ONLY_NO_SEND');
153
+ else if(info.kind==='send'&&age>=TERMINAL_TTL
154
+ &&finishedSendAction(info,entry,runs))
155
+ await safeRemove(tab,entry,state,'CONFIRMED_TERMINAL_ACTION');
156
+ else if (info.kind==='machine'&&info.name==='start'
157
+ &&age>=TERMINAL_TTL
158
+ &&runs.some(r=>['DONE','BLOCKED'].includes(r.phase)
159
+ &&[r.lastStartActionTabId,r.machineActionTabId].includes(tab.id)))
160
+ await safeRemove(tab,entry,state,'CONFIRMED_TERMINAL_START');
161
+ } else if (entry.kind==='chat' && entry.confirmed
162
+ && entry.testOwned===true && entry.terminal==='DONE' && age>=TERMINAL_TTL
163
+ && tab.url===entry.url && await noDraft(tab.id)) {
164
+ await safeRemove(tab,entry,state,'DONE_OWNED_CHAT');
165
+ }
166
+ }
167
+ }
168
+ // Count physical live tabs even beyond the bounded evidence index.
169
+ const after=await chrome.tabs.query({});
170
+ // Pending extension Actions still occupy a physical slot.
171
+ const actionCount=after.filter(t=>classify(t)||t.url==='about:blank'
172
+ &&classify({id:t.id,url:t.pendingUrl})).length;
173
+ const afterIds=new Set(after.map(t=>t.id));
174
+ const liveChatEntries=state.entries.filter(e=>e.kind==='chat'
175
+ && afterIds.has(e.id));
176
+ const chatCount=liveChatEntries.length;
177
+ // Retained historical tabs are provenance, not new send permits.
178
+ const activeChatCount=liveChatEntries.filter(e=>activeChat(e,runs,sameSession)).length;
179
+ const unownedHomes=after.filter(t=>t.url==='https://chatgpt.com/'
180
+ && !state.entries.some(e=>e.kind==='chat'&&e.id===t.id)).length;
181
+ state.peakActions=Math.max(state.peakActions,actionCount);
182
+ state.peakChats=Math.max(state.peakChats,chatCount);
183
+ const chatEntries=state.entries.filter(e=>e.kind==='chat');
184
+ const actionEntries=state.entries.filter(e=>e.kind==='action');
185
+ const slots=Math.max(0,32-chatEntries.length);
186
+ state.entries=[...chatEntries,...(slots?actionEntries.slice(-slots):[])];
187
+ const coveredHold=h=>after.some(t=>t.openerTabId===h.actionTabId&&
188
+ (t.url==='https://chatgpt.com/'||state.entries.some(e=>
189
+ e.kind==='chat'&&e.id===t.id&&e.sessionId===h.sessionId
190
+ &&e.projectId===h.projectId&&(e.taskId??null)===h.taskId)));
191
+ const pendingChatSlots=state.pendingChats.filter(h=>!coveredHold(h)).length;
192
+ state.current={actions:actionCount,chats:chatCount,
193
+ activeChats:activeChatCount,unownedHomes,
194
+ pendingChats:state.pendingChats.length,pendingChatSlots,
195
+ totalTabs:after.length,at:now};
196
+ await save(state);
197
+ return {state,actionCount,chatCount,activeChatCount,unownedHomes,
198
+ pendingChatSlots};
199
+ }
200
+ async function admit(run,{kind='action'}={}) {
201
+ const {state,actionCount,chatCount,activeChatCount,unownedHomes,
202
+ pendingChatSlots}=await reconcile(run);
203
+ const blocked=actionCount+(kind==='action'?1:0)>ACTION_LIMIT
204
+ ? 'RESOURCE_ACTION_LIMIT' : activeChatCount+unownedHomes+
205
+ pendingChatSlots+(kind==='chat'?1:0)>CHAT_LIMIT
206
+ ? 'RESOURCE_CHAT_LIMIT' : null;
207
+ if (blocked) {
208
+ state.denied++;state.lastDenial={code:blocked,at:Date.now()};
209
+ await save(state);
210
+ }
211
+ return blocked?{ok:false,code:blocked,actionCount,chatCount}
212
+ :{ok:true,actionCount,chatCount};
213
+ }
214
+ // An unacknowledged Chat retains its reserved seat.
215
+ async function reserveChat(run,actionTabId) {
216
+ if(!Number.isSafeInteger(actionTabId)||actionTabId<=0
217
+ ||run?.phase!=='PENDING'||!validScope(run.projectId)
218
+ ||run.taskId!==undefined&&!validScope(run.taskId)
219
+ ||typeof run.sessionId!=='string'||run.sessionId.length<8
220
+ ||typeof run.token!=='string'||!/^[0-9a-f-]{36}$/i.test(run.token))
221
+ throw Error('RESOURCE_RESERVATION_SCOPE_INVALID');
222
+ const {state,activeChatCount,unownedHomes,
223
+ pendingChatSlots}=await reconcile(run,{reap:false});
224
+ if(state.pendingChats.some(h=>h.token.toLowerCase()===run.token.toLowerCase()
225
+ ||h.sessionId.toLowerCase()===run.sessionId.toLowerCase()||h.actionTabId===actionTabId)){
226
+ return {ok:false,code:'RESOURCE_CHAT_RESERVATION_EXISTS'};
227
+ }
228
+ if(activeChatCount+unownedHomes+pendingChatSlots>=CHAT_LIMIT){
229
+ state.denied++;state.lastDenial={code:'RESOURCE_CHAT_LIMIT',at:Date.now()};
230
+ await save(state);
231
+ return {ok:false,code:'RESOURCE_CHAT_LIMIT'};
232
+ }
233
+ state.pendingChats.push({token:run.token,sessionId:run.sessionId,
234
+ projectId:run.projectId,taskId:run.taskId??null,
235
+ actionTabId,createdAt:Date.now()});
236
+ await save(state);
237
+ return {ok:true,code:'RESOURCE_CHAT_RESERVED'};
238
+ }
239
+ const {reserve:reserveScopedHandoff,verify:verifyScopedHandoffHold}=
240
+ globalThis.ChatLoopResourceMaintenanceV1.createHandoffReservation({
241
+ load,reserveChat,actionTabMatches,validScope});
242
+ async function releaseReservedChat(run,actionTabId){
243
+ const aborted=run?.phase==='BLOCKED'
244
+ &&run.preSendAbort?.id===run.modelIntent?.id
245
+ &&run.preSendAbort?.actionTabId===actionTabId
246
+ &&run.modelIntent?.state==='ACTION_UNVERIFIED'
247
+ &&run.lastIntentReceipt?.id===run.preSendAbort.id&&run.lastIntentReceipt?.status==='NOT_SENT';
248
+ if(!aborted||run.nextAttempt?.sendBeginAt!==null
249
+ ||run.modelIntent?.action!=='HANDOFF'
250
+ ||!validScope(run.projectId)||!validScope(run.taskId)
251
+ ||!Number.isSafeInteger(actionTabId)||actionTabId<=0
252
+ ||!/^[0-9a-f-]{36}$/i.test(run.turnToken||''))
253
+ throw Error('RESOURCE_RELEASE_SEND_NOT_EXCLUDED');
254
+ const state=await load(),matched=state.pendingChats.filter(h=>
255
+ h.token.toLowerCase()===run.turnToken.toLowerCase()
256
+ &&h.sessionId.toLowerCase()===run.sessionId.toLowerCase()&&h.projectId===run.projectId
257
+ &&h.taskId===run.taskId&&h.actionTabId===actionTabId);
258
+ if(state.pendingChats.some(h=>h.token.toLowerCase()===run.turnToken.toLowerCase()&&h!==matched[0]))
259
+ return {released:false,code:'HOLD_SCOPE_CONFLICT'};
260
+ if(!await noSuccessorForAbortedHandoff(run,actionTabId))
261
+ return {released:false,code:'HANDOFF_ACTION_OR_SUCCESSOR_UNVERIFIED'};
262
+ if(!matched.length)return {released:true,scope:'EXACT_UNSENT_CHAT_HOLD',alreadyAbsent:true};
263
+ state.pendingChats=state.pendingChats.filter(h=>h!==matched[0]);await save(state);
264
+ return {released:true,scope:'EXACT_UNSENT_CHAT_HOLD'};
265
+ }
266
+ async function registerReservedChat(tab,run) {
267
+ if(!Number.isSafeInteger(tab?.id)||tab.id<=0||typeof run?.token!=='string'
268
+ ||typeof run.sessionId!=='string'||tab.url!=='https://chatgpt.com/'||run.phase!=='PENDING')return false;
269
+ const quota=await reconcile(run,{reap:false});
270
+ if(quota.activeChatCount+quota.unownedHomes+quota.pendingChatSlots>CHAT_LIMIT)
271
+ return false;
272
+ const state=await load(),hold=state.pendingChats.find(h=>h.token.toLowerCase()===run.token.toLowerCase());
273
+ if(!hold||hold.sessionId.toLowerCase()!==run.sessionId.toLowerCase()
274
+ ||hold.projectId!==run.projectId
275
+ ||hold.taskId!==(run.taskId??null))return false;
276
+ const live=await chrome.tabs.get(tab.id).catch(()=>null);
277
+ if(!live||live.url!==tab.url||live.openerTabId!==hold.actionTabId)
278
+ return false;
279
+ const prior=state.entries.find(e=>e.id===tab.id);
280
+ if(prior&&!(prior.kind==='chat'&&sameSession(prior,run)))return false;
281
+ if(!prior){
282
+ state.entries.push({id:tab.id,url:tab.url,kind:'chat',
283
+ sessionId:run.sessionId,...scopeOf(run),firstSeenAt:Date.now(),
284
+ confirmed:false,testOwned:/^cl-intent-e2e-[a-z0-9-]+$/i.test(run.projectId)});
285
+ state.created++;
286
+ state.peakChats=Math.max(state.peakChats,
287
+ state.entries.filter(e=>e.kind==='chat').length);
288
+ }
289
+ state.pendingChats=state.pendingChats.filter(h=>h.token.toLowerCase()!==hold.token.toLowerCase());
290
+ await save(state);
291
+ return true;
292
+ }
293
+ async function withRegisteredAction(run,{url,create}) {
294
+ if(typeof url!=='string'||!classify({id:1,url})
295
+ ||typeof create!=='function')throw Error('ACTION_URL_UNVERIFIED');
296
+ const quota=await admit(run,{kind:'action'});
297
+ if(!quota.ok)return {ok:false,code:quota.code};
298
+ const tab=await create(); // Only the Worker caller can create a tab.
299
+ if(!Number.isSafeInteger(tab?.id)||tab.id<=0)
300
+ return {ok:false,code:'ACTION_CREATED_ID_UNVERIFIED',tab};
301
+ const registered=await registerAction({id:tab.id,url});
302
+ return registered?{ok:true,tab}
303
+ :{ok:false,code:'ACTION_RESOURCE_UNVERIFIED',tab};
304
+ }
305
+ async function registerAction(tab) {
306
+ const info=classify(tab);
307
+ if(!info)return false;
308
+ // A stale tab ID must not be registered as the Task's Action.
309
+ if(!await actionTabMatches(tab))return false;
310
+ const state=await load(),prior=state.entries.find(e=>e.id===tab.id);
311
+ if(prior){
312
+ if(prior.kind!=='action'||prior.url!==tab.url)return false;
313
+ // Old owned entries may lack type; unowned upgrades need exact kind.
314
+ if(prior.owned!==true){
315
+ if(prior.type!==info.kind)return false;
316
+ prior.owned=true;
317
+ await save(state);
318
+ }
319
+ return true;
320
+ }
321
+ state.entries.push({id:tab.id,url:tab.url,kind:'action',
322
+ type:info.kind,firstSeenAt:Date.now(),owned:true});
323
+ state.created++;
324
+ state.peakActions=Math.max(state.peakActions,
325
+ state.entries.filter(e=>e.kind==='action').length);
326
+ await save(state);
327
+ return true;
328
+ }
329
+ async function registerChat(tab,run) {
330
+ if (!Number.isInteger(tab?.id) || tab.url!=='https://chatgpt.com/'
331
+ || !(run?.sessionId || run?.token) || run.phase!=='PENDING') return false;
332
+ const state=await load();
333
+ const existing=state.entries.find(e=>e.id===tab.id);
334
+ if(existing)return existing.kind==='chat'&&sameSession(existing,run);
335
+ state.entries.push({id:tab.id,url:tab.url,kind:'chat',
336
+ sessionId:run.sessionId||run.token,...scopeOf(run),
337
+ firstSeenAt:Date.now(),confirmed:false,
338
+ testOwned:/^cl-intent-e2e-[a-z0-9-]+$/i.test(run.projectId||'')});
339
+ state.created++;
340
+ state.peakChats=Math.max(state.peakChats,
341
+ state.entries.filter(e=>e.kind==='chat').length);
342
+ await save(state);
343
+ return true;
344
+ }
345
+ async function adoptChat(tab,run) {
346
+ if(!Number.isInteger(tab?.id)||typeof tab.url!=="string"
347
+ || !/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i.test(tab.url)
348
+ ||!run?.sessionId||run.phase!=="PENDING"||run.adoptTabId!==tab.id)
349
+ return false;
350
+ const quota=await reconcile(run,{reap:false});
351
+ if(quota.activeChatCount+quota.unownedHomes+quota.pendingChatSlots>=CHAT_LIMIT)
352
+ return false;
353
+ const state=await load();
354
+ if(state.entries.some(e=>e.id===tab.id))return false; // Existing Owner, even terminal, is not silently replaced.
355
+ state.entries.push({id:tab.id,url:tab.url,kind:"chat",
356
+ sessionId:run.sessionId,...scopeOf(run),
357
+ firstSeenAt:Date.now(),confirmed:true,
358
+ adopted:true,testOwned:false});
359
+ state.created++;
360
+ state.peakChats=Math.max(state.peakChats,
361
+ state.entries.filter(e=>e.kind==="chat").length);
362
+ await save(state);
363
+ return true;
364
+ }
365
+ async function confirmChat(id,sessionId,url,transfer) {
366
+ const state=await load();
367
+ const entry=state.entries.find(e=>e.id===id&&e.kind==='chat'
368
+ &&e.sessionId===sessionId);
369
+ if (!entry || !/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i.test(url)) return;
370
+ if(transfer&&!await confirmTransfer(state,entry,transfer,sameSession,url))return false;
371
+ entry.url=url;entry.confirmed=true;
372
+ await save(state);return transfer?true:undefined;
373
+ }
374
+ async function terminal(run) {
375
+ if (!(run?.sessionId||run?.token)
376
+ || !['DONE','BLOCKED','CANCELLED'].includes(run.phase)) return;
377
+ const state=await load();
378
+ for(const entry of state.entries) if(entry.kind==='chat'
379
+ &&sameSession(entry,run))entry.terminal=run.phase;
380
+ await save(state);
381
+ }
382
+ let operations=Promise.resolve();
383
+ const guard=operation=>(...args)=>{
384
+ const current=operations.then(()=>operation(...args));
385
+ operations=current.then(()=>undefined,()=>undefined);
386
+ return current;
387
+ };
388
+ globalThis.ChatLoopResourcesV1=Object.freeze({
389
+ reconcile:guard(reconcile),admit:guard(admit),
390
+ registerAction:guard(registerAction),registerChat:guard(registerChat),
391
+ adoptChat:guard(adoptChat),confirmChat:guard(confirmChat),
392
+ terminal:guard(terminal),reserveChat:guard(reserveChat),
393
+ releaseReservedChat:guard(releaseReservedChat),
394
+ reserveScopedHandoff:guard(reserveScopedHandoff),
395
+ verifyScopedHandoffHold:guard(verifyScopedHandoffHold),
396
+ registerReservedChat:guard(registerReservedChat),
397
+ withRegisteredAction:guard(withRegisteredAction),
398
+ KEY,ACTION_LIMIT,CHAT_LIMIT});
399
+ })();
@@ -0,0 +1,43 @@
1
+ {
2
+ "manifest_version": 3,
3
+ "name": "Chat Loop Extension",
4
+ "version": "0.4.34",
5
+ "description": "Chat Loop browser companion: Eye actions, Turn observer, Continue and Handoff Actions",
6
+ "permissions": [
7
+ "activeTab",
8
+ "tabs",
9
+ "downloads",
10
+ "clipboardWrite",
11
+ "nativeMessaging",
12
+ "storage",
13
+ "alarms",
14
+ "scripting"
15
+ ],
16
+ "host_permissions": [
17
+ "https://chatgpt.com/*"
18
+ ],
19
+ "background": {
20
+ "service_worker": "background.js"
21
+ },
22
+ "action": {
23
+ "default_title": "Check Chat Loop web environment",
24
+ "default_popup": "popup.html"
25
+ },
26
+ "content_scripts": [
27
+ {
28
+ "matches": [
29
+ "https://chatgpt.com/*"
30
+ ],
31
+ "js": [
32
+ "content.js",
33
+ "loop/core.js",
34
+ "loop/observer-inspect.js",
35
+ "loop/observer-poll.js",
36
+ "loop/observer-recovery.js",
37
+ "loop/observer-approval.js",
38
+ "loop/observer.js"
39
+ ],
40
+ "run_at": "document_idle"
41
+ }
42
+ ]
43
+ }
@@ -0,0 +1,25 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Chat Loop Extension · 环境检测</title>
7
+ <style>
8
+ body { font-family: system-ui, sans-serif; width: 310px; margin: 0; padding: 18px; color: #242424; }
9
+ h1 { font-size: 17px; margin: 0 0 12px; }
10
+ #status { font-size: 15px; font-weight: 650; }
11
+ #detail { color: #60636a; line-height: 1.5; font-size: 13px; }
12
+ a { color: #1269c7; }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <h1>Chat Loop Extension · 环境检查</h1>
17
+ <p id="status" role="status">正在检测…</p>
18
+ <p id="detail">仅检查当前页面与浏览器类型,不读取聊天内容。</p>
19
+ <p>打开动作页即执行,成功后自动关闭。多 Chat 标签页时请在动作页 URL 指定 <code>?tabId=...</code>。</p>
20
+ <p><a href="screenshot.html" target="_blank">Screenshot</a> · <a href="dom.html" target="_blank">DOM</a> · <a href="text.html" target="_blank">Text</a> · <a href="reload.html" target="_blank">Reload</a> · <button id="clean-action" type="button">Clean 输入框</button></p>
21
+ <p><a href="https://chatgpt.com/" target="_blank" rel="noopener noreferrer">打开 ChatGPT 网页版</a></p>
22
+ <p id="action-state" role="status">最近发送动作:读取中…</p>
23
+ <script src="popup.js"></script>
24
+ </body>
25
+ </html>
@@ -0,0 +1,99 @@
1
+ const CONTRACT = "chat-loop.web-environment.v1";
2
+ const statusNode = document.getElementById("status");
3
+ const detailNode = document.getElementById("detail");
4
+
5
+ function show(status, detail) {
6
+ statusNode.textContent = status;
7
+ detailNode.textContent = detail;
8
+ }
9
+
10
+ function requestProof(tabId) {
11
+ return new Promise((resolve) => {
12
+ chrome.tabs.sendMessage(tabId, { type: "CHAT_LOOP_WEB_CHECK_V1" }, (proof) => {
13
+ if (chrome.runtime.lastError) {
14
+ resolve({ status: "EXTENSION_UNVERIFIED" });
15
+ return;
16
+ }
17
+ resolve(proof);
18
+ });
19
+ });
20
+ }
21
+
22
+ async function check() {
23
+ if(typeof chrome.tabs?.query!=='function'){
24
+ show('EXTENSION_API_UNAVAILABLE','Chrome 标签页 API 不可用,请检查扩展状态。');
25
+ return;
26
+ }
27
+ const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
28
+ const tab = tabs[0];
29
+ if (!tab || !tab.url) {
30
+ show("WEB_UNVERIFIED", "无法确认当前 Chrome 标签页。");
31
+ return;
32
+ }
33
+ let url;
34
+ try { url = new URL(tab.url); } catch {
35
+ show("WEB_UNVERIFIED", "当前标签页地址无效。");
36
+ return;
37
+ }
38
+ if (url.protocol !== "https:" || url.hostname !== "chatgpt.com") {
39
+ show("UNSUPPORTED_PAGE", "请在 Chrome 中打开 https://chatgpt.com 后重新检测。");
40
+ return;
41
+ }
42
+ const proof = await requestProof(tab.id);
43
+ if (proof?.contract !== CONTRACT) {
44
+ show("EXTENSION_UNVERIFIED", "页面尚未连接扩展。请刷新 ChatGPT 页面后再打开此扩展。");
45
+ return;
46
+ }
47
+ if (proof.status === "WEB_READY" && proof.browser === "GOOGLE_CHROME") {
48
+ show("WEB_READY", "Chrome 网页环境已验证;这是扩展本地检测结果,尚未回传给 ChatGPT Plugin。");
49
+ return;
50
+ }
51
+ show(proof.status || "WEB_UNVERIFIED",
52
+ "当前浏览器环境尚未得到 Google Chrome 的有效证明。");
53
+ }
54
+
55
+ void check().catch(() => show("WEB_UNVERIFIED", "浏览器检测失败,请重试。"));
56
+
57
+ // Read-only action feedback; never expose the single-use token or message text.
58
+ async function showActionState() {
59
+ const key = "chat-loop.action.run.v1";
60
+ const run = (await chrome.storage.local.get(key))[key];
61
+ const allowed = ["phase", "action", "reason", "tabId", "page", "sourceTabId", "sourcePage",
62
+ "userKey", "assistantKey", "newUserKey", "startedAt",
63
+ "sessionId", "projectId", "generation",
64
+ "deadlineAt", "handoffPending", "handoffPendingAt", "attemptedNewTabId"];
65
+ const summary = {version: chrome.runtime.getManifest().version};
66
+ if (run) for (const key of allowed) if (run[key] !== undefined) summary[key] = run[key];
67
+ document.body.dataset.actionState = JSON.stringify(summary);
68
+ document.getElementById("action-state").textContent = run
69
+ ? "最近发送动作:" + run.action + " · " + run.phase + (run.reason ? " · " + run.reason : "")
70
+ : "尚无发送动作记录";
71
+ }
72
+ const stateUnavailable=()=>{
73
+ document.getElementById("action-state").textContent =
74
+ "最近发送动作:状态无法确认,不要重复发送";
75
+ };
76
+ void showActionState().catch(stateUnavailable);
77
+ chrome.storage.onChanged.addListener((changes,area)=>{
78
+ // Unrelated storage churn must not repeatedly reread the Owner projection.
79
+ if(area==='local'&&Object.hasOwn(changes,'chat-loop.action.run.v1'))
80
+ void showActionState().catch(stateUnavailable);
81
+ });
82
+
83
+ // User-clicked menu action only; bind to the single active ChatGPT tab.
84
+ document.getElementById('clean-action').addEventListener('click',async()=>{
85
+ if(typeof chrome.tabs?.query!=='function'){
86
+ show('CLEAN_BLOCKED','Chrome 标签页 API 不可用,未执行清理。');
87
+ return;
88
+ }
89
+ let selected;
90
+ try{selected=(await chrome.tabs.query({active:true,currentWindow:true}))[0]}
91
+ catch{show('CLEAN_BLOCKED','当前标签页无法验证,未执行清理。');return;}
92
+ if(!selected?.id||!selected?.url?.startsWith('https://chatgpt.com/')){
93
+ show('CLEAN_NOT_AVAILABLE','请先选中要清理的 ChatGPT 标签页。');return;
94
+ }
95
+ const opened=await chrome.runtime.sendMessage({
96
+ type:'CHAT_LOOP_POPUP_CLEAN_OPEN_V1',tabId:selected.id}).catch(()=>null);
97
+ if(opened?.status!=='OK')
98
+ show('CLEAN_BLOCKED',opened?.code||'CLEAN_ACTION_UNVERIFIED');
99
+ });
@@ -0,0 +1,17 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Chat Loop Extension · Reload</title>
6
+ <style>body{font:15px system-ui,sans-serif;padding:24px;max-width:620px;color:#222}
7
+ #status{font-weight:700}#details{white-space:pre-wrap;overflow-wrap:anywhere;color:#555}
8
+ body[data-result="error"] #status{color:#b00020}</style>
9
+ </head>
10
+ <body>
11
+ <h1>Chat Loop Extension · Reload</h1>
12
+ <p id="status" role="status">STARTING</p>
13
+ <p id="details">正在执行,成功后关闭此页面;失败则保留错误以供诊断。</p>
14
+ <script src="actions/common.js"></script>
15
+ <script src="actions/reload.js"></script>
16
+ </body>
17
+ </html>
@@ -0,0 +1,17 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Chat Loop Extension · Screenshot</title>
6
+ <style>body{font:15px system-ui,sans-serif;padding:24px;max-width:620px;color:#222}
7
+ #status{font-weight:700}#details{white-space:pre-wrap;overflow-wrap:anywhere;color:#555}
8
+ body[data-result="error"] #status{color:#b00020}</style>
9
+ </head>
10
+ <body>
11
+ <h1>Chat Loop Extension · Screenshot</h1>
12
+ <p id="status" role="status">STARTING</p>
13
+ <p id="details">正在执行,成功后关闭此页面;失败则保留错误以供诊断。</p>
14
+ <script src="actions/common.js"></script>
15
+ <script src="actions/screenshot.js"></script>
16
+ </body>
17
+ </html>
@@ -0,0 +1,7 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN"><head><meta charset="utf-8"><title>Chat Loop · session</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><h1>Chat Loop · Session</h1><p id="status" role="status">STARTING</p>
5
+ <p id="details">验证精确 Session 和 Context 写权,不直接操作 Chat 页面。</p>
6
+ <button id="confirm-recovery" type="button" hidden>确认恢复原 Task 写权(不会发送 Chat 消息)</button>
7
+ <script src="actions/session.js"></script></body></html>
@@ -0,0 +1,6 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN"><head><meta charset="utf-8"><title>Chat Loop · start</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><h1>Chat Loop · start</h1><p id="status" role="status">STARTING</p>
5
+ <p id="details">只有已确认意图且成功取得全局执行权才会发送首条消息。</p>
6
+ <script src="actions/start.js"></script></body></html>
@@ -0,0 +1,6 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN"><head><meta charset="utf-8"><title>Chat Loop · status</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><h1>Chat Loop · status</h1><p id="status" role="status">STARTING</p>
5
+ <p id="details">只读查询唯一网页执行会话,不执行网页副作用。</p>
6
+ <script src="actions/status.js"></script></body></html>
@@ -0,0 +1,17 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Chat Loop Extension · Text</title>
6
+ <style>body{font:15px system-ui,sans-serif;padding:24px;max-width:620px;color:#222}
7
+ #status{font-weight:700}#details{white-space:pre-wrap;overflow-wrap:anywhere;color:#555}
8
+ body[data-result="error"] #status{color:#b00020}</style>
9
+ </head>
10
+ <body>
11
+ <h1>Chat Loop Extension · Text</h1>
12
+ <p id="status" role="status">STARTING</p>
13
+ <p id="details">正在执行,成功后关闭此页面;失败则保留错误以供诊断。</p>
14
+ <script src="actions/common.js"></script>
15
+ <script src="actions/text.js"></script>
16
+ </body>
17
+ </html>