Skip to content

@codesoul-co/hypha-inference ​

hypha-inference provides normalized inference requests, backend registration, prompt profiles and prefix/KV cache coordination. Kernel code addresses a backend ID or model alias instead of importing a provider SDK.

bash
npm install @codesoul-co/hypha-inference@1.0.1

Main exports ​

ExportUse
InferenceManagerRegister providers and execute normalized inference
InferenceProviderMinimal provider-neutral inference port
InferenceBackendRegistryRegister HTTP/local backend families
createDefaultInferenceBackendRegistryCompose supported backend adapters
PromptProfileRegistryVersion and resolve layered prompt profiles
InMemoryPrefixCacheProviderTest/local prefix-cache metadata
InMemoryKvCacheProviderTest/local scoped KV reuse

Minimal provider ​

ts
import { InferenceManager } from '@codesoul-co/hypha-inference';

const inference = new InferenceManager();

inference.register({
  id: 'echo',
  infer: async (request) => ({
    id: `response-${request.stepId}`,
    output: request.input,
    usage: { inputTokens: 0, outputTokens: 0 },
  }),
});

const response = await inference.infer('echo', {
  runId: 'run-1',
  stepId: 'step-reason-1',
  modelAlias: 'reasoning.primary',
  input: 'Summarize the verified evidence.',
});

runId and stepId make the request traceable. Add Session/workspace cache scope only when reuse is explicitly allowed by policy.

Cache-aware composition ​

ts
import {
  InferenceManager,
  InMemoryKvCacheProvider,
  InMemoryPrefixCacheProvider,
} from '@codesoul-co/hypha-inference';

const manager = new InferenceManager({
  prefixCache: new InMemoryPrefixCacheProvider(),
  kvCache: new InMemoryKvCacheProvider(),
});

Prefix metadata describes reusable stable prompt segments. KV entries must be bounded by Run, Session or workspace scope. A cache hit is an optimization and must not replace the Event/trace that explains what the Run observed.

Backend selection ​

The backend registry includes OpenAI API, Ollama, SGLang, vLLM and llama.cpp adapters. Register only the backends enabled by deployment configuration. Resolve endpoints and credentials in trusted composition, enforce timeouts/cancellation, and normalize response/error evidence before returning to Kernel.

Prompt profiles ​

PromptProfileRegistry composes versioned layers from controlled sources. Record the selected profile/version or artifact reference in trace evidence; do not silently change the prompt for an in-progress replay fixture.

Production checks ​

  • Reject unknown backend IDs and unapproved aliases at startup.
  • Bound request/output size and redact secrets before tracing.
  • Propagate cancellation and deadlines to the transport.
  • Verify cache scope equivalence before reuse.
  • Test provider failure, timeout, malformed output and cache miss paths.