Skip to content

CLIResourceLoader

Base classes

Name Children Inherits
ResourceLoader
llmling.resources.base
Base class for resource loaders.

⋔ Inheritance diagram

graph TD
  2383770696["cli.CLIResourceLoader"]
  2188557175["base.ResourceLoader"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  2180369605["protocols.CompletionProvider"]
  1073422405["typing.Protocol"]
  3481597255["typing.Generic"]
  2188557175 --> 2383770696
  925645236 --> 2188557175
  2751680898 --> 925645236
  2180369605 --> 2188557175
  1073422405 --> 2180369605
  3481597255 --> 1073422405
  2751680898 --> 3481597255
  3481597255 --> 2188557175

🛈 DocStrings

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

:fa-brands-github: Show source on GitHub