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

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

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                                        4x             4x         4x         4x           4x           4x             4x                 4x         4x                         4x           4x                 4x             4x                         4x                 4x                             4x      
import {
  array,
  boolean,
  literal,
  nullable,
  number,
  object,
  optional,
  size,
  string,
  union,
  unknown,
} from '@metamask/superstruct';
 
export type {
  AssistantStreamDelta,
  ChatChoice,
  ChatMessage,
  ChatParams,
  ChatResult,
  ChatStreamChunk,
  ChatStreamDelta,
  ChatStreamToolCallDelta,
  Tool,
  ToolCall,
  Usage,
} from '../types.ts';
 
const ToolCallStruct = object({
  id: string(),
  type: literal('function'),
  index: optional(number()),
  function: object({ name: string(), arguments: string() }),
});
 
const SystemMessageStruct = object({
  role: literal('system'),
  content: string(),
});
 
const UserMessageStruct = object({
  role: literal('user'),
  content: string(),
});
 
const AssistantMessageStruct = object({
  role: literal('assistant'),
  content: nullable(string()),
  tool_calls: optional(array(ToolCallStruct)),
});
 
const ToolMessageStruct = object({
  role: literal('tool'),
  content: string(),
  tool_call_id: string(),
});
 
const ChatMessageStruct = union([
  SystemMessageStruct,
  UserMessageStruct,
  AssistantMessageStruct,
  ToolMessageStruct,
]);
 
const ToolStruct = object({
  type: literal('function'),
  function: object({
    name: string(),
    description: optional(string()),
    parameters: optional(unknown()),
  }),
});
 
const StopStruct = optional(union([string(), array(string())]));
 
/**
 * Superstruct schema for chat completion request parameters.
 */
export const ChatParamsStruct = object({
  model: size(string(), 1, Infinity),
  messages: array(ChatMessageStruct),
  tools: optional(array(ToolStruct)),
  max_tokens: optional(number()),
  temperature: optional(number()),
  top_p: optional(number()),
  stop: StopStruct,
  seed: optional(number()),
  n: optional(number()),
  stream: optional(boolean()),
});
 
const UsageStruct = object({
  prompt_tokens: number(),
  completion_tokens: number(),
  total_tokens: number(),
});
 
const ChatChoiceStruct = object({
  message: ChatMessageStruct,
  index: number(),
  finish_reason: nullable(string()),
});
 
/**
 * Superstruct schema for a non-streaming `/v1/chat/completions` response body.
 */
export const ChatResultStruct = object({
  id: string(),
  model: string(),
  choices: array(ChatChoiceStruct),
  usage: UsageStruct,
});
 
const ChatStreamToolCallDeltaStruct = object({
  index: optional(number()),
  id: optional(string()),
  type: optional(literal('function')),
  function: optional(
    object({
      name: optional(string()),
      arguments: optional(string()),
    }),
  ),
});
 
/** Wire `delta` before streaming normalization adds `role: 'assistant'`. */
const ChatStreamDeltaWireStruct = object({
  role: optional(literal('assistant')),
  content: optional(string()),
  tool_calls: optional(array(ChatStreamToolCallDeltaStruct)),
});
 
/**
 * Superstruct schema for one SSE `data:` JSON object from `/v1/chat/completions` when `stream: true`.
 */
export const ChatStreamChunkStruct = object({
  id: string(),
  model: string(),
  choices: array(
    object({
      delta: ChatStreamDeltaWireStruct,
      index: number(),
      finish_reason: nullable(string()),
    }),
  ),
});
 
/**
 * Superstruct schema for a `/v1/models` response body (only fields we read).
 */
export const ListModelsResponseStruct = object({
  data: array(object({ id: string() })),
});