opencode-mem-agents 0.3.1 → 0.3.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,130 @@
1
+ import { ZodError } from "zod";
2
+ export class InputValidationError extends Error {
3
+ constructor(message) {
4
+ super(message);
5
+ this.name = "InputValidationError";
6
+ }
7
+ }
8
+ export async function readBody(req, maxBytes) {
9
+ const chunks = [];
10
+ let total = 0;
11
+ for await (const chunk of req) {
12
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
13
+ total += buf.byteLength;
14
+ if (total > maxBytes) {
15
+ throw new InputValidationError("payload too large");
16
+ }
17
+ chunks.push(buf);
18
+ }
19
+ return Buffer.concat(chunks).toString("utf-8");
20
+ }
21
+ export function parseSchema(schema, value) {
22
+ try {
23
+ return schema.parse(value);
24
+ }
25
+ catch (error) {
26
+ if (error instanceof ZodError) {
27
+ const issue = error.issues[0];
28
+ const where = issue?.path?.length ? issue.path.join(".") : "request";
29
+ const reason = issue?.message ?? "invalid input";
30
+ throw new InputValidationError(`${where}: ${reason}`);
31
+ }
32
+ throw error;
33
+ }
34
+ }
35
+ function withTraceId(ctx, body) {
36
+ if (body === null || typeof body !== "object" || Array.isArray(body)) {
37
+ return {
38
+ traceId: ctx.traceId,
39
+ data: body,
40
+ };
41
+ }
42
+ const record = body;
43
+ if ("traceId" in record)
44
+ return body;
45
+ return {
46
+ traceId: ctx.traceId,
47
+ ...record,
48
+ };
49
+ }
50
+ export function sendJson(ctx, res, status, data, traceLog) {
51
+ const payload = withTraceId(ctx, data);
52
+ res.writeHead(status, {
53
+ "Content-Type": "application/json; charset=utf-8",
54
+ "Cache-Control": "no-store",
55
+ });
56
+ res.end(JSON.stringify(payload));
57
+ traceLog(ctx, "out", status, payload, Date.now() - ctx.startedAt);
58
+ }
59
+ export function sendText(ctx, res, status, data, traceLog) {
60
+ res.writeHead(status, {
61
+ "Content-Type": "text/plain; charset=utf-8",
62
+ "Cache-Control": "no-store",
63
+ });
64
+ res.end(data);
65
+ traceLog(ctx, "out", status, { textPreview: data.slice(0, 500), textLength: data.length }, Date.now() - ctx.startedAt);
66
+ }
67
+ export function sendHtml(ctx, res, status, html, traceLog) {
68
+ res.writeHead(status, {
69
+ "Content-Type": "text/html; charset=utf-8",
70
+ "Cache-Control": "no-store",
71
+ });
72
+ res.end(html);
73
+ traceLog(ctx, "out", status, { html: "dashboard", length: html.length }, Date.now() - ctx.startedAt);
74
+ }
75
+ export function sendError(ctx, res, status, code, message, traceLog, details) {
76
+ const body = {
77
+ error: {
78
+ code,
79
+ message,
80
+ },
81
+ };
82
+ if (details !== undefined) {
83
+ body.error = {
84
+ ...body.error,
85
+ details,
86
+ };
87
+ }
88
+ sendJson(ctx, res, status, body, traceLog);
89
+ }
90
+ export function applyCors(req, res, corsOrigin) {
91
+ const requestOrigin = typeof req.headers.origin === "string" ? req.headers.origin : "";
92
+ let allowedOrigin = "";
93
+ if (corsOrigin) {
94
+ if (requestOrigin === corsOrigin) {
95
+ allowedOrigin = corsOrigin;
96
+ }
97
+ }
98
+ else if (requestOrigin) {
99
+ if (/^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/i.test(requestOrigin)) {
100
+ allowedOrigin = requestOrigin;
101
+ }
102
+ }
103
+ if (allowedOrigin) {
104
+ res.setHeader("Access-Control-Allow-Origin", allowedOrigin);
105
+ }
106
+ res.setHeader("Vary", "Origin");
107
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
108
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
109
+ if (requestOrigin && !allowedOrigin) {
110
+ return false;
111
+ }
112
+ return true;
113
+ }
114
+ export function requiresAuth(path, apiToken) {
115
+ if (!apiToken)
116
+ return false;
117
+ if (!path.startsWith("/api"))
118
+ return false;
119
+ if (path === "/api/health" || path === "/api/readiness")
120
+ return false;
121
+ return true;
122
+ }
123
+ export function isAuthorized(req, apiToken) {
124
+ if (!apiToken)
125
+ return true;
126
+ const authHeader = typeof req.headers.authorization === "string" ? req.headers.authorization : "";
127
+ if (!authHeader.startsWith("Bearer "))
128
+ return false;
129
+ return authHeader.slice("Bearer ".length) === apiToken;
130
+ }
@@ -0,0 +1,61 @@
1
+ import type { ActivityParams, ContextSessionParams, MemorySaveBody, SearchParams, TimelineParams, ToolResultBody, TracesParams } from "../contracts.js";
2
+ export interface WorkerTraceEvent {
3
+ id: number;
4
+ trace_id: string;
5
+ direction: string;
6
+ method: string;
7
+ path: string;
8
+ status: number;
9
+ payload: unknown;
10
+ duration_ms: number;
11
+ created_at_epoch: number;
12
+ }
13
+ export interface SearchResult {
14
+ observations: any[];
15
+ totalResults: number;
16
+ }
17
+ export interface TimelineResult {
18
+ anchorId: number;
19
+ observations: any[];
20
+ depthBefore: number;
21
+ depthAfter: number;
22
+ }
23
+ export interface SaveResult {
24
+ id: number;
25
+ status: "saved" | "captured" | "duplicate";
26
+ }
27
+ export declare class WorkerRepository {
28
+ private readonly db;
29
+ private readonly traceRetentionMs;
30
+ private readonly traceMaxPayloadChars;
31
+ private readonly insertObs;
32
+ private readonly insertObsIgnoreCapture;
33
+ private readonly getObsByCaptureKey;
34
+ private readonly getObsByIds;
35
+ private readonly getObsByIdInProject;
36
+ private readonly getLatestObsInProject;
37
+ private readonly findTimelineAnchorByQuery;
38
+ private readonly getTimelineBeforeRows;
39
+ private readonly getTimelineAfterRows;
40
+ private readonly insertIoEvent;
41
+ private readonly deleteStaleIoEvents;
42
+ private readonly getRecentTraceEvents;
43
+ private readonly getTraceEventsByTraceId;
44
+ private traceWritesSincePrune;
45
+ constructor(dbPath: string, traceRetentionDays?: number, traceMaxPayloadChars?: number);
46
+ close(): void;
47
+ logTrace(traceId: string, direction: "in" | "out" | "error", method: string, path: string, status: number, payload: unknown, durationMs?: number): void;
48
+ search(input: SearchParams): SearchResult;
49
+ timeline(input: TimelineParams): TimelineResult;
50
+ getByIds(ids: number[], project?: string): any[];
51
+ saveMemory(input: MemorySaveBody): SaveResult;
52
+ saveToolResult(input: ToolResultBody): SaveResult;
53
+ getContextRows(input: ContextSessionParams): any[];
54
+ getActivityRows(input: ActivityParams): any[];
55
+ getTraces(input: TracesParams): WorkerTraceEvent[];
56
+ private initializeSchema;
57
+ private ensureColumn;
58
+ private resolveTimelineAnchor;
59
+ private pruneStaleTraces;
60
+ private rebuildFtsIfOutOfSync;
61
+ }