diff --git a/CHANGELOG.md b/CHANGELOG.md index 27d68e8..3745d18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.13.8 + +- New Handlebars helpers: last, toJSON, formatMessage (more variants) + ## 0.13.7 - Fix react peerDependency version syntax #73 diff --git a/package.json b/package.json index 36a21c6..ff7be61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "langtail", - "version": "0.13.7", + "version": "0.13.8", "description": "", "main": "./Langtail.js", "packageManager": "pnpm@8.15.6", diff --git a/src/handlebars-helpers.ts b/src/handlebars-helpers.ts index b257784..91dd33d 100644 --- a/src/handlebars-helpers.ts +++ b/src/handlebars-helpers.ts @@ -1,5 +1,6 @@ import type handlebars from "@langtail/handlebars-evalless" import { format, parseISO } from "date-fns" +import { Message } from "../schemas" const isObject = function (val: any) { return typeof val === "object" @@ -68,3 +69,43 @@ export const operatorHelpers: handlebars.HelperDeclareSpec = { return Array.prototype.slice.call(arguments, 0, -1).some(Boolean) }, } + +const formatMessage = (message: Message, includeToolCalls: boolean): string => { + if (!message) return ''; + + let result = `[${message.role}] `; + + if (message.content) { + result += message.content; + } + + if (includeToolCalls && message.tool_calls?.length) { + result += '\nTool Calls:'; + for (const tool of message.tool_calls) { + result += `\n - ${tool.function.name}(${tool.function.arguments})`; + } + } + + return result; +}; + +const formatMessages = (messages: Message[], includeToolCalls: boolean): string => { + if (!Array.isArray(messages)) return ''; + return messages.map((m) => formatMessage(m, includeToolCalls)).join('\n\n'); +}; + +export const variableHelpers: handlebars.HelperDeclareSpec = { + toJSON: function (context) { + return JSON.stringify(context, null, 2); + }, + last: function (array) { + if (Array.isArray(array) && array.length > 0) { + return array[array.length - 1]; + } + return null; + }, + formatMessage: (message: Message) => formatMessage(message, false), + formatMessages: (messages: Message[]) => formatMessages(messages, false), + formatMessageWithToolCalls: (message: Message) => formatMessage(message, true), + formatMessagesWithToolCalls: (messages: Message[]) => formatMessages(messages, true), +} diff --git a/src/template.ts b/src/template.ts index a79594a..65d00e3 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,6 +1,6 @@ import handlebarsEvalless from "@langtail/handlebars-evalless" -import { handlebarsDateHelper, operatorHelpers } from "./handlebars-helpers" +import { handlebarsDateHelper, operatorHelpers, variableHelpers } from "./handlebars-helpers" import { JSONValue } from "./jsonType" import { ContentArray } from "./schemas" @@ -8,6 +8,14 @@ const handlebars = handlebarsEvalless.default ?? handlebarsEvalless handlebars.registerHelper("$date", handlebarsDateHelper) handlebars.registerHelper(operatorHelpers) +handlebars.registerHelper(variableHelpers) + +export const LANGTAIL_HELPERS = [ + "$date", + ...Object.keys(operatorHelpers), + ...Object.keys(variableHelpers), +] + const Visitor = handlebars.Visitor /*