@hugex/artifact 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { DurableObject } from "cloudflare:workers";
2
2
  import type { ArtifactEnv, ArtifactIndexEntry, ArtifactIndexRpc } from "../types";
3
+ import { type ToolRunAdmitInput, type ToolRunAdmitResult, type ToolRunFinishInput, type ToolRunLogEntry } from "../mcp/toolRunContract";
3
4
  export { INDEX_UPSERT_SQL } from "./index-upsert";
4
5
  /**
5
6
  * One instance per tenant (`idFromName('index')`).
@@ -7,6 +8,17 @@ export { INDEX_UPSERT_SQL } from "./index-upsert";
7
8
  */
8
9
  export declare class ArtifactIndexDO extends DurableObject<ArtifactEnv> implements ArtifactIndexRpc {
9
10
  constructor(ctx: DurableObjectState, env: ArtifactEnv);
11
+ /** Additive, idempotent. Separate tables: never touches artifact rows. */
12
+ private ensureToolRunSchema;
13
+ admitToolRun(input: ToolRunAdmitInput): Promise<ToolRunAdmitResult>;
14
+ finishToolRun(input: ToolRunFinishInput): Promise<boolean>;
15
+ listToolRuns(opts?: {
16
+ actor?: string;
17
+ limit?: number;
18
+ }): Promise<ToolRunLogEntry[]>;
19
+ /** Marks expired pending runs unknown and purges rows older than 30 days, in bounded batches. */
20
+ alarm(): Promise<void>;
21
+ private recordDenial;
10
22
  put(entry: ArtifactIndexEntry): Promise<void>;
11
23
  remove(slug: string): Promise<void>;
12
24
  get(slug: string): Promise<ArtifactIndexEntry | null>;
@@ -3,3 +3,4 @@ export { ArtifactIndexDO } from "./do/ArtifactIndexDO";
3
3
  export { handleArtifactHost } from "./host";
4
4
  export { ARTIFACT_PKG_VERSION } from "./version";
5
5
  export * from "./types";
6
+ export type { ToolRunExecutor, ToolRunOutcome, ToolRunSpec, ToolRunLogEntry } from "./mcp/toolRunContract";
@@ -2,6 +2,7 @@ import type { ArtifactEnv } from '../types';
2
2
  import type { ArtifactAccess } from './authz';
3
3
  import type { ArtifactIdentity } from './auth';
4
4
  import type { ConnectionSummary } from './oauth/contract';
5
+ import type { ToolRunExecutor } from './toolRunContract';
5
6
  /**
6
7
  * Which artifact-to-Worker paths this tenant wires. `server`: the tenant exports
7
8
  * `ArtifactWorkerGateway` (artifact `env.HX`); `browser`: `handleArtifactHost` got an
@@ -26,4 +27,8 @@ export interface McpRequestContext {
26
27
  }>;
27
28
  revokeConnection?: (grantId: string) => Promise<boolean>;
28
29
  workerConnections?: WorkerConnectionSupport;
30
+ /** Injected by the tenant through `handleArtifactHost` options. Without it `tool_run` is unavailable. */
31
+ toolRunExecutor?: ToolRunExecutor;
32
+ /** OAuth grant id (from the access token format `userId:grantId:secret`); audit only. */
33
+ grantId?: string;
29
34
  }
@@ -0,0 +1,22 @@
1
+ import type { McpRequestContext } from '../context';
2
+ import type { ToolDefinition, ToolTextResult } from '../tools';
3
+ import { type Row } from './read';
4
+ export declare const toolTools: ToolDefinition[];
5
+ /** Byte, depth and node limits for client JSON. */
6
+ export declare function jsonWithin(value: unknown, maxBytes: number, maxDepth?: number, maxNodes?: number): boolean;
7
+ export type SkillRevisionFn = (context: McpRequestContext, skillId: string) => Promise<string | null>;
8
+ export type SkillProjectionFn = (context: McpRequestContext, skillId: string) => Promise<{
9
+ revision: string;
10
+ name: string;
11
+ instructions: string;
12
+ references: {
13
+ id: string;
14
+ name: string;
15
+ description: string;
16
+ content: string;
17
+ }[];
18
+ } | null>;
19
+ export declare function executeToolDomainTool(name: string, args: Row, context: McpRequestContext, skills: {
20
+ revision: SkillRevisionFn;
21
+ projection: SkillProjectionFn;
22
+ }): Promise<ToolTextResult>;
@@ -4,4 +4,20 @@ import type { McpDomain } from './artifacts';
4
4
  import { type Row } from './read';
5
5
  export declare const toolsDomain: McpDomain;
6
6
  export declare const skillsDomain: McpDomain;
7
+ type RefRow = {
8
+ id: string;
9
+ name: string;
10
+ description: string;
11
+ content: string;
12
+ };
13
+ /** Current skill_get revision (legacy content hash), or null. */
14
+ export declare function skillRevision(context: McpRequestContext, skillId: string): Promise<string | null>;
15
+ /** Fresh skill projection for tool_run (no runtime cache). */
16
+ export declare function skillProjection(context: McpRequestContext, skillId: string): Promise<{
17
+ revision: string;
18
+ name: string;
19
+ instructions: string;
20
+ references: RefRow[];
21
+ } | null>;
7
22
  export declare function executeCatalogTool(name: string, args: Row, context: McpRequestContext): Promise<ToolTextResult>;
23
+ export {};
@@ -5,7 +5,7 @@ import type { ArtifactEnv } from '../types';
5
5
  import type { McpRequestContext } from './context';
6
6
  export declare const MAX_BODY_BYTES: number;
7
7
  export declare const SERVER_NAME = "hugex";
8
- export declare const SERVER_VERSION = "2.4.0";
8
+ export declare const SERVER_VERSION = "2.5.0";
9
9
  export declare const DEFAULT_PROTOCOL_VERSION = "2025-11-25";
10
10
  export declare const SUPPORTED_PROTOCOL_VERSIONS: readonly ['2024-11-05', '2025-03-26', '2025-06-18', '2025-11-25'];
11
11
  export type AccessMode = 'phase1' | 'rbac';
@@ -1,4 +1,7 @@
1
1
  import type { ArtifactEnv } from '../../types';
2
2
  import type { WorkerConnectionSupport } from '../context';
3
+ import type { ToolRunExecutor } from '../toolRunContract';
3
4
  export type { McpAccessTokenProps } from './contract';
4
- export declare function handleOAuthRequest(request: Request, env: ArtifactEnv, ctx: ExecutionContext, workerConnections?: WorkerConnectionSupport): Promise<Response>;
5
+ export declare function handleOAuthRequest(request: Request, env: ArtifactEnv, ctx: ExecutionContext, workerConnections?: WorkerConnectionSupport, toolRunExecutor?: ToolRunExecutor): Promise<Response>;
6
+ /** workers-oauth-provider tokens are `userId:grantId:secret`. Only the grant id is kept (audit). */
7
+ export declare function grantIdFromBearer(authorization: string | null): string | undefined;
@@ -8,11 +8,13 @@ export declare const SKILL_READ_SCOPE = "skills:read";
8
8
  export declare const SKILL_WRITE_SCOPE = "skills:write";
9
9
  export declare const SKILL_SCOPES: readonly ["skills:read", "skills:write"];
10
10
  export declare const TOOL_READ_SCOPE = "tools:read";
11
+ export declare const TOOL_WRITE_SCOPE = "tools:write";
12
+ export declare const TOOL_RUN_SCOPE = "tools:run";
11
13
  export declare const MEMO_READ_SCOPE = "memos:read";
12
14
  export declare const SECRET_READ_SCOPE = "secrets:read";
13
15
  export declare const SECRET_WRITE_SCOPE = "secrets:write";
14
16
  export declare const SECRET_SCOPES: readonly ["secrets:read", "secrets:write"];
15
- export declare const MCP_SCOPES: readonly ["artifacts:read", "artifacts:write", "workers:read", "workers:run", "workers:write", "skills:read", "skills:write", "tools:read", "memos:read", "secrets:read", "secrets:write"];
17
+ export declare const MCP_SCOPES: readonly ["artifacts:read", "artifacts:write", "workers:read", "workers:run", "workers:write", "skills:read", "skills:write", "tools:read", "memos:read", "secrets:read", "secrets:write", "tools:write", "tools:run"];
16
18
  /** Any authenticated grant may call this tool. It is not an OAuth scope. */
17
19
  export declare const GUIDE_TOOL_NAME = "hugex_guide";
18
20
  /**
@@ -23,6 +25,10 @@ export declare const NO_OAUTH_SCOPE = "";
23
25
  export declare const SKILL_WRITE_TOOLS: Set<string>;
24
26
  /** Authoring tools. Each one needs the workers:read + workers:write pair. */
25
27
  export declare const WORKER_WRITE_TOOLS: Set<string>;
28
+ /** Tool authoring. Each one needs the tools:read + tools:write pair (`tool_get` only with view authoring). */
29
+ export declare const TOOL_WRITE_TOOLS: Set<string>;
30
+ /** Direct execution. Needs the tools:read + tools:run pair. */
31
+ export declare const TOOL_RUN_TOOLS: Set<string>;
26
32
  /** `secret_set` needs the secrets:read + secrets:write pair. `secret_list` stays read-only. */
27
33
  export declare const SECRET_WRITE_TOOLS: Set<string>;
28
34
  export declare function effectiveScopes(scopes: ReadonlySet<string>): ReadonlySet<string>;
@@ -0,0 +1,110 @@
1
+ /**
2
+ * `tool_run` executor contract. Structural on purpose: the tenant injects an implementation from
3
+ * `@hugex/ai` (`toolRunExecutor`) through `handleArtifactHost` options, so `@hugex/artifact`
4
+ * never imports the agent runtime. Mirrored in `lib/server/types/artifact.ts`.
5
+ */
6
+ export interface ToolRunSpec {
7
+ runId: string;
8
+ tenant: string;
9
+ user: {
10
+ sub: string;
11
+ email: string;
12
+ org: string;
13
+ };
14
+ tool: {
15
+ id: string;
16
+ name: string;
17
+ kind: 'http' | 'composio' | 'mcp';
18
+ status: string;
19
+ revision: string;
20
+ inputSchema: unknown;
21
+ execution: unknown;
22
+ output: unknown;
23
+ };
24
+ selector: {
25
+ toolSlug?: string;
26
+ toolVersion?: string;
27
+ remoteToolName?: string;
28
+ };
29
+ args: Record<string, unknown>;
30
+ /** Only referenced vault secrets the user can access. */
31
+ secrets: Record<string, string>;
32
+ /** Org service credentials for a provider adapter (COMPOSIO_API_KEY). */
33
+ serviceSecrets: Record<string, string>;
34
+ timeoutMs: number;
35
+ maxResultBytes: number;
36
+ signal: AbortSignal;
37
+ /** Validate selector, arguments and configuration only: no secrets, no network. */
38
+ dryRun?: boolean;
39
+ }
40
+ export interface ToolRunOutcome {
41
+ status: 'completed' | 'failed' | 'unknown';
42
+ result?: unknown;
43
+ truncated?: boolean;
44
+ omitted?: string[];
45
+ bytes?: number;
46
+ httpStatus?: number;
47
+ error?: {
48
+ code: string;
49
+ message?: string;
50
+ };
51
+ /** dryRun: what would run (selector, credential reference names). Never resolved values. */
52
+ preview?: {
53
+ action?: string;
54
+ remoteToolName?: string;
55
+ toolSlug?: string;
56
+ toolVersion?: string;
57
+ credentialRefs?: string[];
58
+ };
59
+ }
60
+ export type ToolRunExecutor = (spec: ToolRunSpec, env: unknown) => Promise<ToolRunOutcome>;
61
+ /** Admission/audit rows kept by ArtifactIndexDO (`tool_run_log`, 30 days). Never args or output. */
62
+ export interface ToolRunAdmitInput {
63
+ runId: string;
64
+ actor: string;
65
+ grantId: string | null;
66
+ org: string;
67
+ toolKey: string;
68
+ kind: string;
69
+ revision: string | null;
70
+ selector: string | null;
71
+ dryRun: boolean;
72
+ deadlineMs: number;
73
+ }
74
+ export type ToolRunAdmitResult = {
75
+ ok: true;
76
+ } | {
77
+ ok: false;
78
+ code: 'rate_limited';
79
+ retryAfterSec: number;
80
+ scope: 'tool' | 'user';
81
+ };
82
+ export interface ToolRunFinishInput {
83
+ runId: string;
84
+ status: 'preview' | 'completed' | 'failed' | 'unknown';
85
+ code: string | null;
86
+ bytes: number;
87
+ }
88
+ export interface ToolRunLogEntry {
89
+ runId: string;
90
+ actor: string;
91
+ grantId: string | null;
92
+ org: string;
93
+ toolKey: string;
94
+ kind: string;
95
+ revision: string | null;
96
+ selector: string | null;
97
+ dryRun: boolean;
98
+ status: string;
99
+ code: string | null;
100
+ bytes: number;
101
+ startedAt: number;
102
+ finishedAt: number | null;
103
+ durationMs: number | null;
104
+ }
105
+ export declare const TOOL_RUN_LIMITS: {
106
+ readonly perToolPerMinute: 10;
107
+ readonly perUserPerMinute: 60;
108
+ readonly windowMs: 60000;
109
+ readonly retentionMs: number;
110
+ };
@@ -1,3 +1,4 @@
1
+ import type { ToolRunAdmitInput, ToolRunAdmitResult, ToolRunExecutor, ToolRunFinishInput, ToolRunLogEntry } from './mcp/toolRunContract';
1
2
  /**
2
3
  * Contract shared by the @hugex/artifact runtime (ArtifactDO, ArtifactIndexDO, host routing) and the MCP layer.
3
4
  * Owned by the orchestrator: workers must not change these shapes without updating plans/notes/artifacts-design.md (§10 contract, workspace root).
@@ -66,6 +67,8 @@ export type ArtifactAgentProxy = (request: Request, env: any, ctx: ExecutionCont
66
67
  export interface ArtifactHostOptions {
67
68
  /** Without it, `__hx/agent/...` is a 404. */
68
69
  agentProxy?: ArtifactAgentProxy;
70
+ /** Turns on `tool_run` in the HugeX MCP server (for example `toolRunExecutor` from `@hugex/ai`). */
71
+ toolRunExecutor?: ToolRunExecutor;
69
72
  }
70
73
  /** A flow the artifact owner allowed this artifact to talk to. */
71
74
  export interface WorkerGrant {
@@ -484,6 +487,15 @@ export interface ArtifactIndexRpc {
484
487
  list(opts?: {
485
488
  owner?: string;
486
489
  }): Promise<ArtifactIndexEntry[]>;
490
+ /** `tool_run` admission: sliding-window quotas + initial audit row in one transaction. */
491
+ admitToolRun(input: ToolRunAdmitInput): Promise<ToolRunAdmitResult>;
492
+ /** Closes the audit row. `false` when the run is unknown (audit_incomplete). */
493
+ finishToolRun(input: ToolRunFinishInput): Promise<boolean>;
494
+ /** Administrative read of the audit log (newest first). */
495
+ listToolRuns(opts?: {
496
+ actor?: string;
497
+ limit?: number;
498
+ }): Promise<ToolRunLogEntry[]>;
487
499
  }
488
500
  /**
489
501
  * Headers the host router sets when forwarding a browser request to ArtifactDO.fetch().
@@ -22,7 +22,11 @@ export declare function capMemoryContent(content: string, maxTokens: number): {
22
22
  };
23
23
  export declare function isStartMemoryEnabled(data: Record<string, unknown> | undefined | null): boolean;
24
24
  export declare function isPersonalMemoryEnabled(data: Record<string, unknown> | undefined | null): boolean;
25
+ export declare function isAgentNotesEnabled(data: Record<string, unknown> | undefined | null): boolean;
26
+ export declare function isAgentOnboardingEnabled(data: Record<string, unknown> | undefined | null): boolean;
27
+ export declare function isMemoryReviewEnabled(data: Record<string, unknown> | undefined | null): boolean;
25
28
  export declare function startMemoryModel(data: Record<string, unknown> | undefined | null): string;
29
+ export declare function startMemoryReviewModel(data: Record<string, unknown> | undefined | null): string;
26
30
  export declare function startMemoryLength(data: Record<string, unknown> | undefined | null): MemoryLength;
27
31
  /** Reasoning models spend output budget on thinking; request extra tokens then cap the document. */
28
32
  export declare function memoryGenerationMaxTokens(length: MemoryLength, reasoningKind?: string): number;