Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Handle starting server in playground [WIP]

Tomas Valenta56229838e30a
diff --git a/api-service/playground/src/sessions/filesystemController.ts b/api-service/playground/src/sessions/filesystemController.ts
index 6e52a5e27..301ec7818 100644
--- a/api-service/playground/src/sessions/filesystemController.ts
+++ b/api-service/playground/src/sessions/filesystemController.ts
@@ -10,7 +10,7 @@ import {
} from 'tsoa'
import { FileInfo as EntryInfo } from '@devbookhq/sdk'
-import { retrieveSession } from './sessions'
+import { retrieveEntry } from './sessions'
interface ListFilesystemDirResponse {
entries: EntryInfo[]
@@ -27,7 +27,7 @@ export class FilesystemController extends Controller {
@Path() id: string,
@Query() path: string,
): Promise<ListFilesystemDirResponse> {
- const session = retrieveSession(id)
+ const session = retrieveEntry(id)
const entries = await session.filesystem!.list(path)
return {
entries,
@@ -39,7 +39,7 @@ export class FilesystemController extends Controller {
@Path() id: string,
@Query() path: string,
): Promise<void> {
- const session = retrieveSession(id)
+ const session = retrieveEntry(id)
await session.filesystem!.makeDir(path)
}
@@ -48,7 +48,7 @@ export class FilesystemController extends Controller {
@Path() id: string,
@Query() path: string,
) {
- const session = retrieveSession(id)
+ const session = retrieveEntry(id)
await session.filesystem!.remove(path)
}
@@ -57,7 +57,7 @@ export class FilesystemController extends Controller {
@Path() id: string,
@Query() path: string,
): Promise<ReadFilesystemFileResponse> {
- const session = retrieveSession(id)
+ const session = retrieveEntry(id)
const content = await session.filesystem!.read(path)
return {
content,
@@ -70,7 +70,7 @@ export class FilesystemController extends Controller {
@Query() path: string,
@BodyProp('content') content: string,
) {
- const session = retrieveSession(id)
+ const session = retrieveEntry(id)
await session.filesystem!.write(path, content)
}
}
diff --git a/api-service/playground/src/sessions/processController.ts b/api-service/playground/src/sessions/processController.ts
index 182e167e5..30c68ce49 100644
--- a/api-service/playground/src/sessions/processController.ts
+++ b/api-service/playground/src/sessions/processController.ts
@@ -2,8 +2,11 @@ import {
Body,
Controller,
Path,
+ Get,
Post,
Route,
+ Delete,
+ Query,
} from 'tsoa'
import {
ProcessManager,
@@ -12,7 +15,7 @@ import {
OutStdoutResponse,
} from '@devbookhq/sdk'
-import { retrieveSession } from './sessions'
+import { retrieveEntry } from './sessions'
interface RunProcessParams extends Pick<Parameters<ProcessManager['start']>[0], 'cmd' | 'envVars' | 'rootdir'> { }
@@ -26,9 +29,37 @@ export class ProcessController extends Controller {
@Post('{id}/process')
public async runProcess(
@Path() id: string,
+ @Query() wait: boolean,
@Body() requestBody: RunProcessParams,
): Promise<RunProcessResponse> {
- const session = retrieveSession(id)
+ const session = retrieveEntry(id)
+
+ const stderr: OutStderrResponse[] = []
+ const stdout: OutStdoutResponse[] = []
+
+ const process = await createSessionProcess({
+ manager: session.process!,
+ ...requestBody,
+ onStderr: (o) => stderr.push(o),
+ onStdout: (o) => stdout.push(o),
+ })
+
+ if (wait) {
+ await process.exited
+ }
+
+ return {
+ stdout,
+ stderr,
+ }
+ }
+
+ @Delete('{id}/process/{processID}')
+ public async stopProcess(
+ @Path() id: string,
+ @Path() processID: string,
+ ): Promise<RunProcessResponse> {
+ const session = retrieveEntry(id)
const stderr: OutStderrResponse[] = []
const stdout: OutStdoutResponse[] = []
@@ -41,6 +72,24 @@ export class ProcessController extends Controller {
})
await process.exited
+ return {
+ stdout,
+ stderr,
+ }
+ }
+
+
+
+ @Get('{id}/process/{pid}/output')
+ public async getProcessOutput(
+ @Path() id: string,
+ @Path() processID: string,
+ ): Promise<RunProcessResponse> {
+ const session = retrieveEntry(id)
+
+
+
+
return {
stdout,
stderr,
diff --git a/api-service/playground/src/sessions/sessions.ts b/api-service/playground/src/sessions/sessions.ts
index d54144b62..e0495b0e1 100644
--- a/api-service/playground/src/sessions/sessions.ts
+++ b/api-service/playground/src/sessions/sessions.ts
@@ -1,4 +1,4 @@
-import { Session } from '@devbookhq/sdk'
+import { OutStderrResponse, OutStdoutResponse, Process, Session } from '@devbookhq/sdk'
import NodeCache from 'node-cache'
export const sessionCache = new NodeCache({
@@ -8,9 +8,20 @@ export const sessionCache = new NodeCache({
deleteOnExpire: true,
})
-sessionCache.on('del', async function (_, session: Session) {
+interface CachedProcess {
+ process: Process
+ stdout: OutStdoutResponse[]
+ stderr: OutStderrResponse[]
+}
+
+interface CacheEntry {
+ session: Session
+ processes?: CachedProcess[]
+}
+
+sessionCache.on('del', async function (_, entry: CacheEntry) {
try {
- await session.close()
+ await entry.session.close()
} catch (err) {
console.error(err)
}
@@ -25,21 +36,26 @@ export async function initSession(envID: string) {
if (!url) throw new Error('Cannot start session')
const [id,] = url.split('.')
- sessionCache.set(id, session)
+
+ const entry: CacheEntry = {
+ session,
+ }
+
+ sessionCache.set(id, entry)
return id
}
-export function retrieveSession(id: string) {
- const session = sessionCache.get(id) as Session | undefined
- if (!session) throw new Error('Session does not exist')
+export function retrieveEntry(id: string) {
+ const entry = sessionCache.get(id) as CacheEntry | undefined
+ if (!entry) throw new Error('Session does not exist')
- return session
+ return entry
}
export async function closeSession(id: string) {
- const session = retrieveSession(id)
- if (session) {
- await session.close()
+ const entry = retrieveEntry(id)
+ if (entry) {
+ await entry.session.close()
sessionCache.del(id)
}
}