@terreno/ai 0.6.0 → 0.7.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.
Files changed (63) hide show
  1. package/dist/index.d.ts +7 -0
  2. package/dist/index.js +24 -1
  3. package/dist/langfuseApp.d.ts +9 -0
  4. package/dist/langfuseApp.js +96 -0
  5. package/dist/langfuseCache.d.ts +16 -0
  6. package/dist/langfuseCache.js +104 -0
  7. package/dist/langfuseClient.d.ts +6 -0
  8. package/dist/langfuseClient.js +82 -0
  9. package/dist/langfusePrompts.d.ts +12 -0
  10. package/dist/langfusePrompts.js +215 -0
  11. package/dist/langfuseRoutesEvaluations.d.ts +3 -0
  12. package/dist/langfuseRoutesEvaluations.js +79 -0
  13. package/dist/langfuseRoutesMiddleware.d.ts +2 -0
  14. package/dist/langfuseRoutesMiddleware.js +13 -0
  15. package/dist/langfuseRoutesPlayground.d.ts +2 -0
  16. package/dist/langfuseRoutesPlayground.js +106 -0
  17. package/dist/langfuseRoutesPrompts.d.ts +2 -0
  18. package/dist/langfuseRoutesPrompts.js +129 -0
  19. package/dist/langfuseRoutesTraces.d.ts +2 -0
  20. package/dist/langfuseRoutesTraces.js +82 -0
  21. package/dist/langfuseTracing.d.ts +4 -0
  22. package/dist/langfuseTracing.js +77 -0
  23. package/dist/langfuseTypes.d.ts +96 -0
  24. package/dist/langfuseTypes.js +2 -0
  25. package/dist/langfuseVercelAi.d.ts +12 -0
  26. package/dist/langfuseVercelAi.js +98 -0
  27. package/dist/routes/gpt.js +110 -79
  28. package/dist/service/aiService.js +3 -0
  29. package/dist/types/index.d.ts +3 -0
  30. package/package.json +16 -3
  31. package/src/index.ts +24 -0
  32. package/src/langfuseApp.ts +57 -0
  33. package/src/langfuseCache.test.ts +83 -0
  34. package/src/langfuseCache.ts +58 -0
  35. package/src/langfuseClient.ts +32 -0
  36. package/src/langfusePrompts.test.ts +143 -0
  37. package/src/langfusePrompts.ts +150 -0
  38. package/src/langfuseRoutes.test.ts +143 -0
  39. package/src/langfuseRoutesEvaluations.ts +48 -0
  40. package/src/langfuseRoutesMiddleware.ts +12 -0
  41. package/src/langfuseRoutesPlayground.ts +50 -0
  42. package/src/langfuseRoutesPrompts.ts +86 -0
  43. package/src/langfuseRoutesTraces.ts +40 -0
  44. package/src/langfuseTracing.ts +33 -0
  45. package/src/langfuseTypes.ts +103 -0
  46. package/src/langfuseVercelAi.ts +72 -0
  47. package/src/routes/gpt.ts +30 -0
  48. package/src/routes/routes.test.ts +9 -1
  49. package/src/service/aiService.ts +3 -0
  50. package/src/types/index.ts +3 -0
  51. package/tsconfig.json +1 -1
  52. package/dist/models/aiRequest.test.d.ts +0 -1
  53. package/dist/models/aiRequest.test.js +0 -189
  54. package/dist/models/fileAttachment.test.d.ts +0 -1
  55. package/dist/models/fileAttachment.test.js +0 -217
  56. package/dist/models/gptHistory.test.d.ts +0 -1
  57. package/dist/models/gptHistory.test.js +0 -318
  58. package/dist/routes/routes.test.d.ts +0 -1
  59. package/dist/routes/routes.test.js +0 -303
  60. package/dist/service/aiService.test.d.ts +0 -1
  61. package/dist/service/aiService.test.js +0 -367
  62. package/dist/tests/bunSetup.d.ts +0 -1
  63. package/dist/tests/bunSetup.js +0 -227
@@ -0,0 +1,50 @@
1
+ import {APIError, asyncHandler, authenticateMiddleware} from "@terreno/api";
2
+ import type express from "express";
3
+
4
+ import {compilePrompt, getPrompt} from "./langfusePrompts";
5
+ import {requireAdmin} from "./langfuseRoutesMiddleware";
6
+ import type {ChatMessage} from "./langfuseTypes";
7
+
8
+ export const addPlaygroundRoutes = (router: express.Application, basePath: string): void => {
9
+ router.post(
10
+ `${basePath}/playground`,
11
+ [authenticateMiddleware(), requireAdmin],
12
+ asyncHandler(async (req, res) => {
13
+ const {promptName, variables, label} = req.body as {
14
+ promptName: string;
15
+ variables?: Record<string, string>;
16
+ label?: string;
17
+ };
18
+
19
+ if (!promptName) {
20
+ throw new APIError({
21
+ detail: "promptName is required",
22
+ status: 400,
23
+ title: "Invalid request",
24
+ });
25
+ }
26
+
27
+ const cached = await getPrompt(promptName, {label, variables});
28
+ const compiled = compilePrompt(cached, variables ?? {});
29
+
30
+ return res.json({
31
+ compiled,
32
+ config: cached.config,
33
+ labels: cached.labels,
34
+ name: cached.name,
35
+ type: cached.type,
36
+ variables: extractVariables(
37
+ cached.type === "text"
38
+ ? (cached.prompt as string)
39
+ : (cached.prompt as ChatMessage[]).map((m) => m.content).join(" ")
40
+ ),
41
+ version: cached.version,
42
+ });
43
+ })
44
+ );
45
+ };
46
+
47
+ const extractVariables = (template: string): string[] => {
48
+ const matches = template.match(/\{\{(\w+)\}\}/g) ?? [];
49
+ return [...new Set(matches.map((m) => m.slice(2, -2)))];
50
+ };
@@ -0,0 +1,86 @@
1
+ import {APIError, asyncHandler, authenticateMiddleware} from "@terreno/api";
2
+ import type express from "express";
3
+
4
+ import {getLangfuseClient} from "./langfuseClient";
5
+ import {createPrompt, invalidatePromptCache} from "./langfusePrompts";
6
+ import {requireAdmin} from "./langfuseRoutesMiddleware";
7
+ import type {ChatMessage} from "./langfuseTypes";
8
+
9
+ export const addPromptRoutes = (router: express.Application, basePath: string): void => {
10
+ router.get(
11
+ `${basePath}/prompts`,
12
+ [authenticateMiddleware(), requireAdmin],
13
+ asyncHandler(async (req, res) => {
14
+ const client = getLangfuseClient();
15
+ const page = Number(req.query.page) || 1;
16
+ const limit = Number(req.query.limit) || 20;
17
+
18
+ const result = await client.api.promptsList({limit, page});
19
+ return res.json({
20
+ data: result.data,
21
+ meta: result.meta,
22
+ });
23
+ })
24
+ );
25
+
26
+ router.get(
27
+ `${basePath}/prompts/:name`,
28
+ [authenticateMiddleware(), requireAdmin],
29
+ asyncHandler(async (req, res) => {
30
+ const client = getLangfuseClient();
31
+ const {name} = req.params;
32
+ const version = req.query.version ? Number(req.query.version) : undefined;
33
+ const label = req.query.label as string | undefined;
34
+
35
+ const prompt = await client.getPrompt(name, version, {
36
+ cacheTtlSeconds: 0,
37
+ ...(label ? {label} : {}),
38
+ });
39
+
40
+ return res.json({
41
+ config: prompt.config,
42
+ labels: prompt.labels,
43
+ name: prompt.name,
44
+ prompt: prompt.prompt,
45
+ tags: prompt.tags,
46
+ type: prompt.type,
47
+ version: prompt.version,
48
+ });
49
+ })
50
+ );
51
+
52
+ router.post(
53
+ `${basePath}/prompts`,
54
+ [authenticateMiddleware(), requireAdmin],
55
+ asyncHandler(async (req, res) => {
56
+ const {name, type, prompt, labels, tags, config} = req.body as {
57
+ name: string;
58
+ type: "text" | "chat";
59
+ prompt: string | ChatMessage[];
60
+ labels?: string[];
61
+ tags?: string[];
62
+ config?: Record<string, unknown>;
63
+ };
64
+
65
+ if (!name || !type || prompt === undefined) {
66
+ throw new APIError({
67
+ detail: "name, type, and prompt are required",
68
+ status: 400,
69
+ title: "Invalid request",
70
+ });
71
+ }
72
+
73
+ const result = await createPrompt({config, labels, name, prompt, tags, type});
74
+ return res.status(201).json(result);
75
+ })
76
+ );
77
+
78
+ router.delete(
79
+ `${basePath}/prompts/:name/cache`,
80
+ [authenticateMiddleware(), requireAdmin],
81
+ asyncHandler(async (req, res) => {
82
+ await invalidatePromptCache(req.params.name);
83
+ return res.json({invalidated: true, name: req.params.name});
84
+ })
85
+ );
86
+ };
@@ -0,0 +1,40 @@
1
+ import {asyncHandler, authenticateMiddleware} from "@terreno/api";
2
+ import type express from "express";
3
+
4
+ import {getLangfuseClient} from "./langfuseClient";
5
+ import {requireAdmin} from "./langfuseRoutesMiddleware";
6
+
7
+ export const addTraceRoutes = (router: express.Application, basePath: string): void => {
8
+ router.get(
9
+ `${basePath}/traces`,
10
+ [authenticateMiddleware(), requireAdmin],
11
+ asyncHandler(async (req, res) => {
12
+ const client = getLangfuseClient();
13
+ const page = Number(req.query.page) || 1;
14
+ const limit = Number(req.query.limit) || 20;
15
+ const userId = req.query.userId as string | undefined;
16
+ const fromTimestamp = req.query.from ? new Date(req.query.from as string) : undefined;
17
+ const toTimestamp = req.query.to ? new Date(req.query.to as string) : undefined;
18
+
19
+ const result = await client.fetchTraces({
20
+ fromTimestamp,
21
+ limit,
22
+ page,
23
+ toTimestamp,
24
+ userId,
25
+ });
26
+
27
+ return res.json({data: result.data, meta: result.meta});
28
+ })
29
+ );
30
+
31
+ router.get(
32
+ `${basePath}/traces/:traceId`,
33
+ [authenticateMiddleware(), requireAdmin],
34
+ asyncHandler(async (req, res) => {
35
+ const client = getLangfuseClient();
36
+ const result = await client.fetchTrace(req.params.traceId);
37
+ return res.json(result.data);
38
+ })
39
+ );
40
+ };
@@ -0,0 +1,33 @@
1
+ import {LangfuseSpanProcessor} from "@langfuse/otel";
2
+ import {NodeSDK} from "@opentelemetry/sdk-node";
3
+ import {logger} from "@terreno/api";
4
+
5
+ import type {LangfuseAppOptions} from "./langfuseTypes";
6
+
7
+ let sdkInstance: NodeSDK | null = null;
8
+
9
+ export const initTracing = (options: LangfuseAppOptions): NodeSDK => {
10
+ const sdk = new NodeSDK({
11
+ serviceName: options.serviceName ?? "terreno-app",
12
+ spanProcessors: [
13
+ new LangfuseSpanProcessor({
14
+ baseUrl: options.baseUrl ?? "https://cloud.langfuse.com",
15
+ publicKey: options.publicKey,
16
+ secretKey: options.secretKey,
17
+ }),
18
+ ],
19
+ });
20
+
21
+ sdk.start();
22
+ sdkInstance = sdk;
23
+ logger.info("Langfuse OpenTelemetry tracing initialized");
24
+ return sdk;
25
+ };
26
+
27
+ export const shutdownTracing = async (): Promise<void> => {
28
+ if (sdkInstance) {
29
+ await sdkInstance.shutdown();
30
+ sdkInstance = null;
31
+ logger.info("Langfuse OpenTelemetry tracing shut down");
32
+ }
33
+ };
@@ -0,0 +1,103 @@
1
+ export interface ScoringFunction {
2
+ name: string;
3
+ description: string;
4
+ scoreType: "numeric" | "categorical" | "boolean";
5
+ categories?: string[];
6
+ range?: {min: number; max: number};
7
+ }
8
+
9
+ export interface LangfuseAppOptions {
10
+ secretKey: string;
11
+ publicKey: string;
12
+ /** Organization slug in Langfuse (default: "flourish-health") */
13
+ organization?: string;
14
+ /** Project name/slug in Langfuse (default: "terreno") */
15
+ project?: string;
16
+ /** Langfuse project ID (UUID) for trace URLs; optional, keys usually imply project */
17
+ projectId?: string;
18
+ baseUrl?: string;
19
+ adminPath?: string;
20
+ enableAdminUI?: boolean;
21
+ enableTracing?: boolean;
22
+ serviceName?: string;
23
+ cache?: {
24
+ promptTtlSeconds?: number;
25
+ traceTtlSeconds?: number;
26
+ };
27
+ evaluation?: {
28
+ enabled?: boolean;
29
+ scoringFunctions?: ScoringFunction[];
30
+ };
31
+ }
32
+
33
+ export interface ChatMessage {
34
+ role: string;
35
+ content: string;
36
+ }
37
+
38
+ export interface LangfuseCachedPrompt {
39
+ name: string;
40
+ version: number;
41
+ type: "text" | "chat";
42
+ prompt: string | ChatMessage[];
43
+ config: Record<string, unknown>;
44
+ labels: string[];
45
+ tags: string[];
46
+ }
47
+
48
+ export interface TelemetrySettings {
49
+ isEnabled?: boolean;
50
+ functionId?: string;
51
+ metadata?: Record<string, string | number | boolean>;
52
+ }
53
+
54
+ export interface PreparePromptResult {
55
+ prompt?: string;
56
+ messages?: ChatMessage[];
57
+ config: Record<string, unknown>;
58
+ telemetry: TelemetrySettings;
59
+ }
60
+
61
+ export interface GetPromptOptions {
62
+ label?: string;
63
+ variables?: Record<string, string>;
64
+ userId?: string;
65
+ }
66
+
67
+ export interface PromptListItem {
68
+ name: string;
69
+ version: number;
70
+ type: "text" | "chat";
71
+ labels: string[];
72
+ tags: string[];
73
+ createdAt: string;
74
+ }
75
+
76
+ export interface TraceListItem {
77
+ id: string;
78
+ name: string;
79
+ userId?: string;
80
+ sessionId?: string;
81
+ timestamp: string;
82
+ input?: unknown;
83
+ output?: unknown;
84
+ metadata?: Record<string, unknown>;
85
+ }
86
+
87
+ export interface PaginatedResult<T> {
88
+ data: T[];
89
+ meta: {
90
+ page: number;
91
+ limit: number;
92
+ total: number;
93
+ totalPages: number;
94
+ };
95
+ }
96
+
97
+ export interface ScoreSubmission {
98
+ traceId: string;
99
+ name: string;
100
+ value: number | string;
101
+ comment?: string;
102
+ dataType?: "NUMERIC" | "CATEGORICAL" | "BOOLEAN";
103
+ }
@@ -0,0 +1,72 @@
1
+ import {logger} from "@terreno/api";
2
+
3
+ import {compilePrompt, getPrompt} from "./langfusePrompts";
4
+ import type {
5
+ ChatMessage,
6
+ GetPromptOptions,
7
+ LangfuseAppOptions,
8
+ PreparePromptResult,
9
+ } from "./langfuseTypes";
10
+
11
+ export const preparePromptForAI = async (
12
+ params: {
13
+ promptName: string;
14
+ variables?: Record<string, string>;
15
+ userId?: string;
16
+ label?: string;
17
+ },
18
+ appOptions?: Pick<LangfuseAppOptions, "cache">
19
+ ): Promise<PreparePromptResult> => {
20
+ const options: GetPromptOptions = {
21
+ label: params.label,
22
+ userId: params.userId,
23
+ variables: params.variables,
24
+ };
25
+
26
+ const cached = await getPrompt(params.promptName, options, appOptions);
27
+ const compiled = compilePrompt(cached, params.variables ?? {});
28
+
29
+ const varKeys = Object.keys(params.variables ?? {});
30
+ logger.debug(
31
+ `Langfuse prompt used: "${params.promptName}" v${cached.version}${varKeys.length ? ` (vars: ${varKeys.join(", ")})` : ""}${params.userId ? ` (user: ${params.userId})` : ""}`
32
+ );
33
+
34
+ const telemetry: PreparePromptResult["telemetry"] = {
35
+ functionId: `prompt:${params.promptName}`,
36
+ isEnabled: true,
37
+ metadata: {
38
+ langfusePromptName: params.promptName,
39
+ langfusePromptVersion: cached.version,
40
+ ...(params.userId ? {userId: params.userId} : {}),
41
+ },
42
+ };
43
+
44
+ if (cached.type === "text") {
45
+ return {
46
+ config: cached.config,
47
+ prompt: compiled as string,
48
+ telemetry,
49
+ };
50
+ }
51
+
52
+ return {
53
+ config: cached.config,
54
+ messages: compiled as ChatMessage[],
55
+ telemetry,
56
+ };
57
+ };
58
+
59
+ export const createTelemetryConfig = (params: {
60
+ functionId: string;
61
+ userId?: string;
62
+ metadata?: Record<string, string | number | boolean>;
63
+ }): PreparePromptResult["telemetry"] => {
64
+ return {
65
+ functionId: params.functionId,
66
+ isEnabled: true,
67
+ metadata: {
68
+ ...params.metadata,
69
+ ...(params.userId ? {userId: params.userId} : {}),
70
+ },
71
+ };
72
+ };
package/src/routes/gpt.ts CHANGED
@@ -9,6 +9,7 @@ import type {Tool} from "ai";
9
9
  import {stepCountIs, streamText} from "ai";
10
10
  import type express from "express";
11
11
  import type mongoose from "mongoose";
12
+ import {createTelemetryConfig, preparePromptForAI} from "../langfuseVercelAi";
12
13
 
13
14
  import {AIRequest} from "../models/aiRequest";
14
15
  import {GptHistory} from "../models/gptHistory";
@@ -189,6 +190,25 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
189
190
  }
190
191
  }
191
192
 
193
+ // Load system prompt from Langfuse if configured
194
+ if (options.langfuseSystemPromptName) {
195
+ try {
196
+ const langfuseResult = await preparePromptForAI({
197
+ promptName: options.langfuseSystemPromptName,
198
+ userId: userId?.toString(),
199
+ });
200
+ const langfusePrompt =
201
+ typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
202
+ if (langfusePrompt) {
203
+ effectiveSystemPrompt = effectiveSystemPrompt
204
+ ? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
205
+ : langfusePrompt;
206
+ }
207
+ } catch (err) {
208
+ logger.debug(`Langfuse system prompt skipped: ${(err as Error).message}`);
209
+ }
210
+ }
211
+
192
212
  // Build content parts from attachments
193
213
  const contentParts: MessageContentPart[] = [{text: prompt, type: "text"}];
194
214
  if (attachments && Array.isArray(attachments)) {
@@ -265,7 +285,17 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
265
285
  const startTime = Date.now();
266
286
  try {
267
287
  logger.debug("Starting streamText", {model: modelId, supportsTools});
288
+ const telemetry = createTelemetryConfig({
289
+ functionId: "gpt-prompt",
290
+ metadata: {
291
+ ...(options.langfuseSystemPromptName
292
+ ? {langfusePromptName: options.langfuseSystemPromptName}
293
+ : {}),
294
+ },
295
+ userId: userId?.toString(),
296
+ });
268
297
  const result = streamText({
298
+ experimental_telemetry: telemetry,
269
299
  messages,
270
300
  model: (aiService as any).model,
271
301
  providerOptions: !supportsTools
@@ -4,13 +4,21 @@ import type express from "express";
4
4
  import mongoose from "mongoose";
5
5
  import passportLocalMongoose from "passport-local-mongoose";
6
6
  import supertest from "supertest";
7
+
7
8
  import {AIRequest} from "../models/aiRequest";
8
9
  import {GptHistory} from "../models/gptHistory";
9
10
  import {AIService} from "../service/aiService";
10
11
  import {addAiRequestsExplorerRoutes} from "./aiRequestsExplorer";
11
- import {addGptRoutes} from "./gpt";
12
12
  import {addGptHistoryRoutes} from "./gptHistories";
13
13
 
14
+ // Mock langfuseVercelAi to avoid transitive langfuse SDK import in tests
15
+ mock.module("../langfuseVercelAi", () => ({
16
+ createTelemetryConfig: () => ({functionId: "test", isEnabled: false}),
17
+ preparePromptForAI: async () => ({config: {}, prompt: "test", telemetry: {isEnabled: false}}),
18
+ }));
19
+
20
+ const {addGptRoutes} = await import("./gpt");
21
+
14
22
  // Test user schema
15
23
  const userSchema = new mongoose.Schema({
16
24
  admin: {default: false, type: Boolean},
@@ -74,6 +74,7 @@ export class AIService {
74
74
 
75
75
  try {
76
76
  const result = await aiGenerateText({
77
+ experimental_telemetry: {functionId: "generate-text", isEnabled: true},
77
78
  maxOutputTokens,
78
79
  model: this.model,
79
80
  prompt,
@@ -114,6 +115,7 @@ export class AIService {
114
115
 
115
116
  try {
116
117
  const result = streamText({
118
+ experimental_telemetry: {functionId: "generate-text-stream", isEnabled: true},
117
119
  maxOutputTokens,
118
120
  model: this.model,
119
121
  prompt,
@@ -244,6 +246,7 @@ export class AIService {
244
246
 
245
247
  try {
246
248
  const result = streamText({
249
+ experimental_telemetry: {functionId: "generate-chat-stream", isEnabled: true},
247
250
  messages: messages.map((m) => ({content: m.content, role: m.role})),
248
251
  model: this.model,
249
252
  stopWhen: stopWhen ?? stepCountIs(1),
@@ -223,6 +223,9 @@ export interface GptRouteOptions {
223
223
  maxSteps?: number;
224
224
  /** Cheap model ID used for generating conversation titles (e.g. "gemini-2.0-flash-lite"). Falls back to the main model if not set. */
225
225
  titleModelId?: string;
226
+ /** Langfuse prompt name to load and use as the system prompt. Compiled with no variables.
227
+ * Falls back gracefully if Langfuse is not configured or the prompt is not found. */
228
+ langfuseSystemPromptName?: string;
226
229
  }
227
230
 
228
231
  export interface GptHistoryRouteOptions {
package/tsconfig.json CHANGED
@@ -26,6 +26,6 @@
26
26
  "strictNullChecks": true,
27
27
  "target": "es5"
28
28
  },
29
- "exclude": ["node_modules"],
29
+ "exclude": ["node_modules", "src/**/*.test.ts", "src/**/*.test.tsx", "src/tests"],
30
30
  "include": ["src/**/*.ts*", "src/*.ts", "types.d.ts"]
31
31
  }
@@ -1 +0,0 @@
1
- export {};