@picassohq/cli 0.0.0-stage → 1.0.1

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 (230) hide show
  1. package/agents/browser_agent.yaml +53 -0
  2. package/agents/code_reviewer.yaml +49 -0
  3. package/agents/codebase_investigator.yaml +70 -0
  4. package/agents/design_auditor.yaml +50 -0
  5. package/agents/devex_auditor.yaml +50 -0
  6. package/agents/doc_writer.yaml +54 -0
  7. package/agents/engineering_architect.yaml +51 -0
  8. package/agents/generalist.yaml +55 -0
  9. package/agents/migration_planner.yaml +51 -0
  10. package/agents/product_reviewer.yaml +48 -0
  11. package/agents/qa_operator.yaml +52 -0
  12. package/agents/release_engineer.yaml +50 -0
  13. package/agents/security_auditor.yaml +62 -0
  14. package/agents/test_writer.yaml +52 -0
  15. package/assets/first-light-lockup.png +0 -0
  16. package/assets/first-light-tagline.png +0 -0
  17. package/assets/house/README.md +26 -0
  18. package/assets/house/auraone-banner.png +0 -0
  19. package/assets/house/auraone-logo.png +0 -0
  20. package/assets/house/kiro-prism-banner.png +0 -0
  21. package/assets/house/kiro-prism-logo.png +0 -0
  22. package/assets/house/picasso-banner.png +0 -0
  23. package/assets/house/picasso-logo.png +0 -0
  24. package/assets/wordmark-app.png +0 -0
  25. package/assets/wordmark-tight.png +0 -0
  26. package/assets/wordmark.png +0 -0
  27. package/design/styles.css +1042 -0
  28. package/dist/build-info.json +37 -0
  29. package/dist/index.js +3028 -0
  30. package/goals/cli-hardening.yaml +40 -0
  31. package/goals/repo-maintenance.yaml +40 -0
  32. package/package.json +49 -4
  33. package/runtime/dsh/picasso-ecosystem.community.amendments.yml +24 -0
  34. package/runtime/dsh/picasso-ecosystem.community.json +185 -0
  35. package/runtime/dsh/picasso-ecosystem.community.patch.yml +40 -0
  36. package/runtime/dsh/picasso-ecosystem.cordis.patch.yml +143 -0
  37. package/runtime/dsh/picasso-isolation.patch.yml +25 -0
  38. package/runtime/dsh/picasso-providers.patch.yml +373 -0
  39. package/runtime/dsh/picasso-runtime.cordis.yml +738 -0
  40. package/runtime/dsh/picasso-sdk.patch.yml +204 -0
  41. package/runtime/engine-manifest.json +214 -0
  42. package/runtime/package.json +6 -0
  43. package/runtime/packages/picasso-cache-probe/cordis.patch.yml +5 -0
  44. package/runtime/packages/picasso-cache-probe/index.js +477 -0
  45. package/runtime/packages/picasso-cache-probe/package.json +38 -0
  46. package/runtime/packages/picasso-commands/cordis.patch.yml +4 -0
  47. package/runtime/packages/picasso-commands/fresh-review.js +573 -0
  48. package/runtime/packages/picasso-commands/index.js +497 -0
  49. package/runtime/packages/picasso-commands/package.json +28 -0
  50. package/runtime/packages/picasso-instructions/conflicts.js +115 -0
  51. package/runtime/packages/picasso-instructions/formats.js +145 -0
  52. package/runtime/packages/picasso-instructions/frontmatter.js +175 -0
  53. package/runtime/packages/picasso-instructions/glob.js +114 -0
  54. package/runtime/packages/picasso-instructions/globals.js +90 -0
  55. package/runtime/packages/picasso-instructions/imports.js +123 -0
  56. package/runtime/packages/picasso-instructions/index.js +326 -0
  57. package/runtime/packages/picasso-instructions/modes.js +64 -0
  58. package/runtime/packages/picasso-instructions/package.json +30 -0
  59. package/runtime/packages/picasso-instructions/parse.js +134 -0
  60. package/runtime/packages/picasso-instructions/report.js +161 -0
  61. package/runtime/packages/picasso-instructions/resolve.js +939 -0
  62. package/runtime/packages/picasso-instructions/scan.js +181 -0
  63. package/runtime/packages/picasso-instructions/settings.js +150 -0
  64. package/runtime/packages/picasso-instructions/skills.js +345 -0
  65. package/runtime/packages/picasso-lean-context/cordis.patch.yml +13 -0
  66. package/runtime/packages/picasso-lean-context/index.js +6049 -0
  67. package/runtime/packages/picasso-lean-context/package.json +26 -0
  68. package/runtime/packages/picasso-lean-context/timeline.js +417 -0
  69. package/runtime/packages/picasso-librarian/classify.js +146 -0
  70. package/runtime/packages/picasso-librarian/cordis.patch.yml +10 -0
  71. package/runtime/packages/picasso-librarian/index.js +878 -0
  72. package/runtime/packages/picasso-librarian/notes.js +159 -0
  73. package/runtime/packages/picasso-librarian/package.json +34 -0
  74. package/runtime/packages/picasso-librarian/state.js +478 -0
  75. package/runtime/packages/picasso-memory/index.js +1184 -0
  76. package/runtime/packages/picasso-memory/package.json +21 -0
  77. package/runtime/packages/picasso-model-router/connections.js +895 -0
  78. package/runtime/packages/picasso-model-router/cordis.patch.yml +17 -0
  79. package/runtime/packages/picasso-model-router/index.js +1081 -0
  80. package/runtime/packages/picasso-model-router/package.json +30 -0
  81. package/runtime/packages/picasso-options/cordis.patch.yml +4 -0
  82. package/runtime/packages/picasso-options/index.js +263 -0
  83. package/runtime/packages/picasso-options/package.json +28 -0
  84. package/runtime/packages/picasso-progress-reports/cordis.patch.yml +10 -0
  85. package/runtime/packages/picasso-progress-reports/index.js +267 -0
  86. package/runtime/packages/picasso-progress-reports/package.json +30 -0
  87. package/runtime/packages/picasso-progress-reports/report.js +235 -0
  88. package/runtime/packages/picasso-sdk-server/aside.js +128 -0
  89. package/runtime/packages/picasso-sdk-server/connections.js +38 -0
  90. package/runtime/packages/picasso-sdk-server/context-timeline.js +27 -0
  91. package/runtime/packages/picasso-sdk-server/egress-gate.js +520 -0
  92. package/runtime/packages/picasso-sdk-server/failure-policy.js +246 -0
  93. package/runtime/packages/picasso-sdk-server/file-input.js +169 -0
  94. package/runtime/packages/picasso-sdk-server/goal.js +131 -0
  95. package/runtime/packages/picasso-sdk-server/ignore-file.js +458 -0
  96. package/runtime/packages/picasso-sdk-server/ignore-plugin.js +12 -0
  97. package/runtime/packages/picasso-sdk-server/ignore-sandbox.js +122 -0
  98. package/runtime/packages/picasso-sdk-server/ignore-snapshot.js +131 -0
  99. package/runtime/packages/picasso-sdk-server/image-input.js +44 -0
  100. package/runtime/packages/picasso-sdk-server/index.js +886 -0
  101. package/runtime/packages/picasso-sdk-server/instructions.js +34 -0
  102. package/runtime/packages/picasso-sdk-server/jobs.js +113 -0
  103. package/runtime/packages/picasso-sdk-server/mcp-gate.js +51 -0
  104. package/runtime/packages/picasso-sdk-server/model-selection.js +162 -0
  105. package/runtime/packages/picasso-sdk-server/package.json +50 -0
  106. package/runtime/packages/picasso-sdk-server/picasso-records.js +50 -0
  107. package/runtime/packages/picasso-sdk-server/provider-reload.js +84 -0
  108. package/runtime/packages/picasso-sdk-server/queue.js +322 -0
  109. package/runtime/packages/picasso-sdk-server/requester.js +43 -0
  110. package/runtime/packages/picasso-sdk-server/rewind.js +108 -0
  111. package/runtime/packages/picasso-sdk-server/schedule.js +187 -0
  112. package/runtime/packages/picasso-sdk-server/session-grants.js +35 -0
  113. package/runtime/packages/picasso-sdk-server/session-state.js +118 -0
  114. package/runtime/packages/picasso-sdk-server/shell-paths.js +869 -0
  115. package/runtime/packages/picasso-sdk-server/stream-deltas.js +211 -0
  116. package/runtime/packages/picasso-sdk-server/team-tasks.js +451 -0
  117. package/runtime/packages/picasso-sdk-server/tool-output.js +128 -0
  118. package/runtime/packages/picasso-sdk-server/tools-list.js +51 -0
  119. package/runtime/packages/picasso-sdk-server/wave2-hooks.js +612 -0
  120. package/runtime/packages/picasso-sdk-server/workspace-archive.js +170 -0
  121. package/runtime/packages/picasso-sdk-server/workspace-restore.js +180 -0
  122. package/runtime/packages/picasso-web-search/backends.js +350 -0
  123. package/runtime/packages/picasso-web-search/config.js +121 -0
  124. package/runtime/packages/picasso-web-search/cordis.patch.yml +7 -0
  125. package/runtime/packages/picasso-web-search/failure.js +111 -0
  126. package/runtime/packages/picasso-web-search/format.js +35 -0
  127. package/runtime/packages/picasso-web-search/index.js +66 -0
  128. package/runtime/packages/picasso-web-search/package.json +38 -0
  129. package/runtime/packages/picasso-web-search/router.js +155 -0
  130. package/runtime/packages/picasso-x-reader/circle.js +324 -0
  131. package/runtime/packages/picasso-x-reader/command.js +169 -0
  132. package/runtime/packages/picasso-x-reader/cordis.patch.yml +8 -0
  133. package/runtime/packages/picasso-x-reader/index.js +170 -0
  134. package/runtime/packages/picasso-x-reader/package.json +39 -0
  135. package/runtime/packages/picasso-x-reader/read.js +370 -0
  136. package/runtime/packages/picasso-x-reader/rewrite.js +38 -0
  137. package/runtime/packages/picasso-x-reader/settings.js +131 -0
  138. package/runtime/packages/picasso-x-reader/urls.js +154 -0
  139. package/runtime/packages/picasso-x-reader/xmd.js +512 -0
  140. package/runtime/plugins/picasso-auth-bridge/index.js +380 -0
  141. package/runtime/plugins/picasso-auth-bridge/package.json +28 -0
  142. package/runtime/plugins/picasso-auto-approve/index.js +1003 -0
  143. package/runtime/plugins/picasso-auto-approve/package.json +28 -0
  144. package/runtime/plugins/picasso-browser/index.js +424 -0
  145. package/runtime/plugins/picasso-browser/package.json +28 -0
  146. package/runtime/plugins/picasso-checkpoints/evidence.js +210 -0
  147. package/runtime/plugins/picasso-checkpoints/index.js +885 -0
  148. package/runtime/plugins/picasso-checkpoints/package.json +29 -0
  149. package/runtime/plugins/picasso-commands/fresh-review.js +573 -0
  150. package/runtime/plugins/picasso-commands/index.js +497 -0
  151. package/runtime/plugins/picasso-commands/package.json +28 -0
  152. package/runtime/plugins/picasso-instructions/conflicts.js +115 -0
  153. package/runtime/plugins/picasso-instructions/formats.js +145 -0
  154. package/runtime/plugins/picasso-instructions/frontmatter.js +175 -0
  155. package/runtime/plugins/picasso-instructions/glob.js +114 -0
  156. package/runtime/plugins/picasso-instructions/globals.js +90 -0
  157. package/runtime/plugins/picasso-instructions/imports.js +123 -0
  158. package/runtime/plugins/picasso-instructions/index.js +326 -0
  159. package/runtime/plugins/picasso-instructions/modes.js +64 -0
  160. package/runtime/plugins/picasso-instructions/package.json +30 -0
  161. package/runtime/plugins/picasso-instructions/parse.js +134 -0
  162. package/runtime/plugins/picasso-instructions/report.js +161 -0
  163. package/runtime/plugins/picasso-instructions/resolve.js +939 -0
  164. package/runtime/plugins/picasso-instructions/scan.js +181 -0
  165. package/runtime/plugins/picasso-instructions/settings.js +150 -0
  166. package/runtime/plugins/picasso-instructions/skills.js +345 -0
  167. package/runtime/plugins/picasso-lean-context/index.js +6049 -0
  168. package/runtime/plugins/picasso-lean-context/package.json +26 -0
  169. package/runtime/plugins/picasso-lean-context/timeline.js +417 -0
  170. package/runtime/plugins/picasso-librarian/classify.js +146 -0
  171. package/runtime/plugins/picasso-librarian/index.js +878 -0
  172. package/runtime/plugins/picasso-librarian/notes.js +159 -0
  173. package/runtime/plugins/picasso-librarian/package.json +34 -0
  174. package/runtime/plugins/picasso-librarian/state.js +478 -0
  175. package/runtime/plugins/picasso-lifecycle-hooks/index.js +186 -0
  176. package/runtime/plugins/picasso-lifecycle-hooks/package.json +25 -0
  177. package/runtime/plugins/picasso-mail/inbox.js +207 -0
  178. package/runtime/plugins/picasso-mail/index.js +293 -0
  179. package/runtime/plugins/picasso-mail/package.json +22 -0
  180. package/runtime/plugins/picasso-memory/index.js +1184 -0
  181. package/runtime/plugins/picasso-memory/package.json +21 -0
  182. package/runtime/plugins/picasso-model-router/connections.js +895 -0
  183. package/runtime/plugins/picasso-model-router/index.js +1081 -0
  184. package/runtime/plugins/picasso-model-router/package.json +30 -0
  185. package/runtime/plugins/picasso-options/index.js +263 -0
  186. package/runtime/plugins/picasso-options/package.json +28 -0
  187. package/runtime/plugins/picasso-progress-reports/index.js +267 -0
  188. package/runtime/plugins/picasso-progress-reports/package.json +30 -0
  189. package/runtime/plugins/picasso-progress-reports/report.js +235 -0
  190. package/runtime/plugins/picasso-provider-models/index.js +436 -0
  191. package/runtime/plugins/picasso-provider-models/package.json +28 -0
  192. package/runtime/plugins/picasso-repo-index/index.js +1850 -0
  193. package/runtime/plugins/picasso-repo-index/package.json +28 -0
  194. package/runtime/plugins/picasso-schedule-pause/index.js +344 -0
  195. package/runtime/plugins/picasso-schedule-pause/package.json +28 -0
  196. package/runtime/plugins/picasso-session-lifecycle/index.js +796 -0
  197. package/runtime/plugins/picasso-session-lifecycle/package.json +28 -0
  198. package/runtime/plugins/picasso-trackers/index.js +895 -0
  199. package/runtime/plugins/picasso-trackers/package.json +28 -0
  200. package/runtime/plugins/picasso-web-search/backends.js +350 -0
  201. package/runtime/plugins/picasso-web-search/config.js +121 -0
  202. package/runtime/plugins/picasso-web-search/failure.js +111 -0
  203. package/runtime/plugins/picasso-web-search/format.js +35 -0
  204. package/runtime/plugins/picasso-web-search/index.js +66 -0
  205. package/runtime/plugins/picasso-web-search/package.json +38 -0
  206. package/runtime/plugins/picasso-web-search/router.js +155 -0
  207. package/runtime/plugins/picasso-x-reader/circle.js +324 -0
  208. package/runtime/plugins/picasso-x-reader/command.js +169 -0
  209. package/runtime/plugins/picasso-x-reader/index.js +170 -0
  210. package/runtime/plugins/picasso-x-reader/package.json +39 -0
  211. package/runtime/plugins/picasso-x-reader/read.js +370 -0
  212. package/runtime/plugins/picasso-x-reader/rewrite.js +38 -0
  213. package/runtime/plugins/picasso-x-reader/settings.js +131 -0
  214. package/runtime/plugins/picasso-x-reader/urls.js +154 -0
  215. package/runtime/plugins/picasso-x-reader/xmd.js +512 -0
  216. package/runtime/relay/attachment-text.mjs +737 -0
  217. package/runtime/relay/companion/companion.css +126 -0
  218. package/runtime/relay/companion/companion.js +648 -0
  219. package/runtime/relay/companion/icon.svg +5 -0
  220. package/runtime/relay/companion/index.html +25 -0
  221. package/runtime/relay/companion/manifest.json +13 -0
  222. package/runtime/relay/companion/sw.js +41 -0
  223. package/runtime/relay/main.mjs +37252 -0
  224. package/runtime/relay/model-listing.mjs +614 -0
  225. package/runtime/relay/versions-routes.mjs +1198 -0
  226. package/runtime/verify-plugins.mjs +205 -0
  227. package/runtime-lock/package-lock.json +10779 -0
  228. package/runtime-lock/package.json +7 -0
  229. package/scripts/install-runtime.mjs +400 -0
  230. package/README.md +0 -3
@@ -0,0 +1,380 @@
1
+ // @picasso/auth-bridge — provider and account sign-in flows through dsh's authorization seam.
2
+ //
3
+ // dsh 0.2.1-rc.5's authorization service (`ctx.authorization`) holds every registered credential-obtaining flow —
4
+ // the pi-ai provider OAuth/device-code logins (`llm-pi-ai/<provider>`), the DeepSeek account grant — but it is an
5
+ // in-process seam: `begin` requires a live interaction object whose `notify`/`prompt` callbacks cannot cross
6
+ // Remote's JSON boundary, so no caller outside the Host can run a flow. This plugin is the crossing. One Host
7
+ // Remote service, bound under the `authorization` namespace, drives a flow for a surface that speaks only JSON:
8
+ //
9
+ // list() -> {flows:[{key, label, methods:[{id,label}], inFlight, attempt?}]} — `attempt` names the running
10
+ // attempt's id when this bridge started it.
11
+ // begin(key, method?) -> {attempt, key, method, status:'running'} — starts one attempt and answers as soon as it
12
+ // is registered; every notice, prompt and the settlement stream over `watch`.
13
+ // watch(attempt[, signal]) -> a stream of the attempt's whole event log, replayed then live, ending after its
14
+ // `settled`/`failed` frame. `attempt` accepts an attempt id or a credential key, which resolves
15
+ // to the key's running attempt (`authorization/unknown-attempt` when none is or was running).
16
+ // answer(key, promptId, value?, declined?, attempt?) -> {answered:true} — resolves the pending prompt: `value`
17
+ // is the option id for a `select`, the typed text for `text`/`secret`. `declined:true` is the
18
+ // human's "no" — the prompt rejects with dsh's AuthorizationDeclinedError (or the attempt is
19
+ // withdrawn when the class cannot be loaded) and the attempt settles `cancelled`.
20
+ // cancel(key) -> {key, cancelled} — withdraws the key's running attempt (dsh's own cancel plus this bridge's).
21
+ //
22
+ // `watch` frames, all JSON-safe:
23
+ // {type:'began', attempt, key, method, at}
24
+ // {type:'notice', attempt, notice:{message, url?, code?}}
25
+ // {type:'prompt', attempt, prompt:{id, kind:'select'|'secret'|'text', message, placeholder?,
26
+ // options?:[{id,label}]}}
27
+ // {type:'prompt-closed', attempt, prompt:<id>, answered:<bool>} — answered, withdrawn by its own signal (a flow
28
+ // retiring the losing question of a race), or ended with the attempt.
29
+ // {type:'settled', attempt, key, status:'authorized'|'cancelled'}
30
+ // {type:'failed', attempt, key, error:{code, message}}
31
+ //
32
+ // Prompt answers travel as strings, never into logs or frames; notices pass only message/url/code through. Nothing
33
+ // credential-shaped leaves the seam: a committed record stays in dsh's credential store exactly as dsh wrote it.
34
+ //
35
+ // Zero runtime dependencies, like @picasso/schedule-pause: Remote methods use the gateway's source-mode markers and
36
+ // failures are structural dsh RemoteErrors (`isDSHRemoteError`, code `authorization/<code>`).
37
+ import { createRequire } from 'node:module';
38
+ import { pathToFileURL } from 'node:url';
39
+
40
+ export const name = 'picasso-auth-bridge';
41
+ export const dshTarget = '0.2.2-rc.3';
42
+ /** Provided under `authorizationBridge` (dsh's own `authorization` name is taken) but namespaced `authorization`. */
43
+ export const SERVICE_KEY = 'authorizationBridge';
44
+ export const NAMESPACE = 'authorization';
45
+ const REMOTE_METHOD_DESCRIPTOR = '@deepseek-ai/dsh-typert-protocol/remote-methods';
46
+ /** Settled attempts stay replayable for a late watcher; only this many are kept. */
47
+ export const FINISHED_LIMIT = 64;
48
+ const PROMPT_KINDS = new Set(['select', 'secret', 'text']);
49
+
50
+ export class AuthorizationBridgeError extends Error {
51
+ constructor(code, message, details = {}) {
52
+ super(message);
53
+ this.name = 'RemoteError';
54
+ this.code = `authorization/${code}`;
55
+ this.details = details;
56
+ this.isDSHRemoteError = true;
57
+ }
58
+ }
59
+
60
+ const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
61
+
62
+ /**
63
+ * The bridge. `authorization` is dsh's authorization service (`list`, `describe`, `begin`, `cancel`), resolved on
64
+ * each call so the plugin can start before it. `declined` is dsh's AuthorizationDeclinedError class — the rejection
65
+ * a declined prompt must carry so the seam reads refusal, not breakage — loaded from the bundled dsh by `apply`
66
+ * (undefined stands in when it cannot be resolved; `answer` then withdraws the attempt for the same `cancelled`).
67
+ */
68
+ export class AuthorizationBridge {
69
+ constructor(options) {
70
+ this.authorization = typeof options.authorization === 'function' ? options.authorization : () => options.authorization;
71
+ this.declined = options.declined;
72
+ this.now = options.now ?? Date.now;
73
+ this.attemptId = options.attemptId ?? (() => crypto.randomUUID());
74
+ this.promptId = options.promptId ?? (() => crypto.randomUUID());
75
+ /** Running attempts by key, and every attempt (running or settled) by id for `watch` replay. */
76
+ this.running = new Map();
77
+ this.attempts = new Map();
78
+ }
79
+
80
+ // ── Remote surface (`authorization/<method>`; wire args are the parameter names) ────────────────────────────
81
+
82
+ /** Every registered flow, marked `inFlight` with the bridge's own attempt id when it is running one. */
83
+ list() {
84
+ const flows = this.service().list();
85
+ return {
86
+ flows: (Array.isArray(flows) ? flows : []).map((entry) => {
87
+ const running = this.running.get(entry.key);
88
+ return { ...entry, ...(running === undefined ? {} : { attempt: running.id }) };
89
+ }),
90
+ };
91
+ }
92
+
93
+ /** Starts one attempt for `key` (method defaults to the flow's first), answering with the new attempt id. */
94
+ begin(key, method) {
95
+ const flowKey = requireString(key, 'key');
96
+ const authorization = this.service();
97
+ const entry = authorization.describe?.(flowKey) ?? (authorization.list?.() ?? []).find((candidate) => candidate.key === flowKey);
98
+ if (entry === undefined) throw new AuthorizationBridgeError('no-flow', `No sign-in flow is registered for ${flowKey}.`, { key: flowKey });
99
+ if (entry.inFlight === true || this.running.has(flowKey)) {
100
+ throw new AuthorizationBridgeError('already-in-flight', `A sign-in for ${flowKey} is already running.`, { key: flowKey });
101
+ }
102
+ const methods = Array.isArray(entry.methods) ? entry.methods : [];
103
+ if (methods.length === 0) throw new AuthorizationBridgeError('no-methods', `The sign-in flow for ${flowKey} offers no methods.`, { key: flowKey });
104
+ const chosen = method === undefined ? methods[0]?.id : requireString(method, 'method');
105
+ if (!methods.some((candidate) => isRecord(candidate) && candidate.id === chosen)) {
106
+ throw new AuthorizationBridgeError('unknown-method', `The sign-in flow for ${flowKey} offers no method ${JSON.stringify(chosen)}.`, { key: flowKey, method: chosen });
107
+ }
108
+ const attempt = {
109
+ id: this.attemptId(),
110
+ key: flowKey,
111
+ method: chosen,
112
+ controller: new AbortController(),
113
+ events: [],
114
+ listeners: new Set(),
115
+ prompts: new Map(),
116
+ ended: false,
117
+ done: Promise.resolve(),
118
+ };
119
+ this.attempts.set(attempt.id, attempt);
120
+ this.running.set(flowKey, attempt);
121
+ // A withdrawn attempt releases any prompt the flow left unanswered, so no await can hang.
122
+ attempt.controller.signal.addEventListener('abort', () => this.withdrawAll(attempt), { once: true });
123
+ this.push(attempt, { type: 'began', attempt: attempt.id, key: flowKey, method: chosen, at: this.now() });
124
+ attempt.done = Promise.resolve()
125
+ .then(() => authorization.begin({ key: flowKey, method: chosen, signal: attempt.controller.signal, interaction: this.interaction(attempt) }))
126
+ .then(
127
+ (outcome) => this.push(attempt, { type: 'settled', attempt: attempt.id, key: flowKey, status: outcome?.status === 'authorized' ? 'authorized' : 'cancelled' }),
128
+ (error) => this.push(attempt, { type: 'failed', attempt: attempt.id, key: flowKey, error: errorFrame(error) }),
129
+ )
130
+ .then(() => this.finish(attempt), () => this.finish(attempt));
131
+ // The attempt lives past this reply; its failures land in the log, never on this call.
132
+ attempt.done.catch(() => undefined);
133
+ return { attempt: attempt.id, key: flowKey, method: chosen, status: 'running' };
134
+ }
135
+
136
+ /** The attempt's whole event log, replayed, then live; the stream ends after `settled`/`failed`. */
137
+ async *watch(attempt, signal) {
138
+ const record = this.attemptFor(attempt);
139
+ let index = 0;
140
+ while (true) {
141
+ if (index < record.events.length) {
142
+ yield record.events[index];
143
+ index += 1;
144
+ continue;
145
+ }
146
+ if (record.ended || signal?.aborted === true) return;
147
+ await this.nextEvent(record, index, signal);
148
+ if (signal?.aborted === true) return;
149
+ }
150
+ }
151
+
152
+ /** Resolves (`value`) or declines (`declined: true`) the pending prompt of `key`'s running attempt. */
153
+ answer(key, promptId, value, declined, attempt) {
154
+ const flowKey = requireString(key, 'key');
155
+ const promptKey = requireString(promptId, 'promptId');
156
+ const record = this.running.get(flowKey);
157
+ if (record === undefined) throw new AuthorizationBridgeError('unknown-attempt', `No sign-in is running for ${flowKey}.`, { key: flowKey });
158
+ if (attempt !== undefined && attempt !== record.id) {
159
+ throw new AuthorizationBridgeError('stale-attempt', `That sign-in attempt for ${flowKey} has ended; begin again.`, { key: flowKey, attempt });
160
+ }
161
+ const pending = record.prompts.get(promptKey);
162
+ if (pending === undefined) {
163
+ throw new AuthorizationBridgeError('unknown-prompt', `No question ${promptKey} is open for ${flowKey}.`, { key: flowKey, prompt: promptKey });
164
+ }
165
+ if (declined === true) {
166
+ // The human's "no" ends the attempt, not just the question: withdraw it, then reject the prompt the way the
167
+ // seam marks refusal. A missing DeclinedError class still settles `cancelled` through the abort.
168
+ record.controller.abort();
169
+ const DeclinedError = this.declined;
170
+ this.closePrompt(record, promptKey, false, () => (DeclinedError === undefined ? new Error('the authorization prompt was declined') : new DeclinedError()));
171
+ return { key: flowKey, prompt: promptKey, answered: true, declined: true };
172
+ }
173
+ if (typeof value !== 'string' || value.length === 0 || value.length > 8192) {
174
+ throw new AuthorizationBridgeError('answer-invalid', `The answer for ${flowKey} must be a non-empty string.`, { key: flowKey, prompt: promptKey });
175
+ }
176
+ this.closePrompt(record, promptKey, true, () => value);
177
+ return { key: flowKey, prompt: promptKey, answered: true };
178
+ }
179
+
180
+ /** Withdraws `key`'s running attempt — this bridge's controller plus dsh's own cancel for attempts it holds. */
181
+ cancel(key) {
182
+ const flowKey = requireString(key, 'key');
183
+ const authorization = this.service();
184
+ const record = this.running.get(flowKey);
185
+ const entry = authorization.describe?.(flowKey);
186
+ if (entry === undefined) throw new AuthorizationBridgeError('no-flow', `No sign-in flow is registered for ${flowKey}.`, { key: flowKey });
187
+ const inFlight = entry.inFlight === true || record !== undefined;
188
+ authorization.cancel?.(flowKey);
189
+ record?.controller.abort();
190
+ return { key: flowKey, cancelled: inFlight };
191
+ }
192
+
193
+ // ── Attempt machinery ──────────────────────────────────────────────────────────────────────────────────────
194
+
195
+ /** `authorization` resolved lazily so a mount order that puts dsh's service after this plugin still works. */
196
+ service() {
197
+ let authorization;
198
+ try {
199
+ authorization = this.authorization();
200
+ } catch {
201
+ authorization = undefined;
202
+ }
203
+ if (!authorization || typeof authorization.list !== 'function' || typeof authorization.begin !== 'function') {
204
+ throw new AuthorizationBridgeError('unavailable', 'dsh\'s authorization service is not mounted.');
205
+ }
206
+ return authorization;
207
+ }
208
+
209
+ /** `attempt` may name an attempt id or a credential key (its running attempt, else its last settled one). */
210
+ attemptFor(attempt) {
211
+ const id = requireString(attempt, 'attempt');
212
+ const record = this.attempts.get(id) ?? this.running.get(id) ?? this.lastFinishedFor(id);
213
+ if (record === undefined) throw new AuthorizationBridgeError('unknown-attempt', `No sign-in attempt ${id} is or was running.`, { attempt: id });
214
+ return record;
215
+ }
216
+
217
+ /** The most recently settled attempt for a key, for `watch(key)` after the attempt already ended. */
218
+ lastFinishedFor(key) {
219
+ for (const record of [...this.attempts.values()].reverse()) if (record.key === key && record.ended) return record;
220
+ return undefined;
221
+ }
222
+
223
+ /** The interaction `authorization.begin` requires: notices logged, prompts correlated by id. */
224
+ interaction(attempt) {
225
+ return {
226
+ notify: (notice) => {
227
+ const safe = sanitizeNotice(notice);
228
+ if (safe !== undefined) this.push(attempt, { type: 'notice', attempt: attempt.id, notice: safe });
229
+ },
230
+ prompt: (prompt) => this.ask(attempt, prompt),
231
+ };
232
+ }
233
+
234
+ /**
235
+ * One `interaction.prompt`: the JSON-safe face is logged with a fresh id, the promise answers through
236
+ * `answer(key, promptId, …)`. A prompt carrying its own `signal` is withdrawn when that signal fires — a flow
237
+ * racing a browser callback against a typed code retires the losing question that way — and every open prompt
238
+ * is rejected when the attempt ends or is withdrawn so no flow await can leak.
239
+ */
240
+ ask(attempt, prompt) {
241
+ if (attempt.ended || attempt.controller.signal.aborted) return Promise.reject(new Error('the authorization attempt has ended'));
242
+ if (prompt?.signal instanceof AbortSignal && prompt.signal.aborted) return Promise.reject(new Error('the authorization prompt was withdrawn'));
243
+ const id = this.promptId();
244
+ const wire = sanitizePrompt(prompt, id);
245
+ return new Promise((resolve, reject) => {
246
+ const pending = { resolve: (value) => resolve(value), reject };
247
+ attempt.prompts.set(id, pending);
248
+ let onAbort;
249
+ if (prompt?.signal instanceof AbortSignal) {
250
+ onAbort = () => this.closePrompt(attempt, id, false, () => new Error('the authorization prompt was withdrawn'));
251
+ prompt.signal.addEventListener('abort', onAbort, { once: true });
252
+ }
253
+ pending.cleanup = () => prompt?.signal?.removeEventListener('abort', onAbort);
254
+ this.push(attempt, { type: 'prompt', attempt: attempt.id, prompt: wire });
255
+ });
256
+ }
257
+
258
+ /** Settle one pending prompt: close it on the log, then resolve or reject the flow's await. */
259
+ closePrompt(attempt, id, answered, settle) {
260
+ const pending = attempt.prompts.get(id);
261
+ if (pending === undefined) return;
262
+ attempt.prompts.delete(id);
263
+ pending.cleanup?.();
264
+ this.push(attempt, { type: 'prompt-closed', attempt: attempt.id, prompt: id, answered });
265
+ if (answered) pending.resolve(settle());
266
+ else pending.reject(settle());
267
+ }
268
+
269
+ /** Every still-open prompt is rejected — the attempt went away before the flow stopped asking. */
270
+ withdrawAll(attempt) {
271
+ for (const id of [...attempt.prompts.keys()]) {
272
+ this.closePrompt(attempt, id, false, () => new Error('the authorization attempt was withdrawn'));
273
+ }
274
+ }
275
+
276
+ /** Append an event to the attempt's log and wake its watchers. Terminal frames mark the attempt ended. */
277
+ push(attempt, event) {
278
+ attempt.events.push(event);
279
+ if (event.type === 'settled' || event.type === 'failed') attempt.ended = true;
280
+ for (const listener of [...attempt.listeners]) listener();
281
+ }
282
+
283
+ /** After the settlement lands: release the key, retire the log beyond FINISHED_LIMIT settled attempts. */
284
+ finish(attempt) {
285
+ if (this.running.get(attempt.key) === attempt) this.running.delete(attempt.key);
286
+ this.withdrawAll(attempt);
287
+ const settled = [...this.attempts.values()].filter((record) => record.ended);
288
+ for (const record of settled.slice(0, Math.max(0, settled.length - FINISHED_LIMIT))) this.attempts.delete(record.id);
289
+ for (const listener of [...attempt.listeners]) listener();
290
+ }
291
+
292
+ /** Resolves when the log grows past `index`, the attempt ends, or `signal` aborts. */
293
+ nextEvent(attempt, index, signal) {
294
+ return new Promise((resolve) => {
295
+ if (attempt.events.length > index || attempt.ended || signal?.aborted === true) return resolve();
296
+ const wake = () => {
297
+ attempt.listeners.delete(wake);
298
+ signal?.removeEventListener('abort', onAbort);
299
+ resolve();
300
+ };
301
+ const onAbort = () => wake();
302
+ attempt.listeners.add(wake);
303
+ signal?.addEventListener('abort', onAbort, { once: true });
304
+ });
305
+ }
306
+ }
307
+
308
+ const REMOTE_METHODS = [
309
+ ...['list', 'begin', 'answer', 'cancel'].map((method) => Object.freeze({ method, invocation: Object.freeze({ kind: 'direct' }) })),
310
+ Object.freeze({ method: 'watch', mode: 'stream', invocation: Object.freeze({ kind: 'direct' }) }),
311
+ ];
312
+
313
+ Object.defineProperty(AuthorizationBridge.prototype, REMOTE_METHOD_DESCRIPTOR, {
314
+ configurable: true,
315
+ value: Object.freeze({ version: 1, methods: Object.freeze(REMOTE_METHODS) }),
316
+ });
317
+
318
+ function requireString(value, field) {
319
+ if (typeof value !== 'string' || value.length === 0 || value.length > 1024) throw new AuthorizationBridgeError('bad-request', `${field} must be a non-empty string.`);
320
+ return value;
321
+ }
322
+
323
+ /** A notice keeps only its string fields (`{message, url, code}` in dsh's vocabulary); nothing else crosses. */
324
+ export function sanitizeNotice(notice) {
325
+ if (!isRecord(notice)) return undefined;
326
+ const safe = {};
327
+ for (const field of ['message', 'url', 'code']) if (typeof notice[field] === 'string' && notice[field]) safe[field] = notice[field];
328
+ if (safe.message === undefined && safe.url === undefined && safe.code === undefined) return undefined;
329
+ return safe;
330
+ }
331
+
332
+ /** A prompt's wire face: kind/message/placeholder/options (option id + label), never its `signal`. */
333
+ export function sanitizePrompt(prompt, id) {
334
+ const source = isRecord(prompt) ? prompt : {};
335
+ const kind = PROMPT_KINDS.has(source.kind) ? source.kind : 'text';
336
+ const wire = {
337
+ id,
338
+ kind,
339
+ message: typeof source.message === 'string' ? source.message : '',
340
+ };
341
+ if (typeof source.placeholder === 'string' && source.placeholder) wire.placeholder = source.placeholder;
342
+ if (kind === 'select') {
343
+ wire.options = (Array.isArray(source.options) ? source.options : []).flatMap((option) => {
344
+ if (!isRecord(option) || typeof option.id !== 'string' || !option.id) return [];
345
+ return [{ id: option.id, label: typeof option.label === 'string' ? option.label : option.id }];
346
+ });
347
+ }
348
+ return wire;
349
+ }
350
+
351
+ /** The `failed` frame's error face: dsh's SCREAMING codes arrive kebab-cased like the rest of the wire. */
352
+ function errorFrame(error) {
353
+ const code = typeof error?.code === 'string' ? error.code : 'failed';
354
+ return { code: code.toLowerCase().replace(/_/g, '-'), message: String(error?.message ?? error) };
355
+ }
356
+
357
+ /** Import one of dsh's bundled packages from a path-loaded plugin: anchor createRequire at the dsh bin's realpath. */
358
+ export async function dshModule(dshBin, specifier) {
359
+ const require = createRequire(dshBin);
360
+ return import(pathToFileURL(require.resolve(specifier)).href);
361
+ }
362
+
363
+ /**
364
+ * Cordis entry. Provides `authorizationBridge`, bound for the Typert gateway under the `authorization` namespace.
365
+ * `config.dshBin` (the launched dsh's realpath, written by the relay's generated patch) is where dsh's own
366
+ * AuthorizationDeclinedError loads from; when it cannot resolve, a declined prompt still cancels the attempt
367
+ * through the withdraw path.
368
+ */
369
+ export async function apply(ctx, config) {
370
+ let declined;
371
+ try {
372
+ declined = (await dshModule(config?.dshBin ?? '/opt/homebrew/bin/dsh', '@deepseek-ai/dsh-authorization')).AuthorizationDeclinedError;
373
+ if (typeof declined !== 'function') declined = undefined;
374
+ } catch (error) {
375
+ ctx.logger?.warn?.(`${name}: AuthorizationDeclinedError unavailable (${error instanceof Error ? error.message : String(error)}); declined prompts cancel attempts instead`);
376
+ }
377
+ const service = new AuthorizationBridge({ authorization: () => ctx.get('authorization'), declined });
378
+ service.typertRemote = Object.freeze({ service, serviceKey: SERVICE_KEY, namespace: NAMESPACE });
379
+ ctx.provide(SERVICE_KEY, service);
380
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@picasso/auth-bridge",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "description": "dsh cordis plugin: authorization/list|begin|answer|cancel|watch Host Remote endpoints. Runs a registered sign-in flow (pi-ai OAuth/device code, account grant) with the interaction dsh's authorization seam requires, tunneling notices, prompts and settlements as JSON.",
6
+ "type": "module",
7
+ "main": "index.js",
8
+ "exports": {
9
+ ".": "./index.js",
10
+ "./cordis.patch.yml": "./cordis.patch.yml",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "dshTarget": "0.2.2-rc.3",
14
+ "dsh": {
15
+ "bundle": {
16
+ "patch": "./cordis.patch.yml"
17
+ }
18
+ },
19
+ "scripts": {
20
+ "test": "node --import ../../test/setup/hermetic-preload.mjs --test test/*.test.mjs"
21
+ },
22
+ "files": [
23
+ "index.js",
24
+ "cordis.patch.yml",
25
+ "README.md"
26
+ ],
27
+ "license": "UNLICENSED"
28
+ }