@surph_ai/cli 0.0.5 → 0.0.6

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/cli.js CHANGED
@@ -17,7 +17,7 @@ const AdmZip = require('adm-zip')
17
17
  // const auth = require('./auth')
18
18
  const deploy = require('./deploy')
19
19
  const scaffold = require('./scaffold')
20
- const { ensureChat } = require('./toolserver/ensure-chat')
20
+ const { ensureChat, chatNodePath } = require('./toolserver/ensure-chat')
21
21
  const utils = require('./utils')
22
22
  const packageJson = require('./package.json')
23
23
 
@@ -166,6 +166,7 @@ program.command('chatserver')
166
166
  cliVersion: VERSION,
167
167
  log: (msg) => console.log(colors.cyan(`\n${msg}\n`))
168
168
  })
169
+ process.env.NODE_PATH = chatNodePath()
169
170
  shell.exec(`nodemon --quiet "${chatPath}"`)
170
171
  } catch (error) {
171
172
  utils.printError(error)
package/index.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@surph_ai/cli",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "./types.d.ts",
@@ -1,15 +1,29 @@
1
1
  const crypto = require('crypto')
2
2
  const fs = require('fs')
3
- const os = require('os')
4
3
  const path = require('path')
5
4
 
6
5
  const MANIFEST_URL = 'https://surph.s3.us-east-1.amazonaws.com/tools/cli/manifest.json'
7
6
  const CHAT_URL = 'https://surph.s3.us-east-1.amazonaws.com/tools/cli/chat.js'
8
- const CACHE_DIR = path.join(os.homedir(), '.surph', 'cache')
9
- const CACHE_FILE = path.join(CACHE_DIR, 'chat.js')
10
- const CACHE_META = path.join(CACHE_DIR, 'chat.meta.json')
7
+ const CLI_NODE_MODULES = path.join(__dirname, '..', 'node_modules')
11
8
  const BUNDLED_FALLBACK = path.join(__dirname, 'chat.js')
12
9
 
10
+ const cachePaths = () => {
11
+ const cacheDir = path.join(process.cwd(), '.surph', 'cache')
12
+ return {
13
+ cacheDir,
14
+ cacheFile: path.join(cacheDir, 'chat.cjs'),
15
+ cacheMeta: path.join(cacheDir, 'chat.meta.json')
16
+ }
17
+ }
18
+
19
+ const chatNodePath = () => {
20
+ const dirs = [
21
+ CLI_NODE_MODULES,
22
+ path.join(process.cwd(), 'node_modules')
23
+ ].filter((dir) => fs.existsSync(dir))
24
+ return [...dirs, process.env.NODE_PATH].filter(Boolean).join(path.delimiter)
25
+ }
26
+
13
27
  const parseVersion = (value) => String(value || '0').split('.').map((part) => {
14
28
  const n = parseInt(part, 10)
15
29
  return Number.isFinite(n) ? n : 0
@@ -39,11 +53,12 @@ const readJsonFile = (filepath) => {
39
53
  }
40
54
 
41
55
  const cacheMatches = (manifest) => {
42
- const meta = readJsonFile(CACHE_META)
56
+ const { cacheFile, cacheMeta } = cachePaths()
57
+ const meta = readJsonFile(cacheMeta)
43
58
  if (!meta || meta.sha256 !== manifest.sha256) return false
44
- if (!fs.existsSync(CACHE_FILE)) return false
59
+ if (!fs.existsSync(cacheFile)) return false
45
60
  if (manifest.size) {
46
- const { size } = fs.statSync(CACHE_FILE)
61
+ const { size } = fs.statSync(cacheFile)
47
62
  if (size !== manifest.size) return false
48
63
  }
49
64
  return true
@@ -80,15 +95,17 @@ const downloadChat = async (manifest) => {
80
95
  throw new Error('Chat server checksum mismatch')
81
96
  }
82
97
 
83
- fs.mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 })
84
- const tmp = `${CACHE_FILE}.tmp`
98
+ const { cacheDir, cacheFile, cacheMeta } = cachePaths()
99
+ fs.mkdirSync(cacheDir, { recursive: true, mode: 0o700 })
100
+ const tmp = `${cacheFile}.tmp`
85
101
  fs.writeFileSync(tmp, buf, { mode: 0o600 })
86
- fs.renameSync(tmp, CACHE_FILE)
87
- fs.writeFileSync(CACHE_META, JSON.stringify(manifest, null, 2) + '\n', { mode: 0o600 })
102
+ fs.renameSync(tmp, cacheFile)
103
+ fs.writeFileSync(cacheMeta, JSON.stringify(manifest, null, 2) + '\n', { mode: 0o600 })
88
104
  }
89
105
 
90
106
  const fallbackPath = () => {
91
- if (fs.existsSync(CACHE_FILE)) return CACHE_FILE
107
+ const { cacheFile } = cachePaths()
108
+ if (fs.existsSync(cacheFile)) return cacheFile
92
109
  if (fs.existsSync(BUNDLED_FALLBACK)) return BUNDLED_FALLBACK
93
110
  return null
94
111
  }
@@ -112,14 +129,15 @@ const ensureChat = async ({ cliVersion, log } = {}) => {
112
129
  )
113
130
  }
114
131
 
132
+ const { cacheFile } = cachePaths()
115
133
  if (cacheMatches(manifest)) {
116
- return CACHE_FILE
134
+ return cacheFile
117
135
  }
118
136
 
119
137
  if (log) log('Downloading latest chat server...')
120
138
  try {
121
139
  await downloadChat(manifest)
122
- return CACHE_FILE
140
+ return cacheFile
123
141
  } catch (error) {
124
142
  const fallback = fallbackPath()
125
143
  if (fallback) {
@@ -130,4 +148,4 @@ const ensureChat = async ({ cliVersion, log } = {}) => {
130
148
  }
131
149
  }
132
150
 
133
- module.exports = { ensureChat }
151
+ module.exports = { ensureChat, chatNodePath }