@elizaos/plugin-wolfram
Version:
Wolfram Alpha and Wolfram Cloud integration plugin for ElizaOS
423 lines (417 loc) • 12.2 kB
TypeScript
import { IAgentRuntime, Service, Plugin } from '@elizaos/core';
import { z } from 'zod';
interface ActionResult {
/** Whether the action succeeded - defaults to true */
success: boolean;
/** Optional text description of the result */
text?: string;
/** Values to merge into the state */
values?: Record<string, any>;
/** Data payload containing action-specific results */
data?: Record<string, any>;
/** Error information if the action failed */
error?: string | Error;
}
interface ActionContext {
/** Results from previously executed actions in this run */
previousResults: ActionResult[];
/** Get a specific previous result by action name */
getPreviousResult?: (actionName: string) => ActionResult | undefined;
}
interface WolframAlphaQueryResult {
success: boolean;
error?: string;
numpods?: number;
pods?: WolframPod[];
assumptions?: WolframAssumption[];
warnings?: WolframWarning[];
sources?: WolframSource[];
didyoumeans?: WolframDidYouMean[];
languagemsg?: string;
futuretopic?: string;
relatedexamples?: string[];
examplepage?: string;
generalization?: WolframGeneralization;
inputstring?: string;
parsetimedout?: boolean;
recalculate?: string;
id?: string;
server?: string;
related?: string;
version?: string;
}
interface WolframPod {
title: string;
scanner: string;
id: string;
position: number;
error: boolean;
numsubpods: number;
subpods: WolframSubpod[];
primary?: boolean;
expressiontypes?: string[];
states?: WolframState[];
infos?: WolframInfo[];
definitions?: WolframDefinition[];
}
interface WolframSubpod {
title?: string;
img?: WolframImage;
plaintext?: string;
mathml?: string;
sound?: WolframSound;
imagemap?: WolframImageMap;
infos?: WolframInfo[];
}
interface WolframImage {
src: string;
alt: string;
title: string;
width: number;
height: number;
type?: string;
themes?: string[];
colorinvertable?: boolean;
}
interface WolframSound {
src: string;
type: string;
}
interface WolframImageMap {
rects: WolframRect[];
}
interface WolframRect {
left: number;
top: number;
right: number;
bottom: number;
query: string;
assumptions?: string;
title?: string;
}
interface WolframAssumption {
type: string;
word?: string;
template?: string;
count?: number;
values?: WolframAssumptionValue[];
}
interface WolframAssumptionValue {
name: string;
desc: string;
input: string;
word?: string;
}
interface WolframWarning {
text: string;
spellcheck?: WolframSpellcheck;
delimiters?: WolframDelimiters;
translation?: WolframTranslation;
reinterpret?: WolframReinterpret;
}
interface WolframSpellcheck {
word: string;
suggestion: string;
text: string;
}
interface WolframDelimiters {
text: string;
}
interface WolframTranslation {
phrase: string;
trans: string;
lang: string;
text: string;
}
interface WolframReinterpret {
text: string;
new?: string;
alternatives?: WolframAlternative[];
level?: string;
}
interface WolframAlternative {
text: string;
input: string;
level?: string;
score?: number;
}
interface WolframSource {
url: string;
text?: string;
}
interface WolframDidYouMean {
score: number;
level: string;
text?: string;
input?: string;
}
interface WolframGeneralization {
topic: string;
desc: string;
url: string;
}
interface WolframState {
name: string;
input: string;
states?: WolframState[];
}
interface WolframInfo {
text?: string;
img?: WolframImage;
links?: WolframLink[];
units?: WolframUnits[];
}
interface WolframLink {
url: string;
text: string;
title?: string;
}
interface WolframUnits {
short: string;
long: string;
}
interface WolframDefinition {
word: string;
desc: string;
}
interface WolframConversationResult {
conversationID: string;
host: string;
s: string;
result?: string;
error?: string;
expired?: boolean;
}
interface WolframSimpleResult {
result: string;
success: boolean;
error?: string;
}
interface WolframShortAnswerResult {
answer: string;
success: boolean;
error?: string;
}
interface WolframSpokenResult {
spoken: string;
success: boolean;
error?: string;
}
interface WolframQueryOptions {
input: string;
format?: string;
output?: string;
appid?: string;
assumption?: string;
podstate?: string;
includepodid?: string;
excludepodid?: string;
podtitle?: string;
podindex?: string;
scanner?: string;
async?: boolean;
ip?: string;
location?: string;
gps?: string;
ignorecase?: boolean;
translation?: boolean;
reinterpret?: boolean;
width?: number;
maxwidth?: number;
plotwidth?: number;
mag?: number;
scantimeout?: number;
podtimeout?: number;
formattimeout?: number;
parsetimeout?: number;
totaltimeout?: number;
units?: "metric" | "imperial";
}
interface WolframLLMOptions {
input: string;
appid: string;
conversationID?: string;
maxchars?: number;
}
declare enum WolframAPIEndpoint {
QUERY = "/query",
SIMPLE = "/simple",
SHORT = "/short",
SPOKEN = "/spoken",
RESULT = "/result",
LLM = "https://www.wolframalpha.com/api/v1/llm-api",
CONVERSATION = "https://www.wolframalpha.com/api/v1/conversation.jsp"
}
interface WolframCacheEntry {
query: string;
result: any;
timestamp: number;
ttl: number;
}
interface WolframAnalysisResult {
input: string;
results: Record<string, string[] | string>;
error?: string;
}
interface WolframServiceStats {
cacheSize: number;
activeConversations: number;
config: {
units?: "metric" | "imperial";
location?: string;
maxResults?: number;
};
}
interface WolframServiceOptions {
appId: string;
cloudApiKey?: string;
endpoint?: string;
timeout?: number;
cache?: boolean;
cacheTTL?: number;
units?: "metric" | "imperial";
location?: string;
maxResults?: number;
}
/**
* Schema for Wolfram API configuration
*/
declare const wolframEnvSchema: z.ZodObject<{
WOLFRAM_APP_ID: z.ZodString;
WOLFRAM_CLOUD_API_KEY: z.ZodOptional<z.ZodString>;
WOLFRAM_API_ENDPOINT: z.ZodDefault<z.ZodOptional<z.ZodString>>;
WOLFRAM_LLM_API_ENDPOINT: z.ZodDefault<z.ZodOptional<z.ZodString>>;
WOLFRAM_CONVERSATION_ENDPOINT: z.ZodDefault<z.ZodOptional<z.ZodString>>;
WOLFRAM_OUTPUT_FORMAT: z.ZodDefault<z.ZodOptional<z.ZodEnum<["plaintext", "image", "mathml", "sound", "wav"]>>>;
WOLFRAM_TIMEOUT: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
WOLFRAM_UNITS: z.ZodDefault<z.ZodOptional<z.ZodEnum<["metric", "imperial"]>>>;
WOLFRAM_LOCATION: z.ZodOptional<z.ZodString>;
WOLFRAM_SCANNERS: z.ZodOptional<z.ZodString>;
WOLFRAM_MAX_RESULTS: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
}, "strip", z.ZodTypeAny, {
WOLFRAM_APP_ID: string;
WOLFRAM_API_ENDPOINT: string;
WOLFRAM_LLM_API_ENDPOINT: string;
WOLFRAM_CONVERSATION_ENDPOINT: string;
WOLFRAM_OUTPUT_FORMAT: "plaintext" | "image" | "mathml" | "sound" | "wav";
WOLFRAM_TIMEOUT: number;
WOLFRAM_UNITS: "metric" | "imperial";
WOLFRAM_MAX_RESULTS: number;
WOLFRAM_CLOUD_API_KEY?: string | undefined;
WOLFRAM_LOCATION?: string | undefined;
WOLFRAM_SCANNERS?: string | undefined;
}, {
WOLFRAM_APP_ID: string;
WOLFRAM_CLOUD_API_KEY?: string | undefined;
WOLFRAM_API_ENDPOINT?: string | undefined;
WOLFRAM_LLM_API_ENDPOINT?: string | undefined;
WOLFRAM_CONVERSATION_ENDPOINT?: string | undefined;
WOLFRAM_OUTPUT_FORMAT?: "plaintext" | "image" | "mathml" | "sound" | "wav" | undefined;
WOLFRAM_TIMEOUT?: number | undefined;
WOLFRAM_UNITS?: "metric" | "imperial" | undefined;
WOLFRAM_LOCATION?: string | undefined;
WOLFRAM_SCANNERS?: string | undefined;
WOLFRAM_MAX_RESULTS?: number | undefined;
}>;
type WolframConfig = z.infer<typeof wolframEnvSchema>;
/**
* Validates Wolfram configuration from runtime environment
*/
declare function validateWolframConfig(runtime: IAgentRuntime): Promise<WolframConfig>;
/**
* Checks if Wolfram configuration is available
*/
declare function isWolframConfigured(runtime: IAgentRuntime): boolean;
declare const WOLFRAM_SERVICE_NAME = "wolfram";
interface AxiosInstance {
get: (url: string, config?: any) => Promise<any>;
defaults: any;
}
declare class WolframService extends Service {
static serviceType: string;
capabilityDescription: string;
wolframConfig: WolframConfig;
client: AxiosInstance;
cache: Map<string, WolframCacheEntry>;
conversationCache: Map<string, string>;
readonly CACHE_TTL = 3600000;
readonly MAX_CACHE_ENTRIES = 200;
constructor(runtime: IAgentRuntime);
initialize(): Promise<void>;
/**
* Validates the API key by making a test request
*/
private validateApiKey;
/**
* Lightweight retry for transient errors (429/5xx)
*/
private getWithRetry;
/**
* Main query method for full Wolfram Alpha results
*/
query(input: string, options?: Partial<WolframQueryOptions>): Promise<WolframAlphaQueryResult>;
/**
* Simple API - returns a single image result
*/
getSimpleAnswer(input: string): Promise<string>;
/**
* Short Answer API - returns a single plaintext result
*/
getShortAnswer(input: string): Promise<WolframShortAnswerResult>;
/**
* Spoken Answer API - returns natural language response
*/
getSpokenAnswer(input: string): Promise<WolframSpokenResult>;
/**
* Conversational API - supports multi-turn conversations
*/
conversationalQuery(input: string, userId: string, maxChars?: number): Promise<WolframConversationResult>;
/**
* Clear conversation context for a user
*/
clearConversation(userId: string): void;
/**
* Specialized method for solving mathematical equations
*/
solveMath(equation: string): Promise<string>;
/**
* Get step-by-step solutions for problems
*/
getStepByStep(problem: string): Promise<string[]>;
/**
* Compute mathematical expressions
*/
compute(expression: string): Promise<string>;
/**
* Get facts about a topic
*/
getFacts(topic: string): Promise<string[]>;
/**
* Analyze data and provide statistical insights
*/
analyzeData(data: string): Promise<WolframAnalysisResult>;
/**
* Format Wolfram Alpha results for display
*/
formatResult(result: WolframAlphaQueryResult): string;
/**
* Cache management methods
*/
private getCached;
private setCached;
private cleanCache;
/**
* Clear all caches
*/
clearCache(): void;
/**
* Get service statistics
*/
getStats(): WolframServiceStats;
/**
* Stop the service and clean up resources
*/
stop(): Promise<void>;
}
declare const wolframPlugin: Plugin;
export { type ActionContext, type ActionResult, WOLFRAM_SERVICE_NAME, WolframAPIEndpoint, type WolframAlphaQueryResult, type WolframAlternative, type WolframAnalysisResult, type WolframAssumption, type WolframAssumptionValue, type WolframCacheEntry, type WolframConfig, type WolframConversationResult, type WolframDefinition, type WolframDelimiters, type WolframDidYouMean, type WolframGeneralization, type WolframImage, type WolframImageMap, type WolframInfo, type WolframLLMOptions, type WolframLink, type WolframPod, type WolframQueryOptions, type WolframRect, type WolframReinterpret, WolframService, type WolframServiceOptions, type WolframServiceStats, type WolframShortAnswerResult, type WolframSimpleResult, type WolframSound, type WolframSource, type WolframSpellcheck, type WolframSpokenResult, type WolframState, type WolframSubpod, type WolframTranslation, type WolframUnits, type WolframWarning, wolframPlugin as default, isWolframConfigured, validateWolframConfig, wolframPlugin };