mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-12-05 22:39:19 +00:00
Every remote contract declares one shape: members only some callers get are grantable(...), and its provider grants them per principal through admit (grantBy in src/runtime/chord.ts; notGranted/ungranted for a factory branch's members its principal never gets). Call sites read grants instead of variant names: live/liveWith/kept/keptWith/withGranted/expectGranted (src/runtime/refs.ts) replace liveIn/keptIn/withHeld(ref, variant)/expectVariant; useObject has no variant option. Old contract versions are refused (Chord slice 15): every remote contract is a new history base with its version bumped; the variant-era histories and accepts of Sessions, SessionTree, Envs, Projects, Views, Mcp, Discord, Search, SessionStatus and Subagents collapse into one declaration each, so pages, TUIs and workers loaded before the deploy get contract_mismatch and reload once. Catalog (local) keeps its history. Tests: holdWith/boundWith, stubs take the grants, contract-versions tests Catalog and the refusal.
64 lines
2.8 KiB
TypeScript
64 lines
2.8 KiB
TypeScript
import { defineFacet, deriveTyped, followNative, forbidden, isOwner } from "@p2/app/v1";
|
|
import { Server, serverOf } from "@p2/app/v1/server";
|
|
import { openResources, resourceState } from "./config.ts";
|
|
import { ModelConfig, ModelResources, Models } from "./contract.ts";
|
|
import { resolveModelScope } from "./scope.ts";
|
|
|
|
export default defineFacet((host) => {
|
|
host.serve(ModelResources, { compose: (table) => table[0]?.value });
|
|
host.serve(ModelConfig, (_params, self) => {
|
|
const server = self.get(Server, {});
|
|
const natives = serverOf(self);
|
|
const resources = self.open(
|
|
() => openResources(server.info.value.dataDir, server.info.value.fake, natives().resourceOptions),
|
|
(opened) => opened.close(),
|
|
);
|
|
self.contribute(ModelResources, {
|
|
get resources() {
|
|
return resources.value;
|
|
},
|
|
});
|
|
// The configuration files' state as the resources hold it: a save below resolves after its reload, which
|
|
// told this source, so the caller's answer comes with the new state.
|
|
const state = followNative(
|
|
self,
|
|
(changed) => resources.value.subscribe(changed),
|
|
() => resourceState(resources.value, server.info.value.fake),
|
|
);
|
|
return {
|
|
summary: self.derive([state], ([value]) => value.summary),
|
|
snapshot: deriveTyped(self, [state], ([value]) => value.snapshot),
|
|
setDefault: async ({ provider, modelId, thinkingLevel }) => {
|
|
const opened = resources.value;
|
|
if (!opened.models.getModel(provider, modelId)) throw new Error("Unknown model");
|
|
await opened.saveSettings({ ...opened.settings, defaultModel: { provider, modelId }, thinkingLevel });
|
|
return null;
|
|
},
|
|
setEnabledModels: async ({ patterns }) => {
|
|
const opened = resources.value;
|
|
const { enabledModels: _previous, ...settings } = opened.settings;
|
|
const cleaned = patterns.map((pattern) => pattern.trim()).filter(Boolean);
|
|
await opened.saveSettings(cleaned.length ? { ...settings, enabledModels: cleaned } : settings);
|
|
return null;
|
|
},
|
|
};
|
|
});
|
|
|
|
host.serve(Models, (_params, principal, self) => {
|
|
if (!isOwner(principal)) throw forbidden("models are owner-only");
|
|
const config = self.get(ModelConfig, {});
|
|
return {
|
|
models: self.derive([config.summary], ([summary]) => summary.models),
|
|
defaultModel: self.derive([config.summary], ([summary]) => summary.defaultModel),
|
|
errors: self.derive([config.summary], ([summary]) => summary.errors),
|
|
enabledModels: self.derive([config.snapshot], ([snapshot]) => snapshot.settings.enabledModels ?? []),
|
|
scopedModels: self.derive(
|
|
[config.summary, config.snapshot],
|
|
([summary, snapshot]) => resolveModelScope(snapshot.settings.enabledModels ?? [], summary.models).models,
|
|
),
|
|
resources: config.snapshot,
|
|
setDefault: (choice, ctx) => config.setDefault(choice, ctx),
|
|
setEnabledModels: (input, ctx) => config.setEnabledModels(input, ctx),
|
|
};
|
|
});
|
|
});
|