@celljs/ta-cloud-handoff 4.37.0 → 4.38.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.en.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A pi package that comes with Ta (on by default, off in Settings → Extensions): **when your computer has to go, a conversation goes on in your cloud workspace, and comes back when you do.** Work runs on your computer first (it needs what is there); the cloud is a standby. Design: [docs/design/ta-cloud-handoff.md](../../docs/design/ta-cloud-handoff.md).
4
4
 
5
- It needs a cloud workspace in Settings → Remote hosts (`ta://<name>`, the address of a gateway or any Ta service and a token), and the conversation's folder in a git repository of this computer with a commit.
5
+ It needs, in Settings → Remote hosts, “Go on here while this computer is away” turned on for a machine reached by ssh (Ta installs itself there and keeps running, a cloud workspace `ta://<name>`; with your company's Ta gateway, its address and a token do too), and the conversation's folder in a git repository of this computer with a commit.
6
6
 
7
7
  - **Go on in the cloud**: the menu of the conversation in the lists (an entry for each cloud workspace), or say so in it (the tool `cloud_handoff`, which goes once the turn ends). The conversation stops here, then the whole work of the repository goes: its history (the branch's whole history the first time, only new commits after), changes not committed, new files, files deleted, byte for byte, done only when both sides make the same git tree. What git ignores (dependencies, builds, `.env`) stays; the cloud installs again as the project does. There it is at the same path when it can be (the paths of the conversation still hold), else under `~/ta-handoff/<path>`. Then the session file goes, with a note at its end (the model reads it, the page draws a line). A turn running here goes on there; otherwise it is just there, to carry on with there.
8
8
  - **While it is away**: it is tagged Cloud in the lists and takes no message here. What only the computer can do (its apps, desktop, devices, network, keychain) the cloud notes with the tool `later_on_computer`, and goes on with the rest.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Ta 自带的 pi 包(默认开启,可在 设置 → 扩展 关闭):**电脑要走开时,把对话交给云端工作区接着做,回来再接回本机。** 任务默认在本机跑(依赖本机环境),云端只是热备。设计记录见 [docs/design/ta-cloud-handoff.md](../../docs/design/ta-cloud-handoff.md)。
4
4
 
5
- 前提:在 设置 → 远程主机 里加了云端工作区(`ta://<名字>`,网关或任何 Ta 服务的地址 + 令牌),对话的目录在本机的一个有提交的 git 仓库里。
5
+ 前提:在 设置 → 远程主机 里给一台能 ssh 的机器打开「电脑走开时在这里接着做」(Ta 会在那边装好并常驻,成为云端工作区 `ta://<名字>`;有公司的 Ta 网关时也可以填网关地址 + 令牌),对话的目录在本机的一个有提交的 git 仓库里。
6
6
 
7
7
  - **交给云端接着做**:对话列表里对话的菜单(有几个云端工作区就有几项),或在对话里说"交给云端接着做"(工具 `cloud_handoff`,这一轮结束后再走)。对话先在本机停下,再把仓库的整个工作状态带过去:已提交的历史(第一次带当前分支的全部历史,之后只带新增的提交)、没提交的改动、新文件、删掉的文件,一个字节不差,两边算出同一个 git tree 才算成功;git 忽略的文件(依赖、构建产物、`.env`)不带,云端按项目自己的方式重装。云端能建同一路径就用同一路径(对话里的路径都还对),否则放在 `~/ta-handoff/<原路径>`。对话文件随后带过去,末尾加一条说明(模型读得到,页面上是一行"换到云端接着做");本机这一轮在跑就在云端接着跑,没在跑就只是放过去,在云端那边接着聊。
8
8
  - **在云端时**:列表里这个对话带"云端"标签,本机不再接受新消息(会提示去云端看或先接回)。云端遇到只有本机能做的事(本机应用、桌面、设备、内网、钥匙串),用工具 `later_on_computer` 记下,接着做其余的。
@@ -195,6 +195,9 @@ var WHY = {
195
195
  sleep: "by itself as the user's computer was about to sleep (its lid closed, most likely)",
196
196
  battery: "by itself as the battery of the user's computer ran low"
197
197
  };
198
+ function short(model) {
199
+ return model ? model.slice(model.indexOf("/") + 1) : "";
200
+ }
198
201
  var HandoffTexts;
199
202
  ((HandoffTexts) => {
200
203
  function toCloud(options) {
@@ -204,6 +207,7 @@ var HandoffTexts;
204
207
  "- What git ignores (installed dependencies, builds, .env files) did not come along: install or build again, as the project does, when you need them.",
205
208
  options.cut ? "- The last turn was stopped as it moved: a command that ran then was cut off. Check what it did, and run it again if needed." : "",
206
209
  `- Leave what only the user's computer can do (its apps, desktop, devices, local network, keychain, signed-in tools): note each with the tool ${LATER_TOOL} (without it, list them at the end of your reply under "Back on the computer"), and go on with the rest.`,
210
+ options.model ? `- The model of this conversation, ${options.model}, has no key in the cloud: another model the cloud can use goes on with it. Work as before; say so if the work needs that model.` : "",
207
211
  "- Push, publish or deploy only if the user asked for it in this conversation.",
208
212
  options.goOn ? "- Go on with the work where it stopped." : ""
209
213
  ].filter(Boolean).join(`
@@ -266,11 +270,26 @@ var HandoffTexts;
266
270
  "zh-TW": `${later} 件事留給本機做`,
267
271
  "en-US": `${later} step(s) left for this computer`,
268
272
  "ja-JP": `${later} 件をこのパソコンで`
273
+ }) : "",
274
+ note.model?.to ? TaLanguageUtil.say({
275
+ "zh-CN": `云端用不了 ${short(note.model.from)},换成 ${short(note.model.to)}`,
276
+ "zh-TW": `雲端用不了 ${short(note.model.from)},換成 ${short(note.model.to)}`,
277
+ "en-US": `no key of ${short(note.model.from)} there: going on with ${short(note.model.to)}`,
278
+ "ja-JP": `クラウドでは ${short(note.model.from)} が使えないので ${short(note.model.to)} で続ける`
269
279
  }) : ""
270
280
  ].filter(Boolean);
271
281
  return [head, ...more].join(" · ");
272
282
  }
273
283
  HandoffTexts.goOn = goOn;
284
+ function noModel(cloud) {
285
+ return TaLanguageUtil.say({
286
+ "zh-CN": `云端 ${cloud} 没有能用的模型:在保险箱里打开模型密钥的“可在我的云端工作区用”,或在那台机器上登录`,
287
+ "zh-TW": `雲端 ${cloud} 沒有能用的模型:在保險箱裡打開模型金鑰的「可在我的雲端工作區用」,或在那台機器上登入`,
288
+ "en-US": `The cloud workspace ${cloud} has no model it can use: let a key of a model go there in the vault, or sign in there`,
289
+ "ja-JP": `クラウド ${cloud} で使えるモデルがありません。金庫でモデルのキーを「クラウドのワークスペースでも使う」にするか、そのマシンでログインしてください`
290
+ });
291
+ }
292
+ HandoffTexts.noModel = noModel;
274
293
  function away(cloud) {
275
294
  return TaLanguageUtil.say({
276
295
  "zh-CN": `这个对话正在云端 ${cloud} 接着做:去那边看,或在对话列表的菜单里点“接回本机”再继续。`,
package/host/index.mjs CHANGED
@@ -172,9 +172,9 @@ class HandoffAuto {
172
172
 
173
173
  // web-src/handoff-service.ts
174
174
  import { randomUUID as randomUUID2 } from "node:crypto";
175
- import { appendFile as appendFile2, readFile, rm, writeFile as writeFile2 } from "node:fs/promises";
175
+ import { appendFile as appendFile2, readFile as readFile2, rm, writeFile as writeFile2 } from "node:fs/promises";
176
176
  import { tmpdir } from "node:os";
177
- import { basename, dirname, isAbsolute as isAbsolute2, join, posix, relative, sep } from "node:path";
177
+ import { basename, dirname, isAbsolute as isAbsolute2, join as join2, posix, relative, sep } from "node:path";
178
178
 
179
179
  // ../ta-protocol/esm/common/message-protocol.js
180
180
  var TaMessageContext;
@@ -640,6 +640,114 @@ var HandoffSessions;
640
640
  HandoffSessions.note = note;
641
641
  })(HandoffSessions ||= {});
642
642
 
643
+ // web-src/handoff-setup.ts
644
+ import { readFile } from "node:fs/promises";
645
+ import { join } from "node:path";
646
+ var LOCKS = [
647
+ { files: ["bun.lock", "bun.lockb"], step: { tool: "bun", command: "bun install --frozen-lockfile" } },
648
+ { files: ["pnpm-lock.yaml"], step: { tool: "pnpm", command: "pnpm install --frozen-lockfile" } },
649
+ { files: ["yarn.lock"], step: { tool: "yarn", command: "yarn install --frozen-lockfile" } },
650
+ { files: ["package-lock.json"], step: { tool: "npm", command: "npm ci" } },
651
+ { files: ["package.json"], step: { tool: "npm", command: "npm install" } }
652
+ ];
653
+ var OTHERS = [
654
+ { file: "uv.lock", step: { tool: "uv", command: "uv sync" } },
655
+ { file: "poetry.lock", step: { tool: "poetry", command: "poetry install" } },
656
+ { file: "requirements.txt", step: { tool: "python3", command: "python3 -m venv .venv && .venv/bin/pip install -r requirements.txt" } },
657
+ { file: "Cargo.lock", step: { tool: "cargo", command: "cargo fetch" } },
658
+ { file: "go.sum", step: { tool: "go", command: "go mod download" } },
659
+ { file: "Gemfile.lock", step: { tool: "bundle", command: "bundle install" } }
660
+ ];
661
+ var DEVCONTAINERS = [".devcontainer/devcontainer.json", ".devcontainer.json"];
662
+ var DEVCONTAINER_COMMANDS = ["onCreateCommand", "updateContentCommand", "postCreateCommand"];
663
+ function q(value) {
664
+ return `'${value.replace(/'/g, `'\\''`)}'`;
665
+ }
666
+ var HandoffSetup;
667
+ ((HandoffSetup) => {
668
+ function steps(files, devcontainer) {
669
+ const own = devcontainerSteps(devcontainer);
670
+ if (own.length) {
671
+ return own;
672
+ }
673
+ const steps2 = [];
674
+ const lock = LOCKS.find((one) => one.files.some((file) => files.has(file)));
675
+ if (lock) {
676
+ steps2.push(lock.step);
677
+ }
678
+ for (const other of OTHERS) {
679
+ if (files.has(other.file)) {
680
+ steps2.push(other.step);
681
+ }
682
+ }
683
+ return steps2;
684
+ }
685
+ HandoffSetup.steps = steps;
686
+ function devcontainerSteps(config) {
687
+ if (!config || typeof config !== "object") {
688
+ return [];
689
+ }
690
+ const steps2 = [];
691
+ const add = (value) => {
692
+ if (typeof value === "string" && value.trim()) {
693
+ steps2.push({ tool: "sh", command: value.trim() });
694
+ } else if (Array.isArray(value) && value.length && value.every((part) => typeof part === "string")) {
695
+ steps2.push({ tool: value[0], command: value.map(q).join(" ") });
696
+ } else if (value && typeof value === "object" && !Array.isArray(value)) {
697
+ Object.values(value).forEach(add);
698
+ }
699
+ };
700
+ for (const key of DEVCONTAINER_COMMANDS) {
701
+ add(config[key]);
702
+ }
703
+ return steps2;
704
+ }
705
+ HandoffSetup.devcontainerSteps = devcontainerSteps;
706
+ function jsonc(text) {
707
+ const stripped = text.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, string) => string ?? "").replace(/,(\s*[}\]])/g, "$1");
708
+ try {
709
+ return JSON.parse(stripped);
710
+ } catch {
711
+ return;
712
+ }
713
+ }
714
+ HandoffSetup.jsonc = jsonc;
715
+ function script(steps2, inputs) {
716
+ if (!steps2.length) {
717
+ return "echo 'ta-prepare: nothing to install'";
718
+ }
719
+ const plan = steps2.map((step) => step.command).join(`
720
+ `);
721
+ return [
722
+ "fail=0",
723
+ 'stamp="$(git rev-parse --git-dir 2>/dev/null || echo .git)/ta-handoff/prepared"',
724
+ `sum=$( { printf '%s\\n' ${q(plan)}; cat ${inputs.map(q).join(" ")} 2>/dev/null; } | git hash-object --stdin)`,
725
+ 'if [ -f "$stamp" ] && [ "$(cat "$stamp")" = "$sum" ]; then echo "ta-prepare: up to date"; exit 0; fi',
726
+ ...steps2.map((step) => `if command -v ${q(step.tool)} >/dev/null 2>&1; then echo ${q(`ta-prepare: ${step.command}`)}; sh -c ${q(step.command)} || fail=1; else echo ${q(`ta-prepare: skipped, there is no ${step.tool}: ${step.command}`)}; fi`),
727
+ 'if [ "$fail" = 0 ]; then mkdir -p "$(dirname "$stamp")" && printf %s "$sum" > "$stamp"; fi',
728
+ 'exit "$fail"'
729
+ ].join(`
730
+ `);
731
+ }
732
+ HandoffSetup.script = script;
733
+ async function of(root) {
734
+ const names = [...new Set([...LOCKS.flatMap((one) => one.files), ...OTHERS.map((one) => one.file), ...DEVCONTAINERS])];
735
+ const found = new Map;
736
+ await Promise.all(names.map(async (name) => {
737
+ const text = await readFile(join(root, name), "utf8").catch(() => {
738
+ return;
739
+ });
740
+ if (text !== undefined) {
741
+ found.set(name, text);
742
+ }
743
+ }));
744
+ const devcontainer = DEVCONTAINERS.map((name) => found.get(name)).find((text) => text !== undefined);
745
+ const files = new Set(found.keys());
746
+ return script(steps(files, devcontainer === undefined ? undefined : jsonc(devcontainer)), [...files].sort());
747
+ }
748
+ HandoffSetup.of = of;
749
+ })(HandoffSetup ||= {});
750
+
643
751
  // web-src/handoff-sides.ts
644
752
  import { spawn } from "node:child_process";
645
753
  import { appendFile, open, writeFile } from "node:fs/promises";
@@ -710,13 +818,13 @@ class CloudSide {
710
818
  return result.output;
711
819
  }
712
820
  async read(path, offset, length) {
713
- const q = HandoffScripts.q;
821
+ const q2 = HandoffScripts.q;
714
822
  const piece = `${path}.ta-piece`;
715
- await this.run(`tail -c +${offset + 1} ${q(path)} | head -c ${length} > ${q(piece)}`);
823
+ await this.run(`tail -c +${offset + 1} ${q2(path)} | head -c ${length} > ${q2(piece)}`);
716
824
  try {
717
825
  return await this.cloud.readFile(this.name, piece);
718
826
  } finally {
719
- await this.run(`rm -f ${q(piece)}`).catch(() => {
827
+ await this.run(`rm -f ${q2(piece)}`).catch(() => {
720
828
  return;
721
829
  });
722
830
  }
@@ -784,6 +892,9 @@ var WHY = {
784
892
  sleep: "by itself as the user's computer was about to sleep (its lid closed, most likely)",
785
893
  battery: "by itself as the battery of the user's computer ran low"
786
894
  };
895
+ function short(model) {
896
+ return model ? model.slice(model.indexOf("/") + 1) : "";
897
+ }
787
898
  var HandoffTexts;
788
899
  ((HandoffTexts) => {
789
900
  function toCloud(options) {
@@ -793,6 +904,7 @@ var HandoffTexts;
793
904
  "- What git ignores (installed dependencies, builds, .env files) did not come along: install or build again, as the project does, when you need them.",
794
905
  options.cut ? "- The last turn was stopped as it moved: a command that ran then was cut off. Check what it did, and run it again if needed." : "",
795
906
  `- Leave what only the user's computer can do (its apps, desktop, devices, local network, keychain, signed-in tools): note each with the tool ${LATER_TOOL} (without it, list them at the end of your reply under "Back on the computer"), and go on with the rest.`,
907
+ options.model ? `- The model of this conversation, ${options.model}, has no key in the cloud: another model the cloud can use goes on with it. Work as before; say so if the work needs that model.` : "",
796
908
  "- Push, publish or deploy only if the user asked for it in this conversation.",
797
909
  options.goOn ? "- Go on with the work where it stopped." : ""
798
910
  ].filter(Boolean).join(`
@@ -855,11 +967,26 @@ var HandoffTexts;
855
967
  "zh-TW": `${later} 件事留給本機做`,
856
968
  "en-US": `${later} step(s) left for this computer`,
857
969
  "ja-JP": `${later} 件をこのパソコンで`
970
+ }) : "",
971
+ note.model?.to ? TaLanguageUtil.say({
972
+ "zh-CN": `云端用不了 ${short(note.model.from)},换成 ${short(note.model.to)}`,
973
+ "zh-TW": `雲端用不了 ${short(note.model.from)},換成 ${short(note.model.to)}`,
974
+ "en-US": `no key of ${short(note.model.from)} there: going on with ${short(note.model.to)}`,
975
+ "ja-JP": `クラウドでは ${short(note.model.from)} が使えないので ${short(note.model.to)} で続ける`
858
976
  }) : ""
859
977
  ].filter(Boolean);
860
978
  return [head, ...more].join(" · ");
861
979
  }
862
980
  HandoffTexts.goOn = goOn;
981
+ function noModel(cloud) {
982
+ return TaLanguageUtil.say({
983
+ "zh-CN": `云端 ${cloud} 没有能用的模型:在保险箱里打开模型密钥的“可在我的云端工作区用”,或在那台机器上登录`,
984
+ "zh-TW": `雲端 ${cloud} 沒有能用的模型:在保險箱裡打開模型金鑰的「可在我的雲端工作區用」,或在那台機器上登入`,
985
+ "en-US": `The cloud workspace ${cloud} has no model it can use: let a key of a model go there in the vault, or sign in there`,
986
+ "ja-JP": `クラウド ${cloud} で使えるモデルがありません。金庫でモデルのキーを「クラウドのワークスペースでも使う」にするか、そのマシンでログインしてください`
987
+ });
988
+ }
989
+ HandoffTexts.noModel = noModel;
863
990
  function away(cloud) {
864
991
  return TaLanguageUtil.say({
865
992
  "zh-CN": `这个对话正在云端 ${cloud} 接着做:去那边看,或在对话列表的菜单里点“接回本机”再继续。`,
@@ -888,7 +1015,7 @@ var HandoffTexts;
888
1015
  var MAX_BUNDLE_BYTES = 512 * 1024 * 1024;
889
1016
  var MAX_WARM_BYTES = 2 * 1024 * 1024 * 1024;
890
1017
  var WARM_REF = "refs/ta-handoff/warm";
891
- var { q, ref, results } = HandoffScripts;
1018
+ var { q: q2, ref, results } = HandoffScripts;
892
1019
 
893
1020
  class CloudHandoffs {
894
1021
  host;
@@ -980,12 +1107,13 @@ class CloudHandoffs {
980
1107
  }
981
1108
  const { id, root, cwd } = handoff;
982
1109
  const side = this.side(cloud, workspace.name);
983
- const bundle = join(this.temp, `ta-handoff-${id}.bundle`);
984
- const copy = join(this.temp, `ta-handoff-${id}.jsonl`);
1110
+ const bundle = join2(this.temp, `ta-handoff-${id}.bundle`);
1111
+ const copy = join2(this.temp, `ta-handoff-${id}.jsonl`);
985
1112
  const clone = `${workspace.name}
986
1113
  ${root}`;
987
1114
  let stopped = false;
988
1115
  try {
1116
+ const switched = await this.modelThere(cloud, workspace.name, open2?.model);
989
1117
  await this.warming.get(clone)?.catch(() => {
990
1118
  return;
991
1119
  });
@@ -1006,7 +1134,7 @@ ${root}`;
1006
1134
  throw new Error(`What ${root} would send is ${Math.round(size / 1048576)} MB, more than ${MAX_BUNDLE_BYTES / 1048576} MB at a time.`);
1007
1135
  }
1008
1136
  const arrived = posix.join(there.home, ".ta", "handoff", `${id}.bundle`);
1009
- await side.run(`mkdir -p ${q(posix.dirname(arrived))}`);
1137
+ await side.run(`mkdir -p ${q2(posix.dirname(arrived))}`);
1010
1138
  await HandoffTransfer.copy(this.local, bundle, size, side, arrived);
1011
1139
  const cloudRoot = there.root;
1012
1140
  const cloudCwd = posix.join(cloudRoot, relative(root, cwd).split(sep).join("/"));
@@ -1014,26 +1142,30 @@ ${root}`;
1014
1142
  if (received.tree !== snapshot.tree) {
1015
1143
  throw new Error("The files in the cloud did not come out the same as here, so nothing moved.");
1016
1144
  }
1017
- const lines = HandoffSessions.lines(await readFile(sessionFile, "utf8"));
1145
+ await cloud.prepare?.(workspace.name, cloudRoot, await HandoffSetup.of(root)).catch(() => {
1146
+ return;
1147
+ });
1148
+ const lines = HandoffSessions.lines(await readFile2(sessionFile, "utf8"));
1018
1149
  const cut = open2?.isStreaming === true;
1019
1150
  const details = { to: "cloud", cloud: workspace.name, ...auto ? { why: auto } : {} };
1020
1151
  const going = cut || goOn;
1021
- const note = HandoffSessions.note(lines, HandoffTexts.toCloud({ cloud: workspace.name, root, cloudRoot, cut, goOn: going, why: auto }), details, !going);
1152
+ if (switched) {
1153
+ details.model = { from: switched };
1154
+ }
1155
+ const note = HandoffSessions.note(lines, HandoffTexts.toCloud({ cloud: workspace.name, root, cloudRoot, cut, goOn: going, why: auto, model: switched }), details, !going);
1022
1156
  const body = new TextEncoder().encode(`${HandoffSessions.relocate(lines.join(`
1023
1157
  `), cloudCwd)}
1024
1158
  ${note}
1025
1159
  `);
1026
1160
  await writeFile2(copy, body);
1027
1161
  const cloudSessionFile = posix.join(there.agent, "sessions", HandoffSessions.directory(cloudCwd), basename(sessionFile));
1028
- await side.run(`mkdir -p ${q(posix.dirname(cloudSessionFile))}`);
1162
+ await side.run(`mkdir -p ${q2(posix.dirname(cloudSessionFile))}`);
1029
1163
  await HandoffTransfer.copy(this.local, copy, body.length, side, cloudSessionFile);
1030
1164
  const server = cloud.server(workspace.name);
1031
1165
  const opened = await server.openSession({ cwd: cloudCwd, sessionFile: cloudSessionFile });
1032
- if (open2?.model?.includes("/")) {
1033
- const [provider, ...model] = open2.model.split("/");
1034
- await server.setModel(opened.sessionId, provider, model.join("/")).catch(() => {
1035
- return;
1036
- });
1166
+ const to = await this.useModel(server, opened.sessionId, switched ? undefined : open2?.model);
1167
+ if (details.model && to) {
1168
+ details.model.to = to;
1037
1169
  }
1038
1170
  if (going) {
1039
1171
  await server.prompt(opened.sessionId, {
@@ -1077,6 +1209,44 @@ ${note}
1077
1209
  await rm(copy, { force: true });
1078
1210
  }
1079
1211
  }
1212
+ async modelThere(cloud, name, model) {
1213
+ const providers = await cloud.providers?.(name).catch(() => {
1214
+ return;
1215
+ });
1216
+ if (!providers) {
1217
+ return;
1218
+ }
1219
+ if (!providers.length) {
1220
+ throw new Error(HandoffTexts.noModel(name));
1221
+ }
1222
+ const provider = model?.includes("/") ? model.split("/")[0] : undefined;
1223
+ return model && provider && !providers.includes(provider) ? model : undefined;
1224
+ }
1225
+ async useModel(server, sessionId, model) {
1226
+ if (model?.includes("/")) {
1227
+ const [provider, ...id] = model.split("/");
1228
+ if (await server.setModel(sessionId, provider, id.join("/")).then(() => true, () => false)) {
1229
+ return;
1230
+ }
1231
+ }
1232
+ const state = await server.getState(sessionId).catch(() => {
1233
+ return;
1234
+ });
1235
+ const available = (await server.getAvailableModels(sessionId).catch(() => {
1236
+ return;
1237
+ }))?.models ?? [];
1238
+ const now = state?.model && available.find((one) => one.provider === state.model?.provider && one.id === state.model?.id);
1239
+ const chosen = now ?? available[0];
1240
+ if (!chosen) {
1241
+ return;
1242
+ }
1243
+ if (!now) {
1244
+ await server.setModel(sessionId, chosen.provider, chosen.id).catch(() => {
1245
+ return;
1246
+ });
1247
+ }
1248
+ return `${chosen.provider}/${chosen.id}`;
1249
+ }
1080
1250
  async resumeHere(running, cwd, sessionFile, error) {
1081
1251
  const opened = await this.host.openSession({ cwd, sessionFile });
1082
1252
  if (running) {
@@ -1107,7 +1277,7 @@ ${root}`;
1107
1277
  }
1108
1278
  const side = this.side(cloud, name);
1109
1279
  const there = results(await side.run(HandoffScripts.prepare(root, `${book.machine}:${root}`, last, await this.identity(root))));
1110
- const file = join(this.temp, `ta-handoff-warm-${randomUUID2().slice(0, 8)}.bundle`);
1280
+ const file = join2(this.temp, `ta-handoff-warm-${randomUUID2().slice(0, 8)}.bundle`);
1111
1281
  try {
1112
1282
  const made = results(await this.local.run(HandoffScripts.bundle(file, ["HEAD"], there.has === "yes" && last ? [last] : []), root));
1113
1283
  const size = Number(made.size);
@@ -1115,7 +1285,7 @@ ${root}`;
1115
1285
  throw new Error(`The history of ${root} is ${Math.round(size / 1048576)} MB, more than ${MAX_WARM_BYTES / 1048576} MB.`);
1116
1286
  }
1117
1287
  const arrived = posix.join(there.home, ".ta", "handoff", basename(file));
1118
- await side.run(`mkdir -p ${q(posix.dirname(arrived))}`);
1288
+ await side.run(`mkdir -p ${q2(posix.dirname(arrived))}`);
1119
1289
  await HandoffTransfer.copy(this.local, file, size, side, arrived);
1120
1290
  await side.run(HandoffScripts.fetch(arrived, "HEAD", WARM_REF), there.root);
1121
1291
  await this.save((next) => {
@@ -1143,7 +1313,7 @@ ${root}`;
1143
1313
  this.claim(handoff.sessionFile);
1144
1314
  const side = this.side(cloud, handoff.cloud);
1145
1315
  const { root, cloudRoot, cloudSessionFile, head, tree } = handoff;
1146
- const bundle = join(this.temp, `ta-handoff-${id}-back.bundle`);
1316
+ const bundle = join2(this.temp, `ta-handoff-${id}-back.bundle`);
1147
1317
  try {
1148
1318
  await this.save((book) => {
1149
1319
  book.handoffs = book.handoffs.map((other) => other.id === id ? { ...other, state: "returning" } : other);
@@ -1164,7 +1334,7 @@ ${root}`;
1164
1334
  const sent = posix.join(posix.dirname(cloudSessionFile), `.ta-handoff-${id}.bundle`);
1165
1335
  const made = results(await side.run(HandoffScripts.bundle(sent, [ref(id, "cloud")], [ref(id, "local")]), cloudRoot));
1166
1336
  await HandoffTransfer.copy(side, sent, Number(made.size), this.local, bundle);
1167
- await side.run(`rm -f ${q(sent)}`);
1337
+ await side.run(`rm -f ${q2(sent)}`);
1168
1338
  await this.local.run(HandoffScripts.fetch(bundle, ref(id, "cloud")), root);
1169
1339
  const here = results(await this.local.run(HandoffScripts.snapshot(id, "here"), root));
1170
1340
  const moved = cloudSnap.head !== head;
@@ -1232,10 +1402,10 @@ ${root}`] = cloudSnap.head;
1232
1402
  }
1233
1403
  const read = async (file) => {
1234
1404
  const size = Number(results(await side.run(HandoffScripts.size(file))).size);
1235
- const copy = join(this.temp, `ta-handoff-${handoff.id}-${basename(file)}`);
1405
+ const copy = join2(this.temp, `ta-handoff-${handoff.id}-${basename(file)}`);
1236
1406
  await HandoffTransfer.copy(side, file, size, this.local, copy);
1237
1407
  try {
1238
- return HandoffSessions.lines(await readFile(copy, "utf8"));
1408
+ return HandoffSessions.lines(await readFile2(copy, "utf8"));
1239
1409
  } finally {
1240
1410
  await rm(copy, { force: true });
1241
1411
  }
@@ -1244,7 +1414,7 @@ ${root}`] = cloudSnap.head;
1244
1414
  const other = cloudFile === handoff.cloudSessionFile ? undefined : HandoffSessions.lines(HandoffSessions.relocate((await read(cloudFile)).join(`
1245
1415
  `), handoff.cwd));
1246
1416
  result.later = HandoffSessions.later([...said, ...other ?? []]);
1247
- const before = other ?? [...HandoffSessions.lines(await readFile(handoff.sessionFile, "utf8")), ...said];
1417
+ const before = other ?? [...HandoffSessions.lines(await readFile2(handoff.sessionFile, "utf8")), ...said];
1248
1418
  const goOn = cut || result.later.length > 0 || result.files === "conflict";
1249
1419
  const details = {
1250
1420
  to: "back",
@@ -1272,7 +1442,7 @@ ${root}`] = cloudSnap.head;
1272
1442
  await appendFile2(sessionFile, `${note}
1273
1443
  `);
1274
1444
  } else {
1275
- sessionFile = join(dirname(handoff.sessionFile), basename(cloudFile));
1445
+ sessionFile = join2(dirname(handoff.sessionFile), basename(cloudFile));
1276
1446
  await writeFile2(sessionFile, `${other.join(`
1277
1447
  `)}
1278
1448
  ${note}
@@ -1288,7 +1458,7 @@ ${note}
1288
1458
  const workspaces = await cloud.list();
1289
1459
  const workspace = name ? workspaces.find((other) => other.name === name) : workspaces[0];
1290
1460
  if (!workspace) {
1291
- throw new Error(name ? `There is no cloud workspace ${name}.` : "Add a cloud workspace first: Settings, Remote hosts, Add a cloud workspace.");
1461
+ throw new Error(name ? `There is no cloud workspace ${name}.` : "Turn on “Go on here while this computer is away” for a machine in Settings, Remote hosts.");
1292
1462
  }
1293
1463
  return { cloud, workspace };
1294
1464
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-cloud-handoff",
3
- "version": "4.37.0",
3
+ "version": "4.38.0",
4
4
  "description": "A pi package for Ta: a conversation of this computer goes on in a cloud workspace while the computer is away, by hand or by itself as it sleeps or runs low, its files and what was said carried over and back, checked the same on both sides",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -27,10 +27,10 @@
27
27
  "watch": "tsc -b -w --preserveWatchOutput"
28
28
  },
29
29
  "devDependencies": {
30
- "@celljs/core": "4.37.0",
31
- "@celljs/ta-protocol": "4.37.0",
32
- "@celljs/ta-web": "4.37.0",
33
- "@types/react": "^19.3.0"
30
+ "@celljs/core": "4.38.0",
31
+ "@celljs/ta-protocol": "4.38.0",
32
+ "@celljs/ta-web": "4.38.0",
33
+ "@types/react": "^19.3.3"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"
package/web/index.js CHANGED
@@ -90,12 +90,13 @@ var EN = {
90
90
  noteBack: "Back on this computer from {cloud}",
91
91
  noteConflict: "{n} file(s) changed on both sides",
92
92
  noteLater: "{n} step(s) left for this computer",
93
+ noteModel: "no key of {model} there: another model goes on",
93
94
  toolHandOff: "Move to the cloud",
94
95
  toolLater: "Leave for the computer",
95
96
  noteSleep: "The computer went to sleep: going on in the cloud · {cloud}",
96
97
  noteBattery: "Battery low: going on in the cloud · {cloud}",
97
98
  settingsTitle: "Cloud handoff",
98
- settingsNoCloud: "Add a cloud workspace first, in Remote hosts: then what runs here goes on there when this computer has to go.",
99
+ settingsNoCloud: "First, in Remote hosts, turn on “Go on here while this computer is away” for a machine you reach by ssh: then what runs here goes on there when this computer has to go.",
99
100
  sleepLabel: "When the computer sleeps",
100
101
  sleepHint: "Lid closed or put to sleep: conversations that run go on in the cloud. On a Mac, sleep waits up to 20 s for it.",
101
102
  batteryLabel: "When the battery runs low",
@@ -131,12 +132,13 @@ var ZH = {
131
132
  noteBack: "从云端 {cloud} 接回本机",
132
133
  noteConflict: "{n} 个文件两边都改过",
133
134
  noteLater: "{n} 件事留给本机做",
135
+ noteModel: "云端用不了 {model},换成那边能用的模型",
134
136
  toolHandOff: "换到云端",
135
137
  toolLater: "留给本机做",
136
138
  noteSleep: "电脑睡眠,换到云端接着做 · {cloud}",
137
139
  noteBattery: "电量低,换到云端接着做 · {cloud}",
138
140
  settingsTitle: "云端接力",
139
- settingsNoCloud: "先在「远程主机」里添加云端工作区:之后电脑要走开时,正在做的事会换到那边接着做。",
141
+ settingsNoCloud: "先在「远程主机」里选一台能 ssh 的机器,打开「电脑走开时在这里接着做」:之后电脑要走开时,正在做的事会换到那边接着做。",
140
142
  sleepLabel: "合盖或睡眠时交给云端",
141
143
  sleepHint: "正在做的对话换到云端接着做。Mac 会等它交接完再睡,最多 20 秒。",
142
144
  batteryLabel: "电量低时交给云端",
@@ -172,12 +174,13 @@ var ZH_TW = {
172
174
  noteBack: "從雲端 {cloud} 接回本機",
173
175
  noteConflict: "{n} 個檔案兩邊都改過",
174
176
  noteLater: "{n} 件事留給本機做",
177
+ noteModel: "雲端用不了 {model},換成那邊能用的模型",
175
178
  toolHandOff: "換到雲端",
176
179
  toolLater: "留給本機做",
177
180
  noteSleep: "電腦睡眠,換到雲端接著做 · {cloud}",
178
181
  noteBattery: "電量低,換到雲端接著做 · {cloud}",
179
182
  settingsTitle: "雲端接力",
180
- settingsNoCloud: "先在「遠端主機」裡新增雲端工作區:之後電腦要走開時,正在做的事會換到那邊接著做。",
183
+ settingsNoCloud: "先在「遠端主機」裡選一台能 ssh 的機器,打開「電腦走開時在這裡接著做」:之後電腦要走開時,正在做的事會換到那邊接著做。",
181
184
  sleepLabel: "闔蓋或睡眠時交給雲端",
182
185
  sleepHint: "正在做的對話換到雲端接著做。Mac 會等它交接完再睡,最多 20 秒。",
183
186
  batteryLabel: "電量低時交給雲端",
@@ -213,12 +216,13 @@ var JA = {
213
216
  noteBack: "クラウド {cloud} からこのパソコンに戻しました",
214
217
  noteConflict: "{n} 個のファイルが両方で変更",
215
218
  noteLater: "{n} 件をこのパソコンで",
219
+ noteModel: "クラウドでは {model} が使えないので別のモデルで続ける",
216
220
  toolHandOff: "クラウドへ移す",
217
221
  toolLater: "パソコンに残す",
218
222
  noteSleep: "パソコンがスリープ、クラウドで続ける · {cloud}",
219
223
  noteBattery: "電池残量が少ない、クラウドで続ける · {cloud}",
220
224
  settingsTitle: "クラウドで続ける",
221
- settingsNoCloud: "先に「リモートホスト」でクラウドのワークスペースを追加してください。パソコンを離れるとき、進行中の作業がそちらで続きます。",
225
+ settingsNoCloud: "先に「リモートホスト」で ssh できるマシンの「パソコンを離れている間ここで続ける」をオンにしてください。パソコンを離れるとき、進行中の作業がそちらで続きます。",
222
226
  sleepLabel: "スリープ時にクラウドへ",
223
227
  sleepHint: "ふたを閉じたりスリープしたりすると、進行中の会話をクラウドで続けます。Mac は最大 20 秒待ってからスリープします。",
224
228
  batteryLabel: "電池残量が少ないときクラウドへ",
@@ -379,6 +383,9 @@ function injectStyle() {
379
383
  function local(cwd) {
380
384
  return cwd.startsWith("/") || /^[A-Za-z]:[\\/]/.test(cwd);
381
385
  }
386
+ function page(url) {
387
+ return /^https?:\/\//.test(url);
388
+ }
382
389
  function reason(error) {
383
390
  return error instanceof Error ? error.message : String(error);
384
391
  }
@@ -432,7 +439,7 @@ function activate(pi) {
432
439
  pi.ui.notify(words().moving, "info", { key: `ta-cloud-handoff:${sessionFile}` });
433
440
  pi.callHost("handOff", sessionFile, cloud).then((moved) => pi.ui.notify(say(moved.wasRunning ? words().moved : words().movedIdle, { cloud: moved.cloud }), "info", {
434
441
  key: `ta-cloud-handoff:${sessionFile}`,
435
- action: { label: words().openCloud, run: () => void pi.openLink(moved.cloudUrl) }
442
+ ...page(moved.cloudUrl) ? { action: { label: words().openCloud, run: () => void pi.openLink(moved.cloudUrl) } } : {}
436
443
  })).catch((error) => pi.ui.notify(say(words().failed, { reason: reason(error) }), "error", { key: `ta-cloud-handoff:${sessionFile}` }));
437
444
  };
438
445
  const takeBack = (handoff) => {
@@ -463,7 +470,7 @@ function activate(pi) {
463
470
  }
464
471
  return [
465
472
  { key: "cloud-handoff-back", label: words().takeBack, icon: icons.Laptop ?? icons.Monitor, run: () => takeBack(handoff) },
466
- { key: "cloud-handoff-open", label: words().openCloud, icon: icons.ExternalLink, run: () => void pi.openLink(handoff.cloudUrl) }
473
+ ...page(handoff.cloudUrl) ? [{ key: "cloud-handoff-open", label: words().openCloud, icon: icons.ExternalLink, run: () => void pi.openLink(handoff.cloudUrl) }] : []
467
474
  ];
468
475
  }
469
476
  if (!session.sessionFile || !local(session.cwd) || session.group) {
@@ -495,7 +502,8 @@ function activate(pi) {
495
502
  cloud: note.cloud
496
503
  }),
497
504
  note.conflicts?.length ? say(w.noteConflict, { n: note.conflicts.length }) : "",
498
- note.later?.length ? say(w.noteLater, { n: note.later.length }) : ""
505
+ note.later?.length ? say(w.noteLater, { n: note.later.length }) : "",
506
+ note.model ? say(w.noteModel, { model: note.model.from.slice(note.model.from.indexOf("/") + 1) }) : ""
499
507
  ].filter(Boolean);
500
508
  return /* @__PURE__ */ React.createElement("div", {
501
509
  className: "tch-note",