forked from e2b-dev/llm-code-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileController.ts
77 lines (71 loc) · 2.25 KB
/
fileController.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
Controller,
Get,
Put,
Header,
Query,
Route,
BodyProp,
TsoaResponse,
Res,
Produces,
} from 'tsoa'
import { dirname } from 'path'
import { CachedSession } from '../sessions/session'
import { openAIConversationIDHeader, textPlainMIME } from '../constants'
import { Environment, defaultEnvironment, getUserSessionID } from './environment'
@Route('files')
export class FileController extends Controller {
/**
* @summary Read the contents of a file at the given path
*
* @param env Environment where to read the file from
* @param path Path to the file to read
* @param notFoundResponse Response to send if the file is not found
* @returns Contents of the file as a string
*/
@Get()
@Produces(textPlainMIME)
public async readFile(
@Query() env: Environment = defaultEnvironment,
@Query() path: string,
@Res() notFoundResponse: TsoaResponse<404, { reason: string }>,
@Header(openAIConversationIDHeader) conversationID?: string,
): Promise<string> {
const sessionID = getUserSessionID(conversationID, env)
const session = await CachedSession.findOrStartSession({ sessionID, envID: env })
// Even though we're returning a string and using @Produces, we need to set the content type manually.
this.setHeader('Content-Type', textPlainMIME)
try {
return await session
.session
.filesystem!
.read(path)
} catch (err) {
console.error(err)
return notFoundResponse(404, {
reason: `File on path "${path}" not found`,
})
}
}
/**
* @summary Write content to a file at the given path
*
* @param env Environment where to write the file
* @param path Path to the file to write
* @param content Content to write to the file
*/
@Put()
public async writeFile(
@Query() env: Environment = defaultEnvironment,
@Query() path: string,
@BodyProp() content: string,
@Header(openAIConversationIDHeader) conversationID?: string,
) {
const sessionID = getUserSessionID(conversationID, env)
const session = await CachedSession.findOrStartSession({ sessionID, envID: env })
const dir = dirname(path)
await session.session.filesystem!.makeDir(dir)
await session.session.filesystem!.write(path, content)
}
}