-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
162 lines (140 loc) · 4.77 KB
/
cli.ts
File metadata and controls
162 lines (140 loc) · 4.77 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
import 'dotenv/config';
import { parseArgs } from 'node:util';
import { resolve } from 'node:path';
import { existsSync } from 'node:fs';
import { loadConfig } from './config.js';
import { Agent } from './agent.js';
import { SlackApi } from './slack-api.js';
import { SlackAdapter } from './platforms/slack.js';
const DEFAULT_MODEL = 'haiku'; // Cost-effective default; override with --model or config
const HELP = `
agentloop - AI agent that monitors chat platforms and responds using Claude Code
USAGE
agentloop serve [options]
OPTIONS
--workspace, -w <path> Workspace directory (default: cwd)
--config, -c <path> Config file path (default: config.json)
--model, -m <model> Claude model: haiku, sonnet, opus (default: haiku)
--channel <name> Slack channel allowlist (can repeat)
--channel-blacklist <name> Slack channel blacklist (can repeat)
--help, -h Show this help
ENVIRONMENT
ANTHROPIC_API_KEY Required for Claude Code
SLACK_XOXC, SLACK_XOXD Required for Slack platform
EXAMPLES
agentloop serve --workspace /app/monorepo
agentloop serve -w ./my-project -c ./agentloop.json
agentloop serve --channel "#dev" --channel "#support"
`;
interface CliOptions {
workspace?: string;
config?: string;
model?: string;
channels?: string[];
channelBlacklist?: string[];
help?: boolean;
}
function parseCliArgs(): { command: string; options: CliOptions } {
const { values, positionals } = parseArgs({
allowPositionals: true,
options: {
workspace: { type: 'string', short: 'w' },
config: { type: 'string', short: 'c' },
model: { type: 'string', short: 'm' },
channel: { type: 'string', multiple: true },
'channel-blacklist': { type: 'string', multiple: true },
help: { type: 'boolean', short: 'h' },
},
});
return {
command: positionals[0] || 'serve',
options: {
workspace: values.workspace,
config: values.config,
model: values.model,
channels: values.channel,
channelBlacklist: values['channel-blacklist'],
help: values.help,
},
};
}
async function serve(options: CliOptions): Promise<void> {
const configPath = options.config || 'config.json';
if (!existsSync(configPath)) {
console.error(`Config file not found: ${configPath}`);
process.exit(1);
}
const config = loadConfig(configPath);
const workspaceDir = options.workspace ? resolve(options.workspace) : config.workspaceDir || process.cwd();
const model = options.model || config.claude?.model || DEFAULT_MODEL;
const slackChannels = options.channels?.length ? options.channels : config.slackChannels;
const slackChannelBlacklist = options.channelBlacklist?.length ? options.channelBlacklist : config.slackChannelBlacklist;
const slackUsers = config.slackUsers;
console.log(`[agentloop] Model: ${model}`);
console.log(`[agentloop] Workspace: ${workspaceDir}`);
const mcpServerNames = Object.keys(config.mcpServers ?? {});
if (mcpServerNames.length) {
console.log(`[agentloop] MCP servers: ${mcpServerNames.join(', ')}`);
}
if (slackChannels?.length) {
console.log(`[agentloop] Channels: ${slackChannels.join(', ')}`);
}
if (slackChannelBlacklist?.length) {
console.log(`[agentloop] Channel blacklist: ${slackChannelBlacklist.join(', ')}`);
}
if (slackUsers?.length) {
console.log(`[agentloop] User allowlist: ${slackUsers.join(', ')}`);
}
const agent = new Agent({
model,
workspaceDir,
mcpServers: config.mcpServers,
});
const adapters: { stop: () => void }[] = [];
if (config.platforms?.includes('slack')) {
const xoxc = process.env.SLACK_XOXC;
const xoxd = process.env.SLACK_XOXD;
if (!xoxc || !xoxd) {
console.error('SLACK_XOXC and SLACK_XOXD environment variables required');
process.exit(1);
}
const slackApi = new SlackApi(xoxc, xoxd);
const adapter = new SlackAdapter(
agent,
slackApi,
slackChannels,
slackChannelBlacklist,
slackUsers,
10_000, // pollIntervalMs
config.maxRetries,
);
await adapter.start();
adapters.push(adapter);
}
const shutdown = () => {
console.log('\n[agentloop] Shutting down...');
adapters.forEach(a => a.stop());
process.exit(0);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
}
async function main(): Promise<void> {
const { command, options } = parseCliArgs();
if (options.help || command === 'help') {
console.log(HELP);
process.exit(0);
}
if (command === 'serve') {
await serve(options);
} else {
console.error(`Unknown command: ${command}`);
console.log(HELP);
process.exit(1);
}
}
main().catch(e => {
console.error('Fatal:', e instanceof Error ? e.message : String(e));
process.exit(1);
});