@friskai/frisk-js 0.3.0 → 0.3.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/dist/adapters/langchain/frisk-callback-handler.d.ts +80 -3
- package/dist/adapters/langchain/frisk-callback-handler.d.ts.map +1 -1
- package/dist/adapters/langchain/index.js +556 -238
- package/dist/adapters/langchain/index.js.map +1 -453
- package/dist/core/frisk-session.d.ts +43 -10
- package/dist/core/frisk-session.d.ts.map +1 -1
- package/dist/core/llm-reasoning/extractLlmReasoning.d.ts +8 -0
- package/dist/core/llm-reasoning/extractLlmReasoning.d.ts.map +1 -0
- package/dist/core/spans/frisk-span.d.ts +38 -0
- package/dist/core/spans/frisk-span.d.ts.map +1 -0
- package/dist/core/spans/index.d.ts +7 -0
- package/dist/core/spans/index.d.ts.map +1 -0
- package/dist/core/spans/llm-call-span.d.ts +23 -0
- package/dist/core/spans/llm-call-span.d.ts.map +1 -0
- package/dist/core/spans/operation-span.d.ts +13 -0
- package/dist/core/spans/operation-span.d.ts.map +1 -0
- package/dist/core/spans/session-span.d.ts +20 -0
- package/dist/core/spans/session-span.d.ts.map +1 -0
- package/dist/core/spans/tool-call-decision-span.d.ts +27 -0
- package/dist/core/spans/tool-call-decision-span.d.ts.map +1 -0
- package/dist/core/spans/tool-call-observation-span.d.ts +28 -0
- package/dist/core/spans/tool-call-observation-span.d.ts.map +1 -0
- package/dist/generated/sdk-meta.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +318 -171
- package/dist/index.js.map +1 -439
- package/dist/telemetry/constants.d.ts +24 -0
- package/dist/telemetry/constants.d.ts.map +1 -1
- package/package.json +2 -1
- package/dist/core/tool-call-span.d.ts +0 -102
- package/dist/core/tool-call-span.d.ts.map +0 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
2
2
|
import type { Serialized } from '@langchain/core/load/serializable';
|
|
3
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
4
|
+
import type { LLMResult } from '@langchain/core/outputs';
|
|
3
5
|
import type { ChainValues } from '@langchain/core/utils/types';
|
|
4
6
|
import { FriskSession } from '../../core/frisk-session';
|
|
5
7
|
import { FriskSDKLoggingOptions } from '../../logging';
|
|
@@ -10,7 +12,12 @@ export type LangchainToolCallStatus = 'error' | 'success';
|
|
|
10
12
|
*
|
|
11
13
|
* This handler integrates with LangChain's callback system to automatically
|
|
12
14
|
* manage tracing spans for Frisk sessions. It initializes tracing when a
|
|
13
|
-
* root chain starts and ends tracing when the root chain completes
|
|
15
|
+
* root chain starts and ends tracing when the root chain completes, and opens
|
|
16
|
+
* operation, LLM-call, and tool-call spans for the runs nested under it.
|
|
17
|
+
*
|
|
18
|
+
* Every span is keyed by the LangChain run id that owns it, so a child span
|
|
19
|
+
* nests under whatever span its `parentRunId` owns rather than all hanging off
|
|
20
|
+
* the session root.
|
|
14
21
|
*
|
|
15
22
|
* @example
|
|
16
23
|
* ```typescript
|
|
@@ -33,6 +40,7 @@ export declare class FriskCallbackHandler extends BaseCallbackHandler {
|
|
|
33
40
|
name: string;
|
|
34
41
|
private session;
|
|
35
42
|
private spansByRunId;
|
|
43
|
+
private llmCallSpanIdByToolCallId;
|
|
36
44
|
private frisk;
|
|
37
45
|
private logger;
|
|
38
46
|
constructor({ session, frisk, logging, }: {
|
|
@@ -42,7 +50,10 @@ export declare class FriskCallbackHandler extends BaseCallbackHandler {
|
|
|
42
50
|
});
|
|
43
51
|
/**
|
|
44
52
|
* Called at the start of a Chain run.
|
|
45
|
-
*
|
|
53
|
+
*
|
|
54
|
+
* Root chains (no parent) initialize tracing; nested chains open an operation
|
|
55
|
+
* span parented under their parent run's span. Either way the resulting span
|
|
56
|
+
* is registered by run id.
|
|
46
57
|
*
|
|
47
58
|
* NOTE: The official documentation and source code types state that `parentRunId` is the 8th argument to this method, but this isn't true.
|
|
48
59
|
* In practice, `parentRunId` is the 4th argument. This may be a documentation bug or an issue with the types, but we need to use the correct argument position to get the parent run ID.
|
|
@@ -50,14 +61,80 @@ export declare class FriskCallbackHandler extends BaseCallbackHandler {
|
|
|
50
61
|
handleChainStart(_chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string): void;
|
|
51
62
|
/**
|
|
52
63
|
* Called at the end of a Chain run.
|
|
53
|
-
*
|
|
64
|
+
*
|
|
65
|
+
* Ends tracing when the root chain completes (closing any spans still open
|
|
66
|
+
* and clearing the registries); otherwise ends the operation span for the run.
|
|
54
67
|
*/
|
|
55
68
|
handleChainEnd(_outputs: ChainValues, runId: string, _parentRunId?: string, _tags?: string[], _kwargs?: {
|
|
56
69
|
inputs?: Record<string, unknown>;
|
|
57
70
|
}): void;
|
|
71
|
+
/**
|
|
72
|
+
* Called at the start of a Chat Model run. Opens an LLM call span and records
|
|
73
|
+
* the prompt messages it is being asked to respond to.
|
|
74
|
+
*/
|
|
75
|
+
handleChatModelStart(_llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string): void;
|
|
76
|
+
/**
|
|
77
|
+
* Called at the start of a (completion) LLM run. Opens an LLM call span and
|
|
78
|
+
* records the raw prompt strings.
|
|
79
|
+
*/
|
|
80
|
+
handleLLMStart(_llm: Serialized, prompts: string[], runId: string, parentRunId?: string): void;
|
|
81
|
+
/**
|
|
82
|
+
* Called at the end of an LLM/Chat Model run. Records responses and requested
|
|
83
|
+
* tool calls, then closes the LLM call span.
|
|
84
|
+
*/
|
|
85
|
+
handleLLMEnd(output: LLMResult, runId: string): void;
|
|
86
|
+
/**
|
|
87
|
+
* Called when an LLM/Chat Model run errors. Records the error, then closes the
|
|
88
|
+
* LLM call span.
|
|
89
|
+
*/
|
|
90
|
+
handleLLMError(err: Error, runId: string): void;
|
|
58
91
|
handleToolStart(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runName?: string): Promise<void>;
|
|
59
92
|
handleToolEnd(output: any, runId: string, parentRunId?: string, tags?: string[]): Promise<void>;
|
|
60
93
|
handleToolError(err: Error, runId: string, parentRunId?: string, tags?: string[]): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Return the span tracked for `parentRunId`, or null when there isn't one.
|
|
96
|
+
*
|
|
97
|
+
* Because every span (session root included) is registered by its run id, a
|
|
98
|
+
* child created under `parentRunId` nests under whatever span that run owns.
|
|
99
|
+
* Null lets the session fall back to the root span.
|
|
100
|
+
*/
|
|
101
|
+
private resolveParentSpan;
|
|
102
|
+
/**
|
|
103
|
+
* Open (or return the existing) LLM call span for `runId`, parented under its
|
|
104
|
+
* parent run's span. Closed at handleLLMEnd/handleLLMError.
|
|
105
|
+
*/
|
|
106
|
+
private openLlmCallSpan;
|
|
107
|
+
/**
|
|
108
|
+
* `JSON.stringify` that never throws out of a callback. On failure (circular
|
|
109
|
+
* structures, BigInt values, etc.) it reports via `captureError` and returns
|
|
110
|
+
* null so the attribute is simply omitted rather than breaking the host
|
|
111
|
+
* LangChain run.
|
|
112
|
+
*/
|
|
113
|
+
private safeJsonStringify;
|
|
114
|
+
/**
|
|
115
|
+
* Collect the trailing prompt messages from the most recent message batch.
|
|
116
|
+
*
|
|
117
|
+
* Walks the last message list backwards, collecting each message until (and
|
|
118
|
+
* excluding) the last AIMessage (`type === "ai"`) — i.e. everything the model
|
|
119
|
+
* is being asked to respond to since its previous turn, including any
|
|
120
|
+
* ToolMessage results. Returns them in chronological order as `{type, content}`
|
|
121
|
+
* objects; ToolMessage entries additionally carry a `metadata` field of
|
|
122
|
+
* `{tool_name, tool_call_id}`.
|
|
123
|
+
*/
|
|
124
|
+
private getPromptMessages;
|
|
125
|
+
/**
|
|
126
|
+
* Record responses and announced tool calls on the LLM call span for `runId`,
|
|
127
|
+
* then end it. Shared by handleLLMEnd and handleLLMError; `err` is recorded on
|
|
128
|
+
* the span before it ends.
|
|
129
|
+
*/
|
|
130
|
+
private finalizeLlmCall;
|
|
131
|
+
/**
|
|
132
|
+
* Stamp a tool-call span with the span id of the LLM call that announced it,
|
|
133
|
+
* then drop the mapping entry so it doesn't grow unbounded over a session.
|
|
134
|
+
*/
|
|
135
|
+
private stampLlmCallSpanId;
|
|
136
|
+
/** Pop the span for `runId` from the registry and end it. */
|
|
137
|
+
private endSpan;
|
|
61
138
|
private closeToolCallSpan;
|
|
62
139
|
}
|
|
63
140
|
//# sourceMappingURL=frisk-callback-handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"frisk-callback-handler.d.ts","sourceRoot":"","sources":["../../../src/adapters/langchain/frisk-callback-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAA;AACpE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AACnE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;
|
|
1
|
+
{"version":3,"file":"frisk-callback-handler.d.ts","sourceRoot":"","sources":["../../../src/adapters/langchain/frisk-callback-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAA;AACpE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AACnE,OAAO,KAAK,EAEV,WAAW,EAEZ,MAAM,0BAA0B,CAAA;AACjC,OAAO,KAAK,EAAkB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAkB,MAAM,0BAA0B,CAAA;AAOvE,OAAO,EAEL,sBAAsB,EAEvB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAElD,MAAM,MAAM,uBAAuB,GAAG,OAAO,GAAG,SAAS,CAAA;AAezD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,oBAAqB,SAAQ,mBAAm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