Skip to content

Commit 9dca129

Browse files
committed
feat(w3rt-core): extract shared tool/workflow/policy types into core boundary
1 parent 24b1cf0 commit 9dca129

File tree

8 files changed

+311
-236
lines changed

8 files changed

+311
-236
lines changed

docs/architecture/multi-mono-migration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ This repo is entering a transition from single-repo accumulation to a federated
2323

2424
1. **Core extraction**
2525
- shared interfaces/types/policy contracts (`w3rt-core`)
26+
- phase-1 seeded in-repo under `src/w3rt-core/`:
27+
- `tool-types.ts`
28+
- `workflow-run-mode.ts`
29+
- `evm-transfer-policy-types.ts`
2630
2. **Chain split**
2731
- one repo per chain family (`chain-*-tools`)
2832
3. **Strategy split**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import type { EvmTransferPolicy } from "../src/chains/evm/policy.js";
4+
import {
5+
parseRunMode as parseRunModeCompat,
6+
resolveWorkflowRunMode as resolveWorkflowRunModeCompat,
7+
} from "../src/chains/shared/workflow-runtime.js";
8+
import { defineTool as defineToolCoreCompat } from "../src/core/types.js";
9+
import { EVM_TRANSFER_POLICY_SCHEMA } from "../src/w3rt-core/evm-transfer-policy-types.js";
10+
import { defineTool as defineToolW3rt } from "../src/w3rt-core/tool-types.js";
11+
import {
12+
parseRunMode as parseRunModeW3rt,
13+
resolveWorkflowRunMode as resolveWorkflowRunModeW3rt,
14+
} from "../src/w3rt-core/workflow-run-mode.js";
15+
16+
describe("w3rt-core phase-1 extraction", () => {
17+
it("keeps core/types and shared/workflow-runtime compatibility exports", () => {
18+
expect(defineToolCoreCompat).toBeTypeOf("function");
19+
expect(defineToolW3rt).toBeTypeOf("function");
20+
expect(parseRunModeCompat("execute")).toBe(parseRunModeW3rt("execute"));
21+
expect(resolveWorkflowRunModeCompat(undefined, "先模拟")).toBe(
22+
resolveWorkflowRunModeW3rt(undefined, "先模拟"),
23+
);
24+
});
25+
26+
it("exports transfer policy schema/types from w3rt-core", () => {
27+
const policy: EvmTransferPolicy = {
28+
schema: EVM_TRANSFER_POLICY_SCHEMA,
29+
version: 1,
30+
updatedAt: new Date().toISOString(),
31+
updatedBy: null,
32+
note: null,
33+
mode: "open",
34+
enforceOn: "mainnet_like",
35+
allowedRecipients: [],
36+
};
37+
expect(policy.schema).toBe("evm.transfer.policy.v1");
38+
});
39+
});

src/chains/evm/policy.ts

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
44

5+
import {
6+
EVM_TRANSFER_POLICY_SCHEMA,
7+
EVM_TRANSFER_POLICY_STORE_SCHEMA,
8+
} from "../../w3rt-core/evm-transfer-policy-types.js";
9+
import type {
10+
EvmTransferPolicy,
11+
EvmTransferPolicyAuditAction,
12+
EvmTransferPolicyAuditRecord,
13+
EvmTransferPolicyCheck,
14+
EvmTransferPolicyCheckInput,
15+
EvmTransferPolicyEnforceOn,
16+
EvmTransferPolicyMode,
17+
EvmTransferPolicyTemplate,
18+
EvmTransferPolicyUpdate,
19+
} from "../../w3rt-core/evm-transfer-policy-types.js";
520
import type { EvmNetwork } from "./runtime.js";
621

722
const EVM_TRANSFER_POLICY_SYMBOL = Symbol.for(
@@ -13,59 +28,18 @@ const EVM_TRANSFER_POLICY_AUDIT_SYMBOL = Symbol.for(
1328
const EVM_TRANSFER_POLICY_PATH_ENV = "EVM_TRANSFER_POLICY_PATH";
1429
const EVM_TRANSFER_POLICY_DIR_ENV = "EVM_TRANSFER_POLICY_DIR";
1530
const EVM_TRANSFER_POLICY_FILENAME = "evm-transfer-policy.json";
16-
const EVM_TRANSFER_POLICY_SCHEMA = "evm.transfer.policy.v1";
17-
const EVM_TRANSFER_POLICY_STORE_SCHEMA = "evm.transfer.policy.store.v1";
1831

19-
export type EvmTransferPolicyMode = "open" | "allowlist";
20-
export type EvmTransferPolicyEnforceOn = "mainnet_like" | "all";
21-
export type EvmTransferPolicyTemplate = "production_safe" | "open_dev";
22-
23-
export type EvmTransferPolicy = {
24-
schema: "evm.transfer.policy.v1";
25-
version: number;
26-
updatedAt: string;
27-
updatedBy: string | null;
28-
note: string | null;
29-
mode: EvmTransferPolicyMode;
30-
enforceOn: EvmTransferPolicyEnforceOn;
31-
allowedRecipients: string[];
32-
};
33-
34-
export type EvmTransferPolicyUpdate = {
35-
mode?: EvmTransferPolicyMode;
36-
enforceOn?: EvmTransferPolicyEnforceOn;
37-
allowedRecipients?: string[];
38-
clearRecipients?: boolean;
39-
updatedBy?: string | null;
40-
note?: string | null;
41-
};
42-
43-
export type EvmTransferPolicyCheckInput = {
44-
network: EvmNetwork;
45-
toAddress: string;
46-
transferType: "native" | "erc20";
47-
tokenAddress?: string;
48-
};
49-
50-
export type EvmTransferPolicyCheck = {
51-
allowed: boolean;
52-
reason: string | null;
53-
policy: EvmTransferPolicy;
54-
};
55-
56-
export type EvmTransferPolicyAuditAction = "set_policy" | "apply_template";
57-
58-
export type EvmTransferPolicyAuditRecord = {
59-
schema: "evm.transfer.policy.audit.v1";
60-
id: string;
61-
at: string;
62-
action: EvmTransferPolicyAuditAction;
63-
template: EvmTransferPolicyTemplate | null;
64-
actor: string | null;
65-
note: string | null;
66-
before: EvmTransferPolicy;
67-
after: EvmTransferPolicy;
68-
};
32+
export type {
33+
EvmTransferPolicy,
34+
EvmTransferPolicyAuditAction,
35+
EvmTransferPolicyAuditRecord,
36+
EvmTransferPolicyCheck,
37+
EvmTransferPolicyCheckInput,
38+
EvmTransferPolicyEnforceOn,
39+
EvmTransferPolicyMode,
40+
EvmTransferPolicyTemplate,
41+
EvmTransferPolicyUpdate,
42+
} from "../../w3rt-core/evm-transfer-policy-types.js";
6943

7044
type StoredPolicyState = {
7145
schema: "evm.transfer.policy.store.v1";
Lines changed: 1 addition & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1 @@
1-
export type WorkflowRunMode = "analysis" | "simulate" | "execute";
2-
export type WorkflowRunModeWithCompose = WorkflowRunMode | "compose";
3-
4-
export type WorkflowRunModeOptions = {
5-
allowCompose: boolean;
6-
};
7-
8-
const DEFAULT_WORKFLOW_RUN_MODE_OPTIONS: WorkflowRunModeOptions = {
9-
allowCompose: false,
10-
};
11-
12-
function isAllowed(
13-
value: string | undefined,
14-
options: WorkflowRunModeOptions,
15-
): boolean {
16-
return (
17-
value === "analysis" ||
18-
value === "simulate" ||
19-
value === "execute" ||
20-
(options.allowCompose && value === "compose")
21-
);
22-
}
23-
24-
const HAS_EXECUTE_HINT =
25-
/(|||||||execute|submit|live\s+order|real\s+order|\bnow\b.*\bexecute\b)/i;
26-
const HAS_SIMULATE_HINT =
27-
/(||仿|dry\s*run|dry\s*run|simulate||||)/i;
28-
const HAS_ANALYSIS_HINT =
29-
/(||||analysis|analyze||)/i;
30-
31-
export function parseRunMode(
32-
value: string | undefined,
33-
options?: Partial<WorkflowRunModeOptions>,
34-
): WorkflowRunMode {
35-
const parsedOptions = {
36-
...DEFAULT_WORKFLOW_RUN_MODE_OPTIONS,
37-
...options,
38-
};
39-
if (isAllowed(value, parsedOptions)) {
40-
return value as WorkflowRunMode;
41-
}
42-
return "analysis";
43-
}
44-
45-
export function parseRunModeWithCompose(
46-
value: string | undefined,
47-
options?: Partial<WorkflowRunModeOptions>,
48-
): WorkflowRunModeWithCompose {
49-
const parsedOptions = {
50-
...DEFAULT_WORKFLOW_RUN_MODE_OPTIONS,
51-
...options,
52-
};
53-
if (isAllowed(value, parsedOptions)) {
54-
return value as WorkflowRunModeWithCompose;
55-
}
56-
return "analysis";
57-
}
58-
59-
export function parseRunModeHint<
60-
TAllowCompose extends boolean | undefined = false,
61-
>(
62-
text: string | undefined,
63-
options?: Partial<WorkflowRunModeOptions> & { allowCompose?: TAllowCompose },
64-
): TAllowCompose extends true
65-
? WorkflowRunModeWithCompose | undefined
66-
: WorkflowRunMode | undefined {
67-
const parsedOptions = {
68-
...DEFAULT_WORKFLOW_RUN_MODE_OPTIONS,
69-
...options,
70-
};
71-
if (!text?.trim()) {
72-
return undefined as TAllowCompose extends true
73-
? WorkflowRunModeWithCompose | undefined
74-
: WorkflowRunMode | undefined;
75-
}
76-
77-
const hasExecute = HAS_EXECUTE_HINT.test(text);
78-
const hasSimulate = HAS_SIMULATE_HINT.test(text);
79-
const hasAnalysis = HAS_ANALYSIS_HINT.test(text);
80-
let mode: WorkflowRunMode | WorkflowRunModeWithCompose | undefined;
81-
if (parsedOptions.allowCompose && /compose|||/.test(text)) {
82-
mode = "compose";
83-
} else if (hasSimulate && !hasExecute) {
84-
mode = "simulate";
85-
} else if (hasAnalysis && !hasExecute && !hasSimulate) {
86-
mode = "analysis";
87-
} else if (hasExecute && !hasSimulate && !hasAnalysis) {
88-
mode = "execute";
89-
} else if (hasSimulate && hasExecute) {
90-
if (
91-
/(|仿|dry\s*run||||)/i.test(text)
92-
) {
93-
mode = "simulate";
94-
} else {
95-
mode = "execute";
96-
}
97-
} else if (hasAnalysis && hasExecute) {
98-
if (/(||)/i.test(text)) {
99-
mode = "analysis";
100-
} else {
101-
mode = "execute";
102-
}
103-
} else if (hasExecute) {
104-
mode = "execute";
105-
} else if (hasSimulate) {
106-
mode = "simulate";
107-
} else if (hasAnalysis) {
108-
mode = "analysis";
109-
}
110-
return mode as TAllowCompose extends true
111-
? WorkflowRunModeWithCompose | undefined
112-
: WorkflowRunMode | undefined;
113-
}
114-
115-
export function resolveWorkflowRunMode<
116-
TAllowCompose extends boolean | undefined = false,
117-
>(
118-
paramsRunMode: string | undefined,
119-
intentText: string | undefined,
120-
options?: Partial<WorkflowRunModeOptions> & { allowCompose?: TAllowCompose },
121-
): TAllowCompose extends true ? WorkflowRunModeWithCompose : WorkflowRunMode {
122-
const resolvedOptions = {
123-
...DEFAULT_WORKFLOW_RUN_MODE_OPTIONS,
124-
...options,
125-
};
126-
const allowCompose = resolvedOptions.allowCompose;
127-
const hint = parseRunModeHint<TAllowCompose>(intentText, options);
128-
const parseMode = (
129-
value: string | undefined,
130-
): WorkflowRunMode | WorkflowRunModeWithCompose =>
131-
allowCompose
132-
? parseRunModeWithCompose(value, resolvedOptions)
133-
: parseRunMode(value, resolvedOptions);
134-
135-
const selectedMode = paramsRunMode != null ? paramsRunMode : hint;
136-
return (
137-
selectedMode != null ? parseMode(selectedMode) : "analysis"
138-
) as TAllowCompose extends true
139-
? WorkflowRunModeWithCompose
140-
: WorkflowRunMode;
141-
}
1+
export * from "../../w3rt-core/workflow-run-mode.js";

src/core/types.ts

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1 @@
1-
import type { Static, TSchema } from "@sinclair/typebox";
2-
3-
export interface RegisteredTool<
4-
TParams extends TSchema = TSchema,
5-
TDetails = unknown,
6-
> {
7-
name: string;
8-
label: string;
9-
description: string;
10-
parameters: TParams;
11-
execute(
12-
toolCallId: string,
13-
params: Static<TParams>,
14-
...rest: unknown[]
15-
): Promise<{
16-
content: { type: string; text: string }[];
17-
details?: TDetails;
18-
}>;
19-
}
20-
21-
export function defineTool<TParams extends TSchema>(
22-
tool: RegisteredTool<TParams, unknown>,
23-
): RegisteredTool<TParams, unknown> {
24-
return tool;
25-
}
26-
27-
export interface ToolRegistrar {
28-
registerTool<TParams extends TSchema = TSchema>(
29-
tool: RegisteredTool<TParams, unknown>,
30-
): void;
31-
}
32-
33-
export type ChainToolGroupName = "read" | "compose" | "execute" | "rpc";
34-
35-
export interface ChainToolGroup {
36-
name: ChainToolGroupName;
37-
tools: RegisteredTool<TSchema, unknown>[];
38-
}
39-
40-
export interface ChainToolset {
41-
chain: string;
42-
groups: ChainToolGroup[];
43-
}
1+
export * from "../w3rt-core/tool-types.js";
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { EvmNetwork } from "../chains/evm/runtime.js";
2+
3+
export const EVM_TRANSFER_POLICY_SCHEMA = "evm.transfer.policy.v1" as const;
4+
export const EVM_TRANSFER_POLICY_STORE_SCHEMA =
5+
"evm.transfer.policy.store.v1" as const;
6+
7+
export type EvmTransferPolicyMode = "open" | "allowlist";
8+
export type EvmTransferPolicyEnforceOn = "mainnet_like" | "all";
9+
export type EvmTransferPolicyTemplate = "production_safe" | "open_dev";
10+
11+
export type EvmTransferPolicy = {
12+
schema: typeof EVM_TRANSFER_POLICY_SCHEMA;
13+
version: number;
14+
updatedAt: string;
15+
updatedBy: string | null;
16+
note: string | null;
17+
mode: EvmTransferPolicyMode;
18+
enforceOn: EvmTransferPolicyEnforceOn;
19+
allowedRecipients: string[];
20+
};
21+
22+
export type EvmTransferPolicyUpdate = {
23+
mode?: EvmTransferPolicyMode;
24+
enforceOn?: EvmTransferPolicyEnforceOn;
25+
allowedRecipients?: string[];
26+
clearRecipients?: boolean;
27+
updatedBy?: string | null;
28+
note?: string | null;
29+
};
30+
31+
export type EvmTransferPolicyCheckInput = {
32+
network: EvmNetwork;
33+
toAddress: string;
34+
transferType: "native" | "erc20";
35+
tokenAddress?: string;
36+
};
37+
38+
export type EvmTransferPolicyCheck = {
39+
allowed: boolean;
40+
reason: string | null;
41+
policy: EvmTransferPolicy;
42+
};
43+
44+
export type EvmTransferPolicyAuditAction = "set_policy" | "apply_template";
45+
46+
export type EvmTransferPolicyAuditRecord = {
47+
schema: "evm.transfer.policy.audit.v1";
48+
id: string;
49+
at: string;
50+
action: EvmTransferPolicyAuditAction;
51+
template: EvmTransferPolicyTemplate | null;
52+
actor: string | null;
53+
note: string | null;
54+
before: EvmTransferPolicy;
55+
after: EvmTransferPolicy;
56+
};

0 commit comments

Comments
 (0)