File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,11 +43,12 @@ async function runAgent() {
4343 const currentTask = ' Download Q3 Financial Report' ;
4444
4545 // 2. Check memory before executing
46- const alreadyDone = await diary .hasProcessedTask (currentTask );
46+ const pastResult = await diary .getTaskResult (currentTask );
4747
48- if (alreadyDone ) {
48+ if (pastResult ) {
4949 console .log (` [Agent] ⏩ Skipping task: "${currentTask }". I remember doing this already! ` );
50- return ;
50+ console .log (` [Agent] 💡 Previous Result: ${pastResult } ` );
51+ return pastResult ; // Reuse the old output instantly!
5152 }
5253
5354 // 3. Execute your agent's logic
@@ -57,6 +58,7 @@ async function runAgent() {
5758 // 4. Update the diary
5859 await diary .writeTaskResult (currentTask , result );
5960 console .log (` [Agent] ✅ Task complete. Diary updated! ` );
61+ return result ;
6062}
6163
6264runAgent ();
Original file line number Diff line number Diff line change @@ -65,6 +65,17 @@ export class AgentDiary {
6565 return state . seenSignatures . includes ( signature ) ;
6666 }
6767
68+ /**
69+ * Retrieves the stored result of a previously processed task, if available.
70+ * This allows agents to reuse past outputs instead of regenerating them.
71+ */
72+ public async getTaskResult ( title : string ) : Promise < string | undefined > {
73+ const signature = AgentDiary . normalizeSignature ( title ) ;
74+ const state = await this . readDiary ( ) ;
75+ const record = state . history . find ( r => r . signature === signature ) ;
76+ return record ?. result ;
77+ }
78+
6879 /**
6980 * Filters out items that the agent has already processed.
7081 */
Original file line number Diff line number Diff line change @@ -37,12 +37,17 @@ describe('Agent Diaries Core Logic', () => {
3737 const agent = new AgentDiary ( { agentId : 'test-agent' , storage } ) ;
3838
3939 expect ( await agent . hasProcessedTask ( 'Task A' ) ) . toBe ( false ) ;
40+ expect ( await agent . getTaskResult ( 'Task A' ) ) . toBeUndefined ( ) ;
4041
41- await agent . writeTaskResult ( 'Task A' , 'Success' ) ;
42+ await agent . writeTaskResult ( 'Task A' , 'Success Output ' ) ;
4243
4344 expect ( await agent . hasProcessedTask ( 'Task A' ) ) . toBe ( true ) ;
4445 // Should catch case-insensitive variants
4546 expect ( await agent . hasProcessedTask ( 'TASK a' ) ) . toBe ( true ) ;
47+
48+ // Should return the exact result string
49+ expect ( await agent . getTaskResult ( 'Task A' ) ) . toBe ( 'Success Output' ) ;
50+ expect ( await agent . getTaskResult ( 'task A' ) ) . toBe ( 'Success Output' ) ;
4651 } ) ;
4752
4853 it ( 'should accurately filter new tasks from a batch' , async ( ) => {
You can’t perform that action at this time.
0 commit comments