@celljs/ta-host 4.7.0 → 4.8.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.
Files changed (145) hide show
  1. package/README.en.md +2 -0
  2. package/README.md +3 -1
  3. package/esm/node/auth-server-impl.d.ts +2 -0
  4. package/esm/node/auth-server-impl.js +7 -0
  5. package/esm/node/auth-server-impl.js.map +1 -1
  6. package/esm/node/extension-providers.d.ts +22 -0
  7. package/esm/node/extension-providers.js +52 -0
  8. package/esm/node/extension-providers.js.map +1 -0
  9. package/esm/node/index.d.ts +2 -0
  10. package/esm/node/index.js +2 -0
  11. package/esm/node/index.js.map +1 -1
  12. package/esm/node/package-host-extension.d.ts +4 -2
  13. package/esm/node/package-host-extension.js +4 -3
  14. package/esm/node/package-host-extension.js.map +1 -1
  15. package/esm/node/package-hosts.d.ts +2 -2
  16. package/esm/node/package-hosts.js +2 -0
  17. package/esm/node/package-hosts.js.map +1 -1
  18. package/esm/node/remote-ui-context.d.ts +6 -1
  19. package/esm/node/remote-ui-context.js +31 -3
  20. package/esm/node/remote-ui-context.js.map +1 -1
  21. package/esm/node/session-files.d.ts +18 -0
  22. package/esm/node/session-files.js +32 -0
  23. package/esm/node/session-files.js.map +1 -0
  24. package/esm/node/settings-server-impl.d.ts +1 -0
  25. package/esm/node/settings-server-impl.js +8 -0
  26. package/esm/node/settings-server-impl.js.map +1 -1
  27. package/esm/node/settings-store.d.ts +6 -1
  28. package/esm/node/settings-store.js +17 -3
  29. package/esm/node/settings-store.js.map +1 -1
  30. package/esm/node/ta-session-factory.d.ts +3 -0
  31. package/esm/node/ta-session-factory.js +16 -3
  32. package/esm/node/ta-session-factory.js.map +1 -1
  33. package/esm/node/ta-session.js +1 -0
  34. package/esm/node/ta-session.js.map +1 -1
  35. package/esm/node/terminal-service.d.ts +18 -2
  36. package/esm/node/terminal-service.js +45 -9
  37. package/esm/node/terminal-service.js.map +1 -1
  38. package/esm/node/terminal-util.d.ts +14 -0
  39. package/esm/node/terminal-util.js +30 -1
  40. package/esm/node/terminal-util.js.map +1 -1
  41. package/esm/node/workspace-protocol.d.ts +2 -0
  42. package/esm/node/workspace-server-impl.d.ts +2 -1
  43. package/esm/node/workspace-server-impl.js +13 -1
  44. package/esm/node/workspace-server-impl.js.map +1 -1
  45. package/esm/node/workspace-service.d.ts +1 -0
  46. package/esm/node/workspace-service.js +12 -0
  47. package/esm/node/workspace-service.js.map +1 -1
  48. package/lib/node/auth-server-impl.d.ts +2 -0
  49. package/lib/node/auth-server-impl.d.ts.map +1 -1
  50. package/lib/node/auth-server-impl.js +7 -0
  51. package/lib/node/auth-server-impl.js.map +1 -1
  52. package/lib/node/extension-providers.d.ts +23 -0
  53. package/lib/node/extension-providers.d.ts.map +1 -0
  54. package/lib/node/extension-providers.js +55 -0
  55. package/lib/node/extension-providers.js.map +1 -0
  56. package/lib/node/index.d.ts +2 -0
  57. package/lib/node/index.d.ts.map +1 -1
  58. package/lib/node/index.js +2 -0
  59. package/lib/node/index.js.map +1 -1
  60. package/lib/node/package-host-extension.d.ts +4 -2
  61. package/lib/node/package-host-extension.d.ts.map +1 -1
  62. package/lib/node/package-host-extension.js +4 -3
  63. package/lib/node/package-host-extension.js.map +1 -1
  64. package/lib/node/package-hosts.d.ts +2 -2
  65. package/lib/node/package-hosts.d.ts.map +1 -1
  66. package/lib/node/package-hosts.js +2 -0
  67. package/lib/node/package-hosts.js.map +1 -1
  68. package/lib/node/remote-ui-context.d.ts +6 -1
  69. package/lib/node/remote-ui-context.d.ts.map +1 -1
  70. package/lib/node/remote-ui-context.js +31 -3
  71. package/lib/node/remote-ui-context.js.map +1 -1
  72. package/lib/node/remote-ui-context.spec.js +23 -0
  73. package/lib/node/remote-ui-context.spec.js.map +1 -1
  74. package/lib/node/session-files.d.ts +19 -0
  75. package/lib/node/session-files.d.ts.map +1 -0
  76. package/lib/node/session-files.js +36 -0
  77. package/lib/node/session-files.js.map +1 -0
  78. package/lib/node/session-files.spec.d.ts +2 -0
  79. package/lib/node/session-files.spec.d.ts.map +1 -0
  80. package/lib/node/session-files.spec.js +51 -0
  81. package/lib/node/session-files.spec.js.map +1 -0
  82. package/lib/node/settings-server-impl.d.ts +1 -0
  83. package/lib/node/settings-server-impl.d.ts.map +1 -1
  84. package/lib/node/settings-server-impl.js +8 -0
  85. package/lib/node/settings-server-impl.js.map +1 -1
  86. package/lib/node/settings-store.d.ts +6 -1
  87. package/lib/node/settings-store.d.ts.map +1 -1
  88. package/lib/node/settings-store.js +17 -3
  89. package/lib/node/settings-store.js.map +1 -1
  90. package/lib/node/settings-store.spec.js +3 -1
  91. package/lib/node/settings-store.spec.js.map +1 -1
  92. package/lib/node/ta-session-factory.d.ts +3 -0
  93. package/lib/node/ta-session-factory.d.ts.map +1 -1
  94. package/lib/node/ta-session-factory.js +16 -3
  95. package/lib/node/ta-session-factory.js.map +1 -1
  96. package/lib/node/ta-session.d.ts.map +1 -1
  97. package/lib/node/ta-session.js +1 -0
  98. package/lib/node/ta-session.js.map +1 -1
  99. package/lib/node/terminal-service.d.ts +18 -2
  100. package/lib/node/terminal-service.d.ts.map +1 -1
  101. package/lib/node/terminal-service.js +44 -8
  102. package/lib/node/terminal-service.js.map +1 -1
  103. package/lib/node/terminal-service.spec.js +31 -0
  104. package/lib/node/terminal-service.spec.js.map +1 -1
  105. package/lib/node/terminal-util.d.ts +14 -0
  106. package/lib/node/terminal-util.d.ts.map +1 -1
  107. package/lib/node/terminal-util.js +30 -1
  108. package/lib/node/terminal-util.js.map +1 -1
  109. package/lib/node/terminal-util.spec.js +24 -0
  110. package/lib/node/terminal-util.spec.js.map +1 -1
  111. package/lib/node/workspace-protocol.d.ts +2 -0
  112. package/lib/node/workspace-protocol.d.ts.map +1 -1
  113. package/lib/node/workspace-server-impl.d.ts +2 -1
  114. package/lib/node/workspace-server-impl.d.ts.map +1 -1
  115. package/lib/node/workspace-server-impl.js +12 -0
  116. package/lib/node/workspace-server-impl.js.map +1 -1
  117. package/lib/node/workspace-service.d.ts +1 -0
  118. package/lib/node/workspace-service.d.ts.map +1 -1
  119. package/lib/node/workspace-service.js +12 -0
  120. package/lib/node/workspace-service.js.map +1 -1
  121. package/lib/node/workspace-service.spec.js +9 -0
  122. package/lib/node/workspace-service.spec.js.map +1 -1
  123. package/package.json +10 -10
  124. package/src/node/auth-server-impl.ts +6 -0
  125. package/src/node/extension-providers.ts +49 -0
  126. package/src/node/index.ts +2 -0
  127. package/src/node/package-host-extension.ts +7 -4
  128. package/src/node/package-hosts.ts +4 -2
  129. package/src/node/remote-ui-context.spec.ts +25 -0
  130. package/src/node/remote-ui-context.ts +34 -5
  131. package/src/node/session-files.spec.ts +52 -0
  132. package/src/node/session-files.ts +41 -0
  133. package/src/node/settings-server-impl.ts +9 -0
  134. package/src/node/settings-store.spec.ts +3 -1
  135. package/src/node/settings-store.ts +17 -4
  136. package/src/node/ta-session-factory.ts +16 -3
  137. package/src/node/ta-session.ts +1 -0
  138. package/src/node/terminal-service.spec.ts +35 -0
  139. package/src/node/terminal-service.ts +46 -14
  140. package/src/node/terminal-util.spec.ts +31 -0
  141. package/src/node/terminal-util.ts +32 -1
  142. package/src/node/workspace-protocol.ts +2 -0
  143. package/src/node/workspace-server-impl.ts +18 -0
  144. package/src/node/workspace-service.spec.ts +11 -1
  145. package/src/node/workspace-service.ts +12 -0
@@ -0,0 +1,41 @@
1
+ import { homedir } from 'node:os';
2
+ import { dirname, posix, resolve } from 'node:path';
3
+ import type { ExecEnvironment } from '@celljs/ta-exec';
4
+ import { ExecOperations } from '@celljs/ta-harness';
5
+ import type { TaSessionFiles } from '@celljs/ta-protocol';
6
+
7
+ /**
8
+ * The files of a session on its execution environment. `piCwd` is the directory pi works in for it
9
+ * (PiDirectories): pi's tools resolve paths there, and ExecOperations maps them into the environment.
10
+ */
11
+ export class ExecSessionFiles implements TaSessionFiles {
12
+ protected readonly operations: ExecOperations;
13
+
14
+ constructor(
15
+ protected readonly environment: ExecEnvironment,
16
+ protected readonly piCwd: string,
17
+ ) {
18
+ this.operations = new ExecOperations(environment, piCwd);
19
+ }
20
+
21
+ /** As pi's tools resolve a path of their arguments: without a leading `@`, `~` the home directory, against pi's directory. */
22
+ resolve(path: string): string {
23
+ const bare = path.startsWith('@') ? path.slice(1) : path;
24
+ const expanded = bare === '~' || bare.startsWith('~/') ? homedir() + bare.slice(1) : bare;
25
+ return this.operations.map(resolve(this.piCwd, expanded));
26
+ }
27
+
28
+ async read(path: string): Promise<Uint8Array | undefined> {
29
+ const stat = await this.environment.stat(path);
30
+ return stat && !stat.isDirectory ? this.environment.readFile(path) : undefined;
31
+ }
32
+
33
+ async write(path: string, data: Uint8Array): Promise<void> {
34
+ await this.environment.mkdir(this.environment.local ? dirname(path) : posix.dirname(path));
35
+ await this.environment.writeFile(path, data);
36
+ }
37
+
38
+ remove(path: string): Promise<void> {
39
+ return this.environment.remove(path, { recursive: false });
40
+ }
41
+ }
@@ -27,6 +27,15 @@ export class TaSettingsServerImpl implements TaSettingsServer {
27
27
  });
28
28
  }
29
29
 
30
+ setLanguage(language: string): Promise<void> {
31
+ return TaErrors.guard(async () => {
32
+ if (typeof language !== 'string' || !/^[a-z]{2,3}(-[A-Za-z0-9]{2,8})*$/.test(language)) {
33
+ throw TaErrors.invalidArgument(`${String(language)} is not a language tag.`, 'Pass a tag such as zh-CN or en-US.');
34
+ }
35
+ process.env.TA_LANGUAGE = language;
36
+ });
37
+ }
38
+
30
39
  protected async cwd(cwd?: string): Promise<string> {
31
40
  return cwd || (await this.environments.defaultCwd());
32
41
  }
@@ -78,7 +78,8 @@ describe('TaSettingsStoreImpl', () => {
78
78
  store = new TaSettingsStoreImpl();
79
79
  Object.assign(store, {
80
80
  codingAgentOptions: { agentDir },
81
- modelRuntimeProvider: { provide: async () => ({ getAvailable: async () => [{ provider: 'fake', id: 'fake-1' }] }) },
81
+ modelRuntimeProvider: { provide: async () => ({ getAvailable: async () => [{ provider: 'fake', id: 'fake-1', name: 'Fake One' }] }) },
82
+ extensionProviders: { ensure: async () => undefined },
82
83
  sessions: { list: () => [session(cwd), session(join(root, 'other'))] },
83
84
  directories: { toPi: (location: string) => location, fromPi: (dir: string) => dir },
84
85
  });
@@ -90,6 +91,7 @@ describe('TaSettingsStoreImpl', () => {
90
91
  const snapshot = await store.update('user', { 'compaction.enabled': false, model: 'fake/fake-1' }, cwd);
91
92
  expect(snapshot.user).toEqual({ model: 'fake/fake-1', 'compaction.enabled': false });
92
93
  expect(snapshot.models).toEqual(['fake/fake-1']);
94
+ expect(snapshot.modelNames).toEqual({ 'fake/fake-1': 'Fake One' });
93
95
  expect(JSON.parse(readFileSync(join(agentDir, 'settings.json'), 'utf8'))).toEqual({
94
96
  theme: 'dark',
95
97
  packages: ['npm:x'],
@@ -3,6 +3,7 @@ import { dirname, join } from 'node:path';
3
3
  import { CODING_AGENT_OPTIONS, type CodingAgentOptions, ModelRuntimeProvider, PiSdkUtil } from '@celljs/ai-coding-agent';
4
4
  import { Autowired, Component, Value } from '@celljs/core';
5
5
  import { TA_SETTINGS, type TaSettingSpec, type TaSettingsScope, type TaSettingsSnapshot } from '@celljs/ta-protocol';
6
+ import { ExtensionProviders } from './extension-providers';
6
7
  import { TaSessionRegistry } from './host-protocol';
7
8
  import { PiDirectories } from './remote/remote-protocol';
8
9
  import { TaErrors } from './ta-errors';
@@ -211,6 +212,9 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
211
212
  @Autowired(TaSessionRegistry)
212
213
  protected readonly sessions: TaSessionRegistry;
213
214
 
215
+ @Autowired(ExtensionProviders)
216
+ protected readonly extensionProviders: ExtensionProviders;
217
+
214
218
  @Autowired(PiDirectories)
215
219
  protected readonly directories: PiDirectories;
216
220
 
@@ -219,7 +223,7 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
219
223
 
220
224
  async get(cwd: string): Promise<TaSettingsSnapshot> {
221
225
  const { userFile, projectFile, trusted } = await this.files(cwd);
222
- const [user, project, models] = await Promise.all([this.read(userFile), this.read(projectFile), this.models()]);
226
+ const [user, project, { models, modelNames }] = await Promise.all([this.read(userFile), this.read(projectFile), this.models()]);
223
227
  return {
224
228
  user: TaSettingsUtil.read(user),
225
229
  project: TaSettingsUtil.read(project),
@@ -228,6 +232,7 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
228
232
  projectFile,
229
233
  projectTrusted: trusted !== false,
230
234
  models,
235
+ modelNames,
231
236
  };
232
237
  }
233
238
 
@@ -291,12 +296,20 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
291
296
  return TaSettingsUtil.parse(text, file);
292
297
  }
293
298
 
294
- protected async models(): Promise<string[]> {
299
+ protected async models(): Promise<{ models: string[]; modelNames: Record<string, string> }> {
295
300
  try {
296
301
  const runtime = await this.modelRuntimeProvider.provide();
297
- return (await runtime.getAvailable()).map(model => `${model.provider}/${model.id}`);
302
+ await this.extensionProviders.ensure();
303
+ const available = await runtime.getAvailable();
304
+ const modelNames: Record<string, string> = {};
305
+ for (const model of available) {
306
+ if (model.name && model.name !== model.id) {
307
+ modelNames[`${model.provider}/${model.id}`] = model.name;
308
+ }
309
+ }
310
+ return { models: available.map(model => `${model.provider}/${model.id}`), modelNames };
298
311
  } catch {
299
- return [];
312
+ return { models: [], modelNames: {} };
300
313
  }
301
314
  }
302
315
 
@@ -26,10 +26,12 @@ import {
26
26
  } from '@celljs/ta-harness';
27
27
  import type { TaOpenSessionOptions } from '@celljs/ta-protocol';
28
28
  import { ClientToolBroker } from './client-tool-broker';
29
+ import { ExtensionProviders } from './extension-providers';
29
30
  import { TA_HOST_OPTIONS, type TaHostOptions, TaSessionFactory } from './host-protocol';
30
31
  import { PackageHostExtension } from './package-host-extension';
31
32
  import { PackageHosts } from './package-hosts';
32
33
  import { PiDirectories } from './remote/remote-protocol';
34
+ import { ExecSessionFiles } from './session-files';
33
35
  import { TaErrors } from './ta-errors';
34
36
  import { TaSession } from './ta-session';
35
37
 
@@ -67,6 +69,9 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
67
69
  @Autowired(PiDirectories)
68
70
  protected readonly directories: PiDirectories;
69
71
 
72
+ @Autowired(ExtensionProviders)
73
+ protected readonly extensionProviders: ExtensionProviders;
74
+
70
75
  async create(options: TaOpenSessionOptions, requestClose: (session: TaSession) => void): Promise<TaSession> {
71
76
  const clientTools = new ClientToolBroker(options.clientTools);
72
77
  const sdk = await PiSdkUtil.load();
@@ -82,6 +87,10 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
82
87
  throw TaErrors.sessionLocked(sessionFile);
83
88
  }
84
89
  try {
90
+ if (options.model && !this.findModel(modelRuntime, options.model)) {
91
+ // A model of a pi package, whose provider is there once the package loaded.
92
+ await this.extensionProviders.ensure();
93
+ }
85
94
  const model = options.model ? this.resolveModel(modelRuntime, options.model) : undefined;
86
95
  const runtime = await sdk.createAgentSessionRuntime(
87
96
  async ({ cwd, agentDir: dir, sessionManager: manager, sessionStartEvent }) => {
@@ -158,7 +167,7 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
158
167
  protected extensionFactories(sdk: PiSdk, environment: ExecEnvironment, piCwd: string, lock: SessionLock): InlineExtension[] {
159
168
  const builtIn = [
160
169
  new SessionLockExtension(lock),
161
- new PackageHostExtension(this.packageHosts),
170
+ new PackageHostExtension(this.packageHosts, new ExecSessionFiles(environment, piCwd)),
162
171
  new BrandExtension(sdk),
163
172
  ...(environment.local ? [] : [new ExecExtension(environment, sdk, piCwd)]),
164
173
  ];
@@ -192,9 +201,13 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
192
201
  return new sdk.ProjectTrustStore(agentDir).get(cwd) ?? !sdk.hasTrustRequiringProjectResources(cwd);
193
202
  }
194
203
 
195
- protected resolveModel(modelRuntime: ModelRuntime, ref: string): Model {
204
+ protected findModel(modelRuntime: ModelRuntime, ref: string): Model | undefined {
196
205
  const index = ref.indexOf('/');
197
- const model = index > 0 ? modelRuntime.getModel(ref.slice(0, index), ref.slice(index + 1)) : undefined;
206
+ return index > 0 ? modelRuntime.getModel(ref.slice(0, index), ref.slice(index + 1)) : undefined;
207
+ }
208
+
209
+ protected resolveModel(modelRuntime: ModelRuntime, ref: string): Model {
210
+ const model = this.findModel(modelRuntime, ref);
198
211
  if (!model) {
199
212
  throw TaErrors.modelNotFound(ref);
200
213
  }
@@ -105,6 +105,7 @@ export class TaSession {
105
105
  info: this.info(),
106
106
  events,
107
107
  pendingUi: this.ui.pendingRequests,
108
+ shownUi: this.ui.shownRequests,
108
109
  pendingClientTools: this.context.clientTools.pendingRequests,
109
110
  lastSeq: this.log.lastSeq,
110
111
  gap,
@@ -67,15 +67,50 @@ describe('DefaultTaTerminals', () => {
67
67
  expect(terminals.list(as(connection))).toEqual([]);
68
68
  });
69
69
 
70
+ it('sizes the shell as its view asked, even before the shell named its terminal', async () => {
71
+ const connection = new FakeConnection();
72
+ const info = await terminals.open(as(connection), { cwd, cols: 80, rows: 24 });
73
+ if (!info.pty) {
74
+ return;
75
+ }
76
+ await terminals.resize(as(connection), info.terminalId, 101, 31);
77
+ await Bun.sleep(300);
78
+ terminals.write(as(connection), info.terminalId, 'stty size\r');
79
+ await until(() => connection.output.includes('31 101'), 'the shell has the size of its view');
80
+ });
81
+
82
+ it('asks each machine for its own shell and system', async () => {
83
+ const asked: string[] = [];
84
+ const service = new DefaultTaTerminals();
85
+ const detect = (service as unknown as { detect(env: { exec: unknown; local: boolean }, machine: string): Promise<{ system: string }> }).detect.bind(
86
+ service,
87
+ );
88
+ const env = (system: string) => ({
89
+ local: false,
90
+ exec: async (_command: string, options: { onData(data: Uint8Array): void }) => {
91
+ asked.push(system);
92
+ options.onData(new TextEncoder().encode(`${system}\n/usr/bin/script\n/bin/zsh`));
93
+ return { exitCode: 0 };
94
+ },
95
+ });
96
+ expect((await detect(env('Linux'), 'box')).system).toBe('Linux');
97
+ expect((await detect(env('Darwin'), '')).system).toBe('Darwin');
98
+ expect((await detect(env('Darwin'), 'box')).system).toBe('Linux');
99
+ expect(asked).toEqual(['Linux', 'Darwin']);
100
+ });
101
+
70
102
  it('keeps a shell for another connection after its own dropped', async () => {
71
103
  const first = new FakeConnection();
72
104
  const info = await terminals.open(as(first), { cwd, cols: 80, rows: 24 });
73
105
  const stranger = new FakeConnection();
74
106
  expect(terminals.list(as(stranger))).toEqual([]);
75
107
  expect(() => terminals.write(as(stranger), info.terminalId, 'x')).toThrow('No terminal');
108
+ terminals.write(as(first), info.terminalId, 'echo first-$((1+2))\r');
109
+ await until(() => first.output.includes('first-3'), 'the first connection got the output');
76
110
  first.close();
77
111
  const second = new FakeConnection();
78
112
  expect(terminals.list(as(second))).toEqual([info]);
113
+ await until(() => second.output.startsWith('\x1bc') && second.output.includes('first-3'), 'the second connection got what the shell showed before');
79
114
  terminals.write(as(second), info.terminalId, 'echo back-$((1+1))\r');
80
115
  await until(() => second.output.includes('back-2'), 'the second connection got the output');
81
116
  await terminals.close(as(second), info.terminalId);
@@ -1,7 +1,7 @@
1
1
  import { type Application, ApplicationLifecycle, Autowired, Component } from '@celljs/core';
2
2
  import type { RpcConnection } from '@celljs/rpc/lib/node';
3
3
  import { type ExecEnvironment, ExecEnvironmentFactory, type ExecProcess } from '@celljs/ta-exec';
4
- import { TaClient, type TaOpenTerminalOptions, type TaTerminalInfo } from '@celljs/ta-protocol';
4
+ import { TaClient, TaLocation, type TaOpenTerminalOptions, type TaTerminalInfo } from '@celljs/ta-protocol';
5
5
  import { TaErrors } from './ta-errors';
6
6
  import { TaTerminals } from './terminal-protocol';
7
7
  import { PipeDiscipline, type TerminalPlatform, TerminalUtil, TtyMarker } from './terminal-util';
@@ -19,12 +19,16 @@ class Terminal {
19
19
  owner?: RpcConnection;
20
20
  orphanTimer?: ReturnType<typeof setTimeout>;
21
21
  pending = '';
22
+ /** The last output, for a page that takes the shell again after its own went (a reload). */
23
+ history = '';
22
24
  flushTimer?: ReturnType<typeof setTimeout>;
23
25
  sending = false;
24
26
  /** The code the shell ended with, as it said, or as its process did. */
25
27
  exitCode?: number;
26
28
  /** Its process ended. */
27
29
  exited = false;
30
+ /** The size its view asked for last; it waits for the name of the terminal when that is not known yet. */
31
+ size?: { cols: number; rows: number };
28
32
  /** Its client knows it ended: nothing more goes. */
29
33
  ended = false;
30
34
  readonly decoder = new TextDecoder();
@@ -51,7 +55,8 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
51
55
 
52
56
  protected readonly terminals = new Map<string, Terminal>();
53
57
  protected readonly watched = new WeakSet<RpcConnection>();
54
- protected platform?: Promise<TerminalPlatform>;
58
+ /** By machine: `''` for this one (or the exec server), else the host of `ssh://`. */
59
+ protected readonly platforms = new Map<string, Promise<TerminalPlatform>>();
55
60
  protected nextId = 0;
56
61
 
57
62
  async open(connection: RpcConnection, options: TaOpenTerminalOptions & { cwd: string }): Promise<TaTerminalInfo> {
@@ -60,7 +65,7 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
60
65
  if (!stat?.isDirectory) {
61
66
  throw TaErrors.invalidArgument(`${options.cwd} is not a directory.`, 'Open the terminal in a directory that exists.');
62
67
  }
63
- const platform = await this.detect(env);
68
+ const platform = await this.detect(env, this.machine(options.cwd));
64
69
  const launch = TerminalUtil.launch(platform, TerminalUtil.size(options.cols, DEFAULT_COLS), TerminalUtil.size(options.rows, DEFAULT_ROWS));
65
70
  // `pty`: false when the shell runs on pipes, so that the view can say why some programs behave otherwise.
66
71
  const info: TaTerminalInfo & { pty: boolean } = { terminalId: `t${++this.nextId}`, cwd: options.cwd, shell: platform.shell, pty: launch.pty };
@@ -89,13 +94,14 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
89
94
 
90
95
  async resize(connection: RpcConnection, terminalId: string, cols: number, rows: number): Promise<void> {
91
96
  const terminal = this.require(connection, terminalId);
92
- if (terminal.marker.tty) {
93
- const command = TerminalUtil.resizeCommand(
94
- terminal.platform,
95
- terminal.marker.tty,
96
- TerminalUtil.size(cols, DEFAULT_COLS),
97
- TerminalUtil.size(rows, DEFAULT_ROWS),
98
- );
97
+ terminal.size = { cols: TerminalUtil.size(cols, DEFAULT_COLS), rows: TerminalUtil.size(rows, DEFAULT_ROWS) };
98
+ await this.applySize(terminal);
99
+ }
100
+
101
+ /** A view sizes its shell as soon as it shows, often before the shell named its terminal: the size waits for it. */
102
+ protected async applySize(terminal: Terminal): Promise<void> {
103
+ if (terminal.marker.tty && terminal.size) {
104
+ const command = TerminalUtil.resizeCommand(terminal.platform, terminal.marker.tty, terminal.size.cols, terminal.size.rows);
99
105
  await terminal.env.exec(command, { timeout: 5000 });
100
106
  }
101
107
  }
@@ -140,10 +146,15 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
140
146
  return terminal;
141
147
  }
142
148
 
149
+ /**
150
+ * A connection takes a shell nobody has. What its page shows went with the page: it gets the last
151
+ * output again, after a reset, so that a page that kept its screen does not show it twice.
152
+ */
143
153
  protected adopt(terminal: Terminal, connection: RpcConnection): void {
144
154
  clearTimeout(terminal.orphanTimer);
145
155
  terminal.orphanTimer = undefined;
146
156
  terminal.owner = connection;
157
+ terminal.pending = terminal.history ? `${TerminalUtil.RESET}${terminal.history}` : terminal.pending;
147
158
  this.watch(connection);
148
159
  this.schedule(terminal);
149
160
  }
@@ -166,7 +177,11 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
166
177
  protected output(terminal: Terminal, text: string): void {
167
178
  const { text: rest, exitCode } = TerminalUtil.takeExitCode(text);
168
179
  terminal.exitCode ??= exitCode;
180
+ const named = terminal.marker.tty !== undefined;
169
181
  const shown = terminal.marker.feed(rest);
182
+ if (!named && terminal.marker.tty !== undefined) {
183
+ void this.applySize(terminal).catch(() => undefined);
184
+ }
170
185
  this.queue(terminal, terminal.discipline ? terminal.discipline.output(shown) : shown);
171
186
  }
172
187
 
@@ -176,8 +191,9 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
176
191
  }
177
192
  terminal.pending += text;
178
193
  if (terminal.pending.length > MAX_PENDING_CHARS) {
179
- terminal.pending = terminal.pending.slice(-MAX_PENDING_CHARS);
194
+ terminal.pending = TerminalUtil.tail(terminal.pending, MAX_PENDING_CHARS);
180
195
  }
196
+ terminal.history = TerminalUtil.tail(terminal.history + text, MAX_PENDING_CHARS);
181
197
  this.schedule(terminal);
182
198
  }
183
199
 
@@ -243,8 +259,21 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
243
259
  terminal.process.kill();
244
260
  }
245
261
 
246
- protected detect(env: ExecEnvironment): Promise<TerminalPlatform> {
247
- this.platform ??= (async () => {
262
+ /** The machine of a location: its shell, `script` and system are those of that machine. */
263
+ protected machine(location: string): string {
264
+ try {
265
+ return TaLocation.parse(location)?.host ?? '';
266
+ } catch {
267
+ return '';
268
+ }
269
+ }
270
+
271
+ protected detect(env: ExecEnvironment, machine: string): Promise<TerminalPlatform> {
272
+ let platform = this.platforms.get(machine);
273
+ if (platform) {
274
+ return platform;
275
+ }
276
+ platform = (async () => {
248
277
  let output = '';
249
278
  const decoder = new TextDecoder();
250
279
  const { exitCode } = await env.exec(`${TerminalUtil.PROBE} 2>/dev/null`, {
@@ -254,6 +283,9 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
254
283
  const fallback = env.local && process.platform === 'win32' ? (process.env.ComSpec ?? 'cmd.exe') : '/bin/sh';
255
284
  return exitCode === 0 ? TerminalUtil.parsePlatform(output, fallback) : { system: '', script: false, shell: fallback };
256
285
  })();
257
- return this.platform;
286
+ // A machine that could not answer is asked again next time.
287
+ platform.catch(() => this.platforms.delete(machine));
288
+ this.platforms.set(machine, platform);
289
+ return platform;
258
290
  }
259
291
  }
@@ -76,3 +76,34 @@ describe('PipeDiscipline', () => {
76
76
  expect(new PipeDiscipline().output('a\nb\r\n')).toBe('a\r\nb\r\n');
77
77
  });
78
78
  });
79
+
80
+ describe('TerminalUtil.tail', () => {
81
+ it('keeps the end, from the start of a line', () => {
82
+ expect(TerminalUtil.tail('abc', 10)).toBe('abc');
83
+ expect(TerminalUtil.tail('one\r\n\x1b[31mtwo\r\nthree', 12)).toBe('three');
84
+ expect(TerminalUtil.tail('abcdefgh', 3)).toBe('fgh');
85
+ });
86
+ });
87
+
88
+ describe('TerminalUtil.UTF8_LOCALE', () => {
89
+ const ctype = (env: Record<string, string>) =>
90
+ new TextDecoder()
91
+ .decode(
92
+ Bun.spawnSync(['/bin/sh', '-c', `${TerminalUtil.UTF8_LOCALE}; printf '%s|%s' "$LC_CTYPE" "$LC_ALL"`], {
93
+ env: { PATH: process.env.PATH ?? '/usr/bin:/bin', ...env },
94
+ }).stdout,
95
+ )
96
+ .split('|');
97
+
98
+ it('gives a shell without a UTF-8 locale one for its characters', () => {
99
+ const [lcCtype, lcAll] = ctype({ LANG: 'C' });
100
+ expect(lcCtype).toMatch(/utf-?8$/i);
101
+ expect(lcAll).toBe('');
102
+ expect(ctype({ LC_ALL: 'POSIX' }).every(value => /utf-?8$/i.test(value))).toBe(true);
103
+ });
104
+
105
+ it('leaves a UTF-8 locale as it is', () => {
106
+ expect(ctype({ LANG: 'zh_CN.UTF-8' })).toEqual(['', '']);
107
+ expect(ctype({ LC_CTYPE: 'en_US.utf8' })).toEqual(['en_US.utf8', '']);
108
+ });
109
+ });
@@ -29,6 +29,35 @@ const TTY_SEARCH_CHARS = 4096;
29
29
  * shell runs on pipes, and {@link PipeDiscipline} does what the terminal would.
30
30
  */
31
31
  export namespace TerminalUtil {
32
+ /** A full reset of the terminal (RIS): its screen, scrollback and modes. */
33
+ export const RESET = '\x1bc';
34
+
35
+ /**
36
+ * At most `max` characters of the end of `text`, from the start of a line when it is cut, so that it
37
+ * does not start inside a control sequence or a character.
38
+ */
39
+ export function tail(text: string, max: number): string {
40
+ if (text.length <= max) {
41
+ return text;
42
+ }
43
+ const cut = text.slice(-max);
44
+ const line = cut.indexOf('\n');
45
+ return line < 0 ? cut : cut.slice(line + 1);
46
+ }
47
+
48
+ /**
49
+ * A shell started by ssh or by an app of the desktop often has no locale (`C`): it then reads
50
+ * Chinese, typed or pasted, as bytes it cannot show (`<ffffffff>`), and programs print `?`. When
51
+ * the locale is not UTF-8, the shell gets one for its characters (LC_CTYPE, and LC_ALL when set):
52
+ * C.UTF-8 where the system has it, else en_US.UTF-8; its messages and formats stay as they were.
53
+ */
54
+ export const UTF8_LOCALE = [
55
+ 'case "${LC_ALL:-${LC_CTYPE:-$LANG}}" in *[Uu][Tt][Ff]-8*|*[Uu][Tt][Ff]8*) ;; *)',
56
+ "ta_utf8=$(locale -a 2>/dev/null | grep -i -x -m 1 -E 'c\\.utf-?8|en_us\\.utf-?8'); ta_utf8=${ta_utf8:-en_US.UTF-8};",
57
+ '[ -n "$LC_ALL" ] && export LC_ALL="$ta_utf8"; export LC_CTYPE="$ta_utf8"; unset ta_utf8;;',
58
+ 'esac',
59
+ ].join(' ');
60
+
32
61
  /** The command that tells the platform: three lines. */
33
62
  export const PROBE = 'uname -s; command -v script; printf %s "$SHELL"';
34
63
 
@@ -47,8 +76,10 @@ export namespace TerminalUtil {
47
76
  const boot = [
48
77
  // macOS's bash 3.2 otherwise asks to switch to zsh on each start.
49
78
  'export TERM=xterm-256color BASH_SILENCE_DEPRECATION_WARNING=1',
50
- `printf '\\033]1337;ta-tty=%s\\007' "$(tty)"`,
79
+ UTF8_LOCALE,
80
+ // Sized before it says its name: a resize that waited for the name comes after this size.
51
81
  `stty cols ${cols} rows ${rows} 2>/dev/null`,
82
+ `printf '\\033]1337;ta-tty=%s\\007' "$(tty)"`,
52
83
  `exec ${quote(platform.shell)} -i`,
53
84
  ].join('; ');
54
85
  let script: string | undefined;
@@ -12,6 +12,8 @@ export interface TaWorkspace {
12
12
  listDirectory(cwd: string, path: string): Promise<TaDirEntry[]>;
13
13
  readFile(cwd: string, path: string): Promise<TaFileContent>;
14
14
  writeFile(cwd: string, path: string, text: string): Promise<void>;
15
+ /** Bytes to a file, the folders on its way made; with `append`, after what it holds. */
16
+ uploadFile(cwd: string, path: string, data: Uint8Array, append: boolean): Promise<void>;
15
17
  findFiles(cwd: string, query: string, limit?: number): Promise<string[]>;
16
18
  runCommand(cwd: string, command: string, args: string[], options?: TaRunCommandOptions): Promise<TaCommandOutput>;
17
19
  }
@@ -3,6 +3,7 @@ import { Rpc } from '@celljs/rpc';
3
3
  import { RpcContext } from '@celljs/rpc/lib/node';
4
4
  import { ExecEnvironmentFactory } from '@celljs/ta-exec';
5
5
  import {
6
+ TA_UPLOAD_PART_BYTES,
6
7
  type TaCommandOutput,
7
8
  type TaDirEntry,
8
9
  type TaFileContent,
@@ -10,6 +11,7 @@ import {
10
11
  type TaRunCommandOptions,
11
12
  type TaTerminalInfo,
12
13
  TaTerminalServer,
14
+ type TaUploadOptions,
13
15
  TaWorkspaceServer,
14
16
  } from '@celljs/ta-protocol';
15
17
  import { TaErrors } from './ta-errors';
@@ -40,6 +42,22 @@ export class TaWorkspaceServerImpl implements TaWorkspaceServer {
40
42
  return TaErrors.guard(async () => this.workspace.writeFile(await this.cwd(cwd), path, text));
41
43
  }
42
44
 
45
+ uploadFile(path: string, data: string, options: TaUploadOptions = {}): Promise<void> {
46
+ return TaErrors.guard(async () => {
47
+ if (typeof data !== 'string') {
48
+ throw TaErrors.invalidArgument('No bytes to write.', 'Pass the bytes of the file in base64.');
49
+ }
50
+ const bytes = Buffer.from(data, 'base64');
51
+ if (bytes.length > TA_UPLOAD_PART_BYTES) {
52
+ throw TaErrors.invalidArgument(
53
+ 'The part is too large.',
54
+ `Send the file in parts of at most ${TA_UPLOAD_PART_BYTES} bytes, each after the first with append.`,
55
+ );
56
+ }
57
+ await this.workspace.uploadFile(await this.cwd(options.cwd), path, bytes, options.append === true);
58
+ });
59
+ }
60
+
43
61
  fileUrl(path: string, cwd?: string): Promise<string> {
44
62
  return TaErrors.guard(async () => this.files.url(await this.cwd(cwd), path));
45
63
  }
@@ -1,6 +1,6 @@
1
1
  import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
2
2
  import { execSync } from 'node:child_process';
3
- import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
3
+ import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { join } from 'node:path';
6
6
  import { LocalExecEnvironment } from '@celljs/ta-exec';
@@ -77,6 +77,16 @@ describe('DefaultTaWorkspace', () => {
77
77
  await expect(workspace().writeFile(repo, '../secret.txt', 'a')).rejects.toThrow('outside');
78
78
  });
79
79
 
80
+ it('uploads bytes in parts, making the folders on the way', async () => {
81
+ await workspace().uploadFile(root, 'up/load/a.bin', new Uint8Array([0, 1, 2]), false);
82
+ await workspace().uploadFile(root, 'up/load/a.bin', new Uint8Array([3]), true);
83
+ expect([...readFileSync(join(root, 'up/load/a.bin'))]).toEqual([0, 1, 2, 3]);
84
+ await workspace().uploadFile(root, 'up/load/a.bin', new Uint8Array([9]), false);
85
+ expect([...readFileSync(join(root, 'up/load/a.bin'))]).toEqual([9]);
86
+ await expect(workspace().uploadFile(root, 'up', new Uint8Array([1]), false)).rejects.toThrow('is a directory');
87
+ await expect(workspace().uploadFile(root, '../out.bin', new Uint8Array([1]), false)).rejects.toThrow('outside');
88
+ });
89
+
80
90
  it('reads the start of a large file', async () => {
81
91
  write(join(root, 'big/large.txt'), 'x'.repeat(1024 * 1024 + 10));
82
92
  const content = await workspace().readFile(join(root, 'big'), 'large.txt');
@@ -112,6 +112,18 @@ export class DefaultTaWorkspace implements TaWorkspace {
112
112
  await env.writeFile(target.absolute, text);
113
113
  }
114
114
 
115
+ async uploadFile(cwd: string, path: string, data: Uint8Array, append: boolean): Promise<void> {
116
+ const env = this.environments.create(cwd);
117
+ const target = WorkspaceUtil.inside(env.cwd, path);
118
+ if (!target.relative || (await env.stat(target.absolute))?.isDirectory) {
119
+ throw TaErrors.invalidArgument(`${path || '.'} is a directory.`, 'Pass the path of a file inside the workspace.');
120
+ }
121
+ if (!append) {
122
+ await env.mkdir(dirname(target.absolute));
123
+ }
124
+ await env.writeFile(target.absolute, data, { append });
125
+ }
126
+
115
127
  async findFiles(cwd: string, query: string, limit = DEFAULT_FIND_LIMIT): Promise<string[]> {
116
128
  const env = this.environments.create(cwd);
117
129
  return WorkspaceUtil.fuzzyFind(await this.listFiles(env), query, Math.max(1, limit));