coact.util¶
Pure helpers for coact with no internal coact imports.
Small, reusable string/name utilities and a thin check_requirements helper
for the optional-dependency backends (Agent SDK, py2mcp/FastMCP, an LLM
provider). Kept dependency-free so every other coact module can import it.
Functions
|
Return the safe |
|
Verify optional dependencies are importable, else raise an actionable error. |
|
Return the first depth-balanced |
|
Resolve a |
|
Return |
|
Convert CamelCase / snake_case / spaces to kebab-case. |
|
Convert CamelCase / kebab-case / spaces to snake_case. |
- coact.util.agent_filename(name)[source]¶
Return the safe
<name>.mdfilename for an agent, rejecting unsafe names.Agent names become files under
.claude/agents/; a name carrying a path separator or..could escape that directory (CWE-22 path traversal) on write or read. Names must be a bare filename stem. RaisesValueErroron an empty, non-string, or path-bearing name — the check lives at the filesystem boundary so in-memoryAgentDefinitions stay unconstrained.>>> agent_filename('ux-analyst') 'ux-analyst.md' >>> agent_filename('../escape') Traceback (most recent call last): ... ValueError: unsafe agent name '../escape': ...- Return type:
- coact.util.check_requirements(modules, *, feature)[source]¶
Verify optional dependencies are importable, else raise an actionable error.
modulesmaps an importable module name to the pip target that provides it (e.g.{'claude_agent_sdk': 'claude-agent-sdk'}). RaisesImportErrorlisting the exactpip installline for whatever is missing — and, when some requirements are present, which ones — the package-UX convention for optional backends (informative, actionable errors).>>> check_requirements({'json': 'json'}, feature='noop') # importable -> no raise- Return type:
- coact.util.first_balanced_span(s, opener='{', closer='}')[source]¶
Return the first depth-balanced
opener…closersubstring ofs.Brackets inside double-quoted JSON strings are ignored, so a valid value followed by prose that itself contains brackets is not over-captured (a greedy
opener.*closerwould run to the lastcloserand fail to parse). ReturnsNonewhen no balanced span starts ins. Shared by the LLM-reply JSON extractors (coact.llm,coact.realize_litellm).>>> first_balanced_span('Result: {"a": 1}. Note: {braces}.') '{"a": 1}' >>> first_balanced_span('see [1, [2, 3]] end', '[', ']') '[1, [2, 3]]' >>> first_balanced_span('no brackets here') is None True
- coact.util.import_object(ref)[source]¶
Resolve a
'module.path:attr'or'module.path.attr'reference.Prefer the
module:attrform (unambiguous). Falls back to splitting on the last dot for the dotted form.>>> import_object('json:dumps') <function dumps at ...> >>> import_object('os.path.join') <function join at ...>- Return type:
- coact.util.safe_filename(name, *, suffix='', kind='name')[source]¶
Return
f"{name}{suffix}"after rejecting unsafe, path-bearing names.Names that become files must be bare filename stems; one carrying a path separator or
..could escape its directory (CWE-22) on write or read. RaisesValueErroron an empty, non-string, or path-bearing name.kindcustomizes the error wording for the caller’s domain. Shared byagent_filename()(.mdagents) and the publish axis (.mcpbbundles).>>> safe_filename('paths', suffix='.mcpb') 'paths.mcpb' >>> safe_filename('../x', suffix='.mcpb') Traceback (most recent call last): ... ValueError: unsafe name '../x': ...- Return type: