automationAgentConfigOptions.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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("模型触发器继续遵循会话侧内置 family 的裁剪规则", () => {
  126. const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan;
  127. const modelGroups: ModelSelectGroup[] = [
  128. {
  129. key: `provider:${providerId}`,
  130. label: "BigModel Coding Plan",
  131. items: [
  132. {
  133. key: `provider:${providerId}:glm-5.2`,
  134. value: encodeCustomModelValue(providerId, "glm-5.2"),
  135. name: "glm-5.2",
  136. },
  137. ],
  138. },
  139. ];
  140. expect(
  141. resolveAutomationModelTriggerLabel({
  142. modelGroups,
  143. modelValue: encodeCustomModelValue(providerId, "glm-5.2"),
  144. fallbackLabel: "选择模型",
  145. }),
  146. ).toBe("glm-5.2");
  147. });
  148. it("已保存模型不在当前候选中时仍回显模型名", () => {
  149. const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan;
  150. const staleModel = encodeCustomModelValue(providerId, "glm-5.2");
  151. expect(
  152. resolveAutomationModelTriggerLabel({
  153. modelGroups: [],
  154. modelValue: staleModel,
  155. fallbackLabel: "选择模型",
  156. }),
  157. ).toBe("glm-5.2");
  158. });
  159. it("runtime provider 包装值只在模型名唯一时映射回菜单项", () => {
  160. expect(resolveAutomationModelItem(groups, "zcode-openai-compatible/model-a")?.value).toBe(
  161. encodeCustomModelValue("demo", "model-a"),
  162. );
  163. });
  164. it("当前值仍合法时保留用户选择", () => {
  165. const option = buildAutomationThoughtLevelOption(
  166. {
  167. id: "thought_level",
  168. name: "Thought Level",
  169. category: "thought_level",
  170. type: "select",
  171. currentValue: "medium",
  172. options: [
  173. { value: "low", name: "Low" },
  174. { value: "medium", name: "Medium" },
  175. { value: "high", name: "High" },
  176. ],
  177. },
  178. "high",
  179. );
  180. expect(option?.currentValue).toBe("high");
  181. expect(option?.options?.map((item) => item.value)).toEqual(["low", "medium", "high"]);
  182. });
  183. it("没有具体档位时留空,不补最高档", () => {
  184. const option = buildAutomationThoughtLevelOption(
  185. {
  186. id: "thought_level",
  187. name: "Thought Level",
  188. category: "thought_level",
  189. type: "select",
  190. options: [
  191. { value: "low", name: "Low" },
  192. { value: "high", name: "High" },
  193. { value: "max", name: "Max" },
  194. ],
  195. },
  196. "",
  197. );
  198. expect(option?.currentValue).toBe("");
  199. });
  200. it.each(["edit", "guarded", "yolo"])(
  201. "编辑态权限 %s 原样回显,菜单用 guarded 替代 edit",
  202. (mode) => {
  203. const option = buildAutomationModeOption(mode);
  204. expect(option.currentValue).toBe(mode);
  205. expect(option.options?.map((item) => item.value)).toEqual([
  206. "build",
  207. "guarded",
  208. "plan",
  209. "yolo",
  210. ]);
  211. },
  212. );
  213. it("ModelSelection 只保存模型身份与 reasoning,不继承旧输出预算", () => {
  214. const previous = {
  215. providerId: "provider-a",
  216. modelId: "model-a",
  217. options: { reasoningLevel: "low", maxOutputTokens: 12_000 },
  218. } as const;
  219. expect(
  220. buildAutomationModelSelection({
  221. selected: { providerId: "provider-a", modelId: "model-a" },
  222. reasoningLevel: "high",
  223. previous,
  224. }),
  225. ).toEqual({
  226. providerId: "provider-a",
  227. modelId: "model-a",
  228. options: { reasoningLevel: "high" },
  229. });
  230. expect(
  231. buildAutomationModelSelection({
  232. selected: { providerId: "provider-b", modelId: "model-b" },
  233. previous,
  234. }),
  235. ).toEqual({ providerId: "provider-b", modelId: "model-b" });
  236. });
  237. it("编辑切到无 Think 模型时显式清空旧档位", () => {
  238. expect(
  239. resolveAutomationThoughtLevelUpdate({
  240. editing: true,
  241. explicitlyChanged: false,
  242. modelChanged: true,
  243. resolvedThoughtLevel: "",
  244. thoughtLevel: "max",
  245. }),
  246. ).toBeNull();
  247. });
  248. it("新建任务的无 Think 模型不写入 thoughtLevel", () => {
  249. expect(
  250. resolveAutomationThoughtLevelUpdate({
  251. editing: false,
  252. explicitlyChanged: false,
  253. modelChanged: true,
  254. resolvedThoughtLevel: "",
  255. thoughtLevel: "max",
  256. }),
  257. ).toBeUndefined();
  258. });
  259. it("把目标模型当前档位固化到定时任务", () => {
  260. expect(
  261. resolveAutomationThoughtLevelUpdate({
  262. editing: false,
  263. explicitlyChanged: false,
  264. modelChanged: true,
  265. resolvedThoughtLevel: "high",
  266. thoughtLevel: "high",
  267. }),
  268. ).toBe("high");
  269. });
  270. it("用户显式选择与默认相同的档位时仍保存 pin", () => {
  271. expect(
  272. resolveAutomationThoughtLevelUpdate({
  273. editing: false,
  274. explicitlyChanged: true,
  275. modelChanged: true,
  276. resolvedThoughtLevel: "high",
  277. thoughtLevel: "high",
  278. }),
  279. ).toBe("high");
  280. });
  281. it("编辑未触碰模型和档位时保留已有显式 pin", () => {
  282. expect(
  283. resolveAutomationThoughtLevelUpdate({
  284. editing: true,
  285. explicitlyChanged: false,
  286. modelChanged: false,
  287. persistedReasoningLevel: "max",
  288. resolvedThoughtLevel: "max",
  289. thoughtLevel: "max",
  290. }),
  291. ).toBe("max");
  292. });
  293. });