service

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 10 Imported by: 0

README

service

Shared utility functions for relay providers, bridging provider adaptors to the relaykit conversion system and providing common helpers for response handling and usage estimation.

Key Functions

  • ConvertRequest -- converts a request to a target relay format (openai/claude/gemini) via relaykit.
  • ConvertRequestByID -- converts a request using a named converter.
  • ResponseText2Usage -- estimates dto.Usage from response text when the provider does not return token counts.
  • EstimateTokenByModel -- heuristic token estimation (~4 chars/token for English, ~2 chars/token for CJK).
  • ValidUsage -- checks whether a usage struct has non-zero values.
  • GetImageFromUrl -- downloads an image from a URL and returns its data and MIME type.
  • GetBase64Data -- returns base64 data from a URL or data URI.
  • ShouldCopyUpstreamHeader -- reports whether a header should be forwarded to the client (skips hop-by-hop headers).
  • IOCopyBytesGracefully -- writes bytes to a ResponseWriter with JSON content type.
  • CloseResponseBodyGracefully -- closes a response body, ignoring errors.
  • TaskErrorWrapper / TaskErrorWrapperLocal -- wrap errors into common.TaskError.

Usage

usage := service.ResponseText2Usage(responseText, "gpt-4o", 100)

Documentation

Overview

Package service provides shared utility functions for relay providers, adapted from LingRein's internal/service package with gin.Context dependencies removed.

This package bridges provider adaptors to the relaykit conversion system and provides common helpers for response handling and usage estimation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseResponseBodyGracefully

func CloseResponseBodyGracefully(resp *http.Response)

CloseResponseBodyGracefully closes a response body, ignoring errors.

func ConvertRequest

func ConvertRequest(ctx context.Context, info *common.RelayInfo, target types.RelayFormat, request any) (*relayconvert.RequestResult, error)

ConvertRequest converts a request to the target relay format using relaykit.

func ConvertRequestByID

func ConvertRequestByID(ctx context.Context, info *common.RelayInfo, converter string, request any) (*relayconvert.RequestResult, error)

ConvertRequestByID converts a request using a named converter.

func EstimateTokenByModel

func EstimateTokenByModel(modelName string, text string) int

EstimateTokenByModel estimates token count for a given model and text. Uses a simple heuristic: ~4 characters per token for English text.

func GetBase64Data

func GetBase64Data(source string, context string) (string, string, error)

GetBase64Data returns base64-encoded data from a source (URL or base64 string).

func GetImageFromUrl

func GetImageFromUrl(url string) ([]byte, string, error)

GetImageFromUrl downloads an image from a URL and returns its data, MIME type, and any error.

func IOCopyBytesGracefully

func IOCopyBytesGracefully(w http.ResponseWriter, resp *http.Response, data []byte)

IOCopyBytesGracefully writes data to the ResponseWriter.

func ResponseText2Usage

func ResponseText2Usage(responseText string, modelName string, promptTokens int) *dto.Usage

ResponseText2Usage estimates usage from response text when the provider doesn't return token counts. Uses a simple word-based estimation.

func ShouldCopyUpstreamHeader

func ShouldCopyUpstreamHeader(key string, values []string) bool

ShouldCopyUpstreamHeader reports whether a header should be copied from the upstream response to the client response.

func TaskErrorWrapper

func TaskErrorWrapper(err error, code string, statusCode int) *common.TaskError

TaskErrorWrapper wraps an error into a TaskError with code and status code.

func TaskErrorWrapperLocal

func TaskErrorWrapperLocal(err error, code string, statusCode int) *common.TaskError

TaskErrorWrapperLocal wraps a local error into a TaskError.

func ValidUsage

func ValidUsage(usage *dto.Usage) bool

ValidUsage checks if a usage struct has non-zero values.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL