@celljs/ta-host 4.9.0 → 4.10.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 +1 -1
- package/README.md +1 -1
- package/esm/node/event-log.d.ts +2 -1
- package/esm/node/event-log.js +3 -2
- package/esm/node/exec-environment-factory.d.ts +7 -0
- package/esm/node/exec-environment-factory.js +30 -7
- package/esm/node/host-protocol.d.ts +14 -5
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/remote/remote-hosts.d.ts +1 -3
- package/esm/node/remote/remote-hosts.js +0 -15
- package/esm/node/remote/remote-protocol.d.ts +0 -2
- package/esm/node/remote/remote-server-impl.js +1 -1
- package/esm/node/remote-ui-context.d.ts +1 -1
- package/esm/node/remote-ui-context.js +3 -1
- package/esm/node/session-catalog.d.ts +1 -1
- package/esm/node/session-catalog.js +8 -3
- package/esm/node/session-handles.d.ts +32 -0
- package/esm/node/session-handles.js +86 -0
- package/esm/node/session-info-util.d.ts +6 -0
- package/esm/node/session-info-util.js +34 -2
- package/esm/node/sign-in/sign-in-pages.js +7 -1
- package/esm/node/ta-access.d.ts +11 -0
- package/esm/node/ta-access.js +29 -3
- package/esm/node/ta-errors.js +17 -2
- package/esm/node/ta-event-util.d.ts +12 -2
- package/esm/node/ta-event-util.js +71 -3
- package/esm/node/ta-server-impl.d.ts +0 -2
- package/esm/node/ta-server-impl.js +11 -10
- package/esm/node/ta-session-factory.d.ts +10 -1
- package/esm/node/ta-session-factory.js +22 -2
- package/esm/node/ta-session-registry.d.ts +12 -1
- package/esm/node/ta-session-registry.js +71 -20
- package/esm/node/ta-session.d.ts +9 -5
- package/esm/node/ta-session.js +18 -10
- package/esm/node/workspace-files.d.ts +15 -5
- package/esm/node/workspace-files.js +22 -15
- package/esm/node/workspace-protocol.d.ts +1 -1
- package/esm/node/workspace-util.d.ts +2 -0
- package/esm/node/workspace-util.js +8 -3
- package/lib/node/event-log.d.ts +2 -1
- package/lib/node/event-log.js +3 -2
- package/lib/node/exec-environment-factory.d.ts +7 -0
- package/lib/node/exec-environment-factory.js +30 -7
- package/lib/node/host-protocol.d.ts +14 -5
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/remote/remote-hosts.d.ts +1 -3
- package/lib/node/remote/remote-hosts.js +0 -15
- package/lib/node/remote/remote-protocol.d.ts +0 -2
- package/lib/node/remote/remote-server-impl.js +1 -1
- package/lib/node/remote-ui-context.d.ts +1 -1
- package/lib/node/remote-ui-context.js +3 -1
- package/lib/node/session-catalog.d.ts +1 -1
- package/lib/node/session-catalog.js +8 -3
- package/lib/node/session-handles.d.ts +33 -0
- package/lib/node/session-handles.js +89 -0
- package/lib/node/session-info-util.d.ts +6 -0
- package/lib/node/session-info-util.js +33 -1
- package/lib/node/sign-in/sign-in-pages.js +7 -1
- package/lib/node/ta-access.d.ts +11 -0
- package/lib/node/ta-access.js +29 -3
- package/lib/node/ta-errors.js +17 -2
- package/lib/node/ta-event-util.d.ts +12 -2
- package/lib/node/ta-event-util.js +71 -3
- package/lib/node/ta-server-impl.d.ts +0 -2
- package/lib/node/ta-server-impl.js +11 -10
- package/lib/node/ta-session-factory.d.ts +10 -1
- package/lib/node/ta-session-factory.js +23 -3
- package/lib/node/ta-session-registry.d.ts +12 -1
- package/lib/node/ta-session-registry.js +71 -20
- package/lib/node/ta-session.d.ts +9 -5
- package/lib/node/ta-session.js +18 -10
- package/lib/node/workspace-files.d.ts +15 -5
- package/lib/node/workspace-files.js +22 -15
- package/lib/node/workspace-protocol.d.ts +1 -1
- package/lib/node/workspace-util.d.ts +2 -0
- package/lib/node/workspace-util.js +8 -3
- package/package.json +12 -12
|
@@ -10,10 +10,16 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.DefaultTaSessionRegistry = void 0;
|
|
13
|
+
const node_fs_1 = require("node:fs");
|
|
13
14
|
const core_1 = require("@celljs/core");
|
|
14
15
|
const client_channel_1 = require("./client-channel");
|
|
15
16
|
const host_protocol_1 = require("./host-protocol");
|
|
17
|
+
const session_handles_1 = require("./session-handles");
|
|
16
18
|
const ta_errors_1 = require("./ta-errors");
|
|
19
|
+
/**
|
|
20
|
+
* The open sessions. A session's id outlives its memory: closed while idle or by a restart, it opens again
|
|
21
|
+
* from its file under the same id at the next call (TaSessionHandles), so clients never see it go.
|
|
22
|
+
*/
|
|
17
23
|
let DefaultTaSessionRegistry = class DefaultTaSessionRegistry {
|
|
18
24
|
constructor() {
|
|
19
25
|
this.sessions = new Map();
|
|
@@ -22,25 +28,17 @@ let DefaultTaSessionRegistry = class DefaultTaSessionRegistry {
|
|
|
22
28
|
this.opening = 0;
|
|
23
29
|
/** The closes under way, by session file: out of the list already, their files locked until they end. */
|
|
24
30
|
this.closing = new Map();
|
|
31
|
+
/** Sessions opening again under their ids: the calls that come meanwhile wait for the same. */
|
|
32
|
+
this.waking = new Map();
|
|
25
33
|
}
|
|
26
|
-
|
|
27
|
-
// A
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (this.sessions.size + this.opening >= this.options.maxSessions) {
|
|
33
|
-
throw ta_errors_1.TaErrors.tooManySessions(this.options.maxSessions);
|
|
34
|
-
}
|
|
35
|
-
this.opening++;
|
|
36
|
-
try {
|
|
37
|
-
const session = await this.factory.create(options, ended => void this.close(ended.id));
|
|
38
|
-
this.sessions.set(session.id, session);
|
|
39
|
-
return session;
|
|
40
|
-
}
|
|
41
|
-
finally {
|
|
42
|
-
this.opening--;
|
|
34
|
+
open(options) {
|
|
35
|
+
// A file that had a session keeps its id, which pages may still have.
|
|
36
|
+
const known = options.sessionFile ? this.handles.idOf(options.sessionFile) : undefined;
|
|
37
|
+
const waking = known && this.waking.get(known);
|
|
38
|
+
if (waking) {
|
|
39
|
+
return waking;
|
|
43
40
|
}
|
|
41
|
+
return this.create(options, known && !this.sessions.has(known) ? known : undefined);
|
|
44
42
|
}
|
|
45
43
|
require(sessionId) {
|
|
46
44
|
const session = this.sessions.get(sessionId);
|
|
@@ -49,17 +47,42 @@ let DefaultTaSessionRegistry = class DefaultTaSessionRegistry {
|
|
|
49
47
|
}
|
|
50
48
|
return session;
|
|
51
49
|
}
|
|
50
|
+
resolve(sessionId) {
|
|
51
|
+
const session = this.sessions.get(sessionId);
|
|
52
|
+
if (session) {
|
|
53
|
+
return Promise.resolve(session);
|
|
54
|
+
}
|
|
55
|
+
const waking = this.waking.get(sessionId);
|
|
56
|
+
if (waking) {
|
|
57
|
+
return waking;
|
|
58
|
+
}
|
|
59
|
+
const handle = this.handles.get(sessionId);
|
|
60
|
+
if (!handle || !(0, node_fs_1.existsSync)(handle.sessionFile)) {
|
|
61
|
+
return Promise.reject(ta_errors_1.TaErrors.sessionNotFound(sessionId));
|
|
62
|
+
}
|
|
63
|
+
const options = { cwd: handle.cwd, sessionFile: handle.sessionFile, clientTools: handle.clientTools };
|
|
64
|
+
const opening = this.create(options, sessionId).finally(() => this.waking.delete(sessionId));
|
|
65
|
+
this.waking.set(sessionId, opening);
|
|
66
|
+
return opening;
|
|
67
|
+
}
|
|
52
68
|
list() {
|
|
53
69
|
return [...this.sessions.values()];
|
|
54
70
|
}
|
|
55
|
-
async close(sessionId) {
|
|
71
|
+
async close(sessionId, idle = false) {
|
|
72
|
+
if (!idle) {
|
|
73
|
+
this.handles.delete(sessionId);
|
|
74
|
+
}
|
|
56
75
|
const session = this.sessions.get(sessionId);
|
|
57
76
|
if (!session) {
|
|
58
77
|
return;
|
|
59
78
|
}
|
|
60
79
|
this.sessions.delete(sessionId);
|
|
61
80
|
const file = session.pi.sessionFile;
|
|
62
|
-
|
|
81
|
+
if (idle && file) {
|
|
82
|
+
// The conversation it has now: pi may have moved to another file since it opened.
|
|
83
|
+
this.handles.set(sessionId, { ...this.handles.get(sessionId), sessionFile: file, cwd: session.cwd });
|
|
84
|
+
}
|
|
85
|
+
const closing = session.close(!idle);
|
|
63
86
|
if (file) {
|
|
64
87
|
this.closing.set(file, closing);
|
|
65
88
|
}
|
|
@@ -73,7 +96,7 @@ let DefaultTaSessionRegistry = class DefaultTaSessionRegistry {
|
|
|
73
96
|
}
|
|
74
97
|
}
|
|
75
98
|
async closeAll() {
|
|
76
|
-
await Promise.all([...this.sessions.keys()].map(id => this.close(id)));
|
|
99
|
+
await Promise.all([...this.sessions.keys()].map(id => this.close(id, true)));
|
|
77
100
|
}
|
|
78
101
|
/** The one channel of a connection; it leaves every session when the connection ends. */
|
|
79
102
|
channel(connection) {
|
|
@@ -90,6 +113,30 @@ let DefaultTaSessionRegistry = class DefaultTaSessionRegistry {
|
|
|
90
113
|
}
|
|
91
114
|
return channel;
|
|
92
115
|
}
|
|
116
|
+
/** `id`: the id it had, to open it again under. */
|
|
117
|
+
async create(options, id) {
|
|
118
|
+
// A conversation closed a moment ago (idle, or by another client) opens once it is closed, not refused.
|
|
119
|
+
const closing = options.sessionFile && this.closing.get(options.sessionFile);
|
|
120
|
+
if (closing) {
|
|
121
|
+
await closing.catch(() => undefined);
|
|
122
|
+
}
|
|
123
|
+
if (this.sessions.size + this.opening >= this.options.maxSessions) {
|
|
124
|
+
throw ta_errors_1.TaErrors.tooManySessions(this.options.maxSessions);
|
|
125
|
+
}
|
|
126
|
+
this.opening++;
|
|
127
|
+
try {
|
|
128
|
+
const session = await this.factory.create(options, (ended, idle) => void this.close(ended.id, idle), id);
|
|
129
|
+
this.sessions.set(session.id, session);
|
|
130
|
+
const sessionFile = session.pi.sessionFile;
|
|
131
|
+
if (sessionFile) {
|
|
132
|
+
this.handles.set(session.id, { sessionFile, cwd: session.cwd, clientTools: options.clientTools });
|
|
133
|
+
}
|
|
134
|
+
return session;
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
this.opening--;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
93
140
|
};
|
|
94
141
|
exports.DefaultTaSessionRegistry = DefaultTaSessionRegistry;
|
|
95
142
|
__decorate([
|
|
@@ -100,6 +147,10 @@ __decorate([
|
|
|
100
147
|
(0, core_1.Autowired)(host_protocol_1.TaSessionFactory),
|
|
101
148
|
__metadata("design:type", Object)
|
|
102
149
|
], DefaultTaSessionRegistry.prototype, "factory", void 0);
|
|
150
|
+
__decorate([
|
|
151
|
+
(0, core_1.Autowired)(session_handles_1.TaSessionHandles),
|
|
152
|
+
__metadata("design:type", Object)
|
|
153
|
+
], DefaultTaSessionRegistry.prototype, "handles", void 0);
|
|
103
154
|
exports.DefaultTaSessionRegistry = DefaultTaSessionRegistry = __decorate([
|
|
104
155
|
(0, core_1.Component)(host_protocol_1.TaSessionRegistry)
|
|
105
156
|
], DefaultTaSessionRegistry);
|
package/lib/node/ta-session.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SessionLock } from '@celljs/ta-harness';
|
|
2
|
-
import type { RpcExtensionUIResponse, TaAttachOptions, TaAttachResult, TaClientToolResult, TaEvent, TaMessagePage, TaNavigateResult, TaPromptInput, TaPromptReceipt, TaSessionInfo } from '@celljs/ta-protocol';
|
|
2
|
+
import type { RpcExtensionUIResponse, TaAttachOptions, TaAttachResult, TaClientToolResult, TaConversation, TaEvent, TaMessagePage, TaNavigateResult, TaPromptInput, TaPromptReceipt, TaSessionInfo } from '@celljs/ta-protocol';
|
|
3
3
|
import type { ClientChannel } from './client-channel';
|
|
4
4
|
import type { ClientToolBroker } from './client-tool-broker';
|
|
5
5
|
import { EventLog } from './event-log';
|
|
@@ -16,8 +16,10 @@ export interface TaSessionContext {
|
|
|
16
16
|
/** The tools of the session that its clients run. */
|
|
17
17
|
clientTools: ClientToolBroker;
|
|
18
18
|
options: TaHostOptions;
|
|
19
|
-
/** The session should end: it has been idle, or an extension asked. */
|
|
20
|
-
requestClose(session: TaSession): void;
|
|
19
|
+
/** The session should end: it has been idle (`idle`: it opens again under its id when called), or an extension asked. */
|
|
20
|
+
requestClose(session: TaSession, idle?: boolean): void;
|
|
21
|
+
/** The id it had, when it opens again (TaSessionHandles); default: a new one. */
|
|
22
|
+
id?: string;
|
|
21
23
|
/** TaOpenSessionOptions.group. */
|
|
22
24
|
group?: string;
|
|
23
25
|
/** TaOpenSessionOptions.appendSystemPrompt: kept in the session file, for when it opens again from it. */
|
|
@@ -31,7 +33,7 @@ export interface TaSessionContext {
|
|
|
31
33
|
*/
|
|
32
34
|
export declare class TaSession {
|
|
33
35
|
protected readonly context: TaSessionContext;
|
|
34
|
-
readonly id:
|
|
36
|
+
readonly id: string;
|
|
35
37
|
readonly createdAt: number;
|
|
36
38
|
readonly ui: RemoteUiContext;
|
|
37
39
|
readonly runtime: AgentSessionRuntime;
|
|
@@ -50,6 +52,8 @@ export declare class TaSession {
|
|
|
50
52
|
info(): TaSessionInfo;
|
|
51
53
|
attach(channel: ClientChannel, afterSeq?: number, options?: TaAttachOptions): TaAttachResult;
|
|
52
54
|
earlierMessages(before: number | undefined, timestamp: number | undefined, budget: number): TaMessagePage;
|
|
55
|
+
/** The conversation of the events sent, as clients get it. */
|
|
56
|
+
protected conversation(): TaConversation;
|
|
53
57
|
detach(channel: ClientChannel): void;
|
|
54
58
|
respondUi(response: RpcExtensionUIResponse): boolean;
|
|
55
59
|
/** Only a client attached runs a call: one that left would hold it for nobody. */
|
|
@@ -73,7 +77,7 @@ export declare class TaSession {
|
|
|
73
77
|
* (a tool that ignores the abort, a stream that hangs) or an extension slow to shut down is given
|
|
74
78
|
* `closeTimeoutMs`; then pi is cut off from its agent, so that nothing more goes into the file.
|
|
75
79
|
*/
|
|
76
|
-
close(): Promise<void>;
|
|
80
|
+
close(announce?: boolean): Promise<void>;
|
|
77
81
|
/** False when `work` takes longer than closeTimeoutMs. */
|
|
78
82
|
protected within(work: Promise<void>): Promise<boolean>;
|
|
79
83
|
protected accept(input: TaPromptInput, run: () => Promise<PromptDisposition>): Promise<TaPromptReceipt>;
|
package/lib/node/ta-session.js
CHANGED
|
@@ -16,15 +16,18 @@ const ta_event_util_1 = require("./ta-event-util");
|
|
|
16
16
|
*/
|
|
17
17
|
class TaSession {
|
|
18
18
|
constructor(context) {
|
|
19
|
+
var _a;
|
|
19
20
|
this.context = context;
|
|
20
|
-
this.id = (0, node_crypto_1.randomUUID)();
|
|
21
21
|
this.createdAt = Date.now();
|
|
22
22
|
this.receipts = new receipt_book_1.ReceiptBook();
|
|
23
23
|
this.channels = new Set();
|
|
24
24
|
this.live = new live_run_1.LiveRun();
|
|
25
25
|
this.started = false;
|
|
26
26
|
this.runtime = context.runtime;
|
|
27
|
-
this.
|
|
27
|
+
this.id = (_a = context.id) !== null && _a !== void 0 ? _a : (0, node_crypto_1.randomUUID)();
|
|
28
|
+
// Opened again: its seqs start past any the session gave before (fewer than one per millisecond), so a
|
|
29
|
+
// client that attaches with one of those gets the conversation again rather than events of another log.
|
|
30
|
+
this.log = new event_log_1.EventLog(this.id, context.options.eventBufferBytes, context.id ? Date.now() : 0);
|
|
28
31
|
this.ui = new remote_ui_context_1.RemoteUiContext(event => this.publish(event));
|
|
29
32
|
context.clientTools.connect(event => this.publish(event));
|
|
30
33
|
}
|
|
@@ -52,6 +55,7 @@ class TaSession {
|
|
|
52
55
|
sessionFile: pi.sessionFile,
|
|
53
56
|
sessionName: pi.sessionName,
|
|
54
57
|
firstMessage: session_info_util_1.SessionInfoUtil.firstMessage(pi.messages),
|
|
58
|
+
lastMessage: session_info_util_1.SessionInfoUtil.lastMessage(pi.messages),
|
|
55
59
|
model: model ? `${model.provider}/${model.id}` : undefined,
|
|
56
60
|
isStreaming: pi.isStreaming,
|
|
57
61
|
messageCount: pi.messages.length,
|
|
@@ -68,9 +72,7 @@ class TaSession {
|
|
|
68
72
|
return {
|
|
69
73
|
info: this.info(),
|
|
70
74
|
events,
|
|
71
|
-
conversation: afterSeq === undefined || gap
|
|
72
|
-
? message_page_1.MessagePage.recent(this.live.conversation(this.pi.messages, this.pi.isStreaming), options.recentBytes)
|
|
73
|
-
: undefined,
|
|
75
|
+
conversation: afterSeq === undefined || gap ? message_page_1.MessagePage.recent(this.conversation(), options.recentBytes) : undefined,
|
|
74
76
|
pendingUi: this.ui.pendingRequests,
|
|
75
77
|
shownUi: this.ui.shownRequests,
|
|
76
78
|
pendingClientTools: this.context.clientTools.pendingRequests,
|
|
@@ -79,7 +81,11 @@ class TaSession {
|
|
|
79
81
|
};
|
|
80
82
|
}
|
|
81
83
|
earlierMessages(before, timestamp, budget) {
|
|
82
|
-
return message_page_1.MessagePage.before(this.
|
|
84
|
+
return message_page_1.MessagePage.before(this.conversation().messages, before, timestamp, budget);
|
|
85
|
+
}
|
|
86
|
+
/** The conversation of the events sent, as clients get it. */
|
|
87
|
+
conversation() {
|
|
88
|
+
return ta_event_util_1.TaEventUtil.leanConversation(this.live.conversation(this.pi.messages, this.pi.isStreaming));
|
|
83
89
|
}
|
|
84
90
|
detach(channel) {
|
|
85
91
|
if (this.channels.delete(channel)) {
|
|
@@ -159,12 +165,14 @@ class TaSession {
|
|
|
159
165
|
* (a tool that ignores the abort, a stream that hangs) or an extension slow to shut down is given
|
|
160
166
|
* `closeTimeoutMs`; then pi is cut off from its agent, so that nothing more goes into the file.
|
|
161
167
|
*/
|
|
162
|
-
async close() {
|
|
168
|
+
async close(announce = true) {
|
|
163
169
|
var _a;
|
|
164
170
|
clearTimeout(this.idleTimer);
|
|
165
171
|
this.ui.cancelAll();
|
|
166
172
|
this.context.clientTools.cancelAll();
|
|
167
|
-
|
|
173
|
+
if (announce) {
|
|
174
|
+
this.publish({ type: 'session_closed' });
|
|
175
|
+
}
|
|
168
176
|
this.channels.clear();
|
|
169
177
|
(_a = this.unsubscribe) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
170
178
|
try {
|
|
@@ -200,7 +208,7 @@ class TaSession {
|
|
|
200
208
|
}
|
|
201
209
|
try {
|
|
202
210
|
const disposition = await run();
|
|
203
|
-
return { clientMessageId: input.clientMessageId, status: disposition === '
|
|
211
|
+
return { clientMessageId: input.clientMessageId, status: disposition === 'started' ? 'accepted' : disposition };
|
|
204
212
|
}
|
|
205
213
|
catch (error) {
|
|
206
214
|
this.receipts.forget(input.clientMessageId);
|
|
@@ -258,7 +266,7 @@ class TaSession {
|
|
|
258
266
|
clearTimeout(this.idleTimer);
|
|
259
267
|
this.idleTimer = undefined;
|
|
260
268
|
if (this.isIdle()) {
|
|
261
|
-
this.idleTimer = setTimeout(() => this.isIdle() && this.context.requestClose(this), this.context.options.idleTimeoutMs);
|
|
269
|
+
this.idleTimer = setTimeout(() => this.isIdle() && this.context.requestClose(this, true), this.context.options.idleTimeoutMs);
|
|
262
270
|
this.idleTimer.unref();
|
|
263
271
|
}
|
|
264
272
|
}
|
|
@@ -23,20 +23,30 @@ export declare namespace WorkspaceFilesUtil {
|
|
|
23
23
|
/**
|
|
24
24
|
* Answers `GET /api/files/<id>/<path>` with the bytes of a file, without the token of ta serve: `<id>`
|
|
25
25
|
* stands for one directory, and only who has the token learns it (TaWorkspaceServer.fileUrl). Paths stay inside
|
|
26
|
-
* that directory; a directory answers its `index.html`. One range is served, so that videos seek.
|
|
26
|
+
* that directory; a directory answers its `index.html`. One range is served, so that videos seek. A file
|
|
27
|
+
* outside the directory of the conversation (a picture an agent left in `/tmp`) has the directory it is in
|
|
28
|
+
* as its own: who has the token runs programs there anyway, and the pages of the workspace still cannot
|
|
29
|
+
* reach it.
|
|
27
30
|
*/
|
|
28
31
|
export declare class DefaultWorkspaceFiles implements WorkspaceFiles, Middleware {
|
|
29
32
|
/** Before TaAccess, which turns away `/api` requests without the token. */
|
|
30
33
|
readonly priority: number;
|
|
31
34
|
protected readonly environments: ExecEnvironmentFactory;
|
|
32
35
|
protected readonly host?: string;
|
|
33
|
-
|
|
36
|
+
/** By id, the directory paths stay in, and the one whose execution environment reads them. */
|
|
37
|
+
protected readonly directories: Map<string, {
|
|
38
|
+
root: string;
|
|
39
|
+
cwd: string;
|
|
40
|
+
}>;
|
|
34
41
|
protected readonly ids: Map<string, string>;
|
|
35
42
|
url(cwd: string, path: string): string;
|
|
36
43
|
handle(ctx: Context, next: () => Promise<void>): Promise<void>;
|
|
37
|
-
protected send(request: IncomingMessage, response: ServerResponse,
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
protected send(request: IncomingMessage, response: ServerResponse, directory: {
|
|
45
|
+
root: string;
|
|
46
|
+
cwd: string;
|
|
47
|
+
}, path: string): Promise<void>;
|
|
48
|
+
/** The file at `path` of `root`, or the `index.html` of the directory there. */
|
|
49
|
+
protected find(env: ExecEnvironment, root: string, path: string): Promise<{
|
|
40
50
|
absolute: string;
|
|
41
51
|
stat: FileStat;
|
|
42
52
|
} | undefined>;
|
|
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.DefaultWorkspaceFiles = exports.WorkspaceFilesUtil = void 0;
|
|
13
13
|
const node_crypto_1 = require("node:crypto");
|
|
14
14
|
const node_fs_1 = require("node:fs");
|
|
15
|
+
const node_path_1 = require("node:path");
|
|
15
16
|
const core_1 = require("@celljs/core");
|
|
16
17
|
const ta_exec_1 = require("@celljs/ta-exec");
|
|
17
18
|
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
@@ -120,24 +121,30 @@ var WorkspaceFilesUtil;
|
|
|
120
121
|
/**
|
|
121
122
|
* Answers `GET /api/files/<id>/<path>` with the bytes of a file, without the token of ta serve: `<id>`
|
|
122
123
|
* stands for one directory, and only who has the token learns it (TaWorkspaceServer.fileUrl). Paths stay inside
|
|
123
|
-
* that directory; a directory answers its `index.html`. One range is served, so that videos seek.
|
|
124
|
+
* that directory; a directory answers its `index.html`. One range is served, so that videos seek. A file
|
|
125
|
+
* outside the directory of the conversation (a picture an agent left in `/tmp`) has the directory it is in
|
|
126
|
+
* as its own: who has the token runs programs there anyway, and the pages of the workspace still cannot
|
|
127
|
+
* reach it.
|
|
124
128
|
*/
|
|
125
129
|
let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
|
|
126
130
|
constructor() {
|
|
127
131
|
/** Before TaAccess, which turns away `/api` requests without the token. */
|
|
128
132
|
this.priority = ta_access_1.ACCESS_MIDDLEWARE_PRIORITY + 1;
|
|
133
|
+
/** By id, the directory paths stay in, and the one whose execution environment reads them. */
|
|
129
134
|
this.directories = new Map();
|
|
130
135
|
this.ids = new Map();
|
|
131
136
|
}
|
|
132
137
|
url(cwd, path) {
|
|
133
|
-
const
|
|
134
|
-
|
|
138
|
+
const absolute = (0, node_path_1.resolve)(cwd, path || '.');
|
|
139
|
+
const root = workspace_util_1.WorkspaceUtil.contains(cwd, absolute) ? cwd : (0, node_path_1.dirname)(absolute);
|
|
140
|
+
const key = `${cwd}\0${root}`;
|
|
141
|
+
let id = this.ids.get(key);
|
|
135
142
|
if (!id) {
|
|
136
143
|
id = (0, node_crypto_1.randomBytes)(ID_BYTES).toString('base64url');
|
|
137
|
-
this.ids.set(
|
|
138
|
-
this.directories.set(id, cwd);
|
|
144
|
+
this.ids.set(key, id);
|
|
145
|
+
this.directories.set(id, { root, cwd });
|
|
139
146
|
}
|
|
140
|
-
return WorkspaceFilesUtil.url(id, relative);
|
|
147
|
+
return WorkspaceFilesUtil.url(id, workspace_util_1.WorkspaceUtil.inside(root, absolute).relative);
|
|
141
148
|
}
|
|
142
149
|
async handle(ctx, next) {
|
|
143
150
|
const request = ctx.request;
|
|
@@ -147,8 +154,8 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
|
|
|
147
154
|
return;
|
|
148
155
|
}
|
|
149
156
|
const response = ctx.response;
|
|
150
|
-
const
|
|
151
|
-
if (!
|
|
157
|
+
const directory = this.directories.get(target.id);
|
|
158
|
+
if (!directory || !this.acceptsHost(request)) {
|
|
152
159
|
this.fail(response, 404);
|
|
153
160
|
return;
|
|
154
161
|
}
|
|
@@ -157,11 +164,11 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
|
|
|
157
164
|
this.fail(response, 405);
|
|
158
165
|
return;
|
|
159
166
|
}
|
|
160
|
-
await this.send(request, response,
|
|
167
|
+
await this.send(request, response, directory, target.path);
|
|
161
168
|
}
|
|
162
|
-
async send(request, response,
|
|
163
|
-
const env = this.environments.create(cwd);
|
|
164
|
-
const file = await this.find(env, path);
|
|
169
|
+
async send(request, response, directory, path) {
|
|
170
|
+
const env = this.environments.create(directory.cwd);
|
|
171
|
+
const file = await this.find(env, directory.root, path);
|
|
165
172
|
if (!file) {
|
|
166
173
|
this.fail(response, 404);
|
|
167
174
|
return;
|
|
@@ -208,11 +215,11 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
|
|
|
208
215
|
response.end(await this.read(env, absolute, stat.size, start, end));
|
|
209
216
|
}
|
|
210
217
|
}
|
|
211
|
-
/** The file at `path`, or the `index.html` of the directory there. */
|
|
212
|
-
async find(env, path) {
|
|
218
|
+
/** The file at `path` of `root`, or the `index.html` of the directory there. */
|
|
219
|
+
async find(env, root, path) {
|
|
213
220
|
let absolute;
|
|
214
221
|
try {
|
|
215
|
-
absolute = workspace_util_1.WorkspaceUtil.inside(
|
|
222
|
+
absolute = workspace_util_1.WorkspaceUtil.inside(root, path).absolute;
|
|
216
223
|
}
|
|
217
224
|
catch (_a) {
|
|
218
225
|
return undefined;
|
|
@@ -17,7 +17,7 @@ export interface TaWorkspace {
|
|
|
17
17
|
}
|
|
18
18
|
/** The bytes of workspace files over plain HTTP, for the page to show them as they are (TaWorkspaceServer.fileUrl). */
|
|
19
19
|
export interface WorkspaceFiles {
|
|
20
|
-
/** The URL of `path` of `cwd`;
|
|
20
|
+
/** The URL of `path` of `cwd`; a file outside `cwd` is served from the directory it is in. */
|
|
21
21
|
url(cwd: string, path: string): string;
|
|
22
22
|
}
|
|
23
23
|
//# sourceMappingURL=workspace-protocol.d.ts.map
|
|
@@ -8,6 +8,8 @@ export declare namespace WorkspaceUtil {
|
|
|
8
8
|
absolute: string;
|
|
9
9
|
relative: string;
|
|
10
10
|
};
|
|
11
|
+
/** Whether `absolute` is `cwd` or inside it. */
|
|
12
|
+
function contains(cwd: string, absolute: string): boolean;
|
|
11
13
|
/** Joins a relative path of a directory with a name, with `/`. */
|
|
12
14
|
function join(dir: string, name: string): string;
|
|
13
15
|
/** Directories first, then by name as people sort (`file2` before `file10`). */
|
|
@@ -12,13 +12,18 @@ var WorkspaceUtil;
|
|
|
12
12
|
/** `path` inside `cwd`, absolute, and relative with `/`; throws when it leaves `cwd`. */
|
|
13
13
|
function inside(cwd, path) {
|
|
14
14
|
const absolute = (0, node_path_1.resolve)(cwd, path || '.');
|
|
15
|
-
|
|
16
|
-
if (rel === '..' || rel.startsWith(`..${node_path_1.sep}`) || (0, node_path_1.isAbsolute)(rel)) {
|
|
15
|
+
if (!contains(cwd, absolute)) {
|
|
17
16
|
throw ta_errors_1.TaErrors.invalidArgument(`The path ${path} is outside of ${cwd}.`, 'Pass a path inside the directory of the workspace.');
|
|
18
17
|
}
|
|
19
|
-
return { absolute, relative:
|
|
18
|
+
return { absolute, relative: (0, node_path_1.relative)(cwd, absolute).split(node_path_1.sep).join('/') };
|
|
20
19
|
}
|
|
21
20
|
WorkspaceUtil.inside = inside;
|
|
21
|
+
/** Whether `absolute` is `cwd` or inside it. */
|
|
22
|
+
function contains(cwd, absolute) {
|
|
23
|
+
const rel = (0, node_path_1.relative)(cwd, absolute);
|
|
24
|
+
return rel !== '..' && !rel.startsWith(`..${node_path_1.sep}`) && !(0, node_path_1.isAbsolute)(rel);
|
|
25
|
+
}
|
|
26
|
+
WorkspaceUtil.contains = contains;
|
|
22
27
|
/** Joins a relative path of a directory with a name, with `/`. */
|
|
23
28
|
function join(dir, name) {
|
|
24
29
|
return dir && dir !== '.' ? node_path_1.posix.join(dir, name) : name;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-host",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.10.0",
|
|
4
4
|
"description": "ta serve: pi sessions for many clients over WebSocket",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -34,19 +34,19 @@
|
|
|
34
34
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@celljs/ai-agent": "4.
|
|
38
|
-
"@celljs/ai-coding-agent": "4.
|
|
39
|
-
"@celljs/core": "4.
|
|
40
|
-
"@celljs/rpc": "4.
|
|
41
|
-
"@celljs/ta-exec": "4.
|
|
42
|
-
"@celljs/ta-harness": "4.
|
|
43
|
-
"@celljs/ta-protocol": "4.
|
|
44
|
-
"@celljs/ta-relay": "4.
|
|
45
|
-
"@celljs/web": "4.
|
|
46
|
-
"@earendil-works/pi-coding-agent": "^1.5.
|
|
37
|
+
"@celljs/ai-agent": "4.10.0",
|
|
38
|
+
"@celljs/ai-coding-agent": "4.10.0",
|
|
39
|
+
"@celljs/core": "4.10.0",
|
|
40
|
+
"@celljs/rpc": "4.10.0",
|
|
41
|
+
"@celljs/ta-exec": "4.10.0",
|
|
42
|
+
"@celljs/ta-harness": "4.10.0",
|
|
43
|
+
"@celljs/ta-protocol": "4.10.0",
|
|
44
|
+
"@celljs/ta-relay": "4.10.0",
|
|
45
|
+
"@celljs/web": "4.10.0",
|
|
46
|
+
"@earendil-works/pi-coding-agent": "^1.5.2"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@earendil-works/pi-ai": "^1.5.
|
|
49
|
+
"@earendil-works/pi-ai": "^1.5.2"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|