agentworkshop 0.5.2 → 0.5.3

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.
@@ -10,7 +10,7 @@
10
10
  import { createLogger } from '../logger'
11
11
  import { randomUUID } from 'node:crypto'
12
12
  import fs from 'node:fs'
13
- import { join } from 'node:path'
13
+ import { dirname, join } from 'node:path'
14
14
  import { ensureDataDir } from '@/shared/config/home.mjs'
15
15
  import { AppError, ErrorCodes } from '../../../utils/errors'
16
16
 
@@ -148,7 +148,7 @@ export class AgentNodeBindingRepo {
148
148
 
149
149
  private flush(): void {
150
150
  try {
151
- fs.mkdirSync(path.dirname(DB_PATH), { recursive: true })
151
+ fs.mkdirSync(dirname(DB_PATH), { recursive: true })
152
152
  fs.writeFileSync(DB_PATH, JSON.stringify(this.list, null, 2), 'utf-8')
153
153
  }
154
154
  catch (err) {
@@ -38,16 +38,34 @@ export function findRepoRoot(startDir = process.cwd()) {
38
38
  }
39
39
  }
40
40
 
41
+ /** 从 cwd 向上找项目级配置根(<dir>/.AgentWorkShop 目录真实存在);找不到返回 null。
42
+ * 向上不超过用户主目录——~/.AgentWorkShop 是回退根,不能被误判为某项目的运行时根 */
43
+ export function findLocalConfigRoot(startDir = process.cwd()) {
44
+ const home = homedir()
45
+ let dir = resolve(startDir)
46
+ for (;;) {
47
+ const candidate = join(dir, HOME_DIRNAME)
48
+ if (dir !== home && existsSync(candidate) && statSync(candidate).isDirectory()) return candidate
49
+ const parent = dirname(dir)
50
+ if (parent === dir || dir === home) return null
51
+ dir = parent
52
+ }
53
+ }
54
+
41
55
  /**
42
56
  * 解析当前运行模式与全部路径。
43
- * 优先级:env.AW_MODE === 'home' 显式强制 home 模式(启动器对全局安装的
44
- * 应用载荷注入;否则包目录自身含 config.yml+nuxt.config.ts 会被误判为检出)
45
- * > cwd 向上找检出 > home 模式。
57
+ * 配置根优先级(用户规约:项目 ./.AgentWorkShop > ~/.AgentWorkShop):
58
+ * env.AW_MODE === 'home' 显式强制 home 模式(启动器对全局安装的应用载荷注入;
59
+ * 否则包目录自身含 config.yml+nuxt.config.ts 会被误判为检出)
60
+ * ② cwd 向上找到真实存在的 ./.AgentWorkShop 目录 → 项目运行时根(检出或纯工作区皆可)
61
+ * ③ cwd 向上找到项目检出(config.yml+nuxt.config.ts)但无 ./.AgentWorkShop
62
+ * → 按规约回退 ~/.AgentWorkShop 作为运行时根(工厂默认 config.yml 仍取检出根)
63
+ * ④ 其余任意目录 → home 模式(~/.AgentWorkShop,AW_HOME 可重定向)
46
64
  * @returns {{
47
65
  * mode: 'repo'|'home',
48
66
  * root: string, // repo=检出根; home=应用载荷根(本包)
49
67
  * home: string, // 用户级根 ~/.AgentWorkShop(AW_HOME 可重定向;用户级指令/全局配置)
50
- * configRoot: string, // 当前生效配置根(repo=<repo>/.AgentWorkShop; home=~/.AgentWorkShop)
68
+ * configRoot: string, // 当前生效配置根(项目运行时=<项目>/.AgentWorkShop; 否则 ~/.AgentWorkShop)
51
69
  * configPath: string, // 生效的 config.yml(工厂默认;repo 在检出根,home 在配置根)
52
70
  * settingsPath: string, // 生效的 runtime-settings.json(两种模式都在配置根)
53
71
  * dataDir: string, // 运行数据目录(两种模式都在配置根)
@@ -55,20 +73,44 @@ export function findRepoRoot(startDir = process.cwd()) {
55
73
  */
56
74
  export function resolveRunMode({ cwd, packageRoot, env = process.env } = {}) {
57
75
  const home = awHome(env)
76
+ const startDir = cwd ?? process.cwd()
58
77
  const forceHome = env.AW_MODE === 'home'
59
- const repoRoot = forceHome ? null : findRepoRoot(cwd ?? process.cwd())
78
+
79
+ // ② 项目级 ./.AgentWorkShop 显式存在 → 它就是运行时根(最高优先)
80
+ if (!forceHome) {
81
+ const localConfigRoot = findLocalConfigRoot(startDir)
82
+ if (localConfigRoot) {
83
+ const projectRoot = dirname(localConfigRoot)
84
+ const repoRoot = isRepoRoot(projectRoot) ? projectRoot : null
85
+ return {
86
+ mode: 'repo',
87
+ root: repoRoot ?? projectRoot,
88
+ home,
89
+ configRoot: localConfigRoot,
90
+ configPath: repoRoot
91
+ ? join(repoRoot, 'config.yml')
92
+ : (existsSync(join(localConfigRoot, 'config.yml')) ? join(localConfigRoot, 'config.yml') : join(packageRoot ?? '', 'config.yml')),
93
+ settingsPath: join(localConfigRoot, 'runtime-settings.json'),
94
+ dataDir: join(localConfigRoot, 'data'),
95
+ }
96
+ }
97
+ }
98
+
99
+ // ③ 检出内运行但无 ./.AgentWorkShop → 回退 ~/.AgentWorkShop(工厂默认配置仍在检出根)
100
+ const repoRoot = forceHome ? null : findRepoRoot(startDir)
60
101
  if (repoRoot) {
61
- const configRoot = join(repoRoot, HOME_DIRNAME)
62
102
  return {
63
103
  mode: 'repo',
64
104
  root: repoRoot,
65
105
  home,
66
- configRoot,
106
+ configRoot: home,
67
107
  configPath: join(repoRoot, 'config.yml'),
68
- settingsPath: join(configRoot, 'runtime-settings.json'),
69
- dataDir: join(configRoot, 'data'),
108
+ settingsPath: join(home, 'runtime-settings.json'),
109
+ dataDir: join(home, 'data'),
70
110
  }
71
111
  }
112
+
113
+ // ④ home 模式
72
114
  return {
73
115
  mode: 'home',
74
116
  root: packageRoot ?? repoRoot,
@@ -82,16 +124,16 @@ export function resolveRunMode({ cwd, packageRoot, env = process.env } = {}) {
82
124
 
83
125
  /**
84
126
  * 运行数据目录(服务端/CLI 通用):
85
- * AW_DATA_DIR 环境变量 > home 模式(AW_MODE=home) 配置根/data
86
- * > repo 模式(cwd config.yml) cwd/.AgentWorkShop/data > cwd/data 兜底。
127
+ * AW_DATA_DIR 环境变量 > resolveRunMode 的配置根/data
128
+ * (项目 ./.AgentWorkShop 显式存在 > 检出内无 .AgentWorkShop 回退 ~/.AgentWorkShop
129
+ * > home 模式 ~/.AgentWorkShop/data > cwd/data 兜底)。
87
130
  * 启动器(scripts/start.mjs / dev-guard.mjs)会经 resolveRunMode 注入 AW_DATA_DIR;
88
131
  * 本函数为其兜底(直接 node .output/server/index.mjs 等无启动器场景)。
89
132
  */
90
133
  export function dataDirFor(cwd = process.cwd(), env = process.env) {
91
134
  if (env.AW_DATA_DIR && String(env.AW_DATA_DIR).trim()) return resolve(String(env.AW_DATA_DIR).trim())
92
- if (env.AW_MODE === 'home') return join(awHome(env), 'data')
93
- if (existsSync(join(cwd, 'config.yml'))) return join(cwd, HOME_DIRNAME, 'data')
94
- return join(cwd, 'data')
135
+ const rm = resolveRunMode({ cwd, env })
136
+ return rm.dataDir
95
137
  }
96
138
 
97
139
  /**
@@ -102,7 +144,9 @@ export function dataDirFor(cwd = process.cwd(), env = process.env) {
102
144
  export function ensureDataDir(cwd = process.cwd(), env = process.env) {
103
145
  const dir = dataDirFor(cwd, env)
104
146
  mkdirSync(dir, { recursive: true })
105
- if (env.AW_MODE === 'home') return dir
147
+ // 旧位置迁移仅当配置根就在 cwd 内(本地 ./.AgentWorkShop);回退 ~/.AgentWorkShop
148
+ // 时不得把检出仓库的旧数据搬进用户全局根(跨项目污染)
149
+ if (env.AW_MODE === 'home' || !resolve(dir).startsWith(resolve(cwd))) return dir
106
150
  const legacyDirs = [join(cwd, 'data'), join(cwd, 'server', 'data')]
107
151
  for (const legacy of legacyDirs) {
108
152
  if (resolve(legacy) === resolve(dir) || !existsSync(legacy)) continue
@@ -140,6 +184,7 @@ export default {
140
184
  awHome,
141
185
  isRepoRoot,
142
186
  findRepoRoot,
187
+ findLocalConfigRoot,
143
188
  resolveRunMode,
144
189
  dataDirFor,
145
190
  ensureDataDir,
@@ -1 +0,0 @@
1
- {"id":"eede1532-1366-486a-bdab-4d27fa03a1d8","timestamp":1788336798647,"prerendered":[]}