a16d70a1b8
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import type { EntityId } from '@dflike/shared';
|
|
import type { LlmService } from './llmService.js';
|
|
|
|
export interface ThoughtRequest {
|
|
entityId: EntityId;
|
|
name: string;
|
|
personality: string;
|
|
currentState: string;
|
|
recentEvents: string;
|
|
}
|
|
|
|
export interface ThoughtResult {
|
|
entityId: EntityId;
|
|
text: string;
|
|
}
|
|
|
|
export async function generateBatchedThoughts(
|
|
requests: ThoughtRequest[],
|
|
llmService: LlmService,
|
|
): Promise<ThoughtResult[]> {
|
|
if (requests.length === 0) return [];
|
|
if (llmService.isDailyLimitReached()) return [];
|
|
|
|
const npcList = requests
|
|
.map((r, i) =>
|
|
`${i + 1}. ${r.name} — Personality: ${r.personality}. State: ${r.currentState}. Recent: ${r.recentEvents}`,
|
|
)
|
|
.join('\n');
|
|
|
|
const response = await llmService.generate('batchedThoughts', { npcList });
|
|
if (response == null) return [];
|
|
|
|
return parseNumberedResponse(response, requests);
|
|
}
|
|
|
|
function parseNumberedResponse(
|
|
response: string,
|
|
requests: ThoughtRequest[],
|
|
): ThoughtResult[] {
|
|
const results: ThoughtResult[] = [];
|
|
const lines = response.split('\n').filter((l) => l.trim().length > 0);
|
|
|
|
for (const line of lines) {
|
|
const match = line.match(/^(\d+)\.\s*(.+)/);
|
|
if (!match) continue;
|
|
|
|
const index = parseInt(match[1], 10) - 1;
|
|
if (index < 0 || index >= requests.length) continue;
|
|
|
|
results.push({
|
|
entityId: requests[index].entityId,
|
|
text: match[2].trim(),
|
|
});
|
|
}
|
|
|
|
return results;
|
|
}
|