@aivin-labs/cli 1.0.0
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/README.md +214 -0
- package/bin/assets/aivin-icon.png +0 -0
- package/bin/cli.mjs +1256 -0
- package/bin/lib/auth.mjs +355 -0
- package/bin/lib/browserAutomation.mjs +575 -0
- package/bin/lib/codegen.mjs +858 -0
- package/bin/lib/connectors.mjs +217 -0
- package/bin/lib/context.mjs +149 -0
- package/bin/lib/deploy.mjs +350 -0
- package/bin/lib/mcpProxy.mjs +380 -0
- package/bin/lib/mission.mjs +68 -0
- package/bin/lib/missionStream.mjs +656 -0
- package/bin/lib/pluginTrigger.mjs +535 -0
- package/bin/lib/scaffold.mjs +653 -0
- package/bin/lib/store.mjs +204 -0
- package/bin/lib/tasks.mjs +345 -0
- package/bin/lib/util.mjs +40 -0
- package/bin/lib/workspace.mjs +280 -0
- package/docs/CHANGELOG.md +8 -0
- package/docs/CLI.md +1079 -0
- package/docs/GETTING_STARTED.md +234 -0
- package/package.json +59 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import inquirer from 'inquirer';
|
|
4
|
+
import { parseApiKeyClient } from './auth.mjs';
|
|
5
|
+
import { browseResults } from './pluginTrigger.mjs';
|
|
6
|
+
|
|
7
|
+
// ── Missions, automation jobs, tasks, and workspace/project selection ─────────────────────────
|
|
8
|
+
//
|
|
9
|
+
// `aivin do` / `aivin do job` / `aivin task` are thin CLI wrappers over the same platform
|
|
10
|
+
// endpoints the web app's chat/automation/task UIs already use (`/agent/start-work`,
|
|
11
|
+
// `/automation/jobs/create`, `/task/create`) - not a new mechanism, just a terminal-native way to
|
|
12
|
+
// reach them with the already-saved API_KEY. All three default to your personal workspace when
|
|
13
|
+
// --workspace is omitted, since GET /workspace/list always returns the caller's Personal
|
|
14
|
+
// workspace first (see WorkspaceService.listWorkspace on the backend).
|
|
15
|
+
|
|
16
|
+
export function missionAuthHeaders() {
|
|
17
|
+
const apiKey = process.env.API_KEY;
|
|
18
|
+
if (!apiKey) {
|
|
19
|
+
console.log(chalk.yellow('⚠️ API_KEY not set - run `aivin login` first'));
|
|
20
|
+
}
|
|
21
|
+
// `x-client-slug` - required for any request hitting the API host directly (no browser
|
|
22
|
+
// Origin/Referer for TrustDomainService to map): checkRegisteredClient falls back to the Host
|
|
23
|
+
// header otherwise (e.g. "beta_api_aivin_vn"), which never matches a real registered_clients
|
|
24
|
+
// entry - not a missing-tenant error, just a gap between "FE calls via its own domain" (has an
|
|
25
|
+
// Origin) and "CLI/SDK calls the API host directly" (nothing to map but this header). Same fix
|
|
26
|
+
// as test-sdk's mintCap.ts applies for its own direct calls.
|
|
27
|
+
const clientSlug = apiKey ? parseApiKeyClient(apiKey) : undefined;
|
|
28
|
+
return {
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: `Bearer ${apiKey || 'dev-token'}`,
|
|
31
|
+
...(clientSlug ? { 'x-client-slug': clientSlug } : {}),
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function missionServerUrl() {
|
|
37
|
+
return process.env.AIVIN_BASE_URL || 'https://api.aivin.cloud';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function listWorkspaces(serverUrl, authHeaders) {
|
|
41
|
+
let res;
|
|
42
|
+
try {
|
|
43
|
+
res = await axios.get(`${serverUrl}/workspace/list`, authHeaders);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
// axios leaves `error.message` empty for some connection-level failures (e.g. ECONNREFUSED) -
|
|
46
|
+
// fall back to `error.code` so this never surfaces as a bare, content-free "❌ ".
|
|
47
|
+
const message = error.response?.data?.message || error.message || error.code || 'Unknown error';
|
|
48
|
+
throw new Error(`Couldn't reach ${serverUrl} to look up workspaces: ${message}`, { cause: error });
|
|
49
|
+
}
|
|
50
|
+
return Array.isArray(res.data) ? res.data : res.data?.items || [];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* workspaces[0] IS "your personal workspace" whenever --workspace is omitted - the backend always
|
|
55
|
+
* unshifts the caller's Personal workspace to the front of GET /workspace/list's response.
|
|
56
|
+
*/
|
|
57
|
+
export async function resolveWorkspace(serverUrl, authHeaders, explicitId) {
|
|
58
|
+
const workspaces = await listWorkspaces(serverUrl, authHeaders);
|
|
59
|
+
if (explicitId) {
|
|
60
|
+
const match = workspaces.find((w) => (w.id || w._id) === explicitId);
|
|
61
|
+
if (!match) throw new Error(`Workspace "${explicitId}" not found or not accessible.`);
|
|
62
|
+
return match;
|
|
63
|
+
}
|
|
64
|
+
const personal = workspaces[0];
|
|
65
|
+
if (!personal) throw new Error('No workspace found for this account. Pass --workspace <id>.');
|
|
66
|
+
return personal;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* A workspace's `agents` array always has its client's default AI Staff agent unshifted to the
|
|
71
|
+
* front (see WorkspaceService.assembleEnrichedWorkspace) - so agents[0] is a sensible default
|
|
72
|
+
* whenever --agent is omitted, same reasoning as resolveWorkspace's workspaces[0].
|
|
73
|
+
*/
|
|
74
|
+
export function resolveAgentId(workspace, explicitAgentId) {
|
|
75
|
+
if (explicitAgentId) return explicitAgentId;
|
|
76
|
+
const agentId = workspace.agents?.[0]?.id || workspace.agents?.[0]?.agent_id;
|
|
77
|
+
if (!agentId) {
|
|
78
|
+
throw new Error(`Workspace "${workspace.name || workspace.id}" has no agent to run as - pass --agent <id>.`);
|
|
79
|
+
}
|
|
80
|
+
return agentId;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ── AI Staff agents - marketplace search, install into a workspace, create, publish ────────────
|
|
84
|
+
|
|
85
|
+
export function formatAgentListLine(agent, isSelected) {
|
|
86
|
+
const marker = isSelected ? chalk.cyan('❯ ') : ' ';
|
|
87
|
+
const label = agent.nickname || agent.name || agent.id || agent.agent_id;
|
|
88
|
+
const name = isSelected ? chalk.bold.cyan(label) : label;
|
|
89
|
+
return `${marker}${name}${chalk.gray(` (${agent.id || agent.agent_id})`)}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function formatAgentDetail(agent) {
|
|
93
|
+
const lines = [];
|
|
94
|
+
const id = agent.id || agent.agent_id;
|
|
95
|
+
lines.push(chalk.bold.cyan(agent.nickname || agent.name || id));
|
|
96
|
+
lines.push(chalk.gray(id));
|
|
97
|
+
if (agent.name && agent.name !== agent.nickname) lines.push(`${chalk.gray('name')} ${agent.name}`);
|
|
98
|
+
if (agent.email) lines.push(`${chalk.gray('email')} ${agent.email}`);
|
|
99
|
+
if (agent.bio) lines.push(`\n${agent.bio}`);
|
|
100
|
+
const pluginIds = agent.plugins?.plugin_ids;
|
|
101
|
+
if (Array.isArray(pluginIds) && pluginIds.length > 0) {
|
|
102
|
+
lines.push(`\n${chalk.gray('plugins')} ${pluginIds.join(', ')}`);
|
|
103
|
+
}
|
|
104
|
+
lines.push(chalk.gray(`\nUse with: --agent ${id}`));
|
|
105
|
+
return lines.join('\n');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* `aivin agent list` - browse agents already INSTALLED in a workspace (not the marketplace, see
|
|
110
|
+
* `agent search` for that). Reuses the same ↑/↓ move, space/enter for details, q/esc to quit
|
|
111
|
+
* picker `plugin search`/`connector list` already use (`browseResults`, defined in
|
|
112
|
+
* pluginTrigger.mjs) instead of inventing a second picker style for the same "browse a list, open
|
|
113
|
+
* one item's detail" shape. Falls back to a flat print for scripts/CI or --plain, same as those.
|
|
114
|
+
*/
|
|
115
|
+
export async function listAgentsCmd(options) {
|
|
116
|
+
const serverUrl = missionServerUrl();
|
|
117
|
+
const authHeaders = missionAuthHeaders();
|
|
118
|
+
const workspace = await resolveWorkspace(serverUrl, authHeaders, options.workspace);
|
|
119
|
+
|
|
120
|
+
const agents = workspace.agents || [];
|
|
121
|
+
if (agents.length === 0) {
|
|
122
|
+
console.log(chalk.yellow(`No agents in workspace "${workspace.name || workspace.id}" - run \`aivin agent install\`/\`aivin agent make\` first.`));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (process.stdout.isTTY && process.stdin.isTTY && !options.plain) {
|
|
127
|
+
await browseResults(agents, `${agents.length} agent(s) in "${workspace.name || workspace.id}":\n`, formatAgentListLine, formatAgentDetail);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
console.log(chalk.blue(`${agents.length} agent(s) in "${workspace.name || workspace.id}":\n`));
|
|
132
|
+
agents.forEach((a) => {
|
|
133
|
+
console.log(chalk.bold(a.nickname || a.name || a.id) + chalk.gray(` (${a.id || a.agent_id})`));
|
|
134
|
+
if (a.bio) console.log(` ${a.bio}`);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function searchAgents(serverUrl, authHeaders, { query, workspaceId, limit } = {}) {
|
|
139
|
+
const params = {};
|
|
140
|
+
if (query) params.query = query;
|
|
141
|
+
if (workspaceId) params.workspace_id = workspaceId;
|
|
142
|
+
if (limit) params.limit = limit;
|
|
143
|
+
let response;
|
|
144
|
+
try {
|
|
145
|
+
response = await axios.get(`${serverUrl}/ai-staff/search`, { ...authHeaders, params });
|
|
146
|
+
} catch (error) {
|
|
147
|
+
const message = error.response?.data?.message || error.message || error.code || 'Unknown error';
|
|
148
|
+
throw new Error(`Agent search failed: ${message}`, { cause: error });
|
|
149
|
+
}
|
|
150
|
+
const data = response.data;
|
|
151
|
+
return Array.isArray(data) ? data : data?.items || [];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export async function pullAgentIntoWorkspace(serverUrl, authHeaders, { agentId, workspaceId }) {
|
|
155
|
+
try {
|
|
156
|
+
await axios.post(`${serverUrl}/ai-staff/pull`, { agent_id: agentId, workspace_id: workspaceId }, authHeaders);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
const message = error.response?.data?.message || error.message || error.code || 'Unknown error';
|
|
159
|
+
throw new Error(`Failed to install agent into workspace: ${message}`, { cause: error });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function createAgent(serverUrl, authHeaders, { name, nickname, email, bio, workspaceId }) {
|
|
164
|
+
let response;
|
|
165
|
+
try {
|
|
166
|
+
response = await axios.post(
|
|
167
|
+
`${serverUrl}/ai-staff/create`,
|
|
168
|
+
{ name, nickname, email, bio, original_workspace_id: workspaceId },
|
|
169
|
+
authHeaders,
|
|
170
|
+
);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
const message = error.response?.data?.message || error.message || error.code || 'Unknown error';
|
|
173
|
+
throw new Error(`Failed to create agent: ${message}`, { cause: error });
|
|
174
|
+
}
|
|
175
|
+
return response.data ?? {};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export async function publishAgent(serverUrl, authHeaders, { agentId, workspaceId }) {
|
|
179
|
+
try {
|
|
180
|
+
await axios.post(`${serverUrl}/ai-staff/update`, { id: agentId, workspace_id: workspaceId, is_published: true }, authHeaders);
|
|
181
|
+
await axios.post(`${serverUrl}/ai-staff/push`, { agent_id: agentId, workspace_id: workspaceId }, authHeaders);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
const message = error.response?.data?.message || error.message || error.code || 'Unknown error';
|
|
184
|
+
throw new Error(`Failed to publish agent: ${message}`, { cause: error });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Interactively resolves which agent `aivin do` should run as: matches `agentNickname` against
|
|
190
|
+
* the workspace's already-installed agents (by nickname/name/id) if given, otherwise prompts to
|
|
191
|
+
* pick one. If the nickname doesn't match anything, or the workspace has no agents at all yet,
|
|
192
|
+
* offers to search-and-install one from the marketplace or create a brand new one, so `aivin do`
|
|
193
|
+
* never dead-ends just because a workspace hasn't been set up with an agent yet.
|
|
194
|
+
*/
|
|
195
|
+
export async function resolveAgentInteractive(serverUrl, authHeaders, workspace, agentNickname) {
|
|
196
|
+
const agents = workspace.agents || [];
|
|
197
|
+
const workspaceId = workspace.id || workspace._id;
|
|
198
|
+
|
|
199
|
+
if (agentNickname) {
|
|
200
|
+
const match = agents.find((a) => a.nickname === agentNickname || a.id === agentNickname || a.name === agentNickname);
|
|
201
|
+
if (match) return match;
|
|
202
|
+
console.log(chalk.yellow(`No agent named "${agentNickname}" found in workspace "${workspace.name}".`));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!process.stdout.isTTY || !process.stdin.isTTY) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
agents.length > 0
|
|
208
|
+
? `Pass --agent <id> (one of: ${agents.map((a) => a.nickname || a.name || a.id).join(', ')}), or run this in an interactive terminal.`
|
|
209
|
+
: `Workspace "${workspace.name}" has no agents - run \`aivin agent install\`/\`aivin agent make\` first, or run this in an interactive terminal.`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const NEW_AGENT_ACTIONS = {
|
|
214
|
+
INSTALL: '__install__',
|
|
215
|
+
CREATE: '__create__',
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
if (agents.length === 0) {
|
|
219
|
+
console.log(chalk.gray(`Workspace "${workspace.name}" has no agents yet.`));
|
|
220
|
+
const { action } = await inquirer.prompt([
|
|
221
|
+
{
|
|
222
|
+
type: 'select',
|
|
223
|
+
name: 'action',
|
|
224
|
+
message: 'What would you like to do?',
|
|
225
|
+
choices: [
|
|
226
|
+
{ name: 'Search & install an agent from the marketplace', value: NEW_AGENT_ACTIONS.INSTALL },
|
|
227
|
+
{ name: 'Create a brand new agent', value: NEW_AGENT_ACTIONS.CREATE },
|
|
228
|
+
],
|
|
229
|
+
},
|
|
230
|
+
]);
|
|
231
|
+
return action === NEW_AGENT_ACTIONS.INSTALL
|
|
232
|
+
? installAgentInteractive(serverUrl, authHeaders, workspaceId)
|
|
233
|
+
: createAgentInteractive(serverUrl, authHeaders, workspaceId);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const choices = [
|
|
237
|
+
...agents.map((a) => ({ name: a.nickname || a.name || a.id, value: a.id || a.agent_id })),
|
|
238
|
+
{ name: chalk.gray('+ Search & install a different agent from the marketplace'), value: NEW_AGENT_ACTIONS.INSTALL },
|
|
239
|
+
{ name: chalk.gray('+ Create a brand new agent'), value: NEW_AGENT_ACTIONS.CREATE },
|
|
240
|
+
];
|
|
241
|
+
const { pickedAgentId } = await inquirer.prompt([
|
|
242
|
+
{ type: 'select', name: 'pickedAgentId', message: 'Which agent should run this?', choices },
|
|
243
|
+
]);
|
|
244
|
+
if (pickedAgentId === NEW_AGENT_ACTIONS.INSTALL) return installAgentInteractive(serverUrl, authHeaders, workspaceId);
|
|
245
|
+
if (pickedAgentId === NEW_AGENT_ACTIONS.CREATE) return createAgentInteractive(serverUrl, authHeaders, workspaceId);
|
|
246
|
+
return agents.find((a) => (a.id || a.agent_id) === pickedAgentId);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export async function installAgentInteractive(serverUrl, authHeaders, workspaceId) {
|
|
250
|
+
const { query } = await inquirer.prompt([
|
|
251
|
+
{ type: 'input', name: 'query', message: 'Search the marketplace for an agent:', validate: (v) => v.trim().length > 0 || 'Required' },
|
|
252
|
+
]);
|
|
253
|
+
const results = await searchAgents(serverUrl, authHeaders, { query, workspaceId, limit: 10 });
|
|
254
|
+
if (results.length === 0) {
|
|
255
|
+
throw new Error(`No marketplace agents found matching "${query}".`);
|
|
256
|
+
}
|
|
257
|
+
const { pickedAgent } = await inquirer.prompt([
|
|
258
|
+
{
|
|
259
|
+
type: 'select',
|
|
260
|
+
name: 'pickedAgent',
|
|
261
|
+
message: 'Install which agent?',
|
|
262
|
+
choices: results.map((a) => ({ name: `${a.nickname || a.name}${a.bio ? chalk.gray(` - ${a.bio}`) : ''}`, value: a })),
|
|
263
|
+
},
|
|
264
|
+
]);
|
|
265
|
+
await pullAgentIntoWorkspace(serverUrl, authHeaders, { agentId: pickedAgent.id, workspaceId });
|
|
266
|
+
console.log(chalk.green(`✅ Installed "${pickedAgent.nickname || pickedAgent.name}" into this workspace.`));
|
|
267
|
+
return pickedAgent;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function createAgentInteractive(serverUrl, authHeaders, workspaceId) {
|
|
271
|
+
const answers = await inquirer.prompt([
|
|
272
|
+
{ type: 'input', name: 'name', message: 'Agent name:', validate: (v) => v.trim().length > 0 || 'Required' },
|
|
273
|
+
{ type: 'input', name: 'nickname', message: 'Agent nickname (used to @-mention/target it):', validate: (v) => v.trim().length > 0 || 'Required' },
|
|
274
|
+
{ type: 'input', name: 'email', message: 'Agent email:', validate: (v) => v.trim().length > 0 || 'Required' },
|
|
275
|
+
{ type: 'input', name: 'bio', message: 'Short bio (optional):' },
|
|
276
|
+
]);
|
|
277
|
+
const agent = await createAgent(serverUrl, authHeaders, { ...answers, workspaceId });
|
|
278
|
+
console.log(chalk.green(`✅ Created agent "${agent.nickname || agent.name}".`));
|
|
279
|
+
return agent;
|
|
280
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
Initial release — split out of `@aivin-labs/sdk` (which shipped the `aivin` binary through
|
|
6
|
+
version 1.0.5) into its own package. Behavior is unchanged from that point; only the packaging
|
|
7
|
+
did. `@aivin-labs/sdk` from `1.1.0` onward is runtime-only and no longer provides the `aivin`
|
|
8
|
+
command — install this package instead.
|