chat-js 1.0.0-alpha.5 → 1.0.0-alpha.7
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 +3 -3
- package/dist/index.js +21864 -21965
- package/package.json +1 -2
- package/templates/chat-app/app/(chat)/layout.tsx +8 -20
- package/templates/chat-app/chatjs/core/gateway/defaults.ts +43 -0
- package/templates/chat-app/chatjs/core/gateway/gateway-provider.ts +23 -0
- package/templates/chat-app/chatjs/core/gateway/models.test.ts +39 -0
- package/templates/chat-app/chatjs/core/gateway/models.ts +67 -0
- package/templates/chat-app/chatjs/core/gateway/provider-types.ts +11 -0
- package/templates/chat-app/chatjs/core/gateway/runtime.test.ts +30 -0
- package/templates/chat-app/chatjs/core/gateway/runtime.ts +62 -0
- package/templates/chat-app/chatjs/plugins/guest-chat/guest-policy.ts +1 -0
- package/templates/chat-app/chatjs/plugins/guest-chat/signed-out.tsx +14 -2
- package/templates/chat-app/chatjs/plugins/mcp/routes/oauth-callback.ts +27 -26
- package/templates/chat-app/chatjs/plugins/model-preferences/settings-link.tsx +1 -2
- package/templates/chat-app/chatjs/providers/gateway-defaults.ts +1 -1
- package/templates/chat-app/chatjs/providers/gateway.ts +47 -26
- package/templates/chat-app/chatjs/tools/attach-workspace-file/schemas.ts +13 -2
- package/templates/chat-app/components/eve/eve-model-picker.tsx +3 -0
- package/templates/chat-app/components/model-selector-view.tsx +8 -15
- package/templates/chat-app/components/model-selector.tsx +1 -0
- package/templates/chat-app/integrations/chat/model-options.ts +75 -0
- package/templates/chat-app/integrations/chat/models.ts +3 -8
- package/templates/chat-app/lib/ai/app-models.test.ts +41 -46
- package/templates/chat-app/lib/ai/app-models.ts +4 -154
- package/templates/chat-app/lib/ai/gateways/fallback-models.ts +1 -1
- package/templates/chat-app/lib/ai/gateways/registry.ts +2 -2
- package/templates/chat-app/lib/ai/models.generated.ts +1 -1
- package/templates/chat-app/lib/ai/models.ts +1 -1
- package/templates/chat-app/lib/ai/to-model-data.test.ts +1 -1
- package/templates/chat-app/lib/ai/to-model-data.ts +1 -1
- package/templates/chat-app/lib/ai/web-chat-models.ts +70 -0
- package/templates/chat-app/lib/config-schema.ts +1 -1
- package/templates/chat-app/lib/eve/application-model.ts +17 -5
- package/templates/chat-app/lib/eve/model-selection.test.ts +6 -0
- package/templates/chat-app/lib/eve/model-selection.ts +41 -7
- package/templates/chat-app/lib/eve/tool-cost.ts +1 -1
- package/templates/chat-app/next.config.ts +0 -1
- package/templates/chat-app/package.json +1 -2
- package/templates/chat-app/providers/chat-models-provider.tsx +1 -2
- package/templates/chat-app/scripts/fetch-models.ts +1 -1
- package/templates/electron/src/config.ts +7 -23
- package/templates/electron/src/lib/auth-client.ts +3 -4
- package/templates/electron/src/startup-config.test.ts +133 -0
|
@@ -3,12 +3,20 @@ import { isFileStorageKey } from "@/lib/file-url";
|
|
|
3
3
|
|
|
4
4
|
export const maximumWorkspaceFileBytes = 32 * 1024 * 1024;
|
|
5
5
|
|
|
6
|
+
// Keep Unicode character validation at runtime so model providers do not have
|
|
7
|
+
// to compile JavaScript Unicode property escapes from the tool's JSON Schema.
|
|
8
|
+
const controlCharacter = /\p{Cc}/u;
|
|
9
|
+
const invalidFilenameCharacter = /[/\\\p{Cc}]/u;
|
|
10
|
+
|
|
6
11
|
export const attachWorkspaceFileInput = z.object({
|
|
7
12
|
path: z
|
|
8
13
|
.string()
|
|
9
14
|
.min(1)
|
|
10
15
|
.max(4096)
|
|
11
|
-
.
|
|
16
|
+
.refine(
|
|
17
|
+
(path) => !controlCharacter.test(path),
|
|
18
|
+
"Path contains a control character."
|
|
19
|
+
)
|
|
12
20
|
.describe(
|
|
13
21
|
"File in the current EVE sandbox workspace, e.g. /workspace/report.xlsx."
|
|
14
22
|
),
|
|
@@ -17,7 +25,10 @@ export const attachWorkspaceFileInput = z.object({
|
|
|
17
25
|
.trim()
|
|
18
26
|
.min(1)
|
|
19
27
|
.max(180)
|
|
20
|
-
.
|
|
28
|
+
.refine(
|
|
29
|
+
(name) => !invalidFilenameCharacter.test(name),
|
|
30
|
+
"Filename contains a separator or control character."
|
|
31
|
+
)
|
|
21
32
|
.optional()
|
|
22
33
|
.describe("Optional download filename, including its extension."),
|
|
23
34
|
});
|
|
@@ -10,9 +10,11 @@ import {
|
|
|
10
10
|
/** The default composer chooses one model. Other selection modes supply a slot. */
|
|
11
11
|
export const EveModelPicker = ({
|
|
12
12
|
disabled = false,
|
|
13
|
+
fallbackModelName,
|
|
13
14
|
modelId,
|
|
14
15
|
}: {
|
|
15
16
|
readonly disabled?: boolean;
|
|
17
|
+
readonly fallbackModelName?: string;
|
|
16
18
|
readonly modelId?: string;
|
|
17
19
|
}): React.JSX.Element => {
|
|
18
20
|
const defaultModel = useDefaultModel();
|
|
@@ -22,6 +24,7 @@ export const EveModelPicker = ({
|
|
|
22
24
|
<fieldset disabled={disabled}>
|
|
23
25
|
<ModelSelector
|
|
24
26
|
className="h-8 w-fit max-w-none shrink justify-start truncate px-2 text-xs @[500px]:h-10 @[500px]:px-3 @[500px]:text-sm"
|
|
27
|
+
fallbackModelName={fallbackModelName}
|
|
25
28
|
selectedModelId={selected}
|
|
26
29
|
selectedModelSelection={selected}
|
|
27
30
|
onModelSelectionChangeAction={async (selection) => {
|
|
@@ -5,6 +5,7 @@ import type { getEnabledFeatures } from "@/lib/features-config";
|
|
|
5
5
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
6
6
|
import { ModelSelectionIndicator } from "@/components/model-selection-indicator";
|
|
7
7
|
import { PopoverButton } from "@/components/popover-button";
|
|
8
|
+
import { nonEmptyText } from "@/components/text-value";
|
|
8
9
|
import type { AppModelDefinition, AppModelId } from "@/lib/ai/app-models";
|
|
9
10
|
import {
|
|
10
11
|
CheckIcon,
|
|
@@ -260,6 +261,7 @@ const PureModelSelector = ({
|
|
|
260
261
|
allowMultiple = true,
|
|
261
262
|
selectedModelId,
|
|
262
263
|
selectedModelSelection,
|
|
264
|
+
fallbackModelName,
|
|
263
265
|
className,
|
|
264
266
|
onModelSelectionChangeAction,
|
|
265
267
|
chatModels,
|
|
@@ -278,6 +280,7 @@ const PureModelSelector = ({
|
|
|
278
280
|
readonly allowMultiple?: boolean;
|
|
279
281
|
readonly selectedModelId: AppModelId;
|
|
280
282
|
readonly selectedModelSelection: SelectedModelValue;
|
|
283
|
+
readonly fallbackModelName?: string;
|
|
281
284
|
readonly onModelSelectionChangeAction?: (
|
|
282
285
|
selection: SelectedModelValue
|
|
283
286
|
) => void | Promise<void>;
|
|
@@ -451,21 +454,11 @@ const PureModelSelector = ({
|
|
|
451
454
|
() => getSelectionCount(optimisticSelection),
|
|
452
455
|
[optimisticSelection]
|
|
453
456
|
);
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
return `Selected model +${selectedModelCount - 1}`;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const modelName = selectedItem?.model.name;
|
|
464
|
-
if (modelName !== undefined && modelName !== "") {
|
|
465
|
-
return modelName;
|
|
466
|
-
}
|
|
467
|
-
return "Select model";
|
|
468
|
-
}, [selectedItem?.model.name, selectedModelCount, useMultipleModels]);
|
|
457
|
+
const modelName = nonEmptyText(selectedItem?.model.name) ?? fallbackModelName;
|
|
458
|
+
const triggerLabel =
|
|
459
|
+
useMultipleModels && selectedModelCount > 1
|
|
460
|
+
? `${modelName ?? "Selected model"} +${selectedModelCount - 1}`
|
|
461
|
+
: (modelName ?? "Select model");
|
|
469
462
|
|
|
470
463
|
const dispatchSelection = useCallback(
|
|
471
464
|
(selection: SelectedModelValue): void => {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { JSONObject, SharedV4ProviderOptions } from "@ai-sdk/provider";
|
|
2
|
+
|
|
3
|
+
const ANTHROPIC_REASONING_BUDGET_TOKENS = 4096;
|
|
4
|
+
const GOOGLE_REASONING_BUDGET_TOKENS = 10_000;
|
|
5
|
+
|
|
6
|
+
const getOpenAIProviderOptions = (
|
|
7
|
+
apiModelId: string,
|
|
8
|
+
reasoning: boolean
|
|
9
|
+
): JSONObject => {
|
|
10
|
+
if (!reasoning) {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
// Vercel IDs include the provider prefix; direct OpenAI IDs do not.
|
|
14
|
+
const modelName = apiModelId.split("/").pop() ?? apiModelId;
|
|
15
|
+
const options: JSONObject = {
|
|
16
|
+
reasoningSummary: "auto",
|
|
17
|
+
};
|
|
18
|
+
if (
|
|
19
|
+
modelName === "gpt-5" ||
|
|
20
|
+
modelName === "gpt-5-mini" ||
|
|
21
|
+
modelName === "gpt-5-nano"
|
|
22
|
+
) {
|
|
23
|
+
options.reasoningEffort = "low";
|
|
24
|
+
}
|
|
25
|
+
return options;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const getModelProviderOptions = (
|
|
29
|
+
model: Readonly<{
|
|
30
|
+
apiModelId: string;
|
|
31
|
+
owned_by: string;
|
|
32
|
+
reasoning: boolean;
|
|
33
|
+
}>
|
|
34
|
+
): SharedV4ProviderOptions => {
|
|
35
|
+
switch (model.owned_by) {
|
|
36
|
+
case "openai": {
|
|
37
|
+
return {
|
|
38
|
+
openai: getOpenAIProviderOptions(model.apiModelId, model.reasoning),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
case "anthropic": {
|
|
42
|
+
if (model.reasoning) {
|
|
43
|
+
return {
|
|
44
|
+
anthropic: {
|
|
45
|
+
thinking: {
|
|
46
|
+
budgetTokens: ANTHROPIC_REASONING_BUDGET_TOKENS,
|
|
47
|
+
type: "enabled",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return { anthropic: {} };
|
|
53
|
+
}
|
|
54
|
+
case "xai": {
|
|
55
|
+
return { xai: {} };
|
|
56
|
+
}
|
|
57
|
+
case "google": {
|
|
58
|
+
if (model.reasoning) {
|
|
59
|
+
return {
|
|
60
|
+
google: {
|
|
61
|
+
thinkingConfig: {
|
|
62
|
+
thinkingBudget: GOOGLE_REASONING_BUDGET_TOKENS,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return { google: {} };
|
|
68
|
+
}
|
|
69
|
+
default: {
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { getModelProviderOptions };
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/** Application model picker choices. Native EVE agents configure their own model. */
|
|
1
|
+
/** Optional web chat overrides. Omit modelIds to use the gateway catalog. */
|
|
4
2
|
export const webModels: {
|
|
5
3
|
readonly model?: string;
|
|
6
|
-
readonly
|
|
7
|
-
} = {
|
|
8
|
-
model: config.ai.workflows.chat,
|
|
9
|
-
allowedModels: [],
|
|
10
|
-
};
|
|
4
|
+
readonly modelIds?: readonly string[];
|
|
5
|
+
} = {};
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { beforeEach, expect, test, vi } from "vitest";
|
|
2
2
|
|
|
3
|
-
import { fetchChatModels
|
|
3
|
+
import { fetchChatModels } from "./app-models";
|
|
4
4
|
import type { ModelData } from "./model-data";
|
|
5
5
|
import { fetchModels } from "./models";
|
|
6
|
+
import { getDefaultWebChatModelId } from "./web-chat-models";
|
|
7
|
+
|
|
8
|
+
const modelSettings = vi.hoisted((): { modelIds?: string[] } => ({}));
|
|
6
9
|
|
|
7
10
|
vi.mock("next/cache", () => ({
|
|
8
11
|
unstable_cache: <Result>(operation: () => Result): (() => Result) =>
|
|
@@ -20,18 +23,7 @@ vi.mock("@/lib/config", () => ({
|
|
|
20
23
|
},
|
|
21
24
|
}));
|
|
22
25
|
vi.mock("@/integrations/chat/models", () => ({
|
|
23
|
-
webModels:
|
|
24
|
-
allowedModels: [
|
|
25
|
-
"openai/gpt-5-mini-reasoning",
|
|
26
|
-
"fixture/known",
|
|
27
|
-
"fixture/new",
|
|
28
|
-
"fixture/last",
|
|
29
|
-
],
|
|
30
|
-
},
|
|
31
|
-
}));
|
|
32
|
-
vi.mock("./models.generated", () => ({
|
|
33
|
-
generatedForGateway: "vercel",
|
|
34
|
-
models: [{ id: "openai/gpt-5-mini" }, { id: "fixture/known" }],
|
|
26
|
+
webModels: modelSettings,
|
|
35
27
|
}));
|
|
36
28
|
vi.mock("./models", () => ({
|
|
37
29
|
fetchModels: vi.fn(),
|
|
@@ -66,39 +58,42 @@ const catalog: ModelData[] = [
|
|
|
66
58
|
{ ...baseModel, id: "fixture/last" },
|
|
67
59
|
];
|
|
68
60
|
|
|
69
|
-
beforeEach(() =>
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
modelSettings.modelIds = undefined;
|
|
63
|
+
vi.mocked(fetchModels).mockResolvedValue(catalog);
|
|
64
|
+
});
|
|
70
65
|
|
|
71
|
-
test(
|
|
66
|
+
test.each([undefined, []])(
|
|
67
|
+
"discovers chat models with modelIds %j",
|
|
68
|
+
async (modelIds) => {
|
|
69
|
+
modelSettings.modelIds = modelIds;
|
|
70
|
+
const models = await fetchChatModels();
|
|
71
|
+
expect(models.map((model: { readonly id: string }) => model.id)).toEqual([
|
|
72
|
+
"openai/gpt-5-mini-reasoning",
|
|
73
|
+
"openai/gpt-5-mini",
|
|
74
|
+
"fixture/known",
|
|
75
|
+
"fixture/new",
|
|
76
|
+
"fixture/last",
|
|
77
|
+
]);
|
|
78
|
+
expect(
|
|
79
|
+
models.find(
|
|
80
|
+
(model: { readonly id: string }) => model.id === "openai/gpt-5-mini"
|
|
81
|
+
)
|
|
82
|
+
).toMatchObject({
|
|
83
|
+
apiModelId: "openai/gpt-5-mini",
|
|
84
|
+
reasoning: false,
|
|
85
|
+
});
|
|
86
|
+
expect(
|
|
87
|
+
catalog.find(
|
|
88
|
+
(model: { readonly id: string }) => model.id === "openai/gpt-5-mini"
|
|
89
|
+
)
|
|
90
|
+
).toMatchObject({ reasoning: true });
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
test("a populated modelIds list restricts the catalog and supplies a usable default", async () => {
|
|
95
|
+
modelSettings.modelIds = ["fixture/new", "fixture/disabled", "fixture/image"];
|
|
72
96
|
const models = await fetchChatModels();
|
|
73
|
-
expect(models.map((model
|
|
74
|
-
|
|
75
|
-
"openai/gpt-5-mini",
|
|
76
|
-
"fixture/known",
|
|
77
|
-
"fixture/new",
|
|
78
|
-
"fixture/last",
|
|
79
|
-
]);
|
|
80
|
-
expect(
|
|
81
|
-
models.find(
|
|
82
|
-
(model: { readonly id: string }) => model.id === "openai/gpt-5-mini"
|
|
83
|
-
)
|
|
84
|
-
).toMatchObject({
|
|
85
|
-
apiModelId: "openai/gpt-5-mini",
|
|
86
|
-
reasoning: false,
|
|
87
|
-
});
|
|
88
|
-
expect(
|
|
89
|
-
catalog.find(
|
|
90
|
-
(model: { readonly id: string }) => model.id === "openai/gpt-5-mini"
|
|
91
|
-
)
|
|
92
|
-
).toMatchObject({ reasoning: true });
|
|
93
|
-
});
|
|
94
|
-
test("readonly catalogs enable configured alternatives alongside curated defaults", async () => {
|
|
95
|
-
const models = Object.freeze(await fetchChatModels());
|
|
96
|
-
const enabled = getDefaultEnabledModels(models);
|
|
97
|
-
expect([...enabled]).toEqual([
|
|
98
|
-
"openai/gpt-5-mini",
|
|
99
|
-
"openai/gpt-5-mini-reasoning",
|
|
100
|
-
"fixture/known",
|
|
101
|
-
"fixture/new",
|
|
102
|
-
"fixture/last",
|
|
103
|
-
]);
|
|
97
|
+
expect(models.map((model) => model.id)).toEqual(["fixture/new"]);
|
|
98
|
+
expect(getDefaultWebChatModelId(models)).toBe("fixture/new");
|
|
104
99
|
});
|
|
@@ -1,163 +1,13 @@
|
|
|
1
|
-
import { webModels } from "@/integrations/chat/models";
|
|
2
|
-
import {
|
|
3
|
-
generatedForGateway,
|
|
4
|
-
models as generatedModels,
|
|
5
|
-
} from "./models.generated";
|
|
6
|
-
import type { AppModelId } from "./app-model-id";
|
|
7
|
-
import type { ModelData } from "./model-data";
|
|
8
|
-
|
|
9
1
|
import { unstable_cache as cache } from "next/cache";
|
|
10
|
-
import { config } from "@/lib/config";
|
|
11
2
|
import { fetchModels } from "./models";
|
|
3
|
+
import { buildWebChatModels } from "./web-chat-models";
|
|
12
4
|
|
|
13
|
-
type AppModelDefinition = Omit<ModelData, "id"> & {
|
|
14
|
-
readonly id: AppModelId;
|
|
15
|
-
readonly apiModelId: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const DISABLED_MODELS = new Set<string>(config.ai.disabledModels);
|
|
19
|
-
const PROVIDER_ORDER = config.ai.providerOrder;
|
|
20
|
-
|
|
21
|
-
const buildAppModels = (models: readonly ModelData[]): AppModelDefinition[] =>
|
|
22
|
-
models
|
|
23
|
-
.flatMap((model): (AppModelDefinition & { disabled: boolean })[] => {
|
|
24
|
-
const modelId = model.id;
|
|
25
|
-
// If the model supports reasoning, return two variants:
|
|
26
|
-
// - Non-reasoning (original id, reasoning=false)
|
|
27
|
-
// - Reasoning (id with -reasoning suffix, reasoning=true)
|
|
28
|
-
if (model.reasoning) {
|
|
29
|
-
const reasoningId = `${modelId}-reasoning`;
|
|
30
|
-
|
|
31
|
-
return [
|
|
32
|
-
{
|
|
33
|
-
...model,
|
|
34
|
-
apiModelId: modelId,
|
|
35
|
-
disabled: DISABLED_MODELS.has(modelId),
|
|
36
|
-
id: reasoningId,
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
...model,
|
|
40
|
-
apiModelId: modelId,
|
|
41
|
-
disabled: DISABLED_MODELS.has(modelId),
|
|
42
|
-
id: modelId,
|
|
43
|
-
reasoning: false,
|
|
44
|
-
},
|
|
45
|
-
];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Models without reasoning stay as-is
|
|
49
|
-
return [
|
|
50
|
-
{
|
|
51
|
-
...model,
|
|
52
|
-
apiModelId: modelId,
|
|
53
|
-
disabled: DISABLED_MODELS.has(modelId),
|
|
54
|
-
id: modelId,
|
|
55
|
-
},
|
|
56
|
-
];
|
|
57
|
-
})
|
|
58
|
-
.filter(
|
|
59
|
-
(model: {
|
|
60
|
-
readonly type: ModelData["type"];
|
|
61
|
-
readonly disabled: boolean;
|
|
62
|
-
}) => model.type === "language" && !model.disabled
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
const buildChatModels = (
|
|
66
|
-
appModels: readonly AppModelDefinition[]
|
|
67
|
-
): AppModelDefinition[] =>
|
|
68
|
-
appModels
|
|
69
|
-
.filter(
|
|
70
|
-
(model: {
|
|
71
|
-
readonly id: string;
|
|
72
|
-
readonly output: { readonly text: boolean };
|
|
73
|
-
}) =>
|
|
74
|
-
model.output.text &&
|
|
75
|
-
(model.id === (webModels.model ?? config.ai.workflows.chat) ||
|
|
76
|
-
webModels.allowedModels.includes(model.id))
|
|
77
|
-
)
|
|
78
|
-
.toSorted(
|
|
79
|
-
(
|
|
80
|
-
leftModel: { readonly owned_by: string },
|
|
81
|
-
rightModel: { readonly owned_by: string }
|
|
82
|
-
) => {
|
|
83
|
-
const leftProviderIndex = PROVIDER_ORDER.indexOf(leftModel.owned_by);
|
|
84
|
-
const rightProviderIndex = PROVIDER_ORDER.indexOf(rightModel.owned_by);
|
|
85
|
-
|
|
86
|
-
const leftIndex =
|
|
87
|
-
leftProviderIndex === -1 ? PROVIDER_ORDER.length : leftProviderIndex;
|
|
88
|
-
const rightIndex =
|
|
89
|
-
rightProviderIndex === -1
|
|
90
|
-
? PROVIDER_ORDER.length
|
|
91
|
-
: rightProviderIndex;
|
|
92
|
-
|
|
93
|
-
return leftIndex - rightIndex;
|
|
94
|
-
}
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
const fetchAllAppModels = cache(
|
|
98
|
-
async (): Promise<AppModelDefinition[]> => {
|
|
99
|
-
const models = await fetchModels();
|
|
100
|
-
return buildAppModels(models);
|
|
101
|
-
},
|
|
102
|
-
["all-app-models"],
|
|
103
|
-
{ revalidate: 3600, tags: ["ai-gateway-models"] }
|
|
104
|
-
);
|
|
105
5
|
const fetchChatModels = cache(
|
|
106
|
-
async ()
|
|
107
|
-
const appModels = await fetchAllAppModels();
|
|
108
|
-
return buildChatModels(appModels);
|
|
109
|
-
},
|
|
6
|
+
async () => buildWebChatModels(await fetchModels()),
|
|
110
7
|
["chat-models"],
|
|
111
8
|
{ revalidate: 3600, tags: ["ai-gateway-models"] }
|
|
112
9
|
);
|
|
113
|
-
/**
|
|
114
|
-
* Whether the generated catalog belongs to the selected gateway.
|
|
115
|
-
* @param {string} gateway - Gateway identity from application configuration.
|
|
116
|
-
* @returns {boolean} True when generated IDs can be used to identify previously known models.
|
|
117
|
-
*/
|
|
118
|
-
const snapshotMatchesGateway = (gateway: string): boolean =>
|
|
119
|
-
generatedForGateway === gateway;
|
|
120
|
-
|
|
121
|
-
const KNOWN_MODEL_IDS = new Set<string>(
|
|
122
|
-
snapshotMatchesGateway(config.ai.gateway)
|
|
123
|
-
? generatedModels.map((model: { readonly id: string }) => model.id)
|
|
124
|
-
: []
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Returns the default enabled models for a given list of app models.
|
|
129
|
-
* Includes curated defaults + any new models from the API not in models.generated.ts
|
|
130
|
-
* @param {readonly { readonly id: AppModelId; readonly apiModelId: string }[]} appModels - Available application variants and their provider IDs.
|
|
131
|
-
* @returns {Set<AppModelId>} Curated IDs, their available reasoning variants, and IDs absent from the matching generated catalog.
|
|
132
|
-
*/
|
|
133
|
-
const getDefaultEnabledModels = (
|
|
134
|
-
appModels: readonly {
|
|
135
|
-
readonly id: AppModelId;
|
|
136
|
-
readonly apiModelId: string;
|
|
137
|
-
}[]
|
|
138
|
-
): Set<AppModelId> => {
|
|
139
|
-
const enabled = new Set<AppModelId>([
|
|
140
|
-
...config.ai.curatedDefaults,
|
|
141
|
-
webModels.model ?? config.ai.workflows.chat,
|
|
142
|
-
...webModels.allowedModels,
|
|
143
|
-
]);
|
|
144
|
-
|
|
145
|
-
// If a curated default has a -reasoning variant, enable it too
|
|
146
|
-
for (const model of appModels) {
|
|
147
|
-
if (model.id.endsWith("-reasoning") && enabled.has(model.apiModelId)) {
|
|
148
|
-
enabled.add(model.id);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Add any new models from the API that aren't in our generated snapshot
|
|
153
|
-
for (const model of appModels) {
|
|
154
|
-
if (!KNOWN_MODEL_IDS.has(model.apiModelId)) {
|
|
155
|
-
enabled.add(model.id);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
10
|
|
|
159
|
-
|
|
160
|
-
};
|
|
161
|
-
export { fetchChatModels, getDefaultEnabledModels };
|
|
162
|
-
export type { AppModelDefinition };
|
|
11
|
+
export { fetchChatModels };
|
|
12
|
+
export type { AppModelDefinition } from "./web-chat-models";
|
|
163
13
|
export type { AppModelId } from "./app-model-id";
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
models as fallbackModels,
|
|
3
3
|
generatedForGateway,
|
|
4
4
|
} from "@/lib/ai/models.generated";
|
|
5
|
-
import type { AiGatewayModel } from "
|
|
5
|
+
import type { AiGatewayModel } from "@/chatjs/core/gateway/models";
|
|
6
6
|
import { createModuleLogger } from "@/lib/logger";
|
|
7
7
|
|
|
8
8
|
const log = createModuleLogger("ai/gateways/fallback");
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { generatedForGateway, models } from "@/lib/ai/models.generated";
|
|
2
2
|
import type { createGateway } from "@/integrations/providers/gateway";
|
|
3
|
-
import type { GatewayProvider as GatewayProviderBase } from "
|
|
3
|
+
import type { GatewayProvider as GatewayProviderBase } from "@/chatjs/core/gateway/gateway-provider";
|
|
4
4
|
import type {
|
|
5
5
|
ConfigMediaModelId,
|
|
6
6
|
StrictLiterals,
|
|
7
|
-
} from "
|
|
7
|
+
} from "@/chatjs/core/gateway/provider-types";
|
|
8
8
|
import type { gatewayType } from "@/chatjs/providers/gateway-defaults";
|
|
9
9
|
|
|
10
10
|
type InstalledGateway = ReturnType<typeof createGateway>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { webModels } from "@/integrations/chat/models";
|
|
2
|
+
import { config } from "@/lib/config";
|
|
3
|
+
import type { ModelData } from "./model-data";
|
|
4
|
+
|
|
5
|
+
type AppModelDefinition = ModelData & { readonly apiModelId: string };
|
|
6
|
+
|
|
7
|
+
const getConfiguredWebChatModel = (
|
|
8
|
+
defaultModelId: string = config.ai.workflows.chat
|
|
9
|
+
): string => webModels.model ?? defaultModelId;
|
|
10
|
+
|
|
11
|
+
const isWebChatModelAllowed = (modelId: string): boolean => {
|
|
12
|
+
const { modelIds } = webModels;
|
|
13
|
+
return (
|
|
14
|
+
modelIds === undefined ||
|
|
15
|
+
modelIds.length === 0 ||
|
|
16
|
+
modelIds.includes(modelId)
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const providerRank = (provider: string): number => {
|
|
21
|
+
const index = config.ai.providerOrder.indexOf(provider);
|
|
22
|
+
return index === -1 ? config.ai.providerOrder.length : index;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const recommendedIds = new Set<string>(config.ai.curatedDefaults);
|
|
26
|
+
const disabledIds = new Set<string>(config.ai.disabledModels);
|
|
27
|
+
|
|
28
|
+
const buildWebChatModels = (
|
|
29
|
+
models: readonly ModelData[]
|
|
30
|
+
): AppModelDefinition[] =>
|
|
31
|
+
models
|
|
32
|
+
.flatMap((model: Readonly<ModelData>): AppModelDefinition[] => {
|
|
33
|
+
if (
|
|
34
|
+
model.type !== "language" ||
|
|
35
|
+
!model.output.text ||
|
|
36
|
+
disabledIds.has(model.id)
|
|
37
|
+
) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
const standard = { ...model, apiModelId: model.id, reasoning: false };
|
|
41
|
+
return model.reasoning
|
|
42
|
+
? [
|
|
43
|
+
{ ...model, apiModelId: model.id, id: `${model.id}-reasoning` },
|
|
44
|
+
standard,
|
|
45
|
+
]
|
|
46
|
+
: [standard];
|
|
47
|
+
})
|
|
48
|
+
.filter((model) => isWebChatModelAllowed(model.id))
|
|
49
|
+
.toSorted(
|
|
50
|
+
(left, right) =>
|
|
51
|
+
Number(recommendedIds.has(right.apiModelId)) -
|
|
52
|
+
Number(recommendedIds.has(left.apiModelId)) ||
|
|
53
|
+
providerRank(left.owned_by) - providerRank(right.owned_by)
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const getDefaultWebChatModelId = (
|
|
57
|
+
models: readonly Pick<AppModelDefinition, "apiModelId" | "id">[],
|
|
58
|
+
preferredModelId = getConfiguredWebChatModel()
|
|
59
|
+
): string | undefined =>
|
|
60
|
+
models.find((model) => model.id === preferredModelId)?.id ??
|
|
61
|
+
models.find((model) => model.id === model.apiModelId)?.id ??
|
|
62
|
+
models[0]?.id;
|
|
63
|
+
|
|
64
|
+
export {
|
|
65
|
+
buildWebChatModels,
|
|
66
|
+
getConfiguredWebChatModel,
|
|
67
|
+
getDefaultWebChatModelId,
|
|
68
|
+
isWebChatModelAllowed,
|
|
69
|
+
};
|
|
70
|
+
export type { AppModelDefinition };
|
|
@@ -63,7 +63,7 @@ const createAiSchema = <Gateway extends GatewayType>(
|
|
|
63
63
|
z.object({
|
|
64
64
|
curatedDefaults: z
|
|
65
65
|
.array(gatewayModelId<Gateway>())
|
|
66
|
-
.describe("
|
|
66
|
+
.describe("Recommended models shown first in the chat catalog"),
|
|
67
67
|
disabledModels: z
|
|
68
68
|
.array(gatewayModelId<Gateway>())
|
|
69
69
|
.describe("Models to hide from all users"),
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { defineDynamic } from "eve/models";
|
|
2
2
|
import { defineState } from "eve/context";
|
|
3
3
|
import { wrapLanguageModel } from "ai";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
getConfiguredWebChatModel,
|
|
6
|
+
isWebChatModelAllowed,
|
|
7
|
+
} from "@/lib/ai/web-chat-models";
|
|
5
8
|
import { isAccountSession } from "./session-identity";
|
|
6
9
|
import { installedToolAvailabilityMiddleware } from "./tool-availability";
|
|
7
10
|
|
|
@@ -30,15 +33,24 @@ export const applicationModel = (model: string, allowWebSelection = true) =>
|
|
|
30
33
|
? context.session.auth.current?.attributes.modelId
|
|
31
34
|
: undefined;
|
|
32
35
|
if (typeof requested === "string") {
|
|
33
|
-
if (
|
|
36
|
+
if (!isWebChatModelAllowed(requested)) {
|
|
34
37
|
throw new Error("This model is not enabled for web chat.");
|
|
35
38
|
}
|
|
36
39
|
selectedModel.update(() => ({ modelId: requested }));
|
|
37
40
|
}
|
|
38
|
-
const modelId =
|
|
39
|
-
(allowWebSelection ? selectedModel.get().modelId : undefined) ?? model;
|
|
40
41
|
const { billing } = await import("@/integrations/billing");
|
|
41
|
-
const { resolveEveModel } =
|
|
42
|
+
const { loadDefaultEveWebModelId, resolveEveModel } =
|
|
43
|
+
await import("./model-selection");
|
|
44
|
+
const retainedModel = allowWebSelection
|
|
45
|
+
? selectedModel.get().modelId
|
|
46
|
+
: undefined;
|
|
47
|
+
let modelId = model;
|
|
48
|
+
if (allowWebSelection) {
|
|
49
|
+
modelId =
|
|
50
|
+
retainedModel !== undefined && isWebChatModelAllowed(retainedModel)
|
|
51
|
+
? retainedModel
|
|
52
|
+
: await loadDefaultEveWebModelId(getConfiguredWebChatModel(model));
|
|
53
|
+
}
|
|
42
54
|
let resolved;
|
|
43
55
|
if (billing.debit === undefined) {
|
|
44
56
|
resolved = await resolveEveModel(modelId);
|
|
@@ -31,6 +31,7 @@ vi.mock("../config", () => ({
|
|
|
31
31
|
config: {
|
|
32
32
|
ai: {
|
|
33
33
|
disabledModels: ["disabled"],
|
|
34
|
+
providerOrder: [],
|
|
34
35
|
gateway: "test",
|
|
35
36
|
workflows: { chat: "plain" },
|
|
36
37
|
},
|
|
@@ -137,3 +138,8 @@ test("accepts live catalog models absent from the snapshot", async () => {
|
|
|
137
138
|
id: "live-only",
|
|
138
139
|
});
|
|
139
140
|
});
|
|
141
|
+
|
|
142
|
+
test("chooses a discovered chat model when the gateway's preferred ID is missing", async () => {
|
|
143
|
+
const { loadDefaultEveWebModelId } = await import("./model-selection");
|
|
144
|
+
expect(await loadDefaultEveWebModelId("missing-default")).toBe("plain");
|
|
145
|
+
});
|