Skip to content

Commit d8ef6f9

Browse files
committed
Restore chat-completion-message.test.js
1 parent e94a80e commit d8ef6f9

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

test/unit/llm-events/langchain/chat-completion-message.test.js

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
const test = require('node:test')
99
const assert = require('node:assert')
10-
const { LlmChatCompletionSummary } = require('#agentlib/llm-events/langchain/index.js')
10+
const { LlmChatCompletionMessage } = require('#agentlib/llm-events/langchain/index.js')
1111

1212
test.beforeEach((ctx) => {
1313
ctx.nr = {}
@@ -26,6 +26,11 @@ test.beforeEach((ctx) => {
2626

2727
ctx.nr.agent = {
2828
config: {
29+
ai_monitoring: {
30+
record_content: {
31+
enabled: true
32+
}
33+
},
2934
applications() {
3035
return ['test-app']
3136
}
@@ -39,19 +44,24 @@ test.beforeEach((ctx) => {
3944

4045
ctx.nr.segment = {
4146
id: 'segment-1',
42-
timer: { start: 1769450379777 },
43-
getDurationInMillis() {
44-
return 42
47+
timer: {
48+
start: 1768511347385
4549
}
4650
}
4751

52+
ctx.nr.completionId = '4bea415a30e702d45f5dd521c74b6216d209'
4853
ctx.nr.runId = 'run-1'
4954
ctx.nr.metadata = { foo: 'foo' }
5055
})
5156

52-
test('summary has expected properties', async (t) => {
53-
const msg = new LlmChatCompletionSummary(t.nr)
54-
assert.match(msg.id, /[a-z0-9-]{32}/)
57+
test('creates entity', async (t) => {
58+
const msg = new LlmChatCompletionMessage({
59+
...t.nr,
60+
sequence: 1,
61+
content: 'hello world',
62+
isResponse: true
63+
})
64+
assert.equal(msg.id, 'run-1-1')
5565
assert.equal(msg.appName, 'test-app')
5666
assert.equal(msg['llm.conversation_id'], 'test-conversation')
5767
assert.equal(msg.span_id, 'segment-1')
@@ -61,8 +71,48 @@ test('summary has expected properties', async (t) => {
6171
assert.equal(msg.ingest_source, 'Node')
6272
assert.equal(msg.vendor, 'langchain')
6373
assert.equal(msg.virtual_llm, true)
64-
assert.equal(msg.tags, '')
65-
assert.equal(msg.duration, 42)
66-
assert.equal(msg['response.number_of_messages'], 0)
67-
assert.equal(msg.timestamp, t.nr.segment.timer.start)
74+
assert.equal(msg.sequence, 1)
75+
assert.equal(msg.role, 'assistant', 'should assume assistant role based on isResponse=true')
76+
assert.equal(msg.content, 'hello world')
77+
assert.equal(msg.completion_id, t.nr.completionId)
78+
assert.equal(msg.timestamp, undefined, 'should not have a timestamp defined if isResponse=true')
79+
})
80+
81+
test('assigns role if given', async(t) => {
82+
const msg = new LlmChatCompletionMessage({
83+
...t.nr,
84+
sequence: 1,
85+
content: 'hello world',
86+
role: 'system'
87+
})
88+
assert.equal(msg.role, 'system')
89+
})
90+
91+
test('assigns role and timestamp correctly if isResponse is false', async(t) => {
92+
const msg = new LlmChatCompletionMessage({
93+
...t.nr,
94+
sequence: 0,
95+
content: 'hello world',
96+
isResponse: false
97+
})
98+
assert.equal(msg.role, 'user', 'role should be user')
99+
assert.equal(msg.timestamp, t.nr.segment.timer.start, 'should have a timestamp defined if isResponse=false')
100+
})
101+
102+
test('assigns id correctly', async (t) => {
103+
let msg = new LlmChatCompletionMessage({ ...t.nr, runId: '', sequence: 1 })
104+
assert.match(msg.id, /[a-z0-9-]{32}/)
105+
106+
msg = new LlmChatCompletionMessage({ ...t.nr, runId: '123456', sequence: 42 })
107+
assert.equal(msg.id, '123456-42')
108+
})
109+
110+
test('respects record_content setting', async (t) => {
111+
t.nr.agent.config.ai_monitoring.record_content.enabled = false
112+
const search = new LlmChatCompletionMessage({
113+
...t.nr,
114+
sequence: 1,
115+
content: 'hello world'
116+
})
117+
assert.equal(search.content, undefined)
68118
})

0 commit comments

Comments
 (0)