MCPcopy
hub / github.com/opentrace/opentrace / MlStrategy

Class MlStrategy

ui/src/runner/browser/enricher/summarizer/strategy.ts:94–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

92const ML_CALL_TIMEOUT_MS = 30_000; // 30s per inference call
93
94class MlStrategy implements SummarizationStrategy {
95 readonly type = 'ml' as const;
96 private summarizer: import('./flanT5Summarizer').FlanT5Summarizer | null =
97 null;
98 private config: SummarizerConfig;
99
100 constructor(config: SummarizerConfig) {
101 this.config = config;
102 }
103
104 async init(): Promise<void> {
105 console.log(
106 `[MlStrategy] init: model=${this.config.model}, timeout=${ML_INIT_TIMEOUT_MS}ms`,
107 );
108 const { FlanT5Summarizer } = await import('./flanT5Summarizer');
109 this.summarizer = new FlanT5Summarizer(this.config);
110 await withTimeout(
111 this.summarizer.init(),
112 ML_INIT_TIMEOUT_MS,
113 'ML summarizer init',
114 );
115 console.log('[MlStrategy] init complete');
116 }
117
118 async summarize(meta: SymbolMetadata): Promise<string> {
119 if (!this.summarizer)
120 throw new Error('MlStrategy not initialized — call init() first');
121 const source = meta.source ?? meta.name;
122 try {
123 return await withTimeout(
124 this.summarizer.summarize(source, meta.kind),
125 ML_CALL_TIMEOUT_MS,
126 `ML summarize(${meta.name})`,
127 );
128 } catch (err) {
129 console.warn(`[MlStrategy] summarize failed for ${meta.name}:`, err);
130 throw err;
131 }
132 }
133
134 async summarizeBatch(items: SymbolMetadata[]): Promise<string[]> {
135 if (!this.summarizer)
136 throw new Error('MlStrategy not initialized — call init() first');
137 return withTimeout(
138 this.summarizer.summarizeBatch(
139 items.map((m) => ({ source: m.source ?? m.name, kind: m.kind })),
140 ),
141 ML_CALL_TIMEOUT_MS * items.length,
142 'ML summarizeBatch',
143 );
144 }
145
146 async dispose(): Promise<void> {
147 await this.summarizer?.dispose();
148 this.summarizer = null;
149 }
150}
151

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected