From 6fc4789f759fa8a3e3384a4e5c77f7ff78d2c997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=8D+85CD?= <50108258+kwaa@users.noreply.github.com> Date: Tue, 31 Dec 2024 17:14:34 +0800 Subject: [PATCH 1/4] feat(backend): extra_body option --- pkg/backend/backend.go | 3 +++ pkg/backend/elevenlabs.go | 19 ++++++++++++++----- pkg/backend/koemotion.go | 18 ++++++++++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) 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()) From 5fe8586b17eb873dddd96b16ae9040fda90a617e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=8D+85CD?= <50108258+kwaa@users.noreply.github.com> Date: Tue, 31 Dec 2024 17:18:07 +0800 Subject: [PATCH 2/4] chore: use `any` instead of `interface{}` Co-authored-by: Neko --- pkg/backend/backend.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/backend/backend.go b/pkg/backend/backend.go index dbeb7d2..4646412 100644 --- a/pkg/backend/backend.go +++ b/pkg/backend/backend.go @@ -33,7 +33,7 @@ type OpenAISpeechRequestOptions struct { Speed int `json:"speed,omitempty"` // Extension: allows you to add custom content to body. - ExtraBody map[string]interface{} `json:"extra_body,omitempty"` + ExtraBody map[string]any `json:"extra_body,omitempty"` } type SpeechRequestOptions struct { From 34829ee107f5039c16c6a0cef60d74f322f1509c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=8D+85CD?= <50108258+kwaa@users.noreply.github.com> Date: Tue, 31 Dec 2024 17:18:28 +0800 Subject: [PATCH 3/4] chore: use `any` instead of `interface{}` Co-authored-by: Neko --- pkg/backend/elevenlabs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/backend/elevenlabs.go b/pkg/backend/elevenlabs.go index 911a98b..905a19c 100644 --- a/pkg/backend/elevenlabs.go +++ b/pkg/backend/elevenlabs.go @@ -33,7 +33,7 @@ func elevenlabs(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resu }, lo.Map( lo.Entries(options.MustGet().ExtraBody), - func(item lo.Entry[string, interface{}], index int) mo.Option[jsonpatch.JSONPatchOperationObject] { + func(item lo.Entry[string, any], index int) mo.Option[jsonpatch.JSONPatchOperationObject] { return jsonpatch.NewAdd(strings.Join([]string{"/", item.Key}, ""), item.Value) })..., )..., From da13c1b34085e4c78a5c0c5bdeecaaadd14d030d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=8D+85CD?= <50108258+kwaa@users.noreply.github.com> Date: Tue, 31 Dec 2024 17:18:39 +0800 Subject: [PATCH 4/4] chore: use `any` instead of `interface{}` Co-authored-by: Neko --- pkg/backend/koemotion.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/backend/koemotion.go b/pkg/backend/koemotion.go index 3344c9c..2c3ebc7 100644 --- a/pkg/backend/koemotion.go +++ b/pkg/backend/koemotion.go @@ -30,7 +30,7 @@ func koemotion(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resul }, lo.Map( lo.Entries(options.MustGet().ExtraBody), - func(item lo.Entry[string, interface{}], index int) mo.Option[jsonpatch.JSONPatchOperationObject] { + func(item lo.Entry[string, any], index int) mo.Option[jsonpatch.JSONPatchOperationObject] { return jsonpatch.NewAdd(strings.Join([]string{"/", item.Key}, ""), item.Value) })..., )...,