Repository navigation
Expand file tree
/
Copy pathProjectAgentForm.tsx
More file actions
179 lines (178 loc) · 5.41 KB
/
Copy pathProjectAgentForm.tsx
File metadata and controls
179 lines (178 loc) · 5.41 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
import * as uiMessages from "./messages";
import { useState } from "react";
import type { Agent, DockState, Mutate, Project, Translate } from "./types";
import { Icon } from "./ui";
import ProviderIcon from "./ProviderIcon";
import WorkspaceDirectory from "./WorkspaceDirectory";
export default function ProjectAgentForm({
project,
state,
busy,
token,
demo,
mutate,
onAdded,
onClose,
onConfigure,
t,
}: {
project: Project;
state: DockState;
busy: boolean;
token: string;
demo: boolean;
mutate: Mutate;
onAdded: (agent: Agent) => void;
onClose: () => void;
onConfigure?: () => void;
t: Translate;
}) {
// Existing project-bound agents can also be reused after upgrading.
const sources = state.agents.filter((agent) => !agent.source_agent_id);
const [sourceID, setSourceID] = useState("");
const [name, setName] = useState("");
const [role, setRole] = useState("");
const [workspace, setWorkspace] = useState("");
const source = sources.find((agent) => agent.id === sourceID);
const environment = source?.environment_id ?? "local";
const needsDirectory =
!!source && environment !== (project.environment_id ?? "local");
return (
<section className="panel inset-form" id="project-agent-form">
<div className="panel-heading">
<h2>{t(...uiMessages.projectagentform_add_agent_to_project_34fdb2)}</h2>
<button
className="icon-button"
aria-label={t(
...uiMessages.projectagentform_cancel_adding_agent_fe4fef,
)}
onClick={onClose}
disabled={busy}
>
<Icon name="close" />
</button>
</div>
<form
onSubmit={async (event) => {
event.preventDefault();
if (!source || (needsDirectory && !workspace.trim())) return;
await mutate(
`/api/projects/${encodeURIComponent(project.id)}/agents`,
{
source_agent_id: source.id,
name: name.trim(),
role: role.trim(),
...(needsDirectory ? { workspace: workspace.trim() } : {}),
},
onAdded,
);
}}
>
<label>
{t(...uiMessages.projectagentform_use_agent_cec758)}
<select
required
value={sourceID}
onChange={(event) => {
const selected = sources.find(
(agent) => agent.id === event.target.value,
);
setSourceID(event.target.value);
setName(selected?.name ?? "");
setRole(selected?.role ?? "");
setWorkspace("");
}}
>
<option value="">
{t(
...uiMessages.projectagentform_choose_a_configured_agent_0b6742,
)}
</option>
{sources.map((agent) => (
<option key={agent.id} value={agent.id}>
{agent.name}
{agent.project_id
? ` · ${state.projects.find((p) => p.id === agent.project_id)?.name ?? ""}`
: ""}
</option>
))}
</select>
</label>
{source && (
<div className="project-agent-source">
<ProviderIcon provider={source.provider} />
{source.name}
</div>
)}
<label>
{t(...uiMessages.projectagentform_name_in_this_project_1b1ec4)}
<input
value={name}
onChange={(event) => setName(event.target.value)}
required
maxLength={100}
placeholder={t(
...uiMessages.projectagentform_for_example_cr_or_coding_62d484,
)}
/>
</label>
<label>
{t(...uiMessages.projectagentform_project_role_optional_c8d252)}
<textarea
value={role}
onChange={(event) => setRole(event.target.value)}
maxLength={4000}
rows={3}
placeholder={t(
...uiMessages.projectagentform_for_example_review_code_risks_and_test_covera_5b7264,
)}
/>
</label>
{needsDirectory && (
<WorkspaceDirectory
required
value={workspace}
onChange={setWorkspace}
environment={environment}
token={token}
t={t}
disabled={busy}
browseEnabled={
!demo &&
state.runtime.enabled &&
(environment === "local" ||
state.environments?.some(
(e) => e.id === environment && e.status === "connected",
) === true)
}
/>
)}
<div className="button-row project-agent-actions">
{onConfigure && (
<button
type="button"
className="secondary"
disabled={busy}
onClick={onConfigure}
>
{t(...uiMessages.projectagentform_configure_a_new_agent_3143bc)}
</button>
)}
<button
className="primary"
type="submit"
disabled={
busy ||
demo ||
!source ||
!name.trim() ||
(needsDirectory && !workspace.trim())
}
>
{t(...uiMessages.projectagentform_add_to_project_f76bee)}
</button>
</div>
</form>
</section>
);
}