agent-gear 2.8.0 → 2.10.0
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/AGENTS.md +35 -5
- package/dist/acp/bridge.d.ts +29 -1
- package/dist/acp/bridge.d.ts.map +1 -1
- package/dist/acp/bridge.js +171 -27
- package/dist/acp/bridge.js.map +1 -1
- package/dist/acp/bridge.test.js +198 -15
- package/dist/acp/bridge.test.js.map +1 -1
- package/dist/acp/detector.d.ts +2 -0
- package/dist/acp/detector.d.ts.map +1 -1
- package/dist/acp/detector.js +18 -3
- package/dist/acp/detector.js.map +1 -1
- package/dist/acp/detector.test.d.ts +2 -0
- package/dist/acp/detector.test.d.ts.map +1 -0
- package/dist/acp/detector.test.js +50 -0
- package/dist/acp/detector.test.js.map +1 -0
- package/dist/acp/initial-config.integration.test.d.ts +2 -0
- package/dist/acp/initial-config.integration.test.d.ts.map +1 -0
- package/dist/acp/initial-config.integration.test.js +95 -0
- package/dist/acp/initial-config.integration.test.js.map +1 -0
- package/dist/cloud-host/supervisor.d.ts +8 -19
- package/dist/cloud-host/supervisor.d.ts.map +1 -1
- package/dist/cloud-host/supervisor.js +7 -27
- package/dist/cloud-host/supervisor.js.map +1 -1
- package/dist/cloud-host/supervisor.test.js +12 -6
- package/dist/cloud-host/supervisor.test.js.map +1 -1
- package/dist/commands/harness.d.ts +5 -1
- package/dist/commands/harness.d.ts.map +1 -1
- package/dist/commands/harness.js +7 -3
- package/dist/commands/harness.js.map +1 -1
- package/dist/exit-codes.d.ts +30 -0
- package/dist/exit-codes.d.ts.map +1 -0
- package/dist/exit-codes.js +52 -0
- package/dist/exit-codes.js.map +1 -0
- package/dist/exit-codes.test.d.ts +2 -0
- package/dist/exit-codes.test.d.ts.map +1 -0
- package/dist/exit-codes.test.js +61 -0
- package/dist/exit-codes.test.js.map +1 -0
- package/dist/harness/dsh-cli.d.ts +17 -0
- package/dist/harness/dsh-cli.d.ts.map +1 -0
- package/dist/harness/dsh-cli.js +51 -0
- package/dist/harness/dsh-cli.js.map +1 -0
- package/dist/harness/dsh-cli.test.d.ts +2 -0
- package/dist/harness/dsh-cli.test.d.ts.map +1 -0
- package/dist/harness/dsh-cli.test.js +51 -0
- package/dist/harness/dsh-cli.test.js.map +1 -0
- package/dist/harness/dsh-config.atomic-failure.test.d.ts +2 -0
- package/dist/harness/dsh-config.atomic-failure.test.d.ts.map +1 -0
- package/dist/harness/dsh-config.atomic-failure.test.js +35 -0
- package/dist/harness/dsh-config.atomic-failure.test.js.map +1 -0
- package/dist/harness/dsh-config.d.ts +46 -0
- package/dist/harness/dsh-config.d.ts.map +1 -0
- package/dist/harness/dsh-config.js +228 -0
- package/dist/harness/dsh-config.js.map +1 -0
- package/dist/harness/dsh-config.test.d.ts +2 -0
- package/dist/harness/dsh-config.test.d.ts.map +1 -0
- package/dist/harness/dsh-config.test.js +229 -0
- package/dist/harness/dsh-config.test.js.map +1 -0
- package/dist/harness/env.d.ts +11 -0
- package/dist/harness/env.d.ts.map +1 -1
- package/dist/harness/env.js +23 -0
- package/dist/harness/env.js.map +1 -1
- package/dist/harness/env.test.js +25 -3
- package/dist/harness/env.test.js.map +1 -1
- package/dist/harness/opencode-cli.d.ts +5 -0
- package/dist/harness/opencode-cli.d.ts.map +1 -1
- package/dist/harness/opencode-cli.js +8 -1
- package/dist/harness/opencode-cli.js.map +1 -1
- package/dist/harness/prepare.d.ts +27 -9
- package/dist/harness/prepare.d.ts.map +1 -1
- package/dist/harness/prepare.js +67 -23
- package/dist/harness/prepare.js.map +1 -1
- package/dist/harness/prepare.test.js +112 -5
- package/dist/harness/prepare.test.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/socketio-client.auth-gate.test.js +3 -3
- package/dist/socketio-client.auth-gate.test.js.map +1 -1
- package/dist/socketio-client.d.ts.map +1 -1
- package/dist/socketio-client.js +27 -3
- package/dist/socketio-client.js.map +1 -1
- package/dist/socketio-client.test.js +37 -0
- package/dist/socketio-client.test.js.map +1 -1
- package/dist/workload-executor.test.js +2 -2
- package/dist/workload-executor.test.js.map +1 -1
- package/package.json +3 -2
- package/src/acp/__fixtures__/fake-initial-config-agent.mjs +113 -0
- package/src/acp/bridge.test.ts +223 -15
- package/src/acp/bridge.ts +188 -27
- package/src/acp/detector.test.ts +64 -0
- package/src/acp/detector.ts +18 -3
- package/src/acp/initial-config.integration.test.ts +120 -0
- package/src/cloud-host/supervisor.test.ts +15 -6
- package/src/cloud-host/supervisor.ts +22 -35
- package/src/commands/harness.ts +9 -3
- package/src/exit-codes.test.ts +73 -0
- package/src/exit-codes.ts +59 -0
- package/src/harness/dsh-cli.test.ts +58 -0
- package/src/harness/dsh-cli.ts +67 -0
- package/src/harness/dsh-config.atomic-failure.test.ts +51 -0
- package/src/harness/dsh-config.test.ts +275 -0
- package/src/harness/dsh-config.ts +280 -0
- package/src/harness/env.test.ts +34 -3
- package/src/harness/env.ts +26 -0
- package/src/harness/opencode-cli.ts +9 -2
- package/src/harness/prepare.test.ts +131 -5
- package/src/harness/prepare.ts +103 -27
- package/src/index.ts +7 -0
- package/src/socketio-client.auth-gate.test.ts +3 -3
- package/src/socketio-client.test.ts +44 -0
- package/src/socketio-client.ts +27 -3
- package/src/workload-executor.test.ts +2 -2
- package/tests/acp-detector.test.ts +28 -8
- package/tests/commands/harness.test.ts +18 -8
- package/tests/socketio-client.test.ts +4 -4
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from 'vitest';
|
|
2
|
+
import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { parse } from 'yaml';
|
|
5
|
+
import { DshConfigError, defaultDshHome, writeDshConfig } from './dsh-config.js';
|
|
6
|
+
|
|
7
|
+
const TMP_BASE = '/tmp/opencode';
|
|
8
|
+
const API_KEY = 'zag_sk_dsh_super_secret';
|
|
9
|
+
|
|
10
|
+
const ENV = {
|
|
11
|
+
modelBaseUrl: 'http://admin-api:8002/v1',
|
|
12
|
+
modelApiKey: API_KEY,
|
|
13
|
+
modelIds: ['model-a', 'model-b'],
|
|
14
|
+
modelContextLengths: { 'model-a': 128000 },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
async function makeHome(): Promise<string> {
|
|
18
|
+
await mkdir(TMP_BASE, { recursive: true });
|
|
19
|
+
return mkdtemp(join(TMP_BASE, 'dsh-'));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function readYaml(path: string): Promise<unknown> {
|
|
23
|
+
return parse(await readFile(path, 'utf-8'));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe('writeDshConfig (T-DSH*)', () => {
|
|
27
|
+
const dirs: string[] = [];
|
|
28
|
+
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('defaultDshHome 优先 DSH_HOME,否则 ~/.dsh', () => {
|
|
34
|
+
expect(defaultDshHome({ DSH_HOME: ' /custom/dsh ' })).toBe('/custom/dsh');
|
|
35
|
+
expect(defaultDshHome({})).toMatch(/\/\.dsh$/);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('T-DSH1: 新建 patch 写入 4 个条目与 ZAG provider,并新建 credentials', async () => {
|
|
39
|
+
const home = await makeHome();
|
|
40
|
+
dirs.push(home);
|
|
41
|
+
|
|
42
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
43
|
+
|
|
44
|
+
expect(result).toMatchObject({
|
|
45
|
+
patchCreated: true,
|
|
46
|
+
credentialsCreated: true,
|
|
47
|
+
modelCount: 2,
|
|
48
|
+
entryCount: 4,
|
|
49
|
+
});
|
|
50
|
+
expect(result.patchPath).toBe(join(home, 'profiles', 'acp', 'cordis.patch.yml'));
|
|
51
|
+
expect(result.credentialsPath).toBe(join(home, '.credentials.yaml'));
|
|
52
|
+
|
|
53
|
+
const patch = (await readYaml(result.patchPath)) as Array<Record<string, any>>;
|
|
54
|
+
expect(patch.map((entry) => entry.id)).toEqual([
|
|
55
|
+
'llm-deepseek',
|
|
56
|
+
'llm-pi-ai',
|
|
57
|
+
'agent-default-model',
|
|
58
|
+
'acp',
|
|
59
|
+
]);
|
|
60
|
+
expect(patch[0]).toEqual({ id: 'llm-deepseek', disabled: true });
|
|
61
|
+
expect(patch[1].config.providers.zag).toMatchObject({
|
|
62
|
+
displayName: 'ZAG',
|
|
63
|
+
apiKeyEnv: 'ZAG_MODEL_API_KEY',
|
|
64
|
+
api: 'openai-completions',
|
|
65
|
+
baseURL: 'http://admin-api:8002/v1',
|
|
66
|
+
models: [
|
|
67
|
+
{ id: 'model-a', name: 'model-a', contextWindow: 128000 },
|
|
68
|
+
{ id: 'model-b', name: 'model-b' },
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
expect(patch[2]).toEqual({
|
|
72
|
+
id: 'agent-default-model',
|
|
73
|
+
config: { provider: 'zag', model: 'model-a' },
|
|
74
|
+
});
|
|
75
|
+
expect(patch[3]).toEqual({
|
|
76
|
+
id: 'acp',
|
|
77
|
+
name: '@deepseek-ai/dsh-acp',
|
|
78
|
+
config: { provider: 'zag', model: 'model-a' },
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const credentials = (await readYaml(result.credentialsPath)) as Record<string, any>;
|
|
82
|
+
expect(credentials).toEqual({ version: 1, refs: { ZAG_MODEL_API_KEY: API_KEY } });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('T-DSH2/T-DSH3: 合并时只替换我方条目(models 整体替换),其他条目与注释保留', async () => {
|
|
86
|
+
const home = await makeHome();
|
|
87
|
+
dirs.push(home);
|
|
88
|
+
const profilesDir = join(home, 'profiles', 'acp');
|
|
89
|
+
await mkdir(profilesDir, { recursive: true });
|
|
90
|
+
const patchPath = join(profilesDir, 'cordis.patch.yml');
|
|
91
|
+
await writeFile(
|
|
92
|
+
patchPath,
|
|
93
|
+
[
|
|
94
|
+
'# top comment',
|
|
95
|
+
'- id: other',
|
|
96
|
+
' value: 1 # keep inline',
|
|
97
|
+
'- id: llm-pi-ai',
|
|
98
|
+
' config:',
|
|
99
|
+
' providers:',
|
|
100
|
+
' zag:',
|
|
101
|
+
' models:',
|
|
102
|
+
' - id: old-model',
|
|
103
|
+
'- id: agent-default-model',
|
|
104
|
+
' config: { provider: old, model: old-model }',
|
|
105
|
+
'',
|
|
106
|
+
].join('\n'),
|
|
107
|
+
'utf-8',
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
await writeDshConfig({ env: ENV, dshHome: home });
|
|
111
|
+
|
|
112
|
+
const text = await readFile(patchPath, 'utf-8');
|
|
113
|
+
expect(text).toContain('# top comment');
|
|
114
|
+
expect(text).toContain('value: 1 # keep inline');
|
|
115
|
+
const patch = parse(text) as Array<Record<string, any>>;
|
|
116
|
+
expect(patch[0]).toEqual({ id: 'other', value: 1 });
|
|
117
|
+
const zag = patch.find((entry) => entry.id === 'llm-pi-ai')!.config.providers.zag;
|
|
118
|
+
expect(zag.models.map((model: { id: string }) => model.id)).toEqual(['model-a', 'model-b']);
|
|
119
|
+
expect(patch.filter((entry) => entry.id === 'llm-pi-ai')).toHaveLength(1);
|
|
120
|
+
expect(patch.filter((entry) => entry.id === 'llm-deepseek')).toHaveLength(1);
|
|
121
|
+
expect(patch.filter((entry) => entry.id === 'acp')).toHaveLength(1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('T-DSH4: contextWindow 仅在正整数时写入', async () => {
|
|
125
|
+
const home = await makeHome();
|
|
126
|
+
dirs.push(home);
|
|
127
|
+
|
|
128
|
+
await writeDshConfig({
|
|
129
|
+
env: {
|
|
130
|
+
...ENV,
|
|
131
|
+
modelContextLengths: {
|
|
132
|
+
'model-a': 0,
|
|
133
|
+
'model-b': -1,
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
dshHome: home,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const patch = (await readYaml(join(home, 'profiles', 'acp', 'cordis.patch.yml'))) as Array<
|
|
140
|
+
Record<string, any>
|
|
141
|
+
>;
|
|
142
|
+
const models = patch.find((entry) => entry.id === 'llm-pi-ai')!.config.providers.zag.models;
|
|
143
|
+
expect(models).toEqual([
|
|
144
|
+
{ id: 'model-a', name: 'model-a' },
|
|
145
|
+
{ id: 'model-b', name: 'model-b' },
|
|
146
|
+
]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('T-DSH5: credentials 合并只替换 refs.ZAG_MODEL_API_KEY,保留其它 refs/records 与注释', async () => {
|
|
150
|
+
const home = await makeHome();
|
|
151
|
+
dirs.push(home);
|
|
152
|
+
await mkdir(home, { recursive: true });
|
|
153
|
+
const credentialsPath = join(home, '.credentials.yaml');
|
|
154
|
+
await writeFile(
|
|
155
|
+
credentialsPath,
|
|
156
|
+
[
|
|
157
|
+
'# credentials comment',
|
|
158
|
+
'version: 1',
|
|
159
|
+
'refs:',
|
|
160
|
+
' OTHER: keep-me',
|
|
161
|
+
'records:',
|
|
162
|
+
' github: token # keep record',
|
|
163
|
+
'',
|
|
164
|
+
].join('\n'),
|
|
165
|
+
'utf-8',
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
169
|
+
|
|
170
|
+
expect(result.credentialsCreated).toBe(false);
|
|
171
|
+
const text = await readFile(credentialsPath, 'utf-8');
|
|
172
|
+
expect(text).toContain('# credentials comment');
|
|
173
|
+
expect(text).toContain('token # keep record');
|
|
174
|
+
const credentials = parse(text) as Record<string, any>;
|
|
175
|
+
expect(credentials.version).toBe(1);
|
|
176
|
+
expect(credentials.refs).toEqual({ OTHER: 'keep-me', ZAG_MODEL_API_KEY: API_KEY });
|
|
177
|
+
expect(credentials.records).toEqual({ github: 'token' });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('T-DSH6: 目录 0700、两文件 0600', async () => {
|
|
181
|
+
const home = await makeHome();
|
|
182
|
+
dirs.push(home);
|
|
183
|
+
|
|
184
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
185
|
+
|
|
186
|
+
expect((await stat(join(home, 'profiles', 'acp'))).mode & 0o777).toBe(0o700);
|
|
187
|
+
expect((await stat(result.patchPath)).mode & 0o777).toBe(0o600);
|
|
188
|
+
expect((await stat(result.credentialsPath)).mode & 0o777).toBe(0o600);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('T-DSH8: 非法 YAML 抛错且不覆盖既有文件', async () => {
|
|
192
|
+
const home = await makeHome();
|
|
193
|
+
dirs.push(home);
|
|
194
|
+
const profilesDir = join(home, 'profiles', 'acp');
|
|
195
|
+
await mkdir(profilesDir, { recursive: true });
|
|
196
|
+
const patchPath = join(profilesDir, 'cordis.patch.yml');
|
|
197
|
+
const broken = '- id: broken\n config: [unclosed\n';
|
|
198
|
+
await writeFile(patchPath, broken, 'utf-8');
|
|
199
|
+
|
|
200
|
+
await expect(writeDshConfig({ env: ENV, dshHome: home })).rejects.toBeInstanceOf(DshConfigError);
|
|
201
|
+
expect(await readFile(patchPath, 'utf-8')).toBe(broken);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('T-DSH8b: credentials 结构非法时抛错且不写 patch', async () => {
|
|
205
|
+
const home = await makeHome();
|
|
206
|
+
dirs.push(home);
|
|
207
|
+
await mkdir(home, { recursive: true });
|
|
208
|
+
const credentialsPath = join(home, '.credentials.yaml');
|
|
209
|
+
const broken = 'version: 1\nrefs: not-a-map\n';
|
|
210
|
+
await writeFile(credentialsPath, broken, 'utf-8');
|
|
211
|
+
|
|
212
|
+
await expect(writeDshConfig({ env: ENV, dshHome: home })).rejects.toBeInstanceOf(DshConfigError);
|
|
213
|
+
expect(await readFile(credentialsPath, 'utf-8')).toBe(broken);
|
|
214
|
+
await expect(stat(join(home, 'profiles', 'acp', 'cordis.patch.yml'))).rejects.toThrow();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('T-DSH9: 空模型清单不写 model 字段、modelCount=0、不抛错', async () => {
|
|
218
|
+
const home = await makeHome();
|
|
219
|
+
dirs.push(home);
|
|
220
|
+
|
|
221
|
+
const result = await writeDshConfig({
|
|
222
|
+
env: { ...ENV, modelIds: [], modelContextLengths: {} },
|
|
223
|
+
dshHome: home,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
expect(result.modelCount).toBe(0);
|
|
227
|
+
const patch = (await readYaml(result.patchPath)) as Array<Record<string, any>>;
|
|
228
|
+
expect(patch.find((entry) => entry.id === 'agent-default-model')!.config).toEqual({
|
|
229
|
+
provider: 'zag',
|
|
230
|
+
});
|
|
231
|
+
expect(patch.find((entry) => entry.id === 'acp')!.config).toEqual({ provider: 'zag' });
|
|
232
|
+
const zag = patch.find((entry) => entry.id === 'llm-pi-ai')!.config.providers.zag;
|
|
233
|
+
expect(zag.models).toEqual([]);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('T-DSH10: 二次执行结果等价(条目不重复、权限不漂移)', async () => {
|
|
237
|
+
const home = await makeHome();
|
|
238
|
+
dirs.push(home);
|
|
239
|
+
|
|
240
|
+
const first = await writeDshConfig({ env: ENV, dshHome: home });
|
|
241
|
+
const firstPatch = await readFile(first.patchPath, 'utf-8');
|
|
242
|
+
const firstCredentials = await readFile(first.credentialsPath, 'utf-8');
|
|
243
|
+
|
|
244
|
+
const second = await writeDshConfig({ env: ENV, dshHome: home });
|
|
245
|
+
|
|
246
|
+
expect(second.patchCreated).toBe(false);
|
|
247
|
+
expect(second.credentialsCreated).toBe(false);
|
|
248
|
+
expect(await readFile(second.patchPath, 'utf-8')).toBe(firstPatch);
|
|
249
|
+
expect(await readFile(second.credentialsPath, 'utf-8')).toBe(firstCredentials);
|
|
250
|
+
const patch = parse(firstPatch) as Array<Record<string, any>>;
|
|
251
|
+
expect(patch).toHaveLength(4);
|
|
252
|
+
expect((await stat(second.patchPath)).mode & 0o777).toBe(0o600);
|
|
253
|
+
expect((await stat(join(home, 'profiles', 'acp'))).mode & 0o777).toBe(0o700);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('T-DSH11: 返回值/错误信息不含 API Key', async () => {
|
|
257
|
+
const home = await makeHome();
|
|
258
|
+
dirs.push(home);
|
|
259
|
+
|
|
260
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
261
|
+
expect(JSON.stringify(result)).not.toContain(API_KEY);
|
|
262
|
+
|
|
263
|
+
const brokenHome = await makeHome();
|
|
264
|
+
dirs.push(brokenHome);
|
|
265
|
+
const profilesDir = join(brokenHome, 'profiles', 'acp');
|
|
266
|
+
await mkdir(profilesDir, { recursive: true });
|
|
267
|
+
await writeFile(join(profilesDir, 'cordis.patch.yml'), '- broken: [\n', 'utf-8');
|
|
268
|
+
try {
|
|
269
|
+
await writeDshConfig({ env: ENV, dshHome: brokenHome });
|
|
270
|
+
expect.unreachable();
|
|
271
|
+
} catch (error) {
|
|
272
|
+
expect(JSON.stringify((error as Error).message)).not.toContain(API_KEY);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
});
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { chmod, mkdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { Document, isMap, isSeq, parseDocument, type YAMLMap, type YAMLSeq } from 'yaml';
|
|
6
|
+
import type { HarnessEnv } from './env.js';
|
|
7
|
+
|
|
8
|
+
/** dsh 凭据 env 引用名(值只写 `.credentials.yaml` 的 `refs`)。 */
|
|
9
|
+
export const ZAG_API_KEY_ENV = 'ZAG_MODEL_API_KEY';
|
|
10
|
+
|
|
11
|
+
/** dsh ACP profile 名(`dsh --profile acp`)。 */
|
|
12
|
+
export const DSH_ACP_PROFILE = 'acp';
|
|
13
|
+
|
|
14
|
+
/** dsh ACP profile 组合包名(写入 patch 的 `acp` 条目)。 */
|
|
15
|
+
export const DSH_ACP_APP_NAME = '@deepseek-ai/dsh-acp';
|
|
16
|
+
|
|
17
|
+
/** 我方在 `cordis.patch.yml` 中维护的 4 个条目(按此顺序写入,按 id 替换)。 */
|
|
18
|
+
export const DSH_PATCH_ENTRY_IDS = [
|
|
19
|
+
'llm-deepseek',
|
|
20
|
+
'llm-pi-ai',
|
|
21
|
+
'agent-default-model',
|
|
22
|
+
'acp',
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
/** 写入 dsh 配置失败(消息只含路径/条目名,不含 API Key 等 secret)。 */
|
|
26
|
+
export class DshConfigError extends Error {
|
|
27
|
+
constructor(message: string) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = 'DshConfigError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type DshConfigEnv = Pick<
|
|
34
|
+
HarnessEnv,
|
|
35
|
+
'modelBaseUrl' | 'modelApiKey' | 'modelIds'
|
|
36
|
+
> &
|
|
37
|
+
Partial<Pick<HarnessEnv, 'modelContextLengths'>>;
|
|
38
|
+
|
|
39
|
+
export interface WriteDshConfigOptions {
|
|
40
|
+
env: DshConfigEnv;
|
|
41
|
+
/** 覆盖 dsh home(缺省 `$DSH_HOME` 或 `~/.dsh`)。 */
|
|
42
|
+
dshHome?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface WriteDshConfigResult {
|
|
46
|
+
/** `cordis.patch.yml` 实际路径。 */
|
|
47
|
+
patchPath: string;
|
|
48
|
+
/** `.credentials.yaml` 实际路径。 */
|
|
49
|
+
credentialsPath: string;
|
|
50
|
+
/** patch 是否新建。 */
|
|
51
|
+
patchCreated: boolean;
|
|
52
|
+
/** credentials 是否新建。 */
|
|
53
|
+
credentialsCreated: boolean;
|
|
54
|
+
/** 写入的模型数量。 */
|
|
55
|
+
modelCount: number;
|
|
56
|
+
/** patch 中我方维护的条目数(固定 4)。 */
|
|
57
|
+
entryCount: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 默认 dsh home:`$DSH_HOME` 优先,否则 `~/.dsh`。 */
|
|
61
|
+
export function defaultDshHome(env: NodeJS.ProcessEnv = process.env): string {
|
|
62
|
+
const configured = typeof env.DSH_HOME === 'string' ? env.DSH_HOME.trim() : '';
|
|
63
|
+
return configured || join(homedir(), '.dsh');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function exists(path: string): Promise<boolean> {
|
|
67
|
+
try {
|
|
68
|
+
await stat(path);
|
|
69
|
+
return true;
|
|
70
|
+
} catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** `llm-pi-ai.providers.zag`:模型清单整体替换,`contextWindow` 仅在正整数时写入。 */
|
|
76
|
+
function buildZagProvider(env: DshConfigEnv): Record<string, unknown> {
|
|
77
|
+
const models = env.modelIds.map((id) => {
|
|
78
|
+
const context = env.modelContextLengths?.[id];
|
|
79
|
+
return typeof context === 'number' && Number.isInteger(context) && context > 0
|
|
80
|
+
? { id, name: id, contextWindow: context }
|
|
81
|
+
: { id, name: id };
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
displayName: 'ZAG',
|
|
85
|
+
apiKeyEnv: ZAG_API_KEY_ENV,
|
|
86
|
+
api: 'openai-completions',
|
|
87
|
+
baseURL: env.modelBaseUrl,
|
|
88
|
+
models,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 我方 patch 条目(按 id;默认模型取 `modelIds[0]`,空清单不写 `model`)。 */
|
|
93
|
+
function buildEntry(id: (typeof DSH_PATCH_ENTRY_IDS)[number], env: DshConfigEnv): Record<string, unknown> {
|
|
94
|
+
const defaultModel = env.modelIds[0];
|
|
95
|
+
switch (id) {
|
|
96
|
+
case 'llm-deepseek':
|
|
97
|
+
return { id, disabled: true };
|
|
98
|
+
case 'llm-pi-ai':
|
|
99
|
+
return { id, config: { providers: { zag: buildZagProvider(env) } } };
|
|
100
|
+
case 'agent-default-model':
|
|
101
|
+
return {
|
|
102
|
+
id,
|
|
103
|
+
config: defaultModel ? { provider: 'zag', model: defaultModel } : { provider: 'zag' },
|
|
104
|
+
};
|
|
105
|
+
case 'acp':
|
|
106
|
+
return {
|
|
107
|
+
id,
|
|
108
|
+
name: DSH_ACP_APP_NAME,
|
|
109
|
+
config: defaultModel ? { provider: 'zag', model: defaultModel } : { provider: 'zag' },
|
|
110
|
+
};
|
|
111
|
+
default:
|
|
112
|
+
throw new DshConfigError(`unsupported dsh patch entry: ${String(id)}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function parsePatchDocument(text: string, path: string): Document {
|
|
117
|
+
if (text.trim().length === 0) return new Document([]);
|
|
118
|
+
const doc = parseDocument(text);
|
|
119
|
+
if (doc.errors.length > 0) {
|
|
120
|
+
const code = (doc.errors[0] as { code?: string }).code ?? 'PARSE_ERROR';
|
|
121
|
+
throw new DshConfigError(`cannot parse dsh patch at ${path}: ${code}`);
|
|
122
|
+
}
|
|
123
|
+
if (doc.contents === null) return new Document([]);
|
|
124
|
+
if (!isSeq(doc.contents)) {
|
|
125
|
+
throw new DshConfigError(`invalid dsh patch at ${path}: expected a top-level array`);
|
|
126
|
+
}
|
|
127
|
+
return doc;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 合并 `cordis.patch.yml`:
|
|
132
|
+
*
|
|
133
|
+
* - 顶层条目数组,按 `id` 定位我方 4 个条目并**整体替换其 config**;
|
|
134
|
+
* - 其余条目与注释逐字保留(新条目追加在末尾);
|
|
135
|
+
* - 解析失败抛错、不写文件(不静默丢数据)。
|
|
136
|
+
*/
|
|
137
|
+
function mergePatchDocument(doc: Document, env: DshConfigEnv): { content: string; entryCount: number } {
|
|
138
|
+
const seq = doc.contents as YAMLSeq;
|
|
139
|
+
const findIndex = (id: string): number =>
|
|
140
|
+
seq.items.findIndex((item) => isMap(item) && item.get('id') === id);
|
|
141
|
+
|
|
142
|
+
for (const id of DSH_PATCH_ENTRY_IDS) {
|
|
143
|
+
const entry = buildEntry(id, env);
|
|
144
|
+
const index = findIndex(id);
|
|
145
|
+
if (index === -1) {
|
|
146
|
+
seq.add(doc.createNode(entry));
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (id === 'llm-deepseek') {
|
|
150
|
+
doc.setIn([index, 'disabled'], true);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const config = entry.config as Record<string, unknown>;
|
|
154
|
+
doc.setIn([index, 'config'], doc.createNode(config));
|
|
155
|
+
if (id === 'acp') doc.setIn([index, 'name'], DSH_ACP_APP_NAME);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let content = doc.toString();
|
|
159
|
+
if (!content.endsWith('\n')) content += '\n';
|
|
160
|
+
return { content, entryCount: DSH_PATCH_ENTRY_IDS.length };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 合并 `.credentials.yaml`:
|
|
165
|
+
*
|
|
166
|
+
* - 已有文件只替换 `refs.ZAG_MODEL_API_KEY`,保留其他 `refs`/`records` 与注释;
|
|
167
|
+
* - 新建写 `{ version: 1, refs: { ZAG_MODEL_API_KEY } }`;
|
|
168
|
+
* - 永远写 `version: 1`(旧扁平格式由 dsh 迁移,我方不写);
|
|
169
|
+
* - 非法 YAML / 非 mapping 抛错不覆盖。
|
|
170
|
+
*/
|
|
171
|
+
function mergeCredentialsDocument(
|
|
172
|
+
text: string,
|
|
173
|
+
path: string,
|
|
174
|
+
env: DshConfigEnv,
|
|
175
|
+
): string {
|
|
176
|
+
let doc: Document;
|
|
177
|
+
if (text.trim().length === 0) {
|
|
178
|
+
doc = new Document({ version: 1, refs: {} });
|
|
179
|
+
} else {
|
|
180
|
+
doc = parseDocument(text);
|
|
181
|
+
if (doc.errors.length > 0) {
|
|
182
|
+
const code = (doc.errors[0] as { code?: string }).code ?? 'PARSE_ERROR';
|
|
183
|
+
throw new DshConfigError(`cannot parse dsh credentials at ${path}: ${code}`);
|
|
184
|
+
}
|
|
185
|
+
if (doc.contents === null) {
|
|
186
|
+
doc = new Document({ version: 1, refs: {} });
|
|
187
|
+
} else if (!isMap(doc.contents)) {
|
|
188
|
+
throw new DshConfigError(`invalid dsh credentials at ${path}: expected a mapping`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const root = doc.contents as YAMLMap;
|
|
193
|
+
root.set('version', 1);
|
|
194
|
+
const refs = root.get('refs');
|
|
195
|
+
if (refs === undefined || refs === null) {
|
|
196
|
+
root.set('refs', doc.createNode({}));
|
|
197
|
+
} else if (!isMap(refs)) {
|
|
198
|
+
throw new DshConfigError(`invalid dsh credentials at ${path}: refs must be a mapping`);
|
|
199
|
+
}
|
|
200
|
+
const refsMap = root.get('refs') as YAMLMap;
|
|
201
|
+
refsMap.set(ZAG_API_KEY_ENV, env.modelApiKey);
|
|
202
|
+
|
|
203
|
+
let content = doc.toString();
|
|
204
|
+
if (!content.endsWith('\n')) content += '\n';
|
|
205
|
+
return content;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 同目录临时文件 + rename 原子替换;失败时清理临时文件、原文件保持完整。 */
|
|
209
|
+
async function atomicWrite(path: string, dir: string, content: string, label: string): Promise<void> {
|
|
210
|
+
const tempPath = join(dir, `.${label}.${process.pid}.${randomUUID()}.tmp`);
|
|
211
|
+
try {
|
|
212
|
+
await writeFile(tempPath, content, { encoding: 'utf-8', mode: 0o600 });
|
|
213
|
+
await chmod(tempPath, 0o600);
|
|
214
|
+
await rename(tempPath, path);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
await rm(tempPath, { force: true }).catch(() => {});
|
|
217
|
+
throw new DshConfigError(`cannot write ${label} at ${path}: ${(error as Error).message}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function readText(path: string, label: string): Promise<{ text: string; created: boolean }> {
|
|
222
|
+
if (!(await exists(path))) return { text: '', created: true };
|
|
223
|
+
try {
|
|
224
|
+
return { text: await readFile(path, 'utf-8'), created: false };
|
|
225
|
+
} catch (error) {
|
|
226
|
+
throw new DshConfigError(`cannot read ${label} at ${path}: ${(error as Error).message}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 写入/合并 dsh 平台渠道模型注入(计划 §3.2.1 / §3.2.2):
|
|
232
|
+
*
|
|
233
|
+
* 1. `~/.dsh/profiles/acp/cordis.patch.yml`(目录 0700、文件 0600);
|
|
234
|
+
* 2. `~/.dsh/.credentials.yaml`(0600);
|
|
235
|
+
* 3. 两文件均走临时文件 + `rename` 原子替换,失败不破坏既有文件;
|
|
236
|
+
* 4. secret 只落凭据文件,不进入返回值/错误信息。
|
|
237
|
+
*/
|
|
238
|
+
export async function writeDshConfig(
|
|
239
|
+
options: WriteDshConfigOptions,
|
|
240
|
+
): Promise<WriteDshConfigResult> {
|
|
241
|
+
const envSource = options.env as DshConfigEnv & { DSH_HOME?: string };
|
|
242
|
+
const dshHome =
|
|
243
|
+
options.dshHome ?? defaultDshHome(envSource as unknown as NodeJS.ProcessEnv);
|
|
244
|
+
const profilesDir = join(dshHome, 'profiles', DSH_ACP_PROFILE);
|
|
245
|
+
const patchPath = join(profilesDir, 'cordis.patch.yml');
|
|
246
|
+
const credentialsPath = join(dshHome, '.credentials.yaml');
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
await mkdir(profilesDir, { recursive: true, mode: 0o700 });
|
|
250
|
+
// `mkdir` 的 mode 只对新建目录生效:已存在目录显式收紧到 0700。
|
|
251
|
+
await chmod(profilesDir, 0o700);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
throw new DshConfigError(
|
|
254
|
+
`cannot set permissions on dsh profile directory ${profilesDir}: ${(error as Error).message}`,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const patchFile = await readText(patchPath, 'dsh patch');
|
|
259
|
+
const patchDoc = parsePatchDocument(patchFile.text, patchPath);
|
|
260
|
+
const mergedPatch = mergePatchDocument(patchDoc, options.env);
|
|
261
|
+
|
|
262
|
+
const credentialsFile = await readText(credentialsPath, 'dsh credentials');
|
|
263
|
+
const credentialsContent = mergeCredentialsDocument(
|
|
264
|
+
credentialsFile.text,
|
|
265
|
+
credentialsPath,
|
|
266
|
+
options.env,
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
await atomicWrite(patchPath, profilesDir, mergedPatch.content, 'cordis.patch');
|
|
270
|
+
await atomicWrite(credentialsPath, dshHome, credentialsContent, 'dsh-credentials');
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
patchPath,
|
|
274
|
+
credentialsPath,
|
|
275
|
+
patchCreated: patchFile.created,
|
|
276
|
+
credentialsCreated: credentialsFile.created,
|
|
277
|
+
modelCount: options.env.modelIds.length,
|
|
278
|
+
entryCount: mergedPatch.entryCount,
|
|
279
|
+
};
|
|
280
|
+
}
|
package/src/harness/env.test.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
HarnessEnvError,
|
|
4
|
+
parseModelContextLengths,
|
|
5
|
+
parseModelIds,
|
|
6
|
+
readHarnessEnv,
|
|
7
|
+
resolvePrepareTypes,
|
|
8
|
+
} from './env.js';
|
|
3
9
|
|
|
4
10
|
const VALID_ENV: NodeJS.ProcessEnv = {
|
|
5
11
|
ZAG_MODEL_BASE_URL: 'http://admin-api:8002/v1',
|
|
6
12
|
ZAG_MODEL_API_KEY: 'zag_sk_super_secret',
|
|
7
13
|
ZAG_MODEL_IDS: '["model-a","model-b"]',
|
|
8
|
-
ZAG_DEFAULT_WORKSPACE: '/
|
|
14
|
+
ZAG_DEFAULT_WORKSPACE: '/home/worker/workspace',
|
|
9
15
|
};
|
|
10
16
|
|
|
11
17
|
describe('readHarnessEnv (T-AG7/T-AG11)', () => {
|
|
@@ -15,7 +21,7 @@ describe('readHarnessEnv (T-AG7/T-AG11)', () => {
|
|
|
15
21
|
modelApiKey: 'zag_sk_super_secret',
|
|
16
22
|
modelIds: ['model-a', 'model-b'],
|
|
17
23
|
modelContextLengths: {},
|
|
18
|
-
defaultWorkspace: '/
|
|
24
|
+
defaultWorkspace: '/home/worker/workspace',
|
|
19
25
|
});
|
|
20
26
|
});
|
|
21
27
|
|
|
@@ -120,3 +126,28 @@ describe('parseModelContextLengths', () => {
|
|
|
120
126
|
expect(() => parseModelContextLengths('{"a":1.5}')).toThrowError(/invalid value/);
|
|
121
127
|
});
|
|
122
128
|
});
|
|
129
|
+
|
|
130
|
+
describe('resolvePrepareTypes (T-PREP2)', () => {
|
|
131
|
+
it('未设置/空串 → 回退 opencode', () => {
|
|
132
|
+
expect(resolvePrepareTypes({})).toEqual(['opencode']);
|
|
133
|
+
expect(resolvePrepareTypes({ ZAG_HARNESS_TYPES: ' ' })).toEqual(['opencode']);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('解析逗号分隔、去空去重并保持顺序', () => {
|
|
137
|
+
expect(resolvePrepareTypes({ ZAG_HARNESS_TYPES: 'opencode, deepseek_harness ,opencode' })).toEqual([
|
|
138
|
+
'opencode',
|
|
139
|
+
'deepseek_harness',
|
|
140
|
+
]);
|
|
141
|
+
expect(resolvePrepareTypes({ ZAG_HARNESS_TYPES: 'deepseek_harness,opencode' })).toEqual([
|
|
142
|
+
'deepseek_harness',
|
|
143
|
+
'opencode',
|
|
144
|
+
]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('白名单过滤 kimi_code / 未知类型;过滤后为空回退 opencode', () => {
|
|
148
|
+
expect(resolvePrepareTypes({ ZAG_HARNESS_TYPES: 'kimi_code,deepseek_harness' })).toEqual([
|
|
149
|
+
'deepseek_harness',
|
|
150
|
+
]);
|
|
151
|
+
expect(resolvePrepareTypes({ ZAG_HARNESS_TYPES: 'kimi_code,bogus' })).toEqual(['opencode']);
|
|
152
|
+
});
|
|
153
|
+
});
|
package/src/harness/env.ts
CHANGED
|
@@ -27,6 +27,32 @@ export class HarnessEnvError extends Error {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/** `harness prepare` 可处理的 harness 类型(与 `ZAG_HARNESS_TYPES` 的 harness slug 对齐)。 */
|
|
31
|
+
export const PREPARE_HARNESS_TYPES = ['opencode', 'deepseek_harness'] as const;
|
|
32
|
+
|
|
33
|
+
export type PrepareHarnessType = (typeof PREPARE_HARNESS_TYPES)[number];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 解析 `ZAG_HARNESS_TYPES`(计划 §3 B5):
|
|
37
|
+
*
|
|
38
|
+
* - 逗号分隔、trim、去重,保持输入顺序;
|
|
39
|
+
* - 白名单过滤(`kimi_code` 等无 prepare 步骤的类型忽略);
|
|
40
|
+
* - 未设置/空串/过滤后为空 → `['opencode']`(旧 worker 手工执行零退化)。
|
|
41
|
+
*/
|
|
42
|
+
export function resolvePrepareTypes(env: NodeJS.ProcessEnv = process.env): PrepareHarnessType[] {
|
|
43
|
+
const raw = typeof env.ZAG_HARNESS_TYPES === 'string' ? env.ZAG_HARNESS_TYPES : '';
|
|
44
|
+
const allowed = new Set<string>(PREPARE_HARNESS_TYPES);
|
|
45
|
+
const types: PrepareHarnessType[] = [];
|
|
46
|
+
const seen = new Set<string>();
|
|
47
|
+
for (const item of raw.split(',')) {
|
|
48
|
+
const value = item.trim();
|
|
49
|
+
if (!value || seen.has(value) || !allowed.has(value)) continue;
|
|
50
|
+
seen.add(value);
|
|
51
|
+
types.push(value as PrepareHarnessType);
|
|
52
|
+
}
|
|
53
|
+
return types.length > 0 ? types : ['opencode'];
|
|
54
|
+
}
|
|
55
|
+
|
|
30
56
|
function nonEmpty(value: string | undefined): string {
|
|
31
57
|
return typeof value === 'string' ? value.trim() : '';
|
|
32
58
|
}
|
|
@@ -45,6 +45,14 @@ export function extractVersion(stdout: string, stderr = ''): string | undefined
|
|
|
45
45
|
return line.replace(/^v/i, '');
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* 版本串归一化:取最后一个空白分隔段(容忍 `dsh 0.1.2` / `v0.1.2` 等输出前缀),
|
|
50
|
+
* 去掉 `v` 前缀。预发布段(如 `0.1.2-alpha.2`)原样保留。
|
|
51
|
+
*/
|
|
52
|
+
export function normalizeCliVersion(version: string): string {
|
|
53
|
+
return (version.trim().split(/\s+/).pop() ?? '').replace(/^v/i, '');
|
|
54
|
+
}
|
|
55
|
+
|
|
48
56
|
/**
|
|
49
57
|
* `opencode --version` 硬校验(`opencode -v` 仅作旧版 CLI 的兼容探测)。
|
|
50
58
|
*
|
|
@@ -60,8 +68,7 @@ export async function verifyOpencodeCli(
|
|
|
60
68
|
): Promise<string> {
|
|
61
69
|
const failures: string[] = [];
|
|
62
70
|
|
|
63
|
-
const normalizeVersion =
|
|
64
|
-
(version.trim().split(/\s+/).pop() ?? '').replace(/^v/i, '');
|
|
71
|
+
const normalizeVersion = normalizeCliVersion;
|
|
65
72
|
|
|
66
73
|
const ensureExpected = (version: string): string => {
|
|
67
74
|
if (expectedVersion && normalizeVersion(version) !== normalizeVersion(expectedVersion)) {
|