@omerrgocmen/crewctl 1.0.3 → 1.3.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.
Files changed (91) hide show
  1. package/README.md +193 -193
  2. package/orchestrator/README.md +473 -462
  3. package/orchestrator/config.default.json +79 -71
  4. package/orchestrator/roles/executor.md +57 -57
  5. package/orchestrator/roles/operator.md +77 -77
  6. package/orchestrator/roles/planner.md +58 -58
  7. package/orchestrator/roles/reviewer.md +63 -63
  8. package/orchestrator/skills/acceptance-criteria.md +18 -18
  9. package/orchestrator/skills/accessibility-audit.md +16 -16
  10. package/orchestrator/skills/accessible-forms.md +16 -16
  11. package/orchestrator/skills/api-design.md +27 -27
  12. package/orchestrator/skills/api-documentation.md +16 -16
  13. package/orchestrator/skills/architecture-decision-record.md +18 -18
  14. package/orchestrator/skills/authentication-design.md +16 -16
  15. package/orchestrator/skills/authorization-review.md +16 -16
  16. package/orchestrator/skills/backward-compatibility.md +17 -17
  17. package/orchestrator/skills/changelog-writing.md +16 -16
  18. package/orchestrator/skills/ci-pipeline-design.md +16 -16
  19. package/orchestrator/skills/cli-design.md +16 -16
  20. package/orchestrator/skills/code-review.md +27 -27
  21. package/orchestrator/skills/configuration-management.md +16 -16
  22. package/orchestrator/skills/container-review.md +16 -16
  23. package/orchestrator/skills/contract-testing.md +16 -16
  24. package/orchestrator/skills/dashboard-design.md +16 -16
  25. package/orchestrator/skills/database-migration.md +16 -16
  26. package/orchestrator/skills/database-schema-design.md +16 -16
  27. package/orchestrator/skills/debugging.md +26 -26
  28. package/orchestrator/skills/dependency-review.md +16 -16
  29. package/orchestrator/skills/design-review.md +29 -29
  30. package/orchestrator/skills/design-system.md +16 -16
  31. package/orchestrator/skills/docs-api-reference.md +16 -16
  32. package/orchestrator/skills/docs-troubleshooting.md +16 -16
  33. package/orchestrator/skills/docs-tutorial.md +16 -16
  34. package/orchestrator/skills/docs-writing.md +25 -25
  35. package/orchestrator/skills/empty-error-loading-states.md +16 -16
  36. package/orchestrator/skills/end-to-end-testing.md +16 -16
  37. package/orchestrator/skills/error-handling.md +16 -16
  38. package/orchestrator/skills/frontend-design.md +28 -28
  39. package/orchestrator/skills/git-commit-writing.md +16 -16
  40. package/orchestrator/skills/graphql-design.md +16 -16
  41. package/orchestrator/skills/incident-runbook.md +18 -18
  42. package/orchestrator/skills/input-validation.md +16 -16
  43. package/orchestrator/skills/integration-testing.md +16 -16
  44. package/orchestrator/skills/interaction-design.md +16 -16
  45. package/orchestrator/skills/landing-page-design.md +16 -16
  46. package/orchestrator/skills/observability-design.md +16 -16
  47. package/orchestrator/skills/openapi-contract.md +16 -16
  48. package/orchestrator/skills/performance-profiling.md +16 -16
  49. package/orchestrator/skills/privacy-review.md +16 -16
  50. package/orchestrator/skills/property-based-testing.md +16 -16
  51. package/orchestrator/skills/pull-request-writing.md +16 -16
  52. package/orchestrator/skills/refactoring.md +16 -16
  53. package/orchestrator/skills/release-readiness.md +16 -16
  54. package/orchestrator/skills/responsive-design.md +16 -16
  55. package/orchestrator/skills/secrets-management.md +16 -16
  56. package/orchestrator/skills/secure-file-upload.md +16 -16
  57. package/orchestrator/skills/security-review.md +26 -26
  58. package/orchestrator/skills/semantic-versioning.md +17 -17
  59. package/orchestrator/skills/seo-on-page.md +16 -16
  60. package/orchestrator/skills/seo-structured-data.md +16 -16
  61. package/orchestrator/skills/seo-technical-audit.md +16 -16
  62. package/orchestrator/skills/sql-query-review.md +16 -16
  63. package/orchestrator/skills/supply-chain-security.md +16 -16
  64. package/orchestrator/skills/test-strategy.md +16 -16
  65. package/orchestrator/skills/threat-modeling.md +16 -16
  66. package/orchestrator/skills/unit-testing.md +16 -16
  67. package/orchestrator/skills/write-tests.md +28 -28
  68. package/orchestrator/src/cli-registry.js +740 -616
  69. package/orchestrator/src/cli.js +140 -135
  70. package/orchestrator/src/doctor.js +64 -64
  71. package/orchestrator/src/engine.js +1735 -1552
  72. package/orchestrator/src/schedule.js +126 -126
  73. package/orchestrator/src/server.js +818 -766
  74. package/orchestrator/src/skill-registry.js +272 -272
  75. package/orchestrator/src/store.js +470 -399
  76. package/orchestrator/web/OrbitControls.js +1417 -1417
  77. package/orchestrator/web/board.html +146 -146
  78. package/orchestrator/web/code.html +213 -213
  79. package/orchestrator/web/flow.html +741 -741
  80. package/orchestrator/web/index.html +602 -588
  81. package/orchestrator/web/jsm/postprocessing/EffectComposer.js +231 -231
  82. package/orchestrator/web/jsm/postprocessing/MaskPass.js +104 -104
  83. package/orchestrator/web/jsm/postprocessing/Pass.js +95 -95
  84. package/orchestrator/web/jsm/postprocessing/RenderPass.js +99 -99
  85. package/orchestrator/web/jsm/postprocessing/ShaderPass.js +77 -77
  86. package/orchestrator/web/jsm/postprocessing/UnrealBloomPass.js +415 -415
  87. package/orchestrator/web/jsm/shaders/CopyShader.js +45 -45
  88. package/orchestrator/web/jsm/shaders/LuminosityHighPassShader.js +66 -66
  89. package/orchestrator/web/site.webmanifest +12 -12
  90. package/orchestrator/web/three.module.min.js +6 -6
  91. package/package.json +51 -51
@@ -1,616 +1,740 @@
1
- const { spawn, spawnSync } = require("child_process");
2
- const fs = require("fs");
3
- const os = require("os");
4
- const path = require("path");
5
-
6
- const isWin = process.platform === "win32";
7
-
8
- const DEFINITIONS = {
9
- codex: {
10
- command: "codex",
11
- versionArgs: ["--version"],
12
- defaultArgs: ["exec", "--skip-git-repo-check"],
13
- // Codex calisirken surekli ilerleme akitir; 3 dakikalik sessizlik gercek bir takilmadir.
14
- silenceTimeoutSeconds: 180,
15
- description: "OpenAI Codex kodlama, hata ayiklama ve inceleme agenti",
16
- capabilities: ["implementation", "debugging", "testing", "review", "analysis"],
17
- roleFile: "roles/executor.md",
18
- },
19
- claude: {
20
- command: "claude",
21
- versionArgs: ["--version"],
22
- defaultArgs: ["-p", "--permission-mode", "acceptEdits", "--output-format", "text"],
23
- description: "Claude Code uygulama, analiz ve test agenti",
24
- capabilities: ["implementation", "debugging", "testing", "review", "web"],
25
- roleFile: "roles/executor.md",
26
- },
27
- gemini: {
28
- command: "gemini",
29
- versionArgs: ["--version"],
30
- // Argumansiz gemini interaktif TUI acar ve izin bekler; yolo ile otonom, stdin'den prompt alir.
31
- defaultArgs: ["--approval-mode", "yolo"],
32
- // Gemini de akis halinde cikti verir; uzun sessizlik takilma isaretidir.
33
- silenceTimeoutSeconds: 180,
34
- description: "Gemini CLI analiz, uygulama ve web arastirma agenti",
35
- capabilities: ["implementation", "planning", "analysis", "research", "web"],
36
- roleFile: "roles/executor.md",
37
- },
38
- opencode: {
39
- command: "opencode",
40
- versionArgs: ["--version"],
41
- // OpenCode saglayici/model kombinasyonuna gore uzun sure sessiz kalabilir. Varsayilan
42
- // agent timeout'u, basarili bir calismayi SIGTERM ile yarida kesmeyecek kadar genis tut.
43
- timeoutSeconds: 1800,
44
- // OpenCode adim baslattiktan sonra model/provider yanitini beklerken uzun sure hic cikti
45
- // akitmayabilir (ozellikle yavas/rate-limitli modeller). 180sn cok agresifti ve calisan bir
46
- // kosmayi yariyordu; sessizlik payini genis tut. Gercek takilma yine timeoutSeconds ile yakalanir.
47
- silenceTimeoutSeconds: 300,
48
- // Bazi eski OpenCode surumleri --auto bayragini tanimaz. Otonom izinler
49
- // engine tarafinda OPENCODE_CONFIG_CONTENT ile surumden bagimsiz aktarilir.
50
- defaultArgs: ["run", "--format", "json", "Attached file contains the full task. Follow it exactly, make the changes, and report what you did.", "--file", "{PROMPT_FILE}"],
51
- description: "OpenCode coklu saglayici kodlama, uygulama ve inceleme agenti",
52
- capabilities: ["implementation", "debugging", "testing", "review", "analysis", "research"],
53
- roleFile: "roles/executor.md",
54
- install: {
55
- windows: "npm install -g opencode-ai (alternatif: choco install opencode / scoop install opencode)",
56
- macos: "brew install anomalyco/tap/opencode (alternatif: npm install -g opencode-ai)",
57
- linux: "curl -fsSL https://opencode.ai/install | bash (alternatif: npm install -g opencode-ai)",
58
- auth: "opencode auth login",
59
- },
60
- },
61
- };
62
- const RESOLVED = new Map();
63
- let OPEN_CODE_MODELS = [];
64
-
65
- // En fazla bir CLI'yi (or. codex) ayni anda hem canli saglik/model probe'u hem de motorun
66
- // operatör kosumu calistirirsa, codex gibi bazi CLI'lar ayni is dizininde ikinci exec oturumunu
67
- // SIGTERM ile keser. Probe'lar "en iyi caba"dir; motor onceliklidir. Aktif probe cocuklarini
68
- // izleyip motor bir goreve baslarken hepsini sonlandiriyoruz.
69
- const ACTIVE_PROBES = new Set();
70
- function registerProbe(child) {
71
- if (!child) return () => {};
72
- ACTIVE_PROBES.add(child);
73
- return () => ACTIVE_PROBES.delete(child);
74
- }
75
- // Uctan uca proses agacini oldur (codex kabuk altinda alt proses baslatabilir).
76
- function terminateProbeTree(child) {
77
- if (!child) return;
78
- try {
79
- if (isWin && child.pid) spawn("taskkill", ["/pid", String(child.pid), "/t", "/f"], { windowsHide: true, stdio: "ignore" });
80
- else child.kill("SIGKILL");
81
- } catch {}
82
- }
83
- // Motor bir goreve baslarken cagirir: uctaki tum canli probe'lari sonlandirir ki operatörun
84
- // CLI'si (codex vb.) temiz bir alanda calissin. Probe'lar sonuc olarak "timeout/failed" doner;
85
- // bu zararsizdir, motor bosaldiginda yeniden denenir.
86
- function abortActiveProbes() {
87
- const count = ACTIVE_PROBES.size;
88
- for (const child of ACTIVE_PROBES) terminateProbeTree(child);
89
- ACTIVE_PROBES.clear();
90
- return count;
91
- }
92
-
93
- function selectOpenCodeModel(models) {
94
- const list = Array.isArray(models) ? models.map(String).filter(Boolean) : [];
95
- const priorities = [
96
- (x) => x === "opencode/big-pickle",
97
- (x) => /^opencode\/.+free$/i.test(x),
98
- (x) => x.startsWith("opencode/"),
99
- (x) => x.startsWith("opencode-go/"),
100
- (x) => x.startsWith("minimax-coding-plan/"),
101
- (x) => !x.startsWith("ollama/"),
102
- ];
103
- for (const match of priorities) {
104
- const found = list.find(match);
105
- if (found) return found;
106
- }
107
- return "";
108
- }
109
-
110
- // `opencode models` ciktisindan saglayici/model satirlarini ayiklar. Baslik, bos satir ve
111
- // log gurultusunu eler; her "saglayici/model" bicimli satir gecerli sayilir.
112
- function parseOpenCodeModels(stdout) {
113
- const found = new Set();
114
- for (const line of String(stdout || "").split(/\r?\n/)) {
115
- const model = line.trim();
116
- if (/^[A-Za-z0-9._-]+\/.+/.test(model) && !/\s/.test(model)) found.add(model);
117
- }
118
- return [...found];
119
- }
120
-
121
- function listOpenCodeModels(command) {
122
- try {
123
- // Tum saglayicilarin listesi sorgulanir. Kullanicilarin ucretli abonelikleri
124
- // (opencode-go, minimax-coding-plan vb.) ayri saglayici olarak gorunur; yalnizca
125
- // "opencode" saglayicisini sorgulamak bu modelleri gizliyor ve kurulum yanlislikla
126
- // "hazir degil" sayiliyordu. Otomatik oneri siralamasini selectOpenCodeModel yapar.
127
- const result = spawnSync(command, ["models"], {
128
- encoding: "utf8", timeout: 20000, windowsHide: true,
129
- shell: isWin && (!path.isAbsolute(command) || /\.(cmd|bat)$/i.test(command)),
130
- });
131
- if (result.status !== 0) return [];
132
- return parseOpenCodeModels(result.stdout);
133
- } catch { return []; }
134
- }
135
-
136
- function adapterId(command) {
137
- const base = path.basename(String(command || "")).toLowerCase().replace(/\.(cmd|bat|exe|ps1)$/, "");
138
- if (base.includes("codex")) return "codex";
139
- if (base.includes("claude")) return "claude";
140
- if (base.includes("gemini")) return "gemini";
141
- if (base.includes("opencode")) return "opencode";
142
- return "custom";
143
- }
144
-
145
- // Farkli paket yoneticileri / kurulum araclari CLI'lari farkli dizinlere koyar. Her PC'de
146
- // calismasi icin yaygin npm/pnpm/yarn/bun/volta/scoop/winget/choco/homebrew konumlarini tarariz.
147
- function commonCandidates(id) {
148
- const home = os.homedir();
149
- if (isWin) {
150
- const APPDATA = process.env.APPDATA, LOCAL = process.env.LOCALAPPDATA, PD = process.env.ProgramData;
151
- const exts = ["cmd", "exe", "ps1", "bat", ""];
152
- const bases = [
153
- APPDATA && path.join(APPDATA, "npm"),
154
- LOCAL && path.join(LOCAL, "pnpm"),
155
- LOCAL && path.join(LOCAL, "Yarn", "bin"),
156
- LOCAL && path.join(LOCAL, "Microsoft", "WinGet", "Links"),
157
- LOCAL && path.join(LOCAL, "Programs", id),
158
- path.join(home, ".bun", "bin"),
159
- path.join(home, ".volta", "bin"),
160
- path.join(home, "scoop", "shims"),
161
- PD && path.join(PD, "chocolatey", "bin"),
162
- path.join(home, ".local", "bin"),
163
- path.join(home, "AppData", "Roaming", "npm"),
164
- ].filter(Boolean);
165
- const out = [];
166
- for (const base of bases) for (const ext of exts) out.push(path.join(base, ext ? `${id}.${ext}` : id));
167
- return out;
168
- }
169
- return [
170
- path.join(home, ".local", "bin", id),
171
- path.join(home, "bin", id),
172
- path.join(home, ".bun", "bin", id),
173
- path.join(home, ".volta", "bin", id),
174
- path.join(home, ".npm-global", "bin", id),
175
- path.join(home, ".yarn", "bin", id),
176
- path.join(home, ".local", "share", "pnpm", id),
177
- `/usr/local/bin/${id}`,
178
- `/opt/homebrew/bin/${id}`,
179
- `/usr/bin/${id}`,
180
- `/snap/bin/${id}`,
181
- ];
182
- }
183
-
184
- // Bilinen bir CLI komutu adapter alanindan daha guvenilir kaynaktir. Eski veya elle
185
- // duzenlenmis config'te adapter=claude + cmd=codex gibi bir celiski varsa hedef CLI'nin
186
- // guvenli varsayilan argumanlarina donulur; boylece bir CLI'ye digerinin bayragi gitmez.
187
- function normalizeAgentAdapter(agent) {
188
- if (!agent || typeof agent !== "object") return false;
189
- const configured = String(agent.adapter || "").trim().toLowerCase();
190
- const inferred = adapterId(agent.cmd);
191
- const adapter = inferred !== "custom" ? inferred : (DEFINITIONS[configured] ? configured : "custom");
192
- let changed = false;
193
- if (inferred !== "custom" && configured && configured !== inferred) {
194
- agent.args = DEFINITIONS[inferred].defaultArgs.slice();
195
- delete agent.model;
196
- delete agent.modelOverride;
197
- changed = true;
198
- }
199
- if (agent.adapter !== adapter) {
200
- agent.adapter = adapter;
201
- changed = true;
202
- }
203
- return changed;
204
- }
205
-
206
- function normalizeAgentAdapters(cfg) {
207
- if (!cfg?.agents || typeof cfg.agents !== "object") return false;
208
- let changed = false;
209
- for (const agent of Object.values(cfg.agents)) if (normalizeAgentAdapter(agent)) changed = true;
210
- return changed;
211
- }
212
-
213
- // cfg verilirse cfg.cliSettings[adapter] o CLI'nin varsayilan model ayari olarak uygulanir.
214
- // Oncelik: agent.model (profil bazli) > cliSettings[adapter].model (global) > CLI varsayilani.
215
- function effectiveAgent(agent, cfg) {
216
- const copy = { ...agent, args: Array.isArray(agent.args) ? agent.args.map(String) : [] };
217
- normalizeAgentAdapter(copy);
218
- const adapter = copy.adapter;
219
- const commandAdapter = adapterId(copy.cmd);
220
- const settings = (cfg && cfg.cliSettings && cfg.cliSettings[adapter]) || {};
221
- if (!copy.silenceTimeoutSeconds && DEFINITIONS[adapter]?.silenceTimeoutSeconds) copy.silenceTimeoutSeconds = DEFINITIONS[adapter].silenceTimeoutSeconds;
222
- if (commandAdapter === adapter && RESOLVED.has(adapter) && !path.isAbsolute(copy.cmd)) copy.cmd = RESOLVED.get(adapter);
223
- if (adapter === "codex") {
224
- const commands = new Set(["exec", "review", "resume", "apply"]);
225
- if (!copy.args.some((arg) => commands.has(arg))) copy.args.unshift("exec", "--skip-git-repo-check");
226
- else if (copy.args.includes("exec") && !copy.args.includes("--skip-git-repo-check")) copy.args.splice(copy.args.indexOf("exec") + 1, 0, "--skip-git-repo-check");
227
- const model = String(copy.model || settings.model || "").trim();
228
- const effort = ["low", "medium", "high", "xhigh", "max", "ultra"].includes(String(settings.reasoningEffort)) ? String(settings.reasoningEffort) : "";
229
- const serviceTier = /^[A-Za-z0-9._-]{1,64}$/.test(String(settings.serviceTier || "")) ? String(settings.serviceTier) : "";
230
- if (model && !copy.args.includes("--model") && !copy.args.includes("-m")) copy.args.push("--model", model);
231
- if (effort && !copy.args.some((arg) => String(arg).startsWith("model_reasoning_effort="))) copy.args.push("-c", `model_reasoning_effort="${effort}"`);
232
- if (serviceTier && !copy.args.some((arg) => String(arg).startsWith("service_tier="))) copy.args.push("-c", `service_tier="${serviceTier}"`);
233
- }
234
- if (adapter === "claude") {
235
- if (!copy.args.includes("-p") && !copy.args.includes("--print")) copy.args.unshift("-p");
236
- if (!copy.args.includes("--output-format")) copy.args.push("--output-format", "text");
237
- }
238
- if (adapter === "gemini") {
239
- // Otonom, non-interaktif calisma icin onay modu sart; yoksa izin bekleyip takilir.
240
- if (!copy.args.includes("--approval-mode") && !copy.args.includes("-y") && !copy.args.includes("--yolo")) copy.args.push("--approval-mode", "yolo");
241
- }
242
- if (adapter === "opencode") {
243
- if (!copy.args.includes("run")) copy.args.unshift("run");
244
- // config.json'da eski otomatik profil kalmis olsa da uyumsuz bayragi temizle.
245
- copy.args = copy.args.filter((arg) => arg !== "--auto");
246
- const formatAt = copy.args.indexOf("--format");
247
- if (formatAt >= 0) copy.args.splice(formatAt, 2);
248
- copy.args.splice(copy.args.indexOf("run") + 1, 0, "--format", "json");
249
- const hasModel = copy.args.includes("--model") || copy.args.includes("-m");
250
- const model = copy.model || String(settings.model || "").trim() || selectOpenCodeModel(OPEN_CODE_MODELS);
251
- if (!hasModel && model) copy.args.splice(copy.args.indexOf("run") + 3, 0, "--model", model);
252
- if (model) copy.model = model;
253
- if (!copy.args.includes("{PROMPT}") && !copy.args.includes("{PROMPT_FILE}")) {
254
- copy.args.push("Attached file contains the full task. Follow it exactly, make the changes, and report what you did.", "--file", "{PROMPT_FILE}");
255
- }
256
- }
257
- return copy;
258
- }
259
-
260
- function probeCommand(command, definition) {
261
- try {
262
- const result = spawnSync(command, definition.versionArgs, {
263
- encoding: "utf8",
264
- timeout: 12000,
265
- windowsHide: true,
266
- shell: isWin && (!path.isAbsolute(command) || /\.(cmd|bat)$/i.test(command)),
267
- });
268
- const output = String(result.stdout || result.stderr || "").trim().split(/\r?\n/)[0];
269
- return { installed: result.status === 0, version: result.status === 0 ? output : "", error: result.status === 0 ? "" : output, resolvedCommand: result.status === 0 ? command : "" };
270
- } catch (error) {
271
- return { installed: false, version: "", error: error.message, resolvedCommand: "" };
272
- }
273
- }
274
-
275
- function probe(id, definition) {
276
- let result = probeCommand(definition.command, definition);
277
- let installedPath = "";
278
- if (!result.installed) {
279
- for (const candidate of commonCandidates(id)) {
280
- if (!fs.existsSync(candidate)) continue;
281
- if (!installedPath) installedPath = candidate;
282
- result = probeCommand(candidate, definition);
283
- if (result.installed) break;
284
- }
285
- // Bazi CLI'lar (or. gemini) --version cagrisinda cmd.exe uzerinden yavas acildigi icin
286
- // zaman asimina ugrar. Ikili dosya diskte mevcutsa, versiyon okunamasa bile "kurulu" say;
287
- // aksi halde kurulu CLI yanlislikla devre disi birakiliyordu.
288
- if (!result.installed && installedPath) {
289
- result = { installed: true, version: "kurulu", error: "", resolvedCommand: installedPath };
290
- }
291
- }
292
- if (result.installed) RESOLVED.set(id, result.resolvedCommand);
293
- if (result.installed && id === "opencode") {
294
- OPEN_CODE_MODELS = listOpenCodeModels(result.resolvedCommand || definition.command);
295
- result.models = OPEN_CODE_MODELS.slice();
296
- result.recommendedModel = selectOpenCodeModel(OPEN_CODE_MODELS);
297
- result.ready = Boolean(result.recommendedModel);
298
- result.readinessError = result.ready ? "" : (OPEN_CODE_MODELS.length
299
- ? "Kullanilabilir otomatik OpenCode modeli bulunamadi. Ayarlardan erisilebilir bir model secin."
300
- : "OpenCode kurulu, ancak model listesi okunamadi. Once saglayici girisini tamamlayin.");
301
- }
302
- return result;
303
- }
304
-
305
- function discoverInstalled() {
306
- return Object.entries(DEFINITIONS).map(([id, definition]) => ({ id, ...definition, ...probe(id, definition) }));
307
- }
308
-
309
- // Tek kaynak: hem gercek gorev calistirici (engine.runCli) hem saglik testi ve model
310
- // listeleme ayni komut kurulumunu kullanir. Bare npm CLI adlarini where.exe ile fiziksel
311
- // yola cevirmiyoruz; cmd.exe PATHEXT/PATH cozumlemesini Unicode olarak kendisi yapar.
312
- function buildCommand(command, args) {
313
- if (!isWin) return { file: command, args, shell: false };
314
- const bare = !path.isAbsolute(command) && !/[\\/]/.test(command);
315
- if (bare) return { file: command, args, shell: true };
316
- if (!/\.(cmd|bat)$/i.test(command)) return { file: command, args, shell: false };
317
- const quote = (value) => `"${String(value).replace(/"/g, '""')}"`;
318
- return { file: process.env.ComSpec || "cmd.exe", args: ["/d", "/s", "/c", `"${[command, ...args].map(quote).join(" ")}"`], shell: false, verbatim: true };
319
- }
320
-
321
- function healthPrompt(id) {
322
- return [
323
- "CLI_TEAM_HEALTH_CHECK",
324
- `Bu ${id} cli aracinin kullanilabilirlik testidir.`,
325
- "Yalnizca HEALTH_OK yaz.",
326
- "Dosya olusturma, degistirme, silme, komut calistirma veya dis sisteme istek gonderme.",
327
- ].join("\n");
328
- }
329
-
330
- // Normal gorev calistiricisi {PROMPT}/{PROMPT_FILE} yer tutucularini cagridan once
331
- // doldurur. Health check de ayni sozlesmeyi kullanmali; aksi halde OpenCode literal
332
- // `{PROMPT_FILE}` yolunu acmaya calisir ve saglikli agent katalogdan yanlislikla elenir.
333
- function preparePromptArgs(args, prompt) {
334
- let tempDir = "", promptFile = "", useStdin = true;
335
- const prepared = (Array.isArray(args) ? args : []).map((arg) => {
336
- const value = String(arg);
337
- if (value.includes("{PROMPT}")) {
338
- useStdin = false;
339
- return value.replaceAll("{PROMPT}", prompt);
340
- }
341
- if (value.includes("{PROMPT_FILE}")) {
342
- useStdin = false;
343
- if (!promptFile) {
344
- tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "cli-team-health-"));
345
- promptFile = path.join(tempDir, "prompt.md");
346
- fs.writeFileSync(promptFile, prompt, "utf8");
347
- }
348
- return value.replaceAll("{PROMPT_FILE}", promptFile);
349
- }
350
- return value;
351
- });
352
- return {
353
- args: prepared,
354
- useStdin,
355
- promptFile,
356
- cleanup() { if (tempDir) { try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch {} } },
357
- };
358
- }
359
-
360
- // Tek kaynak: OpenCode icin otonom izinler surumden bagimsiz olarak OPENCODE_CONFIG_CONTENT
361
- // ile aktarilir. Hem engine.runCli hem saglik testi bu ortami kullanir ki test ile gercek
362
- // calistirma ayni izinlerle calissin.
363
- function agentEnvironment(agent) {
364
- const env = { ...process.env };
365
- if (agent.adapter !== "opencode") return env;
366
- let inline = {};
367
- try { inline = JSON.parse(env.OPENCODE_CONFIG_CONTENT || "{}"); } catch {}
368
- env.OPENCODE_CONFIG_CONTENT = JSON.stringify({ ...inline, permission: { "*": "allow" } });
369
- return env;
370
- }
371
-
372
- function classifyHealthFailure(text) {
373
- const raw = String(text || "");
374
- if (/requires a newer version|upgrade to the latest|version.*unsupported|unsupported.*version|model metadata.*not found|unsupported.*model/i.test(raw)) return { status: "version-incompatible", label: "Sürüm uyumsuz", detail: raw };
375
- if (/api key|unauthorized|unauthenticated|authentication|login required|not logged in|sign in|giriÅŸ gerekli/i.test(raw)) return { status: "auth-required", label: "Giriş gerekli", detail: raw };
376
- if (/rate.?limit|quota|too many requests|billing|credit/i.test(raw)) return { status: "quota", label: "Kota veya ödeme gerekli", detail: raw };
377
- if (/timeout|timed out|zaman aşım/i.test(raw)) return { status: "timeout", label: "Yanıt zaman aşımına uğradı", detail: raw };
378
- return { status: "failed", label: "Test başarısız", detail: raw };
379
- }
380
-
381
- function testInstalledCli(id, options = {}) {
382
- const definition = DEFINITIONS[id];
383
- if (!definition) return Promise.resolve({ id, installed: false, health: { status: "unknown", label: "Bilinmiyor", detail: "Tanımsız CLI" } });
384
- const found = probe(id, definition);
385
- if (!found.installed) return Promise.resolve({ id, installed: false, version: found.version, error: found.error, health: { status: "not-installed", label: "Kurulu değil", detail: found.error || "CLI bulunamadı" } });
386
- const agent = effectiveAgent({ adapter: id, cmd: definition.command, args: definition.defaultArgs.slice() }, options.cfg);
387
- const prompt = healthPrompt(id);
388
- const prepared = preparePromptArgs(agent.args, prompt);
389
- const command = buildCommand(agent.cmd, prepared.args);
390
- const timeoutMs = Math.max(10000, Number(options.timeoutMs || 45000));
391
- return new Promise((resolve) => {
392
- let stdout = "", stderr = "", settled = false;
393
- let child;
394
- try {
395
- child = spawn(command.file, command.args, { env: agentEnvironment(agent), shell: command.shell, windowsHide: true, windowsVerbatimArguments: !!command.verbatim });
396
- } catch (error) {
397
- prepared.cleanup();
398
- return resolve({ id, installed: true, version: found.version, health: classifyHealthFailure(error.message) });
399
- }
400
- const unregister = registerProbe(child);
401
- const timer = setTimeout(() => {
402
- if (settled) return;
403
- settled = true;
404
- unregister();
405
- terminateProbeTree(child);
406
- prepared.cleanup();
407
- resolve({ id, installed: true, version: found.version, health: { status: "timeout", label: "Yanıt zaman aşımına uğradı", detail: `Sağlık testi ${Math.round(timeoutMs / 1000)} saniyede tamamlanmadı.` } });
408
- }, timeoutMs);
409
- child.stdout?.setEncoding("utf8");
410
- child.stderr?.setEncoding("utf8");
411
- child.stdout?.on("data", (data) => { stdout += String(data); });
412
- child.stderr?.on("data", (data) => { stderr += String(data); });
413
- child.on("error", (error) => {
414
- if (settled) return;
415
- settled = true;
416
- clearTimeout(timer);
417
- unregister();
418
- prepared.cleanup();
419
- const failure = classifyHealthFailure(error.message);
420
- resolve({ id, installed: true, version: found.version, health: failure });
421
- });
422
- child.on("close", (code) => {
423
- if (settled) return;
424
- settled = true;
425
- clearTimeout(timer);
426
- unregister();
427
- prepared.cleanup();
428
- const raw = `${stdout}\n${stderr}`.trim();
429
- if (code === 0 && /HEALTH_OK/i.test(raw)) resolve({ id, installed: true, version: found.version, health: { status: "ready", label: "Hazır", detail: "Gerçek sağlık testi başarılı." } });
430
- else resolve({ id, installed: true, version: found.version, health: classifyHealthFailure(raw || `Çıkış kodu ${code}`) });
431
- });
432
- child.stdin?.on("error", () => {});
433
- child.stdin?.end(prepared.useStdin ? prompt : "");
434
- });
435
- }
436
-
437
- async function healthCheckAll(discovered = discoverInstalled(), options = {}) {
438
- const results = await Promise.all(discovered.map((item) => testInstalledCli(item.id, options)));
439
- return discovered.map((item) => ({ ...item, ...(results.find((result) => result.id === item.id) || {}) }));
440
- }
441
-
442
- function listCodexModels(options = {}) {
443
- const command = buildCommand("codex", ["app-server", "--listen", "stdio://"]);
444
- const timeoutMs = Math.max(5000, Number(options.timeoutMs || 20000));
445
- return new Promise((resolve, reject) => {
446
- let buffer = "", settled = false, modelRequestSent = false;
447
- let child, unregister = () => {};
448
- const finish = (error, value) => {
449
- if (settled) return;
450
- settled = true;
451
- clearTimeout(timer);
452
- unregister();
453
- terminateProbeTree(child);
454
- if (error) reject(error); else resolve(value);
455
- };
456
- const timer = setTimeout(() => finish(new Error(`Codex model listesi ${Math.round(timeoutMs / 1000)} saniyede alınamadı.`)), timeoutMs);
457
- try { child = spawn(command.file, command.args, { shell: command.shell, windowsHide: true, windowsVerbatimArguments: !!command.verbatim }); }
458
- catch (error) { finish(error); return; }
459
- unregister = registerProbe(child);
460
- child.stdout?.setEncoding("utf8");
461
- child.stderr?.setEncoding("utf8");
462
- child.stdout?.on("data", (chunk) => {
463
- buffer += String(chunk);
464
- const lines = buffer.split(/\r?\n/);
465
- buffer = lines.pop() || "";
466
- for (const line of lines) {
467
- let message;
468
- try { message = JSON.parse(line); } catch { continue; }
469
- if (message.id === 1 && !modelRequestSent) {
470
- modelRequestSent = true;
471
- child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: 2, method: "model/list", params: { includeHidden: false, limit: 200 } })}\n`);
472
- } else if (message.id === 2) {
473
- if (message.error) { finish(new Error(message.error.message || "Codex model listesi alınamadı.")); return; }
474
- const models = Array.isArray(message.result?.data) ? message.result.data : [];
475
- finish(null, models.filter((model) => model && model.hidden !== true).map((model) => ({
476
- id: String(model.id || model.model || ""),
477
- model: String(model.model || model.id || ""),
478
- displayName: String(model.displayName || model.id || model.model || ""),
479
- description: String(model.description || ""),
480
- defaultReasoningEffort: String(model.defaultReasoningEffort || "medium"),
481
- reasoningEfforts: (model.supportedReasoningEfforts || []).map((item) => ({
482
- id: String(item.reasoningEffort || item.id || ""),
483
- description: String(item.description || ""),
484
- })).filter((item) => item.id),
485
- serviceTiers: (model.serviceTiers || []).map((item) => ({
486
- id: String(item.id || ""),
487
- name: String(item.name || item.id || ""),
488
- description: String(item.description || ""),
489
- })).filter((item) => item.id),
490
- additionalSpeedTiers: Array.isArray(model.additionalSpeedTiers) ? model.additionalSpeedTiers.map(String) : [],
491
- defaultServiceTier: model.defaultServiceTier ? String(model.defaultServiceTier) : "",
492
- isDefault: model.isDefault === true,
493
- })).filter((model) => model.id));
494
- }
495
- }
496
- });
497
- child.stderr?.on("data", () => {});
498
- child.on("error", finish);
499
- child.stdin?.on("error", () => {});
500
- child.stdin?.write(`${JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: { clientInfo: { name: "crewctl", version: "1.0.0", title: "CrewCtl" } } })}\n`);
501
- });
502
- }
503
-
504
- function addMissingAgents(cfg, discovered) {
505
- cfg.agents ||= {};
506
- const ignored = new Set(Array.isArray(cfg.discoveryIgnoredAdapters) ? cfg.discoveryIgnoredAdapters.map(String) : []);
507
- let changed = normalizeAgentAdapters(cfg);
508
- for (const cli of discovered) {
509
- const usable = cli.installed && (cli.id !== "opencode" || cli.ready !== false || Boolean(cfg.cliSettings?.opencode?.model));
510
- const matchingEntries = Object.entries(cfg.agents).filter(([, agent]) => (agent.adapter || adapterId(agent.cmd)) === cli.id);
511
- const existingEntry = matchingEntries.find(([, agent]) => !agent.autoDiscovered) || matchingEntries[0];
512
- const existing = existingEntry?.[1];
513
- // Kullanici otomatik bulunan bir profili sildiyse bu karar kalicidir. O adapter'a ait
514
- // elle olusturulmus profil varsa ona dokunma; yalnizca autoDiscovered kaydini temizle.
515
- if (ignored.has(cli.id)) {
516
- for (const [name, agent] of matchingEntries) {
517
- if (!agent.autoDiscovered) continue;
518
- delete cfg.agents[name];
519
- changed = true;
520
- }
521
- continue;
522
- }
523
- if (existing) {
524
- if (usable && existing.autoDiscovered && existing.unavailablePlaceholder) {
525
- existing.enabled = true;
526
- existing.args = cli.defaultArgs.slice();
527
- delete existing.unavailablePlaceholder;
528
- changed = true;
529
- }
530
- // Eski surumler otomatik oneriyi agent.model alanina yaziyordu. Bu alan global
531
- // CLI ayarini ezdigi icin, kullanici tarafindan acikca override olarak isaretlenmemis
532
- // otomatik profiller modeli daima CLI ayarindan/otomatik secimden miras alir.
533
- if (cli.id === "opencode" && existing.autoDiscovered && existing.model && existing.modelOverride !== true) {
534
- delete existing.model;
535
- changed = true;
536
- }
537
- if (cli.installed && cli.id === "opencode" && existing.autoDiscovered && Number(existing.timeoutSeconds || 0) < cli.timeoutSeconds) {
538
- existing.timeoutSeconds = cli.timeoutSeconds;
539
- changed = true;
540
- }
541
- if (!usable && existing.autoDiscovered && existing.enabled !== false) {
542
- existing.enabled = false;
543
- existing.unavailablePlaceholder = true;
544
- changed = true;
545
- }
546
- continue;
547
- }
548
- // OpenCode adapteri her cihazda katalogda gorunur. Kurulu degilse guvenli
549
- // sekilde devre disi placeholder olur; taramada bulundugunda otomatik etkinlesir.
550
- if (!cli.installed && cli.id !== "opencode") continue;
551
- let name = `${cli.id}-auto`, suffix = 2;
552
- while (cfg.agents[name]) name = `${cli.id}-auto-${suffix++}`;
553
- cfg.agents[name] = {
554
- adapter: cli.id,
555
- cmd: cli.command,
556
- args: cli.defaultArgs.slice(),
557
- enabled: usable,
558
- description: cli.description,
559
- capabilities: cli.capabilities.slice(),
560
- roleFile: cli.roleFile,
561
- costTier: "standard",
562
- timeoutSeconds: cli.timeoutSeconds || 1200,
563
- ...(cli.silenceTimeoutSeconds ? { silenceTimeoutSeconds: cli.silenceTimeoutSeconds } : {}),
564
- autoDiscovered: true,
565
- ...(!usable ? { unavailablePlaceholder: true } : {}),
566
- };
567
- changed = true;
568
- }
569
- return changed;
570
- }
571
-
572
- // Operator, uzman agent'lardan BAGIMSIZ bir CLI secimidir (claude/codex/gemini/opencode).
573
- // operator.md rolunu bu CLI giyer; uzman agent'lar operatorun ALTINDA delege edilir.
574
- // Bir uzman agent silinse bile operator etkilenmez.
575
- function operatorSpec(cli, cfg) {
576
- const op = (cfg && cfg.operator) || {};
577
- const definition = DEFINITIONS[cli];
578
- // Standart CLI'lar DEFINITIONS'tan cozulur. cfg.operator.cmd verilirse (ozel/test operatoru)
579
- // dogrudan o komut kullanilir; boylece operator, uzman agent'lardan tamamen bagimsiz kalir.
580
- const cmd = op.cmd || (definition && definition.command);
581
- if (!cmd) return null;
582
- const args = op.cmd ? (Array.isArray(op.args) ? op.args.slice() : []) : (definition ? definition.defaultArgs.slice() : []);
583
- // Model/effort/tier secimi CLI bazlidir (cfg.cliSettings) ve effectiveAgent icinde uygulanir;
584
- // ayni ayar o CLI'yi kullanan operator ve tum uzman agent'lar icin gecerlidir.
585
- const timeoutSeconds = op.timeoutSeconds || (cfg && cfg.agentTimeoutSeconds) || 900;
586
- const silenceTimeoutSeconds = op.silenceTimeoutSeconds || (definition && definition.silenceTimeoutSeconds) || (cfg && cfg.cliSilenceTimeoutSeconds) || 300;
587
- const adapter = definition ? cli : (op.adapter || adapterId(cmd));
588
- return effectiveAgent({ adapter, cmd, args, timeoutSeconds, silenceTimeoutSeconds }, cfg);
589
- }
590
-
591
- // Operator gecerli, kurulu bir CLI olmali. Eski semada operator.agent (uzman agent adi)
592
- // tutuluyordu; burada operator.cli'ye tasinir. Kurulu degilse ilk kurulu CLI'ye gecilir.
593
- function ensureValidOperator(cfg, discovered) {
594
- cfg.operator ||= {};
595
- let changed = false;
596
- if (cfg.operator.roleFile !== "roles/operator.md") { cfg.operator.roleFile = "roles/operator.md"; changed = true; }
597
- if (!cfg.operator.cli) {
598
- const legacy = cfg.operator.agent;
599
- const source = legacy && cfg.agents?.[legacy] ? cfg.agents[legacy].cmd : legacy;
600
- cfg.operator.cli = adapterId(source || "codex");
601
- changed = true;
602
- }
603
- if ("agent" in cfg.operator) { delete cfg.operator.agent; changed = true; }
604
- const installed = Array.isArray(discovered) ? discovered
605
- .filter((x) => x.installed && (x.id !== "opencode" || x.ready !== false || Boolean(cfg.cliSettings?.opencode?.model)))
606
- .map((x) => x.id) : null;
607
- const known = !!DEFINITIONS[cfg.operator.cli];
608
- const availableOnHost = !installed || installed.length === 0 || installed.includes(cfg.operator.cli);
609
- if (!known || !availableOnHost) {
610
- const fallback = (installed && installed[0]) || Object.keys(DEFINITIONS)[0];
611
- if (fallback && fallback !== cfg.operator.cli) { cfg.operator.cli = fallback; changed = true; }
612
- }
613
- return changed;
614
- }
615
-
616
- module.exports = { DEFINITIONS, KNOWN_CLIS: Object.keys(DEFINITIONS), adapterId, normalizeAgentAdapter, normalizeAgentAdapters, effectiveAgent, preparePromptArgs, operatorSpec, buildCommand, agentEnvironment, discoverInstalled, healthCheckAll, listCodexModels, abortActiveProbes, selectOpenCodeModel, parseOpenCodeModels, addMissingAgents, ensureValidOperator };
1
+ const { spawn, spawnSync } = require("child_process");
2
+ const fs = require("fs");
3
+ const os = require("os");
4
+ const path = require("path");
5
+
6
+ const isWin = process.platform === "win32";
7
+
8
+ const DEFINITIONS = {
9
+ codex: {
10
+ command: "codex",
11
+ versionArgs: ["--version"],
12
+ defaultArgs: ["exec", "--skip-git-repo-check"],
13
+ // Codex calisirken surekli ilerleme akitir; 3 dakikalik sessizlik gercek bir takilmadir.
14
+ silenceTimeoutSeconds: 180,
15
+ description: "OpenAI Codex kodlama, hata ayiklama ve inceleme agenti",
16
+ capabilities: ["implementation", "debugging", "testing", "review", "analysis"],
17
+ roleFile: "roles/executor.md",
18
+ },
19
+ claude: {
20
+ command: "claude",
21
+ versionArgs: ["--version"],
22
+ defaultArgs: ["-p", "--permission-mode", "acceptEdits", "--output-format", "text"],
23
+ description: "Claude Code uygulama, analiz ve test agenti",
24
+ capabilities: ["implementation", "debugging", "testing", "review", "web"],
25
+ roleFile: "roles/executor.md",
26
+ },
27
+ gemini: {
28
+ command: "gemini",
29
+ versionArgs: ["--version"],
30
+ // Argumansiz gemini interaktif TUI acar ve izin bekler; yolo ile otonom, stdin'den prompt alir.
31
+ defaultArgs: ["--approval-mode", "yolo"],
32
+ // Gemini de akis halinde cikti verir; uzun sessizlik takilma isaretidir.
33
+ silenceTimeoutSeconds: 180,
34
+ description: "Gemini CLI analiz, uygulama ve web arastirma agenti",
35
+ capabilities: ["implementation", "planning", "analysis", "research", "web"],
36
+ roleFile: "roles/executor.md",
37
+ },
38
+ opencode: {
39
+ command: "opencode",
40
+ versionArgs: ["--version"],
41
+ // OpenCode saglayici/model kombinasyonuna gore uzun sure sessiz kalabilir. Varsayilan
42
+ // agent timeout'u, basarili bir calismayi SIGTERM ile yarida kesmeyecek kadar genis tut.
43
+ timeoutSeconds: 1800,
44
+ // OpenCode adim baslattiktan sonra model/provider yanitini beklerken uzun sure hic cikti
45
+ // akitmayabilir (ozellikle yavas/rate-limitli modeller). 180sn cok agresifti ve calisan bir
46
+ // kosmayi yariyordu; sessizlik payini genis tut. Gercek takilma yine timeoutSeconds ile yakalanir.
47
+ silenceTimeoutSeconds: 300,
48
+ // Bazi eski OpenCode surumleri --auto bayragini tanimaz. Otonom izinler
49
+ // engine tarafinda OPENCODE_CONFIG_CONTENT ile surumden bagimsiz aktarilir.
50
+ defaultArgs: ["run", "--format", "json", "Attached file contains the full task. Follow it exactly, make the changes, and report what you did.", "--file", "{PROMPT_FILE}"],
51
+ description: "OpenCode coklu saglayici kodlama, uygulama ve inceleme agenti",
52
+ capabilities: ["implementation", "debugging", "testing", "review", "analysis", "research"],
53
+ roleFile: "roles/executor.md",
54
+ install: {
55
+ windows: "npm install -g opencode-ai (alternatif: choco install opencode / scoop install opencode)",
56
+ macos: "brew install anomalyco/tap/opencode (alternatif: npm install -g opencode-ai)",
57
+ linux: "curl -fsSL https://opencode.ai/install | bash (alternatif: npm install -g opencode-ai)",
58
+ auth: "opencode auth login",
59
+ },
60
+ },
61
+ };
62
+ const RESOLVED = new Map();
63
+ let OPEN_CODE_MODELS = [];
64
+
65
+ // En fazla bir CLI'yi (or. codex) ayni anda hem canli saglik/model probe'u hem de motorun
66
+ // operatör kosumu calistirirsa, codex gibi bazi CLI'lar ayni is dizininde ikinci exec oturumunu
67
+ // SIGTERM ile keser. Probe'lar "en iyi caba"dir; motor onceliklidir. Aktif probe cocuklarini
68
+ // izleyip motor bir goreve baslarken hepsini sonlandiriyoruz.
69
+ const ACTIVE_PROBES = new Set();
70
+ function registerProbe(child) {
71
+ if (!child) return () => {};
72
+ ACTIVE_PROBES.add(child);
73
+ return () => ACTIVE_PROBES.delete(child);
74
+ }
75
+ // Uctan uca proses agacini oldur (codex kabuk altinda alt proses baslatabilir).
76
+ function terminateProbeTree(child) {
77
+ if (!child) return;
78
+ try {
79
+ if (isWin && child.pid) spawn("taskkill", ["/pid", String(child.pid), "/t", "/f"], { windowsHide: true, stdio: "ignore" });
80
+ else child.kill("SIGKILL");
81
+ } catch {}
82
+ }
83
+ // Motor bir goreve baslarken cagirir: uctaki tum canli probe'lari sonlandirir ki operatörun
84
+ // CLI'si (codex vb.) temiz bir alanda calissin. Probe'lar sonuc olarak "timeout/failed" doner;
85
+ // bu zararsizdir, motor bosaldiginda yeniden denenir.
86
+ // Iptal sayaci: motor bir goreve baslarken probe'lari oldurdugunde artar. Oldurulen probe
87
+ // "cikis kodu != 0" ile doner ve saglik siniflandiricisi bunu GERCEK bir ariza sanardi;
88
+ // sonuc 6 saatlik onbellege yazilinca CLI o sure boyunca yanlislikla kullanilamaz kalirdi.
89
+ // refreshCliHealth bu sayaci once/sonra karsilastirip kirlenmis sonucu ONBELLEGE YAZMAZ.
90
+ let probeGeneration = 0;
91
+ function currentProbeGeneration() {
92
+ return probeGeneration;
93
+ }
94
+
95
+ function abortActiveProbes() {
96
+ const count = ACTIVE_PROBES.size;
97
+ if (count) probeGeneration++;
98
+ for (const child of ACTIVE_PROBES) terminateProbeTree(child);
99
+ ACTIVE_PROBES.clear();
100
+ return count;
101
+ }
102
+
103
+ function selectOpenCodeModel(models) {
104
+ const list = Array.isArray(models) ? models.map(String).filter(Boolean) : [];
105
+ const priorities = [
106
+ (x) => x === "opencode/big-pickle",
107
+ (x) => /^opencode\/.+free$/i.test(x),
108
+ (x) => x.startsWith("opencode/"),
109
+ (x) => x.startsWith("opencode-go/"),
110
+ (x) => x.startsWith("minimax-coding-plan/"),
111
+ (x) => !x.startsWith("ollama/"),
112
+ ];
113
+ for (const match of priorities) {
114
+ const found = list.find(match);
115
+ if (found) return found;
116
+ }
117
+ return "";
118
+ }
119
+
120
+ // `opencode models` ciktisindan saglayici/model satirlarini ayiklar. Baslik, bos satir ve
121
+ // log gurultusunu eler; her "saglayici/model" bicimli satir gecerli sayilir.
122
+ function parseOpenCodeModels(stdout) {
123
+ const found = new Set();
124
+ for (const line of String(stdout || "").split(/\r?\n/)) {
125
+ const model = line.trim();
126
+ if (/^[A-Za-z0-9._-]+\/.+/.test(model) && !/\s/.test(model)) found.add(model);
127
+ }
128
+ return [...found];
129
+ }
130
+
131
+ function listOpenCodeModels(command) {
132
+ try {
133
+ // Tum saglayicilarin listesi sorgulanir. Kullanicilarin ucretli abonelikleri
134
+ // (opencode-go, minimax-coding-plan vb.) ayri saglayici olarak gorunur; yalnizca
135
+ // "opencode" saglayicisini sorgulamak bu modelleri gizliyor ve kurulum yanlislikla
136
+ // "hazir degil" sayiliyordu. Otomatik oneri siralamasini selectOpenCodeModel yapar.
137
+ const result = spawnSync(command, ["models"], {
138
+ encoding: "utf8", timeout: 20000, windowsHide: true,
139
+ shell: isWin && (!path.isAbsolute(command) || /\.(cmd|bat)$/i.test(command)),
140
+ });
141
+ if (result.status !== 0) return [];
142
+ return parseOpenCodeModels(result.stdout);
143
+ } catch { return []; }
144
+ }
145
+
146
+ function adapterId(command) {
147
+ const base = path.basename(String(command || "")).toLowerCase().replace(/\.(cmd|bat|exe|ps1)$/, "");
148
+ if (base.includes("codex")) return "codex";
149
+ if (base.includes("claude")) return "claude";
150
+ if (base.includes("gemini")) return "gemini";
151
+ if (base.includes("opencode")) return "opencode";
152
+ return "custom";
153
+ }
154
+
155
+ // Farkli paket yoneticileri / kurulum araclari CLI'lari farkli dizinlere koyar. Her PC'de
156
+ // calismasi icin yaygin npm/pnpm/yarn/bun/volta/scoop/winget/choco/homebrew konumlarini tarariz.
157
+ function commonCandidates(id) {
158
+ const home = os.homedir();
159
+ if (isWin) {
160
+ const APPDATA = process.env.APPDATA, LOCAL = process.env.LOCALAPPDATA, PD = process.env.ProgramData;
161
+ const exts = ["cmd", "exe", "ps1", "bat", ""];
162
+ const bases = [
163
+ APPDATA && path.join(APPDATA, "npm"),
164
+ LOCAL && path.join(LOCAL, "pnpm"),
165
+ LOCAL && path.join(LOCAL, "Yarn", "bin"),
166
+ LOCAL && path.join(LOCAL, "Microsoft", "WinGet", "Links"),
167
+ LOCAL && path.join(LOCAL, "Programs", id),
168
+ path.join(home, ".bun", "bin"),
169
+ path.join(home, ".volta", "bin"),
170
+ path.join(home, "scoop", "shims"),
171
+ PD && path.join(PD, "chocolatey", "bin"),
172
+ path.join(home, ".local", "bin"),
173
+ path.join(home, "AppData", "Roaming", "npm"),
174
+ ].filter(Boolean);
175
+ const out = [];
176
+ for (const base of bases) for (const ext of exts) out.push(path.join(base, ext ? `${id}.${ext}` : id));
177
+ return out;
178
+ }
179
+ return [
180
+ path.join(home, ".local", "bin", id),
181
+ path.join(home, "bin", id),
182
+ path.join(home, ".bun", "bin", id),
183
+ path.join(home, ".volta", "bin", id),
184
+ path.join(home, ".npm-global", "bin", id),
185
+ path.join(home, ".yarn", "bin", id),
186
+ path.join(home, ".local", "share", "pnpm", id),
187
+ `/usr/local/bin/${id}`,
188
+ `/opt/homebrew/bin/${id}`,
189
+ `/usr/bin/${id}`,
190
+ `/snap/bin/${id}`,
191
+ ];
192
+ }
193
+
194
+ // Bilinen bir CLI komutu adapter alanindan daha guvenilir kaynaktir. Eski veya elle
195
+ // duzenlenmis config'te adapter=claude + cmd=codex gibi bir celiski varsa hedef CLI'nin
196
+ // guvenli varsayilan argumanlarina donulur; boylece bir CLI'ye digerinin bayragi gitmez.
197
+ function normalizeAgentAdapter(agent) {
198
+ if (!agent || typeof agent !== "object") return false;
199
+ const configured = String(agent.adapter || "").trim().toLowerCase();
200
+ const inferred = adapterId(agent.cmd);
201
+ const adapter = inferred !== "custom" ? inferred : (DEFINITIONS[configured] ? configured : "custom");
202
+ let changed = false;
203
+ if (inferred !== "custom" && configured && configured !== inferred) {
204
+ agent.args = DEFINITIONS[inferred].defaultArgs.slice();
205
+ delete agent.model;
206
+ delete agent.modelOverride;
207
+ changed = true;
208
+ }
209
+ if (agent.adapter !== adapter) {
210
+ agent.adapter = adapter;
211
+ changed = true;
212
+ }
213
+ return changed;
214
+ }
215
+
216
+ function normalizeAgentAdapters(cfg) {
217
+ if (!cfg?.agents || typeof cfg.agents !== "object") return false;
218
+ let changed = false;
219
+ for (const agent of Object.values(cfg.agents)) if (normalizeAgentAdapter(agent)) changed = true;
220
+ return changed;
221
+ }
222
+
223
+ // Ajan hapsi acik mi? cfg.sandbox.mode === "workspace" ise CLI'lar calisma klasoru DISINA
224
+ // yazamaz/aga cikamaz. cfg verilmeyen (test/edge) cagrilar hapissiz kalir; boylece mevcut
225
+ // arguman-esitlik testleri aynen gecer. Gercek calistirmada normalizeConfig mode'u daima
226
+ // doldurur (varsayilan "workspace").
227
+ function confineWorkspace(cfg) {
228
+ return cfg?.sandbox?.mode === "workspace";
229
+ }
230
+ function sandboxWritableRoots(cfg) {
231
+ const roots = cfg?.sandbox?.extraWritableDirs;
232
+ return Array.isArray(roots) ? roots.filter((x) => typeof x === "string" && x.trim()).map(String) : [];
233
+ }
234
+
235
+ // cfg verilirse cfg.cliSettings[adapter] o CLI'nin varsayilan model ayari olarak uygulanir.
236
+ // Oncelik: agent.model (profil bazli) > cliSettings[adapter].model (global) > CLI varsayilani.
237
+ function effectiveAgent(agent, cfg) {
238
+ const copy = { ...agent, args: Array.isArray(agent.args) ? agent.args.map(String) : [] };
239
+ normalizeAgentAdapter(copy);
240
+ const adapter = copy.adapter;
241
+ const commandAdapter = adapterId(copy.cmd);
242
+ const settings = (cfg && cfg.cliSettings && cfg.cliSettings[adapter]) || {};
243
+ if (!copy.silenceTimeoutSeconds && DEFINITIONS[adapter]?.silenceTimeoutSeconds) copy.silenceTimeoutSeconds = DEFINITIONS[adapter].silenceTimeoutSeconds;
244
+ if (commandAdapter === adapter && RESOLVED.has(adapter) && !path.isAbsolute(copy.cmd)) copy.cmd = RESOLVED.get(adapter);
245
+ if (adapter === "codex") {
246
+ const commands = new Set(["exec", "review", "resume", "apply"]);
247
+ if (!copy.args.some((arg) => commands.has(arg))) copy.args.unshift("exec", "--skip-git-repo-check");
248
+ else if (copy.args.includes("exec") && !copy.args.includes("--skip-git-repo-check")) copy.args.splice(copy.args.indexOf("exec") + 1, 0, "--skip-git-repo-check");
249
+ // Hapis: calisma klasoru (spawn cwd = codex "workspace") disina yazma ve ag engellenir.
250
+ // Native sandbox mac Seatbelt / Linux Landlock+seccomp / Windows restricted-token+ACL —
251
+ // Docker/Git GEREKTIRMEZ. Kullanici acikca kendi -s/--sandbox veya bypass bayragini
252
+ // koyduysa ona dokunmayiz. Bayrak, model/effort/tier PUSH'undan ONCE ve exec/skip-git'ten
253
+ // HEMEN SONRA eklenir; testlerin args.slice(0,2) ve slice(-6) beklentileri korunur.
254
+ const alreadySandboxed = copy.args.some((arg) => arg === "-s" || arg === "--sandbox" || arg === "--dangerously-bypass-approvals-and-sandbox" || arg === "--full-auto");
255
+ if (confineWorkspace(cfg) && !alreadySandboxed) {
256
+ const anchor = copy.args.indexOf("--skip-git-repo-check");
257
+ const at = anchor >= 0 ? anchor + 1 : Math.max(copy.args.indexOf("exec") + 1, 0);
258
+ const flags = ["-s", "workspace-write"];
259
+ const roots = sandboxWritableRoots(cfg);
260
+ if (roots.length) flags.push("-c", `sandbox_workspace_write.writable_roots=${JSON.stringify(roots)}`);
261
+ copy.args.splice(at, 0, ...flags);
262
+ }
263
+ const model = String(copy.model || settings.model || "").trim();
264
+ const effort = ["low", "medium", "high", "xhigh", "max", "ultra"].includes(String(settings.reasoningEffort)) ? String(settings.reasoningEffort) : "";
265
+ const serviceTier = /^[A-Za-z0-9._-]{1,64}$/.test(String(settings.serviceTier || "")) ? String(settings.serviceTier) : "";
266
+ if (model && !copy.args.includes("--model") && !copy.args.includes("-m")) copy.args.push("--model", model);
267
+ if (effort && !copy.args.some((arg) => String(arg).startsWith("model_reasoning_effort="))) copy.args.push("-c", `model_reasoning_effort="${effort}"`);
268
+ if (serviceTier && !copy.args.some((arg) => String(arg).startsWith("service_tier="))) copy.args.push("-c", `service_tier="${serviceTier}"`);
269
+ }
270
+ if (adapter === "claude") {
271
+ if (!copy.args.includes("-p") && !copy.args.includes("--print")) copy.args.unshift("-p");
272
+ if (!copy.args.includes("--output-format")) copy.args.push("--output-format", "text");
273
+ }
274
+ if (adapter === "gemini") {
275
+ // Otonom, non-interaktif calisma icin onay modu sart; yoksa izin bekleyip takilir.
276
+ if (!copy.args.includes("--approval-mode") && !copy.args.includes("-y") && !copy.args.includes("--yolo")) copy.args.push("--approval-mode", "yolo");
277
+ }
278
+ if (adapter === "opencode") {
279
+ if (!copy.args.includes("run")) copy.args.unshift("run");
280
+ // config.json'da eski otomatik profil kalmis olsa da uyumsuz bayragi temizle.
281
+ copy.args = copy.args.filter((arg) => arg !== "--auto");
282
+ const formatAt = copy.args.indexOf("--format");
283
+ if (formatAt >= 0) copy.args.splice(formatAt, 2);
284
+ copy.args.splice(copy.args.indexOf("run") + 1, 0, "--format", "json");
285
+ const hasModel = copy.args.includes("--model") || copy.args.includes("-m");
286
+ const model = copy.model || String(settings.model || "").trim() || selectOpenCodeModel(OPEN_CODE_MODELS);
287
+ if (!hasModel && model) copy.args.splice(copy.args.indexOf("run") + 3, 0, "--model", model);
288
+ if (model) copy.model = model;
289
+ if (!copy.args.includes("{PROMPT}") && !copy.args.includes("{PROMPT_FILE}")) {
290
+ copy.args.push("Attached file contains the full task. Follow it exactly, make the changes, and report what you did.", "--file", "{PROMPT_FILE}");
291
+ }
292
+ }
293
+ return copy;
294
+ }
295
+
296
+ function probeCommand(command, definition) {
297
+ try {
298
+ const result = spawnSync(command, definition.versionArgs, {
299
+ encoding: "utf8",
300
+ timeout: 12000,
301
+ windowsHide: true,
302
+ shell: isWin && (!path.isAbsolute(command) || /\.(cmd|bat)$/i.test(command)),
303
+ });
304
+ const output = String(result.stdout || result.stderr || "").trim().split(/\r?\n/)[0];
305
+ return { installed: result.status === 0, version: result.status === 0 ? output : "", error: result.status === 0 ? "" : output, resolvedCommand: result.status === 0 ? command : "" };
306
+ } catch (error) {
307
+ return { installed: false, version: "", error: error.message, resolvedCommand: "" };
308
+ }
309
+ }
310
+
311
+ function probe(id, definition) {
312
+ let result = probeCommand(definition.command, definition);
313
+ let installedPath = "";
314
+ if (!result.installed) {
315
+ for (const candidate of commonCandidates(id)) {
316
+ if (!fs.existsSync(candidate)) continue;
317
+ if (!installedPath) installedPath = candidate;
318
+ result = probeCommand(candidate, definition);
319
+ if (result.installed) break;
320
+ }
321
+ // Bazi CLI'lar (or. gemini) --version cagrisinda cmd.exe uzerinden yavas acildigi icin
322
+ // zaman asimina ugrar. Ikili dosya diskte mevcutsa, versiyon okunamasa bile "kurulu" say;
323
+ // aksi halde kurulu CLI yanlislikla devre disi birakiliyordu.
324
+ if (!result.installed && installedPath) {
325
+ result = { installed: true, version: "kurulu", error: "", resolvedCommand: installedPath };
326
+ }
327
+ }
328
+ if (result.installed) RESOLVED.set(id, result.resolvedCommand);
329
+ if (result.installed && id === "opencode") {
330
+ OPEN_CODE_MODELS = listOpenCodeModels(result.resolvedCommand || definition.command);
331
+ result.models = OPEN_CODE_MODELS.slice();
332
+ result.recommendedModel = selectOpenCodeModel(OPEN_CODE_MODELS);
333
+ result.ready = Boolean(result.recommendedModel);
334
+ result.readinessError = result.ready ? "" : (OPEN_CODE_MODELS.length
335
+ ? "Kullanilabilir otomatik OpenCode modeli bulunamadi. Ayarlardan erisilebilir bir model secin."
336
+ : "OpenCode kurulu, ancak model listesi okunamadi. Once saglayici girisini tamamlayin.");
337
+ }
338
+ return result;
339
+ }
340
+
341
+ // Her acilista tum CLI'lar --version ile yoklaniyordu; olculen maliyet 4.8 saniye ve panel
342
+ // bu sure boyunca hic acilmiyordu. Onbellek YALNIZCA su kosulda kullanilir: CLI daha once
343
+ // KURULU bulunmus VE cozulmus ikili dosya HALA diskte duruyor. Bu durumda tekrar yoklamak
344
+ // zaten bilinen bir cevabi dogrulamaktan ibarettir.
345
+ //
346
+ // Bilerek onbelleklenmeyenler (dogruluk hizdan onceliklidir):
347
+ // - Daha once "kurulu degil" bulunanlar -> yeni kurulan CLI ilk acilista gorunur.
348
+ // - Diskte bulunamayan cozulmus yol -> kaldirilan CLI hemen fark edilir.
349
+ // - opencode -> model listesi/ready durumu degisebilir;
350
+ // her acilista tam yoklanir, davranisi aynen korunur.
351
+ // "Yeniden Tara" (/api/cli/discover) daima force ile cagirir ve onbellegi tumden atlar.
352
+ const DISCOVERY_CACHE_VERSION = 1;
353
+ const ALWAYS_PROBE = new Set(["opencode"]);
354
+
355
+ // probeCommand, CLI'yi PATH uzerinden buldugunda resolvedCommand'i CIPLAK AD olarak dondurur
356
+ // ("codex"), mutlak yol olarak degil. Duz fs.existsSync bu durumda daima false verir ve
357
+ // onbellek hic tutmaz. Bu yuzden ciplak adlar PATH (+ Windows'ta PATHEXT) uzerinde aranir:
358
+ // spawn maliyeti olmadan, birkac milisaniyede ve dogru sonucla.
359
+ function resolvesOnPath(command) {
360
+ if (!command) return false;
361
+ if (command.includes("/") || command.includes("\\")) return fs.existsSync(command);
362
+ const dirs = String(process.env.PATH || "").split(path.delimiter).filter(Boolean);
363
+ const exts = isWin ? String(process.env.PATHEXT || ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean) : [""];
364
+ for (const dir of dirs) {
365
+ for (const ext of exts) {
366
+ try { if (fs.existsSync(path.join(dir, command + ext))) return true; } catch {}
367
+ }
368
+ }
369
+ return false;
370
+ }
371
+
372
+ function cachedProbeUsable(entry) {
373
+ return Boolean(entry && entry.installed && entry.resolvedCommand && resolvesOnPath(entry.resolvedCommand));
374
+ }
375
+
376
+ // OpenCode model listesi persist EDILMEZ; her acilista `opencode models` ile bellekten
377
+ // uretilir. O cagri acilista gecici olarak yavas/basarisiz olursa (soguk ilk cagri, auth
378
+ // oturmamis, ag) liste bos kalir ve panel model dropdown'i o process boyunca bos gorunur.
379
+ // codex icin codexModelCache ile cozulen bu kirilganligi opencode icin de kapatiriz: prob
380
+ // bos donerse son basarili liste (config'te saklanan) fallback olarak kullanilir. Fallback
381
+ // ayni zamanda OPEN_CODE_MODELS'i doldurur ki otomatik model secimi de calissin.
382
+ function applyOpenCodeFallback(entry, fallback) {
383
+ if (!entry || entry.id !== "opencode" || !entry.installed) return entry;
384
+ if ((entry.models && entry.models.length) || !Array.isArray(fallback) || !fallback.length) return entry;
385
+ const models = fallback.map(String).filter(Boolean);
386
+ if (!models.length) return entry;
387
+ OPEN_CODE_MODELS = models.slice();
388
+ entry.models = models.slice();
389
+ entry.recommendedModel = selectOpenCodeModel(models);
390
+ entry.ready = Boolean(entry.recommendedModel);
391
+ entry.modelsFromCache = true;
392
+ if (entry.ready) entry.readinessError = "";
393
+ return entry;
394
+ }
395
+
396
+ function discoverInstalled(options = {}) {
397
+ const cache = options.cache?.version === DISCOVERY_CACHE_VERSION ? options.cache.results || {} : {};
398
+ const force = options.force === true;
399
+ const openCodeFallback = Array.isArray(options.openCodeModels) ? options.openCodeModels : [];
400
+ return Object.entries(DEFINITIONS).map(([id, definition]) => {
401
+ const cached = cache[id];
402
+ if (!force && !ALWAYS_PROBE.has(id) && cachedProbeUsable(cached)) {
403
+ RESOLVED.set(id, cached.resolvedCommand);
404
+ return { id, ...definition, installed: true, version: cached.version || "kurulu", error: "", resolvedCommand: cached.resolvedCommand, fromCache: true };
405
+ }
406
+ const result = { id, ...definition, ...probe(id, definition) };
407
+ return id === "opencode" ? applyOpenCodeFallback(result, openCodeFallback) : result;
408
+ });
409
+ }
410
+
411
+ // Bir sonraki acilista kullanilacak onbellek gorununu uretir. Yalnizca kurulu ve yolu
412
+ // cozulmus girdiler saklanir; digerleri zaten her acilista yeniden yoklanir.
413
+ function discoveryCacheFrom(discovered) {
414
+ const results = {};
415
+ for (const cli of discovered || []) {
416
+ if (cli.installed && cli.resolvedCommand && !ALWAYS_PROBE.has(cli.id)) {
417
+ results[cli.id] = { installed: true, version: cli.version || "kurulu", resolvedCommand: cli.resolvedCommand };
418
+ }
419
+ }
420
+ return { version: DISCOVERY_CACHE_VERSION, checkedAt: new Date().toISOString(), results };
421
+ }
422
+
423
+ // Tek kaynak: hem gercek gorev calistirici (engine.runCli) hem saglik testi ve model
424
+ // listeleme ayni komut kurulumunu kullanir. Bare npm CLI adlarini where.exe ile fiziksel
425
+ // yola cevirmiyoruz; cmd.exe PATHEXT/PATH cozumlemesini Unicode olarak kendisi yapar.
426
+ function buildCommand(command, args) {
427
+ if (!isWin) return { file: command, args, shell: false };
428
+ const bare = !path.isAbsolute(command) && !/[\\/]/.test(command);
429
+ if (bare) {
430
+ // shell:true iken Node argumanlari birlestirip cmd.exe'ye verir ve OTOMATIK TIRNAK KOYMAZ.
431
+ // Bosluk iceren yol argumanlari (or. "C:\Users\John Doe\...\x.settings.json" veya
432
+ // {PROMPT_FILE}) ikiye bolunurdu; bu da --settings/--file yolunu bozardi. Bosluk veya
433
+ // kabuk metakarakteri iceren argumanlari tirnakla; sade bayrak/model adlari dokunulmaz.
434
+ const shellQuote = (value) => {
435
+ const v = String(value);
436
+ return /[\s"^&|<>()%!]/.test(v) ? `"${v.replace(/"/g, '\\"')}"` : v;
437
+ };
438
+ return { file: command, args: args.map(shellQuote), shell: true };
439
+ }
440
+ if (!/\.(cmd|bat)$/i.test(command)) return { file: command, args, shell: false };
441
+ const quote = (value) => `"${String(value).replace(/"/g, '""')}"`;
442
+ return { file: process.env.ComSpec || "cmd.exe", args: ["/d", "/s", "/c", `"${[command, ...args].map(quote).join(" ")}"`], shell: false, verbatim: true };
443
+ }
444
+
445
+ function healthPrompt(id) {
446
+ return [
447
+ "CLI_TEAM_HEALTH_CHECK",
448
+ `Bu ${id} cli aracinin kullanilabilirlik testidir.`,
449
+ "Yalnizca HEALTH_OK yaz.",
450
+ "Dosya olusturma, degistirme, silme, komut calistirma veya dis sisteme istek gonderme.",
451
+ ].join("\n");
452
+ }
453
+
454
+ // Normal gorev calistiricisi {PROMPT}/{PROMPT_FILE} yer tutucularini cagridan once
455
+ // doldurur. Health check de ayni sozlesmeyi kullanmali; aksi halde OpenCode literal
456
+ // `{PROMPT_FILE}` yolunu acmaya calisir ve saglikli agent katalogdan yanlislikla elenir.
457
+ function preparePromptArgs(args, prompt) {
458
+ let tempDir = "", promptFile = "", useStdin = true;
459
+ const prepared = (Array.isArray(args) ? args : []).map((arg) => {
460
+ const value = String(arg);
461
+ if (value.includes("{PROMPT}")) {
462
+ useStdin = false;
463
+ return value.replaceAll("{PROMPT}", prompt);
464
+ }
465
+ if (value.includes("{PROMPT_FILE}")) {
466
+ useStdin = false;
467
+ if (!promptFile) {
468
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "cli-team-health-"));
469
+ promptFile = path.join(tempDir, "prompt.md");
470
+ fs.writeFileSync(promptFile, prompt, "utf8");
471
+ }
472
+ return value.replaceAll("{PROMPT_FILE}", promptFile);
473
+ }
474
+ return value;
475
+ });
476
+ return {
477
+ args: prepared,
478
+ useStdin,
479
+ promptFile,
480
+ cleanup() { if (tempDir) { try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch {} } },
481
+ };
482
+ }
483
+
484
+ // Tek kaynak: OpenCode icin otonom izinler surumden bagimsiz olarak OPENCODE_CONFIG_CONTENT
485
+ // ile aktarilir. Hem engine.runCli hem saglik testi bu ortami kullanir ki test ile gercek
486
+ // calistirma ayni izinlerle calissin.
487
+ function agentEnvironment(agent) {
488
+ const env = { ...process.env };
489
+ if (agent.adapter !== "opencode") return env;
490
+ let inline = {};
491
+ try { inline = JSON.parse(env.OPENCODE_CONFIG_CONTENT || "{}"); } catch {}
492
+ env.OPENCODE_CONFIG_CONTENT = JSON.stringify({ ...inline, permission: { "*": "allow" } });
493
+ return env;
494
+ }
495
+
496
+ function classifyHealthFailure(text) {
497
+ const raw = String(text || "");
498
+ if (/requires a newer version|upgrade to the latest|version.*unsupported|unsupported.*version|model metadata.*not found|unsupported.*model/i.test(raw)) return { status: "version-incompatible", label: "Sürüm uyumsuz", detail: raw };
499
+ if (/api key|unauthorized|unauthenticated|authentication|login required|not logged in|sign in|giriÅŸ gerekli/i.test(raw)) return { status: "auth-required", label: "Giriş gerekli", detail: raw };
500
+ if (/rate.?limit|quota|too many requests|billing|credit/i.test(raw)) return { status: "quota", label: "Kota veya ödeme gerekli", detail: raw };
501
+ if (/timeout|timed out|zaman aşım/i.test(raw)) return { status: "timeout", label: "Yanıt zaman aşımına uğradı", detail: raw };
502
+ return { status: "failed", label: "Test başarısız", detail: raw };
503
+ }
504
+
505
+ function testInstalledCli(id, options = {}) {
506
+ const definition = DEFINITIONS[id];
507
+ if (!definition) return Promise.resolve({ id, installed: false, health: { status: "unknown", label: "Bilinmiyor", detail: "Tanımsız CLI" } });
508
+ const found = probe(id, definition);
509
+ if (!found.installed) return Promise.resolve({ id, installed: false, version: found.version, error: found.error, health: { status: "not-installed", label: "Kurulu değil", detail: found.error || "CLI bulunamadı" } });
510
+ const agent = effectiveAgent({ adapter: id, cmd: definition.command, args: definition.defaultArgs.slice() }, options.cfg);
511
+ const prompt = healthPrompt(id);
512
+ const prepared = preparePromptArgs(agent.args, prompt);
513
+ const command = buildCommand(agent.cmd, prepared.args);
514
+ const timeoutMs = Math.max(10000, Number(options.timeoutMs || 45000));
515
+ return new Promise((resolve) => {
516
+ let stdout = "", stderr = "", settled = false;
517
+ let child;
518
+ try {
519
+ child = spawn(command.file, command.args, { env: agentEnvironment(agent), shell: command.shell, windowsHide: true, windowsVerbatimArguments: !!command.verbatim });
520
+ } catch (error) {
521
+ prepared.cleanup();
522
+ return resolve({ id, installed: true, version: found.version, health: classifyHealthFailure(error.message) });
523
+ }
524
+ const unregister = registerProbe(child);
525
+ const timer = setTimeout(() => {
526
+ if (settled) return;
527
+ settled = true;
528
+ unregister();
529
+ terminateProbeTree(child);
530
+ prepared.cleanup();
531
+ resolve({ id, installed: true, version: found.version, health: { status: "timeout", label: "Yanıt zaman aşımına uğradı", detail: `Sağlık testi ${Math.round(timeoutMs / 1000)} saniyede tamamlanmadı.` } });
532
+ }, timeoutMs);
533
+ child.stdout?.setEncoding("utf8");
534
+ child.stderr?.setEncoding("utf8");
535
+ child.stdout?.on("data", (data) => { stdout += String(data); });
536
+ child.stderr?.on("data", (data) => { stderr += String(data); });
537
+ child.on("error", (error) => {
538
+ if (settled) return;
539
+ settled = true;
540
+ clearTimeout(timer);
541
+ unregister();
542
+ prepared.cleanup();
543
+ const failure = classifyHealthFailure(error.message);
544
+ resolve({ id, installed: true, version: found.version, health: failure });
545
+ });
546
+ child.on("close", (code) => {
547
+ if (settled) return;
548
+ settled = true;
549
+ clearTimeout(timer);
550
+ unregister();
551
+ prepared.cleanup();
552
+ const raw = `${stdout}\n${stderr}`.trim();
553
+ if (code === 0 && /HEALTH_OK/i.test(raw)) resolve({ id, installed: true, version: found.version, health: { status: "ready", label: "Hazır", detail: "Gerçek sağlık testi başarılı." } });
554
+ else resolve({ id, installed: true, version: found.version, health: classifyHealthFailure(raw || `Çıkış kodu ${code}`) });
555
+ });
556
+ child.stdin?.on("error", () => {});
557
+ child.stdin?.end(prepared.useStdin ? prompt : "");
558
+ });
559
+ }
560
+
561
+ async function healthCheckAll(discovered = discoverInstalled(), options = {}) {
562
+ const results = await Promise.all(discovered.map((item) => testInstalledCli(item.id, options)));
563
+ return discovered.map((item) => ({ ...item, ...(results.find((result) => result.id === item.id) || {}) }));
564
+ }
565
+
566
+ function listCodexModels(options = {}) {
567
+ const command = buildCommand("codex", ["app-server", "--listen", "stdio://"]);
568
+ const timeoutMs = Math.max(5000, Number(options.timeoutMs || 20000));
569
+ return new Promise((resolve, reject) => {
570
+ let buffer = "", settled = false, modelRequestSent = false;
571
+ let child, unregister = () => {};
572
+ const finish = (error, value) => {
573
+ if (settled) return;
574
+ settled = true;
575
+ clearTimeout(timer);
576
+ unregister();
577
+ terminateProbeTree(child);
578
+ if (error) reject(error); else resolve(value);
579
+ };
580
+ const timer = setTimeout(() => finish(new Error(`Codex model listesi ${Math.round(timeoutMs / 1000)} saniyede alınamadı.`)), timeoutMs);
581
+ try { child = spawn(command.file, command.args, { shell: command.shell, windowsHide: true, windowsVerbatimArguments: !!command.verbatim }); }
582
+ catch (error) { finish(error); return; }
583
+ unregister = registerProbe(child);
584
+ child.stdout?.setEncoding("utf8");
585
+ child.stderr?.setEncoding("utf8");
586
+ child.stdout?.on("data", (chunk) => {
587
+ buffer += String(chunk);
588
+ const lines = buffer.split(/\r?\n/);
589
+ buffer = lines.pop() || "";
590
+ for (const line of lines) {
591
+ let message;
592
+ try { message = JSON.parse(line); } catch { continue; }
593
+ if (message.id === 1 && !modelRequestSent) {
594
+ modelRequestSent = true;
595
+ child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: 2, method: "model/list", params: { includeHidden: false, limit: 200 } })}\n`);
596
+ } else if (message.id === 2) {
597
+ if (message.error) { finish(new Error(message.error.message || "Codex model listesi alınamadı.")); return; }
598
+ const models = Array.isArray(message.result?.data) ? message.result.data : [];
599
+ finish(null, models.filter((model) => model && model.hidden !== true).map((model) => ({
600
+ id: String(model.id || model.model || ""),
601
+ model: String(model.model || model.id || ""),
602
+ displayName: String(model.displayName || model.id || model.model || ""),
603
+ description: String(model.description || ""),
604
+ defaultReasoningEffort: String(model.defaultReasoningEffort || "medium"),
605
+ reasoningEfforts: (model.supportedReasoningEfforts || []).map((item) => ({
606
+ id: String(item.reasoningEffort || item.id || ""),
607
+ description: String(item.description || ""),
608
+ })).filter((item) => item.id),
609
+ serviceTiers: (model.serviceTiers || []).map((item) => ({
610
+ id: String(item.id || ""),
611
+ name: String(item.name || item.id || ""),
612
+ description: String(item.description || ""),
613
+ })).filter((item) => item.id),
614
+ additionalSpeedTiers: Array.isArray(model.additionalSpeedTiers) ? model.additionalSpeedTiers.map(String) : [],
615
+ defaultServiceTier: model.defaultServiceTier ? String(model.defaultServiceTier) : "",
616
+ isDefault: model.isDefault === true,
617
+ })).filter((model) => model.id));
618
+ }
619
+ }
620
+ });
621
+ child.stderr?.on("data", () => {});
622
+ child.on("error", finish);
623
+ child.stdin?.on("error", () => {});
624
+ child.stdin?.write(`${JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: { clientInfo: { name: "crewctl", version: "1.0.0", title: "CrewCtl" } } })}\n`);
625
+ });
626
+ }
627
+
628
+ function addMissingAgents(cfg, discovered) {
629
+ cfg.agents ||= {};
630
+ const ignored = new Set(Array.isArray(cfg.discoveryIgnoredAdapters) ? cfg.discoveryIgnoredAdapters.map(String) : []);
631
+ let changed = normalizeAgentAdapters(cfg);
632
+ for (const cli of discovered) {
633
+ const usable = cli.installed && (cli.id !== "opencode" || cli.ready !== false || Boolean(cfg.cliSettings?.opencode?.model));
634
+ const matchingEntries = Object.entries(cfg.agents).filter(([, agent]) => (agent.adapter || adapterId(agent.cmd)) === cli.id);
635
+ const existingEntry = matchingEntries.find(([, agent]) => !agent.autoDiscovered) || matchingEntries[0];
636
+ const existing = existingEntry?.[1];
637
+ // Kullanici otomatik bulunan bir profili sildiyse bu karar kalicidir. O adapter'a ait
638
+ // elle olusturulmus profil varsa ona dokunma; yalnizca autoDiscovered kaydini temizle.
639
+ if (ignored.has(cli.id)) {
640
+ for (const [name, agent] of matchingEntries) {
641
+ if (!agent.autoDiscovered) continue;
642
+ delete cfg.agents[name];
643
+ changed = true;
644
+ }
645
+ continue;
646
+ }
647
+ if (existing) {
648
+ if (usable && existing.autoDiscovered && existing.unavailablePlaceholder) {
649
+ existing.enabled = true;
650
+ existing.args = cli.defaultArgs.slice();
651
+ delete existing.unavailablePlaceholder;
652
+ changed = true;
653
+ }
654
+ // Eski surumler otomatik oneriyi agent.model alanina yaziyordu. Bu alan global
655
+ // CLI ayarini ezdigi icin, kullanici tarafindan acikca override olarak isaretlenmemis
656
+ // otomatik profiller modeli daima CLI ayarindan/otomatik secimden miras alir.
657
+ if (cli.id === "opencode" && existing.autoDiscovered && existing.model && existing.modelOverride !== true) {
658
+ delete existing.model;
659
+ changed = true;
660
+ }
661
+ if (cli.installed && cli.id === "opencode" && existing.autoDiscovered && Number(existing.timeoutSeconds || 0) < cli.timeoutSeconds) {
662
+ existing.timeoutSeconds = cli.timeoutSeconds;
663
+ changed = true;
664
+ }
665
+ if (!usable && existing.autoDiscovered && existing.enabled !== false) {
666
+ existing.enabled = false;
667
+ existing.unavailablePlaceholder = true;
668
+ changed = true;
669
+ }
670
+ continue;
671
+ }
672
+ // OpenCode adapteri her cihazda katalogda gorunur. Kurulu degilse guvenli
673
+ // sekilde devre disi placeholder olur; taramada bulundugunda otomatik etkinlesir.
674
+ if (!cli.installed && cli.id !== "opencode") continue;
675
+ let name = `${cli.id}-auto`, suffix = 2;
676
+ while (cfg.agents[name]) name = `${cli.id}-auto-${suffix++}`;
677
+ cfg.agents[name] = {
678
+ adapter: cli.id,
679
+ cmd: cli.command,
680
+ args: cli.defaultArgs.slice(),
681
+ enabled: usable,
682
+ description: cli.description,
683
+ capabilities: cli.capabilities.slice(),
684
+ roleFile: cli.roleFile,
685
+ costTier: "standard",
686
+ timeoutSeconds: cli.timeoutSeconds || 1200,
687
+ ...(cli.silenceTimeoutSeconds ? { silenceTimeoutSeconds: cli.silenceTimeoutSeconds } : {}),
688
+ autoDiscovered: true,
689
+ ...(!usable ? { unavailablePlaceholder: true } : {}),
690
+ };
691
+ changed = true;
692
+ }
693
+ return changed;
694
+ }
695
+
696
+ // Operator, uzman agent'lardan BAGIMSIZ bir CLI secimidir (claude/codex/gemini/opencode).
697
+ // operator.md rolunu bu CLI giyer; uzman agent'lar operatorun ALTINDA delege edilir.
698
+ // Bir uzman agent silinse bile operator etkilenmez.
699
+ function operatorSpec(cli, cfg) {
700
+ const op = (cfg && cfg.operator) || {};
701
+ const definition = DEFINITIONS[cli];
702
+ // Standart CLI'lar DEFINITIONS'tan cozulur. cfg.operator.cmd verilirse (ozel/test operatoru)
703
+ // dogrudan o komut kullanilir; boylece operator, uzman agent'lardan tamamen bagimsiz kalir.
704
+ const cmd = op.cmd || (definition && definition.command);
705
+ if (!cmd) return null;
706
+ const args = op.cmd ? (Array.isArray(op.args) ? op.args.slice() : []) : (definition ? definition.defaultArgs.slice() : []);
707
+ // Model/effort/tier secimi CLI bazlidir (cfg.cliSettings) ve effectiveAgent icinde uygulanir;
708
+ // ayni ayar o CLI'yi kullanan operator ve tum uzman agent'lar icin gecerlidir.
709
+ const timeoutSeconds = op.timeoutSeconds || (cfg && cfg.agentTimeoutSeconds) || 900;
710
+ const silenceTimeoutSeconds = op.silenceTimeoutSeconds || (definition && definition.silenceTimeoutSeconds) || (cfg && cfg.cliSilenceTimeoutSeconds) || 300;
711
+ const adapter = definition ? cli : (op.adapter || adapterId(cmd));
712
+ return effectiveAgent({ adapter, cmd, args, timeoutSeconds, silenceTimeoutSeconds }, cfg);
713
+ }
714
+
715
+ // Operator gecerli, kurulu bir CLI olmali. Eski semada operator.agent (uzman agent adi)
716
+ // tutuluyordu; burada operator.cli'ye tasinir. Kurulu degilse ilk kurulu CLI'ye gecilir.
717
+ function ensureValidOperator(cfg, discovered) {
718
+ cfg.operator ||= {};
719
+ let changed = false;
720
+ if (cfg.operator.roleFile !== "roles/operator.md") { cfg.operator.roleFile = "roles/operator.md"; changed = true; }
721
+ if (!cfg.operator.cli) {
722
+ const legacy = cfg.operator.agent;
723
+ const source = legacy && cfg.agents?.[legacy] ? cfg.agents[legacy].cmd : legacy;
724
+ cfg.operator.cli = adapterId(source || "codex");
725
+ changed = true;
726
+ }
727
+ if ("agent" in cfg.operator) { delete cfg.operator.agent; changed = true; }
728
+ const installed = Array.isArray(discovered) ? discovered
729
+ .filter((x) => x.installed && (x.id !== "opencode" || x.ready !== false || Boolean(cfg.cliSettings?.opencode?.model)))
730
+ .map((x) => x.id) : null;
731
+ const known = !!DEFINITIONS[cfg.operator.cli];
732
+ const availableOnHost = !installed || installed.length === 0 || installed.includes(cfg.operator.cli);
733
+ if (!known || !availableOnHost) {
734
+ const fallback = (installed && installed[0]) || Object.keys(DEFINITIONS)[0];
735
+ if (fallback && fallback !== cfg.operator.cli) { cfg.operator.cli = fallback; changed = true; }
736
+ }
737
+ return changed;
738
+ }
739
+
740
+ module.exports = { DEFINITIONS, KNOWN_CLIS: Object.keys(DEFINITIONS), adapterId, normalizeAgentAdapter, normalizeAgentAdapters, effectiveAgent, preparePromptArgs, operatorSpec, buildCommand, agentEnvironment, discoverInstalled, discoveryCacheFrom, applyOpenCodeFallback, healthCheckAll, listCodexModels, abortActiveProbes, currentProbeGeneration, selectOpenCodeModel, parseOpenCodeModels, addMissingAgents, ensureValidOperator };