@alwaysai/device-agent 0.0.11 → 0.0.13

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.
Files changed (181) hide show
  1. package/lib/application-control/backup.d.ts.map +1 -1
  2. package/lib/application-control/backup.js +8 -2
  3. package/lib/application-control/backup.js.map +1 -1
  4. package/lib/application-control/config.d.ts +12 -4
  5. package/lib/application-control/config.d.ts.map +1 -1
  6. package/lib/application-control/config.js +59 -16
  7. package/lib/application-control/config.js.map +1 -1
  8. package/lib/application-control/environment-variables.d.ts.map +1 -1
  9. package/lib/application-control/environment-variables.js.map +1 -1
  10. package/lib/application-control/index.d.ts +4 -4
  11. package/lib/application-control/index.d.ts.map +1 -1
  12. package/lib/application-control/index.js +4 -3
  13. package/lib/application-control/index.js.map +1 -1
  14. package/lib/application-control/install.d.ts.map +1 -1
  15. package/lib/application-control/install.js +28 -14
  16. package/lib/application-control/install.js.map +1 -1
  17. package/lib/application-control/models.d.ts +7 -1
  18. package/lib/application-control/models.d.ts.map +1 -1
  19. package/lib/application-control/models.js +69 -39
  20. package/lib/application-control/models.js.map +1 -1
  21. package/lib/application-control/status.d.ts.map +1 -1
  22. package/lib/application-control/status.js +18 -14
  23. package/lib/application-control/status.js.map +1 -1
  24. package/lib/application-control/utils.d.ts +0 -2
  25. package/lib/application-control/utils.d.ts.map +1 -1
  26. package/lib/application-control/utils.js +7 -16
  27. package/lib/application-control/utils.js.map +1 -1
  28. package/lib/cloud-connection/app-install-status.d.ts +16 -0
  29. package/lib/cloud-connection/app-install-status.d.ts.map +1 -0
  30. package/lib/cloud-connection/app-install-status.js +53 -0
  31. package/lib/cloud-connection/app-install-status.js.map +1 -0
  32. package/lib/cloud-connection/bootstrap-provision.d.ts +2 -0
  33. package/lib/cloud-connection/bootstrap-provision.d.ts.map +1 -0
  34. package/lib/cloud-connection/bootstrap-provision.js +34 -0
  35. package/lib/cloud-connection/bootstrap-provision.js.map +1 -0
  36. package/lib/cloud-connection/device-agent-cloud-connection.d.ts +12 -35
  37. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  38. package/lib/cloud-connection/device-agent-cloud-connection.js +170 -387
  39. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  40. package/lib/cloud-connection/device-agent.d.ts.map +1 -1
  41. package/lib/cloud-connection/device-agent.js +22 -26
  42. package/lib/cloud-connection/device-agent.js.map +1 -1
  43. package/lib/cloud-connection/live-updates-handler.d.ts +34 -0
  44. package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -0
  45. package/lib/cloud-connection/live-updates-handler.js +167 -0
  46. package/lib/cloud-connection/live-updates-handler.js.map +1 -0
  47. package/lib/cloud-connection/messages.d.ts +14 -0
  48. package/lib/cloud-connection/messages.d.ts.map +1 -0
  49. package/lib/cloud-connection/messages.js +38 -0
  50. package/lib/cloud-connection/messages.js.map +1 -0
  51. package/lib/cloud-connection/publisher.d.ts +14 -0
  52. package/lib/cloud-connection/publisher.d.ts.map +1 -0
  53. package/lib/cloud-connection/publisher.js +44 -0
  54. package/lib/cloud-connection/publisher.js.map +1 -0
  55. package/lib/cloud-connection/shadow-handler.d.ts +34 -0
  56. package/lib/cloud-connection/shadow-handler.d.ts.map +1 -0
  57. package/lib/cloud-connection/shadow-handler.js +94 -0
  58. package/lib/cloud-connection/shadow-handler.js.map +1 -0
  59. package/lib/cloud-connection/shadow.d.ts +16 -0
  60. package/lib/cloud-connection/shadow.d.ts.map +1 -0
  61. package/lib/cloud-connection/shadow.js +36 -0
  62. package/lib/cloud-connection/shadow.js.map +1 -0
  63. package/lib/device-control/device-control.d.ts.map +1 -1
  64. package/lib/device-control/device-control.js +1 -0
  65. package/lib/device-control/device-control.js.map +1 -1
  66. package/lib/docker/docker-cmd.js +1 -1
  67. package/lib/docker/docker-compose-cmd.d.ts.map +1 -1
  68. package/lib/docker/docker-compose-cmd.js +1 -1
  69. package/lib/docker/docker-compose-cmd.js.map +1 -1
  70. package/lib/endpoints.js +10 -10
  71. package/lib/endpoints.js.map +1 -1
  72. package/lib/environment.d.ts.map +1 -1
  73. package/lib/environment.js.map +1 -1
  74. package/lib/infrastructure/agent-config.d.ts +4 -14
  75. package/lib/infrastructure/agent-config.d.ts.map +1 -1
  76. package/lib/infrastructure/agent-config.js +22 -15
  77. package/lib/infrastructure/agent-config.js.map +1 -1
  78. package/lib/infrastructure/agent-config.test.js +26 -18
  79. package/lib/infrastructure/agent-config.test.js.map +1 -1
  80. package/lib/infrastructure/system-id.d.ts +2 -0
  81. package/lib/infrastructure/system-id.d.ts.map +1 -0
  82. package/lib/infrastructure/system-id.js +21 -0
  83. package/lib/infrastructure/system-id.js.map +1 -0
  84. package/lib/infrastructure/tokens-and-device-cfg.d.ts +4 -0
  85. package/lib/infrastructure/tokens-and-device-cfg.d.ts.map +1 -0
  86. package/lib/infrastructure/tokens-and-device-cfg.js +31 -0
  87. package/lib/infrastructure/tokens-and-device-cfg.js.map +1 -0
  88. package/lib/infrastructure/urls.d.ts.map +1 -1
  89. package/lib/infrastructure/urls.js +3 -3
  90. package/lib/infrastructure/urls.js.map +1 -1
  91. package/lib/root.d.ts.map +1 -1
  92. package/lib/root.js +2 -7
  93. package/lib/root.js.map +1 -1
  94. package/lib/subcommands/app/app.d.ts.map +1 -1
  95. package/lib/subcommands/app/app.js +62 -60
  96. package/lib/subcommands/app/app.js.map +1 -1
  97. package/lib/subcommands/app/index.js +2 -2
  98. package/lib/subcommands/device/clean.d.ts +2 -0
  99. package/lib/subcommands/device/clean.d.ts.map +1 -0
  100. package/lib/subcommands/device/clean.js +29 -0
  101. package/lib/subcommands/device/clean.js.map +1 -0
  102. package/lib/subcommands/device/device.d.ts.map +1 -1
  103. package/lib/subcommands/device/device.js +40 -27
  104. package/lib/subcommands/device/device.js.map +1 -1
  105. package/lib/subcommands/device/index.d.ts.map +1 -1
  106. package/lib/subcommands/device/index.js +2 -1
  107. package/lib/subcommands/device/index.js.map +1 -1
  108. package/lib/subcommands/get-model-package.js +5 -5
  109. package/lib/subcommands/index.js +1 -1
  110. package/lib/subcommands/login.js +8 -8
  111. package/lib/subcommands/login.js.map +1 -1
  112. package/lib/util/clean-certs.d.ts +2 -0
  113. package/lib/util/clean-certs.d.ts.map +1 -0
  114. package/lib/util/clean-certs.js +16 -0
  115. package/lib/util/clean-certs.js.map +1 -0
  116. package/lib/util/directories.d.ts +16 -15
  117. package/lib/util/directories.d.ts.map +1 -1
  118. package/lib/util/directories.js +45 -26
  119. package/lib/util/directories.js.map +1 -1
  120. package/lib/util/get-device-id.d.ts +1 -1
  121. package/lib/util/get-device-id.d.ts.map +1 -1
  122. package/lib/util/get-device-id.js +14 -19
  123. package/lib/util/get-device-id.js.map +1 -1
  124. package/lib/util/http-client.d.ts +1 -1
  125. package/lib/util/http-client.d.ts.map +1 -1
  126. package/lib/util/http-client.js +10 -8
  127. package/lib/util/http-client.js.map +1 -1
  128. package/lib/util/logger.d.ts.map +1 -1
  129. package/lib/util/logger.js +4 -5
  130. package/lib/util/logger.js.map +1 -1
  131. package/lib/util/run-in-dir.d.ts.map +1 -1
  132. package/lib/util/run-in-dir.js +1 -0
  133. package/lib/util/run-in-dir.js.map +1 -1
  134. package/package.json +18 -8
  135. package/src/application-control/backup.ts +8 -3
  136. package/src/application-control/config.ts +75 -13
  137. package/src/application-control/environment-variables.ts +3 -3
  138. package/src/application-control/index.ts +19 -6
  139. package/src/application-control/install.ts +52 -28
  140. package/src/application-control/models.ts +100 -56
  141. package/src/application-control/status.ts +26 -21
  142. package/src/application-control/utils.ts +9 -20
  143. package/src/cloud-connection/app-install-status.ts +62 -0
  144. package/src/cloud-connection/bootstrap-provision.ts +40 -0
  145. package/src/cloud-connection/device-agent-cloud-connection.ts +258 -527
  146. package/src/cloud-connection/device-agent.ts +31 -38
  147. package/src/cloud-connection/live-updates-handler.ts +226 -0
  148. package/src/cloud-connection/messages.ts +39 -0
  149. package/src/cloud-connection/publisher.ts +65 -0
  150. package/src/cloud-connection/shadow-handler.ts +154 -0
  151. package/src/cloud-connection/shadow.ts +50 -0
  152. package/src/device-control/device-control.ts +1 -0
  153. package/src/docker/docker-cmd.ts +1 -1
  154. package/src/docker/docker-compose-cmd.ts +5 -2
  155. package/src/endpoints.ts +9 -9
  156. package/src/environment.ts +8 -3
  157. package/src/infrastructure/agent-config.test.ts +34 -23
  158. package/src/infrastructure/agent-config.ts +33 -20
  159. package/src/infrastructure/system-id.ts +18 -0
  160. package/src/infrastructure/tokens-and-device-cfg.ts +39 -0
  161. package/src/infrastructure/urls.ts +4 -2
  162. package/src/root.ts +2 -8
  163. package/src/subcommands/app/app.ts +64 -62
  164. package/src/subcommands/app/index.ts +3 -3
  165. package/src/subcommands/device/clean.ts +26 -0
  166. package/src/subcommands/device/device.ts +66 -50
  167. package/src/subcommands/device/index.ts +2 -1
  168. package/src/subcommands/get-model-package.ts +5 -5
  169. package/src/subcommands/index.ts +1 -1
  170. package/src/subcommands/login.ts +8 -8
  171. package/src/util/clean-certs.ts +12 -0
  172. package/src/util/directories.ts +68 -52
  173. package/src/util/get-device-id.ts +16 -18
  174. package/src/util/http-client.ts +18 -13
  175. package/src/util/logger.ts +6 -6
  176. package/src/util/run-in-dir.ts +2 -1
  177. package/lib/infrastructure/certificates-and-tokens.d.ts +0 -6
  178. package/lib/infrastructure/certificates-and-tokens.d.ts.map +0 -1
  179. package/lib/infrastructure/certificates-and-tokens.js +0 -43
  180. package/lib/infrastructure/certificates-and-tokens.js.map +0 -1
  181. package/src/infrastructure/certificates-and-tokens.ts +0 -53
@@ -1,12 +1,15 @@
1
1
  import { JsSpawner } from 'alwaysai/lib/util';
2
2
 
3
- export async function runDockerComposeCmd(props: { args: string[]; dir: string }) {
3
+ export async function runDockerComposeCmd(props: {
4
+ args: string[];
5
+ dir: string;
6
+ }) {
4
7
  const { args, dir } = props;
5
8
  const spawner = JsSpawner();
6
9
  const output = await spawner.run({
7
10
  exe: 'docker-compose',
8
11
  args,
9
- cwd: dir,
12
+ cwd: dir
10
13
  });
11
14
  return output;
12
15
  }
package/src/endpoints.ts CHANGED
@@ -1,19 +1,19 @@
1
- import { getSystemId } from "alwaysai/lib/infrastructure";
1
+ import { getSystemId } from './infrastructure/system-id';
2
2
 
3
3
  export const getSecondLevelDomain = () => {
4
- let domain = "";
4
+ let domain = '';
5
5
  switch (getSystemId()) {
6
- case "development":
7
- domain = "a6i0.net";
6
+ case 'development':
7
+ domain = 'a6i0.net';
8
8
  break;
9
- case "qa":
10
- domain = "a6i1.net";
9
+ case 'qa':
10
+ domain = 'a6i1.net';
11
11
  break;
12
- case "production":
13
- domain = "alwaysai.co";
12
+ case 'production':
13
+ domain = 'alwaysai.co';
14
14
  break;
15
15
  default:
16
- domain = "alwaysai.co";
16
+ domain = 'alwaysai.co';
17
17
  break;
18
18
  }
19
19
  return domain;
@@ -1,8 +1,13 @@
1
1
  import { platform } from 'os';
2
2
 
3
- export const ALWAYSAI_OS_PLATFORM = parseOsPlatform(process.env.ALWAYSAI_OS_PLATFORM);
4
- export const ALWAYSAI_SHOW_HIDDEN = parseBoolean(process.env.ALWAYSAI_SHOW_HIDDEN);
5
- export const ALWAYSAI_DEVICE_AGENT_MODE = process.env.ALWAYSAI_DEVICE_AGENT_MODE;
3
+ export const ALWAYSAI_OS_PLATFORM = parseOsPlatform(
4
+ process.env.ALWAYSAI_OS_PLATFORM
5
+ );
6
+ export const ALWAYSAI_SHOW_HIDDEN = parseBoolean(
7
+ process.env.ALWAYSAI_SHOW_HIDDEN
8
+ );
9
+ export const ALWAYSAI_DEVICE_AGENT_MODE =
10
+ process.env.ALWAYSAI_DEVICE_AGENT_MODE;
6
11
  export const ALWAYSAI_LOG_LEVEL = process.env.AAI_LOG_LEVEL;
7
12
  export const ALWAYSAI_LOG_TO_CONSOLE = process.env.ALWAYSAI_LOG_TO_CONSOLE;
8
13
 
@@ -5,7 +5,7 @@ const mockAgentModeGetter = jest.fn().mockReturnValue(undefined);
5
5
  jest.mock('../environment', () => ({
6
6
  get ALWAYSAI_DEVICE_AGENT_MODE() {
7
7
  return mockAgentModeGetter();
8
- },
8
+ }
9
9
  }));
10
10
 
11
11
  const configFile = AgentConfigFile(tempy.directory());
@@ -21,9 +21,13 @@ describe('Test Agent Config', () => {
21
21
  expect(apps).toEqual([]);
22
22
  const app = await configFile.getApp({ projectId: 'wrong-id' });
23
23
  expect(app).toBeNull();
24
- const present = await configFile.isAppPresent({ projectId: 'another-id' });
24
+ const present = await configFile.isAppPresent({
25
+ projectId: 'another-id'
26
+ });
25
27
  expect(present).toBeFalsy();
26
- const ready = await configFile.isAppReady({ projectId: 'yet-another-id' });
28
+ const ready = await configFile.isAppReady({
29
+ projectId: 'yet-another-id'
30
+ });
27
31
  expect(ready).toBeFalsy();
28
32
  });
29
33
 
@@ -37,8 +41,8 @@ describe('Test Agent Config', () => {
37
41
  {
38
42
  projectId,
39
43
  version,
40
- ready: false,
41
- },
44
+ ready: false
45
+ }
42
46
  ]);
43
47
  const readyApps = await configFile.getReadyApps();
44
48
  expect(readyApps).toEqual([]);
@@ -46,7 +50,7 @@ describe('Test Agent Config', () => {
46
50
  expect(app).toEqual({
47
51
  projectId,
48
52
  version,
49
- ready: false,
53
+ ready: false
50
54
  });
51
55
  const present = await configFile.isAppPresent({ projectId });
52
56
  expect(present).toBeTruthy();
@@ -65,22 +69,22 @@ describe('Test Agent Config', () => {
65
69
  {
66
70
  projectId,
67
71
  version,
68
- ready: true,
69
- },
72
+ ready: true
73
+ }
70
74
  ]);
71
75
  const readyApps = await configFile.getReadyApps();
72
76
  expect(readyApps).toEqual([
73
77
  {
74
78
  projectId,
75
79
  version,
76
- ready: true,
77
- },
80
+ ready: true
81
+ }
78
82
  ]);
79
83
  const app = await configFile.getApp({ projectId });
80
84
  expect(app).toEqual({
81
85
  projectId,
82
86
  version,
83
- ready: true,
87
+ ready: true
84
88
  });
85
89
  const appVersion = await configFile.getAppVersion({ projectId });
86
90
  expect(appVersion).toEqual(version);
@@ -103,9 +107,13 @@ describe('Test Agent Config', () => {
103
107
  expect(apps).toEqual([]);
104
108
  const app = await configFile.getApp({ projectId: 'wrong-id' });
105
109
  expect(app).toBeNull();
106
- const present = await configFile.isAppPresent({ projectId: 'another-id' });
110
+ const present = await configFile.isAppPresent({
111
+ projectId: 'another-id'
112
+ });
107
113
  expect(present).toBeFalsy();
108
- const ready = await configFile.isAppReady({ projectId: 'yet-another-id' });
114
+ const ready = await configFile.isAppReady({
115
+ projectId: 'yet-another-id'
116
+ });
109
117
  expect(ready).toBeFalsy();
110
118
  });
111
119
 
@@ -129,9 +137,9 @@ describe('Test Agent Config', () => {
129
137
  version,
130
138
  ready: true,
131
139
  backup: {
132
- version,
133
- },
134
- },
140
+ version
141
+ }
142
+ }
135
143
  ]);
136
144
  const app = await configFile.getApp({ projectId });
137
145
  expect(app).toEqual({
@@ -139,8 +147,8 @@ describe('Test Agent Config', () => {
139
147
  version,
140
148
  ready: true,
141
149
  backup: {
142
- version,
143
- },
150
+ version
151
+ }
144
152
  });
145
153
  const backup2 = await configFile.getAppBackup({ projectId });
146
154
  expect(backup2).toEqual({ version });
@@ -168,15 +176,17 @@ describe('Test Agent Config', () => {
168
176
 
169
177
  // test to see if app has backup
170
178
  await configFile.setAppBackup({ projectId });
171
- const expectedBackupVersion = (await configFile.getAppBackup({ projectId }))
172
- ?.version;
179
+ const expectedBackupVersion = (
180
+ await configFile.getAppBackup({ projectId })
181
+ )?.version;
173
182
  expect(expectedBackupVersion).toEqual(version1);
174
183
  expect(await configFile.getAppVersion({ projectId })).toEqual(version1);
175
184
 
176
185
  // simulate second version install
177
186
  await configFile.setAppInstalling({ projectId, version: version2 });
178
- const newExpectedBackupVersion = (await configFile.getAppBackup({ projectId }))
179
- ?.version;
187
+ const newExpectedBackupVersion = (
188
+ await configFile.getAppBackup({ projectId })
189
+ )?.version;
180
190
  expect(newExpectedBackupVersion).toEqual(version1);
181
191
  await configFile.setAppInstalled({ projectId, version: version2 });
182
192
  expect(await configFile.getAppVersion({ projectId })).toEqual(version2);
@@ -190,7 +200,8 @@ describe('Test Agent Config', () => {
190
200
 
191
201
  // test final state of rolled-back app
192
202
  await configFile.setAppInstalled({ projectId, version: version1 });
193
- const finalBackupVersion = (await configFile.getAppBackup({ projectId }))?.version;
203
+ const finalBackupVersion = (await configFile.getAppBackup({ projectId }))
204
+ ?.version;
194
205
  expect(finalBackupVersion).toEqual(version1);
195
206
  expect(await configFile.isAppReady({ projectId })).toEqual(true);
196
207
  expect(await configFile.getAppVersion({ projectId })).toEqual(version1);
@@ -1,5 +1,5 @@
1
1
  import { ConfigFileSchema } from '@alwaysai/config-nodejs';
2
- import { JSONSchemaType } from 'ajv';
2
+ import Ajv, { JSONSchemaType } from 'ajv';
3
3
  import { homedir } from 'os';
4
4
  import { join } from 'path';
5
5
 
@@ -33,21 +33,24 @@ const schema: JSONSchemaType<AgentConfig> = {
33
33
  type: 'object',
34
34
  nullable: true,
35
35
  properties: {
36
- version: { type: 'string' },
36
+ version: { type: 'string' }
37
37
  },
38
38
  required: ['version'],
39
- additionalProperties: true,
40
- },
39
+ additionalProperties: true
40
+ }
41
41
  },
42
42
  required: ['projectId', 'version', 'ready'],
43
- additionalProperties: true,
44
- },
45
- },
43
+ additionalProperties: true
44
+ }
45
+ }
46
46
  },
47
47
  required: ['applications'],
48
- additionalProperties: true,
48
+ additionalProperties: true
49
49
  };
50
50
 
51
+ const ajv = new Ajv();
52
+ const validateFunction = ajv.compile(schema);
53
+
51
54
  const ALWAYSAI_CONFIG_DIR = join(homedir(), '.config', 'alwaysai');
52
55
 
53
56
  const AGENT_CONFIG_FILE_NAME = 'alwaysai.agent.json';
@@ -55,12 +58,12 @@ const AGENT_CONFIG_FILE_NAME = 'alwaysai.agent.json';
55
58
  export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
56
59
  const path = join(dir, AGENT_CONFIG_FILE_NAME);
57
60
  const initialValue: AgentConfig = {
58
- applications: [],
61
+ applications: []
59
62
  };
60
63
  const configFile = ConfigFileSchema({
61
64
  path,
62
- schema,
63
- initialValue,
65
+ validateFunction,
66
+ initialValue
64
67
  });
65
68
 
66
69
  return {
@@ -77,19 +80,21 @@ export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
77
80
  setAppUninstalled,
78
81
  getAppVersion,
79
82
  setAppBackup,
80
- getAppBackup,
83
+ getAppBackup
81
84
  };
82
85
 
83
86
  async function getApps() {
84
87
  const config = configFile.read();
85
- return config.applications;
88
+ return config && config.applications ? config.applications : [];
86
89
  }
87
90
 
88
91
  async function getReadyApps() {
89
92
  const config = configFile.read();
90
- return config.applications.filter((app) => {
91
- return app.ready;
92
- });
93
+ return config && config.applications
94
+ ? config.applications.filter((app) => {
95
+ return app.ready;
96
+ })
97
+ : [];
93
98
  }
94
99
 
95
100
  async function getApp(props: { projectId: string }) {
@@ -124,6 +129,8 @@ export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
124
129
  async function removeApp(props: { projectId: string }) {
125
130
  const { projectId } = props;
126
131
  const config = configFile.read();
132
+ if (!config) return;
133
+
127
134
  const newList = config.applications.filter((app) => {
128
135
  return app.projectId !== projectId;
129
136
  });
@@ -131,7 +138,10 @@ export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
131
138
  configFile.write(config);
132
139
  }
133
140
 
134
- async function setAppInstalling(props: { projectId: string; version: string }) {
141
+ async function setAppInstalling(props: {
142
+ projectId: string;
143
+ version: string;
144
+ }) {
135
145
  const { projectId, version } = props;
136
146
 
137
147
  const app = await getApp({ projectId });
@@ -147,13 +157,16 @@ export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
147
157
  config.applications.push({
148
158
  projectId,
149
159
  version,
150
- ready: false,
160
+ ready: false
151
161
  });
152
162
  configFile.write(config);
153
163
  }
154
164
  }
155
165
 
156
- async function setAppInstalled(props: { projectId: string; version: string }) {
166
+ async function setAppInstalled(props: {
167
+ projectId: string;
168
+ version: string;
169
+ }) {
157
170
  const { projectId, version } = props;
158
171
  const app = await getApp({ projectId });
159
172
  if (app) {
@@ -165,7 +178,7 @@ export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
165
178
  // NOTE: we should never be setting an app as installed
166
179
  // if it doesn't exist (setAppInstalling was never called)
167
180
  throw new Error(
168
- `App ${projectId} was not previously configured and could not be set to installed!`,
181
+ `App ${projectId} was not previously configured and could not be set to installed!`
169
182
  );
170
183
  }
171
184
  }
@@ -0,0 +1,18 @@
1
+ import { DeviceConfigFile } from 'alwaysai/lib/core/device';
2
+ import { ALWAYSAI_SYSTEM_ID } from 'alwaysai/lib/environment';
3
+ import { SystemId } from 'alwaysai/lib/infrastructure';
4
+
5
+ export function getSystemId() {
6
+ if (ALWAYSAI_SYSTEM_ID) {
7
+ // When the env var is set return the overridden value without updating the config file.
8
+ // This prevents one-off commands from having side effects.
9
+ if (Object.keys(SystemId).includes(ALWAYSAI_SYSTEM_ID)) {
10
+ return ALWAYSAI_SYSTEM_ID as SystemId;
11
+ }
12
+ throw new Error(`Invalid ALWAYSAI_SYSTEM_ID: ${ALWAYSAI_SYSTEM_ID}`);
13
+ }
14
+ const maybeConfig = DeviceConfigFile().readIfExists();
15
+ const systemId =
16
+ maybeConfig && maybeConfig.systemId ? maybeConfig.systemId : 'production';
17
+ return systemId;
18
+ }
@@ -0,0 +1,39 @@
1
+ import {
2
+ DeviceTokens,
3
+ writeOrValidateDeviceCfgFile
4
+ } from 'alwaysai/lib/components/device';
5
+ import { checkUserIsLoggedInComponent } from 'alwaysai/lib/components/user';
6
+ import { LOCAL_AAI_CFG_DIR } from 'alwaysai/lib/constants';
7
+ import { checkPaidPlan } from 'alwaysai/lib/core/project';
8
+ import { JsSpawner, writeTokens } from 'alwaysai/lib/util';
9
+ import { logger } from '../util/logger';
10
+ import { microServiceHttpClient } from '../util/http-client';
11
+
12
+ // NOTE: This closely follows the flow of deviceCheckAndUpdateComponent in the CLI
13
+ export async function writeTokenAndDeviceCfg(props: { deviceUuid: string }) {
14
+ const { deviceUuid } = props;
15
+ await checkUserIsLoggedInComponent({ yes: true });
16
+ if (!(await checkPaidPlan())) {
17
+ throw new Error(
18
+ `This action only supported for Enterprise alwaysAI accounts!`
19
+ );
20
+ }
21
+ const { accessToken, refreshToken, idToken } = await microServiceHttpClient(
22
+ 'token-service',
23
+ 'create-device-tokens',
24
+ 'POST',
25
+ JSON.stringify({})
26
+ );
27
+
28
+ const tokens: DeviceTokens = {
29
+ deviceId: deviceUuid,
30
+ accessToken,
31
+ refreshToken,
32
+ idToken
33
+ };
34
+ const tokenSpawner = JsSpawner({ path: LOCAL_AAI_CFG_DIR });
35
+ await writeTokens({ spawner: tokenSpawner, tokens });
36
+ await writeOrValidateDeviceCfgFile({ spawner: tokenSpawner, deviceUuid });
37
+
38
+ logger.info('Updated tokens and certificate');
39
+ }
@@ -1,5 +1,5 @@
1
1
  import { CliTerseError } from '@alwaysai/alwayscli';
2
- import { getSystemId } from 'alwaysai/lib/infrastructure';
2
+ import { getSystemId } from './system-id';
3
3
 
4
4
  export function getIoTCoreEndpointUrl() {
5
5
  let iotCoreEndpointUrl = '';
@@ -15,7 +15,9 @@ export function getIoTCoreEndpointUrl() {
15
15
  iotCoreEndpointUrl = 'a3tzi5g7sq5zsj-ats.iot.us-west-2.amazonaws.com';
16
16
  break;
17
17
  default:
18
- throw new CliTerseError(`Remote deployment not supported on ${getSystemId()}!`);
18
+ throw new CliTerseError(
19
+ `Remote deployment not supported on ${getSystemId()}!`
20
+ );
19
21
  }
20
22
  return iotCoreEndpointUrl;
21
23
  }
package/src/root.ts CHANGED
@@ -1,18 +1,12 @@
1
+ // eslint-disable-next-line
1
2
  const winston = require('winston');
2
3
 
3
4
  import { CliBranch } from '@alwaysai/alwayscli';
4
5
 
5
6
  import { subcommands } from './subcommands';
6
- import { ALWAYSAI_LOG_LEVEL } from './environment';
7
-
8
- winston.configure({
9
- level: ALWAYSAI_LOG_LEVEL || 'info',
10
- transports: [new winston.transports.Console()],
11
- format: winston.format.simple(),
12
- });
13
7
 
14
8
  export const root = CliBranch({
15
9
  name: 'aai-agent',
16
10
  description: 'Manage your alwaysAI production device',
17
- subcommands,
11
+ subcommands
18
12
  });