forked from strands-agents/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-input.cjs
More file actions
164 lines (138 loc) · 5.98 KB
/
process-input.cjs
File metadata and controls
164 lines (138 loc) · 5.98 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
// This file assumes that its run from an environment that already has github and core imported:
// const github = require('@actions/github');
// const core = require('@actions/core');
const fs = require('fs');
async function getIssueInfo(github, context, inputs) {
const issueId = context.eventName === 'workflow_dispatch'
? inputs.issue_id
: context.payload.issue.number.toString();
const command = context.eventName === 'workflow_dispatch'
? inputs.command
: (context.payload.comment.body.match(/^\/strands\s*(.*?)$/m)?.[1]?.trim() || '');
console.log(`Event: ${context.eventName}, Issue ID: ${issueId}, Command: "${command}"`);
const issue = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueId
});
return { issueId, command, issue };
}
async function determineBranch(github, context, issueId, mode, isPullRequest) {
let branchName = 'main';
let headRepo = null;
if (mode === 'implementer' && !isPullRequest) {
branchName = `agent-tasks/${issueId}`;
const mainRef = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'heads/main'
});
try {
console.log("Implementer started on an issue, attempting to create a branch for implementation.")
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/heads/${branchName}`,
sha: mainRef.data.object.sha
});
console.log(`Created branch ${branchName}`);
} catch (error) {
console.log(`Error message: ${String(error)}`)
console.log(`Error JSON: ${JSON.stringify(error)}`)
if (error.message?.includes('already exists')) {
console.log(`Branch ${branchName} already exists`);
} else {
console.error("Unable to create branch. Make sure you have given this job step `content: write` permission.")
throw error;
}
}
} else if (isPullRequest) {
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: issueId
});
branchName = pr.data.head.ref;
// Check if PR is from a fork
const baseRepo = `${context.repo.owner}/${context.repo.repo}`;
const prHeadRepo = pr.data.head.repo?.full_name;
if (prHeadRepo && prHeadRepo !== baseRepo) {
headRepo = prHeadRepo;
console.log(`Detected fork PR from ${headRepo}`);
}
}
return { branchName, headRepo };
}
function buildPrompts(mode, issueId, isPullRequest, command, branchName, inputs) {
const sessionId = inputs.session_id || (mode === 'implementer'
? `${mode}-${branchName}`.replace(/[\/\\]/g, '-')
: `${mode}-${issueId}`);
const scriptFiles = {
'implementer': 'devtools/strands-command/agent-sops/task-implementer.sop.md',
'refiner': 'devtools/strands-command/agent-sops/task-refiner.sop.md',
'release-notes': 'devtools/strands-command/agent-sops/task-release-notes.sop.md',
'reviewer': 'devtools/strands-command/agent-sops/task-reviewer.sop.md',
'adversarial-test': 'devtools/strands-command/agent-sops/task-adversarial-tester.sop.md',
'release-digest': 'devtools/strands-command/agent-sops/task-release-digest.sop.md'
};
const scriptFile = scriptFiles[mode] || scriptFiles['refiner'];
const systemPrompt = fs.readFileSync(scriptFile, 'utf8');
let prompt;
if (mode === 'release-digest') {
prompt = `Run the weekly release digest for this repository.\n${command}\nreview and continue`;
} else if (mode === 'adversarial-test') {
prompt = (isPullRequest)
? `Run adversarial testing on pull request #${issueId}.\n${command}\nreview and continue`
: `Run adversarial testing for the changes referenced in issue #${issueId}.\n${command}\nreview and continue`;
} else {
prompt = (isPullRequest)
? 'The pull request id is:'
: 'The issue id is:';
prompt += `${issueId}\n${command}\nreview and continue`;
}
return { sessionId, systemPrompt, prompt };
}
module.exports = async (context, github, core, inputs) => {
try {
const { issueId, command, issue } = await getIssueInfo(github, context, inputs);
const isPullRequest = !!issue.data.pull_request;
// Determine mode based on explicit command first, then context
let mode;
if (command.startsWith('release-digest') || command.startsWith('digest')) {
mode = 'release-digest';
} else if (command.startsWith('adversarial-test') || command.startsWith('adversarial')) {
mode = 'adversarial-test';
} else if (command.startsWith('release-notes') || command.startsWith('release notes')) {
mode = 'release-notes';
} else if (command.startsWith('implement')) {
mode = 'implementer';
} else if (command.startsWith('review')) {
mode = 'reviewer';
} else if (command.startsWith('refine')) {
mode = 'refiner';
} else {
// Default behavior when no explicit command: PR -> implementer, Issue -> refiner
mode = isPullRequest ? 'implementer' : 'refiner';
}
console.log(`Is PR: ${isPullRequest}, Command: "${command}", Mode: ${mode}`);
const { branchName, headRepo } = await determineBranch(github, context, issueId, mode, isPullRequest);
console.log(`Building prompts - mode: ${mode}, issue: ${issueId}, is PR: ${isPullRequest}`);
const { sessionId, systemPrompt, prompt } = buildPrompts(mode, issueId, isPullRequest, command, branchName, inputs);
console.log(`Session ID: ${sessionId}`);
console.log(`Task prompt: "${prompt}"`);
const outputs = {
branch_name: branchName,
session_id: sessionId,
system_prompt: systemPrompt,
prompt: prompt,
issue_id: issueId,
head_repo: headRepo
};
fs.writeFileSync('strands-parsed-input.json', JSON.stringify(outputs, null, 2));
console.log('Wrote strands-parsed-input.json');
} catch (error) {
const errorMsg = `Failed: ${error.message}`;
console.error(errorMsg);
core.setFailed(errorMsg);
}
};