-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathai-control.js
More file actions
134 lines (119 loc) · 4.91 KB
/
ai-control.js
File metadata and controls
134 lines (119 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2021 - present core.ai. All rights reserved.
/**
* This file is only relevant to desktop apps.
*
* AI can be not active in phoenix code either by:
* 1. Schools with admin control. See https://docs.phcode.dev/docs/control-ai
* 2. user is not entitled to ai services by his subscription.
*
* This file only deals with case 1. you should use `EntitlementsManager.js` to resolve the correct AI entitlment,
* which will reconcile 1 and 2 to give you appropriate status.
**/
/*global */
define(function (require, exports, module) {
const KernalModeTrust = window.KernalModeTrust;
if(!KernalModeTrust){
// integrated extensions will have access to kernal mode, but not external extensions
throw new Error("ai-control.js should have access to KernalModeTrust. Cannot boot without trust ring");
}
const NodeUtils = require("utils/NodeUtils"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils");
let EntitlementsManager;
/**
* Get the platform-specific config file path
* @returns {string} The path to the config file
*/
function _getAIConfigFilePath() {
let aiConfigPath;
// The path is decided by https://github.com/phcode-dev/phoenix-code-ai-control/tree/main/install_scripts
if(!Phoenix.isNativeApp) {
return "";
}
if (Phoenix.platform === 'win') {
aiConfigPath = 'C:\\Program Files\\Phoenix AI Control\\config.json';
} else if (Phoenix.platform === 'mac') {
aiConfigPath = '/Library/Application Support/Phoenix AI Control/config.json';
} else if (Phoenix.platform === 'linux') {
aiConfigPath = '/etc/phoenix-ai-control/config.json';
} else {
throw new Error(`Unsupported platform: ${Phoenix.platform}`);
}
return Phoenix.VFS.getTauriVirtualPath(aiConfigPath);
}
const AI_CONFIG_FILE_PATH = _getAIConfigFilePath();
if(Phoenix.isNativeApp) {
console.log("AI system Config File is: ", AI_CONFIG_FILE_PATH);
}
/**
* Check if the current user is in the allowed users list
* @param {Array<string>} allowedUsers - List of allowed usernames
* @param {string} currentUser to check against
* @returns {boolean} True if current user is allowed
*/
function _isCurrentUserAllowed(allowedUsers, currentUser) {
if (!allowedUsers || !Array.isArray(allowedUsers) || allowedUsers.length === 0) {
return false;
}
return allowedUsers.includes(currentUser);
}
/**
* Get AI control configuration
* @returns {Object} The configuration status and details
*/
async function getAIControlStatus() {
try {
if(!Phoenix.isNativeApp) {
return {aiEnabled: true}; // AI control with system files in not available in browser.
// In browser, AI can be disabled with firewall only.
}
const fileData = await Phoenix.VFS.readFileResolves(AI_CONFIG_FILE_PATH, 'utf8');
if (fileData.error || !fileData.data) {
return {
aiEnabled: true,
message: Strings.AI_CONTROL_ALL_ALLOWED_NO_CONFIG
}; // No ai config file exists
}
const aiConfig = JSON.parse(fileData.data);
const currentUser = await NodeUtils.getOSUserName();
// Check if AI is disabled globally
if (aiConfig.disableAI === true) {
// Check if current user is in allowed users list
if (aiConfig.allowedUsers && _isCurrentUserAllowed(aiConfig.allowedUsers, currentUser)) {
return {
aiEnabled: true,
message: StringUtils.format(Strings.AI_CONTROL_USER_ALLOWED, currentUser)
};
} else if(aiConfig.managedByEmail){
return {
aiEnabled: false,
message: StringUtils.format(Strings.AI_CONTROL_ADMIN_DISABLED_CONTACT, aiConfig.managedByEmail)
};
}
return {
aiEnabled: false,
message: Strings.AI_CONTROL_ADMIN_DISABLED
};
}
// AI is enabled globally
return {
aiEnabled: true,
message: Strings.AI_CONTROL_ALL_ALLOWED
};
} catch (error) {
console.error('Error checking AI control:', error);
return {aiEnabled: true, message: error.message};
}
}
let inited = false;
function init() {
if(inited){
return;
}
inited = true;
EntitlementsManager = KernalModeTrust.EntitlementsManager;
EntitlementsManager.getAIControlStatus = getAIControlStatus;
}
exports.init = init;
});