Back to Nappakura
SOURCE / PINNED RELEASE

Made of little things.

Nappakura

Release
a75ca0f8b596…
Author-recorded commit
4463126213fa…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-8db03001b62

Archive hash verified: b448d77b767ae754…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/agent-tools.ts
import { toolDefinitions } from './tool-definitions.js';
import type { Arguments } from './commands.js';
// Local experimental binding supplied by the host. No transport or shim in the app.
type Context = { signal: AbortSignal; progress: (text: string) => void };
type Binding = {
  profile: string;
  registerTool: (
    tool: unknown,
    execute: (args: Arguments, context: Context) => unknown,
  ) => Promise<{ unregister: () => Promise<void> }>;
  reset: () => Promise<void>;
  onExposureChanged: (fn: (enabled: boolean) => void) => () => void;
};
export function installAgentTools(
  run: (name: string, args: Arguments) => unknown,
  exportPDF: (args: Arguments, signal: AbortSignal) => Promise<unknown>,
  hasFiles: boolean,
  feedback: (text: string, error?: boolean) => void,
) {
  const api = (window as unknown as { napplet?: { webmcp?: Binding } }).napplet
    ?.webmcp;
  if (api?.profile !== 'soy.webmcp/1') return { reset: () => {} };
  let closed = false;
  const registrations: { unregister: () => Promise<void> }[] = [];
  const stop = api.onExposureChanged((enabled) =>
    feedback(
      enabled
        ? 'Agent access enabled by the shell. Tool edits appear here and can be undone.'
        : 'Agent access disabled. You can continue editing manually.',
    ),
  );
  void (async () => {
    for (const descriptor of toolDefinitions) {
      if (closed) break;
      if (descriptor.name === 'export_pdf' && !hasFiles) continue;
      const handle = await api.registerTool(
        { ...descriptor, outputSchema: { type: 'object' } },
        async (args, context) => {
          if (context.signal.aborted) throw new Error('Operation cancelled.');
          context.progress(
            `Working on ${descriptor.name.replaceAll('_', ' ')}.`,
          );
          if (descriptor.name === 'export_pdf')
            return exportPDF(args, context.signal);
          return run(descriptor.name, args);
        },
      );
      if (closed) await handle.unregister();
      else registrations.push(handle);
    }
  })().catch(() =>
    feedback(
      'Agent tools are unavailable in this shell session. The editor still works.',
      true,
    ),
  );
  window.addEventListener(
    'pagehide',
    () => {
      closed = true;
      stop();
      for (const handle of registrations)
        void handle.unregister().catch(() => {});
    },
    { once: true },
  );
  return {
    reset: () => {
      void api
        .reset()
        .catch(() =>
          feedback(
            'Agent access reset could not be confirmed. Close the player before opening another private project.',
            true,
          ),
        );
    },
  };
}