@r_masseater/masseater-utils 0.9.2 → 0.10.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.
@@ -0,0 +1,18 @@
1
+ import { EffortLevel } from "@anthropic-ai/claude-agent-sdk";
2
+ import { Effect } from "effect";
3
+ /** @internal */
4
+ interface AgentRequest {
5
+ readonly directory: string;
6
+ readonly prompt: string;
7
+ readonly schema: Readonly<Record<string, unknown>>;
8
+ readonly tools: readonly string[];
9
+ readonly allowed: readonly string[];
10
+ readonly model: string;
11
+ readonly effort: EffortLevel;
12
+ readonly maxTurns: number;
13
+ }
14
+ /** @internal */
15
+ type AgentQuery = (request: AgentRequest) => Effect.Effect<unknown, string>;
16
+ /** @internal */
17
+ export declare const sdkAgentQuery: AgentQuery;
18
+ export type { AgentQuery, AgentRequest };
@@ -0,0 +1,52 @@
1
+ import { Effect, Option, Schema, Stream, pipe } from "effect";
2
+ // src/shared/lib/agent-query.ts
3
+ const SDK = "@anthropic-ai/claude-agent-sdk";
4
+ const ResultMessage = Schema.Struct({
5
+ type: Schema.Literal("result"),
6
+ subtype: Schema.String,
7
+ is_error: Schema.Boolean,
8
+ structured_output: Schema.optional(Schema.Unknown)
9
+ });
10
+ const isResult = Schema.is(ResultMessage);
11
+ const reasonOf = (failure) => {
12
+ if (failure instanceof Error) return failure.message;
13
+ return String(failure);
14
+ };
15
+ const loadSdk = Effect.tryPromise({
16
+ try: () => import("@anthropic-ai/claude-agent-sdk"),
17
+ catch: () => `${SDK} is not installed. Add it to the devDependencies of the repository root to run agents.`
18
+ });
19
+ const answerOf = (message) => {
20
+ if (message.is_error || message.subtype !== "success") return Effect.fail(`the agent ended with ${message.subtype}`);
21
+ return Effect.fromOption(Option.fromUndefinedOr(message.structured_output)).pipe(Effect.mapError(() => "the agent returned no structured output"));
22
+ };
23
+ const lastResult = (messages) => pipe(Stream.fromAsyncIterable(messages, reasonOf), Stream.filter(isResult), Stream.runLast);
24
+ /** @internal */
25
+ const sdkAgentQuery = Effect.fn("sdkAgentQuery")(function* querying(request) {
26
+ const messages = (yield* loadSdk).query({
27
+ prompt: request.prompt,
28
+ options: {
29
+ cwd: request.directory,
30
+ model: request.model,
31
+ effort: request.effort,
32
+ tools: [...request.tools],
33
+ allowedTools: [...request.allowed],
34
+ permissionMode: "dontAsk",
35
+ settingSources: [],
36
+ mcpServers: {},
37
+ strictMcpConfig: true,
38
+ persistSession: false,
39
+ maxTurns: request.maxTurns,
40
+ outputFormat: {
41
+ type: "json_schema",
42
+ schema: { ...request.schema }
43
+ }
44
+ }
45
+ });
46
+ const result = yield* lastResult(messages);
47
+ const message = yield* Effect.fromOption(result).pipe(Effect.mapError(() => "the agent ended without a result"));
48
+ return yield* answerOf(message);
49
+ });
50
+ export { sdkAgentQuery };
51
+
52
+ //# sourceMappingURL=agent-query.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-query.mjs","names":[],"sources":["../src/shared/lib/agent-query.ts"],"sourcesContent":["import type { EffortLevel, SDKMessage } from \"@anthropic-ai/claude-agent-sdk\";\nimport { Effect, Option, Schema, Stream, pipe } from \"effect\";\n\nconst SDK = \"@anthropic-ai/claude-agent-sdk\";\n\n/** @internal */\nexport interface AgentRequest {\n readonly directory: string;\n readonly prompt: string;\n readonly schema: Readonly<Record<string, unknown>>;\n readonly tools: readonly string[];\n readonly allowed: readonly string[];\n readonly model: string;\n readonly effort: EffortLevel;\n readonly maxTurns: number;\n}\n\n/** @internal */\nexport type AgentQuery = (request: AgentRequest) => Effect.Effect<unknown, string>;\n\nconst ResultMessage = Schema.Struct({\n type: Schema.Literal(\"result\"),\n subtype: Schema.String,\n is_error: Schema.Boolean,\n structured_output: Schema.optional(Schema.Unknown),\n});\n\ntype Result = typeof ResultMessage.Type;\n\nconst isResult = Schema.is(ResultMessage);\n\nconst reasonOf = (failure: unknown): string => {\n if (failure instanceof Error) {\n return failure.message;\n }\n return String(failure);\n};\n\nconst loadSdk = Effect.tryPromise({\n try: () => import(\"@anthropic-ai/claude-agent-sdk\"),\n catch: () =>\n `${SDK} is not installed. Add it to the devDependencies of the repository root to run agents.`,\n});\n\nconst answerOf = (message: Result): Effect.Effect<unknown, string> => {\n if (message.is_error || message.subtype !== \"success\") {\n return Effect.fail(`the agent ended with ${message.subtype}`);\n }\n return Effect.fromOption(Option.fromUndefinedOr(message.structured_output)).pipe(\n Effect.mapError(() => \"the agent returned no structured output\"),\n );\n};\n\nconst lastResult = (\n messages: AsyncIterable<SDKMessage>,\n): Effect.Effect<Option.Option<Result>, string> =>\n pipe(Stream.fromAsyncIterable(messages, reasonOf), Stream.filter(isResult), Stream.runLast);\n\n/** @internal */\nexport const sdkAgentQuery: AgentQuery = Effect.fn(\"sdkAgentQuery\")(function* querying(\n request: AgentRequest,\n) {\n const sdk = yield* loadSdk;\n const messages = sdk.query({\n prompt: request.prompt,\n options: {\n cwd: request.directory,\n model: request.model,\n effort: request.effort,\n tools: [...request.tools],\n allowedTools: [...request.allowed],\n permissionMode: \"dontAsk\",\n settingSources: [],\n mcpServers: {},\n strictMcpConfig: true,\n persistSession: false,\n maxTurns: request.maxTurns,\n outputFormat: { type: \"json_schema\", schema: { ...request.schema } },\n },\n });\n const result = yield* lastResult(messages);\n const message = yield* Effect.fromOption(result).pipe(\n Effect.mapError(() => \"the agent ended without a result\"),\n );\n return yield* answerOf(message);\n});\n"],"mappings":";;AAGA,MAAM,MAAM;AAiBZ,MAAM,gBAAgB,OAAO,OAAO;CAClC,MAAM,OAAO,QAAQ,QAAQ;CAC7B,SAAS,OAAO;CAChB,UAAU,OAAO;CACjB,mBAAmB,OAAO,SAAS,OAAO,OAAO;AACnD,CAAC;AAID,MAAM,WAAW,OAAO,GAAG,aAAa;AAExC,MAAM,YAAY,YAA6B;CAC7C,IAAI,mBAAmB,OACrB,OAAO,QAAQ;CAEjB,OAAO,OAAO,OAAO;AACvB;AAEA,MAAM,UAAU,OAAO,WAAW;CAChC,WAAW,OAAO;CAClB,aACE,GAAG,IAAI;AACX,CAAC;AAED,MAAM,YAAY,YAAoD;CACpE,IAAI,QAAQ,YAAY,QAAQ,YAAY,WAC1C,OAAO,OAAO,KAAK,wBAAwB,QAAQ,SAAS;CAE9D,OAAO,OAAO,WAAW,OAAO,gBAAgB,QAAQ,iBAAiB,CAAC,CAAC,CAAC,KAC1E,OAAO,eAAe,yCAAyC,CACjE;AACF;AAEA,MAAM,cACJ,aAEA,KAAK,OAAO,kBAAkB,UAAU,QAAQ,GAAG,OAAO,OAAO,QAAQ,GAAG,OAAO,OAAO;;AAG5F,MAAa,gBAA4B,OAAO,GAAG,eAAe,CAAC,CAAC,UAAU,SAC5E,SACA;CAEA,MAAM,YAAW,OADE,QAAA,CACE,MAAM;EACzB,QAAQ,QAAQ;EAChB,SAAS;GACP,KAAK,QAAQ;GACb,OAAO,QAAQ;GACf,QAAQ,QAAQ;GAChB,OAAO,CAAC,GAAG,QAAQ,KAAK;GACxB,cAAc,CAAC,GAAG,QAAQ,OAAO;GACjC,gBAAgB;GAChB,gBAAgB,CAAC;GACjB,YAAY,CAAC;GACb,iBAAiB;GACjB,gBAAgB;GAChB,UAAU,QAAQ;GAClB,cAAc;IAAE,MAAM;IAAe,QAAQ,EAAE,GAAG,QAAQ,OAAO;GAAE;EACrE;CACF,CAAC;CACD,MAAM,SAAS,OAAO,WAAW,QAAQ;CACzC,MAAM,UAAU,OAAO,OAAO,WAAW,MAAM,CAAC,CAAC,KAC/C,OAAO,eAAe,kCAAkC,CAC1D;CACA,OAAO,OAAO,SAAS,OAAO;AAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r_masseater/masseater-utils",
3
- "version": "0.9.2",
3
+ "version": "0.10.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, and what the Tauri desktop apps share: OpenTelemetry for the screens, Vite settings, the media-notes screens and the release commands.",
5
5
  "keywords": [
6
6
  "actions",
@@ -64,6 +64,7 @@
64
64
  "#/*": "./src/*"
65
65
  },
66
66
  "exports": {
67
+ "./agent-query": "./dist/agent-query.mjs",
67
68
  "./app-name": "./dist/app-name.mjs",
68
69
  "./browser-test/fonts.conf": "./src/app/browser-test-fonts.conf",
69
70
  "./browser-test/vite": "./dist/browser-test/vite.mjs",
@@ -164,6 +165,7 @@
164
165
  },
165
166
  "devDependencies": {
166
167
  "@alchemy.run/cloudflare": "2.0.0-beta.87",
168
+ "@anthropic-ai/claude-agent-sdk": "0.3.332",
167
169
  "@bufbuild/buf": "1.75.0",
168
170
  "@bufbuild/protobuf": "2.17.0",
169
171
  "@cloudflare/workers-types": "5.20261129.1",
@@ -223,6 +225,7 @@
223
225
  },
224
226
  "peerDependencies": {
225
227
  "@alchemy.run/cloudflare": "^2.0.0-beta.85",
228
+ "@anthropic-ai/claude-agent-sdk": ">=0.3.332",
226
229
  "@bufbuild/protobuf": ">=2.17.0",
227
230
  "@effect/atom-react": ">=4.1.7",
228
231
  "@effect/platform-node": ">=4.1.5",
@@ -270,6 +273,9 @@
270
273
  "@alchemy.run/cloudflare": {
271
274
  "optional": true
272
275
  },
276
+ "@anthropic-ai/claude-agent-sdk": {
277
+ "optional": true
278
+ },
273
279
  "@bufbuild/protobuf": {
274
280
  "optional": true
275
281
  },