@locusai/sdk 0.4.5 → 0.4.9
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/index-node.js +1590 -20
- package/dist/index.js +429 -121
- package/dist/orchestrator.d.ts.map +1 -1
- package/package.json +12 -23
- package/dist/agent/artifact-syncer.js +0 -77
- package/dist/agent/codebase-indexer-service.js +0 -55
- package/dist/agent/index.js +0 -5
- package/dist/agent/sprint-planner.js +0 -68
- package/dist/agent/task-executor.js +0 -60
- package/dist/agent/worker.js +0 -252
- package/dist/ai/anthropic-client.js +0 -70
- package/dist/ai/claude-runner.js +0 -71
- package/dist/ai/index.js +0 -2
- package/dist/core/config.js +0 -15
- package/dist/core/index.js +0 -3
- package/dist/core/indexer.js +0 -113
- package/dist/core/prompt-builder.js +0 -83
- package/dist/events.js +0 -15
- package/dist/modules/auth.js +0 -23
- package/dist/modules/base.js +0 -8
- package/dist/modules/ci.js +0 -7
- package/dist/modules/docs.js +0 -38
- package/dist/modules/invitations.js +0 -22
- package/dist/modules/organizations.js +0 -39
- package/dist/modules/sprints.js +0 -34
- package/dist/modules/tasks.js +0 -56
- package/dist/modules/workspaces.js +0 -49
- package/dist/orchestrator.js +0 -356
- package/dist/utils/colors.js +0 -54
- package/dist/utils/retry.js +0 -37
- package/src/agent/artifact-syncer.ts +0 -111
- package/src/agent/codebase-indexer-service.ts +0 -71
- package/src/agent/index.ts +0 -5
- package/src/agent/sprint-planner.ts +0 -86
- package/src/agent/task-executor.ts +0 -85
- package/src/agent/worker.ts +0 -322
- package/src/ai/anthropic-client.ts +0 -93
- package/src/ai/claude-runner.ts +0 -86
- package/src/ai/index.ts +0 -2
- package/src/core/config.ts +0 -21
- package/src/core/index.ts +0 -3
- package/src/core/indexer.ts +0 -131
- package/src/core/prompt-builder.ts +0 -91
- package/src/events.ts +0 -35
- package/src/index-node.ts +0 -23
- package/src/index.ts +0 -159
- package/src/modules/auth.ts +0 -48
- package/src/modules/base.ts +0 -9
- package/src/modules/ci.ts +0 -12
- package/src/modules/docs.ts +0 -84
- package/src/modules/invitations.ts +0 -45
- package/src/modules/organizations.ts +0 -90
- package/src/modules/sprints.ts +0 -69
- package/src/modules/tasks.ts +0 -110
- package/src/modules/workspaces.ts +0 -94
- package/src/orchestrator.ts +0 -473
- package/src/utils/colors.ts +0 -63
- package/src/utils/retry.ts +0 -56
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { getLocusPath } from "../core/config.js";
|
|
4
|
-
/**
|
|
5
|
-
* Handles syncing local artifacts to the platform
|
|
6
|
-
*/
|
|
7
|
-
export class ArtifactSyncer {
|
|
8
|
-
deps;
|
|
9
|
-
constructor(deps) {
|
|
10
|
-
this.deps = deps;
|
|
11
|
-
}
|
|
12
|
-
async getOrCreateArtifactsGroup() {
|
|
13
|
-
try {
|
|
14
|
-
const groups = await this.deps.client.docs.listGroups(this.deps.workspaceId);
|
|
15
|
-
const artifactsGroup = groups.find((g) => g.name === "Artifacts");
|
|
16
|
-
if (artifactsGroup) {
|
|
17
|
-
return artifactsGroup.id;
|
|
18
|
-
}
|
|
19
|
-
// Create the Artifacts group
|
|
20
|
-
const newGroup = await this.deps.client.docs.createGroup(this.deps.workspaceId, {
|
|
21
|
-
name: "Artifacts",
|
|
22
|
-
order: 999, // Place at the end
|
|
23
|
-
});
|
|
24
|
-
this.deps.log("Created 'Artifacts' group for agent-generated docs", "info");
|
|
25
|
-
return newGroup.id;
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
this.deps.log(`Failed to get/create Artifacts group: ${error}`, "error");
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
async sync() {
|
|
33
|
-
const artifactsDir = getLocusPath(this.deps.projectPath, "artifactsDir");
|
|
34
|
-
if (!existsSync(artifactsDir)) {
|
|
35
|
-
mkdirSync(artifactsDir, { recursive: true });
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
try {
|
|
39
|
-
const files = readdirSync(artifactsDir);
|
|
40
|
-
if (files.length === 0)
|
|
41
|
-
return;
|
|
42
|
-
this.deps.log(`Syncing ${files.length} artifacts to server...`, "info");
|
|
43
|
-
// Get or create the Artifacts group
|
|
44
|
-
const artifactsGroupId = await this.getOrCreateArtifactsGroup();
|
|
45
|
-
// Get existing docs to check for updates
|
|
46
|
-
const existingDocs = await this.deps.client.docs.list(this.deps.workspaceId);
|
|
47
|
-
for (const file of files) {
|
|
48
|
-
const filePath = join(artifactsDir, file);
|
|
49
|
-
if (statSync(filePath).isFile()) {
|
|
50
|
-
const content = readFileSync(filePath, "utf-8");
|
|
51
|
-
const title = file.replace(/\.md$/, "").trim();
|
|
52
|
-
if (!title)
|
|
53
|
-
continue;
|
|
54
|
-
const existing = existingDocs.find((d) => d.title === title);
|
|
55
|
-
if (existing) {
|
|
56
|
-
if (existing.content !== content ||
|
|
57
|
-
existing.groupId !== artifactsGroupId) {
|
|
58
|
-
await this.deps.client.docs.update(existing.id, this.deps.workspaceId, { content, groupId: artifactsGroupId });
|
|
59
|
-
this.deps.log(`Updated artifact: ${file}`, "success");
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
await this.deps.client.docs.create(this.deps.workspaceId, {
|
|
64
|
-
title,
|
|
65
|
-
content,
|
|
66
|
-
groupId: artifactsGroupId,
|
|
67
|
-
});
|
|
68
|
-
this.deps.log(`Created artifact: ${file}`, "success");
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
this.deps.log(`Failed to sync artifacts: ${error}`, "error");
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { CodebaseIndexer } from "../core/indexer.js";
|
|
2
|
-
/**
|
|
3
|
-
* Handles codebase indexing with AI analysis
|
|
4
|
-
*/
|
|
5
|
-
export class CodebaseIndexerService {
|
|
6
|
-
deps;
|
|
7
|
-
indexer;
|
|
8
|
-
constructor(deps) {
|
|
9
|
-
this.deps = deps;
|
|
10
|
-
this.indexer = new CodebaseIndexer(deps.projectPath);
|
|
11
|
-
}
|
|
12
|
-
async reindex() {
|
|
13
|
-
try {
|
|
14
|
-
this.deps.log("Reindexing codebase...", "info");
|
|
15
|
-
const index = await this.indexer.index((msg) => this.deps.log(msg, "info"), async (tree) => {
|
|
16
|
-
const prompt = `You are a codebase analysis expert. Analyze the file tree and extract:
|
|
17
|
-
1. Key symbols (classes, functions, types) and their locations
|
|
18
|
-
2. Responsibilities of each directory/file
|
|
19
|
-
3. Overall project structure
|
|
20
|
-
|
|
21
|
-
Analyze this file tree and provide a JSON response with:
|
|
22
|
-
- "symbols": object mapping symbol names to file paths (array)
|
|
23
|
-
- "responsibilities": object mapping paths to brief descriptions
|
|
24
|
-
|
|
25
|
-
File tree:
|
|
26
|
-
${tree}
|
|
27
|
-
|
|
28
|
-
Return ONLY valid JSON, no markdown formatting.`;
|
|
29
|
-
let response;
|
|
30
|
-
if (this.deps.anthropicClient) {
|
|
31
|
-
// Use Anthropic SDK with caching for faster indexing
|
|
32
|
-
response = await this.deps.anthropicClient.run({
|
|
33
|
-
systemPrompt: "You are a codebase analysis expert specialized in extracting structure and symbols from file trees.",
|
|
34
|
-
userPrompt: prompt,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
// Fallback to Claude CLI
|
|
39
|
-
response = await this.deps.claudeRunner.run(prompt, true);
|
|
40
|
-
}
|
|
41
|
-
// Extract JSON from response (handle markdown code blocks)
|
|
42
|
-
const jsonMatch = response.match(/\{[\s\S]*\}/);
|
|
43
|
-
if (jsonMatch) {
|
|
44
|
-
return JSON.parse(jsonMatch[0]);
|
|
45
|
-
}
|
|
46
|
-
return { symbols: {}, responsibilities: {}, lastIndexed: "" };
|
|
47
|
-
});
|
|
48
|
-
this.indexer.saveIndex(index);
|
|
49
|
-
this.deps.log("Codebase reindexed successfully", "success");
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
this.deps.log(`Failed to reindex codebase: ${error}`, "error");
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
package/dist/agent/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export { ArtifactSyncer } from "./artifact-syncer.js";
|
|
2
|
-
export { CodebaseIndexerService } from "./codebase-indexer-service.js";
|
|
3
|
-
export { SprintPlanner } from "./sprint-planner.js";
|
|
4
|
-
export { TaskExecutor } from "./task-executor.js";
|
|
5
|
-
export { AgentWorker } from "./worker.js";
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Handles sprint planning and mindmap generation
|
|
3
|
-
*/
|
|
4
|
-
export class SprintPlanner {
|
|
5
|
-
deps;
|
|
6
|
-
constructor(deps) {
|
|
7
|
-
this.deps = deps;
|
|
8
|
-
}
|
|
9
|
-
async planSprint(sprint, tasks) {
|
|
10
|
-
this.deps.log(`Planning sprint: ${sprint.name}`, "info");
|
|
11
|
-
try {
|
|
12
|
-
const taskList = tasks
|
|
13
|
-
.map((t) => `- [${t.id}] ${t.title}: ${t.description || "No description"}`)
|
|
14
|
-
.join("\n");
|
|
15
|
-
let plan;
|
|
16
|
-
if (this.deps.anthropicClient) {
|
|
17
|
-
// Use Anthropic SDK with caching for faster planning
|
|
18
|
-
const systemPrompt = `You are an expert project manager and lead engineer specialized in sprint planning and task prioritization.`;
|
|
19
|
-
const userPrompt = `# Sprint Planning: ${sprint.name}
|
|
20
|
-
|
|
21
|
-
## Tasks
|
|
22
|
-
${taskList}
|
|
23
|
-
|
|
24
|
-
## Instructions
|
|
25
|
-
1. Analyze dependencies between these tasks.
|
|
26
|
-
2. Prioritize them for the most efficient execution.
|
|
27
|
-
3. Create a mindmap (in Markdown or Mermaid format) that visualizes the sprint structure.
|
|
28
|
-
4. Output your final plan. The plan should clearly state the order of execution.
|
|
29
|
-
|
|
30
|
-
**IMPORTANT**:
|
|
31
|
-
- Do NOT create any files on the filesystem during this planning phase.
|
|
32
|
-
- Avoid using absolute local paths (e.g., /Users/...) in your output. Use relative paths starting from the project root if necessary.
|
|
33
|
-
- Your output will be saved as the official sprint mindmap on the server.`;
|
|
34
|
-
plan = await this.deps.anthropicClient.run({
|
|
35
|
-
systemPrompt,
|
|
36
|
-
userPrompt,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
// Fallback to Claude CLI
|
|
41
|
-
const planningPrompt = `# Sprint Planning: ${sprint.name}
|
|
42
|
-
|
|
43
|
-
You are an expert project manager and lead engineer. You need to create a mindmap and execution plan for the following tasks in this sprint.
|
|
44
|
-
|
|
45
|
-
## Tasks
|
|
46
|
-
${taskList}
|
|
47
|
-
|
|
48
|
-
## Instructions
|
|
49
|
-
1. Analyze dependencies between these tasks.
|
|
50
|
-
2. Prioritize them for the most efficient execution.
|
|
51
|
-
3. Create a mindmap (in Markdown or Mermaid format) that visualizes the sprint structure.
|
|
52
|
-
4. Output your final plan. The plan should clearly state the order of execution.
|
|
53
|
-
|
|
54
|
-
**IMPORTANT**:
|
|
55
|
-
- Do NOT create any files on the filesystem during this planning phase.
|
|
56
|
-
- Avoid using absolute local paths (e.g., /Users/...) in your output. Use relative paths starting from the project root if necessary.
|
|
57
|
-
- Your output will be saved as the official sprint mindmap on the server.`;
|
|
58
|
-
plan = await this.deps.claudeRunner.run(planningPrompt, true);
|
|
59
|
-
}
|
|
60
|
-
this.deps.log("Sprint mindmap generated and posted to server.", "success");
|
|
61
|
-
return plan;
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
this.deps.log(`Sprint planning failed: ${error}`, "error");
|
|
65
|
-
return sprint.mindmap || "";
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { PromptBuilder } from "../core/prompt-builder.js";
|
|
2
|
-
/**
|
|
3
|
-
* Handles task execution with two-phase approach (planning + execution)
|
|
4
|
-
*/
|
|
5
|
-
export class TaskExecutor {
|
|
6
|
-
deps;
|
|
7
|
-
promptBuilder;
|
|
8
|
-
constructor(deps) {
|
|
9
|
-
this.deps = deps;
|
|
10
|
-
this.promptBuilder = new PromptBuilder(deps.projectPath);
|
|
11
|
-
}
|
|
12
|
-
updateSprintPlan(sprintPlan) {
|
|
13
|
-
this.deps.sprintPlan = sprintPlan;
|
|
14
|
-
}
|
|
15
|
-
async execute(task) {
|
|
16
|
-
this.deps.log(`Executing: ${task.title}`, "info");
|
|
17
|
-
let basePrompt = await this.promptBuilder.build(task);
|
|
18
|
-
if (this.deps.sprintPlan) {
|
|
19
|
-
basePrompt = `## Sprint Context\n${this.deps.sprintPlan}\n\n${basePrompt}`;
|
|
20
|
-
}
|
|
21
|
-
try {
|
|
22
|
-
let plan = "";
|
|
23
|
-
if (this.deps.anthropicClient) {
|
|
24
|
-
// Phase 1: Planning (using Anthropic SDK with caching)
|
|
25
|
-
this.deps.log("Phase 1: Planning (Anthropic SDK)...", "info");
|
|
26
|
-
// Build cacheable context blocks
|
|
27
|
-
const cacheableContext = [basePrompt];
|
|
28
|
-
plan = await this.deps.anthropicClient.run({
|
|
29
|
-
systemPrompt: "You are an expert software engineer. Analyze the task carefully and create a detailed implementation plan.",
|
|
30
|
-
cacheableContext,
|
|
31
|
-
userPrompt: "## Phase 1: Planning\nAnalyze and create a detailed plan for THIS SPECIFIC TASK. Do NOT execute changes yet.",
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
this.deps.log("Skipping Phase 1: Planning (No Anthropic API Key)...", "info");
|
|
36
|
-
}
|
|
37
|
-
// Phase 2: Execution (always using Claude CLI for agentic tools)
|
|
38
|
-
this.deps.log("Starting Execution...", "info");
|
|
39
|
-
let executionPrompt = basePrompt;
|
|
40
|
-
if (plan) {
|
|
41
|
-
executionPrompt += `\n\n## Phase 2: Execution\nBased on the plan, execute the task:\n\n${plan}`;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
executionPrompt += `\n\n## Execution\nExecute the task directly.`;
|
|
45
|
-
}
|
|
46
|
-
executionPrompt += `\n\nWhen finished, output: <promise>COMPLETE</promise>`;
|
|
47
|
-
const output = await this.deps.claudeRunner.run(executionPrompt);
|
|
48
|
-
const success = output.includes("<promise>COMPLETE</promise>");
|
|
49
|
-
return {
|
|
50
|
-
success,
|
|
51
|
-
summary: success
|
|
52
|
-
? "Task completed by Claude"
|
|
53
|
-
: "Claude did not signal completion",
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
return { success: false, summary: `Error: ${error}` };
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
package/dist/agent/worker.js
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { AnthropicClient } from "../ai/anthropic-client.js";
|
|
2
|
-
import { ClaudeRunner } from "../ai/claude-runner.js";
|
|
3
|
-
import { LocusClient } from "../index.js";
|
|
4
|
-
import { c } from "../utils/colors.js";
|
|
5
|
-
import { ArtifactSyncer } from "./artifact-syncer.js";
|
|
6
|
-
import { CodebaseIndexerService } from "./codebase-indexer-service.js";
|
|
7
|
-
import { SprintPlanner } from "./sprint-planner.js";
|
|
8
|
-
import { TaskExecutor } from "./task-executor.js";
|
|
9
|
-
/**
|
|
10
|
-
* Main agent worker that orchestrates task execution
|
|
11
|
-
* Delegates responsibilities to specialized services
|
|
12
|
-
*/
|
|
13
|
-
export class AgentWorker {
|
|
14
|
-
config;
|
|
15
|
-
client;
|
|
16
|
-
claudeRunner;
|
|
17
|
-
anthropicClient;
|
|
18
|
-
// Services
|
|
19
|
-
sprintPlanner;
|
|
20
|
-
indexerService;
|
|
21
|
-
artifactSyncer;
|
|
22
|
-
taskExecutor;
|
|
23
|
-
// State
|
|
24
|
-
consecutiveEmpty = 0;
|
|
25
|
-
maxEmpty = 10;
|
|
26
|
-
maxTasks = 50;
|
|
27
|
-
tasksCompleted = 0;
|
|
28
|
-
pollInterval = 10_000;
|
|
29
|
-
sprintPlan = null;
|
|
30
|
-
constructor(config) {
|
|
31
|
-
this.config = config;
|
|
32
|
-
const projectPath = config.projectPath || process.cwd();
|
|
33
|
-
// Initialize API client
|
|
34
|
-
this.client = new LocusClient({
|
|
35
|
-
baseUrl: config.apiBase,
|
|
36
|
-
token: config.apiKey,
|
|
37
|
-
retryOptions: {
|
|
38
|
-
maxRetries: 3,
|
|
39
|
-
initialDelay: 1000,
|
|
40
|
-
maxDelay: 5000,
|
|
41
|
-
factor: 2,
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
// Initialize AI clients
|
|
45
|
-
this.claudeRunner = new ClaudeRunner(projectPath, config.model);
|
|
46
|
-
this.anthropicClient = config.anthropicApiKey
|
|
47
|
-
? new AnthropicClient({
|
|
48
|
-
apiKey: config.anthropicApiKey,
|
|
49
|
-
model: config.model,
|
|
50
|
-
})
|
|
51
|
-
: null;
|
|
52
|
-
// Initialize services with dependencies
|
|
53
|
-
const logFn = this.log.bind(this);
|
|
54
|
-
this.sprintPlanner = new SprintPlanner({
|
|
55
|
-
anthropicClient: this.anthropicClient,
|
|
56
|
-
claudeRunner: this.claudeRunner,
|
|
57
|
-
log: logFn,
|
|
58
|
-
});
|
|
59
|
-
this.indexerService = new CodebaseIndexerService({
|
|
60
|
-
anthropicClient: this.anthropicClient,
|
|
61
|
-
claudeRunner: this.claudeRunner,
|
|
62
|
-
projectPath,
|
|
63
|
-
log: logFn,
|
|
64
|
-
});
|
|
65
|
-
this.artifactSyncer = new ArtifactSyncer({
|
|
66
|
-
client: this.client,
|
|
67
|
-
workspaceId: config.workspaceId,
|
|
68
|
-
projectPath,
|
|
69
|
-
log: logFn,
|
|
70
|
-
});
|
|
71
|
-
this.taskExecutor = new TaskExecutor({
|
|
72
|
-
anthropicClient: this.anthropicClient,
|
|
73
|
-
claudeRunner: this.claudeRunner,
|
|
74
|
-
projectPath,
|
|
75
|
-
sprintPlan: null, // Will be set after sprint planning
|
|
76
|
-
log: logFn,
|
|
77
|
-
});
|
|
78
|
-
// Log initialization
|
|
79
|
-
if (this.anthropicClient) {
|
|
80
|
-
this.log("Using Anthropic SDK with prompt caching for planning phases", "info");
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
this.log("Using Claude CLI for all phases (no Anthropic API key provided)", "info");
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
log(message, level = "info") {
|
|
87
|
-
const timestamp = new Date().toISOString().split("T")[1]?.slice(0, 8) ?? "";
|
|
88
|
-
const colorFn = {
|
|
89
|
-
info: c.cyan,
|
|
90
|
-
success: c.green,
|
|
91
|
-
warn: c.yellow,
|
|
92
|
-
error: c.red,
|
|
93
|
-
}[level];
|
|
94
|
-
const prefix = { info: "ℹ", success: "✓", warn: "⚠", error: "✗" }[level];
|
|
95
|
-
console.log(`${c.dim(`[${timestamp}]`)} ${c.bold(`[${this.config.agentId.slice(-8)}]`)} ${colorFn(`${prefix} ${message}`)}`);
|
|
96
|
-
}
|
|
97
|
-
async getActiveSprint() {
|
|
98
|
-
try {
|
|
99
|
-
if (this.config.sprintId) {
|
|
100
|
-
return await this.client.sprints.getById(this.config.sprintId, this.config.workspaceId);
|
|
101
|
-
}
|
|
102
|
-
return await this.client.sprints.getActive(this.config.workspaceId);
|
|
103
|
-
}
|
|
104
|
-
catch (_error) {
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
async getNextTask() {
|
|
109
|
-
try {
|
|
110
|
-
const task = await this.client.workspaces.dispatch(this.config.workspaceId, this.config.agentId, this.config.sprintId);
|
|
111
|
-
return task;
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
this.log(`No task dispatched: ${error instanceof Error ? error.message : String(error)}`, "info");
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
async executeTask(task) {
|
|
119
|
-
// Fetch full task details to get comments/feedback
|
|
120
|
-
const fullTask = await this.client.tasks.getById(task.id, this.config.workspaceId);
|
|
121
|
-
// Update task executor with current sprint plan
|
|
122
|
-
this.taskExecutor.updateSprintPlan(this.sprintPlan);
|
|
123
|
-
// Execute the task
|
|
124
|
-
const result = await this.taskExecutor.execute(fullTask);
|
|
125
|
-
// Reindex codebase after execution to ensure fresh context
|
|
126
|
-
await this.indexerService.reindex();
|
|
127
|
-
return result;
|
|
128
|
-
}
|
|
129
|
-
async run() {
|
|
130
|
-
this.log(`Agent started in ${this.config.projectPath || process.cwd()}`, "success");
|
|
131
|
-
// Initial Sprint Planning Phase
|
|
132
|
-
const sprint = await this.getActiveSprint();
|
|
133
|
-
if (sprint) {
|
|
134
|
-
this.log(`Found active sprint: ${sprint.name} (${sprint.id})`, "info");
|
|
135
|
-
const tasks = await this.client.tasks.list(this.config.workspaceId, {
|
|
136
|
-
sprintId: sprint.id,
|
|
137
|
-
});
|
|
138
|
-
this.log(`Sprint tasks found: ${tasks.length}`, "info");
|
|
139
|
-
const latestTaskCreation = tasks.reduce((latest, task) => {
|
|
140
|
-
const taskDate = new Date(task.createdAt);
|
|
141
|
-
return taskDate > latest ? taskDate : latest;
|
|
142
|
-
}, new Date(0));
|
|
143
|
-
const mindmapDate = sprint.mindmapUpdatedAt
|
|
144
|
-
? new Date(sprint.mindmapUpdatedAt)
|
|
145
|
-
: new Date(0);
|
|
146
|
-
// Skip mindmap generation if there's only one task
|
|
147
|
-
if (tasks.length <= 1) {
|
|
148
|
-
this.log("Skipping mindmap generation (only one task in sprint).", "info");
|
|
149
|
-
this.sprintPlan = null;
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
const needsPlanning = !sprint.mindmap ||
|
|
153
|
-
sprint.mindmap.trim() === "" ||
|
|
154
|
-
latestTaskCreation > mindmapDate;
|
|
155
|
-
if (needsPlanning) {
|
|
156
|
-
if (sprint.mindmap && latestTaskCreation > mindmapDate) {
|
|
157
|
-
this.log("New tasks have been added to the sprint since last mindmap. Regenerating...", "warn");
|
|
158
|
-
}
|
|
159
|
-
this.sprintPlan = await this.sprintPlanner.planSprint(sprint, tasks);
|
|
160
|
-
// Save mindmap to server
|
|
161
|
-
await this.client.sprints.update(sprint.id, this.config.workspaceId, {
|
|
162
|
-
mindmap: this.sprintPlan,
|
|
163
|
-
mindmapUpdatedAt: new Date(),
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
this.log("Using existing sprint mindmap.", "info");
|
|
168
|
-
this.sprintPlan = sprint.mindmap ?? null;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
this.log("No active sprint found for planning.", "warn");
|
|
174
|
-
}
|
|
175
|
-
// Main task execution loop
|
|
176
|
-
while (this.tasksCompleted < this.maxTasks &&
|
|
177
|
-
this.consecutiveEmpty < this.maxEmpty) {
|
|
178
|
-
const task = await this.getNextTask();
|
|
179
|
-
if (!task) {
|
|
180
|
-
this.consecutiveEmpty++;
|
|
181
|
-
if (this.consecutiveEmpty >= this.maxEmpty)
|
|
182
|
-
break;
|
|
183
|
-
await new Promise((r) => setTimeout(r, this.pollInterval));
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
this.consecutiveEmpty = 0;
|
|
187
|
-
this.log(`Claimed: ${task.title}`, "success");
|
|
188
|
-
const result = await this.executeTask(task);
|
|
189
|
-
// Sync artifacts after task execution
|
|
190
|
-
await this.artifactSyncer.sync();
|
|
191
|
-
if (result.success) {
|
|
192
|
-
await this.client.tasks.update(task.id, this.config.workspaceId, {
|
|
193
|
-
status: "VERIFICATION",
|
|
194
|
-
});
|
|
195
|
-
await this.client.tasks.addComment(task.id, this.config.workspaceId, {
|
|
196
|
-
author: this.config.agentId,
|
|
197
|
-
text: `✅ ${result.summary}`,
|
|
198
|
-
});
|
|
199
|
-
this.tasksCompleted++;
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
await this.client.tasks.update(task.id, this.config.workspaceId, {
|
|
203
|
-
status: "BACKLOG",
|
|
204
|
-
assignedTo: null,
|
|
205
|
-
});
|
|
206
|
-
await this.client.tasks.addComment(task.id, this.config.workspaceId, {
|
|
207
|
-
author: this.config.agentId,
|
|
208
|
-
text: `❌ ${result.summary}`,
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
process.exit(0);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
// CLI entry point
|
|
216
|
-
if (process.argv[1]?.includes("agent-worker") ||
|
|
217
|
-
process.argv[1]?.includes("worker")) {
|
|
218
|
-
const args = process.argv.slice(2);
|
|
219
|
-
const config = {};
|
|
220
|
-
for (let i = 0; i < args.length; i++) {
|
|
221
|
-
const arg = args[i];
|
|
222
|
-
if (arg === "--agent-id")
|
|
223
|
-
config.agentId = args[++i];
|
|
224
|
-
else if (arg === "--workspace-id")
|
|
225
|
-
config.workspaceId = args[++i];
|
|
226
|
-
else if (arg === "--sprint-id")
|
|
227
|
-
config.sprintId = args[++i];
|
|
228
|
-
else if (arg === "--api-base")
|
|
229
|
-
config.apiBase = args[++i];
|
|
230
|
-
else if (arg === "--api-key")
|
|
231
|
-
config.apiKey = args[++i];
|
|
232
|
-
else if (arg === "--anthropic-api-key")
|
|
233
|
-
config.anthropicApiKey = args[++i];
|
|
234
|
-
else if (arg === "--project-path")
|
|
235
|
-
config.projectPath = args[++i];
|
|
236
|
-
else if (arg === "--model")
|
|
237
|
-
config.model = args[++i];
|
|
238
|
-
}
|
|
239
|
-
if (!config.agentId ||
|
|
240
|
-
!config.workspaceId ||
|
|
241
|
-
!config.apiBase ||
|
|
242
|
-
!config.apiKey ||
|
|
243
|
-
!config.projectPath) {
|
|
244
|
-
console.error("Missing required arguments");
|
|
245
|
-
process.exit(1);
|
|
246
|
-
}
|
|
247
|
-
const worker = new AgentWorker(config);
|
|
248
|
-
worker.run().catch((err) => {
|
|
249
|
-
console.error("Fatal worker error:", err);
|
|
250
|
-
process.exit(1);
|
|
251
|
-
});
|
|
252
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
-
import { DEFAULT_MODEL } from "../core/config.js";
|
|
3
|
-
/**
|
|
4
|
-
* Anthropic Client with Prompt Caching Support
|
|
5
|
-
*
|
|
6
|
-
* This client wraps the official Anthropic SDK and adds support for
|
|
7
|
-
* prompt caching to dramatically reduce latency and costs for repeated
|
|
8
|
-
* context (like codebase indexes, CLAUDE.md, etc.)
|
|
9
|
-
*/
|
|
10
|
-
export class AnthropicClient {
|
|
11
|
-
client;
|
|
12
|
-
model;
|
|
13
|
-
constructor(config) {
|
|
14
|
-
this.client = new Anthropic({
|
|
15
|
-
apiKey: config.apiKey,
|
|
16
|
-
});
|
|
17
|
-
this.model = config.model || DEFAULT_MODEL;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Run a prompt with optional caching for large context blocks
|
|
21
|
-
*
|
|
22
|
-
* @param options - Prompt configuration with cacheable context
|
|
23
|
-
* @returns The generated text response
|
|
24
|
-
*/
|
|
25
|
-
async run(options) {
|
|
26
|
-
const { systemPrompt, cacheableContext = [], userPrompt } = options;
|
|
27
|
-
// Build system message with cache breakpoints
|
|
28
|
-
const systemContent = [];
|
|
29
|
-
if (systemPrompt) {
|
|
30
|
-
systemContent.push({
|
|
31
|
-
type: "text",
|
|
32
|
-
text: systemPrompt,
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
// Add each cacheable context block with cache_control
|
|
36
|
-
for (let i = 0; i < cacheableContext.length; i++) {
|
|
37
|
-
const isLast = i === cacheableContext.length - 1;
|
|
38
|
-
systemContent.push({
|
|
39
|
-
type: "text",
|
|
40
|
-
text: cacheableContext[i],
|
|
41
|
-
// Only the last block gets the cache breakpoint
|
|
42
|
-
...(isLast && {
|
|
43
|
-
cache_control: { type: "ephemeral" },
|
|
44
|
-
}),
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
const response = await this.client.messages.create({
|
|
48
|
-
model: this.model,
|
|
49
|
-
max_tokens: 8000,
|
|
50
|
-
system: systemContent,
|
|
51
|
-
messages: [
|
|
52
|
-
{
|
|
53
|
-
role: "user",
|
|
54
|
-
content: userPrompt,
|
|
55
|
-
},
|
|
56
|
-
],
|
|
57
|
-
});
|
|
58
|
-
// Extract text from response
|
|
59
|
-
const textBlocks = response.content.filter((block) => block.type === "text");
|
|
60
|
-
return textBlocks.map((block) => block.text).join("\n");
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Simple run without caching (for short prompts)
|
|
64
|
-
*/
|
|
65
|
-
async runSimple(prompt) {
|
|
66
|
-
return this.run({
|
|
67
|
-
userPrompt: prompt,
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
package/dist/ai/claude-runner.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { DEFAULT_MODEL } from "../core/config.js";
|
|
3
|
-
export class ClaudeRunner {
|
|
4
|
-
projectPath;
|
|
5
|
-
model;
|
|
6
|
-
constructor(projectPath, model = DEFAULT_MODEL) {
|
|
7
|
-
this.projectPath = projectPath;
|
|
8
|
-
this.model = model;
|
|
9
|
-
}
|
|
10
|
-
async run(prompt, _isPlanning = false) {
|
|
11
|
-
const maxRetries = 3;
|
|
12
|
-
let lastError = null;
|
|
13
|
-
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
14
|
-
try {
|
|
15
|
-
return await this.executeRun(prompt);
|
|
16
|
-
}
|
|
17
|
-
catch (error) {
|
|
18
|
-
const err = error;
|
|
19
|
-
lastError = err;
|
|
20
|
-
const isLastAttempt = attempt === maxRetries;
|
|
21
|
-
if (!isLastAttempt) {
|
|
22
|
-
const delay = Math.pow(2, attempt) * 1000; // 2s, 4s, 8s
|
|
23
|
-
console.warn(`Claude CLI attempt ${attempt} failed: ${err.message}. Retrying in ${delay}ms...`);
|
|
24
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
throw lastError || new Error("Claude CLI failed after multiple attempts");
|
|
29
|
-
}
|
|
30
|
-
executeRun(prompt) {
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
const args = [
|
|
33
|
-
"--dangerously-skip-permissions",
|
|
34
|
-
"--print",
|
|
35
|
-
"--model",
|
|
36
|
-
this.model,
|
|
37
|
-
];
|
|
38
|
-
const claude = spawn("claude", args, {
|
|
39
|
-
cwd: this.projectPath,
|
|
40
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
41
|
-
env: process.env,
|
|
42
|
-
shell: true,
|
|
43
|
-
});
|
|
44
|
-
let output = "";
|
|
45
|
-
let errorOutput = "";
|
|
46
|
-
claude.stdout.on("data", (data) => {
|
|
47
|
-
output += data.toString();
|
|
48
|
-
// Only write to stdout if we're not retrying or if logic dictates
|
|
49
|
-
// process.stdout.write(data.toString());
|
|
50
|
-
});
|
|
51
|
-
claude.stderr.on("data", (data) => {
|
|
52
|
-
errorOutput += data.toString();
|
|
53
|
-
// process.stderr.write(data.toString());
|
|
54
|
-
});
|
|
55
|
-
claude.on("error", (err) => reject(new Error(`Failed to start Claude CLI (shell: true): ${err.message}. Please ensure the 'claude' command is available in your PATH.`)));
|
|
56
|
-
claude.on("close", (code) => {
|
|
57
|
-
if (code === 0)
|
|
58
|
-
resolve(output);
|
|
59
|
-
else {
|
|
60
|
-
const detail = errorOutput.trim();
|
|
61
|
-
const message = detail
|
|
62
|
-
? `Claude CLI error (exit code ${code}): ${detail}`
|
|
63
|
-
: `Claude CLI exited with code ${code}. Please ensure the Claude CLI is installed and you are logged in (run 'claude' manually to check).`;
|
|
64
|
-
reject(new Error(message));
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
claude.stdin.write(prompt);
|
|
68
|
-
claude.stdin.end();
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
package/dist/ai/index.js
DELETED
package/dist/core/config.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
export const DEFAULT_MODEL = "sonnet";
|
|
3
|
-
export const LOCUS_CONFIG = {
|
|
4
|
-
dir: ".locus",
|
|
5
|
-
configFile: "config.json",
|
|
6
|
-
indexFile: "codebase-index.json",
|
|
7
|
-
contextFile: "CLAUDE.md",
|
|
8
|
-
artifactsDir: "artifacts",
|
|
9
|
-
};
|
|
10
|
-
export function getLocusPath(projectPath, fileName) {
|
|
11
|
-
if (fileName === "contextFile") {
|
|
12
|
-
return join(projectPath, LOCUS_CONFIG.contextFile);
|
|
13
|
-
}
|
|
14
|
-
return join(projectPath, LOCUS_CONFIG.dir, LOCUS_CONFIG[fileName]);
|
|
15
|
-
}
|