in src/lib/utils/snippets.ts [208:294]
export function modifySnippet(snippet: string, newProperties: Record<string, unknown>): string {
// JS: HuggingFace InferenceClient (streaming)
if (snippet.includes("client.chatCompletionStream")) {
return insertPropertiesInternal(
snippet,
newProperties,
/client\.chatCompletionStream\s*\(\s*/, // Finds "client.chatCompletionStream("
"{", // The parameters are in an object literal
"}",
(key, value, indent) => `${indent}${key}: ${value},\n`, // JS object literal style
formatJsJsonValue
);
}
// JS: HuggingFace InferenceClient (non-streaming)
else if (snippet.includes("client.chatCompletion") && snippet.includes("InferenceClient")) {
// Ensure it's not the OpenAI client by also checking for InferenceClient
return insertPropertiesInternal(
snippet,
newProperties,
/client\.chatCompletion\s*\(\s*/, // Finds "client.chatCompletion("
"{", // The parameters are in an object literal
"}",
(key, value, indent) => `${indent}${key}: ${value},\n`, // JS object literal style
formatJsJsonValue
);
}
// JS: OpenAI Client
// Check for client.chat.completions.create and a common JS import pattern
else if (
snippet.includes("client.chat.completions.create") &&
(snippet.includes('import { OpenAI } from "openai"') || snippet.includes("new OpenAI("))
) {
return insertPropertiesInternal(
snippet,
newProperties,
/client\.chat\.completions\.create\s*\(\s*/, // Finds "client.chat.completions.create("
"{", // The parameters are in an object literal
"}",
(key, value, indent) => `${indent}${key}: ${value},\n`,
formatJsJsonValue
);
}
// Python: OpenAI or HuggingFace Client using client.chat.completions.create
else if (snippet.includes("client.chat.completions.create")) {
return insertPropertiesInternal(
snippet,
newProperties,
/client\.chat\.completions\.create\s*\(/, // Finds "client.chat.completions.create("
"(", // The parameters are directly in the function call tuple
")",
(key, value, indent) => {
const snakeKey = key.replace(/([A-Z])/g, "_$1").toLowerCase();
return `${indent}${snakeKey}=${value},\n`;
},
formatPythonValue
);
}
// Python: requests example with query({...})
else if (snippet.includes("def query(payload):") && snippet.includes("query({")) {
return insertPropertiesInternal(
snippet,
newProperties,
/query\s*\(\s*/, // Finds "query(" and expects a dictionary literal next
"{", // The parameters are in a dictionary literal
"}",
// Python dict keys are strings, values formatted for Python
(key, formattedValue, indent) => `${indent}"${key}": ${formattedValue},\n`,
formatPythonValue // Use formatPythonValue for the values themselves
);
}
// Shell/curl (JSON content)
else if (snippet.includes("curl") && snippet.includes("-d")) {
return insertPropertiesInternal(
snippet,
newProperties,
/-d\s*'(?:\\n)?\s*/,
"{",
"}",
(key, value, indent) => {
const snakeKey = key.replace(/([A-Z])/g, "_$1").toLowerCase();
return `${indent}"${snakeKey}": ${value},\n`;
},
formatJsJsonValue
);
}
return snippet;
}