@synapsor/dsl 0.1.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/LICENSE +201 -0
- package/README.md +47 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +62 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +411 -0
- package/examples/billing-late-fee.synapsor +52 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @synapsor/dsl
|
|
2
|
+
|
|
3
|
+
`@synapsor/dsl` is a 0.1 preview authoring layer for Synapsor contracts.
|
|
4
|
+
|
|
5
|
+
The DSL is not the source of truth. It compiles to `@synapsor/spec` JSON, and
|
|
6
|
+
the generated JSON is validated by `@synapsor/spec`.
|
|
7
|
+
|
|
8
|
+
## CLI
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
synapsor-dsl validate ./contract.synapsor
|
|
12
|
+
synapsor-dsl compile ./contract.synapsor --out ./synapsor.contract.json
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Runner also exposes:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
synapsor-runner dsl validate ./contract.synapsor
|
|
19
|
+
synapsor-runner dsl compile ./contract.synapsor --out ./synapsor.contract.json
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Supported Preview Constructs
|
|
23
|
+
|
|
24
|
+
- `CREATE AGENT CONTEXT`
|
|
25
|
+
- `CREATE CAPABILITY`
|
|
26
|
+
- `CREATE AGENT WORKFLOW`
|
|
27
|
+
- `BIND ... FROM SESSION|ENVIRONMENT|CLOUD_SESSION|STATIC_DEV|HTTP_CLAIM`
|
|
28
|
+
- `USING CONTEXT`
|
|
29
|
+
- `ON schema.table`
|
|
30
|
+
- `PRIMARY KEY`
|
|
31
|
+
- `TENANT KEY`
|
|
32
|
+
- `CONFLICT GUARD`
|
|
33
|
+
- `ARG`
|
|
34
|
+
- `LOOKUP`
|
|
35
|
+
- `ALLOW READ`
|
|
36
|
+
- `KEEP OUT`
|
|
37
|
+
- `REQUIRE EVIDENCE`
|
|
38
|
+
- `PROPOSE ACTION`
|
|
39
|
+
- `ALLOW WRITE`
|
|
40
|
+
- `PATCH`
|
|
41
|
+
- `APPROVAL ROLE`
|
|
42
|
+
- `WRITEBACK DIRECT SQL|APP HANDLER|CLOUD WORKER|NONE`
|
|
43
|
+
- workflow `ALLOW CAPABILITY`
|
|
44
|
+
|
|
45
|
+
Unsupported Cloud-generated clauses such as `ROOT EXTERNAL`,
|
|
46
|
+
`JOIN EXTERNAL`, `RETURN ANSWER WITH CITATIONS`, `AUTO BRANCH`, or `AUTO MERGE`
|
|
47
|
+
fail explicitly instead of being ignored.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { compileAgentDsl, validateAgentDsl } from "./index.js";
|
|
4
|
+
async function main(argv) {
|
|
5
|
+
const [command, target, ...rest] = argv;
|
|
6
|
+
if (!command || command === "--help" || command === "-h") {
|
|
7
|
+
usage();
|
|
8
|
+
return 0;
|
|
9
|
+
}
|
|
10
|
+
if (command !== "compile" && command !== "validate") {
|
|
11
|
+
process.stderr.write(`Unknown command: synapsor-dsl ${command}\n\n`);
|
|
12
|
+
usage();
|
|
13
|
+
return 2;
|
|
14
|
+
}
|
|
15
|
+
if (!target) {
|
|
16
|
+
process.stderr.write(`synapsor-dsl ${command} requires <contract.synapsor>\n`);
|
|
17
|
+
return 2;
|
|
18
|
+
}
|
|
19
|
+
const source = await fs.readFile(target, "utf8");
|
|
20
|
+
if (command === "validate") {
|
|
21
|
+
const result = validateAgentDsl(source);
|
|
22
|
+
if (rest.includes("--json"))
|
|
23
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
24
|
+
else if (result.ok)
|
|
25
|
+
process.stdout.write(`dsl valid: ${target}\n`);
|
|
26
|
+
else {
|
|
27
|
+
process.stdout.write(`dsl invalid: ${target}\n`);
|
|
28
|
+
for (const error of result.errors)
|
|
29
|
+
process.stdout.write(`error ${error.line}:${error.column} ${error.code}: ${error.message}\n`);
|
|
30
|
+
}
|
|
31
|
+
return result.ok ? 0 : 1;
|
|
32
|
+
}
|
|
33
|
+
const contract = compileAgentDsl(source);
|
|
34
|
+
const text = `${JSON.stringify(contract, null, 2)}\n`;
|
|
35
|
+
const output = option(rest, "--out") ?? option(rest, "--output");
|
|
36
|
+
if (output) {
|
|
37
|
+
await fs.writeFile(output, text, "utf8");
|
|
38
|
+
process.stdout.write(`wrote contract: ${output}\n`);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
process.stdout.write(text);
|
|
42
|
+
}
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
function option(args, name) {
|
|
46
|
+
const index = args.indexOf(name);
|
|
47
|
+
return index === -1 ? undefined : args[index + 1];
|
|
48
|
+
}
|
|
49
|
+
function usage() {
|
|
50
|
+
process.stdout.write(`Synapsor DSL
|
|
51
|
+
|
|
52
|
+
Usage:
|
|
53
|
+
synapsor-dsl validate ./contract.synapsor
|
|
54
|
+
synapsor-dsl compile ./contract.synapsor --out ./synapsor.contract.json
|
|
55
|
+
`);
|
|
56
|
+
}
|
|
57
|
+
main(process.argv.slice(2)).then((code) => {
|
|
58
|
+
process.exitCode = code;
|
|
59
|
+
}).catch((error) => {
|
|
60
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
61
|
+
process.exitCode = 1;
|
|
62
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type SynapsorContract } from "@synapsor/spec";
|
|
2
|
+
export type AgentDslAst = {
|
|
3
|
+
contexts: AgentDslContextAst[];
|
|
4
|
+
capabilities: AgentDslCapabilityAst[];
|
|
5
|
+
workflows: AgentDslWorkflowAst[];
|
|
6
|
+
};
|
|
7
|
+
export type AgentDslContextAst = {
|
|
8
|
+
name: string;
|
|
9
|
+
bindings: Array<{
|
|
10
|
+
name: string;
|
|
11
|
+
source: "session" | "environment" | "cloud_session" | "static_dev" | "http_claim";
|
|
12
|
+
key: string;
|
|
13
|
+
required?: boolean;
|
|
14
|
+
}>;
|
|
15
|
+
tenantBinding?: string;
|
|
16
|
+
principalBinding?: string;
|
|
17
|
+
};
|
|
18
|
+
export type AgentDslCapabilityAst = {
|
|
19
|
+
name: string;
|
|
20
|
+
kind: "read" | "proposal";
|
|
21
|
+
context: string;
|
|
22
|
+
source?: string;
|
|
23
|
+
schema: string;
|
|
24
|
+
table: string;
|
|
25
|
+
primaryKey: string;
|
|
26
|
+
tenantKey?: string;
|
|
27
|
+
conflictKey?: string;
|
|
28
|
+
lookup?: {
|
|
29
|
+
arg: string;
|
|
30
|
+
column: string;
|
|
31
|
+
};
|
|
32
|
+
args: Record<string, {
|
|
33
|
+
type: "string" | "number" | "boolean";
|
|
34
|
+
required?: boolean;
|
|
35
|
+
max_length?: number;
|
|
36
|
+
}>;
|
|
37
|
+
visibleFields: string[];
|
|
38
|
+
keptOutFields: string[];
|
|
39
|
+
evidenceRequired?: boolean;
|
|
40
|
+
maxRows?: number;
|
|
41
|
+
proposal?: {
|
|
42
|
+
action: string;
|
|
43
|
+
allowedFields: string[];
|
|
44
|
+
patch: Record<string, {
|
|
45
|
+
fixed?: string | number | boolean | null;
|
|
46
|
+
from_arg?: string;
|
|
47
|
+
}>;
|
|
48
|
+
approvalRole?: string;
|
|
49
|
+
writebackMode?: "direct_sql" | "app_handler" | "cloud_worker" | "none";
|
|
50
|
+
executor?: string;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export type AgentDslWorkflowAst = {
|
|
54
|
+
name: string;
|
|
55
|
+
context: string;
|
|
56
|
+
allowedCapabilities: string[];
|
|
57
|
+
requiredEvidence?: boolean;
|
|
58
|
+
approvalRole?: string;
|
|
59
|
+
checkpoint?: "none" | "every_step" | "proposal_only";
|
|
60
|
+
};
|
|
61
|
+
export type ValidationResult = {
|
|
62
|
+
ok: boolean;
|
|
63
|
+
errors: Array<{
|
|
64
|
+
line: number;
|
|
65
|
+
column: number;
|
|
66
|
+
code: string;
|
|
67
|
+
message: string;
|
|
68
|
+
}>;
|
|
69
|
+
warnings: Array<{
|
|
70
|
+
line: number;
|
|
71
|
+
column: number;
|
|
72
|
+
code: string;
|
|
73
|
+
message: string;
|
|
74
|
+
}>;
|
|
75
|
+
};
|
|
76
|
+
export declare class AgentDslError extends Error {
|
|
77
|
+
readonly line: number;
|
|
78
|
+
readonly column: number;
|
|
79
|
+
readonly code: string;
|
|
80
|
+
constructor(line: number, column: number, code: string, message: string);
|
|
81
|
+
}
|
|
82
|
+
export declare function parseAgentDsl(source: string): AgentDslAst;
|
|
83
|
+
export declare function compileAgentDsl(source: string): SynapsorContract;
|
|
84
|
+
export declare function validateAgentDsl(source: string): ValidationResult;
|
|
85
|
+
export declare function formatAgentDsl(source: string): string;
|
|
86
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import { assertValidContract, normalizeContract } from "@synapsor/spec";
|
|
2
|
+
export class AgentDslError extends Error {
|
|
3
|
+
line;
|
|
4
|
+
column;
|
|
5
|
+
code;
|
|
6
|
+
constructor(line, column, code, message) {
|
|
7
|
+
super(`${line}:${column} ${code}: ${message}`);
|
|
8
|
+
this.line = line;
|
|
9
|
+
this.column = column;
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.name = "AgentDslError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function parseAgentDsl(source) {
|
|
15
|
+
const blocks = parseBlocks(source);
|
|
16
|
+
const contexts = [];
|
|
17
|
+
const capabilities = [];
|
|
18
|
+
const workflows = [];
|
|
19
|
+
for (const block of blocks) {
|
|
20
|
+
if (block.kind === "context")
|
|
21
|
+
contexts.push(parseContextBlock(block));
|
|
22
|
+
if (block.kind === "capability")
|
|
23
|
+
capabilities.push(parseCapabilityBlock(block));
|
|
24
|
+
if (block.kind === "workflow")
|
|
25
|
+
workflows.push(parseWorkflowBlock(block));
|
|
26
|
+
}
|
|
27
|
+
return { contexts, capabilities, workflows };
|
|
28
|
+
}
|
|
29
|
+
export function compileAgentDsl(source) {
|
|
30
|
+
const ast = parseAgentDsl(source);
|
|
31
|
+
const contexts = ast.contexts.map((context) => ({
|
|
32
|
+
name: context.name,
|
|
33
|
+
bindings: context.bindings,
|
|
34
|
+
...(context.tenantBinding ? { tenant_binding: context.tenantBinding } : {}),
|
|
35
|
+
...(context.principalBinding ? { principal_binding: context.principalBinding } : {}),
|
|
36
|
+
}));
|
|
37
|
+
const capabilities = ast.capabilities.map((capability) => {
|
|
38
|
+
const spec = {
|
|
39
|
+
name: capability.name,
|
|
40
|
+
kind: capability.kind,
|
|
41
|
+
context: capability.context,
|
|
42
|
+
...(capability.source ? { source: capability.source } : {}),
|
|
43
|
+
subject: {
|
|
44
|
+
schema: capability.schema,
|
|
45
|
+
table: capability.table,
|
|
46
|
+
primary_key: capability.primaryKey,
|
|
47
|
+
...(capability.tenantKey ? { tenant_key: capability.tenantKey } : {}),
|
|
48
|
+
...(capability.conflictKey ? { conflict_key: capability.conflictKey } : {}),
|
|
49
|
+
},
|
|
50
|
+
args: capability.args,
|
|
51
|
+
...(capability.lookup ? { lookup: { id_from_arg: capability.lookup.arg } } : {}),
|
|
52
|
+
visible_fields: capability.visibleFields,
|
|
53
|
+
...(capability.keptOutFields.length ? { kept_out_fields: capability.keptOutFields } : {}),
|
|
54
|
+
...(capability.evidenceRequired !== undefined ? { evidence: { required: capability.evidenceRequired, query_audit: true } } : {}),
|
|
55
|
+
...(capability.maxRows ? { max_rows: capability.maxRows } : {}),
|
|
56
|
+
};
|
|
57
|
+
if (capability.kind === "proposal" && capability.proposal) {
|
|
58
|
+
spec.proposal = {
|
|
59
|
+
action: capability.proposal.action,
|
|
60
|
+
allowed_fields: capability.proposal.allowedFields,
|
|
61
|
+
patch: capability.proposal.patch,
|
|
62
|
+
conflict_guard: capability.conflictKey ? { column: capability.conflictKey } : { weak_guard_ack: true },
|
|
63
|
+
approval: { mode: "human", required_role: capability.proposal.approvalRole ?? "local_reviewer" },
|
|
64
|
+
writeback: {
|
|
65
|
+
mode: capability.proposal.writebackMode ?? "direct_sql",
|
|
66
|
+
...(capability.proposal.executor ? { executor: capability.proposal.executor } : {}),
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return spec;
|
|
71
|
+
});
|
|
72
|
+
const workflows = ast.workflows.map((workflow) => ({
|
|
73
|
+
name: workflow.name,
|
|
74
|
+
context: workflow.context,
|
|
75
|
+
allowed_capabilities: workflow.allowedCapabilities,
|
|
76
|
+
...(workflow.requiredEvidence !== undefined ? { required_evidence: workflow.requiredEvidence } : {}),
|
|
77
|
+
...(workflow.approvalRole ? { approval: { required: true, role: workflow.approvalRole } } : {}),
|
|
78
|
+
...(workflow.checkpoint ? { replay: { checkpoint: workflow.checkpoint } } : {}),
|
|
79
|
+
}));
|
|
80
|
+
const contract = {
|
|
81
|
+
spec_version: "0.1",
|
|
82
|
+
kind: "SynapsorContract",
|
|
83
|
+
contexts,
|
|
84
|
+
capabilities,
|
|
85
|
+
...(workflows.length ? { workflows } : {}),
|
|
86
|
+
};
|
|
87
|
+
assertValidContract(contract);
|
|
88
|
+
return normalizeContract(contract);
|
|
89
|
+
}
|
|
90
|
+
export function validateAgentDsl(source) {
|
|
91
|
+
try {
|
|
92
|
+
compileAgentDsl(source);
|
|
93
|
+
return { ok: true, errors: [], warnings: [] };
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (error instanceof AgentDslError) {
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
errors: [{ line: error.line, column: error.column, code: error.code, message: error.message.replace(/^\d+:\d+ [A-Z_]+: /, "") }],
|
|
100
|
+
warnings: [],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
errors: [{ line: 1, column: 1, code: "DSL_VALIDATION_FAILED", message: error instanceof Error ? error.message : String(error) }],
|
|
106
|
+
warnings: [],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export function formatAgentDsl(source) {
|
|
111
|
+
return source.split(/\r?\n/).map((line) => line.trim()).filter((line) => line && !line.startsWith("--")).join("\n");
|
|
112
|
+
}
|
|
113
|
+
function parseBlocks(source) {
|
|
114
|
+
const lines = source.split(/\r?\n/);
|
|
115
|
+
const blocks = [];
|
|
116
|
+
let current;
|
|
117
|
+
lines.forEach((rawLine, index) => {
|
|
118
|
+
const lineNumber = index + 1;
|
|
119
|
+
const stripped = rawLine.replace(/--.*$/, "").trim();
|
|
120
|
+
if (!stripped)
|
|
121
|
+
return;
|
|
122
|
+
const contextMatch = stripped.match(/^CREATE\s+AGENT\s+CONTEXT\s+([A-Za-z_][A-Za-z0-9_.]*)$/i);
|
|
123
|
+
const capabilityMatch = stripped.match(/^CREATE\s+(?:AGENT\s+)?CAPABILITY\s+([A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
124
|
+
const workflowMatch = stripped.match(/^CREATE\s+AGENT\s+WORKFLOW\s+([A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
125
|
+
if (contextMatch || capabilityMatch || workflowMatch) {
|
|
126
|
+
if (current)
|
|
127
|
+
throw dslError(lineNumber, 1, "BLOCK_NOT_CLOSED", "previous CREATE block must end with END before starting another block");
|
|
128
|
+
if (contextMatch?.[1])
|
|
129
|
+
current = { kind: "context", name: contextMatch[1], line: lineNumber, body: [] };
|
|
130
|
+
else if (capabilityMatch?.[1])
|
|
131
|
+
current = { kind: "capability", name: capabilityMatch[1], line: lineNumber, body: [] };
|
|
132
|
+
else if (workflowMatch?.[1])
|
|
133
|
+
current = { kind: "workflow", name: workflowMatch[1], line: lineNumber, body: [] };
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (/^END;?$/i.test(stripped)) {
|
|
137
|
+
if (!current)
|
|
138
|
+
throw dslError(lineNumber, 1, "END_WITHOUT_BLOCK", "END appeared without an open CREATE block");
|
|
139
|
+
blocks.push(current);
|
|
140
|
+
current = undefined;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (!current)
|
|
144
|
+
throw dslError(lineNumber, 1, "EXPECTED_CREATE", "expected CREATE AGENT CONTEXT, CREATE CAPABILITY, or CREATE AGENT WORKFLOW");
|
|
145
|
+
current.body.push({ text: stripped.replace(/;$/, ""), line: lineNumber });
|
|
146
|
+
});
|
|
147
|
+
if (current)
|
|
148
|
+
throw dslError(current.line, 1, "BLOCK_NOT_CLOSED", `${current.name} must end with END`);
|
|
149
|
+
return blocks;
|
|
150
|
+
}
|
|
151
|
+
function parseContextBlock(block) {
|
|
152
|
+
const context = { name: block.name, bindings: [] };
|
|
153
|
+
for (const item of block.body) {
|
|
154
|
+
const bind = item.text.match(/^BIND\s+([A-Za-z_][A-Za-z0-9_]*)\s+FROM\s+(SESSION|ENV|ENVIRONMENT|CLOUD_SESSION|STATIC_DEV|HTTP_CLAIM)\s+([A-Za-z0-9_.-]+)(?:\s+REQUIRED)?$/i);
|
|
155
|
+
if (bind?.[1] && bind[2] && bind[3]) {
|
|
156
|
+
const name = bind[1];
|
|
157
|
+
const source = normalizeBindingSource(bind[2]);
|
|
158
|
+
context.bindings.push({ name, source, key: bind[3], ...(item.text.match(/\sREQUIRED$/i) ? { required: true } : {}) });
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const tenant = item.text.match(/^TENANT\s+BINDING\s+([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
162
|
+
if (tenant?.[1]) {
|
|
163
|
+
context.tenantBinding = tenant[1];
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const principal = item.text.match(/^PRINCIPAL\s+BINDING\s+([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
167
|
+
if (principal?.[1]) {
|
|
168
|
+
context.principalBinding = principal[1];
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
unsupported(item, "context");
|
|
172
|
+
}
|
|
173
|
+
if (context.bindings.length === 0)
|
|
174
|
+
throw dslError(block.line, 1, "CONTEXT_BINDINGS_REQUIRED", "CREATE AGENT CONTEXT requires at least one BIND line");
|
|
175
|
+
context.tenantBinding ??= context.bindings.find((binding) => binding.name === "tenant_id")?.name;
|
|
176
|
+
context.principalBinding ??= context.bindings.find((binding) => binding.name === "principal")?.name;
|
|
177
|
+
return context;
|
|
178
|
+
}
|
|
179
|
+
function parseCapabilityBlock(block) {
|
|
180
|
+
const capability = {
|
|
181
|
+
name: block.name,
|
|
182
|
+
kind: "read",
|
|
183
|
+
context: "",
|
|
184
|
+
schema: "",
|
|
185
|
+
table: "",
|
|
186
|
+
primaryKey: "id",
|
|
187
|
+
args: {},
|
|
188
|
+
visibleFields: [],
|
|
189
|
+
keptOutFields: [],
|
|
190
|
+
};
|
|
191
|
+
for (const item of block.body) {
|
|
192
|
+
const context = item.text.match(/^USING\s+CONTEXT\s+([A-Za-z_][A-Za-z0-9_.]*)$/i);
|
|
193
|
+
if (context?.[1]) {
|
|
194
|
+
capability.context = context[1];
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const source = item.text.match(/^SOURCE\s+([A-Za-z_][A-Za-z0-9_.-]*)$/i);
|
|
198
|
+
if (source?.[1]) {
|
|
199
|
+
capability.source = source[1];
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const on = item.text.match(/^ON\s+([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
203
|
+
if (on?.[1] && on[2]) {
|
|
204
|
+
capability.schema = on[1];
|
|
205
|
+
capability.table = on[2];
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const primary = item.text.match(/^PRIMARY\s+KEY\s+([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
209
|
+
if (primary?.[1]) {
|
|
210
|
+
capability.primaryKey = primary[1];
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const tenant = item.text.match(/^TENANT\s+KEY\s+([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
214
|
+
if (tenant?.[1]) {
|
|
215
|
+
capability.tenantKey = tenant[1];
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const conflict = item.text.match(/^CONFLICT\s+GUARD\s+([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
219
|
+
if (conflict?.[1]) {
|
|
220
|
+
capability.conflictKey = conflict[1];
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const arg = item.text.match(/^ARG\s+([A-Za-z_][A-Za-z0-9_]*)\s+(STRING|TEXT|NUMBER|BOOLEAN|BOOL)(?:\s+REQUIRED)?(?:\s+MAX\s+(\d+))?$/i);
|
|
224
|
+
if (arg?.[1] && arg[2]) {
|
|
225
|
+
capability.args[arg[1]] = {
|
|
226
|
+
type: normalizeArgType(arg[2]),
|
|
227
|
+
...(item.text.match(/\sREQUIRED(?:\s|$)/i) ? { required: true } : {}),
|
|
228
|
+
...(arg[3] ? { max_length: Number(arg[3]) } : {}),
|
|
229
|
+
};
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const lookup = item.text.match(/^LOOKUP\s+([A-Za-z_][A-Za-z0-9_]*)\s+BY\s+([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
233
|
+
if (lookup?.[1] && lookup[2]) {
|
|
234
|
+
capability.lookup = { arg: lookup[1], column: lookup[2] };
|
|
235
|
+
capability.primaryKey = lookup[2];
|
|
236
|
+
if (!capability.args[lookup[1]])
|
|
237
|
+
capability.args[lookup[1]] = { type: "string", required: true, max_length: 128 };
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const read = item.text.match(/^ALLOW\s+READ\s+(.+)$/i);
|
|
241
|
+
if (read?.[1]) {
|
|
242
|
+
capability.visibleFields = parseList(read[1]);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const keptOut = item.text.match(/^KEEP\s+OUT\s+(.+)$/i);
|
|
246
|
+
if (keptOut?.[1]) {
|
|
247
|
+
capability.keptOutFields = parseList(keptOut[1]);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (/^REQUIRE\s+EVIDENCE$/i.test(item.text)) {
|
|
251
|
+
capability.evidenceRequired = true;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const maxRows = item.text.match(/^MAX\s+ROWS\s+(\d+)$/i);
|
|
255
|
+
if (maxRows?.[1]) {
|
|
256
|
+
capability.maxRows = Number(maxRows[1]);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const propose = item.text.match(/^PROPOSE\s+ACTION\s+([A-Za-z_][A-Za-z0-9_.]*)$/i);
|
|
260
|
+
if (propose?.[1]) {
|
|
261
|
+
capability.kind = "proposal";
|
|
262
|
+
capability.proposal = { action: propose[1], allowedFields: [], patch: {} };
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const allowWrite = item.text.match(/^ALLOW\s+WRITE\s+(.+)$/i);
|
|
266
|
+
if (allowWrite?.[1]) {
|
|
267
|
+
ensureProposal(capability, item);
|
|
268
|
+
capability.proposal.allowedFields = parseList(allowWrite[1]);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const patch = item.text.match(/^PATCH\s+([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.+)$/i);
|
|
272
|
+
if (patch?.[1] && patch[2]) {
|
|
273
|
+
ensureProposal(capability, item);
|
|
274
|
+
capability.proposal.patch[patch[1]] = parsePatchBinding(patch[2]);
|
|
275
|
+
if (!capability.proposal.allowedFields.includes(patch[1]))
|
|
276
|
+
capability.proposal.allowedFields.push(patch[1]);
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const approval = item.text.match(/^APPROVAL\s+ROLE\s+([A-Za-z_][A-Za-z0-9_.-]*)$/i);
|
|
280
|
+
if (approval?.[1]) {
|
|
281
|
+
ensureProposal(capability, item);
|
|
282
|
+
capability.proposal.approvalRole = approval[1];
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const writeback = item.text.match(/^WRITEBACK\s+(DIRECT\s+SQL|APP\s+HANDLER|CLOUD\s+WORKER|NONE)(?:\s+EXECUTOR\s+([A-Za-z_][A-Za-z0-9_.-]*))?$/i);
|
|
286
|
+
if (writeback?.[1]) {
|
|
287
|
+
ensureProposal(capability, item);
|
|
288
|
+
capability.proposal.writebackMode = normalizeWritebackMode(writeback[1]);
|
|
289
|
+
if (writeback[2])
|
|
290
|
+
capability.proposal.executor = writeback[2];
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
unsupported(item, "capability");
|
|
294
|
+
}
|
|
295
|
+
if (!capability.context)
|
|
296
|
+
throw dslError(block.line, 1, "CAPABILITY_CONTEXT_REQUIRED", `${block.name} requires USING CONTEXT`);
|
|
297
|
+
if (!capability.schema || !capability.table)
|
|
298
|
+
throw dslError(block.line, 1, "CAPABILITY_SUBJECT_REQUIRED", `${block.name} requires ON schema.table`);
|
|
299
|
+
if (!capability.tenantKey)
|
|
300
|
+
throw dslError(block.line, 1, "CAPABILITY_TENANT_REQUIRED", `${block.name} requires TENANT KEY for 0.1 DSL`);
|
|
301
|
+
if (capability.visibleFields.length === 0)
|
|
302
|
+
throw dslError(block.line, 1, "CAPABILITY_VISIBLE_FIELDS_REQUIRED", `${block.name} requires ALLOW READ`);
|
|
303
|
+
if (Object.keys(capability.args).length === 0 && capability.lookup)
|
|
304
|
+
capability.args[capability.lookup.arg] = { type: "string", required: true, max_length: 128 };
|
|
305
|
+
if (Object.keys(capability.args).length === 0)
|
|
306
|
+
throw dslError(block.line, 1, "CAPABILITY_ARGS_REQUIRED", `${block.name} requires ARG or LOOKUP`);
|
|
307
|
+
if (capability.kind === "proposal" && (!capability.proposal || Object.keys(capability.proposal.patch).length === 0))
|
|
308
|
+
throw dslError(block.line, 1, "PROPOSAL_PATCH_REQUIRED", `${block.name} proposal requires at least one PATCH line`);
|
|
309
|
+
return capability;
|
|
310
|
+
}
|
|
311
|
+
function parseWorkflowBlock(block) {
|
|
312
|
+
const workflow = { name: block.name, context: "", allowedCapabilities: [] };
|
|
313
|
+
for (const item of block.body) {
|
|
314
|
+
const context = item.text.match(/^USING\s+CONTEXT\s+([A-Za-z_][A-Za-z0-9_.]*)$/i);
|
|
315
|
+
if (context?.[1]) {
|
|
316
|
+
workflow.context = context[1];
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const capability = item.text.match(/^ALLOW\s+CAPABILITY\s+([A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
320
|
+
if (capability?.[1]) {
|
|
321
|
+
workflow.allowedCapabilities.push(capability[1]);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
if (/^REQUIRE\s+EVIDENCE$/i.test(item.text)) {
|
|
325
|
+
workflow.requiredEvidence = true;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
const approval = item.text.match(/^APPROVAL\s+REQUIRED\s+ROLE\s+([A-Za-z_][A-Za-z0-9_.-]*)$/i);
|
|
329
|
+
if (approval?.[1]) {
|
|
330
|
+
workflow.approvalRole = approval[1];
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
const checkpoint = item.text.match(/^CHECKPOINT\s+(NONE|EVERY\s+STEP|PROPOSAL\s+ONLY)$/i);
|
|
334
|
+
if (checkpoint?.[1]) {
|
|
335
|
+
workflow.checkpoint = checkpoint[1].toLowerCase().replace(/\s+/g, "_");
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
unsupported(item, "workflow");
|
|
339
|
+
}
|
|
340
|
+
if (!workflow.context)
|
|
341
|
+
throw dslError(block.line, 1, "WORKFLOW_CONTEXT_REQUIRED", `${block.name} requires USING CONTEXT`);
|
|
342
|
+
if (workflow.allowedCapabilities.length === 0)
|
|
343
|
+
throw dslError(block.line, 1, "WORKFLOW_CAPABILITIES_REQUIRED", `${block.name} requires ALLOW CAPABILITY`);
|
|
344
|
+
return workflow;
|
|
345
|
+
}
|
|
346
|
+
function ensureProposal(capability, item) {
|
|
347
|
+
if (!capability.proposal)
|
|
348
|
+
throw dslError(item.line, 1, "PROPOSAL_ACTION_REQUIRED", "proposal clauses require PROPOSE ACTION first");
|
|
349
|
+
}
|
|
350
|
+
function parsePatchBinding(raw) {
|
|
351
|
+
const trimmed = raw.trim();
|
|
352
|
+
const arg = trimmed.match(/^ARG\s+([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
353
|
+
if (arg?.[1])
|
|
354
|
+
return { from_arg: arg[1] };
|
|
355
|
+
if (/^NULL$/i.test(trimmed))
|
|
356
|
+
return { fixed: null };
|
|
357
|
+
if (/^TRUE$/i.test(trimmed))
|
|
358
|
+
return { fixed: true };
|
|
359
|
+
if (/^FALSE$/i.test(trimmed))
|
|
360
|
+
return { fixed: false };
|
|
361
|
+
if (/^-?\d+(?:\.\d+)?$/.test(trimmed))
|
|
362
|
+
return { fixed: Number(trimmed) };
|
|
363
|
+
const quoted = trimmed.match(/^'(.*)'$/);
|
|
364
|
+
if (quoted)
|
|
365
|
+
return { fixed: quoted[1] ?? "" };
|
|
366
|
+
return { fixed: trimmed };
|
|
367
|
+
}
|
|
368
|
+
function normalizeBindingSource(source) {
|
|
369
|
+
const normalized = source.toUpperCase();
|
|
370
|
+
if (normalized === "ENV")
|
|
371
|
+
return "environment";
|
|
372
|
+
if (normalized === "ENVIRONMENT")
|
|
373
|
+
return "environment";
|
|
374
|
+
if (normalized === "SESSION")
|
|
375
|
+
return "session";
|
|
376
|
+
if (normalized === "CLOUD_SESSION")
|
|
377
|
+
return "cloud_session";
|
|
378
|
+
if (normalized === "STATIC_DEV")
|
|
379
|
+
return "static_dev";
|
|
380
|
+
return "http_claim";
|
|
381
|
+
}
|
|
382
|
+
function normalizeArgType(type) {
|
|
383
|
+
const normalized = type.toUpperCase();
|
|
384
|
+
if (normalized === "NUMBER")
|
|
385
|
+
return "number";
|
|
386
|
+
if (normalized === "BOOLEAN" || normalized === "BOOL")
|
|
387
|
+
return "boolean";
|
|
388
|
+
return "string";
|
|
389
|
+
}
|
|
390
|
+
function normalizeWritebackMode(mode) {
|
|
391
|
+
const normalized = mode.toUpperCase().replace(/\s+/g, "_");
|
|
392
|
+
if (normalized === "DIRECT_SQL")
|
|
393
|
+
return "direct_sql";
|
|
394
|
+
if (normalized === "APP_HANDLER")
|
|
395
|
+
return "app_handler";
|
|
396
|
+
if (normalized === "CLOUD_WORKER")
|
|
397
|
+
return "cloud_worker";
|
|
398
|
+
return "none";
|
|
399
|
+
}
|
|
400
|
+
function parseList(value) {
|
|
401
|
+
return value.split(",").map((item) => item.trim()).filter(Boolean);
|
|
402
|
+
}
|
|
403
|
+
function unsupported(item, blockKind) {
|
|
404
|
+
if (/ROOT\s+EXTERNAL|JOIN\s+EXTERNAL|RETURN\s+ANSWER|AUTO\s+BRANCH|AUTO\s+MERGE/i.test(item.text)) {
|
|
405
|
+
throw dslError(item.line, 1, "UNSUPPORTED_PREVIEW_SYNTAX", `${blockKind} clause is not supported by @synapsor/dsl 0.1 preview: ${item.text}`);
|
|
406
|
+
}
|
|
407
|
+
throw dslError(item.line, 1, "UNSUPPORTED_DSL_CLAUSE", `unsupported ${blockKind} clause: ${item.text}`);
|
|
408
|
+
}
|
|
409
|
+
function dslError(line, column, code, message) {
|
|
410
|
+
return new AgentDslError(line, column, code, message);
|
|
411
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
CREATE AGENT CONTEXT local_operator
|
|
2
|
+
BIND tenant_id FROM ENVIRONMENT SYNAPSOR_TENANT_ID REQUIRED
|
|
3
|
+
BIND principal FROM ENVIRONMENT SYNAPSOR_PRINCIPAL REQUIRED
|
|
4
|
+
TENANT BINDING tenant_id
|
|
5
|
+
PRINCIPAL BINDING principal
|
|
6
|
+
END
|
|
7
|
+
|
|
8
|
+
CREATE CAPABILITY billing.inspect_invoice
|
|
9
|
+
USING CONTEXT local_operator
|
|
10
|
+
SOURCE local_postgres
|
|
11
|
+
ON public.invoices
|
|
12
|
+
PRIMARY KEY id
|
|
13
|
+
TENANT KEY tenant_id
|
|
14
|
+
CONFLICT GUARD updated_at
|
|
15
|
+
LOOKUP invoice_id BY id
|
|
16
|
+
ARG invoice_id STRING REQUIRED MAX 128
|
|
17
|
+
ALLOW READ id, tenant_id, customer_id, status, late_fee_cents, waiver_reason, updated_at
|
|
18
|
+
KEEP OUT card_token, internal_risk_score
|
|
19
|
+
REQUIRE EVIDENCE
|
|
20
|
+
MAX ROWS 1
|
|
21
|
+
END
|
|
22
|
+
|
|
23
|
+
CREATE CAPABILITY billing.propose_late_fee_waiver
|
|
24
|
+
USING CONTEXT local_operator
|
|
25
|
+
SOURCE local_postgres
|
|
26
|
+
ON public.invoices
|
|
27
|
+
PRIMARY KEY id
|
|
28
|
+
TENANT KEY tenant_id
|
|
29
|
+
CONFLICT GUARD updated_at
|
|
30
|
+
LOOKUP invoice_id BY id
|
|
31
|
+
ARG invoice_id STRING REQUIRED MAX 128
|
|
32
|
+
ARG waiver_reason TEXT REQUIRED MAX 500
|
|
33
|
+
ALLOW READ id, tenant_id, customer_id, status, late_fee_cents, waiver_reason, updated_at
|
|
34
|
+
KEEP OUT card_token, internal_risk_score
|
|
35
|
+
REQUIRE EVIDENCE
|
|
36
|
+
MAX ROWS 1
|
|
37
|
+
PROPOSE ACTION billing.waive_late_fee
|
|
38
|
+
ALLOW WRITE late_fee_cents, waiver_reason
|
|
39
|
+
PATCH late_fee_cents = 0
|
|
40
|
+
PATCH waiver_reason = ARG waiver_reason
|
|
41
|
+
APPROVAL ROLE billing_lead
|
|
42
|
+
WRITEBACK DIRECT SQL
|
|
43
|
+
END
|
|
44
|
+
|
|
45
|
+
CREATE AGENT WORKFLOW billing.late_fee_review
|
|
46
|
+
USING CONTEXT local_operator
|
|
47
|
+
ALLOW CAPABILITY billing.inspect_invoice
|
|
48
|
+
ALLOW CAPABILITY billing.propose_late_fee_waiver
|
|
49
|
+
REQUIRE EVIDENCE
|
|
50
|
+
APPROVAL REQUIRED ROLE billing_lead
|
|
51
|
+
CHECKPOINT PROPOSAL ONLY
|
|
52
|
+
END
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@synapsor/dsl",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Preview SQL-like Synapsor authoring DSL compiler for canonical @synapsor/spec contracts.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"synapsor-dsl": "dist/cli.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/**",
|
|
14
|
+
"examples/**",
|
|
15
|
+
"README.md",
|
|
16
|
+
"package.json"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@synapsor/spec": "^0.1.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^5.7.0",
|
|
23
|
+
"vitest": "^3.2.0"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"synapsor",
|
|
27
|
+
"dsl",
|
|
28
|
+
"ai-agents",
|
|
29
|
+
"mcp",
|
|
30
|
+
"database-safety"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22.5.0"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc -b",
|
|
40
|
+
"test": "vitest run"
|
|
41
|
+
}
|
|
42
|
+
}
|