dislop 0.9.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.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 -63
- package/README.md +358 -249
- package/README.zh-CN.md +391 -237
- package/action.yml +30 -39
- package/bin/dislop-mcp.mjs +3 -4
- package/bin/dislop.mjs +4 -4594
- package/gemini-extension.json +2 -2
- package/llms-install.md +68 -0
- package/package.json +17 -63
- 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 +235 -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 +293 -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 +2 -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/agents/openai.yaml +1 -1
- package/skills/dislop/references/essay.md +6 -6
- package/skills/dislop/references/essay.zh-CN.md +6 -6
- package/skills/dislop/references/office.md +5 -5
- package/skills/dislop/references/office.zh-CN.md +5 -5
- package/skills/dislop/references/web-ui.md +4 -4
- package/skills/dislop/references/web-ui.zh-CN.md +4 -4
- package/skills/dislop/references/work-docs.md +7 -7
- package/skills/dislop/references/work-docs.zh-CN.md +7 -7
- package/src/cli-main.mjs +292 -0
- package/src/commands/check-fix.mjs +324 -0
- package/src/commands/doctor.mjs +132 -0
- package/src/commands/init.mjs +55 -0
- package/src/commands/login.mjs +82 -0
- package/src/commands/model.mjs +130 -0
- package/src/commands/packs.mjs +81 -0
- package/src/commands/verify.mjs +165 -0
- package/src/commands/voice.mjs +84 -0
- package/src/commands/worklog.mjs +424 -0
- package/src/kernel/config.mjs +759 -0
- package/src/kernel/document-model.mjs +1432 -0
- package/src/kernel/fact-gate.mjs +775 -0
- package/src/kernel/file-discovery.mjs +296 -0
- package/src/{binary-node.mjs → kernel/ooxml-binary.mjs} +615 -890
- package/src/kernel/pack-loader.mjs +801 -0
- package/src/kernel/pack-validator.mjs +491 -0
- package/src/kernel/patcher.mjs +471 -0
- package/src/kernel/pipeline.mjs +530 -0
- package/src/kernel/prompt-assembler.mjs +336 -0
- package/src/kernel/version.mjs +7 -0
- package/src/kernel/voice-profile.mjs +180 -0
- package/src/mcp/stdio-server.mjs +442 -0
- package/src/runners/auto-fix.mjs +74 -0
- package/src/runners/interactive-tty.mjs +488 -0
- package/src/runners/reporters.mjs +272 -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 +138 -0
- package/src/runtime/providers/gemini.mjs +91 -0
- package/src/runtime/providers/local-model.mjs +147 -0
- package/src/runtime/providers/openai-compat.mjs +130 -0
- package/src/runtime/router.mjs +324 -0
- package/src/runtime/worklog.mjs +904 -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,138 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { DISLOP_VERSION } from "../../kernel/version.mjs";
|
|
3
|
+
import { extractStructuredJson } from "../json-extract.mjs";
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_CLOUD_REVIEW_URL = "https://dislop.com/api/v1/review";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Tier 1 Official Cloud adapter (`POST https://dislop.com/api/v1/review`).
|
|
9
|
+
*/
|
|
10
|
+
export async function invokeCloudProvider({
|
|
11
|
+
credentials,
|
|
12
|
+
payload,
|
|
13
|
+
doc,
|
|
14
|
+
promptRules,
|
|
15
|
+
timeoutMs = 45000,
|
|
16
|
+
fetchImpl = globalThis.fetch,
|
|
17
|
+
env = process.env,
|
|
18
|
+
}) {
|
|
19
|
+
const endpoint = env.DISLOP_CLOUD_URL || credentials?.endpoint || DEFAULT_CLOUD_REVIEW_URL;
|
|
20
|
+
const token = credentials?.accessToken || credentials?.token || credentials?.apiKey;
|
|
21
|
+
if (!token) {
|
|
22
|
+
const err = new Error("Missing Official Cloud token in credentials.json. Run 'dislop login'.");
|
|
23
|
+
err.authError = true;
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const controller = new AbortController();
|
|
28
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const res = await fetchImpl(endpoint, {
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers: {
|
|
34
|
+
"Content-Type": "application/json",
|
|
35
|
+
Authorization: `Bearer ${token}`,
|
|
36
|
+
"User-Agent": `dislop-cli/${DISLOP_VERSION}`,
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify({
|
|
39
|
+
schema: "dislop.review-request/1",
|
|
40
|
+
text: Array.isArray(doc?.segments)
|
|
41
|
+
? doc.segments.map((s) => s.text).join("\n\n")
|
|
42
|
+
: String(payload?.userPrompt || ""),
|
|
43
|
+
file: path.basename(doc?.filePath || "document"),
|
|
44
|
+
format: doc?.format || "md",
|
|
45
|
+
modality: doc?.format || "md",
|
|
46
|
+
language: doc?.language || "en",
|
|
47
|
+
systemPrompt: payload.systemPrompt,
|
|
48
|
+
userPrompt: payload.userPrompt,
|
|
49
|
+
rules: promptRules,
|
|
50
|
+
}),
|
|
51
|
+
signal: controller.signal,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
if (res.status === 401 || res.status === 403) {
|
|
55
|
+
const err = new Error(`Official Cloud authentication failed (HTTP ${res.status}). Run 'dislop login'.`);
|
|
56
|
+
err.authError = true;
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
if (!res.ok) {
|
|
60
|
+
const bodyText = await res.text().catch(() => "");
|
|
61
|
+
throw new Error(`Official Cloud request failed (HTTP ${res.status}): ${bodyText.slice(0, 240)}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const data = await res.json();
|
|
65
|
+
if (data && typeof data === "object" && Array.isArray(data.items)) {
|
|
66
|
+
return data;
|
|
67
|
+
}
|
|
68
|
+
if (data && typeof data === "object" && Array.isArray(data.findings)) {
|
|
69
|
+
return adaptLegacyCloudFindings(data, doc, promptRules);
|
|
70
|
+
}
|
|
71
|
+
if (typeof data?.content === "string") {
|
|
72
|
+
return extractStructuredJson(data.content);
|
|
73
|
+
}
|
|
74
|
+
return data;
|
|
75
|
+
} finally {
|
|
76
|
+
clearTimeout(timer);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function adaptLegacyCloudFindings(data, doc, promptRules = []) {
|
|
81
|
+
const proseFallbackRule =
|
|
82
|
+
promptRules.find((r) => r.ruleId.endsWith("/stealth-vapor"))?.ruleId ||
|
|
83
|
+
promptRules[0]?.ruleId ||
|
|
84
|
+
`${doc.language || "en"}/prose/stealth-vapor`;
|
|
85
|
+
const mountedByFull = new Map(promptRules.map((r) => [r.ruleId, r.ruleId]));
|
|
86
|
+
const mountedByBare = new Map();
|
|
87
|
+
for (const r of promptRules) {
|
|
88
|
+
const bare = r.ruleId.split("/").pop();
|
|
89
|
+
if (!mountedByBare.has(bare)) mountedByBare.set(bare, r.ruleId);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const items = [];
|
|
93
|
+
for (const f of data.findings || []) {
|
|
94
|
+
if (!f || typeof f !== "object") continue;
|
|
95
|
+
const rawQuote = String(f.anchor?.quote || f.revision?.find || "").trim();
|
|
96
|
+
const seg =
|
|
97
|
+
(rawQuote && doc.segments.find((s) => s.text.includes(rawQuote))) ||
|
|
98
|
+
(rawQuote && doc.segments.find((s) => rawQuote.includes(s.text))) ||
|
|
99
|
+
null;
|
|
100
|
+
if (!seg) continue;
|
|
101
|
+
|
|
102
|
+
const quote = seg.text.includes(rawQuote) ? rawQuote : seg.text;
|
|
103
|
+
const bareId = f.ruleId ? String(f.ruleId).split("/").pop() : "";
|
|
104
|
+
const ruleId =
|
|
105
|
+
mountedByFull.get(f.ruleId) || mountedByBare.get(bareId) || proseFallbackRule;
|
|
106
|
+
const priority = f.severity === "error" ? "strong" : f.severity === "warning" ? "moderate" : "hint";
|
|
107
|
+
const rev = f.revision || {};
|
|
108
|
+
|
|
109
|
+
let actionType = "prompt";
|
|
110
|
+
let fixSafety = "manual";
|
|
111
|
+
let suggestedReplacement = "";
|
|
112
|
+
let rewritePrompt = String(rev.instruction || rev.question || f.why || "").trim();
|
|
113
|
+
if (rev.kind === "edit") {
|
|
114
|
+
const rep = String(rev.replace ?? "");
|
|
115
|
+
actionType = rep === "" ? "delete" : "replace";
|
|
116
|
+
fixSafety = "review";
|
|
117
|
+
suggestedReplacement = rep;
|
|
118
|
+
rewritePrompt = "";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
items.push({
|
|
122
|
+
ruleId,
|
|
123
|
+
targetId: seg.id,
|
|
124
|
+
priority,
|
|
125
|
+
fixSafety,
|
|
126
|
+
actionType,
|
|
127
|
+
originalQuote: quote,
|
|
128
|
+
suggestedReplacement,
|
|
129
|
+
reason: String(f.why || "").trim() || "Editorial issue flagged by Official Cloud.",
|
|
130
|
+
rewritePrompt,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
documentToneAnchor: String(data?.llmReview?.summary || "").trim(),
|
|
136
|
+
items,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { extractStructuredJson } from "../json-extract.mjs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tier 2 Google Gemini `:generateContent` adapter.
|
|
5
|
+
*/
|
|
6
|
+
export async function invokeGeminiProvider({
|
|
7
|
+
provider,
|
|
8
|
+
payload,
|
|
9
|
+
timeoutMs = 60000,
|
|
10
|
+
fetchImpl = globalThis.fetch,
|
|
11
|
+
}) {
|
|
12
|
+
const baseUrl = String(
|
|
13
|
+
provider.baseUrl || "https://generativelanguage.googleapis.com/v1beta",
|
|
14
|
+
).replace(/\/+$/, "");
|
|
15
|
+
const model = String(provider.model || "gemini-3.8-flash").replace(/^models\//, "");
|
|
16
|
+
|
|
17
|
+
if (!provider.apiKey) {
|
|
18
|
+
const err = new Error(`Gemini provider '${provider.name}' is missing an API key`);
|
|
19
|
+
err.authError = true;
|
|
20
|
+
throw err;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const url = `${baseUrl}/models/${encodeURIComponent(model)}:generateContent`;
|
|
24
|
+
const controller = new AbortController();
|
|
25
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const res = await fetchImpl(url, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
"x-goog-api-key": provider.apiKey,
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify({
|
|
35
|
+
systemInstruction: {
|
|
36
|
+
parts: [{ text: payload.systemPrompt }],
|
|
37
|
+
},
|
|
38
|
+
contents: [
|
|
39
|
+
{
|
|
40
|
+
role: "user",
|
|
41
|
+
parts: [{ text: payload.userPrompt }],
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
generationConfig: {
|
|
45
|
+
temperature: 0.1,
|
|
46
|
+
maxOutputTokens: provider.maxOutputTokens || 8192,
|
|
47
|
+
responseMimeType: "application/json",
|
|
48
|
+
},
|
|
49
|
+
}),
|
|
50
|
+
signal: controller.signal,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (res.status === 401 || res.status === 403) {
|
|
54
|
+
const err = new Error(`Gemini provider '${provider.name}' rejected API key (HTTP ${res.status})`);
|
|
55
|
+
err.authError = true;
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
58
|
+
if (!res.ok) {
|
|
59
|
+
const errText = await res.text().catch(() => "");
|
|
60
|
+
throw new Error(`Gemini provider '${provider.name}' HTTP ${res.status}: ${errText.slice(0, 240)}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const json = await res.json();
|
|
64
|
+
const candidate = json?.candidates?.[0] || {};
|
|
65
|
+
const finishReason = candidate?.finishReason || "";
|
|
66
|
+
const parts = candidate?.content?.parts || [];
|
|
67
|
+
const content = parts
|
|
68
|
+
.filter((p) => !p?.thought)
|
|
69
|
+
.map((p) => p.text || "")
|
|
70
|
+
.join("\n")
|
|
71
|
+
.trim();
|
|
72
|
+
if (!content) {
|
|
73
|
+
if (finishReason === "MAX_TOKENS") {
|
|
74
|
+
throw new Error(`Gemini provider '${provider.name}' truncated output at MAX_TOKENS before returning JSON`);
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`Gemini provider '${provider.name}' returned empty candidate parts`);
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
return extractStructuredJson(content);
|
|
80
|
+
} catch (parseErr) {
|
|
81
|
+
if (finishReason === "MAX_TOKENS") {
|
|
82
|
+
throw new Error(
|
|
83
|
+
`Gemini provider '${provider.name}' hit MAX_TOKENS while emitting JSON: ${parseErr.message}`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
throw parseErr;
|
|
87
|
+
}
|
|
88
|
+
} finally {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { DislopConfigError } from "../../kernel/config.mjs";
|
|
4
|
+
import { extractHostname, isLoopbackHostname } from "../privacy-gate.mjs";
|
|
5
|
+
import { invokeOpenAiCompatProvider } from "./openai-compat.mjs";
|
|
6
|
+
|
|
7
|
+
export const LOCAL_MODEL_CATALOG = Object.freeze({
|
|
8
|
+
"qwen2.5-1.5b-instruct": {
|
|
9
|
+
name: "qwen2.5-1.5b-instruct",
|
|
10
|
+
ollamaTag: "qwen2.5:1.5b",
|
|
11
|
+
sizeLabel: "1.0 GB",
|
|
12
|
+
maxInputTokens: 6000,
|
|
13
|
+
description: "Fast lightweight local model for basic semantic anti-slop checks.",
|
|
14
|
+
},
|
|
15
|
+
"qwen2.5-7b-instruct": {
|
|
16
|
+
name: "qwen2.5-7b-instruct",
|
|
17
|
+
ollamaTag: "qwen2.5:7b",
|
|
18
|
+
sizeLabel: "4.7 GB",
|
|
19
|
+
maxInputTokens: 12000,
|
|
20
|
+
description: "Balanced local model for bilingual EN/ZH editorial suggestions.",
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const OLLAMA_INSTALL_HINT =
|
|
25
|
+
"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'.";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fast zero-subprocess check for whether the `ollama` CLI binary is installed locally.
|
|
29
|
+
*/
|
|
30
|
+
export function detectOllamaBinary(env = process.env) {
|
|
31
|
+
if (env.DISLOP_ASSUME_NO_OLLAMA === "1") {
|
|
32
|
+
return { installed: false, binaryPath: null };
|
|
33
|
+
}
|
|
34
|
+
if (env.DISLOP_OLLAMA_BIN !== undefined) {
|
|
35
|
+
const custom = String(env.DISLOP_OLLAMA_BIN).trim();
|
|
36
|
+
if (custom && fs.existsSync(custom)) {
|
|
37
|
+
return { installed: true, binaryPath: custom };
|
|
38
|
+
}
|
|
39
|
+
return { installed: false, binaryPath: null };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const isWin = process.platform === "win32";
|
|
43
|
+
const binNames = isWin ? ["ollama.exe", "ollama.cmd", "ollama"] : ["ollama"];
|
|
44
|
+
const pathDirs = String(env.PATH || "")
|
|
45
|
+
.split(path.delimiter)
|
|
46
|
+
.map((p) => p.trim())
|
|
47
|
+
.filter(Boolean);
|
|
48
|
+
const fallbackDirs = isWin ? [] : ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"];
|
|
49
|
+
|
|
50
|
+
for (const dir of [...pathDirs, ...fallbackDirs]) {
|
|
51
|
+
for (const name of binNames) {
|
|
52
|
+
const candidate = path.join(dir, name);
|
|
53
|
+
try {
|
|
54
|
+
if (fs.statSync(candidate).isFile()) {
|
|
55
|
+
return { installed: true, binaryPath: candidate };
|
|
56
|
+
}
|
|
57
|
+
} catch {
|
|
58
|
+
// continue scanning
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return { installed: false, binaryPath: null };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function readInstalledModelsManifest(modelsDir) {
|
|
67
|
+
const manifestPath = path.join(modelsDir, "manifest.json");
|
|
68
|
+
if (!fs.existsSync(manifestPath)) {
|
|
69
|
+
return { activeModel: null, installed: {} };
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const raw = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
73
|
+
return {
|
|
74
|
+
activeModel: raw.activeModel || null,
|
|
75
|
+
installed: raw.installed && typeof raw.installed === "object" ? raw.installed : {},
|
|
76
|
+
};
|
|
77
|
+
} catch {
|
|
78
|
+
return { activeModel: null, installed: {} };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function resolveInstalledLocalModel(config, env = process.env) {
|
|
83
|
+
const manifest = readInstalledModelsManifest(config.modelsDir);
|
|
84
|
+
const activeKey =
|
|
85
|
+
manifest.activeModel && manifest.installed[manifest.activeModel]
|
|
86
|
+
? manifest.activeModel
|
|
87
|
+
: Object.keys(manifest.installed)[0] || null;
|
|
88
|
+
|
|
89
|
+
if (!activeKey) return null;
|
|
90
|
+
const entry = manifest.installed[activeKey];
|
|
91
|
+
const baseUrl =
|
|
92
|
+
env.DISLOP_LOCAL_MODEL_URL || entry.baseUrl || "http://127.0.0.1:11434/v1";
|
|
93
|
+
const hostname = extractHostname(baseUrl);
|
|
94
|
+
if (!hostname || !isLoopbackHostname(hostname)) {
|
|
95
|
+
throw new DislopConfigError(
|
|
96
|
+
`Refusing non-loopback endpoint '${baseUrl}' for local model tier. Only localhost/127.0.0.1 are permitted in local mode.`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
tier: "local",
|
|
102
|
+
name: activeKey,
|
|
103
|
+
protocol: "openai",
|
|
104
|
+
baseUrl,
|
|
105
|
+
apiKey: entry.apiKey || "local",
|
|
106
|
+
model: entry.runtimeModel || entry.ollamaTag || activeKey,
|
|
107
|
+
structuredOutput: "json_object",
|
|
108
|
+
maxInputTokens: entry.maxInputTokens || 8000,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Tier 3 Managed Local Small Model adapter (loopback OpenAI-compatible runtime via Ollama).
|
|
114
|
+
*/
|
|
115
|
+
export async function invokeLocalModelProvider({
|
|
116
|
+
localModel,
|
|
117
|
+
payload,
|
|
118
|
+
timeoutMs = 60000,
|
|
119
|
+
fetchImpl = globalThis.fetch,
|
|
120
|
+
env = process.env,
|
|
121
|
+
}) {
|
|
122
|
+
try {
|
|
123
|
+
return await invokeOpenAiCompatProvider({
|
|
124
|
+
provider: localModel,
|
|
125
|
+
payload,
|
|
126
|
+
timeoutMs,
|
|
127
|
+
fetchImpl,
|
|
128
|
+
});
|
|
129
|
+
} catch (err) {
|
|
130
|
+
if (!err?.authError && /Unable to reach provider/i.test(String(err?.message || ""))) {
|
|
131
|
+
const ollama = detectOllamaBinary(env);
|
|
132
|
+
if (!ollama.installed) {
|
|
133
|
+
const missingErr = new Error(
|
|
134
|
+
`Ollama is missing on this machine (required for Tier 3 '--engine local'). ${OLLAMA_INSTALL_HINT}`,
|
|
135
|
+
);
|
|
136
|
+
missingErr.ollamaMissing = true;
|
|
137
|
+
throw missingErr;
|
|
138
|
+
}
|
|
139
|
+
const stoppedErr = new Error(
|
|
140
|
+
`${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}').`,
|
|
141
|
+
);
|
|
142
|
+
stoppedErr.ollamaUnreachable = true;
|
|
143
|
+
throw stoppedErr;
|
|
144
|
+
}
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -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
|
+
}
|