@peer-agent/runtime-core 0.1.0-rc.41 → 0.1.0-rc.42
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/dist/context-accounting-lifecycle.d.ts +1 -1
- package/dist/context-accounting-lifecycle.js +1 -1
- package/dist/conversation-history-projector.d.ts +2 -1
- package/dist/conversation-history-projector.js +55 -4
- package/dist/input-prediction.js +40 -15
- package/dist/project-input-recovery.d.ts +1 -0
- package/dist/project-input-recovery.js +3 -2
- package/package.json +2 -2
|
@@ -31,7 +31,7 @@ export declare function createUnknownContextAccountingSnapshot(input: Readonly<{
|
|
|
31
31
|
export declare function isContextAccountingSnapshotCurrent(snapshot: ContextAccountingSnapshot | null | undefined, identity: ContextAccountingIdentity): snapshot is ContextAccountingSnapshot;
|
|
32
32
|
/**
|
|
33
33
|
* Reproject a restored/displayed context-accounting snapshot onto the current
|
|
34
|
-
* model config window
|
|
34
|
+
* model config window. Occupied tokens stay from
|
|
35
35
|
* the snapshot; only capacity fields + percent are refreshed so UI/runtime do
|
|
36
36
|
* not keep a stale window after the user changes tiers or projection is fixed.
|
|
37
37
|
*/
|
|
@@ -42,7 +42,7 @@ export function isContextAccountingSnapshotCurrent(snapshot, identity) {
|
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* Reproject a restored/displayed context-accounting snapshot onto the current
|
|
45
|
-
* model config window
|
|
45
|
+
* model config window. Occupied tokens stay from
|
|
46
46
|
* the snapshot; only capacity fields + percent are refreshed so UI/runtime do
|
|
47
47
|
* not keep a stale window after the user changes tiers or projection is fixed.
|
|
48
48
|
*/
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* Oversized tool bodies stay intact here so the host can spill them to a
|
|
8
8
|
* local file before the provider request. This projector does not drop them.
|
|
9
9
|
*/
|
|
10
|
-
export declare const CANONICAL_HISTORY_PROJECTOR_VERSION =
|
|
10
|
+
export declare const CANONICAL_HISTORY_PROJECTOR_VERSION = 3;
|
|
11
11
|
export type CanonicalHistoryContentPart = {
|
|
12
12
|
readonly type: 'text';
|
|
13
13
|
readonly text: string;
|
|
@@ -30,6 +30,7 @@ export interface CanonicalHistoryMessage {
|
|
|
30
30
|
readonly toolCalls?: readonly CanonicalHistoryToolCall[];
|
|
31
31
|
}
|
|
32
32
|
export interface CanonicalHistoryContinuity {
|
|
33
|
+
readonly compactionEpoch?: number;
|
|
33
34
|
readonly summary: string;
|
|
34
35
|
readonly sourceMessageId: string;
|
|
35
36
|
readonly method: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { formatResourceFeedbackContext, projectResourceFeedbackTransport } from '@peer-agent/protocol';
|
|
1
|
+
import { formatResourceFeedbackContext, projectResourceFeedbackTransport, normalizeCompactionHistorySelection } from '@peer-agent/protocol';
|
|
2
2
|
/**
|
|
3
3
|
* Canonical persisted-conversation -> active provider history projection.
|
|
4
4
|
*
|
|
@@ -8,7 +8,7 @@ import { formatResourceFeedbackContext, projectResourceFeedbackTransport } from
|
|
|
8
8
|
* Oversized tool bodies stay intact here so the host can spill them to a
|
|
9
9
|
* local file before the provider request. This projector does not drop them.
|
|
10
10
|
*/
|
|
11
|
-
export const CANONICAL_HISTORY_PROJECTOR_VERSION =
|
|
11
|
+
export const CANONICAL_HISTORY_PROJECTOR_VERSION = 3;
|
|
12
12
|
function recordOf(value) {
|
|
13
13
|
return value && typeof value === 'object' && !Array.isArray(value)
|
|
14
14
|
? value
|
|
@@ -477,10 +477,50 @@ function scanCompactionBoundary(messages) {
|
|
|
477
477
|
});
|
|
478
478
|
return { compactionBoundaryIndex, continuity };
|
|
479
479
|
}
|
|
480
|
+
/** Append-only compaction preserves raw chronology and stable source identity. */
|
|
481
|
+
function historySelectionCollector(messages, boundary) {
|
|
482
|
+
const row = recordOf(messages[boundary]);
|
|
483
|
+
const marker = row && compactionMarker(row);
|
|
484
|
+
if (!marker || marker.historySelection === undefined)
|
|
485
|
+
return null;
|
|
486
|
+
const selection = normalizeCompactionHistorySelection(marker.historySelection);
|
|
487
|
+
if (!selection || !row?.id || !continuityFromBoundary(row, boundary)?.summary)
|
|
488
|
+
throw new Error('compaction_history_selection_invalid');
|
|
489
|
+
const wanted = new Set(selection.retainedMessageIds);
|
|
490
|
+
const found = new Map();
|
|
491
|
+
const ordered = [];
|
|
492
|
+
return {
|
|
493
|
+
visit(i, raw) {
|
|
494
|
+
const value = recordOf(raw);
|
|
495
|
+
const id = typeof value?.id === 'string' ? value.id : '';
|
|
496
|
+
if (!wanted.has(id))
|
|
497
|
+
return;
|
|
498
|
+
if (i >= boundary || found.has(id) || !value || compactionMarker(value) || !['user', 'assistant', 'tool'].includes(String(value.role)) || ['agent_message', 'agent_activity'].includes(String(value.kind)))
|
|
499
|
+
throw new Error('compaction_history_selection_invalid');
|
|
500
|
+
found.set(id, value);
|
|
501
|
+
ordered.push(id);
|
|
502
|
+
},
|
|
503
|
+
finish() {
|
|
504
|
+
if (ordered.length !== wanted.size || ordered.some((id, i) => id !== selection.retainedMessageIds[i]))
|
|
505
|
+
throw new Error('compaction_history_selection_invalid');
|
|
506
|
+
return [...selection.retainedMessageIds.map(id => found.get(id)), ...messages.slice(boundary + 1)];
|
|
507
|
+
},
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
function selectedHistory(messages, boundary) {
|
|
511
|
+
const collector = historySelectionCollector(messages, boundary);
|
|
512
|
+
if (!collector)
|
|
513
|
+
return messages.slice(boundary + 1);
|
|
514
|
+
messages.forEach((row, i) => collector.visit(i, row));
|
|
515
|
+
return collector.finish();
|
|
516
|
+
}
|
|
480
517
|
export function projectConversationHistory(values) {
|
|
481
518
|
const messages = Array.isArray(values) ? values : [];
|
|
482
519
|
const scanned = scanCompactionBoundary(messages);
|
|
483
|
-
const active = foldLegacyToolRows(messages
|
|
520
|
+
const active = foldLegacyToolRows(selectedHistory(messages, scanned.compactionBoundaryIndex));
|
|
521
|
+
const selection = recordOf(compactionMarker(recordOf(messages[scanned.compactionBoundaryIndex]) ?? {})?.historySelection);
|
|
522
|
+
if (selection && scanned.continuity)
|
|
523
|
+
scanned.continuity = { ...scanned.continuity, compactionEpoch: Number(selection.compactionEpoch) };
|
|
484
524
|
return finishCanonicalHistory(scanned.compactionBoundaryIndex, scanned.continuity, active.flatMap(projectMessage));
|
|
485
525
|
}
|
|
486
526
|
/**
|
|
@@ -504,7 +544,18 @@ export async function projectConversationHistoryYielding(values, options) {
|
|
|
504
544
|
compactionBoundaryIndex = index;
|
|
505
545
|
continuity = continuityFromBoundary(message, index);
|
|
506
546
|
}
|
|
507
|
-
const
|
|
547
|
+
const collector = historySelectionCollector(messages, compactionBoundaryIndex);
|
|
548
|
+
if (collector) {
|
|
549
|
+
for (let index = 0; index < messages.length; index++) {
|
|
550
|
+
if (index > 0 && index % yieldEvery === 0)
|
|
551
|
+
await pause();
|
|
552
|
+
collector.visit(index, messages[index]);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const active = foldLegacyToolRows(collector ? collector.finish() : messages.slice(compactionBoundaryIndex + 1));
|
|
556
|
+
const selection = recordOf(compactionMarker(recordOf(messages[compactionBoundaryIndex]) ?? {})?.historySelection);
|
|
557
|
+
if (selection && continuity)
|
|
558
|
+
continuity = { ...continuity, compactionEpoch: Number(selection.compactionEpoch) };
|
|
508
559
|
const projectedMessages = [];
|
|
509
560
|
for (let index = 0; index < active.length; index += 1) {
|
|
510
561
|
if (index > 0 && index % yieldEvery === 0)
|
package/dist/input-prediction.js
CHANGED
|
@@ -1,21 +1,46 @@
|
|
|
1
1
|
import { INPUT_PREDICTION_LIMITS } from '@peer-agent/protocol';
|
|
2
|
+
const IDLE_VIEW = Object.freeze({ status: 'idle', candidate: null });
|
|
3
|
+
const WAITING_VIEW = Object.freeze({ status: 'waiting', candidate: null });
|
|
4
|
+
const REQUESTING_VIEW = Object.freeze({ status: 'requesting', candidate: null });
|
|
5
|
+
function samePredictionInput(a, b) {
|
|
6
|
+
return a !== null
|
|
7
|
+
&& a.workspaceId === b.workspaceId
|
|
8
|
+
&& a.conversationId === b.conversationId
|
|
9
|
+
&& a.surface === b.surface
|
|
10
|
+
&& a.text === b.text
|
|
11
|
+
&& a.selectionStart === b.selectionStart
|
|
12
|
+
&& a.selectionEnd === b.selectionEnd
|
|
13
|
+
&& a.contextVersion === b.contextVersion
|
|
14
|
+
&& a.quoteMessageId === b.quoteMessageId
|
|
15
|
+
&& a.eligible === b.eligible
|
|
16
|
+
&& a.active === b.active
|
|
17
|
+
&& a.composing === b.composing;
|
|
18
|
+
}
|
|
2
19
|
/** Owns every asynchronous generation; the editor retains all draft ownership. */
|
|
3
20
|
export function createInputPredictionController({ port, createId, now = Date.now, apply, debounceMs = { next_turn: 300, continuation: 500 }, validationMs = INPUT_PREDICTION_LIMITS.validationMs, }) {
|
|
4
|
-
let input = null,
|
|
21
|
+
let input = null, dismissed = '', acceptedText = null;
|
|
5
22
|
let version = 0, generation = 0, requestId = null, disposed = false;
|
|
6
23
|
let timer, expiry;
|
|
7
|
-
let view =
|
|
24
|
+
let view = IDLE_VIEW;
|
|
8
25
|
const listeners = new Set();
|
|
9
|
-
function publish(next) {
|
|
10
|
-
|
|
11
|
-
|
|
26
|
+
function publish(next) {
|
|
27
|
+
if (view.status === next.status && view.candidate === next.candidate)
|
|
28
|
+
return;
|
|
29
|
+
view = next;
|
|
30
|
+
for (const listener of listeners)
|
|
31
|
+
listener();
|
|
32
|
+
}
|
|
33
|
+
function stopPending() {
|
|
12
34
|
generation++;
|
|
13
35
|
clearTimeout(timer);
|
|
14
36
|
clearTimeout(expiry);
|
|
15
37
|
if (requestId)
|
|
16
38
|
void port.cancel({ requestId, composerInstanceId }).catch(() => { });
|
|
17
39
|
requestId = null;
|
|
18
|
-
|
|
40
|
+
}
|
|
41
|
+
function cancel() {
|
|
42
|
+
stopPending();
|
|
43
|
+
publish(IDLE_VIEW);
|
|
19
44
|
}
|
|
20
45
|
const composerInstanceId = createId();
|
|
21
46
|
const eligible = (value) => value.eligible && value.active && !value.composing
|
|
@@ -28,7 +53,7 @@ export function createInputPredictionController({ port, createId, now = Date.now
|
|
|
28
53
|
const captured = input, ticket = generation, draftVersion = version;
|
|
29
54
|
requestId = createId();
|
|
30
55
|
const id = requestId;
|
|
31
|
-
publish(
|
|
56
|
+
publish(REQUESTING_VIEW);
|
|
32
57
|
try {
|
|
33
58
|
const result = await port.request({ requestId: id, composerInstanceId,
|
|
34
59
|
workspaceId: captured.workspaceId, conversationId: captured.conversationId, surface: captured.surface,
|
|
@@ -44,7 +69,7 @@ export function createInputPredictionController({ port, createId, now = Date.now
|
|
|
44
69
|
|| candidate.draftVersion !== draftVersion || candidate.mode !== (captured.text ? 'continuation' : 'next_turn')
|
|
45
70
|
|| typeof candidate.insertionText !== 'string' || !candidate.insertionText.trim()
|
|
46
71
|
|| !Number.isFinite(Date.parse(candidate.expiresAt)) || Date.parse(candidate.expiresAt) <= now()) {
|
|
47
|
-
publish(
|
|
72
|
+
publish(IDLE_VIEW);
|
|
48
73
|
return;
|
|
49
74
|
}
|
|
50
75
|
publish({ status: 'visible', candidate });
|
|
@@ -52,26 +77,26 @@ export function createInputPredictionController({ port, createId, now = Date.now
|
|
|
52
77
|
}
|
|
53
78
|
catch {
|
|
54
79
|
if (!disposed && generation === ticket)
|
|
55
|
-
publish(
|
|
80
|
+
publish(IDLE_VIEW);
|
|
56
81
|
}
|
|
57
82
|
}
|
|
58
83
|
function update(next) {
|
|
59
84
|
if (disposed)
|
|
60
85
|
return;
|
|
61
|
-
|
|
62
|
-
if (key === fingerprint)
|
|
86
|
+
if (samePredictionInput(input, next))
|
|
63
87
|
return;
|
|
64
88
|
const sameContext = input?.contextVersion === next.contextVersion && input?.conversationId === next.conversationId;
|
|
65
89
|
if (!sameContext || (next.text !== input?.text && next.text !== acceptedText))
|
|
66
90
|
acceptedText = null;
|
|
67
91
|
input = next;
|
|
68
|
-
fingerprint = key;
|
|
69
92
|
version++;
|
|
70
|
-
|
|
93
|
+
stopPending();
|
|
71
94
|
const semanticKey = JSON.stringify([next.workspaceId, next.conversationId, next.contextVersion, next.text, next.quoteMessageId]);
|
|
72
|
-
if (!eligible(next) || dismissed === semanticKey || acceptedText === next.text)
|
|
95
|
+
if (!eligible(next) || dismissed === semanticKey || acceptedText === next.text) {
|
|
96
|
+
publish(IDLE_VIEW);
|
|
73
97
|
return;
|
|
74
|
-
|
|
98
|
+
}
|
|
99
|
+
publish(WAITING_VIEW);
|
|
75
100
|
timer = setTimeout(() => { void request(); }, next.text ? debounceMs.continuation : debounceMs.next_turn);
|
|
76
101
|
}
|
|
77
102
|
async function accept() {
|
|
@@ -23,6 +23,7 @@ export declare function createProjectInputRecovery({ workspaceId, surface, port,
|
|
|
23
23
|
send: (options?: {
|
|
24
24
|
text?: string;
|
|
25
25
|
quoteRefs?: readonly string[];
|
|
26
|
+
taskRefs?: readonly import("@peer-agent/protocol").InputDraftTaskRef[];
|
|
26
27
|
answerTo?: string;
|
|
27
28
|
useDraft?: boolean;
|
|
28
29
|
}) => Promise<ProjectAgentSubmitInputResult>;
|
|
@@ -142,12 +142,13 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
142
142
|
return { ok: false, code: view.error || 'DRAFT_UNAVAILABLE' };
|
|
143
143
|
const useDraft = options.useDraft !== false, draft = view.draft;
|
|
144
144
|
const text = (options.text ?? draft.text).trim(), attachments = useDraft ? draft.attachments : [];
|
|
145
|
+
const taskRefs = options.taskRefs ?? (useDraft ? (draft.taskRefs ?? []) : []);
|
|
145
146
|
if (useDraft && options.text !== undefined && text !== draft.text.trim())
|
|
146
147
|
return { ok: false, code: 'INPUT_CHANGED' };
|
|
147
148
|
const resourceFeedback = useDraft ? normalizeResourceFeedbackList(draft.resourceFeedback) : [];
|
|
148
149
|
if (!resourceFeedback)
|
|
149
150
|
return { ok: false, code: 'INVALID_RESOURCE_FEEDBACK' };
|
|
150
|
-
if (!text && !attachments.length && !resourceFeedback.some(row => row.resource.kind === 'interactive'))
|
|
151
|
+
if (!text && !attachments.length && !taskRefs.length && !resourceFeedback.some(row => row.resource.kind === 'interactive'))
|
|
151
152
|
return { ok: false, code: 'INVALID_INPUT' };
|
|
152
153
|
if (!useDraft && options.answerTo) {
|
|
153
154
|
const existing = view.pending.find(item => item.answerTo === options.answerTo);
|
|
@@ -164,7 +165,7 @@ export function createProjectInputRecovery({ workspaceId, surface, port, createI
|
|
|
164
165
|
catch (error) {
|
|
165
166
|
return { ok: false, code: error.code ?? 'INVALID_RESOURCE_FEEDBACK' };
|
|
166
167
|
}
|
|
167
|
-
const input = { inputId: createId(), text, attachments, ...(resourceFeedback.length ? { resourceFeedback } : {}), createdAt: now(), quoteRefs: options.quoteRefs ?? [],
|
|
168
|
+
const input = { inputId: createId(), text, attachments, ...(resourceFeedback.length ? { resourceFeedback } : {}), ...(taskRefs.length ? { taskRefs } : {}), createdAt: now(), quoteRefs: options.quoteRefs ?? [],
|
|
168
169
|
...(options.answerTo ? { answerTo: options.answerTo } : {}) };
|
|
169
170
|
changed++;
|
|
170
171
|
publish({ pending: [...view.pending, input], ...(useDraft ? { draft: emptyInputDraft() } : {}) });
|
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.42",
|
|
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.42"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"typescript": "^5.9.3"
|