dsh-cohub 0.4.5 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ // src/co-cordis-plugin.ts
2
+ import { defineTool } from "@deepseek-ai/dsh-tools";
3
+ var name = "co-cordis-plugin";
4
+ var inject = ["tools", "cordisInspect", "dynamicCordisRunner"];
5
+ function requireAgent(exec) {
6
+ if (exec.agent === undefined)
7
+ throw new Error("co-cordis 工具需要 Agent 支持的会话");
8
+ return exec.agent;
9
+ }
10
+ function apply(ctx) {
11
+ ctx.tools.register(defineTool({
12
+ name: "co_inspect_list",
13
+ description: "列出 Host 端所有已知的 Cordis Inspect Provider,包含它们的平台、目的、方法名与输入/输出 schema。调用此工具后再用 co_inspect_query 查具体定义。",
14
+ parameters: {},
15
+ output: { schema: { type: "json" }, render: (_a, v) => [{ type: "text", text: JSON.stringify(v, null, 2) }] },
16
+ execute() {
17
+ return Promise.resolve({ providers: ctx.cordisInspect.list() });
18
+ }
19
+ }));
20
+ ctx.tools.register(defineTool({
21
+ name: "co_inspect_query",
22
+ description: "按 platform + provider + method 查一个 Inspect Provider 的具体结构化定义。platform 和 provider 必须来自 co_inspect_list 的返回结果。输入格式必须满足该 method 的 inputSchema。Host 查询在本地执行。此工具只读,不能调用业务 Service 方法或修改运行时。",
23
+ parameters: {
24
+ platform: { type: "string", required: true, enum: ["host", "client"], description: "Provider 所属运行时平台" },
25
+ provider: { type: "string", required: true, description: "Provider ID(来自 co_inspect_list)" },
26
+ method: { type: "string", required: true, description: "方法名(来自 co_inspect_list)" },
27
+ input: { type: "json", description: "查询输入(可选);必须满足该方法的 inputSchema" }
28
+ },
29
+ output: { schema: { type: "json" }, render: (_a, v) => [{ type: "text", text: JSON.stringify(v, null, 2) }] },
30
+ async execute(args, exec) {
31
+ const agent = requireAgent(exec);
32
+ const data = await ctx.cordisInspect.query(args.platform, args.provider, args.method, args.input, agent, exec.signal);
33
+ return { platform: args.platform, provider: args.provider, method: args.method, data };
34
+ }
35
+ }));
36
+ ctx.tools.register(defineTool({
37
+ name: "co_inspect_self",
38
+ description: "查当前会话通过 co_define / co_run 管理的动态 Cordis 插件清单。无参数时列出所有插件摘要;传入 pluginId 时返回该插件的简要状态与包列表;同时传入 pluginId + packageId 时返回该不可变包的具体源码与运行时诊断。",
39
+ parameters: {
40
+ pluginId: { type: "string", description: "插件 ID(省略时列出全部);必须与 packageId 同时传入才能查具体包" },
41
+ packageId: { type: "string", description: "包的不可变 ID;需要 pluginId 一起传入" }
42
+ },
43
+ output: { schema: { type: "json" }, render: (_a, v) => [{ type: "text", text: JSON.stringify(v, null, 2) }] },
44
+ execute(args, exec) {
45
+ const agent = requireAgent(exec);
46
+ if (args.packageId !== undefined && args.pluginId === undefined)
47
+ throw new Error("co_inspect_self: packageId 需要 pluginId 一起传入");
48
+ if (args.pluginId === undefined) {
49
+ return Promise.resolve({
50
+ mode: "plugins",
51
+ plugins: ctx.dynamicCordisRunner.listPlugins(agent).map((ref) => ({
52
+ pluginId: String(ref.pluginId),
53
+ name: ref.name,
54
+ purpose: ref.purpose,
55
+ status: ref.status,
56
+ currentPackageId: String(ref.currentPackageId),
57
+ nextPackageId: ref.nextPackageId !== undefined ? String(ref.nextPackageId) : undefined
58
+ }))
59
+ });
60
+ }
61
+ const plugin = ctx.dynamicCordisRunner.inspectPlugin(agent, args.pluginId);
62
+ if (args.packageId === undefined) {
63
+ return Promise.resolve({
64
+ mode: "plugin",
65
+ pluginId: String(plugin.pluginId),
66
+ name: plugin.name,
67
+ purpose: plugin.purpose,
68
+ status: plugin.status,
69
+ packages: plugin.packages.map((pkg) => ({
70
+ packageId: String(pkg.packageId),
71
+ isCurrent: pkg.packageId === plugin.currentPackageId,
72
+ isNext: pkg.packageId === plugin.nextPackageId
73
+ }))
74
+ });
75
+ }
76
+ const pkg = ctx.dynamicCordisRunner.inspectPackage(agent, args.pluginId, args.packageId);
77
+ return Promise.resolve({
78
+ mode: "package",
79
+ packageId: String(pkg.packageId),
80
+ name: pkg.name,
81
+ purpose: pkg.purpose,
82
+ source: pkg.source,
83
+ diagnostics: pkg.diagnostics
84
+ });
85
+ }
86
+ }));
87
+ ctx.tools.register(defineTool({
88
+ name: "co_define",
89
+ description: "定义一个不可变的 Cordis Package。新建 Plugin 用 kind:'new'(提供 3-6 个小写字母前缀);给已有 Plugin 追加用 kind:'existing'。至少提供 code.host 和 code.client 其中之一。每个 value 是一个返回 Cordis Plugin 的纯 JavaScript 函数体;无 TypeScript/JSX。定义完成后再用 co_run 激活。",
90
+ parameters: {
91
+ plugin: {
92
+ required: true,
93
+ oneOf: [{
94
+ type: "object",
95
+ additionalProperties: false,
96
+ properties: { kind: { type: "string", const: "new", required: true }, idPrefix: { type: "string", required: true, description: "3-6 个小写字母语义前缀" } }
97
+ }, {
98
+ type: "object",
99
+ additionalProperties: false,
100
+ properties: { kind: { type: "string", const: "existing", required: true }, pluginId: { type: "string", required: true, description: "已有 Plugin 的精确 ID" } }
101
+ }]
102
+ },
103
+ name: { type: "string", required: true, description: "简短可读的包名" },
104
+ purpose: { type: "string", required: true, description: "一句话用途说明" },
105
+ code: {
106
+ type: "object",
107
+ additionalProperties: false,
108
+ required: true,
109
+ properties: {
110
+ host: { type: "string", description: "返回 Host 端 Cordis Plugin 的 JS 函数体" },
111
+ client: { type: "string", description: "返回 Client(浏览器)端 Cordis Plugin 的 JS 函数体" }
112
+ }
113
+ }
114
+ },
115
+ output: {
116
+ schema: { type: "object", properties: { pluginId: { type: "string", required: true }, packageId: { type: "string", required: true }, name: { type: "string", required: true }, purpose: { type: "string", required: true }, hasHostHalf: { type: "boolean" }, hasClientHalf: { type: "boolean" } }, additionalProperties: false },
117
+ render: (_a, v) => [{ type: "text", text: `已定义 ${v.pluginId}/${v.packageId} (${v.name});尚未运行,用 co_run 激活。` }]
118
+ },
119
+ execute(args, _exec) {
120
+ const plugin = args.plugin.kind === "new" ? { kind: "new", idPrefix: args.plugin.idPrefix } : { kind: "existing", pluginId: args.plugin.pluginId };
121
+ const receipt = ctx.dynamicCordisRunner.define({
122
+ sessionId: requireAgent(_exec).id,
123
+ plugin,
124
+ name: args.name,
125
+ purpose: args.purpose,
126
+ code: {
127
+ ...args.code.host === undefined ? {} : { host: args.code.host },
128
+ ...args.code.client === undefined ? {} : { client: args.code.client }
129
+ }
130
+ });
131
+ return Promise.resolve({
132
+ pluginId: String(receipt.pluginId),
133
+ packageId: String(receipt.packageId),
134
+ name: receipt.name,
135
+ purpose: receipt.purpose,
136
+ hasHostHalf: receipt.hasHostHalf,
137
+ hasClientHalf: receipt.hasClientHalf
138
+ });
139
+ }
140
+ }));
141
+ ctx.tools.register(defineTool({
142
+ name: "co_run",
143
+ description: "激活一个动态插件。首次激活或重启用 mode:'run';切换到同一 Plugin 下的另一个已定义 Package 用 mode:'update'。Client 端激活可能需要用户审批(返回 awaiting-approval);Host 端激活成功后返回 running。",
144
+ parameters: {
145
+ pluginId: { type: "string", required: true, description: "Plugin ID(来自 co_define 的返回)" },
146
+ packageId: { type: "string", required: true, description: "Package ID(来自 co_define 的返回)" },
147
+ mode: { type: "string", required: true, enum: ["run", "update"], description: "run=首次/重启/回滚;update=切换到同一 Plugin 不同版本" }
148
+ },
149
+ output: { schema: { type: "json" }, render: (_a, v) => [{ type: "text", text: JSON.stringify(v, null, 2) }] },
150
+ async execute(args, exec) {
151
+ const agent = requireAgent(exec);
152
+ const receipt = await ctx.dynamicCordisRunner.run(agent, args.pluginId, args.packageId, args.mode, exec.signal);
153
+ if (!receipt.ok)
154
+ throw new Error(receipt.message);
155
+ return {
156
+ status: receipt.status,
157
+ pluginId: args.pluginId,
158
+ packageId: args.packageId,
159
+ pluginRunId: String(receipt.pluginRunId),
160
+ mode: receipt.mode,
161
+ ...receipt.currentPackageId === undefined ? {} : { currentPackageId: String(receipt.currentPackageId) },
162
+ nextPackageId: String(receipt.nextPackageId),
163
+ ...receipt.hostStatus === undefined ? {} : { hostStatus: receipt.hostStatus }
164
+ };
165
+ }
166
+ }));
167
+ ctx.tools.register(defineTool({
168
+ name: "co_stop",
169
+ description: "停止一个正在运行的动态 Plugin。保留其定义、版本指针与审批记录,可以后续用 co_run 重新激活或回滚。要彻底删除用 co_remove。",
170
+ parameters: { pluginId: { type: "string", required: true, description: "要停止的 Plugin ID" } },
171
+ output: { schema: { type: "object", properties: { stopped: { type: "boolean" } }, additionalProperties: false } },
172
+ async execute(args, exec) {
173
+ await ctx.dynamicCordisRunner.stop(requireAgent(exec), args.pluginId);
174
+ return { stopped: true };
175
+ }
176
+ }));
177
+ ctx.tools.register(defineTool({
178
+ name: "co_remove",
179
+ description: "彻底删除一个动态 Plugin:先停止运行、取消审批请求,然后删除所有版本与授权。执行后其 pluginId、packageId 和 @ 引用全部失效。要保留版本以便重新激活用 co_stop。",
180
+ parameters: { pluginId: { type: "string", required: true, description: "要彻底删除的 Plugin ID" } },
181
+ output: { schema: { type: "object", properties: { removed: { type: "boolean" } }, additionalProperties: false } },
182
+ async execute(args, exec) {
183
+ await ctx.dynamicCordisRunner.undefine(requireAgent(exec), args.pluginId);
184
+ return { removed: true };
185
+ }
186
+ }));
187
+ }
188
+ export {
189
+ apply,
190
+ inject,
191
+ name
192
+ };