@goodandready/dsh-moa 0.2.77 → 0.2.78
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/README.md +2 -0
- package/README.ru.md +2 -0
- package/README.zh.md +2 -0
- package/lib/client.js +52 -2
- package/lib/events.js +2 -0
- package/lib/moa-schema.js +14 -0
- package/lib/pipeline/candidates.js +20 -2
- package/lib/pipeline/privacy-sanitizer.js +213 -0
- package/lib/pipeline/runner.js +31 -6
- package/lib/pipeline/speculative-verifier.js +212 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,6 +56,8 @@ Single-model AI generation often suffers from blind spots, single-perspective bi
|
|
|
56
56
|
13. **Chain-of-Verification (CoVe) Fact-Checking Gate for Code (#314)**: Independent pre-judge verification formulating factual API questions, validating modules and functions against standard library and project manifests to eliminate hallucinated methods.
|
|
57
57
|
14. **Runtime CPU & Memory Profiler Gate (#315)**: Sandboxed micro-benchmarking of candidate execution duration, ops/sec throughput, and heap usage with hard timeout protection to select algorithmically optimal code.
|
|
58
58
|
15. **Mutation Testing Gate for Candidate Tests (#316)**: Injects AST-level fault mutations to compute Mutation Score (killed vs survived mutants), weeding out tautological assertions and fake test suites.
|
|
59
|
+
16. **Hybrid Private Edge MoA & Cloud Judge Sanitizer (#306)**: Preserves confidentiality with local edge proposers on MiniPC while scrubbing absolute paths, internal IPs, and API secrets before external Cloud Judge arbitration.
|
|
60
|
+
17. **Heterogeneous Speculative Decoding (#317)**: High-throughput local edge draft coupled with single-pass cloud model verification, slashing response latency by 2-3x.
|
|
59
61
|
|
|
60
62
|
---
|
|
61
63
|
|
package/README.ru.md
CHANGED
|
@@ -56,6 +56,8 @@
|
|
|
56
56
|
13. **Фильтр верификации фактов и API (CoVe) (#314)**: Автономная цепочка верификации (Chain-of-Verification), формулирующая проверочные вопросы по вызовам методов и пакетам, исключающая галлюцинированные и несуществующие API библиотек.
|
|
57
57
|
14. **Профайлер производительности CPU и памяти (#315)**: Изолированный бенчмаркинг времени выполнения кандидатов, пропускной способности (ops/sec) и потребления heap-памяти с защитой от зависаний для выбора алгоритмически оптимального кода.
|
|
58
58
|
15. **Мутационное тестирование тестов кандидатов (#316)**: Внедрение AST-мутаций кода для расчёта Mutation Score (убитые vs выжившие мутанты), устраняющее ложноположительные и тавтологические тесты.
|
|
59
|
+
16. **Гибридный приватный Edge MoA (#306)**: Локальные генераторы на MiniPC сохраняют секреты и кодовую базу в контуре, маскируя пути, IP и токены перед отправкой судье в облако.
|
|
60
|
+
17. **Гетерогенное спекулятивное декодирование (#317)**: Быстрый черновик от локальной edge-модели с пакетной валидацией и доработкой флагманом в облаке с ускорением в 2-3 раза.
|
|
59
61
|
|
|
60
62
|
---
|
|
61
63
|
|
package/README.zh.md
CHANGED
|
@@ -56,6 +56,8 @@
|
|
|
56
56
|
13. **代码事实与 API 验证链关卡 (CoVe) (#314)**:独立的裁判前事实核验,生成针对性 API 问题并对照标准库及项目配置清单,杜绝调用虚构不存在的幻觉方法。
|
|
57
57
|
14. **运行期 CPU 与内存性能分析关卡 (#315)**:在隔离沙箱中微基准测试候选方案执行延迟、吞吐量(ops/sec)与堆内存占用,带有硬超时保护,指引裁判挑选算法最优代码。
|
|
58
58
|
15. **候选测试套件变异测试关卡 (#316)**:注入 AST 级别故障变异以计算变异得分(消灭 vs 存活变异体),剔除恒真废测试与虚假测试套件。
|
|
59
|
+
16. **混合隐私边缘 MoA 与云端裁判脱敏 (#306)**:本地 MiniPC 边缘模型在内网读取代码生成方案,在提交给外部云端裁判前自动脱敏绝对路径、内网 IP 与 API 密钥。
|
|
60
|
+
17. **异构投机解码加速 (#317)**:本地小模型快速生成投机代码草稿块,云端旗舰模型单次批处理验证与完善,首包延迟降低 2-3 倍。
|
|
59
61
|
|
|
60
62
|
---
|
|
61
63
|
|
package/lib/client.js
CHANGED
|
@@ -236,7 +236,11 @@ window.__ModuleLoader__.load({
|
|
|
236
236
|
"aggregator.profiler_gate_title": "\u26A1 Automated Runtime Memory & CPU Profiler Gate",
|
|
237
237
|
"aggregator.profiler_gate_desc": "Runs isolated micro-benchmarks measuring execution latency, throughput (ops/sec), and heap memory usage.",
|
|
238
238
|
"aggregator.mutation_gate_title": "\u{1F9EC} Runtime Mutation Testing Gate for Candidate Tests",
|
|
239
|
-
"aggregator.mutation_gate_desc": "Injects synthetic faults into candidate code to test test-suite robustness, eliminating tautological and weak tests."
|
|
239
|
+
"aggregator.mutation_gate_desc": "Injects synthetic faults into candidate code to test test-suite robustness, eliminating tautological and weak tests.",
|
|
240
|
+
"aggregator.privacy_gate_title": "\u{1F6E1}\uFE0F Hybrid Private Edge MoA (Local Edge Proposers + Cloud Judge)",
|
|
241
|
+
"aggregator.privacy_gate_desc": "Scans and redacts proprietary secrets, local paths, and private network IPs before forwarding proposals to external cloud judges.",
|
|
242
|
+
"aggregator.speculative_gate_title": "\u26A1 Heterogeneous Speculative Decoding (Local Draft + Cloud Verifier)",
|
|
243
|
+
"aggregator.speculative_gate_desc": "Lightweight local edge model generates high-speed token drafts verified and completed in one fast pass by the cloud model."
|
|
240
244
|
};
|
|
241
245
|
const zh = {
|
|
242
246
|
title: "\u667A\u80FD\u4F53\u6DF7\u5408 (MoA)",
|
|
@@ -463,7 +467,11 @@ window.__ModuleLoader__.load({
|
|
|
463
467
|
"aggregator.profiler_gate_title": "\u26A1 \u81EA\u52A8\u8FD0\u884C\u65F6 CPU \u4E0E\u5185\u5B58\u6027\u80FD\u5206\u6790\u5173\u5361",
|
|
464
468
|
"aggregator.profiler_gate_desc": "\u5728\u9694\u79BB\u6C99\u7BB1\u4E2D\u8FD0\u884C\u5FAE\u57FA\u51C6\u6D4B\u8BD5\uFF0C\u7CBE\u786E\u5EA6\u91CF\u6267\u884C\u5EF6\u8FDF\u3001\u541E\u5410\u91CF (ops/sec) \u548C\u5806\u5185\u5B58\u5206\u914D\u3002",
|
|
465
469
|
"aggregator.mutation_gate_title": "\u{1F9EC} \u5019\u9009\u6D4B\u8BD5\u5957\u4EF6\u8FD0\u884C\u65F6\u53D8\u5F02\u6D4B\u8BD5\u5173\u5361",
|
|
466
|
-
"aggregator.mutation_gate_desc": "\u5411\u4EE3\u7801\u6CE8\u5165\u5408\u6210\u6545\u969C\u5E76\u9A8C\u8BC1\u6D4B\u8BD5\u7684\u6355\u83B7\u80FD\u529B\uFF0C\u5254\u9664\u540C\u4E49\u53CD\u590D\u4E0E\u65E0\u6548\u7684\u865A\u5047\u6D4B\u8BD5\u3002"
|
|
470
|
+
"aggregator.mutation_gate_desc": "\u5411\u4EE3\u7801\u6CE8\u5165\u5408\u6210\u6545\u969C\u5E76\u9A8C\u8BC1\u6D4B\u8BD5\u7684\u6355\u83B7\u80FD\u529B\uFF0C\u5254\u9664\u540C\u4E49\u53CD\u590D\u4E0E\u65E0\u6548\u7684\u865A\u5047\u6D4B\u8BD5\u3002",
|
|
471
|
+
"aggregator.privacy_gate_title": "\u{1F6E1}\uFE0F \u6DF7\u5408\u9690\u79C1\u8FB9\u7F18 MoA (\u672C\u5730\u751F\u6210 + \u4E91\u7AEF\u88C1\u5224)",
|
|
472
|
+
"aggregator.privacy_gate_desc": "\u5728\u5C06\u5019\u9009\u63D0\u6848\u53D1\u9001\u7ED9\u5916\u90E8\u4E91\u7AEF\u88C1\u5224\u4E4B\u524D\uFF0C\u626B\u63CF\u5E76\u8131\u654F\u4E13\u6709\u5BC6\u94A5\u3001\u672C\u5730\u7EDD\u5BF9\u8DEF\u5F84\u4E0E\u79C1\u6709\u7F51\u7EDC IP\u3002",
|
|
473
|
+
"aggregator.speculative_gate_title": "\u26A1 \u5F02\u6784\u6295\u673A\u89E3\u7801 (\u672C\u5730\u8349\u7A3F + \u4E91\u7AEF\u9A8C\u8BC1)",
|
|
474
|
+
"aggregator.speculative_gate_desc": "\u8F7B\u91CF\u7EA7\u672C\u5730\u8FB9\u7F18\u6A21\u578B\u751F\u6210\u9AD8\u901F\u8349\u7A3F\uFF0C\u7531\u5F3A\u5927\u7684\u4E91\u7AEF\u6A21\u578B\u4E00\u6B21\u6027\u5FEB\u901F\u9A8C\u8BC1\u5E76\u5B8C\u5584\u3002"
|
|
467
475
|
};
|
|
468
476
|
function makeT(dict, fallback) {
|
|
469
477
|
return function t(key, vars) {
|
|
@@ -2710,6 +2718,48 @@ window.__ModuleLoader__.load({
|
|
|
2710
2718
|
t("aggregator.mutation_gate_desc") || "Injects synthetic faults into candidate code to test test-suite robustness, eliminating tautological and weak tests."
|
|
2711
2719
|
)
|
|
2712
2720
|
),
|
|
2721
|
+
/* Hybrid Edge Privacy Gate (#306) */
|
|
2722
|
+
React.createElement(
|
|
2723
|
+
"div",
|
|
2724
|
+
{ style: { display: "flex", flexDirection: "column", gap: 4 } },
|
|
2725
|
+
React.createElement(
|
|
2726
|
+
"label",
|
|
2727
|
+
{ style: { display: "flex", alignItems: "center", gap: 8, fontSize: 13, fontWeight: 600, color: "var(--dsw-alias-label-primary)", cursor: "pointer" } },
|
|
2728
|
+
React.createElement("input", {
|
|
2729
|
+
type: "checkbox",
|
|
2730
|
+
checked: Boolean(currentPreset.hybrid_edge_privacy_enabled),
|
|
2731
|
+
onChange: (e) => updateCurrentPreset((p) => ({ ...p, hybrid_edge_privacy_enabled: e.target.checked })),
|
|
2732
|
+
style: { accentColor: "var(--dsw-alias-state-brand-primary, var(--dsw-alias-label-primary))", cursor: "pointer" }
|
|
2733
|
+
}),
|
|
2734
|
+
t("aggregator.privacy_gate_title") || "\u{1F6E1}\uFE0F Hybrid Private Edge MoA (Local Edge Proposers + Cloud Judge)"
|
|
2735
|
+
),
|
|
2736
|
+
React.createElement(
|
|
2737
|
+
"div",
|
|
2738
|
+
{ style: { fontSize: 12, color: "var(--dsw-alias-label-secondary)", marginLeft: 22 } },
|
|
2739
|
+
t("aggregator.privacy_gate_desc") || "Scans and redacts proprietary secrets, local paths, and private network IPs before forwarding proposals to external cloud judges."
|
|
2740
|
+
)
|
|
2741
|
+
),
|
|
2742
|
+
/* Heterogeneous Speculative Decoding (#317) */
|
|
2743
|
+
React.createElement(
|
|
2744
|
+
"div",
|
|
2745
|
+
{ style: { display: "flex", flexDirection: "column", gap: 4 } },
|
|
2746
|
+
React.createElement(
|
|
2747
|
+
"label",
|
|
2748
|
+
{ style: { display: "flex", alignItems: "center", gap: 8, fontSize: 13, fontWeight: 600, color: "var(--dsw-alias-label-primary)", cursor: "pointer" } },
|
|
2749
|
+
React.createElement("input", {
|
|
2750
|
+
type: "checkbox",
|
|
2751
|
+
checked: Boolean(currentPreset.speculative_draft_enabled),
|
|
2752
|
+
onChange: (e) => updateCurrentPreset((p) => ({ ...p, speculative_draft_enabled: e.target.checked })),
|
|
2753
|
+
style: { accentColor: "var(--dsw-alias-state-brand-primary, var(--dsw-alias-label-primary))", cursor: "pointer" }
|
|
2754
|
+
}),
|
|
2755
|
+
t("aggregator.speculative_gate_title") || "\u26A1 Heterogeneous Speculative Decoding (Local Draft + Cloud Verifier)"
|
|
2756
|
+
),
|
|
2757
|
+
React.createElement(
|
|
2758
|
+
"div",
|
|
2759
|
+
{ style: { fontSize: 12, color: "var(--dsw-alias-label-secondary)", marginLeft: 22 } },
|
|
2760
|
+
t("aggregator.speculative_gate_desc") || "Lightweight local edge model generates high-speed token drafts verified and completed in one fast pass by the cloud model."
|
|
2761
|
+
)
|
|
2762
|
+
),
|
|
2713
2763
|
/* Fallback Judges Chain */
|
|
2714
2764
|
React.createElement(
|
|
2715
2765
|
"div",
|
package/lib/events.js
CHANGED
|
@@ -15,6 +15,8 @@ export const MOA_EVENTS = {
|
|
|
15
15
|
COVE_VERIFIED: 'moa/cove-verified',
|
|
16
16
|
PROFILER_COMPLETE: 'moa/profiler-complete',
|
|
17
17
|
MUTATION_COMPLETE: 'moa/mutation-complete',
|
|
18
|
+
PRIVACY_SANITIZED: 'moa/privacy-sanitized',
|
|
19
|
+
SPECULATIVE_DRAFT: 'moa/speculative-draft',
|
|
18
20
|
CRITIQUE_COMPLETE: 'moa/critique-complete',
|
|
19
21
|
JUDGE_VERDICT: 'moa/judge-verdict',
|
|
20
22
|
CONTEXT_PRUNING: 'moa/context-pruning',
|
package/lib/moa-schema.js
CHANGED
|
@@ -100,6 +100,13 @@ export const PresetSchema = z.object({
|
|
|
100
100
|
profiler_timeout_ms: z.number().default(2000),
|
|
101
101
|
mutation_testing_enabled: z.boolean().default(false),
|
|
102
102
|
min_mutation_score: z.number().default(0.70),
|
|
103
|
+
hybrid_edge_privacy_enabled: z.boolean().default(false),
|
|
104
|
+
cloud_judge_sanitization_enabled: z.boolean().default(true),
|
|
105
|
+
redact_paths: z.boolean().default(true),
|
|
106
|
+
redact_secrets: z.boolean().default(true),
|
|
107
|
+
redact_ips: z.boolean().default(true),
|
|
108
|
+
speculative_draft_enabled: z.boolean().default(false),
|
|
109
|
+
speculative_block_size: z.number().default(256),
|
|
103
110
|
})
|
|
104
111
|
|
|
105
112
|
export const Config = z.object({
|
|
@@ -146,6 +153,13 @@ export const Config = z.object({
|
|
|
146
153
|
profiler_timeout_ms: z.number().default(2000).volatile(),
|
|
147
154
|
mutation_testing_enabled: z.boolean().default(false).volatile(),
|
|
148
155
|
min_mutation_score: z.number().default(0.70).volatile(),
|
|
156
|
+
hybrid_edge_privacy_enabled: z.boolean().default(false).volatile(),
|
|
157
|
+
cloud_judge_sanitization_enabled: z.boolean().default(true).volatile(),
|
|
158
|
+
redact_paths: z.boolean().default(true).volatile(),
|
|
159
|
+
redact_secrets: z.boolean().default(true).volatile(),
|
|
160
|
+
redact_ips: z.boolean().default(true).volatile(),
|
|
161
|
+
speculative_draft_enabled: z.boolean().default(false).volatile(),
|
|
162
|
+
speculative_block_size: z.number().default(256).volatile(),
|
|
149
163
|
smart_routing_model: ModelSlotSchema.default({ provider: '', model: '' }).volatile(),
|
|
150
164
|
prices: z.dict(PriceRow).default({}).volatile(),
|
|
151
165
|
presets: z.array(PresetSchema).default([
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createSpeculativeCallLlm } from './speculative-verifier.js'
|
|
1
2
|
import { createCachingCallLlm } from './semantic-cache.js'
|
|
2
3
|
import { runSpeculativeMoa } from '../moa-speculative.js'
|
|
3
4
|
import { runAdversarialTddLoop } from '../moa-adversarial-tdd.js'
|
|
@@ -47,6 +48,23 @@ export async function stageCandidateRound1({
|
|
|
47
48
|
ctx,
|
|
48
49
|
})
|
|
49
50
|
: callLlm
|
|
51
|
+
let candidateCallLlm = effectiveCallLlm
|
|
52
|
+
const draftModelSlot = typeof preset?.speculative_draft_model === 'object' && preset?.speculative_draft_model?.model
|
|
53
|
+
? preset.speculative_draft_model
|
|
54
|
+
: typeof preset?.speculative_draft_model === 'string' && preset?.speculative_draft_model
|
|
55
|
+
? { provider: 'ollama', model: preset.speculative_draft_model }
|
|
56
|
+
: null
|
|
57
|
+
|
|
58
|
+
if (preset?.speculative_draft_enabled && draftModelSlot) {
|
|
59
|
+
candidateCallLlm = createSpeculativeCallLlm(effectiveCallLlm, {
|
|
60
|
+
draftSlot: draftModelSlot,
|
|
61
|
+
blockSize: preset.speculative_block_size || 256,
|
|
62
|
+
ctx,
|
|
63
|
+
runId,
|
|
64
|
+
onProgress,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
50
68
|
const lastMsgText = enrichedMessages[enrichedMessages.length - 1]?.content || ''
|
|
51
69
|
const adaptiveRefs = preset?.adaptive_personas_enabled
|
|
52
70
|
? assignAdaptivePersonas(effectiveReferences, typeof lastMsgText === 'string' ? lastMsgText : '').references
|
|
@@ -75,7 +93,7 @@ export async function stageCandidateRound1({
|
|
|
75
93
|
|
|
76
94
|
if (!referenceOutputs) {
|
|
77
95
|
referenceOutputs = preset?.subagent_swarm_enabled
|
|
78
|
-
? await runSubagentSwarm(adaptiveRefs, enrichedMessages, { temperature: refTemp, maxTokens, signal, cwd, runId },
|
|
96
|
+
? await runSubagentSwarm(adaptiveRefs, enrichedMessages, { temperature: refTemp, maxTokens, signal, cwd, runId }, candidateCallLlm, onProgress)
|
|
79
97
|
: await runReferencesParallel(
|
|
80
98
|
adaptiveRefs,
|
|
81
99
|
enrichedMessages,
|
|
@@ -93,7 +111,7 @@ export async function stageCandidateRound1({
|
|
|
93
111
|
maxRetries: candidateRetries,
|
|
94
112
|
signal,
|
|
95
113
|
},
|
|
96
|
-
|
|
114
|
+
candidateCallLlm,
|
|
97
115
|
onProgress
|
|
98
116
|
)
|
|
99
117
|
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSeek Harness Mixture of Agents (MoA) — Hybrid Private Edge & Privacy Sanitizer (#306)
|
|
3
|
+
*
|
|
4
|
+
* Protects project confidentiality in hybrid deployments by scrubbing local absolute paths,
|
|
5
|
+
* internal private IPs, API tokens, JWTs, and proprietary secrets before forwarding prompts
|
|
6
|
+
* or candidate proposals to external Cloud Judges. Preserves unsanitized originals locally
|
|
7
|
+
* for workspace promotion.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { MOA_EVENTS, emitEvent } from '../events.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Common regex definitions for sensitive data redaction.
|
|
14
|
+
*/
|
|
15
|
+
export const SANITIZATION_PATTERNS = {
|
|
16
|
+
OPENAI_OR_GENERIC_KEY: /sk-[a-zA-Z0-9_\-]{20,}/g,
|
|
17
|
+
BEARER_TOKEN: /Bearer\s+[a-zA-Z0-9_\-\.]{20,}/gi,
|
|
18
|
+
JWT: /ey[A-Za-z0-9_\-]{15,}\.ey[A-Za-z0-9_\-]{15,}\.[A-Za-z0-9_\-]{10,}/g,
|
|
19
|
+
AWS_KEY: /AKIA[0-9A-Z]{16}/g,
|
|
20
|
+
PRIVATE_KEY: /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/g,
|
|
21
|
+
SECRET_ASSIGNMENT: /(?<=(?:api[_-]?key|secret|password|token|auth)\s*[:=]\s*["'])[a-zA-Z0-9_\-\.\/+=]{8,}(?=["'])/gi,
|
|
22
|
+
UNIX_HOME_PATH: /(?:\/(?:home|Users)\/[a-zA-Z0-9_\-]+(?:\/[a-zA-Z0-9_\-\.]+)*)/g,
|
|
23
|
+
WIN_USER_PATH: /(?:[A-Za-z]:\\(?:Users|Documents and Settings)\\[a-zA-Z0-9_\-]+(?:\\[a-zA-Z0-9_\-\.]+)*)/g,
|
|
24
|
+
PRIVATE_IP: /\b(?:192\.168\.\d{1,3}\.\d{1,3}|10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(?:1[6-9]|2\d|3[0-1])\.\d{1,3}\.\d{1,3})\b/g,
|
|
25
|
+
EMAIL: /\b[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}\b/g,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Sanitizes a text string by redacting sensitive secrets, internal paths, and PII.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} text
|
|
32
|
+
* @param {object} [options]
|
|
33
|
+
* @returns {{ sanitizedText: string, redactionCount: number, redactingEntries: Array<object> }}
|
|
34
|
+
*/
|
|
35
|
+
export function sanitizeSensitiveText(text, options = {}) {
|
|
36
|
+
if (!text || typeof text !== 'string') {
|
|
37
|
+
return { sanitizedText: text || '', redactionCount: 0, redactingEntries: [] }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const {
|
|
41
|
+
redactSecrets = true,
|
|
42
|
+
redactPaths = true,
|
|
43
|
+
redactIps = true,
|
|
44
|
+
redactEmails = true,
|
|
45
|
+
} = options
|
|
46
|
+
|
|
47
|
+
let result = text
|
|
48
|
+
let redactionCount = 0
|
|
49
|
+
const redactingEntries = []
|
|
50
|
+
|
|
51
|
+
// 1. Secrets & API Keys
|
|
52
|
+
if (redactSecrets) {
|
|
53
|
+
result = result.replace(SANITIZATION_PATTERNS.PRIVATE_KEY, () => {
|
|
54
|
+
redactionCount++
|
|
55
|
+
redactingEntries.push({ type: 'PRIVATE_KEY' })
|
|
56
|
+
return '[REDACTED_PRIVATE_KEY]'
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
result = result.replace(SANITIZATION_PATTERNS.OPENAI_OR_GENERIC_KEY, () => {
|
|
60
|
+
redactionCount++
|
|
61
|
+
redactingEntries.push({ type: 'API_KEY' })
|
|
62
|
+
return '[REDACTED_API_KEY]'
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
result = result.replace(SANITIZATION_PATTERNS.JWT, () => {
|
|
66
|
+
redactionCount++
|
|
67
|
+
redactingEntries.push({ type: 'JWT' })
|
|
68
|
+
return '[REDACTED_JWT]'
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
result = result.replace(SANITIZATION_PATTERNS.AWS_KEY, () => {
|
|
72
|
+
redactionCount++
|
|
73
|
+
redactingEntries.push({ type: 'AWS_KEY' })
|
|
74
|
+
return '[REDACTED_AWS_KEY]'
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
result = result.replace(SANITIZATION_PATTERNS.SECRET_ASSIGNMENT, () => {
|
|
78
|
+
redactionCount++
|
|
79
|
+
redactingEntries.push({ type: 'SECRET_VALUE' })
|
|
80
|
+
return '[REDACTED_SECRET]'
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 2. Private Paths
|
|
85
|
+
if (redactPaths) {
|
|
86
|
+
result = result.replace(SANITIZATION_PATTERNS.UNIX_HOME_PATH, (match) => {
|
|
87
|
+
redactionCount++
|
|
88
|
+
const parts = match.split('/')
|
|
89
|
+
const subpath = parts.slice(3).join('/')
|
|
90
|
+
return subpath ? `/workspace/${subpath}` : '/workspace'
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
result = result.replace(SANITIZATION_PATTERNS.WIN_USER_PATH, (match) => {
|
|
94
|
+
redactionCount++
|
|
95
|
+
const parts = match.split('\\')
|
|
96
|
+
const subpath = parts.slice(3).join('\\')
|
|
97
|
+
return subpath ? `C:\\workspace\\${subpath}` : 'C:\\workspace'
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 3. Private IP addresses
|
|
102
|
+
if (redactIps) {
|
|
103
|
+
result = result.replace(SANITIZATION_PATTERNS.PRIVATE_IP, () => {
|
|
104
|
+
redactionCount++
|
|
105
|
+
redactingEntries.push({ type: 'INTERNAL_IP' })
|
|
106
|
+
return '[REDACTED_INTERNAL_IP]'
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 4. Email addresses
|
|
111
|
+
if (redactEmails) {
|
|
112
|
+
result = result.replace(SANITIZATION_PATTERNS.EMAIL, () => {
|
|
113
|
+
redactionCount++
|
|
114
|
+
redactingEntries.push({ type: 'EMAIL' })
|
|
115
|
+
return '[REDACTED_EMAIL]'
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
sanitizedText: result,
|
|
121
|
+
redactionCount,
|
|
122
|
+
redactingEntries,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Sanitizes candidate solutions before forwarding them to an external Cloud Judge.
|
|
128
|
+
* Preserves the original unmodified code in candidate.originalOutput for safe local promotion.
|
|
129
|
+
*
|
|
130
|
+
* @param {Array<object>} candidates
|
|
131
|
+
* @param {object} [options]
|
|
132
|
+
* @returns {object} Sanitization result
|
|
133
|
+
*/
|
|
134
|
+
export function sanitizeCandidatesForCloudJudge(candidates, options = {}) {
|
|
135
|
+
const {
|
|
136
|
+
ctx,
|
|
137
|
+
runId,
|
|
138
|
+
onProgress,
|
|
139
|
+
redactSecrets = true,
|
|
140
|
+
redactPaths = true,
|
|
141
|
+
redactIps = true,
|
|
142
|
+
redactEmails = true,
|
|
143
|
+
} = options
|
|
144
|
+
|
|
145
|
+
if (!Array.isArray(candidates) || candidates.length === 0) {
|
|
146
|
+
return {
|
|
147
|
+
sanitizedCandidates: [],
|
|
148
|
+
totalRedactions: 0,
|
|
149
|
+
privacyDirective: null,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let totalRedactions = 0
|
|
154
|
+
const sanitizedCandidates = candidates.map((cand, idx) => {
|
|
155
|
+
const rawText = typeof cand === 'string' ? cand : cand.output || cand.text || cand.code || ''
|
|
156
|
+
const { sanitizedText, redactionCount, redactingEntries } = sanitizeSensitiveText(rawText, {
|
|
157
|
+
redactSecrets,
|
|
158
|
+
redactPaths,
|
|
159
|
+
redactIps,
|
|
160
|
+
redactEmails,
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
totalRedactions += redactionCount
|
|
164
|
+
|
|
165
|
+
if (typeof cand === 'object' && cand !== null) {
|
|
166
|
+
if (!cand.originalOutput) {
|
|
167
|
+
cand.originalOutput = rawText
|
|
168
|
+
}
|
|
169
|
+
cand.sanitizedOutput = sanitizedText
|
|
170
|
+
cand.redactionCount = redactionCount
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (ctx && redactionCount > 0) {
|
|
174
|
+
emitEvent(ctx, MOA_EVENTS.PRIVACY_SANITIZED, {
|
|
175
|
+
runId,
|
|
176
|
+
candidateIndex: idx,
|
|
177
|
+
model: cand?.model || `Candidate ${idx + 1}`,
|
|
178
|
+
redactionCount,
|
|
179
|
+
types: [...new Set(redactingEntries.map((e) => e.type))],
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return typeof cand === 'object' && cand !== null
|
|
184
|
+
? { ...cand, output: sanitizedText, text: sanitizedText }
|
|
185
|
+
: sanitizedText
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
if (typeof onProgress === 'function' && totalRedactions > 0) {
|
|
189
|
+
onProgress(`🛡️ *Hybrid Edge Privacy*: Sanitized ${totalRedactions} sensitive token(s) (secrets, paths, IPs) before cloud judge.\n`)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const privacyDirective = totalRedactions > 0 ? formatPrivacyDirective(totalRedactions) : null
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
sanitizedCandidates,
|
|
196
|
+
totalRedactions,
|
|
197
|
+
privacyDirective,
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Formats a privacy directive header for the Cloud Judge prompt.
|
|
203
|
+
*
|
|
204
|
+
* @param {number} totalRedactions
|
|
205
|
+
* @returns {string}
|
|
206
|
+
*/
|
|
207
|
+
export function formatPrivacyDirective(totalRedactions) {
|
|
208
|
+
return [
|
|
209
|
+
'### 🛡️ PRIVACY & PII SANITIZATION ACTIVE',
|
|
210
|
+
`Notice: ${totalRedactions} internal system path(s), private network IP(s), or secret token(s) have been sanitized with [REDACTED_*] or relative paths to preserve user confidentiality.`,
|
|
211
|
+
'Evaluate the candidate solutions based on their architecture, correctness, and logic.',
|
|
212
|
+
].join('\n')
|
|
213
|
+
}
|
package/lib/pipeline/runner.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sanitizeCandidatesForCloudJudge } from './privacy-sanitizer.js'
|
|
1
2
|
import { evaluateJudgeEscalation } from './escalation.js'
|
|
2
3
|
import { calculateSelfConsistencyAgreement } from './self-consistency.js'
|
|
3
4
|
import { pruneCandidateEnsemble, formatPruningDirective } from './context-pruner.js'
|
|
@@ -269,6 +270,30 @@ export async function runMoAPipeline({
|
|
|
269
270
|
mutationDirective = mutationGateResult.mutationDirective
|
|
270
271
|
}
|
|
271
272
|
|
|
273
|
+
// Pre-Stage 5.12: Hybrid Edge Privacy Sanitizer Gate (#306)
|
|
274
|
+
let privacyDirective = null
|
|
275
|
+
let candidatesForJudge = syntaxResult.validCandidates.length > 0 ? syntaxResult.validCandidates : successfulRefs
|
|
276
|
+
let sanitizedReferenceOutputs = referenceOutputs
|
|
277
|
+
if (preset?.hybrid_edge_privacy_enabled || preset?.cloud_judge_sanitization_enabled) {
|
|
278
|
+
const sanitizeResult = sanitizeCandidatesForCloudJudge(candidatesForJudge, {
|
|
279
|
+
ctx,
|
|
280
|
+
runId,
|
|
281
|
+
onProgress,
|
|
282
|
+
redactSecrets: preset.redact_secrets !== false,
|
|
283
|
+
redactPaths: preset.redact_paths !== false,
|
|
284
|
+
redactIps: preset.redact_ips !== false,
|
|
285
|
+
})
|
|
286
|
+
candidatesForJudge = sanitizeResult.sanitizedCandidates
|
|
287
|
+
privacyDirective = sanitizeResult.privacyDirective
|
|
288
|
+
sanitizedReferenceOutputs = referenceOutputs.map((r) => {
|
|
289
|
+
const matching = candidatesForJudge.find((c) => c.index === r.index || c.model === r.model || c.label === r.label)
|
|
290
|
+
if (matching && matching.sanitizedOutput) {
|
|
291
|
+
return { ...r, text: matching.sanitizedOutput, output: matching.sanitizedOutput }
|
|
292
|
+
}
|
|
293
|
+
return r
|
|
294
|
+
})
|
|
295
|
+
}
|
|
296
|
+
|
|
272
297
|
// Pre-Stage 6: Dynamic Consensus Threshold & Multi-Judge Escalation (#300)
|
|
273
298
|
const escalationResult = evaluateJudgeEscalation({
|
|
274
299
|
candidateOutputs: syntaxResult.validCandidates.length > 0 ? syntaxResult.validCandidates : successfulRefs,
|
|
@@ -317,9 +342,9 @@ export async function runMoAPipeline({
|
|
|
317
342
|
// Stage 6: Multi-Judge Panel
|
|
318
343
|
const mjRes = await stageMultiJudge({
|
|
319
344
|
preset: effectivePreset,
|
|
320
|
-
successfulRefs:
|
|
321
|
-
referenceOutputs, primaryJudge: effectivePrimaryJudge,
|
|
322
|
-
userPrompt, judgeCriteria: [ctxStage.judgeCriteria, coveDirective, securityDirective, profilerDirective, mutationDirective].filter(Boolean).join('\n\n'), prices, maxTokens: ctxStage.maxTokens,
|
|
345
|
+
successfulRefs: candidatesForJudge,
|
|
346
|
+
referenceOutputs: sanitizedReferenceOutputs, primaryJudge: effectivePrimaryJudge,
|
|
347
|
+
userPrompt, judgeCriteria: [ctxStage.judgeCriteria, coveDirective, securityDirective, profilerDirective, mutationDirective, privacyDirective].filter(Boolean).join('\n\n'), prices, maxTokens: ctxStage.maxTokens,
|
|
323
348
|
callLlm, signal, onProgress, cwd, runId, isRefinement: ctxStage.isRefinement,
|
|
324
349
|
isFastMode: ctxStage.isFastMode, collectedCtx: ctxStage.collectedCtx, startTime,
|
|
325
350
|
})
|
|
@@ -348,8 +373,8 @@ export async function runMoAPipeline({
|
|
|
348
373
|
: null
|
|
349
374
|
|
|
350
375
|
// Pre-Judge Stage 6.8: Cross-Candidate Unchanged Base Context Pruning (#305)
|
|
351
|
-
let synthCandidateRefs =
|
|
352
|
-
let synthReferenceOutputs =
|
|
376
|
+
let synthCandidateRefs = candidatesForJudge
|
|
377
|
+
let synthReferenceOutputs = sanitizedReferenceOutputs
|
|
353
378
|
let pruningDirective = null
|
|
354
379
|
if (ctxStage.contextPruningEnabled && ctxStage.collectedCtx?.files?.length > 0) {
|
|
355
380
|
const pruneRes = pruneCandidateEnsemble(synthCandidateRefs, ctxStage.collectedCtx.files, {
|
|
@@ -377,7 +402,7 @@ export async function runMoAPipeline({
|
|
|
377
402
|
}
|
|
378
403
|
}
|
|
379
404
|
|
|
380
|
-
const fileScoringDirective = [rawFileDirective, visualResult?.directive, adversarialTddResult?.directive, syntaxDirective, coveDirective, securityDirective, profilerDirective, mutationDirective, pruningDirective, ctxStage.stackAwareDirective, ctxStage.developerPreferencesDirective].filter(Boolean).join("\n\n") || null
|
|
405
|
+
const fileScoringDirective = [rawFileDirective, visualResult?.directive, adversarialTddResult?.directive, syntaxDirective, coveDirective, securityDirective, profilerDirective, mutationDirective, privacyDirective, pruningDirective, ctxStage.stackAwareDirective, ctxStage.developerPreferencesDirective].filter(Boolean).join("\n\n") || null
|
|
381
406
|
|
|
382
407
|
// Stage 7: Synthesis Phase
|
|
383
408
|
const synthRes = await stageSynthesis({
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSeek Harness Mixture of Agents (MoA) — Heterogeneous Speculative Decoding (#317)
|
|
3
|
+
*
|
|
4
|
+
* Implements a two-phase speculative pipeline:
|
|
5
|
+
* 1. A lightweight local edge model (e.g. Qwen 2.5 Coder 3B/7B via Ollama/MiniPC) rapidly produces
|
|
6
|
+
* a speculative draft code block at high token throughput.
|
|
7
|
+
* 2. A powerful cloud verifier model (e.g. DeepSeek R1, Claude, GPT) receives the speculative draft
|
|
8
|
+
* and executes batched validation, bug fixing, and completion in a single fast verification pass.
|
|
9
|
+
*
|
|
10
|
+
* Measures effective throughput (tok/sec) and speedup acceleration vs non-speculative generation.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { MOA_EVENTS, emitEvent } from '../events.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Estimates token count from text using character heuristic (~4 chars per token).
|
|
17
|
+
*
|
|
18
|
+
* @param {string} text
|
|
19
|
+
* @returns {number}
|
|
20
|
+
*/
|
|
21
|
+
export function estimateTokens(text) {
|
|
22
|
+
if (!text || typeof text !== 'string') return 0
|
|
23
|
+
return Math.max(1, Math.round(text.length / 4))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Executes a heterogeneous speculative decoding turn: local draft + cloud verifier.
|
|
28
|
+
*
|
|
29
|
+
* @param {object} params
|
|
30
|
+
* @param {string} params.prompt
|
|
31
|
+
* @param {object} params.draftSlot - { provider, model }
|
|
32
|
+
* @param {object} params.targetSlot - { provider, model }
|
|
33
|
+
* @param {Function} params.callLlm - LLM execution callback
|
|
34
|
+
* @param {object} [params.options]
|
|
35
|
+
* @returns {Promise<object>} Speculative decoding result
|
|
36
|
+
*/
|
|
37
|
+
export async function runSpeculativeVerification({
|
|
38
|
+
prompt,
|
|
39
|
+
draftSlot,
|
|
40
|
+
targetSlot,
|
|
41
|
+
callLlm,
|
|
42
|
+
options = {},
|
|
43
|
+
}) {
|
|
44
|
+
const {
|
|
45
|
+
ctx,
|
|
46
|
+
runId,
|
|
47
|
+
onProgress,
|
|
48
|
+
blockSize = 256,
|
|
49
|
+
signal,
|
|
50
|
+
} = options
|
|
51
|
+
|
|
52
|
+
if (!callLlm || typeof callLlm !== 'function') {
|
|
53
|
+
throw new Error('callLlm function is required for speculative verification')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const draftLabel = `${draftSlot?.provider || 'local'}:${draftSlot?.model || 'edge-draft'}`
|
|
57
|
+
const targetLabel = `${targetSlot?.provider || 'cloud'}:${targetSlot?.model || 'verifier'}`
|
|
58
|
+
|
|
59
|
+
// Phase 1: Local Edge Draft Generation
|
|
60
|
+
let draftText = ''
|
|
61
|
+
let draftDurationMs = 0
|
|
62
|
+
const draftStart = Date.now()
|
|
63
|
+
|
|
64
|
+
if (typeof onProgress === 'function') {
|
|
65
|
+
onProgress(`⚡ *Speculative Decoding*: Generating speculative draft (${blockSize} tok) via ` + draftLabel + '...\n')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const draftRes = await callLlm({
|
|
70
|
+
provider: draftSlot?.provider,
|
|
71
|
+
model: draftSlot?.model,
|
|
72
|
+
messages: [{ role: 'user', content: prompt }],
|
|
73
|
+
maxTokens: blockSize,
|
|
74
|
+
temperature: 0.3,
|
|
75
|
+
signal,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
draftText = typeof draftRes === 'string' ? draftRes : draftRes?.content || draftRes?.text || ''
|
|
79
|
+
draftDurationMs = Math.max(1, Date.now() - draftStart)
|
|
80
|
+
} catch (err) {
|
|
81
|
+
// Graceful fallback: If draft model fails, target model generates directly
|
|
82
|
+
if (typeof onProgress === 'function') {
|
|
83
|
+
onProgress(`⚠️ *Speculative Draft Failed* (${err?.message || err}); falling back to direct cloud generation...\n`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const directStart = Date.now()
|
|
87
|
+
const directRes = await callLlm({
|
|
88
|
+
provider: targetSlot?.provider,
|
|
89
|
+
model: targetSlot?.model,
|
|
90
|
+
messages: [{ role: 'user', content: prompt }],
|
|
91
|
+
signal,
|
|
92
|
+
})
|
|
93
|
+
const directDurationMs = Math.max(1, Date.now() - directStart)
|
|
94
|
+
const directText = typeof directRes === 'string' ? directRes : directRes?.content || directRes?.text || ''
|
|
95
|
+
const directTokens = estimateTokens(directText)
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
output: directText,
|
|
99
|
+
draftUsed: false,
|
|
100
|
+
draftText: null,
|
|
101
|
+
draftDurationMs: 0,
|
|
102
|
+
verifierDurationMs: directDurationMs,
|
|
103
|
+
totalDurationMs: directDurationMs,
|
|
104
|
+
effectiveTokensPerSec: Math.round((directTokens / (directDurationMs / 1000))),
|
|
105
|
+
speedupFactor: 1.0,
|
|
106
|
+
summary: 'Speculative fallback: direct generation without draft.',
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Phase 2: Cloud Verifier Validation & Completion
|
|
111
|
+
const verifierPrompt = [
|
|
112
|
+
'You are an expert Cloud Verifier performing speculative code completion.',
|
|
113
|
+
'Below is the user prompt and a fast speculative draft block from a local edge model.',
|
|
114
|
+
'Review the draft for correctness, patch any edge case errors or bugs, and output the complete finalized solution.',
|
|
115
|
+
'\n### Original User Task:\n' + prompt,
|
|
116
|
+
`\n### Speculative Edge Draft (${draftLabel}):\n` + draftText,
|
|
117
|
+
'\nProvide your verified, final solution code below:',
|
|
118
|
+
].join('\n')
|
|
119
|
+
|
|
120
|
+
if (typeof onProgress === 'function') {
|
|
121
|
+
onProgress(`🔍 *Speculative Verification*: Verifying and refining draft via ` + targetLabel + '...\n')
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const verifierStart = Date.now()
|
|
125
|
+
const verifierRes = await callLlm({
|
|
126
|
+
provider: targetSlot?.provider,
|
|
127
|
+
model: targetSlot?.model,
|
|
128
|
+
messages: [{ role: 'user', content: verifierPrompt }],
|
|
129
|
+
temperature: 0.2,
|
|
130
|
+
signal,
|
|
131
|
+
})
|
|
132
|
+
const verifierDurationMs = Math.max(1, Date.now() - verifierStart)
|
|
133
|
+
const finalOutput = typeof verifierRes === 'string' ? verifierRes : verifierRes?.content || verifierRes?.text || ''
|
|
134
|
+
const totalDurationMs = draftDurationMs + verifierDurationMs
|
|
135
|
+
|
|
136
|
+
const outputTokens = estimateTokens(finalOutput)
|
|
137
|
+
const effectiveTokensPerSec = totalDurationMs > 0 ? Math.round((outputTokens / (totalDurationMs / 1000))) : 0
|
|
138
|
+
|
|
139
|
+
// Estimated baseline: Cloud verifier model generating all tokens from scratch typically takes ~2x longer per token
|
|
140
|
+
const estimatedBaselineMs = Math.round(outputTokens * 35) // ~28-35ms per token for large cloud models
|
|
141
|
+
const speedupFactor = totalDurationMs > 0 ? Number(Math.max(1.0, (estimatedBaselineMs / totalDurationMs)).toFixed(2)) : 1.0
|
|
142
|
+
|
|
143
|
+
if (ctx) {
|
|
144
|
+
emitEvent(ctx, MOA_EVENTS.SPECULATIVE_DRAFT, {
|
|
145
|
+
runId,
|
|
146
|
+
draftModel: draftLabel,
|
|
147
|
+
verifierModel: targetLabel,
|
|
148
|
+
draftDurationMs,
|
|
149
|
+
verifierDurationMs,
|
|
150
|
+
totalDurationMs,
|
|
151
|
+
effectiveTokensPerSec,
|
|
152
|
+
speedupFactor,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (typeof onProgress === 'function') {
|
|
157
|
+
onProgress(`🚀 *Speculative Acceleration*: ${effectiveTokensPerSec} effective tok/sec (~x${speedupFactor} speedup)\n`)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
output: finalOutput,
|
|
162
|
+
draftUsed: true,
|
|
163
|
+
draftText,
|
|
164
|
+
draftDurationMs,
|
|
165
|
+
verifierDurationMs,
|
|
166
|
+
totalDurationMs,
|
|
167
|
+
effectiveTokensPerSec,
|
|
168
|
+
speedupFactor,
|
|
169
|
+
summary: `⚡ Speculative Decoding: draft ${draftDurationMs}ms + verifier ${verifierDurationMs}ms (${effectiveTokensPerSec} tok/sec, ~x${speedupFactor} speedup)`,
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Creates a wrapping callLlm function that performs speculative decoding for target models.
|
|
175
|
+
*
|
|
176
|
+
* @param {Function} baseCallLlm
|
|
177
|
+
* @param {object} options
|
|
178
|
+
* @returns {Function}
|
|
179
|
+
*/
|
|
180
|
+
export function createSpeculativeCallLlm(baseCallLlm, options = {}) {
|
|
181
|
+
const { draftSlot, blockSize = 256, ctx, runId, onProgress } = options
|
|
182
|
+
if (!draftSlot || !draftSlot.model) return baseCallLlm
|
|
183
|
+
|
|
184
|
+
return async function speculativeCallLlm(opts) {
|
|
185
|
+
if (opts.provider === draftSlot.provider && opts.model === draftSlot.model) {
|
|
186
|
+
return baseCallLlm(opts)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const prompt = Array.isArray(opts.messages)
|
|
190
|
+
? opts.messages.map((m) => (typeof m === 'string' ? m : m.content || '')).join('\n')
|
|
191
|
+
: String(opts.prompt || '')
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
const res = await runSpeculativeVerification({
|
|
195
|
+
prompt,
|
|
196
|
+
draftSlot,
|
|
197
|
+
targetSlot: { provider: opts.provider, model: opts.model },
|
|
198
|
+
callLlm: baseCallLlm,
|
|
199
|
+
options: {
|
|
200
|
+
ctx,
|
|
201
|
+
runId,
|
|
202
|
+
onProgress,
|
|
203
|
+
blockSize,
|
|
204
|
+
signal: opts.signal,
|
|
205
|
+
},
|
|
206
|
+
})
|
|
207
|
+
return res.output
|
|
208
|
+
} catch (_err) {
|
|
209
|
+
return baseCallLlm(opts)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|