@@ -53,6 +53,9 @@ import { fal } from "@fal-ai/client";
5353
5454import BigNumber from "bignumber.js" ;
5555import { createPublicClient , http } from "viem" ;
56+ import fs from "fs" ;
57+ import os from "os" ;
58+ import path from "path" ;
5659
5760type Tool = CoreTool < any , any > ;
5861type StepResult = AIStepResult < any > ;
@@ -1734,6 +1737,15 @@ export const generateImage = async (
17341737 return runtime . getSetting ( "LIVEPEER_GATEWAY_URL" ) ;
17351738 case ModelProviderName . SECRETAI :
17361739 return runtime . getSetting ( "SECRET_AI_API_KEY" ) ;
1740+ case ModelProviderName . NEARAI :
1741+ try {
1742+ // Read auth config from ~/.nearai/config.json if it exists
1743+ const config = JSON . parse ( fs . readFileSync ( path . join ( os . homedir ( ) , '.nearai/config.json' ) , 'utf8' ) ) ;
1744+ return JSON . stringify ( config ?. auth ) ;
1745+ } catch ( e ) {
1746+ elizaLogger . warn ( `Error loading NEAR AI config. The environment variable NEARAI_API_KEY will be used. ${ e } ` ) ;
1747+ }
1748+ return runtime . getSetting ( "NEARAI_API_KEY" ) ;
17371749 default :
17381750 // If no specific match, try the fallback chain
17391751 return (
@@ -2279,6 +2291,8 @@ export async function handleProvider(
22792291 return await handleLivepeer ( options ) ;
22802292 case ModelProviderName . SECRETAI :
22812293 return await handleSecretAi ( options ) ;
2294+ case ModelProviderName . NEARAI :
2295+ return await handleNearAi ( options ) ;
22822296 default : {
22832297 const errorMessage = `Unsupported provider: ${ provider } ` ;
22842298 elizaLogger . error ( errorMessage ) ;
@@ -2661,6 +2675,34 @@ async function handleSecretAi({
26612675 } ) ;
26622676}
26632677
2678+ /**
2679+ * Handles object generation for NEAR AI models.
2680+ *
2681+ * @param {ProviderOptions } options - Options specific to NEAR AI.
2682+ * @returns {Promise<GenerateObjectResult<unknown>> } - A promise that resolves to generated objects.
2683+ */
2684+ async function handleNearAi ( {
2685+ model,
2686+ apiKey,
2687+ schema,
2688+ schemaName,
2689+ schemaDescription,
2690+ mode = "json" ,
2691+ modelOptions,
2692+ } : ProviderOptions ) : Promise < GenerateObjectResult < unknown > > {
2693+ const nearai = createOpenAI ( { apiKey, baseURL : models . nearai . endpoint } ) ;
2694+ // Require structured output if schema is provided
2695+ const settings = schema ? { structuredOutputs : true } : undefined ;
2696+ return await aiGenerateObject ( {
2697+ model : nearai . languageModel ( model , settings ) ,
2698+ schema,
2699+ schemaName,
2700+ schemaDescription,
2701+ mode,
2702+ ...modelOptions ,
2703+ } ) ;
2704+ }
2705+
26642706// Add type definition for Together AI response
26652707interface TogetherAIImageResponse {
26662708 data : Array < {
0 commit comments