@peers-app/peers-sdk 0.1.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 +1 -0
- package/dist/context/data-context.d.ts +31 -0
- package/dist/context/data-context.js +56 -0
- package/dist/context/index.d.ts +3 -0
- package/dist/context/index.js +19 -0
- package/dist/context/user-context-singleton.d.ts +11 -0
- package/dist/context/user-context-singleton.js +121 -0
- package/dist/context/user-context.d.ts +55 -0
- package/dist/context/user-context.js +205 -0
- package/dist/data/assistants.d.ts +68 -0
- package/dist/data/assistants.js +64 -0
- package/dist/data/change-tracking.d.ts +219 -0
- package/dist/data/change-tracking.js +119 -0
- package/dist/data/channels.d.ts +29 -0
- package/dist/data/channels.js +25 -0
- package/dist/data/data-locks.d.ts +37 -0
- package/dist/data/data-locks.js +180 -0
- package/dist/data/data-locks.test.d.ts +1 -0
- package/dist/data/data-locks.test.js +456 -0
- package/dist/data/device-sync-info.d.ts +19 -0
- package/dist/data/device-sync-info.js +24 -0
- package/dist/data/devices.d.ts +51 -0
- package/dist/data/devices.js +36 -0
- package/dist/data/embeddings.d.ts +47 -0
- package/dist/data/embeddings.js +36 -0
- package/dist/data/files/file-read-stream.d.ts +27 -0
- package/dist/data/files/file-read-stream.js +195 -0
- package/dist/data/files/file-write-stream.d.ts +20 -0
- package/dist/data/files/file-write-stream.js +113 -0
- package/dist/data/files/file.types.d.ts +47 -0
- package/dist/data/files/file.types.js +55 -0
- package/dist/data/files/files.d.ts +28 -0
- package/dist/data/files/files.js +127 -0
- package/dist/data/files/files.test.d.ts +1 -0
- package/dist/data/files/files.test.js +728 -0
- package/dist/data/files/index.d.ts +4 -0
- package/dist/data/files/index.js +23 -0
- package/dist/data/group-member-roles.d.ts +9 -0
- package/dist/data/group-member-roles.js +25 -0
- package/dist/data/group-members.d.ts +39 -0
- package/dist/data/group-members.js +68 -0
- package/dist/data/group-members.test.d.ts +1 -0
- package/dist/data/group-members.test.js +287 -0
- package/dist/data/group-permissions.d.ts +8 -0
- package/dist/data/group-permissions.js +73 -0
- package/dist/data/group-share.d.ts +50 -0
- package/dist/data/group-share.js +196 -0
- package/dist/data/groups.d.ts +50 -0
- package/dist/data/groups.js +73 -0
- package/dist/data/groups.test.d.ts +1 -0
- package/dist/data/groups.test.js +153 -0
- package/dist/data/index.d.ts +31 -0
- package/dist/data/index.js +47 -0
- package/dist/data/knowledge/knowledge-frames.d.ts +34 -0
- package/dist/data/knowledge/knowledge-frames.js +34 -0
- package/dist/data/knowledge/knowledge-links.d.ts +30 -0
- package/dist/data/knowledge/knowledge-links.js +25 -0
- package/dist/data/knowledge/knowledge-values.d.ts +35 -0
- package/dist/data/knowledge/knowledge-values.js +35 -0
- package/dist/data/knowledge/peer-types.d.ts +112 -0
- package/dist/data/knowledge/peer-types.js +27 -0
- package/dist/data/knowledge/predicates.d.ts +34 -0
- package/dist/data/knowledge/predicates.js +27 -0
- package/dist/data/messages.d.ts +57 -0
- package/dist/data/messages.js +97 -0
- package/dist/data/orm/client-proxy.data-source.d.ts +27 -0
- package/dist/data/orm/client-proxy.data-source.js +65 -0
- package/dist/data/orm/cursor.d.ts +25 -0
- package/dist/data/orm/cursor.js +47 -0
- package/dist/data/orm/cursor.test.d.ts +1 -0
- package/dist/data/orm/cursor.test.js +315 -0
- package/dist/data/orm/data-query.d.ts +96 -0
- package/dist/data/orm/data-query.js +208 -0
- package/dist/data/orm/data-query.mongo.d.ts +17 -0
- package/dist/data/orm/data-query.mongo.js +267 -0
- package/dist/data/orm/data-query.mongo.test.d.ts +1 -0
- package/dist/data/orm/data-query.mongo.test.js +398 -0
- package/dist/data/orm/data-query.sqlite.d.ts +14 -0
- package/dist/data/orm/data-query.sqlite.js +297 -0
- package/dist/data/orm/data-query.sqlite.test.d.ts +1 -0
- package/dist/data/orm/data-query.sqlite.test.js +377 -0
- package/dist/data/orm/data-query.test.d.ts +1 -0
- package/dist/data/orm/data-query.test.js +553 -0
- package/dist/data/orm/decorators.d.ts +6 -0
- package/dist/data/orm/decorators.js +21 -0
- package/dist/data/orm/dependency-injection.test.d.ts +1 -0
- package/dist/data/orm/dependency-injection.test.js +171 -0
- package/dist/data/orm/doc.d.ts +26 -0
- package/dist/data/orm/doc.js +124 -0
- package/dist/data/orm/event-registry.d.ts +24 -0
- package/dist/data/orm/event-registry.js +40 -0
- package/dist/data/orm/event-registry.test.d.ts +1 -0
- package/dist/data/orm/event-registry.test.js +44 -0
- package/dist/data/orm/factory.d.ts +8 -0
- package/dist/data/orm/factory.js +147 -0
- package/dist/data/orm/index.d.ts +16 -0
- package/dist/data/orm/index.js +32 -0
- package/dist/data/orm/multi-cursors.d.ts +11 -0
- package/dist/data/orm/multi-cursors.js +146 -0
- package/dist/data/orm/multi-cursors.test.d.ts +1 -0
- package/dist/data/orm/multi-cursors.test.js +455 -0
- package/dist/data/orm/sql-db.d.ts +6 -0
- package/dist/data/orm/sql-db.js +2 -0
- package/dist/data/orm/sql.data-source.d.ts +38 -0
- package/dist/data/orm/sql.data-source.js +379 -0
- package/dist/data/orm/sql.data-source.test.d.ts +1 -0
- package/dist/data/orm/sql.data-source.test.js +406 -0
- package/dist/data/orm/subscribable.data-source.d.ts +25 -0
- package/dist/data/orm/subscribable.data-source.js +72 -0
- package/dist/data/orm/table-container-events.test.d.ts +1 -0
- package/dist/data/orm/table-container-events.test.js +93 -0
- package/dist/data/orm/table-container.d.ts +39 -0
- package/dist/data/orm/table-container.js +96 -0
- package/dist/data/orm/table-definitions.system.d.ts +9 -0
- package/dist/data/orm/table-definitions.system.js +29 -0
- package/dist/data/orm/table-definitions.type.d.ts +19 -0
- package/dist/data/orm/table-definitions.type.js +2 -0
- package/dist/data/orm/table-dependencies.d.ts +32 -0
- package/dist/data/orm/table-dependencies.js +2 -0
- package/dist/data/orm/table.d.ts +42 -0
- package/dist/data/orm/table.event-source.test.d.ts +1 -0
- package/dist/data/orm/table.event-source.test.js +341 -0
- package/dist/data/orm/table.js +244 -0
- package/dist/data/orm/types.d.ts +20 -0
- package/dist/data/orm/types.js +115 -0
- package/dist/data/orm/types.test.d.ts +1 -0
- package/dist/data/orm/types.test.js +71 -0
- package/dist/data/package-permissions.d.ts +7 -0
- package/dist/data/package-permissions.js +18 -0
- package/dist/data/packages.d.ts +92 -0
- package/dist/data/packages.js +90 -0
- package/dist/data/peer-events/peer-event-handlers.d.ts +21 -0
- package/dist/data/peer-events/peer-event-handlers.js +28 -0
- package/dist/data/peer-events/peer-event-types.d.ts +119 -0
- package/dist/data/peer-events/peer-event-types.js +29 -0
- package/dist/data/peer-events/peer-events.d.ts +41 -0
- package/dist/data/peer-events/peer-events.js +102 -0
- package/dist/data/persistent-vars.d.ts +87 -0
- package/dist/data/persistent-vars.js +230 -0
- package/dist/data/tool-tests.d.ts +37 -0
- package/dist/data/tool-tests.js +27 -0
- package/dist/data/tools.d.ts +358 -0
- package/dist/data/tools.js +48 -0
- package/dist/data/user-permissions.d.ts +15 -0
- package/dist/data/user-permissions.js +39 -0
- package/dist/data/user-permissions.test.d.ts +1 -0
- package/dist/data/user-permissions.test.js +252 -0
- package/dist/data/users.d.ts +38 -0
- package/dist/data/users.js +73 -0
- package/dist/data/workflow-logs.d.ts +106 -0
- package/dist/data/workflow-logs.js +67 -0
- package/dist/data/workflow-runs.d.ts +103 -0
- package/dist/data/workflow-runs.js +313 -0
- package/dist/data/workflows.d.ts +16 -0
- package/dist/data/workflows.js +21 -0
- package/dist/device/connection.d.ts +41 -0
- package/dist/device/connection.js +249 -0
- package/dist/device/connection.test.d.ts +1 -0
- package/dist/device/connection.test.js +292 -0
- package/dist/device/device-election.d.ts +36 -0
- package/dist/device/device-election.js +137 -0
- package/dist/device/device.d.ts +22 -0
- package/dist/device/device.js +110 -0
- package/dist/device/device.test.d.ts +1 -0
- package/dist/device/device.test.js +203 -0
- package/dist/device/get-trust-level.d.ts +3 -0
- package/dist/device/get-trust-level.js +87 -0
- package/dist/device/socket.type.d.ts +20 -0
- package/dist/device/socket.type.js +15 -0
- package/dist/device/streamed-socket.d.ts +27 -0
- package/dist/device/streamed-socket.js +154 -0
- package/dist/device/streamed-socket.test.d.ts +1 -0
- package/dist/device/streamed-socket.test.js +44 -0
- package/dist/events.d.ts +35 -0
- package/dist/events.js +128 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +50 -0
- package/dist/keys.d.ts +51 -0
- package/dist/keys.js +234 -0
- package/dist/keys.test.d.ts +1 -0
- package/dist/keys.test.js +215 -0
- package/dist/mentions.d.ts +9 -0
- package/dist/mentions.js +46 -0
- package/dist/observable.d.ts +19 -0
- package/dist/observable.js +112 -0
- package/dist/observable.test.d.ts +1 -0
- package/dist/observable.test.js +183 -0
- package/dist/package-loader/get-require.d.ts +10 -0
- package/dist/package-loader/get-require.js +31 -0
- package/dist/package-loader/index.d.ts +1 -0
- package/dist/package-loader/index.js +17 -0
- package/dist/package-loader/package-loader.d.ts +16 -0
- package/dist/package-loader/package-loader.js +102 -0
- package/dist/peers-ui/peers-ui.d.ts +15 -0
- package/dist/peers-ui/peers-ui.js +23 -0
- package/dist/peers-ui/peers-ui.types.d.ts +35 -0
- package/dist/peers-ui/peers-ui.types.js +3 -0
- package/dist/rpc-types.d.ts +45 -0
- package/dist/rpc-types.js +47 -0
- package/dist/serial-json.d.ts +5 -0
- package/dist/serial-json.js +186 -0
- package/dist/serial-json.test.d.ts +1 -0
- package/dist/serial-json.test.js +86 -0
- package/dist/system-ids.d.ts +6 -0
- package/dist/system-ids.js +10 -0
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +17 -0
- package/dist/tools/tools-factory.d.ts +5 -0
- package/dist/tools/tools-factory.js +34 -0
- package/dist/types/app-nav.d.ts +18 -0
- package/dist/types/app-nav.js +10 -0
- package/dist/types/assistant-runner-args.d.ts +9 -0
- package/dist/types/assistant-runner-args.js +2 -0
- package/dist/types/field-type.d.ts +37 -0
- package/dist/types/field-type.js +26 -0
- package/dist/types/peer-device.d.ts +40 -0
- package/dist/types/peer-device.js +14 -0
- package/dist/types/peers-package.d.ts +23 -0
- package/dist/types/peers-package.js +2 -0
- package/dist/types/workflow-logger.d.ts +2 -0
- package/dist/types/workflow-logger.js +2 -0
- package/dist/types/workflow-run-context.d.ts +12 -0
- package/dist/types/workflow-run-context.js +2 -0
- package/dist/types/workflow.d.ts +72 -0
- package/dist/types/workflow.js +24 -0
- package/dist/types/zod-types.d.ts +7 -0
- package/dist/types/zod-types.js +12 -0
- package/dist/users.query.d.ts +13 -0
- package/dist/users.query.js +134 -0
- package/dist/utils.d.ts +39 -0
- package/dist/utils.js +240 -0
- package/dist/utils.test.d.ts +1 -0
- package/dist/utils.test.js +140 -0
- package/package.json +50 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const keys_1 = require("../keys");
|
|
4
|
+
const user_permissions_1 = require("./user-permissions");
|
|
5
|
+
const socket_type_1 = require("../device/socket.type");
|
|
6
|
+
// Test the user permissions logic directly without ORM dependencies
|
|
7
|
+
describe('User Permissions Logic', () => {
|
|
8
|
+
let testUser;
|
|
9
|
+
let userKeys;
|
|
10
|
+
let otherUserKeys;
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
userKeys = (0, keys_1.newKeys)();
|
|
13
|
+
otherUserKeys = (0, keys_1.newKeys)();
|
|
14
|
+
testUser = {
|
|
15
|
+
userId: 'test-user-1',
|
|
16
|
+
name: 'Test User',
|
|
17
|
+
publicKey: userKeys.publicKey,
|
|
18
|
+
publicBoxKey: userKeys.publicBoxKey,
|
|
19
|
+
trustLevel: socket_type_1.TrustLevel.Trusted,
|
|
20
|
+
signature: ''
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
describe('basic signature functionality', () => {
|
|
24
|
+
it('should add signature to user object', () => {
|
|
25
|
+
const signedUser = (0, keys_1.addSignatureToObject)(testUser, userKeys.secretKey);
|
|
26
|
+
expect(signedUser.signature).toBeTruthy();
|
|
27
|
+
expect(signedUser.signature).toContain(':');
|
|
28
|
+
expect(signedUser.userId).toBe(testUser.userId);
|
|
29
|
+
expect(signedUser.name).toBe(testUser.name);
|
|
30
|
+
expect(signedUser.publicKey).toBe(testUser.publicKey);
|
|
31
|
+
});
|
|
32
|
+
it('should verify valid signature', () => {
|
|
33
|
+
const signedUser = (0, keys_1.addSignatureToObject)(testUser, userKeys.secretKey);
|
|
34
|
+
const isValid = (0, keys_1.isObjectSignatureValid)(signedUser);
|
|
35
|
+
expect(isValid).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
it('should reject invalid signature after modification', () => {
|
|
38
|
+
const signedUser = (0, keys_1.addSignatureToObject)(testUser, userKeys.secretKey);
|
|
39
|
+
// Modify the user after signing
|
|
40
|
+
signedUser.name = 'Modified Name';
|
|
41
|
+
const isValid = (0, keys_1.isObjectSignatureValid)(signedUser);
|
|
42
|
+
expect(isValid).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
it('should extract public key from signature', () => {
|
|
45
|
+
const signedUser = (0, keys_1.addSignatureToObject)(testUser, userKeys.secretKey);
|
|
46
|
+
const extractedPublicKey = (0, keys_1.getPublicKeyFromObjectSignature)(signedUser);
|
|
47
|
+
expect(extractedPublicKey).toBe(userKeys.publicKey);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe('signUserObject helper', () => {
|
|
51
|
+
it('should sign user object correctly', () => {
|
|
52
|
+
const signedUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
53
|
+
expect(signedUser.signature).toBeTruthy();
|
|
54
|
+
expect((0, keys_1.isObjectSignatureValid)(signedUser)).toBe(true);
|
|
55
|
+
expect((0, keys_1.getPublicKeyFromObjectSignature)(signedUser)).toBe(userKeys.publicKey);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe('verifyUserSignature', () => {
|
|
59
|
+
it('should verify valid signature for new user', () => {
|
|
60
|
+
const signedUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
61
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser)).not.toThrow();
|
|
62
|
+
});
|
|
63
|
+
it('should verify valid signature for existing user update', () => {
|
|
64
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
65
|
+
const updatedUser = {
|
|
66
|
+
...originalUser,
|
|
67
|
+
name: 'Updated Name',
|
|
68
|
+
signature: ''
|
|
69
|
+
};
|
|
70
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, userKeys.secretKey);
|
|
71
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).not.toThrow();
|
|
72
|
+
});
|
|
73
|
+
it('should reject signature that does not match user public key for new user', () => {
|
|
74
|
+
const signedUser = (0, user_permissions_1.signUserObject)(testUser, otherUserKeys.secretKey);
|
|
75
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser)).toThrow('User signature must be signed with their own public key');
|
|
76
|
+
});
|
|
77
|
+
it('should reject signature that does not match existing user public key', () => {
|
|
78
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
79
|
+
const updatedUser = {
|
|
80
|
+
...originalUser,
|
|
81
|
+
name: 'Updated Name',
|
|
82
|
+
signature: ''
|
|
83
|
+
};
|
|
84
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, otherUserKeys.secretKey);
|
|
85
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).toThrow('User update must be signed by the user themselves');
|
|
86
|
+
});
|
|
87
|
+
it('should reject public key changes', () => {
|
|
88
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
89
|
+
const updatedUser = {
|
|
90
|
+
...originalUser,
|
|
91
|
+
publicKey: otherUserKeys.publicKey,
|
|
92
|
+
signature: ''
|
|
93
|
+
};
|
|
94
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, userKeys.secretKey);
|
|
95
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).toThrow('Public key changes are not currently supported');
|
|
96
|
+
});
|
|
97
|
+
it('should handle missing signature gracefully', () => {
|
|
98
|
+
const userWithoutSignature = { ...testUser };
|
|
99
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(userWithoutSignature)).toThrow();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
describe('verifyUserSignature - Group Context (With Signature)', () => {
|
|
103
|
+
it('should allow valid signature for new user in group context', () => {
|
|
104
|
+
const signedUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
105
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser, undefined)).not.toThrow();
|
|
106
|
+
});
|
|
107
|
+
it('should allow valid signature for existing user update in group context', () => {
|
|
108
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
109
|
+
const updatedUser = {
|
|
110
|
+
...originalUser,
|
|
111
|
+
name: 'Updated Name',
|
|
112
|
+
signature: ''
|
|
113
|
+
};
|
|
114
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, userKeys.secretKey);
|
|
115
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).not.toThrow();
|
|
116
|
+
});
|
|
117
|
+
it('should reject invalid signature in group context', () => {
|
|
118
|
+
const signedUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
119
|
+
// Tamper with the signature
|
|
120
|
+
signedUser.name = 'Tampered Name';
|
|
121
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser, undefined)).toThrow();
|
|
122
|
+
});
|
|
123
|
+
it('should reject signature from wrong user for new user', () => {
|
|
124
|
+
const signedUser = (0, user_permissions_1.signUserObject)(testUser, otherUserKeys.secretKey);
|
|
125
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser, undefined)).toThrow();
|
|
126
|
+
});
|
|
127
|
+
it('should reject signature from wrong user for existing user update', () => {
|
|
128
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
129
|
+
const updatedUser = {
|
|
130
|
+
...originalUser,
|
|
131
|
+
name: 'Updated Name',
|
|
132
|
+
signature: ''
|
|
133
|
+
};
|
|
134
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, otherUserKeys.secretKey);
|
|
135
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).toThrow();
|
|
136
|
+
});
|
|
137
|
+
it('should reject public key changes in group context', () => {
|
|
138
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
139
|
+
const updatedUser = {
|
|
140
|
+
...originalUser,
|
|
141
|
+
publicKey: otherUserKeys.publicKey, // Change public key
|
|
142
|
+
signature: ''
|
|
143
|
+
};
|
|
144
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, userKeys.secretKey);
|
|
145
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).toThrow();
|
|
146
|
+
});
|
|
147
|
+
it('should allow trust level changes with valid signature in group context', () => {
|
|
148
|
+
const originalUser = (0, user_permissions_1.signUserObject)({ ...testUser, trustLevel: socket_type_1.TrustLevel.Unknown }, userKeys.secretKey);
|
|
149
|
+
const updatedUser = {
|
|
150
|
+
...originalUser,
|
|
151
|
+
trustLevel: socket_type_1.TrustLevel.Trusted,
|
|
152
|
+
signature: ''
|
|
153
|
+
};
|
|
154
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, userKeys.secretKey);
|
|
155
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).not.toThrow();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
describe('verifyUserSignature - Signature Requirements', () => {
|
|
159
|
+
it('should reject updates without signature when signature is required', () => {
|
|
160
|
+
const userWithoutSignature = { ...testUser };
|
|
161
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(userWithoutSignature, undefined)).toThrow();
|
|
162
|
+
});
|
|
163
|
+
it('should accept valid signature when signature is provided', () => {
|
|
164
|
+
const signedUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
165
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser, undefined)).not.toThrow();
|
|
166
|
+
});
|
|
167
|
+
it('should reject empty signature when signature is required', () => {
|
|
168
|
+
const userWithEmptySignature = { ...testUser, signature: '' };
|
|
169
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(userWithEmptySignature, undefined)).toThrow();
|
|
170
|
+
});
|
|
171
|
+
it('should reject whitespace-only signature when signature is required', () => {
|
|
172
|
+
const userWithWhitespaceSignature = { ...testUser, signature: ' ' };
|
|
173
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(userWithWhitespaceSignature, undefined)).toThrow();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
describe('edge cases and error handling', () => {
|
|
177
|
+
it('should handle undefined user gracefully', () => {
|
|
178
|
+
// @ts-ignore - Testing edge case
|
|
179
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(undefined, undefined)).toThrow();
|
|
180
|
+
});
|
|
181
|
+
it('should handle malformed signature', () => {
|
|
182
|
+
const userWithBadSignature = { ...testUser, signature: 'invalid-signature' };
|
|
183
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(userWithBadSignature, undefined)).toThrow();
|
|
184
|
+
});
|
|
185
|
+
it('should handle missing public key in user object', () => {
|
|
186
|
+
const userWithoutPublicKey = { ...testUser, publicKey: '' };
|
|
187
|
+
const signedUser = (0, user_permissions_1.signUserObject)(userWithoutPublicKey, userKeys.secretKey);
|
|
188
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser, undefined)).toThrow();
|
|
189
|
+
});
|
|
190
|
+
it('should handle existing user with missing public key', () => {
|
|
191
|
+
const originalUser = { ...testUser, publicKey: '' };
|
|
192
|
+
const updatedUser = { ...testUser, name: 'Updated' };
|
|
193
|
+
const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, userKeys.secretKey);
|
|
194
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).toThrow();
|
|
195
|
+
});
|
|
196
|
+
it('should handle complex trust level enum values', () => {
|
|
197
|
+
const userWithComplexTrust = {
|
|
198
|
+
...testUser,
|
|
199
|
+
trustLevel: socket_type_1.TrustLevel.Malicious
|
|
200
|
+
};
|
|
201
|
+
const signedUser = (0, user_permissions_1.signUserObject)(userWithComplexTrust, userKeys.secretKey);
|
|
202
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser, undefined)).not.toThrow();
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
describe('security scenarios', () => {
|
|
206
|
+
it('should prevent impersonation attacks', () => {
|
|
207
|
+
// Attacker tries to create a user record with victim's public key but signed with attacker's key
|
|
208
|
+
const victimUser = {
|
|
209
|
+
userId: 'victim-user',
|
|
210
|
+
name: 'Victim User',
|
|
211
|
+
publicKey: userKeys.publicKey, // Victim's public key
|
|
212
|
+
publicBoxKey: userKeys.publicBoxKey,
|
|
213
|
+
trustLevel: socket_type_1.TrustLevel.Trusted,
|
|
214
|
+
signature: ''
|
|
215
|
+
};
|
|
216
|
+
const maliciousSignedUser = (0, user_permissions_1.signUserObject)(victimUser, otherUserKeys.secretKey); // Signed with attacker's key
|
|
217
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(maliciousSignedUser, undefined)).toThrow();
|
|
218
|
+
});
|
|
219
|
+
it('should prevent profile takeover attacks', () => {
|
|
220
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
221
|
+
// Attacker tries to update the user's profile with their own signature
|
|
222
|
+
const maliciousUpdate = {
|
|
223
|
+
...originalUser,
|
|
224
|
+
name: 'Attacker Name',
|
|
225
|
+
signature: ''
|
|
226
|
+
};
|
|
227
|
+
const maliciousSignedUpdate = (0, user_permissions_1.signUserObject)(maliciousUpdate, otherUserKeys.secretKey);
|
|
228
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(maliciousSignedUpdate, originalUser)).toThrow();
|
|
229
|
+
});
|
|
230
|
+
it('should prevent public key rotation attacks', () => {
|
|
231
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
232
|
+
// Attacker tries to change the public key to their own
|
|
233
|
+
const keyRotationAttack = {
|
|
234
|
+
...originalUser,
|
|
235
|
+
publicKey: otherUserKeys.publicKey, // Change to attacker's key
|
|
236
|
+
signature: ''
|
|
237
|
+
};
|
|
238
|
+
const signedAttack = (0, user_permissions_1.signUserObject)(keyRotationAttack, userKeys.secretKey);
|
|
239
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedAttack, originalUser)).toThrow();
|
|
240
|
+
});
|
|
241
|
+
it('should allow legitimate name changes with proper signature', () => {
|
|
242
|
+
const originalUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
|
|
243
|
+
const legitimateUpdate = {
|
|
244
|
+
...originalUser,
|
|
245
|
+
name: 'New Legal Name',
|
|
246
|
+
signature: ''
|
|
247
|
+
};
|
|
248
|
+
const signedUpdate = (0, user_permissions_1.signUserObject)(legitimateUpdate, userKeys.secretKey);
|
|
249
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdate, originalUser)).not.toThrow();
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { DataContext } from "../context/data-context";
|
|
3
|
+
import { TrustLevel } from "../device/socket.type";
|
|
4
|
+
import { ISaveOptions, Table } from "./orm";
|
|
5
|
+
export declare const userSchema: z.ZodObject<{
|
|
6
|
+
userId: z.ZodString;
|
|
7
|
+
name: z.ZodString;
|
|
8
|
+
publicKey: z.ZodString;
|
|
9
|
+
publicBoxKey: z.ZodString;
|
|
10
|
+
trustLevel: z.ZodOptional<z.ZodNativeEnum<typeof TrustLevel>>;
|
|
11
|
+
signature: z.ZodOptional<z.ZodString>;
|
|
12
|
+
}, "strip", z.ZodTypeAny, {
|
|
13
|
+
name: string;
|
|
14
|
+
publicKey: string;
|
|
15
|
+
userId: string;
|
|
16
|
+
publicBoxKey: string;
|
|
17
|
+
signature?: string | undefined;
|
|
18
|
+
trustLevel?: TrustLevel | undefined;
|
|
19
|
+
}, {
|
|
20
|
+
name: string;
|
|
21
|
+
publicKey: string;
|
|
22
|
+
userId: string;
|
|
23
|
+
publicBoxKey: string;
|
|
24
|
+
signature?: string | undefined;
|
|
25
|
+
trustLevel?: TrustLevel | undefined;
|
|
26
|
+
}>;
|
|
27
|
+
export type IUser = z.infer<typeof userSchema>;
|
|
28
|
+
export declare class UsersTable extends Table<IUser> {
|
|
29
|
+
static isPassthrough: boolean;
|
|
30
|
+
save(user: IUser, opts?: ISaveOptions): Promise<IUser>;
|
|
31
|
+
/** @deprecated Forbidden on UsersTable; use save() */
|
|
32
|
+
insert(..._args: Parameters<Table<IUser>['insert']>): never;
|
|
33
|
+
/** @deprecated Forbidden on UsersTable; use save() */
|
|
34
|
+
update(..._args: Parameters<Table<IUser>['update']>): never;
|
|
35
|
+
}
|
|
36
|
+
export declare function Users(dataContext?: DataContext): UsersTable;
|
|
37
|
+
export declare const myUserId: import("./persistent-vars").PersistentVar<string>;
|
|
38
|
+
export declare function getMe(): Promise<IUser>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.myUserId = exports.UsersTable = exports.userSchema = void 0;
|
|
4
|
+
exports.Users = Users;
|
|
5
|
+
exports.getMe = getMe;
|
|
6
|
+
const zod_1 = require("zod");
|
|
7
|
+
const user_context_singleton_1 = require("../context/user-context-singleton");
|
|
8
|
+
const socket_type_1 = require("../device/socket.type");
|
|
9
|
+
const orm_1 = require("./orm");
|
|
10
|
+
const table_definitions_system_1 = require("./orm/table-definitions.system");
|
|
11
|
+
const types_1 = require("./orm/types");
|
|
12
|
+
const user_permissions_1 = require("./user-permissions");
|
|
13
|
+
const persistent_vars_1 = require("./persistent-vars");
|
|
14
|
+
exports.userSchema = zod_1.z.object({
|
|
15
|
+
userId: zod_1.z.string(),
|
|
16
|
+
name: zod_1.z.string(),
|
|
17
|
+
publicKey: zod_1.z.string().describe('The public key the user uses to sign messages'),
|
|
18
|
+
publicBoxKey: zod_1.z.string().describe('The public key to use to encrypt data that only this user can decrypt'),
|
|
19
|
+
trustLevel: zod_1.z.nativeEnum(socket_type_1.TrustLevel).optional().describe('The trust level of the user'),
|
|
20
|
+
signature: zod_1.z.string().optional().describe('The signed hash of this user object excluding the signature itself'),
|
|
21
|
+
});
|
|
22
|
+
const metaData = {
|
|
23
|
+
name: 'Users',
|
|
24
|
+
description: 'users',
|
|
25
|
+
primaryKeyName: 'userId',
|
|
26
|
+
fields: (0, types_1.schemaToFields)(exports.userSchema),
|
|
27
|
+
indexes: [
|
|
28
|
+
{ fields: ['name'] },
|
|
29
|
+
{ fields: ['publicKey'] },
|
|
30
|
+
{ fields: ['publicBoxKey'] },
|
|
31
|
+
]
|
|
32
|
+
};
|
|
33
|
+
class UsersTable extends orm_1.Table {
|
|
34
|
+
static isPassthrough = false;
|
|
35
|
+
async save(user, opts) {
|
|
36
|
+
if (UsersTable.isPassthrough) {
|
|
37
|
+
return super.save(user, opts);
|
|
38
|
+
}
|
|
39
|
+
const userContext = await (0, user_context_singleton_1.getUserContext)();
|
|
40
|
+
const userContextUserTable = Users(userContext.userDataContext);
|
|
41
|
+
if (this === userContextUserTable) {
|
|
42
|
+
// user is allowed to make any changes they want to their own user table
|
|
43
|
+
return super.save(user, opts);
|
|
44
|
+
}
|
|
45
|
+
// Get the old user record if it exists
|
|
46
|
+
const oldUser = await this.get(user.userId);
|
|
47
|
+
await (0, user_permissions_1.verifyUserSignature)(user, oldUser);
|
|
48
|
+
return super.save(user, opts);
|
|
49
|
+
}
|
|
50
|
+
/** @deprecated Forbidden on UsersTable; use save() */
|
|
51
|
+
insert(..._args) {
|
|
52
|
+
throw new Error('UsersTable forbids insert; use save()');
|
|
53
|
+
}
|
|
54
|
+
/** @deprecated Forbidden on UsersTable; use save() */
|
|
55
|
+
update(..._args) {
|
|
56
|
+
throw new Error('UsersTable forbids update; use save()');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.UsersTable = UsersTable;
|
|
60
|
+
(0, table_definitions_system_1.registerSystemTableDefinition)(metaData, exports.userSchema, UsersTable);
|
|
61
|
+
function Users(dataContext) {
|
|
62
|
+
return (0, user_context_singleton_1.getTableContainer)(dataContext).getTable(metaData, exports.userSchema, UsersTable);
|
|
63
|
+
}
|
|
64
|
+
exports.myUserId = (0, persistent_vars_1.deviceVar)('myUserId', { defaultValue: '' });
|
|
65
|
+
// let me: IUser | undefined = undefined;
|
|
66
|
+
async function getMe() {
|
|
67
|
+
const userContext = await (0, user_context_singleton_1.getUserContext)();
|
|
68
|
+
const me = await userContext.getMe();
|
|
69
|
+
if (!me) {
|
|
70
|
+
throw new Error('My user object was not found in the DB. Is the user setup?');
|
|
71
|
+
}
|
|
72
|
+
return me;
|
|
73
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { DataContext } from "../context/data-context";
|
|
3
|
+
export declare const workflowLogSchema: z.ZodObject<{
|
|
4
|
+
workflowLogId: z.ZodEffects<z.ZodString, string, string>;
|
|
5
|
+
workflowRunId: z.ZodEffects<z.ZodString, string, string>;
|
|
6
|
+
contextId: z.ZodEffects<z.ZodString, string, string>;
|
|
7
|
+
logDT: z.ZodDefault<z.ZodDate>;
|
|
8
|
+
logText: z.ZodString;
|
|
9
|
+
result: z.ZodOptional<z.ZodString>;
|
|
10
|
+
instruction: z.ZodOptional<z.ZodObject<{
|
|
11
|
+
markdown: z.ZodOptional<z.ZodString>;
|
|
12
|
+
onError: z.ZodOptional<z.ZodString>;
|
|
13
|
+
directCallToolId: z.ZodOptional<z.ZodString>;
|
|
14
|
+
subWorkflowId: z.ZodOptional<z.ZodString>;
|
|
15
|
+
}, "strip", z.ZodTypeAny, {
|
|
16
|
+
markdown?: string | undefined;
|
|
17
|
+
onError?: string | undefined;
|
|
18
|
+
directCallToolId?: string | undefined;
|
|
19
|
+
subWorkflowId?: string | undefined;
|
|
20
|
+
}, {
|
|
21
|
+
markdown?: string | undefined;
|
|
22
|
+
onError?: string | undefined;
|
|
23
|
+
directCallToolId?: string | undefined;
|
|
24
|
+
subWorkflowId?: string | undefined;
|
|
25
|
+
}>>;
|
|
26
|
+
toolId: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
27
|
+
toolArgs: z.ZodOptional<z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>>;
|
|
28
|
+
toolRunTimeMs: z.ZodOptional<z.ZodNumber>;
|
|
29
|
+
resultObject: z.ZodOptional<z.ZodUnion<[z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>, z.ZodArray<z.ZodAny, "many">]>>;
|
|
30
|
+
isError: z.ZodOptional<z.ZodBoolean>;
|
|
31
|
+
}, "strip", z.ZodTypeAny, {
|
|
32
|
+
workflowLogId: string;
|
|
33
|
+
workflowRunId: string;
|
|
34
|
+
contextId: string;
|
|
35
|
+
logDT: Date;
|
|
36
|
+
logText: string;
|
|
37
|
+
toolId?: string | undefined;
|
|
38
|
+
result?: string | undefined;
|
|
39
|
+
instruction?: {
|
|
40
|
+
markdown?: string | undefined;
|
|
41
|
+
onError?: string | undefined;
|
|
42
|
+
directCallToolId?: string | undefined;
|
|
43
|
+
subWorkflowId?: string | undefined;
|
|
44
|
+
} | undefined;
|
|
45
|
+
toolArgs?: z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
|
|
46
|
+
toolRunTimeMs?: number | undefined;
|
|
47
|
+
resultObject?: any[] | z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
|
|
48
|
+
isError?: boolean | undefined;
|
|
49
|
+
}, {
|
|
50
|
+
workflowLogId: string;
|
|
51
|
+
workflowRunId: string;
|
|
52
|
+
contextId: string;
|
|
53
|
+
logText: string;
|
|
54
|
+
toolId?: string | undefined;
|
|
55
|
+
logDT?: Date | undefined;
|
|
56
|
+
result?: string | undefined;
|
|
57
|
+
instruction?: {
|
|
58
|
+
markdown?: string | undefined;
|
|
59
|
+
onError?: string | undefined;
|
|
60
|
+
directCallToolId?: string | undefined;
|
|
61
|
+
subWorkflowId?: string | undefined;
|
|
62
|
+
} | undefined;
|
|
63
|
+
toolArgs?: z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
|
|
64
|
+
toolRunTimeMs?: number | undefined;
|
|
65
|
+
resultObject?: any[] | z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
|
|
66
|
+
isError?: boolean | undefined;
|
|
67
|
+
}>;
|
|
68
|
+
export type IWorkflowLog = z.infer<typeof workflowLogSchema>;
|
|
69
|
+
export declare function WorkflowLogs(dataContext?: DataContext): import("./orm").Table<{
|
|
70
|
+
workflowLogId: string;
|
|
71
|
+
workflowRunId: string;
|
|
72
|
+
contextId: string;
|
|
73
|
+
logDT: Date;
|
|
74
|
+
logText: string;
|
|
75
|
+
toolId?: string | undefined;
|
|
76
|
+
result?: string | undefined;
|
|
77
|
+
instruction?: {
|
|
78
|
+
markdown?: string | undefined;
|
|
79
|
+
onError?: string | undefined;
|
|
80
|
+
directCallToolId?: string | undefined;
|
|
81
|
+
subWorkflowId?: string | undefined;
|
|
82
|
+
} | undefined;
|
|
83
|
+
toolArgs?: z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
|
|
84
|
+
toolRunTimeMs?: number | undefined;
|
|
85
|
+
resultObject?: any[] | z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
|
|
86
|
+
isError?: boolean | undefined;
|
|
87
|
+
}>;
|
|
88
|
+
export declare const getLogger: (workflowRunId: string, contextId: string) => (logData: string | Omit<Partial<IWorkflowLog>, "workflowRunId" | "workflowLogId">) => Promise<{
|
|
89
|
+
workflowLogId: string;
|
|
90
|
+
workflowRunId: string;
|
|
91
|
+
contextId: string;
|
|
92
|
+
logDT: Date;
|
|
93
|
+
logText: string;
|
|
94
|
+
toolId?: string | undefined;
|
|
95
|
+
result?: string | undefined;
|
|
96
|
+
instruction?: {
|
|
97
|
+
markdown?: string | undefined;
|
|
98
|
+
onError?: string | undefined;
|
|
99
|
+
directCallToolId?: string | undefined;
|
|
100
|
+
subWorkflowId?: string | undefined;
|
|
101
|
+
} | undefined;
|
|
102
|
+
toolArgs?: z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
|
|
103
|
+
toolRunTimeMs?: number | undefined;
|
|
104
|
+
resultObject?: any[] | z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
|
|
105
|
+
isError?: boolean | undefined;
|
|
106
|
+
}>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLogger = exports.workflowLogSchema = void 0;
|
|
4
|
+
exports.WorkflowLogs = WorkflowLogs;
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const workflow_1 = require("../types/workflow");
|
|
7
|
+
const zod_types_1 = require("../types/zod-types");
|
|
8
|
+
const utils_1 = require("../utils");
|
|
9
|
+
const types_1 = require("./orm/types");
|
|
10
|
+
const user_context_singleton_1 = require("../context/user-context-singleton");
|
|
11
|
+
const table_definitions_system_1 = require("./orm/table-definitions.system");
|
|
12
|
+
exports.workflowLogSchema = zod_1.z.object({
|
|
13
|
+
workflowLogId: zod_types_1.zodPeerId,
|
|
14
|
+
workflowRunId: zod_types_1.zodPeerId.describe('The workflow run that this log entry is associated with'),
|
|
15
|
+
contextId: zod_types_1.zodPeerId.describe('The context that this log entry is associated with'),
|
|
16
|
+
logDT: zod_1.z.date().default(() => new Date()).describe('The date the log occurred'),
|
|
17
|
+
logText: zod_1.z.string().describe('The text of the log entry'),
|
|
18
|
+
result: zod_1.z.string().optional().describe('The result of the instructions'),
|
|
19
|
+
instruction: workflow_1.workflowInstructionSchema.optional().describe('The instruction that was run'),
|
|
20
|
+
toolId: zod_types_1.zodPeerId.optional().describe('The tool that was run'),
|
|
21
|
+
toolArgs: zod_types_1.zodAnyObject.optional().describe('The arguments that were passed to the tool'),
|
|
22
|
+
toolRunTimeMs: zod_1.z.number().optional().describe('The time it took to run the tool in milliseconds'),
|
|
23
|
+
resultObject: zod_types_1.zodAnyObjectOrArray.optional().describe('If the result is structured data it will be stored here'),
|
|
24
|
+
isError: zod_1.z.boolean().optional().describe('Whether or not the log entry is the result of an error'),
|
|
25
|
+
});
|
|
26
|
+
const metaData = {
|
|
27
|
+
name: 'WorkflowLogs',
|
|
28
|
+
description: 'The log entries for workflow runs.',
|
|
29
|
+
primaryKeyName: 'workflowLogId',
|
|
30
|
+
fields: (0, types_1.schemaToFields)(exports.workflowLogSchema),
|
|
31
|
+
};
|
|
32
|
+
(0, table_definitions_system_1.registerSystemTableDefinition)(metaData, exports.workflowLogSchema);
|
|
33
|
+
function WorkflowLogs(dataContext) {
|
|
34
|
+
return (0, user_context_singleton_1.getTableContainer)(dataContext).getTable(metaData, exports.workflowLogSchema);
|
|
35
|
+
}
|
|
36
|
+
const getLogger = (workflowRunId, contextId) => {
|
|
37
|
+
return async (logData) => {
|
|
38
|
+
try {
|
|
39
|
+
let logRecord;
|
|
40
|
+
if (typeof logData === 'string') {
|
|
41
|
+
logRecord = WorkflowLogs().initRecord({
|
|
42
|
+
workflowRunId,
|
|
43
|
+
workflowLogId: (0, utils_1.newid)(),
|
|
44
|
+
logText: logData,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
logRecord = WorkflowLogs().initRecord({
|
|
49
|
+
workflowRunId,
|
|
50
|
+
workflowLogId: (0, utils_1.newid)(),
|
|
51
|
+
...logData,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
logRecord.workflowRunId = workflowRunId;
|
|
55
|
+
logRecord.logText = logRecord.logText || '';
|
|
56
|
+
logRecord.contextId = contextId;
|
|
57
|
+
return await WorkflowLogs().insert(logRecord);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
// console.error(`Error recording log for assistant run - assistantId: ${assistantId}, messageId: ${messageId}, logData: ${logData}`, err);
|
|
61
|
+
console.error(`Error recording workflow log - logData: ${logData}`, err);
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
exports.getLogger = getLogger;
|
|
67
|
+
// export type IWorkflowLogger = ReturnType<typeof getLogger>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Table } from "./orm/table";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { IDataLock } from "./data-locks";
|
|
4
|
+
import type { DataContext } from "../context/data-context";
|
|
5
|
+
export declare const workflowRunSchema: z.ZodObject<{
|
|
6
|
+
workflowRunId: z.ZodEffects<z.ZodString, string, string>;
|
|
7
|
+
workflowId: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
8
|
+
parentMessageId: z.ZodEffects<z.ZodString, string, string>;
|
|
9
|
+
createdAt: z.ZodDefault<z.ZodDate>;
|
|
10
|
+
scheduleDT: z.ZodOptional<z.ZodDate>;
|
|
11
|
+
instructions: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
12
|
+
markdown: z.ZodOptional<z.ZodString>;
|
|
13
|
+
onError: z.ZodOptional<z.ZodString>;
|
|
14
|
+
directCallToolId: z.ZodOptional<z.ZodString>;
|
|
15
|
+
subWorkflowId: z.ZodOptional<z.ZodString>;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
markdown?: string | undefined;
|
|
18
|
+
onError?: string | undefined;
|
|
19
|
+
directCallToolId?: string | undefined;
|
|
20
|
+
subWorkflowId?: string | undefined;
|
|
21
|
+
}, {
|
|
22
|
+
markdown?: string | undefined;
|
|
23
|
+
onError?: string | undefined;
|
|
24
|
+
directCallToolId?: string | undefined;
|
|
25
|
+
subWorkflowId?: string | undefined;
|
|
26
|
+
}>, "many">>;
|
|
27
|
+
currentInstructionIndex: z.ZodDefault<z.ZodNumber>;
|
|
28
|
+
instructionResults: z.ZodDefault<z.ZodArray<z.ZodAny, "many">>;
|
|
29
|
+
inErrorState: z.ZodOptional<z.ZodBoolean>;
|
|
30
|
+
startedAt: z.ZodOptional<z.ZodDate>;
|
|
31
|
+
completedAt: z.ZodOptional<z.ZodDate>;
|
|
32
|
+
vars: z.ZodDefault<z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>>;
|
|
33
|
+
parentWorkflowRunId: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
34
|
+
defaultAssistantId: z.ZodEffects<z.ZodString, string, string>;
|
|
35
|
+
}, "strip", z.ZodTypeAny, {
|
|
36
|
+
createdAt: Date;
|
|
37
|
+
defaultAssistantId: string;
|
|
38
|
+
instructions: {
|
|
39
|
+
markdown?: string | undefined;
|
|
40
|
+
onError?: string | undefined;
|
|
41
|
+
directCallToolId?: string | undefined;
|
|
42
|
+
subWorkflowId?: string | undefined;
|
|
43
|
+
}[];
|
|
44
|
+
workflowRunId: string;
|
|
45
|
+
parentMessageId: string;
|
|
46
|
+
currentInstructionIndex: number;
|
|
47
|
+
instructionResults: any[];
|
|
48
|
+
vars: {} & {
|
|
49
|
+
[k: string]: any;
|
|
50
|
+
};
|
|
51
|
+
workflowId?: string | undefined;
|
|
52
|
+
scheduleDT?: Date | undefined;
|
|
53
|
+
inErrorState?: boolean | undefined;
|
|
54
|
+
startedAt?: Date | undefined;
|
|
55
|
+
completedAt?: Date | undefined;
|
|
56
|
+
parentWorkflowRunId?: string | undefined;
|
|
57
|
+
}, {
|
|
58
|
+
defaultAssistantId: string;
|
|
59
|
+
workflowRunId: string;
|
|
60
|
+
parentMessageId: string;
|
|
61
|
+
createdAt?: Date | undefined;
|
|
62
|
+
workflowId?: string | undefined;
|
|
63
|
+
instructions?: {
|
|
64
|
+
markdown?: string | undefined;
|
|
65
|
+
onError?: string | undefined;
|
|
66
|
+
directCallToolId?: string | undefined;
|
|
67
|
+
subWorkflowId?: string | undefined;
|
|
68
|
+
}[] | undefined;
|
|
69
|
+
scheduleDT?: Date | undefined;
|
|
70
|
+
currentInstructionIndex?: number | undefined;
|
|
71
|
+
instructionResults?: any[] | undefined;
|
|
72
|
+
inErrorState?: boolean | undefined;
|
|
73
|
+
startedAt?: Date | undefined;
|
|
74
|
+
completedAt?: Date | undefined;
|
|
75
|
+
vars?: z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
|
|
76
|
+
parentWorkflowRunId?: string | undefined;
|
|
77
|
+
}>;
|
|
78
|
+
export type IWorkflowRun = z.infer<typeof workflowRunSchema>;
|
|
79
|
+
declare class WorkflowRunTable extends Table<IWorkflowRun> {
|
|
80
|
+
/** @deprecated Direct calls to save forbidden; use insert or saveWithLock */
|
|
81
|
+
save(..._args: Parameters<Table<any>['insert']>): never;
|
|
82
|
+
/** @deprecated Direct calls to update forbidden; use saveWithLock() */
|
|
83
|
+
update(..._args: Parameters<Table<any>['update']>): never;
|
|
84
|
+
saveWithLock(workflowRun: IWorkflowRun, lock?: IDataLock): Promise<IWorkflowRun>;
|
|
85
|
+
acquireLock(workflowRunId: string, timeoutMs?: number, lockTimeMs?: number): Promise<IDataLock | undefined>;
|
|
86
|
+
saveAndRelease(workflowRun: IWorkflowRun, lock: IDataLock): Promise<void>;
|
|
87
|
+
haltRun(workflowRunId: string): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Removes the error state from a workflow run and locks it so
|
|
90
|
+
* additional editing can be done before it is re-run.
|
|
91
|
+
*/
|
|
92
|
+
clearErrorState(workflowRunId: string): Promise<IDataLock | false>;
|
|
93
|
+
}
|
|
94
|
+
export declare function WorkflowRuns(dataContext?: DataContext): WorkflowRunTable;
|
|
95
|
+
export interface IRunWorkflowOptions {
|
|
96
|
+
workflowId: string;
|
|
97
|
+
parentMessageIdOrChannelIdOrGroupId?: string;
|
|
98
|
+
parentWorkflowRunId?: string;
|
|
99
|
+
vars: Record<string, any>;
|
|
100
|
+
scheduleDT?: Date;
|
|
101
|
+
}
|
|
102
|
+
export declare function runWorkflow(args: IRunWorkflowOptions): Promise<IWorkflowRun>;
|
|
103
|
+
export {};
|