All files / api/src/ai-providers/google utils.ts

100% Statements 45/45
100% Branches 6/6
100% Functions 1/1
100% Lines 45/45

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 591x       1x 1x     1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x     1x 1x 1x 1x 1x 1x 1x 1x   1x     1x 1x 1x 1x 1x   1x 7x 7x 7x 7x 7x 4x 4x 1x 1x  
import {
  GoogleMessageRole,
  GoogleToolChoiceType,
} from '@api/ai-providers/google/types';
import { ChatCompletionFinishReason } from '@shared/types/api/routes/chat-completions-api';
import { ChatCompletionMessageRole } from '@shared/types/api/routes/shared/messages';
import type { ChatCompletionToolChoice } from '@shared/types/api/routes/shared/tools';
 
export const FinishReasonsGeminiToSuperAgents: {
  [key: string]: ChatCompletionFinishReason;
} = {
  FINISH_REASON_UNSPECIFIED: ChatCompletionFinishReason.STOP,
  STOP: ChatCompletionFinishReason.STOP,
  MAX_TOKENS: ChatCompletionFinishReason.LENGTH,
  UNEXPECTED_TOOL_CALL: ChatCompletionFinishReason.TOOL_CALLS,
  TOO_MANY_TOOL_CALLS: ChatCompletionFinishReason.TOOL_CALLS,
  MALFORMED_FUNCTION_CALL: ChatCompletionFinishReason.FUNCTION_CALL,
  SAFETY: ChatCompletionFinishReason.CONTENT_FILTER,
  RECITATION: ChatCompletionFinishReason.CONTENT_FILTER,
  LANGUAGE: ChatCompletionFinishReason.CONTENT_FILTER,
  OTHER: ChatCompletionFinishReason.CONTENT_FILTER,
  BLOCKLIST: ChatCompletionFinishReason.CONTENT_FILTER,
  PROHIBITED_CONTENT: ChatCompletionFinishReason.CONTENT_FILTER,
  SPII: ChatCompletionFinishReason.CONTENT_FILTER,
  IMAGE_SAFETY: ChatCompletionFinishReason.CONTENT_FILTER,
};
 
export const RoleSuperAgentsToGemini: Record<
  ChatCompletionMessageRole,
  GoogleMessageRole
> = {
  [ChatCompletionMessageRole.ASSISTANT]: GoogleMessageRole.MODEL,
  [ChatCompletionMessageRole.DEVELOPER]: GoogleMessageRole.SYSTEM,
  [ChatCompletionMessageRole.SYSTEM]: GoogleMessageRole.SYSTEM,
  [ChatCompletionMessageRole.TOOL]: GoogleMessageRole.FUNCTION,
  [ChatCompletionMessageRole.FUNCTION]: GoogleMessageRole.FUNCTION,
  [ChatCompletionMessageRole.USER]: GoogleMessageRole.USER,
};
 
const ToolChoiceSuperAgentsStringToGemini: Record<
  string,
  GoogleToolChoiceType
> = {
  auto: GoogleToolChoiceType.AUTO,
  none: GoogleToolChoiceType.NONE,
  required: GoogleToolChoiceType.ANY,
};
 
export const transformToolChoiceSuperAgentsToGemini = (
  tool_choice: ChatCompletionToolChoice,
): GoogleToolChoiceType | undefined => {
  if (typeof tool_choice === 'object' && tool_choice.type === 'function')
    return GoogleToolChoiceType.ANY;
  if (typeof tool_choice === 'string') {
    return ToolChoiceSuperAgentsStringToGemini[tool_choice];
  }
  return undefined;
};