@tinchu0/knitt 0.0.0-stage → 0.1.0-beta.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.
- package/LICENSE +202 -0
- package/README.md +64 -2
- package/dist/apps/server/src/conversation-server.js +203 -0
- package/dist/apps/server/src/http-gateway.js +118 -0
- package/dist/apps/server/src/http-main.js +46 -0
- package/dist/apps/server/src/main.js +15 -0
- package/dist/apps/server/src/server.js +253 -0
- package/dist/apps/server/src/web-auth.js +30 -0
- package/dist/apps/server/src/web-picker.js +119 -0
- package/dist/memory-schema.json +1 -0
- package/dist/packages/adapters/src/cli.js +259 -0
- package/dist/packages/adapters/src/command.js +227 -0
- package/dist/packages/adapters/src/discovery.js +128 -0
- package/dist/packages/adapters/src/handoff.js +146 -0
- package/dist/packages/adapters/src/hooks.js +159 -0
- package/dist/packages/adapters/src/launch.js +21 -0
- package/dist/packages/adapters/src/picker.js +40 -0
- package/dist/packages/adapters/src/providers/jsonl.js +105 -0
- package/dist/packages/adapters/src/repository.js +36 -0
- package/dist/packages/adapters/src/scope.js +32 -0
- package/dist/packages/adapters/src/status.js +81 -0
- package/dist/packages/adapters/src/transcript.js +193 -0
- package/dist/packages/contracts/src/capabilities.js +1 -0
- package/dist/packages/contracts/src/conversations.js +30 -0
- package/dist/packages/contracts/src/events.js +27 -0
- package/dist/packages/installer/src/cli.js +325 -0
- package/dist/packages/installer/src/connections.js +352 -0
- package/dist/packages/installer/src/runtime.js +133 -0
- package/dist/packages/memory/src/checkpoints.js +68 -0
- package/dist/packages/memory/src/context.js +91 -0
- package/dist/packages/memory/src/conversations.js +148 -0
- package/dist/packages/memory/src/migrations.js +55 -0
- package/dist/packages/memory/src/store.js +269 -0
- package/dist/scripts/connector-setup.js +77 -0
- package/dist/skills/continue-chat/SKILL.md +13 -0
- package/docs/clients/connector-setup.md +63 -0
- package/docs/clients/local-agent-memory.md +50 -0
- package/docs/clients/npm-installation.md +67 -0
- package/package.json +50 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export function conversationLabel(conversation) {
|
|
2
|
+
return [conversation.title || "(untitled)", conversation.workspace, conversation.modified]
|
|
3
|
+
.join(" · ").replace(/[\x00-\x1f\x7f]/g, " ");
|
|
4
|
+
}
|
|
5
|
+
export async function pickConversation(conversations, io) {
|
|
6
|
+
let query = "";
|
|
7
|
+
let page = 0;
|
|
8
|
+
for (;;) {
|
|
9
|
+
const matches = conversations.filter(c => conversationLabel(c).toLowerCase().includes(query));
|
|
10
|
+
const pages = Math.max(1, Math.ceil(matches.length / 15));
|
|
11
|
+
page = Math.min(page, pages - 1);
|
|
12
|
+
const visible = matches.slice(page * 15, page * 15 + 15);
|
|
13
|
+
io.write("\nChoose the chat to continue. Page " + (page + 1) + "/" + pages + "\n");
|
|
14
|
+
visible.forEach((conversation, i) => io.write(String(i + 1) + ". " + conversationLabel(conversation) + "\n"));
|
|
15
|
+
if (!visible.length)
|
|
16
|
+
io.write("No matching chats. Search again or cancel.\n");
|
|
17
|
+
const answer = (await io.ask("Number, s <search>, n/p for pages, or Enter to cancel: ")).trim();
|
|
18
|
+
if (!answer || answer.toLowerCase() === "q")
|
|
19
|
+
return null;
|
|
20
|
+
if (answer.startsWith("s ")) {
|
|
21
|
+
query = answer.slice(2).trim().toLowerCase();
|
|
22
|
+
page = 0;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (answer === "n") {
|
|
26
|
+
page = Math.min(page + 1, pages - 1);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (answer === "p") {
|
|
30
|
+
page = Math.max(0, page - 1);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (/^[1-9][0-9]*$/.test(answer)) {
|
|
34
|
+
const selected = visible[Number(answer) - 1];
|
|
35
|
+
if (selected)
|
|
36
|
+
return selected;
|
|
37
|
+
}
|
|
38
|
+
io.write("Choose a displayed number. Nothing has been selected.\n");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { existsSync, readdirSync, openSync, readSync, closeSync, statSync, realpathSync, } from "node:fs";
|
|
2
|
+
import { join, relative, isAbsolute, resolve } from "node:path";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
function repository(path) {
|
|
5
|
+
try {
|
|
6
|
+
return execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], {
|
|
7
|
+
cwd: path,
|
|
8
|
+
encoding: "utf8",
|
|
9
|
+
timeout: 1000,
|
|
10
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
11
|
+
}).trim();
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function discoverJsonl(client, root, workspace) {
|
|
18
|
+
const conversations = [], diagnostics = [];
|
|
19
|
+
if (!existsSync(root))
|
|
20
|
+
return {
|
|
21
|
+
conversations,
|
|
22
|
+
diagnostics: [client + " history directory unavailable"],
|
|
23
|
+
};
|
|
24
|
+
const expected = repository(workspace);
|
|
25
|
+
let visited = 0, unsupported = 0;
|
|
26
|
+
function walk(directory, depth) {
|
|
27
|
+
if (depth > 8 || visited >= 10000)
|
|
28
|
+
return;
|
|
29
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
30
|
+
if (++visited > 10000)
|
|
31
|
+
break;
|
|
32
|
+
const path = join(directory, entry.name);
|
|
33
|
+
if (entry.isSymbolicLink())
|
|
34
|
+
continue;
|
|
35
|
+
if (entry.isDirectory()) {
|
|
36
|
+
walk(path, depth + 1);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (!entry.isFile() || !entry.name.endsWith(".jsonl"))
|
|
40
|
+
continue;
|
|
41
|
+
const fd = openSync(path, "r");
|
|
42
|
+
let text = "";
|
|
43
|
+
try {
|
|
44
|
+
const buffer = Buffer.alloc(65536);
|
|
45
|
+
const count = readSync(fd, buffer, 0, buffer.length, 0);
|
|
46
|
+
text = buffer.subarray(0, count).toString("utf8");
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
closeSync(fd);
|
|
50
|
+
}
|
|
51
|
+
let id, cwd;
|
|
52
|
+
for (const line of text.split("\n").slice(0, -1)) {
|
|
53
|
+
let row;
|
|
54
|
+
try {
|
|
55
|
+
row = JSON.parse(line);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const meta = client === "codex"
|
|
61
|
+
? row.type === "session_meta"
|
|
62
|
+
? row.payload
|
|
63
|
+
: null
|
|
64
|
+
: row;
|
|
65
|
+
const nativeId = client === "codex" ? (meta?.id ?? meta?.session_id) : meta?.sessionId;
|
|
66
|
+
if (typeof nativeId === "string" && typeof meta?.cwd === "string") {
|
|
67
|
+
id = nativeId;
|
|
68
|
+
cwd = meta.cwd;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!id || !cwd || id.length > 256) {
|
|
73
|
+
unsupported++;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (resolve(cwd) !== resolve(workspace) &&
|
|
77
|
+
(!expected || repository(cwd) !== expected))
|
|
78
|
+
continue;
|
|
79
|
+
const inside = relative(realpathSync(root), realpathSync(path));
|
|
80
|
+
if (inside.startsWith("..") || isAbsolute(inside))
|
|
81
|
+
continue;
|
|
82
|
+
conversations.push({
|
|
83
|
+
id: client + ":" + id,
|
|
84
|
+
sourceId: id,
|
|
85
|
+
client,
|
|
86
|
+
title: client + " session " + id.slice(0, 24),
|
|
87
|
+
workspace: cwd,
|
|
88
|
+
root,
|
|
89
|
+
modified: statSync(path).mtime.toISOString(),
|
|
90
|
+
transcript: path,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
walk(root, 0);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
diagnostics.push(client + " history could not be fully read");
|
|
99
|
+
}
|
|
100
|
+
if (unsupported)
|
|
101
|
+
diagnostics.push(client + " history files with unsupported metadata: " + unsupported);
|
|
102
|
+
if (visited >= 10000)
|
|
103
|
+
diagnostics.push(client + " discovery limit reached");
|
|
104
|
+
return { conversations, diagnostics };
|
|
105
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
export function resolveRepository(directory) {
|
|
4
|
+
const workspace = execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
5
|
+
cwd: directory,
|
|
6
|
+
encoding: "utf8",
|
|
7
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
8
|
+
}).trim();
|
|
9
|
+
let repoId;
|
|
10
|
+
try {
|
|
11
|
+
const remote = execFileSync("git", ["remote", "get-url", "origin"], {
|
|
12
|
+
cwd: workspace,
|
|
13
|
+
encoding: "utf8",
|
|
14
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
15
|
+
}).trim();
|
|
16
|
+
const ssh = remote.match(/^[^@]+@([^:]+):(.+)$/);
|
|
17
|
+
const url = ssh
|
|
18
|
+
? new URL("https://" + ssh[1] + "/" + ssh[2])
|
|
19
|
+
: new URL(remote);
|
|
20
|
+
repoId = (url.hostname + url.pathname)
|
|
21
|
+
.replace(/\.git$/, "")
|
|
22
|
+
.replace(/\/$/, "");
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
repoId =
|
|
26
|
+
"local:" +
|
|
27
|
+
createHash("sha256")
|
|
28
|
+
.update(execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], {
|
|
29
|
+
cwd: workspace,
|
|
30
|
+
encoding: "utf8",
|
|
31
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
32
|
+
}).trim())
|
|
33
|
+
.digest("hex");
|
|
34
|
+
}
|
|
35
|
+
return { workspace, repoId };
|
|
36
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
export function resolveConnection(payload, env = process.env) {
|
|
3
|
+
const repoId = env.MEMORY_REPO;
|
|
4
|
+
if (!repoId)
|
|
5
|
+
throw new Error("Set MEMORY_REPO for the connected repository");
|
|
6
|
+
const workspacePaths = payload.workspacePaths;
|
|
7
|
+
const workspace = typeof payload.cwd === "string" ? payload.cwd :
|
|
8
|
+
Array.isArray(workspacePaths) && workspacePaths.length === 1 && typeof workspacePaths[0] === "string"
|
|
9
|
+
? workspacePaths[0] : process.cwd();
|
|
10
|
+
if (env.MEMORY_WORKSPACE) {
|
|
11
|
+
const commonGitDir = (cwd) => execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], {
|
|
12
|
+
cwd, encoding: "utf8", timeout: 2000, stdio: ["ignore", "pipe", "ignore"],
|
|
13
|
+
}).trim();
|
|
14
|
+
if (commonGitDir(workspace) !== commonGitDir(env.MEMORY_WORKSPACE)) {
|
|
15
|
+
throw new Error("Wrong workspace. Open " + env.MEMORY_WORKSPACE + " to continue this handoff. No memory was recalled or captured.");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return { repoId, workspace };
|
|
19
|
+
}
|
|
20
|
+
export function resolveScope(payload, env = process.env) {
|
|
21
|
+
const { repoId, workspace } = resolveConnection(payload, env);
|
|
22
|
+
let branch = env.MEMORY_BRANCH;
|
|
23
|
+
if (!branch && !env.MEMORY_FEATURE) {
|
|
24
|
+
branch = execFileSync("git", ["branch", "--show-current"], {
|
|
25
|
+
cwd: workspace, encoding: "utf8", timeout: 2000, stdio: ["ignore", "pipe", "ignore"],
|
|
26
|
+
}).trim();
|
|
27
|
+
}
|
|
28
|
+
const featureId = env.MEMORY_FEATURE ?? branch;
|
|
29
|
+
if (!featureId)
|
|
30
|
+
throw new Error("Detached HEAD requires an explicit MEMORY_FEATURE");
|
|
31
|
+
return { repoId, featureId, ...(branch ? { branch } : {}) };
|
|
32
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { sessionKeySchema } from "../../contracts/src/conversations.js";
|
|
6
|
+
import { openQueue } from "./hooks.js";
|
|
7
|
+
export const captureStatusSchema = z.object({
|
|
8
|
+
session: sessionKeySchema,
|
|
9
|
+
lastAttemptAt: z.string().nullable().default(null),
|
|
10
|
+
captureState: z
|
|
11
|
+
.enum([
|
|
12
|
+
"unobserved",
|
|
13
|
+
"captured",
|
|
14
|
+
"partial",
|
|
15
|
+
"no_supported_text",
|
|
16
|
+
"no_new_text",
|
|
17
|
+
"error",
|
|
18
|
+
])
|
|
19
|
+
.default("unobserved"),
|
|
20
|
+
lastCaptureAt: z.string().nullable(),
|
|
21
|
+
lastDeliveryAt: z.string().nullable(),
|
|
22
|
+
pending: z.number().int().nonnegative(),
|
|
23
|
+
lastErrorCode: z.enum(["CAPTURE_FAILED", "DELIVERY_PENDING"]).nullable(),
|
|
24
|
+
});
|
|
25
|
+
export function recordCaptureStatus(directory, input) {
|
|
26
|
+
const status = captureStatusSchema.parse(input), queue = openQueue(directory), id = JSON.stringify(status.session);
|
|
27
|
+
try {
|
|
28
|
+
queue.exec("CREATE TABLE IF NOT EXISTS capture_status(id TEXT PRIMARY KEY,payload TEXT NOT NULL)");
|
|
29
|
+
const row = queue
|
|
30
|
+
.prepare("SELECT payload FROM capture_status WHERE id=?")
|
|
31
|
+
.get(id);
|
|
32
|
+
const old = row
|
|
33
|
+
? captureStatusSchema.parse(JSON.parse(String(row.payload)))
|
|
34
|
+
: null;
|
|
35
|
+
const value = {
|
|
36
|
+
...status,
|
|
37
|
+
lastAttemptAt: status.lastAttemptAt ?? old?.lastAttemptAt ?? null,
|
|
38
|
+
lastCaptureAt: status.lastCaptureAt ?? old?.lastCaptureAt ?? null,
|
|
39
|
+
lastDeliveryAt: status.lastDeliveryAt ?? old?.lastDeliveryAt ?? null,
|
|
40
|
+
};
|
|
41
|
+
queue
|
|
42
|
+
.prepare("INSERT INTO capture_status VALUES(?,?) ON CONFLICT(id) DO UPDATE SET payload=excluded.payload")
|
|
43
|
+
.run(id, JSON.stringify(value));
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
queue.close();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export function readCaptureStatuses(directory) {
|
|
50
|
+
const path = join(directory, "queue.sqlite");
|
|
51
|
+
if (!existsSync(path))
|
|
52
|
+
return [];
|
|
53
|
+
const db = new DatabaseSync(path, { readOnly: true });
|
|
54
|
+
try {
|
|
55
|
+
if (!db
|
|
56
|
+
.prepare("SELECT name FROM sqlite_master WHERE name='capture_status'")
|
|
57
|
+
.get())
|
|
58
|
+
return [];
|
|
59
|
+
return db
|
|
60
|
+
.prepare("SELECT payload FROM capture_status ORDER BY id")
|
|
61
|
+
.all()
|
|
62
|
+
.map((row) => captureStatusSchema.parse(JSON.parse(String(row.payload))));
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
db.close();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export function pendingForSession(directory, session) {
|
|
69
|
+
const path = join(directory, "queue.sqlite");
|
|
70
|
+
if (!existsSync(path))
|
|
71
|
+
return 0;
|
|
72
|
+
const db = new DatabaseSync(path, { readOnly: true });
|
|
73
|
+
try {
|
|
74
|
+
return Number(db
|
|
75
|
+
.prepare("SELECT COUNT(*) AS n FROM pending WHERE json_extract(payload,'$.repoId')=? AND json_extract(payload,'$.sessionId')=? AND json_extract(payload,'$.source') LIKE ?")
|
|
76
|
+
.get(session.repoId, session.nativeSessionId, session.client + ":%").n);
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
db.close();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { sessionKeySchema } from "../../contracts/src/conversations.js";
|
|
2
|
+
import { openSync, closeSync, fstatSync, readSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { openQueue, redact } from "./hooks.js";
|
|
6
|
+
import { eventSchema } from "../../contracts/src/events.js";
|
|
7
|
+
const windowBytes = 4 * 1024 * 1024;
|
|
8
|
+
const maxLineBytes = 1024 * 1024;
|
|
9
|
+
const hash = (s) => createHash("sha256").update(s).digest("hex");
|
|
10
|
+
function textBlocks(value) {
|
|
11
|
+
if (typeof value === "string")
|
|
12
|
+
return value;
|
|
13
|
+
if (!Array.isArray(value))
|
|
14
|
+
return "";
|
|
15
|
+
return value
|
|
16
|
+
.flatMap((block) => {
|
|
17
|
+
if (block &&
|
|
18
|
+
typeof block === "object" &&
|
|
19
|
+
typeof block.text === "string" &&
|
|
20
|
+
(!block.type ||
|
|
21
|
+
["text", "input_text", "output_text"].includes(block.type)))
|
|
22
|
+
return [block.text];
|
|
23
|
+
return [];
|
|
24
|
+
})
|
|
25
|
+
.join("\n");
|
|
26
|
+
}
|
|
27
|
+
function messageText(row) {
|
|
28
|
+
if (row.type === "USER_INPUT" && typeof row.content === "string") {
|
|
29
|
+
const request = row.content.match(/<USER_REQUEST>([\s\S]*?)<\/USER_REQUEST>/);
|
|
30
|
+
return "user: " + (request ? request[1].trim() : row.content);
|
|
31
|
+
}
|
|
32
|
+
if (["PLANNER_RESPONSE", "ASSISTANT_MESSAGE"].includes(row.type) &&
|
|
33
|
+
typeof row.content === "string") {
|
|
34
|
+
return "assistant: " + row.content;
|
|
35
|
+
}
|
|
36
|
+
const message = row.type === "response_item" ? row.payload : (row.message ?? row);
|
|
37
|
+
if (!message || typeof message !== "object")
|
|
38
|
+
return null;
|
|
39
|
+
const role = message.role ??
|
|
40
|
+
(["user", "assistant"].includes(row.type) ? row.type : undefined);
|
|
41
|
+
if (!["user", "assistant", "model"].includes(role))
|
|
42
|
+
return null;
|
|
43
|
+
const text = textBlocks(message.content ?? message.parts);
|
|
44
|
+
const blocks = message.content ?? message.parts;
|
|
45
|
+
const onlyNonText = Array.isArray(blocks) &&
|
|
46
|
+
blocks.every((block) => block &&
|
|
47
|
+
typeof block === "object" &&
|
|
48
|
+
[
|
|
49
|
+
"tool_use",
|
|
50
|
+
"tool_result",
|
|
51
|
+
"thinking",
|
|
52
|
+
"redacted_thinking",
|
|
53
|
+
"image",
|
|
54
|
+
].includes(block.type));
|
|
55
|
+
if (!text && !onlyNonText)
|
|
56
|
+
throw new Error("Unsupported transcript message shape for role " + role);
|
|
57
|
+
return text ? role + ": " + text : null;
|
|
58
|
+
}
|
|
59
|
+
export function splitText(text) {
|
|
60
|
+
const result = [];
|
|
61
|
+
let part = "";
|
|
62
|
+
let bytes = 0;
|
|
63
|
+
for (const char of text) {
|
|
64
|
+
const size = Buffer.byteLength(char);
|
|
65
|
+
if (bytes + size > 6000) {
|
|
66
|
+
result.push(part);
|
|
67
|
+
part = "";
|
|
68
|
+
bytes = 0;
|
|
69
|
+
}
|
|
70
|
+
part += char;
|
|
71
|
+
bytes += size;
|
|
72
|
+
}
|
|
73
|
+
if (part)
|
|
74
|
+
result.push(part);
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
/** Queue complete JSONL records and their cursor atomically. Never advance past an unfinished line. */
|
|
78
|
+
export function captureTranscript(options) {
|
|
79
|
+
const { directory, client, scope, sessionId } = options;
|
|
80
|
+
if (!sessionId)
|
|
81
|
+
throw new Error("Transcript has no session identity");
|
|
82
|
+
const path = resolve(options.path);
|
|
83
|
+
const fd = openSync(path, "r");
|
|
84
|
+
const queue = openQueue(directory);
|
|
85
|
+
let transaction = false;
|
|
86
|
+
try {
|
|
87
|
+
queue.exec("CREATE TABLE IF NOT EXISTS cursors(id TEXT PRIMARY KEY, offset INTEGER NOT NULL, prefix TEXT NOT NULL)");
|
|
88
|
+
queue.exec("BEGIN IMMEDIATE");
|
|
89
|
+
transaction = true;
|
|
90
|
+
if (options.session) {
|
|
91
|
+
sessionKeySchema.parse(options.session);
|
|
92
|
+
if (options.session.repoId !== scope.repoId ||
|
|
93
|
+
options.session.client !== client ||
|
|
94
|
+
options.session.nativeSessionId !== sessionId)
|
|
95
|
+
throw new Error("Queued session identity mismatch");
|
|
96
|
+
}
|
|
97
|
+
const identityScope = options.session
|
|
98
|
+
? { repoId: scope.repoId, featureId: scope.featureId }
|
|
99
|
+
: scope;
|
|
100
|
+
const key = hash(JSON.stringify({ client, scope: identityScope, sessionId, path }));
|
|
101
|
+
const previous = queue
|
|
102
|
+
.prepare("SELECT offset,prefix FROM cursors WHERE id=?")
|
|
103
|
+
.get(key);
|
|
104
|
+
const size = fstatSync(fd).size;
|
|
105
|
+
let offset = previous ? Number(previous.offset) : 0;
|
|
106
|
+
const prefix = (length) => {
|
|
107
|
+
const buffer = Buffer.alloc(Math.min(length, 128));
|
|
108
|
+
const count = readSync(fd, buffer, 0, buffer.length, 0);
|
|
109
|
+
return hash(buffer.subarray(0, count));
|
|
110
|
+
};
|
|
111
|
+
if (offset > size || (previous && prefix(offset) !== previous.prefix))
|
|
112
|
+
offset = 0;
|
|
113
|
+
const buffer = Buffer.alloc(Math.min(size - offset, windowBytes));
|
|
114
|
+
const read = readSync(fd, buffer, 0, buffer.length, offset);
|
|
115
|
+
let start = 0;
|
|
116
|
+
let events = 0;
|
|
117
|
+
let records = 0;
|
|
118
|
+
while (start < read && records < 500) {
|
|
119
|
+
const end = buffer.indexOf(10, start);
|
|
120
|
+
if (end < 0 || end >= read)
|
|
121
|
+
break;
|
|
122
|
+
if (end - start > maxLineBytes)
|
|
123
|
+
throw new Error("Transcript line exceeds 1 MiB");
|
|
124
|
+
const line = buffer.subarray(start, end).toString("utf8").trim();
|
|
125
|
+
if (line) {
|
|
126
|
+
let row;
|
|
127
|
+
try {
|
|
128
|
+
row = JSON.parse(line);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
throw new Error("Invalid transcript JSON at byte " + (offset + start));
|
|
132
|
+
}
|
|
133
|
+
if (!row || typeof row !== "object" || Array.isArray(row))
|
|
134
|
+
throw new Error("Invalid transcript record");
|
|
135
|
+
const text = messageText(row);
|
|
136
|
+
if (text) {
|
|
137
|
+
const contentParts = splitText(redact(text));
|
|
138
|
+
for (const [index, content] of contentParts.entries()) {
|
|
139
|
+
const event = eventSchema.parse({
|
|
140
|
+
...scope,
|
|
141
|
+
sessionId,
|
|
142
|
+
kind: "progress",
|
|
143
|
+
content,
|
|
144
|
+
eventId: hash(JSON.stringify({
|
|
145
|
+
client,
|
|
146
|
+
scope: identityScope,
|
|
147
|
+
sessionId,
|
|
148
|
+
byte: offset + start,
|
|
149
|
+
index,
|
|
150
|
+
content,
|
|
151
|
+
})),
|
|
152
|
+
source: client +
|
|
153
|
+
":transcript:line-byte=" +
|
|
154
|
+
(offset + start) +
|
|
155
|
+
":part=" +
|
|
156
|
+
index,
|
|
157
|
+
});
|
|
158
|
+
queue
|
|
159
|
+
.prepare("INSERT OR IGNORE INTO pending VALUES(?,?)")
|
|
160
|
+
.run(event.eventId, JSON.stringify(event));
|
|
161
|
+
if (options.session)
|
|
162
|
+
queue
|
|
163
|
+
.prepare("INSERT OR IGNORE INTO pending_sessions VALUES(?,?)")
|
|
164
|
+
.run(event.eventId, JSON.stringify(options.session));
|
|
165
|
+
events++;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
records++;
|
|
170
|
+
start = end + 1;
|
|
171
|
+
}
|
|
172
|
+
if (read - start > maxLineBytes && records < 500)
|
|
173
|
+
throw new Error("Transcript line exceeds 1 MiB");
|
|
174
|
+
const nextOffset = offset + start;
|
|
175
|
+
queue
|
|
176
|
+
.prepare("INSERT INTO cursors VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET offset=excluded.offset,prefix=excluded.prefix")
|
|
177
|
+
.run(key, nextOffset, prefix(nextOffset));
|
|
178
|
+
queue.exec("COMMIT");
|
|
179
|
+
transaction = false;
|
|
180
|
+
return {
|
|
181
|
+
events,
|
|
182
|
+
records,
|
|
183
|
+
offset: nextOffset,
|
|
184
|
+
remainingBytes: size - nextOffset,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
if (transaction)
|
|
189
|
+
queue.exec("ROLLBACK");
|
|
190
|
+
queue.close();
|
|
191
|
+
closeSync(fd);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const id = z.string().trim().min(1).max(256);
|
|
3
|
+
export const sessionKeySchema = z
|
|
4
|
+
.object({ repoId: id, client: id, nativeSessionId: id })
|
|
5
|
+
.strict();
|
|
6
|
+
export const locationSchema = z
|
|
7
|
+
.object({
|
|
8
|
+
workspace: z.string().min(1).max(4096),
|
|
9
|
+
branch: id.optional(),
|
|
10
|
+
commit: id.optional(),
|
|
11
|
+
})
|
|
12
|
+
.strict();
|
|
13
|
+
export const checkpointEntrySchema = z
|
|
14
|
+
.object({
|
|
15
|
+
id,
|
|
16
|
+
category: z.enum([
|
|
17
|
+
"goal",
|
|
18
|
+
"decision",
|
|
19
|
+
"attempt",
|
|
20
|
+
"progress",
|
|
21
|
+
"test_result",
|
|
22
|
+
"blocker",
|
|
23
|
+
"next_step",
|
|
24
|
+
]),
|
|
25
|
+
content: z.string().trim().min(1).max(32768),
|
|
26
|
+
evidenceIds: z.array(id).min(1).max(100),
|
|
27
|
+
status: z.enum(["reported", "verified"]),
|
|
28
|
+
supersedes: id.optional(),
|
|
29
|
+
})
|
|
30
|
+
.strict();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const identifier = z.string().trim().min(1).max(256);
|
|
3
|
+
export const eventSchema = z.object({
|
|
4
|
+
eventId: identifier,
|
|
5
|
+
repoId: identifier,
|
|
6
|
+
featureId: identifier,
|
|
7
|
+
branch: identifier.optional(),
|
|
8
|
+
commit: identifier.optional(),
|
|
9
|
+
author: identifier.optional(),
|
|
10
|
+
sessionId: identifier.optional(),
|
|
11
|
+
source: z.string().max(2048).optional(),
|
|
12
|
+
kind: z.enum(["goal", "decision", "attempt", "progress", "test_result", "blocker", "next_step"]),
|
|
13
|
+
content: z.string().min(1).refine(s => Buffer.byteLength(s, "utf8") <= 32768, "Content exceeds 32 KiB"),
|
|
14
|
+
}).strict();
|
|
15
|
+
export const scopeSchema = z.object({
|
|
16
|
+
repoId: identifier,
|
|
17
|
+
featureId: identifier,
|
|
18
|
+
branch: identifier.optional(),
|
|
19
|
+
maxChars: z.number().int().min(256).max(24000).default(12000),
|
|
20
|
+
});
|
|
21
|
+
export const searchSchema = z.object({
|
|
22
|
+
repoId: identifier,
|
|
23
|
+
featureId: identifier,
|
|
24
|
+
query: z.string().max(256).default(""),
|
|
25
|
+
before: z.number().int().positive().optional(),
|
|
26
|
+
limit: z.number().int().min(1).max(20).default(10),
|
|
27
|
+
});
|