:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): add a first message length input to get ticket (#123917)
Chris Shineedcb7f73b57f
diff --git a/nodejs/src/cdp/async-functions/conversations.ts b/nodejs/src/cdp/async-functions/conversations.tsindex 9ec2f1de65e7..31e5ace6b784 100644--- a/nodejs/src/cdp/async-functions/conversations.ts+++ b/nodejs/src/cdp/async-functions/conversations.ts@@ -51,6 +51,17 @@ async function callInternalTicketApi(})}+function firstCustomerMessageQuery(opts: Record<string, any> | undefined): string {+ const maxBytes = Number(opts?.first_customer_message_max_bytes)+ const hasLength = Number.isFinite(maxBytes) && maxBytes > 0+ if (!hasLength && opts?.include_first_customer_message_text !== true) {+ return ''+ }+ const query = '?include_first_customer_message_text=true'+ // Django clamps the range itself. This bound only keeps a huge number out of exponent notation.+ return hasLength ? `${query}&first_customer_message_max_bytes=${Math.trunc(Math.min(maxBytes, 100_000))}` : query+}+registerAsyncFunction('postHogGetTicket', {execute: async (args, context, result) => {const [opts] = args as [Record<string, any> | undefined]@@ -60,12 +71,10 @@ registerAsyncFunction('postHogGetTicket', {throw new Error("[HogFunction] - postHogGetTicket call missing 'ticket_id' property")}- // Opt-in preview of the customer's first message. Only appended when the workflow enabled- // it, so the default fetch keeps the same URL, response size, and query cost it had.- const query =- opts?.include_first_customer_message_text === true ? '?include_first_customer_message_text=true' : ''-- await callInternalTicketApi(context, result, ticketId, { method: 'GET', query })+ await callInternalTicketApi(context, result, ticketId, {+ method: 'GET',+ query: firstCustomerMessageQuery(opts),+ })},mock: (args, logs) => {@@ -97,7 +106,7 @@ registerAsyncFunction('postHogGetTicket', {last_message_text: null,// The real ticket API omits this field unless the caller opts in, so only include// it here when the workflow asked for it, keeping a mocked preview true to runtime.- ...(args[0]?.include_first_customer_message_text === true ? { first_customer_message_text: null } : {}),+ ...(firstCustomerMessageQuery(args[0]) ? { first_customer_message_text: null } : {}),unread_team_count: 0,unread_customer_count: 0,sla: null,diff --git a/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-get-ticket.template.ts b/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-get-ticket.template.tsindex ec15b73c4a04..09ec64d2b9df 100644--- a/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-get-ticket.template.ts+++ b/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-get-ticket.template.ts@@ -21,7 +21,8 @@ if (empty(inputs.ticket_id)) {let response := postHogGetTicket({'ticket_id': inputs.ticket_id,- 'include_first_customer_message_text': inputs.include_first_customer_message_text+ 'include_first_customer_message_text': inputs.include_first_customer_message_text,+ 'first_customer_message_max_bytes': inputs.first_customer_message_max_bytes})if (response.status != 200) {@@ -50,5 +51,13 @@ return response.bodydescription:"Add a short preview of the customer's first message to the response. Off by default, since it runs an extra query. Turn it on to remind the customer which ticket a message is about.",},+ {+ key: 'first_customer_message_max_bytes',+ type: 'number',+ label: 'First customer message length (bytes)',+ secret: false,+ required: false,+ description: 'Bytes of the first message to include, from 200 to 3,000. Leave empty for the short preview.',+ },],}diff --git a/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-tickets.template.test.ts b/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-tickets.template.test.tsindex a5efcdb6afbe..8999329dac05 100644--- a/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-tickets.template.test.ts+++ b/nodejs/src/cdp/templates/_destinations/posthog_conversations/posthog-tickets.template.test.ts@@ -119,16 +119,46 @@ describe('posthog conversations ticket templates', () => {const fetchUrl = (): string => tester.mockInternalFetch.mock.calls[0][0] as string- it('does not request the field by default', async () => {- await tester.invoke({ ticket_id: TICKET_UUID })- expect(fetchUrl()).not.toContain('include_first_customer_message_text')- })-- it('appends the query param when opted in', async () => {- await tester.invoke({ ticket_id: TICKET_UUID, include_first_customer_message_text: true })- expect(fetchUrl()).toContain(- `/internal/conversations/tickets/${TICKET_UUID}?include_first_customer_message_text=true`- )+ it.each([+ { name: 'does not request the field by default', inputs: {}, query: '' },+ {+ name: 'treats a zero length as unset',+ inputs: { first_customer_message_max_bytes: 0 },+ query: '',+ },+ {+ name: 'turns the field on when only a length is set',+ inputs: { first_customer_message_max_bytes: 3000 },+ query: '?include_first_customer_message_text=true&first_customer_message_max_bytes=3000',+ },+ {+ name: 'appends the query param when opted in',+ inputs: { include_first_customer_message_text: true },+ query: '?include_first_customer_message_text=true',+ },+ {+ name: 'passes the length when opted in',+ inputs: { include_first_customer_message_text: true, first_customer_message_max_bytes: 3000 },+ query: '?include_first_customer_message_text=true&first_customer_message_max_bytes=3000',+ },+ {+ name: 'reads a length saved as text',+ inputs: { first_customer_message_max_bytes: '3000' },+ query: '?include_first_customer_message_text=true&first_customer_message_max_bytes=3000',+ },+ {+ name: 'drops the fraction from a length, which Django would reject',+ inputs: { include_first_customer_message_text: true, first_customer_message_max_bytes: 2500.7 },+ query: '?include_first_customer_message_text=true&first_customer_message_max_bytes=2500',+ },+ {+ name: 'bounds a huge length before it becomes exponent notation',+ inputs: { include_first_customer_message_text: true, first_customer_message_max_bytes: 1e21 },+ query: '?include_first_customer_message_text=true&first_customer_message_max_bytes=100000',+ },+ ])('$name', async ({ inputs, query }) => {+ await tester.invoke({ ticket_id: TICKET_UUID, ...inputs })+ expect(fetchUrl().split(`/internal/conversations/tickets/${TICKET_UUID}`)[1]).toEqual(query)})})})diff --git a/nodejs/tests/fixtures/hog-bytecode/1c71e923817d00b30e49ed3ec54e8dfaa795452dd43ddcd0decebd6f6127262b.json b/nodejs/tests/fixtures/hog-bytecode/1c71e923817d00b30e49ed3ec54e8dfaa795452dd43ddcd0decebd6f6127262b.jsonnew file mode 100644index 000000000000..20fee6510226--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/1c71e923817d00b30e49ed3ec54e8dfaa795452dd43ddcd0decebd6f6127262b.json@@ -0,0 +1 @@+["_H",1,32,"3000",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/d12bc9b149d6b86ef912a93b581eeb4af6433ea0d822d90f1b76b6ecf2442140.json b/nodejs/tests/fixtures/hog-bytecode/d12bc9b149d6b86ef912a93b581eeb4af6433ea0d822d90f1b76b6ecf2442140.jsonnew file mode 100644index 000000000000..1b3b4ba29c8d--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/d12bc9b149d6b86ef912a93b581eeb4af6433ea0d822d90f1b76b6ecf2442140.json@@ -0,0 +1 @@+["_H",1,52,"apiErrorMessage",1,0,28,36,0,32,"body",45,32,"error",45,47,9,35,36,0,32,"body",45,32,"detail",45,47,6,35,36,0,32,"body",45,38,53,0,32,"ticket_id",32,"inputs",1,2,2,"empty",1,40,6,32,"Ticket ID is required",2,"Error",1,49,32,"ticket_id",32,"ticket_id",32,"inputs",1,2,32,"include_first_customer_message_text",32,"include_first_customer_message_text",32,"inputs",1,2,32,"first_customer_message_max_bytes",32,"first_customer_message_max_bytes",32,"inputs",1,2,42,3,2,"postHogGetTicket",1,33,200,36,1,32,"status",45,12,40,22,32,"Failed to fetch ticket (",36,1,32,"status",45,32,"): ",36,1,36,0,54,1,2,"concat",4,2,"Error",1,49,36,1,32,"body",45,38,35,35]