CI / UI Checks (pull_request) Successful in 15s
CI / Rust Checks (pull_request) Failing after 4m58s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
mcp-server запускает сохранённые фоновые задачи: захват по PostgreSQL lease с fence-токеном, зашифрованный input и повторная проверка доступа до сетевого вызова, checkpoint с AttemptID до dispatch и восстановление только по PG. Устаревший worker не может зафиксировать результат. Добавлены миграции v26–v28 (V25 не меняется), guard их отсутствия в старой схеме, метрики/трассы попыток (attempt_trace) и остановка до отправки запроса: стоп в окне после checkpoint и до permit завершает задачу как failed(shutdown_before_dispatch) без сетевых вызовов, соседние queued-задачи остаются доступными. Retry-попытки получают отдельные дочерние spans с durable current_attempt_id. Shared admission/fairness из Story 4.1 сохранены. Проверки: durable_tasks 13/13, stages 11/11 (retry 5), runtime lib 47, bin shutdown 1, registry tasks 16, admission 11, migrations 58 (независимая QA и исходный runtime-прогон), точный exact-head CI остаётся обязательным.
929 lines
32 KiB
Rust
929 lines
32 KiB
Rust
mod admission;
|
|
mod agent;
|
|
mod agent_catalog;
|
|
mod agent_prompt_bindings;
|
|
mod agent_resource_bindings;
|
|
mod api_key;
|
|
mod approval;
|
|
mod artifact_source;
|
|
mod auth;
|
|
mod connection;
|
|
mod import_job;
|
|
mod master_key;
|
|
mod master_key_tasks;
|
|
mod observability;
|
|
mod onboarding;
|
|
mod onboarding_import;
|
|
mod operation;
|
|
mod operation_artifact;
|
|
mod operation_published;
|
|
mod pool_config;
|
|
mod product_event;
|
|
mod prompt;
|
|
mod resource;
|
|
mod secret;
|
|
mod tasks;
|
|
mod upstream;
|
|
mod workspace;
|
|
mod yaml_import;
|
|
|
|
use crank_core::{
|
|
AgentId, AgentOperationBinding, AgentStatus, AgentVersion, ApprovalRequest, ApprovalRequestId,
|
|
AuthProfile, HttpMethod, InvitationId, InvitationToken, InvocationLog, InvocationLogId,
|
|
MembershipRole, OperationId, OperationStatus, PlatformApiKey, PlatformApiKeyId,
|
|
PlatformApiKeyStatus, ProductEventId, Secret, SecretId, SecretVersion, Target, UsageRollup,
|
|
User, UserId, UserSessionId, Workspace, WorkspaceId,
|
|
};
|
|
use serde::{Serialize, de::DeserializeOwned};
|
|
use serde_json::Value;
|
|
use sqlx::{PgPool, Postgres, Row, Transaction, postgres::PgRow, types::Json};
|
|
use time::OffsetDateTime;
|
|
|
|
pub use admission::{
|
|
AdmissionLimits, PostgresExecutionAdmission, admission_health, admission_snapshot,
|
|
};
|
|
pub use pool_config::{PostgresPoolConfig, PostgresPoolConfigError};
|
|
pub use resource::{AuthorizedResourceRead, BoundResourceAdmission};
|
|
pub use tasks::PostgresTaskStore;
|
|
pub use tasks::tasks_health;
|
|
|
|
use crate::{
|
|
error::RegistryError,
|
|
model::{
|
|
AdminBootstrapContractRecord, AdminSecurityAuditRequest, AgentPromptBinding,
|
|
AgentResourceBinding, AgentSummary, AgentVersionRecord, AppendProductEventOutcome,
|
|
AppendProductEventRequest, AppliedImportOperation, ApplyImportJobRequest,
|
|
ApprovalRequestRecord, AuthUserRecord, ConsumeAdminBootstrapContractRequest,
|
|
CreateAdminBootstrapContractRequest, CreateAgentDraftVersionRequest, CreateAgentRequest,
|
|
CreateApprovalRequest, CreateImportJobRequest, CreateInvitationRequest,
|
|
CreateInvocationLogRequest, CreatePlatformApiKeyRequest, CreateSecretRequest,
|
|
CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest,
|
|
DecideApprovalRequest, DescriptorMetadata, ExpireApprovalRequest, FinishApprovalRequest,
|
|
FinishImportJobRequest, ImportConflictMode, ImportJob, ImportJobApplyResult,
|
|
ImportJobCleanupReport, ImportJobId, ImportJobStatus, InvitationRecord,
|
|
InvocationHistoryLoss, InvocationHistoryLossCategory, InvocationHistoryWriteOutcome,
|
|
InvocationLogRecord, InvocationRetentionOutcome, InvocationRetentionPolicy,
|
|
InvocationRetentionStatus, ListApprovalRequestsQuery, ListInvocationLogsQuery,
|
|
ListProductEventsQuery, MasterKeyIdentityCandidate, MasterKeyIdentityRecord,
|
|
MasterKeyRotationRecord, MasterKeyRotationStatus, MembershipRecord,
|
|
OnboardingEventSnapshot, OnboardingImportChoice, OnboardingImportContext,
|
|
OnboardingImportDrift, OnboardingMilestoneResult, OnboardingPresentationMilestone,
|
|
OnboardingSnapshot, OperationAgentRef, OperationSampleMetadata, OperationStateExpectation,
|
|
OperationSummary, OperationUsageSummary, OperationVersionRecord, PlatformApiKeyRecord,
|
|
ProductEventRecord, PublishAgentRequest, PublishRequest, PublishedAgentCatalog,
|
|
PublishedAgentTool, RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest,
|
|
RegisterOnboardingImportContextRequest, RegistryOperation, RotateSecretRequest,
|
|
SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest,
|
|
SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest,
|
|
SecretRecord, SecretVersionRecord, SelectOnboardingImportDraftRequest, SessionRecord,
|
|
SkippedImportOperation, UpdateWorkspaceRequest, UsageAgentBreakdown,
|
|
UsageOperationBreakdown, UsageOutcomeBreakdown, UsageQuery, UsageRollupRecord,
|
|
UsageSummary, UsageTimelinePoint, WorkspaceMembershipRecord, WorkspaceRecord,
|
|
WorkspaceUpstream, YamlImportJob, YamlImportJobCompletion, YamlImportJobId,
|
|
YamlImportJobStatus,
|
|
},
|
|
};
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct PostgresRegistry {
|
|
pool: PgPool,
|
|
}
|
|
|
|
impl PostgresRegistry {
|
|
async fn list_agent_bindings(
|
|
&self,
|
|
agent_id: &AgentId,
|
|
version: u32,
|
|
) -> Result<Vec<AgentOperationBinding>, RegistryError> {
|
|
let rows = sqlx::query(
|
|
"select
|
|
operation_id,
|
|
operation_version,
|
|
tool_name,
|
|
tool_title,
|
|
tool_description_override,
|
|
enabled
|
|
from agent_operation_bindings
|
|
where agent_id = $1 and agent_version = $2
|
|
order by tool_name asc",
|
|
)
|
|
.bind(agent_id.as_str())
|
|
.bind(to_db_version(version))
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
|
|
rows.iter().map(map_agent_binding).collect()
|
|
}
|
|
}
|
|
|
|
async fn insert_version_row(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
snapshot: &RegistryOperation,
|
|
change_note: Option<&str>,
|
|
created_by: Option<&str>,
|
|
) -> Result<(), RegistryError> {
|
|
sqlx::query(
|
|
"insert into operation_versions (
|
|
operation_id,
|
|
version,
|
|
status,
|
|
target_json,
|
|
input_schema_json,
|
|
output_schema_json,
|
|
input_mapping_json,
|
|
output_mapping_json,
|
|
execution_config_json,
|
|
tool_description_json,
|
|
samples_json,
|
|
generated_draft_json,
|
|
config_export_json,
|
|
wizard_state_json,
|
|
name,
|
|
display_name,
|
|
category,
|
|
protocol,
|
|
security_level,
|
|
snapshot_provenance,
|
|
snapshot_observed_at,
|
|
published_at,
|
|
published_by,
|
|
change_note,
|
|
created_at,
|
|
created_by
|
|
) values (
|
|
$1, $2, $3, $4, $5, $6, $7, $8,
|
|
$9, $10, $11, $12, $13, $14,
|
|
$15, $16, $17, $18, $19, 'native_v4', $20::timestamptz,
|
|
$21::timestamptz, $22, $23, $24::timestamptz, $25
|
|
)",
|
|
)
|
|
.bind(snapshot.id.as_str())
|
|
.bind(to_db_version(snapshot.version))
|
|
.bind(serialize_enum_text(&snapshot.status, "status")?)
|
|
.bind(Json(serialize_json_value(&snapshot.target)?))
|
|
.bind(Json(serialize_json_value(&snapshot.input_schema)?))
|
|
.bind(Json(serialize_json_value(&snapshot.output_schema)?))
|
|
.bind(Json(serialize_json_value(&snapshot.input_mapping)?))
|
|
.bind(Json(serialize_json_value(&snapshot.output_mapping)?))
|
|
.bind(Json(serialize_json_value(&snapshot.execution_config)?))
|
|
.bind(Json(serialize_json_value(&snapshot.tool_description)?))
|
|
.bind(serialize_option_json_value(&snapshot.samples)?.map(Json))
|
|
.bind(serialize_option_json_value(&snapshot.generated_draft)?.map(Json))
|
|
.bind(serialize_option_json_value(&snapshot.config_export)?.map(Json))
|
|
.bind(serialize_option_json_value(&snapshot.wizard_state)?.map(Json))
|
|
.bind(&snapshot.name)
|
|
.bind(&snapshot.display_name)
|
|
.bind(&snapshot.category)
|
|
.bind(serialize_enum_text(&snapshot.protocol, "protocol")?)
|
|
.bind(serialize_enum_text(
|
|
&snapshot.security_level,
|
|
"security_level",
|
|
)?)
|
|
.bind(snapshot.updated_at)
|
|
.bind(snapshot.published_at)
|
|
.bind(Option::<&str>::None)
|
|
.bind(change_note)
|
|
.bind(snapshot.updated_at)
|
|
.bind(created_by)
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn insert_operation_rows(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
workspace_id: &WorkspaceId,
|
|
snapshot: &RegistryOperation,
|
|
created_by: Option<&str>,
|
|
) -> Result<(), RegistryError> {
|
|
sqlx::query(
|
|
"insert into operations (
|
|
id,
|
|
workspace_id,
|
|
name,
|
|
display_name,
|
|
category,
|
|
protocol,
|
|
security_level,
|
|
status,
|
|
current_draft_version,
|
|
latest_published_version,
|
|
created_at,
|
|
updated_at,
|
|
published_at
|
|
) values (
|
|
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
|
|
$11::timestamptz,
|
|
$12::timestamptz,
|
|
$13::timestamptz
|
|
)",
|
|
)
|
|
.bind(snapshot.id.as_str())
|
|
.bind(workspace_id.as_str())
|
|
.bind(&snapshot.name)
|
|
.bind(&snapshot.display_name)
|
|
.bind(&snapshot.category)
|
|
.bind(serialize_enum_text(&snapshot.protocol, "protocol")?)
|
|
.bind(serialize_enum_text(
|
|
&snapshot.security_level,
|
|
"security_level",
|
|
)?)
|
|
.bind(serialize_enum_text(&snapshot.status, "status")?)
|
|
.bind(to_db_version(snapshot.version))
|
|
.bind(
|
|
snapshot
|
|
.published_at
|
|
.as_ref()
|
|
.map(|_| to_db_version(snapshot.version)),
|
|
)
|
|
.bind(snapshot.created_at)
|
|
.bind(snapshot.updated_at)
|
|
.bind(snapshot.published_at)
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
|
|
insert_version_row(tx, snapshot, None, created_by).await
|
|
}
|
|
|
|
async fn insert_agent_version_row(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
version: &AgentVersion,
|
|
) -> Result<(), RegistryError> {
|
|
sqlx::query(
|
|
"insert into agent_versions (
|
|
agent_id,
|
|
version,
|
|
status,
|
|
instructions_json,
|
|
tool_selection_policy_json,
|
|
created_at
|
|
) values (
|
|
$1, $2, $3, $4, $5, $6::timestamptz
|
|
)",
|
|
)
|
|
.bind(version.agent_id.as_str())
|
|
.bind(to_db_version(version.version))
|
|
.bind(serialize_enum_text(&version.status, "status")?)
|
|
.bind(Json(version.instructions.clone()))
|
|
.bind(Json(serialize_json_value(&version.tool_selection_policy)?))
|
|
.bind(version.created_at)
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn replace_agent_bindings_rows(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
workspace_id: &WorkspaceId,
|
|
agent_id: &AgentId,
|
|
version: u32,
|
|
bindings: &[AgentOperationBinding],
|
|
) -> Result<(), RegistryError> {
|
|
let operation_ids = bindings
|
|
.iter()
|
|
.map(|binding| binding.operation_id.as_str())
|
|
.collect::<std::collections::BTreeSet<_>>();
|
|
for operation_id in operation_ids {
|
|
let status = sqlx::query_scalar::<_, String>(
|
|
"select status from operations
|
|
where workspace_id = $1 and id = $2
|
|
for update",
|
|
)
|
|
.bind(workspace_id.as_str())
|
|
.bind(operation_id)
|
|
.fetch_optional(&mut **tx)
|
|
.await?;
|
|
match status.as_deref() {
|
|
Some("archived") => {
|
|
return Err(RegistryError::OperationArchived {
|
|
operation_id: operation_id.to_owned(),
|
|
});
|
|
}
|
|
Some(_) => {}
|
|
None => {
|
|
return Err(RegistryError::OperationNotFound {
|
|
operation_id: operation_id.to_owned(),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
sqlx::query(
|
|
"delete from agent_operation_bindings
|
|
where agent_id = $1 and agent_version = $2",
|
|
)
|
|
.bind(agent_id.as_str())
|
|
.bind(to_db_version(version))
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
|
|
for binding in bindings {
|
|
sqlx::query(
|
|
"insert into agent_operation_bindings (
|
|
agent_id,
|
|
agent_version,
|
|
operation_id,
|
|
operation_version,
|
|
tool_name,
|
|
tool_title,
|
|
tool_description_override,
|
|
enabled
|
|
) values ($1, $2, $3, $4, $5, $6, $7, $8)",
|
|
)
|
|
.bind(agent_id.as_str())
|
|
.bind(to_db_version(version))
|
|
.bind(binding.operation_id.as_str())
|
|
.bind(to_db_version(binding.operation_version))
|
|
.bind(&binding.tool_name)
|
|
.bind(&binding.tool_title)
|
|
.bind(binding.tool_description_override.as_deref())
|
|
.bind(binding.enabled)
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn build_user(
|
|
id: String,
|
|
email: String,
|
|
display_name: String,
|
|
status: String,
|
|
created_at: OffsetDateTime,
|
|
) -> Result<User, RegistryError> {
|
|
Ok(User {
|
|
id: UserId::new(id),
|
|
email,
|
|
display_name,
|
|
status: deserialize_enum_text(&status, "status")?,
|
|
created_at,
|
|
})
|
|
}
|
|
|
|
fn map_user_update_error(error: sqlx::Error, user_id: &UserId, email: &str) -> RegistryError {
|
|
match error {
|
|
sqlx::Error::RowNotFound => RegistryError::UserNotFound {
|
|
user_id: user_id.as_str().to_owned(),
|
|
},
|
|
sqlx::Error::Database(database_error)
|
|
if database_error.code().as_deref() == Some("23505") =>
|
|
{
|
|
RegistryError::UserEmailAlreadyExists {
|
|
email: email.to_owned(),
|
|
}
|
|
}
|
|
other => RegistryError::Storage(other),
|
|
}
|
|
}
|
|
|
|
fn map_invocation_log_record(row: &PgRow) -> Result<InvocationLogRecord, RegistryError> {
|
|
Ok(InvocationLogRecord {
|
|
log: InvocationLog {
|
|
id: InvocationLogId::new(row.try_get::<String, _>("id")?),
|
|
workspace_id: WorkspaceId::new(row.try_get::<String, _>("workspace_id")?),
|
|
agent_id: row
|
|
.try_get::<Option<String>, _>("agent_id")?
|
|
.map(AgentId::new),
|
|
platform_api_key_id: row
|
|
.try_get::<Option<String>, _>("platform_api_key_id")?
|
|
.map(PlatformApiKeyId::new),
|
|
operation_id: OperationId::new(row.try_get::<String, _>("operation_id")?),
|
|
operation_version: row
|
|
.try_get::<Option<i32>, _>("operation_version")?
|
|
.map(|value| {
|
|
u32::try_from(value).map_err(|_| RegistryError::InvalidNumericValue {
|
|
field: "operation_version",
|
|
value: i64::from(value),
|
|
})
|
|
})
|
|
.transpose()?,
|
|
source: deserialize_enum_text(&row.try_get::<String, _>("source")?, "source")?,
|
|
level: deserialize_enum_text(&row.try_get::<String, _>("level")?, "level")?,
|
|
status: deserialize_enum_text(&row.try_get::<String, _>("status")?, "status")?,
|
|
tool_name: row.try_get("tool_name")?,
|
|
message: row.try_get("message")?,
|
|
request_id: row.try_get("request_id")?,
|
|
trace_id: row.try_get("trace_id")?,
|
|
status_code: match row.try_get::<Option<i32>, _>("status_code")? {
|
|
Some(value) => {
|
|
Some(
|
|
u16::try_from(value).map_err(|_| RegistryError::InvalidNumericValue {
|
|
field: "status_code",
|
|
value: i64::from(value),
|
|
})?,
|
|
)
|
|
}
|
|
None => None,
|
|
},
|
|
duration_ms: to_u64(row.try_get::<i64, _>("duration_ms")?, "duration_ms")?,
|
|
error_kind: row.try_get("error_kind")?,
|
|
execution_stage: row
|
|
.try_get::<Option<String>, _>("execution_stage")?
|
|
.map(|value| deserialize_enum_text(&value, "execution_stage"))
|
|
.transpose()?,
|
|
execution_error_code: row
|
|
.try_get::<Option<String>, _>("execution_error_code")?
|
|
.map(|value| deserialize_enum_text(&value, "execution_error_code"))
|
|
.transpose()?,
|
|
retryability: row
|
|
.try_get::<Option<String>, _>("retryability")?
|
|
.map(|value| deserialize_enum_text(&value, "retryability"))
|
|
.transpose()?,
|
|
outcome_certainty: row
|
|
.try_get::<Option<String>, _>("outcome_certainty")?
|
|
.map(|value| deserialize_enum_text(&value, "outcome_certainty"))
|
|
.transpose()?,
|
|
request_preview: row.try_get::<Json<Value>, _>("request_preview_json")?.0,
|
|
response_preview: row.try_get::<Json<Value>, _>("response_preview_json")?.0,
|
|
created_at: row.try_get("created_at")?,
|
|
},
|
|
operation_name: row.try_get("operation_name")?,
|
|
operation_display_name: row.try_get("operation_display_name")?,
|
|
agent_slug: row.try_get("agent_slug")?,
|
|
agent_display_name: row.try_get("agent_display_name")?,
|
|
})
|
|
}
|
|
|
|
fn map_operation_usage_summary(row: &PgRow) -> Result<OperationUsageSummary, RegistryError> {
|
|
build_operation_usage_summary(
|
|
row.try_get("operation_id")?,
|
|
row.try_get("calls_today")?,
|
|
row.try_get("error_rate_pct")?,
|
|
row.try_get("avg_latency_ms")?,
|
|
)
|
|
}
|
|
|
|
fn target_summary(target: &Target) -> (String, String) {
|
|
match target {
|
|
Target::Rest(rest) => (
|
|
join_target_url(&rest.base_url, &rest.path_template),
|
|
match rest.method {
|
|
HttpMethod::Get => "GET",
|
|
HttpMethod::Post => "POST",
|
|
HttpMethod::Put => "PUT",
|
|
HttpMethod::Patch => "PATCH",
|
|
HttpMethod::Delete => "DELETE",
|
|
HttpMethod::Head => "HEAD",
|
|
HttpMethod::Options => "OPTIONS",
|
|
}
|
|
.to_owned(),
|
|
),
|
|
}
|
|
}
|
|
|
|
fn join_target_url(base: &str, path: &str) -> String {
|
|
if path.is_empty() {
|
|
return base.to_owned();
|
|
}
|
|
|
|
if path.starts_with("http://") || path.starts_with("https://") {
|
|
return path.to_owned();
|
|
}
|
|
|
|
let trimmed_base = base.trim_end_matches('/');
|
|
let trimmed_path = path.trim_start_matches('/');
|
|
|
|
if trimmed_base.is_empty() {
|
|
format!("/{trimmed_path}")
|
|
} else if trimmed_path.is_empty() {
|
|
trimmed_base.to_owned()
|
|
} else {
|
|
format!("{trimmed_base}/{trimmed_path}")
|
|
}
|
|
}
|
|
|
|
fn map_agent_binding(row: &PgRow) -> Result<AgentOperationBinding, RegistryError> {
|
|
Ok(AgentOperationBinding {
|
|
operation_id: OperationId::new(row.try_get::<String, _>("operation_id")?),
|
|
operation_version: from_db_version(row.try_get("operation_version")?, "operation_version")?,
|
|
tool_name: row.try_get("tool_name")?,
|
|
tool_title: row.try_get("tool_title")?,
|
|
tool_description_override: row.try_get("tool_description_override")?,
|
|
enabled: row.try_get("enabled")?,
|
|
})
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn build_agent_summary(
|
|
id: String,
|
|
workspace_id: String,
|
|
slug: String,
|
|
display_name: String,
|
|
description: String,
|
|
status: String,
|
|
current_draft_version: i32,
|
|
latest_published_version: Option<i32>,
|
|
catalog_revision: i64,
|
|
created_at: OffsetDateTime,
|
|
updated_at: OffsetDateTime,
|
|
published_at: Option<OffsetDateTime>,
|
|
) -> Result<AgentSummary, RegistryError> {
|
|
Ok(AgentSummary {
|
|
id: AgentId::new(id),
|
|
workspace_id: WorkspaceId::new(workspace_id),
|
|
slug,
|
|
display_name,
|
|
description,
|
|
status: deserialize_enum_text(&status, "status")?,
|
|
current_draft_version: from_db_version(current_draft_version, "current_draft_version")?,
|
|
latest_published_version: latest_published_version
|
|
.map(|value| from_db_version(value, "latest_published_version"))
|
|
.transpose()?,
|
|
catalog_revision,
|
|
created_at,
|
|
updated_at,
|
|
published_at,
|
|
})
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn build_operation_summary(
|
|
id: String,
|
|
workspace_id: String,
|
|
name: String,
|
|
display_name: String,
|
|
category: String,
|
|
protocol: String,
|
|
security_level: String,
|
|
target_json: Value,
|
|
status: String,
|
|
current_draft_version: i32,
|
|
latest_published_version: Option<i32>,
|
|
can_delete: bool,
|
|
created_at: OffsetDateTime,
|
|
updated_at: OffsetDateTime,
|
|
published_at: Option<OffsetDateTime>,
|
|
) -> Result<OperationSummary, RegistryError> {
|
|
let target: Target = deserialize_json_value(target_json)?;
|
|
let (target_url, target_action) = target_summary(&target);
|
|
|
|
Ok(OperationSummary {
|
|
id: OperationId::new(id),
|
|
workspace_id: WorkspaceId::new(workspace_id),
|
|
name,
|
|
display_name,
|
|
category,
|
|
protocol: deserialize_enum_text(&protocol, "protocol")?,
|
|
security_level: deserialize_enum_text(&security_level, "security_level")?,
|
|
target_url,
|
|
target_action,
|
|
status: deserialize_enum_text(&status, "status")?,
|
|
current_draft_version: from_db_version(current_draft_version, "current_draft_version")?,
|
|
latest_published_version: latest_published_version
|
|
.map(|value| from_db_version(value, "latest_published_version"))
|
|
.transpose()?,
|
|
can_delete,
|
|
created_at,
|
|
updated_at,
|
|
published_at,
|
|
})
|
|
}
|
|
|
|
fn build_auth_profile(
|
|
id: String,
|
|
workspace_id: String,
|
|
name: String,
|
|
kind: String,
|
|
config_json: Value,
|
|
created_at: OffsetDateTime,
|
|
updated_at: OffsetDateTime,
|
|
) -> Result<AuthProfile, RegistryError> {
|
|
Ok(AuthProfile {
|
|
id: crank_core::AuthProfileId::new(id),
|
|
workspace_id: WorkspaceId::new(workspace_id),
|
|
name,
|
|
kind: deserialize_enum_text(&kind, "kind")?,
|
|
config: deserialize_json_value(config_json)?,
|
|
created_at,
|
|
updated_at,
|
|
})
|
|
}
|
|
|
|
fn build_operation_usage_summary(
|
|
operation_id: String,
|
|
calls_today: i64,
|
|
error_rate_pct: f64,
|
|
avg_latency_ms: i64,
|
|
) -> Result<OperationUsageSummary, RegistryError> {
|
|
Ok(OperationUsageSummary {
|
|
operation_id: OperationId::new(operation_id),
|
|
calls_today: to_u64(calls_today, "calls_today")?,
|
|
error_rate_pct,
|
|
avg_latency_ms: to_u64(avg_latency_ms, "avg_latency_ms")?,
|
|
})
|
|
}
|
|
|
|
fn build_operation_agent_ref(
|
|
operation_id: String,
|
|
agent_id: String,
|
|
agent_slug: String,
|
|
display_name: String,
|
|
) -> Result<OperationAgentRef, RegistryError> {
|
|
Ok(OperationAgentRef {
|
|
operation_id: OperationId::new(operation_id),
|
|
agent_id: AgentId::new(agent_id),
|
|
agent_slug,
|
|
display_name,
|
|
})
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn build_agent_version_record(
|
|
summary: &AgentSummary,
|
|
version: i32,
|
|
status: String,
|
|
instructions_json: Value,
|
|
tool_selection_policy_json: Value,
|
|
created_at: OffsetDateTime,
|
|
bindings: Vec<AgentOperationBinding>,
|
|
resource_bindings: Vec<AgentResourceBinding>,
|
|
prompt_bindings: Vec<AgentPromptBinding>,
|
|
) -> Result<AgentVersionRecord, RegistryError> {
|
|
let version = from_db_version(version, "version")?;
|
|
let status = deserialize_enum_text(&status, "status")?;
|
|
|
|
Ok(AgentVersionRecord {
|
|
agent_id: summary.id.clone(),
|
|
workspace_id: summary.workspace_id.clone(),
|
|
version,
|
|
status,
|
|
created_at,
|
|
bindings,
|
|
resource_bindings,
|
|
prompt_bindings,
|
|
snapshot: AgentVersion {
|
|
agent_id: summary.id.clone(),
|
|
version,
|
|
status,
|
|
instructions: instructions_json,
|
|
tool_selection_policy: deserialize_json_value(tool_selection_policy_json)?,
|
|
created_at,
|
|
},
|
|
})
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn build_operation_version_record(
|
|
id: String,
|
|
workspace_id: String,
|
|
name: String,
|
|
display_name: String,
|
|
category: String,
|
|
protocol: String,
|
|
security_level: String,
|
|
operation_created_at: OffsetDateTime,
|
|
operation_updated_at: OffsetDateTime,
|
|
operation_published_at: Option<OffsetDateTime>,
|
|
version: i32,
|
|
status: String,
|
|
target_json: Value,
|
|
input_schema_json: Value,
|
|
output_schema_json: Value,
|
|
input_mapping_json: Value,
|
|
output_mapping_json: Value,
|
|
execution_config_json: Value,
|
|
tool_description_json: Value,
|
|
samples_json: Option<Value>,
|
|
generated_draft_json: Option<Value>,
|
|
config_export_json: Option<Value>,
|
|
wizard_state_json: Option<Value>,
|
|
change_note: Option<String>,
|
|
created_at: OffsetDateTime,
|
|
created_by: Option<String>,
|
|
) -> Result<OperationVersionRecord, RegistryError> {
|
|
let operation_id = OperationId::new(id);
|
|
let version = from_db_version(version, "version")?;
|
|
let status = deserialize_enum_text(&status, "status")?;
|
|
|
|
Ok(OperationVersionRecord {
|
|
operation_id: operation_id.clone(),
|
|
workspace_id: WorkspaceId::new(workspace_id),
|
|
version,
|
|
status,
|
|
change_note,
|
|
created_at,
|
|
created_by,
|
|
snapshot: RegistryOperation {
|
|
id: operation_id,
|
|
name,
|
|
display_name,
|
|
category,
|
|
protocol: deserialize_enum_text(&protocol, "protocol")?,
|
|
security_level: deserialize_enum_text(&security_level, "security_level")?,
|
|
status,
|
|
version,
|
|
target: deserialize_json_value(target_json)?,
|
|
input_schema: deserialize_json_value(input_schema_json)?,
|
|
output_schema: deserialize_json_value(output_schema_json)?,
|
|
input_mapping: deserialize_json_value(input_mapping_json)?,
|
|
output_mapping: deserialize_json_value(output_mapping_json)?,
|
|
execution_config: deserialize_json_value(execution_config_json)?,
|
|
tool_description: deserialize_json_value(tool_description_json)?,
|
|
samples: samples_json.map(deserialize_json_value).transpose()?,
|
|
generated_draft: generated_draft_json
|
|
.map(deserialize_json_value)
|
|
.transpose()?,
|
|
config_export: config_export_json.map(deserialize_json_value).transpose()?,
|
|
wizard_state: wizard_state_json.map(deserialize_json_value).transpose()?,
|
|
created_at: operation_created_at,
|
|
updated_at: operation_updated_at,
|
|
published_at: operation_published_at,
|
|
},
|
|
})
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn build_published_agent_tool(
|
|
workspace_id: String,
|
|
workspace_slug: String,
|
|
agent_id: String,
|
|
agent_slug: String,
|
|
tool_name: String,
|
|
tool_title: String,
|
|
tool_description: String,
|
|
operation: RegistryOperation,
|
|
) -> Result<PublishedAgentTool, RegistryError> {
|
|
Ok(PublishedAgentTool {
|
|
workspace_id: WorkspaceId::new(workspace_id),
|
|
workspace_slug,
|
|
agent_id: AgentId::new(agent_id),
|
|
agent_slug,
|
|
tool_name,
|
|
tool_title,
|
|
tool_description,
|
|
operation,
|
|
})
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn build_sample_metadata(
|
|
id: String,
|
|
operation_id: String,
|
|
version: i32,
|
|
sample_kind: String,
|
|
storage_ref: String,
|
|
content_type: String,
|
|
file_name: Option<String>,
|
|
created_at: OffsetDateTime,
|
|
) -> Result<OperationSampleMetadata, RegistryError> {
|
|
Ok(OperationSampleMetadata {
|
|
id: crank_core::SampleId::new(id),
|
|
operation_id: OperationId::new(operation_id),
|
|
version: from_db_version(version, "version")?,
|
|
sample_kind: deserialize_enum_text(&sample_kind, "sample_kind")?,
|
|
storage_ref,
|
|
content_type,
|
|
file_name,
|
|
created_at,
|
|
})
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn build_descriptor_metadata(
|
|
id: String,
|
|
operation_id: Option<String>,
|
|
version: Option<i32>,
|
|
descriptor_kind: String,
|
|
storage_ref: String,
|
|
source_name: Option<String>,
|
|
package_index: Option<Value>,
|
|
created_at: OffsetDateTime,
|
|
) -> Result<DescriptorMetadata, RegistryError> {
|
|
Ok(DescriptorMetadata {
|
|
id: crank_core::DescriptorId::new(id),
|
|
operation_id: operation_id.map(OperationId::new),
|
|
version: version
|
|
.map(|value| from_db_version(value, "version"))
|
|
.transpose()?,
|
|
descriptor_kind: deserialize_enum_text(&descriptor_kind, "descriptor_kind")?,
|
|
storage_ref,
|
|
source_name,
|
|
package_index,
|
|
created_at,
|
|
})
|
|
}
|
|
|
|
fn map_yaml_import_job(row: &PgRow) -> Result<YamlImportJob, RegistryError> {
|
|
Ok(YamlImportJob {
|
|
id: YamlImportJobId::new(row.try_get::<String, _>("id")?),
|
|
source_sample_id: row
|
|
.try_get::<Option<String>, _>("source_sample_id")?
|
|
.map(crank_core::SampleId::new),
|
|
status: deserialize_enum_text(&row.try_get::<String, _>("status")?, "status")?,
|
|
format_version: row.try_get("format_version")?,
|
|
mode: deserialize_enum_text(&row.try_get::<String, _>("mode")?, "mode")?,
|
|
result_operation_id: row
|
|
.try_get::<Option<String>, _>("result_operation_id")?
|
|
.map(OperationId::new),
|
|
result_version: row
|
|
.try_get::<Option<i32>, _>("result_version")?
|
|
.map(|value| from_db_version(value, "result_version"))
|
|
.transpose()?,
|
|
error_text: row.try_get("error_text")?,
|
|
created_at: row.try_get("created_at")?,
|
|
finished_at: row.try_get("finished_at")?,
|
|
})
|
|
}
|
|
|
|
fn map_import_job(row: &PgRow) -> Result<ImportJob, RegistryError> {
|
|
Ok(ImportJob {
|
|
id: ImportJobId::new(row.try_get::<String, _>("id")?),
|
|
workspace_id: WorkspaceId::new(row.try_get::<String, _>("workspace_id")?),
|
|
kind: deserialize_enum_text(&row.try_get::<String, _>("kind")?, "kind")?,
|
|
source_format: row.try_get("source_format")?,
|
|
source_version: row.try_get("source_version")?,
|
|
status: deserialize_enum_text(&row.try_get::<String, _>("status")?, "status")?,
|
|
preview_payload: row.try_get("preview_payload")?,
|
|
created_operation_ids: row.try_get("created_operation_ids")?,
|
|
error_text: row.try_get("error_text")?,
|
|
created_at: row.try_get("created_at")?,
|
|
expires_at: row.try_get("expires_at")?,
|
|
finished_at: row.try_get("finished_at")?,
|
|
})
|
|
}
|
|
|
|
fn map_usage_operation_breakdown(row: &PgRow) -> Result<UsageOperationBreakdown, RegistryError> {
|
|
Ok(UsageOperationBreakdown {
|
|
operation_id: OperationId::new(row.try_get::<String, _>("operation_id")?),
|
|
operation_name: row.try_get("operation_name")?,
|
|
operation_display_name: row.try_get("operation_display_name")?,
|
|
protocol: deserialize_enum_text(&row.try_get::<String, _>("protocol")?, "protocol")?,
|
|
calls_total: to_u64(row.try_get::<i64, _>("calls_total")?, "calls_total")?,
|
|
calls_error: to_u64(row.try_get::<i64, _>("calls_error")?, "calls_error")?,
|
|
p50_ms: to_u64(row.try_get::<i64, _>("p50_ms")?, "p50_ms")?,
|
|
p95_ms: to_u64(row.try_get::<i64, _>("p95_ms")?, "p95_ms")?,
|
|
p99_ms: to_u64(row.try_get::<i64, _>("p99_ms")?, "p99_ms")?,
|
|
})
|
|
}
|
|
|
|
fn map_usage_agent_breakdown(row: &PgRow) -> Result<UsageAgentBreakdown, RegistryError> {
|
|
Ok(UsageAgentBreakdown {
|
|
agent_id: AgentId::new(row.try_get::<String, _>("agent_id")?),
|
|
agent_slug: row.try_get("agent_slug")?,
|
|
agent_display_name: row.try_get("agent_display_name")?,
|
|
calls_total: to_u64(row.try_get::<i64, _>("calls_total")?, "calls_total")?,
|
|
calls_error: to_u64(row.try_get::<i64, _>("calls_error")?, "calls_error")?,
|
|
p50_ms: to_u64(row.try_get::<i64, _>("p50_ms")?, "p50_ms")?,
|
|
p95_ms: to_u64(row.try_get::<i64, _>("p95_ms")?, "p95_ms")?,
|
|
p99_ms: to_u64(row.try_get::<i64, _>("p99_ms")?, "p99_ms")?,
|
|
})
|
|
}
|
|
|
|
fn serialize_json_value<T: Serialize>(value: &T) -> Result<Value, RegistryError> {
|
|
Ok(serde_json::to_value(value)?)
|
|
}
|
|
|
|
fn serialize_option_json_value<T: Serialize>(
|
|
value: &Option<T>,
|
|
) -> Result<Option<Value>, RegistryError> {
|
|
value.as_ref().map(serialize_json_value).transpose()
|
|
}
|
|
|
|
fn deserialize_json_value<T: DeserializeOwned>(value: Value) -> Result<T, RegistryError> {
|
|
Ok(serde_json::from_value(value)?)
|
|
}
|
|
|
|
fn serialize_enum_text<T: Serialize>(
|
|
value: &T,
|
|
field: &'static str,
|
|
) -> Result<String, RegistryError> {
|
|
serde_json::to_value(value)?
|
|
.as_str()
|
|
.map(ToOwned::to_owned)
|
|
.ok_or(RegistryError::InvalidEnumRepresentation { field })
|
|
}
|
|
|
|
fn deserialize_enum_text<T: DeserializeOwned>(
|
|
value: &str,
|
|
field: &'static str,
|
|
) -> Result<T, RegistryError> {
|
|
serde_json::from_value(Value::String(value.to_owned()))
|
|
.map_err(|_| RegistryError::InvalidEnumRepresentation { field })
|
|
}
|
|
|
|
fn to_db_version(value: u32) -> i32 {
|
|
value as i32
|
|
}
|
|
|
|
fn from_db_version(value: i32, field: &'static str) -> Result<u32, RegistryError> {
|
|
u32::try_from(value).map_err(|_| RegistryError::InvalidNumericValue {
|
|
field,
|
|
value: i64::from(value),
|
|
})
|
|
}
|
|
|
|
fn to_u64(value: i64, field: &'static str) -> Result<u64, RegistryError> {
|
|
u64::try_from(value).map_err(|_| RegistryError::InvalidNumericValue { field, value })
|
|
}
|
|
|
|
fn usage_bucket_sql(bucket: crate::model::UsageBucket) -> &'static str {
|
|
match bucket {
|
|
crate::model::UsageBucket::Hour => "hour",
|
|
crate::model::UsageBucket::Day => "day",
|
|
crate::model::UsageBucket::Week => "week",
|
|
crate::model::UsageBucket::Month => "month",
|
|
}
|
|
}
|