Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/junior/src/chat/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function normalizeGenAiFinishReasons(value: unknown): unknown {
}

const contextStorage = new AsyncLocalStorage<LogAttributes>();
const spanStorage = new AsyncLocalStorage<unknown>();
const logRecordSinks = new Set<(record: EmittedLogRecord) => void>();
const deploymentLogAttributes = getDeploymentTelemetryAttributes();
type ConsoleTextStyle = Parameters<typeof styleText>[0];
Expand Down Expand Up @@ -1664,7 +1665,7 @@ export async function withSpan<T>(
...normalizedAttributes,
},
},
callback,
(span) => spanStorage.run(span, callback),
);
});
}
Expand Down Expand Up @@ -1694,7 +1695,7 @@ export function getTracePropagationHeaders(): TracePropagationHeaders {
/** Set attributes on the currently active Sentry span. */
export function setSpanAttributes(attributes: Record<string, unknown>): void {
const sentry = Sentry as unknown as { getActiveSpan?: () => unknown };
const span = sentry.getActiveSpan?.();
const span = sentry.getActiveSpan?.() ?? spanStorage.getStore();
if (!span) {
return;
}
Expand All @@ -1716,7 +1717,7 @@ export function setSpanAttributes(attributes: Record<string, unknown>): void {
/** Set the status of the currently active Sentry span. */
export function setSpanStatus(status: "ok" | "error"): void {
const sentry = Sentry as unknown as { getActiveSpan?: () => unknown };
const span = sentry.getActiveSpan?.();
const span = sentry.getActiveSpan?.() ?? spanStorage.getStore();
if (!span) {
return;
}
Expand Down
45 changes: 35 additions & 10 deletions packages/junior/src/chat/tool-support/pi-tool-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,40 @@ export function createPiAgentTools(
}
return true;
};
const isRecord = (value: unknown): value is Record<string, unknown> =>
Boolean(value && typeof value === "object" && !Array.isArray(value));
const contentEchoesDetails = (
content: ReturnType<typeof normalizeToolResult>["content"],
details: unknown,
): boolean => {
if (content.length !== 1 || content[0]?.type !== "text") {
return false;
}
try {
return (
JSON.stringify(JSON.parse(content[0].text)) === JSON.stringify(details)
);
} catch {
return false;
}
};
const toolResultTracePayload = (
normalized: ReturnType<typeof normalizeToolResult>,
) => {
const { content, details } = normalized;
if (isRecord(details) && details.rawResult !== undefined) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i need to f/u here im not sure why we have this rawResult thing

return details.rawResult;
}
if (
content.length > 0 &&
isRecord(details) &&
details.content === undefined &&
!contentEchoesDetails(content, details)
) {
return { ...details, content };
}
return details;
};
const executeDefinition = async (args: {
normalizedToolCallId: string | undefined;
params: Record<string, unknown>;
Expand Down Expand Up @@ -154,16 +188,7 @@ export function createPiAgentTools(
if (isSandbox && pluginAuthOrchestration) {
await pluginAuthOrchestration.maybeHandleAuthSignal(normalized.details);
}
let resultAttributeValue = normalized.details;
if (
normalized.details &&
typeof normalized.details === "object" &&
"rawResult" in normalized.details &&
(normalized.details as { rawResult?: unknown }).rawResult !== undefined
) {
resultAttributeValue = (normalized.details as { rawResult: unknown })
.rawResult;
}
const resultAttributeValue = toolResultTracePayload(normalized);
const toolResultAttribute = serializeToolPayload(resultAttributeValue);
if (toolResultAttribute) {
setSpanAttributes({
Expand Down
Loading