Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Typing
When adding or changing a class, make sure to modify the corresponding .rbs file in the `./sig/mindee` folder.
2 changes: 1 addition & 1 deletion bin/v2/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def execute

# Parse the arguments and run the given product command.
# @param command [String]
# @return [Array(Mindee::V2::Parsing::BaseResponse, String)] Response and its summary.
# @return [Array(Mindee::V2::Parsing::BaseInferenceResponse, String)] Response and its summary.
def run_product(command)
@product_parser[command].parse!(@arguments)
@options[:file_path] = @arguments.shift
Expand Down
4 changes: 2 additions & 2 deletions lib/mindee/input/local_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def valid_hmac_signature?(secret_key, signature)
end

# Deserializes a loaded response
# @param response_class [Class<V2::Parsing::BaseResponse>] class to return.
# @return [V2::Parsing::JobResponse, Mindee::V2::Parsing::BaseResponse]
# @param response_class [Class<V2::Parsing::BaseInferenceResponse>] class to return.
# @return [V2::Parsing::JobResponse, Mindee::V2::Parsing::BaseInferenceResponse]
def deserialize_response(response_class)
response_class.new(as_hash) # : Mindee::V2::Parsing::JobResponse | Mindee::V2::Parsing::BaseResponse
end
Expand Down
12 changes: 6 additions & 6 deletions lib/mindee/v2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def enqueue(
)
params_instance = case params
when Hash
product.params_type.from_hash(params: params)
product.parameters_class.from_hash(params: params)
when Mindee::V2::ClientOptions::BaseProductParameters
params
else
Expand All @@ -54,7 +54,7 @@ def get_job_by_url(url)
# Get the result of an inference that was previously enqueued.
# @param product [Class<Mindee::V2::Product::BaseProduct>] The return class.
# @param url [String] HTTPS URL to the result.
# @return [Mindee::V2::Parsing::BaseResponse]
# @return [Mindee::V2::Parsing::BaseInferenceResponse]
def get_result_by_url(product, url)
@mindee_api.req_get_result_by_url(product, url)
end
Expand All @@ -63,7 +63,7 @@ def get_result_by_url(product, url)
# Prefer using `get_result_by_url` using the result URL returned by the Job.
# @param product [Class<Mindee::V2::Product::BaseProduct>] The return class.
# @param resource [String] ID of the inference or URL to the result.
# @return [Mindee::V2::Parsing::BaseResponse]
# @return [Mindee::V2::Parsing::BaseInferenceResponse]
def get_result(product, resource)
@mindee_api.req_get_result_by_id(product, resource)
end
Expand All @@ -83,7 +83,7 @@ def get_job(job_id)
# @param params [Hash, ClientOptions::BaseProductParameters] Parameters for the inference.
# @param polling_options [Hash, PollingOptions, nil] Parameters for polling.
# @param cancellation_token [Mindee::HTTP::CancellationToken, nil] Token for cancellation.
# @return [Parsing::BaseResponse]
# @return [Parsing::BaseInferenceResponse]
def enqueue_and_get_result(
product,
input_source,
Expand All @@ -93,7 +93,7 @@ def enqueue_and_get_result(
)
params_instance = case params
when Hash
product.params_type.from_hash(params: params)
product.parameters_class.from_hash(params: params)
when Mindee::V2::ClientOptions::BaseProductParameters
params
else
Expand Down Expand Up @@ -246,7 +246,7 @@ def poll_on_job(
# @param polling_options [Input::PollingOptions]
# @param wait_for_webhooks [Boolean]
# @param cancellation_token [Mindee::HTTP::CancellationToken, nil]
# @return [Mindee::V2::Parsing::BaseResponse]
# @return [Mindee::V2::Parsing::BaseInferenceResponse]
def poll_for_product_result(
product,
enqueue_response,
Expand Down
30 changes: 30 additions & 0 deletions lib/mindee/v2/client_options/base_annotation_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Mindee
module V2
module ClientOptions
# Base parameters for document annotations.
class BaseAnnotationParameters
# UUID of the annotated document.
# @return [String]
attr_reader :document_id

# Default constructor.
# @param document_id [String] UUID of the annotated document.
def initialize(document_id)
trimmed_id = document_id&.strip
raise Error::MindeeError, 'Document ID must be provided' if trimmed_id.nil? || trimmed_id.empty?

# NOTE: document_id is included in the request URL path, it is not a parameter.
@document_id = trimmed_id
end

# Gets the request parameters for the upload request.
# @return [Hash[String, String]]
def request_parameters
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Mindee
module V2
module ClientOptions
# Base parameters for document upload operations.
class BaseRagDocumentUploadParameters
# UUID of the model that the uploaded RAG document is linked to.
# @return [String]
attr_reader :model_id

# Default constructor.
# @param model_id [String] UUID of the model that the uploaded RAG document is linked to.
def initialize(model_id)
trimmed_id = model_id&.strip
raise Error::MindeeError, 'Model ID must be provided' if trimmed_id.nil? || trimmed_id.empty?

@model_id = trimmed_id
end

# Gets the request parameters for the upload request.
# @return [Hash[String, String]]
def request_parameters
{ 'model_id' => @model_id }
end
end
end
end
end
7 changes: 0 additions & 7 deletions lib/mindee/v2/http/.rubocop.yml

This file was deleted.

36 changes: 21 additions & 15 deletions lib/mindee/v2/http/mindee_api_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def initialize(api_key: nil)

# Send a file to the asynchronous processing queue for inference processing.
#
# @param input_source [Input::Source::LocalInputSource, Input::Source::URLInputSource]
# @param params [ClientOptions::BaseProductParameters]
# @param input_source [Input::Source::LocalInputSource, Input::Source::URLInputSource] Local or remote file
# as an input.
# @param params [ClientOptions::BaseProductParameters] Parameters relating to the enqueueing options.
# @return [Mindee::V2::Parsing::JobResponse]
# @raise [Mindee::Error::MindeeHttpErrorV2]
def req_post_product_enqueue(input_source, params)
Expand All @@ -32,25 +33,29 @@ def req_post_product_enqueue(input_source, params)
Mindee::V2::Parsing::JobResponse.new(process_response(response))
end

# Retrieves a result from a given queue.
# Get the result of an inference that was previously enqueued.
# Raises an error if the server's response contains an error.
#
# @param product [Class<Mindee::V2::Product::BaseProduct>]
# @param inference_id [String] ID of the inference or URL to the result.
# @return [Mindee::V2::Parsing::BaseResponse]
# @param inference_id [String] The inference ID for the result.
# @return [Mindee::V2::Parsing::BaseInferenceResponse]
def req_get_result_by_id(product, inference_id)
req_get_result_by_url(product, "#{@settings.base_url}/v2/products/#{product.slug}/results/#{inference_id}")
end

# Retrieves a queued job.
# Get the status of an inference that was previously enqueued.
# Raises an error if the server's response contains an error.
#
# @param job_id [String] ID of the job or URL to the job.
# @param job_id [String] The Job ID as returned by the enqueue request.
# @return [Mindee::V2::Parsing::JobResponse]
def req_get_job_by_id(job_id)
req_get_job_by_url("#{@settings.base_url}/v2/jobs/#{job_id}")
end

# Retrieves a queued job from its polling URL.
# Get the status of an inference that was previously enqueued.
# Raises an error if the server's response contains an error.
#
# @param url [String] HTTPS URL to the job.
# @param url [String] The polling URL as returned by a Job's polling_url property.
# @return [Mindee::V2::Parsing::JobResponse]
def req_get_job_by_url(url)
validate_https_url!(url)
Expand All @@ -59,22 +64,23 @@ def req_get_job_by_url(url)
Mindee::V2::Parsing::JobResponse.new(process_response(response))
end

# Retrieves a result from its URL.
# Get the result of an inference that was previously enqueued.
# Raises an error if the server's response contains an error.
#
# @param product [Class<Mindee::V2::Product::BaseProduct>]
# @param url [String] HTTPS URL to the result.
# @return [Mindee::V2::Parsing::BaseResponse]
# @param url [String] The URL as returned by a Job's result_url property.
# @return [Mindee::V2::Parsing::BaseInferenceResponse]
def req_get_result_by_url(product, url)
validate_https_url!(url)
@settings.check_api_key
response = poll(url)
product.response_type.new(process_response(response))
product.response_class.new(process_response(response))
end

# Retrieves a list of resources with the given criteria.
#
# @param params [ClientOptions::BaseSearchParameters] Search parameters.
# @return [Mindee::V2::Parsing::Search::BaseSearchResponse] A search response containing the matching
# resources.
# @return [Mindee::V2::Parsing::Search::BaseSearchResponse]
def req_get_search(params)
@settings.check_api_key
uri = URI("#{@settings.base_url}/v2/search/#{params.slug}")
Expand Down
4 changes: 3 additions & 1 deletion lib/mindee/v2/parsing.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require_relative 'parsing/common_response'
require_relative 'parsing/base_response'
Comment thread
ianardee marked this conversation as resolved.
Resolved
require_relative 'parsing/base_inference_response'
require_relative 'parsing/base_rag_annotation_response'
require_relative 'parsing/error_response'
require_relative 'parsing/field'
require_relative 'parsing/inference_file'
Expand Down
15 changes: 15 additions & 0 deletions lib/mindee/v2/parsing/base_inference_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require_relative 'base_response'

module Mindee
module V2
module Parsing
# Base class for V2 inference responses.
class BaseInferenceResponse < Mindee::V2::Parsing::BaseResponse
# @return [BaseInference] The inference result for a split utility request
attr_reader :inference
end
end
end
end
40 changes: 40 additions & 0 deletions lib/mindee/v2/parsing/base_rag_annotation_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'time'
require_relative 'base_inference_response'

module Mindee
module V2
module Parsing
# Base class for all RAG document responses from the V2 API.
class BaseRagAnnotationResponse < BaseInferenceResponse
# Unique identifier of the RAG document.
# @return [String]
attr_reader :id

# Original filename of the uploaded document.
# @return [String]
attr_reader :filename

# Date and time of the document creation.
# @return [Time]
attr_reader :created_at

# Current status of the RAG document.
# @return [String]
attr_reader :status

# Default constructor.
# @param server_response [Hash[String | Symbol, untyped]]
def initialize(server_response)
super

@id = server_response['id']
@filename = server_response['filename']
@created_at = Time.parse(server_response['created_at'])
@status = server_response['status']
end
end
end
end
end
15 changes: 10 additions & 5 deletions lib/mindee/v2/parsing/base_response.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# frozen_string_literal: true

require_relative 'common_response'
require 'json'

module Mindee
module V2
module Parsing
# Base class for V2 inference responses.
class BaseResponse < Mindee::V2::Parsing::CommonResponse
# @return [BaseInference] The inference result for a split utility request
attr_reader :inference
# Base class for inference and job responses on the V2 API.
class BaseResponse
Comment thread
ianardee marked this conversation as resolved.
Resolved
# @return [String]
attr_reader :raw_http

# @param http_response [Hash]
def initialize(http_response)
@raw_http = JSON.generate(http_response)
end
end
end
end
Expand Down
20 changes: 0 additions & 20 deletions lib/mindee/v2/parsing/common_response.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/mindee/v2/parsing/failed_inference_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mindee
module V2
module Parsing
# "Webhook payload returned when an inference fails before producing a result.
class FailedInferenceResponse < Mindee::V2::Parsing::CommonResponse
class FailedInferenceResponse < Mindee::V2::Parsing::BaseResponse
# @return [String] UUID of the failed inference.
attr_reader :inference_id
# @return [String] UUID of the model used.
Expand Down
1 change: 0 additions & 1 deletion lib/mindee/v2/parsing/inference_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require_relative 'common_response'
require_relative 'job'

module Mindee
Expand Down
4 changes: 2 additions & 2 deletions lib/mindee/v2/parsing/job_response.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

require_relative 'common_response'
require_relative 'base_response'
require_relative 'job'

module Mindee
module V2
module Parsing
# HTTP response wrapper that embeds a V2 job.
class JobResponse < CommonResponse
class JobResponse < BaseResponse
# @return [Job] Parsed job payload.
attr_reader :job

Expand Down
2 changes: 1 addition & 1 deletion lib/mindee/v2/parsing/search/base_search_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module V2
module Parsing
module Search
# Base class for search responses.
class BaseSearchResponse < CommonResponse
class BaseSearchResponse < BaseResponse
# Pagination metadata.
# @return [Mindee::V2::Parsing::Search::PaginationMetadata]
attr_reader :pagination
Expand Down
Loading
Loading