-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Henry Fontanier
committed
Mar 26, 2024
1 parent
5381510
commit b3db839
Showing
7 changed files
with
251 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import type { WithAPIErrorReponse } from "@dust-tt/types"; | ||
import { | ||
CreateTemplateFormSchema, | ||
isAssistantTemplateTagNameTypeArray, | ||
} from "@dust-tt/types"; | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as reporter from "io-ts-reporters"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import { USED_MODEL_CONFIGS } from "@app/components/assistant_builder/InstructionScreen"; | ||
import { Authenticator, getSession } from "@app/lib/auth"; | ||
import { TemplateResource } from "@app/lib/resources/template_resource"; | ||
import { apiError, withLogging } from "@app/logger/withlogging"; | ||
|
||
export type CreateTemplateResponseBody = { | ||
success: boolean; | ||
}; | ||
|
||
async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<WithAPIErrorReponse<CreateTemplateResponseBody>> | ||
): Promise<void> { | ||
const session = await getSession(req, res); | ||
const auth = await Authenticator.fromSuperUserSession(session, null); | ||
|
||
if (!auth.isDustSuperUser()) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "user_not_found", | ||
message: "Could not find the user.", | ||
}, | ||
}); | ||
} | ||
|
||
switch (req.method) { | ||
case "POST": | ||
const bodyValidation = CreateTemplateFormSchema.decode(req.body); | ||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `The request body is invalid: ${pathError}`, | ||
}, | ||
}); | ||
} | ||
const body = bodyValidation.right; | ||
|
||
if (!isAssistantTemplateTagNameTypeArray(body.tags)) { | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: | ||
"The request body is invalid: tags must be an array of template tag names.", | ||
}, | ||
}); | ||
} | ||
|
||
const model = USED_MODEL_CONFIGS.find( | ||
(config) => config.modelId === body.presetModel | ||
); | ||
|
||
if (!model) { | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: "The request body is invalid: model not found.", | ||
}, | ||
}); | ||
} | ||
|
||
await TemplateResource.makeNew({ | ||
sId: uuidv4(), | ||
name: body.name, | ||
description: body.description ?? null, | ||
presetHandle: body.presetHandle ?? null, | ||
presetInstructions: body.presetInstructions ?? null, | ||
presetTemperature: body.presetTemperature ?? null, | ||
presetAction: body.presetAction, | ||
helpInstructions: body.helpInstructions ?? null, | ||
helpActions: body.helpActions ?? null, | ||
tags: body.tags, | ||
presetProviderId: model.providerId, | ||
presetModelId: model.modelId, | ||
// TODO | ||
visibility: "draft", | ||
presetDescription: null, | ||
}); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
}); | ||
break; | ||
|
||
default: | ||
return apiError(req, res, { | ||
status_code: 405, | ||
api_error: { | ||
type: "method_not_supported_error", | ||
message: "The method passed is not supported, POST is expected.", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export default withLogging(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.