返回 CodeWhale
constitution.rs
根目录 / crates / app-server / src / runtime_api / constitution.rs
1 use super::*;
2 use codewhale_config::user_constitution::ProfileConstitution;
3
4 fn model_guidance(document: &ProfileConstitution) -> Result<String, ApiError> {
5 Ok(document
6 .as_user_constitution()
7 .map_err(|error| ApiError::bad_request(error.to_string()))?
8 .render_body())
9 }
10
11 pub(super) async fn get_constitution(
12 State(state): State<RuntimeApiState>,
13 ) -> Result<Json<Value>, ApiError> {
14 let snapshot = codewhale_engine::profile_constitution::load(state.config_profile.as_deref())
15 .await
16 .map_err(|error| ApiError::conflict(error.to_string()))?;
17 match snapshot {
18 Some(snapshot) => {
19 let rendered = model_guidance(&snapshot.constitution)?;
20 Ok(Json(
21 json!({"source":"profile", "accountId":snapshot.account_id,
22 "revision":snapshot.revision, "constitution":snapshot.constitution,
23 "modelGuidance":rendered, "applies":"next_turn"}),
24 ))
25 }
26 None => {
27 #[cfg(any(test, feature = "test-support"))]
28 let env_ticket = codewhale_engine::test_support::env_scope_ticket();
29 let rendered = tokio::task::spawn_blocking(move || {
30 #[cfg(any(test, feature = "test-support"))]
31 let _membership = codewhale_engine::test_support::join_env_scope(env_ticket);
32 codewhale_engine::prompts::load_active_user_constitution()
33 .map(|document| document.render_body())
34 })
35 .await
36 .map_err(|error| ApiError::internal(error.to_string()))?;
37 Ok(Json(json!({"source":"local", "revision":null,
38 "modelGuidance":rendered, "applies":"next_turn"})))
39 }
40 }
41 }
42
43 pub(super) async fn preview_constitution(
44 Json(document): Json<ProfileConstitution>,
45 ) -> Result<Json<Value>, ApiError> {
46 let rendered = model_guidance(&document)?;
47 Ok(Json(json!({"modelGuidance":rendered, "saved":false})))
48 }
49
49 lines RUST