@keystrokehq/keystroke 1.0.13 → 1.0.14
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/dist/agent.cjs +116 -9
- package/dist/agent.cjs.map +1 -1
- package/dist/agent.d.cts +1 -1
- package/dist/agent.d.mts +1 -1
- package/dist/agent.mjs +116 -9
- package/dist/agent.mjs.map +1 -1
- package/dist/{index-Ohwnfidc.d.cts → index-BQEzTb7P.d.cts} +3 -3
- package/dist/index-BQEzTb7P.d.cts.map +1 -0
- package/dist/{index-DdAOmbQc.d.mts → index-ub0OqZXh.d.mts} +3 -3
- package/dist/index-ub0OqZXh.d.mts.map +1 -0
- package/dist/trigger.d.cts +1 -1
- package/dist/trigger.d.mts +1 -1
- package/package.json +3 -3
- package/dist/index-DdAOmbQc.d.mts.map +0 -1
- package/dist/index-Ohwnfidc.d.cts.map +0 -1
package/dist/agent.cjs
CHANGED
|
@@ -5325,10 +5325,29 @@ function validateToolArguments(tool, toolCall) {
|
|
|
5325
5325
|
throw new Error(errorMessage);
|
|
5326
5326
|
}
|
|
5327
5327
|
//#endregion
|
|
5328
|
-
//#region ../agent/dist/schemas-
|
|
5328
|
+
//#region ../agent/dist/schemas-T2usEGaN.mjs
|
|
5329
5329
|
const getModelLoose = require_event_stream.getModel;
|
|
5330
|
+
const DEFAULT_GATEWAY_ORIGIN = "https://ai-gateway.vercel.sh";
|
|
5330
5331
|
const AGENT_MODEL_ID_PATTERN = /^[a-z0-9-]+\/.+/;
|
|
5331
5332
|
const AgentModelIdSchema = zod.z.custom((value) => typeof value === "string" && AGENT_MODEL_ID_PATTERN.test(value), "model must be vendor/model-id");
|
|
5333
|
+
const GatewayModelPricingSchema = zod.z.object({
|
|
5334
|
+
input: zod.z.string().optional(),
|
|
5335
|
+
output: zod.z.string().optional(),
|
|
5336
|
+
input_cache_read: zod.z.string().optional()
|
|
5337
|
+
});
|
|
5338
|
+
const GatewayModelResponseSchema = zod.z.object({
|
|
5339
|
+
id: zod.z.string().min(1),
|
|
5340
|
+
name: zod.z.string().optional(),
|
|
5341
|
+
type: zod.z.string().optional(),
|
|
5342
|
+
context_window: zod.z.number().int().positive().optional(),
|
|
5343
|
+
max_tokens: zod.z.number().int().positive().optional(),
|
|
5344
|
+
tags: zod.z.array(zod.z.string()).optional(),
|
|
5345
|
+
pricing: GatewayModelPricingSchema.optional()
|
|
5346
|
+
});
|
|
5347
|
+
function parseGatewayModelResponse(body) {
|
|
5348
|
+
const parsed = GatewayModelResponseSchema.safeParse(body);
|
|
5349
|
+
return parsed.success ? parsed.data : void 0;
|
|
5350
|
+
}
|
|
5332
5351
|
function splitAgentModelId(modelId) {
|
|
5333
5352
|
const slash = modelId.indexOf("/");
|
|
5334
5353
|
return {
|
|
@@ -5359,11 +5378,95 @@ function resolveGatewayBaseUrl() {
|
|
|
5359
5378
|
const projectId = require_dist$2.getProjectScopeId();
|
|
5360
5379
|
return `${platformUrl.replace(/\/$/, "")}/internal/projects/${encodeURIComponent(projectId)}/llm`;
|
|
5361
5380
|
}
|
|
5362
|
-
function
|
|
5381
|
+
function resolveGatewayOrigin(baseUrl) {
|
|
5382
|
+
return baseUrl ?? DEFAULT_GATEWAY_ORIGIN;
|
|
5383
|
+
}
|
|
5384
|
+
function pricePerMillion(value) {
|
|
5385
|
+
if (!value) return 0;
|
|
5386
|
+
const perToken = Number(value);
|
|
5387
|
+
if (!Number.isFinite(perToken) || perToken <= 0) return 0;
|
|
5388
|
+
return perToken * 1e6;
|
|
5389
|
+
}
|
|
5390
|
+
function buildGatewayModel(raw, baseUrl) {
|
|
5391
|
+
if (raw.type && raw.type !== "language") return;
|
|
5392
|
+
const tags = raw.tags ?? [];
|
|
5393
|
+
return {
|
|
5394
|
+
id: raw.id,
|
|
5395
|
+
name: raw.name ?? raw.id,
|
|
5396
|
+
api: "anthropic-messages",
|
|
5397
|
+
provider: "vercel-ai-gateway",
|
|
5398
|
+
baseUrl: resolveGatewayOrigin(baseUrl),
|
|
5399
|
+
reasoning: tags.includes("reasoning"),
|
|
5400
|
+
input: tags.includes("vision") ? ["text", "image"] : ["text"],
|
|
5401
|
+
cost: {
|
|
5402
|
+
input: pricePerMillion(raw.pricing?.input),
|
|
5403
|
+
output: pricePerMillion(raw.pricing?.output),
|
|
5404
|
+
cacheRead: pricePerMillion(raw.pricing?.input_cache_read),
|
|
5405
|
+
cacheWrite: 0
|
|
5406
|
+
},
|
|
5407
|
+
contextWindow: raw.context_window ?? 128e3,
|
|
5408
|
+
maxTokens: raw.max_tokens ?? 8192
|
|
5409
|
+
};
|
|
5410
|
+
}
|
|
5411
|
+
const gatewayModelCache = /* @__PURE__ */ new Map();
|
|
5412
|
+
const gatewayModelPending = /* @__PURE__ */ new Map();
|
|
5413
|
+
function gatewayModelCacheKey(modelId, baseUrl) {
|
|
5414
|
+
return `${resolveGatewayOrigin(baseUrl)}::${modelId}`;
|
|
5415
|
+
}
|
|
5416
|
+
function readCachedGatewayModel(cacheKey) {
|
|
5417
|
+
const entry = gatewayModelCache.get(cacheKey);
|
|
5418
|
+
if (!entry) return { cached: false };
|
|
5419
|
+
return {
|
|
5420
|
+
cached: true,
|
|
5421
|
+
model: entry.status === "found" ? entry.model : void 0
|
|
5422
|
+
};
|
|
5423
|
+
}
|
|
5424
|
+
function writeCachedGatewayModel(cacheKey, model) {
|
|
5425
|
+
gatewayModelCache.set(cacheKey, model ? {
|
|
5426
|
+
status: "found",
|
|
5427
|
+
model
|
|
5428
|
+
} : { status: "missing" });
|
|
5429
|
+
}
|
|
5430
|
+
async function fetchGatewayModel(modelId, baseUrl) {
|
|
5431
|
+
const cacheKey = gatewayModelCacheKey(modelId, baseUrl);
|
|
5432
|
+
const cached = readCachedGatewayModel(cacheKey);
|
|
5433
|
+
if (cached.cached) return cached.model;
|
|
5434
|
+
const pending = gatewayModelPending.get(cacheKey);
|
|
5435
|
+
if (pending) return pending;
|
|
5436
|
+
const request = (async () => {
|
|
5437
|
+
const gatewayApiKey = process.env.AI_GATEWAY_API_KEY?.trim();
|
|
5438
|
+
const headers = {};
|
|
5439
|
+
if (gatewayApiKey) headers.authorization = `Bearer ${gatewayApiKey}`;
|
|
5440
|
+
const url = `${resolveGatewayOrigin(baseUrl)}/v1/models/${modelId}`;
|
|
5441
|
+
try {
|
|
5442
|
+
const response = await fetch(url, { headers });
|
|
5443
|
+
if (!response.ok) {
|
|
5444
|
+
writeCachedGatewayModel(cacheKey, void 0);
|
|
5445
|
+
return;
|
|
5446
|
+
}
|
|
5447
|
+
const raw = parseGatewayModelResponse(await response.json());
|
|
5448
|
+
if (!raw) {
|
|
5449
|
+
writeCachedGatewayModel(cacheKey, void 0);
|
|
5450
|
+
return;
|
|
5451
|
+
}
|
|
5452
|
+
const model = buildGatewayModel(raw, baseUrl);
|
|
5453
|
+
writeCachedGatewayModel(cacheKey, model);
|
|
5454
|
+
return model;
|
|
5455
|
+
} catch {
|
|
5456
|
+
writeCachedGatewayModel(cacheKey, void 0);
|
|
5457
|
+
return;
|
|
5458
|
+
} finally {
|
|
5459
|
+
gatewayModelPending.delete(cacheKey);
|
|
5460
|
+
}
|
|
5461
|
+
})();
|
|
5462
|
+
gatewayModelPending.set(cacheKey, request);
|
|
5463
|
+
return request;
|
|
5464
|
+
}
|
|
5465
|
+
async function resolveGatewayModel(modelId) {
|
|
5466
|
+
const baseUrl = resolveGatewayBaseUrl();
|
|
5363
5467
|
for (const candidate of agentModelIdCandidates(modelId)) {
|
|
5364
5468
|
const registered = getModelLoose("vercel-ai-gateway", candidate);
|
|
5365
5469
|
if (registered) {
|
|
5366
|
-
const baseUrl = resolveGatewayBaseUrl();
|
|
5367
5470
|
if (!baseUrl) return registered;
|
|
5368
5471
|
return {
|
|
5369
5472
|
...registered,
|
|
@@ -5371,6 +5474,10 @@ function resolveGatewayModel(modelId) {
|
|
|
5371
5474
|
};
|
|
5372
5475
|
}
|
|
5373
5476
|
}
|
|
5477
|
+
for (const candidate of agentModelIdCandidates(modelId)) {
|
|
5478
|
+
const fetched = await fetchGatewayModel(candidate, baseUrl);
|
|
5479
|
+
if (fetched) return fetched;
|
|
5480
|
+
}
|
|
5374
5481
|
throw new Error(`Unknown gateway model: ${modelId}`);
|
|
5375
5482
|
}
|
|
5376
5483
|
function resolveDirectModel(modelId) {
|
|
@@ -5382,7 +5489,7 @@ function resolveDirectModel(modelId) {
|
|
|
5382
5489
|
throw new Error(`Unknown model: ${modelId}`);
|
|
5383
5490
|
}
|
|
5384
5491
|
/** Resolve a user-authored model id to a pi-ai {@link Model} for the current runtime env. */
|
|
5385
|
-
function resolveAgentModel(modelId) {
|
|
5492
|
+
async function resolveAgentModel(modelId) {
|
|
5386
5493
|
if (shouldUseGatewayTransport()) return resolveGatewayModel(modelId);
|
|
5387
5494
|
return resolveDirectModel(modelId);
|
|
5388
5495
|
}
|
|
@@ -13940,7 +14047,7 @@ function normalizeTitle(raw) {
|
|
|
13940
14047
|
async function generateSessionTitle(message) {
|
|
13941
14048
|
const input = message.trim();
|
|
13942
14049
|
if (!input) return null;
|
|
13943
|
-
return normalizeTitle((await completeSimple(resolveAgentModel(TITLE_MODEL_ID), {
|
|
14050
|
+
return normalizeTitle((await completeSimple(await resolveAgentModel(TITLE_MODEL_ID), {
|
|
13944
14051
|
systemPrompt: TITLE_SYSTEM_PROMPT,
|
|
13945
14052
|
messages: [{
|
|
13946
14053
|
role: "user",
|
|
@@ -14351,7 +14458,7 @@ async function buildAgentRuntime(def, ctx, runPrompt = {}, agentSlug) {
|
|
|
14351
14458
|
return {
|
|
14352
14459
|
...createAgent({
|
|
14353
14460
|
systemPrompt,
|
|
14354
|
-
model: resolveAgentModel(def.model),
|
|
14461
|
+
model: await resolveAgentModel(def.model),
|
|
14355
14462
|
thinkingLevel,
|
|
14356
14463
|
messages: ctx.messages,
|
|
14357
14464
|
tools
|
|
@@ -14529,7 +14636,7 @@ const RESPOND_TOOL_NAME = "respond";
|
|
|
14529
14636
|
const STRUCTURED_OUTPUT_INSTRUCTION = "Respond only by calling `respond` with arguments matching the schema; do not answer in plain text.";
|
|
14530
14637
|
const MAX_OBJECT_ATTEMPTS = 3;
|
|
14531
14638
|
/** Same validation as `defineAgent` — format check before catalog lookup. */
|
|
14532
|
-
function resolvePromptModel(model) {
|
|
14639
|
+
async function resolvePromptModel(model) {
|
|
14533
14640
|
return resolveAgentModel(AgentModelIdSchema.parse(model));
|
|
14534
14641
|
}
|
|
14535
14642
|
/**
|
|
@@ -14580,7 +14687,7 @@ async function recordUsageFromMessage(message, hooks) {
|
|
|
14580
14687
|
if (usage) await hooks?.onUsage?.(usage);
|
|
14581
14688
|
}
|
|
14582
14689
|
async function runPromptText(opts, hooks) {
|
|
14583
|
-
const model = resolvePromptModel(opts.model);
|
|
14690
|
+
const model = await resolvePromptModel(opts.model);
|
|
14584
14691
|
const reasoning = resolvePromptReasoning(opts.thinkingLevel);
|
|
14585
14692
|
const message = await completeSimple(model, {
|
|
14586
14693
|
systemPrompt: opts.system,
|
|
@@ -14601,7 +14708,7 @@ async function runPromptText(opts, hooks) {
|
|
|
14601
14708
|
async function runPromptObject(opts, hooks) {
|
|
14602
14709
|
const schema = opts.outputSchema;
|
|
14603
14710
|
if (!schema) throw new Error("outputSchema is required for structured LLM output");
|
|
14604
|
-
const model = resolvePromptModel(opts.model);
|
|
14711
|
+
const model = await resolvePromptModel(opts.model);
|
|
14605
14712
|
const reasoning = resolvePromptReasoning(opts.thinkingLevel);
|
|
14606
14713
|
const { toolSchema, unwrap } = wrapSchemaForTool(schema);
|
|
14607
14714
|
const tool = buildRespondTool(toolSchema);
|