@parallelworks/code 0.1.0 → 0.2.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 (70) hide show
  1. package/README.md +138 -24
  2. package/dist/api.d.ts +3469 -0
  3. package/dist/apiClient.d.ts +26 -0
  4. package/dist/apiClient.js +5 -0
  5. package/dist/claudeApi.d.ts +27 -0
  6. package/dist/claudeApi.js +250 -0
  7. package/dist/command.d.ts +47 -0
  8. package/dist/command.js +64 -0
  9. package/dist/commandsApi.d.ts +64 -0
  10. package/dist/commandsApi.js +267 -0
  11. package/dist/createPwCodeApi.d.ts +688 -0
  12. package/dist/createPwCodeApi.js +31 -0
  13. package/dist/index.d.ts +16 -3
  14. package/dist/index.js +16 -3
  15. package/dist/locales/en/agentTranscript.js +170 -0
  16. package/dist/locales/en/agents.js +380 -0
  17. package/dist/locales/en.d.ts +531 -0
  18. package/dist/locales/en.js +8 -0
  19. package/dist/locales/es/agentTranscript.js +170 -0
  20. package/dist/locales/es/agents.js +380 -0
  21. package/dist/locales/es.d.ts +3 -0
  22. package/dist/locales/es.js +8 -0
  23. package/dist/locales/ja/agentTranscript.js +170 -0
  24. package/dist/locales/ja/agents.js +380 -0
  25. package/dist/locales/ja.d.ts +3 -0
  26. package/dist/locales/ja.js +8 -0
  27. package/dist/locales/ko/agentTranscript.js +170 -0
  28. package/dist/locales/ko/agents.js +380 -0
  29. package/dist/locales/ko.d.ts +3 -0
  30. package/dist/locales/ko.js +8 -0
  31. package/dist/locales/zh/agentTranscript.js +170 -0
  32. package/dist/locales/zh/agents.js +380 -0
  33. package/dist/locales/zh.d.ts +3 -0
  34. package/dist/locales/zh.js +8 -0
  35. package/dist/machine.d.ts +49 -0
  36. package/dist/machine.js +60 -0
  37. package/dist/messages.d.ts +17 -0
  38. package/dist/messages.js +31 -0
  39. package/dist/notices.d.ts +11 -0
  40. package/dist/notices.js +94 -0
  41. package/dist/pullRequest.d.ts +8 -0
  42. package/dist/pullRequest.js +29 -0
  43. package/dist/query.d.ts +28 -0
  44. package/dist/query.js +25 -0
  45. package/dist/session.d.ts +70 -1
  46. package/dist/session.js +101 -1
  47. package/dist/sessionEvents.d.ts +27 -0
  48. package/dist/sessionEvents.js +212 -0
  49. package/dist/sessionsApi.d.ts +105 -0
  50. package/dist/sessionsApi.js +513 -0
  51. package/dist/toolStrings.d.ts +4 -0
  52. package/dist/toolStrings.js +95 -0
  53. package/dist/transcript.d.ts +12 -1
  54. package/dist/transcript.js +40 -2
  55. package/dist/types.d.ts +20 -1
  56. package/dist/useTranscript.d.ts +4 -1
  57. package/dist/useTranscript.js +54 -11
  58. package/dist/wording.d.ts +43 -0
  59. package/dist/wording.js +101 -0
  60. package/locales/en/agentTranscript.json +162 -0
  61. package/locales/en/agents.json +366 -0
  62. package/locales/es/agentTranscript.json +162 -0
  63. package/locales/es/agents.json +366 -0
  64. package/locales/ja/agentTranscript.json +162 -0
  65. package/locales/ja/agents.json +366 -0
  66. package/locales/ko/agentTranscript.json +162 -0
  67. package/locales/ko/agents.json +366 -0
  68. package/locales/zh/agentTranscript.json +162 -0
  69. package/locales/zh/agents.json +366 -0
  70. package/package.json +23 -8
package/README.md CHANGED
@@ -1,24 +1,32 @@
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 `@parallelworks/ui/ai/headless` is imported from the ui
19
+ package, for the message and part types and the reducers that build them.
19
20
 
20
21
  ## What is in it
21
22
 
23
+ - `createPwCodeApi(client, options)`, the calls and SWR data hooks of pw code
24
+ sessions over the host's `openapi-fetch` client: the sessions and machines
25
+ listed (`useSessions`, `useMachines`), a session's transcript
26
+ (`useSessionDetail`), its live event feed (`useSessionEvents`), what can be
27
+ done to it (`useSessionMutations`, `sendMessage`, `steer`, `answerApproval`,
28
+ `resolveTyped`, ...), its subagents, pull requests and worktree, and the
29
+ Claude Code models, accounts and sign-in of its machine.
22
30
  - The wire types (`PwCodeSessionDetail`, `PwCodeStreamEvent`,
23
31
  `PwCodeApprovalRequest`, ...): a session's replayed history, its event
24
32
  frames and the answers it accepts. A host that generates its API types can
@@ -32,34 +40,136 @@ message and part types and the reducers that build them.
32
40
  requests and the answer it accepts.
33
41
  - The paths a session's images are served from (`sessionImageUrl`,
34
42
  `sessionGitHubImageUrl`).
43
+ - What a list of sessions and machines means, as pure functions over the
44
+ structural `PwCodeSession` and `PwCodeMachine`: which sessions wait on the
45
+ reader and in what order they show (`triageSessions`, `newestFirst`,
46
+ `needsYou`, `isActiveSession`, `isMidTurn`), the tone a row takes
47
+ (`sessionVariant`), whether a machine can be reached or can start a session
48
+ (`isMachineDown`, `canStartHere`), the folders a machine's sessions suggest
49
+ (`recentDirectories`), and the command that picks a session up in a terminal
50
+ (`terminalHandoff`).
51
+ - Where typed text goes (`slashAction`, `shellCommand`) and the commands a
52
+ session offers (`paletteOptions`), read from the structural `PwCodePalette`.
53
+ - The short label of a pull or merge request link (`pullRequestLabel`,
54
+ `pastedPullRequestLabel`).
55
+ - The messages a session is shown in, in English, Spanish, Japanese, Korean
56
+ and Chinese (`loadPwCodeMessages`), and the words built from them: a
57
+ session's label and status (`sessionLabel`, `sessionStateText`), why a
58
+ machine can't be reached (`machineHint`), what the composer says while it is
59
+ locked (`composerState`), and the transcript's own lines
60
+ (`useTranscriptWords`, `useCompactionNotice`, `useRecapNotice`,
61
+ `useToolStrings`).
62
+
63
+ ## Messages
64
+
65
+ The messages are use-intl catalogs, keyed by namespace (`agents`,
66
+ `agentTranscript`). Load them for the reader's language and merge them into the
67
+ host's own, wherever its `IntlProvider` takes messages:
68
+
69
+ ```tsx
70
+ import { loadPwCodeMessages, pwCodeLocales } from '@parallelworks/code'
71
+ import { IntlProvider } from 'use-intl'
72
+
73
+ // One of pwCodeLocales; any other locale rejects.
74
+ const code = await loadPwCodeMessages(locale)
75
+
76
+ root.render(
77
+ <IntlProvider locale={locale} messages={{ ...hostMessages, ...code }}>
78
+ <App />
79
+ </IntlProvider>
80
+ )
81
+ ```
82
+
83
+ Each language is its own import, so a bundler that splits code loads only the
84
+ one shown. The JSON files themselves ship too, as
85
+ `@parallelworks/code/locales/<lang>/<namespace>.json`.
86
+
87
+ The functions that word a session take a translator over the `agents`
88
+ namespace. `usePwCodeTranslations('agents')` gives one typed from these
89
+ catalogs, whatever the host declares to use-intl; outside React, use-intl's
90
+ `createTranslator` makes one:
91
+
92
+ ```ts
93
+ import { createTranslator } from 'use-intl'
94
+ import { loadPwCodeMessages, sessionStatusText } from '@parallelworks/code'
95
+
96
+ const t = createTranslator({
97
+ locale: 'es',
98
+ messages: await loadPwCodeMessages('es'),
99
+ namespace: 'agents',
100
+ })
101
+ sessionStatusText({ status: 'awaiting_approval' }, t) // "aprobación necesaria"
102
+ ```
103
+
104
+ A host that types its own messages for use-intl adds `PwCodeMessages` to them,
105
+ so its `useTranslations('agents')` checks keys too.
106
+
107
+ ## The API
108
+
109
+ ```ts
110
+ import { createPwCodeApi } from '@parallelworks/code'
111
+ import createClient from 'openapi-fetch'
112
+ import type { paths } from './api' // the host's own types of the platform's API
113
+
114
+ const client = createClient<paths>({ baseUrl: 'https://activate.example.com' })
115
+ export const pwCode = createPwCodeApi(client, {
116
+ // The event feed's socket, opened on the platform's host and signed in as
117
+ // the client is. It may resolve asynchronously, after fetching a token.
118
+ openSocket: path => new WebSocket(`wss://activate.example.com${path}`),
119
+ })
120
+ ```
121
+
122
+ Create it once per client, at module level. Its hooks key SWR's cache as
123
+ `@parallelworks/client/swr`'s hooks do (`['api', path, init]`, or `null` for a
124
+ read they skip), and throw the client's error as those do, so a host that reads
125
+ the same routes through those shares their cache entries, and revalidating one
126
+ revalidates the other. `hidden`, when given, says the app is out of sight,
127
+ where animation frames do not run and feed frames are applied as they arrive.
128
+
129
+ The package carries the types of the routes it calls (`PwCodePaths`), generated
130
+ from the platform's OpenAPI spec. A client whose own API types lack one of
131
+ those routes, or type one differently, is refused at compile time, naming the
132
+ routes it lacks; regenerate the host's types from a newer spec.
35
133
 
36
134
  ## Using the hook
37
135
 
38
136
  ```tsx
39
137
  import {
138
+ pasteRefs,
40
139
  pendingMessages,
41
140
  turnRunning,
42
141
  usePwCodeTranscript,
43
142
  } from '@parallelworks/code'
143
+ import { pwCode } from './pwCode'
44
144
 
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)
145
+ function useSession(machine: string, sessionId: string) {
146
+ // The session's status, history and last seq.
147
+ const { data: detail, mutate } = pwCode.useSessionDetail(
148
+ machine,
149
+ sessionId,
150
+ undefined
151
+ )
48
152
  const { transcript, onEvent, cursor, sendMessage } = usePwCodeTranscript({
49
153
  sessionId,
50
154
  detail,
51
155
  // Resolves to { turnId, queued } once accepted, or { error } to show.
52
- send: (text, pastes, images) =>
53
- postMessage(sessionId, text, pastes, images),
156
+ send: async (text, pastes, images) => {
157
+ const { data, error } = await pwCode.sendMessage(machine, sessionId, {
158
+ text,
159
+ ...pasteRefs(pastes),
160
+ ...(images ? { images } : {}),
161
+ })
162
+ return error ? { error: error.message } : data
163
+ },
54
164
  onEffect: effect => {
55
165
  // What a frame asks of the host: 'refetch', 'cleared', 'state' or 'notice'.
56
166
  if (effect.type === 'refetch') {
57
- refetch()
167
+ void mutate()
58
168
  }
59
169
  },
60
170
  })
61
- // The host's own socket: it reconnects from cursor(), the last seq applied.
62
- useSessionEvents(sessionId, cursor, onEvent)
171
+ // The feed reconnects from cursor(), the last seq applied.
172
+ pwCode.useSessionEvents(machine, sessionId, cursor, onEvent)
63
173
 
64
174
  return {
65
175
  messages: transcript?.messages ?? [],
@@ -74,17 +184,21 @@ function useSession(sessionId: string) {
74
184
  `steer` hands text to the turn that is running and `withdraw` takes a queued
75
185
  message back; without them every message sent during a turn waits for it to
76
186
  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.
187
+ transcript's own lines in the reader's language: `useCompactionNotice()`,
188
+ `useRecapNotice()` and `useTranscriptWords()` give them from this package's
189
+ messages. `imageUrl` turns an image's `ref` into the URL the host serves it
190
+ from.
79
191
 
80
192
  On the web, `SessionView` from `@parallelworks/ui/ai` draws these messages. A
81
193
  host with its own components renders `ChatMessage` and its parts itself.
82
194
 
83
195
  ## No DOM
84
196
 
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.
197
+ The build imports `react`, `luxon`, `swr`, `use-intl`, `gemoji` (for the
198
+ emoji shortcodes in a session's name) and `@parallelworks/ui/ai/headless`, and
199
+ its declarations `openapi-fetch` too, and nothing else, so it loads where there
200
+ is no DOM: a React Native bundle, a server, a test. It opens sockets only
201
+ through the host's `openSocket`. The reducers need no React at all.
88
202
 
89
203
  ## Versions
90
204