@aiyiran/myclaw 1.1.197 → 1.1.198
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/assets/myclaw-inject.js +99 -17
- package/package.json +1 -1
package/assets/myclaw-inject.js
CHANGED
|
@@ -1492,12 +1492,16 @@ btn.addEventListener("click", function () {
|
|
|
1492
1492
|
return (b.lastUpdated || 0) > (a.lastUpdated || 0) ? b : a;
|
|
1493
1493
|
});
|
|
1494
1494
|
// 记下"新项目建好后要自动创建的会话",靠 localStorage 把意图带过整页跳转。
|
|
1495
|
-
// 跳转后的新页面加载时,processPendingSessions()
|
|
1495
|
+
// 跳转后的新页面加载时,processPendingSessions() 会读取;确认可创建后才清除。
|
|
1496
1496
|
try {
|
|
1497
|
-
|
|
1497
|
+
var pendingSessions = {
|
|
1498
1498
|
agentId: newest.id,
|
|
1499
|
-
names: ["程序", "美术", "策划"]
|
|
1500
|
-
|
|
1499
|
+
names: ["程序", "美术", "策划"],
|
|
1500
|
+
createdAt: new Date().toISOString(),
|
|
1501
|
+
myclawVersion: MYCLAW_VERSION
|
|
1502
|
+
};
|
|
1503
|
+
localStorage.setItem(PENDING_SESSIONS_KEY, JSON.stringify(pendingSessions));
|
|
1504
|
+
logNewSession("pending_saved", pendingSessions);
|
|
1501
1505
|
} catch (e) {}
|
|
1502
1506
|
window.location.href = window.location.origin + '/chat?session=' + encodeURIComponent('agent:' + newest.id + ':main');
|
|
1503
1507
|
})
|
|
@@ -1584,6 +1588,75 @@ btn.addEventListener("click", function () {
|
|
|
1584
1588
|
|
|
1585
1589
|
// localStorage 键:跨"新项目跳转"传递"待自动创建的会话"意图
|
|
1586
1590
|
var PENDING_SESSIONS_KEY = "myclaw_pending_sessions";
|
|
1591
|
+
var NEWSESSION_LOG_KEY = "myclaw_newsession_logs";
|
|
1592
|
+
var AUTO_SESSION_WAIT_MS = 120000;
|
|
1593
|
+
var pendingSessionsProcessing = false;
|
|
1594
|
+
|
|
1595
|
+
// 同时写浏览器控制台和 localStorage,便于页面跳转后保留诊断证据。
|
|
1596
|
+
// 日志只记录创建状态,不记录 token、消息内容等敏感信息。
|
|
1597
|
+
function logNewSession(event, details) {
|
|
1598
|
+
var entry = {
|
|
1599
|
+
time: new Date().toISOString(),
|
|
1600
|
+
event: event,
|
|
1601
|
+
details: details || {}
|
|
1602
|
+
};
|
|
1603
|
+
console.log("[myclaw-newsession]", event, entry.details);
|
|
1604
|
+
try {
|
|
1605
|
+
var logs = JSON.parse(localStorage.getItem(NEWSESSION_LOG_KEY) || "[]");
|
|
1606
|
+
if (!Array.isArray(logs)) logs = [];
|
|
1607
|
+
logs.push(entry);
|
|
1608
|
+
if (logs.length > 100) logs = logs.slice(logs.length - 100);
|
|
1609
|
+
localStorage.setItem(NEWSESSION_LOG_KEY, JSON.stringify(logs));
|
|
1610
|
+
} catch (e) {}
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
function pendingSessionState(pending, startedAt) {
|
|
1614
|
+
var app = getOpenclawApp();
|
|
1615
|
+
var btn = findNativeNewSessionBtn();
|
|
1616
|
+
var sessionKey = app && app.sessionKey ? app.sessionKey : null;
|
|
1617
|
+
return {
|
|
1618
|
+
elapsedMs: Date.now() - startedAt,
|
|
1619
|
+
targetAgentId: pending.agentId || null,
|
|
1620
|
+
currentAgentId: deriveAgentId(sessionKey),
|
|
1621
|
+
sessionKey: sessionKey,
|
|
1622
|
+
appReady: !!app,
|
|
1623
|
+
clientReady: !!(app && app.client),
|
|
1624
|
+
connected: !!(app && app.connected),
|
|
1625
|
+
buttonFound: !!btn,
|
|
1626
|
+
buttonDisabled: btn ? !!btn.disabled : null
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
// 等待项目页面、网关和原生新会话按钮真正可用。状态变化及每 5 秒心跳都会留日志。
|
|
1631
|
+
function waitForPendingSessionReady(pending, timeoutMs) {
|
|
1632
|
+
return new Promise(function (resolve) {
|
|
1633
|
+
var startedAt = Date.now();
|
|
1634
|
+
var lastSignature = "";
|
|
1635
|
+
var lastLogAt = 0;
|
|
1636
|
+
(function poll() {
|
|
1637
|
+
var state = pendingSessionState(pending, startedAt);
|
|
1638
|
+
var signature = [state.currentAgentId, state.appReady, state.clientReady, state.connected, state.buttonFound, state.buttonDisabled].join("|");
|
|
1639
|
+
if (signature !== lastSignature || Date.now() - lastLogAt >= 5000) {
|
|
1640
|
+
logNewSession("waiting", state);
|
|
1641
|
+
lastSignature = signature;
|
|
1642
|
+
lastLogAt = Date.now();
|
|
1643
|
+
}
|
|
1644
|
+
var app = getOpenclawApp();
|
|
1645
|
+
var targetMatches = !pending.agentId || state.currentAgentId === pending.agentId;
|
|
1646
|
+
if (app && app.client && targetMatches && state.buttonFound && !state.buttonDisabled) {
|
|
1647
|
+
logNewSession("ready", state);
|
|
1648
|
+
resolve(app);
|
|
1649
|
+
return;
|
|
1650
|
+
}
|
|
1651
|
+
if (Date.now() - startedAt > timeoutMs) {
|
|
1652
|
+
logNewSession("ready_timeout", state);
|
|
1653
|
+
resolve(null);
|
|
1654
|
+
return;
|
|
1655
|
+
}
|
|
1656
|
+
setTimeout(poll, 250);
|
|
1657
|
+
})();
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1587
1660
|
|
|
1588
1661
|
// 轮询等待某条件就绪,返回 predicate 的真值结果(超时返回 null)
|
|
1589
1662
|
function waitForReady(pred, timeoutMs, intervalMs) {
|
|
@@ -1603,6 +1676,7 @@ btn.addEventListener("click", function () {
|
|
|
1603
1676
|
// 成功 resolve(newKey),失败 reject(Error)。供手动按钮与批量创建复用。
|
|
1604
1677
|
function createNamedSessionAsync(label) {
|
|
1605
1678
|
return new Promise(function (resolve, reject) {
|
|
1679
|
+
logNewSession("create_start", { label: label });
|
|
1606
1680
|
var app = getOpenclawApp();
|
|
1607
1681
|
if (!app) { reject(new Error("未找到 openclaw 应用")); return; }
|
|
1608
1682
|
if (!app.client) { reject(new Error("网关未连接")); return; }
|
|
@@ -1614,14 +1688,17 @@ btn.addEventListener("click", function () {
|
|
|
1614
1688
|
var prevKey = app.sessionKey;
|
|
1615
1689
|
// 由原生按钮负责创建 + 切换
|
|
1616
1690
|
nativeBtn.click();
|
|
1691
|
+
logNewSession("native_button_clicked", { label: label, previousSessionKey: prevKey });
|
|
1617
1692
|
|
|
1618
1693
|
waitForSessionChange(app, prevKey, 8000).then(function (newKey) {
|
|
1619
1694
|
if (!newKey) { reject(new Error("新会话创建超时")); return; }
|
|
1695
|
+
logNewSession("session_key_changed", { label: label, sessionKey: newKey });
|
|
1620
1696
|
var params = { key: newKey, label: label };
|
|
1621
1697
|
var agentId = deriveAgentId(newKey);
|
|
1622
1698
|
if (agentId) params.agentId = agentId;
|
|
1623
1699
|
app.client.request("sessions.patch", params).then(function () {
|
|
1624
1700
|
console.log("[myclaw-newsession] 创建并命名成功:", newKey, label);
|
|
1701
|
+
logNewSession("create_success", { label: label, sessionKey: newKey });
|
|
1625
1702
|
resolve(newKey);
|
|
1626
1703
|
}).catch(reject);
|
|
1627
1704
|
});
|
|
@@ -1647,7 +1724,8 @@ btn.addEventListener("click", function () {
|
|
|
1647
1724
|
return waitForReady(function () {
|
|
1648
1725
|
var b = findNativeNewSessionBtn();
|
|
1649
1726
|
return (b && !b.disabled) ? b : null;
|
|
1650
|
-
},
|
|
1727
|
+
}, 15000, 150).then(function (btn) {
|
|
1728
|
+
if (!btn) throw new Error("等待原生新会话按钮可用超时:" + name);
|
|
1651
1729
|
return createNamedSessionAsync(name);
|
|
1652
1730
|
});
|
|
1653
1731
|
});
|
|
@@ -1655,8 +1733,9 @@ btn.addEventListener("click", function () {
|
|
|
1655
1733
|
}
|
|
1656
1734
|
|
|
1657
1735
|
// 页面加载后调用一次:若存在"待创建会话"意图,且当前正处于目标项目,就自动依次建好。
|
|
1658
|
-
//
|
|
1736
|
+
// 等待超时时保留标记供刷新重试;确认可以开建后才清除,避免重复创建。
|
|
1659
1737
|
function processPendingSessions() {
|
|
1738
|
+
if (pendingSessionsProcessing) return;
|
|
1660
1739
|
var raw = null;
|
|
1661
1740
|
try { raw = localStorage.getItem(PENDING_SESSIONS_KEY); } catch (e) { return; }
|
|
1662
1741
|
if (!raw) return;
|
|
@@ -1664,30 +1743,33 @@ btn.addEventListener("click", function () {
|
|
|
1664
1743
|
var pending = null;
|
|
1665
1744
|
try { pending = JSON.parse(raw); } catch (e) {}
|
|
1666
1745
|
if (!pending || !Array.isArray(pending.names) || !pending.names.length) {
|
|
1746
|
+
logNewSession("pending_invalid", { raw: raw });
|
|
1667
1747
|
try { localStorage.removeItem(PENDING_SESSIONS_KEY); } catch (e) {}
|
|
1668
1748
|
return;
|
|
1669
1749
|
}
|
|
1750
|
+
pendingSessionsProcessing = true;
|
|
1751
|
+
logNewSession("pending_loaded", pending);
|
|
1670
1752
|
|
|
1671
1753
|
// 等 app + client + sessionKey 就绪,并确认当前确实停在目标项目上、原生按钮可用
|
|
1672
|
-
|
|
1673
|
-
var app = getOpenclawApp();
|
|
1674
|
-
if (!app || !app.client || !app.sessionKey) return null;
|
|
1675
|
-
if (pending.agentId && deriveAgentId(app.sessionKey) !== pending.agentId) return null;
|
|
1676
|
-
var btn = findNativeNewSessionBtn();
|
|
1677
|
-
if (!btn || btn.disabled) return null;
|
|
1678
|
-
return app;
|
|
1679
|
-
}, 20000, 250).then(function (app) {
|
|
1680
|
-
// 就绪判定完成即清标记:成功不重复、失败也不再纠缠
|
|
1681
|
-
try { localStorage.removeItem(PENDING_SESSIONS_KEY); } catch (e) {}
|
|
1754
|
+
waitForPendingSessionReady(pending, AUTO_SESSION_WAIT_MS).then(function (app) {
|
|
1682
1755
|
if (!app) {
|
|
1683
|
-
|
|
1756
|
+
pendingSessionsProcessing = false;
|
|
1757
|
+
console.warn("[myclaw-newsession] 目标项目等待超时;保留标记,刷新后可重试");
|
|
1758
|
+
showToast("自动创建等待超时,刷新页面后会重试", "error");
|
|
1684
1759
|
return;
|
|
1685
1760
|
}
|
|
1761
|
+
// 确认真正可以开始创建后才清标记,避免项目初始化较慢时永久丢失任务。
|
|
1762
|
+
try { localStorage.removeItem(PENDING_SESSIONS_KEY); } catch (e) {}
|
|
1763
|
+
logNewSession("pending_cleared_for_batch", { agentId: pending.agentId, names: pending.names });
|
|
1686
1764
|
showToast("正在为新项目创建 " + pending.names.length + " 个对话…");
|
|
1687
1765
|
createSessionsSequential(pending.names).then(function () {
|
|
1766
|
+
pendingSessionsProcessing = false;
|
|
1767
|
+
logNewSession("batch_success", { agentId: pending.agentId, names: pending.names });
|
|
1688
1768
|
showToast("已创建:" + pending.names.join(" / "), "success");
|
|
1689
1769
|
}).catch(function (err) {
|
|
1770
|
+
pendingSessionsProcessing = false;
|
|
1690
1771
|
console.error("[myclaw-newsession] 批量创建失败:", err);
|
|
1772
|
+
logNewSession("batch_failed", { agentId: pending.agentId, error: err && err.message ? err.message : String(err) });
|
|
1691
1773
|
showToast("批量创建部分失败:" + (err && err.message ? err.message : err), "error");
|
|
1692
1774
|
});
|
|
1693
1775
|
});
|