veryfront 0.1.634 → 0.1.635
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/esm/deno.js +1 -1
- package/esm/src/agent/runtime/text-generation-runtime-message-converter.d.ts.map +1 -1
- package/esm/src/agent/runtime/text-generation-runtime-message-converter.js +154 -13
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +1 -1
package/esm/deno.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-generation-runtime-message-converter.d.ts","sourceRoot":"","sources":["../../../../src/src/agent/runtime/text-generation-runtime-message-converter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAGV,4BAA4B,
|
|
1
|
+
{"version":3,"file":"text-generation-runtime-message-converter.d.ts","sourceRoot":"","sources":["../../../../src/src/agent/runtime/text-generation-runtime-message-converter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAGV,4BAA4B,EAK7B,MAAM,4CAA4C,CAAC;AAEpD,OAAO,EAGL,KAAK,OAAO,EAGb,MAAM,aAAa,CAAC;AAiKrB;;GAEG;AACH,wBAAgB,qCAAqC,CAAC,GAAG,EAAE,OAAO,GAAG,4BAA4B,CAyFhG;AA2GD;;GAEG;AACH,wBAAgB,sCAAsC,CACpD,QAAQ,EAAE,OAAO,EAAE,GAClB,4BAA4B,EAAE,CAyBhC"}
|
|
@@ -14,6 +14,73 @@ function getStringPartField(part, key) {
|
|
|
14
14
|
const value = part[key];
|
|
15
15
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
16
16
|
}
|
|
17
|
+
function isRecord(value) {
|
|
18
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
function getRecordPartField(part, key) {
|
|
21
|
+
if (!isRecord(part))
|
|
22
|
+
return undefined;
|
|
23
|
+
const value = part[key];
|
|
24
|
+
return isRecord(value) ? value : undefined;
|
|
25
|
+
}
|
|
26
|
+
function hasOwnField(part, key) {
|
|
27
|
+
return Object.hasOwn(part, key);
|
|
28
|
+
}
|
|
29
|
+
function getToolInputRecord(part) {
|
|
30
|
+
return getRecordPartField(part, "args") ?? getRecordPartField(part, "input") ?? {};
|
|
31
|
+
}
|
|
32
|
+
function getTextGenerationToolCallPart(part) {
|
|
33
|
+
if (!isRecord(part) || typeof part.type !== "string") {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (part.type !== "tool_call" &&
|
|
37
|
+
part.type !== "tool-call" &&
|
|
38
|
+
!(part.type.startsWith("tool-") && part.type !== "tool-result")) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const toolCallId = getStringPartField(part, "toolCallId") ??
|
|
42
|
+
getStringPartField(part, "tool_call_id") ??
|
|
43
|
+
getStringPartField(part, "id");
|
|
44
|
+
const toolName = getStringPartField(part, "toolName") ??
|
|
45
|
+
getStringPartField(part, "tool_name") ??
|
|
46
|
+
getStringPartField(part, "name") ??
|
|
47
|
+
(part.type.startsWith("tool-") && part.type !== "tool-call"
|
|
48
|
+
? part.type.replace(/^tool-/, "")
|
|
49
|
+
: undefined);
|
|
50
|
+
if (!toolCallId || !toolName) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
type: "tool-call",
|
|
55
|
+
toolCallId,
|
|
56
|
+
toolName,
|
|
57
|
+
input: getToolInputRecord(part),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function getTextGenerationToolResultPart(part, toolNamesById) {
|
|
61
|
+
if (!isRecord(part) || part.type !== "tool-result" && part.type !== "tool_result") {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const toolCallId = getStringPartField(part, "toolCallId") ??
|
|
65
|
+
getStringPartField(part, "tool_call_id");
|
|
66
|
+
if (!toolCallId) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const value = hasOwnField(part, "result")
|
|
70
|
+
? part.result
|
|
71
|
+
: hasOwnField(part, "output")
|
|
72
|
+
? part.output
|
|
73
|
+
: null;
|
|
74
|
+
return {
|
|
75
|
+
type: "tool-result",
|
|
76
|
+
toolCallId,
|
|
77
|
+
toolName: getStringPartField(part, "toolName") ??
|
|
78
|
+
getStringPartField(part, "tool_name") ??
|
|
79
|
+
toolNamesById.get(toolCallId) ??
|
|
80
|
+
"unknown",
|
|
81
|
+
output: { type: "json", value },
|
|
82
|
+
};
|
|
83
|
+
}
|
|
17
84
|
function buildAttachmentContextFromParts(parts) {
|
|
18
85
|
const refs = parts.flatMap((part) => {
|
|
19
86
|
const type = getStringPartField(part, "type");
|
|
@@ -108,15 +175,15 @@ export function convertToTextGenerationRuntimeMessage(msg) {
|
|
|
108
175
|
content.push({ type: "text", text: part.text });
|
|
109
176
|
continue;
|
|
110
177
|
}
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
(part.type.startsWith("tool-") && part.type !== "tool-result")) {
|
|
114
|
-
const toolPart = part;
|
|
178
|
+
const toolPart = getTextGenerationToolCallPart(part);
|
|
179
|
+
if (toolPart) {
|
|
115
180
|
content.push({
|
|
116
181
|
type: "tool-call",
|
|
117
182
|
toolCallId: toolPart.toolCallId,
|
|
118
183
|
toolName: toolPart.toolName,
|
|
119
|
-
input:
|
|
184
|
+
input: part.type === "tool_call"
|
|
185
|
+
? toolPart.input
|
|
186
|
+
: getToolArguments(part),
|
|
120
187
|
});
|
|
121
188
|
}
|
|
122
189
|
}
|
|
@@ -161,10 +228,80 @@ function hasProviderSendableAssistantContent(message) {
|
|
|
161
228
|
return typeof part.text === "string" &&
|
|
162
229
|
part.text.length > 0;
|
|
163
230
|
}
|
|
164
|
-
return part
|
|
165
|
-
(part.type.startsWith("tool-") && part.type !== "tool-result");
|
|
231
|
+
return getTextGenerationToolCallPart(part) !== null;
|
|
166
232
|
});
|
|
167
233
|
}
|
|
234
|
+
function convertAssistantMessageToTextGenerationRuntimeMessages(message) {
|
|
235
|
+
const assistantContent = [];
|
|
236
|
+
const deferredAssistantContent = [];
|
|
237
|
+
const toolResults = [];
|
|
238
|
+
const pendingToolCallIds = new Set();
|
|
239
|
+
const toolNamesById = new Map();
|
|
240
|
+
const messages = [];
|
|
241
|
+
const flushAssistantMessage = (content) => {
|
|
242
|
+
if (content.length === 0) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
messages.push({ role: "assistant", content: [...content] });
|
|
246
|
+
content.length = 0;
|
|
247
|
+
};
|
|
248
|
+
const flushToolMessage = () => {
|
|
249
|
+
if (toolResults.length === 0) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
messages.push({ role: "tool", content: [...toolResults] });
|
|
253
|
+
toolResults.length = 0;
|
|
254
|
+
};
|
|
255
|
+
const pushAssistantPart = (part) => {
|
|
256
|
+
if (part.type === "tool-call") {
|
|
257
|
+
if (deferredAssistantContent.length > 0) {
|
|
258
|
+
flushAssistantMessage(assistantContent);
|
|
259
|
+
flushToolMessage();
|
|
260
|
+
flushAssistantMessage(deferredAssistantContent);
|
|
261
|
+
}
|
|
262
|
+
assistantContent.push(part);
|
|
263
|
+
pendingToolCallIds.add(part.toolCallId);
|
|
264
|
+
toolNamesById.set(part.toolCallId, part.toolName);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (pendingToolCallIds.size > 0) {
|
|
268
|
+
deferredAssistantContent.push(part);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (toolResults.length > 0) {
|
|
272
|
+
flushAssistantMessage(assistantContent);
|
|
273
|
+
flushToolMessage();
|
|
274
|
+
flushAssistantMessage(deferredAssistantContent);
|
|
275
|
+
}
|
|
276
|
+
assistantContent.push(part);
|
|
277
|
+
};
|
|
278
|
+
const pushToolResult = (part) => {
|
|
279
|
+
if (!pendingToolCallIds.has(part.toolCallId)) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
toolResults.push(part);
|
|
283
|
+
pendingToolCallIds.delete(part.toolCallId);
|
|
284
|
+
};
|
|
285
|
+
for (const part of message.parts) {
|
|
286
|
+
if (part.type === "text" && "text" in part) {
|
|
287
|
+
pushAssistantPart({ type: "text", text: part.text });
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const toolCallPart = getTextGenerationToolCallPart(part);
|
|
291
|
+
if (toolCallPart) {
|
|
292
|
+
pushAssistantPart(toolCallPart);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const toolResultPart = getTextGenerationToolResultPart(part, toolNamesById);
|
|
296
|
+
if (toolResultPart) {
|
|
297
|
+
pushToolResult(toolResultPart);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
flushAssistantMessage(assistantContent);
|
|
301
|
+
flushToolMessage();
|
|
302
|
+
flushAssistantMessage(deferredAssistantContent);
|
|
303
|
+
return messages;
|
|
304
|
+
}
|
|
168
305
|
/**
|
|
169
306
|
* Convert an array of veryfront Messages to the current text-generation runtime message format.
|
|
170
307
|
*/
|
|
@@ -174,13 +311,17 @@ export function convertToTextGenerationRuntimeMessages(messages) {
|
|
|
174
311
|
if (!hasProviderSendableAssistantContent(message)) {
|
|
175
312
|
continue;
|
|
176
313
|
}
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
314
|
+
const convertedMessages = message.role === "assistant"
|
|
315
|
+
? convertAssistantMessageToTextGenerationRuntimeMessages(message)
|
|
316
|
+
: [convertToTextGenerationRuntimeMessage(message)];
|
|
317
|
+
for (const convertedMessage of convertedMessages) {
|
|
318
|
+
const previousMessage = textGenerationRuntimeMessages.at(-1);
|
|
319
|
+
if (previousMessage?.role === "tool" && convertedMessage.role === "tool") {
|
|
320
|
+
previousMessage.content.push(...convertedMessage.content);
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
textGenerationRuntimeMessages.push(convertedMessage);
|
|
182
324
|
}
|
|
183
|
-
textGenerationRuntimeMessages.push(convertedMessage);
|
|
184
325
|
}
|
|
185
326
|
return textGenerationRuntimeMessages;
|
|
186
327
|
}
|