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.

3.2 KB
Raw Blame
import type { CallFlags } from '@/call/flags'
import type { CommandContext } from '@/plugins/base'
import type { CatalogOp } from '@/plugins/catalog'
import { readFile } from 'node:fs/promises'
import { LOCAL_FILES } from '@/call/files'
import { unsupportedFlags } from '@/call/flags'
import { applyPins, loadInput, resolveFileRefs } from '@/call/input'
import { rendererFor } from '@/call/render/index'
import { buildRequest } from '@/call/request'
import { BaseError } from '@/errors/base'
import { ErrorCode } from '@/errors/codes'
import { runSignal } from '@/plugins/commands/cancel'
import { assertValidInput, Outcome } from '@/plugins/commands/command'
import { http } from '@/plugins/http'
import { io } from '@/plugins/io'
import { session } from '@/plugins/session'
import { KIND } from '@/protocol/kinds'
import { commandWords } from '@/protocol/op-id'
import { PIN } from '@/protocol/pins'
import { flagToken } from '@/util/flag-name'

/** One catalog operation, its call flags, and the fields typed as flags of their own. */
export type CallRequest = Readonly<{
  id: string
  op: CatalogOp
  flags: CallFlags
  fields: Record<string, unknown>
}>

const FILE_ENCODING = 'utf8'
const DEPRECATED_NOTICE = 'deprecated'
const ONLY_NEEDS_STREAM = '--only requires --stream'

function assertSupportedFlags(kind: string, flags: CallFlags): void {
  const [refused] = unsupportedFlags(kind, flags)
  if (refused !== undefined) {
    throw new BaseError({
      code: ErrorCode.UsageInvalidFlag,
      message: `${flagToken(refused)} is not supported by ${kind} operations`,
    })
  }
  // --only filters the events --stream emits; on its own it filters nothing.
  if (flags.only !== undefined && flags.stream !== true)
    throw new BaseError({ code: ErrorCode.UsageInvalidFlag, message: ONLY_NEEDS_STREAM })
}

async function readAll(stream: NodeJS.ReadableStream): Promise<string> {
  stream.setEncoding(FILE_ENCODING)
  let text = ''
  for await (const chunk of stream) text += chunk
  return text
}

export async function executeCall(req: CallRequest, ctx: CommandContext): Promise<Outcome> {
  assertSupportedFlags(req.op.kind, req.flags)

  const out = await ctx.get(io)
  const source = {
    stdin: () => readAll(out.streams.in),
    readFile: (path: string) => readFile(path, FILE_ENCODING),
  }
  const whole = await loadInput({ ...source, raw: req.flags.input })
  const fields = await resolveFileRefs(req.fields, req.op.input, source)
  const workspaceId = await (await ctx.get(session)).workspaceId()
  const body = applyPins({ ...whole, ...fields }, req.op.input, { [PIN.Workspace]: workspaceId })
  assertValidInput({ command: commandWords(req.id), schema: req.op.input, input: body })

  if (req.op.deprecated) out.notice(`${DEPRECATED_NOTICE}: ${req.id}`)

  // SIGINT cancels the whole invocation; this controller ends only this call's stream.
  const call = new AbortController()
  if (req.op.kind === KIND.Sse) ctx.defer(() => call.abort())
  const signal = AbortSignal.any([runSignal(), call.signal])

  const res = await (
    await ctx.get(http)
  ).request(async (cat) => ({
    ...(await buildRequest(cat.opOrThrow(req.id), body, LOCAL_FILES)),
    signal,
  }))

  const code = await rendererFor(req.op.kind, out)(res, out, req.flags, req.id)
  return new Outcome(code)
}