dislop 0.9.0 → 1.0.1
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/.claude-plugin/marketplace.json +3 -3
- package/.claude-plugin/plugin.json +2 -2
- package/.codex-plugin/plugin.json +7 -8
- package/.cursor-plugin/plugin.json +2 -3
- package/.pre-commit-hooks.yaml +5 -12
- package/PRODUCT.md +19 -57
- package/README.md +360 -252
- package/README.zh-CN.md +390 -240
- package/action.yml +30 -39
- package/bin/dislop-mcp.mjs +3 -4
- package/bin/dislop.mjs +4 -4582
- package/gemini-extension.json +2 -2
- package/llms-install.md +68 -0
- package/package.json +17 -55
- package/packs/common/artifact-qa/pack.json +5 -0
- package/packs/common/artifact-qa/prompts/rules.md +11 -0
- package/packs/common/artifact-qa/scripts/index.mjs +173 -0
- package/packs/common/slide-deck/pack.json +6 -0
- package/packs/common/slide-deck/prompts/rules.md +11 -0
- package/packs/common/slide-deck/scripts/index.mjs +62 -0
- package/packs/common/web-ui/pack.json +6 -0
- package/packs/common/web-ui/prompts/rules.md +11 -0
- package/packs/common/web-ui/scripts/index.mjs +80 -0
- package/packs/en/artifact-qa/pack.json +6 -0
- package/packs/en/artifact-qa/prompts/rules.md +14 -0
- package/packs/en/artifact-qa/scripts/index.mjs +90 -0
- package/packs/en/prose/pack.json +7 -0
- package/packs/en/prose/prompts/rules.md +26 -0
- package/packs/en/prose/scripts/index.mjs +290 -0
- package/packs/en/slide-deck/pack.json +7 -0
- package/packs/en/slide-deck/prompts/rules.md +17 -0
- package/packs/en/slide-deck/scripts/index.mjs +81 -0
- package/packs/en/web-ui/pack.json +7 -0
- package/packs/en/web-ui/prompts/rules.md +20 -0
- package/packs/en/web-ui/scripts/index.mjs +103 -0
- package/packs/zh/artifact-qa/pack.json +6 -0
- package/packs/zh/artifact-qa/prompts/rules.md +14 -0
- package/packs/zh/artifact-qa/scripts/index.mjs +89 -0
- package/packs/zh/prose/pack.json +7 -0
- package/packs/zh/prose/prompts/rules.md +26 -0
- package/packs/zh/prose/scripts/index.mjs +305 -0
- package/packs/zh/slide-deck/pack.json +7 -0
- package/packs/zh/slide-deck/prompts/rules.md +17 -0
- package/packs/zh/slide-deck/scripts/index.mjs +75 -0
- package/packs/zh/web-ui/pack.json +7 -0
- package/packs/zh/web-ui/prompts/rules.md +20 -0
- package/packs/zh/web-ui/scripts/index.mjs +98 -0
- package/plugin.json +5 -6
- package/server.json +8 -2
- package/skills/deslop-docs/SKILL.md +10 -8
- package/skills/deslop-slides-docx/SKILL.md +7 -8
- package/skills/deslop-verify/SKILL.md +7 -8
- package/skills/deslop-web-ui/SKILL.md +7 -8
- package/skills/dislop/SKILL.md +16 -28
- package/skills/dislop/references/essay.md +1 -1
- package/skills/dislop/references/essay.zh-CN.md +1 -1
- package/skills/dislop/references/office.md +1 -1
- package/skills/dislop/references/office.zh-CN.md +1 -1
- package/skills/dislop/references/web-ui.md +1 -1
- package/skills/dislop/references/web-ui.zh-CN.md +1 -1
- package/skills/dislop/references/work-docs.md +1 -1
- package/skills/dislop/references/work-docs.zh-CN.md +1 -1
- package/src/cli-main.mjs +210 -0
- package/src/commands/check-fix.mjs +169 -0
- package/src/commands/doctor.mjs +110 -0
- package/src/commands/init.mjs +49 -0
- package/src/commands/login.mjs +82 -0
- package/src/commands/model.mjs +130 -0
- package/src/commands/packs.mjs +44 -0
- package/src/commands/verify.mjs +133 -0
- package/src/commands/voice.mjs +84 -0
- package/src/kernel/config.mjs +232 -0
- package/src/kernel/document-model.mjs +1412 -0
- package/src/kernel/fact-gate.mjs +615 -0
- package/src/kernel/file-discovery.mjs +139 -0
- package/src/{binary-node.mjs → kernel/ooxml-binary.mjs} +532 -893
- package/src/kernel/pack-loader.mjs +715 -0
- package/src/kernel/patcher.mjs +341 -0
- package/src/kernel/pipeline.mjs +267 -0
- package/src/kernel/prompt-assembler.mjs +307 -0
- package/src/kernel/voice-profile.mjs +177 -0
- package/src/mcp/stdio-server.mjs +372 -0
- package/src/runners/auto-fix.mjs +59 -0
- package/src/runners/interactive-tty.mjs +461 -0
- package/src/runners/reporters.mjs +256 -0
- package/src/runtime/chunker.mjs +87 -0
- package/src/runtime/json-extract.mjs +144 -0
- package/src/runtime/privacy-gate.mjs +97 -0
- package/src/runtime/providers/anthropic.mjs +65 -0
- package/src/runtime/providers/cloud.mjs +136 -0
- package/src/runtime/providers/gemini.mjs +91 -0
- package/src/runtime/providers/local-model.mjs +139 -0
- package/src/runtime/providers/openai-compat.mjs +130 -0
- package/src/runtime/router.mjs +315 -0
- package/src/sdk/index.mjs +137 -0
- package/models/manifest.json +0 -90
- package/models/pocket-slop-v1.json +0 -1
- package/packs/business-brief/pack.json +0 -937
- package/packs/core/pack.json +0 -209
- package/packs/deck-and-ui/pack.json +0 -539
- package/packs/engineering-spec/pack.json +0 -144
- package/packs/executive-deck/pack.json +0 -98
- package/packs/literary-essay/pack.json +0 -344
- package/packs/regulated-comms/pack.json +0 -99
- package/patterns/ai-tic-rubric.json +0 -252
- package/patterns/banks/composite-fusion.json +0 -48
- package/patterns/banks/discourse-rhythm.json +0 -95
- package/patterns/banks/lexical-signals.json +0 -92
- package/patterns/banks/syntactic-frames.json +0 -52
- package/patterns/banks/visual-deck.json +0 -44
- package/patterns/dimensions/artifacts.json +0 -121
- package/patterns/dimensions/discourse.json +0 -438
- package/patterns/dimensions/lexical.json +0 -198
- package/patterns/dimensions/rhythm.json +0 -258
- package/patterns/dimensions/syntactic.json +0 -285
- package/patterns/dimensions/visual-and-deck.json +0 -530
- package/patterns/taxonomy.json +0 -112
- package/src/core.mjs +0 -5497
- package/src/dislop/review-picker.vim +0 -114
- package/src/local-model.mjs +0 -997
- package/src/pattern-engine.mjs +0 -1003
- package/src/pocket-model.mjs +0 -729
- package/src/review-apply.mjs +0 -151
- package/src/review-client.mjs +0 -1073
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { invokeOpenAiCompatProvider } from "./openai-compat.mjs";
|
|
4
|
+
|
|
5
|
+
export const LOCAL_MODEL_CATALOG = Object.freeze({
|
|
6
|
+
"qwen2.5-1.5b-instruct": {
|
|
7
|
+
name: "qwen2.5-1.5b-instruct",
|
|
8
|
+
ollamaTag: "qwen2.5:1.5b",
|
|
9
|
+
sizeLabel: "1.0 GB",
|
|
10
|
+
maxInputTokens: 6000,
|
|
11
|
+
description: "Fast lightweight local model for basic semantic anti-slop checks.",
|
|
12
|
+
},
|
|
13
|
+
"qwen2.5-7b-instruct": {
|
|
14
|
+
name: "qwen2.5-7b-instruct",
|
|
15
|
+
ollamaTag: "qwen2.5:7b",
|
|
16
|
+
sizeLabel: "4.7 GB",
|
|
17
|
+
maxInputTokens: 12000,
|
|
18
|
+
description: "Balanced local model for bilingual EN/ZH editorial suggestions.",
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const OLLAMA_INSTALL_HINT =
|
|
23
|
+
"Install Ollama from https://ollama.com (e.g., 'brew install ollama' on macOS), start 'ollama serve', then run 'dislop model pull qwen2.5-1.5b-instruct'.";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Fast zero-subprocess check for whether the `ollama` CLI binary is installed locally.
|
|
27
|
+
*/
|
|
28
|
+
export function detectOllamaBinary(env = process.env) {
|
|
29
|
+
if (env.DISLOP_ASSUME_NO_OLLAMA === "1") {
|
|
30
|
+
return { installed: false, binaryPath: null };
|
|
31
|
+
}
|
|
32
|
+
if (env.DISLOP_OLLAMA_BIN !== undefined) {
|
|
33
|
+
const custom = String(env.DISLOP_OLLAMA_BIN).trim();
|
|
34
|
+
if (custom && fs.existsSync(custom)) {
|
|
35
|
+
return { installed: true, binaryPath: custom };
|
|
36
|
+
}
|
|
37
|
+
return { installed: false, binaryPath: null };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const isWin = process.platform === "win32";
|
|
41
|
+
const binNames = isWin ? ["ollama.exe", "ollama.cmd", "ollama"] : ["ollama"];
|
|
42
|
+
const pathDirs = String(env.PATH || "")
|
|
43
|
+
.split(path.delimiter)
|
|
44
|
+
.map((p) => p.trim())
|
|
45
|
+
.filter(Boolean);
|
|
46
|
+
const fallbackDirs = isWin ? [] : ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"];
|
|
47
|
+
|
|
48
|
+
for (const dir of [...pathDirs, ...fallbackDirs]) {
|
|
49
|
+
for (const name of binNames) {
|
|
50
|
+
const candidate = path.join(dir, name);
|
|
51
|
+
try {
|
|
52
|
+
if (fs.statSync(candidate).isFile()) {
|
|
53
|
+
return { installed: true, binaryPath: candidate };
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
// continue scanning
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { installed: false, binaryPath: null };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function readInstalledModelsManifest(modelsDir) {
|
|
65
|
+
const manifestPath = path.join(modelsDir, "manifest.json");
|
|
66
|
+
if (!fs.existsSync(manifestPath)) {
|
|
67
|
+
return { activeModel: null, installed: {} };
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const raw = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
71
|
+
return {
|
|
72
|
+
activeModel: raw.activeModel || null,
|
|
73
|
+
installed: raw.installed && typeof raw.installed === "object" ? raw.installed : {},
|
|
74
|
+
};
|
|
75
|
+
} catch {
|
|
76
|
+
return { activeModel: null, installed: {} };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function resolveInstalledLocalModel(config, env = process.env) {
|
|
81
|
+
const manifest = readInstalledModelsManifest(config.modelsDir);
|
|
82
|
+
const activeKey =
|
|
83
|
+
manifest.activeModel && manifest.installed[manifest.activeModel]
|
|
84
|
+
? manifest.activeModel
|
|
85
|
+
: Object.keys(manifest.installed)[0] || null;
|
|
86
|
+
|
|
87
|
+
if (!activeKey) return null;
|
|
88
|
+
const entry = manifest.installed[activeKey];
|
|
89
|
+
const baseUrl =
|
|
90
|
+
env.DISLOP_LOCAL_MODEL_URL || entry.baseUrl || "http://127.0.0.1:11434/v1";
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
tier: "local",
|
|
94
|
+
name: activeKey,
|
|
95
|
+
protocol: "openai",
|
|
96
|
+
baseUrl,
|
|
97
|
+
apiKey: entry.apiKey || "local",
|
|
98
|
+
model: entry.runtimeModel || entry.ollamaTag || activeKey,
|
|
99
|
+
structuredOutput: "json_object",
|
|
100
|
+
maxInputTokens: entry.maxInputTokens || 8000,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Tier 3 Managed Local Small Model adapter (loopback OpenAI-compatible runtime via Ollama).
|
|
106
|
+
*/
|
|
107
|
+
export async function invokeLocalModelProvider({
|
|
108
|
+
localModel,
|
|
109
|
+
payload,
|
|
110
|
+
timeoutMs = 60000,
|
|
111
|
+
fetchImpl = globalThis.fetch,
|
|
112
|
+
env = process.env,
|
|
113
|
+
}) {
|
|
114
|
+
try {
|
|
115
|
+
return await invokeOpenAiCompatProvider({
|
|
116
|
+
provider: localModel,
|
|
117
|
+
payload,
|
|
118
|
+
timeoutMs,
|
|
119
|
+
fetchImpl,
|
|
120
|
+
});
|
|
121
|
+
} catch (err) {
|
|
122
|
+
if (!err?.authError && /Unable to reach provider/i.test(String(err?.message || ""))) {
|
|
123
|
+
const ollama = detectOllamaBinary(env);
|
|
124
|
+
if (!ollama.installed) {
|
|
125
|
+
const missingErr = new Error(
|
|
126
|
+
`Ollama is missing on this machine (required for Tier 3 '--engine local'). ${OLLAMA_INSTALL_HINT}`,
|
|
127
|
+
);
|
|
128
|
+
missingErr.ollamaMissing = true;
|
|
129
|
+
throw missingErr;
|
|
130
|
+
}
|
|
131
|
+
const stoppedErr = new Error(
|
|
132
|
+
`${err.message}. Ensure the local Ollama server is running ('ollama serve' or open the Ollama app) and that '${localModel.model}' has been pulled ('dislop model pull ${localModel.name}').`,
|
|
133
|
+
);
|
|
134
|
+
stoppedErr.ollamaUnreachable = true;
|
|
135
|
+
throw stoppedErr;
|
|
136
|
+
}
|
|
137
|
+
throw err;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { extractStructuredJson } from "../json-extract.mjs";
|
|
2
|
+
|
|
3
|
+
function isReasoningModel(model) {
|
|
4
|
+
return /^(?:o1|o3|o4|gpt-[5-9])\b|deepseek-reasoner|\breasoner\b/i.test(String(model || ""));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Tier 2 OpenAI-compatible `/v1/chat/completions` adapter
|
|
9
|
+
* (covers OpenAI, OpenRouter, DeepSeek, CLIProxyAPI/Responses bridges, Ollama, vLLM, LM Studio).
|
|
10
|
+
*/
|
|
11
|
+
export async function invokeOpenAiCompatProvider({
|
|
12
|
+
provider,
|
|
13
|
+
payload,
|
|
14
|
+
timeoutMs = 60000,
|
|
15
|
+
fetchImpl = globalThis.fetch,
|
|
16
|
+
}) {
|
|
17
|
+
const baseUrl = String(provider.baseUrl || "https://api.openai.com/v1").replace(/\/+$/, "");
|
|
18
|
+
const url = `${baseUrl}/chat/completions`;
|
|
19
|
+
const model = provider.model || "gpt-4o-mini";
|
|
20
|
+
const modePref = provider.structuredOutput || "json_object";
|
|
21
|
+
|
|
22
|
+
const modeFallbacks =
|
|
23
|
+
modePref === "json_schema"
|
|
24
|
+
? ["json_schema", "json_object", "none"]
|
|
25
|
+
: modePref === "json_object"
|
|
26
|
+
? ["json_object", "none"]
|
|
27
|
+
: ["none"];
|
|
28
|
+
|
|
29
|
+
let omitTemperature = isReasoningModel(model);
|
|
30
|
+
let useInputField = provider.payloadStyle === "input";
|
|
31
|
+
let lastErr = null;
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < modeFallbacks.length; i += 1) {
|
|
34
|
+
const mode = modeFallbacks[i];
|
|
35
|
+
const body = useInputField
|
|
36
|
+
? {
|
|
37
|
+
model,
|
|
38
|
+
input: `${payload.systemPrompt}\n\n${payload.userPrompt}`,
|
|
39
|
+
}
|
|
40
|
+
: {
|
|
41
|
+
model,
|
|
42
|
+
messages: [
|
|
43
|
+
{ role: "system", content: payload.systemPrompt },
|
|
44
|
+
{ role: "user", content: payload.userPrompt },
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
if (!omitTemperature) {
|
|
49
|
+
body.temperature = 0.1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!useInputField) {
|
|
53
|
+
if (mode === "json_schema" && payload.jsonSchema) {
|
|
54
|
+
body.response_format = {
|
|
55
|
+
type: "json_schema",
|
|
56
|
+
json_schema: payload.jsonSchema,
|
|
57
|
+
};
|
|
58
|
+
} else if (mode === "json_object") {
|
|
59
|
+
body.response_format = { type: "json_object" };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const headers = {
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
};
|
|
66
|
+
if (provider.apiKey) {
|
|
67
|
+
headers.Authorization = `Bearer ${provider.apiKey}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const controller = new AbortController();
|
|
71
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
72
|
+
try {
|
|
73
|
+
let res;
|
|
74
|
+
try {
|
|
75
|
+
res = await fetchImpl(url, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
headers,
|
|
78
|
+
body: JSON.stringify(body),
|
|
79
|
+
signal: controller.signal,
|
|
80
|
+
});
|
|
81
|
+
} catch (netErr) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
`Unable to reach provider '${provider.name}' at ${url} (${netErr?.cause?.code || netErr.message})`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (res.status === 401 || res.status === 403) {
|
|
88
|
+
const err = new Error(`Provider '${provider.name}' rejected API key (HTTP ${res.status})`);
|
|
89
|
+
err.authError = true;
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!res.ok) {
|
|
94
|
+
const errText = await res.text().catch(() => "");
|
|
95
|
+
if (res.status === 400 || res.status === 422) {
|
|
96
|
+
// If proxy rejects `temperature`, strip it and retry the same structured-output mode
|
|
97
|
+
if (!omitTemperature && /temperature/i.test(errText)) {
|
|
98
|
+
omitTemperature = true;
|
|
99
|
+
i -= 1;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
// If proxy expects `"input"` instead of `"messages"` (Responses-style bridge), switch & retry
|
|
103
|
+
if (!useInputField && /\binput\b/i.test(errText) && /\bmessages\b|required|missing/i.test(errText)) {
|
|
104
|
+
useInputField = true;
|
|
105
|
+
i -= 1;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (mode !== "none") {
|
|
109
|
+
lastErr = new Error(`HTTP ${res.status}: ${errText.slice(0, 200)}`);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`Provider '${provider.name}' HTTP ${res.status}: ${errText.slice(0, 240)}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const json = await res.json();
|
|
117
|
+
const content =
|
|
118
|
+
json?.choices?.[0]?.message?.content ??
|
|
119
|
+
(typeof json?.output_text === "string" ? json.output_text : null);
|
|
120
|
+
if (typeof content !== "string" || !content.trim()) {
|
|
121
|
+
throw new Error(`Provider '${provider.name}' returned empty completion content`);
|
|
122
|
+
}
|
|
123
|
+
return extractStructuredJson(content);
|
|
124
|
+
} finally {
|
|
125
|
+
clearTimeout(timer);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
throw lastErr || new Error(`Provider '${provider.name}' failed structured output negotiation`);
|
|
130
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { DislopConfigError, readJsonFileStrict } from "../kernel/config.mjs";
|
|
3
|
+
import { checkPrivacyConsent, isLoopbackHostname, extractHostname } from "./privacy-gate.mjs";
|
|
4
|
+
import { invokeAnthropicProvider } from "./providers/anthropic.mjs";
|
|
5
|
+
import { invokeCloudProvider } from "./providers/cloud.mjs";
|
|
6
|
+
import { invokeGeminiProvider } from "./providers/gemini.mjs";
|
|
7
|
+
import {
|
|
8
|
+
OLLAMA_INSTALL_HINT,
|
|
9
|
+
detectOllamaBinary,
|
|
10
|
+
invokeLocalModelProvider,
|
|
11
|
+
resolveInstalledLocalModel,
|
|
12
|
+
} from "./providers/local-model.mjs";
|
|
13
|
+
import { invokeOpenAiCompatProvider } from "./providers/openai-compat.mjs";
|
|
14
|
+
|
|
15
|
+
const AMBIENT_ENV_CANDIDATES = [
|
|
16
|
+
{
|
|
17
|
+
name: "env-openai",
|
|
18
|
+
protocol: "openai",
|
|
19
|
+
envVarName: "OPENAI_API_KEY",
|
|
20
|
+
baseUrl: "https://api.openai.com/v1",
|
|
21
|
+
model: "gpt-4o-mini",
|
|
22
|
+
structuredOutput: "json_schema",
|
|
23
|
+
maxInputTokens: 16000,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "env-anthropic",
|
|
27
|
+
protocol: "anthropic",
|
|
28
|
+
envVarName: "ANTHROPIC_API_KEY",
|
|
29
|
+
baseUrl: "https://api.anthropic.com/v1",
|
|
30
|
+
model: "claude-3-5-haiku-latest",
|
|
31
|
+
structuredOutput: "json_object",
|
|
32
|
+
maxInputTokens: 16000,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "env-gemini",
|
|
36
|
+
protocol: "gemini",
|
|
37
|
+
envVarName: "GEMINI_API_KEY",
|
|
38
|
+
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
|
|
39
|
+
model: "gemini-3.8-flash",
|
|
40
|
+
structuredOutput: "json_object",
|
|
41
|
+
maxInputTokens: 24000,
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const BUILTIN_PROVIDER_ALIASES = new Map([
|
|
46
|
+
["openai", "env-openai"],
|
|
47
|
+
["env-openai", "env-openai"],
|
|
48
|
+
["anthropic", "env-anthropic"],
|
|
49
|
+
["claude", "env-anthropic"],
|
|
50
|
+
["env-anthropic", "env-anthropic"],
|
|
51
|
+
["gemini", "env-gemini"],
|
|
52
|
+
["google", "env-gemini"],
|
|
53
|
+
["env-gemini", "env-gemini"],
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
function readAmbientCandidate(cand, env, modelOverride) {
|
|
57
|
+
const keyVal = env[cand.envVarName] || (cand.envVarName === "GEMINI_API_KEY" ? env.GOOGLE_API_KEY : "");
|
|
58
|
+
if (!keyVal || !String(keyVal).trim()) return null;
|
|
59
|
+
return {
|
|
60
|
+
...cand,
|
|
61
|
+
model: modelOverride || cand.model,
|
|
62
|
+
apiKey: String(keyVal).trim(),
|
|
63
|
+
fromAmbientEnv: true,
|
|
64
|
+
envVarName: env[cand.envVarName] ? cand.envVarName : "GOOGLE_API_KEY",
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const UPGRADE_TIER_HINT =
|
|
69
|
+
"💡 Running local deterministic checks only. For deeper semantic de-slopping, run 'dislop login' (Cloud), pass '--engine auto' / '--engine byok', or run 'dislop model pull' (requires Ollama: https://ollama.com).";
|
|
70
|
+
|
|
71
|
+
export function loadCloudCredentials(credentialsPath) {
|
|
72
|
+
if (!fs.existsSync(credentialsPath)) return null;
|
|
73
|
+
try {
|
|
74
|
+
const creds = readJsonFileStrict(credentialsPath);
|
|
75
|
+
const token = creds?.accessToken || creds?.token || creds?.apiKey;
|
|
76
|
+
if (!token) return null;
|
|
77
|
+
if (creds.expiresAt && Number(creds.expiresAt) > 0 && Date.now() >= Number(creds.expiresAt)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return creds;
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function resolveTier2ProviderCandidate(config, env = process.env) {
|
|
87
|
+
const configured = config.providers || {};
|
|
88
|
+
const modelOverride = config.modelOverride ? String(config.modelOverride).trim() : "";
|
|
89
|
+
|
|
90
|
+
// 1. Named activeProvider from CLI `--provider` or `config.json`
|
|
91
|
+
if (config.activeProvider) {
|
|
92
|
+
const entry = configured[config.activeProvider];
|
|
93
|
+
if (!entry) {
|
|
94
|
+
const aliasTarget = BUILTIN_PROVIDER_ALIASES.get(String(config.activeProvider).toLowerCase());
|
|
95
|
+
if (aliasTarget) {
|
|
96
|
+
const cand = AMBIENT_ENV_CANDIDATES.find((c) => c.name === aliasTarget);
|
|
97
|
+
return cand ? readAmbientCandidate(cand, env, modelOverride) : null;
|
|
98
|
+
}
|
|
99
|
+
throw new DislopConfigError(
|
|
100
|
+
`Configured provider '${config.activeProvider}' not found in config.json providers.`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const apiKey = entry.apiKey || (entry.apiKeyEnv ? env[entry.apiKeyEnv] : "");
|
|
104
|
+
const host = extractHostname(entry.baseUrl || "https://api.openai.com/v1");
|
|
105
|
+
if (!apiKey && !isLoopbackHostname(host)) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
name: config.activeProvider,
|
|
110
|
+
protocol: entry.protocol || "openai",
|
|
111
|
+
baseUrl: entry.baseUrl || "https://api.openai.com/v1",
|
|
112
|
+
apiKey: apiKey || "",
|
|
113
|
+
model: modelOverride || entry.model || "gpt-4o-mini",
|
|
114
|
+
structuredOutput: entry.structuredOutput || "json_object",
|
|
115
|
+
maxInputTokens: Number(entry.maxInputTokens) || 16000,
|
|
116
|
+
fromAmbientEnv: false,
|
|
117
|
+
envVarName: entry.apiKeyEnv || null,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 2. First usable provider entry in config.json
|
|
122
|
+
for (const [name, entry] of Object.entries(configured)) {
|
|
123
|
+
const apiKey = entry.apiKey || (entry.apiKeyEnv ? env[entry.apiKeyEnv] : "");
|
|
124
|
+
const host = extractHostname(entry.baseUrl || "https://api.openai.com/v1");
|
|
125
|
+
if (!apiKey && !isLoopbackHostname(host)) continue;
|
|
126
|
+
return {
|
|
127
|
+
name,
|
|
128
|
+
protocol: entry.protocol || "openai",
|
|
129
|
+
baseUrl: entry.baseUrl || "https://api.openai.com/v1",
|
|
130
|
+
apiKey: apiKey || "",
|
|
131
|
+
model: modelOverride || entry.model || "gpt-4o-mini",
|
|
132
|
+
structuredOutput: entry.structuredOutput || "json_object",
|
|
133
|
+
maxInputTokens: Number(entry.maxInputTokens) || 16000,
|
|
134
|
+
fromAmbientEnv: false,
|
|
135
|
+
envVarName: entry.apiKeyEnv || null,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 3. Ambient environment variable detection (subject to Privacy Consent Gate)
|
|
140
|
+
for (const cand of AMBIENT_ENV_CANDIDATES) {
|
|
141
|
+
const resolved = readAmbientCandidate(cand, env, modelOverride);
|
|
142
|
+
if (resolved) return resolved;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Resolve the active LLM execution route according to `config.routing`:
|
|
150
|
+
* - `"local"` (default): Tier 3 local model if installed -> otherwise deterministic-only + upgrade hint (0 uploads).
|
|
151
|
+
* - `"auto"`: Tier 1 Cloud -> Tier 2 BYOK/Loopback (with Privacy Gate) -> Tier 3 Local -> deterministic-only + hint.
|
|
152
|
+
* - `"cloud"`: Tier 1 Cloud strictly (throws Exit Code 2 `DislopConfigError` if missing auth).
|
|
153
|
+
* - `"byok"`: Tier 2 BYOK/Loopback strictly (throws Exit Code 2 `DislopConfigError` if missing provider/key).
|
|
154
|
+
*/
|
|
155
|
+
export async function resolveExecutionRoute({
|
|
156
|
+
config,
|
|
157
|
+
isTty,
|
|
158
|
+
env = process.env,
|
|
159
|
+
stdin = process.stdin,
|
|
160
|
+
stderr = process.stderr,
|
|
161
|
+
fetchImpl = globalThis.fetch,
|
|
162
|
+
promptFn = null,
|
|
163
|
+
}) {
|
|
164
|
+
if (config.noLlm) {
|
|
165
|
+
return { tier: "none", engineLabel: "deterministic", maxInputTokens: 0, execute: null };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const mode = config.routing || "local";
|
|
169
|
+
|
|
170
|
+
// Explicit Cloud
|
|
171
|
+
if (mode === "cloud") {
|
|
172
|
+
const creds = loadCloudCredentials(config.credentialsPath);
|
|
173
|
+
if (!creds) {
|
|
174
|
+
throw new DislopConfigError(
|
|
175
|
+
"Engine 'cloud' requires authentication. Run 'dislop login' first.",
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
tier: "cloud",
|
|
180
|
+
engineLabel: "cloud:dislop",
|
|
181
|
+
maxInputTokens: 20000,
|
|
182
|
+
execute: (payload, doc, promptRules) =>
|
|
183
|
+
invokeCloudProvider({ credentials: creds, payload, doc, promptRules, fetchImpl, env }),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Explicit BYOK
|
|
188
|
+
if (mode === "byok") {
|
|
189
|
+
const provider = resolveTier2ProviderCandidate(config, env);
|
|
190
|
+
if (!provider) {
|
|
191
|
+
throw new DislopConfigError(
|
|
192
|
+
"Engine 'byok' requires a configured provider in config.json or an OPENAI_API_KEY / ANTHROPIC_API_KEY / GEMINI_API_KEY environment variable.",
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
await checkPrivacyConsent({
|
|
196
|
+
provider,
|
|
197
|
+
config,
|
|
198
|
+
isTty,
|
|
199
|
+
explicitByokFlag: Boolean(config.explicitEngine),
|
|
200
|
+
stdin,
|
|
201
|
+
stderr,
|
|
202
|
+
promptFn,
|
|
203
|
+
});
|
|
204
|
+
return buildTier2Route(provider, fetchImpl);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 3-Tier Cascade (`--engine auto` or `routing: "auto"`)
|
|
208
|
+
if (mode === "auto") {
|
|
209
|
+
const creds = loadCloudCredentials(config.credentialsPath);
|
|
210
|
+
if (creds) {
|
|
211
|
+
return {
|
|
212
|
+
tier: "cloud",
|
|
213
|
+
engineLabel: "cloud:dislop",
|
|
214
|
+
maxInputTokens: 20000,
|
|
215
|
+
execute: (payload, doc, promptRules) =>
|
|
216
|
+
invokeCloudProvider({ credentials: creds, payload, doc, promptRules, fetchImpl, env }),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const provider = resolveTier2ProviderCandidate(config, env);
|
|
221
|
+
if (provider) {
|
|
222
|
+
const consent = await checkPrivacyConsent({
|
|
223
|
+
provider,
|
|
224
|
+
config,
|
|
225
|
+
isTty,
|
|
226
|
+
explicitByokFlag: false,
|
|
227
|
+
stdin,
|
|
228
|
+
stderr,
|
|
229
|
+
promptFn,
|
|
230
|
+
});
|
|
231
|
+
if (consent.approved) {
|
|
232
|
+
return buildTier2Route(provider, fetchImpl);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Default `"local"` mode (and Tier 3 fallback of `"auto"`): check installed local model
|
|
238
|
+
const localModel = resolveInstalledLocalModel(config, env);
|
|
239
|
+
if (localModel) {
|
|
240
|
+
const resolvedLocal = config.modelOverride
|
|
241
|
+
? { ...localModel, model: String(config.modelOverride).trim() }
|
|
242
|
+
: localModel;
|
|
243
|
+
return {
|
|
244
|
+
tier: "local",
|
|
245
|
+
engineLabel: `local:${resolvedLocal.model}`,
|
|
246
|
+
maxInputTokens: resolvedLocal.maxInputTokens || 8000,
|
|
247
|
+
fallbackOnConnRefused: !config.explicitEngine,
|
|
248
|
+
execute: (payload) => invokeLocalModelProvider({ localModel: resolvedLocal, payload, fetchImpl, env }),
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Also allow loopback provider configured in `config.providers` when routing === "local"
|
|
253
|
+
const maybeLoopback = resolveTier2ProviderCandidate(config, env);
|
|
254
|
+
if (maybeLoopback && isLoopbackHostname(extractHostname(maybeLoopback.baseUrl))) {
|
|
255
|
+
if (String(maybeLoopback.baseUrl).includes(":11434") || /ollama/i.test(maybeLoopback.name)) {
|
|
256
|
+
return {
|
|
257
|
+
tier: "local",
|
|
258
|
+
engineLabel: `local:${maybeLoopback.model}`,
|
|
259
|
+
maxInputTokens: maybeLoopback.maxInputTokens || 8000,
|
|
260
|
+
fallbackOnConnRefused: !config.explicitEngine,
|
|
261
|
+
execute: (payload) =>
|
|
262
|
+
invokeLocalModelProvider({ localModel: maybeLoopback, payload, fetchImpl, env }),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
return buildTier2Route(maybeLoopback, fetchImpl, "local");
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const ollama = detectOllamaBinary(env);
|
|
269
|
+
let fallbackHint = UPGRADE_TIER_HINT;
|
|
270
|
+
if (config.explicitEngine && mode === "local") {
|
|
271
|
+
fallbackHint = ollama.installed
|
|
272
|
+
? "💡 No Tier 3 local model installed yet. Running deterministic Script Packs only — start 'ollama serve' and run 'dislop model pull qwen2.5-1.5b-instruct' to enable local LLM checks."
|
|
273
|
+
: `💡 Ollama is missing on this machine (required for Tier 3 '--engine local' LLM checks). Running deterministic Script Packs only — ${OLLAMA_INSTALL_HINT}`;
|
|
274
|
+
} else if (!ollama.installed) {
|
|
275
|
+
fallbackHint =
|
|
276
|
+
"💡 Running local deterministic checks only (Ollama is not installed for Tier 3 local LLM). For deeper semantic de-slopping, run 'dislop login' (Cloud), pass '--engine auto' / '--engine byok', or install Ollama (https://ollama.com) and run 'dislop model pull'.";
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
tier: "none",
|
|
281
|
+
engineLabel: "deterministic",
|
|
282
|
+
maxInputTokens: 0,
|
|
283
|
+
hint: fallbackHint,
|
|
284
|
+
execute: null,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function buildTier2Route(provider, fetchImpl, labelPrefix = "byok") {
|
|
289
|
+
const protocol = String(provider.protocol || "openai").toLowerCase();
|
|
290
|
+
const engineLabel = `${labelPrefix}:${provider.model}`;
|
|
291
|
+
const maxInputTokens = provider.maxInputTokens || 16000;
|
|
292
|
+
|
|
293
|
+
if (protocol === "anthropic") {
|
|
294
|
+
return {
|
|
295
|
+
tier: labelPrefix,
|
|
296
|
+
engineLabel,
|
|
297
|
+
maxInputTokens,
|
|
298
|
+
execute: (payload) => invokeAnthropicProvider({ provider, payload, fetchImpl }),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
if (protocol === "gemini") {
|
|
302
|
+
return {
|
|
303
|
+
tier: labelPrefix,
|
|
304
|
+
engineLabel,
|
|
305
|
+
maxInputTokens,
|
|
306
|
+
execute: (payload) => invokeGeminiProvider({ provider, payload, fetchImpl }),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
return {
|
|
310
|
+
tier: labelPrefix,
|
|
311
|
+
engineLabel,
|
|
312
|
+
maxInputTokens,
|
|
313
|
+
execute: (payload) => invokeOpenAiCompatProvider({ provider, payload, fetchImpl }),
|
|
314
|
+
};
|
|
315
|
+
}
|