@r_masseater/masseater-utils 0.3.0 → 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.
- package/README.md +2 -2
- package/dist/index-Dq8LlOKp.d.mts +36 -0
- package/dist/node-main.d.mts +3 -17
- package/dist/node-main.mjs +2 -42
- package/dist/node-main.mjs.map +1 -1
- package/dist/plugin-kit.d.mts +9 -5683
- package/dist/plugin-kit.mjs +51 -11982
- package/dist/plugin-kit.mjs.map +1 -1
- package/dist/run-main-BD4uT8sr.mjs +45 -0
- package/dist/run-main-BD4uT8sr.mjs.map +1 -0
- package/package.json +1 -1
- package/skills/read-hook-input/SKILL.md +35 -2
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { t as flushUsage } from "./usage-meter-CmCceKx1.mjs";
|
|
2
|
+
import { Array, Cause, Console, Effect, Layer, Option, Predicate, Record, Runtime, String, pipe } from "effect";
|
|
3
|
+
import { runMain } from "@effect/platform-node/NodeRuntime";
|
|
4
|
+
import { layer } from "@effect/platform-node/NodeServices";
|
|
5
|
+
import { FetchHttpClient } from "effect/http";
|
|
6
|
+
import { Otlp, OtlpSerialization } from "effect/observability";
|
|
7
|
+
// src/features/run-main/model/report.ts
|
|
8
|
+
const TAG = "_tag";
|
|
9
|
+
const fieldsOf = (failure) => pipe(Option.liftPredicate(failure, Predicate.isObject), Option.map((object) => Record.toEntries({ ...object })), Option.getOrElse(() => []), Array.flatMap(([key, value]) => pipe(Option.liftPredicate(value, Predicate.isString), Option.filter(() => key !== TAG), Option.map((text) => `${key}: ${text}`), Option.toArray)), Array.join(", "));
|
|
10
|
+
const describedOf = (reason, name) => pipe(Option.liftPredicate(reason, Cause.isFailReason), Option.map((failed) => fieldsOf(failed.error)), Option.filter(String.isNonEmpty), Option.match({
|
|
11
|
+
onNone: () => name,
|
|
12
|
+
onSome: (fields) => `${name} (${fields})`
|
|
13
|
+
}));
|
|
14
|
+
const linesOf = (serviceName) => (reason) => pipe(Cause.prettyErrors(Cause.fromReasons([reason])), Array.map((error) => {
|
|
15
|
+
if (String.isEmpty(error.message)) return `${serviceName}: ${describedOf(reason, error.name)}`;
|
|
16
|
+
if (Cause.isDieReason(reason)) return `${serviceName}: ${error.name}: ${error.message}`;
|
|
17
|
+
return `${serviceName}: ${error.message}`;
|
|
18
|
+
}));
|
|
19
|
+
/** @internal */
|
|
20
|
+
const reportOf = (serviceName) => (cause) => {
|
|
21
|
+
if (Cause.hasInterruptsOnly(cause) || !cause.pipe(Cause.squash, Runtime.getErrorReported)) return [];
|
|
22
|
+
return pipe(cause.reasons, Array.filter((reason) => !Cause.isInterruptReason(reason)), Array.flatMap(linesOf(serviceName)));
|
|
23
|
+
};
|
|
24
|
+
// src/features/run-main/model/run-main.ts
|
|
25
|
+
const platform = Layer.mergeAll(layer, FetchHttpClient.layer);
|
|
26
|
+
const runWithTracingAnd = ({ serviceName, services }) => (program) => {
|
|
27
|
+
const tracing = Otlp.layerFromConfig({ resource: { serviceName } }).pipe(Layer.provide(OtlpSerialization.layerJson), Layer.provide(FetchHttpClient.layer));
|
|
28
|
+
const provided = Layer.provideMerge(services, platform);
|
|
29
|
+
const report = reportOf(serviceName);
|
|
30
|
+
runMain(Layer.mergeAll(tracing, provided).pipe(Layer.build, Effect.flatMap((context) => Effect.provideContext(program, context)), Effect.scoped, Effect.ensuring(flushUsage), Effect.tapCause((cause) => Effect.forEach(report(cause), (line) => Console.error(line), {
|
|
31
|
+
concurrency: 1,
|
|
32
|
+
discard: true
|
|
33
|
+
}))), { disableErrorReporting: true });
|
|
34
|
+
};
|
|
35
|
+
const runWithTracing = (serviceName) => runWithTracingAnd({
|
|
36
|
+
serviceName,
|
|
37
|
+
services: Layer.empty
|
|
38
|
+
});
|
|
39
|
+
/** @internal */
|
|
40
|
+
const runMainWith = runWithTracingAnd;
|
|
41
|
+
/** @internal */
|
|
42
|
+
const runMain$1 = runWithTracing;
|
|
43
|
+
export { runMainWith as n, runMain$1 as t };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=run-main-BD4uT8sr.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-main-BD4uT8sr.mjs","names":["Rec","Arr","Str","nodeServices","runMain"],"sources":["../src/features/run-main/model/report.ts","../src/features/run-main/model/run-main.ts"],"sourcesContent":["import {\n Array as Arr,\n Cause,\n Option,\n Predicate,\n Record as Rec,\n Runtime,\n String as Str,\n pipe,\n} from \"effect\";\n\nconst TAG = \"_tag\";\n\nconst fieldsOf = (failure: unknown): string =>\n pipe(\n Option.liftPredicate(failure, Predicate.isObject),\n Option.map((object): readonly (readonly [string, unknown])[] => Rec.toEntries({ ...object })),\n Option.getOrElse((): readonly (readonly [string, unknown])[] => []),\n Arr.flatMap(([key, value]) =>\n pipe(\n Option.liftPredicate(value, Predicate.isString),\n Option.filter(() => key !== TAG),\n Option.map((text) => `${key}: ${text}`),\n Option.toArray,\n ),\n ),\n Arr.join(\", \"),\n );\n\nconst describedOf = <Failure>(reason: Cause.Reason<Failure>, name: string): string =>\n pipe(\n Option.liftPredicate(reason, Cause.isFailReason),\n Option.map((failed) => fieldsOf(failed.error)),\n Option.filter(Str.isNonEmpty),\n Option.match({ onNone: () => name, onSome: (fields) => `${name} (${fields})` }),\n );\n\nconst linesOf =\n (serviceName: string) =>\n <Failure>(reason: Cause.Reason<Failure>): readonly string[] =>\n pipe(\n Cause.prettyErrors(Cause.fromReasons([reason])),\n Arr.map((error) => {\n if (Str.isEmpty(error.message)) {\n return `${serviceName}: ${describedOf(reason, error.name)}`;\n }\n if (Cause.isDieReason(reason)) {\n return `${serviceName}: ${error.name}: ${error.message}`;\n }\n return `${serviceName}: ${error.message}`;\n }),\n );\n\n/** @internal */\nexport const reportOf =\n (serviceName: string) =>\n <Failure>(cause: Cause.Cause<Failure>): readonly string[] => {\n if (Cause.hasInterruptsOnly(cause) || !cause.pipe(Cause.squash, Runtime.getErrorReported)) {\n return [];\n }\n return pipe(\n cause.reasons,\n Arr.filter((reason) => !Cause.isInterruptReason(reason)),\n Arr.flatMap(linesOf(serviceName)),\n );\n };\n","import { runMain as nodeRuntimeRunMain } from \"@effect/platform-node/NodeRuntime\";\nimport { layer as nodeServices } from \"@effect/platform-node/NodeServices\";\nimport type { NodeServices } from \"@effect/platform-node/NodeServices\";\nimport { Console, Effect, Layer } from \"effect\";\nimport { FetchHttpClient } from \"effect/http\";\nimport type { HttpClient } from \"effect/http\";\nimport { Otlp, OtlpSerialization } from \"effect/observability\";\n\nimport { flushUsage } from \"#/shared/lib/usage-meter.ts\";\n\nimport { reportOf } from \"./report.ts\";\n\n/** @internal */\nexport type Platform = NodeServices | HttpClient.HttpClient;\n\n/** @internal */\nexport type Run<Provided> = <Success, Failure>(\n program: Effect.Effect<Success, Failure, Platform | Provided>,\n) => void;\n\nconst platform = Layer.mergeAll(nodeServices, FetchHttpClient.layer);\n\nconst runWithTracingAnd =\n <Provided, LayerFailure>({\n serviceName,\n services,\n }: Readonly<{\n serviceName: string;\n services: Layer.Layer<Provided, LayerFailure, Platform>;\n }>): Run<Provided> =>\n (program) => {\n const tracing = Otlp.layerFromConfig({ resource: { serviceName } }).pipe(\n Layer.provide(OtlpSerialization.layerJson),\n Layer.provide(FetchHttpClient.layer),\n );\n const provided = Layer.provideMerge(services, platform);\n const report = reportOf(serviceName);\n nodeRuntimeRunMain(\n Layer.mergeAll(tracing, provided).pipe(\n Layer.build,\n Effect.flatMap((context) => Effect.provideContext(program, context)),\n Effect.scoped,\n Effect.ensuring(flushUsage),\n Effect.tapCause((cause) =>\n Effect.forEach(report(cause), (line) => Console.error(line), {\n concurrency: 1,\n discard: true,\n }),\n ),\n ),\n { disableErrorReporting: true },\n );\n };\n\nconst runWithTracing = (serviceName: string): Run<never> =>\n runWithTracingAnd({ serviceName, services: Layer.empty });\n\n/** @internal */\nexport const runMainWith = runWithTracingAnd;\n\n/** @internal */\nexport const runMain = runWithTracing;\n"],"mappings":";;;;;;;AAWA,MAAM,MAAM;AAE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,WAAW,MAAM,SAASE,OAAc,gBAAgB,KAAK;AAEnE,MAAM,qBACqB,EACvB,aACA,gBAKD,YAAY;CACX,MAAM,UAAU,KAAK,gBAAgB,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,KAClE,MAAM,QAAQ,kBAAkB,SAAS,GACzC,MAAM,QAAQ,gBAAgB,KAAK,CACrC;CACA,MAAM,WAAW,MAAM,aAAa,UAAU,QAAQ;CACtD,MAAM,SAAS,SAAS,WAAW;CACnC,QACE,MAAM,SAAS,SAAS,QAAQ,CAAC,CAAC,KAChC,MAAM,OACN,OAAO,SAAS,YAAY,OAAO,eAAe,SAAS,OAAO,CAAC,GACnE,OAAO,QACP,OAAO,SAAS,UAAU,GAC1B,OAAO,UAAU,UACf,OAAO,QAAQ,OAAO,KAAK,IAAI,SAAS,QAAQ,MAAM,IAAI,GAAG;EAC3D,aAAa;EACb,SAAS;CACX,CAAC,CACH,CACF,GACA,EAAE,uBAAuB,KAAK,CAChC;AACF;AAEF,MAAM,kBAAkB,gBACtB,kBAAkB;CAAE;CAAa,UAAU,MAAM;AAAM,CAAC;;AAG1D,MAAa,cAAc;;AAG3B,MAAaC,YAAU"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@r_masseater/masseater-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "The small shared libraries of masseater's repositories as subpaths of one package: a chip row for React, Claude Code plugin helpers, Effect entry points for Node.js and GitHub Actions, file import mapping, Node package readers, OTLP assertions for Vitest and Cloudflare Workers tracing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"actions",
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
name: read-hook-input
|
|
3
3
|
description: >
|
|
4
4
|
Use when a Claude Code plugin hook or script must read JSON it did not write, such as a hook
|
|
5
|
-
payload on stdin or a settings file:
|
|
6
|
-
|
|
5
|
+
payload on stdin or a settings file: runHook, readHookInput, writeHookOutput and blockHook for a
|
|
6
|
+
hook written with Effect, and parseJson and isRecord for plain code.
|
|
7
7
|
metadata:
|
|
8
8
|
type: core
|
|
9
9
|
library: "@r_masseater/masseater-utils"
|
|
10
10
|
sources:
|
|
11
11
|
- src/app/plugin-kit*
|
|
12
|
+
- src/features/run-main/model/hook-io.ts
|
|
13
|
+
- src/features/run-main/model/run-hook.ts
|
|
12
14
|
- src/shared/lib/record.ts
|
|
13
15
|
- README.md
|
|
14
16
|
---
|
|
@@ -25,6 +27,37 @@ bun add @r_masseater/masseater-utils
|
|
|
25
27
|
|
|
26
28
|
Commit the `bun.lock` that `bun add` writes. Claude Code installs the dependencies of a plugin from its `package.json` and a text `bun.lock` when it caches the plugin.
|
|
27
29
|
|
|
30
|
+
## Run a hook with Effect
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
bun add @r_masseater/masseater-utils effect @effect/platform-node
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import {
|
|
38
|
+
blockHook,
|
|
39
|
+
readHookInput,
|
|
40
|
+
runHook,
|
|
41
|
+
writeHookOutput,
|
|
42
|
+
} from "@r_masseater/masseater-utils/plugin-kit";
|
|
43
|
+
import { Effect, Schema } from "effect";
|
|
44
|
+
|
|
45
|
+
const Input = Schema.Struct({ tool_input: Schema.Struct({ command: Schema.String }) });
|
|
46
|
+
const Output = Schema.Struct({ systemMessage: Schema.String });
|
|
47
|
+
|
|
48
|
+
runHook("my-plugin")(
|
|
49
|
+
Effect.gen(function* guard() {
|
|
50
|
+
const input = yield* readHookInput(Input);
|
|
51
|
+
if (input.tool_input.command.startsWith("pnpm")) {
|
|
52
|
+
return yield* blockHook("Use vp instead of pnpm.");
|
|
53
|
+
}
|
|
54
|
+
yield* writeHookOutput(Output)({ systemMessage: "checked" });
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`readHookInput` decodes the JSON on stdin with the schema and keeps only the fields the schema names. `writeHookOutput` encodes the value and writes it to stdout. `blockHook` ends the hook with exit code 2 and writes the reason to stderr, which Claude Code shows to the model. `runHook` provides the Node services and sends traces under the given service name, like `runMain`. List `effect` and `@effect/platform-node` in the plugin's `dependencies`, because they are optional peers of this package and Claude Code installs only what the plugin lists.
|
|
60
|
+
|
|
28
61
|
## Read a payload
|
|
29
62
|
|
|
30
63
|
```ts
|