@parallelworks/code 0.1.0 → 0.3.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 (118) hide show
  1. package/README.md +171 -24
  2. package/dist/ago.d.ts +8 -0
  3. package/dist/ago.js +26 -0
  4. package/dist/api.d.ts +3547 -0
  5. package/dist/apiClient.d.ts +26 -0
  6. package/dist/apiClient.js +5 -0
  7. package/dist/base64.d.ts +3 -0
  8. package/dist/base64.js +15 -0
  9. package/dist/chatHandoff.d.ts +12 -0
  10. package/dist/chatHandoff.js +27 -0
  11. package/dist/claudeAccounts.d.ts +112 -0
  12. package/dist/claudeAccounts.js +234 -0
  13. package/dist/claudeApi.d.ts +27 -0
  14. package/dist/claudeApi.js +250 -0
  15. package/dist/claudeSignIn.d.ts +62 -0
  16. package/dist/claudeSignIn.js +239 -0
  17. package/dist/command.d.ts +47 -0
  18. package/dist/command.js +64 -0
  19. package/dist/commandsApi.d.ts +64 -0
  20. package/dist/commandsApi.js +267 -0
  21. package/dist/contextRow.d.ts +25 -0
  22. package/dist/contextRow.js +16 -0
  23. package/dist/createPwCodeApi.d.ts +696 -0
  24. package/dist/createPwCodeApi.js +33 -0
  25. package/dist/howSessionsAppear.d.ts +12 -0
  26. package/dist/howSessionsAppear.js +24 -0
  27. package/dist/index.d.ts +39 -3
  28. package/dist/index.js +38 -3
  29. package/dist/locales/en/agentTranscript.js +162 -0
  30. package/dist/locales/en/agents.js +380 -0
  31. package/dist/locales/en.d.ts +523 -0
  32. package/dist/locales/en.js +8 -0
  33. package/dist/locales/es/agentTranscript.js +162 -0
  34. package/dist/locales/es/agents.js +380 -0
  35. package/dist/locales/es.d.ts +3 -0
  36. package/dist/locales/es.js +8 -0
  37. package/dist/locales/ja/agentTranscript.js +162 -0
  38. package/dist/locales/ja/agents.js +380 -0
  39. package/dist/locales/ja.d.ts +3 -0
  40. package/dist/locales/ja.js +8 -0
  41. package/dist/locales/ko/agentTranscript.js +162 -0
  42. package/dist/locales/ko/agents.js +380 -0
  43. package/dist/locales/ko.d.ts +3 -0
  44. package/dist/locales/ko.js +8 -0
  45. package/dist/locales/zh/agentTranscript.js +162 -0
  46. package/dist/locales/zh/agents.js +380 -0
  47. package/dist/locales/zh.d.ts +3 -0
  48. package/dist/locales/zh.js +8 -0
  49. package/dist/machine.d.ts +51 -0
  50. package/dist/machine.js +60 -0
  51. package/dist/messages.d.ts +17 -0
  52. package/dist/messages.js +31 -0
  53. package/dist/models.d.ts +47 -0
  54. package/dist/models.js +49 -0
  55. package/dist/notices.d.ts +12 -0
  56. package/dist/notices.js +120 -0
  57. package/dist/pullRequest.d.ts +8 -0
  58. package/dist/pullRequest.js +29 -0
  59. package/dist/pullRequestList.d.ts +52 -0
  60. package/dist/pullRequestList.js +106 -0
  61. package/dist/query.d.ts +28 -0
  62. package/dist/query.js +25 -0
  63. package/dist/session.d.ts +75 -1
  64. package/dist/session.js +101 -1
  65. package/dist/sessionActions.d.ts +105 -0
  66. package/dist/sessionActions.js +178 -0
  67. package/dist/sessionEvents.d.ts +27 -0
  68. package/dist/sessionEvents.js +212 -0
  69. package/dist/sessionGroups.d.ts +49 -0
  70. package/dist/sessionGroups.js +72 -0
  71. package/dist/sessionHeader.d.ts +14 -0
  72. package/dist/sessionHeader.js +16 -0
  73. package/dist/sessionList.d.ts +134 -0
  74. package/dist/sessionList.js +271 -0
  75. package/dist/sessionsApi.d.ts +105 -0
  76. package/dist/sessionsApi.js +511 -0
  77. package/dist/settingsApi.d.ts +36 -0
  78. package/dist/settingsApi.js +151 -0
  79. package/dist/startSession.d.ts +138 -0
  80. package/dist/startSession.js +234 -0
  81. package/dist/terminalHandoffPanel.d.ts +30 -0
  82. package/dist/terminalHandoffPanel.js +33 -0
  83. package/dist/toolStrings.d.ts +4 -0
  84. package/dist/toolStrings.js +95 -0
  85. package/dist/transcript.d.ts +14 -1
  86. package/dist/transcript.js +55 -14
  87. package/dist/types.d.ts +20 -1
  88. package/dist/usageLimit.d.ts +8 -0
  89. package/dist/usageLimit.js +13 -0
  90. package/dist/useAwayRecap.d.ts +30 -0
  91. package/dist/useAwayRecap.js +75 -0
  92. package/dist/useComposer.d.ts +121 -0
  93. package/dist/useComposer.js +244 -0
  94. package/dist/useComposerImages.d.ts +66 -0
  95. package/dist/useComposerImages.js +195 -0
  96. package/dist/useConversation.d.ts +190 -0
  97. package/dist/useConversation.js +287 -0
  98. package/dist/useSubagent.d.ts +58 -0
  99. package/dist/useSubagent.js +149 -0
  100. package/dist/useTranscript.d.ts +4 -1
  101. package/dist/useTranscript.js +54 -11
  102. package/dist/wording.d.ts +43 -0
  103. package/dist/wording.js +101 -0
  104. package/dist/workingDirectories.d.ts +32 -0
  105. package/dist/workingDirectories.js +71 -0
  106. package/dist/worktreeChoice.d.ts +31 -0
  107. package/dist/worktreeChoice.js +38 -0
  108. package/locales/en/agentTranscript.json +154 -0
  109. package/locales/en/agents.json +366 -0
  110. package/locales/es/agentTranscript.json +154 -0
  111. package/locales/es/agents.json +366 -0
  112. package/locales/ja/agentTranscript.json +154 -0
  113. package/locales/ja/agents.json +366 -0
  114. package/locales/ko/agentTranscript.json +154 -0
  115. package/locales/ko/agents.json +366 -0
  116. package/locales/zh/agentTranscript.json +154 -0
  117. package/locales/zh/agents.json +366 -0
  118. package/package.json +24 -9
package/README.md CHANGED
@@ -1,24 +1,34 @@
1
1
  # @parallelworks/code
2
2
 
3
- Shared client logic for pw code sessions on Parallel Works ACTIVATE: what a
4
- session's daemon sends, and the transcript its
5
- history and live event stream reduce to. Every app that shows a session uses
6
- the same code, so a session reads the same in each.
3
+ Shared client logic for pw code sessions on Parallel Works ACTIVATE: the calls
4
+ and data hooks a session's pages read through, what a session's daemon sends,
5
+ and the transcript its history and live event stream reduce to. Every app that
6
+ shows a session uses the same code, so a session reads the same in each.
7
7
 
8
- It draws nothing and makes no requests. The host fetches the session, holds
9
- the socket and renders the messages; this package turns the one into the
10
- other.
8
+ It draws nothing. The host hands it an API client, which carries the
9
+ platform's address and signs each request, and a way to open its sockets; it
10
+ renders the messages this package turns the session into, worded in the
11
+ reader's language.
11
12
 
12
13
  ```bash
13
- pnpm add @parallelworks/code @parallelworks/ui react
14
+ pnpm add @parallelworks/code @parallelworks/ui react openapi-fetch swr use-intl
14
15
  ```
15
16
 
16
- `@parallelworks/ui` and `react` are peer dependencies. Only
17
- `@parallelworks/ui/ai/headless` is imported from the ui package, for the
18
- message and part types and the reducers that build them.
17
+ `@parallelworks/ui`, `react`, `openapi-fetch`, `swr` and `use-intl` are peer
18
+ dependencies. Only the ui package's two entries without a DOM are imported:
19
+ `@parallelworks/ui/ai/headless`, for the message and part types and the
20
+ reducers that build them, and `@parallelworks/ui/headless`, for its duration
21
+ format, its strings and its clock.
19
22
 
20
23
  ## What is in it
21
24
 
25
+ - `createPwCodeApi(client, options)`, the calls and SWR data hooks of pw code
26
+ sessions over the host's `openapi-fetch` client: the sessions and machines
27
+ listed (`useSessions`, `useMachines`), a session's transcript
28
+ (`useSessionDetail`), its live event feed (`useSessionEvents`), what can be
29
+ done to it (`useSessionMutations`, `sendMessage`, `steer`, `answerApproval`,
30
+ `resolveTyped`, ...), its subagents, pull requests and worktree, and the
31
+ Claude Code models, accounts and sign-in of its machine.
22
32
  - The wire types (`PwCodeSessionDetail`, `PwCodeStreamEvent`,
23
33
  `PwCodeApprovalRequest`, ...): a session's replayed history, its event
24
34
  frames and the answers it accepts. A host that generates its API types can
@@ -32,34 +42,166 @@ message and part types and the reducers that build them.
32
42
  requests and the answer it accepts.
33
43
  - The paths a session's images are served from (`sessionImageUrl`,
34
44
  `sessionGitHubImageUrl`).
45
+ - What a list of sessions and machines means, as pure functions over the
46
+ structural `PwCodeSession` and `PwCodeMachine`: which sessions wait on the
47
+ reader and in what order they show (`triageSessions`, `newestFirst`,
48
+ `needsYou`, `isActiveSession`, `isMidTurn`), the tone a row takes
49
+ (`sessionVariant`), whether a machine can be reached or can start a session
50
+ (`isMachineDown`, `canStartHere`), the folders a machine's sessions suggest
51
+ (`recentDirectories`), and the command that picks a session up in a terminal
52
+ (`terminalHandoff`).
53
+ - Starting a session, as hooks a host draws its own start screen over:
54
+ where it starts (`useStartLocation`, `startMachines`), the permission mode
55
+ it starts in (`useStartPermissionMode`, `startingMode`), its model,
56
+ allocation and Claude Code account (`useStartModel`), and the start itself
57
+ with its first prompt and images (`useStartSession`). The host passes in its
58
+ saved preferences and opens the session once it starts.
59
+ - The models a session can run (`isClaudeModel`, `runsClaude`, `canRunModel`,
60
+ `modelLabel`), the picker a typed command opens (`pickerCommand`), and the
61
+ machine's Claude models a model picker offers (`useModelCatalog`).
62
+ - A session's working directories: the path helpers that cut a long path to
63
+ fit (`pathSegments`, `shortestPath`, `cutToFit`, `cutSpacing`,
64
+ `lastSegmentAt`), where a directory is saved (`isSavedDirectory`,
65
+ `directoryScopes`), what an approval allowed in one (`allowedAccess`), and
66
+ closing one added for the session only (`useRemoveSessionDirectory`).
67
+ - The choice to keep or remove a session's worktree when the session is
68
+ deleted (`useWorktreeChoice`), and what removing it would delete
69
+ (`worktreeWork`).
70
+ - Where typed text goes (`slashAction`, `shellCommand`) and the commands a
71
+ session offers (`paletteOptions`), read from the structural `PwCodePalette`.
72
+ - The short label of a pull or merge request link (`pullRequestLabel`,
73
+ `pastedPullRequestLabel`), and how a session's pull requests are listed:
74
+ their order (`orderPullRequests`), which fold into a count and how the
75
+ counts read (`foldPullRequests`, `donePullRequestsToFold`,
76
+ `pullRequestGroupState`, `pullRequestWords`), and the row of directories
77
+ and pull requests under a session's title (`sessionContextRow`).
78
+ - What a session's header offers: picking the session up in a terminal
79
+ (`terminalHandoffPanel`), the line a turn shows while it waits for a usage
80
+ limit (`useUsageLimitWait`), the worktree the session works in, read again
81
+ when it moves (`useFollowedWorktree`), and how sessions get listed
82
+ (`howSessionsAppear`).
83
+ - The messages a session is shown in, in English, Spanish, Japanese, Korean
84
+ and Chinese (`loadPwCodeMessages`), and the words built from them: a
85
+ session's label and status (`sessionLabel`, `sessionStateText`), why a
86
+ machine can't be reached (`machineHint`), what the composer says while it is
87
+ locked (`composerState`), and the transcript's own lines
88
+ (`useTranscriptWords`, `useGoalNotice`, `useCompactionNotice`,
89
+ `useRecapNotice`, `useToolStrings`).
90
+ - How long ago something happened, as "5m", for labels passed as strings
91
+ (`useCompactAgo`). It reads `@parallelworks/ui`'s `time` strings, which a
92
+ host sets on `UIProvider`, or on `UIStringsProvider` where there is no DOM,
93
+ and re-reads every 30 seconds on the ui package's clock.
94
+
95
+ ## Messages
96
+
97
+ The messages are use-intl catalogs, keyed by namespace (`agents`,
98
+ `agentTranscript`). Load them for the reader's language and merge them into the
99
+ host's own, wherever its `IntlProvider` takes messages:
100
+
101
+ ```tsx
102
+ import { loadPwCodeMessages, pwCodeLocales } from '@parallelworks/code'
103
+ import { IntlProvider } from 'use-intl'
104
+
105
+ // One of pwCodeLocales; any other locale rejects.
106
+ const code = await loadPwCodeMessages(locale)
107
+
108
+ root.render(
109
+ <IntlProvider locale={locale} messages={{ ...hostMessages, ...code }}>
110
+ <App />
111
+ </IntlProvider>
112
+ )
113
+ ```
114
+
115
+ Each language is its own import, so a bundler that splits code loads only the
116
+ one shown. The JSON files themselves ship too, as
117
+ `@parallelworks/code/locales/<lang>/<namespace>.json`.
118
+
119
+ The functions that word a session take a translator over the `agents`
120
+ namespace. `usePwCodeTranslations('agents')` gives one typed from these
121
+ catalogs, whatever the host declares to use-intl; outside React, use-intl's
122
+ `createTranslator` makes one:
123
+
124
+ ```ts
125
+ import { createTranslator } from 'use-intl'
126
+ import { loadPwCodeMessages, sessionStatusText } from '@parallelworks/code'
127
+
128
+ const t = createTranslator({
129
+ locale: 'es',
130
+ messages: await loadPwCodeMessages('es'),
131
+ namespace: 'agents',
132
+ })
133
+ sessionStatusText({ status: 'awaiting_approval' }, t) // "aprobación necesaria"
134
+ ```
135
+
136
+ A host that types its own messages for use-intl adds `PwCodeMessages` to them,
137
+ so its `useTranslations('agents')` checks keys too.
138
+
139
+ ## The API
140
+
141
+ ```ts
142
+ import { createPwCodeApi } from '@parallelworks/code'
143
+ import createClient from 'openapi-fetch'
144
+ import type { paths } from './api' // the host's own types of the platform's API
145
+
146
+ const client = createClient<paths>({ baseUrl: 'https://activate.example.com' })
147
+ export const pwCode = createPwCodeApi(client, {
148
+ // The event feed's socket, opened on the platform's host and signed in as
149
+ // the client is. It may resolve asynchronously, after fetching a token.
150
+ openSocket: path => new WebSocket(`wss://activate.example.com${path}`),
151
+ })
152
+ ```
153
+
154
+ Create it once per client, at module level. Its hooks key SWR's cache as
155
+ `@parallelworks/client/swr`'s hooks do (`['api', path, init]`, or `null` for a
156
+ read they skip), and throw the client's error as those do, so a host that reads
157
+ the same routes through those shares their cache entries, and revalidating one
158
+ revalidates the other. `hidden`, when given, says the app is out of sight,
159
+ where animation frames do not run and feed frames are applied as they arrive.
160
+
161
+ The package carries the types of the routes it calls (`PwCodePaths`), generated
162
+ from the platform's OpenAPI spec. A client whose own API types lack one of
163
+ those routes, or type one differently, is refused at compile time, naming the
164
+ routes it lacks; regenerate the host's types from a newer spec.
35
165
 
36
166
  ## Using the hook
37
167
 
38
168
  ```tsx
39
169
  import {
170
+ pasteRefs,
40
171
  pendingMessages,
41
172
  turnRunning,
42
173
  usePwCodeTranscript,
43
174
  } from '@parallelworks/code'
175
+ import { pwCode } from './pwCode'
44
176
 
45
- function useSession(sessionId: string) {
46
- // The host's own fetch of the session: its status, history and last seq.
47
- const { detail, refetch } = useSessionDetail(sessionId)
177
+ function useSession(machine: string, sessionId: string) {
178
+ // The session's status, history and last seq.
179
+ const { data: detail, mutate } = pwCode.useSessionDetail(
180
+ machine,
181
+ sessionId,
182
+ undefined
183
+ )
48
184
  const { transcript, onEvent, cursor, sendMessage } = usePwCodeTranscript({
49
185
  sessionId,
50
186
  detail,
51
187
  // Resolves to { turnId, queued } once accepted, or { error } to show.
52
- send: (text, pastes, images) =>
53
- postMessage(sessionId, text, pastes, images),
188
+ send: async (text, pastes, images) => {
189
+ const { data, error } = await pwCode.sendMessage(machine, sessionId, {
190
+ text,
191
+ ...pasteRefs(pastes),
192
+ ...(images ? { images } : {}),
193
+ })
194
+ return error ? { error: error.message } : data
195
+ },
54
196
  onEffect: effect => {
55
197
  // What a frame asks of the host: 'refetch', 'cleared', 'state' or 'notice'.
56
198
  if (effect.type === 'refetch') {
57
- refetch()
199
+ void mutate()
58
200
  }
59
201
  },
60
202
  })
61
- // The host's own socket: it reconnects from cursor(), the last seq applied.
62
- useSessionEvents(sessionId, cursor, onEvent)
203
+ // The feed reconnects from cursor(), the last seq applied.
204
+ pwCode.useSessionEvents(machine, sessionId, cursor, onEvent)
63
205
 
64
206
  return {
65
207
  messages: transcript?.messages ?? [],
@@ -74,17 +216,22 @@ function useSession(sessionId: string) {
74
216
  `steer` hands text to the turn that is running and `withdraw` takes a queued
75
217
  message back; without them every message sent during a turn waits for it to
76
218
  end. `goalNotice`, `compactionNotice`, `recapNotice` and `words` supply the
77
- transcript's own lines in the reader's language, and `imageUrl` turns an
78
- image's `ref` into the URL the host serves it from.
219
+ transcript's own lines in the reader's language: `useGoalNotice()`,
220
+ `useCompactionNotice()`, `useRecapNotice()` and `useTranscriptWords()` give
221
+ them from this package's messages. `imageUrl` turns an image's `ref` into the URL the host serves it
222
+ from.
79
223
 
80
224
  On the web, `SessionView` from `@parallelworks/ui/ai` draws these messages. A
81
225
  host with its own components renders `ChatMessage` and its parts itself.
82
226
 
83
227
  ## No DOM
84
228
 
85
- The build imports `react`, `luxon` and `@parallelworks/ui/ai/headless`, and
86
- nothing else, so it loads where there is no DOM: a React Native bundle, a
87
- server, a test. The reducers need no React at all.
229
+ The build imports `react`, `luxon`, `swr`, `use-intl`, `gemoji` (for the
230
+ emoji shortcodes in a session's name), `@parallelworks/ui/ai/headless` and
231
+ `@parallelworks/ui/headless`, and its declarations `openapi-fetch` too, and
232
+ nothing else, so it loads where there
233
+ is no DOM: a React Native bundle, a server, a test. It opens sockets only
234
+ through the host's `openSocket`. The reducers need no React at all.
88
235
 
89
236
  ## Versions
90
237
 
package/dist/ago.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /** "5m" since a time, in `@parallelworks/ui`'s `time` strings as a compact
2
+ * RelativeTime reads, for labels built into sentences and passed as strings.
3
+ * It re-reads every 30 seconds. */
4
+ export declare function useCompactAgo(): {
5
+ now: number;
6
+ compact: (iso: string | undefined) => string;
7
+ compactMinutes: (minutes: number) => string;
8
+ };
package/dist/ago.js ADDED
@@ -0,0 +1,26 @@
1
+ import { useCallback, useMemo } from "react";
2
+ import { DateTime } from "luxon";
3
+ import { useClock, useStrings } from "@parallelworks/ui/headless";
4
+ // src/ago.ts
5
+ /** "5m" since a time, in `@parallelworks/ui`'s `time` strings as a compact
6
+ * RelativeTime reads, for labels built into sentences and passed as strings.
7
+ * It re-reads every 30 seconds. */
8
+ function useCompactAgo() {
9
+ const t = useStrings().time;
10
+ const tick = useClock((current) => Math.floor(current / 3e4));
11
+ const now = useMemo(() => DateTime.now().toMillis(), [tick]);
12
+ const compactMinutes = useCallback((minutes) => {
13
+ if (!Number.isFinite(minutes) || minutes < 1) return t.now;
14
+ if (minutes < 60) return t.minutes(minutes);
15
+ const hours = Math.floor(minutes / 60);
16
+ if (hours < 24) return t.hours(hours);
17
+ const days = Math.floor(hours / 24);
18
+ return days < 365 ? t.days(days) : t.years(Math.floor(days / 365));
19
+ }, [t]);
20
+ return {
21
+ now,
22
+ compact: useCallback((iso) => iso ? compactMinutes(Math.floor((now - DateTime.fromISO(iso).toMillis()) / 6e4)) : "", [now, compactMinutes]),
23
+ compactMinutes
24
+ };
25
+ }
26
+ export { useCompactAgo };