chat-js 1.0.0-alpha.8 → 1.0.0-beta.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.
- package/README.md +7 -5
- package/dist/index.js +7161 -6308
- package/package.json +2 -2
- package/templates/chat-app/agent/agent.ts +0 -2
- package/templates/chat-app/app/editor.css +0 -6
- package/templates/chat-app/app/layout.tsx +11 -18
- package/templates/chat-app/app/opengraph-image.png +0 -0
- package/templates/chat-app/chat.config.ts +0 -44
- package/templates/chat-app/chatjs/core/gateway/runtime.ts +0 -7
- package/templates/chat-app/chatjs/core/ui/slots.tsx +15 -5
- package/templates/chat-app/chatjs/generated/installed-chat-ui.ts +4 -0
- package/templates/chat-app/chatjs/generated/installed.ts +2 -0
- package/templates/chat-app/chatjs/plugins/guest-chat/signed-out.tsx +7 -4
- package/templates/chat-app/chatjs/plugins/mcp/routes/connectors-page.tsx +4 -1
- package/templates/chat-app/chatjs/plugins/mcp/ui/connectors-settings.tsx +1 -1
- package/templates/chat-app/chatjs/plugins/mcp/ui/mcp-connect-dialog.tsx +1 -1
- package/templates/chat-app/chatjs/plugins/mcp/ui/mcp-create-dialog.tsx +1 -1
- package/templates/chat-app/chatjs/plugins/model-preferences/model-row.tsx +3 -0
- package/templates/chat-app/chatjs/plugins/model-preferences/models-settings.tsx +1 -0
- package/templates/chat-app/chatjs/plugins/projects/routes/project-page.tsx +10 -1
- package/templates/chat-app/chatjs/plugins/projects/ui/delete-project-dialog.tsx +0 -3
- package/templates/chat-app/chatjs/plugins/projects/ui/project-icon-picker.tsx +1 -0
- package/templates/chat-app/chatjs/plugins/streamdown-math/chat-ui.ts +11 -0
- package/templates/chat-app/chatjs/plugins/streamdown-math/plugin.json +6 -0
- package/templates/chat-app/chatjs/plugins/streamdown-mermaid/chat-ui.ts +10 -0
- package/templates/chat-app/chatjs/plugins/streamdown-mermaid/plugin.json +6 -0
- package/templates/chat-app/chatjs/providers/gateway.ts +49 -55
- package/templates/chat-app/chatjs/tools/generate-image/tool.test.ts +3 -14
- package/templates/chat-app/components/ai-elements/response.tsx +16 -15
- package/templates/chat-app/components/auth-form-view.tsx +35 -43
- package/templates/chat-app/components/chat/chat-composer.tsx +55 -20
- package/templates/chat-app/components/chat/chat-sidebar.tsx +5 -3
- package/templates/chat-app/components/composer/prompt-form.tsx +7 -1
- package/templates/chat-app/components/eve/eve-chat-error.tsx +12 -2
- package/templates/chat-app/components/eve/eve-conversation.tsx +223 -154
- package/templates/chat-app/components/eve/eve-creation-recovery.tsx +32 -1
- package/templates/chat-app/components/eve/eve-history-list.tsx +1 -1
- package/templates/chat-app/components/model-selection-indicator.tsx +12 -10
- package/templates/chat-app/components/model-selector-view.tsx +211 -186
- package/templates/chat-app/components/new-chat-button.tsx +1 -1
- package/templates/chat-app/components/settings/settings-nav.tsx +1 -1
- package/templates/chat-app/components/settings/settings-navigation-link.tsx +1 -0
- package/templates/chat-app/components/sidebar-top-row.tsx +1 -1
- package/templates/chat-app/components/ui/sidebar.tsx +1 -1
- package/templates/chat-app/components/ui/trace.tsx +1 -1
- package/templates/chat-app/hooks/use-auto-focus.ts +12 -13
- package/templates/chat-app/lib/ai/active-gateway.ts +0 -9
- package/templates/chat-app/lib/ai/app-models.test.ts +0 -4
- package/templates/chat-app/lib/ai/app-models.ts +11 -6
- package/templates/chat-app/lib/ai/gateways/registry.ts +4 -31
- package/templates/chat-app/lib/ai/model-catalog.ts +50 -0
- package/templates/chat-app/lib/ai/models.ts +4 -39
- package/templates/chat-app/lib/ai/providers.ts +2 -16
- package/templates/chat-app/lib/config-schema.ts +0 -96
- package/templates/chat-app/lib/devtools-disabled.ts +1 -0
- package/templates/chat-app/lib/eve/model-selection.test.ts +47 -50
- package/templates/chat-app/lib/eve/model-selection.ts +5 -37
- package/templates/chat-app/lib/eve/tool-cost.test.ts +18 -6
- package/templates/chat-app/lib/eve/tool-cost.ts +10 -18
- package/templates/chat-app/oxfmt.config.ts +0 -2
- package/templates/chat-app/package.json +7 -9
- package/templates/chat-app/proxy.ts +2 -4
- package/templates/chat-app/scripts/build-eve.test.ts +56 -0
- package/templates/chat-app/scripts/build-eve.ts +12 -5
- package/templates/chat-app/scripts/check-env.ts +2 -17
- package/templates/chat-app/scripts/environment-validation-report.ts +1 -20
- package/templates/chat-app/trpc/react.tsx +2 -2
- package/templates/electron/forge.config.ts +1 -19
- package/templates/electron/scripts/write-branding.ts +1 -5
- package/templates/chat-app/app/privacy/page.tsx +0 -153
- package/templates/chat-app/app/terms/page.tsx +0 -270
- package/templates/chat-app/components/ai-elements/context-hover.radix.tsx +0 -15
- package/templates/chat-app/components/ai-elements/context-hover.tsx +0 -17
- package/templates/chat-app/components/ai-elements/context.tsx +0 -405
- package/templates/chat-app/components/composer/editor.css +0 -96
- package/templates/chat-app/components/lexical-chat-input.tsx +0 -276
- package/templates/chat-app/components/model-selection-indicator.radix.tsx +0 -17
- package/templates/chat-app/components/ui/resizable.tsx +0 -49
- package/templates/chat-app/lib/ai/gateways/fallback-models.ts +0 -29
- package/templates/chat-app/lib/ai/models.generated.ts +0 -4242
- package/templates/chat-app/lib/ai/usage-token-details.test.ts +0 -76
- package/templates/chat-app/lib/ai/usage-token-details.ts +0 -33
- package/templates/chat-app/scripts/fetch-models.ts +0 -30
- package/templates/chat-app/scripts/gateway-snapshot-warning.test.ts +0 -9
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import { unstable_cache as cache } from "next/cache";
|
|
2
1
|
import { fetchModels } from "./models";
|
|
2
|
+
import type { AppModelDefinition } from "./web-chat-models";
|
|
3
3
|
import { buildWebChatModels } from "./web-chat-models";
|
|
4
4
|
|
|
5
|
-
const fetchChatModels =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
const fetchChatModels = async (): Promise<AppModelDefinition[]> => {
|
|
6
|
+
let models;
|
|
7
|
+
try {
|
|
8
|
+
models = await fetchModels();
|
|
9
|
+
} catch {
|
|
10
|
+
// Discovery logs the failure. Keep the workspace accessible and retry on reload.
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
return buildWebChatModels(models);
|
|
14
|
+
};
|
|
10
15
|
|
|
11
16
|
export { fetchChatModels };
|
|
12
17
|
export type { AppModelDefinition } from "./web-chat-models";
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { generatedForGateway, models } from "@/lib/ai/models.generated";
|
|
2
1
|
import type { createGateway } from "@/integrations/providers/gateway";
|
|
3
2
|
import type { GatewayProvider as GatewayProviderBase } from "@/chatjs/core/gateway/gateway-provider";
|
|
4
3
|
import type {
|
|
@@ -27,36 +26,10 @@ type ConfigLanguageModelId = [StrictLiterals<RuntimeLanguageModelId>] extends [
|
|
|
27
26
|
: StrictLiterals<RuntimeLanguageModelId>;
|
|
28
27
|
type GatewayModelIdMap = Record<GatewayType, ConfigLanguageModelId>;
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
> = Tags extends readonly [infer FirstTag, ...infer RemainingTags]
|
|
35
|
-
? FirstTag extends Tag
|
|
36
|
-
? true
|
|
37
|
-
: TupleIncludes<RemainingTags, Tag>
|
|
38
|
-
: false;
|
|
39
|
-
|
|
40
|
-
// Extract language models with "image-generation" tag from the snapshot
|
|
41
|
-
type MultimodalImageModel =
|
|
42
|
-
Extract<
|
|
43
|
-
(typeof models)[number],
|
|
44
|
-
{ type: "language"; tags: readonly string[] }
|
|
45
|
-
> extends infer Model
|
|
46
|
-
? Model extends { id: infer Id; tags: infer Tags extends readonly string[] }
|
|
47
|
-
? TupleIncludes<Tags, "image-generation"> extends true
|
|
48
|
-
? Id
|
|
49
|
-
: never
|
|
50
|
-
: never
|
|
51
|
-
: never;
|
|
52
|
-
|
|
53
|
-
type GatewayImageModelIdMap = {
|
|
54
|
-
[SelectedGateway in GatewayType]:
|
|
55
|
-
| ConfigMediaModelId<InstalledGateway["createImageModel"]>
|
|
56
|
-
| (SelectedGateway extends typeof generatedForGateway
|
|
57
|
-
? MultimodalImageModel
|
|
58
|
-
: never);
|
|
59
|
-
};
|
|
29
|
+
type GatewayImageModelIdMap = Record<
|
|
30
|
+
GatewayType,
|
|
31
|
+
ConfigMediaModelId<InstalledGateway["createImageModel"]>
|
|
32
|
+
>;
|
|
60
33
|
|
|
61
34
|
type GatewayVideoModelIdMap = Record<
|
|
62
35
|
GatewayType,
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ModelData } from "./model-data";
|
|
2
|
+
import type { ReadonlyAiGatewayModel } from "./to-model-data";
|
|
3
|
+
import { getActiveGateway } from "./active-gateway";
|
|
4
|
+
import { toModelData } from "./to-model-data";
|
|
5
|
+
|
|
6
|
+
import { createModuleLogger } from "@/lib/logger";
|
|
7
|
+
|
|
8
|
+
const log = createModuleLogger("ai/models");
|
|
9
|
+
export const discoverModels = async (): Promise<ModelData[]> => {
|
|
10
|
+
const activeGateway = getActiveGateway();
|
|
11
|
+
|
|
12
|
+
log.debug({ gateway: activeGateway.type }, "Fetching models from gateway");
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
const models = await activeGateway.fetchModels();
|
|
16
|
+
log.info(
|
|
17
|
+
{ gateway: activeGateway.type, modelCount: models.length },
|
|
18
|
+
"Successfully fetched models from gateway"
|
|
19
|
+
);
|
|
20
|
+
return models.map((model: ReadonlyAiGatewayModel) => toModelData(model));
|
|
21
|
+
} catch (error) {
|
|
22
|
+
log.error(
|
|
23
|
+
{ err: error, gateway: activeGateway.type },
|
|
24
|
+
"Error fetching models from gateway"
|
|
25
|
+
);
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// EVE also runs outside Next.js, where the framework data cache is unavailable.
|
|
31
|
+
const MODEL_CATALOG_TTL_MS = 3_600_000;
|
|
32
|
+
let catalog: { expires: number; models: ModelData[] } | undefined;
|
|
33
|
+
let loading: Promise<ModelData[]> | undefined;
|
|
34
|
+
|
|
35
|
+
export const loadModelCatalog = async (): Promise<readonly ModelData[]> => {
|
|
36
|
+
if (catalog && catalog.expires > Date.now()) {
|
|
37
|
+
return catalog.models;
|
|
38
|
+
}
|
|
39
|
+
if (loading) {
|
|
40
|
+
return await loading;
|
|
41
|
+
}
|
|
42
|
+
loading = discoverModels();
|
|
43
|
+
try {
|
|
44
|
+
const models = await loading;
|
|
45
|
+
catalog = { expires: Date.now() + MODEL_CATALOG_TTL_MS, models };
|
|
46
|
+
return models;
|
|
47
|
+
} finally {
|
|
48
|
+
loading = undefined;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
@@ -1,44 +1,9 @@
|
|
|
1
|
-
import type { AiGatewayModel } from "@/chatjs/core/gateway/models";
|
|
2
|
-
import type { ModelData } from "./model-data";
|
|
3
|
-
import type { ReadonlyAiGatewayModel } from "./to-model-data";
|
|
4
|
-
|
|
5
|
-
import { config } from "@/lib/config";
|
|
6
|
-
import { createModuleLogger } from "@/lib/logger";
|
|
7
|
-
import { getActiveGateway } from "./active-gateway";
|
|
8
|
-
import { toModelData } from "./to-model-data";
|
|
9
1
|
import { unstable_cache } from "next/cache";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const fetchModelsRaw = async (): Promise<AiGatewayModel[]> => {
|
|
14
|
-
const activeGateway = getActiveGateway();
|
|
15
|
-
|
|
16
|
-
log.debug({ gateway: activeGateway.type }, "Fetching models from gateway");
|
|
17
|
-
|
|
18
|
-
try {
|
|
19
|
-
const models = await activeGateway.fetchModels();
|
|
20
|
-
log.info(
|
|
21
|
-
{ gateway: activeGateway.type, modelCount: models.length },
|
|
22
|
-
"Successfully fetched models from gateway"
|
|
23
|
-
);
|
|
24
|
-
return models;
|
|
25
|
-
} catch (error) {
|
|
26
|
-
log.error(
|
|
27
|
-
{ err: error, gateway: activeGateway.type },
|
|
28
|
-
"Error fetching models from gateway"
|
|
29
|
-
);
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
2
|
+
import { config } from "@/lib/config";
|
|
3
|
+
import { discoverModels } from "./model-catalog";
|
|
33
4
|
|
|
34
5
|
export const fetchModels = unstable_cache(
|
|
35
|
-
|
|
36
|
-
const models = await fetchModelsRaw();
|
|
37
|
-
return models.map((model: ReadonlyAiGatewayModel) => toModelData(model));
|
|
38
|
-
},
|
|
6
|
+
discoverModels,
|
|
39
7
|
[`ai-gateway-models-${config.ai.gateway}`],
|
|
40
|
-
{
|
|
41
|
-
revalidate: 3600,
|
|
42
|
-
tags: ["ai-gateway-models"],
|
|
43
|
-
}
|
|
8
|
+
{ revalidate: 3600, tags: ["ai-gateway-models"] }
|
|
44
9
|
);
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import type { LanguageModelMiddleware } from "ai";
|
|
2
1
|
import type { LanguageModelV4 } from "@ai-sdk/provider";
|
|
3
2
|
import { extractReasoningMiddleware, wrapLanguageModel } from "ai";
|
|
4
3
|
import type { AppModelId } from "./app-model-id";
|
|
5
4
|
|
|
6
|
-
import { devToolsMiddleware } from "@ai-sdk/devtools";
|
|
7
5
|
import { getActiveGateway } from "./active-gateway";
|
|
8
6
|
import { loadEveModelDefinition } from "../eve/model-selection";
|
|
9
7
|
|
|
@@ -16,24 +14,12 @@ const getLanguageModel = async (
|
|
|
16
14
|
model.apiModelId
|
|
17
15
|
);
|
|
18
16
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// Add devtools middleware in development
|
|
22
|
-
if (process.env.NODE_ENV === "development") {
|
|
23
|
-
middlewares.push(devToolsMiddleware());
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Add reasoning middleware if the model supports reasoning
|
|
27
|
-
if (model.reasoning && model.owned_by === "xai") {
|
|
28
|
-
middlewares.push(extractReasoningMiddleware({ tagName: "think" }));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (middlewares.length === 0) {
|
|
17
|
+
if (!model.reasoning || model.owned_by !== "xai") {
|
|
32
18
|
return languageProvider;
|
|
33
19
|
}
|
|
34
20
|
|
|
35
21
|
return wrapLanguageModel({
|
|
36
|
-
middleware:
|
|
22
|
+
middleware: extractReasoningMiddleware({ tagName: "think" }),
|
|
37
23
|
model: languageProvider,
|
|
38
24
|
});
|
|
39
25
|
};
|
|
@@ -89,44 +89,6 @@ const aiConfigSchema = installedGatewaySchema.prefault({
|
|
|
89
89
|
...gatewayModelDefaults,
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
-
const createOrganizationSchema = (): z.ZodObject<{
|
|
93
|
-
contact: z.ZodObject<{ legalEmail: z.ZodEmail; privacyEmail: z.ZodEmail }>;
|
|
94
|
-
name: z.ZodString;
|
|
95
|
-
}> =>
|
|
96
|
-
z.object({
|
|
97
|
-
contact: z.object({ legalEmail: z.email(), privacyEmail: z.email() }),
|
|
98
|
-
name: z.string(),
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
const createPoliciesSchema = (): z.ZodObject<{
|
|
102
|
-
privacy: z.ZodObject<{
|
|
103
|
-
lastUpdated: z.ZodOptional<z.ZodString>;
|
|
104
|
-
title: z.ZodString;
|
|
105
|
-
}>;
|
|
106
|
-
terms: z.ZodObject<{
|
|
107
|
-
lastUpdated: z.ZodOptional<z.ZodString>;
|
|
108
|
-
title: z.ZodString;
|
|
109
|
-
}>;
|
|
110
|
-
}> =>
|
|
111
|
-
z.object({
|
|
112
|
-
privacy: z.object({
|
|
113
|
-
lastUpdated: z.string().optional(),
|
|
114
|
-
title: z.string(),
|
|
115
|
-
}),
|
|
116
|
-
terms: z.object({ lastUpdated: z.string().optional(), title: z.string() }),
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
const createServicesSchema = (): z.ZodObject<{
|
|
120
|
-
aiProviders: z.ZodArray<z.ZodString>;
|
|
121
|
-
hosting: z.ZodString;
|
|
122
|
-
paymentProcessors: z.ZodArray<z.ZodString>;
|
|
123
|
-
}> =>
|
|
124
|
-
z.object({
|
|
125
|
-
aiProviders: z.array(z.string()),
|
|
126
|
-
hosting: z.string(),
|
|
127
|
-
paymentProcessors: z.array(z.string()),
|
|
128
|
-
});
|
|
129
|
-
|
|
130
92
|
const createAcceptedAttachmentTypesSchema = (): z.ZodObject<{
|
|
131
93
|
"application/pdf": z.ZodArray<z.ZodString>;
|
|
132
94
|
"image/jpeg": z.ZodArray<z.ZodString>;
|
|
@@ -138,23 +100,6 @@ const createAcceptedAttachmentTypesSchema = (): z.ZodObject<{
|
|
|
138
100
|
"image/png": z.array(z.string()),
|
|
139
101
|
});
|
|
140
102
|
|
|
141
|
-
const pricingConfigSchema = z.object({
|
|
142
|
-
currency: z.string().optional(),
|
|
143
|
-
free: z
|
|
144
|
-
.object({
|
|
145
|
-
name: z.string(),
|
|
146
|
-
summary: z.string(),
|
|
147
|
-
})
|
|
148
|
-
.optional(),
|
|
149
|
-
pro: z
|
|
150
|
-
.object({
|
|
151
|
-
monthlyPrice: z.number(),
|
|
152
|
-
name: z.string(),
|
|
153
|
-
summary: z.string(),
|
|
154
|
-
})
|
|
155
|
-
.optional(),
|
|
156
|
-
});
|
|
157
|
-
|
|
158
103
|
const attachmentsConfigObjectSchema = z.object({
|
|
159
104
|
acceptedTypes: createAcceptedAttachmentTypesSchema().describe(
|
|
160
105
|
"Accepted MIME types with their file extensions"
|
|
@@ -228,15 +173,6 @@ const configDescriptionSchema = z.object({
|
|
|
228
173
|
attachments: attachmentsConfigObjectSchema,
|
|
229
174
|
authentication: authenticationConfigObjectSchema,
|
|
230
175
|
features: featuresConfigObjectSchema,
|
|
231
|
-
legal: z.object({
|
|
232
|
-
governingLaw: z.string(),
|
|
233
|
-
minimumAge: z.number(),
|
|
234
|
-
refundPolicy: z.string(),
|
|
235
|
-
}),
|
|
236
|
-
organization: createOrganizationSchema(),
|
|
237
|
-
policies: createPoliciesSchema(),
|
|
238
|
-
pricing: pricingConfigSchema.optional(),
|
|
239
|
-
services: createServicesSchema(),
|
|
240
176
|
});
|
|
241
177
|
|
|
242
178
|
const configSchema = z.object({
|
|
@@ -252,41 +188,11 @@ const configSchema = z.object({
|
|
|
252
188
|
attachments: attachmentsConfigSchema,
|
|
253
189
|
authentication: authenticationConfigSchema,
|
|
254
190
|
features: featuresConfigSchema,
|
|
255
|
-
legal: z
|
|
256
|
-
.object({
|
|
257
|
-
governingLaw: z.string(),
|
|
258
|
-
minimumAge: z.number(),
|
|
259
|
-
refundPolicy: z.string(),
|
|
260
|
-
})
|
|
261
|
-
.default({
|
|
262
|
-
governingLaw: "United States",
|
|
263
|
-
minimumAge: 13,
|
|
264
|
-
refundPolicy: "no-refunds",
|
|
265
|
-
}),
|
|
266
|
-
organization: createOrganizationSchema().default({
|
|
267
|
-
contact: {
|
|
268
|
-
legalEmail: "legal@your-domain.com",
|
|
269
|
-
privacyEmail: "privacy@your-domain.com",
|
|
270
|
-
},
|
|
271
|
-
name: "Your Organization",
|
|
272
|
-
}),
|
|
273
|
-
policies: createPoliciesSchema().default({
|
|
274
|
-
privacy: { title: "Privacy Policy" },
|
|
275
|
-
terms: { title: "Terms of Service" },
|
|
276
|
-
}),
|
|
277
|
-
pricing: pricingConfigSchema.optional(),
|
|
278
|
-
services: createServicesSchema().default({
|
|
279
|
-
aiProviders: ["OpenAI", "Anthropic", "Google"],
|
|
280
|
-
hosting: "Vercel",
|
|
281
|
-
paymentProcessors: [],
|
|
282
|
-
}),
|
|
283
191
|
});
|
|
284
192
|
|
|
285
193
|
// Output types (after defaults applied)
|
|
286
194
|
type Config = z.infer<typeof configSchema>;
|
|
287
195
|
|
|
288
|
-
type PricingConfig = z.infer<typeof pricingConfigSchema>;
|
|
289
|
-
|
|
290
196
|
type AiConfig = z.infer<typeof aiConfigSchema>;
|
|
291
197
|
|
|
292
198
|
type AttachmentsConfig = z.infer<typeof attachmentsConfigSchema>;
|
|
@@ -397,7 +303,6 @@ export {
|
|
|
397
303
|
FEATURES_DEFAULTS,
|
|
398
304
|
featuresConfigObjectSchema,
|
|
399
305
|
featuresConfigSchema,
|
|
400
|
-
pricingConfigSchema,
|
|
401
306
|
};
|
|
402
307
|
export type {
|
|
403
308
|
AiConfig,
|
|
@@ -406,6 +311,5 @@ export type {
|
|
|
406
311
|
Config,
|
|
407
312
|
ConfigInput,
|
|
408
313
|
FeaturesConfig,
|
|
409
|
-
PricingConfig,
|
|
410
314
|
};
|
|
411
315
|
export type { GatewayType } from "@/lib/ai/gateways/registry";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ReactQueryDevtools = (): null => null;
|
|
@@ -1,44 +1,10 @@
|
|
|
1
1
|
import { MockLanguageModelV3 } from "ai/test";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
getEveModelDefinition,
|
|
6
|
-
loadEveModelDefinition,
|
|
7
|
-
resolveEveModel,
|
|
8
|
-
} from "./model-selection";
|
|
4
|
+
import { loadEveModelDefinition, resolveEveModel } from "./model-selection";
|
|
9
5
|
|
|
10
|
-
vi.
|
|
11
|
-
|
|
12
|
-
createLanguageModel: (id: string) =>
|
|
13
|
-
new MockLanguageModelV3({
|
|
14
|
-
doGenerate: vi
|
|
15
|
-
.fn()
|
|
16
|
-
.mockRejectedValue(new Error(`provider model: ${id}`)),
|
|
17
|
-
modelId: id,
|
|
18
|
-
provider: "test",
|
|
19
|
-
}),
|
|
20
|
-
fetchModels: async () => {
|
|
21
|
-
const { getFallbackModels } =
|
|
22
|
-
await import("../ai/gateways/fallback-models");
|
|
23
|
-
return [
|
|
24
|
-
...getFallbackModels("test"),
|
|
25
|
-
{ id: "live-only", pricing: {}, tags: [], type: "language" },
|
|
26
|
-
];
|
|
27
|
-
},
|
|
28
|
-
}),
|
|
29
|
-
}));
|
|
30
|
-
vi.mock("../config", () => ({
|
|
31
|
-
config: {
|
|
32
|
-
ai: {
|
|
33
|
-
disabledModels: ["disabled"],
|
|
34
|
-
providerOrder: [],
|
|
35
|
-
gateway: "test",
|
|
36
|
-
workflows: { chat: "plain" },
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
}));
|
|
40
|
-
vi.mock("../ai/gateways/fallback-models", () => ({
|
|
41
|
-
getFallbackModels: () => [
|
|
6
|
+
const { discoverModels } = vi.hoisted(() => ({
|
|
7
|
+
discoverModels: vi.fn().mockResolvedValue([
|
|
42
8
|
{
|
|
43
9
|
context_window: 1000,
|
|
44
10
|
id: "plain",
|
|
@@ -73,16 +39,42 @@ vi.mock("../ai/gateways/fallback-models", () => ({
|
|
|
73
39
|
},
|
|
74
40
|
{ id: "disabled", pricing: {}, tags: [], type: "language" },
|
|
75
41
|
{ id: "image", pricing: {}, tags: [], type: "image" },
|
|
76
|
-
],
|
|
42
|
+
]),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
vi.mock("../ai/active-gateway", () => ({
|
|
46
|
+
getActiveGateway: () => ({
|
|
47
|
+
createLanguageModel: (id: string) =>
|
|
48
|
+
new MockLanguageModelV3({
|
|
49
|
+
doGenerate: vi
|
|
50
|
+
.fn()
|
|
51
|
+
.mockRejectedValue(new Error(`provider model: ${id}`)),
|
|
52
|
+
modelId: id,
|
|
53
|
+
provider: "test",
|
|
54
|
+
}),
|
|
55
|
+
fetchModels: discoverModels,
|
|
56
|
+
}),
|
|
57
|
+
}));
|
|
58
|
+
vi.mock("../config", () => ({
|
|
59
|
+
config: {
|
|
60
|
+
ai: {
|
|
61
|
+
disabledModels: ["disabled"],
|
|
62
|
+
providerOrder: [],
|
|
63
|
+
gateway: "test",
|
|
64
|
+
workflows: { chat: "plain" },
|
|
65
|
+
},
|
|
66
|
+
},
|
|
77
67
|
}));
|
|
78
68
|
|
|
79
69
|
test("keeps the provider model and reasoning variant distinct", async () => {
|
|
80
|
-
expect(
|
|
70
|
+
expect(await loadEveModelDefinition()).toMatchObject({
|
|
81
71
|
id: "plain",
|
|
82
72
|
reasoning: false,
|
|
83
73
|
});
|
|
84
|
-
expect(
|
|
85
|
-
|
|
74
|
+
expect(await loadEveModelDefinition("thinking")).toMatchObject({
|
|
75
|
+
reasoning: false,
|
|
76
|
+
});
|
|
77
|
+
expect(await loadEveModelDefinition("thinking-reasoning")).toMatchObject({
|
|
86
78
|
apiModelId: "thinking",
|
|
87
79
|
reasoning: true,
|
|
88
80
|
});
|
|
@@ -127,19 +119,24 @@ test("the logical reasoning identity still dispatches to the original provider m
|
|
|
127
119
|
});
|
|
128
120
|
test.each(["unknown", "disabled", "image", "plain-reasoning"])(
|
|
129
121
|
"rejects unavailable selection %s",
|
|
130
|
-
(id) => {
|
|
131
|
-
expect((
|
|
122
|
+
async (id) => {
|
|
123
|
+
await expect(loadEveModelDefinition(id)).rejects.toThrow("not available");
|
|
132
124
|
}
|
|
133
125
|
);
|
|
134
126
|
|
|
135
|
-
test("accepts live catalog models absent from the snapshot", async () => {
|
|
136
|
-
expect(() => getEveModelDefinition("live-only")).toThrow();
|
|
137
|
-
expect(await loadEveModelDefinition("live-only")).toMatchObject({
|
|
138
|
-
id: "live-only",
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
127
|
test("chooses a discovered chat model when the gateway's preferred ID is missing", async () => {
|
|
143
128
|
const { loadDefaultEveWebModelId } = await import("./model-selection");
|
|
144
129
|
expect(await loadDefaultEveWebModelId("missing-default")).toBe("plain");
|
|
145
130
|
});
|
|
131
|
+
|
|
132
|
+
test("retries discovery after a gateway error and reuses the successful catalog", async () => {
|
|
133
|
+
vi.resetModules();
|
|
134
|
+
const { loadEveModelDefinition: load } = await import("./model-selection");
|
|
135
|
+
discoverModels
|
|
136
|
+
.mockClear()
|
|
137
|
+
.mockRejectedValueOnce(new Error("gateway unavailable"));
|
|
138
|
+
await expect(load("plain")).rejects.toThrow("gateway unavailable");
|
|
139
|
+
expect(await load("plain")).toMatchObject({ id: "plain" });
|
|
140
|
+
await load("thinking");
|
|
141
|
+
expect(discoverModels).toHaveBeenCalledTimes(2);
|
|
142
|
+
});
|
|
@@ -4,10 +4,8 @@ import { wrapLanguageModel } from "ai";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
|
|
6
6
|
import { getActiveGateway } from "@/lib/ai/active-gateway";
|
|
7
|
-
import { getFallbackModels } from "@/lib/ai/gateways/fallback-models";
|
|
8
7
|
import type { ModelData } from "@/lib/ai/model-data";
|
|
9
|
-
import
|
|
10
|
-
import { toModelData } from "@/lib/ai/to-model-data";
|
|
8
|
+
import { loadModelCatalog } from "@/lib/ai/model-catalog";
|
|
11
9
|
import {
|
|
12
10
|
buildWebChatModels,
|
|
13
11
|
getDefaultWebChatModelId,
|
|
@@ -15,7 +13,6 @@ import {
|
|
|
15
13
|
import { config } from "@/lib/config";
|
|
16
14
|
|
|
17
15
|
const serializedOptions = z.record(z.string(), z.record(z.string(), z.json()));
|
|
18
|
-
const MODEL_CATALOG_TTL_MS = 3_600_000;
|
|
19
16
|
type EveModelDefinition = ModelData & {
|
|
20
17
|
apiModelId: string;
|
|
21
18
|
selectedModelId: string;
|
|
@@ -29,12 +26,9 @@ class EveModelUnavailableError extends Error {
|
|
|
29
26
|
}
|
|
30
27
|
|
|
31
28
|
const getEveModelDefinition = (
|
|
32
|
-
|
|
33
|
-
models: readonly ModelData[]
|
|
34
|
-
(model: Readonly<ReadonlyAiGatewayModel>) => toModelData(model)
|
|
35
|
-
)
|
|
29
|
+
id: string,
|
|
30
|
+
models: readonly ModelData[]
|
|
36
31
|
): EveModelDefinition => {
|
|
37
|
-
const id = requestedId ?? config.ai.workflows.chat;
|
|
38
32
|
const model = models.find(
|
|
39
33
|
(item: Readonly<ModelData>) =>
|
|
40
34
|
item.id === id || (item.reasoning && `${item.id}-reasoning` === id)
|
|
@@ -56,31 +50,6 @@ const getEveModelDefinition = (
|
|
|
56
50
|
};
|
|
57
51
|
};
|
|
58
52
|
|
|
59
|
-
let catalog: { expires: number; models: ModelData[] } | undefined;
|
|
60
|
-
let loading: Promise<ModelData[]> | undefined;
|
|
61
|
-
|
|
62
|
-
const loadEveModelCatalog = async (): Promise<readonly ModelData[]> => {
|
|
63
|
-
if (!catalog || catalog.expires < Date.now()) {
|
|
64
|
-
loading ??= (async (): Promise<ModelData[]> => {
|
|
65
|
-
try {
|
|
66
|
-
const models = await getActiveGateway().fetchModels();
|
|
67
|
-
const converted = models.map(
|
|
68
|
-
(model: Readonly<ReadonlyAiGatewayModel>) => toModelData(model)
|
|
69
|
-
);
|
|
70
|
-
catalog = {
|
|
71
|
-
expires: Date.now() + MODEL_CATALOG_TTL_MS,
|
|
72
|
-
models: converted,
|
|
73
|
-
};
|
|
74
|
-
return converted;
|
|
75
|
-
} finally {
|
|
76
|
-
loading = undefined;
|
|
77
|
-
}
|
|
78
|
-
})();
|
|
79
|
-
return await loading;
|
|
80
|
-
}
|
|
81
|
-
return catalog.models;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
53
|
const defaultEveWebModelId = (
|
|
85
54
|
models: readonly ModelData[],
|
|
86
55
|
preferredModelId?: string
|
|
@@ -98,12 +67,12 @@ const defaultEveWebModelId = (
|
|
|
98
67
|
const loadDefaultEveWebModelId = async (
|
|
99
68
|
preferredModelId: string
|
|
100
69
|
): Promise<string> =>
|
|
101
|
-
defaultEveWebModelId(await
|
|
70
|
+
defaultEveWebModelId(await loadModelCatalog(), preferredModelId);
|
|
102
71
|
|
|
103
72
|
const loadEveModelDefinition = async (
|
|
104
73
|
requestedId?: string
|
|
105
74
|
): Promise<EveModelDefinition> => {
|
|
106
|
-
const models = await
|
|
75
|
+
const models = await loadModelCatalog();
|
|
107
76
|
return getEveModelDefinition(
|
|
108
77
|
requestedId ?? defaultEveWebModelId(models),
|
|
109
78
|
models
|
|
@@ -126,7 +95,6 @@ const resolveEveModel = async (requestedId?: string): Promise<SessionModel> => {
|
|
|
126
95
|
};
|
|
127
96
|
export {
|
|
128
97
|
EveModelUnavailableError,
|
|
129
|
-
getEveModelDefinition,
|
|
130
98
|
loadDefaultEveWebModelId,
|
|
131
99
|
loadEveModelDefinition,
|
|
132
100
|
resolveEveModel,
|
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
import { expect, test, vi } from "vitest";
|
|
2
2
|
|
|
3
|
+
import { loadModelCatalog } from "../ai/model-catalog";
|
|
3
4
|
import { createEveToolCost } from "./tool-cost";
|
|
4
5
|
|
|
6
|
+
const { fetchModels } = vi.hoisted(() => ({
|
|
7
|
+
fetchModels: vi.fn().mockResolvedValue([
|
|
8
|
+
{ id: "priced", pricing: { input: "0.000001", output: "0.000002" } },
|
|
9
|
+
{ id: "priced-image", pricing: { image: "0.04" } },
|
|
10
|
+
]),
|
|
11
|
+
}));
|
|
5
12
|
vi.mock("../ai/active-gateway", () => ({
|
|
6
|
-
getActiveGateway: () => ({
|
|
7
|
-
fetchModels: vi.fn().mockResolvedValue([
|
|
8
|
-
{ id: "priced", pricing: { input: "0.000001", output: "0.000002" } },
|
|
9
|
-
{ id: "priced-image", pricing: { image: "0.04" } },
|
|
10
|
-
]),
|
|
11
|
-
}),
|
|
13
|
+
getActiveGateway: () => ({ fetchModels }),
|
|
12
14
|
}));
|
|
15
|
+
|
|
16
|
+
test("uses cached catalog pricing for image and token charges", async () => {
|
|
17
|
+
await loadModelCatalog();
|
|
18
|
+
fetchModels.mockClear();
|
|
19
|
+
const cost = createEveToolCost();
|
|
20
|
+
cost.addImageCost("priced-image", 1, {}, "image");
|
|
21
|
+
cost.addLLMCost("priced", { inputTokens: 100, outputTokens: 200 }, "image");
|
|
22
|
+
expect(await cost.totalUsd()).toBeCloseTo(0.0405);
|
|
23
|
+
expect(fetchModels).not.toHaveBeenCalled();
|
|
24
|
+
});
|
|
13
25
|
test("combines API and nested model usage without rounding each call", async () => {
|
|
14
26
|
const cost = createEveToolCost();
|
|
15
27
|
cost.addAPICost("api", 5);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
import { getActiveGateway } from "@/lib/ai/active-gateway";
|
|
4
|
-
import type { ReadonlyNativeSurface } from "@/lib/readonly-native-surface";
|
|
1
|
+
import type { ModelData } from "@/lib/ai/model-data";
|
|
2
|
+
import { loadModelCatalog } from "@/lib/ai/model-catalog";
|
|
5
3
|
|
|
6
4
|
import { createToolUsage } from "./tool-usage";
|
|
7
5
|
import type { ToolUsage } from "./tool-usage";
|
|
@@ -11,17 +9,12 @@ interface TokenUsage {
|
|
|
11
9
|
outputTokens?: number;
|
|
12
10
|
}
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
const pricingForModel = (
|
|
16
|
-
fetchedModels: readonly {
|
|
17
|
-
readonly id: string;
|
|
18
|
-
readonly pricing: ReadonlyNativeSurface<AiGatewayModel["pricing"]>;
|
|
19
|
-
}[],
|
|
12
|
+
const pricingForModel = async (
|
|
20
13
|
modelId: string
|
|
21
|
-
):
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
): Promise<ModelData["pricing"] | undefined> => {
|
|
15
|
+
const models = await loadModelCatalog();
|
|
16
|
+
return models.find((model) => model.id === modelId)?.pricing;
|
|
17
|
+
};
|
|
25
18
|
|
|
26
19
|
const tokenCost = (
|
|
27
20
|
tokens: number | undefined,
|
|
@@ -79,8 +72,8 @@ export const createEveToolCost = (
|
|
|
79
72
|
if (!Number.isInteger(count) || count < 0) {
|
|
80
73
|
throw new Error("Invalid generated image count.");
|
|
81
74
|
}
|
|
82
|
-
const
|
|
83
|
-
const rate = Number(
|
|
75
|
+
const pricing = await pricingForModel(modelId);
|
|
76
|
+
const rate = Number(pricing?.image);
|
|
84
77
|
if (!Number.isFinite(rate) || rate < 0) {
|
|
85
78
|
throw new Error("Image provider pricing is unavailable.");
|
|
86
79
|
}
|
|
@@ -99,8 +92,7 @@ export const createEveToolCost = (
|
|
|
99
92
|
) {
|
|
100
93
|
throw new Error("Provider usage is unavailable.");
|
|
101
94
|
}
|
|
102
|
-
const
|
|
103
|
-
const pricing = pricingForModel(fetchedModels, modelId);
|
|
95
|
+
const pricing = await pricingForModel(modelId);
|
|
104
96
|
return (
|
|
105
97
|
tokenCost(tokens.inputTokens, pricing?.input) +
|
|
106
98
|
tokenCost(tokens.outputTokens, pricing?.output)
|
|
@@ -20,8 +20,6 @@ export default defineConfig({
|
|
|
20
20
|
"**/composer-icons.generated.ts",
|
|
21
21
|
".eve/**",
|
|
22
22
|
"tests/eve-results/**",
|
|
23
|
-
// The model catalog is generator-owned; avoid unrelated snapshot churn.
|
|
24
|
-
"lib/ai/models.generated.ts",
|
|
25
23
|
"**/chatjs/generated/*.ts",
|
|
26
24
|
],
|
|
27
25
|
// Preserve module evaluation order; lint-policy changes must not reorder imports.
|