@free-intelligence/core 1.2.0 → 1.12.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/README.md +1 -0
- package/dist/availability/index.d.ts +132 -0
- package/dist/availability/index.js +278 -0
- package/dist/availability/index.js.map +1 -0
- package/dist/index.d.ts +907 -112
- package/dist/index.js +448 -9
- package/dist/index.js.map +1 -1
- package/package.json +31 -2
- package/src/agent/conversation-state-vectors.test.ts +125 -0
- package/src/agent/conversation-state.test.ts +320 -0
- package/src/agent/conversation-state.ts +278 -0
- package/src/agent/events.ts +50 -1
- package/src/agent/hook.ts +26 -2
- package/src/agent/sse-fixture.test.ts +53 -0
- package/src/agent/sse.ts +26 -0
- package/src/agent/state.test.ts +60 -0
- package/src/agent/state.ts +44 -0
- package/src/agent/transcript.test.ts +166 -15
- package/src/agent/transcript.ts +92 -13
- package/src/agent/turn-vectors.test.ts +75 -0
- package/src/agent/wire-map.ts +131 -0
- package/src/agent/wire.generated.ts +265 -0
- package/src/availability/availability.test.ts +130 -0
- package/src/availability/defaults.ts +89 -0
- package/src/availability/index.ts +6 -0
- package/src/availability/time.ts +38 -0
- package/src/availability/types.ts +69 -0
- package/src/availability/validation.ts +133 -0
- package/src/availability/week.ts +50 -0
- package/src/chat/message.ts +81 -2
- package/src/chat/record.generated.ts +156 -0
- package/src/conversation/derivations-vectors.test.ts +65 -0
- package/src/conversation/helpers.test.ts +329 -0
- package/src/conversation/helpers.ts +248 -3
- package/src/conversation/index.ts +17 -0
- package/src/conversation/library.ts +12 -0
- package/src/conversation/markdown.test.ts +84 -0
- package/src/conversation/markdown.ts +67 -0
- package/src/conversation/record.ts +29 -1
- package/src/copy.generated.ts +19 -0
- package/src/index.ts +55 -3
package/dist/index.js
CHANGED
|
@@ -5,7 +5,11 @@ function initialAgentTurnState() {
|
|
|
5
5
|
steps: [],
|
|
6
6
|
text: "",
|
|
7
7
|
sources: [],
|
|
8
|
+
reactions: [],
|
|
8
9
|
meta: null,
|
|
10
|
+
author: null,
|
|
11
|
+
heartbeats: 0,
|
|
12
|
+
deferred: false,
|
|
9
13
|
status: "thinking"
|
|
10
14
|
};
|
|
11
15
|
}
|
|
@@ -102,10 +106,17 @@ function applyAgentEvent(state, event) {
|
|
|
102
106
|
text,
|
|
103
107
|
plan,
|
|
104
108
|
sources: event.sources ?? state.sources,
|
|
109
|
+
reactions: event.reactions ?? state.reactions,
|
|
105
110
|
meta: event.meta ?? state.meta,
|
|
106
111
|
status: "done"
|
|
107
112
|
};
|
|
108
113
|
}
|
|
114
|
+
case "author":
|
|
115
|
+
return { ...state, author: event.author };
|
|
116
|
+
case "ping":
|
|
117
|
+
return { ...state, heartbeats: state.heartbeats + 1 };
|
|
118
|
+
case "deferred":
|
|
119
|
+
return { ...state, deferred: true };
|
|
109
120
|
case "meta":
|
|
110
121
|
return { ...state, meta: event.meta };
|
|
111
122
|
case "error":
|
|
@@ -117,23 +128,337 @@ function applyAgentEvent(state, event) {
|
|
|
117
128
|
}
|
|
118
129
|
}
|
|
119
130
|
|
|
131
|
+
// src/agent/wire-map.ts
|
|
132
|
+
function rec(v) {
|
|
133
|
+
return v && typeof v === "object" ? v : {};
|
|
134
|
+
}
|
|
135
|
+
function str(v, fallback = "") {
|
|
136
|
+
return v == null ? fallback : String(v);
|
|
137
|
+
}
|
|
138
|
+
function nullableStr(v) {
|
|
139
|
+
return typeof v === "string" && v.trim() ? v : null;
|
|
140
|
+
}
|
|
141
|
+
function fromWireEvent(frame) {
|
|
142
|
+
const ev = rec(frame);
|
|
143
|
+
const data = rec(ev.data);
|
|
144
|
+
switch (ev.type) {
|
|
145
|
+
case "open":
|
|
146
|
+
return { type: "open" };
|
|
147
|
+
case "text":
|
|
148
|
+
return { type: "text", delta: str(ev.text) };
|
|
149
|
+
case "element": {
|
|
150
|
+
const el = rec(ev.element);
|
|
151
|
+
const id = str(el.id);
|
|
152
|
+
if (!id) return null;
|
|
153
|
+
return {
|
|
154
|
+
type: "author",
|
|
155
|
+
author: {
|
|
156
|
+
id,
|
|
157
|
+
name: str(el.name) || str(el.label) || id,
|
|
158
|
+
symbol: el.symbol ?? null,
|
|
159
|
+
engine: el.engine ?? null
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
case "tool_call": {
|
|
164
|
+
const t = rec(ev.tool ?? ev.call);
|
|
165
|
+
return {
|
|
166
|
+
type: "tool_call",
|
|
167
|
+
call: {
|
|
168
|
+
id: t.id ?? null,
|
|
169
|
+
name: str(t.name),
|
|
170
|
+
server: t.server ?? null,
|
|
171
|
+
isError: t.is_error ?? t.isError ?? null
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
case "plan":
|
|
176
|
+
return { type: "plan", steps: (data.steps ?? []).map((s) => str(s)) };
|
|
177
|
+
case "plan_rejected":
|
|
178
|
+
return {
|
|
179
|
+
type: "plan_rejected",
|
|
180
|
+
rejection: {
|
|
181
|
+
reason: str(data.reason),
|
|
182
|
+
matched: data.matched ?? [],
|
|
183
|
+
guard: data.guard ?? null
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
case "step_started":
|
|
187
|
+
return { type: "step_started", index: Number(data.step_index ?? -1) };
|
|
188
|
+
case "step_done": {
|
|
189
|
+
const raw = str(data.status, "done");
|
|
190
|
+
const status = raw === "failed" ? "failed" : raw === "cancelled" ? "cancelled" : "done";
|
|
191
|
+
return {
|
|
192
|
+
type: "step_done",
|
|
193
|
+
index: Number(data.step_index ?? -1),
|
|
194
|
+
status,
|
|
195
|
+
summary: data.summary ? str(data.summary) : void 0,
|
|
196
|
+
error: data.error ? str(data.error) : void 0
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
case "step_noted":
|
|
200
|
+
return { type: "step_noted", index: Number(data.step_index ?? -1), note: str(data.note) };
|
|
201
|
+
case "plan_amended":
|
|
202
|
+
return { type: "plan_amended", action: data.action === "replan" ? "replan" : "insert" };
|
|
203
|
+
case "plan_cancelled":
|
|
204
|
+
return { type: "plan_cancelled", reason: data.reason ? str(data.reason) : void 0 };
|
|
205
|
+
case "plan_completed":
|
|
206
|
+
return {
|
|
207
|
+
type: "plan_completed",
|
|
208
|
+
completedCount: Number(data.completed_count ?? 0),
|
|
209
|
+
failedCount: Number(data.failed_count ?? 0),
|
|
210
|
+
cancelledCount: Number(data.cancelled_count ?? 0)
|
|
211
|
+
};
|
|
212
|
+
case "plan_failed":
|
|
213
|
+
return {
|
|
214
|
+
type: "plan_failed",
|
|
215
|
+
completedCount: Number(data.completed_count ?? 0),
|
|
216
|
+
failedCount: Number(data.failed_count ?? 0),
|
|
217
|
+
cancelledCount: Number(data.cancelled_count ?? 0)
|
|
218
|
+
};
|
|
219
|
+
case "result": {
|
|
220
|
+
const r = rec(ev.result);
|
|
221
|
+
const model = nullableStr(r.model);
|
|
222
|
+
const reactions = Array.isArray(r.reactions) ? r.reactions.map((x) => str(x).trim()).filter(Boolean) : [];
|
|
223
|
+
return {
|
|
224
|
+
type: "result",
|
|
225
|
+
text: str(r.text),
|
|
226
|
+
...model ? { meta: { model } } : {},
|
|
227
|
+
...reactions.length > 0 ? { reactions } : {}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
case "error":
|
|
231
|
+
return { type: "error", message: str(ev.message, "error") };
|
|
232
|
+
case "ping":
|
|
233
|
+
return { type: "ping" };
|
|
234
|
+
case "deferred":
|
|
235
|
+
return { type: "deferred" };
|
|
236
|
+
case "done":
|
|
237
|
+
return { type: "done" };
|
|
238
|
+
default:
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// src/agent/sse.ts
|
|
244
|
+
function takeSseFrames(buffer) {
|
|
245
|
+
const frames = buffer.split("\n\n");
|
|
246
|
+
const rest = frames.pop() ?? "";
|
|
247
|
+
const payloads = [];
|
|
248
|
+
for (const frame of frames) {
|
|
249
|
+
const data = frame.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trim());
|
|
250
|
+
if (data.length > 0) payloads.push(data.join("\n"));
|
|
251
|
+
}
|
|
252
|
+
return { payloads, rest };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/copy.generated.ts
|
|
256
|
+
var COPY = {
|
|
257
|
+
/** Título de un hilo sin mensaje de usuario usable (deriveConversationTitle). */
|
|
258
|
+
conversationDefaultTitle: "New chat",
|
|
259
|
+
/** Aviso (no error) de un turno diferido: el motor sigue trabajando tras cerrar el stream. */
|
|
260
|
+
turnDeferredNotice: (author) => "{author} sigue escribiendo. Su respuesta aparecer\xE1 aqu\xED en cuanto termine.".split("{author}").join(author),
|
|
261
|
+
/** El aviso cuando la relectura acotada del hilo se agotó sin que llegara la respuesta diferida. */
|
|
262
|
+
turnDeferredTimeout: "La respuesta tard\xF3 m\xE1s de lo normal. Vuelve a abrir este chat para verla.",
|
|
263
|
+
/** Falla genérica del transporte o del motor, cuando no trae mensaje propio. */
|
|
264
|
+
turnFailed: "La respuesta fall\xF3. Intenta de nuevo.",
|
|
265
|
+
/** El watchdog de inactividad cerró el turno. */
|
|
266
|
+
turnTimeout: "La respuesta tard\xF3 demasiado. Intenta de nuevo.",
|
|
267
|
+
/** El turno cerró sin texto, sin reacción y sin deferred: el silencio es una falla. */
|
|
268
|
+
turnEmpty: "La respuesta lleg\xF3 vac\xEDa. Intenta de nuevo."
|
|
269
|
+
};
|
|
270
|
+
|
|
120
271
|
// src/agent/transcript.ts
|
|
121
|
-
function makeUserMessage(text) {
|
|
122
|
-
return {
|
|
272
|
+
function makeUserMessage(text, author, images) {
|
|
273
|
+
return {
|
|
274
|
+
role: "user",
|
|
275
|
+
author,
|
|
276
|
+
content: text,
|
|
277
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
278
|
+
...images && images.length > 0 ? { images } : {}
|
|
279
|
+
};
|
|
123
280
|
}
|
|
124
|
-
function
|
|
125
|
-
const
|
|
281
|
+
function snapshotTrace(turn) {
|
|
282
|
+
const hasPlan = turn.plan != null && turn.plan.steps.length > 0;
|
|
283
|
+
const hasTools = turn.steps.length > 0;
|
|
284
|
+
const hasSources = turn.sources.length > 0;
|
|
285
|
+
const model = turn.meta?.model?.trim() || void 0;
|
|
286
|
+
if (!hasPlan && !hasTools && !hasSources && !model) return void 0;
|
|
287
|
+
return {
|
|
288
|
+
...hasPlan ? { plan: turn.plan } : {},
|
|
289
|
+
...hasTools ? { tools: turn.steps } : {},
|
|
290
|
+
...hasSources ? { sources: turn.sources } : {},
|
|
291
|
+
...model ? { model } : {}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function foldAssistantTurn(turn, defaultAuthor) {
|
|
295
|
+
const trace = snapshotTrace(turn);
|
|
126
296
|
return {
|
|
127
297
|
role: "assistant",
|
|
298
|
+
author: turn.author ?? defaultAuthor,
|
|
128
299
|
content: turn.text,
|
|
129
300
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
130
|
-
...
|
|
301
|
+
...trace ? { trace } : {},
|
|
302
|
+
// The speaker's reactions are a gesture on the message, kept as a field so
|
|
303
|
+
// the shell renders chips and a reload keeps them; absent when there were none.
|
|
304
|
+
...turn.reactions.length > 0 ? { reactions: turn.reactions } : {}
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function messageKey(message) {
|
|
308
|
+
return `${message.role}\0${message.timestamp ?? ""}\0${message.content}`;
|
|
309
|
+
}
|
|
310
|
+
function mergeConversationMessages(target, source) {
|
|
311
|
+
const known = new Set(target.map(messageKey));
|
|
312
|
+
const missing = source.filter((m) => !known.has(messageKey(m)));
|
|
313
|
+
if (missing.length === 0) return target;
|
|
314
|
+
return [...target, ...missing].map((message, order) => ({ message, order })).sort((a, b) => (a.message.timestamp ?? "").localeCompare(b.message.timestamp ?? "") || a.order - b.order).map(({ message }) => message);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// src/agent/conversation-state.ts
|
|
318
|
+
function deferredNotice(author) {
|
|
319
|
+
return {
|
|
320
|
+
kind: "deferred",
|
|
321
|
+
message: COPY.turnDeferredNotice(author.name)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function initialConversationState(seed = []) {
|
|
325
|
+
return {
|
|
326
|
+
messages: seed,
|
|
327
|
+
pending: false,
|
|
328
|
+
failure: null,
|
|
329
|
+
notice: null,
|
|
330
|
+
timedOut: false,
|
|
331
|
+
unsent: { text: null, images: null },
|
|
332
|
+
lastSent: null,
|
|
333
|
+
// The mount seed is not a confirmed turn.
|
|
334
|
+
skipPersist: true
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
var NO_DRAFT = { text: null, images: null };
|
|
338
|
+
function revertOptimistic(state, controlled) {
|
|
339
|
+
if (controlled) return state;
|
|
340
|
+
const last = state.messages[state.messages.length - 1];
|
|
341
|
+
if (last?.role !== "user") return state;
|
|
342
|
+
return {
|
|
343
|
+
...state,
|
|
344
|
+
messages: state.messages.slice(0, -1),
|
|
345
|
+
unsent: {
|
|
346
|
+
text: last.content,
|
|
347
|
+
images: last.images && last.images.length > 0 ? last.images : null
|
|
348
|
+
}
|
|
131
349
|
};
|
|
132
350
|
}
|
|
351
|
+
function applyConversationEvent(state, event) {
|
|
352
|
+
switch (event.type) {
|
|
353
|
+
case "send": {
|
|
354
|
+
const text = event.text.trim();
|
|
355
|
+
const images = event.images && event.images.length > 0 ? event.images : void 0;
|
|
356
|
+
if (!text && !images) return state;
|
|
357
|
+
const next = {
|
|
358
|
+
...state,
|
|
359
|
+
pending: true,
|
|
360
|
+
failure: null,
|
|
361
|
+
notice: null,
|
|
362
|
+
timedOut: false,
|
|
363
|
+
// A new send supersedes any draft still waiting to be recovered.
|
|
364
|
+
unsent: NO_DRAFT,
|
|
365
|
+
lastSent: { text, ...images ? { images } : {} },
|
|
366
|
+
// The optimistic push is not a confirmed turn.
|
|
367
|
+
skipPersist: true
|
|
368
|
+
};
|
|
369
|
+
if (event.controlled) return next;
|
|
370
|
+
return {
|
|
371
|
+
...next,
|
|
372
|
+
messages: [...state.messages, makeUserMessage(text, event.author, images)]
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
case "turn_settled": {
|
|
376
|
+
if (!state.pending) return state;
|
|
377
|
+
const base = { ...state, pending: false };
|
|
378
|
+
if (event.controlled || !event.turn.text && event.turn.reactions.length === 0) {
|
|
379
|
+
if (event.turn.deferred) {
|
|
380
|
+
return { ...base, notice: deferredNotice(event.turn.author ?? event.author) };
|
|
381
|
+
}
|
|
382
|
+
if (event.controlled) return base;
|
|
383
|
+
if (event.stopped) return { ...base, messages: [...state.messages], skipPersist: false };
|
|
384
|
+
return {
|
|
385
|
+
...revertOptimistic(base, false),
|
|
386
|
+
failure: { kind: "empty", message: COPY.turnEmpty }
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
...base,
|
|
391
|
+
messages: [...state.messages, foldAssistantTurn(event.turn, event.author)],
|
|
392
|
+
// A settled turn IS confirmed — this one gets persisted.
|
|
393
|
+
skipPersist: false
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
case "turn_failed": {
|
|
397
|
+
if (!state.pending) return state;
|
|
398
|
+
const reverted = revertOptimistic({ ...state, pending: false }, event.controlled);
|
|
399
|
+
if (event.appHandled) return reverted;
|
|
400
|
+
return {
|
|
401
|
+
...reverted,
|
|
402
|
+
failure: {
|
|
403
|
+
kind: "stream",
|
|
404
|
+
message: event.message || COPY.turnFailed
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
case "turn_timeout": {
|
|
409
|
+
if (!state.pending || state.timedOut) return state;
|
|
410
|
+
return {
|
|
411
|
+
...revertOptimistic({ ...state, pending: false }, event.controlled),
|
|
412
|
+
timedOut: true,
|
|
413
|
+
failure: { kind: "timeout", message: COPY.turnTimeout }
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
case "dismiss_failure":
|
|
417
|
+
return state.failure === null ? state : { ...state, failure: null, timedOut: false };
|
|
418
|
+
case "dismiss_notice":
|
|
419
|
+
return state.notice === null ? state : { ...state, notice: null };
|
|
420
|
+
case "clear_unsent":
|
|
421
|
+
return state.unsent.text === null && state.unsent.images === null ? state : { ...state, unsent: NO_DRAFT };
|
|
422
|
+
case "hydrate":
|
|
423
|
+
return { ...initialConversationState(event.messages), lastSent: state.lastSent };
|
|
424
|
+
case "merge_seed": {
|
|
425
|
+
const messages = mergeConversationMessages(state.messages, event.messages);
|
|
426
|
+
return messages === state.messages ? state : { ...state, messages, skipPersist: true };
|
|
427
|
+
}
|
|
428
|
+
case "persist_skip_consumed":
|
|
429
|
+
return state.skipPersist ? { ...state, skipPersist: false } : state;
|
|
430
|
+
default:
|
|
431
|
+
return state;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// src/conversation/markdown.ts
|
|
436
|
+
function etiqueta(m, o) {
|
|
437
|
+
if (m.role === "user") return o.labels?.user ?? "T\xFA";
|
|
438
|
+
if (m.role === "assistant") return o.labels?.assistant ?? "Asistente";
|
|
439
|
+
return m.role;
|
|
440
|
+
}
|
|
441
|
+
function conversationToMarkdown(record, options = {}) {
|
|
442
|
+
const partes = [`# ${record.title || "Conversaci\xF3n"}`, ""];
|
|
443
|
+
const fecha = record.createdAt || record.updatedAt;
|
|
444
|
+
if (fecha) partes.push(`*${new Date(fecha).toLocaleString()}*`, "");
|
|
445
|
+
if (options.source) partes.push(`*${options.source}*`, "");
|
|
446
|
+
for (const m of record.messages) {
|
|
447
|
+
const contenido = (m.content ?? "").trim();
|
|
448
|
+
if (!contenido) continue;
|
|
449
|
+
partes.push(`## ${etiqueta(m, options)}`, "", contenido, "");
|
|
450
|
+
}
|
|
451
|
+
return `${partes.join("\n").trimEnd()}
|
|
452
|
+
`;
|
|
453
|
+
}
|
|
454
|
+
function conversationFileName(record) {
|
|
455
|
+
const base = (record.title || "conversacion").normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/[^\w\s-]/g, "").trim().replace(/\s+/g, "-").slice(0, 60).replace(/-+$/, "");
|
|
456
|
+
return `${base || "conversacion"}.md`;
|
|
457
|
+
}
|
|
133
458
|
|
|
134
459
|
// src/conversation/helpers.ts
|
|
135
460
|
var CONVERSATION_SCHEMA_VERSION = 1;
|
|
136
|
-
var DEFAULT_TITLE =
|
|
461
|
+
var DEFAULT_TITLE = COPY.conversationDefaultTitle;
|
|
137
462
|
var TITLE_MAX = 60;
|
|
138
463
|
var PREVIEW_MAX = 120;
|
|
139
464
|
function truncate(text, max) {
|
|
@@ -145,7 +470,17 @@ function sanitizeConversationMessage(message) {
|
|
|
145
470
|
return {
|
|
146
471
|
role: message.role,
|
|
147
472
|
content: message.content,
|
|
148
|
-
timestamp: message.timestamp
|
|
473
|
+
timestamp: message.timestamp,
|
|
474
|
+
...message.author ? { author: message.author } : {},
|
|
475
|
+
...message.trace ? { trace: message.trace } : {},
|
|
476
|
+
// Attached images are user-visible message CONTENT (OG118-IMAGE-UPLOAD-1),
|
|
477
|
+
// not metadata — dropping them would blank the picture on reload the way
|
|
478
|
+
// dropping `author` used to anonymize bubbles. Producers downscale before
|
|
479
|
+
// encoding, so the persisted base64 stays within the record size caps.
|
|
480
|
+
...message.images && message.images.length > 0 ? { images: message.images.map((i) => ({ mediaType: i.mediaType, data: i.data })) } : {},
|
|
481
|
+
// The speaker's reactions are user-visible too (chips under the bubble);
|
|
482
|
+
// dropping them would strip the gesture on reload. Plain strings, no payload.
|
|
483
|
+
...message.reactions && message.reactions.length > 0 ? { reactions: message.reactions.map(String) } : {}
|
|
149
484
|
};
|
|
150
485
|
}
|
|
151
486
|
function deriveConversationTitle(messages, max = TITLE_MAX) {
|
|
@@ -176,25 +511,129 @@ function createConversationRecord(args) {
|
|
|
176
511
|
schemaVersion: CONVERSATION_SCHEMA_VERSION
|
|
177
512
|
};
|
|
178
513
|
}
|
|
514
|
+
function resolveConversationTitle(messages, prev) {
|
|
515
|
+
if (prev?.titleCustom && prev.title.trim() !== "") return prev.title;
|
|
516
|
+
return deriveConversationTitle(messages);
|
|
517
|
+
}
|
|
518
|
+
function applyConversationMetadataPatch(record, patch) {
|
|
519
|
+
const next = { ...record };
|
|
520
|
+
if (patch.title !== void 0) next.title = patch.title;
|
|
521
|
+
if (patch.titleCustom !== void 0) next.titleCustom = patch.titleCustom;
|
|
522
|
+
if (patch.updatedAt !== void 0) next.updatedAt = patch.updatedAt;
|
|
523
|
+
if (patch.pinnedAt !== void 0) {
|
|
524
|
+
if (patch.pinnedAt === null) delete next.pinnedAt;
|
|
525
|
+
else next.pinnedAt = patch.pinnedAt;
|
|
526
|
+
}
|
|
527
|
+
if (patch.archivedAt !== void 0) {
|
|
528
|
+
if (patch.archivedAt === null) delete next.archivedAt;
|
|
529
|
+
else next.archivedAt = patch.archivedAt;
|
|
530
|
+
}
|
|
531
|
+
return next;
|
|
532
|
+
}
|
|
533
|
+
function conversationRenamePatch(record, rawTitle, now) {
|
|
534
|
+
const trimmed = rawTitle.trim().replace(/\s+/g, " ");
|
|
535
|
+
const ts = now ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
536
|
+
if (trimmed === "") {
|
|
537
|
+
return {
|
|
538
|
+
title: deriveConversationTitle(record.messages),
|
|
539
|
+
titleCustom: false,
|
|
540
|
+
updatedAt: ts
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
return { title: trimmed.slice(0, TITLE_MAX), titleCustom: true, updatedAt: ts };
|
|
544
|
+
}
|
|
545
|
+
function conversationPinPatch(pinned, now) {
|
|
546
|
+
if (pinned) {
|
|
547
|
+
return { pinnedAt: now ?? (/* @__PURE__ */ new Date()).toISOString(), archivedAt: null };
|
|
548
|
+
}
|
|
549
|
+
return { pinnedAt: null };
|
|
550
|
+
}
|
|
551
|
+
function conversationArchivePatch(archived, now) {
|
|
552
|
+
if (archived) {
|
|
553
|
+
return { archivedAt: now ?? (/* @__PURE__ */ new Date()).toISOString(), pinnedAt: null };
|
|
554
|
+
}
|
|
555
|
+
return { archivedAt: null };
|
|
556
|
+
}
|
|
557
|
+
function renameConversationRecord(record, rawTitle, now) {
|
|
558
|
+
return applyConversationMetadataPatch(
|
|
559
|
+
record,
|
|
560
|
+
conversationRenamePatch(record, rawTitle, now)
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
function setConversationPinned(record, pinned, now) {
|
|
564
|
+
return applyConversationMetadataPatch(record, conversationPinPatch(pinned, now));
|
|
565
|
+
}
|
|
566
|
+
function setConversationArchived(record, archived, now) {
|
|
567
|
+
return applyConversationMetadataPatch(
|
|
568
|
+
record,
|
|
569
|
+
conversationArchivePatch(archived, now)
|
|
570
|
+
);
|
|
571
|
+
}
|
|
179
572
|
function summarizeConversation(record) {
|
|
180
573
|
return {
|
|
181
574
|
id: record.id,
|
|
182
575
|
title: record.title,
|
|
183
576
|
createdAt: record.createdAt,
|
|
184
577
|
updatedAt: record.updatedAt,
|
|
185
|
-
preview: record.preview
|
|
578
|
+
preview: record.preview,
|
|
579
|
+
...record.pinnedAt ? { pinnedAt: record.pinnedAt } : {},
|
|
580
|
+
...record.archivedAt ? { archivedAt: record.archivedAt } : {},
|
|
581
|
+
...record.projectId ? { projectId: record.projectId } : {}
|
|
186
582
|
};
|
|
187
583
|
}
|
|
584
|
+
function normalizeForSearch(text) {
|
|
585
|
+
return text.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
586
|
+
}
|
|
587
|
+
function filterConversationSummaries(summaries, query) {
|
|
588
|
+
const terms = normalizeForSearch(query).split(/\s+/).filter(Boolean);
|
|
589
|
+
if (terms.length === 0) return summaries;
|
|
590
|
+
return summaries.filter((s) => {
|
|
591
|
+
const haystack = normalizeForSearch(`${s.title} ${s.preview}`);
|
|
592
|
+
return terms.every((t) => haystack.includes(t));
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
function organizeConversationSummaries(summaries) {
|
|
596
|
+
const pinned = [];
|
|
597
|
+
const active = [];
|
|
598
|
+
const archived = [];
|
|
599
|
+
for (const s of summaries) {
|
|
600
|
+
if (s.archivedAt) archived.push(s);
|
|
601
|
+
else if (s.pinnedAt) pinned.push(s);
|
|
602
|
+
else active.push(s);
|
|
603
|
+
}
|
|
604
|
+
pinned.sort((a, b) => (b.pinnedAt ?? "").localeCompare(a.pinnedAt ?? ""));
|
|
605
|
+
active.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
606
|
+
archived.sort((a, b) => (b.archivedAt ?? "").localeCompare(a.archivedAt ?? ""));
|
|
607
|
+
return { pinned, active, archived };
|
|
608
|
+
}
|
|
188
609
|
export {
|
|
189
610
|
CONVERSATION_SCHEMA_VERSION,
|
|
611
|
+
COPY,
|
|
190
612
|
applyAgentEvent,
|
|
613
|
+
applyConversationEvent,
|
|
614
|
+
applyConversationMetadataPatch,
|
|
615
|
+
conversationArchivePatch,
|
|
616
|
+
conversationFileName,
|
|
617
|
+
conversationPinPatch,
|
|
618
|
+
conversationRenamePatch,
|
|
619
|
+
conversationToMarkdown,
|
|
191
620
|
createConversationRecord,
|
|
192
621
|
deriveConversationPreview,
|
|
193
622
|
deriveConversationTitle,
|
|
623
|
+
filterConversationSummaries,
|
|
194
624
|
foldAssistantTurn,
|
|
625
|
+
fromWireEvent,
|
|
195
626
|
initialAgentTurnState,
|
|
627
|
+
initialConversationState,
|
|
196
628
|
makeUserMessage,
|
|
629
|
+
mergeConversationMessages,
|
|
630
|
+
organizeConversationSummaries,
|
|
631
|
+
renameConversationRecord,
|
|
632
|
+
resolveConversationTitle,
|
|
197
633
|
sanitizeConversationMessage,
|
|
198
|
-
|
|
634
|
+
setConversationArchived,
|
|
635
|
+
setConversationPinned,
|
|
636
|
+
summarizeConversation,
|
|
637
|
+
takeSseFrames
|
|
199
638
|
};
|
|
200
639
|
//# sourceMappingURL=index.js.map
|