+26
-25
utils/messageAgent.ts
+26
-25
utils/messageAgent.ts
···
1
-
import { LettaClient } from "@letta-ai/letta-client";
1
+
import Letta from "@letta-ai/letta-client";
2
2
import { agentContext } from "./agentContext.ts";
3
3
// Helper function to format tool arguments as inline key-value pairs
4
4
const formatArgsInline = (args: unknown): string => {
···
28
28
return `${str.slice(0, maxLength)}... (truncated, ${str.length} total chars)`;
29
29
};
30
30
31
-
export const client = new LettaClient({
32
-
token: Deno.env.get("LETTA_API_KEY"),
33
-
project: Deno.env.get("LETTA_PROJECT_NAME"),
31
+
export const client = new Letta({
32
+
apiKey: Deno.env.get("LETTA_API_KEY"),
33
+
// @ts-ignore: Letta SDK type definition might be slightly off or expecting different casing
34
+
projectId: Deno.env.get("LETTA_PROJECT_ID"),
34
35
});
35
36
36
37
export const messageAgent = async (prompt: string) => {
37
38
const agent = Deno.env.get("LETTA_AGENT_ID");
38
39
39
40
if (agent) {
40
-
const reachAgent = await client.agents.messages.createStream(agent, {
41
+
const reachAgent = await client.agents.messages.stream(agent, {
41
42
messages: [
42
43
{
43
-
role: "user",
44
-
content: [
45
-
{
46
-
type: "text",
47
-
text: prompt,
48
-
},
49
-
],
44
+
role: "system",
45
+
content: prompt,
50
46
},
51
47
],
52
-
streamTokens: true,
48
+
stream_tokens: true,
53
49
});
54
50
55
51
for await (const response of reachAgent) {
56
-
if (response.messageType === "reasoning_message") {
52
+
if (response.message_type === "reasoning_message") {
57
53
// console.log(`💭 reasoning…`);
58
-
} else if (response.messageType === "assistant_message") {
54
+
} else if (response.message_type === "assistant_message") {
59
55
console.log(`💬 ${agentContext.agentBskyName}: ${response.content}`);
60
-
} else if (response.messageType === "tool_call_message") {
61
-
const formattedArgs = formatArgsInline(response.toolCall.arguments);
62
-
console.log(
63
-
`🗜️ tool called: ${response.toolCall.name} with args: ${formattedArgs}`,
64
-
);
65
-
} else if (response.messageType === "tool_return_message") {
66
-
const toolReturn = response.toolReturn;
56
+
} else if (response.message_type === "tool_call_message") {
57
+
if (
58
+
Array.isArray(response.tool_calls) && response.tool_calls.length > 0
59
+
) {
60
+
const toolCall = response.tool_calls[0];
61
+
const formattedArgs = formatArgsInline(toolCall.arguments);
62
+
console.log(
63
+
`🗜️ tool called: ${toolCall.name} with args: ${formattedArgs}`,
64
+
);
65
+
}
66
+
} else if (response.message_type === "tool_return_message") {
67
+
const toolReturn = response.tool_returns;
67
68
const returnStr = typeof toolReturn === "string"
68
69
? toolReturn
69
70
: JSON.stringify(toolReturn);
70
71
console.log(`🔧 tool response: ${truncateString(returnStr)}`);
71
-
} else if (response.messageType === "usage_statistics") {
72
-
console.log(`🔢 total steps: ${response.stepCount}`);
73
-
} else if (response.messageType === "hidden_reasoning_message") {
72
+
} else if (response.message_type === "usage_statistics") {
73
+
console.log(`🔢 total steps: ${response.step_count}`);
74
+
} else if (response.message_type === "hidden_reasoning_message") {
74
75
console.log(`hidden reasoning…`);
75
76
}
76
77
}