@try-works/dsh-minimal-router 0.0.0-stage → 0.2.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/LICENSE +1 -0
- package/README.md +69 -2
- package/client/index.js +70039 -0
- package/cordis.patch.yml +147 -0
- package/lib/backend.js +14 -0
- package/lib/build-id.js +4 -0
- package/lib/capability-sync.js +27 -0
- package/lib/engine-adapter.js +657 -0
- package/lib/evidence.js +1107 -0
- package/lib/host.js +554 -0
- package/lib/index.js +2299 -0
- package/lib/route-application.js +780 -0
- package/lib/routing.js +858 -0
- package/lib/settings-route.js +52 -0
- package/lib/settings-store.js +770 -0
- package/lib/summariser.js +157 -0
- package/lib/telemetry.js +1019 -0
- package/lib/tool-result-pruner.js +224 -0
- package/package.json +38 -4
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// @dsh-local-router/plugin — inline result summariser (Run 35, DEC-35-03 a).
|
|
2
|
+
//
|
|
3
|
+
// DSH has no per-result summarisation hook. This is the plugin-owned summariser
|
|
4
|
+
// that the tool-result pruner calls in summarise mode; it is opt-in through the
|
|
5
|
+
// saved Context document's lazySummary block and reads it live on every call, so
|
|
6
|
+
// a save applies without a restart.
|
|
7
|
+
//
|
|
8
|
+
// Honoured per result:
|
|
9
|
+
// enabled the only switch that permits a model call
|
|
10
|
+
// model agent (the child's own current model) | local | remote
|
|
11
|
+
// reserve token floor: a smaller body is skipped, and the value caps the
|
|
12
|
+
// summary maxTokens (the budget the summary may spend)
|
|
13
|
+
// perResult how many results in one turn may be summarised
|
|
14
|
+
//
|
|
15
|
+
// Fail-open by construction: every failure returns null, so the pruner keeps the
|
|
16
|
+
// original body and never blocks the tool result. lastResult() reports what
|
|
17
|
+
// happened (including the route that produced a summary) for the audit row.
|
|
18
|
+
|
|
19
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value)
|
|
20
|
+
const isNonEmptyString = (value) => typeof value === "string" && value.trim().length > 0
|
|
21
|
+
|
|
22
|
+
/** Rough token estimate (4 characters per token) used by the reserve floor. */
|
|
23
|
+
export const estimateTokens = (text) => Math.ceil(String(text ?? "").length / 4)
|
|
24
|
+
|
|
25
|
+
const SUMMARY_INSTRUCTION = [
|
|
26
|
+
"Condense the following tool result into a terse summary.",
|
|
27
|
+
"Preserve verbatim every identifier, file path, command, error string, numeric value, and function signature.",
|
|
28
|
+
"Keep every fact a later model would need to answer follow-up questions.",
|
|
29
|
+
"Do not invent facts. Output only the summary text."
|
|
30
|
+
].join(" ")
|
|
31
|
+
|
|
32
|
+
const routeOf = (selection) =>
|
|
33
|
+
isRecord(selection) && isNonEmptyString(selection.provider) && isNonEmptyString(selection.model)
|
|
34
|
+
? { provider: selection.provider, model: selection.model }
|
|
35
|
+
: null
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create the inline summariser. Every dependency is injected so the block's
|
|
39
|
+
* semantics are testable without a live LLM or model catalogue.
|
|
40
|
+
* @param options.llm ctx.llm (stream)
|
|
41
|
+
* @param options.readContext () => saved context fragment (live read)
|
|
42
|
+
* @param options.readMainModel () => { provider, model } | null (remote route)
|
|
43
|
+
* @param options.readLocalModel () => { provider, model } | null (local route)
|
|
44
|
+
* @param options.readTurn (agent) => turn number | null (perResult scope)
|
|
45
|
+
*/
|
|
46
|
+
export const createInlineSummariser = (options = {}) => {
|
|
47
|
+
const opts = isRecord(options) ? options : {}
|
|
48
|
+
const llm = opts.llm
|
|
49
|
+
const readContext = typeof opts.readContext === "function" ? opts.readContext : () => null
|
|
50
|
+
const readMainModel = typeof opts.readMainModel === "function" ? opts.readMainModel : () => null
|
|
51
|
+
const readLocalModel = typeof opts.readLocalModel === "function" ? opts.readLocalModel : () => null
|
|
52
|
+
const readTurn = typeof opts.readTurn === "function" ? opts.readTurn : () => null
|
|
53
|
+
// One allowance per (session, turn): the page's perResult is "how many results
|
|
54
|
+
// may be summarised", and its validation names "per turn".
|
|
55
|
+
const counts = new Map()
|
|
56
|
+
let last = null
|
|
57
|
+
|
|
58
|
+
const blockNow = () => {
|
|
59
|
+
const context = readContext()
|
|
60
|
+
const block = isRecord(context) && isRecord(context.lazySummary) ? context.lazySummary : null
|
|
61
|
+
return block !== null && block.enabled === true ? block : null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const available = () => blockNow() !== null && llm !== null && typeof llm?.stream === "function"
|
|
65
|
+
|
|
66
|
+
const routeFor = (block, agent) => {
|
|
67
|
+
const wanted = isNonEmptyString(block.model) ? block.model : "agent"
|
|
68
|
+
if (wanted === "agent") {
|
|
69
|
+
let selection = null
|
|
70
|
+
try {
|
|
71
|
+
selection = typeof agent?.model?.current === "function" ? agent.model.current() : null
|
|
72
|
+
} catch {
|
|
73
|
+
selection = null
|
|
74
|
+
}
|
|
75
|
+
return routeOf(selection) ?? routeOf(readMainModel())
|
|
76
|
+
}
|
|
77
|
+
if (wanted === "local") return routeOf(readLocalModel())
|
|
78
|
+
if (wanted === "remote") return routeOf(readMainModel())
|
|
79
|
+
return null
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const turnKey = (agent) => {
|
|
83
|
+
const sessionId = isNonEmptyString(agent?.session?.id) ? agent.session.id : "unknown-session"
|
|
84
|
+
let turn = null
|
|
85
|
+
try {
|
|
86
|
+
turn = typeof readTurn === "function" ? readTurn(agent) : null
|
|
87
|
+
} catch {
|
|
88
|
+
turn = null
|
|
89
|
+
}
|
|
90
|
+
return sessionId + "|" + (Number.isInteger(turn) ? String(turn) : "all")
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const skip = (reason) => {
|
|
94
|
+
last = { status: "skipped", reason }
|
|
95
|
+
return null
|
|
96
|
+
}
|
|
97
|
+
const fail = (reason) => {
|
|
98
|
+
last = { status: "failed", reason }
|
|
99
|
+
return null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Summarise one result body. Never throws.
|
|
104
|
+
* @returns the summary text, or null when the block is off, the body is under
|
|
105
|
+
* the reserve floor, the per-turn allowance is spent, or the model
|
|
106
|
+
* call failed (the caller keeps the original body).
|
|
107
|
+
*/
|
|
108
|
+
const summarise = async (body, meta = {}) => {
|
|
109
|
+
last = { status: "skipped", reason: "disabled" }
|
|
110
|
+
const block = blockNow()
|
|
111
|
+
if (block === null) return null
|
|
112
|
+
if (llm === null || typeof llm?.stream !== "function") return fail("no-llm")
|
|
113
|
+
if (!isNonEmptyString(body)) return skip("empty-body")
|
|
114
|
+
const reserve = Number.isInteger(block.reserve) && block.reserve > 0 ? block.reserve : 0
|
|
115
|
+
if (reserve > 0 && estimateTokens(body) < reserve) return skip("below-reserve")
|
|
116
|
+
const perResult = Number.isInteger(block.perResult) && block.perResult >= 1 ? block.perResult : 1
|
|
117
|
+
const key = turnKey(meta?.agent)
|
|
118
|
+
const used = counts.get(key) ?? 0
|
|
119
|
+
if (used >= perResult) return skip("per-result-limit")
|
|
120
|
+
const route = routeFor(block, meta?.agent)
|
|
121
|
+
if (route === null) return fail("no-route")
|
|
122
|
+
// Counting the attempt (not only the success) bounds repeated failed calls.
|
|
123
|
+
counts.set(key, used + 1)
|
|
124
|
+
if (counts.size > 512) counts.clear()
|
|
125
|
+
try {
|
|
126
|
+
const messages = [
|
|
127
|
+
{ role: "user", content: [{ type: "text", text: SUMMARY_INSTRUCTION }] },
|
|
128
|
+
{ role: "user", content: [{ type: "text", text: body }] }
|
|
129
|
+
]
|
|
130
|
+
let text = ""
|
|
131
|
+
const request = {
|
|
132
|
+
provider: route.provider,
|
|
133
|
+
model: route.model,
|
|
134
|
+
messages
|
|
135
|
+
}
|
|
136
|
+
if (reserve > 0) request.maxTokens = reserve
|
|
137
|
+
for await (const chunk of llm.stream(request)) {
|
|
138
|
+
if (chunk !== null && typeof chunk === "object" && chunk.type === "text-delta") text += chunk.text
|
|
139
|
+
}
|
|
140
|
+
const summary = text.trim()
|
|
141
|
+
if (summary.length === 0) return fail("empty-summary")
|
|
142
|
+
last = { status: "summarised", reason: null, provider: route.provider, model: route.model, blockModel: block.model }
|
|
143
|
+
return summary
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return fail("model-error: " + (error instanceof Error ? error.message : String(error)))
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
available,
|
|
151
|
+
summarise,
|
|
152
|
+
lastResult: () => last,
|
|
153
|
+
resetLast: () => {
|
|
154
|
+
last = null
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|