-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
148 lines (126 loc) · 4.36 KB
/
context.ts
File metadata and controls
148 lines (126 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Context - Memory Context Assembler
*
* Assembles memory context based on trigger type
*/
import type { MemoryContext, TriggerType } from './types.js';
import { readSoul, readPeople, readTasks, readCron, readCronSys, readAllL9 } from './l9.js';
import { readRecentMessages, readAllMessages } from './l0.js';
import { readDailySummaries } from './l1.js';
import { readWeeklySummaries } from './l2.js';
import { readWeeklySummaries as readL2WeeklySummaries } from './l2.js';
import { L1_DIR, L2_DIR, PATHS } from './constants.js';
import { buildVariableContext, substituteVariables, hasVariables } from './sys-inject.js';
/**
* Stub functions for scheduler context
*/
export function getTaskContext(projectDir: string): string {
const tasks = readTasks(projectDir);
return tasks || 'No tasks defined';
}
export function getCronContext(projectDir: string, taskContent: string): string {
const cron = readCron(projectDir);
return cron || 'No cron tasks defined';
}
export function getCronSysContext(projectDir: string, taskContent: string): string {
if (!taskContent) {
return 'No task content provided';
}
if (hasVariables(taskContent)) {
const variables = buildVariableContext(projectDir);
return substituteVariables(taskContent, variables);
}
return taskContent;
}
/**
* Get directory listing info
*/
function getDirectoryInfo(projectDir: string): string {
const lines: string[] = [];
lines.push(`# Memory System`);
lines.push(`- L0 (raw messages): ${PATHS.L0}/`);
lines.push(`- L1 (daily summaries): ${PATHS.L1}/`);
lines.push(`- L2 (weekly summaries): ${PATHS.L2}/`);
lines.push(`- L9 (long-term): SOUL.md, PEOPLE.md, TASK.md, CRON.md, CRON_SYS.md`);
return lines.join('\n');
}
/**
* Build memory context for a given trigger type
*/
export function buildMemoryContext(
projectDir: string,
triggerType: TriggerType
): MemoryContext {
// Read L9 files (long-term memory)
const longTermMemory = readAllL9(projectDir);
// Read recent messages from L0
const today = new Date();
const todayStr = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
const recentMessages = readRecentMessages(projectDir, todayStr, 60);
// Read L1 summaries (last 5 days)
const dailySummaries = readDailySummaries(projectDir, 5);
// Read L2 summaries (last 5 weeks)
const weeklySummaries = readL2WeeklySummaries(projectDir, 5);
return {
directoryInfo: getDirectoryInfo(projectDir),
longTermMemory: {
soul: longTermMemory.soul,
people: longTermMemory.people,
tasks: longTermMemory.tasks,
cron: longTermMemory.cron,
cronSys: longTermMemory.cronSys,
},
recentMessages,
dailySummaries,
weeklySummaries,
triggerType,
};
}
/**
* Build context for feishu message trigger
*/
export function getFeishuContext(projectDir: string): MemoryContext {
return buildMemoryContext(projectDir, 'feishu_message');
}
/**
* Build context for scheduled trigger
*/
export function getScheduledContext(projectDir: string): MemoryContext {
return buildMemoryContext(projectDir, 'scheduled');
}
/**
* Format memory context as prompt text
*/
export function formatContextAsPrompt(context: MemoryContext): string {
const parts: string[] = [];
// Long-term memory
if (context.longTermMemory.soul) {
parts.push('## Agent Personality (SOUL)\n' + context.longTermMemory.soul);
}
if (context.longTermMemory.people) {
parts.push('## User Profiles (PEOPLE)\n' + context.longTermMemory.people);
}
if (context.longTermMemory.tasks) {
parts.push('## Current Tasks\n' + context.longTermMemory.tasks);
}
if (context.weeklySummaries.length > 0) {
parts.push('## Weekly Summaries\n');
for (const summary of context.weeklySummaries) {
parts.push(`### ${summary.week_start} - ${summary.week_end}\n${summary.summary}`);
}
}
if (context.dailySummaries.length > 0) {
parts.push('## Recent Daily Summaries\n');
for (const summary of context.dailySummaries) {
parts.push(`### ${summary.date}\n${summary.summary}`);
}
}
if (context.recentMessages.length > 0) {
parts.push('## Recent Conversation\n');
for (const msg of context.recentMessages) {
const role = msg.role === 'user' ? 'User' : 'Assistant';
parts.push(`[${msg.timestamp}] ${role}: ${msg.content}`);
}
}
return parts.join('\n\n');
}