Files
promptkit/llm_adapter.go

43 lines
1.3 KiB
Go

package promptkit
import (
"context"
"errors"
"fmt"
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
"gitea.maximumdirect.net/eric/promptkit/internal/llm"
)
type publicLLMClientAdapter struct {
client LLMClient
}
func (a publicLLMClientAdapter) Generate(ctx context.Context, req domain.GenerateRequest) (*domain.GenerateResponse, error) {
resp, err := a.client.Generate(ctx, fromDomainGenerateRequest(req))
if err == nil && resp == nil {
err = fmt.Errorf("%w: %w: llm client returned nil response", ErrLLMGenerate, ErrMalformedResponse)
}
if err != nil {
switch {
case errors.Is(err, ErrTransport):
return nil, classifiedClientError{cause: err, category: llm.ErrTransport}
case errors.Is(err, ErrMalformedResponse):
return nil, classifiedClientError{cause: err, category: llm.ErrMalformedResponse}
}
return nil, err
}
return toDomainGenerateResponse(resp), nil
}
// classifiedClientError makes an explicit public category available internally
// without replacing the client's diagnostic or original error chain.
type classifiedClientError struct {
cause error
category error
}
func (e classifiedClientError) Error() string { return e.cause.Error() }
func (e classifiedClientError) Unwrap() error { return e.cause }
func (e classifiedClientError) Is(target error) bool { return target == e.category }