mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 18:44:31 +00:00
fix: tolerate missing pi-coding-agent backend export
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import type { Api, Model } from "@mariozechner/pi-ai";
|
import type { Api, Model } from "@mariozechner/pi-ai";
|
||||||
|
import type { ModelRegistry } from "@mariozechner/pi-coding-agent";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { isModernModelRef } from "./live-model-filter.js";
|
import { isModernModelRef } from "./live-model-filter.js";
|
||||||
import { normalizeModelCompat } from "./model-compat.js";
|
import { normalizeModelCompat } from "./model-compat.js";
|
||||||
import { resolveForwardCompatModel } from "./model-forward-compat.js";
|
import { resolveForwardCompatModel } from "./model-forward-compat.js";
|
||||||
import type { ModelRegistry } from "./pi-model-discovery.js";
|
|
||||||
|
|
||||||
const baseModel = (): Model<Api> =>
|
const baseModel = (): Model<Api> =>
|
||||||
({
|
({
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import type { Api, Model } from "@mariozechner/pi-ai";
|
import type { Api, Model } from "@mariozechner/pi-ai";
|
||||||
|
import type { ModelRegistry } from "@mariozechner/pi-coding-agent";
|
||||||
import { DEFAULT_CONTEXT_TOKENS } from "./defaults.js";
|
import { DEFAULT_CONTEXT_TOKENS } from "./defaults.js";
|
||||||
import { normalizeModelCompat } from "./model-compat.js";
|
import { normalizeModelCompat } from "./model-compat.js";
|
||||||
import { normalizeProviderId } from "./model-selection.js";
|
import { normalizeProviderId } from "./model-selection.js";
|
||||||
import type { ModelRegistry } from "./pi-model-discovery.js";
|
|
||||||
|
|
||||||
const OPENAI_CODEX_GPT_53_MODEL_ID = "gpt-5.3-codex";
|
const OPENAI_CODEX_GPT_53_MODEL_ID = "gpt-5.3-codex";
|
||||||
const OPENAI_CODEX_TEMPLATE_MODEL_IDS = ["gpt-5.2-codex"] as const;
|
const OPENAI_CODEX_TEMPLATE_MODEL_IDS = ["gpt-5.2-codex"] as const;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Api, Model } from "@mariozechner/pi-ai";
|
import type { Api, Model } from "@mariozechner/pi-ai";
|
||||||
|
import type { AuthStorage, ModelRegistry } from "@mariozechner/pi-coding-agent";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import type { ModelDefinitionConfig } from "../../config/types.js";
|
import type { ModelDefinitionConfig } from "../../config/types.js";
|
||||||
import { resolveOpenClawAgentDir } from "../agent-paths.js";
|
import { resolveOpenClawAgentDir } from "../agent-paths.js";
|
||||||
@@ -7,12 +8,7 @@ import { buildModelAliasLines } from "../model-alias-lines.js";
|
|||||||
import { normalizeModelCompat } from "../model-compat.js";
|
import { normalizeModelCompat } from "../model-compat.js";
|
||||||
import { resolveForwardCompatModel } from "../model-forward-compat.js";
|
import { resolveForwardCompatModel } from "../model-forward-compat.js";
|
||||||
import { normalizeProviderId } from "../model-selection.js";
|
import { normalizeProviderId } from "../model-selection.js";
|
||||||
import {
|
import { discoverAuthStorage, discoverModels } from "../pi-model-discovery.js";
|
||||||
discoverAuthStorage,
|
|
||||||
discoverModels,
|
|
||||||
type AuthStorage,
|
|
||||||
type ModelRegistry,
|
|
||||||
} from "../pi-model-discovery.js";
|
|
||||||
|
|
||||||
type InlineModelEntry = ModelDefinitionConfig & {
|
type InlineModelEntry = ModelDefinitionConfig & {
|
||||||
provider: string;
|
provider: string;
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||||
import type { Api, AssistantMessage, Model } from "@mariozechner/pi-ai";
|
import type { Api, AssistantMessage, Model } from "@mariozechner/pi-ai";
|
||||||
|
import type { AuthStorage, ModelRegistry } from "@mariozechner/pi-coding-agent";
|
||||||
import type { ThinkLevel } from "../../../auto-reply/thinking.js";
|
import type { ThinkLevel } from "../../../auto-reply/thinking.js";
|
||||||
import type { SessionSystemPromptReport } from "../../../config/sessions/types.js";
|
import type { SessionSystemPromptReport } from "../../../config/sessions/types.js";
|
||||||
import type { PluginHookBeforeAgentStartResult } from "../../../plugins/types.js";
|
import type { PluginHookBeforeAgentStartResult } from "../../../plugins/types.js";
|
||||||
import type { MessagingToolSend } from "../../pi-embedded-messaging.js";
|
import type { MessagingToolSend } from "../../pi-embedded-messaging.js";
|
||||||
import type { AuthStorage, ModelRegistry } from "../../pi-model-discovery.js";
|
|
||||||
import type { NormalizedUsage } from "../../usage.js";
|
import type { NormalizedUsage } from "../../usage.js";
|
||||||
import type { RunEmbeddedPiAgentParams } from "./params.js";
|
import type { RunEmbeddedPiAgentParams } from "./params.js";
|
||||||
|
|
||||||
|
|||||||
26
src/agents/pi-model-discovery.compat.test.ts
Normal file
26
src/agents/pi-model-discovery.compat.test.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
describe("pi-model-discovery module compatibility", () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.resetModules();
|
||||||
|
vi.doUnmock("@mariozechner/pi-coding-agent");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("loads when InMemoryAuthStorageBackend is not exported", async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
vi.doMock("@mariozechner/pi-coding-agent", () => {
|
||||||
|
class MockAuthStorage {}
|
||||||
|
class MockModelRegistry {}
|
||||||
|
|
||||||
|
return {
|
||||||
|
AuthStorage: MockAuthStorage,
|
||||||
|
ModelRegistry: MockModelRegistry,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(import("./pi-model-discovery.js")).resolves.toMatchObject({
|
||||||
|
discoverAuthStorage: expect.any(Function),
|
||||||
|
discoverModels: expect.any(Function),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,14 +1,46 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import {
|
import * as PiCodingAgent from "@mariozechner/pi-coding-agent";
|
||||||
AuthStorage,
|
import type {
|
||||||
InMemoryAuthStorageBackend,
|
AuthStorage as PiAuthStorage,
|
||||||
ModelRegistry,
|
ModelRegistry as PiModelRegistry,
|
||||||
} from "@mariozechner/pi-coding-agent";
|
} from "@mariozechner/pi-coding-agent";
|
||||||
import { ensureAuthProfileStore } from "./auth-profiles.js";
|
import { ensureAuthProfileStore } from "./auth-profiles.js";
|
||||||
import { resolvePiCredentialMapFromStore, type PiCredentialMap } from "./pi-auth-credentials.js";
|
import { resolvePiCredentialMapFromStore, type PiCredentialMap } from "./pi-auth-credentials.js";
|
||||||
|
|
||||||
export { AuthStorage, ModelRegistry } from "@mariozechner/pi-coding-agent";
|
const PiAuthStorageClass = PiCodingAgent.AuthStorage;
|
||||||
|
const PiModelRegistryClass = PiCodingAgent.ModelRegistry;
|
||||||
|
|
||||||
|
export { PiAuthStorageClass as AuthStorage, PiModelRegistryClass as ModelRegistry };
|
||||||
|
|
||||||
|
type InMemoryAuthStorageBackendLike = {
|
||||||
|
withLock<T>(
|
||||||
|
update: (current: string) => {
|
||||||
|
result: T;
|
||||||
|
next?: string;
|
||||||
|
},
|
||||||
|
): T;
|
||||||
|
};
|
||||||
|
|
||||||
|
function createInMemoryAuthStorageBackend(
|
||||||
|
initialData: PiCredentialMap,
|
||||||
|
): InMemoryAuthStorageBackendLike {
|
||||||
|
let snapshot = JSON.stringify(initialData, null, 2);
|
||||||
|
return {
|
||||||
|
withLock<T>(
|
||||||
|
update: (current: string) => {
|
||||||
|
result: T;
|
||||||
|
next?: string;
|
||||||
|
},
|
||||||
|
): T {
|
||||||
|
const { result, next } = update(snapshot);
|
||||||
|
if (typeof next === "string") {
|
||||||
|
snapshot = next;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||||
@@ -60,19 +92,25 @@ function scrubLegacyStaticAuthJsonEntries(pathname: string): void {
|
|||||||
function createAuthStorage(AuthStorageLike: unknown, path: string, creds: PiCredentialMap) {
|
function createAuthStorage(AuthStorageLike: unknown, path: string, creds: PiCredentialMap) {
|
||||||
const withInMemory = AuthStorageLike as { inMemory?: (data?: unknown) => unknown };
|
const withInMemory = AuthStorageLike as { inMemory?: (data?: unknown) => unknown };
|
||||||
if (typeof withInMemory.inMemory === "function") {
|
if (typeof withInMemory.inMemory === "function") {
|
||||||
return withInMemory.inMemory(creds) as AuthStorage;
|
return withInMemory.inMemory(creds) as PiAuthStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
const withFromStorage = AuthStorageLike as {
|
const withFromStorage = AuthStorageLike as {
|
||||||
fromStorage?: (storage: unknown) => unknown;
|
fromStorage?: (storage: unknown) => unknown;
|
||||||
};
|
};
|
||||||
if (typeof withFromStorage.fromStorage === "function") {
|
if (typeof withFromStorage.fromStorage === "function") {
|
||||||
const backend = new InMemoryAuthStorageBackend();
|
const backendCtor = (
|
||||||
|
PiCodingAgent as { InMemoryAuthStorageBackend?: new () => InMemoryAuthStorageBackendLike }
|
||||||
|
).InMemoryAuthStorageBackend;
|
||||||
|
const backend =
|
||||||
|
typeof backendCtor === "function"
|
||||||
|
? new backendCtor()
|
||||||
|
: createInMemoryAuthStorageBackend(creds);
|
||||||
backend.withLock(() => ({
|
backend.withLock(() => ({
|
||||||
result: undefined,
|
result: undefined,
|
||||||
next: JSON.stringify(creds, null, 2),
|
next: JSON.stringify(creds, null, 2),
|
||||||
}));
|
}));
|
||||||
return withFromStorage.fromStorage(backend) as AuthStorage;
|
return withFromStorage.fromStorage(backend) as PiAuthStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
const withFactory = AuthStorageLike as { create?: (path: string) => unknown };
|
const withFactory = AuthStorageLike as { create?: (path: string) => unknown };
|
||||||
@@ -80,7 +118,7 @@ function createAuthStorage(AuthStorageLike: unknown, path: string, creds: PiCred
|
|||||||
typeof withFactory.create === "function"
|
typeof withFactory.create === "function"
|
||||||
? withFactory.create(path)
|
? withFactory.create(path)
|
||||||
: new (AuthStorageLike as { new (path: string): unknown })(path)
|
: new (AuthStorageLike as { new (path: string): unknown })(path)
|
||||||
) as AuthStorage & {
|
) as PiAuthStorage & {
|
||||||
setRuntimeApiKey?: (provider: string, apiKey: string) => void;
|
setRuntimeApiKey?: (provider: string, apiKey: string) => void;
|
||||||
};
|
};
|
||||||
if (typeof withRuntimeOverride.setRuntimeApiKey === "function") {
|
if (typeof withRuntimeOverride.setRuntimeApiKey === "function") {
|
||||||
@@ -101,13 +139,13 @@ function resolvePiCredentials(agentDir: string): PiCredentialMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compatibility helpers for pi-coding-agent 0.50+ (discover* helpers removed).
|
// Compatibility helpers for pi-coding-agent 0.50+ (discover* helpers removed).
|
||||||
export function discoverAuthStorage(agentDir: string): AuthStorage {
|
export function discoverAuthStorage(agentDir: string): PiAuthStorage {
|
||||||
const credentials = resolvePiCredentials(agentDir);
|
const credentials = resolvePiCredentials(agentDir);
|
||||||
const authPath = path.join(agentDir, "auth.json");
|
const authPath = path.join(agentDir, "auth.json");
|
||||||
scrubLegacyStaticAuthJsonEntries(authPath);
|
scrubLegacyStaticAuthJsonEntries(authPath);
|
||||||
return createAuthStorage(AuthStorage, authPath, credentials);
|
return createAuthStorage(PiAuthStorageClass, authPath, credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function discoverModels(authStorage: AuthStorage, agentDir: string): ModelRegistry {
|
export function discoverModels(authStorage: PiAuthStorage, agentDir: string): PiModelRegistry {
|
||||||
return new ModelRegistry(authStorage, path.join(agentDir, "models.json"));
|
return new PiModelRegistryClass(authStorage, path.join(agentDir, "models.json"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { Api, Model } from "@mariozechner/pi-ai";
|
import type { Api, Model } from "@mariozechner/pi-ai";
|
||||||
|
import type { ModelRegistry } from "@mariozechner/pi-coding-agent";
|
||||||
import { resolveForwardCompatModel } from "../../agents/model-forward-compat.js";
|
import { resolveForwardCompatModel } from "../../agents/model-forward-compat.js";
|
||||||
import { parseModelRef } from "../../agents/model-selection.js";
|
import { parseModelRef } from "../../agents/model-selection.js";
|
||||||
import type { ModelRegistry } from "../../agents/pi-model-discovery.js";
|
|
||||||
import type { RuntimeEnv } from "../../runtime.js";
|
import type { RuntimeEnv } from "../../runtime.js";
|
||||||
import { resolveConfiguredEntries } from "./list.configured.js";
|
import { resolveConfiguredEntries } from "./list.configured.js";
|
||||||
import { formatErrorWithStack } from "./list.errors.js";
|
import { formatErrorWithStack } from "./list.errors.js";
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Api, Model } from "@mariozechner/pi-ai";
|
import type { Api, Model } from "@mariozechner/pi-ai";
|
||||||
|
import type { ModelRegistry } from "@mariozechner/pi-coding-agent";
|
||||||
import { resolveOpenClawAgentDir } from "../../agents/agent-paths.js";
|
import { resolveOpenClawAgentDir } from "../../agents/agent-paths.js";
|
||||||
import type { AuthProfileStore } from "../../agents/auth-profiles.js";
|
import type { AuthProfileStore } from "../../agents/auth-profiles.js";
|
||||||
import { listProfilesForProvider } from "../../agents/auth-profiles.js";
|
import { listProfilesForProvider } from "../../agents/auth-profiles.js";
|
||||||
@@ -8,7 +9,6 @@ import {
|
|||||||
resolveEnvApiKey,
|
resolveEnvApiKey,
|
||||||
} from "../../agents/model-auth.js";
|
} from "../../agents/model-auth.js";
|
||||||
import { ensureOpenClawModelsJson } from "../../agents/models-config.js";
|
import { ensureOpenClawModelsJson } from "../../agents/models-config.js";
|
||||||
import type { ModelRegistry } from "../../agents/pi-model-discovery.js";
|
|
||||||
import { discoverAuthStorage, discoverModels } from "../../agents/pi-model-discovery.js";
|
import { discoverAuthStorage, discoverModels } from "../../agents/pi-model-discovery.js";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import {
|
import {
|
||||||
|
|||||||
Reference in New Issue
Block a user