@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
@@ -14,28 +14,20 @@
14
14
  limitations under the License.
15
15
  */ import { type } from '@lowdefy/helpers';
16
16
  import { ConfigError } from '@lowdefy/errors';
17
+ import evaluateRoutineOperators from '../evaluateRoutineOperators.js';
17
18
  async function controlLog(context, routineContext, { control }) {
18
- const { endpointId, logger, evaluateOperators } = context;
19
- const { items } = routineContext;
19
+ const { endpointId, logger } = context;
20
20
  const location = control['~k'] ?? ':log';
21
21
  logger.debug({
22
22
  event: 'debug_control_log'
23
23
  });
24
- const log = evaluateOperators({
24
+ const log = evaluateRoutineOperators(context, routineContext, {
25
25
  input: control[':log'],
26
- items,
27
- location,
28
- payload: routineContext.payload,
29
- state: routineContext.state,
30
- steps: routineContext.steps
26
+ location
31
27
  });
32
- const logLevel = evaluateOperators({
28
+ const logLevel = evaluateRoutineOperators(context, routineContext, {
33
29
  input: control[':level'],
34
- items,
35
- location,
36
- payload: routineContext.payload,
37
- state: routineContext.state,
38
- steps: routineContext.steps
30
+ location
39
31
  }) ?? 'info';
40
32
  if (!type.isString(logLevel)) {
41
33
  throw new ConfigError(`Invalid :log in endpoint "${endpointId}" - :level must be a string.`, {
@@ -14,24 +14,21 @@
14
14
  limitations under the License.
15
15
  */ import { ConfigError } from '@lowdefy/errors';
16
16
  import runRoutine from '../runRoutine.js';
17
+ import evaluateRoutineOperators from '../evaluateRoutineOperators.js';
17
18
  async function controlParallelFor(context, routineContext, { control }) {
18
- const { endpointId, logger, evaluateOperators } = context;
19
+ const { endpointId, logger } = context;
19
20
  const { items } = routineContext;
20
21
  const itemName = control[':parallel_for'];
21
22
  if (!itemName) {
22
23
  throw new Error(`Invalid :parallel_for in endpoint "${endpointId}" - missing variable name in :parallel_for.`);
23
24
  }
24
- const array = evaluateOperators({
25
+ const array = evaluateRoutineOperators(context, routineContext, {
25
26
  input: control[':in'],
26
- items,
27
- location: control['~k'] ?? ':parallel_for',
28
- payload: routineContext.payload,
29
- state: routineContext.state,
30
- steps: routineContext.steps
27
+ location: control['~k'] ?? ':parallel_for'
31
28
  });
32
29
  logger.debug({
33
30
  event: 'debug_control_parallel',
34
- array,
31
+ itemCount: Array.isArray(array) ? array.length : null,
35
32
  itemName
36
33
  });
37
34
  if (!Array.isArray(array)) {
@@ -50,9 +47,8 @@ async function controlParallelFor(context, routineContext, { control }) {
50
47
  };
51
48
  logger.debug({
52
49
  event: 'debug_control_parallel_iteration',
53
- itemName: itemName,
54
- value: item,
55
- items: updatedItems
50
+ itemName,
51
+ index
56
52
  });
57
53
  return runRoutine(context, {
58
54
  ...routineContext,
@@ -13,27 +13,25 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { UserError } from '@lowdefy/errors';
16
+ import { type } from '@lowdefy/helpers';
17
+ import createWireProjection from '../../../response/createWireProjection.js';
18
+ import evaluateRoutineOperators from '../evaluateRoutineOperators.js';
16
19
  async function controlReject(context, routineContext, { control }) {
17
- const { evaluateOperators } = context;
18
- const { items } = routineContext;
19
20
  const location = control['~k'] ?? ':reject';
20
- const message = evaluateOperators({
21
+ const message = evaluateRoutineOperators(context, routineContext, {
21
22
  input: control[':reject'],
22
- items,
23
- location,
24
- payload: routineContext.payload,
25
- state: routineContext.state,
26
- steps: routineContext.steps
23
+ location
27
24
  });
28
- const cause = evaluateOperators({
25
+ const cause = evaluateRoutineOperators(context, routineContext, {
29
26
  input: control[':cause'],
30
- items,
31
- location,
32
- payload: routineContext.payload,
33
- state: routineContext.state,
34
- steps: routineContext.steps
27
+ location
35
28
  });
36
- const error = new UserError(message, {
29
+ // A reject is the author telling the user something, so it stays a UserError, whose message the
30
+ // wire passes through as written. An Error given as the message is reduced to what the wire
31
+ // would show for it - the author's text for a UserError, the generic message for a plugin error
32
+ // - rather than stringified with its library text; `{ _error: message }` opts into the real text.
33
+ const text = type.isError(message) ? createWireProjection(context)(message).message : message;
34
+ const error = new UserError(text, {
37
35
  cause,
38
36
  isReject: true
39
37
  });
@@ -12,20 +12,16 @@
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
- */ async function controlReturn(context, routineContext, { control }) {
16
- const { evaluateOperators } = context;
17
- const { items } = routineContext;
18
- const response = evaluateOperators({
15
+ */ import evaluateRoutineOperators from '../evaluateRoutineOperators.js';
16
+ async function controlReturn(context, routineContext, { control }) {
17
+ const response = evaluateRoutineOperators(context, routineContext, {
19
18
  input: control[':return'],
20
- items,
21
- location: control['~k'] ?? ':return',
22
- payload: routineContext.payload,
23
- state: routineContext.state,
24
- steps: routineContext.steps
19
+ location: control['~k'] ?? ':return'
25
20
  });
21
+ // No evaluated values: they can carry chat text or other personal information,
22
+ // which no log level may hold.
26
23
  context.logger.debug({
27
- event: 'debug_control_return',
28
- response
24
+ event: 'debug_control_return'
29
25
  });
30
26
  return {
31
27
  status: 'return',
@@ -14,21 +14,17 @@
14
14
  limitations under the License.
15
15
  */ import { ConfigError } from '@lowdefy/errors';
16
16
  import { ReservedKeyError, set } from '@lowdefy/helpers';
17
+ import evaluateRoutineOperators from '../evaluateRoutineOperators.js';
17
18
  function controlSetState(context, routineContext, { control }) {
18
- const { logger, evaluateOperators } = context;
19
- const { items } = routineContext;
20
- const evaluatedSetState = evaluateOperators({
19
+ const { logger } = context;
20
+ const evaluatedSetState = evaluateRoutineOperators(context, routineContext, {
21
21
  input: control[':set_state'],
22
- items,
23
- location: control['~k'] ?? ':set_state',
24
- payload: routineContext.payload,
25
- state: routineContext.state,
26
- steps: routineContext.steps
22
+ location: control['~k'] ?? ':set_state'
27
23
  });
28
24
  logger.debug({
29
25
  event: 'debug_control_set_state',
30
26
  input: control[':set_state'],
31
- evaluated: evaluatedSetState
27
+ keys: Object.keys(evaluatedSetState ?? {})
32
28
  });
33
29
  Object.entries(evaluatedSetState).forEach(([key, value])=>{
34
30
  try {
@@ -13,27 +13,23 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import runRoutine from '../runRoutine.js';
16
+ import evaluateRoutineOperators from '../evaluateRoutineOperators.js';
16
17
  async function controlSwitch(context, routineContext, { control }) {
17
- const { endpointId, logger, evaluateOperators } = context;
18
- const { items } = routineContext;
18
+ const { endpointId, logger } = context;
19
19
  const cases = control[':switch'];
20
20
  logger.debug({
21
21
  event: 'debug_control_switch'
22
22
  });
23
23
  for (const caseObj of cases){
24
- const evaluatedCase = evaluateOperators({
24
+ const evaluatedCase = evaluateRoutineOperators(context, routineContext, {
25
25
  input: caseObj[':case'],
26
- items,
27
- location: caseObj['~k'] ?? control['~k'] ?? ':switch',
28
- payload: routineContext.payload,
29
- state: routineContext.state,
30
- steps: routineContext.steps
26
+ location: caseObj['~k'] ?? control['~k'] ?? ':switch'
31
27
  });
32
28
  logger.debug({
33
29
  event: 'debug_control_switch_case',
34
30
  case: {
35
31
  input: caseObj[':case'],
36
- evaluated: evaluatedCase
32
+ result: Boolean(evaluatedCase)
37
33
  }
38
34
  });
39
35
  if (evaluatedCase) {
@@ -13,29 +13,27 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { UserError } from '@lowdefy/errors';
16
+ import { type } from '@lowdefy/helpers';
17
+ import evaluateRoutineOperators from '../evaluateRoutineOperators.js';
16
18
  async function controlThrow(context, routineContext, { control }) {
17
- const { evaluateOperators } = context;
18
- const { items } = routineContext;
19
19
  const location = control['~k'] ?? ':throw';
20
- const message = evaluateOperators({
20
+ const message = evaluateRoutineOperators(context, routineContext, {
21
21
  input: control[':throw'],
22
- items,
23
- location,
24
- payload: routineContext.payload,
25
- state: routineContext.state,
26
- steps: routineContext.steps
27
- });
28
- const cause = evaluateOperators({
29
- input: control[':cause'],
30
- items,
31
- location,
32
- payload: routineContext.payload,
33
- state: routineContext.state,
34
- steps: routineContext.steps
35
- });
36
- const error = new UserError(message, {
37
- cause
22
+ location
38
23
  });
24
+ // An Error, such as a rethrown `_error`, goes on unchanged. Wrapped in a UserError it would be
25
+ // stringified into the message, which the wire sends as author text; under its own class the
26
+ // wire makes it generic unless it was a UserError. It carries its own cause, so :cause is unused.
27
+ let error = message;
28
+ if (!type.isError(message)) {
29
+ const cause = evaluateRoutineOperators(context, routineContext, {
30
+ input: control[':cause'],
31
+ location
32
+ });
33
+ error = new UserError(message, {
34
+ cause
35
+ });
36
+ }
39
37
  // Log under `err` — the pino error serializer (createNodeLogger) is registered
40
38
  // for the `err` key only; an Error passed as `error` is JSON-dumped without its
41
39
  // non-enumerable `message`/`stack`, producing a log line with no message.
@@ -12,7 +12,8 @@
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
- */ import runRoutine from '../runRoutine.js';
15
+ */ import { projectCaughtError } from '@lowdefy/helpers';
16
+ import runRoutine from '../runRoutine.js';
16
17
  async function controlTry(context, routineContext, { control }) {
17
18
  context.logger.debug({
18
19
  event: 'debug_control_try'
@@ -20,15 +21,24 @@ async function controlTry(context, routineContext, { control }) {
20
21
  let res = await runRoutine(context, routineContext, {
21
22
  routine: control[':try']
22
23
  });
23
- if (res.status === 'error') {
24
- if (control[':catch']) {
25
- context.logger.debug({
26
- event: 'debug_control_catch'
27
- });
28
- res = await runRoutine(context, routineContext, {
29
- routine: control[':catch']
30
- });
31
- }
24
+ if (res.status === 'error' && control[':catch']) {
25
+ context.logger.debug({
26
+ event: 'debug_control_catch'
27
+ });
28
+ // A child context rather than a field set on routineContext: the same object is handed to
29
+ // :try, :finally and every :parallel branch, so writing to it would let an inner :try clobber
30
+ // the outer catch's error and parallel branches read each other's. steps and state are still
31
+ // shared because routines mutate them in place. The projection is built once here, never the
32
+ // raw error, so no operator in the catch can read its received value or source.
33
+ const error = projectCaughtError(res.error, {
34
+ scrub: context.scrubSecrets
35
+ });
36
+ res = await runRoutine(context, {
37
+ ...routineContext,
38
+ error
39
+ }, {
40
+ routine: control[':catch']
41
+ });
32
42
  }
33
43
  if (control[':finally']) {
34
44
  context.logger.debug({
@@ -0,0 +1,26 @@
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 operatorScope from './operatorScope.js';
16
+ // Every routine step evaluates against the same frame. Taking the frame from routineContext in one
17
+ // place means a step handler cannot drop part of it (state, items, loop indices).
18
+ function evaluateRoutineOperators(context, routineContext, { input, location }) {
19
+ return context.evaluateOperators({
20
+ arrayIndices: routineContext.arrayIndices,
21
+ input,
22
+ location,
23
+ ...operatorScope(routineContext)
24
+ });
25
+ }
26
+ export default evaluateRoutineOperators;
@@ -18,29 +18,34 @@ import findSchedule from './findSchedule.js';
18
18
  import getEndpointConfig from './getEndpointConfig.js';
19
19
  import getEnvironmentSchedules from './getEnvironmentSchedules.js';
20
20
  import scheduleBackground from './scheduleBackground.js';
21
- // Vercel fires cron jobs only on the production deployment, so the schedules of every other
22
- // environment are registered there as /api/cron-forward/<environment>/<endpointId> jobs. When one
23
- // fires, this pings the environment's own /api/cron/<endpointId> with that environment's
24
- // CRON_SECRET (a Lowdefy secret named in config.cron.environments) so the environment runs its own
25
- // code, and answers Vercel immediately: the ping is fire-and-forget, kept alive by scheduleBackground
26
- // and bounded by the function duration, and its outcome exists only in the logs.
21
+ // Vercel fires cron jobs only on the production deployment, so the schedules of every environment
22
+ // with a cron.secret are registered there as /api/cron-forward/<environment>/<endpointId> jobs. When
23
+ // one fires, this pings the environment's own /api/cron/<endpointId> at its url with that
24
+ // environment's CRON_SECRET (the Lowdefy secret named by config.environments.<env>.cron.secret) so
25
+ // the environment runs its own code, and answers Vercel immediately: the ping is fire-and-forget,
26
+ // kept alive by scheduleBackground and bounded by the function duration, and its outcome exists only
27
+ // in the logs.
27
28
  async function forwardScheduledEndpoint(context, { environment, endpointId, cron }) {
28
29
  const { config, logger, secrets } = context;
29
- const target = config?.cron?.environments?.[environment];
30
+ const target = config?.environments?.[environment];
30
31
  if (!type.isObject(target)) {
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
- if (type.isUndefined(target.url)) {
34
- throw new ConfigError(`Cron environment "${environment}" has no url to forward to: it is the environment that runs the crons.`);
34
+ if (environment === config.environment) {
35
+ throw new ConfigError(`Cron environment "${environment}" is this deployment's own environment: its crons run here, not forwarded.`);
35
36
  }
36
- if (target.enabled === false) {
37
+ if (target.cron?.enabled === false) {
37
38
  throw new ConfigError(`Cron environment "${environment}" is disabled.`);
38
39
  }
39
- const secret = secrets?.[target.secret];
40
+ const secretName = target.cron?.secret;
41
+ if (type.isUndefined(secretName)) {
42
+ throw new ConfigError(`Cron environment "${environment}" has no cron.secret: crons are only forwarded to environments with one.`);
43
+ }
44
+ const secret = secrets?.[secretName];
40
45
  if (!type.isString(secret) || secret === '') {
41
- throw new ConfigError(`Secret "${target.secret}" holding the CRON_SECRET of cron environment "${environment}" is not set. Set the LOWDEFY_SECRET_${target.secret} environment variable on this deployment.`);
46
+ throw new ConfigError(`Secret "${secretName}" holding the CRON_SECRET of cron environment "${environment}" is not set. Set the LOWDEFY_SECRET_${secretName} environment variable on this deployment.`);
42
47
  }
43
- // The same build is deployed to every environment, so the local artifact says whether the target
48
+ // Every environment builds from the same config, so the local artifact says whether the target
44
49
  // declares the firing schedule: fail here with a config error instead of having the target 500.
45
50
  const endpointConfig = await getEndpointConfig(context, {
46
51
  endpointId
@@ -13,17 +13,15 @@
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
- // With config.cron.environments declared the build resolves `schedules` onto every environment
17
- // (schedules.<name>, defaults inherited), so an environment run reads its own list; without
18
- // environments the endpoint carries a plain array.
16
+ // With config.environments declared the build resolves `schedules` onto every environment
17
+ // (schedules.<name>, defaults inherited, plus schedules.default), so an environment run reads its
18
+ // own list and a deployment with no current environment reads the default; without environments
19
+ // the endpoint carries a plain array.
19
20
  function getEnvironmentSchedules({ endpointConfig, environment }) {
20
21
  const schedules = endpointConfig.schedules;
21
22
  if (type.isArray(schedules)) {
22
23
  return schedules;
23
24
  }
24
- if (environment === undefined) {
25
- return [];
26
- }
27
- return schedules?.[environment] ?? [];
25
+ return schedules?.[environment ?? 'default'] ?? [];
28
26
  }
29
27
  export default getEnvironmentSchedules;
@@ -16,20 +16,17 @@
16
16
  import { type } from '@lowdefy/helpers';
17
17
  import addStepResult from './addStepResult.js';
18
18
  import prepareAgent from '../agent/prepareAgent.js';
19
+ import evaluateRoutineOperators from './evaluateRoutineOperators.js';
19
20
  async function handleAgentCall(context, routineContext, { step }) {
20
- const { logger, evaluateOperators } = context;
21
+ const { logger } = context;
21
22
  logger.debug({
22
23
  event: 'debug_start_agent_call',
23
24
  step
24
25
  });
25
26
  // Evaluate operators in step.properties (resolves agentId, prompt)
26
- const evaluatedProperties = evaluateOperators({
27
+ const evaluatedProperties = evaluateRoutineOperators(context, routineContext, {
27
28
  input: step.properties,
28
- items: routineContext.items,
29
- location: step.stepId,
30
- payload: routineContext.payload,
31
- state: routineContext.state,
32
- steps: routineContext.steps
29
+ location: step.stepId
33
30
  });
34
31
  const { agentId, prompt } = evaluatedProperties;
35
32
  if (!type.isString(agentId)) {
@@ -38,7 +35,7 @@ async function handleAgentCall(context, routineContext, { step }) {
38
35
  });
39
36
  }
40
37
  if (!type.isString(prompt)) {
41
- throw new ConfigError(`CallAgent step "${step.stepId}" properties.prompt must evaluate to a string. Received ${JSON.stringify(prompt)}.`, {
38
+ throw new ConfigError(`CallAgent step "${step.stepId}" properties.prompt must evaluate to a string. Received a value of type "${prompt === null ? 'null' : typeof prompt}".`, {
42
39
  configKey: step['~k']
43
40
  });
44
41
  }
@@ -17,20 +17,17 @@ import { ConfigError } from '@lowdefy/errors';
17
17
  import addStepResult from './addStepResult.js';
18
18
  import invokeEndpoint from './invokeEndpoint.js';
19
19
  import scheduleBackground from './scheduleBackground.js';
20
+ import evaluateRoutineOperators from './evaluateRoutineOperators.js';
20
21
  async function handleEndpointCall(context, routineContext, { step }) {
21
- const { logger, evaluateOperators } = context;
22
+ const { logger } = context;
22
23
  logger.debug({
23
24
  event: 'debug_start_endpoint_call',
24
25
  step
25
26
  });
26
27
  // Evaluate operators in step.properties (resolves endpointId, payload)
27
- const evaluatedProperties = evaluateOperators({
28
+ const evaluatedProperties = evaluateRoutineOperators(context, routineContext, {
28
29
  input: step.properties,
29
- items: routineContext.items,
30
- location: step.stepId,
31
- payload: routineContext.payload,
32
- state: routineContext.state,
33
- steps: routineContext.steps
30
+ location: step.stepId
34
31
  });
35
32
  // detached: true — fire-and-forget the call back through the deployment's
36
33
  // /api/detached route, so the target runs in its OWN function invocation
@@ -95,8 +92,7 @@ async function handleEndpointCall(context, routineContext, { step }) {
95
92
  logger.debug({
96
93
  event: 'debug_end_endpoint_call',
97
94
  stepId: step.stepId,
98
- targetEndpointId: evaluatedProperties.endpointId,
99
- response
95
+ targetEndpointId: evaluatedProperties.endpointId
100
96
  });
101
97
  return {
102
98
  status: 'continue'
@@ -16,10 +16,12 @@
16
16
  import { ConfigError } from '@lowdefy/errors';
17
17
  import { type } from '@lowdefy/helpers';
18
18
  import addStepResult from './addStepResult.js';
19
+ import getCurrentEnvironment from '../../context/getCurrentEnvironment.js';
19
20
  import derivePreview from '../notifications/derivePreview.js';
20
21
  import getNotificationConfig from '../notifications/getNotificationConfig.js';
21
22
  import resolveNotificationLinks from '../notifications/resolveNotificationLinks.js';
22
23
  import resolveThemeLogo from '../notifications/resolveThemeLogo.js';
24
+ import evaluateRoutineOperators from './evaluateRoutineOperators.js';
23
25
  function itemHasPageLinks(item, dataKeys) {
24
26
  const links = Object.values(item.links ?? {});
25
27
  const arrayLinks = dataKeys.flatMap((key)=>(type.isArray(item[key]) ? item[key] : []).map((entry)=>entry?.link));
@@ -29,18 +31,14 @@ function itemHasPageLinks(item, dataKeys) {
29
31
  ].some((link)=>type.isObject(link) && !type.isNone(link.pageId));
30
32
  }
31
33
  async function handleRenderNotification(context, routineContext, { step }) {
32
- const { logger, evaluateOperators } = context;
34
+ const { logger } = context;
33
35
  logger.debug({
34
36
  event: 'debug_start_render_notification',
35
37
  step
36
38
  });
37
- const evaluatedProperties = evaluateOperators({
39
+ const evaluatedProperties = evaluateRoutineOperators(context, routineContext, {
38
40
  input: step.properties,
39
- items: routineContext.items,
40
- location: step.stepId,
41
- payload: routineContext.payload,
42
- state: routineContext.state,
43
- steps: routineContext.steps
41
+ location: step.stepId
44
42
  });
45
43
  const { notificationId, data, landingPage, recordId } = evaluatedProperties;
46
44
  let { serverUrl } = evaluatedProperties;
@@ -65,6 +63,15 @@ async function handleRenderNotification(context, routineContext, { step }) {
65
63
  configKey: step['~k']
66
64
  });
67
65
  }
66
+ } else {
67
+ // The current environment's url is the deployment origin, so links need no per-step wiring.
68
+ // On the dev server the request origin is the local app; in production it is never used, since
69
+ // a spoofed Host header would then steer the links in outgoing mail.
70
+ serverUrl = getCurrentEnvironment({
71
+ config: context.config
72
+ })?.url ?? (context.dev === true ? context.origin : undefined);
73
+ }
74
+ if (!type.isNone(serverUrl)) {
68
75
  serverUrl = serverUrl.replace(/\/$/, '');
69
76
  }
70
77
  if (!type.isNone(landingPage) && !type.isString(landingPage)) {
@@ -111,7 +118,7 @@ async function handleRenderNotification(context, routineContext, { step }) {
111
118
  });
112
119
  if (itemHasPageLinks(data, Template.dataKeys ?? [])) {
113
120
  if (type.isNone(serverUrl)) {
114
- throw new ConfigError(`Notification "${notificationId}" has links but no server URL is available. Set the serverUrl step property.`, {
121
+ throw new ConfigError(`Notification "${notificationId}" has links but no server URL is available. Set the serverUrl step property, or the url of the current environment in config.environments.`, {
115
122
  configKey: step['~k']
116
123
  });
117
124
  }
@@ -23,7 +23,6 @@ import getRequestResolver from '../request/getRequestResolver.js';
23
23
  import validateSchemas from '../request/validateSchemas.js';
24
24
  async function handleRequest(context, routineContext, { request }) {
25
25
  const { logger } = context;
26
- const { items } = routineContext;
27
26
  logger.debug({
28
27
  event: 'debug_start_request',
29
28
  request
@@ -42,11 +41,8 @@ async function handleRequest(context, routineContext, { request }) {
42
41
  });
43
42
  const { connectionProperties, requestProperties } = evaluateOperators(context, {
44
43
  connectionConfig,
45
- items,
46
- payload: routineContext.payload,
47
44
  requestConfig,
48
- state: routineContext.state,
49
- steps: routineContext.steps
45
+ routineContext
50
46
  });
51
47
  checkConnectionRead(context, {
52
48
  connectionConfig,
@@ -78,10 +74,11 @@ async function handleRequest(context, routineContext, { request }) {
78
74
  result,
79
75
  stepId: request.stepId
80
76
  });
77
+ // No evaluated values: they can carry chat text or other personal information,
78
+ // which no log level may hold.
81
79
  context.logger.debug({
82
80
  event: 'debug_end_request',
83
- id: requestConfig.id,
84
- result
81
+ id: requestConfig.id
85
82
  });
86
83
  return {
87
84
  status: 'continue'
@@ -13,7 +13,9 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { validate } from '@lowdefy/ajv';
16
+ import { UserError } from '@lowdefy/errors';
16
17
  import addStepResult from './addStepResult.js';
18
+ import evaluateRoutineOperators from './evaluateRoutineOperators.js';
17
19
  function buildErrorMessage(errors, stepId) {
18
20
  const first = errors?.[0];
19
21
  if (!first) return `ValidateSchema step "${stepId}" failed.`;
@@ -21,17 +23,14 @@ function buildErrorMessage(errors, stepId) {
21
23
  return `ValidateSchema step "${stepId}" failed at ${path}: ${first.message}.`;
22
24
  }
23
25
  async function handleValidateSchema(context, routineContext, { step }) {
24
- const { logger, evaluateOperators } = context;
26
+ const { logger } = context;
25
27
  logger.debug({
26
28
  event: 'debug_start_validate_schema',
27
29
  step
28
30
  });
29
- const evaluatedProperties = evaluateOperators({
31
+ const evaluatedProperties = evaluateRoutineOperators(context, routineContext, {
30
32
  input: step.properties,
31
- items: routineContext.items,
32
- location: step.stepId,
33
- payload: routineContext.payload,
34
- steps: routineContext.steps
33
+ location: step.stepId
35
34
  });
36
35
  const { schema, data, throwOnInvalid = true } = evaluatedProperties;
37
36
  const { valid, errors } = validate({
@@ -48,15 +47,18 @@ async function handleValidateSchema(context, routineContext, { step }) {
48
47
  stepId: step.stepId
49
48
  });
50
49
  if (!valid && throwOnInvalid) {
51
- const error = new Error(buildErrorMessage(result.errors, step.stepId), {
52
- cause: result.errors
53
- });
50
+ // A UserError, so the caller sees which field failed: the message is built from the schema
51
+ // path and rule, never the data, and every other server error reaches the client generic.
52
+ // No cause: a UserError's cause crosses the wire as author data, and the ajv errors carry
53
+ // server schema detail. They stay in the step result and the log.
54
+ const error = new UserError(buildErrorMessage(result.errors, step.stepId));
54
55
  // Log under `err` — see controlThrow: only the `err` key runs the pino error
55
56
  // serializer, so `error` would drop the message from the log line.
56
57
  logger.error({
57
58
  event: 'error_validate_schema',
58
59
  stepId: step.stepId,
59
- err: error
60
+ err: error,
61
+ errors: result.errors
60
62
  });
61
63
  return {
62
64
  status: 'error',
@@ -30,6 +30,7 @@ async function invokeEndpoint(context, { endpointId, payload, endpointDepth }) {
30
30
  steps: {},
31
31
  payload: payload ?? {},
32
32
  arrayIndices: [],
33
+ error: null,
33
34
  items: {},
34
35
  state: {},
35
36
  endpointDepth: endpointDepth + 1