@fraqjs/plugin-ai 0.1.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/dist/index.d.mts +38 -0
- package/dist/index.mjs +72 -0
- package/package.json +38 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { LanguageModel } from "ai";
|
|
2
|
+
|
|
3
|
+
//#region src/provider.d.ts
|
|
4
|
+
type SupportedSDK = '@ai-sdk/anthropic' | '@ai-sdk/deepseek' | '@ai-sdk/google' | '@ai-sdk/openai';
|
|
5
|
+
interface ProviderConfig {
|
|
6
|
+
sdk: SupportedSDK;
|
|
7
|
+
options: {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
models: string[];
|
|
13
|
+
}
|
|
14
|
+
declare function resolveLanguageModels(config: ProviderConfig): Promise<LanguageModel[]>;
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/service.d.ts
|
|
17
|
+
interface AiServiceOptions {
|
|
18
|
+
models: Record<string, LanguageModel>;
|
|
19
|
+
aliases: Record<string, string>;
|
|
20
|
+
defaultModel: string;
|
|
21
|
+
}
|
|
22
|
+
declare class AiService {
|
|
23
|
+
private readonly options;
|
|
24
|
+
constructor(options: AiServiceOptions);
|
|
25
|
+
hasModel(name: string): boolean;
|
|
26
|
+
model(name?: string): LanguageModel;
|
|
27
|
+
models(): string[];
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/index.d.ts
|
|
31
|
+
interface AiPluginOptions {
|
|
32
|
+
providers: Record<string, ProviderConfig | Record<string, LanguageModel>>;
|
|
33
|
+
aliases?: Record<string, string>;
|
|
34
|
+
defaultModel?: string;
|
|
35
|
+
}
|
|
36
|
+
declare const AiPlugin: import("@fraqjs/fraq").Plugin<[options: AiPluginOptions], import("@fraqjs/fraq").Injection | undefined, import("@fraqjs/fraq").Injection | undefined>;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { AiPlugin, AiPlugin as default, AiPluginOptions, AiService, AiServiceOptions, ProviderConfig, SupportedSDK, resolveLanguageModels };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { definePlugin } from "@fraqjs/fraq";
|
|
2
|
+
//#region src/provider.ts
|
|
3
|
+
async function resolveLanguageModels(config) {
|
|
4
|
+
const { sdk, options, models } = config;
|
|
5
|
+
let provider;
|
|
6
|
+
switch (sdk) {
|
|
7
|
+
case "@ai-sdk/anthropic":
|
|
8
|
+
provider = (await import("@ai-sdk/anthropic")).createAnthropic(options);
|
|
9
|
+
break;
|
|
10
|
+
case "@ai-sdk/deepseek":
|
|
11
|
+
provider = (await import("@ai-sdk/deepseek")).createDeepSeek(options);
|
|
12
|
+
break;
|
|
13
|
+
case "@ai-sdk/google":
|
|
14
|
+
provider = (await import("@ai-sdk/google")).createGoogleGenerativeAI(options);
|
|
15
|
+
break;
|
|
16
|
+
case "@ai-sdk/openai":
|
|
17
|
+
provider = (await import("@ai-sdk/openai")).createOpenAI(options);
|
|
18
|
+
break;
|
|
19
|
+
default: throw new Error(`Unsupported AI SDK: ${sdk}`);
|
|
20
|
+
}
|
|
21
|
+
return models.map((model) => provider.languageModel(model));
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/service.ts
|
|
25
|
+
var AiService = class {
|
|
26
|
+
options;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.options = options;
|
|
29
|
+
if (!options.models[options.defaultModel]) throw new Error(`Invalid default model "${options.defaultModel}": model does not exist.`);
|
|
30
|
+
for (const [alias, target] of Object.entries(options.aliases)) if (!options.models[target]) throw new Error(`Invalid alias "${alias}": target model "${target}" does not exist.`);
|
|
31
|
+
}
|
|
32
|
+
hasModel(name) {
|
|
33
|
+
return !!this.options.models[name] || !!this.options.aliases[name];
|
|
34
|
+
}
|
|
35
|
+
model(name) {
|
|
36
|
+
const modelByName = this.options.models[name ?? this.options.defaultModel];
|
|
37
|
+
if (modelByName) return modelByName;
|
|
38
|
+
const aliasTarget = this.options.aliases[name ?? ""];
|
|
39
|
+
if (aliasTarget) return this.options.models[aliasTarget];
|
|
40
|
+
throw new Error(`Model not found: ${name}`);
|
|
41
|
+
}
|
|
42
|
+
models() {
|
|
43
|
+
return Object.keys(this.options.models);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/index.ts
|
|
48
|
+
function isProviderConfig(config) {
|
|
49
|
+
return "sdk" in config && "options" in config && "models" in config;
|
|
50
|
+
}
|
|
51
|
+
const AiPlugin = definePlugin({
|
|
52
|
+
name: "ai",
|
|
53
|
+
provides: [AiService],
|
|
54
|
+
async apply(ctx, options) {
|
|
55
|
+
const models = {};
|
|
56
|
+
for (const [name, config] of Object.entries(options.providers)) if (isProviderConfig(config)) (await resolveLanguageModels(config)).forEach((model, idx) => {
|
|
57
|
+
const modelName = `${name}/${config.models[idx]}`;
|
|
58
|
+
models[modelName] = model;
|
|
59
|
+
});
|
|
60
|
+
else Object.entries(config).forEach(([modelName, model]) => {
|
|
61
|
+
models[`${name}/${modelName}`] = model;
|
|
62
|
+
});
|
|
63
|
+
if (Object.keys(models).length === 0) throw new Error("No language models resolved from the provided AI SDK configurations.");
|
|
64
|
+
ctx.provide(AiService, new AiService({
|
|
65
|
+
models,
|
|
66
|
+
aliases: options.aliases ?? {},
|
|
67
|
+
defaultModel: options.defaultModel ?? Object.keys(models)[0]
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
//#endregion
|
|
72
|
+
export { AiPlugin, AiPlugin as default, AiService, resolveLanguageModels };
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fraqjs/plugin-ai",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Vercel AI SDK integration plugin for Fraq",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "dist/index.mjs",
|
|
10
|
+
"typings": "dist/index.d.mts",
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "fraqjs",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/fraqjs/fraq.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://fraq.ntqqrev.org/",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"ai": "^6.0.0",
|
|
21
|
+
"@fraqjs/fraq": "^0.8.2"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@ai-sdk/anthropic": "^3.0.81",
|
|
25
|
+
"@ai-sdk/deepseek": "^2.0.35",
|
|
26
|
+
"@ai-sdk/google": "^3.0.80",
|
|
27
|
+
"@ai-sdk/openai": "^3.0.68",
|
|
28
|
+
"@ai-sdk/provider": "^3.0.10",
|
|
29
|
+
"@fraqjs/mock": "^0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=22"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsdown",
|
|
36
|
+
"test": "tsx --test test/*.test.ts"
|
|
37
|
+
}
|
|
38
|
+
}
|