Files
oh-my-pi/packages/coding-agent/examples/sdk/09-api-keys-and-oauth.ts
T
oldschoola 21d152c70c feat(coding-agent): async ConfigFile + ModelRegistry.create() factory; fix MemorySessionStorage O(N^2) appends
F6: defer the JSON -> YAML migration out of ConfigFile's constructor and
add an async path so the boot sequence stops blocking the event loop on
sync I/O. New ConfigFile.tryLoadAsync/loadAsync/loadOrDefaultAsync/
getMtimeMsAsync and static ConfigFile.warmup. The migration is now
idempotent (per-process cache) so relocate() does not re-run it.
ModelRegistry.create(authStorage, modelsPath?) is a new async factory
that runs the warmup before the sync constructor's bundled-model load.
Production call sites (main.ts, sdk.ts, task/executor.ts, commit
pipelines, SDK example) all switched. Sync new ModelRegistry(...)
constructor is still supported for tests.

F2: rewrite MemorySessionStorage's mirror as { chunks: string[]; byteLen;
mtimeMs } so writeLineSync appends a single chunk in O(1) instead of
read-modify-writing the entire file (which was O(N) per append, O(N^2)
per session). statSync now reports true UTF-8 byte length instead of
character count. readTextPrefix walks chunks until the byte budget is
exhausted instead of materialising the full mirror.

Also rolls in per-package CHANGELOG entries for F1-F8.
2026-05-31 04:46:06 +02:00

55 lines
1.6 KiB
TypeScript

/**
* API Keys and OAuth
*
* Configure API key resolution via AuthStorage and ModelRegistry.
*/
import {
AuthStorage,
createAgentSession,
discoverAuthStorage,
discoverModels,
ModelRegistry,
SessionManager,
} from "@oh-my-pi/pi-coding-agent";
// Default: discoverAuthStorage() uses ~/.omp/agent/agent.db
// discoverModels() loads built-in + custom models from ~/.omp/agent/models.json
const authStorage = await discoverAuthStorage();
const modelRegistry = await discoverModels(authStorage);
await createAgentSession({
sessionManager: SessionManager.inMemory(),
authStorage,
modelRegistry,
});
console.log("Session with default auth storage and model registry");
// Custom auth storage location
const customAuthStorage = await AuthStorage.create("/tmp/my-app/agent.db");
const customModelRegistry = await ModelRegistry.create(customAuthStorage, "/tmp/my-app/models.json");
await createAgentSession({
sessionManager: SessionManager.inMemory(),
authStorage: customAuthStorage,
modelRegistry: customModelRegistry,
});
console.log("Session with custom auth storage location");
// Runtime API key override (not persisted to disk)
authStorage.setRuntimeApiKey("anthropic", "sk-my-temp-key");
await createAgentSession({
sessionManager: SessionManager.inMemory(),
authStorage,
modelRegistry,
});
console.log("Session with runtime API key override");
// No models.json - only built-in models
const simpleRegistry = await ModelRegistry.create(authStorage); // null = no models.json
await createAgentSession({
sessionManager: SessionManager.inMemory(),
authStorage,
modelRegistry: simpleRegistry,
});
console.log("Session with only built-in models");