From c793cb9bbce5519d930a1e292e84606007a1066f Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sun, 2 Feb 2025 20:53:28 +0800 Subject: [PATCH] refactor: consolidate type definitions into types.ts (#5) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Han Xiao --- src/agent.ts | 25 +-------- src/tools/brave-search.ts | 12 +--- src/tools/dedup.ts | 7 +-- src/tools/error-analyzer.ts | 10 +--- src/tools/evaluator.ts | 7 +-- src/tools/getURLIndex.ts | 22 +------- src/tools/query-rewriter.ts | 7 +-- src/tools/read.ts | 12 +--- src/tools/search.ts | 12 +--- src/types.ts | 108 +++++++++++++++++++++++++++++++++++- src/utils/token-tracker.ts | 5 +- 11 files changed, 125 insertions(+), 102 deletions(-) diff --git a/src/agent.ts b/src/agent.ts index fdec4d4..66b4645 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -9,7 +9,7 @@ import {evaluateAnswer} from "./tools/evaluator"; import {analyzeSteps} from "./tools/error-analyzer"; import {GEMINI_API_KEY, JINA_API_KEY, MODEL_NAME, SEARCH_PROVIDER, STEP_SLEEP} from "./config"; import {tokenTracker} from "./utils/token-tracker"; -import {StepAction} from "./types"; +import {StepAction, SchemaProperty, ResponseSchema} from "./types"; async function sleep(ms: number) { const seconds = Math.ceil(ms / 1000); @@ -17,27 +17,6 @@ async function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } -type SchemaProperty = { - type: SchemaType; - description: string; - enum?: string[]; - items?: { - type: SchemaType; - description?: string; - properties?: Record; - required?: string[]; - }; - properties?: Record; - required?: string[]; - maxItems?: number; -}; - -type ResponseSchema = { - type: SchemaType; - properties: Record; - required: string[]; -}; - function getSchema(allowReflect: boolean, allowRead: boolean, allowAnswer: boolean, allowSearch: boolean): ResponseSchema { const actions: string[] = []; const properties: Record = { @@ -672,4 +651,4 @@ export async function main() { if (require.main === module) { main().catch(console.error); -} \ No newline at end of file +} diff --git a/src/tools/brave-search.ts b/src/tools/brave-search.ts index d027b39..d605e09 100644 --- a/src/tools/brave-search.ts +++ b/src/tools/brave-search.ts @@ -1,15 +1,7 @@ import axios from 'axios'; import {BRAVE_API_KEY} from "../config"; -interface BraveSearchResponse { - web: { - results: Array<{ - title: string; - description: string; - url: string; - }>; - }; -} +import { BraveSearchResponse } from '../types'; export async function braveSearch(query: string): Promise<{ response: BraveSearchResponse }> { const response = await axios.get('https://api.search.brave.com/res/v1/web/search', { @@ -27,4 +19,4 @@ export async function braveSearch(query: string): Promise<{ response: BraveSearc // Maintain the same return structure as the original code return { response: response.data }; -} \ No newline at end of file +} diff --git a/src/tools/dedup.ts b/src/tools/dedup.ts index e1655c5..42926e4 100644 --- a/src/tools/dedup.ts +++ b/src/tools/dedup.ts @@ -2,10 +2,7 @@ import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai"; import { GEMINI_API_KEY, MODEL_NAME } from "../config"; import { tokenTracker } from "../utils/token-tracker"; -type DedupResponse = { - thought: string; - unique_queries: string[]; -}; +import { DedupResponse } from '../types'; const responseSchema = { type: SchemaType.OBJECT, @@ -143,4 +140,4 @@ export async function main() { if (require.main === module) { main().catch(console.error); -} \ No newline at end of file +} diff --git a/src/tools/error-analyzer.ts b/src/tools/error-analyzer.ts index bcefaf2..b2fa168 100644 --- a/src/tools/error-analyzer.ts +++ b/src/tools/error-analyzer.ts @@ -2,11 +2,7 @@ import {GoogleGenerativeAI, SchemaType} from "@google/generative-ai"; import { GEMINI_API_KEY, MODEL_NAME } from "../config"; import { tokenTracker } from "../utils/token-tracker"; -type EvaluationResponse = { - recap: string; - blame: string; - improvement: string; -}; +import { ErrorAnalysisResponse } from '../types'; const responseSchema = { type: SchemaType.OBJECT, @@ -117,13 +113,13 @@ ${diaryContext.join('\n')} `; } -export async function analyzeSteps(diaryContext: string[]): Promise<{ response: EvaluationResponse, tokens: number }> { +export async function analyzeSteps(diaryContext: string[]): Promise<{ response: ErrorAnalysisResponse, tokens: number }> { try { const prompt = getPrompt(diaryContext); const result = await model.generateContent(prompt); const response = await result.response; const usage = response.usageMetadata; - const json = JSON.parse(response.text()) as EvaluationResponse; + const json = JSON.parse(response.text()) as ErrorAnalysisResponse; console.log('Error analysis:', { is_valid: !json.blame, reason: json.blame || 'No issues found' diff --git a/src/tools/evaluator.ts b/src/tools/evaluator.ts index 5f51637..598a4f7 100644 --- a/src/tools/evaluator.ts +++ b/src/tools/evaluator.ts @@ -2,10 +2,7 @@ import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai"; import { GEMINI_API_KEY, MODEL_NAME } from "../config"; import { tokenTracker } from "../utils/token-tracker"; -type EvaluationResponse = { - is_definitive: boolean; - reasoning: string; -}; +import { EvaluationResponse } from '../types'; const responseSchema = { type: SchemaType.OBJECT, @@ -105,4 +102,4 @@ async function main() { if (require.main === module) { main().catch(console.error); -} \ No newline at end of file +} diff --git a/src/tools/getURLIndex.ts b/src/tools/getURLIndex.ts index 4e8a933..8a0da3b 100644 --- a/src/tools/getURLIndex.ts +++ b/src/tools/getURLIndex.ts @@ -1,24 +1,6 @@ -interface SearchResult { - title: string; - url: string; - description: string; -} +import { SearchResult, QueryResult, StepData } from '../types'; -interface QueryResult { - query: string; - results: SearchResult[]; -} - -export interface StepData { - step: number; - question: string; - action: string; - reasoning: string; - searchQuery?: string; - result?: QueryResult[]; -} - -export function buildURLMap(data: StepData[]): Record { +export function buildURLMap(data: StepData[]): Record { const urlMap: Record = {}; data.forEach(step => { diff --git a/src/tools/query-rewriter.ts b/src/tools/query-rewriter.ts index c452b1e..f5f7781 100644 --- a/src/tools/query-rewriter.ts +++ b/src/tools/query-rewriter.ts @@ -3,10 +3,7 @@ import { GEMINI_API_KEY, MODEL_NAME } from "../config"; import { tokenTracker } from "../utils/token-tracker"; import { SearchAction } from "../types"; -type KeywordsResponse = { - thought: string; - queries: string[]; -}; +import { KeywordsResponse } from '../types'; const responseSchema = { type: SchemaType.OBJECT, @@ -125,4 +122,4 @@ export async function rewriteQuery(action: SearchAction): Promise<{ queries: str console.error('Error in query rewriting:', error); throw error; } -} \ No newline at end of file +} diff --git a/src/tools/read.ts b/src/tools/read.ts index 4ea27cd..28b4dbf 100644 --- a/src/tools/read.ts +++ b/src/tools/read.ts @@ -1,17 +1,7 @@ import https from 'https'; import { tokenTracker } from "../utils/token-tracker"; -interface ReadResponse { - code: number; - status: number; - data: { - title: string; - description: string; - url: string; - content: string; - usage: { tokens: number; }; - }; -} +import { ReadResponse } from '../types'; export function readUrl(url: string, token: string): Promise<{ response: ReadResponse, tokens: number }> { return new Promise((resolve, reject) => { diff --git a/src/tools/search.ts b/src/tools/search.ts index f92f06d..ab3b486 100644 --- a/src/tools/search.ts +++ b/src/tools/search.ts @@ -1,17 +1,7 @@ import https from 'https'; import { tokenTracker } from "../utils/token-tracker"; -interface SearchResponse { - code: number; - status: number; - data: Array<{ - title: string; - description: string; - url: string; - content: string; - usage: { tokens: number; }; - }>; -} +import { SearchResponse } from '../types'; export function search(query: string, token: string): Promise<{ response: SearchResponse, tokens: number }> { return new Promise((resolve, reject) => { diff --git a/src/types.ts b/src/types.ts index 1f59173..5c34bff 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,6 @@ +import { SchemaType } from "@google/generative-ai"; + +// Action Types type BaseAction = { action: "search" | "answer" | "reflect" | "visit"; thoughts: string; @@ -27,4 +30,107 @@ export type VisitAction = BaseAction & { URLTargets: string[]; }; -export type StepAction = SearchAction | AnswerAction | ReflectAction | VisitAction; \ No newline at end of file +export type StepAction = SearchAction | AnswerAction | ReflectAction | VisitAction; + +// Response Types +export interface TokenUsage { + tool: string; + tokens: number; +} + +export interface SearchResponse { + code: number; + status: number; + data: Array<{ + title: string; + description: string; + url: string; + content: string; + usage: { tokens: number; }; + }>; +} + +export interface BraveSearchResponse { + web: { + results: Array<{ + title: string; + description: string; + url: string; + }>; + }; +} + +export type DedupResponse = { + thought: string; + unique_queries: string[]; +}; + +export interface ReadResponse { + code: number; + status: number; + data: { + title: string; + description: string; + url: string; + content: string; + usage: { tokens: number; }; + }; +} + +export type EvaluationResponse = { + is_definitive: boolean; + reasoning: string; +}; + +export type ErrorAnalysisResponse = { + recap: string; + blame: string; + improvement: string; +}; + +export interface SearchResult { + title: string; + url: string; + description: string; +} + +export interface QueryResult { + query: string; + results: SearchResult[]; +} + +export interface StepData { + step: number; + question: string; + action: string; + reasoning: string; + searchQuery?: string; + result?: QueryResult[]; +} + +export type KeywordsResponse = { + thought: string; + queries: string[]; +}; + +// Schema Types +export type SchemaProperty = { + type: SchemaType; + description: string; + enum?: string[]; + items?: { + type: SchemaType; + description?: string; + properties?: Record; + required?: string[]; + }; + properties?: Record; + required?: string[]; + maxItems?: number; +}; + +export type ResponseSchema = { + type: SchemaType; + properties: Record; + required: string[]; +}; diff --git a/src/utils/token-tracker.ts b/src/utils/token-tracker.ts index c6bf77c..944ba09 100644 --- a/src/utils/token-tracker.ts +++ b/src/utils/token-tracker.ts @@ -1,9 +1,6 @@ import { EventEmitter } from 'events'; -interface TokenUsage { - tool: string; - tokens: number; -} +import { TokenUsage } from '../types'; class TokenTracker extends EventEmitter { private usages: TokenUsage[] = [];