|
4 | 4 | parseMarkdownCodeBlock, |
5 | 5 | } from 'common/util/file' |
6 | 6 |
|
7 | | -import { CoreMessage } from 'ai' |
8 | | -import { toolSchema } from 'common/constants/tools' |
9 | 7 | import { env } from '../env.mjs' |
10 | 8 | import { saveMessage } from '../llm-apis/message-cost-tracker' |
11 | 9 | import { logger } from '../util/logger' |
@@ -242,141 +240,3 @@ export async function rerank( |
242 | 240 | return files.map((f) => f.path) |
243 | 241 | } |
244 | 242 | } |
245 | | - |
246 | | -const toolParams = Object.entries(toolSchema) |
247 | | - .map(([name, params]) => { |
248 | | - return `\`${name}\`: [${params |
249 | | - .map((p) => (typeof p === 'string' ? JSON.stringify(p) : `${p}`)) |
250 | | - .join(', ')}]` |
251 | | - }) |
252 | | - .join('\n') |
253 | | - |
254 | | -const outputSchema = { |
255 | | - type: 'object', |
256 | | - properties: { |
257 | | - tool_calls: { |
258 | | - type: 'array', |
259 | | - items: { |
260 | | - type: 'object', |
261 | | - properties: { |
262 | | - name: { type: 'string' }, |
263 | | - args: { type: 'object' }, |
264 | | - }, |
265 | | - required: ['name', 'args'], |
266 | | - }, |
267 | | - }, |
268 | | - }, |
269 | | -} |
270 | | - |
271 | | -const systemInstructions = ` |
272 | | -You are a helpful assistant that structures potentially malformed XML text into tool calls. |
273 | | -
|
274 | | -Below are the available tools and their parameters. Parameters are either "String" or /RegExp/ values. |
275 | | -${toolParams} |
276 | | -
|
277 | | -Some tools will have multi-line parameters (frequently write_file). Do not forget to add these to the tool_calls. |
278 | | -
|
279 | | -None of the parameters are required. Do not infer any additional tools or parameters beyond those explicitly listed in the user message. |
280 | | -
|
281 | | -Output according to this schema: |
282 | | -${JSON.stringify(outputSchema, null, 2)} |
283 | | -`.trim() |
284 | | - |
285 | | -export async function toolFormatter( |
286 | | - assistantMessage: string, |
287 | | - options: { |
288 | | - clientSessionId: string |
289 | | - fingerprintId: string |
290 | | - userInputId: string |
291 | | - userId: string | undefined |
292 | | - messageId: string |
293 | | - } |
294 | | -) { |
295 | | - const { clientSessionId, fingerprintId, userInputId, userId, messageId } = |
296 | | - options |
297 | | - const startTime = Date.now() |
298 | | - |
299 | | - const messages: CoreMessage[] = [ |
300 | | - { role: 'system', content: systemInstructions }, |
301 | | - { role: 'user', content: assistantMessage }, |
302 | | - ] |
303 | | - |
304 | | - try { |
305 | | - const response = (await Promise.race([ |
306 | | - fetch('https://tool-formatter.endpoint.relace.run/v1/code/format', { |
307 | | - method: 'POST', |
308 | | - headers: { |
309 | | - 'Content-Type': 'application/json', |
310 | | - Authorization: `Bearer ${env.RELACE_API_KEY}`, |
311 | | - }, |
312 | | - body: JSON.stringify({ |
313 | | - model: 'Tool-Formatter', |
314 | | - messages, |
315 | | - temperature: 0, |
316 | | - max_tokens: 8192, |
317 | | - response_format: { |
318 | | - type: 'json_schema', |
319 | | - json_schema: { |
320 | | - name: 'tool_calls', |
321 | | - schema: outputSchema, |
322 | | - }, |
323 | | - }, |
324 | | - 'relace-metadata': { |
325 | | - 'codebuff-id': messageId, |
326 | | - }, |
327 | | - }), |
328 | | - }), |
329 | | - timeoutPromise(100_000), |
330 | | - ])) as Response |
331 | | - |
332 | | - if (!response.ok) { |
333 | | - const errorText = await response |
334 | | - .text() |
335 | | - .catch(() => 'No error text available') |
336 | | - logger.error( |
337 | | - { |
338 | | - status: response.status, |
339 | | - statusText: response.statusText, |
340 | | - errorText, |
341 | | - assistantMessageLength: assistantMessage.length, |
342 | | - messageId, |
343 | | - }, |
344 | | - 'Relace Tool Formatter API error' |
345 | | - ) |
346 | | - return |
347 | | - } |
348 | | - |
349 | | - const json = await response.json() |
350 | | - const toolCalls = JSON.parse(json.choices[0].message.content) |
351 | | - |
352 | | - saveMessage({ |
353 | | - messageId, |
354 | | - userId, |
355 | | - clientSessionId, |
356 | | - fingerprintId, |
357 | | - userInputId, |
358 | | - model: 'relace-tool-formatter', |
359 | | - request: messages, |
360 | | - response: toolCalls, |
361 | | - inputTokens: countTokens(systemInstructions + assistantMessage), |
362 | | - outputTokens: countTokens(JSON.stringify(toolCalls)), |
363 | | - finishedAt: new Date(), |
364 | | - latencyMs: Date.now() - startTime, |
365 | | - chargeUser: false, |
366 | | - }) |
367 | | - |
368 | | - logger.info({ toolCalls }, 'Relace Tool Formatter') |
369 | | - } catch (error) { |
370 | | - logger.error( |
371 | | - { |
372 | | - error: |
373 | | - error && typeof error === 'object' && 'message' in error |
374 | | - ? error.message |
375 | | - : 'Unknown error', |
376 | | - }, |
377 | | - 'Error calling Relace Tool Formatter API' |
378 | | - ) |
379 | | - // Silent failure as specified - no fallback needed |
380 | | - return |
381 | | - } |
382 | | -} |
0 commit comments