@gjermundgaraba/effect-actions 0.41.0 → 0.41.1

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.
@@ -27,6 +27,13 @@ const assertToolsServable = (lifted) => {
27
27
  const misplaced = lifted.flatMap(([action, lift]) => isMisplaced(action, lift) ? [action.name] : []);
28
28
  if (misplaced.length > 0) throw new Error(`MCP media must be the success or an array of it, a member of a union success whose other members encode to a string, or a top-level field of a struct success, one, optional or a required array: ${misplaced.join(", ")} (make the image such a field, or give an optional array as a required one, empty when absent; an image in input or an error is not served over MCP, so leave that action out of actions)`);
29
29
  };
30
+ const structuredOf = (lift) => {
31
+ switch (lift._tag) {
32
+ case "Plain": return lift.success;
33
+ case "Fields": return lift.rest;
34
+ case "Either": return;
35
+ }
36
+ };
30
37
  const internalErrorResult = new McpSchema.CallToolResult({
31
38
  isError: true,
32
39
  content: [{
@@ -89,9 +96,9 @@ const registerTools = Effect.fnUntraced(function* (apps, authentication) {
89
96
  const toolSuccess = toolSuccessOf(action.success);
90
97
  const run = answeringWhole(action, handler);
91
98
  const lift = liftOf(toolSuccess);
92
- const media = lift.fields.length === 0 ? void 0 : lift;
93
- const structuredSuccess = media === void 0 ? toolSuccess : media.rest;
94
- const text = media === void 0 && isText(toolSuccess);
99
+ const structuredSuccess = structuredOf(lift);
100
+ const text = lift._tag === "Plain" && isText(toolSuccess);
101
+ const isOtherwise = lift._tag === "Either" ? Schema.is(lift.otherwise) : () => false;
95
102
  const structured = structuredSuccess === void 0 || text ? void 0 : Schema.toCodecJson(structuredSuccess);
96
103
  const outputSchema = structured === void 0 ? void 0 : yield* Schema.decodeUnknownEffect(McpSchema.ToolOutputJson)(toolJsonSchema(structured, false)).pipe(Effect.orDie);
97
104
  const tool = yield* toolOf(action, outputSchema).pipe(Effect.orDie);
@@ -100,22 +107,21 @@ const registerTools = Effect.fnUntraced(function* (apps, authentication) {
100
107
  const encodeFailure = encoder(Schema.Union(projectedErrors(action)));
101
108
  const reported = (input) => action.progress === void 0 ? run(input) : Effect.flatMap(progressNotifier(registry.notifications, action.progress), (sink) => run(input, sink));
102
109
  const call = (input) => asCaller(authentication, action, reported(input));
103
- const liftedFieldNames = new Set(media?.fields.map(({ name }) => name));
110
+ const liftedFieldNames = new Set(lift._tag === "Plain" ? [] : lift.fields.map(({ name }) => name));
104
111
  const resultOf = (encoded, success) => {
105
- const answer = text || media?.otherwise !== void 0 ? encoded : void 0;
106
- if (Predicate.isString(answer)) return new McpSchema.CallToolResult({
112
+ if ((text || isOtherwise(success)) && Predicate.isString(encoded)) return new McpSchema.CallToolResult({
107
113
  isError: false,
108
114
  content: [{
109
115
  type: "text",
110
- text: answer
116
+ text: encoded
111
117
  }]
112
118
  });
113
- if (media === void 0) return new McpSchema.CallToolResult({
119
+ if (lift._tag === "Plain") return new McpSchema.CallToolResult({
114
120
  isError: false,
115
121
  structuredContent: encoded,
116
122
  content: nativeJsonTextOf(encoded)
117
123
  });
118
- const blocks = liftedFrom(media, success).map(contentBlockOf);
124
+ const blocks = liftedFrom(lift.fields, success).map(contentBlockOf);
119
125
  if (structured === void 0) return new McpSchema.CallToolResult({
120
126
  isError: false,
121
127
  content: blocks
@@ -30,23 +30,25 @@ const ProgressOf = (progress) => Schema.TaggedStruct("Progress", { value: progre
30
30
  value: reported.value
31
31
  }))
32
32
  }));
33
+ const jsonTextPartOf = (encoded) => Prompt.textPart({ text: Predicate.isString(encoded) ? encoded : JSON.stringify(encoded) });
34
+ const filePartsOf = (lifted) => lifted.map(({ value }) => Prompt.filePart({
35
+ mediaType: value.mimeType,
36
+ data: value.data
37
+ }));
33
38
  const modelOutputOf = (success) => {
34
39
  const lift = liftOf(success);
35
- if (lift.fields.length === 0) return;
36
- const encodeRest = lift.rest === void 0 ? void 0 : Schema.encodeUnknownSync(Schema.toCodecJson(lift.rest));
37
- const encodeOtherwise = lift.otherwise === void 0 ? void 0 : Schema.encodeUnknownSync(Schema.toCodecJson(lift.otherwise));
38
- const textOf = (result) => {
39
- const text = encodeOtherwise === void 0 || liftedFrom(lift, result).length > 0 ? void 0 : encodeOtherwise(result);
40
- return Predicate.isString(text) ? [Prompt.textPart({ text })] : [];
41
- };
42
- return (result) => [
43
- ...textOf(result),
44
- ...encodeRest === void 0 ? [] : [Prompt.textPart({ text: JSON.stringify(encodeRest(result)) })],
45
- ...liftedFrom(lift, result).map(({ value }) => Prompt.filePart({
46
- mediaType: value.mimeType,
47
- data: value.data
48
- }))
49
- ];
40
+ switch (lift._tag) {
41
+ case "Plain": return;
42
+ case "Either": {
43
+ const isOtherwise = Schema.is(lift.otherwise);
44
+ const encodeOtherwise = Schema.encodeUnknownSync(Schema.toCodecJson(lift.otherwise));
45
+ return (result) => isOtherwise(result) ? [jsonTextPartOf(encodeOtherwise(result))] : filePartsOf(liftedFrom(lift.fields, result));
46
+ }
47
+ case "Fields": {
48
+ const encodeRest = lift.rest === void 0 ? void 0 : Schema.encodeUnknownSync(Schema.toCodecJson(lift.rest));
49
+ return (result) => [...encodeRest === void 0 ? [] : [Prompt.textPart({ text: JSON.stringify(encodeRest(result)) })], ...filePartsOf(liftedFrom(lift.fields, result))];
50
+ }
51
+ }
50
52
  };
51
53
  const jsonToolOf = (action) => {
52
54
  const success = toolSuccessOf(action.success);
@@ -37,12 +37,12 @@ const liftOf = (success) => {
37
37
  const ast = unsuspended(success.ast);
38
38
  const whole = wholeSuccessMediaOf(ast);
39
39
  if (whole !== void 0) return {
40
+ _tag: "Fields",
40
41
  fields: [{
41
42
  name: void 0,
42
43
  ...whole
43
44
  }],
44
- rest: void 0,
45
- otherwise: void 0
45
+ rest: void 0
46
46
  };
47
47
  if (SchemaAST.isUnion(ast) && ast.encoding === void 0) {
48
48
  const members = ast.types.map((member) => [member, wholeSuccessMediaOf(member)]);
@@ -51,16 +51,20 @@ const liftOf = (success) => {
51
51
  ...lifted
52
52
  }]);
53
53
  const others = members.flatMap(([member, lifted]) => lifted === void 0 ? [Schema.make(member)] : []);
54
- if (media.length > 0) return {
54
+ const [only, ...more] = others;
55
+ if (media.length > 0) return only === void 0 ? {
56
+ _tag: "Fields",
55
57
  fields: media,
56
- rest: void 0,
57
- otherwise: others.length > 1 ? Schema.Union(others) : others[0]
58
+ rest: void 0
59
+ } : {
60
+ _tag: "Either",
61
+ fields: media,
62
+ otherwise: more.length === 0 ? only : Schema.Union(others)
58
63
  };
59
64
  }
60
65
  if (!SchemaAST.isObjects(ast) || ast.encoding !== void 0) return {
61
- fields: [],
62
- rest: success,
63
- otherwise: void 0
66
+ _tag: "Plain",
67
+ success
64
68
  };
65
69
  const fields = ast.propertySignatures.flatMap(({ name, type }) => {
66
70
  const field = mediaFieldOf(type);
@@ -70,20 +74,19 @@ const liftOf = (success) => {
70
74
  }];
71
75
  });
72
76
  if (fields.length === 0) return {
73
- fields,
74
- rest: success,
75
- otherwise: void 0
77
+ _tag: "Plain",
78
+ success
76
79
  };
77
80
  const names = new Set(fields.map(({ name }) => name));
78
81
  const rest = ast.propertySignatures.filter(({ name }) => !names.has(name));
79
82
  const { identifier: _identifierOfWholeSuccess, ...annotationsOfRest } = ast.annotations ?? {};
80
83
  return {
84
+ _tag: "Fields",
81
85
  fields,
82
- otherwise: void 0,
83
86
  rest: rest.length === 0 && ast.indexSignatures.length === 0 ? void 0 : Schema.make(new SchemaAST.Objects(rest, ast.indexSignatures, annotationsOfRest))
84
87
  };
85
88
  };
86
- const liftedFrom = (lift, success) => lift.fields.flatMap(({ name, kind, many }) => {
89
+ const liftedFrom = (fields, success) => fields.flatMap(({ name, kind, many }) => {
87
90
  const field = name !== void 0 && Predicate.hasProperty(success, name) ? success[name] : void 0;
88
91
  const value = name === void 0 ? success : field;
89
92
  const listed = isMediaArray(value) ? value : [];
@@ -48,12 +48,12 @@ const callTool = (client, url, action, success, refusedWith, input, sink) => {
48
48
  const declared = result._meta?.[errorKey];
49
49
  return yield* declared === void 0 ? Effect.fail(undeclared) : failWith(projectedErrors(action), JSON.stringify(declared), undeclared);
50
50
  }
51
- if (lift.fields.length === 0 ? isText(success) : lift.otherwise !== void 0 && result.content.every((block) => block.type === "text")) {
51
+ if (lift._tag === "Plain" ? isText(success) : lift._tag === "Either" && result.content.every((block) => block.type === "text")) {
52
52
  if (firstText === void 0 || result.structuredContent !== void 0) return yield* callError(method, name, new UnexpectedContent({ body: text }));
53
53
  return yield* Schema.decodeEffect(Schema.toCodecJson(success))(firstText);
54
54
  }
55
- if (result.structuredContent === void 0 && lift.rest !== void 0) return yield* callError(method, name, new UnexpectedContent({ body: text }));
56
- return yield* Schema.decodeUnknownEffect(Schema.toCodecJson(success))(unliftedFrom(lift, result));
55
+ if ((lift._tag === "Plain" || lift._tag === "Fields" && lift.rest !== void 0) && result.structuredContent === void 0) return yield* callError(method, name, new UnexpectedContent({ body: text }));
56
+ return yield* Schema.decodeUnknownEffect(Schema.toCodecJson(success))(unliftedFrom(lift._tag === "Plain" ? [] : lift.fields, result));
57
57
  });
58
58
  };
59
59
  function client(contracts, { url = defaultPath, transformClient, authentication } = {}) {
@@ -68,12 +68,21 @@ const holdsMediaAnywhere = (ast) => {
68
68
  }
69
69
  return false;
70
70
  };
71
- const isMisplaced = (action, { rest, otherwise }) => [
72
- action.input,
73
- ...action.error,
74
- ...rest === void 0 ? [] : [rest],
75
- ...otherwise === void 0 ? [] : [otherwise]
76
- ].some(({ ast }) => holdsMediaAnywhere(ast)) || otherwise !== void 0 && !isText(otherwise);
71
+ const servedBeside = (lift) => {
72
+ switch (lift._tag) {
73
+ case "Plain": return lift.success;
74
+ case "Fields": return lift.rest;
75
+ case "Either": return lift.otherwise;
76
+ }
77
+ };
78
+ const isMisplaced = (action, lift) => {
79
+ const served = servedBeside(lift);
80
+ return [
81
+ action.input,
82
+ ...action.error,
83
+ ...served === void 0 ? [] : [served]
84
+ ].some(({ ast }) => holdsMediaAnywhere(ast)) || lift._tag === "Either" && !isText(lift.otherwise);
85
+ };
77
86
  const mediaOf = ({ name, kind, many }, result) => {
78
87
  const values = result.content.flatMap((block) => block.type === kind && block._meta?.["io.github.gjermundgaraba.effect-actions/field"] === name ? [{
79
88
  data: Base64.encode(block.data),
@@ -81,13 +90,13 @@ const mediaOf = ({ name, kind, many }, result) => {
81
90
  }] : []);
82
91
  return many || values.length > 1 ? values : values[0];
83
92
  };
84
- const unliftedFrom = (lift, result) => {
85
- const [whole] = lift.fields;
93
+ const unliftedFrom = (fields, result) => {
94
+ const [whole] = fields;
86
95
  if (whole !== void 0 && whole.name === void 0) return mediaOf(whole, result);
87
96
  const { structuredContent = {} } = result;
88
97
  if (!Predicate.isObject(structuredContent)) return structuredContent;
89
- const lifted = new Set(lift.fields.map(({ name }) => name));
90
- return Object.fromEntries([...Object.entries(structuredContent).filter(([key]) => !lifted.has(key)), ...lift.fields.flatMap((field) => {
98
+ const lifted = new Set(fields.map(({ name }) => name));
99
+ return Object.fromEntries([...Object.entries(structuredContent).filter(([key]) => !lifted.has(key)), ...fields.flatMap((field) => {
91
100
  const value = mediaOf(field, result);
92
101
  return field.name === void 0 || value === void 0 ? [] : [[field.name, value]];
93
102
  })]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjermundgaraba/effect-actions",
3
- "version": "0.41.0",
3
+ "version": "0.41.1",
4
4
  "description": "Define Effect action contracts once and project them onto HTTP, MCP, Effect RPC, native Toolkits, and CLIs.",
5
5
  "keywords": [
6
6
  "ai-agents",