@sentry/junior 0.258.0 → 0.260.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/dist/adapter-Dj5BTJRd.js +270 -0
- package/dist/{admin-My1SLGVO.js → admin-tzkZDqrH.js} +1 -1
- package/dist/{agent-hooks-BJ_5mlRH.js → agent-hooks-V4Hfc2tB.js} +711 -697
- package/dist/{agent-runner-DGsFQw72.js → agent-runner-DvTUyGTY.js} +751 -2508
- package/dist/api/schema.d.ts +239 -54
- package/dist/api/schema.js +4 -4
- package/dist/api.js +218 -57
- package/dist/{app-PX2H1PxU.d.ts → app-BsBUs3rF.d.ts} +6 -3
- package/dist/app.d.ts +2 -2
- package/dist/app.js +31 -31
- package/dist/{assistant-reply-H1M-91oF.js → assistant-reply-CEOMzsWy.js} +1 -1
- package/dist/{automation-DWBFBenG.js → automation-DrpcA-SX.js} +3 -0
- package/dist/{briefs-vYoK85rJ.js → briefs-BSCkyYqs.js} +4 -4
- package/dist/{catalog-runtime-BN4DVxco.js → catalog-runtime-Bpcs2b2L.js} +1 -1
- package/dist/cli/chat.d.ts +1 -1
- package/dist/cli/chat.js +16 -16
- package/dist/cli/check.js +3 -3
- package/dist/cli/main.js +6 -2
- package/dist/cli/plugins.d.ts +1 -1
- package/dist/cli/plugins.js +4 -4
- package/dist/cli/run.d.ts +2 -1
- package/dist/cli/run.js +2 -1
- package/dist/cli/snapshot-create.js +4 -3
- package/dist/cli/upgrade.d.ts +1 -1
- package/dist/cli/upgrade.js +3 -3
- package/dist/{client-DbyHuMFU.js → client-JeyzRwsT.js} +57 -4
- package/dist/{config-_WO_AjCB.js → config-ULs6Zl9b.js} +5 -2
- package/dist/{connections-ag_1JOhB.js → connections-CZ7HhEmN.js} +174 -10
- package/dist/{conversation-DjN3mlog.js → conversation-CvuKJ3tA.js} +108 -155
- package/dist/{conversation-privacy-DHP_xnVj.js → conversation-privacy-BQSlZXgV.js} +1 -1
- package/dist/{credential-sync-B-CPX_ag.js → credential-sync-BKcv5rBe.js} +14 -177
- package/dist/{executor-RvG6kVe1.js → executor-BoGNVEDn.js} +3 -2
- package/dist/{images-C2J_bj2g.js → images-BBmDXQCB.js} +3 -3
- package/dist/instrumentation.js +2 -2
- package/dist/{job-queue-DVe37it2.js → job-queue-CbdVyaCo.js} +1 -1
- package/dist/{logging-DsHONhVm.js → logging-DSMmkHXG.js} +1 -1
- package/dist/{message-DyitL8no.js → message-zaj5gBbS.js} +3 -3
- package/dist/nitro.d.ts +2 -2
- package/dist/nitro.js +3 -3
- package/dist/{oauth-callback-server-CDby-pCn.js → oauth-callback-server-yYFG8Own.js} +2 -2
- package/dist/{plugins-B11Mfkmv.d.ts → plugins-B_0Cr0MO.d.ts} +5 -1
- package/dist/{profile-DpYjfKke.js → profile-ByWkAY7L.js} +24 -293
- package/dist/{prompt-xgpyMtg7.js → prompt-DeVax2qO.js} +1 -1
- package/dist/{registry-Du566MtJ.js → registry-BL-osYWN.js} +41 -2
- package/dist/{resolve-ClCIgh8E.js → resolve-HuPG2VMB.js} +9 -4
- package/dist/{result-BBbKA61B.js → result-DS7V4a3-.js} +9 -14
- package/dist/{runner-CCuqFSqx.js → runner-Cba_OfGl.js} +11 -11
- package/dist/{sandbox-egress-signals-DY1mbKF4.js → sandbox-egress-signals-C7NcPzea.js} +1 -1
- package/dist/{schema-DrMh3NtR.js → schema-OzL8Zw4H.js} +82 -3
- package/dist/service-account-DqwHhwJA.js +120 -0
- package/dist/service-account-Ko1Xs--F.js +661 -0
- package/dist/{session-stop-CIoFiwwe.js → session-stop-HfKx-pky.js} +180 -22
- package/dist/{skills-DyQBLIrK.js → skills-D6_bcAuW.js} +2 -2
- package/dist/{spawn-Dqfeo7XP.js → spawn-BWqK6EwM.js} +2 -2
- package/dist/{store-DJLwq4x8.js → store-DzRbd5VT.js} +6 -6
- package/dist/{task-queue-BzW4MaWU.js → task-queue-9LICOzcL.js} +3 -3
- package/dist/{task-runner-D3qJh4fK.js → task-runner-DGnd_B5X.js} +10 -10
- package/dist/{tasks-CEXKTASS.js → tasks-CjrS098O.js} +4928 -2389
- package/dist/thread-state-CqkYowEz.js +184 -0
- package/dist/{turn-failure-response-CWx7aNIh.js → turn-failure-response-BkKmGqFt.js} +8 -9
- package/dist/{turn-lifecycle-C9JFst7-.js → turn-lifecycle-DYv8IV6w.js} +1 -1
- package/dist/{turn-session-routing-CUxtfpr7.js → turn-session-routing-TaVhKmdz.js} +1 -1
- package/dist/{validation-BHRxa5G4.js → validation-BrXMYPyo.js} +1 -1
- package/dist/{version-C0U6rHg0.js → version-Ct049vav.js} +1 -1
- package/dist/version.js +1 -1
- package/dist/{viewer-CGvvRqKp.js → viewer-C-J0ewlH.js} +19 -10
- package/dist/{web-input-Keb5GRWj.js → web-input-C_xB3RXp.js} +41 -37
- package/dist/{work-DRcsJ3hV.js → work-D829Ue7k.js} +9 -10
- package/dist/{worker-Cjo-QqPq.js → worker-BRTYzlpk.js} +5 -5
- package/migrations/0061_workspace_vcpus.sql +1 -0
- package/migrations/meta/0061_snapshot.json +3471 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +6 -6
- package/dist/experimental-BNK1l1jv.js +0 -49
- package/dist/oauth-authorization-djQn5Nhy.js +0 -97
- package/dist/pending-auth-D8J1uJUS.js +0 -442
- package/dist/thread-state-W3f6kAtz.js +0 -426
|
@@ -1,426 +0,0 @@
|
|
|
1
|
-
import { B as toOptionalNumber, V as toOptionalString, z as isRecord } from "./logging-DsHONhVm.js";
|
|
2
|
-
import { d as getDb } from "./viewer-CGvvRqKp.js";
|
|
3
|
-
import { d as juniorDestinations } from "./agent-invocations-Qfz7dWgU.js";
|
|
4
|
-
import { l as juniorLocationConfigurations } from "./executor-RvG6kVe1.js";
|
|
5
|
-
import { Gt as persistConversationMessages } from "./tasks-CEXKTASS.js";
|
|
6
|
-
import { it as JUNIOR_THREAD_STATE_TTL_MS } from "./agent-hooks-BJ_5mlRH.js";
|
|
7
|
-
import { w as getStateAdapter } from "./profile-DpYjfKke.js";
|
|
8
|
-
import { z } from "zod";
|
|
9
|
-
import { randomUUID } from "node:crypto";
|
|
10
|
-
import { and, eq, sql } from "drizzle-orm";
|
|
11
|
-
// src/chat/configuration/types.ts
|
|
12
|
-
/** JSON value accepted by Location configuration. */
|
|
13
|
-
const configValueSchema = z.json();
|
|
14
|
-
// src/chat/configuration/validation.ts
|
|
15
|
-
const CONFIG_KEY_RE = /^[a-z0-9]+(?:\.[a-z0-9-]+)+$/;
|
|
16
|
-
const SECRET_KEY_RE = /(?:^|[_.-])(token|secret|password|passphrase|api[-_]?key|private[-_]?key|credential|auth)(?:$|[_.-])/i;
|
|
17
|
-
const SECRET_VALUE_PATTERNS = [
|
|
18
|
-
/\bBearer\s+[A-Za-z0-9._-]{16,}\b/i,
|
|
19
|
-
/-----BEGIN [A-Z ]*PRIVATE KEY-----/,
|
|
20
|
-
/\b(?:ghp|ghs|github_pat)_[A-Za-z0-9_]{20,}\b/,
|
|
21
|
-
/\bxox[baprs]-[A-Za-z0-9-]{10,}\b/i,
|
|
22
|
-
/\bsk-[A-Za-z0-9]{20,}\b/,
|
|
23
|
-
/\bAIza[0-9A-Za-z\-_]{30,}\b/,
|
|
24
|
-
/\bAKIA[0-9A-Z]{16}\b/,
|
|
25
|
-
/\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/
|
|
26
|
-
];
|
|
27
|
-
function validateConfigKey(key) {
|
|
28
|
-
const trimmed = key.trim();
|
|
29
|
-
if (!trimmed) return "Configuration key must not be empty";
|
|
30
|
-
if (!CONFIG_KEY_RE.test(trimmed)) return `Invalid configuration key "${key}"; expected dotted lowercase namespace (for example "provider.repo")`;
|
|
31
|
-
if (SECRET_KEY_RE.test(trimmed)) return `Configuration key "${key}" appears to be secret-related and is not allowed`;
|
|
32
|
-
}
|
|
33
|
-
function collectStringValues(value, output, depth = 0) {
|
|
34
|
-
if (depth > 5) return;
|
|
35
|
-
if (typeof value === "string") {
|
|
36
|
-
output.push(value);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (Array.isArray(value)) {
|
|
40
|
-
for (const item of value) collectStringValues(item, output, depth + 1);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
if (value && typeof value === "object") for (const [key, nested] of Object.entries(value)) {
|
|
44
|
-
output.push(key);
|
|
45
|
-
collectStringValues(nested, output, depth + 1);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function validateConfigValue(value) {
|
|
49
|
-
const stringValues = [];
|
|
50
|
-
collectStringValues(value, stringValues);
|
|
51
|
-
for (const text of stringValues) for (const pattern of SECRET_VALUE_PATTERNS) if (pattern.test(text)) return "Configuration value appears to contain secret material and is not allowed";
|
|
52
|
-
}
|
|
53
|
-
// src/chat/configuration/service.ts
|
|
54
|
-
function sanitizeEntry(value) {
|
|
55
|
-
if (!isRecord(value)) return;
|
|
56
|
-
const key = toOptionalString(value.key);
|
|
57
|
-
if (!key || validateConfigKey(key)) return;
|
|
58
|
-
const updatedAt = toOptionalString(value.updatedAt);
|
|
59
|
-
if (!updatedAt) return;
|
|
60
|
-
if (value.scope !== "channel" && value.scope !== "conversation" && value.scope !== "location") return;
|
|
61
|
-
const parsedValue = configValueSchema.safeParse(value.value);
|
|
62
|
-
if (!parsedValue.success) return;
|
|
63
|
-
return {
|
|
64
|
-
key,
|
|
65
|
-
value: parsedValue.data,
|
|
66
|
-
scope: "location",
|
|
67
|
-
updatedAt,
|
|
68
|
-
updatedBy: toOptionalString(value.updatedBy),
|
|
69
|
-
source: toOptionalString(value.source),
|
|
70
|
-
expiresAt: toOptionalString(value.expiresAt)
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
/** Coerce legacy persisted configuration into current Location entries. */
|
|
74
|
-
function coerceLegacyLocationConfig(raw) {
|
|
75
|
-
const rawConfig = isRecord(raw) && isRecord(raw.configuration) ? raw.configuration : {};
|
|
76
|
-
const rawEntries = isRecord(rawConfig.entries) ? rawConfig.entries : {};
|
|
77
|
-
return Object.values(rawEntries).flatMap((value) => {
|
|
78
|
-
const entry = sanitizeEntry(value);
|
|
79
|
-
return entry ? [entry] : [];
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
/** Create a Location configuration service over entry-level storage. */
|
|
83
|
-
function createLocationConfigurationService(storage) {
|
|
84
|
-
const get = async (key) => await storage.get(key.trim());
|
|
85
|
-
const set = async (input) => {
|
|
86
|
-
const normalizedKey = input.key.trim();
|
|
87
|
-
const keyError = validateConfigKey(normalizedKey);
|
|
88
|
-
if (keyError) throw new Error(keyError);
|
|
89
|
-
const value = configValueSchema.parse(input.value);
|
|
90
|
-
const valueError = validateConfigValue(value);
|
|
91
|
-
if (valueError) throw new Error(valueError);
|
|
92
|
-
const entry = {
|
|
93
|
-
key: normalizedKey,
|
|
94
|
-
value,
|
|
95
|
-
scope: "location",
|
|
96
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
97
|
-
updatedBy: toOptionalString(input.updatedBy),
|
|
98
|
-
source: toOptionalString(input.source),
|
|
99
|
-
expiresAt: toOptionalString(input.expiresAt)
|
|
100
|
-
};
|
|
101
|
-
await storage.set(entry);
|
|
102
|
-
return entry;
|
|
103
|
-
};
|
|
104
|
-
const unset = async (key) => await storage.unset(key.trim());
|
|
105
|
-
const list = async (options = {}) => {
|
|
106
|
-
const prefix = options.prefix?.trim();
|
|
107
|
-
return (await storage.list()).filter((entry) => prefix ? entry.key.startsWith(prefix) : true).sort((a, b) => a.key.localeCompare(b.key));
|
|
108
|
-
};
|
|
109
|
-
const resolve = async (key) => (await get(key))?.value;
|
|
110
|
-
const resolveValues = async (options = {}) => {
|
|
111
|
-
const keys = options.keys?.map((key) => key.trim()).filter(Boolean);
|
|
112
|
-
const entries = await list(options.prefix ? { prefix: options.prefix } : {});
|
|
113
|
-
return Object.fromEntries(entries.filter((entry) => !keys || keys.includes(entry.key)).map((entry) => [entry.key, entry.value]));
|
|
114
|
-
};
|
|
115
|
-
return {
|
|
116
|
-
get,
|
|
117
|
-
set,
|
|
118
|
-
unset,
|
|
119
|
-
list,
|
|
120
|
-
resolve,
|
|
121
|
-
resolveValues
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
// src/chat/configuration/sql.ts
|
|
125
|
-
/** Resolve or create the canonical Location for a Slack Destination. */
|
|
126
|
-
async function resolveLocationId(db, destination) {
|
|
127
|
-
const channelId = destination.channelId;
|
|
128
|
-
const now = /* @__PURE__ */ new Date();
|
|
129
|
-
const location = (await db.insert(juniorDestinations).values({
|
|
130
|
-
id: randomUUID(),
|
|
131
|
-
provider: "slack",
|
|
132
|
-
providerTenantId: destination.teamId,
|
|
133
|
-
providerDestinationId: channelId,
|
|
134
|
-
kind: channelId.startsWith("D") ? "dm" : channelId.startsWith("G") ? "group" : "channel",
|
|
135
|
-
parentDestinationId: null,
|
|
136
|
-
displayName: null,
|
|
137
|
-
visibility: "unknown",
|
|
138
|
-
metadata: { platform: "slack" },
|
|
139
|
-
createdAt: now,
|
|
140
|
-
updatedAt: now
|
|
141
|
-
}).onConflictDoUpdate({
|
|
142
|
-
target: [
|
|
143
|
-
juniorDestinations.provider,
|
|
144
|
-
juniorDestinations.providerTenantId,
|
|
145
|
-
juniorDestinations.providerDestinationId
|
|
146
|
-
],
|
|
147
|
-
set: { id: sql`${juniorDestinations.id}` }
|
|
148
|
-
}).returning({ id: juniorDestinations.id }))[0];
|
|
149
|
-
if (!location) throw new Error("Location upsert returned no row");
|
|
150
|
-
return location.id;
|
|
151
|
-
}
|
|
152
|
-
/** Persist configuration rows for the Location addressed by a Slack Destination. */
|
|
153
|
-
function createSqlLocationConfigurationStorage(db, destination) {
|
|
154
|
-
const locationWhere = and(eq(juniorDestinations.provider, "slack"), eq(juniorDestinations.providerTenantId, destination.teamId), eq(juniorDestinations.providerDestinationId, destination.channelId));
|
|
155
|
-
const selectEntries = () => db.select({
|
|
156
|
-
key: juniorLocationConfigurations.key,
|
|
157
|
-
value: juniorLocationConfigurations.value,
|
|
158
|
-
updatedAt: juniorLocationConfigurations.updatedAt,
|
|
159
|
-
updatedBy: juniorLocationConfigurations.updatedBy,
|
|
160
|
-
source: juniorLocationConfigurations.source,
|
|
161
|
-
expiresAt: juniorLocationConfigurations.expiresAt
|
|
162
|
-
}).from(juniorLocationConfigurations).innerJoin(juniorDestinations, eq(juniorDestinations.id, juniorLocationConfigurations.locationId));
|
|
163
|
-
const entryFromRow = (row) => ({
|
|
164
|
-
key: row.key,
|
|
165
|
-
value: configValueSchema.parse(JSON.parse(row.value)),
|
|
166
|
-
scope: "location",
|
|
167
|
-
updatedAt: row.updatedAt.toISOString(),
|
|
168
|
-
updatedBy: row.updatedBy ?? void 0,
|
|
169
|
-
source: row.source ?? void 0,
|
|
170
|
-
expiresAt: row.expiresAt ?? void 0
|
|
171
|
-
});
|
|
172
|
-
const list = async () => (await selectEntries().where(locationWhere)).map(entryFromRow);
|
|
173
|
-
const set = async (entry) => {
|
|
174
|
-
const locationId = await resolveLocationId(db, destination);
|
|
175
|
-
await db.insert(juniorLocationConfigurations).values({
|
|
176
|
-
locationId,
|
|
177
|
-
key: entry.key,
|
|
178
|
-
value: JSON.stringify(entry.value),
|
|
179
|
-
updatedAt: new Date(entry.updatedAt),
|
|
180
|
-
updatedBy: entry.updatedBy,
|
|
181
|
-
source: entry.source,
|
|
182
|
-
expiresAt: entry.expiresAt
|
|
183
|
-
}).onConflictDoUpdate({
|
|
184
|
-
target: [juniorLocationConfigurations.locationId, juniorLocationConfigurations.key],
|
|
185
|
-
set: {
|
|
186
|
-
value: JSON.stringify(entry.value),
|
|
187
|
-
updatedAt: new Date(entry.updatedAt),
|
|
188
|
-
updatedBy: entry.updatedBy,
|
|
189
|
-
source: entry.source,
|
|
190
|
-
expiresAt: entry.expiresAt
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
};
|
|
194
|
-
return {
|
|
195
|
-
get: async (key) => {
|
|
196
|
-
const rows = await selectEntries().where(and(locationWhere, eq(juniorLocationConfigurations.key, key))).limit(1);
|
|
197
|
-
return rows[0] ? entryFromRow(rows[0]) : void 0;
|
|
198
|
-
},
|
|
199
|
-
list,
|
|
200
|
-
set,
|
|
201
|
-
unset: async (key) => {
|
|
202
|
-
const locationId = await resolveLocationId(db, destination);
|
|
203
|
-
return (await db.delete(juniorLocationConfigurations).where(and(eq(juniorLocationConfigurations.locationId, locationId), eq(juniorLocationConfigurations.key, key))).returning({ key: juniorLocationConfigurations.key })).length > 0;
|
|
204
|
-
},
|
|
205
|
-
insertLegacy: async (entries) => {
|
|
206
|
-
if (entries.length === 0) return;
|
|
207
|
-
const locationId = await resolveLocationId(db, destination);
|
|
208
|
-
await db.insert(juniorLocationConfigurations).values(entries.map((entry) => ({
|
|
209
|
-
locationId,
|
|
210
|
-
key: entry.key,
|
|
211
|
-
value: JSON.stringify(entry.value),
|
|
212
|
-
updatedAt: new Date(entry.updatedAt),
|
|
213
|
-
updatedBy: entry.updatedBy,
|
|
214
|
-
source: entry.source,
|
|
215
|
-
expiresAt: entry.expiresAt
|
|
216
|
-
}))).onConflictDoNothing();
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
/** Resolve SQL-owned Location configuration and copy live legacy entries once. */
|
|
221
|
-
function createDurableLocationConfigurationService(args) {
|
|
222
|
-
const storage = createSqlLocationConfigurationStorage(args.db, args.destination);
|
|
223
|
-
let cutover;
|
|
224
|
-
const ensureCutover = () => cutover ??= (async () => {
|
|
225
|
-
if ((await storage.list()).length > 0) return;
|
|
226
|
-
const legacy = coerceLegacyLocationConfig(await args.loadLegacy());
|
|
227
|
-
await storage.insertLegacy(legacy);
|
|
228
|
-
})();
|
|
229
|
-
return createLocationConfigurationService({
|
|
230
|
-
get: async (key) => {
|
|
231
|
-
await ensureCutover();
|
|
232
|
-
return await storage.get(key);
|
|
233
|
-
},
|
|
234
|
-
list: async () => {
|
|
235
|
-
await ensureCutover();
|
|
236
|
-
return await storage.list();
|
|
237
|
-
},
|
|
238
|
-
set: async (entry) => {
|
|
239
|
-
await ensureCutover();
|
|
240
|
-
await storage.set(entry);
|
|
241
|
-
},
|
|
242
|
-
unset: async (key) => {
|
|
243
|
-
await ensureCutover();
|
|
244
|
-
return await storage.unset(key);
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
// src/chat/state/conversation.ts
|
|
249
|
-
function defaultConversationState() {
|
|
250
|
-
return {
|
|
251
|
-
schemaVersion: 1,
|
|
252
|
-
messages: [],
|
|
253
|
-
compactions: [],
|
|
254
|
-
processing: {},
|
|
255
|
-
vision: { byFileId: {} }
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
function coercePendingAuthState(value) {
|
|
259
|
-
if (!isRecord(value)) return;
|
|
260
|
-
const kind = value.kind;
|
|
261
|
-
const provider = toOptionalString(value.provider);
|
|
262
|
-
const actorId = toOptionalString(value.actorId);
|
|
263
|
-
const authSessionId = toOptionalString(value.authSessionId);
|
|
264
|
-
const scope = toOptionalString(value.scope);
|
|
265
|
-
const sessionId = toOptionalString(value.sessionId);
|
|
266
|
-
const linkSentAtMs = toOptionalNumber(value.linkSentAtMs);
|
|
267
|
-
if (kind !== "mcp" && kind !== "plugin" || !provider || !actorId || kind === "mcp" && !authSessionId || !sessionId || typeof linkSentAtMs !== "number") return;
|
|
268
|
-
const base = {
|
|
269
|
-
provider,
|
|
270
|
-
actorId,
|
|
271
|
-
...scope ? { scope } : void 0,
|
|
272
|
-
sessionId,
|
|
273
|
-
linkSentAtMs
|
|
274
|
-
};
|
|
275
|
-
return kind === "mcp" ? {
|
|
276
|
-
...base,
|
|
277
|
-
authSessionId,
|
|
278
|
-
kind
|
|
279
|
-
} : {
|
|
280
|
-
...base,
|
|
281
|
-
kind
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
/** Safely coerce an unknown persisted value into a ThreadConversationState. */
|
|
285
|
-
function coerceThreadConversationState(value) {
|
|
286
|
-
if (!isRecord(value)) return defaultConversationState();
|
|
287
|
-
const root = value;
|
|
288
|
-
const rawConversation = isRecord(root.conversation) ? root.conversation : {};
|
|
289
|
-
const messages = [];
|
|
290
|
-
const compactions = [];
|
|
291
|
-
const rawProcessing = isRecord(rawConversation.processing) ? rawConversation.processing : {};
|
|
292
|
-
const processing = {
|
|
293
|
-
activeTurnId: toOptionalString(rawProcessing.activeTurnId),
|
|
294
|
-
lastCompletedAtMs: toOptionalNumber(rawProcessing.lastCompletedAtMs),
|
|
295
|
-
pendingAuth: coercePendingAuthState(rawProcessing.pendingAuth)
|
|
296
|
-
};
|
|
297
|
-
const rawVision = isRecord(rawConversation.vision) ? rawConversation.vision : {};
|
|
298
|
-
const rawVisionByFileId = isRecord(rawVision.byFileId) ? rawVision.byFileId : {};
|
|
299
|
-
const byFileId = {};
|
|
300
|
-
for (const [fileId, value] of Object.entries(rawVisionByFileId)) {
|
|
301
|
-
if (typeof fileId !== "string" || fileId.trim().length === 0) continue;
|
|
302
|
-
if (!isRecord(value)) continue;
|
|
303
|
-
const summary = toOptionalString(value.summary);
|
|
304
|
-
const analyzedAtMs = toOptionalNumber(value.analyzedAtMs);
|
|
305
|
-
if (!summary || !analyzedAtMs) continue;
|
|
306
|
-
byFileId[fileId] = {
|
|
307
|
-
summary,
|
|
308
|
-
analyzedAtMs
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
return {
|
|
312
|
-
schemaVersion: 1,
|
|
313
|
-
messages,
|
|
314
|
-
compactions,
|
|
315
|
-
processing,
|
|
316
|
-
vision: {
|
|
317
|
-
backfillCompletedAtMs: toOptionalNumber(rawVision.backfillCompletedAtMs),
|
|
318
|
-
byFileId
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Wrap active conversation control into the thread scratch envelope.
|
|
324
|
-
*
|
|
325
|
-
* Visible history lives in SQL. Image summaries use a separate disposable
|
|
326
|
-
* cache, so thread scratch does not retain them.
|
|
327
|
-
*/
|
|
328
|
-
function buildConversationStatePatch(conversation) {
|
|
329
|
-
return { conversation: {
|
|
330
|
-
schemaVersion: 1,
|
|
331
|
-
processing: { ...conversation.processing }
|
|
332
|
-
} };
|
|
333
|
-
}
|
|
334
|
-
// src/chat/runtime/thread-state.ts
|
|
335
|
-
function threadStateKey(threadId) {
|
|
336
|
-
return `thread-state:${threadId}`;
|
|
337
|
-
}
|
|
338
|
-
function channelStateKey(channelId) {
|
|
339
|
-
return `channel-state:${channelId}`;
|
|
340
|
-
}
|
|
341
|
-
function buildThreadStatePayload(patch) {
|
|
342
|
-
const payload = {};
|
|
343
|
-
if (patch.conversation) Object.assign(payload, buildConversationStatePatch(patch.conversation));
|
|
344
|
-
if (patch.sandboxRef !== void 0) {
|
|
345
|
-
payload.app_sandbox_id = patch.sandboxRef?.id ?? "";
|
|
346
|
-
payload.app_sandbox_dependency_profile_hash = patch.sandboxRef?.profileHash ?? "";
|
|
347
|
-
payload.app_sandbox_workspace_id = patch.sandboxRef?.workspaceId ?? "";
|
|
348
|
-
}
|
|
349
|
-
return payload;
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Merge a payload into thread scratch with Junior's TTL.
|
|
353
|
-
*
|
|
354
|
-
* Chat SDK state writes hardcode a 30-day TTL. This boundary owns Junior's
|
|
355
|
-
* shorter retention policy for the same scratch keys.
|
|
356
|
-
*/
|
|
357
|
-
async function mergePersistedState(key, payload) {
|
|
358
|
-
if (Object.keys(payload).length === 0) return;
|
|
359
|
-
const stateAdapter = getStateAdapter();
|
|
360
|
-
await stateAdapter.connect();
|
|
361
|
-
const existing = await stateAdapter.get(key) ?? {};
|
|
362
|
-
await stateAdapter.set(key, {
|
|
363
|
-
...existing,
|
|
364
|
-
...payload
|
|
365
|
-
}, JUNIOR_THREAD_STATE_TTL_MS);
|
|
366
|
-
}
|
|
367
|
-
/** Extract persisted sandbox metadata from thread state payload. */
|
|
368
|
-
function getPersistedSandboxState(state) {
|
|
369
|
-
const id = toOptionalString(state.app_sandbox_id);
|
|
370
|
-
if (!id) return;
|
|
371
|
-
const profileHash = toOptionalString(state.app_sandbox_dependency_profile_hash);
|
|
372
|
-
const workspaceId = toOptionalString(state.app_sandbox_workspace_id);
|
|
373
|
-
return {
|
|
374
|
-
id,
|
|
375
|
-
...profileHash ? { profileHash } : void 0,
|
|
376
|
-
...workspaceId ? { workspaceId } : void 0
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
/** Persist a thread-state patch for a Chat thread handle. */
|
|
380
|
-
async function persistThreadState(thread, patch) {
|
|
381
|
-
if (patch.conversation) await persistConversationMessages({
|
|
382
|
-
conversation: patch.conversation,
|
|
383
|
-
conversationId: toOptionalString(thread.id) ?? toOptionalString(thread.runId)
|
|
384
|
-
});
|
|
385
|
-
await persistThreadRuntimeState(thread, patch);
|
|
386
|
-
}
|
|
387
|
-
/** Persist only the Redis-backed runtime scratch in a thread-state patch. */
|
|
388
|
-
async function persistThreadRuntimeState(thread, patch) {
|
|
389
|
-
const threadId = toOptionalString(thread.id) ?? toOptionalString(thread.runId);
|
|
390
|
-
if (!threadId) throw new Error("thread id is required to persist runtime scratch");
|
|
391
|
-
await mergePersistedState(threadStateKey(threadId), buildThreadStatePayload(patch));
|
|
392
|
-
}
|
|
393
|
-
/** Load the persisted state payload for a thread without requiring a Chat singleton. */
|
|
394
|
-
async function getPersistedThreadState(threadId) {
|
|
395
|
-
const stateAdapter = getStateAdapter();
|
|
396
|
-
await stateAdapter.connect();
|
|
397
|
-
return await stateAdapter.get(threadStateKey(threadId)) ?? {};
|
|
398
|
-
}
|
|
399
|
-
/** Persist a thread-state patch by thread id without constructing a Chat thread. */
|
|
400
|
-
async function persistThreadStateById(threadId, patch) {
|
|
401
|
-
if (patch.conversation) await persistConversationMessages({
|
|
402
|
-
conversation: patch.conversation,
|
|
403
|
-
conversationId: threadId
|
|
404
|
-
});
|
|
405
|
-
await mergePersistedState(threadStateKey(threadId), buildThreadStatePayload(patch));
|
|
406
|
-
}
|
|
407
|
-
/** Load legacy Redis-backed channel state during the SQL cutover. */
|
|
408
|
-
async function getLegacyChannelState(channelId) {
|
|
409
|
-
const stateAdapter = getStateAdapter();
|
|
410
|
-
await stateAdapter.connect();
|
|
411
|
-
return await stateAdapter.get(channelStateKey(channelId)) ?? {};
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Resolve durable location configuration.
|
|
415
|
-
*
|
|
416
|
-
* Slack still reads the old channel-id Redis bag once during cutover.
|
|
417
|
-
*/
|
|
418
|
-
function getLocationConfigurationService(destination) {
|
|
419
|
-
if (destination.platform !== "slack") throw new Error("Location configuration requires a Slack Location");
|
|
420
|
-
return createDurableLocationConfigurationService({
|
|
421
|
-
destination,
|
|
422
|
-
db: getDb(),
|
|
423
|
-
loadLegacy: async () => await getLegacyChannelState(destination.channelId)
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
export { persistThreadState as a, configValueSchema as c, persistThreadRuntimeState as i, getPersistedSandboxState as n, persistThreadStateById as o, getPersistedThreadState as r, coerceThreadConversationState as s, getLocationConfigurationService as t };
|