Skip to main content

surjection_core/adapters/
omp.rs

1//! oh-my-pi (`omp`): SPEC 7.3 and 7.4.
2//!
3//! - Root: `~/.omp`, or `~/$PI_CONFIG_DIR` (a directory name under home).
4//! - User scope: `<root>/agent/mcp.json` for the default profile, or
5//!   `$PI_CODING_AGENT_DIR/mcp.json` when that is set; a named profile
6//!   `<name>` uses `<root>/profiles/<name>/agent/mcp.json` and ignores
7//!   `PI_CODING_AGENT_DIR`. `profiles` in `clients.toml` picks the profiles.
8//! - Project scope: `<P>/.omp/mcp.json`.
9//! - Entries: `mcpServers.<name>` with an explicit `type`; `timeout` is in
10//!   milliseconds. A new file starts with the `$schema` line omp writes
11//!   itself. Top-level `disabledServers` and `enabledServers` are never
12//!   touched.
13
14use crate::adapters::caps::{
15    Caps, CommandRef, CompatSource, DisableMech, EnvRefSyntax, FieldSet, ProjectDisable,
16    ProjectSemantics, Source, TimeUnit, TimeoutField, TransportSet,
17};
18use crate::adapters::detect::Probe;
19use crate::adapters::location::Location;
20use crate::adapters::mapper::McpServersJson;
21use crate::adapters::spec::{AdapterSpec, ContainerPath, McpSpec, Profiles, Tier, exact_names};
22use crate::env::Os;
23use crate::formats::Format;
24use crate::model::clients::passthrough_of;
25
26pub const ID: &str = "omp";
27
28/// The profile name meaning the default user file.
29pub const DEFAULT_PROFILE: &str = "default";
30
31/// `~/.omp`, or `~/$PI_CONFIG_DIR`.
32const ROOT: Location = Location::HomeNamedDir {
33    var: "PI_CONFIG_DIR",
34    default: ".omp",
35    rel: "",
36};
37
38/// The default profile's user file: `<root>/agent/mcp.json`, or
39/// `$PI_CODING_AGENT_DIR/mcp.json`. Unlike Pi, omp does not expand a
40/// leading `~` in the variable (omp 19.1 resolves it with `path.resolve`,
41/// relative to the working directory), so no `tilde` here.
42const USER_FILE: Location = Location::EnvDir {
43    var: "PI_CODING_AGENT_DIR",
44    fallback: &Location::HomeNamedDir {
45        var: "PI_CONFIG_DIR",
46        default: ".omp",
47        rel: "agent",
48    },
49    rel: "mcp.json",
50    tilde: false,
51};
52
53/// What omp writes at the top of a new MCP file.
54const SKELETON: &str = "{\n  \"$schema\": \"https://raw.githubusercontent.com/can1357/oh-my-pi/main/packages/coding-agent/src/config/mcp-schema.json\"\n}\n";
55
56const CAPS: Caps = Caps {
57    transports: TransportSet::ALL,
58    env_ref: Some(EnvRefSyntax::DollarBrace {
59        default: true,
60        bare: false,
61    }),
62    env_ref_fields: FieldSet::COMMAND
63        .union(FieldSet::ARGS)
64        .union(FieldSet::ENV)
65        .union(FieldSet::CWD)
66        .union(FieldSet::URL)
67        .union(FieldSet::HEADERS),
68    command_ref: Some(CommandRef::Bang),
69    native_disable: Some(DisableMech::EnabledFalse),
70    // The project entry replaces the user one, so the suppression is the
71    // whole entry with `enabled: false` (`[verify]` whether an
72    // `enabled`-only entry is enough).
73    project_disable: Some(ProjectDisable::EnabledFalse { partial: false }),
74    project_semantics: ProjectSemantics::Replace,
75    precedence: &[Source::Project, Source::User],
76    compat_reads: &[
77        CompatSource::below("claude-code", Source::User),
78        CompatSource::below("claude-code", Source::Project),
79        CompatSource::below("codex", Source::User),
80        CompatSource::below("codex", Source::Project),
81        CompatSource::below("gemini", Source::User),
82        CompatSource::below("gemini", Source::Project),
83        CompatSource::below("opencode", Source::User),
84        CompatSource::below("opencode", Source::Project),
85        CompatSource::below("cursor", Source::User),
86        CompatSource::below("cursor", Source::Project),
87        CompatSource::below("windsurf", Source::User),
88        CompatSource::below("windsurf", Source::Project),
89        CompatSource::below("vscode-legacy", Source::Project),
90    ],
91    trust_gate: None,
92    rewrites_file_at_runtime: false,
93    restart_required: false,
94    tool_filter: None,
95    // `[verify]` whether omp on Windows starts `npx` without `cmd /c`.
96    windows_shell_shim: false,
97    timeouts: &[TimeoutField {
98        key: "timeout",
99        unit: TimeUnit::Milliseconds,
100    }],
101    passthrough_allowlist: passthrough_of(ID),
102};
103
104static MAPPER: McpServersJson = McpServersJson {
105    caps: &CAPS,
106    type_field: true,
107    remote_type: true,
108    cwd: true,
109    http_aliases: &[],
110    url_implies_http: true,
111    valid_name,
112};
113
114pub static SPEC: AdapterSpec = AdapterSpec {
115    id: ID,
116    display_name: "oh-my-pi",
117    tier: Tier::A,
118    detect: &[Probe::Path(ROOT), Probe::Executable("omp")],
119    mcp: Some(McpSpec {
120        oses: &[Os::Windows, Os::Linux, Os::MacOs],
121        read_only: false,
122        user: &[USER_FILE],
123        user_files: None,
124        user_mirrors: &[],
125        profiles: Some(Profiles {
126            default: DEFAULT_PROFILE,
127            dir: Location::HomeNamedDir {
128                var: "PI_CONFIG_DIR",
129                default: ".omp",
130                rel: "profiles",
131            },
132            rel: "agent/mcp.json",
133            valid_name: valid_profile,
134        }),
135        project: &[Location::Project(".omp/mcp.json")],
136        project_local: &[],
137        format: Format::Json,
138        skeleton: SKELETON,
139        container: ContainerPath(&["mcpServers"]),
140        project_local_container: ContainerPath(&["mcpServers"]),
141        legacy_containers: &[],
142        same_name: exact_names,
143        replaced_by: &[],
144        mapper: &MAPPER,
145        caps: CAPS,
146        sign_in: crate::adapters::sign_in::SignIn::UNKNOWN,
147    }),
148};
149
150/// omp server names: up to 100 characters of letters, digits, `_`, `-`,
151/// `.` and `:`, with single spaces between non-space segments.
152fn valid_name(name: &str) -> bool {
153    name.chars().count() <= 100
154        && !name.is_empty()
155        && name.split(' ').all(|segment| {
156            !segment.is_empty()
157                && segment
158                    .chars()
159                    .all(|c| c.is_alphanumeric() || matches!(c, '_' | '-' | '.' | ':'))
160        })
161}
162
163/// omp profile names: `[a-z0-9][a-z0-9._-]{0,63}`, not ending in `.`, and
164/// not a Windows reserved device name (`CON`, `COM1`, `nul.txt`, ...).
165fn valid_profile(name: &str) -> bool {
166    let bytes = name.as_bytes();
167    let pattern = matches!(bytes.first(), Some(b'a'..=b'z' | b'0'..=b'9'))
168        && bytes.len() <= 64
169        && bytes
170            .iter()
171            .all(|b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'.' | b'_' | b'-'));
172    let base = name.split('.').next().unwrap_or(name).to_ascii_uppercase();
173    let reserved = matches!(base.as_str(), "CON" | "PRN" | "AUX" | "NUL")
174        || (base.len() == 4
175            && (base.starts_with("COM") || base.starts_with("LPT"))
176            && base.as_bytes()[3].is_ascii_digit());
177    pattern && !name.ends_with('.') && !reserved
178}