@genesislcap/foundation-ai 14.438.1 → 14.439.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 (32) hide show
  1. package/dist/dts/ai-provider.d.ts +9 -3
  2. package/dist/dts/ai-provider.d.ts.map +1 -1
  3. package/dist/dts/index.d.ts +5 -2
  4. package/dist/dts/index.d.ts.map +1 -1
  5. package/dist/dts/providers/anthropic-provider.d.ts +26 -0
  6. package/dist/dts/providers/anthropic-provider.d.ts.map +1 -0
  7. package/dist/dts/transports/anthropic-transport.d.ts +93 -0
  8. package/dist/dts/transports/anthropic-transport.d.ts.map +1 -0
  9. package/dist/dts/transports/gemini-transport.d.ts +19 -2
  10. package/dist/dts/transports/gemini-transport.d.ts.map +1 -1
  11. package/dist/dts/transports/server-openai-transport.d.ts +1 -0
  12. package/dist/dts/transports/server-openai-transport.d.ts.map +1 -1
  13. package/dist/dts/types/chat.types.d.ts +25 -0
  14. package/dist/dts/types/chat.types.d.ts.map +1 -1
  15. package/dist/dts/types/config.types.d.ts +33 -2
  16. package/dist/dts/types/config.types.d.ts.map +1 -1
  17. package/dist/dts/types/status.types.d.ts +7 -0
  18. package/dist/dts/types/status.types.d.ts.map +1 -1
  19. package/dist/dts/types/transports.types.d.ts +22 -0
  20. package/dist/dts/types/transports.types.d.ts.map +1 -1
  21. package/dist/esm/ai-provider.js +24 -1
  22. package/dist/esm/index.js +3 -1
  23. package/dist/esm/providers/anthropic-provider.js +64 -0
  24. package/dist/esm/providers/gemini-provider.js +1 -1
  25. package/dist/esm/providers/openai-provider.js +2 -2
  26. package/dist/esm/transports/anthropic-transport.js +367 -0
  27. package/dist/esm/transports/gemini-transport.js +50 -3
  28. package/dist/esm/transports/server-openai-transport.js +16 -1
  29. package/dist/esm/types/config.types.js +6 -0
  30. package/dist/foundation-ai.api.json +1351 -7
  31. package/dist/foundation-ai.d.ts +230 -6
  32. package/package.json +11 -11
@@ -27,7 +27,7 @@ export declare const AI_FEATURE_FLAG = "ai";
27
27
  *
28
28
  * @beta
29
29
  */
30
- export declare type AIConfig = ServerAIConfig | ChromeAIConfig | GeminiAIConfig;
30
+ export declare type AIConfig = ServerAIConfig | ChromeAIConfig | GeminiAIConfig | AnthropicAIConfig;
31
31
 
32
32
  /**
33
33
  * AI Provider interface for foundation-ai.
@@ -103,7 +103,7 @@ declare interface AIProviderConfig {
103
103
  *
104
104
  * @beta
105
105
  */
106
- export declare type AIProviderType = 'openai' | 'chrome' | 'gemini' | 'none';
106
+ export declare type AIProviderType = 'openai' | 'chrome' | 'gemini' | 'anthropic' | 'none';
107
107
 
108
108
  /**
109
109
  * AI status for display in the header indicator.
@@ -114,6 +114,13 @@ export declare interface AIStatus {
114
114
  provider: AIProviderType;
115
115
  model: string;
116
116
  chromeAvailability?: ChromeAvailability;
117
+ /**
118
+ * Maximum input context window for the active model, in tokens. Used by the
119
+ * chat UI to render context utilisation. Optional — providers without a
120
+ * well-defined limit (e.g. Chrome on-device) leave it undefined and the UI
121
+ * hides the indicator.
122
+ */
123
+ contextLimit?: number;
117
124
  }
118
125
 
119
126
  /**
@@ -127,6 +134,149 @@ declare interface AITransport {
127
134
  isAvailable?(): Promise<boolean>;
128
135
  }
129
136
 
137
+ /**
138
+ * Anthropic server-proxy AI configuration (client calls your server; server calls Anthropic).
139
+ *
140
+ * @remarks
141
+ * API key stays on the server. URL defaults to `/gwf/ai-service/anthropic/chat`.
142
+ * When `apiKey` is provided, the transport calls `api.anthropic.com/v1/messages` directly
143
+ * (browser direct calls require `anthropic-dangerous-direct-browser-access: true`, which the
144
+ * transport sends automatically — only use this in trusted contexts).
145
+ *
146
+ * @beta
147
+ */
148
+ export declare interface AnthropicAIConfig extends AIProviderConfig {
149
+ providerType: 'anthropic';
150
+ /** Defaults to `claude-haiku-4-5-20251001`. Only {@link SUPPORTED_ANTHROPIC_MODEL_IDS} are accepted at runtime. */
151
+ model?: AnthropicModelId;
152
+ serverEndpoint?: string;
153
+ apiKey?: string;
154
+ /**
155
+ * Hard cap on tokens generated per response. Anthropic's `max_tokens` is a required
156
+ * field on every request. Defaults to 4096.
157
+ */
158
+ maxTokens?: number;
159
+ }
160
+
161
+ /**
162
+ * Anthropic Messages API model ids supported by {@link AnthropicTransport}.
163
+ *
164
+ * @beta
165
+ */
166
+ export declare type AnthropicModelId = 'claude-opus-4-7' | 'claude-sonnet-4-6' | 'claude-haiku-4-5-20251001';
167
+
168
+ /**
169
+ * Anthropic Claude AI provider. Uses {@link AnthropicTransport} to handle requests.
170
+ * Implements criteria interpretation and multi-turn chat.
171
+ *
172
+ * @beta
173
+ */
174
+ export declare class AnthropicProvider implements AIProvider {
175
+ private readonly transport;
176
+ private readonly criteriaInstructions?;
177
+ constructor(config: AnthropicProviderConfig, transport: AnthropicTransport);
178
+ getStatus(): Promise<AIStatus>;
179
+ interpretCriteria(input: string, context: CriteriaInterpretContext): Promise<CriteriaInterpretationResult | null>;
180
+ prompt(message: string, options?: ChatRequestOptions): Promise<string>;
181
+ chat(history: ChatMessage[], userMessage: string, options?: ChatRequestOptions): Promise<ChatMessage>;
182
+ streamChat(history: ChatMessage[], userMessage: string, options?: ChatRequestOptions): AsyncIterable<ChatStreamChunk>;
183
+ }
184
+
185
+ declare interface AnthropicProviderConfig {
186
+ criteriaInstructions?: string;
187
+ }
188
+
189
+ /**
190
+ * Transport for Anthropic Claude. Calls the Messages API directly when `apiKey`
191
+ * is provided, otherwise falls back to a server-proxy endpoint (if `serverEndpoint`
192
+ * is configured).
193
+ *
194
+ * Implements `AITransport` (structured prompt via tool-forcing) and `ChatTransport`
195
+ * (multi-turn chat).
196
+ *
197
+ * @beta
198
+ */
199
+ export declare class AnthropicTransport implements AITransport, ChatTransport, CostReportingTransport {
200
+ private readonly model;
201
+ private readonly timeout;
202
+ private readonly apiKey;
203
+ private readonly serverEndpoint;
204
+ private readonly maxTokens;
205
+ /**
206
+ * Estimated USD cost accumulated across every successful request on this
207
+ * transport instance. Convenience accessor for non-chat consumers
208
+ * (telemetry, debug overlays). The main chat UI sums per-message `cost`
209
+ * fields instead so its session total stays attributed to chat turns only.
210
+ */
211
+ private lifetimeCostUsd;
212
+ constructor(config?: AnthropicTransportConfig);
213
+ getConfig(): {
214
+ provider: 'anthropic';
215
+ model: AnthropicModelId;
216
+ contextLimit: number;
217
+ };
218
+ /** Estimated USD cost accumulated across every successful request on this transport instance. */
219
+ getLifetimeCost(): number;
220
+ /** Reset the lifetime cost counter. Intended for chat-clear / new-session flows. */
221
+ resetLifetimeCost(): void;
222
+ sendStructuredPrompt(options: StructuredPromptOptions): Promise<string>;
223
+ sendChatMessage(history: ChatMessage[], userMessage: string, options?: ChatRequestOptions): Promise<ChatMessage>;
224
+ private static readonly TOKENS_PER_MILLION;
225
+ private static readonly COST_DECIMAL_PLACES;
226
+ /**
227
+ * Logs the per-call cost breakdown, accumulates the lifetime running total,
228
+ * and returns the per-call total so the caller can attach it to the response
229
+ * message.
230
+ */
231
+ private logTokenUsage;
232
+ /**
233
+ * Convert the internal `ChatMessage[]` history into Anthropic's message format.
234
+ *
235
+ * Anthropic alternates strictly `user` / `assistant`. Tool calls are emitted by
236
+ * the assistant as `tool_use` blocks; the corresponding `tool_result` blocks
237
+ * must appear in the *next* user message and reference the tool by `tool_use_id`.
238
+ * Consecutive same-role turns are merged by the API but we merge here to keep
239
+ * the payload tidy.
240
+ */
241
+ private toAnthropicMessages;
242
+ private fromAnthropicResponse;
243
+ private buildEndpoint;
244
+ private static readonly MAX_RETRIES;
245
+ private static readonly RATE_LIMIT_STATUS;
246
+ private static readonly SERVICE_UNAVAILABLE_STATUS;
247
+ private static readonly OVERLOADED_STATUS;
248
+ private static readonly RETRYABLE_STATUSES;
249
+ private static readonly BACKOFF_BASE_MS;
250
+ private post;
251
+ }
252
+
253
+ declare interface AnthropicTransportConfig {
254
+ /**
255
+ * Defaults to `claude-haiku-4-5-20251001`. Only {@link SUPPORTED_ANTHROPIC_MODEL_IDS}
256
+ * are accepted.
257
+ */
258
+ model?: AnthropicModelId;
259
+ timeout?: number;
260
+ /**
261
+ * API key for direct calls to the Anthropic API.
262
+ * Must be provided explicitly; callers are responsible for reading it from
263
+ * their environment (e.g. a build-time injected global). When set, requests
264
+ * include `anthropic-dangerous-direct-browser-access: true` so the API
265
+ * accepts the call from a browser origin.
266
+ */
267
+ apiKey?: string;
268
+ /**
269
+ * Server-proxy endpoint (e.g. `/gwf/ai-service/anthropic/chat`).
270
+ * Only used when no API key is available. When omitted and no API key
271
+ * is found, requests will fail.
272
+ */
273
+ serverEndpoint?: string;
274
+ /**
275
+ * Hard cap on tokens generated per response. Defaults to 4096.
276
+ */
277
+ maxTokens?: number;
278
+ }
279
+
130
280
  /**
131
281
  * Agent engine configuration for the chat assistant.
132
282
  *
@@ -287,6 +437,18 @@ export declare interface ChatMessage {
287
437
  * context window utilisation in the UI.
288
438
  */
289
439
  inputTokens?: number;
440
+ /**
441
+ * Total output (generated) tokens for the request that produced this message.
442
+ * Set by transports that return usage metadata. Used alongside `cost`.
443
+ */
444
+ outputTokens?: number;
445
+ /**
446
+ * Estimated USD cost for the single request that produced this message,
447
+ * derived from the active model's input/output rates. Set by transports.
448
+ * Hosts can sum this across the message list to display a running session
449
+ * cost without re-deriving rates.
450
+ */
451
+ cost?: number;
290
452
  }
291
453
 
292
454
  /**
@@ -528,6 +690,19 @@ export declare interface ChatUiConfig {
528
690
  * - `false` — hidden
529
691
  */
530
692
  showContextUsage?: boolean;
693
+ /**
694
+ * Show the running session cost (aggregated USD across all chat turns) in
695
+ * the settings panel.
696
+ * - `true` or `undefined` (default) — shown when cost data is available
697
+ * - `false` — hidden
698
+ */
699
+ showSessionCost?: boolean;
700
+ /**
701
+ * Show the active model id in the settings panel.
702
+ * - `true` or `undefined` (default) — shown when the provider exposes a model
703
+ * - `false` — hidden
704
+ */
705
+ showActiveModel?: boolean;
531
706
  }
532
707
 
533
708
  /**
@@ -551,6 +726,29 @@ export declare interface ChromeAIConfig extends AIProviderConfig {
551
726
  */
552
727
  export declare type ChromeAvailability = 'available' | 'downloading' | 'downloadable' | 'unavailable';
553
728
 
729
+ /**
730
+ * Optional capability implemented by transports that can report the estimated
731
+ * USD cost they have incurred over their lifetime.
732
+ *
733
+ * @remarks
734
+ * Surfaces a transport-lifetime running total for non-chat consumers such as
735
+ * telemetry, debug overlays, or admin dashboards. The chat UI deliberately
736
+ * does not depend on this — it sums per-message `cost` fields instead so the
737
+ * session total stays scoped to chat turns and excludes other call sites
738
+ * (suggestions, criteria interpretation, etc.) that share the same transport.
739
+ *
740
+ * Consumers can either accept a `CostReportingTransport` directly or duck-type
741
+ * with `'getLifetimeCost' in transport`.
742
+ *
743
+ * @beta
744
+ */
745
+ export declare interface CostReportingTransport {
746
+ /** Estimated USD cost accumulated across every successful request on this transport instance. */
747
+ getLifetimeCost(): number;
748
+ /** Reset the lifetime cost counter. Intended for chat-clear / new-session flows. */
749
+ resetLifetimeCost(): void;
750
+ }
751
+
554
752
  /**
555
753
  * Creates an AIProvider from config.
556
754
  * Uses ChromeProvider when providerType is 'chrome'.
@@ -669,20 +867,37 @@ declare interface GeminiProviderConfig {
669
867
  *
670
868
  * @beta
671
869
  */
672
- export declare class GeminiTransport implements AITransport, ChatTransport {
870
+ export declare class GeminiTransport implements AITransport, ChatTransport, CostReportingTransport {
673
871
  private readonly model;
674
872
  private readonly timeout;
675
873
  private readonly apiKey;
676
874
  private readonly serverEndpoint;
875
+ /**
876
+ * Estimated USD cost accumulated across every successful request on this
877
+ * transport instance. Convenience accessor for non-chat consumers
878
+ * (telemetry, debug overlays). The main chat UI sums per-message `cost`
879
+ * fields instead so its session total stays attributed to chat turns only.
880
+ */
881
+ private lifetimeCostUsd;
677
882
  constructor(config?: GeminiTransportConfig);
678
883
  getConfig(): {
679
884
  provider: 'gemini';
680
885
  model: GeminiModelId;
886
+ contextLimit: number;
681
887
  };
888
+ /** Estimated USD cost accumulated across every successful request on this transport instance. */
889
+ getLifetimeCost(): number;
890
+ /** Reset the lifetime cost counter. Intended for chat-clear / new-session flows. */
891
+ resetLifetimeCost(): void;
682
892
  sendStructuredPrompt(options: StructuredPromptOptions): Promise<string>;
683
893
  sendChatMessage(history: ChatMessage[], userMessage: string, options?: ChatRequestOptions): Promise<ChatMessage>;
684
894
  private static readonly TOKENS_PER_MILLION;
685
895
  private static readonly COST_DECIMAL_PLACES;
896
+ /**
897
+ * Logs the per-call cost breakdown, accumulates the lifetime running total,
898
+ * and returns the per-call total so the caller can attach it to the response
899
+ * message.
900
+ */
686
901
  private logTokenUsage;
687
902
  private toGeminiContents;
688
903
  private fromGeminiResponse;
@@ -814,9 +1029,15 @@ export declare function resolveAIConfig(options?: ResolveAIConfigOptions): Promi
814
1029
  * @beta
815
1030
  */
816
1031
  export declare interface ResolveAIConfigOptions {
817
- /** Cloud provider (openai, gemini). Default: openai */
818
- provider?: 'openai' | 'gemini';
819
- /** Model name. OpenAI default: gpt-4o-mini. Gemini: only {@link SUPPORTED_GEMINI_MODEL_IDS} (default flash-lite). */
1032
+ /** Cloud provider (openai, gemini, anthropic). Default: openai */
1033
+ provider?: 'openai' | 'gemini' | 'anthropic';
1034
+ /**
1035
+ * Model name.
1036
+ *
1037
+ * - OpenAI default: `gpt-4o-mini`.
1038
+ * - Gemini: only {@link SUPPORTED_GEMINI_MODEL_IDS} (default `gemini-2.5-flash-lite`).
1039
+ * - Anthropic: only {@link SUPPORTED_ANTHROPIC_MODEL_IDS} (default `claude-haiku-4-5-20251001`).
1040
+ */
820
1041
  model?: string;
821
1042
  /** Prefer Chrome built-in AI when available */
822
1043
  preferChrome?: boolean;
@@ -884,6 +1105,9 @@ export declare interface SubAgentRequestOptions {
884
1105
  chatInputDuringExecution?: ChatInputDuringExecutionMode;
885
1106
  }
886
1107
 
1108
+ /** @beta */
1109
+ export declare const SUPPORTED_ANTHROPIC_MODEL_IDS: readonly AnthropicModelId[];
1110
+
887
1111
  /** @beta */
888
1112
  export declare const SUPPORTED_GEMINI_MODEL_IDS: readonly GeminiModelId[];
889
1113
 
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": "14.438.1",
4
+ "version": "14.439.0",
5
5
  "sideEffects": false,
6
6
  "license": "SEE LICENSE IN license.txt",
7
7
  "main": "dist/esm/index.js",
@@ -51,17 +51,17 @@
51
51
  }
52
52
  },
53
53
  "devDependencies": {
54
- "@genesislcap/foundation-testing": "14.438.1",
55
- "@genesislcap/genx": "14.438.1",
56
- "@genesislcap/rollup-builder": "14.438.1",
57
- "@genesislcap/ts-builder": "14.438.1",
58
- "@genesislcap/uvu-playwright-builder": "14.438.1",
59
- "@genesislcap/vite-builder": "14.438.1",
60
- "@genesislcap/webpack-builder": "14.438.1"
54
+ "@genesislcap/foundation-testing": "14.439.0",
55
+ "@genesislcap/genx": "14.439.0",
56
+ "@genesislcap/rollup-builder": "14.439.0",
57
+ "@genesislcap/ts-builder": "14.439.0",
58
+ "@genesislcap/uvu-playwright-builder": "14.439.0",
59
+ "@genesislcap/vite-builder": "14.439.0",
60
+ "@genesislcap/webpack-builder": "14.439.0"
61
61
  },
62
62
  "dependencies": {
63
- "@genesislcap/foundation-logger": "14.438.1",
64
- "@genesislcap/foundation-utils": "14.438.1",
63
+ "@genesislcap/foundation-logger": "14.439.0",
64
+ "@genesislcap/foundation-utils": "14.439.0",
65
65
  "@microsoft/fast-foundation": "2.50.0"
66
66
  },
67
67
  "repository": {
@@ -72,5 +72,5 @@
72
72
  "publishConfig": {
73
73
  "access": "public"
74
74
  },
75
- "gitHead": "65abdfa1a8d4d0ad873ed356dff62952dd2e2a92"
75
+ "gitHead": "25adab0e86be16092b258c8d7f429c4c6971d968"
76
76
  }