Skip to content
2 min read · 495 words

Class: WebLLMChatCompletionsAdapter ​

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:259

Opinionated cross-environment LLM adapter for the WebLLM Chat Completions wire shape.

Remarks ​

Construction validates options eagerly via @nhtio/adk/batteries!validateOptions and throws @nhtio/adk/batteries!E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS on failure — config bugs fail loud, not at dispatch time. The returned instance is reusable: call WebLLMChatCompletionsAdapter.executor once per DispatchRunner configuration to obtain an @nhtio/adk!DispatchExecutorFn bound to the baseline plus optional executor-scope overrides.

Per-iteration overrides live on the active @nhtio/adk!DispatchContext's stash.webLLMChatCompletions slot and take highest precedence — they merge into the executor-scope shape on every iteration. helpers merge key-by-key across all three layers; every other field is replaced wholesale at the highest layer that sets it.

Constructors ​

Constructor ​

ts
new WebLLMChatCompletionsAdapter(options: unknown): WebLLMChatCompletionsAdapter;

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:290

Parameters ​

ParameterTypeDescription
optionsunknownConstructor-baseline options. Re-validated on every iteration after per-dispatch and per-iteration overrides are layered in.

Returns ​

WebLLMChatCompletionsAdapter

Throws ​

@nhtio/adk/batteries!E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS when options does not satisfy @nhtio/adk/batteries!webLLMChatCompletionsOptionsSchema.

Properties ​

PropertyModifierTypeDescriptionDefined in
STASH_KEYreadonly"webLLMChatCompletions"Customary key for per-iteration overrides on ctx.stash. The adapter reads ctx.stash.get(WebLLMChatCompletionsAdapter.STASH_KEY, {}) at the start of every iteration and merges the value into the resolved options shape.src/batteries/llm/webllm_chat_completions/adapter.ts:265

Methods ​

executor() ​

ts
executor(overrides?: Partial<WebLLMChatCompletionsAdapterOptions>): DispatchExecutorFn;

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:407

Returns an @nhtio/adk!DispatchExecutorFn bound to this adapter's baseline plus optional executor-scope overrides. The returned function is reusable across iterations — every iteration re-merges with ctx.stash[STASH_KEY] and re-validates the result.

Parameters ​

ParameterTypeDescription
overrides?Partial<WebLLMChatCompletionsAdapterOptions>Optional executor-scope overrides. Higher precedence than the baseline, lower precedence than ctx.stash[STASH_KEY].

Returns ​

DispatchExecutorFn

An @nhtio/adk!DispatchExecutorFn suitable for DispatchRunner.


isAvailable() ​

ts
isAvailable(): boolean;

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:318

Instance-level availability check, honouring an injected WebLLMChatCompletionsAdapterOptions.isWebGPUAvailable override.

Returns ​

boolean

true when a WebLLM engine can run in the current environment.


preload() ​

ts
preload(overrides?: Partial<WebLLMChatCompletionsAdapterOptions>): Promise<MLCEngineInterface>;

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:301

Eagerly loads (and caches) the engine so the first dispatch does not pay the model-load cost.

Parameters ​

ParameterTypeDescription
overrides?Partial<WebLLMChatCompletionsAdapterOptions>Optional option overrides layered over the constructor baseline.

Returns ​

Promise<MLCEngineInterface>

The resolved WebLLMEngine.


reset() ​

ts
reset(): void;

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:307

Drops the cached engine and any in-flight load so the next dispatch re-resolves it.

Returns ​

void


isAvailable() ​

ts
static isAvailable(): boolean;

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:276

Whether the runtime can host a WebLLM engine — i.e. WebGPU (navigator.gpu) is present.

Returns ​

boolean

true when WebGPU is available in the current environment.


isWebLLMChatCompletionsAdapter() ​

ts
static isWebLLMChatCompletionsAdapter(value: unknown): value is WebLLMChatCompletionsAdapter;

Defined in: src/batteries/llm/webllm_chat_completions/adapter.ts:1212

Returns true when value is an WebLLMChatCompletionsAdapter instance.

Parameters ​

ParameterTypeDescription
valueunknownThe value to test.

Returns ​

value is WebLLMChatCompletionsAdapter

true when value is an WebLLMChatCompletionsAdapter instance.