@pluginfinity/core 0.3.2 → 0.4.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/config.js +1 -1
- package/env.js +1 -1
- package/frontmatter.js +1 -1
- package/hook-io/build.js +152 -0
- package/hook-io/events.js +502 -0
- package/hook-io/index.js +37 -0
- package/hook-io/tool-inputs.js +91 -0
- package/hook-io/zig.js +78 -0
- package/hooks.js +137 -5
- package/index.d.ts +334 -8
- package/index.js +6 -2
- package/lsp.js +1 -1
- package/monitors.js +1 -1
- package/package.json +2 -2
- package/target.js +5 -0
- package/version.js +2 -2
package/hook-io/zig.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { CLAUDE_HOOK_EVENTS } from "../hooks.js";
|
|
3
|
+
import { HOOK_IO } from "./events.js";
|
|
4
|
+
import { TOOL_INPUTS } from "./tool-inputs.js";
|
|
5
|
+
|
|
6
|
+
// src/hook-io/zig.ts
|
|
7
|
+
const KEYWORDS = new Set("addrspace align allowzero and anyframe anytype asm async await break callconv catch comptime const continue defer else enum errdefer error export extern fn for if inline linksection noalias noinline nosuspend opaque or orelse packed pub resume return struct suspend switch test threadlocal try union unreachable usingnamespace var volatile while".split(" "));
|
|
8
|
+
const identifier = (name) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name) && !KEYWORDS.has(name) ? name : `@"${name}"`;
|
|
9
|
+
const COMMON = /* @__PURE__ */ new Set([
|
|
10
|
+
"session_id",
|
|
11
|
+
"transcript_path",
|
|
12
|
+
"cwd",
|
|
13
|
+
"prompt_id",
|
|
14
|
+
"scratchpad_dir",
|
|
15
|
+
"permission_mode",
|
|
16
|
+
"effort",
|
|
17
|
+
"agent_id",
|
|
18
|
+
"agent_type"
|
|
19
|
+
]);
|
|
20
|
+
const zigType = (schema, indent) => {
|
|
21
|
+
if (Array.isArray(schema.anyOf)) {
|
|
22
|
+
const variants = schema.anyOf;
|
|
23
|
+
const present = variants.filter((variant) => variant.type !== "null");
|
|
24
|
+
const [only] = present;
|
|
25
|
+
return present.length === 1 && variants.length === 2 && only !== void 0 ? `?${zigType(only, indent)}` : "std.json.Value";
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(schema.enum)) return "[]const u8";
|
|
28
|
+
if (schema.type === "string") return "[]const u8";
|
|
29
|
+
if (schema.type === "number") return "f64";
|
|
30
|
+
if (schema.type === "boolean") return "bool";
|
|
31
|
+
if (schema.type === "array") return `[]const ${zigType(schema.items ?? {}, indent)}`;
|
|
32
|
+
if (schema.type === "object" && schema.properties !== void 0) return zigStruct(schema, indent, false);
|
|
33
|
+
return "std.json.Value";
|
|
34
|
+
};
|
|
35
|
+
const zigStruct = (schema, indent, event) => {
|
|
36
|
+
const inner = `${indent} `;
|
|
37
|
+
const required = new Set(schema.required ?? []);
|
|
38
|
+
return `struct {\n${Object.entries(schema.properties).map(([name, field]) => {
|
|
39
|
+
const type = zigType(field, inner);
|
|
40
|
+
const optional = !required.has(name) || event && COMMON.has(name);
|
|
41
|
+
const doc = typeof field.description === "string" ? `${inner}/// ${field.description}\n` : "";
|
|
42
|
+
const spelled = optional ? `${type.startsWith("?") ? type : `?${type}`} = null` : type;
|
|
43
|
+
return `${doc}${inner}${identifier(name)}: ${spelled},\n`;
|
|
44
|
+
}).join("")}${indent}}`;
|
|
45
|
+
};
|
|
46
|
+
const declaration = (name, schema, indent, event) => {
|
|
47
|
+
const json = Schema.toJsonSchemaDocument(schema).schema;
|
|
48
|
+
return `${typeof json.description === "string" ? `${indent}/// ${json.description}\n` : ""}${indent}pub const ${identifier(name)} = ${zigStruct(json, indent, event)};\n`;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The Zig source of `zig/src/events.zig`: a struct for each Claude Code hook
|
|
52
|
+
* event's input and, under `tools`, for each built-in tool's `tool_input`. The
|
|
53
|
+
* text is what `zig fmt` would write, so a regeneration that changes nothing
|
|
54
|
+
* writes the same bytes.
|
|
55
|
+
*
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
const hookEventsZig = () => [
|
|
59
|
+
"//! Generated from the hook event schemas in @pluginfinity/core. Do not edit:",
|
|
60
|
+
"//! run `pnpm --filter @pluginfinity/core run hook-io:zig`.",
|
|
61
|
+
"//!",
|
|
62
|
+
"//! One struct per Claude Code hook event, as `Hook.as` reads it. The fields every",
|
|
63
|
+
"//! event may carry are optional, since a host other than Claude Code sends fewer;",
|
|
64
|
+
"//! an event's own fields are required where Claude Code always sends them. A",
|
|
65
|
+
"//! field whose values are a documented set is text, so a value added later still",
|
|
66
|
+
"//! parses, and a free-form field is a `std.json.Value`.",
|
|
67
|
+
"",
|
|
68
|
+
"const std = @import(\"std\");",
|
|
69
|
+
"",
|
|
70
|
+
...CLAUDE_HOOK_EVENTS.map((event) => declaration(event, HOOK_IO[event].input, "", true)),
|
|
71
|
+
"/// The `tool_input` of each built-in tool, as `Hook.toolInput` reads it.",
|
|
72
|
+
"pub const tools = struct {",
|
|
73
|
+
Object.entries(TOOL_INPUTS).map(([tool, schema]) => declaration(tool, schema, " ", false)).join("\n").trimEnd(),
|
|
74
|
+
"};",
|
|
75
|
+
""
|
|
76
|
+
].join("\n");
|
|
77
|
+
|
|
78
|
+
export { hookEventsZig };
|
package/hooks.js
CHANGED
|
@@ -12,6 +12,7 @@ const CLAUDE_HOOK_EVENTS = [
|
|
|
12
12
|
"Setup",
|
|
13
13
|
"UserPromptSubmit",
|
|
14
14
|
"UserPromptExpansion",
|
|
15
|
+
"PreSkillShellCommand",
|
|
15
16
|
"PreToolUse",
|
|
16
17
|
"PermissionRequest",
|
|
17
18
|
"PermissionDenied",
|
|
@@ -71,7 +72,25 @@ const sharedHookFields = {
|
|
|
71
72
|
/** What a target without this event does. Defaults to `"fail"`. */
|
|
72
73
|
fallback: Schema.optionalKey(HookFallback),
|
|
73
74
|
/** Deny or block when the script fails before answering. Defaults to failing open. */
|
|
74
|
-
failClosed: Schema.optionalKey(Schema.Boolean)
|
|
75
|
+
failClosed: Schema.optionalKey(Schema.Boolean),
|
|
76
|
+
/**
|
|
77
|
+
* A Claude Code permission rule, such as `Bash(git *)` or `Edit(*.ts)`: the
|
|
78
|
+
* hook runs only for a tool call that matches, and no process is spawned
|
|
79
|
+
* otherwise. Tool events only. A host without it runs the hook on every
|
|
80
|
+
* matcher hit.
|
|
81
|
+
*/
|
|
82
|
+
if: Schema.optionalKey(Schema.String.check(Schema.isMinLength(1))),
|
|
83
|
+
/** The spinner message shown while the hook runs, on a host that has one. */
|
|
84
|
+
statusMessage: Schema.optionalKey(Schema.String)
|
|
85
|
+
};
|
|
86
|
+
const programHookFields = {
|
|
87
|
+
/** Run in the background without blocking. The hook can then decide nothing. */
|
|
88
|
+
async: Schema.optionalKey(Schema.Boolean),
|
|
89
|
+
/**
|
|
90
|
+
* Run in the background, and wake the agent when the hook asks to: a script
|
|
91
|
+
* calls `hook_rewake "<message>"`, a native hook `h.rewake(message)`.
|
|
92
|
+
*/
|
|
93
|
+
asyncRewake: Schema.optionalKey(Schema.Boolean)
|
|
75
94
|
};
|
|
76
95
|
/**
|
|
77
96
|
* A hook that runs a script the plugin ships. The build checks the script
|
|
@@ -82,6 +101,7 @@ const sharedHookFields = {
|
|
|
82
101
|
const ScriptHook = Schema.Struct({
|
|
83
102
|
script: PluginRelativePath,
|
|
84
103
|
args: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
104
|
+
...programHookFields,
|
|
85
105
|
...sharedHookFields
|
|
86
106
|
});
|
|
87
107
|
/**
|
|
@@ -91,15 +111,127 @@ const ScriptHook = Schema.Struct({
|
|
|
91
111
|
*/
|
|
92
112
|
const CommandHook = Schema.Struct({
|
|
93
113
|
command: Schema.String.check(Schema.isMinLength(1)),
|
|
114
|
+
/** The shell the command runs in, on a host that lets a hook choose. Defaults to the host's own. */
|
|
115
|
+
shell: Schema.optionalKey(Schema.Literals(["bash", "powershell"])),
|
|
116
|
+
...programHookFields,
|
|
117
|
+
...sharedHookFields
|
|
118
|
+
});
|
|
119
|
+
/**
|
|
120
|
+
* A hook that runs a prebuilt native program, one file per platform. `binary`
|
|
121
|
+
* is the path the files share: the build ships every `<binary>-<os>-<arch>`
|
|
122
|
+
* the plugin holds, named as `uname -s` and `uname -m` print there (such as
|
|
123
|
+
* `hooks/bin/guard-Darwin-arm64` and `hooks/bin/guard-Linux-x86_64`), and
|
|
124
|
+
* writes each host a command that runs the one for the machine.
|
|
125
|
+
*
|
|
126
|
+
* @public
|
|
127
|
+
*/
|
|
128
|
+
const BinaryHook = Schema.Struct({
|
|
129
|
+
binary: PluginRelativePath,
|
|
130
|
+
args: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
131
|
+
...programHookFields,
|
|
132
|
+
...sharedHookFields
|
|
133
|
+
});
|
|
134
|
+
/**
|
|
135
|
+
* A hook that sends the event's JSON to a URL as an HTTP POST and reads the
|
|
136
|
+
* response body as the hook's answer.
|
|
137
|
+
*
|
|
138
|
+
* @public
|
|
139
|
+
*/
|
|
140
|
+
const HttpHook = Schema.Struct({
|
|
141
|
+
http: Schema.String.check(Schema.isMinLength(1)),
|
|
142
|
+
/** Extra request headers. A value may name an environment variable as `$NAME` or `${NAME}`. */
|
|
143
|
+
headers: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
|
|
144
|
+
/** The environment variables `headers` may interpolate. Any other reads as empty. */
|
|
145
|
+
allowedEnvVars: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
146
|
+
...sharedHookFields
|
|
147
|
+
});
|
|
148
|
+
/**
|
|
149
|
+
* A hook that calls a tool on an MCP server and reads its text output as the
|
|
150
|
+
* hook's answer. A `server` that names one of the plugin's own servers is
|
|
151
|
+
* written with the plugin's scope.
|
|
152
|
+
*
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
155
|
+
const McpToolHook = Schema.Struct({
|
|
156
|
+
mcpTool: Schema.Struct({
|
|
157
|
+
server: Schema.String.check(Schema.isMinLength(1)),
|
|
158
|
+
tool: Schema.String.check(Schema.isMinLength(1)),
|
|
159
|
+
/** The tool's arguments. A string value may take a field of the event as `${tool_input.file_path}`. */
|
|
160
|
+
input: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown))
|
|
161
|
+
}),
|
|
162
|
+
...sharedHookFields
|
|
163
|
+
});
|
|
164
|
+
/**
|
|
165
|
+
* A hook that asks a model one question and reads `{ok, reason}` back.
|
|
166
|
+
* `$ARGUMENTS` in the prompt is the event's JSON.
|
|
167
|
+
*
|
|
168
|
+
* @public
|
|
169
|
+
*/
|
|
170
|
+
const PromptHook = Schema.Struct({
|
|
171
|
+
prompt: Schema.String.check(Schema.isMinLength(1)),
|
|
172
|
+
model: Schema.optionalKey(Schema.String),
|
|
173
|
+
/** Give a refusal's reason back to the agent and continue, instead of ending the turn. */
|
|
174
|
+
continueOnBlock: Schema.optionalKey(Schema.Boolean),
|
|
175
|
+
...sharedHookFields
|
|
176
|
+
});
|
|
177
|
+
/**
|
|
178
|
+
* A hook that runs a subagent with tool access to verify a condition; `agent`
|
|
179
|
+
* is its prompt. Experimental on Claude Code.
|
|
180
|
+
*
|
|
181
|
+
* @public
|
|
182
|
+
*/
|
|
183
|
+
const AgentHook = Schema.Struct({
|
|
184
|
+
agent: Schema.String.check(Schema.isMinLength(1)),
|
|
185
|
+
model: Schema.optionalKey(Schema.String),
|
|
94
186
|
...sharedHookFields
|
|
95
187
|
});
|
|
96
188
|
/**
|
|
97
|
-
* One hook: exactly one of `script
|
|
98
|
-
* entry decodes as whichever
|
|
189
|
+
* One hook: exactly one of `script`, `command`, `binary`, `http`, `mcpTool`,
|
|
190
|
+
* `prompt` or `agent`. Decode strictly, or a mixed entry decodes as whichever
|
|
191
|
+
* member matches first.
|
|
99
192
|
*
|
|
100
193
|
* @public
|
|
101
194
|
*/
|
|
102
|
-
const HookEntry = Schema.Union([
|
|
195
|
+
const HookEntry = Schema.Union([
|
|
196
|
+
ScriptHook,
|
|
197
|
+
CommandHook,
|
|
198
|
+
BinaryHook,
|
|
199
|
+
HttpHook,
|
|
200
|
+
McpToolHook,
|
|
201
|
+
PromptHook,
|
|
202
|
+
AgentHook
|
|
203
|
+
]);
|
|
204
|
+
/**
|
|
205
|
+
* The kinds of hook handler a host can run, in Claude Code's names. A
|
|
206
|
+
* `script`, a `command` and a `binary` are all `command` handlers.
|
|
207
|
+
*
|
|
208
|
+
* @public
|
|
209
|
+
*/
|
|
210
|
+
const HOOK_HANDLERS = [
|
|
211
|
+
"command",
|
|
212
|
+
"http",
|
|
213
|
+
"mcp_tool",
|
|
214
|
+
"prompt",
|
|
215
|
+
"agent"
|
|
216
|
+
];
|
|
217
|
+
/**
|
|
218
|
+
* The handler kind an entry is.
|
|
219
|
+
*
|
|
220
|
+
* @public
|
|
221
|
+
*/
|
|
222
|
+
const hookHandler = (entry) => "http" in entry ? "http" : "mcpTool" in entry ? "mcp_tool" : "prompt" in entry ? "prompt" : "agent" in entry ? "agent" : "command";
|
|
223
|
+
/**
|
|
224
|
+
* The entry fields beyond the handler itself that only some hosts carry.
|
|
225
|
+
*
|
|
226
|
+
* @public
|
|
227
|
+
*/
|
|
228
|
+
const HOOK_ENTRY_FIELDS = [
|
|
229
|
+
"if",
|
|
230
|
+
"statusMessage",
|
|
231
|
+
"async",
|
|
232
|
+
"asyncRewake",
|
|
233
|
+
"shell"
|
|
234
|
+
];
|
|
103
235
|
/**
|
|
104
236
|
* The hooks for one event. An empty list removes a base event's hooks when
|
|
105
237
|
* used in a target override.
|
|
@@ -124,4 +256,4 @@ const makeHooks = (events) => Schema.Struct(Object.fromEntries(events.map((event
|
|
|
124
256
|
*/
|
|
125
257
|
const Hooks = makeHooks(CLAUDE_HOOK_EVENTS);
|
|
126
258
|
|
|
127
|
-
export { CLAUDE_HOOK_EVENTS, ClaudeHookEvent, CommandHook, HookEntries, HookEntry, HookFallback, Hooks, PluginRelativePath, ScriptHook, makeHooks };
|
|
259
|
+
export { AgentHook, BinaryHook, CLAUDE_HOOK_EVENTS, ClaudeHookEvent, CommandHook, HOOK_ENTRY_FIELDS, HOOK_HANDLERS, HookEntries, HookEntry, HookFallback, Hooks, HttpHook, McpToolHook, PluginRelativePath, PromptHook, ScriptHook, hookHandler, makeHooks };
|