openlayer 0.16.2 → 0.17.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/CHANGELOG.md +39 -0
- package/_shims/bun-runtime.js +1 -2
- package/_shims/bun-runtime.js.map +1 -1
- package/_shims/node-runtime.js +18 -9
- package/_shims/node-runtime.js.map +1 -1
- package/_shims/node-runtime.mjs.map +1 -1
- package/_shims/registry.js +2 -2
- package/_shims/registry.js.map +1 -1
- package/_shims/registry.mjs.map +1 -1
- package/_shims/web-runtime.js +1 -2
- package/_shims/web-runtime.js.map +1 -1
- package/_shims/web-runtime.mjs.map +1 -1
- package/core.d.ts +2 -4
- package/core.d.ts.map +1 -1
- package/core.js +9 -9
- package/core.js.map +1 -1
- package/core.mjs +4 -4
- package/core.mjs.map +1 -1
- package/error.js.map +1 -1
- package/error.mjs.map +1 -1
- package/index.js +17 -7
- package/index.js.map +1 -1
- package/index.mjs.map +1 -1
- package/internal/qs/stringify.js +1 -2
- package/internal/qs/stringify.js.map +1 -1
- package/internal/qs/stringify.mjs.map +1 -1
- package/internal/qs/utils.js +9 -9
- package/internal/qs/utils.js.map +1 -1
- package/internal/qs/utils.mjs.map +1 -1
- package/lib/core/cli.js +17 -7
- package/lib/core/cli.js.map +1 -1
- package/lib/core/cli.mjs.map +1 -1
- package/lib/core/openai-monitor.d.ts.map +1 -1
- package/lib/core/openai-monitor.js +1 -1
- package/lib/core/openai-monitor.js.map +1 -1
- package/lib/core/openai-monitor.mjs +1 -1
- package/lib/core/openai-monitor.mjs.map +1 -1
- package/lib/integrations/bedrockAgentTracer.js +49 -50
- package/lib/integrations/bedrockAgentTracer.js.map +1 -1
- package/lib/integrations/bedrockAgentTracer.mjs +48 -48
- package/lib/integrations/bedrockAgentTracer.mjs.map +1 -1
- package/lib/integrations/index.d.ts +5 -0
- package/lib/integrations/index.d.ts.map +1 -0
- package/lib/integrations/index.js +21 -0
- package/lib/integrations/index.js.map +1 -0
- package/lib/integrations/index.mjs +5 -0
- package/lib/integrations/index.mjs.map +1 -0
- package/lib/integrations/langchainCallback.d.ts +83 -27
- package/lib/integrations/langchainCallback.d.ts.map +1 -1
- package/lib/integrations/langchainCallback.js +597 -88
- package/lib/integrations/langchainCallback.js.map +1 -1
- package/lib/integrations/langchainCallback.mjs +599 -87
- package/lib/integrations/langchainCallback.mjs.map +1 -1
- package/lib/integrations/openAiTracer.js +1 -2
- package/lib/integrations/openAiTracer.js.map +1 -1
- package/lib/integrations/openAiTracer.mjs.map +1 -1
- package/lib/integrations/tracedTool.d.ts +66 -0
- package/lib/integrations/tracedTool.d.ts.map +1 -0
- package/lib/integrations/tracedTool.js +186 -0
- package/lib/integrations/tracedTool.js.map +1 -0
- package/lib/integrations/tracedTool.mjs +149 -0
- package/lib/integrations/tracedTool.mjs.map +1 -0
- package/lib/tracing/steps.d.ts +120 -1
- package/lib/tracing/steps.d.ts.map +1 -1
- package/lib/tracing/steps.js +128 -4
- package/lib/tracing/steps.js.map +1 -1
- package/lib/tracing/steps.mjs +119 -1
- package/lib/tracing/steps.mjs.map +1 -1
- package/lib/tracing/tracer.d.ts +69 -1
- package/lib/tracing/tracer.d.ts.map +1 -1
- package/lib/tracing/tracer.js +92 -18
- package/lib/tracing/tracer.js.map +1 -1
- package/lib/tracing/tracer.mjs +78 -10
- package/lib/tracing/tracer.mjs.map +1 -1
- package/package.json +3 -1
- package/resources/commits/commits.js +17 -7
- package/resources/commits/commits.js.map +1 -1
- package/resources/commits/test-results.js.map +1 -1
- package/resources/commits/test-results.mjs.map +1 -1
- package/resources/inference-pipelines/data.d.ts +8 -0
- package/resources/inference-pipelines/data.d.ts.map +1 -1
- package/resources/inference-pipelines/inference-pipelines.js +17 -7
- package/resources/inference-pipelines/inference-pipelines.js.map +1 -1
- package/resources/inference-pipelines/inference-pipelines.mjs.map +1 -1
- package/resources/inference-pipelines/test-results.js.map +1 -1
- package/resources/inference-pipelines/test-results.mjs.map +1 -1
- package/resources/projects/commits.js.map +1 -1
- package/resources/projects/commits.mjs.map +1 -1
- package/resources/projects/inference-pipelines.js.map +1 -1
- package/resources/projects/inference-pipelines.mjs.map +1 -1
- package/resources/projects/projects.js +17 -7
- package/resources/projects/projects.js.map +1 -1
- package/resources/projects/projects.mjs.map +1 -1
- package/resources/projects/tests.js.map +1 -1
- package/resources/projects/tests.mjs.map +1 -1
- package/resources/storage/storage.js +17 -7
- package/resources/storage/storage.js.map +1 -1
- package/shims/node.d.ts +0 -4
- package/shims/node.d.ts.map +1 -1
- package/shims/node.js +0 -25
- package/shims/node.js.map +1 -1
- package/shims/node.mjs +0 -1
- package/shims/node.mjs.map +1 -1
- package/shims/web.js +0 -25
- package/shims/web.js.map +1 -1
- package/shims/web.mjs +0 -1
- package/shims/web.mjs.map +1 -1
- package/src/core.ts +4 -4
- package/src/lib/core/openai-monitor.ts +1 -1
- package/src/lib/integrations/index.ts +4 -0
- package/src/lib/integrations/langchainCallback.ts +802 -88
- package/src/lib/integrations/tracedTool.ts +175 -0
- package/src/lib/tracing/index.d.ts +10 -0
- package/src/lib/tracing/steps.ts +218 -5
- package/src/lib/tracing/tracer.ts +153 -9
- package/src/resources/inference-pipelines/data.ts +10 -0
- package/src/uploads.ts +3 -2
- package/src/version.ts +1 -1
- package/uploads.d.ts +2 -2
- package/uploads.d.ts.map +1 -1
- package/uploads.js +3 -4
- package/uploads.js.map +1 -1
- package/uploads.mjs +1 -2
- package/uploads.mjs.map +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/lib/tracing/enums.d.ts +0 -5
- package/lib/tracing/enums.d.ts.map +0 -1
- package/lib/tracing/enums.js +0 -9
- package/lib/tracing/enums.js.map +0 -1
- package/lib/tracing/enums.mjs +0 -6
- package/lib/tracing/enums.mjs.map +0 -1
- package/src/lib/tracing/enums.ts +0 -4
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { tool } from '@langchain/core/tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { addFunctionCallStepToTrace } from '../tracing/tracer';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Enhanced tool wrapper that automatically creates function call traces.
|
|
7
|
+
*
|
|
8
|
+
* This wrapper automatically creates StepType.FUNCTION_CALL steps for any tool invocation,
|
|
9
|
+
* providing detailed tracing of function calls within agent workflows.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { tracedTool } from 'openlayer/lib/integrations/tracedTool';
|
|
14
|
+
*
|
|
15
|
+
* const weatherTool = tracedTool(
|
|
16
|
+
* async ({ city }) => {
|
|
17
|
+
* const weather = await getWeatherAPI(city);
|
|
18
|
+
* return weather;
|
|
19
|
+
* },
|
|
20
|
+
* {
|
|
21
|
+
* name: "get_weather",
|
|
22
|
+
* description: "Get weather for a city",
|
|
23
|
+
* schema: z.object({ city: z.string() }),
|
|
24
|
+
* metadata: {
|
|
25
|
+
* provider: "weather_api",
|
|
26
|
+
* category: "external_api"
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* );
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function tracedTool<T extends Record<string, any>>(
|
|
33
|
+
func: (input: T) => Promise<string> | string,
|
|
34
|
+
config: {
|
|
35
|
+
name: string;
|
|
36
|
+
description: string;
|
|
37
|
+
schema: z.ZodSchema<T>;
|
|
38
|
+
metadata?: Record<string, any>;
|
|
39
|
+
responseSchema?: z.ZodSchema<any>;
|
|
40
|
+
},
|
|
41
|
+
) {
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
return (tool as any)(
|
|
44
|
+
async (input: unknown) => {
|
|
45
|
+
const typedInput = input as T;
|
|
46
|
+
// Create a traced function call step (prefix will be added automatically)
|
|
47
|
+
const { step: functionStep, endStep: endFunctionStep } = addFunctionCallStepToTrace({
|
|
48
|
+
name: config.name,
|
|
49
|
+
functionName: config.name,
|
|
50
|
+
arguments: typedInput,
|
|
51
|
+
metadata: {
|
|
52
|
+
...config.metadata,
|
|
53
|
+
toolType: 'traced_tool',
|
|
54
|
+
executionContext: 'auto_traced',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
// Execute the original function
|
|
60
|
+
const result = await func(typedInput);
|
|
61
|
+
|
|
62
|
+
// Update function step with result (timing handled automatically by step framework)
|
|
63
|
+
functionStep.log({
|
|
64
|
+
output: result,
|
|
65
|
+
metadata: {
|
|
66
|
+
...functionStep.metadata,
|
|
67
|
+
outputLength: typeof result === 'string' ? result.length : JSON.stringify(result).length,
|
|
68
|
+
success: true,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
// Log error in function step
|
|
75
|
+
functionStep.log({
|
|
76
|
+
metadata: {
|
|
77
|
+
...functionStep.metadata,
|
|
78
|
+
error: error instanceof Error ? error.message : String(error),
|
|
79
|
+
errorType: error instanceof Error ? error.constructor.name : 'Unknown',
|
|
80
|
+
success: false,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
throw error;
|
|
84
|
+
} finally {
|
|
85
|
+
endFunctionStep();
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: config.name,
|
|
90
|
+
description: config.description,
|
|
91
|
+
schema: config.schema,
|
|
92
|
+
...(config.responseSchema && { responseSchema: config.responseSchema }),
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Enhanced agent wrapper that automatically creates agent traces.
|
|
99
|
+
*
|
|
100
|
+
* This wrapper automatically creates StepType.AGENT steps for agent functions,
|
|
101
|
+
* providing clear agent boundaries in traces.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import { tracedAgent } from 'openlayer/lib/integrations/tracedTool';
|
|
106
|
+
*
|
|
107
|
+
* const researchAgent = tracedAgent(
|
|
108
|
+
* async (query) => {
|
|
109
|
+
* // Agent logic here
|
|
110
|
+
* return result;
|
|
111
|
+
* },
|
|
112
|
+
* {
|
|
113
|
+
* name: "Research Agent",
|
|
114
|
+
* agentType: "research_specialist",
|
|
115
|
+
* version: "1.0.0"
|
|
116
|
+
* }
|
|
117
|
+
* );
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export function tracedAgent<T, R>(
|
|
121
|
+
agentFunc: (input: T) => Promise<R> | R,
|
|
122
|
+
config: {
|
|
123
|
+
name: string;
|
|
124
|
+
agentType: string;
|
|
125
|
+
version?: string;
|
|
126
|
+
metadata?: Record<string, any>;
|
|
127
|
+
},
|
|
128
|
+
) {
|
|
129
|
+
return async (input: T): Promise<R> => {
|
|
130
|
+
const { startAgentStep } = await import('../tracing/tracer');
|
|
131
|
+
|
|
132
|
+
// Create an agent step (prefix will be added automatically)
|
|
133
|
+
const { step: agentStep, endStep: endAgentStep } = startAgentStep({
|
|
134
|
+
name: config.name,
|
|
135
|
+
agentType: config.agentType,
|
|
136
|
+
inputs: input,
|
|
137
|
+
metadata: {
|
|
138
|
+
...config.metadata,
|
|
139
|
+
agentVersion: config.version || '1.0.0',
|
|
140
|
+
executionContext: 'traced_agent',
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
// Execute the agent function
|
|
146
|
+
const result = await agentFunc(input);
|
|
147
|
+
|
|
148
|
+
// Update agent step with result (timing handled automatically by step framework)
|
|
149
|
+
agentStep.log({
|
|
150
|
+
output: result,
|
|
151
|
+
metadata: {
|
|
152
|
+
...agentStep.metadata,
|
|
153
|
+
success: true,
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return result;
|
|
158
|
+
} catch (error) {
|
|
159
|
+
// Log error in agent step
|
|
160
|
+
agentStep.log({
|
|
161
|
+
metadata: {
|
|
162
|
+
...agentStep.metadata,
|
|
163
|
+
error: error instanceof Error ? error.message : String(error),
|
|
164
|
+
errorType: error instanceof Error ? error.constructor.name : 'Unknown',
|
|
165
|
+
success: false,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
throw error;
|
|
169
|
+
} finally {
|
|
170
|
+
endAgentStep();
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export default tracedTool;
|
|
@@ -1 +1,11 @@
|
|
|
1
1
|
export * as tracer from './tracer';
|
|
2
|
+
export {
|
|
3
|
+
addChatCompletionStepToTrace,
|
|
4
|
+
addChainStepToTrace,
|
|
5
|
+
addAgentStepToTrace,
|
|
6
|
+
addToolStepToTrace,
|
|
7
|
+
addRetrieverStepToTrace,
|
|
8
|
+
addFunctionCallStepToTrace,
|
|
9
|
+
startAgentStep,
|
|
10
|
+
addHandoffStepToTrace,
|
|
11
|
+
} from './tracer';
|
package/src/lib/tracing/steps.ts
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module with the different Step classes and helper functions that can be used in a trace.
|
|
3
|
+
*
|
|
4
|
+
* This is the consolidated module for all step-related functionality in the Openlayer SDK,
|
|
5
|
+
* similar to the Python SDK's steps.py module.
|
|
6
|
+
*/
|
|
7
|
+
|
|
1
8
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
|
|
9
|
+
|
|
10
|
+
// ============================= ENUMS ============================= //
|
|
11
|
+
|
|
12
|
+
export enum StepType {
|
|
13
|
+
USER_CALL = 'user_call',
|
|
14
|
+
CHAT_COMPLETION = 'chat_completion',
|
|
15
|
+
CHAIN = 'chain',
|
|
16
|
+
AGENT = 'agent',
|
|
17
|
+
TOOL = 'tool',
|
|
18
|
+
RETRIEVER = 'retriever',
|
|
19
|
+
FUNCTION_CALL = 'function_call',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ============================= INTERFACES ============================= //
|
|
3
23
|
|
|
4
24
|
export interface StepData {
|
|
5
25
|
name: string;
|
|
@@ -25,6 +45,15 @@ export interface ChatCompletionStepData extends StepData {
|
|
|
25
45
|
modelParameters: Record<string, any> | null;
|
|
26
46
|
}
|
|
27
47
|
|
|
48
|
+
// ============================= STEP CLASSES ============================= //
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Step, defined as a single function call being traced.
|
|
52
|
+
*
|
|
53
|
+
* This is the base class for all the different types of steps that can be
|
|
54
|
+
* used in a trace. Steps can also contain nested steps, which represent
|
|
55
|
+
* function calls made within the parent step.
|
|
56
|
+
*/
|
|
28
57
|
export class Step {
|
|
29
58
|
name: string;
|
|
30
59
|
id: string;
|
|
@@ -38,21 +67,33 @@ export class Step {
|
|
|
38
67
|
latency: number | null = null;
|
|
39
68
|
steps: Step[] = [];
|
|
40
69
|
|
|
41
|
-
constructor(
|
|
70
|
+
constructor(
|
|
71
|
+
name: string,
|
|
72
|
+
inputs: any = null,
|
|
73
|
+
output: any = null,
|
|
74
|
+
metadata: Record<string, any> = {},
|
|
75
|
+
startTime?: number | null,
|
|
76
|
+
endTime?: number | null,
|
|
77
|
+
) {
|
|
42
78
|
this.name = name;
|
|
43
79
|
this.id = uuidv4();
|
|
44
80
|
this.inputs = inputs;
|
|
45
81
|
this.output = output;
|
|
46
82
|
this.metadata = metadata;
|
|
47
|
-
|
|
48
83
|
this.startTime = startTime ?? Date.now();
|
|
49
84
|
this.endTime = endTime ?? null;
|
|
50
85
|
}
|
|
51
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Adds a nested step to the current step.
|
|
89
|
+
*/
|
|
52
90
|
addNestedStep(nestedStep: Step): void {
|
|
53
91
|
this.steps.push(nestedStep);
|
|
54
92
|
}
|
|
55
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Logs step data.
|
|
96
|
+
*/
|
|
56
97
|
log(data: Partial<Record<keyof this, any>>): void {
|
|
57
98
|
Object.keys(data).forEach((key) => {
|
|
58
99
|
if (key in this) {
|
|
@@ -62,6 +103,9 @@ export class Step {
|
|
|
62
103
|
});
|
|
63
104
|
}
|
|
64
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Dictionary representation of the Step.
|
|
108
|
+
*/
|
|
65
109
|
toJSON(): StepData {
|
|
66
110
|
return {
|
|
67
111
|
name: this.name,
|
|
@@ -79,13 +123,26 @@ export class Step {
|
|
|
79
123
|
}
|
|
80
124
|
}
|
|
81
125
|
|
|
126
|
+
/**
|
|
127
|
+
* User call step represents a generic user call in the trace.
|
|
128
|
+
*/
|
|
82
129
|
export class UserCallStep extends Step {
|
|
83
|
-
constructor(
|
|
130
|
+
constructor(
|
|
131
|
+
name: string,
|
|
132
|
+
inputs: any = null,
|
|
133
|
+
output: any = null,
|
|
134
|
+
metadata: Record<string, any> = {},
|
|
135
|
+
startTime?: number | null,
|
|
136
|
+
endTime?: number | null,
|
|
137
|
+
) {
|
|
84
138
|
super(name, inputs, output, metadata, startTime, endTime);
|
|
85
139
|
this.stepType = StepType.USER_CALL;
|
|
86
140
|
}
|
|
87
141
|
}
|
|
88
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Chat completion step represents an LLM chat completion in the trace.
|
|
145
|
+
*/
|
|
89
146
|
export class ChatCompletionStep extends Step {
|
|
90
147
|
provider: string | null = null;
|
|
91
148
|
promptTokens: number | null = null;
|
|
@@ -95,7 +152,14 @@ export class ChatCompletionStep extends Step {
|
|
|
95
152
|
model: string | null = null;
|
|
96
153
|
modelParameters: Record<string, any> | null = null;
|
|
97
154
|
|
|
98
|
-
|
|
155
|
+
constructor(
|
|
156
|
+
name: string,
|
|
157
|
+
inputs: any = null,
|
|
158
|
+
output: any = null,
|
|
159
|
+
metadata: Record<string, any> = {},
|
|
160
|
+
startTime?: number | null,
|
|
161
|
+
endTime?: number | null,
|
|
162
|
+
) {
|
|
99
163
|
super(name, inputs, output, metadata, startTime, endTime);
|
|
100
164
|
this.stepType = StepType.CHAT_COMPLETION;
|
|
101
165
|
}
|
|
@@ -114,3 +178,152 @@ export class ChatCompletionStep extends Step {
|
|
|
114
178
|
};
|
|
115
179
|
}
|
|
116
180
|
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Chain step represents sequential operations or workflow chains in AI applications.
|
|
184
|
+
*/
|
|
185
|
+
export class ChainStep extends Step {
|
|
186
|
+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
|
|
187
|
+
super(name, inputs, output, metadata);
|
|
188
|
+
this.stepType = StepType.CHAIN;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Agent step tracks AI agent actions and decision-making processes.
|
|
194
|
+
*/
|
|
195
|
+
export class AgentStep extends Step {
|
|
196
|
+
tool: string | null = null;
|
|
197
|
+
action: any = null;
|
|
198
|
+
|
|
199
|
+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
|
|
200
|
+
super(name, inputs, output, metadata);
|
|
201
|
+
this.stepType = StepType.AGENT;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Tool step tracks individual tool executions within AI workflows.
|
|
207
|
+
*/
|
|
208
|
+
export class ToolStep extends Step {
|
|
209
|
+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
|
|
210
|
+
super(name, inputs, output, metadata);
|
|
211
|
+
this.stepType = StepType.TOOL;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Retriever step tracks document retrieval and search operations.
|
|
217
|
+
*/
|
|
218
|
+
export class RetrieverStep extends Step {
|
|
219
|
+
documents: any[] = [];
|
|
220
|
+
|
|
221
|
+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
|
|
222
|
+
super(name, inputs, output, metadata);
|
|
223
|
+
this.stepType = StepType.RETRIEVER;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Function call step tracks specific function calls with detailed execution metadata.
|
|
229
|
+
*/
|
|
230
|
+
export class FunctionCallStep extends Step {
|
|
231
|
+
functionName: string | null = null;
|
|
232
|
+
arguments: Record<string, any> | null = null;
|
|
233
|
+
result: any = null;
|
|
234
|
+
executionTime: number | null = null;
|
|
235
|
+
|
|
236
|
+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
|
|
237
|
+
super(name, inputs, output, metadata);
|
|
238
|
+
this.stepType = StepType.FUNCTION_CALL;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ============================= FACTORY FUNCTION ============================= //
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Factory function to create a step based on the step type.
|
|
246
|
+
*/
|
|
247
|
+
export function stepFactory(
|
|
248
|
+
stepType: StepType,
|
|
249
|
+
name: string,
|
|
250
|
+
inputs: any = null,
|
|
251
|
+
output: any = null,
|
|
252
|
+
metadata: Record<string, any> = {},
|
|
253
|
+
startTime?: number | null,
|
|
254
|
+
endTime?: number | null,
|
|
255
|
+
): Step {
|
|
256
|
+
if (!Object.values(StepType).includes(stepType)) {
|
|
257
|
+
throw new Error(`Step type ${stepType} not recognized.`);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
switch (stepType) {
|
|
261
|
+
case StepType.USER_CALL:
|
|
262
|
+
return new UserCallStep(name, inputs, output, metadata, startTime, endTime);
|
|
263
|
+
case StepType.CHAT_COMPLETION:
|
|
264
|
+
return new ChatCompletionStep(name, inputs, output, metadata, startTime, endTime);
|
|
265
|
+
case StepType.CHAIN:
|
|
266
|
+
return new ChainStep(name, inputs, output, metadata);
|
|
267
|
+
case StepType.AGENT:
|
|
268
|
+
return new AgentStep(name, inputs, output, metadata);
|
|
269
|
+
case StepType.TOOL:
|
|
270
|
+
return new ToolStep(name, inputs, output, metadata);
|
|
271
|
+
case StepType.RETRIEVER:
|
|
272
|
+
return new RetrieverStep(name, inputs, output, metadata);
|
|
273
|
+
case StepType.FUNCTION_CALL:
|
|
274
|
+
return new FunctionCallStep(name, inputs, output, metadata);
|
|
275
|
+
default:
|
|
276
|
+
throw new Error(`Step type ${stepType} not recognized.`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ============================= HELPER FUNCTION INTERFACES ============================= //
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Interface for step creation parameters used by helper functions
|
|
284
|
+
*/
|
|
285
|
+
export interface StepCreationParams {
|
|
286
|
+
name: string;
|
|
287
|
+
inputs?: any;
|
|
288
|
+
output?: any;
|
|
289
|
+
metadata?: Record<string, any>;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export interface ChatCompletionStepParams extends StepCreationParams {
|
|
293
|
+
latency: number;
|
|
294
|
+
tokens?: number | null;
|
|
295
|
+
promptTokens?: number | null;
|
|
296
|
+
completionTokens?: number | null;
|
|
297
|
+
model?: string | null;
|
|
298
|
+
modelParameters?: Record<string, any> | null;
|
|
299
|
+
rawOutput?: string | null;
|
|
300
|
+
provider?: string;
|
|
301
|
+
startTime?: number | null;
|
|
302
|
+
endTime?: number | null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export interface AgentStepParams extends StepCreationParams {
|
|
306
|
+
tool?: string;
|
|
307
|
+
action?: any;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export interface RetrieverStepParams extends StepCreationParams {
|
|
311
|
+
documents?: any[];
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export interface FunctionCallStepParams extends StepCreationParams {
|
|
315
|
+
functionName: string;
|
|
316
|
+
arguments: Record<string, any>;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export interface EnhancedAgentStepParams extends StepCreationParams {
|
|
320
|
+
agentType: string;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export interface HandoffStepParams {
|
|
324
|
+
name: string;
|
|
325
|
+
fromComponent: string;
|
|
326
|
+
toComponent: string;
|
|
327
|
+
handoffData: any;
|
|
328
|
+
metadata?: Record<string, any>;
|
|
329
|
+
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
// tracing/tracer.ts
|
|
2
2
|
|
|
3
|
-
import Openlayer from '../../index';
|
|
4
|
-
import { StepType } from './enums';
|
|
5
|
-
import { ChatCompletionStep, Step, UserCallStep } from './steps';
|
|
6
3
|
import { Trace } from './traces';
|
|
4
|
+
import {
|
|
5
|
+
Step,
|
|
6
|
+
ChatCompletionStep,
|
|
7
|
+
AgentStep,
|
|
8
|
+
RetrieverStep,
|
|
9
|
+
FunctionCallStep,
|
|
10
|
+
StepType,
|
|
11
|
+
stepFactory,
|
|
12
|
+
} from './steps';
|
|
13
|
+
import Openlayer from '../../index';
|
|
7
14
|
|
|
8
15
|
let currentTrace: Trace | null = null;
|
|
9
16
|
|
|
@@ -36,12 +43,7 @@ function createStep(
|
|
|
36
43
|
openlayerInferencePipelineId?: string,
|
|
37
44
|
): [Step, () => void] {
|
|
38
45
|
metadata = metadata || {};
|
|
39
|
-
|
|
40
|
-
if (stepType === StepType.CHAT_COMPLETION) {
|
|
41
|
-
newStep = new ChatCompletionStep(name, inputs, output, metadata, startTime, endTime);
|
|
42
|
-
} else {
|
|
43
|
-
newStep = new UserCallStep(name, inputs, output, metadata, startTime, endTime);
|
|
44
|
-
}
|
|
46
|
+
const newStep = stepFactory(stepType, name, inputs, output, metadata, startTime, endTime);
|
|
45
47
|
|
|
46
48
|
const inferencePipelineId = openlayerInferencePipelineId || process.env['OPENLAYER_INFERENCE_PIPELINE_ID'];
|
|
47
49
|
|
|
@@ -63,7 +65,19 @@ function createStep(
|
|
|
63
65
|
stepStack.push(newStep);
|
|
64
66
|
|
|
65
67
|
const endStep = () => {
|
|
68
|
+
// Calculate latency for this step before removing from stack
|
|
69
|
+
newStep.endTime = Date.now();
|
|
70
|
+
// Only calculate latency if it hasn't been manually set
|
|
71
|
+
if (newStep.latency === null) {
|
|
72
|
+
if (newStep.startTime && newStep.endTime) {
|
|
73
|
+
newStep.latency = newStep.endTime - newStep.startTime;
|
|
74
|
+
} else {
|
|
75
|
+
newStep.latency = 0; // Fallback to 0 if timestamps are missing
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
66
79
|
stepStack.pop(); // Remove the current step from the stack
|
|
80
|
+
console.debug(`Ending step ${newStep.name}`);
|
|
67
81
|
|
|
68
82
|
if (isRootStep) {
|
|
69
83
|
console.debug('Ending the trace...');
|
|
@@ -225,6 +239,136 @@ export function addChatCompletionStepToTrace(
|
|
|
225
239
|
endStep();
|
|
226
240
|
}
|
|
227
241
|
|
|
242
|
+
export function addChainStepToTrace(params: {
|
|
243
|
+
name: string;
|
|
244
|
+
inputs: any;
|
|
245
|
+
output?: any;
|
|
246
|
+
metadata?: Record<string, any>;
|
|
247
|
+
}) {
|
|
248
|
+
// Add "Handoffs: " prefix for chain steps (representing workflow transitions)
|
|
249
|
+
const stepName = `Handoffs: ${params.name}`;
|
|
250
|
+
const [step, endStep] = createStep(stepName, StepType.CHAIN, params.inputs, params.output, params.metadata);
|
|
251
|
+
return { step, endStep };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function addAgentStepToTrace(params: {
|
|
255
|
+
name: string;
|
|
256
|
+
inputs: any;
|
|
257
|
+
output?: any;
|
|
258
|
+
metadata?: Record<string, any>;
|
|
259
|
+
tool?: string;
|
|
260
|
+
action?: any;
|
|
261
|
+
}) {
|
|
262
|
+
// Add "Agent: " prefix for agent steps
|
|
263
|
+
const stepName = `Agent: ${params.name}`;
|
|
264
|
+
const [step, endStep] = createStep(stepName, StepType.AGENT, params.inputs, params.output, params.metadata);
|
|
265
|
+
if (step instanceof AgentStep) {
|
|
266
|
+
step.tool = params.tool || null;
|
|
267
|
+
step.action = params.action || null;
|
|
268
|
+
}
|
|
269
|
+
return { step, endStep };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function addToolStepToTrace(params: {
|
|
273
|
+
name: string;
|
|
274
|
+
inputs: any;
|
|
275
|
+
output?: any;
|
|
276
|
+
metadata?: Record<string, any>;
|
|
277
|
+
}) {
|
|
278
|
+
const [step, endStep] = createStep(
|
|
279
|
+
params.name,
|
|
280
|
+
StepType.TOOL,
|
|
281
|
+
params.inputs,
|
|
282
|
+
params.output,
|
|
283
|
+
params.metadata,
|
|
284
|
+
);
|
|
285
|
+
return { step, endStep };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function addRetrieverStepToTrace(params: {
|
|
289
|
+
name: string;
|
|
290
|
+
inputs: any;
|
|
291
|
+
output?: any;
|
|
292
|
+
metadata?: Record<string, any>;
|
|
293
|
+
documents?: any[];
|
|
294
|
+
}) {
|
|
295
|
+
const [step, endStep] = createStep(
|
|
296
|
+
params.name,
|
|
297
|
+
StepType.RETRIEVER,
|
|
298
|
+
params.inputs,
|
|
299
|
+
params.output,
|
|
300
|
+
params.metadata,
|
|
301
|
+
);
|
|
302
|
+
if (step instanceof RetrieverStep) {
|
|
303
|
+
step.documents = params.documents || [];
|
|
304
|
+
}
|
|
305
|
+
return { step, endStep };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function addFunctionCallStepToTrace(params: {
|
|
309
|
+
name: string;
|
|
310
|
+
functionName: string;
|
|
311
|
+
arguments: Record<string, any>;
|
|
312
|
+
output?: any;
|
|
313
|
+
metadata?: Record<string, any>;
|
|
314
|
+
}) {
|
|
315
|
+
// Add "Tool Call: " prefix for better dashboard visibility
|
|
316
|
+
const stepName = `Tool Call: ${params.name}`;
|
|
317
|
+
const [step, endStep] = createStep(
|
|
318
|
+
stepName,
|
|
319
|
+
StepType.FUNCTION_CALL,
|
|
320
|
+
params.arguments,
|
|
321
|
+
params.output,
|
|
322
|
+
params.metadata,
|
|
323
|
+
);
|
|
324
|
+
if (step instanceof FunctionCallStep) {
|
|
325
|
+
step.functionName = params.functionName;
|
|
326
|
+
step.arguments = params.arguments;
|
|
327
|
+
step.result = params.output;
|
|
328
|
+
}
|
|
329
|
+
return { step, endStep };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Enhanced agent step creator with nested LLM tracking
|
|
333
|
+
export function startAgentStep(params: {
|
|
334
|
+
name: string;
|
|
335
|
+
agentType: string;
|
|
336
|
+
inputs: any;
|
|
337
|
+
metadata?: Record<string, any>;
|
|
338
|
+
}) {
|
|
339
|
+
// Add "Agent: " prefix for better dashboard visibility
|
|
340
|
+
const stepName = `Agent: ${params.name}`;
|
|
341
|
+
const [step, endStep] = createStep(stepName, StepType.AGENT, params.inputs, undefined, {
|
|
342
|
+
...params.metadata,
|
|
343
|
+
agentType: params.agentType,
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
if (step instanceof AgentStep) {
|
|
347
|
+
step.action = params.inputs;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return { step, endStep };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Explicit handoff function for agent-to-agent or component-to-component transitions
|
|
354
|
+
export function addHandoffStepToTrace(params: {
|
|
355
|
+
name: string;
|
|
356
|
+
fromComponent: string;
|
|
357
|
+
toComponent: string;
|
|
358
|
+
handoffData: any;
|
|
359
|
+
metadata?: Record<string, any>;
|
|
360
|
+
}) {
|
|
361
|
+
const stepName = `Handoffs: ${params.fromComponent} → ${params.toComponent}`;
|
|
362
|
+
const [step, endStep] = createStep(stepName, StepType.CHAIN, params.handoffData, undefined, {
|
|
363
|
+
...params.metadata,
|
|
364
|
+
handoffType: 'component_transition',
|
|
365
|
+
fromComponent: params.fromComponent,
|
|
366
|
+
toComponent: params.toComponent,
|
|
367
|
+
timestamp: new Date().toISOString(),
|
|
368
|
+
});
|
|
369
|
+
return { step, endStep };
|
|
370
|
+
}
|
|
371
|
+
|
|
228
372
|
export function postProcessTrace(traceObj: Trace): { traceData: any; inputVariableNames: string[] } {
|
|
229
373
|
const rootStep = traceObj.steps[0];
|
|
230
374
|
|
|
@@ -118,11 +118,21 @@ export namespace DataStreamParams {
|
|
|
118
118
|
*/
|
|
119
119
|
questionColumnName?: string;
|
|
120
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Name of the column with the session id.
|
|
123
|
+
*/
|
|
124
|
+
sessionIdColumnName?: string | null;
|
|
125
|
+
|
|
121
126
|
/**
|
|
122
127
|
* Name of the column with the timestamps. Timestamps must be in UNIX sec format.
|
|
123
128
|
* If not provided, the upload timestamp is used.
|
|
124
129
|
*/
|
|
125
130
|
timestampColumnName?: string;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Name of the column with the user id.
|
|
134
|
+
*/
|
|
135
|
+
userIdColumnName?: string | null;
|
|
126
136
|
}
|
|
127
137
|
|
|
128
138
|
export namespace LlmData {
|
package/src/uploads.ts
CHANGED
|
@@ -156,8 +156,9 @@ async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {
|
|
|
156
156
|
}
|
|
157
157
|
} else {
|
|
158
158
|
throw new Error(
|
|
159
|
-
`Unexpected data type: ${typeof value}; constructor: ${
|
|
160
|
-
?.name
|
|
159
|
+
`Unexpected data type: ${typeof value}; constructor: ${
|
|
160
|
+
value?.constructor?.name
|
|
161
|
+
}; props: ${propsForError(value)}`,
|
|
161
162
|
);
|
|
162
163
|
}
|
|
163
164
|
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.17.0'; // x-release-please-version
|
package/uploads.d.ts
CHANGED
|
@@ -69,7 +69,7 @@ export declare const isMultipartBody: (body: any) => body is MultipartBody;
|
|
|
69
69
|
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
70
70
|
* Otherwise returns the request as is.
|
|
71
71
|
*/
|
|
72
|
-
export declare const maybeMultipartFormRequestOptions: <T = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<
|
|
73
|
-
export declare const multipartFormRequestOptions: <T = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<
|
|
72
|
+
export declare const maybeMultipartFormRequestOptions: <T = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<T | MultipartBody>>;
|
|
73
|
+
export declare const multipartFormRequestOptions: <T = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<T | MultipartBody>>;
|
|
74
74
|
export declare const createForm: <T = Record<string, unknown>>(body: T | undefined) => Promise<FormData>;
|
|
75
75
|
//# sourceMappingURL=uploads.d.ts.map
|