agentlang 0.0.24 → 0.0.26

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/src/cli/main.ts CHANGED
@@ -7,8 +7,8 @@ import {
7
7
  internModule,
8
8
  load,
9
9
  loadAppConfig,
10
- runPostInitTasks,
11
- runPreInitTasks,
10
+ loadCoreModules,
11
+ runStandaloneStatements,
12
12
  } from '../runtime/loader.js';
13
13
  import { NodeFileSystem } from 'langium/node';
14
14
  import { extractDocument } from '../runtime/loader.js';
@@ -23,6 +23,9 @@ import { prepareIntegrations } from '../runtime/integrations.js';
23
23
  import { isNodeEnv } from '../utils/runtime.js';
24
24
  import { OpenAPIClientAxios } from 'openapi-client-axios';
25
25
  import { registerOpenApiModule } from '../runtime/openapi.js';
26
+ import { initDatabase } from '../runtime/resolvers/sqldb/database.js';
27
+ import { runInitFunctions } from '../runtime/util.js';
28
+ import { startServer } from '../api/http.js';
26
29
 
27
30
  const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
28
31
 
@@ -79,7 +82,29 @@ export const parseAndValidate = async (fileName: string): Promise<void> => {
79
82
  }
80
83
  };
81
84
 
85
+ export async function runPostInitTasks(appSpec?: ApplicationSpec, config?: Config) {
86
+ await initDatabase(config?.store);
87
+ await runInitFunctions();
88
+ await runStandaloneStatements();
89
+ if (appSpec) startServer(appSpec, config?.service?.port || 8080);
90
+ }
91
+
92
+ export async function runPreInitTasks(): Promise<boolean> {
93
+ let result: boolean = true;
94
+ await loadCoreModules().catch((reason: any) => {
95
+ const msg = `Failed to load core modules - ${reason.toString()}`;
96
+ logger.error(msg);
97
+ console.log(chalk.red(msg));
98
+ result = false;
99
+ });
100
+ return result;
101
+ }
102
+
82
103
  export const runModule = async (fileName: string): Promise<void> => {
104
+ const r: boolean = await runPreInitTasks();
105
+ if (!r) {
106
+ throw new Error('Failed to initialize runtime');
107
+ }
83
108
  const configDir =
84
109
  path.dirname(fileName) === '.' ? process.cwd() : path.resolve(process.cwd(), fileName);
85
110
  const config: Config = await loadAppConfig(configDir);
@@ -51,7 +51,6 @@ import {
51
51
  makeFqName,
52
52
  maybeExtends,
53
53
  registerInitFunction,
54
- runInitFunctions,
55
54
  } from './util.js';
56
55
  import { getFileSystem, toFsPath, readFile, readdir, exists } from '../utils/fs-utils.js';
57
56
  import { URI } from 'vscode-uri';
@@ -69,8 +68,6 @@ import { Config, ConfigSchema, setAppConfig } from './state.js';
69
68
  import { getModuleFn, importModule } from './jsmodules.js';
70
69
  import { SetSubscription } from './defs.js';
71
70
  import { ExtendedFileSystem } from '../utils/fs/interfaces.js';
72
- import { initDatabase } from './resolvers/sqldb/database.js';
73
- import { startServer } from '../api/http.js';
74
71
  import z from 'zod';
75
72
 
76
73
  export async function extractDocument(
@@ -250,34 +247,7 @@ export async function flushAllAndLoad(
250
247
  return await load(fileName, fsOptions, callback);
251
248
  }
252
249
 
253
- export async function runPreInitTasks(): Promise<boolean> {
254
- let result: boolean = true;
255
- await loadCoreModules().catch((reason: any) => {
256
- const msg = `Failed to load core modules - ${reason.toString()}`;
257
- logger.error(msg);
258
- console.log(chalk.red(msg));
259
- result = false;
260
- });
261
- return result;
262
- }
263
-
264
- export async function runPostInitTasks(appSpec?: ApplicationSpec, config?: Config) {
265
- await initDatabase(config?.store);
266
- await runInitFunctions();
267
- await runStandaloneStatements();
268
- if (appSpec) startServer(appSpec, config?.service?.port || 8080);
269
- }
270
-
271
- export async function loadAppConfig(
272
- configDir: string,
273
- runPreInit: boolean = true
274
- ): Promise<Config> {
275
- if (runPreInit) {
276
- const r: boolean = await runPreInitTasks();
277
- if (!r) {
278
- throw new Error('Failed to initialize runtime');
279
- }
280
- }
250
+ export async function loadAppConfig(configDir: string): Promise<Config> {
281
251
  let cfgObj: any = undefined;
282
252
  const fs = await getFileSystem();
283
253
  const alCfgFile = `${configDir}/config.al`;
@@ -41,6 +41,7 @@ import {
41
41
  isPath,
42
42
  findUqCompositeAttributes,
43
43
  escapeFqName,
44
+ encryptPassword,
44
45
  } from './util.js';
45
46
  import { parseStatement } from '../language/parser.js';
46
47
  import { ActiveSessionInfo, AdminSession } from './auth/defs.js';
@@ -1572,6 +1573,7 @@ const builtInChecks = new Map([
1572
1573
  ['UUID', isString],
1573
1574
  ['URL', isString],
1574
1575
  ['Path', isPath],
1576
+ ['Password', isString],
1575
1577
  [
1576
1578
  'Map',
1577
1579
  (obj: any) => {
@@ -1659,6 +1661,19 @@ export function defaultAttributes(schema: RecordSchema): Map<string, any> {
1659
1661
  return result;
1660
1662
  }
1661
1663
 
1664
+ export function passwordAttributes(schema: RecordSchema): Set<string> | undefined {
1665
+ let result: Set<string> | undefined = undefined;
1666
+ schema.forEach((v: AttributeSpec, k: string) => {
1667
+ if (v.type == 'Password') {
1668
+ if (result == undefined) {
1669
+ result = new Set<string>();
1670
+ }
1671
+ result?.add(k);
1672
+ }
1673
+ });
1674
+ return result;
1675
+ }
1676
+
1662
1677
  export function objectAttributes(schema: RecordSchema): Array<string> | undefined {
1663
1678
  let result: Array<string> | undefined;
1664
1679
  schema.forEach((v: AttributeSpec, k: string) => {
@@ -2297,6 +2312,12 @@ export class Instance {
2297
2312
  return Object.fromEntries(result);
2298
2313
  }
2299
2314
 
2315
+ static fromObject(name: string, moduleName: string, obj: object): Instance {
2316
+ const inst = Instance.EmptyInstance(name, moduleName);
2317
+ const attrs = new Map(Object.entries(obj));
2318
+ return Instance.newWithAttributes(inst, attrs);
2319
+ }
2320
+
2300
2321
  static asSerializableValue(v: any, forSerialization: boolean): any {
2301
2322
  if (v instanceof Instance) {
2302
2323
  const inst = v as Instance;
@@ -2320,9 +2341,9 @@ export class Instance {
2320
2341
  return Object.fromEntries(attrs);
2321
2342
  }
2322
2343
 
2323
- attributesWithStringifiedObjects(): object {
2344
+ static stringifyObjects(attributes: InstanceAttributes): object {
2324
2345
  const attrs = newInstanceAttributes();
2325
- this.attributes.forEach((v: any, k: string) => {
2346
+ attributes.forEach((v: any, k: string) => {
2326
2347
  if (v instanceof Object) {
2327
2348
  attrs.set(k, JSON.stringify(v instanceof Map ? Object.fromEntries(v) : v));
2328
2349
  } else {
@@ -2332,6 +2353,10 @@ export class Instance {
2332
2353
  return Object.fromEntries(attrs);
2333
2354
  }
2334
2355
 
2356
+ attributesWithStringifiedObjects(): object {
2357
+ return Instance.stringifyObjects(this.attributes);
2358
+ }
2359
+
2335
2360
  queryAttributesAsObject(): object {
2336
2361
  if (this.queryAttributes != undefined) {
2337
2362
  return Object.fromEntries(this.queryAttributes);
@@ -2522,6 +2547,22 @@ function maybeSetDefaultAttributeValues(
2522
2547
  return attributes;
2523
2548
  }
2524
2549
 
2550
+ function postProcessAttributes(
2551
+ schema: RecordSchema,
2552
+ attributes: InstanceAttributes
2553
+ ): InstanceAttributes {
2554
+ const pswdAttrs = passwordAttributes(schema);
2555
+ if (pswdAttrs) {
2556
+ pswdAttrs.forEach((n: string) => {
2557
+ const v: string | undefined = attributes.get(n);
2558
+ if (v) {
2559
+ attributes.set(n, encryptPassword(v));
2560
+ }
2561
+ });
2562
+ }
2563
+ return attributes;
2564
+ }
2565
+
2525
2566
  export function makeInstance(
2526
2567
  moduleName: string,
2527
2568
  entryName: string,
@@ -2544,7 +2585,7 @@ export function makeInstance(
2544
2585
  });
2545
2586
  }
2546
2587
  if (!queryAttributes && !queryAll) {
2547
- attributes = maybeSetDefaultAttributeValues(schema, attributes);
2588
+ attributes = postProcessAttributes(schema, maybeSetDefaultAttributeValues(schema, attributes));
2548
2589
  }
2549
2590
  return new Instance(
2550
2591
  record,
@@ -138,7 +138,7 @@ export class SqlDbResolver extends Resolver {
138
138
  const queryVals: object = Object.fromEntries(
139
139
  new Map<string, any>().set(PathAttributeName, inst.attributes.get(PathAttributeName))
140
140
  );
141
- const updateObj: object = Object.fromEntries(newAttrs);
141
+ const updateObj: object = Instance.stringifyObjects(newAttrs);
142
142
  await updateRow(
143
143
  asTableReference(inst.moduleName, inst.name),
144
144
  queryObj,
@@ -11,6 +11,8 @@ import {
11
11
  Statement,
12
12
  } from '../language/generated/ast.js';
13
13
  import { readFile } from '../utils/fs-utils.js';
14
+ import bcrypt from 'bcryptjs';
15
+ import path from 'node:path';
14
16
 
15
17
  export const QuerySuffix = '?';
16
18
 
@@ -494,3 +496,23 @@ const ReservedNames = new Set([
494
496
  export function isReservedName(s: string): boolean {
495
497
  return ReservedNames.has(s);
496
498
  }
499
+
500
+ export function encryptPassword(s: string): string {
501
+ return bcrypt.hashSync(s, 10);
502
+ }
503
+
504
+ export function comparePassword(s: string, hash: string): boolean {
505
+ return bcrypt.compareSync(s, hash);
506
+ }
507
+
508
+ export function fileExtension(fileName: string): string {
509
+ if (isNodeEnv) {
510
+ return path.extname(fileName);
511
+ } else {
512
+ const idx = fileName.lastIndexOf('.');
513
+ if (idx >= 0) {
514
+ return fileName.substring(idx);
515
+ }
516
+ }
517
+ return '';
518
+ }