@usestratus/mcp-aws 0.1.0
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.
- package/README.md +610 -0
- package/dist/auth/api-key.d.ts +29 -0
- package/dist/auth/api-key.d.ts.map +1 -0
- package/dist/auth/api-key.js +42 -0
- package/dist/auth/api-key.js.map +1 -0
- package/dist/auth/cognito.d.ts +26 -0
- package/dist/auth/cognito.d.ts.map +1 -0
- package/dist/auth/cognito.js +58 -0
- package/dist/auth/cognito.js.map +1 -0
- package/dist/auth/index.d.ts +11 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +21 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/metadata.d.ts +30 -0
- package/dist/auth/metadata.d.ts.map +1 -0
- package/dist/auth/metadata.js +25 -0
- package/dist/auth/metadata.js.map +1 -0
- package/dist/auth/types.d.ts +8 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +2 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/codemode/executor.d.ts +29 -0
- package/dist/codemode/executor.d.ts.map +1 -0
- package/dist/codemode/executor.js +154 -0
- package/dist/codemode/executor.js.map +1 -0
- package/dist/codemode/index.d.ts +4 -0
- package/dist/codemode/index.d.ts.map +1 -0
- package/dist/codemode/index.js +3 -0
- package/dist/codemode/index.js.map +1 -0
- package/dist/codemode/types.d.ts +10 -0
- package/dist/codemode/types.d.ts.map +1 -0
- package/dist/codemode/types.js +195 -0
- package/dist/codemode/types.js.map +1 -0
- package/dist/compose.d.ts +57 -0
- package/dist/compose.d.ts.map +1 -0
- package/dist/compose.js +138 -0
- package/dist/compose.js.map +1 -0
- package/dist/context.d.ts +22 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +39 -0
- package/dist/context.js.map +1 -0
- package/dist/deploy.d.ts +57 -0
- package/dist/deploy.d.ts.map +1 -0
- package/dist/deploy.js +281 -0
- package/dist/deploy.js.map +1 -0
- package/dist/disclosure/index.d.ts +3 -0
- package/dist/disclosure/index.d.ts.map +1 -0
- package/dist/disclosure/index.js +3 -0
- package/dist/disclosure/index.js.map +1 -0
- package/dist/disclosure/search.d.ts +15 -0
- package/dist/disclosure/search.d.ts.map +1 -0
- package/dist/disclosure/search.js +73 -0
- package/dist/disclosure/search.js.map +1 -0
- package/dist/disclosure/tier.d.ts +16 -0
- package/dist/disclosure/tier.d.ts.map +1 -0
- package/dist/disclosure/tier.js +69 -0
- package/dist/disclosure/tier.js.map +1 -0
- package/dist/errors.d.ts +34 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +56 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +93 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +28 -0
- package/dist/events.js.map +1 -0
- package/dist/gating/combinators.d.ts +10 -0
- package/dist/gating/combinators.d.ts.map +1 -0
- package/dist/gating/combinators.js +34 -0
- package/dist/gating/combinators.js.map +1 -0
- package/dist/gating/gates.d.ts +21 -0
- package/dist/gating/gates.d.ts.map +1 -0
- package/dist/gating/gates.js +74 -0
- package/dist/gating/gates.js.map +1 -0
- package/dist/gating/index.d.ts +3 -0
- package/dist/gating/index.d.ts.map +1 -0
- package/dist/gating/index.js +3 -0
- package/dist/gating/index.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +161 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +768 -0
- package/dist/server.js.map +1 -0
- package/dist/session/dynamo.d.ts +23 -0
- package/dist/session/dynamo.d.ts.map +1 -0
- package/dist/session/dynamo.js +92 -0
- package/dist/session/dynamo.js.map +1 -0
- package/dist/session/index.d.ts +4 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +4 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/memory.d.ts +16 -0
- package/dist/session/memory.d.ts.map +1 -0
- package/dist/session/memory.js +43 -0
- package/dist/session/memory.js.map +1 -0
- package/dist/session/sqlite.d.ts +17 -0
- package/dist/session/sqlite.d.ts.map +1 -0
- package/dist/session/sqlite.js +79 -0
- package/dist/session/sqlite.js.map +1 -0
- package/dist/ssrf.d.ts +25 -0
- package/dist/ssrf.d.ts.map +1 -0
- package/dist/ssrf.js +88 -0
- package/dist/ssrf.js.map +1 -0
- package/dist/types.d.ts +110 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +21 -0
- package/dist/types.js.map +1 -0
- package/package.json +50 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cognito.d.ts","sourceRoot":"","sources":["../../src/auth/cognito.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5D,MAAM,MAAM,iBAAiB,GAAG;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAQF;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,iBAAiB,GAAG,YAAY,CAE/D;AAED,qBAAa,WAAY,YAAW,YAAY;IAC/C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAwC;IAC7D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,MAAM,EAAE,iBAAiB;IAQ/B,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CA+B9D"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
2
|
+
const UNAUTHENTICATED = {
|
|
3
|
+
authenticated: false,
|
|
4
|
+
roles: [],
|
|
5
|
+
claims: {},
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Factory: create a Cognito JWT auth provider.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* server.auth(cognito({ userPoolId: "us-east-1_abc", region: "us-east-1" }));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function cognito(config) {
|
|
16
|
+
return new CognitoAuth(config);
|
|
17
|
+
}
|
|
18
|
+
export class CognitoAuth {
|
|
19
|
+
issuer;
|
|
20
|
+
jwks;
|
|
21
|
+
audience;
|
|
22
|
+
rolesClaim;
|
|
23
|
+
constructor(config) {
|
|
24
|
+
this.issuer = `https://cognito-idp.${config.region}.amazonaws.com/${config.userPoolId}`;
|
|
25
|
+
const jwksUrl = new URL(`${this.issuer}/.well-known/jwks.json`);
|
|
26
|
+
this.jwks = createRemoteJWKSet(jwksUrl);
|
|
27
|
+
this.audience = config.audience;
|
|
28
|
+
this.rolesClaim = config.rolesClaim ?? "cognito:groups";
|
|
29
|
+
}
|
|
30
|
+
async authenticate(request) {
|
|
31
|
+
const raw = request.headers.authorization ?? request.headers.Authorization;
|
|
32
|
+
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
33
|
+
if (!value) {
|
|
34
|
+
return UNAUTHENTICATED;
|
|
35
|
+
}
|
|
36
|
+
const token = value.startsWith("Bearer ") ? value.slice(7) : value;
|
|
37
|
+
try {
|
|
38
|
+
const { payload } = await jwtVerify(token, this.jwks, {
|
|
39
|
+
issuer: this.issuer,
|
|
40
|
+
audience: this.audience,
|
|
41
|
+
});
|
|
42
|
+
const rolesClaim = payload[this.rolesClaim];
|
|
43
|
+
const roles = Array.isArray(rolesClaim)
|
|
44
|
+
? rolesClaim.filter((r) => typeof r === "string")
|
|
45
|
+
: [];
|
|
46
|
+
return {
|
|
47
|
+
authenticated: true,
|
|
48
|
+
subject: payload.sub,
|
|
49
|
+
roles,
|
|
50
|
+
claims: payload,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return UNAUTHENTICATED;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=cognito.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cognito.js","sourceRoot":"","sources":["../../src/auth/cognito.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAWrD,MAAM,eAAe,GAAgB;IACpC,aAAa,EAAE,KAAK;IACpB,KAAK,EAAE,EAAE;IACT,MAAM,EAAE,EAAE;CACV,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,OAAO,CAAC,MAAyB;IAChD,OAAO,IAAI,WAAW,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,OAAO,WAAW;IACN,MAAM,CAAS;IACf,IAAI,CAAwC;IAC5C,QAAQ,CAAU;IAClB,UAAU,CAAS;IAEpC,YAAY,MAAyB;QACpC,IAAI,CAAC,MAAM,GAAG,uBAAuB,MAAM,CAAC,MAAM,kBAAkB,MAAM,CAAC,UAAU,EAAE,CAAC;QACxF,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,wBAAwB,CAAC,CAAC;QAChE,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;QAChC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,gBAAgB,CAAC;IACzD,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,OAAoB;QACtC,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;QAC3E,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;QAEhD,IAAI,CAAC,KAAK,EAAE,CAAC;YACZ,OAAO,eAAe,CAAC;QACxB,CAAC;QAED,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QAEnE,IAAI,CAAC;YACJ,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE;gBACrD,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,QAAQ,EAAE,IAAI,CAAC,QAAQ;aACvB,CAAC,CAAC;YAEH,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5C,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC;gBACtC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC;gBAC9D,CAAC,CAAC,EAAE,CAAC;YAEN,OAAO;gBACN,aAAa,EAAE,IAAI;gBACnB,OAAO,EAAE,OAAO,CAAC,GAAG;gBACpB,KAAK;gBACL,MAAM,EAAE,OAAkC;aAC1C,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,eAAe,CAAC;QACxB,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AuthProvider } from "./types.js";
|
|
2
|
+
export { apiKey, ApiKeyAuth, type ApiKeyAuthConfig, type ApiKeyEntry } from "./api-key.js";
|
|
3
|
+
export { cognito, CognitoAuth, type CognitoAuthConfig } from "./cognito.js";
|
|
4
|
+
export { buildResourceMetadata, buildWwwAuthenticateHeader, type ProtectedResourceMetadata, type ResourceMetadataConfig, } from "./metadata.js";
|
|
5
|
+
export type { AuthProvider, AuthRequest } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Tries each provider in order. Returns the first successful (authenticated) result.
|
|
8
|
+
* If none succeed, returns unauthenticated.
|
|
9
|
+
*/
|
|
10
|
+
export declare function chainAuth(...providers: AuthProvider[]): AuthProvider;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,YAAY,CAAC;AAE5D,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3F,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC5E,OAAO,EACN,qBAAqB,EACrB,0BAA0B,EAC1B,KAAK,yBAAyB,EAC9B,KAAK,sBAAsB,GAC3B,MAAM,eAAe,CAAC;AACvB,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE5D;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,SAAS,EAAE,YAAY,EAAE,GAAG,YAAY,CAYpE"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { apiKey, ApiKeyAuth } from "./api-key.js";
|
|
2
|
+
export { cognito, CognitoAuth } from "./cognito.js";
|
|
3
|
+
export { buildResourceMetadata, buildWwwAuthenticateHeader, } from "./metadata.js";
|
|
4
|
+
/**
|
|
5
|
+
* Tries each provider in order. Returns the first successful (authenticated) result.
|
|
6
|
+
* If none succeed, returns unauthenticated.
|
|
7
|
+
*/
|
|
8
|
+
export function chainAuth(...providers) {
|
|
9
|
+
return {
|
|
10
|
+
async authenticate(request) {
|
|
11
|
+
for (const provider of providers) {
|
|
12
|
+
const result = await provider.authenticate(request);
|
|
13
|
+
if (result.authenticated) {
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return { authenticated: false, roles: [], claims: {} };
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,UAAU,EAA2C,MAAM,cAAc,CAAC;AAC3F,OAAO,EAAE,OAAO,EAAE,WAAW,EAA0B,MAAM,cAAc,CAAC;AAC5E,OAAO,EACN,qBAAqB,EACrB,0BAA0B,GAG1B,MAAM,eAAe,CAAC;AAGvB;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,GAAG,SAAyB;IACrD,OAAO;QACN,KAAK,CAAC,YAAY,CAAC,OAAoB;YACtC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;gBAClC,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;gBACpD,IAAI,MAAM,CAAC,aAAa,EAAE,CAAC;oBAC1B,OAAO,MAAM,CAAC;gBACf,CAAC;YACF,CAAC;YACD,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;QACxD,CAAC;KACD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 9728 OAuth 2.0 Protected Resource Metadata.
|
|
3
|
+
* Lets MCP clients discover which authorization server to use.
|
|
4
|
+
*/
|
|
5
|
+
export type ProtectedResourceMetadata = {
|
|
6
|
+
resource: string;
|
|
7
|
+
authorization_servers: string[];
|
|
8
|
+
bearer_methods_supported?: string[];
|
|
9
|
+
scopes_supported?: string[];
|
|
10
|
+
};
|
|
11
|
+
export type ResourceMetadataConfig = {
|
|
12
|
+
/** The base URL of this MCP server (e.g. "https://api.example.com") */
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
/** The MCP endpoint path (default: "/mcp") */
|
|
15
|
+
mcpPath?: string;
|
|
16
|
+
/** Authorization server URLs (e.g. Cognito issuer URL) */
|
|
17
|
+
authorizationServers: string[];
|
|
18
|
+
/** Supported scopes (default: ["openid"]) */
|
|
19
|
+
scopes?: string[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Build the RFC 9728 Protected Resource Metadata document.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildResourceMetadata(config: ResourceMetadataConfig): ProtectedResourceMetadata;
|
|
25
|
+
/**
|
|
26
|
+
* Build the WWW-Authenticate header value for a 401 response.
|
|
27
|
+
* Includes the resource_metadata URL per RFC 9728.
|
|
28
|
+
*/
|
|
29
|
+
export declare function buildWwwAuthenticateHeader(baseUrl: string): string;
|
|
30
|
+
//# sourceMappingURL=metadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../../src/auth/metadata.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,yBAAyB,GAAG;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,qBAAqB,EAAE,MAAM,EAAE,CAAC;IAChC,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAC;IACpC,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACpC,uEAAuE;IACvE,OAAO,EAAE,MAAM,CAAC;IAChB,8CAA8C;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0DAA0D;IAC1D,oBAAoB,EAAE,MAAM,EAAE,CAAC;IAC/B,6CAA6C;IAC7C,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAEF;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,sBAAsB,GAAG,yBAAyB,CAQ/F;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAGlE"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 9728 OAuth 2.0 Protected Resource Metadata.
|
|
3
|
+
* Lets MCP clients discover which authorization server to use.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Build the RFC 9728 Protected Resource Metadata document.
|
|
7
|
+
*/
|
|
8
|
+
export function buildResourceMetadata(config) {
|
|
9
|
+
const mcpPath = config.mcpPath ?? "/mcp";
|
|
10
|
+
return {
|
|
11
|
+
resource: `${config.baseUrl}${mcpPath}`,
|
|
12
|
+
authorization_servers: config.authorizationServers,
|
|
13
|
+
bearer_methods_supported: ["header"],
|
|
14
|
+
scopes_supported: config.scopes ?? ["openid"],
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Build the WWW-Authenticate header value for a 401 response.
|
|
19
|
+
* Includes the resource_metadata URL per RFC 9728.
|
|
20
|
+
*/
|
|
21
|
+
export function buildWwwAuthenticateHeader(baseUrl) {
|
|
22
|
+
const metadataUrl = `${baseUrl}/.well-known/oauth-protected-resource`;
|
|
23
|
+
return `Bearer realm="mcp-server", resource_metadata="${metadataUrl}"`;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=metadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metadata.js","sourceRoot":"","sources":["../../src/auth/metadata.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAoBH;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAA8B;IACnE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC;IACzC,OAAO;QACN,QAAQ,EAAE,GAAG,MAAM,CAAC,OAAO,GAAG,OAAO,EAAE;QACvC,qBAAqB,EAAE,MAAM,CAAC,oBAAoB;QAClD,wBAAwB,EAAE,CAAC,QAAQ,CAAC;QACpC,gBAAgB,EAAE,MAAM,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC;KAC7C,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CAAC,OAAe;IACzD,MAAM,WAAW,GAAG,GAAG,OAAO,uCAAuC,CAAC;IACtE,OAAO,iDAAiD,WAAW,GAAG,CAAC;AACxE,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AuthContext } from "../types.js";
|
|
2
|
+
export type AuthRequest = {
|
|
3
|
+
headers: Record<string, string | string[] | undefined>;
|
|
4
|
+
};
|
|
5
|
+
export type AuthProvider = {
|
|
6
|
+
authenticate(request: AuthRequest): Promise<AuthContext>;
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,MAAM,MAAM,WAAW,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;CACvD,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IAC1B,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACzD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code mode executor: runs LLM-generated code in a sandbox with tool access.
|
|
3
|
+
* Copied from @usestratus/sdk (no dependency link — this package stands alone).
|
|
4
|
+
*/
|
|
5
|
+
export type ExecuteResult = {
|
|
6
|
+
result: unknown;
|
|
7
|
+
error?: string;
|
|
8
|
+
logs?: string[];
|
|
9
|
+
};
|
|
10
|
+
export type Executor = {
|
|
11
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
12
|
+
};
|
|
13
|
+
export type FunctionExecutorOptions = {
|
|
14
|
+
timeout?: number;
|
|
15
|
+
};
|
|
16
|
+
export declare class FunctionExecutor implements Executor {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(options?: FunctionExecutorOptions);
|
|
19
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
20
|
+
}
|
|
21
|
+
export type WorkerExecutorOptions = {
|
|
22
|
+
timeout?: number;
|
|
23
|
+
};
|
|
24
|
+
export declare class WorkerExecutor implements Executor {
|
|
25
|
+
#private;
|
|
26
|
+
constructor(options?: WorkerExecutorOptions);
|
|
27
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/codemode/executor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,MAAM,MAAM,aAAa,GAAG;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACtB,OAAO,CACN,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,GAC3D,OAAO,CAAC,aAAa,CAAC,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACrC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,qBAAa,gBAAiB,YAAW,QAAQ;;gBAGpC,OAAO,CAAC,EAAE,uBAAuB;IAIvC,OAAO,CACZ,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,GAC3D,OAAO,CAAC,aAAa,CAAC;CAuCzB;AAID,MAAM,MAAM,qBAAqB,GAAG;IACnC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAyCF,qBAAa,cAAe,YAAW,QAAQ;;gBAGlC,OAAO,CAAC,EAAE,qBAAqB;IAIrC,OAAO,CACZ,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,GAC3D,OAAO,CAAC,aAAa,CAAC;CAwEzB"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code mode executor: runs LLM-generated code in a sandbox with tool access.
|
|
3
|
+
* Copied from @usestratus/sdk (no dependency link — this package stands alone).
|
|
4
|
+
*/
|
|
5
|
+
import { Worker } from "node:worker_threads";
|
|
6
|
+
export class FunctionExecutor {
|
|
7
|
+
#timeout;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.#timeout = options?.timeout ?? 30_000;
|
|
10
|
+
}
|
|
11
|
+
async execute(code, fns) {
|
|
12
|
+
const logs = [];
|
|
13
|
+
const consoleProxy = {
|
|
14
|
+
log: (...args) => {
|
|
15
|
+
logs.push(args.map(String).join(" "));
|
|
16
|
+
},
|
|
17
|
+
warn: (...args) => {
|
|
18
|
+
logs.push(`[warn] ${args.map(String).join(" ")}`);
|
|
19
|
+
},
|
|
20
|
+
error: (...args) => {
|
|
21
|
+
logs.push(`[error] ${args.map(String).join(" ")}`);
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
try {
|
|
25
|
+
const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor;
|
|
26
|
+
const fn = new AsyncFunction("codemode", "console", `return await (${code})()`);
|
|
27
|
+
const timeoutMs = this.#timeout;
|
|
28
|
+
const result = await Promise.race([
|
|
29
|
+
fn(fns, consoleProxy),
|
|
30
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error("Execution timed out")), timeoutMs)),
|
|
31
|
+
]);
|
|
32
|
+
return { result, logs };
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
return {
|
|
36
|
+
result: undefined,
|
|
37
|
+
error: err instanceof Error ? err.message : String(err),
|
|
38
|
+
logs,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const WORKER_BOOTSTRAP = `
|
|
44
|
+
const { parentPort } = require("worker_threads");
|
|
45
|
+
|
|
46
|
+
const codemode = new Proxy({}, {
|
|
47
|
+
get: (_, name) => async (args) => {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const callId = String(Math.random());
|
|
50
|
+
parentPort.postMessage({ type: "tool_call", callId, name: String(name), args });
|
|
51
|
+
const handler = (msg) => {
|
|
52
|
+
if (msg.callId === callId) {
|
|
53
|
+
parentPort.off("message", handler);
|
|
54
|
+
if (msg.error) reject(new Error(msg.error));
|
|
55
|
+
else resolve(msg.result);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
parentPort.on("message", handler);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const __logs = [];
|
|
64
|
+
const __console = {
|
|
65
|
+
log: (...a) => __logs.push(a.map(String).join(" ")),
|
|
66
|
+
warn: (...a) => __logs.push("[warn] " + a.map(String).join(" ")),
|
|
67
|
+
error: (...a) => __logs.push("[error] " + a.map(String).join(" ")),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
(async () => {
|
|
71
|
+
try {
|
|
72
|
+
const __code = process.env.__CODEMODE_CODE;
|
|
73
|
+
const fn = new Function("codemode", "console", "return (" + __code + ")()");
|
|
74
|
+
const result = await fn(codemode, __console);
|
|
75
|
+
parentPort.postMessage({ type: "done", result, logs: __logs });
|
|
76
|
+
} catch (err) {
|
|
77
|
+
parentPort.postMessage({ type: "done", error: err instanceof Error ? err.message : String(err), logs: __logs });
|
|
78
|
+
}
|
|
79
|
+
})();
|
|
80
|
+
`;
|
|
81
|
+
export class WorkerExecutor {
|
|
82
|
+
#timeout;
|
|
83
|
+
constructor(options) {
|
|
84
|
+
this.#timeout = options?.timeout ?? 30_000;
|
|
85
|
+
}
|
|
86
|
+
async execute(code, fns) {
|
|
87
|
+
return new Promise((resolve) => {
|
|
88
|
+
const timeoutMs = this.#timeout;
|
|
89
|
+
let settled = false;
|
|
90
|
+
const worker = new Worker(WORKER_BOOTSTRAP, {
|
|
91
|
+
eval: true,
|
|
92
|
+
env: { __CODEMODE_CODE: code },
|
|
93
|
+
});
|
|
94
|
+
const timer = setTimeout(() => {
|
|
95
|
+
if (!settled) {
|
|
96
|
+
settled = true;
|
|
97
|
+
worker.terminate();
|
|
98
|
+
resolve({ result: undefined, error: "Execution timed out" });
|
|
99
|
+
}
|
|
100
|
+
}, timeoutMs);
|
|
101
|
+
worker.on("message", async (msg) => {
|
|
102
|
+
if (msg.type === "tool_call") {
|
|
103
|
+
const { callId, name, args } = msg;
|
|
104
|
+
const fn = fns[name];
|
|
105
|
+
if (!fn) {
|
|
106
|
+
worker.postMessage({ callId, error: `Tool "${name}" not found` });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
const result = await fn(args);
|
|
111
|
+
worker.postMessage({ callId, result });
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
worker.postMessage({
|
|
115
|
+
callId,
|
|
116
|
+
error: err instanceof Error ? err.message : String(err),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else if (msg.type === "done") {
|
|
121
|
+
if (!settled) {
|
|
122
|
+
settled = true;
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
worker.terminate();
|
|
125
|
+
if (msg.error) {
|
|
126
|
+
resolve({
|
|
127
|
+
result: undefined,
|
|
128
|
+
error: msg.error,
|
|
129
|
+
logs: msg.logs,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
resolve({
|
|
134
|
+
result: msg.result,
|
|
135
|
+
logs: msg.logs,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
worker.on("error", (err) => {
|
|
142
|
+
if (!settled) {
|
|
143
|
+
settled = true;
|
|
144
|
+
clearTimeout(timer);
|
|
145
|
+
resolve({
|
|
146
|
+
result: undefined,
|
|
147
|
+
error: err instanceof Error ? err.message : String(err),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../src/codemode/executor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAmB7C,MAAM,OAAO,gBAAgB;IACnB,QAAQ,CAAS;IAE1B,YAAY,OAAiC;QAC5C,IAAI,CAAC,QAAQ,GAAG,OAAO,EAAE,OAAO,IAAI,MAAM,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,OAAO,CACZ,IAAY,EACZ,GAA6D;QAE7D,MAAM,IAAI,GAAa,EAAE,CAAC;QAE1B,MAAM,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OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;gBAC1B,IAAI,CAAC,OAAO,EAAE,CAAC;oBACd,OAAO,GAAG,IAAI,CAAC;oBACf,YAAY,CAAC,KAAK,CAAC,CAAC;oBACpB,OAAO,CAAC;wBACP,MAAM,EAAE,SAAS;wBACjB,KAAK,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;qBACvD,CAAC,CAAC;gBACJ,CAAC;YACF,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACJ,CAAC;CACD"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { Executor, ExecuteResult, FunctionExecutorOptions, WorkerExecutorOptions, } from "./executor.js";
|
|
2
|
+
export { FunctionExecutor, WorkerExecutor } from "./executor.js";
|
|
3
|
+
export { generateTypes, normalizeCode, sanitizeToolName } from "./types.js";
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/codemode/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACX,QAAQ,EACR,aAAa,EACb,uBAAuB,EACvB,qBAAqB,GACrB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/codemode/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code mode: type generation and code normalization for MCP tools.
|
|
3
|
+
* Adapted from @usestratus/sdk codemode, uses JSON Schema directly
|
|
4
|
+
* instead of depending on Zod/zodToJsonSchema.
|
|
5
|
+
*/
|
|
6
|
+
import type { ToolConfig } from "../types.js";
|
|
7
|
+
export declare function sanitizeToolName(name: string): string;
|
|
8
|
+
export declare function generateTypes(tools: ToolConfig[]): string;
|
|
9
|
+
export declare function normalizeCode(code: string): string;
|
|
10
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/codemode/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA0E9C,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAQrD;AA0ED,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,MAAM,CA+BzD;AASD,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAUlD"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code mode: type generation and code normalization for MCP tools.
|
|
3
|
+
* Adapted from @usestratus/sdk codemode, uses JSON Schema directly
|
|
4
|
+
* instead of depending on Zod/zodToJsonSchema.
|
|
5
|
+
*/
|
|
6
|
+
// ── JS reserved words ──────────────────────────────────────────────
|
|
7
|
+
const JS_RESERVED = new Set([
|
|
8
|
+
"abstract",
|
|
9
|
+
"arguments",
|
|
10
|
+
"await",
|
|
11
|
+
"boolean",
|
|
12
|
+
"break",
|
|
13
|
+
"byte",
|
|
14
|
+
"case",
|
|
15
|
+
"catch",
|
|
16
|
+
"char",
|
|
17
|
+
"class",
|
|
18
|
+
"const",
|
|
19
|
+
"continue",
|
|
20
|
+
"debugger",
|
|
21
|
+
"default",
|
|
22
|
+
"delete",
|
|
23
|
+
"do",
|
|
24
|
+
"double",
|
|
25
|
+
"else",
|
|
26
|
+
"enum",
|
|
27
|
+
"eval",
|
|
28
|
+
"export",
|
|
29
|
+
"extends",
|
|
30
|
+
"false",
|
|
31
|
+
"final",
|
|
32
|
+
"finally",
|
|
33
|
+
"float",
|
|
34
|
+
"for",
|
|
35
|
+
"function",
|
|
36
|
+
"goto",
|
|
37
|
+
"if",
|
|
38
|
+
"implements",
|
|
39
|
+
"import",
|
|
40
|
+
"in",
|
|
41
|
+
"instanceof",
|
|
42
|
+
"int",
|
|
43
|
+
"interface",
|
|
44
|
+
"let",
|
|
45
|
+
"long",
|
|
46
|
+
"native",
|
|
47
|
+
"new",
|
|
48
|
+
"null",
|
|
49
|
+
"package",
|
|
50
|
+
"private",
|
|
51
|
+
"protected",
|
|
52
|
+
"public",
|
|
53
|
+
"return",
|
|
54
|
+
"short",
|
|
55
|
+
"static",
|
|
56
|
+
"super",
|
|
57
|
+
"switch",
|
|
58
|
+
"synchronized",
|
|
59
|
+
"this",
|
|
60
|
+
"throw",
|
|
61
|
+
"throws",
|
|
62
|
+
"transient",
|
|
63
|
+
"true",
|
|
64
|
+
"try",
|
|
65
|
+
"typeof",
|
|
66
|
+
"undefined",
|
|
67
|
+
"var",
|
|
68
|
+
"void",
|
|
69
|
+
"volatile",
|
|
70
|
+
"while",
|
|
71
|
+
"with",
|
|
72
|
+
"yield",
|
|
73
|
+
]);
|
|
74
|
+
// ── Name sanitization ──────────────────────────────────────────────
|
|
75
|
+
export function sanitizeToolName(name) {
|
|
76
|
+
if (!name)
|
|
77
|
+
return "_";
|
|
78
|
+
let sanitized = name.replace(/[-.\s]/g, "_");
|
|
79
|
+
sanitized = sanitized.replace(/[^a-zA-Z0-9_$]/g, "");
|
|
80
|
+
if (!sanitized)
|
|
81
|
+
return "_";
|
|
82
|
+
if (/^[0-9]/.test(sanitized))
|
|
83
|
+
sanitized = `_${sanitized}`;
|
|
84
|
+
if (JS_RESERVED.has(sanitized))
|
|
85
|
+
sanitized = `${sanitized}_`;
|
|
86
|
+
return sanitized;
|
|
87
|
+
}
|
|
88
|
+
function toCamelCase(str) {
|
|
89
|
+
return str
|
|
90
|
+
.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())
|
|
91
|
+
.replace(/^[a-z]/, (letter) => letter.toUpperCase());
|
|
92
|
+
}
|
|
93
|
+
// ── Zod → JSON Schema (minimal) ───────────────────────────────────
|
|
94
|
+
function zodToJsonSchema(schema) {
|
|
95
|
+
// Zod v4 has .toJsonSchema(), fall back to a basic conversion
|
|
96
|
+
if ("toJsonSchema" in schema && typeof schema.toJsonSchema === "function") {
|
|
97
|
+
return schema.toJsonSchema();
|
|
98
|
+
}
|
|
99
|
+
// Fallback: try to get shape from ZodObject
|
|
100
|
+
return { type: "object" };
|
|
101
|
+
}
|
|
102
|
+
function jsonSchemaToTypeString(schema, indent, depth) {
|
|
103
|
+
if (depth > 15)
|
|
104
|
+
return "unknown";
|
|
105
|
+
const type = schema.type;
|
|
106
|
+
if (type === "string")
|
|
107
|
+
return "string";
|
|
108
|
+
if (type === "number" || type === "integer")
|
|
109
|
+
return "number";
|
|
110
|
+
if (type === "boolean")
|
|
111
|
+
return "boolean";
|
|
112
|
+
if (type === "null")
|
|
113
|
+
return "null";
|
|
114
|
+
if (type === "array") {
|
|
115
|
+
const items = schema.items;
|
|
116
|
+
if (items)
|
|
117
|
+
return `${jsonSchemaToTypeString(items, indent, depth + 1)}[]`;
|
|
118
|
+
return "unknown[]";
|
|
119
|
+
}
|
|
120
|
+
if (type === "object" || schema.properties) {
|
|
121
|
+
const props = (schema.properties ?? {});
|
|
122
|
+
const required = new Set(schema.required ?? []);
|
|
123
|
+
const lines = [];
|
|
124
|
+
for (const [propName, propSchema] of Object.entries(props)) {
|
|
125
|
+
const propType = jsonSchemaToTypeString(propSchema, `${indent} `, depth + 1);
|
|
126
|
+
const opt = required.has(propName) ? "" : "?";
|
|
127
|
+
const desc = propSchema.description;
|
|
128
|
+
if (desc) {
|
|
129
|
+
lines.push(`${indent} /** ${desc} */`);
|
|
130
|
+
}
|
|
131
|
+
lines.push(`${indent} ${propName}${opt}: ${propType};`);
|
|
132
|
+
}
|
|
133
|
+
if (lines.length === 0)
|
|
134
|
+
return "Record<string, unknown>";
|
|
135
|
+
return `{\n${lines.join("\n")}\n${indent}}`;
|
|
136
|
+
}
|
|
137
|
+
if (schema.enum) {
|
|
138
|
+
const vals = schema.enum;
|
|
139
|
+
return vals
|
|
140
|
+
.map((v) => {
|
|
141
|
+
if (v === null)
|
|
142
|
+
return "null";
|
|
143
|
+
if (typeof v === "string")
|
|
144
|
+
return `"${v}"`;
|
|
145
|
+
return String(v);
|
|
146
|
+
})
|
|
147
|
+
.join(" | ");
|
|
148
|
+
}
|
|
149
|
+
return "unknown";
|
|
150
|
+
}
|
|
151
|
+
// ── Public: generateTypes ──────────────────────────────────────────
|
|
152
|
+
export function generateTypes(tools) {
|
|
153
|
+
let availableTools = "";
|
|
154
|
+
let availableTypes = "";
|
|
155
|
+
for (const t of tools) {
|
|
156
|
+
const safeName = sanitizeToolName(t.name);
|
|
157
|
+
const camelName = toCamelCase(safeName);
|
|
158
|
+
try {
|
|
159
|
+
const jsonSchema = t.inputSchema ? zodToJsonSchema(t.inputSchema) : { type: "object" };
|
|
160
|
+
const inputType = `type ${camelName}Input = ${jsonSchemaToTypeString(jsonSchema, "", 0)}`;
|
|
161
|
+
const outputType = `type ${camelName}Output = unknown`;
|
|
162
|
+
availableTypes += `\n${inputType.trim()}`;
|
|
163
|
+
availableTypes += `\n${outputType.trim()}`;
|
|
164
|
+
const desc = t.description?.trim() ?? t.name;
|
|
165
|
+
availableTools += `\n\t/** ${desc} */`;
|
|
166
|
+
availableTools += `\n\t${safeName}: (input: ${camelName}Input) => Promise<${camelName}Output>;`;
|
|
167
|
+
availableTools += "\n";
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
availableTypes += `\ntype ${camelName}Input = unknown`;
|
|
171
|
+
availableTypes += `\ntype ${camelName}Output = unknown`;
|
|
172
|
+
availableTools += `\n\t/** ${t.name} */`;
|
|
173
|
+
availableTools += `\n\t${safeName}: (input: ${camelName}Input) => Promise<${camelName}Output>;`;
|
|
174
|
+
availableTools += "\n";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
availableTools = `\ndeclare const codemode: {${availableTools}}`;
|
|
178
|
+
return `\n${availableTypes}\n${availableTools}\n`.trim();
|
|
179
|
+
}
|
|
180
|
+
// ── Public: normalizeCode ──────────────────────────────────────────
|
|
181
|
+
function stripCodeFences(code) {
|
|
182
|
+
const match = code.match(/^```(?:js|javascript|typescript|ts|tsx|jsx)?\s*\n([\s\S]*?)```\s*$/);
|
|
183
|
+
return match?.[1] ?? code;
|
|
184
|
+
}
|
|
185
|
+
export function normalizeCode(code) {
|
|
186
|
+
const trimmed = stripCodeFences(code.trim());
|
|
187
|
+
if (!trimmed.trim())
|
|
188
|
+
return "async () => {}";
|
|
189
|
+
const source = trimmed.trim();
|
|
190
|
+
if (/^async\s*\(/.test(source) && source.includes("=>")) {
|
|
191
|
+
return source;
|
|
192
|
+
}
|
|
193
|
+
return `async () => {\n${source}\n}`;
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/codemode/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,sEAAsE;AAEtE,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC;IAC3B,UAAU;IACV,WAAW;IACX,OAAO;IACP,SAAS;IACT,OAAO;IACP,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;IACN,OAAO;IACP,OAAO;IACP,UAAU;IACV,UAAU;IACV,SAAS;IACT,QAAQ;IACR,IAAI;IACJ,QAAQ;IACR,MAAM;IACN,MAAM;IACN,MAAM;IACN,QAAQ;IACR,SAAS;IACT,OAAO;IACP,OAAO;IACP,SAAS;IACT,OAAO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