@r_masseater/masseater-utils 0.2.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r_masseater/masseater-utils",
3
- "version": "0.2.1",
3
+ "version": "0.4.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",
@@ -39,7 +39,8 @@
39
39
  "type": "module",
40
40
  "sideEffects": [
41
41
  "*.css",
42
- "./dist/vitest-otlp/setup.mjs"
42
+ "./dist/vitest-otlp/setup.mjs",
43
+ "./dist/model-server/process.mjs"
43
44
  ],
44
45
  "imports": {
45
46
  "#/*": "./src/*"
@@ -49,6 +50,8 @@
49
50
  "./skerm.css": "./src/app/skerm.css",
50
51
  "./usage-meter": "./dist/usage-meter.mjs",
51
52
  "./usage-meter/report": "./dist/usage-meter/report.mjs",
53
+ "./model-server": "./dist/model-server.mjs",
54
+ "./model-server/process": "./dist/model-server/process.mjs",
52
55
  "./plugin-kit": "./dist/plugin-kit.mjs",
53
56
  "./node-main": "./dist/node-main.mjs",
54
57
  "./hook-flags": "./dist/hook-flags.mjs",
@@ -101,6 +104,7 @@
101
104
  "@bufbuild/protobuf": "2.17.0",
102
105
  "@cloudflare/workers-types": "5.20261129.1",
103
106
  "@effect/platform-node": "4.2.0",
107
+ "@effect/platform-node-shared": "4.2.0",
104
108
  "@effect/vitest": "4.2.0",
105
109
  "@npmcli/package-json": "8.0.1",
106
110
  "@opentelemetry/api": "1.10.0",
@@ -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: parseJson to parse without throwing and isRecord to narrow
6
- the result to a plain object.
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
@@ -0,0 +1,50 @@
1
+ ---
2
+ name: use-model-server
3
+ description: >
4
+ Use when a Claude Code plugin hook or script needs a local model, such as text embeddings for
5
+ search: the model-server subpath of @r_masseater/masseater-utils runs one resident process per user that every plugin shares,
6
+ started on the first call and stopped when idle. Covers embed, warm, prepare, status and the
7
+ EmbeddingGemma 2 prefixes.
8
+ metadata:
9
+ type: core
10
+ library: "@r_masseater/masseater-utils"
11
+ sources:
12
+ - src/app/model-server*
13
+ - src/entities/embedding/model/text.ts
14
+ ---
15
+
16
+ # use-model-server
17
+
18
+ Loading a model takes seconds and gigabytes, and a hook runs as a new process each time. A plugin that loads its own model pays that on every hook and holds a second copy beside every other plugin. This subpath keeps one process per user that holds the models, so a hook pays only a socket round trip, about 35 ms for one query.
19
+
20
+ ## Setup
21
+
22
+ ```sh
23
+ bun add @r_masseater/masseater-utils
24
+ ```
25
+
26
+ Commit the `bun.lock` that `bun add` writes. The package bundles what it needs; the inference runtime installs itself into the user's cache folder on the first call.
27
+
28
+ ## Embed
29
+
30
+ ```ts
31
+ import { asDocument, asQuery, dot, embed } from "@r_masseater/masseater-utils/model-server";
32
+
33
+ const prompt = "add a table to D1";
34
+ const [query] = await embed({ texts: [asQuery(prompt)], timeoutMillis: 2000 });
35
+ const documents = await embed({
36
+ texts: [asDocument({ title: "db", text: "Schema, migrations and connections" })],
37
+ });
38
+ const score = dot(query, documents[0]);
39
+ ```
40
+
41
+ - Prefix a search query with `asQuery` and a searched text with `asDocument({ title, text })`. Without the prefixes the model still works but ranks worse.
42
+ - Vectors are normalized, so `dot(query, document)` is the cosine similarity.
43
+ - Embed the documents once and keep their vectors keyed by a hash of their text. Embed only the query in a hook.
44
+ - Keep texts short. `maxTokens` defaults to 512; long batches use gigabytes of memory.
45
+
46
+ ## Hooks
47
+
48
+ - Call `warm({})` from `SessionStart`. It returns at once and the server loads the model in the background.
49
+ - In `UserPromptSubmit` or `PostToolUse`, pass a short `timeoutMillis` and fall back to something without the model when `embed` rejects. The first run of a machine installs the runtime and downloads the model, which takes minutes.
50
+ - `status({})` says whether a server runs and which models it holds, without starting one.