sandbox0 0.7.8 → 0.7.10
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/dist/apispec/src/apis/ObservabilityApi.d.ts +11 -5
- package/dist/apispec/src/apis/ObservabilityApi.d.ts.map +1 -1
- package/dist/apispec/src/apis/ObservabilityApi.js +22 -4
- package/dist/apispec/src/apis/SessionsApi.d.ts +202 -0
- package/dist/apispec/src/apis/SessionsApi.d.ts.map +1 -0
- package/dist/apispec/src/apis/SessionsApi.js +563 -0
- package/dist/apispec/src/apis/index.d.ts +1 -0
- package/dist/apispec/src/apis/index.d.ts.map +1 -1
- package/dist/apispec/src/apis/index.js +1 -0
- package/dist/apispec/src/models/CreateExecutionSessionAttemptRequest.d.ts +33 -0
- package/dist/apispec/src/models/CreateExecutionSessionAttemptRequest.d.ts.map +1 -0
- package/dist/apispec/src/models/CreateExecutionSessionAttemptRequest.js +41 -0
- package/dist/apispec/src/models/ExecutionSession.d.ts +97 -0
- package/dist/apispec/src/models/ExecutionSession.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSession.js +85 -0
- package/dist/apispec/src/models/ExecutionSessionAttempt.d.ts +75 -0
- package/dist/apispec/src/models/ExecutionSessionAttempt.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionAttempt.js +61 -0
- package/dist/apispec/src/models/ExecutionSessionDesiredState.d.ts +26 -0
- package/dist/apispec/src/models/ExecutionSessionDesiredState.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionDesiredState.js +43 -0
- package/dist/apispec/src/models/ExecutionSessionDesiredStateRequest.d.ts +34 -0
- package/dist/apispec/src/models/ExecutionSessionDesiredStateRequest.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionDesiredStateRequest.js +44 -0
- package/dist/apispec/src/models/ExecutionSessionEvent.d.ts +96 -0
- package/dist/apispec/src/models/ExecutionSessionEvent.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionEvent.js +77 -0
- package/dist/apispec/src/models/ExecutionSessionEventCursor.d.ts +39 -0
- package/dist/apispec/src/models/ExecutionSessionEventCursor.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionEventCursor.js +47 -0
- package/dist/apispec/src/models/ExecutionSessionEventPage.d.ts +41 -0
- package/dist/apispec/src/models/ExecutionSessionEventPage.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionEventPage.js +49 -0
- package/dist/apispec/src/models/ExecutionSessionEventRetentionSpec.d.ts +39 -0
- package/dist/apispec/src/models/ExecutionSessionEventRetentionSpec.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionEventRetentionSpec.js +43 -0
- package/dist/apispec/src/models/ExecutionSessionIOMode.d.ts +26 -0
- package/dist/apispec/src/models/ExecutionSessionIOMode.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionIOMode.js +43 -0
- package/dist/apispec/src/models/ExecutionSessionIOSpec.d.ts +41 -0
- package/dist/apispec/src/models/ExecutionSessionIOSpec.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionIOSpec.js +45 -0
- package/dist/apispec/src/models/ExecutionSessionInputRequest.d.ts +51 -0
- package/dist/apispec/src/models/ExecutionSessionInputRequest.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionInputRequest.js +49 -0
- package/dist/apispec/src/models/ExecutionSessionInputResponse.d.ts +51 -0
- package/dist/apispec/src/models/ExecutionSessionInputResponse.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionInputResponse.js +55 -0
- package/dist/apispec/src/models/ExecutionSessionLifecycleSpec.d.ts +66 -0
- package/dist/apispec/src/models/ExecutionSessionLifecycleSpec.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionLifecycleSpec.js +54 -0
- package/dist/apispec/src/models/ExecutionSessionPhase.d.ts +34 -0
- package/dist/apispec/src/models/ExecutionSessionPhase.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionPhase.js +51 -0
- package/dist/apispec/src/models/ExecutionSessionReadinessSpec.d.ts +52 -0
- package/dist/apispec/src/models/ExecutionSessionReadinessSpec.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionReadinessSpec.js +48 -0
- package/dist/apispec/src/models/ExecutionSessionReadinessType.d.ts +27 -0
- package/dist/apispec/src/models/ExecutionSessionReadinessType.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionReadinessType.js +44 -0
- package/dist/apispec/src/models/ExecutionSessionRestartPolicy.d.ts +27 -0
- package/dist/apispec/src/models/ExecutionSessionRestartPolicy.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionRestartPolicy.js +44 -0
- package/dist/apispec/src/models/ExecutionSessionRestartSpec.d.ts +58 -0
- package/dist/apispec/src/models/ExecutionSessionRestartSpec.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionRestartSpec.js +50 -0
- package/dist/apispec/src/models/ExecutionSessionRuntimeRecoveryPolicy.d.ts +26 -0
- package/dist/apispec/src/models/ExecutionSessionRuntimeRecoveryPolicy.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionRuntimeRecoveryPolicy.js +43 -0
- package/dist/apispec/src/models/ExecutionSessionSignalRequest.d.ts +39 -0
- package/dist/apispec/src/models/ExecutionSessionSignalRequest.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionSignalRequest.js +45 -0
- package/dist/apispec/src/models/ExecutionSessionSpec.d.ts +81 -0
- package/dist/apispec/src/models/ExecutionSessionSpec.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionSpec.js +61 -0
- package/dist/apispec/src/models/ExecutionSessionTerminalResizeRequest.d.ts +45 -0
- package/dist/apispec/src/models/ExecutionSessionTerminalResizeRequest.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionTerminalResizeRequest.js +49 -0
- package/dist/apispec/src/models/ExecutionSessionTerminalSpec.d.ts +45 -0
- package/dist/apispec/src/models/ExecutionSessionTerminalSpec.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionTerminalSpec.js +45 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketAck.d.ts +46 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketAck.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketAck.js +51 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketError.d.ts +52 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketError.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketError.js +55 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketEvent.d.ts +47 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketEvent.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketEvent.js +54 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketInput.d.ts +70 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketInput.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketInput.js +61 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketRequest.d.ts +25 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketRequest.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketRequest.js +58 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketResize.d.ts +64 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketResize.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketResize.js +61 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketResponse.d.ts +25 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketResponse.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketResponse.js +58 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketSignal.d.ts +58 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketSignal.d.ts.map +1 -0
- package/dist/apispec/src/models/ExecutionSessionWebSocketSignal.js +57 -0
- package/dist/apispec/src/models/ObservabilityEventSource.d.ts +3 -0
- package/dist/apispec/src/models/ObservabilityEventSource.d.ts.map +1 -1
- package/dist/apispec/src/models/ObservabilityEventSource.js +4 -1
- package/dist/apispec/src/models/SandboxAuditActor.d.ts +58 -0
- package/dist/apispec/src/models/SandboxAuditActor.d.ts.map +1 -0
- package/dist/apispec/src/models/SandboxAuditActor.js +52 -0
- package/dist/apispec/src/models/SandboxAuditActorKind.d.ts +31 -0
- package/dist/apispec/src/models/SandboxAuditActorKind.d.ts.map +1 -0
- package/dist/apispec/src/models/SandboxAuditActorKind.js +48 -0
- package/dist/apispec/src/models/SandboxAuditEventPhase.d.ts +27 -0
- package/dist/apispec/src/models/SandboxAuditEventPhase.d.ts.map +1 -0
- package/dist/apispec/src/models/SandboxAuditEventPhase.js +44 -0
- package/dist/apispec/src/models/SandboxAuditIntegrity.d.ts +79 -0
- package/dist/apispec/src/models/SandboxAuditIntegrity.d.ts.map +1 -0
- package/dist/apispec/src/models/SandboxAuditIntegrity.js +75 -0
- package/dist/apispec/src/models/SandboxAuditProducer.d.ts +45 -0
- package/dist/apispec/src/models/SandboxAuditProducer.d.ts.map +1 -0
- package/dist/apispec/src/models/SandboxAuditProducer.js +47 -0
- package/dist/apispec/src/models/SandboxAuditRequest.d.ts +69 -0
- package/dist/apispec/src/models/SandboxAuditRequest.d.ts.map +1 -0
- package/dist/apispec/src/models/SandboxAuditRequest.js +53 -0
- package/dist/apispec/src/models/SandboxAuditResource.d.ts +45 -0
- package/dist/apispec/src/models/SandboxAuditResource.d.ts.map +1 -0
- package/dist/apispec/src/models/SandboxAuditResource.js +49 -0
- package/dist/apispec/src/models/SandboxObservabilityEvent.d.ts +70 -3
- package/dist/apispec/src/models/SandboxObservabilityEvent.d.ts.map +1 -1
- package/dist/apispec/src/models/SandboxObservabilityEvent.js +53 -7
- package/dist/apispec/src/models/SandboxObservabilityEventType.d.ts +3 -0
- package/dist/apispec/src/models/SandboxObservabilityEventType.d.ts.map +1 -1
- package/dist/apispec/src/models/SandboxObservabilityEventType.js +4 -1
- package/dist/apispec/src/models/SandboxObservabilityOutcome.d.ts +2 -0
- package/dist/apispec/src/models/SandboxObservabilityOutcome.d.ts.map +1 -1
- package/dist/apispec/src/models/SandboxObservabilityOutcome.js +3 -1
- package/dist/apispec/src/models/SuccessAcceptedResponse.d.ts +40 -0
- package/dist/apispec/src/models/SuccessAcceptedResponse.d.ts.map +1 -0
- package/dist/apispec/src/models/SuccessAcceptedResponse.js +46 -0
- package/dist/apispec/src/models/SuccessAcceptedResponseAllOfData.d.ts +33 -0
- package/dist/apispec/src/models/SuccessAcceptedResponseAllOfData.d.ts.map +1 -0
- package/dist/apispec/src/models/SuccessAcceptedResponseAllOfData.js +43 -0
- package/dist/apispec/src/models/SuccessExecutionSessionEventPageResponse.d.ts +40 -0
- package/dist/apispec/src/models/SuccessExecutionSessionEventPageResponse.d.ts.map +1 -0
- package/dist/apispec/src/models/SuccessExecutionSessionEventPageResponse.js +46 -0
- package/dist/apispec/src/models/SuccessExecutionSessionInputResponse.d.ts +40 -0
- package/dist/apispec/src/models/SuccessExecutionSessionInputResponse.d.ts.map +1 -0
- package/dist/apispec/src/models/SuccessExecutionSessionInputResponse.js +46 -0
- package/dist/apispec/src/models/SuccessExecutionSessionListResponse.d.ts +40 -0
- package/dist/apispec/src/models/SuccessExecutionSessionListResponse.d.ts.map +1 -0
- package/dist/apispec/src/models/SuccessExecutionSessionListResponse.js +46 -0
- package/dist/apispec/src/models/SuccessExecutionSessionListResponseAllOfData.d.ts +34 -0
- package/dist/apispec/src/models/SuccessExecutionSessionListResponseAllOfData.d.ts.map +1 -0
- package/dist/apispec/src/models/SuccessExecutionSessionListResponseAllOfData.js +44 -0
- package/dist/apispec/src/models/SuccessExecutionSessionResponse.d.ts +40 -0
- package/dist/apispec/src/models/SuccessExecutionSessionResponse.d.ts.map +1 -0
- package/dist/apispec/src/models/SuccessExecutionSessionResponse.js +46 -0
- package/dist/apispec/src/models/index.d.ts +46 -0
- package/dist/apispec/src/models/index.d.ts.map +1 -1
- package/dist/apispec/src/models/index.js +46 -0
- package/dist/client.d.ts +3 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +14 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/models.d.ts +44 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/sandbox_contexts.d.ts +1 -1
- package/dist/sandbox_files.d.ts +1 -1
- package/dist/sandbox_network.d.ts +1 -1
- package/dist/sandbox_observability.d.ts +1 -1
- package/dist/sandbox_services.d.ts +1 -1
- package/dist/sandbox_sessions.d.ts +39 -0
- package/dist/sandbox_sessions.d.ts.map +1 -0
- package/dist/sandbox_sessions.js +275 -0
- package/package.json +1 -1
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { models } from "./apispec_compat.js";
|
|
2
|
+
import { APIError, wrapApiCall } from "./errors.js";
|
|
3
|
+
import { ensureData, ensureModel } from "./response.js";
|
|
4
|
+
import { Sandbox } from "./sandbox.js";
|
|
5
|
+
import { WebSocketClient } from "./ws_client.js";
|
|
6
|
+
function getClient(sandbox) {
|
|
7
|
+
return sandbox.client;
|
|
8
|
+
}
|
|
9
|
+
Sandbox.prototype.listSessions = async function () {
|
|
10
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsGet({ id: this.id }));
|
|
11
|
+
return ensureData(response, "list sessions returned empty response").sessions ?? [];
|
|
12
|
+
};
|
|
13
|
+
Sandbox.prototype.createSession = async function (spec, options) {
|
|
14
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsPost({
|
|
15
|
+
id: this.id,
|
|
16
|
+
executionSessionSpec: spec,
|
|
17
|
+
idempotencyKey: options?.idempotencyKey,
|
|
18
|
+
}));
|
|
19
|
+
return ensureData(response, "create session returned empty response");
|
|
20
|
+
};
|
|
21
|
+
Sandbox.prototype.getSession = async function (sessionId) {
|
|
22
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdGet({
|
|
23
|
+
id: this.id,
|
|
24
|
+
sessionId,
|
|
25
|
+
}));
|
|
26
|
+
return ensureData(response, "get session returned empty response");
|
|
27
|
+
};
|
|
28
|
+
Sandbox.prototype.updateSession = async function (sessionId, spec) {
|
|
29
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdPut({
|
|
30
|
+
id: this.id,
|
|
31
|
+
sessionId,
|
|
32
|
+
executionSessionSpec: spec,
|
|
33
|
+
}));
|
|
34
|
+
return ensureData(response, "update session returned empty response");
|
|
35
|
+
};
|
|
36
|
+
Sandbox.prototype.deleteSession = async function (sessionId) {
|
|
37
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdDelete({
|
|
38
|
+
id: this.id,
|
|
39
|
+
sessionId,
|
|
40
|
+
}));
|
|
41
|
+
return ensureModel(response, "delete session returned empty response");
|
|
42
|
+
};
|
|
43
|
+
Sandbox.prototype.setSessionDesiredState = async function (sessionId, state) {
|
|
44
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdDesiredStatePut({
|
|
45
|
+
id: this.id,
|
|
46
|
+
sessionId,
|
|
47
|
+
executionSessionDesiredStateRequest: { state },
|
|
48
|
+
}));
|
|
49
|
+
return ensureData(response, "set session desired state returned empty response");
|
|
50
|
+
};
|
|
51
|
+
Sandbox.prototype.createSessionAttempt = async function (sessionId, replaceCurrent = false) {
|
|
52
|
+
const request = { replaceCurrent };
|
|
53
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdAttemptsPost({
|
|
54
|
+
id: this.id,
|
|
55
|
+
sessionId,
|
|
56
|
+
createExecutionSessionAttemptRequest: request,
|
|
57
|
+
}));
|
|
58
|
+
return ensureData(response, "create session attempt returned empty response");
|
|
59
|
+
};
|
|
60
|
+
Sandbox.prototype.writeSessionInput = async function (sessionId, request) {
|
|
61
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdInputsPost({
|
|
62
|
+
id: this.id,
|
|
63
|
+
sessionId,
|
|
64
|
+
executionSessionInputRequest: request,
|
|
65
|
+
}));
|
|
66
|
+
return ensureData(response, "write session input returned empty response");
|
|
67
|
+
};
|
|
68
|
+
Sandbox.prototype.sendSessionSignal = async function (sessionId, request) {
|
|
69
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdSignalsPost({
|
|
70
|
+
id: this.id,
|
|
71
|
+
sessionId,
|
|
72
|
+
executionSessionSignalRequest: request,
|
|
73
|
+
}));
|
|
74
|
+
return ensureModel(response, "send session signal returned empty response");
|
|
75
|
+
};
|
|
76
|
+
Sandbox.prototype.resizeSessionTerminal = async function (sessionId, request) {
|
|
77
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdTerminalPut({
|
|
78
|
+
id: this.id,
|
|
79
|
+
sessionId,
|
|
80
|
+
executionSessionTerminalResizeRequest: request,
|
|
81
|
+
}));
|
|
82
|
+
return ensureModel(response, "resize session terminal returned empty response");
|
|
83
|
+
};
|
|
84
|
+
Sandbox.prototype.listSessionEvents = async function (sessionId, options) {
|
|
85
|
+
const response = await wrapApiCall(() => getClient(this).apispec.sessions.apiV1SandboxesIdSessionsSessionIdEventsGet({
|
|
86
|
+
id: this.id,
|
|
87
|
+
sessionId,
|
|
88
|
+
after: options?.after,
|
|
89
|
+
limit: options?.limit,
|
|
90
|
+
}));
|
|
91
|
+
return ensureData(response, "list session events returned empty response");
|
|
92
|
+
};
|
|
93
|
+
Sandbox.prototype.watchSessionEvents = async function (sessionId, options) {
|
|
94
|
+
const response = await getClient(this).openSandboxSessionEventStream(this.id, sessionId, options);
|
|
95
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
96
|
+
if (!contentType.toLowerCase().startsWith("text/event-stream")) {
|
|
97
|
+
throw new APIError({
|
|
98
|
+
statusCode: response.status,
|
|
99
|
+
code: "unexpected_response",
|
|
100
|
+
message: `unexpected session event stream content type: ${contentType}`,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return new ExecutionSessionEventStream(response);
|
|
104
|
+
};
|
|
105
|
+
Sandbox.prototype.connectSession = async function (sessionId, options) {
|
|
106
|
+
const client = getClient(this);
|
|
107
|
+
const path = `/api/v1/sandboxes/${encodeURIComponent(this.id)}/sessions/${encodeURIComponent(sessionId)}/ws`;
|
|
108
|
+
const wsUrl = new URL(client.websocketUrl(path));
|
|
109
|
+
if (options?.after !== undefined) {
|
|
110
|
+
wsUrl.searchParams.set("after", String(options.after));
|
|
111
|
+
}
|
|
112
|
+
const socket = await WebSocketClient.connect(wsUrl.toString(), { headers: await client.wsHeaders() });
|
|
113
|
+
await socket.waitForOpen();
|
|
114
|
+
return new ExecutionSessionConnection(socket);
|
|
115
|
+
};
|
|
116
|
+
export class ExecutionSessionEventStream {
|
|
117
|
+
response;
|
|
118
|
+
body;
|
|
119
|
+
reader;
|
|
120
|
+
consumed = false;
|
|
121
|
+
constructor(response) {
|
|
122
|
+
if (!response.body) {
|
|
123
|
+
throw new APIError({
|
|
124
|
+
statusCode: response.status,
|
|
125
|
+
code: "unexpected_response",
|
|
126
|
+
message: "session event stream response did not include a body",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
this.response = response;
|
|
130
|
+
this.body = response.body;
|
|
131
|
+
}
|
|
132
|
+
async *[Symbol.asyncIterator]() {
|
|
133
|
+
if (this.consumed) {
|
|
134
|
+
throw new Error("session event stream can only be consumed once");
|
|
135
|
+
}
|
|
136
|
+
this.consumed = true;
|
|
137
|
+
const reader = this.body.getReader();
|
|
138
|
+
this.reader = reader;
|
|
139
|
+
const decoder = new TextDecoder();
|
|
140
|
+
let buffer = "";
|
|
141
|
+
try {
|
|
142
|
+
while (true) {
|
|
143
|
+
const { value, done } = await reader.read();
|
|
144
|
+
if (done)
|
|
145
|
+
break;
|
|
146
|
+
buffer += decoder.decode(value, { stream: true });
|
|
147
|
+
buffer = buffer.replace(/\r\n/g, "\n");
|
|
148
|
+
let boundary = buffer.indexOf("\n\n");
|
|
149
|
+
while (boundary >= 0) {
|
|
150
|
+
const event = parseSSEEvent(buffer.slice(0, boundary));
|
|
151
|
+
buffer = buffer.slice(boundary + 2);
|
|
152
|
+
if (event)
|
|
153
|
+
yield event;
|
|
154
|
+
boundary = buffer.indexOf("\n\n");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
buffer += decoder.decode();
|
|
158
|
+
const event = parseSSEEvent(buffer);
|
|
159
|
+
if (event)
|
|
160
|
+
yield event;
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
this.reader = undefined;
|
|
164
|
+
reader.releaseLock();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async close() {
|
|
168
|
+
if (this.reader) {
|
|
169
|
+
await this.reader.cancel();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!this.body.locked) {
|
|
173
|
+
await this.body.cancel();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
export class ExecutionSessionConnection {
|
|
178
|
+
readable;
|
|
179
|
+
socket;
|
|
180
|
+
constructor(socket) {
|
|
181
|
+
this.socket = socket;
|
|
182
|
+
this.readable = new ReadableStream({
|
|
183
|
+
start: (controller) => {
|
|
184
|
+
let closed = false;
|
|
185
|
+
socket.onMessage((data) => {
|
|
186
|
+
if (closed)
|
|
187
|
+
return;
|
|
188
|
+
const message = parseSessionWebSocketMessage(data);
|
|
189
|
+
if (message)
|
|
190
|
+
controller.enqueue(message);
|
|
191
|
+
});
|
|
192
|
+
socket.onError((error) => {
|
|
193
|
+
if (closed)
|
|
194
|
+
return;
|
|
195
|
+
closed = true;
|
|
196
|
+
controller.error(error);
|
|
197
|
+
});
|
|
198
|
+
socket.onClose(() => {
|
|
199
|
+
if (closed)
|
|
200
|
+
return;
|
|
201
|
+
closed = true;
|
|
202
|
+
controller.close();
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
cancel: () => socket.close(),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
send(request) {
|
|
209
|
+
this.socket.send(JSON.stringify(models.ExecutionSessionWebSocketRequestToJSON(request)));
|
|
210
|
+
}
|
|
211
|
+
async *messages() {
|
|
212
|
+
const reader = this.readable.getReader();
|
|
213
|
+
try {
|
|
214
|
+
while (true) {
|
|
215
|
+
const { value, done } = await reader.read();
|
|
216
|
+
if (done)
|
|
217
|
+
return;
|
|
218
|
+
yield value;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
finally {
|
|
222
|
+
reader.releaseLock();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/** Detaches this client without stopping the session or closing stdin. */
|
|
226
|
+
close() {
|
|
227
|
+
this.socket.close();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function parseSSEEvent(block) {
|
|
231
|
+
const data = block
|
|
232
|
+
.split("\n")
|
|
233
|
+
.filter((line) => line.startsWith("data:"))
|
|
234
|
+
.map((line) => line.slice(5).replace(/^ /, ""))
|
|
235
|
+
.join("\n");
|
|
236
|
+
if (!data)
|
|
237
|
+
return undefined;
|
|
238
|
+
return models.ExecutionSessionEventFromJSON(JSON.parse(data));
|
|
239
|
+
}
|
|
240
|
+
function parseSessionWebSocketMessage(data) {
|
|
241
|
+
try {
|
|
242
|
+
const raw = JSON.parse(webSocketText(data));
|
|
243
|
+
const type = raw?.type;
|
|
244
|
+
if (type === "event" && raw.event) {
|
|
245
|
+
return { type, event: models.ExecutionSessionEventFromJSON(raw.event) };
|
|
246
|
+
}
|
|
247
|
+
if (type === "ack") {
|
|
248
|
+
return { type, requestId: raw.request_id };
|
|
249
|
+
}
|
|
250
|
+
if (type === "error") {
|
|
251
|
+
return { type, requestId: raw.request_id, error: String(raw.error ?? "") };
|
|
252
|
+
}
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function webSocketText(data) {
|
|
260
|
+
if (typeof data === "string")
|
|
261
|
+
return data;
|
|
262
|
+
if (Array.isArray(data)) {
|
|
263
|
+
const length = data.reduce((total, item) => total + item.byteLength, 0);
|
|
264
|
+
const joined = new Uint8Array(length);
|
|
265
|
+
let offset = 0;
|
|
266
|
+
for (const item of data) {
|
|
267
|
+
joined.set(item, offset);
|
|
268
|
+
offset += item.byteLength;
|
|
269
|
+
}
|
|
270
|
+
return new TextDecoder().decode(joined);
|
|
271
|
+
}
|
|
272
|
+
if (data instanceof ArrayBuffer)
|
|
273
|
+
return new TextDecoder().decode(new Uint8Array(data));
|
|
274
|
+
return new TextDecoder().decode(data);
|
|
275
|
+
}
|