agentlang 0.6.4 → 0.6.6

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 (46) hide show
  1. package/out/api/http.js +2 -2
  2. package/out/api/http.js.map +1 -1
  3. package/out/runtime/auth/cognito.d.ts +1 -0
  4. package/out/runtime/auth/cognito.d.ts.map +1 -1
  5. package/out/runtime/auth/cognito.js +23 -0
  6. package/out/runtime/auth/cognito.js.map +1 -1
  7. package/out/runtime/auth/interface.d.ts +1 -0
  8. package/out/runtime/auth/interface.d.ts.map +1 -1
  9. package/out/runtime/interpreter.d.ts.map +1 -1
  10. package/out/runtime/interpreter.js +4 -2
  11. package/out/runtime/interpreter.js.map +1 -1
  12. package/out/runtime/module.d.ts +10 -2
  13. package/out/runtime/module.d.ts.map +1 -1
  14. package/out/runtime/module.js +102 -46
  15. package/out/runtime/module.js.map +1 -1
  16. package/out/runtime/modules/ai.d.ts.map +1 -1
  17. package/out/runtime/modules/ai.js +5 -4
  18. package/out/runtime/modules/ai.js.map +1 -1
  19. package/out/runtime/modules/auth.d.ts +1 -0
  20. package/out/runtime/modules/auth.d.ts.map +1 -1
  21. package/out/runtime/modules/auth.js +38 -0
  22. package/out/runtime/modules/auth.js.map +1 -1
  23. package/out/runtime/monitor.d.ts.map +1 -1
  24. package/out/runtime/monitor.js +4 -4
  25. package/out/runtime/monitor.js.map +1 -1
  26. package/out/setupClassic.d.ts +98 -0
  27. package/out/setupClassic.d.ts.map +1 -0
  28. package/out/setupClassic.js +38 -0
  29. package/out/setupClassic.js.map +1 -0
  30. package/out/setupCommon.d.ts +2 -0
  31. package/out/setupCommon.d.ts.map +1 -0
  32. package/out/setupCommon.js +33 -0
  33. package/out/setupCommon.js.map +1 -0
  34. package/out/setupExtended.d.ts +40 -0
  35. package/out/setupExtended.d.ts.map +1 -0
  36. package/out/setupExtended.js +67 -0
  37. package/out/setupExtended.js.map +1 -0
  38. package/package.json +186 -185
  39. package/src/api/http.ts +2 -2
  40. package/src/runtime/auth/cognito.ts +25 -0
  41. package/src/runtime/auth/interface.ts +1 -0
  42. package/src/runtime/interpreter.ts +4 -1
  43. package/src/runtime/module.ts +124 -48
  44. package/src/runtime/modules/ai.ts +5 -4
  45. package/src/runtime/modules/auth.ts +36 -0
  46. package/src/runtime/monitor.ts +4 -4
@@ -47,6 +47,7 @@ import {
47
47
  forceAsFqName,
48
48
  validateIdFormat,
49
49
  nameContainsSepEscape,
50
+ registerInitFunction,
50
51
  } from './util.js';
51
52
  import { parseStatement } from '../language/parser.js';
52
53
  import { ActiveSessionInfo, AdminSession } from './auth/defs.js';
@@ -73,6 +74,8 @@ import {
73
74
  removeAgentResponseSchema,
74
75
  removeAgentScenarios,
75
76
  } from './agents/common.js';
77
+ import { Environment } from './interpreter.js';
78
+ import { isNode } from '../utils/fs-utils.js';
76
79
 
77
80
  export class ModuleEntry {
78
81
  name: string;
@@ -1117,6 +1120,25 @@ ${attrs.join(',\n')}
1117
1120
  }
1118
1121
  }
1119
1122
 
1123
+ const SysAttr_Created = '__created';
1124
+ const SysAttr_LastModified = '__last_modified';
1125
+ const SysAttr_CreatedBy = '__created_by';
1126
+ const SysAttr_LastModifiedBy = '__last_modified_by';
1127
+
1128
+ const SysAttr_CreatedSpec: AttributeSpec = asSystemAttribute({
1129
+ type: 'DateTime',
1130
+ properties: new Map<string, any>().set('default', 'now()'),
1131
+ });
1132
+
1133
+ const SysAttr_LastModifiedSpec = SysAttr_CreatedSpec;
1134
+
1135
+ const SysAttr_CreatedBySpec: AttributeSpec = asSystemAttribute({
1136
+ type: 'String',
1137
+ properties: new Map<string, any>().set('optional', true),
1138
+ });
1139
+
1140
+ const SysAttr_LastModifiedBySpec = SysAttr_CreatedBySpec;
1141
+
1120
1142
  export class Entity extends Record {
1121
1143
  override type: RecordType = RecordType.ENTITY;
1122
1144
 
@@ -1127,6 +1149,16 @@ export class Entity extends Record {
1127
1149
  parentEntryName?: string
1128
1150
  ) {
1129
1151
  super(name, moduleName, scm, parentEntryName);
1152
+ this.addMetaAttributes();
1153
+ }
1154
+
1155
+ private addMetaAttributes(): Entity {
1156
+ this.schema
1157
+ .set(SysAttr_Created, SysAttr_CreatedSpec)
1158
+ .set(SysAttr_CreatedBy, SysAttr_CreatedBySpec)
1159
+ .set(SysAttr_LastModified, SysAttr_LastModifiedSpec)
1160
+ .set(SysAttr_LastModifiedBy, SysAttr_LastModifiedBySpec);
1161
+ return this;
1130
1162
  }
1131
1163
 
1132
1164
  setRbacSpecifications(rbac: RbacSpecification[]): Entity {
@@ -2255,6 +2287,17 @@ export class Module {
2255
2287
  return false;
2256
2288
  }
2257
2289
 
2290
+ removeEvent(name: string): boolean {
2291
+ if (this.isEvent(name)) {
2292
+ const r: boolean = this.removeEntry(name);
2293
+ if (r) {
2294
+ this.removeEntry(asWorkflowName(name));
2295
+ return r;
2296
+ }
2297
+ }
2298
+ return false;
2299
+ }
2300
+
2258
2301
  private getEntriesOfType(t: RecordType): ModuleEntry[] {
2259
2302
  return this.entries.filter((v: ModuleEntry) => {
2260
2303
  const r: Record = v as Record;
@@ -2413,7 +2456,26 @@ export class Module {
2413
2456
  }
2414
2457
  }
2415
2458
 
2416
- const moduleDb = new Map<string, Module>();
2459
+ declare global {
2460
+ var al_moduleDb: Map<string, Module> | undefined;
2461
+ }
2462
+
2463
+ let browserModuleDb: Map<string, Module> | undefined;
2464
+
2465
+ const getModuleDb = function (): Map<string, Module> {
2466
+ if (isNode()) {
2467
+ if (globalThis.al_moduleDb === undefined) {
2468
+ globalThis.al_moduleDb = new Map<string, Module>();
2469
+ }
2470
+ return globalThis.al_moduleDb;
2471
+ } else {
2472
+ if (browserModuleDb === undefined) {
2473
+ browserModuleDb = new Map<string, Module>();
2474
+ }
2475
+ return browserModuleDb;
2476
+ }
2477
+ };
2478
+
2417
2479
  let activeModule: string = '';
2418
2480
 
2419
2481
  export function getActiveModuleName() {
@@ -2422,12 +2484,13 @@ export function getActiveModuleName() {
2422
2484
 
2423
2485
  export function addModule(name: string): Module {
2424
2486
  const mod: Module = new Module(name);
2425
- moduleDb.set(name, mod);
2487
+ getModuleDb().set(name, mod);
2426
2488
  activeModule = name;
2427
2489
  return mod;
2428
2490
  }
2429
2491
 
2430
2492
  export function removeModule(name: string): boolean {
2493
+ const moduleDb = getModuleDb();
2431
2494
  if (moduleDb.has(name)) {
2432
2495
  moduleDb.delete(name);
2433
2496
  return true;
@@ -2439,13 +2502,13 @@ addModule(DefaultModuleName);
2439
2502
  addRecord('env', DefaultModuleName);
2440
2503
 
2441
2504
  export function getModuleNames(): string[] {
2442
- const ks: Iterable<string> = moduleDb.keys();
2505
+ const ks: Iterable<string> = getModuleDb().keys();
2443
2506
  return Array.from(ks);
2444
2507
  }
2445
2508
 
2446
2509
  export function getUserModuleNames(): string[] {
2447
2510
  const result: Array<string> = new Array<string>();
2448
- Array.from(moduleDb.keys()).forEach((n: string) => {
2511
+ Array.from(getModuleDb().keys()).forEach((n: string) => {
2449
2512
  if (!DefaultModules.has(n)) {
2450
2513
  result.push(n);
2451
2514
  }
@@ -2454,11 +2517,11 @@ export function getUserModuleNames(): string[] {
2454
2517
  }
2455
2518
 
2456
2519
  export function isModule(name: string): boolean {
2457
- return moduleDb.has(name);
2520
+ return getModuleDb().has(name);
2458
2521
  }
2459
2522
 
2460
2523
  export function fetchModule(moduleName: string): Module {
2461
- const module: Module | undefined = moduleDb.get(moduleName);
2524
+ const module: Module | undefined = getModuleDb().get(moduleName);
2462
2525
  if (module === undefined) {
2463
2526
  throw new Error(`Module not found - ${moduleName}`);
2464
2527
  }
@@ -2466,7 +2529,7 @@ export function fetchModule(moduleName: string): Module {
2466
2529
  }
2467
2530
 
2468
2531
  export function allModuleNames(): string[] {
2469
- return [...moduleDb.keys()];
2532
+ return [...getModuleDb().keys()];
2470
2533
  }
2471
2534
 
2472
2535
  export function fetchModuleEntry(entryName: string, moduleName: string): ModuleEntry {
@@ -2829,21 +2892,23 @@ export function addWorkflow(
2829
2892
  }
2830
2893
  if (!statements) statements = new Array<Statement>();
2831
2894
  if (hdr) {
2832
- const eventFqName = makeFqName(moduleName, name);
2833
- const entityDef = getEntityDef(hdr.name, moduleName);
2834
- if (hdr.tag == '@after') {
2835
- entityDef?.addAfterTrigger({
2836
- on: hdr.prefix,
2837
- event: eventFqName,
2838
- async: false,
2839
- });
2840
- } else {
2841
- entityDef?.addBeforeTrigger({
2842
- on: hdr.prefix,
2843
- event: eventFqName,
2844
- async: false,
2845
- });
2846
- }
2895
+ registerInitFunction(() => {
2896
+ const eventFqName = makeFqName(moduleName, name);
2897
+ const entityDef = getEntityDef(hdr.name, moduleName);
2898
+ if (hdr.tag == '@after') {
2899
+ entityDef?.addAfterTrigger({
2900
+ on: hdr.prefix,
2901
+ event: eventFqName,
2902
+ async: false,
2903
+ });
2904
+ } else {
2905
+ entityDef?.addBeforeTrigger({
2906
+ on: hdr.prefix,
2907
+ event: eventFqName,
2908
+ async: false,
2909
+ });
2910
+ }
2911
+ });
2847
2912
  }
2848
2913
  return module.addEntry(
2849
2914
  new Workflow(asWorkflowName(name), statements, moduleName, hdr ? true : false)
@@ -3178,14 +3243,7 @@ export function removeWorkflow(name: string, moduleName = activeModule): boolean
3178
3243
 
3179
3244
  export function removeEvent(name: string, moduleName = activeModule): boolean {
3180
3245
  const module: Module = fetchModule(moduleName);
3181
- if (module.isEvent(name)) {
3182
- const r: boolean = module.removeEntry(name);
3183
- if (r) {
3184
- module.removeEntry(asWorkflowName(name));
3185
- return r;
3186
- }
3187
- }
3188
- return false;
3246
+ return module.removeEvent(name);
3189
3247
  }
3190
3248
 
3191
3249
  function getAttributeSpec(attrsSpec: RecordSchema, attrName: string): AttributeSpec {
@@ -3264,7 +3322,7 @@ export class Instance {
3264
3322
  queryAttributeValues: InstanceAttributes | undefined;
3265
3323
  relatedInstances: Map<string, Instance[]> | undefined;
3266
3324
  private contextData: Map<string, any> | undefined;
3267
- private id: string;
3325
+ private ___id: string;
3268
3326
 
3269
3327
  constructor(
3270
3328
  record: Record,
@@ -3274,7 +3332,7 @@ export class Instance {
3274
3332
  queryAttributes?: InstanceAttributes,
3275
3333
  queryAttributeValues?: InstanceAttributes
3276
3334
  ) {
3277
- this.id = crypto.randomUUID();
3335
+ this.___id = crypto.randomUUID();
3278
3336
  this.record = record;
3279
3337
  this.name = name;
3280
3338
  this.moduleName = moduleName;
@@ -3297,6 +3355,10 @@ export class Instance {
3297
3355
  );
3298
3356
  }
3299
3357
 
3358
+ static IsInstance(obj: any): boolean {
3359
+ return obj instanceof Instance || (obj instanceof Object && obj.___id);
3360
+ }
3361
+
3300
3362
  static clone(inst: Instance): Instance {
3301
3363
  const attrs = newInstanceAttributes();
3302
3364
  inst.attributes.forEach((v: any, k: string) => {
@@ -3306,7 +3368,16 @@ export class Instance {
3306
3368
  }
3307
3369
 
3308
3370
  getId(): string {
3309
- return this.id;
3371
+ return this.___id;
3372
+ }
3373
+
3374
+ metaAttributeValues(): any {
3375
+ return {
3376
+ created: this.lookup(SysAttr_Created),
3377
+ createdBy: this.lookup(SysAttr_CreatedBy),
3378
+ lastModified: this.lookup(SysAttr_LastModified),
3379
+ lastModifiedBy: this.lookup(SysAttr_LastModifiedBy),
3380
+ };
3310
3381
  }
3311
3382
 
3312
3383
  normalizeAttributes(attrs: InstanceAttributes): InstanceAttributes {
@@ -3411,7 +3482,7 @@ export class Instance {
3411
3482
  }
3412
3483
 
3413
3484
  static asSerializableValue(v: any, forSerialization: boolean): any {
3414
- if (v instanceof Instance) {
3485
+ if (Instance.IsInstance(v)) {
3415
3486
  const inst = v as Instance;
3416
3487
  return forSerialization ? inst.asSerializableObject() : inst.asObject();
3417
3488
  } else if (v instanceof Object) {
@@ -3488,10 +3559,10 @@ export class Instance {
3488
3559
  if (relInsts === undefined) {
3489
3560
  relInsts = new Array<Instance>();
3490
3561
  }
3491
- if (insts instanceof Instance) {
3492
- relInsts.push(insts);
3562
+ if (Instance.IsInstance(insts)) {
3563
+ relInsts.push(insts as Instance);
3493
3564
  } else {
3494
- insts.forEach((inst: Instance) => {
3565
+ (insts as Instance[]).forEach((inst: Instance) => {
3495
3566
  relInsts.push(inst);
3496
3567
  });
3497
3568
  }
@@ -3591,7 +3662,7 @@ export class Instance {
3591
3662
  export function maybeInstanceAsString(result: any): string {
3592
3663
  if (!isString(result)) {
3593
3664
  try {
3594
- if (result instanceof Instance) {
3665
+ if (Instance.IsInstance(result)) {
3595
3666
  const inst = result as Instance;
3596
3667
  return JSON.stringify(inst.asSerializableObject());
3597
3668
  } else if (result instanceof Array) {
@@ -3729,7 +3800,7 @@ export function isRecordInstance(inst: Instance): boolean {
3729
3800
 
3730
3801
  export function getAllModuleEntries(f: Function): Map<string, string[]> {
3731
3802
  const result: Map<string, string[]> = new Map<string, string[]>();
3732
- moduleDb.forEach((module: Module, k: string) => {
3803
+ getModuleDb().forEach((module: Module, k: string) => {
3733
3804
  result.set(
3734
3805
  k,
3735
3806
  f(module).map((me: ModuleEntry) => {
@@ -3779,16 +3850,9 @@ export function getBetweenInstanceNodeValues(inst: Instance): BetweenInstanceNod
3779
3850
  };
3780
3851
  }
3781
3852
 
3782
- export function isInstance(obj: any): boolean {
3783
- if (obj) {
3784
- return obj instanceof Instance;
3785
- }
3786
- return false;
3787
- }
3788
-
3789
3853
  export function isInstanceOfType(obj: any, fqName: string): boolean {
3790
3854
  if (obj) {
3791
- return isInstance(obj) && fqName == (obj as Instance).getFqName();
3855
+ return Instance.IsInstance(obj) && fqName == (obj as Instance).getFqName();
3792
3856
  }
3793
3857
  return false;
3794
3858
  }
@@ -3799,7 +3863,7 @@ export function assertInstance(obj: any) {
3799
3863
  throw new Error(`Empty instances`);
3800
3864
  }
3801
3865
  obj.forEach(assertInstance);
3802
- } else if (!(obj instanceof Instance)) {
3866
+ } else if (!Instance.IsInstance(obj)) {
3803
3867
  throw new Error(`${obj} is not an Instance`);
3804
3868
  }
3805
3869
  }
@@ -3929,3 +3993,15 @@ export function getAttributeNames(entityFqName: string): Array<string> {
3929
3993
  const scm = fetchModule(parts.getModuleName()).getRecord(parts.getEntryName()).schema;
3930
3994
  return [...scm.keys()];
3931
3995
  }
3996
+
3997
+ export function maybeSetMetaAttributes(
3998
+ attrs: InstanceAttributes,
3999
+ env: Environment,
4000
+ inUpdateMode: boolean = false
4001
+ ) {
4002
+ const user = env.getActiveUser();
4003
+ attrs.set(SysAttr_LastModified, now()).set(SysAttr_LastModifiedBy, user);
4004
+ if (!inUpdateMode && attrs.get(SysAttr_CreatedBy) === undefined) {
4005
+ attrs.set(SysAttr_CreatedBy, user);
4006
+ }
4007
+ }
@@ -377,8 +377,8 @@ Only return a pure JSON object with no extra text, annotations etc.`;
377
377
  if (obj === null || obj === undefined) return this;
378
378
  let r: Instance | Instance[] | undefined = undefined;
379
379
  if (
380
- obj instanceof Instance ||
381
- (obj instanceof Array && obj.length > 0 && obj[0] instanceof Instance)
380
+ Instance.IsInstance(obj) ||
381
+ (obj instanceof Array && obj.length > 0 && Instance.IsInstance(obj[0]))
382
382
  ) {
383
383
  r = obj;
384
384
  } else {
@@ -394,8 +394,9 @@ Only return a pure JSON object with no extra text, annotations etc.`;
394
394
  });
395
395
  n = r[0].getFqName();
396
396
  } else {
397
- data = extractScratchData(scratchNames, r);
398
- n = r.getFqName();
397
+ const i = r as Instance;
398
+ data = extractScratchData(scratchNames, i);
399
+ n = i.getFqName();
399
400
  }
400
401
  if (data) env.addToScratchPad(n, data);
401
402
  return this;
@@ -44,6 +44,7 @@ entity User {
44
44
 
45
45
  workflow AfterDeleteUser {
46
46
  {RemoveUserSession {id AfterDeleteUser.User.id}}
47
+ await Auth.deleteUser(AfterDeleteUser.User.id, AfterDeleteUser.User.email)
47
48
  }
48
49
 
49
50
  @public workflow CreateUser {
@@ -569,6 +570,41 @@ export async function activateUser(userId: string, env: Environment): Promise<Re
569
570
  }
570
571
  }
571
572
 
573
+ export async function deleteUser(userId: string, email: string, env: Environment): Promise<Result> {
574
+ const needCommit = env ? false : true;
575
+ env = env ? env : new Environment();
576
+ const f = async () => {
577
+ try {
578
+ if (email) {
579
+ try {
580
+ await fetchAuthImpl().deleteUser(email, env);
581
+ } catch (err: any) {
582
+ // If user doesn't exist in Cognito, log warning but continue with local deletion
583
+ if (err.message && err.message.includes('not found')) {
584
+ logger.warn(`User ${email} not found in Cognito, continuing with local deletion`);
585
+ } else {
586
+ logger.error(`Failed to delete user ${email} from Cognito: ${err.message}`);
587
+ throw err;
588
+ }
589
+ }
590
+ }
591
+
592
+ return {
593
+ status: 'ok',
594
+ message: 'User deleted successfully',
595
+ };
596
+ } catch (err: any) {
597
+ logger.error(`Failed to delete user ${userId}: ${err.message}`);
598
+ throw err;
599
+ }
600
+ };
601
+ if (needCommit) {
602
+ return await env.callInTransaction(f);
603
+ } else {
604
+ return await f();
605
+ }
606
+ }
607
+
572
608
  export async function updateUserLastLogin(id: string, env: Environment): Promise<Result> {
573
609
  return await evalEvent(
574
610
  'UpdateUserLastLogin',
@@ -1,4 +1,4 @@
1
- import { Instance, isAgentEventInstance, isInstance } from './module.js';
1
+ import { Instance, isAgentEventInstance } from './module.js';
2
2
  import { disableInternalMonitoring, enableInternalMonitoring } from './state.js';
3
3
 
4
4
  export class MonitorEntry {
@@ -104,7 +104,7 @@ export class MonitorEntry {
104
104
  }
105
105
 
106
106
  private static resultAsObject(result: any): object {
107
- if (result instanceof Instance) return result.asSerializableObject();
107
+ if (Instance.IsInstance(result)) return result.asSerializableObject();
108
108
  else if (result instanceof Array)
109
109
  return result.map((v: any) => {
110
110
  return MonitorEntry.resultAsObject(v);
@@ -327,11 +327,11 @@ export class Monitor {
327
327
  r.timestamp = this.timestamp;
328
328
  if (this.flowResult !== undefined) {
329
329
  let fr = this.flowResult;
330
- if (fr instanceof Array && isInstance(fr[0])) {
330
+ if (fr instanceof Array && Instance.IsInstance(fr[0])) {
331
331
  fr = fr.map((v: any) => {
332
332
  return v.asSerializableObject();
333
333
  });
334
- } else if (isInstance(fr)) {
334
+ } else if (Instance.IsInstance(fr)) {
335
335
  fr = fr.asSerializableObject();
336
336
  }
337
337
  r.flowResult = fr;