Address Homarr provider review feedback
This commit is contained in:
1 parent
4ebaea29fd
commit
ea9075bde5
9 files changed
+199
-88
No files matched your search
@@ -25,11 +25,14 @@ const (
|
||||
defaultDailyLimit = 50
|
||||
maxProviderRequestUnits = 1000
|
||||
maxChatRequestBytes = 8 << 20
|
||||
maxChatResponseBytes = 8 << 20
|
||||
maxUpstreamErrorBytes = 1 << 20
|
||||
openRouterDefaultURL = "https://openrouter.ai/api/v1"
|
||||
providerRequestTimeout = 5 * time.Minute
|
||||
)
|
||||
|
||||
var errProviderResponseTooLarge = errors.New("provider response is too large")
|
||||
|
||||
type homarrProvider struct {
|
||||
apiKey string
|
||||
baseURL string
|
||||
@@ -119,6 +122,9 @@ func newHomarrProviderFromEnv() (*homarrProvider, error) {
|
||||
}
|
||||
|
||||
func (provider *homarrProvider) models(event *core.RequestEvent) error {
|
||||
if provider.apiKey == "" {
|
||||
return event.JSON(http.StatusServiceUnavailable, openAIError("The Homarr provider is not configured."))
|
||||
}
|
||||
return event.JSON(http.StatusOK, map[string]any{
|
||||
"object": "list",
|
||||
"data": []map[string]any{{
|
||||
@@ -245,15 +251,27 @@ func (provider *homarrProvider) chat(event *core.RequestEvent) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
responseBody, readErr := io.ReadAll(upstreamResponse.Body)
|
||||
usage := usageFromJSON(responseBody)
|
||||
provider.finishActivity(event.App, privateRequest, publicActivity, statusForError(readErr), usage, startedAt)
|
||||
responseBody, readErr := readBoundedBody(upstreamResponse.Body, maxChatResponseBytes)
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
provider.finishActivity(event.App, privateRequest, publicActivity, "failed", requestUsage{}, startedAt)
|
||||
return event.JSON(http.StatusBadGateway, openAIError("The model endpoint returned an invalid response."))
|
||||
}
|
||||
usage := usageFromJSON(responseBody)
|
||||
provider.finishActivity(event.App, privateRequest, publicActivity, "completed", usage, startedAt)
|
||||
return event.Blob(upstreamResponse.StatusCode, contentType, responseBody)
|
||||
}
|
||||
|
||||
func readBoundedBody(reader io.Reader, limit int64) ([]byte, error) {
|
||||
body, err := io.ReadAll(io.LimitReader(reader, limit+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if int64(len(body)) > limit {
|
||||
return nil, errProviderResponseTooLarge
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func countRequestUnits(messages []providerMessage) int {
|
||||
units := 1
|
||||
for index := len(messages) - 1; index >= 0 && messages[index].Role == "tool"; index-- {
|
||||
@@ -386,6 +404,9 @@ func (provider *homarrProvider) finishActivity(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !requestBelongsToQuotaDay(quota.GetString("day"), startedAt) {
|
||||
return nil
|
||||
}
|
||||
quota.Set("inputTokens", quota.GetInt("inputTokens")+usage.InputTokens)
|
||||
quota.Set("outputTokens", quota.GetInt("outputTokens")+usage.OutputTokens)
|
||||
quota.Set("totalTokens", quota.GetInt("totalTokens")+usage.TotalTokens)
|
||||
@@ -393,6 +414,10 @@ func (provider *homarrProvider) finishActivity(
|
||||
})
|
||||
}
|
||||
|
||||
func requestBelongsToQuotaDay(day string, startedAt time.Time) bool {
|
||||
return day == startedAt.UTC().Format(time.DateOnly)
|
||||
}
|
||||
|
||||
func statusForError(err error) string {
|
||||
if err != nil {
|
||||
return "failed"
|
||||
|
||||
@@ -2,7 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCountRequestUnits(t *testing.T) {
|
||||
@@ -84,3 +86,23 @@ func TestProviderEnvironment(t *testing.T) {
|
||||
t.Fatal("expected invalid daily limit to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadBoundedBody(t *testing.T) {
|
||||
body, err := readBoundedBody(strings.NewReader("1234"), 4)
|
||||
if err != nil || string(body) != "1234" {
|
||||
t.Fatalf("unexpected bounded body: %q, %v", body, err)
|
||||
}
|
||||
if _, err := readBoundedBody(strings.NewReader("12345"), 4); err != errProviderResponseTooLarge {
|
||||
t.Fatalf("expected response size error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestBelongsToQuotaDay(t *testing.T) {
|
||||
startedAt := time.Date(2026, 8, 12, 1, 30, 0, 0, time.FixedZone("CEST", 2*60*60))
|
||||
if !requestBelongsToQuotaDay("2026-08-11", startedAt) {
|
||||
t.Fatal("expected request to match its UTC start day")
|
||||
}
|
||||
if requestBelongsToQuotaDay("2026-08-12", startedAt) {
|
||||
t.Fatal("request usage must not be added after the quota record resets")
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,24 @@ for client_id in workshop-test-client workshop-rotated-client; do
|
||||
node apps/workshop/tests/oauth-sync.integration.mjs
|
||||
done
|
||||
|
||||
OPENROUTER_API_KEY= PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml up -d --force-recreate workshop
|
||||
for attempt in $(seq 1 60); do
|
||||
if curl --fail --silent "http://127.0.0.1:$WORKSHOP_TEST_PORT/api/health" >/dev/null; then
|
||||
break
|
||||
fi
|
||||
if [ "$attempt" -eq 60 ]; then
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
models_status=$(curl --silent --output /dev/null --write-out '%{http_code}' \
|
||||
"http://127.0.0.1:$WORKSHOP_TEST_PORT/api/ai/v1/models")
|
||||
if [ "$models_status" != "503" ]; then
|
||||
echo "Expected an unconfigured Homarr provider to return 503, received $models_status"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml stop workshop
|
||||
printf 'y\n' | PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
|
||||
Reference in new issue
Block a user