agentlang 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/LICENSE +83 -0
  2. package/README.md +120 -0
  3. package/bin/cli.js +4 -0
  4. package/out/api/http.d.ts +3 -0
  5. package/out/api/http.d.ts.map +1 -0
  6. package/out/api/http.js +290 -0
  7. package/out/api/http.js.map +1 -0
  8. package/out/cli/cli-util.d.ts +7 -0
  9. package/out/cli/cli-util.d.ts.map +1 -0
  10. package/out/cli/cli-util.js +9 -0
  11. package/out/cli/cli-util.js.map +1 -0
  12. package/out/cli/docs.d.ts +2 -0
  13. package/out/cli/docs.d.ts.map +1 -0
  14. package/out/cli/docs.js +236 -0
  15. package/out/cli/docs.js.map +1 -0
  16. package/out/cli/main.d.ts +288 -0
  17. package/out/cli/main.d.ts.map +1 -0
  18. package/out/cli/main.js +119 -0
  19. package/out/cli/main.js.map +1 -0
  20. package/out/cli/openapi-docs.yml +695 -0
  21. package/out/extension/main.cjs +18093 -0
  22. package/out/extension/main.cjs.map +7 -0
  23. package/out/extension/main.d.ts +4 -0
  24. package/out/extension/main.d.ts.map +1 -0
  25. package/out/extension/main.js +42 -0
  26. package/out/extension/main.js.map +1 -0
  27. package/out/language/agentlang-module.d.ts +42 -0
  28. package/out/language/agentlang-module.d.ts.map +1 -0
  29. package/out/language/agentlang-module.js +42 -0
  30. package/out/language/agentlang-module.js.map +1 -0
  31. package/out/language/agentlang-validator.d.ts +15 -0
  32. package/out/language/agentlang-validator.d.ts.map +1 -0
  33. package/out/language/agentlang-validator.js +50 -0
  34. package/out/language/agentlang-validator.js.map +1 -0
  35. package/out/language/generated/ast.d.ts +491 -0
  36. package/out/language/generated/ast.d.ts.map +1 -0
  37. package/out/language/generated/ast.js +934 -0
  38. package/out/language/generated/ast.js.map +1 -0
  39. package/out/language/generated/grammar.d.ts +7 -0
  40. package/out/language/generated/grammar.d.ts.map +1 -0
  41. package/out/language/generated/grammar.js +4475 -0
  42. package/out/language/generated/grammar.js.map +1 -0
  43. package/out/language/generated/module.d.ts +14 -0
  44. package/out/language/generated/module.d.ts.map +1 -0
  45. package/out/language/generated/module.js +21 -0
  46. package/out/language/generated/module.js.map +1 -0
  47. package/out/language/main-browser.d.ts +2 -0
  48. package/out/language/main-browser.d.ts.map +1 -0
  49. package/out/language/main-browser.js +10 -0
  50. package/out/language/main-browser.js.map +1 -0
  51. package/out/language/main.cjs +36229 -0
  52. package/out/language/main.cjs.map +7 -0
  53. package/out/language/main.d.ts +2 -0
  54. package/out/language/main.d.ts.map +1 -0
  55. package/out/language/main.js +11 -0
  56. package/out/language/main.js.map +1 -0
  57. package/out/language/parser.d.ts +9 -0
  58. package/out/language/parser.d.ts.map +1 -0
  59. package/out/language/parser.js +273 -0
  60. package/out/language/parser.js.map +1 -0
  61. package/out/language/syntax.d.ts +155 -0
  62. package/out/language/syntax.d.ts.map +1 -0
  63. package/out/language/syntax.js +527 -0
  64. package/out/language/syntax.js.map +1 -0
  65. package/out/runtime/agents/common.d.ts +2 -0
  66. package/out/runtime/agents/common.d.ts.map +1 -0
  67. package/out/runtime/agents/common.js +178 -0
  68. package/out/runtime/agents/common.js.map +1 -0
  69. package/out/runtime/agents/impl/openai.d.ts +8 -0
  70. package/out/runtime/agents/impl/openai.d.ts.map +1 -0
  71. package/out/runtime/agents/impl/openai.js +15 -0
  72. package/out/runtime/agents/impl/openai.js.map +1 -0
  73. package/out/runtime/agents/provider.d.ts +21 -0
  74. package/out/runtime/agents/provider.d.ts.map +1 -0
  75. package/out/runtime/agents/provider.js +32 -0
  76. package/out/runtime/agents/provider.js.map +1 -0
  77. package/out/runtime/agents/registry.d.ts +2 -0
  78. package/out/runtime/agents/registry.d.ts.map +1 -0
  79. package/out/runtime/agents/registry.js +10 -0
  80. package/out/runtime/agents/registry.js.map +1 -0
  81. package/out/runtime/auth/cognito.d.ts +16 -0
  82. package/out/runtime/auth/cognito.d.ts.map +1 -0
  83. package/out/runtime/auth/cognito.js +186 -0
  84. package/out/runtime/auth/cognito.js.map +1 -0
  85. package/out/runtime/auth/defs.d.ts +11 -0
  86. package/out/runtime/auth/defs.d.ts.map +1 -0
  87. package/out/runtime/auth/defs.js +24 -0
  88. package/out/runtime/auth/defs.js.map +1 -0
  89. package/out/runtime/auth/interface.d.ts +22 -0
  90. package/out/runtime/auth/interface.d.ts.map +1 -0
  91. package/out/runtime/auth/interface.js +2 -0
  92. package/out/runtime/auth/interface.js.map +1 -0
  93. package/out/runtime/defs.js +24 -0
  94. package/out/runtime/defs.js.map +1 -0
  95. package/out/runtime/interpreter.d.ts +69 -0
  96. package/out/runtime/interpreter.d.ts.map +1 -0
  97. package/out/runtime/interpreter.js +1163 -0
  98. package/out/runtime/interpreter.js.map +1 -0
  99. package/out/runtime/loader.d.ts +25 -0
  100. package/out/runtime/loader.d.ts.map +1 -0
  101. package/out/runtime/loader.js +346 -0
  102. package/out/runtime/loader.js.map +1 -0
  103. package/out/runtime/logger.d.ts +2 -0
  104. package/out/runtime/logger.d.ts.map +1 -0
  105. package/out/runtime/logger.js +44 -0
  106. package/out/runtime/logger.js.map +1 -0
  107. package/out/runtime/module.d.ts +273 -0
  108. package/out/runtime/module.d.ts.map +1 -0
  109. package/out/runtime/module.js +1786 -0
  110. package/out/runtime/module.js.map +1 -0
  111. package/out/runtime/modules/ai.d.ts +26 -0
  112. package/out/runtime/modules/ai.d.ts.map +1 -0
  113. package/out/runtime/modules/ai.js +211 -0
  114. package/out/runtime/modules/ai.js.map +1 -0
  115. package/out/runtime/modules/auth.d.ts +39 -0
  116. package/out/runtime/modules/auth.d.ts.map +1 -0
  117. package/out/runtime/modules/auth.js +359 -0
  118. package/out/runtime/modules/auth.js.map +1 -0
  119. package/out/runtime/modules/core.d.ts +2 -0
  120. package/out/runtime/modules/core.d.ts.map +1 -0
  121. package/out/runtime/modules/core.js +67 -0
  122. package/out/runtime/modules/core.js.map +1 -0
  123. package/out/runtime/relgraph.d.ts +21 -0
  124. package/out/runtime/relgraph.d.ts.map +1 -0
  125. package/out/runtime/relgraph.js +156 -0
  126. package/out/runtime/relgraph.js.map +1 -0
  127. package/out/runtime/resolvers/interface.d.ts +59 -0
  128. package/out/runtime/resolvers/interface.d.ts.map +1 -0
  129. package/out/runtime/resolvers/interface.js +111 -0
  130. package/out/runtime/resolvers/interface.js.map +1 -0
  131. package/out/runtime/resolvers/registry.d.ts +8 -0
  132. package/out/runtime/resolvers/registry.d.ts.map +1 -0
  133. package/out/runtime/resolvers/registry.js +26 -0
  134. package/out/runtime/resolvers/registry.js.map +1 -0
  135. package/out/runtime/resolvers/sqldb/database.d.ts +50 -0
  136. package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -0
  137. package/out/runtime/resolvers/sqldb/database.js +618 -0
  138. package/out/runtime/resolvers/sqldb/database.js.map +1 -0
  139. package/out/runtime/resolvers/sqldb/dbutil.d.ts +18 -0
  140. package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -0
  141. package/out/runtime/resolvers/sqldb/dbutil.js +221 -0
  142. package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -0
  143. package/out/runtime/resolvers/sqldb/impl.d.ts +26 -0
  144. package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -0
  145. package/out/runtime/resolvers/sqldb/impl.js +300 -0
  146. package/out/runtime/resolvers/sqldb/impl.js.map +1 -0
  147. package/out/runtime/state.js +83 -0
  148. package/out/runtime/state.js.map +1 -0
  149. package/out/runtime/util.d.ts +43 -0
  150. package/out/runtime/util.d.ts.map +1 -0
  151. package/out/runtime/util.js +447 -0
  152. package/out/runtime/util.js.map +1 -0
  153. package/out/setupClassic.d.ts +98 -0
  154. package/out/setupClassic.d.ts.map +1 -0
  155. package/out/setupClassic.js +38 -0
  156. package/out/setupClassic.js.map +1 -0
  157. package/out/setupCommon.d.ts +2 -0
  158. package/out/setupCommon.d.ts.map +1 -0
  159. package/out/setupCommon.js +33 -0
  160. package/out/setupCommon.js.map +1 -0
  161. package/out/setupExtended.d.ts +40 -0
  162. package/out/setupExtended.d.ts.map +1 -0
  163. package/out/setupExtended.js +67 -0
  164. package/out/setupExtended.js.map +1 -0
  165. package/out/syntaxes/agentlang.monarch.d.ts +77 -0
  166. package/out/syntaxes/agentlang.monarch.d.ts.map +1 -0
  167. package/out/syntaxes/agentlang.monarch.js +31 -0
  168. package/out/syntaxes/agentlang.monarch.js.map +1 -0
  169. package/out/utils/fs/index.d.ts +14 -0
  170. package/out/utils/fs/index.d.ts.map +1 -0
  171. package/out/utils/fs/index.js +26 -0
  172. package/out/utils/fs/index.js.map +1 -0
  173. package/out/utils/fs/interfaces.d.ts +105 -0
  174. package/out/utils/fs/interfaces.d.ts.map +1 -0
  175. package/out/utils/fs/interfaces.js +5 -0
  176. package/out/utils/fs/interfaces.js.map +1 -0
  177. package/out/utils/fs/lightning-fs.d.ts +116 -0
  178. package/out/utils/fs/lightning-fs.d.ts.map +1 -0
  179. package/out/utils/fs/lightning-fs.js +243 -0
  180. package/out/utils/fs/lightning-fs.js.map +1 -0
  181. package/out/utils/fs/node-fs.d.ts +93 -0
  182. package/out/utils/fs/node-fs.d.ts.map +1 -0
  183. package/out/utils/fs/node-fs.js +169 -0
  184. package/out/utils/fs/node-fs.js.map +1 -0
  185. package/out/utils/fs-utils.d.ts +153 -0
  186. package/out/utils/fs-utils.d.ts.map +1 -0
  187. package/out/utils/fs-utils.js +271 -0
  188. package/out/utils/fs-utils.js.map +1 -0
  189. package/out/utils/runtime.d.ts +36 -0
  190. package/out/utils/runtime.d.ts.map +1 -0
  191. package/out/utils/runtime.js +39 -0
  192. package/out/utils/runtime.js.map +1 -0
  193. package/package.json +155 -0
  194. package/src/api/http.ts +361 -0
  195. package/src/cli/cli-util.ts +18 -0
  196. package/src/cli/main.ts +146 -0
  197. package/src/extension/main.ts +51 -0
  198. package/src/language/agentlang-module.ts +75 -0
  199. package/src/language/agentlang-validator.ts +60 -0
  200. package/src/language/agentlang.langium +178 -0
  201. package/src/language/generated/ast.ts +1698 -0
  202. package/src/language/generated/grammar.ts +4477 -0
  203. package/src/language/generated/module.ts +25 -0
  204. package/src/language/main-browser.ts +19 -0
  205. package/src/language/main.ts +13 -0
  206. package/src/language/parser.ts +329 -0
  207. package/src/language/syntax.ts +646 -0
  208. package/src/runtime/agents/common.ts +177 -0
  209. package/src/runtime/agents/impl/openai.ts +19 -0
  210. package/src/runtime/agents/provider.ts +58 -0
  211. package/src/runtime/agents/registry.ts +9 -0
  212. package/src/runtime/auth/cognito.ts +225 -0
  213. package/src/runtime/auth/defs.ts +33 -0
  214. package/src/runtime/auth/interface.ts +31 -0
  215. package/src/runtime/defs.ts +33 -0
  216. package/src/runtime/interpreter.ts +1352 -0
  217. package/src/runtime/loader.ts +450 -0
  218. package/src/runtime/logger.ts +51 -0
  219. package/src/runtime/module.ts +2188 -0
  220. package/src/runtime/modules/ai.ts +257 -0
  221. package/src/runtime/modules/auth.ts +489 -0
  222. package/src/runtime/modules/core.ts +95 -0
  223. package/src/runtime/relgraph.ts +195 -0
  224. package/src/runtime/resolvers/interface.ts +160 -0
  225. package/src/runtime/resolvers/registry.ts +30 -0
  226. package/src/runtime/resolvers/sqldb/database.ts +823 -0
  227. package/src/runtime/resolvers/sqldb/dbutil.ts +257 -0
  228. package/src/runtime/resolvers/sqldb/impl.ts +471 -0
  229. package/src/runtime/state.ts +87 -0
  230. package/src/runtime/util.ts +513 -0
  231. package/src/setupClassic.ts +43 -0
  232. package/src/setupCommon.ts +33 -0
  233. package/src/setupExtended.ts +79 -0
  234. package/src/syntaxes/agentlang.monarch.ts +31 -0
  235. package/src/utils/fs/index.ts +28 -0
  236. package/src/utils/fs/interfaces.ts +118 -0
  237. package/src/utils/fs/lightning-fs.ts +284 -0
  238. package/src/utils/fs/node-fs.ts +185 -0
  239. package/src/utils/fs-utils.ts +304 -0
  240. package/src/utils/runtime.ts +43 -0
@@ -0,0 +1,471 @@
1
+ import {
2
+ assertInstance,
3
+ AttributeEntry,
4
+ BetweenInstanceNodeValuesResult,
5
+ findIdAttribute,
6
+ getAllBetweenRelationships,
7
+ getAllOneToOneRelationshipsForEntity,
8
+ getBetweenInstanceNodeValues,
9
+ Instance,
10
+ InstanceAttributes,
11
+ isBetweenRelationship,
12
+ newInstanceAttributes,
13
+ Relationship,
14
+ } from '../../module.js';
15
+ import { escapeFqName, makeFqName, splitFqName } from '../../util.js';
16
+ import { JoinInfo, Resolver } from '../interface.js';
17
+ import { asTableName } from './dbutil.js';
18
+ import {
19
+ getMany,
20
+ insertRow,
21
+ updateRow,
22
+ getAllConnected,
23
+ startDbTransaction,
24
+ commitDbTransaction,
25
+ rollbackDbTransaction,
26
+ hardDeleteRow,
27
+ DbContext,
28
+ insertBetweenRow,
29
+ addRowForFullTextSearch,
30
+ vectorStoreSearch,
31
+ vectorStoreSearchEntryExists,
32
+ deleteFullTextSearchEntry,
33
+ JoinClause,
34
+ JoinOn,
35
+ makeJoinOn,
36
+ getManyByJoin,
37
+ } from './database.js';
38
+ import { Environment } from '../../interpreter.js';
39
+ import { OpenAIEmbeddings } from '@langchain/openai';
40
+ import { Embeddings } from '@langchain/core/embeddings';
41
+ import { DeletedFlagAttributeName, ParentAttributeName, PathAttributeName } from '../../defs.js';
42
+
43
+ function maybeFindIdAttributeName(inst: Instance): string | undefined {
44
+ const attrEntry: AttributeEntry | undefined = findIdAttribute(inst);
45
+ if (attrEntry != undefined) {
46
+ return attrEntry.name;
47
+ }
48
+ return undefined;
49
+ }
50
+
51
+ export class SqlDbResolver extends Resolver {
52
+ private txnId: string | undefined;
53
+ private embeddings: Embeddings;
54
+
55
+ constructor(name: string) {
56
+ super();
57
+ this.name = name;
58
+ this.embeddings = new OpenAIEmbeddings();
59
+ }
60
+
61
+ public override getName(): string {
62
+ return this.name;
63
+ }
64
+
65
+ private getDbContext(resourceFqName: string): DbContext {
66
+ const activeEnv: Environment = this.getUserData() as Environment;
67
+ if (!activeEnv) {
68
+ throw new Error('Active environment context is required by SqlDbResolver');
69
+ }
70
+ return new DbContext(
71
+ resourceFqName,
72
+ this.authInfo,
73
+ activeEnv,
74
+ this.txnId,
75
+ activeEnv.isInKernelMode()
76
+ );
77
+ }
78
+
79
+ public override onSetPath(moduleName: string, entryName: string): string {
80
+ return entryName;
81
+ }
82
+
83
+ private async insertInstance(inst: Instance, orUpdate = false): Promise<Instance> {
84
+ if (isBetweenRelationship(inst.name, inst.moduleName)) {
85
+ const nodeVals: BetweenInstanceNodeValuesResult = getBetweenInstanceNodeValues(inst);
86
+ assertInstance(nodeVals.node1);
87
+ assertInstance(nodeVals.node2);
88
+ await this.connectInstances(nodeVals.node1, nodeVals.node2, nodeVals.entry, orUpdate);
89
+ return inst;
90
+ } else {
91
+ const idAttrName: string | undefined = maybeFindIdAttributeName(inst);
92
+ ensureOneToOneAttributes(inst);
93
+ const attrs: InstanceAttributes = inst.attributes;
94
+ if (idAttrName != undefined) {
95
+ const idAttrVal: any = attrs.get(idAttrName);
96
+ const pp: string | undefined = attrs.get(PathAttributeName);
97
+ const n: string = `${inst.moduleName}/${inst.name}`;
98
+ let p: string = '';
99
+ if (pp != undefined) p = `${pp}/${escapeFqName(n)}/${idAttrVal}`;
100
+ else p = `${n.replace('/', '$')}/${idAttrVal}`;
101
+ attrs.set(PathAttributeName, p);
102
+ }
103
+ const n: string = asTableName(inst.moduleName, inst.name);
104
+ const rowObj: object = inst.attributesAsObject();
105
+ const ctx = this.getDbContext(inst.getFqName());
106
+ await insertRow(n, rowObj, ctx, orUpdate);
107
+ if (inst.record.getFullTextSearchAttributes()) {
108
+ const path = attrs.get(PathAttributeName);
109
+ if (!(await vectorStoreSearchEntryExists(n, path, ctx))) {
110
+ const res = await this.embeddings.embedQuery(JSON.stringify(rowObj));
111
+ await addRowForFullTextSearch(n, path, res, ctx);
112
+ }
113
+ }
114
+ return inst;
115
+ }
116
+ }
117
+
118
+ public override async createInstance(inst: Instance): Promise<Instance> {
119
+ return await this.insertInstance(inst);
120
+ }
121
+
122
+ public override async upsertInstance(inst: Instance): Promise<Instance> {
123
+ return await this.insertInstance(inst, true);
124
+ }
125
+
126
+ public override async updateInstance(
127
+ inst: Instance,
128
+ newAttrs: InstanceAttributes
129
+ ): Promise<Instance> {
130
+ const queryObj: object = Object.fromEntries(new Map<string, any>().set(PathAttributeName, '='));
131
+ const queryVals: object = Object.fromEntries(
132
+ new Map<string, any>().set(PathAttributeName, inst.attributes.get(PathAttributeName))
133
+ );
134
+ const updateObj: object = Object.fromEntries(newAttrs);
135
+ await updateRow(
136
+ asTableName(inst.moduleName, inst.name),
137
+ queryObj,
138
+ queryVals,
139
+ updateObj,
140
+ this.getDbContext(inst.getFqName())
141
+ );
142
+ return inst.mergeAttributes(newAttrs);
143
+ }
144
+
145
+ static EmptyResultSet: Array<Instance> = new Array<Instance>();
146
+
147
+ public override async queryInstances(
148
+ inst: Instance,
149
+ queryAll: boolean = false
150
+ ): Promise<Instance[]> {
151
+ let result = SqlDbResolver.EmptyResultSet;
152
+
153
+ const tableName = asTableName(inst.moduleName, inst.name);
154
+ const rslt: any = await getMany(
155
+ tableName,
156
+ queryAll ? undefined : inst.queryAttributesAsObject(),
157
+ queryAll ? undefined : inst.queryAttributeValuesAsObject(),
158
+ this.getDbContext(inst.getFqName())
159
+ );
160
+ if (rslt instanceof Array) {
161
+ result = new Array<Instance>();
162
+ rslt.forEach((r: object) => {
163
+ const attrs: InstanceAttributes = maybeNormalizeAttributeNames(
164
+ tableName,
165
+ new Map(Object.entries(r))
166
+ );
167
+ attrs.delete(DeletedFlagAttributeName);
168
+ result.push(Instance.newWithAttributes(inst, attrs));
169
+ });
170
+ }
171
+ return result;
172
+ }
173
+
174
+ static MarkDeletedObject: object = Object.fromEntries(
175
+ newInstanceAttributes().set(DeletedFlagAttributeName, true)
176
+ );
177
+
178
+ public override async deleteInstance(
179
+ target: Instance | Instance[],
180
+ purge: boolean
181
+ ): Promise<Instance[] | Instance> {
182
+ if (target != null) {
183
+ if (target instanceof Array) {
184
+ for (let i = 0; i < target.length; ++i) {
185
+ await this.deleteInstanceHelper(target[i], purge);
186
+ }
187
+ } else {
188
+ await this.deleteInstanceHelper(target, purge);
189
+ }
190
+ }
191
+ return target;
192
+ }
193
+
194
+ public override async queryChildInstances(
195
+ parentPath: string,
196
+ inst: Instance
197
+ ): Promise<Instance[]> {
198
+ inst.addQuery(PathAttributeName, 'like', parentPath + '%');
199
+ return await this.queryInstances(inst, false);
200
+ }
201
+
202
+ public override async queryConnectedInstances(
203
+ relationship: Relationship,
204
+ connectedInstance: Instance,
205
+ inst: Instance
206
+ ): Promise<Instance[]> {
207
+ let result = SqlDbResolver.EmptyResultSet;
208
+ if (relationship.isOneToOne()) {
209
+ const col = relationship.getAliasFor(connectedInstance);
210
+ inst.addQuery(col, '=', connectedInstance.lookup(PathAttributeName));
211
+ return await this.queryInstances(inst, false);
212
+ } else {
213
+ await getAllConnected(
214
+ asTableName(inst.moduleName, inst.name),
215
+ inst.queryAttributesAsObject(),
216
+ inst.queryAttributeValuesAsObject(),
217
+ {
218
+ connectionTable: asTableName(inst.moduleName, relationship.name),
219
+ fromColumn: relationship.node1.alias,
220
+ fromValue: `'${connectedInstance.lookup(PathAttributeName)}'`,
221
+ toColumn: relationship.node2.alias,
222
+ toRef: PathAttributeName,
223
+ },
224
+ this.getDbContext(inst.getFqName())
225
+ ).then((rslt: any) => {
226
+ if (rslt instanceof Array) {
227
+ result = new Array<Instance>();
228
+ const connInst: Instance = Instance.EmptyInstance(
229
+ relationship.node2.path.getEntryName(),
230
+ relationship.node2.path.getModuleName()
231
+ );
232
+ rslt.forEach((r: object) => {
233
+ const attrs: InstanceAttributes = new Map(Object.entries(r));
234
+ attrs.delete(DeletedFlagAttributeName);
235
+ result.push(Instance.newWithAttributes(connInst, attrs));
236
+ });
237
+ }
238
+ });
239
+ return result;
240
+ }
241
+ }
242
+
243
+ public override async queryByJoin(
244
+ inst: Instance,
245
+ joinsSpec: JoinInfo[],
246
+ intoSpec: Map<string, string>
247
+ ): Promise<any> {
248
+ const tableName = asTableName(inst.moduleName, inst.name);
249
+ const joinClauses: JoinClause[] = [];
250
+ this.processJoinInfo(tableName, inst, joinsSpec, joinClauses);
251
+ intoSpec.forEach((v: string, k: string) => {
252
+ const p = splitFqName(v);
253
+ const mn = p.hasModule() ? p.getModuleName() : inst.moduleName;
254
+ intoSpec.set(k, asTableName(mn, p.getEntryName()));
255
+ });
256
+ const rslt: any = await getManyByJoin(
257
+ tableName,
258
+ inst.queryAttributesAsObject(),
259
+ inst.queryAttributeValuesAsObject(),
260
+ joinClauses,
261
+ intoSpec,
262
+ this.getDbContext(inst.getFqName())
263
+ );
264
+ return rslt;
265
+ }
266
+
267
+ private processJoinInfo(
268
+ joinParentTable: string,
269
+ joinInst: Instance,
270
+ joinsSpec: JoinInfo[],
271
+ joinClauses: JoinClause[]
272
+ ) {
273
+ joinsSpec.forEach((ji: JoinInfo) => {
274
+ const rel: Relationship = ji.relationship;
275
+ const joinTableName = asTableName(ji.queryInstance.moduleName, ji.queryInstance.name);
276
+ let joinOn: JoinOn | JoinOn[] | undefined;
277
+ if (rel.isContains()) {
278
+ const walkDown = rel.isParent(joinInst);
279
+ const pathRef = `${joinParentTable}.${walkDown ? PathAttributeName : ParentAttributeName}`;
280
+ joinOn = makeJoinOn(
281
+ `"${joinTableName}"."${walkDown ? ParentAttributeName : PathAttributeName}"`,
282
+ pathRef
283
+ );
284
+ } else {
285
+ const pathRef = `${joinParentTable}.${PathAttributeName}`;
286
+ if (rel.isOneToOne()) {
287
+ joinOn = makeJoinOn(
288
+ `"${joinTableName}"."${rel.getAliasForName(joinInst.getFqName())}"`,
289
+ pathRef
290
+ );
291
+ } else {
292
+ const relTableName = asTableName(rel.moduleName, rel.name);
293
+ const jPathRef = `"${joinTableName}"."${PathAttributeName}"`;
294
+ const fqn = ji.queryInstance.getFqName();
295
+ const n1 = rel.getAliasForName(fqn);
296
+ const n2 = rel.getInverseAliasForName(fqn);
297
+ joinClauses.push({
298
+ tableName: relTableName,
299
+ joinOn: makeJoinOn(`"${relTableName}"."${n2}"`, pathRef),
300
+ });
301
+ joinOn = [
302
+ makeJoinOn(jPathRef, `"${relTableName}"."${n1}"`),
303
+ makeJoinOn(`"${relTableName}"."${n1}"`, jPathRef),
304
+ ];
305
+ }
306
+ }
307
+ if (joinOn) {
308
+ joinClauses.push({
309
+ tableName: joinTableName,
310
+ queryObject: ji.queryInstance.queryAttributesAsObject(),
311
+ queryValues: ji.queryInstance.queryAttributeValuesAsObject(),
312
+ joinOn: joinOn,
313
+ });
314
+ } else {
315
+ throw new Error(
316
+ `Relationship type for ${ji.relationship.name} not supported for join-queries`
317
+ );
318
+ }
319
+ if (ji.subJoins) {
320
+ this.processJoinInfo(joinTableName, ji.queryInstance, ji.subJoins, joinClauses);
321
+ }
322
+ });
323
+ }
324
+
325
+ private async deleteInstanceHelper(target: Instance, purge: boolean) {
326
+ target.addQuery(PathAttributeName);
327
+ const queryVals: object = Object.fromEntries(
328
+ newInstanceAttributes().set(PathAttributeName, target.attributes.get(PathAttributeName))
329
+ );
330
+ const tableName = asTableName(target.moduleName, target.name);
331
+ const ctx = this.getDbContext(target.getFqName());
332
+ if (purge) {
333
+ await hardDeleteRow(tableName, [[PathAttributeName, target.lookup(PathAttributeName)]], ctx);
334
+ } else {
335
+ await updateRow(
336
+ tableName,
337
+ target.queryAttributesAsObject(),
338
+ queryVals,
339
+ SqlDbResolver.MarkDeletedObject,
340
+ ctx
341
+ );
342
+ }
343
+ if (target.record.getFullTextSearchAttributes()) {
344
+ await deleteFullTextSearchEntry(tableName, target.lookup(PathAttributeName), ctx);
345
+ }
346
+ }
347
+
348
+ public override async connectInstances(
349
+ node1: Instance,
350
+ otherNodeOrNodes: Instance | Instance[],
351
+ relEntry: Relationship,
352
+ orUpdate: boolean
353
+ ): Promise<Instance> {
354
+ if (otherNodeOrNodes instanceof Array) {
355
+ for (let i = 0; i < otherNodeOrNodes.length; ++i) {
356
+ await this.connectInstancesHelper(node1, otherNodeOrNodes[i], relEntry, orUpdate);
357
+ }
358
+ return node1;
359
+ } else {
360
+ await this.connectInstancesHelper(node1, otherNodeOrNodes as Instance, relEntry, orUpdate);
361
+ return node1;
362
+ }
363
+ }
364
+
365
+ async connectInstancesHelper(
366
+ node1: Instance,
367
+ node2: Instance,
368
+ relEntry: Relationship,
369
+ orUpdate: boolean
370
+ ): Promise<void> {
371
+ const n: string = asTableName(relEntry.moduleName, relEntry.name);
372
+ const [firstNode, secondNode] = relEntry.isFirstNode(node1) ? [node1, node2] : [node2, node1];
373
+ const a1: string = relEntry.node1.alias;
374
+ const a2: string = relEntry.node2.alias;
375
+ const n1path: any = orUpdate ? firstNode.lookup(PathAttributeName) : undefined;
376
+ if (relEntry.isOneToOne()) {
377
+ await this.updateInstance(
378
+ node1,
379
+ newInstanceAttributes().set(relEntry.node2.alias, node2.lookup(PathAttributeName))
380
+ );
381
+ await this.updateInstance(
382
+ node2,
383
+ newInstanceAttributes().set(relEntry.node1.alias, node1.lookup(PathAttributeName))
384
+ );
385
+ } else {
386
+ if (orUpdate) {
387
+ await hardDeleteRow(
388
+ n,
389
+ [
390
+ [a1, n1path],
391
+ [a2, secondNode.lookup(PathAttributeName)],
392
+ ],
393
+ this.getDbContext(relEntry.getFqName())
394
+ );
395
+ }
396
+ await insertBetweenRow(
397
+ n,
398
+ a1,
399
+ a2,
400
+ firstNode,
401
+ secondNode,
402
+ relEntry,
403
+ this.getDbContext(relEntry.getFqName())
404
+ );
405
+ }
406
+ }
407
+
408
+ public override async fullTextSearch(
409
+ entryName: string,
410
+ moduleName: string,
411
+ query: string,
412
+ options?: Map<string, any>
413
+ ): Promise<any> {
414
+ const queryVec = await this.embeddings.embedQuery(query);
415
+ const ctx = this.getDbContext(makeFqName(moduleName, entryName));
416
+ let limit = 5;
417
+ if (options && options.has('limit')) {
418
+ limit = options.get('limit') as number;
419
+ }
420
+ return await vectorStoreSearch(asTableName(moduleName, entryName), queryVec, limit, ctx);
421
+ }
422
+
423
+ public override async startTransaction(): Promise<string> {
424
+ this.txnId = await startDbTransaction();
425
+ return this.txnId;
426
+ }
427
+
428
+ public override async commitTransaction(txnId: string): Promise<string> {
429
+ if (txnId == this.txnId) {
430
+ await commitDbTransaction(txnId);
431
+ this.txnId = undefined;
432
+ }
433
+ return txnId;
434
+ }
435
+
436
+ public override async rollbackTransaction(txnId: string): Promise<string> {
437
+ if (txnId == this.txnId) {
438
+ await rollbackDbTransaction(txnId);
439
+ this.txnId = undefined;
440
+ }
441
+ return txnId;
442
+ }
443
+ }
444
+
445
+ function ensureOneToOneAttributes(inst: Instance) {
446
+ const betRels = getAllBetweenRelationships();
447
+ getAllOneToOneRelationshipsForEntity(inst.moduleName, inst.name, betRels).forEach(
448
+ (re: Relationship) => {
449
+ const n = re.getInverseAliasFor(inst);
450
+ if (!inst.attributes.has(n)) {
451
+ inst.attributes.set(n, crypto.randomUUID());
452
+ }
453
+ }
454
+ );
455
+ }
456
+
457
+ function maybeNormalizeAttributeNames(
458
+ tableName: string,
459
+ attrs: InstanceAttributes
460
+ ): InstanceAttributes {
461
+ const ks = [...attrs.keys()];
462
+ if (ks[0].startsWith(tableName)) {
463
+ const n = tableName.length;
464
+ ks.forEach((k: string) => {
465
+ const v = attrs.get(k);
466
+ attrs.delete(k);
467
+ attrs.set(k.substring(n + 1), v);
468
+ });
469
+ }
470
+ return attrs;
471
+ }
@@ -0,0 +1,87 @@
1
+ import { z } from 'zod';
2
+
3
+ // Config validation schema
4
+ export const ConfigSchema = z.object({
5
+ service: z
6
+ .object({
7
+ port: z.number(),
8
+ })
9
+ .default({
10
+ port: 8080,
11
+ }),
12
+ store: z
13
+ .discriminatedUnion('type', [
14
+ z.object({
15
+ type: z.literal('postgres'),
16
+ host: z.string().default('localhost'),
17
+ username: z.string().default('postgres'),
18
+ password: z.string().default('postgres'),
19
+ dbname: z.string().default('postgres'),
20
+ port: z.number().default(5432),
21
+ }),
22
+ z.object({
23
+ type: z.literal('mysql'),
24
+ host: z.string().default('localhost'),
25
+ username: z.string().default('mysql'),
26
+ password: z.string().default('mysql'),
27
+ dbname: z.string().default('mysql'),
28
+ port: z.number().default(3306),
29
+ }),
30
+ z.object({
31
+ type: z.literal('sqlite'),
32
+ dbname: z.string().optional(),
33
+ }),
34
+ ])
35
+ .optional(),
36
+ graphql: z
37
+ .object({
38
+ enabled: z.boolean().default(false),
39
+ })
40
+ .optional(),
41
+ rbacEnabled: z.boolean().optional(),
42
+ auditTrail: z
43
+ .object({
44
+ enabled: z.boolean().default(false),
45
+ })
46
+ .optional(),
47
+ authentication: z
48
+ .discriminatedUnion('service', [
49
+ z.object({
50
+ service: z.literal('okta'),
51
+ superuserEmail: z.string(),
52
+ domain: z.string(),
53
+ cookieDomain: z.string().optional(),
54
+ authServer: z.string().default('default'),
55
+ clientSecret: z.string(),
56
+ apiToken: z.string(),
57
+ scope: z.string().default('openid offline_access'),
58
+ cookieTtlMs: z.number().default(1209600000),
59
+ introspect: z.boolean().default(true),
60
+ authorizeRedirectUrl: z.string(),
61
+ clientUrl: z.string(),
62
+ roleClaim: z.string().default('roles'),
63
+ defaultRole: z.string().default('user'),
64
+ clientId: z.string(),
65
+ }),
66
+ z.object({
67
+ service: z.literal('cognito'),
68
+ superuserEmail: z.string(),
69
+ superuserPassword: z.string().optional(),
70
+ isIdentityStore: z.boolean().default(false),
71
+ userPoolId: z.string(),
72
+ clientId: z.string(),
73
+ whitelistEnabled: z.boolean().default(false),
74
+ disableUserSessions: z.boolean().default(false),
75
+ }),
76
+ ])
77
+ .optional(),
78
+ });
79
+
80
+ export type Config = z.infer<typeof ConfigSchema>;
81
+
82
+ export let AppConfig: Config | undefined;
83
+
84
+ export function setAppConfig(config: Config): Config {
85
+ AppConfig = config;
86
+ return AppConfig;
87
+ }