@fungi.computer/oyster 0.18.2 → 0.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,13 +21,28 @@ export declare class AgentModelRefused extends Error {
21
21
  readonly reason: AgentModelRefusal;
22
22
  constructor(reason: AgentModelRefusal);
23
23
  }
24
+ /**
25
+ * An Agent with no saved choice runs on a model its host can reach, chosen when
26
+ * it runs: Workers AI where a Cloudflare account answers, then the Fungi
27
+ * gateway, then the first stored provider key's catalog default. A key added
28
+ * later just works; one removed moves the default on. Nothing is saved, so an
29
+ * Agent's own choice always wins.
30
+ */
31
+ export declare function resolveDefaultChoice(ready: Readonly<{
32
+ workersAi: boolean;
33
+ gateway: boolean;
34
+ keys: readonly Readonly<{
35
+ keyId: string;
36
+ provider: string;
37
+ }>[];
38
+ }>): Promise<AgentModelChoice>;
24
39
  /** Read the maintained Pi catalog; never accept a model from a different egress. */
25
40
  export declare function selectAgentModel(choice: AgentModelChoice): Promise<Model<Api>>;
26
41
  /** The Agent DO is the only durable owner. Absence is a new-format default, not an old-env reader. */
27
42
  export declare function agentModelChoice(storage: Readonly<{
28
43
  get<T = unknown>(key: string): Promise<T | undefined>;
29
44
  put(key: string, value: AgentModelChoice): Promise<void>;
30
- }>, admit: (choice: AgentModelChoice) => void | Promise<void>): {
45
+ }>, admit: (choice: AgentModelChoice) => void | Promise<void>, fallback?: () => Promise<AgentModelChoice>): {
31
46
  read(): Promise<AgentModelChoice>;
32
47
  set(input: AgentModelChoice): Promise<AgentModelChoice>;
33
48
  };
@@ -41,6 +41,38 @@ async function byokCatalog(provider) {
41
41
  .openrouterProvider()
42
42
  .getModels();
43
43
  }
44
+ /**
45
+ * An Agent with no saved choice runs on a model its host can reach, chosen when
46
+ * it runs: Workers AI where a Cloudflare account answers, then the Fungi
47
+ * gateway, then the first stored provider key's catalog default. A key added
48
+ * later just works; one removed moves the default on. Nothing is saved, so an
49
+ * Agent's own choice always wins.
50
+ */
51
+ export async function resolveDefaultChoice(ready) {
52
+ if (ready.workersAi)
53
+ return defaultChoice;
54
+ if (ready.gateway)
55
+ return {
56
+ keySource: "ai-gateway",
57
+ model: { providerId: "openai", modelId: "openai/gpt-6-luna" },
58
+ };
59
+ for (const { keyId, provider } of ready.keys) {
60
+ if (provider !== "anthropic" &&
61
+ provider !== "openai" &&
62
+ provider !== "openrouter")
63
+ continue;
64
+ const catalog = await byokCatalog(provider);
65
+ const model = catalog.find((entry) => "default" in entry && entry.default === true) ??
66
+ catalog[0];
67
+ if (model !== undefined)
68
+ return {
69
+ keySource: "byok",
70
+ keyId,
71
+ model: { providerId: provider, modelId: model.id },
72
+ };
73
+ }
74
+ return defaultChoice;
75
+ }
44
76
  /** Read the maintained Pi catalog; never accept a model from a different egress. */
45
77
  export async function selectAgentModel(choice) {
46
78
  const { providerId, modelId, reasoning } = choice.model;
@@ -72,12 +104,12 @@ export async function selectAgentModel(choice) {
72
104
  return model;
73
105
  }
74
106
  /** The Agent DO is the only durable owner. Absence is a new-format default, not an old-env reader. */
75
- export function agentModelChoice(storage, admit) {
107
+ export function agentModelChoice(storage, admit, fallback = () => Promise.resolve(defaultChoice)) {
76
108
  return {
77
109
  async read() {
78
110
  const stored = await storage.get(key);
79
111
  return stored === undefined
80
- ? defaultChoice
112
+ ? fallback()
81
113
  : Schema.decodeUnknownSync(agentModelChoiceSchema, {
82
114
  onExcessProperty: "error",
83
115
  })(stored);
@@ -43,7 +43,7 @@ import { standaloneBrowserLiveView, standaloneBrowserSource, } from "./browser.j
43
43
  import { standaloneWebSource } from "./web.js";
44
44
  import { configuredProviderEgress, providerFetch, } from "@fungi.computer/veil/gatekeeper";
45
45
  import { createAssistantMessageEventStream } from "@earendil-works/pi-ai/utils/event-stream";
46
- import { agentModelChoice, agentModelChoiceSchema, AgentModelRefused, selectAgentModel, } from "./agent-model-choice.js";
46
+ import { agentModelChoice, agentModelChoiceSchema, AgentModelRefused, resolveDefaultChoice, selectAgentModel, } from "./agent-model-choice.js";
47
47
  import { CLOUDFLARE_GATEWAY_BINDING_AUTH_SENTINEL, createAiBindingFetch, } from "@earendil-works/pi-ai/api/cloudflare-ai-binding";
48
48
  import { cloudflareAccountReady } from "./cloudflare-account.js";
49
49
  import { cloudflareWorkersAIProvider } from "@earendil-works/pi-ai/providers/cloudflare-workers-ai";
@@ -877,6 +877,15 @@ export class StandaloneAgentBase extends ShiitakeDO {
877
877
  throw new AgentModelRefused("key_not_connected");
878
878
  if (choice.keySource === "workers-ai" && this.env.AI === undefined)
879
879
  throw new AgentModelRefused("key_not_connected");
880
+ }, async () => {
881
+ const pin = await this.policyDirectory();
882
+ return resolveDefaultChoice({
883
+ workersAi: this.env.AI !== undefined && cloudflareAccountReady(this.env),
884
+ gateway: configuredProviderEgress(this.env).baseUrl("ai-gateway") !== null,
885
+ keys: pin === null
886
+ ? []
887
+ : await this.env.AGENT_DIRECTORY.getByName(pin.directoryName).listModelKeys(),
888
+ });
880
889
  });
881
890
  }
882
891
  /** Secret-free default of this Agent; stored separately from Session history. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/oyster",
3
- "version": "0.18.2",
3
+ "version": "0.18.3",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Run your own Apps on Cloudflare Workers or celld: installable App Releases, each with its own Durable Object, SQLite database and declared bindings",
6
6
  "type": "module",