Develop plugins with WebIDE

Updated at:

AI Gateway extends functionality through plugins. WebIDE provides an integrated environment to develop and compile custom plugins.

Background information

To simplify plugin development and environment setup, AI Gateway provides a WebIDE runtime where you can develop and compile custom plugins from a single interface.

WebIDE integrates with Apsara DevOps and stores your code in your own Git repository, providing persistent storage, version control, a standardized pipeline environment, and automated builds.

Note
  • When you use WebIDE to write plugins for the first time, if the organization has not completed RAM role authorization, the system redirects you to the authorization intermediary page. The organization administrator must complete role authorization in the RAM console. After authorization, organization members can use WebIDE to write plugins. For more information about the authorization intermediary page, see WebIDE Authorization Intermediary Page.

Procedure

1. Write a gateway plugin

  1. Log on to the AI Gateway console.

  2. In the left-side navigation pane, click Plug-in. In the top navigation bar, select a region.

  3. On the Plugins page, click Write Plugin.

    The system checks whether you are a member of the organization that owns the plugin repository:

    • If you are already in the organization: You are redirected to the plugin editing page. Click Create to proceed.

    • If you are not in the organization: A dialog is displayed indicating that you are not a member of the organization that owns the plugin repository and cannot write plugins directly. Click Copy Authorization Link to copy the link to the clipboard, and then send it to the organization administrator to complete authorization.

  4. On the New plugin page, enter a plugin identifier and name, and then click Start Writing to open WebIDE and begin developing your gateway plugin.

2. Build and publish

After you write your plugin, build and publish it before installing it on a gateway.

  1. In the upper-right corner of the WebIDE console, click Build, and then click Confirm.

    Note
    • If you try to build with uncommitted files, you must choose to either commit and build, or build directly. A direct build does not include the uncommitted changes.

    • You can click Build Log in the upper-right corner to view the build status in real time.

  2. After the build is complete, click Publish Version in the upper-right corner.

  3. On the Add Version page, configure the parameters and click Publish. The following table describes the parameters.

    Parameter

    Description

    Example

    Version number

    The version number, in X.Y.Z format.

    1.0.0

    Version description

    A description of the changes in this version.

    Modified the response format.

    Execution phase

    The execution order of the phases: authentication phase > authorization phase > statistics phase > default phase. If your plugin does not depend on the execution order of other plugins, you can select the default phase.

    default phase

    Execution priority

    The execution priority within a phase. A higher number indicates higher priority.

    100

    Compatible gateway version

    • Any version: The plugin can be installed on any gateway version.

    • Specific version: The plugin can be installed only on the specified gateway version or later.

    Any version

Basic plugin code structure

Plugin development has two main parts: parsing the plugin configuration and implementing handler functions. These functions process requests and responses at different stages, such as handling headers and body content.

func init() {
	wrapper.SetCtx(
		// The name of the plugin. You can customize this.
		"my-plugin",

		// Set a custom function to parse the plugin configuration. This method is suitable when
		// the global, route, domain, and service-level configurations share the same schema.
		wrapper.ParseConfigBy(parseConfig),

		// Set a custom function to process request headers when the gateway receives them from the client.
		wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),

		// Set a custom function to process response headers when the gateway receives them from the backend service.
		wrapper.ProcessResponseHeadersBy(onHttpResponseHeaders),

		// Set a custom function to process the request body when the gateway receives it from the client.
		wrapper.ProcessRequestBodyBy(onHttpRequestBody),

		// Set a custom function to process the response body when the gateway receives it from the backend service.
		wrapper.ProcessResponseBodyBy(onHttpResponseBody),

		// Set a custom function to process the streaming request body.
		wrapper.ProcessStreamingRequestBodyBy(onHttpStreamingRequestBody),

		// Set a custom function to process the streaming response body.
		wrapper.ProcessStreamingResponseBodyBy(onHttpStreamingResponseBody),

		// Set a custom function to handle the completion of a streaming request.
		//wrapper.ProcessStreamDoneBy(onHttpStreamDone),
	)
}

// MyConfig defines custom plugin configuration.
// If the plugin requires no configuration, define an empty struct: type MyConfig struct {}
type MyConfig struct {
	// Whether to enable the mock feature.
	mockEnable bool
}

// The YAML configuration you enter in the console is automatically converted to JSON.
// You can parse the configuration directly from this JSON parameter.
func parseConfig(json gjson.Result, config *MyConfig, log logs.Log) error {
	// Parse the configuration and update it in the config object.
	config.mockEnable = json.Get("mockEnable").Bool()
	return nil
}

// Request header handler.
func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log logs.Log) types.Action {
	proxywasm.AddHttpRequestHeader("hello", "world")
	if config.mockEnable {
		proxywasm.SendHttpResponse(200, nil, []byte("hello world"), -1)
	}
	return types.ActionContinue
}

// Request body handler.
func onHttpRequestBody(ctx wrapper.HttpContext, config MyConfig, body []byte, log logs.Log) types.Action {
	return types.ActionContinue
}

// Response body handler.
func onHttpResponseBody(ctx wrapper.HttpContext, config MyConfig, body []byte, log logs.Log) types.Action {
	return types.ActionContinue
}

// Response header handler.
func onHttpResponseHeaders(ctx wrapper.HttpContext, config MyConfig, log logs.Log) types.Action {
	return types.ActionContinue
}

Generate plugin code with a large model

WebIDE can generate plugin code from natural language descriptions.

  1. In the upper-right corner of the WebIDE console, click Generate with AI Assistant.

  2. In the editor, provide a detailed description of your requirements.

  3. Click Start Generation and wait for the code generation to complete.

Related documentation

  • For more information about plugin development, see Develop plugins.

  • For more information about installing, enabling, and uninstalling plugins, see Manage plugins.