@sentry/junior-memory 0.76.0

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.
package/src/agent.ts ADDED
@@ -0,0 +1,437 @@
1
+ import type { PluginModel } from "@sentry/junior-plugin-api";
2
+ import { z } from "zod";
3
+ import { memoryRuntimeContextSchema } from "./types";
4
+
5
+ const memoryTargetSchema = z.enum(["requester", "conversation"]);
6
+ const memoryKindSchema = z.enum(["preference", "procedure", "fact"]);
7
+ const memoryRejectReasonSchema = z.enum([
8
+ "not_public_shareable",
9
+ "secret_or_credential",
10
+ "sensitive_personal",
11
+ "third_party_personal",
12
+ "vague_or_not_self_contained",
13
+ "not_durable",
14
+ "assistant_or_system_detail",
15
+ "unsupported_scope",
16
+ ]);
17
+ const createMemoryRequestSchema = z
18
+ .object({
19
+ content: z.string().min(1),
20
+ expiresAtMs: z.number().finite().optional(),
21
+ runtimeContext: memoryRuntimeContextSchema,
22
+ sourceContext: z
23
+ .object({
24
+ currentUserText: z.string().min(1).optional(),
25
+ })
26
+ .strict()
27
+ .optional(),
28
+ })
29
+ .strict();
30
+ const extractSessionRequestSchema = z
31
+ .object({
32
+ existingMemories: z
33
+ .array(
34
+ z
35
+ .object({
36
+ content: z.string().min(1),
37
+ })
38
+ .strict(),
39
+ )
40
+ .max(10)
41
+ .default([]),
42
+ runtimeContext: memoryRuntimeContextSchema,
43
+ transcript: z
44
+ .array(
45
+ z.discriminatedUnion("type", [
46
+ z
47
+ .object({
48
+ type: z.literal("message"),
49
+ role: z.enum(["user", "assistant"]),
50
+ text: z.string().min(1),
51
+ })
52
+ .strict(),
53
+ z
54
+ .object({
55
+ type: z.literal("toolResult"),
56
+ toolName: z.string().min(1),
57
+ isError: z.boolean(),
58
+ text: z.string().min(1),
59
+ })
60
+ .strict(),
61
+ ]),
62
+ )
63
+ .min(1),
64
+ })
65
+ .strict();
66
+
67
+ const expiresAtMsSchema = z
68
+ .number()
69
+ .finite()
70
+ .nullable()
71
+ .describe(
72
+ "Expiration timestamp when the fact should expire, otherwise null.",
73
+ );
74
+ const memoryReviewDecisionSchema = z.discriminatedUnion("decision", [
75
+ z
76
+ .object({
77
+ decision: z.literal("store"),
78
+ target: memoryTargetSchema,
79
+ content: z.string().min(1),
80
+ expiresAtMs: z.number().finite().optional(),
81
+ })
82
+ .strict(),
83
+ z
84
+ .object({
85
+ decision: z.literal("reject"),
86
+ reason: memoryRejectReasonSchema,
87
+ })
88
+ .strict(),
89
+ ]);
90
+ const memoryReviewResponseSchema = z.discriminatedUnion("decision", [
91
+ z
92
+ .object({
93
+ decision: z.literal("store"),
94
+ kind: memoryKindSchema.describe(
95
+ "Use preference only for requester-owned personal preferences, opinions, habits, or workflows. Use procedure for reusable task or process instructions. Use fact for shared project, channel, operational, or runbook knowledge.",
96
+ ),
97
+ canonicalFact: z
98
+ .string()
99
+ .min(1)
100
+ .describe(
101
+ "Stored memory text. It must be self-contained and must not include requester names, requester/user labels, source labels, or first- or second-person wording.",
102
+ ),
103
+ expiresAtMs: expiresAtMsSchema,
104
+ })
105
+ .strict(),
106
+ z
107
+ .object({
108
+ decision: z.literal("reject"),
109
+ reason: memoryRejectReasonSchema,
110
+ })
111
+ .strict(),
112
+ ]);
113
+ const extractedMemorySchema = z
114
+ .object({
115
+ kind: memoryKindSchema.describe(
116
+ "Use preference only for requester-owned personal preferences, opinions, habits, or workflows. Use procedure for reusable task or process instructions. Use fact for shared project, channel, operational, or runbook knowledge.",
117
+ ),
118
+ canonicalFact: z
119
+ .string()
120
+ .min(1)
121
+ .describe(
122
+ "Stored memory text as one self-contained fact. It must not include requester names, requester/user labels, source labels, or first- or second-person wording.",
123
+ ),
124
+ expiresAtMs: expiresAtMsSchema,
125
+ })
126
+ .strict();
127
+ const extractMemoriesResponseSchema = z
128
+ .object({
129
+ memories: z
130
+ .array(extractedMemorySchema)
131
+ .max(5)
132
+ .describe(
133
+ "Accepted public/shareable durable memories from the completed run. Return one object per distinct source assertion and classify it with kind.",
134
+ ),
135
+ })
136
+ .strict();
137
+
138
+ type MemoryReviewResponse = z.output<typeof memoryReviewResponseSchema>;
139
+ type ExtractMemoriesResponse = z.output<typeof extractMemoriesResponseSchema>;
140
+
141
+ export type MemoryTarget = z.output<typeof memoryTargetSchema>;
142
+ type MemoryKind = z.output<typeof memoryKindSchema>;
143
+
144
+ export type MemoryReview = z.output<typeof memoryReviewDecisionSchema>;
145
+
146
+ export type CreateMemoryRequest = z.output<typeof createMemoryRequestSchema>;
147
+ export type ExtractSessionRequest = z.output<
148
+ typeof extractSessionRequestSchema
149
+ >;
150
+ export interface ExtractedMemory {
151
+ content: string;
152
+ expiresAtMs: number | null;
153
+ target: MemoryTarget;
154
+ }
155
+
156
+ export interface MemoryAgent {
157
+ extractSessionMemories(
158
+ request: ExtractSessionRequest,
159
+ ): Promise<ExtractedMemory[]> | ExtractedMemory[];
160
+ reviewCreateRequest(
161
+ request: CreateMemoryRequest,
162
+ ): Promise<MemoryReview> | MemoryReview;
163
+ }
164
+
165
+ const MEMORY_REVIEW_SYSTEM = [
166
+ "You are Junior's memory review agent.",
167
+ "Review one memory candidate and return one structured review decision.",
168
+ "Store only public/shareable, self-contained facts that are useful beyond this turn.",
169
+ "Reject secrets, credentials, private or sensitive personal details, gossip, speculative claims about other people, assistant/system implementation details, vague references, and low-durability chatter.",
170
+ "Use the runtime context only for authority and scope; do not accept model-provided actor ids, scope ids, aliases, or arbitrary subjects.",
171
+ ].join("\n");
172
+ const MEMORY_EXTRACTION_SYSTEM = [
173
+ "You are Junior's passive memory extraction agent. Return only structured memories worth storing.",
174
+ "Use the completed run transcript as source evidence, including user-authored messages and tool results.",
175
+ "Assistant text is context for interpreting the run, not independent evidence for new facts.",
176
+ "Reject secrets, credentials, private or sensitive personal details, gossip, speculative claims about other people, assistant/system implementation details, vague references, and low-durability chatter.",
177
+ "If no public, durable, self-contained memory remains after rewriting, return an empty memories array.",
178
+ ].join("\n");
179
+ const CANONICAL_CONTENT_RULES = [
180
+ "- Stored memory text must be a rewritten fact, not copied user wording or a sentence about who said it.",
181
+ "- Store the minimum useful assertion supported by source evidence; do not add adjacent steps, caveats, or generalized advice.",
182
+ "- Do not return both concise and expanded variants of the same source assertion; keep the shortest self-contained canonical memory.",
183
+ "- Put ownership in structured fields, not prose.",
184
+ "- For requester memories, omit the subject and write a stable fact such as 'Prefers X', 'Uses Y', or 'Thinks Z'.",
185
+ "- Drop perspective/provenance markers while preserving useful context.",
186
+ "- Remove requester names, display names, requester/user labels, first- or second-person wording, thread labels, channel labels, and source labels.",
187
+ ];
188
+
189
+ function targetForKind(kind: MemoryKind): MemoryTarget {
190
+ if (kind === "preference") {
191
+ return "requester";
192
+ }
193
+ return "conversation";
194
+ }
195
+
196
+ function escapeXml(value: string): string {
197
+ return value
198
+ .replaceAll("&", "&amp;")
199
+ .replaceAll("<", "&lt;")
200
+ .replaceAll(">", "&gt;");
201
+ }
202
+
203
+ function runtimeDescription(
204
+ request: Pick<CreateMemoryRequest, "expiresAtMs" | "runtimeContext">,
205
+ ): string {
206
+ const runtime = request.runtimeContext;
207
+ const requester =
208
+ runtime.requester?.platform === "slack"
209
+ ? `slack:${runtime.requester.teamId}:${runtime.requester.userId}`
210
+ : runtime.requester?.platform === "local"
211
+ ? `local:${runtime.requester.userId}`
212
+ : "none";
213
+ const source =
214
+ runtime.source.platform === "slack"
215
+ ? `slack:${runtime.source.teamId}:${runtime.source.channelId}`
216
+ : `local:${runtime.source.conversationId}`;
217
+ const lines = [
218
+ `- requester: ${escapeXml(requester)}`,
219
+ `- source: ${escapeXml(source)}`,
220
+ `- has_conversation: ${runtime.conversationId ? "true" : "false"}`,
221
+ `- expires_at: ${
222
+ request.expiresAtMs === undefined
223
+ ? "never"
224
+ : escapeXml(new Date(request.expiresAtMs).toISOString())
225
+ }`,
226
+ ];
227
+ return ["<runtime>", ...lines, "</runtime>"].join("\n");
228
+ }
229
+
230
+ function sourceContext(request: CreateMemoryRequest): string | undefined {
231
+ const currentUserText = request.sourceContext?.currentUserText?.trim();
232
+ if (!currentUserText) {
233
+ return undefined;
234
+ }
235
+ return [
236
+ "<source-context>",
237
+ "The current user-authored text is source evidence for explicit memory requests. Use it to recover the concrete fact when the candidate is incomplete, vague, or over-personalized. Store only rewritten, self-contained memory content.",
238
+ "<current-user-message>",
239
+ escapeXml(currentUserText),
240
+ "</current-user-message>",
241
+ "</source-context>",
242
+ ].join("\n");
243
+ }
244
+
245
+ function existingMemoriesContext(request: ExtractSessionRequest): string {
246
+ if (request.existingMemories.length === 0) {
247
+ return "<existing-memories>[]</existing-memories>";
248
+ }
249
+ return [
250
+ "<existing-memories>",
251
+ "Use these only to skip memories that are already covered or semantically redundant. They are not source evidence for new memories.",
252
+ escapeXml(JSON.stringify(request.existingMemories)),
253
+ "</existing-memories>",
254
+ ].join("\n");
255
+ }
256
+
257
+ function memoryKindsContext(): string {
258
+ return [
259
+ "<memory-kinds>",
260
+ "- preference: a durable first-person personal preference, opinion, habit, or workflow owned by the current requester. Stored as requester memory.",
261
+ "- procedure: reusable instructions for how a task, lookup, investigation, process, triage flow, or runbook should be done. Store the method, source-of-truth, prerequisite, or decision path when it took effort to discover. Stored as conversation memory.",
262
+ "- fact: stable shared project, channel, operational, or runbook knowledge that is not a personal requester preference. Direct answers to user inquiries qualify only when they are durable beyond this run. Stored as conversation memory.",
263
+ "</memory-kinds>",
264
+ ].join("\n");
265
+ }
266
+
267
+ function reviewPrompt(request: CreateMemoryRequest): string {
268
+ const sections = [
269
+ "<memory-review-input>",
270
+ "Review the candidate memory using the runtime-owned context below.",
271
+ "",
272
+ runtimeDescription(request),
273
+ "",
274
+ sourceContext(request),
275
+ "",
276
+ "<candidate>",
277
+ escapeXml(request.content),
278
+ "</candidate>",
279
+ "",
280
+ "<rules>",
281
+ "- Return store only when the candidate is public/shareable, durable, and self-contained.",
282
+ "- First classify the memory kind: preference, procedure, or fact.",
283
+ "- Use kind=preference only for first-person facts authored by the current requester about their own preference, opinion, habit, identity, or workflow.",
284
+ "- Reject named third-person personal facts such as another person's preference, opinion, habit, identity, relationship, or workflow. Do not assume a named person is the current requester.",
285
+ "- Use kind=procedure for reusable task/process/runbook instructions.",
286
+ "- Use kind=fact for shared project, channel, operational, or runbook knowledge.",
287
+ "- When current-user-message contains an explicit memory request with a concrete fact or procedure, extract from current-user-message even if the candidate is vague, incomplete, or phrased as an instruction.",
288
+ "- A candidate may be badly phrased by an outer assistant or extraction pass. When current-user-message contains the requester's own first-person memory fact, treat that as requester-authored source evidence and canonicalize the fact instead of rejecting for third-person wording.",
289
+ "- When candidate wording personalizes a shared task, process, runbook, project, channel, or operational fact, use current-user-message to recover the shared fact and classify it as procedure or fact.",
290
+ "- Explicit procedure requests are valid when the source text contains both task context and action. Canonicalize them as shared procedure facts instead of rejecting them as vague.",
291
+ "- Store content as person-less, source-less canonical knowledge. Ownership and source live in structured metadata, not prose.",
292
+ "- For requester memories, omit the subject and write the content as a stable fact such as 'Prefers X', 'Uses Y', or 'Thinks Z'.",
293
+ "- Remove requester names, display names, requester/user labels, first- or second-person wording, thread labels, channel labels, and source labels from stored content.",
294
+ "- Reject third-party personal profile facts, even if they mention a name.",
295
+ "- Reject vague content such as 'remember this' unless the candidate or current-user-message contains the concrete fact.",
296
+ "- Preserve the requested expiration when one exists; otherwise set expiresAtMs to null.",
297
+ "- If unsure, reject.",
298
+ "</rules>",
299
+ "</memory-review-input>",
300
+ ].filter((section): section is string => section !== undefined);
301
+ return sections.join("\n");
302
+ }
303
+
304
+ function runTranscriptContext(request: ExtractSessionRequest): string {
305
+ return [
306
+ "<run-transcript>",
307
+ ...request.transcript.map((entry, index) => {
308
+ if (entry.type === "toolResult") {
309
+ return [
310
+ `<tool-result index="${index}" tool="${escapeXml(entry.toolName)}" is_error="${entry.isError ? "true" : "false"}">`,
311
+ escapeXml(entry.text),
312
+ "</tool-result>",
313
+ ].join("\n");
314
+ }
315
+ return [
316
+ `<message index="${index}" role="${entry.role}">`,
317
+ escapeXml(entry.text),
318
+ "</message>",
319
+ ].join("\n");
320
+ }),
321
+ "</run-transcript>",
322
+ ].join("\n");
323
+ }
324
+
325
+ function sessionExtractionPrompt(request: ExtractSessionRequest): string {
326
+ return [
327
+ "<memory-extraction-input>",
328
+ "Extract durable memories from this completed agent run using the runtime-owned context below.",
329
+ "",
330
+ runtimeDescription({
331
+ runtimeContext: request.runtimeContext,
332
+ }),
333
+ "",
334
+ existingMemoriesContext(request),
335
+ "",
336
+ memoryKindsContext(),
337
+ "",
338
+ runTranscriptContext(request),
339
+ "",
340
+ "<rules>",
341
+ "- Return at most five memories.",
342
+ "- Use user messages and successful tool results as source evidence for storable facts.",
343
+ "- Use failed tool results only when the failure reveals durable process knowledge, not transient errors.",
344
+ "- Use assistant messages only as context; do not store the assistant's claims unless supported by user messages or tool results.",
345
+ "- Return one memory per distinct fact.",
346
+ "- Prefer storing how to achieve a result: stable source-of-truth, query location, workflow, prerequisite, caveat, or reusable decision path that took effort to discover.",
347
+ "- Store direct answers to user inquiries only when they are stable operational/project knowledge, not values that naturally change over time.",
348
+ "- Do not store point-in-time analytics, search, issue, metric, incident, availability, or status answers just because a tool produced them.",
349
+ "- Do not store the fact that the user asked for advice, search, recall, planning, listing, inspection, or removal. Store only stable knowledge discovered in response, such as a reusable method or source-of-truth.",
350
+ "- A user question asking how, what, where, or whether to do something is not source evidence for the answer. Store the answer only when supported by a user-authored factual statement or a tool result.",
351
+ "- Set kind=procedure for reusable task/process/runbook instructions.",
352
+ "- Set kind=fact for shared team, project, channel, runbook, or operational knowledge.",
353
+ "- Set kind=preference only for clear durable first-person facts authored by the current requester about their own preference, opinion, habit, identity, or workflow.",
354
+ "- Reject named third-person personal facts such as another person's preference, opinion, habit, identity, relationship, or workflow. Do not assume a named person is the current requester.",
355
+ "- User-authored task instructions are procedures, not preferences, unless they explicitly describe the requester's personal preference or habit.",
356
+ "- Procedural statements such as 'for X, do Y', 'when X, do Y', and 'to accomplish X, do Y' belong in procedures.",
357
+ ...CANONICAL_CONTENT_RULES,
358
+ "- Skip a candidate when existing-memories already cover the same durable fact.",
359
+ "- Reject third-party personal profile facts, even if they mention a name.",
360
+ "- If unsure, return no memory for that candidate.",
361
+ "</rules>",
362
+ "</memory-extraction-input>",
363
+ ].join("\n");
364
+ }
365
+
366
+ /** Create the memory-owned agent that reviews and extracts memory candidates. */
367
+ export function createMemoryAgent(model: PluginModel): MemoryAgent {
368
+ return {
369
+ async extractSessionMemories(rawRequest) {
370
+ const request = extractSessionRequestSchema.parse(rawRequest);
371
+ const result = await model.completeObject({
372
+ schema: extractMemoriesResponseSchema,
373
+ system: MEMORY_EXTRACTION_SYSTEM,
374
+ prompt: sessionExtractionPrompt(request),
375
+ maxTokens: 1_000,
376
+ });
377
+ return extractedMemoriesFromResponse(
378
+ extractMemoriesResponseSchema.parse(result.object),
379
+ );
380
+ },
381
+ async reviewCreateRequest(rawRequest) {
382
+ const request = parseCreateMemoryRequest(rawRequest);
383
+ const result = await model.completeObject({
384
+ schema: memoryReviewResponseSchema,
385
+ system: MEMORY_REVIEW_SYSTEM,
386
+ prompt: reviewPrompt(request),
387
+ maxTokens: 700,
388
+ });
389
+ const response = memoryReviewResponseSchema.parse(result.object);
390
+ return memoryReviewFromResponse(response);
391
+ },
392
+ };
393
+ }
394
+
395
+ function memoryReviewFromResponse(
396
+ response: MemoryReviewResponse,
397
+ ): MemoryReview {
398
+ if (response.decision === "store") {
399
+ return parseMemoryReview({
400
+ decision: "store",
401
+ target: targetForKind(response.kind),
402
+ content: response.canonicalFact,
403
+ ...(response.expiresAtMs !== null
404
+ ? { expiresAtMs: response.expiresAtMs }
405
+ : {}),
406
+ });
407
+ }
408
+ return parseMemoryReview({
409
+ decision: "reject",
410
+ reason: response.reason,
411
+ });
412
+ }
413
+
414
+ function extractedMemoriesFromResponse(
415
+ response: ExtractMemoriesResponse,
416
+ ): ExtractedMemory[] {
417
+ const toMemory = (
418
+ memory: z.output<typeof extractedMemorySchema>,
419
+ ): ExtractedMemory => ({
420
+ content: memory.canonicalFact,
421
+ expiresAtMs: memory.expiresAtMs,
422
+ target: targetForKind(memory.kind),
423
+ });
424
+ return response.memories.map(toMemory);
425
+ }
426
+
427
+ /** Parse the structured decision returned by the memory agent. */
428
+ export function parseMemoryReview(result: unknown): MemoryReview {
429
+ return memoryReviewDecisionSchema.parse(result);
430
+ }
431
+
432
+ /** Parse the structured input sent to the memory agent. */
433
+ export function parseCreateMemoryRequest(
434
+ request: unknown,
435
+ ): CreateMemoryRequest {
436
+ return createMemoryRequestSchema.parse(request);
437
+ }
@@ -0,0 +1,30 @@
1
+ import type { juniorMemoryMemories } from "../db/schema";
2
+
3
+ function formatDate(ms: number | null): string {
4
+ return ms === null ? "-" : new Date(ms).toISOString();
5
+ }
6
+
7
+ /** Format a memory row as an operator-safe CLI projection. */
8
+ export function formatMemory(
9
+ row: typeof juniorMemoryMemories.$inferSelect,
10
+ args: {
11
+ showContent: boolean;
12
+ },
13
+ ): string {
14
+ const lines = [
15
+ `id=${row.id}`,
16
+ `scope=${row.scope}`,
17
+ `scope_key=${row.scopeKey}`,
18
+ `subject_type=${row.subjectType}`,
19
+ ...(row.subjectKey ? [`subject_key=${row.subjectKey}`] : []),
20
+ `type=${row.type}`,
21
+ `created_at=${formatDate(row.createdAtMs)}`,
22
+ `observed_at=${formatDate(row.observedAtMs)}`,
23
+ `expires_at=${formatDate(row.expiresAtMs)}`,
24
+ `archived_at=${formatDate(row.archivedAtMs)}`,
25
+ ];
26
+ if (args.showContent) {
27
+ lines.push(`content=${row.content}`);
28
+ }
29
+ return lines.join("\n");
30
+ }
@@ -0,0 +1,15 @@
1
+ import type { PluginCliCommandDefinition } from "@sentry/junior-plugin-api";
2
+ import { configureMemorySearchCommand } from "./search";
3
+ import { configureMemoryShowCommand } from "./show";
4
+
5
+ /** Create the plugin-owned memory admin CLI command. */
6
+ export function createMemoryCliCommand(): PluginCliCommandDefinition {
7
+ return {
8
+ name: "memory",
9
+ summary: "Inspect Junior memory state",
10
+ configure(command, junior) {
11
+ configureMemorySearchCommand(command, junior);
12
+ configureMemoryShowCommand(command, junior);
13
+ },
14
+ };
15
+ }
@@ -0,0 +1,119 @@
1
+ import { InvalidArgumentError, Option, type Command } from "commander";
2
+ import { and, desc, eq, gt, ilike, isNull, or, type SQL } from "drizzle-orm";
3
+ import type {
4
+ PluginCliActionContext,
5
+ PluginCliHost,
6
+ } from "@sentry/junior-plugin-api";
7
+ import { juniorMemoryMemories } from "../db/schema";
8
+ import type { MemoryDb } from "../store";
9
+ import { MEMORY_SCOPES, type MemoryScope } from "../types";
10
+ import { formatMemory } from "./format";
11
+
12
+ interface SearchOptions {
13
+ limit: number;
14
+ scope: MemoryScope;
15
+ scopeKey: string;
16
+ showContent?: boolean;
17
+ }
18
+
19
+ function parseLimit(value: string): number {
20
+ const parsed = Number(value);
21
+ if (!Number.isFinite(parsed)) {
22
+ throw new InvalidArgumentError("--limit must be a number");
23
+ }
24
+ return Math.min(100, Math.max(1, Math.floor(parsed)));
25
+ }
26
+
27
+ async function runSearch(
28
+ ctx: PluginCliActionContext,
29
+ queryParts: string[] | undefined,
30
+ options: SearchOptions,
31
+ ): Promise<number> {
32
+ const query = (queryParts ?? []).join(" ").trim();
33
+ const nowMs = Date.now();
34
+ const terms = [
35
+ ...new Set(
36
+ query
37
+ .toLowerCase()
38
+ .split(/[^a-z0-9_'-]+/)
39
+ .map((term) => term.trim())
40
+ .filter((term) => term.length >= 2),
41
+ ),
42
+ ];
43
+
44
+ const db = ctx.db as MemoryDb;
45
+ const activeExpirationPredicate = or(
46
+ isNull(juniorMemoryMemories.expiresAtMs),
47
+ gt(juniorMemoryMemories.expiresAtMs, nowMs),
48
+ );
49
+ const predicates: SQL[] = [
50
+ eq(juniorMemoryMemories.scope, options.scope),
51
+ eq(juniorMemoryMemories.scopeKey, options.scopeKey),
52
+ isNull(juniorMemoryMemories.archivedAtMs),
53
+ isNull(juniorMemoryMemories.supersededAtMs),
54
+ isNull(juniorMemoryMemories.supersededById),
55
+ ];
56
+ if (activeExpirationPredicate) {
57
+ predicates.push(activeExpirationPredicate);
58
+ }
59
+ if (terms.length > 0) {
60
+ const termPredicate = or(
61
+ ...terms.map((term) => ilike(juniorMemoryMemories.content, `%${term}%`)),
62
+ );
63
+ if (termPredicate) {
64
+ predicates.push(termPredicate);
65
+ }
66
+ }
67
+ const rows = await db
68
+ .select()
69
+ .from(juniorMemoryMemories)
70
+ .where(and(...predicates))
71
+ .orderBy(desc(juniorMemoryMemories.createdAtMs))
72
+ .limit(options.limit);
73
+
74
+ if (rows.length === 0) {
75
+ await ctx.io.writeOutput("No memories matched.\n");
76
+ return 0;
77
+ }
78
+
79
+ await ctx.io.writeOutput(
80
+ `${rows
81
+ .map((row) =>
82
+ formatMemory(row, { showContent: Boolean(options.showContent) }),
83
+ )
84
+ .join("\n\n")}\n`,
85
+ );
86
+ return 0;
87
+ }
88
+
89
+ /** Wire the memory search admin subcommand under the plugin namespace. */
90
+ export function configureMemorySearchCommand(
91
+ parent: Command,
92
+ junior: PluginCliHost,
93
+ ): void {
94
+ parent
95
+ .command("search")
96
+ .description("Search visible memories")
97
+ .argument("[query...]", "Search query")
98
+ .addOption(
99
+ new Option("--scope <scope>", "Memory scope")
100
+ .choices([...MEMORY_SCOPES])
101
+ .makeOptionMandatory(),
102
+ )
103
+ .requiredOption("--scope-key <key>", "Scope key")
104
+ .addOption(
105
+ new Option("--limit <n>", "Maximum rows")
106
+ .argParser(parseLimit)
107
+ .default(20),
108
+ )
109
+ .option("--show-content", "Print raw memory content")
110
+ .action(
111
+ junior.action(async (ctx, queryParts, options) => {
112
+ return await runSearch(
113
+ ctx,
114
+ queryParts as string[] | undefined,
115
+ options as SearchOptions,
116
+ );
117
+ }),
118
+ );
119
+ }
@@ -0,0 +1,44 @@
1
+ import type { Command } from "commander";
2
+ import type {
3
+ PluginCliActionContext,
4
+ PluginCliHost,
5
+ } from "@sentry/junior-plugin-api";
6
+ import { eq } from "drizzle-orm";
7
+ import { juniorMemoryMemories } from "../db/schema";
8
+ import type { MemoryDb } from "../store";
9
+ import { formatMemory } from "./format";
10
+
11
+ async function runShow(
12
+ ctx: PluginCliActionContext,
13
+ id: string,
14
+ ): Promise<number> {
15
+ const db = ctx.db as MemoryDb;
16
+ const rows = await db
17
+ .select()
18
+ .from(juniorMemoryMemories)
19
+ .where(eq(juniorMemoryMemories.id, id))
20
+ .limit(1);
21
+ if (!rows[0]) {
22
+ await ctx.io.writeError(`Memory not found: ${id}\n`);
23
+ return 1;
24
+ }
25
+
26
+ await ctx.io.writeOutput(`${formatMemory(rows[0], { showContent: true })}\n`);
27
+ return 0;
28
+ }
29
+
30
+ /** Wire the explicit raw-content memory inspection subcommand. */
31
+ export function configureMemoryShowCommand(
32
+ parent: Command,
33
+ junior: PluginCliHost,
34
+ ): void {
35
+ parent
36
+ .command("show")
37
+ .description("Show one memory")
38
+ .argument("<id>", "Memory id")
39
+ .action(
40
+ junior.action(async (ctx, id) => {
41
+ return await runShow(ctx, id as string);
42
+ }),
43
+ );
44
+ }