-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.ts
More file actions
177 lines (157 loc) · 5.15 KB
/
index.ts
File metadata and controls
177 lines (157 loc) · 5.15 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bun
/**
* Augment Agent GitHub Action
* Main entry point for the action
*/
import { spawn, SpawnOptions } from 'child_process';
import process from 'process';
import { ValidationUtils } from './utils/validation.js';
import { TemplateProcessor } from './template/template-processor.js';
import { logger } from './utils/logger.js';
import { ActionInputs } from './types/inputs.js';
/**
* Execute a shell command and return a promise
*/
function execCommand(
command: string,
args: string[] = [],
options: SpawnOptions = {}
): Promise<number> {
return new Promise((resolve, reject) => {
// Join command and args into a single shell command for proper quoting
const fullCommand = `${command} ${args
.map(arg => {
// Properly quote arguments that contain spaces or special characters
if (arg.includes(' ') || arg.includes('"') || arg.includes("'")) {
return `"${arg.replace(/"/g, '\\"')}"`;
}
return arg;
})
.join(' ')}`;
const child = spawn(fullCommand, [], {
stdio: 'inherit',
shell: true,
...options,
});
child.on('close', code => {
if (code === 0) {
resolve(code);
} else {
reject(new Error(`Command failed with exit code ${code}`));
}
});
child.on('error', error => {
reject(error);
});
});
}
/**
* Set up environment variables for the augment script
*/
function setupEnvironment(inputs: ActionInputs): void {
// Set authentication environment variables
if (inputs.augmentSessionAuth) {
// Use session authentication
process.env.AUGMENT_SESSION_AUTH = inputs.augmentSessionAuth;
} else {
// Use token + URL authentication
process.env.AUGMENT_API_TOKEN = inputs.augmentApiToken;
process.env.AUGMENT_API_URL = inputs.augmentApiUrl;
}
// Set GitHub token if provided
if (inputs.githubToken) {
process.env.GITHUB_API_TOKEN = inputs.githubToken;
}
}
/**
* Process templates and generate instruction file
*/
async function processTemplate(inputs: ActionInputs): Promise<string> {
logger.info('Preparing template', {
templateDirectory: inputs.templateDirectory,
templateName: inputs.templateName,
});
// Create and run template processor
const processor = TemplateProcessor.create(inputs);
const instructionFilePath = await processor.processTemplate(inputs);
logger.info('Template processing completed', {
instructionFile: instructionFilePath,
});
return instructionFilePath;
}
/**
* Run the augment script with appropriate arguments
*/
async function runAugmentScript(inputs: ActionInputs): Promise<void> {
let instruction_value: string;
let is_file: boolean;
if (inputs.instruction) {
instruction_value = inputs.instruction;
is_file = false;
logger.debug('Using direct instruction', { instruction: inputs.instruction });
} else if (inputs.instructionFile) {
instruction_value = inputs.instructionFile;
is_file = true;
logger.debug('Using instruction file', { instructionFile: inputs.instructionFile });
} else {
instruction_value = await processTemplate(inputs);
is_file = true;
logger.debug('Using template-generated instruction file', {
instructionFile: instruction_value,
});
}
const args = ['--print'];
if (inputs.model && inputs.model.trim().length > 0) {
args.push('--model', inputs.model.trim());
}
if (is_file) {
logger.info(`📄 Using instruction file: ${instruction_value}`);
args.push('--instruction-file', instruction_value);
} else {
logger.info('📝 Using direct instruction');
args.push('--instruction', instruction_value);
}
const uniqueRules = Array.from(new Set(inputs.rules ?? [])).filter(rule => rule.length > 0);
if (uniqueRules.length > 0) {
logger.info(`🔧 Applying ${uniqueRules.length} rule file(s)`);
for (const rulePath of uniqueRules) {
logger.info(` - ${rulePath}`);
args.push('--rules', rulePath);
}
}
const uniqueMcpConfigs = Array.from(new Set(inputs.mcpConfigs ?? [])).filter(
config => config.length > 0
);
if (uniqueMcpConfigs.length > 0) {
logger.info(`🧩 Applying ${uniqueMcpConfigs.length} MCP config file(s)`);
for (const configPath of uniqueMcpConfigs) {
logger.info(` - ${configPath}`);
args.push('--mcp-config', configPath);
}
}
await execCommand('auggie', args);
logger.info('✅ Augment Agent completed successfully');
}
/**
* Main function
*/
async function main(): Promise<void> {
try {
logger.info('🔍 Validating inputs...');
const inputs = ValidationUtils.validateInputs();
logger.info('⚙️ Setting up environment...');
setupEnvironment(inputs);
logger.info('🚀 Starting Augment Agent...');
await runAugmentScript(inputs);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.setFailed(errorMessage);
}
}
// Run the action only if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(error => {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.setFailed(`Unexpected error: ${errorMessage}`);
});
}