@JSONSchema, providing a custom tool, and running a thinking session.
The Full Example
Understanding the Code
Opening an Agent
open() function accepts a named agent string and establishes a connection using the Agent Client Protocol (ACP). You can override the agent at runtime by setting the THINKWELL_AGENT or THINKWELL_AGENT_CMD environment variable.
Defining Structured Output with @JSONSchema
@JSONSchema JSDoc tag is the key to type-safe AI outputs. When you annotate an interface with @JSONSchema, Thinkwell automatically generates a JSON Schema at build time and attaches it to the interface as a static .Schema property.
This means:
- At build time: Thinkwell transforms your TypeScript interface into a JSON Schema
- At runtime: The schema is passed to the AI agent, which constrains its output to match
- In your code: You get full TypeScript type checking on the result
description fields in the schema, helping the AI understand what each field should contain.
Building a Thinking Session
.think(schema)- Starts a new session and specifies the expected output schema.text(prompt)- Provides the prompt or instructions for the AI.tool(name, description, handler)- Registers a tool the AI can call.run()- Executes the session and returns the typed result
Providing Tools
- A name - How the AI refers to and invokes the tool
- A description - Helps the AI understand when and how to use the tool
- A handler function - The actual implementation that runs when called
current_time tool lets the AI ask “what time is it?” so it can generate an appropriate greeting like “Good morning!” or “Good evening!”
Cleanup
try/finally pattern ensures cleanup happens even if an error occurs.
Running the Example
Save the code to a file (e.g.,greeting.ts) and run it with the Thinkwell CLI:
