mirror of
https://github.com/alibaba/higress.git
synced 2026-05-26 21:57:30 +08:00
Migrate WASM Go Plugins to New SDK and Go 1.24 (#2532)
This commit is contained in:
@@ -23,10 +23,11 @@ import (
|
||||
|
||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||
"github.com/higress-group/wasm-go/pkg/log"
|
||||
"github.com/santhosh-tekuri/jsonschema"
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"github.com/higress-group/wasm-go/pkg/wrapper"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -110,7 +111,9 @@ type PluginConfig struct {
|
||||
enableJsonSchemaValidation bool
|
||||
}
|
||||
|
||||
func main() {
|
||||
func main() {}
|
||||
|
||||
func init() {
|
||||
wrapper.SetCtx(
|
||||
"ai-json-resp",
|
||||
wrapper.ParseConfigBy(parseConfig),
|
||||
@@ -141,7 +144,7 @@ func parseUrl(url string) (string, string) {
|
||||
return url[:index], url[index:]
|
||||
}
|
||||
|
||||
func parseConfig(result gjson.Result, config *PluginConfig, log wrapper.Log) error {
|
||||
func parseConfig(result gjson.Result, config *PluginConfig, log log.Log) error {
|
||||
config.serviceName = result.Get("serviceName").String()
|
||||
config.serviceUrl = result.Get("serviceUrl").String()
|
||||
config.serviceDomain = result.Get("serviceDomain").String()
|
||||
@@ -278,7 +281,7 @@ func (r *RequestContext) assembleReqBody(config PluginConfig) []byte {
|
||||
return reqBody
|
||||
}
|
||||
|
||||
func (r *RequestContext) SaveBodyToHistMsg(log wrapper.Log, reqBody []byte, respBody []byte) {
|
||||
func (r *RequestContext) SaveBodyToHistMsg(log log.Log, reqBody []byte, respBody []byte) {
|
||||
r.RespBody = respBody
|
||||
lastUserMessage := ""
|
||||
lastSystemMessage := ""
|
||||
@@ -318,7 +321,7 @@ func (r *RequestContext) SaveBodyToHistMsg(log wrapper.Log, reqBody []byte, resp
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RequestContext) SaveStrToHistMsg(log wrapper.Log, errMsg string) {
|
||||
func (r *RequestContext) SaveStrToHistMsg(log log.Log, errMsg string) {
|
||||
r.HistoryMessages = append(r.HistoryMessages, chatMessage{
|
||||
Role: "system",
|
||||
Content: errMsg,
|
||||
@@ -340,7 +343,7 @@ func (c *PluginConfig) ValidateBody(body []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *PluginConfig) ValidateJson(body []byte, log wrapper.Log) (string, error) {
|
||||
func (c *PluginConfig) ValidateJson(body []byte, log log.Log) (string, error) {
|
||||
content := gjson.ParseBytes(body).Get(c.contentPath).String()
|
||||
// first extract json from response body
|
||||
if content == "" {
|
||||
@@ -399,7 +402,7 @@ func (c *PluginConfig) ExtractJson(bodyStr string) (string, error) {
|
||||
return jsonStr, nil
|
||||
}
|
||||
|
||||
func sendResponse(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log, body []byte) {
|
||||
func sendResponse(ctx wrapper.HttpContext, config PluginConfig, log log.Log, body []byte) {
|
||||
log.Infof("Final send: Code %d, Message %s, Body: %s", config.rejectStruct.RejectCode, config.rejectStruct.RejectMsg, string(body))
|
||||
header := [][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
@@ -414,7 +417,7 @@ func sendResponse(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log,
|
||||
}
|
||||
}
|
||||
|
||||
func recursiveRefineJson(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log, retryCount int, requestContext *RequestContext) {
|
||||
func recursiveRefineJson(ctx wrapper.HttpContext, config PluginConfig, log log.Log, retryCount int, requestContext *RequestContext) {
|
||||
// if retry count exceeds max retry count, return the response
|
||||
if retryCount >= config.maxRetry {
|
||||
log.Debugf("retry count exceeds max retry count")
|
||||
@@ -445,7 +448,7 @@ func recursiveRefineJson(ctx wrapper.HttpContext, config PluginConfig, log wrapp
|
||||
}, uint32(config.serviceTimeout))
|
||||
}
|
||||
|
||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
||||
if config.rejectStruct.RejectCode != HTTP_STATUS_OK {
|
||||
sendResponse(ctx, config, log, nil)
|
||||
return types.ActionPause
|
||||
@@ -505,7 +508,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrap
|
||||
return types.ActionContinue
|
||||
}
|
||||
|
||||
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
||||
// if the request is from this plugin, continue the request
|
||||
fromThisPlugin, ok := ctx.GetContext(FROM_THIS_PLUGIN_KEY).(bool)
|
||||
if ok && fromThisPlugin {
|
||||
|
||||
Reference in New Issue
Block a user