yuanflow-cli 0.1.0 → 0.1.1

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.
Files changed (39) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +34 -50
  3. package/bin/yuanflow-cli.js +26 -0
  4. package/bin/yuanflow-skill.cjs +334 -0
  5. package/generated/registry.json +24641 -0
  6. package/lib/skill-installer/agents.cjs +203 -0
  7. package/lib/skill-installer/discover-skills.cjs +79 -0
  8. package/lib/skill-installer/installer.cjs +300 -0
  9. package/lib/skill-installer/publish.cjs +53 -0
  10. package/lib/skill-installer/repo-source.cjs +157 -0
  11. package/package.json +56 -6
  12. package/scripts/generate-registry.js +174 -0
  13. package/src/agent-protocol.js +169 -0
  14. package/src/cli.js +382 -0
  15. package/src/config.js +31 -0
  16. package/src/registry.js +45 -0
  17. package/src/request.js +97 -0
  18. package/src/shortcuts.js +346 -0
  19. package/cli.js +0 -3
  20. package/src/ycloud/cli.js +0 -29
  21. package/src/ycloud/commands/analysis.js +0 -82
  22. package/src/ycloud/commands/auth.js +0 -191
  23. package/src/ycloud/commands/commands.js +0 -262
  24. package/src/ycloud/commands/compliance.js +0 -146
  25. package/src/ycloud/commands/config.js +0 -103
  26. package/src/ycloud/commands/health.js +0 -35
  27. package/src/ycloud/commands/index.js +0 -381
  28. package/src/ycloud/commands/kb.js +0 -82
  29. package/src/ycloud/commands/schema.js +0 -229
  30. package/src/ycloud/commands/shared.js +0 -30
  31. package/src/ycloud/commands/tool-registry.js +0 -209
  32. package/src/ycloud/commands/tool-runner.js +0 -226
  33. package/src/ycloud/commands/tool.js +0 -178
  34. package/src/ycloud/commands/version.js +0 -84
  35. package/src/ycloud/core/config.js +0 -78
  36. package/src/ycloud/core/http.js +0 -133
  37. package/src/ycloud/core/token.js +0 -30
  38. package/src/ycloud/resources/.gitkeep +0 -1
  39. package/src/ycloud/resources/tool_catalog_full.json +0 -1
@@ -1,381 +0,0 @@
1
- import {
2
- executeAuthCharges,
3
- executeAuthInfo,
4
- executeAuthStatus,
5
- executeAuthValidate,
6
- getAuthHelp,
7
- } from "./auth.js";
8
- import {
9
- executeAnalysisCreatorProfile,
10
- getAnalysisHelp,
11
- } from "./analysis.js";
12
- import {
13
- executeComplianceCheck,
14
- getComplianceHelp,
15
- } from "./compliance.js";
16
- import {
17
- buildExposedCommands,
18
- executeCommandsDescribe,
19
- executeCommandsList,
20
- getCommandsHelp,
21
- } from "./commands.js";
22
- import {
23
- executeConfigClearToken,
24
- executeConfigGet,
25
- executeConfigSetToken,
26
- getConfigHelp,
27
- } from "./config.js";
28
- import { executeHealthCheck, getHealthHelp } from "./health.js";
29
- import { executeKbSearch, getKbHelp } from "./kb.js";
30
- import { executeSchema, getSchemaHelp } from "./schema.js";
31
- import { buildToolRegistry } from "./tool-registry.js";
32
- import {
33
- createDomainHelp,
34
- createToolCommandHelp,
35
- executeRegisteredTool,
36
- } from "./tool-runner.js";
37
- import { executeToolCatalog, executeToolExecute, getToolHelp } from "./tool.js";
38
- import { executeVersionCheck, getVersionHelp } from "./version.js";
39
-
40
- const TOP_LEVEL_HELP = `YuanFlow CLI local command tool for Agent-friendly cloud API access.
41
-
42
- USAGE:
43
- ycloud <command> [subcommand] [flags]
44
-
45
- EXAMPLES:
46
- ycloud auth validate --output json
47
- ycloud tool catalog --output json
48
- ycloud kb search --query "短视频脚本结构" --top-k 5 --output json
49
-
50
- GLOBAL FLAGS:
51
- --output <string> output format: json (default) | text
52
- --trace-id <string> optional request trace id
53
- --timeout <int> request timeout in milliseconds
54
- --base-url <string> override API base URL
55
- -h, --help show help
56
-
57
- COMMANDS:
58
- auth token validation and account info
59
- health service health check
60
- version local CLI version info
61
- tool tool catalog and tool execution
62
- kb knowledge base search
63
- analysis creator analysis commands
64
- compliance compliance checking commands
65
- config local CLI config management
66
- commands list and describe exposed commands
67
- schema inspect command parameter schema from OpenAPI
68
- content wrapped content generation tools
69
- social social media atomic tools
70
- oss oss helper tools
71
- speech speech helper tools
72
- `;
73
-
74
- function parseArgs(rawArgs) {
75
- const tokens = [...rawArgs];
76
- const options = {
77
- output: "json",
78
- traceId: undefined,
79
- timeout: undefined,
80
- baseUrl: undefined,
81
- help: false,
82
- };
83
- const positional = [];
84
-
85
- for (let index = 0; index < tokens.length; index += 1) {
86
- const token = tokens[index];
87
- if (token === "-h" || token === "--help") {
88
- options.help = true;
89
- continue;
90
- }
91
- if (token === "--output") {
92
- options.output = tokens[index + 1] || "json";
93
- index += 1;
94
- continue;
95
- }
96
- if (token === "--trace-id") {
97
- options.traceId = tokens[index + 1];
98
- index += 1;
99
- continue;
100
- }
101
- if (token === "--timeout") {
102
- options.timeout = tokens[index + 1];
103
- index += 1;
104
- continue;
105
- }
106
- if (token === "--base-url") {
107
- options.baseUrl = tokens[index + 1];
108
- index += 1;
109
- continue;
110
- }
111
- positional.push(token);
112
- }
113
-
114
- return { positional, options };
115
- }
116
-
117
- function getCommandName(positional) {
118
- return `${positional[0] || ""} ${positional[1] || ""}`.trim() || "unknown";
119
- }
120
-
121
- function asJson(command, success, data, error = null, meta = {}, warnings = []) {
122
- return {
123
- success,
124
- command,
125
- data,
126
- warnings,
127
- error,
128
- meta,
129
- };
130
- }
131
-
132
- function renderJson(payload) {
133
- return `${JSON.stringify(payload)}\n`;
134
- }
135
-
136
- function renderTextPayload(payload) {
137
- const lines = [
138
- `command: ${payload.command}`,
139
- `success: ${payload.success}`,
140
- ];
141
-
142
- if (payload.error) {
143
- lines.push(`error.code: ${payload.error.code}`);
144
- lines.push(`error.message: ${payload.error.message}`);
145
- lines.push(`error.retryable: ${payload.error.retryable}`);
146
- }
147
-
148
- if (payload.data !== null && payload.data !== undefined) {
149
- lines.push("data:");
150
- lines.push(JSON.stringify(payload.data, null, 2));
151
- }
152
-
153
- if (payload.meta && Object.keys(payload.meta).length > 0) {
154
- lines.push("meta:");
155
- lines.push(JSON.stringify(payload.meta, null, 2));
156
- }
157
-
158
- return `${lines.join("\n")}\n`;
159
- }
160
-
161
- function renderOutput(payload, globalOptions) {
162
- if (globalOptions.output === "text") {
163
- return renderTextPayload(payload);
164
- }
165
- return renderJson(payload);
166
- }
167
-
168
- function fail(command, code, message, exitCode = 1, retryable = false) {
169
- const payload = asJson(command, false, null, { code, message, retryable }, {});
170
- return {
171
- code: exitCode,
172
- output: renderOutput(payload, this?.globalOptions || { output: "json" }),
173
- };
174
- }
175
-
176
- function ok(command, data, meta = {}) {
177
- const payload = asJson(command, true, data, null, meta);
178
- return {
179
- code: 0,
180
- output: renderOutput(payload, this?.globalOptions || { output: "json" }),
181
- };
182
- }
183
-
184
- function text(commandHelp) {
185
- return {
186
- code: 0,
187
- output: `${commandHelp}\n`,
188
- };
189
- }
190
-
191
- function parseCommandFlags(tokens) {
192
- const options = {};
193
- for (let index = 0; index < tokens.length; index += 1) {
194
- const token = tokens[index];
195
- if (!token.startsWith("--")) {
196
- continue;
197
- }
198
- const name = token.slice(2);
199
- const next = tokens[index + 1];
200
- if (!next || next.startsWith("--")) {
201
- options[name] = true;
202
- continue;
203
- }
204
- options[name] = next;
205
- index += 1;
206
- }
207
- return options;
208
- }
209
-
210
- let cachedToolCatalogMap = null;
211
- let cachedToolRegistry = null;
212
-
213
- async function getToolCatalogMap() {
214
- if (cachedToolCatalogMap) {
215
- return cachedToolCatalogMap;
216
- }
217
- const { readFile } = await import("node:fs/promises");
218
- const { join } = await import("node:path");
219
- const { fileURLToPath } = await import("node:url");
220
- const currentDir = fileURLToPath(new URL(".", import.meta.url));
221
- const jsonText = await readFile(
222
- join(currentDir, "..", "resources", "tool_catalog_full.json"),
223
- "utf8",
224
- );
225
- const raw = JSON.parse(jsonText);
226
- const items = raw.data?.data?.items || [];
227
- cachedToolCatalogMap = new Map(items.map((item) => [item.tool_id, item]));
228
- return cachedToolCatalogMap;
229
- }
230
-
231
- async function getToolRegistry() {
232
- if (cachedToolRegistry) {
233
- return cachedToolRegistry;
234
- }
235
- const catalogMap = await getToolCatalogMap();
236
- cachedToolRegistry = buildToolRegistry(catalogMap);
237
- return cachedToolRegistry;
238
- }
239
-
240
- function buildContext(globalOptions, commandName) {
241
- return {
242
- globalOptions,
243
- commandName,
244
- ok: ok.bind({ globalOptions }),
245
- fail: fail.bind({ globalOptions }),
246
- parseCommandFlags,
247
- };
248
- }
249
-
250
- export async function executeCommand(rawArgs) {
251
- const { positional, options } = parseArgs(rawArgs);
252
- const commandName = getCommandName(positional);
253
- const context = buildContext(options, commandName);
254
- const toolRegistry = await getToolRegistry();
255
- const catalogMap = await getToolCatalogMap();
256
- const exposedCommands = buildExposedCommands(toolRegistry, catalogMap);
257
- const allowedToolIds = new Set(
258
- exposedCommands
259
- .filter((item) => item.kind === "tool")
260
- .map((item) => item.tool_id),
261
- );
262
-
263
- if (positional.length === 0 || options.help) {
264
- if (positional.length === 0) {
265
- return text(TOP_LEVEL_HELP);
266
- }
267
- if (positional[0] === "auth") {
268
- return text(getAuthHelp(positional[1]));
269
- }
270
- if (positional[0] === "health") {
271
- return text(getHealthHelp(positional[1]));
272
- }
273
- if (positional[0] === "version") {
274
- return text(getVersionHelp(positional[1]));
275
- }
276
- if (positional[0] === "tool") {
277
- return text(getToolHelp(positional[1]));
278
- }
279
- if (positional[0] === "kb") {
280
- return text(getKbHelp(positional[1]));
281
- }
282
- if (positional[0] === "analysis") {
283
- return text(getAnalysisHelp(positional[1]));
284
- }
285
- if (positional[0] === "compliance") {
286
- return text(getComplianceHelp(positional[1]));
287
- }
288
- if (positional[0] === "config") {
289
- return text(getConfigHelp(positional[1]));
290
- }
291
- if (positional[0] === "commands") {
292
- return text(getCommandsHelp(positional[1]));
293
- }
294
- if (positional[0] === "schema") {
295
- return text(getSchemaHelp());
296
- }
297
- if (toolRegistry[positional[0]]) {
298
- if (positional[1]) {
299
- const config = toolRegistry[positional[0]].commands[positional[1]];
300
- if (config) {
301
- return text(
302
- createToolCommandHelp(
303
- positional[0],
304
- positional[1],
305
- config,
306
- catalogMap.get(config.toolId),
307
- ),
308
- );
309
- }
310
- }
311
- return text(createDomainHelp(positional[0], toolRegistry[positional[0]], catalogMap));
312
- }
313
- return text(TOP_LEVEL_HELP);
314
- }
315
-
316
- if (positional[0] === "auth" && positional[1] === "info") {
317
- return executeAuthInfo(context, positional.slice(2));
318
- }
319
- if (positional[0] === "auth" && positional[1] === "validate") {
320
- return executeAuthValidate(context, positional.slice(2));
321
- }
322
- if (positional[0] === "auth" && positional[1] === "charges") {
323
- return executeAuthCharges(context, positional.slice(2));
324
- }
325
- if (positional[0] === "auth" && positional[1] === "status") {
326
- return executeAuthStatus(context, positional.slice(2));
327
- }
328
- if (positional[0] === "health" && positional[1] === "check") {
329
- return executeHealthCheck(context, positional.slice(2));
330
- }
331
- if (positional[0] === "version" && positional[1] === "check") {
332
- return executeVersionCheck(context, positional.slice(2));
333
- }
334
- if (positional[0] === "tool" && positional[1] === "catalog") {
335
- return executeToolCatalog(context, positional.slice(2));
336
- }
337
- if (positional[0] === "tool" && positional[1] === "execute") {
338
- return executeToolExecute(context, positional.slice(2), allowedToolIds);
339
- }
340
- if (positional[0] === "kb" && positional[1] === "search") {
341
- return executeKbSearch(context, positional.slice(2));
342
- }
343
- if (
344
- positional[0] === "analysis" &&
345
- positional[1] === "creator-profile"
346
- ) {
347
- return executeAnalysisCreatorProfile(context, positional.slice(2));
348
- }
349
- if (positional[0] === "compliance" && positional[1] === "check") {
350
- return executeComplianceCheck(context, positional.slice(2));
351
- }
352
- if (positional[0] === "config" && positional[1] === "set-token") {
353
- return executeConfigSetToken(context, positional.slice(2));
354
- }
355
- if (positional[0] === "config" && positional[1] === "get") {
356
- return executeConfigGet(context, positional.slice(2));
357
- }
358
- if (positional[0] === "config" && positional[1] === "clear-token") {
359
- return executeConfigClearToken(context, positional.slice(2));
360
- }
361
- if (positional[0] === "commands" && positional[1] === "list") {
362
- return executeCommandsList(context, exposedCommands);
363
- }
364
- if (positional[0] === "commands" && positional[1] === "describe") {
365
- return executeCommandsDescribe(context, positional.slice(2), exposedCommands);
366
- }
367
- if (positional[0] === "schema") {
368
- return executeSchema(context, positional.slice(1));
369
- }
370
- if (toolRegistry[positional[0]]) {
371
- return executeRegisteredTool(
372
- context,
373
- positional.slice(1),
374
- positional[0],
375
- toolRegistry[positional[0]],
376
- catalogMap,
377
- );
378
- }
379
-
380
- return fail(commandName, "UNKNOWN_COMMAND", "未知命令", 1, false);
381
- }
@@ -1,82 +0,0 @@
1
- import { apiPost } from "../core/http.js";
2
- import { getToken } from "../core/token.js";
3
- import {
4
- getGlobalFlagsHelp,
5
- parseFloatNumber,
6
- parseInteger,
7
- } from "./shared.js";
8
-
9
- export function getKbHelp() {
10
- return `Search knowledge base content
11
-
12
- Usage:
13
- ycloud kb search [flags]
14
-
15
- Required Flags:
16
- --query string search keyword
17
-
18
- Optional Flags:
19
- --top-k int number of results to return (default 5)
20
- --min-score float minimum similarity score
21
- --course-name string course name filter
22
-
23
- ${getGlobalFlagsHelp()}
24
-
25
- Examples:
26
- ycloud kb search --query "短视频脚本结构" --top-k 5 --output json
27
-
28
- Read on success:
29
- data.hits
30
-
31
- Read on error:
32
- error.code
33
- error.message
34
- error.retryable`;
35
- }
36
-
37
- async function requireToken(context) {
38
- const token = await getToken();
39
- if (!token) {
40
- return context.fail(
41
- context.commandName,
42
- "TOKEN_MISSING",
43
- "缺少 YUANCHUANG_API_TOKEN",
44
- 3,
45
- false,
46
- );
47
- }
48
- return token;
49
- }
50
-
51
- export async function executeKbSearch(context, tokens) {
52
- const options = context.parseCommandFlags(tokens);
53
- if (!options.query) {
54
- return context.fail(
55
- "kb search",
56
- "BAD_ARGUMENT",
57
- "缺少必填参数 --query",
58
- 2,
59
- false,
60
- );
61
- }
62
-
63
- const token = await requireToken(context);
64
- if (typeof token !== "string") {
65
- return token;
66
- }
67
-
68
- const data = await apiPost("/v1/knowledge-base/search", {
69
- token,
70
- traceId: context.globalOptions.traceId,
71
- timeout: context.globalOptions.timeout,
72
- baseUrl: context.globalOptions.baseUrl,
73
- body: {
74
- query: options.query,
75
- top_k: parseInteger(options["top-k"]) ?? 5,
76
- min_score: parseFloatNumber(options["min-score"]),
77
- course_name: options["course-name"],
78
- },
79
- });
80
-
81
- return context.ok("kb search", data);
82
- }
@@ -1,229 +0,0 @@
1
- import { fetchOpenApiSpec } from "../core/http.js";
2
- import { getGlobalFlagsHelp } from "./shared.js";
3
-
4
- const COMMAND_SCHEMA_MAP = {
5
- "auth.validate": {
6
- command: "auth validate",
7
- method: "GET",
8
- path: "/v1/auth/validate",
9
- cliFlags: [],
10
- },
11
- "auth.info": {
12
- command: "auth info",
13
- method: "GET",
14
- path: "/v1/auth/info",
15
- cliFlags: [],
16
- },
17
- "auth.charges": {
18
- command: "auth charges",
19
- method: "GET",
20
- path: "/v1/auth/charges",
21
- cliFlags: [
22
- { flag: "--model-id", apiField: "model_id", required: false },
23
- { flag: "--charge-type", apiField: "charge_type", required: false },
24
- { flag: "--user-level", apiField: "user_level", required: false },
25
- { flag: "--start-at", apiField: "start_at", required: false },
26
- { flag: "--end-at", apiField: "end_at", required: false },
27
- { flag: "--limit", apiField: "limit", required: false },
28
- { flag: "--offset", apiField: "offset", required: false },
29
- ],
30
- },
31
- "health.check": {
32
- command: "health check",
33
- method: "GET",
34
- path: "/v1/health",
35
- cliFlags: [],
36
- },
37
- "tool.catalog": {
38
- command: "tool catalog",
39
- method: "GET",
40
- path: "/v1/tools/catalog",
41
- cliFlags: [],
42
- },
43
- "version.check": {
44
- command: "version check",
45
- method: "GET",
46
- path: "/v1/version/check",
47
- cliFlags: [
48
- { flag: "--app-id", apiField: "app_id", required: false },
49
- { flag: "--platform", apiField: "platform", required: false },
50
- { flag: "--channel", apiField: "channel", required: false },
51
- {
52
- flag: "--current-version",
53
- apiField: "current_version",
54
- required: false,
55
- },
56
- { flag: "--current-build", apiField: "current_build", required: false },
57
- ],
58
- },
59
- "tool.execute": {
60
- command: "tool execute",
61
- method: "POST",
62
- path: "/v1/tools/execute",
63
- cliFlags: [
64
- { flag: "--tool-id", apiField: "tool_id", required: true },
65
- { flag: "--params", apiField: "params", required: false },
66
- { flag: "--params-file", apiField: "params", required: false },
67
- { flag: "--session-id", apiField: "session_id", required: false },
68
- { flag: "--trace-id", apiField: "trace_id", required: false },
69
- {
70
- flag: "--idempotency-key",
71
- apiField: "idempotency_key",
72
- required: false,
73
- },
74
- ],
75
- },
76
- "kb.search": {
77
- command: "kb search",
78
- method: "POST",
79
- path: "/v1/knowledge-base/search",
80
- cliFlags: [
81
- { flag: "--query", apiField: "query", required: true },
82
- { flag: "--top-k", apiField: "top_k", required: false },
83
- { flag: "--min-score", apiField: "min_score", required: false },
84
- { flag: "--course-name", apiField: "course_name", required: false },
85
- ],
86
- },
87
- "analysis.creator-profile": {
88
- command: "analysis creator-profile",
89
- method: "POST",
90
- path: "/v1/analysis/douyin/creator-profile",
91
- cliFlags: [
92
- { flag: "--profile-url", apiField: "profile_url", required: true },
93
- { flag: "--video-limit", apiField: "video_limit", required: false },
94
- ],
95
- },
96
- "compliance.check": {
97
- command: "compliance check",
98
- method: "POST",
99
- path: "/v1/compliance/check",
100
- cliFlags: [
101
- { flag: "--content", apiField: "content", required: false },
102
- { flag: "--content-file", apiField: "content", required: false },
103
- { flag: "--project-id", apiField: "project_id", required: false },
104
- { flag: "--metadata", apiField: "metadata", required: false },
105
- { flag: "--metadata-file", apiField: "metadata", required: false },
106
- ],
107
- },
108
- };
109
-
110
- export function getSchemaHelp() {
111
- return `Inspect command parameter schema from OpenAPI
112
-
113
- Usage:
114
- ycloud schema <command> [flags]
115
-
116
- Required Flags:
117
- none
118
-
119
- Optional Flags:
120
- none
121
-
122
- ${getGlobalFlagsHelp()}
123
-
124
- Examples:
125
- ycloud schema list --output json
126
- ycloud schema kb.search --output json
127
- ycloud schema tool.execute --output text
128
-
129
- Read on success:
130
- data.command
131
- data.api.path
132
- data.api.request_body
133
-
134
- Read on error:
135
- error.code
136
- error.message
137
- error.retryable`;
138
- }
139
-
140
- function dereferenceSchema(spec, schema) {
141
- if (!schema) {
142
- return {};
143
- }
144
- if (schema.$ref) {
145
- const refName = schema.$ref.split("/").pop();
146
- return spec.components?.schemas?.[refName] || {};
147
- }
148
- return schema;
149
- }
150
-
151
- function normalizeProperties(properties = {}) {
152
- return Object.entries(properties).map(([name, value]) => ({
153
- name,
154
- type: value.type || value.$ref || "unknown",
155
- }));
156
- }
157
-
158
- export async function executeSchema(context, tokens) {
159
- const key = tokens[0];
160
- if (!key) {
161
- return context.fail(
162
- "schema",
163
- "BAD_ARGUMENT",
164
- "缺少命令标识,例如 kb.search",
165
- 2,
166
- false,
167
- );
168
- }
169
-
170
- if (key === "list") {
171
- return context.ok("schema list", {
172
- commands: Object.entries(COMMAND_SCHEMA_MAP).map(([itemKey, value]) => ({
173
- key: itemKey,
174
- command: value.command,
175
- method: value.method,
176
- path: value.path,
177
- })),
178
- });
179
- }
180
-
181
- const target = COMMAND_SCHEMA_MAP[key];
182
- if (!target) {
183
- return context.fail(
184
- "schema",
185
- "BAD_ARGUMENT",
186
- `不支持的 schema 命令: ${key}`,
187
- 2,
188
- false,
189
- );
190
- }
191
-
192
- const spec = await fetchOpenApiSpec({
193
- timeout: context.globalOptions.timeout,
194
- baseUrl: context.globalOptions.baseUrl,
195
- });
196
- const operation =
197
- spec.paths?.[target.path]?.[target.method.toLowerCase()] || {};
198
-
199
- let requestBody = {
200
- required: [],
201
- properties: [],
202
- };
203
-
204
- const bodySchema = dereferenceSchema(
205
- spec,
206
- operation.requestBody?.content?.["application/json"]?.schema,
207
- );
208
- if (bodySchema && Object.keys(bodySchema).length > 0) {
209
- requestBody = {
210
- required: bodySchema.required || [],
211
- properties: normalizeProperties(bodySchema.properties),
212
- };
213
- }
214
-
215
- const data = {
216
- command: target.command,
217
- cli: {
218
- flags: target.cliFlags,
219
- },
220
- api: {
221
- method: target.method,
222
- path: target.path,
223
- summary: operation.summary || null,
224
- request_body: requestBody,
225
- },
226
- };
227
-
228
- return context.ok(`schema ${key}`, data);
229
- }