-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathcourse.ex
152 lines (132 loc) · 4.72 KB
/
course.ex
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
defmodule Cadet.Courses.Course do
@moduledoc """
The Course entity stores the configuration of a particular course.
"""
use Cadet, :model
alias Cadet.Courses.AssessmentConfig
@type t :: %__MODULE__{
course_name: String.t(),
course_short_name: String.t(),
viewable: boolean(),
enable_game: boolean(),
enable_achievements: boolean(),
enable_sourcecast: boolean(),
enable_stories: boolean(),
enable_llm_grading: boolean(),
llm_api_key: String.t() | nil,
source_chapter: integer(),
source_variant: String.t(),
module_help_text: String.t(),
assets_prefix: String.t() | nil
}
schema "courses" do
field(:course_name, :string)
field(:course_short_name, :string)
field(:viewable, :boolean, default: true)
field(:enable_game, :boolean, default: true)
field(:enable_achievements, :boolean, default: true)
field(:enable_sourcecast, :boolean, default: true)
field(:enable_stories, :boolean, default: false)
field(:enable_llm_grading, :boolean)
field(:llm_api_key, :string)
field(:source_chapter, :integer)
field(:source_variant, :string)
field(:module_help_text, :string)
# for now, only settable from database
field(:assets_prefix, :string, default: nil)
has_many(:assessment_config, AssessmentConfig)
timestamps()
end
@required_fields ~w(course_name viewable enable_game
enable_achievements enable_sourcecast enable_stories source_chapter source_variant)a
@optional_fields ~w(course_short_name module_help_text enable_llm_grading llm_api_key)a
@spec changeset(
{map(), map()}
| %{
:__struct__ => atom() | %{:__changeset__ => map(), optional(any()) => any()},
optional(atom()) => any()
},
%{optional(:__struct__) => none(), optional(atom() | binary()) => any()}
) :: Ecto.Changeset.t()
def changeset(course, params) do
course
|> cast(params, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> validate_sublanguage_combination(params)
|> put_encrypted_llm_api_key()
end
def put_encrypted_llm_api_key(changeset) do
if llm_api_key = get_change(changeset, :llm_api_key) do
if is_binary(llm_api_key) and llm_api_key != "" do
secret = Application.get_env(:openai, :encryption_key)
if is_binary(secret) and byte_size(secret) >= 16 do
# Use first 16 bytes for AES-128, 24 for AES-192, or 32 for AES-256
key = binary_part(secret, 0, min(32, byte_size(secret)))
# Use AES in GCM mode for encryption
iv = :crypto.strong_rand_bytes(16)
{ciphertext, tag} =
:crypto.crypto_one_time_aead(
:aes_gcm,
key,
iv,
llm_api_key,
"",
true
)
# Store both the IV, ciphertext and tag
encrypted = iv <> tag <> ciphertext
put_change(changeset, :llm_api_key, Base.encode64(encrypted))
else
add_error(changeset, :llm_api_key, "encryption key not configured properly")
end
else
# If empty string or nil is provided, don't encrypt but don't add error
changeset
end
else
# The key is not being changed, so we need to preserve the existing value
put_change(changeset, :llm_api_key, changeset.data.llm_api_key)
end
end
# Validates combination of Source chapter and variant
defp validate_sublanguage_combination(changeset, params) do
chap = Map.has_key?(params, :source_chapter)
var = Map.has_key?(params, :source_variant)
# not (chap xor var)
case {chap, var} do
{true, true} ->
case get_field(changeset, :source_chapter) do
1 ->
validate_inclusion(changeset, :source_variant, [
"default",
"lazy",
"wasm",
"native",
"typed"
])
2 ->
validate_inclusion(changeset, :source_variant, ["default", "lazy", "native", "typed"])
3 ->
validate_inclusion(changeset, :source_variant, [
"default",
"concurrent",
"non-det",
"native",
"typed"
])
4 ->
validate_inclusion(changeset, :source_variant, ["default", "gpu", "native"])
_ ->
add_error(changeset, :source_chapter, "is invalid")
end
{false, false} ->
changeset
{_, _} ->
add_error(
changeset,
:source_chapter,
"source chapter and source variant must be present together"
)
end
end
end