@tpsdev-ai/flair 0.3.17 → 0.3.19
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/README.md +30 -27
- package/dist/cli.js +38 -0
- package/package.json +1 -2
- package/resources/A2AAdapter.ts +0 -510
- package/resources/Agent.ts +0 -10
- package/resources/AgentCard.ts +0 -65
- package/resources/AgentSeed.ts +0 -119
- package/resources/IngestEvents.ts +0 -189
- package/resources/Integration.ts +0 -14
- package/resources/IssueTokens.ts +0 -29
- package/resources/Memory.ts +0 -151
- package/resources/MemoryBootstrap.ts +0 -323
- package/resources/MemoryConsolidate.ts +0 -121
- package/resources/MemoryFeed.ts +0 -48
- package/resources/MemoryMaintenance.ts +0 -95
- package/resources/MemoryReflect.ts +0 -122
- package/resources/OrgEvent.ts +0 -63
- package/resources/OrgEventCatchup.ts +0 -89
- package/resources/OrgEventMaintenance.ts +0 -37
- package/resources/SemanticSearch.ts +0 -197
- package/resources/SkillScan.ts +0 -146
- package/resources/Soul.ts +0 -10
- package/resources/SoulFeed.ts +0 -15
- package/resources/WorkspaceLatest.ts +0 -66
- package/resources/WorkspaceState.ts +0 -102
- package/resources/auth-middleware.ts +0 -501
- package/resources/embeddings-provider.ts +0 -61
- package/resources/embeddings.ts +0 -28
- package/resources/health.ts +0 -7
- package/resources/memory-feed-lib.ts +0 -22
- package/resources/table-helpers.ts +0 -46
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* POST /MemoryReflect
|
|
3
|
-
*
|
|
4
|
-
* Gathers recent memories for an agent and returns a structured reflection
|
|
5
|
-
* prompt. The agent feeds the prompt + memories to its LLM and writes
|
|
6
|
-
* insights back as persistent memories (with derivedFrom linking).
|
|
7
|
-
*
|
|
8
|
-
* Request:
|
|
9
|
-
* agentId string — which agent to reflect on
|
|
10
|
-
* scope string — "recent" | "tagged" | "all" (default: "recent")
|
|
11
|
-
* since string? — ISO timestamp lower bound (default: 24h ago)
|
|
12
|
-
* maxMemories number? — cap (default: 50)
|
|
13
|
-
* focus string? — "lessons_learned" | "patterns" | "decisions" | "errors" (default: "lessons_learned")
|
|
14
|
-
* tag string? — required when scope="tagged"
|
|
15
|
-
*
|
|
16
|
-
* Response:
|
|
17
|
-
* memories Memory[] — source memories included in the prompt
|
|
18
|
-
* prompt string — structured LLM prompt
|
|
19
|
-
* suggestedTags string[] — tags Flair detected in the source set
|
|
20
|
-
* count number — number of memories included
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { Resource, databases } from "@harperfast/harper";
|
|
24
|
-
import { isAdmin } from "./auth-middleware.js";
|
|
25
|
-
import { patchRecordSilent } from "./table-helpers.js";
|
|
26
|
-
|
|
27
|
-
const FOCUS_PROMPTS: Record<string, string> = {
|
|
28
|
-
lessons_learned:
|
|
29
|
-
"Review these memories and identify concrete lessons learned. For each lesson: what happened, what you learned, and how it should change future behavior. Write atomic memories with durability=persistent.",
|
|
30
|
-
patterns:
|
|
31
|
-
"Identify recurring patterns across these memories. What themes, approaches, or outcomes appear multiple times? Extract each pattern as a persistent memory.",
|
|
32
|
-
decisions:
|
|
33
|
-
"Catalog the key decisions made and their outcomes. For each: what was decided, why, and what resulted. Promote important decisions to persistent.",
|
|
34
|
-
errors:
|
|
35
|
-
"Extract errors, bugs, and failures. For each: what failed, root cause, and fix applied. These are high-value persistent memories.",
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export class ReflectMemories extends Resource {
|
|
39
|
-
async post(data: any) {
|
|
40
|
-
const {
|
|
41
|
-
agentId,
|
|
42
|
-
scope = "recent",
|
|
43
|
-
since,
|
|
44
|
-
maxMemories = 50,
|
|
45
|
-
focus = "lessons_learned",
|
|
46
|
-
tag,
|
|
47
|
-
} = data || {};
|
|
48
|
-
|
|
49
|
-
if (!agentId) return new Response(JSON.stringify({ error: "agentId required" }), { status: 400 });
|
|
50
|
-
|
|
51
|
-
// Auth: agent can only reflect on own memories unless admin
|
|
52
|
-
const actorId = (this as any).request?.tpsAgent;
|
|
53
|
-
if (actorId && actorId !== agentId && !(await isAdmin(actorId))) {
|
|
54
|
-
return new Response(JSON.stringify({ error: "forbidden: can only reflect on own memories" }), { status: 403 });
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const sinceDate = since ? new Date(since) : new Date(Date.now() - 24 * 3600_000);
|
|
58
|
-
const memories: any[] = [];
|
|
59
|
-
|
|
60
|
-
for await (const record of (databases as any).flair.Memory.search()) {
|
|
61
|
-
if (record.agentId !== agentId) continue;
|
|
62
|
-
if (record.archived) continue;
|
|
63
|
-
if (record.durability === "permanent") continue; // permanent memories don't need reflection
|
|
64
|
-
|
|
65
|
-
if (scope === "tagged") {
|
|
66
|
-
if (!tag || !(record.tags ?? []).includes(tag)) continue;
|
|
67
|
-
} else if (scope === "recent") {
|
|
68
|
-
if (!record.createdAt || new Date(record.createdAt) < sinceDate) continue;
|
|
69
|
-
}
|
|
70
|
-
// scope="all" passes everything
|
|
71
|
-
|
|
72
|
-
const { embedding, ...rest } = record;
|
|
73
|
-
memories.push(rest);
|
|
74
|
-
if (memories.length >= maxMemories) break;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
memories.sort((a, b) => (a.createdAt ?? "").localeCompare(b.createdAt ?? ""));
|
|
78
|
-
|
|
79
|
-
// Collect tags present in source memories
|
|
80
|
-
const tagSet = new Set<string>();
|
|
81
|
-
for (const m of memories) {
|
|
82
|
-
for (const t of m.tags ?? []) tagSet.add(t);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Build prompt
|
|
86
|
-
const focusText = FOCUS_PROMPTS[focus] ?? FOCUS_PROMPTS.lessons_learned;
|
|
87
|
-
const memorySummary = memories
|
|
88
|
-
.map((m, i) => `[${i + 1}] (${m.id}) ${m.createdAt?.slice(0, 10) ?? "?"}: ${m.content.slice(0, 300)}`)
|
|
89
|
-
.join("\n");
|
|
90
|
-
|
|
91
|
-
const prompt = `# Memory Reflection — ${agentId}
|
|
92
|
-
Focus: ${focus}
|
|
93
|
-
Scope: ${scope} (since ${sinceDate.toISOString()})
|
|
94
|
-
Memories: ${memories.length}
|
|
95
|
-
|
|
96
|
-
## Task
|
|
97
|
-
${focusText}
|
|
98
|
-
|
|
99
|
-
## Source Memories
|
|
100
|
-
${memorySummary || "(none)"}
|
|
101
|
-
|
|
102
|
-
## Instructions
|
|
103
|
-
For each insight:
|
|
104
|
-
1. Write a new memory with durability=persistent
|
|
105
|
-
2. Set derivedFrom=[<source memory ids>]
|
|
106
|
-
3. Set tags from the source memories where relevant
|
|
107
|
-
4. Keep each memory atomic — one insight per record`;
|
|
108
|
-
|
|
109
|
-
// Update lastReflected on source memories (read-modify-write to preserve embeddings)
|
|
110
|
-
const now = new Date().toISOString();
|
|
111
|
-
for (const m of memories) {
|
|
112
|
-
patchRecordSilent((databases as any).flair.Memory, m.id, { lastReflected: now });
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
memories,
|
|
117
|
-
prompt,
|
|
118
|
-
suggestedTags: [...tagSet].slice(0, 20),
|
|
119
|
-
count: memories.length,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
}
|
package/resources/OrgEvent.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OrgEvent.ts — Harper table resource for org-wide activity events.
|
|
3
|
-
*
|
|
4
|
-
* Auth: Ed25519 middleware sets request.tpsAgent.
|
|
5
|
-
* Write: authorId must match authenticated agent (or admin).
|
|
6
|
-
* Read: any authenticated participant can read (org-scoped).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { databases } from "@harperfast/harper";
|
|
10
|
-
|
|
11
|
-
export class OrgEvent extends (databases as any).flair.OrgEvent {
|
|
12
|
-
async post(content: any, context?: any) {
|
|
13
|
-
const agentId = context?.request?.tpsAgent;
|
|
14
|
-
|
|
15
|
-
// authorId must match authenticated agent (unless admin)
|
|
16
|
-
if (agentId && !context?.request?.tpsAgentIsAdmin && content.authorId !== agentId) {
|
|
17
|
-
return new Response(
|
|
18
|
-
JSON.stringify({ error: "forbidden: authorId must match authenticated agent" }),
|
|
19
|
-
{ status: 403, headers: { "Content-Type": "application/json" } },
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Generate composite ID if not provided
|
|
24
|
-
if (!content.id) {
|
|
25
|
-
content.id = `${content.authorId}-${Date.now()}`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
content.createdAt = new Date().toISOString();
|
|
29
|
-
|
|
30
|
-
// Harper 5: table resources use put() for create/upsert (post() removed)
|
|
31
|
-
return (databases as any).flair.OrgEvent.put(content);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async put(content: any, context?: any) {
|
|
35
|
-
const agentId = context?.request?.tpsAgent;
|
|
36
|
-
|
|
37
|
-
if (agentId && !context?.request?.tpsAgentIsAdmin && content.authorId !== agentId) {
|
|
38
|
-
return new Response(
|
|
39
|
-
JSON.stringify({ error: "forbidden: authorId must match authenticated agent" }),
|
|
40
|
-
{ status: 403, headers: { "Content-Type": "application/json" } },
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return (databases as any).flair.OrgEvent.put(content);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async delete(id: any, context?: any) {
|
|
48
|
-
const agentId = context?.request?.tpsAgent;
|
|
49
|
-
if (!agentId) return super.delete(id, context);
|
|
50
|
-
|
|
51
|
-
const record = await this.get(id);
|
|
52
|
-
if (!record) return super.delete(id, context);
|
|
53
|
-
|
|
54
|
-
if (!context?.request?.tpsAgentIsAdmin && record.authorId !== agentId) {
|
|
55
|
-
return new Response(
|
|
56
|
-
JSON.stringify({ error: "forbidden: cannot delete events authored by another agent" }),
|
|
57
|
-
{ status: 403, headers: { "Content-Type": "application/json" } },
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return super.delete(id, context);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OrgEventCatchup.ts — Custom resource for filtered event retrieval.
|
|
3
|
-
*
|
|
4
|
-
* GET /OrgEventCatchup/{participantId}?since=<ISO timestamp>
|
|
5
|
-
*
|
|
6
|
-
* Returns events where:
|
|
7
|
-
* - targetIds includes participantId OR targetIds is empty/null
|
|
8
|
-
* - createdAt >= since
|
|
9
|
-
* Sorted by createdAt ascending (oldest first for in-order processing).
|
|
10
|
-
* Limit 50 events max.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { Resource, databases } from "@harperfast/harper";
|
|
14
|
-
|
|
15
|
-
export class OrgEventCatchup extends Resource {
|
|
16
|
-
// HarperDB calls get(pathInfo, context) where pathInfo is the URL segment after /OrgEventCatchup/
|
|
17
|
-
async get(pathInfo?: any) {
|
|
18
|
-
const request = (this as any).request;
|
|
19
|
-
const callerAgent = request?.tpsAgent;
|
|
20
|
-
const callerIsAdmin = request?.tpsAgentIsAdmin === true;
|
|
21
|
-
|
|
22
|
-
// Harper routes /OrgEventCatchup/{id} with pathInfo.id as the path segment
|
|
23
|
-
const participantId: string | null =
|
|
24
|
-
(typeof pathInfo === "object" && pathInfo !== null ? (pathInfo as any).id : null) ??
|
|
25
|
-
(typeof pathInfo === "string" ? pathInfo : null) ??
|
|
26
|
-
(this as any).getId?.() ??
|
|
27
|
-
null;
|
|
28
|
-
|
|
29
|
-
if (!participantId) {
|
|
30
|
-
return new Response(
|
|
31
|
-
JSON.stringify({ error: "participantId required in path: GET /OrgEventCatchup/{participantId}" }),
|
|
32
|
-
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Auth: requesting agent must match participantId (or admin)
|
|
37
|
-
if (callerAgent && !callerIsAdmin && callerAgent !== participantId) {
|
|
38
|
-
return new Response(
|
|
39
|
-
JSON.stringify({ error: "forbidden: can only fetch events for yourself" }),
|
|
40
|
-
{ status: 403, headers: { "Content-Type": "application/json" } },
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Harper parses query params into pathInfo.conditions array:
|
|
45
|
-
// e.g. ?since=value → conditions: [{attribute:"since", value:"...", comparator:"equals"}]
|
|
46
|
-
// pathInfo.id is the URL path segment (participantId).
|
|
47
|
-
const since: string | null =
|
|
48
|
-
(typeof pathInfo === "object" && pathInfo !== null
|
|
49
|
-
? (pathInfo as any).conditions?.find((c: any) => c.attribute === "since")?.value ?? null
|
|
50
|
-
: null);
|
|
51
|
-
if (!since) {
|
|
52
|
-
return new Response(
|
|
53
|
-
JSON.stringify({ error: "since query parameter required (ISO timestamp)" }),
|
|
54
|
-
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const sinceDate = new Date(since);
|
|
59
|
-
if (isNaN(sinceDate.getTime())) {
|
|
60
|
-
return new Response(
|
|
61
|
-
JSON.stringify({ error: "invalid since timestamp" }),
|
|
62
|
-
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Query all OrgEvents and filter in-memory
|
|
67
|
-
const results: any[] = [];
|
|
68
|
-
for await (const event of (databases as any).flair.OrgEvent.search()) {
|
|
69
|
-
// Filter by createdAt >= since
|
|
70
|
-
if (!event.createdAt || event.createdAt < since) continue;
|
|
71
|
-
|
|
72
|
-
// Filter by targetIds: includes participantId OR empty/null
|
|
73
|
-
const targets = event.targetIds;
|
|
74
|
-
const isTargeted = !targets || targets.length === 0 || targets.includes(participantId);
|
|
75
|
-
if (!isTargeted) continue;
|
|
76
|
-
|
|
77
|
-
// Skip expired events
|
|
78
|
-
if (event.expiresAt && new Date(event.expiresAt) < new Date()) continue;
|
|
79
|
-
|
|
80
|
-
results.push(event);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Sort ascending by createdAt (oldest first)
|
|
84
|
-
results.sort((a: any, b: any) => (a.createdAt || "").localeCompare(b.createdAt || ""));
|
|
85
|
-
|
|
86
|
-
// Limit to 50
|
|
87
|
-
return results.slice(0, 50);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OrgEventMaintenance.ts — Maintenance worker for expired OrgEvents.
|
|
3
|
-
*
|
|
4
|
-
* POST /OrgEventMaintenance/ — deletes all records where expiresAt < now.
|
|
5
|
-
* Auth: admin only.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { Resource, databases } from "@harperfast/harper";
|
|
9
|
-
|
|
10
|
-
export class OrgEventMaintenance extends Resource {
|
|
11
|
-
async post(_data: any, context?: any) {
|
|
12
|
-
// Admin-only
|
|
13
|
-
if (!context?.request?.tpsAgentIsAdmin) {
|
|
14
|
-
return new Response(
|
|
15
|
-
JSON.stringify({ error: "forbidden: admin only" }),
|
|
16
|
-
{ status: 403, headers: { "Content-Type": "application/json" } },
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const now = new Date().toISOString();
|
|
21
|
-
const toDelete: string[] = [];
|
|
22
|
-
|
|
23
|
-
for await (const event of (databases as any).flair.OrgEvent.search()) {
|
|
24
|
-
if (event.expiresAt && event.expiresAt < now) {
|
|
25
|
-
toDelete.push(event.id);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
for (const id of toDelete) {
|
|
30
|
-
try {
|
|
31
|
-
await (databases as any).flair.OrgEvent.delete(id);
|
|
32
|
-
} catch {}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return { deleted: toDelete.length };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import { Resource, databases } from "@harperfast/harper";
|
|
2
|
-
import { getEmbedding, getMode } from "./embeddings-provider.js";
|
|
3
|
-
import { patchRecord } from "./table-helpers.js";
|
|
4
|
-
|
|
5
|
-
function cosineSimilarity(a: number[], b: number[]): number {
|
|
6
|
-
let dot = 0;
|
|
7
|
-
const len = Math.min(a.length, b.length);
|
|
8
|
-
for (let i = 0; i < len; i++) dot += a[i] * b[i];
|
|
9
|
-
return dot;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// ─── Temporal Decay + Relevance Scoring ─────────────────────────────────────
|
|
13
|
-
|
|
14
|
-
const DURABILITY_WEIGHTS: Record<string, number> = {
|
|
15
|
-
permanent: 1.0,
|
|
16
|
-
persistent: 0.9,
|
|
17
|
-
standard: 0.7,
|
|
18
|
-
ephemeral: 0.4,
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// Half-life in days for exponential decay per durability level
|
|
22
|
-
const DECAY_HALF_LIFE_DAYS: Record<string, number> = {
|
|
23
|
-
permanent: Infinity, // never decays
|
|
24
|
-
persistent: 90,
|
|
25
|
-
standard: 30,
|
|
26
|
-
ephemeral: 7,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
function recencyFactor(createdAt: string, durability: string): number {
|
|
30
|
-
const halfLife = DECAY_HALF_LIFE_DAYS[durability] ?? 30;
|
|
31
|
-
if (halfLife === Infinity) return 1.0;
|
|
32
|
-
const ageDays = (Date.now() - Date.parse(createdAt)) / (1000 * 60 * 60 * 24);
|
|
33
|
-
const lambda = Math.LN2 / halfLife;
|
|
34
|
-
return Math.exp(-lambda * ageDays);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function retrievalBoost(retrievalCount: number): number {
|
|
38
|
-
if (!retrievalCount || retrievalCount <= 0) return 1.0;
|
|
39
|
-
return 1.0 + 0.1 * Math.log2(retrievalCount); // gentle boost: 10 retrievals → ~1.33x
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function compositeScore(
|
|
43
|
-
semanticScore: number,
|
|
44
|
-
record: { durability?: string; createdAt?: string; retrievalCount?: number; supersedes?: string },
|
|
45
|
-
): number {
|
|
46
|
-
const durability = record.durability ?? "standard";
|
|
47
|
-
const dWeight = DURABILITY_WEIGHTS[durability] ?? 0.7;
|
|
48
|
-
const rFactor = record.createdAt ? recencyFactor(record.createdAt, durability) : 1.0;
|
|
49
|
-
const rBoost = retrievalBoost(record.retrievalCount ?? 0);
|
|
50
|
-
return semanticScore * dWeight * rFactor * rBoost;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export class SemanticSearch extends Resource {
|
|
54
|
-
async post(data: any) {
|
|
55
|
-
const { agentId, q, queryEmbedding, tag, subject, subjects, limit = 10, includeSuperseded = false, scoring = "composite", minScore = 0, since } = data || {};
|
|
56
|
-
const subjectFilter = subjects
|
|
57
|
-
? new Set((subjects as string[]).map((s: string) => s.toLowerCase()))
|
|
58
|
-
: subject
|
|
59
|
-
? new Set([(subject as string).toLowerCase()])
|
|
60
|
-
: null;
|
|
61
|
-
|
|
62
|
-
// Defense-in-depth: verify agentId matches authenticated agent.
|
|
63
|
-
// The middleware already enforces this for non-admins, but double-check here
|
|
64
|
-
// so direct Harper API calls (e.g., admin scripts) are also scoped correctly.
|
|
65
|
-
const authenticatedAgent: string | undefined = (this as any).request?.headers?.get?.("x-tps-agent");
|
|
66
|
-
const callerIsAdmin: boolean = (this as any).request?.tpsAgentIsAdmin === true;
|
|
67
|
-
if (authenticatedAgent && !callerIsAdmin && agentId && agentId !== authenticatedAgent) {
|
|
68
|
-
return new Response(JSON.stringify({
|
|
69
|
-
error: "forbidden: agentId must match authenticated agent",
|
|
70
|
-
}), { status: 403, headers: { "Content-Type": "application/json" } });
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Determine searchable agent IDs (own + granted)
|
|
74
|
-
const searchAgentIds = new Set<string>();
|
|
75
|
-
if (agentId) searchAgentIds.add(agentId);
|
|
76
|
-
|
|
77
|
-
if (agentId) {
|
|
78
|
-
try {
|
|
79
|
-
for await (const grant of (databases as any).flair.MemoryGrant.search({
|
|
80
|
-
conditions: [{ attribute: "granteeId", comparator: "equals", value: agentId }],
|
|
81
|
-
})) {
|
|
82
|
-
if (grant.scope === "search" || grant.scope === "read") {
|
|
83
|
-
searchAgentIds.add(grant.ownerId);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
} catch { /* MemoryGrant may not exist */ }
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Generate query embedding
|
|
90
|
-
let qEmb = queryEmbedding;
|
|
91
|
-
if (!qEmb && q) {
|
|
92
|
-
if (getMode() !== "none") {
|
|
93
|
-
try { qEmb = await getEmbedding(String(q).slice(0, 500)); } catch {}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ─── Temporal intent detection ────────────────────────────────────────────
|
|
98
|
-
// If the query implies a time window and no explicit `since` was provided,
|
|
99
|
-
// auto-detect and apply a recency boost.
|
|
100
|
-
let sinceDate: Date | null = since ? new Date(since) : null;
|
|
101
|
-
let temporalBoost = 1.0;
|
|
102
|
-
if (q && !sinceDate) {
|
|
103
|
-
const lq = String(q).toLowerCase();
|
|
104
|
-
if (/\btoday\b|\bthis morning\b|\bthis afternoon\b/.test(lq)) {
|
|
105
|
-
const d = new Date(); d.setHours(0, 0, 0, 0);
|
|
106
|
-
sinceDate = d;
|
|
107
|
-
temporalBoost = 1.5; // boost recent results for temporal queries
|
|
108
|
-
} else if (/\byesterday\b/.test(lq)) {
|
|
109
|
-
const d = new Date(); d.setDate(d.getDate() - 1); d.setHours(0, 0, 0, 0);
|
|
110
|
-
sinceDate = d;
|
|
111
|
-
temporalBoost = 1.3;
|
|
112
|
-
} else if (/\bthis week\b|\blast few days\b/.test(lq)) {
|
|
113
|
-
sinceDate = new Date(Date.now() - 7 * 24 * 3600_000);
|
|
114
|
-
temporalBoost = 1.2;
|
|
115
|
-
} else if (/\blast week\b/.test(lq)) {
|
|
116
|
-
sinceDate = new Date(Date.now() - 14 * 24 * 3600_000);
|
|
117
|
-
temporalBoost = 1.1;
|
|
118
|
-
} else if (/\brecently\b|\blately\b/.test(lq)) {
|
|
119
|
-
sinceDate = new Date(Date.now() - 3 * 24 * 3600_000);
|
|
120
|
-
temporalBoost = 1.3;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const results: any[] = [];
|
|
125
|
-
|
|
126
|
-
// Iterate ALL memories, filter by agent ID set
|
|
127
|
-
for await (const record of (databases as any).flair.Memory.search()) {
|
|
128
|
-
// Filter by agent
|
|
129
|
-
if (searchAgentIds.size > 0 && !searchAgentIds.has(record.agentId)) {
|
|
130
|
-
if (record.visibility !== "office") continue;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (record.archived === true) continue;
|
|
134
|
-
if (record.expiresAt && Date.parse(record.expiresAt) < Date.now()) continue;
|
|
135
|
-
if (tag && !(record.tags || []).includes(tag)) continue;
|
|
136
|
-
if (subjectFilter && record.subject && !subjectFilter.has(String(record.subject).toLowerCase())) continue;
|
|
137
|
-
// Time window filter
|
|
138
|
-
if (sinceDate && record.createdAt && new Date(record.createdAt) < sinceDate) continue;
|
|
139
|
-
|
|
140
|
-
let semanticScore = 0;
|
|
141
|
-
let keywordHit = false;
|
|
142
|
-
if (q && String(record.content || "").toLowerCase().includes(String(q).toLowerCase())) {
|
|
143
|
-
keywordHit = true;
|
|
144
|
-
}
|
|
145
|
-
if (qEmb && record.embedding && qEmb.length === record.embedding.length) {
|
|
146
|
-
semanticScore = cosineSimilarity(qEmb, record.embedding);
|
|
147
|
-
}
|
|
148
|
-
// Keyword match is a small tiebreaker (5%), not a primary signal.
|
|
149
|
-
// This prevents weak semantic matches from ranking high just because
|
|
150
|
-
// a query word appears in the content.
|
|
151
|
-
const rawScore = semanticScore + (keywordHit ? 0.05 : 0);
|
|
152
|
-
if (q && rawScore === 0) continue;
|
|
153
|
-
|
|
154
|
-
// Apply composite scoring (temporal decay + durability + retrieval boost + temporal intent)
|
|
155
|
-
let finalScore = scoring === "raw" ? rawScore : compositeScore(rawScore, record);
|
|
156
|
-
if (temporalBoost > 1.0) finalScore *= temporalBoost;
|
|
157
|
-
|
|
158
|
-
const { embedding, ...rest } = record;
|
|
159
|
-
results.push({
|
|
160
|
-
...rest,
|
|
161
|
-
_score: Math.round(finalScore * 1000) / 1000,
|
|
162
|
-
_rawScore: scoring !== "raw" ? Math.round(rawScore * 1000) / 1000 : undefined,
|
|
163
|
-
_source: record.agentId !== agentId ? record.agentId : undefined,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Build superseded set and filter (unless caller opts in to see full history)
|
|
168
|
-
let filteredResults = results;
|
|
169
|
-
if (!includeSuperseded) {
|
|
170
|
-
const supersededIds = new Set<string>();
|
|
171
|
-
for (const r of results) {
|
|
172
|
-
if (r.supersedes) supersededIds.add(r.supersedes);
|
|
173
|
-
}
|
|
174
|
-
filteredResults = results.filter((r: any) => !supersededIds.has(r.id));
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Apply minimum score filter
|
|
178
|
-
if (minScore > 0) {
|
|
179
|
-
filteredResults = filteredResults.filter((r: any) => r._score >= minScore);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
filteredResults.sort((a: any, b: any) => b._score - a._score);
|
|
183
|
-
const topResults = filteredResults.slice(0, limit);
|
|
184
|
-
|
|
185
|
-
// Async hit tracking — don't block the response
|
|
186
|
-
// Use patchRecord to avoid wiping other fields (embedding, content, etc.)
|
|
187
|
-
const now = new Date().toISOString();
|
|
188
|
-
for (const r of topResults) {
|
|
189
|
-
patchRecord((databases as any).flair.Memory, r.id, {
|
|
190
|
-
retrievalCount: (r.retrievalCount || 0) + 1,
|
|
191
|
-
lastRetrieved: now,
|
|
192
|
-
}).catch(() => {});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return { results: topResults };
|
|
196
|
-
}
|
|
197
|
-
}
|
package/resources/SkillScan.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { Resource } from "@harperfast/harper";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* POST /SkillScan/
|
|
5
|
-
*
|
|
6
|
-
* Static analysis of skill content for security violations.
|
|
7
|
-
* Scans for shell commands, network calls, fs writes, env access,
|
|
8
|
-
* encoded payloads, zero-width chars, and homoglyphs.
|
|
9
|
-
*
|
|
10
|
-
* Request: { content: string }
|
|
11
|
-
* Response: { safe, violations, riskLevel }
|
|
12
|
-
*
|
|
13
|
-
* Auth: any authenticated agent (read-only analysis).
|
|
14
|
-
* Size limit: 8KB (8192 bytes).
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
interface Violation {
|
|
18
|
-
type: string;
|
|
19
|
-
line: number;
|
|
20
|
-
content: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type RiskLevel = "low" | "medium" | "high" | "critical";
|
|
24
|
-
|
|
25
|
-
const SHELL_PATTERNS = [
|
|
26
|
-
{ regex: /\bexec\s*\(/, type: "shell_command" },
|
|
27
|
-
{ regex: /\bspawn\s*\(/, type: "shell_command" },
|
|
28
|
-
{ regex: /\bsystem\s*\(/, type: "shell_command" },
|
|
29
|
-
{ regex: /`[^`]*`/, type: "shell_backtick" },
|
|
30
|
-
{ regex: /\bchild_process\b/, type: "shell_command" },
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
const NETWORK_PATTERNS = [
|
|
34
|
-
{ regex: /\bfetch\s*\(/, type: "network_call" },
|
|
35
|
-
{ regex: /\bcurl\b/, type: "network_call" },
|
|
36
|
-
{ regex: /https?:\/\//, type: "url_reference" },
|
|
37
|
-
{ regex: /\bXMLHttpRequest\b/, type: "network_call" },
|
|
38
|
-
{ regex: /\baxios\b/, type: "network_call" },
|
|
39
|
-
];
|
|
40
|
-
|
|
41
|
-
const FS_PATTERNS = [
|
|
42
|
-
{ regex: /\bfs\.write/, type: "fs_write" },
|
|
43
|
-
{ regex: /\bwriteFile/, type: "fs_write" },
|
|
44
|
-
{ regex: />[>]?\s*[\/~]/, type: "fs_redirect" },
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
const ENV_PATTERNS = [
|
|
48
|
-
{ regex: /\bprocess\.env\b/, type: "env_access" },
|
|
49
|
-
{ regex: /\$ENV\b/, type: "env_access" },
|
|
50
|
-
{ regex: /\$\{?\w+\}?/, type: "env_variable" },
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
const ENCODING_PATTERNS = [
|
|
54
|
-
{ regex: /\batob\s*\(/, type: "base64_decode" },
|
|
55
|
-
{ regex: /\bbtoa\s*\(/, type: "base64_encode" },
|
|
56
|
-
{ regex: /Buffer\.from\s*\([^)]*,\s*['"]base64['"]/, type: "base64_decode" },
|
|
57
|
-
{ regex: /Buffer\.from\s*\([^)]*,\s*['"]hex['"]/, type: "hex_decode" },
|
|
58
|
-
{ regex: /\\x[0-9a-fA-F]{2}/, type: "hex_escape" },
|
|
59
|
-
{ regex: /\\u200[b-f]|\\u2060|\\ufeff/, type: "zero_width_char" },
|
|
60
|
-
];
|
|
61
|
-
|
|
62
|
-
// Unicode zero-width and homoglyph detection (raw chars)
|
|
63
|
-
const UNICODE_PATTERNS = [
|
|
64
|
-
{ regex: /[\u200B-\u200F\u2060\uFEFF]/, type: "zero_width_char" },
|
|
65
|
-
{ regex: /[\u0410-\u044F]/, type: "cyrillic_homoglyph" }, // Cyrillic chars that look like Latin
|
|
66
|
-
];
|
|
67
|
-
|
|
68
|
-
const ALL_PATTERNS = [
|
|
69
|
-
...SHELL_PATTERNS,
|
|
70
|
-
...NETWORK_PATTERNS,
|
|
71
|
-
...FS_PATTERNS,
|
|
72
|
-
...ENV_PATTERNS,
|
|
73
|
-
...ENCODING_PATTERNS,
|
|
74
|
-
...UNICODE_PATTERNS,
|
|
75
|
-
];
|
|
76
|
-
|
|
77
|
-
function assessRisk(violations: Violation[]): RiskLevel {
|
|
78
|
-
if (violations.length === 0) return "low";
|
|
79
|
-
|
|
80
|
-
const types = new Set(violations.map((v) => v.type));
|
|
81
|
-
const hasShell = types.has("shell_command") || types.has("shell_backtick");
|
|
82
|
-
const hasNetwork = types.has("network_call");
|
|
83
|
-
const hasFs = types.has("fs_write") || types.has("fs_redirect");
|
|
84
|
-
const hasZeroWidth = types.has("zero_width_char");
|
|
85
|
-
const hasHomoglyph = types.has("cyrillic_homoglyph");
|
|
86
|
-
|
|
87
|
-
// Critical: shell + encoding, or zero-width/homoglyph obfuscation
|
|
88
|
-
if ((hasShell && (types.has("base64_decode") || types.has("hex_decode"))) ||
|
|
89
|
-
hasZeroWidth || hasHomoglyph) {
|
|
90
|
-
return "critical";
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// High: direct shell commands or fs writes
|
|
94
|
-
if (hasShell || hasFs) return "high";
|
|
95
|
-
|
|
96
|
-
// Medium: network calls or encoding without shell
|
|
97
|
-
if (hasNetwork || types.has("base64_decode") || types.has("hex_decode")) return "medium";
|
|
98
|
-
|
|
99
|
-
return "low";
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export class SkillScan extends Resource {
|
|
103
|
-
async post(data: any, context?: any) {
|
|
104
|
-
const { content } = data || {};
|
|
105
|
-
|
|
106
|
-
if (!content || typeof content !== "string") {
|
|
107
|
-
return new Response(
|
|
108
|
-
JSON.stringify({ error: "content (string) required" }),
|
|
109
|
-
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// 8KB size limit
|
|
114
|
-
const byteLength = new TextEncoder().encode(content).length;
|
|
115
|
-
if (byteLength > 8192) {
|
|
116
|
-
return new Response(
|
|
117
|
-
JSON.stringify({ error: `Content exceeds 8KB limit (${byteLength} bytes)` }),
|
|
118
|
-
{ status: 413, headers: { "Content-Type": "application/json" } },
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const lines = content.split("\n");
|
|
123
|
-
const violations: Violation[] = [];
|
|
124
|
-
|
|
125
|
-
for (let i = 0; i < lines.length; i++) {
|
|
126
|
-
const line = lines[i];
|
|
127
|
-
for (const pattern of ALL_PATTERNS) {
|
|
128
|
-
if (pattern.regex.test(line)) {
|
|
129
|
-
violations.push({
|
|
130
|
-
type: pattern.type,
|
|
131
|
-
line: i + 1,
|
|
132
|
-
content: line.trim().slice(0, 200),
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const riskLevel = assessRisk(violations);
|
|
139
|
-
|
|
140
|
-
return {
|
|
141
|
-
safe: violations.length === 0,
|
|
142
|
-
violations,
|
|
143
|
-
riskLevel,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
}
|
package/resources/Soul.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { databases } from "@harperfast/harper";
|
|
2
|
-
|
|
3
|
-
export class Soul extends (databases as any).flair.Soul {
|
|
4
|
-
async post(content: any, context?: any) {
|
|
5
|
-
content.durability ||= "permanent";
|
|
6
|
-
content.createdAt = new Date().toISOString();
|
|
7
|
-
content.updatedAt = content.createdAt;
|
|
8
|
-
return super.post(content, context);
|
|
9
|
-
}
|
|
10
|
-
}
|
package/resources/SoulFeed.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Resource, databases } from '@harperfast/harper';
|
|
2
|
-
|
|
3
|
-
export class FeedSouls extends Resource {
|
|
4
|
-
async *connect(target: any, incomingMessages: any) {
|
|
5
|
-
const subscription = await (databases as any).flair.Soul.subscribe(target);
|
|
6
|
-
|
|
7
|
-
if (!incomingMessages) {
|
|
8
|
-
return subscription;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
for await (const event of subscription) {
|
|
12
|
-
yield event;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|