dsh-nova-lewis 0.2.1 → 0.2.3
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.cn.md +5 -5
- package/README.md +8 -8
- package/dist/client.js +90 -27
- package/host/children.mjs +8 -3
- package/host/client.mjs +23 -5
- package/host/composer.css +1 -0
- package/host/exact-composer-text.mjs +31 -0
- package/host/index.mjs +107 -54
- package/host/models.mjs +15 -10
- package/host/plan-view.mjs +0 -17
- package/host/roll-call.mjs +118 -0
- package/package.json +5 -5
- package/src/context.mjs +15 -3
- package/src/easter-egg.mjs +86 -0
- package/src/engine-plan.mjs +37 -3
- package/src/engine.mjs +11 -7
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// 彩蛋.xlsx, Sheet1!B2:I8. Column C labels the languages of the replies in D.
|
|
2
|
+
export const eggPhrases = Object.freeze({
|
|
3
|
+
古怪: Object.freeze(['にゃ〜 ฅ^•ﻌ•^ฅ', 'Ciallo~(∠・ω< )⌒★']),
|
|
4
|
+
各国语言: Object.freeze(['Coucou!', 'Holaaa~', 'Hallöchen!', 'Привет!', '안뇽!', 'はーい!( ´ ▽ ` )ノ', 'やっほー!(≧▽≦)']),
|
|
5
|
+
正常人类: Object.freeze(['怎么啦?', '嗨', '一直在啊']),
|
|
6
|
+
不正常: Object.freeze(['你猜?', '可以不在吗?', '阿巴阿巴', '又有活了']),
|
|
7
|
+
程序员: Object.freeze(['Let me cook', 'Noodling…']),
|
|
8
|
+
颜文字: Object.freeze(['👀', '( ̄▽ ̄)', '(・∀・)', '(`・ω・´)', '(=^・ω・^=)']),
|
|
9
|
+
nova系列: Object.freeze(['Still shining ✩', 'Ad astra ✧', '(∩^o^)⊃━☆゚.*', '星星收到啦 ✦']),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const closings = [
|
|
13
|
+
"**NOVA:See? We're all here. ✦**",
|
|
14
|
+
'**NOVA:Here we are, together. ♡**',
|
|
15
|
+
'**NOVA:Present and shining! ✧**',
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
// Draw every item once before refilling. At a cycle boundary avoid repeating the last draw.
|
|
19
|
+
function drawFrom(items, random) {
|
|
20
|
+
let pool = [], previous
|
|
21
|
+
return () => {
|
|
22
|
+
let available = pool.length
|
|
23
|
+
if (!available) {
|
|
24
|
+
pool = items.filter(item=>item!==previous)
|
|
25
|
+
available = pool.length
|
|
26
|
+
if (previous!==undefined) pool.push(previous)
|
|
27
|
+
}
|
|
28
|
+
const index = Math.floor(random() * (available || pool.length))
|
|
29
|
+
previous = pool.splice(index,1)[0]
|
|
30
|
+
return previous
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const modelName = route => route.modelId || route.label || route.id
|
|
35
|
+
const oneLine = text => String(text).replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]+/gu,' ')
|
|
36
|
+
.replace(/[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/gu,'').trim()
|
|
37
|
+
const renderPhrase = (phrase, route) => phrase.replaceAll('【模型】',()=>oneLine(modelName(route)))
|
|
38
|
+
|
|
39
|
+
export function allocateEggReplies(routes, random = Math.random) {
|
|
40
|
+
const categories = Object.keys(eggPhrases)
|
|
41
|
+
const nextCategory = drawFrom(categories, random)
|
|
42
|
+
const members = new Map(categories.map(category=>[category,[]]))
|
|
43
|
+
const replies = routes.map(route=>{
|
|
44
|
+
const category = nextCategory()
|
|
45
|
+
const reply = {routeId:route.id, category, text:'', choices:[]}
|
|
46
|
+
members.get(category).push({route, reply})
|
|
47
|
+
return reply
|
|
48
|
+
})
|
|
49
|
+
for (const [category, team] of members) {
|
|
50
|
+
if (!team.length) continue
|
|
51
|
+
const phrases = eggPhrases[category]
|
|
52
|
+
const nextPhrase = drawFrom(phrases,random), unused = new Set(phrases)
|
|
53
|
+
for (const {route, reply} of team) {
|
|
54
|
+
const phrase = nextPhrase()
|
|
55
|
+
unused.delete(phrase)
|
|
56
|
+
reply.text = renderPhrase(phrase,route)
|
|
57
|
+
reply.choices.push(reply.text)
|
|
58
|
+
}
|
|
59
|
+
// Disjoint menus let each live model choose freely without duplicating another model's choices.
|
|
60
|
+
const extraPhrase = drawFrom([...unused],random), recipient = drawFrom(team,random)
|
|
61
|
+
for (let i = 0; i < unused.size; i++) {
|
|
62
|
+
const {route, reply} = recipient()
|
|
63
|
+
reply.choices.push(renderPhrase(extraPhrase(),route))
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return replies
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function markdownText(value) {
|
|
70
|
+
return oneLine(value).replace(/[\\`*_\[\]<>~&|]/g,'\\$&')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function formatEggReply(route, text) {
|
|
74
|
+
const provider = route.providerId || route.apiId
|
|
75
|
+
const name = provider ? provider+' / '+modelName(route) : modelName(route)
|
|
76
|
+
return '**'+markdownText(name)+'**:'+markdownText(text)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function chooseNovaClosing(random = Math.random) {
|
|
80
|
+
return closings[Math.floor(random() * closings.length)]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function formatEggMessage(lines, closing = chooseNovaClosing()) {
|
|
84
|
+
if (!closings.includes(closing)) throw Error('未知的 NOVA 彩蛋结语。')
|
|
85
|
+
return lines.length ? lines.join(' \n')+'\n\n'+closing : closing
|
|
86
|
+
}
|
package/src/engine-plan.mjs
CHANGED
|
@@ -17,9 +17,9 @@ export function routeCatalog(routes) {
|
|
|
17
17
|
return copy(routes)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export function normalizeNodes(plan) {
|
|
21
|
-
if (!Array.isArray(plan?.nodes) || !plan.nodes.length || plan.nodes.length >
|
|
22
|
-
throw Error('计划需包含 1 至
|
|
20
|
+
export function normalizeNodes(plan, maxNodes = 100) {
|
|
21
|
+
if (!Array.isArray(plan?.nodes) || !plan.nodes.length || plan.nodes.length > maxNodes) {
|
|
22
|
+
throw Error('计划需包含 1 至 '+maxNodes+' 个节点')
|
|
23
23
|
}
|
|
24
24
|
const ids = new Set()
|
|
25
25
|
const deliverables = new Set()
|
|
@@ -79,6 +79,40 @@ export function normalizeNodes(plan) {
|
|
|
79
79
|
return nodes
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
// A coordinator can describe the work without enumerating a large roster in its JSON.
|
|
83
|
+
// Give each remaining selected text route its own artifact and wire it into the existing final.
|
|
84
|
+
export function includeSelectedContributors(state, nodes) {
|
|
85
|
+
const final = nodes.find(node => node.final && !node.optional)
|
|
86
|
+
if (!final) return
|
|
87
|
+
const assigned = new Set(nodes.map(node => node.routeId))
|
|
88
|
+
const allowed = new Set(state.allowedRouteIds)
|
|
89
|
+
const ids = new Set(nodes.map(node => node.id))
|
|
90
|
+
const deliverables = new Set(nodes.map(node => node.deliverable.trim().normalize('NFKC').toLowerCase()))
|
|
91
|
+
const finalSources = state.sources.filter(source => final.sourceIds.includes(source.id))
|
|
92
|
+
const extra = []
|
|
93
|
+
let ordinal = 0
|
|
94
|
+
for (const route of state.routes) {
|
|
95
|
+
if (!allowed.has(route.id) || assigned.has(route.id) || route.enabled === false || !(route.capabilities ?? ['text']).includes('text')) continue
|
|
96
|
+
let id, deliverable
|
|
97
|
+
do {
|
|
98
|
+
ordinal++
|
|
99
|
+
id = 'contributor-' + ordinal
|
|
100
|
+
deliverable = '独立建议 ' + ordinal
|
|
101
|
+
} while (ids.has(id) || deliverables.has(deliverable))
|
|
102
|
+
extra.push({id, title:'独立建议 · '+(route.label || route.modelId || route.id), role:'contributor', routeId:route.id,
|
|
103
|
+
deliverable, acceptance:'就当前任务给出具体、可核查的建议。', optional:true,
|
|
104
|
+
instructions:'独立提出对当前任务有帮助的结论、依据或需要核实的问题,交给最终负责人参考。',
|
|
105
|
+
sourceIds:finalSources.filter(source => mayRead(source, route)).map(source => source.id),
|
|
106
|
+
assignment:{routeId:route.id, role:'contributor', source:'selection', reasons:['此型号已勾选参与,本次独立提供建议。'], evidence:[]}})
|
|
107
|
+
}
|
|
108
|
+
if (!extra.length) return
|
|
109
|
+
for (const contributor of normalizeNodes({nodes:extra}, extra.length)) {
|
|
110
|
+
nodes.push(contributor)
|
|
111
|
+
final.deps.push(contributor.id)
|
|
112
|
+
}
|
|
113
|
+
final.instructions = [final.instructions, '结合直接上游各型号的独立建议,核实后给出最终答复。'].filter(Boolean).join('\n')
|
|
114
|
+
}
|
|
115
|
+
|
|
82
116
|
export function positiveNumber(value) {
|
|
83
117
|
return Number.isSafeInteger(value) && value > 0 ? value : 0
|
|
84
118
|
}
|
package/src/engine.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assignmentsHash, copy, descendants, incompatibility, inheritedSources, mayRead,
|
|
1
|
+
import { assignmentsHash, copy, descendants, incompatibility, inheritedSources, mayRead, includeSelectedContributors,
|
|
2
2
|
normalizeNodes, positiveNumber, routeCatalog, strings, uid, writeConflict } from './engine-plan.mjs'
|
|
3
3
|
import { estimateInput } from './context.mjs'
|
|
4
4
|
import { assessReview, reviewDecisions } from './engine-review.mjs'
|
|
@@ -87,7 +87,9 @@ function beginAttempt(state, node, route, kind, reserved) {
|
|
|
87
87
|
|
|
88
88
|
function publish(state, plan, fromPlanner = false) {
|
|
89
89
|
if (plan?.validationError) throw Error(String(plan.validationError))
|
|
90
|
-
const
|
|
90
|
+
const limit = state.settings.easterEgg ? state.allowedRouteIds.length :
|
|
91
|
+
state.settings.includeAllParticipants ? 100 + state.allowedRouteIds.length : undefined
|
|
92
|
+
const nodes = normalizeNodes(plan, limit)
|
|
91
93
|
if (fromPlanner && nodes.length > 1) {
|
|
92
94
|
const byId = new Map(nodes.map(n => [n.id, n])), prerequisites = new Set()
|
|
93
95
|
const include = id => {
|
|
@@ -110,6 +112,7 @@ function publish(state, plan, fromPlanner = false) {
|
|
|
110
112
|
if (!state.sources.some(source => source.id === id)) throw Error(node.id + ' 引用了不存在的资料:' + id)
|
|
111
113
|
}
|
|
112
114
|
}
|
|
115
|
+
if (fromPlanner && state.settings.includeAllParticipants) includeSelectedContributors(state, nodes)
|
|
113
116
|
state.nodes = nodes
|
|
114
117
|
state.planVersion += 1
|
|
115
118
|
state.assignmentsHash = assignmentsHash(state)
|
|
@@ -232,7 +235,8 @@ function largerPlanningOutput(state, attempt, error) {
|
|
|
232
235
|
if (!route || limitReason(state,node).reason) return 0
|
|
233
236
|
const window = Math.min(...[route.contextWindow,route.window,route.deploymentWindow].filter(positiveNumber))
|
|
234
237
|
const available = state.maxTokens == null ? Infinity : remainingTokens(state,node) - error.inputEstimate
|
|
235
|
-
const
|
|
238
|
+
const modelLimit = Math.min(route.maxOutput || Infinity, route.outputLimit || Infinity)
|
|
239
|
+
const limit = Math.min(Number.isFinite(modelLimit) ? modelLimit : error.maxTokens * 2,
|
|
236
240
|
window - (attempt.context?.margin || 0) - error.inputEstimate, available)
|
|
237
241
|
return limit > error.maxTokens ? Math.floor(limit) : 0
|
|
238
242
|
}
|
|
@@ -267,8 +271,9 @@ export class RunEngine {
|
|
|
267
271
|
this.#beforeAutoStart = options.beforeAutoStart
|
|
268
272
|
const settings = copy(options.settings || {})
|
|
269
273
|
const allowedRouteIds = strings(options.allowedRouteIds ?? routes.filter(route => route.enabled !== false).map(route => route.id), '允许的路由')
|
|
270
|
-
const plannerAssignment =
|
|
271
|
-
|
|
274
|
+
const plannerAssignment = settings.easterEgg || !allowedRouteIds.length ? null : options.plannerAssignment ||
|
|
275
|
+
choosePlanner(routes.filter(r=>allowedRouteIds.includes(r.id)), options.task || '',
|
|
276
|
+
{lockedId:options.plannerRouteId,preferredId:options.preferredPlannerId})
|
|
272
277
|
const configured = key => options[key] ?? settings[key]
|
|
273
278
|
const maxTokens = safeLimit(configured('maxTokens'), 'Token 预算')
|
|
274
279
|
const defaultFinalTokens = maxTokens == null ? 0 : Math.min(2048, Math.floor(maxTokens / 8))
|
|
@@ -583,8 +588,7 @@ export class RunEngine {
|
|
|
583
588
|
if (attempt?.status !== 'running') throw Object.assign(Error('此请求已结束'), {dispatched:false})
|
|
584
589
|
const node = attempt.nodeId === 'plan' ? {id:'plan',outputReserve:attempt.planningOutputTokens} : byNode(state,attempt.nodeId)
|
|
585
590
|
// The host now has the actual assembled request, including the selected catalog and conversation history.
|
|
586
|
-
const
|
|
587
|
-
const ceiling = Math.min(input + output, desired, node.tokenBudget || Infinity, remainingTokens(state,node,id))
|
|
591
|
+
const ceiling = Math.min(input + output, node.tokenBudget || Infinity, remainingTokens(state,node,id))
|
|
588
592
|
if (ceiling <= input) throw Object.assign(Error('输入约 '+input+' Token,当前请求可用 '+Math.max(0,ceiling)+' Token,无法预留输出。'), {dispatched:false})
|
|
589
593
|
attempt.reservedTokens = ceiling
|
|
590
594
|
return {attemptId:id, reservedTokens:ceiling}
|