@parallelworks/code 0.2.0 → 0.4.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.
- package/README.md +602 -84
- package/dist/accountApi.d.ts +31 -0
- package/dist/accountApi.js +182 -0
- package/dist/agentSwitcher.d.ts +109 -0
- package/dist/agentSwitcher.js +165 -0
- package/dist/ago.d.ts +8 -0
- package/dist/ago.js +26 -0
- package/dist/allocations.d.ts +41 -0
- package/dist/allocations.js +76 -0
- package/dist/api.d.ts +1213 -66
- package/dist/base64.d.ts +3 -0
- package/dist/base64.js +15 -0
- package/dist/chatHandoff.d.ts +12 -0
- package/dist/chatHandoff.js +27 -0
- package/dist/chatStrings.d.ts +188 -0
- package/dist/chatStrings.js +217 -0
- package/dist/claudeAccounts.d.ts +149 -0
- package/dist/claudeAccounts.js +328 -0
- package/dist/claudeApi.js +1 -1
- package/dist/claudeSignIn.d.ts +80 -0
- package/dist/claudeSignIn.js +249 -0
- package/dist/commandsApi.d.ts +11 -1
- package/dist/commandsApi.js +24 -5
- package/dist/contextRow.d.ts +25 -0
- package/dist/contextRow.js +16 -0
- package/dist/createPwCodeApi.d.ts +52 -5
- package/dist/createPwCodeApi.js +20 -2
- package/dist/directories.d.ts +161 -0
- package/dist/directories.js +301 -0
- package/dist/goal.d.ts +89 -0
- package/dist/goal.js +164 -0
- package/dist/howSessionsAppear.d.ts +12 -0
- package/dist/howSessionsAppear.js +24 -0
- package/dist/imageFit.d.ts +23 -0
- package/dist/imageFit.js +27 -0
- package/dist/images.d.ts +28 -0
- package/dist/images.js +38 -6
- package/dist/index.d.ts +41 -3
- package/dist/index.js +40 -4
- package/dist/loadError.d.ts +8 -0
- package/dist/loadError.js +19 -0
- package/dist/locales/en/agentTranscript.js +75 -22
- package/dist/locales/en/agents.js +76 -14
- package/dist/locales/en.d.ts +129 -21
- package/dist/locales/es/agentTranscript.js +75 -22
- package/dist/locales/es/agents.js +76 -14
- package/dist/locales/ja/agentTranscript.js +78 -25
- package/dist/locales/ja/agents.js +49 -124
- package/dist/locales/ko/agentTranscript.js +75 -22
- package/dist/locales/ko/agents.js +76 -14
- package/dist/locales/zh/agentTranscript.js +73 -20
- package/dist/locales/zh/agents.js +76 -14
- package/dist/machine.d.ts +4 -0
- package/dist/modelPicker.d.ts +79 -0
- package/dist/modelPicker.js +107 -0
- package/dist/models.d.ts +47 -0
- package/dist/models.js +49 -0
- package/dist/notices.d.ts +2 -1
- package/dist/notices.js +75 -21
- package/dist/platformDefaults.d.ts +22 -0
- package/dist/platformDefaults.js +30 -0
- package/dist/preferences.d.ts +78 -0
- package/dist/preferences.js +128 -0
- package/dist/pullRequestList.d.ts +60 -0
- package/dist/pullRequestList.js +125 -0
- package/dist/query.d.ts +6 -2
- package/dist/query.js +85 -10
- package/dist/session.d.ts +10 -2
- package/dist/session.js +12 -8
- package/dist/sessionActions.d.ts +115 -0
- package/dist/sessionActions.js +186 -0
- package/dist/sessionEvents.d.ts +8 -3
- package/dist/sessionEvents.js +10 -5
- package/dist/sessionGroups.d.ts +50 -0
- package/dist/sessionGroups.js +73 -0
- package/dist/sessionHeader.d.ts +14 -0
- package/dist/sessionHeader.js +16 -0
- package/dist/sessionList.d.ts +182 -0
- package/dist/sessionList.js +333 -0
- package/dist/sessionScreen.d.ts +193 -0
- package/dist/sessionScreen.js +213 -0
- package/dist/sessionsApi.d.ts +13 -3
- package/dist/sessionsApi.js +90 -33
- package/dist/settingsApi.d.ts +38 -0
- package/dist/settingsApi.js +151 -0
- package/dist/startPage.d.ts +153 -0
- package/dist/startPage.js +174 -0
- package/dist/startSession.d.ts +172 -0
- package/dist/startSession.js +256 -0
- package/dist/terminalHandoffPanel.d.ts +30 -0
- package/dist/terminalHandoffPanel.js +33 -0
- package/dist/transcript.d.ts +2 -0
- package/dist/transcript.js +62 -26
- package/dist/types.d.ts +11 -1
- package/dist/usageLimit.d.ts +8 -0
- package/dist/usageLimit.js +13 -0
- package/dist/useAwayRecap.d.ts +30 -0
- package/dist/useAwayRecap.js +75 -0
- package/dist/useComposer.d.ts +145 -0
- package/dist/useComposer.js +295 -0
- package/dist/useComposerImages.d.ts +71 -0
- package/dist/useComposerImages.js +218 -0
- package/dist/useComposerPastes.d.ts +70 -0
- package/dist/useComposerPastes.js +130 -0
- package/dist/useConversation.d.ts +199 -0
- package/dist/useConversation.js +314 -0
- package/dist/useSubagent.d.ts +104 -0
- package/dist/useSubagent.js +212 -0
- package/dist/useTranscript.d.ts +23 -3
- package/dist/useTranscript.js +76 -19
- package/dist/wording.js +2 -2
- package/dist/workingDirectories.d.ts +63 -0
- package/dist/workingDirectories.js +95 -0
- package/dist/worktreeChoice.d.ts +31 -0
- package/dist/worktreeChoice.js +38 -0
- package/locales/en/agentTranscript.json +69 -21
- package/locales/en/agents.json +73 -13
- package/locales/es/agentTranscript.json +69 -21
- package/locales/es/agents.json +73 -13
- package/locales/ja/agentTranscript.json +72 -24
- package/locales/ja/agents.json +73 -13
- package/locales/ko/agentTranscript.json +69 -21
- package/locales/ko/agents.json +73 -13
- package/locales/zh/agentTranscript.json +67 -19
- package/locales/zh/agents.json +73 -13
- package/package.json +6 -3
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
|
+
import type { PwCodeApiError, PwCodeClient, PwCodeQuery, Schemas } from './apiClient.js';
|
|
3
|
+
/** The preview that turns pw code on, for the reader or the whole platform. */
|
|
4
|
+
export declare const PW_CODE_PREVIEW = "agents";
|
|
5
|
+
/** Whether pw code is on for the reader: its preview is, for them or for the
|
|
6
|
+
* whole platform. Its routes answer 404 otherwise. */
|
|
7
|
+
export declare function pwCodeEnabled(previews: readonly Pick<Schemas['Preview'], 'preview'>[] | null | undefined): boolean;
|
|
8
|
+
/** The platform's models, as the gateway offers them to the reader. */
|
|
9
|
+
export interface PwCodePlatformModels {
|
|
10
|
+
/** Empty until they load. */
|
|
11
|
+
models: Schemas['ModelEntry'][];
|
|
12
|
+
/** The connections that cannot serve their models, such as one whose key
|
|
13
|
+
* is rejected. */
|
|
14
|
+
providerIssues: Schemas['ProviderIssue'][];
|
|
15
|
+
/** The list has answered, or failed to. */
|
|
16
|
+
loaded: boolean;
|
|
17
|
+
isLoading: boolean;
|
|
18
|
+
error: PwCodeApiError | undefined;
|
|
19
|
+
/** Asks for the list again. */
|
|
20
|
+
refresh: () => void;
|
|
21
|
+
}
|
|
22
|
+
export interface PwCodeAccountOptions {
|
|
23
|
+
/** Called when a preference failed to save; it shows its saved value again. */
|
|
24
|
+
onPreferenceError?: ((error: unknown) => void) | undefined;
|
|
25
|
+
}
|
|
26
|
+
export declare function accountApi(client: PwCodeClient, { onPreferenceError }?: PwCodeAccountOptions): {
|
|
27
|
+
usePlatformModels: () => PwCodePlatformModels;
|
|
28
|
+
useAllocations: (read?: boolean) => PwCodeQuery<Schemas['Allocation'][] | null>;
|
|
29
|
+
usePwCodeEnabled: () => boolean | undefined;
|
|
30
|
+
usePreference: <T>(key: string, fallback: T) => [T, Dispatch<SetStateAction<T>>];
|
|
31
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { preferenceKey } from "./preferences.js";
|
|
2
|
+
import { createQuery } from "./query.js";
|
|
3
|
+
import { useCallback, useEffect, useRef, useSyncExternalStore } from "react";
|
|
4
|
+
import useSWR from "swr";
|
|
5
|
+
// src/accountApi.ts
|
|
6
|
+
/** The preview that turns pw code on, for the reader or the whole platform. */
|
|
7
|
+
var PW_CODE_PREVIEW = "agents";
|
|
8
|
+
/** Whether pw code is on for the reader: its preview is, for them or for the
|
|
9
|
+
* whole platform. Its routes answer 404 otherwise. */
|
|
10
|
+
function pwCodeEnabled(previews) {
|
|
11
|
+
return previews?.some((p) => p.preview === "agents") ?? false;
|
|
12
|
+
}
|
|
13
|
+
var WRITE_DELAY_MS = 500;
|
|
14
|
+
var READ_RETRY_MS = 5e3;
|
|
15
|
+
var PREFERENCES_INIT = {};
|
|
16
|
+
function accountApi(client, { onPreferenceError } = {}) {
|
|
17
|
+
const useQuery = createQuery(client);
|
|
18
|
+
function usePlatformModels() {
|
|
19
|
+
const { data, error, isLoading, mutate } = useQuery("/api/openai/v1/models", void 0);
|
|
20
|
+
const refresh = useCallback(() => void mutate(), [mutate]);
|
|
21
|
+
return {
|
|
22
|
+
models: data?.data ?? [],
|
|
23
|
+
providerIssues: data?.provider_issues ?? [],
|
|
24
|
+
loaded: data !== void 0 || error !== void 0,
|
|
25
|
+
isLoading,
|
|
26
|
+
error,
|
|
27
|
+
refresh
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** The reader's allocations, every kind, once `read`. */
|
|
31
|
+
function useAllocations(read = true) {
|
|
32
|
+
return useQuery("/api/allocations", read ? void 0 : null);
|
|
33
|
+
}
|
|
34
|
+
const readPreviews = async () => {
|
|
35
|
+
const { data, error } = await client.GET("/api/previews");
|
|
36
|
+
if (error) throw error;
|
|
37
|
+
return data;
|
|
38
|
+
};
|
|
39
|
+
/** Whether pw code is on for the reader; undefined until the previews load. */
|
|
40
|
+
function usePwCodeEnabled() {
|
|
41
|
+
const { data } = useSWR("/api/previews", readPreviews);
|
|
42
|
+
return data === void 0 ? void 0 : pwCodeEnabled(data);
|
|
43
|
+
}
|
|
44
|
+
const unsaved = /* @__PURE__ */ new Map();
|
|
45
|
+
const waiting = /* @__PURE__ */ new Map();
|
|
46
|
+
const pending = /* @__PURE__ */ new Map();
|
|
47
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
48
|
+
let revision = 0;
|
|
49
|
+
let flushTimer;
|
|
50
|
+
let retryTimer;
|
|
51
|
+
let writing = false;
|
|
52
|
+
let mutateCopy;
|
|
53
|
+
function changed() {
|
|
54
|
+
revision += 1;
|
|
55
|
+
for (const listener of listeners) listener();
|
|
56
|
+
}
|
|
57
|
+
function subscribe(listener) {
|
|
58
|
+
listeners.add(listener);
|
|
59
|
+
return () => {
|
|
60
|
+
listeners.delete(listener);
|
|
61
|
+
if (listeners.size === 0) stopRetry();
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function current(key, copy, fallback) {
|
|
65
|
+
if (unsaved.has(key)) return unsaved.get(key);
|
|
66
|
+
const saved = copy?.preferences[key];
|
|
67
|
+
return saved === void 0 ? fallback : saved;
|
|
68
|
+
}
|
|
69
|
+
function settled(batch) {
|
|
70
|
+
for (const [key, value] of Object.entries(batch)) if (!pending.has(key) && unsaved.get(key) === value) unsaved.delete(key);
|
|
71
|
+
changed();
|
|
72
|
+
}
|
|
73
|
+
async function flush() {
|
|
74
|
+
flushTimer = void 0;
|
|
75
|
+
if (pending.size === 0 || writing) return;
|
|
76
|
+
const batch = Object.fromEntries(pending);
|
|
77
|
+
pending.clear();
|
|
78
|
+
writing = true;
|
|
79
|
+
try {
|
|
80
|
+
const { data, error } = await client.PATCH("/api/user/preferences", { body: { preferences: batch } });
|
|
81
|
+
if (error) {
|
|
82
|
+
settled(batch);
|
|
83
|
+
onPreferenceError?.(error);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
await mutateCopy?.(data, { revalidate: false });
|
|
87
|
+
settled(batch);
|
|
88
|
+
} catch (thrown) {
|
|
89
|
+
settled(batch);
|
|
90
|
+
onPreferenceError?.(thrown);
|
|
91
|
+
} finally {
|
|
92
|
+
writing = false;
|
|
93
|
+
if (pending.size > 0 && !flushTimer) flushTimer = setTimeout(flush, 0);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function save(key, value) {
|
|
97
|
+
unsaved.set(key, value);
|
|
98
|
+
pending.set(key, value);
|
|
99
|
+
if (flushTimer) clearTimeout(flushTimer);
|
|
100
|
+
flushTimer = setTimeout(flush, WRITE_DELAY_MS);
|
|
101
|
+
}
|
|
102
|
+
function applyWaiting(copy) {
|
|
103
|
+
stopRetry();
|
|
104
|
+
if (waiting.size === 0) return;
|
|
105
|
+
for (const [key, { fallback, updates }] of waiting) save(key, updates.reduce((previous, update) => update(previous), current(key, copy, fallback)));
|
|
106
|
+
waiting.clear();
|
|
107
|
+
changed();
|
|
108
|
+
}
|
|
109
|
+
function retryRead(read) {
|
|
110
|
+
if (retryTimer) return;
|
|
111
|
+
retryTimer = setTimeout(() => {
|
|
112
|
+
retryTimer = void 0;
|
|
113
|
+
if (waiting.size > 0) read().catch(() => {});
|
|
114
|
+
}, READ_RETRY_MS);
|
|
115
|
+
}
|
|
116
|
+
function stopRetry() {
|
|
117
|
+
clearTimeout(retryTimer);
|
|
118
|
+
retryTimer = void 0;
|
|
119
|
+
}
|
|
120
|
+
function hold(key, update, fallback) {
|
|
121
|
+
const held = waiting.get(key) ?? {
|
|
122
|
+
fallback,
|
|
123
|
+
updates: []
|
|
124
|
+
};
|
|
125
|
+
waiting.set(key, {
|
|
126
|
+
...held,
|
|
127
|
+
updates: [...held.updates, update]
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function update(key, step, fallback, copy) {
|
|
131
|
+
if (!copy && !unsaved.has(key)) {
|
|
132
|
+
hold(key, step, fallback);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
save(key, step(current(key, copy, fallback)));
|
|
136
|
+
}
|
|
137
|
+
function change(key, next, fallback, copy, mutate) {
|
|
138
|
+
mutateCopy = mutate;
|
|
139
|
+
if (copy) applyWaiting(copy);
|
|
140
|
+
if (typeof next === "function") update(key, next, fallback, copy);
|
|
141
|
+
else {
|
|
142
|
+
waiting.delete(key);
|
|
143
|
+
save(key, next);
|
|
144
|
+
}
|
|
145
|
+
changed();
|
|
146
|
+
}
|
|
147
|
+
/** A choice kept in the reader's account, as `useState` returns it: the
|
|
148
|
+
* value saved under `key`, or `fallback` until one is, and a setter that
|
|
149
|
+
* shows the change at once and saves it shortly after. An update computed
|
|
150
|
+
* from the previous value waits for the account's copy, and is shown and
|
|
151
|
+
* saved once it loads; a read that failed is retried meanwhile. A key
|
|
152
|
+
* with a dot is saved with a colon in its place, which the account
|
|
153
|
+
* requires. */
|
|
154
|
+
function usePreference(key, fallback) {
|
|
155
|
+
const accountKey = preferenceKey(key);
|
|
156
|
+
const { data, error, mutate } = useQuery("/api/user/preferences", PREFERENCES_INIT);
|
|
157
|
+
useSyncExternalStore(subscribe, () => revision);
|
|
158
|
+
const copy = useRef(data);
|
|
159
|
+
copy.current = data;
|
|
160
|
+
const start = useRef(fallback);
|
|
161
|
+
start.current = fallback;
|
|
162
|
+
const holding = waiting.size > 0;
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
if (data) applyWaiting(data);
|
|
165
|
+
else if (error && holding) retryRead(mutate);
|
|
166
|
+
}, [
|
|
167
|
+
data,
|
|
168
|
+
error,
|
|
169
|
+
holding,
|
|
170
|
+
mutate
|
|
171
|
+
]);
|
|
172
|
+
const set = useCallback((next) => change(accountKey, next, start.current, copy.current, mutate), [accountKey, mutate]);
|
|
173
|
+
return [current(accountKey, data, fallback), set];
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
usePlatformModels,
|
|
177
|
+
useAllocations,
|
|
178
|
+
usePwCodeEnabled,
|
|
179
|
+
usePreference
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
export { PW_CODE_PREVIEW, accountApi, pwCodeEnabled };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Schemas } from './apiClient.js';
|
|
2
|
+
import type { PwCodeSubagentUsage } from './types.js';
|
|
3
|
+
import type { PwCodeConversationState } from './useConversation.js';
|
|
4
|
+
/** One subagent as the switcher lists it. */
|
|
5
|
+
export interface PwCodeSwitcherAgent {
|
|
6
|
+
id: string;
|
|
7
|
+
/** As the daemon names it; unset for a plain task. */
|
|
8
|
+
agentType?: string | undefined;
|
|
9
|
+
description: string;
|
|
10
|
+
status: 'running' | 'done' | 'failed' | 'stopped';
|
|
11
|
+
awaitingApproval: boolean;
|
|
12
|
+
/** Started in the turn under way or the last one, not an earlier one. */
|
|
13
|
+
current: boolean;
|
|
14
|
+
startedAt?: string | undefined;
|
|
15
|
+
endedAt?: string | undefined;
|
|
16
|
+
tokens: number;
|
|
17
|
+
}
|
|
18
|
+
/** `usage` is the feed's latest count, which the listing trails. As in the
|
|
19
|
+
* terminal, the context is counted once, not each request's rereading of it. */
|
|
20
|
+
export declare function switcherAgent(sub: Schemas['Subagent'], usage?: PwCodeSubagentUsage): PwCodeSwitcherAgent;
|
|
21
|
+
/** How a subagent stands: an approval it waits on outranks its status. */
|
|
22
|
+
export type PwCodeAgentState = PwCodeSwitcherAgent['status'] | 'approval';
|
|
23
|
+
/** The tone a state's word is said in, which the host maps to its own colors. */
|
|
24
|
+
export type PwCodeStateTone = 'warning' | 'error' | 'muted';
|
|
25
|
+
/** An agent at work shows a turning mark, as a session does. Any other shows
|
|
26
|
+
* a dot in its state's tone, pulsing while it waits on the reader and unlit
|
|
27
|
+
* once it was stopped or rests. */
|
|
28
|
+
export declare function agentMark(state: PwCodeAgentState | PwCodeConversationState): {
|
|
29
|
+
working: true;
|
|
30
|
+
} | {
|
|
31
|
+
working: false;
|
|
32
|
+
tone: 'warning' | 'error' | 'success' | 'muted';
|
|
33
|
+
pulse: boolean;
|
|
34
|
+
off: boolean;
|
|
35
|
+
};
|
|
36
|
+
/** `explore`, or the transcript's word for a task with no type of its own. */
|
|
37
|
+
export declare function useAgentTypeLabel(): (agentType: string | undefined) => string;
|
|
38
|
+
/** One subagent wherever it is named: its type, what it was asked, and how it stands. */
|
|
39
|
+
export interface PwCodeAgentLine {
|
|
40
|
+
agent: PwCodeSwitcherAgent;
|
|
41
|
+
type: string;
|
|
42
|
+
description: string;
|
|
43
|
+
state: PwCodeAgentState;
|
|
44
|
+
stateWord: string;
|
|
45
|
+
stateTone: PwCodeStateTone;
|
|
46
|
+
/** The state needs the reader, so it is said in words beside its mark. */
|
|
47
|
+
flagged: boolean;
|
|
48
|
+
/** Still running, so it can be stopped. */
|
|
49
|
+
stoppable: boolean;
|
|
50
|
+
/** Names the agent, for a stop button among others. */
|
|
51
|
+
stopLabel: string;
|
|
52
|
+
}
|
|
53
|
+
export declare function usePwCodeAgentLine(): (agent: PwCodeSwitcherAgent) => PwCodeAgentLine;
|
|
54
|
+
/** A subagent's row in the switcher's list. */
|
|
55
|
+
export interface PwCodeSwitcherRow extends PwCodeAgentLine {
|
|
56
|
+
/** How long it has run, or ran; null before it started. */
|
|
57
|
+
elapsed: string | null;
|
|
58
|
+
/** Its tokens, compact; null while it has used none. */
|
|
59
|
+
tokens: string | null;
|
|
60
|
+
viewing: boolean;
|
|
61
|
+
/** From an earlier turn and folded away with the rest of them. */
|
|
62
|
+
folded: boolean;
|
|
63
|
+
}
|
|
64
|
+
/** A count in the switcher's header, and the agent its button opens. */
|
|
65
|
+
export interface PwCodeSwitcherCount {
|
|
66
|
+
count: number;
|
|
67
|
+
label: string;
|
|
68
|
+
first: string;
|
|
69
|
+
}
|
|
70
|
+
export interface PwCodeAgentSwitcher {
|
|
71
|
+
main: {
|
|
72
|
+
state: PwCodeConversationState;
|
|
73
|
+
stateWord: string;
|
|
74
|
+
stateTone: PwCodeStateTone;
|
|
75
|
+
/** Waits on an approval, so it is said in words beside its mark. */
|
|
76
|
+
flagged: boolean;
|
|
77
|
+
viewing: boolean;
|
|
78
|
+
};
|
|
79
|
+
/** This turn's agents, then earlier ones. */
|
|
80
|
+
agents: PwCodeSwitcherRow[];
|
|
81
|
+
/** The row that unfolds the earlier agents; null when there are none. */
|
|
82
|
+
earlier: {
|
|
83
|
+
count: number;
|
|
84
|
+
label: string;
|
|
85
|
+
} | null;
|
|
86
|
+
/** What the turn's agents are doing, in one line. */
|
|
87
|
+
summary: string;
|
|
88
|
+
/** An agent runs without waiting on the reader. */
|
|
89
|
+
working: boolean;
|
|
90
|
+
/** Agents waiting on an approval, from any turn. */
|
|
91
|
+
approvals: PwCodeSwitcherCount | null;
|
|
92
|
+
/** This turn's agents that failed. */
|
|
93
|
+
failed: PwCodeSwitcherCount | null;
|
|
94
|
+
/** Agents still running, approvals pending or not, which stopping all would stop. */
|
|
95
|
+
running: number;
|
|
96
|
+
}
|
|
97
|
+
/** A session's subagents as a switcher shows them, beside its main
|
|
98
|
+
* conversation. While the list is open and an agent runs, the elapsed times
|
|
99
|
+
* tick each second. */
|
|
100
|
+
export declare function usePwCodeAgentSwitcher({ agents, mainState, viewing, open, earlierOpen, }: {
|
|
101
|
+
agents: readonly PwCodeSwitcherAgent[];
|
|
102
|
+
mainState: PwCodeConversationState;
|
|
103
|
+
/** The subagent in view; null for the main conversation. */
|
|
104
|
+
viewing: string | null;
|
|
105
|
+
/** Whether the list is shown. */
|
|
106
|
+
open: boolean;
|
|
107
|
+
/** Whether the earlier turns' agents are unfolded. */
|
|
108
|
+
earlierOpen: boolean;
|
|
109
|
+
}): PwCodeAgentSwitcher;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { usePwCodeTranslations } from "./messages.js";
|
|
2
|
+
import { turnDuration } from "./notices.js";
|
|
3
|
+
import { useCallback, useMemo } from "react";
|
|
4
|
+
import { sanitizeLabel } from "@parallelworks/ui/ai/headless";
|
|
5
|
+
import { useFormatter } from "use-intl";
|
|
6
|
+
import { DateTime } from "luxon";
|
|
7
|
+
import { useClock } from "@parallelworks/ui/headless";
|
|
8
|
+
// src/agentSwitcher.ts
|
|
9
|
+
/** `usage` is the feed's latest count, which the listing trails. As in the
|
|
10
|
+
* terminal, the context is counted once, not each request's rereading of it. */
|
|
11
|
+
function switcherAgent(sub, usage) {
|
|
12
|
+
const used = usage ?? sub.usage;
|
|
13
|
+
return {
|
|
14
|
+
id: sub.id,
|
|
15
|
+
agentType: sub.agentType,
|
|
16
|
+
description: sub.description || sub.id,
|
|
17
|
+
status: sub.status,
|
|
18
|
+
awaitingApproval: sub.awaitingApproval === true,
|
|
19
|
+
current: sub.current === true,
|
|
20
|
+
startedAt: sub.startedAt,
|
|
21
|
+
endedAt: sub.endedAt,
|
|
22
|
+
tokens: (used?.latestInputTokens ?? 0) + (used?.outputTokens ?? 0)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function agentState(agent) {
|
|
26
|
+
return agent.awaitingApproval ? "approval" : agent.status;
|
|
27
|
+
}
|
|
28
|
+
function stateTone(state) {
|
|
29
|
+
return state === "approval" ? "warning" : state === "failed" ? "error" : "muted";
|
|
30
|
+
}
|
|
31
|
+
/** An agent at work shows a turning mark, as a session does. Any other shows
|
|
32
|
+
* a dot in its state's tone, pulsing while it waits on the reader and unlit
|
|
33
|
+
* once it was stopped or rests. */
|
|
34
|
+
function agentMark(state) {
|
|
35
|
+
if (state === "running") return { working: true };
|
|
36
|
+
return {
|
|
37
|
+
working: false,
|
|
38
|
+
tone: state === "done" ? "success" : stateTone(state),
|
|
39
|
+
pulse: state === "approval",
|
|
40
|
+
off: state === "stopped" || state === "idle"
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** `explore`, or the transcript's word for a task with no type of its own. */
|
|
44
|
+
function useAgentTypeLabel() {
|
|
45
|
+
const t = usePwCodeTranslations("agentTranscript");
|
|
46
|
+
return useCallback((agentType) => agentType && agentType !== "default" ? sanitizeLabel(agentType) : t("transcript.subagent"), [t]);
|
|
47
|
+
}
|
|
48
|
+
var flagged = (state) => state === "approval" || state === "failed";
|
|
49
|
+
function stateWord(state, t) {
|
|
50
|
+
switch (state) {
|
|
51
|
+
case "approval": return t("switcher.approval");
|
|
52
|
+
case "running": return t("switcher.running");
|
|
53
|
+
case "failed": return t("switcher.failed");
|
|
54
|
+
case "stopped": return t("switcher.stopped");
|
|
55
|
+
case "done": return t("switcher.done");
|
|
56
|
+
default: return t("switcher.idle");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function usePwCodeAgentLine() {
|
|
60
|
+
const t = usePwCodeTranslations("agents");
|
|
61
|
+
const typeLabel = useAgentTypeLabel();
|
|
62
|
+
return useCallback((agent) => {
|
|
63
|
+
const state = agentState(agent);
|
|
64
|
+
const type = typeLabel(agent.agentType);
|
|
65
|
+
const description = sanitizeLabel(agent.description);
|
|
66
|
+
return {
|
|
67
|
+
agent,
|
|
68
|
+
type,
|
|
69
|
+
description,
|
|
70
|
+
state,
|
|
71
|
+
stateWord: stateWord(state, t),
|
|
72
|
+
stateTone: stateTone(state),
|
|
73
|
+
flagged: flagged(state),
|
|
74
|
+
stoppable: agent.status === "running",
|
|
75
|
+
stopLabel: t("switcher.stop", { name: t("switcher.agentName", {
|
|
76
|
+
type,
|
|
77
|
+
description
|
|
78
|
+
}) })
|
|
79
|
+
};
|
|
80
|
+
}, [t, typeLabel]);
|
|
81
|
+
}
|
|
82
|
+
function elapsedMs(agent, nowSeconds) {
|
|
83
|
+
if (!agent.startedAt) return null;
|
|
84
|
+
const started = DateTime.fromISO(agent.startedAt).toMillis();
|
|
85
|
+
const ended = agent.endedAt ? DateTime.fromISO(agent.endedAt).toMillis() : nowSeconds * 1e3;
|
|
86
|
+
return Math.max(0, ended - started);
|
|
87
|
+
}
|
|
88
|
+
/** Failed agents and those awaiting approval are left out: the header counts
|
|
89
|
+
* them on their own. */
|
|
90
|
+
function turnSummary(current, t) {
|
|
91
|
+
const tally = (status) => current.filter((a) => a.status === status && !a.awaitingApproval).length;
|
|
92
|
+
const running = tally("running");
|
|
93
|
+
const done = tally("done");
|
|
94
|
+
const stopped = tally("stopped");
|
|
95
|
+
return [
|
|
96
|
+
running > 0 && t("switcher.runningCount", { count: running }),
|
|
97
|
+
done > 0 && t("switcher.doneCount", { count: done }),
|
|
98
|
+
stopped > 0 && t("switcher.stoppedCount", { count: stopped })
|
|
99
|
+
].filter(Boolean).join(" · ");
|
|
100
|
+
}
|
|
101
|
+
function switcherCount(agents, label) {
|
|
102
|
+
const [first] = agents;
|
|
103
|
+
return first ? {
|
|
104
|
+
count: agents.length,
|
|
105
|
+
label: label(agents.length),
|
|
106
|
+
first: first.id
|
|
107
|
+
} : null;
|
|
108
|
+
}
|
|
109
|
+
/** A session's subagents as a switcher shows them, beside its main
|
|
110
|
+
* conversation. While the list is open and an agent runs, the elapsed times
|
|
111
|
+
* tick each second. */
|
|
112
|
+
function usePwCodeAgentSwitcher({ agents, mainState, viewing, open, earlierOpen }) {
|
|
113
|
+
const t = usePwCodeTranslations("agents");
|
|
114
|
+
const format = useFormatter();
|
|
115
|
+
const line = usePwCodeAgentLine();
|
|
116
|
+
const anyRunning = agents.some((a) => a.status === "running");
|
|
117
|
+
const now = useClock((ms) => Math.floor(ms / 1e3), open && anyRunning);
|
|
118
|
+
return useMemo(() => {
|
|
119
|
+
const current = agents.filter((a) => a.current);
|
|
120
|
+
const earlier = agents.filter((a) => !a.current);
|
|
121
|
+
const running = agents.filter((a) => a.status === "running");
|
|
122
|
+
const row = (agent) => {
|
|
123
|
+
const elapsed = elapsedMs(agent, now);
|
|
124
|
+
return {
|
|
125
|
+
...line(agent),
|
|
126
|
+
elapsed: elapsed === null ? null : turnDuration(t, elapsed),
|
|
127
|
+
tokens: agent.tokens > 0 ? t("switcher.tokens", { tokens: format.number(agent.tokens, {
|
|
128
|
+
notation: "compact",
|
|
129
|
+
maximumFractionDigits: 1
|
|
130
|
+
}) }) : null,
|
|
131
|
+
viewing: agent.id === viewing,
|
|
132
|
+
folded: !agent.current && !earlierOpen && agent.id !== viewing
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
return {
|
|
136
|
+
main: {
|
|
137
|
+
state: mainState,
|
|
138
|
+
stateWord: stateWord(mainState, t),
|
|
139
|
+
stateTone: stateTone(mainState),
|
|
140
|
+
flagged: flagged(mainState),
|
|
141
|
+
viewing: viewing === null
|
|
142
|
+
},
|
|
143
|
+
agents: [...current, ...earlier].map(row),
|
|
144
|
+
earlier: earlier.length > 0 ? {
|
|
145
|
+
count: earlier.length,
|
|
146
|
+
label: earlierOpen ? t("switcher.hideEarlier", { count: earlier.length }) : t("switcher.showEarlier", { count: earlier.length })
|
|
147
|
+
} : null,
|
|
148
|
+
summary: current.length === 0 && earlier.length > 0 ? t("switcher.earlierOnly", { count: earlier.length }) : turnSummary(current, t),
|
|
149
|
+
working: running.some((a) => !a.awaitingApproval),
|
|
150
|
+
approvals: switcherCount(agents.filter((a) => a.awaitingApproval), (count) => t("switcher.approvals", { count })),
|
|
151
|
+
failed: switcherCount(current.filter((a) => a.status === "failed"), (count) => t("switcher.failedCount", { count })),
|
|
152
|
+
running: running.length
|
|
153
|
+
};
|
|
154
|
+
}, [
|
|
155
|
+
agents,
|
|
156
|
+
mainState,
|
|
157
|
+
viewing,
|
|
158
|
+
earlierOpen,
|
|
159
|
+
now,
|
|
160
|
+
t,
|
|
161
|
+
format,
|
|
162
|
+
line
|
|
163
|
+
]);
|
|
164
|
+
}
|
|
165
|
+
export { agentMark, switcherAgent, useAgentTypeLabel, usePwCodeAgentLine, usePwCodeAgentSwitcher };
|
package/dist/ago.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** "5m" since a time, for labels built into sentences. It counts days rather
|
|
2
|
+
* than turning into a date as a compact RelativeTime does, and re-reads every
|
|
3
|
+
* 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 { usePwCodeTranslations } from "./messages.js";
|
|
2
|
+
import { useCallback, useMemo } from "react";
|
|
3
|
+
import { DateTime } from "luxon";
|
|
4
|
+
import { useClock } from "@parallelworks/ui/headless";
|
|
5
|
+
// src/ago.ts
|
|
6
|
+
/** "5m" since a time, for labels built into sentences. It counts days rather
|
|
7
|
+
* than turning into a date as a compact RelativeTime does, and re-reads every
|
|
8
|
+
* 30 seconds. */
|
|
9
|
+
function useCompactAgo() {
|
|
10
|
+
const t = usePwCodeTranslations("agents");
|
|
11
|
+
const tick = useClock((current) => Math.floor(current / 3e4));
|
|
12
|
+
const now = useMemo(() => DateTime.now().toMillis(), [tick]);
|
|
13
|
+
const compactMinutes = useCallback((minutes) => {
|
|
14
|
+
if (!Number.isFinite(minutes) || minutes < 1) return t("ago.now");
|
|
15
|
+
if (minutes < 60) return t("ago.minutes", { n: minutes });
|
|
16
|
+
const hours = Math.floor(minutes / 60);
|
|
17
|
+
if (hours < 24) return t("ago.hours", { n: hours });
|
|
18
|
+
return t("ago.days", { n: Math.floor(hours / 24) });
|
|
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 };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Schemas } from './apiClient.js';
|
|
2
|
+
import type { PwCodeApi } from './createPwCodeApi.js';
|
|
3
|
+
type Allocation = Schemas['Allocation'];
|
|
4
|
+
/** Allocations that may fund `allows`; an empty allowed list means
|
|
5
|
+
* unrestricted. Non-chargeable allocations fund nothing directly. */
|
|
6
|
+
export declare function allocationsForType<A extends Pick<Allocation, 'nonChargeable' | 'allowed'>>(allocations: readonly A[], allows?: string): A[];
|
|
7
|
+
/** The allocations AI usage can bill: the gateway refuses a folder or one
|
|
8
|
+
* restricted to other resource types. */
|
|
9
|
+
export declare function aiAllocations<A extends Pick<Allocation, 'nonChargeable' | 'allowed'>>(allocations: readonly A[]): A[];
|
|
10
|
+
/** An organization model, which the platform bills to an allocation; the
|
|
11
|
+
* gateway refuses one without it. */
|
|
12
|
+
export declare function billsAllocation(model: string | null | undefined): boolean;
|
|
13
|
+
/** Why new usage cannot bill an allocation. */
|
|
14
|
+
export type PwCodeAllocationProblem = 'notRatedInUsd' | 'noBudget' | 'noActivePeriod' | 'budgetSpent' | 'noAccess' | 'budgetExceeded';
|
|
15
|
+
/** Why new usage cannot bill `a`, or null when it can. Where `requireUsd`,
|
|
16
|
+
* one not rated in USD cannot. */
|
|
17
|
+
export declare function allocationProblem(a: Pick<Allocation, 'usable' | 'ratedInUsd' | 'unusableReason'>, requireUsd: boolean): PwCodeAllocationProblem | null;
|
|
18
|
+
/** What `a` has spent of its budget, `used / total unit`, each amount as
|
|
19
|
+
* `amount` writes it. */
|
|
20
|
+
export declare function allocationUsage(a: Pick<Allocation, 'used' | 'estimatedUsed' | 'total' | 'unit'>, amount: (value: number) => string): string;
|
|
21
|
+
/** An allocation as a picker offers it. */
|
|
22
|
+
export interface PwCodeAllocationChoice {
|
|
23
|
+
value: string;
|
|
24
|
+
label: string;
|
|
25
|
+
/** Its spend, after why it cannot be billed when it cannot. */
|
|
26
|
+
description: string;
|
|
27
|
+
disabled: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** `a` as a picker offers it: one usage cannot bill stays listed, disabled,
|
|
30
|
+
* saying why. */
|
|
31
|
+
export declare function allocationChoice(a: Pick<Allocation, 'name' | 'usable' | 'ratedInUsd' | 'unusableReason' | 'used' | 'estimatedUsed' | 'total' | 'unit'>, { requireUsd, describe, amount, }: {
|
|
32
|
+
requireUsd: boolean;
|
|
33
|
+
/** Says why in the reader's language. */
|
|
34
|
+
describe: (problem: PwCodeAllocationProblem) => string;
|
|
35
|
+
amount: (value: number) => string;
|
|
36
|
+
}): PwCodeAllocationChoice;
|
|
37
|
+
/** The allocations the model may bill, as a picker offers them in the
|
|
38
|
+
* reader's language: none for a model that bills none, and undefined while
|
|
39
|
+
* they load. Which one is chosen first is the host's. */
|
|
40
|
+
export declare function useModelAllocations(api: Pick<PwCodeApi, 'useAllocations'>, model: string | null | undefined): PwCodeAllocationChoice[] | undefined;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { usePwCodeTranslations } from "./messages.js";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { isOrgModel } from "@parallelworks/ui/ai/headless";
|
|
4
|
+
import { useFormatter } from "use-intl";
|
|
5
|
+
// src/allocations.ts
|
|
6
|
+
/** Allocations that may fund `allows`; an empty allowed list means
|
|
7
|
+
* unrestricted. Non-chargeable allocations fund nothing directly. */
|
|
8
|
+
function allocationsForType(allocations, allows) {
|
|
9
|
+
return allocations.filter((a) => !a.nonChargeable && (!allows || !a.allowed?.length || a.allowed.includes(allows)));
|
|
10
|
+
}
|
|
11
|
+
/** The allocations AI usage can bill: the gateway refuses a folder or one
|
|
12
|
+
* restricted to other resource types. */
|
|
13
|
+
function aiAllocations(allocations) {
|
|
14
|
+
return allocationsForType(allocations, "ai");
|
|
15
|
+
}
|
|
16
|
+
/** An organization model, which the platform bills to an allocation; the
|
|
17
|
+
* gateway refuses one without it. */
|
|
18
|
+
function billsAllocation(model) {
|
|
19
|
+
return isOrgModel(model);
|
|
20
|
+
}
|
|
21
|
+
/** Why new usage cannot bill `a`, or null when it can. Where `requireUsd`,
|
|
22
|
+
* one not rated in USD cannot. */
|
|
23
|
+
function allocationProblem(a, requireUsd) {
|
|
24
|
+
if (requireUsd && a.ratedInUsd === false) return "notRatedInUsd";
|
|
25
|
+
if (a.usable !== false) return null;
|
|
26
|
+
switch (a.unusableReason) {
|
|
27
|
+
case "no-budget": return "noBudget";
|
|
28
|
+
case "no-active-period": return "noActivePeriod";
|
|
29
|
+
case "budget-spent": return "budgetSpent";
|
|
30
|
+
case "no-access": return "noAccess";
|
|
31
|
+
default: return "budgetExceeded";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** What `a` has spent of its budget, `used / total unit`, each amount as
|
|
35
|
+
* `amount` writes it. */
|
|
36
|
+
function allocationUsage(a, amount) {
|
|
37
|
+
return `${amount(Math.max(a.used ?? 0, a.estimatedUsed ?? 0))} / ${amount(a.total)} ${a.unit || "USD"}`;
|
|
38
|
+
}
|
|
39
|
+
/** `a` as a picker offers it: one usage cannot bill stays listed, disabled,
|
|
40
|
+
* saying why. */
|
|
41
|
+
function allocationChoice(a, { requireUsd, describe, amount }) {
|
|
42
|
+
const problem = allocationProblem(a, requireUsd);
|
|
43
|
+
const usage = allocationUsage(a, amount);
|
|
44
|
+
return {
|
|
45
|
+
value: a.name,
|
|
46
|
+
label: a.name,
|
|
47
|
+
description: problem ? `${describe(problem)} · ${usage}` : usage,
|
|
48
|
+
disabled: problem !== null
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** The allocations the model may bill, as a picker offers them in the
|
|
52
|
+
* reader's language: none for a model that bills none, and undefined while
|
|
53
|
+
* they load. Which one is chosen first is the host's. */
|
|
54
|
+
function useModelAllocations(api, model) {
|
|
55
|
+
const bills = billsAllocation(model);
|
|
56
|
+
const { data } = api.useAllocations(bills);
|
|
57
|
+
const t = usePwCodeTranslations("agents");
|
|
58
|
+
const format = useFormatter();
|
|
59
|
+
return useMemo(() => {
|
|
60
|
+
if (!bills) return [];
|
|
61
|
+
if (!data) return;
|
|
62
|
+
const describe = (problem) => t(`allocation.${problem}`);
|
|
63
|
+
const amount = (value) => format.number(value, { maximumFractionDigits: 2 });
|
|
64
|
+
return aiAllocations(data).map((a) => allocationChoice(a, {
|
|
65
|
+
requireUsd: false,
|
|
66
|
+
describe,
|
|
67
|
+
amount
|
|
68
|
+
}));
|
|
69
|
+
}, [
|
|
70
|
+
bills,
|
|
71
|
+
data,
|
|
72
|
+
t,
|
|
73
|
+
format
|
|
74
|
+
]);
|
|
75
|
+
}
|
|
76
|
+
export { aiAllocations, allocationChoice, allocationProblem, allocationUsage, allocationsForType, billsAllocation, useModelAllocations };
|