@lunchbox-labs/opencode-tool-inspector 0.3.3 → 1.0.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.
package/README.md CHANGED
@@ -2,24 +2,28 @@
2
2
 
3
3
  Browse the tools available to OpenCode agents from the TUI: each tool's description, input schema and the agents that can use it.
4
4
 
5
- Requires Node **22.18+**. Built for OpenCode **1.19.x**, starting at **1.19.0**. For Kilo, use [`@lunchbox-labs/kilo-tool-inspector`](https://www.npmjs.com/package/@lunchbox-labs/kilo-tool-inspector).
5
+ Requires Node **22.18+** and OpenCode **2.4.3** or newer in 2.4.x. Version 1.0 requires OpenCode 2; 0.3.x is the last release for OpenCode 1. For Kilo, use [`@lunchbox-labs/kilo-tool-inspector`](https://www.npmjs.com/package/@lunchbox-labs/kilo-tool-inspector).
6
6
 
7
7
  ## Install
8
8
 
9
- Add the package to `plugin` in `tui.jsonc`:
9
+ Add the package to `plugins` in `opencode.jsonc`:
10
10
 
11
11
  ```jsonc
12
- { "plugin": ["@lunchbox-labs/opencode-tool-inspector"] }
12
+ { "plugins": ["@lunchbox-labs/opencode-tool-inspector"] }
13
13
  ```
14
14
 
15
+ The entry loads the package's server part, which reads OpenCode's tool registry, and its TUI part. It doesn't work from `cli.json`, which loads only TUI parts. When upgrading from 0.3, remove the entry OpenCode 2 migrated from `tui.json` into `cli.json` and add the package to `plugins` in `opencode.jsonc`. If `/tools` is missing, check `/plugins`, then run `opencode service restart`.
16
+
15
17
  ## Browse tools
16
18
 
17
- Run `/tools`, or **Browse tools** from the command palette, to list the tools OpenCode offers a model; the dialog's title names which one. Type to filter the list, press Enter to see a tool's input schema, the agents that can use it and its full description, and press Escape to return to the list.
19
+ Run `/tools`, or **Browse tools** from the command palette, to list every tool registered for the project: built-in, plugin and MCP tools. Type to filter the list by tool name, press Enter to see a tool's input schema, the agents that can use it and its full description, and press Escape to return to the list.
20
+
21
+ A tool's detail shows each agent's access under its configured permission rules and the current session's. It also says when only some models are offered the tool, and when agents with Code Mode call it through `execute`.
18
22
 
19
23
  ## Documentation
20
24
 
21
25
  The [wiki](https://github.com/lunchbox-labs/tool-inspector-plugin/wiki) has the full guides:
22
26
 
23
- - [Usage](https://github.com/lunchbox-labs/tool-inspector-plugin/wiki/Usage): which model's tools are listed, what the list leaves out and how input schemas are shown.
27
+ - [Usage](https://github.com/lunchbox-labs/tool-inspector-plugin/wiki/Usage): which tools are listed, what the list can't tell you and how input schemas are shown.
24
28
  - [Agent access](https://github.com/lunchbox-labs/tool-inspector-plugin/wiki/Usage#agent-access): how a tool's detail shows each agent's access.
25
- - [Developer](https://github.com/lunchbox-labs/tool-inspector-plugin/wiki/Developer): development, compatibility testing and publishing.
29
+ - [Developer](https://github.com/lunchbox-labs/tool-inspector-plugin/wiki/Developer): development, the RPC other plugins can call, compatibility testing and publishing.
@@ -0,0 +1,14 @@
1
+ // ../core/src/errors.ts
2
+ /** A readable message for anything a host or its client throws or returns as an error. */
3
+ function errorText(error) {
4
+ if (error instanceof Error) return error.message;
5
+ if (typeof error === "object" && error !== null) {
6
+ const record = error;
7
+ if (typeof record.message === "string") return record.message;
8
+ if (typeof record.data?.message === "string") return record.data.message;
9
+ return JSON.stringify(error);
10
+ }
11
+ return String(error);
12
+ }
13
+
14
+ export { errorText as t };
package/dist/rpc.d.ts ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * How the TUI part reads the tool registry, which OpenCode 2 exposes to server plugins only. The
3
+ * server part implements `tools`; the TUI part and any other client call it through
4
+ * `client.rpc(toolInspectorRpc)`.
5
+ */
6
+ export declare const toolInspectorRpc: {
7
+ readonly id: "tool-inspector";
8
+ readonly methods: {
9
+ /** Every tool registered at the call's location, after every plugin's changes. */
10
+ readonly tools: {
11
+ readonly input: {
12
+ readonly type: "object";
13
+ };
14
+ readonly output: {
15
+ readonly type: "object";
16
+ readonly properties: {
17
+ readonly tools: {
18
+ readonly type: "array";
19
+ readonly items: {
20
+ readonly type: "object";
21
+ readonly properties: {
22
+ readonly id: {
23
+ readonly type: "string";
24
+ };
25
+ readonly description: {
26
+ readonly type: "string";
27
+ };
28
+ readonly permission: {
29
+ readonly type: "string";
30
+ };
31
+ readonly codeMode: {
32
+ readonly type: "boolean";
33
+ };
34
+ readonly input: {};
35
+ readonly inputError: {
36
+ readonly type: "string";
37
+ };
38
+ readonly codeModeInput: {};
39
+ readonly codeModeInputError: {
40
+ readonly type: "string";
41
+ };
42
+ readonly codeModeDescription: {
43
+ readonly type: "string";
44
+ };
45
+ };
46
+ readonly required: readonly ["id", "description", "permission", "codeMode"];
47
+ };
48
+ };
49
+ };
50
+ readonly required: readonly ["tools"];
51
+ };
52
+ readonly errors: {
53
+ /** The tools couldn't be listed; the error's message says why. */
54
+ readonly failed: {
55
+ readonly type: "object";
56
+ };
57
+ };
58
+ };
59
+ };
60
+ readonly events: {};
61
+ };
62
+ /** One registered tool as the `tools` method returns it. */
63
+ export interface ListedTool {
64
+ /** The name the model calls the tool by. */
65
+ readonly id: string;
66
+ readonly description: string;
67
+ /** The permission action the host checks for the tool, such as `edit` for `write`. */
68
+ readonly permission: string;
69
+ /** Whether the tool runs inside Code Mode's `execute` for agents that have Code Mode. */
70
+ readonly codeMode: boolean;
71
+ /** The input as JSON Schema, left out when it couldn't be converted. */
72
+ readonly input?: unknown;
73
+ /** Why `input` is left out. */
74
+ readonly inputError?: string;
75
+ /** The input Code Mode presents instead, for a tool that declares one. */
76
+ readonly codeModeInput?: unknown;
77
+ /** Why `codeModeInput` is left out although the tool declares one. */
78
+ readonly codeModeInputError?: string;
79
+ /** The description Code Mode presents instead, for a tool that declares one. */
80
+ readonly codeModeDescription?: string;
81
+ }
82
+ export interface ToolsOutput {
83
+ readonly tools: readonly ListedTool[];
84
+ }
85
+ /**
86
+ * Reads the `tools` method's output, which TypeScript sees as `unknown` because the contract is
87
+ * JSON Schema. Skips entries without an id; `undefined` when the output isn't a tool list.
88
+ */
89
+ export declare function listedTools(output: unknown): ListedTool[] | undefined;
package/dist/rpc.js ADDED
@@ -0,0 +1,73 @@
1
+ // src/rpc.ts
2
+ /**
3
+ * How the TUI part reads the tool registry, which OpenCode 2 exposes to server plugins only. The
4
+ * server part implements `tools`; the TUI part and any other client call it through
5
+ * `client.rpc(toolInspectorRpc)`.
6
+ */
7
+ const toolInspectorRpc = {
8
+ id: "tool-inspector",
9
+ methods: {
10
+ /** Every tool registered at the call's location, after every plugin's changes. */
11
+ tools: {
12
+ input: { type: "object" },
13
+ output: {
14
+ type: "object",
15
+ properties: { tools: {
16
+ type: "array",
17
+ items: {
18
+ type: "object",
19
+ properties: {
20
+ id: { type: "string" },
21
+ description: { type: "string" },
22
+ permission: { type: "string" },
23
+ codeMode: { type: "boolean" },
24
+ input: {},
25
+ inputError: { type: "string" },
26
+ codeModeInput: {},
27
+ codeModeInputError: { type: "string" },
28
+ codeModeDescription: { type: "string" }
29
+ },
30
+ required: [
31
+ "id",
32
+ "description",
33
+ "permission",
34
+ "codeMode"
35
+ ]
36
+ }
37
+ } },
38
+ required: ["tools"]
39
+ },
40
+ errors: {
41
+ /** The tools couldn't be listed; the error's message says why. */
42
+ failed: { type: "object" } }
43
+ } },
44
+ events: {}
45
+ };
46
+ function isRecord(value) {
47
+ return typeof value === "object" && value !== null;
48
+ }
49
+ /**
50
+ * Reads the `tools` method's output, which TypeScript sees as `unknown` because the contract is
51
+ * JSON Schema. Skips entries without an id; `undefined` when the output isn't a tool list.
52
+ */
53
+ function listedTools(output) {
54
+ if (!isRecord(output) || !Array.isArray(output.tools)) return;
55
+ const tools = [];
56
+ for (const item of output.tools) {
57
+ if (!isRecord(item) || typeof item.id !== "string" || item.id === "") continue;
58
+ tools.push({
59
+ id: item.id,
60
+ description: typeof item.description === "string" ? item.description : "",
61
+ permission: typeof item.permission === "string" && item.permission !== "" ? item.permission : item.id,
62
+ codeMode: item.codeMode === true,
63
+ ...Object.hasOwn(item, "input") ? { input: item.input } : {},
64
+ ...typeof item.inputError === "string" ? { inputError: item.inputError } : {},
65
+ ...Object.hasOwn(item, "codeModeInput") ? { codeModeInput: item.codeModeInput } : {},
66
+ ...typeof item.codeModeInputError === "string" ? { codeModeInputError: item.codeModeInputError } : {},
67
+ ...typeof item.codeModeDescription === "string" ? { codeModeDescription: item.codeModeDescription } : {}
68
+ });
69
+ }
70
+ return tools;
71
+ }
72
+
73
+ export { listedTools, toolInspectorRpc };
@@ -0,0 +1,7 @@
1
+ import { Plugin } from "@opencode/plugin";
2
+ /**
3
+ * The server part. OpenCode 2 lists its tools to server plugins only, so this part answers the
4
+ * TUI part's request for them.
5
+ */
6
+ declare const server: Plugin.Plugin;
7
+ export { server as default };
package/dist/server.js ADDED
@@ -0,0 +1,247 @@
1
+ import { t as errorText } from "./chunks/errors.js";
2
+ import { toolInspectorRpc } from "./rpc.js";
3
+
4
+ // src/input-schema.ts
5
+ function isRecord(value) {
6
+ return typeof value === "object" && value !== null && !Array.isArray(value);
7
+ }
8
+ function isPlainObject(value) {
9
+ if (!isRecord(value)) return false;
10
+ const prototype = Object.getPrototypeOf(value);
11
+ return prototype === null || prototype === Object.prototype;
12
+ }
13
+ function canFlattenAllOf(allOf, parent) {
14
+ const keys = new Set(Object.keys(parent).filter((key) => key !== "allOf"));
15
+ return allOf.every((item) => Object.keys(item).every((key) => {
16
+ if (keys.has(key)) return false;
17
+ keys.add(key);
18
+ return true;
19
+ }));
20
+ }
21
+ /**
22
+ * The host's own simplification of the JSON Schema Effect emits, so the input reads as the model
23
+ * receives it: an integer's safe-integer range is dropped, and `allOf` members merge into their
24
+ * parent when no keyword would be overwritten.
25
+ */
26
+ function flattenAllOf(value) {
27
+ if (Array.isArray(value)) return value.map(flattenAllOf);
28
+ if (!isRecord(value)) return value;
29
+ const integer = value.type === "integer";
30
+ const schema = Object.fromEntries(Object.entries(value).filter(([key, item]) => !integer || !(key === "minimum" && item === Number.MIN_SAFE_INTEGER || key === "maximum" && item === Number.MAX_SAFE_INTEGER)).map(([key, item]) => [key, flattenAllOf(item)]));
31
+ const allOf = schema.allOf;
32
+ if (!Array.isArray(allOf)) return schema;
33
+ const members = allOf.filter(isRecord);
34
+ if (members.length !== allOf.length || !canFlattenAllOf(members, schema)) return schema;
35
+ const { allOf: _merged, ...rest } = schema;
36
+ return flattenAllOf({
37
+ ...Object.assign({}, ...members),
38
+ ...rest
39
+ });
40
+ }
41
+ /** The definition a `#/$defs/<name>` reference names. */
42
+ function definitionName(reference) {
43
+ const match = /^#\/\$defs\/([^/]*)$/.exec(reference);
44
+ if (match === null) return;
45
+ try {
46
+ return decodeURIComponent(match[1]).replaceAll("~1", "/").replaceAll("~0", "~");
47
+ } catch {
48
+ return;
49
+ }
50
+ }
51
+ /**
52
+ * The host's inlining of the references Effect emits for named schemas, so a field shows its
53
+ * shape where it is used. A definition that contains itself keeps its reference.
54
+ */
55
+ function inlineLocalReferences(value, definitions, seen = /* @__PURE__ */ new Set()) {
56
+ if (Array.isArray(value)) return value.map((item) => inlineLocalReferences(item, definitions, seen));
57
+ if (!isRecord(value)) return value;
58
+ const local = definitions ?? (isRecord(value.$defs) ? value.$defs : void 0);
59
+ if (typeof value.$ref === "string" && local !== void 0) {
60
+ const name = definitionName(value.$ref);
61
+ const target = name !== void 0 && !seen.has(name) && Object.hasOwn(local, name) ? local[name] : void 0;
62
+ if (name !== void 0 && target !== void 0 && target !== null) {
63
+ const { $ref: _reference, ...rest } = value;
64
+ const resolved = inlineLocalReferences(target, local, new Set(seen).add(name));
65
+ const siblings = inlineLocalReferences(rest, local, seen);
66
+ if (!isRecord(resolved) || !isRecord(siblings)) return resolved;
67
+ return Object.keys(resolved).every((key) => !(key in siblings)) ? {
68
+ ...resolved,
69
+ ...siblings
70
+ } : { allOf: [resolved, siblings] };
71
+ }
72
+ }
73
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, inlineLocalReferences(item, local, seen)]));
74
+ }
75
+ function hasLocalReference(value) {
76
+ if (Array.isArray(value)) return value.some(hasLocalReference);
77
+ if (!isRecord(value)) return false;
78
+ return typeof value.$ref === "string" && value.$ref.startsWith("#/$defs/") || Object.values(value).some(hasLocalReference);
79
+ }
80
+ function dropDefinitionsIfResolved(value) {
81
+ if (!isRecord(value) || hasLocalReference(value)) return value;
82
+ const { $defs: _definitions, ...rest } = value;
83
+ return rest;
84
+ }
85
+ const annotationKeywords = /* @__PURE__ */ new Set([
86
+ "title",
87
+ "description",
88
+ "default",
89
+ "examples",
90
+ "readOnly",
91
+ "writeOnly"
92
+ ]);
93
+ /**
94
+ * The host describes an Effect struct without fields, which Effect emits as "anything but null",
95
+ * as an empty object, keeping its annotations.
96
+ */
97
+ function emptyInputJsonSchema(effect, schema) {
98
+ const document = effect.toJsonSchemaDocument(schema, { referencePolicy: () => void 0 });
99
+ if (!isRecord(document.schema)) return;
100
+ const { not, ...rest } = document.schema;
101
+ if (!isRecord(not) || not.type !== "null" || Object.keys(not).length !== 1) return;
102
+ if (!Object.keys(rest).every((key) => annotationKeywords.has(key))) return;
103
+ return {
104
+ ...rest,
105
+ type: "object",
106
+ properties: {},
107
+ additionalProperties: false
108
+ };
109
+ }
110
+ function effectJsonSchema(effect, schema) {
111
+ const empty = emptyInputJsonSchema(effect, schema);
112
+ if (empty !== void 0) return empty;
113
+ const document = effect.toJsonSchemaDocument(schema, { onExcessProperty: "error" });
114
+ const root = document.schema;
115
+ return dropDefinitionsIfResolved(inlineLocalReferences(flattenAllOf(Object.keys(document.definitions).length === 0 || !isRecord(root) ? root : {
116
+ ...root,
117
+ $defs: document.definitions
118
+ })));
119
+ }
120
+ /** Only JSON survives the RPC, so anything else in a schema is dropped or refused here. */
121
+ function jsonOnly(schema) {
122
+ const text = JSON.stringify(schema);
123
+ if (text === void 0) return { error: "its schema isn't JSON" };
124
+ return { schema: JSON.parse(text) };
125
+ }
126
+ const draft = "draft-2020-12";
127
+ async function standardJsonSchema(schema, loaders) {
128
+ const standard = Reflect.get(schema, "~standard");
129
+ const jsonSchema = isRecord(standard) ? standard.jsonSchema : void 0;
130
+ const input = isRecord(jsonSchema) ? jsonSchema.input : void 0;
131
+ if (typeof input === "function") return jsonOnly(Reflect.apply(input, jsonSchema, [{ target: draft }]));
132
+ const vendor = isRecord(standard) ? standard.vendor : void 0;
133
+ if (vendor !== "zod" && !("_zod" in schema)) return { error: `its ${typeof vendor === "string" ? vendor : "Standard Schema"} schema doesn't convert to JSON Schema` };
134
+ const convert = Reflect.get(schema, "toJSONSchema");
135
+ if (typeof convert === "function") return jsonOnly(Reflect.apply(convert, schema, [{
136
+ target: draft,
137
+ io: "input"
138
+ }]));
139
+ let zod;
140
+ try {
141
+ zod = await loaders.zod();
142
+ } catch {
143
+ return { error: "it is a Zod schema that can't convert itself, and Zod's converter isn't available to this plugin" };
144
+ }
145
+ return jsonOnly(zod.toJSONSchema(schema, {
146
+ target: draft,
147
+ io: "input"
148
+ }));
149
+ }
150
+ /**
151
+ * Converts a registered tool's input to JSON Schema, following the host's own conversion case
152
+ * by case: no input is an empty schema, a Standard Schema converts itself, an Effect schema goes
153
+ * through the host's Effect and the host's simplification, and any other object is taken as JSON
154
+ * Schema already. The host refuses to register a tool whose input it can't convert, so an error
155
+ * here is this plugin's limit, not the tool's: a module it couldn't load, or a conversion that
156
+ * differs from the host's.
157
+ */
158
+ async function inputJsonSchema(input, loaders) {
159
+ try {
160
+ if (input === void 0 || input === null) return { schema: {} };
161
+ if (typeof input !== "object" && typeof input !== "function") return typeof input === "boolean" ? { schema: input } : { error: "its input isn't a schema" };
162
+ if ("~standard" in input) return await standardJsonSchema(input, loaders);
163
+ if (isPlainObject(input)) return jsonOnly(input);
164
+ let effect;
165
+ try {
166
+ effect = await loaders.effect();
167
+ } catch (error) {
168
+ return { error: `this plugin couldn't load Effect to check it (${errorText(error)})` };
169
+ }
170
+ return jsonOnly(effect.isSchema(input) ? effectJsonSchema(effect, input) : input);
171
+ } catch (error) {
172
+ return { error: errorText(error) };
173
+ }
174
+ }
175
+
176
+ // src/list-tools.ts
177
+ /**
178
+ * Imports a module that isn't a dependency. Taking the name as an argument keeps every build from
179
+ * resolving it, so it loads only where a copy is installed beside the plugin.
180
+ */
181
+ const importUnbundled = (specifier) => import(specifier);
182
+ const hostLoaders = {
183
+ /** The host maps a plugin's `effect` import to its own copy, the one its tools' schemas come from. */
184
+ effect: async () => (await import("effect")).Schema,
185
+ async zod() {
186
+ const module = await importUnbundled("zod/v4/core");
187
+ if (typeof module !== "object" || module === null || typeof Reflect.get(module, "toJSONSchema") !== "function") throw new Error("zod/v4/core has no toJSONSchema");
188
+ return module;
189
+ }
190
+ };
191
+ /**
192
+ * Core's `field` would bring the TUI part's permission code into the chunk the server part loads,
193
+ * so the server part keeps its own.
194
+ */
195
+ function field(value, key) {
196
+ return typeof value === "object" && value !== null ? Reflect.get(value, key) : void 0;
197
+ }
198
+ /** The registered tools as the `tools` method returns them, each with its input as JSON Schema. */
199
+ async function listTools(registered, loaders = hostLoaders) {
200
+ let effect;
201
+ let zod;
202
+ const once = {
203
+ effect: () => effect ??= loaders.effect(),
204
+ zod: () => zod ??= loaders.zod()
205
+ };
206
+ const tools = [];
207
+ for (const tool of registered) {
208
+ const options = tool.options;
209
+ const permission = field(options, "permission");
210
+ const codeMode = field(options, "codemode");
211
+ const input = await inputJsonSchema(tool.input, once);
212
+ const codeModeInput = field(codeMode, "input");
213
+ const override = codeModeInput === void 0 || codeModeInput === null ? void 0 : await inputJsonSchema(codeModeInput, once);
214
+ const codeModeDescription = field(codeMode, "description");
215
+ tools.push({
216
+ id: tool.id,
217
+ description: typeof tool.description === "string" ? tool.description : "",
218
+ permission: typeof permission === "string" && permission !== "" ? permission : tool.id,
219
+ codeMode: codeMode !== false,
220
+ ..."error" in input ? { inputError: input.error } : { input: input.schema },
221
+ ...override === void 0 ? {} : "error" in override ? { codeModeInputError: override.error } : { codeModeInput: override.schema },
222
+ ...typeof codeModeDescription === "string" ? { codeModeDescription } : {}
223
+ });
224
+ }
225
+ return { tools };
226
+ }
227
+
228
+ // src/server.ts
229
+ /**
230
+ * The server part. OpenCode 2 lists its tools to server plugins only, so this part answers the
231
+ * TUI part's request for them.
232
+ */
233
+ const server = {
234
+ id: toolInspectorRpc.id,
235
+ async setup(context) {
236
+ const registration = await context.rpc.register(toolInspectorRpc, { async tools(_input, call) {
237
+ try {
238
+ return await listTools(await context.tool.list());
239
+ } catch (error) {
240
+ return call.error("failed", errorText(error), {});
241
+ }
242
+ } });
243
+ return () => registration.dispose();
244
+ }
245
+ };
246
+
247
+ export { server as default };
package/dist/tui.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- declare const plugin: {
2
- id: string;
3
- tui: import("@opencode-ai/plugin/tui").TuiPlugin;
4
- };
5
- export { plugin as default };
1
+ import { Plugin } from "@opencode/plugin/tui";
2
+ /** The TUI part: the `/tools` browser, which reads tools through the server part. */
3
+ declare const tui: Plugin.Definition;
4
+ export { tui as default };