diff --git a/apps/web/src/components/settings/ProviderInstanceCard.tsx b/apps/web/src/components/settings/ProviderInstanceCard.tsx index cd5455cc19556..474328e5bf37a 100644 --- a/apps/web/src/components/settings/ProviderInstanceCard.tsx +++ b/apps/web/src/components/settings/ProviderInstanceCard.tsx @@ -47,6 +47,7 @@ import { Button } from "../ui/button"; import { DraftInput } from "../ui/draft-input"; import { SecretSourceInput, type SecretSource } from "./SecretSourceInput"; import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; +import { RefreshIcon } from "../ui/refresh-icon"; import { Switch } from "../ui/switch"; import { ManagedControl, ManagedNotice } from "../ManagedPolicy"; import { managedInstanceLocks, useManagedPolicy } from "../../state/managedPolicy"; @@ -731,6 +732,8 @@ interface ProviderInstanceCardProps { readonly onRunUpdate?: (() => void) | undefined; readonly onInstallRecommended?: (() => void) | undefined; readonly isUpdating?: boolean | undefined; + readonly onRefreshModels?: (() => void) | undefined; + readonly isRefreshingModels?: boolean | undefined; readonly onAcceptUrlAuth?: ((action: AcpRegistryUrlAuthAction) => void) | undefined; readonly environmentId?: EnvironmentId | undefined; readonly secretSources?: ReadonlyArray; @@ -802,6 +805,8 @@ export function ProviderInstanceCard({ onRunUpdate, onInstallRecommended, isUpdating = false, + onRefreshModels, + isRefreshingModels = false, onAcceptUrlAuth, environmentId, acpProjects = EMPTY_ACP_PROJECTS, @@ -1485,7 +1490,25 @@ export function ProviderInstanceCard({ ) : null} {driverOption !== undefined ? ( - + + + {isRefreshingModels ? Refreshing models : Refresh models} + + ) : undefined + } + >

diff --git a/apps/web/src/components/settings/ProviderSettingsPanel.environment.browser.test.tsx b/apps/web/src/components/settings/ProviderSettingsPanel.environment.browser.test.tsx index af279ee3c132a..0c634490e132f 100644 --- a/apps/web/src/components/settings/ProviderSettingsPanel.environment.browser.test.tsx +++ b/apps/web/src/components/settings/ProviderSettingsPanel.environment.browser.test.tsx @@ -593,6 +593,21 @@ describe("EnvironmentProviderSettings instance removal", () => { }), ); }); + + it("refreshes models for the selected provider instance", async () => { + await setup(); + await setProviders([provider()]); + await using _screen = await render(panel({ targetInstanceId: codexId })); + + await userEvent.click(page.getByRole("button", { name: "Refresh models" })); + + await vi.waitFor(() => + expect(state.refresh).toHaveBeenCalledWith({ + environmentId, + input: { instanceId: codexId, refreshModels: true }, + }), + ); + }); }); describe("EnvironmentProviderSettings health check interval", () => { diff --git a/apps/web/src/components/settings/ProviderSettingsPanel.tsx b/apps/web/src/components/settings/ProviderSettingsPanel.tsx index acad8f7665643..e3dd952d12a79 100644 --- a/apps/web/src/components/settings/ProviderSettingsPanel.tsx +++ b/apps/web/src/components/settings/ProviderSettingsPanel.tsx @@ -635,8 +635,12 @@ export function EnvironmentProviderSettings({ const [updatingProviderInstanceIds, setUpdatingProviderInstanceIds] = useState< ReadonlySet >(() => new Set()); + const [refreshingModelInstanceIds, setRefreshingModelInstanceIds] = useState< + ReadonlySet + >(() => new Set()); const refreshingRef = useRef(false); const updatingInstanceIdsRef = useRef>(new Set()); + const refreshingModelInstanceIdsRef = useRef>(new Set()); const acceptUrlAuthentication = useStableCallback( (instanceId: ProviderInstanceId, action: AcpRegistryUrlAuthAction) => { @@ -706,6 +710,48 @@ export function EnvironmentProviderSettings({ })(); }; + const refreshProviderModels = useStableCallback(async (instanceId: ProviderInstanceId) => { + if ( + refreshingModelInstanceIdsRef.current.has(instanceId) || + !readEnvironmentScope(environmentId, AuthOrchestrationReadScope) + ) + return; + refreshingModelInstanceIdsRef.current.add(instanceId); + setRefreshingModelInstanceIds((previous) => new Set(previous).add(instanceId)); + const result = await refreshServerProviders({ + environmentId, + input: { instanceId, refreshModels: true }, + }); + if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + console.warn("Failed to refresh provider models", { + operation: "refresh-provider-models", + environmentId, + instanceId, + ...safeErrorLogAttributes(error), + }); + toastManager.add( + stackedThreadToast({ + type: "error", + title: gt("Could not refresh models"), + description: + error instanceof Error + ? (errorDisplayMessage(error, resolveNow) ?? error.message) + : gt("The provider model list could not be refreshed."), + }), + ); + } + refreshingModelInstanceIdsRef.current.delete(instanceId); + setRefreshingModelInstanceIds((previous) => { + if (!previous.has(instanceId)) { + return previous; + } + const next = new Set(previous); + next.delete(instanceId); + return next; + }); + }); + const runProviderUpdate = useStableCallback( async ( candidate: Pick, @@ -1086,6 +1132,7 @@ export function EnvironmentProviderSettings({ const stableToggleModelFavorite = useStableCallback(toggleModelFavorite); const actions: ProviderInstanceRowActions = { acceptUrlAuthentication, + refreshProviderModels, runProviderUpdate, updateInstance, enableInstance, @@ -1117,8 +1164,10 @@ export function EnvironmentProviderSettings({ selected={mode === "list" && selectedRow?.instanceId === row.instanceId} readOnly={readOnly} canWriteSettings={canWriteSettings} + canRefreshModels={canRefreshProviders} liveProvider={serverProviders.find((candidate) => candidate.instanceId === row.instanceId)} isUpdateDispatching={updatingProviderInstanceIds.has(row.instanceId)} + isRefreshingModels={isRefreshingProviders || refreshingModelInstanceIds.has(row.instanceId)} showProjectOverride={showProjectOverride} projectOverrideValue={ showProjectOverride @@ -1391,6 +1440,7 @@ interface ProviderInstanceRowActions { instanceId: ProviderInstanceId, action: AcpRegistryUrlAuthAction, ) => void; + readonly refreshProviderModels: (instanceId: ProviderInstanceId) => Promise; readonly runProviderUpdate: ( candidate: Pick, targetVersion?: string, @@ -1433,8 +1483,10 @@ function ProviderInstanceRow({ selected, readOnly, canWriteSettings, + canRefreshModels, liveProvider, isUpdateDispatching, + isRefreshingModels, showProjectOverride, projectOverrideValue, projectOverrideEffectiveEnabled, @@ -1453,8 +1505,10 @@ function ProviderInstanceRow({ readonly selected: boolean; readonly readOnly: boolean; readonly canWriteSettings: boolean; + readonly canRefreshModels: boolean; readonly liveProvider: ServerProvider | undefined; readonly isUpdateDispatching: boolean; + readonly isRefreshingModels: boolean; readonly showProjectOverride: boolean; readonly projectOverrideValue: boolean | undefined; readonly projectOverrideEffectiveEnabled: boolean; @@ -1511,6 +1565,12 @@ function ProviderInstanceRow({ onAcceptUrlAuth={ readOnly ? undefined : (action) => actions.acceptUrlAuthentication(instanceId, action) } + onRefreshModels={ + mode === "editor" && canRefreshModels + ? () => void actions.refreshProviderModels(instanceId) + : undefined + } + isRefreshingModels={isRefreshingModels} instanceId={instanceId} instance={instance} driverOption={driverOption} diff --git a/apps/web/src/i18n/translations/de.json b/apps/web/src/i18n/translations/de.json index d7a12f6096981..8a1a9bda82162 100644 --- a/apps/web/src/i18n/translations/de.json +++ b/apps/web/src/i18n/translations/de.json @@ -9025,6 +9025,8 @@ }, ", das in diesem Build nicht verfügbar ist. Die Konfiguration bleibt erhalten." ], + "a9b5d2852719fddc": "Modelle werden aktualisiert", + "4811262c294ce7d5": "Modelle aktualisieren", "b534d4cd528ede39": "Modelleinstellungen und benutzerdefinierte Modelle werden in der ausgewählten Umgebung gespeichert und mit verbundenen Geräten geteilt.", "caaef21e8722857d": "Schnellmodus", "a860db2b1bb3aa31": "Kein Reasoning", @@ -9136,6 +9138,8 @@ "9cd6647baae39b79": "Aktualisiere den Anbieter und starte die Authentifizierung erneut.", "34e971a4cd3cfdf3": "Authentifizierung konnte nicht fortgesetzt werden", "25617239c4886378": "Die Authentifizierungsanfrage ist abgelaufen.", + "254dc4b4151094ef": "Modelle konnten nicht aktualisiert werden", + "114f22c0e3900fad": "Die Modellliste des Anbieters konnte nicht aktualisiert werden.", "1bc94053b36b3f40": "{provider} konnte nicht aktualisiert werden", "5b39dc5c8173ee94": "Der Update-Befehl des Anbieters konnte nicht gestartet werden.", "53e1e3591fc1abc9": "Anbieterinstanz konnte nicht aktualisiert werden", diff --git a/apps/web/src/i18n/translations/en.json b/apps/web/src/i18n/translations/en.json index bffc4eb39db8d..9dcd2ef9e914b 100644 --- a/apps/web/src/i18n/translations/en.json +++ b/apps/web/src/i18n/translations/en.json @@ -9033,6 +9033,8 @@ }, ", which is not available in this build. Its configuration is preserved." ], + "a9b5d2852719fddc": "Refreshing models", + "4811262c294ce7d5": "Refresh models", "b534d4cd528ede39": "Model preferences and custom models are saved on the selected environment and shared across connected devices.", "caaef21e8722857d": "Fast mode", "a860db2b1bb3aa31": "No reasoning", @@ -9144,6 +9146,8 @@ "9cd6647baae39b79": "Refresh the provider and start the authentication flow again.", "34e971a4cd3cfdf3": "Could not continue authentication", "25617239c4886378": "The authentication request expired.", + "254dc4b4151094ef": "Could not refresh models", + "114f22c0e3900fad": "The provider model list could not be refreshed.", "1bc94053b36b3f40": "Could not update {provider}", "5b39dc5c8173ee94": "The provider update command could not be started.", "53e1e3591fc1abc9": "Could not update provider instance", diff --git a/apps/web/src/i18n/translations/es.json b/apps/web/src/i18n/translations/es.json index fcd532f1fd825..7277489e1ebd2 100644 --- a/apps/web/src/i18n/translations/es.json +++ b/apps/web/src/i18n/translations/es.json @@ -9023,6 +9023,8 @@ }, ", que no está disponible en esta compilación. Se conserva su configuración." ], + "a9b5d2852719fddc": "Actualizando modelos", + "4811262c294ce7d5": "Actualizar modelos", "b534d4cd528ede39": "Las preferencias de modelos y los modelos personalizados se guardan en el entorno seleccionado y se comparten entre los dispositivos conectados.", "caaef21e8722857d": "Modo rápido", "a860db2b1bb3aa31": "Sin razonamiento", @@ -9134,6 +9136,8 @@ "9cd6647baae39b79": "Actualiza el proveedor y vuelve a iniciar el flujo de autenticación.", "34e971a4cd3cfdf3": "No se pudo continuar la autenticación", "25617239c4886378": "La solicitud de autenticación caducó.", + "254dc4b4151094ef": "No se pudieron actualizar los modelos", + "114f22c0e3900fad": "No se pudo actualizar la lista de modelos del proveedor.", "1bc94053b36b3f40": "No se pudo actualizar {provider}", "5b39dc5c8173ee94": "No se pudo iniciar el comando de actualización del proveedor.", "53e1e3591fc1abc9": "No se pudo actualizar la instancia del proveedor", diff --git a/apps/web/src/i18n/translations/fr.json b/apps/web/src/i18n/translations/fr.json index 88592734f037b..a8686acbe016e 100644 --- a/apps/web/src/i18n/translations/fr.json +++ b/apps/web/src/i18n/translations/fr.json @@ -9025,6 +9025,8 @@ }, ", qui n’est pas disponible dans cette version. Sa configuration est conservée." ], + "a9b5d2852719fddc": "Actualisation des modèles", + "4811262c294ce7d5": "Actualiser les modèles", "b534d4cd528ede39": "Les préférences de modèle et les modèles personnalisés sont enregistrés dans l’environnement sélectionné et partagés entre les appareils connectés.", "caaef21e8722857d": "Mode rapide", "a860db2b1bb3aa31": "Sans raisonnement", @@ -9136,6 +9138,8 @@ "9cd6647baae39b79": "Actualisez le fournisseur et relancez l’authentification.", "34e971a4cd3cfdf3": "Impossible de continuer l’authentification", "25617239c4886378": "La demande d’authentification a expiré.", + "254dc4b4151094ef": "Impossible d’actualiser les modèles", + "114f22c0e3900fad": "Impossible d’actualiser la liste des modèles du fournisseur.", "1bc94053b36b3f40": "Impossible de mettre à jour {provider}", "5b39dc5c8173ee94": "La commande de mise à jour du fournisseur n’a pas pu démarrer.", "53e1e3591fc1abc9": "Impossible de mettre à jour l’instance du fournisseur", diff --git a/apps/web/src/i18n/translations/ja.json b/apps/web/src/i18n/translations/ja.json index 36859e5c062fa..29788073e0af5 100644 --- a/apps/web/src/i18n/translations/ja.json +++ b/apps/web/src/i18n/translations/ja.json @@ -8980,6 +8980,8 @@ }, " を使用していますが、このビルドでは利用できません。設定は保持されます。" ], + "a9b5d2852719fddc": "モデルを更新中", + "4811262c294ce7d5": "モデルを更新", "b534d4cd528ede39": "モデルの設定とカスタムモデルは選択した環境に保存され、接続されているデバイス間で共有されます。", "caaef21e8722857d": "高速モード", "a860db2b1bb3aa31": "推論なし", @@ -9091,6 +9093,8 @@ "9cd6647baae39b79": "プロバイダーを更新して、認証フローをもう一度開始してください。", "34e971a4cd3cfdf3": "認証を続行できませんでした", "25617239c4886378": "認証リクエストの期限が切れました。", + "254dc4b4151094ef": "モデルを更新できませんでした", + "114f22c0e3900fad": "プロバイダーのモデル一覧を更新できませんでした。", "1bc94053b36b3f40": "{provider} を更新できませんでした", "5b39dc5c8173ee94": "プロバイダーの更新コマンドを開始できませんでした。", "53e1e3591fc1abc9": "プロバイダーのインスタンスを更新できませんでした", diff --git a/apps/web/src/i18n/translations/ko.json b/apps/web/src/i18n/translations/ko.json index 53ee2a93974fa..644a9041bc0e6 100644 --- a/apps/web/src/i18n/translations/ko.json +++ b/apps/web/src/i18n/translations/ko.json @@ -8982,6 +8982,8 @@ }, "을(를) 사용하지만 이 빌드에서는 사용할 수 없습니다. 구성은 보존됩니다." ], + "a9b5d2852719fddc": "모델 새로고침 중", + "4811262c294ce7d5": "모델 새로고침", "b534d4cd528ede39": "모델 기본 설정과 사용자 지정 모델은 선택한 환경에 저장되며 연결된 기기 간에 공유됩니다.", "caaef21e8722857d": "빠른 모드", "a860db2b1bb3aa31": "추론 없음", @@ -9093,6 +9095,8 @@ "9cd6647baae39b79": "제공자를 새로 고치고 인증 절차를 다시 시작하세요.", "34e971a4cd3cfdf3": "인증을 계속할 수 없습니다", "25617239c4886378": "인증 요청이 만료되었습니다.", + "254dc4b4151094ef": "모델을 새로고침하지 못했습니다", + "114f22c0e3900fad": "공급자 모델 목록을 새로고침하지 못했습니다.", "1bc94053b36b3f40": "{provider}을(를) 업데이트할 수 없습니다", "5b39dc5c8173ee94": "제공자 업데이트 명령을 시작할 수 없습니다.", "53e1e3591fc1abc9": "제공자 인스턴스를 업데이트할 수 없습니다", diff --git a/apps/web/src/i18n/translations/pt-BR.json b/apps/web/src/i18n/translations/pt-BR.json index 9594cd2896117..bdd2f8f5f2f9d 100644 --- a/apps/web/src/i18n/translations/pt-BR.json +++ b/apps/web/src/i18n/translations/pt-BR.json @@ -9024,6 +9024,8 @@ }, ", que não está disponível nesta build. A configuração dela é preservada." ], + "a9b5d2852719fddc": "Atualizando modelos", + "4811262c294ce7d5": "Atualizar modelos", "b534d4cd528ede39": "As preferências de modelo e os modelos personalizados são salvos no ambiente selecionado e compartilhados entre os dispositivos conectados.", "caaef21e8722857d": "Modo rápido", "a860db2b1bb3aa31": "Sem raciocínio", @@ -9135,6 +9137,8 @@ "9cd6647baae39b79": "Atualize o provedor e inicie o fluxo de autenticação novamente.", "34e971a4cd3cfdf3": "Não foi possível continuar a autenticação", "25617239c4886378": "A solicitação de autenticação expirou.", + "254dc4b4151094ef": "Não foi possível atualizar os modelos", + "114f22c0e3900fad": "Não foi possível atualizar a lista de modelos do provedor.", "1bc94053b36b3f40": "Não foi possível atualizar o {provider}", "5b39dc5c8173ee94": "Não foi possível iniciar o comando de atualização do provedor.", "53e1e3591fc1abc9": "Não foi possível atualizar a instância do provedor", diff --git a/apps/web/src/i18n/translations/ru.json b/apps/web/src/i18n/translations/ru.json index f74090c44cde5..b6cacd1438352 100644 --- a/apps/web/src/i18n/translations/ru.json +++ b/apps/web/src/i18n/translations/ru.json @@ -9025,6 +9025,8 @@ }, ", который недоступен в этой сборке. Его конфигурация сохранена." ], + "a9b5d2852719fddc": "Обновление моделей", + "4811262c294ce7d5": "Обновить модели", "b534d4cd528ede39": "Настройки моделей и свои модели сохраняются в выбранном окружении и доступны на всех подключённых устройствах.", "caaef21e8722857d": "Быстрый режим", "a860db2b1bb3aa31": "Без рассуждений", @@ -9136,6 +9138,8 @@ "9cd6647baae39b79": "Обновите провайдера и начните аутентификацию заново.", "34e971a4cd3cfdf3": "Не удалось продолжить аутентификацию", "25617239c4886378": "Срок запроса аутентификации истёк.", + "254dc4b4151094ef": "Не удалось обновить модели", + "114f22c0e3900fad": "Не удалось обновить список моделей провайдера.", "1bc94053b36b3f40": "Не удалось обновить {provider}", "5b39dc5c8173ee94": "Не удалось запустить команду обновления провайдера.", "53e1e3591fc1abc9": "Не удалось обновить экземпляр провайдера", diff --git a/apps/web/src/i18n/translations/sv.json b/apps/web/src/i18n/translations/sv.json index f83c0691df978..fe5a23bc86b69 100644 --- a/apps/web/src/i18n/translations/sv.json +++ b/apps/web/src/i18n/translations/sv.json @@ -9031,6 +9031,8 @@ }, ", som inte finns i den här versionen. Dess konfiguration bevaras." ], + "a9b5d2852719fddc": "Uppdaterar modeller", + "4811262c294ce7d5": "Uppdatera modeller", "b534d4cd528ede39": "Modellinställningar och anpassade modeller sparas i den valda miljön och delas mellan anslutna enheter.", "caaef21e8722857d": "Snabbläge", "a860db2b1bb3aa31": "Inget resonerande", @@ -9142,6 +9144,8 @@ "9cd6647baae39b79": "Uppdatera leverantören och starta autentiseringen igen.", "34e971a4cd3cfdf3": "Kunde inte fortsätta autentiseringen", "25617239c4886378": "Autentiseringsbegäran har gått ut.", + "254dc4b4151094ef": "Kunde inte uppdatera modeller", + "114f22c0e3900fad": "Leverantörens modellista kunde inte uppdateras.", "1bc94053b36b3f40": "Kunde inte uppdatera {provider}", "5b39dc5c8173ee94": "Leverantörens uppdateringskommando kunde inte startas.", "53e1e3591fc1abc9": "Kunde inte uppdatera leverantörsinstansen", diff --git a/apps/web/src/i18n/translations/zh.json b/apps/web/src/i18n/translations/zh.json index 937e39a4eaeee..b31635c0f01f0 100644 --- a/apps/web/src/i18n/translations/zh.json +++ b/apps/web/src/i18n/translations/zh.json @@ -9041,6 +9041,8 @@ }, ",当前版本不支持。其配置会保留。" ], + "a9b5d2852719fddc": "正在刷新模型", + "4811262c294ce7d5": "刷新模型", "b534d4cd528ede39": "模型偏好和自定义模型保存在所选环境中,并在已连接的设备间共享。", "caaef21e8722857d": "快速模式", "a860db2b1bb3aa31": "无推理", @@ -9152,6 +9154,8 @@ "9cd6647baae39b79": "请刷新提供方并重新开始认证。", "34e971a4cd3cfdf3": "无法继续认证", "25617239c4886378": "认证请求已过期。", + "254dc4b4151094ef": "无法刷新模型", + "114f22c0e3900fad": "无法刷新此提供商的模型列表。", "1bc94053b36b3f40": "无法更新 {provider}", "5b39dc5c8173ee94": "无法启动提供方更新命令。", "53e1e3591fc1abc9": "无法更新提供方实例", diff --git a/docs/adr/0031-pi-rpc-protocol-v2-chunked-frames.md b/docs/adr/0031-pi-rpc-protocol-v2-chunked-frames.md new file mode 100644 index 0000000000000..b27cfe3a4e323 --- /dev/null +++ b/docs/adr/0031-pi-rpc-protocol-v2-chunked-frames.md @@ -0,0 +1,21 @@ +# ADR 0031: Negotiate OhMyPi RPC protocol v2 and reassemble chunked frames + +- Status: accepted +- Date: 2026-11-20 + +OhMyPi (`omp` v19+) enforces a 1 MiB (`maxFrameBytes: 1048576`) single-frame limit on RPC protocol +v1. Environments with large model catalogs (for example when `OPENROUTER_API_KEY` exposes hundreds +of OpenRouter models) exceed 1 MiB on `get_available_models`, causing `omp` to reply with +`"RPC response exceeded the transport limit"` and discovery to degrade to the warning that models +and commands could not be refreshed. + +When a Pi RPC connection receives a `ready` frame advertising `supportedProtocolVersions` with `2`, +[rpc.ts](../../packages/provider-pi/src/server/rpc.ts) sends `negotiate_protocol` for version `2` +and reassembles incoming `rpc_chunk` frames (base64 UTF-8 slices up to 64 MiB) before routing them. +Requests queued after `ready` wait for negotiation to settle; a request sent before `ready` that +fails with `"RPC response exceeded the transport limit"` retries once after v2 negotiation succeeds. +Plain Pi never emits `ready` with protocol v2, so its transport behavior is unchanged. + +Preserve when updating from upstream unless upstream adds Pi/OhMyPi RPC protocol v2 negotiation and +`rpc_chunk` reassembly itself, in which case drop this fork change in favor of upstream's +implementation. diff --git a/docs/adr/0032-provider-models-refresh-button.md b/docs/adr/0032-provider-models-refresh-button.md new file mode 100644 index 0000000000000..bbff34730cd86 --- /dev/null +++ b/docs/adr/0032-provider-models-refresh-button.md @@ -0,0 +1,17 @@ +# ADR 0032: Refresh models button on provider settings + +- Status: accepted +- Date: 2026-11-20 + +The Providers settings panel previously had a top-level "Refresh provider status" button that +re-probed every provider on the environment, with no way to refresh just the selected provider's +model list from its Models section. + +[ProviderInstanceCard](../../apps/web/src/components/settings/ProviderInstanceCard.tsx) adds a +"Refresh models" action in the Models section header, wired through +[ProviderSettingsPanel](../../apps/web/src/components/settings/ProviderSettingsPanel.tsx) to the +existing `server.refreshProviders` RPC with `{ instanceId, refreshModels: true }`. The button is +disabled while the provider is disabled or already refreshing, and surfaces failures in a toast. + +Preserve when updating from upstream unless upstream adds a per-provider model refresh button to the +provider config page, in which case drop this fork change in favor of upstream's control. diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.ts index 97281a57e326a..5706510a2f135 100644 --- a/packages/provider-pi/src/server/adapter.test.ts +++ b/packages/provider-pi/src/server/adapter.test.ts @@ -7492,3 +7492,162 @@ process.stdout.write('{"type":"ready"}\\n'); }).pipe(Effect.provide(NodeServices.layer)), ); }); + +describe("PiRpc protocol v2", () => { + const encodeChunks = (frame: Record, chunkId: string, chunkSize = 64) => { + const payload = Buffer.from(JSON.stringify(frame), "utf8"); + const count = Math.ceil(payload.byteLength / chunkSize); + const lines: string[] = []; + for (let index = 0; index < count; index += 1) { + lines.push( + `${JSON.stringify({ + type: "rpc_chunk", + chunkId, + index, + count, + byteLength: payload.byteLength, + data: payload.subarray(index * chunkSize, (index + 1) * chunkSize).toString("base64"), + })}\n`, + ); + } + return lines.join(""); + }; + + it.effect( + "negotiates protocol v2 on ready, reassembles chunked frames, and retries pre-ready overflowed requests", + () => + Effect.gen(function* () { + const stdout = yield* Queue.unbounded(); + const push = (text: string) => + Queue.offer(stdout, new TextEncoder().encode(text)).pipe(Effect.asVoid); + let protocolVersion = 1; + let readySent = false; + let chunkSeq = 0; + let stdinBuffer = ""; + + const handleStdinLine = (line: string) => + Effect.gen(function* () { + if (!readySent) { + readySent = true; + yield* push( + `${JSON.stringify({ + type: "ready", + protocolVersion: 1, + supportedProtocolVersions: [1, 2], + maxFrameBytes: 1048576, + maxReassembledFrameBytes: 67108864, + })}\n`, + ); + } + const command = JSON.parse(line) as Record; + const id = String(command["id"]); + if (command["type"] === "negotiate_protocol") { + protocolVersion = command["protocolVersion"] === 2 ? 2 : 1; + yield* push( + `${JSON.stringify({ + id, + type: "response", + command: "negotiate_protocol", + success: true, + data: { protocolVersion }, + })}\n`, + ); + return; + } + if (command["type"] === "get_available_models") { + if (protocolVersion < 2) { + yield* push( + `${JSON.stringify({ + id, + type: "response", + command: "get_available_models", + success: false, + error: "RPC response exceeded the transport limit", + })}\n`, + ); + return; + } + yield* push( + encodeChunks( + { + id, + type: "response", + command: "get_available_models", + success: true, + data: { + models: [ + { provider: "openrouter", id: "anthropic/claude-sonnet-4", name: "Claude" }, + ], + }, + }, + `rpc-${++chunkSeq}`, + ), + ); + } + }); + + const stdin = Sink.forEach((chunk) => + Effect.gen(function* () { + stdinBuffer += new TextDecoder().decode(chunk); + let newline = stdinBuffer.indexOf("\n"); + while (newline >= 0) { + const line = stdinBuffer.slice(0, newline); + stdinBuffer = stdinBuffer.slice(newline + 1); + if (line.length > 0) yield* handleStdinLine(line); + newline = stdinBuffer.indexOf("\n"); + } + }), + ); + + const spawner = ChildProcessSpawner.make(() => + Effect.succeed( + ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(FAKE_PID), + exitCode: Effect.never, + isRunning: Effect.succeed(true), + kill: () => Effect.void, + unref: Effect.succeed(Effect.void), + stdin, + stdout: Stream.fromQueue(stdout), + stderr: Stream.empty, + all: Stream.empty, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + }), + ), + ); + + const connection = yield* makePiRpcConnection({ + command: "omp", + args: ["--mode", "rpc", "--no-session"], + cwd: undefined, + env: {}, + }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)); + + // First request goes out before `ready`, hits v1 overflow, then retries once v2 negotiates. + const firstResult = yield* connection.request({ type: "get_available_models" }); + assert.deepEqual(firstResult, { + models: [{ provider: "openrouter", id: "anthropic/claude-sonnet-4", name: "Claude" }], + }); + + // Subsequent request uses v2 directly. + const secondResult = yield* connection.request({ type: "get_available_models" }); + assert.deepEqual(secondResult, { + models: [{ provider: "openrouter", id: "anthropic/claude-sonnet-4", name: "Claude" }], + }); + + // Chunked event frames are also reassembled and delivered on events. + yield* push( + encodeChunks( + { type: "message_end", message: { role: "assistant", text: "chunked payload" } }, + `rpc-${++chunkSeq}`, + ), + ); + assert.equal((yield* Queue.take(connection.events))["type"], "ready"); + assert.deepEqual(yield* Queue.take(connection.events), { + type: "message_end", + message: { role: "assistant", text: "chunked payload" }, + }); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); +}); diff --git a/packages/provider-pi/src/server/rpc.ts b/packages/provider-pi/src/server/rpc.ts index 3cdb2058f1cf3..42480c216fd42 100644 --- a/packages/provider-pi/src/server/rpc.ts +++ b/packages/provider-pi/src/server/rpc.ts @@ -114,6 +114,112 @@ interface PendingPiRequest { // MCP duplicates images in model content and CallToolResult; budget both plus bounded overhead. const MAX_PI_RECORD_CHARS = 2 * MAX_TOOL_OUTPUT_IMAGES * MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH + 8 * 1024 * 1024; +const MAX_PI_REASSEMBLED_BYTES = 64 * 1024 * 1024; +const PI_RESPONSE_TRANSPORT_LIMIT_ERROR = "RPC response exceeded the transport limit"; + +function supportsRpcProtocolV2(record: PiRpcRecord): boolean { + return ( + record["type"] === "ready" && + Array.isArray(record["supportedProtocolVersions"]) && + record["supportedProtocolVersions"].includes(2) + ); +} + +interface PendingRpcChunk { + readonly chunkId: string; + readonly count: number; + readonly byteLength: number; + nextIndex: number; + readonly chunks: Array; + receivedBytes: number; +} + +function makeRpcChunkAssembler() { + let pending: PendingRpcChunk | undefined; + const utf8Decoder = new TextDecoder("utf-8", { fatal: true }); + + return (record: PiRpcRecord): { readonly record?: PiRpcRecord; readonly warning?: string } => { + if (record["type"] !== "rpc_chunk") { + if (pending !== undefined) { + pending = undefined; + return { record, warning: "Interrupted pi RPC chunk sequence." }; + } + return { record }; + } + + const chunkId = piRecordString(record, "chunkId"); + const index = piRecordNumber(record, "index"); + const count = piRecordNumber(record, "count"); + const byteLength = piRecordNumber(record, "byteLength"); + const data = piRecordString(record, "data"); + if ( + chunkId === undefined || + chunkId.length === 0 || + chunkId.length > 128 || + index === undefined || + count === undefined || + byteLength === undefined || + !Number.isSafeInteger(index) || + !Number.isSafeInteger(count) || + !Number.isSafeInteger(byteLength) || + index < 0 || + count < 2 || + index >= count || + byteLength <= 0 || + byteLength > MAX_PI_REASSEMBLED_BYTES || + data === undefined || + data.length === 0 + ) { + pending = undefined; + return { warning: "Dropping invalid pi RPC chunk metadata." }; + } + + const bytes = Buffer.from(data, "base64"); + if (pending === undefined) { + if (index !== 0) { + return { warning: "Dropping out-of-order pi RPC chunk." }; + } + pending = { chunkId, count, byteLength, nextIndex: 0, chunks: [], receivedBytes: 0 }; + } + + if ( + pending.chunkId !== chunkId || + pending.count !== count || + pending.byteLength !== byteLength || + pending.nextIndex !== index + ) { + pending = undefined; + return { warning: "Dropping mismatched pi RPC chunk sequence." }; + } + + pending.chunks.push(bytes); + pending.receivedBytes += bytes.byteLength; + pending.nextIndex += 1; + if (pending.receivedBytes > pending.byteLength) { + pending = undefined; + return { warning: "Dropping oversized pi RPC chunk sequence." }; + } + if (pending.nextIndex < pending.count) { + return {}; + } + + const completed = pending; + pending = undefined; + if (completed.receivedBytes !== completed.byteLength) { + return { warning: "Dropping truncated pi RPC chunk sequence." }; + } + try { + const decoded = utf8Decoder.decode(Buffer.concat(completed.chunks)); + const reassembled = parsePiRecord(decoded); + if (reassembled === undefined) { + return { warning: "Dropping undecodable pi RPC chunk payload." }; + } + return { record: reassembled }; + } catch { + return { warning: "Dropping undecodable pi RPC chunk payload." }; + } + }; +} function makeJsonlFramer() { let buffer = ""; @@ -290,6 +396,8 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp const outgoing = yield* Queue.unbounded(); const transportDown = yield* Deferred.make(); const exitDeferred = yield* Deferred.make(); + const assembleChunk = makeRpcChunkAssembler(); + let protocolV2Negotiation: Deferred.Deferred | undefined; let nextRequestId = 0; const failTransport = Effect.fn("PiRpc.failTransport")(function* (error: PiRpcError) { @@ -303,9 +411,48 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp yield* Queue.fail(events, error); }); + const send = + /* Protocol records and writes include streaming deltas; avoid a span per record. */ + Effect.fnUntraced(function* (record: PiRpcRecord): Effect.fn.Return { + const accepted = yield* Queue.offer( + outgoing, + new TextEncoder().encode(`${encodeJsonLine(record)}\n`), + ); + if (!accepted) { + return yield* Deferred.await(transportDown); + } + }); + const routeRecord = /* Protocol records and writes include streaming deltas; avoid a span per record. */ - Effect.fnUntraced(function* (record: PiRpcRecord) { + Effect.fnUntraced(function* (rawRecord: PiRpcRecord) { + const { record, warning } = assembleChunk(rawRecord); + if (warning !== undefined) { + yield* Effect.logWarning(warning); + } + if (record === undefined) return; + if (protocolV2Negotiation === undefined && supportsRpcProtocolV2(record)) { + const negotiation = yield* Deferred.make(); + protocolV2Negotiation = negotiation; + const id = `t3-${nextRequestId++}`; + const responseDeferred = yield* Deferred.make(); + pendingRequests.set(id, { deferred: responseDeferred }); + yield* send({ type: "negotiate_protocol", protocolVersion: 2, id }).pipe( + Effect.catch(() => + Effect.sync(() => { + pendingRequests.delete(id); + }).pipe(Effect.andThen(Deferred.succeed(negotiation, false))), + ), + ); + yield* Deferred.await(responseDeferred).pipe( + Effect.matchEffect({ + onFailure: () => Deferred.succeed(negotiation, false), + onSuccess: (data) => + Deferred.succeed(negotiation, piRecordNumber(data, "protocolVersion") === 2), + }), + Effect.forkIn(scope), + ); + } if (record["type"] === "response" && typeof record["id"] === "string") { const pending = pendingRequests.get(record["id"]); if (pending !== undefined) { @@ -424,21 +571,21 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp Effect.forkIn(scope), ); - const send = - /* Protocol records and writes include streaming deltas; avoid a span per record. */ - Effect.fnUntraced(function* (record: PiRpcRecord): Effect.fn.Return { - const accepted = yield* Queue.offer( - outgoing, - new TextEncoder().encode(`${encodeJsonLine(record)}\n`), - ); - if (!accepted) { - return yield* Deferred.await(transportDown); - } - }); + const awaitProtocolV2Negotiation = ( + negotiation: Deferred.Deferred, + operation: string, + timeoutMs: number, + ): Effect.Effect => + Effect.raceFirst(Deferred.await(negotiation), Deferred.await(transportDown)).pipe( + Effect.timeoutOrElse({ + duration: Duration.millis(timeoutMs), + orElse: () => Effect.fail(new PiRpcTimeoutError({ operation, timeoutMs })), + }), + ); - const request = Effect.fn("PiRpc.request")(function* ( + const sendRequestOnce = Effect.fnUntraced(function* ( record: PiRpcRecord, - timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, + timeoutMs: number, ): Effect.fn.Return { const id = `t3-${nextRequestId++}`; const deferred = yield* Deferred.make(); @@ -462,6 +609,37 @@ export const makePiRpcConnection = Effect.fnUntraced(function* (options: PiRpcSp ); }); + const request = Effect.fn("PiRpc.request")(function* ( + record: PiRpcRecord, + timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, + ): Effect.fn.Return { + const operation = String(record["type"] ?? "request"); + const negotiationAtStart = protocolV2Negotiation; + if (negotiationAtStart !== undefined) { + yield* awaitProtocolV2Negotiation(negotiationAtStart, operation, timeoutMs); + } + return yield* sendRequestOnce(record, timeoutMs).pipe( + // A first request can reach omp before its `ready` frame triggers v2 negotiation. + Effect.catchIf( + (error): error is PiRpcError => + negotiationAtStart === undefined && + error._tag === "PiRpcError" && + error.detail === PI_RESPONSE_TRANSPORT_LIMIT_ERROR && + protocolV2Negotiation !== undefined, + (error) => + Effect.gen(function* () { + const negotiated = yield* awaitProtocolV2Negotiation( + protocolV2Negotiation!, + operation, + timeoutMs, + ); + if (!negotiated) return yield* error; + return yield* sendRequestOnce(record, timeoutMs); + }), + ), + ); + }); + return { send, request,