-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(agents): remove datetime from system prompts, add to user messages #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { getCurrentDateTimeZone } from "./date"; | ||
|
|
||
| /** | ||
| * Return a small XML-like snippet containing environment details. | ||
| * | ||
| * This function obtains the current local date/time (including timezone) | ||
| * and formats it into a simple <environment_details> block suitable for | ||
| * embedding in generated documentation or logs. | ||
| * | ||
| * @returns A string containing the environment details in XML-like format. | ||
| */ | ||
| export function getEnvironmentDetails(now: Date = new Date()): string { | ||
| const localTime = getCurrentDateTimeZone(now); | ||
| return `<environment_details>\n<current_time>${localTime}</current_time>\n</environment_details>`; | ||
| } | ||
|
|
||
| /** | ||
| * Wrap the given message in a simple XML-like <message> block and | ||
| * append the current environment details. | ||
| * | ||
| * This is useful for producing messages that include the original content | ||
| * alongside timestamp/timezone context for logging or documentation. | ||
| * | ||
| * @param message - The main message content to wrap. | ||
| * @returns A combined string containing the message and environment details. | ||
| */ | ||
| export function wrapMessage(message: string, now: Date = new Date()): string { | ||
| const query = `<message>\n${message}\n</message>`; | ||
| const environmentDetails = getEnvironmentDetails(now); | ||
| return `${query}\n\n${environmentDetails}`; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** Return current date/time formatted with timezone, e.g. "YYYY-MM-DDTHH:mm:ss±HH:MM (TimeZone)". */ | ||
| export function getCurrentDateTimeZone(now: Date = new Date()): string { | ||
| let timeZone = "UTC"; | ||
| try { | ||
| timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone ?? "UTC"; | ||
| } catch { | ||
| // Fallback to UTC label if Intl is unavailable or fails | ||
| } | ||
| return `${formatDateTimeWithOffset(now)} (${timeZone})`; | ||
| } | ||
|
|
||
| /** Format a Date as "YYYY-MM-DDTHH:mm:ss±HH:MM". */ | ||
| export function formatDateTimeWithOffset(date: Date): string { | ||
| const year = date.getFullYear(); | ||
| const month = String(date.getMonth() + 1).padStart(2, "0"); | ||
| const day = String(date.getDate()).padStart(2, "0"); | ||
| const hour = String(date.getHours()).padStart(2, "0"); | ||
| const minute = String(date.getMinutes()).padStart(2, "0"); | ||
| const second = String(date.getSeconds()).padStart(2, "0"); | ||
| const offsetMinutes = -date.getTimezoneOffset(); // getTimezoneOffset uses the opposite sign | ||
| const offsetSign = offsetMinutes >= 0 ? "+" : "-"; | ||
| const absOffsetMinutes = Math.abs(offsetMinutes); | ||
| const offsetHours = String(Math.floor(absOffsetMinutes / 60)).padStart(2, "0"); | ||
| const offsetMins = String(absOffsetMinutes % 60).padStart(2, "0"); | ||
| return `${year}-${month}-${day}T${hour}:${minute}:${second}${offsetSign}${offsetHours}:${offsetMins}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export { getDateContext } from "./date-context"; | ||
| export { getEnvironmentDetails, wrapMessage } from "./context"; | ||
| export { formatDateTimeWithOffset, getCurrentDateTimeZone } from "./date"; | ||
| export { UserMessageWrapper } from "./processor"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import type { MastraMessageV2 } from "@mastra/core"; | ||
| import type { Processor } from "@mastra/core/processors"; | ||
| import { wrapMessage } from "./context"; | ||
|
|
||
| /** | ||
| * Processor that wraps user message text parts with environment details. | ||
| * | ||
| * This processor implements the Processor interface and is intended to be run | ||
| * before messages are sent to the model. For each message with role "user", | ||
| * it iterates over content parts and replaces text parts by calling | ||
| * wrapMessage(part.text), thereby appending environment details. | ||
| * | ||
| * The transformation is performed in-place on the provided messages array. | ||
| * | ||
| * @implements {Processor} | ||
| */ | ||
| export class UserMessageWrapper implements Processor { | ||
| readonly name = "user-message-wrapper"; | ||
|
|
||
| /** | ||
| * Process input messages by wrapping text parts of user messages. | ||
| * | ||
| * Iterates over the provided messages array and for each message whose | ||
| * role is "user" replaces the text of parts with type "text" by the | ||
| * output of wrapMessage. The original messages array is mutated and | ||
| * returned for convenience. | ||
| * | ||
| * @param {{ messages: MastraMessageV2[] }} param0 - Object containing the messages array to process. | ||
| * @returns {MastraMessageV2[]} The same messages array after modification. | ||
| */ | ||
| processInput({ messages }: { messages: MastraMessageV2[] }): MastraMessageV2[] { | ||
| if (!Array.isArray(messages) || messages.length === 0) return messages; | ||
| messages.forEach((msg) => { | ||
| if (msg.role !== "user") return; | ||
| msg.content.parts.forEach((part) => { | ||
| if (part.type !== "text") return; | ||
| const text = part.text ?? ""; | ||
| // Idempotency: avoid double wrap if already contains markers | ||
| if (text.includes("<environment_details>") || text.includes("<message>")) return; | ||
| part.text = wrapMessage(text); | ||
| }); | ||
| }); | ||
| return messages; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The hardcoded XML-like tag name
messageshould be extracted as a constant to improve maintainability and consistency with other XML-like structures in this module.