-
Notifications
You must be signed in to change notification settings - Fork 954
feat(sdks/kotlin): add interactive PTY session support #1052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ferponse
wants to merge
6
commits into
opensandbox-group:main
from
ferponse:feat/sdk-kotlin-pty-sessions
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
889b764
feat(sdks/kotlin): add interactive PTY session support
15fb425
fix(sdks/kotlin): make PTY API Java-friendly and carry WS handshake h…
b7da849
fix(sdks/kotlin): harden PTY adapter and track /pty in the execd spec
bfdb7e0
fix(sdks/kotlin): keep PTY endpoint scheme consistent with sibling ad…
8fe18b3
fix(sdks/kotlin): proxy-aware PTY scheme and track /pty in the execd …
efad1ce
chore(sdks/kotlin): keep /pty out of the shared execd spec for this PR
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...dbox/src/main/kotlin/com/alibaba/opensandbox/sandbox/domain/models/execd/pty/PtyModels.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright 2025 Alibaba Group Holding Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.alibaba.opensandbox.sandbox.domain.models.execd.pty | ||
|
|
||
| /** | ||
| * Streaming mode for a PTY WebSocket connection. | ||
| * | ||
| * - [PTY]: a real pseudo-terminal (ANSI, `stty`, resize). Default. | ||
| * - [PIPE]: stdout/stderr are split into separate frames, without a TTY. | ||
| */ | ||
| enum class PtyMode { | ||
| PTY, | ||
| PIPE, | ||
| } | ||
|
|
||
| /** | ||
| * A created PTY session. | ||
| * | ||
| * The shell is not started until the first WebSocket attaches; this only | ||
| * identifies the server-side session. | ||
| * | ||
| * @property sessionId Server-assigned identifier of the PTY session | ||
| */ | ||
| class PtySession( | ||
| val sessionId: String, | ||
| ) | ||
|
|
||
| /** | ||
| * Current status of a PTY session. | ||
| * | ||
| * @property sessionId Identifier of the PTY session | ||
| * @property running Whether the underlying shell process is alive | ||
| * @property outputOffset Byte offset of the buffered output; pass it as `since` | ||
| * when reconnecting to replay scrollback from that point | ||
| */ | ||
| class PtySessionStatus( | ||
| val sessionId: String, | ||
| val running: Boolean, | ||
| val outputOffset: Long, | ||
| ) | ||
|
|
||
| /** | ||
| * WebSocket target for attaching to a PTY session. | ||
| * | ||
| * @property url The `ws://` or `wss://` URL to open | ||
| * @property headers Routing/auth headers that must be sent on the WebSocket handshake | ||
| * (header-mode ingress and secure-access endpoints require them) | ||
| */ | ||
| class PtyWebSocket( | ||
| val url: String, | ||
| val headers: Map<String, String>, | ||
| ) |
101 changes: 101 additions & 0 deletions
101
...box/kotlin/sandbox/src/main/kotlin/com/alibaba/opensandbox/sandbox/domain/services/Pty.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * Copyright 2025 Alibaba Group Holding Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.alibaba.opensandbox.sandbox.domain.services | ||
|
|
||
| import com.alibaba.opensandbox.sandbox.domain.models.execd.pty.PtyMode | ||
| import com.alibaba.opensandbox.sandbox.domain.models.execd.pty.PtySession | ||
| import com.alibaba.opensandbox.sandbox.domain.models.execd.pty.PtySessionStatus | ||
| import com.alibaba.opensandbox.sandbox.domain.models.execd.pty.PtyWebSocket | ||
|
|
||
| /** | ||
| * Interactive pseudo-terminal (PTY) session lifecycle for a sandbox. | ||
| * | ||
| * A PTY session is a long-lived shell driven over a WebSocket. This service manages the | ||
| * session lifecycle over execd's HTTP API ([createSession] / [getSession] / [deleteSession]) | ||
| * and resolves the WebSocket target ([webSocket]) that a client connects to in order to stream | ||
| * the interactive session. Driving the WebSocket itself (binary stdin/stdout frames, resize, | ||
| * takeover) is left to the caller, which can use any WebSocket client. | ||
| * | ||
| * Explicit overloads (rather than Kotlin default arguments) are provided so the API stays | ||
| * ergonomic from Java, where interface default arguments are not emitted as overloads. | ||
| * | ||
| * PTY is only supported on Unix-like platforms (Linux/macOS). | ||
| */ | ||
| interface Pty { | ||
| /** | ||
| * Creates a new PTY session. The shell does not start until the first WebSocket attaches. | ||
| * | ||
| * @param cwd Optional working directory for the shell | ||
| * @param command Optional command to run instead of the default login shell | ||
| * @return The created session | ||
| */ | ||
| fun createSession( | ||
| cwd: String?, | ||
| command: String?, | ||
| ): PtySession | ||
|
|
||
| /** Creates a new PTY session in the given working directory with the default shell. */ | ||
| fun createSession(cwd: String?): PtySession = createSession(cwd, null) | ||
|
|
||
| /** Creates a new PTY session with the default working directory and shell. */ | ||
| fun createSession(): PtySession = createSession(null, null) | ||
|
|
||
| /** | ||
| * Retrieves the current status of a PTY session. | ||
| * | ||
| * @param sessionId Identifier of the PTY session | ||
| * @return Session status, including the output offset usable for replay | ||
| */ | ||
| fun getSession(sessionId: String): PtySessionStatus | ||
|
|
||
| /** | ||
| * Tears down a PTY session on the server side. | ||
| * | ||
| * @param sessionId Identifier of the PTY session | ||
| */ | ||
| fun deleteSession(sessionId: String) | ||
|
|
||
| /** | ||
| * Resolves the WebSocket target used to attach to a PTY session. | ||
| * | ||
| * The returned [PtyWebSocket] carries both the `ws`/`wss` URL and the routing/auth headers | ||
| * resolved for the sandbox endpoint; callers must send those headers on the WebSocket | ||
| * handshake (header-mode ingress and secure-access endpoints rely on them), just as the REST | ||
| * calls in this service do. | ||
| * | ||
| * @param sessionId Identifier of the PTY session | ||
| * @param mode Streaming mode ([PtyMode.PTY]; [PtyMode.PIPE] adds `pty=0`) | ||
| * @param since Optional byte offset to replay buffered output from on reconnect | ||
| * @param takeover When true, evict the current holder (`takeover=1`) and attach to the same shell | ||
| * @return The WebSocket URL together with the headers to send on the handshake | ||
| */ | ||
| fun webSocket( | ||
| sessionId: String, | ||
| mode: PtyMode, | ||
| since: Long?, | ||
| takeover: Boolean, | ||
| ): PtyWebSocket | ||
|
|
||
| /** Resolves the WebSocket target for the given mode, without replay or takeover. */ | ||
| fun webSocket( | ||
| sessionId: String, | ||
| mode: PtyMode, | ||
| ): PtyWebSocket = webSocket(sessionId, mode, null, false) | ||
|
|
||
| /** Resolves the WebSocket target in PTY mode, without replay or takeover. */ | ||
| fun webSocket(sessionId: String): PtyWebSocket = webSocket(sessionId, PtyMode.PTY, null, false) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.