agentlang 0.0.3 → 0.0.4
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/README.md +16 -47
- package/out/api/http.d.ts.map +1 -1
- package/out/api/http.js +12 -0
- package/out/api/http.js.map +1 -1
- package/out/cli/main.d.ts +1 -3
- package/out/cli/main.d.ts.map +1 -1
- package/out/cli/main.js +6 -12
- package/out/cli/main.js.map +1 -1
- package/out/language/generated/ast.d.ts +80 -18
- package/out/language/generated/ast.d.ts.map +1 -1
- package/out/language/generated/ast.js +119 -26
- package/out/language/generated/ast.js.map +1 -1
- package/out/language/generated/grammar.d.ts +1 -1
- package/out/language/generated/grammar.d.ts.map +1 -1
- package/out/language/generated/grammar.js +831 -238
- package/out/language/generated/grammar.js.map +1 -1
- package/out/language/generated/module.d.ts +1 -1
- package/out/language/generated/module.js +1 -1
- package/out/language/main.cjs +995 -331
- package/out/language/main.cjs.map +2 -2
- package/out/language/parser.js +13 -6
- package/out/language/parser.js.map +1 -1
- package/out/runtime/agents/common.d.ts +1 -1
- package/out/runtime/agents/common.d.ts.map +1 -1
- package/out/runtime/agents/common.js +1 -1
- package/out/runtime/auth/cognito.d.ts +4 -1
- package/out/runtime/auth/cognito.d.ts.map +1 -1
- package/out/runtime/auth/cognito.js +540 -73
- package/out/runtime/auth/cognito.js.map +1 -1
- package/out/runtime/auth/defs.d.ts +3 -0
- package/out/runtime/auth/defs.d.ts.map +1 -1
- package/out/runtime/auth/defs.js +17 -1
- package/out/runtime/auth/defs.js.map +1 -1
- package/out/runtime/auth/interface.d.ts +6 -1
- package/out/runtime/auth/interface.d.ts.map +1 -1
- package/out/runtime/defs.d.ts +21 -0
- package/out/runtime/defs.d.ts.map +1 -1
- package/out/runtime/defs.js +35 -0
- package/out/runtime/defs.js.map +1 -1
- package/out/runtime/interpreter.d.ts.map +1 -1
- package/out/runtime/interpreter.js +45 -36
- package/out/runtime/interpreter.js.map +1 -1
- package/out/runtime/loader.d.ts +4 -2
- package/out/runtime/loader.d.ts.map +1 -1
- package/out/runtime/loader.js +148 -29
- package/out/runtime/loader.js.map +1 -1
- package/out/runtime/module.d.ts +48 -5
- package/out/runtime/module.d.ts.map +1 -1
- package/out/runtime/module.js +200 -9
- package/out/runtime/module.js.map +1 -1
- package/out/runtime/modules/ai.d.ts +7 -5
- package/out/runtime/modules/ai.d.ts.map +1 -1
- package/out/runtime/modules/ai.js +50 -24
- package/out/runtime/modules/ai.js.map +1 -1
- package/out/runtime/modules/auth.d.ts +17 -1
- package/out/runtime/modules/auth.d.ts.map +1 -1
- package/out/runtime/modules/auth.js +282 -30
- package/out/runtime/modules/auth.js.map +1 -1
- package/out/runtime/modules/core.d.ts.map +1 -1
- package/out/runtime/modules/core.js +3 -1
- package/out/runtime/modules/core.js.map +1 -1
- package/out/runtime/relgraph.d.ts.map +1 -1
- package/out/runtime/relgraph.js +2 -2
- package/out/runtime/relgraph.js.map +1 -1
- package/out/runtime/resolvers/interface.d.ts +37 -2
- package/out/runtime/resolvers/interface.d.ts.map +1 -1
- package/out/runtime/resolvers/interface.js +103 -5
- package/out/runtime/resolvers/interface.js.map +1 -1
- package/out/runtime/resolvers/registry.d.ts +3 -2
- package/out/runtime/resolvers/registry.d.ts.map +1 -1
- package/out/runtime/resolvers/registry.js +3 -0
- package/out/runtime/resolvers/registry.js.map +1 -1
- package/out/runtime/state.d.ts +31 -3
- package/out/runtime/state.d.ts.map +1 -1
- package/out/runtime/state.js +11 -1
- package/out/runtime/state.js.map +1 -1
- package/out/runtime/util.d.ts +4 -0
- package/out/runtime/util.d.ts.map +1 -1
- package/out/runtime/util.js +16 -0
- package/out/runtime/util.js.map +1 -1
- package/out/syntaxes/agentlang.monarch.js +2 -2
- package/out/syntaxes/agentlang.monarch.js.map +1 -1
- package/out/utils/http.d.ts +2 -0
- package/out/utils/http.d.ts.map +1 -0
- package/out/utils/http.js +5 -0
- package/out/utils/http.js.map +1 -0
- package/package.json +8 -6
- package/src/api/http.ts +15 -0
- package/src/cli/main.ts +6 -12
- package/src/language/agentlang.langium +31 -10
- package/src/language/generated/ast.ts +212 -44
- package/src/language/generated/grammar.ts +831 -238
- package/src/language/generated/module.ts +1 -1
- package/src/language/parser.ts +12 -8
- package/src/runtime/agents/common.ts +1 -1
- package/src/runtime/auth/cognito.ts +605 -74
- package/src/runtime/auth/defs.ts +17 -1
- package/src/runtime/auth/interface.ts +6 -1
- package/src/runtime/defs.ts +45 -0
- package/src/runtime/interpreter.ts +43 -34
- package/src/runtime/loader.ts +159 -30
- package/src/runtime/module.ts +243 -10
- package/src/runtime/modules/ai.ts +52 -28
- package/src/runtime/modules/auth.ts +330 -38
- package/src/runtime/modules/core.ts +3 -1
- package/src/runtime/relgraph.ts +2 -8
- package/src/runtime/resolvers/interface.ts +141 -6
- package/src/runtime/resolvers/registry.ts +5 -2
- package/src/runtime/state.ts +11 -1
- package/src/runtime/util.ts +17 -0
- package/src/syntaxes/agentlang.monarch.ts +2 -2
- package/src/utils/http.ts +5 -0
- package/src/index.ts +0 -29
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../../src/runtime/modules/ai.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAiD,MAAM,mBAAmB,CAAC;AAC/F,OAAO,EAAe,QAAQ,EAA8B,MAAM,cAAc,CAAC;AAEjF,OAAO,EACL,oBAAoB,EAKrB,MAAM,uBAAuB,CAAC;AAK/B,eAAO,MAAM,gBAAgB,QAA2B,CAAC;;
|
|
1
|
+
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../../src/runtime/modules/ai.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAiD,MAAM,mBAAmB,CAAC;AAC/F,OAAO,EAAe,QAAQ,EAA8B,MAAM,cAAc,CAAC;AAEjF,OAAO,EACL,oBAAoB,EAKrB,MAAM,uBAAuB,CAAC;AAK/B,eAAO,MAAM,gBAAgB,QAA2B,CAAC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qName } from '../util.js';
|
|
2
2
|
import { makeEventEvaluator, parseAndEvaluateStatement } from '../interpreter.js';
|
|
3
3
|
import { fetchModule, instanceToObject, isModule } from '../module.js';
|
|
4
4
|
import { provider } from '../agents/registry.js';
|
|
@@ -7,20 +7,22 @@ import { AIMessage, HumanMessage } from '@langchain/core/messages';
|
|
|
7
7
|
import { PlannerInstructions } from '../agents/common.js';
|
|
8
8
|
import { PathAttributeNameQuery } from '../defs.js';
|
|
9
9
|
export const CoreAIModuleName = makeCoreModuleName('ai');
|
|
10
|
+
export const AgentEntityName = 'Agent';
|
|
11
|
+
export const LlmEntityName = 'LLM';
|
|
10
12
|
export default `module ${CoreAIModuleName}
|
|
11
13
|
|
|
12
|
-
entity
|
|
14
|
+
entity ${LlmEntityName} {
|
|
13
15
|
name String @id,
|
|
14
16
|
service String @default("openai"),
|
|
15
17
|
config Map @optional
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
entity
|
|
20
|
+
entity ${AgentEntityName} {
|
|
19
21
|
name String @id,
|
|
20
22
|
type @enum("chat", "planner") @default("chat"),
|
|
21
23
|
instruction String @optional,
|
|
22
|
-
tools String
|
|
23
|
-
documents String
|
|
24
|
+
tools String @optional, // comma-separated values
|
|
25
|
+
documents String @optional, // comma-separated values
|
|
24
26
|
llm String
|
|
25
27
|
}
|
|
26
28
|
|
|
@@ -35,18 +37,18 @@ workflow findAgentChatSession {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
workflow saveAgentChatSession {
|
|
38
|
-
|
|
40
|
+
{agentChatSession {id saveAgentChatSession.id, messages saveAgentChatSession.messages}, @upsert}
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
entity
|
|
43
|
+
entity Document {
|
|
42
44
|
title String @id,
|
|
43
45
|
content String,
|
|
44
46
|
@meta {"fullTextSearch": "*"}
|
|
45
47
|
}
|
|
46
48
|
`;
|
|
47
|
-
export const AgentFqName = makeFqName(CoreAIModuleName,
|
|
49
|
+
export const AgentFqName = makeFqName(CoreAIModuleName, AgentEntityName);
|
|
48
50
|
const ProviderDb = new Map();
|
|
49
|
-
export class
|
|
51
|
+
export class AgentInstance {
|
|
50
52
|
constructor() {
|
|
51
53
|
this.llm = '';
|
|
52
54
|
this.name = '';
|
|
@@ -54,7 +56,7 @@ export class Agent {
|
|
|
54
56
|
this.type = 'chat';
|
|
55
57
|
}
|
|
56
58
|
static FromInstance(agentInstance) {
|
|
57
|
-
return instanceToObject(agentInstance, new
|
|
59
|
+
return instanceToObject(agentInstance, new AgentInstance());
|
|
58
60
|
}
|
|
59
61
|
isPlanner() {
|
|
60
62
|
return (this.tools && this.tools.length > 0) || this.type == 'planner';
|
|
@@ -93,13 +95,13 @@ export class Agent {
|
|
|
93
95
|
}
|
|
94
96
|
async maybeAddRelevantDocuments(message, env) {
|
|
95
97
|
if (this.documents && this.documents.length > 0) {
|
|
96
|
-
const s = `${message}. Relevant documents are: ${this.documents
|
|
97
|
-
const result = await parseHelper(`{agentlang_ai/
|
|
98
|
+
const s = `${message}. Relevant documents are: ${this.documents}`;
|
|
99
|
+
const result = await parseHelper(`{agentlang_ai/Document? "${s}"}`, env);
|
|
98
100
|
if (result && result.length > 0) {
|
|
99
101
|
const docs = [];
|
|
100
102
|
for (let i = 0; i < result.length; ++i) {
|
|
101
103
|
const v = result[i];
|
|
102
|
-
const r = await parseHelper(`{agentlang_ai/
|
|
104
|
+
const r = await parseHelper(`{agentlang_ai/Document {${PathAttributeNameQuery} "${v.id}"}}`, env);
|
|
103
105
|
if (r && r.length > 0) {
|
|
104
106
|
docs.push(r[0]);
|
|
105
107
|
}
|
|
@@ -117,14 +119,38 @@ export class Agent {
|
|
|
117
119
|
}
|
|
118
120
|
toolsAsString() {
|
|
119
121
|
if (this.tools) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
122
|
+
const tooldefs = new Array();
|
|
123
|
+
const slimModules = new Map();
|
|
124
|
+
this.tools.split(',').forEach((n) => {
|
|
125
|
+
let moduleName;
|
|
126
|
+
let entryName;
|
|
127
|
+
if (isFqName(n)) {
|
|
128
|
+
const parts = splitFqName(n);
|
|
129
|
+
moduleName = parts.getModuleName();
|
|
130
|
+
entryName = parts.getEntryName();
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
moduleName = n;
|
|
134
|
+
}
|
|
135
|
+
if (isModule(moduleName)) {
|
|
136
|
+
const m = fetchModule(moduleName);
|
|
137
|
+
if (entryName) {
|
|
138
|
+
const hasmod = slimModules.has(moduleName);
|
|
139
|
+
const defs = hasmod ? slimModules.get(moduleName) : new Array();
|
|
140
|
+
defs === null || defs === void 0 ? void 0 : defs.push(m.getEntry(entryName).toString());
|
|
141
|
+
if (!hasmod && defs) {
|
|
142
|
+
slimModules.set(moduleName, defs);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
tooldefs.push(fetchModule(moduleName).toString());
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
slimModules.forEach((defs, modName) => {
|
|
151
|
+
tooldefs.push(`module ${modName}\n${defs.join('\n')}`);
|
|
152
|
+
});
|
|
153
|
+
return tooldefs.join('\n');
|
|
128
154
|
}
|
|
129
155
|
else {
|
|
130
156
|
return '';
|
|
@@ -136,10 +162,10 @@ async function parseHelper(stmt, env) {
|
|
|
136
162
|
return env.getLastResult();
|
|
137
163
|
}
|
|
138
164
|
export async function findAgentByName(name, env) {
|
|
139
|
-
const result = await parseHelper(`{
|
|
165
|
+
const result = await parseHelper(`{${AgentFqName} {name? "${name}"}}`, env);
|
|
140
166
|
if (result instanceof Array && result.length > 0) {
|
|
141
167
|
const agentInstance = result[0];
|
|
142
|
-
return
|
|
168
|
+
return AgentInstance.FromInstance(agentInstance);
|
|
143
169
|
}
|
|
144
170
|
else {
|
|
145
171
|
throw new Error(`Failed to fine agent ${name}`);
|
|
@@ -148,7 +174,7 @@ export async function findAgentByName(name, env) {
|
|
|
148
174
|
export async function findProviderForLLM(llmName, env) {
|
|
149
175
|
let p = ProviderDb.get(llmName);
|
|
150
176
|
if (p == undefined) {
|
|
151
|
-
const result = await parseAndEvaluateStatement(`{${CoreAIModuleName}
|
|
177
|
+
const result = await parseAndEvaluateStatement(`{${CoreAIModuleName}/${LlmEntityName} {name? "${llmName}"}}`, undefined, env);
|
|
152
178
|
if (result.length > 0) {
|
|
153
179
|
const llm = result[0];
|
|
154
180
|
const service = llm.lookup('service');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai.js","sourceRoot":"","sources":["../../../src/runtime/modules/ai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"ai.js","sourceRoot":"","sources":["../../../src/runtime/modules/ai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACnF,OAAO,EAAe,kBAAkB,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAC/F,OAAO,EAAE,WAAW,EAAY,gBAAgB,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAGL,gBAAgB,EAChB,YAAY,EACZ,aAAa,GACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAe,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEpD,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC;AACzD,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC;AACvC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AE,OAAO,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC;QAC5C,CAAC;aAAM,IAAI,EAAE,YAAY,SAAS,EAAE,CAAC;YACnC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC;QACjD,CAAC;aAAM,CAAC;YACN,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC;QAC9C,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,MAAc,EACd,GAAgB;IAEhB,MAAM,MAAM,GAAoB,MAAM,SAAS,CAAC,sBAAsB,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,GAAG,CAAC,CAAC;IAC7F,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,UAAU,EAAE,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3F,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,MAAc,EAAE,QAAe,EAAE,GAAgB;IAC1F,MAAM,SAAS,CACb,sBAAsB,EACtB,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC,EAAE,EACrE,GAAG,CACJ,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,aAAuB;IAC/C,OAAO,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -2,6 +2,7 @@ import { Result, Environment } from '../interpreter.js';
|
|
|
2
2
|
import { RbacPermissionFlag } from '../module.js';
|
|
3
3
|
import { UserInfo } from '../auth/interface.js';
|
|
4
4
|
import { ActiveSessionInfo } from '../auth/defs.js';
|
|
5
|
+
import { getHttpStatusForError } from '../auth/cognito.js';
|
|
5
6
|
export declare const CoreAuthModuleName: string;
|
|
6
7
|
declare const _default: string;
|
|
7
8
|
export default _default;
|
|
@@ -26,7 +27,22 @@ export declare const canUserRead: PermCheckForUser;
|
|
|
26
27
|
export declare const canUserUpdate: PermCheckForUser;
|
|
27
28
|
export declare const canUserDelete: PermCheckForUser;
|
|
28
29
|
export declare function signUpUser(username: string, password: string, userData: object, env: Environment): Promise<UserInfo>;
|
|
29
|
-
export declare function loginUser(username: string, password: string, env: Environment): Promise<string>;
|
|
30
|
+
export declare function loginUser(username: string, password: string, env: Environment): Promise<string | object>;
|
|
30
31
|
export declare function verifySession(token: string, env?: Environment): Promise<ActiveSessionInfo>;
|
|
32
|
+
export declare function getUserInfo(userId: string, env: Environment): Promise<UserInfo>;
|
|
33
|
+
export declare function getUserInfoByEmail(email: string, env: Environment): Promise<UserInfo>;
|
|
31
34
|
export declare function requireAuth(moduleName: string, eventName: string): boolean;
|
|
35
|
+
export { getHttpStatusForError };
|
|
36
|
+
export declare function createAuthErrorResponse(error: Error): {
|
|
37
|
+
error: string;
|
|
38
|
+
message: string;
|
|
39
|
+
statusCode: number;
|
|
40
|
+
};
|
|
41
|
+
export declare function isAuthError(error: any): boolean;
|
|
42
|
+
export declare function sanitizeErrorForLogging(error: Error): {
|
|
43
|
+
name: string;
|
|
44
|
+
message: string;
|
|
45
|
+
sanitizedMessage: string;
|
|
46
|
+
};
|
|
47
|
+
export declare function isRetryableError(error: Error): boolean;
|
|
32
48
|
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../src/runtime/modules/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAsB,MAAM,mBAAmB,CAAC;AAE5E,OAAO,
|
|
1
|
+
{"version":3,"file":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AAM,CAAC;IAChB,gBAAgB,EAAE,MAAM,CAAC;CAC1B,CAeA;AAGD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAUtD"}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Environment, makeEventEvaluator } from '../interpreter.js';
|
|
2
2
|
import { logger } from '../logger.js';
|
|
3
|
-
import { RbacPermissionFlag } from '../module.js';
|
|
3
|
+
import { makeInstance, newInstanceAttributes, RbacPermissionFlag } from '../module.js';
|
|
4
4
|
import { makeCoreModuleName } from '../util.js';
|
|
5
5
|
import { isSqlTrue } from '../resolvers/sqldb/dbutil.js';
|
|
6
|
-
import { AdminUserId, BypassSession, isAuthEnabled } from '../auth/defs.js';
|
|
6
|
+
import { AdminUserId, BypassSession, isAuthEnabled, isRbacEnabled, } from '../auth/defs.js';
|
|
7
7
|
import { isNodeEnv } from '../../utils/runtime.js';
|
|
8
|
-
import { CognitoAuth } from '../auth/cognito.js';
|
|
8
|
+
import { CognitoAuth, getHttpStatusForError } from '../auth/cognito.js';
|
|
9
|
+
import { UnauthorisedError, UserNotFoundError, UserNotConfirmedError, PasswordResetRequiredError, TooManyRequestsError, InvalidParameterError, ExpiredCodeError, CodeMismatchError, BadRequestError, } from '../defs.js';
|
|
9
10
|
export const CoreAuthModuleName = makeCoreModuleName('auth');
|
|
10
11
|
export default `module ${CoreAuthModuleName}
|
|
11
12
|
|
|
@@ -53,7 +54,7 @@ entity Permission {
|
|
|
53
54
|
relationship RolePermission between(Role, Permission)
|
|
54
55
|
|
|
55
56
|
workflow CreateRole {
|
|
56
|
-
|
|
57
|
+
{Role {name CreateRole.name}, @upsert}
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
workflow FindRole {
|
|
@@ -64,13 +65,13 @@ workflow FindRole {
|
|
|
64
65
|
workflow AssignUserToRole {
|
|
65
66
|
{User {id? AssignUserToRole.userId}} as [user];
|
|
66
67
|
{Role {name? AssignUserToRole.roleName}} as [role];
|
|
67
|
-
|
|
68
|
+
{UserRole {User user, Role role}, @upsert}
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
workflow AssignUserToRoleByEmail {
|
|
71
72
|
{User {email? AssignUserToRoleByEmail.email}} as [user];
|
|
72
73
|
{Role {name? AssignUserToRoleByEmail.roleName}} as [role];
|
|
73
|
-
|
|
74
|
+
{UserRole {User user, Role role}, @upsert}
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
workflow FindUserRoles {
|
|
@@ -79,19 +80,20 @@ workflow FindUserRoles {
|
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
workflow CreatePermission {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
{Permission {id CreatePermission.id,
|
|
84
|
+
resourceFqName CreatePermission.resourceFqName,
|
|
85
|
+
c CreatePermission.c,
|
|
86
|
+
r CreatePermission.r,
|
|
87
|
+
u CreatePermission.u,
|
|
88
|
+
d CreatePermission.d},
|
|
89
|
+
RolePermission {Role {name? CreatePermission.roleName}},
|
|
90
|
+
@upsert}
|
|
89
91
|
}
|
|
90
92
|
|
|
91
93
|
workflow AddPermissionToRole {
|
|
92
94
|
{Role {name? AddPermissionToRole.roleName}} as role;
|
|
93
95
|
{Permission {id? AddPermissionToRole.permissionId}} as perm;
|
|
94
|
-
|
|
96
|
+
{RolePermission {Role role, Permission perm}, @upsert}
|
|
95
97
|
}
|
|
96
98
|
|
|
97
99
|
workflow FindRolePermissions {
|
|
@@ -106,6 +108,7 @@ entity Session {
|
|
|
106
108
|
isActive Boolean
|
|
107
109
|
}
|
|
108
110
|
|
|
111
|
+
|
|
109
112
|
workflow CreateSession {
|
|
110
113
|
{Session {id CreateSession.id, userId CreateSession.userId,
|
|
111
114
|
authToken CreateSession.authToken, isActive true}}
|
|
@@ -125,6 +128,7 @@ workflow RemoveSession {
|
|
|
125
128
|
purge {Session {id? RemoveSession.id}}
|
|
126
129
|
}
|
|
127
130
|
|
|
131
|
+
|
|
128
132
|
workflow signup {
|
|
129
133
|
await Auth.signUpUser(signup.email, signup.password, signup.userData)
|
|
130
134
|
}
|
|
@@ -132,6 +136,14 @@ workflow signup {
|
|
|
132
136
|
workflow login {
|
|
133
137
|
await Auth.loginUser(login.email, login.password)
|
|
134
138
|
}
|
|
139
|
+
|
|
140
|
+
workflow getUser {
|
|
141
|
+
await Auth.getUserInfo(getUser.userId)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
workflow getUserByEmail {
|
|
145
|
+
await Auth.getUserInfoByEmail(getUserByEmail.email)
|
|
146
|
+
}
|
|
135
147
|
`;
|
|
136
148
|
const evalEvent = makeEventEvaluator(CoreAuthModuleName);
|
|
137
149
|
export async function createUser(id, email, firstName, lastName, env) {
|
|
@@ -217,11 +229,20 @@ export async function assignUserToRole(userId, roleName, env) {
|
|
|
217
229
|
});
|
|
218
230
|
return r;
|
|
219
231
|
}
|
|
232
|
+
let DefaultRoleInstance;
|
|
220
233
|
export async function findUserRoles(userId, env) {
|
|
221
234
|
const result = await evalEvent('FindUserRoles', { userId: userId }, env);
|
|
222
235
|
const inst = result ? result[0] : undefined;
|
|
223
236
|
if (inst) {
|
|
224
|
-
|
|
237
|
+
let roles = inst.getRelatedInstances('UserRole');
|
|
238
|
+
if (roles == undefined) {
|
|
239
|
+
roles = [];
|
|
240
|
+
}
|
|
241
|
+
if (DefaultRoleInstance == undefined) {
|
|
242
|
+
DefaultRoleInstance = makeInstance(CoreAuthModuleName, 'Role', newInstanceAttributes().set('name', '*'));
|
|
243
|
+
}
|
|
244
|
+
roles.push(DefaultRoleInstance);
|
|
245
|
+
return roles;
|
|
225
246
|
}
|
|
226
247
|
return undefined;
|
|
227
248
|
}
|
|
@@ -243,7 +264,7 @@ async function updatePermissionCacheForRole(role, env) {
|
|
|
243
264
|
}
|
|
244
265
|
}
|
|
245
266
|
export async function userHasPermissions(userId, resourceFqName, perms, env) {
|
|
246
|
-
if (userId == AdminUserId) {
|
|
267
|
+
if (userId == AdminUserId || !isRbacEnabled()) {
|
|
247
268
|
return true;
|
|
248
269
|
}
|
|
249
270
|
let userRoles = UserRoleCache.get(userId);
|
|
@@ -316,33 +337,181 @@ function fetchAuthImpl() {
|
|
|
316
337
|
}
|
|
317
338
|
export async function signUpUser(username, password, userData, env) {
|
|
318
339
|
let result;
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
340
|
+
try {
|
|
341
|
+
await fetchAuthImpl().signUp(username, password, userData ? new Map(Object.entries(userData)) : undefined, env, (userInfo) => {
|
|
342
|
+
result = userInfo;
|
|
343
|
+
});
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
catch (err) {
|
|
347
|
+
logger.error(`Signup failed for ${username}: ${err.message}`);
|
|
348
|
+
throw err; // Re-throw to preserve error type for HTTP status mapping
|
|
349
|
+
}
|
|
323
350
|
}
|
|
324
351
|
export async function loginUser(username, password, env) {
|
|
325
352
|
let result = '';
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
353
|
+
try {
|
|
354
|
+
await fetchAuthImpl().login(username, password, env, (r) => {
|
|
355
|
+
// Check if Cognito is configured by checking if we have the tokens
|
|
356
|
+
if (r.idToken && r.accessToken && r.refreshToken) {
|
|
357
|
+
// Return full token response for Cognito
|
|
358
|
+
result = {
|
|
359
|
+
id_token: r.idToken,
|
|
360
|
+
access_token: r.accessToken,
|
|
361
|
+
refresh_token: r.refreshToken,
|
|
362
|
+
token_type: 'Bearer',
|
|
363
|
+
expires_in: 3600,
|
|
364
|
+
userId: r.userId,
|
|
365
|
+
sessionId: r.sessionId,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
// Return string format for non-Cognito authentication
|
|
370
|
+
result = `${r.userId}/${r.sessionId}`;
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
catch (err) {
|
|
376
|
+
logger.error(`Login failed for ${username}: ${err.message}`);
|
|
377
|
+
throw err; // Re-throw to preserve error type for HTTP status mapping
|
|
378
|
+
}
|
|
330
379
|
}
|
|
331
380
|
export async function verifySession(token, env) {
|
|
332
381
|
if (!isAuthEnabled())
|
|
333
382
|
return BypassSession;
|
|
383
|
+
// Check if token is a JWT (Cognito ID token) or userId/sessionId format
|
|
384
|
+
if (isJwtToken(token)) {
|
|
385
|
+
return await verifyJwtToken(token, env);
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
return await verifySessionToken(token, env);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function isJwtToken(token) {
|
|
392
|
+
// Simple JWT structure check - JWT tokens have 3 parts separated by dots
|
|
393
|
+
return !!(token && typeof token === 'string' && token.split('.').length === 3);
|
|
394
|
+
}
|
|
395
|
+
async function verifyJwtToken(token, env) {
|
|
396
|
+
const needCommit = env ? false : true;
|
|
397
|
+
env = env ? env : new Environment();
|
|
398
|
+
const f = async () => {
|
|
399
|
+
try {
|
|
400
|
+
// Validate JWT structure first
|
|
401
|
+
if (!isJwtToken(token)) {
|
|
402
|
+
throw new UnauthorisedError('Invalid JWT token structure');
|
|
403
|
+
}
|
|
404
|
+
// Verify the JWT token directly with Cognito
|
|
405
|
+
await fetchAuthImpl().verifyToken(token, env);
|
|
406
|
+
// Extract user information from JWT payload
|
|
407
|
+
const parts = token.split('.');
|
|
408
|
+
const payload = JSON.parse(atob(parts[1]));
|
|
409
|
+
// Extract user ID from standard JWT claims (sub or cognito:username)
|
|
410
|
+
const userId = payload.sub || payload['cognito:username'];
|
|
411
|
+
const email = payload.email || payload['cognito:username'];
|
|
412
|
+
if (!userId) {
|
|
413
|
+
throw new UnauthorisedError('Invalid JWT token: missing user identifier');
|
|
414
|
+
}
|
|
415
|
+
let localUser = null;
|
|
416
|
+
if (email) {
|
|
417
|
+
localUser = await findUserByEmail(email, env);
|
|
418
|
+
}
|
|
419
|
+
if (!localUser && userId) {
|
|
420
|
+
localUser = await findUser(userId, env);
|
|
421
|
+
}
|
|
422
|
+
if (!localUser) {
|
|
423
|
+
logger.warn(`User not found in local database for JWT token. Email: ${email}, UserId: ${userId}`);
|
|
424
|
+
throw new UnauthorisedError(`User not found in local database`);
|
|
425
|
+
}
|
|
426
|
+
// Use the local user's ID for consistency
|
|
427
|
+
const localUserId = localUser.lookup('id');
|
|
428
|
+
// For JWT tokens, we use the token itself as sessionId for tracking
|
|
429
|
+
return { sessionId: token.substring(0, 32), userId: localUserId };
|
|
430
|
+
}
|
|
431
|
+
catch (err) {
|
|
432
|
+
if (err instanceof UnauthorisedError) {
|
|
433
|
+
throw err;
|
|
434
|
+
}
|
|
435
|
+
logger.error(`JWT token verification failed:`, {
|
|
436
|
+
errorName: err.name,
|
|
437
|
+
errorMessage: err.message,
|
|
438
|
+
});
|
|
439
|
+
throw new UnauthorisedError('JWT token verification failed');
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
if (needCommit) {
|
|
443
|
+
return await env.callInTransaction(f);
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
return await f();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
async function verifySessionToken(token, env) {
|
|
334
450
|
const parts = token.split('/');
|
|
335
451
|
const sessId = parts[1];
|
|
336
452
|
const needCommit = env ? false : true;
|
|
337
453
|
env = env ? env : new Environment();
|
|
338
454
|
const f = async () => {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
455
|
+
try {
|
|
456
|
+
const sess = await findSession(sessId, env);
|
|
457
|
+
if (sess != undefined) {
|
|
458
|
+
await fetchAuthImpl().verifyToken(sess.lookup('authToken'), env);
|
|
459
|
+
return { sessionId: sessId, userId: parts[0] };
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
logger.warn(`No active session found for user '${parts[0]}'`);
|
|
463
|
+
throw new UnauthorisedError(`No active session for user '${parts[0]}'`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
catch (err) {
|
|
467
|
+
if (err instanceof UnauthorisedError) {
|
|
468
|
+
throw err;
|
|
469
|
+
}
|
|
470
|
+
// Log error details for debugging
|
|
471
|
+
logger.error(`Session verification failed for user '${parts[0]}':`, {
|
|
472
|
+
errorName: err.name,
|
|
473
|
+
errorMessage: err.message,
|
|
474
|
+
sessionId: sessId,
|
|
475
|
+
});
|
|
476
|
+
throw new UnauthorisedError('Session verification failed');
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
if (needCommit) {
|
|
480
|
+
return await env.callInTransaction(f);
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
return await f();
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
export async function getUserInfo(userId, env) {
|
|
487
|
+
const needCommit = env ? false : true;
|
|
488
|
+
env = env ? env : new Environment();
|
|
489
|
+
const f = async () => {
|
|
490
|
+
try {
|
|
491
|
+
return await fetchAuthImpl().getUser(userId, env);
|
|
492
|
+
}
|
|
493
|
+
catch (err) {
|
|
494
|
+
logger.error(`Failed to get user info for ${userId}: ${err.message}`);
|
|
495
|
+
throw err; // Re-throw to preserve error type
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
if (needCommit) {
|
|
499
|
+
return await env.callInTransaction(f);
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
return await f();
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
export async function getUserInfoByEmail(email, env) {
|
|
506
|
+
const needCommit = env ? false : true;
|
|
507
|
+
env = env ? env : new Environment();
|
|
508
|
+
const f = async () => {
|
|
509
|
+
try {
|
|
510
|
+
return await fetchAuthImpl().getUserByEmail(email, env);
|
|
343
511
|
}
|
|
344
|
-
|
|
345
|
-
|
|
512
|
+
catch (err) {
|
|
513
|
+
logger.error(`Failed to get user info for email ${email}: ${err.message}`);
|
|
514
|
+
throw err; // Re-throw to preserve error type
|
|
346
515
|
}
|
|
347
516
|
};
|
|
348
517
|
if (needCommit) {
|
|
@@ -356,4 +525,87 @@ export function requireAuth(moduleName, eventName) {
|
|
|
356
525
|
const f = moduleName == CoreAuthModuleName && (eventName == 'login' || eventName == 'signup');
|
|
357
526
|
return !f;
|
|
358
527
|
}
|
|
528
|
+
// Export getHttpStatusForError for use in HTTP handlers
|
|
529
|
+
export { getHttpStatusForError };
|
|
530
|
+
// Helper function to create standardized error responses
|
|
531
|
+
export function createAuthErrorResponse(error) {
|
|
532
|
+
const statusCode = getHttpStatusForError(error);
|
|
533
|
+
let errorType = 'AUTHENTICATION_ERROR';
|
|
534
|
+
if (error instanceof UserNotFoundError) {
|
|
535
|
+
errorType = 'USER_NOT_FOUND';
|
|
536
|
+
}
|
|
537
|
+
else if (error instanceof UnauthorisedError) {
|
|
538
|
+
errorType = 'UNAUTHORIZED';
|
|
539
|
+
}
|
|
540
|
+
else if (error instanceof UserNotConfirmedError) {
|
|
541
|
+
errorType = 'USER_NOT_CONFIRMED';
|
|
542
|
+
}
|
|
543
|
+
else if (error instanceof PasswordResetRequiredError) {
|
|
544
|
+
errorType = 'PASSWORD_RESET_REQUIRED';
|
|
545
|
+
}
|
|
546
|
+
else if (error instanceof TooManyRequestsError) {
|
|
547
|
+
errorType = 'TOO_MANY_REQUESTS';
|
|
548
|
+
}
|
|
549
|
+
else if (error instanceof InvalidParameterError) {
|
|
550
|
+
errorType = 'INVALID_PARAMETER';
|
|
551
|
+
}
|
|
552
|
+
else if (error instanceof ExpiredCodeError) {
|
|
553
|
+
errorType = 'EXPIRED_CODE';
|
|
554
|
+
}
|
|
555
|
+
else if (error instanceof CodeMismatchError) {
|
|
556
|
+
errorType = 'CODE_MISMATCH';
|
|
557
|
+
}
|
|
558
|
+
else if (error instanceof BadRequestError) {
|
|
559
|
+
errorType = 'BAD_REQUEST';
|
|
560
|
+
}
|
|
561
|
+
// Log error creation for debugging purposes
|
|
562
|
+
logger.debug(`Creating auth error response:`, {
|
|
563
|
+
errorType: errorType,
|
|
564
|
+
statusCode: statusCode,
|
|
565
|
+
originalError: error.name,
|
|
566
|
+
});
|
|
567
|
+
return {
|
|
568
|
+
error: errorType,
|
|
569
|
+
message: error.message,
|
|
570
|
+
statusCode: statusCode,
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
// Helper function to check if an error is a known auth error
|
|
574
|
+
export function isAuthError(error) {
|
|
575
|
+
return (error instanceof UnauthorisedError ||
|
|
576
|
+
error instanceof UserNotFoundError ||
|
|
577
|
+
error instanceof UserNotConfirmedError ||
|
|
578
|
+
error instanceof PasswordResetRequiredError ||
|
|
579
|
+
error instanceof TooManyRequestsError ||
|
|
580
|
+
error instanceof InvalidParameterError ||
|
|
581
|
+
error instanceof ExpiredCodeError ||
|
|
582
|
+
error instanceof CodeMismatchError ||
|
|
583
|
+
error instanceof BadRequestError);
|
|
584
|
+
}
|
|
585
|
+
// Helper function to sanitize error details before logging
|
|
586
|
+
export function sanitizeErrorForLogging(error) {
|
|
587
|
+
const sanitizedMessage = error.message
|
|
588
|
+
.replace(/password/gi, '[REDACTED]')
|
|
589
|
+
.replace(/token/gi, '[REDACTED]')
|
|
590
|
+
.replace(/secret/gi, '[REDACTED]')
|
|
591
|
+
.replace(/key/gi, '[REDACTED]')
|
|
592
|
+
.replace(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g, '[EMAIL_REDACTED]')
|
|
593
|
+
.replace(/\b[A-Fa-f0-9]{32,}\b/g, '[TOKEN_REDACTED]')
|
|
594
|
+
.replace(/\b\d{4,}\b/g, '[NUMBER_REDACTED]');
|
|
595
|
+
return {
|
|
596
|
+
name: error.name,
|
|
597
|
+
message: error.message,
|
|
598
|
+
sanitizedMessage: sanitizedMessage,
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
// Helper function to determine if an error should be retried
|
|
602
|
+
export function isRetryableError(error) {
|
|
603
|
+
// Only retry on certain types of errors
|
|
604
|
+
return (error instanceof TooManyRequestsError ||
|
|
605
|
+
(error.message
|
|
606
|
+
? error.message.includes('temporarily unavailable') ||
|
|
607
|
+
error.message.includes('service error') ||
|
|
608
|
+
error.message.includes('timeout')
|
|
609
|
+
: false));
|
|
610
|
+
}
|
|
359
611
|
//# sourceMappingURL=auth.js.map
|