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

100% Statements 8/8
100% Branches 2/2
100% Functions 3/3
100% Lines 8/8

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                    4x                           4x       8x   3x     3x 1x   2x     8x    
import type {
  ChatParams,
  ChatResult,
  SampleParams,
  SampleResult,
} from './types.ts';
 
/**
 * Canonical service name for the language model service in `ClusterConfig.services`.
 */
export const LANGUAGE_MODEL_SERVICE_NAME = 'languageModelService';
 
/**
 * Wraps `chat` and optional `sample` functions into a flat, stateless kernel service object.
 * Use the returned `{ name, service }` with `kernel.registerKernelServiceObject(name, service)`.
 *
 * Return values are plain hardened data — no exos — so they are safely serializable
 * across the kernel marshal boundary.
 *
 * @param chat - Function that performs a chat completion request.
 * @param sample - Optional function that performs a raw token-prediction request.
 *   If not provided, `service.sample()` throws "raw sampling not supported by this backend".
 * @returns An object with `name` and `service` fields for use with the kernel.
 */
export const makeKernelLanguageModelService = (
  chat: (params: ChatParams & { stream?: true & false }) => Promise<ChatResult>,
  sample?: (params: SampleParams) => Promise<SampleResult>,
): { name: string; service: object } => {
  const service = harden({
    async chat(params: ChatParams): Promise<ChatResult> {
      return harden(await chat(params as ChatParams & { stream?: never }));
    },
    async sample(params: SampleParams): Promise<SampleResult> {
      if (!sample) {
        throw new Error('raw sampling not supported by this backend');
      }
      return harden(await sample(params));
    },
  });
  return harden({ name: LANGUAGE_MODEL_SERVICE_NAME, service });
};