@nones/agentflow-opencode 0.1.0 → 0.6.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/LICENSE +21 -0
- package/README.md +119 -39
- package/package.json +8 -1
- package/src/adapter.ts +122 -4
- package/src/index.ts +18 -0
- package/src/normalize.ts +291 -0
- package/src/provider-history.ts +342 -0
- package/src/transcript.ts +930 -0
- package/src/types.ts +28 -104
package/src/normalize.ts
CHANGED
|
@@ -861,6 +861,297 @@ export function extractModelListing(value: unknown): ModelListing {
|
|
|
861
861
|
return { shape: sawShape ? "known" : "unknown", models };
|
|
862
862
|
}
|
|
863
863
|
|
|
864
|
+
/** One concrete, directly usable model entry from an active-catalogue read. */
|
|
865
|
+
export interface ActiveModelOption {
|
|
866
|
+
/** Exact `provider/id` string usable verbatim in agent/profile configuration. */
|
|
867
|
+
id: string;
|
|
868
|
+
provider: [string, string];
|
|
869
|
+
label?: string;
|
|
870
|
+
description?: string;
|
|
871
|
+
default?: boolean;
|
|
872
|
+
reasoningEfforts?: string[];
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
export interface ActiveProviderOption {
|
|
876
|
+
id: string;
|
|
877
|
+
name?: string;
|
|
878
|
+
models: ActiveModelOption[];
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
export interface ActiveCatalogue {
|
|
882
|
+
shape: "known" | "unknown";
|
|
883
|
+
providers: ActiveProviderOption[];
|
|
884
|
+
defaultModel?: string;
|
|
885
|
+
/** Provider ids advertised as present but explicitly not connected/authenticated. */
|
|
886
|
+
unconnected: string[];
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
const CONNECTED_KEYS = ["connected", "isConnected", "activated", "active"];
|
|
890
|
+
const AUTH_KEYS = [
|
|
891
|
+
"authenticated",
|
|
892
|
+
"isAuthenticated",
|
|
893
|
+
"authorized",
|
|
894
|
+
"authorised",
|
|
895
|
+
];
|
|
896
|
+
const EXPOSED_KEYS = ["exposed", "expose", "public", "enabled"];
|
|
897
|
+
|
|
898
|
+
function flag(
|
|
899
|
+
record: JsonRecord,
|
|
900
|
+
keys: readonly string[],
|
|
901
|
+
): boolean | undefined {
|
|
902
|
+
for (const key of keys) {
|
|
903
|
+
const value = record[key];
|
|
904
|
+
if (typeof value === "boolean") return value;
|
|
905
|
+
}
|
|
906
|
+
return undefined;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/** Whether a provider description is explicitly advertised as connected. */
|
|
910
|
+
function connectedMarker(entry: JsonRecord): boolean | undefined {
|
|
911
|
+
for (const key of [...CONNECTED_KEYS, ...AUTH_KEYS]) {
|
|
912
|
+
const value = entry[key];
|
|
913
|
+
if (typeof value === "boolean") return value;
|
|
914
|
+
}
|
|
915
|
+
if (asString(entry.source) === "config") return true;
|
|
916
|
+
return undefined;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function excludedMarker(entry: JsonRecord): boolean {
|
|
920
|
+
for (const key of EXPOSED_KEYS) {
|
|
921
|
+
if (entry[key] === false) return true;
|
|
922
|
+
}
|
|
923
|
+
const connected = flag(entry, CONNECTED_KEYS);
|
|
924
|
+
if (connected === false) return true;
|
|
925
|
+
const authenticated = flag(entry, AUTH_KEYS);
|
|
926
|
+
if (authenticated === false) return true;
|
|
927
|
+
const origin = asString(entry.source) ?? asString(entry.origin);
|
|
928
|
+
return origin === "models.dev" || origin === "public" || origin === "catalog";
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function reasoningEfforts(info: JsonRecord): string[] | undefined {
|
|
932
|
+
const efforts = info.reasoningEfforts ?? info.reasoningEffort ?? info.efforts;
|
|
933
|
+
if (Array.isArray(efforts)) {
|
|
934
|
+
const values = efforts
|
|
935
|
+
.map((entry) =>
|
|
936
|
+
isRecord(entry) ? asString(entry.name ?? entry.id) : asString(entry),
|
|
937
|
+
)
|
|
938
|
+
.filter((entry): entry is string => entry !== undefined);
|
|
939
|
+
if (values.length > 0) return dedupeStrings(values);
|
|
940
|
+
}
|
|
941
|
+
// Models.dev-style variants are exposed in OpenCode as reasoning effort levels.
|
|
942
|
+
const variants = info.variants;
|
|
943
|
+
if (isRecord(variants)) {
|
|
944
|
+
const keys = Object.keys(variants).filter((key) => key.length > 0);
|
|
945
|
+
if (keys.length > 0) return dedupeStrings(keys);
|
|
946
|
+
}
|
|
947
|
+
return undefined;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
function dedupeStrings(values: readonly string[]): string[] {
|
|
951
|
+
const seen = new Set<string>();
|
|
952
|
+
const result: string[] = [];
|
|
953
|
+
for (const value of values) {
|
|
954
|
+
if (seen.has(value)) continue;
|
|
955
|
+
seen.add(value);
|
|
956
|
+
result.push(value);
|
|
957
|
+
}
|
|
958
|
+
return result;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* Parse the active model catalogue served by a local, authenticated endpoint.
|
|
963
|
+
*
|
|
964
|
+
* Unlike {@link extractModelListing} (a best-effort presence check), this keeps
|
|
965
|
+
* presentation metadata and reports connectivity explicitly. Unconnected or
|
|
966
|
+
* public inventory is collected in `unconnected` and never merged into
|
|
967
|
+
* `providers`, so a caller cannot accidentally treat public inventory as active.
|
|
968
|
+
*/
|
|
969
|
+
export function extractActiveCatalogue(value: unknown): ActiveCatalogue {
|
|
970
|
+
const body = unwrapData(value);
|
|
971
|
+
const seen = new Set<string>();
|
|
972
|
+
const providers: ActiveProviderOption[] = [];
|
|
973
|
+
const unconnected: string[] = [];
|
|
974
|
+
let shape: "known" | "unknown" = "unknown";
|
|
975
|
+
|
|
976
|
+
const pushProvider = (entry: JsonRecord, providerHint?: string) => {
|
|
977
|
+
const providerID =
|
|
978
|
+
asString(entry.id) ??
|
|
979
|
+
asString(entry.providerID) ??
|
|
980
|
+
asString(entry.providerId) ??
|
|
981
|
+
asString(entry.name) ??
|
|
982
|
+
providerHint;
|
|
983
|
+
if (!providerID) return;
|
|
984
|
+
// An explicit connected/authenticated marker always wins. Otherwise a
|
|
985
|
+
// provider that is explicitly unconnected or public inventory is recorded
|
|
986
|
+
// as excluded and never surfaces as an active model.
|
|
987
|
+
const excluded = connectedMarker(entry) === false || excludedMarker(entry);
|
|
988
|
+
if (excluded) {
|
|
989
|
+
if (!unconnected.includes(providerID)) unconnected.push(providerID);
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
const models = providerModels(entry);
|
|
993
|
+
if (models === undefined) return;
|
|
994
|
+
const options: ActiveModelOption[] = [];
|
|
995
|
+
for (const model of models) {
|
|
996
|
+
if (isRecord(model) && excludedMarker(model)) continue;
|
|
997
|
+
const option = toActiveOption(providerID, model);
|
|
998
|
+
if (!option) continue;
|
|
999
|
+
if (seen.has(option.id)) continue;
|
|
1000
|
+
seen.add(option.id);
|
|
1001
|
+
options.push(option);
|
|
1002
|
+
}
|
|
1003
|
+
if (options.length === 0) return;
|
|
1004
|
+
providers.push({
|
|
1005
|
+
id: providerID,
|
|
1006
|
+
...(asString(entry.name) ? { name: asString(entry.name) } : {}),
|
|
1007
|
+
models: options,
|
|
1008
|
+
});
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
const visitList = (list: readonly unknown[]): boolean => {
|
|
1012
|
+
let known = 0;
|
|
1013
|
+
for (const entry of list) {
|
|
1014
|
+
if (isRecord(entry) && providerModels(entry) !== undefined) {
|
|
1015
|
+
pushProvider(entry);
|
|
1016
|
+
known += 1;
|
|
1017
|
+
continue;
|
|
1018
|
+
}
|
|
1019
|
+
if (isRecord(entry)) {
|
|
1020
|
+
const option = toActiveOption(undefined, entry);
|
|
1021
|
+
if (option) {
|
|
1022
|
+
known += 1;
|
|
1023
|
+
if (excludedMarker(entry)) continue;
|
|
1024
|
+
const provider = option.provider[0];
|
|
1025
|
+
let target = providers.find((candidate) => candidate.id === provider);
|
|
1026
|
+
if (!target) {
|
|
1027
|
+
target = { id: provider, models: [] };
|
|
1028
|
+
providers.push(target);
|
|
1029
|
+
}
|
|
1030
|
+
if (!seen.has(option.id)) {
|
|
1031
|
+
seen.add(option.id);
|
|
1032
|
+
target.models.push(option);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
return list.length === 0 || known === list.length;
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
if (Array.isArray(body)) {
|
|
1041
|
+
shape = visitList(body) ? "known" : "unknown";
|
|
1042
|
+
} else if (isRecord(body)) {
|
|
1043
|
+
const lists: readonly unknown[] | undefined = Array.isArray(body.providers)
|
|
1044
|
+
? body.providers
|
|
1045
|
+
: Array.isArray(body.models)
|
|
1046
|
+
? body.models
|
|
1047
|
+
: undefined;
|
|
1048
|
+
if (lists) {
|
|
1049
|
+
shape = visitList(lists) ? "known" : "unknown";
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
const defaultModel = readDefaultModel(value) ?? readDefaultModel(body);
|
|
1054
|
+
|
|
1055
|
+
return {
|
|
1056
|
+
shape,
|
|
1057
|
+
providers,
|
|
1058
|
+
...(defaultModel ? { defaultModel } : {}),
|
|
1059
|
+
unconnected,
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
function providerModels(entry: JsonRecord): readonly unknown[] | undefined {
|
|
1064
|
+
for (const key of ["models", "items", "list"]) {
|
|
1065
|
+
const candidate = entry[key];
|
|
1066
|
+
if (Array.isArray(candidate)) return candidate;
|
|
1067
|
+
if (isRecord(candidate)) {
|
|
1068
|
+
return Object.entries(candidate).map(([id, model]) =>
|
|
1069
|
+
isRecord(model) ? { id, ...model } : model,
|
|
1070
|
+
);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
return undefined;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
function toActiveOption(
|
|
1077
|
+
providerHint: string | undefined,
|
|
1078
|
+
entry: unknown,
|
|
1079
|
+
): ActiveModelOption | undefined {
|
|
1080
|
+
if (typeof entry === "string") {
|
|
1081
|
+
if (!providerHint) return undefined;
|
|
1082
|
+
const info: JsonRecord = {};
|
|
1083
|
+
return buildOption(providerHint, entry, info);
|
|
1084
|
+
}
|
|
1085
|
+
if (!isRecord(entry)) return undefined;
|
|
1086
|
+
const providerID =
|
|
1087
|
+
asString(entry.providerID) ??
|
|
1088
|
+
asString(entry.providerId) ??
|
|
1089
|
+
asString(entry.provider) ??
|
|
1090
|
+
providerHint;
|
|
1091
|
+
const modelID =
|
|
1092
|
+
asString(entry.modelID) ?? asString(entry.modelId) ?? asString(entry.model);
|
|
1093
|
+
const id = asString(entry.id);
|
|
1094
|
+
let provider = providerID;
|
|
1095
|
+
let model = modelID;
|
|
1096
|
+
if (provider && !model && id) {
|
|
1097
|
+
const parsed = tryParseModelRef(id, "model catalogue");
|
|
1098
|
+
if (parsed && parsed.providerID === provider) {
|
|
1099
|
+
model = parsed.id;
|
|
1100
|
+
} else {
|
|
1101
|
+
model = id;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
if (!provider && id) {
|
|
1105
|
+
const parsed = tryParseModelRef(id, "model catalogue");
|
|
1106
|
+
if (parsed) {
|
|
1107
|
+
provider = parsed.providerID;
|
|
1108
|
+
model = parsed.id;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
if (!provider || !model) return undefined;
|
|
1112
|
+
return buildOption(provider, model, entry);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
function buildOption(
|
|
1116
|
+
provider: string,
|
|
1117
|
+
model: string,
|
|
1118
|
+
info: JsonRecord,
|
|
1119
|
+
): ActiveModelOption {
|
|
1120
|
+
const nameValue = asString(info.name) ?? asString(info.label);
|
|
1121
|
+
// Only use a display name when it differs from the raw id; otherwise there is
|
|
1122
|
+
// no human label worth carrying.
|
|
1123
|
+
const label = nameValue && nameValue !== model ? nameValue : undefined;
|
|
1124
|
+
const description = asString(info.description) ?? asString(info.summary);
|
|
1125
|
+
const efforts = reasoningEfforts(info);
|
|
1126
|
+
const isDefault =
|
|
1127
|
+
info.default === true ||
|
|
1128
|
+
info.isDefault === true ||
|
|
1129
|
+
info.is_default === true ||
|
|
1130
|
+
info.default === "true";
|
|
1131
|
+
return {
|
|
1132
|
+
id: `${provider}/${model}`,
|
|
1133
|
+
provider: [provider, model],
|
|
1134
|
+
...(label ? { label } : {}),
|
|
1135
|
+
...(description ? { description } : {}),
|
|
1136
|
+
...(isDefault ? { default: true } : {}),
|
|
1137
|
+
...(efforts ? { reasoningEfforts: efforts } : {}),
|
|
1138
|
+
};
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
function readDefaultModel(value: unknown): string | undefined {
|
|
1142
|
+
if (!isRecord(value)) return undefined;
|
|
1143
|
+
const candidate = value.defaultModel ?? value.default;
|
|
1144
|
+
if (typeof candidate === "string") {
|
|
1145
|
+
const parsed = tryParseModelRef(candidate, "defaultModel");
|
|
1146
|
+
return parsed ? modelToString(parsed) : undefined;
|
|
1147
|
+
}
|
|
1148
|
+
if (isRecord(candidate)) {
|
|
1149
|
+
const parsed = tryParseModelRef(candidate, "defaultModel");
|
|
1150
|
+
return parsed ? modelToString(parsed) : undefined;
|
|
1151
|
+
}
|
|
1152
|
+
return undefined;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
864
1155
|
export function classifyRateLimit(
|
|
865
1156
|
body: unknown,
|
|
866
1157
|
rawText: string,
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider bridge for durable visible history.
|
|
3
|
+
*
|
|
4
|
+
* The engine owns the profile and the `HistoryStore`; this module only turns an
|
|
5
|
+
* adapter slice and an already-resolved profile into the optional provider
|
|
6
|
+
* history capability consumed by the core store. It never imports
|
|
7
|
+
* `@nones/agentflow-core`, so the shapes below are structural copies of
|
|
8
|
+
* `CapabilityHistoryPage` and `HistoryRecord` and stay assignable to them.
|
|
9
|
+
*
|
|
10
|
+
* Boundary rules:
|
|
11
|
+
* - `read` is read-only: it only calls `inspectTranscript`, which issues
|
|
12
|
+
* `GET /api/session/{id}/message`. Reconciling history never sends a prompt;
|
|
13
|
+
* - the provider's own cursor and stable message/part ids are the durable
|
|
14
|
+
* identity, so a replayed page upserts instead of double-counting usage;
|
|
15
|
+
* - a provider failure is returned as `error` (with no records), never as an
|
|
16
|
+
* empty transcript, and paging metadata is forwarded exactly as the endpoint
|
|
17
|
+
* reported it rather than assumed;
|
|
18
|
+
* - normalized OpenCode summaries already drop raw tool arguments, tool output,
|
|
19
|
+
* provider headers and private reasoning; only that safe view is mapped.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { resolveDirectory, readSettings } from "./config";
|
|
23
|
+
import { OpenCodeHttpError } from "./errors";
|
|
24
|
+
import { normalizeTranscript } from "./transcript";
|
|
25
|
+
import type { Profile, OpenCodeAdapterOptions } from "./types";
|
|
26
|
+
import type {
|
|
27
|
+
BoundedState,
|
|
28
|
+
TranscriptMessageSummary,
|
|
29
|
+
TranscriptPage,
|
|
30
|
+
TranscriptPageRequest,
|
|
31
|
+
TranscriptToolPart,
|
|
32
|
+
TranscriptUsage,
|
|
33
|
+
} from "./transcript";
|
|
34
|
+
|
|
35
|
+
/** Subset of a persisted history record the store re-normalizes and bounds. */
|
|
36
|
+
export interface ProviderHistoryPart {
|
|
37
|
+
id: string;
|
|
38
|
+
kind: "text" | "tool" | "reasoning" | "unknown";
|
|
39
|
+
state: BoundedState | "redacted" | "unsupported";
|
|
40
|
+
text?: string;
|
|
41
|
+
length?: number;
|
|
42
|
+
type?: string;
|
|
43
|
+
name?: string;
|
|
44
|
+
status?: string;
|
|
45
|
+
timeStart?: number;
|
|
46
|
+
timeEnd?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Structural copy of the core `HistoryRecord`. */
|
|
50
|
+
export interface ProviderHistoryRecord {
|
|
51
|
+
providerMessageId: string;
|
|
52
|
+
role: "assistant" | "user" | "unknown";
|
|
53
|
+
providerSessionId?: string;
|
|
54
|
+
parentMessageId?: string;
|
|
55
|
+
model?: { providerID: string; id: string };
|
|
56
|
+
createdAt?: number;
|
|
57
|
+
completedAt?: number;
|
|
58
|
+
usage?: TranscriptUsage;
|
|
59
|
+
usageState: "complete" | "partial" | "unknown";
|
|
60
|
+
unmappedCounters: string[];
|
|
61
|
+
text: string;
|
|
62
|
+
textState: BoundedState;
|
|
63
|
+
parts: ProviderHistoryPart[];
|
|
64
|
+
omissions: string[];
|
|
65
|
+
cursor?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Structural copy of the core `CapabilityHistoryPage`. */
|
|
69
|
+
export interface ProviderHistoryPage {
|
|
70
|
+
records: readonly ProviderHistoryRecord[];
|
|
71
|
+
nextCursor?: string;
|
|
72
|
+
paging: "complete" | "unknown" | "unsupported";
|
|
73
|
+
capturedAt?: number;
|
|
74
|
+
error?: string;
|
|
75
|
+
notice?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ProviderHistoryCapability {
|
|
79
|
+
read(request: {
|
|
80
|
+
sessionId: string;
|
|
81
|
+
cursor?: string;
|
|
82
|
+
limit?: number;
|
|
83
|
+
signal?: AbortSignal;
|
|
84
|
+
}): Promise<ProviderHistoryPage>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The minimal adapter slice this bridge needs; never the whole adapter. */
|
|
88
|
+
export interface ProviderHistorySource {
|
|
89
|
+
inspectTranscript(
|
|
90
|
+
profile: Profile,
|
|
91
|
+
sessionID: string,
|
|
92
|
+
request?: TranscriptPageRequest & { signal?: AbortSignal },
|
|
93
|
+
): Promise<TranscriptPage>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const KNOWN_COUNTERS = [
|
|
97
|
+
"input",
|
|
98
|
+
"output",
|
|
99
|
+
"cachedInput",
|
|
100
|
+
"cacheWrite",
|
|
101
|
+
"reasoning",
|
|
102
|
+
] as const;
|
|
103
|
+
|
|
104
|
+
function mapPart(
|
|
105
|
+
part: TranscriptMessageSummary["parts"][number],
|
|
106
|
+
): ProviderHistoryPart {
|
|
107
|
+
if (part.kind === "text") {
|
|
108
|
+
return {
|
|
109
|
+
id: part.id,
|
|
110
|
+
kind: "text",
|
|
111
|
+
state: part.state,
|
|
112
|
+
...(part.text === "" ? {} : { text: part.text }),
|
|
113
|
+
...(part.length === undefined ? {} : { length: part.length }),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (part.kind === "tool") {
|
|
117
|
+
const tool: TranscriptToolPart = part;
|
|
118
|
+
return {
|
|
119
|
+
id: tool.id,
|
|
120
|
+
kind: "tool",
|
|
121
|
+
// Tool arguments and output are never carried; the store keeps them explicitly omitted.
|
|
122
|
+
state: "omitted",
|
|
123
|
+
...(tool.name === undefined ? {} : { name: tool.name }),
|
|
124
|
+
...(tool.status === undefined ? {} : { status: tool.status }),
|
|
125
|
+
...(tool.time?.start === undefined ? {} : { timeStart: tool.time.start }),
|
|
126
|
+
...(tool.time?.end === undefined ? {} : { timeEnd: tool.time.end }),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (part.kind === "reasoning") {
|
|
130
|
+
return { id: part.id, kind: "reasoning", state: "redacted" };
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
id: part.id,
|
|
134
|
+
kind: "unknown",
|
|
135
|
+
state: "unsupported",
|
|
136
|
+
...(part.type === undefined ? {} : { type: part.type }),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* A counter the provider did not report is missing; an attempt that reports
|
|
142
|
+
* only some known counters is explicitly `partial`, never a complete zero.
|
|
143
|
+
*/
|
|
144
|
+
function usageState(usage: TranscriptUsage | undefined): {
|
|
145
|
+
usageState: "complete" | "partial" | "unknown";
|
|
146
|
+
unmappedCounters: string[];
|
|
147
|
+
} {
|
|
148
|
+
if (usage === undefined)
|
|
149
|
+
return { usageState: "unknown", unmappedCounters: [] };
|
|
150
|
+
const missing = KNOWN_COUNTERS.filter((key) => usage[key] === undefined);
|
|
151
|
+
return {
|
|
152
|
+
usageState: missing.length === 0 ? "complete" : "partial",
|
|
153
|
+
unmappedCounters: [],
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Map one normalized safe summary into a durable store record. */
|
|
158
|
+
export function toHistoryRecord(
|
|
159
|
+
message: TranscriptMessageSummary,
|
|
160
|
+
cursor: string | undefined,
|
|
161
|
+
): ProviderHistoryRecord {
|
|
162
|
+
const parts = message.parts.map(mapPart);
|
|
163
|
+
// Only visible text parts feed the joined message text; a reasoning part's
|
|
164
|
+
// raw text is never carried, so it can never leak through this join.
|
|
165
|
+
const textParts = message.parts.filter(
|
|
166
|
+
(
|
|
167
|
+
part,
|
|
168
|
+
): part is Extract<
|
|
169
|
+
TranscriptMessageSummary["parts"][number],
|
|
170
|
+
{ kind: "text" }
|
|
171
|
+
> => part.kind === "text" && part.state !== "omitted",
|
|
172
|
+
);
|
|
173
|
+
const text = textParts.map((part) => part.text).join("\n");
|
|
174
|
+
const textState: BoundedState =
|
|
175
|
+
textParts.length === 0
|
|
176
|
+
? "omitted"
|
|
177
|
+
: textParts.some((part) => part.state === "truncated")
|
|
178
|
+
? "truncated"
|
|
179
|
+
: "complete";
|
|
180
|
+
const usage = usageState(message.usage);
|
|
181
|
+
return {
|
|
182
|
+
providerMessageId: message.id,
|
|
183
|
+
role: message.role,
|
|
184
|
+
...(message.sessionID === undefined
|
|
185
|
+
? {}
|
|
186
|
+
: { providerSessionId: message.sessionID }),
|
|
187
|
+
...(message.parentID === undefined
|
|
188
|
+
? {}
|
|
189
|
+
: { parentMessageId: message.parentID }),
|
|
190
|
+
...(message.model === undefined ? {} : { model: message.model }),
|
|
191
|
+
...(message.createdAt === undefined
|
|
192
|
+
? {}
|
|
193
|
+
: { createdAt: message.createdAt }),
|
|
194
|
+
...(message.completedAt === undefined
|
|
195
|
+
? {}
|
|
196
|
+
: { completedAt: message.completedAt }),
|
|
197
|
+
...(message.usage === undefined ? {} : { usage: message.usage }),
|
|
198
|
+
usageState: usage.usageState,
|
|
199
|
+
unmappedCounters: usage.unmappedCounters,
|
|
200
|
+
text,
|
|
201
|
+
textState,
|
|
202
|
+
parts,
|
|
203
|
+
omissions: [],
|
|
204
|
+
...(cursor === undefined ? {} : { cursor }),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Turn a normalized transcript page into a store page. Safe OpenCode
|
|
210
|
+
* summaries become records; anything the summary dropped (headers, tool
|
|
211
|
+
* payloads, reasoning, error text) is recorded as an explicit omission so the
|
|
212
|
+
* stored view never looks more complete than it is.
|
|
213
|
+
*/
|
|
214
|
+
export function toHistoryPage(
|
|
215
|
+
page: TranscriptPage,
|
|
216
|
+
cursor?: string,
|
|
217
|
+
): ProviderHistoryPage {
|
|
218
|
+
const { summary: normalized } = normalizeTranscript(page.entries);
|
|
219
|
+
const omissions = normalized.omitted.map(
|
|
220
|
+
(entry) => `${entry.kind}: ${entry.reason}`,
|
|
221
|
+
);
|
|
222
|
+
return {
|
|
223
|
+
records: normalized.messages.map((message) => {
|
|
224
|
+
// `cursor` is the provider cursor that produced this page, not the next.
|
|
225
|
+
const record = toHistoryRecord(message, cursor);
|
|
226
|
+
return omissions.length === 0
|
|
227
|
+
? record
|
|
228
|
+
: { ...record, omissions: [...record.omissions, ...omissions] };
|
|
229
|
+
}),
|
|
230
|
+
...(page.nextCursor === undefined ? {} : { nextCursor: page.nextCursor }),
|
|
231
|
+
// Forward paging exactly; `unknown`/`unsupported` forbid following a cursor.
|
|
232
|
+
paging: page.status,
|
|
233
|
+
capturedAt: page.capturedAt,
|
|
234
|
+
...(normalized.messages.length === 0 &&
|
|
235
|
+
normalized.invalid > 0 &&
|
|
236
|
+
page.entries.length > 0
|
|
237
|
+
? {
|
|
238
|
+
notice: `no message had a stable provider id (${normalized.invalid} invalid)`,
|
|
239
|
+
}
|
|
240
|
+
: {}),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function failurePage(error: unknown): ProviderHistoryPage {
|
|
245
|
+
return {
|
|
246
|
+
records: [],
|
|
247
|
+
paging: "unknown",
|
|
248
|
+
error:
|
|
249
|
+
error instanceof OpenCodeHttpError
|
|
250
|
+
? `provider history read failed with HTTP ${error.status}`
|
|
251
|
+
: "provider history read failed",
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Bind a capability to one effective profile. The engine resolves the profile
|
|
257
|
+
* once and passes the exact object it uses for turn admission, so history is
|
|
258
|
+
* read from the same endpoint, directory and credential reference.
|
|
259
|
+
*/
|
|
260
|
+
export function createProviderHistoryCapability(
|
|
261
|
+
adapter: ProviderHistorySource,
|
|
262
|
+
profile: Profile,
|
|
263
|
+
): ProviderHistoryCapability {
|
|
264
|
+
return {
|
|
265
|
+
async read(request) {
|
|
266
|
+
try {
|
|
267
|
+
const page = await adapter.inspectTranscript(
|
|
268
|
+
profile,
|
|
269
|
+
request.sessionId,
|
|
270
|
+
{
|
|
271
|
+
...(request.cursor === undefined ? {} : { cursor: request.cursor }),
|
|
272
|
+
...(request.limit === undefined ? {} : { limit: request.limit }),
|
|
273
|
+
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
274
|
+
},
|
|
275
|
+
);
|
|
276
|
+
return toHistoryPage(page, request.cursor);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
// A provider/config failure is an explicit error state, never empty history.
|
|
279
|
+
return failurePage(error);
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Whether history collection is opted in for a profile. Off by default, because
|
|
287
|
+
* ingesting visible conversation is a deliberate host decision.
|
|
288
|
+
*/
|
|
289
|
+
export function historyIntakeEnabled(profile: Profile): boolean {
|
|
290
|
+
const value = readSettings(profile).historyIntake;
|
|
291
|
+
return value === true || value === "enabled";
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export interface ProfileHistoryBindingOptions {
|
|
295
|
+
/** Adapter construction options; defaults to `defaultCwd` when no directory is set. */
|
|
296
|
+
options?: OpenCodeAdapterOptions;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Build the profile for a history read by layering the engine's effective
|
|
301
|
+
* profile over the adapter's default execution directory, so a directory-less
|
|
302
|
+
* history read resolves like a turn rather than failing to connect.
|
|
303
|
+
*/
|
|
304
|
+
export function historyProfile(
|
|
305
|
+
profile: Profile,
|
|
306
|
+
options: ProfileHistoryBindingOptions = {},
|
|
307
|
+
): Profile {
|
|
308
|
+
const fallback = options.options?.defaultCwd;
|
|
309
|
+
if (fallback === undefined) return profile;
|
|
310
|
+
if (resolveDirectory(profile, {}) !== undefined) {
|
|
311
|
+
return profile;
|
|
312
|
+
}
|
|
313
|
+
return { ...profile, settings: { ...profile.settings, directory: fallback } };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The literal `"disabled"` the core resolver understands. Return it, or place it
|
|
318
|
+
* on the adapter as `history`, to record that collection is deliberately off
|
|
319
|
+
* rather than looking like an empty transcript.
|
|
320
|
+
*/
|
|
321
|
+
export function historyDisabled(): "disabled" {
|
|
322
|
+
return "disabled";
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Minimal hook for the engine. `adapter` is the live adapter (anything with
|
|
327
|
+
* `inspectTranscript`), `profile` is the effective profile already used for
|
|
328
|
+
* `open`/`turn`. Returns an object the existing core flow accepts:
|
|
329
|
+
* `resolveHistoryCapability` then `readProviderHistory`. When intake is off the
|
|
330
|
+
* result is the literal `"disabled"` and nothing is read.
|
|
331
|
+
*/
|
|
332
|
+
export function bindHistoryCapability(
|
|
333
|
+
adapter: ProviderHistorySource,
|
|
334
|
+
profile: Profile,
|
|
335
|
+
options: ProfileHistoryBindingOptions = {},
|
|
336
|
+
): ProviderHistoryCapability | "disabled" {
|
|
337
|
+
if (!historyIntakeEnabled(profile)) return historyDisabled();
|
|
338
|
+
return createProviderHistoryCapability(
|
|
339
|
+
adapter,
|
|
340
|
+
historyProfile(profile, options),
|
|
341
|
+
);
|
|
342
|
+
}
|