Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(model): resolve equivalent same-endpoint provider profiles

Chrisdfe3d7247a1c
diff --git a/packages/model-compat/README.md b/packages/model-compat/README.md
index e44ca1047a..9a95535e20 100644
--- a/packages/model-compat/README.md
+++ b/packages/model-compat/README.md
@@ -57,8 +57,8 @@ of upstream's generic conversion, with reverse transforms applied in reverse ord
`src/upstream-profiles.json` contains compatibility declarations extracted statically from all
92 entries in the pinned upstream registry. No upstream registry code or credential code is
-executed. Matching requires exact URL origin and path boundaries. Equal endpoint identities that
-cannot be distinguished are left unresolved, rather than choosing an account/provider by name.
+executed. Matching requires exact URL origin and path boundaries. Equal endpoint profiles are narrowed by the declared auth mode when available; profiles with
+identical transport policies share compatibility rules. Conflicting policies remain unresolved.
Unclassified gateways retain Cindy's existing route-specific rules; they do not inherit every
patch for models with a familiar name.
diff --git a/packages/model-compat/src/__tests__/boundaries.test.ts b/packages/model-compat/src/__tests__/boundaries.test.ts
index d21618ab7b..ef38d745f3 100644
--- a/packages/model-compat/src/__tests__/boundaries.test.ts
+++ b/packages/model-compat/src/__tests__/boundaries.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest';
+import { resolveProviderCompatibilityProfile } from '../profiles';
import { createCodexResponsesCompatibilityAdapter, prepareResponsesCompatibility, normalizeProviderRequest, chatCompatibilityCapabilities, sanitizeXaiTools } from '../index';
describe('protocol data boundaries', () => {
@@ -91,3 +92,24 @@ it('preserves an explicit native Chat reasoning disable when no provider wire ma
const input = { model: 'gpt-5.4', reasoning_effort: 'none' };
expect(normalizeProviderRequest(input, { harness: 'codex', protocol: 'openai-chat', upstreamBase: 'https://api.openai.com/v1', model: input.model })).toEqual(input);
});
+
+describe('same-endpoint provider profiles', () => {
+ const kimi = { harness: 'codex' as const, protocol: 'openai-chat' as const, upstreamBase: 'https://api.kimi.com/coding/v1', model: 'k3[1m]' };
+ it.each([undefined, 'oauth', 'api-key'] as const)('applies Kimi wire fixes with auth mode %s', authMode => {
+ const route = { ...kimi, authMode };
+ const body = { model: 'k3[1m]', temperature: 0.7, top_p: 0.9, frequency_penalty: 1, presence_penalty: 1, reasoning_effort: 'high', messages: [] };
+ expect(normalizeProviderRequest(body, route)).toEqual({ model: 'k3', messages: [] });
+ expect(chatCompatibilityCapabilities(route)).toMatchObject({ reasoningHistoryField: 'reasoning_content', toolCallReasoningPlaceholder: true });
+ expect(resolveProviderCompatibilityProfile(route)?.id).toBe(authMode === 'api-key' ? 'kimi-code' : 'kimi');
+ });
+ it('shares identical key profiles and preserves explicit bridge overrides', () => {
+ const route = { ...kimi, upstreamBase: 'https://api.fireworks.ai/inference/v1', authMode: 'api-key' as const };
+ expect(resolveProviderCompatibilityProfile(route)).not.toBeNull();
+ expect(chatCompatibilityCapabilities(kimi, { toolCallReasoningPlaceholder: false }).toolCallReasoningPlaceholder).toBe(false);
+ });
+ it('does not guess between conflicting policies or match a lookalike endpoint', () => {
+ expect(resolveProviderCompatibilityProfile({ ...kimi, upstreamBase: 'https://opencode.ai/zen/v1', authMode: 'api-key' })).toBeNull();
+ expect(resolveProviderCompatibilityProfile({ ...kimi, upstreamBase: 'https://api.kimi.com/coding/v10' })).toBeNull();
+ expect(resolveProviderCompatibilityProfile({ ...kimi, upstreamBase: 'https://api.kimi.com.example/coding/v1' })).toBeNull();
+ });
+});
diff --git a/packages/model-compat/src/profiles.ts b/packages/model-compat/src/profiles.ts
index 18eea03c56..e62795872b 100644
--- a/packages/model-compat/src/profiles.ts
+++ b/packages/model-compat/src/profiles.ts
@@ -25,6 +25,18 @@ function servesEndpoint(base: string, actual: string): boolean {
} catch { return false; }
}
+// Compare every transport declaration, ignoring only account identity metadata.
+// Sort object keys because registry declarations need not use identical key order.
+function policySignature(profile: ProviderCompatibilityProfile): string {
+ const canonical = (value: unknown): unknown => {
+ if (Array.isArray(value)) return value.map(canonical);
+ if (!isPlainObject(value)) return value;
+ return Object.fromEntries(Object.keys(value).sort().map(key => [key, canonical(value[key])]));
+ };
+ const policy = Object.fromEntries(Object.entries(profile).filter(([key]) => key !== 'id' && key !== 'authKind'));
+ return JSON.stringify(canonical(policy));
+}
+
/** Upstream provider declarations are compatibility data only: they never add accounts or models. */
export function resolveProviderCompatibilityProfile(route: CompatibilityRoute): ProviderCompatibilityProfile | null {
// OpenCodex's xAI OAuth transport rewrites the public registry base to this exact
@@ -34,9 +46,17 @@ export function resolveProviderCompatibilityProfile(route: CompatibilityRoute):
}
const matches = (profiles as ProviderCompatibilityProfile[]).filter(profile => servesEndpoint(profile.baseUrl, route.upstreamBase));
matches.sort((a, b) => b.baseUrl.length - a.baseUrl.length);
- // Multiple provider identities at the same endpoint cannot be resolved by the model label alone.
- if (matches.length > 1 && matches[0]!.baseUrl === matches[1]!.baseUrl) return null;
- return matches[0] ?? null;
+ if (!matches.length) return null;
+ let candidates = matches.filter(profile => profile.baseUrl === matches[0]!.baseUrl);
+ const authKind = route.authMode === 'api-key' ? 'key' : route.authMode;
+ if (authKind) {
+ const authenticated = candidates.filter(profile => profile.authKind === authKind);
+ if (authenticated.length) candidates = authenticated;
+ }
+ const first = candidates[0]!;
+ // Duplicate account profiles with the same wire contract share the same patches.
+ // Truly conflicting declarations remain unresolved; never guess from a model name.
+ return candidates.every(profile => policySignature(profile) === policySignature(first)) ? first : null;
}
/** OpenCodex uses exact model membership, with a colon variant falling back to its base. */