|
1 | 1 | import * as fs from "fs/promises"; |
2 | 2 | import * as path from "path"; |
3 | | -import { |
4 | | - IOutlineData, |
5 | | - IOutlineParam, |
6 | | - IOutlineResult, |
7 | | -} from "../interfaces/Outline.interface"; |
8 | | - |
9 | | -export const dumpOutlineResult = async < |
10 | | - Data extends IOutlineData = IOutlineData, |
11 | | - Param extends IOutlineParam = IOutlineParam |
12 | | ->( |
13 | | - result: IOutlineResult<Data, Param>, |
14 | | - outputDir = "./dump/outline" |
15 | | -) => { |
16 | | - if (!result.isValid) { |
17 | | - return; |
18 | | - } |
19 | | - |
20 | | - // Extract system messages and system reminders from existing data |
21 | | - const systemMessages = result.history.filter((m) => m.role === "system"); |
22 | | - const userMessages = result.history.filter((m) => m.role === "user"); |
23 | | - const subfolderPath = path.join(outputDir, result.resultId); |
24 | | - |
25 | | - await fs.mkdir(subfolderPath, { recursive: true }); |
| 3 | +import { IOutlineResult } from "../interfaces/Outline.interface"; |
| 4 | +import beginContext from "../utils/beginContext"; |
26 | 5 |
|
27 | | - { |
28 | | - let summary = `# Outline Result Summary\n`; |
29 | | - |
30 | | - { |
31 | | - summary += `\n`; |
32 | | - summary += `**ResultId**: ${result.resultId}\n`; |
33 | | - summary += `**Generation Date**: ${new Date().toISOString()}\n`; |
34 | | - summary += `\n`; |
| 6 | +export const dumpOutlineResult = beginContext( |
| 7 | + async (result: IOutlineResult, outputDir = "./dump/outline") => { |
| 8 | + if (!result.isValid) { |
| 9 | + return; |
35 | 10 | } |
36 | 11 |
|
37 | | - if (result.param) { |
38 | | - summary += `## Input Parameters\n\n`; |
39 | | - summary += "```json\n"; |
40 | | - summary += JSON.stringify(result.param, null, 2); |
41 | | - summary += "\n```\n\n"; |
42 | | - } |
| 12 | + // Extract system messages and system reminders from existing data |
| 13 | + const systemMessages = result.history.filter((m) => m.role === "system"); |
| 14 | + const userMessages = result.history.filter((m) => m.role === "user"); |
| 15 | + const subfolderPath = path.join(outputDir, result.resultId); |
43 | 16 |
|
44 | | - if (result.data) { |
45 | | - summary += `## Generated Data\n\n`; |
46 | | - summary += "```json\n"; |
47 | | - summary += JSON.stringify(result.data, null, 2); |
48 | | - summary += "\n```\n\n"; |
| 17 | + try { |
| 18 | + await fs.access(subfolderPath); |
| 19 | + return; |
| 20 | + } catch { |
| 21 | + await fs.mkdir(subfolderPath, { recursive: true }); |
49 | 22 | } |
50 | 23 |
|
51 | | - // Add system messages to summary |
52 | | - if (systemMessages.length > 0) { |
53 | | - summary += `## System Messages\n\n`; |
54 | | - systemMessages.forEach((msg, idx) => { |
55 | | - summary += `### System Message ${idx + 1}\n\n`; |
56 | | - summary += "```\n"; |
57 | | - summary += msg.content; |
| 24 | + { |
| 25 | + let summary = `# Outline Result Summary\n`; |
| 26 | + |
| 27 | + { |
| 28 | + summary += `\n`; |
| 29 | + summary += `**ResultId**: ${result.resultId}\n`; |
| 30 | + summary += `**Generation Date**: ${new Date().toISOString()}\n`; |
| 31 | + summary += `\n`; |
| 32 | + } |
| 33 | + |
| 34 | + if (result.param) { |
| 35 | + summary += `## Input Parameters\n\n`; |
| 36 | + summary += "```json\n"; |
| 37 | + summary += JSON.stringify(result.param, null, 2); |
58 | 38 | summary += "\n```\n\n"; |
59 | | - }); |
| 39 | + } |
| 40 | + |
| 41 | + if (result.data) { |
| 42 | + summary += `## Generated Data\n\n`; |
| 43 | + summary += "```json\n"; |
| 44 | + summary += JSON.stringify(result.data, null, 2); |
| 45 | + summary += "\n```\n\n"; |
| 46 | + } |
| 47 | + |
| 48 | + // Add system messages to summary |
| 49 | + if (systemMessages.length > 0) { |
| 50 | + summary += `## System Messages\n\n`; |
| 51 | + systemMessages.forEach((msg, idx) => { |
| 52 | + summary += `### System Message ${idx + 1}\n\n`; |
| 53 | + summary += msg.content; |
| 54 | + summary += "\n"; |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + const summaryFile = path.join(subfolderPath, "00_system_prompt.md"); |
| 59 | + await fs.writeFile(summaryFile, summary, "utf8"); |
60 | 60 | } |
61 | 61 |
|
62 | | - const summaryFile = path.join(subfolderPath, "00_system_prompt.md"); |
63 | | - await fs.writeFile(summaryFile, summary, "utf8"); |
64 | | - } |
| 62 | + { |
| 63 | + await Promise.all( |
| 64 | + Array.from(userMessages.entries()).map(async ([idx, message]) => { |
| 65 | + const messageNum = String(idx + 1).padStart(2, "0"); |
| 66 | + const contentFileName = `${messageNum}_user_message.md`; |
| 67 | + const contentFilePath = path.join(subfolderPath, contentFileName); |
| 68 | + |
| 69 | + let content = `# User Input ${idx + 1}\n\n`; |
| 70 | + content += `**ResultId**: ${result.resultId}\n\n`; |
| 71 | + content += message.content; |
| 72 | + content += "\n"; |
| 73 | + |
| 74 | + await fs.writeFile(contentFilePath, content, "utf8"); |
| 75 | + }) |
| 76 | + ); |
| 77 | + } |
65 | 78 |
|
66 | | - { |
67 | | - await Promise.all( |
68 | | - Array.from(userMessages.entries()).map(async ([idx, message]) => { |
69 | | - const messageNum = String(idx + 1).padStart(2, "0"); |
70 | | - const contentFileName = `${messageNum}_user_message.md`; |
71 | | - const contentFilePath = path.join(subfolderPath, contentFileName); |
72 | | - |
73 | | - let content = `# User Input ${idx + 1}\n\n`; |
74 | | - content += `**ResultId**: ${result.resultId}\n\n`; |
75 | | - content += "```\n"; |
76 | | - content += message.content; |
| 79 | + { |
| 80 | + const messageNum = String(userMessages.length + 1).padStart(2, "0"); |
| 81 | + const contentFileName = `${messageNum}_llm_output.md`; |
| 82 | + const contentFilePath = path.join(subfolderPath, contentFileName); |
| 83 | + |
| 84 | + let content = `# Full Outline Result\n\n`; |
| 85 | + content += `**ResultId**: ${result.resultId}\n\n`; |
| 86 | + |
| 87 | + if (result.param) { |
| 88 | + content += `## Completion Input Data\n\n`; |
| 89 | + content += "```json\n"; |
| 90 | + content += JSON.stringify(result.param, null, 2); |
| 91 | + content += "\n```\n\n"; |
| 92 | + } |
| 93 | + |
| 94 | + if (result.data) { |
| 95 | + content += `## Completion Output Data\n\n`; |
| 96 | + content += "```json\n"; |
| 97 | + content += JSON.stringify(result.data, null, 2); |
77 | 98 | content += "\n```\n"; |
| 99 | + } |
78 | 100 |
|
79 | | - await fs.writeFile(contentFilePath, content, "utf8"); |
80 | | - }) |
81 | | - ); |
82 | | - } |
83 | | - |
84 | | - { |
85 | | - const messageNum = String(userMessages.length + 1).padStart(2, "0"); |
86 | | - const contentFileName = `${messageNum}_llm_output.md`; |
87 | | - const contentFilePath = path.join(subfolderPath, contentFileName); |
88 | | - |
89 | | - let content = `# Full Outline Result\n\n`; |
90 | | - content += `**ResultId**: ${result.resultId}\n\n`; |
91 | | - |
92 | | - if (result.param) { |
93 | | - content += `## Completion Input Data\n\n`; |
94 | | - content += "```json\n"; |
95 | | - content += JSON.stringify(result.param, null, 2); |
96 | | - content += "\n```\n\n"; |
| 101 | + await fs.writeFile(contentFilePath, content, "utf8"); |
97 | 102 | } |
98 | | - |
99 | | - if (result.data) { |
100 | | - content += `## Completion Output Data\n\n`; |
101 | | - content += "```json\n"; |
102 | | - content += JSON.stringify(result.data, null, 2); |
103 | | - content += "\n```\n"; |
104 | | - } |
105 | | - |
106 | | - await fs.writeFile(contentFilePath, content, "utf8"); |
107 | 103 | } |
108 | | -}; |
| 104 | +); |
0 commit comments