Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 1f5b762

Browse files
committed
Improve files handling
1 parent 748f18e commit 1f5b762

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

openapi.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ paths:
9090
schema:
9191
type: string
9292
description: Contents of the file as a string
93+
'404':
94+
content:
95+
application/json:
96+
schema:
97+
properties:
98+
reason:
99+
type: string
100+
required:
101+
- reason
102+
type: object
103+
description: Response to send if the file is not found
93104
summary: Read the contents of a file at the given path
94105
put:
95106
operationId: WriteFile

src/generated/routes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function RegisterRoutes(app: Router) {
7171
const args = {
7272
env: {"default":"Nodejs","in":"query","name":"env","ref":"Environment"},
7373
path: {"in":"query","name":"path","required":true,"dataType":"string"},
74+
notFoundResponse: {"in":"res","name":"404","required":true,"dataType":"nestedObjectLiteral","nestedProperties":{"reason":{"dataType":"string","required":true}}},
7475
conversationID: {"in":"header","name":"openai-conversation-id","dataType":"string"},
7576
};
7677

src/plugin/filesController.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
Query,
77
Route,
88
Consumes,
9+
TsoaResponse,
10+
Res,
911
Body,
1012
Produces,
1113
} from 'tsoa'
@@ -22,24 +24,33 @@ export class FilesystemController extends Controller {
2224
*
2325
* @param env Environment where to read the file from
2426
* @param path Path to the file to read
27+
* @param notFoundResponse Response to send if the file is not found
2528
* @returns Contents of the file as a string
2629
*/
2730
@Get()
2831
@Produces(textPlainMIME)
2932
public async readFile(
3033
@Query() env: Environment = defaultEnvironment,
3134
@Query() path: string,
35+
@Res() notFoundResponse: TsoaResponse<404, { reason: string }>,
3236
@Header(openAIConversationIDHeader) conversationID?: string,
3337
): Promise<string> {
3438
const sessionID = getUserSessionID(conversationID, env)
3539
const session = await CachedSession.findOrStartSession({ sessionID, envID: env })
3640

3741
this.setHeader('Content-Type', textPlainMIME)
3842

39-
return await session
40-
.session
41-
.filesystem!
42-
.read(path)
43+
try {
44+
return await session
45+
.session
46+
.filesystem!
47+
.read(path)
48+
} catch (err) {
49+
console.error(err)
50+
return notFoundResponse(404, {
51+
reason: `File on path "${path}" not found`,
52+
})
53+
}
4354
}
4455

4556
/**

0 commit comments

Comments
 (0)