@lowdefy/api 5.2.0 → 5.3.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/dist/index.js CHANGED
@@ -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 callEndpoint from './routes/endpoints/callEndpoint.js';
15
+ */ import callAgent from './routes/agent/callAgent.js';
16
+ import callEndpoint from './routes/endpoints/callEndpoint.js';
16
17
  import callRequest from './routes/request/callRequest.js';
17
18
  import createApiContext from './context/createApiContext.js';
18
19
  import createSessionCallback from './routes/auth/callbacks/createSessionCallback.js';
@@ -21,4 +22,4 @@ import getNextAuthConfig from './routes/auth/getNextAuthConfig.js';
21
22
  import getPageConfig from './routes/page/getPageConfig.js';
22
23
  import getRootConfig from './routes/rootConfig/getRootConfig.js';
23
24
  import logClientError from './routes/log/logClientError.js';
24
- export { callEndpoint, callRequest, createApiContext, createSessionCallback, getHomeAndMenus, getNextAuthConfig, getPageConfig, getRootConfig, logClientError };
25
+ export { callAgent, callEndpoint, callRequest, createApiContext, createSessionCallback, getHomeAndMenus, getNextAuthConfig, getPageConfig, getRootConfig, logClientError };
@@ -0,0 +1,217 @@
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 { serializer, type } from '@lowdefy/helpers';
16
+ import createEvaluateOperators from '../../context/createEvaluateOperators.js';
17
+ import authorizeApiEndpoint from '../endpoints/authorizeApiEndpoint.js';
18
+ import getEndpointConfig from '../endpoints/getEndpointConfig.js';
19
+ import runRoutine from '../endpoints/runRoutine.js';
20
+ import getAgentConfig from './getAgentConfig.js';
21
+ import getAgentResolver from './getAgentResolver.js';
22
+ import getConnectionConfig from '../connections/getConnectionConfig.js';
23
+ import getConnection from '../connections/getConnection.js';
24
+ async function callAgent(context, { agentId, pageId, messages, conversationId, urlQuery, sharedState }) {
25
+ const { logger } = context;
26
+ context.pageId = pageId;
27
+ context.evaluateOperators = createEvaluateOperators(context);
28
+ logger.debug({
29
+ event: 'debug_agent',
30
+ agentId,
31
+ pageId
32
+ });
33
+ const agentConfig = await getAgentConfig(context, {
34
+ agentId
35
+ });
36
+ const agentContext = {
37
+ conversationId: conversationId ?? undefined,
38
+ pageId,
39
+ sharedState: sharedState ?? {},
40
+ urlQuery: urlQuery ?? {},
41
+ userId: context.user?.sub ?? context.user?.id ?? null
42
+ };
43
+ // Evaluate operators in agent properties (e.g. _user, _secret, _payload)
44
+ agentConfig.properties = context.evaluateOperators({
45
+ input: agentConfig.properties ?? {},
46
+ location: agentConfig.agentId,
47
+ payload: agentContext,
48
+ steps: {}
49
+ });
50
+ // Load connection config from build artifacts using agent's connectionId
51
+ const connectionConfig = await getConnectionConfig(context, {
52
+ connectionId: agentConfig.connectionId,
53
+ configKey: agentConfig['~k']
54
+ });
55
+ // Get connection plugin from registry
56
+ const connection = getConnection(context, {
57
+ connectionConfig
58
+ });
59
+ // Evaluate operators in connection properties
60
+ const connectionProperties = context.evaluateOperators({
61
+ input: connectionConfig.properties || {},
62
+ location: connectionConfig.connectionId,
63
+ payload: {},
64
+ steps: {}
65
+ });
66
+ // Create connection instance (e.g., Anthropic provider)
67
+ const connectionInstance = connection.create({
68
+ connection: connectionProperties
69
+ });
70
+ // Get agent type from plugin registry
71
+ const agentType = getAgentResolver(context, {
72
+ agentConfig
73
+ });
74
+ // Build resolver context with callEndpoint that allows InternalApi endpoints
75
+ const resolverContext = {
76
+ agentContext,
77
+ evaluateOperators: (input)=>context.evaluateOperators({
78
+ input,
79
+ location: agentConfig.agentId,
80
+ payload: agentContext,
81
+ steps: {}
82
+ }),
83
+ callEndpoint: async (endpointId, { payload, abortSignal })=>{
84
+ const endpointConfig = await getEndpointConfig(context, {
85
+ endpointId
86
+ });
87
+ authorizeApiEndpoint(context, {
88
+ endpointConfig
89
+ });
90
+ const routineContext = {
91
+ steps: {},
92
+ payload: payload ?? {},
93
+ arrayIndices: [],
94
+ items: {},
95
+ endpointDepth: 0
96
+ };
97
+ const { error, response, status } = await runRoutine(context, routineContext, {
98
+ routine: endpointConfig.routine
99
+ });
100
+ const success = ![
101
+ 'error',
102
+ 'reject'
103
+ ].includes(status);
104
+ return {
105
+ error: serializer.serialize(error),
106
+ response: serializer.serialize(response),
107
+ status: success ? 'success' : status,
108
+ success
109
+ };
110
+ },
111
+ getEndpointConfig: async ({ endpointId })=>{
112
+ return getEndpointConfig(context, {
113
+ endpointId
114
+ });
115
+ },
116
+ getAgentConfig: async ({ agentId })=>{
117
+ return getAgentConfig(context, {
118
+ agentId
119
+ });
120
+ },
121
+ getConnectionForAgent: async ({ agentConfig: subAgentConfig })=>{
122
+ const subConnectionConfig = await getConnectionConfig(context, {
123
+ connectionId: subAgentConfig.connectionId,
124
+ configKey: subAgentConfig['~k']
125
+ });
126
+ const subConnection = getConnection(context, {
127
+ connectionConfig: subConnectionConfig
128
+ });
129
+ const subConnectionProperties = context.evaluateOperators({
130
+ input: subConnectionConfig.properties || {},
131
+ location: subConnectionConfig.connectionId,
132
+ payload: {},
133
+ steps: {}
134
+ });
135
+ return subConnection.create({
136
+ connection: subConnectionProperties
137
+ });
138
+ },
139
+ resolveMcpSources: async ({ agentConfig: subAgentConfig })=>{
140
+ const resolvedMcp = [];
141
+ for (const mcpSource of subAgentConfig.mcp ?? []){
142
+ if (!type.isNone(mcpSource.connectionId)) {
143
+ const mcpConnConfig = await getConnectionConfig(context, {
144
+ connectionId: mcpSource.connectionId,
145
+ configKey: subAgentConfig['~k']
146
+ });
147
+ const mcpConnection = getConnection(context, {
148
+ connectionConfig: mcpConnConfig
149
+ });
150
+ const mcpConnProps = context.evaluateOperators({
151
+ input: mcpConnConfig.properties || {},
152
+ location: mcpConnConfig.connectionId,
153
+ payload: {},
154
+ steps: {}
155
+ });
156
+ const mcpConfig = mcpConnection.create({
157
+ connection: mcpConnProps
158
+ });
159
+ const { connectionId: _, ...overrides } = mcpSource;
160
+ resolvedMcp.push({
161
+ ...mcpConfig,
162
+ ...overrides
163
+ });
164
+ } else {
165
+ resolvedMcp.push(mcpSource);
166
+ }
167
+ }
168
+ return resolvedMcp;
169
+ }
170
+ };
171
+ // Resolve MCP connection references to inline config.
172
+ // Agent-level overrides (like confirm) may still contain operators —
173
+ // handleAgentChat evaluates those via its existing evaluateOperators call.
174
+ const resolvedMcp = [];
175
+ for (const mcpSource of agentConfig.mcp ?? []){
176
+ if (!type.isNone(mcpSource.connectionId)) {
177
+ const mcpConnConfig = await getConnectionConfig(context, {
178
+ connectionId: mcpSource.connectionId,
179
+ configKey: agentConfig['~k']
180
+ });
181
+ const mcpConnection = getConnection(context, {
182
+ connectionConfig: mcpConnConfig
183
+ });
184
+ const mcpConnProps = context.evaluateOperators({
185
+ input: mcpConnConfig.properties || {},
186
+ location: mcpConnConfig.connectionId,
187
+ payload: {},
188
+ steps: {}
189
+ });
190
+ const mcpConfig = mcpConnection.create({
191
+ connection: mcpConnProps
192
+ });
193
+ // Merge: connection properties as base, agent-level overrides on top
194
+ const { connectionId: _, ...overrides } = mcpSource;
195
+ resolvedMcp.push({
196
+ ...mcpConfig,
197
+ ...overrides
198
+ });
199
+ } else {
200
+ resolvedMcp.push(mcpSource);
201
+ }
202
+ }
203
+ agentConfig.mcp = resolvedMcp;
204
+ // Call the agent resolver
205
+ const { response } = await agentType.resolver({
206
+ connection: connectionInstance,
207
+ properties: {
208
+ agent: agentConfig,
209
+ messages
210
+ },
211
+ context: resolverContext
212
+ });
213
+ return {
214
+ response
215
+ };
216
+ }
217
+ export default callAgent;
@@ -0,0 +1,30 @@
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 { ConfigError } from '@lowdefy/errors';
16
+ async function getAgentConfig({ logger, readConfigFile }, { agentId }) {
17
+ const agent = await readConfigFile(`agents/${agentId}.json`);
18
+ if (!agent) {
19
+ const err = new ConfigError(`Agent "${agentId}" does not exist.`);
20
+ logger.debug({
21
+ params: {
22
+ agentId
23
+ },
24
+ err
25
+ }, err.message);
26
+ throw err;
27
+ }
28
+ return agent;
29
+ }
30
+ export default getAgentConfig;
@@ -0,0 +1,33 @@
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 { ConfigError } from '@lowdefy/errors';
16
+ function getAgentResolver({ agents, logger }, { agentConfig }) {
17
+ const agentType = agents[agentConfig.type];
18
+ if (!agentType) {
19
+ const err = new ConfigError(`Agent type "${agentConfig.type}" can not be found.`, {
20
+ configKey: agentConfig['~k']
21
+ });
22
+ logger.debug({
23
+ params: {
24
+ id: agentConfig.agentId,
25
+ type: agentConfig.type
26
+ },
27
+ err
28
+ }, err.message);
29
+ throw err;
30
+ }
31
+ return agentType;
32
+ }
33
+ export default getAgentResolver;
@@ -13,16 +13,15 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigError } from '@lowdefy/errors';
16
- async function getConnectionConfig({ logger, readConfigFile }, { requestConfig }) {
17
- const { connectionId, requestId } = requestConfig;
16
+ async function getConnectionConfig({ logger, readConfigFile }, { connectionId, configKey }) {
18
17
  let err;
19
18
  if (!connectionId) {
20
- err = new ConfigError(`Request "${requestId}" does not specify a connection.`, {
21
- configKey: requestConfig['~k']
19
+ err = new ConfigError('Connection id is missing.', {
20
+ configKey
22
21
  });
23
22
  logger.debug({
24
23
  params: {
25
- requestId
24
+ connectionId
26
25
  },
27
26
  err
28
27
  }, err.message);
@@ -31,11 +30,11 @@ async function getConnectionConfig({ logger, readConfigFile }, { requestConfig }
31
30
  const connection = await readConfigFile(`connections/${connectionId}.json`);
32
31
  if (!connection) {
33
32
  err = new ConfigError(`Connection "${connectionId}" does not exist.`, {
34
- configKey: requestConfig['~k']
33
+ configKey
35
34
  });
36
35
  logger.debug({
37
36
  params: {
38
- requestId
37
+ connectionId
39
38
  },
40
39
  err
41
40
  }, err.message);
@@ -17,8 +17,8 @@ import callRequestResolver from '../request/callRequestResolver.js';
17
17
  import checkConnectionRead from '../request/checkConnectionRead.js';
18
18
  import checkConnectionWrite from '../request/checkConnectionWrite.js';
19
19
  import evaluateOperators from '../request/evaluateOperators.js';
20
- import getConnection from '../request/getConnection.js';
21
- import getConnectionConfig from '../request/getConnectionConfig.js';
20
+ import getConnection from '../connections/getConnection.js';
21
+ import getConnectionConfig from '../connections/getConnectionConfig.js';
22
22
  import getRequestResolver from '../request/getRequestResolver.js';
23
23
  import validateSchemas from '../request/validateSchemas.js';
24
24
  async function handleRequest(context, routineContext, { request }) {
@@ -30,7 +30,8 @@ async function handleRequest(context, routineContext, { request }) {
30
30
  });
31
31
  const requestConfig = request;
32
32
  const connectionConfig = await getConnectionConfig(context, {
33
- requestConfig
33
+ connectionId: requestConfig.connectionId,
34
+ configKey: requestConfig['~k']
34
35
  });
35
36
  const connection = getConnection(context, {
36
37
  connectionConfig
@@ -18,8 +18,8 @@ import callRequestResolver from './callRequestResolver.js';
18
18
  import checkConnectionRead from './checkConnectionRead.js';
19
19
  import checkConnectionWrite from './checkConnectionWrite.js';
20
20
  import evaluateOperators from './evaluateOperators.js';
21
- import getConnection from './getConnection.js';
22
- import getConnectionConfig from './getConnectionConfig.js';
21
+ import getConnection from '../connections/getConnection.js';
22
+ import getConnectionConfig from '../connections/getConnectionConfig.js';
23
23
  import getRequestConfig from './getRequestConfig.js';
24
24
  import getRequestResolver from './getRequestResolver.js';
25
25
  import validateSchemas from './validateSchemas.js';
@@ -43,7 +43,8 @@ async function callRequest(context, { blockId, pageId, payload, requestId }) {
43
43
  requestId
44
44
  });
45
45
  const connectionConfig = await getConnectionConfig(context, {
46
- requestConfig
46
+ connectionId: requestConfig.connectionId,
47
+ configKey: requestConfig['~k']
47
48
  });
48
49
  authorizeRequest(context, {
49
50
  requestConfig
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/api",
3
- "version": "5.2.0",
3
+ "version": "5.3.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -34,13 +34,13 @@
34
34
  "dist/*"
35
35
  ],
36
36
  "dependencies": {
37
- "@lowdefy/ajv": "5.2.0",
38
- "@lowdefy/errors": "5.2.0",
39
- "@lowdefy/helpers": "5.2.0",
40
- "@lowdefy/node-utils": "5.2.0",
41
- "@lowdefy/nunjucks": "5.2.0",
42
- "@lowdefy/operators": "5.2.0",
43
- "@lowdefy/operators-js": "5.2.0"
37
+ "@lowdefy/ajv": "5.3.0",
38
+ "@lowdefy/errors": "5.3.0",
39
+ "@lowdefy/helpers": "5.3.0",
40
+ "@lowdefy/node-utils": "5.3.0",
41
+ "@lowdefy/nunjucks": "5.3.0",
42
+ "@lowdefy/operators": "5.3.0",
43
+ "@lowdefy/operators-js": "5.3.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@jest/globals": "28.1.3",