messages.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import type { ImageContent, Message, TextContent } from "@earendil-works/pi-ai";
  2. import type { AgentMessage } from "../types.ts";
  3. export const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:
  4. <summary>
  5. `;
  6. export const COMPACTION_SUMMARY_SUFFIX = `
  7. </summary>`;
  8. export const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:
  9. <summary>
  10. `;
  11. export const BRANCH_SUMMARY_SUFFIX = `</summary>`;
  12. export interface BashExecutionMessage {
  13. role: "bashExecution";
  14. command: string;
  15. output: string;
  16. exitCode: number | undefined;
  17. cancelled: boolean;
  18. truncated: boolean;
  19. fullOutputPath?: string;
  20. timestamp: number;
  21. excludeFromContext?: boolean;
  22. }
  23. export interface CustomMessage<T = unknown> {
  24. role: "custom";
  25. customType: string;
  26. content: string | (TextContent | ImageContent)[];
  27. display: boolean;
  28. details?: T;
  29. timestamp: number;
  30. }
  31. export interface BranchSummaryMessage {
  32. role: "branchSummary";
  33. summary: string;
  34. fromId: string;
  35. timestamp: number;
  36. }
  37. export interface CompactionSummaryMessage {
  38. role: "compactionSummary";
  39. summary: string;
  40. tokensBefore: number;
  41. timestamp: number;
  42. }
  43. declare module "../types.ts" {
  44. interface CustomAgentMessages {
  45. bashExecution: BashExecutionMessage;
  46. custom: CustomMessage;
  47. branchSummary: BranchSummaryMessage;
  48. compactionSummary: CompactionSummaryMessage;
  49. }
  50. }
  51. export function bashExecutionToText(msg: BashExecutionMessage): string {
  52. let text = `Ran \`${msg.command}\`\n`;
  53. if (msg.output) {
  54. text += `\`\`\`\n${msg.output}\n\`\`\``;
  55. } else {
  56. text += "(no output)";
  57. }
  58. if (msg.cancelled) {
  59. text += "\n\n(command cancelled)";
  60. } else if (msg.exitCode !== null && msg.exitCode !== undefined && msg.exitCode !== 0) {
  61. text += `\n\nCommand exited with code ${msg.exitCode}`;
  62. }
  63. if (msg.truncated && msg.fullOutputPath) {
  64. text += `\n\n[Output truncated. Full output: ${msg.fullOutputPath}]`;
  65. }
  66. return text;
  67. }
  68. export function createBranchSummaryMessage(summary: string, fromId: string, timestamp: string): BranchSummaryMessage {
  69. return {
  70. role: "branchSummary",
  71. summary,
  72. fromId,
  73. timestamp: new Date(timestamp).getTime(),
  74. };
  75. }
  76. export function createCompactionSummaryMessage(
  77. summary: string,
  78. tokensBefore: number,
  79. timestamp: string,
  80. ): CompactionSummaryMessage {
  81. return {
  82. role: "compactionSummary",
  83. summary,
  84. tokensBefore,
  85. timestamp: new Date(timestamp).getTime(),
  86. };
  87. }
  88. export function createCustomMessage(
  89. customType: string,
  90. content: string | (TextContent | ImageContent)[],
  91. display: boolean,
  92. details: unknown | undefined,
  93. timestamp: string,
  94. ): CustomMessage {
  95. return {
  96. role: "custom",
  97. customType,
  98. content,
  99. display,
  100. details,
  101. timestamp: new Date(timestamp).getTime(),
  102. };
  103. }
  104. export function convertToLlm(messages: AgentMessage[]): Message[] {
  105. return messages
  106. .map((m): Message | undefined => {
  107. switch (m.role) {
  108. case "bashExecution":
  109. if (m.excludeFromContext) {
  110. return undefined;
  111. }
  112. return {
  113. role: "user",
  114. content: [{ type: "text", text: bashExecutionToText(m) }],
  115. timestamp: m.timestamp,
  116. };
  117. case "custom": {
  118. const content = typeof m.content === "string" ? [{ type: "text" as const, text: m.content }] : m.content;
  119. return {
  120. role: "user",
  121. content,
  122. timestamp: m.timestamp,
  123. };
  124. }
  125. case "branchSummary":
  126. return {
  127. role: "user",
  128. content: [{ type: "text" as const, text: BRANCH_SUMMARY_PREFIX + m.summary + BRANCH_SUMMARY_SUFFIX }],
  129. timestamp: m.timestamp,
  130. };
  131. case "compactionSummary":
  132. return {
  133. role: "user",
  134. content: [
  135. { type: "text" as const, text: COMPACTION_SUMMARY_PREFIX + m.summary + COMPACTION_SUMMARY_SUFFIX },
  136. ],
  137. timestamp: m.timestamp,
  138. };
  139. case "user":
  140. case "assistant":
  141. case "toolResult":
  142. return m;
  143. default:
  144. return undefined;
  145. }
  146. })
  147. .filter((m): m is Message => m !== undefined);
  148. }