Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: not always open new window #31

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
32 changes: 20 additions & 12 deletions src/core/eko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WorkflowGenerator } from '../services/workflow/generator';
import { ClaudeProvider } from '../services/llm/claude-provider';
import { OpenaiProvider } from '../services/llm/openai-provider';
import {
EkoConfig,
LLMConfig,
EkoInvokeParam,
LLMProvider,
Tool,
Expand All @@ -11,6 +11,8 @@ import {
OpenaiConfig,
WorkflowCallback,
NodeOutput,
ExecutionContext,
EkoConfig,
} from '../types';
import { ToolRegistry } from './tool-registry';

Expand All @@ -23,30 +25,35 @@ export class Eko {
private llmProvider: LLMProvider;
private toolRegistry = new ToolRegistry();
private workflowGeneratorMap = new Map<Workflow, WorkflowGenerator>();
private ekoConfig: EkoConfig;

constructor(config: EkoConfig) {
if (typeof config == 'string') {
this.llmProvider = new ClaudeProvider(config);
} else if ('llm' in config) {
if (config.llm == 'claude') {
let claudeConfig = config as ClaudeConfig;
constructor(llmConfig: LLMConfig, ekoConfig?: EkoConfig) {
console.log("ekoConfig: " + ekoConfig);
this.ekoConfig = ekoConfig ? ekoConfig : {
alwaysOpenNewWindow: false,
};
if (typeof llmConfig == 'string') {
this.llmProvider = new ClaudeProvider(llmConfig);
} else if ('llm' in llmConfig) {
if (llmConfig.llm == 'claude') {
let claudeConfig = llmConfig as ClaudeConfig;
this.llmProvider = new ClaudeProvider(
claudeConfig.apiKey,
claudeConfig.modelName,
claudeConfig.options
);
} else if (config.llm == 'openai') {
let openaiConfig = config as OpenaiConfig;
} else if (llmConfig.llm == 'openai') {
let openaiConfig = llmConfig as OpenaiConfig;
this.llmProvider = new OpenaiProvider(
openaiConfig.apiKey,
openaiConfig.modelName,
openaiConfig.options
);
} else {
throw new Error('Unknown parameter: llm > ' + config['llm']);
throw new Error('Unknown parameter: llm > ' + llmConfig['llm']);
}
} else {
this.llmProvider = config as LLMProvider;
this.llmProvider = llmConfig as LLMProvider;
}
Eko.tools.forEach((tool) => this.toolRegistry.registerTool(tool));
}
Expand Down Expand Up @@ -132,8 +139,9 @@ export class Eko {
if (typeof tool === 'string') {
tool = this.getTool(tool);
}
let context = {
let context: ExecutionContext = {
llmProvider: this.llmProvider,
ekoConfig: this.ekoConfig,
variables: new Map<string, unknown>(),
tools: new Map<string, Tool<any, any>>(),
callback,
Expand Down
36 changes: 22 additions & 14 deletions src/extension/tools/export_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,32 @@ export class ExportFile implements Tool<ExportFileParam, unknown> {
} else {
filename = params.filename;
}
try {
let tabId = await getTabId(context);
let tabId = await getTabId(context);

const url = 'https://www.google.com';
const exportClosure = async (tabId: number) => {
await chrome.scripting.executeScript({
target: { tabId: tabId as number },
func: exportFile,
args: [filename, type, params.content],
});
} catch (e) {
let tab = await open_new_tab('https://www.google.com', true);
target: { tabId: tabId },
func: exportFile,
args: [filename, type, params.content],
});}
const openNewTabAndProcess = async (url: string, tabId: number) => {
let tab = await open_new_tab(url, true);
context.callback?.hooks?.onTabCreated?.(tab.id as number);
let tabId = tab.id as number;
await chrome.scripting.executeScript({
target: { tabId: tabId as number },
func: exportFile,
args: [filename, type, params.content],
});
tabId = tab.id as number;
await exportClosure(tabId);
await sleep(5000);
await chrome.tabs.remove(tabId);
};
console.log("context.ekoConfig: "+context.ekoConfig);
if (context.ekoConfig.alwaysOpenNewWindow) {
await openNewTabAndProcess(url, tabId);
} else {
try {
await exportClosure(tabId as number);
} catch (e) {
await openNewTabAndProcess(url, tabId);
}
}
return { success: true };
}
Expand Down
9 changes: 8 additions & 1 deletion src/extension/tools/web_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ export class WebSearch implements Tool<WebSearchParam, WebSearchResult[]> {
}
let taskId = new Date().getTime() + '';
let searchs = [{ url: url as string, keyword: query as string }];
let searchInfo = await deepSearch(context, taskId, searchs, maxResults || 5);
let searchInfo: any;
console.log("context.ekoConfig: "+context.ekoConfig);
if (context.ekoConfig.alwaysOpenNewWindow) {
searchInfo = await deepSearch(context, taskId, searchs, maxResults || 5);
} else {
let window = await chrome.windows.getCurrent();
searchInfo = await deepSearch(context, taskId, searchs, maxResults || 5, window);
}
let links = searchInfo.result[0]?.links || [];
return links.filter((s: any) => s.content) as WebSearchResult[];
}
Expand Down
1 change: 1 addition & 0 deletions src/models/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class WorkflowImpl implements Workflow {
__skip: false,
__abort: false,
workflow: this,
ekoConfig: {alwaysOpenNewWindow: false},
variables: this.variables,
llmProvider: this.llmProvider as LLMProvider,
tools: new Map(node.action.tools.map(tool => [tool.name, tool])),
Expand Down
5 changes: 5 additions & 0 deletions src/types/action.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ export interface Property {
properties?: Properties;
}

export interface EkoConfig {
alwaysOpenNewWindow: boolean;
}

export interface ExecutionContext {
llmProvider: LLMProvider;
ekoConfig: EkoConfig;
variables: Map<string, unknown>;
workflow?: Workflow;
tools?: Map<string, Tool<any, any>>;
Expand Down
2 changes: 1 addition & 1 deletion src/types/eko.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface OpenaiConfig {

export type ClaudeApiKey = string;

export type EkoConfig = ClaudeApiKey | ClaudeConfig | OpenaiConfig | LLMProvider;
export type LLMConfig = ClaudeApiKey | ClaudeConfig | OpenaiConfig | LLMProvider;

export interface EkoInvokeParam {
tools?: Array<string> | Array<Tool<any, any>>;
Expand Down
2 changes: 0 additions & 2 deletions src/types/llm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export interface ToolDefinition {
};
}

export interface LLMConfig {}

export interface ToolCall {
id: string;
name: string;
Expand Down