@opengeni/config 1.3.0 → 1.4.2

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.
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { managedUserEmailAllowed } from "./managed-user-admission";
2
+ export * from "./realtime-voice-pricing";
3
+ export * from "./web-search";
2
4
  import { EnvCreditPromotionPolicy } from "./credit-promotions";
3
5
  export {
4
6
  CreditPromotionPolicy,
@@ -408,6 +410,11 @@ const SettingsSchema = z.object({
408
410
  delegationSecret: z.string().optional(),
409
411
  defaultFirstPartyMcpTools: EnvFirstPartyMcpTools,
410
412
  allowedFirstPartyMcpTools: EnvFirstPartyMcpTools,
413
+ // Deployment fact: the isolated artifact materializer workload runs and
414
+ // drains export jobs. Without it an export is queued forever, so the export
415
+ // tools leave the first-party ceiling. Helm sets this from
416
+ // artifactMaterializer.enabled.
417
+ artifactMaterializerDeployed: EnvBoolean.default(false),
411
418
  // sandbox workspace scoped stream-token HMAC secret (sandbox contract §C.3 / stream-token availability contract).
412
419
  // When unset, the API falls back to `delegationSecret` (the same HMAC envelope
413
420
  // family, `ogs_` vs `ogd_` prefix). REQUIRED-WHEN-DESKTOP, but the absence of
@@ -725,6 +732,35 @@ const SettingsSchema = z.object({
725
732
  voiceInputAzureApiVersion: z.string().optional(),
726
733
  voiceInputAzureApiKey: z.string().optional(),
727
734
  voiceInputAzureAdToken: z.string().optional(),
735
+ // Hosted realtime voice is configured independently of dictation.
736
+ azureLiveEndpoint: z.string().url().optional(),
737
+ azureLiveApiKey: z.string().optional(),
738
+ azureLiveDeployment: z.string().default("gpt-live-1"),
739
+ azureLiveVoice: z.string().default("marin"),
740
+ // Credit price of deployment-funded live voice (RealtimeVoicePricing JSON).
741
+ // Required while credit billing is active, otherwise the model is withheld.
742
+ azureLivePricingJson: z.string().optional(),
743
+ // Managed AI Gateway live voice prices keyed by upstream model id.
744
+ aiGatewayRealtimePricingJson: z.string().optional(),
745
+ // Azure Speech credentials are explicit: a different resource can host MAI.
746
+ voiceInputMaiEndpoint: z.string().url().optional(),
747
+ voiceInputMaiApiKey: z.string().optional(),
748
+ voiceInputMaiApiVersion: z.string().default("2025-10-15"),
749
+ voiceInputMaiPricingJson: z.string().optional(),
750
+ voiceInputMaiModel: z.string().default("MAI-Transcribe-2"),
751
+ // Underlying model of the Azure deployment (for example gpt-4o-transcribe or
752
+ // whisper). Azure routes by deployment name, so this only selects built-in
753
+ // pricing and the provider-reported usage shape (Whisper requests
754
+ // verbose_json so Azure reports the billed audio duration). Defaults to the
755
+ // deployment name.
756
+ voiceInputAzureModel: z.string().trim().min(1).max(256).optional(),
757
+ // Opengeni credit pricing for deployment-funded voice input (`openai`,
758
+ // `azure-openai`). JSON VoiceInputPricing; overrides the built-in list price
759
+ // for the configured model. Validated at boot. When credit billing is active
760
+ // (OPENGENI_BILLING_MODE=stripe or OPENGENI_USAGE_LIMITS_MODE=managed) a
761
+ // deployment-funded provider without any pricing is not offered at all.
762
+ voiceInputOpenaiPricingJson: z.string().optional(),
763
+ voiceInputAzurePricingJson: z.string().optional(),
728
764
  // Legacy opt-in for undocumented ChatGPT /backend-api/transcribe. When
729
765
  // OPENGENI_CODEX_SUBSCRIPTION_ENABLED is true, Codex STT is included without
730
766
  // this flag. Set false and omit codex-subscription from PROVIDER_ORDER to
@@ -847,6 +883,18 @@ const SettingsSchema = z.object({
847
883
  // merged with the MCP-server tools (getAllTools = [...mcpTools, ...tools])
848
884
  // and the sandbox capability tools, never replacing them.
849
885
  webSearchEnabled: EnvBoolean.default(true),
886
+ // Provider-agnostic web search (`web_search` / `web_fetch` agent tools,
887
+ // see ./web-search.ts). Inert until a provider is named; validated lazily so
888
+ // a misconfiguration withholds the tools instead of failing boot.
889
+ webSearchProvider: z.string().optional(),
890
+ webSearchApiKey: z.string().optional(),
891
+ webSearchBaseUrl: z.string().optional(),
892
+ webFetchProvider: z.string().optional(),
893
+ webFetchApiKey: z.string().optional(),
894
+ webFetchBaseUrl: z.string().optional(),
895
+ webSearchProviderMode: z.string().optional(),
896
+ webSearchPricingJson: z.string().optional(),
897
+ webSearchRequestTimeoutMs: z.coerce.number().int().positive().max(120_000).default(20_000),
850
898
  // Jev (TypeSafe's fast judge model) for worker-side agent tools. Without a
851
899
  // usable key every Jev-backed feature is off. The key stays on the server
852
900
  // (API and worker) and never reaches a sandbox or Connected Machine.
@@ -1665,16 +1713,28 @@ export function personalGitHubOAuthCallbackUrl(publicBaseUrl: string | undefined
1665
1713
  export type VoiceInputProviderId =
1666
1714
  | "openai"
1667
1715
  | "azure-openai"
1716
+ | "azure-mai"
1668
1717
  | "codex-subscription"
1669
1718
  | "supergrok-subscription";
1670
1719
 
1671
1720
  export type VoiceInputProviderConfig =
1721
+ | {
1722
+ id: "azure-mai";
1723
+ kind: "azure-mai";
1724
+ pricing: VoiceInputPricing | null;
1725
+ endpoint: string;
1726
+ apiKey: string;
1727
+ apiVersion: string;
1728
+ model: string;
1729
+ }
1672
1730
  | {
1673
1731
  id: "openai";
1674
1732
  kind: "openai";
1675
1733
  apiKey: string;
1676
1734
  baseUrl: string;
1677
1735
  model: string;
1736
+ /** Deployment-funded: Opengeni pays upstream and charges credits. */
1737
+ pricing: VoiceInputPricing | null;
1678
1738
  }
1679
1739
  | {
1680
1740
  id: "azure-openai";
@@ -1684,6 +1744,10 @@ export type VoiceInputProviderConfig =
1684
1744
  apiVersion: string;
1685
1745
  apiKey: string | null;
1686
1746
  adToken: string | null;
1747
+ /** Underlying model (pricing + response shape); defaults to the deployment name. */
1748
+ model: string;
1749
+ /** Deployment-funded: Opengeni pays upstream and charges credits. */
1750
+ pricing: VoiceInputPricing | null;
1687
1751
  }
1688
1752
  | {
1689
1753
  id: "codex-subscription";
@@ -1707,10 +1771,7 @@ export function usableJevApiKey(settings: Pick<Settings, "jevApiKey">): string |
1707
1771
 
1708
1772
  /** Deployment half of agent configuration: hard capability limits. */
1709
1773
  export function agentConfigDeploymentPolicy(
1710
- settings: Pick<
1711
- Settings,
1712
- "webSearchEnabled" | "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools"
1713
- >,
1774
+ settings: FirstPartyMcpToolPolicySettings & Pick<Settings, "webSearchEnabled">,
1714
1775
  ): AgentConfigDeploymentLimits {
1715
1776
  return agentConfigDeploymentLimitsFromAllowlist(
1716
1777
  resolveFirstPartyMcpToolPolicy(settings).allowed,
@@ -1767,6 +1828,58 @@ export function isUsableVoiceInputSecret(value: string | null | undefined): valu
1767
1828
  * whether at least one non-experimental (or probed experimental) provider exists.
1768
1829
  */
1769
1830
  export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInputProviderConfig[] {
1831
+ return voiceInputProviderRegistryWithIssues(settings).providers;
1832
+ }
1833
+
1834
+ /** Why a credentialed deployment-funded voice provider is withheld. */
1835
+ export type VoiceInputPricingIssue = {
1836
+ providerId: "openai" | "azure-openai" | "azure-mai";
1837
+ env: string;
1838
+ reason: "malformed" | "unpriced";
1839
+ message: string;
1840
+ };
1841
+
1842
+ /**
1843
+ * Pricing problems that withhold a credentialed deployment-funded provider.
1844
+ * A malformed PRICING_JSON never fails boot (workers share this config and
1845
+ * must not crash-loop over a voice-only setting); the API logs each issue
1846
+ * once at startup and the provider stays unavailable.
1847
+ */
1848
+ export function voiceInputPricingIssues(settings: Settings): VoiceInputPricingIssue[] {
1849
+ return voiceInputProviderRegistryWithIssues(settings).issues;
1850
+ }
1851
+
1852
+ function voiceInputProviderPricing(
1853
+ settings: Settings,
1854
+ issues: VoiceInputPricingIssue[],
1855
+ providerId: VoiceInputPricingIssue["providerId"],
1856
+ env: string,
1857
+ raw: string | undefined,
1858
+ model: string,
1859
+ ): { usable: true; pricing: VoiceInputPricing | null } | { usable: false } {
1860
+ const resolved = safeResolveVoiceInputPricing(raw, model, env);
1861
+ if (!resolved.ok) {
1862
+ issues.push({ providerId, env, reason: "malformed", message: resolved.error });
1863
+ return { usable: false };
1864
+ }
1865
+ if (!resolved.pricing && voiceInputCreditBillingActive(settings)) {
1866
+ // Never serve unpriced deployment-paid audio on a credit-billed deployment.
1867
+ issues.push({
1868
+ providerId,
1869
+ env,
1870
+ reason: "unpriced",
1871
+ message: `${env} is required: credit billing is active and model "${model}" has no built-in voice price`,
1872
+ });
1873
+ return { usable: false };
1874
+ }
1875
+ return { usable: true, pricing: resolved.pricing };
1876
+ }
1877
+
1878
+ function voiceInputProviderRegistryWithIssues(settings: Settings): {
1879
+ providers: VoiceInputProviderConfig[];
1880
+ issues: VoiceInputPricingIssue[];
1881
+ } {
1882
+ const issues: VoiceInputPricingIssue[] = [];
1770
1883
  const order = settings.voiceInputProviderOrder
1771
1884
  .split(",")
1772
1885
  .map((part) => part.trim())
@@ -1774,6 +1887,7 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1774
1887
  (part): part is VoiceInputProviderId =>
1775
1888
  part === "openai" ||
1776
1889
  part === "azure-openai" ||
1890
+ part === "azure-mai" ||
1777
1891
  part === "codex-subscription" ||
1778
1892
  part === "supergrok-subscription",
1779
1893
  );
@@ -1782,6 +1896,32 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1782
1896
  for (const id of order) {
1783
1897
  if (seen.has(id)) continue;
1784
1898
  seen.add(id);
1899
+ if (id === "azure-mai") {
1900
+ if (
1901
+ !settings.voiceInputMaiEndpoint ||
1902
+ !isUsableVoiceInputSecret(settings.voiceInputMaiApiKey)
1903
+ )
1904
+ continue;
1905
+ const priced = voiceInputProviderPricing(
1906
+ settings,
1907
+ issues,
1908
+ "azure-mai",
1909
+ "OPENGENI_VOICE_INPUT_MAI_PRICING_JSON",
1910
+ settings.voiceInputMaiPricingJson,
1911
+ settings.voiceInputMaiModel,
1912
+ );
1913
+ if (!priced.usable) continue;
1914
+ providers.push({
1915
+ id,
1916
+ kind: "azure-mai",
1917
+ pricing: priced.pricing,
1918
+ endpoint: settings.voiceInputMaiEndpoint.replace(/\/+$/, ""),
1919
+ apiKey: settings.voiceInputMaiApiKey,
1920
+ apiVersion: settings.voiceInputMaiApiVersion,
1921
+ model: settings.voiceInputMaiModel,
1922
+ });
1923
+ continue;
1924
+ }
1785
1925
  if (id === "openai") {
1786
1926
  if (!settings.voiceInputOpenaiEnabled) continue;
1787
1927
  const apiKey = settings.voiceInputOpenaiApiKey ?? settings.openaiApiKey;
@@ -1795,9 +1935,19 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1795
1935
  ) {
1796
1936
  continue;
1797
1937
  }
1938
+ const priced = voiceInputProviderPricing(
1939
+ settings,
1940
+ issues,
1941
+ "openai",
1942
+ "OPENGENI_VOICE_INPUT_OPENAI_PRICING_JSON",
1943
+ settings.voiceInputOpenaiPricingJson,
1944
+ settings.voiceInputOpenaiModel,
1945
+ );
1946
+ if (!priced.usable) continue;
1798
1947
  providers.push({
1799
1948
  id: "openai",
1800
1949
  kind: "openai",
1950
+ pricing: priced.pricing,
1801
1951
  apiKey,
1802
1952
  baseUrl: (
1803
1953
  settings.voiceInputOpenaiBaseUrl ??
@@ -1840,6 +1990,17 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1840
1990
  ) {
1841
1991
  continue;
1842
1992
  }
1993
+ const model = settings.voiceInputAzureModel ?? deployment;
1994
+ const priced = voiceInputProviderPricing(
1995
+ settings,
1996
+ issues,
1997
+ "azure-openai",
1998
+ "OPENGENI_VOICE_INPUT_AZURE_PRICING_JSON",
1999
+ settings.voiceInputAzurePricingJson,
2000
+ model,
2001
+ );
2002
+ if (!priced.usable) continue;
2003
+ const pricing = priced.pricing;
1843
2004
  providers.push({
1844
2005
  id: "azure-openai",
1845
2006
  kind: "azure-openai",
@@ -1848,6 +2009,8 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1848
2009
  apiVersion,
1849
2010
  apiKey: isUsableVoiceInputSecret(apiKey) ? apiKey : null,
1850
2011
  adToken: isUsableVoiceInputSecret(adToken) ? adToken : null,
2012
+ model,
2013
+ pricing,
1851
2014
  });
1852
2015
  continue;
1853
2016
  }
@@ -1874,7 +2037,251 @@ export function resolveVoiceInputProviderRegistry(settings: Settings): VoiceInpu
1874
2037
  });
1875
2038
  }
1876
2039
  }
1877
- return providers;
2040
+ return { providers, issues };
2041
+ }
2042
+
2043
+ /**
2044
+ * Opengeni credit price for one deployment-funded transcription provider.
2045
+ * Rates are upstream list prices in integer USD micros; `marginBps` (500 =
2046
+ * +5%) is added on top, exactly like model pricing. `microsPerMinute` prices
2047
+ * provider-reported audio duration (Whisper, `usage.type: "duration"`) and is
2048
+ * a server-measured WAV duration when provider usage is absent. Token
2049
+ * rates price `usage.type: "tokens"` (gpt-4o-transcribe family); `input`
2050
+ * covers text input tokens and, unless `audioInput` is set, audio tokens.
2051
+ */
2052
+ export type VoiceInputPricing = {
2053
+ microsPerMinute: number;
2054
+ inputMicrosPerMillionTokens?: number | undefined;
2055
+ audioInputMicrosPerMillionTokens?: number | undefined;
2056
+ outputMicrosPerMillionTokens?: number | undefined;
2057
+ marginBps?: number | undefined;
2058
+ };
2059
+
2060
+ const VoiceInputPricingSchema = z
2061
+ .object({
2062
+ microsPerMinute: z.number().int().positive().max(100_000_000),
2063
+ inputMicrosPerMillionTokens: z.number().int().nonnegative().max(1_000_000_000).optional(),
2064
+ audioInputMicrosPerMillionTokens: z.number().int().nonnegative().max(1_000_000_000).optional(),
2065
+ outputMicrosPerMillionTokens: z.number().int().nonnegative().max(1_000_000_000).optional(),
2066
+ marginBps: z.number().int().min(0).max(100_000).optional(),
2067
+ })
2068
+ .strict()
2069
+ .superRefine((pricing, ctx) => {
2070
+ const input = pricing.inputMicrosPerMillionTokens !== undefined;
2071
+ const output = pricing.outputMicrosPerMillionTokens !== undefined;
2072
+ if (input !== output) {
2073
+ ctx.addIssue({
2074
+ code: "custom",
2075
+ message: "inputMicrosPerMillionTokens and outputMicrosPerMillionTokens are set together",
2076
+ });
2077
+ }
2078
+ if (pricing.audioInputMicrosPerMillionTokens !== undefined && !input) {
2079
+ ctx.addIssue({
2080
+ code: "custom",
2081
+ message: "audioInputMicrosPerMillionTokens requires the token input/output rates",
2082
+ });
2083
+ }
2084
+ });
2085
+
2086
+ /**
2087
+ * Built-in list prices (OpenAI API pricing, "Transcription models" table)
2088
+ * plus the default 5% Opengeni margin used for model pricing. Azure Global
2089
+ * Standard deployments use the same list prices; set the provider
2090
+ * PRICING_JSON when a deployment's contracted or regional price differs.
2091
+ */
2092
+ export const defaultVoiceInputPricing: Readonly<Record<string, VoiceInputPricing>> = {
2093
+ // Audio tokens are listed separately from text tokens on the model pages;
2094
+ // set explicitly so the audio rate never silently follows a text-rate edit.
2095
+ "gpt-4o-transcribe": {
2096
+ microsPerMinute: 6_000,
2097
+ inputMicrosPerMillionTokens: 2_500_000,
2098
+ audioInputMicrosPerMillionTokens: 2_500_000,
2099
+ outputMicrosPerMillionTokens: 10_000_000,
2100
+ marginBps: 500,
2101
+ },
2102
+ "gpt-4o-transcribe-diarize": {
2103
+ microsPerMinute: 6_000,
2104
+ inputMicrosPerMillionTokens: 2_500_000,
2105
+ audioInputMicrosPerMillionTokens: 2_500_000,
2106
+ outputMicrosPerMillionTokens: 10_000_000,
2107
+ marginBps: 500,
2108
+ },
2109
+ "gpt-4o-mini-transcribe": {
2110
+ microsPerMinute: 3_000,
2111
+ inputMicrosPerMillionTokens: 1_250_000,
2112
+ audioInputMicrosPerMillionTokens: 1_250_000,
2113
+ outputMicrosPerMillionTokens: 5_000_000,
2114
+ marginBps: 500,
2115
+ },
2116
+ "gpt-transcribe": { microsPerMinute: 4_500, marginBps: 500 },
2117
+ whisper: { microsPerMinute: 6_000, marginBps: 500 },
2118
+ "whisper-1": { microsPerMinute: 6_000, marginBps: 500 },
2119
+ };
2120
+
2121
+ /** Lowercased model with a dated snapshot suffix (`-2025-03-20`) removed. */
2122
+ export function canonicalVoiceInputModel(model: string): string {
2123
+ return model
2124
+ .trim()
2125
+ .toLowerCase()
2126
+ .replace(/-\d{4}-\d{2}-\d{2}$/, "");
2127
+ }
2128
+
2129
+ /** Whisper reports its billed audio duration only in `verbose_json`. */
2130
+ export function voiceInputModelReportsDurationOnly(model: string): boolean {
2131
+ return canonicalVoiceInputModel(model).startsWith("whisper");
2132
+ }
2133
+
2134
+ /** Parse one provider pricing JSON. Throws a configuration error when malformed. */
2135
+ export function parseVoiceInputPricingJson(
2136
+ raw: string | undefined,
2137
+ env = "voice-input pricing JSON",
2138
+ ): VoiceInputPricing | null {
2139
+ if (raw === undefined || raw.trim() === "") return null;
2140
+ let parsed: unknown;
2141
+ try {
2142
+ parsed = JSON.parse(raw);
2143
+ } catch {
2144
+ throw new Error(`${env} must be valid JSON`);
2145
+ }
2146
+ const result = VoiceInputPricingSchema.safeParse(parsed);
2147
+ if (!result.success) {
2148
+ throw new Error(
2149
+ `${env} is invalid: ${result.error.issues.map((issue) => issue.message).join("; ")}`,
2150
+ );
2151
+ }
2152
+ return result.data;
2153
+ }
2154
+
2155
+ /** Explicit provider pricing JSON wins; otherwise the built-in list price for the model. */
2156
+ export function resolveVoiceInputPricing(
2157
+ raw: string | undefined,
2158
+ model: string,
2159
+ ): VoiceInputPricing | null {
2160
+ return (
2161
+ parseVoiceInputPricingJson(raw) ??
2162
+ defaultVoiceInputPricing[canonicalVoiceInputModel(model)] ??
2163
+ null
2164
+ );
2165
+ }
2166
+
2167
+ /** Non-throwing {@link resolveVoiceInputPricing} for registry resolution. */
2168
+ export function safeResolveVoiceInputPricing(
2169
+ raw: string | undefined,
2170
+ model: string,
2171
+ env?: string,
2172
+ ): { ok: true; pricing: VoiceInputPricing | null } | { ok: false; error: string } {
2173
+ let explicit: VoiceInputPricing | null;
2174
+ try {
2175
+ explicit = parseVoiceInputPricingJson(raw, env);
2176
+ } catch (error) {
2177
+ return { ok: false, error: error instanceof Error ? error.message : String(error) };
2178
+ }
2179
+ return {
2180
+ ok: true,
2181
+ pricing: explicit ?? defaultVoiceInputPricing[canonicalVoiceInputModel(model)] ?? null,
2182
+ };
2183
+ }
2184
+
2185
+ /** Same predicate as model/credit limits: credit balance and debits are enforced. */
2186
+ export function voiceInputCreditBillingActive(
2187
+ settings: Pick<Settings, "billingMode" | "usageLimitsMode">,
2188
+ ): boolean {
2189
+ return settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
2190
+ }
2191
+
2192
+ /** Provider-reported transcription usage, normalized. Never client-reported. */
2193
+ export type VoiceInputUsage =
2194
+ | {
2195
+ kind: "tokens";
2196
+ inputTokens: number;
2197
+ audioInputTokens: number;
2198
+ textInputTokens: number;
2199
+ outputTokens: number;
2200
+ }
2201
+ | { kind: "duration"; seconds: number };
2202
+
2203
+ export type VoiceInputCost = {
2204
+ /** Upstream list cost before margin, rounded up to whole micros. */
2205
+ providerCostMicros: number;
2206
+ /** Opengeni credit price after margin, rounded up to whole micros. */
2207
+ creditCostMicros: number;
2208
+ };
2209
+
2210
+ /** True when token usage can be priced exactly. */
2211
+ export function voiceInputPricingHasTokenRates(pricing: VoiceInputPricing): boolean {
2212
+ return (
2213
+ pricing.inputMicrosPerMillionTokens !== undefined &&
2214
+ pricing.outputMicrosPerMillionTokens !== undefined
2215
+ );
2216
+ }
2217
+
2218
+ /**
2219
+ * Exact integer credit math. Token usage needs token rates; duration usage
2220
+ * uses `microsPerMinute` at millisecond resolution. Both round up once, after
2221
+ * summing, so a positive use never becomes a free debit.
2222
+ */
2223
+ export function calculateVoiceInputCost(
2224
+ pricing: VoiceInputPricing,
2225
+ usage: VoiceInputUsage,
2226
+ ): VoiceInputCost {
2227
+ let numerator: bigint;
2228
+ let denominator: bigint;
2229
+ if (usage.kind === "tokens") {
2230
+ if (
2231
+ pricing.inputMicrosPerMillionTokens === undefined ||
2232
+ pricing.outputMicrosPerMillionTokens === undefined
2233
+ ) {
2234
+ throw new Error("voice-input token usage requires token pricing");
2235
+ }
2236
+ for (const value of [
2237
+ usage.inputTokens,
2238
+ usage.audioInputTokens,
2239
+ usage.textInputTokens,
2240
+ usage.outputTokens,
2241
+ ]) {
2242
+ if (!Number.isSafeInteger(value) || value < 0) {
2243
+ throw new Error("voice-input token usage must be non-negative integers");
2244
+ }
2245
+ }
2246
+ const audioRate =
2247
+ pricing.audioInputMicrosPerMillionTokens ?? pricing.inputMicrosPerMillionTokens;
2248
+ // Input tokens the provider did not attribute are priced as audio (never cheaper).
2249
+ const unattributed = Math.max(
2250
+ 0,
2251
+ usage.inputTokens - usage.audioInputTokens - usage.textInputTokens,
2252
+ );
2253
+ numerator =
2254
+ BigInt(usage.audioInputTokens + unattributed) * BigInt(audioRate) +
2255
+ BigInt(usage.textInputTokens) * BigInt(pricing.inputMicrosPerMillionTokens) +
2256
+ BigInt(usage.outputTokens) * BigInt(pricing.outputMicrosPerMillionTokens);
2257
+ denominator = 1_000_000n;
2258
+ } else {
2259
+ if (!Number.isFinite(usage.seconds) || usage.seconds < 0) {
2260
+ throw new Error("voice-input duration must be a non-negative number of seconds");
2261
+ }
2262
+ numerator = BigInt(Math.ceil(usage.seconds * 1_000)) * BigInt(pricing.microsPerMinute);
2263
+ denominator = 60_000n;
2264
+ }
2265
+ const margin = BigInt(10_000 + (pricing.marginBps ?? 0));
2266
+ const provider = (numerator + denominator - 1n) / denominator;
2267
+ const scaled = denominator * 10_000n;
2268
+ const credit = (numerator * margin + scaled - 1n) / scaled;
2269
+ if (credit > BigInt(Number.MAX_SAFE_INTEGER)) {
2270
+ throw new Error("voice-input cost exceeds the supported billing range");
2271
+ }
2272
+ return { providerCostMicros: Number(provider), creditCostMicros: Number(credit) };
2273
+ }
2274
+
2275
+ /**
2276
+ * Deployment-funded providers that have credentials but are withheld because
2277
+ * credit billing is active and no price is known. The API logs these (and
2278
+ * malformed pricing, see {@link voiceInputPricingIssues}) once at startup so
2279
+ * an operator sees why voice input stayed unavailable.
2280
+ */
2281
+ export function unpricedVoiceInputProviders(settings: Settings): VoiceInputProviderId[] {
2282
+ return voiceInputPricingIssues(settings).flatMap((issue) =>
2283
+ issue.reason === "unpriced" ? [issue.providerId] : [],
2284
+ );
1878
2285
  }
1879
2286
 
1880
2287
  /** True when the deployment has at least one supported (non-experimental) provider. */
@@ -3487,6 +3894,7 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3487
3894
  delegationSecret: optional("OPENGENI_DELEGATION_SECRET"),
3488
3895
  defaultFirstPartyMcpTools: optional("OPENGENI_DEFAULT_FIRST_PARTY_MCP_TOOLS"),
3489
3896
  allowedFirstPartyMcpTools: optional("OPENGENI_ALLOWED_FIRST_PARTY_MCP_TOOLS"),
3897
+ artifactMaterializerDeployed: optional("OPENGENI_ARTIFACT_MATERIALIZER_DEPLOYED"),
3490
3898
  streamTokenSecret: optional("OPENGENI_STREAM_TOKEN_SECRET"),
3491
3899
  streamControlEnabled: optional("OPENGENI_STREAM_CONTROL_ENABLED"),
3492
3900
  workDiscoveryEnabled: optional("OPENGENI_WORK_DISCOVERY_ENABLED"),
@@ -3614,6 +4022,20 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3614
4022
  voiceInputAzureApiVersion: optional("OPENGENI_VOICE_INPUT_AZURE_API_VERSION"),
3615
4023
  voiceInputAzureApiKey: optional("OPENGENI_VOICE_INPUT_AZURE_API_KEY"),
3616
4024
  voiceInputAzureAdToken: optional("OPENGENI_VOICE_INPUT_AZURE_AD_TOKEN"),
4025
+ azureLiveEndpoint: optional("OPENGENI_AZURE_LIVE_ENDPOINT"),
4026
+ azureLiveApiKey: optional("OPENGENI_AZURE_LIVE_API_KEY"),
4027
+ azureLiveDeployment: optional("OPENGENI_AZURE_LIVE_DEPLOYMENT"),
4028
+ azureLiveVoice: optional("OPENGENI_AZURE_LIVE_VOICE"),
4029
+ azureLivePricingJson: optional("OPENGENI_AZURE_LIVE_PRICING_JSON"),
4030
+ aiGatewayRealtimePricingJson: optional("OPENGENI_AI_GATEWAY_REALTIME_PRICING_JSON"),
4031
+ voiceInputMaiEndpoint: optional("OPENGENI_VOICE_INPUT_MAI_ENDPOINT"),
4032
+ voiceInputMaiApiKey: optional("OPENGENI_VOICE_INPUT_MAI_API_KEY"),
4033
+ voiceInputMaiApiVersion: optional("OPENGENI_VOICE_INPUT_MAI_API_VERSION"),
4034
+ voiceInputMaiPricingJson: optional("OPENGENI_VOICE_INPUT_MAI_PRICING_JSON"),
4035
+ voiceInputMaiModel: optional("OPENGENI_VOICE_INPUT_MAI_MODEL"),
4036
+ voiceInputAzureModel: optional("OPENGENI_VOICE_INPUT_AZURE_MODEL"),
4037
+ voiceInputOpenaiPricingJson: optional("OPENGENI_VOICE_INPUT_OPENAI_PRICING_JSON"),
4038
+ voiceInputAzurePricingJson: optional("OPENGENI_VOICE_INPUT_AZURE_PRICING_JSON"),
3617
4039
  voiceInputCodexExperimentalEnabled: optional("OPENGENI_VOICE_INPUT_CODEX_EXPERIMENTAL"),
3618
4040
  modelPricingJson: optional("OPENGENI_MODEL_PRICING_JSON"),
3619
4041
  modelCatalogSource,
@@ -3644,6 +4066,15 @@ export function getSettings(source: NodeJS.ProcessEnv = process.env): Settings {
3644
4066
  openaiReasoningEncryptedContent: optional("OPENGENI_OPENAI_REASONING_ENCRYPTED_CONTENT"),
3645
4067
  openaiMaxRetries: optional("OPENGENI_OPENAI_MAX_RETRIES"),
3646
4068
  webSearchEnabled: optional("OPENGENI_WEB_SEARCH_ENABLED"),
4069
+ webSearchProvider: optional("OPENGENI_WEB_SEARCH_PROVIDER"),
4070
+ webSearchApiKey: optional("OPENGENI_WEB_SEARCH_API_KEY"),
4071
+ webSearchBaseUrl: optional("OPENGENI_WEB_SEARCH_BASE_URL"),
4072
+ webFetchProvider: optional("OPENGENI_WEB_FETCH_PROVIDER"),
4073
+ webFetchApiKey: optional("OPENGENI_WEB_FETCH_API_KEY"),
4074
+ webFetchBaseUrl: optional("OPENGENI_WEB_FETCH_BASE_URL"),
4075
+ webSearchProviderMode: optional("OPENGENI_WEB_SEARCH_PROVIDER_MODE"),
4076
+ webSearchPricingJson: optional("OPENGENI_WEB_SEARCH_PRICING_JSON"),
4077
+ webSearchRequestTimeoutMs: optional("OPENGENI_WEB_SEARCH_REQUEST_TIMEOUT_MS"),
3647
4078
  jevApiKey: optional("OPENGENI_JEV_API_KEY"),
3648
4079
  jevBaseUrl: optional("OPENGENI_JEV_BASE_URL"),
3649
4080
  jevModel: optional("OPENGENI_JEV_MODEL"),
@@ -3983,13 +4414,35 @@ export type FirstPartyMcpToolPolicy = {
3983
4414
  allowed: FirstPartyMcpToolNameType[];
3984
4415
  };
3985
4416
 
4417
+ export type FirstPartyMcpToolPolicySettings = Pick<
4418
+ Settings,
4419
+ "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools" | "artifactMaterializerDeployed"
4420
+ >;
4421
+
4422
+ const EDITABLE_ARTIFACT_EXPORT_TOOLS: readonly FirstPartyMcpToolNameType[] = [
4423
+ "editable_artifact_export",
4424
+ "editable_artifact_export_status",
4425
+ ];
4426
+
4427
+ /**
4428
+ * First-party tools whose backing workload this deployment does not run. They
4429
+ * are absent from the ceiling (never offered) and an explicit request for one
4430
+ * is dropped rather than rejected: it is a deployment fact, not a caller error.
4431
+ */
4432
+ export function deploymentUnavailableFirstPartyMcpTools(
4433
+ settings: Pick<Settings, "artifactMaterializerDeployed">,
4434
+ ): ReadonlySet<FirstPartyMcpToolNameType> {
4435
+ return new Set(settings.artifactMaterializerDeployed ? [] : EDITABLE_ARTIFACT_EXPORT_TOOLS);
4436
+ }
4437
+
3986
4438
  /** Resolve the deployment's session-tool defaults and hard execution ceiling. */
3987
4439
  export function resolveFirstPartyMcpToolPolicy(
3988
- settings: Pick<Settings, "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools">,
4440
+ settings: FirstPartyMcpToolPolicySettings,
3989
4441
  ): FirstPartyMcpToolPolicy {
4442
+ const unavailable = deploymentUnavailableFirstPartyMcpTools(settings);
3990
4443
  const allowed = currentAgentLearningToolSelection(
3991
4444
  settings.allowedFirstPartyMcpTools ?? [...FIRST_PARTY_MCP_TOOL_NAMES],
3992
- ).filter((tool) => !isRetiredNativeAtlassianTool(tool));
4445
+ ).filter((tool) => !isRetiredNativeAtlassianTool(tool) && !unavailable.has(tool));
3993
4446
  const allowedSet = new Set(allowed);
3994
4447
  const defaults = currentAgentLearningToolSelection(
3995
4448
  settings.defaultFirstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
@@ -4002,7 +4455,7 @@ export function resolveFirstPartyMcpToolPolicy(
4002
4455
 
4003
4456
  /** Apply the deployment ceiling to an existing durable session selection. */
4004
4457
  export function allowedFirstPartyMcpToolsForSession(
4005
- settings: Pick<Settings, "defaultFirstPartyMcpTools" | "allowedFirstPartyMcpTools">,
4458
+ settings: FirstPartyMcpToolPolicySettings,
4006
4459
  selected: readonly FirstPartyMcpToolNameType[] | null | undefined,
4007
4460
  ): FirstPartyMcpToolNameType[] {
4008
4461
  const policy = resolveFirstPartyMcpToolPolicy(settings);
@@ -5424,6 +5877,71 @@ function matchesAdditiveCapabilityDefinitionVersion(
5424
5877
  return false;
5425
5878
  }
5426
5879
 
5880
+ /** The one pre-enablement hosted web-search declaration an operator may upgrade from. */
5881
+ function webSearchPreEnablementState(): CapabilityStateV1 {
5882
+ return { upstream: "unknown", runnable: false };
5883
+ }
5884
+
5885
+ function matchesWebSearchEnablementDefinitionVersion(
5886
+ model: ConfiguredModel,
5887
+ provider: ResolvedModelProvider,
5888
+ policy: TurnExecutionPolicyV1,
5889
+ ): boolean {
5890
+ // An operator turning on hosted web search for an existing model changes
5891
+ // only capabilities.hostedTools.webSearch from the exact unknown/off
5892
+ // declaration to a runnable one. Reconstruct that single historical
5893
+ // declaration and require every other current field to reproduce the frozen
5894
+ // digest; never ignore the digest. Turning web search off, or changing any
5895
+ // other capability, still fails closed. Do not compose this with the
5896
+ // latency/input-modality subsets or the wire-profile, implicit-caching, or
5897
+ // Claude-accounting migration exceptions.
5898
+ if (!model.capabilities.hostedTools.webSearch.runnable) return false;
5899
+ const { definitionVersion: _definitionVersion, ...modelWithoutVersion } = model;
5900
+ const capabilities = {
5901
+ ...model.capabilities,
5902
+ hostedTools: { ...model.capabilities.hostedTools, webSearch: webSearchPreEnablementState() },
5903
+ };
5904
+ return (
5905
+ policy.definitionVersion ===
5906
+ definitionVersionFor({ ...modelWithoutVersion, capabilities }, provider)
5907
+ );
5908
+ }
5909
+
5910
+ /**
5911
+ * The executable model an accepted turn runs: the current definition, except
5912
+ * that a turn frozen before hosted web search was enabled keeps its frozen
5913
+ * tool set (no web_search) on every recovery attempt. Only the next accepted
5914
+ * logical turn resolves the newly enabled tool. Call only with a policy that
5915
+ * already passed assertTurnExecutionPolicyMatchesConfigV1.
5916
+ */
5917
+ export function configuredModelForAcceptedTurnExecutionPolicy(
5918
+ model: ConfiguredModel,
5919
+ provider: ResolvedModelProvider,
5920
+ policy: TurnExecutionPolicyV1,
5921
+ ): ConfiguredModel {
5922
+ if (
5923
+ policy.definitionVersion === model.definitionVersion ||
5924
+ // Runtime test doubles may resolve a partial shape; a model without a
5925
+ // runnable web-search declaration has nothing to withhold.
5926
+ model.capabilities?.hostedTools?.webSearch?.runnable !== true ||
5927
+ !matchesWebSearchEnablementDefinitionVersion(model, provider, policy)
5928
+ ) {
5929
+ return model;
5930
+ }
5931
+ return {
5932
+ ...model,
5933
+ capabilities: {
5934
+ ...model.capabilities,
5935
+ hostedTools: {
5936
+ ...model.capabilities.hostedTools,
5937
+ webSearch: webSearchPreEnablementState(),
5938
+ },
5939
+ },
5940
+ hostedWebSearch: false,
5941
+ definitionVersion: policy.definitionVersion,
5942
+ };
5943
+ }
5944
+
5427
5945
  /**
5428
5946
  * The built-in provider's stable id: "openai" on the OpenAI platform, "azure"
5429
5947
  * on Azure. Exported because the workspace model-policy gate must attribute
@@ -6243,7 +6761,8 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
6243
6761
  parsed.definitionVersion === legacyImplicitOpenAiDefinitionVersion ||
6244
6762
  parsed.definitionVersion ===
6245
6763
  legacyCodexAstraImplicitCachingDefinitionVersionFor(resolved.model, resolved.provider) ||
6246
- matchesAdditiveCapabilityDefinitionVersion(resolved.model, resolved.provider, parsed);
6764
+ matchesAdditiveCapabilityDefinitionVersion(resolved.model, resolved.provider, parsed) ||
6765
+ matchesWebSearchEnablementDefinitionVersion(resolved.model, resolved.provider, parsed);
6247
6766
  const identityMismatched =
6248
6767
  parsed.providerId !== resolved.provider.id ||
6249
6768
  parsed.upstreamModelId !== resolved.model.upstreamModelId ||
@@ -6258,7 +6777,11 @@ export function assertTurnExecutionPolicyMatchesConfigV1(
6258
6777
  if (!definitionVersionMatches && !legacyClaudeAccountingMatches) {
6259
6778
  throw new TurnExecutionPolicyDefinitionMismatchError();
6260
6779
  }
6261
- return { policy: parsed, provider: resolved.provider, model: resolved.model };
6780
+ return {
6781
+ policy: parsed,
6782
+ provider: resolved.provider,
6783
+ model: configuredModelForAcceptedTurnExecutionPolicy(resolved.model, resolved.provider, parsed),
6784
+ };
6262
6785
  }
6263
6786
 
6264
6787
  /**
@@ -7576,6 +8099,28 @@ export function parseIntegrationsOauthClientsJson(
7576
8099
  return out;
7577
8100
  }
7578
8101
 
8102
+ /**
8103
+ * The operator-registered client for one authorization server, matched by
8104
+ * exact issuer/URL key first and then trailing-slash-insensitively. The OAuth
8105
+ * start flow and the connector catalog's connectability projection share this
8106
+ * so a catalog row is offered exactly when a start would find a client.
8107
+ */
8108
+ export function findIntegrationsOauthClient(
8109
+ configured: Readonly<Record<string, IntegrationOAuthClientConfig>>,
8110
+ candidates: readonly string[],
8111
+ ): IntegrationOAuthClientConfig | null {
8112
+ const normalize = (value: string) => value.replace(/\/+$/, "");
8113
+ for (const key of new Set(candidates.flatMap((candidate) => [candidate, normalize(candidate)]))) {
8114
+ const entry = configured[key];
8115
+ if (entry) return entry;
8116
+ }
8117
+ const normalizedCandidates = new Set(candidates.map(normalize));
8118
+ for (const [key, entry] of Object.entries(configured)) {
8119
+ if (normalizedCandidates.has(normalize(key))) return entry;
8120
+ }
8121
+ return null;
8122
+ }
8123
+
7579
8124
  export const SocialOAuthClientConfigSchema = z.object({
7580
8125
  clientId: z.string().min(1),
7581
8126
  clientSecret: z.string().min(1).optional(),
@@ -8370,6 +8915,9 @@ function validateSettings(settings: Settings, source: NodeJS.ProcessEnv = proces
8370
8915
  );
8371
8916
  }
8372
8917
  parseExposedPorts(settings.dockerExposedPorts);
8918
+ // Malformed voice-input pricing deliberately does not fail boot: every
8919
+ // process shares this config, and a voice-only typo must not crash-loop
8920
+ // workers. The provider is withheld and the API logs voiceInputPricingIssues.
8373
8921
  sandboxEnvironmentVariableNames(settings);
8374
8922
  sandboxLifecycleHookIds(settings);
8375
8923
  // Fail fast on a malformed warm-rate table (P2.1).