@lark-base-open/base-database-toolchain 1.2.0 → 1.3.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.
- package/CHANGELOG.md +27 -0
- package/README.md +7 -1
- package/bin/base-database-toolchain.mjs +13 -1
- package/dist/app-builder/core.d.mts +4 -0
- package/dist/app-builder/core.mjs +736 -110
- package/dist/app-builder/package.json +1 -1
- package/dist/data/config-schema.json +95 -0
- package/dist/dependency-policy.d.ts +13 -0
- package/dist/dependency-policy.js +187 -0
- package/dist/dev-worker.mjs +27 -21
- package/dist/function-description.d.ts +12 -0
- package/dist/function-description.js +38 -0
- package/dist/function-params.d.ts +50 -0
- package/dist/function-params.js +157 -0
- package/dist/function-schema.d.ts +35 -0
- package/dist/function-schema.js +490 -0
- package/dist/go-adapter.d.mts +1 -1
- package/dist/go-adapter.mjs +299 -225
- package/dist/mutation-schema.d.ts +151 -0
- package/dist/mutation-schema.js +158 -0
- package/dist/mutation.d.ts +12 -0
- package/dist/mutation.js +145 -0
- package/dist/npm-policy.d.ts +16 -0
- package/dist/npm-policy.js +193 -0
- package/dist/page-link-guard.js +3 -2
- package/dist/project-contract.d.ts +2 -0
- package/dist/project-contract.js +38 -0
- package/dist/project-layout.d.ts +15 -0
- package/dist/project-layout.js +89 -0
- package/dist/source-archive-inputs.d.ts +13 -0
- package/dist/source-archive-inputs.js +105 -0
- package/npm-shrinkwrap.json +13 -3
- package/package.json +15 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"type":"module","version":"1.0.
|
|
1
|
+
{"type":"module","version":"1.0.12"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"title": "Base Database CLI configuration",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"additionalProperties": false,
|
|
6
|
+
"required": [
|
|
7
|
+
"configVersion",
|
|
8
|
+
"appToken"
|
|
9
|
+
],
|
|
10
|
+
"properties": {
|
|
11
|
+
"$schema": {
|
|
12
|
+
"type": "string"
|
|
13
|
+
},
|
|
14
|
+
"configVersion": {
|
|
15
|
+
"const": 1
|
|
16
|
+
},
|
|
17
|
+
"name": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"minLength": 1
|
|
20
|
+
},
|
|
21
|
+
"runtimeProfile": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"minLength": 1
|
|
24
|
+
},
|
|
25
|
+
"larkProfile": {
|
|
26
|
+
"type": "string",
|
|
27
|
+
"minLength": 1,
|
|
28
|
+
"maxLength": 64,
|
|
29
|
+
"description": "Name of an existing lark-cli profile; credentials stay in lark-cli."
|
|
30
|
+
},
|
|
31
|
+
"appToken": {
|
|
32
|
+
"type": "string",
|
|
33
|
+
"minLength": 1
|
|
34
|
+
},
|
|
35
|
+
"bindings": {},
|
|
36
|
+
"template": {
|
|
37
|
+
"type": "object",
|
|
38
|
+
"additionalProperties": false,
|
|
39
|
+
"required": [
|
|
40
|
+
"name",
|
|
41
|
+
"version"
|
|
42
|
+
],
|
|
43
|
+
"properties": {
|
|
44
|
+
"name": {
|
|
45
|
+
"const": "database-starter"
|
|
46
|
+
},
|
|
47
|
+
"version": {
|
|
48
|
+
"enum": [
|
|
49
|
+
1,
|
|
50
|
+
2
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"description": "Starter layout version. Omitted in legacy database-starter version 1 projects."
|
|
55
|
+
},
|
|
56
|
+
"managedAgentDocs": {
|
|
57
|
+
"type": "boolean",
|
|
58
|
+
"default": true,
|
|
59
|
+
"description": "Maintain the base-app managed section in AGENTS.md and the CLAUDE.md reference. Set false to opt out."
|
|
60
|
+
},
|
|
61
|
+
"functionInputs": {
|
|
62
|
+
"enum": [
|
|
63
|
+
"named",
|
|
64
|
+
"positional"
|
|
65
|
+
],
|
|
66
|
+
"description": "Classic Page/helper input contract; defaults to positional when omitted. Workspace and SDK clients use named inputs."
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"allOf": [
|
|
70
|
+
{
|
|
71
|
+
"if": {
|
|
72
|
+
"required": [
|
|
73
|
+
"template"
|
|
74
|
+
],
|
|
75
|
+
"properties": {
|
|
76
|
+
"template": {
|
|
77
|
+
"properties": {
|
|
78
|
+
"version": {
|
|
79
|
+
"const": 2
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"type": "object"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"then": {
|
|
87
|
+
"properties": {
|
|
88
|
+
"functionInputs": {
|
|
89
|
+
"const": "named"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface PackageDependencyPolicy {
|
|
2
|
+
restrictedProtocols: string[];
|
|
3
|
+
rejectLocalPaths: boolean;
|
|
4
|
+
rejectScpGitSyntax: boolean;
|
|
5
|
+
rejectHostedGitShorthand: boolean;
|
|
6
|
+
allowedValues: string[];
|
|
7
|
+
allowedLockfileRegistryHosts?: string[];
|
|
8
|
+
}
|
|
9
|
+
export declare function parsePackageDependencyPolicy(value: unknown): PackageDependencyPolicy;
|
|
10
|
+
export declare function validateWorkspacePackageDependencies(workspace: string, policy: PackageDependencyPolicy): void;
|
|
11
|
+
export declare function validateWorkspaceLockRegistries(workspace: string, policy: PackageDependencyPolicy): void;
|
|
12
|
+
export { lockRepairHint, LockRegistryError, validateLockRegistries, validateDependencyLock, } from './npm-policy.js';
|
|
13
|
+
export type { LockRegistryGroup } from './npm-policy.js';
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readdirSync, readFileSync, readlinkSync, } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
3
|
+
import { RE2JS } from 're2js';
|
|
4
|
+
import { validateLockRegistries } from './npm-policy.js';
|
|
5
|
+
import { projectLayout } from './project-layout.js';
|
|
6
|
+
import { sourceArchiveConfig, sourceArchiveEntryIgnored, sourceArchiveRootFiles, sourceArchiveRoots, } from './source-archive-inputs.js';
|
|
7
|
+
const MAX_PACKAGE_JSON_SIZE = 1024 * 1024;
|
|
8
|
+
const MAX_ARCHIVE_UNCOMPRESSED_SIZE = 200 * 1024 * 1024;
|
|
9
|
+
function record(value) {
|
|
10
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
function stringList(value, field) {
|
|
13
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== 'string')) {
|
|
14
|
+
throw new Error(`Dependency policy ${field} must be a string array`);
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
export function parsePackageDependencyPolicy(value) {
|
|
19
|
+
if (!record(value))
|
|
20
|
+
throw new Error('Dependency policy must be an object');
|
|
21
|
+
for (const field of [
|
|
22
|
+
'rejectLocalPaths',
|
|
23
|
+
'rejectScpGitSyntax',
|
|
24
|
+
'rejectHostedGitShorthand',
|
|
25
|
+
]) {
|
|
26
|
+
if (typeof value[field] !== 'boolean') {
|
|
27
|
+
throw new Error(`Dependency policy ${field} must be a boolean`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const allowedValues = stringList(value.allowedValues, 'allowedValues');
|
|
31
|
+
compileAllowedValuePatterns(allowedValues);
|
|
32
|
+
return {
|
|
33
|
+
restrictedProtocols: stringList(value.restrictedProtocols, 'restrictedProtocols'),
|
|
34
|
+
rejectLocalPaths: value.rejectLocalPaths,
|
|
35
|
+
rejectScpGitSyntax: value.rejectScpGitSyntax,
|
|
36
|
+
rejectHostedGitShorthand: value.rejectHostedGitShorthand,
|
|
37
|
+
allowedValues,
|
|
38
|
+
...('allowedLockfileRegistryHosts' in value
|
|
39
|
+
? {
|
|
40
|
+
allowedLockfileRegistryHosts: stringList(value.allowedLockfileRegistryHosts, 'allowedLockfileRegistryHosts'),
|
|
41
|
+
}
|
|
42
|
+
: {}),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function compileAllowedValuePatterns(patterns) {
|
|
46
|
+
return patterns.map((pattern, index) => {
|
|
47
|
+
try {
|
|
48
|
+
return RE2JS.compile(pattern);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw new Error(`Dependency policy allowedValues[${index}] must be a valid regular expression: ${String(error)}`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function matchesAllowedPattern(value, patterns) {
|
|
56
|
+
return patterns.some((pattern) => pattern.testExact(value));
|
|
57
|
+
}
|
|
58
|
+
function isLocalPath(value) {
|
|
59
|
+
if (value === '.' || value === '..' || value === '~')
|
|
60
|
+
return true;
|
|
61
|
+
if (/^[\/]/u.test(value))
|
|
62
|
+
return true;
|
|
63
|
+
if (/^(?:.{1,2}|~)[\/]/u.test(value))
|
|
64
|
+
return true;
|
|
65
|
+
return /^[A-Za-z]:[\/]/u.test(value);
|
|
66
|
+
}
|
|
67
|
+
function isScpGitSyntax(value) {
|
|
68
|
+
const at = value.indexOf('@');
|
|
69
|
+
const colon = value.indexOf(':');
|
|
70
|
+
return at > 0 && colon > at + 1 && !/[\/]/u.test(value.slice(0, at));
|
|
71
|
+
}
|
|
72
|
+
function isRestrictedValue(value, policy) {
|
|
73
|
+
const normalized = value.trim();
|
|
74
|
+
if (policy.rejectLocalPaths && isLocalPath(normalized))
|
|
75
|
+
return true;
|
|
76
|
+
if (policy.rejectScpGitSyntax && isScpGitSyntax(normalized))
|
|
77
|
+
return true;
|
|
78
|
+
const separator = normalized.indexOf(':');
|
|
79
|
+
if (separator < 0) {
|
|
80
|
+
return (policy.rejectHostedGitShorthand &&
|
|
81
|
+
!normalized.startsWith('@') &&
|
|
82
|
+
normalized.includes('/'));
|
|
83
|
+
}
|
|
84
|
+
const protocol = normalized.slice(0, separator).toLowerCase();
|
|
85
|
+
return policy.restrictedProtocols.includes(protocol);
|
|
86
|
+
}
|
|
87
|
+
function validateDependencies(manifest, policy, allowedPatterns, path) {
|
|
88
|
+
if (manifest === null)
|
|
89
|
+
return;
|
|
90
|
+
if (!record(manifest))
|
|
91
|
+
throw new Error(`${path} must contain a JSON object`);
|
|
92
|
+
const dependencies = manifest.dependencies;
|
|
93
|
+
if (dependencies === undefined || dependencies === null)
|
|
94
|
+
return;
|
|
95
|
+
if (!record(dependencies))
|
|
96
|
+
throw new Error(`${path} dependencies must be an object`);
|
|
97
|
+
for (const [name, value] of Object.entries(dependencies)) {
|
|
98
|
+
if (typeof value !== 'string') {
|
|
99
|
+
throw new Error(`${path} dependencies.${name} must be a string`);
|
|
100
|
+
}
|
|
101
|
+
if (isRestrictedValue(value, policy) &&
|
|
102
|
+
!matchesAllowedPattern(value, allowedPatterns)) {
|
|
103
|
+
throw new Error(`${path} dependencies.${name} value ${JSON.stringify(value)} uses a restricted source that the target application dependency policy does not allow; ask the administrator of the target environment to allow this source before retrying`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function addSourceSize(state, size) {
|
|
108
|
+
state.totalSize += size;
|
|
109
|
+
if (state.totalSize > MAX_ARCHIVE_UNCOMPRESSED_SIZE) {
|
|
110
|
+
throw new Error(`Artifact uncompressed content is ${state.totalSize} bytes, exceeds 200 MiB (${MAX_ARCHIVE_UNCOMPRESSED_SIZE} bytes)`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function scanPath(path, state) {
|
|
114
|
+
const info = lstatSync(path);
|
|
115
|
+
if (info.isSymbolicLink()) {
|
|
116
|
+
if (basename(path) === 'package.json') {
|
|
117
|
+
throw new Error(`${relative(state.workspace, path)} must be a regular file`);
|
|
118
|
+
}
|
|
119
|
+
// Archiver stores the target relative to the link's parent, even when the
|
|
120
|
+
// original target is absolute or contains redundant path components.
|
|
121
|
+
const parent = dirname(path);
|
|
122
|
+
const target = relative(parent, resolve(parent, readlinkSync(path)));
|
|
123
|
+
addSourceSize(state, Buffer.byteLength(target));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (info.isDirectory()) {
|
|
127
|
+
for (const entry of readdirSync(path)) {
|
|
128
|
+
const child = join(path, entry);
|
|
129
|
+
if (!sourceArchiveEntryIgnored(entry, lstatSync(child).isDirectory()))
|
|
130
|
+
scanPath(child, state);
|
|
131
|
+
}
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (!info.isFile())
|
|
135
|
+
return;
|
|
136
|
+
addSourceSize(state, info.size);
|
|
137
|
+
if (basename(path) !== 'package.json')
|
|
138
|
+
return;
|
|
139
|
+
const displayPath = relative(state.workspace, path);
|
|
140
|
+
if (info.size > MAX_PACKAGE_JSON_SIZE) {
|
|
141
|
+
throw new Error(`${displayPath} exceeds size limit 1MB`);
|
|
142
|
+
}
|
|
143
|
+
let manifest;
|
|
144
|
+
try {
|
|
145
|
+
manifest = JSON.parse(readFileSync(path, 'utf8'));
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
throw new Error(`${displayPath} is not valid JSON: ${String(error)}`);
|
|
149
|
+
}
|
|
150
|
+
validateDependencies(manifest, state.policy, state.allowedPatterns, displayPath);
|
|
151
|
+
}
|
|
152
|
+
export function validateWorkspacePackageDependencies(workspace, policy) {
|
|
153
|
+
const state = {
|
|
154
|
+
workspace,
|
|
155
|
+
policy,
|
|
156
|
+
allowedPatterns: compileAllowedValuePatterns(policy.allowedValues),
|
|
157
|
+
totalSize: 0,
|
|
158
|
+
};
|
|
159
|
+
const roots = [
|
|
160
|
+
...sourceArchiveRoots(workspace).map(({ source }) => source),
|
|
161
|
+
...sourceArchiveRootFiles(workspace),
|
|
162
|
+
];
|
|
163
|
+
addSourceSize(state, Buffer.byteLength(sourceArchiveConfig(workspace)));
|
|
164
|
+
for (const root of roots) {
|
|
165
|
+
const path = join(workspace, root);
|
|
166
|
+
if (existsSync(path))
|
|
167
|
+
scanPath(path, state);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export function validateWorkspaceLockRegistries(workspace, policy) {
|
|
171
|
+
const hosts = policy.allowedLockfileRegistryHosts;
|
|
172
|
+
if (!hosts?.length)
|
|
173
|
+
return;
|
|
174
|
+
for (const directory of projectLayout(workspace).dependencies) {
|
|
175
|
+
const name = existsSync(join(workspace, directory, 'npm-shrinkwrap.json'))
|
|
176
|
+
? 'npm-shrinkwrap.json'
|
|
177
|
+
: 'package-lock.json';
|
|
178
|
+
const file = join(directory, name);
|
|
179
|
+
const path = join(workspace, file);
|
|
180
|
+
if (!existsSync(path))
|
|
181
|
+
continue;
|
|
182
|
+
const lock = JSON.parse(readFileSync(path, 'utf8'));
|
|
183
|
+
if (record(lock))
|
|
184
|
+
validateLockRegistries(lock, file, hosts);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export { lockRepairHint, LockRegistryError, validateLockRegistries, validateDependencyLock, } from './npm-policy.js';
|
package/dist/dev-worker.mjs
CHANGED
|
@@ -16,6 +16,31 @@ export function runDevelopmentWorker() {
|
|
|
16
16
|
let scanTimer;
|
|
17
17
|
let workspace;
|
|
18
18
|
let snapshot;
|
|
19
|
+
// Flush changes before replying to a snapshot request. Otherwise a fast build
|
|
20
|
+
// could publish a non-FaaS edit before the next periodic scan reports it.
|
|
21
|
+
function scanChanges() {
|
|
22
|
+
const current = watchSnapshot(workspace);
|
|
23
|
+
const changed = changedFiles(snapshot, current);
|
|
24
|
+
snapshot = current;
|
|
25
|
+
if (changed.length) {
|
|
26
|
+
const files = [];
|
|
27
|
+
let size = 0;
|
|
28
|
+
for (const file of changed) {
|
|
29
|
+
const bytes = Buffer.byteLength(JSON.stringify(file)) + 1;
|
|
30
|
+
if (files.length === 100 || size + bytes > 8192)
|
|
31
|
+
break;
|
|
32
|
+
files.push(file);
|
|
33
|
+
size += bytes;
|
|
34
|
+
}
|
|
35
|
+
emit({
|
|
36
|
+
event: 'changed',
|
|
37
|
+
files,
|
|
38
|
+
filesTruncated: files.length !== changed.length,
|
|
39
|
+
fingerprint: snapshotFingerprint(current),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return current;
|
|
43
|
+
}
|
|
19
44
|
const lines = createInterface({ input: process.stdin, crlfDelay: Infinity });
|
|
20
45
|
async function stop(code = 0) {
|
|
21
46
|
if (stopping)
|
|
@@ -61,7 +86,7 @@ export function runDevelopmentWorker() {
|
|
|
61
86
|
if (input.command === 'snapshot' &&
|
|
62
87
|
typeof input.requestId === 'string' &&
|
|
63
88
|
/^[A-Za-z0-9-]{1,64}$/.test(input.requestId)) {
|
|
64
|
-
const current =
|
|
89
|
+
const current = scanChanges();
|
|
65
90
|
emit({
|
|
66
91
|
event: 'snapshot',
|
|
67
92
|
requestId: input.requestId,
|
|
@@ -96,26 +121,7 @@ export function runDevelopmentWorker() {
|
|
|
96
121
|
if (!stopping)
|
|
97
122
|
scanTimer = setInterval(() => {
|
|
98
123
|
try {
|
|
99
|
-
|
|
100
|
-
const changed = changedFiles(snapshot, current);
|
|
101
|
-
snapshot = current;
|
|
102
|
-
if (changed.length) {
|
|
103
|
-
const files = [];
|
|
104
|
-
let size = 0;
|
|
105
|
-
for (const file of changed) {
|
|
106
|
-
const bytes = Buffer.byteLength(JSON.stringify(file)) + 1;
|
|
107
|
-
if (files.length === 100 || size + bytes > 8192)
|
|
108
|
-
break;
|
|
109
|
-
files.push(file);
|
|
110
|
-
size += bytes;
|
|
111
|
-
}
|
|
112
|
-
emit({
|
|
113
|
-
event: 'changed',
|
|
114
|
-
files,
|
|
115
|
-
filesTruncated: files.length !== changed.length,
|
|
116
|
-
fingerprint: snapshotFingerprint(current),
|
|
117
|
-
});
|
|
118
|
-
}
|
|
124
|
+
scanChanges();
|
|
119
125
|
}
|
|
120
126
|
catch (error) {
|
|
121
127
|
fail(error);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type FunctionHint } from './function-params.js';
|
|
2
|
+
/** Shared discovery contract for SQL, Mutation and FaaS (AI Functions are separate). */
|
|
3
|
+
export declare const FUNCTION_DESCRIPTION_GUIDANCE = "Write the description field in each listed meta.json: state what the function is for, when to use it, when not to use it and which neighbouring function to use instead, and whether it is read-only or writes data. See references/agent-tool-design.md in the lark-base-database Skill.";
|
|
4
|
+
/**
|
|
5
|
+
* Returns why description is invalid, or undefined when it satisfies the contract.
|
|
6
|
+
* Short descriptions are hints only (see functionDescriptionHint); the CLI does not
|
|
7
|
+
* compare description with name.
|
|
8
|
+
*/
|
|
9
|
+
export declare function functionDescriptionProblem(description: unknown, _name?: unknown): string | undefined;
|
|
10
|
+
/** Non-blocking hint for a valid but short Function description. */
|
|
11
|
+
export declare function functionDescriptionHint(description: unknown, path: string): FunctionHint | undefined;
|
|
12
|
+
export declare function validateFunctionDescription(description: unknown, name: unknown, path: string): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AGENT_TOOL_DESIGN_REFERENCE, SHORT_DESCRIPTION_THRESHOLD, } from './function-params.js';
|
|
2
|
+
/** Shared discovery contract for SQL, Mutation and FaaS (AI Functions are separate). */
|
|
3
|
+
export const FUNCTION_DESCRIPTION_GUIDANCE = `Write the description field in each listed meta.json: state what the function is for, when to use it, when not to use it and which neighbouring function to use instead, and whether it is read-only or writes data. See ${AGENT_TOOL_DESIGN_REFERENCE}.`;
|
|
4
|
+
/**
|
|
5
|
+
* Returns why description is invalid, or undefined when it satisfies the contract.
|
|
6
|
+
* Short descriptions are hints only (see functionDescriptionHint); the CLI does not
|
|
7
|
+
* compare description with name.
|
|
8
|
+
*/
|
|
9
|
+
export function functionDescriptionProblem(description, _name) {
|
|
10
|
+
if (description === undefined || description === null)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (typeof description !== 'string') {
|
|
13
|
+
return 'is required and must be a string';
|
|
14
|
+
}
|
|
15
|
+
if ([...description].length > 500) {
|
|
16
|
+
// Match the server limit on the actual submitted value, including whitespace.
|
|
17
|
+
return 'must contain at most 500 Unicode characters';
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
/** Non-blocking hint for a valid but short Function description. */
|
|
22
|
+
export function functionDescriptionHint(description, path) {
|
|
23
|
+
if (typeof description === 'string' &&
|
|
24
|
+
[...description.trim()].length >= SHORT_DESCRIPTION_THRESHOLD) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
path: `${path}#/description`,
|
|
29
|
+
kind: 'function_description',
|
|
30
|
+
message: `Function description is shorter than ${SHORT_DESCRIPTION_THRESHOLD} characters; state its purpose, when to use it, when not to use it and which neighbouring function to use instead. See ${AGENT_TOOL_DESIGN_REFERENCE}.`,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export function validateFunctionDescription(description, name, path) {
|
|
34
|
+
const reason = functionDescriptionProblem(description, name);
|
|
35
|
+
if (reason) {
|
|
36
|
+
throw new Error(`${path}: description ${reason}. ${FUNCTION_DESCRIPTION_GUIDANCE}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Author-declared parameter contract shared by Database Function `meta.params[]`
|
|
3
|
+
* and Mutation `spec.params[]`. The service renders the Agent-facing inputSchema
|
|
4
|
+
* from these declarations; the CLI only checks the same deterministic rules
|
|
5
|
+
* before upload and reports soft hints without blocking.
|
|
6
|
+
*/
|
|
7
|
+
export declare const AGENT_TOOL_DESIGN_REFERENCE = "references/agent-tool-design.md in the lark-base-database Skill";
|
|
8
|
+
export declare const PARAMETER_NAME_PATTERN: RegExp;
|
|
9
|
+
export declare const MAX_PARAMETER_DESCRIPTION = 500;
|
|
10
|
+
export declare const SHORT_DESCRIPTION_THRESHOLD = 8;
|
|
11
|
+
export type FunctionHintKind = 'short_description' | 'binding' | 'enum_unchecked' | 'function_description';
|
|
12
|
+
/** One non-blocking authoring hint; local hints and service `results[].hints[]` share this shape. */
|
|
13
|
+
export interface FunctionHint {
|
|
14
|
+
functionId?: string;
|
|
15
|
+
/** JSON Pointer inside the declaration, prefixed by the source file for local hints. */
|
|
16
|
+
path: string;
|
|
17
|
+
kind: string;
|
|
18
|
+
message: string;
|
|
19
|
+
}
|
|
20
|
+
export type EnumValueType = 'string' | 'integer' | 'number' | 'boolean';
|
|
21
|
+
export interface DeclaredParameterResult {
|
|
22
|
+
problems: string[];
|
|
23
|
+
hints: FunctionHint[];
|
|
24
|
+
}
|
|
25
|
+
export declare function shortDescriptionHint(path: string, subject: string): FunctionHint;
|
|
26
|
+
/**
|
|
27
|
+
* Check `description` of one declared parameter or FaaS property.
|
|
28
|
+
* Missing/blank/too long is a problem; shorter than the threshold is only a hint.
|
|
29
|
+
*/
|
|
30
|
+
export declare function checkParameterDescription(description: unknown, label: string, path: string, result: DeclaredParameterResult): void;
|
|
31
|
+
/**
|
|
32
|
+
* Validate `name`, `description` and optional `enum` of a declared parameter list.
|
|
33
|
+
* `enumType` maps one parameter to the JSON type its enum values (or array
|
|
34
|
+
* elements) must use; `undefined` means the kind does not accept enum.
|
|
35
|
+
*/
|
|
36
|
+
export declare function checkDeclaredParameters(params: readonly unknown[], options: {
|
|
37
|
+
/** Label prefix such as `meta.params` or `spec.params`. */
|
|
38
|
+
label: string;
|
|
39
|
+
/** JSON Pointer prefix such as `/meta/params`. */
|
|
40
|
+
pointer: string;
|
|
41
|
+
/** Source file used to prefix hint paths. */
|
|
42
|
+
file: string;
|
|
43
|
+
enumType: (param: Record<string, unknown>) => EnumValueType | undefined;
|
|
44
|
+
}): DeclaredParameterResult;
|
|
45
|
+
/** SQL `meta.params[].kind` → enum value type; `TEXT[]` constrains elements. */
|
|
46
|
+
export declare function sqlEnumType(param: Record<string, unknown>): EnumValueType | undefined;
|
|
47
|
+
/** Mutation `spec.params[].kind` → enum value type; `list:true` constrains elements. */
|
|
48
|
+
export declare function mutationEnumType(param: Record<string, unknown>): EnumValueType | undefined;
|
|
49
|
+
/** Format collected hints for one-line human output. */
|
|
50
|
+
export declare function formatFunctionHint(hint: FunctionHint): string;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Author-declared parameter contract shared by Database Function `meta.params[]`
|
|
3
|
+
* and Mutation `spec.params[]`. The service renders the Agent-facing inputSchema
|
|
4
|
+
* from these declarations; the CLI only checks the same deterministic rules
|
|
5
|
+
* before upload and reports soft hints without blocking.
|
|
6
|
+
*/
|
|
7
|
+
export const AGENT_TOOL_DESIGN_REFERENCE = 'references/agent-tool-design.md in the lark-base-database Skill';
|
|
8
|
+
export const PARAMETER_NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
|
|
9
|
+
export const MAX_PARAMETER_DESCRIPTION = 500;
|
|
10
|
+
export const SHORT_DESCRIPTION_THRESHOLD = 8;
|
|
11
|
+
const MAX_ENUM_ITEMS = 100;
|
|
12
|
+
const MAX_ENUM_ITEM_LENGTH = 255;
|
|
13
|
+
function codePoints(value) {
|
|
14
|
+
return [...value].length;
|
|
15
|
+
}
|
|
16
|
+
function isRecord(value) {
|
|
17
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
18
|
+
}
|
|
19
|
+
function enumValueMatches(value, type) {
|
|
20
|
+
switch (type) {
|
|
21
|
+
case 'string':
|
|
22
|
+
return typeof value === 'string';
|
|
23
|
+
case 'integer':
|
|
24
|
+
return typeof value === 'number' && Number.isSafeInteger(value);
|
|
25
|
+
case 'number':
|
|
26
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
27
|
+
case 'boolean':
|
|
28
|
+
return typeof value === 'boolean';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function shortDescriptionHint(path, subject) {
|
|
32
|
+
return {
|
|
33
|
+
path,
|
|
34
|
+
kind: 'short_description',
|
|
35
|
+
message: `${subject} description is shorter than ${SHORT_DESCRIPTION_THRESHOLD} characters; state the business meaning, unit and how the caller obtains the value. See ${AGENT_TOOL_DESIGN_REFERENCE}.`,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Check `description` of one declared parameter or FaaS property.
|
|
40
|
+
* Missing/blank/too long is a problem; shorter than the threshold is only a hint.
|
|
41
|
+
*/
|
|
42
|
+
export function checkParameterDescription(description, label, path, result) {
|
|
43
|
+
if (typeof description !== 'string' || description.trim() === '') {
|
|
44
|
+
result.problems.push(`${label}.description is required and must be a non-blank string`);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (codePoints(description) > MAX_PARAMETER_DESCRIPTION) {
|
|
48
|
+
result.problems.push(`${label}.description must contain at most ${MAX_PARAMETER_DESCRIPTION} Unicode characters`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (codePoints(description.trim()) < SHORT_DESCRIPTION_THRESHOLD) {
|
|
52
|
+
result.hints.push(shortDescriptionHint(path, label));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Validate `name`, `description` and optional `enum` of a declared parameter list.
|
|
57
|
+
* `enumType` maps one parameter to the JSON type its enum values (or array
|
|
58
|
+
* elements) must use; `undefined` means the kind does not accept enum.
|
|
59
|
+
*/
|
|
60
|
+
export function checkDeclaredParameters(params, options) {
|
|
61
|
+
const result = { problems: [], hints: [] };
|
|
62
|
+
const names = new Set();
|
|
63
|
+
params.forEach((param, index) => {
|
|
64
|
+
const label = `${options.label}[${index}]`;
|
|
65
|
+
const pointer = `${options.pointer}/${index}`;
|
|
66
|
+
if (!isRecord(param)) {
|
|
67
|
+
result.problems.push(`${label} must be an object`);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const { name } = param;
|
|
71
|
+
if (typeof name !== 'string' || name === '') {
|
|
72
|
+
result.problems.push(`${label}.name is required; give every parameter a stable name such as "periodId"`);
|
|
73
|
+
}
|
|
74
|
+
else if (!PARAMETER_NAME_PATTERN.test(name)) {
|
|
75
|
+
result.problems.push(`${label}.name must match ${PARAMETER_NAME_PATTERN.source}`);
|
|
76
|
+
}
|
|
77
|
+
else if (names.has(name)) {
|
|
78
|
+
result.problems.push(`${label}.name duplicates ${JSON.stringify(name)}; names must be unique within one Function`);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
names.add(name);
|
|
82
|
+
}
|
|
83
|
+
checkParameterDescription(param.description, label, `${options.file}#${pointer}/description`, result);
|
|
84
|
+
if (param.enum === undefined)
|
|
85
|
+
return;
|
|
86
|
+
const values = param.enum;
|
|
87
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
88
|
+
result.problems.push(`${label}.enum must be a non-empty array`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const type = options.enumType(param);
|
|
92
|
+
if (!type) {
|
|
93
|
+
result.problems.push(`${label}.enum is only supported for string, integer, number or boolean kinds (kind ${JSON.stringify(param.kind)})`);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (values.length > MAX_ENUM_ITEMS) {
|
|
97
|
+
result.problems.push(`${label}.enum must contain at most ${MAX_ENUM_ITEMS} values`);
|
|
98
|
+
}
|
|
99
|
+
const seen = new Set();
|
|
100
|
+
values.forEach((value, valueIndex) => {
|
|
101
|
+
const valueLabel = `${label}.enum[${valueIndex}]`;
|
|
102
|
+
if (value === null) {
|
|
103
|
+
result.problems.push(`${valueLabel} must not be null; nullable parameters keep their null branch without listing null in enum`);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (!enumValueMatches(value, type)) {
|
|
107
|
+
result.problems.push(`${valueLabel} must be a JSON ${type} to match kind ${JSON.stringify(param.kind)}`);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (typeof value === 'string' &&
|
|
111
|
+
codePoints(value) > MAX_ENUM_ITEM_LENGTH) {
|
|
112
|
+
result.problems.push(`${valueLabel} must contain at most ${MAX_ENUM_ITEM_LENGTH} Unicode characters`);
|
|
113
|
+
}
|
|
114
|
+
const key = JSON.stringify(value);
|
|
115
|
+
if (seen.has(key)) {
|
|
116
|
+
result.problems.push(`${valueLabel} duplicates ${key}`);
|
|
117
|
+
}
|
|
118
|
+
seen.add(key);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
const SQL_ENUM_TYPES = {
|
|
124
|
+
TEXT: 'string',
|
|
125
|
+
INT: 'integer',
|
|
126
|
+
DOUBLE: 'number',
|
|
127
|
+
BOOL: 'boolean',
|
|
128
|
+
};
|
|
129
|
+
/** SQL `meta.params[].kind` → enum value type; `TEXT[]` constrains elements. */
|
|
130
|
+
export function sqlEnumType(param) {
|
|
131
|
+
if (typeof param.kind !== 'string')
|
|
132
|
+
return undefined;
|
|
133
|
+
const scalar = param.kind.endsWith('[]')
|
|
134
|
+
? param.kind.slice(0, -2)
|
|
135
|
+
: param.kind;
|
|
136
|
+
return SQL_ENUM_TYPES[scalar];
|
|
137
|
+
}
|
|
138
|
+
const MUTATION_ENUM_TYPES = {
|
|
139
|
+
TEXT: 'string',
|
|
140
|
+
USER_ID: 'string',
|
|
141
|
+
GROUP_ID: 'string',
|
|
142
|
+
RECORD_ID: 'string',
|
|
143
|
+
INT: 'integer',
|
|
144
|
+
DOUBLE: 'number',
|
|
145
|
+
BOOL: 'boolean',
|
|
146
|
+
};
|
|
147
|
+
/** Mutation `spec.params[].kind` → enum value type; `list:true` constrains elements. */
|
|
148
|
+
export function mutationEnumType(param) {
|
|
149
|
+
return typeof param.kind === 'string'
|
|
150
|
+
? MUTATION_ENUM_TYPES[param.kind]
|
|
151
|
+
: undefined;
|
|
152
|
+
}
|
|
153
|
+
/** Format collected hints for one-line human output. */
|
|
154
|
+
export function formatFunctionHint(hint) {
|
|
155
|
+
const prefix = hint.functionId ? `${hint.functionId}: ` : '';
|
|
156
|
+
return `${prefix}${hint.path}: ${hint.message}`;
|
|
157
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type DeclaredParameterResult } from './function-params.js';
|
|
2
|
+
interface SQLParamMeta {
|
|
3
|
+
kind: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
enum?: unknown[];
|
|
6
|
+
}
|
|
7
|
+
export declare function normalizeFunctionInputSchema(value: unknown, source: string): string;
|
|
8
|
+
export declare function sqlParamSchema(param: SQLParamMeta, index: number): {
|
|
9
|
+
enum: unknown[];
|
|
10
|
+
} | {
|
|
11
|
+
enum?: undefined;
|
|
12
|
+
} | {
|
|
13
|
+
type: string[];
|
|
14
|
+
items: {
|
|
15
|
+
enum: unknown[];
|
|
16
|
+
} | {
|
|
17
|
+
enum?: undefined;
|
|
18
|
+
};
|
|
19
|
+
description?: string | undefined;
|
|
20
|
+
};
|
|
21
|
+
/** Named-route declaration projection; mirrors the server's nullable/range/enum semantics. */
|
|
22
|
+
export declare function namedSQLParamSchema(param: Record<string, unknown>, index: number): Record<string, unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* Local Page input contract for the positional `invoke_function` route.
|
|
25
|
+
* It is never uploaded: the service renders the Agent-facing named-object
|
|
26
|
+
* inputSchema from `meta.params` itself.
|
|
27
|
+
*/
|
|
28
|
+
export declare function deriveSQLInputSchema(meta: Record<string, unknown>, sql: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* FaaS discovery contract on top of the Schema subset: the root is an object and
|
|
31
|
+
* every top-level business property (its key is the parameter name) carries a
|
|
32
|
+
* non-blank description. Short descriptions are hints only.
|
|
33
|
+
*/
|
|
34
|
+
export declare function checkFaaSInputSchemaContract(value: unknown, file: string): DeclaredParameterResult;
|
|
35
|
+
export {};
|