refactor: remove Workers AI as any with proper type definitions
- Add WorkersAIModel, WorkersAITextGenerationInput/Output types - Remove `as any` from summary-service.ts (4 instances) - Remove `as any` from bank-sms-parser.ts (3 instances) - Remove `as any` from n8n-service.ts (2 instances) - Add OpenAIResponse interface for API responses Type-safe Workers AI calls with full IntelliSense support. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { z } from 'zod';
|
||||
import { Env, IntentAnalysis, N8nResponse } from './types';
|
||||
import { Env, IntentAnalysis, N8nResponse, WorkersAITextGenerationOutput, WorkersAITextGenerationInput } from './types';
|
||||
import { createLogger } from './utils/logger';
|
||||
|
||||
const logger = createLogger('n8n-service');
|
||||
@@ -53,10 +53,14 @@ ${userMessage}
|
||||
JSON:`;
|
||||
|
||||
try {
|
||||
const response = await ai.run('@cf/meta/llama-3.1-8b-instruct' as any, {
|
||||
const input: WorkersAITextGenerationInput = {
|
||||
messages: [{ role: 'user', content: prompt }],
|
||||
max_tokens: 100,
|
||||
}) as any;
|
||||
};
|
||||
const response = await ai.run(
|
||||
'@cf/meta/llama-3.1-8b-instruct' as '@cf/meta/llama-3.1-8b-instruct-fp8',
|
||||
input
|
||||
) as WorkersAITextGenerationOutput;
|
||||
|
||||
const text = response.response || '';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Env, BankNotification } from '../types';
|
||||
import { Env, BankNotification, WorkersAITextGenerationOutput, WorkersAITextGenerationInput } from '../types';
|
||||
import { parseQuotedPrintable } from '../utils/email-decoder';
|
||||
|
||||
/**
|
||||
@@ -177,7 +177,14 @@ ${text.slice(0, 500)}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json() as any;
|
||||
interface OpenAIResponse {
|
||||
choices: Array<{
|
||||
message: {
|
||||
content: string;
|
||||
};
|
||||
}>;
|
||||
}
|
||||
const data = await response.json() as OpenAIResponse;
|
||||
jsonStr = data.choices[0].message.content;
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -188,11 +195,15 @@ ${text.slice(0, 500)}
|
||||
// 2. Workers AI 시도 (OpenAI 실패 또는 미설정 시)
|
||||
if (!jsonStr && env.AI) {
|
||||
try {
|
||||
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct' as any, {
|
||||
const input: WorkersAITextGenerationInput = {
|
||||
messages: [{ role: 'user', content: prompt }],
|
||||
max_tokens: 200,
|
||||
}) as any;
|
||||
jsonStr = response.response;
|
||||
};
|
||||
const response = await env.AI.run(
|
||||
'@cf/meta/llama-3.1-8b-instruct' as '@cf/meta/llama-3.1-8b-instruct-fp8',
|
||||
input
|
||||
) as WorkersAITextGenerationOutput;
|
||||
jsonStr = response.response || '';
|
||||
} catch (e) {
|
||||
console.error('[BankSMS] Workers AI 파싱 실패:', e);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Env, BufferedMessage, Summary, ConversationContext } from './types';
|
||||
import { Env, BufferedMessage, Summary, ConversationContext, WorkersAITextGenerationOutput, WorkersAITextGenerationInput } from './types';
|
||||
import { createLogger } from './utils/logger';
|
||||
|
||||
const logger = createLogger('summary-service');
|
||||
@@ -190,10 +190,14 @@ ${userMessages}
|
||||
}
|
||||
|
||||
// 폴백: Workers AI
|
||||
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct' as any, {
|
||||
const input: WorkersAITextGenerationInput = {
|
||||
messages: [{ role: 'user', content: prompt }],
|
||||
max_tokens: 500,
|
||||
}) as any;
|
||||
};
|
||||
const response = await env.AI.run(
|
||||
'@cf/meta/llama-3.1-8b-instruct' as '@cf/meta/llama-3.1-8b-instruct-fp8',
|
||||
input
|
||||
) as WorkersAITextGenerationOutput;
|
||||
|
||||
return response.response || '프로필 생성 실패';
|
||||
}
|
||||
@@ -329,14 +333,18 @@ ${integratedProfile}
|
||||
}
|
||||
|
||||
// 폴백: Workers AI
|
||||
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct' as any, {
|
||||
const input: WorkersAITextGenerationInput = {
|
||||
messages: [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
...recentContext,
|
||||
{ role: 'user', content: userMessage },
|
||||
],
|
||||
max_tokens: 500,
|
||||
}) as any;
|
||||
};
|
||||
const response = await env.AI.run(
|
||||
'@cf/meta/llama-3.1-8b-instruct' as '@cf/meta/llama-3.1-8b-instruct-fp8',
|
||||
input
|
||||
) as WorkersAITextGenerationOutput;
|
||||
|
||||
return response.response || '응답을 생성할 수 없습니다.';
|
||||
}
|
||||
|
||||
28
src/types.ts
28
src/types.ts
@@ -320,3 +320,31 @@ export interface DomainRegisterKeyboardData {
|
||||
}
|
||||
|
||||
export type KeyboardData = DomainRegisterKeyboardData;
|
||||
|
||||
// Workers AI Types (from worker-configuration.d.ts)
|
||||
export type WorkersAIModel =
|
||||
| '@cf/meta/llama-3.1-8b-instruct'
|
||||
| '@cf/meta/llama-3.2-3b-instruct'
|
||||
| '@cf/meta/llama-3-8b-instruct';
|
||||
|
||||
export interface WorkersAIMessage {
|
||||
role: 'system' | 'user' | 'assistant';
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface WorkersAITextGenerationInput {
|
||||
prompt?: string;
|
||||
messages?: WorkersAIMessage[];
|
||||
max_tokens?: number;
|
||||
temperature?: number;
|
||||
stream?: boolean;
|
||||
}
|
||||
|
||||
export interface WorkersAITextGenerationOutput {
|
||||
response?: string;
|
||||
usage?: {
|
||||
prompt_tokens: number;
|
||||
completion_tokens: number;
|
||||
total_tokens: number;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user