@harperfast/harper 5.2.0-beta.4 → 5.2.1
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/bin/backup.ts +169 -0
- package/bin/cliOperations.ts +92 -73
- package/bin/copyDb.ts +21 -4
- package/bin/harper.ts +43 -55
- package/bin/help.ts +216 -0
- package/components/Application.ts +236 -46
- package/components/ApplicationScope.ts +26 -0
- package/components/EntryHandler.ts +410 -105
- package/components/RuntimeModuleTracker.ts +189 -0
- package/components/Scope.ts +120 -30
- package/components/componentLoader.ts +177 -25
- package/components/deployLifecycle.ts +119 -33
- package/components/mcp/toolRegistry.ts +10 -0
- package/components/mcp/tools/application.ts +12 -5
- package/components/mcp/tools/operations.ts +9 -0
- package/components/mcp/tools/schemas/operationDescriptions.ts +3 -0
- package/components/mcp/tools/schemas/operations.ts +9 -0
- package/components/operations.js +5 -6
- package/components/operationsValidation.js +32 -2
- package/components/scopeMount.ts +150 -0
- package/config/configUtils.ts +12 -9
- package/config-root.schema.json +14 -0
- package/dataLayer/backupManifest.ts +102 -0
- package/dataLayer/blobBackup.ts +286 -0
- package/dataLayer/harperBridge/ResourceBridge.ts +69 -5
- package/dataLayer/hdbInfoController.ts +8 -0
- package/dataLayer/restoreMarker.ts +276 -0
- package/dataLayer/rocksdbBackup.ts +1100 -0
- package/dataLayer/schemaDescribe.ts +2 -1
- package/dist/bin/backup.d.ts +9 -0
- package/dist/bin/backup.js +192 -0
- package/dist/bin/backup.js.map +1 -0
- package/dist/bin/cliOperations.d.ts +13 -0
- package/dist/bin/cliOperations.js +89 -70
- package/dist/bin/cliOperations.js.map +1 -1
- package/dist/bin/copyDb.js +13 -2
- package/dist/bin/copyDb.js.map +1 -1
- package/dist/bin/harper.d.ts +13 -0
- package/dist/bin/harper.js +45 -56
- package/dist/bin/harper.js.map +1 -1
- package/dist/bin/help.d.ts +8 -0
- package/dist/bin/help.js +192 -0
- package/dist/bin/help.js.map +1 -0
- package/dist/components/Application.d.ts +16 -1
- package/dist/components/Application.js +210 -38
- package/dist/components/Application.js.map +1 -1
- package/dist/components/ApplicationScope.d.ts +7 -0
- package/dist/components/ApplicationScope.js +22 -0
- package/dist/components/ApplicationScope.js.map +1 -1
- package/dist/components/EntryHandler.d.ts +4 -4
- package/dist/components/EntryHandler.js +386 -95
- package/dist/components/EntryHandler.js.map +1 -1
- package/dist/components/RuntimeModuleTracker.d.ts +11 -0
- package/dist/components/RuntimeModuleTracker.js +189 -0
- package/dist/components/RuntimeModuleTracker.js.map +1 -0
- package/dist/components/Scope.d.ts +38 -1
- package/dist/components/Scope.js +117 -28
- package/dist/components/Scope.js.map +1 -1
- package/dist/components/componentLoader.d.ts +2 -6
- package/dist/components/componentLoader.js +160 -18
- package/dist/components/componentLoader.js.map +1 -1
- package/dist/components/deployLifecycle.d.ts +6 -2
- package/dist/components/deployLifecycle.js +109 -31
- package/dist/components/deployLifecycle.js.map +1 -1
- package/dist/components/mcp/toolRegistry.d.ts +7 -0
- package/dist/components/mcp/toolRegistry.js +10 -0
- package/dist/components/mcp/toolRegistry.js.map +1 -1
- package/dist/components/mcp/tools/application.js +11 -5
- package/dist/components/mcp/tools/application.js.map +1 -1
- package/dist/components/mcp/tools/operations.js +9 -0
- package/dist/components/mcp/tools/operations.js.map +1 -1
- package/dist/components/mcp/tools/schemas/operationDescriptions.js +2 -0
- package/dist/components/mcp/tools/schemas/operationDescriptions.js.map +1 -1
- package/dist/components/mcp/tools/schemas/operations.js +9 -0
- package/dist/components/mcp/tools/schemas/operations.js.map +1 -1
- package/dist/components/operations.js +6 -6
- package/dist/components/operations.js.map +1 -1
- package/dist/components/operationsValidation.js +34 -2
- package/dist/components/operationsValidation.js.map +1 -1
- package/dist/components/scopeMount.d.ts +86 -0
- package/dist/components/scopeMount.js +131 -0
- package/dist/components/scopeMount.js.map +1 -0
- package/dist/config/configUtils.js +13 -9
- package/dist/config/configUtils.js.map +1 -1
- package/dist/dataLayer/backupManifest.d.ts +26 -0
- package/dist/dataLayer/backupManifest.js +97 -0
- package/dist/dataLayer/backupManifest.js.map +1 -0
- package/dist/dataLayer/blobBackup.d.ts +87 -0
- package/dist/dataLayer/blobBackup.js +282 -0
- package/dist/dataLayer/blobBackup.js.map +1 -0
- package/dist/dataLayer/harperBridge/ResourceBridge.d.ts +3 -0
- package/dist/dataLayer/harperBridge/ResourceBridge.js +45 -5
- package/dist/dataLayer/harperBridge/ResourceBridge.js.map +1 -1
- package/dist/dataLayer/hdbInfoController.js +4 -0
- package/dist/dataLayer/hdbInfoController.js.map +1 -1
- package/dist/dataLayer/restoreMarker.d.ts +122 -0
- package/dist/dataLayer/restoreMarker.js +261 -0
- package/dist/dataLayer/restoreMarker.js.map +1 -0
- package/dist/dataLayer/rocksdbBackup.d.ts +127 -0
- package/dist/dataLayer/rocksdbBackup.js +1039 -0
- package/dist/dataLayer/rocksdbBackup.js.map +1 -0
- package/dist/dataLayer/schemaDescribe.js +2 -1
- package/dist/dataLayer/schemaDescribe.js.map +1 -1
- package/dist/resources/DatabaseTransaction.d.ts +55 -0
- package/dist/resources/DatabaseTransaction.js +282 -109
- package/dist/resources/DatabaseTransaction.js.map +1 -1
- package/dist/resources/ResourceInterface.d.ts +8 -5
- package/dist/resources/ResourceInterface.js.map +1 -1
- package/dist/resources/Resources.js +22 -4
- package/dist/resources/Resources.js.map +1 -1
- package/dist/resources/Table.d.ts +6 -5
- package/dist/resources/Table.js +114 -37
- package/dist/resources/Table.js.map +1 -1
- package/dist/resources/analytics/write.js +6 -6
- package/dist/resources/analytics/write.js.map +1 -1
- package/dist/resources/blob.d.ts +8 -1
- package/dist/resources/blob.js +31 -14
- package/dist/resources/blob.js.map +1 -1
- package/dist/resources/databases.d.ts +59 -1
- package/dist/resources/databases.js +386 -41
- package/dist/resources/databases.js.map +1 -1
- package/dist/resources/jsResource.d.ts +4 -26
- package/dist/resources/jsResource.js +5 -59
- package/dist/resources/jsResource.js.map +1 -1
- package/dist/resources/models/Models.d.ts +11 -1
- package/dist/resources/models/Models.js +10 -1
- package/dist/resources/models/Models.js.map +1 -1
- package/dist/resources/models/backendRegistry.d.ts +9 -0
- package/dist/resources/models/backendRegistry.js +10 -0
- package/dist/resources/models/backendRegistry.js.map +1 -1
- package/dist/resources/models/openaiStream.d.ts +16 -1
- package/dist/resources/models/openaiStream.js +113 -21
- package/dist/resources/models/openaiStream.js.map +1 -1
- package/dist/resources/models/v1/chatCompletions.d.ts +54 -0
- package/dist/resources/models/v1/chatCompletions.js +115 -0
- package/dist/resources/models/v1/chatCompletions.js.map +1 -0
- package/dist/resources/models/v1/embeddings.d.ts +11 -0
- package/dist/resources/models/v1/embeddings.js +71 -0
- package/dist/resources/models/v1/embeddings.js.map +1 -0
- package/dist/resources/models/v1/errors.d.ts +54 -0
- package/dist/resources/models/v1/errors.js +130 -0
- package/dist/resources/models/v1/errors.js.map +1 -0
- package/dist/resources/models/v1/index.d.ts +36 -0
- package/dist/resources/models/v1/index.js +75 -0
- package/dist/resources/models/v1/index.js.map +1 -0
- package/dist/resources/models/v1/models.d.ts +26 -0
- package/dist/resources/models/v1/models.js +44 -0
- package/dist/resources/models/v1/models.js.map +1 -0
- package/dist/resources/models/v1/translation.d.ts +133 -0
- package/dist/resources/models/v1/translation.js +298 -0
- package/dist/resources/models/v1/translation.js.map +1 -0
- package/dist/resources/roles.d.ts +1 -1
- package/dist/resources/roles.js +54 -7
- package/dist/resources/roles.js.map +1 -1
- package/dist/resources/transaction.js +0 -3
- package/dist/resources/transaction.js.map +1 -1
- package/dist/security/jsLoader.js +84 -33
- package/dist/security/jsLoader.js.map +1 -1
- package/dist/security/role.js +4 -0
- package/dist/security/role.js.map +1 -1
- package/dist/security/superUserGuard.d.ts +7 -0
- package/dist/security/superUserGuard.js +23 -0
- package/dist/security/superUserGuard.js.map +1 -0
- package/dist/security/tokenAuthentication.d.ts +0 -1
- package/dist/security/tokenAuthentication.js +6 -2
- package/dist/security/tokenAuthentication.js.map +1 -1
- package/dist/security/user.d.ts +6 -1
- package/dist/security/user.js +23 -1
- package/dist/security/user.js.map +1 -1
- package/dist/server/REST.js +25 -9
- package/dist/server/REST.js.map +1 -1
- package/dist/server/fastifyRoutes.js +15 -1
- package/dist/server/fastifyRoutes.js.map +1 -1
- package/dist/server/http.d.ts +23 -2
- package/dist/server/http.js +99 -12
- package/dist/server/http.js.map +1 -1
- package/dist/server/itc/serverHandlers.js +7 -1
- package/dist/server/itc/serverHandlers.js.map +1 -1
- package/dist/server/jobs/jobProcess.js +20 -1
- package/dist/server/jobs/jobProcess.js.map +1 -1
- package/dist/server/jobs/jobRunner.js +10 -0
- package/dist/server/jobs/jobRunner.js.map +1 -1
- package/dist/server/jobs/jobs.js +11 -0
- package/dist/server/jobs/jobs.js.map +1 -1
- package/dist/server/middlewareChain.d.ts +10 -1
- package/dist/server/middlewareChain.js +81 -21
- package/dist/server/middlewareChain.js.map +1 -1
- package/dist/server/operationsServer.d.ts +0 -2
- package/dist/server/operationsServer.js.map +1 -1
- package/dist/server/serverHelpers/multipartParser.js +9 -0
- package/dist/server/serverHelpers/multipartParser.js.map +1 -1
- package/dist/server/serverHelpers/operationAuthorizationState.d.ts +2 -0
- package/dist/server/serverHelpers/operationAuthorizationState.js +13 -0
- package/dist/server/serverHelpers/operationAuthorizationState.js.map +1 -0
- package/dist/server/serverHelpers/registeredOperations.d.ts +3 -2
- package/dist/server/serverHelpers/registeredOperations.js +14 -13
- package/dist/server/serverHelpers/registeredOperations.js.map +1 -1
- package/dist/server/serverHelpers/serverHandlers.js +27 -7
- package/dist/server/serverHelpers/serverHandlers.js.map +1 -1
- package/dist/server/serverHelpers/serverUtilities.d.ts +1 -1
- package/dist/server/serverHelpers/serverUtilities.js +21 -7
- package/dist/server/serverHelpers/serverUtilities.js.map +1 -1
- package/dist/server/static.js +68 -21
- package/dist/server/static.js.map +1 -1
- package/dist/server/storageReclamation.d.ts +17 -0
- package/dist/server/storageReclamation.js +90 -6
- package/dist/server/storageReclamation.js.map +1 -1
- package/dist/server/threads/socketRouter.js +20 -0
- package/dist/server/threads/socketRouter.js.map +1 -1
- package/dist/server/threads/threadServer.js +12 -0
- package/dist/server/threads/threadServer.js.map +1 -1
- package/dist/sqlEngine/diff/differential.js +7 -3
- package/dist/sqlEngine/diff/differential.js.map +1 -1
- package/dist/sqlTranslator/index.js +6 -1
- package/dist/sqlTranslator/index.js.map +1 -1
- package/dist/upgrade/upgradePrompt.d.ts +2 -2
- package/dist/upgrade/upgradePrompt.js +22 -3
- package/dist/upgrade/upgradePrompt.js.map +1 -1
- package/dist/utility/OperationFunctionCaller.js +25 -6
- package/dist/utility/OperationFunctionCaller.js.map +1 -1
- package/dist/utility/errors/commonErrors.d.ts +1 -0
- package/dist/utility/errors/commonErrors.js +1 -0
- package/dist/utility/errors/commonErrors.js.map +1 -1
- package/dist/utility/hdbTerms.d.ts +13 -1
- package/dist/utility/hdbTerms.js +13 -1
- package/dist/utility/hdbTerms.js.map +1 -1
- package/dist/utility/install/installer.js +32 -0
- package/dist/utility/install/installer.js.map +1 -1
- package/dist/utility/logging/harper_logger.d.ts +47 -0
- package/dist/utility/logging/harper_logger.js +803 -38
- package/dist/utility/logging/harper_logger.js.map +1 -1
- package/dist/utility/operation_authorization.js +13 -3
- package/dist/utility/operation_authorization.js.map +1 -1
- package/npm-shrinkwrap.json +198 -193
- package/package.json +12 -7
- package/resources/DESIGN.md +2 -0
- package/resources/DatabaseTransaction.ts +310 -104
- package/resources/ResourceInterface.ts +8 -5
- package/resources/Resources.ts +22 -4
- package/resources/Table.ts +341 -263
- package/resources/analytics/write.ts +22 -20
- package/resources/blob.ts +31 -14
- package/resources/databases.ts +387 -46
- package/resources/jsResource.ts +5 -62
- package/resources/models/Models.ts +14 -1
- package/resources/models/backendRegistry.ts +10 -0
- package/resources/models/openaiStream.ts +131 -19
- package/resources/models/v1/chatCompletions.ts +128 -0
- package/resources/models/v1/embeddings.ts +70 -0
- package/resources/models/v1/errors.ts +141 -0
- package/resources/models/v1/index.ts +72 -0
- package/resources/models/v1/models.ts +53 -0
- package/resources/models/v1/translation.ts +362 -0
- package/resources/roles.ts +67 -7
- package/resources/transaction.ts +0 -3
- package/security/jsLoader.ts +84 -30
- package/security/role.ts +7 -0
- package/security/superUserGuard.ts +20 -0
- package/security/tokenAuthentication.ts +6 -3
- package/security/user.ts +26 -1
- package/server/DESIGN.md +61 -34
- package/server/REST.ts +25 -9
- package/server/fastifyRoutes.ts +20 -1
- package/server/http.ts +100 -13
- package/server/itc/serverHandlers.js +7 -1
- package/server/jobs/jobProcess.ts +18 -1
- package/server/jobs/jobRunner.ts +10 -0
- package/server/jobs/jobs.ts +11 -0
- package/server/middlewareChain.ts +79 -20
- package/server/operationsServer.ts +0 -2
- package/server/serverHelpers/multipartParser.ts +9 -0
- package/server/serverHelpers/operationAuthorizationState.ts +11 -0
- package/server/serverHelpers/registeredOperations.ts +19 -15
- package/server/serverHelpers/serverHandlers.js +28 -7
- package/server/serverHelpers/serverUtilities.ts +29 -7
- package/server/static.ts +87 -23
- package/server/storageReclamation.ts +104 -8
- package/server/threads/socketRouter.ts +20 -0
- package/server/threads/threadServer.js +11 -0
- package/sqlTranslator/index.ts +6 -1
- package/static/defaultConfig.yaml +3 -0
- package/studio/web/assets/{Chat-DK3GlWEb.js → Chat-aApwhRmz.js} +2 -2
- package/studio/web/assets/{Chat-DK3GlWEb.js.map → Chat-aApwhRmz.js.map} +1 -1
- package/studio/web/assets/{FloatingChat-fBcC1Ew_.js → FloatingChat-sC0H91n9.js} +4 -4
- package/studio/web/assets/{FloatingChat-fBcC1Ew_.js.map → FloatingChat-sC0H91n9.js.map} +1 -1
- package/studio/web/assets/{apiToken-DJo1nakA.js → apiToken-DZ8JhHOI.js} +2 -2
- package/studio/web/assets/{apiToken-DJo1nakA.js.map → apiToken-DZ8JhHOI.js.map} +1 -1
- package/studio/web/assets/{applications-BDfH8urd.js → applications-9SEVLIO5.js} +2 -2
- package/studio/web/assets/{applications-BDfH8urd.js.map → applications-9SEVLIO5.js.map} +1 -1
- package/studio/web/assets/{index-BHo3c2Gk.js → index-Dy3uDGXb.js} +6 -6
- package/studio/web/assets/index-Dy3uDGXb.js.map +1 -0
- package/studio/web/assets/{index.lazy-BTo0y6UM.js → index.lazy-CN1zq4I4.js} +4 -4
- package/studio/web/assets/{index.lazy-BTo0y6UM.js.map → index.lazy-CN1zq4I4.js.map} +1 -1
- package/studio/web/assets/{notifications-CMxvWNnz.js → notifications-BFAF07xr.js} +2 -2
- package/studio/web/assets/{notifications-CMxvWNnz.js.map → notifications-BFAF07xr.js.map} +1 -1
- package/studio/web/assets/{notifications-D3GoB26g.js → notifications-CKlYVvVN.js} +2 -2
- package/studio/web/assets/{notifications-D3GoB26g.js.map → notifications-CKlYVvVN.js.map} +1 -1
- package/studio/web/assets/{profile-Doj5FVDE.js → profile-A1zhEdFG.js} +2 -2
- package/studio/web/assets/{profile-Doj5FVDE.js.map → profile-A1zhEdFG.js.map} +1 -1
- package/studio/web/assets/{setComponentFile-yinsqJy0.js → setComponentFile-SEtBt_GV.js} +2 -2
- package/studio/web/assets/{setComponentFile-yinsqJy0.js.map → setComponentFile-SEtBt_GV.js.map} +1 -1
- package/studio/web/assets/{setup-DJwR0BHd.js → setup-BisINqdH.js} +2 -2
- package/studio/web/assets/{setup-DJwR0BHd.js.map → setup-BisINqdH.js.map} +1 -1
- package/studio/web/assets/{status-Br_AbsJs.js → status-BuQoCc7l.js} +2 -2
- package/studio/web/assets/{status-Br_AbsJs.js.map → status-BuQoCc7l.js.map} +1 -1
- package/studio/web/assets/{swagger-ui-react-02XH5sVf.js → swagger-ui-react-CAi_s1PC.js} +2 -2
- package/studio/web/assets/{swagger-ui-react-02XH5sVf.js.map → swagger-ui-react-CAi_s1PC.js.map} +1 -1
- package/studio/web/assets/{tsMode-D1DMKY7h.js → tsMode-DT74tlkM.js} +2 -2
- package/studio/web/assets/{tsMode-D1DMKY7h.js.map → tsMode-DT74tlkM.js.map} +1 -1
- package/studio/web/assets/{useEntityRestURL-DtRblRgw.js → useEntityRestURL-CGRGc1n7.js} +2 -2
- package/studio/web/assets/{useEntityRestURL-DtRblRgw.js.map → useEntityRestURL-CGRGc1n7.js.map} +1 -1
- package/studio/web/index.html +1 -1
- package/upgrade/upgradePrompt.ts +22 -3
- package/utility/OperationFunctionCaller.ts +24 -3
- package/utility/errors/commonErrors.ts +2 -0
- package/utility/hdbTerms.ts +13 -1
- package/utility/install/installer.ts +37 -0
- package/utility/logging/harper_logger.ts +786 -41
- package/utility/operation_authorization.ts +31 -3
- package/studio/web/assets/index-BHo3c2Gk.js.map +0 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GET /v1/models` — OpenAI-compatible model list endpoint (#631).
|
|
3
|
+
*
|
|
4
|
+
* Enumerates all registered embedding and generative backends from the
|
|
5
|
+
* process-wide `backendRegistry`. The response mirrors the OpenAI shape:
|
|
6
|
+
* `{ object: 'list', data: [{ id, object: 'model', created, owned_by }] }`.
|
|
7
|
+
*
|
|
8
|
+
* `logicalName` (not `backend.name`) is the `id` — it's what callers pass as
|
|
9
|
+
* `model` in subsequent requests.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Resource } from '../../Resource.ts';
|
|
13
|
+
import { listBackends } from '../backendRegistry.ts';
|
|
14
|
+
import { authorizeV1Request, type OpenAIErrorResponse } from './errors.ts';
|
|
15
|
+
|
|
16
|
+
export interface OAIModelEntry {
|
|
17
|
+
id: string;
|
|
18
|
+
object: 'model';
|
|
19
|
+
created: number;
|
|
20
|
+
owned_by: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface OAIModelList {
|
|
24
|
+
object: 'list';
|
|
25
|
+
data: OAIModelEntry[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// @ts-ignore — Resource base class is not typed for static dispatch; pattern mirrors login.ts
|
|
29
|
+
export class V1Models extends Resource {
|
|
30
|
+
// Reserve this fixed route: a later app registration at the same path becomes a
|
|
31
|
+
// loud conflict (ErrorResource) instead of silently replacing the gateway and its
|
|
32
|
+
// super_user gate. See Resources.set.
|
|
33
|
+
static reservedPath = true;
|
|
34
|
+
|
|
35
|
+
static get(_target: unknown, request: unknown): OAIModelList | OpenAIErrorResponse {
|
|
36
|
+
const authError = authorizeV1Request(request as any);
|
|
37
|
+
if (authError) return authError;
|
|
38
|
+
|
|
39
|
+
const created = Math.floor(Date.now() / 1000);
|
|
40
|
+
// OpenAI model ids are unique; a logical name registered for both generative and
|
|
41
|
+
// embedding (e.g. `default` in each section) is one model id to callers.
|
|
42
|
+
const ids = new Set<string>();
|
|
43
|
+
const data: OAIModelEntry[] = [];
|
|
44
|
+
for (const kind of ['generative', 'embedding'] as const) {
|
|
45
|
+
for (const { logicalName } of listBackends(kind)) {
|
|
46
|
+
if (ids.has(logicalName)) continue;
|
|
47
|
+
ids.add(logicalName);
|
|
48
|
+
data.push({ id: logicalName, object: 'model', created, owned_by: 'harper' });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { object: 'list', data };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI ↔ Harper internal shape mappers for the `/v1/*` gateway (#631).
|
|
3
|
+
*
|
|
4
|
+
* All functions are pure (no I/O, no side-effects) so they can be unit-tested
|
|
5
|
+
* in isolation without a running Harper instance.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { randomUUID } from 'node:crypto';
|
|
9
|
+
import type {
|
|
10
|
+
EmbedOpts,
|
|
11
|
+
GenerateInput,
|
|
12
|
+
GenerateOpts,
|
|
13
|
+
GenerateResult,
|
|
14
|
+
Message,
|
|
15
|
+
ToolCall,
|
|
16
|
+
ToolDef,
|
|
17
|
+
TokenUsage,
|
|
18
|
+
} from '../types.ts';
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// OpenAI wire shapes — inlined to avoid a runtime dependency on the SDK
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
export interface OAIMessageIn {
|
|
25
|
+
role: string;
|
|
26
|
+
/** May be null for assistant messages that only contain tool_calls. */
|
|
27
|
+
content: string | null;
|
|
28
|
+
tool_calls?: Array<{
|
|
29
|
+
id: string;
|
|
30
|
+
type: string;
|
|
31
|
+
function: { name: string; arguments: string };
|
|
32
|
+
}>;
|
|
33
|
+
/** Present on role === 'tool' messages. */
|
|
34
|
+
tool_call_id?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface OAIToolIn {
|
|
38
|
+
type: 'function';
|
|
39
|
+
function: {
|
|
40
|
+
name: string;
|
|
41
|
+
description?: string;
|
|
42
|
+
parameters?: object;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface OAIChatRequest {
|
|
47
|
+
model?: string;
|
|
48
|
+
messages: OAIMessageIn[];
|
|
49
|
+
tools?: OAIToolIn[];
|
|
50
|
+
/** Subset recognised: 'none' | 'auto' | 'required' | {type:'function', function:{name}}. */
|
|
51
|
+
tool_choice?: unknown;
|
|
52
|
+
temperature?: number;
|
|
53
|
+
/** OpenAI v1 field; superseded by max_completion_tokens in v1+. */
|
|
54
|
+
max_tokens?: number;
|
|
55
|
+
/** Preferred alias; takes precedence over max_tokens when both present. */
|
|
56
|
+
max_completion_tokens?: number;
|
|
57
|
+
response_format?: { type: string; json_schema?: unknown };
|
|
58
|
+
stream?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// OpenAI request → Harper internal
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Map an OpenAI `messages` array to Harper `Message[]`.
|
|
67
|
+
* Normalises `tool_calls[].function.arguments` from JSON strings to parsed
|
|
68
|
+
* objects (Harper's internal contract); maps `tool_call_id` to `toolCallId`.
|
|
69
|
+
*/
|
|
70
|
+
export function translateMessages(oaiMessages: OAIMessageIn[]): Message[] {
|
|
71
|
+
return oaiMessages.map((m): Message => {
|
|
72
|
+
// Content parts are flattened to the text Harper's Message.content expects; shapes
|
|
73
|
+
// that cannot be flattened are rejected up front by validateChatRequest.
|
|
74
|
+
const content = Array.isArray(m.content)
|
|
75
|
+
? (m.content as Array<{ text: string }>).map((part) => part.text).join('')
|
|
76
|
+
: (m.content ?? '');
|
|
77
|
+
const base: Message = {
|
|
78
|
+
// `developer` is OpenAI's successor to `system` (the API itself treats the two
|
|
79
|
+
// identically, converting `system` to `developer` on models that use the newer
|
|
80
|
+
// hierarchy). Harper's internal Message union carries the channel as `system`,
|
|
81
|
+
// which every provider adapter maps to its system-instruction slot — so the
|
|
82
|
+
// instruction's priority is preserved rather than degraded to `user`. Roles
|
|
83
|
+
// outside the union are rejected up front by validateChatRequest, so the cast
|
|
84
|
+
// below is over a vetted set.
|
|
85
|
+
role: m.role === 'developer' ? 'system' : (m.role as Message['role']),
|
|
86
|
+
content,
|
|
87
|
+
};
|
|
88
|
+
if (m.tool_calls?.length) {
|
|
89
|
+
base.toolCalls = m.tool_calls.map((tc): ToolCall => {
|
|
90
|
+
let args: object;
|
|
91
|
+
try {
|
|
92
|
+
args = JSON.parse(tc.function.arguments);
|
|
93
|
+
} catch {
|
|
94
|
+
// Preserve unparseable argument strings under a sentinel key rather
|
|
95
|
+
// than dropping them — backend can decide what to do.
|
|
96
|
+
args = { _raw: tc.function.arguments };
|
|
97
|
+
}
|
|
98
|
+
return { id: tc.id, name: tc.function.name, arguments: args };
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (m.tool_call_id) base.toolCallId = m.tool_call_id;
|
|
102
|
+
return base;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Map OpenAI `tools[]` to Harper `ToolDef[]`. */
|
|
107
|
+
export function translateTools(oaiTools: OAIToolIn[]): ToolDef[] {
|
|
108
|
+
return oaiTools.map((t): ToolDef => ({
|
|
109
|
+
name: t.function.name,
|
|
110
|
+
description: t.function.description ?? '',
|
|
111
|
+
parameters: t.function.parameters ?? {},
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Build Harper `GenerateInput` from translated messages and tool definitions.
|
|
117
|
+
* Uses the object form `{ messages, tools }` when tools are present, so that
|
|
118
|
+
* tool definitions travel alongside messages per Harper's type contract.
|
|
119
|
+
*/
|
|
120
|
+
export function toGenerateInput(messages: Message[], tools: ToolDef[] | undefined): GenerateInput {
|
|
121
|
+
if (tools?.length) return { messages, tools };
|
|
122
|
+
return messages;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** `tool_choice` values the internal contract can faithfully represent. */
|
|
126
|
+
function isRepresentableToolChoice(choice: unknown): boolean {
|
|
127
|
+
return choice === undefined || choice === null || choice === 'auto' || choice === 'none';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Validate the OpenAI wire shapes this gateway maps, returning a client-facing
|
|
132
|
+
* message for a 400 or `null` when the request is well-formed.
|
|
133
|
+
*
|
|
134
|
+
* Kept separate from the mappers so malformed nested input (`messages: [null]`,
|
|
135
|
+
* `tool_calls: [{}]`, `tools: [{}]`) becomes an OpenAI-shaped 400 instead of a
|
|
136
|
+
* TypeError escaping the handler as an RFC 9457 500.
|
|
137
|
+
*/
|
|
138
|
+
export function validateChatRequest(body: OAIChatRequest): string | null {
|
|
139
|
+
const req = body as any;
|
|
140
|
+
// Top-level fields that control routing and framing first: a non-string `model`
|
|
141
|
+
// would silently invoke the configured default model (potentially expensive), and a
|
|
142
|
+
// truthy non-boolean `stream` ("false") would return SSE the client didn't ask for.
|
|
143
|
+
if (req.model !== undefined && typeof req.model !== 'string') return "'model' must be a string";
|
|
144
|
+
if (req.stream !== undefined && typeof req.stream !== 'boolean') return "'stream' must be a boolean";
|
|
145
|
+
if (req.temperature !== undefined) {
|
|
146
|
+
if (typeof req.temperature !== 'number' || !Number.isFinite(req.temperature)) {
|
|
147
|
+
return "'temperature' must be a finite number";
|
|
148
|
+
}
|
|
149
|
+
if (req.temperature < 0 || req.temperature > 2) return "'temperature' must be between 0 and 2";
|
|
150
|
+
}
|
|
151
|
+
for (const field of ['max_tokens', 'max_completion_tokens']) {
|
|
152
|
+
const value = req[field];
|
|
153
|
+
if (value === undefined) continue;
|
|
154
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value < 1) {
|
|
155
|
+
return `'${field}' must be a positive integer`;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (!Array.isArray(req.messages) || req.messages.length === 0) return "'messages' must be a non-empty array";
|
|
159
|
+
for (let i = 0; i < req.messages.length; i++) {
|
|
160
|
+
const m = req.messages[i];
|
|
161
|
+
if (!m || typeof m !== 'object' || Array.isArray(m)) return `'messages[${i}]' must be an object`;
|
|
162
|
+
if (typeof m.role !== 'string') return `'messages[${i}].role' must be a string`;
|
|
163
|
+
// Closed set: translateMessages casts into Harper's Message union, so an
|
|
164
|
+
// unknown role must be a loud 400 here — previously it was cast through and
|
|
165
|
+
// provider adapters degraded it to 'user', silently changing its priority.
|
|
166
|
+
// 'developer' is accepted and carried as 'system' (see translateMessages);
|
|
167
|
+
// the deprecated 'function' role is deliberately not supported.
|
|
168
|
+
if (!['system', 'developer', 'user', 'assistant', 'tool'].includes(m.role)) {
|
|
169
|
+
return `'messages[${i}].role' must be one of 'system', 'developer', 'user', 'assistant', 'tool'`;
|
|
170
|
+
}
|
|
171
|
+
// OpenAI allows content parts: [{ type: 'text', text: '...' }, ...]. Harper's
|
|
172
|
+
// Message.content is a string, so those are flattened in translateMessages; reject
|
|
173
|
+
// shapes we cannot flatten rather than passing a non-string downstream.
|
|
174
|
+
if (m.content !== undefined && m.content !== null && typeof m.content !== 'string') {
|
|
175
|
+
if (!Array.isArray(m.content)) return `'messages[${i}].content' must be a string, array of parts, or null`;
|
|
176
|
+
for (let p = 0; p < m.content.length; p++) {
|
|
177
|
+
const part = m.content[p];
|
|
178
|
+
if (!part || typeof part !== 'object') return `'messages[${i}].content[${p}]' must be an object`;
|
|
179
|
+
if (part.type !== 'text' || typeof part.text !== 'string') {
|
|
180
|
+
return `'messages[${i}].content[${p}]' must be a text part ({ type: 'text', text: string }); other part types are not supported yet`;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (m.tool_calls !== undefined) {
|
|
185
|
+
if (!Array.isArray(m.tool_calls)) return `'messages[${i}].tool_calls' must be an array`;
|
|
186
|
+
for (let j = 0; j < m.tool_calls.length; j++) {
|
|
187
|
+
const tc = m.tool_calls[j];
|
|
188
|
+
const at = `'messages[${i}].tool_calls[${j}]`;
|
|
189
|
+
if (!tc || typeof tc !== 'object') return `${at}' must be an object`;
|
|
190
|
+
if (!tc.function || typeof tc.function !== 'object') return `${at}.function' is required`;
|
|
191
|
+
if (typeof tc.function.name !== 'string') return `${at}.function.name' must be a string`;
|
|
192
|
+
if (typeof tc.function.arguments !== 'string') return `${at}.function.arguments' must be a JSON string`;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (req.tools !== undefined) {
|
|
197
|
+
if (!Array.isArray(req.tools)) return "'tools' must be an array";
|
|
198
|
+
for (let i = 0; i < req.tools.length; i++) {
|
|
199
|
+
const t = req.tools[i];
|
|
200
|
+
if (!t || typeof t !== 'object') return `'tools[${i}]' must be an object`;
|
|
201
|
+
if (!t.function || typeof t.function !== 'object') return `'tools[${i}].function' is required`;
|
|
202
|
+
if (typeof t.function.name !== 'string') return `'tools[${i}].function.name' must be a string`;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (!isRepresentableToolChoice(req.tool_choice)) {
|
|
206
|
+
// Better a clear 400 than silently downgrading 'required'/named selection to 'auto'.
|
|
207
|
+
return "'tool_choice' supports 'auto' and 'none'; 'required' and named function selection are not supported yet";
|
|
208
|
+
}
|
|
209
|
+
if (req.response_format !== undefined) {
|
|
210
|
+
const rf = req.response_format;
|
|
211
|
+
if (!rf || typeof rf !== 'object' || Array.isArray(rf)) return "'response_format' must be an object";
|
|
212
|
+
if (rf.type === 'json_schema') {
|
|
213
|
+
const wrapper = rf.json_schema;
|
|
214
|
+
if (!wrapper || typeof wrapper !== 'object') {
|
|
215
|
+
return "'response_format.json_schema' is required when type is 'json_schema'";
|
|
216
|
+
}
|
|
217
|
+
if (!wrapper.schema || typeof wrapper.schema !== 'object') {
|
|
218
|
+
return "'response_format.json_schema.schema' must be a JSON Schema object";
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Map an OpenAI chat-completion request body to `GenerateOpts`.
|
|
227
|
+
*
|
|
228
|
+
* `tool_choice` is honored by the caller, not here: `'none'` omits tools from the
|
|
229
|
+
* generate input entirely, and unrepresentable choices are rejected up front by
|
|
230
|
+
* `validateChatRequest`. Full in-process tool-call orchestration is #612 (out of
|
|
231
|
+
* scope for #631), so tool calls are always returned to the caller to invoke.
|
|
232
|
+
*
|
|
233
|
+
* Assumes `validateChatRequest` has already passed.
|
|
234
|
+
*/
|
|
235
|
+
export function toGenerateOpts(body: OAIChatRequest): GenerateOpts {
|
|
236
|
+
const opts: GenerateOpts = { toolMode: 'return' };
|
|
237
|
+
if (typeof body.model === 'string') opts.model = body.model;
|
|
238
|
+
if (typeof body.temperature === 'number') opts.temperature = body.temperature;
|
|
239
|
+
const maxTokens = body.max_completion_tokens ?? body.max_tokens;
|
|
240
|
+
if (typeof maxTokens === 'number') opts.maxTokens = maxTokens;
|
|
241
|
+
if (body.response_format) {
|
|
242
|
+
const rf = body.response_format;
|
|
243
|
+
if (rf.type === 'json_object') {
|
|
244
|
+
opts.responseFormat = 'json';
|
|
245
|
+
} else if (rf.type === 'json_schema') {
|
|
246
|
+
// The wire value is a wrapper ({ name, strict, schema }); Harper's contract wants
|
|
247
|
+
// the JSON Schema itself. Passing the wrapper makes the backend wrap it again and
|
|
248
|
+
// send metadata where the provider expects the schema.
|
|
249
|
+
opts.responseFormat = { schema: (rf.json_schema as { schema: object }).schema };
|
|
250
|
+
} else {
|
|
251
|
+
opts.responseFormat = 'text';
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return opts;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Map an OpenAI embeddings request body to `EmbedOpts`. */
|
|
258
|
+
export function toEmbedOpts(body: { model?: string }): EmbedOpts {
|
|
259
|
+
const opts: EmbedOpts = {};
|
|
260
|
+
if (typeof body.model === 'string') opts.model = body.model;
|
|
261
|
+
return opts;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ---------------------------------------------------------------------------
|
|
265
|
+
// Harper internal → OpenAI response shapes
|
|
266
|
+
// ---------------------------------------------------------------------------
|
|
267
|
+
|
|
268
|
+
interface OAIToolCallOut {
|
|
269
|
+
id: string;
|
|
270
|
+
type: 'function';
|
|
271
|
+
function: { name: string; arguments: string };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
interface OAIAssistantMessage {
|
|
275
|
+
role: 'assistant';
|
|
276
|
+
/** null when the message contains only tool_calls. */
|
|
277
|
+
content: string | null;
|
|
278
|
+
tool_calls?: OAIToolCallOut[];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export interface OAIChatCompletion {
|
|
282
|
+
id: string;
|
|
283
|
+
object: 'chat.completion';
|
|
284
|
+
created: number;
|
|
285
|
+
model: string;
|
|
286
|
+
choices: Array<{
|
|
287
|
+
index: number;
|
|
288
|
+
message: OAIAssistantMessage;
|
|
289
|
+
finish_reason: string;
|
|
290
|
+
}>;
|
|
291
|
+
usage: {
|
|
292
|
+
prompt_tokens: number;
|
|
293
|
+
completion_tokens: number;
|
|
294
|
+
total_tokens: number;
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function toOAIToolCalls(toolCalls: ToolCall[]): OAIToolCallOut[] {
|
|
299
|
+
return toolCalls.map((tc) => ({
|
|
300
|
+
id: tc.id,
|
|
301
|
+
type: 'function',
|
|
302
|
+
// Guard against backends that return arguments as a pre-serialised JSON string;
|
|
303
|
+
// passing a string through JSON.stringify would double-encode it.
|
|
304
|
+
function: {
|
|
305
|
+
name: tc.name,
|
|
306
|
+
arguments: typeof tc.arguments === 'string' ? tc.arguments : JSON.stringify(tc.arguments),
|
|
307
|
+
},
|
|
308
|
+
}));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Map a Harper `GenerateResult` to an OpenAI `chat.completion` response object. */
|
|
312
|
+
export function toChatCompletion(result: GenerateResult, model: string, id?: string): OAIChatCompletion {
|
|
313
|
+
const completionId = id ?? `chatcmpl-${randomUUID().replaceAll('-', '')}`;
|
|
314
|
+
const hasTools = !!result.toolCalls?.length;
|
|
315
|
+
const message: OAIAssistantMessage = {
|
|
316
|
+
role: 'assistant',
|
|
317
|
+
// OpenAI sets content to null when there are tool calls and no text content.
|
|
318
|
+
content: result.content || (hasTools ? null : ''),
|
|
319
|
+
};
|
|
320
|
+
if (hasTools) message.tool_calls = toOAIToolCalls(result.toolCalls!);
|
|
321
|
+
const usage = result.usage ?? {};
|
|
322
|
+
return {
|
|
323
|
+
id: completionId,
|
|
324
|
+
object: 'chat.completion',
|
|
325
|
+
created: Math.floor(Date.now() / 1000),
|
|
326
|
+
model,
|
|
327
|
+
choices: [{ index: 0, message, finish_reason: result.finishReason }],
|
|
328
|
+
usage: {
|
|
329
|
+
prompt_tokens: usage.promptTokens ?? 0,
|
|
330
|
+
completion_tokens: usage.completionTokens ?? 0,
|
|
331
|
+
total_tokens: (usage.promptTokens ?? 0) + (usage.completionTokens ?? 0),
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export interface OAIEmbedResponse {
|
|
337
|
+
object: 'list';
|
|
338
|
+
data: Array<{ embedding: number[]; index: number; object: 'embedding' }>;
|
|
339
|
+
model: string;
|
|
340
|
+
usage: { prompt_tokens: number; total_tokens: number };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Map `Float32Array[]` from `models.embed()` to an OpenAI embeddings response. */
|
|
344
|
+
export function toEmbedResponse(vecs: Float32Array[], model: string, usage?: TokenUsage): OAIEmbedResponse {
|
|
345
|
+
return {
|
|
346
|
+
object: 'list',
|
|
347
|
+
data: vecs.map((vec, index) => ({
|
|
348
|
+
embedding: Array.from(vec),
|
|
349
|
+
index,
|
|
350
|
+
object: 'embedding',
|
|
351
|
+
})),
|
|
352
|
+
model,
|
|
353
|
+
// Embeddings have no completion side, so OpenAI reports the same count in both
|
|
354
|
+
// fields. Harper backends spell it `embeddingTokens` (with `promptTokens` as a
|
|
355
|
+
// fallback for backends that report it that way); mapping must be symmetric —
|
|
356
|
+
// a backend supplying only embeddingTokens must not leave prompt_tokens at 0.
|
|
357
|
+
usage: {
|
|
358
|
+
prompt_tokens: usage?.embeddingTokens ?? usage?.promptTokens ?? 0,
|
|
359
|
+
total_tokens: usage?.embeddingTokens ?? usage?.promptTokens ?? 0,
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
}
|
package/resources/roles.ts
CHANGED
|
@@ -13,21 +13,80 @@ const USERS_NOT_DBS: readonly string[] = RESERVED_DATABASE_NAMES;
|
|
|
13
13
|
* This is the component for handling role declarations in the Harper system. This will read roles.yaml for role
|
|
14
14
|
* definitions and ensure that they are created in the system database.
|
|
15
15
|
*/
|
|
16
|
-
export function handleApplication(
|
|
17
|
-
scope
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
export function handleApplication(
|
|
17
|
+
scope: import('../components/Scope.ts').Scope,
|
|
18
|
+
ensureRoleFn: (role: any) => Promise<unknown> = ensureRole
|
|
19
|
+
) {
|
|
20
|
+
const rolesByFile = new Map<string, Set<string>>();
|
|
21
|
+
const contentsByFile = new Map<string, Buffer>();
|
|
22
|
+
let pending = Promise.resolve();
|
|
23
|
+
const enqueue = (operation: () => Promise<void>) => {
|
|
24
|
+
const queued = pending.then(operation);
|
|
25
|
+
pending = queued.catch(() => {});
|
|
26
|
+
return queued;
|
|
27
|
+
};
|
|
28
|
+
const entryHandler = scope.handleEntry((entry) =>
|
|
29
|
+
enqueue(async () => {
|
|
30
|
+
if (entry.entryType !== 'file') return;
|
|
31
|
+
const previousRoles = rolesByFile.get(entry.absolutePath) ?? new Set<string>();
|
|
32
|
+
if (entry.eventType === 'unlink') {
|
|
33
|
+
rolesByFile.delete(entry.absolutePath);
|
|
34
|
+
contentsByFile.delete(entry.absolutePath);
|
|
35
|
+
warnForStaleRoles(scope, previousRoles, rolesByFile);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
contentsByFile.set(entry.absolutePath, entry.contents);
|
|
39
|
+
const currentRoles = await handleFile(entry.contents, ensureRoleFn);
|
|
40
|
+
rolesByFile.set(entry.absolutePath, currentRoles);
|
|
41
|
+
warnForStaleRoles(
|
|
42
|
+
scope,
|
|
43
|
+
new Set([...previousRoles].filter((roleName) => !currentRoles.has(roleName))),
|
|
44
|
+
rolesByFile
|
|
45
|
+
);
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
if (!entryHandler) return;
|
|
49
|
+
scope.on('deploy:end', () => {
|
|
50
|
+
void entryHandler.ready
|
|
51
|
+
.then(() =>
|
|
52
|
+
enqueue(async () => {
|
|
53
|
+
for (const contents of contentsByFile.values()) {
|
|
54
|
+
try {
|
|
55
|
+
await handleFile(contents, ensureRoleFn);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
scope.logger.error?.('Could not reconcile a roles file after deploy:', error);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
)
|
|
62
|
+
.catch((error) => scope.logger.error?.('Could not reconcile roles after deploy:', error));
|
|
20
63
|
});
|
|
21
64
|
}
|
|
22
65
|
|
|
66
|
+
function warnForStaleRoles(
|
|
67
|
+
scope: import('../components/Scope.ts').Scope,
|
|
68
|
+
removedRoles: Set<string>,
|
|
69
|
+
rolesByFile: Map<string, Set<string>>
|
|
70
|
+
): void {
|
|
71
|
+
const stillDeclared = new Set<string>();
|
|
72
|
+
for (const roleNames of rolesByFile.values()) for (const roleName of roleNames) stillDeclared.add(roleName);
|
|
73
|
+
const staleRoles = [...removedRoles].filter((roleName) => !stillDeclared.has(roleName));
|
|
74
|
+
if (staleRoles.length === 0) return;
|
|
75
|
+
scope.logger.warn(
|
|
76
|
+
`Role definitions removed for ${staleRoles.join(', ')}; existing roles and grants remain active until explicitly altered or deleted`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
23
80
|
/**
|
|
24
81
|
* This function will handle the roles.yaml file content that has been read, and ensure that the roles are translated to
|
|
25
82
|
* the right shape and created in the system database.
|
|
26
83
|
* @param rolesContent
|
|
27
84
|
*/
|
|
28
|
-
async function handleFile(rolesContent) {
|
|
85
|
+
async function handleFile(rolesContent, ensureRoleFn: (role: any) => Promise<unknown>) {
|
|
29
86
|
let rolesToDefine = parseDocument(rolesContent.toString(), { simpleKeys: true } as any).toJSON();
|
|
87
|
+
const roleNames = new Set<string>();
|
|
30
88
|
for (let roleName in rolesToDefine) {
|
|
89
|
+
roleNames.add(roleName);
|
|
31
90
|
let role = rolesToDefine[roleName];
|
|
32
91
|
if (!role.permission) {
|
|
33
92
|
// we allow the permission object to be collapsed into the root object for convenience
|
|
@@ -78,8 +137,9 @@ async function handleFile(rolesContent) {
|
|
|
78
137
|
}
|
|
79
138
|
}
|
|
80
139
|
role.role = role.id = roleName;
|
|
81
|
-
await
|
|
140
|
+
await ensureRoleFn(role);
|
|
82
141
|
}
|
|
142
|
+
return roleNames;
|
|
83
143
|
}
|
|
84
144
|
async function ensureRole(role) {
|
|
85
145
|
const roleTable = getDatabases().system.hdb_role;
|
|
@@ -87,10 +147,10 @@ async function ensureRole(role) {
|
|
|
87
147
|
for await (let existingRole of roleTable.search([{ attribute: 'role', value: role.role }] as any)) {
|
|
88
148
|
// use the existing role id so we can update in place. Legacy roles may have a UUID for the id instead of the role name
|
|
89
149
|
const { __createdtime__, __updatedtime__, ...existingRoleData } = existingRole;
|
|
150
|
+
role.id = existingRole.id;
|
|
90
151
|
if (isEqual(existingRoleData, role)) {
|
|
91
152
|
return;
|
|
92
153
|
}
|
|
93
|
-
role.id = existingRole.id;
|
|
94
154
|
return alterRole(role);
|
|
95
155
|
}
|
|
96
156
|
return addRole(role);
|
package/resources/transaction.ts
CHANGED
|
@@ -42,9 +42,6 @@ export function transaction<T>(
|
|
|
42
42
|
if (context.replicatedConfirmation) transaction.replicatedConfirmation = context.replicatedConfirmation;
|
|
43
43
|
if (context.sourceApply) transaction.sourceApply = true;
|
|
44
44
|
transaction.setContext(context);
|
|
45
|
-
|
|
46
|
-
// create a resource cache so that multiple requests to the same resource return the same resource
|
|
47
|
-
if (!context.resourceCache) context.resourceCache = new Map();
|
|
48
45
|
let result;
|
|
49
46
|
try {
|
|
50
47
|
result =
|