@shdwsdk/cli 1.2.1 → 1.2.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.
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/codegen.js +81 -81
- package/dist/connectivity.d.ts +1 -1
- package/dist/connectivity.d.ts.map +1 -1
- package/dist/connectivity.js +1 -1
- package/dist/connectivity.js.map +1 -1
- package/dist/wizard.d.ts.map +1 -1
- package/dist/wizard.js +22 -22
- package/dist/wizard.js.map +1 -1
- package/package.json +56 -47
- package/src/bin.ts +12 -12
- package/src/codegen.ts +167 -167
- package/src/connectivity.ts +131 -131
- package/src/env.ts +144 -144
- package/src/index.ts +31 -31
- package/src/wizard.ts +266 -267
package/src/codegen.ts
CHANGED
|
@@ -1,167 +1,167 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// @shdwsdk/cli — Boilerplate Integration Code Generator
|
|
3
|
-
// Generates a pre-configured `src/lib/shdw.ts` (or custom path) wired with
|
|
4
|
-
// `ShdwTelemetryClient` and `wrapClient` for the user's selected AI model
|
|
5
|
-
// provider and cloud datacenter grid region.
|
|
6
|
-
// ============================================================================
|
|
7
|
-
|
|
8
|
-
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
9
|
-
import { dirname, resolve } from "node:path";
|
|
10
|
-
import {
|
|
11
|
-
resolveGridRegionFactor,
|
|
12
|
-
resolveModelCoefficient,
|
|
13
|
-
} from "@shdwsdk/sdk";
|
|
14
|
-
|
|
15
|
-
export interface GenerateBoilerplateParams {
|
|
16
|
-
cwd: string;
|
|
17
|
-
outputPath?: string;
|
|
18
|
-
modelPattern: string;
|
|
19
|
-
gridRegion: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface GeneratedBoilerplateResult {
|
|
23
|
-
filePath: string;
|
|
24
|
-
relativePath: string;
|
|
25
|
-
overwroteExisting: boolean;
|
|
26
|
-
providerLabel: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function buildBoilerplateSource(params: {
|
|
30
|
-
modelPattern: string;
|
|
31
|
-
gridRegion: string;
|
|
32
|
-
}): { source: string; providerLabel: string } {
|
|
33
|
-
const modelCoeff = resolveModelCoefficient(params.modelPattern);
|
|
34
|
-
const gridFactor = resolveGridRegionFactor(params.gridRegion);
|
|
35
|
-
|
|
36
|
-
const header = `// ============================================================================
|
|
37
|
-
// SHDWsdk Integration (${modelCoeff.displayName})
|
|
38
|
-
// Region: ${gridFactor.displayName} (${gridFactor.intensityGco2PerKwh} gCO2e/kWh)
|
|
39
|
-
// Strictly non-blocking: <=1500ms AbortController timeout & silent fallback.
|
|
40
|
-
// ============================================================================
|
|
41
|
-
|
|
42
|
-
`;
|
|
43
|
-
|
|
44
|
-
if (modelCoeff.provider === "anthropic") {
|
|
45
|
-
return {
|
|
46
|
-
providerLabel: "Anthropic",
|
|
47
|
-
source: `${header}import Anthropic from "@anthropic-ai/sdk";
|
|
48
|
-
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
49
|
-
|
|
50
|
-
export const shdw = new ShdwTelemetryClient({
|
|
51
|
-
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
52
|
-
endpoint: "https://shdwsdk.ai/api/v1/ingest-telemetry",
|
|
53
|
-
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
54
|
-
defaultWorkloadName: "Production Anthropic AI Service",
|
|
55
|
-
timeoutMs: 1500,
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
export const anthropic = shdw.wrapClient(new Anthropic(), {
|
|
59
|
-
provider: "anthropic",
|
|
60
|
-
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
61
|
-
defaultModel: "${params.modelPattern}",
|
|
62
|
-
});
|
|
63
|
-
`,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (modelCoeff.provider === "gemini") {
|
|
68
|
-
return {
|
|
69
|
-
providerLabel: "Google Gemini",
|
|
70
|
-
source: `${header}import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
71
|
-
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
72
|
-
|
|
73
|
-
export const shdw = new ShdwTelemetryClient({
|
|
74
|
-
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
75
|
-
endpoint: "https://shdwsdk.ai/api/v1/ingest-telemetry",
|
|
76
|
-
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
77
|
-
defaultWorkloadName: "Production Google Gemini Service",
|
|
78
|
-
timeoutMs: 1500,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY ?? "");
|
|
82
|
-
|
|
83
|
-
export const geminiModel = shdw.wrapClient(
|
|
84
|
-
genAI.getGenerativeModel({ model: "${params.modelPattern}" }),
|
|
85
|
-
{
|
|
86
|
-
provider: "gemini",
|
|
87
|
-
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
88
|
-
defaultModel: "${params.modelPattern}",
|
|
89
|
-
}
|
|
90
|
-
);
|
|
91
|
-
`,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (modelCoeff.provider === "openai") {
|
|
96
|
-
return {
|
|
97
|
-
providerLabel: "OpenAI",
|
|
98
|
-
source: `${header}import OpenAI from "openai";
|
|
99
|
-
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
100
|
-
|
|
101
|
-
export const shdw = new ShdwTelemetryClient({
|
|
102
|
-
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
103
|
-
endpoint: "https://shdwsdk.ai/api/v1/ingest-telemetry",
|
|
104
|
-
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
105
|
-
defaultWorkloadName: "Production OpenAI Service",
|
|
106
|
-
timeoutMs: 1500,
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
export const openai = shdw.wrapClient(new OpenAI(), {
|
|
110
|
-
provider: "openai",
|
|
111
|
-
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
112
|
-
defaultModel: "${params.modelPattern}",
|
|
113
|
-
});
|
|
114
|
-
`,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Custom / OpenAI-compatible (Mistral, Llama, DeepSeek, Cohere)
|
|
119
|
-
return {
|
|
120
|
-
providerLabel: modelCoeff.vendorGroup,
|
|
121
|
-
source: `${header}import OpenAI from "openai";
|
|
122
|
-
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
123
|
-
|
|
124
|
-
export const shdw = new ShdwTelemetryClient({
|
|
125
|
-
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
126
|
-
endpoint: "https://shdwsdk.ai/api/v1/ingest-telemetry",
|
|
127
|
-
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
128
|
-
defaultWorkloadName: "Production ${modelCoeff.vendorGroup} Service",
|
|
129
|
-
timeoutMs: 1500,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// Works with any OpenAI-compatible client (Mistral, vLLM, DeepSeek, Together, etc.)
|
|
133
|
-
export const aiClient = shdw.wrapClient(
|
|
134
|
-
new OpenAI({
|
|
135
|
-
apiKey: process.env.AI_PROVIDER_API_KEY ?? process.env.OPENAI_API_KEY ?? "",
|
|
136
|
-
}),
|
|
137
|
-
{
|
|
138
|
-
provider: "custom",
|
|
139
|
-
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
140
|
-
defaultModel: "${params.modelPattern}",
|
|
141
|
-
}
|
|
142
|
-
);
|
|
143
|
-
`,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function generateShdwBoilerplateFile(
|
|
148
|
-
params: GenerateBoilerplateParams
|
|
149
|
-
): GeneratedBoilerplateResult {
|
|
150
|
-
const relativePath = params.outputPath?.trim() || "src/lib/shdw.ts";
|
|
151
|
-
const filePath = resolve(params.cwd, relativePath);
|
|
152
|
-
const overwroteExisting = existsSync(filePath);
|
|
153
|
-
|
|
154
|
-
mkdirSync(dirname(filePath), { recursive: true });
|
|
155
|
-
const { source, providerLabel } = buildBoilerplateSource({
|
|
156
|
-
modelPattern: params.modelPattern,
|
|
157
|
-
gridRegion: params.gridRegion,
|
|
158
|
-
});
|
|
159
|
-
writeFileSync(filePath, source, "utf8");
|
|
160
|
-
|
|
161
|
-
return {
|
|
162
|
-
filePath,
|
|
163
|
-
relativePath,
|
|
164
|
-
overwroteExisting,
|
|
165
|
-
providerLabel,
|
|
166
|
-
};
|
|
167
|
-
}
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// @shdwsdk/cli — Boilerplate Integration Code Generator
|
|
3
|
+
// Generates a pre-configured `src/lib/shdw.ts` (or custom path) wired with
|
|
4
|
+
// `ShdwTelemetryClient` and `wrapClient` for the user's selected AI model
|
|
5
|
+
// provider and cloud datacenter grid region.
|
|
6
|
+
// ============================================================================
|
|
7
|
+
|
|
8
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { dirname, resolve } from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
resolveGridRegionFactor,
|
|
12
|
+
resolveModelCoefficient,
|
|
13
|
+
} from "@shdwsdk/sdk";
|
|
14
|
+
|
|
15
|
+
export interface GenerateBoilerplateParams {
|
|
16
|
+
cwd: string;
|
|
17
|
+
outputPath?: string;
|
|
18
|
+
modelPattern: string;
|
|
19
|
+
gridRegion: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface GeneratedBoilerplateResult {
|
|
23
|
+
filePath: string;
|
|
24
|
+
relativePath: string;
|
|
25
|
+
overwroteExisting: boolean;
|
|
26
|
+
providerLabel: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function buildBoilerplateSource(params: {
|
|
30
|
+
modelPattern: string;
|
|
31
|
+
gridRegion: string;
|
|
32
|
+
}): { source: string; providerLabel: string } {
|
|
33
|
+
const modelCoeff = resolveModelCoefficient(params.modelPattern);
|
|
34
|
+
const gridFactor = resolveGridRegionFactor(params.gridRegion);
|
|
35
|
+
|
|
36
|
+
const header = `// ============================================================================
|
|
37
|
+
// SHDWsdk Integration (${modelCoeff.displayName})
|
|
38
|
+
// Region: ${gridFactor.displayName} (${gridFactor.intensityGco2PerKwh} gCO2e/kWh)
|
|
39
|
+
// Strictly non-blocking: <=1500ms AbortController timeout & silent fallback.
|
|
40
|
+
// ============================================================================
|
|
41
|
+
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
if (modelCoeff.provider === "anthropic") {
|
|
45
|
+
return {
|
|
46
|
+
providerLabel: "Anthropic",
|
|
47
|
+
source: `${header}import Anthropic from "@anthropic-ai/sdk";
|
|
48
|
+
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
49
|
+
|
|
50
|
+
export const shdw = new ShdwTelemetryClient({
|
|
51
|
+
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
52
|
+
endpoint: "https://www.shdwsdk.ai/api/v1/ingest-telemetry",
|
|
53
|
+
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
54
|
+
defaultWorkloadName: "Production Anthropic AI Service",
|
|
55
|
+
timeoutMs: 1500,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export const anthropic = shdw.wrapClient(new Anthropic(), {
|
|
59
|
+
provider: "anthropic",
|
|
60
|
+
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
61
|
+
defaultModel: "${params.modelPattern}",
|
|
62
|
+
});
|
|
63
|
+
`,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (modelCoeff.provider === "gemini") {
|
|
68
|
+
return {
|
|
69
|
+
providerLabel: "Google Gemini",
|
|
70
|
+
source: `${header}import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
71
|
+
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
72
|
+
|
|
73
|
+
export const shdw = new ShdwTelemetryClient({
|
|
74
|
+
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
75
|
+
endpoint: "https://www.shdwsdk.ai/api/v1/ingest-telemetry",
|
|
76
|
+
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
77
|
+
defaultWorkloadName: "Production Google Gemini Service",
|
|
78
|
+
timeoutMs: 1500,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY ?? "");
|
|
82
|
+
|
|
83
|
+
export const geminiModel = shdw.wrapClient(
|
|
84
|
+
genAI.getGenerativeModel({ model: "${params.modelPattern}" }),
|
|
85
|
+
{
|
|
86
|
+
provider: "gemini",
|
|
87
|
+
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
88
|
+
defaultModel: "${params.modelPattern}",
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (modelCoeff.provider === "openai") {
|
|
96
|
+
return {
|
|
97
|
+
providerLabel: "OpenAI",
|
|
98
|
+
source: `${header}import OpenAI from "openai";
|
|
99
|
+
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
100
|
+
|
|
101
|
+
export const shdw = new ShdwTelemetryClient({
|
|
102
|
+
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
103
|
+
endpoint: "https://www.shdwsdk.ai/api/v1/ingest-telemetry",
|
|
104
|
+
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
105
|
+
defaultWorkloadName: "Production OpenAI Service",
|
|
106
|
+
timeoutMs: 1500,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
export const openai = shdw.wrapClient(new OpenAI(), {
|
|
110
|
+
provider: "openai",
|
|
111
|
+
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
112
|
+
defaultModel: "${params.modelPattern}",
|
|
113
|
+
});
|
|
114
|
+
`,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Custom / OpenAI-compatible (Mistral, Llama, DeepSeek, Cohere)
|
|
119
|
+
return {
|
|
120
|
+
providerLabel: modelCoeff.vendorGroup,
|
|
121
|
+
source: `${header}import OpenAI from "openai";
|
|
122
|
+
import { ShdwTelemetryClient } from "@shdwsdk/sdk";
|
|
123
|
+
|
|
124
|
+
export const shdw = new ShdwTelemetryClient({
|
|
125
|
+
apiKey: process.env.SHDW_API_KEY ?? "",
|
|
126
|
+
endpoint: "https://www.shdwsdk.ai/api/v1/ingest-telemetry",
|
|
127
|
+
defaultGridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
128
|
+
defaultWorkloadName: "Production ${modelCoeff.vendorGroup} Service",
|
|
129
|
+
timeoutMs: 1500,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Works with any OpenAI-compatible client (Mistral, vLLM, DeepSeek, Together, etc.)
|
|
133
|
+
export const aiClient = shdw.wrapClient(
|
|
134
|
+
new OpenAI({
|
|
135
|
+
apiKey: process.env.AI_PROVIDER_API_KEY ?? process.env.OPENAI_API_KEY ?? "",
|
|
136
|
+
}),
|
|
137
|
+
{
|
|
138
|
+
provider: "custom",
|
|
139
|
+
gridRegion: process.env.SHDW_GRID_REGION ?? "${gridFactor.region}",
|
|
140
|
+
defaultModel: "${params.modelPattern}",
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
`,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function generateShdwBoilerplateFile(
|
|
148
|
+
params: GenerateBoilerplateParams
|
|
149
|
+
): GeneratedBoilerplateResult {
|
|
150
|
+
const relativePath = params.outputPath?.trim() || "src/lib/shdw.ts";
|
|
151
|
+
const filePath = resolve(params.cwd, relativePath);
|
|
152
|
+
const overwroteExisting = existsSync(filePath);
|
|
153
|
+
|
|
154
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
155
|
+
const { source, providerLabel } = buildBoilerplateSource({
|
|
156
|
+
modelPattern: params.modelPattern,
|
|
157
|
+
gridRegion: params.gridRegion,
|
|
158
|
+
});
|
|
159
|
+
writeFileSync(filePath, source, "utf8");
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
filePath,
|
|
163
|
+
relativePath,
|
|
164
|
+
overwroteExisting,
|
|
165
|
+
providerLabel,
|
|
166
|
+
};
|
|
167
|
+
}
|
package/src/connectivity.ts
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// @shdwsdk/cli — Live Connectivity Validation Ping
|
|
3
|
-
// Sends a lightweight validation ping to the SHDWsdk ingestion endpoint using
|
|
4
|
-
// an AbortController strictly clamped to <= 1500ms.
|
|
5
|
-
// ============================================================================
|
|
6
|
-
|
|
7
|
-
import { calculateFootprint, resolveModelCoefficient } from "@shdwsdk/sdk";
|
|
8
|
-
|
|
9
|
-
export const DEFAULT_SHDW_INGEST_ENDPOINT =
|
|
10
|
-
"https://shdwsdk.ai/api/v1/ingest-telemetry";
|
|
11
|
-
const MAX_CONNECTIVITY_TIMEOUT_MS = 1500;
|
|
12
|
-
|
|
13
|
-
export interface ConnectivityTestResult {
|
|
14
|
-
ok: boolean;
|
|
15
|
-
status: number | null;
|
|
16
|
-
latencyMs: number;
|
|
17
|
-
endpoint: string;
|
|
18
|
-
message: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export async function testShdwConnectivity(params: {
|
|
22
|
-
apiKey: string;
|
|
23
|
-
modelPattern: string;
|
|
24
|
-
gridRegion: string;
|
|
25
|
-
endpoint?: string;
|
|
26
|
-
timeoutMs?: number;
|
|
27
|
-
customFetch?: typeof fetch;
|
|
28
|
-
}): Promise<ConnectivityTestResult> {
|
|
29
|
-
const endpoint = params.endpoint ?? DEFAULT_SHDW_INGEST_ENDPOINT;
|
|
30
|
-
const timeoutMs = Math.min(
|
|
31
|
-
MAX_CONNECTIVITY_TIMEOUT_MS,
|
|
32
|
-
Math.max(100, params.timeoutMs ?? MAX_CONNECTIVITY_TIMEOUT_MS)
|
|
33
|
-
);
|
|
34
|
-
const fetchImpl = params.customFetch ?? globalThis.fetch.bind(globalThis);
|
|
35
|
-
|
|
36
|
-
const coeff = resolveModelCoefficient(params.modelPattern);
|
|
37
|
-
const footprint = calculateFootprint({
|
|
38
|
-
modelName: params.modelPattern,
|
|
39
|
-
promptTokens: 1,
|
|
40
|
-
completionTokens: 1,
|
|
41
|
-
gridRegion: params.gridRegion,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const controller = new AbortController();
|
|
45
|
-
const timeoutId = setTimeout(() => {
|
|
46
|
-
try {
|
|
47
|
-
controller.abort();
|
|
48
|
-
} catch {
|
|
49
|
-
// Ignore abort errors
|
|
50
|
-
}
|
|
51
|
-
}, timeoutMs);
|
|
52
|
-
|
|
53
|
-
const startedAt = Date.now();
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
const res = await fetchImpl(endpoint, {
|
|
57
|
-
method: "POST",
|
|
58
|
-
headers: {
|
|
59
|
-
"Content-Type": "application/json",
|
|
60
|
-
"X-SHDW-Key": params.apiKey,
|
|
61
|
-
"X-SHDW-SDK-Version": "1.2.0-cli-init",
|
|
62
|
-
},
|
|
63
|
-
body: JSON.stringify({
|
|
64
|
-
events: [
|
|
65
|
-
{
|
|
66
|
-
model_name: params.modelPattern,
|
|
67
|
-
provider: coeff.provider,
|
|
68
|
-
prompt_tokens: 1,
|
|
69
|
-
completion_tokens: 1,
|
|
70
|
-
estimated_energy_kwh: footprint.estimatedEnergyKwh,
|
|
71
|
-
carbon_gco2e: footprint.carbonGco2e,
|
|
72
|
-
grid_region: footprint.gridRegion,
|
|
73
|
-
grid_intensity_gco2_kwh: footprint.gridIntensityGco2PerKwh,
|
|
74
|
-
is_streaming: false,
|
|
75
|
-
latency_ms: 1,
|
|
76
|
-
timestamp: new Date().toISOString(),
|
|
77
|
-
workload_name: "CLI Setup Verification Ping",
|
|
78
|
-
},
|
|
79
|
-
],
|
|
80
|
-
}),
|
|
81
|
-
signal: controller.signal,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const latencyMs = Math.max(1, Date.now() - startedAt);
|
|
85
|
-
|
|
86
|
-
if (res.ok || res.status === 202) {
|
|
87
|
-
return {
|
|
88
|
-
ok: true,
|
|
89
|
-
status: res.status,
|
|
90
|
-
latencyMs,
|
|
91
|
-
endpoint,
|
|
92
|
-
message: `Connected (${res.status}) in ${latencyMs}ms — API key and telemetry route verified.`,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (res.status === 401 || res.status === 403) {
|
|
97
|
-
return {
|
|
98
|
-
ok: false,
|
|
99
|
-
status: res.status,
|
|
100
|
-
latencyMs,
|
|
101
|
-
endpoint,
|
|
102
|
-
message: `Endpoint reachable (${res.status}), but API key was rejected. Verify your shdw_live_* key in the Dashboard.`,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
ok: false,
|
|
108
|
-
status: res.status,
|
|
109
|
-
latencyMs,
|
|
110
|
-
endpoint,
|
|
111
|
-
message: `Endpoint returned HTTP ${res.status} in ${latencyMs}ms. Local config was saved; telemetry will fail silently until the endpoint recovers.`,
|
|
112
|
-
};
|
|
113
|
-
} catch (err: unknown) {
|
|
114
|
-
const latencyMs = Math.max(1, Date.now() - startedAt);
|
|
115
|
-
const isAbort =
|
|
116
|
-
err instanceof Error &&
|
|
117
|
-
(err.name === "AbortError" || err.message.toLowerCase().includes("abort"));
|
|
118
|
-
|
|
119
|
-
return {
|
|
120
|
-
ok: false,
|
|
121
|
-
status: null,
|
|
122
|
-
latencyMs,
|
|
123
|
-
endpoint,
|
|
124
|
-
message: isAbort
|
|
125
|
-
? `Connectivity check timed out after ${timeoutMs}ms (non-blocking guard active). Local config was saved.`
|
|
126
|
-
: `Could not reach ${endpoint} (${err instanceof Error ? err.message : "network error"}). Local config was saved.`,
|
|
127
|
-
};
|
|
128
|
-
} finally {
|
|
129
|
-
clearTimeout(timeoutId);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// @shdwsdk/cli — Live Connectivity Validation Ping
|
|
3
|
+
// Sends a lightweight validation ping to the SHDWsdk ingestion endpoint using
|
|
4
|
+
// an AbortController strictly clamped to <= 1500ms.
|
|
5
|
+
// ============================================================================
|
|
6
|
+
|
|
7
|
+
import { calculateFootprint, resolveModelCoefficient } from "@shdwsdk/sdk";
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_SHDW_INGEST_ENDPOINT =
|
|
10
|
+
"https://www.shdwsdk.ai/api/v1/ingest-telemetry";
|
|
11
|
+
const MAX_CONNECTIVITY_TIMEOUT_MS = 1500;
|
|
12
|
+
|
|
13
|
+
export interface ConnectivityTestResult {
|
|
14
|
+
ok: boolean;
|
|
15
|
+
status: number | null;
|
|
16
|
+
latencyMs: number;
|
|
17
|
+
endpoint: string;
|
|
18
|
+
message: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function testShdwConnectivity(params: {
|
|
22
|
+
apiKey: string;
|
|
23
|
+
modelPattern: string;
|
|
24
|
+
gridRegion: string;
|
|
25
|
+
endpoint?: string;
|
|
26
|
+
timeoutMs?: number;
|
|
27
|
+
customFetch?: typeof fetch;
|
|
28
|
+
}): Promise<ConnectivityTestResult> {
|
|
29
|
+
const endpoint = params.endpoint ?? DEFAULT_SHDW_INGEST_ENDPOINT;
|
|
30
|
+
const timeoutMs = Math.min(
|
|
31
|
+
MAX_CONNECTIVITY_TIMEOUT_MS,
|
|
32
|
+
Math.max(100, params.timeoutMs ?? MAX_CONNECTIVITY_TIMEOUT_MS)
|
|
33
|
+
);
|
|
34
|
+
const fetchImpl = params.customFetch ?? globalThis.fetch.bind(globalThis);
|
|
35
|
+
|
|
36
|
+
const coeff = resolveModelCoefficient(params.modelPattern);
|
|
37
|
+
const footprint = calculateFootprint({
|
|
38
|
+
modelName: params.modelPattern,
|
|
39
|
+
promptTokens: 1,
|
|
40
|
+
completionTokens: 1,
|
|
41
|
+
gridRegion: params.gridRegion,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const controller = new AbortController();
|
|
45
|
+
const timeoutId = setTimeout(() => {
|
|
46
|
+
try {
|
|
47
|
+
controller.abort();
|
|
48
|
+
} catch {
|
|
49
|
+
// Ignore abort errors
|
|
50
|
+
}
|
|
51
|
+
}, timeoutMs);
|
|
52
|
+
|
|
53
|
+
const startedAt = Date.now();
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const res = await fetchImpl(endpoint, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: {
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
"X-SHDW-Key": params.apiKey,
|
|
61
|
+
"X-SHDW-SDK-Version": "1.2.0-cli-init",
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
events: [
|
|
65
|
+
{
|
|
66
|
+
model_name: params.modelPattern,
|
|
67
|
+
provider: coeff.provider,
|
|
68
|
+
prompt_tokens: 1,
|
|
69
|
+
completion_tokens: 1,
|
|
70
|
+
estimated_energy_kwh: footprint.estimatedEnergyKwh,
|
|
71
|
+
carbon_gco2e: footprint.carbonGco2e,
|
|
72
|
+
grid_region: footprint.gridRegion,
|
|
73
|
+
grid_intensity_gco2_kwh: footprint.gridIntensityGco2PerKwh,
|
|
74
|
+
is_streaming: false,
|
|
75
|
+
latency_ms: 1,
|
|
76
|
+
timestamp: new Date().toISOString(),
|
|
77
|
+
workload_name: "CLI Setup Verification Ping",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
}),
|
|
81
|
+
signal: controller.signal,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const latencyMs = Math.max(1, Date.now() - startedAt);
|
|
85
|
+
|
|
86
|
+
if (res.ok || res.status === 202) {
|
|
87
|
+
return {
|
|
88
|
+
ok: true,
|
|
89
|
+
status: res.status,
|
|
90
|
+
latencyMs,
|
|
91
|
+
endpoint,
|
|
92
|
+
message: `Connected (${res.status}) in ${latencyMs}ms — API key and telemetry route verified.`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (res.status === 401 || res.status === 403) {
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
status: res.status,
|
|
100
|
+
latencyMs,
|
|
101
|
+
endpoint,
|
|
102
|
+
message: `Endpoint reachable (${res.status}), but API key was rejected. Verify your shdw_live_* key in the Dashboard.`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
ok: false,
|
|
108
|
+
status: res.status,
|
|
109
|
+
latencyMs,
|
|
110
|
+
endpoint,
|
|
111
|
+
message: `Endpoint returned HTTP ${res.status} in ${latencyMs}ms. Local config was saved; telemetry will fail silently until the endpoint recovers.`,
|
|
112
|
+
};
|
|
113
|
+
} catch (err: unknown) {
|
|
114
|
+
const latencyMs = Math.max(1, Date.now() - startedAt);
|
|
115
|
+
const isAbort =
|
|
116
|
+
err instanceof Error &&
|
|
117
|
+
(err.name === "AbortError" || err.message.toLowerCase().includes("abort"));
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
ok: false,
|
|
121
|
+
status: null,
|
|
122
|
+
latencyMs,
|
|
123
|
+
endpoint,
|
|
124
|
+
message: isAbort
|
|
125
|
+
? `Connectivity check timed out after ${timeoutMs}ms (non-blocking guard active). Local config was saved.`
|
|
126
|
+
: `Could not reach ${endpoint} (${err instanceof Error ? err.message : "network error"}). Local config was saved.`,
|
|
127
|
+
};
|
|
128
|
+
} finally {
|
|
129
|
+
clearTimeout(timeoutId);
|
|
130
|
+
}
|
|
131
|
+
}
|