@ugm/desiagent 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +251 -0
- package/dist/__tests__/test-utils.d.ts +24 -0
- package/dist/__tests__/test-utils.d.ts.map +1 -0
- package/dist/__tests__/test-utils.js +32 -0
- package/dist/__tests__/test-utils.js.map +1 -0
- package/dist/core/execution/agents.d.ts +60 -0
- package/dist/core/execution/agents.d.ts.map +1 -0
- package/dist/core/execution/agents.js +249 -0
- package/dist/core/execution/agents.js.map +1 -0
- package/dist/core/execution/artifacts.d.ts +27 -0
- package/dist/core/execution/artifacts.d.ts.map +1 -0
- package/dist/core/execution/artifacts.js +93 -0
- package/dist/core/execution/artifacts.js.map +1 -0
- package/dist/core/execution/costs.d.ts +160 -0
- package/dist/core/execution/costs.d.ts.map +1 -0
- package/dist/core/execution/costs.js +196 -0
- package/dist/core/execution/costs.js.map +1 -0
- package/dist/core/execution/dagExecutor.d.ts +112 -0
- package/dist/core/execution/dagExecutor.d.ts.map +1 -0
- package/dist/core/execution/dagExecutor.js +647 -0
- package/dist/core/execution/dagExecutor.js.map +1 -0
- package/dist/core/execution/dags.d.ts +167 -0
- package/dist/core/execution/dags.d.ts.map +1 -0
- package/dist/core/execution/dags.js +713 -0
- package/dist/core/execution/dags.js.map +1 -0
- package/dist/core/execution/executions.d.ts +158 -0
- package/dist/core/execution/executions.d.ts.map +1 -0
- package/dist/core/execution/executions.js +258 -0
- package/dist/core/execution/executions.js.map +1 -0
- package/dist/core/execution/goals.d.ts +62 -0
- package/dist/core/execution/goals.d.ts.map +1 -0
- package/dist/core/execution/goals.js +245 -0
- package/dist/core/execution/goals.js.map +1 -0
- package/dist/core/execution/runs.d.ts +65 -0
- package/dist/core/execution/runs.d.ts.map +1 -0
- package/dist/core/execution/runs.js +219 -0
- package/dist/core/execution/runs.js.map +1 -0
- package/dist/core/execution/tools.d.ts +24 -0
- package/dist/core/execution/tools.d.ts.map +1 -0
- package/dist/core/execution/tools.js +33 -0
- package/dist/core/execution/tools.js.map +1 -0
- package/dist/core/orchestration/index.d.ts +7 -0
- package/dist/core/orchestration/index.d.ts.map +1 -0
- package/dist/core/orchestration/index.js +7 -0
- package/dist/core/orchestration/index.js.map +1 -0
- package/dist/core/orchestration/orchestrator.d.ts +39 -0
- package/dist/core/orchestration/orchestrator.d.ts.map +1 -0
- package/dist/core/orchestration/orchestrator.js +141 -0
- package/dist/core/orchestration/orchestrator.js.map +1 -0
- package/dist/core/orchestration/planner.d.ts +63 -0
- package/dist/core/orchestration/planner.d.ts.map +1 -0
- package/dist/core/orchestration/planner.js +99 -0
- package/dist/core/orchestration/planner.js.map +1 -0
- package/dist/core/providers/factory.d.ts +25 -0
- package/dist/core/providers/factory.d.ts.map +1 -0
- package/dist/core/providers/factory.js +86 -0
- package/dist/core/providers/factory.js.map +1 -0
- package/dist/core/providers/index.d.ts +11 -0
- package/dist/core/providers/index.d.ts.map +1 -0
- package/dist/core/providers/index.js +10 -0
- package/dist/core/providers/index.js.map +1 -0
- package/dist/core/providers/ollama.d.ts +36 -0
- package/dist/core/providers/ollama.d.ts.map +1 -0
- package/dist/core/providers/ollama.js +123 -0
- package/dist/core/providers/ollama.js.map +1 -0
- package/dist/core/providers/openai.d.ts +33 -0
- package/dist/core/providers/openai.d.ts.map +1 -0
- package/dist/core/providers/openai.js +114 -0
- package/dist/core/providers/openai.js.map +1 -0
- package/dist/core/providers/openrouter.d.ts +38 -0
- package/dist/core/providers/openrouter.d.ts.map +1 -0
- package/dist/core/providers/openrouter.js +269 -0
- package/dist/core/providers/openrouter.js.map +1 -0
- package/dist/core/providers/types.d.ts +101 -0
- package/dist/core/providers/types.d.ts.map +1 -0
- package/dist/core/providers/types.js +7 -0
- package/dist/core/providers/types.js.map +1 -0
- package/dist/core/tools/base.d.ts +56 -0
- package/dist/core/tools/base.d.ts.map +1 -0
- package/dist/core/tools/base.js +60 -0
- package/dist/core/tools/base.js.map +1 -0
- package/dist/core/tools/bash.d.ts +50 -0
- package/dist/core/tools/bash.d.ts.map +1 -0
- package/dist/core/tools/bash.js +179 -0
- package/dist/core/tools/bash.js.map +1 -0
- package/dist/core/tools/edit.d.ts +39 -0
- package/dist/core/tools/edit.d.ts.map +1 -0
- package/dist/core/tools/edit.js +67 -0
- package/dist/core/tools/edit.js.map +1 -0
- package/dist/core/tools/executor.d.ts +33 -0
- package/dist/core/tools/executor.d.ts.map +1 -0
- package/dist/core/tools/executor.js +105 -0
- package/dist/core/tools/executor.js.map +1 -0
- package/dist/core/tools/fetchPage.d.ts +46 -0
- package/dist/core/tools/fetchPage.d.ts.map +1 -0
- package/dist/core/tools/fetchPage.js +87 -0
- package/dist/core/tools/fetchPage.js.map +1 -0
- package/dist/core/tools/fetchURLs.d.ts +39 -0
- package/dist/core/tools/fetchURLs.d.ts.map +1 -0
- package/dist/core/tools/fetchURLs.js +67 -0
- package/dist/core/tools/fetchURLs.js.map +1 -0
- package/dist/core/tools/glob.d.ts +36 -0
- package/dist/core/tools/glob.d.ts.map +1 -0
- package/dist/core/tools/glob.js +78 -0
- package/dist/core/tools/glob.js.map +1 -0
- package/dist/core/tools/grep.d.ts +51 -0
- package/dist/core/tools/grep.d.ts.map +1 -0
- package/dist/core/tools/grep.js +152 -0
- package/dist/core/tools/grep.js.map +1 -0
- package/dist/core/tools/index.d.ts +22 -0
- package/dist/core/tools/index.d.ts.map +1 -0
- package/dist/core/tools/index.js +22 -0
- package/dist/core/tools/index.js.map +1 -0
- package/dist/core/tools/llmExecute.d.ts +153 -0
- package/dist/core/tools/llmExecute.d.ts.map +1 -0
- package/dist/core/tools/llmExecute.js +105 -0
- package/dist/core/tools/llmExecute.js.map +1 -0
- package/dist/core/tools/readEmail.d.ts +68 -0
- package/dist/core/tools/readEmail.d.ts.map +1 -0
- package/dist/core/tools/readEmail.js +182 -0
- package/dist/core/tools/readEmail.js.map +1 -0
- package/dist/core/tools/readFile.d.ts +42 -0
- package/dist/core/tools/readFile.d.ts.map +1 -0
- package/dist/core/tools/readFile.js +79 -0
- package/dist/core/tools/readFile.js.map +1 -0
- package/dist/core/tools/registry.d.ts +53 -0
- package/dist/core/tools/registry.d.ts.map +1 -0
- package/dist/core/tools/registry.js +112 -0
- package/dist/core/tools/registry.js.map +1 -0
- package/dist/core/tools/sendEmail.d.ts +83 -0
- package/dist/core/tools/sendEmail.d.ts.map +1 -0
- package/dist/core/tools/sendEmail.js +132 -0
- package/dist/core/tools/sendEmail.js.map +1 -0
- package/dist/core/tools/sendWebhook.d.ts +48 -0
- package/dist/core/tools/sendWebhook.d.ts.map +1 -0
- package/dist/core/tools/sendWebhook.js +61 -0
- package/dist/core/tools/sendWebhook.js.map +1 -0
- package/dist/core/tools/webSearch.d.ts +34 -0
- package/dist/core/tools/webSearch.d.ts.map +1 -0
- package/dist/core/tools/webSearch.js +104 -0
- package/dist/core/tools/webSearch.js.map +1 -0
- package/dist/core/tools/writeFile.d.ts +49 -0
- package/dist/core/tools/writeFile.d.ts.map +1 -0
- package/dist/core/tools/writeFile.js +120 -0
- package/dist/core/tools/writeFile.js.map +1 -0
- package/dist/db/client.d.ts +22 -0
- package/dist/db/client.d.ts.map +1 -0
- package/dist/db/client.js +178 -0
- package/dist/db/client.js.map +1 -0
- package/dist/db/schema.d.ts +1846 -0
- package/dist/db/schema.d.ts.map +1 -0
- package/dist/db/schema.js +175 -0
- package/dist/db/schema.js.map +1 -0
- package/dist/errors/index.d.ts +80 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +135 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +172 -0
- package/dist/index.js.map +1 -0
- package/dist/services/initDB.d.ts +17 -0
- package/dist/services/initDB.d.ts.map +1 -0
- package/dist/services/initDB.js +212 -0
- package/dist/services/initDB.js.map +1 -0
- package/dist/types/agent.d.ts +354 -0
- package/dist/types/agent.d.ts.map +1 -0
- package/dist/types/agent.js +63 -0
- package/dist/types/agent.js.map +1 -0
- package/dist/types/client.d.ts +309 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +2 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/config.d.ts +96 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +33 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/dag.d.ts +253 -0
- package/dist/types/dag.d.ts.map +1 -0
- package/dist/types/dag.js +46 -0
- package/dist/types/dag.js.map +1 -0
- package/dist/types/execution.d.ts +171 -0
- package/dist/types/execution.d.ts.map +1 -0
- package/dist/types/execution.js +41 -0
- package/dist/types/execution.js.map +1 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +11 -0
- package/dist/types/index.js.map +1 -0
- package/dist/util/cron-validator.d.ts +16 -0
- package/dist/util/cron-validator.d.ts.map +1 -0
- package/dist/util/cron-validator.js +31 -0
- package/dist/util/cron-validator.js.map +1 -0
- package/dist/util/dag-utils.d.ts +54 -0
- package/dist/util/dag-utils.d.ts.map +1 -0
- package/dist/util/dag-utils.js +167 -0
- package/dist/util/dag-utils.js.map +1 -0
- package/dist/util/logger.d.ts +21 -0
- package/dist/util/logger.d.ts.map +1 -0
- package/dist/util/logger.js +87 -0
- package/dist/util/logger.js.map +1 -0
- package/package.json +82 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Orchestrator
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates goal execution by managing the agent loop
|
|
5
|
+
*/
|
|
6
|
+
import type { DrizzleDB } from '../../db/client.js';
|
|
7
|
+
import type { LLMProvider } from '../providers/types.js';
|
|
8
|
+
import { ToolExecutor } from '../tools/executor.js';
|
|
9
|
+
import { RunsService } from '../execution/runs.js';
|
|
10
|
+
/**
|
|
11
|
+
* Orchestrator configuration
|
|
12
|
+
*/
|
|
13
|
+
export interface OrchestratorConfig {
|
|
14
|
+
db: DrizzleDB;
|
|
15
|
+
llmProvider: LLMProvider;
|
|
16
|
+
toolExecutor: ToolExecutor;
|
|
17
|
+
runService: RunsService;
|
|
18
|
+
maxSteps?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Execution event
|
|
22
|
+
*/
|
|
23
|
+
export interface ExecutionEvent {
|
|
24
|
+
type: 'step_start' | 'step_complete' | 'tool_call' | 'tool_result' | 'execution_complete' | 'execution_failed';
|
|
25
|
+
data: Record<string, any>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Agent Orchestrator
|
|
29
|
+
*/
|
|
30
|
+
export declare class AgentOrchestrator {
|
|
31
|
+
private config;
|
|
32
|
+
private logger;
|
|
33
|
+
constructor(config: OrchestratorConfig);
|
|
34
|
+
/**
|
|
35
|
+
* Execute a run
|
|
36
|
+
*/
|
|
37
|
+
executeRun(runId: string): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=orchestrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../../../src/core/orchestration/orchestrator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAMnD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,SAAS,CAAC;IACd,WAAW,EAAE,WAAW,CAAC;IACzB,YAAY,EAAE,YAAY,CAAC;IAC3B,UAAU,EAAE,WAAW,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EACA,YAAY,GACZ,eAAe,GACf,WAAW,GACX,aAAa,GACb,oBAAoB,GACpB,kBAAkB,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC3B;AAED;;GAEG;AACH,qBAAa,iBAAiB;IAGhB,OAAO,CAAC,MAAM;IAF1B,OAAO,CAAC,MAAM,CAAe;gBAET,MAAM,EAAE,kBAAkB;IAE9C;;OAEG;IACG,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAgL/C"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Orchestrator
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates goal execution by managing the agent loop
|
|
5
|
+
*/
|
|
6
|
+
import { AgentPlanner } from './planner.js';
|
|
7
|
+
import { getLogger } from '../../util/logger.js';
|
|
8
|
+
import { ExecutionError } from '../../errors/index.js';
|
|
9
|
+
import { runs, agents } from '../../db/schema.js';
|
|
10
|
+
import { eq } from 'drizzle-orm';
|
|
11
|
+
/**
|
|
12
|
+
* Agent Orchestrator
|
|
13
|
+
*/
|
|
14
|
+
export class AgentOrchestrator {
|
|
15
|
+
config;
|
|
16
|
+
logger = getLogger();
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Execute a run
|
|
22
|
+
*/
|
|
23
|
+
async executeRun(runId) {
|
|
24
|
+
const { db, toolExecutor, runService, llmProvider } = this.config;
|
|
25
|
+
this.logger.info(`Starting run execution: ${runId}`);
|
|
26
|
+
try {
|
|
27
|
+
// Get run and goal
|
|
28
|
+
const run = await db.query.runs.findFirst({
|
|
29
|
+
where: eq(runs.id, runId),
|
|
30
|
+
with: { goal: true },
|
|
31
|
+
});
|
|
32
|
+
if (!run) {
|
|
33
|
+
throw new ExecutionError(`Run not found: ${runId}`, runId);
|
|
34
|
+
}
|
|
35
|
+
const goal = run.goal;
|
|
36
|
+
if (!goal) {
|
|
37
|
+
throw new ExecutionError(`Goal not found for run: ${runId}`, runId);
|
|
38
|
+
}
|
|
39
|
+
// Get agent if specified
|
|
40
|
+
let customPrompt;
|
|
41
|
+
let customProvider = llmProvider;
|
|
42
|
+
if (goal.agentId) {
|
|
43
|
+
const agent = await db.query.agents.findFirst({
|
|
44
|
+
where: eq(agents.id, goal.agentId),
|
|
45
|
+
});
|
|
46
|
+
if (agent) {
|
|
47
|
+
customPrompt = agent.promptTemplate;
|
|
48
|
+
this.logger.info(`Using agent: ${agent.name}@${agent.version}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Create planner
|
|
52
|
+
const planner = new AgentPlanner(customProvider, customPrompt);
|
|
53
|
+
// Update run to running
|
|
54
|
+
await runService._updateStatus(runId, 'running');
|
|
55
|
+
const maxSteps = this.config.maxSteps || 20;
|
|
56
|
+
const tools = toolExecutor.listTools();
|
|
57
|
+
let workingMemory = run.workingMemory || {};
|
|
58
|
+
let currentStep = 0;
|
|
59
|
+
const stepHistory = [];
|
|
60
|
+
// Main execution loop
|
|
61
|
+
while (currentStep < maxSteps) {
|
|
62
|
+
currentStep++;
|
|
63
|
+
const stepsRemaining = maxSteps - currentStep;
|
|
64
|
+
this.logger.info(`Executing step ${currentStep}/${maxSteps}`);
|
|
65
|
+
try {
|
|
66
|
+
// Plan next step
|
|
67
|
+
const goalParams = goal.params || {};
|
|
68
|
+
const plan = await planner.plan({
|
|
69
|
+
objective: goal.objective,
|
|
70
|
+
workingMemory,
|
|
71
|
+
stepHistory,
|
|
72
|
+
stepsRemaining,
|
|
73
|
+
tools,
|
|
74
|
+
temperature: goalParams.temperature,
|
|
75
|
+
maxTokens: goalParams.maxTokens,
|
|
76
|
+
});
|
|
77
|
+
// Record thought
|
|
78
|
+
void await runService._addStep(runId, currentStep, plan.thought);
|
|
79
|
+
stepHistory.push({
|
|
80
|
+
stepNo: currentStep,
|
|
81
|
+
thought: plan.thought,
|
|
82
|
+
});
|
|
83
|
+
// Execute tool calls if any
|
|
84
|
+
if (plan.toolCalls && plan.toolCalls.length > 0) {
|
|
85
|
+
const results = [];
|
|
86
|
+
for (const toolCall of plan.toolCalls) {
|
|
87
|
+
this.logger.info(`Executing tool: ${toolCall.name}`);
|
|
88
|
+
const toolResult = await toolExecutor.execute(toolCall.name, toolCall.arguments, toolCall.id);
|
|
89
|
+
if (toolResult.status === 'success') {
|
|
90
|
+
results.push(toolResult.output);
|
|
91
|
+
this.logger.debug(`Tool ${toolCall.name} completed successfully`);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.logger.error(`Tool ${toolCall.name} failed: ${toolResult.error?.message}`);
|
|
95
|
+
results.push(`Error: ${toolResult.error?.message}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Update working memory with tool results
|
|
99
|
+
workingMemory = {
|
|
100
|
+
...workingMemory,
|
|
101
|
+
lastToolResults: results,
|
|
102
|
+
lastToolCalls: plan.toolCalls,
|
|
103
|
+
};
|
|
104
|
+
// Record tool call in step history
|
|
105
|
+
stepHistory[stepHistory.length - 1].toolName = plan.toolCalls[0].name;
|
|
106
|
+
stepHistory[stepHistory.length - 1].toolInput = plan.toolCalls[0].arguments;
|
|
107
|
+
stepHistory[stepHistory.length - 1].observation = results.join('; ');
|
|
108
|
+
}
|
|
109
|
+
// Check if we should finish
|
|
110
|
+
if (plan.shouldFinish) {
|
|
111
|
+
this.logger.info(`Agent signaled completion at step ${currentStep}`);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
117
|
+
this.logger.error(`Step ${currentStep} failed: ${errorMsg}`);
|
|
118
|
+
// Record error and continue or fail
|
|
119
|
+
await runService._updateStatus(runId, 'failed', `Step ${currentStep} error: ${errorMsg}`);
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Update run to completed
|
|
124
|
+
await runService._updateStatus(runId, 'completed');
|
|
125
|
+
await runService._updateWorkingMemory(runId, workingMemory);
|
|
126
|
+
this.logger.info(`Run completed successfully after ${currentStep} steps`);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
130
|
+
this.logger.error(`Run execution failed: ${errorMsg}`);
|
|
131
|
+
try {
|
|
132
|
+
await runService._updateStatus(runId, 'failed', errorMsg);
|
|
133
|
+
}
|
|
134
|
+
catch (updateError) {
|
|
135
|
+
this.logger.error(`Failed to update run status: ${updateError instanceof Error ? updateError.message : String(updateError)}`);
|
|
136
|
+
}
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=orchestrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestrator.js","sourceRoot":"","sources":["../../../src/core/orchestration/orchestrator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AA2BjC;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAGR;IAFZ,MAAM,GAAG,SAAS,EAAE,CAAC;IAE7B,YAAoB,MAA0B;QAA1B,WAAM,GAAN,MAAM,CAAoB;IAAG,CAAC;IAElD;;OAEG;IACH,KAAK,CAAC,UAAU,CAAC,KAAa;QAC5B,MAAM,EAAE,EAAE,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QAElE,IAAI,CAAC,MAAM,CAAC,IAAI,CA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IAAI,CAAC,MAAM,CAAC,KAAK,CACf,gCAAgC,WAAW,YAAY,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,CAC3G,CAAC;YACJ,CAAC;YAED,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Planner
|
|
3
|
+
*
|
|
4
|
+
* Uses LLM to plan the next step in goal execution
|
|
5
|
+
*/
|
|
6
|
+
import type { ToolDefinition } from '../../types/index.js';
|
|
7
|
+
import type { LLMProvider } from '../providers/types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Context for planning
|
|
10
|
+
*/
|
|
11
|
+
export interface PlannerContext {
|
|
12
|
+
objective: string;
|
|
13
|
+
workingMemory: Record<string, any>;
|
|
14
|
+
stepHistory: Array<{
|
|
15
|
+
stepNo: number;
|
|
16
|
+
thought: string;
|
|
17
|
+
toolName?: string;
|
|
18
|
+
toolInput?: Record<string, any>;
|
|
19
|
+
observation?: string;
|
|
20
|
+
}>;
|
|
21
|
+
stepsRemaining: number;
|
|
22
|
+
tools: ToolDefinition[];
|
|
23
|
+
temperature?: number;
|
|
24
|
+
maxTokens?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Tool call from planner
|
|
28
|
+
*/
|
|
29
|
+
export interface PlannerToolCall {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
arguments: Record<string, any>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Result from planner
|
|
36
|
+
*/
|
|
37
|
+
export interface PlannerResult {
|
|
38
|
+
thought: string;
|
|
39
|
+
toolCalls?: PlannerToolCall[];
|
|
40
|
+
shouldFinish: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Agent Planner
|
|
44
|
+
*/
|
|
45
|
+
export declare class AgentPlanner {
|
|
46
|
+
private llmProvider;
|
|
47
|
+
private customPrompt?;
|
|
48
|
+
private logger;
|
|
49
|
+
constructor(llmProvider: LLMProvider, customPrompt?: string | undefined);
|
|
50
|
+
/**
|
|
51
|
+
* Plan the next step
|
|
52
|
+
*/
|
|
53
|
+
plan(context: PlannerContext): Promise<PlannerResult>;
|
|
54
|
+
/**
|
|
55
|
+
* Build system prompt
|
|
56
|
+
*/
|
|
57
|
+
private buildSystemPrompt;
|
|
58
|
+
/**
|
|
59
|
+
* Build user prompt
|
|
60
|
+
*/
|
|
61
|
+
private buildUserPrompt;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=planner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"planner.d.ts","sourceRoot":"","sources":["../../../src/core/orchestration/planner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGzD;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACnC,WAAW,EAAE,KAAK,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAChC,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC,CAAC;IACH,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,eAAe,EAAE,CAAC;IAC9B,YAAY,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,qBAAa,YAAY;IAIrB,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,YAAY,CAAC;IAJvB,OAAO,CAAC,MAAM,CAAe;gBAGnB,WAAW,EAAE,WAAW,EACxB,YAAY,CAAC,EAAE,MAAM,YAAA;IAG/B;;OAEG;IACG,IAAI,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC;IAuC3D;;OAEG;IACH,OAAO,CAAC,iBAAiB;IA0BzB;;OAEG;IACH,OAAO,CAAC,eAAe;CAuBxB"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Planner
|
|
3
|
+
*
|
|
4
|
+
* Uses LLM to plan the next step in goal execution
|
|
5
|
+
*/
|
|
6
|
+
import { getLogger } from '../../util/logger.js';
|
|
7
|
+
/**
|
|
8
|
+
* Agent Planner
|
|
9
|
+
*/
|
|
10
|
+
export class AgentPlanner {
|
|
11
|
+
llmProvider;
|
|
12
|
+
customPrompt;
|
|
13
|
+
logger = getLogger();
|
|
14
|
+
constructor(llmProvider, customPrompt) {
|
|
15
|
+
this.llmProvider = llmProvider;
|
|
16
|
+
this.customPrompt = customPrompt;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Plan the next step
|
|
20
|
+
*/
|
|
21
|
+
async plan(context) {
|
|
22
|
+
const systemPrompt = this.buildSystemPrompt(context);
|
|
23
|
+
const userPrompt = this.buildUserPrompt(context);
|
|
24
|
+
this.logger.debug(`Planning step, stepsRemaining: ${context.stepsRemaining}`);
|
|
25
|
+
try {
|
|
26
|
+
const response = await this.llmProvider.callWithTools({
|
|
27
|
+
messages: [
|
|
28
|
+
{ role: 'system', content: systemPrompt },
|
|
29
|
+
{ role: 'user', content: userPrompt },
|
|
30
|
+
],
|
|
31
|
+
tools: context.tools,
|
|
32
|
+
temperature: context.temperature ?? 0.7,
|
|
33
|
+
maxTokens: context.maxTokens ?? 4096,
|
|
34
|
+
});
|
|
35
|
+
const shouldFinish = (response.finishReason === 'stop' &&
|
|
36
|
+
(!response.toolCalls || response.toolCalls.length === 0)) ||
|
|
37
|
+
context.stepsRemaining <= 1;
|
|
38
|
+
this.logger.debug(`Plan result: ${response.toolCalls?.length || 0} tool calls, shouldFinish: ${shouldFinish}`);
|
|
39
|
+
return {
|
|
40
|
+
thought: response.thought,
|
|
41
|
+
toolCalls: response.toolCalls,
|
|
42
|
+
shouldFinish,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
this.logger.error(`Planning failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Build system prompt
|
|
52
|
+
*/
|
|
53
|
+
buildSystemPrompt(context) {
|
|
54
|
+
if (this.customPrompt) {
|
|
55
|
+
return this.customPrompt;
|
|
56
|
+
}
|
|
57
|
+
const toolsList = context.tools
|
|
58
|
+
.map((t) => `- ${t.function.name}: ${t.function.description}`)
|
|
59
|
+
.join('\n');
|
|
60
|
+
const memoryStr = JSON.stringify(context.workingMemory, null, 2);
|
|
61
|
+
return `You are an autonomous agent helping to accomplish the following objective:
|
|
62
|
+
|
|
63
|
+
Objective: ${context.objective}
|
|
64
|
+
|
|
65
|
+
You have access to the following tools:
|
|
66
|
+
${toolsList}
|
|
67
|
+
|
|
68
|
+
Current working memory:
|
|
69
|
+
${memoryStr}
|
|
70
|
+
|
|
71
|
+
Your task is to think about what to do next and use the available tools to accomplish the objective.
|
|
72
|
+
When responding, be concise and focus on the next action to take.
|
|
73
|
+
If you have achieved the objective, respond with "DONE: [summary of what was accomplished]" and no tool calls.`;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Build user prompt
|
|
77
|
+
*/
|
|
78
|
+
buildUserPrompt(context) {
|
|
79
|
+
let prompt = `Steps completed: ${context.stepHistory.length}
|
|
80
|
+
Steps remaining: ${context.stepsRemaining}
|
|
81
|
+
|
|
82
|
+
${context.stepHistory.length > 0 ? 'Execution history:\n' : ''}`;
|
|
83
|
+
for (const step of context.stepHistory) {
|
|
84
|
+
prompt += `\nStep ${step.stepNo}: ${step.thought}`;
|
|
85
|
+
if (step.toolName) {
|
|
86
|
+
prompt += `\n Tool: ${step.toolName}`;
|
|
87
|
+
if (step.toolInput) {
|
|
88
|
+
prompt += `\n Input: ${JSON.stringify(step.toolInput)}`;
|
|
89
|
+
}
|
|
90
|
+
if (step.observation) {
|
|
91
|
+
prompt += `\n Result: ${step.observation}`;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
prompt += '\n\nWhat is the next action?';
|
|
96
|
+
return prompt;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=planner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"planner.js","sourceRoot":"","sources":["../../../src/core/orchestration/planner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAuCjD;;GAEG;AACH,MAAM,OAAO,YAAY;IAIb;IACA;IAJF,MAAM,GAAG,SAAS,EAAE,CAAC;IAE7B,YACU,WAAwB,EACxB,YAAqB;QADrB,gBAAW,GAAX,WAAW,CAAa;QACxB,iBAAY,GAAZ,YAAY,CAAS;IAC5B,CAAC;IAEJ;;OAEG;IACH,KAAK,CAAC,IAAI,CAAC,OAAuB;QAChC,MAAM,YAAY,GAAG,IAAI,CAAC,iBAAiB,CAAC,OAAO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C;oBACrB,MAAM,IAAI,eAAe,IAAI,CAAC,WAAW,EAAE,CAAC;gBAC9C,CAAC;YACH,CAAC;QACH,CAAC;QAED,MAAM,IAAI,8BAA8B,CAAC;QAEzC,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Provider Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates LLM providers based on configuration
|
|
5
|
+
*/
|
|
6
|
+
import type { LLMProvider } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Clear the provider cache
|
|
9
|
+
*/
|
|
10
|
+
export declare function clearProviderCache(): void;
|
|
11
|
+
/**
|
|
12
|
+
* Create an LLM provider based on configuration (with caching)
|
|
13
|
+
*/
|
|
14
|
+
export declare function createLLMProvider(config: {
|
|
15
|
+
provider: 'openai' | 'openrouter' | 'ollama';
|
|
16
|
+
apiKey?: string;
|
|
17
|
+
baseUrl?: string;
|
|
18
|
+
model?: string;
|
|
19
|
+
maxTokens?: number;
|
|
20
|
+
}): LLMProvider;
|
|
21
|
+
/**
|
|
22
|
+
* Validate LLM setup
|
|
23
|
+
*/
|
|
24
|
+
export declare function validateLLMSetup(provider: LLMProvider, model: string): Promise<void>;
|
|
25
|
+
//# sourceMappingURL=factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../../src/core/providers/factory.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAY9C;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE;IACN,QAAQ,EAAE,QAAQ,GAAG,YAAY,GAAG,QAAQ,CAAC;IAC7C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,GACA,WAAW,CA8Cb;AAWD;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,QAAQ,EAAE,WAAW,EACrB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC,CAcf"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Provider Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates LLM providers based on configuration
|
|
5
|
+
*/
|
|
6
|
+
import { OpenAIProvider } from './openai.js';
|
|
7
|
+
import { OllamaProvider } from './ollama.js';
|
|
8
|
+
import { OpenRouterProvider } from './openrouter.js';
|
|
9
|
+
import { getLogger } from '../../util/logger.js';
|
|
10
|
+
/**
|
|
11
|
+
* LLM Provider cache for reusing provider instances
|
|
12
|
+
* Key format: "provider:model:maxTokens"
|
|
13
|
+
*/
|
|
14
|
+
const providerCache = new Map();
|
|
15
|
+
/**
|
|
16
|
+
* Clear the provider cache
|
|
17
|
+
*/
|
|
18
|
+
export function clearProviderCache() {
|
|
19
|
+
providerCache.clear();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Create an LLM provider based on configuration (with caching)
|
|
23
|
+
*/
|
|
24
|
+
export function createLLMProvider(config) {
|
|
25
|
+
const logger = getLogger();
|
|
26
|
+
const model = config.model || getDefaultModel(config.provider);
|
|
27
|
+
const maxTokens = config.maxTokens || 4096;
|
|
28
|
+
const cacheKey = `${config.provider}:${model}:${maxTokens}`;
|
|
29
|
+
// Check cache first
|
|
30
|
+
const cached = providerCache.get(cacheKey);
|
|
31
|
+
if (cached) {
|
|
32
|
+
logger.debug(`Provider cache hit: ${cacheKey}`);
|
|
33
|
+
return cached;
|
|
34
|
+
}
|
|
35
|
+
let provider;
|
|
36
|
+
if (config.provider === 'openai') {
|
|
37
|
+
const apiKey = config.apiKey;
|
|
38
|
+
if (!apiKey) {
|
|
39
|
+
throw new Error('OPENAI_API_KEY is required for openai provider');
|
|
40
|
+
}
|
|
41
|
+
logger.info(`Creating OpenAI provider (model: ${model})`);
|
|
42
|
+
provider = new OpenAIProvider(apiKey, model, maxTokens);
|
|
43
|
+
}
|
|
44
|
+
else if (config.provider === 'openrouter') {
|
|
45
|
+
const apiKey = config.apiKey || process.env.OPENROUTER_API_KEY;
|
|
46
|
+
if (!apiKey) {
|
|
47
|
+
throw new Error('OPENROUTER_API_KEY is required for openrouter provider');
|
|
48
|
+
}
|
|
49
|
+
logger.info(`Creating OpenRouter provider (model: ${model})`);
|
|
50
|
+
provider = new OpenRouterProvider(apiKey, model, maxTokens);
|
|
51
|
+
}
|
|
52
|
+
else if (config.provider === 'ollama') {
|
|
53
|
+
const baseUrl = config.baseUrl || 'http://localhost:11434';
|
|
54
|
+
logger.info(`Creating Ollama provider (url: ${baseUrl}, model: ${model})`);
|
|
55
|
+
provider = new OllamaProvider(baseUrl, model, maxTokens);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
throw new Error(`Unsupported LLM provider: ${config.provider}`);
|
|
59
|
+
}
|
|
60
|
+
// Cache the provider
|
|
61
|
+
providerCache.set(cacheKey, provider);
|
|
62
|
+
return provider;
|
|
63
|
+
}
|
|
64
|
+
function getDefaultModel(provider) {
|
|
65
|
+
switch (provider) {
|
|
66
|
+
case 'openai': return 'gpt-4o';
|
|
67
|
+
case 'openrouter': return 'anthropic/claude-3.5-sonnet';
|
|
68
|
+
case 'ollama': return 'mistral';
|
|
69
|
+
default: return 'gpt-4o';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Validate LLM setup
|
|
74
|
+
*/
|
|
75
|
+
export async function validateLLMSetup(provider, model) {
|
|
76
|
+
const logger = getLogger();
|
|
77
|
+
logger.info(`Validating ${provider.name} provider for model: ${model}`);
|
|
78
|
+
const result = await provider.validateToolCallSupport(model);
|
|
79
|
+
if (!result.supported) {
|
|
80
|
+
logger.warn(`Model ${model} may not support tool calling: ${result.message}`);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
logger.info(`Model ${model} supports tool calling`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/core/providers/factory.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD;;;GAGG;AACH,MAAM,aAAa,GAAG,IAAI,GAAG,EAAuB,CAAC;AAErD;;GAEG;AACH,MAAM,UAAU,kBAAkB;IAChC,aAAa,CAAC,KAAK,EAAE,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAMC;IAED,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAE3B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC/D,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,IAAI,CAAC;IAC3C,MAAM,QAAQ,GAAG,GAAG,MAAM,CAAC,QAAQ,IAAI,KAAK,IAAI,SAAS,EAAE,CAAC;IAE5D,oBAAoB;IACpB,MAAM,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC3C,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,CAAC,KAAK,CAAC,uBAAuB,QAAQ,EAAE,CAAC,CAAC;QAChD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,IAAI,QAAqB,CAAC;IAE1B,IAAI,MAAM,CAAC,QAA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C;AAED,SAAS,eAAe,CAAC,QAAgB;IACvC,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,QAAQ,CAAC,CAAC,OAAO,QAAQ,CAAC;QAC/B,KAAK,YAAY,CAAC,CAAC,OAAO,6BAA6B,CAAC;QACxD,KAAK,QAAQ,CAAC,CAAC,OAAO,SAAS,CAAC;QAChC,OAAO,CAAC,CAAC,OAAO,QAAQ,CAAC;IAC3B,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,QAAqB,EACrB,KAAa;IAEb,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAE3B,MAAM,CAAC,IAAI,CAAC,cAAc,QAAQ,CAAC,IAAI,wBAAwB,KAAK,EAAE,CAAC,CAAC;IAExE,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,uBAAuB,CAAC,KAAK,CAAC,CAAC;IAE7D,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACtB,MAAM,CAAC,IAAI,CACT,SAAS,KAAK,kCAAkC,MAAM,CAAC,OAAO,EAAE,CACjE,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,IAAI,CAAC,SAAS,KAAK,wBAAwB,CAAC,CAAC;IACtD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Providers Module
|
|
3
|
+
*
|
|
4
|
+
* LLM provider implementations and factories
|
|
5
|
+
*/
|
|
6
|
+
export type { Message, ToolCall, UsageInfo, FinishReason, ChatParams, ChatResponse, LLMCallParams, LLMResponse, LLMProvider, ProviderConfig, } from './types.js';
|
|
7
|
+
export { OpenAIProvider } from './openai.js';
|
|
8
|
+
export { OllamaProvider } from './ollama.js';
|
|
9
|
+
export { OpenRouterProvider } from './openrouter.js';
|
|
10
|
+
export { createLLMProvider, validateLLMSetup, clearProviderCache } from './factory.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/providers/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EACV,OAAO,EACP,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,aAAa,EACb,WAAW,EACX,WAAW,EACX,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Providers Module
|
|
3
|
+
*
|
|
4
|
+
* LLM provider implementations and factories
|
|
5
|
+
*/
|
|
6
|
+
export { OpenAIProvider } from './openai.js';
|
|
7
|
+
export { OllamaProvider } from './ollama.js';
|
|
8
|
+
export { OpenRouterProvider } from './openrouter.js';
|
|
9
|
+
export { createLLMProvider, validateLLMSetup, clearProviderCache } from './factory.js';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/providers/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAeH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ollama LLM Provider
|
|
3
|
+
*
|
|
4
|
+
* Implements LLMProvider interface for Ollama (local LLM)
|
|
5
|
+
*/
|
|
6
|
+
import type { LLMProvider, ChatParams, ChatResponse, LLMCallParams, LLMResponse } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Ollama Provider
|
|
9
|
+
*/
|
|
10
|
+
export declare class OllamaProvider implements LLMProvider {
|
|
11
|
+
name: string;
|
|
12
|
+
private baseUrl;
|
|
13
|
+
private model;
|
|
14
|
+
private logger;
|
|
15
|
+
constructor(baseUrl?: string, model?: string, _defaultMaxTokens?: number);
|
|
16
|
+
/**
|
|
17
|
+
* Validate if model supports tool calling
|
|
18
|
+
*/
|
|
19
|
+
validateToolCallSupport(model: string): Promise<{
|
|
20
|
+
supported: boolean;
|
|
21
|
+
message?: string;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Simple chat call (no tools)
|
|
25
|
+
*/
|
|
26
|
+
chat(params: ChatParams): Promise<ChatResponse>;
|
|
27
|
+
/**
|
|
28
|
+
* Call with tool support (limited/not supported)
|
|
29
|
+
*/
|
|
30
|
+
callWithTools(params: LLMCallParams): Promise<LLMResponse>;
|
|
31
|
+
/**
|
|
32
|
+
* Basic tool call parsing from text (heuristic)
|
|
33
|
+
*/
|
|
34
|
+
private parseToolCallsFromText;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=ollama.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ollama.d.ts","sourceRoot":"","sources":["../../../src/core/providers/ollama.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,YAAY,EACZ,aAAa,EACb,WAAW,EACZ,MAAM,YAAY,CAAC;AAIpB;;GAEG;AACH,qBAAa,cAAe,YAAW,WAAW;IAChD,IAAI,SAAY;IAChB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,MAAM,CAAe;gBAG3B,OAAO,GAAE,MAAiC,EAC1C,KAAK,GAAE,MAAkB,EACzB,iBAAiB,GAAE,MAAa;IAOlC;;OAEG;IACG,uBAAuB,CAC3B,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IASpD;;OAEG;IACG,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC;IAmCrD;;OAEG;IACG,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,WAAW,CAAC;IAwBhE;;OAEG;IACH,OAAO,CAAC,sBAAsB;CA4C/B"}
|