@stigmer/react-core 0.3.2 → 0.4.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.
- package/README.md +1 -1
- package/agent/useAgentVersions.d.ts +76 -0
- package/agent/useAgentVersions.d.ts.map +1 -0
- package/agent/useAgentVersions.js +120 -0
- package/agent/useAgentVersions.js.map +1 -0
- package/file/useSessionFiles.d.ts +55 -0
- package/file/useSessionFiles.d.ts.map +1 -0
- package/file/useSessionFiles.js +113 -0
- package/file/useSessionFiles.js.map +1 -0
- package/index.d.ts +12 -0
- package/index.d.ts.map +1 -1
- package/index.js +6 -0
- package/index.js.map +1 -1
- package/internal.d.ts +1 -0
- package/internal.d.ts.map +1 -1
- package/internal.js +1 -0
- package/internal.js.map +1 -1
- package/package.json +3 -3
- package/run/deriveRunFileChanges.d.ts +31 -0
- package/run/deriveRunFileChanges.d.ts.map +1 -0
- package/run/deriveRunFileChanges.js +124 -0
- package/run/deriveRunFileChanges.js.map +1 -0
- package/run/tool-categories.d.ts +3 -1
- package/run/tool-categories.d.ts.map +1 -1
- package/run/useAnswerCustomToolUse.d.ts +62 -0
- package/run/useAnswerCustomToolUse.d.ts.map +1 -0
- package/run/useAnswerCustomToolUse.js +67 -0
- package/run/useAnswerCustomToolUse.js.map +1 -0
- package/run/useCreateRun.d.ts +9 -1
- package/run/useCreateRun.d.ts.map +1 -1
- package/run/useCreateRun.js +1 -0
- package/run/useCreateRun.js.map +1 -1
- package/session/useSessionConversation.d.ts +19 -0
- package/session/useSessionConversation.d.ts.map +1 -1
- package/session/useSessionConversation.js +12 -0
- package/session/useSessionConversation.js.map +1 -1
- package/session/useSessionFileChanges.d.ts +47 -0
- package/session/useSessionFileChanges.d.ts.map +1 -0
- package/session/useSessionFileChanges.js +46 -0
- package/session/useSessionFileChanges.js.map +1 -0
- package/session/useSessionOutputs.d.ts +71 -0
- package/session/useSessionOutputs.d.ts.map +1 -0
- package/session/useSessionOutputs.js +113 -0
- package/session/useSessionOutputs.js.map +1 -0
- package/src/agent/useAgentVersions.ts +186 -0
- package/src/file/useSessionFiles.ts +144 -0
- package/src/index.ts +12 -0
- package/src/internal.ts +1 -0
- package/src/run/deriveRunFileChanges.ts +135 -0
- package/src/run/tool-categories.ts +1 -1
- package/src/run/useAnswerCustomToolUse.ts +119 -0
- package/src/run/useCreateRun.ts +10 -1
- package/src/session/useSessionConversation.ts +43 -0
- package/src/session/useSessionFileChanges.ts +67 -0
- package/src/session/useSessionOutputs.ts +167 -0
- package/src/test/samples.ts +30 -0
- package/src/version-history/version-entry.ts +41 -0
- package/test/samples.d.ts +11 -0
- package/test/samples.d.ts.map +1 -1
- package/test/samples.js +28 -0
- package/test/samples.js.map +1 -1
- package/version-history/version-entry.d.ts +37 -0
- package/version-history/version-entry.d.ts.map +1 -0
- package/version-history/version-entry.js +6 -0
- package/version-history/version-entry.js.map +1 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Run } from "@stigmer/apis/ai/stigmer/agentic/run/v1/api_pb";
|
|
2
|
+
import type { DisplayFileChange } from "@stigmer/client";
|
|
3
|
+
/** Return value of {@link useSessionFileChanges}. */
|
|
4
|
+
export interface UseSessionFileChangesReturn {
|
|
5
|
+
/**
|
|
6
|
+
* One {@link DisplayFileChange} per file touched in the session — the *net*
|
|
7
|
+
* change for that path (see {@link deriveRunFileChanges} for the
|
|
8
|
+
* net-diff semantics) — ordered for a file-list (modified, then
|
|
9
|
+
* created/renamed, then deleted; alphabetical within each group).
|
|
10
|
+
*/
|
|
11
|
+
readonly fileChanges: readonly DisplayFileChange[];
|
|
12
|
+
/** `true` when at least one file was changed across all runs. */
|
|
13
|
+
readonly hasFileChanges: boolean;
|
|
14
|
+
/** Number of distinct files changed. */
|
|
15
|
+
readonly fileChangeCount: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Pure derivation hook that aggregates an agent's file changes across every
|
|
19
|
+
* run in a session into one net change per file. A headless building
|
|
20
|
+
* block for a consolidated session-changes surface (a side list of every
|
|
21
|
+
* file the conversation touched; the React kit draws one as its file
|
|
22
|
+
* changes view); the Console itself renders file changes in the transcript
|
|
23
|
+
* (stamped edit rows + the per-turn decision bar) and does not consume this
|
|
24
|
+
* hook.
|
|
25
|
+
*
|
|
26
|
+
* A thin memoizing wrapper over {@link deriveRunFileChanges} — the
|
|
27
|
+
* execution-domain core. The source (each run's file record, via
|
|
28
|
+
* `displayFileChangeSets`), the net-diff collapse, and the file-list ordering
|
|
29
|
+
* are all documented on the core.
|
|
30
|
+
*
|
|
31
|
+
* @param executions - All runs for a session, in chronological order.
|
|
32
|
+
* Pass both completed and active-stream runs. Ordering matters: the
|
|
33
|
+
* net collapse anchors on each path's first and last change.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* function ChangedFileCount({ sessionId, org }: { sessionId: string; org: string }) {
|
|
38
|
+
* const conv = useSessionConversation(sessionId, org);
|
|
39
|
+
* const { fileChangeCount, hasFileChanges } = useSessionFileChanges(conv.runs);
|
|
40
|
+
* return hasFileChanges ? <span>{fileChangeCount} files changed</span> : null;
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @see useFileChangeContent — resolves a change's before/after text for diffing
|
|
45
|
+
*/
|
|
46
|
+
export declare function useSessionFileChanges(executions: readonly Run[]): UseSessionFileChangesReturn;
|
|
47
|
+
//# sourceMappingURL=useSessionFileChanges.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSessionFileChanges.d.ts","sourceRoot":"","sources":["../src/session/useSessionFileChanges.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gDAAgD,CAAC;AAC1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGzD,qDAAqD;AACrD,MAAM,WAAW,2BAA2B;IAC1C;;;;;OAKG;IACH,QAAQ,CAAC,WAAW,EAAE,SAAS,iBAAiB,EAAE,CAAC;IACnD,iEAAiE;IACjE,QAAQ,CAAC,cAAc,EAAE,OAAO,CAAC;IACjC,wCAAwC;IACxC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,SAAS,GAAG,EAAE,GACzB,2BAA2B,CAS7B"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
// Every file a conversation's turns changed, one net change per path: the
|
|
3
|
+
// data a side list of the conversation's changes reads. A memoizing wrapper
|
|
4
|
+
// over `deriveRunFileChanges`, which holds the net-change rules.
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import { deriveRunFileChanges } from "../run/deriveRunFileChanges.js";
|
|
7
|
+
/**
|
|
8
|
+
* Pure derivation hook that aggregates an agent's file changes across every
|
|
9
|
+
* run in a session into one net change per file. A headless building
|
|
10
|
+
* block for a consolidated session-changes surface (a side list of every
|
|
11
|
+
* file the conversation touched; the React kit draws one as its file
|
|
12
|
+
* changes view); the Console itself renders file changes in the transcript
|
|
13
|
+
* (stamped edit rows + the per-turn decision bar) and does not consume this
|
|
14
|
+
* hook.
|
|
15
|
+
*
|
|
16
|
+
* A thin memoizing wrapper over {@link deriveRunFileChanges} — the
|
|
17
|
+
* execution-domain core. The source (each run's file record, via
|
|
18
|
+
* `displayFileChangeSets`), the net-diff collapse, and the file-list ordering
|
|
19
|
+
* are all documented on the core.
|
|
20
|
+
*
|
|
21
|
+
* @param executions - All runs for a session, in chronological order.
|
|
22
|
+
* Pass both completed and active-stream runs. Ordering matters: the
|
|
23
|
+
* net collapse anchors on each path's first and last change.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* function ChangedFileCount({ sessionId, org }: { sessionId: string; org: string }) {
|
|
28
|
+
* const conv = useSessionConversation(sessionId, org);
|
|
29
|
+
* const { fileChangeCount, hasFileChanges } = useSessionFileChanges(conv.runs);
|
|
30
|
+
* return hasFileChanges ? <span>{fileChangeCount} files changed</span> : null;
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @see useFileChangeContent — resolves a change's before/after text for diffing
|
|
35
|
+
*/
|
|
36
|
+
export function useSessionFileChanges(executions) {
|
|
37
|
+
return useMemo(() => {
|
|
38
|
+
const fileChanges = deriveRunFileChanges(executions);
|
|
39
|
+
return {
|
|
40
|
+
fileChanges,
|
|
41
|
+
hasFileChanges: fileChanges.length > 0,
|
|
42
|
+
fileChangeCount: fileChanges.length,
|
|
43
|
+
};
|
|
44
|
+
}, [executions]);
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=useSessionFileChanges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSessionFileChanges.js","sourceRoot":"","sources":["../src/session/useSessionFileChanges.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,0EAA0E;AAC1E,4EAA4E;AAC5E,iEAAiE;AAEjE,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAGhC,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAiBtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,qBAAqB,CACnC,UAA0B;IAE1B,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,MAAM,WAAW,GAAG,oBAAoB,CAAC,UAAU,CAAC,CAAC;QACrD,OAAO;YACL,WAAW;YACX,cAAc,EAAE,WAAW,CAAC,MAAM,GAAG,CAAC;YACtC,eAAe,EAAE,WAAW,CAAC,MAAM;SACpC,CAAC;IACJ,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { File as StigmerFile } from "@stigmer/apis/ai/stigmer/agentic/file/v1/api_pb";
|
|
2
|
+
import type { Run } from "@stigmer/apis/ai/stigmer/agentic/run/v1/api_pb";
|
|
3
|
+
/**
|
|
4
|
+
* One output File, with what content views and Apply gating need about the
|
|
5
|
+
* turn that returned it.
|
|
6
|
+
*/
|
|
7
|
+
export interface SessionOutputEntry {
|
|
8
|
+
/** The File the latest turn to save at this path returned. */
|
|
9
|
+
readonly file: StigmerFile;
|
|
10
|
+
/**
|
|
11
|
+
* The older Files saved at the same output path by earlier turns, newest
|
|
12
|
+
* first (the oldest last). Empty when one turn saved the path.
|
|
13
|
+
*/
|
|
14
|
+
readonly earlier: readonly StigmerFile[];
|
|
15
|
+
/** ID of the run that returned this File. */
|
|
16
|
+
readonly runId: string;
|
|
17
|
+
/** Whether the run that returned it has ended. */
|
|
18
|
+
readonly isTerminal: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* `true` when another entry shares this File's filename at a different
|
|
21
|
+
* output path. Consumers render the path's folder to tell them apart.
|
|
22
|
+
*/
|
|
23
|
+
readonly hasNameCollision: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** Return value of {@link useSessionOutputs}. */
|
|
26
|
+
export interface UseSessionOutputsReturn {
|
|
27
|
+
/** One entry per output path, sorted by filename. */
|
|
28
|
+
readonly outputs: readonly SessionOutputEntry[];
|
|
29
|
+
/** `true` when at least one turn returned a File. */
|
|
30
|
+
readonly hasOutputs: boolean;
|
|
31
|
+
/** Number of entries (output paths, not versions). */
|
|
32
|
+
readonly outputCount: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The stable identity of an output within a conversation: its output path
|
|
36
|
+
* (where the agent saved it under the outputs folder), falling back to the
|
|
37
|
+
* filename.
|
|
38
|
+
*
|
|
39
|
+
* The one identity of an output: the de-duplication here (newest turn wins
|
|
40
|
+
* per key) reads it, and so must anything that opens an output and later
|
|
41
|
+
* looks its entry up again (the React kit's editor tabs key an open output
|
|
42
|
+
* by it), or the lookup could miss its File.
|
|
43
|
+
*/
|
|
44
|
+
export declare function outputKey(file: StigmerFile): string;
|
|
45
|
+
/**
|
|
46
|
+
* Pure derivation hook that lists the Files a conversation's turns returned,
|
|
47
|
+
* one per output path, sorted by filename — like a file explorer over the
|
|
48
|
+
* conversation's outputs.
|
|
49
|
+
*
|
|
50
|
+
* @param files - The conversation's Files, newest first (`useSessionFiles`).
|
|
51
|
+
* @param runs - The conversation's runs, in chronological order. A File's
|
|
52
|
+
* turn decides which of two Files at one path wins (the later turn) and
|
|
53
|
+
* whether the File's turn has ended.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* function MadeFiles({ sessionId, org }: { sessionId: string; org: string }) {
|
|
58
|
+
* const conv = useSessionConversation(sessionId, org);
|
|
59
|
+
* const { files } = useSessionFiles(org, sessionId, sessionFilesRefreshKey(conv.runs));
|
|
60
|
+
* const { outputCount } = useSessionOutputs(files, conv.runs);
|
|
61
|
+
* return <p>{outputCount} files made</p>;
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function useSessionOutputs(files: readonly StigmerFile[], runs: readonly Run[]): UseSessionOutputsReturn;
|
|
66
|
+
/**
|
|
67
|
+
* The derivation behind {@link useSessionOutputs}, for non-hook callers
|
|
68
|
+
* and tests.
|
|
69
|
+
*/
|
|
70
|
+
export declare function deriveSessionOutputs(files: readonly StigmerFile[], runs: readonly Run[]): UseSessionOutputsReturn;
|
|
71
|
+
//# sourceMappingURL=useSessionOutputs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSessionOutputs.d.ts","sourceRoot":"","sources":["../src/session/useSessionOutputs.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,iDAAiD,CAAC;AAC3F,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gDAAgD,CAAC;AAI1E;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,8DAA8D;IAC9D,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,SAAS,WAAW,EAAE,CAAC;IACzC,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,kDAAkD;IAClD,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;CACpC;AAED,iDAAiD;AACjD,MAAM,WAAW,uBAAuB;IACtC,qDAAqD;IACrD,QAAQ,CAAC,OAAO,EAAE,SAAS,kBAAkB,EAAE,CAAC;IAChD,qDAAqD;IACrD,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,sDAAsD;IACtD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAEnD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,IAAI,EAAE,SAAS,GAAG,EAAE,GACnB,uBAAuB,CAEzB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,IAAI,EAAE,SAAS,GAAG,EAAE,GACnB,uBAAuB,CA0DzB"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
/**
|
|
3
|
+
* Pure derivation of what a conversation's agent saved: the Files its turns
|
|
4
|
+
* returned (what agents saved in their outputs folder), one entry per output
|
|
5
|
+
* path, newest turn winning, with the earlier versions behind it.
|
|
6
|
+
*
|
|
7
|
+
* Trade-offs:
|
|
8
|
+
* - Only Files a turn returned are listed; a person's uploads carry no run
|
|
9
|
+
* id and stay out (they are the conversation's inputs, listed by
|
|
10
|
+
* `useSessionGivenFiles`).
|
|
11
|
+
* - Identity is the output path, so a later turn saving `reports/q3.md` again
|
|
12
|
+
* takes the entry, the way a folder shows one file per path; the earlier
|
|
13
|
+
* Files at that path stay reachable in `earlier`, so no turn's output is
|
|
14
|
+
* lost from view.
|
|
15
|
+
*/
|
|
16
|
+
import { useMemo } from "react";
|
|
17
|
+
import { RunPhase } from "@stigmer/apis/ai/stigmer/agentic/run/v1/enum_pb";
|
|
18
|
+
import { isTerminalPhase } from "../run/run-phases.js";
|
|
19
|
+
/**
|
|
20
|
+
* The stable identity of an output within a conversation: its output path
|
|
21
|
+
* (where the agent saved it under the outputs folder), falling back to the
|
|
22
|
+
* filename.
|
|
23
|
+
*
|
|
24
|
+
* The one identity of an output: the de-duplication here (newest turn wins
|
|
25
|
+
* per key) reads it, and so must anything that opens an output and later
|
|
26
|
+
* looks its entry up again (the React kit's editor tabs key an open output
|
|
27
|
+
* by it), or the lookup could miss its File.
|
|
28
|
+
*/
|
|
29
|
+
export function outputKey(file) {
|
|
30
|
+
return file.status?.outputPath || file.spec?.filename || file.metadata?.id || "";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Pure derivation hook that lists the Files a conversation's turns returned,
|
|
34
|
+
* one per output path, sorted by filename — like a file explorer over the
|
|
35
|
+
* conversation's outputs.
|
|
36
|
+
*
|
|
37
|
+
* @param files - The conversation's Files, newest first (`useSessionFiles`).
|
|
38
|
+
* @param runs - The conversation's runs, in chronological order. A File's
|
|
39
|
+
* turn decides which of two Files at one path wins (the later turn) and
|
|
40
|
+
* whether the File's turn has ended.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* function MadeFiles({ sessionId, org }: { sessionId: string; org: string }) {
|
|
45
|
+
* const conv = useSessionConversation(sessionId, org);
|
|
46
|
+
* const { files } = useSessionFiles(org, sessionId, sessionFilesRefreshKey(conv.runs));
|
|
47
|
+
* const { outputCount } = useSessionOutputs(files, conv.runs);
|
|
48
|
+
* return <p>{outputCount} files made</p>;
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function useSessionOutputs(files, runs) {
|
|
53
|
+
return useMemo(() => deriveSessionOutputs(files, runs), [files, runs]);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The derivation behind {@link useSessionOutputs}, for non-hook callers
|
|
57
|
+
* and tests.
|
|
58
|
+
*/
|
|
59
|
+
export function deriveSessionOutputs(files, runs) {
|
|
60
|
+
const runOrder = new Map();
|
|
61
|
+
runs.forEach((run, index) => {
|
|
62
|
+
const id = run.metadata?.id;
|
|
63
|
+
if (!id)
|
|
64
|
+
return;
|
|
65
|
+
runOrder.set(id, {
|
|
66
|
+
index,
|
|
67
|
+
terminal: isTerminalPhase(run.status?.phase ?? RunPhase.RUN_PHASE_UNSPECIFIED),
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
// Oldest first, so a later write to the same key wins: by the turn's place
|
|
71
|
+
// in the conversation, then by list position (the list is newest first).
|
|
72
|
+
// A File whose run is not loaded registered at that run's end, so it ranks
|
|
73
|
+
// before every loaded run and counts as ended.
|
|
74
|
+
const outputs = files
|
|
75
|
+
.map((file, position) => ({ file, position, runId: file.status?.runId ?? "" }))
|
|
76
|
+
.filter((output) => output.runId !== "")
|
|
77
|
+
.sort((a, b) => {
|
|
78
|
+
const ia = runOrder.get(a.runId)?.index ?? -1;
|
|
79
|
+
const ib = runOrder.get(b.runId)?.index ?? -1;
|
|
80
|
+
return ia !== ib ? ia - ib : b.position - a.position;
|
|
81
|
+
});
|
|
82
|
+
const entryMap = new Map();
|
|
83
|
+
for (const { file, runId } of outputs) {
|
|
84
|
+
const key = outputKey(file);
|
|
85
|
+
const previous = entryMap.get(key);
|
|
86
|
+
entryMap.set(key, {
|
|
87
|
+
file,
|
|
88
|
+
earlier: previous ? [previous.file, ...previous.earlier] : [],
|
|
89
|
+
runId,
|
|
90
|
+
isTerminal: runOrder.get(runId)?.terminal ?? true,
|
|
91
|
+
hasNameCollision: false,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const entries = Array.from(entryMap.values());
|
|
95
|
+
entries.sort((a, b) => filenameOf(a.file).localeCompare(filenameOf(b.file), undefined, { sensitivity: "base" }));
|
|
96
|
+
const nameCount = new Map();
|
|
97
|
+
for (const entry of entries) {
|
|
98
|
+
const lower = filenameOf(entry.file).toLowerCase();
|
|
99
|
+
nameCount.set(lower, (nameCount.get(lower) ?? 0) + 1);
|
|
100
|
+
}
|
|
101
|
+
const result = entries.map((entry) => (nameCount.get(filenameOf(entry.file).toLowerCase()) ?? 0) > 1
|
|
102
|
+
? { ...entry, hasNameCollision: true }
|
|
103
|
+
: entry);
|
|
104
|
+
return {
|
|
105
|
+
outputs: result,
|
|
106
|
+
hasOutputs: result.length > 0,
|
|
107
|
+
outputCount: result.length,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function filenameOf(file) {
|
|
111
|
+
return file.spec?.filename ?? "";
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=useSessionOutputs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSessionOutputs.js","sourceRoot":"","sources":["../src/session/useSessionOutputs.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// An agent's version history and how a version reads to a person. Every
|
|
4
|
+
// apply that changes an agent records a version under its content hash, and
|
|
5
|
+
// each turn of a conversation records the version it ran
|
|
6
|
+
// (`RunStatus.agent_version_hash`); a host that draws its own chat reads the
|
|
7
|
+
// history here and words a turn's version with `agentVersionLabel` ("v2",
|
|
8
|
+
// else a short hash), with no rendering package. The React kit's Versions
|
|
9
|
+
// tab, quality report and session view draw from the same two names.
|
|
10
|
+
//
|
|
11
|
+
// The history is mapped to `VersionEntry`, the timeline shape every
|
|
12
|
+
// versioned resource shares, so the kit's `VersionTimeline` draws it as is;
|
|
13
|
+
// the full spec of each version stays reachable by hash through `getSpec`.
|
|
14
|
+
// Tests: ./__tests__/useAgentVersions.test.tsx.
|
|
15
|
+
|
|
16
|
+
import { useCallback, useMemo } from "react";
|
|
17
|
+
import { create } from "@bufbuild/protobuf";
|
|
18
|
+
import { timestampDate } from "@bufbuild/protobuf/wkt";
|
|
19
|
+
import type { AgentSpec } from "@stigmer/apis/ai/stigmer/agentic/agent/v1/spec_pb";
|
|
20
|
+
import {
|
|
21
|
+
ListAgentVersionsInputSchema,
|
|
22
|
+
type AgentVersionEntry as ProtoAgentVersionEntry,
|
|
23
|
+
} from "@stigmer/apis/ai/stigmer/agentic/agent/v1/version_pb";
|
|
24
|
+
import { useStigmer } from "../hooks.js";
|
|
25
|
+
import { useCursorPages } from "../internal/useCursorPages.js";
|
|
26
|
+
import type { VersionEntry } from "../version-history/version-entry.js";
|
|
27
|
+
|
|
28
|
+
/** Return value of {@link useAgentVersions}. */
|
|
29
|
+
export interface UseAgentVersionsReturn {
|
|
30
|
+
/** Version entries mapped to the generic timeline format (newest first). */
|
|
31
|
+
readonly versions: readonly VersionEntry[];
|
|
32
|
+
/** `true` when the agent has no recorded version yet. */
|
|
33
|
+
readonly isEmpty: boolean;
|
|
34
|
+
/** `true` while the initial fetch is in flight. */
|
|
35
|
+
readonly isLoading: boolean;
|
|
36
|
+
/** Error from the last failed request, or `null`. */
|
|
37
|
+
readonly error: Error | null;
|
|
38
|
+
/** Discard cached data and re-fetch from the server. */
|
|
39
|
+
readonly refetch: () => void;
|
|
40
|
+
/**
|
|
41
|
+
* The spec a version stored, by its hash: what a turn recorded on that
|
|
42
|
+
* version runs. `null` for a hash the loaded pages do not hold.
|
|
43
|
+
*/
|
|
44
|
+
readonly getSpec: (versionHash: string) => AgentSpec | null;
|
|
45
|
+
/** `true` while the server holds entries beyond those loaded. */
|
|
46
|
+
readonly hasMore: boolean;
|
|
47
|
+
/** Load the next page. No-op while one is loading or when nothing follows. */
|
|
48
|
+
readonly loadMore: () => void;
|
|
49
|
+
/** `true` while a `loadMore` read is in flight. */
|
|
50
|
+
readonly isLoadingMore: boolean;
|
|
51
|
+
/** Error from the last failed `loadMore`, or `null`. */
|
|
52
|
+
readonly loadMoreError: Error | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Data hook that fetches an agent's version history.
|
|
57
|
+
*
|
|
58
|
+
* Every apply that changes an agent records a version under its content
|
|
59
|
+
* hash; this hook pages the `listVersions` RPC (newest first, `hasMore` /
|
|
60
|
+
* `loadMore` for older ones) and maps each entry to the
|
|
61
|
+
* generic {@link VersionEntry} every version timeline draws, keeping
|
|
62
|
+
* each version's full spec for {@link UseAgentVersionsReturn.getSpec}.
|
|
63
|
+
*
|
|
64
|
+
* An agent last written before agents were versioned has no history until
|
|
65
|
+
* its next apply: `versions` is empty and `isEmpty` is `true`.
|
|
66
|
+
*
|
|
67
|
+
* Pass `null` for `org` or `slug` to skip fetching (stable no-op).
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```tsx
|
|
71
|
+
* function AgentHistory() {
|
|
72
|
+
* const { versions, isEmpty } = useAgentVersions("acme", "variance-reporter");
|
|
73
|
+
* if (isEmpty) return <p>No versions yet</p>;
|
|
74
|
+
* return (
|
|
75
|
+
* <ul>
|
|
76
|
+
* {versions.map((v) => (
|
|
77
|
+
* <li key={v.id}>{v.tag ?? v.label}</li>
|
|
78
|
+
* ))}
|
|
79
|
+
* </ul>
|
|
80
|
+
* );
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function useAgentVersions(
|
|
85
|
+
org: string | null,
|
|
86
|
+
slug: string | null,
|
|
87
|
+
): UseAgentVersionsReturn {
|
|
88
|
+
const stigmer = useStigmer();
|
|
89
|
+
|
|
90
|
+
const pages = useCursorPages<ProtoAgentVersionEntry>(
|
|
91
|
+
org && slug
|
|
92
|
+
? (pageToken) =>
|
|
93
|
+
stigmer.agent.listVersions(
|
|
94
|
+
create(ListAgentVersionsInputSchema, { org, slug, pageToken }),
|
|
95
|
+
)
|
|
96
|
+
: null,
|
|
97
|
+
[org, slug, stigmer],
|
|
98
|
+
(entry) => entry.versionHash,
|
|
99
|
+
);
|
|
100
|
+
const rawVersions = pages.items;
|
|
101
|
+
const { hasMore, loadMore, isLoadingMore, loadMoreError, isLoading, error, refetch } = pages;
|
|
102
|
+
|
|
103
|
+
const versions = useMemo(
|
|
104
|
+
() => rawVersions.map(mapProtoToVersionEntry),
|
|
105
|
+
[rawVersions],
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const specByHash = useMemo(() => {
|
|
109
|
+
const map = new Map<string, AgentSpec>();
|
|
110
|
+
for (const entry of rawVersions) {
|
|
111
|
+
if (entry.versionHash !== "" && entry.specSnapshot !== undefined) {
|
|
112
|
+
map.set(entry.versionHash, entry.specSnapshot);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return map;
|
|
116
|
+
}, [rawVersions]);
|
|
117
|
+
|
|
118
|
+
const getSpec = useCallback(
|
|
119
|
+
(versionHash: string): AgentSpec | null =>
|
|
120
|
+
specByHash.get(versionHash) ?? null,
|
|
121
|
+
[specByHash],
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const isEmpty = versions.length === 0;
|
|
125
|
+
|
|
126
|
+
return useMemo(
|
|
127
|
+
() => ({
|
|
128
|
+
versions,
|
|
129
|
+
isEmpty,
|
|
130
|
+
hasMore,
|
|
131
|
+
loadMore,
|
|
132
|
+
isLoadingMore,
|
|
133
|
+
loadMoreError,
|
|
134
|
+
isLoading,
|
|
135
|
+
error,
|
|
136
|
+
refetch,
|
|
137
|
+
getSpec,
|
|
138
|
+
}),
|
|
139
|
+
[versions, isEmpty, hasMore, loadMore, isLoadingMore, loadMoreError, isLoading, error, refetch, getSpec],
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** How many hex characters of a version hash a label shows. */
|
|
144
|
+
const SHORT_HASH_LENGTH = 12;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* How a version reads to a person: its tag when it has one, else the
|
|
148
|
+
* short hash the Versions tab shows. `versions` is the agent's history as
|
|
149
|
+
* {@link useAgentVersions} returns it; a hash it does not hold (still
|
|
150
|
+
* loading, or a history the viewer cannot read) reads as its short hash.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```tsx
|
|
154
|
+
* import type { Run } from "@stigmer/apis/ai/stigmer/agentic/run/v1/api_pb";
|
|
155
|
+
*
|
|
156
|
+
* function RunVersion({ run }: { run: Run }) {
|
|
157
|
+
* const { versions } = useAgentVersions("acme", "reviewer");
|
|
158
|
+
* return <span>{agentVersionLabel(versions, run.status?.agentVersionHash ?? "")}</span>; // "v2" or "3f2a9c1e0b7d"
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
export function agentVersionLabel(
|
|
163
|
+
versions: readonly VersionEntry[],
|
|
164
|
+
versionHash: string,
|
|
165
|
+
): string {
|
|
166
|
+
const entry = versions.find((v) => v.id === versionHash);
|
|
167
|
+
return entry?.tag ?? versionHash.slice(0, SHORT_HASH_LENGTH);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function mapProtoToVersionEntry(proto: ProtoAgentVersionEntry): VersionEntry {
|
|
171
|
+
return {
|
|
172
|
+
id: proto.versionHash,
|
|
173
|
+
timestamp: proto.appliedAt ? timestampDate(proto.appliedAt) : new Date(0),
|
|
174
|
+
actor: proto.appliedBy
|
|
175
|
+
? {
|
|
176
|
+
id: proto.appliedBy.id,
|
|
177
|
+
avatar: proto.appliedBy.avatar || undefined,
|
|
178
|
+
displayName: undefined,
|
|
179
|
+
}
|
|
180
|
+
: undefined,
|
|
181
|
+
label: proto.versionHash.slice(0, SHORT_HASH_LENGTH),
|
|
182
|
+
sublabel: proto.message || undefined,
|
|
183
|
+
isCurrent: proto.isCurrent,
|
|
184
|
+
tag: proto.tag || undefined,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Data hook over a conversation's Files: one list per conversation, read
|
|
5
|
+
* through `stigmer.file.listAll({org, session_id})`, with the Files each turn
|
|
6
|
+
* returned grouped by run id.
|
|
7
|
+
*
|
|
8
|
+
* Trade-offs:
|
|
9
|
+
* - Files join a conversation at two moments, and nothing streams either:
|
|
10
|
+
* the Files sent with a message become the conversation's when its turn
|
|
11
|
+
* starts, and a turn's outputs register when it ends. So the list re-reads
|
|
12
|
+
* when the host says a turn started or ended (`refreshKey`, each run's id
|
|
13
|
+
* with whether it has ended). That key moves exactly when the host's runs
|
|
14
|
+
* refetch shows either, so Files follow the runs with no second trigger,
|
|
15
|
+
* no callback threaded through the send path, and no polling.
|
|
16
|
+
* - A re-read keeps the previous list on screen (stale-while-revalidate);
|
|
17
|
+
* only a different conversation resets it.
|
|
18
|
+
* - The server lists newest first, and both fields keep that order.
|
|
19
|
+
* - A person's uploads carry no run id and are listed in `files` but never
|
|
20
|
+
* in `outputsByRun`: the outputs of a turn are only what it saved.
|
|
21
|
+
* - The key's rule is one function, `sessionFilesRefreshKey`, which every
|
|
22
|
+
* caller passes its runs through, so a host cannot build a key that
|
|
23
|
+
* misses a turn's end (a run count does: it moves when a turn starts and
|
|
24
|
+
* not when it ends, so that turn's outputs never appear).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
28
|
+
import type { File as StigmerFile } from "@stigmer/apis/ai/stigmer/agentic/file/v1/api_pb";
|
|
29
|
+
import type { Run } from "@stigmer/apis/ai/stigmer/agentic/run/v1/api_pb";
|
|
30
|
+
import { RunPhase } from "@stigmer/apis/ai/stigmer/agentic/run/v1/enum_pb";
|
|
31
|
+
import { isTerminalPhase } from "../run/run-phases.js";
|
|
32
|
+
import { useStigmer } from "../hooks.js";
|
|
33
|
+
import { useFetch } from "../internal/useFetch.js";
|
|
34
|
+
|
|
35
|
+
const NO_FILES: readonly StigmerFile[] = [];
|
|
36
|
+
|
|
37
|
+
/** Return value of {@link useSessionFiles}. */
|
|
38
|
+
export interface UseSessionFilesReturn {
|
|
39
|
+
/** Every File of the conversation, newest first. */
|
|
40
|
+
readonly files: readonly StigmerFile[];
|
|
41
|
+
/** The Files each turn returned, by run id, newest first. */
|
|
42
|
+
readonly outputsByRun: ReadonlyMap<string, readonly StigmerFile[]>;
|
|
43
|
+
/** True while the first read is in flight. */
|
|
44
|
+
readonly isLoading: boolean;
|
|
45
|
+
/** Error from the last failed read, or `null`. */
|
|
46
|
+
readonly error: Error | null;
|
|
47
|
+
/** Read the list again (a stopped turn's outputs arrive after its end). */
|
|
48
|
+
readonly refetch: () => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Data hook that lists a conversation's Files and groups each turn's
|
|
53
|
+
* outputs by run id.
|
|
54
|
+
*
|
|
55
|
+
* @param org - The conversation's organization, or `null` to skip.
|
|
56
|
+
* @param sessionId - The conversation's id, or `null` to skip.
|
|
57
|
+
* @param refreshKey - A string that changes when a turn starts or ends:
|
|
58
|
+
* {@link sessionFilesRefreshKey} of the conversation's runs. A change
|
|
59
|
+
* re-reads the list.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* function TurnsWithFiles({ org, sessionId }: { org: string; sessionId: string }) {
|
|
64
|
+
* const conversation = useSessionConversation(sessionId, org);
|
|
65
|
+
* // Re-read as each turn starts and ends, so the files it made appear.
|
|
66
|
+
* const { outputsByRun } = useSessionFiles(org, sessionId, sessionFilesRefreshKey(conversation.runs));
|
|
67
|
+
* return <p>{outputsByRun.size} turns made files</p>;
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function useSessionFiles(
|
|
72
|
+
org: string | null,
|
|
73
|
+
sessionId: string | null,
|
|
74
|
+
refreshKey: string,
|
|
75
|
+
): UseSessionFilesReturn {
|
|
76
|
+
const stigmer = useStigmer();
|
|
77
|
+
const active = !!org && !!sessionId;
|
|
78
|
+
const { data, isLoading, error, refetch } = useFetch<readonly StigmerFile[]>(
|
|
79
|
+
org && sessionId
|
|
80
|
+
? async () => {
|
|
81
|
+
const files: StigmerFile[] = [];
|
|
82
|
+
for await (const file of stigmer.file.listAll({ org, sessionId })) {
|
|
83
|
+
files.push(file);
|
|
84
|
+
}
|
|
85
|
+
return files;
|
|
86
|
+
}
|
|
87
|
+
: null,
|
|
88
|
+
[org, sessionId, active, stigmer],
|
|
89
|
+
NO_FILES,
|
|
90
|
+
{ cacheKey: active ? `session-files:${sessionId}` : undefined },
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// A changed key re-reads in place; the first render's key is the mount's
|
|
94
|
+
// own read, so it never doubles it.
|
|
95
|
+
const lastKey = useRef(refreshKey);
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
if (lastKey.current === refreshKey) return;
|
|
98
|
+
lastKey.current = refreshKey;
|
|
99
|
+
refetch();
|
|
100
|
+
}, [refreshKey, refetch]);
|
|
101
|
+
|
|
102
|
+
const outputsByRun = useMemo(() => {
|
|
103
|
+
const byRun = new Map<string, StigmerFile[]>();
|
|
104
|
+
for (const file of data) {
|
|
105
|
+
const runId = file.status?.runId;
|
|
106
|
+
if (!runId) continue;
|
|
107
|
+
const list = byRun.get(runId);
|
|
108
|
+
if (list) list.push(file);
|
|
109
|
+
else byRun.set(runId, [file]);
|
|
110
|
+
}
|
|
111
|
+
return byRun as ReadonlyMap<string, readonly StigmerFile[]>;
|
|
112
|
+
}, [data]);
|
|
113
|
+
|
|
114
|
+
return useMemo(
|
|
115
|
+
() => ({ files: data, outputsByRun, isLoading, error, refetch }),
|
|
116
|
+
[data, outputsByRun, isLoading, error, refetch],
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The `refreshKey` {@link useSessionFiles} re-reads on: each run's id with
|
|
122
|
+
* whether it has ended, so the key moves when a turn starts (the Files sent
|
|
123
|
+
* with its message join the conversation) and when it ends (its outputs
|
|
124
|
+
* register), and at no other time.
|
|
125
|
+
*
|
|
126
|
+
* @param runs - The conversation's runs (`useSessionConversation().runs`).
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```tsx
|
|
130
|
+
* function FileCount({ org, sessionId }: { org: string; sessionId: string }) {
|
|
131
|
+
* const conversation = useSessionConversation(sessionId, org);
|
|
132
|
+
* const { files } = useSessionFiles(org, sessionId, sessionFilesRefreshKey(conversation.runs));
|
|
133
|
+
* return <p>{files.length} files</p>;
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export function sessionFilesRefreshKey(runs: readonly Run[]): string {
|
|
138
|
+
return runs
|
|
139
|
+
.map((run) => {
|
|
140
|
+
const ended = isTerminalPhase(run.status?.phase ?? RunPhase.RUN_PHASE_UNSPECIFIED);
|
|
141
|
+
return `${run.metadata?.id ?? ""}:${ended ? "ended" : "started"}`;
|
|
142
|
+
})
|
|
143
|
+
.join(",");
|
|
144
|
+
}
|