@keystrokehq/keystroke 0.0.162 → 0.0.163

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.
Files changed (47) hide show
  1. package/dist/action.cjs +1 -1
  2. package/dist/action.mjs +1 -1
  3. package/dist/agent.cjs +19 -36
  4. package/dist/agent.cjs.map +1 -1
  5. package/dist/agent.d.cts +1 -1
  6. package/dist/agent.d.mts +1 -1
  7. package/dist/agent.mjs +19 -36
  8. package/dist/agent.mjs.map +1 -1
  9. package/dist/credentials.cjs +1 -1
  10. package/dist/credentials.mjs +1 -1
  11. package/dist/{dist-mblcM3Bv.cjs → dist-BNB2mfIe.cjs} +3 -3
  12. package/dist/{dist-mblcM3Bv.cjs.map → dist-BNB2mfIe.cjs.map} +1 -1
  13. package/dist/{dist-BIbZjRd7.mjs → dist-B_b7urZi.mjs} +1 -2
  14. package/dist/dist-B_b7urZi.mjs.map +1 -0
  15. package/dist/{dist-DXzvN8aq.mjs → dist-BjWzektn.mjs} +20 -160
  16. package/dist/{dist-DXzvN8aq.mjs.map → dist-BjWzektn.mjs.map} +1 -1
  17. package/dist/{dist-lWDTuftA.cjs → dist-DK1mbPAf.cjs} +1 -2
  18. package/dist/dist-DK1mbPAf.cjs.map +1 -0
  19. package/dist/{dist-CQH7Ircq.mjs → dist-DpsVuMhU.mjs} +3 -3
  20. package/dist/{dist-CQH7Ircq.mjs.map → dist-DpsVuMhU.mjs.map} +1 -1
  21. package/dist/{dist-DiqTL3ZF.cjs → dist-DqmPOnX5.cjs} +25 -177
  22. package/dist/{dist-DiqTL3ZF.cjs.map → dist-DqmPOnX5.cjs.map} +1 -1
  23. package/dist/index-CaW3JRFU.d.cts.map +1 -1
  24. package/dist/index-CaW3JRFU.d.mts.map +1 -1
  25. package/dist/{index-IVnunyAF.d.mts → index-DMr0802v.d.mts} +3 -2
  26. package/dist/index-DMr0802v.d.mts.map +1 -0
  27. package/dist/{index-DIm3_7or.d.cts → index-DuGLQUMl.d.cts} +3 -2
  28. package/dist/index-DuGLQUMl.d.cts.map +1 -0
  29. package/dist/{mistral-DuG294d1.cjs → mistral-CV_wqkNv.cjs} +2 -2
  30. package/dist/{mistral-DuG294d1.cjs.map → mistral-CV_wqkNv.cjs.map} +1 -1
  31. package/dist/{mistral-CDuUHkpO.mjs → mistral-Cf8bwl7k.mjs} +2 -2
  32. package/dist/{mistral-CDuUHkpO.mjs.map → mistral-Cf8bwl7k.mjs.map} +1 -1
  33. package/dist/{sse-DCsg_ELF.cjs → sse-DowizN6O.cjs} +2 -2
  34. package/dist/{sse-DCsg_ELF.cjs.map → sse-DowizN6O.cjs.map} +1 -1
  35. package/dist/{sse-BBd2aLiw.mjs → sse-Duxm9CEQ.mjs} +2 -2
  36. package/dist/{sse-BBd2aLiw.mjs.map → sse-Duxm9CEQ.mjs.map} +1 -1
  37. package/dist/trigger.cjs +1 -1
  38. package/dist/trigger.d.cts +1 -1
  39. package/dist/trigger.d.mts +1 -1
  40. package/dist/trigger.mjs +1 -1
  41. package/dist/workflow.cjs +1 -1
  42. package/dist/workflow.mjs +1 -1
  43. package/package.json +1 -1
  44. package/dist/dist-BIbZjRd7.mjs.map +0 -1
  45. package/dist/dist-lWDTuftA.cjs.map +0 -1
  46. package/dist/index-DIm3_7or.d.cts.map +0 -1
  47. package/dist/index-IVnunyAF.d.mts.map +0 -1
@@ -1,5 +1,5 @@
1
1
  import { Bn as __require, Ln as __commonJSMin, Vn as __toESM } from "./dist-Dv44b9V1.mjs";
2
- import { C as normalizeCredentialList, _ as CREDENTIAL_SCOPE_LEVEL_TO_SCOPE_TYPE } from "./dist-BIbZjRd7.mjs";
2
+ import { C as normalizeCredentialList, _ as CREDENTIAL_SCOPE_LEVEL_TO_SCOPE_TYPE } from "./dist-B_b7urZi.mjs";
3
3
  import { AsyncLocalStorage } from "node:async_hooks";
4
4
  import "node:fs";
5
5
  import { ZodFirstPartyTypeKind } from "zod/v3";
@@ -13251,7 +13251,7 @@ async function connectMcpServer(name, options) {
13251
13251
  }
13252
13252
  async function createTransport(url, transport, requestInit, fetchImpl) {
13253
13253
  if (transport === "sse") {
13254
- const { SSEClientTransport } = await import("./sse-BBd2aLiw.mjs");
13254
+ const { SSEClientTransport } = await import("./sse-Duxm9CEQ.mjs");
13255
13255
  return new SSEClientTransport(url, {
13256
13256
  requestInit,
13257
13257
  fetch: fetchImpl
@@ -16447,6 +16447,7 @@ const agentSessions = pgTable("agent_sessions", {
16447
16447
  parentSpanId: text$1("parent_span_id"),
16448
16448
  ranByUserId: text$1("ran_by_user_id"),
16449
16449
  canceledAt: timestamp("canceled_at", { withTimezone: true }),
16450
+ totalDurationMs: integer$1("total_duration_ms").notNull().default(0),
16450
16451
  createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
16451
16452
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull(),
16452
16453
  deletedAt: timestamp("deleted_at", { withTimezone: true })
@@ -16460,62 +16461,14 @@ const agentSessionsSqlite = sqliteTable("agent_sessions", {
16460
16461
  parentSpanId: text("parent_span_id"),
16461
16462
  ranByUserId: text("ran_by_user_id"),
16462
16463
  canceledAt: integer("canceled_at", { mode: "timestamp_ms" }),
16464
+ totalDurationMs: integer("total_duration_ms").notNull().default(0),
16463
16465
  createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull(),
16464
16466
  updatedAt: integer("updated_at", { mode: "timestamp_ms" }).notNull(),
16465
16467
  deletedAt: integer("deleted_at", { mode: "timestamp_ms" })
16466
16468
  }, (table) => [index("agent_sessions_agent_updated_idx").on(table.agentId, table.updatedAt)]);
16467
- const agentRuns = pgTable("agent_runs", {
16468
- id: text$1("id").primaryKey(),
16469
- sessionId: text$1("session_id").notNull().references(() => agentSessions.id),
16470
- projectId: text$1("project_id").notNull(),
16471
- agentId: text$1("agent_id").notNull().references(() => agents.id),
16472
- runIndex: integer$1("run_index").notNull(),
16473
- sourceKind: text$1("source_kind").$type().notNull().default("api"),
16474
- sourceId: text$1("source_id"),
16475
- ranByUserId: text$1("ran_by_user_id"),
16476
- status: text$1("status").$type().notNull(),
16477
- error: jsonb("error"),
16478
- startedAt: timestamp("started_at", { withTimezone: true }).notNull(),
16479
- finishedAt: timestamp("finished_at", { withTimezone: true }),
16480
- totalCostUsd: doublePrecision("total_cost_usd"),
16481
- totalTokens: integer$1("total_tokens"),
16482
- modelsUsed: jsonb("models_used").$type(),
16483
- createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
16484
- updatedAt: timestamp("updated_at", { withTimezone: true }).notNull(),
16485
- deletedAt: timestamp("deleted_at", { withTimezone: true })
16486
- }, (table) => [
16487
- index$1("agent_runs_session_run_index_idx").on(table.sessionId, table.runIndex),
16488
- index$1("agent_runs_agent_started_idx").on(table.agentId, table.startedAt),
16489
- index$1("agent_runs_project_started_idx").on(table.projectId, table.startedAt)
16490
- ]);
16491
- const agentRunsSqlite = sqliteTable("agent_runs", {
16492
- id: text("id").primaryKey(),
16493
- sessionId: text("session_id").notNull().references(() => agentSessionsSqlite.id),
16494
- projectId: text("project_id").notNull(),
16495
- agentId: text("agent_id").notNull().references(() => agentsSqlite.id),
16496
- runIndex: integer("run_index").notNull(),
16497
- sourceKind: text("source_kind").$type().notNull().default("api"),
16498
- sourceId: text("source_id"),
16499
- ranByUserId: text("ran_by_user_id"),
16500
- status: text("status").$type().notNull(),
16501
- error: text("error", { mode: "json" }),
16502
- startedAt: integer("started_at", { mode: "timestamp_ms" }).notNull(),
16503
- finishedAt: integer("finished_at", { mode: "timestamp_ms" }),
16504
- totalCostUsd: real("total_cost_usd"),
16505
- totalTokens: integer("total_tokens"),
16506
- modelsUsed: text("models_used", { mode: "json" }).$type(),
16507
- createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull(),
16508
- updatedAt: integer("updated_at", { mode: "timestamp_ms" }).notNull(),
16509
- deletedAt: integer("deleted_at", { mode: "timestamp_ms" })
16510
- }, (table) => [
16511
- index("agent_runs_session_run_index_idx").on(table.sessionId, table.runIndex),
16512
- index("agent_runs_agent_started_idx").on(table.agentId, table.startedAt),
16513
- index("agent_runs_project_started_idx").on(table.projectId, table.startedAt)
16514
- ]);
16515
16469
  const agentEvents = pgTable("agent_events", {
16516
16470
  id: text$1("id").primaryKey(),
16517
16471
  sessionId: text$1("session_id").notNull().references(() => agentSessions.id),
16518
- runId: text$1("run_id").references(() => agentRuns.id),
16519
16472
  seq: integer$1("seq").notNull(),
16520
16473
  eventType: text$1("event_type").notNull(),
16521
16474
  payload: jsonb("payload").notNull(),
@@ -16525,7 +16478,6 @@ const agentEvents = pgTable("agent_events", {
16525
16478
  const agentEventsSqlite = sqliteTable("agent_events", {
16526
16479
  id: text("id").primaryKey(),
16527
16480
  sessionId: text("session_id").notNull().references(() => agentSessionsSqlite.id),
16528
- runId: text("run_id").references(() => agentRunsSqlite.id),
16529
16481
  seq: integer("seq").notNull(),
16530
16482
  eventType: text("event_type").notNull(),
16531
16483
  payload: text("payload", { mode: "json" }).notNull(),
@@ -17046,10 +16998,6 @@ const tableRegistry = {
17046
16998
  pg: agentEvents,
17047
16999
  sqlite: agentEventsSqlite
17048
17000
  },
17049
- agentRuns: {
17050
- pg: agentRuns,
17051
- sqlite: agentRunsSqlite
17052
- },
17053
17001
  usageRecords: {
17054
17002
  pg: usageRecords,
17055
17003
  sqlite: usageRecordsSqlite
@@ -17168,51 +17116,28 @@ async function insertAgentSession(values) {
17168
17116
  async function selectActiveAgentSession(sessionId) {
17169
17117
  return (await getDb$1().select().from(agentSessions).where(scopedAnd$1(agentSessions, eq(agentSessions.id, sessionId), isNull(agentSessions.deletedAt))).limit(1))[0];
17170
17118
  }
17171
- async function selectAgentSessionById(sessionId) {
17172
- return selectActiveAgentSession(sessionId);
17173
- }
17174
17119
  async function touchAgentSession(sessionId, updatedAt) {
17175
17120
  await getDb$1().update(agentSessions).set({ updatedAt }).where(scopedAnd$1(agentSessions, eq(agentSessions.id, sessionId), isNull(agentSessions.deletedAt)));
17176
17121
  }
17122
+ async function addAgentSessionDuration(sessionId, deltaMs) {
17123
+ if (deltaMs <= 0) return;
17124
+ await getDb$1().update(agentSessions).set({ totalDurationMs: sql`${agentSessions.totalDurationMs} + ${deltaMs}` }).where(scopedAnd$1(agentSessions, eq(agentSessions.id, sessionId), isNull(agentSessions.deletedAt)));
17125
+ }
17177
17126
  async function maxSeqForSession(sessionId) {
17178
17127
  return (await getDb$1().select({ value: max(agentEvents.seq) }).from(agentEvents).innerJoin(agentSessions, eq(agentSessions.id, agentEvents.sessionId)).where(scopedAnd$1(agentSessions, eq(agentEvents.sessionId, sessionId), isNull(agentEvents.deletedAt))))[0]?.value ?? 0;
17179
17128
  }
17180
17129
  async function insertAgentEvent(values) {
17181
- await getDb$1().insert(agentEvents).values({
17182
- ...values,
17183
- runId: values.runId ?? null
17184
- });
17130
+ await getDb$1().insert(agentEvents).values(values);
17185
17131
  }
17186
17132
  async function listMessagePayloads(sessionId, messageEventType) {
17187
- return (await getDb$1().select({ payload: agentEvents.payload }).from(agentEvents).innerJoin(agentSessions, eq(agentSessions.id, agentEvents.sessionId)).where(scopedAnd$1(agentSessions, eq(agentEvents.sessionId, sessionId), eq(agentEvents.eventType, messageEventType), isNull(agentEvents.deletedAt))).orderBy(asc(agentEvents.seq))).map((row) => row.payload);
17133
+ return (await listMessageEventsForSession(sessionId, messageEventType)).map((row) => row.payload);
17188
17134
  }
17189
- async function nextRunIndex(sessionId) {
17190
- return ((await getDb$1().select({ value: max(agentRuns.runIndex) }).from(agentRuns).where(scopedAnd$1(agentRuns, eq(agentRuns.sessionId, sessionId), isNull(agentRuns.deletedAt))))[0]?.value ?? -1) + 1;
17191
- }
17192
- async function insertAgentRun(values) {
17193
- await getDb$1().insert(agentRuns).values({
17194
- ...values,
17195
- projectId: projectScopeId(),
17196
- sourceKind: values.sourceKind ?? "api",
17197
- sourceId: values.sourceId ?? null,
17198
- ranByUserId: values.ranByUserId ?? null,
17199
- error: null,
17200
- finishedAt: null,
17201
- totalCostUsd: null,
17202
- totalTokens: null,
17203
- modelsUsed: null
17204
- });
17205
- }
17206
- async function finalizeAgentRun(runId, values) {
17207
- await getDb$1().update(agentRuns).set({
17208
- status: values.status,
17209
- finishedAt: values.finishedAt,
17210
- error: values.error ?? null,
17211
- totalCostUsd: values.totalCostUsd,
17212
- totalTokens: values.totalTokens,
17213
- modelsUsed: values.modelsUsed,
17214
- updatedAt: values.updatedAt
17215
- }).where(scopedAnd$1(agentRuns, eq(agentRuns.id, runId), isNull(agentRuns.deletedAt)));
17135
+ async function listMessageEventsForSession(sessionId, messageEventType) {
17136
+ return await getDb$1().select({
17137
+ payload: agentEvents.payload,
17138
+ createdAt: agentEvents.createdAt,
17139
+ seq: agentEvents.seq
17140
+ }).from(agentEvents).innerJoin(agentSessions, eq(agentSessions.id, agentEvents.sessionId)).where(scopedAnd$1(agentSessions, eq(agentEvents.sessionId, sessionId), eq(agentEvents.eventType, messageEventType), isNull(agentEvents.deletedAt))).orderBy(asc(agentEvents.seq));
17216
17141
  }
17217
17142
  async function insertUsageRecord(values) {
17218
17143
  await getDb$1().insert(usageRecords).values({
@@ -17222,24 +17147,6 @@ async function insertUsageRecord(values) {
17222
17147
  metadata: values.metadata ?? null
17223
17148
  });
17224
17149
  }
17225
- async function sumUsageForRun(runId, runKind) {
17226
- const records = await getDb$1().select().from(usageRecords).where(and(eq(usageRecords.runId, runId), eq(usageRecords.runKind, runKind)));
17227
- let totalCostUsd = 0;
17228
- let totalTokens = 0;
17229
- const modelsUsed = /* @__PURE__ */ new Set();
17230
- for (const record of records) {
17231
- totalCostUsd += record.amount;
17232
- if (record.kind === "llm") {
17233
- if (record.unit === "tokens" && record.quantity != null) totalTokens += record.quantity;
17234
- modelsUsed.add(record.label);
17235
- }
17236
- }
17237
- return {
17238
- totalCostUsd,
17239
- totalTokens,
17240
- modelsUsed: [...modelsUsed]
17241
- };
17242
- }
17243
17150
  const MESSAGE_EVENT_TYPE = "message";
17244
17151
  /** Org/user credentials live once in the shared org database; project scope stays per-project. */
17245
17152
  function credentialScopeAnd(scopeType, ...conditions) {
@@ -17342,12 +17249,11 @@ async function touchSession(sessionId) {
17342
17249
  async function nextSeq(sessionId) {
17343
17250
  return await maxSeqForSession(sessionId) + 1;
17344
17251
  }
17345
- async function appendEvent(sessionId, eventType, payload, options) {
17252
+ async function appendEvent(sessionId, eventType, payload) {
17346
17253
  const seq = await nextSeq(sessionId);
17347
17254
  await insertAgentEvent({
17348
17255
  id: crypto.randomUUID(),
17349
17256
  sessionId,
17350
- runId: options?.runId ?? null,
17351
17257
  seq,
17352
17258
  eventType,
17353
17259
  payload,
@@ -17359,31 +17265,11 @@ async function appendEvent(sessionId, eventType, payload, options) {
17359
17265
  async function listMessageEvents(sessionId) {
17360
17266
  return listMessagePayloads(sessionId, MESSAGE_EVENT_TYPE);
17361
17267
  }
17362
- async function createAgentRun(options) {
17363
- const now = /* @__PURE__ */ new Date();
17364
- const runId = crypto.randomUUID();
17365
- const runIndex = await nextRunIndex(options.sessionId);
17366
- await insertAgentRun({
17367
- id: runId,
17368
- sessionId: options.sessionId,
17369
- agentId: options.agentId,
17370
- runIndex,
17371
- sourceKind: options.sourceKind,
17372
- sourceId: options.sourceId,
17373
- ranByUserId: options.ranByUserId,
17374
- status: "running",
17375
- startedAt: now,
17376
- createdAt: now,
17377
- updatedAt: now,
17378
- deletedAt: null
17379
- });
17380
- return runId;
17381
- }
17382
17268
  async function recordLlmUsageFromAssistantMessage(options) {
17383
17269
  const { usage } = options;
17384
17270
  await insertUsageRecord({
17385
17271
  id: crypto.randomUUID(),
17386
- runId: options.runId,
17272
+ runId: options.sessionId,
17387
17273
  runKind: "agent",
17388
17274
  kind: "llm",
17389
17275
  label: usage.label,
@@ -17394,32 +17280,6 @@ async function recordLlmUsageFromAssistantMessage(options) {
17394
17280
  createdAt: /* @__PURE__ */ new Date()
17395
17281
  });
17396
17282
  }
17397
- async function finalizeAgentRunFromPrompt(options) {
17398
- const finishedAt = options.finishedAt ?? /* @__PURE__ */ new Date();
17399
- const rollups = await sumUsageForRun(options.runId, "agent");
17400
- await finalizeAgentRun(options.runId, {
17401
- status: options.status,
17402
- finishedAt,
17403
- error: options.error ?? null,
17404
- totalCostUsd: rollups.totalCostUsd,
17405
- totalTokens: rollups.totalTokens,
17406
- modelsUsed: rollups.modelsUsed,
17407
- updatedAt: finishedAt
17408
- });
17409
- }
17410
- async function resolveAgentRunSourceFromSession(sessionId) {
17411
- const session = await selectAgentSessionById(sessionId);
17412
- if (!session) return {
17413
- sourceKind: "api",
17414
- sourceId: null,
17415
- ranByUserId: null
17416
- };
17417
- return {
17418
- sourceKind: session.sourceKind,
17419
- sourceId: session.sourceId,
17420
- ranByUserId: session.ranByUserId
17421
- };
17422
- }
17423
17283
  const ALGORITHM$1 = "aes-256-gcm";
17424
17284
  const IV_LENGTH$1 = 12;
17425
17285
  function resolveEncryptionKey$1() {
@@ -18410,6 +18270,6 @@ async function resolveActionCredentials(requirements, options) {
18410
18270
  return parseResolvedCredentials(requirements, await resolveCredentials(requirements, options.consumer, options.contextOverride));
18411
18271
  }
18412
18272
  //#endregion
18413
- export { auth as A, connectMcpDefinition as C, isMcp as D, defineMcp as E, JSONRPCMessageSchema as F, createFetchWithInit as M, normalizeHeaders as N, createParser as O, zodToJsonSchema as P, withSpan as S, connectMcpStdio as T, resolveRunSourceFromTraceContext as _, resolveActionCredentials as a, getTraceContext as b, appendEvent as c, finalizeAgentRunFromPrompt as d, getAgentByRoute as f, resolveAgentRunSourceFromSession as g, recordLlmUsageFromAssistantMessage as h, isCredentialError as i, extractWWWAuthenticateParams as j, UnauthorizedError as k, createAgentRun as l, listMessageEvents as m, captureCredentialToolErrors as n, resolveMcpTools as o, getSession as p, createCredentialResolver as r, MESSAGE_EVENT_TYPE as s, buildCredentialRunContext as t, createSession as u, touchSession as v, connectMcpServer as w, logSystem as x, captureConsole as y };
18273
+ export { createFetchWithInit as A, connectMcpStdio as C, UnauthorizedError as D, createParser as E, zodToJsonSchema as M, JSONRPCMessageSchema as N, auth as O, connectMcpServer as S, isMcp as T, captureConsole as _, resolveActionCredentials as a, withSpan as b, addAgentSessionDuration as c, getAgentByRoute as d, getSession as f, touchSession as g, resolveRunSourceFromTraceContext as h, isCredentialError as i, normalizeHeaders as j, extractWWWAuthenticateParams as k, appendEvent as l, recordLlmUsageFromAssistantMessage as m, captureCredentialToolErrors as n, resolveMcpTools as o, listMessageEvents as p, createCredentialResolver as r, MESSAGE_EVENT_TYPE as s, buildCredentialRunContext as t, createSession as u, getTraceContext as v, defineMcp as w, connectMcpDefinition as x, logSystem as y };
18414
18274
 
18415
- //# sourceMappingURL=dist-DXzvN8aq.mjs.map
18275
+ //# sourceMappingURL=dist-BjWzektn.mjs.map