@lobu/runtime-device 21.2.1 → 23.0.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.
Files changed (233) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +91 -83
  3. package/npm-shrinkwrap.json +492 -438
  4. package/package.json +12 -2
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
  6. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +5 -6
  7. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +1 -0
  8. package/vendor/cli/dist/commands/_lib/apply/client.js +26 -7
  9. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +9 -1
  10. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +1 -0
  11. package/vendor/cli/dist/commands/_lib/apply/diff.js +46 -4
  12. package/vendor/cli/dist/commands/_lib/apply/map-config.js +18 -38
  13. package/vendor/cli/dist/commands/_lib/init-from-org/bootstrap.js +5 -17
  14. package/vendor/cli/dist/commands/init.d.ts +0 -2
  15. package/vendor/cli/dist/commands/init.js +0 -27
  16. package/vendor/cli/dist/commands/login.d.ts +17 -0
  17. package/vendor/cli/dist/commands/login.js +43 -17
  18. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +0 -8
  19. package/vendor/cli/dist/commands/memory/_lib/errors.js +0 -12
  20. package/vendor/cli/dist/commands/memory/_lib/mcp.js +6 -6
  21. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +4 -10
  22. package/vendor/cli/dist/config/define.d.ts +21 -15
  23. package/vendor/cli/dist/config/define.js +2 -2
  24. package/vendor/cli/dist/connectors/README.md +21 -6
  25. package/vendor/cli/dist/connectors/_google/actions.ts +270 -0
  26. package/vendor/cli/dist/connectors/_google/discovery/calendar_v3.json +1 -0
  27. package/vendor/cli/dist/connectors/_google/discovery/chat_v1.json +1 -0
  28. package/vendor/cli/dist/connectors/_google/discovery/docs_v1.json +1 -0
  29. package/vendor/cli/dist/connectors/_google/discovery/drive_v3.json +1 -0
  30. package/vendor/cli/dist/connectors/_google/discovery/gmail_v1.json +1 -0
  31. package/vendor/cli/dist/connectors/_google/discovery/people_v1.json +1 -0
  32. package/vendor/cli/dist/connectors/_google/discovery/sheets_v4.json +1 -0
  33. package/vendor/cli/dist/connectors/_google/discovery/tasks_v1.json +1 -0
  34. package/vendor/cli/dist/connectors/_google/discovery/youtube_v3.json +1 -0
  35. package/vendor/cli/dist/connectors/_google/discovery.ts +442 -0
  36. package/vendor/cli/dist/connectors/_google/errors.ts +91 -0
  37. package/vendor/cli/dist/connectors/_google/list-feed.ts +223 -0
  38. package/vendor/cli/dist/connectors/_google/policies.ts +127 -0
  39. package/vendor/cli/dist/connectors/agentid.ts +43 -0
  40. package/vendor/cli/dist/connectors/chat-user-identity.ts +2 -1
  41. package/vendor/cli/dist/connectors/github-automation-events.ts +9 -0
  42. package/vendor/cli/dist/connectors/github-identity.ts +28 -3
  43. package/vendor/cli/dist/connectors/github.ts +101 -59
  44. package/vendor/cli/dist/connectors/google_calendar.ts +207 -719
  45. package/vendor/cli/dist/connectors/google_drive.ts +16 -2
  46. package/vendor/cli/dist/connectors/google_gmail.ts +52 -33
  47. package/vendor/cli/dist/connectors/herdr.ts +406 -0
  48. package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
  49. package/vendor/cli/dist/connectors/jira.ts +35 -227
  50. package/vendor/cli/dist/connectors/linear.ts +7 -4
  51. package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
  52. package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
  53. package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
  54. package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
  55. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +1185 -0
  56. package/vendor/cli/dist/connectors/linkedin-inbox.ts +156 -0
  57. package/vendor/cli/dist/connectors/linkedin-like.ts +114 -0
  58. package/vendor/cli/dist/connectors/linkedin-takeout.ts +577 -0
  59. package/vendor/cli/dist/connectors/linkedin.ts +3998 -0
  60. package/vendor/cli/dist/connectors/postgres.ts +27 -2
  61. package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
  62. package/vendor/cli/dist/connectors/tiktok-inbox-page.js +230 -0
  63. package/vendor/cli/dist/connectors/tiktok-web-page.js +217 -0
  64. package/vendor/cli/dist/connectors/tiktok_web.ts +319 -0
  65. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +285 -567
  66. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +74 -455
  67. package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +135 -0
  68. package/vendor/cli/dist/connectors/whatsapp_web.ts +159 -618
  69. package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
  70. package/vendor/cli/dist/connectors/x-identity.ts +31 -2
  71. package/vendor/cli/dist/connectors/x.ts +539 -253
  72. package/vendor/cli/dist/index.js +1 -37
  73. package/vendor/cli/dist/internal/context.d.ts +10 -0
  74. package/vendor/cli/dist/internal/context.js +26 -16
  75. package/vendor/cli/dist/internal/node-version.d.ts +1 -1
  76. package/vendor/cli/dist/internal/node-version.js +1 -1
  77. package/vendor/cli/dist/internal/oauth.d.ts +3 -1
  78. package/vendor/cli/dist/internal/oauth.js +4 -3
  79. package/vendor/cli/dist/internal/runtime-components.d.ts +9 -0
  80. package/vendor/cli/dist/internal/runtime-components.js +67 -2
  81. package/vendor/cli/dist/mac-auth.js +17 -1
  82. package/vendor/cli/dist/providers.json +1 -1
  83. package/vendor/cli/dist/runtime-components.json +4 -4
  84. package/vendor/cli/dist/templates/AGENTS.md.tmpl +12 -5
  85. package/vendor/cli/dist/templates/README.md.tmpl +1 -1
  86. package/vendor/cli/dist/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  87. package/vendor/cli/dist/vendor/ora/index.d.ts +332 -0
  88. package/vendor/cli/dist/vendor/ora/index.js +424 -0
  89. package/vendor/cli/dist/vendor/ora/license +9 -0
  90. package/vendor/cli/dist/vendor/ora/package.json +64 -0
  91. package/vendor/cli/dist/vendor/ora/readme.md +329 -0
  92. package/vendor/cli/package.json +2 -2
  93. package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
  94. package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
  95. package/vendor/connector-sdk/dist/browser-requirement.d.ts +36 -0
  96. package/vendor/connector-sdk/dist/browser-requirement.js +60 -0
  97. package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
  98. package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
  99. package/vendor/connector-sdk/dist/connector-runtime.d.ts +3 -1
  100. package/vendor/connector-sdk/dist/connector-runtime.js +10 -0
  101. package/vendor/connector-sdk/dist/connector-types.d.ts +169 -45
  102. package/vendor/connector-sdk/dist/define-connector.d.ts +4 -2
  103. package/vendor/connector-sdk/dist/define-connector.js +3 -0
  104. package/vendor/connector-sdk/dist/index.d.ts +8 -2
  105. package/vendor/connector-sdk/dist/index.js +3 -0
  106. package/vendor/connector-sdk/dist/reaction-client-types.d.ts +16 -21
  107. package/vendor/connector-sdk/dist/reaction-client-types.typecheck.d.ts +5 -0
  108. package/vendor/connector-sdk/package.json +13 -1
  109. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +43 -30
  110. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +416 -330
  111. package/vendor/connector-worker/dist/agent-turn/memory.d.ts +3 -2
  112. package/vendor/connector-worker/dist/agent-turn/memory.js +3 -20
  113. package/vendor/connector-worker/dist/agent-turn/types.d.ts +3 -15
  114. package/vendor/connector-worker/dist/agent-turn/workspace.d.ts +2 -2
  115. package/vendor/connector-worker/dist/agent-turn/workspace.js +2 -4
  116. package/vendor/connector-worker/dist/daemon/action-monitor.d.ts +12 -0
  117. package/vendor/connector-worker/dist/daemon/action-monitor.js +25 -0
  118. package/vendor/connector-worker/dist/daemon/agent-turn.js +0 -9
  119. package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
  120. package/vendor/connector-worker/dist/daemon/automation.js +6 -6
  121. package/vendor/connector-worker/dist/daemon/builtins/index.d.ts +12 -0
  122. package/vendor/connector-worker/dist/daemon/builtins/index.js +61 -1
  123. package/vendor/connector-worker/dist/daemon/builtins/os-shell.d.ts +2 -1
  124. package/vendor/connector-worker/dist/daemon/builtins/os-shell.js +40 -27
  125. package/vendor/connector-worker/dist/daemon/client.d.ts +6 -4
  126. package/vendor/connector-worker/dist/daemon/client.js +3 -3
  127. package/vendor/connector-worker/dist/daemon/device-concurrency.d.ts +8 -0
  128. package/vendor/connector-worker/dist/daemon/device-concurrency.js +13 -0
  129. package/vendor/connector-worker/dist/daemon/executor.d.ts +1 -1
  130. package/vendor/connector-worker/dist/daemon/executor.js +48 -120
  131. package/vendor/connector-worker/dist/daemon/log.d.ts +2 -0
  132. package/vendor/connector-worker/dist/daemon/log.js +23 -2
  133. package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +23 -6
  134. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +57 -23
  135. package/vendor/connector-worker/dist/daemon/native-bridge/client.d.ts +6 -0
  136. package/vendor/connector-worker/dist/daemon/native-bridge/client.js +15 -0
  137. package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +29 -25
  138. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.d.ts +1 -1
  139. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.js +1 -0
  140. package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +3 -0
  141. package/vendor/connector-worker/dist/daemon/poll-loop.js +5 -1
  142. package/vendor/connector-worker/dist/daemon/start.d.ts +1 -0
  143. package/vendor/connector-worker/dist/daemon/start.js +15 -20
  144. package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
  145. package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
  146. package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
  147. package/vendor/connector-worker/dist/executor/interface.d.ts +34 -2
  148. package/vendor/connector-worker/dist/executor/isolate.js +66 -15
  149. package/vendor/connector-worker/dist/executor/redact.d.ts +1 -0
  150. package/vendor/connector-worker/dist/executor/redact.js +97 -26
  151. package/vendor/connector-worker/dist/executor/runtime.d.ts +7 -0
  152. package/vendor/connector-worker/dist/executor/runtime.js +26 -0
  153. package/vendor/connector-worker/dist/isolate/bridge.js +3 -1
  154. package/vendor/connector-worker/dist/mac-device-daemon.js +24 -29
  155. package/vendor/connector-worker/package.json +2 -2
  156. package/vendor/core/dist/agent-policy.js +1 -1
  157. package/vendor/core/dist/agent-store.d.ts +2 -0
  158. package/vendor/core/dist/agent-tooling.d.ts +1 -1
  159. package/vendor/core/dist/agent-tooling.js +1 -1
  160. package/vendor/core/dist/capabilities.d.ts +1 -1
  161. package/vendor/core/dist/capabilities.js +2 -1
  162. package/vendor/core/dist/connector-query-errors.d.ts +32 -0
  163. package/vendor/core/dist/connector-query-errors.js +41 -1
  164. package/vendor/core/dist/contracts/agent-settings.d.ts +0 -11
  165. package/vendor/core/dist/contracts/agent-settings.js +1 -7
  166. package/vendor/core/dist/contracts/tools/collection-presentation.d.ts +93 -0
  167. package/vendor/core/dist/contracts/tools/collection-presentation.js +88 -0
  168. package/vendor/core/dist/contracts/tools/collection-selection.d.ts +47 -0
  169. package/vendor/core/dist/contracts/tools/collection-selection.js +74 -0
  170. package/vendor/core/dist/contracts/tools/manage-agents.d.ts +10 -4
  171. package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +1 -1
  172. package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +5 -1
  173. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +19 -4
  174. package/vendor/core/dist/contracts/tools/manage-automations.js +25 -8
  175. package/vendor/core/dist/contracts/tools/manage-connections.d.ts +1 -0
  176. package/vendor/core/dist/contracts/tools/manage-connections.js +3 -0
  177. package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
  178. package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +11 -0
  179. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +42 -3
  180. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +181 -152
  181. package/vendor/core/dist/contracts/tools/manage-entity.js +118 -162
  182. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
  183. package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
  184. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +41 -3
  185. package/vendor/core/dist/contracts/tools/manage-operations.js +40 -2
  186. package/vendor/core/dist/contracts/tools/manage-views.d.ts +182 -7
  187. package/vendor/core/dist/contracts/tools/manage-views.js +49 -13
  188. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +11 -2
  189. package/vendor/core/dist/contracts/tools/read-knowledge.js +23 -3
  190. package/vendor/core/dist/contracts/tools/save-memory.d.ts +3 -6
  191. package/vendor/core/dist/contracts/tools/save-memory.js +2 -6
  192. package/vendor/core/dist/contracts/tools/view-attach.d.ts +2 -1
  193. package/vendor/core/dist/contracts/tools/view-attach.js +7 -2
  194. package/vendor/core/dist/contracts/worker/protocol.d.ts +130 -159
  195. package/vendor/core/dist/contracts/worker/protocol.js +88 -76
  196. package/vendor/core/dist/device-pin-tombstones.d.ts +44 -0
  197. package/vendor/core/dist/device-pin-tombstones.js +57 -0
  198. package/vendor/core/dist/index.d.ts +5 -5
  199. package/vendor/core/dist/index.js +4 -6
  200. package/vendor/core/dist/json-template.d.ts +6 -10
  201. package/vendor/core/dist/json-template.js +5 -9
  202. package/vendor/core/dist/logger.js +17 -26
  203. package/vendor/core/dist/shell-safety.d.ts +9 -0
  204. package/vendor/core/dist/shell-safety.js +154 -0
  205. package/vendor/core/dist/suggestions.d.ts +1 -1
  206. package/vendor/core/dist/suggestions.js +1 -1
  207. package/vendor/core/dist/tool-trace-summary.d.ts +1 -1
  208. package/vendor/core/dist/tool-trace-summary.js +1 -1
  209. package/vendor/core/dist/types.d.ts +1 -3
  210. package/vendor/core/dist/utils/diagnostic-scrubber.d.ts +7 -0
  211. package/vendor/core/dist/utils/diagnostic-scrubber.js +437 -0
  212. package/vendor/core/dist/worker/auth.d.ts +44 -6
  213. package/vendor/core/dist/worker/auth.js +44 -11
  214. package/vendor/core/dist/worker/wire.d.ts +0 -10
  215. package/vendor/core/package.json +20 -11
  216. package/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  217. package/vendor/ora/index.d.ts +332 -0
  218. package/vendor/ora/index.js +424 -0
  219. package/vendor/ora/license +9 -0
  220. package/vendor/ora/package.json +53 -0
  221. package/vendor/ora/readme.md +329 -0
  222. package/vendor/cli/dist/commands/telemetry.d.ts +0 -10
  223. package/vendor/cli/dist/commands/telemetry.js +0 -66
  224. package/vendor/connector-worker/dist/daemon/device-chat.d.ts +0 -9
  225. package/vendor/connector-worker/dist/daemon/device-chat.js +0 -61
  226. package/vendor/core/dist/contracts/worker/device-chat.d.ts +0 -4
  227. package/vendor/core/dist/contracts/worker/device-chat.js +0 -26
  228. package/vendor/core/dist/sentry.d.ts +0 -26
  229. package/vendor/core/dist/sentry.js +0 -133
  230. package/vendor/core/dist/tool-policy.d.ts +0 -74
  231. package/vendor/core/dist/tool-policy.js +0 -305
  232. package/vendor/core/dist/utils/sentry-scrubber.d.ts +0 -29
  233. package/vendor/core/dist/utils/sentry-scrubber.js +0 -228
@@ -0,0 +1,270 @@
1
+ /**
2
+ * Connector actions compiled from a pinned Google Discovery document.
3
+ *
4
+ * One action per Discovery method, executed by one generic `execute`. What
5
+ * Discovery cannot say is supplied per API by a small {@link GoogleApiPolicy}:
6
+ * which scopes the connector may ask for, which methods stay hidden, which
7
+ * POSTs only read, and parameter defaults such as Gmail's `userId: 'me'`.
8
+ */
9
+ import {
10
+ type ActionContext,
11
+ type ActionDefinition,
12
+ type ActionResult,
13
+ type ConnectorFile,
14
+ downloadSizeError,
15
+ fileDownloadOutput,
16
+ type HttpClient,
17
+ } from '@lobu/connector-sdk';
18
+ import {
19
+ buildGoogleRequest,
20
+ type DiscoveryDocument,
21
+ type DiscoveryMethod,
22
+ discoveryMethods,
23
+ methodInputSchema,
24
+ methodParameters,
25
+ type UploadMedia,
26
+ } from './discovery';
27
+ import { classifyGoogleError, type GoogleError } from './errors';
28
+
29
+ export interface GoogleApiPolicy {
30
+ /**
31
+ * Scopes this connector may request, narrowest first. Discovery lists the
32
+ * scopes that can EACH authorize a method (any-of); a method requires the
33
+ * first ladder scope it accepts. A method that accepts none is not exposed.
34
+ */
35
+ scopeLadder: string[];
36
+ /**
37
+ * Rungs every connection is granted at connect time (the auth method's
38
+ * `requiredScopes`); the narrowest rung when omitted. A method one of them
39
+ * authorizes requires it rather than a narrower rung the connection lacks.
40
+ */
41
+ connectScopes?: string[];
42
+ /** Exact method ids never exposed. */
43
+ blockedMethods?: string[];
44
+ /** Method id prefixes never exposed. */
45
+ blockedPrefixes?: string[];
46
+ /** Non-GET methods that only read, e.g. `calendar.freebusy.query`. */
47
+ readMethods?: string[];
48
+ /** Non-DELETE methods that destroy data irreversibly, e.g. `calendar.calendars.clear`. */
49
+ destructiveMethods?: string[];
50
+ /** Filled when the caller omits the parameter, e.g. `{ userId: 'me' }`. */
51
+ defaults?: Record<string, unknown>;
52
+ }
53
+
54
+ /**
55
+ * Push-channel plumbing. A `watch` call registers a callback URL with Google
56
+ * and must be paired with renewal and teardown, which the connector owns
57
+ * through its webhook lifecycle — never something an agent invokes ad hoc.
58
+ */
59
+ const CHANNEL_METHOD = /\.(watch|(?:channels|users)\.stop)$/;
60
+
61
+ type SkipReason = 'blocked' | 'channel' | 'deprecated' | 'scope_outside_ladder';
62
+
63
+ interface ActionTarget {
64
+ method: DiscoveryMethod;
65
+ /** The policy defaults this method takes, applied under the caller's input. */
66
+ defaults: Record<string, unknown>;
67
+ /**
68
+ * The response is file bytes: `alt` asks for them with `alt=media` (Drive
69
+ * `files.get`, whose plain response is JSON metadata); `body` is a method
70
+ * that only ever answers with bytes (Drive `files.export`).
71
+ */
72
+ download?: 'alt' | 'body';
73
+ }
74
+
75
+ export interface CompiledGoogleApi {
76
+ doc: DiscoveryDocument;
77
+ actions: Record<string, ActionDefinition>;
78
+ targets: Map<string, ActionTarget>;
79
+ skipped: Array<{ methodId: string; reason: SkipReason }>;
80
+ }
81
+
82
+ /** `calendar.events.list` → `events_list`. */
83
+ function actionKeyFor(doc: DiscoveryDocument, methodId: string): string {
84
+ return methodId.replace(`${doc.name}.`, '').replace(/\./g, '_');
85
+ }
86
+
87
+ function connectScopes(policy: GoogleApiPolicy): string[] {
88
+ return policy.connectScopes ?? policy.scopeLadder.slice(0, 1);
89
+ }
90
+
91
+ /**
92
+ * The OAuth method's scope fields. Only `optionalScopes` can be requested as an
93
+ * upgrade later, so every rung a compiled action may require is listed there.
94
+ */
95
+ export function oauthScopes(policy: GoogleApiPolicy): { requiredScopes: string[]; optionalScopes: string[] } {
96
+ const required = connectScopes(policy);
97
+ return { requiredScopes: required, optionalScopes: policy.scopeLadder.filter((s) => !required.includes(s)) };
98
+ }
99
+
100
+ function pickScope(accepted: string[] | undefined, policy: GoogleApiPolicy): string | null | undefined {
101
+ if (!accepted || accepted.length === 0) return null; // public method: no scope needed
102
+ const granted = connectScopes(policy).find((scope) => accepted.includes(scope));
103
+ return granted ?? policy.scopeLadder.find((scope) => accepted.includes(scope));
104
+ }
105
+
106
+ export function compileGoogleActions(doc: DiscoveryDocument, policy: GoogleApiPolicy): CompiledGoogleApi {
107
+ const compiled: CompiledGoogleApi = { doc, actions: {}, targets: new Map(), skipped: [] };
108
+ const blocked = (id: string) =>
109
+ policy.blockedMethods?.includes(id) || policy.blockedPrefixes?.some((p) => id.startsWith(p));
110
+
111
+ for (const method of discoveryMethods(doc)) {
112
+ const skip = (reason: SkipReason) => compiled.skipped.push({ methodId: method.id, reason });
113
+ if (blocked(method.id)) {
114
+ skip('blocked');
115
+ continue;
116
+ }
117
+ if (CHANNEL_METHOD.test(method.id)) {
118
+ skip('channel');
119
+ continue;
120
+ }
121
+ if (method.deprecated) {
122
+ skip('deprecated');
123
+ continue;
124
+ }
125
+ const scope = pickScope(method.scopes, policy);
126
+ if (scope === undefined) {
127
+ skip('scope_outside_ladder');
128
+ continue;
129
+ }
130
+
131
+ const isRead = method.httpMethod === 'GET' || policy.readMethods?.includes(method.id) === true;
132
+ const defaults = Object.fromEntries(
133
+ methodParameters(method)
134
+ .filter(([name]) => policy.defaults && name in policy.defaults)
135
+ .map(([name]) => [name, policy.defaults?.[name]])
136
+ );
137
+ const key = actionKeyFor(doc, method.id);
138
+ const base: ActionDefinition = {
139
+ key,
140
+ name: method.id,
141
+ description: method.description,
142
+ kind: isRead ? 'read' : 'write',
143
+ ...(scope ? { requiredScopes: [scope] } : {}),
144
+ annotations: isRead
145
+ ? { readOnlyHint: true, idempotentHint: true }
146
+ : {
147
+ openWorldHint: true,
148
+ ...(method.httpMethod === 'DELETE' || policy.destructiveMethods?.includes(method.id)
149
+ ? { destructiveHint: true }
150
+ : {}),
151
+ ...(method.httpMethod === 'PUT' ? { idempotentHint: true } : {}),
152
+ },
153
+ inputSchema: methodInputSchema(doc, method, defaults),
154
+ };
155
+ compiled.actions[key] = base;
156
+ const bytesOnly = method.supportsMediaDownload && !method.response;
157
+ compiled.targets.set(key, { method, defaults, ...(bytesOnly ? { download: 'body' as const } : {}) });
158
+
159
+ if (method.supportsMediaDownload && method.response) {
160
+ const mediaKey = `${key}_media`;
161
+ compiled.actions[mediaKey] = {
162
+ ...base,
163
+ key: mediaKey,
164
+ name: `${method.id} (media)`,
165
+ description: `Download the content bytes of ${method.id} (alt=media) as an attachment.`,
166
+ kind: 'read',
167
+ annotations: { readOnlyHint: true, idempotentHint: true },
168
+ };
169
+ compiled.targets.set(mediaKey, { method, defaults, download: 'alt' });
170
+ }
171
+ }
172
+ return compiled;
173
+ }
174
+
175
+ const MAX_RATE_LIMIT_ATTEMPTS = 3;
176
+
177
+ export async function executeGoogleAction(
178
+ compiled: CompiledGoogleApi,
179
+ ctx: ActionContext,
180
+ http: HttpClient,
181
+ sleep: (ms: number) => Promise<void> = (ms) => new Promise((r) => setTimeout(r, ms))
182
+ ): Promise<ActionResult> {
183
+ const target = compiled.targets.get(ctx.actionKey);
184
+ if (!target) return { success: false, error: `Unknown action: ${ctx.actionKey}` };
185
+ const action = compiled.actions[ctx.actionKey];
186
+ const { media, ...input } = { ...target.defaults, ...ctx.input };
187
+
188
+ try {
189
+ const request = buildGoogleRequest(compiled.doc, target.method, input, {
190
+ ...(target.download === 'alt' ? { media: 'download' as const } : {}),
191
+ ...(media === undefined ? {} : { upload: uploadMedia(media, target.method) }),
192
+ });
193
+ for (let attempt = 1; ; attempt++) {
194
+ const response = await http.raw(request.url, {
195
+ method: request.method,
196
+ headers: request.headers,
197
+ body: request.body,
198
+ idempotent: action.kind === 'read',
199
+ });
200
+ if (response.ok) {
201
+ if (target.download) return await downloadResult(response, String(input.fileId ?? input.id ?? target.method.id));
202
+ return { success: true, output: jsonOutput(await response.text()) };
203
+ }
204
+
205
+ const error = classifyGoogleError(response.status, await response.text());
206
+ // The SDK throws after retrying 429/5xx. Google also reports per-user rate
207
+ // limits as 403 `rateLimitExceeded`, which only the body identifies.
208
+ if (response.status === 403 && error.code === 'rate_limited' && attempt < MAX_RATE_LIMIT_ATTEMPTS) {
209
+ await sleep(2 ** attempt * 500);
210
+ continue;
211
+ }
212
+ return actionFailure(target.method.id, error);
213
+ }
214
+ } catch (error) {
215
+ // The SDK client throws its HttpStatusError for 429/5xx once its own
216
+ // retries are spent. Read the fields, not `instanceof`: the class reaches
217
+ // this module through more than one copy of the SDK (bundles, test mocks).
218
+ const { status, bodyText } = (error ?? {}) as { status?: unknown; bodyText?: unknown };
219
+ if (typeof status === 'number') {
220
+ return actionFailure(target.method.id, classifyGoogleError(status, typeof bodyText === 'string' ? bodyText : ''));
221
+ }
222
+ return { success: false, error: error instanceof Error ? error.message : String(error) };
223
+ }
224
+ }
225
+
226
+ function actionFailure(methodId: string, error: GoogleError): ActionResult {
227
+ return {
228
+ success: false,
229
+ error: `${methodId} failed (${error.status} ${error.code}): ${error.message}`,
230
+ output: { error_code: error.code, http_status: error.status, retryable: error.retryable },
231
+ };
232
+ }
233
+
234
+ /** The server hands a resolved file input over as base64; Google wants the bytes. */
235
+ function uploadMedia(media: unknown, method: DiscoveryMethod): UploadMedia {
236
+ const file = media as Partial<ConnectorFile> | null;
237
+ if (typeof file?.base64 !== 'string' || typeof file.content_type !== 'string') {
238
+ throw new Error(`${method.id}: \`media\` must be a file input (run_sdk ctx.files or a Lobu file reference).`);
239
+ }
240
+ return { bytes: Buffer.from(file.base64, 'base64'), contentType: file.content_type };
241
+ }
242
+
243
+ /**
244
+ * File bytes as an attachment. Refused before reading when Google declares a
245
+ * size over the connector limit, and again after, for responses that do not:
246
+ * an isolate buffers the whole body, so an unbounded read is an OOM.
247
+ */
248
+ async function downloadResult(response: Response, label: string): Promise<ActionResult> {
249
+ const declared = Number(response.headers.get('content-length'));
250
+ const tooBig = downloadSizeError(Number.isFinite(declared) && declared > 0 ? declared : undefined, label);
251
+ if (tooBig) {
252
+ await response.body?.cancel();
253
+ return { success: false, error: tooBig };
254
+ }
255
+ const bytes = new Uint8Array(await response.arrayBuffer());
256
+ const receivedTooBig = downloadSizeError(bytes.length, label);
257
+ if (receivedTooBig) return { success: false, error: receivedTooBig };
258
+ const mimeType = response.headers.get('content-type')?.split(';')[0]?.trim() || 'application/octet-stream';
259
+ return { success: true, output: fileDownloadOutput({ bytes, filename: label, mimeType }) };
260
+ }
261
+
262
+ /** Google answers JSON; an empty body (DELETE) is `{}`, anything else is kept as text. */
263
+ function jsonOutput(text: string): Record<string, unknown> {
264
+ if (!text) return {};
265
+ try {
266
+ return JSON.parse(text) as Record<string, unknown>;
267
+ } catch {
268
+ return { content: text };
269
+ }
270
+ }