scalix 0.0.1 → 1.1.0
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/.env.example +29 -0
- package/CHANGELOG.md +122 -0
- package/LICENSE +100 -0
- package/README.md +300 -2
- package/bin/scalix.js +19 -0
- package/dist/agent-definitions.js +100 -0
- package/dist/agent-registry.js +226 -0
- package/dist/agent-replay.js +166 -0
- package/dist/agent.js +1130 -0
- package/dist/api-client.js +77 -0
- package/dist/auto-verify.js +136 -0
- package/dist/change-tracker.js +53 -0
- package/dist/cli.js +2454 -0
- package/dist/commands.js +71 -0
- package/dist/config.js +16 -0
- package/dist/constants.js +358 -0
- package/dist/context-builder.js +158 -0
- package/dist/context-manager.js +148 -0
- package/dist/context-retriever.js +257 -0
- package/dist/cost-tracker.js +85 -0
- package/dist/diff.js +140 -0
- package/dist/git-context.js +183 -0
- package/dist/git-undo.js +67 -0
- package/dist/grounding-checker.js +154 -0
- package/dist/headless.js +743 -0
- package/dist/hooks.js +100 -0
- package/dist/injection-detector.js +86 -0
- package/dist/ipc-server.js +232 -0
- package/dist/logger.js +45 -0
- package/dist/markdown.js +41 -0
- package/dist/mcp.js +49 -0
- package/dist/model-router.js +68 -0
- package/dist/plugins.js +132 -0
- package/dist/project-index.js +376 -0
- package/dist/project-memory.js +165 -0
- package/dist/safe-math.js +194 -0
- package/dist/sandbox.js +403 -0
- package/dist/scoped-rules.js +82 -0
- package/dist/sessions.js +106 -0
- package/dist/settings.js +260 -0
- package/dist/sub-agent.js +154 -0
- package/dist/task-board.js +105 -0
- package/dist/token-counter.js +47 -0
- package/dist/tool-helper.js +97 -0
- package/dist/tool-selector.js +77 -0
- package/dist/tools-advanced.js +487 -0
- package/dist/tools-browser.js +534 -0
- package/dist/tools-extra.js +390 -0
- package/dist/tools-vision.js +116 -0
- package/dist/tools-voice.js +147 -0
- package/dist/tools-web.js +346 -0
- package/dist/tools.js +380 -0
- package/dist/types.js +1 -0
- package/dist/update-checker.js +111 -0
- package/dist/worktree.js +172 -0
- package/docs/DISTRIBUTION.md +301 -0
- package/package.json +71 -6
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { EventEmitter } from 'eventemitter3';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { logger } from './logger.js';
|
|
4
|
+
import { MULTI_AGENT_DEFAULTS } from './constants.js';
|
|
5
|
+
export class AgentRegistry extends EventEmitter {
|
|
6
|
+
static instance = null;
|
|
7
|
+
agents = new Map();
|
|
8
|
+
agentRefs = new Map();
|
|
9
|
+
messages = [];
|
|
10
|
+
limits;
|
|
11
|
+
constructor(limits) {
|
|
12
|
+
super();
|
|
13
|
+
this.limits = {
|
|
14
|
+
maxDepth: limits?.maxDepth ?? MULTI_AGENT_DEFAULTS.maxDepth,
|
|
15
|
+
maxConcurrentAgents: limits?.maxConcurrentAgents ?? MULTI_AGENT_DEFAULTS.maxConcurrentAgents,
|
|
16
|
+
globalTokenBudget: limits?.globalTokenBudget ?? MULTI_AGENT_DEFAULTS.globalTokenBudget,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
static getInstance(limits) {
|
|
20
|
+
if (!AgentRegistry.instance) {
|
|
21
|
+
AgentRegistry.instance = new AgentRegistry(limits);
|
|
22
|
+
}
|
|
23
|
+
return AgentRegistry.instance;
|
|
24
|
+
}
|
|
25
|
+
static resetInstance() {
|
|
26
|
+
if (AgentRegistry.instance) {
|
|
27
|
+
AgentRegistry.instance.removeAllListeners();
|
|
28
|
+
AgentRegistry.instance.agents.clear();
|
|
29
|
+
AgentRegistry.instance.agentRefs.clear();
|
|
30
|
+
AgentRegistry.instance.messages = [];
|
|
31
|
+
}
|
|
32
|
+
AgentRegistry.instance = null;
|
|
33
|
+
}
|
|
34
|
+
register(id, agentRef, parentId, metadata) {
|
|
35
|
+
const depth = parentId ? this.getDepth(parentId) + 1 : 0;
|
|
36
|
+
const entry = {
|
|
37
|
+
id,
|
|
38
|
+
parentId,
|
|
39
|
+
depth,
|
|
40
|
+
description: metadata.description,
|
|
41
|
+
model: metadata.model,
|
|
42
|
+
status: 'running',
|
|
43
|
+
tokenUsage: 0,
|
|
44
|
+
stepCount: 0,
|
|
45
|
+
spawnedAt: Date.now(),
|
|
46
|
+
completedAt: null,
|
|
47
|
+
};
|
|
48
|
+
this.agents.set(id, entry);
|
|
49
|
+
this.agentRefs.set(id, agentRef);
|
|
50
|
+
this.emit('agent:spawned', entry);
|
|
51
|
+
logger.info('Agent registered', { id, parentId, depth, description: metadata.description });
|
|
52
|
+
}
|
|
53
|
+
deregister(id, status) {
|
|
54
|
+
const entry = this.agents.get(id);
|
|
55
|
+
if (!entry)
|
|
56
|
+
return;
|
|
57
|
+
entry.status = status;
|
|
58
|
+
entry.completedAt = Date.now();
|
|
59
|
+
this.agentRefs.delete(id);
|
|
60
|
+
this.emit('agent:completed', entry);
|
|
61
|
+
logger.info('Agent deregistered', { id, status, tokens: entry.tokenUsage, steps: entry.stepCount });
|
|
62
|
+
}
|
|
63
|
+
getAgent(id) {
|
|
64
|
+
return this.agentRefs.get(id);
|
|
65
|
+
}
|
|
66
|
+
getMeta(id) {
|
|
67
|
+
return this.agents.get(id);
|
|
68
|
+
}
|
|
69
|
+
listActive() {
|
|
70
|
+
return [...this.agents.values()].filter(a => a.status === 'running');
|
|
71
|
+
}
|
|
72
|
+
listAll() {
|
|
73
|
+
return [...this.agents.values()];
|
|
74
|
+
}
|
|
75
|
+
getChildren(parentId) {
|
|
76
|
+
return [...this.agents.values()].filter(a => a.parentId === parentId);
|
|
77
|
+
}
|
|
78
|
+
getDepth(id) {
|
|
79
|
+
const entry = this.agents.get(id);
|
|
80
|
+
if (!entry)
|
|
81
|
+
return 0;
|
|
82
|
+
if (!entry.parentId)
|
|
83
|
+
return 0;
|
|
84
|
+
return 1 + this.getDepth(entry.parentId);
|
|
85
|
+
}
|
|
86
|
+
canSpawn(parentId) {
|
|
87
|
+
const activeCount = this.listActive().length;
|
|
88
|
+
if (activeCount >= this.limits.maxConcurrentAgents) {
|
|
89
|
+
return { allowed: false, reason: `Concurrent agent limit reached (${activeCount}/${this.limits.maxConcurrentAgents})` };
|
|
90
|
+
}
|
|
91
|
+
if (parentId) {
|
|
92
|
+
const depth = this.getDepth(parentId) + 1;
|
|
93
|
+
if (depth >= this.limits.maxDepth) {
|
|
94
|
+
return { allowed: false, reason: `Maximum agent depth reached (${depth}/${this.limits.maxDepth})` };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const totalTokens = this.getTotalTokens();
|
|
98
|
+
if (totalTokens >= this.limits.globalTokenBudget) {
|
|
99
|
+
return { allowed: false, reason: `Global token budget exhausted (${totalTokens.toLocaleString()}/${this.limits.globalTokenBudget.toLocaleString()})` };
|
|
100
|
+
}
|
|
101
|
+
return { allowed: true };
|
|
102
|
+
}
|
|
103
|
+
// --- Messaging ---
|
|
104
|
+
sendMessage(from, to, content) {
|
|
105
|
+
if (from === to) {
|
|
106
|
+
return { error: 'Cannot send a message to self' };
|
|
107
|
+
}
|
|
108
|
+
const recipient = this.agents.get(to);
|
|
109
|
+
if (!recipient) {
|
|
110
|
+
return { error: `Agent "${to}" not found` };
|
|
111
|
+
}
|
|
112
|
+
if (recipient.status !== 'running') {
|
|
113
|
+
return { error: `Agent "${to}" is not running (status: ${recipient.status})` };
|
|
114
|
+
}
|
|
115
|
+
const msg = {
|
|
116
|
+
id: randomUUID(),
|
|
117
|
+
from,
|
|
118
|
+
to,
|
|
119
|
+
content,
|
|
120
|
+
timestamp: Date.now(),
|
|
121
|
+
read: false,
|
|
122
|
+
};
|
|
123
|
+
this.messages.push(msg);
|
|
124
|
+
this.emit('agent:message', msg);
|
|
125
|
+
return msg;
|
|
126
|
+
}
|
|
127
|
+
broadcast(from, content) {
|
|
128
|
+
const sent = [];
|
|
129
|
+
for (const agent of this.listActive()) {
|
|
130
|
+
if (agent.id === from)
|
|
131
|
+
continue;
|
|
132
|
+
const result = this.sendMessage(from, agent.id, content);
|
|
133
|
+
if ('id' in result)
|
|
134
|
+
sent.push(result);
|
|
135
|
+
}
|
|
136
|
+
return sent;
|
|
137
|
+
}
|
|
138
|
+
getUnread(agentId) {
|
|
139
|
+
return this.messages.filter(m => m.to === agentId && !m.read);
|
|
140
|
+
}
|
|
141
|
+
markRead(agentId) {
|
|
142
|
+
for (const msg of this.messages) {
|
|
143
|
+
if (msg.to === agentId && !msg.read) {
|
|
144
|
+
msg.read = true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// --- Resource Tracking ---
|
|
149
|
+
recordTokens(id, tokens) {
|
|
150
|
+
const entry = this.agents.get(id);
|
|
151
|
+
if (entry) {
|
|
152
|
+
entry.tokenUsage += tokens;
|
|
153
|
+
}
|
|
154
|
+
const totalTokens = this.getTotalTokens();
|
|
155
|
+
const budgetPct = totalTokens / this.limits.globalTokenBudget;
|
|
156
|
+
if (budgetPct >= 0.9) {
|
|
157
|
+
this.emit('budget:warning', {
|
|
158
|
+
totalTokens,
|
|
159
|
+
budget: this.limits.globalTokenBudget,
|
|
160
|
+
percentage: Math.round(budgetPct * 100),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
recordStep(id) {
|
|
165
|
+
const entry = this.agents.get(id);
|
|
166
|
+
if (entry) {
|
|
167
|
+
entry.stepCount++;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
getTotalTokens() {
|
|
171
|
+
let total = 0;
|
|
172
|
+
for (const entry of this.agents.values()) {
|
|
173
|
+
total += entry.tokenUsage;
|
|
174
|
+
}
|
|
175
|
+
return total;
|
|
176
|
+
}
|
|
177
|
+
getUsageSummary() {
|
|
178
|
+
const all = [...this.agents.values()];
|
|
179
|
+
return {
|
|
180
|
+
totalTokens: this.getTotalTokens(),
|
|
181
|
+
activeCount: all.filter(a => a.status === 'running').length,
|
|
182
|
+
completedCount: all.filter(a => a.status === 'completed').length,
|
|
183
|
+
failedCount: all.filter(a => a.status === 'failed' || a.status === 'aborted').length,
|
|
184
|
+
byAgent: all.map(a => ({
|
|
185
|
+
id: a.id,
|
|
186
|
+
description: a.description,
|
|
187
|
+
model: a.model,
|
|
188
|
+
tokens: a.tokenUsage,
|
|
189
|
+
steps: a.stepCount,
|
|
190
|
+
status: a.status,
|
|
191
|
+
})),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
getAgentTree(rootId) {
|
|
195
|
+
const lines = [];
|
|
196
|
+
const roots = rootId
|
|
197
|
+
? [this.agents.get(rootId)].filter(Boolean)
|
|
198
|
+
: [...this.agents.values()].filter(a => !a.parentId);
|
|
199
|
+
const render = (entry, indent) => {
|
|
200
|
+
const statusIcon = entry.status === 'running' ? '*' : entry.status === 'completed' ? '+' : 'x';
|
|
201
|
+
lines.push(`${indent}[${statusIcon}] ${entry.id.slice(0, 8)} ${entry.description} (${entry.model}, ${entry.tokenUsage.toLocaleString()} tok)`);
|
|
202
|
+
const children = this.getChildren(entry.id);
|
|
203
|
+
for (const child of children) {
|
|
204
|
+
render(child, indent + ' ');
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
for (const root of roots) {
|
|
208
|
+
render(root, '');
|
|
209
|
+
}
|
|
210
|
+
return lines.join('\n') || '(no agents)';
|
|
211
|
+
}
|
|
212
|
+
getLimits() {
|
|
213
|
+
return { ...this.limits };
|
|
214
|
+
}
|
|
215
|
+
setLimits(limits) {
|
|
216
|
+
if (limits.maxDepth !== undefined)
|
|
217
|
+
this.limits.maxDepth = limits.maxDepth;
|
|
218
|
+
if (limits.maxConcurrentAgents !== undefined)
|
|
219
|
+
this.limits.maxConcurrentAgents = limits.maxConcurrentAgents;
|
|
220
|
+
if (limits.globalTokenBudget !== undefined)
|
|
221
|
+
this.limits.globalTokenBudget = limits.globalTokenBudget;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
export function generateAgentId() {
|
|
225
|
+
return randomUUID();
|
|
226
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { USER_CONFIG_DIR } from './constants.js';
|
|
4
|
+
const REPLAYS_DIR = path.join(process.env.HOME || process.env.USERPROFILE || '.', USER_CONFIG_DIR, 'replays');
|
|
5
|
+
export class AgentReplay {
|
|
6
|
+
session;
|
|
7
|
+
stepCounter = 0;
|
|
8
|
+
startTime;
|
|
9
|
+
recording = true;
|
|
10
|
+
constructor(sessionId, model) {
|
|
11
|
+
this.startTime = Date.now();
|
|
12
|
+
this.session = {
|
|
13
|
+
id: sessionId,
|
|
14
|
+
model,
|
|
15
|
+
startedAt: new Date().toISOString(),
|
|
16
|
+
steps: [],
|
|
17
|
+
totalTokens: 0,
|
|
18
|
+
totalCost: 0,
|
|
19
|
+
forkPoints: [],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
record(kind, data = {}) {
|
|
23
|
+
if (!this.recording)
|
|
24
|
+
return { id: -1, kind, timestamp: 0, elapsed: 0, data };
|
|
25
|
+
const step = {
|
|
26
|
+
id: this.stepCounter++,
|
|
27
|
+
kind,
|
|
28
|
+
timestamp: Date.now(),
|
|
29
|
+
elapsed: Date.now() - this.startTime,
|
|
30
|
+
data,
|
|
31
|
+
};
|
|
32
|
+
this.session.steps.push(step);
|
|
33
|
+
if (kind === 'user_message') {
|
|
34
|
+
this.session.forkPoints.push(step.id);
|
|
35
|
+
}
|
|
36
|
+
return step;
|
|
37
|
+
}
|
|
38
|
+
recordUsage(tokens, cost) {
|
|
39
|
+
this.session.totalTokens += tokens;
|
|
40
|
+
this.session.totalCost += cost;
|
|
41
|
+
}
|
|
42
|
+
pause() {
|
|
43
|
+
this.recording = false;
|
|
44
|
+
}
|
|
45
|
+
resume() {
|
|
46
|
+
this.recording = true;
|
|
47
|
+
}
|
|
48
|
+
getSteps() {
|
|
49
|
+
return [...this.session.steps];
|
|
50
|
+
}
|
|
51
|
+
getStepsSince(stepId) {
|
|
52
|
+
const idx = this.session.steps.findIndex((s) => s.id === stepId);
|
|
53
|
+
if (idx === -1)
|
|
54
|
+
return [];
|
|
55
|
+
return this.session.steps.slice(idx);
|
|
56
|
+
}
|
|
57
|
+
getForkPoints() {
|
|
58
|
+
return this.session.forkPoints.map((id) => {
|
|
59
|
+
const step = this.session.steps.find((s) => s.id === id);
|
|
60
|
+
return {
|
|
61
|
+
stepId: id,
|
|
62
|
+
message: step?.data?.content || '(unknown)',
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
getTimeline() {
|
|
67
|
+
const lines = [];
|
|
68
|
+
let toolCallCount = 0;
|
|
69
|
+
for (const step of this.session.steps) {
|
|
70
|
+
const time = formatElapsed(step.elapsed);
|
|
71
|
+
switch (step.kind) {
|
|
72
|
+
case 'user_message':
|
|
73
|
+
lines.push(`\n[${time}] YOU: ${truncate(step.data.content, 60)}`);
|
|
74
|
+
toolCallCount = 0;
|
|
75
|
+
break;
|
|
76
|
+
case 'thinking_start':
|
|
77
|
+
lines.push(`[${time}] Thinking...`);
|
|
78
|
+
break;
|
|
79
|
+
case 'thinking_end':
|
|
80
|
+
lines.push(`[${time}] Done thinking`);
|
|
81
|
+
break;
|
|
82
|
+
case 'stream_end':
|
|
83
|
+
lines.push(`[${time}] AI: ${truncate(step.data.text, 80)}`);
|
|
84
|
+
break;
|
|
85
|
+
case 'tool_call':
|
|
86
|
+
toolCallCount++;
|
|
87
|
+
lines.push(`[${time}] Tool #${toolCallCount}: ${step.data.name} → ${truncate(JSON.stringify(step.data.args), 50)}`);
|
|
88
|
+
break;
|
|
89
|
+
case 'tool_result':
|
|
90
|
+
lines.push(`[${time}] Result: ${truncate(step.data.result, 60)}`);
|
|
91
|
+
break;
|
|
92
|
+
case 'tool_blocked':
|
|
93
|
+
lines.push(`[${time}] BLOCKED: ${step.data.reason}`);
|
|
94
|
+
break;
|
|
95
|
+
case 'error':
|
|
96
|
+
lines.push(`[${time}] ERROR: ${step.data.message}`);
|
|
97
|
+
break;
|
|
98
|
+
case 'context_compress':
|
|
99
|
+
lines.push(`[${time}] Context compressed (${step.data.saved} tokens saved)`);
|
|
100
|
+
break;
|
|
101
|
+
case 'model_switch':
|
|
102
|
+
lines.push(`[${time}] Model: ${step.data.from} → ${step.data.to}`);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
lines.push('');
|
|
107
|
+
lines.push(`Total: ${this.session.steps.length} steps, ${this.session.totalTokens.toLocaleString()} tokens`);
|
|
108
|
+
return lines.join('\n');
|
|
109
|
+
}
|
|
110
|
+
async save() {
|
|
111
|
+
await fs.mkdir(REPLAYS_DIR, { recursive: true });
|
|
112
|
+
const filePath = path.join(REPLAYS_DIR, `${this.session.id}.json`);
|
|
113
|
+
await fs.writeFile(filePath, JSON.stringify(this.session, null, 2), 'utf-8');
|
|
114
|
+
return filePath;
|
|
115
|
+
}
|
|
116
|
+
static async load(sessionId) {
|
|
117
|
+
const filePath = path.join(REPLAYS_DIR, `${sessionId}.json`);
|
|
118
|
+
const raw = await fs.readFile(filePath, 'utf-8');
|
|
119
|
+
const data = JSON.parse(raw);
|
|
120
|
+
const replay = new AgentReplay(data.id, data.model);
|
|
121
|
+
replay.session = data;
|
|
122
|
+
replay.stepCounter = data.steps.length;
|
|
123
|
+
replay.recording = false;
|
|
124
|
+
return replay;
|
|
125
|
+
}
|
|
126
|
+
static async listReplays() {
|
|
127
|
+
try {
|
|
128
|
+
await fs.mkdir(REPLAYS_DIR, { recursive: true });
|
|
129
|
+
const files = await fs.readdir(REPLAYS_DIR);
|
|
130
|
+
const replays = [];
|
|
131
|
+
for (const file of files) {
|
|
132
|
+
if (!file.endsWith('.json'))
|
|
133
|
+
continue;
|
|
134
|
+
try {
|
|
135
|
+
const raw = await fs.readFile(path.join(REPLAYS_DIR, file), 'utf-8');
|
|
136
|
+
const data = JSON.parse(raw);
|
|
137
|
+
replays.push({
|
|
138
|
+
id: data.id,
|
|
139
|
+
model: data.model,
|
|
140
|
+
date: data.startedAt,
|
|
141
|
+
steps: data.steps.length,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
console.error(`Warning: corrupted replay file skipped: ${file}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return replays.sort((a, b) => b.date.localeCompare(a.date));
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function formatElapsed(ms) {
|
|
156
|
+
const seconds = Math.floor(ms / 1000);
|
|
157
|
+
const minutes = Math.floor(seconds / 60);
|
|
158
|
+
const s = seconds % 60;
|
|
159
|
+
return minutes > 0 ? `${minutes}m${s.toString().padStart(2, '0')}s` : `${s}s`;
|
|
160
|
+
}
|
|
161
|
+
function truncate(s, max) {
|
|
162
|
+
if (!s)
|
|
163
|
+
return '';
|
|
164
|
+
const clean = s.replace(/\n/g, ' ').trim();
|
|
165
|
+
return clean.length > max ? clean.slice(0, max - 1) + '\u2026' : clean;
|
|
166
|
+
}
|