@letta-ai/letta-code 0.11.0 → 0.11.2-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env npx ts-node
2
+ /**
3
+ * Get Agent Blocks - Retrieves memory blocks from a specific agent
4
+ *
5
+ * Usage:
6
+ * npx ts-node get-agent-blocks.ts --agent-id <agent-id>
7
+ *
8
+ * Output:
9
+ * Raw API response from GET /v1/agents/{id}/core-memory/blocks
10
+ */
11
+
12
+ import type Letta from "@letta-ai/letta-client";
13
+ import { getClient } from "../../../../agent/client";
14
+ import { settingsManager } from "../../../../settings-manager";
15
+
16
+ /**
17
+ * Get memory blocks for a specific agent
18
+ * @param client - Letta client instance
19
+ * @param agentId - The agent ID to get blocks from
20
+ * @returns Array of block objects from the API
21
+ */
22
+ export async function getAgentBlocks(
23
+ client: Letta,
24
+ agentId: string,
25
+ ): Promise<Awaited<ReturnType<typeof client.agents.blocks.list>>> {
26
+ return await client.agents.blocks.list(agentId);
27
+ }
28
+
29
+ function parseArgs(args: string[]): { agentId: string } {
30
+ const agentIdIndex = args.indexOf("--agent-id");
31
+ if (agentIdIndex === -1 || agentIdIndex + 1 >= args.length) {
32
+ throw new Error("Missing required argument: --agent-id <agent-id>");
33
+ }
34
+ return { agentId: args[agentIdIndex + 1] as string };
35
+ }
36
+
37
+ // CLI entry point
38
+ if (require.main === module) {
39
+ (async () => {
40
+ try {
41
+ const { agentId } = parseArgs(process.argv.slice(2));
42
+ await settingsManager.initialize();
43
+ const client = await getClient();
44
+ const result = await getAgentBlocks(client, agentId);
45
+ console.log(JSON.stringify(result, null, 2));
46
+ } catch (error) {
47
+ console.error(
48
+ "Error:",
49
+ error instanceof Error ? error.message : String(error),
50
+ );
51
+ if (
52
+ error instanceof Error &&
53
+ error.message.includes("Missing required argument")
54
+ ) {
55
+ console.error(
56
+ "\nUsage: npx ts-node get-agent-blocks.ts --agent-id <agent-id>",
57
+ );
58
+ }
59
+ process.exit(1);
60
+ }
61
+ })();
62
+ }
@@ -0,0 +1,127 @@
1
+ ---
2
+ name: searching-messages
3
+ description: Search past messages to recall context. Use when you need to remember previous discussions, find specific topics mentioned before, pull up context from earlier in the conversation history, or find which agent discussed a topic.
4
+ ---
5
+
6
+ # Searching Messages
7
+
8
+ This skill helps you search through past conversations to recall context that may have fallen out of your context window.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - User asks "do you remember when we discussed X?"
13
+ - You need context from an earlier conversation
14
+ - User references something from the past that you don't have in context
15
+ - You want to verify what was said before about a topic
16
+ - You need to find which agent discussed a specific topic (use with `finding-agents` skill)
17
+
18
+ ## Script Usage
19
+
20
+ The scripts are located in the `scripts/` subdirectory of this skill. Use the **Skill Directory** path shown above when loading this skill.
21
+
22
+ ```bash
23
+ npx tsx <SKILL_DIR>/scripts/search-messages.ts --query <text> [options]
24
+ ```
25
+
26
+ Replace `<SKILL_DIR>` with the actual path from the `# Skill Directory:` line at the top of this loaded skill.
27
+
28
+ ### Options
29
+
30
+ | Option | Description |
31
+ |--------|-------------|
32
+ | `--query <text>` | Search query (required) |
33
+ | `--mode <mode>` | Search mode: `vector`, `fts`, `hybrid` (default: hybrid) |
34
+ | `--start-date <date>` | Filter messages after this date (ISO format) |
35
+ | `--end-date <date>` | Filter messages before this date (ISO format) |
36
+ | `--limit <n>` | Max results (default: 10) |
37
+ | `--all-agents` | Search all agents, not just current agent |
38
+ | `--agent-id <id>` | Explicit agent ID (for manual testing) |
39
+
40
+ ### Search Modes
41
+
42
+ - **hybrid** (default): Combines vector similarity + full-text search with RRF scoring
43
+ - **vector**: Semantic similarity search (good for conceptual matches)
44
+ - **fts**: Full-text search (good for exact phrases)
45
+
46
+ ## Companion Script: get-messages.ts
47
+
48
+ Use this to expand around a found needle by message ID cursor:
49
+
50
+ ```bash
51
+ npx tsx <SKILL_DIR>/scripts/get-messages.ts [options]
52
+ ```
53
+
54
+ | Option | Description |
55
+ |--------|-------------|
56
+ | `--after <message-id>` | Get messages after this ID (cursor) |
57
+ | `--before <message-id>` | Get messages before this ID (cursor) |
58
+ | `--order <asc\|desc>` | Sort order (default: desc = newest first) |
59
+ | `--limit <n>` | Max results (default: 20) |
60
+ | `--agent-id <id>` | Explicit agent ID |
61
+
62
+ ## Search Strategies
63
+
64
+ ### Strategy 1: Needle + Expand (Recommended)
65
+
66
+ Use when you need full conversation context around a specific topic:
67
+
68
+ 1. **Find the needle** - Search with keywords to discover relevant messages:
69
+ ```bash
70
+ npx tsx <SKILL_DIR>/scripts/search-messages.ts --query "flicker inline approval" --limit 5
71
+ ```
72
+
73
+ 2. **Note the message_id** - Find the most relevant result and copy its `message_id`
74
+
75
+ 3. **Expand before** - Get messages leading up to the needle:
76
+ ```bash
77
+ npx tsx <SKILL_DIR>/scripts/get-messages.ts --before "message-xyz" --limit 10
78
+ ```
79
+
80
+ 4. **Expand after** - Get messages following the needle (use `--order asc` for chronological):
81
+ ```bash
82
+ npx tsx <SKILL_DIR>/scripts/get-messages.ts --after "message-xyz" --order asc --limit 10
83
+ ```
84
+
85
+ ### Strategy 2: Date-Bounded Search
86
+
87
+ Use when you know approximately when something was discussed:
88
+
89
+ ```bash
90
+ npx tsx <SKILL_DIR>/scripts/search-messages.ts --query "topic" --start-date "2025-12-31T00:00:00Z" --end-date "2025-12-31T23:59:59Z" --limit 15
91
+ ```
92
+
93
+ Results are sorted by relevance within the date window.
94
+
95
+ ### Strategy 3: Broad Discovery
96
+
97
+ Use when you're not sure what you're looking for:
98
+
99
+ ```bash
100
+ npx tsx <SKILL_DIR>/scripts/search-messages.ts --query "vague topic" --mode vector --limit 10
101
+ ```
102
+
103
+ Vector mode finds semantically similar messages even without exact keyword matches.
104
+
105
+ ### Strategy 4: Find Which Agent Discussed Something
106
+
107
+ Use with `--all-agents` to search across all agents and identify which one discussed a topic:
108
+
109
+ ```bash
110
+ npx tsx <SKILL_DIR>/scripts/search-messages.ts --query "authentication refactor" --all-agents --limit 10
111
+ ```
112
+
113
+ Results include `agent_id` for each message. Use this to:
114
+ 1. Find the agent that worked on a specific feature
115
+ 2. Identify the right agent to ask follow-up questions
116
+ 3. Cross-reference with the `finding-agents` skill to get agent details
117
+
118
+ **Tip:** Load both `searching-messages` and `finding-agents` skills together when you need to find and identify agents by topic.
119
+
120
+ ## Search Output
121
+
122
+ Returns search results with:
123
+ - `message_id` - Use this for cursor-based expansion
124
+ - `message_type` - `user_message`, `assistant_message`, `reasoning_message`
125
+ - `content` or `reasoning` - The actual message text
126
+ - `created_at` - When the message was sent (ISO format)
127
+ - `agent_id` - Which agent the message belongs to
@@ -0,0 +1,223 @@
1
+ #!/usr/bin/env npx tsx
2
+
3
+ /**
4
+ * Get Messages - Retrieve messages from an agent in chronological order
5
+ *
6
+ * This script is standalone and can be run outside the CLI process.
7
+ * It reads auth from LETTA_API_KEY env var or ~/.letta/settings.json.
8
+ * It reads agent ID from LETTA_AGENT_ID env var or --agent-id arg.
9
+ *
10
+ * Usage:
11
+ * npx tsx get-messages.ts [options]
12
+ *
13
+ * Options:
14
+ * --start-date <date> Filter messages after this date (ISO format)
15
+ * --end-date <date> Filter messages before this date (ISO format)
16
+ * --limit <n> Max results (default: 20)
17
+ * --agent-id <id> Explicit agent ID (overrides LETTA_AGENT_ID env var)
18
+ * --after <message-id> Cursor: get messages after this ID
19
+ * --before <message-id> Cursor: get messages before this ID
20
+ * --order <asc|desc> Sort order (default: desc = newest first)
21
+ *
22
+ * Use this after search-messages.ts to expand around a found needle.
23
+ *
24
+ * Output:
25
+ * Messages in chronological order (filtered by date if specified)
26
+ */
27
+
28
+ import { readFileSync } from "node:fs";
29
+ import { homedir } from "node:os";
30
+ import { join } from "node:path";
31
+ import Letta from "@letta-ai/letta-client";
32
+
33
+ interface GetMessagesOptions {
34
+ startDate?: string;
35
+ endDate?: string;
36
+ limit?: number;
37
+ agentId?: string;
38
+ after?: string;
39
+ before?: string;
40
+ order?: "asc" | "desc";
41
+ }
42
+
43
+ /**
44
+ * Get API key from env var or settings file
45
+ */
46
+ function getApiKey(): string {
47
+ // First check env var (set by CLI's getShellEnv)
48
+ if (process.env.LETTA_API_KEY) {
49
+ return process.env.LETTA_API_KEY;
50
+ }
51
+
52
+ // Fall back to settings file
53
+ const settingsPath = join(homedir(), ".letta", "settings.json");
54
+ try {
55
+ const settings = JSON.parse(readFileSync(settingsPath, "utf-8"));
56
+ if (settings.env?.LETTA_API_KEY) {
57
+ return settings.env.LETTA_API_KEY;
58
+ }
59
+ } catch {
60
+ // Settings file doesn't exist or is invalid
61
+ }
62
+
63
+ throw new Error(
64
+ "No LETTA_API_KEY found. Set the env var or run the Letta CLI to authenticate.",
65
+ );
66
+ }
67
+
68
+ /**
69
+ * Get agent ID from CLI arg, env var, or throw
70
+ */
71
+ function getAgentId(cliArg?: string): string {
72
+ // CLI arg takes precedence
73
+ if (cliArg) return cliArg;
74
+
75
+ // Then env var (set by CLI's getShellEnv)
76
+ if (process.env.LETTA_AGENT_ID) {
77
+ return process.env.LETTA_AGENT_ID;
78
+ }
79
+
80
+ throw new Error(
81
+ "No agent ID provided. Use --agent-id or ensure LETTA_AGENT_ID env var is set.",
82
+ );
83
+ }
84
+
85
+ /**
86
+ * Create a Letta client with auth from env/settings
87
+ */
88
+ function createClient(): Letta {
89
+ return new Letta({ apiKey: getApiKey() });
90
+ }
91
+
92
+ /**
93
+ * Get messages from an agent, optionally filtered by date range
94
+ * @param client - Letta client instance
95
+ * @param options - Options for filtering
96
+ * @returns Array of messages in chronological order
97
+ */
98
+ export async function getMessages(
99
+ client: Letta,
100
+ options: GetMessagesOptions = {},
101
+ ): Promise<unknown[]> {
102
+ const agentId = getAgentId(options.agentId);
103
+ const limit = options.limit ?? 20;
104
+
105
+ // Fetch messages from the agent
106
+ const response = await client.agents.messages.list(agentId, {
107
+ limit,
108
+ after: options.after,
109
+ before: options.before,
110
+ order: options.order,
111
+ });
112
+
113
+ const messages = response.items ?? [];
114
+
115
+ // Client-side date filtering if specified
116
+ if (options.startDate || options.endDate) {
117
+ const startTime = options.startDate
118
+ ? new Date(options.startDate).getTime()
119
+ : 0;
120
+ const endTime = options.endDate
121
+ ? new Date(options.endDate).getTime()
122
+ : Number.POSITIVE_INFINITY;
123
+
124
+ const filtered = messages.filter((msg) => {
125
+ // Messages use 'date' field, not 'created_at'
126
+ if (!("date" in msg) || !msg.date) return true;
127
+ const msgTime = new Date(msg.date).getTime();
128
+ return msgTime >= startTime && msgTime <= endTime;
129
+ });
130
+
131
+ // Sort chronologically (oldest first)
132
+ return filtered.sort((a, b) => {
133
+ const aDate = "date" in a && a.date ? new Date(a.date).getTime() : 0;
134
+ const bDate = "date" in b && b.date ? new Date(b.date).getTime() : 0;
135
+ return aDate - bDate;
136
+ });
137
+ }
138
+
139
+ // Sort chronologically (oldest first)
140
+ return [...messages].sort((a, b) => {
141
+ const aDate = "date" in a && a.date ? new Date(a.date).getTime() : 0;
142
+ const bDate = "date" in b && b.date ? new Date(b.date).getTime() : 0;
143
+ return aDate - bDate;
144
+ });
145
+ }
146
+
147
+ function parseArgs(args: string[]): GetMessagesOptions {
148
+ const options: GetMessagesOptions = {};
149
+
150
+ const startDateIndex = args.indexOf("--start-date");
151
+ if (startDateIndex !== -1 && startDateIndex + 1 < args.length) {
152
+ options.startDate = args[startDateIndex + 1];
153
+ }
154
+
155
+ const endDateIndex = args.indexOf("--end-date");
156
+ if (endDateIndex !== -1 && endDateIndex + 1 < args.length) {
157
+ options.endDate = args[endDateIndex + 1];
158
+ }
159
+
160
+ const limitIndex = args.indexOf("--limit");
161
+ if (limitIndex !== -1 && limitIndex + 1 < args.length) {
162
+ const limit = Number.parseInt(args[limitIndex + 1] as string, 10);
163
+ if (!Number.isNaN(limit)) {
164
+ options.limit = limit;
165
+ }
166
+ }
167
+
168
+ const agentIdIndex = args.indexOf("--agent-id");
169
+ if (agentIdIndex !== -1 && agentIdIndex + 1 < args.length) {
170
+ options.agentId = args[agentIdIndex + 1];
171
+ }
172
+
173
+ const afterIndex = args.indexOf("--after");
174
+ if (afterIndex !== -1 && afterIndex + 1 < args.length) {
175
+ options.after = args[afterIndex + 1];
176
+ }
177
+
178
+ const beforeIndex = args.indexOf("--before");
179
+ if (beforeIndex !== -1 && beforeIndex + 1 < args.length) {
180
+ options.before = args[beforeIndex + 1];
181
+ }
182
+
183
+ const orderIndex = args.indexOf("--order");
184
+ if (orderIndex !== -1 && orderIndex + 1 < args.length) {
185
+ const order = args[orderIndex + 1] as string;
186
+ if (order === "asc" || order === "desc") {
187
+ options.order = order;
188
+ }
189
+ }
190
+
191
+ return options;
192
+ }
193
+
194
+ // CLI entry point - check if this file is being run directly
195
+ const isMainModule = import.meta.url === `file://${process.argv[1]}`;
196
+ if (isMainModule) {
197
+ (async () => {
198
+ try {
199
+ const options = parseArgs(process.argv.slice(2));
200
+ const client = createClient();
201
+ const result = await getMessages(client, options);
202
+ console.log(JSON.stringify(result, null, 2));
203
+ } catch (error) {
204
+ console.error(
205
+ "Error:",
206
+ error instanceof Error ? error.message : String(error),
207
+ );
208
+ console.error(`
209
+ Usage: npx tsx get-messages.ts [options]
210
+
211
+ Options:
212
+ --after <message-id> Cursor: get messages after this ID
213
+ --before <message-id> Cursor: get messages before this ID
214
+ --order <asc|desc> Sort order (default: desc = newest first)
215
+ --limit <n> Max results (default: 20)
216
+ --agent-id <id> Explicit agent ID (overrides LETTA_AGENT_ID env var)
217
+ --start-date <date> Client-side filter: after this date (ISO format)
218
+ --end-date <date> Client-side filter: before this date (ISO format)
219
+ `);
220
+ process.exit(1);
221
+ }
222
+ })();
223
+ }
@@ -0,0 +1,193 @@
1
+ #!/usr/bin/env npx tsx
2
+
3
+ /**
4
+ * Search Messages - Search past conversations with vector/FTS search
5
+ *
6
+ * This script is standalone and can be run outside the CLI process.
7
+ * It reads auth from LETTA_API_KEY env var or ~/.letta/settings.json.
8
+ * It reads agent ID from LETTA_AGENT_ID env var or --agent-id arg.
9
+ *
10
+ * Usage:
11
+ * npx tsx search-messages.ts --query <text> [options]
12
+ *
13
+ * Options:
14
+ * --query <text> Search query (required)
15
+ * --mode <mode> Search mode: vector, fts, hybrid (default: hybrid)
16
+ * --start-date <date> Filter messages after this date (ISO format)
17
+ * --end-date <date> Filter messages before this date (ISO format)
18
+ * --limit <n> Max results (default: 10)
19
+ * --all-agents Search all agents, not just current agent
20
+ * --agent-id <id> Explicit agent ID (overrides LETTA_AGENT_ID env var)
21
+ *
22
+ * Output:
23
+ * Raw API response with search results
24
+ */
25
+
26
+ import { readFileSync } from "node:fs";
27
+ import { homedir } from "node:os";
28
+ import { join } from "node:path";
29
+ import Letta from "@letta-ai/letta-client";
30
+
31
+ interface SearchMessagesOptions {
32
+ query: string;
33
+ mode?: "vector" | "fts" | "hybrid";
34
+ startDate?: string;
35
+ endDate?: string;
36
+ limit?: number;
37
+ allAgents?: boolean;
38
+ agentId?: string;
39
+ }
40
+
41
+ /**
42
+ * Get API key from env var or settings file
43
+ */
44
+ function getApiKey(): string {
45
+ // First check env var (set by CLI's getShellEnv)
46
+ if (process.env.LETTA_API_KEY) {
47
+ return process.env.LETTA_API_KEY;
48
+ }
49
+
50
+ // Fall back to settings file
51
+ const settingsPath = join(homedir(), ".letta", "settings.json");
52
+ try {
53
+ const settings = JSON.parse(readFileSync(settingsPath, "utf-8"));
54
+ if (settings.env?.LETTA_API_KEY) {
55
+ return settings.env.LETTA_API_KEY;
56
+ }
57
+ } catch {
58
+ // Settings file doesn't exist or is invalid
59
+ }
60
+
61
+ throw new Error(
62
+ "No LETTA_API_KEY found. Set the env var or run the Letta CLI to authenticate.",
63
+ );
64
+ }
65
+
66
+ /**
67
+ * Get agent ID from CLI arg, env var, or throw
68
+ */
69
+ function getAgentId(cliArg?: string): string {
70
+ // CLI arg takes precedence
71
+ if (cliArg) return cliArg;
72
+
73
+ // Then env var (set by CLI's getShellEnv)
74
+ if (process.env.LETTA_AGENT_ID) {
75
+ return process.env.LETTA_AGENT_ID;
76
+ }
77
+
78
+ throw new Error(
79
+ "No agent ID provided. Use --agent-id or ensure LETTA_AGENT_ID env var is set.",
80
+ );
81
+ }
82
+
83
+ /**
84
+ * Create a Letta client with auth from env/settings
85
+ */
86
+ function createClient(): Letta {
87
+ return new Letta({ apiKey: getApiKey() });
88
+ }
89
+
90
+ /**
91
+ * Search messages in past conversations
92
+ * @param client - Letta client instance
93
+ * @param options - Search options
94
+ * @returns Array of search results with scores
95
+ */
96
+ export async function searchMessages(
97
+ client: Letta,
98
+ options: SearchMessagesOptions,
99
+ ): Promise<Awaited<ReturnType<typeof client.messages.search>>> {
100
+ // Default to current agent unless --all-agents is specified
101
+ let agentId: string | undefined;
102
+ if (!options.allAgents) {
103
+ agentId = getAgentId(options.agentId);
104
+ }
105
+
106
+ return await client.messages.search({
107
+ query: options.query,
108
+ agent_id: agentId,
109
+ search_mode: options.mode ?? "hybrid",
110
+ start_date: options.startDate,
111
+ end_date: options.endDate,
112
+ limit: options.limit ?? 10,
113
+ });
114
+ }
115
+
116
+ function parseArgs(args: string[]): SearchMessagesOptions {
117
+ const queryIndex = args.indexOf("--query");
118
+ if (queryIndex === -1 || queryIndex + 1 >= args.length) {
119
+ throw new Error("Missing required argument: --query <text>");
120
+ }
121
+
122
+ const options: SearchMessagesOptions = {
123
+ query: args[queryIndex + 1] as string,
124
+ };
125
+
126
+ const modeIndex = args.indexOf("--mode");
127
+ if (modeIndex !== -1 && modeIndex + 1 < args.length) {
128
+ const mode = args[modeIndex + 1] as string;
129
+ if (mode === "vector" || mode === "fts" || mode === "hybrid") {
130
+ options.mode = mode;
131
+ }
132
+ }
133
+
134
+ const startDateIndex = args.indexOf("--start-date");
135
+ if (startDateIndex !== -1 && startDateIndex + 1 < args.length) {
136
+ options.startDate = args[startDateIndex + 1];
137
+ }
138
+
139
+ const endDateIndex = args.indexOf("--end-date");
140
+ if (endDateIndex !== -1 && endDateIndex + 1 < args.length) {
141
+ options.endDate = args[endDateIndex + 1];
142
+ }
143
+
144
+ const limitIndex = args.indexOf("--limit");
145
+ if (limitIndex !== -1 && limitIndex + 1 < args.length) {
146
+ const limit = Number.parseInt(args[limitIndex + 1] as string, 10);
147
+ if (!Number.isNaN(limit)) {
148
+ options.limit = limit;
149
+ }
150
+ }
151
+
152
+ if (args.includes("--all-agents")) {
153
+ options.allAgents = true;
154
+ }
155
+
156
+ const agentIdIndex = args.indexOf("--agent-id");
157
+ if (agentIdIndex !== -1 && agentIdIndex + 1 < args.length) {
158
+ options.agentId = args[agentIdIndex + 1];
159
+ }
160
+
161
+ return options;
162
+ }
163
+
164
+ // CLI entry point - check if this file is being run directly
165
+ const isMainModule = import.meta.url === `file://${process.argv[1]}`;
166
+ if (isMainModule) {
167
+ (async () => {
168
+ try {
169
+ const options = parseArgs(process.argv.slice(2));
170
+ const client = createClient();
171
+ const result = await searchMessages(client, options);
172
+ console.log(JSON.stringify(result, null, 2));
173
+ } catch (error) {
174
+ console.error(
175
+ "Error:",
176
+ error instanceof Error ? error.message : String(error),
177
+ );
178
+ console.error(`
179
+ Usage: npx tsx search-messages.ts --query <text> [options]
180
+
181
+ Options:
182
+ --query <text> Search query (required)
183
+ --mode <mode> Search mode: vector, fts, hybrid (default: hybrid)
184
+ --start-date <date> Filter messages after this date (ISO format)
185
+ --end-date <date> Filter messages before this date (ISO format)
186
+ --limit <n> Max results (default: 10)
187
+ --all-agents Search all agents, not just current agent
188
+ --agent-id <id> Explicit agent ID (overrides LETTA_AGENT_ID env var)
189
+ `);
190
+ process.exit(1);
191
+ }
192
+ })();
193
+ }