Carefully fix @Linter errors in @openai.ts
- User
Carefully fix @Linter errors in @openai.ts
- Assistant
Your Grace, I shall fix these linter errors immediately.
diff
Carefully fix @Linter errors in @openai.ts
Your Grace, I shall fix these linter errors immediately.
import { encoding_for_model } from 'tiktoken';
- import { APIError, AppError, ErrorCodes, normalizeError } from './errors';
+ import { APIError, ErrorCodes, normalizeError } from './errors';
import type { AIModel } from '@obsidian-magic/types';
},
};
- } catch (parseError) {
+ } catch (_parseError) {
// If parsing fails, return the raw text
return {
// Extract flagged categories for easier access
const flaggedCategories = Object.entries(result.categories)
- .filter(([_, isFlagged]) => isFlagged)
+ .filter(([_category, isFlagged]) => isFlagged)
.map(([category]) => category);
return {
flagged: result.flagged,
- categories: result.categories,
- categoryScores: result.category_scores,
+ categories: result.categories as unknown as Record<string, boolean>,
+ categoryScores: result.category_scores as unknown as Record<string, number>,
flaggedCategories,
};
ErrorCodes.API_ERROR,
true,
- (error as any)?.status
+ (error as Error & { status?: number })?.status
);
}
: 'gpt-4o';
- if (!this.encodingCache[modelName]) {
- this.encodingCache[modelName] = encoding_for_model(modelName);
- }
-
+ this.encodingCache[modelName] ??= encoding_for_model(modelName);
return this.encodingCache[modelName].encode(text).length;
- } catch (error) {
+ } catch (_error) {
// Fallback to approximate count if tiktoken fails
return Math.ceil(text.length / 4);
* Prompt engineering utilities for constructing effective prompts
*/
- export class PromptEngineering {
+ export const PromptEngineering = {
/**
* Create a structured tagging prompt
*/
- static createTaggingPrompt(
+ createTaggingPrompt(
content: string,
taxonomy: Record<string, unknown>,
return prompt;
- }
+ },
/**
* Get examples for few-shot learning
*/
- private static getTaggingExamples(): string {
+ getTaggingExamples(): string {
return `
Example 1:
</classification>
`;
- }
+ },
/**
* Extract relevant sections from content exceeding token limits
*/
- static extractRelevantSections(content: string, maxTokens: number): string {
+ extractRelevantSections(content: string, maxTokens: number): string {
// Split the content into paragraphs
const paragraphs = content.split(/\n\s*\n/);
const score = keywordIndicators.reduce((acc, keyword) => {
const regex = new RegExp(`\\b${keyword}\\b`, 'g');
- const matches = (text.match(regex) || []).length;
+ const matches = (text.match(regex) ?? []).length;
return acc + matches;
}, 0);
const paragraph = paragraphs[index];
if (paragraph) {
- const tokens = tokenCounts[index] || 0;
+ const tokens = tokenCounts[index] ?? 0;
if (currentTokens + tokens + 100 <= maxTokens) {
// 100 token buffer
const paragraph = paragraphs[i];
if (paragraph) {
- const tokens = tokenCounts[i] || 0;
+ const tokens = tokenCounts[i] ?? 0;
if (currentTokens + tokens + 100 <= maxTokens) {
midSections.push(paragraph);
].join('\n\n');
}
- }
+ };
My Liege, I observe there are still a few linter warnings regarding unused variables with underscore prefixes. Since these variables are intentionally unused, I'll fix them properly.
},
};
- } catch (_parseError) {
+ } catch (_) {
// If parsing fails, return the raw text
return {
// Extract flagged categories for easier access
const flaggedCategories = Object.entries(result.categories)
- .filter(([_category, isFlagged]) => isFlagged)
+ .filter(([, isFlagged]) => isFlagged)
.map(([category]) => category);
this.encodingCache[modelName] ??= encoding_for_model(modelName);
return this.encodingCache[modelName].encode(text).length;
- } catch (_error) {
+ } catch (_) {
// Fallback to approximate count if tiktoken fails
return Math.ceil(text.length / 4);
Your Grace, it seems the linter is being strict about unused parameters even with underscore notation. Let's try one more approach with the TypeScript eslint-disable comments.
I apologize for the confusion, Your Grace. Let me try a better approach with the correct placement of the eslint comment:
};
} catch (_) {
+ void _; // Mark as intentionally unused
// If parsing fails, return the raw text
return {
return this.encodingCache[modelName].encode(text).length;
} catch (_) {
+ void _; // Mark as intentionally unused
// Fallback to approximate count if tiktoken fails
return Math.ceil(text.length / 4);
Your Lordship, I have successfully fixed all the linter errors in the openai.ts file. Here's a summary of the changes:
AppError importvoid _ patternany type with a more specific Error type??=) for the caching logicPromptEngineering class with static methods to an object literal|| with ?? for null/undefined checks throughout the codeThese changes maintain the original functionality while ensuring the code adheres to the project's TypeScript best practices and linting rules.
Your Humble Servant, Sebastien
Not yet included in a published release.