automationAgentConfigOptions.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { describe, expect, it } from "vitest";
  2. import type { ModelSelectGroup } from "@/ModelConfigSelect.js";
  3. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  4. import {
  5. buildAutomationModelSelectGroups,
  6. buildAutomationModelSelection,
  7. buildAutomationModeOption,
  8. buildAutomationThoughtLevelOption,
  9. resolveAutomationPreferredModelValue,
  10. resolveAutomationModelItem,
  11. resolveAutomationModelTriggerLabel,
  12. resolveAutomationThoughtLevelUpdate,
  13. } from "@/settings/automationAgentConfigOptions.js";
  14. import { encodeCustomModelValue } from "@/lib/zcodeCustomModelValue.js";
  15. const groups: ModelSelectGroup[] = [
  16. {
  17. key: "provider:demo",
  18. label: "Demo",
  19. items: [
  20. {
  21. key: "provider:demo:model-a",
  22. value: encodeCustomModelValue("demo", "model-a"),
  23. name: "model-a",
  24. },
  25. {
  26. key: "provider:demo:model-b",
  27. value: encodeCustomModelValue("demo", "model-b"),
  28. name: "model-b",
  29. },
  30. ],
  31. },
  32. ];
  33. describe("automation model/thought linkage", () => {
  34. it("新建任务把 Host preferredSelection 初始化为具体模型身份", () => {
  35. expect(
  36. resolveAutomationPreferredModelValue({
  37. revision: 2,
  38. preferredSelection: { providerId: "demo", modelId: "model-b" },
  39. providers: [],
  40. }),
  41. ).toBe(encodeCustomModelValue("demo", "model-b"));
  42. });
  43. it("Host 没有 preferredSelection 时不伪造默认模型", () => {
  44. expect(resolveAutomationPreferredModelValue({ revision: 2, providers: [] })).toBeNull();
  45. });
  46. it("Registry View 已发布时不再用旧 Provider 重做 entitlement 过滤", () => {
  47. expect(
  48. buildAutomationModelSelectGroups({
  49. selectedProvider: "glm",
  50. labels: {},
  51. registrySelectionView: { revision: 1, providers: [] },
  52. }),
  53. ).toEqual([]);
  54. });
  55. it("新建任务模型列表直接使用 Registry 已裁决的模型集合", () => {
  56. const groups = buildAutomationModelSelectGroups({
  57. selectedProvider: "glm",
  58. labels: {},
  59. registrySelectionView: {
  60. revision: 1,
  61. providers: [
  62. {
  63. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  64. providerName: "BigModel Coding",
  65. config: {
  66. access: {
  67. type: "zhipu-account",
  68. },
  69. api: {
  70. type: "anthropic-messages",
  71. baseUrl: "https://open.bigmodel.cn/api/anthropic",
  72. },
  73. models: ["coding-only-model"],
  74. },
  75. models: [{ modelId: "coding-only-model", config: {} }],
  76. },
  77. ],
  78. },
  79. });
  80. expect(groups.flatMap((group) => group.items.map((item) => item.name))).toEqual([
  81. "coding-only-model",
  82. ]);
  83. });
  84. it("使用当前候选档位,但非法旧值留空,不采用 preview 默认值", () => {
  85. const option = buildAutomationThoughtLevelOption(
  86. {
  87. id: "thought_level",
  88. name: "Thought Level",
  89. category: "thought_level",
  90. type: "select",
  91. currentValue: "high",
  92. options: [
  93. { value: "minimal", name: "Minimal" },
  94. { value: "high", name: "High" },
  95. ],
  96. },
  97. "medium",
  98. );
  99. expect(option?.currentValue).toBe("");
  100. expect(option?.options?.map((item) => item.value)).toEqual(["minimal", "high"]);
  101. });
  102. it("兼容会话内创建任务保存的 provider/model 值", () => {
  103. expect(resolveAutomationModelItem(groups, "demo/model-a")?.name).toBe("model-a");
  104. });
  105. it("模型触发器从 Registry View 显示 providerName/modelName", () => {
  106. expect(
  107. resolveAutomationModelTriggerLabel({
  108. modelGroups: groups,
  109. modelSelectionView: {
  110. revision: 1,
  111. providers: [
  112. {
  113. providerId: "demo",
  114. providerName: "Demo Provider",
  115. config: {},
  116. models: [{ modelId: "model-a", config: {} }],
  117. },
  118. ],
  119. },
  120. modelValue: "demo/model-a",
  121. fallbackLabel: "选择模型",
  122. }),
  123. ).toBe("Demo Provider/model-a");
  124. });
  125. it("Registry View 已发布时使用其中的 Provider label", () => {
  126. expect(
  127. resolveAutomationModelTriggerLabel({
  128. modelGroups: groups,
  129. modelSelectionView: {
  130. revision: 1,
  131. providers: [
  132. {
  133. providerId: "demo",
  134. providerName: "Registry Provider",
  135. config: {
  136. api: {
  137. type: "openai-chat-completions",
  138. baseUrl: "https://registry.example.com/v1",
  139. },
  140. access: { type: "api-key", apiKey: "test-key" },
  141. models: ["model-a"],
  142. },
  143. models: [{ modelId: "model-a", config: {} }],
  144. },
  145. ],
  146. },
  147. modelValue: "demo/model-a",
  148. fallbackLabel: "选择模型",
  149. }),
  150. ).toBe("Registry Provider/model-a");
  151. });
  152. it("模型触发器继续遵循会话侧内置 family 的裁剪规则", () => {
  153. const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan;
  154. const modelGroups: ModelSelectGroup[] = [
  155. {
  156. key: `provider:${providerId}`,
  157. label: "BigModel Coding Plan",
  158. items: [
  159. {
  160. key: `provider:${providerId}:glm-5.2`,
  161. value: encodeCustomModelValue(providerId, "glm-5.2"),
  162. name: "glm-5.2",
  163. },
  164. ],
  165. },
  166. ];
  167. expect(
  168. resolveAutomationModelTriggerLabel({
  169. modelGroups,
  170. modelValue: encodeCustomModelValue(providerId, "glm-5.2"),
  171. fallbackLabel: "选择模型",
  172. }),
  173. ).toBe("glm-5.2");
  174. });
  175. it("已保存模型不在当前候选中时仍回显模型名", () => {
  176. const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan;
  177. const staleModel = encodeCustomModelValue(providerId, "glm-5.2");
  178. expect(
  179. resolveAutomationModelTriggerLabel({
  180. modelGroups: [],
  181. modelValue: staleModel,
  182. fallbackLabel: "选择模型",
  183. }),
  184. ).toBe("glm-5.2");
  185. });
  186. it("runtime provider 包装值只在模型名唯一时映射回菜单项", () => {
  187. expect(resolveAutomationModelItem(groups, "zcode-openai-compatible/model-a")?.value).toBe(
  188. encodeCustomModelValue("demo", "model-a"),
  189. );
  190. });
  191. it("当前值仍合法时保留用户选择", () => {
  192. const option = buildAutomationThoughtLevelOption(
  193. {
  194. id: "thought_level",
  195. name: "Thought Level",
  196. category: "thought_level",
  197. type: "select",
  198. currentValue: "medium",
  199. options: [
  200. { value: "low", name: "Low" },
  201. { value: "medium", name: "Medium" },
  202. { value: "high", name: "High" },
  203. ],
  204. },
  205. "high",
  206. );
  207. expect(option?.currentValue).toBe("high");
  208. expect(option?.options?.map((item) => item.value)).toEqual(["low", "medium", "high"]);
  209. });
  210. it("没有具体档位时留空,不补最高档", () => {
  211. const option = buildAutomationThoughtLevelOption(
  212. {
  213. id: "thought_level",
  214. name: "Thought Level",
  215. category: "thought_level",
  216. type: "select",
  217. options: [
  218. { value: "low", name: "Low" },
  219. { value: "high", name: "High" },
  220. { value: "max", name: "Max" },
  221. ],
  222. },
  223. "",
  224. );
  225. expect(option?.currentValue).toBe("");
  226. });
  227. it.each(["edit", "guarded", "yolo"])("编辑态权限 %s 原样回显,菜单用 guarded 替代 edit", (mode) => {
  228. const option = buildAutomationModeOption(mode);
  229. expect(option.currentValue).toBe(mode);
  230. expect(option.options?.map((item) => item.value)).toEqual(["build", "guarded", "plan", "yolo"]);
  231. });
  232. it("ModelSelection 只保存模型身份与 reasoning,不继承旧输出预算", () => {
  233. const previous = {
  234. providerId: "provider-a",
  235. modelId: "model-a",
  236. options: { reasoningLevel: "low", maxOutputTokens: 12_000 },
  237. } as const;
  238. expect(
  239. buildAutomationModelSelection({
  240. selected: { providerId: "provider-a", modelId: "model-a" },
  241. reasoningLevel: "high",
  242. previous,
  243. }),
  244. ).toEqual({
  245. providerId: "provider-a",
  246. modelId: "model-a",
  247. options: { reasoningLevel: "high" },
  248. });
  249. expect(
  250. buildAutomationModelSelection({
  251. selected: { providerId: "provider-b", modelId: "model-b" },
  252. previous,
  253. }),
  254. ).toEqual({ providerId: "provider-b", modelId: "model-b" });
  255. });
  256. it("编辑切到无 Think 模型时显式清空旧档位", () => {
  257. expect(
  258. resolveAutomationThoughtLevelUpdate({
  259. editing: true,
  260. explicitlyChanged: false,
  261. modelChanged: true,
  262. resolvedThoughtLevel: "",
  263. thoughtLevel: "max",
  264. }),
  265. ).toBeNull();
  266. });
  267. it("新建任务的无 Think 模型不写入 thoughtLevel", () => {
  268. expect(
  269. resolveAutomationThoughtLevelUpdate({
  270. editing: false,
  271. explicitlyChanged: false,
  272. modelChanged: true,
  273. resolvedThoughtLevel: "",
  274. thoughtLevel: "max",
  275. }),
  276. ).toBeUndefined();
  277. });
  278. it("把目标模型当前档位固化到定时任务", () => {
  279. expect(
  280. resolveAutomationThoughtLevelUpdate({
  281. editing: false,
  282. explicitlyChanged: false,
  283. modelChanged: true,
  284. resolvedThoughtLevel: "high",
  285. thoughtLevel: "high",
  286. }),
  287. ).toBe("high");
  288. });
  289. it("用户显式选择与默认相同的档位时仍保存 pin", () => {
  290. expect(
  291. resolveAutomationThoughtLevelUpdate({
  292. editing: false,
  293. explicitlyChanged: true,
  294. modelChanged: true,
  295. resolvedThoughtLevel: "high",
  296. thoughtLevel: "high",
  297. }),
  298. ).toBe("high");
  299. });
  300. it("编辑未触碰模型和档位时保留已有显式 pin", () => {
  301. expect(
  302. resolveAutomationThoughtLevelUpdate({
  303. editing: true,
  304. explicitlyChanged: false,
  305. modelChanged: false,
  306. persistedReasoningLevel: "max",
  307. resolvedThoughtLevel: "max",
  308. thoughtLevel: "max",
  309. }),
  310. ).toBe("max");
  311. });
  312. });