@seaveyon/harness-switch 0.34.0 → 0.34.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.
@@ -10388,6 +10388,8 @@ var en_default = {
10388
10388
  intro: "The configurations below reference channels this save removes. Confirming switches them to the selected replacement and keeps them linked to this template; the shown diffs are exactly what will be written.",
10389
10389
  computing: "Computing differences…",
10390
10390
  affectedTitle: "Affected configurations",
10391
+ affectedCount: "{{count}} affected references",
10392
+ draftItem: "Tool model draft",
10391
10393
  configColumn: "Configuration",
10392
10394
  stateColumn: "State",
10393
10395
  replacementColumn: "Replacement",
@@ -10397,12 +10399,17 @@ var en_default = {
10397
10399
  uniqueCandidate: "only compatible option, preselected",
10398
10400
  pickPrompt: "Choose a replacement",
10399
10401
  noCandidate: "No compatible channel",
10402
+ chooseCandidate: "Choose a replacement",
10403
+ replacementRequired: "Choose a compatible replacement for these references.",
10404
+ collectionCollision: "These tool models resolve to the same destination. Choose different channels or models.",
10400
10405
  noCandidateHint: "{{profile}} has no compatible replacement. Go back to the draft and pick a channel for it.",
10401
10406
  templateDiff: "Template changes",
10402
10407
  confirm: "Confirm overwrite",
10403
10408
  stale: "The preview is stale or the state has changed. Refresh and try again.",
10404
10409
  refresh: "Refresh preview",
10405
- collectionHint: "This configuration is managed by the tool-models flow; its field updates belong there, and for now it blocks the whole overwrite.",
10410
+ collectionHint: "Tool model channel references are saved with the template. To update the tool’s models and startup settings, continue to the tool model preview and apply the changes.",
10411
+ reviewCollection: "Preview {{tool}} model updates",
10412
+ reviewCollectionHint: "Review the saved tool model list, model overrides, and startup selection.",
10406
10413
  blockedTitle: "There are unresolved blockers. Nothing will be written.",
10407
10414
  retry: "Retry this item",
10408
10415
  resumeTitle: "Continue updating affected configurations",
@@ -12072,9 +12079,11 @@ var zh_CN_default = {
12072
12079
  },
12073
12080
  overwrite: {
12074
12081
  title: "覆盖引用的配置",
12075
- intro: "以下配置引用了将被移除的渠道。确认后将改用选定的替代渠道并保持关联本模板;差异即为最终写入内容。",
12082
+ intro: "核对渠道替换和配置差异,确认后保留这些引用与模板的关联。",
12076
12083
  computing: "正在计算差异…",
12077
12084
  affectedTitle: "受影响的配置",
12085
+ affectedCount: "{{count}} 个受影响的引用",
12086
+ draftItem: "工具模型草稿",
12078
12087
  configColumn: "配置",
12079
12088
  stateColumn: "状态",
12080
12089
  replacementColumn: "替换为",
@@ -12084,12 +12093,17 @@ var zh_CN_default = {
12084
12093
  uniqueCandidate: "唯一兼容渠道,已预选",
12085
12094
  pickPrompt: "选择替代渠道",
12086
12095
  noCandidate: "无兼容渠道",
12096
+ chooseCandidate: "待选择渠道",
12097
+ replacementRequired: "请为以下引用选择兼容的替代渠道。",
12098
+ collectionCollision: "以下工具模型的替换目标重复,请选择不同渠道或模型。",
12087
12099
  noCandidateHint: "{{profile}} 没有兼容替代,请回到草稿为它补选渠道。",
12088
12100
  templateDiff: "模板变更",
12089
12101
  confirm: "确认覆盖",
12090
12102
  stale: "预览已过期或状态已变化,请刷新后重试。",
12091
12103
  refresh: "刷新预览",
12092
- collectionHint: "该配置由工具模型托管,字段更新需走工具模型流程;本轮会阻止整单覆盖。",
12104
+ collectionHint: "工具模型的渠道引用随模板保存。要更新工具中的模型和启动设置,请继续预览并应用工具模型。",
12105
+ reviewCollection: "预览 {{tool}} 工具模型更新",
12106
+ reviewCollectionHint: "将预览已保存的工具模型列表、模型覆盖项及启动选择。",
12093
12107
  blockedTitle: "存在无法解决的阻塞项,覆盖不会写入任何内容。",
12094
12108
  retry: "重试该项",
12095
12109
  resumeTitle: "继续更新受影响的配置",
@@ -26817,9 +26831,8 @@ var modelFavoriteSchema = favoriteFields.extend({
26817
26831
  updatedAt: exports_external.iso.datetime()
26818
26832
  }).superRefine(validateFavorite);
26819
26833
  var favoriteStoreSchema = exports_external.object({ schemaVersion: exports_external.literal(1), favorites: exports_external.array(modelFavoriteSchema).max(1000) }).refine((value) => new Set(value.favorites.map((favorite) => favorite.id)).size === value.favorites.length);
26820
- var overwriteRemapSchema = exports_external.object({
26834
+ var overwriteRemapFields = exports_external.object({
26821
26835
  harness: exports_external.enum(HARNESS_IDS),
26822
- profile: exports_external.string().trim().min(1).max(120),
26823
26836
  fromConnectionId: exports_external.uuid(),
26824
26837
  resolution: exports_external.enum(["auto", "chosen"]),
26825
26838
  connectionId: exports_external.uuid(),
@@ -26827,6 +26840,17 @@ var overwriteRemapSchema = exports_external.object({
26827
26840
  ignorePreference: exports_external.boolean().default(false),
26828
26841
  allowAuthOverwrite: exports_external.boolean().optional()
26829
26842
  });
26843
+ var overwriteReferenceFields = [
26844
+ { profile: exports_external.string().trim().min(1).max(120), itemId: exports_external.never().optional() },
26845
+ { itemId: exports_external.uuid(), profile: exports_external.never().optional() }
26846
+ ];
26847
+ var overwriteRemapSchema = exports_external.union([
26848
+ overwriteRemapFields.extend(overwriteReferenceFields[0]),
26849
+ overwriteRemapFields.extend(overwriteReferenceFields[1])
26850
+ ]);
26851
+ function overwriteReferenceKey(ref) {
26852
+ return ref.profile === undefined ? `${ref.harness}/item/${ref.itemId}` : `${ref.harness}/profile/${ref.profile}`;
26853
+ }
26830
26854
  var updateFavoriteRequestSchema = exports_external.object({
26831
26855
  name: favoriteFields.shape.name.optional(),
26832
26856
  notes: exports_external.string().max(4096).optional(),
@@ -36541,21 +36565,29 @@ function resolveOverwriteRemaps({
36541
36565
  const affected = [];
36542
36566
  const resolvedRemaps = [];
36543
36567
  const absorbed = new Map;
36544
- const usedExplicit = new Set(remaps.map((remap) => `${remap.harness}/${remap.profile}/${remap.fromConnectionId}`));
36568
+ const unused = new Map(remaps.map((remap) => [overwriteReferenceKey(remap), remap]));
36569
+ if (unused.size !== remaps.length) {
36570
+ problems.push({ code: ERROR_CODES.favoritePlanStale });
36571
+ }
36545
36572
  for (const ref of refs) {
36546
- if (draft.connections.some((connection) => connection.id === ref.link.connectionId)) {
36573
+ const retained = draft.connections.find((connection) => connection.id === ref.connectionId);
36574
+ if (retained && pickProtocolForHarness(retained, ref.harness)) {
36547
36575
  continue;
36548
36576
  }
36549
- const previous = current.connections.find((connection) => connection.id === ref.link.connectionId);
36550
- const fallback = ref.link;
36577
+ const previous = current.connections.find((connection) => connection.id === ref.connectionId);
36551
36578
  const old = {
36552
- providerId: previous?.providerId ?? fallback.providerId ?? "",
36553
- endpointKey: previous?.endpointKey ?? fallback.providerEndpoint ?? "",
36554
- requestModelId: previous?.requestModelId ?? fallback.requestModelId ?? ""
36579
+ providerId: previous?.providerId ?? ref.link?.baseline.providerId ?? "",
36580
+ endpointKey: previous?.endpointKey ?? ref.link?.baseline.providerEndpoint ?? "",
36581
+ requestModelId: previous?.requestModelId ?? ref.link?.baseline.model ?? ""
36555
36582
  };
36556
- const explicit = remaps.find((remap) => remap.harness === ref.harness && remap.profile === ref.name && remap.fromConnectionId === ref.link.connectionId);
36557
- usedExplicit.delete(`${ref.harness}/${ref.name}/${ref.link.connectionId}`);
36558
- const sameAccount = draft.connections.filter((connection) => connection.providerId === old.providerId && connection.endpointKey === old.endpointKey && connection.requestModelId === old.requestModelId);
36583
+ const key = overwriteReferenceKey(ref);
36584
+ const explicit = unused.get(key);
36585
+ unused.delete(key);
36586
+ if (explicit && explicit.fromConnectionId !== ref.connectionId) {
36587
+ problems.push({ code: ERROR_CODES.favoritePlanStale });
36588
+ continue;
36589
+ }
36590
+ const sameAccount = draft.connections.filter((connection) => connection.id !== ref.connectionId && connection.providerId === old.providerId && connection.endpointKey === old.endpointKey && connection.requestModelId === old.requestModelId);
36559
36591
  let target;
36560
36592
  let resolution = "none";
36561
36593
  if (explicit) {
@@ -36564,15 +36596,15 @@ function resolveOverwriteRemaps({
36564
36596
  problems.push({ code: ERROR_CODES.favoriteEndpointMissing });
36565
36597
  continue;
36566
36598
  }
36567
- if (!pickProtocolForHarness(target, ref.harness)) {
36568
- problems.push({ code: ERROR_CODES.favoriteProtocolUnsupported });
36569
- continue;
36570
- }
36571
36599
  const isEquivalent = sameAccount.length === 1 && sameAccount[0].id === target.id;
36572
36600
  resolution = isEquivalent ? "auto" : "chosen";
36573
36601
  if (isEquivalent) {
36574
36602
  target = absorb(target, previous, ref.harness, absorbed);
36575
36603
  }
36604
+ if (!pickProtocolForHarness(target, ref.harness)) {
36605
+ problems.push({ code: ERROR_CODES.favoriteProtocolUnsupported });
36606
+ continue;
36607
+ }
36576
36608
  } else if (sameAccount.length === 1) {
36577
36609
  target = absorb(sameAccount[0], previous, ref.harness, absorbed);
36578
36610
  if (!target || !pickProtocolForHarness(target, ref.harness)) {
@@ -36581,14 +36613,14 @@ function resolveOverwriteRemaps({
36581
36613
  }
36582
36614
  resolution = "auto";
36583
36615
  }
36584
- const collectionManaged = ref.link.collectionOverrides !== undefined;
36616
+ const identity2 = ref.profile === undefined ? { itemId: ref.itemId } : { profile: ref.profile };
36585
36617
  affected.push({
36586
36618
  harness: ref.harness,
36587
- profile: ref.name,
36619
+ ...identity2,
36588
36620
  inUse: false,
36589
- collectionManaged,
36621
+ collectionManaged: ref.itemId !== undefined || ref.link?.collectionOverrides !== undefined,
36590
36622
  from: {
36591
- connectionId: ref.link.connectionId,
36623
+ connectionId: ref.connectionId,
36592
36624
  providerId: old.providerId,
36593
36625
  endpointKey: old.endpointKey,
36594
36626
  requestModelId: old.requestModelId
@@ -36601,8 +36633,8 @@ function resolveOverwriteRemaps({
36601
36633
  }
36602
36634
  resolvedRemaps.push({
36603
36635
  harness: ref.harness,
36604
- profile: ref.name,
36605
- fromConnectionId: ref.link.connectionId,
36636
+ ...identity2,
36637
+ fromConnectionId: ref.connectionId,
36606
36638
  resolution,
36607
36639
  connectionId: target.id,
36608
36640
  overwriteDiverged: explicit?.overwriteDiverged ?? false,
@@ -36610,7 +36642,17 @@ function resolveOverwriteRemaps({
36610
36642
  allowAuthOverwrite: explicit?.allowAuthOverwrite
36611
36643
  });
36612
36644
  }
36613
- if (usedExplicit.size) {
36645
+ const referenced = new Set(refs.map((ref) => ref.connectionId));
36646
+ for (const previous of current.connections) {
36647
+ if (referenced.has(previous.id) || draft.connections.some((connection) => connection.id === previous.id)) {
36648
+ continue;
36649
+ }
36650
+ const survivors = draft.connections.filter((connection) => connection.providerId === previous.providerId && connection.endpointKey === previous.endpointKey && connection.requestModelId === previous.requestModelId);
36651
+ if (survivors.length === 1) {
36652
+ absorb(survivors[0], previous, undefined, absorbed);
36653
+ }
36654
+ }
36655
+ if (unused.size) {
36614
36656
  problems.push({ code: ERROR_CODES.favoritePlanStale });
36615
36657
  }
36616
36658
  return {
@@ -36628,7 +36670,7 @@ function absorb(candidate, previous, harness, absorbed) {
36628
36670
  return candidate;
36629
36671
  }
36630
36672
  const base = absorbed.get(candidate.id) ?? candidate;
36631
- const missingHarnessProtocol = pickProtocolForHarness(base, harness) ? [] : [FAVORITE_PROTOCOL_SUPPORT[harness][0]];
36673
+ const missingHarnessProtocol = !harness || pickProtocolForHarness(base, harness) ? [] : [FAVORITE_PROTOCOL_SUPPORT[harness][0]];
36632
36674
  const merged = {
36633
36675
  ...base,
36634
36676
  ...syncConnectionProtocols([
@@ -36835,45 +36877,6 @@ class ToolModelsService {
36835
36877
  });
36836
36878
  return preview;
36837
36879
  }
36838
- remapFavoriteConnections(favoriteId, remaps) {
36839
- if (!remaps.size) {
36840
- return;
36841
- }
36842
- const store = this.read();
36843
- let changed = false;
36844
- for (const harness of toolModelsHarnessSchema.options) {
36845
- const state = store.collections[harness];
36846
- if (!state) {
36847
- continue;
36848
- }
36849
- let dirty = false;
36850
- const items = state.draft.items.map((item) => {
36851
- if (item.source.kind !== "favorite" || item.source.favoriteId !== favoriteId) {
36852
- return item;
36853
- }
36854
- const nextId = remaps.get(item.source.connectionId);
36855
- if (!nextId || nextId === item.source.connectionId) {
36856
- return item;
36857
- }
36858
- dirty = true;
36859
- return {
36860
- ...item,
36861
- source: { ...item.source, connectionId: nextId }
36862
- };
36863
- });
36864
- if (dirty) {
36865
- changed = true;
36866
- store.collections[harness] = {
36867
- ...state,
36868
- revision: state.revision + 1,
36869
- draft: { ...state.draft, items }
36870
- };
36871
- }
36872
- }
36873
- if (changed) {
36874
- this.files.writeJson(this.environment.files.toolModels, store);
36875
- }
36876
- }
36877
36880
  favoriteDraftItems(favoriteId) {
36878
36881
  const store = this.read();
36879
36882
  return toolModelsHarnessSchema.options.flatMap((harness) => {
@@ -36886,6 +36889,62 @@ class ToolModelsService {
36886
36889
  }));
36887
36890
  });
36888
36891
  }
36892
+ checkFavoriteRemaps(favoriteId, resolved, entries) {
36893
+ const problems = [];
36894
+ const store = this.read();
36895
+ const favorites = new Map(this.favorites.list().map((favorite) => [favorite.id, favorite]));
36896
+ const remaps = new Map(entries.map((entry) => [`${entry.harness}/${entry.itemId}`, entry.connectionId]));
36897
+ for (const harness of toolModelsHarnessSchema.options) {
36898
+ const state = store.collections[harness];
36899
+ const seen = new Map;
36900
+ for (const item of state?.draft.items ?? []) {
36901
+ if (item.source.kind !== "favorite") {
36902
+ continue;
36903
+ }
36904
+ const source = item.source;
36905
+ const affected = source.favoriteId === favoriteId;
36906
+ const before = favorites.get(source.favoriteId)?.connections.find((entry) => entry.id === source.connectionId);
36907
+ const connectionId = remaps.get(`${harness}/${item.id}`) ?? source.connectionId;
36908
+ const connection = affected ? resolved.connections.find((entry) => entry.id === connectionId) : before;
36909
+ const protocol = connection && pickProtocolForHarness(connection, harness);
36910
+ const subject = {
36911
+ harness,
36912
+ profile: state?.applied.find((entry) => entry.id === item.id)?.profile,
36913
+ model: before ? `${before.label} · ${before.requestModelId}` : source.connectionId
36914
+ };
36915
+ if (!connection || !protocol) {
36916
+ if (affected) {
36917
+ problems.push({
36918
+ ...subject,
36919
+ code: connection ? ERROR_CODES.favoriteProtocolUnsupported : ERROR_CODES.favoriteEndpointMissing
36920
+ });
36921
+ }
36922
+ continue;
36923
+ }
36924
+ const identity2 = `${collectionRoute(connection, protocol)}/${connection.requestModelId}`;
36925
+ const other = seen.get(identity2);
36926
+ if (other && (affected || other.affected)) {
36927
+ problems.push({
36928
+ ...subject,
36929
+ code: ERROR_CODES.toolModelsConflict,
36930
+ otherModel: other.profile ?? other.model
36931
+ });
36932
+ }
36933
+ seen.set(identity2, { ...subject, affected });
36934
+ if (affected) {
36935
+ try {
36936
+ this.projectModel(harness, resolved, connection, item);
36937
+ } catch (error52) {
36938
+ if (!(error52 instanceof HttpError)) {
36939
+ throw error52;
36940
+ }
36941
+ problems.push({ ...subject, code: error52.code });
36942
+ }
36943
+ }
36944
+ }
36945
+ }
36946
+ return problems;
36947
+ }
36889
36948
  remapFavoriteConnectionsScoped(favoriteId, entries) {
36890
36949
  if (!entries.length) {
36891
36950
  return;
@@ -37014,29 +37073,13 @@ class ToolModelsService {
37014
37073
  if (!protocol) {
37015
37074
  throw failure2(ERROR_CODES.favoriteProtocolUnsupported);
37016
37075
  }
37017
- const route = `${provider.id}/${endpoint.key}/${protocol}`;
37076
+ const route = collectionRoute(connection, protocol);
37018
37077
  const identity2 = `${route}/${connection.requestModelId}`;
37019
37078
  if (seen.has(identity2)) {
37020
37079
  throw failure2(ERROR_CODES.toolModelsConflict);
37021
37080
  }
37022
37081
  seen.add(identity2);
37023
- const effective = {
37024
- ...connection,
37025
- factOverrides: { ...connection.factOverrides, ...item.factOverrides },
37026
- preferenceOverrides: { ...connection.preferenceOverrides, ...item.preferenceOverrides }
37027
- };
37028
- if (!createFavoriteRequestSchema.safeParse({
37029
- ...favorite,
37030
- connections: favorite.connections.map((entry) => entry.id === effective.id ? effective : entry)
37031
- }).success) {
37032
- throw new HttpError(400, ERROR_CODES.toolModelsFactsInvalid, {
37033
- code: ERROR_CODES.toolModelsFactsInvalid
37034
- });
37035
- }
37036
- const projection = adapter.projectFavorite(favorite, effective);
37037
- if (projection.blockers.length) {
37038
- throw failure2(projection.blockers[0].code);
37039
- }
37082
+ const projection = this.projectModel(harness, favorite, connection, item);
37040
37083
  const baseName = `${favorite.name} · ${connection.label}`.replace(/[\\/]/g, "-").slice(0, 120);
37041
37084
  const existingName = state.applied.find((entry) => entry.id === item.id)?.profile;
37042
37085
  const name = existingName ?? (this.profiles.get(harness, baseName) || names.has(baseName) ? `${baseName.slice(0, 110)}-${item.id.slice(0, 8)}` : baseName);
@@ -37099,6 +37142,26 @@ class ToolModelsService {
37099
37142
  };
37100
37143
  });
37101
37144
  }
37145
+ projectModel(harness, favorite, connection, item) {
37146
+ const effective = {
37147
+ ...connection,
37148
+ factOverrides: { ...connection.factOverrides, ...item.factOverrides },
37149
+ preferenceOverrides: { ...connection.preferenceOverrides, ...item.preferenceOverrides }
37150
+ };
37151
+ if (!createFavoriteRequestSchema.safeParse({
37152
+ ...favorite,
37153
+ connections: favorite.connections.map((entry) => entry.id === effective.id ? effective : entry)
37154
+ }).success) {
37155
+ throw new HttpError(400, ERROR_CODES.toolModelsFactsInvalid, {
37156
+ code: ERROR_CODES.toolModelsFactsInvalid
37157
+ });
37158
+ }
37159
+ const projection = this.adapters.get(harness).projectFavorite(favorite, effective);
37160
+ if (projection.blockers.length) {
37161
+ throw failure2(projection.blockers[0].code);
37162
+ }
37163
+ return projection;
37164
+ }
37102
37165
  compare(harness, revision) {
37103
37166
  const state = this.get(harness);
37104
37167
  if (state.revision !== revision) {
@@ -37157,6 +37220,9 @@ ToolModelsService = __decorateElement(_init, 0, "ToolModelsService", _dec, ToolM
37157
37220
  __runInitializers(_init, 1, ToolModelsService);
37158
37221
  __decoratorMetadata(_init, ToolModelsService);
37159
37222
  let _ToolModelsService = ToolModelsService;
37223
+ function collectionRoute(connection, protocol) {
37224
+ return `${connection.providerId}/${connection.endpointKey}/${protocol}`;
37225
+ }
37160
37226
  function hash2(value) {
37161
37227
  return createHash8("sha256").update(JSON.stringify(value)).digest("hex");
37162
37228
  }
@@ -37392,57 +37458,35 @@ class ModelFavoriteApplyService {
37392
37458
  if (favorite.revision !== request.expectedRevision) {
37393
37459
  throw failure3(ERROR_CODES.favoriteRevisionConflict);
37394
37460
  }
37395
- const resolution = resolveOverwriteRemaps({
37396
- current: favorite,
37397
- draft: request.draft,
37398
- refs: this.linkedReferences(favoriteId),
37399
- remaps: request.remaps
37400
- });
37401
- if (resolution.problems.length) {
37402
- throw failure3(resolution.problems[0].code);
37403
- }
37404
- const resolved = createFavoriteRequestSchema.parse(resolution.resolved);
37405
- const synthesized = {
37406
- ...resolved,
37407
- id: favorite.id,
37408
- revision: favorite.revision,
37409
- createdAt: favorite.createdAt,
37410
- updatedAt: favorite.updatedAt
37411
- };
37412
- const affected = resolution.affected.map((row) => ({
37413
- ...row,
37414
- inUse: this.activation.getActive(row.harness)?.name === row.profile
37415
- }));
37416
- const blockers = [];
37417
- for (const row of affected) {
37418
- if (row.collectionManaged && row.inUse) {
37419
- blockers.push({ code: ERROR_CODES.toolModelsConflict });
37420
- }
37421
- if (!row.connectionId) {
37422
- blockers.push({ code: ERROR_CODES.favoriteConnectionInUse });
37423
- }
37424
- }
37425
- const toolModelEntries = this.resolveToolModelRemaps(synthesized, affected, blockers);
37461
+ const { resolution, affected, blockers, toolModelEntries } = this.resolveOverwrite(favorite, request.draft, request.remaps);
37462
+ const synthesized = { ...favorite, ...resolution.resolved };
37426
37463
  const items = [];
37427
37464
  for (const remap of resolution.remaps) {
37428
37465
  const row = affected.find((entry) => entry.harness === remap.harness && entry.profile === remap.profile);
37429
- if (!row || row.collectionManaged || !row.connectionId) {
37466
+ if (!row || row.collectionManaged || remap.profile === undefined) {
37430
37467
  continue;
37431
37468
  }
37432
- const computed = this.computeItem(synthesized, {
37433
- harness: remap.harness,
37434
- connectionId: remap.connectionId,
37435
- profile: remap.profile,
37436
- existing: true,
37437
- mode: row.inUse ? "activate" : "save",
37438
- overwriteDiverged: remap.overwriteDiverged,
37439
- ignorePreference: remap.ignorePreference,
37440
- allowAuthOverwrite: remap.allowAuthOverwrite
37441
- });
37442
- items.push({
37443
- public: computed.public,
37444
- fingerprint: this.fingerprintOf(synthesized, computed.public)
37445
- });
37469
+ try {
37470
+ const computed = this.computeItem(synthesized, {
37471
+ harness: remap.harness,
37472
+ connectionId: remap.connectionId,
37473
+ profile: remap.profile,
37474
+ existing: true,
37475
+ mode: row.inUse ? "activate" : "save",
37476
+ overwriteDiverged: remap.overwriteDiverged,
37477
+ ignorePreference: remap.ignorePreference,
37478
+ allowAuthOverwrite: remap.allowAuthOverwrite
37479
+ });
37480
+ items.push({
37481
+ public: computed.public,
37482
+ fingerprint: this.fingerprintOf(synthesized, computed.public)
37483
+ });
37484
+ } catch (error52) {
37485
+ if (!(error52 instanceof HttpError)) {
37486
+ throw error52;
37487
+ }
37488
+ blockers.push({ code: error52.code, harness: row.harness, profile: row.profile });
37489
+ }
37446
37490
  }
37447
37491
  for (const [id, record2] of this.overwrites) {
37448
37492
  if (record2.expiresAt < Date.now()) {
@@ -37474,6 +37518,7 @@ class ModelFavoriteApplyService {
37474
37518
  blockers,
37475
37519
  toolModelEntries,
37476
37520
  toolModelsHash: this.toolModelsStateHash(),
37521
+ managedProfilesHash: this.managedProfilesHash(affected),
37477
37522
  items
37478
37523
  });
37479
37524
  return preview;
@@ -37492,17 +37537,11 @@ class ModelFavoriteApplyService {
37492
37537
  if (record2.blockers.length) {
37493
37538
  throw failure3(record2.blockers[0].code);
37494
37539
  }
37495
- const resolution = resolveOverwriteRemaps({
37496
- current: this.favorites.get(params.favoriteId),
37497
- draft: params.next,
37498
- refs: this.linkedReferences(params.favoriteId),
37499
- remaps: params.remaps
37500
- });
37501
- const stale = resolution.problems.length || canon(resolution.affected.map((row) => ({
37502
- ...row,
37503
- inUse: this.activation.getActive(row.harness)?.name === row.profile
37504
- }))) !== canon(record2.affected) || canon(resolution.remaps) !== canon(record2.remaps) || canon(createFavoriteRequestSchema.parse(resolution.resolved)) !== canon(createFavoriteRequestSchema.parse(record2.resolved));
37505
- if (stale) {
37540
+ if (this.toolModelsStateHash() !== record2.toolModelsHash || this.managedProfilesHash(record2.affected) !== record2.managedProfilesHash) {
37541
+ throw failure3(ERROR_CODES.favoritePlanStale);
37542
+ }
37543
+ const { resolution, affected, blockers, toolModelEntries } = this.resolveOverwrite(this.favorites.get(params.favoriteId), params.next, params.remaps);
37544
+ if (blockers.length || canon(affected) !== canon(record2.affected) || canon(toolModelEntries) !== canon(record2.toolModelEntries) || canon(resolution.remaps) !== canon(record2.remaps) || canon(resolution.resolved) !== canon(createFavoriteRequestSchema.parse(record2.resolved))) {
37506
37545
  throw failure3(ERROR_CODES.favoritePlanStale);
37507
37546
  }
37508
37547
  for (const item of record2.items) {
@@ -37513,9 +37552,6 @@ class ModelFavoriteApplyService {
37513
37552
  throw failure3(ERROR_CODES.favoritePlanStale);
37514
37553
  }
37515
37554
  }
37516
- if (this.toolModelsStateHash() !== record2.toolModelsHash) {
37517
- throw failure3(ERROR_CODES.favoritePlanStale);
37518
- }
37519
37555
  return {
37520
37556
  resolved: record2.resolved,
37521
37557
  remaps: record2.remaps,
@@ -37525,35 +37561,61 @@ class ModelFavoriteApplyService {
37525
37561
  consumeOverwrite(approvalId) {
37526
37562
  this.overwrites.delete(approvalId);
37527
37563
  }
37528
- resolveToolModelRemaps(resolved, affected, problems) {
37529
- const entries = [];
37530
- const identities = new Map;
37531
- for (const draft of this.toolModels.favoriteDraftItems(resolved.id)) {
37532
- if (resolved.connections.some((connection) => connection.id === draft.connectionId)) {
37533
- continue;
37534
- }
37535
- const row = affected.find((entry) => entry.harness === draft.harness && entry.profile === draft.profile);
37536
- const target = row?.connectionId ? resolved.connections.find((connection) => connection.id === row.connectionId) : undefined;
37537
- if (!row?.connectionId || !target) {
37538
- problems.push({ code: ERROR_CODES.toolModelsConflict });
37539
- continue;
37540
- }
37541
- const protocol = pickProtocolForHarness(target, draft.harness);
37542
- if (!protocol) {
37543
- problems.push({ code: ERROR_CODES.favoriteProtocolUnsupported });
37544
- continue;
37564
+ prepareRegularUpdate(favorite, draft) {
37565
+ const { resolution, affected, blockers, toolModelEntries } = this.resolveOverwrite(favorite, draft, []);
37566
+ if (blockers.length || affected.some((row) => row.collectionManaged && row.profile !== undefined)) {
37567
+ throw failure3(ERROR_CODES.favoriteConnectionInUse);
37568
+ }
37569
+ return { resolved: resolution.resolved, remaps: resolution.remaps, toolModelEntries };
37570
+ }
37571
+ resolveOverwrite(favorite, draft, remaps) {
37572
+ const profiles = this.linkedReferences(favorite.id);
37573
+ const drafts = this.toolModels.favoriteDraftItems(favorite.id);
37574
+ const owners = drafts.map((item) => {
37575
+ const orphanedLegacySource = item.itemId === item.connectionId && !favorite.connections.some((connection) => connection.id === item.connectionId);
37576
+ return {
37577
+ item,
37578
+ owner: profiles.find((ref) => ref.harness === item.harness && ref.profile === item.profile && (ref.connectionId === item.connectionId || orphanedLegacySource) && ref.link?.collectionOverrides !== undefined)
37579
+ };
37580
+ });
37581
+ const refs = [
37582
+ ...profiles,
37583
+ ...owners.flatMap(({ item, owner }) => owner ? [] : [{ harness: item.harness, itemId: item.itemId, connectionId: item.connectionId }])
37584
+ ];
37585
+ const resolution = resolveOverwriteRemaps({ current: favorite, draft, refs, remaps });
37586
+ if (resolution.problems.length) {
37587
+ throw failure3(resolution.problems[0].code);
37588
+ }
37589
+ const resolved = createFavoriteRequestSchema.safeParse(resolution.resolved);
37590
+ const affected = resolution.affected.map((row) => ({
37591
+ ...row,
37592
+ inUse: row.profile !== undefined && this.activation.getActive(row.harness)?.name === row.profile
37593
+ }));
37594
+ const choices = new Map(affected.map((row) => [overwriteReferenceKey(row), row.connectionId]));
37595
+ const toolModelEntries = owners.flatMap(({ item, owner }) => {
37596
+ const connectionId = choices.get(overwriteReferenceKey(owner ?? { harness: item.harness, itemId: item.itemId }));
37597
+ return connectionId ? [{ harness: item.harness, itemId: item.itemId, connectionId }] : [];
37598
+ });
37599
+ const blockers = affected.flatMap((row) => row.connectionId ? [] : [
37600
+ {
37601
+ code: ERROR_CODES.favoriteConnectionInUse,
37602
+ harness: row.harness,
37603
+ profile: row.profile,
37604
+ model: row.from.requestModelId || row.from.connectionId
37545
37605
  }
37546
- const identity2 = `${target.providerId}/${target.endpointKey}/${protocol}/${target.requestModelId}`;
37547
- const seen = identities.get(draft.harness) ?? new Set;
37548
- if (seen.has(identity2)) {
37549
- problems.push({ code: ERROR_CODES.toolModelsConflict });
37550
- continue;
37606
+ ]);
37607
+ if (!resolved.success) {
37608
+ blockers.push({ code: ERROR_CODES.favoriteConnectionInUse });
37609
+ } else {
37610
+ resolution.resolved = resolved.data;
37611
+ if (blockers.length === 0) {
37612
+ blockers.push(...this.toolModels.checkFavoriteRemaps(favorite.id, resolved.data, toolModelEntries));
37551
37613
  }
37552
- seen.add(identity2);
37553
- identities.set(draft.harness, seen);
37554
- entries.push({ harness: draft.harness, itemId: draft.itemId, connectionId: target.id });
37555
37614
  }
37556
- return entries;
37615
+ return { resolution, affected, blockers, toolModelEntries };
37616
+ }
37617
+ managedProfilesHash(affected) {
37618
+ return hash3(affected.flatMap((row) => row.collectionManaged && row.profile !== undefined ? [this.profiles.fingerprint(row.harness, row.profile)] : []));
37557
37619
  }
37558
37620
  toolModelsStateHash() {
37559
37621
  return hash3(this.files.readOptional(this.environment.files.toolModels) ?? null);
@@ -37562,11 +37624,9 @@ class ModelFavoriteApplyService {
37562
37624
  return HARNESS_IDS.flatMap((harness) => this.profiles.peek(harness).flatMap((profile) => profile.modelFavorite?.favoriteId === favoriteId ? [
37563
37625
  {
37564
37626
  harness,
37565
- name: profile.name,
37566
- link: {
37567
- connectionId: profile.modelFavorite.connectionId,
37568
- collectionOverrides: profile.modelFavorite.collectionOverrides
37569
- }
37627
+ profile: profile.name,
37628
+ connectionId: profile.modelFavorite.connectionId,
37629
+ link: profile.modelFavorite
37570
37630
  }
37571
37631
  ] : []));
37572
37632
  }
@@ -37785,95 +37845,14 @@ class ModelFavoriteService {
37785
37845
  update(id, input, session) {
37786
37846
  this.store.assertRevision(id, input.expectedRevision);
37787
37847
  const current = this.store.get(id);
37788
- let next = createFavoriteRequestSchema.parse({ ...current, ...input });
37848
+ const next = createFavoriteRequestSchema.parse({ ...current, ...input });
37789
37849
  this.validateConnections(next);
37790
37850
  if (input.overwriteApprovalId || input.connectionRemaps?.length) {
37791
37851
  return this.updateOverwrite(id, current, next, input, session);
37792
37852
  }
37793
- const remaps = [];
37794
- const absorbed = new Map;
37795
- const absorbRemoved = (previous, harness) => {
37796
- const survivor = sameAccountModel(previous, next.connections);
37797
- if (!survivor) {
37798
- return;
37799
- }
37800
- const base = absorbed.get(survivor.id) ?? survivor;
37801
- const merged = {
37802
- ...base,
37803
- ...syncConnectionProtocols([
37804
- ...connectionProtocols(base),
37805
- ...connectionProtocols(previous),
37806
- ...harness && !pickProtocolForHarness(base, harness) ? [FAVORITE_PROTOCOL_SUPPORT[harness][0]] : []
37807
- ])
37808
- };
37809
- absorbed.set(merged.id, merged);
37810
- return merged;
37811
- };
37812
- for (const ref of this.references(id)) {
37813
- if (next.connections.some((connection) => connection.id === ref.link.connectionId)) {
37814
- continue;
37815
- }
37816
- const previous = current.connections.find((connection) => connection.id === ref.link.connectionId);
37817
- const target = previous ?? {
37818
- providerId: ref.link.baseline.providerId,
37819
- endpointKey: ref.link.baseline.providerEndpoint,
37820
- requestModelId: ref.link.baseline.model,
37821
- protocol: undefined,
37822
- protocols: undefined
37823
- };
37824
- const merged = absorbRemoved(target, ref.harness);
37825
- if (!merged || !pickProtocolForHarness(merged, ref.harness)) {
37826
- throw new HttpError(409, ERROR_CODES.favoriteConnectionInUse, {
37827
- code: ERROR_CODES.favoriteConnectionInUse
37828
- });
37829
- }
37830
- remaps.push({ ref, connectionId: merged.id });
37831
- }
37832
- const connectionRemaps = new Map(remaps.map(({ ref, connectionId }) => [ref.link.connectionId, connectionId]));
37833
- for (const previous of current.connections) {
37834
- if (next.connections.some((connection) => connection.id === previous.id)) {
37835
- continue;
37836
- }
37837
- if (connectionRemaps.has(previous.id)) {
37838
- continue;
37839
- }
37840
- const merged = absorbRemoved(previous);
37841
- if (merged) {
37842
- connectionRemaps.set(previous.id, merged.id);
37843
- }
37844
- }
37845
- if (absorbed.size) {
37846
- next = createFavoriteRequestSchema.parse({
37847
- ...next,
37848
- connections: next.connections.map((connection) => absorbed.get(connection.id) ?? connection)
37849
- });
37850
- this.validateConnections(next);
37851
- for (const { ref, connectionId } of remaps) {
37852
- const connection = next.connections.find((entry) => entry.id === connectionId);
37853
- if (!connection || !pickProtocolForHarness(connection, ref.harness)) {
37854
- throw new HttpError(409, ERROR_CODES.favoriteConnectionInUse, {
37855
- code: ERROR_CODES.favoriteConnectionInUse
37856
- });
37857
- }
37858
- }
37859
- }
37860
- return this.backups.protect("change", () => this.liveWrite.transaction({
37861
- kind: "favorite-apply",
37862
- harness: remaps[0]?.ref.harness ?? "claude",
37863
- profile: current.name,
37864
- writes: [],
37865
- metadata: connectionRemaps.size ? ["favorites", "profiles", "toolModels"] : ["favorites"]
37866
- }, () => {
37867
- const updated = this.store.update(id, next, input.expectedRevision);
37868
- for (const { ref, connectionId } of remaps) {
37869
- this.profiles.setFavoriteLink(ref.harness, ref.name, {
37870
- ...ref.link,
37871
- connectionId
37872
- });
37873
- }
37874
- this.toolModels.remapFavoriteConnections(id, connectionRemaps);
37875
- return updated;
37876
- }), { action: "update", name: current.name });
37853
+ const prepared = this.apply.prepareRegularUpdate(current, next);
37854
+ this.validateConnections(prepared.resolved);
37855
+ return this.commitUpdate(current, input.expectedRevision, prepared);
37877
37856
  }
37878
37857
  remove(id, revision) {
37879
37858
  this.store.assertRevision(id, revision);
@@ -37899,7 +37878,12 @@ class ModelFavoriteService {
37899
37878
  next,
37900
37879
  session
37901
37880
  });
37902
- const updated = this.backups.protect("change", () => this.liveWrite.transaction({
37881
+ const updated = this.commitUpdate(current, input.expectedRevision, prepared);
37882
+ this.apply.consumeOverwrite(input.overwriteApprovalId);
37883
+ return updated;
37884
+ }
37885
+ commitUpdate(current, revision, prepared) {
37886
+ return this.backups.protect("change", () => this.liveWrite.transaction({
37903
37887
  kind: "favorite-apply",
37904
37888
  harness: prepared.remaps[0]?.harness ?? "claude",
37905
37889
  profile: current.name,
@@ -37910,8 +37894,11 @@ class ModelFavoriteService {
37910
37894
  ...prepared.toolModelEntries.length ? ["toolModels"] : []
37911
37895
  ]
37912
37896
  }, () => {
37913
- const saved = this.store.update(id, prepared.resolved, input.expectedRevision);
37897
+ const saved = this.store.update(current.id, prepared.resolved, revision);
37914
37898
  for (const remap of prepared.remaps) {
37899
+ if (remap.profile === undefined) {
37900
+ continue;
37901
+ }
37915
37902
  const link = this.profiles.get(remap.harness, remap.profile)?.modelFavorite;
37916
37903
  if (link) {
37917
37904
  this.profiles.setFavoriteLink(remap.harness, remap.profile, {
@@ -37920,11 +37907,9 @@ class ModelFavoriteService {
37920
37907
  });
37921
37908
  }
37922
37909
  }
37923
- this.toolModels.remapFavoriteConnectionsScoped(id, prepared.toolModelEntries);
37910
+ this.toolModels.remapFavoriteConnectionsScoped(current.id, prepared.toolModelEntries);
37924
37911
  return saved;
37925
37912
  }), { action: "update", name: current.name });
37926
- this.apply.consumeOverwrite(input.overwriteApprovalId);
37927
- return updated;
37928
37913
  }
37929
37914
  sourceFingerprint(harness, name) {
37930
37915
  const profile = this.profiles.get(harness, name);
@@ -38060,9 +38045,6 @@ ModelFavoriteService = __decorateElement(_init, 0, "ModelFavoriteService", _dec,
38060
38045
  __runInitializers(_init, 1, ModelFavoriteService);
38061
38046
  __decoratorMetadata(_init, ModelFavoriteService);
38062
38047
  let _ModelFavoriteService = ModelFavoriteService;
38063
- function sameAccountModel(previous, connections) {
38064
- return connections.find((connection) => connection.providerId === previous.providerId && connection.endpointKey === previous.endpointKey && connection.requestModelId === previous.requestModelId);
38065
- }
38066
38048
 
38067
38049
  // src/http/routes/harnesses.ts
38068
38050
  function createHarnessRoutes(services) {