@lowdefy/api 6.0.0 → 6.2.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.
Files changed (48) hide show
  1. package/dist/context/createEvaluateOperators.js +3 -1
  2. package/dist/context/getCurrentEnvironment.js +27 -0
  3. package/dist/index.js +2 -1
  4. package/dist/response/buildEndpointResult.js +3 -1
  5. package/dist/response/createWireProjection.js +88 -0
  6. package/dist/response/normalizeErrorSources.js +10 -13
  7. package/dist/response/redactErrorResponse.js +42 -8
  8. package/dist/response/redactResponse.js +10 -10
  9. package/dist/routes/agent/callAgent.js +20 -2
  10. package/dist/routes/agent/prepareAgent.js +6 -0
  11. package/dist/routes/agent/redactMessages.js +136 -0
  12. package/dist/routes/auth/defaultAuthUrl.js +31 -0
  13. package/dist/routes/auth/getAuthConfig.js +5 -1
  14. package/dist/routes/endpoints/callEndpoint.js +4 -2
  15. package/dist/routes/endpoints/control/controlFor.js +7 -11
  16. package/dist/routes/endpoints/control/controlIf.js +7 -9
  17. package/dist/routes/endpoints/control/controlLog.js +6 -14
  18. package/dist/routes/endpoints/control/controlParallelFor.js +7 -11
  19. package/dist/routes/endpoints/control/controlReject.js +13 -15
  20. package/dist/routes/endpoints/control/controlReturn.js +7 -11
  21. package/dist/routes/endpoints/control/controlSetState.js +5 -9
  22. package/dist/routes/endpoints/control/controlSwitch.js +5 -9
  23. package/dist/routes/endpoints/control/controlThrow.js +17 -19
  24. package/dist/routes/endpoints/control/controlTry.js +20 -10
  25. package/dist/routes/endpoints/evaluateRoutineOperators.js +26 -0
  26. package/dist/routes/endpoints/forwardScheduledEndpoint.js +19 -14
  27. package/dist/routes/endpoints/getEnvironmentSchedules.js +5 -7
  28. package/dist/routes/endpoints/handleAgentCall.js +5 -8
  29. package/dist/routes/endpoints/handleEndpointCall.js +5 -9
  30. package/dist/routes/endpoints/handleRenderNotification.js +15 -8
  31. package/dist/routes/endpoints/handleRequest.js +4 -7
  32. package/dist/routes/endpoints/handleValidateSchema.js +12 -10
  33. package/dist/routes/endpoints/invokeEndpoint.js +1 -0
  34. package/dist/routes/endpoints/operatorScope.js +27 -0
  35. package/dist/routes/endpoints/resolveCronEnvironment.js +7 -6
  36. package/dist/routes/endpoints/runDetachedEndpoint.js +1 -0
  37. package/dist/routes/endpoints/runScheduledEndpoint.js +1 -0
  38. package/dist/routes/endpoints/runWebhookEndpoint.js +1 -0
  39. package/dist/routes/endpoints/test/runTest.js +17 -2
  40. package/dist/routes/mcp/createMcpServer.js +20 -2
  41. package/dist/routes/request/callRequest.js +9 -3
  42. package/dist/routes/request/callRequestResolver.js +6 -0
  43. package/dist/routes/request/evaluateOperators.js +8 -12
  44. package/dist/routes/websocket/createChannelRegistry.js +19 -5
  45. package/dist/routes/websocket/createWebSocketConnection.js +15 -3
  46. package/dist/test/testContext.js +4 -2
  47. package/package.json +9 -9
  48. package/dist/response/omitErrorProps.js +0 -51
@@ -0,0 +1,27 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ // The routine fields operators can read. A field added to the routine frame goes here, so every
16
+ // routine step and control sees it rather than only the ones that remembered to pass it.
17
+ function operatorScope(routineContext) {
18
+ const { error, items, payload, state, steps } = routineContext;
19
+ return {
20
+ error: error ?? null,
21
+ items,
22
+ payload,
23
+ state,
24
+ steps
25
+ };
26
+ }
27
+ export default operatorScope;
@@ -16,22 +16,23 @@
16
16
  import { ConfigError } from '@lowdefy/errors';
17
17
  // The environment a cron run resolves its schedules for. A forwarded request names it in the
18
18
  // x-lowdefy-cron-environment header; a request without one is the deployment running its own crons,
19
- // which is the environment declared without a url (the one Vercel fires crons on). Without
20
- // config.cron there are no environments and the endpoint's plain schedules apply.
19
+ // which is the current environment (config.environment). Without config.environments there are no
20
+ // environments and the endpoint's plain schedules apply; without a current environment the
21
+ // `default` schedules apply.
21
22
  function resolveCronEnvironment({ config, environment }) {
22
- const environments = config?.cron?.environments;
23
+ const environments = config?.environments;
23
24
  if (type.isNone(environments)) {
24
25
  if (!type.isNone(environment)) {
25
- throw new ConfigError(`Cron environment "${environment}" is not configured: lowdefy.config.cron.environments is not defined.`);
26
+ throw new ConfigError(`Cron environment "${environment}" is not configured: config.environments is not defined.`);
26
27
  }
27
28
  return undefined;
28
29
  }
29
30
  if (!type.isNone(environment)) {
30
31
  if (!type.isObject(environments[environment])) {
31
- throw new ConfigError(`Cron environment "${environment}" is not declared in lowdefy.config.cron.environments.`);
32
+ throw new ConfigError(`Cron environment "${environment}" is not declared in config.environments.`);
32
33
  }
33
34
  return environment;
34
35
  }
35
- return Object.keys(environments).find((name)=>type.isObject(environments[name]) && type.isUndefined(environments[name].url));
36
+ return config.environment;
36
37
  }
37
38
  export default resolveCronEnvironment;
@@ -50,6 +50,7 @@ async function runDetachedEndpoint(context, { endpointId, payload }) {
50
50
  steps: {},
51
51
  payload: serializer.deserialize(payload ?? {}),
52
52
  arrayIndices: [],
53
+ error: null,
53
54
  items: {},
54
55
  state: {},
55
56
  endpointDepth: 0
@@ -70,6 +70,7 @@ async function runScheduledEndpoint(context, { endpointId, cron, environment })
70
70
  steps: {},
71
71
  payload: schedule.payload ?? {},
72
72
  arrayIndices: [],
73
+ error: null,
73
74
  items: {},
74
75
  state: {},
75
76
  endpointDepth: 0
@@ -67,6 +67,7 @@ async function runWebhookEndpoint(context, { endpointId, body, query, headers })
67
67
  headers: headers ?? {}
68
68
  },
69
69
  arrayIndices: [],
70
+ error: null,
70
71
  items: {},
71
72
  state: {},
72
73
  endpointDepth: 0
@@ -1,4 +1,18 @@
1
- import { jest } from '@jest/globals';
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { jest } from '@jest/globals';
2
16
  import { wait } from '@lowdefy/helpers';
3
17
  import { operatorsServer } from '@lowdefy/operators-js';
4
18
  import createEvaluateOperators from '../../../context/createEvaluateOperators.js';
@@ -7,7 +21,7 @@ import testContext from '../../../test/testContext.js';
7
21
  // const { _date, _eq, _payload, _secret, _user } = operatorsServer;
8
22
  const operators = {
9
23
  ...operatorsServer,
10
- _error: ()=>{
24
+ _throw_test: ()=>{
11
25
  throw new Error('Test error.');
12
26
  }
13
27
  };
@@ -89,6 +103,7 @@ async function runTest({ routine, payload = {} }) {
89
103
  const routineContext = {
90
104
  steps: {},
91
105
  payload,
106
+ error: null,
92
107
  items: {},
93
108
  arrayIndices: [],
94
109
  state: {},
@@ -17,6 +17,7 @@ import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprot
17
17
  import { AuthenticationError } from '@lowdefy/errors';
18
18
  import { serializer, type } from '@lowdefy/helpers';
19
19
  import callEndpoint from '../endpoints/callEndpoint.js';
20
+ import createWireProjection from '../../response/createWireProjection.js';
20
21
  import isUnauthenticatedHuman from '../endpoints/isUnauthenticatedHuman.js';
21
22
  // LLM-safe tool names use the same rule as buildAgents tool naming.
22
23
  function toToolName(id) {
@@ -102,11 +103,25 @@ async function createMcpServer({ context }) {
102
103
  });
103
104
  if (!success) {
104
105
  const deserialized = serializer.deserialize(error);
106
+ let text = deserialized?.message ?? 'Endpoint failed.';
107
+ // The dev MCP route serves a coding agent, which is a dev tool: it gets the real
108
+ // message and the config location so it can go straight to the YAML. A prod MCP
109
+ // client is an end-user reader and keeps the wire message.
110
+ if (context.mode === 'dev' && !type.isNone(error?.devError)) {
111
+ const devError = serializer.deserialize(error.devError);
112
+ text = devError.message;
113
+ if (!type.isNone(devError.source)) {
114
+ text = `${text} (at ${devError.source})`;
115
+ }
116
+ if (!type.isNone(devError.hint)) {
117
+ text = `${text} Hint: ${devError.hint}`;
118
+ }
119
+ }
105
120
  return {
106
121
  content: [
107
122
  {
108
123
  type: 'text',
109
- text: deserialized?.message ?? 'Endpoint failed.'
124
+ text
110
125
  }
111
126
  ],
112
127
  isError: true
@@ -144,11 +159,14 @@ async function createMcpServer({ context }) {
144
159
  } else {
145
160
  context.logger.error(error);
146
161
  }
162
+ // This error never went through an endpoint result, so it has not been projected yet.
163
+ // The projection keeps an AuthenticationError's message.
164
+ const text = context.mode === 'dev' ? error.message : createWireProjection(context)(error).message;
147
165
  return {
148
166
  content: [
149
167
  {
150
168
  type: 'text',
151
- text: error.message
169
+ text
152
170
  }
153
171
  ],
154
172
  isError: true
@@ -59,10 +59,16 @@ async function callRequest(context, { blockId, pageId, payload, requestId }) {
59
59
  });
60
60
  const { connectionProperties, requestProperties } = evaluateOperators(context, {
61
61
  connectionConfig,
62
- payload: requestPayload,
63
62
  requestConfig,
64
- state: {},
65
- steps: {}
63
+ // A page request runs outside any routine, so it evaluates against an empty frame.
64
+ routineContext: {
65
+ arrayIndices: [],
66
+ error: null,
67
+ items: {},
68
+ payload: requestPayload,
69
+ state: {},
70
+ steps: {}
71
+ }
66
72
  });
67
73
  checkConnectionRead(context, {
68
74
  connectionConfig,
@@ -13,6 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { RequestError, ServiceError } from '@lowdefy/errors';
16
+ import getCurrentEnvironment from '../../context/getCurrentEnvironment.js';
16
17
  import invokeEndpoint from '../endpoints/invokeEndpoint.js';
17
18
  async function callRequestResolver(context, { connectionProperties, endpointDepth, requestConfig, requestProperties, requestResolver }) {
18
19
  const { blockId, endpointId, logger, pageId, payload } = context;
@@ -49,6 +50,11 @@ async function callRequestResolver(context, { connectionProperties, endpointDept
49
50
  connection: connectionProperties,
50
51
  connectionId: requestConfig.connectionId,
51
52
  endpointId,
53
+ // The deployment's current environment ({ name, url, email, ... }) or null, so connection
54
+ // types can apply environment settings such as the email delivery filter.
55
+ environment: getCurrentEnvironment({
56
+ config: context.config
57
+ }),
52
58
  pageId,
53
59
  payload,
54
60
  request: requestProperties,
@@ -12,21 +12,17 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ function evaluateOperators({ evaluateOperators }, { connectionConfig, items, payload, requestConfig, state, steps }) {
16
- const connectionProperties = evaluateOperators({
15
+ */ import evaluateRoutineOperators from '../endpoints/evaluateRoutineOperators.js';
16
+ // Connection and request properties evaluate against the same frame, so `_item` in a connection's
17
+ // properties resolves inside a `:for` loop just like it does in the request's.
18
+ function evaluateOperators(context, { connectionConfig, requestConfig, routineContext }) {
19
+ const connectionProperties = evaluateRoutineOperators(context, routineContext, {
17
20
  input: connectionConfig.properties || {},
18
- location: connectionConfig.connectionId,
19
- payload,
20
- state,
21
- steps
21
+ location: connectionConfig.connectionId
22
22
  });
23
- const requestProperties = evaluateOperators({
23
+ const requestProperties = evaluateRoutineOperators(context, routineContext, {
24
24
  input: requestConfig.properties || {},
25
- items,
26
- location: requestConfig.stepId ?? requestConfig.requestId,
27
- payload,
28
- state,
29
- steps
25
+ location: requestConfig.stepId ?? requestConfig.requestId
30
26
  });
31
27
  return {
32
28
  connectionProperties,
@@ -15,6 +15,7 @@
15
15
  */ import crypto from 'crypto';
16
16
  import { serializer } from '@lowdefy/helpers';
17
17
  import { ConfigError, PluginError, ServiceError } from '@lowdefy/errors';
18
+ import createWireProjection from '../../response/createWireProjection.js';
18
19
  import prepareChannel from './prepareChannel.js';
19
20
  const MAX_RETRIES = 5;
20
21
  const RETRY_BASE_MS = 1000;
@@ -39,6 +40,23 @@ function createChannelRegistry() {
39
40
  subscriber.send(message);
40
41
  });
41
42
  }
43
+ // Config reads a broadcast error only as a message string, so it gets the wire
44
+ // message alone - no requestId, since no single request raised it, and no
45
+ // devError, since the dev terminal already logs it in full. The channel's
46
+ // context belongs to whoever subscribed first, so the message is translated
47
+ // with each subscriber's own locale.
48
+ function broadcastError(channel, error) {
49
+ channel.subscribers.forEach((subscriber)=>{
50
+ const { message } = createWireProjection({
51
+ i18n: subscriber.i18n
52
+ })(error);
53
+ subscriber.send(JSON.stringify({
54
+ type: 'error',
55
+ websocketId: channel.websocketId,
56
+ message
57
+ }));
58
+ });
59
+ }
42
60
  function wrapResolverError({ channel, error }) {
43
61
  if (!error.configKey) {
44
62
  error.configKey = channel.websocketConfig['~k'];
@@ -86,11 +104,7 @@ function createChannelRegistry() {
86
104
  err: wrapped
87
105
  }, wrapped.message);
88
106
  channel.context.handleError(wrapped);
89
- broadcast(channel, {
90
- type: 'error',
91
- websocketId: channel.websocketId,
92
- message: wrapped.message
93
- });
107
+ broadcastError(channel, wrapped);
94
108
  // A run that stayed healthy resets the backoff window.
95
109
  if (Date.now() - startedAt > HEALTHY_RESET_MS) {
96
110
  channel.retryCount = 0;
@@ -13,6 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { type } from '@lowdefy/helpers';
16
+ import redactErrorResponse from '../../response/redactErrorResponse.js';
16
17
  // Wraps one client websocket connection: parses frames, dispatches
17
18
  // subscribe/unsubscribe/publish to the channel registry, and answers every
18
19
  // frame with an ack or an error so client actions never hang.
@@ -21,7 +22,10 @@ function createWebSocketConnection(context, { registry, send }) {
21
22
  const subscriber = {
22
23
  id: context.rid,
23
24
  subscriptions: new Map(),
24
- send
25
+ send,
26
+ // A shared channel broadcasts to every subscriber, so each one carries its
27
+ // own locale for the error message it is sent.
28
+ i18n: context.i18n
25
29
  };
26
30
  function sendError({ message, requestId, websocketId }) {
27
31
  send(JSON.stringify({
@@ -31,6 +35,14 @@ function createWebSocketConnection(context, { registry, send }) {
31
35
  message
32
36
  }));
33
37
  }
38
+ function sendErrorPayload({ error, requestId, websocketId }) {
39
+ send(JSON.stringify({
40
+ type: 'error',
41
+ websocketId,
42
+ requestId,
43
+ error: redactErrorResponse(context, error)
44
+ }));
45
+ }
34
46
  async function handleFrame(frame) {
35
47
  const { payload, requestId, websocketId } = frame;
36
48
  if (!type.isString(websocketId)) {
@@ -104,8 +116,8 @@ function createWebSocketConnection(context, { registry, send }) {
104
116
  err: error
105
117
  }, error.message);
106
118
  context.handleError(error);
107
- sendError({
108
- message: error.message,
119
+ sendErrorPayload({
120
+ error,
109
121
  requestId: frame.requestId,
110
122
  websocketId: frame.websocketId
111
123
  });
@@ -18,9 +18,9 @@ function testContext({ appMeta = {}, config = {}, configDirectory, connections =
18
18
  error: ()=>{},
19
19
  info: ()=>{},
20
20
  warn: ()=>{}
21
- }, operators = {
21
+ }, mode = 'prod', operators = {
22
22
  _test: ()=>'test'
23
- }, readConfigFile, secrets = {}, session, system } = {}) {
23
+ }, readConfigFile, scrubSecrets = (value)=>value, secrets = {}, session, system } = {}) {
24
24
  return {
25
25
  appMeta,
26
26
  authorize: createAuthorize({
@@ -39,8 +39,10 @@ function testContext({ appMeta = {}, config = {}, configDirectory, connections =
39
39
  },
40
40
  headers,
41
41
  logger,
42
+ mode,
42
43
  operators,
43
44
  readConfigFile,
45
+ scrubSecrets,
44
46
  secrets,
45
47
  session,
46
48
  steps: {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/api",
3
- "version": "6.0.0",
3
+ "version": "6.2.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -34,14 +34,14 @@
34
34
  "dist/*"
35
35
  ],
36
36
  "dependencies": {
37
- "@lowdefy/ajv": "6.0.0",
38
- "@lowdefy/build": "6.0.0",
39
- "@lowdefy/errors": "6.0.0",
40
- "@lowdefy/helpers": "6.0.0",
41
- "@lowdefy/node-utils": "6.0.0",
42
- "@lowdefy/nunjucks": "6.0.0",
43
- "@lowdefy/operators": "6.0.0",
44
- "@lowdefy/operators-js": "6.0.0",
37
+ "@lowdefy/ajv": "6.2.0",
38
+ "@lowdefy/build": "6.2.0",
39
+ "@lowdefy/errors": "6.2.0",
40
+ "@lowdefy/helpers": "6.2.0",
41
+ "@lowdefy/node-utils": "6.2.0",
42
+ "@lowdefy/nunjucks": "6.2.0",
43
+ "@lowdefy/operators": "6.2.0",
44
+ "@lowdefy/operators-js": "6.2.0",
45
45
  "@modelcontextprotocol/sdk": "1.29.0"
46
46
  },
47
47
  "devDependencies": {
@@ -1,51 +0,0 @@
1
- /*
2
- Copyright 2020-2026 Lowdefy, Inc
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */ import { UserError } from '@lowdefy/errors';
16
- import { type } from '@lowdefy/helpers';
17
- // The client-bound error policy: which fields of an error may cross the wire.
18
- // Passed to serializer.serialize as `omitErrorProps`, so extractErrorProps applies
19
- // it at EVERY error node the walk emits - the cause chain, an Error-valued own
20
- // property, and an Error nested inside a plain object or array. The policy is
21
- // stated against the emitter rather than against a response shape, so it cannot
22
- // become depth-limited.
23
- //
24
- // `received` is not merely "may be sensitive": on the request path
25
- // callRequestResolver sets it to the EVALUATED request properties, so a _secret
26
- // resolved into a request header is in it. `stack` exposes server internals,
27
- // including absolute node_modules paths. Both are unbounded runtime data nobody
28
- // chose, in every environment - the full value stays in the server log, which is
29
- // where a developer on their own machine reads it.
30
- const ALWAYS_OMITTED = [
31
- 'received',
32
- 'stack'
33
- ];
34
- const OMITTED_WITH_CAUSE = [
35
- ...ALWAYS_OMITTED,
36
- 'cause'
37
- ];
38
- function omitErrorProps(error) {
39
- // An Error cause is the trace the browser renders (name + message per level),
40
- // so it is always kept - fields are taken from causes, causes are never pruned.
41
- // A non-Error cause is internal server config: the whole endpoint routine, a
42
- // control node, ajv errors. UserError is the exception, the one class whose
43
- // payload the author wrote for the client.
44
- //
45
- // type.isError is `instanceof Error`, the same test extractErrorProps uses to
46
- // decide which branch emits the cause - the two must agree.
47
- if (type.isError(error.cause)) return ALWAYS_OMITTED;
48
- if (error instanceof UserError) return ALWAYS_OMITTED;
49
- return OMITTED_WITH_CAUSE;
50
- }
51
- export default omitErrorProps;