@peer-agent/runtime-core 0.1.0-rc.31 → 0.1.0-rc.34

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/index.d.ts CHANGED
@@ -23,3 +23,4 @@ export * from './conversation-history-projector.ts';
23
23
  export * from './runtime-usage-accounting.ts';
24
24
  export * from './project-input-recovery.ts';
25
25
  export * from './resource-edit-state.ts';
26
+ export * from './input-prediction.ts';
package/dist/index.js CHANGED
@@ -23,3 +23,4 @@ export * from "./conversation-history-projector.js";
23
23
  export * from "./runtime-usage-accounting.js";
24
24
  export * from "./project-input-recovery.js";
25
25
  export * from "./resource-edit-state.js";
26
+ export * from "./input-prediction.js";
@@ -0,0 +1,40 @@
1
+ import { type InputCandidate, type InputPredictionPort, type InputPredictionSurface } from '@peer-agent/protocol';
2
+ export interface PredictionInput {
3
+ readonly workspaceId: string;
4
+ readonly conversationId: string;
5
+ readonly surface: InputPredictionSurface;
6
+ readonly text: string;
7
+ readonly selectionStart: number;
8
+ readonly selectionEnd: number;
9
+ readonly contextVersion: string;
10
+ readonly quoteMessageId?: string;
11
+ readonly eligible: boolean;
12
+ readonly active: boolean;
13
+ readonly composing: boolean;
14
+ }
15
+ export interface PredictionView {
16
+ readonly status: 'idle' | 'waiting' | 'requesting' | 'visible' | 'accepting';
17
+ readonly candidate: InputCandidate | null;
18
+ }
19
+ /** Owns every asynchronous generation; the editor retains all draft ownership. */
20
+ export declare function createInputPredictionController({ port, createId, now, apply, debounceMs, validationMs, }: {
21
+ port: InputPredictionPort;
22
+ createId: () => string;
23
+ now?: () => number;
24
+ apply: (text: string, input: PredictionInput) => void;
25
+ debounceMs?: {
26
+ next_turn: number;
27
+ continuation: number;
28
+ };
29
+ validationMs?: number;
30
+ }): {
31
+ composerInstanceId: string;
32
+ update: (next: PredictionInput) => void;
33
+ accept: () => Promise<boolean>;
34
+ invalidate: () => void;
35
+ suppressDraft(text: string): void;
36
+ dismiss(): void;
37
+ getSnapshot: () => PredictionView;
38
+ subscribe(listener: () => void): () => void;
39
+ dispose(): void;
40
+ };
@@ -0,0 +1,118 @@
1
+ import { INPUT_PREDICTION_LIMITS } from '@peer-agent/protocol';
2
+ /** Owns every asynchronous generation; the editor retains all draft ownership. */
3
+ export function createInputPredictionController({ port, createId, now = Date.now, apply, debounceMs = { next_turn: 300, continuation: 500 }, validationMs = INPUT_PREDICTION_LIMITS.validationMs, }) {
4
+ let input = null, fingerprint = '', dismissed = '', acceptedText = null;
5
+ let version = 0, generation = 0, requestId = null, disposed = false;
6
+ let timer, expiry;
7
+ let view = { status: 'idle', candidate: null };
8
+ const listeners = new Set();
9
+ function publish(next) { view = next; for (const listener of listeners)
10
+ listener(); }
11
+ function cancel() {
12
+ generation++;
13
+ clearTimeout(timer);
14
+ clearTimeout(expiry);
15
+ if (requestId)
16
+ void port.cancel({ requestId, composerInstanceId }).catch(() => { });
17
+ requestId = null;
18
+ publish({ status: 'idle', candidate: null });
19
+ }
20
+ const composerInstanceId = createId();
21
+ const eligible = (value) => value.eligible && value.active && !value.composing
22
+ && !!value.workspaceId && !!value.conversationId && !!value.contextVersion
23
+ && value.selectionStart === value.selectionEnd && value.selectionEnd === value.text.length
24
+ && value.text.length <= INPUT_PREDICTION_LIMITS.draftLength;
25
+ async function request() {
26
+ if (!input || disposed || !eligible(input))
27
+ return;
28
+ const captured = input, ticket = generation, draftVersion = version;
29
+ requestId = createId();
30
+ const id = requestId;
31
+ publish({ status: 'requesting', candidate: null });
32
+ try {
33
+ const result = await port.request({ requestId: id, composerInstanceId,
34
+ workspaceId: captured.workspaceId, conversationId: captured.conversationId, surface: captured.surface,
35
+ mode: captured.text ? 'continuation' : 'next_turn', draftVersion,
36
+ draftText: captured.text, selectionStart: captured.selectionStart, selectionEnd: captured.selectionEnd,
37
+ ...(captured.quoteMessageId ? { quoteMessageId: captured.quoteMessageId } : {}),
38
+ });
39
+ if (disposed || generation !== ticket || version !== draftVersion || !input || !eligible(input))
40
+ return;
41
+ const candidate = result.kind === 'candidate' ? result.candidate : null;
42
+ if (!candidate || candidate.requestId !== id || candidate.composerInstanceId !== composerInstanceId
43
+ || candidate.workspaceId !== captured.workspaceId || candidate.conversationId !== captured.conversationId
44
+ || candidate.draftVersion !== draftVersion || candidate.mode !== (captured.text ? 'continuation' : 'next_turn')
45
+ || typeof candidate.insertionText !== 'string' || !candidate.insertionText.trim()
46
+ || !Number.isFinite(Date.parse(candidate.expiresAt)) || Date.parse(candidate.expiresAt) <= now()) {
47
+ publish({ status: 'idle', candidate: null });
48
+ return;
49
+ }
50
+ publish({ status: 'visible', candidate });
51
+ expiry = setTimeout(cancel, Math.max(0, Date.parse(candidate.expiresAt) - now()));
52
+ }
53
+ catch {
54
+ if (!disposed && generation === ticket)
55
+ publish({ status: 'idle', candidate: null });
56
+ }
57
+ }
58
+ function update(next) {
59
+ if (disposed)
60
+ return;
61
+ const key = JSON.stringify(next);
62
+ if (key === fingerprint)
63
+ return;
64
+ const sameContext = input?.contextVersion === next.contextVersion && input?.conversationId === next.conversationId;
65
+ if (!sameContext || (next.text !== input?.text && next.text !== acceptedText))
66
+ acceptedText = null;
67
+ input = next;
68
+ fingerprint = key;
69
+ version++;
70
+ cancel();
71
+ const semanticKey = JSON.stringify([next.workspaceId, next.conversationId, next.contextVersion, next.text, next.quoteMessageId]);
72
+ if (!eligible(next) || dismissed === semanticKey || acceptedText === next.text)
73
+ return;
74
+ publish({ status: 'waiting', candidate: null });
75
+ timer = setTimeout(() => { void request(); }, next.text ? debounceMs.continuation : debounceMs.next_turn);
76
+ }
77
+ async function accept() {
78
+ if (!input || !eligible(input) || view.status !== 'visible' || !view.candidate)
79
+ return false;
80
+ const captured = input, candidate = view.candidate, ticket = generation, draftVersion = version;
81
+ if (Date.parse(candidate.expiresAt) <= now()) {
82
+ cancel();
83
+ return false;
84
+ }
85
+ publish({ status: 'accepting', candidate });
86
+ let timeout;
87
+ try {
88
+ const result = await Promise.race([port.validate({ requestId: candidate.requestId,
89
+ candidateId: candidate.candidateId, composerInstanceId }),
90
+ new Promise(resolve => { timeout = setTimeout(() => resolve({ kind: 'unavailable' }), validationMs); })]);
91
+ if (disposed || generation !== ticket || version !== draftVersion || !input || !eligible(input))
92
+ return false;
93
+ if (result.kind !== 'valid' || Date.parse(candidate.expiresAt) <= now()) {
94
+ cancel();
95
+ return false;
96
+ }
97
+ acceptedText = captured.text + candidate.insertionText;
98
+ cancel();
99
+ apply(acceptedText, captured);
100
+ return true;
101
+ }
102
+ catch {
103
+ if (generation === ticket)
104
+ cancel();
105
+ return false;
106
+ }
107
+ finally {
108
+ clearTimeout(timeout);
109
+ }
110
+ }
111
+ return { composerInstanceId, update, accept, invalidate: cancel,
112
+ suppressDraft(text) { acceptedText = text; cancel(); },
113
+ dismiss() { if (input)
114
+ dismissed = JSON.stringify([input.workspaceId, input.conversationId, input.contextVersion, input.text, input.quoteMessageId]); cancel(); },
115
+ getSnapshot: () => view,
116
+ subscribe(listener) { listeners.add(listener); return () => { listeners.delete(listener); }; },
117
+ dispose() { disposed = true; cancel(); listeners.clear(); }, };
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peer-agent/runtime-core",
3
- "version": "0.1.0-rc.31",
3
+ "version": "0.1.0-rc.34",
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.31"
41
+ "@peer-agent/protocol": "0.1.0-rc.34"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "^5.9.3"