History

feat(pluginhost): introduce browser-navigable plugin resources in Management API

- Added `resources` field in `management.register` for defining browser-accessible resources. - Updated examples and documentation to reflect resource-based paths under `/v0/resource/plugins/<pluginID>/...`. - Replaced legacy `GET` menu routes with resource-based implementations for consistent plugin behavior. - Enhanced request handling for resource paths, including proper response headers and streamlined test coverage.

Luis Pater committed 5 months agov7.1.59

Commit 44ea9ab1 parent 2aeb41c22 files · +341 −69Browse files

Showing the changes to examples/plugin/simple (5 of 22 files). Show all

Unified · Split

5 files
  1. examples/plugin/simple/README.md +3 −1
  2. examples/plugin/simple/README_CN.md +3 −1
  3. examples/plugin/simple/c/src/plugin.c +3 −3
  4. examples/plugin/simple/go/main.go +11 −6
  5. examples/plugin/simple/rust/src/lib.rs +2 −2
examples/plugin/simple/README.md+3−1
@@ -187,7 +187,9 @@ PUT /v0/management/plugins/{pluginID}/config · 186 unmodified lines
187187 PATCH /v0/management/plugins/{pluginID}/config
188188 ```
189189
190−Plugin-owned Management API routes are registered through `management.register` and handled through `management.handle`.
190+Plugin-owned Management API routes are registered through the `routes` field of `management.register` and handled through `management.handle`.
191+
192+Browser-navigable menu resources are registered through the `resources` field of `management.register`. CPA exposes those resources under `/v0/resource/plugins/<pluginID>/...`; for example, a plugin with ID `example` and resource path `/status` is served as `/v0/resource/plugins/example/status`.
191193
192194 ## Trust Boundary
193195
examples/plugin/simple/README_CN.md+3−1
@@ -185,7 +185,9 @@ PUT /v0/management/plugins/{pluginID}/config · 184 unmodified lines
185185 PATCH /v0/management/plugins/{pluginID}/config
186186 ```
187187
188−插件自有 Management API 路由通过 `management.register` 注册,通过 `management.handle` 处理。
188+插件自有 Management API 路由通过 `management.register` 的 `routes` 字段注册,并通过 `management.handle` 处理。
189+
190+可由浏览器直接访问的菜单资源通过 `management.register` 的 `resources` 字段注册。CPA 会将这些资源暴露在 `/v0/resource/plugins/<pluginID>/...` 下;例如插件 ID 为 `example` 且资源路径为 `/status` 时,最终路径是 `/v0/resource/plugins/example/status`。
189191
190192 ## 信任边界
191193
examples/plugin/simple/c/src/plugin.c+3−3
@@ -84,8 +84,8 @@ static const char* CLI_REGISTER_RESPONSE = · 83 unmodified lines
8484 static const char* CLI_EXECUTE_RESPONSE =
8585 "{\"ok\":true,\"result\":{\"Stdout\":\"cGx1Z2luIGV4YW1wbGUgYyBjb21tYW5kCg==\",\"ExitCode\":0}}";
8686 static const char* MANAGEMENT_REGISTER_RESPONSE =
87− "{\"ok\":true,\"result\":{\"Routes\":[{\"Method\":\"GET\",\"Path\":\"/plugins/example-c/status\","
88− "\"Menu\":\"Example C Plugin\",\"Description\":\"Shows example C plugin status.\"}]}}";
87+ "{\"ok\":true,\"result\":{\"Resources\":[{\"Path\":\"/status\","
88+ "\"Menu\":\"Example C Plugin\",\"Description\":\"CPA exposes this menu resource under /v0/resource/plugins/example-c/status.\"}]}}";
8989 static const char* UNKNOWN_METHOD_RESPONSE =
9090 "{\"ok\":false,\"error\":{\"code\":\"unknown_method\",\"message\":\"unknown method\"}}";
9191 static const char* INVALID_METHOD_RESPONSE =
@@ -421,7 +421,7 @@ static char* make_http_response(const uint8_t* request, size_t request_len) { · 329 unmodified lines
421421 char* url = extract_json_string(json, "URL");
422422 char* path = extract_json_string(json, "Path");
423423 char* method_escaped = json_escape(method == NULL ? "GET" : method);
424− char* target_escaped = json_escape(url != NULL ? url : (path == NULL ? "/plugins/example-c/status" : path));
424+ char* target_escaped = json_escape(url != NULL ? url : (path == NULL ? "/v0/resource/plugins/example-c/status" : path));
425425 char* body_json = format_string(
426426 "{\"plugin\":\"example-c\",\"method\":\"%s\",\"target\":\"%s\"}",
427427 method_escaped == NULL ? "" : method_escaped,
examples/plugin/simple/go/main.go+11−6
@@ -100,7 +100,8 @@ type streamResponse struct { · 99 unmodified lines
100100 }
101101
102102 type managementRegistrationResponse struct {
103− Routes []pluginapi.ManagementRoute `json:"routes,omitempty"`
103+ Routes []pluginapi.ManagementRoute `json:"routes,omitempty"`
104+ Resources []pluginapi.ResourceRoute `json:"resources,omitempty"`
104105 }
105106
106107 func main() {}
@@ -207,14 +208,18 @@ func handleMethod(method string, request []byte) ([]byte, error) { · 100 unmodified lines
207208 case pluginabi.MethodCommandLineExecute:
208209 return okEnvelope(pluginapi.CommandLineExecutionResponse{Stdout: []byte("plugin example command\n")})
209210 case pluginabi.MethodManagementRegister:
210− return okEnvelope(managementRegistrationResponse{Routes: []pluginapi.ManagementRoute{{
211− Method: http.MethodGet,
212− Path: "/plugins/example/status",
211+ // CPA exposes menu resources under /v0/resource/plugins/<plugin-id>/.
212+ return okEnvelope(managementRegistrationResponse{Resources: []pluginapi.ResourceRoute{{
213+ Path: "/status",
213214 Menu: "Example Plugin",
214− Description: "Shows example plugin status.",
215+ Description: "Shows example plugin status as a browser-navigable resource.",
215216 }}})
216217 case pluginabi.MethodManagementHandle:
217− return okEnvelope(pluginapi.ManagementResponse{StatusCode: http.StatusOK, Body: []byte(`{"plugin":"example"}`)})
218+ return okEnvelope(pluginapi.ManagementResponse{
219+ StatusCode: http.StatusOK,
220+ Headers: http.Header{"Content-Type": []string{"text/html; charset=utf-8"}},
221+ Body: []byte(`<!doctype html><title>Example Plugin</title><main>Example Plugin</main>`),
222+ })
218223 default:
219224 return errorEnvelope("unknown_method", "unknown method: "+method), nil
220225 }
examples/plugin/simple/rust/src/lib.rs+2−2
@@ -18,7 +18,7 @@ const FRONTEND_AUTH_RESPONSE: &str = r#"{"ok":true,"result":{"Authenticated":tru · 17 unmodified lines
1818 const STREAM_RESPONSE: &str = r#"{"ok":true,"result":{"headers":{"content-type":["text/event-stream"]},"chunks":[{"Payload":"cGx1Z2luLWV4YW1wbGUtcnVzdAo="}]}}"#;
1919 const CLI_REGISTER_RESPONSE: &str = r#"{"ok":true,"result":{"Flags":[{"Name":"plugin-example-rust-command","Usage":"Run the example Rust ABI plugin command","Type":"bool"}]}}"#;
2020 const CLI_EXECUTE_RESPONSE: &str = r#"{"ok":true,"result":{"Stdout":"cGx1Z2luIGV4YW1wbGUgcnVzdCBjb21tYW5kCg==","ExitCode":0}}"#;
21−const MANAGEMENT_REGISTER_RESPONSE: &str = r#"{"ok":true,"result":{"Routes":[{"Method":"GET","Path":"/plugins/example-rust/status","Menu":"Example Rust Plugin","Description":"Shows example Rust plugin status."}]}}"#;
21+const MANAGEMENT_REGISTER_RESPONSE: &str = r#"{"ok":true,"result":{"Resources":[{"Path":"/status","Menu":"Example Rust Plugin","Description":"CPA exposes this menu resource under /v0/resource/plugins/example-rust/status."}]}}"#;
2222 const UNKNOWN_METHOD_RESPONSE: &str = r#"{"ok":false,"error":{"code":"unknown_method","message":"unknown method"}}"#;
2323 const INVALID_METHOD_RESPONSE: &str = r#"{"ok":false,"error":{"code":"invalid_method","message":"method is required"}}"#;
2424
@@ -170,7 +170,7 @@ fn make_http_response(request: &[u8]) -> String { · 145 unmodified lines
170170 let method = extract_json_string(&json, "Method").unwrap_or_else(|| "GET".to_string());
171171 let target = extract_json_string(&json, "URL")
172172 .or_else(|| extract_json_string(&json, "Path"))
173− .unwrap_or_else(|| "/plugins/example-rust/status".to_string());
173+ .unwrap_or_else(|| "/v0/resource/plugins/example-rust/status".to_string());
174174 let body = format!(
175175 r#"{{"plugin":"example-rust","method":"{}","target":"{}"}}"#,
176176 json_escape(&method),