@amoree-code/ocean 1.1.0 → 1.3.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.
@@ -1,165 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { mkdir, readFile, writeFile } from "node:fs/promises";
3
- import path from "node:path";
4
- import { redactRuntimeText } from "@ocean/core/domain/redaction/redaction.js";
5
- import { oceanPath, SYSTEM_DIR } from "@ocean/core/shared/paths.js";
6
- import { addSkillCandidate } from "./skill-curation.js";
7
- const observationsFile = () => oceanPath(SYSTEM_DIR, "skills", "observations.json");
8
- async function load() {
9
- try {
10
- return JSON.parse(await readFile(observationsFile(), "utf8"));
11
- }
12
- catch (error) {
13
- if (error.code === "ENOENT")
14
- return [];
15
- throw error;
16
- }
17
- }
18
- async function save(items) {
19
- await mkdir(path.dirname(observationsFile()), { recursive: true });
20
- await writeFile(observationsFile(), `${JSON.stringify(items.slice(-200), null, 2)}\n`, { mode: 0o600 });
21
- }
22
- function observationId(sessionId, signalType, summary) {
23
- return `obs-${createHash("sha256").update(`${sessionId}:${signalType}:${summary}`).digest("hex").slice(0, 16)}`;
24
- }
25
- const CORRECTION_PATTERN = /\bno,|\bactually,|\bnot that\b|\binstead of\b|\bshould use\b|\bthat'?s wrong\b/i;
26
- function userInputTexts(events) {
27
- return events
28
- .filter((event) => event.type === "user_input")
29
- .map((event) => ({
30
- id: event.eventId,
31
- text: redactRuntimeText(event.data).replace(/\s+/g, " ").trim(),
32
- }))
33
- .filter((event) => event.text.length > 12)
34
- .slice(0, 80);
35
- }
36
- // True when the user pushed back in this session ("no, use X", "that's wrong").
37
- export function hasUserCorrection(events) {
38
- return userInputTexts(events).some((event) => CORRECTION_PATTERN.test(event.text));
39
- }
40
- // What a completed, proven session is worth remembering. Pure: reads events, writes nothing.
41
- export function detectSessionSignals(events) {
42
- const proven = events.filter((event) => event.type === "evidence" && /"result"\s*:\s*"proven"/.test(event.data));
43
- if (!proven.length)
44
- return [];
45
- const outputs = events
46
- .filter((event) => ["provider_output", "text", "json"].includes(event.type))
47
- .map((event) => ({
48
- id: event.eventId,
49
- text: redactRuntimeText(event.data).replace(/\s+/g, " ").trim(),
50
- }))
51
- .filter((event) => event.text.length > 12)
52
- .slice(0, 80);
53
- const userInputs = userInputTexts(events);
54
- const candidates = [];
55
- const decisions = outputs.filter((event) => /\bdecision\s*:/i.test(event.text));
56
- if (decisions.length)
57
- candidates.push({
58
- signalType: "explicit-decision",
59
- summary: decisions[0].text.slice(0, 500),
60
- refs: decisions.slice(0, 4).map((event) => `event:${event.id}`),
61
- confidence: 0.9,
62
- });
63
- const counts = new Map();
64
- for (const event of outputs) {
65
- const normalized = event.text.toLocaleLowerCase();
66
- const current = counts.get(normalized) ?? { count: 0, refs: [] };
67
- current.count += 1;
68
- current.refs.push(`event:${event.id}`);
69
- counts.set(normalized, current);
70
- }
71
- const repeated = [...counts.entries()].find(([, value]) => value.count >= 2);
72
- if (repeated)
73
- candidates.push({
74
- signalType: "repeated-procedure",
75
- summary: repeated[0].slice(0, 500),
76
- refs: repeated[1].refs.slice(0, 4),
77
- confidence: 0.8,
78
- });
79
- const corrections = userInputs.filter((event) => CORRECTION_PATTERN.test(event.text));
80
- if (corrections.length)
81
- candidates.push({
82
- signalType: "repeated-correction",
83
- summary: corrections[0].text.slice(0, 500),
84
- refs: corrections.slice(0, 4).map((event) => `event:${event.id}`),
85
- confidence: 0.7,
86
- });
87
- return candidates;
88
- }
89
- export async function observeSessionWithStore(store, sessionId) {
90
- const session = store.get(sessionId);
91
- if (!session)
92
- throw new Error(`Session not found: ${sessionId}`);
93
- if (session.status !== "completed")
94
- return [];
95
- const events = store.listEvents(sessionId);
96
- const candidates = detectSessionSignals(events);
97
- const items = await load();
98
- const created = [];
99
- for (const candidate of candidates.slice(0, 4)) {
100
- const id = observationId(sessionId, candidate.signalType, candidate.summary);
101
- if (items.some((item) => item.observationId === id))
102
- continue;
103
- created.push({
104
- observationId: id,
105
- sourceSessionId: sessionId,
106
- taskId: session.taskId,
107
- profileId: session.profile,
108
- signalType: candidate.signalType,
109
- summary: candidate.summary,
110
- evidenceRefs: candidate.refs,
111
- confidence: candidate.confidence,
112
- status: "observed",
113
- createdAt: new Date().toISOString(),
114
- reviewedAt: null,
115
- skillCandidateId: null,
116
- });
117
- }
118
- if (created.length)
119
- await save([...items, ...created]);
120
- return created;
121
- }
122
- export async function observeSession(sessionId, openStore) {
123
- const store = await openStore();
124
- try {
125
- return await observeSessionWithStore(store, sessionId);
126
- }
127
- finally {
128
- store.close();
129
- }
130
- }
131
- export async function listObservations() {
132
- return load();
133
- }
134
- /**
135
- * Approving an observation creates the skill candidate it was promising —
136
- * reviewObservation used to only flip a status flag with nothing downstream.
137
- * A duplicate or invalid candidate (see addSkillCandidate) leaves the
138
- * approval standing but skillCandidateId null; the summary is short and
139
- * redacted, so this should be rare.
140
- */
141
- export async function reviewObservation(observationIdValue, status) {
142
- const items = await load();
143
- const item = items.find((candidate) => candidate.observationId === observationIdValue);
144
- if (!item)
145
- throw new Error(`Observation not found: ${observationIdValue}`);
146
- item.status = status;
147
- item.reviewedAt = new Date().toISOString();
148
- if (status === "approved" && !item.skillCandidateId) {
149
- try {
150
- const candidate = await addSkillCandidate({
151
- id: `learned-${item.observationId.slice(4, 16)}`,
152
- name: `Learned: ${item.signalType} (${item.observationId.slice(4, 10)})`,
153
- instructions: item.summary,
154
- sourceSessionId: item.sourceSessionId,
155
- });
156
- item.skillCandidateId = candidate.id;
157
- }
158
- catch {
159
- // Duplicate id/name or invalid content — approval stands, no candidate.
160
- }
161
- }
162
- await save(items);
163
- return item;
164
- }
165
- //# sourceMappingURL=task-observer.js.map