@lowdefy/api 6.1.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.
- package/dist/routes/agent/callAgent.js +20 -2
- package/dist/routes/agent/redactMessages.js +136 -0
- package/dist/routes/endpoints/callEndpoint.js +3 -2
- package/dist/routes/endpoints/control/controlFor.js +3 -4
- package/dist/routes/endpoints/control/controlIf.js +3 -1
- package/dist/routes/endpoints/control/controlParallelFor.js +3 -4
- package/dist/routes/endpoints/control/controlReturn.js +3 -2
- package/dist/routes/endpoints/control/controlSetState.js +1 -1
- package/dist/routes/endpoints/control/controlSwitch.js +1 -1
- package/dist/routes/endpoints/handleAgentCall.js +1 -1
- package/dist/routes/endpoints/handleEndpointCall.js +1 -2
- package/dist/routes/endpoints/handleRequest.js +3 -2
- package/package.json +9 -9
|
@@ -12,8 +12,10 @@
|
|
|
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
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import createEvaluateOperators from '../../context/createEvaluateOperators.js';
|
|
16
17
|
import prepareAgent from './prepareAgent.js';
|
|
18
|
+
import redactMessages from './redactMessages.js';
|
|
17
19
|
async function callAgent(context, { agentId, pageId, messages, conversationId, urlQuery, sharedState }) {
|
|
18
20
|
const { logger } = context;
|
|
19
21
|
context.pageId = pageId;
|
|
@@ -36,12 +38,28 @@ async function callAgent(context, { agentId, pageId, messages, conversationId, u
|
|
|
36
38
|
endpointDepth: 0,
|
|
37
39
|
mode: 'chat'
|
|
38
40
|
});
|
|
41
|
+
// Mask the user's text before the resolver hands it to the model, title generation, tools
|
|
42
|
+
// and hooks.
|
|
43
|
+
let agentMessages = messages;
|
|
44
|
+
if (!type.isNone(agentConfig.redact)) {
|
|
45
|
+
const redacted = redactMessages({
|
|
46
|
+
messages,
|
|
47
|
+
redact: resolverContext.evaluateOperators(agentConfig.redact),
|
|
48
|
+
configKey: agentConfig['~k']
|
|
49
|
+
});
|
|
50
|
+
agentMessages = redacted.messages;
|
|
51
|
+
logger.debug({
|
|
52
|
+
event: 'debug_agent_redact',
|
|
53
|
+
agentId,
|
|
54
|
+
replacements: redacted.counts
|
|
55
|
+
});
|
|
56
|
+
}
|
|
39
57
|
// Call the agent resolver
|
|
40
58
|
const { response } = await agentType.resolver({
|
|
41
59
|
connection: connectionInstance,
|
|
42
60
|
properties: {
|
|
43
61
|
agent: agentConfig,
|
|
44
|
-
messages
|
|
62
|
+
messages: agentMessages
|
|
45
63
|
},
|
|
46
64
|
context: resolverContext
|
|
47
65
|
});
|
|
@@ -0,0 +1,136 @@
|
|
|
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
|
+
import { type } from '@lowdefy/helpers';
|
|
17
|
+
const DEFAULT_REPLACEMENT = '[removed]';
|
|
18
|
+
// The build checks a literal redact config; this checks it again after operators are
|
|
19
|
+
// evaluated, since a pattern an operator returns is only known now. Returns the patterns as
|
|
20
|
+
// { name, regex }, each regex global so a replace masks every match.
|
|
21
|
+
function buildPatterns({ redact, configKey }) {
|
|
22
|
+
if (!type.isObject(redact)) {
|
|
23
|
+
throw new ConfigError('Agent "redact" should be an object.', {
|
|
24
|
+
received: redact,
|
|
25
|
+
configKey
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (!type.isArray(redact.patterns) || redact.patterns.length === 0) {
|
|
29
|
+
throw new ConfigError('Agent "redact.patterns" should be a non-empty array.', {
|
|
30
|
+
received: redact.patterns,
|
|
31
|
+
configKey
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return redact.patterns.map((entry, index)=>{
|
|
35
|
+
const { name, pattern, flags } = type.isString(entry) ? {
|
|
36
|
+
pattern: entry
|
|
37
|
+
} : entry ?? {};
|
|
38
|
+
const location = `redact.patterns[${index}]`;
|
|
39
|
+
if (!type.isString(pattern)) {
|
|
40
|
+
throw new ConfigError(`Agent "${location}.pattern" should be a string.`, {
|
|
41
|
+
received: pattern,
|
|
42
|
+
configKey
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (!type.isNone(name) && !type.isString(name)) {
|
|
46
|
+
throw new ConfigError(`Agent "${location}.name" should be a string.`, {
|
|
47
|
+
received: name,
|
|
48
|
+
configKey
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (!type.isNone(flags) && !type.isString(flags)) {
|
|
52
|
+
throw new ConfigError(`Agent "${location}.flags" should be a string.`, {
|
|
53
|
+
received: flags,
|
|
54
|
+
configKey
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// With the sticky flag a global replace stops at the first gap between matches.
|
|
58
|
+
if (type.isString(flags) && flags.includes('y')) {
|
|
59
|
+
throw new ConfigError(`Agent "${location}.flags" should not include "y".`, {
|
|
60
|
+
received: flags,
|
|
61
|
+
configKey
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
const allFlags = (flags ?? '').includes('g') ? flags : `${flags ?? ''}g`;
|
|
65
|
+
let regex;
|
|
66
|
+
try {
|
|
67
|
+
regex = new RegExp(pattern, allFlags);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
throw new ConfigError(`Agent "${location}" is not a valid regular expression: ${error.message}`, {
|
|
70
|
+
cause: error,
|
|
71
|
+
received: pattern,
|
|
72
|
+
configKey
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
// A pattern that matches empty text would put the replacement between every character.
|
|
76
|
+
if (regex.test('')) {
|
|
77
|
+
throw new ConfigError(`Agent "${location}" matches empty text.`, {
|
|
78
|
+
received: pattern,
|
|
79
|
+
configKey
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
regex.lastIndex = 0;
|
|
83
|
+
return {
|
|
84
|
+
name: name ?? location,
|
|
85
|
+
regex
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
// Masks every match of the agent's redact patterns in the text parts of the user's messages,
|
|
90
|
+
// before the model, title generation, tools and hooks see them. Every user message is masked,
|
|
91
|
+
// not only the last: the browser sends the whole conversation each turn. Assistant and tool
|
|
92
|
+
// messages, and file parts, are left as they are. The messages are the client's, not yet
|
|
93
|
+
// validated, so a malformed message or part is passed over here and refused by the agent's
|
|
94
|
+
// message validation. counts holds the replacements made per pattern name, never the text.
|
|
95
|
+
function redactMessages({ messages, redact, configKey }) {
|
|
96
|
+
const patterns = buildPatterns({
|
|
97
|
+
redact,
|
|
98
|
+
configKey
|
|
99
|
+
});
|
|
100
|
+
const replacement = redact.replacement ?? DEFAULT_REPLACEMENT;
|
|
101
|
+
if (!type.isString(replacement)) {
|
|
102
|
+
throw new ConfigError('Agent "redact.replacement" should be a string.', {
|
|
103
|
+
received: replacement,
|
|
104
|
+
configKey
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const counts = {};
|
|
108
|
+
function maskText(text) {
|
|
109
|
+
let masked = text;
|
|
110
|
+
for (const { name, regex } of patterns){
|
|
111
|
+
masked = masked.replace(regex, ()=>{
|
|
112
|
+
counts[name] = (counts[name] ?? 0) + 1;
|
|
113
|
+
return replacement;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return masked;
|
|
117
|
+
}
|
|
118
|
+
const redacted = messages.map((message)=>{
|
|
119
|
+
if (message?.role !== 'user' || !type.isArray(message.parts)) return message;
|
|
120
|
+
return {
|
|
121
|
+
...message,
|
|
122
|
+
parts: message.parts.map((part)=>{
|
|
123
|
+
if (part?.type !== 'text' || !type.isString(part.text)) return part;
|
|
124
|
+
return {
|
|
125
|
+
...part,
|
|
126
|
+
text: maskText(part.text)
|
|
127
|
+
};
|
|
128
|
+
})
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
messages: redacted,
|
|
133
|
+
counts
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export default redactMessages;
|
|
@@ -27,12 +27,13 @@ async function callEndpoint(context, { blockId, endpointId, pageId, payload }) {
|
|
|
27
27
|
context.endpointId = endpointId;
|
|
28
28
|
context.pageId = pageId;
|
|
29
29
|
context.evaluateOperators = createEvaluateOperators(context);
|
|
30
|
+
// Ids only: a payload can carry chat text or other personal information (an agent
|
|
31
|
+
// chat's save-thread call sends the whole thread), which no log level may hold.
|
|
30
32
|
logger.debug({
|
|
31
33
|
event: 'debug_endpoint',
|
|
32
34
|
blockId,
|
|
33
35
|
endpointId,
|
|
34
|
-
pageId
|
|
35
|
-
payload
|
|
36
|
+
pageId
|
|
36
37
|
});
|
|
37
38
|
const endpointConfig = await getEndpointConfig(context, {
|
|
38
39
|
endpointId
|
|
@@ -28,7 +28,7 @@ async function controlFor(context, routineContext, { control }) {
|
|
|
28
28
|
});
|
|
29
29
|
logger.debug({
|
|
30
30
|
event: 'debug_control_for',
|
|
31
|
-
array,
|
|
31
|
+
itemCount: Array.isArray(array) ? array.length : null,
|
|
32
32
|
itemName
|
|
33
33
|
});
|
|
34
34
|
if (!Array.isArray(array)) {
|
|
@@ -47,9 +47,8 @@ async function controlFor(context, routineContext, { control }) {
|
|
|
47
47
|
};
|
|
48
48
|
logger.debug({
|
|
49
49
|
event: 'debug_control_for_iteration',
|
|
50
|
-
itemName
|
|
51
|
-
|
|
52
|
-
items: updatedItems
|
|
50
|
+
itemName,
|
|
51
|
+
index
|
|
53
52
|
});
|
|
54
53
|
const res = await runRoutine(context, {
|
|
55
54
|
...routineContext,
|
|
@@ -20,11 +20,13 @@ async function controlIf(context, routineContext, { control }) {
|
|
|
20
20
|
input: control[':if'],
|
|
21
21
|
location: control['~k'] ?? ':if'
|
|
22
22
|
});
|
|
23
|
+
// No evaluated values: they can carry chat text or other personal information,
|
|
24
|
+
// which no log level may hold.
|
|
23
25
|
logger.debug({
|
|
24
26
|
event: 'debug_control_if',
|
|
25
27
|
condition: {
|
|
26
28
|
input: control[':if'],
|
|
27
|
-
|
|
29
|
+
result: Boolean(evaluatedIf)
|
|
28
30
|
}
|
|
29
31
|
});
|
|
30
32
|
if (evaluatedIf) {
|
|
@@ -28,7 +28,7 @@ async function controlParallelFor(context, routineContext, { control }) {
|
|
|
28
28
|
});
|
|
29
29
|
logger.debug({
|
|
30
30
|
event: 'debug_control_parallel',
|
|
31
|
-
array,
|
|
31
|
+
itemCount: Array.isArray(array) ? array.length : null,
|
|
32
32
|
itemName
|
|
33
33
|
});
|
|
34
34
|
if (!Array.isArray(array)) {
|
|
@@ -47,9 +47,8 @@ async function controlParallelFor(context, routineContext, { control }) {
|
|
|
47
47
|
};
|
|
48
48
|
logger.debug({
|
|
49
49
|
event: 'debug_control_parallel_iteration',
|
|
50
|
-
itemName
|
|
51
|
-
|
|
52
|
-
items: updatedItems
|
|
50
|
+
itemName,
|
|
51
|
+
index
|
|
53
52
|
});
|
|
54
53
|
return runRoutine(context, {
|
|
55
54
|
...routineContext,
|
|
@@ -18,9 +18,10 @@ async function controlReturn(context, routineContext, { control }) {
|
|
|
18
18
|
input: control[':return'],
|
|
19
19
|
location: control['~k'] ?? ':return'
|
|
20
20
|
});
|
|
21
|
+
// No evaluated values: they can carry chat text or other personal information,
|
|
22
|
+
// which no log level may hold.
|
|
21
23
|
context.logger.debug({
|
|
22
|
-
event: 'debug_control_return'
|
|
23
|
-
response
|
|
24
|
+
event: 'debug_control_return'
|
|
24
25
|
});
|
|
25
26
|
return {
|
|
26
27
|
status: 'return',
|
|
@@ -24,7 +24,7 @@ function controlSetState(context, routineContext, { control }) {
|
|
|
24
24
|
logger.debug({
|
|
25
25
|
event: 'debug_control_set_state',
|
|
26
26
|
input: control[':set_state'],
|
|
27
|
-
|
|
27
|
+
keys: Object.keys(evaluatedSetState ?? {})
|
|
28
28
|
});
|
|
29
29
|
Object.entries(evaluatedSetState).forEach(([key, value])=>{
|
|
30
30
|
try {
|
|
@@ -35,7 +35,7 @@ async function handleAgentCall(context, routineContext, { step }) {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
if (!type.isString(prompt)) {
|
|
38
|
-
throw new ConfigError(`CallAgent step "${step.stepId}" properties.prompt must evaluate to a string. Received ${
|
|
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}".`, {
|
|
39
39
|
configKey: step['~k']
|
|
40
40
|
});
|
|
41
41
|
}
|
|
@@ -92,8 +92,7 @@ async function handleEndpointCall(context, routineContext, { step }) {
|
|
|
92
92
|
logger.debug({
|
|
93
93
|
event: 'debug_end_endpoint_call',
|
|
94
94
|
stepId: step.stepId,
|
|
95
|
-
targetEndpointId: evaluatedProperties.endpointId
|
|
96
|
-
response
|
|
95
|
+
targetEndpointId: evaluatedProperties.endpointId
|
|
97
96
|
});
|
|
98
97
|
return {
|
|
99
98
|
status: 'continue'
|
|
@@ -74,10 +74,11 @@ async function handleRequest(context, routineContext, { request }) {
|
|
|
74
74
|
result,
|
|
75
75
|
stepId: request.stepId
|
|
76
76
|
});
|
|
77
|
+
// No evaluated values: they can carry chat text or other personal information,
|
|
78
|
+
// which no log level may hold.
|
|
77
79
|
context.logger.debug({
|
|
78
80
|
event: 'debug_end_request',
|
|
79
|
-
id: requestConfig.id
|
|
80
|
-
result
|
|
81
|
+
id: requestConfig.id
|
|
81
82
|
});
|
|
82
83
|
return {
|
|
83
84
|
status: 'continue'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/api",
|
|
3
|
-
"version": "6.
|
|
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.
|
|
38
|
-
"@lowdefy/build": "6.
|
|
39
|
-
"@lowdefy/errors": "6.
|
|
40
|
-
"@lowdefy/helpers": "6.
|
|
41
|
-
"@lowdefy/node-utils": "6.
|
|
42
|
-
"@lowdefy/nunjucks": "6.
|
|
43
|
-
"@lowdefy/operators": "6.
|
|
44
|
-
"@lowdefy/operators-js": "6.
|
|
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": {
|