@armadra/agent 0.3.0 → 0.4.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.
Files changed (198) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/README.md +93 -44
  3. package/dist/agent/retry.d.ts +1 -1
  4. package/dist/agent/retry.js +2 -1
  5. package/dist/agent/session-cache.js +1 -1
  6. package/dist/agent/session-classifier.d.ts +18 -0
  7. package/dist/agent/session-classifier.js +103 -0
  8. package/dist/agent/session-core.d.ts +13 -0
  9. package/dist/agent/session-tools.js +24 -3
  10. package/dist/agent/session.d.ts +3 -0
  11. package/dist/agent/session.js +12 -3
  12. package/dist/agent/tool-runner.js +15 -2
  13. package/dist/agent/types.d.ts +9 -1
  14. package/dist/ai/apis/anthropic-messages.js +3 -2
  15. package/dist/ai/apis/google-generative-ai.js +3 -2
  16. package/dist/ai/apis/openai-completions.js +3 -2
  17. package/dist/ai/apis/openai-responses.js +3 -2
  18. package/dist/ai/http.d.ts +28 -6
  19. package/dist/ai/http.js +41 -8
  20. package/dist/ai/providers/registry.d.ts +8 -1
  21. package/dist/ai/providers/registry.js +30 -22
  22. package/dist/ai/providers/suggest.d.ts +18 -0
  23. package/dist/ai/providers/suggest.js +72 -0
  24. package/dist/ai/sse.d.ts +6 -2
  25. package/dist/ai/sse.js +22 -2
  26. package/dist/ai/types.d.ts +9 -2
  27. package/dist/bundle/ama.cjs +15279 -10151
  28. package/dist/cli/args.d.ts +19 -4
  29. package/dist/cli/args.js +83 -10
  30. package/dist/cli/bootstrap.js +27 -3
  31. package/dist/cli/codemode-notice.d.ts +20 -0
  32. package/dist/cli/codemode-notice.js +55 -0
  33. package/dist/cli/compose-session.d.ts +5 -0
  34. package/dist/cli/compose-session.js +32 -1
  35. package/dist/cli/compose-store.d.ts +1 -1
  36. package/dist/cli/compose-store.js +3 -1
  37. package/dist/cli/compose.d.ts +14 -4
  38. package/dist/cli/compose.js +29 -10
  39. package/dist/cli/default-model.d.ts +38 -1
  40. package/dist/cli/default-model.js +96 -9
  41. package/dist/cli/deps.d.ts +36 -0
  42. package/dist/cli/exit-codes.d.ts +2 -0
  43. package/dist/cli/exit-codes.js +3 -0
  44. package/dist/cli/fake-visibility.d.ts +13 -0
  45. package/dist/cli/fake-visibility.js +26 -0
  46. package/dist/cli/from-prompt.d.ts +18 -0
  47. package/dist/cli/from-prompt.js +49 -0
  48. package/dist/cli/main.d.ts +9 -2
  49. package/dist/cli/main.js +77 -3
  50. package/dist/cli/proxy.d.ts +51 -0
  51. package/dist/cli/proxy.js +135 -0
  52. package/dist/cli/startup-screen.d.ts +29 -0
  53. package/dist/cli/startup-screen.js +47 -0
  54. package/dist/cli/startup-steps.d.ts +1 -1
  55. package/dist/cli/startup-steps.js +14 -12
  56. package/dist/cli/subcommands/config.d.ts +19 -3
  57. package/dist/cli/subcommands/config.js +108 -21
  58. package/dist/cli/subcommands/context.js +4 -1
  59. package/dist/cli/subcommands/doctor.js +12 -1
  60. package/dist/cli/subcommands/init.js +2 -1
  61. package/dist/cli/subcommands/models-discover.d.ts +15 -9
  62. package/dist/cli/subcommands/models-discover.js +53 -46
  63. package/dist/cli/subcommands/probe-runner.d.ts +96 -0
  64. package/dist/cli/subcommands/probe-runner.js +264 -0
  65. package/dist/cli/subcommands/providers-probe.d.ts +34 -0
  66. package/dist/cli/subcommands/providers-probe.js +87 -0
  67. package/dist/cli/subcommands/providers.d.ts +3 -2
  68. package/dist/cli/subcommands/providers.js +48 -45
  69. package/dist/cli/subcommands/sessions-export.d.ts +10 -0
  70. package/dist/cli/subcommands/sessions-export.js +59 -0
  71. package/dist/cli/subcommands/sessions-search.d.ts +13 -0
  72. package/dist/cli/subcommands/sessions-search.js +103 -0
  73. package/dist/cli/subcommands/sessions.d.ts +3 -2
  74. package/dist/cli/subcommands/sessions.js +22 -1
  75. package/dist/cli/subcommands/stats.d.ts +17 -0
  76. package/dist/cli/subcommands/stats.js +198 -0
  77. package/dist/cli/system-prompt-arg.d.ts +11 -0
  78. package/dist/cli/system-prompt-arg.js +34 -0
  79. package/dist/codemode/modes.d.ts +4 -11
  80. package/dist/codemode/modes.js +5 -27
  81. package/dist/codemode/tool.d.ts +18 -14
  82. package/dist/codemode/tool.js +71 -28
  83. package/dist/config/init.d.ts +6 -2
  84. package/dist/config/init.js +12 -5
  85. package/dist/config/json-schema.d.ts +1 -0
  86. package/dist/config/json-schema.js +36 -3
  87. package/dist/config/key-docs.d.ts +22 -0
  88. package/dist/config/key-docs.js +105 -0
  89. package/dist/config/merge.d.ts +9 -8
  90. package/dist/config/merge.js +22 -7
  91. package/dist/config/schema.d.ts +1 -1
  92. package/dist/config/schema.js +24 -5
  93. package/dist/config/types.d.ts +34 -6
  94. package/dist/config/types.js +17 -1
  95. package/dist/modes/commands-core.js +6 -5
  96. package/dist/modes/interactive/approval-dialog.d.ts +21 -5
  97. package/dist/modes/interactive/approval-dialog.js +106 -27
  98. package/dist/modes/interactive/commands.d.ts +11 -2
  99. package/dist/modes/interactive/commands.js +56 -16
  100. package/dist/modes/interactive/interactive-mode.d.ts +2 -1
  101. package/dist/modes/interactive/interactive-mode.js +65 -81
  102. package/dist/modes/interactive/key-dispatch.d.ts +3 -1
  103. package/dist/modes/interactive/key-dispatch.js +5 -6
  104. package/dist/modes/interactive/line/line-mode.d.ts +1 -0
  105. package/dist/modes/interactive/line/line-mode.js +6 -4
  106. package/dist/modes/interactive/line/line-render.d.ts +4 -0
  107. package/dist/modes/interactive/line/line-render.js +28 -6
  108. package/dist/modes/interactive/message-view.d.ts +48 -9
  109. package/dist/modes/interactive/message-view.js +238 -44
  110. package/dist/modes/interactive/panels.d.ts +18 -0
  111. package/dist/modes/interactive/panels.js +143 -0
  112. package/dist/modes/interactive/pickers.d.ts +23 -2
  113. package/dist/modes/interactive/pickers.js +48 -15
  114. package/dist/modes/interactive/run-indicator.d.ts +51 -0
  115. package/dist/modes/interactive/run-indicator.js +189 -0
  116. package/dist/modes/interactive/startup-header.d.ts +40 -0
  117. package/dist/modes/interactive/startup-header.js +169 -0
  118. package/dist/modes/interactive/status-bar.d.ts +18 -15
  119. package/dist/modes/interactive/status-bar.js +98 -56
  120. package/dist/modes/interactive/tool-summary.d.ts +46 -0
  121. package/dist/modes/interactive/tool-summary.js +218 -0
  122. package/dist/modes/interactive/tool-view.d.ts +48 -15
  123. package/dist/modes/interactive/tool-view.js +203 -145
  124. package/dist/modes/print/print-mode.d.ts +31 -4
  125. package/dist/modes/print/print-mode.js +116 -7
  126. package/dist/modes/rpc/commands.js +5 -0
  127. package/dist/permissions/auto-safe.d.ts +60 -0
  128. package/dist/permissions/auto-safe.js +529 -0
  129. package/dist/permissions/classifier.d.ts +64 -0
  130. package/dist/permissions/classifier.js +184 -0
  131. package/dist/permissions/dangerous.d.ts +5 -0
  132. package/dist/permissions/dangerous.js +1 -1
  133. package/dist/permissions/modes.d.ts +30 -0
  134. package/dist/permissions/modes.js +78 -0
  135. package/dist/permissions/pipeline.d.ts +31 -4
  136. package/dist/permissions/pipeline.js +196 -6
  137. package/dist/permissions/protected.d.ts +19 -0
  138. package/dist/permissions/protected.js +74 -0
  139. package/dist/permissions/rules.js +3 -0
  140. package/dist/permissions/types.d.ts +50 -3
  141. package/dist/permissions/types.js +3 -0
  142. package/dist/sdk.d.ts +9 -3
  143. package/dist/sdk.js +10 -2
  144. package/dist/session/export.d.ts +32 -0
  145. package/dist/session/export.js +187 -0
  146. package/dist/session/redact.d.ts +15 -0
  147. package/dist/session/redact.js +55 -0
  148. package/dist/session/reuse.d.ts +33 -0
  149. package/dist/session/reuse.js +86 -0
  150. package/dist/session/scan.d.ts +34 -0
  151. package/dist/session/scan.js +140 -0
  152. package/dist/session/search.d.ts +52 -0
  153. package/dist/session/search.js +211 -0
  154. package/dist/session/stats-aggregate.d.ts +63 -0
  155. package/dist/session/stats-aggregate.js +163 -0
  156. package/dist/session/stats-index.d.ts +26 -0
  157. package/dist/session/stats-index.js +91 -0
  158. package/dist/session/stats-scan.d.ts +54 -0
  159. package/dist/session/stats-scan.js +236 -0
  160. package/dist/tools/presets.d.ts +35 -8
  161. package/dist/tools/presets.js +56 -17
  162. package/dist/tui/component.d.ts +8 -2
  163. package/dist/tui/component.js +3 -1
  164. package/dist/tui/components/box.d.ts +6 -1
  165. package/dist/tui/components/box.js +16 -6
  166. package/dist/tui/components/card.d.ts +23 -0
  167. package/dist/tui/components/card.js +37 -0
  168. package/dist/tui/components/editor-history.d.ts +6 -0
  169. package/dist/tui/components/editor-history.js +45 -0
  170. package/dist/tui/components/editor-paste.d.ts +1 -1
  171. package/dist/tui/components/editor-paste.js +4 -4
  172. package/dist/tui/components/editor.d.ts +11 -5
  173. package/dist/tui/components/editor.js +52 -58
  174. package/dist/tui/components/key-value.d.ts +3 -0
  175. package/dist/tui/components/key-value.js +16 -6
  176. package/dist/tui/components/loader.d.ts +37 -7
  177. package/dist/tui/components/loader.js +84 -21
  178. package/dist/tui/components/markdown.d.ts +5 -1
  179. package/dist/tui/components/markdown.js +45 -21
  180. package/dist/tui/components/meter.d.ts +3 -3
  181. package/dist/tui/components/meter.js +13 -11
  182. package/dist/tui/components/select-list.d.ts +23 -1
  183. package/dist/tui/components/select-list.js +76 -13
  184. package/dist/tui/glyphs.d.ts +68 -0
  185. package/dist/tui/glyphs.js +114 -0
  186. package/dist/tui/theme.d.ts +30 -6
  187. package/dist/tui/theme.js +103 -17
  188. package/dist/tui.d.ts +4 -2
  189. package/dist/tui.js +3 -1
  190. package/docs/codemode.md +23 -9
  191. package/docs/hooks.md +10 -10
  192. package/docs/permissions.md +148 -0
  193. package/docs/providers.md +33 -10
  194. package/docs/rpc.md +36 -36
  195. package/docs/session-format.md +2 -1
  196. package/docs/sessions.md +134 -0
  197. package/docs/tui.md +139 -62
  198. package/package.json +3 -1
@@ -7,6 +7,7 @@ import { loadConfigFile } from "../../config/load.js";
7
7
  import { mergeBaseLayers } from "../../config/merge.js";
8
8
  import { CONFIG_FILE, resolveConfigDir, resolveDataDir, userFile } from "../../config/paths.js";
9
9
  import { loadProfile } from "../../config/profile.js";
10
+ import { hideFakeProvider } from "../fake-visibility.js";
10
11
  export function loadUserLevel(io, options = {}) {
11
12
  const configDir = resolveConfigDir({ env: io.env });
12
13
  const dataDir = resolveDataDir({ env: io.env });
@@ -32,11 +33,13 @@ export function loadUserLevel(io, options = {}) {
32
33
  return result;
33
34
  }
34
35
  export async function buildRegistry(level, io, deps) {
35
- return deps.providers.create({
36
+ // 列表类子命令(doctor / models list / providers list / config show)缺省不列 fake
37
+ const registry = await deps.providers.create({
36
38
  config: level.merged.config,
37
39
  cwd: io.cwd,
38
40
  authFile: level.authFile,
39
41
  authEnv: level.profile?.authEnv ?? true,
40
42
  dataDir: level.dataDir,
41
43
  });
44
+ return hideFakeProvider(registry, io.env);
42
45
  }
@@ -18,7 +18,8 @@ import { loadHookConfigs } from "../../hooks/config.js";
18
18
  import { AMA_VERSION } from "../../version.js";
19
19
  import { parseSubArgs } from "../args.js";
20
20
  import { ExitCode } from "../exit-codes.js";
21
- import { describeModel } from "./config.js";
21
+ import { describeProxy, inspectProxy } from "../proxy.js";
22
+ import { describeCodemode, describeModel } from "./config.js";
22
23
  import { buildRegistry, loadUserLevel } from "./context.js";
23
24
  export const DOCTOR_USAGE = `用法:ama doctor [--profile <文件>] [--auth-file <文件>] [--trust | --no-trust]
24
25
  `;
@@ -184,6 +185,11 @@ function terminalSection(report, io) {
184
185
  const tui = io.stdinIsTTY && io.stdoutIsTTY && env["TERM"] !== "dumb";
185
186
  report.item(`缺省界面:${tui ? "终端界面" : "行式(--no-tui 等价)"}`);
186
187
  }
188
+ function proxySection(report, io) {
189
+ report.section("代理");
190
+ for (const line of describeProxy(inspectProxy(io.env)))
191
+ report.item(line);
192
+ }
187
193
  export async function runDoctor(argv, io, deps) {
188
194
  const { values, flags } = parseSubArgs(argv, ["profile", "auth-file"], ["trust", "no-trust"]);
189
195
  if (flags.has("help")) {
@@ -223,6 +229,10 @@ export async function runDoctor(argv, io, deps) {
223
229
  for (const warning of merged.warnings)
224
230
  report.item(` 收紧:${warning}`);
225
231
  report.item(`有效权限模式:${merged.config.permission?.mode ?? "default"}`);
232
+ const codemode = describeCodemode(merged.config);
233
+ report.item(`codemode:${codemode.mode}(${codemode.reason})`);
234
+ if (codemode.unavailable !== undefined)
235
+ report.item(` ${codemode.unavailable}`);
226
236
  }
227
237
  catch {
228
238
  // probeLine 已报告
@@ -240,6 +250,7 @@ export async function runDoctor(argv, io, deps) {
240
250
  await keySection(report, io, level, deps);
241
251
  hookSection(report, io, level, trusted);
242
252
  report.item(`用户级 hooks.json:${userFile(level, HOOKS_FILE)}`);
253
+ proxySection(report, io);
243
254
  terminalSection(report, io);
244
255
  io.stdout(report.text());
245
256
  return report.problems > 0 ? ExitCode.Config : ExitCode.Ok;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * `ama init [--force]`(docs/providers.md「配置目录」):建配置目录与缺省文件,逐个报告状态。
3
3
  */
4
- import { describeInit, initConfigDir } from "../../config/init.js";
4
+ import { INIT_NEXT_STEPS, describeInit, initConfigDir } from "../../config/init.js";
5
5
  import { resolveConfigDir } from "../../config/paths.js";
6
6
  import { parseSubArgs, UsageError } from "../args.js";
7
7
  import { ExitCode } from "../exit-codes.js";
@@ -18,5 +18,6 @@ export function runInit(argv, io) {
18
18
  throw new UsageError(`多余的参数:${positionals.join(" ")}`);
19
19
  const result = initConfigDir(resolveConfigDir({ env: io.env }), { force: flags.has("force") });
20
20
  io.stdout(describeInit(result));
21
+ io.stdout(`\n${INIT_NEXT_STEPS.join("\n")}\n`);
21
22
  return ExitCode.Ok;
22
23
  }
@@ -5,9 +5,10 @@
5
5
  * 带供应商的鉴权头,打印 id 列表并标出已配置的条目。
6
6
  *
7
7
  * `--probe`:同一中转下不同模型支持的协议不同,对每个 id 依次试供应商协议 → completions →
8
- * responses → messages(去重),各发一次最小请求(`ama models check` 同款,maxTokens 16),
9
- * 记第一个成功的协议;每模型最多 3 次请求,`--limit`(缺省 30)限制探测的模型数,执行前打印
10
- * 预估;401 / 403 / 429 立即停止(key 无效或被限流,继续只会浪费请求)。
8
+ * responses → messages(去重),各发一次最小请求(maxTokens 16,见到首个流事件即判可用并断开),
9
+ * 记第一个成功的协议;模型之间并发(`--concurrency`,缺省 6),单次超时 `--probe-timeout`(缺省
10
+ * 15 s)。每模型最多 3 次请求,`--limit`(缺省 30)限制探测的模型数,执行前打印预估;401 / 403 立即
11
+ * 停止,429 降并发并重试一次、仍 429 停止(见 probe-runner.ts)。
11
12
  *
12
13
  * `--write`:新条目合并进用户级 config.json 的 `providers.<id>.models`——已有同 id 不覆盖;只写
13
14
  * `id` 与探到的 `api`(与供应商协议相同时省略);带 `--probe` 时只写探测成功的模型。不猜
@@ -19,7 +20,6 @@
19
20
  import type { Api, Model, ProviderData, ProviderRegistryApi } from "../../ai/types.js";
20
21
  import type { ModelsAction } from "./models.js";
21
22
  export declare const DISCOVER_TIMEOUT_MS = 15000;
22
- export declare const PROBE_TIMEOUT_MS = 30000;
23
23
  export declare const DEFAULT_PROBE_LIMIT = 30;
24
24
  /** 中转常见的三种协议,按此顺序探测(供应商协议排最前)。 */
25
25
  export declare const PROBE_APIS: readonly Api[];
@@ -31,16 +31,22 @@ export declare function discoverModels(provider: ProviderData, apiKey: string |
31
31
  export declare function probeOrder(provider: ProviderData): Api[];
32
32
  export interface ProbeOptions {
33
33
  timeoutMs?: number;
34
+ /** 同时在途的模型数(每个模型内按协议顺序依次试);缺省 6。 */
35
+ concurrency?: number;
36
+ /** 测试用:429 重试前的退避。 */
37
+ retryDelayMs?: number;
34
38
  /** 每次请求后回调。 */
35
39
  onAttempt?: (id: string, api: Api, error: string | undefined) => void;
36
- /** 每个模型探完回调(进度输出)。 */
40
+ /** 每个模型探完立即回调(按完成顺序,进度用)。 */
41
+ onSettled?: () => void;
42
+ /** 每个模型探完回调,按模型顺序(不按完成顺序)。 */
37
43
  onResult?: (id: string, api: Api | undefined) => void;
38
44
  /** 因鉴权失败或限流提前停止时回调。 */
39
45
  onStop?: (reason: string) => void;
40
46
  }
41
- export declare const FATAL_STATUS: RegExp;
42
- /** 返回已探测模型 → 第一个成功的协议(都失败为 undefined);超出 `limit` 或提前停止的不在表里。 */
47
+ /**
48
+ * 返回已探测模型 → 第一个成功的协议(都失败为 undefined);超出 `limit` 或因提前停止没探完的不在表里。
49
+ * 模型之间并发(有界),同一模型内按协议顺序依次试,第一个成功即停。
50
+ */
43
51
  export declare function probeModelApis(registry: ProviderRegistryApi, provider: ProviderData, ids: string[], limit: number, options?: ProbeOptions): Promise<Map<string, Api | undefined>>;
44
- /** 一次最小请求;成功返回 undefined,失败返回错误文本(`ama providers add --probe` 也用)。 */
45
- export declare function attempt(registry: ProviderRegistryApi, model: Model, apiKey: string | undefined, timeoutMs?: number): Promise<string | undefined>;
46
52
  export declare const DISCOVER_ACTION: ModelsAction;
@@ -5,9 +5,10 @@
5
5
  * 带供应商的鉴权头,打印 id 列表并标出已配置的条目。
6
6
  *
7
7
  * `--probe`:同一中转下不同模型支持的协议不同,对每个 id 依次试供应商协议 → completions →
8
- * responses → messages(去重),各发一次最小请求(`ama models check` 同款,maxTokens 16),
9
- * 记第一个成功的协议;每模型最多 3 次请求,`--limit`(缺省 30)限制探测的模型数,执行前打印
10
- * 预估;401 / 403 / 429 立即停止(key 无效或被限流,继续只会浪费请求)。
8
+ * responses → messages(去重),各发一次最小请求(maxTokens 16,见到首个流事件即判可用并断开),
9
+ * 记第一个成功的协议;模型之间并发(`--concurrency`,缺省 6),单次超时 `--probe-timeout`(缺省
10
+ * 15 s)。每模型最多 3 次请求,`--limit`(缺省 30)限制探测的模型数,执行前打印预估;401 / 403 立即
11
+ * 停止,429 降并发并重试一次、仍 429 停止(见 probe-runner.ts)。
11
12
  *
12
13
  * `--write`:新条目合并进用户级 config.json 的 `providers.<id>.models`——已有同 id 不覆盖;只写
13
14
  * `id` 与探到的 `api`(与供应商协议相同时省略);带 `--probe` 时只写探测成功的模型。不猜
@@ -27,8 +28,8 @@ import { writeConfigFile } from "../../config/write.js";
27
28
  import { UsageError } from "../args.js";
28
29
  import { ExitCode } from "../exit-codes.js";
29
30
  import { compactTokens } from "./model-meta.js";
31
+ import { describeProbePlan, parseProbeTuning, ProbeProgress, ProbeScheduler, } from "./probe-runner.js";
30
32
  export const DISCOVER_TIMEOUT_MS = 15_000;
31
- export const PROBE_TIMEOUT_MS = 30_000;
32
33
  export const DEFAULT_PROBE_LIMIT = 30;
33
34
  const ANTHROPIC_VERSION = "2023-06-01";
34
35
  /** 中转常见的三种协议,按此顺序探测(供应商协议排最前)。 */
@@ -57,57 +58,57 @@ export function probeOrder(provider) {
57
58
  ? [provider.api, ...PROBE_APIS.filter((api) => api !== provider.api)]
58
59
  : [...PROBE_APIS];
59
60
  }
60
- export const FATAL_STATUS = /^(?:HTTP )?(?:401|403|429)\b/;
61
- /** 返回已探测模型 → 第一个成功的协议(都失败为 undefined);超出 `limit` 或提前停止的不在表里。 */
61
+ /**
62
+ * 返回已探测模型 → 第一个成功的协议(都失败为 undefined);超出 `limit` 或因提前停止没探完的不在表里。
63
+ * 模型之间并发(有界),同一模型内按协议顺序依次试,第一个成功即停。
64
+ */
62
65
  export async function probeModelApis(registry, provider, ids, limit, options = {}) {
63
66
  const result = new Map();
64
67
  const key = await registry.resolveApiKey(provider.id);
65
68
  const order = probeOrder(provider).filter((api) => registry.getApi(api) !== undefined);
66
- for (const id of ids.slice(0, Math.max(0, limit))) {
69
+ const selected = ids.slice(0, Math.max(0, limit));
70
+ const scheduler = new ProbeScheduler(registry, key.apiKey, {
71
+ concurrency: options.concurrency,
72
+ timeoutMs: options.timeoutMs,
73
+ retryDelayMs: options.retryDelayMs,
74
+ });
75
+ const done = [];
76
+ let printed = 0;
77
+ await scheduler.run(selected.length, async (index) => {
78
+ const id = selected[index];
67
79
  const known = provider.models.find((m) => m.id === id);
68
- let found;
69
80
  for (const api of order) {
70
81
  const model = known !== undefined
71
82
  ? { ...known, api }
72
83
  : materializeModel(withCustomDefaults({ id }, provider.id, api), provider);
73
- const error = await attempt(registry, model, key.apiKey, options.timeoutMs);
74
- options.onAttempt?.(id, api, error);
75
- if (error === undefined) {
76
- found = api;
77
- break;
78
- }
79
- if (FATAL_STATUS.test(error)) {
80
- options.onStop?.(error);
81
- return result;
84
+ const outcome = await scheduler.probe(model);
85
+ if (outcome.aborted)
86
+ return { complete: false, api: undefined };
87
+ options.onAttempt?.(id, api, outcome.error);
88
+ if (outcome.error === undefined)
89
+ return { complete: true, api };
90
+ if (scheduler.stopped !== undefined)
91
+ return { complete: false, api: undefined };
92
+ }
93
+ return { complete: true, api: undefined };
94
+ }, (index, found) => {
95
+ done[index] = found;
96
+ options.onSettled?.();
97
+ // 按模型顺序交出结果:前面的都完成了才输出
98
+ while (printed < selected.length && done[printed] !== undefined) {
99
+ const entry = done[printed];
100
+ const id = selected[printed];
101
+ if (entry.complete) {
102
+ result.set(id, entry.api);
103
+ options.onResult?.(id, entry.api);
82
104
  }
105
+ printed++;
83
106
  }
84
- result.set(id, found);
85
- options.onResult?.(id, found);
86
- }
107
+ });
108
+ if (scheduler.stopped !== undefined)
109
+ options.onStop?.(scheduler.stopped);
87
110
  return result;
88
111
  }
89
- /** 一次最小请求;成功返回 undefined,失败返回错误文本(`ama providers add --probe` 也用)。 */
90
- export async function attempt(registry, model, apiKey, timeoutMs = PROBE_TIMEOUT_MS) {
91
- const impl = registry.getApi(model.api);
92
- if (impl === undefined)
93
- return `协议 ${model.api} 尚未实现`;
94
- try {
95
- const message = await impl
96
- .stream(model, { messages: [{ role: "user", content: "Reply with: ok", timestamp: Date.now() }] }, {
97
- signal: AbortSignal.timeout(timeoutMs),
98
- ...(apiKey !== undefined ? { apiKey } : {}),
99
- maxTokens: 16,
100
- cacheRetention: "none",
101
- })
102
- .result();
103
- if (message.stopReason === "error" || message.stopReason === "aborted")
104
- return message.errorMessage ?? message.stopReason;
105
- return undefined;
106
- }
107
- catch (error) {
108
- return error instanceof Error ? error.message : String(error);
109
- }
110
- }
111
112
  function parseLimit(raw) {
112
113
  if (raw === undefined)
113
114
  return DEFAULT_PROBE_LIMIT;
@@ -202,13 +203,19 @@ async function run(ctx) {
202
203
  const ids = found.map((m) => m.id);
203
204
  const count = Math.min(limit, ids.length);
204
205
  const order = probeOrder(provider);
206
+ const tuning = parseProbeTuning(ctx.values);
205
207
  io.stdout(`\n探测协议:${count} 个模型(${order.join(" → ")}),最多 ${count * order.length} 次请求` +
206
- `${ids.length > count ? `;另有 ${ids.length - count} 个超出 --limit ${limit},未探测` : ""}\n`);
208
+ `${ids.length > count ? `;另有 ${ids.length - count} 个超出 --limit ${limit},未探测` : ""}\n` +
209
+ `${describeProbePlan(count * order.length, tuning.concurrency, tuning.timeoutMs)}\n`);
207
210
  let stopped;
211
+ const progress = new ProbeProgress(io.stdout, io.stdoutIsTTY, count);
208
212
  const probed = await probeModelApis(registry, provider, ids, limit, {
209
- onResult: (modelId, api) => io.stdout(` ${modelId} ${api ?? "不可用(三种协议均失败)"}\n`),
213
+ ...tuning,
214
+ onSettled: () => progress.tick(),
215
+ onResult: (modelId, api) => progress.line(` ${modelId} ${api ?? "不可用(三种协议均失败)"}\n`),
210
216
  onStop: (reason) => (stopped = reason),
211
217
  });
218
+ progress.finish();
212
219
  if (write) {
213
220
  const entries = [];
214
221
  for (const [modelId, api] of probed) {
@@ -224,9 +231,9 @@ async function run(ctx) {
224
231
  return ExitCode.Ok;
225
232
  }
226
233
  export const DISCOVER_ACTION = {
227
- usage: "ama models discover <provider> [--probe] [--write] [--limit <n>]",
234
+ usage: "ama models discover <provider> [--probe] [--write] [--limit <n>] [--concurrency <n>] [--probe-timeout <ms>]",
228
235
  required: "<provider>",
229
- valueOptions: ["limit"],
236
+ valueOptions: ["limit", "concurrency", "probe-timeout"],
230
237
  flagOptions: ["probe", "write"],
231
238
  run,
232
239
  };
@@ -0,0 +1,96 @@
1
+ /**
2
+ * 探测的执行层(`ama providers add|refresh --probe`、`ama models discover --probe` 共用)。
3
+ *
4
+ * - **首事件即判定**:HTTP 成功、流里出现第一个内容事件(text / thinking / toolcall)或 done 即判可用,
5
+ * 立刻中止请求;只见到 `start`(推理模型可能要想很久才出第一个 delta)时再等 `settleMs`,期间没有
6
+ * error 事件也判可用——防止中转先回 200、紧接着在流里报错被误判。HTTP 错误、流 error 事件、超时判不可用。
7
+ * - **有界并发**:同时在途 ≤ `concurrency`;结果按任务下标交回,调用方自己按顺序输出。
8
+ * - **限流**:429 时并发减半(至少 1),等 `retryDelayMs` 后重试该请求一次;重试仍 429 → 停止;
9
+ * 401 / 403 → 立即停止。停止后不再发新请求,并中止在途的请求(它们的结果标记为 aborted)。
10
+ * - **恢复(加法增、乘法减)**:降并发之后,每连续 `recoverAfter`(缺省 4)次没被限流的请求,并发 +1,
11
+ * 直到回到初始并发;中途再遇 429 重新减半、计数清零。
12
+ *
13
+ * `ama models check` 不走这里:它只发一次,要看到完整回复与耗时。
14
+ */
15
+ import type { Model, ProviderRegistryApi } from "../../ai/types.js";
16
+ export declare const PROBE_TIMEOUT_MS = 15000;
17
+ export declare const DEFAULT_PROBE_CONCURRENCY = 6;
18
+ export declare const MAX_PROBE_CONCURRENCY = 16;
19
+ export declare const PROBE_SETTLE_MS = 1000;
20
+ export declare const RATE_LIMIT_RETRY_MS = 2000;
21
+ /** 降并发后,连续多少次没被限流才 +1。 */
22
+ export declare const RECOVER_AFTER = 4;
23
+ /** 鉴权失败或限流:继续只会浪费请求。 */
24
+ export declare const FATAL_STATUS: RegExp;
25
+ export interface ProbeTuning {
26
+ timeoutMs?: number | undefined;
27
+ settleMs?: number | undefined;
28
+ }
29
+ /** 一次探测请求的结果:error 为 undefined 即可用;aborted 表示被调度方中止,结果作废。 */
30
+ export interface ProbeOutcome {
31
+ error?: string;
32
+ aborted?: boolean;
33
+ }
34
+ /** 发一次最小请求,见到首个有效事件即判定并中止。 */
35
+ export declare function probeOnce(registry: ProviderRegistryApi, model: Model, apiKey: string | undefined, tuning?: ProbeTuning, signal?: AbortSignal): Promise<ProbeOutcome>;
36
+ export interface SchedulerOptions extends ProbeTuning {
37
+ concurrency?: number | undefined;
38
+ retryDelayMs?: number | undefined;
39
+ /** 429 降并发时回调(新的并发数)。 */
40
+ onThrottle?: (concurrency: number) => void;
41
+ /** 连续多少次没被限流后并发 +1(缺省 RECOVER_AFTER)。 */
42
+ recoverAfter?: number | undefined;
43
+ /** 并发回升时回调(新的并发数)。 */
44
+ onRecover?: (concurrency: number) => void;
45
+ }
46
+ /**
47
+ * 有界并发的探测调度器:`run` 按下标调度任务,任务里用 `probe` 发请求(带 429 重试与停止判定)。
48
+ */
49
+ export declare class ProbeScheduler {
50
+ private readonly registry;
51
+ private readonly apiKey;
52
+ private readonly options;
53
+ private limit;
54
+ private readonly initial;
55
+ /** 上次降并发以来连续没被限流的请求数。 */
56
+ private streak;
57
+ private readonly controller;
58
+ private stopReason;
59
+ private active;
60
+ /** 观测用:同时在途请求数的峰值。 */
61
+ peak: number;
62
+ constructor(registry: ProviderRegistryApi, apiKey: string | undefined, options?: SchedulerOptions);
63
+ get stopped(): string | undefined;
64
+ get concurrency(): number;
65
+ stop(reason: string): void;
66
+ /** 一次探测;429 降并发、退避后重试一次,仍 429 或 401 / 403 → 停止。 */
67
+ probe(model: Model): Promise<ProbeOutcome>;
68
+ /** 一次没被限流的请求:攒够 recoverAfter 次就并发 +1(不超过初始值)。 */
69
+ private recover;
70
+ /** 并发执行 `count` 个任务;`onDone` 按完成顺序回调。停止后未开始的任务不执行(结果为 undefined)。 */
71
+ run<T>(count: number, task: (index: number) => Promise<T>, onDone?: (index: number, result: T) => void): Promise<(T | undefined)[]>;
72
+ private get signal();
73
+ }
74
+ /** `--concurrency`(1–16)与 `--probe-timeout`(毫秒,1000–300000)。 */
75
+ export declare function parseProbeTuning(values: ReadonlyMap<string, string>): {
76
+ concurrency: number;
77
+ timeoutMs: number;
78
+ };
79
+ /** 预估文案:`并发 6,单次最长 15 s,预计不超过 150 s`。 */
80
+ export declare function describeProbePlan(requests: number, concurrency: number, timeoutMs: number): string;
81
+ /** 进度:TTY 下单行刷新「探测 18/60」;非 TTY 只在结束时打印一行汇总。 */
82
+ export declare class ProbeProgress {
83
+ private readonly write;
84
+ private readonly tty;
85
+ private readonly total;
86
+ private done;
87
+ private shown;
88
+ private readonly started;
89
+ constructor(write: (text: string) => void, tty: boolean, total: number);
90
+ /** 输出一行结果(TTY 下先擦掉进度行,再重画)。 */
91
+ line(text: string): void;
92
+ tick(): void;
93
+ finish(): void;
94
+ private draw;
95
+ private clear;
96
+ }
@@ -0,0 +1,264 @@
1
+ /**
2
+ * 探测的执行层(`ama providers add|refresh --probe`、`ama models discover --probe` 共用)。
3
+ *
4
+ * - **首事件即判定**:HTTP 成功、流里出现第一个内容事件(text / thinking / toolcall)或 done 即判可用,
5
+ * 立刻中止请求;只见到 `start`(推理模型可能要想很久才出第一个 delta)时再等 `settleMs`,期间没有
6
+ * error 事件也判可用——防止中转先回 200、紧接着在流里报错被误判。HTTP 错误、流 error 事件、超时判不可用。
7
+ * - **有界并发**:同时在途 ≤ `concurrency`;结果按任务下标交回,调用方自己按顺序输出。
8
+ * - **限流**:429 时并发减半(至少 1),等 `retryDelayMs` 后重试该请求一次;重试仍 429 → 停止;
9
+ * 401 / 403 → 立即停止。停止后不再发新请求,并中止在途的请求(它们的结果标记为 aborted)。
10
+ * - **恢复(加法增、乘法减)**:降并发之后,每连续 `recoverAfter`(缺省 4)次没被限流的请求,并发 +1,
11
+ * 直到回到初始并发;中途再遇 429 重新减半、计数清零。
12
+ *
13
+ * `ama models check` 不走这里:它只发一次,要看到完整回复与耗时。
14
+ */
15
+ import { UsageError } from "../args.js";
16
+ export const PROBE_TIMEOUT_MS = 15_000;
17
+ export const DEFAULT_PROBE_CONCURRENCY = 6;
18
+ export const MAX_PROBE_CONCURRENCY = 16;
19
+ export const PROBE_SETTLE_MS = 1_000;
20
+ export const RATE_LIMIT_RETRY_MS = 2_000;
21
+ /** 降并发后,连续多少次没被限流才 +1。 */
22
+ export const RECOVER_AFTER = 4;
23
+ /** 鉴权失败或限流:继续只会浪费请求。 */
24
+ export const FATAL_STATUS = /^(?:HTTP )?(?:401|403|429)\b/;
25
+ const RATE_LIMITED = /^(?:HTTP )?429\b/;
26
+ const sleep = (ms, signal) => new Promise((resolve) => {
27
+ const timer = setTimeout(done, ms);
28
+ function done() {
29
+ clearTimeout(timer);
30
+ signal?.removeEventListener("abort", done);
31
+ resolve();
32
+ }
33
+ signal?.addEventListener("abort", done, { once: true });
34
+ });
35
+ /** 发一次最小请求,见到首个有效事件即判定并中止。 */
36
+ export async function probeOnce(registry, model, apiKey, tuning = {}, signal) {
37
+ const impl = registry.getApi(model.api);
38
+ if (impl === undefined)
39
+ return { error: `协议 ${model.api} 尚未实现` };
40
+ if (signal?.aborted)
41
+ return { aborted: true };
42
+ const timeoutMs = tuning.timeoutMs ?? PROBE_TIMEOUT_MS;
43
+ const settleMs = tuning.settleMs ?? PROBE_SETTLE_MS;
44
+ const controller = new AbortController();
45
+ let timedOut = false;
46
+ const timer = setTimeout(() => {
47
+ timedOut = true;
48
+ controller.abort();
49
+ }, timeoutMs);
50
+ const onAbort = () => controller.abort();
51
+ signal?.addEventListener("abort", onAbort, { once: true });
52
+ const verdict = (outcome) => {
53
+ if (signal?.aborted)
54
+ return { aborted: true };
55
+ if (timedOut)
56
+ return { error: `超时(${Math.round(timeoutMs / 1000)} s 内没有响应)` };
57
+ return outcome;
58
+ };
59
+ try {
60
+ const stream = impl.stream(model, { messages: [{ role: "user", content: "Reply with: ok", timestamp: Date.now() }] }, {
61
+ signal: controller.signal,
62
+ ...(apiKey !== undefined ? { apiKey } : {}),
63
+ maxTokens: 16,
64
+ cacheRetention: "none",
65
+ purpose: "probe",
66
+ });
67
+ const events = stream[Symbol.asyncIterator]();
68
+ let settled;
69
+ while (true) {
70
+ const next = events.next();
71
+ const step = settled !== undefined ? await Promise.race([next, settled]) : await next;
72
+ if (step === "settled")
73
+ return verdict({});
74
+ if (step.done)
75
+ break;
76
+ const event = step.value;
77
+ if (event.type === "error")
78
+ return verdict({ error: event.message.errorMessage ?? event.reason });
79
+ if (event.type === "start") {
80
+ settled ??= sleep(settleMs, controller.signal).then(() => "settled");
81
+ continue;
82
+ }
83
+ return verdict({});
84
+ }
85
+ // 流没有逐个产出事件(实现只给 result):按最终消息判定
86
+ const message = await stream.result();
87
+ if (message.stopReason === "error" || message.stopReason === "aborted")
88
+ return verdict({ error: message.errorMessage ?? message.stopReason });
89
+ return verdict({});
90
+ }
91
+ catch (error) {
92
+ return verdict({ error: error instanceof Error ? error.message : String(error) });
93
+ }
94
+ finally {
95
+ clearTimeout(timer);
96
+ signal?.removeEventListener("abort", onAbort);
97
+ // 判定后立刻断开:不等推理模型把 16 个 token 想完
98
+ controller.abort();
99
+ }
100
+ }
101
+ /**
102
+ * 有界并发的探测调度器:`run` 按下标调度任务,任务里用 `probe` 发请求(带 429 重试与停止判定)。
103
+ */
104
+ export class ProbeScheduler {
105
+ registry;
106
+ apiKey;
107
+ options;
108
+ limit;
109
+ initial;
110
+ /** 上次降并发以来连续没被限流的请求数。 */
111
+ streak = 0;
112
+ controller = new AbortController();
113
+ stopReason;
114
+ active = 0;
115
+ /** 观测用:同时在途请求数的峰值。 */
116
+ peak = 0;
117
+ constructor(registry, apiKey, options = {}) {
118
+ this.registry = registry;
119
+ this.apiKey = apiKey;
120
+ this.options = options;
121
+ this.limit = options.concurrency ?? DEFAULT_PROBE_CONCURRENCY;
122
+ this.initial = this.limit;
123
+ }
124
+ get stopped() {
125
+ return this.stopReason;
126
+ }
127
+ get concurrency() {
128
+ return this.limit;
129
+ }
130
+ stop(reason) {
131
+ if (this.stopReason !== undefined)
132
+ return;
133
+ this.stopReason = reason;
134
+ this.controller.abort();
135
+ }
136
+ /** 一次探测;429 降并发、退避后重试一次,仍 429 或 401 / 403 → 停止。 */
137
+ async probe(model) {
138
+ if (this.stopReason !== undefined)
139
+ return { aborted: true };
140
+ let outcome = await probeOnce(this.registry, model, this.apiKey, this.options, this.signal);
141
+ if (outcome.error !== undefined && RATE_LIMITED.test(outcome.error)) {
142
+ this.limit = Math.max(1, Math.floor(this.limit / 2));
143
+ this.streak = 0;
144
+ this.options.onThrottle?.(this.limit);
145
+ await sleep(this.options.retryDelayMs ?? RATE_LIMIT_RETRY_MS, this.signal);
146
+ if (this.stopReason !== undefined)
147
+ return { aborted: true };
148
+ outcome = await probeOnce(this.registry, model, this.apiKey, this.options, this.signal);
149
+ if (outcome.error !== undefined && RATE_LIMITED.test(outcome.error)) {
150
+ this.stop(`连续 429 限流:${outcome.error}`);
151
+ return outcome;
152
+ }
153
+ }
154
+ if (outcome.error !== undefined && FATAL_STATUS.test(outcome.error))
155
+ this.stop(outcome.error);
156
+ else if (outcome.aborted !== true)
157
+ this.recover();
158
+ return outcome;
159
+ }
160
+ /** 一次没被限流的请求:攒够 recoverAfter 次就并发 +1(不超过初始值)。 */
161
+ recover() {
162
+ if (this.limit >= this.initial)
163
+ return;
164
+ this.streak++;
165
+ if (this.streak < (this.options.recoverAfter ?? RECOVER_AFTER))
166
+ return;
167
+ this.streak = 0;
168
+ this.limit++;
169
+ this.options.onRecover?.(this.limit);
170
+ }
171
+ /** 并发执行 `count` 个任务;`onDone` 按完成顺序回调。停止后未开始的任务不执行(结果为 undefined)。 */
172
+ async run(count, task, onDone) {
173
+ const results = new Array(count).fill(undefined);
174
+ let next = 0;
175
+ await new Promise((resolve, reject) => {
176
+ const pump = () => {
177
+ while (this.stopReason === undefined && next < count && this.active < this.limit) {
178
+ const index = next++;
179
+ this.active++;
180
+ this.peak = Math.max(this.peak, this.active);
181
+ task(index).then((result) => {
182
+ this.active--;
183
+ results[index] = result;
184
+ onDone?.(index, result);
185
+ pump();
186
+ }, (error) => {
187
+ this.active--;
188
+ this.stop(error instanceof Error ? error.message : String(error));
189
+ reject(error);
190
+ });
191
+ }
192
+ if (this.active === 0 && (next >= count || this.stopReason !== undefined))
193
+ resolve();
194
+ };
195
+ pump();
196
+ });
197
+ return results;
198
+ }
199
+ get signal() {
200
+ return this.controller.signal;
201
+ }
202
+ }
203
+ /** `--concurrency`(1–16)与 `--probe-timeout`(毫秒,1000–300000)。 */
204
+ export function parseProbeTuning(values) {
205
+ const int = (name, fallback, min, max) => {
206
+ const raw = values.get(name);
207
+ if (raw === undefined)
208
+ return fallback;
209
+ const n = Number(raw);
210
+ if (!Number.isInteger(n) || n < min || n > max)
211
+ throw new UsageError(`--${name} 需要 ${min}–${max} 的整数:${raw}`);
212
+ return n;
213
+ };
214
+ return {
215
+ concurrency: int("concurrency", DEFAULT_PROBE_CONCURRENCY, 1, MAX_PROBE_CONCURRENCY),
216
+ timeoutMs: int("probe-timeout", PROBE_TIMEOUT_MS, 1_000, 300_000),
217
+ };
218
+ }
219
+ /** 预估文案:`并发 6,单次最长 15 s,预计不超过 150 s`。 */
220
+ export function describeProbePlan(requests, concurrency, timeoutMs) {
221
+ const worst = Math.ceil(requests / concurrency) * Math.ceil(timeoutMs / 1000);
222
+ return `并发 ${concurrency},单次最长 ${Math.round(timeoutMs / 1000)} s,预计不超过 ${worst} s`;
223
+ }
224
+ /** 进度:TTY 下单行刷新「探测 18/60」;非 TTY 只在结束时打印一行汇总。 */
225
+ export class ProbeProgress {
226
+ write;
227
+ tty;
228
+ total;
229
+ done = 0;
230
+ shown = false;
231
+ started = Date.now();
232
+ constructor(write, tty, total) {
233
+ this.write = write;
234
+ this.tty = tty;
235
+ this.total = total;
236
+ }
237
+ /** 输出一行结果(TTY 下先擦掉进度行,再重画)。 */
238
+ line(text) {
239
+ this.clear();
240
+ this.write(text);
241
+ this.draw();
242
+ }
243
+ tick() {
244
+ this.done++;
245
+ this.draw();
246
+ }
247
+ finish() {
248
+ this.clear();
249
+ const seconds = ((Date.now() - this.started) / 1000).toFixed(1);
250
+ this.write(`探测完成 ${this.done}/${this.total},用时 ${seconds} s\n`);
251
+ }
252
+ draw() {
253
+ if (!this.tty)
254
+ return;
255
+ this.write(`\r\x1b[2K探测 ${this.done}/${this.total}`);
256
+ this.shown = true;
257
+ }
258
+ clear() {
259
+ if (!this.tty || !this.shown)
260
+ return;
261
+ this.write("\r\x1b[2K");
262
+ this.shown = false;
263
+ }
264
+ }