@alwaysai/device-agent 0.0.10 → 0.0.11-internal-qa-2

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 (59) hide show
  1. package/lib/application-control/backup.d.ts.map +1 -1
  2. package/lib/application-control/backup.js +4 -3
  3. package/lib/application-control/backup.js.map +1 -1
  4. package/lib/application-control/install.d.ts.map +1 -1
  5. package/lib/application-control/install.js +6 -5
  6. package/lib/application-control/install.js.map +1 -1
  7. package/lib/application-control/models.d.ts.map +1 -1
  8. package/lib/application-control/models.js +3 -2
  9. package/lib/application-control/models.js.map +1 -1
  10. package/lib/application-control/status.d.ts.map +1 -1
  11. package/lib/application-control/status.js +6 -5
  12. package/lib/application-control/status.js.map +1 -1
  13. package/lib/application-control/utils.d.ts.map +1 -1
  14. package/lib/application-control/utils.js +3 -2
  15. package/lib/application-control/utils.js.map +1 -1
  16. package/lib/cloud-connection/device-agent-cloud-connection.d.ts +0 -1
  17. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  18. package/lib/cloud-connection/device-agent-cloud-connection.js +20 -21
  19. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  20. package/lib/environment.d.ts +1 -0
  21. package/lib/environment.d.ts.map +1 -1
  22. package/lib/environment.js +2 -1
  23. package/lib/environment.js.map +1 -1
  24. package/lib/index.js +2 -1
  25. package/lib/index.js.map +1 -1
  26. package/lib/infrastructure/agent-config.d.ts.map +1 -1
  27. package/lib/infrastructure/agent-config.js +8 -4
  28. package/lib/infrastructure/agent-config.js.map +1 -1
  29. package/lib/infrastructure/certificates-and-tokens.d.ts.map +1 -1
  30. package/lib/infrastructure/certificates-and-tokens.js +3 -2
  31. package/lib/infrastructure/certificates-and-tokens.js.map +1 -1
  32. package/lib/subcommands/app/app.d.ts.map +1 -1
  33. package/lib/subcommands/app/app.js +6 -5
  34. package/lib/subcommands/app/app.js.map +1 -1
  35. package/lib/subcommands/device/device.d.ts.map +1 -1
  36. package/lib/subcommands/device/device.js +3 -2
  37. package/lib/subcommands/device/device.js.map +1 -1
  38. package/lib/subcommands/get-model-package.d.ts.map +1 -1
  39. package/lib/subcommands/get-model-package.js +2 -1
  40. package/lib/subcommands/get-model-package.js.map +1 -1
  41. package/lib/util/logger.d.ts +4 -0
  42. package/lib/util/logger.d.ts.map +1 -0
  43. package/lib/util/logger.js +25 -0
  44. package/lib/util/logger.js.map +1 -0
  45. package/package.json +2 -2
  46. package/src/application-control/backup.ts +4 -3
  47. package/src/application-control/install.ts +6 -5
  48. package/src/application-control/models.ts +4 -3
  49. package/src/application-control/status.ts +6 -5
  50. package/src/application-control/utils.ts +3 -2
  51. package/src/cloud-connection/device-agent-cloud-connection.ts +15 -12
  52. package/src/environment.ts +1 -0
  53. package/src/index.ts +2 -1
  54. package/src/infrastructure/agent-config.ts +8 -4
  55. package/src/infrastructure/certificates-and-tokens.ts +3 -2
  56. package/src/subcommands/app/app.ts +6 -5
  57. package/src/subcommands/device/device.ts +3 -2
  58. package/src/subcommands/get-model-package.ts +2 -1
  59. package/src/util/logger.ts +28 -0
package/src/index.ts CHANGED
@@ -10,9 +10,10 @@ import { root } from './root';
10
10
  import { runDeviceAgentCloudInterface } from './cloud-connection/device-agent-cloud-connection';
11
11
  import { AgentConfigFile } from './infrastructure/agent-config';
12
12
  import { ALWAYSAI_DEVICE_AGENT_MODE } from './environment';
13
+ import { logger } from './util/logger';
13
14
 
14
15
  if (module === require.main) {
15
- console.log('Starting alwaysAI Device Agent');
16
+ logger.info('Starting alwaysAI Device Agent');
16
17
  if (!AgentConfigFile().exists()) {
17
18
  AgentConfigFile().initialize();
18
19
  }
@@ -82,14 +82,16 @@ export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
82
82
 
83
83
  async function getApps() {
84
84
  const config = configFile.read();
85
- return config.applications;
85
+ return config && config.applications ? config.applications : [];
86
86
  }
87
87
 
88
88
  async function getReadyApps() {
89
89
  const config = configFile.read();
90
- return config.applications.filter((app) => {
91
- return app.ready;
92
- });
90
+ return config && config.applications
91
+ ? config.applications.filter((app) => {
92
+ return app.ready;
93
+ })
94
+ : [];
93
95
  }
94
96
 
95
97
  async function getApp(props: { projectId: string }) {
@@ -124,6 +126,8 @@ export function AgentConfigFile(dir = ALWAYSAI_CONFIG_DIR) {
124
126
  async function removeApp(props: { projectId: string }) {
125
127
  const { projectId } = props;
126
128
  const config = configFile.read();
129
+ if (!config) return;
130
+
127
131
  const newList = config.applications.filter((app) => {
128
132
  return app.projectId !== projectId;
129
133
  });
@@ -9,6 +9,7 @@ import {
9
9
  refreshDevice,
10
10
  } from 'alwaysai/lib/infrastructure';
11
11
  import { JsSpawner, Spawner, writeCertificate, writeTokens } from 'alwaysai/lib/util';
12
+ import { logger } from '../util/logger';
12
13
 
13
14
  // NOTE: This is reimplemented from the CLI to work for local checks
14
15
  export async function getTargetHardwareUuid(spawner: Spawner) {
@@ -56,7 +57,7 @@ export async function writeCertificateAndToken(props: { deviceUuid: string }) {
56
57
  let iotKeys = device.iot_keys;
57
58
  if (!iotKeys) {
58
59
  // there are no certificates, we need to generate new ones
59
- console.log('Generating new certificate');
60
+ logger.info('Generating new certificate');
60
61
  const response = await getDeviceCertificates(device.uuid, device.mode);
61
62
  iotKeys = response.iotKeys;
62
63
  }
@@ -67,5 +68,5 @@ export async function writeCertificateAndToken(props: { deviceUuid: string }) {
67
68
  privateKey: iotKeys.keyPair.PrivateKey,
68
69
  rootCa: iotKeys.rootCA,
69
70
  });
70
- console.log('Updated tokens and certificate');
71
+ logger.info('Updated tokens and certificate');
71
72
  }
@@ -24,6 +24,7 @@ import {
24
24
  updateModels,
25
25
  } from '../../application-control';
26
26
  import { AgentConfigFile } from '../../infrastructure/agent-config';
27
+ import { logger } from '../../util/logger';
27
28
 
28
29
  export const listAppsCliLeaf = CliLeaf({
29
30
  name: 'list',
@@ -47,7 +48,7 @@ export const listAppReleasesCliLeaf = CliLeaf({
47
48
  async action(_, opts) {
48
49
  const { project } = opts;
49
50
  const releaseHistory = await listAppReleases({ projectId: project });
50
- console.log(releaseHistory);
51
+ logger.info(releaseHistory);
51
52
  },
52
53
  });
53
54
 
@@ -66,7 +67,7 @@ export const listAppLatestReleaseCliLeaf = CliLeaf({
66
67
  if (latestReleaseHash === undefined) {
67
68
  throw new CliTerseError('This application has not been published yet');
68
69
  }
69
- console.log(latestReleaseHash);
70
+ logger.info(latestReleaseHash);
70
71
  },
71
72
  });
72
73
 
@@ -107,7 +108,7 @@ export const getAppStatusCliLeaf = CliLeaf({
107
108
  async action(_, opts) {
108
109
  const { project } = opts;
109
110
  const appStatus = await getAppStatus({ projectId: project });
110
- console.log(appStatus);
111
+ logger.info(appStatus);
111
112
  },
112
113
  });
113
114
 
@@ -143,7 +144,7 @@ export const getAppLogsCliLeaf = CliLeaf({
143
144
  const readable = await getAppLogs({ projectId: project });
144
145
  readable.setEncoding('utf8');
145
146
  for await (const chunk of readable) {
146
- console.log(chunk);
147
+ logger.info(chunk);
147
148
  }
148
149
  },
149
150
  });
@@ -307,7 +308,7 @@ export const getAllEnvsCLiLeaf = CliLeaf({
307
308
  },
308
309
  async action(_, opts) {
309
310
  const { project } = opts;
310
- console.log(await getAllEnvs({ projectId: project }));
311
+ logger.info(await getAllEnvs({ projectId: project }));
311
312
  },
312
313
  });
313
314
 
@@ -8,6 +8,7 @@ import {
8
8
  getTargetHardwareUuid,
9
9
  writeCertificateAndToken,
10
10
  } from '../../infrastructure/certificates-and-tokens';
11
+ import { logger } from '../../util/logger';
11
12
 
12
13
  export const initCliLeaf = CliLeaf({
13
14
  name: 'init',
@@ -24,7 +25,7 @@ export const initCliLeaf = CliLeaf({
24
25
  },
25
26
  async action(_, opts) {
26
27
  const { name, description } = opts;
27
- console.log('Initializing device');
28
+ logger.info('Initializing device');
28
29
  await checkUserIsLoggedInComponent({ yes: true });
29
30
  if (!(await checkPaidPlan())) {
30
31
  throw new Error(`This action only supported for Enterprise alwaysAI accounts!`);
@@ -44,7 +45,7 @@ export const initCliLeaf = CliLeaf({
44
45
  };
45
46
  const response = await addDevice(deviceToAdd, 'production');
46
47
  await writeCertificateAndToken({ deviceUuid: response.deviceUUID });
47
- console.log(`Initialized device as ${response.deviceUUID}`);
48
+ logger.info(`Initialized device as ${response.deviceUUID}`);
48
49
  },
49
50
  });
50
51
 
@@ -2,6 +2,7 @@ import { CliLeaf, CliNumberInput, CliStringInput } from '@alwaysai/alwayscli';
2
2
  import { appInstallModel } from 'alwaysai/lib/core/app';
3
3
  import { CliRpcClient } from 'alwaysai/lib/infrastructure';
4
4
  import { JsSpawner } from 'alwaysai/lib/util';
5
+ import { logger } from '../util/logger';
5
6
 
6
7
  export const getModelPackageCliLeaf = CliLeaf({
7
8
  name: 'get-model-package',
@@ -27,6 +28,6 @@ export const getModelPackageCliLeaf = CliLeaf({
27
28
  const version =
28
29
  opts.version || (await CliRpcClient().getModelVersion({ id })).version;
29
30
  await appInstallModel(spawner, id, version);
30
- console.log(`Completed downloading ${id} to ${path}`);
31
+ logger.info(`Completed downloading ${id} to ${path}`);
31
32
  },
32
33
  });
@@ -0,0 +1,28 @@
1
+ import * as winston from 'winston';
2
+ import 'winston-daily-rotate-file';
3
+ import * as path from 'path';
4
+ import { AAI_DIR } from 'alwaysai/lib/constants';
5
+ import { ALWAYSAI_LOG_LEVEL, ALWAYSAI_LOG_TO_CONSOLE } from '../environment';
6
+
7
+ const LOG_LEVEL = ALWAYSAI_LOG_LEVEL || 'info';
8
+ const transports = ALWAYSAI_LOG_TO_CONSOLE
9
+ ? [new winston.transports.Console({ level: LOG_LEVEL })]
10
+ : [
11
+ new winston.transports.DailyRotateFile({
12
+ filename: path.join(AAI_DIR, 'agent-logs', 'agent-logs.txt'),
13
+ maxSize: '5m',
14
+ maxFiles: '2d',
15
+ }),
16
+ ];
17
+
18
+ export const logger = winston.createLogger({
19
+ level: LOG_LEVEL,
20
+ format: winston.format.combine(
21
+ winston.format.errors({ stack: true }),
22
+ winston.format.splat(),
23
+ winston.format.simple(),
24
+ ),
25
+ transports,
26
+ });
27
+
28
+ logger.info(`Initialized logger with log level: ${LOG_LEVEL}`);