forked from e2b-dev/llm-code-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandController.ts
60 lines (54 loc) · 1.51 KB
/
commandController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
Controller,
Post,
Header,
BodyProp,
Route,
Query,
} from 'tsoa'
import { CachedSession } from '../sessions/session'
import { openAIConversationIDHeader } from '../constants'
import { Environment, defaultEnvironment, getUserSessionID } from './environment'
/**
*
*/
interface CommandResponse {
/**
* Standard error output from the command
*/
stderr: string
/**
* Standard output from the command
*/
stdout: string
}
@Route('commands')
export class CommandController extends Controller {
/**
* @summary Run a command in a shell
*
* @param env Environment to run the command in
* @param command Command to run
* @param workDir Working directory to run the command in
* @returns JSON containing the standard output and error output of the command
*/
@Post()
public async runCommand(
@BodyProp() command: string,
@BodyProp() workDir: string,
@Query() env: Environment = defaultEnvironment,
@Header(openAIConversationIDHeader) conversationID?: string,
): Promise<CommandResponse> {
const sessionID = getUserSessionID(conversationID, env)
const session = await CachedSession.findOrStartSession({ sessionID, envID: env })
const cachedProcess = await session.startProcess({
cmd: command,
rootdir: workDir,
})
await cachedProcess.process?.exited
return {
stderr: cachedProcess.response.stderr.map(({ line }) => line).join('\n'),
stdout: cachedProcess.response.stdout.map(({ line }) => line).join('\n'),
}
}
}