diff --git a/pkg/backend/backend.go b/pkg/backend/backend.go index 08ff456..dbeb7d2 100644 --- a/pkg/backend/backend.go +++ b/pkg/backend/backend.go @@ -31,6 +31,9 @@ type OpenAISpeechRequestOptions struct { // Select a value from 0.25 to 4.0. // 1.0 is the default. Speed int `json:"speed,omitempty"` + + // Extension: allows you to add custom content to body. + ExtraBody map[string]interface{} `json:"extra_body,omitempty"` } type SpeechRequestOptions struct { diff --git a/pkg/backend/elevenlabs.go b/pkg/backend/elevenlabs.go index 70fcf04..911a98b 100644 --- a/pkg/backend/elevenlabs.go +++ b/pkg/backend/elevenlabs.go @@ -23,11 +23,20 @@ func elevenlabs(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resu patchedPayload := jsonpatch.ApplyPatches( options.MustGet().body.OrElse(new(bytes.Buffer)).Bytes(), mo.Some(jsonpatch.ApplyOptions{AllowMissingPathOnRemove: true}), - jsonpatch.NewRemove("/model"), - jsonpatch.NewRemove("/voice"), - jsonpatch.NewRemove("/input"), - jsonpatch.NewAdd("/text", options.MustGet().Input), - jsonpatch.NewAdd("/model_id", options.MustGet().Model), + append( + []mo.Option[jsonpatch.JSONPatchOperationObject]{ + jsonpatch.NewRemove("/model"), + jsonpatch.NewRemove("/voice"), + jsonpatch.NewRemove("/input"), + jsonpatch.NewAdd("/text", options.MustGet().Input), + jsonpatch.NewAdd("/model_id", options.MustGet().Model), + }, + lo.Map( + lo.Entries(options.MustGet().ExtraBody), + func(item lo.Entry[string, interface{}], index int) mo.Option[jsonpatch.JSONPatchOperationObject] { + return jsonpatch.NewAdd(strings.Join([]string{"/", item.Key}, ""), item.Value) + })..., + )..., ) if patchedPayload.IsError() { return mo.Err[any](apierrors.NewErrInternal().WithDetail(patchedPayload.Error().Error()).WithCaller()) diff --git a/pkg/backend/koemotion.go b/pkg/backend/koemotion.go index b319758..3344c9c 100644 --- a/pkg/backend/koemotion.go +++ b/pkg/backend/koemotion.go @@ -11,6 +11,7 @@ import ( "github.com/moeru-ai/unspeech/pkg/apierrors" "github.com/moeru-ai/unspeech/pkg/utils" "github.com/moeru-ai/unspeech/pkg/utils/jsonpatch" + "github.com/samber/lo" "github.com/samber/mo" "github.com/vincent-petithory/dataurl" ) @@ -20,10 +21,19 @@ func koemotion(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resul patchedPayload := jsonpatch.ApplyPatches( options.MustGet().body.OrElse(new(bytes.Buffer)).Bytes(), mo.Some(jsonpatch.ApplyOptions{AllowMissingPathOnRemove: true}), - jsonpatch.NewRemove("/model"), - jsonpatch.NewRemove("/voice"), - jsonpatch.NewRemove("/input"), - jsonpatch.NewAdd("/text", options.MustGet().Input), + append( + []mo.Option[jsonpatch.JSONPatchOperationObject]{ + jsonpatch.NewRemove("/model"), + jsonpatch.NewRemove("/voice"), + jsonpatch.NewRemove("/input"), + jsonpatch.NewAdd("/text", options.MustGet().Input), + }, + lo.Map( + lo.Entries(options.MustGet().ExtraBody), + func(item lo.Entry[string, interface{}], index int) mo.Option[jsonpatch.JSONPatchOperationObject] { + return jsonpatch.NewAdd(strings.Join([]string{"/", item.Key}, ""), item.Value) + })..., + )..., ) if patchedPayload.IsError() { return mo.Err[any](apierrors.NewErrInternal().WithDetail(patchedPayload.Error().Error()).WithCaller())