-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path16c-credentials-cli-tools.ts
More file actions
190 lines (174 loc) · 6.3 KB
/
Copy path16c-credentials-cli-tools.ts
File metadata and controls
190 lines (174 loc) · 6.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Credentials -- CLI tools with explicit credential declarations.
*
* Demonstrates:
* - Explicit credentials on agents and tools
* - cliConfig.allowedCommands defines which CLI tools the agent can use
* - credentials: [...] declares which secrets the server must inject
* - Multi-credential tools (aws needs multiple env vars)
*
* Setup (one-time, via CLI):
* conductor config save
* conductor secret put GITHUB_TOKEN <your-github-token>
* conductor secret put AWS_ACCESS_KEY_ID <your-aws-access-key-id>
* conductor secret put AWS_SECRET_ACCESS_KEY <your-aws-secret-access-key>
*
* Requirements:
* - Conductor server running at CONDUCTOR_SERVER_URL
* - CONDUCTOR_AGENT_LLM_MODEL set (or defaults to openai/gpt-4o-mini)
* - gh and aws CLIs installed
*/
import { execSync } from 'node:child_process';
import { Agent, AgentRuntime, tool } from '@io-orkes/conductor-javascript/agents';
import { llmModel } from './settings';
// -- gh tool: list pull requests ----------------------------------------------
const ghListPrs = tool(
async (args: { repo: string; state?: string }) => {
const state = args.state ?? 'open';
const ghToken = process.env.GITHUB_TOKEN ?? '';
try {
const stdout = execSync(
`gh pr list --repo ${args.repo} --state ${state} --limit 10 --json number,title,author,createdAt,url`,
{
timeout: 15_000,
encoding: 'utf-8',
env: { ...process.env, GH_TOKEN: ghToken },
},
);
const prs = JSON.parse(stdout);
return { repo: args.repo, state, pull_requests: prs };
} catch (err) {
return { error: String(err) };
}
},
{
name: 'gh_list_prs',
description: 'List pull requests for a GitHub repo using the gh CLI. repo format: "owner/repo"',
inputSchema: {
type: 'object',
properties: {
repo: { type: 'string', description: 'Repository in "owner/repo" format' },
state: { type: 'string', description: '"open", "closed", or "all"' },
},
required: ['repo'],
},
credentials: ['GITHUB_TOKEN'],
},
);
// -- gh tool: create pull request ---------------------------------------------
const ghCreatePr = tool(
async (args: { repo: string; title: string; body: string; head: string; base?: string }) => {
const base = args.base ?? 'main';
const ghToken = process.env.GITHUB_TOKEN ?? '';
try {
const stdout = execSync(
`gh pr create --repo ${args.repo} --title "${args.title}" --body "${args.body}" --head ${args.head} --base ${base}`,
{
timeout: 15_000,
encoding: 'utf-8',
env: { ...process.env, GH_TOKEN: ghToken },
},
);
return { url: stdout.trim() };
} catch (err) {
return { error: String(err) };
}
},
{
name: 'gh_create_pr',
description: 'Create a pull request via the gh CLI.',
inputSchema: {
type: 'object',
properties: {
repo: { type: 'string', description: 'Repository in "owner/repo" format' },
title: { type: 'string', description: 'PR title' },
body: { type: 'string', description: 'PR body' },
head: { type: 'string', description: 'Source branch' },
base: { type: 'string', description: 'Target branch (default: main)' },
},
required: ['repo', 'title', 'body', 'head'],
},
credentials: ['GITHUB_TOKEN'],
},
);
// -- aws tool: list S3 buckets ------------------------------------------------
const awsListS3Buckets = tool(
async () => {
try {
const stdout = execSync('aws s3 ls --output json', {
timeout: 15_000,
encoding: 'utf-8',
});
const lines = stdout
.trim()
.split('\n')
.filter((l) => l.trim());
const buckets = lines.map((line) => {
const parts = line.trim().split(/\s+/);
return parts.length >= 3
? { created: `${parts[0]} ${parts[1]}`, name: parts[2] }
: { name: line.trim() };
});
return { buckets };
} catch (err) {
return { error: String(err) };
}
},
{
name: 'aws_list_s3_buckets',
description: "List S3 buckets accessible with the user's AWS credentials.",
inputSchema: { type: 'object', properties: {} },
credentials: ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN'],
},
);
// -- aws tool: get caller identity --------------------------------------------
const awsGetCallerIdentity = tool(
async () => {
try {
const stdout = execSync('aws sts get-caller-identity --output json', {
timeout: 10_000,
encoding: 'utf-8',
});
return JSON.parse(stdout);
} catch (err) {
return { error: String(err) };
}
},
{
name: 'aws_get_caller_identity',
description: 'Return the AWS identity (account, ARN) for the current credentials.',
inputSchema: { type: 'object', properties: {} },
credentials: ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN'],
},
);
// -- Agent with CLI allowed commands ------------------------------------------
export const githubAwsAgent = new Agent({
name: 'devops_agent',
model: llmModel,
tools: [ghListPrs, ghCreatePr, awsListS3Buckets, awsGetCallerIdentity],
cliConfig: { enabled: true, allowedCommands: ['gh', 'aws'] },
credentials: ['GITHUB_TOKEN', 'GH_TOKEN', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN'],
instructions:
'You are a DevOps assistant. You can manage GitHub pull requests and ' +
'inspect AWS resources. Always confirm destructive actions before proceeding.',
});
// -- Run ----------------------------------------------------------------------
const task = process.argv.slice(2).join(' ') || 'Who am I in AWS, and list my S3 buckets?';
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(githubAwsAgent, task);
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(githubAwsAgent);
// CLI alternative:
// conductor deploy --package examples/agents --agents devops_agent
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(githubAwsAgent);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);