@genesislcap/foundation-ai 15.91.0 → 15.92.0

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.
Files changed (35) hide show
  1. package/README.md +26 -103
  2. package/dist/dts/ai-provider.d.ts +5 -3
  3. package/dist/dts/ai-provider.d.ts.map +1 -1
  4. package/dist/dts/index.d.ts +7 -4
  5. package/dist/dts/index.d.ts.map +1 -1
  6. package/dist/dts/providers/openai-provider.d.ts +8 -7
  7. package/dist/dts/providers/openai-provider.d.ts.map +1 -1
  8. package/dist/dts/transports/budget-exhausted-error.d.ts +5 -7
  9. package/dist/dts/transports/budget-exhausted-error.d.ts.map +1 -1
  10. package/dist/dts/transports/context-overflow-error.d.ts.map +1 -1
  11. package/dist/dts/transports/openai-transport.d.ts +119 -0
  12. package/dist/dts/transports/openai-transport.d.ts.map +1 -0
  13. package/dist/dts/types/chat.types.d.ts +12 -0
  14. package/dist/dts/types/chat.types.d.ts.map +1 -1
  15. package/dist/dts/types/config.types.d.ts +44 -16
  16. package/dist/dts/types/config.types.d.ts.map +1 -1
  17. package/dist/dts/utils/token-cost.d.ts +65 -1
  18. package/dist/dts/utils/token-cost.d.ts.map +1 -1
  19. package/dist/dts/utils/tool-schema.d.ts +38 -0
  20. package/dist/dts/utils/tool-schema.d.ts.map +1 -1
  21. package/dist/esm/ai-provider.js +21 -14
  22. package/dist/esm/index.js +4 -2
  23. package/dist/esm/providers/openai-provider.js +15 -9
  24. package/dist/esm/transports/budget-exhausted-error.js +6 -7
  25. package/dist/esm/transports/context-overflow-error.js +2 -0
  26. package/dist/esm/transports/openai-transport.js +752 -0
  27. package/dist/esm/types/config.types.js +12 -1
  28. package/dist/esm/utils/token-cost.js +89 -0
  29. package/dist/esm/utils/tool-schema.js +108 -0
  30. package/dist/foundation-ai.api.json +1745 -137
  31. package/dist/foundation-ai.d.ts +270 -28
  32. package/package.json +9 -9
  33. package/dist/dts/transports/server-openai-transport.d.ts +0 -23
  34. package/dist/dts/transports/server-openai-transport.d.ts.map +0 -1
  35. package/dist/esm/transports/server-openai-transport.js +0 -94
@@ -130,7 +130,7 @@ export declare const AI_TIER_VENDORS: readonly ["anthropic", "gemini"];
130
130
  *
131
131
  * @beta
132
132
  */
133
- export declare type AIConfig = ServerAIConfig | ChromeAIConfig | GeminiAIConfig | AnthropicAIConfig;
133
+ export declare type AIConfig = ChromeAIConfig | GeminiAIConfig | AnthropicAIConfig | OpenAIAIConfig;
134
134
 
135
135
  /**
136
136
  * AI Provider interface for foundation-ai.
@@ -800,13 +800,11 @@ export declare interface AnthropicUsageRecord {
800
800
  * and if it is reachable, `blocked` then derives true and locks a composer with
801
801
  * headroom left.
802
802
  *
803
- * Two vendors are excluded for that reason:
804
- * - `chrome` runs on-device — no pot to exhaust, and free.
805
- * - `openai` is **not budgeted by the proxy**. ai-service rejects it up front
806
- * with `400 UNSUPPORTED_PROVIDER` (`SUPPORTED_PROVIDERS` in `aiVendor.js`), no
807
- * `MODEL_PRICING` row names it, and `otherVendorAvailable` is computed over
808
- * Anthropic and Gemini alone. So it can never raise a 402, and a 402 is never
809
- * a statement about it.
803
+ * `chrome` is excluded for that reason: it runs on-device — no pot to exhaust, and free.
804
+ *
805
+ * `openai` is metered (GENC-1829): ai-service budgets it on its own `SpentUsdOpenai` counter and
806
+ * proxies it on `/api/openai/chat`. The proxy computes `otherVendorAvailable` over the vendors it
807
+ * OFFERS, so with OpenAI switched off there a 402 still says nothing about it.
810
808
  *
811
809
  * @beta
812
810
  */
@@ -1766,6 +1764,18 @@ export declare interface ChatMessage {
1766
1764
  * and `providerName`.
1767
1765
  */
1768
1766
  provider?: AIProviderType;
1767
+ /**
1768
+ * Opaque, provider-specific state attached to this MESSAGE that must round-trip back to the
1769
+ * originating provider on later requests — the message-level twin of
1770
+ * `ChatToolCall.providerMetadata`, same contract: keyed by provider id, a transport reads only its
1771
+ * own key, and the contents are not part of the stable public contract.
1772
+ *
1773
+ * Exists because some state belongs to a turn that made no tool call. OpenAI's Responses API marks
1774
+ * each assistant message with a `phase` (`commentary` / `final_answer`) and attaches encrypted
1775
+ * reasoning items to it; with no tool call to hang them on, a text-only turn would lose both, and a
1776
+ * preamble replayed without its `phase` can be read back as the final answer.
1777
+ */
1778
+ providerMetadata?: Record<string, unknown>;
1769
1779
  }
1770
1780
 
1771
1781
  /**
@@ -2898,7 +2908,7 @@ export declare interface CostReportingTransport {
2898
2908
  /**
2899
2909
  * Creates an AIProvider from config.
2900
2910
  * Uses ChromeProvider when providerType is 'chrome'.
2901
- * Uses OpenAIProvider with ServerOpenAITransport when provider is set (server-only).
2911
+ * Uses GeminiProvider, AnthropicProvider or OpenAIProvider for their `providerType`.
2902
2912
  * Otherwise returns DefaultAIProvider.
2903
2913
  * When the AI feature flag is disabled (?feature.ai), always returns DefaultAIProvider.
2904
2914
  *
@@ -3779,6 +3789,248 @@ export declare interface ObservableAIProviderRegistry extends AIProviderRegistry
3779
3789
  subscribe(listener: () => void): () => void;
3780
3790
  }
3781
3791
 
3792
+ /** @beta */
3793
+ export declare const OPENAI_CACHE_WRITE_12H_MULTIPLIER = 2.5;
3794
+
3795
+ /**
3796
+ * Cache-write multipliers on the base input rate, by TTL —
3797
+ * https://developers.openai.com/api/docs/guides/prompt-caching. These are the WHOLE rate for a
3798
+ * written token, not a premium added to it, which is the same shape as Anthropic's.
3799
+ *
3800
+ * @beta
3801
+ */
3802
+ export declare const OPENAI_CACHE_WRITE_30M_MULTIPLIER = 1.25;
3803
+
3804
+ /** @beta */
3805
+ export declare const OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER = 2;
3806
+
3807
+ /** @beta */
3808
+ export declare const OPENAI_LONG_CONTEXT_OUTPUT_MULTIPLIER = 1.5;
3809
+
3810
+ /**
3811
+ * Above this many input tokens the WHOLE request bills at the long-context tier — input and cached
3812
+ * input at {@link OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER}, output at
3813
+ * {@link OPENAI_LONG_CONTEXT_OUTPUT_MULTIPLIER}. Like Gemini's tier it is a property of the request,
3814
+ * not a marginal rate, and it is chosen on the whole prompt: uncached, cached and written tokens.
3815
+ *
3816
+ * @beta
3817
+ */
3818
+ export declare const OPENAI_LONG_CONTEXT_THRESHOLD = 272000;
3819
+
3820
+ /**
3821
+ * OpenAI server-proxy AI configuration (client calls your server; server calls OpenAI's Responses
3822
+ * API).
3823
+ *
3824
+ * @remarks
3825
+ * API key stays on the server: set `serverEndpoint` to the proxy route (Create's ai-service mounts
3826
+ * it at `/api/openai/chat`, e.g. `/gwf/ai-service/api/openai/chat`). There is no default, and with
3827
+ * neither `serverEndpoint` nor `apiKey` the transport throws. When `apiKey` is provided the
3828
+ * transport calls `api.openai.com/v1/responses` directly — only use this in trusted contexts.
3829
+ *
3830
+ * @beta
3831
+ */
3832
+ export declare interface OpenAIAIConfig extends AIProviderConfig {
3833
+ providerType: 'openai';
3834
+ /** Defaults to `gpt-6-luna`. Only {@link SUPPORTED_OPENAI_MODEL_IDS} are accepted at runtime. */
3835
+ model?: OpenAIModelId;
3836
+ serverEndpoint?: string;
3837
+ apiKey?: string;
3838
+ /** Per-response output ceiling (`max_output_tokens`). Omitted when unset. */
3839
+ maxTokens?: number;
3840
+ /**
3841
+ * Ask for reasoning summaries (`reasoning.summary`) so a thinking turn has something to show.
3842
+ * Off by default: OpenAI may require organisation verification before it returns them, and an
3843
+ * unverified organisation would otherwise see every reasoning turn rejected.
3844
+ */
3845
+ reasoningSummary?: 'auto' | 'concise' | 'detailed';
3846
+ }
3847
+
3848
+ /**
3849
+ * OpenAI Responses API model ids supported by {@link OpenAITransport}.
3850
+ *
3851
+ * @remarks
3852
+ * The GPT-6 family only: on gpt-6-astra, gpt-6.1-sol and gpt-6.1-terra, tool calling is available
3853
+ * on the Responses API alone, and the transport is built on it. Older ids are not listed because
3854
+ * nothing here prices or tests them — see {@link SUPPORTED_ANTHROPIC_MODEL_IDS} for why an unlisted
3855
+ * model must be a decision rather than a fall-through.
3856
+ *
3857
+ * @beta
3858
+ */
3859
+ export declare type OpenAIModelId = 'gpt-6-astra' | 'gpt-6.1-sol' | 'gpt-6.1-terra' | 'gpt-6-sol' | 'gpt-6-luna';
3860
+
3861
+ /**
3862
+ * General OpenAI provider, on `OpenAITransport` (the Responses API, direct or via a server proxy):
3863
+ * criteria, prompt and chat — the same shape as its Gemini and Anthropic twins.
3864
+ *
3865
+ * @beta
3866
+ */
3867
+ export declare class OpenAIProvider implements AIProvider {
3868
+ private readonly transport;
3869
+ private readonly criteriaInstructions?;
3870
+ constructor(config: OpenAIProviderConfig, transport: OpenAITransport);
3871
+ getStatus(): Promise<AIStatus>;
3872
+ interpretCriteria(input: string, context: CriteriaInterpretContext): Promise<CriteriaInterpretationResult | null>;
3873
+ prompt(message: string, options?: ChatRequestOptions): Promise<string>;
3874
+ chat(history: ChatMessage[], userMessage: string, options?: ChatRequestOptions): Promise<ChatMessage>;
3875
+ }
3876
+
3877
+ declare interface OpenAIProviderConfig {
3878
+ criteriaInstructions?: string;
3879
+ }
3880
+
3881
+ /**
3882
+ * The rates a request on `model` with `promptTokens` of input is billed at. `promptTokens` is the
3883
+ * whole prompt; pass 0 when only the standard tier is wanted.
3884
+ *
3885
+ * @beta
3886
+ */
3887
+ export declare function openaiRatesFor(model: OpenAIModelId, promptTokens: number): TokenRates & {
3888
+ cachedPerMillion: number;
3889
+ };
3890
+
3891
+ /**
3892
+ * Cost one OpenAI request.
3893
+ *
3894
+ * @beta
3895
+ */
3896
+ export declare function openaiTokenCost(model: OpenAIModelId, usage: OpenAIUsageRecord): TokenCost;
3897
+
3898
+ /**
3899
+ * Transport for OpenAI's Responses API. Calls it directly when `apiKey` is provided, otherwise
3900
+ * posts to a server-proxy endpoint (ai-service `/api/openai/chat`) that adds the key, meters spend
3901
+ * and frames the stream.
3902
+ *
3903
+ * Stateless by design: `store: false`, no `previous_response_id`. Everything a later turn needs
3904
+ * (encrypted reasoning, `phase`) rides on the persisted `ChatMessage`s, so sessions survive restore
3905
+ * and vendor switches the same way they do on the other transports.
3906
+ *
3907
+ * Implements `AITransport` (structured prompt) and `ChatTransport` (multi-turn chat).
3908
+ *
3909
+ * @beta
3910
+ */
3911
+ export declare class OpenAITransport implements AITransport, ChatTransport, CostReportingTransport {
3912
+ private readonly model;
3913
+ private readonly timeout;
3914
+ private readonly stallTimeout;
3915
+ private readonly apiKey;
3916
+ private readonly serverEndpoint;
3917
+ private readonly maxTokens;
3918
+ private readonly reasoningSummary;
3919
+ private lifetimeCostUsd;
3920
+ /** Net of write premiums, so it can go negative on a cold cache — same contract as Anthropic. */
3921
+ private lifetimeSavingsUsd;
3922
+ /**
3923
+ * The clamped policies already warned about, so each warns once rather than on every turn of a
3924
+ * tool loop. Per policy: a `minimal` warning must not silence the costlier `off` one.
3925
+ */
3926
+ private readonly warnedThinkingClamped;
3927
+ constructor(config?: OpenAITransportConfig);
3928
+ getConfig(): {
3929
+ provider: 'openai';
3930
+ model: OpenAIModelId;
3931
+ contextLimit: number;
3932
+ };
3933
+ getLifetimeCost(): number;
3934
+ getLifetimeSavings(): number;
3935
+ resetLifetimeCost(): void;
3936
+ sendStructuredPrompt(options: StructuredPromptOptions): Promise<string>;
3937
+ sendChatMessage(history: ChatMessage[], userMessage: string, options?: ChatRequestOptions): Promise<ChatMessage>;
3938
+ /** Undo strict mode's absent-becomes-null, in tool arguments and in a `responseSchema` answer. */
3939
+ private restoreOptionalFields;
3940
+ /**
3941
+ * The Responses request body for a turn. Public for tests and for hosts that want to inspect the
3942
+ * exact wire shape; not part of the stable contract.
3943
+ *
3944
+ * @internal
3945
+ */
3946
+ buildRequestBody(history: ChatMessage[], userMessage: string, options?: ChatRequestOptions): Record<string, unknown>;
3947
+ private warnIfThinkingClamped;
3948
+ private toInputItems;
3949
+ /**
3950
+ * A tool result, always as content parts — never the plain-string form. A cache hit needs the
3951
+ * whole rendered prefix to match, and a breakpoint can only sit on a part, so a string output
3952
+ * that became an array when it carried one would render differently on the next turn.
3953
+ */
3954
+ private toFunctionCallOutput;
3955
+ /**
3956
+ * Put a history-scope breakpoint on every stored item that can carry one — a user message's last
3957
+ * text part or a tool result's — up to the latest `MAX_HISTORY_BREAKPOINTS`.
3958
+ *
3959
+ * One breakpoint on the last item (Anthropic's shape) never hits on OpenAI: lookup checks only the
3960
+ * breakpoints in this request, and the last item moves every turn, so the previous turn's boundary
3961
+ * was unmarked and every turn rewrote the whole history to cache. Marking each boundary keeps the
3962
+ * previous turn's in the request. Assistant items cannot carry one.
3963
+ */
3964
+ private markHistoryBreakpoints;
3965
+ private static readonly COST_DECIMAL_PLACES;
3966
+ private usageFromResponse;
3967
+ private fromResponse;
3968
+ private buildEndpoint;
3969
+ /**
3970
+ * 429 is retried only once the proxy has ruled out an account wall — `insufficient_quota` and the
3971
+ * spend-limit codes arrive as `PROVIDER_REFUSED`, which `postWithRetry` never retries. 500/502/503
3972
+ * are OpenAI's documented transient faults (`server_error`, `server_is_overloaded`).
3973
+ */
3974
+ private static readonly RATE_LIMIT_STATUS;
3975
+ private static readonly SERVER_ERROR_STATUS;
3976
+ private static readonly BAD_GATEWAY_STATUS;
3977
+ private static readonly SERVICE_UNAVAILABLE_STATUS;
3978
+ private static readonly RETRYABLE_STATUSES;
3979
+ private static readonly BACKOFF_BASE_MS;
3980
+ private post;
3981
+ }
3982
+
3983
+ /** @beta */
3984
+ export declare interface OpenAITransportConfig {
3985
+ /** Defaults to `gpt-6-luna`. Only {@link SUPPORTED_OPENAI_MODEL_IDS} are accepted. */
3986
+ model?: OpenAIModelId;
3987
+ timeout?: number;
3988
+ /**
3989
+ * Inactivity window (ms) on the server-proxied heartbeat path. Ignored on direct API calls.
3990
+ * Defaults to 30_000.
3991
+ */
3992
+ stallTimeout?: number;
3993
+ /** API key for direct calls to the OpenAI API. Must be provided explicitly. */
3994
+ apiKey?: string;
3995
+ /** Server-proxy endpoint (e.g. `/gwf/ai-service/api/openai/chat`). Used when no API key is set. */
3996
+ serverEndpoint?: string;
3997
+ /** Per-request `max_output_tokens` default. Omitted (model ceiling) when unset. */
3998
+ maxTokens?: number;
3999
+ /**
4000
+ * `reasoning.summary` to request when the turn reasons. Defaults to `'none'` (no summary
4001
+ * requested): OpenAI may require organisation verification before it returns summaries, and an
4002
+ * unverified organisation would otherwise see every reasoning turn rejected.
4003
+ */
4004
+ reasoningSummary?: 'auto' | 'concise' | 'detailed' | 'none';
4005
+ }
4006
+
4007
+ /**
4008
+ * Token counts from an OpenAI Responses `usage` block.
4009
+ *
4010
+ * @remarks
4011
+ * Gemini's input semantics, not Anthropic's: `input_tokens` is the **whole** prompt and both cache
4012
+ * buckets sit inside it, so the uncached remainder is a subtraction. Reasoning tokens need no field
4013
+ * of their own — `output_tokens` already includes `output_tokens_details.reasoning_tokens`.
4014
+ *
4015
+ * @beta
4016
+ */
4017
+ export declare interface OpenAIUsageRecord {
4018
+ /** `usage.input_tokens` — the whole prompt, cached reads AND writes included. */
4019
+ inputTokens: number;
4020
+ /** `usage.output_tokens`. Includes reasoning tokens, which bill as output. */
4021
+ outputTokens: number;
4022
+ /** `usage.input_tokens_details.cached_tokens` — cache reads. */
4023
+ cacheReadTokens: number;
4024
+ /** `usage.input_tokens_details.cache_write_tokens` — the total across both TTLs. */
4025
+ cacheWriteTokens: number;
4026
+ /**
4027
+ * `usage.input_tokens_details.cache_write_tokens_details.ttl_12h_tokens` — the 12-hour
4028
+ * portion of {@link OpenAIUsageRecord.cacheWriteTokens}; the remainder is costed at the 30-minute
4029
+ * rate.
4030
+ */
4031
+ cacheWrite12hTokens: number;
4032
+ }
4033
+
3782
4034
  /**
3783
4035
  * The proxy's machine-readable code for "the upstream vendor refused this account".
3784
4036
  *
@@ -3927,7 +4179,9 @@ export declare interface RegisterAIProvidersOptions {
3927
4179
 
3928
4180
  /**
3929
4181
  * Resolves AI config, optionally preferring Chrome when available.
3930
- * Use before registering AIProvider. Server URL is derived from API_HOST.
4182
+ * Use before registering AIProvider. The cloud configs it returns carry no endpoint: add
4183
+ * `serverEndpoint` (a server proxy) or `apiKey` (direct, trusted contexts only) before
4184
+ * `createAIProvider`.
3931
4185
  * Returns null when the AI feature flag is disabled (?feature.ai).
3932
4186
  *
3933
4187
  * @beta
@@ -3945,7 +4199,7 @@ export declare interface ResolveAIConfigOptions {
3945
4199
  /**
3946
4200
  * Model name.
3947
4201
  *
3948
- * - OpenAI default: `gpt-4o-mini`.
4202
+ * - OpenAI: only {@link SUPPORTED_OPENAI_MODEL_IDS} (default `gpt-6-luna`).
3949
4203
  * - Gemini: only {@link SUPPORTED_GEMINI_MODEL_IDS} (default `gemini-2.5-flash-lite`).
3950
4204
  * - Anthropic: only {@link SUPPORTED_ANTHROPIC_MODEL_IDS} (default `claude-haiku-4-5-20251001`).
3951
4205
  */
@@ -4031,22 +4285,6 @@ export declare class ResponseTruncatedError extends Error {
4031
4285
  toolNames: string[]);
4032
4286
  }
4033
4287
 
4034
- /**
4035
- * Server-proxy AI configuration (client calls your server; server calls the provider).
4036
- *
4037
- * @remarks
4038
- * API key stays on the server. URL is derived from API_HOST at /ai-service/chat-completions.
4039
- * Use provider to select backend (openai, anthropic, etc.).
4040
- *
4041
- * @beta
4042
- */
4043
- export declare interface ServerAIConfig extends AIProviderConfig {
4044
- provider: ServerAIProviderType;
4045
- }
4046
-
4047
- /** Server-proxy providers only (API keys stay on server). */
4048
- declare type ServerAIProviderType = Extract<AIProviderType, 'openai'>;
4049
-
4050
4288
  declare type SpeechRecognitionErrorHandler = (error: string) => void;
4051
4289
 
4052
4290
  declare type SpeechRecognitionResultHandler = (transcript: string, isFinal: boolean) => void;
@@ -4175,6 +4413,9 @@ export declare const SUPPORTED_ANTHROPIC_MODEL_IDS: readonly AnthropicModelId[];
4175
4413
  /** @beta */
4176
4414
  export declare const SUPPORTED_GEMINI_MODEL_IDS: readonly GeminiModelId[];
4177
4415
 
4416
+ /** @beta */
4417
+ export declare const SUPPORTED_OPENAI_MODEL_IDS: readonly OpenAIModelId[];
4418
+
4178
4419
  /**
4179
4420
  * When a message's timestamp is visible in the transcript.
4180
4421
  *
@@ -4343,7 +4584,8 @@ export declare const VENDOR_LABELS: Readonly<Record<Exclude<AIProviderType, 'non
4343
4584
  * Which vendor a model id belongs to, or `undefined` when no supported provider claims it.
4344
4585
  *
4345
4586
  * @remarks
4346
- * Derived from {@link SUPPORTED_ANTHROPIC_MODEL_IDS} and {@link SUPPORTED_GEMINI_MODEL_IDS},
4587
+ * Derived from {@link SUPPORTED_ANTHROPIC_MODEL_IDS}, {@link SUPPORTED_GEMINI_MODEL_IDS} and
4588
+ * {@link SUPPORTED_OPENAI_MODEL_IDS},
4347
4589
  * so it cannot drift from them the way a hand-maintained map does — adding a model to an
4348
4590
  * allowlist is enough to teach this too.
4349
4591
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@genesislcap/foundation-ai",
3
3
  "description": "Genesis Foundation AI - Provider-agnostic AI configuration and shared utilities",
4
- "version": "15.91.0",
4
+ "version": "15.92.0",
5
5
  "sideEffects": false,
6
6
  "license": "SEE LICENSE IN license.txt",
7
7
  "main": "dist/esm/index.js",
@@ -49,16 +49,16 @@
49
49
  }
50
50
  },
51
51
  "devDependencies": {
52
- "@genesislcap/foundation-testing": "15.91.0",
53
- "@genesislcap/genx": "15.91.0",
54
- "@genesislcap/ts-builder": "15.91.0",
55
- "@genesislcap/uvu-playwright-builder": "15.91.0",
56
- "@genesislcap/vite-builder": "15.91.0",
57
- "@genesislcap/webpack-builder": "15.91.0"
52
+ "@genesislcap/foundation-testing": "15.92.0",
53
+ "@genesislcap/genx": "15.92.0",
54
+ "@genesislcap/ts-builder": "15.92.0",
55
+ "@genesislcap/uvu-playwright-builder": "15.92.0",
56
+ "@genesislcap/vite-builder": "15.92.0",
57
+ "@genesislcap/webpack-builder": "15.92.0"
58
58
  },
59
59
  "dependencies": {
60
- "@genesislcap/foundation-logger": "15.91.0",
61
- "@genesislcap/foundation-utils": "15.91.0",
60
+ "@genesislcap/foundation-logger": "15.92.0",
61
+ "@genesislcap/foundation-utils": "15.92.0",
62
62
  "@microsoft/fast-foundation": "2.50.0"
63
63
  },
64
64
  "repository": {
@@ -1,23 +0,0 @@
1
- import type { AITransport, StructuredPromptOptions } from '../types';
2
- export interface ServerOpenAITransportConfig {
3
- model?: string;
4
- timeout?: number;
5
- }
6
- /**
7
- * Server-proxy transport for OpenAI. Calls the standard server endpoint.
8
- * API key stays on the server; client never sees it.
9
- *
10
- * @beta
11
- */
12
- export declare class ServerOpenAITransport implements AITransport {
13
- private readonly model;
14
- private readonly timeout;
15
- constructor(config?: ServerOpenAITransportConfig);
16
- getConfig(): {
17
- provider: 'openai';
18
- model: string;
19
- contextLimit?: number;
20
- };
21
- sendStructuredPrompt(options: StructuredPromptOptions): Promise<string>;
22
- }
23
- //# sourceMappingURL=server-openai-transport.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server-openai-transport.d.ts","sourceRoot":"","sources":["../../../src/transports/server-openai-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AA0BrE,MAAM,WAAW,2BAA2B;IAC1C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;GAKG;AACH,qBAAa,qBAAsB,YAAW,WAAW;IACvD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAErB,MAAM,GAAE,2BAAgC;IAKpD,SAAS,IAAI;QAAE,QAAQ,EAAE,QAAQ,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE;IAQnE,oBAAoB,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC;CAiE9E"}
@@ -1,94 +0,0 @@
1
- import { __awaiter } from "tslib";
2
- import { budgetExhaustedFrom, codeOf, isBudgetRejection, parseJsonOrUndefined, VENDOR_LABELS, } from './budget-exhausted-error';
3
- import { providerRefusalOf, providerRefusedFrom } from './provider-refused';
4
- const AI_SERVER_PATH = '/gwf/ai-service/chat-completions';
5
- const DEFAULT_MODEL = 'gpt-4o-mini';
6
- const DEFAULT_TIMEOUT = 30000;
7
- /**
8
- * Maximum input context window (tokens) per known OpenAI model.
9
- * OpenAI's model identifier is a free-form string, so this is a best-effort
10
- * lookup — unknown models return undefined and the context indicator hides.
11
- * Source: https://platform.openai.com/docs/models
12
- */
13
- const OPENAI_CONTEXT_LIMITS = {
14
- 'gpt-4o': 128000,
15
- 'gpt-4o-mini': 128000,
16
- 'gpt-4-turbo': 128000,
17
- };
18
- /**
19
- * Server-proxy transport for OpenAI. Calls the standard server endpoint.
20
- * API key stays on the server; client never sees it.
21
- *
22
- * @beta
23
- */
24
- export class ServerOpenAITransport {
25
- constructor(config = {}) {
26
- var _a, _b;
27
- this.model = (_a = config.model) !== null && _a !== void 0 ? _a : DEFAULT_MODEL;
28
- this.timeout = (_b = config.timeout) !== null && _b !== void 0 ? _b : DEFAULT_TIMEOUT;
29
- }
30
- getConfig() {
31
- return {
32
- provider: 'openai',
33
- model: this.model,
34
- contextLimit: OPENAI_CONTEXT_LIMITS[this.model],
35
- };
36
- }
37
- sendStructuredPrompt(options) {
38
- return __awaiter(this, void 0, void 0, function* () {
39
- var _a, _b;
40
- const { systemPrompt, userPrompt, responseSchema } = options;
41
- const controller = new AbortController();
42
- const timeoutId = setTimeout(() => controller.abort(), this.timeout);
43
- const response = yield fetch(AI_SERVER_PATH, {
44
- method: 'POST',
45
- headers: {
46
- 'Content-Type': 'application/json',
47
- },
48
- body: JSON.stringify({
49
- provider: 'openai',
50
- model: this.model,
51
- systemPrompt,
52
- userPrompt,
53
- responseSchema,
54
- }),
55
- signal: controller.signal,
56
- credentials: 'include',
57
- });
58
- clearTimeout(timeoutId);
59
- if (!response.ok) {
60
- const err = yield response.text();
61
- const parsed = parseJsonOrUndefined(err);
62
- // This transport does its own bare `fetch` rather than going through
63
- // `postWithRetry` (it has no retry ladder, no stall timer and no framed
64
- // mode), so it would otherwise be the one proxy call where a 402 stays an
65
- // opaque `AI proxy error 402`. Classify it through the same shared
66
- // predicate the envelope uses, so the typed error is uniform across
67
- // transports even though the envelope is not.
68
- //
69
- // Scope, stated plainly: this transport serves only `sendStructuredPrompt`
70
- // — it has no `chat()` — and its single caller
71
- // (`OpenAIProvider.interpretCriteria`) catches everything and returns
72
- // null. So a 402 here degrades smart-search silently and can never latch
73
- // the assistant's `blocked` state, before or after this change. The typed
74
- // throw improves the log line and any host that catches it directly. See
75
- // `docs/migration-GENC-1464.md` §"Transport coverage".
76
- // The provider wall (GENC-1506), classified before the budget wall for the same
77
- // ordering reason `postWithRetry` has: a relayed upstream `402` must not be
78
- // reported as the caller's own spend cap. Same honest caveat as the budget
79
- // classification below — this transport's only caller catches everything and
80
- // returns null, so the typed throw improves the log line and nothing else.
81
- const refusal = providerRefusalOf('openai', codeOf(parsed), parsed);
82
- if (refusal) {
83
- throw providerRefusedFrom(VENDOR_LABELS.openai, refusal, response.status, parsed, err || undefined);
84
- }
85
- if (isBudgetRejection(response.status, codeOf(parsed))) {
86
- throw budgetExhaustedFrom(VENDOR_LABELS.openai, parsed, err || undefined);
87
- }
88
- throw new Error(`AI proxy error ${response.status}: ${err}`);
89
- }
90
- const data = (yield response.json());
91
- return (_b = (_a = data === null || data === void 0 ? void 0 : data.content) !== null && _a !== void 0 ? _a : data === null || data === void 0 ? void 0 : data.CONTENT) !== null && _b !== void 0 ? _b : '';
92
- });
93
- }
94
- }