@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
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Common types, zod schemas, functions, and classes that are used internally in Peers and for building packages.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { EventRegistry, TableContainer, ITableDependencies, ITableMetaData, Table } from "../data/orm";
|
|
2
|
+
import { DataSourceFactory } from "../data/orm/table-definitions.type";
|
|
3
|
+
import { PackageLoader } from "../package-loader/package-loader";
|
|
4
|
+
import { UserContext } from "./user-context";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
export declare class DataContext {
|
|
7
|
+
readonly userContext: UserContext;
|
|
8
|
+
readonly groupId?: string | undefined;
|
|
9
|
+
readonly dataSourceFactory: DataSourceFactory;
|
|
10
|
+
readonly tableContainer: TableContainer;
|
|
11
|
+
readonly packageLoader: PackageLoader;
|
|
12
|
+
readonly eventRegistry: EventRegistry;
|
|
13
|
+
constructor(userContext: UserContext, groupId?: string | undefined);
|
|
14
|
+
get dataContextId(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Call this to indicate that this should be the current data context to be used unless otherwise specified
|
|
17
|
+
*/
|
|
18
|
+
setAsDefault(): void;
|
|
19
|
+
/**
|
|
20
|
+
* Creates table dependencies for the new DI pattern.
|
|
21
|
+
* This factory method provides all dependencies a table needs in one object.
|
|
22
|
+
*/
|
|
23
|
+
createTableDependencies(metaData: ITableMetaData, schema?: z.AnyZodObject): ITableDependencies;
|
|
24
|
+
/**
|
|
25
|
+
* Convenience method for creating tables with the new DI pattern.
|
|
26
|
+
* This is the preferred way to create custom table classes.
|
|
27
|
+
*/
|
|
28
|
+
createTable<T extends {
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
}>(metaData: ITableMetaData, schema?: z.AnyZodObject, TableClass?: new (metaData: ITableMetaData, deps: ITableDependencies) => Table<T>): Table<T>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataContext = void 0;
|
|
4
|
+
const orm_1 = require("../data/orm");
|
|
5
|
+
const package_loader_1 = require("../package-loader/package-loader");
|
|
6
|
+
class DataContext {
|
|
7
|
+
userContext;
|
|
8
|
+
groupId;
|
|
9
|
+
dataSourceFactory;
|
|
10
|
+
tableContainer;
|
|
11
|
+
packageLoader;
|
|
12
|
+
eventRegistry;
|
|
13
|
+
constructor(userContext, groupId) {
|
|
14
|
+
this.userContext = userContext;
|
|
15
|
+
this.groupId = groupId;
|
|
16
|
+
// Initialize dependencies in the correct order to avoid circular references
|
|
17
|
+
this.eventRegistry = new orm_1.EventRegistry(this.dataContextId);
|
|
18
|
+
this.packageLoader = new package_loader_1.PackageLoader(this);
|
|
19
|
+
// Set up data source factory with group wrapping
|
|
20
|
+
const baseFactory = userContext.dataSourceFactory;
|
|
21
|
+
this.dataSourceFactory = (metaData, schema) => baseFactory(metaData, schema, this.groupId);
|
|
22
|
+
// Set up table factory and table container
|
|
23
|
+
const tableFactory = (metaData, schema, TableClass) => this.createTable(metaData, schema, TableClass);
|
|
24
|
+
this.tableContainer = new orm_1.TableContainer(tableFactory, groupId);
|
|
25
|
+
}
|
|
26
|
+
get dataContextId() {
|
|
27
|
+
return this.groupId || this.userContext.userId();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Call this to indicate that this should be the current data context to be used unless otherwise specified
|
|
31
|
+
*/
|
|
32
|
+
setAsDefault() {
|
|
33
|
+
this.userContext.defaultDataContext(this);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates table dependencies for the new DI pattern.
|
|
37
|
+
* This factory method provides all dependencies a table needs in one object.
|
|
38
|
+
*/
|
|
39
|
+
createTableDependencies(metaData, schema) {
|
|
40
|
+
return {
|
|
41
|
+
dataSource: this.dataSourceFactory(metaData, schema),
|
|
42
|
+
eventRegistry: this.eventRegistry,
|
|
43
|
+
schema,
|
|
44
|
+
groupId: this.groupId
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Convenience method for creating tables with the new DI pattern.
|
|
49
|
+
* This is the preferred way to create custom table classes.
|
|
50
|
+
*/
|
|
51
|
+
createTable(metaData, schema, TableClass = orm_1.Table) {
|
|
52
|
+
const deps = this.createTableDependencies(metaData, schema);
|
|
53
|
+
return new TableClass(metaData, deps);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.DataContext = DataContext;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./user-context-singleton"), exports);
|
|
18
|
+
__exportStar(require("./data-context"), exports);
|
|
19
|
+
__exportStar(require("./user-context"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Table } from "../data/orm";
|
|
2
|
+
import type { DataContext } from "./data-context";
|
|
3
|
+
import { UserContext } from "./user-context";
|
|
4
|
+
export declare function getUserContext(): Promise<UserContext>;
|
|
5
|
+
export declare function setUserContext(userContext: UserContext): void;
|
|
6
|
+
export declare function getDefaultDataContext(): Promise<DataContext>;
|
|
7
|
+
export declare function getTableContainer(dataContext?: DataContext | undefined): import("../data/orm").TableContainer;
|
|
8
|
+
export declare function setDefaultClientUserContext(): void;
|
|
9
|
+
export declare function getAllTables(): {
|
|
10
|
+
[tableName: string]: Table<any>;
|
|
11
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getUserContext = getUserContext;
|
|
4
|
+
exports.setUserContext = setUserContext;
|
|
5
|
+
exports.getDefaultDataContext = getDefaultDataContext;
|
|
6
|
+
exports.getTableContainer = getTableContainer;
|
|
7
|
+
exports.setDefaultClientUserContext = setDefaultClientUserContext;
|
|
8
|
+
exports.getAllTables = getAllTables;
|
|
9
|
+
const client_proxy_data_source_1 = require("../data/orm/client-proxy.data-source");
|
|
10
|
+
const rpc_types_1 = require("../rpc-types");
|
|
11
|
+
const user_context_1 = require("./user-context");
|
|
12
|
+
const utils_1 = require("../utils");
|
|
13
|
+
// This should be the only singleton pattern in the application
|
|
14
|
+
// everything should be driven off the UserContext instance
|
|
15
|
+
var userContextInstance = undefined;
|
|
16
|
+
var userContextResolved;
|
|
17
|
+
var userContextPromise;
|
|
18
|
+
async function getUserContext() {
|
|
19
|
+
if (userContextInstance) {
|
|
20
|
+
return userContextInstance.loadingPromise;
|
|
21
|
+
}
|
|
22
|
+
if (!userContextPromise) {
|
|
23
|
+
userContextPromise = new Promise(resolve => {
|
|
24
|
+
userContextResolved = resolve;
|
|
25
|
+
if (userContextInstance) {
|
|
26
|
+
resolve(userContextInstance.loadingPromise);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return userContextPromise;
|
|
31
|
+
}
|
|
32
|
+
function setUserContext(userContext) {
|
|
33
|
+
if (userContextInstance) {
|
|
34
|
+
throw new Error(`Cannot set user context again after it has already been set`);
|
|
35
|
+
}
|
|
36
|
+
userContextInstance = userContext;
|
|
37
|
+
if (userContextResolved) {
|
|
38
|
+
userContextResolved(userContext.loadingPromise);
|
|
39
|
+
}
|
|
40
|
+
if (typeof window !== 'undefined') {
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
window.userContext = userContext;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async function getDefaultDataContext() {
|
|
46
|
+
const userContext = await getUserContext();
|
|
47
|
+
return userContext.defaultDataContext();
|
|
48
|
+
}
|
|
49
|
+
function getTableContainer(dataContext) {
|
|
50
|
+
dataContext ??= userContextInstance?.defaultDataContext();
|
|
51
|
+
if (!dataContext) {
|
|
52
|
+
throw new Error('dataContext not provided and user context singleton not yet set');
|
|
53
|
+
}
|
|
54
|
+
return dataContext.tableContainer;
|
|
55
|
+
}
|
|
56
|
+
function setDefaultClientUserContext() {
|
|
57
|
+
const clientProxyDataSourceFactory = (metaData, schema, groupId) => {
|
|
58
|
+
return new client_proxy_data_source_1.ClientProxyDataSource(metaData, schema, groupId ?? '');
|
|
59
|
+
};
|
|
60
|
+
const userContext = new user_context_1.UserContext(clientProxyDataSourceFactory);
|
|
61
|
+
(async () => {
|
|
62
|
+
const { GroupsTable } = await Promise.resolve().then(() => require("../data/groups"));
|
|
63
|
+
const { GroupMembersTable } = await Promise.resolve().then(() => require("../data/group-members"));
|
|
64
|
+
const { UsersTable } = await Promise.resolve().then(() => require("../data/users"));
|
|
65
|
+
GroupsTable.isPassthrough = true;
|
|
66
|
+
GroupMembersTable.isPassthrough = true;
|
|
67
|
+
UsersTable.isPassthrough = true;
|
|
68
|
+
})();
|
|
69
|
+
setUserContext(userContext);
|
|
70
|
+
}
|
|
71
|
+
function getAllTables() {
|
|
72
|
+
const tables = getTableContainer().getAllTables();
|
|
73
|
+
const tableMap = {};
|
|
74
|
+
for (const table of tables) {
|
|
75
|
+
const tableName = (0, utils_1.getFullTableName)(table.metaData);
|
|
76
|
+
tableMap[tableName] = table;
|
|
77
|
+
}
|
|
78
|
+
return tableMap;
|
|
79
|
+
}
|
|
80
|
+
async function getTable(dataContextId, tableName) {
|
|
81
|
+
const userContext = await getUserContext();
|
|
82
|
+
const table = userContext
|
|
83
|
+
.getDataContext(dataContextId || userContext.userId())
|
|
84
|
+
.tableContainer
|
|
85
|
+
.getTable(tableName);
|
|
86
|
+
return table;
|
|
87
|
+
}
|
|
88
|
+
// TODO: add check that client has permission to do the operation to the table
|
|
89
|
+
rpc_types_1.rpcServerCalls.tableGet = async (dataContextId, tableName, id) => {
|
|
90
|
+
const table = await getTable(dataContextId, tableName);
|
|
91
|
+
return table.get(id);
|
|
92
|
+
};
|
|
93
|
+
rpc_types_1.rpcServerCalls.tableList = async (dataContextId, tableName, filter, opts) => {
|
|
94
|
+
const table = await getTable(dataContextId, tableName);
|
|
95
|
+
return table.list(filter, opts);
|
|
96
|
+
};
|
|
97
|
+
rpc_types_1.rpcServerCalls.tableCount = async (dataContextId, tableName, filter) => {
|
|
98
|
+
const table = await getTable(dataContextId, tableName);
|
|
99
|
+
return table.count(filter);
|
|
100
|
+
};
|
|
101
|
+
rpc_types_1.rpcServerCalls.tableSave = async (dataContextId, tableName, data, opts) => {
|
|
102
|
+
const table = await getTable(dataContextId, tableName);
|
|
103
|
+
return table.save(data, opts);
|
|
104
|
+
};
|
|
105
|
+
rpc_types_1.rpcServerCalls.tableInsert = async (dataContextId, tableName, data) => {
|
|
106
|
+
const table = await getTable(dataContextId, tableName);
|
|
107
|
+
return table.insert(data);
|
|
108
|
+
};
|
|
109
|
+
rpc_types_1.rpcServerCalls.tableUpdate = async (dataContextId, tableName, data) => {
|
|
110
|
+
const table = await getTable(dataContextId, tableName);
|
|
111
|
+
return table.update(data);
|
|
112
|
+
};
|
|
113
|
+
rpc_types_1.rpcServerCalls.tableDelete = async (dataContextId, tableName, idOrRecord) => {
|
|
114
|
+
const table = await getTable(dataContextId, tableName);
|
|
115
|
+
return table.delete(idOrRecord);
|
|
116
|
+
};
|
|
117
|
+
rpc_types_1.rpcServerCalls.tableMethodCall = async (dataContextId, tableName, methodName, ...args) => {
|
|
118
|
+
const table = await getTable(dataContextId, tableName);
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
return table[methodName](...args);
|
|
121
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DataContext } from "../context/data-context";
|
|
2
|
+
import { IUser } from "../data";
|
|
3
|
+
import type { DataSourceFactory, IDataChangedEvent, Table } from "../data/orm";
|
|
4
|
+
import { TrustLevel } from "../device/socket.type";
|
|
5
|
+
import { IPublicPrivateKeys } from "../keys";
|
|
6
|
+
import { Observable } from "../observable";
|
|
7
|
+
export declare class UserContext {
|
|
8
|
+
readonly dataSourceFactory: DataSourceFactory;
|
|
9
|
+
readonly userId: Observable<string>;
|
|
10
|
+
readonly deviceId: Observable<string>;
|
|
11
|
+
readonly currentlyActiveGroupId: Observable<string | undefined>;
|
|
12
|
+
readonly packagesRootDir: Observable<string>;
|
|
13
|
+
readonly reloadPackagesOnPageRefresh: Observable<boolean>;
|
|
14
|
+
readonly packageLocalPathsResolved: Observable<{
|
|
15
|
+
[packageId: string]: string;
|
|
16
|
+
}>;
|
|
17
|
+
readonly groupIds: Observable<string[]>;
|
|
18
|
+
readonly userDataContext: DataContext;
|
|
19
|
+
readonly groupDataContexts: Map<string, DataContext>;
|
|
20
|
+
readonly defaultDataContext: Observable<DataContext>;
|
|
21
|
+
readonly loadingPromise: Promise<UserContext>;
|
|
22
|
+
constructor(dataSourceFactory: DataSourceFactory);
|
|
23
|
+
private init;
|
|
24
|
+
/**
|
|
25
|
+
* We want to load all packages as part of user context initialization
|
|
26
|
+
* so any tables in packages have been registered are available at sync time
|
|
27
|
+
*/
|
|
28
|
+
private loadAllPackages;
|
|
29
|
+
private loadGroupContexts;
|
|
30
|
+
getDataContext(groupId: string): DataContext;
|
|
31
|
+
/**
|
|
32
|
+
* Load observables from the database
|
|
33
|
+
* This is pretty ugly but get's around guards on the default logic which is waiting
|
|
34
|
+
* for the user context to be fully loaded before proceeding with standard data access
|
|
35
|
+
*/
|
|
36
|
+
private loadUserContextObservablesFromDB;
|
|
37
|
+
getMe(): Promise<{
|
|
38
|
+
name: string;
|
|
39
|
+
publicKey: string;
|
|
40
|
+
userId: string;
|
|
41
|
+
publicBoxKey: string;
|
|
42
|
+
signature?: string | undefined;
|
|
43
|
+
trustLevel?: TrustLevel | undefined;
|
|
44
|
+
} | undefined>;
|
|
45
|
+
syncUserAndGroupObjects(keys: IPublicPrivateKeys, me?: IUser): Promise<void>;
|
|
46
|
+
subscribeToDataChangedAcrossAllGroups<T extends {
|
|
47
|
+
[key: string]: any;
|
|
48
|
+
}>(table: Table<T>, handler: (evt: ICrossGroupSubscriptionHandlerArgs<T>) => any): import("../events").ISubscriptionResult;
|
|
49
|
+
}
|
|
50
|
+
export interface ICrossGroupSubscriptionHandlerArgs<T> {
|
|
51
|
+
name: string;
|
|
52
|
+
userContext: UserContext;
|
|
53
|
+
dataContext: DataContext;
|
|
54
|
+
data: IDataChangedEvent<T>;
|
|
55
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UserContext = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const data_context_1 = require("../context/data-context");
|
|
6
|
+
const data_1 = require("../data");
|
|
7
|
+
const socket_type_1 = require("../device/socket.type");
|
|
8
|
+
const events_1 = require("../events");
|
|
9
|
+
const keys_1 = require("../keys");
|
|
10
|
+
const observable_1 = require("../observable");
|
|
11
|
+
const utils_1 = require("../utils");
|
|
12
|
+
class UserContext {
|
|
13
|
+
dataSourceFactory;
|
|
14
|
+
userId = (0, observable_1.observable)('');
|
|
15
|
+
deviceId = (0, observable_1.observable)('');
|
|
16
|
+
currentlyActiveGroupId = (0, observable_1.observable)();
|
|
17
|
+
packagesRootDir = (0, observable_1.observable)('');
|
|
18
|
+
reloadPackagesOnPageRefresh = (0, observable_1.observable)(false);
|
|
19
|
+
packageLocalPathsResolved = (0, observable_1.observable)({});
|
|
20
|
+
groupIds = (0, observable_1.observable)([]);
|
|
21
|
+
userDataContext;
|
|
22
|
+
groupDataContexts = new Map();
|
|
23
|
+
defaultDataContext = (0, observable_1.observable)();
|
|
24
|
+
loadingPromise;
|
|
25
|
+
constructor(dataSourceFactory) {
|
|
26
|
+
this.dataSourceFactory = dataSourceFactory;
|
|
27
|
+
this.userDataContext = new data_context_1.DataContext(this);
|
|
28
|
+
this.defaultDataContext(this.userDataContext);
|
|
29
|
+
this.loadingPromise = this.init();
|
|
30
|
+
}
|
|
31
|
+
async init() {
|
|
32
|
+
await this.loadUserContextObservablesFromDB();
|
|
33
|
+
await this.loadGroupContexts();
|
|
34
|
+
await this.loadAllPackages();
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* We want to load all packages as part of user context initialization
|
|
39
|
+
* so any tables in packages have been registered are available at sync time
|
|
40
|
+
*/
|
|
41
|
+
loadAllPackages() {
|
|
42
|
+
this.userDataContext.packageLoader.loadAllPackages();
|
|
43
|
+
for (const groupContext of this.groupDataContexts.values()) {
|
|
44
|
+
groupContext.packageLoader.loadAllPackages();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async loadGroupContexts() {
|
|
48
|
+
const groupsTable = (0, data_1.Groups)(this.userDataContext);
|
|
49
|
+
const groups = await groupsTable.list({ disabled: { $ne: true } });
|
|
50
|
+
this.groupIds(groups.map(g => g.groupId));
|
|
51
|
+
groupsTable.dataChanged.subscribe(evt => {
|
|
52
|
+
const groupId = evt.dataObject.groupId;
|
|
53
|
+
if (evt.op === 'delete' || evt.dataObject.disabled) {
|
|
54
|
+
this.groupIds(this.groupIds().filter(id => id !== groupId));
|
|
55
|
+
this.groupDataContexts.delete(groupId);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
this.getDataContext(groupId);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
// load up group data contexts and set currently active group
|
|
62
|
+
const activeGroupId = this.currentlyActiveGroupId();
|
|
63
|
+
for (const groupId of this.groupIds()) {
|
|
64
|
+
const groupContext = this.getDataContext(groupId);
|
|
65
|
+
if (groupId === activeGroupId) {
|
|
66
|
+
this.defaultDataContext(groupContext);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// if no active group, set to user data context
|
|
70
|
+
if (!this.defaultDataContext()) {
|
|
71
|
+
this.defaultDataContext(this.userDataContext);
|
|
72
|
+
}
|
|
73
|
+
// ensure when currentlyActiveGroupId changes, currentlyActiveGroup is updated and vice versa
|
|
74
|
+
this.currentlyActiveGroupId.subscribe(async (groupId) => {
|
|
75
|
+
const dataContext = this.getDataContext(groupId || this.userId());
|
|
76
|
+
this.defaultDataContext(dataContext);
|
|
77
|
+
});
|
|
78
|
+
this.defaultDataContext.subscribe((group) => {
|
|
79
|
+
if (!group) {
|
|
80
|
+
this.defaultDataContext(this.userDataContext);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
this.currentlyActiveGroupId(group.dataContextId);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
getDataContext(groupId) {
|
|
88
|
+
if (groupId === this.userId()) {
|
|
89
|
+
return this.userDataContext;
|
|
90
|
+
}
|
|
91
|
+
if (!(0, utils_1.isid)(groupId)) {
|
|
92
|
+
throw new Error(`dataContextId is not a valid id: ${groupId}`);
|
|
93
|
+
}
|
|
94
|
+
if (!this.groupDataContexts.has(groupId)) {
|
|
95
|
+
if (!this.groupIds().includes(groupId)) {
|
|
96
|
+
this.groupIds([...this.groupIds(), groupId]);
|
|
97
|
+
}
|
|
98
|
+
const groupContext = new data_context_1.DataContext(this, groupId);
|
|
99
|
+
this.groupDataContexts.set(groupId, groupContext);
|
|
100
|
+
}
|
|
101
|
+
return this.groupDataContexts.get(groupId);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Load observables from the database
|
|
105
|
+
* This is pretty ugly but get's around guards on the default logic which is waiting
|
|
106
|
+
* for the user context to be fully loaded before proceeding with standard data access
|
|
107
|
+
*/
|
|
108
|
+
async loadUserContextObservablesFromDB() {
|
|
109
|
+
const persistentVars = await this.userDataContext.tableContainer.getTableByName(data_1.persistentVarsMetaData.name);
|
|
110
|
+
const vars = await persistentVars.list({ name: { $in: ['myUserId', 'thisDeviceId', 'currentlyActiveGroupId', 'packagesRootDir', 'reloadPackagesOnPageRefresh', 'packageLocalPathsResolved'] } });
|
|
111
|
+
const varDbValues = vars.reduce((acc, curr) => {
|
|
112
|
+
acc[curr.name] = curr.value?.value;
|
|
113
|
+
return acc;
|
|
114
|
+
}, {});
|
|
115
|
+
const userIdPVar = (0, data_1.deviceVar)('myUserId', { dbValue: varDbValues['myUserId'], userContext: this });
|
|
116
|
+
this.userId(userIdPVar());
|
|
117
|
+
(0, observable_1.linkObservables)(userIdPVar, this.userId);
|
|
118
|
+
const deviceIdPVar = (0, data_1.deviceVar)('thisDeviceId', { dbValue: varDbValues['thisDeviceId'], userContext: this });
|
|
119
|
+
this.deviceId(deviceIdPVar());
|
|
120
|
+
(0, observable_1.linkObservables)(deviceIdPVar, this.deviceId);
|
|
121
|
+
const currentlyActiveGroupIdPVar = (0, data_1.deviceVar)('currentlyActiveGroupId', { dbValue: varDbValues['currentlyActiveGroupId'], userContext: this });
|
|
122
|
+
this.currentlyActiveGroupId(currentlyActiveGroupIdPVar());
|
|
123
|
+
(0, observable_1.linkObservables)(currentlyActiveGroupIdPVar, this.currentlyActiveGroupId);
|
|
124
|
+
const packagesRootDirPVar = (0, data_1.deviceVar)('packagesRootDir', { defaultValue: '~/peers-packages', dbValue: varDbValues['packagesRootDir'], userContext: this });
|
|
125
|
+
this.packagesRootDir(packagesRootDirPVar());
|
|
126
|
+
(0, observable_1.linkObservables)(packagesRootDirPVar, this.packagesRootDir);
|
|
127
|
+
const reloadPackagesOnPageRefreshPVar = (0, data_1.deviceVar)('reloadPackagesOnPageRefresh', { defaultValue: false, dbValue: varDbValues['reloadPackagesOnPageRefresh'], userContext: this });
|
|
128
|
+
this.reloadPackagesOnPageRefresh(reloadPackagesOnPageRefreshPVar());
|
|
129
|
+
(0, observable_1.linkObservables)(reloadPackagesOnPageRefreshPVar, this.reloadPackagesOnPageRefresh);
|
|
130
|
+
const packageLocalPathsResolvedPVar = (0, data_1.deviceVar)('packageLocalPathsResolved', { defaultValue: {}, dbValue: varDbValues['packageLocalPathsResolved'], userContext: this });
|
|
131
|
+
this.packageLocalPathsResolved(packageLocalPathsResolvedPVar());
|
|
132
|
+
(0, observable_1.linkObservables)(packageLocalPathsResolvedPVar, this.packageLocalPathsResolved);
|
|
133
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
134
|
+
if (process.env.USER_ID) {
|
|
135
|
+
this.userId(process.env.USER_ID);
|
|
136
|
+
}
|
|
137
|
+
if (process.env.DEVICE_ID) {
|
|
138
|
+
this.deviceId(process.env.DEVICE_ID);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
await Promise.all([
|
|
142
|
+
userIdPVar.loadingPromise,
|
|
143
|
+
deviceIdPVar.loadingPromise,
|
|
144
|
+
packagesRootDirPVar.loadingPromise,
|
|
145
|
+
reloadPackagesOnPageRefreshPVar.loadingPromise,
|
|
146
|
+
packageLocalPathsResolvedPVar.loadingPromise,
|
|
147
|
+
]);
|
|
148
|
+
}
|
|
149
|
+
async getMe() {
|
|
150
|
+
let dbMe = await (0, data_1.Users)(this.userDataContext).get(this.userId(), { useCache: true });
|
|
151
|
+
return dbMe;
|
|
152
|
+
}
|
|
153
|
+
async syncUserAndGroupObjects(keys, me) {
|
|
154
|
+
// update my user object for all contexts
|
|
155
|
+
const userContext = this;
|
|
156
|
+
me ??= await this.getMe();
|
|
157
|
+
if (!me) {
|
|
158
|
+
throw new Error('My user object was not passed in and was not found in my personal db');
|
|
159
|
+
}
|
|
160
|
+
me.publicKey = keys.publicKey;
|
|
161
|
+
me.publicBoxKey = keys.publicBoxKey;
|
|
162
|
+
const meSigned = (0, keys_1.addSignatureToObject)(me, keys.secretKey);
|
|
163
|
+
// sync my user to personal db
|
|
164
|
+
if (!(0, lodash_1.isEqual)(me, meSigned)) {
|
|
165
|
+
(0, keys_1.verifyObjectSignature)(meSigned);
|
|
166
|
+
await (0, data_1.Users)(userContext.userDataContext).save(meSigned);
|
|
167
|
+
}
|
|
168
|
+
// sync my user to all all my groups
|
|
169
|
+
for (const [, dataContext] of userContext.groupDataContexts) {
|
|
170
|
+
let groupMe = await (0, data_1.Users)(dataContext).get(me.userId);
|
|
171
|
+
if (!groupMe || groupMe.name !== me.name || groupMe.publicKey !== me.publicKey) {
|
|
172
|
+
groupMe = (0, keys_1.addSignatureToObject)({ ...me, trustLevel: groupMe?.trustLevel || socket_type_1.TrustLevel.NewUser }, keys.secretKey);
|
|
173
|
+
await (0, data_1.Users)(dataContext).save(groupMe);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// sync group objects to my personal db
|
|
177
|
+
for (const [, dataContext] of userContext.groupDataContexts) {
|
|
178
|
+
if (!dataContext.groupId) {
|
|
179
|
+
console.error('Group data context is missing group id. Something went wrong');
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const groupObject = await (0, data_1.Groups)(dataContext).get(dataContext.groupId);
|
|
183
|
+
const myGroupObject = await (0, data_1.Groups)(this.userDataContext).get(dataContext.groupId);
|
|
184
|
+
if (groupObject && !(0, lodash_1.isEqual)(groupObject, myGroupObject)) {
|
|
185
|
+
(0, keys_1.verifyObjectSignature)(groupObject);
|
|
186
|
+
await (0, data_1.Groups)(this.userDataContext).save(groupObject);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
subscribeToDataChangedAcrossAllGroups(table, handler) {
|
|
191
|
+
const tableEventPrefix = `${table.tableName}_DataChanged_`;
|
|
192
|
+
const subscription = (0, events_1.subscribe)(evt => evt.name.startsWith(tableEventPrefix), async (evt) => {
|
|
193
|
+
const dataContextId = evt.name.endsWith('_') ? this.userId() : evt.name.split('_').pop();
|
|
194
|
+
const dataContext = this.getDataContext(dataContextId);
|
|
195
|
+
handler({
|
|
196
|
+
name: evt.name,
|
|
197
|
+
userContext: this,
|
|
198
|
+
dataContext,
|
|
199
|
+
data: evt.data,
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
return subscription;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
exports.UserContext = UserContext;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { DataContext } from "../context/data-context";
|
|
3
|
+
export declare enum ToolInclusionStrategy {
|
|
4
|
+
Fixed = "Fixed",
|
|
5
|
+
Linked = "Linked",
|
|
6
|
+
Relevant = "Relevant"
|
|
7
|
+
}
|
|
8
|
+
export declare const assistantSchema: z.ZodObject<{
|
|
9
|
+
assistantId: z.ZodEffects<z.ZodString, string, string>;
|
|
10
|
+
name: z.ZodString;
|
|
11
|
+
createdAt: z.ZodDefault<z.ZodDate>;
|
|
12
|
+
updatedAt: z.ZodOptional<z.ZodDate>;
|
|
13
|
+
description: z.ZodOptional<z.ZodString>;
|
|
14
|
+
assistantRunnerToolId: z.ZodDefault<z.ZodString>;
|
|
15
|
+
assistantRunnerConfig: z.ZodDefault<z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>>;
|
|
16
|
+
toolsToInclude: z.ZodDefault<z.ZodString>;
|
|
17
|
+
toolInclusionStrategy: z.ZodDefault<z.ZodNativeEnum<typeof ToolInclusionStrategy>>;
|
|
18
|
+
}, "strip", z.ZodTypeAny, {
|
|
19
|
+
name: string;
|
|
20
|
+
createdAt: Date;
|
|
21
|
+
assistantId: string;
|
|
22
|
+
assistantRunnerToolId: string;
|
|
23
|
+
assistantRunnerConfig: {} & {
|
|
24
|
+
[k: string]: any;
|
|
25
|
+
};
|
|
26
|
+
toolsToInclude: string;
|
|
27
|
+
toolInclusionStrategy: ToolInclusionStrategy;
|
|
28
|
+
description?: string | undefined;
|
|
29
|
+
updatedAt?: Date | undefined;
|
|
30
|
+
}, {
|
|
31
|
+
name: string;
|
|
32
|
+
assistantId: string;
|
|
33
|
+
description?: string | undefined;
|
|
34
|
+
createdAt?: Date | undefined;
|
|
35
|
+
updatedAt?: Date | undefined;
|
|
36
|
+
assistantRunnerToolId?: string | undefined;
|
|
37
|
+
assistantRunnerConfig?: z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
|
|
38
|
+
toolsToInclude?: string | undefined;
|
|
39
|
+
toolInclusionStrategy?: ToolInclusionStrategy | undefined;
|
|
40
|
+
}>;
|
|
41
|
+
export type IAssistant = z.infer<typeof assistantSchema>;
|
|
42
|
+
export declare function Assistants(dataContext?: DataContext): import("./orm").Table<{
|
|
43
|
+
name: string;
|
|
44
|
+
createdAt: Date;
|
|
45
|
+
assistantId: string;
|
|
46
|
+
assistantRunnerToolId: string;
|
|
47
|
+
assistantRunnerConfig: {} & {
|
|
48
|
+
[k: string]: any;
|
|
49
|
+
};
|
|
50
|
+
toolsToInclude: string;
|
|
51
|
+
toolInclusionStrategy: ToolInclusionStrategy;
|
|
52
|
+
description?: string | undefined;
|
|
53
|
+
updatedAt?: Date | undefined;
|
|
54
|
+
}>;
|
|
55
|
+
export declare const getPrimaryAssistant: (dataContext?: DataContext) => Promise<{
|
|
56
|
+
name: string;
|
|
57
|
+
createdAt: Date;
|
|
58
|
+
assistantId: string;
|
|
59
|
+
assistantRunnerToolId: string;
|
|
60
|
+
assistantRunnerConfig: {} & {
|
|
61
|
+
[k: string]: any;
|
|
62
|
+
};
|
|
63
|
+
toolsToInclude: string;
|
|
64
|
+
toolInclusionStrategy: ToolInclusionStrategy;
|
|
65
|
+
description?: string | undefined;
|
|
66
|
+
updatedAt?: Date | undefined;
|
|
67
|
+
}>;
|
|
68
|
+
export declare function getAllAssistantIdsMentioned(message: string): Promise<string[]>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPrimaryAssistant = exports.assistantSchema = exports.ToolInclusionStrategy = void 0;
|
|
4
|
+
exports.Assistants = Assistants;
|
|
5
|
+
exports.getAllAssistantIdsMentioned = getAllAssistantIdsMentioned;
|
|
6
|
+
const mentions_1 = require("../mentions");
|
|
7
|
+
const types_1 = require("./orm/types");
|
|
8
|
+
const user_context_singleton_1 = require("../context/user-context-singleton");
|
|
9
|
+
const table_definitions_system_1 = require("./orm/table-definitions.system");
|
|
10
|
+
const zod_1 = require("zod");
|
|
11
|
+
const system_ids_1 = require("../system-ids");
|
|
12
|
+
const zod_types_1 = require("../types/zod-types");
|
|
13
|
+
const persistent_vars_1 = require("./persistent-vars");
|
|
14
|
+
var ToolInclusionStrategy;
|
|
15
|
+
(function (ToolInclusionStrategy) {
|
|
16
|
+
ToolInclusionStrategy["Fixed"] = "Fixed";
|
|
17
|
+
ToolInclusionStrategy["Linked"] = "Linked";
|
|
18
|
+
ToolInclusionStrategy["Relevant"] = "Relevant";
|
|
19
|
+
})(ToolInclusionStrategy || (exports.ToolInclusionStrategy = ToolInclusionStrategy = {}));
|
|
20
|
+
exports.assistantSchema = zod_1.z.object({
|
|
21
|
+
assistantId: zod_types_1.zodPeerId,
|
|
22
|
+
name: zod_1.z.string(),
|
|
23
|
+
createdAt: zod_1.z.date().default(() => new Date()),
|
|
24
|
+
updatedAt: zod_1.z.date().optional(),
|
|
25
|
+
description: zod_1.z.string().optional().describe(`Information about the assistant that won't be used in the interaction but can still be used to differentiate between assistants`),
|
|
26
|
+
assistantRunnerToolId: zod_1.z.string().default(system_ids_1.defaultAssistantRunnerToolId).describe('The tool id of the model to use to run the assistant'),
|
|
27
|
+
assistantRunnerConfig: zod_types_1.zodAnyObject.default({}).describe('The configuration to use when running the assistant'),
|
|
28
|
+
toolsToInclude: zod_1.z.string().default('').describe('Link the tools that should always be available to this assistant. Only linked tools will be included. All other content will be ignored.\n\nex: `$hello-world-tool`'),
|
|
29
|
+
// toolsToExclude: z.string().optional().describe('Describe or link the tools that should never be available to this assistant'),
|
|
30
|
+
toolInclusionStrategy: zod_1.z.nativeEnum(ToolInclusionStrategy).default(ToolInclusionStrategy.Linked).describe([
|
|
31
|
+
'Describe if and how additional tools should be included on each run of the assistant.',
|
|
32
|
+
'- **Fixed** - Only include the tools specified in `toolsToInclude`.',
|
|
33
|
+
"- **Linked** - `Fixed` plus tools linked in the message or chat history of the assistant's current conversation.",
|
|
34
|
+
'- **Relevant** - `Linked` plus tools matching the context of the conversation determined via vector search (embeddings) of the tools descriptions.',
|
|
35
|
+
].join('\n')),
|
|
36
|
+
});
|
|
37
|
+
const metaData = {
|
|
38
|
+
name: 'Assistants',
|
|
39
|
+
description: 'The assistants that are available to help users',
|
|
40
|
+
primaryKeyName: 'assistantId',
|
|
41
|
+
fields: (0, types_1.schemaToFields)(exports.assistantSchema),
|
|
42
|
+
iconClassName: 'bi bi-person-fill-gear',
|
|
43
|
+
indexes: [
|
|
44
|
+
{ fields: ['name'], },
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
(0, table_definitions_system_1.registerSystemTableDefinition)(metaData, exports.assistantSchema);
|
|
48
|
+
function Assistants(dataContext) {
|
|
49
|
+
return (0, user_context_singleton_1.getTableContainer)(dataContext).getTable(metaData, exports.assistantSchema);
|
|
50
|
+
}
|
|
51
|
+
const getPrimaryAssistant = async (dataContext) => {
|
|
52
|
+
let myPrimaryAssistant = undefined;
|
|
53
|
+
const assistantId = (await (0, persistent_vars_1.getPersistentVar)("primaryAssistantId", dataContext))?.value;
|
|
54
|
+
myPrimaryAssistant = await Assistants(dataContext).get(assistantId || system_ids_1.defaultAssistantId);
|
|
55
|
+
if (!myPrimaryAssistant) {
|
|
56
|
+
throw new Error('Primary assistant not found');
|
|
57
|
+
}
|
|
58
|
+
return myPrimaryAssistant;
|
|
59
|
+
};
|
|
60
|
+
exports.getPrimaryAssistant = getPrimaryAssistant;
|
|
61
|
+
async function getAllAssistantIdsMentioned(message) {
|
|
62
|
+
const mentions = (0, mentions_1.getAllMentions)(message);
|
|
63
|
+
return mentions.filter(m => m.kind === 'assistant').map(m => m.id);
|
|
64
|
+
}
|