forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathlanguage-model.ts
More file actions
141 lines (124 loc) · 4.47 KB
/
language-model.ts
File metadata and controls
141 lines (124 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* MCP language model adapter for the AI SDK
*/
import type { LanguageModelV1, LanguageModelV1CallOptions } from 'ai';
import { MCPClient } from '../client';
import { MCPCapabilityManager } from '../context/manager';
import { MCPError, MCPErrorType } from '../client/types';
/**
* Options for creating an MCP language model adapter
*/
export interface MCPLanguageModelOptions {
/**
* MCP client
*/
client: MCPClient;
/**
* Capability manager
*/
capabilityManager?: MCPCapabilityManager;
/**
* Model name
*/
model?: string;
}
/**
* Create an MCP language model adapter
*
* @param options Options for creating the adapter
* @returns Language model adapter
*/
export function createMCPLanguageModel(options: MCPLanguageModelOptions): LanguageModelV1 {
const { client, capabilityManager, model } = options;
// Create a capability manager if not provided
const manager = capabilityManager || new MCPCapabilityManager(client);
return {
specificationVersion: 'v1',
provider: 'mcp',
modelId: `mcp-${model || 'default'}`,
defaultObjectGenerationMode: undefined,
doStream: async (options: LanguageModelV1CallOptions) => {
throw new Error('Streaming is not supported by the MCP adapter yet');
},
doGenerate: async (options: LanguageModelV1CallOptions) => {
try {
// Refresh capabilities if needed
if (!manager.getCapabilities().tools.length) {
await manager.refreshAll();
}
// Find a suitable tool for text generation
const tools = manager.getCapabilities().tools;
const generateTool = tools.find(
(tool) => tool.name === 'generate' || tool.name.includes('generate'),
);
if (!generateTool) {
throw new MCPError(
'No text generation tool found',
MCPErrorType.TOOL_CALL_ERROR,
);
}
// Extract parameters from options
const { maxTokens, temperature, topP, stopSequences } = options;
// Prepare messages for the tool call
const messages = [];
// Add user content if available
if (options.prompt) {
messages.push({ role: 'user', content: options.prompt });
}
// Call the tool with the messages
const result = await client.callTool(generateTool.name, {
messages,
maxTokens,
temperature,
topP,
stopSequences,
});
// Extract the text from the result
const text = result.content.map((item) => item.text).join('');
// Prepare the request body for rawCall
const requestBody = {
messages,
maxTokens,
temperature,
topP,
stopSequences,
};
return {
text,
toolCalls: undefined,
finishReason: 'stop',
usage: {
promptTokens: 0,
completionTokens: 0,
},
rawCall: {
rawPrompt: messages,
rawSettings: {
maxTokens,
temperature,
topP,
stopSequences,
},
},
request: {
body: JSON.stringify(requestBody),
},
response: {
id: `mcp-response-${Date.now()}`,
timestamp: new Date(),
modelId: `mcp-${model || 'default'}`,
},
};
} catch (error) {
if (error instanceof MCPError) {
throw error;
}
throw new MCPError(
`Failed to generate text: ${error instanceof Error ? error.message : String(error)}`,
MCPErrorType.TOOL_CALL_ERROR,
error,
);
}
},
};
}