Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.6 KB
Raw Blame
// These Python endpoints expect repeated keys; oRPC v1 serializes arrays with indexed keys.
const repeatedQueryArrayRules = [
  { path: /\/agent$/, fields: ['tag_ids', 'creator_ids'] },
  { path: /\/agent\/[^/]+\/logs$/, fields: ['sources', 'statuses'] },
  { path: /\/agent\/[^/]+\/logs\/[^/]+\/messages$/, fields: ['sources', 'statuses'] },
  { path: /\/apps$/, fields: ['tag_ids', 'creator_ids'] },
  { path: /\/apps\/starred$/, fields: ['tag_ids', 'creator_ids'] },
  { path: /\/apps\/[^/]+\/annotations$/, fields: ['annotation_id'] },
  { path: /\/datasets$/, fields: ['ids', 'tag_ids', 'creator_ids'] },
  { path: /\/datasets\/[^/]+\/documents$/, fields: ['document_id'] },
  { path: /\/datasets\/[^/]+\/documents\/status\/[^/]+\/batch$/, fields: ['document_id'] },
  { path: /\/datasets\/[^/]+\/documents\/[^/]+\/segment\/[^/]+$/, fields: ['segment_id'] },
  { path: /\/datasets\/[^/]+\/documents\/[^/]+\/segments$/, fields: ['segment_id', 'status'] },
  { path: /\/knowledge-fs\/spaces$/, fields: ['creator_ids', 'tag_ids'] },
  { path: /\/trial-apps\/[^/]+\/datasets$/, fields: ['ids'] },
  { path: /\/workspaces\/current\/customized-snippets$/, fields: ['tag_ids', 'creators'] },
  {
    path: /\/workspaces\/current\/human-input\/v2\/contact-options\/batch$/,
    fields: ['contact_ids'],
  },
  { path: /\/workspaces\/current\/human-input\/v2\/contacts\/batch$/, fields: ['contact_ids'] },
  { path: /\/workspaces\/current\/skills$/, fields: ['tag'] },
  { path: /\/workspaces\/current\/plugin\/[^/]+\/list$/, fields: ['tags'] },
  {
    path: /\/workspaces\/current\/tool-provider\/builtin\/[^/]+\/credential\/info$/,
    fields: ['include_credential_ids'],
  },
  {
    path: /\/workspaces\/current\/tool-provider\/builtin\/[^/]+\/credentials$/,
    fields: ['include_credential_ids'],
  },
]

export function normalizeConsoleOpenAPIURL(url: string | URL) {
  const normalizedUrl = new URL(url)
  const rule = repeatedQueryArrayRules.find(({ path }) => path.test(normalizedUrl.pathname))

  for (const field of rule?.fields ?? []) {
    const pattern = new RegExp(`^${field}\\[(\\d+)\\]$`)
    const values: Array<{ index: number; value: string }> = []
    const indexedKeys = new Set<string>()

    for (const [key, value] of normalizedUrl.searchParams) {
      const match = pattern.exec(key)
      if (!match) continue
      indexedKeys.add(key)
      values.push({ index: Number(match[1]), value })
    }

    for (const key of indexedKeys) normalizedUrl.searchParams.delete(key)
    for (const { value } of values.sort((a, b) => a.index - b.index))
      normalizedUrl.searchParams.append(field, value)
  }

  return normalizedUrl.href
}