Skip to content
API reference

knapsack

Import
import * as api from "@fungi.computer/knapsack";
Kind Export Description
class FilesEditError Expected Files edit refusal, kept distinct from unexpected host failures.
class ComputerUnavailableError One Computer whose advertisement could not be acquired.
class McpServerUnavailableError One MCP server whose tools could not be acquired.
type alias FilesEditErrorReason Typed refusal from a bounded or conditional Files edit.
type alias FilesPatch Host-owned native patch operation receiving the invocation’s deadline and signal.
type alias FilesPort Captured host file authority; the host owns path confinement and atomic replacement.
type alias FileToolName Closed set of direct, model-visible Files tools; changing it is an ADR.
type alias FileTool A MuleTool minted only by this module, named from the closed FileToolName set. No host can construct one: the brand has no public constructor, so a host-made MuleTool fails typecheck against files?.
type alias FilesSurface One Mycelium module and the direct Files tools built from the same port.
type alias FilesConnectionOptions Host inputs for a captured Files connection.
type alias KnapsackComputer Generic advertised-call interface supplied by one Computer provider.
type alias ComputerConnectionOptions Selection and naming required to connect one Computer to Mycelium.
type alias ComputerUnavailableReason Why one Computer contributed no module to an acquisition.
type alias ComputersConnectionOptions A captured host connection list; authority remains with its supplying host.
type alias McpServerConnection One remote MCP server a host has admitted for an Agent.
type alias McpServersConnectionOptions A captured host list of remote MCP servers; authority stays with the host.
type alias McpServerUnavailableReason Why one MCP server contributed no module to an acquisition.
type alias UiConnectionOptions How one Agent host connects its Sessions’ open Apps to Knapsack.ui.
type alias UiConnection Live commands of the App open in each Agent Session’s browser. The Agent reaches them through the ui module (ui.discover, ui.execute); the browser runs them through Whistle over the transport this connection serves.
constant listInputSchema List one directory level.
constant globInputSchema Match paths below a directory by glob.
constant grepInputSchema Search file content, optionally restricted to matching paths.
constant searchOutputSchema Found paths or matching lines, and which cap (if any) cut the search short.
constant editInputSchema Text replacement request decoded before the captured Files port is called.
constant filesEditFailureSchema Structured edit refusal, including its reason and affected path.
constant editOutputSchema Successful edit metadata or a typed edit refusal.
constant Knapsack Agent tool connections available for composition through Mycelium.
function edit() Apply the same edit behavior as the Files edit operation directly through a captured FilesPort.
function presentReadForGuest() Guest code and MCP callers never receive base64: the image goes through the execution’s image collector (the images.view path) and the value is a caption.

Expected Files edit refusal, kept distinct from unexpected host failures.

  • Error
Signature
new FilesEditError(
reason,
path,
detail
): FilesEditError;
Parameter Type
reason FilesEditErrorReason
path string
detail string

FilesEditError

Signature
Error.constructor;
Signature
readonly reason: FilesEditErrorReason;
Signature
readonly path: string;

ComputerUnavailableErrorclass

Section titled “ComputerUnavailableError”

One Computer whose advertisement could not be acquired.

  • Error
Signature
new ComputerUnavailableError(
computerId,
reason,
cause?
): ComputerUnavailableError;
Parameter Type
computerId string
reason ComputerUnavailableReason
cause? unknown

ComputerUnavailableError

Signature
Error.constructor;
Signature
readonly name: "ComputerUnavailableError" = "ComputerUnavailableError";
Signature
Error.name;
Signature
readonly computerId: string;
Signature
readonly reason: ComputerUnavailableReason;

McpServerUnavailableErrorclass

Section titled “McpServerUnavailableError”

One MCP server whose tools could not be acquired.

  • Error
Signature
new McpServerUnavailableError(
serverId,
reason,
cause?
): McpServerUnavailableError;
Parameter Type
serverId string
reason McpServerUnavailableReason
cause? unknown

McpServerUnavailableError

Signature
Error.constructor;
Signature
readonly name: "McpServerUnavailableError" = "McpServerUnavailableError";
Signature
Error.name;
Signature
readonly serverId: string;
Signature
readonly reason: McpServerUnavailableReason;

FilesEditErrorReasontype alias

Section titled “FilesEditErrorReason”
Signature
type FilesEditErrorReason =
| "not-found"
| "ambiguous"
| "file-too-large"
| "partial-character"
| "unchanged"
| "not-text"
| "conflict";

Typed refusal from a bounded or conditional Files edit.


FilesPatchtype alias

Section titled “FilesPatch”
Signature
type FilesPatch = (
input,
context,
) => Promise<z.output<typeof patchOutputSchema>>;

Host-owned native patch operation receiving the invocation’s deadline and signal.

Parameter Type
input z.output<typeof patchInputSchema>
context InvocationContext

Promise<z.output<typeof patchOutputSchema>>


FilesPorttype alias

Section titled “FilesPort”
Signature
type FilesPort = Readonly<{
patch?: FilesPatch;
read: (input, context) => Promise<z.output<typeof readOutputSchema>>;
list: (input, context) => Promise<z.output<typeof searchOutputSchema>>;
glob: (input, context) => Promise<z.output<typeof searchOutputSchema>>;
grep: (input, context) => Promise<z.output<typeof searchOutputSchema>>;
replace: (
input,
context,
) => Promise<
Readonly<{
path: string;
bytes: number;
}>
>;
}>;

Captured host file authority; the host owns path confinement and atomic replacement.


FileToolNametype alias

Section titled “FileToolName”
Signature
type FileToolName = "read" | "list" | "glob" | "grep" | "apply_patch";

Closed set of direct, model-visible Files tools; changing it is an ADR.


Signature
type FileTool = LabelledTool &
Readonly<{
[fileToolBrand]: true;
}> &
Readonly<{
descriptor: Readonly<{
name: FileToolName;
}>;
module: "files";
access: Access;
}>;

A MuleTool minted only by this module, named from the closed FileToolName set. No host can construct one: the brand has no public constructor, so a host-made MuleTool fails typecheck against files?.


FilesSurfacetype alias

Section titled “FilesSurface”
Signature
type FilesSurface = Readonly<{
module: ModuleRegistration;
tools: readonly FileTool[];
}>;

One Mycelium module and the direct Files tools built from the same port.


FilesConnectionOptionstype alias

Section titled “FilesConnectionOptions”
Signature
type FilesConnectionOptions = Readonly<{
port: FilesPort;
pathInstructions?: string;
}>;

Host inputs for a captured Files connection.


KnapsackComputertype alias

Section titled “KnapsackComputer”
Signature
type KnapsackComputer = Readonly<{
getCapabilities: (signal) => Promise<MyceliumJsonValue>;
call: (capabilityId, input, context) => Promise<MyceliumJsonValue>;
}>;

Generic advertised-call interface supplied by one Computer provider.


ComputerConnectionOptionstype alias

Section titled “ComputerConnectionOptions”
Signature
type ComputerConnectionOptions = Readonly<{
id: string;
name: string;
namespace?: readonly string[];
confirmCalls?: boolean;
files?: FilesConnectionOptions;
role?: string;
computer: KnapsackComputer;
}>;

Selection and naming required to connect one Computer to Mycelium.


ComputerUnavailableReasontype alias

Section titled “ComputerUnavailableReason”
Signature
type ComputerUnavailableReason =
"rejected" | "timeout" | "invalid-advertisement";

Why one Computer contributed no module to an acquisition.


ComputersConnectionOptionstype alias

Section titled “ComputersConnectionOptions”
Signature
type ComputersConnectionOptions = Readonly<{
id: string;
read: () => Readonly<{
version: string;
connections: readonly ComputerConnectionOptions[];
}>;
subscribe: (invalidate) => () => void;
onUnavailable: (error) => void;
describeTimeoutMillis?: number;
}>;

A captured host connection list; authority remains with its supplying host.


McpServerConnectiontype alias

Section titled “McpServerConnection”
Signature
type McpServerConnection = Readonly<{
id: string;
name: string;
url: URL;
headers?: Readonly<Record<string, string>>;
}>;

One remote MCP server a host has admitted for an Agent.


McpServersConnectionOptionstype alias

Section titled “McpServersConnectionOptions”
Signature
type McpServersConnectionOptions = Readonly<{
id: string;
read: () => Promise<
Readonly<{
version: string;
servers: readonly McpServerConnection[];
}>
>;
subscribe: (invalidate) => () => void;
onUnavailable: (error) => void;
fetch: (url, init?) => Promise<Response>;
namespace?: readonly string[];
connectTimeoutMillis?: number;
}>;

A captured host list of remote MCP servers; authority stays with the host.


McpServerUnavailableReasontype alias

Section titled “McpServerUnavailableReason”
Signature
type McpServerUnavailableReason =
"rejected" | "unauthorized" | "timeout" | "invalid-advertisement";

Why one MCP server contributed no module to an acquisition.


UiConnectionOptionstype alias

Section titled “UiConnectionOptions”
Signature
type UiConnectionOptions = Readonly<{
id: string;
hasSession: (sessionId) => Promise<boolean>;
}>;

How one Agent host connects its Sessions’ open Apps to Knapsack.ui.


UiConnectiontype alias

Section titled “UiConnection”
Signature
type UiConnection = Readonly<{
source: CapabilitySource;
fetch: (request) => Promise<Response>;
close: () => Promise<void>;
}>;

Live commands of the App open in each Agent Session’s browser. The Agent reaches them through the ui module (ui.discover, ui.execute); the browser runs them through Whistle over the transport this connection serves.

listInputSchemaconstant

Section titled “listInputSchema”
Signature
const listInputSchema: ZodLazy<
ZodObject<
{
path: ZodDefault<ZodLazy<ZodString>>;
limit: ZodDefault<ZodNumber>;
},
$strict
>
>;

List one directory level.


globInputSchemaconstant

Section titled “globInputSchema”
Signature
const globInputSchema: ZodLazy<
ZodObject<
{
path: ZodDefault<ZodLazy<ZodString>>;
pattern: ZodString;
limit: ZodDefault<ZodNumber>;
},
$strict
>
>;

Match paths below a directory by glob.


grepInputSchemaconstant

Section titled “grepInputSchema”
Signature
const grepInputSchema: ZodLazy<
ZodObject<
{
path: ZodDefault<ZodLazy<ZodString>>;
pattern: ZodString;
include: ZodOptional<ZodString>;
regex: ZodDefault<ZodBoolean>;
ignoreCase: ZodDefault<ZodBoolean>;
limit: ZodDefault<ZodNumber>;
},
$strict
>
>;

Search file content, optionally restricted to matching paths.


searchOutputSchemaconstant

Section titled “searchOutputSchema”
Signature
const searchOutputSchema: ZodLazy<
ZodObject<
{
path: ZodLazy<ZodString>;
results: ZodArray<
ZodLazy<
ZodObject<
{
path: ZodLazy<ZodString>;
type: ZodExactOptional<
ZodEnum<{
file: "file";
directory: "directory";
symlink: "symlink";
}>
>;
line: ZodExactOptional<ZodNumber>;
text: ZodExactOptional<ZodString>;
},
$strict
>
>
>;
truncated: ZodNullable<
ZodEnum<{
limit: "limit";
scan: "scan";
}>
>;
},
$strict
>
>;

Found paths or matching lines, and which cap (if any) cut the search short.


editInputSchemaconstant

Section titled “editInputSchema”
Signature
const editInputSchema: ZodLazy<
ZodObject<
{
path: ZodLazy<ZodString>;
oldText: ZodString;
newText: ZodString;
},
$strict
>
>;

Text replacement request decoded before the captured Files port is called.


filesEditFailureSchemaconstant

Section titled “filesEditFailureSchema”
Signature
const filesEditFailureSchema: ZodLazy<ZodObject<{
status: ZodLiteral<"failed">;
error: ZodObject<{
kind: ZodEnum<{
not-found: "not-found";
ambiguous: "ambiguous";
file-too-large: "file-too-large";
partial-character: "partial-character";
unchanged: "unchanged";
not-text: "not-text";
conflict: "conflict";
}>;
path: ZodLazy<ZodString>;
message: ZodString;
}, $strict>;
}, $strict>>;

Structured edit refusal, including its reason and affected path.


editOutputSchemaconstant

Section titled “editOutputSchema”
Signature
const editOutputSchema: ZodUnion<readonly [ZodLazy<ZodObject<{
path: ZodLazy<ZodString>;
bytes: ZodNumber;
diff: ZodString;
firstChangedLine: ZodNullable<ZodNumber>;
unifiedPatch: ZodNullable<ZodString>;
unifiedPatchBytes: ZodNumber;
unifiedPatchAddedLines: ZodNumber;
unifiedPatchRemovedLines: ZodNumber;
unifiedPatchTruncated: ZodBoolean;
}, $strict>>, ZodLazy<ZodObject<{
status: ZodLiteral<"failed">;
error: ZodObject<{
kind: ZodEnum<{
not-found: "not-found";
ambiguous: "ambiguous";
file-too-large: "file-too-large";
partial-character: "partial-character";
unchanged: "unchanged";
not-text: "not-text";
conflict: "conflict";
}>;
path: ZodLazy<ZodString>;
message: ZodString;
}, $strict>;
}, $strict>>]>;

Successful edit metadata or a typed edit refusal.


Knapsackconstant

Section titled “Knapsack”
Signature
const Knapsack: Readonly<{
computer: (input) => CapabilitySource;
computers: (input) => CapabilitySource;
files: (input) => FilesSurface;
mcpServers: (input) => CapabilitySource;
ui: (options) => UiConnection;
}>;

Agent tool connections available for composition through Mycelium.

Signature
function edit(
port,
input,
context,
): Promise<
| {
path: string;
bytes: number;
diff: string;
firstChangedLine: number | null;
unifiedPatch: string | null;
unifiedPatchBytes: number;
unifiedPatchAddedLines: number;
unifiedPatchRemovedLines: number;
unifiedPatchTruncated: boolean;
}
| {
status: "failed";
error: {
kind:
| "not-found"
| "ambiguous"
| "file-too-large"
| "partial-character"
| "unchanged"
| "not-text"
| "conflict";
path: string;
message: string;
};
}
>;

Apply the same edit behavior as the Files edit operation directly through a captured FilesPort.

Parameter Type
port FilesPort
input { path: string; oldText: string; newText: string; }
input.path string
input.oldText string
input.newText string
context InvocationContext

Promise< | { path: string; bytes: number; diff: string; firstChangedLine: number | null; unifiedPatch: string | null; unifiedPatchBytes: number; unifiedPatchAddedLines: number; unifiedPatchRemovedLines: number; unifiedPatchTruncated: boolean; } | { status: "failed"; error: { kind: | "not-found" | "ambiguous" | "file-too-large" | "partial-character" | "unchanged" | "not-text" | "conflict"; path: string; message: string; }; }>


presentReadForGuest()function

Section titled “presentReadForGuest()”
Signature
function presentReadForGuest(
result,
context,
):
| {
path: string;
content: string;
offset: number;
nextOffset: number | null;
truncated: boolean;
file?: {
computerId: string;
path: string;
};
}
| {
path: string;
image: {
ref: string;
mimeType: "image/png" | "image/jpeg" | "image/gif" | "image/webp";
bytes: number;
};
};

Guest code and MCP callers never receive base64: the image goes through the execution’s image collector (the images.view path) and the value is a caption.

Parameter Type
result | { path: string; content: string; offset: number; nextOffset: number | null; truncated: boolean; file?: { computerId: string; path: string; }; } | { path: string; image: { mimeType: "image/png" | "image/jpeg" | "image/gif" | "image/webp"; data: string; bytes: number; }; file?: { computerId: string; path: string; }; }
context InvocationContext
Signature
{
path: string;
content: string;
offset: number;
nextOffset: number | null;
truncated: boolean;
file?: {
computerId: string;
path: string;
};
}
Name Type Default value Description
path string pathSchema -
content string - -
offset number - -
nextOffset number | null - -
truncated boolean - -
file? object - Captured hosted file identity; a local Files host may have no Computer identity.
file.computerId string - -
file.path string - -

Signature
{
path: string;
image: {
ref: string;
mimeType: "image/png" | "image/jpeg" | "image/gif" | "image/webp";
bytes: number;
}
}

Re-exports readInputSchema


Re-exports readOutputSchema


Re-exports readGuestOutputSchema


Re-exports patchInputSchema


Re-exports patchOutputSchema