Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
134 lines (121 loc) · 3.93 KB
/
Copy pathindex.ts
File metadata and controls
134 lines (121 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type { Config, Plugin } from "@opencode-ai/plugin";
import { version } from "../package.json";
import { loadConfig } from "./config.js";
export const ObserverPlugin: Plugin = async (input, options) => {
const config = loadConfig(options);
if (!config.enabled) {
return {};
}
const { createTelemetry } = await import("./telemetry/factory.js");
const { probeEndpoint } = await import("./telemetry/probe.js");
const { createCoordinator } = await import("./adapter/opencode/coordinator.js");
const { getOpenCodeVersion } = await import("./adapter/opencode/version.js");
const { resolveUser, isUserIDEnabled } = await import("./user/resolve.js");
const log = (
level: "info" | "warn" | "error",
message: string,
extra?: Record<string, unknown>,
) => {
// Isolate synchronous throws and async failures without delaying host callbacks.
void Promise.resolve()
.then(() =>
input.client.app.log({
signal: AbortSignal.timeout(1000),
body: { service: "opencode-observer", level, message, extra },
}),
)
.catch(() => undefined);
};
const logError = (error: unknown) =>
log("error", error instanceof Error ? error.message : "Trace processing failed");
const state = {
setup: undefined as Promise<void> | undefined,
coordinator: undefined as ReturnType<typeof createCoordinator> | undefined,
disposed: false,
probe: new AbortController(),
};
const initialize = async (providers: Config["provider"]) => {
const userIDEnabled = isUserIDEnabled();
const [serviceVersion, user] = await Promise.all([
getOpenCodeVersion(input.client).catch(() => undefined),
resolveUser(providers),
]);
if (state.disposed) {
return;
}
const observer = await createTelemetry({
...config,
serviceVersion,
spanAttributes: {
...(user === undefined ? {} : { "user.id": user?.id ?? "unknown" }),
...config.spanAttributes,
},
});
if (state.disposed) {
await observer.shutdown();
return;
}
state.coordinator = createCoordinator({
observer,
captureContent: config.captureContent,
captureHttpHeaders: config.captureHttpHeaders,
llmTimingMode: config.llmTimingMode,
userIdentity: { enabled: userIDEnabled, id: user?.id },
log: logError,
});
await state.coordinator.startSdkModelCapture().catch(logError);
if (state.disposed) {
return;
}
const url = new URL(config.endpoint);
const endpoint = `${url.origin}${url.pathname}`;
log("info", "Observer plugin initialized", {
version,
serviceVersion,
endpoint,
protocol: config.otlpProtocol,
});
void probeEndpoint(config.endpoint, state.probe.signal)
.then((result) => {
if (state.disposed) {
return;
}
log(
result.ok ? "info" : "warn",
result.ok
? "OTLP endpoint TCP reachable"
: "OTLP endpoint TCP unreachable; exports may fail",
{ endpoint, protocol: config.otlpProtocol, ms: result.ms, error: result.error },
);
})
.catch(logError);
};
return {
async config(hostConfig) {
if (state.disposed) {
return;
}
state.setup ??= initialize(hostConfig.provider);
await state.setup;
},
async dispose() {
state.disposed = true;
state.probe.abort();
const disposal = state.coordinator?.hooks.dispose();
await state.setup;
await disposal;
},
async "chat.message"(input, output) {
await state.coordinator?.hooks["chat.message"](input, output);
},
async "chat.params"(input, output) {
await state.coordinator?.hooks["chat.params"](input, output);
},
async "chat.headers"(input, output) {
await state.coordinator?.hooks["chat.headers"](input, output);
},
async event(input) {
await state.coordinator?.hooks.event(input);
},
};
};