# 리서치 에이전트 커스텀 노드 함수

## 1. Tool - Web Search

### Input Variables

* $args = {{$flow\.state.arguments}} : `LLM` 노드에서 생성하는 Output 중 \<tool\_call>\</tool\_call> 안의 툴 사용 arguments 를 state 에 저장해두고 사용합니다. (아래 모든 Tool 이 동일하게 사용합니다)

```javascript
const fetch = require('node-fetch');
const querystring = require('querystring');

const args = JSON.parse($args);

const search_query = args.search_query;
const num_results = args.num_results;

const apiKey = 'YOUR_API_KEY_HERE';

const searchQuery = async (queryObj) => {
  const { q, recency, domains } = queryObj;
  const url = `https://www.searchapi.io/api/v1/search`;

  let days = 0;
  if (recency) {
    days = parseInt(recency, 10);
  }

  let query = null;
  let domainStr = '';
  if (domains) {
    domainStr = domains.map(domain => `site:${domain.trim()}`).join(' OR ');
    query = `${q} ${domainStr}`;
  } else {
    query = q;
  }

  const params = {
    engine: 'google',
    api_key: apiKey,
    q: query
  };

  if (num_results) {
    params.num = num_results
  } else {
    params.num = 5
  }

  if (days > 0) {
    params.tbs = `qdr:d${days}`;
  }

  try {
    const response = await fetch(`${url}?${querystring.stringify(params)}`);

    if (!response.ok) {
      throw new Error(`API 요청 실패: ${response.statusText}`);
    }

    const data = await response.json();
    const results = data.organic_results || [];

    const search_summary = results.map(item => ({
      title: item.title || null,
      url: item.link || null,
      snippet: item.snippet || null,
      source: item.source || null,
      date: item.date || null
    }));

    return search_summary;
  } catch (error) {
    return `Error: ${error.message}`;
  }
};

return (async () => {
  const results = await Promise.all(search_query.map(searchQuery));
  const flatResults = results.flat();
  const updatedList = flatResults.map((d, idx) => ({
    id: idx,
    ...d
}));
  const resultString = updatedList.map(item => 
    item.date
      ? `- ${item.title} (${item.url}): ${item.date} — ${item.snippet} 【search${item.id}】`
      : `- ${item.title} (${item.url}): ${item.snippet} 【search${item.id}】`
  ).join('\n');

  return resultString;
})();

```

## 2. Tool - Open URL

* Open URL Tool 의 경우는 추가 패키지가 필요하기 때문에 MCP Tool 로 구현하여 사용했습니다.
* GenOS 메뉴 중 `배포 > 도구 > MCP 서버 & MCP 도구` 메뉴에서 생성하고, `Tool` 노드에서 해당 MCP 도구를 선택하여 사용합니다.
* 아래는 LLM 이 생성한 argument 를 파싱하는 코드입니다.

```javascript
try {
    // 안전한 JSON 파싱
    let args;
    if (typeof $args === 'string') {
        args = JSON.parse($args);
    } else {
        args = $args;
    }
    
    console.log('파싱된 args:', args);
    
    const opens_list = args.opens || [];
    
    console.log('추출된 opens_list:', opens_list);
    
    return opens_list;
    
} catch (error) {
    console.error('JSON 파싱 오류:', error.message);
    console.error('입력값:', $args);
    
    // 오류 발생시 빈 배열 반환
    return [];
}
```

## 3. Tool - External APIs

* 다른 API 도 아래와 동일하게 사용하시면 됩니다.
* 예시는 DeepSearch API (기업, 금융 데이터)

```javascript
const fetch = require('node-fetch');

const args = JSON.parse($args);

const symbols = args.symbols;
const base_url = "https://api-v2.deepsearch.com/v2/companies";
const headers = {
  "Authorization": "Bearer YOUR_API_KEY_HERE",
  "Content-Type": "application/json"
};

const fetchSymbol = async (symbol) => {
  try {
    const response = await fetch(
      `${base_url}/${symbol}`,
      {headers: headers}
    );

    if (!response.ok) {
      throw new Error(`API 요청 실패: ${response.statusText}`);
    }

    const result = await response.json();
    const data = result.data || {};
    
    return data;
  } catch (error) {
    return `Error: ${error.message}`
  }
}

return (async() => {
  const results = await Promise.all(symbols.map(fetchSymbol));
  return results;
})();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://genos-docs.gitbook.io/default/v1.7.5.1/advanced-tutorials/guides/workflow/research-agent/function-research-agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
