@vibe-kit/grok-cli
Version:
An open-source AI agent that brings the power of Grok directly into your terminal.
83 lines • 2.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GrokClient = void 0;
const openai_1 = __importDefault(require("openai"));
class GrokClient {
constructor(apiKey, model, baseURL) {
this.currentModel = "grok-3-latest";
this.client = new openai_1.default({
apiKey,
baseURL: baseURL || process.env.GROK_BASE_URL || "https://api.x.ai/v1",
timeout: 360000,
});
if (model) {
this.currentModel = model;
}
}
setModel(model) {
this.currentModel = model;
}
getCurrentModel() {
return this.currentModel;
}
async chat(messages, tools, model, searchOptions) {
try {
const requestPayload = {
model: model || this.currentModel,
messages,
tools: tools || [],
tool_choice: tools && tools.length > 0 ? "auto" : undefined,
temperature: 0.7,
max_tokens: 4000,
};
// Add search parameters if specified
if (searchOptions?.search_parameters) {
requestPayload.search_parameters = searchOptions.search_parameters;
}
const response = await this.client.chat.completions.create(requestPayload);
return response;
}
catch (error) {
throw new Error(`Grok API error: ${error.message}`);
}
}
async *chatStream(messages, tools, model, searchOptions) {
try {
const requestPayload = {
model: model || this.currentModel,
messages,
tools: tools || [],
tool_choice: tools && tools.length > 0 ? "auto" : undefined,
temperature: 0.7,
max_tokens: 4000,
stream: true,
};
// Add search parameters if specified
if (searchOptions?.search_parameters) {
requestPayload.search_parameters = searchOptions.search_parameters;
}
const stream = (await this.client.chat.completions.create(requestPayload));
for await (const chunk of stream) {
yield chunk;
}
}
catch (error) {
throw new Error(`Grok API error: ${error.message}`);
}
}
async search(query, searchParameters) {
const searchMessage = {
role: "user",
content: query,
};
const searchOptions = {
search_parameters: searchParameters || { mode: "on" },
};
return this.chat([searchMessage], [], undefined, searchOptions);
}
}
exports.GrokClient = GrokClient;
//# sourceMappingURL=client.js.map