@opengeni/worker-bundle 0.20.0 → 0.20.3
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/activities/agent-turn.d.ts +25 -2
- package/dist/activities/google-drive-publication.d.ts +53 -0
- package/dist/activities/image-generation-references.d.ts +2 -0
- package/dist/activities/knowledge-source-sync-driver.d.ts +21 -0
- package/dist/activities/knowledge-source-sync.d.ts +29 -1
- package/dist/activities/run-input.d.ts +20 -2
- package/dist/activities/session-state.d.ts +5 -3
- package/dist/activities/types.d.ts +27 -0
- package/dist/{activities-control-XURE4VKN.js → activities-control-BJSDRSIP.js} +369 -20
- package/dist/activities-control-BJSDRSIP.js.map +1 -0
- package/dist/activities-control.d.ts +2 -2
- package/dist/{activities-turn-6EBXGGLS.js → activities-turn-J5YO7XXX.js} +2281 -1074
- package/dist/activities-turn-J5YO7XXX.js.map +1 -0
- package/dist/activities.d.ts +3 -3
- package/dist/{chunk-TYNPCG4U.js → chunk-N5TJIFCE.js} +69 -1
- package/dist/chunk-N5TJIFCE.js.map +1 -0
- package/dist/{chunk-5GRJIP5H.js → chunk-P6Z2SLAA.js} +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/observability-metrics.d.ts +37 -0
- package/dist/workflow-bundle.js +174 -4
- package/dist/workflows/session.d.ts +17 -1
- package/package.json +13 -13
- package/src/activities/agent-turn.ts +1801 -1175
- package/src/activities/documents.ts +6 -4
- package/src/activities/google-drive-publication.ts +840 -0
- package/src/activities/image-generation-references.ts +11 -2
- package/src/activities/knowledge-source-sync-driver.ts +23 -0
- package/src/activities/knowledge-source-sync.ts +384 -9
- package/src/activities/run-input.ts +175 -51
- package/src/activities/session-state.ts +69 -5
- package/src/activities/types.ts +27 -0
- package/src/activities.ts +2 -0
- package/src/observability-metrics.ts +184 -3
- package/src/workflows/session.ts +199 -8
- package/dist/activities-control-XURE4VKN.js.map +0 -1
- package/dist/activities-turn-6EBXGGLS.js.map +0 -1
- package/dist/chunk-TYNPCG4U.js.map +0 -1
- /package/dist/{chunk-5GRJIP5H.js.map → chunk-P6Z2SLAA.js.map} +0 -0
|
@@ -0,0 +1,840 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { AttemptToolApprovalRequiredError, type AttemptToolDefinition } from "@opengeni/codemode";
|
|
3
|
+
import type { McpPersonalConnectionDelegation } from "@opengeni/contracts";
|
|
4
|
+
import {
|
|
5
|
+
GOOGLE_DRIVE_FILE_SCOPE,
|
|
6
|
+
GOOGLE_DRIVE_FULL_SCOPE,
|
|
7
|
+
GOOGLE_DRIVE_PUBLICATION_CREATE_ACTION,
|
|
8
|
+
GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
9
|
+
GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
10
|
+
GOOGLE_DRIVE_PROVIDER_DOMAIN,
|
|
11
|
+
GoogleDriveConnectionMetadata,
|
|
12
|
+
GoogleDrivePublicationReceipt,
|
|
13
|
+
GoogleDrivePublicationToolInput,
|
|
14
|
+
googleDriveScopesAllowCapability,
|
|
15
|
+
type GoogleDriveOutputDestination,
|
|
16
|
+
type GoogleDrivePublicationReceipt as GoogleDrivePublicationReceiptValue,
|
|
17
|
+
type GoogleDrivePublicationToolInput as GoogleDrivePublicationToolInputValue,
|
|
18
|
+
} from "@opengeni/contracts/google-drive";
|
|
19
|
+
import {
|
|
20
|
+
beginConnectorActionExecution,
|
|
21
|
+
completeConnectorActionExecution,
|
|
22
|
+
getConnectionMetadata,
|
|
23
|
+
getWorkspaceGrant,
|
|
24
|
+
prepareConnectorActionApproval,
|
|
25
|
+
requireFile,
|
|
26
|
+
type ConnectorActionAttemptIdentity,
|
|
27
|
+
type ConnectorActionInvocation,
|
|
28
|
+
type Database,
|
|
29
|
+
type ResolveConnectionCredentialInput,
|
|
30
|
+
type ResolveConnectionCredentialResult,
|
|
31
|
+
} from "@opengeni/db";
|
|
32
|
+
import {
|
|
33
|
+
readAuthorizedEditableArtifactMaterialization,
|
|
34
|
+
type PersistedEditableArtifactMaterializationJob,
|
|
35
|
+
} from "@opengeni/db/editable-artifact-durable-export";
|
|
36
|
+
import { readResponseJsonBounded, undiciFetch, type FetchLike } from "@opengeni/network";
|
|
37
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
38
|
+
|
|
39
|
+
const DRIVE_API = "https://www.googleapis.com/drive/v3";
|
|
40
|
+
const DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3";
|
|
41
|
+
const DRIVE_FOLDER_MIME = "application/vnd.google-apps.folder";
|
|
42
|
+
const RESPONSE_MAX_BYTES = 256 * 1024;
|
|
43
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
44
|
+
const FIRST_PARTY_MCP_SUBJECT_ID = "worker:first-party-mcp";
|
|
45
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u;
|
|
46
|
+
|
|
47
|
+
export type GoogleDrivePublicationTarget = Readonly<{
|
|
48
|
+
ownerSubjectId: string;
|
|
49
|
+
connectionId: string;
|
|
50
|
+
destination: GoogleDriveOutputDestination;
|
|
51
|
+
credentialScope: typeof GOOGLE_DRIVE_FILE_SCOPE | typeof GOOGLE_DRIVE_FULL_SCOPE;
|
|
52
|
+
}>;
|
|
53
|
+
|
|
54
|
+
export type GoogleDrivePublicationPorts = {
|
|
55
|
+
getConnection: typeof getConnectionMetadata;
|
|
56
|
+
getMembership: typeof getWorkspaceGrant;
|
|
57
|
+
readMaterialization: typeof readAuthorizedEditableArtifactMaterialization;
|
|
58
|
+
requireFile: typeof requireFile;
|
|
59
|
+
prepare: typeof prepareConnectorActionApproval;
|
|
60
|
+
begin: typeof beginConnectorActionExecution;
|
|
61
|
+
complete: typeof completeConnectorActionExecution;
|
|
62
|
+
fetch: FetchLike;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const defaultPorts: GoogleDrivePublicationPorts = {
|
|
66
|
+
getConnection: getConnectionMetadata,
|
|
67
|
+
getMembership: getWorkspaceGrant,
|
|
68
|
+
readMaterialization: readAuthorizedEditableArtifactMaterialization,
|
|
69
|
+
requireFile,
|
|
70
|
+
prepare: prepareConnectorActionApproval,
|
|
71
|
+
begin: beginConnectorActionExecution,
|
|
72
|
+
complete: completeConnectorActionExecution,
|
|
73
|
+
fetch: undiciFetch,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export async function resolveGoogleDrivePublicationTarget(
|
|
77
|
+
db: Database,
|
|
78
|
+
workspaceId: string,
|
|
79
|
+
delegations: readonly McpPersonalConnectionDelegation[],
|
|
80
|
+
ports: Pick<GoogleDrivePublicationPorts, "getConnection" | "getMembership"> = defaultPorts,
|
|
81
|
+
): Promise<GoogleDrivePublicationTarget | null> {
|
|
82
|
+
const frozen = delegations.filter(
|
|
83
|
+
(delegation) =>
|
|
84
|
+
delegation.serverId === GOOGLE_DRIVE_PUBLICATION_SERVER_ID &&
|
|
85
|
+
delegation.providerDomain.toLowerCase() === GOOGLE_DRIVE_PROVIDER_DOMAIN &&
|
|
86
|
+
delegation.kind === "oauth2",
|
|
87
|
+
);
|
|
88
|
+
if (frozen.length !== 1) return null;
|
|
89
|
+
const delegation = frozen[0]!;
|
|
90
|
+
if (!(await ports.getMembership(db, delegation.ownerSubjectId, workspaceId))) return null;
|
|
91
|
+
const connection = await ports.getConnection(
|
|
92
|
+
db,
|
|
93
|
+
workspaceId,
|
|
94
|
+
delegation.connectionId,
|
|
95
|
+
delegation.ownerSubjectId,
|
|
96
|
+
);
|
|
97
|
+
if (
|
|
98
|
+
!connection ||
|
|
99
|
+
connection.id !== delegation.connectionId ||
|
|
100
|
+
connection.subjectId !== delegation.ownerSubjectId ||
|
|
101
|
+
connection.providerDomain.toLowerCase() !== GOOGLE_DRIVE_PROVIDER_DOMAIN ||
|
|
102
|
+
connection.kind !== "oauth2" ||
|
|
103
|
+
connection.status !== "active" ||
|
|
104
|
+
!googleDriveScopesAllowCapability(connection.grantedScopes, "publish_file")
|
|
105
|
+
) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const metadata = GoogleDriveConnectionMetadata.safeParse(connection.metadata);
|
|
109
|
+
if (
|
|
110
|
+
!metadata.success ||
|
|
111
|
+
!metadata.data.outputDestination ||
|
|
112
|
+
metadata.data.lifecycle?.state === "paused" ||
|
|
113
|
+
(metadata.data.lifecycle && metadata.data.lifecycle.state !== "active")
|
|
114
|
+
) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
return Object.freeze({
|
|
118
|
+
ownerSubjectId: delegation.ownerSubjectId,
|
|
119
|
+
connectionId: connection.id,
|
|
120
|
+
destination: metadata.data.outputDestination,
|
|
121
|
+
credentialScope: connection.grantedScopes.includes(GOOGLE_DRIVE_FILE_SCOPE)
|
|
122
|
+
? GOOGLE_DRIVE_FILE_SCOPE
|
|
123
|
+
: GOOGLE_DRIVE_FULL_SCOPE,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function googleDrivePublicationConnectorCall(
|
|
128
|
+
target: GoogleDrivePublicationTarget,
|
|
129
|
+
rawRequest: unknown,
|
|
130
|
+
approvalId: string,
|
|
131
|
+
): ConnectorActionInvocation {
|
|
132
|
+
const request = GoogleDrivePublicationToolInput.parse(rawRequest);
|
|
133
|
+
return {
|
|
134
|
+
approvalId,
|
|
135
|
+
connectionId: target.connectionId,
|
|
136
|
+
serverId: GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
137
|
+
toolName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
138
|
+
arguments: {
|
|
139
|
+
action: GOOGLE_DRIVE_PUBLICATION_CREATE_ACTION,
|
|
140
|
+
destination: target.destination,
|
|
141
|
+
artifact: {
|
|
142
|
+
id: request.file.artifactId,
|
|
143
|
+
versionId: request.file.versionId,
|
|
144
|
+
title: request.title,
|
|
145
|
+
modality: request.modality,
|
|
146
|
+
sourceSha256: request.file.sha256,
|
|
147
|
+
},
|
|
148
|
+
canonicalReceipt: { ...request.file },
|
|
149
|
+
idempotencyKeyDigest: createHash("sha256")
|
|
150
|
+
.update(request.idempotencyKey, "utf8")
|
|
151
|
+
.digest("hex"),
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function createGoogleDrivePublicationAttemptTool(input: {
|
|
157
|
+
db: Database;
|
|
158
|
+
objectStorage: ObjectStorage;
|
|
159
|
+
identity: ConnectorActionAttemptIdentity;
|
|
160
|
+
subjectId: string;
|
|
161
|
+
target: GoogleDrivePublicationTarget;
|
|
162
|
+
resolveCredential: (
|
|
163
|
+
input: ResolveConnectionCredentialInput,
|
|
164
|
+
) => Promise<ResolveConnectionCredentialResult>;
|
|
165
|
+
signal?: AbortSignal;
|
|
166
|
+
ports?: GoogleDrivePublicationPorts;
|
|
167
|
+
}): AttemptToolDefinition {
|
|
168
|
+
const ports = input.ports ?? defaultPorts;
|
|
169
|
+
return {
|
|
170
|
+
identity: {
|
|
171
|
+
serverId: GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
172
|
+
toolName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
173
|
+
},
|
|
174
|
+
modelName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
175
|
+
codemodePath: ["google_drive", "publish_file"],
|
|
176
|
+
title: "Publish artifact to Google Drive",
|
|
177
|
+
description:
|
|
178
|
+
"Publish a completed editable-artifact Office export into the configured Google Drive output folder. First call editable_artifact_export and poll editable_artifact_export_status until it returns a file receipt, then pass that exact receipt here.",
|
|
179
|
+
inputSchema: {
|
|
180
|
+
type: "object",
|
|
181
|
+
properties: {
|
|
182
|
+
title: { type: "string", minLength: 1, maxLength: 512 },
|
|
183
|
+
modality: {
|
|
184
|
+
type: "string",
|
|
185
|
+
enum: ["document", "spreadsheet", "presentation"],
|
|
186
|
+
},
|
|
187
|
+
file: {
|
|
188
|
+
type: "object",
|
|
189
|
+
properties: {
|
|
190
|
+
fileId: { type: "string", format: "uuid" },
|
|
191
|
+
filename: { type: "string", minLength: 1, maxLength: 512 },
|
|
192
|
+
contentType: { type: "string", minLength: 1, maxLength: 256 },
|
|
193
|
+
sizeBytes: { type: "integer", minimum: 1 },
|
|
194
|
+
sha256: { type: "string", pattern: "^[0-9a-f]{64}$" },
|
|
195
|
+
artifactId: { type: "string", pattern: "^[0-9a-f]{32}$" },
|
|
196
|
+
versionId: { type: "string", pattern: "^[0-9a-f]{32}$" },
|
|
197
|
+
materializationJobId: { type: "string", pattern: "^[0-9a-f]{32}$" },
|
|
198
|
+
sourceHeadSequence: { type: "integer", minimum: 0 },
|
|
199
|
+
sourceStateHash: {
|
|
200
|
+
type: "string",
|
|
201
|
+
pattern: "^sha256:[0-9a-f]{64}$",
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
required: [
|
|
205
|
+
"fileId",
|
|
206
|
+
"filename",
|
|
207
|
+
"contentType",
|
|
208
|
+
"sizeBytes",
|
|
209
|
+
"sha256",
|
|
210
|
+
"artifactId",
|
|
211
|
+
"versionId",
|
|
212
|
+
"materializationJobId",
|
|
213
|
+
"sourceHeadSequence",
|
|
214
|
+
"sourceStateHash",
|
|
215
|
+
],
|
|
216
|
+
additionalProperties: false,
|
|
217
|
+
},
|
|
218
|
+
idempotencyKey: { type: "string", minLength: 1, maxLength: 200 },
|
|
219
|
+
},
|
|
220
|
+
required: ["title", "modality", "file", "idempotencyKey"],
|
|
221
|
+
additionalProperties: false,
|
|
222
|
+
},
|
|
223
|
+
outputSchema: {
|
|
224
|
+
type: "object",
|
|
225
|
+
properties: {
|
|
226
|
+
connectionId: { type: "string", format: "uuid" },
|
|
227
|
+
providerFileId: { type: "string" },
|
|
228
|
+
webViewLink: { type: "string", format: "uri" },
|
|
229
|
+
mimeType: {
|
|
230
|
+
type: "string",
|
|
231
|
+
enum: [
|
|
232
|
+
"application/vnd.google-apps.document",
|
|
233
|
+
"application/vnd.google-apps.spreadsheet",
|
|
234
|
+
"application/vnd.google-apps.presentation",
|
|
235
|
+
],
|
|
236
|
+
},
|
|
237
|
+
destination: {
|
|
238
|
+
type: "object",
|
|
239
|
+
properties: {
|
|
240
|
+
folderId: { type: "string" },
|
|
241
|
+
folderName: { type: "string" },
|
|
242
|
+
driveId: { type: ["string", "null"] },
|
|
243
|
+
location: { type: "string", enum: ["my_drive", "shared_drive"] },
|
|
244
|
+
selectedAt: { type: "string" },
|
|
245
|
+
},
|
|
246
|
+
required: ["folderId", "folderName", "driveId", "location", "selectedAt"],
|
|
247
|
+
additionalProperties: false,
|
|
248
|
+
},
|
|
249
|
+
replayed: { type: "boolean" },
|
|
250
|
+
},
|
|
251
|
+
required: [
|
|
252
|
+
"connectionId",
|
|
253
|
+
"providerFileId",
|
|
254
|
+
"webViewLink",
|
|
255
|
+
"mimeType",
|
|
256
|
+
"destination",
|
|
257
|
+
"replayed",
|
|
258
|
+
],
|
|
259
|
+
additionalProperties: false,
|
|
260
|
+
},
|
|
261
|
+
annotations: {
|
|
262
|
+
title: "Publish artifact to Google Drive",
|
|
263
|
+
readOnlyHint: false,
|
|
264
|
+
destructiveHint: false,
|
|
265
|
+
idempotentHint: true,
|
|
266
|
+
openWorldHint: true,
|
|
267
|
+
},
|
|
268
|
+
source: "mcp",
|
|
269
|
+
approval: "policy",
|
|
270
|
+
execute: async (raw, context) => {
|
|
271
|
+
const request = GoogleDrivePublicationToolInput.parse(raw);
|
|
272
|
+
const connectorCall = googleDrivePublicationConnectorCall(
|
|
273
|
+
input.target,
|
|
274
|
+
request,
|
|
275
|
+
context.operationId,
|
|
276
|
+
);
|
|
277
|
+
let requestId: string | null = null;
|
|
278
|
+
if (context.caller.kind === "codemode") {
|
|
279
|
+
const admission = await ports.begin(input.db, input.identity, connectorCall);
|
|
280
|
+
if (!admission.allowed) {
|
|
281
|
+
throw new Error(`Google Drive publication was not executed: ${admission.reason}`);
|
|
282
|
+
}
|
|
283
|
+
if (!admission.managed) {
|
|
284
|
+
throw new Error("Google Drive publication has no explicit connector action policy");
|
|
285
|
+
}
|
|
286
|
+
requestId = admission.requestId;
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
const receipt = await executeGoogleDrivePublication(
|
|
290
|
+
{
|
|
291
|
+
db: input.db,
|
|
292
|
+
objectStorage: input.objectStorage,
|
|
293
|
+
identity: input.identity,
|
|
294
|
+
subjectId: input.subjectId,
|
|
295
|
+
target: input.target,
|
|
296
|
+
request,
|
|
297
|
+
resolveCredential: input.resolveCredential,
|
|
298
|
+
...((context.signal ?? input.signal) ? { signal: context.signal ?? input.signal } : {}),
|
|
299
|
+
},
|
|
300
|
+
ports,
|
|
301
|
+
);
|
|
302
|
+
if (requestId) {
|
|
303
|
+
await ports.complete(input.db, {
|
|
304
|
+
accountId: input.identity.accountId,
|
|
305
|
+
workspaceId: input.identity.workspaceId,
|
|
306
|
+
requestId,
|
|
307
|
+
attemptId: input.identity.attemptId,
|
|
308
|
+
outcome: "completed",
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
content: [{ type: "text", text: JSON.stringify(receipt) }],
|
|
313
|
+
structuredContent: receipt,
|
|
314
|
+
};
|
|
315
|
+
} catch (error) {
|
|
316
|
+
if (requestId) {
|
|
317
|
+
await ports.complete(input.db, {
|
|
318
|
+
accountId: input.identity.accountId,
|
|
319
|
+
workspaceId: input.identity.workspaceId,
|
|
320
|
+
requestId,
|
|
321
|
+
attemptId: input.identity.attemptId,
|
|
322
|
+
outcome: "uncertain",
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export async function authorizeGoogleDrivePublicationAttempt(input: {
|
|
332
|
+
db: Database;
|
|
333
|
+
identity: ConnectorActionAttemptIdentity;
|
|
334
|
+
target: GoogleDrivePublicationTarget;
|
|
335
|
+
approvalId: string;
|
|
336
|
+
arguments: unknown;
|
|
337
|
+
ports?: Pick<GoogleDrivePublicationPorts, "prepare">;
|
|
338
|
+
}): Promise<void> {
|
|
339
|
+
const request = GoogleDrivePublicationToolInput.parse(input.arguments);
|
|
340
|
+
const preparation = await (input.ports ?? defaultPorts).prepare(
|
|
341
|
+
input.db,
|
|
342
|
+
input.identity,
|
|
343
|
+
googleDrivePublicationConnectorCall(input.target, request, input.approvalId),
|
|
344
|
+
);
|
|
345
|
+
if (!preparation.managed) {
|
|
346
|
+
throw new Error("Google Drive publication has no explicit connector action policy");
|
|
347
|
+
}
|
|
348
|
+
if (preparation.decision === "ask") throw new AttemptToolApprovalRequiredError();
|
|
349
|
+
if (preparation.decision === "block") {
|
|
350
|
+
throw new Error("Google Drive publication is blocked by connector action policy");
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export async function executeGoogleDrivePublication(
|
|
355
|
+
input: {
|
|
356
|
+
db: Database;
|
|
357
|
+
objectStorage: ObjectStorage;
|
|
358
|
+
identity: ConnectorActionAttemptIdentity;
|
|
359
|
+
subjectId: string;
|
|
360
|
+
target: GoogleDrivePublicationTarget;
|
|
361
|
+
request: GoogleDrivePublicationToolInputValue;
|
|
362
|
+
resolveCredential: (
|
|
363
|
+
input: ResolveConnectionCredentialInput,
|
|
364
|
+
) => Promise<ResolveConnectionCredentialResult>;
|
|
365
|
+
signal?: AbortSignal;
|
|
366
|
+
},
|
|
367
|
+
ports: Pick<
|
|
368
|
+
GoogleDrivePublicationPorts,
|
|
369
|
+
"getConnection" | "readMaterialization" | "requireFile" | "fetch"
|
|
370
|
+
> = defaultPorts,
|
|
371
|
+
): Promise<GoogleDrivePublicationReceiptValue> {
|
|
372
|
+
const request = GoogleDrivePublicationToolInput.parse(input.request);
|
|
373
|
+
const connection = await ports.getConnection(
|
|
374
|
+
input.db,
|
|
375
|
+
input.identity.workspaceId,
|
|
376
|
+
input.target.connectionId,
|
|
377
|
+
input.subjectId,
|
|
378
|
+
);
|
|
379
|
+
if (!connection || connection.subjectId !== input.subjectId) {
|
|
380
|
+
throw new Error("Google Drive publication connection is unavailable");
|
|
381
|
+
}
|
|
382
|
+
assertPublicationConnection(input, connection);
|
|
383
|
+
const materialization = await ports.readMaterialization(input.db, {
|
|
384
|
+
scope: {
|
|
385
|
+
accountId: input.identity.accountId,
|
|
386
|
+
workspaceId: input.identity.workspaceId,
|
|
387
|
+
},
|
|
388
|
+
artifactId: request.file.artifactId,
|
|
389
|
+
jobId: request.file.materializationJobId,
|
|
390
|
+
actor: {
|
|
391
|
+
kind: "agent",
|
|
392
|
+
subjectId: FIRST_PARTY_MCP_SUBJECT_ID,
|
|
393
|
+
replicaId: publicationActorReplicaId(input.identity),
|
|
394
|
+
sessionId: input.identity.sessionId,
|
|
395
|
+
turnId: input.identity.turnId,
|
|
396
|
+
attemptId: input.identity.attemptId,
|
|
397
|
+
generation: input.identity.executionGeneration,
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
assertCanonicalMaterialization(input.identity, request, materialization);
|
|
401
|
+
const expected = expectedExportWorkspaceFile(input.identity.workspaceId, request);
|
|
402
|
+
if (request.file.fileId !== expected.fileId) {
|
|
403
|
+
throw new Error("Google Drive publication requires a canonical editable-artifact export file");
|
|
404
|
+
}
|
|
405
|
+
const sourceFile = await ports.requireFile(
|
|
406
|
+
input.db,
|
|
407
|
+
input.identity.workspaceId,
|
|
408
|
+
request.file.fileId,
|
|
409
|
+
);
|
|
410
|
+
if (
|
|
411
|
+
sourceFile.status !== "ready" ||
|
|
412
|
+
sourceFile.id !== expected.fileId ||
|
|
413
|
+
sourceFile.safeFilename !== expected.safeFilename ||
|
|
414
|
+
sourceFile.objectKey !== expected.objectKey ||
|
|
415
|
+
sourceFile.filename !== request.file.filename ||
|
|
416
|
+
sourceFile.contentType !== request.file.contentType ||
|
|
417
|
+
sourceFile.sizeBytes !== request.file.sizeBytes ||
|
|
418
|
+
sourceFile.sha256 !== request.file.sha256
|
|
419
|
+
) {
|
|
420
|
+
throw new Error("Editable artifact export file differs from its canonical receipt");
|
|
421
|
+
}
|
|
422
|
+
const bytes = await input.objectStorage.getFileBytes(sourceFile);
|
|
423
|
+
if (
|
|
424
|
+
bytes.byteLength !== request.file.sizeBytes ||
|
|
425
|
+
createHash("sha256").update(bytes).digest("hex") !== request.file.sha256
|
|
426
|
+
) {
|
|
427
|
+
throw new Error("Editable artifact export bytes failed verification");
|
|
428
|
+
}
|
|
429
|
+
const credential = await input.resolveCredential({
|
|
430
|
+
workspaceId: input.identity.workspaceId,
|
|
431
|
+
subjectId: input.subjectId,
|
|
432
|
+
serverId: GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
433
|
+
toolName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
434
|
+
connectionRef: {
|
|
435
|
+
providerDomain: GOOGLE_DRIVE_PROVIDER_DOMAIN,
|
|
436
|
+
connectionId: input.target.connectionId,
|
|
437
|
+
kind: "oauth2",
|
|
438
|
+
subjectScope: "subject",
|
|
439
|
+
scopes: [input.target.credentialScope],
|
|
440
|
+
},
|
|
441
|
+
destinationUrl: `${DRIVE_UPLOAD_API}/files`,
|
|
442
|
+
forceRefresh: false,
|
|
443
|
+
});
|
|
444
|
+
if (credential.status !== "ok" || credential.connectionId !== input.target.connectionId) {
|
|
445
|
+
throw new Error("Google Drive publication credential is unavailable");
|
|
446
|
+
}
|
|
447
|
+
const resolvedConnection = await ports.getConnection(
|
|
448
|
+
input.db,
|
|
449
|
+
input.identity.workspaceId,
|
|
450
|
+
input.target.connectionId,
|
|
451
|
+
input.subjectId,
|
|
452
|
+
);
|
|
453
|
+
if (!resolvedConnection) {
|
|
454
|
+
throw new Error("Google Drive publication authority changed; retry on a new turn");
|
|
455
|
+
}
|
|
456
|
+
assertPublicationConnection(input, resolvedConnection);
|
|
457
|
+
return await publishToGoogleDrive({
|
|
458
|
+
workspaceId: input.identity.workspaceId,
|
|
459
|
+
connectionId: input.target.connectionId,
|
|
460
|
+
destination: input.target.destination,
|
|
461
|
+
request,
|
|
462
|
+
bytes,
|
|
463
|
+
headers: credential.headers,
|
|
464
|
+
fetchImpl: ports.fetch,
|
|
465
|
+
...(input.signal ? { signal: input.signal } : {}),
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function assertPublicationConnection(
|
|
470
|
+
input: { subjectId: string; target: GoogleDrivePublicationTarget },
|
|
471
|
+
connection: NonNullable<Awaited<ReturnType<typeof getConnectionMetadata>>>,
|
|
472
|
+
): void {
|
|
473
|
+
const metadata = GoogleDriveConnectionMetadata.parse(connection.metadata);
|
|
474
|
+
const credentialScope = connection.grantedScopes.includes(GOOGLE_DRIVE_FILE_SCOPE)
|
|
475
|
+
? GOOGLE_DRIVE_FILE_SCOPE
|
|
476
|
+
: GOOGLE_DRIVE_FULL_SCOPE;
|
|
477
|
+
if (
|
|
478
|
+
input.subjectId !== input.target.ownerSubjectId ||
|
|
479
|
+
connection.id !== input.target.connectionId ||
|
|
480
|
+
connection.subjectId !== input.target.ownerSubjectId ||
|
|
481
|
+
connection.providerDomain.toLowerCase() !== GOOGLE_DRIVE_PROVIDER_DOMAIN ||
|
|
482
|
+
connection.kind !== "oauth2" ||
|
|
483
|
+
connection.status !== "active" ||
|
|
484
|
+
metadata.lifecycle?.state === "paused" ||
|
|
485
|
+
(metadata.lifecycle && metadata.lifecycle.state !== "active") ||
|
|
486
|
+
!googleDriveScopesAllowCapability(connection.grantedScopes, "publish_file") ||
|
|
487
|
+
credentialScope !== input.target.credentialScope ||
|
|
488
|
+
!metadata.outputDestination ||
|
|
489
|
+
!sameDestination(metadata.outputDestination, input.target.destination)
|
|
490
|
+
) {
|
|
491
|
+
throw new Error("Google Drive publication authority changed; retry on a new turn");
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function publicationActorReplicaId(identity: ConnectorActionAttemptIdentity): string {
|
|
496
|
+
const value = createHash("sha256")
|
|
497
|
+
.update(
|
|
498
|
+
`google-drive-publication:${identity.sessionId}:${identity.turnId}:${identity.attemptId}:${identity.executionGeneration}`,
|
|
499
|
+
"utf8",
|
|
500
|
+
)
|
|
501
|
+
.digest("hex")
|
|
502
|
+
.slice(0, 16);
|
|
503
|
+
return /^0+$/u.test(value) ? `1${value.slice(1)}` : value;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function assertCanonicalMaterialization(
|
|
507
|
+
identity: ConnectorActionAttemptIdentity,
|
|
508
|
+
request: GoogleDrivePublicationToolInputValue,
|
|
509
|
+
job: PersistedEditableArtifactMaterializationJob | null,
|
|
510
|
+
): void {
|
|
511
|
+
const format = officeFormat(request.modality, request.file.contentType);
|
|
512
|
+
if (
|
|
513
|
+
!job ||
|
|
514
|
+
job.scope.accountId !== identity.accountId ||
|
|
515
|
+
job.scope.workspaceId !== identity.workspaceId ||
|
|
516
|
+
job.artifactId !== request.file.artifactId ||
|
|
517
|
+
job.id !== request.file.materializationJobId ||
|
|
518
|
+
job.versionId !== request.file.versionId ||
|
|
519
|
+
job.targetHeadSequence !== request.file.sourceHeadSequence ||
|
|
520
|
+
job.stateHash !== request.file.sourceStateHash ||
|
|
521
|
+
job.format !== format ||
|
|
522
|
+
job.state !== "succeeded" ||
|
|
523
|
+
!job.result ||
|
|
524
|
+
job.result.byteSize !== request.file.sizeBytes ||
|
|
525
|
+
job.result.mimeType !== request.file.contentType ||
|
|
526
|
+
job.result.contentHash !== `sha256:${request.file.sha256}`
|
|
527
|
+
) {
|
|
528
|
+
throw new Error("Google Drive publication receipt differs from its canonical materialization");
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function expectedExportWorkspaceFile(
|
|
533
|
+
workspaceId: string,
|
|
534
|
+
request: GoogleDrivePublicationToolInputValue,
|
|
535
|
+
): { fileId: string; safeFilename: string; objectKey: string } {
|
|
536
|
+
const format = officeFormat(request.modality, request.file.contentType);
|
|
537
|
+
const fileId = deterministicUuid(
|
|
538
|
+
`editable-artifact-export:file:${workspaceId}:${request.file.artifactId}:${request.file.versionId}:${request.file.materializationJobId}`,
|
|
539
|
+
);
|
|
540
|
+
const safeFilename = `artifact-${request.file.artifactId}-${request.file.materializationJobId}.${format}`;
|
|
541
|
+
return {
|
|
542
|
+
fileId,
|
|
543
|
+
safeFilename,
|
|
544
|
+
objectKey: `workspaces/${workspaceId}/files/${fileId}/artifact-exports/${safeFilename}`,
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function officeFormat(
|
|
549
|
+
modality: GoogleDrivePublicationToolInputValue["modality"],
|
|
550
|
+
contentType: string,
|
|
551
|
+
): "docx" | "xlsx" | "pptx" {
|
|
552
|
+
const expected = {
|
|
553
|
+
document: {
|
|
554
|
+
format: "docx" as const,
|
|
555
|
+
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
556
|
+
},
|
|
557
|
+
spreadsheet: {
|
|
558
|
+
format: "xlsx" as const,
|
|
559
|
+
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
560
|
+
},
|
|
561
|
+
presentation: {
|
|
562
|
+
format: "pptx" as const,
|
|
563
|
+
contentType: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
564
|
+
},
|
|
565
|
+
}[modality];
|
|
566
|
+
if (contentType !== expected.contentType) {
|
|
567
|
+
throw new Error("Google Drive native publication requires the matching Office export format");
|
|
568
|
+
}
|
|
569
|
+
return expected.format;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function deterministicUuid(seed: string): string {
|
|
573
|
+
const bytes = createHash("sha256").update(seed, "utf8").digest().subarray(0, 16);
|
|
574
|
+
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x50;
|
|
575
|
+
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80;
|
|
576
|
+
const hex = bytes.toString("hex");
|
|
577
|
+
const value = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
578
|
+
if (!UUID_PATTERN.test(value)) throw new Error("Generated workspace file identity is invalid");
|
|
579
|
+
return value;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
async function publishToGoogleDrive(input: {
|
|
583
|
+
workspaceId: string;
|
|
584
|
+
connectionId: string;
|
|
585
|
+
destination: GoogleDriveOutputDestination;
|
|
586
|
+
request: GoogleDrivePublicationToolInputValue;
|
|
587
|
+
bytes: Uint8Array;
|
|
588
|
+
headers: Record<string, string>;
|
|
589
|
+
fetchImpl: FetchLike;
|
|
590
|
+
signal?: AbortSignal;
|
|
591
|
+
}): Promise<GoogleDrivePublicationReceiptValue> {
|
|
592
|
+
await verifyDestinationAtWriteTime(input);
|
|
593
|
+
const operationKey = publicationOperationKey(input);
|
|
594
|
+
const existing = await findExistingPublication(input, operationKey);
|
|
595
|
+
if (existing) return driveReceipt(input, existing, true);
|
|
596
|
+
|
|
597
|
+
const mimeType = googleNativeMime(input.request.modality);
|
|
598
|
+
const boundary = `opengeni_${operationKey.slice(0, 32)}`;
|
|
599
|
+
const metadata = JSON.stringify({
|
|
600
|
+
name: input.request.title,
|
|
601
|
+
mimeType,
|
|
602
|
+
parents: [input.destination.folderId],
|
|
603
|
+
appProperties: {
|
|
604
|
+
opengeniPublicationKey: operationKey,
|
|
605
|
+
opengeniArtifactId: input.request.file.artifactId,
|
|
606
|
+
opengeniArtifactVersionId: input.request.file.versionId,
|
|
607
|
+
opengeniSourceSha256: input.request.file.sha256,
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
const prefix = new TextEncoder().encode(
|
|
611
|
+
`--${boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n${metadata}\r\n--${boundary}\r\nContent-Type: ${input.request.file.contentType}\r\n\r\n`,
|
|
612
|
+
);
|
|
613
|
+
const suffix = new TextEncoder().encode(`\r\n--${boundary}--\r\n`);
|
|
614
|
+
const body = new Uint8Array(prefix.byteLength + input.bytes.byteLength + suffix.byteLength);
|
|
615
|
+
body.set(prefix, 0);
|
|
616
|
+
body.set(input.bytes, prefix.byteLength);
|
|
617
|
+
body.set(suffix, prefix.byteLength + input.bytes.byteLength);
|
|
618
|
+
const url = new URL(`${DRIVE_UPLOAD_API}/files`);
|
|
619
|
+
url.searchParams.set("uploadType", "multipart");
|
|
620
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
621
|
+
url.searchParams.set(
|
|
622
|
+
"fields",
|
|
623
|
+
"id,name,mimeType,parents,driveId,webViewLink,appProperties,trashed",
|
|
624
|
+
);
|
|
625
|
+
const created = parseDriveFile(
|
|
626
|
+
await requestJson(input.fetchImpl, url, {
|
|
627
|
+
method: "POST",
|
|
628
|
+
headers: {
|
|
629
|
+
...input.headers,
|
|
630
|
+
accept: "application/json",
|
|
631
|
+
"content-type": `multipart/related; boundary=${boundary}`,
|
|
632
|
+
"content-length": String(body.byteLength),
|
|
633
|
+
},
|
|
634
|
+
body,
|
|
635
|
+
...(input.signal ? { signal: input.signal } : {}),
|
|
636
|
+
}),
|
|
637
|
+
);
|
|
638
|
+
assertPublicationMatch(input, created, operationKey);
|
|
639
|
+
return driveReceipt(input, created, false);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
async function verifyDestinationAtWriteTime(
|
|
643
|
+
input: Parameters<typeof publishToGoogleDrive>[0],
|
|
644
|
+
): Promise<void> {
|
|
645
|
+
const url = new URL(`${DRIVE_API}/files/${encodeURIComponent(input.destination.folderId)}`);
|
|
646
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
647
|
+
url.searchParams.set("fields", "id,name,mimeType,driveId,trashed,capabilities(canAddChildren)");
|
|
648
|
+
const folder = record(await requestJson(input.fetchImpl, url, { headers: input.headers }));
|
|
649
|
+
const capabilities = record(folder.capabilities);
|
|
650
|
+
if (
|
|
651
|
+
folder.id !== input.destination.folderId ||
|
|
652
|
+
folder.name !== input.destination.folderName ||
|
|
653
|
+
folder.mimeType !== DRIVE_FOLDER_MIME ||
|
|
654
|
+
folder.trashed === true ||
|
|
655
|
+
capabilities.canAddChildren !== true ||
|
|
656
|
+
(folder.driveId ?? null) !== input.destination.driveId
|
|
657
|
+
) {
|
|
658
|
+
throw new Error("Google Drive output destination changed or is no longer writable");
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
async function findExistingPublication(
|
|
663
|
+
input: Parameters<typeof publishToGoogleDrive>[0],
|
|
664
|
+
operationKey: string,
|
|
665
|
+
): Promise<DriveFile | null> {
|
|
666
|
+
const url = new URL(`${DRIVE_API}/files`);
|
|
667
|
+
url.searchParams.set(
|
|
668
|
+
"q",
|
|
669
|
+
`trashed = false and '${driveQuery(input.destination.folderId)}' in parents and appProperties has { key='opengeniPublicationKey' and value='${operationKey}' }`,
|
|
670
|
+
);
|
|
671
|
+
url.searchParams.set("spaces", "drive");
|
|
672
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
673
|
+
url.searchParams.set("includeItemsFromAllDrives", "true");
|
|
674
|
+
if (input.destination.driveId) {
|
|
675
|
+
url.searchParams.set("corpora", "drive");
|
|
676
|
+
url.searchParams.set("driveId", input.destination.driveId);
|
|
677
|
+
} else {
|
|
678
|
+
url.searchParams.set("corpora", "user");
|
|
679
|
+
}
|
|
680
|
+
url.searchParams.set(
|
|
681
|
+
"fields",
|
|
682
|
+
"files(id,name,mimeType,parents,driveId,webViewLink,appProperties,trashed)",
|
|
683
|
+
);
|
|
684
|
+
const payload = record(await requestJson(input.fetchImpl, url, { headers: input.headers }));
|
|
685
|
+
const files = Array.isArray(payload.files) ? payload.files.map(parseDriveFile) : [];
|
|
686
|
+
if (files.length > 1) throw new Error("Google Drive publication idempotency marker is ambiguous");
|
|
687
|
+
const existing = files[0] ?? null;
|
|
688
|
+
if (existing) assertPublicationMatch(input, existing, operationKey);
|
|
689
|
+
return existing;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
type DriveFile = {
|
|
693
|
+
id: string;
|
|
694
|
+
name: string;
|
|
695
|
+
mimeType: string;
|
|
696
|
+
parents: string[];
|
|
697
|
+
driveId: string | null;
|
|
698
|
+
webViewLink: string;
|
|
699
|
+
appProperties: Record<string, string>;
|
|
700
|
+
trashed: boolean;
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
function parseDriveFile(value: unknown): DriveFile {
|
|
704
|
+
const item = record(value);
|
|
705
|
+
const appProperties = record(item.appProperties);
|
|
706
|
+
if (
|
|
707
|
+
typeof item.id !== "string" ||
|
|
708
|
+
typeof item.name !== "string" ||
|
|
709
|
+
typeof item.mimeType !== "string" ||
|
|
710
|
+
!Array.isArray(item.parents) ||
|
|
711
|
+
!item.parents.every((entry) => typeof entry === "string") ||
|
|
712
|
+
(item.driveId !== undefined && item.driveId !== null && typeof item.driveId !== "string") ||
|
|
713
|
+
typeof item.webViewLink !== "string" ||
|
|
714
|
+
typeof item.trashed !== "boolean"
|
|
715
|
+
) {
|
|
716
|
+
throw new Error("Google Drive returned an invalid publication receipt");
|
|
717
|
+
}
|
|
718
|
+
return {
|
|
719
|
+
id: item.id,
|
|
720
|
+
name: item.name,
|
|
721
|
+
mimeType: item.mimeType,
|
|
722
|
+
parents: item.parents,
|
|
723
|
+
driveId: (item.driveId as string | null | undefined) ?? null,
|
|
724
|
+
webViewLink: item.webViewLink,
|
|
725
|
+
appProperties: Object.fromEntries(
|
|
726
|
+
Object.entries(appProperties).filter(
|
|
727
|
+
(entry): entry is [string, string] => typeof entry[1] === "string",
|
|
728
|
+
),
|
|
729
|
+
),
|
|
730
|
+
trashed: item.trashed,
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
function assertPublicationMatch(
|
|
735
|
+
input: Parameters<typeof publishToGoogleDrive>[0],
|
|
736
|
+
file: DriveFile,
|
|
737
|
+
operationKey: string,
|
|
738
|
+
): void {
|
|
739
|
+
if (
|
|
740
|
+
file.trashed ||
|
|
741
|
+
file.name !== input.request.title ||
|
|
742
|
+
file.mimeType !== googleNativeMime(input.request.modality) ||
|
|
743
|
+
file.parents.length !== 1 ||
|
|
744
|
+
file.parents[0] !== input.destination.folderId ||
|
|
745
|
+
file.driveId !== input.destination.driveId ||
|
|
746
|
+
file.appProperties.opengeniPublicationKey !== operationKey ||
|
|
747
|
+
file.appProperties.opengeniArtifactId !== input.request.file.artifactId ||
|
|
748
|
+
file.appProperties.opengeniArtifactVersionId !== input.request.file.versionId ||
|
|
749
|
+
file.appProperties.opengeniSourceSha256 !== input.request.file.sha256
|
|
750
|
+
) {
|
|
751
|
+
throw new Error("Google Drive publication idempotency key conflicts with another file");
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function driveReceipt(
|
|
756
|
+
input: Parameters<typeof publishToGoogleDrive>[0],
|
|
757
|
+
file: DriveFile,
|
|
758
|
+
replayed: boolean,
|
|
759
|
+
): GoogleDrivePublicationReceiptValue {
|
|
760
|
+
return GoogleDrivePublicationReceipt.parse({
|
|
761
|
+
connectionId: input.connectionId,
|
|
762
|
+
providerFileId: file.id,
|
|
763
|
+
webViewLink: file.webViewLink,
|
|
764
|
+
mimeType: googleNativeMime(input.request.modality),
|
|
765
|
+
destination: input.destination,
|
|
766
|
+
replayed,
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function publicationOperationKey(input: Parameters<typeof publishToGoogleDrive>[0]): string {
|
|
771
|
+
return createHash("sha256")
|
|
772
|
+
.update(
|
|
773
|
+
JSON.stringify({
|
|
774
|
+
workspaceId: input.workspaceId,
|
|
775
|
+
connectionId: input.connectionId,
|
|
776
|
+
destination: input.destination,
|
|
777
|
+
artifactId: input.request.file.artifactId,
|
|
778
|
+
versionId: input.request.file.versionId,
|
|
779
|
+
materializationJobId: input.request.file.materializationJobId,
|
|
780
|
+
sourceSha256: input.request.file.sha256,
|
|
781
|
+
idempotencyKey: input.request.idempotencyKey,
|
|
782
|
+
}),
|
|
783
|
+
"utf8",
|
|
784
|
+
)
|
|
785
|
+
.digest("hex");
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
function googleNativeMime(
|
|
789
|
+
modality: GoogleDrivePublicationToolInputValue["modality"],
|
|
790
|
+
): GoogleDrivePublicationReceiptValue["mimeType"] {
|
|
791
|
+
switch (modality) {
|
|
792
|
+
case "document":
|
|
793
|
+
return "application/vnd.google-apps.document";
|
|
794
|
+
case "spreadsheet":
|
|
795
|
+
return "application/vnd.google-apps.spreadsheet";
|
|
796
|
+
case "presentation":
|
|
797
|
+
return "application/vnd.google-apps.presentation";
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
function sameDestination(
|
|
802
|
+
left: GoogleDriveOutputDestination,
|
|
803
|
+
right: GoogleDriveOutputDestination,
|
|
804
|
+
): boolean {
|
|
805
|
+
return (
|
|
806
|
+
left.folderId === right.folderId &&
|
|
807
|
+
left.folderName === right.folderName &&
|
|
808
|
+
left.driveId === right.driveId &&
|
|
809
|
+
left.location === right.location &&
|
|
810
|
+
left.selectedAt === right.selectedAt
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
async function requestJson(fetchImpl: FetchLike, url: URL, init: RequestInit): Promise<unknown> {
|
|
815
|
+
let response: Response;
|
|
816
|
+
try {
|
|
817
|
+
response = await fetchImpl(url, {
|
|
818
|
+
...init,
|
|
819
|
+
redirect: "error",
|
|
820
|
+
signal: init.signal ?? AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
821
|
+
});
|
|
822
|
+
} catch {
|
|
823
|
+
throw new Error("Google Drive publication transport failed");
|
|
824
|
+
}
|
|
825
|
+
if (!response.ok) {
|
|
826
|
+
await response.body?.cancel().catch(() => undefined);
|
|
827
|
+
throw new Error(`Google Drive publication request failed with status ${response.status}`);
|
|
828
|
+
}
|
|
829
|
+
return await readResponseJsonBounded(response, RESPONSE_MAX_BYTES, "Google Drive publication");
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
function record(value: unknown): Record<string, unknown> {
|
|
833
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
834
|
+
? (value as Record<string, unknown>)
|
|
835
|
+
: {};
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function driveQuery(value: string): string {
|
|
839
|
+
return value.replace(/\\/gu, "\\\\").replace(/'/gu, "\\'");
|
|
840
|
+
}
|