offsite 0.0.3 → 0.0.5

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 (118) hide show
  1. package/README.md +20 -8
  2. package/apps/server/dist/main.mjs +5772 -0
  3. package/apps/server/dist/main.mjs.map +1 -0
  4. package/apps/web/dist/apple-touch-icon.png +0 -0
  5. package/apps/web/dist/assets/{angular-html-ji4UO7hp.js → angular-html-B53hPuXT.js} +1 -1
  6. package/apps/web/dist/assets/{angular-ts-CkobLhDa.js → angular-ts--nhprFvo.js} +1 -1
  7. package/apps/web/dist/assets/{apl-DAjPJTs4.js → apl-BMZ6gSxk.js} +1 -1
  8. package/apps/web/dist/assets/{astro-DlwA-zmc.js → astro-BTM15MDF.js} +1 -1
  9. package/apps/web/dist/assets/{blade-wTlHdbgB.js → blade-Dr8Nx-1X.js} +1 -1
  10. package/apps/web/dist/assets/{c-HyCIpwKL.js → c-CYWvPMKX.js} +1 -1
  11. package/apps/web/dist/assets/{chapel-SOYLpFSa.js → chapel-C8sB6Foh.js} +1 -1
  12. package/apps/web/dist/assets/{cobol-DlvLD04V.js → cobol-FnuKKfgs.js} +1 -1
  13. package/apps/web/dist/assets/{coffee-CGp5oJTY.js → coffee-DJrh6kIB.js} +1 -1
  14. package/apps/web/dist/assets/{cpp-CzmH13Nm.js → cpp-BLAvrTU1.js} +1 -1
  15. package/apps/web/dist/assets/{crystal-zS14MbR_.js → crystal-BthZctDJ.js} +1 -1
  16. package/apps/web/dist/assets/{css-DQEQn59R.js → css-DjklwMwW.js} +1 -1
  17. package/apps/web/dist/assets/{edge-Bkv0KzZU.js → edge-BunAX5o-.js} +1 -1
  18. package/apps/web/dist/assets/{elixir-B8tse0js.js → elixir-DgeksfSS.js} +1 -1
  19. package/apps/web/dist/assets/{elm-F2InAxd7.js → elm-l1eWnD1P.js} +1 -1
  20. package/apps/web/dist/assets/{erb-CAIamwOm.js → erb-JKFWiSFc.js} +1 -1
  21. package/apps/web/dist/assets/{git-rebase-U0z47rUi.js → git-rebase-AsNqLj-a.js} +1 -1
  22. package/apps/web/dist/assets/{glimmer-js-Bg_zPoN-.js → glimmer-js-B8iwahHO.js} +1 -1
  23. package/apps/web/dist/assets/{glimmer-ts-v3suqAPH.js → glimmer-ts-7zfF4XN6.js} +1 -1
  24. package/apps/web/dist/assets/{glsl-8io-Pcmd.js → glsl-BJka2Ayq.js} +1 -1
  25. package/apps/web/dist/assets/{graphql-B_q9cV-C.js → graphql-7lx4uUDH.js} +1 -1
  26. package/apps/web/dist/assets/{hack-DosfNWDX.js → hack-Jm3cv9jc.js} +1 -1
  27. package/apps/web/dist/assets/{haml-DcBtkGn5.js → haml-Csl_BhoG.js} +1 -1
  28. package/apps/web/dist/assets/{handlebars-2F0KxPFu.js → handlebars-CFp1rmy8.js} +1 -1
  29. package/apps/web/dist/assets/{html-DrWZZyp3.js → html-D_zffrV4.js} +1 -1
  30. package/apps/web/dist/assets/{html-derivative-BaV8vDp-.js → html-derivative-B-pqZuPW.js} +1 -1
  31. package/apps/web/dist/assets/{http-Dwo7FxJA.js → http-CU_emOoa.js} +1 -1
  32. package/apps/web/dist/assets/{hurl-9r6SjmMP.js → hurl-BRBJ1eQP.js} +1 -1
  33. package/apps/web/dist/assets/index-0B-Gdd7b.css +2 -0
  34. package/apps/web/dist/assets/index-CJuLsqhP.js +1148 -0
  35. package/apps/web/dist/assets/{java-CAkt7S2h.js → java-DWuVSV9c.js} +1 -1
  36. package/apps/web/dist/assets/{javascript-CMo8GEjU.js → javascript-CPq3YNXl.js} +1 -1
  37. package/apps/web/dist/assets/{jinja-XhRt7Tzv.js → jinja-Cwr4gaQT.js} +1 -1
  38. package/apps/web/dist/assets/{jison-BXZQEb4C.js → jison-tIc7kJPc.js} +1 -1
  39. package/apps/web/dist/assets/{json-SyJDQmRp.js → json-CuLUmV8d.js} +1 -1
  40. package/apps/web/dist/assets/{jsx-B76-zKzH.js → jsx-c0vioNM-.js} +1 -1
  41. package/apps/web/dist/assets/{julia-Ca5lbuMA.js → julia-DL1_3nCX.js} +1 -1
  42. package/apps/web/dist/assets/{just-CDt2RojJ.js → just-Dz1Oq8ln.js} +1 -1
  43. package/apps/web/dist/assets/{latex-D9bybvWo.js → latex-BtJjjmHB.js} +1 -1
  44. package/apps/web/dist/assets/{liquid-DMp-tByG.js → liquid-CUxfxikC.js} +1 -1
  45. package/apps/web/dist/assets/{lua-zNnfh_4o.js → lua-FURW1z3g.js} +1 -1
  46. package/apps/web/dist/assets/{marko-Ce5B11L0.js → marko-D33gqJht.js} +1 -1
  47. package/apps/web/dist/assets/{mdc-Btapt9E8.js → mdc-rJL-euOm.js} +1 -1
  48. package/apps/web/dist/assets/{nginx-Bzjverzh.js → nginx-CChB1oIE.js} +1 -1
  49. package/apps/web/dist/assets/{nim-D6zpNhUX.js → nim-C3VcT8nQ.js} +1 -1
  50. package/apps/web/dist/assets/{org-DTy-FvFD.js → org-CX9ySstM.js} +1 -1
  51. package/apps/web/dist/assets/{perl-BV8Dxusy.js → perl-BmdWqdcq.js} +1 -1
  52. package/apps/web/dist/assets/{php-BjkIa26S.js → php-CMtd7vHw.js} +1 -1
  53. package/apps/web/dist/assets/{pug-DdfmTHk6.js → pug-DRlx8d5w.js} +1 -1
  54. package/apps/web/dist/assets/{qml-BWLYImhB.js → qml-WYwqwy0P.js} +1 -1
  55. package/apps/web/dist/assets/{r-CKc9klse.js → r-C1yAXCUm.js} +1 -1
  56. package/apps/web/dist/assets/{razor-YcF2a59Y.js → razor-RKu7WkDv.js} +1 -1
  57. package/apps/web/dist/assets/{regexp-D3esVJ1X.js → regexp-DuLU1P7q.js} +1 -1
  58. package/apps/web/dist/assets/{rst-DVaj4AQD.js → rst-CmK0AAXk.js} +1 -1
  59. package/apps/web/dist/assets/{ruby-DBJQEVev.js → ruby-DwgaoIUu.js} +1 -1
  60. package/apps/web/dist/assets/{sas-NSuLkGD1.js → sas-iTrXuHXc.js} +1 -1
  61. package/apps/web/dist/assets/{scss-TMy8m-so.js → scss-CEysq-zA.js} +1 -1
  62. package/apps/web/dist/assets/{shellscript-BVuIsFkY.js → shellscript-B0mTpBTm.js} +1 -1
  63. package/apps/web/dist/assets/{shellsession-CV7MHIu6.js → shellsession-BuzD9EVx.js} +1 -1
  64. package/apps/web/dist/assets/{soy-DDY0EoKE.js → soy-ClDsFuFk.js} +1 -1
  65. package/apps/web/dist/assets/{sql-CoZqqtiv.js → sql-Dc1h3orO.js} +1 -1
  66. package/apps/web/dist/assets/{stata-B8C1noIb.js → stata-l31dxrtC.js} +1 -1
  67. package/apps/web/dist/assets/{surrealql-BYiUJt-V.js → surrealql-x6LM7pkl.js} +1 -1
  68. package/apps/web/dist/assets/{svelte-DGeZ8dPs.js → svelte-CtkYFYuK.js} +1 -1
  69. package/apps/web/dist/assets/{templ-Dn21SE_C.js → templ-DHMinkR-.js} +1 -1
  70. package/apps/web/dist/assets/{tex-Da9GBCsM.js → tex-CXbWoQMY.js} +1 -1
  71. package/apps/web/dist/assets/{ts-tags-BuL1T6K3.js → ts-tags-BikYq-3m.js} +1 -1
  72. package/apps/web/dist/assets/{tsx-DiLCnfgl.js → tsx-zHJnYG9T.js} +1 -1
  73. package/apps/web/dist/assets/{twig-Cx1peiH-.js → twig-DKCfiSr9.js} +1 -1
  74. package/apps/web/dist/assets/{typescript-pVxvHJ4R.js → typescript-K9TDgxRD.js} +1 -1
  75. package/apps/web/dist/assets/{typst-CJQbitzj.js → typst-BpXkFpDR.js} +1 -1
  76. package/apps/web/dist/assets/{vue-CXKyVnbB.js → vue-BYYRFXPM.js} +1 -1
  77. package/apps/web/dist/assets/{vue-html-AQjnBMTh.js → vue-html-BR347tzO.js} +1 -1
  78. package/apps/web/dist/assets/{vue-vine-DYxt9AbK.js → vue-vine-Jv4OHYgW.js} +1 -1
  79. package/apps/web/dist/assets/{xml-CEJ01uJ1.js → xml-DuYGU-7M.js} +1 -1
  80. package/apps/web/dist/assets/{xsl-CULFBy6v.js → xsl-B950Zbyt.js} +1 -1
  81. package/apps/web/dist/assets/{yaml-BNf68xM_.js → yaml-XFYTn8dR.js} +1 -1
  82. package/apps/web/dist/icon-192.png +0 -0
  83. package/apps/web/dist/icon-512.png +0 -0
  84. package/apps/web/dist/icon-maskable-512.png +0 -0
  85. package/apps/web/dist/icon.svg +5 -0
  86. package/apps/web/dist/index.html +4 -2
  87. package/apps/web/dist/sw.js +1 -81
  88. package/bin/offsite.js +1 -1
  89. package/package.json +5 -1
  90. package/apps/server/dist/args.js +0 -11
  91. package/apps/server/dist/claude.js +0 -245
  92. package/apps/server/dist/config.js +0 -191
  93. package/apps/server/dist/content.js +0 -172
  94. package/apps/server/dist/db.js +0 -13
  95. package/apps/server/dist/files.js +0 -50
  96. package/apps/server/dist/git.js +0 -133
  97. package/apps/server/dist/history.js +0 -137
  98. package/apps/server/dist/hub.js +0 -10
  99. package/apps/server/dist/main.js +0 -70
  100. package/apps/server/dist/projects.js +0 -92
  101. package/apps/server/dist/protocol.js +0 -20
  102. package/apps/server/dist/push.js +0 -152
  103. package/apps/server/dist/run.js +0 -375
  104. package/apps/server/dist/runs.js +0 -102
  105. package/apps/server/dist/schemes.js +0 -262
  106. package/apps/server/dist/sessions.js +0 -99
  107. package/apps/server/dist/socket.js +0 -193
  108. package/apps/server/dist/storage.js +0 -71
  109. package/apps/server/dist/terminal/alerts.js +0 -145
  110. package/apps/server/dist/terminal/process.js +0 -207
  111. package/apps/server/dist/terminal/shell-check.js +0 -91
  112. package/apps/server/dist/terminal/shell-integration.js +0 -112
  113. package/apps/server/dist/terminal/terminal.js +0 -402
  114. package/apps/web/dist/assets/index-CIBt6t2M.css +0 -2
  115. package/apps/web/dist/assets/index-CPpMMYyZ.js +0 -902
  116. /package/apps/server/src/{terminal → services/terminal}/shell-integration/bash/offsite.bash +0 -0
  117. /package/apps/server/src/{terminal → services/terminal}/shell-integration/fish/offsite.fish +0 -0
  118. /package/apps/server/src/{terminal → services/terminal}/shell-integration/zsh/.zshenv +0 -0
@@ -1,375 +0,0 @@
1
- import { randomUUID } from 'node:crypto';
2
- import { query, } from '@anthropic-ai/claude-agent-sdk';
3
- import { claudeOptions, recordLimit } from './claude.js';
4
- import { toContext, toMessage } from './content.js';
5
- import { broadcast, publish } from './hub.js';
6
- const IDLE_LIMIT = 10 * 60 * 1000;
7
- const imageTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'];
8
- export const runs = new Map();
9
- export function activity() {
10
- const result = {};
11
- for (const run of runs.values())
12
- result[run.sessionId] = {
13
- running: run.running,
14
- waiting: run.pending.size > 0,
15
- };
16
- return result;
17
- }
18
- export function broadcastActivity() {
19
- broadcast({ type: 'activity', activity: activity() });
20
- }
21
- export class Run {
22
- sessionId;
23
- messages = [];
24
- pending = new Map();
25
- query;
26
- closed;
27
- resumeAt;
28
- queue = [];
29
- settings;
30
- context = null;
31
- running = false;
32
- inbox = [];
33
- wake = null;
34
- idleTimer;
35
- stopping = false;
36
- constructor(options) {
37
- this.sessionId = options.sessionId;
38
- this.resumeAt = options.resumeAt;
39
- this.settings = options.settings;
40
- const settings = options.settings;
41
- this.query = query({
42
- prompt: this.input(),
43
- options: {
44
- ...claudeOptions(settings.accountId, options.cwd),
45
- ...(options.resume
46
- ? { resume: options.sessionId }
47
- : { sessionId: options.sessionId }),
48
- ...(options.resumeAt === null
49
- ? {}
50
- : { resumeSessionAt: options.resumeAt }),
51
- enableFileCheckpointing: true,
52
- ...(settings.permissionMode === null
53
- ? {}
54
- : { permissionMode: settings.permissionMode }),
55
- ...(settings.model === null ? {} : { model: settings.model }),
56
- ...(settings.effort === null ? {} : { effort: settings.effort }),
57
- allowDangerouslySkipPermissions: true,
58
- includePartialMessages: true,
59
- canUseTool: (toolName, input, context) => this.ask({
60
- toolName,
61
- input,
62
- title: context.title ?? null,
63
- suggestions: context.suggestions ?? [],
64
- signal: context.signal,
65
- }),
66
- },
67
- });
68
- runs.set(this.sessionId, this);
69
- this.closed = this.read();
70
- }
71
- submit(prompt, delivery) {
72
- if (!this.running) {
73
- this.push(prompt, undefined);
74
- return;
75
- }
76
- switch (delivery) {
77
- case 'steer':
78
- this.push(prompt, 'next');
79
- return;
80
- case 'queue':
81
- this.queue = [...this.queue, { id: randomUUID(), prompt }];
82
- this.publishQueue();
83
- }
84
- }
85
- unqueue(id) {
86
- this.queue = this.queue.filter((item) => item.id !== id);
87
- this.publishQueue();
88
- }
89
- async configure(settings) {
90
- if (settings.model !== this.settings.model)
91
- await this.query.setModel(settings.model ?? undefined);
92
- if (settings.effort !== this.settings.effort)
93
- await this.query.applyFlagSettings({ effortLevel: settings.effort });
94
- if (settings.permissionMode !== null &&
95
- settings.permissionMode !== this.settings.permissionMode)
96
- await this.query.setPermissionMode(settings.permissionMode);
97
- this.updateSettings({
98
- ...settings,
99
- accountId: this.settings.accountId,
100
- permissionMode: settings.permissionMode ?? this.settings.permissionMode,
101
- });
102
- }
103
- respond(requestId, decision, answers) {
104
- const pending = this.pending.get(requestId);
105
- if (pending === undefined)
106
- return;
107
- const input = pending.request.input;
108
- switch (decision) {
109
- case 'allow':
110
- this.settle(requestId, {
111
- behavior: 'allow',
112
- updatedInput: answers === null ? input : { ...input, answers },
113
- });
114
- return;
115
- case 'always':
116
- this.settle(requestId, {
117
- behavior: 'allow',
118
- updatedInput: input,
119
- updatedPermissions: pending.suggestions,
120
- });
121
- return;
122
- case 'deny':
123
- this.settle(requestId, {
124
- behavior: 'deny',
125
- message: 'The user rejected this action',
126
- interrupt: true,
127
- });
128
- }
129
- }
130
- stop() {
131
- this.stopping = true;
132
- runs.delete(this.sessionId);
133
- this.query.close();
134
- }
135
- push(prompt, priority) {
136
- clearTimeout(this.idleTimer);
137
- for (const image of prompt.images)
138
- if (!imageTypes.includes(image.mediaType))
139
- throw new Error(`${image.mediaType} images are not supported`);
140
- const uuid = randomUUID();
141
- this.inbox.push({
142
- type: 'user',
143
- message: {
144
- role: 'user',
145
- content: [
146
- ...prompt.images.map((image) => ({
147
- type: 'image',
148
- source: {
149
- type: 'base64',
150
- media_type: image.mediaType,
151
- data: image.data,
152
- },
153
- })),
154
- ...(prompt.text === ''
155
- ? []
156
- : [{ type: 'text', text: prompt.text }]),
157
- ],
158
- },
159
- parent_tool_use_id: null,
160
- uuid,
161
- ...(priority === undefined ? {} : { priority }),
162
- });
163
- this.wake?.();
164
- this.add({
165
- type: 'user',
166
- uuid,
167
- time: Date.now(),
168
- content: [
169
- ...prompt.images.map((image) => ({ type: 'image', ...image })),
170
- ...(prompt.text === ''
171
- ? []
172
- : [{ type: 'text', text: prompt.text }]),
173
- ],
174
- parent: null,
175
- });
176
- this.running = true;
177
- broadcastActivity();
178
- }
179
- async refreshContext() {
180
- this.context = toContext(await this.query.getContextUsage());
181
- publish(this.sessionId, {
182
- type: 'context',
183
- sessionId: this.sessionId,
184
- context: this.context,
185
- });
186
- }
187
- async *input() {
188
- while (true) {
189
- const next = this.inbox.shift();
190
- if (next !== undefined) {
191
- yield next;
192
- continue;
193
- }
194
- await new Promise((resolve) => {
195
- this.wake = resolve;
196
- });
197
- }
198
- }
199
- async read() {
200
- try {
201
- for await (const message of this.query)
202
- this.receive(message);
203
- }
204
- catch (error) {
205
- if (!this.stopping)
206
- this.add(notice(error instanceof Error ? error.message : String(error)));
207
- }
208
- clearTimeout(this.idleTimer);
209
- for (const id of this.pending.keys())
210
- this.settle(id, { behavior: 'deny', message: 'Session ended' });
211
- if (runs.get(this.sessionId) === this)
212
- runs.delete(this.sessionId);
213
- this.running = false;
214
- broadcastActivity();
215
- }
216
- receive(message) {
217
- switch (message.type) {
218
- case 'assistant':
219
- this.add(toMessage('assistant', message, Date.now()));
220
- if (message.error === 'rate_limit')
221
- this.add(notice('This account hit its usage limit. Pick another account below and send again.'));
222
- return;
223
- case 'user':
224
- if (message.isSynthetic)
225
- return;
226
- this.add(toMessage('user', { ...message, uuid: message.uuid ?? randomUUID() }, Date.now()));
227
- return;
228
- case 'stream_event':
229
- if (message.parent_tool_use_id === null &&
230
- message.event.type === 'content_block_delta' &&
231
- message.event.delta.type === 'text_delta')
232
- publish(this.sessionId, {
233
- type: 'delta',
234
- sessionId: this.sessionId,
235
- text: message.event.delta.text,
236
- });
237
- return;
238
- case 'rate_limit_event': {
239
- const accountId = this.settings.accountId;
240
- if (accountId === null)
241
- throw new Error('Rate limit event for a run without an account');
242
- broadcast({
243
- type: 'limits',
244
- accountId,
245
- limits: recordLimit(accountId, message.rate_limit_info),
246
- });
247
- return;
248
- }
249
- case 'result':
250
- if (message.subtype !== 'success')
251
- this.add(notice(message.errors.join('\n') || message.subtype));
252
- this.finishTurn();
253
- this.refreshContext().catch((error) => console.error(error));
254
- return;
255
- case 'system':
256
- switch (message.subtype) {
257
- case 'init':
258
- this.updateSettings({
259
- ...this.settings,
260
- permissionMode: message.permissionMode,
261
- });
262
- return;
263
- case 'status':
264
- if (message.permissionMode !== undefined)
265
- this.updateSettings({
266
- ...this.settings,
267
- permissionMode: message.permissionMode,
268
- });
269
- return;
270
- case 'compact_boundary':
271
- this.add({
272
- type: 'notice',
273
- uuid: message.uuid,
274
- time: Date.now(),
275
- text: 'Conversation compacted',
276
- isError: false,
277
- });
278
- return;
279
- case 'local_command_output':
280
- this.add({
281
- type: 'notice',
282
- uuid: message.uuid,
283
- time: Date.now(),
284
- text: message.content,
285
- isError: false,
286
- });
287
- }
288
- }
289
- }
290
- finishTurn() {
291
- const [next, ...rest] = this.queue;
292
- if (next !== undefined) {
293
- this.queue = rest;
294
- this.publishQueue();
295
- this.push(next.prompt, undefined);
296
- return;
297
- }
298
- this.running = false;
299
- broadcastActivity();
300
- this.idleTimer = setTimeout(() => {
301
- if (!this.running && this.pending.size === 0)
302
- this.stop();
303
- }, IDLE_LIMIT);
304
- }
305
- ask(options) {
306
- const request = {
307
- id: randomUUID(),
308
- toolName: options.toolName,
309
- input: options.input,
310
- title: options.title,
311
- canAlwaysAllow: options.suggestions.length > 0,
312
- };
313
- return new Promise((resolve) => {
314
- this.pending.set(request.id, {
315
- request,
316
- suggestions: options.suggestions,
317
- resolve,
318
- });
319
- publish(this.sessionId, {
320
- type: 'request',
321
- sessionId: this.sessionId,
322
- request,
323
- });
324
- broadcastActivity();
325
- options.signal.addEventListener('abort', () => this.settle(request.id, { behavior: 'deny', message: 'Cancelled' }));
326
- });
327
- }
328
- settle(requestId, result) {
329
- const pending = this.pending.get(requestId);
330
- if (pending === undefined)
331
- return;
332
- this.pending.delete(requestId);
333
- pending.resolve(result);
334
- publish(this.sessionId, {
335
- type: 'requestDone',
336
- sessionId: this.sessionId,
337
- requestId,
338
- });
339
- broadcastActivity();
340
- }
341
- add(message) {
342
- if (this.messages.some((item) => item.uuid === message.uuid))
343
- return;
344
- this.messages.push(message);
345
- publish(this.sessionId, {
346
- type: 'message',
347
- sessionId: this.sessionId,
348
- message,
349
- });
350
- }
351
- updateSettings(settings) {
352
- this.settings = settings;
353
- publish(this.sessionId, {
354
- type: 'settings',
355
- sessionId: this.sessionId,
356
- settings,
357
- });
358
- }
359
- publishQueue() {
360
- publish(this.sessionId, {
361
- type: 'queue',
362
- sessionId: this.sessionId,
363
- queue: this.queue,
364
- });
365
- }
366
- }
367
- function notice(text) {
368
- return {
369
- type: 'notice',
370
- uuid: randomUUID(),
371
- time: Date.now(),
372
- text,
373
- isError: true,
374
- };
375
- }
@@ -1,102 +0,0 @@
1
- import { randomUUID } from 'node:crypto';
2
- import { statSync } from 'node:fs';
3
- import { getSessionMessages } from '@anthropic-ai/claude-agent-sdk';
4
- import { history } from './history.js';
5
- import { publish } from './hub.js';
6
- import { Run, runs } from './run.js';
7
- export function stopAll() {
8
- for (const run of runs.values())
9
- run.stop();
10
- }
11
- export async function rewind(request) {
12
- const sessionId = request.sessionId;
13
- if (runs.get(sessionId)?.running === true)
14
- throw new Error('Stop Claude before rewinding');
15
- const run = runFor(sessionId, request.cwd, request.settings);
16
- let result = null;
17
- if (request.code) {
18
- const files = await run.query.rewindFiles(request.messageId, {
19
- dryRun: request.dryRun,
20
- });
21
- if (!files.canRewind)
22
- throw new Error(files.error ?? 'The files cannot be restored');
23
- result = {
24
- files: files.filesChanged ?? [],
25
- insertions: files.insertions ?? 0,
26
- deletions: files.deletions ?? 0,
27
- };
28
- }
29
- if (request.dryRun || !request.conversation)
30
- return result;
31
- const disk = await getSessionMessages(sessionId, { dir: request.cwd });
32
- const index = disk.findIndex((item) => item.uuid === request.messageId);
33
- if (index < 1)
34
- throw new Error('Cannot rewind the conversation to that point');
35
- run.stop();
36
- await run.closed;
37
- new Run({
38
- sessionId,
39
- cwd: request.cwd,
40
- resume: true,
41
- resumeAt: disk[index - 1].uuid,
42
- settings: request.settings,
43
- });
44
- publish(sessionId, await history(sessionId, request.cwd));
45
- return result;
46
- }
47
- export async function open(client, sessionId, cwd) {
48
- client.opening = sessionId;
49
- const event = await history(sessionId, cwd);
50
- if (client.opening !== sessionId)
51
- return;
52
- client.sessionId = sessionId;
53
- client.send(event);
54
- }
55
- export async function send(client, request) {
56
- if (!statSync(request.cwd, { throwIfNoEntry: false })?.isDirectory())
57
- throw new Error(`${request.cwd} is not a folder`);
58
- if (request.sessionId === null) {
59
- const sessionId = randomUUID();
60
- const run = new Run({
61
- sessionId,
62
- cwd: request.cwd,
63
- resume: false,
64
- resumeAt: null,
65
- settings: request.settings,
66
- });
67
- client.opening = sessionId;
68
- client.sessionId = sessionId;
69
- client.send({ type: 'started', sessionId, cwd: request.cwd });
70
- run.submit(request.prompt, request.delivery);
71
- return;
72
- }
73
- const existing = runs.get(request.sessionId);
74
- if (existing !== undefined &&
75
- !existing.running &&
76
- (await changedElsewhere(existing, request.cwd)))
77
- existing.stop();
78
- const run = runFor(request.sessionId, request.cwd, request.settings);
79
- if (run === existing)
80
- await run.configure(request.settings);
81
- else
82
- await open(client, request.sessionId, request.cwd);
83
- run.submit(request.prompt, request.delivery);
84
- }
85
- function runFor(sessionId, cwd, settings) {
86
- const run = runs.get(sessionId);
87
- if (run !== undefined && run.settings.accountId === settings.accountId)
88
- return run;
89
- if (run !== undefined) {
90
- if (run.running)
91
- throw new Error('Wait for this turn to finish before switching accounts');
92
- run.stop();
93
- }
94
- return new Run({ sessionId, cwd, resume: true, resumeAt: null, settings });
95
- }
96
- async function changedElsewhere(run, cwd) {
97
- if (run.messages.length === 0)
98
- return false;
99
- const history = await getSessionMessages(run.sessionId, { dir: cwd });
100
- const last = history.at(-1)?.uuid;
101
- return last !== undefined && !run.messages.some((item) => item.uuid === last);
102
- }