@terreno/ai 0.13.1 → 0.14.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/dist/langfuseCache.d.ts +5 -6
- package/dist/langfuseCache.js +11 -2
- package/dist/langfuseClient.js +5 -1
- package/dist/models/fileAttachment.js +17 -6
- package/dist/models/gptHistory.js +36 -17
- package/dist/routes/aiRequestsExplorer.d.ts +2 -1
- package/dist/routes/aiRequestsExplorer.js +18 -4
- package/dist/routes/files.d.ts +2 -1
- package/dist/routes/files.js +2 -2
- package/dist/routes/gpt.d.ts +2 -1
- package/dist/routes/gptHistories.d.ts +2 -1
- package/dist/routes/mcp.d.ts +2 -1
- package/dist/routes/mcp.js +2 -5
- package/dist/routes/projects.d.ts +2 -1
- package/dist/service/aiService.d.ts +3 -3
- package/dist/service/fileStorage.js +3 -2
- package/package.json +4 -3
- package/src/langfuseCache.ts +19 -5
- package/src/langfuseClient.test.ts +29 -52
- package/src/langfuseClient.ts +5 -1
- package/src/langfusePrompts.test.ts +11 -10
- package/src/langfuseRoutes.test.ts +27 -19
- package/src/langfuseVercelAi.test.ts +8 -12
- package/src/models/aiRequest.ts +2 -2
- package/src/models/fileAttachment.ts +17 -6
- package/src/models/gptHistory.ts +36 -17
- package/src/routes/aiRequestsExplorer.ts +20 -6
- package/src/routes/files.ts +8 -6
- package/src/routes/gpt.ts +21 -16
- package/src/routes/gptHistories.ts +2 -1
- package/src/routes/mcp.ts +7 -8
- package/src/routes/projects.ts +1 -1
- package/src/routes/routes.test.ts +64 -1
- package/src/service/aiService.test.ts +1 -0
- package/src/service/aiService.ts +4 -4
- package/src/service/fileStorage.ts +3 -2
- package/src/tests/bunSetup.ts +60 -0
- package/src/types/index.ts +2 -0
|
@@ -4,6 +4,12 @@ import mongoose from "mongoose";
|
|
|
4
4
|
import passportLocalMongoose from "passport-local-mongoose";
|
|
5
5
|
import supertest from "supertest";
|
|
6
6
|
|
|
7
|
+
import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
|
|
8
|
+
import {addEvaluationRoutes} from "./langfuseRoutesEvaluations";
|
|
9
|
+
import {addPlaygroundRoutes} from "./langfuseRoutesPlayground";
|
|
10
|
+
import {addPromptRoutes} from "./langfuseRoutesPrompts";
|
|
11
|
+
import {addTraceRoutes} from "./langfuseRoutesTraces";
|
|
12
|
+
|
|
7
13
|
const scoreCreate = mock(() => {});
|
|
8
14
|
const promptCreate = mock(async (_params: Record<string, unknown>) => ({}));
|
|
9
15
|
const promptGet = mock(async (name: string) => ({
|
|
@@ -26,25 +32,6 @@ const traceList = mock(async () => ({
|
|
|
26
32
|
const traceGet = mock(async (traceId: string) => ({id: traceId, name: "Trace"}));
|
|
27
33
|
const flush = mock(async () => {});
|
|
28
34
|
|
|
29
|
-
const realLangfuseClient = await import("./langfuseClient");
|
|
30
|
-
mock.module("./langfuseClient", () => ({
|
|
31
|
-
...realLangfuseClient,
|
|
32
|
-
getLangfuseClient: () => ({
|
|
33
|
-
api: {
|
|
34
|
-
prompts: {list: promptsList},
|
|
35
|
-
trace: {get: traceGet, list: traceList},
|
|
36
|
-
},
|
|
37
|
-
flush,
|
|
38
|
-
prompt: {create: promptCreate, get: promptGet},
|
|
39
|
-
score: {create: scoreCreate},
|
|
40
|
-
}),
|
|
41
|
-
}));
|
|
42
|
-
|
|
43
|
-
const {addPromptRoutes} = await import("./langfuseRoutesPrompts");
|
|
44
|
-
const {addTraceRoutes} = await import("./langfuseRoutesTraces");
|
|
45
|
-
const {addPlaygroundRoutes} = await import("./langfuseRoutesPlayground");
|
|
46
|
-
const {addEvaluationRoutes} = await import("./langfuseRoutesEvaluations");
|
|
47
|
-
|
|
48
35
|
const userSchema = new mongoose.Schema({
|
|
49
36
|
admin: {default: false, type: Boolean},
|
|
50
37
|
email: {index: true, type: String},
|
|
@@ -89,6 +76,27 @@ describe("Langfuse routes", () => {
|
|
|
89
76
|
});
|
|
90
77
|
|
|
91
78
|
beforeEach(() => {
|
|
79
|
+
// Init the (fake) langfuse client and wire our local mocks onto its
|
|
80
|
+
// methods so the route handlers exercise customizable responses without
|
|
81
|
+
// making real SDK calls.
|
|
82
|
+
initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
83
|
+
const client = getLangfuseClient() as unknown as {
|
|
84
|
+
api: {
|
|
85
|
+
prompts: {list: typeof promptsList};
|
|
86
|
+
trace: {get: typeof traceGet; list: typeof traceList};
|
|
87
|
+
};
|
|
88
|
+
flush: typeof flush;
|
|
89
|
+
prompt: {create: typeof promptCreate; get: typeof promptGet};
|
|
90
|
+
score: {create: typeof scoreCreate};
|
|
91
|
+
};
|
|
92
|
+
client.api.prompts.list = promptsList;
|
|
93
|
+
client.api.trace.get = traceGet;
|
|
94
|
+
client.api.trace.list = traceList;
|
|
95
|
+
client.flush = flush;
|
|
96
|
+
client.prompt.create = promptCreate;
|
|
97
|
+
client.prompt.get = promptGet;
|
|
98
|
+
client.score.create = scoreCreate;
|
|
99
|
+
|
|
92
100
|
app = setupServer({
|
|
93
101
|
addRoutes: (router) => {
|
|
94
102
|
addPromptRoutes(router, "/admin/langfuse");
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {LangfuseCache} from "./langfuseCache";
|
|
4
|
+
import {getLangfuseClient, initLangfuseClient} from "./langfuseClient";
|
|
5
|
+
import {createTelemetryConfig, preparePromptForAI} from "./langfuseVercelAi";
|
|
4
6
|
|
|
5
7
|
const mockPromptGet = mock(async (_name: string, _options?: Record<string, unknown>) => ({
|
|
6
8
|
config: {temperature: 0.5},
|
|
@@ -12,22 +14,16 @@ const mockPromptGet = mock(async (_name: string, _options?: Record<string, unkno
|
|
|
12
14
|
version: 3,
|
|
13
15
|
}));
|
|
14
16
|
|
|
15
|
-
// Mock the langfuse SDK client so the real `getPrompt`/`compilePrompt` from
|
|
16
|
-
// `./langfusePrompts` can be exercised without network access. We intentionally
|
|
17
|
-
// do NOT mock `./langfusePrompts` directly because that would leak into other
|
|
18
|
-
// test suites that share the module cache in the same bun process.
|
|
19
|
-
const realLangfuseClient = await import("./langfuseClient");
|
|
20
|
-
mock.module("./langfuseClient", () => ({
|
|
21
|
-
...realLangfuseClient,
|
|
22
|
-
getLangfuseClient: () => ({prompt: {get: mockPromptGet}}),
|
|
23
|
-
}));
|
|
24
|
-
|
|
25
|
-
const {createTelemetryConfig, preparePromptForAI} = await import("./langfuseVercelAi");
|
|
26
|
-
|
|
27
17
|
describe("langfuseVercelAi", () => {
|
|
28
18
|
beforeEach(async () => {
|
|
29
19
|
mockPromptGet.mockClear();
|
|
30
20
|
await LangfuseCache.deleteMany({});
|
|
21
|
+
// Re-init the (fake) langfuse client and wire the prompt.get mock so the
|
|
22
|
+
// real `getPrompt`/`compilePrompt` from `./langfusePrompts` runs against
|
|
23
|
+
// a customizable response without network access.
|
|
24
|
+
initLangfuseClient({publicKey: "pk-test", secretKey: "sk-test"});
|
|
25
|
+
(getLangfuseClient() as unknown as {prompt: {get: typeof mockPromptGet}}).prompt.get =
|
|
26
|
+
mockPromptGet;
|
|
31
27
|
});
|
|
32
28
|
|
|
33
29
|
describe("preparePromptForAI", () => {
|
package/src/models/aiRequest.ts
CHANGED
|
@@ -68,7 +68,7 @@ aiRequestSchema.index({aiModel: 1, created: -1});
|
|
|
68
68
|
aiRequestSchema.index({created: -1, requestType: 1});
|
|
69
69
|
aiRequestSchema.index({parentRequestId: 1});
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
aiRequestSchema.statics.logRequest = async function (
|
|
72
72
|
this: AIRequestModel,
|
|
73
73
|
params: LogRequestParams
|
|
74
74
|
): Promise<AIRequestDocument> {
|
|
@@ -76,7 +76,7 @@ aiRequestSchema.index({parentRequestId: 1});
|
|
|
76
76
|
return request.save();
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
aiRequestSchema.statics.logMultiAgentRequest = async function (
|
|
80
80
|
this: AIRequestModel,
|
|
81
81
|
params: LogMultiAgentRequestParams
|
|
82
82
|
): Promise<AIRequestDocument> {
|
|
@@ -5,12 +5,23 @@ import type {FileAttachmentDocument, FileAttachmentModel} from "../types";
|
|
|
5
5
|
|
|
6
6
|
const fileAttachmentSchema = new mongoose.Schema<FileAttachmentDocument, FileAttachmentModel>(
|
|
7
7
|
{
|
|
8
|
-
filename: {required: true, type: String},
|
|
9
|
-
gcsKey: {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
filename: {description: "Original name of the uploaded file", required: true, type: String},
|
|
9
|
+
gcsKey: {
|
|
10
|
+
description: "Google Cloud Storage key identifying this file",
|
|
11
|
+
required: true,
|
|
12
|
+
type: String,
|
|
13
|
+
unique: true,
|
|
14
|
+
},
|
|
15
|
+
mimeType: {description: "MIME type of the uploaded file", required: true, type: String},
|
|
16
|
+
size: {description: "File size in bytes", required: true, type: Number},
|
|
17
|
+
url: {description: "Public or signed URL for accessing the file", required: true, type: String},
|
|
18
|
+
userId: {
|
|
19
|
+
description: "The user who uploaded this file",
|
|
20
|
+
index: true,
|
|
21
|
+
ref: "User",
|
|
22
|
+
required: true,
|
|
23
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
24
|
+
},
|
|
14
25
|
},
|
|
15
26
|
{strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
|
|
16
27
|
);
|
package/src/models/gptHistory.ts
CHANGED
|
@@ -5,36 +5,45 @@ import type {GptHistoryDocument, GptHistoryModel} from "../types";
|
|
|
5
5
|
|
|
6
6
|
const contentPartSchema = new mongoose.Schema(
|
|
7
7
|
{
|
|
8
|
-
filename: {type: String},
|
|
9
|
-
mimeType: {type: String},
|
|
10
|
-
text: {type: String},
|
|
11
|
-
type: {
|
|
12
|
-
|
|
8
|
+
filename: {description: "Original filename of the attached file", type: String},
|
|
9
|
+
mimeType: {description: "MIME type of the content part", type: String},
|
|
10
|
+
text: {description: "Text content of this part", type: String},
|
|
11
|
+
type: {
|
|
12
|
+
description: "The kind of content this part represents",
|
|
13
|
+
enum: ["text", "image", "file"],
|
|
14
|
+
required: true,
|
|
15
|
+
type: String,
|
|
16
|
+
},
|
|
17
|
+
url: {description: "URL pointing to the content resource", type: String},
|
|
13
18
|
},
|
|
14
|
-
{_id: false}
|
|
19
|
+
{_id: false, strict: "throw"}
|
|
15
20
|
);
|
|
16
21
|
|
|
17
22
|
const gptHistoryPromptSchema = new mongoose.Schema(
|
|
18
23
|
{
|
|
19
|
-
args: {type: mongoose.Schema.Types.Mixed},
|
|
20
|
-
content: {type: [contentPartSchema]},
|
|
21
|
-
model: {type: String},
|
|
24
|
+
args: {description: "Arguments passed to a tool call", type: mongoose.Schema.Types.Mixed},
|
|
25
|
+
content: {description: "Multipart content attached to this prompt", type: [contentPartSchema]},
|
|
26
|
+
model: {description: "AI model identifier used for this prompt", type: String},
|
|
22
27
|
rating: {
|
|
23
28
|
description: "User feedback rating for this prompt",
|
|
24
29
|
enum: ["up", "down"],
|
|
25
30
|
type: String,
|
|
26
31
|
},
|
|
27
|
-
result: {type: mongoose.Schema.Types.Mixed},
|
|
28
|
-
text: {required: true, type: String},
|
|
29
|
-
toolCallId: {
|
|
30
|
-
|
|
32
|
+
result: {description: "Result returned from a tool call", type: mongoose.Schema.Types.Mixed},
|
|
33
|
+
text: {description: "Text content of the prompt or response", required: true, type: String},
|
|
34
|
+
toolCallId: {
|
|
35
|
+
description: "Identifier linking a tool result to its originating call",
|
|
36
|
+
type: String,
|
|
37
|
+
},
|
|
38
|
+
toolName: {description: "Name of the tool that was invoked", type: String},
|
|
31
39
|
type: {
|
|
40
|
+
description: "Role of this message in the conversation",
|
|
32
41
|
enum: ["user", "assistant", "system", "tool-call", "tool-result"],
|
|
33
42
|
required: true,
|
|
34
43
|
type: String,
|
|
35
44
|
},
|
|
36
45
|
},
|
|
37
|
-
{_id: false}
|
|
46
|
+
{_id: false, strict: "throw"}
|
|
38
47
|
);
|
|
39
48
|
|
|
40
49
|
const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel>(
|
|
@@ -45,9 +54,19 @@ const gptHistorySchema = new mongoose.Schema<GptHistoryDocument, GptHistoryModel
|
|
|
45
54
|
ref: "Project",
|
|
46
55
|
type: mongoose.Schema.Types.ObjectId,
|
|
47
56
|
},
|
|
48
|
-
prompts: {
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
prompts: {
|
|
58
|
+
default: [],
|
|
59
|
+
description: "Ordered list of messages in this conversation",
|
|
60
|
+
type: [gptHistoryPromptSchema],
|
|
61
|
+
},
|
|
62
|
+
title: {description: "Auto-generated title from the first assistant response", type: String},
|
|
63
|
+
userId: {
|
|
64
|
+
description: "The user who owns this conversation history",
|
|
65
|
+
index: true,
|
|
66
|
+
ref: "User",
|
|
67
|
+
required: true,
|
|
68
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
69
|
+
},
|
|
51
70
|
},
|
|
52
71
|
{strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
|
|
53
72
|
);
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
|
|
1
|
+
import {APIError, asyncHandler, authenticateMiddleware, createOpenApiBuilder} from "@terreno/api";
|
|
2
2
|
import type express from "express";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
3
4
|
|
|
4
5
|
import {AIRequest} from "../models/aiRequest";
|
|
5
6
|
import type {AiRequestsExplorerRouteOptions} from "../types";
|
|
6
7
|
|
|
7
8
|
export const addAiRequestsExplorerRoutes = (
|
|
8
|
-
router:
|
|
9
|
+
router: express.Router,
|
|
9
10
|
options?: AiRequestsExplorerRouteOptions
|
|
10
11
|
): void => {
|
|
11
12
|
router.get(
|
|
@@ -31,7 +32,7 @@ export const addAiRequestsExplorerRoutes = (
|
|
|
31
32
|
.build(),
|
|
32
33
|
],
|
|
33
34
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
34
|
-
const user =
|
|
35
|
+
const user = req.user as {admin?: boolean} | undefined;
|
|
35
36
|
if (!user?.admin) {
|
|
36
37
|
return res.status(403).json({error: "Admin access required"});
|
|
37
38
|
}
|
|
@@ -43,7 +44,9 @@ export const addAiRequestsExplorerRoutes = (
|
|
|
43
44
|
);
|
|
44
45
|
const skip = (page - 1) * limit;
|
|
45
46
|
|
|
46
|
-
const match: Record<string,
|
|
47
|
+
const match: Record<string, unknown> & {created?: {$gte?: Date; $lte?: Date}} = {
|
|
48
|
+
deleted: {$ne: true},
|
|
49
|
+
};
|
|
47
50
|
|
|
48
51
|
if (req.query.requestType) {
|
|
49
52
|
match.requestType = req.query.requestType;
|
|
@@ -54,10 +57,21 @@ export const addAiRequestsExplorerRoutes = (
|
|
|
54
57
|
if (req.query.startDate || req.query.endDate) {
|
|
55
58
|
match.created = {};
|
|
56
59
|
if (req.query.startDate) {
|
|
57
|
-
|
|
60
|
+
const startDt = DateTime.fromISO(req.query.startDate as string, {zone: "utc"});
|
|
61
|
+
if (!startDt.isValid) {
|
|
62
|
+
throw new APIError({
|
|
63
|
+
status: 400,
|
|
64
|
+
title: "Invalid startDate format (expected ISO 8601)",
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
match.created.$gte = startDt.toJSDate();
|
|
58
68
|
}
|
|
59
69
|
if (req.query.endDate) {
|
|
60
|
-
|
|
70
|
+
const endDt = DateTime.fromISO(req.query.endDate as string, {zone: "utc"});
|
|
71
|
+
if (!endDt.isValid) {
|
|
72
|
+
throw new APIError({status: 400, title: "Invalid endDate format (expected ISO 8601)"});
|
|
73
|
+
}
|
|
74
|
+
match.created.$lte = endDt.toJSDate();
|
|
61
75
|
}
|
|
62
76
|
}
|
|
63
77
|
|
package/src/routes/files.ts
CHANGED
|
@@ -21,7 +21,7 @@ const ALLOWED_MIME_TYPES = new Set([
|
|
|
21
21
|
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
|
22
22
|
|
|
23
23
|
export const addFileRoutes = (
|
|
24
|
-
router:
|
|
24
|
+
router: express.Router,
|
|
25
25
|
options: FileRouteOptions & {fileStorageService: FileStorageService}
|
|
26
26
|
): void => {
|
|
27
27
|
const {fileStorageService, maxFileSize = DEFAULT_MAX_FILE_SIZE} = options;
|
|
@@ -56,8 +56,9 @@ export const addFileRoutes = (
|
|
|
56
56
|
.build(),
|
|
57
57
|
],
|
|
58
58
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
59
|
-
const file = (req as
|
|
60
|
-
const userId = (req as
|
|
59
|
+
const file = (req as express.Request & {file?: Express.Multer.File}).file;
|
|
60
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)
|
|
61
|
+
?._id as mongoose.Types.ObjectId;
|
|
61
62
|
|
|
62
63
|
if (!file) {
|
|
63
64
|
throw new APIError({status: 400, title: "No file provided"});
|
|
@@ -87,7 +88,7 @@ export const addFileRoutes = (
|
|
|
87
88
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
88
89
|
const gcsKey = req.params.gcsKey as string;
|
|
89
90
|
|
|
90
|
-
const attachment = await FileAttachment.
|
|
91
|
+
const attachment = await FileAttachment.findOneOrNone({deleted: false, gcsKey});
|
|
91
92
|
if (!attachment) {
|
|
92
93
|
throw new APIError({status: 404, title: "File not found"});
|
|
93
94
|
}
|
|
@@ -110,9 +111,10 @@ export const addFileRoutes = (
|
|
|
110
111
|
],
|
|
111
112
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
112
113
|
const gcsKey = req.params.gcsKey as string;
|
|
113
|
-
const userId = (req as
|
|
114
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)
|
|
115
|
+
?._id as mongoose.Types.ObjectId;
|
|
114
116
|
|
|
115
|
-
const attachment = await FileAttachment.
|
|
117
|
+
const attachment = await FileAttachment.findOneOrNone({deleted: false, gcsKey});
|
|
116
118
|
if (!attachment) {
|
|
117
119
|
throw new APIError({status: 404, title: "File not found"});
|
|
118
120
|
}
|
package/src/routes/gpt.ts
CHANGED
|
@@ -16,7 +16,12 @@ import {GptHistory} from "../models/gptHistory";
|
|
|
16
16
|
import {Project} from "../models/project";
|
|
17
17
|
import {AIService} from "../service/aiService";
|
|
18
18
|
import {TITLE_GENERATION_PROMPT} from "../service/prompts";
|
|
19
|
-
import type {
|
|
19
|
+
import type {
|
|
20
|
+
GptHistoryDocument,
|
|
21
|
+
GptHistoryPrompt,
|
|
22
|
+
GptRouteOptions,
|
|
23
|
+
MessageContentPart,
|
|
24
|
+
} from "../types";
|
|
20
25
|
|
|
21
26
|
const DEMO_RESPONSE =
|
|
22
27
|
"This is demo mode. To use AI features, paste your Gemini API key in Settings.";
|
|
@@ -90,7 +95,7 @@ const generateTitle = async (
|
|
|
90
95
|
}
|
|
91
96
|
};
|
|
92
97
|
|
|
93
|
-
export const addGptRoutes = (router:
|
|
98
|
+
export const addGptRoutes = (router: express.Router, options: GptRouteOptions): void => {
|
|
94
99
|
const {mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps} = options;
|
|
95
100
|
|
|
96
101
|
router.post(
|
|
@@ -135,7 +140,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
135
140
|
model: requestModel,
|
|
136
141
|
projectId,
|
|
137
142
|
} = req.body;
|
|
138
|
-
const userId = (req as
|
|
143
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
139
144
|
|
|
140
145
|
if (!prompt || typeof prompt !== "string") {
|
|
141
146
|
throw new APIError({status: 400, title: "prompt is required"});
|
|
@@ -158,7 +163,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
158
163
|
}
|
|
159
164
|
|
|
160
165
|
// Load or create history
|
|
161
|
-
let history;
|
|
166
|
+
let history: GptHistoryDocument | null;
|
|
162
167
|
if (historyId) {
|
|
163
168
|
history = await GptHistory.findById(historyId);
|
|
164
169
|
if (!history) {
|
|
@@ -228,7 +233,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
228
233
|
if (attachments && Array.isArray(attachments)) {
|
|
229
234
|
logger.debug("Processing attachments", {
|
|
230
235
|
count: attachments.length,
|
|
231
|
-
types: attachments.map((a:
|
|
236
|
+
types: attachments.map((a: {mimeType?: string; type?: string; url?: string}) => ({
|
|
232
237
|
mimeType: a.mimeType,
|
|
233
238
|
type: a.type,
|
|
234
239
|
urlLength: a.url?.length ?? 0,
|
|
@@ -311,7 +316,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
311
316
|
const result = streamText({
|
|
312
317
|
experimental_telemetry: telemetry,
|
|
313
318
|
messages,
|
|
314
|
-
model:
|
|
319
|
+
model: aiService.model,
|
|
315
320
|
providerOptions: !supportsTools
|
|
316
321
|
? {
|
|
317
322
|
google: {responseModalities: ["TEXT", "IMAGE"]},
|
|
@@ -320,7 +325,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
320
325
|
: undefined,
|
|
321
326
|
stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
|
|
322
327
|
system: effectiveSystemPrompt ?? undefined,
|
|
323
|
-
temperature:
|
|
328
|
+
temperature: aiService.defaultTemperature,
|
|
324
329
|
toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
|
|
325
330
|
tools: allTools,
|
|
326
331
|
});
|
|
@@ -332,7 +337,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
332
337
|
|
|
333
338
|
for await (const part of result.fullStream as AsyncIterable<{
|
|
334
339
|
type: string;
|
|
335
|
-
[key: string]:
|
|
340
|
+
[key: string]: unknown;
|
|
336
341
|
}>) {
|
|
337
342
|
partCount++;
|
|
338
343
|
if (partCount <= 5 || part.type === "error" || part.type === "file") {
|
|
@@ -407,9 +412,9 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
407
412
|
// Persist tool call in history
|
|
408
413
|
history.prompts.push({
|
|
409
414
|
args: part.input as Record<string, unknown>,
|
|
410
|
-
text: `Tool call: ${part.toolName}`,
|
|
411
|
-
toolCallId: part.toolCallId,
|
|
412
|
-
toolName: part.toolName,
|
|
415
|
+
text: `Tool call: ${part.toolName as string}`,
|
|
416
|
+
toolCallId: part.toolCallId as string,
|
|
417
|
+
toolName: part.toolName as string,
|
|
413
418
|
type: "tool-call",
|
|
414
419
|
});
|
|
415
420
|
} else if (part.type === "tool-result") {
|
|
@@ -447,9 +452,9 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
447
452
|
// Persist tool result in history (without the large file data)
|
|
448
453
|
history.prompts.push({
|
|
449
454
|
result: cleanResult as unknown,
|
|
450
|
-
text: `Tool result: ${part.toolName}`,
|
|
451
|
-
toolCallId: part.toolCallId,
|
|
452
|
-
toolName: part.toolName,
|
|
455
|
+
text: `Tool result: ${part.toolName as string}`,
|
|
456
|
+
toolCallId: part.toolCallId as string,
|
|
457
|
+
toolName: part.toolName as string,
|
|
453
458
|
type: "tool-result",
|
|
454
459
|
});
|
|
455
460
|
}
|
|
@@ -615,7 +620,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
615
620
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
616
621
|
const {id} = req.params;
|
|
617
622
|
const {promptIndex, rating} = req.body;
|
|
618
|
-
const userId = (req as
|
|
623
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
619
624
|
|
|
620
625
|
if (typeof promptIndex !== "number" || promptIndex < 0) {
|
|
621
626
|
throw new APIError({status: 400, title: "promptIndex must be a non-negative number"});
|
|
@@ -662,7 +667,7 @@ export const addGptRoutes = (router: any, options: GptRouteOptions): void => {
|
|
|
662
667
|
],
|
|
663
668
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
664
669
|
const {text} = req.body;
|
|
665
|
-
const userId = (req as
|
|
670
|
+
const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
|
|
666
671
|
|
|
667
672
|
if (!text || typeof text !== "string") {
|
|
668
673
|
throw new APIError({status: 400, title: "text is required"});
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {type ModelRouterOptions, modelRouter, Permissions} from "@terreno/api";
|
|
2
|
+
import type express from "express";
|
|
2
3
|
import {GptHistory} from "../models/gptHistory";
|
|
3
4
|
import type {GptHistoryDocument, GptHistoryRouteOptions} from "../types";
|
|
4
5
|
|
|
5
6
|
export const addGptHistoryRoutes = (
|
|
6
|
-
router:
|
|
7
|
+
router: express.Router,
|
|
7
8
|
options?: Partial<ModelRouterOptions<GptHistoryDocument>> & GptHistoryRouteOptions
|
|
8
9
|
): void => {
|
|
9
10
|
router.use(
|
package/src/routes/mcp.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type express from "express";
|
|
|
3
3
|
|
|
4
4
|
import type {McpRouteOptions} from "../types";
|
|
5
5
|
|
|
6
|
-
export const addMcpRoutes = (router:
|
|
6
|
+
export const addMcpRoutes = (router: express.Router, options: McpRouteOptions): void => {
|
|
7
7
|
const {mcpService} = options;
|
|
8
8
|
|
|
9
9
|
router.get(
|
|
@@ -20,7 +20,7 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
|
|
|
20
20
|
.build(),
|
|
21
21
|
],
|
|
22
22
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
23
|
-
const user =
|
|
23
|
+
const user = req.user as {admin?: boolean} | undefined;
|
|
24
24
|
if (!user?.admin) {
|
|
25
25
|
throw new APIError({status: 403, title: "Admin access required"});
|
|
26
26
|
}
|
|
@@ -42,11 +42,10 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
|
|
|
42
42
|
],
|
|
43
43
|
asyncHandler(async (_req: express.Request, res: express.Response) => {
|
|
44
44
|
const tools = await mcpService.getTools();
|
|
45
|
-
const toolList = Object.entries(tools).map(([name, tool]) =>
|
|
46
|
-
|
|
47
|
-
name,
|
|
48
|
-
|
|
49
|
-
}));
|
|
45
|
+
const toolList = Object.entries(tools).map(([name, tool]) => {
|
|
46
|
+
const t = tool as {description?: string; parameters?: unknown};
|
|
47
|
+
return {description: t.description, name, parameters: t.parameters};
|
|
48
|
+
});
|
|
50
49
|
return res.json({data: toolList});
|
|
51
50
|
})
|
|
52
51
|
);
|
|
@@ -63,7 +62,7 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
|
|
|
63
62
|
.build(),
|
|
64
63
|
],
|
|
65
64
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
66
|
-
const user =
|
|
65
|
+
const user = req.user as {admin?: boolean} | undefined;
|
|
67
66
|
if (!user?.admin) {
|
|
68
67
|
throw new APIError({status: 403, title: "Admin access required"});
|
|
69
68
|
}
|
package/src/routes/projects.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {Project} from "../models/project";
|
|
|
13
13
|
import type {ProjectDocument} from "../types";
|
|
14
14
|
|
|
15
15
|
export const addProjectRoutes = (
|
|
16
|
-
router:
|
|
16
|
+
router: express.Router,
|
|
17
17
|
options?: {openApiOptions?: Record<string, unknown>}
|
|
18
18
|
): void => {
|
|
19
19
|
// Add memory to a project (registered before modelRouter so it isn't shadowed)
|
|
@@ -916,6 +916,54 @@ describe("AI Routes", () => {
|
|
|
916
916
|
await Project.deleteMany({});
|
|
917
917
|
});
|
|
918
918
|
|
|
919
|
+
it("streams text-delta events through the SSE path with a minimal mock model", async () => {
|
|
920
|
+
// Covers the text-delta SSE branch by using a model that emits only
|
|
921
|
+
// text-delta + finish (no step-start/step-end events).
|
|
922
|
+
const noStepModel = {
|
|
923
|
+
doGenerate: mock(async () => ({
|
|
924
|
+
content: [{text: "ok", type: "text" as const}],
|
|
925
|
+
finishReason: "stop" as const,
|
|
926
|
+
usage: {inputTokens: 1, outputTokens: 1},
|
|
927
|
+
})),
|
|
928
|
+
doStream: mock(async () => ({
|
|
929
|
+
stream: new ReadableStream({
|
|
930
|
+
start(controller) {
|
|
931
|
+
controller.enqueue({delta: "buffered tail", id: "t1", type: "text-delta" as const});
|
|
932
|
+
controller.enqueue({
|
|
933
|
+
finishReason: "stop" as const,
|
|
934
|
+
type: "finish" as const,
|
|
935
|
+
usage: {inputTokens: 1, outputTokens: 1},
|
|
936
|
+
});
|
|
937
|
+
controller.close();
|
|
938
|
+
},
|
|
939
|
+
}),
|
|
940
|
+
})),
|
|
941
|
+
modelId: "no-step-model",
|
|
942
|
+
provider: "mock-provider",
|
|
943
|
+
specificationVersion: "v2" as const,
|
|
944
|
+
supportedUrls: {},
|
|
945
|
+
};
|
|
946
|
+
const customApp = setupServer({
|
|
947
|
+
addRoutes: (router, options) => {
|
|
948
|
+
addGptRoutes(router, {
|
|
949
|
+
aiService: new AIService({model: noStepModel as unknown as LanguageModel}),
|
|
950
|
+
openApiOptions: options,
|
|
951
|
+
});
|
|
952
|
+
},
|
|
953
|
+
skipListen: true,
|
|
954
|
+
userModel: UserModel,
|
|
955
|
+
});
|
|
956
|
+
const agent = await authAsUser(customApp, "notAdmin");
|
|
957
|
+
const res = await agent
|
|
958
|
+
.post("/gpt/prompt")
|
|
959
|
+
.send({prompt: "Hi"})
|
|
960
|
+
.buffer(true)
|
|
961
|
+
.parse(sseCollect);
|
|
962
|
+
expect(res.status).toBe(200);
|
|
963
|
+
const body = (res as SseResponse).body;
|
|
964
|
+
expect(body).toContain("buffered tail");
|
|
965
|
+
});
|
|
966
|
+
|
|
919
967
|
it("uses per-request tools via createRequestTools", async () => {
|
|
920
968
|
const createRequestTools = mock(() => ({
|
|
921
969
|
localLookup: {description: "Per-request tool"} as unknown as Tool,
|
|
@@ -1101,7 +1149,6 @@ describe("AI Routes", () => {
|
|
|
1101
1149
|
|
|
1102
1150
|
it("handles AIRequest.logRequest failure gracefully", async () => {
|
|
1103
1151
|
const originalLogRequest = AIRequest.logRequest;
|
|
1104
|
-
// biome-ignore lint/suspicious/noExplicitAny: Override static method for test mock.
|
|
1105
1152
|
AIRequest.logRequest = mock(async () => {
|
|
1106
1153
|
throw new Error("database write failed");
|
|
1107
1154
|
}) as unknown as typeof AIRequest.logRequest;
|
|
@@ -1417,6 +1464,22 @@ describe("AI Routes", () => {
|
|
|
1417
1464
|
expect(res.body.total).toBe(1);
|
|
1418
1465
|
});
|
|
1419
1466
|
|
|
1467
|
+
it("returns 400 for invalid startDate format", async () => {
|
|
1468
|
+
const agent = await authAsUser(app, "admin");
|
|
1469
|
+
const res = await agent.get("/aiRequestsExplorer?startDate=not-a-date");
|
|
1470
|
+
|
|
1471
|
+
expect(res.status).toBe(400);
|
|
1472
|
+
expect(res.body.title).toContain("Invalid startDate");
|
|
1473
|
+
});
|
|
1474
|
+
|
|
1475
|
+
it("returns 400 for invalid endDate format", async () => {
|
|
1476
|
+
const agent = await authAsUser(app, "admin");
|
|
1477
|
+
const res = await agent.get("/aiRequestsExplorer?endDate=not-a-date");
|
|
1478
|
+
|
|
1479
|
+
expect(res.status).toBe(400);
|
|
1480
|
+
expect(res.body.title).toContain("Invalid endDate");
|
|
1481
|
+
});
|
|
1482
|
+
|
|
1420
1483
|
it("filters by startDate and endDate range", async () => {
|
|
1421
1484
|
const a = await AIRequest.create({
|
|
1422
1485
|
aiModel: "gpt-4",
|
package/src/service/aiService.ts
CHANGED
|
@@ -39,8 +39,8 @@ const getModelId = (model: LanguageModel): string => {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
export class AIService {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
readonly model: LanguageModel;
|
|
43
|
+
readonly defaultTemperature: number;
|
|
44
44
|
|
|
45
45
|
constructor({model, defaultTemperature = TemperaturePresets.DEFAULT}: AIServiceOptions) {
|
|
46
46
|
this.model = model;
|
|
@@ -216,13 +216,13 @@ export class AIService {
|
|
|
216
216
|
parts.push({image: new URL(part.url), mediaType: part.mimeType, type: "image"});
|
|
217
217
|
} else if (part.type === "file") {
|
|
218
218
|
logger.debug("Building file message part", {
|
|
219
|
-
filename:
|
|
219
|
+
filename: part.filename,
|
|
220
220
|
mediaType: part.mimeType,
|
|
221
221
|
urlPrefix: part.url?.substring(0, 50),
|
|
222
222
|
});
|
|
223
223
|
parts.push({
|
|
224
224
|
data: new URL(part.url),
|
|
225
|
-
filename:
|
|
225
|
+
filename: part.filename,
|
|
226
226
|
mediaType: part.mimeType,
|
|
227
227
|
type: "file",
|
|
228
228
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {Storage, type StorageOptions} from "@google-cloud/storage";
|
|
2
|
+
import {DateTime} from "luxon";
|
|
2
3
|
|
|
3
4
|
import {FileAttachment} from "../models/fileAttachment";
|
|
4
5
|
|
|
@@ -31,7 +32,7 @@ export class FileStorageService {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
async upload({buffer, filename, mimeType, userId}: UploadFileParams): Promise<UploadFileResult> {
|
|
34
|
-
const timestamp =
|
|
35
|
+
const timestamp = DateTime.now().toMillis();
|
|
35
36
|
const sanitizedFilename = filename.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
36
37
|
const gcsKey = `uploads/${userId.toString()}/${timestamp}-${sanitizedFilename}`;
|
|
37
38
|
|
|
@@ -59,7 +60,7 @@ export class FileStorageService {
|
|
|
59
60
|
const file = this.bucket.file(gcsKey);
|
|
60
61
|
const [url] = await file.getSignedUrl({
|
|
61
62
|
action: "read",
|
|
62
|
-
expires:
|
|
63
|
+
expires: DateTime.now().plus({hours: 1}).toMillis(),
|
|
63
64
|
version: "v4",
|
|
64
65
|
});
|
|
65
66
|
return url;
|