All files / kernel-language-model-service/src/ollama types.ts

100% Statements 2/2
100% Branches 0/0
100% Functions 0/0
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                                                              3x                               3x                                      
import { object, optional, number, size, string } from '@metamask/superstruct';
import type { Infer } from '@metamask/superstruct';
import type {
  GenerateRequest,
  GenerateResponse,
  ListResponse,
  AbortableAsyncIterator,
  Config,
} from 'ollama';
 
import type { LanguageModel } from '../types.ts';
 
/**
 * Interface for an Ollama client that can list models and generate responses.
 * Provides the minimal interface required for Ollama operations.
 */
type OllamaClient = {
  list: () => Promise<ListResponse>;
  generate: (
    request: GenerateRequest,
  ) => Promise<AbortableAsyncIterator<GenerateResponse>>;
};
export type { GenerateRequest, GenerateResponse, OllamaClient };
 
/**
 * Configuration for creating an Ollama service in a Node.js environment.
 * Requires a fetch implementation to be provided as an endowment for security.
 */
export type OllamaNodejsConfig = {
  endowments: { fetch: typeof fetch };
  clientConfig?: Partial<Omit<Config, 'fetch'>>;
};
 
/**
 * Superstruct schema for Ollama model options.
 * Defines the validation rules for model generation parameters.
 *
 * Note: Uses snake_case to match Ollama's Python-style API.
 */
export const OllamaModelOptionsStruct = object({
  // Ollama is pythonic, using snake_case for its options.
 
  temperature: optional(number()),
  top_p: optional(number()),
  top_k: optional(number()),
  repeat_penalty: optional(number()),
  repeat_last_n: optional(number()),
  seed: optional(number()),
  num_ctx: optional(number()),
});
 
/**
 * Superstruct schema for Ollama instance configuration.
 * Validates that the model name is a non-empty string.
 */
export const OllamaInstanceConfigStruct = object({
  model: size(string(), 1, Infinity),
  options: optional(OllamaModelOptionsStruct),
});
 
/**
 * Type representing valid Ollama model options.
 */
export type OllamaModelOptions = Infer<typeof OllamaModelOptionsStruct>;
 
/**
 * Type representing valid Ollama instance configuration.
 */
export type OllamaInstanceConfig = Infer<typeof OllamaInstanceConfigStruct>;
 
/**
 * Type representing an Ollama language model instance.
 */
export type OllamaModel = LanguageModel<OllamaModelOptions, GenerateResponse>;