Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Handle persistent processes; Cleanup
Tomas Valentab507973327d9
diff --git a/api-service/playground/src/sessions/filesystemController.ts b/api-service/playground/src/sessions/filesystemController.tsindex 301ec7818..264f30c93 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 { retrieveEntry } from './sessions'+import { CachedSession } from './session'interface ListFilesystemDirResponse {entries: EntryInfo[]@@ -27,8 +27,12 @@ export class FilesystemController extends Controller {@Path() id: string,@Query() path: string,): Promise<ListFilesystemDirResponse> {- const session = retrieveEntry(id)- const entries = await session.filesystem!.list(path)+ const entries = await CachedSession+ .findSession(id)+ .session+ .filesystem!+ .list(path)+return {entries,}@@ -39,8 +43,11 @@ export class FilesystemController extends Controller {@Path() id: string,@Query() path: string,): Promise<void> {- const session = retrieveEntry(id)- await session.filesystem!.makeDir(path)+ await CachedSession+ .findSession(id)+ .session+ .filesystem+ ?.makeDir(path)}@Delete('{id}/filesystem')@@ -48,8 +55,11 @@ export class FilesystemController extends Controller {@Path() id: string,@Query() path: string,) {- const session = retrieveEntry(id)- await session.filesystem!.remove(path)+ await CachedSession+ .findSession(id)+ .session+ .filesystem!+ .remove(path)}@Get('{id}/filesystem/file')@@ -57,8 +67,12 @@ export class FilesystemController extends Controller {@Path() id: string,@Query() path: string,): Promise<ReadFilesystemFileResponse> {- const session = retrieveEntry(id)- const content = await session.filesystem!.read(path)+ const content = await CachedSession+ .findSession(id)+ .session+ .filesystem!+ .read(path)+return {content,}@@ -70,7 +84,10 @@ export class FilesystemController extends Controller {@Query() path: string,@BodyProp('content') content: string,) {- const session = retrieveEntry(id)- await session.filesystem!.write(path, content)+ await CachedSession+ .findSession(id)+ .session+ .filesystem!+ .write(path, content)}}diff --git a/api-service/playground/src/sessions/process.ts b/api-service/playground/src/sessions/process.tsindex b790e7561..0276f8921 100644--- a/api-service/playground/src/sessions/process.ts+++ b/api-service/playground/src/sessions/process.ts@@ -3,7 +3,6 @@ import {OutStdoutResponse,ProcessManager,createSessionProcess,- Process,} from '@devbookhq/sdk'export interface RunProcessParams extends Pick<Parameters<ProcessManager['start']>[0], 'cmd' | 'envVars' | 'rootdir'> { }@@ -13,8 +12,18 @@ export class CachedProcess {readonly stderr: OutStderrResponse[] = []private started = false- process?: Process- exited?: Promise<void>++ finished = false+ process?: Awaited<ReturnType<typeof createSessionProcess>>++ get response() {+ return {+ stdout: this.stdout,+ stderr: this.stderr,+ finished: this.finished,+ processID: this.process?.processID!,+ }+ }constructor(private readonly manager: ProcessManager) { }@@ -32,6 +41,10 @@ export class CachedProcess {onStdout: stdout.push,})+ process.exited.finally(() => {+ this.finished = true+ })+this.process = processreturn process}diff --git a/api-service/playground/src/sessions/processController.ts b/api-service/playground/src/sessions/processController.tsdeleted file mode 100644index 30c68ce49..000000000--- a/api-service/playground/src/sessions/processController.ts+++ /dev/null@@ -1,98 +0,0 @@-import {- Body,- Controller,- Path,- Get,- Post,- Route,- Delete,- Query,-} from 'tsoa'-import {- ProcessManager,- createSessionProcess,- OutStderrResponse,- OutStdoutResponse,-} from '@devbookhq/sdk'--import { retrieveEntry } from './sessions'--interface RunProcessParams extends Pick<Parameters<ProcessManager['start']>[0], 'cmd' | 'envVars' | 'rootdir'> { }--interface RunProcessResponse {- stderr: OutStderrResponse[]- stdout: OutStdoutResponse[]-}--@Route('sessions')-export class ProcessController extends Controller {- @Post('{id}/process')- public async runProcess(- @Path() id: string,- @Query() wait: boolean,- @Body() requestBody: RunProcessParams,- ): Promise<RunProcessResponse> {- 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[] = []-- const process = await createSessionProcess({- manager: session.process!,- ...requestBody,- onStderr: (o) => stderr.push(o),- onStdout: (o) => stdout.push(o),- })-- 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/processesController.ts b/api-service/playground/src/sessions/processesController.tsnew file mode 100644index 000000000..25aced1f5--- /dev/null+++ b/api-service/playground/src/sessions/processesController.ts@@ -0,0 +1,106 @@+import {+ Body,+ Controller,+ Path,+ Get,+ Post,+ Route,+ Delete,+ Query,+ BodyProp,+} from 'tsoa'+import {+ ProcessManager,+ OutStderrResponse,+ OutStdoutResponse,+} from '@devbookhq/sdk'++import { CachedSession } from './session'++interface StartProcessParams extends Pick<Parameters<ProcessManager['start']>[0], 'cmd' | 'envVars' | 'rootdir'> { }++interface ProcessResponse {+ stderr: OutStderrResponse[]+ stdout: OutStdoutResponse[]+ processID: string+ finished: boolean+}++@Route('sessions')+export class ProcessController extends Controller {+ /**+ *+ * @param id+ * @param wait if true the request will wait until the process ends and then return the `stdout`, `stderr` and `processID`.+ * @param requestBody+ * @returns `processID` and all `stdout` and `stderr` that the process outputted until now.+ */+ @Post('{id}/processes')+ public async startProcess(+ @Path() id: string,+ @Body() requestBody: StartProcessParams,+ @Query() wait?: boolean,+ ): Promise<ProcessResponse> {+ const cachedProcess = await CachedSession+ .findSession(id)+ .startProcess(requestBody)++ if (wait) {+ await cachedProcess.process?.exited+ }++ return cachedProcess.response+ }++ @Delete('{id}/processes/{processID}')+ public async stopProcess(+ @Path() id: string,+ @Path() processID: string,+ @Query('results') results?: boolean+ ): Promise<ProcessResponse | undefined> {+ const cachedProcess = await CachedSession+ .findSession(id)+ .stopProcess(processID)++ if (cachedProcess && results) {+ return cachedProcess.response+ }+ }++ @Post('{id}/processes/{processID}/stdin')+ public async writeProcessStdin(+ @Path() id: string,+ @Path() processID: string,+ @BodyProp('stdin') stdin: string,+ ): Promise<void> {+ await CachedSession+ .findSession(id)+ .findProcess(processID)+ ?.process+ ?.sendStdin(stdin)+ }++ /**+ *+ * @param id+ * @param processID+ * @param wait if true the request will wait until the process ends and then return the `stdout`, `stderr` and `processID`.+ * @returns `processID` and all `stdout` and `stderr` that the process outputted until now.+ */+ @Get('{id}/processes/{processID}')+ public async getProcess(+ @Path() id: string,+ @Path() processID: string,+ @Query() wait?: boolean,+ ): Promise<ProcessResponse> {+ const cachedProcess = CachedSession+ .findSession(id)+ .findProcess(processID)++ if (wait) {+ await cachedProcess?.process?.exited+ }++ return cachedProcess?.response!+ }+}diff --git a/api-service/playground/src/sessions/session.ts b/api-service/playground/src/sessions/session.tsindex 66b87422c..a24779c80 100644--- a/api-service/playground/src/sessions/session.ts+++ b/api-service/playground/src/sessions/session.ts@@ -1,5 +1,6 @@-import { Session } from '@devbookhq/sdk'+import { OpenedPort as OpenPort, Session } from '@devbookhq/sdk'import NodeCache from 'node-cache'+import { CachedProcess, RunProcessParams } from './process'export const sessionCache = new NodeCache({@@ -20,7 +21,9 @@ sessionCache.on('expired', async function (_, cached: CachedSession) {export class CachedSession {private readonly cachedProcesses: CachedProcess[] = []- private id?: string+ ports: OpenPort[] = []++ id?: stringsession: Session/**@@ -29,7 +32,17 @@ export class CachedSession {* @param envID*/constructor(envID: string) {- this.session = new Session({ id: envID })+ this.session = new Session({+ id: envID,+ onClose: () => {+ this.delete()+ },+ codeSnippet: {+ onScanPorts: (ports) => {+ this.ports = ports+ },+ },+ })}async init() {@@ -42,7 +55,7 @@ export class CachedSession {this.id = idsessionCache.set(id, this)- return id+ return this}async delete() {@@ -55,11 +68,16 @@ export class CachedSession {}async stopProcess(processID: string) {- await this.findProcess(processID)?.process?.kill()+ const cachedProcess = this.findProcess(processID)+ if (!cachedProcess) return++ await cachedProcess.process?.kill()const idx = this.cachedProcesses.findIndex(p => p.process?.processID === processID)if (idx !== -1) {this.cachedProcesses.splice(idx, 1)}++ return cachedProcess}findProcess(processID: string) {diff --git a/api-service/playground/src/sessions/sessionsController.ts b/api-service/playground/src/sessions/sessionsController.tsindex 56d37b647..d52aef30a 100644--- a/api-service/playground/src/sessions/sessionsController.ts+++ b/api-service/playground/src/sessions/sessionsController.ts@@ -2,15 +2,18 @@ import {Controller,Delete,Path,+ Get,Post,BodyProp,Route,} from 'tsoa'-import { closeSession, initSession } from './sessions'+import { CachedSession } from './session'+import { OpenedPort as OpenPort } from '@devbookhq/sdk'-interface CreateSessionResponse {+interface SessionResponse {id: string+ ports: OpenPort[]}@Route('sessions')@@ -18,15 +21,33 @@ export class SessionsController extends Controller {@Post()public async createSessions(@BodyProp('envID') envID: string,- ): Promise<CreateSessionResponse> {- const id = await initSession(envID)- return { id }+ ): Promise<SessionResponse> {+ const cachedSession = await new CachedSession(envID).init()++ return {+ id: cachedSession.id!,+ ports: cachedSession.ports,+ }}@Delete('{id}')public async deleteSession(@Path() id: string,): Promise<void> {- await closeSession(id)+ await CachedSession+ .findSession(id)+ .delete()+ }++ @Get('{id}')+ public async getSession(+ @Path() id: string,+ ): Promise<SessionResponse> {+ const cachedSession = CachedSession.findSession(id)++ return {+ id: cachedSession.id!,+ ports: cachedSession.ports,+ }}}