Skip to content

feat(ai): add failover rule helpers#1484

Merged
hwbrzzl merged 4 commits into
masterfrom
optimize-ai-openai-failover-reasons
Jun 10, 2026
Merged

feat(ai): add failover rule helpers#1484
hwbrzzl merged 4 commits into
masterfrom
optimize-ai-openai-failover-reasons

Conversation

@goravel-coder

@goravel-coder goravel-coder commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds AI failover rule helpers that external providers can use to classify provider-specific errors.
  • Supports plain substring patterns and slash-delimited regex patterns for custom failover reasons.
  • Removes the embedded OpenAI provider now that OpenAI lives in goravel/openai.

Why

The framework should stay provider-agnostic: it should react to FailoverError, while provider packages decide how their own errors map to failover reasons. The new helper API lets external providers compile and apply user-provided failover rules without moving provider-specific matching into the framework runtime.

package config

import (
	openai "github.com/goravel/openai"

	"github.com/goravel/framework/facades"
)

func init() {
	config := facades.Config()
	config.Add("ai", map[string]any{
		"default": "openai",
		"providers": map[string]any{
			"openai": map[string]any{
				"via": openai.New(),
				"failover": map[string][]string{
					"context_length_exceeded": {
						"maximum context length",
						"/(?i)context.*length/",
					},
				},
			},
		},
	})
}

This keeps OpenAI-specific behavior in goravel/openai while the framework owns only the shared contract and helper functions. Providers can call the helpers when they build or execute requests, then return NewFailoverError through the normal failover path.

@goravel-coder goravel-coder requested a review from a team as a code owner June 10, 2026 07:08
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.58824% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.28%. Comparing base (9e0d1e9) to head (c5029bf).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
ai/failover.go 70.58% 10 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1484      +/-   ##
==========================================
- Coverage   69.40%   69.28%   -0.13%     
==========================================
  Files         377      377              
  Lines       30190    29613     -577     
==========================================
- Hits        20954    20517     -437     
+ Misses       8256     8151     -105     
+ Partials      980      945      -35     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread ai/application_test.go Outdated
Comment on lines +932 to +935
tests := []struct {
name string
run func(t *testing.T)
}{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.Run style for these cases.

@goravel-coder goravel-coder changed the title feat(ai): add configurable failover rules feat(ai): add failover rule helpers Jun 10, 2026
@hwbrzzl hwbrzzl merged commit 4520b67 into master Jun 10, 2026
16 of 19 checks passed
@hwbrzzl hwbrzzl deleted the optimize-ai-openai-failover-reasons branch June 10, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants