Skip to content

cli

Class info

Classes

Name Children Inherits
CLIResourceLoader
llmling.resources.loaders.cli
Loads context from CLI command execution.

    🛈 DocStrings

    CLI command context loader.

    CLIResourceLoader

    Bases: ResourceLoader[CLIResource]

    Loads context from CLI command execution.

    Source code in src/llmling/resources/loaders/cli.py
    class CLIResourceLoader(ResourceLoader[CLIResource]):
        """Loads context from CLI command execution."""
    
        context_class = CLIResource
        uri_scheme = "cli"
        supported_mime_types: ClassVar[list[str]] = ["text/plain"]
    
        async def _load_impl(
            self,
            resource: CLIResource,
            name: str,
            processor_registry: ProcessorRegistry | None,
        ) -> AsyncIterator[LoadedResource]:
            """Execute command and load output."""
            command = cmd if isinstance((cmd := resource.command), str) else " ".join(cmd)
            try:
                kwargs: dict[str, Any] = dict(
                    stdout=asyncio.subprocess.PIPE,
                    stderr=asyncio.subprocess.PIPE,
                    cwd=resource.cwd,
                )
                if resource.shell:
                    proc = await asyncio.create_subprocess_shell(command, **kwargs)
                else:
                    proc = await asyncio.create_subprocess_exec(*command.split(), **kwargs)
                coro = proc.communicate()
                stdout, stderr = await asyncio.wait_for(coro, timeout=resource.timeout)
    
                if proc.returncode != 0:
                    error = stderr.decode().strip()
                    msg = f"Command failed with code {proc.returncode}: {error}"
                    raise exceptions.LoaderError(msg)  # noqa: TRY301
    
                content = stdout.decode()
    
                if processor_registry and (procs := resource.processors):
                    processed = await processor_registry.process(content, procs)
                    content = processed.content
                meta = {"command": command, "exit_code": proc.returncode}
                yield create_loaded_resource(
                    content=content,
                    source_type="cli",
                    uri=self.create_uri(name=name),
                    mime_type=self.supported_mime_types[0],
                    name=resource.description or f"CLI Output: {command}",
                    description=resource.description,
                    additional_metadata=meta,
                )
            except Exception as exc:
                msg = f"CLI command execution failed: {command}"
                raise exceptions.LoaderError(msg) from exc