MCPcopy Index your code
hub / github.com/microsoft/TypeChat / createJsonMathAgent

Function createJsonMathAgent

typescript/examples/multiSchema/src/agent.ts:62–111  ·  view source on GitHub ↗
(name: string, description: string,
     model: TypeChatLanguageModel,
     schema: string)

Source from the content-addressed store, hash-verified

60}
61
62export function createJsonMathAgent<T extends object>
63 (name: string, description: string,
64 model: TypeChatLanguageModel,
65 schema: string): MathAgent<T>
66{
67 async function _handleCall(func: string, args: any[]): Promise<unknown> {
68 // implementation goes here
69 console.log(`${func}(${args.map(arg => typeof arg === "number" ? arg : JSON.stringify(arg, undefined, 2)).join(", ")})`);
70 switch (func) {
71 case "add":
72 return args[0] + args[1];
73 case "sub":
74 return args[0] - args[1];
75 case "mul":
76 return args[0] * args[1];
77 case "div":
78 return args[0] / args[1];
79 case "neg":
80 return -args[0];
81 case "id":
82 return args[0];
83 }
84 return NaN;
85 }
86
87 const _translator = createProgramTranslator(model, schema);
88 const mathAgent : MathAgent<T> = {
89 _translator,
90 name: name,
91 description: description,
92 handleMessage: _handleMessage,
93 };
94
95 return mathAgent;
96
97 async function _handleMessage(request: string): Promise<Result<T>> {
98 const response = await _translator.translate(request);
99 if (!response.success) {
100 console.log(response.message);
101 return response;
102 }
103
104 const program = response.data;
105 console.log(getData(createModuleTextFromProgram(program)));
106 console.log("Running program:");
107 const result = await evaluateJsonProgram(program, _handleCall);
108 console.log(`Result: ${typeof result === "number" ? result : "Error"}`);
109 return success("Successful evaluation" as any);
110 }
111}

Callers 1

main.tsFile · 0.90

Calls 1

createProgramTranslatorFunction · 0.90

Tested by

no test coverage detected