heyio 3.0.2 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/server.js +1 -1
- package/dist/api/server.js.map +1 -1
- package/dist/logging/logger.d.ts.map +1 -1
- package/dist/logging/logger.js +13 -1
- package/dist/logging/logger.js.map +1 -1
- package/node_modules/@io/shared/package.json +1 -1
- package/package.json +7 -2
- package/public/assets/index-2RY89H3W.js +336 -0
- package/public/assets/index-2RY89H3W.js.map +1 -0
- package/public/assets/index-D3cGfBsj.css +1 -0
- package/public/index.html +14 -0
- package/src/api/middleware/auth.ts +0 -76
- package/src/api/notifications.ts +0 -122
- package/src/api/routes/activity.ts +0 -29
- package/src/api/routes/attachments.ts +0 -93
- package/src/api/routes/config.ts +0 -115
- package/src/api/routes/conversations.ts +0 -87
- package/src/api/routes/health.ts +0 -18
- package/src/api/routes/inbox.ts +0 -98
- package/src/api/routes/schedules.ts +0 -121
- package/src/api/routes/skills.ts +0 -105
- package/src/api/routes/squads.ts +0 -145
- package/src/api/routes/usage.ts +0 -57
- package/src/api/routes/wiki.ts +0 -49
- package/src/api/server.ts +0 -186
- package/src/config.ts +0 -3
- package/src/copilot/client.ts +0 -42
- package/src/copilot/health-monitor.ts +0 -85
- package/src/copilot/orchestrator.ts +0 -222
- package/src/copilot/tools.ts +0 -707
- package/src/index.ts +0 -113
- package/src/logging/logger.ts +0 -26
- package/src/models/index.ts +0 -11
- package/src/models/pricing.ts +0 -121
- package/src/models/registry.ts +0 -131
- package/src/models/token-tracker.ts +0 -151
- package/src/scheduler/engine.ts +0 -146
- package/src/skills/index.ts +0 -13
- package/src/skills/store.ts +0 -188
- package/src/squad/agent.ts +0 -326
- package/src/squad/autonomy.ts +0 -78
- package/src/squad/event-bus.ts +0 -71
- package/src/squad/execution/index.ts +0 -17
- package/src/squad/execution/instance.ts +0 -186
- package/src/squad/execution/meeting.ts +0 -191
- package/src/squad/execution/pr.ts +0 -127
- package/src/squad/execution/runner.ts +0 -97
- package/src/squad/execution/tasks.ts +0 -111
- package/src/squad/execution/worktree.ts +0 -138
- package/src/squad/hiring.ts +0 -222
- package/src/squad/index.ts +0 -17
- package/src/squad/manager.ts +0 -337
- package/src/squad/name-generator.ts +0 -135
- package/src/squad/roles/templates.ts +0 -104
- package/src/squad/skill-parser.ts +0 -120
- package/src/squad/source-resolver.ts +0 -57
- package/src/store/activity.ts +0 -176
- package/src/store/db.ts +0 -237
- package/src/store/inbox.ts +0 -199
- package/src/store/schedules.ts +0 -199
- package/src/wiki/index.ts +0 -12
- package/src/wiki/store.ts +0 -139
- package/tsconfig.json +0 -9
package/src/api/server.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
|
-
import { createServer } from 'node:http';
|
|
3
|
-
import { join, resolve } from 'node:path';
|
|
4
|
-
import { fileURLToPath } from 'node:url';
|
|
5
|
-
import express from 'express';
|
|
6
|
-
import { type WebSocket, WebSocketServer } from 'ws';
|
|
7
|
-
import type { IOConfig } from '../config.js';
|
|
8
|
-
import { sendMessage } from '../copilot/orchestrator.js';
|
|
9
|
-
import { createChildLogger } from '../logging/logger.js';
|
|
10
|
-
import { authMiddleware, verifyWsToken } from './middleware/auth.js';
|
|
11
|
-
import { initNotifications, subscribeClient, unsubscribeClient } from './notifications.js';
|
|
12
|
-
import { activityRouter } from './routes/activity.js';
|
|
13
|
-
import { attachmentsRouter } from './routes/attachments.js';
|
|
14
|
-
import { configRouter } from './routes/config.js';
|
|
15
|
-
import { conversationsRouter } from './routes/conversations.js';
|
|
16
|
-
import { healthRouter } from './routes/health.js';
|
|
17
|
-
import { inboxRouter } from './routes/inbox.js';
|
|
18
|
-
import { schedulesRouter } from './routes/schedules.js';
|
|
19
|
-
import { skillsRouter } from './routes/skills.js';
|
|
20
|
-
import { squadsRouter } from './routes/squads.js';
|
|
21
|
-
import { usageRouter } from './routes/usage.js';
|
|
22
|
-
import { wikiRouter } from './routes/wiki.js';
|
|
23
|
-
|
|
24
|
-
export interface ApiServer {
|
|
25
|
-
start(): Promise<void>;
|
|
26
|
-
stop(): Promise<void>;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Connected WebSocket clients keyed by connection ID
|
|
30
|
-
const wsClients = new Map<string, WebSocket>();
|
|
31
|
-
|
|
32
|
-
export function createApiServer(config: IOConfig): ApiServer {
|
|
33
|
-
const logger = createChildLogger('api');
|
|
34
|
-
const app = express();
|
|
35
|
-
app.use(express.json());
|
|
36
|
-
|
|
37
|
-
// Auth middleware — verifies Supabase JWT if configured
|
|
38
|
-
app.use('/api', authMiddleware(config));
|
|
39
|
-
|
|
40
|
-
// Routes
|
|
41
|
-
app.use('/api', healthRouter());
|
|
42
|
-
app.use('/api', usageRouter());
|
|
43
|
-
app.use('/api', squadsRouter());
|
|
44
|
-
app.use('/api', activityRouter());
|
|
45
|
-
app.use('/api', attachmentsRouter(config.dataDir));
|
|
46
|
-
app.use('/api', inboxRouter());
|
|
47
|
-
app.use('/api', schedulesRouter());
|
|
48
|
-
app.use('/api', conversationsRouter());
|
|
49
|
-
app.use('/api', configRouter());
|
|
50
|
-
app.use('/api/wiki', wikiRouter);
|
|
51
|
-
app.use('/api', skillsRouter);
|
|
52
|
-
|
|
53
|
-
// POST /api/messages — send a message to the orchestrator
|
|
54
|
-
app.post('/api/messages', async (req, res) => {
|
|
55
|
-
const { content, source, connectionId } = req.body as {
|
|
56
|
-
content?: string;
|
|
57
|
-
source?: 'tui' | 'telegram' | 'web';
|
|
58
|
-
connectionId?: string;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
if (!content) {
|
|
62
|
-
res.status(400).json({ error: 'content is required' });
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const ws = connectionId ? wsClients.get(connectionId) : undefined;
|
|
67
|
-
|
|
68
|
-
const onDelta = (accumulated: string, done: boolean) => {
|
|
69
|
-
if (ws && ws.readyState === ws.OPEN) {
|
|
70
|
-
ws.send(
|
|
71
|
-
JSON.stringify({
|
|
72
|
-
type: done ? 'message' : 'delta',
|
|
73
|
-
content: accumulated,
|
|
74
|
-
}),
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
const response = await sendMessage(content, source ?? 'web', onDelta);
|
|
81
|
-
res.json({ status: 'ok', content: response });
|
|
82
|
-
} catch (err) {
|
|
83
|
-
logger.error({ err }, 'Error processing message');
|
|
84
|
-
res.status(500).json({ error: 'Failed to process message' });
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
// Serve web frontend static files (production build)
|
|
89
|
-
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
90
|
-
const webDistPath = resolve(__dirname, '../../../web/dist');
|
|
91
|
-
if (existsSync(webDistPath)) {
|
|
92
|
-
app.use(express.static(webDistPath));
|
|
93
|
-
// SPA fallback: serve index.html for any non-API route
|
|
94
|
-
app.get('*', (_req, res) => {
|
|
95
|
-
res.sendFile(join(webDistPath, 'index.html'));
|
|
96
|
-
});
|
|
97
|
-
logger.info({ path: webDistPath }, 'Serving web frontend');
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const server = createServer(app);
|
|
101
|
-
|
|
102
|
-
// WebSocket server for streaming
|
|
103
|
-
const wss = new WebSocketServer({ server, path: '/ws' });
|
|
104
|
-
|
|
105
|
-
wss.on('connection', (ws, req) => {
|
|
106
|
-
// Verify token from query string if auth is configured
|
|
107
|
-
const url = new URL(req.url ?? '', `http://${req.headers.host}`);
|
|
108
|
-
const token = url.searchParams.get('token');
|
|
109
|
-
if (!verifyWsToken(config, token)) {
|
|
110
|
-
ws.close(4001, 'Unauthorized');
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
const connectionId = crypto.randomUUID();
|
|
114
|
-
wsClients.set(connectionId, ws);
|
|
115
|
-
subscribeClient(connectionId, ws);
|
|
116
|
-
logger.info({ connectionId }, 'WebSocket client connected');
|
|
117
|
-
|
|
118
|
-
// Send the connection ID to the client
|
|
119
|
-
ws.send(JSON.stringify({ type: 'connected', connectionId }));
|
|
120
|
-
|
|
121
|
-
ws.on('message', (data) => {
|
|
122
|
-
try {
|
|
123
|
-
const parsed = JSON.parse(data.toString()) as {
|
|
124
|
-
type?: string;
|
|
125
|
-
content?: string;
|
|
126
|
-
source?: string;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
if (parsed.type === 'message' && parsed.content) {
|
|
130
|
-
const source = (parsed.source as 'tui' | 'telegram' | 'web') ?? 'tui';
|
|
131
|
-
|
|
132
|
-
const onDelta = (accumulated: string, done: boolean) => {
|
|
133
|
-
if (ws.readyState === ws.OPEN) {
|
|
134
|
-
ws.send(
|
|
135
|
-
JSON.stringify({
|
|
136
|
-
type: done ? 'message' : 'delta',
|
|
137
|
-
content: accumulated,
|
|
138
|
-
}),
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
sendMessage(parsed.content, source, onDelta).catch((err) => {
|
|
144
|
-
logger.error({ err }, 'Error processing WebSocket message');
|
|
145
|
-
if (ws.readyState === ws.OPEN) {
|
|
146
|
-
ws.send(JSON.stringify({ type: 'error', content: 'Failed to process message' }));
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
} catch (err) {
|
|
151
|
-
logger.error({ err }, 'Failed to parse WebSocket message');
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
ws.on('close', () => {
|
|
156
|
-
wsClients.delete(connectionId);
|
|
157
|
-
unsubscribeClient(connectionId);
|
|
158
|
-
logger.info({ connectionId }, 'WebSocket client disconnected');
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
return {
|
|
163
|
-
async start() {
|
|
164
|
-
return new Promise<void>((resolve) => {
|
|
165
|
-
server.listen(config.apiPort, () => {
|
|
166
|
-
logger.info({ port: config.apiPort }, 'API server listening');
|
|
167
|
-
resolve();
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
async stop() {
|
|
173
|
-
return new Promise<void>((resolve, reject) => {
|
|
174
|
-
for (const ws of wsClients.values()) {
|
|
175
|
-
ws.close();
|
|
176
|
-
}
|
|
177
|
-
wsClients.clear();
|
|
178
|
-
wss.close();
|
|
179
|
-
server.close((err) => {
|
|
180
|
-
if (err) reject(err);
|
|
181
|
-
else resolve();
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
}
|
package/src/config.ts
DELETED
package/src/copilot/client.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { CopilotClient } from '@github/copilot-sdk';
|
|
2
|
-
import type { Logger } from 'pino';
|
|
3
|
-
import { createChildLogger } from '../logging/logger.js';
|
|
4
|
-
|
|
5
|
-
let client: CopilotClient | undefined;
|
|
6
|
-
let logger: Logger;
|
|
7
|
-
|
|
8
|
-
function getLogger(): Logger {
|
|
9
|
-
if (!logger) {
|
|
10
|
-
logger = createChildLogger('copilot-client');
|
|
11
|
-
}
|
|
12
|
-
return logger;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export async function getClient(): Promise<CopilotClient> {
|
|
16
|
-
if (!client) {
|
|
17
|
-
client = new CopilotClient();
|
|
18
|
-
await client.start();
|
|
19
|
-
getLogger().info('Copilot client started');
|
|
20
|
-
}
|
|
21
|
-
return client;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export async function resetClient(): Promise<void> {
|
|
25
|
-
if (client) {
|
|
26
|
-
getLogger().warn('Resetting Copilot client');
|
|
27
|
-
try {
|
|
28
|
-
await client.stop();
|
|
29
|
-
} catch (err) {
|
|
30
|
-
getLogger().error({ err }, 'Error stopping client during reset');
|
|
31
|
-
}
|
|
32
|
-
client = undefined;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function stopClient(): Promise<void> {
|
|
37
|
-
if (client) {
|
|
38
|
-
await client.stop();
|
|
39
|
-
client = undefined;
|
|
40
|
-
getLogger().info('Copilot client stopped');
|
|
41
|
-
}
|
|
42
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { createChildLogger } from '../logging/logger.js';
|
|
2
|
-
import { getClient, resetClient } from './client.js';
|
|
3
|
-
|
|
4
|
-
const logger = () => createChildLogger('health-monitor');
|
|
5
|
-
|
|
6
|
-
let healthCheckInterval: ReturnType<typeof setInterval> | null = null;
|
|
7
|
-
let startTime: number = Date.now();
|
|
8
|
-
|
|
9
|
-
export interface HealthStatus {
|
|
10
|
-
status: 'healthy' | 'degraded' | 'unhealthy';
|
|
11
|
-
uptime: number;
|
|
12
|
-
copilotConnected: boolean;
|
|
13
|
-
lastCheck: Date;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
let lastStatus: HealthStatus = {
|
|
17
|
-
status: 'healthy',
|
|
18
|
-
uptime: 0,
|
|
19
|
-
copilotConnected: false,
|
|
20
|
-
lastCheck: new Date(),
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Start periodic health monitoring of the Copilot SDK connection.
|
|
25
|
-
* Attempts reconnect if connection is lost.
|
|
26
|
-
*/
|
|
27
|
-
export function startHealthMonitor(intervalMs = 30_000): void {
|
|
28
|
-
startTime = Date.now();
|
|
29
|
-
|
|
30
|
-
healthCheckInterval = setInterval(async () => {
|
|
31
|
-
await checkHealth();
|
|
32
|
-
}, intervalMs);
|
|
33
|
-
|
|
34
|
-
// Initial check
|
|
35
|
-
checkHealth();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function stopHealthMonitor(): void {
|
|
39
|
-
if (healthCheckInterval) {
|
|
40
|
-
clearInterval(healthCheckInterval);
|
|
41
|
-
healthCheckInterval = null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function getHealthStatus(): HealthStatus {
|
|
46
|
-
return {
|
|
47
|
-
...lastStatus,
|
|
48
|
-
uptime: Math.floor((Date.now() - startTime) / 1000),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async function checkHealth(): Promise<void> {
|
|
53
|
-
const log = logger();
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
// Verify we can get the client (it auto-starts if needed)
|
|
57
|
-
const client = await getClient();
|
|
58
|
-
lastStatus = {
|
|
59
|
-
status: 'healthy',
|
|
60
|
-
uptime: Math.floor((Date.now() - startTime) / 1000),
|
|
61
|
-
copilotConnected: true,
|
|
62
|
-
lastCheck: new Date(),
|
|
63
|
-
};
|
|
64
|
-
} catch (err) {
|
|
65
|
-
log.warn({ err }, 'Health check failed — Copilot client unavailable');
|
|
66
|
-
lastStatus = {
|
|
67
|
-
status: 'unhealthy',
|
|
68
|
-
uptime: Math.floor((Date.now() - startTime) / 1000),
|
|
69
|
-
copilotConnected: false,
|
|
70
|
-
lastCheck: new Date(),
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// Attempt reconnect
|
|
74
|
-
try {
|
|
75
|
-
log.info('Attempting Copilot client reconnect...');
|
|
76
|
-
await resetClient();
|
|
77
|
-
await getClient();
|
|
78
|
-
log.info('Copilot client reconnected successfully');
|
|
79
|
-
lastStatus.status = 'healthy';
|
|
80
|
-
lastStatus.copilotConnected = true;
|
|
81
|
-
} catch (reconnectErr) {
|
|
82
|
-
log.error({ err: reconnectErr }, 'Reconnect failed');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { approveAll } from '@github/copilot-sdk';
|
|
2
|
-
import type { IOConfig } from '../config.js';
|
|
3
|
-
import { createChildLogger } from '../logging/logger.js';
|
|
4
|
-
import { getActiveSkillsContent } from '../skills/index.js';
|
|
5
|
-
import { listSquads } from '../squad/manager.js';
|
|
6
|
-
import { getDatabase } from '../store/db.js';
|
|
7
|
-
import { getOrchestratorScopes, getPageListing } from '../wiki/index.js';
|
|
8
|
-
import { getClient } from './client.js';
|
|
9
|
-
import { createOrchestratorTools } from './tools.js';
|
|
10
|
-
|
|
11
|
-
type Session = Awaited<ReturnType<Awaited<ReturnType<typeof getClient>>['createSession']>>;
|
|
12
|
-
|
|
13
|
-
let logger: ReturnType<typeof createChildLogger>;
|
|
14
|
-
|
|
15
|
-
let session: Session | undefined;
|
|
16
|
-
let sessionId: string | undefined;
|
|
17
|
-
|
|
18
|
-
interface QueuedMessage {
|
|
19
|
-
prompt: string;
|
|
20
|
-
source?: 'tui' | 'telegram' | 'web';
|
|
21
|
-
onDelta: (accumulated: string, done: boolean) => void;
|
|
22
|
-
resolve: (value: string) => void;
|
|
23
|
-
reject: (reason: unknown) => void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const messageQueue: QueuedMessage[] = [];
|
|
27
|
-
let processing = false;
|
|
28
|
-
|
|
29
|
-
const SYSTEM_MESSAGE_BASE = `You are IO, an AI orchestrator daemon. You help users manage their software projects through intelligent conversation and by coordinating specialized agent squads.
|
|
30
|
-
|
|
31
|
-
## Your Capabilities
|
|
32
|
-
- Answer general questions directly
|
|
33
|
-
- Manage squads: create, monitor, and coordinate teams of AI agents for specific projects
|
|
34
|
-
- Delegate project-specific work to the appropriate squad's team lead
|
|
35
|
-
- Track token usage and costs across all squads
|
|
36
|
-
- Manage the inbox: squads send you deliverables and questions that need user attention
|
|
37
|
-
|
|
38
|
-
## Routing Rules
|
|
39
|
-
- If the user's message relates to a project that has an assigned squad, ALWAYS delegate to that squad using the delegate_to_squad tool
|
|
40
|
-
- If the user asks about squad status, use the appropriate squad tools
|
|
41
|
-
- For general questions unrelated to any squad's project, answer directly
|
|
42
|
-
- NEVER answer project-specific questions yourself if a squad exists for that project
|
|
43
|
-
|
|
44
|
-
## Inbox Rules
|
|
45
|
-
- When a squad has a pending question, proactively tell the user about it
|
|
46
|
-
- Use list_inbox to check for unread items when the user asks about notifications or inbox
|
|
47
|
-
- When the user answers a squad's question, use respond_to_inbox to deliver their response and unblock the squad
|
|
48
|
-
- Summarize deliverables in a friendly, readable way when presenting them to the user
|
|
49
|
-
`;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Build the system message with current squad registry context.
|
|
53
|
-
*/
|
|
54
|
-
async function buildSystemMessage(): Promise<string> {
|
|
55
|
-
try {
|
|
56
|
-
const squads = await listSquads();
|
|
57
|
-
const wikiListing = getPageListing(getOrchestratorScopes());
|
|
58
|
-
const skillsContent = await getActiveSkillsContent('orchestrator');
|
|
59
|
-
|
|
60
|
-
const wikiSection = `\n## Wiki Knowledge\n${wikiListing}\n\nUse read_wiki to access page content. Use write_wiki to record important knowledge.\n`;
|
|
61
|
-
|
|
62
|
-
if (squads.length === 0) {
|
|
63
|
-
return `${SYSTEM_MESSAGE_BASE}\n## Active Squads\n(No squads currently active)${wikiSection}${skillsContent}`;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const squadList = squads
|
|
67
|
-
.map(
|
|
68
|
-
(s) =>
|
|
69
|
-
`- **${s.name}**: project at \`${s.projectPath}\`${s.repoUrl ? ` (${s.repoUrl})` : ''} [autonomy: ${s.autonomyTier}]`,
|
|
70
|
-
)
|
|
71
|
-
.join('\n');
|
|
72
|
-
|
|
73
|
-
return `${SYSTEM_MESSAGE_BASE}\n## Active Squads\n${squadList}\n\nWhen a user's message mentions any of the above projects (by name, path, or related topic), delegate to the corresponding squad.${wikiSection}${skillsContent}`;
|
|
74
|
-
} catch {
|
|
75
|
-
return `${SYSTEM_MESSAGE_BASE}\n## Active Squads\n(Unable to load squad registry)\n`;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export async function initOrchestrator(config: IOConfig): Promise<void> {
|
|
80
|
-
logger = createChildLogger('orchestrator');
|
|
81
|
-
const client = await getClient();
|
|
82
|
-
|
|
83
|
-
const systemMessage = await buildSystemMessage();
|
|
84
|
-
|
|
85
|
-
const sessionOptions = {
|
|
86
|
-
model: config.defaultModel,
|
|
87
|
-
streaming: true,
|
|
88
|
-
tools: createOrchestratorTools(),
|
|
89
|
-
systemMessage: { mode: 'replace' as const, content: systemMessage },
|
|
90
|
-
onPermissionRequest: approveAll,
|
|
91
|
-
infiniteSessions: {
|
|
92
|
-
enabled: true,
|
|
93
|
-
backgroundCompactionThreshold: 0.8,
|
|
94
|
-
bufferExhaustionThreshold: 0.95,
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// Try to resume existing session
|
|
99
|
-
const savedSessionId = await getSavedSessionId();
|
|
100
|
-
if (savedSessionId) {
|
|
101
|
-
try {
|
|
102
|
-
session = await client.resumeSession(savedSessionId, sessionOptions);
|
|
103
|
-
sessionId = savedSessionId;
|
|
104
|
-
logger.info({ sessionId }, 'Resumed orchestrator session');
|
|
105
|
-
return;
|
|
106
|
-
} catch (err) {
|
|
107
|
-
logger.warn({ err, sessionId: savedSessionId }, 'Failed to resume session, creating new one');
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Create new session
|
|
112
|
-
session = await client.createSession(sessionOptions);
|
|
113
|
-
sessionId = session.sessionId;
|
|
114
|
-
await saveSessionId(sessionId);
|
|
115
|
-
logger.info({ sessionId }, 'Created new orchestrator session');
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function sendMessage(
|
|
119
|
-
prompt: string,
|
|
120
|
-
source: 'tui' | 'telegram' | 'web',
|
|
121
|
-
onDelta: (accumulated: string, done: boolean) => void,
|
|
122
|
-
): Promise<string> {
|
|
123
|
-
return new Promise((resolve, reject) => {
|
|
124
|
-
messageQueue.push({ prompt, source, onDelta, resolve, reject });
|
|
125
|
-
processQueue();
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async function processQueue(): Promise<void> {
|
|
130
|
-
if (processing || messageQueue.length === 0) return;
|
|
131
|
-
processing = true;
|
|
132
|
-
|
|
133
|
-
while (messageQueue.length > 0) {
|
|
134
|
-
const msg = messageQueue.shift()!;
|
|
135
|
-
try {
|
|
136
|
-
const response = await processMessage(msg);
|
|
137
|
-
msg.resolve(response);
|
|
138
|
-
} catch (err) {
|
|
139
|
-
logger.error({ err }, 'Error processing message');
|
|
140
|
-
msg.reject(err);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
processing = false;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async function processMessage(msg: QueuedMessage): Promise<string> {
|
|
148
|
-
if (!session) {
|
|
149
|
-
throw new Error('Orchestrator session not initialized');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
let accumulated = '';
|
|
153
|
-
|
|
154
|
-
// Subscribe to streaming deltas
|
|
155
|
-
const unsubDelta = session.on('assistant.message_delta', (event) => {
|
|
156
|
-
accumulated += event.data.deltaContent;
|
|
157
|
-
msg.onDelta(accumulated, false);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
const result = await session.sendAndWait(
|
|
162
|
-
{ prompt: msg.prompt },
|
|
163
|
-
600_000, // 10 minute timeout
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
const finalContent = result?.data?.content || accumulated || '(No response)';
|
|
167
|
-
msg.onDelta(finalContent, true);
|
|
168
|
-
|
|
169
|
-
// Persist conversation (fire-and-forget)
|
|
170
|
-
persistConversation(msg.prompt, finalContent, msg.source);
|
|
171
|
-
|
|
172
|
-
return finalContent;
|
|
173
|
-
} finally {
|
|
174
|
-
unsubDelta();
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export async function destroyOrchestrator(): Promise<void> {
|
|
179
|
-
session = undefined;
|
|
180
|
-
sessionId = undefined;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async function getSavedSessionId(): Promise<string | undefined> {
|
|
184
|
-
try {
|
|
185
|
-
const db = getDatabase();
|
|
186
|
-
const result = await db.execute(
|
|
187
|
-
"SELECT value FROM io_state WHERE key = 'orchestrator_session_id'",
|
|
188
|
-
);
|
|
189
|
-
if (result.rows.length > 0) {
|
|
190
|
-
return result.rows[0].value as string;
|
|
191
|
-
}
|
|
192
|
-
return undefined;
|
|
193
|
-
} catch {
|
|
194
|
-
return undefined;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
async function saveSessionId(id: string): Promise<void> {
|
|
199
|
-
const db = getDatabase();
|
|
200
|
-
await db.execute({
|
|
201
|
-
sql: "INSERT OR REPLACE INTO io_state (key, value) VALUES ('orchestrator_session_id', ?)",
|
|
202
|
-
args: [id],
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function persistConversation(
|
|
207
|
-
userMessage: string,
|
|
208
|
-
assistantResponse: string,
|
|
209
|
-
source?: string,
|
|
210
|
-
): void {
|
|
211
|
-
const db = getDatabase();
|
|
212
|
-
const now = new Date().toISOString();
|
|
213
|
-
|
|
214
|
-
db.execute({
|
|
215
|
-
sql: "INSERT INTO conversations (id, role, content, source, created_at) VALUES (?, 'user', ?, ?, ?)",
|
|
216
|
-
args: [crypto.randomUUID(), userMessage, source ?? null, now],
|
|
217
|
-
});
|
|
218
|
-
db.execute({
|
|
219
|
-
sql: "INSERT INTO conversations (id, role, content, source, created_at) VALUES (?, 'assistant', ?, ?, ?)",
|
|
220
|
-
args: [crypto.randomUUID(), assistantResponse, source ?? null, now],
|
|
221
|
-
});
|
|
222
|
-
}
|