Skip to content
3 min read · 581 words

Class: OpfsSpoolStore

Defined in: batteries/storage/opfs/index.ts:431

"Give bytes, get a reader" persistence layer over an OPFS directory.

Remarks

write(callId, bytes) resolves the root directory (lazily, on first call), opens or creates the file named keyPrefix + callId, then writes via the API matching the current scope: a FileSystemSyncAccessHandle in worker scopes, OpfsFileHandle.createWritable() on the main thread. A fresh OpfsSpoolReader pointed at the same file is returned.

read(callId) returns a reader without re-writing; delete(callId) removes the entry.

The store is otherwise stateless — it owns no in-memory cache of writes. Multiple OpfsSpoolStore instances sharing the same root directory and key prefix see the same data.

Example

ts
import { OpfsSpoolStore } from "@nhtio/adk/batteries/storage/opfs";

const store = new OpfsSpoolStore({ keyPrefix: "agent-runs/" });

const bytes = await tool.executor(ctx)(args);
const reader = await store.write(callId, bytes);
const Ctor = tool.artifactConstructor?.() ?? SpooledArtifact;
const artifact = new Ctor(reader);

Implements

Constructors

Constructor

ts
new OpfsSpoolStore(opts?: OpfsSpoolStoreOptions): OpfsSpoolStore;

Defined in: batteries/storage/opfs/index.ts:437

Parameters

ParameterType
optsOpfsSpoolStoreOptions

Returns

OpfsSpoolStore

Methods

delete()

ts
delete(callId: string): Promise<boolean>;

Defined in: batteries/storage/opfs/index.ts:528

Removes the entry under callId.

Parameters

ParameterTypeDescription
callIdstringIdentifier whose entry should be removed.

Returns

Promise<boolean>

true if the entry existed and was removed; false if it didn't exist.

Implementation of

ts
SpoolStore.delete;

has()

ts
has(callId: string): Promise<boolean>;

Defined in: batteries/storage/opfs/index.ts:546

Returns true if a file is present under callId.

Parameters

ParameterTypeDescription
callIdstringIdentifier to test.

Returns

Promise<boolean>

true when the file exists, false otherwise.


keyFor()

ts
keyFor(callId: string): string;

Defined in: batteries/storage/opfs/index.ts:565

Returns the full filename for a given callId (i.e. keyPrefix + callId).

Parameters

ParameterType
callIdstring

Returns

string

Remarks

Useful for tests or for callers that want to interact with the underlying OPFS directory directly.


read()

ts
read(callId: string, opts?: OpfsSpoolReaderOptions): Promise<OpfsSpoolReader | undefined>;

Defined in: batteries/storage/opfs/index.ts:507

Returns a reader over the bytes previously written under callId.

Parameters

ParameterTypeDescription
callIdstringIdentifier supplied to a prior OpfsSpoolStore.write call.
opts?OpfsSpoolReaderOptionsPer-call override for streamThresholdBytes.

Returns

Promise<OpfsSpoolReader | undefined>

An OpfsSpoolReader, or undefined if the key is missing.

Remarks

Returns undefined if the file does not exist.

Implementation of

ts
SpoolStore.read;

write()

ts
write(
   callId: string,
   bytes:
  | string
  | Uint8Array<ArrayBufferLike>
  | ReadableStream<Uint8Array<ArrayBufferLike>>,
opts?: OpfsSpoolReaderOptions): Promise<OpfsSpoolReader>;

Defined in: batteries/storage/opfs/index.ts:470

Persists bytes under callId and returns a reader bound to the stored key.

Parameters

ParameterTypeDescription
callIdstringIdentifier used to retrieve the bytes via OpfsSpoolStore.read.
bytes| string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>>The bytes to store, as a string, Uint8Array, or ReadableStream<Uint8Array>.
opts?OpfsSpoolReaderOptionsPer-call override for streamThresholdBytes.

Returns

Promise<OpfsSpoolReader>

An OpfsSpoolReader over the stored bytes.

Remarks

string input is encoded as UTF-8; Uint8Array is stored byte-faithfully; ReadableStream<Uint8Array> is written incrementally — the stream is consumed chunk-by-chunk straight to OPFS without first materializing the whole payload in memory, which is the point of accepting a stream for a durable store.

Implementation of

ts
SpoolStore.write;

isOpfsSpoolStore()

ts
static isOpfsSpoolStore(value: unknown): value is OpfsSpoolStore;

Defined in: batteries/storage/opfs/index.ts:452

Returns true if value is an OpfsSpoolStore instance.

Parameters

ParameterTypeDescription
valueunknownThe value to test.

Returns

value is OpfsSpoolStore

true when value is an OpfsSpoolStore instance.

Remarks

Uses @nhtio/adk!isInstanceOf for cross-realm safety.