@peer-agent/runtime-core 0.1.0-rc.43 → 0.1.0-rc.45

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,4 +1,31 @@
1
1
  import { encodeResourceFeedbackTransport, normalizeResourceFeedbackList, emptyInputDraft, projectInputWasReceived } from '@peer-agent/protocol';
2
+ const DRAFT_UPLOAD_CHUNK_BYTES = 16 * 1024 * 1024;
3
+ function estimateUploadBytes(item) {
4
+ const body = item.kind === 'image' ? item.dataUrl.length : item.kind === 'text' ? item.text.length : 0;
5
+ return body + 512;
6
+ }
7
+ function partitionUploadChunks(uploads) {
8
+ if (uploads.length <= 1)
9
+ return [uploads];
10
+ const chunks = [];
11
+ let current = [];
12
+ let currentBytes = 0;
13
+ for (const item of uploads) {
14
+ const bytes = estimateUploadBytes(item);
15
+ if (current.length > 0 && currentBytes + bytes > DRAFT_UPLOAD_CHUNK_BYTES) {
16
+ chunks.push(current);
17
+ current = [item];
18
+ currentBytes = bytes;
19
+ }
20
+ else {
21
+ current.push(item);
22
+ currentBytes += bytes;
23
+ }
24
+ }
25
+ if (current.length > 0)
26
+ chunks.push(current);
27
+ return chunks;
28
+ }
2
29
  /** Presentation continuity only; ports own storage and the existing governed input queue. */
3
30
  export function createProjectInputRecovery({ workspaceId, surface, port, createId, now = () => new Date().toISOString(), receiptTimeoutMs = 30_000 }) {
4
31
  const address = { workspaceId, surface };
@@ -50,19 +77,47 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
50
77
  const all = [...state.draft.attachments, ...state.pending.flatMap(item => item.attachments)];
51
78
  const uploads = [...new Map(all.filter(item => storedAttachments.get(item.id) !== item).map(item => [item.id, item])).values()];
52
79
  const { attachments, ...draft } = state.draft;
80
+ const chunks = partitionUploadChunks(uploads);
81
+ let revision = state.revision;
82
+ const available = new Set(storedAttachments.keys());
53
83
  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;
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 });
60
115
  }
61
116
  saved = version;
62
117
  storedAttachments.clear();
63
118
  for (const item of all)
64
119
  storedAttachments.set(item.id, item);
65
- publish({ revision: result.revision, error: null });
120
+ publish({ revision, error: null });
66
121
  }
67
122
  catch {
68
123
  publish({ error: 'DRAFT_SAVE_FAILED' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peer-agent/runtime-core",
3
- "version": "0.1.0-rc.43",
3
+ "version": "0.1.0-rc.45",
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.43"
41
+ "@peer-agent/protocol": "0.1.0-rc.45"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "^5.9.3"