@peer-agent/runtime-core 0.1.0-rc.44 → 0.1.0-rc.46
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,9 +1,11 @@
|
|
|
1
|
-
import { type InputDraftValue, type LocalInputSurface, type ProjectAgentSubmitInputResult, type ProjectInputDraftSnapshot, type ProjectInputRecoveryPort, type UnconfirmedProjectInput } from '@peer-agent/protocol';
|
|
1
|
+
import { type InputDraftValue, type LocalInputSurface, type ProjectAgentSubmitInputResult, type ProjectInputDraftSnapshot, type ProjectInputMutationResult, type ProjectInputRecoveryPort, type UnconfirmedProjectInput } from '@peer-agent/protocol';
|
|
2
2
|
export interface InputRecoveryView extends ProjectInputDraftSnapshot {
|
|
3
3
|
readonly ready: boolean;
|
|
4
4
|
readonly error: string | null;
|
|
5
5
|
readonly sending: readonly string[];
|
|
6
6
|
readonly received: readonly UnconfirmedProjectInput[];
|
|
7
|
+
readonly rejected: Readonly<Record<string, string>>;
|
|
8
|
+
readonly mutating: readonly string[];
|
|
7
9
|
}
|
|
8
10
|
/** Presentation continuity only; ports own storage and the existing governed input queue. */
|
|
9
11
|
export declare function createProjectInputRecovery({ workspaceId, surface, port, createId, now, receiptTimeoutMs }: {
|
|
@@ -27,7 +29,14 @@ export declare function createProjectInputRecovery({ workspaceId, surface, port,
|
|
|
27
29
|
answerTo?: string;
|
|
28
30
|
useDraft?: boolean;
|
|
29
31
|
}) => Promise<ProjectAgentSubmitInputResult>;
|
|
30
|
-
|
|
32
|
+
discard: (inputId: string) => Promise<ProjectInputMutationResult>;
|
|
33
|
+
restoreToDraft: (inputId: string, options?: {
|
|
34
|
+
readonly replaceDraft?: boolean;
|
|
35
|
+
}) => Promise<ProjectInputMutationResult>;
|
|
36
|
+
observeReceived(ids: readonly string[], answered?: readonly {
|
|
37
|
+
readonly answerTo: string;
|
|
38
|
+
readonly text: string;
|
|
39
|
+
}[]): void;
|
|
31
40
|
retry(inputId: string): Promise<ProjectAgentSubmitInputResult> | Promise<{
|
|
32
41
|
ok: boolean;
|
|
33
42
|
code: string;
|
|
@@ -1,14 +1,144 @@
|
|
|
1
|
-
import { encodeResourceFeedbackTransport, normalizeResourceFeedbackList, emptyInputDraft, projectInputWasReceived } from '@peer-agent/protocol';
|
|
1
|
+
import { encodeResourceFeedbackTransport, normalizeResourceFeedbackList, emptyInputDraft, isDefiniteProjectInputRejection, projectInputWasReceived } from '@peer-agent/protocol';
|
|
2
|
+
const EMPTY_REJECTED = Object.freeze({});
|
|
3
|
+
const EMPTY_IDS = Object.freeze([]);
|
|
4
|
+
const RESOURCE_REJECTION_CODES = new Set([
|
|
5
|
+
'INVALID_RESOURCE_FEEDBACK',
|
|
6
|
+
'RESOURCE_FEEDBACK_RECIPIENT_CHANGED',
|
|
7
|
+
'RESOURCE_FEEDBACK_ATTACHMENT_LIMIT',
|
|
8
|
+
'INTERACTIVE_FEEDBACK_UNAVAILABLE',
|
|
9
|
+
]);
|
|
10
|
+
const DRAFT_UPLOAD_CHUNK_BYTES = 16 * 1024 * 1024;
|
|
11
|
+
function estimateUploadBytes(item) {
|
|
12
|
+
const body = item.kind === 'image' ? item.dataUrl.length : item.kind === 'text' ? item.text.length : 0;
|
|
13
|
+
return body + 512;
|
|
14
|
+
}
|
|
15
|
+
function partitionUploadChunks(uploads) {
|
|
16
|
+
if (uploads.length <= 1)
|
|
17
|
+
return [uploads];
|
|
18
|
+
const chunks = [];
|
|
19
|
+
let current = [];
|
|
20
|
+
let currentBytes = 0;
|
|
21
|
+
for (const item of uploads) {
|
|
22
|
+
const bytes = estimateUploadBytes(item);
|
|
23
|
+
if (current.length > 0 && currentBytes + bytes > DRAFT_UPLOAD_CHUNK_BYTES) {
|
|
24
|
+
chunks.push(current);
|
|
25
|
+
current = [item];
|
|
26
|
+
currentBytes = bytes;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
current.push(item);
|
|
30
|
+
currentBytes += bytes;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (current.length > 0)
|
|
34
|
+
chunks.push(current);
|
|
35
|
+
return chunks;
|
|
36
|
+
}
|
|
2
37
|
/** Presentation continuity only; ports own storage and the existing governed input queue. */
|
|
3
38
|
export function createProjectInputRecovery({ workspaceId, surface, port, createId, now = () => new Date().toISOString(), receiptTimeoutMs = 30_000 }) {
|
|
4
39
|
const address = { workspaceId, surface };
|
|
5
|
-
let view = { revision: 0, draft: emptyInputDraft(), pending: [], ready: false, error: null, sending:
|
|
40
|
+
let view = { revision: 0, draft: emptyInputDraft(), pending: [], ready: false, error: null, sending: EMPTY_IDS, received: [], rejected: EMPTY_REJECTED, mutating: EMPTY_IDS };
|
|
6
41
|
let changed = 0, saved = 0;
|
|
7
42
|
let earlyEdits = null;
|
|
8
43
|
let writing = null, loading = null;
|
|
9
44
|
const listeners = new Set(), storedAttachments = new Map(), active = new Map();
|
|
45
|
+
const draftOrigins = new Map();
|
|
46
|
+
const optionOrigins = new Set();
|
|
47
|
+
const mutatingIds = new Set();
|
|
10
48
|
const publish = (patch) => { view = { ...view, ...patch }; for (const fn of listeners)
|
|
11
49
|
fn(); };
|
|
50
|
+
const isDraftEmpty = (draft) => !draft.text && !draft.quote && !draft.attachments.length
|
|
51
|
+
&& !draft.taskRefs?.length && !draft.resourceFeedback?.length;
|
|
52
|
+
const hasComposedContent = (input) => Boolean(input.attachments.length || input.quoteRefs.length || input.taskRefs?.length || input.resourceFeedback?.length);
|
|
53
|
+
const omitRejected = (ids) => {
|
|
54
|
+
if (!ids.some(id => Object.prototype.hasOwnProperty.call(view.rejected, id)))
|
|
55
|
+
return view.rejected;
|
|
56
|
+
const next = {};
|
|
57
|
+
for (const [key, value] of Object.entries(view.rejected)) {
|
|
58
|
+
if (!ids.includes(key))
|
|
59
|
+
next[key] = value;
|
|
60
|
+
}
|
|
61
|
+
return Object.keys(next).length ? next : EMPTY_REJECTED;
|
|
62
|
+
};
|
|
63
|
+
const markRejected = (inputId, code) => {
|
|
64
|
+
if (view.rejected[inputId] === code)
|
|
65
|
+
return;
|
|
66
|
+
publish({ rejected: { ...view.rejected, [inputId]: code } });
|
|
67
|
+
};
|
|
68
|
+
const clearRejected = (ids) => {
|
|
69
|
+
const rejected = omitRejected(ids);
|
|
70
|
+
if (rejected !== view.rejected)
|
|
71
|
+
publish({ rejected });
|
|
72
|
+
};
|
|
73
|
+
const quoteForRestore = (input) => {
|
|
74
|
+
const savedQuote = draftOrigins.get(input.inputId);
|
|
75
|
+
if (savedQuote !== undefined)
|
|
76
|
+
return savedQuote;
|
|
77
|
+
if (input.quoteRefs.length >= 2 && input.quoteRefs[0] && input.quoteRefs[1]) {
|
|
78
|
+
return { messageId: input.quoteRefs[0], text: input.quoteRefs[1] };
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
};
|
|
82
|
+
const buildRestoredDraft = (input, rejectionCode) => {
|
|
83
|
+
const keepResourceFeedback = Boolean(input.resourceFeedback?.length && (!rejectionCode || !RESOURCE_REJECTION_CODES.has(rejectionCode)));
|
|
84
|
+
return {
|
|
85
|
+
text: input.text,
|
|
86
|
+
quote: quoteForRestore(input),
|
|
87
|
+
attachments: input.attachments,
|
|
88
|
+
...(input.taskRefs?.length ? { taskRefs: input.taskRefs } : {}),
|
|
89
|
+
...(keepResourceFeedback ? { resourceFeedback: input.resourceFeedback } : {}),
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
async function writeState(draftValue, pendingList) {
|
|
93
|
+
const all = [...draftValue.attachments, ...pendingList.flatMap(item => item.attachments)];
|
|
94
|
+
const uploads = [...new Map(all.filter(item => storedAttachments.get(item.id) !== item).map(item => [item.id, item])).values()];
|
|
95
|
+
const { attachments, ...draft } = draftValue;
|
|
96
|
+
const chunks = partitionUploadChunks(uploads);
|
|
97
|
+
let revision = view.revision;
|
|
98
|
+
const available = new Set(storedAttachments.keys());
|
|
99
|
+
try {
|
|
100
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
101
|
+
const chunk = chunks[i];
|
|
102
|
+
for (const item of chunk)
|
|
103
|
+
available.add(item.id);
|
|
104
|
+
const isFinal = i === chunks.length - 1;
|
|
105
|
+
const result = await port.write({
|
|
106
|
+
...address,
|
|
107
|
+
revision,
|
|
108
|
+
draft: {
|
|
109
|
+
...draft,
|
|
110
|
+
attachmentIds: isFinal
|
|
111
|
+
? attachments.map(item => item.id)
|
|
112
|
+
: attachments.filter(item => available.has(item.id)).map(item => item.id),
|
|
113
|
+
},
|
|
114
|
+
pending: pendingList.map(({ attachments: pendingAttachments, ...input }) => ({
|
|
115
|
+
...input,
|
|
116
|
+
attachmentIds: isFinal
|
|
117
|
+
? pendingAttachments.map(item => item.id)
|
|
118
|
+
: pendingAttachments.filter(item => available.has(item.id)).map(item => item.id),
|
|
119
|
+
})),
|
|
120
|
+
uploads: chunk,
|
|
121
|
+
});
|
|
122
|
+
if (!result.ok) {
|
|
123
|
+
publish({ error: result.code });
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
revision = result.revision;
|
|
127
|
+
for (const item of chunk)
|
|
128
|
+
storedAttachments.set(item.id, item);
|
|
129
|
+
if (!isFinal)
|
|
130
|
+
publish({ revision, error: null });
|
|
131
|
+
}
|
|
132
|
+
storedAttachments.clear();
|
|
133
|
+
for (const item of all)
|
|
134
|
+
storedAttachments.set(item.id, item);
|
|
135
|
+
return revision;
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
publish({ error: 'DRAFT_SAVE_FAILED' });
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
12
142
|
async function load() {
|
|
13
143
|
if (loading)
|
|
14
144
|
return loading;
|
|
@@ -42,32 +172,23 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
42
172
|
async function flush() {
|
|
43
173
|
if (!view.ready)
|
|
44
174
|
return false;
|
|
45
|
-
|
|
46
|
-
|
|
175
|
+
while (writing) {
|
|
176
|
+
const ok = await writing;
|
|
177
|
+
if (!ok)
|
|
178
|
+
return false;
|
|
179
|
+
if (saved === changed)
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
if (saved === changed)
|
|
183
|
+
return true;
|
|
47
184
|
writing = (async () => {
|
|
48
185
|
while (saved !== changed) {
|
|
49
186
|
const version = changed, state = view;
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
const { attachments, ...draft } = state.draft;
|
|
53
|
-
try {
|
|
54
|
-
const result = await port.write({ ...address, revision: state.revision,
|
|
55
|
-
draft: { ...draft, attachmentIds: attachments.map(item => item.id) },
|
|
56
|
-
pending: state.pending.map(({ attachments, ...input }) => ({ ...input, attachmentIds: attachments.map(item => item.id) })), uploads });
|
|
57
|
-
if (!result.ok) {
|
|
58
|
-
publish({ error: result.code });
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
saved = version;
|
|
62
|
-
storedAttachments.clear();
|
|
63
|
-
for (const item of all)
|
|
64
|
-
storedAttachments.set(item.id, item);
|
|
65
|
-
publish({ revision: result.revision, error: null });
|
|
66
|
-
}
|
|
67
|
-
catch {
|
|
68
|
-
publish({ error: 'DRAFT_SAVE_FAILED' });
|
|
187
|
+
const revision = await writeState(state.draft, state.pending);
|
|
188
|
+
if (revision === null)
|
|
69
189
|
return false;
|
|
70
|
-
|
|
190
|
+
saved = version;
|
|
191
|
+
publish({ revision, error: null });
|
|
71
192
|
}
|
|
72
193
|
return true;
|
|
73
194
|
})().finally(() => { writing = null; });
|
|
@@ -90,15 +211,132 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
90
211
|
void flush();
|
|
91
212
|
}
|
|
92
213
|
function acknowledge(ids) {
|
|
214
|
+
for (const id of ids) {
|
|
215
|
+
draftOrigins.delete(id);
|
|
216
|
+
optionOrigins.delete(id);
|
|
217
|
+
}
|
|
218
|
+
const rejected = omitRejected(ids);
|
|
93
219
|
const pending = view.pending.filter(item => !ids.includes(item.inputId));
|
|
94
|
-
if (pending.length === view.pending.length)
|
|
220
|
+
if (pending.length === view.pending.length) {
|
|
221
|
+
if (rejected !== view.rejected)
|
|
222
|
+
publish({ rejected });
|
|
95
223
|
return;
|
|
224
|
+
}
|
|
96
225
|
const received = [...view.received, ...view.pending.filter(item => ids.includes(item.inputId))];
|
|
97
226
|
changed++;
|
|
98
|
-
publish({ pending, received });
|
|
227
|
+
publish({ pending, received, ...(rejected !== view.rejected ? { rejected } : {}) });
|
|
228
|
+
void flush();
|
|
229
|
+
}
|
|
230
|
+
function dismiss(ids) {
|
|
231
|
+
for (const id of ids) {
|
|
232
|
+
draftOrigins.delete(id);
|
|
233
|
+
optionOrigins.delete(id);
|
|
234
|
+
}
|
|
235
|
+
const rejected = omitRejected(ids);
|
|
236
|
+
const pending = view.pending.filter(item => !ids.includes(item.inputId));
|
|
237
|
+
if (pending.length === view.pending.length) {
|
|
238
|
+
if (rejected !== view.rejected)
|
|
239
|
+
publish({ rejected });
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
changed++;
|
|
243
|
+
publish({ pending, ...(rejected !== view.rejected ? { rejected } : {}) });
|
|
99
244
|
void flush();
|
|
100
245
|
}
|
|
246
|
+
function recoverClosedDraft(input, originalQuote) {
|
|
247
|
+
if (!view.pending.some(item => item.inputId === input.inputId))
|
|
248
|
+
return;
|
|
249
|
+
const rejected = omitRejected([input.inputId]);
|
|
250
|
+
if (isDraftEmpty(view.draft)) {
|
|
251
|
+
const pending = view.pending.filter(item => item.inputId !== input.inputId);
|
|
252
|
+
const restored = {
|
|
253
|
+
text: input.text,
|
|
254
|
+
quote: originalQuote,
|
|
255
|
+
attachments: input.attachments,
|
|
256
|
+
...(input.taskRefs?.length ? { taskRefs: input.taskRefs } : {}),
|
|
257
|
+
...(input.resourceFeedback?.length ? { resourceFeedback: input.resourceFeedback } : {}),
|
|
258
|
+
};
|
|
259
|
+
changed++;
|
|
260
|
+
publish({ pending, draft: restored, ...(rejected !== view.rejected ? { rejected } : {}) });
|
|
261
|
+
void flush();
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const pending = view.pending.map(item => {
|
|
265
|
+
if (item.inputId !== input.inputId || !item.answerTo)
|
|
266
|
+
return item;
|
|
267
|
+
const { answerTo: _closed, ...plain } = item;
|
|
268
|
+
return plain;
|
|
269
|
+
});
|
|
270
|
+
changed++;
|
|
271
|
+
publish({ pending, ...(rejected !== view.rejected ? { rejected } : {}) });
|
|
272
|
+
void flush();
|
|
273
|
+
}
|
|
274
|
+
async function persistPendingMutation(inputId, mode, options) {
|
|
275
|
+
if (!view.ready && !await load())
|
|
276
|
+
return { ok: false, code: view.error || 'DRAFT_UNAVAILABLE' };
|
|
277
|
+
if (active.has(inputId))
|
|
278
|
+
return { ok: false, code: 'INPUT_SENDING' };
|
|
279
|
+
if (mutatingIds.has(inputId))
|
|
280
|
+
return { ok: false, code: 'INPUT_BUSY' };
|
|
281
|
+
if (!view.pending.some(item => item.inputId === inputId))
|
|
282
|
+
return { ok: false, code: 'INPUT_NOT_PENDING' };
|
|
283
|
+
mutatingIds.add(inputId);
|
|
284
|
+
publish({ mutating: [...mutatingIds] });
|
|
285
|
+
try {
|
|
286
|
+
while (writing || saved !== changed) {
|
|
287
|
+
const ok = writing ? await writing : await flush();
|
|
288
|
+
if (!ok)
|
|
289
|
+
return { ok: false, code: view.error || 'DRAFT_SAVE_FAILED' };
|
|
290
|
+
}
|
|
291
|
+
if (active.has(inputId))
|
|
292
|
+
return { ok: false, code: 'INPUT_SENDING' };
|
|
293
|
+
const target = view.pending.find(item => item.inputId === inputId);
|
|
294
|
+
if (!target)
|
|
295
|
+
return { ok: false, code: 'INPUT_NOT_PENDING' };
|
|
296
|
+
if (mode === 'restore' && !options?.replaceDraft && !isDraftEmpty(view.draft)) {
|
|
297
|
+
return { ok: false, code: 'DRAFT_NOT_EMPTY' };
|
|
298
|
+
}
|
|
299
|
+
const baseVersion = changed;
|
|
300
|
+
const nextPendingForDisk = view.pending.filter(item => item.inputId !== inputId);
|
|
301
|
+
const nextDraftForDisk = mode === 'restore' ? buildRestoredDraft(target, view.rejected[inputId]) : view.draft;
|
|
302
|
+
let writtenRevision = null;
|
|
303
|
+
writing = (async () => {
|
|
304
|
+
writtenRevision = await writeState(nextDraftForDisk, nextPendingForDisk);
|
|
305
|
+
return writtenRevision !== null;
|
|
306
|
+
})().finally(() => { writing = null; });
|
|
307
|
+
const ok = await writing;
|
|
308
|
+
if (!ok || writtenRevision === null)
|
|
309
|
+
return { ok: false, code: view.error || 'DRAFT_SAVE_FAILED' };
|
|
310
|
+
if (mode === 'restore' && changed !== baseVersion) {
|
|
311
|
+
publish({ revision: writtenRevision, error: null });
|
|
312
|
+
void flush();
|
|
313
|
+
return { ok: false, code: 'DRAFT_NOT_EMPTY' };
|
|
314
|
+
}
|
|
315
|
+
draftOrigins.delete(inputId);
|
|
316
|
+
optionOrigins.delete(inputId);
|
|
317
|
+
const rejected = omitRejected([inputId]);
|
|
318
|
+
const nextPending = view.pending.filter(item => item.inputId !== inputId);
|
|
319
|
+
if (changed === baseVersion)
|
|
320
|
+
saved = baseVersion;
|
|
321
|
+
publish({
|
|
322
|
+
revision: writtenRevision,
|
|
323
|
+
error: null,
|
|
324
|
+
pending: nextPending,
|
|
325
|
+
...(mode === 'restore' ? { draft: nextDraftForDisk } : {}),
|
|
326
|
+
...(rejected !== view.rejected ? { rejected } : {}),
|
|
327
|
+
});
|
|
328
|
+
if (saved !== changed)
|
|
329
|
+
void flush();
|
|
330
|
+
return { ok: true };
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
mutatingIds.delete(inputId);
|
|
334
|
+
publish({ mutating: mutatingIds.size ? [...mutatingIds] : EMPTY_IDS });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
101
337
|
function dispatch(input) {
|
|
338
|
+
if (mutatingIds.has(input.inputId))
|
|
339
|
+
return Promise.resolve({ ok: false, code: 'INPUT_BUSY' });
|
|
102
340
|
const running = active.get(input.inputId);
|
|
103
341
|
if (running)
|
|
104
342
|
return running;
|
|
@@ -114,8 +352,34 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
114
352
|
acknowledge([input.inputId]);
|
|
115
353
|
return result;
|
|
116
354
|
}
|
|
355
|
+
if (isDefiniteProjectInputRejection(result, workspaceId, input.inputId)) {
|
|
356
|
+
const code = result.code;
|
|
357
|
+
if (!view.pending.some(item => item.inputId === input.inputId))
|
|
358
|
+
return { ok: false, code };
|
|
359
|
+
if (code === 'QUESTION_CLOSED') {
|
|
360
|
+
if (optionOrigins.has(input.inputId) && !hasComposedContent(input)) {
|
|
361
|
+
dismiss([input.inputId]);
|
|
362
|
+
}
|
|
363
|
+
else if (draftOrigins.has(input.inputId)) {
|
|
364
|
+
const originalQuote = draftOrigins.get(input.inputId) ?? null;
|
|
365
|
+
draftOrigins.delete(input.inputId);
|
|
366
|
+
recoverClosedDraft(input, originalQuote);
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
markRejected(input.inputId, code);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
markRejected(input.inputId, code);
|
|
374
|
+
}
|
|
375
|
+
return { ok: false, code };
|
|
376
|
+
}
|
|
377
|
+
clearRejected([input.inputId]);
|
|
117
378
|
return { ok: false, code: result.code || 'INPUT_OUTCOME_UNKNOWN' };
|
|
118
|
-
}).catch(() =>
|
|
379
|
+
}).catch(() => {
|
|
380
|
+
clearRejected([input.inputId]);
|
|
381
|
+
return { ok: false, code: 'INPUT_OUTCOME_UNKNOWN' };
|
|
382
|
+
});
|
|
119
383
|
let timer;
|
|
120
384
|
try {
|
|
121
385
|
return await Promise.race([submitted, new Promise(resolve => {
|
|
@@ -131,7 +395,8 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
131
395
|
}
|
|
132
396
|
finally {
|
|
133
397
|
active.delete(input.inputId);
|
|
134
|
-
|
|
398
|
+
const sending = view.sending.filter(id => id !== input.inputId);
|
|
399
|
+
publish({ sending: sending.length ? sending : EMPTY_IDS });
|
|
135
400
|
}
|
|
136
401
|
})();
|
|
137
402
|
active.set(input.inputId, job);
|
|
@@ -151,9 +416,15 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
151
416
|
if (!text && !attachments.length && !taskRefs.length && !resourceFeedback.some(row => row.resource.kind === 'interactive'))
|
|
152
417
|
return { ok: false, code: 'INVALID_INPUT' };
|
|
153
418
|
if (!useDraft && options.answerTo) {
|
|
419
|
+
if (view.received.some(item => item.answerTo === options.answerTo))
|
|
420
|
+
return { ok: false, code: 'QUESTION_CLOSED' };
|
|
154
421
|
const existing = view.pending.find(item => item.answerTo === options.answerTo);
|
|
155
|
-
if (existing)
|
|
156
|
-
|
|
422
|
+
if (existing) {
|
|
423
|
+
if (view.rejected[existing.inputId] === 'QUESTION_CLOSED')
|
|
424
|
+
return { ok: false, code: 'QUESTION_CLOSED' };
|
|
425
|
+
if (!view.rejected[existing.inputId])
|
|
426
|
+
return existing.text === text ? dispatch(existing) : { ok: false, code: 'INPUT_PENDING_ANSWER' };
|
|
427
|
+
}
|
|
157
428
|
}
|
|
158
429
|
if (view.pending.length >= 32)
|
|
159
430
|
return { ok: false, code: 'DRAFT_LIMIT' };
|
|
@@ -167,6 +438,10 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
167
438
|
}
|
|
168
439
|
const input = { inputId: createId(), text, attachments, ...(resourceFeedback.length ? { resourceFeedback } : {}), ...(taskRefs.length ? { taskRefs } : {}), createdAt: now(), quoteRefs: options.quoteRefs ?? [],
|
|
169
440
|
...(options.answerTo ? { answerTo: options.answerTo } : {}) };
|
|
441
|
+
if (useDraft)
|
|
442
|
+
draftOrigins.set(input.inputId, draft.quote);
|
|
443
|
+
else if (options.answerTo)
|
|
444
|
+
optionOrigins.add(input.inputId);
|
|
170
445
|
changed++;
|
|
171
446
|
publish({ pending: [...view.pending, input], ...(useDraft ? { draft: emptyInputDraft() } : {}) });
|
|
172
447
|
return dispatch(input);
|
|
@@ -175,17 +450,42 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
175
450
|
getSnapshot: () => view,
|
|
176
451
|
subscribe(fn) { listeners.add(fn); return () => { listeners.delete(fn); }; },
|
|
177
452
|
load, edit, flush, acknowledge, send,
|
|
178
|
-
|
|
179
|
-
|
|
453
|
+
discard: (inputId) => persistPendingMutation(inputId, 'discard'),
|
|
454
|
+
restoreToDraft: (inputId, options) => persistPendingMutation(inputId, 'restore', options),
|
|
455
|
+
observeReceived(ids, answered) {
|
|
456
|
+
const isDuplicateCommittedReplyAnswer = (item) => Boolean(answered?.length
|
|
457
|
+
&& !active.has(item.inputId)
|
|
458
|
+
&& item.answerTo?.startsWith('card:question:reply:')
|
|
459
|
+
&& !item.attachments.length
|
|
460
|
+
&& !item.taskRefs?.length
|
|
461
|
+
&& !item.resourceFeedback?.length
|
|
462
|
+
&& !item.quoteRefs.length
|
|
463
|
+
&& answered.some(entry => entry.answerTo === item.answerTo && entry.text.trim() === item.text.trim()));
|
|
464
|
+
const dropped = [];
|
|
465
|
+
const pending = view.pending.filter(item => {
|
|
466
|
+
const drop = ids.includes(item.inputId) || isDuplicateCommittedReplyAnswer(item);
|
|
467
|
+
if (drop) {
|
|
468
|
+
draftOrigins.delete(item.inputId);
|
|
469
|
+
optionOrigins.delete(item.inputId);
|
|
470
|
+
dropped.push(item.inputId);
|
|
471
|
+
}
|
|
472
|
+
return !drop;
|
|
473
|
+
});
|
|
474
|
+
const rejected = dropped.length ? omitRejected(dropped) : view.rejected;
|
|
180
475
|
const received = view.received.filter(item => !ids.includes(item.inputId));
|
|
181
476
|
if (pending.length !== view.pending.length) {
|
|
182
477
|
changed++;
|
|
183
|
-
publish({ pending, received });
|
|
478
|
+
publish({ pending, received, ...(rejected !== view.rejected ? { rejected } : {}) });
|
|
184
479
|
void flush();
|
|
185
480
|
}
|
|
186
481
|
else if (received.length !== view.received.length)
|
|
187
482
|
publish({ received });
|
|
188
483
|
},
|
|
189
|
-
retry(inputId) {
|
|
484
|
+
retry(inputId) {
|
|
485
|
+
if (mutatingIds.has(inputId))
|
|
486
|
+
return Promise.resolve({ ok: false, code: 'INPUT_BUSY' });
|
|
487
|
+
const input = view.pending.find(item => item.inputId === inputId);
|
|
488
|
+
return input ? dispatch(input) : Promise.resolve({ ok: false, code: 'INPUT_NOT_PENDING' });
|
|
489
|
+
},
|
|
190
490
|
};
|
|
191
491
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peer-agent/runtime-core",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.46",
|
|
4
4
|
"description": "Host-neutral capability, permission, projection, and Evidence primitives for Peer Agent runtimes.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"LICENSE"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@peer-agent/protocol": "0.1.0-rc.
|
|
41
|
+
"@peer-agent/protocol": "0.1.0-rc.46"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"typescript": "^5.9.3"
|