feat(web): Document/Endpoint/Resource artifact rewrite (ADR 0007)
Replace the monolithic compose_*/configure author surface with file-level
web|endpoint artifacts, split ext/{web,data,security,net}, and lock
no-degrade rules so the model cannot regress to God-object APIs.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
33f8b94bb0
commit
67bac1bf02
54 files changed
+4642
-2505
No files matched your search
@@ -0,0 +1,117 @@
|
||||
//! Web / Endpoint Artifact classification from bound Metadata.
|
||||
//!
|
||||
//! Spec: `doc/design/ext-web-artifact.md` · ADR 0007.
|
||||
//! Does not add CORE_CONSTRUCTS — reuses flat frontmatter `type` / `类型`.
|
||||
|
||||
use crate::value::Value;
|
||||
|
||||
/// Kind of executable document recognized for the Web model.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ArtifactKind {
|
||||
/// `type: web` / `类型: 网页`
|
||||
Web,
|
||||
/// `type: endpoint` / `类型: 端点`
|
||||
Endpoint,
|
||||
/// `type: prompt` / `类型: 提示` (AI stack; classified for inspect)
|
||||
Prompt,
|
||||
/// `type: llm` / other known types
|
||||
Other,
|
||||
}
|
||||
|
||||
/// Read `type` or `类型` from bound metadata.
|
||||
pub fn metadata_type(metadata: &[(String, Value)]) -> Option<String> {
|
||||
for key in ["type", "类型"] {
|
||||
if let Some(v) = meta_get(metadata, key) {
|
||||
let s = v.as_display();
|
||||
if !s.is_empty() {
|
||||
return Some(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Classify artifact kind from metadata (None if no type key).
|
||||
pub fn classify(metadata: &[(String, Value)]) -> Option<ArtifactKind> {
|
||||
let t = metadata_type(metadata)?;
|
||||
Some(match t.as_str() {
|
||||
"web" | "网页" => ArtifactKind::Web,
|
||||
"endpoint" | "端点" => ArtifactKind::Endpoint,
|
||||
"prompt" | "提示" => ArtifactKind::Prompt,
|
||||
_ => ArtifactKind::Other,
|
||||
})
|
||||
}
|
||||
|
||||
/// True when this module is a Web Document Artifact.
|
||||
pub fn is_web_document(metadata: &[(String, Value)]) -> bool {
|
||||
matches!(classify(metadata), Some(ArtifactKind::Web))
|
||||
}
|
||||
|
||||
/// True when this module is an Endpoint Artifact.
|
||||
pub fn is_endpoint(metadata: &[(String, Value)]) -> bool {
|
||||
matches!(classify(metadata), Some(ArtifactKind::Endpoint))
|
||||
}
|
||||
|
||||
/// Lookup a metadata string (EN key, then ZH aliases).
|
||||
pub fn meta_text(metadata: &[(String, Value)], en: &str, zh: &str) -> Option<String> {
|
||||
for key in [en, zh] {
|
||||
if let Some(v) = meta_get(metadata, key) {
|
||||
let s = v.as_display();
|
||||
if !s.is_empty() {
|
||||
return Some(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Route for a web document: `route` / `路由`, else None.
|
||||
pub fn web_route(metadata: &[(String, Value)]) -> Option<String> {
|
||||
meta_text(metadata, "route", "路由")
|
||||
}
|
||||
|
||||
/// HTTP method defaulting to GET.
|
||||
pub fn http_method(metadata: &[(String, Value)]) -> String {
|
||||
meta_text(metadata, "method", "方法").unwrap_or_else(|| "GET".to_string())
|
||||
}
|
||||
|
||||
/// Endpoint path: `path` / `路径`.
|
||||
pub fn endpoint_path(metadata: &[(String, Value)]) -> Option<String> {
|
||||
meta_text(metadata, "path", "路径")
|
||||
}
|
||||
|
||||
fn meta_get<'a>(metadata: &'a [(String, Value)], key: &str) -> Option<&'a Value> {
|
||||
metadata.iter().find(|(k, _)| k == key).map(|(_, v)| v)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::value::Value;
|
||||
|
||||
#[test]
|
||||
fn classify_web_en_zh() {
|
||||
let en = vec![("type".into(), Value::Text("web".into()))];
|
||||
assert_eq!(classify(&en), Some(ArtifactKind::Web));
|
||||
let zh = vec![("类型".into(), Value::Text("网页".into()))];
|
||||
assert_eq!(classify(&zh), Some(ArtifactKind::Web));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classify_endpoint() {
|
||||
let m = vec![
|
||||
("type".into(), Value::Text("endpoint".into())),
|
||||
("path".into(), Value::Text("/api/x".into())),
|
||||
("method".into(), Value::Text("POST".into())),
|
||||
];
|
||||
assert!(is_endpoint(&m));
|
||||
assert_eq!(endpoint_path(&m).as_deref(), Some("/api/x"));
|
||||
assert_eq!(http_method(&m), "POST");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_type() {
|
||||
let m = vec![("title".into(), Value::Text("x".into()))];
|
||||
assert_eq!(classify(&m), None);
|
||||
}
|
||||
}
|
||||
@@ -201,6 +201,7 @@ pub enum HostFn {
|
||||
Meta = 192,
|
||||
MetaGet = 193,
|
||||
ModuleSource = 194,
|
||||
ArtifactKind = 195,
|
||||
}
|
||||
|
||||
|
||||
@@ -392,6 +393,7 @@ impl HostFn {
|
||||
192 => Self::Meta,
|
||||
193 => Self::MetaGet,
|
||||
194 => Self::ModuleSource,
|
||||
195 => Self::ArtifactKind,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -586,6 +588,7 @@ impl HostFn {
|
||||
"host_meta" | "meta" => Self::Meta,
|
||||
"host_meta_get" | "meta_get" => Self::MetaGet,
|
||||
"host_module_source" | "module_source" => Self::ModuleSource,
|
||||
"host_artifact_kind" | "artifact_kind" => Self::ArtifactKind,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -767,6 +770,7 @@ impl HostFn {
|
||||
Self::Meta => "host_meta",
|
||||
Self::MetaGet => "host_meta_get",
|
||||
Self::ModuleSource => "host_module_source",
|
||||
Self::ArtifactKind => "host_artifact_kind",
|
||||
Self::WritebackRecord => "host_writeback_record",
|
||||
Self::WritebackGet => "host_writeback_get",
|
||||
Self::WritebackClear => "host_writeback_clear",
|
||||
@@ -790,7 +794,7 @@ impl HostFn {
|
||||
&["path"]
|
||||
}
|
||||
Self::WriteText | Self::AppendText => &["path", "text"],
|
||||
Self::NowUnix | Self::NowMs | Self::Args | Self::Cwd | Self::Meta | Self::ModuleSource => {
|
||||
Self::NowUnix | Self::NowMs | Self::Args | Self::Cwd | Self::Meta | Self::ModuleSource | Self::ArtifactKind => {
|
||||
&[]
|
||||
}
|
||||
Self::FormatTime => &["unix", "pattern"],
|
||||
@@ -998,6 +1002,7 @@ pub fn call_host(
|
||||
HostFn::Meta => sys::meta(ctx),
|
||||
HostFn::MetaGet => sys::meta_get(ctx, require(bound, "key")?),
|
||||
HostFn::ModuleSource => crate::host::agent_rt::module_source(ctx),
|
||||
HostFn::ArtifactKind => sys::artifact_kind(ctx),
|
||||
HostFn::Exit => sys::exit(ctx, require(bound, "code")?),
|
||||
HostFn::Exec => sys::exec(
|
||||
ctx,
|
||||
|
||||
@@ -109,6 +109,17 @@ pub fn meta_get(ctx: &HostContext, key: &Value) -> Result<Value, String> {
|
||||
.unwrap_or(Value::None))
|
||||
}
|
||||
|
||||
/// Classify entry Artifact (`web` / `endpoint` / `prompt` / `other`); missing type → None.
|
||||
pub fn artifact_kind(ctx: &HostContext) -> Result<Value, String> {
|
||||
Ok(match crate::artifact::classify(&ctx.entry_metadata) {
|
||||
None => Value::None,
|
||||
Some(crate::artifact::ArtifactKind::Web) => Value::Text("web".into()),
|
||||
Some(crate::artifact::ArtifactKind::Endpoint) => Value::Text("endpoint".into()),
|
||||
Some(crate::artifact::ArtifactKind::Prompt) => Value::Text("prompt".into()),
|
||||
Some(crate::artifact::ArtifactKind::Other) => Value::Text("other".into()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn exit(ctx: &HostContext, code: &Value) -> Result<Value, String> {
|
||||
let c = as_i64(code, "code")?;
|
||||
if ctx.soft_side_effects {
|
||||
|
||||
@@ -205,12 +205,53 @@ impl Interpreter {
|
||||
true,
|
||||
);
|
||||
}
|
||||
// Web Artifact entry (ADR 0007): `type: web|endpoint` may be pure document —
|
||||
// no `# main`. Return a Result-shaped summary for run/check gold paths.
|
||||
if let Some(kind) = crate::artifact::classify(&module.metadata) {
|
||||
use crate::artifact::ArtifactKind;
|
||||
if matches!(kind, ArtifactKind::Web | ArtifactKind::Endpoint) {
|
||||
return Ok(Self::artifact_run_summary(module, kind));
|
||||
}
|
||||
}
|
||||
bail!("no `# main` object to run");
|
||||
})();
|
||||
self.site_module = prev;
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
fn artifact_run_summary(module: &Module, kind: crate::artifact::ArtifactKind) -> Value {
|
||||
use crate::artifact::{endpoint_path, http_method, web_route, ArtifactKind};
|
||||
let kind_s = match kind {
|
||||
ArtifactKind::Web => "web",
|
||||
ArtifactKind::Endpoint => "endpoint",
|
||||
ArtifactKind::Prompt => "prompt",
|
||||
ArtifactKind::Other => "other",
|
||||
};
|
||||
let mut value = vec![
|
||||
("kind".into(), Value::Text(kind_s.into())),
|
||||
("method".into(), Value::Text(http_method(&module.metadata))),
|
||||
];
|
||||
match kind {
|
||||
ArtifactKind::Web => {
|
||||
if let Some(r) = web_route(&module.metadata) {
|
||||
value.push(("route".into(), Value::Text(r)));
|
||||
}
|
||||
}
|
||||
ArtifactKind::Endpoint => {
|
||||
if let Some(r) = endpoint_path(&module.metadata) {
|
||||
value.push(("path".into(), Value::Text(r)));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Value::Map(vec![
|
||||
("ok".into(), Value::Bool(true)),
|
||||
("value".into(), Value::Map(value)),
|
||||
("error".into(), Value::None),
|
||||
])
|
||||
}
|
||||
|
||||
/// Resolve `lib.member` on the site (entry) module — used by ABI `call_lib_path`.
|
||||
pub(crate) fn call_site_lib_path(
|
||||
&mut self,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
//! CLI also offers `view` for AST-backed browsing.
|
||||
|
||||
pub mod aliases;
|
||||
pub mod artifact;
|
||||
pub mod ast;
|
||||
pub mod binding;
|
||||
pub mod browser_session;
|
||||
|
||||
Reference in new issue
Block a user