@nick848/sf-cli 1.0.12 → 1.0.13

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.js CHANGED
@@ -8885,7 +8885,7 @@ async function executeShell(command, ctx) {
8885
8885
  init_cjs_shims();
8886
8886
  init_new();
8887
8887
  async function handleNaturalLanguage(input, ctx) {
8888
- input.trim().toLowerCase();
8888
+ const trimmedInput = input.trim();
8889
8889
  const session = getActiveSession();
8890
8890
  if (session) {
8891
8891
  const result = await handleWorkflowInput(input, ctx);
@@ -8896,81 +8896,163 @@ async function handleNaturalLanguage(input, ctx) {
8896
8896
  };
8897
8897
  }
8898
8898
  }
8899
- ctx.contextManager.addMessage({
8900
- role: "user",
8901
- content: input
8902
- });
8903
- return {
8904
- output: chalk9__default.default.cyan("\u6B63\u5728\u601D\u8003...") + chalk9__default.default.yellow("\n\n\u81EA\u7136\u8BED\u8A00\u5904\u7406\u529F\u80FD\u5F00\u53D1\u4E2D...") + chalk9__default.default.gray(`
8905
- \u8F93\u5165: ${input}`),
8906
- contextUsed: input.length
8907
- };
8899
+ const apiKey = ctx.configManager.get("apiKey");
8900
+ if (!apiKey) {
8901
+ return {
8902
+ output: chalk9__default.default.yellow("\u26A0\uFE0F \u672A\u914D\u7F6E API Key\uFF0C\u65E0\u6CD5\u4F7F\u7528 AI \u529F\u80FD") + chalk9__default.default.gray("\n\n\u8BF7\u5148\u6267\u884C /model \u914D\u7F6E\u6A21\u578B") + chalk9__default.default.gray("\n\u652F\u6301: GLM-5, GPT-4o, Claude"),
8903
+ contextUsed: 0
8904
+ };
8905
+ }
8906
+ try {
8907
+ const response = await ctx.modelService.sendMessage(
8908
+ [
8909
+ {
8910
+ role: "system",
8911
+ content: buildSystemPrompt(ctx)
8912
+ },
8913
+ {
8914
+ role: "user",
8915
+ content: trimmedInput
8916
+ }
8917
+ ],
8918
+ {
8919
+ temperature: 0.7,
8920
+ maxTokens: 2e3
8921
+ }
8922
+ );
8923
+ ctx.contextManager.addMessage({
8924
+ role: "user",
8925
+ content: trimmedInput
8926
+ });
8927
+ ctx.contextManager.addMessage({
8928
+ role: "assistant",
8929
+ content: response.content
8930
+ });
8931
+ return {
8932
+ output: response.content,
8933
+ contextUsed: response.usage?.totalTokens || 0
8934
+ };
8935
+ } catch (error) {
8936
+ const errorMessage = error.message;
8937
+ if (errorMessage.includes("\u672A\u914D\u7F6E") || errorMessage.includes("\u672A\u521D\u59CB\u5316")) {
8938
+ return {
8939
+ output: chalk9__default.default.yellow("\u26A0\uFE0F \u6A21\u578B\u672A\u6B63\u786E\u914D\u7F6E") + chalk9__default.default.gray("\n\n\u8BF7\u6267\u884C /model \u91CD\u65B0\u914D\u7F6E"),
8940
+ contextUsed: 0
8941
+ };
8942
+ }
8943
+ if (errorMessage.includes("timeout") || errorMessage.includes("\u8D85\u65F6")) {
8944
+ return {
8945
+ output: chalk9__default.default.red("\u2717 \u8BF7\u6C42\u8D85\u65F6\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5"),
8946
+ contextUsed: 0
8947
+ };
8948
+ }
8949
+ return {
8950
+ output: chalk9__default.default.red(`\u2717 \u5904\u7406\u5931\u8D25: ${errorMessage}`),
8951
+ contextUsed: 0
8952
+ };
8953
+ }
8954
+ }
8955
+ function buildSystemPrompt(ctx) {
8956
+ const parts = [
8957
+ "\u4F60\u662F sf-cli \u7684 AI \u52A9\u624B\uFF0C\u4E00\u4E2A\u4E13\u4E1A\u7684\u8F6F\u4EF6\u5F00\u53D1\u52A9\u624B\u3002",
8958
+ "",
8959
+ "\u4F60\u53EF\u4EE5\u5E2E\u52A9\u7528\u6237\uFF1A",
8960
+ "1. \u7406\u89E3\u548C\u5206\u6790\u9700\u6C42",
8961
+ "2. \u63D0\u4F9B\u4EE3\u7801\u5EFA\u8BAE\u548C\u5B9E\u73B0\u65B9\u6848",
8962
+ "3. \u56DE\u7B54\u6280\u672F\u95EE\u9898",
8963
+ "4. \u8F85\u52A9\u8FDB\u884C\u4EE3\u7801\u5BA1\u67E5",
8964
+ "",
8965
+ "\u5F53\u524D\u9879\u76EE\u4FE1\u606F\uFF1A",
8966
+ `- \u5DE5\u4F5C\u76EE\u5F55: ${ctx.options.workingDirectory}`,
8967
+ `- \u6A21\u578B: ${ctx.modelService.getCurrentModel() || "\u672A\u6307\u5B9A"}`
8968
+ ];
8969
+ return parts.join("\n");
8908
8970
  }
8909
8971
 
8910
8972
  // src/cli/executor.ts
8911
8973
  init_new();
8912
- var BASIC_COMMANDS = [
8974
+ var ALWAYS_ALLOWED = [
8913
8975
  "help",
8914
8976
  "h",
8915
8977
  "?",
8916
- "init",
8917
- "i",
8918
8978
  "model",
8919
8979
  "m",
8920
- "new",
8921
- "n",
8922
8980
  "exit",
8923
8981
  "e",
8924
8982
  "q",
8925
8983
  "quit",
8926
8984
  "clear",
8927
8985
  "c",
8928
- "update",
8929
- "u",
8930
8986
  "version",
8931
8987
  "v"
8932
8988
  ];
8989
+ var REQUIRES_API_KEY = [
8990
+ "new",
8991
+ "n",
8992
+ "init",
8993
+ "i",
8994
+ "update",
8995
+ "u"
8996
+ ];
8933
8997
  var CommandExecutor = class {
8934
8998
  async execute(parseResult, ctx) {
8935
8999
  if (!parseResult.success || !parseResult.command) {
8936
9000
  return { output: chalk9__default.default.red(`\u9519\u8BEF: ${parseResult.error}`) };
8937
9001
  }
8938
9002
  const { command } = parseResult;
9003
+ const hasApiKey = !!ctx.configManager.get("apiKey");
8939
9004
  const hasActiveWorkflow = getActiveSession() !== null;
8940
- if (!hasActiveWorkflow) {
8941
- if (command.type === "slash" /* SLASH */) {
8942
- const cmd = command.command?.toLowerCase() || "";
8943
- if (!BASIC_COMMANDS.includes(cmd)) {
9005
+ if (command.type === "slash" /* SLASH */) {
9006
+ const cmd = command.command?.toLowerCase() || "";
9007
+ if (ALWAYS_ALLOWED.includes(cmd)) {
9008
+ return this.executeSlashCommand(command, ctx);
9009
+ }
9010
+ if (REQUIRES_API_KEY.includes(cmd)) {
9011
+ if (!hasApiKey) {
8944
9012
  return {
8945
- output: chalk9__default.default.yellow("\u5F53\u524D\u6CA1\u6709\u6D3B\u8DC3\u7684\u5DE5\u4F5C\u6D41") + chalk9__default.default.gray("\n\u8BF7\u5148\u4F7F\u7528 ") + chalk9__default.default.cyan("/new <\u9700\u6C42\u63CF\u8FF0>") + chalk9__default.default.gray(" \u542F\u52A8\u65B0\u5DE5\u4F5C\u6D41")
9013
+ output: chalk9__default.default.yellow("\u26A0\uFE0F \u8BF7\u5148\u914D\u7F6E API Key") + chalk9__default.default.gray("\n\n\u6267\u884C /model \u9009\u62E9\u6A21\u578B\u5E76\u914D\u7F6E API Key")
8946
9014
  };
8947
9015
  }
8948
- } else if (command.type === "dollar" /* DOLLAR */) {
9016
+ return this.executeSlashCommand(command, ctx);
9017
+ }
9018
+ if (!hasActiveWorkflow) {
9019
+ return {
9020
+ output: chalk9__default.default.yellow("\u5F53\u524D\u6CA1\u6709\u6D3B\u8DC3\u7684\u5DE5\u4F5C\u6D41") + chalk9__default.default.gray("\n\u8BF7\u5148\u4F7F\u7528 ") + chalk9__default.default.cyan("/new <\u9700\u6C42\u63CF\u8FF0>") + chalk9__default.default.gray(" \u542F\u52A8\u65B0\u5DE5\u4F5C\u6D41")
9021
+ };
9022
+ }
9023
+ return this.executeSlashCommand(command, ctx);
9024
+ }
9025
+ if (command.type === "dollar" /* DOLLAR */) {
9026
+ if (!hasActiveWorkflow) {
8949
9027
  return {
8950
9028
  output: chalk9__default.default.yellow("\u5F53\u524D\u6CA1\u6709\u6D3B\u8DC3\u7684\u5DE5\u4F5C\u6D41\uFF0C\u65E0\u6CD5\u8C03\u7528 Agent") + chalk9__default.default.gray("\n\u8BF7\u5148\u4F7F\u7528 ") + chalk9__default.default.cyan("/new <\u9700\u6C42\u63CF\u8FF0>") + chalk9__default.default.gray(" \u542F\u52A8\u65B0\u5DE5\u4F5C\u6D41")
8951
9029
  };
8952
- } else if (command.type === "shell" /* SHELL */) {
9030
+ }
9031
+ if (!hasApiKey) {
9032
+ return {
9033
+ output: chalk9__default.default.yellow("\u26A0\uFE0F \u8BF7\u5148\u914D\u7F6E API Key") + chalk9__default.default.gray("\n\n\u6267\u884C /model \u914D\u7F6E\u6A21\u578B")
9034
+ };
9035
+ }
9036
+ return this.executeAgent(command, ctx);
9037
+ }
9038
+ if (command.type === "shell" /* SHELL */) {
9039
+ if (!hasActiveWorkflow) {
8953
9040
  return {
8954
9041
  output: chalk9__default.default.yellow("\u5F53\u524D\u6CA1\u6709\u6D3B\u8DC3\u7684\u5DE5\u4F5C\u6D41\uFF0C\u65E0\u6CD5\u6267\u884C Shell \u547D\u4EE4") + chalk9__default.default.gray("\n\u8BF7\u5148\u4F7F\u7528 ") + chalk9__default.default.cyan("/new <\u9700\u6C42\u63CF\u8FF0>") + chalk9__default.default.gray(" \u542F\u52A8\u65B0\u5DE5\u4F5C\u6D41")
8955
9042
  };
8956
9043
  }
9044
+ return this.executeShell(command, ctx);
8957
9045
  }
8958
- switch (command.type) {
8959
- case "slash" /* SLASH */:
8960
- return this.executeSlashCommand(command, ctx);
8961
- case "at" /* AT */:
8962
- return this.executeFileReference(command, ctx);
8963
- case "dollar" /* DOLLAR */:
8964
- return this.executeAgent(command, ctx);
8965
- case "shell" /* SHELL */:
8966
- return this.executeShell(command, ctx);
8967
- case "natural" /* NATURAL */:
8968
- return this.executeNaturalLanguage(command, ctx);
8969
- case "yolo" /* YOLO */:
8970
- return this.executeYolo(ctx);
8971
- default:
8972
- return { output: chalk9__default.default.red("\u672A\u77E5\u7684\u547D\u4EE4\u7C7B\u578B") };
9046
+ if (command.type === "at" /* AT */) {
9047
+ return this.executeFileReference(command, ctx);
9048
+ }
9049
+ if (command.type === "natural" /* NATURAL */) {
9050
+ return this.executeNaturalLanguage(command, ctx);
9051
+ }
9052
+ if (command.type === "yolo" /* YOLO */) {
9053
+ return this.executeYolo(ctx);
8973
9054
  }
9055
+ return { output: chalk9__default.default.red("\u672A\u77E5\u7684\u547D\u4EE4\u7C7B\u578B") };
8974
9056
  }
8975
9057
  async executeSlashCommand(command, ctx) {
8976
9058
  const result = await runSlashCommand(