@letta-ai/letta-code 0.11.1 → 0.11.2-next.2

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,200 @@
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 { createRequire } from "node:module";
28
+ import { homedir } from "node:os";
29
+ import { join } from "node:path";
30
+
31
+ // Use createRequire for @letta-ai/letta-client so NODE_PATH is respected
32
+ // (ES module imports don't respect NODE_PATH, but require does)
33
+ const require = createRequire(import.meta.url);
34
+ const Letta = require("@letta-ai/letta-client")
35
+ .default as typeof import("@letta-ai/letta-client").default;
36
+ type LettaClient = InstanceType<typeof Letta>;
37
+
38
+ interface SearchMessagesOptions {
39
+ query: string;
40
+ mode?: "vector" | "fts" | "hybrid";
41
+ startDate?: string;
42
+ endDate?: string;
43
+ limit?: number;
44
+ allAgents?: boolean;
45
+ agentId?: string;
46
+ }
47
+
48
+ /**
49
+ * Get API key from env var or settings file
50
+ */
51
+ function getApiKey(): string {
52
+ // First check env var (set by CLI's getShellEnv)
53
+ if (process.env.LETTA_API_KEY) {
54
+ return process.env.LETTA_API_KEY;
55
+ }
56
+
57
+ // Fall back to settings file
58
+ const settingsPath = join(homedir(), ".letta", "settings.json");
59
+ try {
60
+ const settings = JSON.parse(readFileSync(settingsPath, "utf-8"));
61
+ if (settings.env?.LETTA_API_KEY) {
62
+ return settings.env.LETTA_API_KEY;
63
+ }
64
+ } catch {
65
+ // Settings file doesn't exist or is invalid
66
+ }
67
+
68
+ throw new Error(
69
+ "No LETTA_API_KEY found. Set the env var or run the Letta CLI to authenticate.",
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Get agent ID from CLI arg, env var, or throw
75
+ */
76
+ function getAgentId(cliArg?: string): string {
77
+ // CLI arg takes precedence
78
+ if (cliArg) return cliArg;
79
+
80
+ // Then env var (set by CLI's getShellEnv)
81
+ if (process.env.LETTA_AGENT_ID) {
82
+ return process.env.LETTA_AGENT_ID;
83
+ }
84
+
85
+ throw new Error(
86
+ "No agent ID provided. Use --agent-id or ensure LETTA_AGENT_ID env var is set.",
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Create a Letta client with auth from env/settings
92
+ */
93
+ function createClient(): LettaClient {
94
+ return new Letta({ apiKey: getApiKey() });
95
+ }
96
+
97
+ /**
98
+ * Search messages in past conversations
99
+ * @param client - Letta client instance
100
+ * @param options - Search options
101
+ * @returns Array of search results with scores
102
+ */
103
+ export async function searchMessages(
104
+ client: LettaClient,
105
+ options: SearchMessagesOptions,
106
+ ): Promise<Awaited<ReturnType<typeof client.messages.search>>> {
107
+ // Default to current agent unless --all-agents is specified
108
+ let agentId: string | undefined;
109
+ if (!options.allAgents) {
110
+ agentId = getAgentId(options.agentId);
111
+ }
112
+
113
+ return await client.messages.search({
114
+ query: options.query,
115
+ agent_id: agentId,
116
+ search_mode: options.mode ?? "hybrid",
117
+ start_date: options.startDate,
118
+ end_date: options.endDate,
119
+ limit: options.limit ?? 10,
120
+ });
121
+ }
122
+
123
+ function parseArgs(args: string[]): SearchMessagesOptions {
124
+ const queryIndex = args.indexOf("--query");
125
+ if (queryIndex === -1 || queryIndex + 1 >= args.length) {
126
+ throw new Error("Missing required argument: --query <text>");
127
+ }
128
+
129
+ const options: SearchMessagesOptions = {
130
+ query: args[queryIndex + 1] as string,
131
+ };
132
+
133
+ const modeIndex = args.indexOf("--mode");
134
+ if (modeIndex !== -1 && modeIndex + 1 < args.length) {
135
+ const mode = args[modeIndex + 1] as string;
136
+ if (mode === "vector" || mode === "fts" || mode === "hybrid") {
137
+ options.mode = mode;
138
+ }
139
+ }
140
+
141
+ const startDateIndex = args.indexOf("--start-date");
142
+ if (startDateIndex !== -1 && startDateIndex + 1 < args.length) {
143
+ options.startDate = args[startDateIndex + 1];
144
+ }
145
+
146
+ const endDateIndex = args.indexOf("--end-date");
147
+ if (endDateIndex !== -1 && endDateIndex + 1 < args.length) {
148
+ options.endDate = args[endDateIndex + 1];
149
+ }
150
+
151
+ const limitIndex = args.indexOf("--limit");
152
+ if (limitIndex !== -1 && limitIndex + 1 < args.length) {
153
+ const limit = Number.parseInt(args[limitIndex + 1] as string, 10);
154
+ if (!Number.isNaN(limit)) {
155
+ options.limit = limit;
156
+ }
157
+ }
158
+
159
+ if (args.includes("--all-agents")) {
160
+ options.allAgents = true;
161
+ }
162
+
163
+ const agentIdIndex = args.indexOf("--agent-id");
164
+ if (agentIdIndex !== -1 && agentIdIndex + 1 < args.length) {
165
+ options.agentId = args[agentIdIndex + 1];
166
+ }
167
+
168
+ return options;
169
+ }
170
+
171
+ // CLI entry point - check if this file is being run directly
172
+ const isMainModule = import.meta.url === `file://${process.argv[1]}`;
173
+ if (isMainModule) {
174
+ (async () => {
175
+ try {
176
+ const options = parseArgs(process.argv.slice(2));
177
+ const client = createClient();
178
+ const result = await searchMessages(client, options);
179
+ console.log(JSON.stringify(result, null, 2));
180
+ } catch (error) {
181
+ console.error(
182
+ "Error:",
183
+ error instanceof Error ? error.message : String(error),
184
+ );
185
+ console.error(`
186
+ Usage: npx tsx search-messages.ts --query <text> [options]
187
+
188
+ Options:
189
+ --query <text> Search query (required)
190
+ --mode <mode> Search mode: vector, fts, hybrid (default: hybrid)
191
+ --start-date <date> Filter messages after this date (ISO format)
192
+ --end-date <date> Filter messages before this date (ISO format)
193
+ --limit <n> Max results (default: 10)
194
+ --all-agents Search all agents, not just current agent
195
+ --agent-id <id> Explicit agent ID (overrides LETTA_AGENT_ID env var)
196
+ `);
197
+ process.exit(1);
198
+ }
199
+ })();
200
+ }