gadget/packages/ai-toolbox/src/toolbox.ts

84 lines
2.0 KiB
TypeScript

// src/toolbox.ts
// Copyright (C) 2026 Rob Colbert <rob.colbert@openplatform.us>
// Licensed under the Apache License, Version 2.0
import type { IAiTool } from "@gadget/ai";
import type { IProject, ChatSessionMode } from "@gadget/api";
export interface GadgetToolboxEnvironment {
NODE_ENV: string;
services?: {
google?: {
cse?: {
apiKey?: string;
engineId?: string;
};
};
};
workspace?: {
workspaceDir?: string;
projectDir?: string;
cacheDir?: string;
};
project?: IProject;
chatSessionMode?: ChatSessionMode;
}
export type ToolMap = Map<string, IAiTool>;
export type ToolSet = Set<IAiTool>;
export class AiToolbox {
private _env: GadgetToolboxEnvironment;
private tools: ToolMap = new Map<string, IAiTool>();
private modeSets: Map<string, ToolSet> = new Map<string, Set<IAiTool>>();
constructor(env: GadgetToolboxEnvironment) {
this._env = env;
}
get env(): GadgetToolboxEnvironment {
return this._env;
}
updateWorkspace(workspace: NonNullable<GadgetToolboxEnvironment["workspace"]>): void {
this._env.workspace = workspace;
}
updateProjectContext(project: IProject, mode: ChatSessionMode): void {
this._env.project = project;
this._env.chatSessionMode = mode;
}
register(tool: IAiTool, modes?: string[]): void {
if (this.tools.has(tool.name)) {
throw new Error(`tool already registered: ${tool.name}`);
}
this.tools.set(tool.name, tool);
if (!modes) {
return;
}
for (const mode of modes) {
let set = this.modeSets.get(mode);
if (!set) {
set = new Set<IAiTool>();
this.modeSets.set(mode, set);
}
set.add(tool);
}
}
getTool(name: string): IAiTool | undefined {
return this.tools.get(name);
}
getModeSet(mode: string): ToolSet | undefined {
return this.modeSets.get(mode);
}
getToolNamesForMode(mode: string): string[] {
return Array.from(this.getModeSet(mode) || []).map((tool) => tool.name);
}
}