@agentskit/cli 0.7.1 → 0.8.2

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/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ var prompts = require('@inquirer/prompts');
22
22
  var kleur = require('kleur');
23
23
  var chokidar = require('chokidar');
24
24
  var rag = require('@agentskit/rag');
25
+ var agentSchema = require('@agentskit/core/agent-schema');
25
26
 
26
27
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
27
28
 
@@ -2959,6 +2960,187 @@ function registerRagCommand(program) {
2959
2960
  });
2960
2961
  }
2961
2962
 
2963
+ // src/ai/scaffold.ts
2964
+ function snakeToCamel(name) {
2965
+ return name.replace(/[-_](\w)/g, (_, c) => c.toUpperCase());
2966
+ }
2967
+ function toolStub(tool) {
2968
+ const fnName = snakeToCamel(tool.name);
2969
+ const schema = tool.schema ? JSON.stringify(tool.schema, null, 2) : '{ type: "object", properties: {} }';
2970
+ return `import { defineTool } from '@agentskit/core'
2971
+
2972
+ /**
2973
+ * ${tool.description ?? tool.name}
2974
+ * ${tool.implementation ? `
2975
+ * Implementation hint: ${tool.implementation}` : ""}
2976
+ */
2977
+ export const ${fnName} = defineTool({
2978
+ name: '${tool.name}',
2979
+ description: ${JSON.stringify(tool.description ?? "")},
2980
+ schema: ${schema} as const,
2981
+ ${tool.requiresConfirmation ? "requiresConfirmation: true," : ""}
2982
+ async execute(args) {
2983
+ // TODO: implement ${tool.name}
2984
+ return { args }
2985
+ },
2986
+ })
2987
+ `;
2988
+ }
2989
+ function indexTs(schema) {
2990
+ const toolImports2 = (schema.tools ?? []).map((t) => `import { ${snakeToCamel(t.name)} } from './tools/${t.name}'`).join("\n");
2991
+ const toolList2 = (schema.tools ?? []).map((t) => snakeToCamel(t.name)).join(", ");
2992
+ return `import { createRuntime } from '@agentskit/runtime'
2993
+ ${toolImports2}
2994
+
2995
+ export const agent = {
2996
+ name: '${schema.name}',
2997
+ systemPrompt: ${JSON.stringify(schema.systemPrompt ?? "")},
2998
+ tools: [${toolList2}],
2999
+ }
3000
+
3001
+ export function createAgent(adapter: Parameters<typeof createRuntime>[0]['adapter']) {
3002
+ return createRuntime({
3003
+ adapter,
3004
+ systemPrompt: agent.systemPrompt,
3005
+ tools: agent.tools,
3006
+ })
3007
+ }
3008
+ `;
3009
+ }
3010
+ function readme(schema) {
3011
+ const lines = [];
3012
+ lines.push(`# ${schema.name}`);
3013
+ lines.push("");
3014
+ if (schema.description) lines.push(schema.description, "");
3015
+ lines.push(`## Model`);
3016
+ lines.push("");
3017
+ lines.push(`- Provider: \`${schema.model.provider}\``);
3018
+ if (schema.model.model) lines.push(`- Model: \`${schema.model.model}\``);
3019
+ lines.push("");
3020
+ if (schema.tools && schema.tools.length > 0) {
3021
+ lines.push("## Tools");
3022
+ lines.push("");
3023
+ for (const t of schema.tools) {
3024
+ lines.push(`- \`${t.name}\` \u2014 ${t.description ?? ""}`);
3025
+ }
3026
+ lines.push("");
3027
+ }
3028
+ if (schema.skills && schema.skills.length > 0) {
3029
+ lines.push("## Skills");
3030
+ lines.push("");
3031
+ for (const s of schema.skills) lines.push(`- ${s}`);
3032
+ lines.push("");
3033
+ }
3034
+ lines.push("## Generated by");
3035
+ lines.push("");
3036
+ lines.push('`npx agentskit ai "<description>"`');
3037
+ lines.push("");
3038
+ return lines.join("\n");
3039
+ }
3040
+ function scaffoldAgent(schema) {
3041
+ const files = [
3042
+ { path: "agent.json", content: JSON.stringify(schema, null, 2) + "\n" },
3043
+ { path: "agent.ts", content: indexTs(schema) },
3044
+ { path: "README.md", content: readme(schema) }
3045
+ ];
3046
+ for (const tool of schema.tools ?? []) {
3047
+ files.push({ path: `tools/${tool.name}.ts`, content: toolStub(tool) });
3048
+ }
3049
+ return files;
3050
+ }
3051
+ async function writeScaffold(files, outDir, opts = {}) {
3052
+ const { writeFile: writeFile2, mkdir: mkdir2, access } = await import('fs/promises');
3053
+ const { dirname, join: join5 } = await import('path');
3054
+ const written = [];
3055
+ for (const f of files) {
3056
+ const full = join5(outDir, f.path);
3057
+ await mkdir2(dirname(full), { recursive: true });
3058
+ if (!opts.overwrite) {
3059
+ try {
3060
+ await access(full);
3061
+ continue;
3062
+ } catch {
3063
+ }
3064
+ }
3065
+ await writeFile2(full, f.content, "utf8");
3066
+ written.push(f.path);
3067
+ }
3068
+ return written;
3069
+ }
3070
+ var PLANNER_SYSTEM_PROMPT = `You design AI agents for the AgentsKit framework.
3071
+ Given a user's plain-language description, return a single JSON object
3072
+ matching the AgentsKit AgentSchema type. Do not include commentary or
3073
+ markdown fences \u2014 emit raw JSON only.
3074
+
3075
+ Schema:
3076
+ {
3077
+ "name": "kebab-or-snake-case",
3078
+ "description": "short summary",
3079
+ "systemPrompt": "you are...",
3080
+ "model": { "provider": "anthropic"|"openai"|"gemini"|..., "model": "..." },
3081
+ "tools": [{ "name": "search", "description": "...", "schema": {...}, "implementation": "fetch('/api/search?q=...')" }],
3082
+ "memory": { "kind": "inMemory"|"localStorage", "key": "..." },
3083
+ "skills": ["researcher", "critic"]
3084
+ }
3085
+
3086
+ Prefer fewer well-scoped tools over many overlapping ones. Name tools
3087
+ in snake_case.`;
3088
+ function extractJson(text) {
3089
+ const fenced = text.match(/```(?:json)?\s*([\s\S]+?)```/);
3090
+ const raw = fenced?.[1] ?? text;
3091
+ const start = raw.indexOf("{");
3092
+ const end = raw.lastIndexOf("}");
3093
+ if (start < 0 || end <= start) throw new Error("Planner response did not contain a JSON object");
3094
+ return raw.slice(start, end + 1);
3095
+ }
3096
+ function createAdapterPlanner(adapter) {
3097
+ return async (description) => {
3098
+ const messages = [
3099
+ { id: "sys", role: "system", content: PLANNER_SYSTEM_PROMPT, status: "complete", createdAt: /* @__PURE__ */ new Date(0) },
3100
+ { id: "user", role: "user", content: description, status: "complete", createdAt: /* @__PURE__ */ new Date(0) }
3101
+ ];
3102
+ const request = { messages, context: { temperature: 0 } };
3103
+ const source = adapter.createSource(request);
3104
+ let text = "";
3105
+ for await (const chunk of source.stream()) {
3106
+ if (chunk.type === "text" && chunk.content) text += chunk.content;
3107
+ }
3108
+ const json = extractJson(text);
3109
+ return agentSchema.validateAgentSchema(JSON.parse(json));
3110
+ };
3111
+ }
3112
+
3113
+ // src/commands/ai.ts
3114
+ function registerAiCommand(program) {
3115
+ program.command("ai <description...>").description("Generate an agent (config + tools + skill wiring) from a natural-language description.").option("-o, --out <dir>", "Output directory", "./my-agent").option("--provider <provider>", "Planner provider (openai | anthropic | ...)", "anthropic").option("--model <model>", "Planner model id").option("--api-key <key>", "API key (falls back to env)").option("--base-url <url>", "Override base URL (for OpenAI-compatible endpoints)").option("--overwrite", "Overwrite existing files", false).option("--dry-run", "Print the plan + files without writing", false).action(async (descriptionWords, options) => {
3116
+ const description = descriptionWords.join(" ").trim();
3117
+ if (!description) {
3118
+ process.stderr.write("agentskit ai: missing description.\n");
3119
+ process.exit(1);
3120
+ }
3121
+ const resolved = resolveChatProvider({
3122
+ provider: options.provider,
3123
+ model: options.model,
3124
+ apiKey: options.apiKey,
3125
+ baseUrl: options.baseUrl
3126
+ });
3127
+ const planner2 = createAdapterPlanner(resolved.adapter);
3128
+ process.stderr.write(`Planning agent for: "${description}"
3129
+ `);
3130
+ const schema = await planner2(description);
3131
+ const files = scaffoldAgent(schema);
3132
+ if (options.dryRun) {
3133
+ process.stdout.write(JSON.stringify({ schema, files: files.map((f) => f.path) }, null, 2) + "\n");
3134
+ return;
3135
+ }
3136
+ const written = await writeScaffold(files, options.out, { overwrite: options.overwrite });
3137
+ process.stderr.write(`Wrote ${written.length} file(s) to ${options.out}
3138
+ `);
3139
+ for (const f of written) process.stderr.write(` + ${f}
3140
+ `);
3141
+ });
3142
+ }
3143
+
2962
3144
  // src/commands/index.ts
2963
3145
  function createCli() {
2964
3146
  const program = new commander.Command();
@@ -2971,6 +3153,7 @@ function createCli() {
2971
3153
  registerConfigCommand(program);
2972
3154
  registerTunnelCommand(program);
2973
3155
  registerRagCommand(program);
3156
+ registerAiCommand(program);
2974
3157
  return program;
2975
3158
  }
2976
3159