import { Agent } from "thinkwell:agent";
import { CLAUDE_CODE } from "thinkwell:connectors";
import * as fs from "fs/promises";
import Sentiment from "sentiment";
/**
* A section of a document with its sentiment analysis.
* @JSONSchema
*/
export interface DocumentSection {
/** The section title */
title: string;
/** The sentiment score from the analysis tool */
sentimentScore: number;
/** A brief summary of the section */
summary: string;
}
/**
* Analysis of a document's sentiment and content.
* @JSONSchema
*/
export interface DocumentAnalysis {
/** The overall emotional tone of the document */
overallTone: "positive" | "negative" | "mixed" | "neutral";
/** Analysis of each section */
sections: DocumentSection[];
/** A recommendation based on the analysis */
recommendation: string;
}
/**
* A text passage to analyze.
* @JSONSchema
*/
export interface TextPassage {
/** The text passage to analyze */
text: string;
}
// Initialize the sentiment analyzer (from the `sentiment` npm package)
const sentimentAnalyzer = new Sentiment();
async function main() {
const agent = await Agent.connect(process.env.THINKWELL_AGENT_CMD ?? CLAUDE_CODE);
try {
const feedback = await fs.readFile(
new URL("feedback.txt", import.meta.url),
"utf-8"
);
const analysis = await agent
.think(DocumentAnalysis.Schema)
.text(`
Analyze the following customer feedback document.
Use the sentiment analysis tool to measure the emotional tone of each section,
then provide an overall analysis with recommendations.
`)
.quote(feedback, "feedback")
// Custom tool: wraps the `sentiment` npm package as an MCP tool
.tool(
"analyze_sentiment",
"Analyze the sentiment of a text passage.",
TextPassage.Schema,
async (passage) => {
const result = sentimentAnalyzer.analyze(passage.text);
return {
score: result.score,
comparative: result.comparative,
positive: result.positive,
negative: result.negative,
};
}
)
.run();
console.log(`Overall Tone: ${analysis.overallTone}`);
console.log(`Recommendation: ${analysis.recommendation}`);
} finally {
agent.close();
}
}
main();