import type { IModelVendor } from '../IModelVendor';
import type { OpenAIAccessSchema } from '../../server/openai/openai.access';

import { ModelVendorOpenAI } from '../openai/openai.vendor';


interface DTogetherAIServiceSettings {
  togetherKey: string;
  togetherHost: string;
  togetherFreeTrial: boolean;
  csf?: boolean;
}

export const ModelVendorTogetherAI: IModelVendor<DTogetherAIServiceSettings, OpenAIAccessSchema> = {
  id: 'togetherai',
  name: 'Together AI',
  displayRank: 34,
  displayGroup: 'cloud',
  location: 'cloud',
  instanceLimit: 1,
  hasServerConfigKey: 'hasLlmTogetherAI',

  /// client-side-fetch ///
  csfAvailable: _csfTogetherAIAvailable,

  // functions
  initializeSetup: () => ({
    togetherKey: '',
    togetherHost: 'https://api.together.xyz',
    togetherFreeTrial: false,
  }),
  validateSetup: (setup) => {
    return setup.togetherKey?.length >= 50;
  },
  getTransportAccess: (partialSetup) => ({
    dialect: 'togetherai',
    clientSideFetch: _csfTogetherAIAvailable(partialSetup) && !!partialSetup?.csf,
    oaiKey: partialSetup?.togetherKey || '',
    oaiOrg: '',
    oaiHost: partialSetup?.togetherHost || '',
    heliKey: '',
  }),

  // there is delay for Together Free API calls
  rateLimitChatGenerate: async (_llm, partialSetup) => {
    const now = Date.now();
    const elapsed = now - nextGenerationTs;
    const wait = partialSetup?.togetherFreeTrial
      ? 1000 + 50 /* 1 seconds for free call, plus some safety margin */
      : 50;

    if (elapsed < wait) {
      const delay = wait - elapsed;
      nextGenerationTs = now + delay;
      await new Promise(resolve => setTimeout(resolve, delay));
    } else {
      nextGenerationTs = now;
    }
  },


  // OpenAI transport ('togetherai' dialect in 'access')
  rpcUpdateModelsOrThrow: ModelVendorOpenAI.rpcUpdateModelsOrThrow,

};

// rate limit timestamp
let nextGenerationTs = 0;

function _csfTogetherAIAvailable(s?: Partial<DTogetherAIServiceSettings>) {
  return !!s?.togetherKey;
}
