@meshagent/meshagent 0.29.2 → 0.30.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/CHANGELOG.md +12 -0
- package/dist/browser/agent-client.js +3 -28
- package/dist/browser/agent.js +6 -6
- package/dist/browser/containers-client.d.ts +125 -0
- package/dist/browser/containers-client.js +458 -0
- package/dist/browser/database-client.d.ts +42 -6
- package/dist/browser/database-client.js +610 -77
- package/dist/browser/developer-client.d.ts +2 -2
- package/dist/browser/developer-client.js +60 -15
- package/dist/browser/helpers.js +4 -3
- package/dist/browser/index.d.ts +1 -0
- package/dist/browser/index.js +1 -0
- package/dist/browser/lk-client.js +12 -3
- package/dist/browser/meshagent-client.d.ts +5 -0
- package/dist/browser/messaging-client.d.ts +1 -0
- package/dist/browser/messaging-client.js +52 -8
- package/dist/browser/queues-client.d.ts +2 -0
- package/dist/browser/queues-client.js +34 -7
- package/dist/browser/response.d.ts +28 -0
- package/dist/browser/response.js +76 -1
- package/dist/browser/room-client.d.ts +43 -1
- package/dist/browser/room-client.js +204 -0
- package/dist/browser/secrets-client.d.ts +1 -0
- package/dist/browser/secrets-client.js +32 -27
- package/dist/browser/storage-client.d.ts +22 -7
- package/dist/browser/storage-client.js +353 -15
- package/dist/browser/sync-client.d.ts +12 -13
- package/dist/browser/sync-client.js +263 -65
- package/dist/esm/agent-client.js +3 -28
- package/dist/esm/agent.js +6 -6
- package/dist/esm/containers-client.d.ts +125 -0
- package/dist/esm/containers-client.js +453 -0
- package/dist/esm/database-client.d.ts +42 -6
- package/dist/esm/database-client.js +611 -78
- package/dist/esm/developer-client.d.ts +2 -2
- package/dist/esm/developer-client.js +61 -16
- package/dist/esm/helpers.js +4 -3
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/lk-client.js +12 -3
- package/dist/esm/meshagent-client.d.ts +5 -0
- package/dist/esm/messaging-client.d.ts +1 -0
- package/dist/esm/messaging-client.js +52 -8
- package/dist/esm/queues-client.d.ts +2 -0
- package/dist/esm/queues-client.js +35 -8
- package/dist/esm/response.d.ts +28 -0
- package/dist/esm/response.js +73 -0
- package/dist/esm/room-client.d.ts +43 -1
- package/dist/esm/room-client.js +207 -3
- package/dist/esm/secrets-client.d.ts +1 -0
- package/dist/esm/secrets-client.js +33 -28
- package/dist/esm/storage-client.d.ts +22 -7
- package/dist/esm/storage-client.js +353 -15
- package/dist/esm/sync-client.d.ts +12 -13
- package/dist/esm/sync-client.js +263 -64
- package/dist/node/agent-client.js +3 -28
- package/dist/node/agent.js +6 -6
- package/dist/node/containers-client.d.ts +125 -0
- package/dist/node/containers-client.js +458 -0
- package/dist/node/database-client.d.ts +42 -6
- package/dist/node/database-client.js +610 -77
- package/dist/node/developer-client.d.ts +2 -2
- package/dist/node/developer-client.js +60 -15
- package/dist/node/helpers.js +4 -3
- package/dist/node/index.d.ts +1 -0
- package/dist/node/index.js +1 -0
- package/dist/node/lk-client.js +12 -3
- package/dist/node/meshagent-client.d.ts +5 -0
- package/dist/node/messaging-client.d.ts +1 -0
- package/dist/node/messaging-client.js +52 -8
- package/dist/node/queues-client.d.ts +2 -0
- package/dist/node/queues-client.js +34 -7
- package/dist/node/response.d.ts +28 -0
- package/dist/node/response.js +76 -1
- package/dist/node/room-client.d.ts +43 -1
- package/dist/node/room-client.js +204 -0
- package/dist/node/secrets-client.d.ts +1 -0
- package/dist/node/secrets-client.js +32 -27
- package/dist/node/storage-client.d.ts +22 -7
- package/dist/node/storage-client.js +353 -15
- package/dist/node/sync-client.d.ts +12 -13
- package/dist/node/sync-client.js +263 -65
- package/package.json +1 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SyncClient =
|
|
3
|
+
exports.SyncClient = void 0;
|
|
4
4
|
const event_emitter_1 = require("./event-emitter");
|
|
5
5
|
const schema_1 = require("./schema");
|
|
6
|
-
const stream_controller_1 = require("./stream-controller");
|
|
7
6
|
const room_server_client_1 = require("./room-server-client");
|
|
7
|
+
const response_1 = require("./response");
|
|
8
8
|
const utils_1 = require("./utils");
|
|
9
9
|
const runtime_1 = require("./runtime");
|
|
10
10
|
const completer_1 = require("./completer");
|
|
@@ -21,99 +21,251 @@ function normalizeSyncPath(path) {
|
|
|
21
21
|
}
|
|
22
22
|
return normalized;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
function parseSyncOpenStateChunkHeaders(headers) {
|
|
25
|
+
if (headers["kind"] !== "state" ||
|
|
26
|
+
typeof headers["path"] !== "string" ||
|
|
27
|
+
typeof headers["schema"] !== "object" ||
|
|
28
|
+
headers["schema"] == null) {
|
|
29
|
+
throw new room_server_client_1.RoomServerException("unexpected return type from sync.open");
|
|
29
30
|
}
|
|
31
|
+
return {
|
|
32
|
+
kind: "state",
|
|
33
|
+
path: headers["path"],
|
|
34
|
+
schema: headers["schema"],
|
|
35
|
+
};
|
|
30
36
|
}
|
|
31
|
-
|
|
37
|
+
function parseSyncOpenOutputChunkHeaders(headers) {
|
|
38
|
+
const kind = headers["kind"];
|
|
39
|
+
if ((kind !== "state" && kind !== "sync") ||
|
|
40
|
+
typeof headers["path"] !== "string") {
|
|
41
|
+
throw new room_server_client_1.RoomServerException("unexpected return type from sync.open");
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
kind,
|
|
45
|
+
path: headers["path"],
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
class SyncOpenStreamState {
|
|
49
|
+
constructor(params) {
|
|
50
|
+
this.params = params;
|
|
51
|
+
this._inputQueue = [];
|
|
52
|
+
this._inputWaiters = [];
|
|
53
|
+
this._inputClosed = false;
|
|
54
|
+
}
|
|
55
|
+
_enqueueChunk(chunk) {
|
|
56
|
+
const waiter = this._inputWaiters.shift();
|
|
57
|
+
if (waiter) {
|
|
58
|
+
waiter(chunk);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this._inputQueue.push(chunk);
|
|
62
|
+
}
|
|
63
|
+
_nextChunk() {
|
|
64
|
+
const queued = this._inputQueue.shift();
|
|
65
|
+
if (queued !== undefined) {
|
|
66
|
+
return Promise.resolve(queued);
|
|
67
|
+
}
|
|
68
|
+
return new Promise((resolve) => {
|
|
69
|
+
this._inputWaiters.push(resolve);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
async *inputStream() {
|
|
73
|
+
yield new response_1.BinaryContent({
|
|
74
|
+
data: new Uint8Array(),
|
|
75
|
+
headers: {
|
|
76
|
+
kind: "start",
|
|
77
|
+
path: this.params.path,
|
|
78
|
+
create: this.params.create,
|
|
79
|
+
vector: this.params.vector,
|
|
80
|
+
schema: this.params.schemaJson,
|
|
81
|
+
schema_path: this.params.schemaPath,
|
|
82
|
+
initial_json: this.params.initialJson,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
while (true) {
|
|
86
|
+
const chunk = await this._nextChunk();
|
|
87
|
+
if (chunk === SyncOpenStreamState.INPUT_STREAM_CLOSE) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
yield chunk;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
attachTask(task) {
|
|
94
|
+
this._task = task
|
|
95
|
+
.catch((error) => {
|
|
96
|
+
this._error = error;
|
|
97
|
+
throw error;
|
|
98
|
+
})
|
|
99
|
+
.finally(() => {
|
|
100
|
+
this.closeInputStream();
|
|
101
|
+
});
|
|
102
|
+
void this._task.catch(() => undefined);
|
|
103
|
+
}
|
|
104
|
+
closeInputStream() {
|
|
105
|
+
if (this._inputClosed) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
this._inputClosed = true;
|
|
109
|
+
this._enqueueChunk(SyncOpenStreamState.INPUT_STREAM_CLOSE);
|
|
110
|
+
}
|
|
111
|
+
queueSync(data) {
|
|
112
|
+
if (this._error instanceof Error) {
|
|
113
|
+
throw this._error;
|
|
114
|
+
}
|
|
115
|
+
if (this._error !== undefined) {
|
|
116
|
+
throw new room_server_client_1.RoomServerException(`sync stream failed: ${String(this._error)}`);
|
|
117
|
+
}
|
|
118
|
+
if (this._inputClosed) {
|
|
119
|
+
throw new room_server_client_1.RoomServerException("attempted to sync to a document that is not connected");
|
|
120
|
+
}
|
|
121
|
+
this._enqueueChunk(new response_1.BinaryContent({
|
|
122
|
+
data,
|
|
123
|
+
headers: { kind: "sync" },
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
async wait() {
|
|
127
|
+
await this._task;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
SyncOpenStreamState.INPUT_STREAM_CLOSE = Symbol("sync-open-input-close");
|
|
32
131
|
class SyncClient extends event_emitter_1.EventEmitter {
|
|
33
132
|
constructor({ room }) {
|
|
34
133
|
super();
|
|
35
134
|
this._connectingDocuments = {};
|
|
36
|
-
this._changesToSync = new stream_controller_1.StreamController();
|
|
37
135
|
this._connectedDocuments = {};
|
|
136
|
+
this._documentStreams = {};
|
|
38
137
|
this.client = room;
|
|
39
|
-
this.client.protocol.addHandler("room.sync", this._handleSync.bind(this));
|
|
40
138
|
this.client.protocol.addHandler("room.status", this._handleStatus.bind(this));
|
|
41
139
|
}
|
|
140
|
+
_unexpectedResponseError(operation) {
|
|
141
|
+
return new room_server_client_1.RoomServerException(`unexpected return type from sync.${operation}`);
|
|
142
|
+
}
|
|
143
|
+
async _invoke(operation, input) {
|
|
144
|
+
return await this.client.invoke({
|
|
145
|
+
toolkit: "sync",
|
|
146
|
+
tool: operation,
|
|
147
|
+
input,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
42
150
|
start({ onDone, onError } = {}) {
|
|
43
151
|
this.client.protocol.start({ onDone, onError });
|
|
44
|
-
(async () => {
|
|
45
|
-
for await (const message of this._changesToSync.stream) {
|
|
46
|
-
await this.client.sendRequest("room.sync", { path: message.path }, utils_1.encoder.encode(message.base64));
|
|
47
|
-
}
|
|
48
|
-
})();
|
|
49
152
|
}
|
|
50
153
|
dispose() {
|
|
51
154
|
super.dispose();
|
|
52
|
-
this.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const doc = rc.ref;
|
|
155
|
+
for (const streamState of Object.values(this._documentStreams)) {
|
|
156
|
+
streamState.closeInputStream();
|
|
157
|
+
}
|
|
158
|
+
this._documentStreams = {};
|
|
159
|
+
for (const rc of Object.values(this._connectedDocuments)) {
|
|
160
|
+
(0, runtime_1.unregisterDocument)(rc.ref.id);
|
|
161
|
+
}
|
|
162
|
+
this._connectedDocuments = {};
|
|
163
|
+
this._connectingDocuments = {};
|
|
164
|
+
}
|
|
165
|
+
_applySyncPayload(rc, payload) {
|
|
166
|
+
const doc = rc.ref;
|
|
167
|
+
if (payload.length > 0) {
|
|
66
168
|
const base64 = utils_1.decoder.decode(payload);
|
|
67
169
|
(0, runtime_1.applyBackendChanges)(doc.id, base64);
|
|
68
|
-
this.emit("synced", { type: "sync", doc });
|
|
69
|
-
if (!doc.isSynchronized) {
|
|
70
|
-
doc.setSynchronizedComplete();
|
|
71
|
-
}
|
|
72
170
|
}
|
|
73
|
-
|
|
74
|
-
|
|
171
|
+
this.emit("synced", { type: "sync", doc });
|
|
172
|
+
if (!doc.isSynchronized) {
|
|
173
|
+
doc.setSynchronizedComplete();
|
|
75
174
|
}
|
|
76
175
|
}
|
|
77
|
-
async _handleStatus(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
176
|
+
async _handleStatus(_protocol, _messageId, _type, bytes) {
|
|
177
|
+
if (!bytes) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const [header] = (0, utils_1.unpackMessage)(bytes);
|
|
181
|
+
this.emit("status", { type: "status", status: header.status });
|
|
81
182
|
}
|
|
82
183
|
async create(path, json) {
|
|
83
|
-
|
|
84
|
-
await this.
|
|
184
|
+
const normalizedPath = normalizeSyncPath(path);
|
|
185
|
+
await this._invoke("create", {
|
|
186
|
+
path: normalizedPath,
|
|
187
|
+
json: json ?? null,
|
|
188
|
+
schema: null,
|
|
189
|
+
schema_path: null,
|
|
190
|
+
});
|
|
85
191
|
}
|
|
86
|
-
async open(path, { create = true } = {}) {
|
|
192
|
+
async open(path, { create = true, initialJson, schema, } = {}) {
|
|
87
193
|
path = normalizeSyncPath(path);
|
|
88
194
|
const pending = this._connectingDocuments[path];
|
|
89
195
|
if (pending) {
|
|
90
196
|
await pending;
|
|
91
197
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return
|
|
198
|
+
const connected = this._connectedDocuments[path];
|
|
199
|
+
if (connected) {
|
|
200
|
+
connected.count++;
|
|
201
|
+
return connected.ref;
|
|
96
202
|
}
|
|
97
|
-
const
|
|
98
|
-
this._connectingDocuments[path] =
|
|
203
|
+
const connecting = new completer_1.Completer();
|
|
204
|
+
this._connectingDocuments[path] = connecting.fut;
|
|
205
|
+
let streamState;
|
|
206
|
+
let iterator;
|
|
99
207
|
try {
|
|
100
|
-
|
|
101
|
-
|
|
208
|
+
streamState = new SyncOpenStreamState({
|
|
209
|
+
path,
|
|
210
|
+
create,
|
|
211
|
+
vector: null,
|
|
212
|
+
schemaJson: schema == null ? null : schema.toJson(),
|
|
213
|
+
schemaPath: null,
|
|
214
|
+
initialJson: initialJson ?? null,
|
|
215
|
+
});
|
|
216
|
+
const responseStream = await this.client.invokeStream({
|
|
217
|
+
toolkit: "sync",
|
|
218
|
+
tool: "open",
|
|
219
|
+
input: streamState.inputStream(),
|
|
220
|
+
});
|
|
221
|
+
iterator = responseStream[Symbol.asyncIterator]();
|
|
222
|
+
const first = await iterator.next();
|
|
223
|
+
if (first.done || first.value === undefined) {
|
|
224
|
+
throw new room_server_client_1.RoomServerException("sync.open stream closed before the initial document state was returned");
|
|
225
|
+
}
|
|
226
|
+
const firstChunk = first.value;
|
|
227
|
+
if (firstChunk instanceof response_1.ErrorContent) {
|
|
228
|
+
throw new room_server_client_1.RoomServerException(firstChunk.text, firstChunk.code);
|
|
229
|
+
}
|
|
230
|
+
if (!(firstChunk instanceof response_1.BinaryContent)) {
|
|
231
|
+
throw this._unexpectedResponseError("open");
|
|
232
|
+
}
|
|
233
|
+
const stateHeaders = parseSyncOpenStateChunkHeaders(firstChunk.headers);
|
|
234
|
+
if (normalizeSyncPath(stateHeaders.path) !== path) {
|
|
235
|
+
throw new room_server_client_1.RoomServerException("sync.open stream returned a mismatched path");
|
|
236
|
+
}
|
|
102
237
|
const doc = new room_server_client_1.MeshDocument({
|
|
103
|
-
schema,
|
|
104
|
-
sendChangesToBackend: (
|
|
105
|
-
|
|
238
|
+
schema: schema_1.MeshSchema.fromJson(stateHeaders.schema),
|
|
239
|
+
sendChangesToBackend: (base64) => {
|
|
240
|
+
try {
|
|
241
|
+
streamState?.queueSync(utils_1.encoder.encode(base64));
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
}
|
|
106
245
|
},
|
|
107
246
|
});
|
|
108
247
|
const rc = new utils_1.RefCount(doc);
|
|
109
248
|
this._connectedDocuments[path] = rc;
|
|
249
|
+
this._documentStreams[path] = streamState;
|
|
250
|
+
this._applySyncPayload(rc, firstChunk.data);
|
|
251
|
+
streamState.attachTask(this._consumeOpenStream({
|
|
252
|
+
path,
|
|
253
|
+
rc,
|
|
254
|
+
iterator,
|
|
255
|
+
streamState,
|
|
256
|
+
}));
|
|
110
257
|
this.emit("connected", { type: "connect", doc });
|
|
111
|
-
|
|
258
|
+
connecting.complete(rc);
|
|
259
|
+
await doc.synchronized;
|
|
112
260
|
return doc;
|
|
113
261
|
}
|
|
114
|
-
catch (
|
|
115
|
-
|
|
116
|
-
|
|
262
|
+
catch (error) {
|
|
263
|
+
streamState?.closeInputStream();
|
|
264
|
+
if (iterator) {
|
|
265
|
+
await iterator.return?.();
|
|
266
|
+
}
|
|
267
|
+
connecting.completeError(error);
|
|
268
|
+
throw error;
|
|
117
269
|
}
|
|
118
270
|
finally {
|
|
119
271
|
delete this._connectingDocuments[path];
|
|
@@ -121,11 +273,6 @@ class SyncClient extends event_emitter_1.EventEmitter {
|
|
|
121
273
|
}
|
|
122
274
|
async close(path) {
|
|
123
275
|
path = normalizeSyncPath(path);
|
|
124
|
-
const pending = this._connectingDocuments[path];
|
|
125
|
-
if (pending) {
|
|
126
|
-
await pending;
|
|
127
|
-
await new Promise(resolve => setTimeout(resolve, 50));
|
|
128
|
-
}
|
|
129
276
|
const rc = this._connectedDocuments[path];
|
|
130
277
|
if (!rc) {
|
|
131
278
|
throw new room_server_client_1.RoomServerException(`Not connected to ${path}`);
|
|
@@ -134,14 +281,65 @@ class SyncClient extends event_emitter_1.EventEmitter {
|
|
|
134
281
|
rc.count--;
|
|
135
282
|
if (rc.count === 0) {
|
|
136
283
|
delete this._connectedDocuments[path];
|
|
137
|
-
|
|
138
|
-
|
|
284
|
+
const streamState = this._documentStreams[path];
|
|
285
|
+
delete this._documentStreams[path];
|
|
286
|
+
if (streamState) {
|
|
287
|
+
streamState.closeInputStream();
|
|
288
|
+
try {
|
|
289
|
+
await streamState.wait();
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
(0, runtime_1.unregisterDocument)(doc.id);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
(0, runtime_1.unregisterDocument)(doc.id);
|
|
297
|
+
}
|
|
139
298
|
}
|
|
140
299
|
this.emit("closed", { type: "close", doc });
|
|
141
300
|
}
|
|
142
301
|
async sync(path, data) {
|
|
143
302
|
path = normalizeSyncPath(path);
|
|
144
|
-
|
|
303
|
+
if (!this._connectedDocuments[path]) {
|
|
304
|
+
throw new room_server_client_1.RoomServerException("attempted to sync to a document that is not connected");
|
|
305
|
+
}
|
|
306
|
+
const streamState = this._documentStreams[path];
|
|
307
|
+
if (!streamState) {
|
|
308
|
+
throw new room_server_client_1.RoomServerException("attempted to sync to a document that is not connected");
|
|
309
|
+
}
|
|
310
|
+
streamState.queueSync(data);
|
|
311
|
+
}
|
|
312
|
+
async _consumeOpenStream(params) {
|
|
313
|
+
try {
|
|
314
|
+
while (true) {
|
|
315
|
+
const next = await params.iterator.next();
|
|
316
|
+
if (next.done || next.value === undefined) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const chunk = next.value;
|
|
320
|
+
if (chunk instanceof response_1.ErrorContent) {
|
|
321
|
+
throw new room_server_client_1.RoomServerException(chunk.text, chunk.code);
|
|
322
|
+
}
|
|
323
|
+
if (chunk instanceof response_1.ControlContent) {
|
|
324
|
+
if (chunk.method === "close") {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
throw this._unexpectedResponseError("open");
|
|
328
|
+
}
|
|
329
|
+
if (!(chunk instanceof response_1.BinaryContent)) {
|
|
330
|
+
throw this._unexpectedResponseError("open");
|
|
331
|
+
}
|
|
332
|
+
const headers = parseSyncOpenOutputChunkHeaders(chunk.headers);
|
|
333
|
+
if (normalizeSyncPath(headers.path) !== params.path) {
|
|
334
|
+
throw new room_server_client_1.RoomServerException("sync.open stream returned a mismatched path");
|
|
335
|
+
}
|
|
336
|
+
this._applySyncPayload(params.rc, chunk.data);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
finally {
|
|
340
|
+
params.streamState.closeInputStream();
|
|
341
|
+
await params.iterator.return?.();
|
|
342
|
+
}
|
|
145
343
|
}
|
|
146
344
|
}
|
|
147
345
|
exports.SyncClient = SyncClient;
|
package/dist/esm/agent-client.js
CHANGED
|
@@ -167,37 +167,12 @@ export class AgentsClient {
|
|
|
167
167
|
this.client = room;
|
|
168
168
|
}
|
|
169
169
|
async call(params) {
|
|
170
|
-
await this.client.
|
|
170
|
+
await this.client.call(params);
|
|
171
171
|
}
|
|
172
172
|
async listToolkits(params) {
|
|
173
|
-
|
|
174
|
-
if (params?.participantId != null) {
|
|
175
|
-
request["participant_id"] = params.participantId;
|
|
176
|
-
}
|
|
177
|
-
if (params?.participantName != null) {
|
|
178
|
-
request["participant_name"] = params.participantName;
|
|
179
|
-
}
|
|
180
|
-
if (params?.timeout !== undefined) {
|
|
181
|
-
request["timeout"] = params.timeout;
|
|
182
|
-
}
|
|
183
|
-
const result = (await this.client.sendRequest("agent.list_toolkits", request));
|
|
184
|
-
const tools = result.json["tools"];
|
|
185
|
-
const toolkits = [];
|
|
186
|
-
for (const name of Object.keys(tools)) {
|
|
187
|
-
const data = tools[name];
|
|
188
|
-
toolkits.push(ToolkitDescription.fromJson(data, { name }));
|
|
189
|
-
}
|
|
190
|
-
return toolkits;
|
|
173
|
+
return await this.client.listToolkits(params);
|
|
191
174
|
}
|
|
192
175
|
async invokeTool(params) {
|
|
193
|
-
|
|
194
|
-
toolkit: params.toolkit,
|
|
195
|
-
tool: params.tool,
|
|
196
|
-
arguments: {
|
|
197
|
-
type: "json",
|
|
198
|
-
json: params.arguments,
|
|
199
|
-
},
|
|
200
|
-
};
|
|
201
|
-
return await this.client.sendRequest("agent.invoke_tool", request);
|
|
176
|
+
return await this.client.invoke(params);
|
|
202
177
|
}
|
|
203
178
|
}
|
package/dist/esm/agent.js
CHANGED
|
@@ -88,15 +88,15 @@ export class RemoteToolkit extends Toolkit {
|
|
|
88
88
|
}
|
|
89
89
|
async start({ public_: isPublic = false } = {}) {
|
|
90
90
|
const handler = this._toolCall.bind(this);
|
|
91
|
-
this.client.protocol.addHandler(`
|
|
91
|
+
this.client.protocol.addHandler(`room.tool_call.${this.name}`, handler);
|
|
92
92
|
await this._register(isPublic);
|
|
93
93
|
}
|
|
94
94
|
async stop() {
|
|
95
95
|
await this._unregister();
|
|
96
|
-
this.client.protocol.removeHandler(`
|
|
96
|
+
this.client.protocol.removeHandler(`room.tool_call.${this.name}`);
|
|
97
97
|
}
|
|
98
98
|
async _register(public_) {
|
|
99
|
-
const response = await this.client.sendRequest("
|
|
99
|
+
const response = await this.client.sendRequest("room.register_toolkit", {
|
|
100
100
|
name: this.name,
|
|
101
101
|
title: this.title,
|
|
102
102
|
description: this.description,
|
|
@@ -109,7 +109,7 @@ export class RemoteToolkit extends Toolkit {
|
|
|
109
109
|
async _unregister() {
|
|
110
110
|
if (!this._registrationId)
|
|
111
111
|
return;
|
|
112
|
-
await this.client.sendRequest("
|
|
112
|
+
await this.client.sendRequest("room.unregister_toolkit", {
|
|
113
113
|
id: this._registrationId,
|
|
114
114
|
});
|
|
115
115
|
}
|
|
@@ -139,11 +139,11 @@ export class RemoteToolkit extends Toolkit {
|
|
|
139
139
|
args = rawArguments ?? {};
|
|
140
140
|
}
|
|
141
141
|
const response = await this.execute(toolName, args);
|
|
142
|
-
await this.client.protocol.send("
|
|
142
|
+
await this.client.protocol.send("room.tool_call_response", response.pack(), messageId);
|
|
143
143
|
}
|
|
144
144
|
catch (e) {
|
|
145
145
|
const err = new ErrorContent({ text: String(e) });
|
|
146
|
-
await this.client.protocol.send("
|
|
146
|
+
await this.client.protocol.send("room.tool_call_response", err.pack(), messageId);
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { ContainerMountSpec } from "./meshagent-client";
|
|
2
|
+
import { Content } from "./response";
|
|
3
|
+
import { RoomClient } from "./room-client";
|
|
4
|
+
export interface DockerSecret {
|
|
5
|
+
username: string;
|
|
6
|
+
password: string;
|
|
7
|
+
registry: string;
|
|
8
|
+
email?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ContainerImage {
|
|
11
|
+
id: string;
|
|
12
|
+
tags: string[];
|
|
13
|
+
size?: number;
|
|
14
|
+
labels: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
export interface ContainerParticipantInfo {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
export interface RoomContainer {
|
|
21
|
+
id: string;
|
|
22
|
+
image: string;
|
|
23
|
+
name?: string;
|
|
24
|
+
startedBy: ContainerParticipantInfo;
|
|
25
|
+
state: string;
|
|
26
|
+
private: boolean;
|
|
27
|
+
serviceId?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface ContainerLogsSession {
|
|
30
|
+
stream: AsyncIterable<string>;
|
|
31
|
+
result: Promise<void>;
|
|
32
|
+
cancel(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export declare class ExecSession {
|
|
35
|
+
readonly command: string;
|
|
36
|
+
readonly result: Promise<number>;
|
|
37
|
+
readonly previousOutput: Uint8Array[];
|
|
38
|
+
readonly output: AsyncIterable<Uint8Array>;
|
|
39
|
+
private readonly requestId;
|
|
40
|
+
private readonly containerId;
|
|
41
|
+
private readonly tty?;
|
|
42
|
+
private readonly resultCompleter;
|
|
43
|
+
private readonly outputController;
|
|
44
|
+
private readonly queuedInput;
|
|
45
|
+
private inputClosed;
|
|
46
|
+
private closed;
|
|
47
|
+
private inputWaiter;
|
|
48
|
+
private lastResizeWidth?;
|
|
49
|
+
private lastResizeHeight?;
|
|
50
|
+
constructor(params: {
|
|
51
|
+
requestId: string;
|
|
52
|
+
command: string;
|
|
53
|
+
containerId: string;
|
|
54
|
+
tty?: boolean;
|
|
55
|
+
});
|
|
56
|
+
inputStream(): AsyncIterable<Content>;
|
|
57
|
+
write(data: Uint8Array): Promise<void>;
|
|
58
|
+
resize(params: {
|
|
59
|
+
width: number;
|
|
60
|
+
height: number;
|
|
61
|
+
}): Promise<void>;
|
|
62
|
+
stop(): Promise<void>;
|
|
63
|
+
kill(): Promise<void>;
|
|
64
|
+
close(status: number): void;
|
|
65
|
+
closeError(error: unknown): void;
|
|
66
|
+
addOutput(data: Uint8Array): void;
|
|
67
|
+
get isClosed(): boolean;
|
|
68
|
+
private queueInput;
|
|
69
|
+
private closeInputStream;
|
|
70
|
+
}
|
|
71
|
+
export declare class ContainersClient {
|
|
72
|
+
private readonly room;
|
|
73
|
+
constructor({ room }: {
|
|
74
|
+
room: RoomClient;
|
|
75
|
+
});
|
|
76
|
+
private unexpectedResponseError;
|
|
77
|
+
private invoke;
|
|
78
|
+
listImages(): Promise<ContainerImage[]>;
|
|
79
|
+
pullImage(params: {
|
|
80
|
+
tag: string;
|
|
81
|
+
credentials?: DockerSecret[];
|
|
82
|
+
}): Promise<void>;
|
|
83
|
+
run(params: {
|
|
84
|
+
image: string;
|
|
85
|
+
command?: string;
|
|
86
|
+
workingDir?: string;
|
|
87
|
+
env?: Record<string, string>;
|
|
88
|
+
mountPath?: string;
|
|
89
|
+
mountSubpath?: string;
|
|
90
|
+
role?: string;
|
|
91
|
+
participantName?: string;
|
|
92
|
+
ports?: Record<number, number> | Record<string, number>;
|
|
93
|
+
credentials?: DockerSecret[];
|
|
94
|
+
name?: string;
|
|
95
|
+
mounts?: ContainerMountSpec;
|
|
96
|
+
writableRootFs?: boolean;
|
|
97
|
+
private?: boolean;
|
|
98
|
+
}): Promise<string>;
|
|
99
|
+
runService(params: {
|
|
100
|
+
serviceId: string;
|
|
101
|
+
env?: Record<string, string>;
|
|
102
|
+
}): Promise<string>;
|
|
103
|
+
exec(params: {
|
|
104
|
+
containerId: string;
|
|
105
|
+
command: string;
|
|
106
|
+
tty?: boolean;
|
|
107
|
+
}): ExecSession;
|
|
108
|
+
stop(params: {
|
|
109
|
+
containerId: string;
|
|
110
|
+
force?: boolean;
|
|
111
|
+
}): Promise<void>;
|
|
112
|
+
waitForExit(params: {
|
|
113
|
+
containerId: string;
|
|
114
|
+
}): Promise<number>;
|
|
115
|
+
deleteContainer(params: {
|
|
116
|
+
containerId: string;
|
|
117
|
+
}): Promise<void>;
|
|
118
|
+
logs(params: {
|
|
119
|
+
containerId: string;
|
|
120
|
+
follow?: boolean;
|
|
121
|
+
}): ContainerLogsSession;
|
|
122
|
+
list(params?: {
|
|
123
|
+
all?: boolean;
|
|
124
|
+
}): Promise<RoomContainer[]>;
|
|
125
|
+
}
|