@paperclipai/shared 2026.1202.0 → 2026.1203.0-beta.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 (327) hide show
  1. package/dist/.paperclip-build-complete +1 -1
  2. package/dist/adapter-login.d.ts +6 -0
  3. package/dist/adapter-login.d.ts.map +1 -0
  4. package/dist/adapter-login.js +22 -0
  5. package/dist/adapter-login.js.map +1 -0
  6. package/dist/agent-configuration.d.ts +3 -0
  7. package/dist/agent-configuration.d.ts.map +1 -1
  8. package/dist/agent-runner.d.ts +4 -0
  9. package/dist/agent-runner.d.ts.map +1 -1
  10. package/dist/agent-runner.js +9 -7
  11. package/dist/agent-runner.js.map +1 -1
  12. package/dist/agent-setup-progress.d.ts +97 -0
  13. package/dist/agent-setup-progress.d.ts.map +1 -0
  14. package/dist/agent-setup-progress.js +47 -0
  15. package/dist/agent-setup-progress.js.map +1 -0
  16. package/dist/aggregator-app-catalog.d.ts +2 -0
  17. package/dist/aggregator-app-catalog.d.ts.map +1 -1
  18. package/dist/aggregator-app-catalog.js +4 -3
  19. package/dist/aggregator-app-catalog.js.map +1 -1
  20. package/dist/ai-connections.d.ts +5 -0
  21. package/dist/ai-connections.d.ts.map +1 -1
  22. package/dist/ai-connections.js +13 -0
  23. package/dist/ai-connections.js.map +1 -1
  24. package/dist/ai-provider-routing.d.ts.map +1 -1
  25. package/dist/ai-provider-routing.js +5 -1
  26. package/dist/ai-provider-routing.js.map +1 -1
  27. package/dist/antigravity-models.d.ts +63 -0
  28. package/dist/antigravity-models.d.ts.map +1 -0
  29. package/dist/antigravity-models.generated.d.ts +138 -0
  30. package/dist/antigravity-models.generated.d.ts.map +1 -0
  31. package/dist/antigravity-models.generated.js +211 -0
  32. package/dist/antigravity-models.generated.js.map +1 -0
  33. package/dist/antigravity-models.js +141 -0
  34. package/dist/antigravity-models.js.map +1 -0
  35. package/dist/antigravity-models.test.d.ts +2 -0
  36. package/dist/antigravity-models.test.d.ts.map +1 -0
  37. package/dist/antigravity-models.test.js +81 -0
  38. package/dist/antigravity-models.test.js.map +1 -0
  39. package/dist/app-definitions/anthropic.json +2 -2
  40. package/dist/app-definitions/{google.json → antigravity.json} +8 -8
  41. package/dist/app-definitions/asana.json +1 -0
  42. package/dist/app-definitions/aws.json +0 -1
  43. package/dist/app-definitions/google-workspace.json +2 -3
  44. package/dist/app-definitions/railway.json +2 -1
  45. package/dist/app-definitions/slack.json +3 -3
  46. package/dist/app-definitions/speko.json +2 -2
  47. package/dist/app-definitions.generated.js +1 -1
  48. package/dist/app-definitions.generated.js.map +1 -1
  49. package/dist/app-definitions.js +1 -1
  50. package/dist/app-definitions.js.map +1 -1
  51. package/dist/app-definitions.test.js +36 -3
  52. package/dist/app-definitions.test.js.map +1 -1
  53. package/dist/cloud-inference.d.ts +143 -0
  54. package/dist/cloud-inference.d.ts.map +1 -0
  55. package/dist/cloud-inference.js +175 -0
  56. package/dist/cloud-inference.js.map +1 -0
  57. package/dist/connector-availability.d.ts.map +1 -1
  58. package/dist/connector-availability.js +1 -3
  59. package/dist/connector-availability.js.map +1 -1
  60. package/dist/connector-availability.test.js +3 -0
  61. package/dist/connector-availability.test.js.map +1 -1
  62. package/dist/constants.d.ts +16 -3
  63. package/dist/constants.d.ts.map +1 -1
  64. package/dist/constants.js +23 -0
  65. package/dist/constants.js.map +1 -1
  66. package/dist/customer-success.d.ts +76 -1
  67. package/dist/customer-success.d.ts.map +1 -1
  68. package/dist/customer-success.js +118 -3
  69. package/dist/customer-success.js.map +1 -1
  70. package/dist/environment-support.d.ts +31 -2
  71. package/dist/environment-support.d.ts.map +1 -1
  72. package/dist/environment-support.js +40 -2
  73. package/dist/environment-support.js.map +1 -1
  74. package/dist/environment-support.test.js +51 -7
  75. package/dist/environment-support.test.js.map +1 -1
  76. package/dist/feature-catalog.d.ts.map +1 -1
  77. package/dist/feature-catalog.js +22 -8
  78. package/dist/feature-catalog.js.map +1 -1
  79. package/dist/google-sign-in-deployment.d.ts +7 -0
  80. package/dist/google-sign-in-deployment.d.ts.map +1 -0
  81. package/dist/google-sign-in-deployment.js +12 -0
  82. package/dist/google-sign-in-deployment.js.map +1 -0
  83. package/dist/google-sign-in-deployment.test.d.ts +2 -0
  84. package/dist/google-sign-in-deployment.test.d.ts.map +1 -0
  85. package/dist/google-sign-in-deployment.test.js +33 -0
  86. package/dist/google-sign-in-deployment.test.js.map +1 -0
  87. package/dist/google-workspace-connectors.d.ts +4 -0
  88. package/dist/google-workspace-connectors.d.ts.map +1 -1
  89. package/dist/google-workspace-connectors.js +33 -0
  90. package/dist/google-workspace-connectors.js.map +1 -1
  91. package/dist/google-workspace-connectors.test.js +19 -3
  92. package/dist/google-workspace-connectors.test.js.map +1 -1
  93. package/dist/google-workspace-setup-links.test.d.ts +2 -0
  94. package/dist/google-workspace-setup-links.test.d.ts.map +1 -0
  95. package/dist/google-workspace-setup-links.test.js +26 -0
  96. package/dist/google-workspace-setup-links.test.js.map +1 -0
  97. package/dist/headless/channels.d.ts +142 -0
  98. package/dist/headless/channels.d.ts.map +1 -0
  99. package/dist/headless/channels.js +103 -0
  100. package/dist/headless/channels.js.map +1 -0
  101. package/dist/headless/events.d.ts +48 -0
  102. package/dist/headless/events.d.ts.map +1 -0
  103. package/dist/headless/events.js +29 -0
  104. package/dist/headless/events.js.map +1 -0
  105. package/dist/headless/headless.test.d.ts +2 -0
  106. package/dist/headless/headless.test.d.ts.map +1 -0
  107. package/dist/headless/headless.test.js +240 -0
  108. package/dist/headless/headless.test.js.map +1 -0
  109. package/dist/headless/identity.d.ts +41 -0
  110. package/dist/headless/identity.d.ts.map +1 -0
  111. package/dist/headless/identity.js +17 -0
  112. package/dist/headless/identity.js.map +1 -0
  113. package/dist/headless/index.d.ts +8 -0
  114. package/dist/headless/index.d.ts.map +1 -0
  115. package/dist/headless/index.js +8 -0
  116. package/dist/headless/index.js.map +1 -0
  117. package/dist/headless/labels.d.ts +41 -0
  118. package/dist/headless/labels.d.ts.map +1 -0
  119. package/dist/headless/labels.js +133 -0
  120. package/dist/headless/labels.js.map +1 -0
  121. package/dist/headless/leases.d.ts +317 -0
  122. package/dist/headless/leases.d.ts.map +1 -0
  123. package/dist/headless/leases.js +194 -0
  124. package/dist/headless/leases.js.map +1 -0
  125. package/dist/headless/plans.d.ts +108 -0
  126. package/dist/headless/plans.d.ts.map +1 -0
  127. package/dist/headless/plans.js +230 -0
  128. package/dist/headless/plans.js.map +1 -0
  129. package/dist/headless/pure.d.ts +28 -0
  130. package/dist/headless/pure.d.ts.map +1 -0
  131. package/dist/headless/pure.js +111 -0
  132. package/dist/headless/pure.js.map +1 -0
  133. package/dist/headless/schemas.d.ts +59 -0
  134. package/dist/headless/schemas.d.ts.map +1 -0
  135. package/dist/headless/schemas.js +471 -0
  136. package/dist/headless/schemas.js.map +1 -0
  137. package/dist/index.d.ts +21 -8
  138. package/dist/index.d.ts.map +1 -1
  139. package/dist/index.js +18 -6
  140. package/dist/index.js.map +1 -1
  141. package/dist/issue-creation-source.d.ts +9 -0
  142. package/dist/issue-creation-source.d.ts.map +1 -1
  143. package/dist/issue-creation-source.js +14 -0
  144. package/dist/issue-creation-source.js.map +1 -1
  145. package/dist/issue-references.d.ts.map +1 -1
  146. package/dist/issue-references.js +2 -0
  147. package/dist/issue-references.js.map +1 -1
  148. package/dist/issue-references.test.js +10 -0
  149. package/dist/issue-references.test.js.map +1 -1
  150. package/dist/leading-user-command.d.ts +49 -0
  151. package/dist/leading-user-command.d.ts.map +1 -0
  152. package/dist/leading-user-command.js +35 -0
  153. package/dist/leading-user-command.js.map +1 -0
  154. package/dist/leading-user-command.test.d.ts +2 -0
  155. package/dist/leading-user-command.test.d.ts.map +1 -0
  156. package/dist/leading-user-command.test.js +52 -0
  157. package/dist/leading-user-command.test.js.map +1 -0
  158. package/dist/onboarding-telemetry.d.ts +288 -0
  159. package/dist/onboarding-telemetry.d.ts.map +1 -0
  160. package/dist/onboarding-telemetry.js +356 -0
  161. package/dist/onboarding-telemetry.js.map +1 -0
  162. package/dist/question-set.d.ts +1 -0
  163. package/dist/question-set.d.ts.map +1 -1
  164. package/dist/question-set.js +22 -0
  165. package/dist/question-set.js.map +1 -1
  166. package/dist/run-diagnostics.d.ts +16 -0
  167. package/dist/run-diagnostics.d.ts.map +1 -0
  168. package/dist/run-diagnostics.js +59 -0
  169. package/dist/run-diagnostics.js.map +1 -0
  170. package/dist/run-failure-cause.d.ts +4 -0
  171. package/dist/run-failure-cause.d.ts.map +1 -1
  172. package/dist/run-failure-cause.js +7 -0
  173. package/dist/run-failure-cause.js.map +1 -1
  174. package/dist/run-failure-cause.test.js +8 -1
  175. package/dist/run-failure-cause.test.js.map +1 -1
  176. package/dist/sdk/client.d.ts +3 -0
  177. package/dist/sdk/client.d.ts.map +1 -0
  178. package/dist/sdk/client.js +869 -0
  179. package/dist/sdk/client.js.map +1 -0
  180. package/dist/sdk/env.d.ts +33 -0
  181. package/dist/sdk/env.d.ts.map +1 -0
  182. package/dist/sdk/env.js +254 -0
  183. package/dist/sdk/env.js.map +1 -0
  184. package/dist/sdk/index.d.ts +5 -0
  185. package/dist/sdk/index.d.ts.map +1 -0
  186. package/dist/sdk/index.js +5 -0
  187. package/dist/sdk/index.js.map +1 -0
  188. package/dist/sdk/lite.d.ts +6 -0
  189. package/dist/sdk/lite.d.ts.map +1 -0
  190. package/dist/sdk/lite.js +5 -0
  191. package/dist/sdk/lite.js.map +1 -0
  192. package/dist/sdk/memory-backend.d.ts +54 -0
  193. package/dist/sdk/memory-backend.d.ts.map +1 -0
  194. package/dist/sdk/memory-backend.js +2020 -0
  195. package/dist/sdk/memory-backend.js.map +1 -0
  196. package/dist/sdk/sdk.test.d.ts +2 -0
  197. package/dist/sdk/sdk.test.d.ts.map +1 -0
  198. package/dist/sdk/sdk.test.js +626 -0
  199. package/dist/sdk/sdk.test.js.map +1 -0
  200. package/dist/sdk/sse.d.ts +3 -0
  201. package/dist/sdk/sse.d.ts.map +1 -0
  202. package/dist/sdk/sse.js +80 -0
  203. package/dist/sdk/sse.js.map +1 -0
  204. package/dist/sdk/types.d.ts +210 -0
  205. package/dist/sdk/types.d.ts.map +1 -0
  206. package/dist/sdk/types.js +19 -0
  207. package/dist/sdk/types.js.map +1 -0
  208. package/dist/telemetry/connector-events.test.js +23 -1
  209. package/dist/telemetry/connector-events.test.js.map +1 -1
  210. package/dist/telemetry/events.d.ts +13 -0
  211. package/dist/telemetry/events.d.ts.map +1 -1
  212. package/dist/telemetry/events.js +14 -0
  213. package/dist/telemetry/events.js.map +1 -1
  214. package/dist/telemetry/generated/paperclip-telemetry.d.ts +57 -3
  215. package/dist/telemetry/generated/paperclip-telemetry.d.ts.map +1 -1
  216. package/dist/telemetry/generated/paperclip-telemetry.js +39 -0
  217. package/dist/telemetry/generated/paperclip-telemetry.js.map +1 -1
  218. package/dist/telemetry/index.d.ts +1 -1
  219. package/dist/telemetry/index.d.ts.map +1 -1
  220. package/dist/telemetry/index.js +1 -1
  221. package/dist/telemetry/index.js.map +1 -1
  222. package/dist/telemetry/retention.d.ts.map +1 -1
  223. package/dist/telemetry/retention.js +2 -0
  224. package/dist/telemetry/retention.js.map +1 -1
  225. package/dist/tool-catalog-availability.d.ts +10 -0
  226. package/dist/tool-catalog-availability.d.ts.map +1 -0
  227. package/dist/tool-catalog-availability.js +10 -0
  228. package/dist/tool-catalog-availability.js.map +1 -0
  229. package/dist/tool-catalog-availability.test.d.ts +2 -0
  230. package/dist/tool-catalog-availability.test.d.ts.map +1 -0
  231. package/dist/tool-catalog-availability.test.js +17 -0
  232. package/dist/tool-catalog-availability.test.js.map +1 -0
  233. package/dist/types/attention.d.ts +28 -1
  234. package/dist/types/attention.d.ts.map +1 -1
  235. package/dist/types/attention.js.map +1 -1
  236. package/dist/types/document-annotation.d.ts +13 -1
  237. package/dist/types/document-annotation.d.ts.map +1 -1
  238. package/dist/types/execution-projection.d.ts +1 -1
  239. package/dist/types/execution-projection.d.ts.map +1 -1
  240. package/dist/types/heartbeat.d.ts +19 -1
  241. package/dist/types/heartbeat.d.ts.map +1 -1
  242. package/dist/types/index.d.ts +5 -5
  243. package/dist/types/index.d.ts.map +1 -1
  244. package/dist/types/index.js.map +1 -1
  245. package/dist/types/instance.d.ts +13 -1
  246. package/dist/types/instance.d.ts.map +1 -1
  247. package/dist/types/instance.js.map +1 -1
  248. package/dist/types/issue.d.ts +31 -2
  249. package/dist/types/issue.d.ts.map +1 -1
  250. package/dist/types/plugin.d.ts +5 -0
  251. package/dist/types/plugin.d.ts.map +1 -1
  252. package/dist/types/responsibility.d.ts +4 -2
  253. package/dist/types/responsibility.d.ts.map +1 -1
  254. package/dist/types/tool-access.d.ts +21 -2
  255. package/dist/types/tool-access.d.ts.map +1 -1
  256. package/dist/types/tool-access.js.map +1 -1
  257. package/dist/upstream-failure-codes.d.ts +30 -0
  258. package/dist/upstream-failure-codes.d.ts.map +1 -0
  259. package/dist/upstream-failure-codes.js +82 -0
  260. package/dist/upstream-failure-codes.js.map +1 -0
  261. package/dist/upstream-failure-codes.test.d.ts +2 -0
  262. package/dist/upstream-failure-codes.test.d.ts.map +1 -0
  263. package/dist/upstream-failure-codes.test.js +55 -0
  264. package/dist/upstream-failure-codes.test.js.map +1 -0
  265. package/dist/validators/access.d.ts +12 -0
  266. package/dist/validators/access.d.ts.map +1 -1
  267. package/dist/validators/access.js +6 -0
  268. package/dist/validators/access.js.map +1 -1
  269. package/dist/validators/access.test.js +17 -0
  270. package/dist/validators/access.test.js.map +1 -1
  271. package/dist/validators/agent.d.ts +4 -0
  272. package/dist/validators/agent.d.ts.map +1 -1
  273. package/dist/validators/agent.js +2 -1
  274. package/dist/validators/agent.js.map +1 -1
  275. package/dist/validators/agent.test.js +17 -1
  276. package/dist/validators/agent.test.js.map +1 -1
  277. package/dist/validators/app-definition.d.ts +4 -0
  278. package/dist/validators/app-definition.d.ts.map +1 -1
  279. package/dist/validators/chat-channels.test.js +11 -1
  280. package/dist/validators/chat-channels.test.js.map +1 -1
  281. package/dist/validators/company-portability.d.ts +5 -0
  282. package/dist/validators/company-portability.d.ts.map +1 -1
  283. package/dist/validators/document-annotation.d.ts +6 -0
  284. package/dist/validators/document-annotation.d.ts.map +1 -1
  285. package/dist/validators/document-annotation.js +2 -1
  286. package/dist/validators/document-annotation.js.map +1 -1
  287. package/dist/validators/index.d.ts +4 -4
  288. package/dist/validators/index.d.ts.map +1 -1
  289. package/dist/validators/index.js +4 -4
  290. package/dist/validators/index.js.map +1 -1
  291. package/dist/validators/instance.d.ts +6 -0
  292. package/dist/validators/instance.d.ts.map +1 -1
  293. package/dist/validators/instance.js +8 -2
  294. package/dist/validators/instance.js.map +1 -1
  295. package/dist/validators/instance.test.js +4 -2
  296. package/dist/validators/instance.test.js.map +1 -1
  297. package/dist/validators/issue.d.ts +211 -0
  298. package/dist/validators/issue.d.ts.map +1 -1
  299. package/dist/validators/issue.js +81 -4
  300. package/dist/validators/issue.js.map +1 -1
  301. package/dist/validators/issue.test.js +114 -1
  302. package/dist/validators/issue.test.js.map +1 -1
  303. package/dist/validators/plugin.d.ts +15 -0
  304. package/dist/validators/plugin.d.ts.map +1 -1
  305. package/dist/validators/plugin.js +28 -0
  306. package/dist/validators/plugin.js.map +1 -1
  307. package/dist/validators/plugin.test.js +31 -0
  308. package/dist/validators/plugin.test.js.map +1 -1
  309. package/dist/validators/secret.d.ts +4 -0
  310. package/dist/validators/secret.d.ts.map +1 -1
  311. package/dist/validators/secret.js +27 -0
  312. package/dist/validators/secret.js.map +1 -1
  313. package/dist/validators/tool-access.d.ts +6 -0
  314. package/dist/validators/tool-access.d.ts.map +1 -1
  315. package/dist/validators/tool-access.js +5 -7
  316. package/dist/validators/tool-access.js.map +1 -1
  317. package/dist/validators/tool-access.test.js +4 -1
  318. package/dist/validators/tool-access.test.js.map +1 -1
  319. package/package.json +9 -1
  320. package/dist/connection-intent-guidance.test.d.ts +0 -2
  321. package/dist/connection-intent-guidance.test.d.ts.map +0 -1
  322. package/dist/connection-intent-guidance.test.js +0 -24
  323. package/dist/connection-intent-guidance.test.js.map +0 -1
  324. package/dist/telemetry/readme-contract.test.d.ts +0 -2
  325. package/dist/telemetry/readme-contract.test.d.ts.map +0 -1
  326. package/dist/telemetry/readme-contract.test.js +0 -17
  327. package/dist/telemetry/readme-contract.test.js.map +0 -1
@@ -0,0 +1,869 @@
1
+ import { parseDurationMs, parseSchemaRef } from "../headless/pure.js";
2
+ import { extractProfileApiKey, readNodeEnv, readStoredBoardAuthToken, readStoredContext, readStoredProfile, readTrustedContext, sameOriginUrl, } from "./env.js";
3
+ import { parseSseEventStream } from "./sse.js";
4
+ import { PaperclipError, } from "./types.js";
5
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
6
+ const ISSUE_ID_RE = /^[A-Z][A-Z0-9]*-\d+$/i;
7
+ function isIssueReference(target) {
8
+ const trimmed = target.trim();
9
+ return UUID_RE.test(trimmed) || ISSUE_ID_RE.test(trimmed);
10
+ }
11
+ function appendMultiParam(params, key, value) {
12
+ if (value === undefined)
13
+ return;
14
+ const values = Array.isArray(value) ? value : [value];
15
+ for (const item of values) {
16
+ if (item !== undefined && item !== "") {
17
+ params.append(key, item);
18
+ }
19
+ }
20
+ }
21
+ function buildBlocksFromSchemaInput(blocks, schema) {
22
+ const result = blocks ? [...blocks] : [];
23
+ if (schema) {
24
+ const parsed = parseSchemaRef(schema.ref);
25
+ result.push({
26
+ schema: parsed.name,
27
+ version: parsed.version,
28
+ data: schema.data,
29
+ });
30
+ }
31
+ return result.length > 0 ? result : undefined;
32
+ }
33
+ async function parseErrorResponse(res) {
34
+ const text = await res.text().catch(() => "");
35
+ let parsed = null;
36
+ if (text) {
37
+ try {
38
+ const json = JSON.parse(text);
39
+ if (typeof json === "object" && json !== null) {
40
+ parsed = json;
41
+ }
42
+ }
43
+ catch {
44
+ // non-JSON response
45
+ }
46
+ }
47
+ const nestedDetails = parsed?.details && typeof parsed.details === "object" && !Array.isArray(parsed.details)
48
+ ? parsed.details
49
+ : null;
50
+ const code = (typeof parsed?.code === "string" && parsed.code) ||
51
+ (typeof nestedDetails?.code === "string" && nestedDetails.code) ||
52
+ (typeof parsed?.error === "string" && parsed.error) ||
53
+ `http_${res.status}`;
54
+ const message = (typeof parsed?.message === "string" && parsed.message) ||
55
+ (typeof parsed?.error === "string" && parsed.error) ||
56
+ text ||
57
+ `Request failed with status ${res.status}`;
58
+ const rawNext = Array.isArray(parsed?.next)
59
+ ? parsed.next
60
+ : Array.isArray(nestedDetails?.next)
61
+ ? nestedDetails.next
62
+ : undefined;
63
+ const next = rawNext?.filter((x) => typeof x === "string");
64
+ return new PaperclipError({
65
+ status: res.status,
66
+ code,
67
+ message,
68
+ next,
69
+ details: parsed ?? undefined,
70
+ });
71
+ }
72
+ function createHandle(state) {
73
+ const resolveAuthHeaders = () => {
74
+ const headers = {};
75
+ const nodeEnv = readNodeEnv();
76
+ const trustedCtx = readTrustedContext(state.homeDir);
77
+ const storedBoardAuthToken = state.apiBaseFromRepoLocal
78
+ ? undefined
79
+ : readStoredBoardAuthToken(state.apiBase, state.homeDir);
80
+ const ambientKey = nodeEnv.apiKey ?? trustedCtx?.apiKey ?? trustedCtx?.token;
81
+ const rawProfileCfg = state.profile
82
+ ? readStoredProfile(state.profile, state.homeDir)
83
+ : null;
84
+ const rawProfileKey = extractProfileApiKey(rawProfileCfg);
85
+ const originlessProfileKey = rawProfileKey && !rawProfileCfg?.apiBase ? rawProfileKey : undefined;
86
+ if (state.apiBaseFromRepoLocal &&
87
+ !state.explicitApiKey &&
88
+ (ambientKey || originlessProfileKey)) {
89
+ throw new Error(`Refusing to send ambient credentials to untrusted repo-local apiBase '${state.apiBase}'. Pass apiBase or apiKey explicitly if you trust this endpoint.`);
90
+ }
91
+ let token = state.explicitApiKey ??
92
+ (state.apiBaseFromRepoLocal ? undefined : (ambientKey ?? storedBoardAuthToken));
93
+ let sendProfileHeader = false;
94
+ if (state.profile) {
95
+ const profileOriginOk = rawProfileCfg?.apiBase
96
+ ? sameOriginUrl(rawProfileCfg.apiBase, state.apiBase)
97
+ : !state.apiBaseFromRepoLocal;
98
+ const trustedProfileKey = profileOriginOk ? rawProfileKey : undefined;
99
+ if (trustedProfileKey) {
100
+ token = state.explicitApiKey ?? trustedProfileKey;
101
+ }
102
+ else {
103
+ sendProfileHeader = true;
104
+ }
105
+ }
106
+ if (token) {
107
+ headers.authorization = `Bearer ${token}`;
108
+ }
109
+ if (sendProfileHeader && state.profile) {
110
+ headers["x-paperclip-actor-profile"] = state.profile;
111
+ }
112
+ if (state.scopes) {
113
+ headers["x-paperclip-actor-scopes"] = JSON.stringify(state.scopes);
114
+ }
115
+ return headers;
116
+ };
117
+ const requestRaw = async (path, init = {}) => {
118
+ const url = `${state.apiBase}${path.startsWith("/") ? path : `/${path}`}`;
119
+ const headers = new Headers(init.headers);
120
+ const authHeaders = resolveAuthHeaders();
121
+ for (const [k, v] of Object.entries(authHeaders)) {
122
+ if (!headers.has(k)) {
123
+ headers.set(k, v);
124
+ }
125
+ }
126
+ if (init.body !== undefined && !headers.has("content-type")) {
127
+ headers.set("content-type", "application/json");
128
+ }
129
+ const res = await state.fetchImpl(url, {
130
+ ...init,
131
+ headers,
132
+ });
133
+ if (!res.ok) {
134
+ throw await parseErrorResponse(res);
135
+ }
136
+ return res;
137
+ };
138
+ const requestJson = async (path, init = {}) => {
139
+ const res = await requestRaw(path, init);
140
+ return (await res.json());
141
+ };
142
+ const resolveCompanyId = async () => {
143
+ if (state.cachedCompanyId)
144
+ return state.cachedCompanyId;
145
+ if (state.companyPromise)
146
+ return state.companyPromise;
147
+ state.companyPromise = (async () => {
148
+ const nodeEnv = readNodeEnv();
149
+ const storedCtx = readStoredContext(state.homeDir);
150
+ const companyRef = state.companyArg ??
151
+ nodeEnv.company ??
152
+ storedCtx?.companyId ??
153
+ storedCtx?.defaultCompanyId;
154
+ if (companyRef && UUID_RE.test(companyRef.trim())) {
155
+ state.cachedCompanyId = companyRef.trim().toLowerCase();
156
+ return state.cachedCompanyId;
157
+ }
158
+ const qs = companyRef
159
+ ? `?company=${encodeURIComponent(companyRef.trim())}`
160
+ : "";
161
+ const resolved = await requestJson(`/api/companies/resolve-default${qs}`, { method: "GET" });
162
+ state.cachedCompanyId = resolved.id;
163
+ return resolved.id;
164
+ })();
165
+ try {
166
+ return await state.companyPromise;
167
+ }
168
+ catch (err) {
169
+ state.companyPromise = null;
170
+ throw err;
171
+ }
172
+ };
173
+ const buildClaimedTask = (initial) => {
174
+ let currentClaimToken = initial.claimToken;
175
+ let currentClaimEpoch = initial.claimEpoch;
176
+ let currentLeaseExpiresAt = initial.leaseExpiresAt;
177
+ let currentTask = initial.issue;
178
+ const claimed = {
179
+ get task() {
180
+ return currentTask;
181
+ },
182
+ get claimToken() {
183
+ return currentClaimToken;
184
+ },
185
+ get claimEpoch() {
186
+ return currentClaimEpoch;
187
+ },
188
+ get leaseExpiresAt() {
189
+ return currentLeaseExpiresAt;
190
+ },
191
+ cursor: initial.cursor,
192
+ async renew(lease) {
193
+ const leaseSec = lease !== undefined
194
+ ? Math.max(1, Math.round(parseDurationMs(lease) / 1000))
195
+ : undefined;
196
+ const view = await requestJson(`/api/issues/${encodeURIComponent(currentTask.id)}/claim/renew`, {
197
+ method: "POST",
198
+ headers: { "x-paperclip-claim-token": currentClaimToken },
199
+ body: JSON.stringify({
200
+ claimToken: currentClaimToken,
201
+ claimEpoch: currentClaimEpoch,
202
+ ...(leaseSec !== undefined ? { leaseSec } : {}),
203
+ }),
204
+ });
205
+ currentTask = view.issue;
206
+ currentClaimToken = view.claimToken;
207
+ currentClaimEpoch = view.claimEpoch;
208
+ currentLeaseExpiresAt = view.leaseExpiresAt;
209
+ return view;
210
+ },
211
+ async release(commentOrOpts) {
212
+ const payload = typeof commentOrOpts === "string"
213
+ ? { comment: commentOrOpts }
214
+ : { ...(commentOrOpts ?? {}) };
215
+ return requestJson(`/api/issues/${encodeURIComponent(currentTask.id)}/claim/release`, {
216
+ method: "POST",
217
+ headers: { "x-paperclip-claim-token": currentClaimToken },
218
+ body: JSON.stringify({
219
+ ...payload,
220
+ claimToken: currentClaimToken,
221
+ claimEpoch: currentClaimEpoch,
222
+ }),
223
+ });
224
+ },
225
+ async handoff(opts) {
226
+ const updated = await requestJson(`/api/issues/${encodeURIComponent(currentTask.id)}/handoff`, {
227
+ method: "POST",
228
+ headers: { "x-paperclip-claim-token": currentClaimToken },
229
+ body: JSON.stringify({
230
+ ...opts,
231
+ claimToken: currentClaimToken,
232
+ claimEpoch: currentClaimEpoch,
233
+ }),
234
+ });
235
+ currentTask = updated;
236
+ return updated;
237
+ },
238
+ async complete(opts) {
239
+ const payload = typeof opts === "string"
240
+ ? { message: opts }
241
+ : { ...(opts ?? {}) };
242
+ const updated = await requestJson(`/api/issues/${encodeURIComponent(currentTask.id)}/claim/complete`, {
243
+ method: "POST",
244
+ headers: { "x-paperclip-claim-token": currentClaimToken },
245
+ body: JSON.stringify({
246
+ ...payload,
247
+ claimToken: currentClaimToken,
248
+ claimEpoch: currentClaimEpoch,
249
+ }),
250
+ });
251
+ currentTask = updated;
252
+ return updated;
253
+ },
254
+ async fail(reason, opts) {
255
+ const updated = await requestJson(`/api/issues/${encodeURIComponent(currentTask.id)}/claim/fail`, {
256
+ method: "POST",
257
+ headers: { "x-paperclip-claim-token": currentClaimToken },
258
+ body: JSON.stringify({
259
+ claimToken: currentClaimToken,
260
+ claimEpoch: currentClaimEpoch,
261
+ reason,
262
+ ...(opts?.requeue !== undefined ? { requeue: opts.requeue } : {}),
263
+ }),
264
+ });
265
+ currentTask = updated;
266
+ return updated;
267
+ },
268
+ async ask(question, opts) {
269
+ return requestJson(`/api/issues/${encodeURIComponent(currentTask.id)}/ask`, {
270
+ method: "POST",
271
+ headers: { "x-paperclip-claim-token": currentClaimToken },
272
+ body: JSON.stringify({
273
+ ...(opts ?? {}),
274
+ question,
275
+ claimToken: currentClaimToken,
276
+ }),
277
+ });
278
+ },
279
+ async confirm(prompt, opts) {
280
+ return requestJson(`/api/issues/${encodeURIComponent(currentTask.id)}/confirm`, {
281
+ method: "POST",
282
+ headers: { "x-paperclip-claim-token": currentClaimToken },
283
+ body: JSON.stringify({
284
+ ...(opts ?? {}),
285
+ prompt,
286
+ claimToken: currentClaimToken,
287
+ }),
288
+ });
289
+ },
290
+ };
291
+ return claimed;
292
+ };
293
+ return {
294
+ as(profile, opts) {
295
+ return createHandle({
296
+ ...state,
297
+ profile,
298
+ scopes: opts?.scopes,
299
+ cachedCompanyId: state.cachedCompanyId,
300
+ companyPromise: state.companyPromise,
301
+ });
302
+ },
303
+ async whoami() {
304
+ const companyId = await resolveCompanyId();
305
+ return requestJson(`/api/whoami?companyId=${encodeURIComponent(companyId)}`, { method: "GET" });
306
+ },
307
+ channels: {
308
+ async list(prefix) {
309
+ const companyId = await resolveCompanyId();
310
+ const params = new URLSearchParams();
311
+ if (prefix)
312
+ params.set("prefix", prefix);
313
+ const qs = params.toString();
314
+ const res = await requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels${qs ? `?${qs}` : ""}`, { method: "GET" });
315
+ return Array.isArray(res) ? res : res.items;
316
+ },
317
+ async create(path, opts) {
318
+ const companyId = await resolveCompanyId();
319
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels`, {
320
+ method: "POST",
321
+ body: JSON.stringify({
322
+ ...(opts ?? {}),
323
+ path,
324
+ }),
325
+ });
326
+ },
327
+ async ensure(path, opts) {
328
+ return this.create(path, opts);
329
+ },
330
+ async update(path, opts) {
331
+ const companyId = await resolveCompanyId();
332
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels`, {
333
+ method: "PATCH",
334
+ body: JSON.stringify({
335
+ ...(opts ?? {}),
336
+ channel: path,
337
+ }),
338
+ });
339
+ },
340
+ members: {
341
+ async list(channel) {
342
+ const companyId = await resolveCompanyId();
343
+ const res = await requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels/members?channel=${encodeURIComponent(channel)}`, { method: "GET" });
344
+ return res.items;
345
+ },
346
+ async add(channel, member) {
347
+ const companyId = await resolveCompanyId();
348
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels/members`, {
349
+ method: "POST",
350
+ body: JSON.stringify({
351
+ ...member,
352
+ channel,
353
+ }),
354
+ });
355
+ },
356
+ async remove(channel, member) {
357
+ const companyId = await resolveCompanyId();
358
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels/members`, {
359
+ method: "DELETE",
360
+ body: JSON.stringify({
361
+ ...member,
362
+ channel,
363
+ }),
364
+ });
365
+ },
366
+ },
367
+ },
368
+ async post(channel, bodyOrOpts, maybeOpts) {
369
+ const companyId = await resolveCompanyId();
370
+ const input = typeof bodyOrOpts === "string"
371
+ ? { ...(maybeOpts ?? {}), text: bodyOrOpts }
372
+ : bodyOrOpts;
373
+ const blocks = buildBlocksFromSchemaInput(input.blocks, input.schema);
374
+ const { schema: _ignoredSchema, ...rest } = input;
375
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels/posts`, {
376
+ method: "POST",
377
+ body: JSON.stringify({
378
+ ...rest,
379
+ channel,
380
+ ...(blocks ? { blocks } : {}),
381
+ }),
382
+ });
383
+ },
384
+ async reply(id, bodyOrOpts, maybeOpts) {
385
+ const input = typeof bodyOrOpts === "string"
386
+ ? { ...(maybeOpts ?? {}), body: bodyOrOpts }
387
+ : bodyOrOpts;
388
+ const blocks = buildBlocksFromSchemaInput(input.blocks, input.schema);
389
+ const bodyText = input.body ?? input.text ?? "";
390
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/comments`, {
391
+ method: "POST",
392
+ body: JSON.stringify({
393
+ body: bodyText,
394
+ ...(input.labels ? { kvLabels: input.labels, labels: input.labels } : {}),
395
+ ...(blocks ? { blocks } : {}),
396
+ ...(input.after ? { afterCommentId: input.after, after: input.after } : {}),
397
+ ...(input.internal !== undefined ? { internal: input.internal } : {}),
398
+ ...(input.idempotencyKey
399
+ ? {
400
+ clientRequestId: input.idempotencyKey,
401
+ idempotencyKey: input.idempotencyKey,
402
+ }
403
+ : {}),
404
+ }),
405
+ });
406
+ },
407
+ async read(channelOrId, filters) {
408
+ if (isIssueReference(channelOrId)) {
409
+ const res = await requestJson(`/api/issues/${encodeURIComponent(channelOrId)}?view=thread`, { method: "GET" });
410
+ if ("kind" in res && res.kind === "thread") {
411
+ return res;
412
+ }
413
+ const issue = res;
414
+ const commentsRes = await requestJson(`/api/issues/${encodeURIComponent(channelOrId)}/comments`, {
415
+ method: "GET",
416
+ });
417
+ const comments = Array.isArray(commentsRes)
418
+ ? commentsRes
419
+ : commentsRes.items;
420
+ return {
421
+ kind: "thread",
422
+ issue,
423
+ comments,
424
+ interactions: [],
425
+ cursor: issue.cursor,
426
+ };
427
+ }
428
+ const companyId = await resolveCompanyId();
429
+ const params = new URLSearchParams();
430
+ params.set("channel", channelOrId);
431
+ appendMultiParam(params, "label", filters?.label);
432
+ appendMultiParam(params, "status", filters?.status);
433
+ if (filters?.after)
434
+ params.set("after", filters.after);
435
+ if (filters?.limit !== undefined) {
436
+ params.set("limit", String(filters.limit));
437
+ }
438
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels/threads?${params.toString()}`, { method: "GET" });
439
+ },
440
+ async label(id, mutation) {
441
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/kv-labels`, {
442
+ method: "PATCH",
443
+ body: JSON.stringify(mutation),
444
+ });
445
+ },
446
+ tasks: {
447
+ async create(channelOrId, input, maybeOpts) {
448
+ if (isIssueReference(channelOrId)) {
449
+ const opts = typeof input === "string"
450
+ ? { ...(maybeOpts ?? {}), text: input }
451
+ : { ...(input ?? {}) };
452
+ return requestJson(`/api/issues/${encodeURIComponent(channelOrId)}`, {
453
+ method: "PATCH",
454
+ body: JSON.stringify({
455
+ status: "todo",
456
+ ...(opts.priority ? { priority: opts.priority } : {}),
457
+ ...(opts.labels ? { kvLabels: opts.labels, labels: opts.labels } : {}),
458
+ ...(opts.projectId !== undefined ? { projectId: opts.projectId } : {}),
459
+ ...(opts.board ? { scope: "board" } : {}),
460
+ ...(opts.assigneeAgentId !== undefined
461
+ ? { assigneeAgentId: opts.assigneeAgentId }
462
+ : {}),
463
+ ...(opts.for ? { for: opts.for } : {}),
464
+ ...(opts.resultSchema ? { resultSchema: opts.resultSchema } : {}),
465
+ }),
466
+ });
467
+ }
468
+ const companyId = await resolveCompanyId();
469
+ const opts = typeof input === "string"
470
+ ? { ...(maybeOpts ?? {}), text: input }
471
+ : { ...(input ?? {}) };
472
+ if (opts.needs && opts.needs.length > 0) {
473
+ const batch = await requestJson(`/api/companies/${encodeURIComponent(companyId)}/issues/batch`, {
474
+ method: "POST",
475
+ body: JSON.stringify({
476
+ channel: channelOrId,
477
+ ...(opts.projectId !== undefined ? { projectId: opts.projectId } : {}),
478
+ ...(opts.board !== undefined ? { board: opts.board } : {}),
479
+ ...(opts.idempotencyKey ? { idempotencyKey: opts.idempotencyKey } : {}),
480
+ tasks: [
481
+ {
482
+ ...(opts.title !== undefined ? { title: opts.title } : {}),
483
+ ...(opts.text !== undefined ? { text: opts.text } : {}),
484
+ ...(opts.description !== undefined ? { description: opts.description } : {}),
485
+ ...(opts.priority !== undefined ? { priority: opts.priority } : {}),
486
+ ...(opts.labels !== undefined ? { labels: opts.labels } : {}),
487
+ ...(opts.tagLabels !== undefined ? { tagLabels: opts.tagLabels } : {}),
488
+ ...(opts.for !== undefined ? { for: opts.for } : {}),
489
+ ...(opts.assigneeAgentId !== undefined
490
+ ? { assigneeAgentId: opts.assigneeAgentId }
491
+ : {}),
492
+ ...(opts.resultSchema !== undefined ? { resultSchema: opts.resultSchema } : {}),
493
+ ...(opts.blocks !== undefined ? { blocks: opts.blocks } : {}),
494
+ ...(opts.board !== undefined ? { board: opts.board } : {}),
495
+ needs: opts.needs,
496
+ },
497
+ ],
498
+ }),
499
+ });
500
+ return batch.tasks[0].issue;
501
+ }
502
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/channels/posts`, {
503
+ method: "POST",
504
+ body: JSON.stringify({
505
+ ...opts,
506
+ channel: channelOrId,
507
+ status: "todo",
508
+ }),
509
+ });
510
+ },
511
+ async createPlan(channel, tasks, opts) {
512
+ const companyId = await resolveCompanyId();
513
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/issues/batch`, {
514
+ method: "POST",
515
+ body: JSON.stringify({
516
+ channel,
517
+ ...(opts?.projectId !== undefined
518
+ ? { projectId: opts.projectId }
519
+ : {}),
520
+ ...(opts?.board !== undefined ? { board: opts.board } : {}),
521
+ ...(opts?.idempotencyKey
522
+ ? { idempotencyKey: opts.idempotencyKey }
523
+ : {}),
524
+ tasks,
525
+ }),
526
+ });
527
+ },
528
+ async list(filters) {
529
+ const companyId = await resolveCompanyId();
530
+ const params = new URLSearchParams();
531
+ params.set("headless", "1");
532
+ if (filters?.channel)
533
+ params.set("channel", filters.channel);
534
+ appendMultiParam(params, "status", filters?.status ?? filters?.state);
535
+ appendMultiParam(params, "label", filters?.label);
536
+ if (filters?.assignee)
537
+ params.set("assignee", filters.assignee);
538
+ if (filters?.project)
539
+ params.set("project", filters.project);
540
+ if (filters?.ready !== undefined) {
541
+ params.set("ready", String(filters.ready));
542
+ }
543
+ if (filters?.after)
544
+ params.set("after", filters.after);
545
+ if (filters?.limit !== undefined) {
546
+ params.set("limit", String(filters.limit));
547
+ }
548
+ const qs = params.toString();
549
+ const res = await requestJson(`/api/companies/${encodeURIComponent(companyId)}/issues${qs ? `?${qs}` : ""}`, { method: "GET" });
550
+ if (Array.isArray(res)) {
551
+ return {
552
+ items: res,
553
+ cursor: res[res.length - 1]?.cursor ?? "evt_0",
554
+ };
555
+ }
556
+ return {
557
+ items: res.items,
558
+ cursor: res.cursor ?? res.items[res.items.length - 1]?.cursor ?? "evt_0",
559
+ };
560
+ },
561
+ async claim(idOrReadyQuery, maybeOpts) {
562
+ const companyId = await resolveCompanyId();
563
+ const isId = typeof idOrReadyQuery === "string";
564
+ const opts = isId
565
+ ? { ...(maybeOpts ?? {}) }
566
+ : { ...(idOrReadyQuery ?? {}), ready: true };
567
+ const labels = [
568
+ ...(opts.labels ?? []),
569
+ ...(opts.label
570
+ ? Array.isArray(opts.label)
571
+ ? opts.label
572
+ : [opts.label]
573
+ : []),
574
+ ];
575
+ const leaseSec = opts.leaseSec ??
576
+ (opts.lease !== undefined
577
+ ? Math.max(1, Math.round(parseDurationMs(opts.lease) / 1000))
578
+ : undefined);
579
+ const waitSec = opts.waitSec ??
580
+ (opts.wait !== undefined
581
+ ? Math.max(0, parseDurationMs(opts.wait) / 1000)
582
+ : undefined);
583
+ const statuses = [
584
+ ...(opts.statuses ?? []),
585
+ ...(opts.status
586
+ ? Array.isArray(opts.status)
587
+ ? opts.status
588
+ : [opts.status]
589
+ : []),
590
+ ];
591
+ const body = {
592
+ ...(isId ? { issueId: idOrReadyQuery } : { ready: opts.ready ?? true }),
593
+ ...(opts.channel ? { channel: opts.channel } : {}),
594
+ ...(opts.projectId ? { projectId: opts.projectId } : {}),
595
+ ...(labels.length > 0 ? { labels } : {}),
596
+ ...(statuses.length > 0 ? { statuses } : {}),
597
+ ...(opts.assignee ? { assignee: opts.assignee } : {}),
598
+ ...(leaseSec !== undefined ? { leaseSec } : {}),
599
+ ...(waitSec !== undefined ? { waitSec } : {}),
600
+ ...(opts.includeUntrusted !== undefined
601
+ ? { includeUntrusted: opts.includeUntrusted }
602
+ : {}),
603
+ };
604
+ const res = await requestRaw(`/api/companies/${encodeURIComponent(companyId)}/tasks/claim`, {
605
+ method: "POST",
606
+ body: JSON.stringify(body),
607
+ signal: opts.signal,
608
+ });
609
+ if (res.status === 204) {
610
+ return null;
611
+ }
612
+ const claimView = (await res.json());
613
+ return buildClaimedTask(claimView);
614
+ },
615
+ async assign(id, assigneeOrOpts, maybeOpts) {
616
+ const payload = typeof assigneeOrOpts === "string" || assigneeOrOpts === null
617
+ ? { ...(maybeOpts ?? {}), assignee: assigneeOrOpts }
618
+ : assigneeOrOpts;
619
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/assign`, {
620
+ method: "POST",
621
+ body: JSON.stringify(payload),
622
+ });
623
+ },
624
+ async handoff(id, opts) {
625
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/handoff`, {
626
+ method: "POST",
627
+ headers: opts.claimToken
628
+ ? { "x-paperclip-claim-token": opts.claimToken }
629
+ : undefined,
630
+ body: JSON.stringify(opts),
631
+ });
632
+ },
633
+ async cancel(id, reasonOrOpts) {
634
+ const payload = typeof reasonOrOpts === "string"
635
+ ? { reason: reasonOrOpts }
636
+ : { ...(reasonOrOpts ?? {}) };
637
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/cancel`, {
638
+ method: "POST",
639
+ headers: payload.claimToken
640
+ ? { "x-paperclip-claim-token": payload.claimToken }
641
+ : undefined,
642
+ body: JSON.stringify(payload),
643
+ });
644
+ },
645
+ async reopen(id, opts) {
646
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/reopen`, {
647
+ method: "POST",
648
+ body: JSON.stringify(opts ?? {}),
649
+ });
650
+ },
651
+ async dependencies(id) {
652
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/dependencies`, { method: "GET" });
653
+ },
654
+ async updateDependencies(id, mutation) {
655
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/dependencies`, {
656
+ method: "POST",
657
+ headers: mutation.claimToken
658
+ ? { "x-paperclip-claim-token": mutation.claimToken }
659
+ : undefined,
660
+ body: JSON.stringify(mutation),
661
+ });
662
+ },
663
+ },
664
+ async ask(id, question, opts) {
665
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/ask`, {
666
+ method: "POST",
667
+ body: JSON.stringify({
668
+ ...(opts ?? {}),
669
+ question,
670
+ }),
671
+ });
672
+ },
673
+ async confirm(id, prompt, opts) {
674
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/confirm`, {
675
+ method: "POST",
676
+ body: JSON.stringify({
677
+ ...(opts ?? {}),
678
+ prompt,
679
+ }),
680
+ });
681
+ },
682
+ async approve(id, opts) {
683
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/approve`, {
684
+ method: "POST",
685
+ body: JSON.stringify(opts ?? {}),
686
+ });
687
+ },
688
+ async reject(id, opts) {
689
+ return requestJson(`/api/issues/${encodeURIComponent(id)}/reject`, {
690
+ method: "POST",
691
+ body: JSON.stringify(opts ?? {}),
692
+ });
693
+ },
694
+ schemas: {
695
+ async list() {
696
+ const companyId = await resolveCompanyId();
697
+ const res = await requestJson(`/api/companies/${encodeURIComponent(companyId)}/schemas`, {
698
+ method: "GET",
699
+ });
700
+ return Array.isArray(res) ? res : res.items;
701
+ },
702
+ async get(ref) {
703
+ const companyId = await resolveCompanyId();
704
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/schemas/${encodeURIComponent(ref)}`, { method: "GET" });
705
+ },
706
+ async create(input) {
707
+ const companyId = await resolveCompanyId();
708
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/schemas`, {
709
+ method: "POST",
710
+ body: JSON.stringify(input),
711
+ });
712
+ },
713
+ },
714
+ async schedule(target, opts) {
715
+ const companyId = await resolveCompanyId();
716
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/schedules`, {
717
+ method: "POST",
718
+ body: JSON.stringify({
719
+ ...opts,
720
+ target,
721
+ }),
722
+ });
723
+ },
724
+ schedules: {
725
+ async list(filters) {
726
+ const companyId = await resolveCompanyId();
727
+ const params = new URLSearchParams();
728
+ if (filters?.target)
729
+ params.set("target", filters.target);
730
+ const qs = params.toString();
731
+ const res = await requestJson(`/api/companies/${encodeURIComponent(companyId)}/schedules${qs ? `?${qs}` : ""}`, { method: "GET" });
732
+ return Array.isArray(res) ? res : res.items;
733
+ },
734
+ async cancel(id) {
735
+ const companyId = await resolveCompanyId();
736
+ return requestJson(`/api/companies/${encodeURIComponent(companyId)}/schedules/${encodeURIComponent(id)}`, { method: "DELETE" });
737
+ },
738
+ },
739
+ async wait(targetOrFilters, maybeFilters, opts) {
740
+ const companyId = await resolveCompanyId();
741
+ let target;
742
+ let filters = {};
743
+ if (typeof targetOrFilters === "string") {
744
+ target = targetOrFilters;
745
+ filters = maybeFilters ?? {};
746
+ }
747
+ else if (targetOrFilters && typeof targetOrFilters === "object") {
748
+ filters = targetOrFilters;
749
+ }
750
+ else if (maybeFilters && typeof maybeFilters === "object") {
751
+ filters = maybeFilters;
752
+ }
753
+ const params = new URLSearchParams();
754
+ if (target) {
755
+ if (isIssueReference(target)) {
756
+ params.set("issueId", target);
757
+ }
758
+ else {
759
+ params.set("channel", target);
760
+ }
761
+ }
762
+ if (filters.channel)
763
+ params.set("channel", filters.channel);
764
+ if (filters.issueId)
765
+ params.set("issueId", filters.issueId);
766
+ appendMultiParam(params, "event", filters.event);
767
+ appendMultiParam(params, "label", filters.label);
768
+ if (filters.mentions)
769
+ params.set("mentions", filters.mentions);
770
+ if (filters.schema)
771
+ params.set("schema", filters.schema);
772
+ if (filters.after)
773
+ params.set("after", filters.after);
774
+ const timeoutInput = opts?.timeout ?? filters.timeout;
775
+ const totalTimeoutMs = timeoutInput !== undefined ? parseDurationMs(timeoutInput) : 60_000;
776
+ const deadline = Date.now() + Math.max(0, totalTimeoutMs);
777
+ const SERVER_MAX_WAIT_MS = 120_000;
778
+ let currentAfter = filters.after;
779
+ while (true) {
780
+ if (opts?.signal?.aborted) {
781
+ throw new DOMException("The operation was aborted", "AbortError");
782
+ }
783
+ const remainingMs = totalTimeoutMs <= 0 ? 0 : Math.max(0, deadline - Date.now());
784
+ const hopMs = totalTimeoutMs <= 0
785
+ ? 0
786
+ : Math.min(Math.max(1, remainingMs), SERVER_MAX_WAIT_MS);
787
+ const hopParams = new URLSearchParams(params);
788
+ if (currentAfter) {
789
+ hopParams.set("after", currentAfter);
790
+ }
791
+ hopParams.set("timeoutMs", String(hopMs));
792
+ const res = await requestRaw(`/api/companies/${encodeURIComponent(companyId)}/events/wait?${hopParams.toString()}`, {
793
+ method: "GET",
794
+ signal: opts?.signal,
795
+ });
796
+ if (res.status !== 204) {
797
+ return (await res.json());
798
+ }
799
+ const nextCursor = res.headers.get("x-paperclip-cursor")?.trim();
800
+ if (!currentAfter && nextCursor) {
801
+ currentAfter = nextCursor;
802
+ }
803
+ if (totalTimeoutMs <= 0 || Date.now() >= deadline) {
804
+ return null;
805
+ }
806
+ }
807
+ },
808
+ async *events(filters, opts) {
809
+ const companyId = await resolveCompanyId();
810
+ const params = new URLSearchParams();
811
+ if (filters?.channel)
812
+ params.set("channel", filters.channel);
813
+ if (filters?.issueId)
814
+ params.set("issueId", filters.issueId);
815
+ appendMultiParam(params, "event", filters?.event);
816
+ appendMultiParam(params, "label", filters?.label);
817
+ if (filters?.mentions)
818
+ params.set("mentions", filters.mentions);
819
+ if (filters?.schema)
820
+ params.set("schema", filters.schema);
821
+ if (filters?.after)
822
+ params.set("after", filters.after);
823
+ const res = await requestRaw(`/api/companies/${encodeURIComponent(companyId)}/events/stream?${params.toString()}`, {
824
+ method: "GET",
825
+ headers: { accept: "text/event-stream" },
826
+ signal: opts?.signal,
827
+ });
828
+ if (!res.body) {
829
+ return;
830
+ }
831
+ yield* parseSseEventStream(res.body, opts?.signal);
832
+ },
833
+ };
834
+ }
835
+ export function paperclip(opts = {}) {
836
+ const nodeEnv = readNodeEnv();
837
+ const storedCtx = readStoredContext(opts.homeDir);
838
+ const trustedCtx = readTrustedContext(opts.homeDir);
839
+ const rawApiBase = opts.apiBase ??
840
+ nodeEnv.apiBase ??
841
+ storedCtx?.apiBase ??
842
+ storedCtx?.apiUrl ??
843
+ "http://127.0.0.1:3100";
844
+ const apiBase = rawApiBase.replace(/\/+$/, "");
845
+ const apiBaseFromRepoLocal = !opts.apiBase &&
846
+ !nodeEnv.apiBase &&
847
+ Boolean(storedCtx?.fromRepoLocal) &&
848
+ !sameOriginUrl(apiBase, trustedCtx?.apiBase ?? trustedCtx?.apiUrl);
849
+ const profile = opts.profile !== undefined
850
+ ? opts.profile
851
+ : nodeEnv.profile ?? storedCtx?.profile ?? null;
852
+ const fetchImpl = opts.fetch ?? globalThis.fetch;
853
+ if (typeof fetchImpl !== "function") {
854
+ throw new Error("globalThis.fetch is unavailable; provide opts.fetch when creating a paperclip() client");
855
+ }
856
+ return createHandle({
857
+ apiBase,
858
+ apiBaseFromRepoLocal,
859
+ explicitApiKey: opts.apiKey,
860
+ companyArg: opts.company,
861
+ profile,
862
+ scopes: opts.scopes,
863
+ fetchImpl,
864
+ homeDir: opts.homeDir,
865
+ cachedCompanyId: null,
866
+ companyPromise: null,
867
+ });
868
+ }
869
+ //# sourceMappingURL=client.js.map