@webiny/api-core 6.4.8 → 6.4.9
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/exports/api.d.ts +1 -2
- package/exports/api.js +1 -1
- package/features/ai/Ai.d.ts +3 -1
- package/features/ai/Ai.js +22 -24
- package/features/ai/Ai.js.map +1 -1
- package/features/ai/AiModelRegistry.d.ts +12 -0
- package/features/ai/AiModelRegistry.js +28 -0
- package/features/ai/AiModelRegistry.js.map +1 -0
- package/features/ai/abstractions.d.ts +8 -0
- package/features/ai/abstractions.js +2 -1
- package/features/ai/abstractions.js.map +1 -1
- package/features/ai/feature.js +2 -0
- package/features/ai/feature.js.map +1 -1
- package/features/ai/index.d.ts +2 -2
- package/features/ai/index.js +1 -1
- package/package.json +15 -15
package/exports/api.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export { AiSdk, AiSdkFactory, AiConnectionFactory, Ai } from "../features/ai/index.js";
|
|
2
|
-
export type { IAiConnection, IAiConnectionInline } from "../features/ai/index.js";
|
|
1
|
+
export { AiSdk, AiSdkFactory, AiConnectionFactory, Ai, AiModelRegistry } from "../features/ai/index.js";
|
|
3
2
|
export { Logger } from "../features/logger/index.js";
|
|
4
3
|
export { Encryption } from "../features/encryption/index.js";
|
|
5
4
|
export { BuildParam, BuildParams } from "../features/buildParams/index.js";
|
package/exports/api.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Ai, AiConnectionFactory, AiSdk, AiSdkFactory } from "../features/ai/index.js";
|
|
1
|
+
export { Ai, AiConnectionFactory, AiModelRegistry, AiSdk, AiSdkFactory } from "../features/ai/index.js";
|
|
2
2
|
export { Logger } from "../features/logger/index.js";
|
|
3
3
|
export { Encryption } from "../features/encryption/index.js";
|
|
4
4
|
export { BuildParam, BuildParams } from "../features/buildParams/index.js";
|
package/features/ai/Ai.d.ts
CHANGED
|
@@ -3,15 +3,17 @@ import { streamText } from "ai";
|
|
|
3
3
|
import { Ai as AiAbstraction } from "./abstractions.js";
|
|
4
4
|
import { AiSdkFactory } from "./abstractions.js";
|
|
5
5
|
import { AiConnectionFactory } from "./abstractions.js";
|
|
6
|
+
import { AiModelRegistry } from "./abstractions.js";
|
|
6
7
|
import type { AiGenerateTextParams, AiModel } from "./abstractions.js";
|
|
7
8
|
import type { AiStreamTextParams } from "./abstractions.js";
|
|
8
9
|
import type { IAiConnectionInline } from "./abstractions.js";
|
|
9
10
|
declare class AiImpl implements AiAbstraction.Interface {
|
|
10
11
|
private readonly sdkFactories;
|
|
11
12
|
private readonly connectionFactories;
|
|
13
|
+
private readonly modelRegistry;
|
|
12
14
|
private sdkCache;
|
|
13
15
|
private resolvedConnections;
|
|
14
|
-
constructor(sdkFactories: AiSdkFactory.Interface[], connectionFactories: AiConnectionFactory.Interface[]);
|
|
16
|
+
constructor(sdkFactories: AiSdkFactory.Interface[], connectionFactories: AiConnectionFactory.Interface[], modelRegistry: AiModelRegistry.Interface);
|
|
15
17
|
generateText(params: AiGenerateTextParams): ReturnType<typeof generateText>;
|
|
16
18
|
streamText(params: AiStreamTextParams): Promise<ReturnType<typeof streamText>>;
|
|
17
19
|
listModels(): Promise<AiModel[]>;
|
package/features/ai/Ai.js
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import { createImplementation } from "@webiny/feature/api";
|
|
2
2
|
import { generateText, streamText } from "ai";
|
|
3
|
-
import { Ai, AiConnectionFactory, AiSdkFactory } from "./abstractions.js";
|
|
4
|
-
function toAiModel(factory, modelId, modelName) {
|
|
5
|
-
return {
|
|
6
|
-
providerId: factory.id,
|
|
7
|
-
providerName: factory.name,
|
|
8
|
-
modelId,
|
|
9
|
-
modelName
|
|
10
|
-
};
|
|
11
|
-
}
|
|
3
|
+
import { Ai, AiConnectionFactory, AiModelRegistry, AiSdkFactory } from "./abstractions.js";
|
|
12
4
|
class AiImpl {
|
|
13
|
-
constructor(sdkFactories, connectionFactories){
|
|
5
|
+
constructor(sdkFactories, connectionFactories, modelRegistry){
|
|
14
6
|
this.sdkFactories = sdkFactories;
|
|
15
7
|
this.connectionFactories = connectionFactories;
|
|
8
|
+
this.modelRegistry = modelRegistry;
|
|
16
9
|
this.sdkCache = new Map();
|
|
17
10
|
this.resolvedConnections = null;
|
|
18
11
|
}
|
|
@@ -32,30 +25,34 @@ class AiImpl {
|
|
|
32
25
|
});
|
|
33
26
|
}
|
|
34
27
|
listModels() {
|
|
35
|
-
return
|
|
28
|
+
return this.modelRegistry.listModels();
|
|
36
29
|
}
|
|
37
30
|
async listModelsByConnections() {
|
|
38
|
-
const connections = await
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
31
|
+
const [connections, models] = await Promise.all([
|
|
32
|
+
this.getConnections(),
|
|
33
|
+
this.modelRegistry.listModels()
|
|
34
|
+
]);
|
|
35
|
+
const connectedProviderIds = new Set(connections.map((c)=>c.sdkName));
|
|
36
|
+
return models.filter((m)=>connectedProviderIds.has(m.providerId));
|
|
44
37
|
}
|
|
45
38
|
async listModelsByConnection(connection) {
|
|
46
|
-
const conn = await
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
const [conn, models] = await Promise.all([
|
|
40
|
+
this.resolveConnection(void 0, connection),
|
|
41
|
+
this.modelRegistry.listModels()
|
|
42
|
+
]);
|
|
43
|
+
return models.filter((m)=>m.providerId === conn.sdkName);
|
|
50
44
|
}
|
|
51
45
|
async resolveLanguageModel(modelId, connection) {
|
|
52
46
|
const slashIndex = modelId.indexOf("/");
|
|
53
47
|
if (-1 === slashIndex) throw new Error(`Invalid model ID "${modelId}". Expected format: "<providerId>/<modelId>" (e.g. "openai/gpt-4o").`);
|
|
54
48
|
const providerId = modelId.slice(0, slashIndex);
|
|
55
|
-
const
|
|
49
|
+
const rawModelId = modelId.slice(slashIndex + 1);
|
|
50
|
+
const models = await this.modelRegistry.listModels();
|
|
51
|
+
const found = models.some((m)=>m.providerId === providerId && m.modelId === rawModelId);
|
|
52
|
+
if (!found) throw new Error(`Model "${modelId}" is not available. Use listModels() to see available models.`);
|
|
56
53
|
const conn = await this.resolveConnection(providerId, connection);
|
|
57
54
|
const sdk = await this.getSdk(conn);
|
|
58
|
-
return sdk.languageModel(
|
|
55
|
+
return sdk.languageModel(rawModelId);
|
|
59
56
|
}
|
|
60
57
|
async getConnections() {
|
|
61
58
|
if (!this.resolvedConnections) this.resolvedConnections = await Promise.all(this.connectionFactories.map((f)=>f.execute()));
|
|
@@ -108,7 +105,8 @@ const Ai_Ai = createImplementation({
|
|
|
108
105
|
{
|
|
109
106
|
multiple: true
|
|
110
107
|
}
|
|
111
|
-
]
|
|
108
|
+
],
|
|
109
|
+
AiModelRegistry
|
|
112
110
|
]
|
|
113
111
|
});
|
|
114
112
|
export { Ai_Ai as Ai };
|
package/features/ai/Ai.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/ai/Ai.js","sources":["../../../src/features/ai/Ai.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { generateText } from \"ai\";\nimport { streamText } from \"ai\";\nimport { Ai as AiAbstraction } from \"./abstractions.js\";\nimport { AiSdkFactory } from \"./abstractions.js\";\nimport { AiConnectionFactory } from \"./abstractions.js\";\nimport type { AiGenerateTextParams, AiModel, IAiSdkFactory } from \"./abstractions.js\";\nimport type { AiStreamTextParams } from \"./abstractions.js\";\nimport type { IAiSdk } from \"./abstractions.js\";\nimport type { IAiConnection } from \"./abstractions.js\";\nimport type { IAiConnectionInline } from \"./abstractions.js\";\nimport type { LanguageModel } from \"ai\";\n\nfunction toAiModel(factory: IAiSdkFactory, modelId: string, modelName: string): AiModel {\n return {\n providerId: factory.id,\n providerName: factory.name,\n modelId,\n modelName\n };\n}\n\nclass AiImpl implements AiAbstraction.Interface {\n private sdkCache = new Map<string, IAiSdk>();\n private resolvedConnections: IAiConnection[] | null = null;\n\n constructor(\n private readonly sdkFactories: AiSdkFactory.Interface[],\n private readonly connectionFactories: AiConnectionFactory.Interface[]\n ) {}\n\n generateText(params: AiGenerateTextParams): ReturnType<typeof generateText> {\n const { model, connection, ...rest } = params;\n return this.resolveLanguageModel(model, connection).then(resolvedModel => {\n // Cast required: spreading the discriminated Prompt union loses its narrowing.\n return generateText({ model: resolvedModel, ...rest } as Parameters<\n typeof generateText\n >[0]);\n });\n }\n\n async streamText(params: AiStreamTextParams): Promise<ReturnType<typeof streamText>> {\n const { model, connection, ...rest } = params;\n const resolvedModel = await this.resolveLanguageModel(model, connection);\n // Cast required: spreading the discriminated Prompt union loses its narrowing.\n return streamText({ model: resolvedModel, ...rest } as Parameters<typeof streamText>[0]);\n }\n\n listModels(): Promise<AiModel[]> {\n return Promise.resolve(\n this.sdkFactories.flatMap(factory =>\n factory.models.map(m => toAiModel(factory, m.id, m.name))\n )\n );\n }\n\n async listModelsByConnections(): Promise<AiModel[]> {\n const connections = await this.getConnections();\n return connections.flatMap(conn => {\n const factory = this.sdkFactories.find(f => f.id === conn.sdkName);\n if (!factory) {\n return [];\n }\n return factory.models.map(m => toAiModel(factory, m.id, m.name));\n });\n }\n\n async listModelsByConnection(connection: string | IAiConnectionInline): Promise<AiModel[]> {\n const conn = await this.resolveConnection(undefined, connection);\n const factory = this.sdkFactories.find(f => f.id === conn.sdkName);\n if (!factory) {\n return [];\n }\n return factory.models.map(m => toAiModel(factory, m.id, m.name));\n }\n\n private async resolveLanguageModel(\n modelId: string,\n connection?: string | IAiConnectionInline\n ): Promise<LanguageModel> {\n const slashIndex = modelId.indexOf(\"/\");\n if (slashIndex === -1) {\n throw new Error(\n `Invalid model ID \"${modelId}\". Expected format: \"<providerId>/<modelId>\" (e.g. \"openai/gpt-4o\").`\n );\n }\n\n const providerId = modelId.slice(0, slashIndex);\n const modelName = modelId.slice(slashIndex + 1);\n\n const conn = await this.resolveConnection(providerId, connection);\n const sdk = await this.getSdk(conn);\n return sdk.languageModel(modelName);\n }\n\n private async getConnections(): Promise<IAiConnection[]> {\n if (!this.resolvedConnections) {\n this.resolvedConnections = await Promise.all(\n this.connectionFactories.map(f => f.execute())\n );\n }\n return this.resolvedConnections;\n }\n\n private async resolveConnection(\n providerId: string | undefined,\n connection?: string | IAiConnectionInline\n ): Promise<IAiConnectionInline> {\n if (typeof connection === \"object\") {\n return connection;\n }\n\n const connections = await this.getConnections();\n\n if (typeof connection === \"string\") {\n const found = connections.find(c => c.id === connection);\n if (!found) {\n const known = connections.map(c => `\"${c.id}\"`).join(\", \");\n throw new Error(\n `Unknown AI connection \"${connection}\". Registered connections: ${known}.`\n );\n }\n return found;\n }\n\n const found = connections.find(c => c.sdkName === providerId);\n if (!found) {\n const known = connections.map(c => `\"${c.id}\" (${c.sdkName})`).join(\", \");\n throw new Error(\n `No AI connection found for provider \"${providerId}\". Registered connections: ${known}.`\n );\n }\n return found;\n }\n\n private async getSdk(connection: IAiConnectionInline): Promise<IAiSdk> {\n const cacheKey =\n \"id\" in connection\n ? (connection as IAiConnection).id\n : `${connection.sdkName}:${connection.apiKey ?? \"__env__\"}`;\n\n const cached = this.sdkCache.get(cacheKey);\n if (cached) {\n return cached;\n }\n\n const factory = this.sdkFactories.find(f => f.id === connection.sdkName);\n if (!factory) {\n const known = this.sdkFactories.map(f => `\"${f.id}\"`).join(\", \");\n throw new Error(\n `No AI SDK factory found for \"${connection.sdkName}\". Registered factories: ${known}.`\n );\n }\n\n const sdk = await factory.execute(connection.apiKey);\n this.sdkCache.set(cacheKey, sdk);\n return sdk;\n }\n}\n\nexport const Ai = createImplementation({\n abstraction: AiAbstraction,\n implementation: AiImpl,\n dependencies: [\n [AiSdkFactory, { multiple: true }],\n [AiConnectionFactory, { multiple: true }]\n ]\n});\n"],"names":["toAiModel","factory","modelId","modelName","AiImpl","sdkFactories","connectionFactories","Map","params","model","connection","rest","resolvedModel","generateText","streamText","Promise","m","connections","conn","f","undefined","slashIndex","Error","providerId","sdk","found","c","known","cacheKey","cached","Ai","createImplementation","AiAbstraction","AiSdkFact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,MAAM,IAAIG,MACN,CAAC,6BAA6B,EAAEZ,WAAW,OAAO,CAAC,yBAAyB,EAAEiB,MAAM,CAAC,CAAC;QAE9F;QAEA,MAAMH,MAAM,MAAMvB,QAAQ,OAAO,CAACS,WAAW,MAAM;QACnD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACkB,UAAUJ;QAC5B,OAAOA;IACX;AACJ;AAEO,MAAMM,QAAKC,qBAAqB;IACnC,aAAaC;IACb,gBAAgB5B;IAChB,cAAc;QACV;YAAC6B;YAAc;gBAAE,UAAU;YAAK;SAAE;QAClC;YAACC;YAAqB;gBAAE,UAAU;YAAK;SAAE;KAC5C;AACL"}
|
|
1
|
+
{"version":3,"file":"features/ai/Ai.js","sources":["../../../src/features/ai/Ai.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { generateText } from \"ai\";\nimport { streamText } from \"ai\";\nimport { Ai as AiAbstraction } from \"./abstractions.js\";\nimport { AiSdkFactory } from \"./abstractions.js\";\nimport { AiConnectionFactory } from \"./abstractions.js\";\nimport { AiModelRegistry } from \"./abstractions.js\";\nimport type { AiGenerateTextParams, AiModel } from \"./abstractions.js\";\nimport type { AiStreamTextParams } from \"./abstractions.js\";\nimport type { IAiSdk } from \"./abstractions.js\";\nimport type { IAiConnection } from \"./abstractions.js\";\nimport type { IAiConnectionInline } from \"./abstractions.js\";\nimport type { LanguageModel } from \"ai\";\n\nclass AiImpl implements AiAbstraction.Interface {\n private sdkCache = new Map<string, IAiSdk>();\n private resolvedConnections: IAiConnection[] | null = null;\n\n constructor(\n private readonly sdkFactories: AiSdkFactory.Interface[],\n private readonly connectionFactories: AiConnectionFactory.Interface[],\n private readonly modelRegistry: AiModelRegistry.Interface\n ) {}\n\n generateText(params: AiGenerateTextParams): ReturnType<typeof generateText> {\n const { model, connection, ...rest } = params;\n return this.resolveLanguageModel(model, connection).then(resolvedModel => {\n // Cast required: spreading the discriminated Prompt union loses its narrowing.\n return generateText({ model: resolvedModel, ...rest } as Parameters<\n typeof generateText\n >[0]);\n });\n }\n\n async streamText(params: AiStreamTextParams): Promise<ReturnType<typeof streamText>> {\n const { model, connection, ...rest } = params;\n const resolvedModel = await this.resolveLanguageModel(model, connection);\n // Cast required: spreading the discriminated Prompt union loses its narrowing.\n return streamText({ model: resolvedModel, ...rest } as Parameters<typeof streamText>[0]);\n }\n\n listModels(): Promise<AiModel[]> {\n return this.modelRegistry.listModels();\n }\n\n async listModelsByConnections(): Promise<AiModel[]> {\n const [connections, models] = await Promise.all([\n this.getConnections(),\n this.modelRegistry.listModels()\n ]);\n const connectedProviderIds = new Set(connections.map(c => c.sdkName));\n return models.filter(m => connectedProviderIds.has(m.providerId));\n }\n\n async listModelsByConnection(connection: string | IAiConnectionInline): Promise<AiModel[]> {\n const [conn, models] = await Promise.all([\n this.resolveConnection(undefined, connection),\n this.modelRegistry.listModels()\n ]);\n return models.filter(m => m.providerId === conn.sdkName);\n }\n\n private async resolveLanguageModel(\n modelId: string,\n connection?: string | IAiConnectionInline\n ): Promise<LanguageModel> {\n const slashIndex = modelId.indexOf(\"/\");\n if (slashIndex === -1) {\n throw new Error(\n `Invalid model ID \"${modelId}\". Expected format: \"<providerId>/<modelId>\" (e.g. \"openai/gpt-4o\").`\n );\n }\n\n const providerId = modelId.slice(0, slashIndex);\n const rawModelId = modelId.slice(slashIndex + 1);\n\n // Validate the model is in the registry.\n const models = await this.modelRegistry.listModels();\n const found = models.some(m => m.providerId === providerId && m.modelId === rawModelId);\n if (!found) {\n throw new Error(\n `Model \"${modelId}\" is not available. Use listModels() to see available models.`\n );\n }\n\n const conn = await this.resolveConnection(providerId, connection);\n const sdk = await this.getSdk(conn);\n return sdk.languageModel(rawModelId);\n }\n\n private async getConnections(): Promise<IAiConnection[]> {\n if (!this.resolvedConnections) {\n this.resolvedConnections = await Promise.all(\n this.connectionFactories.map(f => f.execute())\n );\n }\n return this.resolvedConnections;\n }\n\n private async resolveConnection(\n providerId: string | undefined,\n connection?: string | IAiConnectionInline\n ): Promise<IAiConnectionInline> {\n if (typeof connection === \"object\") {\n return connection;\n }\n\n const connections = await this.getConnections();\n\n if (typeof connection === \"string\") {\n const found = connections.find(c => c.id === connection);\n if (!found) {\n const known = connections.map(c => `\"${c.id}\"`).join(\", \");\n throw new Error(\n `Unknown AI connection \"${connection}\". Registered connections: ${known}.`\n );\n }\n return found;\n }\n\n const found = connections.find(c => c.sdkName === providerId);\n if (!found) {\n const known = connections.map(c => `\"${c.id}\" (${c.sdkName})`).join(\", \");\n throw new Error(\n `No AI connection found for provider \"${providerId}\". Registered connections: ${known}.`\n );\n }\n return found;\n }\n\n private async getSdk(connection: IAiConnectionInline): Promise<IAiSdk> {\n const cacheKey =\n \"id\" in connection\n ? (connection as IAiConnection).id\n : `${connection.sdkName}:${connection.apiKey ?? \"__env__\"}`;\n\n const cached = this.sdkCache.get(cacheKey);\n if (cached) {\n return cached;\n }\n\n const factory = this.sdkFactories.find(f => f.id === connection.sdkName);\n if (!factory) {\n const known = this.sdkFactories.map(f => `\"${f.id}\"`).join(\", \");\n throw new Error(\n `No AI SDK factory found for \"${connection.sdkName}\". Registered factories: ${known}.`\n );\n }\n\n const sdk = await factory.execute(connection.apiKey);\n this.sdkCache.set(cacheKey, sdk);\n return sdk;\n }\n}\n\nexport const Ai = createImplementation({\n abstraction: AiAbstraction,\n implementation: AiImpl,\n dependencies: [\n [AiSdkFactory, { multiple: true }],\n [AiConnectionFactory, { multiple: true }],\n AiModelRegistry\n ]\n});\n"],"names":["AiImpl","sdkFactories","connectionFactories","modelRegistry","Map","params","model","connection","rest","resolvedModel","generateText","streamText","connections","models","Promise","connectedProviderIds","Set","c","m","conn","undefined","modelId","slashIndex","Error","providerId","rawModelId","found","sdk","f","known","cacheKey","cached","factory","Ai","createImplementation","AiAbstraction","AiSdkFactory","AiConnectionFactory","AiModelRegistry"],"mappings":";;;AAcA,MAAMA;IAIF,YACqBC,YAAsC,EACtCC,mBAAoD,EACpDC,aAAwC,CAC3D;aAHmBF,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X;AACJ;AAEO,MAAMM,QAAKC,qBAAqB;IACnC,aAAaC;IACb,gBAAgBnC;IAChB,cAAc;QACV;YAACoC;YAAc;gBAAE,UAAU;YAAK;SAAE;QAClC;YAACC;YAAqB;gBAAE,UAAU;YAAK;SAAE;QACzCC;KACH;AACL"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AiModelRegistry as AiModelRegistryAbstraction } from "./abstractions.js";
|
|
2
|
+
import { AiSdkFactory } from "./abstractions.js";
|
|
3
|
+
import type { AiModel } from "./abstractions.js";
|
|
4
|
+
declare class AiModelRegistryImpl implements AiModelRegistryAbstraction.Interface {
|
|
5
|
+
private readonly sdkFactories;
|
|
6
|
+
constructor(sdkFactories: AiSdkFactory.Interface[]);
|
|
7
|
+
listModels(): Promise<AiModel[]>;
|
|
8
|
+
}
|
|
9
|
+
export declare const AiModelRegistry: typeof AiModelRegistryImpl & {
|
|
10
|
+
__abstraction: import("@webiny/di").Abstraction<import("./abstractions.js").IAiModelRegistry>;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AiModelRegistry, AiSdkFactory } from "./abstractions.js";
|
|
2
|
+
class AiModelRegistryImpl {
|
|
3
|
+
constructor(sdkFactories){
|
|
4
|
+
this.sdkFactories = sdkFactories;
|
|
5
|
+
}
|
|
6
|
+
async listModels() {
|
|
7
|
+
return this.sdkFactories.flatMap((factory)=>factory.models.map((m)=>({
|
|
8
|
+
providerId: factory.id,
|
|
9
|
+
providerName: factory.name,
|
|
10
|
+
modelId: m.id,
|
|
11
|
+
modelName: m.name
|
|
12
|
+
})));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const AiModelRegistry_AiModelRegistry = AiModelRegistry.createImplementation({
|
|
16
|
+
implementation: AiModelRegistryImpl,
|
|
17
|
+
dependencies: [
|
|
18
|
+
[
|
|
19
|
+
AiSdkFactory,
|
|
20
|
+
{
|
|
21
|
+
multiple: true
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
]
|
|
25
|
+
});
|
|
26
|
+
export { AiModelRegistry_AiModelRegistry as AiModelRegistry };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=AiModelRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/ai/AiModelRegistry.js","sources":["../../../src/features/ai/AiModelRegistry.ts"],"sourcesContent":["import { AiModelRegistry as AiModelRegistryAbstraction } from \"./abstractions.js\";\nimport { AiSdkFactory } from \"./abstractions.js\";\nimport type { AiModel } from \"./abstractions.js\";\n\nclass AiModelRegistryImpl implements AiModelRegistryAbstraction.Interface {\n constructor(private readonly sdkFactories: AiSdkFactory.Interface[]) {}\n\n async listModels(): Promise<AiModel[]> {\n return this.sdkFactories.flatMap(factory =>\n factory.models.map(m => ({\n providerId: factory.id,\n providerName: factory.name,\n modelId: m.id,\n modelName: m.name\n }))\n );\n }\n}\n\nexport const AiModelRegistry = AiModelRegistryAbstraction.createImplementation({\n implementation: AiModelRegistryImpl,\n dependencies: [[AiSdkFactory, { multiple: true }]]\n});\n"],"names":["AiModelRegistryImpl","sdkFactories","factory","m","AiModelRegistry","AiModelRegistryAbstraction","AiSdkFactory"],"mappings":";AAIA,MAAMA;IACF,YAA6BC,YAAsC,CAAE;aAAxCA,YAAY,GAAZA;IAAyC;IAEtE,MAAM,aAAiC;QACnC,OAAO,IAAI,CAAC,YAAY,CAAC,OAAO,CAACC,CAAAA,UAC7BA,QAAQ,MAAM,CAAC,GAAG,CAACC,CAAAA,IAAM;oBACrB,YAAYD,QAAQ,EAAE;oBACtB,cAAcA,QAAQ,IAAI;oBAC1B,SAASC,EAAE,EAAE;oBACb,WAAWA,EAAE,IAAI;gBACrB;IAER;AACJ;AAEO,MAAMC,kCAAkBC,gBAAAA,oBAA+C,CAAC;IAC3E,gBAAgBL;IAChB,cAAc;QAAC;YAACM;YAAc;gBAAE,UAAU;YAAK;SAAE;KAAC;AACtD"}
|
|
@@ -70,6 +70,14 @@ export declare namespace Ai {
|
|
|
70
70
|
type GenerateTextParams = AiGenerateTextParams;
|
|
71
71
|
type StreamTextParams = AiStreamTextParams;
|
|
72
72
|
}
|
|
73
|
+
export interface IAiModelRegistry {
|
|
74
|
+
listModels(): Promise<AiModel[]>;
|
|
75
|
+
}
|
|
76
|
+
/** Single source of truth for available AI models. Decoratable to restrict the model list. */
|
|
77
|
+
export declare const AiModelRegistry: import("@webiny/di").Abstraction<IAiModelRegistry>;
|
|
78
|
+
export declare namespace AiModelRegistry {
|
|
79
|
+
type Interface = IAiModelRegistry;
|
|
80
|
+
}
|
|
73
81
|
export interface IAiSdkTool<TInput = any> {
|
|
74
82
|
readonly name: string;
|
|
75
83
|
readonly description: string;
|
|
@@ -3,8 +3,9 @@ const AiSdk = createAbstraction("AiSdk");
|
|
|
3
3
|
const AiSdkFactory = createAbstraction("AiSdkFactory");
|
|
4
4
|
const AiConnectionFactory = createAbstraction("AiConnectionFactory");
|
|
5
5
|
const Ai = createAbstraction("Ai");
|
|
6
|
+
const AiModelRegistry = createAbstraction("AiModelRegistry");
|
|
6
7
|
const AiSdkTool = createAbstraction("AiSdkTool");
|
|
7
8
|
const AiSdkTools = createAbstraction("AiSdkTools");
|
|
8
|
-
export { Ai, AiConnectionFactory, AiSdk, AiSdkFactory, AiSdkTool, AiSdkTools };
|
|
9
|
+
export { Ai, AiConnectionFactory, AiModelRegistry, AiSdk, AiSdkFactory, AiSdkTool, AiSdkTools };
|
|
9
10
|
|
|
10
11
|
//# sourceMappingURL=abstractions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/ai/abstractions.js","sources":["../../../src/features/ai/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { generateText } from \"ai\";\nimport type { streamText } from \"ai\";\nimport type { LanguageModel } from \"ai\";\nimport type { FlexibleSchema, ToolSet } from \"ai\";\n\n// AiSdk\n\nexport interface IAiSdkModel {\n id: string; // raw model id, e.g. \"claude-sonnet-4-5\"\n name: string; // human-friendly name, e.g. \"Claude Sonnet 4.5\"\n}\n\nexport interface IAiSdk {\n languageModel(modelId: string): LanguageModel;\n}\n\n/** A single AI SDK instance (e.g. OpenAI, Anthropic) that resolves model instances. */\nexport const AiSdk = createAbstraction<IAiSdk>(\"AiSdk\");\n\nexport namespace AiSdk {\n export type Interface = IAiSdk;\n}\n\n// AiSdkFactory\n\nexport interface IAiSdkFactory {\n readonly id: string; // machine id, e.g. \"anthropic\"\n readonly name: string; // human-friendly name, e.g. \"Anthropic\"\n readonly models: readonly IAiSdkModel[]; // static model list, no API call needed\n execute(apiKey?: string): Promise<IAiSdk>;\n}\n\n/** Factory that asynchronously initialises an AI SDK. Register one per provider namespace. */\nexport const AiSdkFactory = createAbstraction<IAiSdkFactory>(\"AiSdkFactory\");\n\nexport namespace AiSdkFactory {\n export type Interface = IAiSdkFactory;\n}\n\n// AiConnection\n\nexport interface IAiConnectionInline {\n readonly sdkName: string;\n readonly apiKey?: string;\n}\n\nexport interface IAiConnection extends IAiConnectionInline {\n readonly id: string;\n}\n\n// AiConnectionFactory\n\nexport interface IAiConnectionFactory {\n execute(): Promise<IAiConnection>;\n}\n\n/** Factory that asynchronously produces an AiConnection. */\nexport const AiConnectionFactory = createAbstraction<IAiConnectionFactory>(\"AiConnectionFactory\");\n\nexport namespace AiConnectionFactory {\n export type Interface = IAiConnectionFactory;\n}\n\n// Ai\n\ntype SDKGenerateTextParams = Parameters<typeof generateText>[0];\ntype SDKStreamTextParams = Parameters<typeof streamText>[0];\n\nexport type AiGenerateTextParams = Omit<SDKGenerateTextParams, \"model\"> & {\n model: string;\n connection?: string | IAiConnectionInline;\n};\nexport type AiStreamTextParams = Omit<SDKStreamTextParams, \"model\"> & {\n model: string;\n connection?: string | IAiConnectionInline;\n};\n\nexport interface AiModel {\n providerId: string; // e.g. \"anthropic\"\n providerName: string; // e.g. \"Anthropic\"\n modelId: string; // e.g. \"claude-sonnet-4-5\"\n modelName: string; // e.g. \"Claude Sonnet 4.5\"\n}\n\nexport interface IAi {\n generateText(params: AiGenerateTextParams): ReturnType<typeof generateText>;\n streamText(params: AiStreamTextParams): Promise<ReturnType<typeof streamText>>;\n listModels(): Promise<AiModel[]>;\n listModelsByConnections(): Promise<AiModel[]>;\n listModelsByConnection(connection: string | IAiConnectionInline): Promise<AiModel[]>;\n}\n\n/** Interact with AI language models using registered providers. */\nexport const Ai = createAbstraction<IAi>(\"Ai\");\n\nexport namespace Ai {\n export type Interface = IAi;\n export type GenerateTextParams = AiGenerateTextParams;\n export type StreamTextParams = AiStreamTextParams;\n}\n\n// AiSdkTool\n\nexport interface IAiSdkTool<TInput = any> {\n readonly name: string;\n readonly description: string;\n readonly inputSchema: FlexibleSchema<TInput>;\n execute(input: TInput): Promise<unknown>;\n}\n\n/** A single tool that can be provided to AI generateText/streamText calls. */\nexport const AiSdkTool = createAbstraction<IAiSdkTool>(\"AiSdkTool\");\n\nexport namespace AiSdkTool {\n export type Interface = IAiSdkTool;\n}\n\n// AiSdkTools\n\nexport interface IAiSdkTools {\n getToolSet(): ToolSet;\n}\n\n/** Collection of AI SDK tools. Returns a ready-to-use ToolSet for generateText/streamText. */\nexport const AiSdkTools = createAbstraction<IAiSdkTools>(\"AiSdkTools\");\n\nexport namespace AiSdkTools {\n export type Interface = IAiSdkTools;\n}\n"],"names":["AiSdk","createAbstraction","AiSdkFactory","AiConnectionFactory","Ai","AiSdkTool","AiSdkTools"],"mappings":";AAkBO,MAAMA,QAAQC,kBAA0B;AAgBxC,MAAMC,eAAeD,kBAAiC;AAwBtD,MAAME,sBAAsBF,kBAAwC;AAoCpE,MAAMG,KAAKH,kBAAuB;
|
|
1
|
+
{"version":3,"file":"features/ai/abstractions.js","sources":["../../../src/features/ai/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { generateText } from \"ai\";\nimport type { streamText } from \"ai\";\nimport type { LanguageModel } from \"ai\";\nimport type { FlexibleSchema, ToolSet } from \"ai\";\n\n// AiSdk\n\nexport interface IAiSdkModel {\n id: string; // raw model id, e.g. \"claude-sonnet-4-5\"\n name: string; // human-friendly name, e.g. \"Claude Sonnet 4.5\"\n}\n\nexport interface IAiSdk {\n languageModel(modelId: string): LanguageModel;\n}\n\n/** A single AI SDK instance (e.g. OpenAI, Anthropic) that resolves model instances. */\nexport const AiSdk = createAbstraction<IAiSdk>(\"AiSdk\");\n\nexport namespace AiSdk {\n export type Interface = IAiSdk;\n}\n\n// AiSdkFactory\n\nexport interface IAiSdkFactory {\n readonly id: string; // machine id, e.g. \"anthropic\"\n readonly name: string; // human-friendly name, e.g. \"Anthropic\"\n readonly models: readonly IAiSdkModel[]; // static model list, no API call needed\n execute(apiKey?: string): Promise<IAiSdk>;\n}\n\n/** Factory that asynchronously initialises an AI SDK. Register one per provider namespace. */\nexport const AiSdkFactory = createAbstraction<IAiSdkFactory>(\"AiSdkFactory\");\n\nexport namespace AiSdkFactory {\n export type Interface = IAiSdkFactory;\n}\n\n// AiConnection\n\nexport interface IAiConnectionInline {\n readonly sdkName: string;\n readonly apiKey?: string;\n}\n\nexport interface IAiConnection extends IAiConnectionInline {\n readonly id: string;\n}\n\n// AiConnectionFactory\n\nexport interface IAiConnectionFactory {\n execute(): Promise<IAiConnection>;\n}\n\n/** Factory that asynchronously produces an AiConnection. */\nexport const AiConnectionFactory = createAbstraction<IAiConnectionFactory>(\"AiConnectionFactory\");\n\nexport namespace AiConnectionFactory {\n export type Interface = IAiConnectionFactory;\n}\n\n// Ai\n\ntype SDKGenerateTextParams = Parameters<typeof generateText>[0];\ntype SDKStreamTextParams = Parameters<typeof streamText>[0];\n\nexport type AiGenerateTextParams = Omit<SDKGenerateTextParams, \"model\"> & {\n model: string;\n connection?: string | IAiConnectionInline;\n};\nexport type AiStreamTextParams = Omit<SDKStreamTextParams, \"model\"> & {\n model: string;\n connection?: string | IAiConnectionInline;\n};\n\nexport interface AiModel {\n providerId: string; // e.g. \"anthropic\"\n providerName: string; // e.g. \"Anthropic\"\n modelId: string; // e.g. \"claude-sonnet-4-5\"\n modelName: string; // e.g. \"Claude Sonnet 4.5\"\n}\n\nexport interface IAi {\n generateText(params: AiGenerateTextParams): ReturnType<typeof generateText>;\n streamText(params: AiStreamTextParams): Promise<ReturnType<typeof streamText>>;\n listModels(): Promise<AiModel[]>;\n listModelsByConnections(): Promise<AiModel[]>;\n listModelsByConnection(connection: string | IAiConnectionInline): Promise<AiModel[]>;\n}\n\n/** Interact with AI language models using registered providers. */\nexport const Ai = createAbstraction<IAi>(\"Ai\");\n\nexport namespace Ai {\n export type Interface = IAi;\n export type GenerateTextParams = AiGenerateTextParams;\n export type StreamTextParams = AiStreamTextParams;\n}\n\n// AiModelRegistry\n\nexport interface IAiModelRegistry {\n listModels(): Promise<AiModel[]>;\n}\n\n/** Single source of truth for available AI models. Decoratable to restrict the model list. */\nexport const AiModelRegistry = createAbstraction<IAiModelRegistry>(\"AiModelRegistry\");\n\nexport namespace AiModelRegistry {\n export type Interface = IAiModelRegistry;\n}\n\n// AiSdkTool\n\nexport interface IAiSdkTool<TInput = any> {\n readonly name: string;\n readonly description: string;\n readonly inputSchema: FlexibleSchema<TInput>;\n execute(input: TInput): Promise<unknown>;\n}\n\n/** A single tool that can be provided to AI generateText/streamText calls. */\nexport const AiSdkTool = createAbstraction<IAiSdkTool>(\"AiSdkTool\");\n\nexport namespace AiSdkTool {\n export type Interface = IAiSdkTool;\n}\n\n// AiSdkTools\n\nexport interface IAiSdkTools {\n getToolSet(): ToolSet;\n}\n\n/** Collection of AI SDK tools. Returns a ready-to-use ToolSet for generateText/streamText. */\nexport const AiSdkTools = createAbstraction<IAiSdkTools>(\"AiSdkTools\");\n\nexport namespace AiSdkTools {\n export type Interface = IAiSdkTools;\n}\n"],"names":["AiSdk","createAbstraction","AiSdkFactory","AiConnectionFactory","Ai","AiModelRegistry","AiSdkTool","AiSdkTools"],"mappings":";AAkBO,MAAMA,QAAQC,kBAA0B;AAgBxC,MAAMC,eAAeD,kBAAiC;AAwBtD,MAAME,sBAAsBF,kBAAwC;AAoCpE,MAAMG,KAAKH,kBAAuB;AAelC,MAAMI,kBAAkBJ,kBAAoC;AAgB5D,MAAMK,YAAYL,kBAA8B;AAahD,MAAMM,aAAaN,kBAA+B"}
|
package/features/ai/feature.js
CHANGED
|
@@ -2,12 +2,14 @@ import { createFeature } from "@webiny/feature/api";
|
|
|
2
2
|
import { OpenAiSdkFactory } from "./OpenAiSdkFactory.js";
|
|
3
3
|
import { AnthropicSdkFactory } from "./AnthropicSdkFactory.js";
|
|
4
4
|
import { Ai } from "./Ai.js";
|
|
5
|
+
import { AiModelRegistry } from "./AiModelRegistry.js";
|
|
5
6
|
import { AiSdkTools } from "./AiSdkTools.js";
|
|
6
7
|
const AiFeature = createFeature({
|
|
7
8
|
name: "AiFeature",
|
|
8
9
|
register (container) {
|
|
9
10
|
container.register(OpenAiSdkFactory);
|
|
10
11
|
container.register(AnthropicSdkFactory);
|
|
12
|
+
container.register(AiModelRegistry);
|
|
11
13
|
container.register(Ai);
|
|
12
14
|
container.register(AiSdkTools);
|
|
13
15
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/ai/feature.js","sources":["../../../src/features/ai/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { OpenAiSdkFactory } from \"./OpenAiSdkFactory.js\";\nimport { AnthropicSdkFactory } from \"./AnthropicSdkFactory.js\";\nimport { Ai } from \"./Ai.js\";\nimport { AiSdkTools } from \"./AiSdkTools.js\";\n\nexport const AiFeature = createFeature({\n name: \"AiFeature\",\n register(container) {\n container.register(OpenAiSdkFactory);\n container.register(AnthropicSdkFactory);\n container.register(Ai);\n container.register(AiSdkTools);\n }\n});\n"],"names":["AiFeature","createFeature","container","OpenAiSdkFactory","AnthropicSdkFactory","Ai","AiSdkTools"],"mappings":"
|
|
1
|
+
{"version":3,"file":"features/ai/feature.js","sources":["../../../src/features/ai/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { OpenAiSdkFactory } from \"./OpenAiSdkFactory.js\";\nimport { AnthropicSdkFactory } from \"./AnthropicSdkFactory.js\";\nimport { Ai } from \"./Ai.js\";\nimport { AiModelRegistry } from \"./AiModelRegistry.js\";\nimport { AiSdkTools } from \"./AiSdkTools.js\";\n\nexport const AiFeature = createFeature({\n name: \"AiFeature\",\n register(container) {\n container.register(OpenAiSdkFactory);\n container.register(AnthropicSdkFactory);\n container.register(AiModelRegistry);\n container.register(Ai);\n container.register(AiSdkTools);\n }\n});\n"],"names":["AiFeature","createFeature","container","OpenAiSdkFactory","AnthropicSdkFactory","AiModelRegistry","Ai","AiSdkTools"],"mappings":";;;;;;AAOO,MAAMA,YAAYC,cAAc;IACnC,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;QACnBD,UAAU,QAAQ,CAACE;QACnBF,UAAU,QAAQ,CAACG;QACnBH,UAAU,QAAQ,CAACI;QACnBJ,UAAU,QAAQ,CAACK;IACvB;AACJ"}
|
package/features/ai/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { AiSdk, AiSdkFactory, AiConnectionFactory, Ai, AiSdkTool, AiSdkTools } from "./abstractions.js";
|
|
2
|
-
export type { IAiConnection, IAiConnectionInline, AiModel, IAiSdkModel, IAiSdkTool, IAiSdkTools } from "./abstractions.js";
|
|
1
|
+
export { AiSdk, AiSdkFactory, AiConnectionFactory, Ai, AiModelRegistry, AiSdkTool, AiSdkTools } from "./abstractions.js";
|
|
2
|
+
export type { IAiConnection, IAiConnectionInline, AiModel, IAiModelRegistry, IAiSdkModel, IAiSdkTool, IAiSdkTools } from "./abstractions.js";
|
|
3
3
|
export { AiFeature } from "./feature.js";
|
package/features/ai/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { Ai, AiConnectionFactory, AiSdk, AiSdkFactory, AiSdkTool, AiSdkTools } from "./abstractions.js";
|
|
1
|
+
export { Ai, AiConnectionFactory, AiModelRegistry, AiSdk, AiSdkFactory, AiSdkTool, AiSdkTools } from "./abstractions.js";
|
|
2
2
|
export { AiFeature } from "./feature.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-core",
|
|
3
|
-
"version": "6.4.
|
|
3
|
+
"version": "6.4.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,17 +16,17 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@ai-sdk/anthropic": "4.0.10",
|
|
18
18
|
"@ai-sdk/openai": "4.0.9",
|
|
19
|
-
"@webiny/api": "6.4.
|
|
19
|
+
"@webiny/api": "6.4.9",
|
|
20
20
|
"@webiny/di": "1.0.2",
|
|
21
|
-
"@webiny/error": "6.4.
|
|
22
|
-
"@webiny/feature": "6.4.
|
|
23
|
-
"@webiny/feature-flags": "6.4.
|
|
24
|
-
"@webiny/handler": "6.4.
|
|
25
|
-
"@webiny/handler-db": "6.4.
|
|
26
|
-
"@webiny/handler-graphql": "6.4.
|
|
27
|
-
"@webiny/plugins": "6.4.
|
|
28
|
-
"@webiny/utils": "6.4.
|
|
29
|
-
"@webiny/wcp": "6.4.
|
|
21
|
+
"@webiny/error": "6.4.9",
|
|
22
|
+
"@webiny/feature": "6.4.9",
|
|
23
|
+
"@webiny/feature-flags": "6.4.9",
|
|
24
|
+
"@webiny/handler": "6.4.9",
|
|
25
|
+
"@webiny/handler-db": "6.4.9",
|
|
26
|
+
"@webiny/handler-graphql": "6.4.9",
|
|
27
|
+
"@webiny/plugins": "6.4.9",
|
|
28
|
+
"@webiny/utils": "6.4.9",
|
|
29
|
+
"@webiny/wcp": "6.4.9",
|
|
30
30
|
"ai": "7.0.18",
|
|
31
31
|
"dataloader": "2.2.3",
|
|
32
32
|
"jose": "6.2.3",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/jsonwebtoken": "9.0.10",
|
|
42
|
-
"@webiny/build-tools": "6.4.
|
|
43
|
-
"@webiny/db-dynamodb": "6.4.
|
|
44
|
-
"@webiny/handler-aws": "6.4.
|
|
45
|
-
"@webiny/project-utils": "6.4.
|
|
42
|
+
"@webiny/build-tools": "6.4.9",
|
|
43
|
+
"@webiny/db-dynamodb": "6.4.9",
|
|
44
|
+
"@webiny/handler-aws": "6.4.9",
|
|
45
|
+
"@webiny/project-utils": "6.4.9",
|
|
46
46
|
"jest-dynalite": "3.6.1",
|
|
47
47
|
"rimraf": "6.1.3",
|
|
48
48
|
"typescript": "6.0.3",
|