-
-
Notifications
You must be signed in to change notification settings - Fork 80.9k
Expand file tree
/
Copy pathindex.ts
More file actions
147 lines (144 loc) · 4.98 KB
/
Copy pathindex.ts
File metadata and controls
147 lines (144 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Canvas plugin entrypoint for node canvas control, hosted A2UI routes, and
* node CLI registration.
*/
import type { IncomingMessage, ServerResponse } from "node:http";
import type { Duplex } from "node:stream";
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
import { definePluginEntry, type AnyAgentTool } from "openclaw/plugin-sdk/plugin-entry";
import { canvasConfigSchema, isCanvasHostEnabled } from "./src/config.js";
import { A2UI_PATH, CANVAS_HOST_PATH, CANVAS_WS_PATH } from "./src/host/a2ui-shared.js";
import { CanvasToolSchema } from "./src/tool-schema.js";
const CANVAS_NODE_COMMANDS = [
"canvas.present",
"canvas.hide",
"canvas.navigate",
"canvas.eval",
"canvas.snapshot",
"canvas.a2ui.push",
"canvas.a2ui.pushJSONL",
"canvas.a2ui.reset",
];
function createLazyCanvasTool(params: {
config?: OpenClawConfig;
workspaceDir?: string;
}): AnyAgentTool {
const loadTool = createLazyRuntimeModule(() =>
import("./src/tool.js").then(({ createCanvasTool }) =>
createCanvasTool({
config: params.config,
workspaceDir: params.workspaceDir,
}),
),
);
return {
label: "Canvas",
name: "canvas",
description:
"Control node canvases (present/hide/navigate/eval/snapshot/A2UI). Use snapshot to capture the rendered UI.",
parameters: CanvasToolSchema,
execute: async (...args: Parameters<AnyAgentTool["execute"]>) =>
await (await loadTool()).execute(...args),
};
}
export default definePluginEntry({
id: "canvas",
name: "Canvas",
description: "Experimental Canvas control and A2UI rendering surfaces for paired nodes.",
configSchema: canvasConfigSchema,
reload: {
restartPrefixes: ["plugins.enabled", "plugins.allow", "plugins.deny", "plugins.entries.canvas"],
},
register(api) {
if (isCanvasHostEnabled(api.config)) {
const httpRouteHandlerLoader = createLazyRuntimeModule(() =>
import("./src/http-route.js").then(({ createCanvasHttpRouteHandler }) =>
createCanvasHttpRouteHandler({
config: api.config,
pluginConfig: api.pluginConfig,
runtime: {
log: (...args) => api.logger.info(args.map(String).join(" ")),
error: (...args) => api.logger.error(args.map(String).join(" ")),
exit: (code) => {
throw new Error(`canvas host requested process exit ${code}`);
},
},
}),
),
);
const loadHttpRouteHandler = httpRouteHandlerLoader;
const handleHttpRequest = async (req: IncomingMessage, res: ServerResponse) =>
await (await loadHttpRouteHandler()).handleHttpRequest(req, res);
const handleUpgrade = async (req: IncomingMessage, socket: Duplex, head: Buffer) =>
await (await loadHttpRouteHandler()).handleUpgrade(req, socket, head);
const nodeCapability = { surface: "canvas" };
api.registerHttpRoute({
path: A2UI_PATH,
auth: "plugin",
match: "prefix",
nodeCapability,
handler: handleHttpRequest,
});
api.registerHttpRoute({
path: CANVAS_HOST_PATH,
auth: "plugin",
match: "prefix",
nodeCapability,
handler: handleHttpRequest,
});
api.registerHttpRoute({
path: CANVAS_WS_PATH,
auth: "plugin",
match: "exact",
nodeCapability,
handler: handleHttpRequest,
handleUpgrade,
});
api.registerService({
id: "canvas-host",
start: () => {},
stop: async () => {
const httpRouteHandler = await httpRouteHandlerLoader.peek();
await httpRouteHandler?.close();
},
});
const loadResolveCanvasHttpPathToLocalPath = createLazyRuntimeModule(() =>
import("./src/documents.js").then(
({ resolveCanvasHttpPathToLocalPath }) => resolveCanvasHttpPathToLocalPath,
),
);
api.registerHostedMediaResolver(async (mediaUrl) => {
return (await loadResolveCanvasHttpPathToLocalPath())(mediaUrl);
});
}
api.registerNodeInvokePolicy({
commands: CANVAS_NODE_COMMANDS,
defaultPlatforms: ["ios", "android", "macos", "windows", "unknown"],
foregroundRestrictedOnIos: true,
handle: (ctx) => ctx.invokeNode(),
});
api.registerTool((ctx) =>
createLazyCanvasTool({
config: ctx.runtimeConfig ?? ctx.config,
workspaceDir: ctx.workspaceDir,
}),
);
api.registerNodeCliFeature(
async ({ program }) => {
const { createDefaultCanvasCliDependencies, registerNodesCanvasCommands } =
await import("./src/cli.js");
registerNodesCanvasCommands(program, createDefaultCanvasCliDependencies());
},
{
descriptors: [
{
name: "canvas",
description: "Capture or render canvas content from a paired node",
hasSubcommands: true,
},
],
},
);
},
});