weft-core 0.1.1

OpenAI-compatible AI agent runtime with WASM capability plugin system
Documentation
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

/// Top-level package.toml structure.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageManifest {
    #[serde(default)]
    pub identity: Option<PackageIdentity>,
    #[serde(rename = "package_info")]
    pub package_info: PackageDescriptor,
    #[serde(default)]
    pub capability: Option<PackageCapability>,
    #[serde(default)]
    pub package: Option<PackageMeta>,
    #[serde(default)]
    pub runtime_contract: Option<PackageRuntimeContract>,
    #[serde(default)]
    pub lifecycle: Option<PackageLifecycle>,
    #[serde(default)]
    pub permissions: PackagePermissions,
    #[serde(default)]
    pub dependencies: HashMap<String, String>,
    #[serde(default)]
    pub config: HashMap<String, PackageConfigField>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackageCapability {
    #[serde(default)]
    pub provides: Vec<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackageIdentity {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub description: String,
}

/// [package_info] section — identity and entry point.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageDescriptor {
    pub name: String,
    pub version: String,
    pub description: String,
    #[serde(default = "default_entry")]
    pub entry: String,
    /// Capabilities this package provides (e.g., ["chat_channel", "tool", "transform"])
    #[serde(default)]
    pub provides: Vec<String>,
    /// Chat endpoint path if this package provides chat_channel capability
    pub chat_endpoint: Option<String>,
}

fn default_entry() -> String {
    "package.wasm".into()
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackageMeta {
    #[serde(default)]
    pub entry: Option<String>,
    #[serde(default)]
    pub runtime: Option<String>,
    /// Package kind, e.g. "product" (capability composition, no executable entry),
    /// "foundation", "provider". Absent for most leaf packages.
    #[serde(default)]
    pub kind: Option<String>,
    #[serde(default)]
    pub api_version: Option<String>,
    #[serde(default)]
    pub native_allowed: bool,
    #[serde(default)]
    pub expected_digest: Option<String>,
    #[serde(default)]
    pub chat_endpoint: Option<String>,
    #[serde(default)]
    pub provides: Vec<String>,
    #[serde(default)]
    pub priority: Option<i32>,
    #[serde(default)]
    pub assets: Vec<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackageRuntimeContract {
    #[serde(default)]
    pub startup_mode: Option<String>,
    #[serde(default)]
    pub communication: Vec<String>,
    #[serde(default)]
    pub config_delivery: Vec<String>,
    #[serde(default)]
    pub readiness_probe: Option<String>,
    #[serde(default)]
    pub liveness_probe: Option<String>,
    #[serde(default)]
    pub stop_mode: Option<String>,
    #[serde(default)]
    pub restart_policy: Option<String>,
}

/// [config.*] section — schema for package configuration fields.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageConfigField {
    #[serde(rename = "type")]
    pub field_type: String, // string, number, boolean, select
    #[serde(default)]
    pub required: bool,
    #[serde(default)]
    pub secret: bool,
    pub default: Option<serde_json::Value>,
    pub description: Option<String>,
    #[serde(default)]
    pub options: Option<Vec<String>>, // for select type
    pub min: Option<f64>,
    pub max: Option<f64>,
}

/// [permissions] section — capability declarations.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackagePermissions {
    #[serde(default)]
    pub process: bool,
    #[serde(default)]
    pub network: bool,
    #[serde(default)]
    pub auth: bool,
    #[serde(default)]
    pub storage: bool,
    #[serde(default)]
    pub pipeline: bool,
    #[serde(default)]
    pub routes: bool,
    #[serde(default)]
    pub events: bool,
    #[serde(default)]
    pub log: bool,
    #[serde(default)]
    pub ui: bool,
    #[serde(default)]
    pub config: bool,
    #[serde(default)]
    pub scheduler: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackageLifecycle {
    #[serde(default)]
    pub healthcheck: Option<String>,
}

fn clean_optional_string(value: Option<String>) -> Option<String> {
    value.and_then(|entry| {
        let trimmed = entry.trim();
        if trimmed.is_empty() {
            None
        } else {
            Some(trimmed.to_string())
        }
    })
}

impl PackageManifest {
    pub fn resolved_entry(&self) -> Option<String> {
        clean_optional_string(
            self.package
                .as_ref()
                .and_then(|package| package.entry.clone())
                .or_else(|| Some(self.package_info.entry.clone())),
        )
    }

    pub fn runtime_kind(&self) -> String {
        if let Some(runtime) = clean_optional_string(
            self.package
                .as_ref()
                .and_then(|package| package.runtime.clone()),
        ) {
            return runtime;
        }

        match self.resolved_entry() {
            Some(entry) if entry.ends_with(".wasm") => "wasm".into(),
            Some(_) => "service".into(),
            None => "wasm".into(),
        }
    }

    pub fn resolved_chat_endpoint(&self) -> Option<String> {
        clean_optional_string(
            self.package
                .as_ref()
                .and_then(|package| package.chat_endpoint.clone())
                .or_else(|| self.package_info.chat_endpoint.clone()),
        )
    }

    pub fn resolved_provides(&self) -> Vec<String> {
        let mut provides = self.package_info.provides.clone();
        if let Some(capability) = &self.capability {
            provides.extend(capability.provides.clone());
        }
        if let Some(package) = &self.package {
            provides.extend(package.provides.clone());
        }
        provides.retain(|value| !value.trim().is_empty());
        provides.sort();
        provides.dedup();
        provides
    }

    /// True if this is a product package (`[package] kind = "product"`), which
    /// composes capabilities via [requires]/[bindings] and has no executable
    /// entry of its own — so wasm/entry checks do not apply.
    pub fn is_product(&self) -> bool {
        self.package
            .as_ref()
            .and_then(|package| package.kind.as_deref())
            .map(|kind| kind.eq_ignore_ascii_case("product"))
            .unwrap_or(false)
    }

    pub fn resolved_healthcheck(&self) -> Option<String> {
        clean_optional_string(
            self.runtime_contract
                .as_ref()
                .and_then(|contract| contract.readiness_probe.clone())
                .or_else(|| {
                    self.lifecycle
                        .as_ref()
                        .and_then(|lifecycle| lifecycle.healthcheck.clone())
                }),
        )
    }
}

/// Load and parse a package.toml from a package directory.
pub fn load_manifest(package_dir: &Path) -> Result<PackageManifest> {
    let manifest_path = package_dir.join("package.toml");
    let content = std::fs::read_to_string(&manifest_path)
        .with_context(|| format!("Failed to read {}", manifest_path.display()))?;

    let has_package_section = content.lines().any(|line| line.trim() == "[package_info]");
    let mut manifest: PackageManifest = if has_package_section {
        toml::from_str(&content)
            .with_context(|| format!("Failed to parse {}", manifest_path.display()))?
    } else {
        #[derive(Debug, Clone, Default, Serialize, Deserialize)]
        struct IdentityOnlyManifest {
            #[serde(default)]
            identity: Option<PackageIdentity>,
            #[serde(default)]
            capability: Option<PackageCapability>,
            #[serde(default)]
            package: Option<PackageMeta>,
            #[serde(default)]
            runtime_contract: Option<PackageRuntimeContract>,
            #[serde(default)]
            lifecycle: Option<PackageLifecycle>,
            #[serde(default)]
            permissions: PackagePermissions,
            #[serde(default)]
            dependencies: HashMap<String, String>,
            #[serde(default)]
            config: HashMap<String, PackageConfigField>,
        }

        let raw: IdentityOnlyManifest = toml::from_str(&content)
            .with_context(|| format!("Failed to parse {}", manifest_path.display()))?;
        PackageManifest {
            identity: raw.identity,
            package_info: PackageDescriptor {
                name: String::new(),
                version: String::new(),
                description: String::new(),
                entry: default_entry(),
                provides: Vec::new(),
                chat_endpoint: None,
            },
            capability: raw.capability,
            package: raw.package,
            runtime_contract: raw.runtime_contract,
            lifecycle: raw.lifecycle,
            permissions: raw.permissions,
            dependencies: raw.dependencies,
            config: raw.config,
        }
    };

    if manifest.package_info.name.trim().is_empty() {
        if let Some(identity) = &manifest.identity {
            manifest.package_info.name = identity.name.clone();
            manifest.package_info.version = identity.version.clone();
            manifest.package_info.description = identity.description.clone();
            if manifest.package_info.entry.trim().is_empty() {
                manifest.package_info.entry = default_entry();
            }
        }
    }

    Ok(manifest)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_package_manifest() {
        let toml_str = r#"
[package_info]
name = "weft-claw"
version = "0.1.0"
description = "Weft Claw AI agent orchestration"
entry = "backend.ts"

[permissions]
process = true
network = true
auth = true
storage = true
pipeline = false
routes = true
events = true
log = true
ui = true
config = true
scheduler = true

[ui]
title = "Weft Claw"
icon = "robot"
entry = "ui/index.html"
"#;
        let manifest: PackageManifest = toml::from_str(toml_str).unwrap();
        assert_eq!(manifest.package_info.name, "weft-claw");
        assert_eq!(manifest.package_info.version, "0.1.0");
        assert_eq!(manifest.package_info.entry, "backend.ts"); // explicit entry in toml
        assert!(manifest.permissions.process);
        assert!(manifest.permissions.network);
        assert!(!manifest.permissions.pipeline);
    }

    #[test]
    fn test_parse_minimal_manifest() {
        let toml_str = r#"
[package_info]
name = "simple"
version = "0.1.0"
description = "A simple package"
"#;
        let manifest: PackageManifest = toml::from_str(toml_str).unwrap();
        assert_eq!(manifest.package_info.name, "simple");
        assert_eq!(manifest.package_info.entry, "package.wasm"); // default entry
        assert!(!manifest.permissions.process);
    }

    #[test]
    fn test_parse_identity_manifest() {
        let toml_str = r#"
[identity]
name = "companion-core"
version = "0.1.0"
description = "Companion runtime"

[package]
entry = "server.py"
runtime = "service"
"#;

        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("package.toml");
        std::fs::write(&path, toml_str).unwrap();
        let loaded = load_manifest(temp.path()).unwrap();
        assert_eq!(loaded.package_info.name, "companion-core");
        assert_eq!(loaded.package_info.version, "0.1.0");
        assert_eq!(loaded.package_info.description, "Companion runtime");
        assert_eq!(loaded.resolved_entry().as_deref(), Some("server.py"));
    }

    #[test]
    fn test_parse_modern_service_manifest() {
        let toml_str = r#"
[package_info]
name = "ticker-watch"
version = "0.1.0"
description = "Ticker watcher"

[package]
entry = "dist/server.js"
runtime = "service"
chat_endpoint = "/chat"

[runtime_contract]
startup_mode = "persistent"
communication = ["http"]
config_delivery = ["config_file"]
readiness_probe = "http://127.0.0.1:43111/health"
restart_policy = "host_managed"

[lifecycle]
healthcheck = "http://127.0.0.1:43111/health"
"#;
        let manifest: PackageManifest = toml::from_str(toml_str).unwrap();
        assert_eq!(manifest.resolved_entry().as_deref(), Some("dist/server.js"));
        assert_eq!(manifest.runtime_kind(), "service");
        assert_eq!(manifest.resolved_chat_endpoint().as_deref(), Some("/chat"));
        assert_eq!(
            manifest.resolved_healthcheck().as_deref(),
            Some("http://127.0.0.1:43111/health")
        );
    }

    #[test]
    fn test_resolved_provides_includes_package_values() {
        let toml_str = r#"
[package_info]
name = "companion-core"
version = "0.1.0"
description = "Companion"

[package]
entry = "server.py"
runtime = "service"
provides = ["chat_channel", "companion.turn.handle"]
"#;
        let manifest: PackageManifest = toml::from_str(toml_str).unwrap();
        assert!(manifest
            .resolved_provides()
            .contains(&"chat_channel".to_string()));
        assert!(manifest
            .resolved_provides()
            .contains(&"companion.turn.handle".to_string()));
    }

    #[test]
    fn test_resolved_provides_includes_capability_section() {
        let toml_str = r#"
[identity]
name = "companion-core"
version = "0.1.0"
description = "Companion runtime"

[package]
entry = "server.py"
runtime = "service"

[capability]
provides = ["chat_channel", "companion.turn.handle"]
"#;
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("package.toml");
        std::fs::write(&path, toml_str).unwrap();
        let loaded = load_manifest(temp.path()).unwrap();
        assert!(loaded
            .resolved_provides()
            .contains(&"chat_channel".to_string()));
        assert!(loaded
            .resolved_provides()
            .contains(&"companion.turn.handle".to_string()));
    }

    #[test]
    fn test_load_current_official_package_manifests() {
        let repo_root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");

        let agent_core = load_manifest(
            &repo_root
                .join("packages")
                .join("official")
                .join("agent-core"),
        )
        .expect("agent-core manifest should load");
        assert_eq!(agent_core.package_info.name, "agent-runtime");
        assert_eq!(agent_core.runtime_kind(), "wasm");
        assert!(agent_core
            .resolved_provides()
            .contains(&"agent.runtime".to_string()));

        let weft_claw = load_manifest(
            &repo_root
                .join("packages")
                .join("official")
                .join("weft-claw"),
        )
        .expect("weft-claw manifest should load");
        assert_eq!(weft_claw.package_info.name, "weft-claw");
        assert_eq!(weft_claw.runtime_kind(), "wasm");
        assert!(weft_claw
            .resolved_provides()
            .contains(&"weft_claw.turn".to_string()));
    }
}