@lapis-notes/app-profile 0.0.0-stage → 0.1.0
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/CHANGELOG.md +6 -0
- package/LICENSE.md +4 -0
- package/README.md +20 -2
- package/dist/index.d.ts +34 -0
- package/dist/index.js +43 -0
- package/dist/local-plugin-distribution.d.ts +3 -0
- package/dist/local-plugin-distribution.js +102 -0
- package/dist/native-chat/NativeChat.svelte +114 -0
- package/dist/native-chat/NativeChat.svelte.d.ts +15 -0
- package/dist/native-chat/conversation.d.ts +10 -0
- package/dist/native-chat/conversation.js +161 -0
- package/dist/native-chat/history.svelte.d.ts +14 -0
- package/dist/native-chat/history.svelte.js +18 -0
- package/dist/native-chat/index.d.ts +6 -0
- package/dist/native-chat/index.js +4 -0
- package/dist/native-chat/metadata.d.ts +14 -0
- package/dist/native-chat/metadata.js +74 -0
- package/dist/native-chat/navigation.d.ts +13 -0
- package/dist/native-chat/navigation.js +55 -0
- package/dist/native-chat/observer.d.ts +28 -0
- package/dist/native-chat/observer.js +343 -0
- package/dist/native-chat/participant-capture.d.ts +7 -0
- package/dist/native-chat/participant-capture.js +108 -0
- package/dist/native-chat/participant-observer.d.ts +24 -0
- package/dist/native-chat/participant-observer.js +125 -0
- package/dist/native-chat/participant-projection.d.ts +9 -0
- package/dist/native-chat/participant-projection.js +116 -0
- package/dist/native-chat/plugin.d.ts +5 -0
- package/dist/native-chat/plugin.js +60 -0
- package/dist/native-chat/queue-capture.d.ts +57 -0
- package/dist/native-chat/queue-capture.js +41 -0
- package/dist/native-chat/queue-observer.d.ts +16 -0
- package/dist/native-chat/queue-observer.js +190 -0
- package/dist/native-chat/queue-receipt.d.ts +3 -0
- package/dist/native-chat/queue-receipt.js +36 -0
- package/dist/native-chat/send.d.ts +11 -0
- package/dist/native-chat/send.js +42 -0
- package/dist/native-chat/session.d.ts +32 -0
- package/dist/native-chat/session.js +82 -0
- package/dist/native-chat/view.d.ts +20 -0
- package/dist/native-chat/view.js +90 -0
- package/dist/native-repository.d.ts +7 -0
- package/dist/native-repository.js +26 -0
- package/dist/plugin-browser.d.ts +3 -0
- package/dist/plugin-browser.js +55 -0
- package/dist/plugin-host-build.d.ts +12 -0
- package/dist/plugin-host-build.js +88 -0
- package/dist/workspace-session/NotesWorkspaceSession.svelte +261 -0
- package/dist/workspace-session/NotesWorkspaceSession.svelte.d.ts +8 -0
- package/dist/workspace-session/application.d.ts +9 -0
- package/dist/workspace-session/application.js +47 -0
- package/dist/workspace-session/database-status.d.ts +2 -0
- package/dist/workspace-session/database-status.js +37 -0
- package/dist/workspace-session/index.d.ts +2 -0
- package/dist/workspace-session/index.js +1 -0
- package/dist/workspace-session/leave.d.ts +9 -0
- package/dist/workspace-session/leave.js +25 -0
- package/dist/workspace-session/lifecycle.d.ts +14 -0
- package/dist/workspace-session/lifecycle.js +83 -0
- package/dist/workspace-session/types.d.ts +33 -0
- package/dist/workspace-session/types.js +1 -0
- package/package.json +98 -3
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { captureNativeParticipants, canonicalParticipant, participantAgents, } from "./participant-capture";
|
|
2
|
+
import { refused } from "./send";
|
|
3
|
+
const running = (message) => message.deliveries.some((d) => d.state === "pending" || d.state === "started");
|
|
4
|
+
/** View-local correlation only. There is no writer, dispatch loop or retry here. */
|
|
5
|
+
export class NativeChatParticipantObserver {
|
|
6
|
+
session;
|
|
7
|
+
profiles = [];
|
|
8
|
+
#captures = new Map();
|
|
9
|
+
#messages = new Map();
|
|
10
|
+
constructor(session) {
|
|
11
|
+
this.session = session;
|
|
12
|
+
}
|
|
13
|
+
get declared() {
|
|
14
|
+
return !!this.profiles.length;
|
|
15
|
+
}
|
|
16
|
+
get uncertain() {
|
|
17
|
+
return [...this.#captures.values()].some((capture) => !capture.acknowledged);
|
|
18
|
+
}
|
|
19
|
+
has(head) {
|
|
20
|
+
return !!head && this.#messages.has(head.conversation_id);
|
|
21
|
+
}
|
|
22
|
+
async catalog() {
|
|
23
|
+
if (!this.session.hasParticipants) {
|
|
24
|
+
this.profiles = [];
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
const value = await this.session.participant({ kind: "profiles" });
|
|
28
|
+
if (value.kind !== "profiles")
|
|
29
|
+
throw new Error("Native participant profiles are unavailable");
|
|
30
|
+
const agents = participantAgents(value.profiles);
|
|
31
|
+
this.profiles = structuredClone(value.profiles);
|
|
32
|
+
return agents;
|
|
33
|
+
}
|
|
34
|
+
async read(head) {
|
|
35
|
+
if (!head || !this.session.hasParticipants)
|
|
36
|
+
return;
|
|
37
|
+
const value = await this.session.participant({
|
|
38
|
+
kind: "for_turn",
|
|
39
|
+
request_id: head.request_id,
|
|
40
|
+
});
|
|
41
|
+
if (value.kind !== "message")
|
|
42
|
+
throw new Error("Native participant receipt is unavailable");
|
|
43
|
+
if (!value.message) {
|
|
44
|
+
this.#messages.delete(head.conversation_id);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const message = value.message;
|
|
48
|
+
if (message.conversation_id !== head.conversation_id ||
|
|
49
|
+
!message.deliveries.some((d) => d.request_id === head.request_id))
|
|
50
|
+
throw new Error("Native participant receipt belongs to another request");
|
|
51
|
+
this.#messages.set(head.conversation_id, message);
|
|
52
|
+
return message;
|
|
53
|
+
}
|
|
54
|
+
busy(head) {
|
|
55
|
+
const message = head && this.#messages.get(head.conversation_id);
|
|
56
|
+
return !!message && running(message);
|
|
57
|
+
}
|
|
58
|
+
async stop(head) {
|
|
59
|
+
const message = this.#messages.get(head.conversation_id);
|
|
60
|
+
if (!message || !running(message))
|
|
61
|
+
return false;
|
|
62
|
+
const value = await this.session.participant({
|
|
63
|
+
kind: "stop",
|
|
64
|
+
acceptance_id: message.acceptance_id,
|
|
65
|
+
});
|
|
66
|
+
if (value.kind !== "stop_requested")
|
|
67
|
+
throw new Error("Native participant Stop acknowledgement is unavailable");
|
|
68
|
+
return value.accepted;
|
|
69
|
+
}
|
|
70
|
+
async accept(action, scope, fallback, head) {
|
|
71
|
+
const fingerprint = canonicalParticipant(action);
|
|
72
|
+
const previous = this.#captures.get(action.id);
|
|
73
|
+
if (previous) {
|
|
74
|
+
if (fingerprint !== previous.fingerprint)
|
|
75
|
+
throw Object.assign(new Error("Participant identity was reused for another submission"), { code: "INVALID_REQUEST" });
|
|
76
|
+
const retained = await this.status(action.id);
|
|
77
|
+
if (retained)
|
|
78
|
+
return retained;
|
|
79
|
+
throw new Error("The original participant acceptance remains uncertain; inspect it without replaying");
|
|
80
|
+
}
|
|
81
|
+
if (this.uncertain)
|
|
82
|
+
throw Object.assign(new Error("Inspect outstanding participant actions first"), { code: "BUSY" });
|
|
83
|
+
if (this.#captures.size >= 128)
|
|
84
|
+
this.#captures.delete(this.#captures.keys().next().value);
|
|
85
|
+
const input = captureNativeParticipants(action, this.profiles, scope, fallback, head);
|
|
86
|
+
this.#captures.set(action.id, { fingerprint, input });
|
|
87
|
+
let value;
|
|
88
|
+
try {
|
|
89
|
+
value = await this.session.participant({ kind: "accept", input });
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (refused(error).status === "refused")
|
|
93
|
+
this.#captures.delete(action.id);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
return this.#accepted(action.id, input, value);
|
|
97
|
+
}
|
|
98
|
+
#accepted(actionId, input, value) {
|
|
99
|
+
if (value.kind !== "message" ||
|
|
100
|
+
!value.message ||
|
|
101
|
+
value.message.acceptance_id !== input.acceptance_id ||
|
|
102
|
+
canonicalParticipant(value.message.input) !== canonicalParticipant(input))
|
|
103
|
+
throw new Error("Native participant acknowledgement is unavailable");
|
|
104
|
+
this.#messages.set(value.message.conversation_id, value.message);
|
|
105
|
+
const capture = this.#captures.get(actionId);
|
|
106
|
+
if (capture)
|
|
107
|
+
capture.acknowledged = true;
|
|
108
|
+
return value.message.conversation_id;
|
|
109
|
+
}
|
|
110
|
+
pending(id) {
|
|
111
|
+
return this.#captures.has(id);
|
|
112
|
+
}
|
|
113
|
+
async status(id) {
|
|
114
|
+
const captured = this.#captures.get(id);
|
|
115
|
+
if (!captured)
|
|
116
|
+
return;
|
|
117
|
+
const value = await this.session.participant({
|
|
118
|
+
kind: "read",
|
|
119
|
+
acceptance_id: captured.input.acceptance_id,
|
|
120
|
+
});
|
|
121
|
+
if (value.kind === "message" && !value.message)
|
|
122
|
+
return;
|
|
123
|
+
return this.#accepted(id, captured.input, value);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { TranscriptItem } from "@lapismd/design-core/chat";
|
|
2
|
+
import type { ParticipantMessage } from "@lapis-notes/api/native-turns";
|
|
3
|
+
type Row = Record<string, unknown>;
|
|
4
|
+
/** Labels come from immutable binding records, never current catalog display text. */
|
|
5
|
+
export declare function participantLabels(bindings: readonly Row[], message?: ParticipantMessage): Map<string, string>;
|
|
6
|
+
/** Current native outcomes supersede stale portable markers without repairing files. */
|
|
7
|
+
export declare function participantEntries(values: Row[], message?: ParticipantMessage): Row[];
|
|
8
|
+
export declare function participantReceiptItem(value: Row, revision: (text: string) => string): TranscriptItem;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
function object(value) {
|
|
2
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
3
|
+
? value
|
|
4
|
+
: undefined;
|
|
5
|
+
}
|
|
6
|
+
function recipient(value) {
|
|
7
|
+
const row = object(value);
|
|
8
|
+
if (!row ||
|
|
9
|
+
typeof row.id !== "string" ||
|
|
10
|
+
typeof row.label !== "string" ||
|
|
11
|
+
typeof row.token !== "string")
|
|
12
|
+
throw new Error("The saved native participant attribution is unavailable");
|
|
13
|
+
return { id: row.id, label: row.label, token: row.token };
|
|
14
|
+
}
|
|
15
|
+
/** Labels come from immutable binding records, never current catalog display text. */
|
|
16
|
+
export function participantLabels(bindings, message) {
|
|
17
|
+
const labels = new Map();
|
|
18
|
+
const captured = object(message?.input.dispatch);
|
|
19
|
+
for (const [index, delivery] of (message?.deliveries ?? []).entries()) {
|
|
20
|
+
const entry = Array.isArray(captured?.deliveries)
|
|
21
|
+
? object(captured.deliveries[index])
|
|
22
|
+
: undefined;
|
|
23
|
+
labels.set(delivery.agent_binding_id, recipient(entry?.recipient).label);
|
|
24
|
+
}
|
|
25
|
+
const seen = new Set();
|
|
26
|
+
for (const binding of bindings) {
|
|
27
|
+
if (binding.type !== "binding.created" || binding.participant === undefined)
|
|
28
|
+
continue;
|
|
29
|
+
if (binding.schemaVersion !== 3 ||
|
|
30
|
+
typeof binding.id !== "string" ||
|
|
31
|
+
seen.has(binding.id))
|
|
32
|
+
throw new Error("The saved native participant binding is ambiguous");
|
|
33
|
+
seen.add(binding.id);
|
|
34
|
+
const label = recipient(binding.participant).label;
|
|
35
|
+
if (labels.has(binding.id) && labels.get(binding.id) !== label)
|
|
36
|
+
throw new Error("The saved native participant binding changed its captured label");
|
|
37
|
+
labels.set(binding.id, label);
|
|
38
|
+
}
|
|
39
|
+
return labels;
|
|
40
|
+
}
|
|
41
|
+
/** Current native outcomes supersede stale portable markers without repairing files. */
|
|
42
|
+
export function participantEntries(values, message) {
|
|
43
|
+
if (!message)
|
|
44
|
+
return latest(values);
|
|
45
|
+
const dispatch = object(message.input.dispatch), user = object(message.input.user);
|
|
46
|
+
if (!dispatch || !user || !Array.isArray(dispatch.deliveries))
|
|
47
|
+
throw new Error("The native participant capture is unavailable");
|
|
48
|
+
const result = values.filter((v) => v.type !== "agent.delivery" || v.dispatchId !== dispatch.id);
|
|
49
|
+
if (!result.some((v) => v.id === user.id))
|
|
50
|
+
result.unshift(user);
|
|
51
|
+
for (const [index, delivery] of message.deliveries.entries()) {
|
|
52
|
+
const captured = object(dispatch.deliveries[index]);
|
|
53
|
+
if (!captured ||
|
|
54
|
+
captured.id !== delivery.delivery_id ||
|
|
55
|
+
message.input.turns[index]?.request_id !== delivery.request_id)
|
|
56
|
+
throw new Error("The native participant delivery association is unavailable");
|
|
57
|
+
result.push({
|
|
58
|
+
schemaVersion: 3,
|
|
59
|
+
type: "agent.delivery",
|
|
60
|
+
id: `delivery:${delivery.delivery_id}:${delivery.state}`,
|
|
61
|
+
parentId: user.id,
|
|
62
|
+
dispatchId: dispatch.id,
|
|
63
|
+
delivery: {
|
|
64
|
+
...captured,
|
|
65
|
+
state: delivery.state,
|
|
66
|
+
agentBindingId: delivery.agent_binding_id,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return latest(result);
|
|
71
|
+
}
|
|
72
|
+
function latest(values) {
|
|
73
|
+
const last = new Map();
|
|
74
|
+
const key = (value) => JSON.stringify([value.dispatchId, object(value.delivery)?.id]);
|
|
75
|
+
values.forEach((value, index) => {
|
|
76
|
+
if (value.type === "agent.delivery")
|
|
77
|
+
last.set(key(value), index);
|
|
78
|
+
});
|
|
79
|
+
return values.filter((value, index) => value.type !== "agent.delivery" || last.get(key(value)) === index);
|
|
80
|
+
}
|
|
81
|
+
export function participantReceiptItem(value, revision) {
|
|
82
|
+
const delivery = object(value.delivery), saved = recipient(delivery?.recipient);
|
|
83
|
+
const states = {
|
|
84
|
+
pending: "Waiting for native dispatch.",
|
|
85
|
+
started: "Running.",
|
|
86
|
+
completed: "Completed.",
|
|
87
|
+
rejected: "Did not complete.",
|
|
88
|
+
unknown: "Outcome uncertain; it will not be replayed automatically.",
|
|
89
|
+
not_sent: "Not sent; no automatic retry.",
|
|
90
|
+
cancelled: "Cancelled.",
|
|
91
|
+
};
|
|
92
|
+
const state = delivery?.state;
|
|
93
|
+
if (typeof value.id !== "string" ||
|
|
94
|
+
typeof state !== "string" ||
|
|
95
|
+
!Object.hasOwn(states, state))
|
|
96
|
+
throw new Error("The native participant outcome cannot be displayed");
|
|
97
|
+
const text = `${saved.label}: ${states[state]}`;
|
|
98
|
+
return {
|
|
99
|
+
id: value.id,
|
|
100
|
+
kind: "context",
|
|
101
|
+
label: `${saved.label} status`,
|
|
102
|
+
...(["unknown", "not_sent", "rejected", "cancelled"].includes(state)
|
|
103
|
+
? { notice: "warning" }
|
|
104
|
+
: {}),
|
|
105
|
+
blocks: [
|
|
106
|
+
{
|
|
107
|
+
id: `${value.id}:status`,
|
|
108
|
+
revision: revision(text),
|
|
109
|
+
kind: "markdown",
|
|
110
|
+
label: "Participant status",
|
|
111
|
+
text,
|
|
112
|
+
commentable: false,
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type App, type PluginConstructor } from "@lapis-notes/api";
|
|
2
|
+
import { type NativeChatHost, type NativeChatLeave } from "./session";
|
|
3
|
+
export declare function prepareNativeChatLeave(app: App, reason: NativeChatLeave): void;
|
|
4
|
+
/** Hosts inject authority; the same plugin/screen never instantiates renderer AI writers. */
|
|
5
|
+
export declare function createNativeChatPlugin(factory: (app: App) => NativeChatHost): PluginConstructor;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Plugin } from "@lapis-notes/api";
|
|
2
|
+
import { NativeChatSession, } from "./session";
|
|
3
|
+
import { NativeChatView } from "./view";
|
|
4
|
+
import { NativeChatNavigation } from "./navigation";
|
|
5
|
+
const modes = new WeakMap();
|
|
6
|
+
export function prepareNativeChatLeave(app, reason) {
|
|
7
|
+
modes.set(app, reason);
|
|
8
|
+
}
|
|
9
|
+
/** Hosts inject authority; the same plugin/screen never instantiates renderer AI writers. */
|
|
10
|
+
export function createNativeChatPlugin(factory) {
|
|
11
|
+
return class NativeChatPlugin extends Plugin {
|
|
12
|
+
session;
|
|
13
|
+
views = new Set();
|
|
14
|
+
navigation;
|
|
15
|
+
constructor(app) {
|
|
16
|
+
super(app, {
|
|
17
|
+
id: "ai",
|
|
18
|
+
name: "AI",
|
|
19
|
+
version: "0.1.0",
|
|
20
|
+
minAppVersion: "0.0.1",
|
|
21
|
+
author: "Lapis Notes",
|
|
22
|
+
description: "Chat with your configured agents.",
|
|
23
|
+
});
|
|
24
|
+
this.session = new NativeChatSession(factory(app));
|
|
25
|
+
this.navigation = new NativeChatNavigation(app);
|
|
26
|
+
}
|
|
27
|
+
async onload() {
|
|
28
|
+
for (const [type, title, icon] of [
|
|
29
|
+
["ai", "AI", "sparkles"],
|
|
30
|
+
["ai-conversation-history", "AI conversations", "history"],
|
|
31
|
+
]) {
|
|
32
|
+
const open = async () => {
|
|
33
|
+
if (type === "ai")
|
|
34
|
+
await this.navigation.chat();
|
|
35
|
+
else
|
|
36
|
+
await this.navigation.history();
|
|
37
|
+
};
|
|
38
|
+
this.registerSidebarView(type, (leaf) => new NativeChatView(leaf, this.session, this.views, type, this.navigation), { side: "right", title, icon }, {
|
|
39
|
+
kind: "command",
|
|
40
|
+
command: {
|
|
41
|
+
id: type === "ai" ? "open-chat" : "open-history",
|
|
42
|
+
name: type === "ai" ? "Open Chat" : "Open History",
|
|
43
|
+
callback: () => {
|
|
44
|
+
void open().catch((error) => this.session.report(error));
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
if (type === "ai")
|
|
49
|
+
this.addRibbonIcon(icon, "Open Chat", () => {
|
|
50
|
+
void open().catch((error) => this.session.report(error));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async onunload() {
|
|
55
|
+
await Promise.all([...this.views].map((view) => view.close()));
|
|
56
|
+
await this.session.close(modes.get(this.app) ?? "disable");
|
|
57
|
+
modes.delete(this.app);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { QueueEntry } from "@lapis-notes/api/native-turns";
|
|
2
|
+
/** Retained attribution and choices, never executable or provider authority. */
|
|
3
|
+
export type NativeQueueSelection = import("@lapis-notes/api/native-turns").QueueSelection;
|
|
4
|
+
/** Adapted from frozen Desktop f276120b queue-capture; no queue engine lives here. */
|
|
5
|
+
export declare function captureNativeQueue(text: string, selection: NativeQueueSelection): {
|
|
6
|
+
version: number;
|
|
7
|
+
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
8
|
+
state: string;
|
|
9
|
+
user: {
|
|
10
|
+
schemaVersion: number;
|
|
11
|
+
id: string;
|
|
12
|
+
type: string;
|
|
13
|
+
role: string;
|
|
14
|
+
text: string;
|
|
15
|
+
recipients: import("@lapis-notes/api/native-turns").QueueRecipient[];
|
|
16
|
+
createdAt: string;
|
|
17
|
+
provenance: {
|
|
18
|
+
originClass: string;
|
|
19
|
+
sourceKind: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
selection: {
|
|
23
|
+
attachments: never[];
|
|
24
|
+
thinking?: import("@lapis-notes/api/native-turns").QueueThinking | undefined;
|
|
25
|
+
targets: import("@lapis-notes/api/native-turns").QueueCaptureTarget[];
|
|
26
|
+
runtime: string;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
/** Presentation decoding only; Core validates every admitted retained capture. */
|
|
30
|
+
export declare function capturedQueue(entry: {
|
|
31
|
+
input: {
|
|
32
|
+
capture: QueueEntry["input"]["capture"];
|
|
33
|
+
};
|
|
34
|
+
}): {
|
|
35
|
+
version: number;
|
|
36
|
+
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
37
|
+
state: string;
|
|
38
|
+
user: {
|
|
39
|
+
schemaVersion: number;
|
|
40
|
+
id: string;
|
|
41
|
+
type: string;
|
|
42
|
+
role: string;
|
|
43
|
+
text: string;
|
|
44
|
+
recipients: import("@lapis-notes/api/native-turns").QueueRecipient[];
|
|
45
|
+
createdAt: string;
|
|
46
|
+
provenance: {
|
|
47
|
+
originClass: string;
|
|
48
|
+
sourceKind: string;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
selection: {
|
|
52
|
+
attachments: never[];
|
|
53
|
+
thinking?: import("@lapis-notes/api/native-turns").QueueThinking | undefined;
|
|
54
|
+
targets: import("@lapis-notes/api/native-turns").QueueCaptureTarget[];
|
|
55
|
+
runtime: string;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** Adapted from frozen Desktop f276120b queue-capture; no queue engine lives here. */
|
|
2
|
+
export function captureNativeQueue(text, selection) {
|
|
3
|
+
if (!text.trim() || text.trim().startsWith("/"))
|
|
4
|
+
throw new Error("Application commands and empty prompts cannot be queued");
|
|
5
|
+
if (selection.recipient.id !== selection.target.id)
|
|
6
|
+
throw new Error("The declared queued recipient does not match its selection");
|
|
7
|
+
return {
|
|
8
|
+
version: 1,
|
|
9
|
+
id: crypto.randomUUID(),
|
|
10
|
+
state: "queued",
|
|
11
|
+
user: {
|
|
12
|
+
schemaVersion: 3,
|
|
13
|
+
id: `queued-user-${crypto.randomUUID()}`,
|
|
14
|
+
type: "message",
|
|
15
|
+
role: "user",
|
|
16
|
+
text: text.trim(),
|
|
17
|
+
recipients: [structuredClone(selection.recipient)],
|
|
18
|
+
createdAt: new Date().toISOString(),
|
|
19
|
+
provenance: { originClass: "owner", sourceKind: "user-message" },
|
|
20
|
+
},
|
|
21
|
+
selection: {
|
|
22
|
+
targets: [structuredClone(selection.target)],
|
|
23
|
+
runtime: "acp",
|
|
24
|
+
...(selection.thinking ? { thinking: selection.thinking } : {}),
|
|
25
|
+
attachments: [],
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Presentation decoding only; Core validates every admitted retained capture. */
|
|
30
|
+
export function capturedQueue(entry) {
|
|
31
|
+
const capture = entry.input.capture;
|
|
32
|
+
if (capture?.version !== 1 ||
|
|
33
|
+
typeof capture.id !== "string" ||
|
|
34
|
+
typeof capture.user?.id !== "string" ||
|
|
35
|
+
typeof capture.user.text !== "string" ||
|
|
36
|
+
!Array.isArray(capture.user.recipients) ||
|
|
37
|
+
capture.user.recipients.length !== 1 ||
|
|
38
|
+
!capture.user.recipients.every((r) => typeof r.id === "string" && typeof r.label === "string"))
|
|
39
|
+
throw new Error("The saved native queue cannot be displayed");
|
|
40
|
+
return capture;
|
|
41
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AgentAction, AgentSnapshot } from "@lapismd/agent-ui/core";
|
|
2
|
+
import type { TurnSummary, TurnTarget } from "@lapis-notes/api/native-turns";
|
|
3
|
+
import type { NativeChatSession } from "./session";
|
|
4
|
+
/** Correlation and presentation only. Core alone persists and advances queues. */
|
|
5
|
+
export declare class NativeChatQueueObserver {
|
|
6
|
+
#private;
|
|
7
|
+
readonly session: NativeChatSession;
|
|
8
|
+
constructor(session: NativeChatSession);
|
|
9
|
+
read(head?: TurnSummary): Promise<Pick<AgentSnapshot, "queued" | "queueState">>;
|
|
10
|
+
has(head?: TurnSummary): boolean;
|
|
11
|
+
paused(head?: TurnSummary): boolean;
|
|
12
|
+
accept(action: AgentAction, head: TurnSummary, target: TurnTarget): Promise<boolean>;
|
|
13
|
+
control(action: AgentAction, head: TurnSummary): Promise<void>;
|
|
14
|
+
status(id: string): Promise<string | undefined>;
|
|
15
|
+
pending(id: string): boolean;
|
|
16
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { refused } from "./send";
|
|
2
|
+
import { captureNativeQueue, capturedQueue } from "./queue-capture";
|
|
3
|
+
/** Correlation and presentation only. Core alone persists and advances queues. */
|
|
4
|
+
export class NativeChatQueueObserver {
|
|
5
|
+
session;
|
|
6
|
+
#uncertain = new Map();
|
|
7
|
+
#known = new Map();
|
|
8
|
+
#queues = new Map();
|
|
9
|
+
constructor(session) {
|
|
10
|
+
this.session = session;
|
|
11
|
+
}
|
|
12
|
+
async read(head) {
|
|
13
|
+
if (!head || !this.session.hasQueue)
|
|
14
|
+
return { queued: [] };
|
|
15
|
+
const queue = await this.#read(head.conversation_id);
|
|
16
|
+
if (!queue)
|
|
17
|
+
return { queued: [] };
|
|
18
|
+
return {
|
|
19
|
+
queued: queue.entries
|
|
20
|
+
.filter((entry) => entry.phase === "pending" || entry.phase === "unknown")
|
|
21
|
+
.map((entry) => {
|
|
22
|
+
const capture = capturedQueue(entry);
|
|
23
|
+
return {
|
|
24
|
+
id: entry.input.acceptance_id,
|
|
25
|
+
submission: {
|
|
26
|
+
id: capture.user.id,
|
|
27
|
+
text: capture.user.text,
|
|
28
|
+
comments: [],
|
|
29
|
+
},
|
|
30
|
+
recipients: capture.user.recipients.map((recipient) => ({
|
|
31
|
+
id: recipient.id,
|
|
32
|
+
label: recipient.label,
|
|
33
|
+
state: entry.phase === "unknown"
|
|
34
|
+
? "unknown"
|
|
35
|
+
: "queued",
|
|
36
|
+
disabled: entry.phase !== "pending",
|
|
37
|
+
})),
|
|
38
|
+
};
|
|
39
|
+
}),
|
|
40
|
+
queueState: {
|
|
41
|
+
paused: queue.paused,
|
|
42
|
+
saving: false,
|
|
43
|
+
running: queue.entries.some((entry) => entry.phase === "started"),
|
|
44
|
+
resuming: false,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async #read(id) {
|
|
49
|
+
const value = await this.session.queue({
|
|
50
|
+
kind: "read",
|
|
51
|
+
conversation_id: id,
|
|
52
|
+
});
|
|
53
|
+
if (value.kind !== "queue")
|
|
54
|
+
throw new Error("Native queue is unavailable");
|
|
55
|
+
if (value.queue) {
|
|
56
|
+
if (value.queue.conversation_id !== id)
|
|
57
|
+
throw new Error("Native queue returned another conversation");
|
|
58
|
+
this.#queues.set(id, value.queue);
|
|
59
|
+
}
|
|
60
|
+
else
|
|
61
|
+
this.#queues.delete(id);
|
|
62
|
+
return value.queue;
|
|
63
|
+
}
|
|
64
|
+
has(head) {
|
|
65
|
+
return !!head && this.#queues.has(head.conversation_id);
|
|
66
|
+
}
|
|
67
|
+
paused(head) {
|
|
68
|
+
const queue = head && this.#queues.get(head.conversation_id);
|
|
69
|
+
return !!queue?.paused && queue.entries.some((e) => e.phase === "pending");
|
|
70
|
+
}
|
|
71
|
+
async accept(action, head, target) {
|
|
72
|
+
const original = this.#uncertain.get(action.id) ?? this.#known.get(action.id);
|
|
73
|
+
if (original) {
|
|
74
|
+
if (original.kind !== "accept" ||
|
|
75
|
+
original.input.conversation_id !== head.conversation_id ||
|
|
76
|
+
original.input.target_id !== target.id ||
|
|
77
|
+
capturedQueue({ input: original.input }).user.text !==
|
|
78
|
+
action.submission.text.trim())
|
|
79
|
+
throw Object.assign(new Error("Queue identity was reused for another submission"), { code: "INVALID_REQUEST" });
|
|
80
|
+
if (await this.status(action.id))
|
|
81
|
+
return true;
|
|
82
|
+
throw new Error("The original queue acceptance remains uncertain; inspect it without replaying");
|
|
83
|
+
}
|
|
84
|
+
const selection = await this.session.queueSelection(target.id);
|
|
85
|
+
if (!selection)
|
|
86
|
+
return false;
|
|
87
|
+
let capture;
|
|
88
|
+
try {
|
|
89
|
+
capture = captureNativeQueue(action.submission.text, {
|
|
90
|
+
...selection,
|
|
91
|
+
target: {
|
|
92
|
+
...selection.target,
|
|
93
|
+
executionMode: selection.target.executionMode ?? target.default_mode,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
throw Object.assign(error, { code: "UNSUPPORTED" });
|
|
99
|
+
}
|
|
100
|
+
await this.#call(action.id, {
|
|
101
|
+
kind: "accept",
|
|
102
|
+
input: {
|
|
103
|
+
acceptance_id: action.id,
|
|
104
|
+
conversation_id: head.conversation_id,
|
|
105
|
+
after_request_id: head.request_id,
|
|
106
|
+
scope_dir: head.scope_dir,
|
|
107
|
+
target_id: target.id,
|
|
108
|
+
capture,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
async control(action, head) {
|
|
114
|
+
const conversation_id = head.conversation_id;
|
|
115
|
+
const command = action.kind === "resume_queue"
|
|
116
|
+
? { kind: "resume", conversation_id, after_request_id: head.request_id }
|
|
117
|
+
: action.kind === "cancel_queued"
|
|
118
|
+
? {
|
|
119
|
+
kind: "cancel",
|
|
120
|
+
conversation_id,
|
|
121
|
+
acceptance_id: action.target ?? "",
|
|
122
|
+
}
|
|
123
|
+
: { kind: "stop", conversation_id };
|
|
124
|
+
await this.#call(action.id, command);
|
|
125
|
+
}
|
|
126
|
+
async #call(id, command) {
|
|
127
|
+
if (this.#uncertain.size >= 128)
|
|
128
|
+
throw Object.assign(new Error("Inspect outstanding queue actions first"), { code: "BUSY" });
|
|
129
|
+
this.#uncertain.set(id, command);
|
|
130
|
+
let value;
|
|
131
|
+
try {
|
|
132
|
+
value = await this.session.queue(command);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
if (refused(error).status === "refused")
|
|
136
|
+
this.#uncertain.delete(id);
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
if (value.kind !== "queue" || !value.queue)
|
|
140
|
+
throw new Error("Native queue acknowledgement is unavailable");
|
|
141
|
+
const expected = command.kind === "accept"
|
|
142
|
+
? command.input.conversation_id
|
|
143
|
+
: command.conversation_id;
|
|
144
|
+
if (value.queue.conversation_id !== expected)
|
|
145
|
+
throw new Error("Native queue acknowledged another conversation");
|
|
146
|
+
this.#queues.set(expected, value.queue);
|
|
147
|
+
this.#remember(id, command);
|
|
148
|
+
this.#uncertain.delete(id);
|
|
149
|
+
}
|
|
150
|
+
async status(id) {
|
|
151
|
+
const command = this.#uncertain.get(id) ?? this.#known.get(id);
|
|
152
|
+
if (!command)
|
|
153
|
+
return;
|
|
154
|
+
const conversation = command.kind === "accept"
|
|
155
|
+
? command.input.conversation_id
|
|
156
|
+
: command.conversation_id;
|
|
157
|
+
const queue = await this.#read(conversation);
|
|
158
|
+
if (!queue)
|
|
159
|
+
return;
|
|
160
|
+
const accepted = command.kind === "accept"
|
|
161
|
+
? queue.entries.some((e) => e.input.acceptance_id === command.input.acceptance_id &&
|
|
162
|
+
canonical(e.input) === canonical(command.input))
|
|
163
|
+
: command.kind === "cancel"
|
|
164
|
+
? queue.entries.some((e) => e.input.acceptance_id === command.acceptance_id &&
|
|
165
|
+
e.phase === "cancelled")
|
|
166
|
+
: command.kind === "stop"
|
|
167
|
+
? queue.paused
|
|
168
|
+
: !queue.paused;
|
|
169
|
+
if (!accepted)
|
|
170
|
+
return;
|
|
171
|
+
this.#uncertain.delete(id);
|
|
172
|
+
this.#remember(id, command);
|
|
173
|
+
return conversation;
|
|
174
|
+
}
|
|
175
|
+
#remember(id, command) {
|
|
176
|
+
this.#known.set(id, command);
|
|
177
|
+
if (this.#known.size > 128)
|
|
178
|
+
this.#known.delete(this.#known.keys().next().value);
|
|
179
|
+
}
|
|
180
|
+
pending(id) {
|
|
181
|
+
return this.#uncertain.has(id) || this.#known.has(id);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function canonical(value) {
|
|
185
|
+
return JSON.stringify(value, (_key, next) => next && typeof next === "object" && !Array.isArray(next)
|
|
186
|
+
? Object.fromEntries(Object.keys(next)
|
|
187
|
+
.sort()
|
|
188
|
+
.map((key) => [key, next[key]]))
|
|
189
|
+
: next);
|
|
190
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { TranscriptItem } from "@lapismd/design-core/chat";
|
|
2
|
+
/** Saved receipts are status data, never prompt instructions or retry requests. */
|
|
3
|
+
export declare function queueReceiptItem(value: Record<string, unknown>, revision: (text: string) => string): TranscriptItem;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { capturedQueue } from "./queue-capture";
|
|
2
|
+
/** Saved receipts are status data, never prompt instructions or retry requests. */
|
|
3
|
+
export function queueReceiptItem(value, revision) {
|
|
4
|
+
const labels = {
|
|
5
|
+
completed: "completed",
|
|
6
|
+
cancelled: "cancelled",
|
|
7
|
+
rejected: "rejected",
|
|
8
|
+
not_sent: "not sent",
|
|
9
|
+
unknown: "uncertain; it will not be replayed automatically",
|
|
10
|
+
};
|
|
11
|
+
const outcome = typeof value.outcome === "string" ? value.outcome : "";
|
|
12
|
+
const capture = capturedQueue({ input: { capture: value.queued } });
|
|
13
|
+
if (typeof value.id !== "string" ||
|
|
14
|
+
value.parentId !== capture.user.id ||
|
|
15
|
+
!labels[outcome])
|
|
16
|
+
throw new Error("The saved native queue receipt cannot be displayed");
|
|
17
|
+
const text = `Queued message ${labels[outcome]}.` +
|
|
18
|
+
(outcome === "completed" ? "" : `\n\n${capture.user.text}`);
|
|
19
|
+
return {
|
|
20
|
+
id: value.id,
|
|
21
|
+
kind: "context",
|
|
22
|
+
label: "Queue status",
|
|
23
|
+
...(outcome === "completed" ? {} : { notice: "warning" }),
|
|
24
|
+
createdAt: typeof value.createdAt === "string" ? value.createdAt : undefined,
|
|
25
|
+
blocks: [
|
|
26
|
+
{
|
|
27
|
+
id: `${value.id}:status`,
|
|
28
|
+
revision: revision(text),
|
|
29
|
+
kind: "markdown",
|
|
30
|
+
label: "Queue status",
|
|
31
|
+
text,
|
|
32
|
+
commentable: false,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
}
|