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(pi): raise unknown model output token fallback (#3486)

nanaco9e508983a202
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
index 1476fc4569..c240abf76c 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
@@ -253,6 +253,89 @@ describe('Pi provider-aware model routing', () => {
await nativeHandle.close();
});
+ it('uses explicit output caps and a context-bounded fallback for gateway and native models', async () => {
+ const availableModels: ModelDescriptor[] = [
+ {
+ id: 'gateway-fallback-model', displayName: 'Gateway fallback', contextWindow: 128_000,
+ efforts: [], defaultEffort: null,
+ },
+ {
+ id: 'gateway-explicit-model', displayName: 'Gateway explicit', contextWindow: 200_000,
+ maxOutputTokens: 90_000, efforts: [], defaultEffort: null,
+ },
+ {
+ id: 'gateway-small-context-model', displayName: 'Gateway small context', contextWindow: 32_000,
+ efforts: [], defaultEffort: null,
+ },
+ {
+ id: 'native-fallback-model', displayName: 'Native fallback', contextWindow: 200_000,
+ efforts: [], defaultEffort: null,
+ },
+ {
+ id: 'native-explicit-model', displayName: 'Native explicit', contextWindow: 200_000,
+ efforts: [], defaultEffort: null,
+ },
+ {
+ id: 'native-small-context-model', displayName: 'Native small context', contextWindow: 32_000,
+ efforts: [], defaultEffort: null,
+ },
+ ];
+ const descriptorResolver = (_providerId: string | null | undefined, modelId: string) =>
+ availableModels.find((candidate) => candidate.id === modelId)!;
+ const agent = new PiAgent({
+ auth: {
+ getState: async () => ({ authenticated: true, identity: 'test', authSource: 'api-key' as const }),
+ triggerLogin: async () => ({ authenticated: true }),
+ logout: async () => {},
+ getAuthEnv: async () => ({}),
+ },
+ runtimeConfig: { endpoint: 'http://127.0.0.1:9988' },
+ binaryPath: path.join(agentHome, 'pi'),
+ logger: noopLogger,
+ capabilityAdditions: { availableModels },
+ resolvePiAgentHome: () => agentHome,
+ resolvePiGatewayModelDescriptor: descriptorResolver,
+ resolvePiGatewayModelApi: () => 'anthropic-messages',
+ resolvePiNativeProviders: async () => ({
+ providers: [{
+ id: 'native', name: 'Native', baseUrl: 'http://native.test', api: 'openai-completions',
+ models: [
+ { id: 'native-fallback-model', contextWindow: 200_000 },
+ { id: 'native-explicit-model', contextWindow: 200_000, maxTokens: 90_000 },
+ { id: 'native-small-context-model', contextWindow: 32_000 },
+ ],
+ }],
+ env: {},
+ }),
+ });
+
+ const gatewayHandle = await agent.startSession({
+ sessionId: 'max-tokens-gateway', workingDir: cwd, model: 'gateway-fallback-model', providerId: 'openai',
+ });
+ const gatewayModels = JSON.parse(
+ readFileSync(path.join(captured.env.PI_CODING_AGENT_DIR as string, 'models.json'), 'utf8'),
+ ) as { providers: Record<string, { models?: Array<Record<string, unknown>> }> };
+ expect(gatewayModels.providers.cindy?.models).toEqual(expect.arrayContaining([
+ expect.objectContaining({ id: 'gateway-fallback-model', maxTokens: 65_536 }),
+ expect.objectContaining({ id: 'gateway-explicit-model', maxTokens: 90_000 }),
+ expect.objectContaining({ id: 'gateway-small-context-model', maxTokens: 32_000 }),
+ ]));
+ await gatewayHandle.close();
+
+ const nativeHandle = await agent.startSession({
+ sessionId: 'max-tokens-native', workingDir: cwd, model: 'native-fallback-model', providerId: 'native',
+ });
+ const nativeModels = JSON.parse(
+ readFileSync(path.join(captured.env.PI_CODING_AGENT_DIR as string, 'models.json'), 'utf8'),
+ ) as { providers: Record<string, { models?: Array<Record<string, unknown>> }> };
+ expect(nativeModels.providers.native?.models).toEqual(expect.arrayContaining([
+ expect.objectContaining({ id: 'native-fallback-model', maxTokens: 65_536 }),
+ expect.objectContaining({ id: 'native-explicit-model', maxTokens: 90_000 }),
+ expect.objectContaining({ id: 'native-small-context-model', maxTokens: 32_000 }),
+ ]));
+ await nativeHandle.close();
+ });
+
it('routes host subscriptions through PI native providers and wire model ids', async () => {
const authProviderIds: Array<string | null | undefined> = [];
let resolveProxyProviderId: (() => string | null) | undefined;
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 4883a28aec..da72247b72 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -452,6 +452,21 @@ function effortToPiThinkingLevel(effort: Effort): string {
const PI_NATIVE_THINKING_LEVELS = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'] as const;
+/**
+ * Last-resort maxTokens for models that provide no authoritative output limit.
+ * Explicit Model Access / native-provider metadata always wins. Pi separately clamps each
+ * request to the remaining context window, so keep this fallback context-bounded as well.
+ *
+ * This is deliberately a compatibility fallback rather than a claimed model capability:
+ * unknown upstreams may still have a lower output limit and should publish explicit metadata.
+ * Raising the legacy gateway/native defaults (32k/16k) to 64k prevents known long-thinking
+ * turns from being cut off solely by Cindy's synthetic value without making the fallback
+ * unbounded.
+ */
+function piMaxTokensFallback(contextWindow: number | undefined): number {
+ return contextWindow && contextWindow > 0 ? Math.min(contextWindow, 65_536) : 65_536;
+}
+
/** 从本次启动写入 models.json 的 native model 快照提取可用 effort。 */
function startupEffortsOfNativeModel(model: PiNativeModelSpec | undefined): readonly Effort[] | undefined {
if (!model) return undefined;
@@ -1443,7 +1458,7 @@ export class PiAgent extends BaseAgent {
input: supportsImageInput ? ['text', 'image'] : ['text'],
// Model Access v3 requires this value; never replace the server limit with a client guess.
contextWindow: m.contextWindow,
- maxTokens: m.maxOutputTokens && m.maxOutputTokens > 0 ? m.maxOutputTokens : 32_000,
+ maxTokens: m.maxOutputTokens && m.maxOutputTokens > 0 ? m.maxOutputTokens : piMaxTokensFallback(m.contextWindow),
// 计费单位与目录一致($/1M tokens);pi 按此自行计价,usage 事件的 cost 才有真值。
cost: {
input: m.cost?.input ?? 0,
@@ -1481,21 +1496,24 @@ export class PiAgent extends BaseAgent {
}
const nativeModels = (
np.inheritModels ? np.models.filter((model) => model.api !== undefined || model.catalogAddition === true) : np.models
- ).map((m) => ({
- id: m.wireId ?? m.id,
- name: m.name ?? m.id,
- ...(m.baseUrl ? { baseUrl: m.baseUrl } : {}),
- ...(m.headers && Object.keys(m.headers).length > 0 ? { headers: m.headers } : {}),
- ...(m.api ? { api: m.api } : {}),
- reasoning: m.reasoning ?? false,
- ...(m.thinkingLevelMap ? { thinkingLevelMap: { ...m.thinkingLevelMap } } : {}),
- input: m.input ?? ['text'],
- contextWindow: m.contextWindow && m.contextWindow > 0 ? m.contextWindow : 128_000,
- maxTokens: m.maxTokens && m.maxTokens > 0 ? m.maxTokens : 16_000,
- ...(m.cost ? { cost: structuredClone(m.cost) } : {}),
- ...(m.compat ? { compat: structuredClone(m.compat) } : {}),
- ...(m.samplingParams ? { samplingParams: structuredClone(m.samplingParams) } : {}),
- }));
+ ).map((m) => {
+ const contextWindow = m.contextWindow && m.contextWindow > 0 ? m.contextWindow : 128_000;
+ return {
+ id: m.wireId ?? m.id,
+ name: m.name ?? m.id,
+ ...(m.baseUrl ? { baseUrl: m.baseUrl } : {}),
+ ...(m.headers && Object.keys(m.headers).length > 0 ? { headers: m.headers } : {}),
+ ...(m.api ? { api: m.api } : {}),
+ reasoning: m.reasoning ?? false,
+ ...(m.thinkingLevelMap ? { thinkingLevelMap: { ...m.thinkingLevelMap } } : {}),
+ input: m.input ?? ['text'],
+ contextWindow,
+ maxTokens: m.maxTokens && m.maxTokens > 0 ? m.maxTokens : piMaxTokensFallback(contextWindow),
+ ...(m.cost ? { cost: structuredClone(m.cost) } : {}),
+ ...(m.compat ? { compat: structuredClone(m.compat) } : {}),
+ ...(m.samplingParams ? { samplingParams: structuredClone(m.samplingParams) } : {}),
+ };
+ });
if (!np.inheritModels && !np.api) {
throw new Error(`pi: native provider '${np.id}' has no default api`);
}