From a2cb4ce68686ce93537c9fb48d2043394fea42d7 Mon Sep 17 00:00:00 2001 From: kappa Date: Mon, 19 Jan 2026 23:42:17 +0900 Subject: [PATCH] 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 --- src/n8n-service.ts | 10 +++++++--- src/services/bank-sms-parser.ts | 21 ++++++++++++++++----- src/summary-service.ts | 18 +++++++++++++----- src/types.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/n8n-service.ts b/src/n8n-service.ts index cc7cbc9..7cc27b9 100644 --- a/src/n8n-service.ts +++ b/src/n8n-service.ts @@ -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 || ''; diff --git a/src/services/bank-sms-parser.ts b/src/services/bank-sms-parser.ts index fae5fd7..a83a82e 100644 --- a/src/services/bank-sms-parser.ts +++ b/src/services/bank-sms-parser.ts @@ -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); } diff --git a/src/summary-service.ts b/src/summary-service.ts index 8eea9a9..89ec11b 100644 --- a/src/summary-service.ts +++ b/src/summary-service.ts @@ -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 || '응답을 생성할 수 없습니다.'; } diff --git a/src/types.ts b/src/types.ts index 4de8e9b..20fada0 100644 --- a/src/types.ts +++ b/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; + }; +}