Conjure.Files.Anthropic (Conjure v0.1.1-alpha)

View Source

Download files created by Anthropic Skills via the Files API.

Skills that create documents (xlsx, pptx, pdf) include file references in the response. This module provides functions to download and manage those files.

API Callback Format

All functions accept an api_callback for HTTP operations:

api_callback = fn method, path, body, opts ->
  # Make HTTP request to Anthropic API
  # Return {:ok, response_body} or {:error, reason}
end

Example

# Extract file IDs from response
file_ids = Conjure.API.Anthropic.extract_file_ids(response)

# Download each file
for file_id <- file_ids do
  {:ok, content, filename} = Conjure.Files.Anthropic.download(
    file_id,
    api_callback
  )

  File.write!(filename, content)
end

References

Summary

Functions

Delete a file from Anthropic.

Download multiple files to a directory.

Download file content only (without metadata).

Extract file IDs from an API response.

List files in your Anthropic account.

Get metadata about a file.

Types

api_callback()

@type api_callback() :: (method :: atom(),
                   path :: String.t(),
                   body :: map() | nil,
                   opts :: keyword() ->
                     {:ok, map() | binary()} | {:error, term()})

file_metadata()

@type file_metadata() :: %{
  id: String.t(),
  filename: String.t(),
  size_bytes: non_neg_integer(),
  created_at: String.t(),
  purpose: String.t()
}

Functions

delete(file_id, api_callback, opts \\ [])

@spec delete(String.t(), api_callback(), keyword()) ::
  :ok | {:error, Conjure.Error.t()}

Delete a file from Anthropic.

Example

:ok = Conjure.Files.Anthropic.delete("file_abc123", api_callback)

download(file_id, api_callback, opts \\ [])

@spec download(String.t(), api_callback(), keyword()) ::
  {:ok, binary(), String.t()} | {:error, Conjure.Error.t()}

Download file content.

Returns the file content as binary data along with the filename.

Example

{:ok, content, filename} = Conjure.Files.Anthropic.download(
  "file_abc123",
  api_callback
)

File.write!(filename, content)

download_all(file_ids, directory, api_callback, opts \\ [])

@spec download_all([String.t()], Path.t(), api_callback(), keyword()) :: [
  ok: Path.t(),
  error: Conjure.Error.t()
]

Download multiple files to a directory.

Returns a list of results for each file.

Example

file_ids = ["file_abc", "file_def"]

results = Conjure.Files.Anthropic.download_all(
  file_ids,
  "/tmp/downloads",
  api_callback
)

for {file_id, result} <- Enum.zip(file_ids, results) do
  case result do
    {:ok, path} -> IO.puts("Downloaded: #{path}")
    {:error, error} -> IO.puts("Failed #{file_id}: #{error.message}")
  end
end

download_content(file_id, api_callback, opts \\ [])

@spec download_content(String.t(), api_callback(), keyword()) ::
  {:ok, binary()} | {:error, Conjure.Error.t()}

Download file content only (without metadata).

Use this if you already know the filename or don't need it.

Example

{:ok, content} = Conjure.Files.Anthropic.download_content(
  "file_abc123",
  api_callback
)

download_to_file(file_id, directory, api_callback, opts \\ [])

@spec download_to_file(String.t(), Path.t(), api_callback(), keyword()) ::
  {:ok, Path.t()} | {:error, Conjure.Error.t()}

Download a file and save it to disk.

Options

  • :overwrite - Overwrite existing file (default: false)

Example

{:ok, path} = Conjure.Files.Anthropic.download_to_file(
  "file_abc123",
  "/tmp/downloads",
  api_callback
)

# path => "/tmp/downloads/budget_2024.xlsx"

extract_file_ids(response)

@spec extract_file_ids(map()) :: [String.t()]

Extract file IDs from an API response.

Delegates to Conjure.API.Anthropic.extract_file_ids/1.

Example

file_ids = Conjure.Files.Anthropic.extract_file_ids(response)
# => ["file_abc123", "file_def456"]

list(api_callback, opts \\ [])

@spec list(
  api_callback(),
  keyword()
) :: {:ok, [map()]} | {:error, Conjure.Error.t()}

List files in your Anthropic account.

Options

  • :limit - Maximum number of files to return (default: 100)
  • :purpose - Filter by purpose

Example

{:ok, files} = Conjure.Files.Anthropic.list(api_callback)

for file <- files do
  IO.puts("#{file["filename"]} (#{file["size_bytes"]} bytes)")
end

metadata(file_id, api_callback, opts \\ [])

@spec metadata(String.t(), api_callback(), keyword()) ::
  {:ok, file_metadata()} | {:error, Conjure.Error.t()}

Get metadata about a file.

Returns file information including filename, size, and creation time.

Example

{:ok, metadata} = Conjure.Files.Anthropic.metadata("file_abc123", api_callback)

metadata.filename
# => "budget_2024.xlsx"

metadata.size_bytes
# => 15234