agentlang 0.3.5 → 0.3.7

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 (74) hide show
  1. package/README.md +82 -29
  2. package/out/api/http.js +2 -2
  3. package/out/api/http.js.map +1 -1
  4. package/out/extension/main.cjs +250 -250
  5. package/out/extension/main.cjs.map +2 -2
  6. package/out/language/generated/ast.js +0 -1
  7. package/out/language/generated/ast.js.map +1 -1
  8. package/out/language/main.cjs +502 -502
  9. package/out/language/main.cjs.map +3 -3
  10. package/out/language/syntax.js +8 -8
  11. package/out/language/syntax.js.map +1 -1
  12. package/out/runtime/auth/cognito.js +2 -2
  13. package/out/runtime/auth/cognito.js.map +1 -1
  14. package/out/runtime/exec-graph.d.ts.map +1 -1
  15. package/out/runtime/exec-graph.js +6 -5
  16. package/out/runtime/exec-graph.js.map +1 -1
  17. package/out/runtime/interpreter.js +49 -49
  18. package/out/runtime/interpreter.js.map +1 -1
  19. package/out/runtime/jsmodules.js +6 -6
  20. package/out/runtime/jsmodules.js.map +1 -1
  21. package/out/runtime/loader.js +6 -6
  22. package/out/runtime/loader.js.map +1 -1
  23. package/out/runtime/module.d.ts.map +1 -1
  24. package/out/runtime/module.js +72 -66
  25. package/out/runtime/module.js.map +1 -1
  26. package/out/runtime/modules/ai.js +5 -5
  27. package/out/runtime/modules/ai.js.map +1 -1
  28. package/out/runtime/modules/auth.d.ts.map +1 -1
  29. package/out/runtime/modules/auth.js +21 -18
  30. package/out/runtime/modules/auth.js.map +1 -1
  31. package/out/runtime/modules/core.js +1 -1
  32. package/out/runtime/modules/core.js.map +1 -1
  33. package/out/runtime/openapi.js +2 -2
  34. package/out/runtime/openapi.js.map +1 -1
  35. package/out/runtime/relgraph.js +1 -1
  36. package/out/runtime/relgraph.js.map +1 -1
  37. package/out/runtime/resolvers/authinfo.js +2 -2
  38. package/out/runtime/resolvers/authinfo.js.map +1 -1
  39. package/out/runtime/resolvers/interface.js +1 -1
  40. package/out/runtime/resolvers/interface.js.map +1 -1
  41. package/out/runtime/resolvers/registry.js +1 -1
  42. package/out/runtime/resolvers/registry.js.map +1 -1
  43. package/out/runtime/resolvers/sqldb/database.js +11 -11
  44. package/out/runtime/resolvers/sqldb/database.js.map +1 -1
  45. package/out/runtime/resolvers/sqldb/impl.js +4 -4
  46. package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
  47. package/out/runtime/util.js +5 -5
  48. package/out/runtime/util.js.map +1 -1
  49. package/out/utils/runtime.d.ts.map +1 -1
  50. package/out/utils/runtime.js +4 -2
  51. package/out/utils/runtime.js.map +1 -1
  52. package/package.json +184 -182
  53. package/src/api/http.ts +2 -2
  54. package/src/language/generated/ast.ts +1 -1
  55. package/src/language/syntax.ts +8 -8
  56. package/src/runtime/auth/cognito.ts +107 -2
  57. package/src/runtime/auth/interface.ts +1 -0
  58. package/src/runtime/exec-graph.ts +6 -5
  59. package/src/runtime/interpreter.ts +49 -49
  60. package/src/runtime/jsmodules.ts +6 -6
  61. package/src/runtime/loader.ts +6 -6
  62. package/src/runtime/module.ts +71 -66
  63. package/src/runtime/modules/ai.ts +5 -5
  64. package/src/runtime/modules/auth.ts +54 -20
  65. package/src/runtime/modules/core.ts +1 -1
  66. package/src/runtime/openapi.ts +2 -2
  67. package/src/runtime/relgraph.ts +1 -1
  68. package/src/runtime/resolvers/authinfo.ts +2 -2
  69. package/src/runtime/resolvers/interface.ts +1 -1
  70. package/src/runtime/resolvers/registry.ts +1 -1
  71. package/src/runtime/resolvers/sqldb/database.ts +11 -11
  72. package/src/runtime/resolvers/sqldb/impl.ts +4 -4
  73. package/src/runtime/util.ts +5 -5
  74. package/src/utils/runtime.ts +3 -3
package/package.json CHANGED
@@ -1,183 +1,185 @@
1
1
  {
2
- "name": "agentlang",
3
- "description": "The easiest way to build the most reliable AI agents - enterprise-grade teams of AI agents that collaborate with each other and humans",
4
- "version": "0.3.5",
5
- "license": "Sustainable Use License",
6
- "author": "agentlang-ai",
7
- "homepage": "https://github.com/agentlang-ai/agentlang#readme",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https://github.com/agentlang-ai/agentlang.git"
11
- },
12
- "bugs": {
13
- "url": "https://github.com/agentlang-ai/agentlang/issues"
14
- },
15
- "keywords": [
16
- "agentlang",
17
- "agents-generation",
18
- "ai-agents",
19
- "enterprise",
20
- "multi-agent",
21
- "no-code",
22
- "agent-teams",
23
- "production-ready"
24
- ],
25
- "files": [
26
- "bin",
27
- "out",
28
- "src",
29
- "LICENSE"
30
- ],
31
- "type": "module",
32
- "publishConfig": {
33
- "access": "public",
34
- "registry": "https://registry.npmjs.org/"
35
- },
36
- "dependencies": {
37
- "@aws-sdk/client-cognito-identity": "^3.828.0",
38
- "@aws-sdk/client-cognito-identity-provider": "^3.828.0",
39
- "@aws-sdk/credential-providers": "^3.828.0",
40
- "@isomorphic-git/lightning-fs": "^4.6.1",
41
- "@langchain/anthropic": "^0.3.32",
42
- "@langchain/core": "^0.3.78",
43
- "@langchain/openai": "^0.6.16",
44
- "amazon-cognito-identity-js": "^6.3.15",
45
- "aws-jwt-verify": "^5.1.0",
46
- "bcryptjs": "^3.0.2",
47
- "buffer": "^6.0.3",
48
- "chalk": "~5.3.0",
49
- "commander": "~11.0.0",
50
- "express": "^5.1.0",
51
- "handlebars": "^4.7.8",
52
- "langchain": "^0.3.36",
53
- "langium": "^3.5.0",
54
- "openapi-client-axios": "^7.6.0",
55
- "pg": "^8.16.1",
56
- "pgvector": "^0.2.1",
57
- "sqlite3": "^5.1.7",
58
- "sql.js": "^1.13.0",
59
- "typeorm": "^0.3.25",
60
- "vscode-languageserver": "^9.0.1",
61
- "winston": "^3.17.0",
62
- "winston-daily-rotate-file": "^5.0.0",
63
- "zod": "^3.25.55"
64
- },
65
- "optionalDependencies": {},
66
- "overrides": {
67
- "semver": "^7.5.3",
68
- "trim-newlines": ">=3.0.1"
69
- },
70
- "devDependencies": {
71
- "@codingame/esbuild-import-meta-url-plugin": "~1.0.2",
72
- "@eslint/js": "^9.26.0",
73
- "@types/cookie-parser": "^1.4.9",
74
- "@types/express": "^5.0.1",
75
- "@types/node": "^18.19.110",
76
- "@types/sql.js": "^1.4.9",
77
- "@types/vscode": "^1.100.0",
78
- "@typescript-eslint/eslint-plugin": "~8.32.1",
79
- "@typescript-eslint/parser": "~8.32.1",
80
- "brace-expansion": ">=2.0.2",
81
- "concurrently": "~8.2.1",
82
- "esbuild": "^0.25.4",
83
- "eslint": "~9.26.0",
84
- "generator-langium": "^3.0.0",
85
- "http-server": "~14.1.1",
86
- "langium-cli": "^3.5.0",
87
- "nodemon": "^3.1.10",
88
- "prettier": "^3.5.3",
89
- "typescript": "^5.8.3",
90
- "typescript-eslint": "^8.32.1",
91
- "vite": "^6.3.5",
92
- "vite-plugin-node-polyfills": "^0.24.0",
93
- "vitest": "^3.1.3",
94
- "vscode-languageclient": "^9.0.1",
95
- "vscode-uri": "^3.1.0"
96
- },
97
- "volta": {
98
- "node": ">=20.0.0",
99
- "npm": ">=10.8.2"
100
- },
101
- "displayName": "agentlang",
102
- "engines": {
103
- "vscode": "^1.67.0",
104
- "node": ">=20.0.0"
105
- },
106
- "categories": [
107
- "Programming Languages"
108
- ],
109
- "contributes": {
110
- "languages": [
111
- {
112
- "id": "agentlang",
113
- "aliases": [
114
- "Agentlang",
115
- "agentlang"
116
- ],
117
- "extensions": [
118
- ".al"
119
- ],
120
- "configuration": "./language-configuration.json"
121
- }
122
- ],
123
- "grammars": [
124
- {
125
- "language": "agentlang",
126
- "scopeName": "source.agentlang",
127
- "path": "syntaxes/agentlang.tmLanguage.json"
128
- }
129
- ]
130
- },
131
- "activationEvents": [
132
- "onLanguage:agentlang"
133
- ],
134
- "main": "./out/extension/main.cjs",
135
- "types": "./out/index.d.ts",
136
- "exports": {
137
- ".": {
138
- "types": "./out/index.d.ts",
139
- "import": "./out/extension/main.cjs",
140
- "require": "./out/extension/main.cjs"
141
- },
142
- "./browser": {
143
- "types": "./out/browser.d.ts",
144
- "import": "./out/browser.js",
145
- "default": "./out/browser.js"
146
- },
147
- "./src/*": {
148
- "types": "./out/*.d.ts",
149
- "import": "./out/*.js",
150
- "require": "./out/*.js",
151
- "default": "./out/*.js"
152
- },
153
- "./out/*": "./out/*"
154
- },
155
- "bin": {
156
- "agentlang-cli": "./bin/cli.js"
157
- },
158
- "lint-staged": {
159
- "*.{js,ts,tsx}": [
160
- "eslint --fix --cache",
161
- "prettier --write"
162
- ],
163
- "*.{json,md,yml,yaml}": [
164
- "prettier --write"
165
- ]
166
- },
167
- "scripts": {
168
- "build": "tsc -b tsconfig.src.json && tsc -b tsconfig.declarations.json && node esbuild.mjs",
169
- "build:ts": "tsc -b tsconfig.src.json",
170
- "watch": "concurrently -n tsc,esbuild -c blue,yellow \"tsc -b tsconfig.src.json --watch\" \"node esbuild.mjs --watch\"",
171
- "lint": "eslint . --ext .ts",
172
- "lint:fix": "eslint . --ext .ts --fix",
173
- "format": "prettier --write \"src/**/*.{js,ts,tsx,json,md}\"",
174
- "format:check": "prettier --check \"src/**/*.{js,ts,tsx,json,md}\"",
175
- "langium:generate": "langium generate",
176
- "langium:generate:production": "langium generate --mode=production",
177
- "langium:watch": "langium generate --watch",
178
- "vscode:prepublish": "npm run build && npm run lint",
179
- "dev": "vite",
180
- "test": "vitest run",
181
- "test:verbose": "VITEST_VERBOSE=true vitest run"
182
- }
183
- }
2
+ "name": "agentlang",
3
+ "description": "The easiest way to build the most reliable AI agents - enterprise-grade teams of AI agents that collaborate with each other and humans",
4
+ "version": "0.3.7",
5
+ "license": "Sustainable Use License",
6
+ "author": "agentlang-ai",
7
+ "homepage": "https://github.com/agentlang-ai/agentlang#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/agentlang-ai/agentlang.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/agentlang-ai/agentlang/issues"
14
+ },
15
+ "keywords": [
16
+ "agentlang",
17
+ "agents-generation",
18
+ "ai-agents",
19
+ "enterprise",
20
+ "multi-agent",
21
+ "no-code",
22
+ "agent-teams",
23
+ "production-ready"
24
+ ],
25
+ "files": [
26
+ "bin",
27
+ "out",
28
+ "src",
29
+ "LICENSE"
30
+ ],
31
+ "type": "module",
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "registry": "https://registry.npmjs.org/"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc -b tsconfig.src.json && tsc -b tsconfig.declarations.json && node esbuild.mjs",
38
+ "build:ts": "tsc -b tsconfig.src.json",
39
+ "watch": "concurrently -n tsc,esbuild -c blue,yellow \"tsc -b tsconfig.src.json --watch\" \"node esbuild.mjs --watch\"",
40
+ "lint": "eslint . --ext .ts",
41
+ "lint:fix": "eslint . --ext .ts --fix",
42
+ "format": "prettier --write \"src/**/*.{js,ts,tsx,json,md}\"",
43
+ "format:check": "prettier --check \"src/**/*.{js,ts,tsx,json,md}\"",
44
+ "langium:generate": "langium generate",
45
+ "langium:generate:production": "langium generate --mode=production",
46
+ "langium:watch": "langium generate --watch",
47
+ "vscode:prepublish": "npm run build && npm run lint",
48
+ "dev": "vite",
49
+ "test": "vitest run",
50
+ "test:verbose": "VITEST_VERBOSE=true vitest run"
51
+ },
52
+ "dependencies": {
53
+ "@aws-sdk/client-cognito-identity": "^3.828.0",
54
+ "@aws-sdk/client-cognito-identity-provider": "^3.828.0",
55
+ "@aws-sdk/credential-providers": "^3.828.0",
56
+ "@isomorphic-git/lightning-fs": "^4.6.1",
57
+ "@langchain/anthropic": "^0.3.32",
58
+ "@langchain/core": "^0.3.78",
59
+ "@langchain/openai": "^0.6.16",
60
+ "@types/jsonwebtoken": "^9.0.10",
61
+ "amazon-cognito-identity-js": "^6.3.15",
62
+ "aws-jwt-verify": "^5.1.0",
63
+ "bcryptjs": "^3.0.2",
64
+ "buffer": "^6.0.3",
65
+ "chalk": "~5.3.0",
66
+ "commander": "~11.0.0",
67
+ "express": "^5.1.0",
68
+ "handlebars": "^4.7.8",
69
+ "jsonwebtoken": "^9.0.2",
70
+ "langchain": "^0.3.36",
71
+ "langium": "^3.5.0",
72
+ "openapi-client-axios": "^7.6.0",
73
+ "pg": "^8.16.1",
74
+ "pgvector": "^0.2.1",
75
+ "sql.js": "^1.13.0",
76
+ "sqlite3": "^5.1.7",
77
+ "typeorm": "^0.3.25",
78
+ "vscode-languageserver": "^9.0.1",
79
+ "winston": "^3.17.0",
80
+ "winston-daily-rotate-file": "^5.0.0",
81
+ "zod": "^3.25.55"
82
+ },
83
+ "overrides": {
84
+ "semver": "^7.5.3",
85
+ "trim-newlines": ">=3.0.1"
86
+ },
87
+ "devDependencies": {
88
+ "@codingame/esbuild-import-meta-url-plugin": "~1.0.2",
89
+ "@eslint/js": "^9.26.0",
90
+ "@types/cookie-parser": "^1.4.9",
91
+ "@types/express": "^5.0.1",
92
+ "@types/node": "^18.19.110",
93
+ "@types/sql.js": "^1.4.9",
94
+ "@types/vscode": "^1.100.0",
95
+ "@typescript-eslint/eslint-plugin": "~8.32.1",
96
+ "@typescript-eslint/parser": "~8.32.1",
97
+ "brace-expansion": ">=2.0.2",
98
+ "concurrently": "~8.2.1",
99
+ "esbuild": "^0.25.4",
100
+ "eslint": "~9.26.0",
101
+ "generator-langium": "^3.0.0",
102
+ "http-server": "~14.1.1",
103
+ "langium-cli": "^3.5.0",
104
+ "nodemon": "^3.1.10",
105
+ "prettier": "^3.5.3",
106
+ "typescript": "^5.8.3",
107
+ "typescript-eslint": "^8.32.1",
108
+ "vite": "^6.3.5",
109
+ "vite-plugin-node-polyfills": "^0.24.0",
110
+ "vitest": "^3.1.3",
111
+ "vscode-languageclient": "^9.0.1",
112
+ "vscode-uri": "^3.1.0"
113
+ },
114
+ "volta": {
115
+ "node": ">=20.0.0",
116
+ "npm": ">=10.8.2"
117
+ },
118
+ "displayName": "agentlang",
119
+ "engines": {
120
+ "vscode": "^1.67.0",
121
+ "node": ">=20.0.0"
122
+ },
123
+ "categories": [
124
+ "Programming Languages"
125
+ ],
126
+ "contributes": {
127
+ "languages": [
128
+ {
129
+ "id": "agentlang",
130
+ "aliases": [
131
+ "Agentlang",
132
+ "agentlang"
133
+ ],
134
+ "extensions": [
135
+ ".al"
136
+ ],
137
+ "configuration": "./language-configuration.json"
138
+ }
139
+ ],
140
+ "grammars": [
141
+ {
142
+ "language": "agentlang",
143
+ "scopeName": "source.agentlang",
144
+ "path": "syntaxes/agentlang.tmLanguage.json"
145
+ }
146
+ ]
147
+ },
148
+ "activationEvents": [
149
+ "onLanguage:agentlang"
150
+ ],
151
+ "main": "./out/extension/main.cjs",
152
+ "types": "./out/index.d.ts",
153
+ "exports": {
154
+ ".": {
155
+ "types": "./out/index.d.ts",
156
+ "import": "./out/extension/main.cjs",
157
+ "require": "./out/extension/main.cjs"
158
+ },
159
+ "./browser": {
160
+ "types": "./out/browser.d.ts",
161
+ "import": "./out/browser.js",
162
+ "default": "./out/browser.js"
163
+ },
164
+ "./src/*": {
165
+ "types": "./out/*.d.ts",
166
+ "import": "./out/*.js",
167
+ "require": "./out/*.js",
168
+ "default": "./out/*.js"
169
+ },
170
+ "./out/*": "./out/*"
171
+ },
172
+ "bin": {
173
+ "agentlang-cli": "./bin/cli.js"
174
+ },
175
+ "lint-staged": {
176
+ "*.{js,ts,tsx}": [
177
+ "eslint --fix --cache",
178
+ "prettier --write"
179
+ ],
180
+ "*.{json,md,yml,yaml}": [
181
+ "prettier --write"
182
+ ]
183
+ },
184
+ "packageManager": "pnpm@10.13.1+sha512.37ebf1a5c7a30d5fabe0c5df44ee8da4c965ca0c5af3dbab28c3a1681b70a256218d05c81c9c0dcf767ef6b8551eb5b960042b9ed4300c59242336377e01cfad"
185
+ }
package/src/api/http.ts CHANGED
@@ -287,7 +287,7 @@ function queryPatternFromPath(path: string, req: Request): string {
287
287
  let entityName = r[1];
288
288
  const id = r[2];
289
289
  const parts = r[3];
290
- if (parts.length == 2 && id == undefined) {
290
+ if (parts.length == 2 && id === undefined) {
291
291
  if (req.query && Object.keys(req.query).length > 0) {
292
292
  const pat = objectAsAttributesPattern(req.query);
293
293
  return `{${moduleName}/${entityName} ${pat}}`;
@@ -306,7 +306,7 @@ function queryPatternFromPath(path: string, req: Request): string {
306
306
  return `{${pm}/${pe} {${PathAttributeNameQuery} "${p}"}, ${relName} {${moduleName}/${entityName}? {}}}`;
307
307
  }
308
308
  entityName = restoreFqName(entityName);
309
- if (id == undefined) {
309
+ if (id === undefined) {
310
310
  return `{${moduleName}/${entityName} {${PathAttributeNameQuery}like "${path}%"}}`;
311
311
  } else {
312
312
  return `{${moduleName}/${entityName} {${PathAttributeNameQuery} "${path}"}}`;
@@ -3,7 +3,7 @@
3
3
  * DO NOT EDIT MANUALLY!
4
4
  ******************************************************************************/
5
5
 
6
- /* eslint-disable */
6
+
7
7
  import * as langium from 'langium';
8
8
 
9
9
  export const AgentlangTerminals = {
@@ -20,7 +20,7 @@ export class BasePattern {
20
20
  }
21
21
 
22
22
  addAlias(alias: string) {
23
- if (this.aliases == undefined) {
23
+ if (this.aliases === undefined) {
24
24
  this.aliases = [];
25
25
  }
26
26
  this.aliases.push(alias);
@@ -31,7 +31,7 @@ export class BasePattern {
31
31
  }
32
32
 
33
33
  addHandler(k: 'not_found' | 'error', handler: BasePattern) {
34
- if (this.handlers == undefined) {
34
+ if (this.handlers === undefined) {
35
35
  this.handlers = new Map();
36
36
  }
37
37
  this.handlers.set(k, handler);
@@ -150,12 +150,12 @@ export class LiteralPattern extends BasePattern {
150
150
  const arr = new Array<string>();
151
151
  m.forEach((v: BasePattern, key: any) => {
152
152
  let k: any = key.str;
153
- if (k == undefined) {
153
+ if (k === undefined) {
154
154
  k = key.num;
155
155
  } else {
156
156
  k = `"${k}"`;
157
157
  }
158
- if (k == undefined) {
158
+ if (k === undefined) {
159
159
  k = key.bool;
160
160
  }
161
161
  arr.push(`${k}: ${v.toString()}`);
@@ -407,7 +407,7 @@ export class CrudPattern extends BasePattern {
407
407
  }
408
408
 
409
409
  addInto(alias: string, attr: string): CrudPattern {
410
- if (this.into == undefined) {
410
+ if (this.into === undefined) {
411
411
  this.into = new Map();
412
412
  }
413
413
  this.into.set(alias, attr);
@@ -459,7 +459,7 @@ export class CrudPattern extends BasePattern {
459
459
  }
460
460
 
461
461
  addRelationship(n: string, p: CrudPattern[] | CrudPattern) {
462
- if (this.relationships == undefined) {
462
+ if (this.relationships === undefined) {
463
463
  this.relationships = new Map();
464
464
  }
465
465
  this.relationships.set(n, p);
@@ -483,7 +483,7 @@ export class CrudPattern extends BasePattern {
483
483
  }
484
484
 
485
485
  private relationshipsAsString(): string | undefined {
486
- if (this.relationships != undefined) {
486
+ if (this.relationships !== undefined) {
487
487
  const result: Array<string> = [];
488
488
  this.relationships.forEach((p: CrudPattern | CrudPattern[], n: string) => {
489
489
  const ps = p instanceof Array ? `[${patternsToString(p, ',')}]` : p.toString();
@@ -587,7 +587,7 @@ export class ForEachPattern extends BasePattern {
587
587
  }
588
588
 
589
589
  override toString(): string {
590
- if (this.source == undefined || this.variable == undefined) {
590
+ if (this.source === undefined || this.variable === undefined) {
591
591
  throw new Error('`for` requires variable and source-pattern');
592
592
  }
593
593
  let s = `for ${this.variable} in ${this.source.toString()}`;
@@ -531,7 +531,7 @@ export class CognitoAuth implements AgentlangAuth {
531
531
  );
532
532
  },
533
533
  });
534
- while (result == undefined && authError == undefined) {
534
+ while (result === undefined && authError === undefined) {
535
535
  await sleepMilliseconds(100);
536
536
  }
537
537
  if (authError) {
@@ -624,7 +624,7 @@ export class CognitoAuth implements AgentlangAuth {
624
624
  );
625
625
  },
626
626
  });
627
- while (result == undefined && authError == undefined) {
627
+ while (result === undefined && authError === undefined) {
628
628
  await sleepMilliseconds(100);
629
629
  }
630
630
  if (authError) {
@@ -1165,4 +1165,109 @@ export class CognitoAuth implements AgentlangAuth {
1165
1165
  handleCognitoError(err, 'acceptInvitation');
1166
1166
  }
1167
1167
  }
1168
+
1169
+ async callback(code: string, env: Environment, cb: LoginCallback): Promise<void> {
1170
+ try {
1171
+ if (!isNodeEnv) {
1172
+ throw new Error('Callback authentication is only supported in Node.js environment');
1173
+ }
1174
+
1175
+ const clientId = this.fetchConfig('ClientId');
1176
+ const region = process.env.AWS_REGION || 'us-east-1';
1177
+ const redirectUri = process.env.COGNITO_REDIRECT_URI || 'http://localhost:3000/auth/callback';
1178
+ const hostedDomain = process.env.COGNITO_HOSTED_DOMAIN;
1179
+ const tokenEndpoint = `https://${hostedDomain}.auth.${region}.amazoncognito.com/oauth2/token`;
1180
+
1181
+ const tokenRequestBody = new URLSearchParams({
1182
+ grant_type: 'authorization_code',
1183
+ client_id: clientId,
1184
+ code: code,
1185
+ redirect_uri: redirectUri,
1186
+ });
1187
+
1188
+ logger.debug(`Exchanging authorization code for tokens via OAuth2 endpoint`);
1189
+
1190
+ console.log('te', tokenEndpoint, tokenRequestBody.toString());
1191
+ const response = await fetch(tokenEndpoint, {
1192
+ method: 'POST',
1193
+ headers: {
1194
+ 'Content-Type': 'application/x-www-form-urlencoded',
1195
+ },
1196
+ body: tokenRequestBody.toString(),
1197
+ });
1198
+
1199
+ if (!response.ok) {
1200
+ const errorText = await response.text();
1201
+ throw new Error(`Token exchange failed: ${response.status} ${errorText}`);
1202
+ }
1203
+
1204
+ const tokenData = await response.json();
1205
+
1206
+ if (!tokenData.access_token || !tokenData.id_token || !tokenData.refresh_token) {
1207
+ throw new Error('Missing required tokens in response');
1208
+ }
1209
+
1210
+ const {
1211
+ access_token: AccessToken,
1212
+ id_token: IdToken,
1213
+ refresh_token: RefreshToken,
1214
+ } = tokenData;
1215
+
1216
+ const idTokenPayload = this.decodeJwtPayload(IdToken);
1217
+ const userEmail = idTokenPayload.email;
1218
+ const firstName = idTokenPayload['given_name'] || idTokenPayload['name'] || '';
1219
+ const lastName = idTokenPayload['family_name'] || '';
1220
+ const userGroups = idTokenPayload['cognito:groups'];
1221
+
1222
+ if (!userEmail) {
1223
+ throw new Error('Email not found in ID attributes');
1224
+ }
1225
+
1226
+ let localUser = await findUserByEmail(userEmail, env);
1227
+ if (!localUser) {
1228
+ localUser = await ensureUser(userEmail, firstName, lastName, env);
1229
+ }
1230
+ const userId = localUser.lookup('id');
1231
+
1232
+ if (userGroups) {
1233
+ await ensureUserRoles(userId, userGroups, env);
1234
+ }
1235
+
1236
+ const localSession = await ensureUserSession(userId, IdToken, AccessToken, RefreshToken, env);
1237
+
1238
+ const sessionInfo: SessionInfo = {
1239
+ sessionId: localSession.lookup('id'),
1240
+ userId: userId,
1241
+ authToken: IdToken,
1242
+ idToken: IdToken,
1243
+ accessToken: AccessToken,
1244
+ refreshToken: RefreshToken,
1245
+ systemSesionInfo: tokenData,
1246
+ };
1247
+
1248
+ logger.info(`Auth callback successful for user ${userEmail}`);
1249
+ cb(sessionInfo);
1250
+ } catch (err: any) {
1251
+ console.log(err);
1252
+ logger.error(`Auth callback failed: ${sanitizeErrorMessage(err.message)}`);
1253
+ handleCognitoError(err, 'callback');
1254
+ }
1255
+ }
1256
+
1257
+ private decodeJwtPayload(token: string): any {
1258
+ try {
1259
+ const base64Url = token.split('.')[1];
1260
+ const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
1261
+ const jsonPayload = decodeURIComponent(
1262
+ atob(base64)
1263
+ .split('')
1264
+ .map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
1265
+ .join('')
1266
+ );
1267
+ return JSON.parse(jsonPayload);
1268
+ } catch (err) {
1269
+ logger.error(`Failed to decode JWT payload: ${err}`);
1270
+ throw new Error('Invalid JWT token');
1271
+ }
1272
+ }
1168
1273
  }
@@ -77,4 +77,5 @@ export interface AgentlangAuth {
77
77
  newPassword: string,
78
78
  env: Environment
79
79
  ): Promise<void>;
80
+ callback(code: string, env: Environment, cb: LoginCallback): Promise<void>;
80
81
  }