Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.1 KB
Raw Blame
import { AIProviderName, isNil } from '@activepieces/core-utils';
import { ModelChoice } from '@activepieces/shared';

function sameConfig({
  left,
  right,
}: {
  left: unknown;
  right: unknown;
}): boolean {
  return JSON.stringify(left) === JSON.stringify(right);
}

function modelPickChanged({
  picked,
  current,
}: {
  picked: ModelPick;
  current: ModelPick;
}): boolean {
  const same = (left?: string | null, right?: string | null) =>
    (left ?? null) === (right ?? null);
  return !(
    same(picked.provider, current.provider) &&
    same(picked.modelName, current.modelName) &&
    same(picked.providerConfigId, current.providerConfigId) &&
    same(picked.modelTierId, current.modelTierId)
  );
}

function hasModel({ config }: { config: ModelPick }): boolean {
  return (
    !isNil(config.modelTierId) ||
    (!isNil(config.provider) && !isNil(config.modelName))
  );
}

function modelChoiceOf({ config }: { config: ModelPick }): ModelChoice | null {
  if (!isNil(config.modelTierId)) {
    return { type: 'tier', tierId: config.modelTierId };
  }
  const provider = Object.values(AIProviderName).find(
    (name) => name === config.provider,
  );
  if (isNil(provider) || isNil(config.modelName)) {
    return null;
  }
  return {
    type: 'model',
    provider,
    providerConfigId: config.providerConfigId ?? '',
    modelId: config.modelName,
  };
}

function modelPickOf({ choice }: { choice: ModelChoice }): ModelPick {
  return choice.type === 'tier'
    ? {
        provider: null,
        modelName: null,
        providerConfigId: null,
        modelTierId: choice.tierId,
      }
    : {
        provider: choice.provider,
        modelName: choice.modelId,
        providerConfigId: choice.providerConfigId,
        modelTierId: null,
      };
}

function serverMovedWhileTyping({
  fromServer,
  lastSeen,
  unsavedTyping,
}: {
  fromServer: unknown;
  lastSeen: unknown;
  unsavedTyping: boolean;
}): boolean {
  return unsavedTyping && !sameConfig({ left: fromServer, right: lastSeen });
}

function leaveGuard({
  blockerState,
  exitRequested,
}: {
  blockerState: string;
  exitRequested: boolean;
}): LeaveGuard {
  if (blockerState !== 'blocked' && !exitRequested) {
    return { open: false, discardAction: 'none' };
  }
  return {
    open: true,
    discardAction: blockerState === 'blocked' ? 'proceed' : 'exit',
  };
}

function createWriteLock(): WriteLock {
  let held = false;
  return {
    claim: () => {
      if (held) return false;
      held = true;
      return true;
    },
    release: () => {
      held = false;
    },
    held: () => held,
  };
}

export const agentEditState = {
  sameConfig,
  serverMovedWhileTyping,
  modelPickChanged,
  hasModel,
  modelChoiceOf,
  modelPickOf,
  leaveGuard,
  createWriteLock,
};

export type ModelPick = {
  provider?: string | null;
  modelName?: string | null;
  providerConfigId?: string | null;
  modelTierId?: string | null;
};
export type LeaveGuard = {
  open: boolean;
  discardAction: 'proceed' | 'exit' | 'none';
};
export type WriteLock = {
  claim: () => boolean;
  release: () => void;
  held: () => boolean;
};