57 lines
3.3 KiB
JavaScript
57 lines
3.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.summarizeConversation = summarizeConversation;
|
|
const jsx_runtime_1 = require("./jsx/jsx-runtime");
|
|
/**
|
|
* Copyright (c) Microsoft Corporation.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
const jsx_runtime_2 = require("./jsx/jsx-runtime");
|
|
function summarizeConversation(task, conversation, options) {
|
|
const summary = ['## Task', task];
|
|
const combinedState = {};
|
|
const assistantMessages = conversation.messages.filter(message => message.role === 'assistant');
|
|
for (let turn = 0; turn < assistantMessages.length - 1; ++turn) {
|
|
if (turn === 0) {
|
|
summary.push('');
|
|
summary.push('## History');
|
|
}
|
|
summary.push(``);
|
|
const text = assistantMessages[turn].content.filter(part => part.type === 'text').map(part => part.text).join('\n');
|
|
const toolCalls = assistantMessages[turn].content.filter(part => part.type === 'tool_call');
|
|
for (const toolCall of toolCalls) {
|
|
if (toolCall.result) {
|
|
for (const [name, state] of Object.entries(toolCall.result._meta?.['dev.lowire/state'] || {}))
|
|
combinedState[name] = state;
|
|
}
|
|
}
|
|
const message = assistantMessages[turn];
|
|
summary.push((0, jsx_runtime_1.jsxs)("step", { turn: turn + 1, children: [(0, jsx_runtime_1.jsx)("title", { children: text }), toolCalls.map(toolCall => (0, jsx_runtime_1.jsxs)("tool-call", { children: [(0, jsx_runtime_1.jsx)("name", { children: toolCall.name }), Object.keys(toolCall.arguments).length > 0 && (0, jsx_runtime_1.jsx)("arguments", { children: Object.entries(toolCall.arguments).map(([key, value]) => (0, jsx_runtime_2.jsx)(key, { children: [JSON.stringify(value)] })) })] })), toolCalls.map(toolCall => toolCall.result?._meta?.['dev.lowire/history'] || []).flat().map(h => (0, jsx_runtime_2.jsx)(h.category, { children: [h.content] })), message.toolError && (0, jsx_runtime_1.jsx)("error", { children: message.toolError })] }));
|
|
}
|
|
const lastMessage = assistantMessages[assistantMessages.length - 1];
|
|
if (lastMessage) { // Remove state from combined state as it'll be a part of the last assistant message.
|
|
for (const part of lastMessage.content.filter(part => part.type === 'tool_call')) {
|
|
for (const name of Object.keys(part.result?._meta?.['dev.lowire/state'] || {}))
|
|
delete combinedState[name];
|
|
}
|
|
}
|
|
for (const [name, state] of Object.entries(combinedState)) {
|
|
summary.push('');
|
|
summary.push((0, jsx_runtime_1.jsx)("state", { name: name, children: state }));
|
|
}
|
|
options.debug?.('lowire:summary')(summary.join('\n'));
|
|
options.debug?.('lowire:summary')(JSON.stringify(lastMessage, null, 2));
|
|
return { summary: summary.join('\n'), lastMessage };
|
|
}
|