@causyne/agent 1.17.0 → 1.19.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/package.json +1 -1
- package/src/agentLoop.js +2348 -2518
- package/src/agentLoop.ts +2865 -0
- package/src/checkpoints.d.ts +4 -0
- package/src/cliMode.js +10 -3
- package/src/commands.js +274 -25
- package/src/config.js +22 -0
- package/src/contextManager.d.ts +10 -0
- package/src/continuum/adapters/antigravity.js +193 -0
- package/src/continuum/adapters/antigravity.ts +204 -0
- package/src/continuum/adapters/base.js +48 -0
- package/src/continuum/adapters/base.ts +57 -0
- package/src/continuum/adapters/causyne.js +66 -0
- package/src/continuum/adapters/causyne.ts +72 -0
- package/src/continuum/adapters/claudeCode.js +171 -0
- package/src/continuum/adapters/claudeCode.ts +179 -0
- package/src/continuum/adapters/codex.js +181 -0
- package/src/continuum/adapters/codex.ts +193 -0
- package/src/continuum/adapters/opencode.js +134 -0
- package/src/continuum/adapters/opencode.ts +153 -0
- package/src/continuum/compactor.js +54 -0
- package/src/continuum/compactor.ts +83 -0
- package/src/continuum/index.js +8 -0
- package/src/continuum/index.ts +9 -0
- package/src/continuum/ingest.js +63 -0
- package/src/continuum/ingest.ts +89 -0
- package/src/continuum/sanitizer.js +55 -0
- package/src/continuum/sanitizer.ts +59 -0
- package/src/continuum/scanner.js +86 -0
- package/src/continuum/scanner.ts +106 -0
- package/src/continuum/types.js +3 -0
- package/src/continuum/types.ts +51 -0
- package/src/continuum/ui/continuumPicker.js +111 -0
- package/src/continuum/ui/continuumPicker.ts +145 -0
- package/src/cortex/autoCondenser.d.ts +2 -0
- package/src/cortex/vectorDb.d.ts +1 -0
- package/src/designPalettes.d.ts +2 -0
- package/src/devMode.d.ts +1 -0
- package/src/dynamicCatalogue.js +140 -1
- package/src/dynamicCatalogue.ts +179 -1
- package/src/embodimentSession.d.ts +2 -0
- package/src/entrypoint.js +12 -0
- package/src/memory.d.ts +1 -0
- package/src/memoryManager.d.ts +1 -0
- package/src/modelIds.d.ts +2 -0
- package/src/outputStore.d.ts +4 -0
- package/src/providers/acp.js +321 -267
- package/src/providers/acp.ts +360 -0
- package/src/providers/antigravity.js +389 -239
- package/src/providers/antigravity.ts +450 -0
- package/src/providers/base.d.ts +68 -0
- package/src/providers/factory.d.ts +1 -0
- package/src/provisioning/capabilityProvisioner.d.ts +2 -0
- package/src/sandbox.d.ts +1 -0
- package/src/skillsManager.d.ts +3 -0
- package/src/slopDetector.d.ts +3 -0
- package/src/taskBuffer.d.ts +1 -0
- package/src/tokenTracker.d.ts +1 -0
- package/src/toolPacks.d.ts +8 -0
- package/src/tools/registry.d.ts +4 -0
- package/src/tui/chatRenderer.js +71 -12
- package/src/tui/clipboardSync.js +36 -0
- package/src/tui/clipboardSync.ts +36 -0
- package/src/tui/dialogs.js +15 -2
- package/src/tui/home.js +27 -25
- package/src/tui/modelPicker.js +221 -0
- package/src/tui/modelPicker.ts +290 -0
- package/src/tui/prompt.js +17 -6
- package/src/tui/selectList.js +20 -0
- package/src/tui/theme.js +4 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// OpenCode Continuum Adapter: reads sessions from ~/.local/share/opencode/opencode.db.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
8
|
+
import { BaseContinuumAdapter, isSafeIdentifier } from './base.js';
|
|
9
|
+
import type { ContinuumSessionSummary, ContinuumSessionDetail, ContinuumMessage } from '../types.js';
|
|
10
|
+
import { cleanUserText, cleanAssistantText } from '../sanitizer.js';
|
|
11
|
+
|
|
12
|
+
export class OpenCodeAdapter extends BaseContinuumAdapter {
|
|
13
|
+
readonly platform = 'opencode' as const;
|
|
14
|
+
readonly label = 'OpenCode';
|
|
15
|
+
|
|
16
|
+
private readonly dbPath = path.join(os.homedir(), '.local/share/opencode/opencode.db');
|
|
17
|
+
|
|
18
|
+
isAvailable(): boolean {
|
|
19
|
+
return fs.existsSync(this.dbPath);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async listSessions(limit = 100): Promise<ContinuumSessionSummary[]> {
|
|
23
|
+
if (!this.isAvailable()) return [];
|
|
24
|
+
|
|
25
|
+
let db: DatabaseSync | null = null;
|
|
26
|
+
try {
|
|
27
|
+
db = new DatabaseSync(this.dbPath, { readOnly: true });
|
|
28
|
+
const rows = db.prepare(`
|
|
29
|
+
SELECT id, title, time_created, time_updated, directory
|
|
30
|
+
FROM session
|
|
31
|
+
ORDER BY time_updated DESC
|
|
32
|
+
LIMIT ?
|
|
33
|
+
`).all(limit) as Array<{
|
|
34
|
+
id: string;
|
|
35
|
+
title: string;
|
|
36
|
+
time_created: number;
|
|
37
|
+
time_updated: number;
|
|
38
|
+
directory: string;
|
|
39
|
+
}>;
|
|
40
|
+
|
|
41
|
+
return rows.map(r => ({
|
|
42
|
+
id: `opencode:${r.id}`,
|
|
43
|
+
sourcePlatform: this.platform,
|
|
44
|
+
sourceLabel: this.label,
|
|
45
|
+
title: r.title || 'Untitled Session',
|
|
46
|
+
workspacePath: r.directory || '',
|
|
47
|
+
updatedAt: r.time_updated || r.time_created || Date.now(),
|
|
48
|
+
messageCount: 1,
|
|
49
|
+
snippet: `OpenCode workspace: ${path.basename(r.directory || '')}`
|
|
50
|
+
}));
|
|
51
|
+
} catch {
|
|
52
|
+
return [];
|
|
53
|
+
} finally {
|
|
54
|
+
if (db) {
|
|
55
|
+
try { db.close(); } catch { /* ignore */ }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async getSession(id: string): Promise<ContinuumSessionDetail | null> {
|
|
61
|
+
if (!this.isAvailable()) return null;
|
|
62
|
+
const rawId = id.startsWith('opencode:') ? id.slice('opencode:'.length) : id;
|
|
63
|
+
if (!isSafeIdentifier(rawId)) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let db: DatabaseSync | null = null;
|
|
68
|
+
try {
|
|
69
|
+
db = new DatabaseSync(this.dbPath, { readOnly: true });
|
|
70
|
+
const sessionRow = db.prepare(`
|
|
71
|
+
SELECT id, title, time_created, time_updated, directory
|
|
72
|
+
FROM session
|
|
73
|
+
WHERE id = ?
|
|
74
|
+
`).get(rawId) as {
|
|
75
|
+
id: string;
|
|
76
|
+
title: string;
|
|
77
|
+
time_created: number;
|
|
78
|
+
time_updated: number;
|
|
79
|
+
directory: string;
|
|
80
|
+
} | undefined;
|
|
81
|
+
|
|
82
|
+
if (!sessionRow) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const messageRows = db.prepare(`
|
|
87
|
+
SELECT m.id, json_extract(m.data, '$.role') as role,
|
|
88
|
+
m.time_created,
|
|
89
|
+
json_extract(p.data, '$.type') as ptype,
|
|
90
|
+
json_extract(p.data, '$.text') as ptext
|
|
91
|
+
FROM message m
|
|
92
|
+
JOIN part p ON m.id = p.message_id
|
|
93
|
+
WHERE m.session_id = ? AND json_extract(p.data, '$.type') = 'text'
|
|
94
|
+
ORDER BY m.time_created ASC, p.id ASC
|
|
95
|
+
`).all(rawId) as Array<{
|
|
96
|
+
id: string;
|
|
97
|
+
role: string;
|
|
98
|
+
time_created: number;
|
|
99
|
+
ptype: string;
|
|
100
|
+
ptext: string;
|
|
101
|
+
}>;
|
|
102
|
+
|
|
103
|
+
// Group multi-part messages into unified turns
|
|
104
|
+
const grouped = new Map<string, { role: 'user' | 'assistant'; time: number; texts: string[] }>();
|
|
105
|
+
for (const row of messageRows) {
|
|
106
|
+
if (!row.ptext || !row.ptext.trim()) continue;
|
|
107
|
+
const existing = grouped.get(row.id);
|
|
108
|
+
if (existing) {
|
|
109
|
+
existing.texts.push(row.ptext);
|
|
110
|
+
} else {
|
|
111
|
+
grouped.set(row.id, {
|
|
112
|
+
role: row.role === 'user' ? 'user' : 'assistant',
|
|
113
|
+
time: row.time_created,
|
|
114
|
+
texts: [row.ptext]
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const messages: ContinuumMessage[] = [];
|
|
120
|
+
for (const item of grouped.values()) {
|
|
121
|
+
const joined = item.texts.join('\n');
|
|
122
|
+
const cleaned = item.role === 'user' ? cleanUserText(joined) : cleanAssistantText(joined);
|
|
123
|
+
if (cleaned) {
|
|
124
|
+
messages.push({
|
|
125
|
+
role: item.role,
|
|
126
|
+
content: cleaned,
|
|
127
|
+
timestamp: item.time
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (messages.length === 0) return null;
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
id: `opencode:${sessionRow.id}`,
|
|
136
|
+
sourcePlatform: this.platform,
|
|
137
|
+
sourceLabel: this.label,
|
|
138
|
+
title: sessionRow.title || 'Untitled Session',
|
|
139
|
+
workspacePath: sessionRow.directory || '',
|
|
140
|
+
updatedAt: sessionRow.time_updated || sessionRow.time_created,
|
|
141
|
+
messageCount: messages.length,
|
|
142
|
+
snippet: messages[0]?.content.slice(0, 100) || '',
|
|
143
|
+
messages
|
|
144
|
+
};
|
|
145
|
+
} catch {
|
|
146
|
+
return null;
|
|
147
|
+
} finally {
|
|
148
|
+
if (db) {
|
|
149
|
+
try { db.close(); } catch { /* ignore */ }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Continuum Context Compactor: protects token budgets and preserves Root Anchor Intent.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
/**
|
|
4
|
+
* Compact a long multi-turn peer conversation into an optimal context window
|
|
5
|
+
* without losing root intention or recent critical decisions.
|
|
6
|
+
*/
|
|
7
|
+
export function compactContinuumMessages(messages, options = {}) {
|
|
8
|
+
const maxTurns = options.maxTurns ?? 30;
|
|
9
|
+
const recentTurns = options.recentTurns ?? 12;
|
|
10
|
+
if (messages.length <= maxTurns) {
|
|
11
|
+
return prefixHandshake(messages, options);
|
|
12
|
+
}
|
|
13
|
+
// Find root anchor (first user message)
|
|
14
|
+
const rootIndex = messages.findIndex(m => m.role === 'user');
|
|
15
|
+
const rootMessage = rootIndex !== -1
|
|
16
|
+
? messages[rootIndex]
|
|
17
|
+
: { role: 'user', content: 'Initial user turn' };
|
|
18
|
+
const recentStart = messages.length - recentTurns;
|
|
19
|
+
const recent = messages.slice(recentStart);
|
|
20
|
+
// If the root intent is already within the recent window, simply return recent
|
|
21
|
+
if (rootIndex !== -1 && rootIndex >= recentStart) {
|
|
22
|
+
return prefixHandshake(recent, options);
|
|
23
|
+
}
|
|
24
|
+
// Summarize middle turns between root and recent
|
|
25
|
+
const middleStart = rootIndex !== -1 ? rootIndex + 1 : 0;
|
|
26
|
+
const middleEnd = recentStart;
|
|
27
|
+
const middleCount = Math.max(0, middleEnd - middleStart);
|
|
28
|
+
const middleSummaryText = `[Continuum Context Condensation]\n` +
|
|
29
|
+
`Preserved initial root intent and the last ${recent.length} active turns.\n` +
|
|
30
|
+
`${middleCount} intermediate turns were compressed. ` +
|
|
31
|
+
`Original agent: ${options.platform || 'peer agent'}, topic: "${options.sourceTitle || 'Untitled'}".`;
|
|
32
|
+
const condensedMiddle = {
|
|
33
|
+
role: 'system',
|
|
34
|
+
content: middleSummaryText
|
|
35
|
+
};
|
|
36
|
+
const compacted = [
|
|
37
|
+
rootMessage,
|
|
38
|
+
condensedMiddle,
|
|
39
|
+
...recent
|
|
40
|
+
];
|
|
41
|
+
return prefixHandshake(compacted, options);
|
|
42
|
+
}
|
|
43
|
+
function prefixHandshake(messages, options) {
|
|
44
|
+
const platformLabel = options.platform ? options.platform.toUpperCase() : 'PEER AGENT';
|
|
45
|
+
const header = `[Continuum Handshake · Adopted from ${platformLabel}]\n` +
|
|
46
|
+
`Session: "${options.sourceTitle || 'Untitled'}"\n` +
|
|
47
|
+
`Original workspace: ${options.originalWorkspace || '(unspecified)'}\n` +
|
|
48
|
+
`Status: Adopted into Causyne Agent foreground session. Seamlessly continue the user's objective.`;
|
|
49
|
+
const anchorMessage = {
|
|
50
|
+
role: 'system',
|
|
51
|
+
content: header
|
|
52
|
+
};
|
|
53
|
+
return [anchorMessage, ...messages];
|
|
54
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Continuum Context Compactor: protects token budgets and preserves Root Anchor Intent.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
|
|
4
|
+
import type { ContinuumMessage, AgentPlatform } from './types.js';
|
|
5
|
+
|
|
6
|
+
export interface CompactionOptions {
|
|
7
|
+
maxTurns?: number;
|
|
8
|
+
recentTurns?: number;
|
|
9
|
+
platform?: AgentPlatform;
|
|
10
|
+
sourceTitle?: string;
|
|
11
|
+
originalWorkspace?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Compact a long multi-turn peer conversation into an optimal context window
|
|
16
|
+
* without losing root intention or recent critical decisions.
|
|
17
|
+
*/
|
|
18
|
+
export function compactContinuumMessages(
|
|
19
|
+
messages: ContinuumMessage[],
|
|
20
|
+
options: CompactionOptions = {}
|
|
21
|
+
): ContinuumMessage[] {
|
|
22
|
+
const maxTurns = options.maxTurns ?? 30;
|
|
23
|
+
const recentTurns = options.recentTurns ?? 12;
|
|
24
|
+
|
|
25
|
+
if (messages.length <= maxTurns) {
|
|
26
|
+
return prefixHandshake(messages, options);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Find root anchor (first user message)
|
|
30
|
+
const rootIndex = messages.findIndex(m => m.role === 'user');
|
|
31
|
+
const rootMessage: ContinuumMessage = rootIndex !== -1
|
|
32
|
+
? messages[rootIndex]!
|
|
33
|
+
: { role: 'user', content: 'Initial user turn' };
|
|
34
|
+
|
|
35
|
+
const recentStart = messages.length - recentTurns;
|
|
36
|
+
const recent = messages.slice(recentStart);
|
|
37
|
+
|
|
38
|
+
// If the root intent is already within the recent window, simply return recent
|
|
39
|
+
if (rootIndex !== -1 && rootIndex >= recentStart) {
|
|
40
|
+
return prefixHandshake(recent, options);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Summarize middle turns between root and recent
|
|
44
|
+
const middleStart = rootIndex !== -1 ? rootIndex + 1 : 0;
|
|
45
|
+
const middleEnd = recentStart;
|
|
46
|
+
const middleCount = Math.max(0, middleEnd - middleStart);
|
|
47
|
+
|
|
48
|
+
const middleSummaryText = `[Continuum Context Condensation]\n` +
|
|
49
|
+
`Preserved initial root intent and the last ${recent.length} active turns.\n` +
|
|
50
|
+
`${middleCount} intermediate turns were compressed. ` +
|
|
51
|
+
`Original agent: ${options.platform || 'peer agent'}, topic: "${options.sourceTitle || 'Untitled'}".`;
|
|
52
|
+
|
|
53
|
+
const condensedMiddle: ContinuumMessage = {
|
|
54
|
+
role: 'system',
|
|
55
|
+
content: middleSummaryText
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const compacted: ContinuumMessage[] = [
|
|
59
|
+
rootMessage,
|
|
60
|
+
condensedMiddle,
|
|
61
|
+
...recent
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
return prefixHandshake(compacted, options);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function prefixHandshake(
|
|
68
|
+
messages: ContinuumMessage[],
|
|
69
|
+
options: CompactionOptions
|
|
70
|
+
): ContinuumMessage[] {
|
|
71
|
+
const platformLabel = options.platform ? options.platform.toUpperCase() : 'PEER AGENT';
|
|
72
|
+
const header = `[Continuum Handshake · Adopted from ${platformLabel}]\n` +
|
|
73
|
+
`Session: "${options.sourceTitle || 'Untitled'}"\n` +
|
|
74
|
+
`Original workspace: ${options.originalWorkspace || '(unspecified)'}\n` +
|
|
75
|
+
`Status: Adopted into Causyne Agent foreground session. Seamlessly continue the user's objective.`;
|
|
76
|
+
|
|
77
|
+
const anchorMessage: ContinuumMessage = {
|
|
78
|
+
role: 'system',
|
|
79
|
+
content: header
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return [anchorMessage, ...messages];
|
|
83
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Causyne Continuum: Universal Cross-Agent Session Extraction, Inspection & Adoption Engine.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
export * from './types.js';
|
|
4
|
+
export * from './sanitizer.js';
|
|
5
|
+
export * from './compactor.js';
|
|
6
|
+
export * from './scanner.js';
|
|
7
|
+
export * from './ingest.js';
|
|
8
|
+
export * from './ui/continuumPicker.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Causyne Continuum: Universal Cross-Agent Session Extraction, Inspection & Adoption Engine.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
|
|
4
|
+
export * from './types.js';
|
|
5
|
+
export * from './sanitizer.js';
|
|
6
|
+
export * from './compactor.js';
|
|
7
|
+
export * from './scanner.js';
|
|
8
|
+
export * from './ingest.js';
|
|
9
|
+
export * from './ui/continuumPicker.js';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Continuum Ingestion Engine: adopts peer agent sessions into Causyne's native journal.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
import { ContinuumScanner } from './scanner.js';
|
|
4
|
+
import { compactContinuumMessages } from './compactor.js';
|
|
5
|
+
import { createSession, saveSessionMessages, getActiveSessionId, getSession, setActiveSession } from '../sessionJournal.js';
|
|
6
|
+
/**
|
|
7
|
+
* Safely adopt an external agent's session into Causyne's active session journal.
|
|
8
|
+
* Automatically saves and checkpoints any existing active session so no data is ever lost.
|
|
9
|
+
*/
|
|
10
|
+
export async function adoptContinuumSession(target, options = {}) {
|
|
11
|
+
let detail = null;
|
|
12
|
+
let targetId = typeof target === 'string' ? target : target.id;
|
|
13
|
+
if (typeof target === 'string') {
|
|
14
|
+
const scanner = options.scanner || new ContinuumScanner();
|
|
15
|
+
detail = await scanner.fetchSession(target);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
detail = target;
|
|
19
|
+
}
|
|
20
|
+
if (!detail || !detail.messages || detail.messages.length === 0) {
|
|
21
|
+
return {
|
|
22
|
+
ok: false,
|
|
23
|
+
sessionId: targetId,
|
|
24
|
+
title: 'Unknown',
|
|
25
|
+
sourcePlatform: 'antigravity',
|
|
26
|
+
messageCount: 0,
|
|
27
|
+
error: `Could not load session "${targetId}". It may have been moved or removed.`
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// 1. Checkpoint current active session if it holds unsaved messages
|
|
31
|
+
const currentActiveId = getActiveSessionId();
|
|
32
|
+
if (currentActiveId && options.currentLoopMessages && options.currentLoopMessages.length > 0) {
|
|
33
|
+
const existing = getSession(currentActiveId);
|
|
34
|
+
if (existing) {
|
|
35
|
+
saveSessionMessages(currentActiveId, options.currentLoopMessages);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 2. Compact messages & inject Continuum handshake anchor
|
|
39
|
+
const compacted = compactContinuumMessages(detail.messages, {
|
|
40
|
+
platform: detail.sourcePlatform,
|
|
41
|
+
sourceTitle: detail.title,
|
|
42
|
+
originalWorkspace: detail.workspacePath
|
|
43
|
+
});
|
|
44
|
+
// 3. Create a new sovereign Causyne session
|
|
45
|
+
const targetWorkspace = options.workspace || detail.workspacePath || process.cwd();
|
|
46
|
+
const title = `[Adopted] ${detail.title}`;
|
|
47
|
+
const newSession = createSession(targetWorkspace, title, true);
|
|
48
|
+
// 4. Ingest compacted messages into journal
|
|
49
|
+
const sessionMessages = compacted.map(m => ({
|
|
50
|
+
role: m.role,
|
|
51
|
+
content: m.content
|
|
52
|
+
}));
|
|
53
|
+
saveSessionMessages(newSession.id, sessionMessages);
|
|
54
|
+
setActiveSession(newSession.id);
|
|
55
|
+
return {
|
|
56
|
+
ok: true,
|
|
57
|
+
sessionId: newSession.id,
|
|
58
|
+
title: newSession.title,
|
|
59
|
+
sourcePlatform: detail.sourcePlatform,
|
|
60
|
+
messageCount: sessionMessages.length,
|
|
61
|
+
preservedActiveSessionId: currentActiveId
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Continuum Ingestion Engine: adopts peer agent sessions into Causyne's native journal.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
|
|
4
|
+
import type { ContinuumAdoptResult, ContinuumSessionDetail } from './types.js';
|
|
5
|
+
import { ContinuumScanner } from './scanner.js';
|
|
6
|
+
import { compactContinuumMessages } from './compactor.js';
|
|
7
|
+
import {
|
|
8
|
+
createSession,
|
|
9
|
+
saveSessionMessages,
|
|
10
|
+
getActiveSessionId,
|
|
11
|
+
getSession,
|
|
12
|
+
setActiveSession,
|
|
13
|
+
type SessionMessage
|
|
14
|
+
} from '../sessionJournal.js';
|
|
15
|
+
|
|
16
|
+
export interface IngestOptions {
|
|
17
|
+
workspace?: string | undefined;
|
|
18
|
+
scanner?: ContinuumScanner | undefined;
|
|
19
|
+
currentLoopMessages?: Array<{ role: string; content?: unknown }> | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Safely adopt an external agent's session into Causyne's active session journal.
|
|
24
|
+
* Automatically saves and checkpoints any existing active session so no data is ever lost.
|
|
25
|
+
*/
|
|
26
|
+
export async function adoptContinuumSession(
|
|
27
|
+
target: string | ContinuumSessionDetail,
|
|
28
|
+
options: IngestOptions = {}
|
|
29
|
+
): Promise<ContinuumAdoptResult> {
|
|
30
|
+
let detail: ContinuumSessionDetail | null = null;
|
|
31
|
+
let targetId = typeof target === 'string' ? target : target.id;
|
|
32
|
+
|
|
33
|
+
if (typeof target === 'string') {
|
|
34
|
+
const scanner = options.scanner || new ContinuumScanner();
|
|
35
|
+
detail = await scanner.fetchSession(target);
|
|
36
|
+
} else {
|
|
37
|
+
detail = target;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!detail || !detail.messages || detail.messages.length === 0) {
|
|
41
|
+
return {
|
|
42
|
+
ok: false,
|
|
43
|
+
sessionId: targetId,
|
|
44
|
+
title: 'Unknown',
|
|
45
|
+
sourcePlatform: 'antigravity',
|
|
46
|
+
messageCount: 0,
|
|
47
|
+
error: `Could not load session "${targetId}". It may have been moved or removed.`
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 1. Checkpoint current active session if it holds unsaved messages
|
|
52
|
+
const currentActiveId = getActiveSessionId();
|
|
53
|
+
if (currentActiveId && options.currentLoopMessages && options.currentLoopMessages.length > 0) {
|
|
54
|
+
const existing = getSession(currentActiveId);
|
|
55
|
+
if (existing) {
|
|
56
|
+
saveSessionMessages(currentActiveId, options.currentLoopMessages as SessionMessage[]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 2. Compact messages & inject Continuum handshake anchor
|
|
61
|
+
const compacted = compactContinuumMessages(detail.messages, {
|
|
62
|
+
platform: detail.sourcePlatform,
|
|
63
|
+
sourceTitle: detail.title,
|
|
64
|
+
originalWorkspace: detail.workspacePath
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// 3. Create a new sovereign Causyne session
|
|
68
|
+
const targetWorkspace = options.workspace || detail.workspacePath || process.cwd();
|
|
69
|
+
const title = `[Adopted] ${detail.title}`;
|
|
70
|
+
const newSession = createSession(targetWorkspace, title, true);
|
|
71
|
+
|
|
72
|
+
// 4. Ingest compacted messages into journal
|
|
73
|
+
const sessionMessages: SessionMessage[] = compacted.map(m => ({
|
|
74
|
+
role: m.role,
|
|
75
|
+
content: m.content
|
|
76
|
+
}));
|
|
77
|
+
|
|
78
|
+
saveSessionMessages(newSession.id, sessionMessages);
|
|
79
|
+
setActiveSession(newSession.id);
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
ok: true,
|
|
83
|
+
sessionId: newSession.id,
|
|
84
|
+
title: newSession.title,
|
|
85
|
+
sourcePlatform: detail.sourcePlatform,
|
|
86
|
+
messageCount: sessionMessages.length,
|
|
87
|
+
preservedActiveSessionId: currentActiveId
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Sanitizer for cross-agent prompt wrappers and verbose tool dumps.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
/** Strip bulky system wrapper tags and metadata blocks from user prompt. */
|
|
4
|
+
export function cleanUserText(text) {
|
|
5
|
+
if (!text)
|
|
6
|
+
return '';
|
|
7
|
+
let str = String(text);
|
|
8
|
+
str = str.replace(/<\/?USER_REQUEST>/gi, '');
|
|
9
|
+
if (str.includes('<ADDITIONAL_METADATA>')) {
|
|
10
|
+
str = str.split(/<ADDITIONAL_METADATA>/i)[0] || '';
|
|
11
|
+
}
|
|
12
|
+
if (str.includes('<environment_context>')) {
|
|
13
|
+
const parts = str.split(/<\/environment_context>/i);
|
|
14
|
+
if (parts.length > 1) {
|
|
15
|
+
str = parts.slice(1).join('\n');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (str.includes('<causyne_universal_skills>')) {
|
|
19
|
+
const parts = str.split(/<\/causyne_universal_skills>/i);
|
|
20
|
+
if (parts.length > 1) {
|
|
21
|
+
str = parts.slice(1).join('\n');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (str.includes('## My request:')) {
|
|
25
|
+
const parts = str.split('## My request:');
|
|
26
|
+
if (parts.length > 1) {
|
|
27
|
+
str = parts.slice(1).join('\n');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Remove common internal system instruction blocks if embedded inside user turn
|
|
31
|
+
str = str.replace(/\[System Instructions\][\s\S]*?(?=\[User\]|\n\n[a-zA-Z0-9]|$)/gi, '');
|
|
32
|
+
const lines = str
|
|
33
|
+
.split(/\r?\n/)
|
|
34
|
+
.map(line => line.trim())
|
|
35
|
+
.filter(line => line.length > 0);
|
|
36
|
+
return lines.join('\n').trim();
|
|
37
|
+
}
|
|
38
|
+
/** Summarize and truncate excessive tool dump text while preserving answer & rationale. */
|
|
39
|
+
export function cleanAssistantText(text, maxChars = 2000) {
|
|
40
|
+
if (!text)
|
|
41
|
+
return '';
|
|
42
|
+
const str = String(text).trim();
|
|
43
|
+
if (str.length <= maxChars) {
|
|
44
|
+
return str;
|
|
45
|
+
}
|
|
46
|
+
const half = Math.floor(maxChars / 2);
|
|
47
|
+
return `${str.slice(0, half)}\n\n... [output condensed for context efficiency] ...\n\n${str.slice(-half)}`;
|
|
48
|
+
}
|
|
49
|
+
/** Extract a clean, meaningful title from the first user request. */
|
|
50
|
+
export function deriveSessionTitle(firstPrompt, fallback = 'Untitled Session') {
|
|
51
|
+
const clean = cleanUserText(firstPrompt).replace(/\s+/g, ' ').trim();
|
|
52
|
+
if (!clean)
|
|
53
|
+
return fallback;
|
|
54
|
+
return clean.length > 60 ? `${clean.slice(0, 57)}…` : clean;
|
|
55
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Sanitizer for cross-agent prompt wrappers and verbose tool dumps.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
|
|
4
|
+
/** Strip bulky system wrapper tags and metadata blocks from user prompt. */
|
|
5
|
+
export function cleanUserText(text: unknown): string {
|
|
6
|
+
if (!text) return '';
|
|
7
|
+
let str = String(text);
|
|
8
|
+
|
|
9
|
+
str = str.replace(/<\/?USER_REQUEST>/gi, '');
|
|
10
|
+
if (str.includes('<ADDITIONAL_METADATA>')) {
|
|
11
|
+
str = str.split(/<ADDITIONAL_METADATA>/i)[0] || '';
|
|
12
|
+
}
|
|
13
|
+
if (str.includes('<environment_context>')) {
|
|
14
|
+
const parts = str.split(/<\/environment_context>/i);
|
|
15
|
+
if (parts.length > 1) {
|
|
16
|
+
str = parts.slice(1).join('\n');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (str.includes('<causyne_universal_skills>')) {
|
|
20
|
+
const parts = str.split(/<\/causyne_universal_skills>/i);
|
|
21
|
+
if (parts.length > 1) {
|
|
22
|
+
str = parts.slice(1).join('\n');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (str.includes('## My request:')) {
|
|
26
|
+
const parts = str.split('## My request:');
|
|
27
|
+
if (parts.length > 1) {
|
|
28
|
+
str = parts.slice(1).join('\n');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Remove common internal system instruction blocks if embedded inside user turn
|
|
33
|
+
str = str.replace(/\[System Instructions\][\s\S]*?(?=\[User\]|\n\n[a-zA-Z0-9]|$)/gi, '');
|
|
34
|
+
|
|
35
|
+
const lines = str
|
|
36
|
+
.split(/\r?\n/)
|
|
37
|
+
.map(line => line.trim())
|
|
38
|
+
.filter(line => line.length > 0);
|
|
39
|
+
|
|
40
|
+
return lines.join('\n').trim();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Summarize and truncate excessive tool dump text while preserving answer & rationale. */
|
|
44
|
+
export function cleanAssistantText(text: unknown, maxChars = 2000): string {
|
|
45
|
+
if (!text) return '';
|
|
46
|
+
const str = String(text).trim();
|
|
47
|
+
if (str.length <= maxChars) {
|
|
48
|
+
return str;
|
|
49
|
+
}
|
|
50
|
+
const half = Math.floor(maxChars / 2);
|
|
51
|
+
return `${str.slice(0, half)}\n\n... [output condensed for context efficiency] ...\n\n${str.slice(-half)}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Extract a clean, meaningful title from the first user request. */
|
|
55
|
+
export function deriveSessionTitle(firstPrompt: string, fallback = 'Untitled Session'): string {
|
|
56
|
+
const clean = cleanUserText(firstPrompt).replace(/\s+/g, ' ').trim();
|
|
57
|
+
if (!clean) return fallback;
|
|
58
|
+
return clean.length > 60 ? `${clean.slice(0, 57)}…` : clean;
|
|
59
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Continuum Scanner: orchestrates dynamic discovery across all peer agent adapters.
|
|
2
|
+
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
|
+
import { AntigravityAdapter } from './adapters/antigravity.js';
|
|
4
|
+
import { OpenCodeAdapter } from './adapters/opencode.js';
|
|
5
|
+
import { CodexAdapter } from './adapters/codex.js';
|
|
6
|
+
import { ClaudeCodeAdapter } from './adapters/claudeCode.js';
|
|
7
|
+
import { CausyneAdapter } from './adapters/causyne.js';
|
|
8
|
+
export class ContinuumScanner {
|
|
9
|
+
adapters;
|
|
10
|
+
constructor(customAdapters) {
|
|
11
|
+
this.adapters = customAdapters || [
|
|
12
|
+
new AntigravityAdapter(),
|
|
13
|
+
new OpenCodeAdapter(),
|
|
14
|
+
new CodexAdapter(),
|
|
15
|
+
new ClaudeCodeAdapter(),
|
|
16
|
+
new CausyneAdapter()
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
getAllAdapters() {
|
|
20
|
+
return this.adapters;
|
|
21
|
+
}
|
|
22
|
+
getAvailableAdapters() {
|
|
23
|
+
return this.adapters.filter(a => a.isAvailable());
|
|
24
|
+
}
|
|
25
|
+
async scanAll(options = {}) {
|
|
26
|
+
return this.scanAllSessions(options);
|
|
27
|
+
}
|
|
28
|
+
async scanAllSessions(options = {}) {
|
|
29
|
+
const activeAdapters = this.getAvailableAdapters().filter(a => {
|
|
30
|
+
if (!options.platform || options.platform === 'all')
|
|
31
|
+
return true;
|
|
32
|
+
return a.platform === options.platform;
|
|
33
|
+
});
|
|
34
|
+
const results = await Promise.allSettled(activeAdapters.map(adapter => adapter.listSessions(options.limit ?? 50)));
|
|
35
|
+
let all = [];
|
|
36
|
+
for (const res of results) {
|
|
37
|
+
if (res.status === 'fulfilled' && Array.isArray(res.value)) {
|
|
38
|
+
all.push(...res.value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Filter by text query if provided
|
|
42
|
+
if (options.query && options.query.trim()) {
|
|
43
|
+
const q = options.query.toLowerCase().trim();
|
|
44
|
+
all = all.filter(s => s.title.toLowerCase().includes(q) ||
|
|
45
|
+
s.snippet.toLowerCase().includes(q) ||
|
|
46
|
+
s.workspacePath.toLowerCase().includes(q) ||
|
|
47
|
+
s.sourceLabel.toLowerCase().includes(q));
|
|
48
|
+
}
|
|
49
|
+
// Filter by workspace if specified
|
|
50
|
+
if (options.workspaceFilter && options.workspaceFilter.trim()) {
|
|
51
|
+
const wf = options.workspaceFilter.toLowerCase().trim();
|
|
52
|
+
all = all.filter(s => s.workspacePath.toLowerCase().includes(wf));
|
|
53
|
+
}
|
|
54
|
+
// Sort by most recently updated
|
|
55
|
+
all.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
56
|
+
if (options.limit && options.limit > 0) {
|
|
57
|
+
return all.slice(0, options.limit);
|
|
58
|
+
}
|
|
59
|
+
return all;
|
|
60
|
+
}
|
|
61
|
+
async fetchSession(id) {
|
|
62
|
+
const colonIndex = id.indexOf(':');
|
|
63
|
+
if (colonIndex === -1) {
|
|
64
|
+
// Try all adapters sequentially
|
|
65
|
+
for (const adapter of this.getAvailableAdapters()) {
|
|
66
|
+
const detail = await adapter.getSession(id);
|
|
67
|
+
if (detail)
|
|
68
|
+
return detail;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const platformPrefix = id.slice(0, colonIndex).toLowerCase();
|
|
73
|
+
const adapter = this.adapters.find(a => a.platform === platformPrefix ||
|
|
74
|
+
(platformPrefix === 'claude' && a.platform === 'claude_code') ||
|
|
75
|
+
(platformPrefix === 'agy' && a.platform === 'antigravity'));
|
|
76
|
+
if (adapter) {
|
|
77
|
+
return adapter.getSession(id);
|
|
78
|
+
}
|
|
79
|
+
for (const a of this.getAvailableAdapters()) {
|
|
80
|
+
const detail = await a.getSession(id);
|
|
81
|
+
if (detail)
|
|
82
|
+
return detail;
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|