@opentiny/next-sdk 0.1.0 → 0.1.1

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.
@@ -0,0 +1,87 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
21
+ import { streamText, stepCountIs, generateText } from 'ai';
22
+ import { getMcpClients, getMcpTools } from './utils';
23
+ import { AIProviderFactories } from './utils/aiProviderFactories';
24
+ export class AgentModelProvider {
25
+ constructor({ llmConfig, mcpServers, llm }) {
26
+ this.isGetMcpClients = false;
27
+ this.mcpClients = [];
28
+ // 1、保存 mcpServer
29
+ this.mcpServers = mcpServers || [];
30
+ // 2、保存 llm
31
+ if (llm) {
32
+ this.llm = llm;
33
+ }
34
+ else if (llmConfig) {
35
+ let providerFn;
36
+ if (typeof llmConfig.providerType === 'string') {
37
+ providerFn = AIProviderFactories[llmConfig.providerType];
38
+ }
39
+ else {
40
+ providerFn = llmConfig.providerType;
41
+ }
42
+ this.llm = providerFn({
43
+ apiKey: llmConfig.apiKey,
44
+ baseURL: llmConfig.baseURL
45
+ });
46
+ }
47
+ else {
48
+ throw new Error('Either llmConfig or llm must be provided');
49
+ }
50
+ }
51
+ initClients() {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ if (!this.isGetMcpClients) {
54
+ this.mcpClients = yield getMcpClients(this.mcpServers);
55
+ this.isGetMcpClients = true;
56
+ }
57
+ });
58
+ }
59
+ chat(_a) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ var { model, maxSteps = 5 } = _a, options = __rest(_a, ["model", "maxSteps"]);
62
+ if (!this.llm) {
63
+ throw new Error('LLM is not initialized');
64
+ }
65
+ // 每次会话需要获取最新的工具列表,因为工具是会发生变化的
66
+ yield this.initClients();
67
+ const tools = yield getMcpTools(this.mcpClients);
68
+ return generateText(Object.assign({
69
+ // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
70
+ model: this.llm(model), tools: tools, stopWhen: stepCountIs(maxSteps) }, options));
71
+ });
72
+ }
73
+ chatStream(_a) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ var { model, maxSteps = 5 } = _a, options = __rest(_a, ["model", "maxSteps"]);
76
+ if (!this.llm) {
77
+ throw new Error('LLM is not initialized');
78
+ }
79
+ // 每次会话需要获取最新的工具列表,因为工具是会发生变化的
80
+ yield this.initClients();
81
+ const tools = yield getMcpTools(this.mcpClients);
82
+ return streamText(Object.assign({
83
+ // @ts-ignore 同上
84
+ model: this.llm(model), tools: tools, stopWhen: stepCountIs(maxSteps) }, options));
85
+ });
86
+ }
87
+ }
@@ -0,0 +1,32 @@
1
+ export type { experimental_MCPClient as MCPClient } from 'ai';
2
+ import type { ProviderV2 } from '@ai-sdk/provider';
3
+ import type { MCPTransport } from 'ai';
4
+ /** 代理模型提供器的大语言配置对象 */
5
+ export interface IAgentModelProviderLlmConfig {
6
+ apiKey: string;
7
+ baseURL: string;
8
+ /** 支持内置的常用模型,或者传入一个ai-sdk官方的Provider工厂函数
9
+ * @example
10
+ * import { createOpenAI } from '@ai-sdk/openai'
11
+ */
12
+ providerType: 'openai' | 'deepseek' | ((options: any) => ProviderV2);
13
+ }
14
+ /** Mcp Server的配置对象 */
15
+ export type McpServerConfig = {
16
+ type: 'streamableHttp' | 'sse';
17
+ url: string;
18
+ } | {
19
+ transport: MCPTransport;
20
+ };
21
+ /** */
22
+ export interface IAgentModelProviderOption {
23
+ /** ai-sdk官方的Provider实例,不能与 llmConfig 同时传入
24
+ * @example
25
+ * import { openai } from '@ai-sdk/openai'
26
+ */
27
+ llm?: ProviderV2;
28
+ /** 代理模型提供器的大语言配置对象, 不能与 llm 同时传入 */
29
+ llmConfig?: IAgentModelProviderLlmConfig;
30
+ /** Mcp Server的配置对象的集合 */
31
+ mcpServers?: McpServerConfig[];
32
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { createOpenAI } from '@ai-sdk/openai';
2
+ import { createDeepSeek } from '@ai-sdk/deepseek';
3
+ export declare const AIProviderFactories: {
4
+ openai: typeof createOpenAI;
5
+ deepseek: typeof createDeepSeek;
6
+ };
@@ -0,0 +1,6 @@
1
+ import { createOpenAI } from '@ai-sdk/openai';
2
+ import { createDeepSeek } from '@ai-sdk/deepseek';
3
+ export const AIProviderFactories = {
4
+ ['openai']: createOpenAI,
5
+ ['deepseek']: createDeepSeek
6
+ };
@@ -0,0 +1,8 @@
1
+ import { ToolSet } from 'ai';
2
+ import { McpServerConfig, MCPClient } from '../type';
3
+ /** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
4
+ * 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
5
+ */
6
+ export declare const getMcpClients: (mcpServers: McpServerConfig[]) => Promise<(MCPClient | never[])[]>;
7
+ /** 合并所有的Mcp Tools */
8
+ export declare const getMcpTools: (mcpClients: MCPClient[]) => Promise<ToolSet>;
@@ -0,0 +1,43 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { experimental_createMCPClient as createMCPClient } from 'ai';
11
+ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
12
+ /** 创建 McpClients, 其中 mcpServers 允许为配置为 McpServerConfig, 或者任意的 MCPTransport
13
+ * 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client
14
+ */
15
+ export const getMcpClients = (mcpServers) => __awaiter(void 0, void 0, void 0, function* () {
16
+ if (!mcpServers || (mcpServers === null || mcpServers === void 0 ? void 0 : mcpServers.length) === 0) {
17
+ return [];
18
+ }
19
+ // 使用 Promise.all 并行处理所有 mcpServer 项
20
+ const allMcpClients = yield Promise.all(mcpServers.map((item) => __awaiter(void 0, void 0, void 0, function* () {
21
+ try {
22
+ let transport;
23
+ // FIXME
24
+ if ('type' in item && item.type === 'streamableHttp') {
25
+ transport = new StreamableHTTPClientTransport(new URL(item.url));
26
+ }
27
+ else {
28
+ transport = item; // sse 或 自定义的 MCPTranport
29
+ }
30
+ return createMCPClient({ transport: transport });
31
+ }
32
+ catch (error) {
33
+ console.error(`Failed to create MCP client`, item, error);
34
+ return [];
35
+ }
36
+ })));
37
+ return allMcpClients;
38
+ });
39
+ /** 合并所有的Mcp Tools */
40
+ export const getMcpTools = (mcpClients) => __awaiter(void 0, void 0, void 0, function* () {
41
+ const tools = yield Promise.all(mcpClients.map((client) => { var _a; return (_a = client === null || client === void 0 ? void 0 : client.tools) === null || _a === void 0 ? void 0 : _a.call(client); }));
42
+ return tools.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {});
43
+ });