MCPcopy Create free account
hub / github.com/Roy3838/Observer / startAgentLoop

Function startAgentLoop

app/src/utils/main_loop.ts:79–207  ·  view source on GitHub ↗
(agentId: string, getToken?: TokenProvider, skipWhitelistCheck = false)

Source from the content-addressed store, hash-verified

77}
78
79export async function startAgentLoop(agentId: string, getToken?: TokenProvider, skipWhitelistCheck = false): Promise<void> {
80 if (activeLoops[agentId]?.isRunning) {
81 Logger.warn(agentId, `Agent is already running`);
82 return;
83 }
84
85 const isFirstAgent = getRunningAgentIds().length === 0;
86
87
88 try {
89 const agent = await getAgent(agentId);
90 if (!agent) throw new Error(`Agent ${agentId} not found`);
91
92 // Check phone whitelist before starting (unless explicitly skipped)
93 if (!skipWhitelistCheck) {
94 const agentCode = await getAgentCode(agentId) || '';
95 const { phoneNumbers, hasTools, channel } = await checkPhoneWhitelist(agentCode, getToken);
96
97 // If phone tools are used and there are issues, throw error for UI to handle
98 if (hasTools && (phoneNumbers.length === 0 || phoneNumbers.some(p => !p.isWhitelisted))) {
99 const error: any = new Error('Phone whitelist check failed');
100 error.whitelistCheck = { phoneNumbers, hasTools, channel };
101 throw error;
102 }
103 }
104
105 const requiredStreams = getRequiredStreamsForPrompt(agent.system_prompt);
106
107 if (requiredStreams.length > 0) {
108 // A single, transactional call to the StreamManager.
109 await StreamManager.requestStreamsForAgent(agentId, requiredStreams);
110 }
111
112 if (isFirstAgent) {
113 recordingManager.initialize();
114 }
115
116 // Generate sessionId and start new session
117 const sessionId = `session_${new Date().toISOString()}_${Math.random().toString(36).substring(2, 9)}`;
118 IterationStore.startSession(agentId, sessionId);
119
120 // Clear any previous change detection data for fresh start
121 clearAgentChangeData(agentId);
122 resetNoulTrend(agentId);
123
124 const intervalMs = agent.loop_interval_seconds * 1000;
125 activeLoops[agentId] = {
126 intervalId: null,
127 isRunning: true,
128 isExecuting: false,
129 intervalMs,
130 getToken
131 };
132
133 // Logger dispatches the window event automatically
134 Logger.info(agentId, 'Agent started', {
135 logType: 'agent-status-changed',
136 content: { agentId, status: 'running' }

Callers 5

handleCommandMethod · 0.90
executeJavaScriptFunction · 0.90
registry.tsFile · 0.90
toggleAgentFunction · 0.90
AppContentFunction · 0.90

Calls 15

getAgentFunction · 0.90
getAgentCodeFunction · 0.90
checkPhoneWhitelistFunction · 0.90
clearAgentChangeDataFunction · 0.90
resetNoulTrendFunction · 0.90
getRunningAgentIdsFunction · 0.85
executeIterationFunction · 0.85
warnMethod · 0.80
startSessionMethod · 0.80
infoMethod · 0.80

Tested by

no test coverage detected