@aiyiran/myclaw 1.1.198 → 1.1.199

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.
@@ -1492,7 +1492,7 @@ 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,
@@ -1574,18 +1574,6 @@ btn.addEventListener("click", function () {
1574
1574
  return null;
1575
1575
  }
1576
1576
 
1577
- // 轮询等待 sessionKey 变化(原生按钮创建+切换后,key 会变成新会话)
1578
- function waitForSessionChange(app, prevKey, timeoutMs) {
1579
- return new Promise(function (resolve) {
1580
- var start = Date.now();
1581
- (function poll() {
1582
- if (app.sessionKey && app.sessionKey !== prevKey) { resolve(app.sessionKey); return; }
1583
- if (Date.now() - start > timeoutMs) { resolve(null); return; }
1584
- setTimeout(poll, 80);
1585
- })();
1586
- });
1587
- }
1588
-
1589
1577
  // localStorage 键:跨"新项目跳转"传递"待自动创建的会话"意图
1590
1578
  var PENDING_SESSIONS_KEY = "myclaw_pending_sessions";
1591
1579
  var NEWSESSION_LOG_KEY = "myclaw_newsession_logs";
@@ -1627,7 +1615,7 @@ btn.addEventListener("click", function () {
1627
1615
  };
1628
1616
  }
1629
1617
 
1630
- // 等待项目页面、网关和原生新会话按钮真正可用。状态变化及每 5 秒心跳都会留日志。
1618
+ // 等待项目页面和网关真正可用。按钮状态仍记入日志,但自动创建不再依赖 UI 按钮。
1631
1619
  function waitForPendingSessionReady(pending, timeoutMs) {
1632
1620
  return new Promise(function (resolve) {
1633
1621
  var startedAt = Date.now();
@@ -1643,7 +1631,7 @@ btn.addEventListener("click", function () {
1643
1631
  }
1644
1632
  var app = getOpenclawApp();
1645
1633
  var targetMatches = !pending.agentId || state.currentAgentId === pending.agentId;
1646
- if (app && app.client && targetMatches && state.buttonFound && !state.buttonDisabled) {
1634
+ if (app && app.client && app.connected && targetMatches) {
1647
1635
  logNewSession("ready", state);
1648
1636
  resolve(app);
1649
1637
  return;
@@ -1658,49 +1646,33 @@ btn.addEventListener("click", function () {
1658
1646
  });
1659
1647
  }
1660
1648
 
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
- }
1674
-
1675
- // 创建一个带中文名的新会话(Promise 版):点原生按钮 → 等切换 → sessions.patch 改名。
1676
- // 成功 resolve(newKey),失败 reject(Error)。供手动按钮与批量创建复用。
1677
- function createNamedSessionAsync(label) {
1649
+ // 自动批量创建使用 Gateway 官方 sessions.create RPC,不再模拟点击 UI。
1650
+ // 新版 Control UI 点击按钮后不一定立刻更新 app.sessionKey,以页面切换判断成功会误报超时。
1651
+ function createNamedSessionDirectAsync(label, agentId) {
1678
1652
  return new Promise(function (resolve, reject) {
1679
- logNewSession("create_start", { label: label });
1680
1653
  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);
1654
+ if (!app || !app.client || !app.connected) {
1655
+ reject(new Error("网关未连接"));
1656
+ return;
1657
+ }
1658
+ var params = { label: label };
1659
+ if (agentId) params.agentId = agentId;
1660
+ logNewSession("rpc_create_request", params);
1661
+ app.client.request("sessions.create", params).then(function (result) {
1662
+ var newKey = result && typeof result.key === "string" ? result.key.trim() : "";
1663
+ if (!newKey) {
1664
+ logNewSession("rpc_create_invalid_response", { label: label, result: result || null });
1665
+ reject(new Error("sessions.create 未返回会话 key"));
1666
+ return;
1667
+ }
1668
+ logNewSession("rpc_create_success", { label: label, sessionKey: newKey });
1669
+ resolve(newKey);
1670
+ }).catch(function (err) {
1671
+ logNewSession("rpc_create_failed", {
1672
+ label: label,
1673
+ error: err && err.message ? err.message : String(err)
1674
+ });
1675
+ reject(err);
1704
1676
  });
1705
1677
  });
1706
1678
  }
@@ -1708,32 +1680,33 @@ btn.addEventListener("click", function () {
1708
1680
  // 手动"💬 新会话"按钮走这里(带 toast 反馈)
1709
1681
  function createNamedSession(label) {
1710
1682
  showToast("正在创建:" + label);
1711
- createNamedSessionAsync(label).then(function () {
1683
+ var app = getOpenclawApp();
1684
+ var agentId = deriveAgentId(app && app.sessionKey);
1685
+ createNamedSessionDirectAsync(label, agentId).then(function (newKey) {
1712
1686
  showToast("已创建:" + label, "success");
1687
+ setTimeout(function () {
1688
+ window.location.href = window.location.origin + "/chat?session=" + encodeURIComponent(newKey);
1689
+ }, 300);
1713
1690
  }).catch(function (err) {
1714
1691
  showToast(err && err.message ? err.message : String(err), "error");
1715
1692
  console.error("[myclaw-newsession] 创建失败:", err);
1716
1693
  });
1717
1694
  }
1718
1695
 
1719
- // 顺序创建多个会话(一个建完再建下一个,避免共用原生按钮/ sessionKey 时的竞态)
1720
- function createSessionsSequential(names) {
1696
+ // 顺序创建多个会话;每个 RPC 完成后再创建下一个。
1697
+ function createSessionsSequential(names, agentId, onCreated) {
1721
1698
  return names.reduce(function (chain, name) {
1722
1699
  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);
1700
+ return createNamedSessionDirectAsync(name, agentId).then(function (newKey) {
1701
+ if (onCreated) onCreated(name, newKey);
1702
+ return newKey;
1730
1703
  });
1731
1704
  });
1732
1705
  }, Promise.resolve());
1733
1706
  }
1734
1707
 
1735
1708
  // 页面加载后调用一次:若存在"待创建会话"意图,且当前正处于目标项目,就自动依次建好。
1736
- // 等待超时时保留标记供刷新重试;确认可以开建后才清除,避免重复创建。
1709
+ // 等待或批量创建失败时保留剩余任务;全部完成后才清除标记。
1737
1710
  function processPendingSessions() {
1738
1711
  if (pendingSessionsProcessing) return;
1739
1712
  var raw = null;
@@ -1750,7 +1723,7 @@ btn.addEventListener("click", function () {
1750
1723
  pendingSessionsProcessing = true;
1751
1724
  logNewSession("pending_loaded", pending);
1752
1725
 
1753
- // 等 app + client + sessionKey 就绪,并确认当前确实停在目标项目上、原生按钮可用
1726
+ // 等 app + client + sessionKey 就绪,并确认当前确实停在目标项目上。
1754
1727
  waitForPendingSessionReady(pending, AUTO_SESSION_WAIT_MS).then(function (app) {
1755
1728
  if (!app) {
1756
1729
  pendingSessionsProcessing = false;
@@ -1758,19 +1731,32 @@ btn.addEventListener("click", function () {
1758
1731
  showToast("自动创建等待超时,刷新页面后会重试", "error");
1759
1732
  return;
1760
1733
  }
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 () {
1734
+ var originalNames = pending.names.slice();
1735
+ logNewSession("batch_start", { agentId: pending.agentId, names: originalNames });
1736
+ showToast("正在为新项目创建 " + originalNames.length + " 个对话…");
1737
+ createSessionsSequential(originalNames, pending.agentId, function (createdName, newKey) {
1738
+ // 每成功一个就保存剩余任务;中途失败或刷新时只重试尚未完成的名称。
1739
+ pending.names = pending.names.filter(function (name) { return name !== createdName; });
1740
+ try { localStorage.setItem(PENDING_SESSIONS_KEY, JSON.stringify(pending)); } catch (e) {}
1741
+ logNewSession("pending_progress_saved", {
1742
+ createdName: createdName,
1743
+ sessionKey: newKey,
1744
+ remainingNames: pending.names.slice()
1745
+ });
1746
+ }).then(function () {
1766
1747
  pendingSessionsProcessing = false;
1767
- logNewSession("batch_success", { agentId: pending.agentId, names: pending.names });
1768
- showToast("已创建:" + pending.names.join(" / "), "success");
1748
+ try { localStorage.removeItem(PENDING_SESSIONS_KEY); } catch (e) {}
1749
+ logNewSession("batch_success", { agentId: pending.agentId, names: originalNames });
1750
+ showToast("已创建:" + originalNames.join(" / "), "success");
1769
1751
  }).catch(function (err) {
1770
1752
  pendingSessionsProcessing = false;
1771
1753
  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");
1754
+ logNewSession("batch_failed", {
1755
+ agentId: pending.agentId,
1756
+ remainingNames: pending.names.slice(),
1757
+ error: err && err.message ? err.message : String(err)
1758
+ });
1759
+ showToast("批量创建部分失败,刷新后会重试:" + (err && err.message ? err.message : err), "error");
1774
1760
  });
1775
1761
  });
1776
1762
  }
package/index.js CHANGED
@@ -1596,7 +1596,7 @@ const MACHINE_CONFIG = [
1596
1596
  { name: '伊伊', claw: 'claw6', desc: '伊伊 的机器' },
1597
1597
  { name: '梓浩', claw: 'claw7', desc: '梓浩 的机器' },
1598
1598
  { name: '刘泓毅', claw: 'claw8', desc: '刘泓毅 的机器' },
1599
- { name: 'Mia', claw: 'claw9', desc: 'Mia 的机器' },
1599
+ { name: '袁驿博', claw: 'claw9', desc: '袁驿博 的机器' },
1600
1600
  { name: '雨熙', claw: 'claw10', desc: '雨熙 的机器' },
1601
1601
  { name: '绍博', claw: 'claw11', desc: '绍博 的机器' },
1602
1602
  { name: '黄玺澄', claw: 'claw12', desc: '玺澄 的机器' },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiyiran/myclaw",
3
- "version": "1.1.198",
3
+ "version": "1.1.199",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {