@peer-agent/runtime-core 0.1.0-rc.45 → 0.1.0-rc.47
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,4 +1,12 @@
|
|
|
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
|
+
]);
|
|
2
10
|
const DRAFT_UPLOAD_CHUNK_BYTES = 16 * 1024 * 1024;
|
|
3
11
|
function estimateUploadBytes(item) {
|
|
4
12
|
const body = item.kind === 'image' ? item.dataUrl.length : item.kind === 'text' ? item.text.length : 0;
|
|
@@ -29,13 +37,108 @@ function partitionUploadChunks(uploads) {
|
|
|
29
37
|
/** Presentation continuity only; ports own storage and the existing governed input queue. */
|
|
30
38
|
export function createProjectInputRecovery({ workspaceId, surface, port, createId, now = () => new Date().toISOString(), receiptTimeoutMs = 30_000 }) {
|
|
31
39
|
const address = { workspaceId, surface };
|
|
32
|
-
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 };
|
|
33
41
|
let changed = 0, saved = 0;
|
|
34
42
|
let earlyEdits = null;
|
|
35
43
|
let writing = null, loading = null;
|
|
36
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();
|
|
37
48
|
const publish = (patch) => { view = { ...view, ...patch }; for (const fn of listeners)
|
|
38
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
|
+
}
|
|
39
142
|
async function load() {
|
|
40
143
|
if (loading)
|
|
41
144
|
return loading;
|
|
@@ -69,60 +172,23 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
69
172
|
async function flush() {
|
|
70
173
|
if (!view.ready)
|
|
71
174
|
return false;
|
|
72
|
-
|
|
73
|
-
|
|
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;
|
|
74
184
|
writing = (async () => {
|
|
75
185
|
while (saved !== changed) {
|
|
76
186
|
const version = changed, state = view;
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
const { attachments, ...draft } = state.draft;
|
|
80
|
-
const chunks = partitionUploadChunks(uploads);
|
|
81
|
-
let revision = state.revision;
|
|
82
|
-
const available = new Set(storedAttachments.keys());
|
|
83
|
-
try {
|
|
84
|
-
for (let i = 0; i < chunks.length; i++) {
|
|
85
|
-
const chunk = chunks[i];
|
|
86
|
-
for (const item of chunk)
|
|
87
|
-
available.add(item.id);
|
|
88
|
-
const isFinal = i === chunks.length - 1;
|
|
89
|
-
const result = await port.write({
|
|
90
|
-
...address,
|
|
91
|
-
revision,
|
|
92
|
-
draft: {
|
|
93
|
-
...draft,
|
|
94
|
-
attachmentIds: isFinal
|
|
95
|
-
? attachments.map(item => item.id)
|
|
96
|
-
: attachments.filter(item => available.has(item.id)).map(item => item.id),
|
|
97
|
-
},
|
|
98
|
-
pending: state.pending.map(({ attachments: pendingAttachments, ...input }) => ({
|
|
99
|
-
...input,
|
|
100
|
-
attachmentIds: isFinal
|
|
101
|
-
? pendingAttachments.map(item => item.id)
|
|
102
|
-
: pendingAttachments.filter(item => available.has(item.id)).map(item => item.id),
|
|
103
|
-
})),
|
|
104
|
-
uploads: chunk,
|
|
105
|
-
});
|
|
106
|
-
if (!result.ok) {
|
|
107
|
-
publish({ error: result.code });
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
revision = result.revision;
|
|
111
|
-
for (const item of chunk)
|
|
112
|
-
storedAttachments.set(item.id, item);
|
|
113
|
-
if (!isFinal)
|
|
114
|
-
publish({ revision, error: null });
|
|
115
|
-
}
|
|
116
|
-
saved = version;
|
|
117
|
-
storedAttachments.clear();
|
|
118
|
-
for (const item of all)
|
|
119
|
-
storedAttachments.set(item.id, item);
|
|
120
|
-
publish({ revision, error: null });
|
|
121
|
-
}
|
|
122
|
-
catch {
|
|
123
|
-
publish({ error: 'DRAFT_SAVE_FAILED' });
|
|
187
|
+
const revision = await writeState(state.draft, state.pending);
|
|
188
|
+
if (revision === null)
|
|
124
189
|
return false;
|
|
125
|
-
|
|
190
|
+
saved = version;
|
|
191
|
+
publish({ revision, error: null });
|
|
126
192
|
}
|
|
127
193
|
return true;
|
|
128
194
|
})().finally(() => { writing = null; });
|
|
@@ -145,15 +211,132 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
145
211
|
void flush();
|
|
146
212
|
}
|
|
147
213
|
function acknowledge(ids) {
|
|
214
|
+
for (const id of ids) {
|
|
215
|
+
draftOrigins.delete(id);
|
|
216
|
+
optionOrigins.delete(id);
|
|
217
|
+
}
|
|
218
|
+
const rejected = omitRejected(ids);
|
|
148
219
|
const pending = view.pending.filter(item => !ids.includes(item.inputId));
|
|
149
|
-
if (pending.length === view.pending.length)
|
|
220
|
+
if (pending.length === view.pending.length) {
|
|
221
|
+
if (rejected !== view.rejected)
|
|
222
|
+
publish({ rejected });
|
|
150
223
|
return;
|
|
224
|
+
}
|
|
151
225
|
const received = [...view.received, ...view.pending.filter(item => ids.includes(item.inputId))];
|
|
152
226
|
changed++;
|
|
153
|
-
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 } : {}) });
|
|
244
|
+
void flush();
|
|
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 } : {}) });
|
|
154
272
|
void flush();
|
|
155
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
|
+
}
|
|
156
337
|
function dispatch(input) {
|
|
338
|
+
if (mutatingIds.has(input.inputId))
|
|
339
|
+
return Promise.resolve({ ok: false, code: 'INPUT_BUSY' });
|
|
157
340
|
const running = active.get(input.inputId);
|
|
158
341
|
if (running)
|
|
159
342
|
return running;
|
|
@@ -169,8 +352,34 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
169
352
|
acknowledge([input.inputId]);
|
|
170
353
|
return result;
|
|
171
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]);
|
|
172
378
|
return { ok: false, code: result.code || 'INPUT_OUTCOME_UNKNOWN' };
|
|
173
|
-
}).catch(() =>
|
|
379
|
+
}).catch(() => {
|
|
380
|
+
clearRejected([input.inputId]);
|
|
381
|
+
return { ok: false, code: 'INPUT_OUTCOME_UNKNOWN' };
|
|
382
|
+
});
|
|
174
383
|
let timer;
|
|
175
384
|
try {
|
|
176
385
|
return await Promise.race([submitted, new Promise(resolve => {
|
|
@@ -186,7 +395,8 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
186
395
|
}
|
|
187
396
|
finally {
|
|
188
397
|
active.delete(input.inputId);
|
|
189
|
-
|
|
398
|
+
const sending = view.sending.filter(id => id !== input.inputId);
|
|
399
|
+
publish({ sending: sending.length ? sending : EMPTY_IDS });
|
|
190
400
|
}
|
|
191
401
|
})();
|
|
192
402
|
active.set(input.inputId, job);
|
|
@@ -206,9 +416,15 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
206
416
|
if (!text && !attachments.length && !taskRefs.length && !resourceFeedback.some(row => row.resource.kind === 'interactive'))
|
|
207
417
|
return { ok: false, code: 'INVALID_INPUT' };
|
|
208
418
|
if (!useDraft && options.answerTo) {
|
|
419
|
+
if (view.received.some(item => item.answerTo === options.answerTo))
|
|
420
|
+
return { ok: false, code: 'QUESTION_CLOSED' };
|
|
209
421
|
const existing = view.pending.find(item => item.answerTo === options.answerTo);
|
|
210
|
-
if (existing)
|
|
211
|
-
|
|
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
|
+
}
|
|
212
428
|
}
|
|
213
429
|
if (view.pending.length >= 32)
|
|
214
430
|
return { ok: false, code: 'DRAFT_LIMIT' };
|
|
@@ -222,6 +438,10 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
222
438
|
}
|
|
223
439
|
const input = { inputId: createId(), text, attachments, ...(resourceFeedback.length ? { resourceFeedback } : {}), ...(taskRefs.length ? { taskRefs } : {}), createdAt: now(), quoteRefs: options.quoteRefs ?? [],
|
|
224
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);
|
|
225
445
|
changed++;
|
|
226
446
|
publish({ pending: [...view.pending, input], ...(useDraft ? { draft: emptyInputDraft() } : {}) });
|
|
227
447
|
return dispatch(input);
|
|
@@ -230,17 +450,42 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
230
450
|
getSnapshot: () => view,
|
|
231
451
|
subscribe(fn) { listeners.add(fn); return () => { listeners.delete(fn); }; },
|
|
232
452
|
load, edit, flush, acknowledge, send,
|
|
233
|
-
|
|
234
|
-
|
|
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;
|
|
235
475
|
const received = view.received.filter(item => !ids.includes(item.inputId));
|
|
236
476
|
if (pending.length !== view.pending.length) {
|
|
237
477
|
changed++;
|
|
238
|
-
publish({ pending, received });
|
|
478
|
+
publish({ pending, received, ...(rejected !== view.rejected ? { rejected } : {}) });
|
|
239
479
|
void flush();
|
|
240
480
|
}
|
|
241
481
|
else if (received.length !== view.received.length)
|
|
242
482
|
publish({ received });
|
|
243
483
|
},
|
|
244
|
-
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
|
+
},
|
|
245
490
|
};
|
|
246
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.47",
|
|
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.47"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"typescript": "^5.9.3"
|