@goodandready/dsh-moa 0.2.57 → 0.2.58
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/lib/moa-llm.js +38 -2
- package/package.json +1 -1
package/lib/moa-llm.js
CHANGED
|
@@ -21,6 +21,41 @@ function combineSignals(signal, timeoutSignal) {
|
|
|
21
21
|
return combinedCtrl.signal
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Normalizes message content into an array of content blocks ({ type: 'text', text: ... })
|
|
26
|
+
* required by DSH 0.2.x LLM adapters (e.g. adapters executing message.content.some(...)).
|
|
27
|
+
*/
|
|
28
|
+
export function toContentBlocks(content) {
|
|
29
|
+
if (Array.isArray(content)) return content
|
|
30
|
+
if (typeof content === 'string') {
|
|
31
|
+
return content.length === 0 ? [] : [{ type: 'text', text: content }]
|
|
32
|
+
}
|
|
33
|
+
if (content && typeof content === 'object') {
|
|
34
|
+
const text = typeof content.text === 'string'
|
|
35
|
+
? content.text
|
|
36
|
+
: (typeof content.content === 'string' ? content.content : undefined)
|
|
37
|
+
if (text !== undefined) {
|
|
38
|
+
return text.length === 0 ? [] : [{ type: 'text', text }]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (content === null || content === undefined) return []
|
|
42
|
+
return [{ type: 'text', text: String(content) }]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Normalizes an array of messages to guarantee that each message has a role
|
|
47
|
+
* and its content is an array of content blocks.
|
|
48
|
+
*/
|
|
49
|
+
export function toLlmMessages(messages) {
|
|
50
|
+
return (Array.isArray(messages) ? messages : [])
|
|
51
|
+
.filter((m) => m && typeof m === 'object' && typeof m.role === 'string')
|
|
52
|
+
.map((m) => ({
|
|
53
|
+
...m,
|
|
54
|
+
role: m.role,
|
|
55
|
+
content: toContentBlocks(m.content),
|
|
56
|
+
}))
|
|
57
|
+
}
|
|
58
|
+
|
|
24
59
|
/**
|
|
25
60
|
* Unified LLM Dispatcher using ctx.llm.prepareCall / stream
|
|
26
61
|
*/
|
|
@@ -70,10 +105,11 @@ export function createLlmCaller(ctx) {
|
|
|
70
105
|
const combinedSignal = combineSignals(signal, abortCtrl.signal)
|
|
71
106
|
|
|
72
107
|
try {
|
|
108
|
+
const normalizedMessages = toLlmMessages(messages)
|
|
73
109
|
const streamConfig = prep.config || callConfig
|
|
74
110
|
const stream = prep.stream({
|
|
75
111
|
...streamConfig,
|
|
76
|
-
messages,
|
|
112
|
+
messages: normalizedMessages,
|
|
77
113
|
temperature: streamConfig.temperature ?? temperature,
|
|
78
114
|
maxTokens: streamConfig.maxTokens ?? maxTokens,
|
|
79
115
|
signal: combinedSignal,
|
|
@@ -129,7 +165,7 @@ export function createLlmCaller(ctx) {
|
|
|
129
165
|
content: fullText,
|
|
130
166
|
text: fullText,
|
|
131
167
|
usage: usageInfo || {
|
|
132
|
-
inputTokens: Math.round(JSON.stringify(
|
|
168
|
+
inputTokens: Math.round(JSON.stringify(normalizedMessages).length / 4),
|
|
133
169
|
outputTokens: Math.round(fullText.length / 4),
|
|
134
170
|
},
|
|
135
171
|
}
|