Refactor Ollama client and related components

Update error handling and improve streaming capabilities in the Ollama client and related components. Key changes
include:

- Simplify error types and improve error handling
- Refactor streaming logic to use async generators
- Update tool execution and vault indexing
- Improve utility functions and types
- Update test files to reflect changes
This commit is contained in:
2026-05-05 17:09:16 +02:00
parent cc580889d2
commit 59df2f6856
44 changed files with 270 additions and 1369 deletions
+11 -241
View File
@@ -1,256 +1,26 @@
export interface PluginSettings {
ollamaUrl: string;
model: string;
lastIndexTime: number;
}
export enum ErrorType {
NETWORK_ERROR = 'network_error',
API_ERROR = 'api_error',
VALIDATION_ERROR = 'validation_error',
STREAMING_ERROR = 'streaming_error',
TOOL_EXECUTION_ERROR = 'tool_execution_error',
PATH_VALIDATION_ERROR = 'path_validation_error',
UNKNOWN_ERROR = 'unknown_error',
}
export class OllamaError extends Error {
constructor(
message: string,
public readonly type: ErrorType,
public readonly details?: Record<string, any>
) {
super(message);
this.name = 'OllamaError';
}
}
export class NetworkError extends OllamaError {
constructor(
message: string,
public readonly statusCode?: number
) {
super(message, ErrorType.NETWORK_ERROR, { statusCode });
this.name = 'NetworkError';
}
}
export class ApiError extends OllamaError {
constructor(
message: string,
public readonly apiError?: any
) {
super(message, ErrorType.API_ERROR, { apiError });
this.name = 'ApiError';
}
}
export class ValidationError extends OllamaError {
constructor(
message: string,
public readonly validationDetails?: Record<string, string>
) {
super(message, ErrorType.VALIDATION_ERROR, validationDetails);
this.name = 'ValidationError';
}
}
export class StreamingError extends OllamaError {
constructor(
message: string,
public readonly chunkDetails?: any
) {
super(message, ErrorType.STREAMING_ERROR, chunkDetails);
this.name = 'StreamingError';
}
}
export class ToolExecutionError extends OllamaError {
constructor(
message: string,
public readonly toolName?: string
) {
super(message, ErrorType.TOOL_EXECUTION_ERROR, { toolName });
this.name = 'ToolExecutionError';
}
}
export class PathValidationError extends OllamaError {
constructor(
message: string,
public readonly invalidPath?: string
) {
super(message, ErrorType.PATH_VALIDATION_ERROR, { invalidPath });
this.name = 'PathValidationError';
}
}
// src/types.ts
export interface OllamaMessage {
role: 'system' | 'user' | 'assistant' | 'tool';
role: 'system' | 'user' | 'assistant';
content: string;
tool_calls?: ToolCall[];
}
export interface ToolCall {
function: {
name: string;
arguments: string | Record<string, any>;
};
tool_name: string;
arguments: Record<string, unknown>;
}
export interface OllamaTool {
type: 'function';
function: {
name: string;
description: string;
parameters: {
type: 'object';
properties: Record<string, { type: string }>;
required: string[];
};
};
}
export interface ToolResult {
success: boolean;
message: string;
// Adding optional details field for better error reporting
details?: Record<string, any>;
}
export interface VaultIndexEntry {
title: string;
content: string;
score: number;
}
export interface ChatMessage {
id: string;
role: 'user' | 'assistant' | 'tool';
content: string;
timestamp: number;
isStreaming?: boolean;
tool_calls?: ToolCall[];
}
/**
* API Response Types
*/
export interface OllamaChatResponse {
export interface ModelConfig {
model: string;
created_at: string;
message: {
role: 'assistant';
content: string;
tool_calls?: ToolCall[];
};
done: boolean;
total_duration?: number;
load_duration?: number;
prompt_eval_count?: number;
eval_count?: number;
}
export interface OllamaStreamResponse {
model: string;
created_at: string;
message: {
role: 'assistant';
content: string;
tool_calls?: ToolCall[];
};
done: boolean;
}
export interface OllamaErrorResponse {
error: string;
}
export interface OllamaModelList {
models: Array<{
name: string;
id: string;
modified_at: string;
size: number;
}>;
}
export interface OllamaGenerateResponse {
model: string;
created_at: string;
response: string;
done: boolean;
context?: number[];
total_duration?: number;
load_duration?: number;
prompt_eval_count?: number;
eval_count?: number;
}
export interface OllamaPullStatus {
status: string;
digest: string;
total_size: number;
completed_size: number;
}
export interface OllamaEmbeddingResponse {
embeddings: number[];
model: string;
total_duration?: number;
load_duration?: number;
prompt_eval_count?: number;
eval_count?: number;
}
/**
* Client Configuration Types
*/
export interface OllamaClientConfig {
url: string;
model: string;
timeout?: number;
maxRetries?: number;
}
export interface OllamaRequestOptions {
timeout?: number;
signal?: AbortSignal;
export enum RequestType {
Chat = 'chat',
Tool = 'tool',
}
/**
* Streaming Types
*/
export interface StreamChunk {
content: string;
tool_calls?: ToolCall[];
done?: boolean;
error?: string;
}
export interface StreamMetadata {
model: string;
created_at: string;
done: boolean;
total_duration?: number;
load_duration?: number;
}
export interface ChatSession {
id: string;
messages: OllamaMessage[];
createdAt: number;
model: string;
}
export interface ToolDefinition {
type: 'function';
function: {
name: string;
description: string;
parameters: Record<string, any>;
};
export interface ExecutionResult {
success: boolean;
output: string;
}