
Product
Introducing Pull Request Stories to Help Security Teams Track Supply Chain Risks
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
@clavata/sdk
Advanced tools
This directory contains the Clavata JavaScript SDK with type definitions.
Email support@clavata.ai if you do not already have an API key/token.
Initialize the SDK to get started making requests to the Clavata API.
import Clavata from "@clavata/sdk";
// API key can be provided directly or via environment variable
const clavata = new Clavata({ apiKey: "<YOUR_CLAVATA_API_KEY>" } as Options);
The client constructor accepts the following options:
interface Options {
// Your API key. If not provided you _must_ then set CLAVATA_API_KEY as an environment variable
apiKey?: string;
// Optional
server?: string; // Server endpoint (default: "gateway.app.clavata.ai:443")
insecure?: boolean; // Use insecure connection. (default: false)
keepalive?: {
// Connection keepalive settings
timeout: number; // How long to wait for a response to a keepalive ping before considering the connection dead (in ms). (default: 10000)
permitWithoutCalls: boolean; // Allows keepalive pings to maintain idle connections. (default: true)
time: number; // How frequently to send keepalive pings to check if the connection is alive (in ms). (default: 10000)
};
maxMessageLength?: number; // Max message size (default: 100MB)
retryConfig:
| {
// Configuration for automatic retries. Will retry by default. Set to `false` to disable this. See https://grpc.io/docs/guides/retry for more info.
maxAttempts?: number; // Maximum number of retry attempts (default: 5)
initialBackoff?: string; // Initial backoff time in milliseconds (default: "1s")
maxBackoff?: string; // Maximum backoff time (default: "30s")
backoffMultiplier?: number; // Multiplier for backoff time after each retry (default: 2.0)
jitter?: number; // Jitter factor between 0 and 1 (default: 0.2, 0 = no jitter, 1 = full jitter)
}
| false;
}
If neither an API key is provided to the constructor nor is a process.env.CLAVATA_API_KEY
environment variable set then the constructor will throw an error.
getJob(jobId: string): Promise<Job | undefined>
listJobs(request: ListJobsRequest): Promise<ListJobsResponse>
createJob(request: CreateJobRequest): Promise<CreateJobResponse[]>
evaluate
instead.evaluate(request: EvaluateRequest): Promise<AsyncIterable<EvaluateMessage | StreamError>>
const stream = clavata.evaluate({
contentData: [
{
metadata: { id: "1" },
content: { value: "Hello, world!", $case: "text" },
contentType: "text",
},
{
metadata: { id: "2" },
content: { value: Buffer.from("<BASE64_DATA>", "base64"), $case: "image" },
contentType: "image/png",
},
{
metadata: { id: "3" },
content: { value: "<IMAGE_URL>", $case: "imageUrl" },
contentType: "image/jpeg",
},
],
policyId: "00000000-0000-0000-0000-000000000000",
});
for await (const item of stream) {
if (item instanceof StreamError) throw item;
const { metadata, report } = item;
console.log({ id: metadata?.id, matches: report?.matches });
}
Under certain circumstances, the Clavata API will refuse to evaluate content as requested. There are only a few possible reasons for this:
webp
, png
and jpg
images.When a refusal occurs, a StreamError
will be returned (for evaluate
) or thrown (for createJob
). You can check whether the error was caused by a refusal using the getRefusalReason()
method on the StreamError
type. If the error was not caused by a refusal, this method will return undefined
. If the error is due to a refusal, a RefusalReason
will be returned.
The RefusalReason
type is a enum with strings matching one of:
"CSAM"
"UNSUPPORTED_IMAGE_FORMAT"
"INVALID_IMAGE"
There is also an "UNKNOWN"
option, but this case indicates something went wrong decoding the "reason" information in the response.
import { RefusalReason } from "@clavata/sdk";
const stream = clavata.evaluate(...)
for await (const item of stream) {
if (item instanceof StreamError) {
const reason = item.getRefusalReason();
if (!reason) {
// Not a refusal, you can check the other possible identification methods like
if (item.isTransient()) {
// Transient error so we can retry
}
}
// It was a refusal, so we can use a switch statement to figure out why
switch (reason) {
case RefusalReason.CSAM:
// Handle CSAM appropriately
break;
case RefusalReason.UNSUPPORTED_IMAGE_FORMAT:
// Convert image and try again
break;
case RefusalReason.INVALID_IMAGE:
// data corruption, maybe try to re-download from CDN and try again?
break;
default:
// unknown reason for refusal
}
}
}
If you prefer, you can convert a StreamError
into a RefusalError
with the toRefusalError()
method. Once again, if the error was not caused by a refusal, this method will return undefined
. If, however, a refusal did occur the RefusalError
type that is returned has the following interface that you can use to understand why:
interface RefusalError {
isCSAM(): boolean;
isUnsupportedImageFormat(): boolean;
isInvalidImage(): boolean;
isUnknown(): boolean;
getReason(): RefusalReason;
}
// So you can do this:
if (item instanceof StreamError) {
const refusalErr = item.toRefusalError();
if (refusalErr.isCSAM()) {
// Take action based on the content having been CSAM
}
}
Which interface you use to determine the reason is up to you, they are functionally equivalent.
createJob
When calling createJob
, a promise is returned. If an error occurs, that error is automatically converted to a StreamError
by the SDK and re-thrown. You can then catch the StreamError
and check for refusals the same way.
Update the version with npm version
to the next SEMVER then run npm publish
.
FAQs
This directory contains the JS version of our SDK.
The npm package @clavata/sdk receives a total of 545 weekly downloads. As such, @clavata/sdk popularity was classified as not popular.
We found that @clavata/sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.