@aiyiran/myclaw 1.1.196 → 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 +194 -22
- package/package.json +1 -1
package/assets/myclaw-inject.js
CHANGED
|
@@ -1491,6 +1491,18 @@ btn.addEventListener("click", function () {
|
|
|
1491
1491
|
var newest = agents.reduce(function (a, b) {
|
|
1492
1492
|
return (b.lastUpdated || 0) > (a.lastUpdated || 0) ? b : a;
|
|
1493
1493
|
});
|
|
1494
|
+
// 记下"新项目建好后要自动创建的会话",靠 localStorage 把意图带过整页跳转。
|
|
1495
|
+
// 跳转后的新页面加载时,processPendingSessions() 会读取;确认可创建后才清除。
|
|
1496
|
+
try {
|
|
1497
|
+
var pendingSessions = {
|
|
1498
|
+
agentId: newest.id,
|
|
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);
|
|
1505
|
+
} catch (e) {}
|
|
1494
1506
|
window.location.href = window.location.origin + '/chat?session=' + encodeURIComponent('agent:' + newest.id + ':main');
|
|
1495
1507
|
})
|
|
1496
1508
|
.catch(function () { location.reload(); });
|
|
@@ -1574,34 +1586,191 @@ btn.addEventListener("click", function () {
|
|
|
1574
1586
|
});
|
|
1575
1587
|
}
|
|
1576
1588
|
|
|
1577
|
-
//
|
|
1578
|
-
|
|
1589
|
+
// localStorage 键:跨"新项目跳转"传递"待自动创建的会话"意图
|
|
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) {
|
|
1579
1614
|
var app = getOpenclawApp();
|
|
1580
|
-
|
|
1581
|
-
|
|
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
|
+
}
|
|
1660
|
+
|
|
1661
|
+
// 轮询等待某条件就绪,返回 predicate 的真值结果(超时返回 null)
|
|
1662
|
+
function waitForReady(pred, timeoutMs, intervalMs) {
|
|
1663
|
+
return new Promise(function (resolve) {
|
|
1664
|
+
var start = Date.now();
|
|
1665
|
+
(function poll() {
|
|
1666
|
+
var v = null;
|
|
1667
|
+
try { v = pred(); } catch (e) { v = null; }
|
|
1668
|
+
if (v) { resolve(v); return; }
|
|
1669
|
+
if (Date.now() - start > timeoutMs) { resolve(null); return; }
|
|
1670
|
+
setTimeout(poll, intervalMs || 150);
|
|
1671
|
+
})();
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1582
1674
|
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1675
|
+
// 创建一个带中文名的新会话(Promise 版):点原生按钮 → 等切换 → sessions.patch 改名。
|
|
1676
|
+
// 成功 resolve(newKey),失败 reject(Error)。供手动按钮与批量创建复用。
|
|
1677
|
+
function createNamedSessionAsync(label) {
|
|
1678
|
+
return new Promise(function (resolve, reject) {
|
|
1679
|
+
logNewSession("create_start", { label: label });
|
|
1680
|
+
var app = getOpenclawApp();
|
|
1681
|
+
if (!app) { reject(new Error("未找到 openclaw 应用")); return; }
|
|
1682
|
+
if (!app.client) { reject(new Error("网关未连接")); return; }
|
|
1683
|
+
|
|
1684
|
+
var nativeBtn = findNativeNewSessionBtn();
|
|
1685
|
+
if (!nativeBtn) { reject(new Error("未找到原生新会话按钮")); return; }
|
|
1686
|
+
if (nativeBtn.disabled) { reject(new Error("当前无法新建(有进行中的任务?)")); return; }
|
|
1687
|
+
|
|
1688
|
+
var prevKey = app.sessionKey;
|
|
1689
|
+
// 由原生按钮负责创建 + 切换
|
|
1690
|
+
nativeBtn.click();
|
|
1691
|
+
logNewSession("native_button_clicked", { label: label, previousSessionKey: prevKey });
|
|
1692
|
+
|
|
1693
|
+
waitForSessionChange(app, prevKey, 8000).then(function (newKey) {
|
|
1694
|
+
if (!newKey) { reject(new Error("新会话创建超时")); return; }
|
|
1695
|
+
logNewSession("session_key_changed", { label: label, sessionKey: newKey });
|
|
1696
|
+
var params = { key: newKey, label: label };
|
|
1697
|
+
var agentId = deriveAgentId(newKey);
|
|
1698
|
+
if (agentId) params.agentId = agentId;
|
|
1699
|
+
app.client.request("sessions.patch", params).then(function () {
|
|
1700
|
+
console.log("[myclaw-newsession] 创建并命名成功:", newKey, label);
|
|
1701
|
+
logNewSession("create_success", { label: label, sessionKey: newKey });
|
|
1702
|
+
resolve(newKey);
|
|
1703
|
+
}).catch(reject);
|
|
1704
|
+
});
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1586
1707
|
|
|
1587
|
-
|
|
1708
|
+
// 手动"💬 新会话"按钮走这里(带 toast 反馈)
|
|
1709
|
+
function createNamedSession(label) {
|
|
1588
1710
|
showToast("正在创建:" + label);
|
|
1711
|
+
createNamedSessionAsync(label).then(function () {
|
|
1712
|
+
showToast("已创建:" + label, "success");
|
|
1713
|
+
}).catch(function (err) {
|
|
1714
|
+
showToast(err && err.message ? err.message : String(err), "error");
|
|
1715
|
+
console.error("[myclaw-newsession] 创建失败:", err);
|
|
1716
|
+
});
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
// 顺序创建多个会话(一个建完再建下一个,避免共用原生按钮/ sessionKey 时的竞态)
|
|
1720
|
+
function createSessionsSequential(names) {
|
|
1721
|
+
return names.reduce(function (chain, name) {
|
|
1722
|
+
return chain.then(function () {
|
|
1723
|
+
// 每个之间等原生按钮重新可用,稳一点
|
|
1724
|
+
return waitForReady(function () {
|
|
1725
|
+
var b = findNativeNewSessionBtn();
|
|
1726
|
+
return (b && !b.disabled) ? b : null;
|
|
1727
|
+
}, 15000, 150).then(function (btn) {
|
|
1728
|
+
if (!btn) throw new Error("等待原生新会话按钮可用超时:" + name);
|
|
1729
|
+
return createNamedSessionAsync(name);
|
|
1730
|
+
});
|
|
1731
|
+
});
|
|
1732
|
+
}, Promise.resolve());
|
|
1733
|
+
}
|
|
1589
1734
|
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1735
|
+
// 页面加载后调用一次:若存在"待创建会话"意图,且当前正处于目标项目,就自动依次建好。
|
|
1736
|
+
// 等待超时时保留标记供刷新重试;确认可以开建后才清除,避免重复创建。
|
|
1737
|
+
function processPendingSessions() {
|
|
1738
|
+
if (pendingSessionsProcessing) return;
|
|
1739
|
+
var raw = null;
|
|
1740
|
+
try { raw = localStorage.getItem(PENDING_SESSIONS_KEY); } catch (e) { return; }
|
|
1741
|
+
if (!raw) return;
|
|
1742
|
+
|
|
1743
|
+
var pending = null;
|
|
1744
|
+
try { pending = JSON.parse(raw); } catch (e) {}
|
|
1745
|
+
if (!pending || !Array.isArray(pending.names) || !pending.names.length) {
|
|
1746
|
+
logNewSession("pending_invalid", { raw: raw });
|
|
1747
|
+
try { localStorage.removeItem(PENDING_SESSIONS_KEY); } catch (e) {}
|
|
1748
|
+
return;
|
|
1749
|
+
}
|
|
1750
|
+
pendingSessionsProcessing = true;
|
|
1751
|
+
logNewSession("pending_loaded", pending);
|
|
1752
|
+
|
|
1753
|
+
// 等 app + client + sessionKey 就绪,并确认当前确实停在目标项目上、原生按钮可用
|
|
1754
|
+
waitForPendingSessionReady(pending, AUTO_SESSION_WAIT_MS).then(function (app) {
|
|
1755
|
+
if (!app) {
|
|
1756
|
+
pendingSessionsProcessing = false;
|
|
1757
|
+
console.warn("[myclaw-newsession] 目标项目等待超时;保留标记,刷新后可重试");
|
|
1758
|
+
showToast("自动创建等待超时,刷新页面后会重试", "error");
|
|
1759
|
+
return;
|
|
1760
|
+
}
|
|
1761
|
+
// 确认真正可以开始创建后才清标记,避免项目初始化较慢时永久丢失任务。
|
|
1762
|
+
try { localStorage.removeItem(PENDING_SESSIONS_KEY); } catch (e) {}
|
|
1763
|
+
logNewSession("pending_cleared_for_batch", { agentId: pending.agentId, names: pending.names });
|
|
1764
|
+
showToast("正在为新项目创建 " + pending.names.length + " 个对话…");
|
|
1765
|
+
createSessionsSequential(pending.names).then(function () {
|
|
1766
|
+
pendingSessionsProcessing = false;
|
|
1767
|
+
logNewSession("batch_success", { agentId: pending.agentId, names: pending.names });
|
|
1768
|
+
showToast("已创建:" + pending.names.join(" / "), "success");
|
|
1602
1769
|
}).catch(function (err) {
|
|
1603
|
-
|
|
1604
|
-
console.error("[myclaw-newsession]
|
|
1770
|
+
pendingSessionsProcessing = false;
|
|
1771
|
+
console.error("[myclaw-newsession] 批量创建失败:", err);
|
|
1772
|
+
logNewSession("batch_failed", { agentId: pending.agentId, error: err && err.message ? err.message : String(err) });
|
|
1773
|
+
showToast("批量创建部分失败:" + (err && err.message ? err.message : err), "error");
|
|
1605
1774
|
});
|
|
1606
1775
|
});
|
|
1607
1776
|
}
|
|
@@ -2398,6 +2567,9 @@ btn.addEventListener("click", function () {
|
|
|
2398
2567
|
createNewAgentButton();
|
|
2399
2568
|
injectStyles();
|
|
2400
2569
|
|
|
2570
|
+
// 新项目跳转过来后:若有"待创建会话"意图,自动依次建好 策划 / 美术 / 程序
|
|
2571
|
+
processPendingSessions();
|
|
2572
|
+
|
|
2401
2573
|
// 初始化 VoiceInput SDK
|
|
2402
2574
|
initVoice();
|
|
2403
2575
|
|