mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 11:03:07 +00:00
- Create base Store class with private backend and protected getBackend() - Add SettingsStore, ProviderKeysStore, SessionsStore - Each store defines its own schema via getConfig() - AppStorage now takes stores + backend in constructor - Remove SessionsRepository (logic moved to SessionsStore) - Update all consumers to use store API (storage.settings.get/set, storage.providerKeys.get/set) - Update example app to follow new pattern: create stores, gather configs, create backend, wire - Benefits: stores own their schema, no circular deps, cleaner separation
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { type AgentContext, agentLoop, type Message, type PromptConfig, type UserMessage } from "@mariozechner/pi-ai";
|
|
import { getAppStorage } from "../../storage/app-storage.js";
|
|
import type { AgentRunConfig, AgentTransport } from "./types.js";
|
|
|
|
/**
|
|
* Transport that calls LLM providers directly.
|
|
* Optionally routes calls through a CORS proxy if enabled in settings.
|
|
*/
|
|
export class ProviderTransport implements AgentTransport {
|
|
async *run(messages: Message[], userMessage: Message, cfg: AgentRunConfig, signal?: AbortSignal) {
|
|
// Get API key from storage
|
|
const apiKey = await getAppStorage().providerKeys.get(cfg.model.provider);
|
|
if (!apiKey) {
|
|
throw new Error("no-api-key");
|
|
}
|
|
|
|
// Check if CORS proxy is enabled
|
|
const proxyEnabled = await getAppStorage().settings.get<boolean>("proxy.enabled");
|
|
const proxyUrl = await getAppStorage().settings.get<string>("proxy.url");
|
|
|
|
// Clone model and modify baseUrl if proxy is enabled
|
|
let model = cfg.model;
|
|
if (proxyEnabled && proxyUrl && cfg.model.baseUrl) {
|
|
model = {
|
|
...cfg.model,
|
|
baseUrl: `${proxyUrl}/?url=${encodeURIComponent(cfg.model.baseUrl)}`,
|
|
};
|
|
}
|
|
|
|
// Messages are already LLM-compatible (filtered by Agent)
|
|
const context: AgentContext = {
|
|
systemPrompt: cfg.systemPrompt,
|
|
messages,
|
|
tools: cfg.tools,
|
|
};
|
|
|
|
const pc: PromptConfig = {
|
|
model,
|
|
reasoning: cfg.reasoning,
|
|
apiKey,
|
|
};
|
|
|
|
// Yield events from agentLoop
|
|
for await (const ev of agentLoop(userMessage as unknown as UserMessage, context, pc, signal)) {
|
|
yield ev;
|
|
}
|
|
}
|
|
}
|