@pluginfinity/hooks 0.0.0-stage → 0.7.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/contract.js ADDED
@@ -0,0 +1,48 @@
1
+ import { Schema } from "effect";
2
+ import { HOOK_IO } from "./contract/events.js";
3
+ import { CLAUDE_HOOK_EVENTS, ClaudeHookEvent } from "./contract/hook-events.js";
4
+ import { TOOL_INPUTS } from "./contract/tool-inputs.js";
5
+ import { hookEventsZig } from "./contract/zig.js";
6
+ import { matcherApplies } from "./runtime/input.js";
7
+ import { ALLOWED_DECISIONS, ANSWER_EVENTS, DECISION_TAGS, makeDecision, supports } from "./contract/decisions.js";
8
+ import { FACTS_SCHEMA_VERSION, Facts, decodeFacts, factsHas, factsToolName, factsToolPrefix } from "./contract/facts.js";
9
+ import { closedResponse, encodeDecision } from "./contract/wire.js";
10
+
11
+ // src/contract/index.ts
12
+ /**
13
+ * pluginfinity's hook contract, platform-free: each hook event's input and output
14
+ * schema, the built-in tool inputs, the host-neutral decisions and their wire
15
+ * encoding, the build facts, and the generators for the JSON Schema files and Zig types.
16
+ *
17
+ * @packageDocumentation
18
+ */
19
+ const DIALECT = "https://json-schema.org/draft/2020-12/schema";
20
+ const render = (schema) => {
21
+ const document = Schema.toJsonSchemaDocument(schema);
22
+ const definitions = Object.keys(document.definitions).length === 0 ? {} : { $defs: document.definitions };
23
+ return `${JSON.stringify({
24
+ $schema: DIALECT,
25
+ ...document.schema,
26
+ ...definitions
27
+ }, null, " ")}\n`;
28
+ };
29
+ /**
30
+ * The JSON Schema documents for every Claude Code hook event, one for its
31
+ * input and one for its output, and one for each built-in tool's
32
+ * `tool_input`. The text is deterministic, so a regeneration that changes
33
+ * nothing writes the same bytes.
34
+ *
35
+ * @public
36
+ */
37
+ const hookSchemaFiles = () => [...CLAUDE_HOOK_EVENTS.flatMap((event) => [{
38
+ name: `${event}.input.schema.json`,
39
+ content: render(HOOK_IO[event].input)
40
+ }, {
41
+ name: `${event}.output.schema.json`,
42
+ content: render(HOOK_IO[event].output)
43
+ }]), ...Object.entries(TOOL_INPUTS).map(([tool, schema]) => ({
44
+ name: `tool-input.${tool}.schema.json`,
45
+ content: render(schema)
46
+ }))];
47
+
48
+ export { ALLOWED_DECISIONS, ANSWER_EVENTS, CLAUDE_HOOK_EVENTS, ClaudeHookEvent, DECISION_TAGS, FACTS_SCHEMA_VERSION, Facts, HOOK_IO, TOOL_INPUTS, closedResponse, decodeFacts, encodeDecision, factsHas, factsToolName, factsToolPrefix, hookEventsZig, hookSchemaFiles, makeDecision, matcherApplies, supports };
package/hook.js ADDED
@@ -0,0 +1,62 @@
1
+ import { makeDecision } from "./contract/decisions.js";
2
+ import { CLAUDE_HOOK_EVENTS } from "./contract/hook-events.js";
3
+
4
+ // src/hook.ts
5
+ /**
6
+ * The brand a module hook carries.
7
+ *
8
+ * @public
9
+ */
10
+ const HookModuleTypeId = "~@pluginfinity/hooks/HookModule";
11
+ /**
12
+ * Whether a value is a module hook.
13
+ *
14
+ * @public
15
+ */
16
+ const isHookModule = (u) => typeof u === "object" && u !== null && u["~@pluginfinity/hooks/HookModule"] === "~@pluginfinity/hooks/HookModule";
17
+ const builder = (event) => ({ make: (...args) => {
18
+ const [first, second] = args;
19
+ const tool = typeof first === "function" ? void 0 : first.tool;
20
+ const handler = typeof first === "function" ? first : second;
21
+ return {
22
+ [HookModuleTypeId]: HookModuleTypeId,
23
+ event,
24
+ tools: tool === void 0 ? void 0 : typeof tool === "string" ? [tool] : [...tool],
25
+ handler
26
+ };
27
+ } });
28
+ /**
29
+ * The module hook builders, one per Claude Code event, and the decision
30
+ * constructors a handler returns.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { Hook } from "@pluginfinity/hooks";
35
+ *
36
+ * export default Hook.PreToolUse.make({ tool: "Bash" }, (event) =>
37
+ * event.tool_input.command.includes("rm -rf /") ? Hook.deny("not here") : Hook.allow(),
38
+ * );
39
+ * ```
40
+ *
41
+ * @public
42
+ */
43
+ const Hook = {
44
+ ...Object.fromEntries(CLAUDE_HOOK_EVENTS.map((event) => [event, builder(event)])),
45
+ noop: () => makeDecision.Noop(),
46
+ allow: (reason, options) => makeDecision.Allow({
47
+ ...reason === void 0 ? {} : { reason },
48
+ ...options?.updatedInput === void 0 ? {} : { updatedInput: options.updatedInput }
49
+ }),
50
+ deny: (reason) => makeDecision.Deny(reason === void 0 ? {} : { reason }),
51
+ ask: (reason) => makeDecision.Ask(reason === void 0 ? {} : { reason }),
52
+ block: (reason) => makeDecision.Block(reason === void 0 ? {} : { reason }),
53
+ context: (text) => makeDecision.Context({ text }),
54
+ systemMessage: (text) => makeDecision.SystemMessage({ text }),
55
+ rewake: (message) => makeDecision.Rewake({ message }),
56
+ raw: (host, json) => makeDecision.Raw({
57
+ host,
58
+ json
59
+ })
60
+ };
61
+
62
+ export { Hook, HookModuleTypeId, isHookModule };