@tomflow/chatloop-extension 0.3.83 → 0.4.0
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 +74 -73
- package/actions.generated.mjs +11 -0
- package/bin/cli.mjs +22 -24
- package/bin/native-host.mjs +14 -5
- package/capabilities/chat_dom.mjs +1 -0
- package/capabilities/chat_screenshot.mjs +1 -0
- package/capabilities/chat_text.mjs +1 -0
- package/capabilities/clean.mjs +1 -0
- package/capabilities/continue.mjs +1 -0
- package/capabilities/handoff.mjs +1 -0
- package/capabilities/reload.mjs +1 -0
- package/capabilities/start.mjs +1 -0
- package/capabilities/status.mjs +1 -0
- package/extension/actions/common.js +14 -3
- package/extension/actions/continue.js +1 -1
- package/extension/actions/handoff.js +5 -3
- package/extension/actions/index.js +2 -0
- package/extension/actions/read-mode.js +9 -0
- package/extension/actions/start.js +1 -1
- package/extension/background.js +16 -2188
- package/extension/loop/action-catalog.generated.js +2 -0
- package/extension/loop/api-aspect.generated.js +192 -0
- package/extension/loop/core.js +81 -3
- package/extension/loop/index.js +34 -0
- package/extension/loop/observer-approval.js +227 -0
- package/extension/loop/observer-inspect.js +144 -0
- package/extension/loop/observer-poll.js +171 -0
- package/extension/loop/observer-recovery.js +109 -0
- package/extension/loop/observer.js +27 -630
- package/extension/loop/resources.js +8 -1
- package/extension/manifest.json +6 -2
- package/extension/worker/action-result.js +136 -0
- package/extension/worker/action-take.js +220 -0
- package/extension/worker/admission.js +122 -0
- package/extension/worker/adopt-prepare.js +155 -0
- package/extension/worker/adopt-recovery.js +185 -0
- package/extension/worker/adopt-result.js +120 -0
- package/extension/worker/bootstrap.js +172 -0
- package/extension/worker/manual.js +165 -0
- package/extension/worker/readiness.js +242 -0
- package/extension/worker/router.js +150 -0
- package/extension/worker/scheduler.js +147 -0
- package/extension/worker/turn-failure.js +197 -0
- package/extension/worker/turn-finished.js +200 -0
- package/index.mjs +62 -0
- package/lib/action-routing.mjs +18 -0
- package/lib/api-aspect.mjs +190 -0
- package/lib/bridge.mjs +26 -7
- package/lib/context-binding.mjs +6 -1
- package/lib/context-ipc.mjs +44 -2
- package/lib/owner-evidence.mjs +67 -0
- package/lib/session-file.mjs +20 -8
- package/package.json +13 -3
- package/scripts/build-actions.mjs +133 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Read-only Observer helper: no composer writes, Chrome Owner or sends.
|
|
2
|
+
(() => {
|
|
3
|
+
globalThis.ChatLoopObserverInspectV1=({conversationPath,visible,messageId})=>{
|
|
4
|
+
function activeEvidenceConversation() {
|
|
5
|
+
const path=conversationPath();
|
|
6
|
+
if(!path)return null;
|
|
7
|
+
const mains=[...document.querySelectorAll("main")].filter(visible);
|
|
8
|
+
if(mains.length!==1)return null;
|
|
9
|
+
const main=mains[0];
|
|
10
|
+
const legacy=[...main.querySelectorAll(
|
|
11
|
+
'[data-message-author-role="user"],[data-message-author-role="assistant"]')];
|
|
12
|
+
const nodes=legacy.length?legacy:[...main.querySelectorAll(
|
|
13
|
+
'[data-chatgpt-search-unit-key][data-chatgpt-search-message-ids]')];
|
|
14
|
+
const role=node=>node.getAttribute("data-message-author-role")
|
|
15
|
+
||node.getAttribute("data-chatgpt-search-unit-key")?.split(":").at(-1);
|
|
16
|
+
return {path,nodes,role};
|
|
17
|
+
}
|
|
18
|
+
function inspect() {
|
|
19
|
+
const path = conversationPath();
|
|
20
|
+
if (!path) return {path: "", unavailable: "CHAT_NOT_IDENTIFIED"};
|
|
21
|
+
// ChatGPT can keep multiple conversation <main> nodes in the DOM;
|
|
22
|
+
// only the currently rendered one may supply Turn identity or status.
|
|
23
|
+
const mains = [...document.querySelectorAll("main")].filter(visible);
|
|
24
|
+
if (mains.length !== 1) return {path,
|
|
25
|
+
unavailable: mains.length ? "MAIN_AMBIGUOUS" : "MAIN_UNAVAILABLE"};
|
|
26
|
+
const main = mains[0];
|
|
27
|
+
const legacy = [...main.querySelectorAll(
|
|
28
|
+
'[data-message-author-role="user"],[data-message-author-role="assistant"]')];
|
|
29
|
+
const roles = legacy.length ? legacy : [...main.querySelectorAll(
|
|
30
|
+
'[data-chatgpt-search-unit-key][data-chatgpt-search-message-ids]')];
|
|
31
|
+
const roleOf = el => el.getAttribute?.("data-message-author-role")
|
|
32
|
+
|| el.getAttribute?.("data-chatgpt-search-unit-key")?.split(":").at(-1);
|
|
33
|
+
const userIndex = roles.findLastIndex(el => roleOf(el) === "user");
|
|
34
|
+
const lastUser = userIndex >= 0 ? roles[userIndex] : null;
|
|
35
|
+
const afterUser = roles.slice(userIndex + 1).filter(el => roleOf(el) === "assistant");
|
|
36
|
+
const lastAssistant = afterUser.at(-1) || null;
|
|
37
|
+
const lastHistoricAssistant = roles.filter(el => roleOf(el) === "assistant").at(-1);
|
|
38
|
+
const userKey = messageId(lastUser, "user");
|
|
39
|
+
const assistantKey = messageId(lastAssistant, "assistant");
|
|
40
|
+
const historicAssistantKey = messageId(lastHistoricAssistant, "assistant");
|
|
41
|
+
// The immediately preceding assistant before a NEW user Turn is the
|
|
42
|
+
// only eligible confirmation anchor. Historical interactive cards do
|
|
43
|
+
// not block a later assistant Turn or authorize a new action.
|
|
44
|
+
const priorIndex = roles.findLastIndex((el, i) =>
|
|
45
|
+
i < userIndex && roleOf(el) === "assistant");
|
|
46
|
+
const priorAssistant = priorIndex >= 0 ? roles[priorIndex] : null;
|
|
47
|
+
const priorAssistantKey = messageId(priorAssistant, "assistant");
|
|
48
|
+
const priorUser = priorIndex > 0 ? roles.slice(0,priorIndex)
|
|
49
|
+
.filter(el=>roleOf(el)==="user").at(-1) : null;
|
|
50
|
+
const priorAssistantUserKey = messageId(priorUser,"user");
|
|
51
|
+
const directReplyAfterPriorAssistant = priorIndex>=0
|
|
52
|
+
&& !roles.slice(priorIndex+1,userIndex).some(el=>roleOf(el)==="user");
|
|
53
|
+
const scope = lastAssistant?.closest?.('[data-testid^="conversation-turn-"]')
|
|
54
|
+
|| lastAssistant?.closest?.('[data-talvt-turn-state]') || lastAssistant;
|
|
55
|
+
const reply = lastAssistant?.querySelector?.(".markdown") || lastAssistant;
|
|
56
|
+
const text = (reply?.innerText ?? reply?.textContent ?? "").trim();
|
|
57
|
+
// A Chat Loop confirmation is a REAL actionable choice surface in the
|
|
58
|
+
// latest assistant Turn, never a form elsewhere on the page. Disabled
|
|
59
|
+
// Submit still means pending: no user message has yet been submitted.
|
|
60
|
+
const controls = [...scope?.querySelectorAll?.(
|
|
61
|
+
'[role="radiogroup"],[role="radio"],input[type="radio"],' +
|
|
62
|
+
'select,[role="combobox"],textarea,[role="textbox"],' +
|
|
63
|
+
'input[type="checkbox"],[role="checkbox"]') || []];
|
|
64
|
+
const submit = [...scope?.querySelectorAll?.('button') || []].some(button =>
|
|
65
|
+
visible(button) && /(?:提交|确认|submit|confirm)/i.test(
|
|
66
|
+
(button.innerText || button.textContent ||
|
|
67
|
+
button.getAttribute?.("aria-label") || "").trim()));
|
|
68
|
+
const labels = (scope?.innerText || scope?.textContent || "").slice(-2000);
|
|
69
|
+
const pendingConfirmation = Boolean(lastAssistant && submit
|
|
70
|
+
&& controls.some(visible)
|
|
71
|
+
&& /(?:确认|修改|不通过|拒绝|自定义|approval|revise|reject|choice)/i.test(labels));
|
|
72
|
+
const stop = [...document.querySelectorAll(
|
|
73
|
+
'button[data-testid="stop-button"],button[aria-label="Stop generating"],' +
|
|
74
|
+
'button[aria-label="Stop"],button[aria-label="停止生成"],' +
|
|
75
|
+
'button[aria-label="停止响应"]')].some(visible);
|
|
76
|
+
const busy = afterUser.some(node => {
|
|
77
|
+
const part = node.closest?.('[data-testid^="conversation-turn-"]')
|
|
78
|
+
|| node.closest?.('[data-talvt-turn-state]') || node;
|
|
79
|
+
return [...part.querySelectorAll?.('[aria-busy="true"],[data-tool-status="running"],' +
|
|
80
|
+
'[data-state="running"],[data-testid="tool-running"]') || []].some(visible);
|
|
81
|
+
});
|
|
82
|
+
const toolParts = afterUser.flatMap(node => {
|
|
83
|
+
const part = node.closest?.('[data-testid^="conversation-turn-"]')
|
|
84
|
+
|| node.closest?.('[data-talvt-turn-state]') || node;
|
|
85
|
+
return [...part.querySelectorAll?.('[data-testid*="tool"],[data-tool-status]') || []];
|
|
86
|
+
});
|
|
87
|
+
const toolUnknown = toolParts.some(el => {
|
|
88
|
+
if (!visible(el)) return false;
|
|
89
|
+
if (el.matches?.('[data-tool-status="running"],[data-testid="tool-running"]')) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
const status = el.getAttribute?.("data-tool-status")
|
|
93
|
+
|| el.getAttribute?.("data-state") || "";
|
|
94
|
+
// A failed/cancelled tool call can be terminal as a *tool result* while
|
|
95
|
+
// the assistant may continue and finish its own Turn. Page-level errors
|
|
96
|
+
// are handled separately by snap.fatal; never equate tool failure to
|
|
97
|
+
// a completed assistant reply.
|
|
98
|
+
return !/^(done|complete|completed|success|succeeded|finished|failed|error|cancelled|canceled)$/i.test(status)
|
|
99
|
+
&& !el.matches?.('[data-testid*="tool-completed"],[data-testid*="tool-finished"]');
|
|
100
|
+
});
|
|
101
|
+
const editor = document.querySelector(
|
|
102
|
+
'#prompt-textarea[contenteditable="true"],' +
|
|
103
|
+
'[data-testid="composer-input"][contenteditable="true"],' +
|
|
104
|
+
'[contenteditable="true"][data-composer-markdown],' +
|
|
105
|
+
'[contenteditable="true"][data-placeholder]');
|
|
106
|
+
const composerReady = visible(editor) && !editor.closest?.('[aria-disabled="true"]');
|
|
107
|
+
const copyAction = scope && [...scope.querySelectorAll?.(
|
|
108
|
+
'button[data-testid="copy-turn-action-button"],' +
|
|
109
|
+
'button[aria-label="Copy"],button[aria-label="Copy response"],' +
|
|
110
|
+
'button[aria-label="复制"],button[aria-label="复制回复"]') || []].some(visible);
|
|
111
|
+
// A visible interruption banner is not a completed assistant Turn, even
|
|
112
|
+
// when a partial reply has Copy and no Stop button.
|
|
113
|
+
const errorPattern = /unknown error|something went wrong|an error occurred|unable to load|connection (?:lost|interrupted)|waiting for (?:full|complete) response|发生错误|出错了|请求失败|网络错误|连接已中断|正在等待完整答复/i;
|
|
114
|
+
const errorNodes = [...main.querySelectorAll(
|
|
115
|
+
'[role="alert"],[role="status"],[data-testid="conversation-error"],' +
|
|
116
|
+
'[data-testid*="connection-status"]')].filter(visible);
|
|
117
|
+
// Only explicit conversation/context exhaustion is eligible for error Handoff.
|
|
118
|
+
// Generic Unknown error is never evidence of a capacity limit.
|
|
119
|
+
const contextLimitPattern = /(?:conversation (?:has (?:reached|exceeded) (?:its |the )?(?:maximum|context)|is too long)|context (?:window|length|limit) (?:has been |was )?(?:exceeded|reached)|maximum conversation (?:length|limit) (?:reached|exceeded)|对话(?:上下文|长度)(?:已)?(?:达到|超过)(?:上限|限制)|上下文(?:窗口|长度)(?:已)?(?:达到|超过)(?:上限|限制))/i;
|
|
120
|
+
const contextLimit = errorNodes.some(el => contextLimitPattern.test(el.innerText ?? el.textContent ?? ""));
|
|
121
|
+
// An anti-bot/security challenge is not an ordinary generation failure.
|
|
122
|
+
// A visible native Retry is NOT permission to automate bypass attempts.
|
|
123
|
+
const securityChallenge = errorNodes.some(el =>
|
|
124
|
+
/\bcloudflare_challenge\b/i.test(el.innerText ?? el.textContent ?? ""));
|
|
125
|
+
const fatal = contextLimit || securityChallenge || errorNodes.some(el =>
|
|
126
|
+
errorPattern.test(el.innerText ?? el.textContent ?? ""));
|
|
127
|
+
const retryAvailable = !contextLimit && !securityChallenge && errorNodes.some(el =>
|
|
128
|
+
[...el.querySelectorAll?.('button') || []].some(button => visible(button)
|
|
129
|
+
&& /^(?:retry|try again|重试|再试一次)$/i.test(
|
|
130
|
+
(button.innerText || button.textContent || button.getAttribute?.('aria-label') || '').trim())));
|
|
131
|
+
const dialog = [...document.querySelectorAll(
|
|
132
|
+
'[role="dialog"][aria-modal="true"],dialog[open]')].some(visible);
|
|
133
|
+
const turnState = scope?.getAttribute?.("data-talvt-turn-state") || null;
|
|
134
|
+
return {path, userKey, assistantKey, historicAssistantKey, priorAssistantKey,
|
|
135
|
+
priorAssistantUserKey, directReplyAfterPriorAssistant,
|
|
136
|
+
pendingConfirmation, text,
|
|
137
|
+
hasUser: Boolean(lastUser), hasAssistant: Boolean(lastAssistant),
|
|
138
|
+
stop, busy, toolUnknown, turnState, composerReady, copyAction,
|
|
139
|
+
fatal, errorReason: securityChallenge ? "SECURITY_CHALLENGE"
|
|
140
|
+
: contextLimit ? "CONTEXT_LIMIT" : "PAGE_ERROR", retryAvailable, dialog};
|
|
141
|
+
}
|
|
142
|
+
return {activeEvidenceConversation,inspect};
|
|
143
|
+
};
|
|
144
|
+
})();
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Read-only Observer helper: no composer writes, Chrome Owner or sends.
|
|
2
|
+
(() => {
|
|
3
|
+
globalThis.ChatLoopObserverPollV1=({session,inspect,emit,resetCandidate,
|
|
4
|
+
fingerprint,STABLE_MS,getState})=>{
|
|
5
|
+
function poll() {
|
|
6
|
+
try {
|
|
7
|
+
const snap = inspect();
|
|
8
|
+
if (!snap.path) {
|
|
9
|
+
// Preserve a fresh homepage submit, but discard stale submit intent
|
|
10
|
+
// when leaving an already identified conversation.
|
|
11
|
+
if (session.path) session.firstSendSeen = false;
|
|
12
|
+
session.path = "";
|
|
13
|
+
session.sawHomepage = true;
|
|
14
|
+
session.attaching = false;
|
|
15
|
+
session.attachingFirstTurn = false;
|
|
16
|
+
resetCandidate();
|
|
17
|
+
return emit("UNKNOWN", snap.unavailable, snap);
|
|
18
|
+
}
|
|
19
|
+
if (snap.unavailable || snap.hasUser && !snap.userKey) {
|
|
20
|
+
resetCandidate();
|
|
21
|
+
return emit("UNKNOWN", snap.unavailable || "USER_ID_MISSING", snap);
|
|
22
|
+
}
|
|
23
|
+
if (snap.path !== session.path) {
|
|
24
|
+
const cameFromAnotherPage = Boolean(session.path) || session.sawHomepage;
|
|
25
|
+
const recentSend = !session.path && session.firstSendSeen
|
|
26
|
+
&& Date.now() - session.firstSendAt <= 30000;
|
|
27
|
+
const firstTurn = recentSend && Boolean(snap.userKey);
|
|
28
|
+
session.firstSendSeen = false;
|
|
29
|
+
session.sawHomepage = false;
|
|
30
|
+
const previousUser = getState().userKey || null;
|
|
31
|
+
session.path = snap.path;
|
|
32
|
+
session.baselineUser = firstTurn ? null : snap.userKey;
|
|
33
|
+
session.baselineAssistant = firstTurn ? null : snap.historicAssistantKey;
|
|
34
|
+
session.pendingUser = firstTurn ? snap.userKey : null;
|
|
35
|
+
session.completedUser = null;
|
|
36
|
+
resetCandidate();
|
|
37
|
+
session.attachingFirstTurn = recentSend && !snap.userKey;
|
|
38
|
+
// Cold content-script startup can precede historical message hydration.
|
|
39
|
+
// An empty /c/ route is not proof the conversation has no history.
|
|
40
|
+
session.attaching = !recentSend && (cameFromAnotherPage || !snap.userKey);
|
|
41
|
+
session.priorUser = cameFromAnotherPage ? previousUser : null;
|
|
42
|
+
session.attachSignature = "";
|
|
43
|
+
session.attachSince = 0;
|
|
44
|
+
if (firstTurn) return emit("RUNNING", "FIRST_CHAT_TURN_SUBMITTED", snap);
|
|
45
|
+
if (session.attachingFirstTurn) {
|
|
46
|
+
session.baselineUser = null;
|
|
47
|
+
session.baselineAssistant = null;
|
|
48
|
+
return emit("RUNNING", "WAITING_FIRST_USER", snap);
|
|
49
|
+
}
|
|
50
|
+
if (session.attaching) {
|
|
51
|
+
session.baselineUser = null;
|
|
52
|
+
session.baselineAssistant = null;
|
|
53
|
+
return emit("UNKNOWN", "ATTACHING_CHAT", snap);
|
|
54
|
+
}
|
|
55
|
+
return emit(snap.stop || snap.busy ? "UNKNOWN" : "IDLE",
|
|
56
|
+
snap.stop || snap.busy ? "ATTACHED_MID_TURN" : "BASELINE_ESTABLISHED", snap);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (session.attachingFirstTurn) {
|
|
60
|
+
if (Date.now() - session.firstSendAt > 30000) {
|
|
61
|
+
session.firstSendSeen = false;
|
|
62
|
+
session.attachingFirstTurn = false;
|
|
63
|
+
session.attaching = true;
|
|
64
|
+
return emit("UNKNOWN", "FIRST_USER_NOT_CONFIRMED", snap);
|
|
65
|
+
}
|
|
66
|
+
if (!snap.userKey) return emit("RUNNING", "WAITING_FIRST_USER", snap);
|
|
67
|
+
session.firstSendSeen = false;
|
|
68
|
+
session.attachingFirstTurn = false;
|
|
69
|
+
session.pendingUser = snap.userKey;
|
|
70
|
+
return emit("RUNNING", "FIRST_CHAT_TURN_SUBMITTED", snap);
|
|
71
|
+
}
|
|
72
|
+
if (session.attaching) {
|
|
73
|
+
if (!snap.userKey) return emit("UNKNOWN", "WAITING_CHAT_HYDRATION", snap);
|
|
74
|
+
if (session.priorUser && snap.userKey === session.priorUser) {
|
|
75
|
+
session.attachSignature = "";
|
|
76
|
+
return emit("UNKNOWN", "STALE_PREVIOUS_CHAT_DOM", snap);
|
|
77
|
+
}
|
|
78
|
+
if (snap.stop || snap.busy || snap.turnState && snap.turnState !== "complete") {
|
|
79
|
+
session.attachSignature = "";
|
|
80
|
+
return emit("UNKNOWN", "ATTACHED_MID_TURN", snap);
|
|
81
|
+
}
|
|
82
|
+
if (!snap.composerReady || snap.hasAssistant && !snap.assistantKey) {
|
|
83
|
+
session.attachSignature = "";
|
|
84
|
+
return emit("UNKNOWN", "ATTACHING_CHAT", snap);
|
|
85
|
+
}
|
|
86
|
+
const signature = snap.userKey + "|" + (snap.historicAssistantKey || "");
|
|
87
|
+
if (signature !== session.attachSignature) {
|
|
88
|
+
session.attachSignature = signature;
|
|
89
|
+
session.attachSince = Date.now();
|
|
90
|
+
return emit("UNKNOWN", "ATTACHING_CHAT", snap);
|
|
91
|
+
}
|
|
92
|
+
if (Date.now() - session.attachSince < STABLE_MS) {
|
|
93
|
+
return emit("UNKNOWN", "WAITING_CHAT_HYDRATION", snap);
|
|
94
|
+
}
|
|
95
|
+
session.attaching = false;
|
|
96
|
+
session.priorUser = null;
|
|
97
|
+
session.baselineUser = snap.userKey;
|
|
98
|
+
session.baselineAssistant = snap.historicAssistantKey;
|
|
99
|
+
session.pendingUser = null;
|
|
100
|
+
return emit("IDLE", "BASELINE_ESTABLISHED", snap);
|
|
101
|
+
}
|
|
102
|
+
if (snap.fatal || snap.dialog) {
|
|
103
|
+
resetCandidate();
|
|
104
|
+
// A visible page failure must reach the owner, not merely update the
|
|
105
|
+
// content-script's local status. Report once for the specific pending
|
|
106
|
+
// user Turn; Worker independently verifies the same error twice.
|
|
107
|
+
if (snap.fatal && snap.userKey && snap.userKey !== session.completedUser
|
|
108
|
+
&& (snap.userKey === session.pendingUser
|
|
109
|
+
|| session.baselineUser && snap.userKey !== session.baselineUser)
|
|
110
|
+
&& session.reportedErrorUser !== snap.userKey) {
|
|
111
|
+
session.reportedErrorUser = snap.userKey;
|
|
112
|
+
void Promise.resolve(chrome.runtime.sendMessage({
|
|
113
|
+
type: "CHAT_LOOP_TURN_BLOCKED_V1", page: snap.path,
|
|
114
|
+
userKey: snap.userKey, reason: snap.errorReason,
|
|
115
|
+
retryAvailable: snap.retryAvailable === true
|
|
116
|
+
})).catch(() => {});
|
|
117
|
+
}
|
|
118
|
+
return emit(snap.fatal ? "BLOCKED" : "UNKNOWN",
|
|
119
|
+
snap.fatal ? snap.errorReason : "MODAL_PRESENT", snap);
|
|
120
|
+
}
|
|
121
|
+
if (!snap.userKey || snap.userKey === session.baselineUser
|
|
122
|
+
&& session.pendingUser === null) {
|
|
123
|
+
return emit("IDLE", "NO_NEW_USER_TURN", snap);
|
|
124
|
+
}
|
|
125
|
+
if (snap.userKey === session.completedUser) {
|
|
126
|
+
return emit("IDLE", "TURN_ALREADY_EMITTED", snap);
|
|
127
|
+
}
|
|
128
|
+
if (snap.userKey !== session.pendingUser) {
|
|
129
|
+
if (!session.firstTurnAt) session.firstTurnAt = Date.now();
|
|
130
|
+
session.pendingUser = snap.userKey;
|
|
131
|
+
session.candidate = "";
|
|
132
|
+
session.stableSince = 0;
|
|
133
|
+
}
|
|
134
|
+
if (snap.hasAssistant && !snap.assistantKey) {
|
|
135
|
+
resetCandidate();
|
|
136
|
+
return emit("UNKNOWN", "ASSISTANT_ID_MISSING", snap);
|
|
137
|
+
}
|
|
138
|
+
if (!snap.assistantKey || snap.assistantKey === session.baselineAssistant) {
|
|
139
|
+
return emit("RUNNING", "WAITING_FOR_NEW_ASSISTANT", snap);
|
|
140
|
+
}
|
|
141
|
+
if (snap.stop || snap.busy || snap.turnState && snap.turnState !== "complete") {
|
|
142
|
+
session.candidate = "";
|
|
143
|
+
return emit("RUNNING", snap.busy ? "TOOL_OR_ASSISTANT_BUSY" : "STOP_BUTTON_PRESENT", snap);
|
|
144
|
+
}
|
|
145
|
+
if (snap.toolUnknown) {
|
|
146
|
+
session.candidate = "";
|
|
147
|
+
return emit("UNKNOWN", "TOOL_COMPLETION_UNVERIFIED", snap);
|
|
148
|
+
}
|
|
149
|
+
if (!snap.composerReady || !snap.copyAction || !snap.text) {
|
|
150
|
+
session.candidate = "";
|
|
151
|
+
return emit("UNKNOWN", "FINAL_UI_EVIDENCE_MISSING", snap);
|
|
152
|
+
}
|
|
153
|
+
const signature = snap.userKey + "|" + snap.assistantKey + "|" + fingerprint(snap.text);
|
|
154
|
+
if (signature !== session.candidate) {
|
|
155
|
+
session.candidate = signature;
|
|
156
|
+
session.stableSince = Date.now();
|
|
157
|
+
return emit("RUNNING", "ANSWER_NOT_YET_STABLE", snap);
|
|
158
|
+
}
|
|
159
|
+
if (Date.now() - session.stableSince < STABLE_MS) {
|
|
160
|
+
return emit("RUNNING", "WAITING_FOR_STABILITY", snap);
|
|
161
|
+
}
|
|
162
|
+
session.completedUser = snap.userKey;
|
|
163
|
+
return emit("TURN_FINISHED", "EVIDENCE_CONFIRMED", snap, true);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
resetCandidate();
|
|
166
|
+
return emit("UNKNOWN", "OBSERVER_EXCEPTION", {path: session.path});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return poll;
|
|
170
|
+
};
|
|
171
|
+
})();
|
|
@@ -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
|
+
})();
|