Skip to content

Commit b95d7b2

Browse files
committed
refactor: Enable makeJsonAgent to be bundled with @endo/bundle-source
This refactors kernel-utils and kernel-errors to enable makeJsonAgent to be bundled into vats using @endo/bundle-source. Changes: - kernel-utils: Added subpath exports (counter, merge-disjoint-records, schema) - kernel-errors: Added bundleable subpath with simplified error classes - kernel-errors: Extracted ErrorCode to error-codes.ts (no @metamask/utils dep) - ocap-kernel: Moved isRetryableNetworkError from kernel-errors to utils - kernel-agents: Updated to use subpath imports The bundler was failing due to transitive dependencies on @noble/hashes which has ESM/CJS compatibility issues. By using subpath exports and moving libp2p-specific code to ocap-kernel, we avoid pulling in unbundleable dependencies.
1 parent 1f6a864 commit b95d7b2

File tree

24 files changed

+192
-54
lines changed

24 files changed

+192
-54
lines changed

packages/kernel-agents/src/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mergeDisjointRecords } from '@metamask/kernel-utils';
1+
import { mergeDisjointRecords } from '@metamask/kernel-utils/merge-disjoint-records';
22
import type { Logger } from '@metamask/logger';
33
import type { LanguageModel } from '@ocap/kernel-language-model-service';
44

packages/kernel-agents/src/attempt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SampleGenerationError } from '@metamask/kernel-errors';
1+
import { SampleGenerationError } from '@metamask/kernel-errors/bundleable';
22
import type { Logger } from '@metamask/logger';
33
import type { LanguageModel } from '@ocap/kernel-language-model-service';
44

packages/kernel-agents/src/capabilities/discover.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { E } from '@endo/eventual-send';
2-
import type { DiscoverableExo, MethodSchema } from '@metamask/kernel-utils';
2+
import type { DiscoverableExo } from '@metamask/kernel-utils/discoverable';
3+
import type { MethodSchema } from '@metamask/kernel-utils/schema';
34

45
import type { CapabilityRecord, CapabilitySpec } from '../types.ts';
56

packages/kernel-agents/src/strategies/json/prepare-attempt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mergeDisjointRecords } from '@metamask/kernel-utils';
1+
import { mergeDisjointRecords } from '@metamask/kernel-utils/merge-disjoint-records';
22
import type { Logger } from '@metamask/logger';
33

44
import { makeEvaluator } from './evaluator.ts';

packages/kernel-agents/src/strategies/json/sample-collector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SampleGenerationError } from '@metamask/kernel-errors';
1+
import { SampleGenerationError } from '@metamask/kernel-errors/bundleable';
22
import type { Logger } from '@metamask/logger';
33

44
import type { SampleCollector } from '../../types.ts';

packages/kernel-agents/src/task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeCounter } from '@metamask/kernel-utils';
1+
import { makeCounter } from '@metamask/kernel-utils/counter';
22

33
import type { Task, CapabilityRecord } from './types.ts';
44

packages/kernel-agents/src/types/capability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { JsonSchema } from '@metamask/kernel-utils';
1+
import type { JsonSchema } from '@metamask/kernel-utils/schema';
22

33
export type Capability<Args extends Record<string, unknown>, Return = null> = (
44
args: Args,

packages/kernel-errors/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
"default": "./dist/index.cjs"
3030
}
3131
},
32+
"./bundleable": {
33+
"import": {
34+
"types": "./dist/bundleable/index.d.mts",
35+
"default": "./dist/bundleable/index.mjs"
36+
},
37+
"require": {
38+
"types": "./dist/bundleable/index.d.cts",
39+
"default": "./dist/bundleable/index.cjs"
40+
}
41+
},
3242
"./package.json": "./package.json"
3343
},
3444
"main": "./dist/index.cjs",
@@ -56,7 +66,6 @@
5666
"test:watch": "vitest --config vitest.config.ts"
5767
},
5868
"dependencies": {
59-
"@libp2p/interface": "2.11.0",
6069
"@metamask/superstruct": "^3.2.1",
6170
"@metamask/utils": "^11.9.0"
6271
},

packages/kernel-errors/src/BaseError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Json } from '@metamask/utils';
22

3-
import { ErrorCode } from './constants.ts';
3+
import { ErrorCode } from './error-codes.ts';
44
import type {
55
MarshaledOcapError,
66
OcapError,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ErrorCode } from '../error-codes.ts';
2+
3+
/**
4+
* Error options with optional stack trace.
5+
*/
6+
type ErrorOptionsWithStack = {
7+
cause?: Error;
8+
stack?: string;
9+
};
10+
11+
/**
12+
* Bundleable version of SampleGenerationError.
13+
* This version does not include marshaling logic to avoid dependencies
14+
* on @metamask/utils that prevent vat bundling.
15+
*
16+
* An error indicating that the LLM generated invalid response.
17+
* This error should trigger resampling from the LLM.
18+
*/
19+
export class SampleGenerationError extends Error {
20+
public readonly code: typeof ErrorCode.SampleGenerationError;
21+
22+
public readonly data: { sample: string };
23+
24+
/**
25+
* Creates a new SampleGenerationError.
26+
*
27+
* @param sample - The invalid sample text generated by the LLM.
28+
* @param cause - The underlying error that caused sample generation to fail.
29+
* @param options - Additional error options including stack.
30+
* @param options.stack - The stack trace of the error.
31+
*/
32+
constructor(sample: string, cause: Error, options?: ErrorOptionsWithStack) {
33+
super('LLM generated invalid response.', { cause });
34+
35+
this.name = 'SampleGenerationError';
36+
this.code = ErrorCode.SampleGenerationError;
37+
this.data = { sample };
38+
39+
if (options?.stack) {
40+
this.stack = options.stack;
41+
}
42+
43+
harden(this);
44+
}
45+
}
46+
harden(SampleGenerationError);

0 commit comments

Comments
 (0)