Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

7.7 KB
Raw Blame
/*
 *  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import {
  DSL,
  IAgentLogsRequest,
  IPipeLineListRequest,
} from '@/interfaces/database/agent';
import { IAgentWebhookTraceRequest } from '@/interfaces/request/agent';
import api from '@/utils/api';
import { registerNextServer } from '@/utils/register-server';
import request from '@/utils/request';
import dayjs from 'dayjs';

const {
  createAgent,
  updateAgent: updateAgentApi,
  listAgents,
  deleteAgent,
  agentChatCompletion,
  resetAgent,
  listAgentTemplate,
  testDbConnect,
  getInputElements,
  trace,
  fetchVersionList,
  fetchVersion,
  deleteVersion,
  updateVersion: updateVersionApi,
  getAgent,
  fetchAgentSessions,
  fetchExternalAgentInputs,
  prompt,
  cancelDataflow,
  cancelCanvas,
  listBuiltinPipelines,
  getBuiltinPipeline,
} = api;

const methods = {
  getAgent: {
    url: getAgent,
    method: 'get',
  },
  createAgent: {
    url: createAgent,
    method: 'post',
  },
  fetchVersionList: {
    url: fetchVersionList,
    method: 'get',
  },
  fetchVersion: {
    url: (config: { agentId: string; versionId: string }) =>
      fetchVersion(config.agentId, config.versionId),
    method: 'get',
  },
  deleteVersion: {
    url: (config: { agentId: string; versionId: string }) =>
      deleteVersion(config.agentId, config.versionId),
    method: 'delete',
  },
  updateVersion: {
    url: (config: { agentId: string; versionId: string }) =>
      updateVersionApi(config.agentId, config.versionId),
    method: 'put',
  },
  applyVersion: {
    url: (config: { agentId: string; versionId: string }) =>
      api.applyVersion(config.agentId, config.versionId),
    method: 'post',
  },
  listAgents: {
    url: listAgents,
    method: 'get',
  },
  listAgentVersions: {
    url: api.listAgentVersions,
    method: 'get',
  },
  listAgentTags: {
    url: api.listAgentTags,
    method: 'get',
  },
  resetAgent: {
    url: resetAgent,
    method: 'post',
  },
  deleteAgent: {
    url: deleteAgent,
    method: 'delete',
  },
  agentChatCompletion: {
    url: agentChatCompletion,
    method: 'post',
  },
  listAgentTemplate: {
    url: listAgentTemplate,
    method: 'get',
  },
  testDbConnect: {
    url: testDbConnect,
    method: 'post',
  },
  getInputElements: {
    url: getInputElements,
    method: 'get',
  },
  debugSingle: {
    url: (config: { agentId: string; componentId: string }) =>
      api.debug(config.agentId, config.componentId),
    method: 'post',
  },
  uploadAgentFile: {
    url: (config: { agentId: string }) => api.uploadAgentFile(config.agentId),
    method: 'post',
  },
  trace: {
    url: (config: { agentId: string; messageId: string }) =>
      trace(config.agentId, config.messageId),
    method: 'get',
  },
  inputForm: {
    url: (config: { agentId: string; componentId: string }) =>
      api.inputForm(config.agentId, config.componentId),
    method: 'get',
  },
  fetchAgentLogs: {
    url: fetchAgentSessions,
    method: 'get',
  },
  fetchExternalAgentInputs: {
    url: fetchExternalAgentInputs,
    method: 'get',
  },
  fetchPrompt: {
    url: prompt,
    method: 'get',
  },
  cancelDataflow: {
    url: cancelDataflow,
    method: 'post',
  },
  cancelCanvas: {
    url: cancelCanvas,
    method: 'post',
  },
  createAgentSession: {
    url: api.createAgentSession,
    method: 'post',
  },
  listBuiltinPipelines: {
    url: listBuiltinPipelines,
    method: 'get',
  },
  getBuiltinPipeline: {
    url: getBuiltinPipeline,
    method: 'get',
  },
} as const;

const agentService = registerNextServer<keyof typeof methods>(methods);

export const updateAgent = (
  agentId: string,
  params: {
    title?: string;
    dsl?: DSL | Record<string, any>;
    avatar?: string | null;
    description?: string | null;
    permission?: string;
    release?: string;
    autosave?: boolean;
    alias?: string;
  },
) => {
  return request(updateAgentApi(agentId), { method: 'put', data: params });
};

export const updateVersion = (
  agentId: string,
  versionId: string,
  params: { alias?: string; title?: string; description?: string },
) => {
  return request(updateVersionApi(agentId, versionId), {
    method: 'patch',
    data: params,
  });
};

export const updateAgentTags = (agentId: string, tags: string[]) => {
  return request(api.updateAgentTags(agentId), {
    method: 'put',
    data: { tags: tags.join(',') },
  });
};

export const fetchTrace = (data: { canvas_id: string; message_id: string }) => {
  return request.get(
    methods.trace.url({
      agentId: data.canvas_id,
      messageId: data.message_id,
    }),
  );
};

// Used by the shared/embedded chat page where the only credential available
// is the share (beta) APIToken (fixes #14985).
export const fetchSharedTrace = (data: {
  shared_id: string;
  message_id: string;
}) => {
  return request.get(api.sharedTrace(data.shared_id, data.message_id));
};
export const fetchAgentLogsByCanvasId = (
  canvasId: string,
  params: IAgentLogsRequest,
) => {
  // Serialize Date values as local wall-clock strings ("YYYY-MM-DD HH:mm:ss").
  // Axios' default serializer turns a Date into a UTC ISO string, which the
  // backend then shifts by the server timezone — causing the picked local day
  // to mismatch the server-local dates shown in the table. Sending a plain
  // local datetime makes the backend compare it as-is against stored dates.
  // from_date snaps to the start of the day (00:00:00), to_date to the end
  // (23:59:59), so the full picked day range is covered.
  const normalizeDate = (value: string | Date | undefined, isEnd = false) => {
    if (!(value instanceof Date)) return value;
    const day = dayjs(value);
    return (isEnd ? day.endOf('day') : day.startOf('day')).format(
      'YYYY-MM-DD HH:mm:ss',
    );
  };

  const normalizedParams: IAgentLogsRequest = {
    ...params,
    from_date: normalizeDate(params.from_date),
    to_date: normalizeDate(params.to_date, true),
  };

  return request.get(methods.fetchAgentLogs.url(canvasId), {
    params: normalizedParams,
  });
};

export const fetchAgentLogsById = (
  canvasId: string,
  sessionId: string,
  params?: Pick<IAgentLogsRequest, 'include_reference' | 'include_prompt'>,
) => {
  return request.get(api.fetchAgentSessionById(canvasId, sessionId), {
    params,
  });
};

export const fetchPipeLineList = (params: IPipeLineListRequest) => {
  return request.get(api.listAgents, { params: params });
};

export const fetchWebhookTrace = (
  id: string,
  params: IAgentWebhookTraceRequest,
) => {
  return request.get(api.fetchWebhookTrace(id), { params: params });
};

export const issueAgentWebhookCredential = (id: string) =>
  request.post(api.issueAgentWebhookCredential(id), { data: {} });

export const revokeAgentWebhookCredential = (id: string) =>
  request.delete(api.revokeAgentWebhookCredential(id));

export function createAgentSession({ id, name }: { id: string; name: string }) {
  return request.post(api.createAgentSession(id), { data: { name } });
}

export const deleteAgentSession = (canvasId: string, sessionId: string) => {
  return request.delete(api.fetchAgentSessionById(canvasId, sessionId));
};

export const uploadAgentFile = (agentId: string, data: FormData) => {
  return request(api.uploadAgentFile(agentId), {
    method: 'post',
    data,
  });
};

export default agentService;