@lapis-notes/app-profile 0.0.0-stage → 0.2.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/LICENSE.md +4 -0
  3. package/README.md +23 -2
  4. package/dist/index.d.ts +34 -0
  5. package/dist/index.js +43 -0
  6. package/dist/local-plugin-distribution.d.ts +3 -0
  7. package/dist/local-plugin-distribution.js +102 -0
  8. package/dist/native-chat/NativeChat.svelte +114 -0
  9. package/dist/native-chat/NativeChat.svelte.d.ts +15 -0
  10. package/dist/native-chat/conversation.d.ts +10 -0
  11. package/dist/native-chat/conversation.js +161 -0
  12. package/dist/native-chat/history.svelte.d.ts +14 -0
  13. package/dist/native-chat/history.svelte.js +18 -0
  14. package/dist/native-chat/index.d.ts +6 -0
  15. package/dist/native-chat/index.js +4 -0
  16. package/dist/native-chat/metadata.d.ts +14 -0
  17. package/dist/native-chat/metadata.js +74 -0
  18. package/dist/native-chat/navigation.d.ts +13 -0
  19. package/dist/native-chat/navigation.js +55 -0
  20. package/dist/native-chat/observer.d.ts +28 -0
  21. package/dist/native-chat/observer.js +343 -0
  22. package/dist/native-chat/participant-capture.d.ts +7 -0
  23. package/dist/native-chat/participant-capture.js +108 -0
  24. package/dist/native-chat/participant-observer.d.ts +24 -0
  25. package/dist/native-chat/participant-observer.js +125 -0
  26. package/dist/native-chat/participant-projection.d.ts +9 -0
  27. package/dist/native-chat/participant-projection.js +116 -0
  28. package/dist/native-chat/plugin.d.ts +5 -0
  29. package/dist/native-chat/plugin.js +66 -0
  30. package/dist/native-chat/queue-capture.d.ts +57 -0
  31. package/dist/native-chat/queue-capture.js +41 -0
  32. package/dist/native-chat/queue-observer.d.ts +16 -0
  33. package/dist/native-chat/queue-observer.js +190 -0
  34. package/dist/native-chat/queue-receipt.d.ts +3 -0
  35. package/dist/native-chat/queue-receipt.js +36 -0
  36. package/dist/native-chat/send.d.ts +11 -0
  37. package/dist/native-chat/send.js +42 -0
  38. package/dist/native-chat/session.d.ts +32 -0
  39. package/dist/native-chat/session.js +82 -0
  40. package/dist/native-chat/view.d.ts +20 -0
  41. package/dist/native-chat/view.js +90 -0
  42. package/dist/native-repository.d.ts +7 -0
  43. package/dist/native-repository.js +26 -0
  44. package/dist/plugin-browser.d.ts +3 -0
  45. package/dist/plugin-browser.js +55 -0
  46. package/dist/plugin-host-build.d.ts +12 -0
  47. package/dist/plugin-host-build.js +88 -0
  48. package/dist/workspace-session/NotesWorkspaceSession.svelte +266 -0
  49. package/dist/workspace-session/NotesWorkspaceSession.svelte.d.ts +8 -0
  50. package/dist/workspace-session/application.d.ts +9 -0
  51. package/dist/workspace-session/application.js +47 -0
  52. package/dist/workspace-session/database-status.d.ts +2 -0
  53. package/dist/workspace-session/database-status.js +37 -0
  54. package/dist/workspace-session/index.d.ts +2 -0
  55. package/dist/workspace-session/index.js +1 -0
  56. package/dist/workspace-session/leave.d.ts +9 -0
  57. package/dist/workspace-session/leave.js +25 -0
  58. package/dist/workspace-session/lifecycle.d.ts +14 -0
  59. package/dist/workspace-session/lifecycle.js +83 -0
  60. package/dist/workspace-session/native-chat-registration.d.ts +3 -0
  61. package/dist/workspace-session/native-chat-registration.js +10 -0
  62. package/dist/workspace-session/startup-plugin-retirement.d.ts +3 -0
  63. package/dist/workspace-session/startup-plugin-retirement.js +8 -0
  64. package/dist/workspace-session/types.d.ts +33 -0
  65. package/dist/workspace-session/types.js +1 -0
  66. package/package.json +99 -3
@@ -0,0 +1,11 @@
1
+ import type { AgentAction } from "@lapismd/agent-ui/core";
2
+ import type { TurnTarget } from "@lapis-notes/api/native-turns";
3
+ import type { NativeChatSession } from "./session";
4
+ export declare function refused(error: unknown): {
5
+ status: "refused";
6
+ reason: string;
7
+ } | {
8
+ status: "unknown";
9
+ reason?: undefined;
10
+ };
11
+ export declare function nativeSendTarget(session: NativeChatSession, action: AgentAction, fallback: string): Promise<TurnTarget>;
@@ -0,0 +1,42 @@
1
+ export function refused(error) {
2
+ const code = error.code;
3
+ return [
4
+ "INVALID_REQUEST",
5
+ "UNSUPPORTED",
6
+ "BUSY",
7
+ "PERMISSION_DENIED",
8
+ "EINVAL",
9
+ "ENOTSUP",
10
+ "EBUSY",
11
+ "EACCES",
12
+ ].includes(code ?? "")
13
+ ? {
14
+ status: "refused",
15
+ reason: error instanceof Error
16
+ ? error.message
17
+ : "The native request was refused",
18
+ }
19
+ : { status: "unknown" };
20
+ }
21
+ export async function nativeSendTarget(session, action, fallback) {
22
+ const submission = action.submission, recipients = action.recipients ?? [fallback];
23
+ if (!submission?.text.trim() ||
24
+ submission.id !== action.id ||
25
+ submission.comments.length ||
26
+ action.attachments?.length ||
27
+ recipients.length !== 1 ||
28
+ !recipients[0])
29
+ throw Object.assign(new Error("Native chat currently accepts one authored text prompt for one agent"), { code: "UNSUPPORTED" });
30
+ const targets = await session.call({ kind: "targets" }).catch(() => null);
31
+ const target = targets?.kind === "targets"
32
+ ? targets.targets.find((row) => row.id === recipients[0])
33
+ : undefined;
34
+ const mode = target?.default_mode;
35
+ if (typeof mode !== "string" ||
36
+ !mode.length ||
37
+ mode.trim() !== mode ||
38
+ new TextEncoder().encode(mode).length > 256 ||
39
+ /[\u0000-\u001f\u007f-\u009f]/u.test(mode))
40
+ throw Object.assign(new Error("The native agent's declared mode is unavailable"), { code: "UNSUPPORTED" });
41
+ return target;
42
+ }
@@ -0,0 +1,32 @@
1
+ import type { NativeTurnService, NativeQueueService, QueueCommand, QueueValue, TurnCommand, TurnValue, NativeParticipantService, ParticipantCommand, ParticipantValue } from "@lapis-notes/api/native-turns";
2
+ import type { NativeQueueSelection } from "./queue-capture";
3
+ export interface NativeChatQueue extends NativeQueueService {
4
+ selection(targetId: string): Promise<NativeQueueSelection | undefined>;
5
+ }
6
+ export type NativeChatLeave = "detach" | "close" | "disable";
7
+ export interface NativeChatHost extends NativeTurnService {
8
+ queue?: NativeChatQueue;
9
+ participants?: NativeParticipantService;
10
+ allowed(): boolean;
11
+ activate(): Promise<void>;
12
+ leave(reason: NativeChatLeave): Promise<void>;
13
+ readText(path: string): Promise<string>;
14
+ report?(error: Error): void;
15
+ }
16
+ /** Feature lifetime only. It never owns a transcript writer or accepted job. */
17
+ export declare class NativeChatSession {
18
+ #private;
19
+ readonly host: NativeChatHost;
20
+ constructor(host: NativeChatHost);
21
+ private live;
22
+ private open;
23
+ call(command: TurnCommand): Promise<TurnValue>;
24
+ get hasQueue(): boolean;
25
+ get hasParticipants(): boolean;
26
+ participant(command: ParticipantCommand): Promise<ParticipantValue>;
27
+ queue(command: QueueCommand): Promise<QueueValue>;
28
+ queueSelection(targetId: string): Promise<NativeQueueSelection | undefined>;
29
+ readText(path: string): Promise<string>;
30
+ report(error: Error): void;
31
+ close(reason: NativeChatLeave): Promise<void>;
32
+ }
@@ -0,0 +1,82 @@
1
+ /** Feature lifetime only. It never owns a transcript writer or accepted job. */
2
+ export class NativeChatSession {
3
+ host;
4
+ #opening;
5
+ #closing;
6
+ #fenced = false;
7
+ constructor(host) {
8
+ this.host = host;
9
+ }
10
+ live() {
11
+ if (this.#fenced)
12
+ throw new Error("Native chat is closed");
13
+ if (!this.host.allowed())
14
+ throw new Error("AI requires a trusted workspace");
15
+ }
16
+ open() {
17
+ this.live();
18
+ return (this.#opening ??= (async () => {
19
+ await this.host.activate();
20
+ this.live();
21
+ const value = await this.host.call({
22
+ kind: "set_enabled",
23
+ enabled: true,
24
+ });
25
+ if (value.kind !== "availability" || !value.enabled)
26
+ throw new Error("Native AI is unavailable");
27
+ this.live();
28
+ })().catch((error) => {
29
+ this.#opening = undefined;
30
+ throw error;
31
+ }));
32
+ }
33
+ async call(command) {
34
+ await this.open();
35
+ this.live();
36
+ return this.host.call(command);
37
+ }
38
+ get hasQueue() {
39
+ return !!this.host.queue;
40
+ }
41
+ get hasParticipants() {
42
+ return !!this.host.participants;
43
+ }
44
+ async participant(command) {
45
+ await this.open();
46
+ this.live();
47
+ if (!this.host.participants)
48
+ throw new Error("Native participants are unavailable");
49
+ const value = await this.host.participants.call(command);
50
+ this.live();
51
+ return value;
52
+ }
53
+ async queue(command) {
54
+ await this.open();
55
+ this.live();
56
+ if (!this.host.queue)
57
+ throw new Error("Native queue is unavailable");
58
+ return this.host.queue.call(command);
59
+ }
60
+ async queueSelection(targetId) {
61
+ await this.open();
62
+ this.live();
63
+ const selection = await this.host.queue?.selection(targetId);
64
+ this.live();
65
+ return selection;
66
+ }
67
+ async readText(path) {
68
+ this.live();
69
+ return this.host.readText(path);
70
+ }
71
+ report(error) {
72
+ this.host.report?.(error);
73
+ }
74
+ close(reason) {
75
+ this.#fenced = true;
76
+ // Native leave fences/drains independently of lost activation/send replies.
77
+ return (this.#closing ??= this.host.leave(reason).catch((error) => {
78
+ this.#closing = undefined;
79
+ throw error;
80
+ }));
81
+ }
82
+ }
@@ -0,0 +1,20 @@
1
+ import { View, type WorkspaceLeaf } from "@lapis-notes/api";
2
+ import type { NativeChatSession } from "./session";
3
+ import type { NativeChatNavigation } from "./navigation";
4
+ export declare class NativeChatView extends View {
5
+ #private;
6
+ readonly session: NativeChatSession;
7
+ readonly views: Set<NativeChatView>;
8
+ readonly type: string;
9
+ readonly navigation?: NativeChatNavigation | undefined;
10
+ constructor(leaf: WorkspaceLeaf, session: NativeChatSession, views: Set<NativeChatView>, type?: string, navigation?: NativeChatNavigation | undefined);
11
+ getViewType(): string;
12
+ getDisplayText(): string;
13
+ getIcon(): string;
14
+ protected onOpen(): Promise<void>;
15
+ protected onClose(): Promise<void>;
16
+ getState(): Record<string, unknown>;
17
+ onload(): void;
18
+ close(): Promise<void>;
19
+ onunload(): Promise<void>;
20
+ }
@@ -0,0 +1,90 @@
1
+ import { View } from "@lapis-notes/api";
2
+ import { mount, unmount } from "svelte";
3
+ import NativeChat from "./NativeChat.svelte";
4
+ export class NativeChatView extends View {
5
+ session;
6
+ views;
7
+ type;
8
+ navigation;
9
+ #component;
10
+ #selection;
11
+ #scope;
12
+ #disposed = new Set();
13
+ constructor(leaf, session, views, type = "ai", navigation) {
14
+ super(leaf);
15
+ this.session = session;
16
+ this.views = views;
17
+ this.type = type;
18
+ this.navigation = navigation;
19
+ }
20
+ getViewType() {
21
+ return this.type;
22
+ }
23
+ getDisplayText() {
24
+ return this.type === "ai" ? "AI" : "AI conversations";
25
+ }
26
+ getIcon() {
27
+ return this.type === "ai" ? "sparkles" : "history";
28
+ }
29
+ async onOpen() { }
30
+ async onClose() {
31
+ await this.close();
32
+ }
33
+ getState() {
34
+ const state = super.getState();
35
+ const selection = this.#selection ??
36
+ (typeof state.conversationId === "string"
37
+ ? state.conversationId
38
+ : undefined);
39
+ return {
40
+ ...state,
41
+ scopeDir: this.#scope ??
42
+ (typeof state.scopeDir === "string" ? state.scopeDir : ""),
43
+ conversationId: selection?.startsWith("draft:") ? undefined : selection,
44
+ };
45
+ }
46
+ onload() {
47
+ this.views.add(this);
48
+ const state = super.getState();
49
+ this.#scope = typeof state.scopeDir === "string" ? state.scopeDir : "";
50
+ this.#component = mount(NativeChat, {
51
+ target: this.containerEl,
52
+ props: {
53
+ session: this.session,
54
+ initialSession: typeof state.conversationId === "string"
55
+ ? state.conversationId
56
+ : undefined,
57
+ screen: this.type === "ai" ? "chat" : "history",
58
+ initialScope: this.#scope,
59
+ onHistory: this.navigation
60
+ ? () => this.navigation.history()
61
+ : undefined,
62
+ onConversation: this.navigation
63
+ ? (location) => this.navigation.chat(location)
64
+ : undefined,
65
+ onSelection: (id, scope) => {
66
+ this.#selection = id;
67
+ this.#scope = scope;
68
+ this.app.workspace.requestSaveLayout();
69
+ },
70
+ onDispose: (pending) => {
71
+ this.#disposed.add(pending);
72
+ void pending
73
+ .finally(() => this.#disposed.delete(pending))
74
+ .catch(() => { });
75
+ },
76
+ },
77
+ });
78
+ }
79
+ async close() {
80
+ const component = this.#component;
81
+ this.#component = undefined;
82
+ if (component)
83
+ await unmount(component);
84
+ await Promise.all([...this.#disposed]);
85
+ this.views.delete(this);
86
+ }
87
+ async onunload() {
88
+ await this.close();
89
+ }
90
+ }
@@ -0,0 +1,7 @@
1
+ import type { App } from "@lapis-notes/api";
2
+ import { type RepositoryFactory } from "@lapis-notes/history/repository";
3
+ export type { RepositoryFactory } from "@lapis-notes/history/repository";
4
+ /** Optional native profile; the ordinary four-plugin profile stays unchanged. */
5
+ export declare function registerNativeRepository(app: App, factory: RepositoryFactory): () => void;
6
+ export declare function prepareNativeRepositoryLeave(app: App): Promise<void>;
7
+ export declare function resumeNativeRepositoryLeave(app: App): Promise<void>;
@@ -0,0 +1,26 @@
1
+ import { bindRepository, RepositoryPlugin, prepareRepositoryLeave, } from "@lapis-notes/history/repository";
2
+ /** Optional native profile; the ordinary four-plugin profile stays unchanged. */
3
+ export function registerNativeRepository(app, factory) {
4
+ const release = bindRepository(app, factory);
5
+ try {
6
+ app.plugins.registerStaticPlugins([
7
+ { plugin: RepositoryPlugin, required: false, enabledByDefault: true },
8
+ ]);
9
+ return release;
10
+ }
11
+ catch (error) {
12
+ release();
13
+ throw error;
14
+ }
15
+ }
16
+ export async function prepareNativeRepositoryLeave(app) {
17
+ prepareRepositoryLeave(app, "reload");
18
+ const plugin = app.plugins.plugins.get("repository-history");
19
+ if (plugin instanceof RepositoryPlugin)
20
+ await plugin.backup?.prepareLeave();
21
+ }
22
+ export async function resumeNativeRepositoryLeave(app) {
23
+ const plugin = app.plugins.plugins.get("repository-history");
24
+ if (plugin instanceof RepositoryPlugin)
25
+ await plugin.backup?.resumeLeave();
26
+ }
@@ -0,0 +1,3 @@
1
+ import type { App } from "@lapis-notes/api";
2
+ /** Register the default app browser before restoring persisted workspace leaves. */
3
+ export declare function registerNotesPluginBrowser(app: App): () => void;
@@ -0,0 +1,55 @@
1
+ import { getWorkspaceHostBinding } from "@lapis-notes/api/workspace-host";
2
+ import { openPluginBrowser, PluginsView } from "@lapis-notes/workspace";
3
+ /** Register the default app browser before restoring persisted workspace leaves. */
4
+ export function registerNotesPluginBrowser(app) {
5
+ app.workspace.registerView("plugins", (leaf) => new PluginsView(leaf));
6
+ let pending = null;
7
+ let disposed = false;
8
+ const removeUrlHandler = app.urls.registerProtocolHandler("plugins", async (params) => {
9
+ if (disposed || !/^[a-z0-9][a-z0-9-]{0,62}$/u.test(params.id ?? ""))
10
+ return;
11
+ const version = params.version;
12
+ if (version !== undefined &&
13
+ !/^[A-Za-z0-9][A-Za-z0-9.+-]{0,127}$/u.test(version))
14
+ return;
15
+ await openPluginBrowser(app, params.id, () => !disposed, version);
16
+ if (!disposed)
17
+ getWorkspaceHostBinding(app.workspace).controller.settings.close();
18
+ });
19
+ const open = () => {
20
+ if (disposed)
21
+ return Promise.resolve();
22
+ if (pending)
23
+ return pending;
24
+ pending = openPluginBrowser(app, undefined, () => !disposed).finally(() => {
25
+ pending = null;
26
+ });
27
+ return pending;
28
+ };
29
+ const removeRibbon = app.workspace.leftRibbon.addItem({
30
+ id: "app:plugins",
31
+ title: "Plugins",
32
+ icon: "package",
33
+ hidden: false,
34
+ callback: () => void open(),
35
+ });
36
+ app.commands.registerCommand({
37
+ id: "app:open-plugins",
38
+ name: "Open Plugins",
39
+ icon: "package",
40
+ callback: () => void open(),
41
+ });
42
+ return () => {
43
+ if (disposed)
44
+ return;
45
+ disposed = true;
46
+ removeUrlHandler();
47
+ for (const leaf of app.workspace.getLeavesOfType("plugins")) {
48
+ if (leaf.view instanceof PluginsView)
49
+ leaf.view.unload();
50
+ }
51
+ removeRibbon();
52
+ app.commands.unregisterCommand("app:open-plugins");
53
+ app.workspace.unregisterView("plugins");
54
+ };
55
+ }
@@ -0,0 +1,12 @@
1
+ import type { Plugin } from "vite";
2
+ export declare const rendererPluginImportMap: {
3
+ imports: {
4
+ [k: string]: string;
5
+ };
6
+ };
7
+ export declare const rendererPluginHostInputs: {
8
+ [k: string]: string;
9
+ };
10
+ export declare function resolvePluginHostModule(id: string): string | undefined;
11
+ export declare function loadPluginHostModule(id: string): string | undefined;
12
+ export declare function pluginHostImportMapPlugin(): Plugin;
@@ -0,0 +1,88 @@
1
+ import { pluginHostModuleRegistry } from "@lapis-notes/api/plugin-host-modules";
2
+ const registry = pluginHostModuleRegistry;
3
+ const editorRuntimeSpecifiers = new Set([
4
+ "@codemirror/autocomplete",
5
+ "@codemirror/commands",
6
+ "@codemirror/language",
7
+ "@codemirror/lang-markdown",
8
+ "@codemirror/lint",
9
+ "@codemirror/search",
10
+ "@codemirror/state",
11
+ "@codemirror/view",
12
+ "@lezer/common",
13
+ "@lezer/highlight",
14
+ "@lezer/lr",
15
+ ]);
16
+ const modules = Object.entries(registry).filter(([specifier, entry]) => entry.public &&
17
+ entry.platforms.includes("web") &&
18
+ (entry.packageName === "@lapis-notes/api" ||
19
+ entry.packageName === "@lapismd/design-core" ||
20
+ entry.packageName === "@lapis-notes/ui" ||
21
+ specifier === "@lapis-notes/markdown" ||
22
+ specifier === "@lapis-notes/markdown/embed" ||
23
+ editorRuntimeSpecifiers.has(specifier) ||
24
+ entry.source === "renderer-abi"));
25
+ const names = new Map(modules.map(([specifier, entry]) => [
26
+ specifier,
27
+ specifier === "@lapis-notes/api"
28
+ ? "plugin-host-api"
29
+ : specifier === "@lapis-notes/api/workspace-host"
30
+ ? "plugin-host-workspace-host"
31
+ : `plugin-host-${entry.moduleId}`,
32
+ ]));
33
+ const virtualIds = new Map(modules.map(([specifier]) => [specifier, `\0lapis-${names.get(specifier)}`]));
34
+ export const rendererPluginImportMap = {
35
+ imports: Object.fromEntries(modules.map(([specifier]) => [specifier, `/${names.get(specifier)}.js`])),
36
+ };
37
+ export const rendererPluginHostInputs = Object.fromEntries(modules.map(([specifier]) => [
38
+ names.get(specifier),
39
+ virtualIds.get(specifier),
40
+ ]));
41
+ export function resolvePluginHostModule(id) {
42
+ for (const [specifier, virtualId] of virtualIds) {
43
+ if (id === virtualId || id === rendererPluginImportMap.imports[specifier])
44
+ return virtualId;
45
+ }
46
+ return undefined;
47
+ }
48
+ export function loadPluginHostModule(id) {
49
+ for (const [specifier, entry] of modules) {
50
+ if (id !== virtualIds.get(specifier))
51
+ continue;
52
+ return (`export * from ${JSON.stringify(entry.entry)};\n` +
53
+ (entry.defaultExport
54
+ ? `export { default } from ${JSON.stringify(entry.entry)};\n`
55
+ : ""));
56
+ }
57
+ return undefined;
58
+ }
59
+ export function pluginHostImportMapPlugin() {
60
+ return {
61
+ name: "lapis-plugin-host-import-map",
62
+ resolveId(id) {
63
+ return resolvePluginHostModule(id);
64
+ },
65
+ load(id) {
66
+ return loadPluginHostModule(id);
67
+ },
68
+ configureServer(server) {
69
+ server.middlewares.use((request, _response, next) => {
70
+ const pathname = request.url?.split("?", 1)[0];
71
+ const id = pathname && resolvePluginHostModule(pathname);
72
+ if (id)
73
+ request.url = "/@id/__x00__" + id.slice(1);
74
+ next();
75
+ });
76
+ },
77
+ transformIndexHtml() {
78
+ return [
79
+ {
80
+ tag: "script",
81
+ attrs: { type: "importmap" },
82
+ children: JSON.stringify(rendererPluginImportMap),
83
+ injectTo: "head-prepend",
84
+ },
85
+ ];
86
+ },
87
+ };
88
+ }