Class: RailsProof::OpenAiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_proof/open_ai_client.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULT_MODEL =
"gpt-5.6"
RESPONSE_SCHEMA =
{
  type: :object,
  properties: {
    suggestions: {
      type: :array,
      items: {
        type: :object,
        properties: {
          kind: {
            type: :string,
            enum: %w[coverage contract_check]
          },
          name: { type: :string },
          reason: { type: :string },
          test_code: { type: :string }
        },
        required: %w[kind name reason test_code],
        additionalProperties: false
      }
    }
  },
  required: %w[suggestions],
  additionalProperties: false
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: ENV.fetch("RAILSPROOF_OPENAI_MODEL", DEFAULT_MODEL), sdk_client: nil) ⇒ OpenAiClient

Returns a new instance of OpenAiClient.



36
37
38
39
40
41
42
# File 'lib/rails_proof/open_ai_client.rb', line 36

def initialize(
  model: ENV.fetch("RAILSPROOF_OPENAI_MODEL", DEFAULT_MODEL),
  sdk_client: nil
)
  @model = model
  @sdk_client = sdk_client
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



34
35
36
# File 'lib/rails_proof/open_ai_client.rb', line 34

def model
  @model
end

Instance Method Details

#suggest_tests(context:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_proof/open_ai_client.rb', line 44

def suggest_tests(context:)
  response = client.responses.create(
    model: model,
    input: [
      {
        role: :system,
        content: system_prompt
      },
      {
        role: :user,
        content: JSON.generate(context)
      }
    ],
    text: {
      format: {
        type: :json_schema,
        name: "rails_proof_test_suggestions",
        strict: true,
        schema: RESPONSE_SCHEMA
      }
    }
  )

  parse_suggestions(response.output_text)
rescue Error
  raise
rescue StandardError => error
  raise Error, "OpenAI request failed: #{error.message}"
end