@terreno/ai 0.3.2
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/.claude/CLAUDE.local.md +208 -0
- package/.cursor/rules/00-root.mdc +342 -0
- package/.cursorrules +221 -0
- package/.github/copilot-instructions.md +337 -0
- package/AGENTS.md +337 -0
- package/biome.jsonc +20 -0
- package/bunfig.toml +3 -0
- package/dist/aiApp.d.ts +59 -0
- package/dist/aiApp.js +67 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/models/aiRequest.d.ts +2 -0
- package/dist/models/aiRequest.js +76 -0
- package/dist/models/aiRequest.test.d.ts +1 -0
- package/dist/models/aiRequest.test.js +187 -0
- package/dist/models/fileAttachment.d.ts +2 -0
- package/dist/models/fileAttachment.js +25 -0
- package/dist/models/fileAttachment.test.d.ts +1 -0
- package/dist/models/fileAttachment.test.js +217 -0
- package/dist/models/gptHistory.d.ts +2 -0
- package/dist/models/gptHistory.js +56 -0
- package/dist/models/gptHistory.test.d.ts +1 -0
- package/dist/models/gptHistory.test.js +336 -0
- package/dist/models/index.d.ts +3 -0
- package/dist/models/index.js +9 -0
- package/dist/routes/aiRequestsExplorer.d.ts +2 -0
- package/dist/routes/aiRequestsExplorer.js +153 -0
- package/dist/routes/files.d.ts +5 -0
- package/dist/routes/files.js +168 -0
- package/dist/routes/gpt.d.ts +2 -0
- package/dist/routes/gpt.js +593 -0
- package/dist/routes/gptHistories.d.ts +3 -0
- package/dist/routes/gptHistories.js +29 -0
- package/dist/routes/index.d.ts +5 -0
- package/dist/routes/index.js +13 -0
- package/dist/routes/mcp.d.ts +2 -0
- package/dist/routes/mcp.js +133 -0
- package/dist/routes/routes.test.d.ts +1 -0
- package/dist/routes/routes.test.js +303 -0
- package/dist/service/aiService.d.ts +24 -0
- package/dist/service/aiService.js +453 -0
- package/dist/service/aiService.test.d.ts +1 -0
- package/dist/service/aiService.test.js +367 -0
- package/dist/service/fileStorage.d.ts +24 -0
- package/dist/service/fileStorage.js +143 -0
- package/dist/service/index.d.ts +4 -0
- package/dist/service/index.js +15 -0
- package/dist/service/mcpService.d.ts +14 -0
- package/dist/service/mcpService.js +271 -0
- package/dist/service/prompts.d.ts +4 -0
- package/dist/service/prompts.js +14 -0
- package/dist/tests/bunSetup.d.ts +1 -0
- package/dist/tests/bunSetup.js +227 -0
- package/dist/types/index.d.ts +170 -0
- package/dist/types/index.js +7 -0
- package/package.json +55 -0
- package/src/aiApp.ts +110 -0
- package/src/index.ts +6 -0
- package/src/models/aiRequest.test.ts +101 -0
- package/src/models/aiRequest.ts +45 -0
- package/src/models/fileAttachment.test.ts +119 -0
- package/src/models/fileAttachment.ts +31 -0
- package/src/models/gptHistory.test.ts +209 -0
- package/src/models/gptHistory.ts +71 -0
- package/src/models/index.ts +3 -0
- package/src/routes/aiRequestsExplorer.ts +107 -0
- package/src/routes/files.ts +128 -0
- package/src/routes/gpt.ts +503 -0
- package/src/routes/gptHistories.ts +31 -0
- package/src/routes/index.ts +5 -0
- package/src/routes/mcp.ts +76 -0
- package/src/routes/routes.test.ts +168 -0
- package/src/service/aiService.test.ts +242 -0
- package/src/service/aiService.ts +269 -0
- package/src/service/fileStorage.ts +74 -0
- package/src/service/index.ts +9 -0
- package/src/service/mcpService.ts +120 -0
- package/src/service/prompts.ts +18 -0
- package/src/tests/bunSetup.ts +163 -0
- package/src/types/index.ts +224 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { FindExactlyOnePlugin, FindOneOrNonePlugin } from "@terreno/api";
|
|
2
|
+
import type { LanguageModel } from "ai";
|
|
3
|
+
import type mongoose from "mongoose";
|
|
4
|
+
export declare const AI_REQUEST_TYPES: readonly ["general", "remix", "summarization", "translation"];
|
|
5
|
+
export type AIRequestType = (typeof AI_REQUEST_TYPES)[number];
|
|
6
|
+
export type AIRequestDocument = mongoose.Document<mongoose.Types.ObjectId> & {
|
|
7
|
+
created: Date;
|
|
8
|
+
deleted: boolean;
|
|
9
|
+
error?: string;
|
|
10
|
+
metadata?: Record<string, unknown>;
|
|
11
|
+
aiModel: string;
|
|
12
|
+
prompt: string;
|
|
13
|
+
requestType: AIRequestType;
|
|
14
|
+
response?: string;
|
|
15
|
+
responseTime?: number;
|
|
16
|
+
tokensUsed?: number;
|
|
17
|
+
updated: Date;
|
|
18
|
+
userId?: mongoose.Types.ObjectId;
|
|
19
|
+
};
|
|
20
|
+
export interface AIRequestStatics extends FindExactlyOnePlugin<AIRequestDocument>, FindOneOrNonePlugin<AIRequestDocument> {
|
|
21
|
+
logRequest(params: LogRequestParams): Promise<AIRequestDocument>;
|
|
22
|
+
}
|
|
23
|
+
export type AIRequestModel = mongoose.Model<AIRequestDocument> & AIRequestStatics;
|
|
24
|
+
export interface LogRequestParams {
|
|
25
|
+
error?: string;
|
|
26
|
+
metadata?: Record<string, unknown>;
|
|
27
|
+
aiModel: string;
|
|
28
|
+
prompt: string;
|
|
29
|
+
requestType: AIRequestType;
|
|
30
|
+
response?: string;
|
|
31
|
+
responseTime?: number;
|
|
32
|
+
tokensUsed?: number;
|
|
33
|
+
userId?: mongoose.Types.ObjectId;
|
|
34
|
+
}
|
|
35
|
+
export interface TextContentPart {
|
|
36
|
+
type: "text";
|
|
37
|
+
text: string;
|
|
38
|
+
}
|
|
39
|
+
export interface ImageContentPart {
|
|
40
|
+
type: "image";
|
|
41
|
+
url: string;
|
|
42
|
+
mimeType?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface FileContentPart {
|
|
45
|
+
type: "file";
|
|
46
|
+
url: string;
|
|
47
|
+
filename?: string;
|
|
48
|
+
mimeType: string;
|
|
49
|
+
}
|
|
50
|
+
export type MessageContentPart = TextContentPart | ImageContentPart | FileContentPart;
|
|
51
|
+
export interface GptHistoryPrompt {
|
|
52
|
+
model?: string;
|
|
53
|
+
rating?: "up" | "down";
|
|
54
|
+
text: string;
|
|
55
|
+
type: "user" | "assistant" | "system" | "tool-call" | "tool-result";
|
|
56
|
+
content?: MessageContentPart[];
|
|
57
|
+
toolCallId?: string;
|
|
58
|
+
toolName?: string;
|
|
59
|
+
args?: Record<string, unknown>;
|
|
60
|
+
result?: unknown;
|
|
61
|
+
}
|
|
62
|
+
export type GptHistoryDocument = mongoose.Document<mongoose.Types.ObjectId> & {
|
|
63
|
+
created: Date;
|
|
64
|
+
deleted: boolean;
|
|
65
|
+
prompts: GptHistoryPrompt[];
|
|
66
|
+
title?: string;
|
|
67
|
+
updated: Date;
|
|
68
|
+
userId: mongoose.Types.ObjectId;
|
|
69
|
+
};
|
|
70
|
+
export interface GptHistoryStatics extends FindExactlyOnePlugin<GptHistoryDocument>, FindOneOrNonePlugin<GptHistoryDocument> {
|
|
71
|
+
}
|
|
72
|
+
export type GptHistoryModel = mongoose.Model<GptHistoryDocument> & GptHistoryStatics;
|
|
73
|
+
export interface AIServiceOptions {
|
|
74
|
+
defaultTemperature?: number;
|
|
75
|
+
model: LanguageModel;
|
|
76
|
+
}
|
|
77
|
+
export interface GenerateTextOptions {
|
|
78
|
+
maxOutputTokens?: number;
|
|
79
|
+
prompt: string;
|
|
80
|
+
stopWhen?: import("ai").StopCondition<any>;
|
|
81
|
+
systemPrompt?: string;
|
|
82
|
+
temperature?: number;
|
|
83
|
+
toolChoice?: "auto" | "none" | "required";
|
|
84
|
+
tools?: Record<string, import("ai").Tool>;
|
|
85
|
+
userId?: mongoose.Types.ObjectId;
|
|
86
|
+
}
|
|
87
|
+
export interface GenerateStreamOptions {
|
|
88
|
+
maxOutputTokens?: number;
|
|
89
|
+
prompt: string;
|
|
90
|
+
systemPrompt?: string;
|
|
91
|
+
temperature?: number;
|
|
92
|
+
userId?: mongoose.Types.ObjectId;
|
|
93
|
+
}
|
|
94
|
+
export interface GenerateChatStreamOptions {
|
|
95
|
+
messages: Array<{
|
|
96
|
+
content: string;
|
|
97
|
+
role: "user" | "assistant" | "system";
|
|
98
|
+
}>;
|
|
99
|
+
stopWhen?: import("ai").StopCondition<any>;
|
|
100
|
+
systemPrompt?: string;
|
|
101
|
+
toolChoice?: "auto" | "none" | "required";
|
|
102
|
+
tools?: Record<string, import("ai").Tool>;
|
|
103
|
+
userId?: mongoose.Types.ObjectId;
|
|
104
|
+
}
|
|
105
|
+
export interface RemixOptions {
|
|
106
|
+
text: string;
|
|
107
|
+
userId?: mongoose.Types.ObjectId;
|
|
108
|
+
}
|
|
109
|
+
export interface SummaryOptions {
|
|
110
|
+
text: string;
|
|
111
|
+
userId?: mongoose.Types.ObjectId;
|
|
112
|
+
}
|
|
113
|
+
export interface TranslateOptions {
|
|
114
|
+
sourceLanguage?: string;
|
|
115
|
+
targetLanguage: string;
|
|
116
|
+
text: string;
|
|
117
|
+
userId?: mongoose.Types.ObjectId;
|
|
118
|
+
}
|
|
119
|
+
export interface GptRouteOptions {
|
|
120
|
+
/** Pre-configured AIService. Optional when using per-request keys or demo mode. */
|
|
121
|
+
aiService?: import("../service/aiService").AIService;
|
|
122
|
+
/** Factory to create a LanguageModel from a per-request API key (x-ai-api-key header). */
|
|
123
|
+
createModelFn?: (apiKey: string, modelId?: string) => import("ai").LanguageModel;
|
|
124
|
+
/** Factory to create per-request tools (e.g. tools that need the request's API key). Merged with static tools. */
|
|
125
|
+
createRequestTools?: (req: import("express").Request) => Record<string, import("ai").Tool>;
|
|
126
|
+
/** Return canned responses when no AI service is available. */
|
|
127
|
+
demoMode?: boolean;
|
|
128
|
+
mcpService?: import("../service/mcpService").MCPService;
|
|
129
|
+
openApiOptions?: Record<string, unknown>;
|
|
130
|
+
tools?: Record<string, import("ai").Tool>;
|
|
131
|
+
toolChoice?: "auto" | "none" | "required";
|
|
132
|
+
maxSteps?: number;
|
|
133
|
+
}
|
|
134
|
+
export interface GptHistoryRouteOptions {
|
|
135
|
+
openApiOptions?: Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
export interface AiRequestsExplorerRouteOptions {
|
|
138
|
+
openApiOptions?: Record<string, unknown>;
|
|
139
|
+
}
|
|
140
|
+
export interface FileRouteOptions {
|
|
141
|
+
gcsBucket: string;
|
|
142
|
+
maxFileSize?: number;
|
|
143
|
+
openApiOptions?: Record<string, unknown>;
|
|
144
|
+
}
|
|
145
|
+
export interface McpRouteOptions {
|
|
146
|
+
mcpService: import("../service/mcpService").MCPService;
|
|
147
|
+
openApiOptions?: Record<string, unknown>;
|
|
148
|
+
}
|
|
149
|
+
export interface FileAttachmentDocument extends mongoose.Document<mongoose.Types.ObjectId> {
|
|
150
|
+
created: Date;
|
|
151
|
+
deleted: boolean;
|
|
152
|
+
filename: string;
|
|
153
|
+
gcsKey: string;
|
|
154
|
+
mimeType: string;
|
|
155
|
+
size: number;
|
|
156
|
+
updated: Date;
|
|
157
|
+
url: string;
|
|
158
|
+
userId: mongoose.Types.ObjectId;
|
|
159
|
+
}
|
|
160
|
+
export interface FileAttachmentStatics extends FindExactlyOnePlugin<FileAttachmentDocument>, FindOneOrNonePlugin<FileAttachmentDocument> {
|
|
161
|
+
}
|
|
162
|
+
export type FileAttachmentModel = mongoose.Model<FileAttachmentDocument> & FileAttachmentStatics;
|
|
163
|
+
export interface MCPServerConfig {
|
|
164
|
+
name: string;
|
|
165
|
+
transport: {
|
|
166
|
+
type: "sse";
|
|
167
|
+
url: string;
|
|
168
|
+
headers?: Record<string, string>;
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AI_REQUEST_TYPES = void 0;
|
|
4
|
+
// ============================================================
|
|
5
|
+
// AIRequest Types
|
|
6
|
+
// ============================================================
|
|
7
|
+
exports.AI_REQUEST_TYPES = ["general", "remix", "summarization", "translation"];
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Josh Gachnang",
|
|
3
|
+
"bugs": {
|
|
4
|
+
"url": "https://github.com/FlourishHealth/terreno/issues"
|
|
5
|
+
},
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@ai-sdk/mcp": "^1.0.21",
|
|
8
|
+
"@google-cloud/storage": "^7.15.0",
|
|
9
|
+
"@terreno/api": "0.3.2",
|
|
10
|
+
"ai": "^6.0.97",
|
|
11
|
+
"multer": "^1.4.5-lts.1"
|
|
12
|
+
},
|
|
13
|
+
"description": "AI service layer for Terreno apps - GPT chat, request logging, and admin tools.",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@biomejs/biome": "^2.3.6",
|
|
16
|
+
"@types/bun": "^1.2.4",
|
|
17
|
+
"@types/express": "^4.17.21",
|
|
18
|
+
"@types/multer": "^1.4.12",
|
|
19
|
+
"@types/passport-local-mongoose": "^6.1.5",
|
|
20
|
+
"@types/supertest": "^6.0.2",
|
|
21
|
+
"passport-local-mongoose": "^9.0.1",
|
|
22
|
+
"supertest": "^7.0.0",
|
|
23
|
+
"typescript": "5.8.2",
|
|
24
|
+
"winston": "^3.18.3"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/FlourishHealth/terreno#readme",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"ai",
|
|
29
|
+
"gpt",
|
|
30
|
+
"mongoose",
|
|
31
|
+
"express"
|
|
32
|
+
],
|
|
33
|
+
"license": "Apache-2.0",
|
|
34
|
+
"main": "dist/index.js",
|
|
35
|
+
"name": "@terreno/ai",
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@terreno/api": "0.3.2",
|
|
38
|
+
"mongoose": "^8.0.0"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+ssh://git@github.com/FlourishHealth/terreno.git"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"compile": "bun tsc",
|
|
46
|
+
"dev": "bun tsc -w",
|
|
47
|
+
"lint": "biome check ./src",
|
|
48
|
+
"lint:fix": "biome check --write ./src",
|
|
49
|
+
"lint:unsafefix": "biome check --fix --unsafe ./src",
|
|
50
|
+
"test": "bun test --preload ./src/tests/bunSetup.ts",
|
|
51
|
+
"test:ci": "bun test --preload ./src/tests/bunSetup.ts"
|
|
52
|
+
},
|
|
53
|
+
"types": "dist/index.d.ts",
|
|
54
|
+
"version": "0.3.2"
|
|
55
|
+
}
|
package/src/aiApp.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type {TerrenoPlugin} from "@terreno/api";
|
|
2
|
+
import type {LanguageModel, Tool} from "ai";
|
|
3
|
+
import type express from "express";
|
|
4
|
+
|
|
5
|
+
import {addAiRequestsExplorerRoutes} from "./routes/aiRequestsExplorer";
|
|
6
|
+
import {addFileRoutes} from "./routes/files";
|
|
7
|
+
import {addGptRoutes} from "./routes/gpt";
|
|
8
|
+
import {addGptHistoryRoutes} from "./routes/gptHistories";
|
|
9
|
+
import {addMcpRoutes} from "./routes/mcp";
|
|
10
|
+
import type {AIService} from "./service/aiService";
|
|
11
|
+
import type {FileStorageService} from "./service/fileStorage";
|
|
12
|
+
import type {MCPService} from "./service/mcpService";
|
|
13
|
+
|
|
14
|
+
export interface AiAppOptions {
|
|
15
|
+
/** Pre-configured AIService instance. Optional when using per-request keys or demo mode. */
|
|
16
|
+
aiService?: AIService;
|
|
17
|
+
/** Factory function to create a LanguageModel from a per-request API key (sent via x-ai-api-key header). */
|
|
18
|
+
createModelFn?: (apiKey: string) => LanguageModel;
|
|
19
|
+
/** When true and no AI service is available, routes return canned demo responses instead of failing. */
|
|
20
|
+
demoMode?: boolean;
|
|
21
|
+
/** File storage service for handling file uploads to GCS. */
|
|
22
|
+
fileStorageService?: FileStorageService;
|
|
23
|
+
/** GCS bucket name for file uploads. Required alongside fileStorageService. */
|
|
24
|
+
gcsBucket?: string;
|
|
25
|
+
/** Maximum number of tool-calling steps per chat request. Defaults to 5 when tools are present. */
|
|
26
|
+
maxSteps?: number;
|
|
27
|
+
/** MCP service for connecting to external tool servers. */
|
|
28
|
+
mcpService?: MCPService;
|
|
29
|
+
/** OpenAPI options passed through to route builders for spec generation. */
|
|
30
|
+
openApiOptions?: Record<string, unknown>;
|
|
31
|
+
/** Tool choice strategy for chat requests. Defaults to "auto" when tools are present. */
|
|
32
|
+
toolChoice?: "auto" | "none" | "required";
|
|
33
|
+
/** Tool definitions available to the AI model during chat. */
|
|
34
|
+
tools?: Record<string, Tool>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* TerrenoPlugin that mounts all AI routes (GPT chat, history, file uploads, MCP, admin explorer).
|
|
39
|
+
*
|
|
40
|
+
* Supports three modes of operation:
|
|
41
|
+
* - **Configured**: Pass an `aiService` for server-wide AI access
|
|
42
|
+
* - **Per-request keys**: Pass a `createModelFn` so clients can supply their own API key via `x-ai-api-key` header
|
|
43
|
+
* - **Demo mode**: When neither service nor per-request key is available, returns a canned response
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import {AiApp, AIService} from "@terreno/ai";
|
|
48
|
+
* import {google} from "@ai-sdk/google";
|
|
49
|
+
*
|
|
50
|
+
* const aiService = new AIService({model: google("gemini-2.5-flash")});
|
|
51
|
+
* new AiApp({aiService, tools: myTools}).register(app);
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // Demo mode with per-request key support (no server-side API key needed)
|
|
57
|
+
* new AiApp({
|
|
58
|
+
* createModelFn: (key) => google("gemini-2.5-flash", {apiKey: key}),
|
|
59
|
+
* demoMode: true,
|
|
60
|
+
* }).register(app);
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export class AiApp implements TerrenoPlugin {
|
|
64
|
+
private options: AiAppOptions;
|
|
65
|
+
|
|
66
|
+
constructor(options: AiAppOptions) {
|
|
67
|
+
this.options = options;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
register(app: express.Application): void {
|
|
71
|
+
const router = app;
|
|
72
|
+
const {
|
|
73
|
+
aiService,
|
|
74
|
+
createModelFn,
|
|
75
|
+
demoMode,
|
|
76
|
+
fileStorageService,
|
|
77
|
+
gcsBucket,
|
|
78
|
+
maxSteps,
|
|
79
|
+
mcpService,
|
|
80
|
+
openApiOptions,
|
|
81
|
+
toolChoice,
|
|
82
|
+
tools,
|
|
83
|
+
} = this.options;
|
|
84
|
+
|
|
85
|
+
addGptHistoryRoutes(router, {openApiOptions});
|
|
86
|
+
addGptRoutes(router, {
|
|
87
|
+
aiService,
|
|
88
|
+
createModelFn,
|
|
89
|
+
demoMode,
|
|
90
|
+
maxSteps,
|
|
91
|
+
mcpService,
|
|
92
|
+
openApiOptions,
|
|
93
|
+
toolChoice,
|
|
94
|
+
tools,
|
|
95
|
+
});
|
|
96
|
+
addAiRequestsExplorerRoutes(router, {openApiOptions});
|
|
97
|
+
|
|
98
|
+
if (fileStorageService && gcsBucket) {
|
|
99
|
+
addFileRoutes(router, {
|
|
100
|
+
fileStorageService,
|
|
101
|
+
gcsBucket,
|
|
102
|
+
openApiOptions,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (mcpService) {
|
|
107
|
+
addMcpRoutes(router, {mcpService, openApiOptions});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import {AIRequest} from "./aiRequest";
|
|
5
|
+
|
|
6
|
+
describe("AIRequest Model", () => {
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
await AIRequest.deleteMany({});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
await AIRequest.deleteMany({});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("schema", () => {
|
|
16
|
+
it("should create an AI request with required fields", async () => {
|
|
17
|
+
const request = await AIRequest.create({
|
|
18
|
+
aiModel: "gpt-4",
|
|
19
|
+
prompt: "Hello",
|
|
20
|
+
requestType: "general",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
expect(request.aiModel).toBe("gpt-4");
|
|
24
|
+
expect(request.prompt).toBe("Hello");
|
|
25
|
+
expect(request.requestType).toBe("general");
|
|
26
|
+
expect(request.created).toBeDefined();
|
|
27
|
+
expect(request.updated).toBeDefined();
|
|
28
|
+
expect(request.deleted).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should create a request with all fields", async () => {
|
|
32
|
+
const userId = new mongoose.Types.ObjectId();
|
|
33
|
+
const request = await AIRequest.create({
|
|
34
|
+
aiModel: "gemini-2.5-flash",
|
|
35
|
+
error: undefined,
|
|
36
|
+
metadata: {key: "value"},
|
|
37
|
+
prompt: "Translate this",
|
|
38
|
+
requestType: "translation",
|
|
39
|
+
response: "Translated text",
|
|
40
|
+
responseTime: 1500,
|
|
41
|
+
tokensUsed: 100,
|
|
42
|
+
userId,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
expect(request.aiModel).toBe("gemini-2.5-flash");
|
|
46
|
+
expect(request.requestType).toBe("translation");
|
|
47
|
+
expect(request.response).toBe("Translated text");
|
|
48
|
+
expect(request.responseTime).toBe(1500);
|
|
49
|
+
expect(request.tokensUsed).toBe(100);
|
|
50
|
+
expect(request.userId?.toString()).toBe(userId.toString());
|
|
51
|
+
expect(request.metadata).toEqual({key: "value"});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should reject invalid request types", async () => {
|
|
55
|
+
await expect(
|
|
56
|
+
AIRequest.create({
|
|
57
|
+
aiModel: "gpt-4",
|
|
58
|
+
prompt: "test",
|
|
59
|
+
requestType: "invalid" as any,
|
|
60
|
+
})
|
|
61
|
+
).rejects.toThrow();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("logRequest static", () => {
|
|
66
|
+
it("should create a request via logRequest", async () => {
|
|
67
|
+
const request = await AIRequest.logRequest({
|
|
68
|
+
aiModel: "gpt-4",
|
|
69
|
+
prompt: "test prompt",
|
|
70
|
+
requestType: "general",
|
|
71
|
+
response: "test response",
|
|
72
|
+
responseTime: 500,
|
|
73
|
+
tokensUsed: 50,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(request._id).toBeDefined();
|
|
77
|
+
expect(request.prompt).toBe("test prompt");
|
|
78
|
+
expect(request.response).toBe("test response");
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("soft delete", () => {
|
|
83
|
+
it("should filter out deleted records by default", async () => {
|
|
84
|
+
await AIRequest.create({
|
|
85
|
+
aiModel: "gpt-4",
|
|
86
|
+
deleted: true,
|
|
87
|
+
prompt: "deleted",
|
|
88
|
+
requestType: "general",
|
|
89
|
+
});
|
|
90
|
+
await AIRequest.create({
|
|
91
|
+
aiModel: "gpt-4",
|
|
92
|
+
prompt: "active",
|
|
93
|
+
requestType: "general",
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const results = await AIRequest.find({});
|
|
97
|
+
expect(results.length).toBe(1);
|
|
98
|
+
expect(results[0].prompt).toBe("active");
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {createdUpdatedPlugin, findExactlyOne, findOneOrNone, isDeletedPlugin} from "@terreno/api";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import type {AIRequestDocument, AIRequestModel, LogRequestParams} from "../types";
|
|
5
|
+
|
|
6
|
+
const aiRequestSchema = new mongoose.Schema<AIRequestDocument, AIRequestModel>(
|
|
7
|
+
{
|
|
8
|
+
aiModel: {required: true, type: String},
|
|
9
|
+
error: {type: String},
|
|
10
|
+
metadata: {type: mongoose.Schema.Types.Mixed},
|
|
11
|
+
prompt: {required: true, type: String},
|
|
12
|
+
requestType: {
|
|
13
|
+
enum: ["general", "remix", "summarization", "translation"],
|
|
14
|
+
required: true,
|
|
15
|
+
type: String,
|
|
16
|
+
},
|
|
17
|
+
response: {type: String},
|
|
18
|
+
responseTime: {type: Number},
|
|
19
|
+
tokensUsed: {type: Number},
|
|
20
|
+
userId: {ref: "User", type: mongoose.Schema.Types.ObjectId},
|
|
21
|
+
},
|
|
22
|
+
{strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
aiRequestSchema.plugin(createdUpdatedPlugin);
|
|
26
|
+
aiRequestSchema.plugin(isDeletedPlugin);
|
|
27
|
+
aiRequestSchema.plugin(findOneOrNone);
|
|
28
|
+
aiRequestSchema.plugin(findExactlyOne);
|
|
29
|
+
|
|
30
|
+
aiRequestSchema.index({created: -1, userId: 1});
|
|
31
|
+
aiRequestSchema.index({aiModel: 1, created: -1});
|
|
32
|
+
aiRequestSchema.index({created: -1, requestType: 1});
|
|
33
|
+
|
|
34
|
+
(aiRequestSchema.statics as any).logRequest = async function (
|
|
35
|
+
this: AIRequestModel,
|
|
36
|
+
params: LogRequestParams
|
|
37
|
+
): Promise<AIRequestDocument> {
|
|
38
|
+
const request = new this(params);
|
|
39
|
+
return request.save();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const AIRequest = mongoose.model<AIRequestDocument, AIRequestModel>(
|
|
43
|
+
"AIRequest",
|
|
44
|
+
aiRequestSchema
|
|
45
|
+
);
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import {FileAttachment} from "./fileAttachment";
|
|
5
|
+
|
|
6
|
+
describe("FileAttachment Model", () => {
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
await FileAttachment.deleteMany({});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
await FileAttachment.deleteMany({});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("schema", () => {
|
|
16
|
+
it("should create a file attachment with required fields", async () => {
|
|
17
|
+
const userId = new mongoose.Types.ObjectId();
|
|
18
|
+
const attachment = await FileAttachment.create({
|
|
19
|
+
filename: "test.pdf",
|
|
20
|
+
gcsKey: "uploads/user123/1234-test.pdf",
|
|
21
|
+
mimeType: "application/pdf",
|
|
22
|
+
size: 1024,
|
|
23
|
+
url: "https://storage.googleapis.com/bucket/uploads/user123/1234-test.pdf",
|
|
24
|
+
userId,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
expect(attachment.filename).toBe("test.pdf");
|
|
28
|
+
expect(attachment.gcsKey).toBe("uploads/user123/1234-test.pdf");
|
|
29
|
+
expect(attachment.mimeType).toBe("application/pdf");
|
|
30
|
+
expect(attachment.size).toBe(1024);
|
|
31
|
+
expect(attachment.url).toContain("storage.googleapis.com");
|
|
32
|
+
expect(attachment.userId.toString()).toBe(userId.toString());
|
|
33
|
+
expect(attachment.created).toBeDefined();
|
|
34
|
+
expect(attachment.deleted).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should require filename", async () => {
|
|
38
|
+
const userId = new mongoose.Types.ObjectId();
|
|
39
|
+
await expect(
|
|
40
|
+
FileAttachment.create({
|
|
41
|
+
gcsKey: "key",
|
|
42
|
+
mimeType: "text/plain",
|
|
43
|
+
size: 100,
|
|
44
|
+
url: "https://example.com/file",
|
|
45
|
+
userId,
|
|
46
|
+
})
|
|
47
|
+
).rejects.toThrow();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should require gcsKey", async () => {
|
|
51
|
+
const userId = new mongoose.Types.ObjectId();
|
|
52
|
+
await expect(
|
|
53
|
+
FileAttachment.create({
|
|
54
|
+
filename: "test.txt",
|
|
55
|
+
mimeType: "text/plain",
|
|
56
|
+
size: 100,
|
|
57
|
+
url: "https://example.com/file",
|
|
58
|
+
userId,
|
|
59
|
+
})
|
|
60
|
+
).rejects.toThrow();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should enforce unique gcsKey", async () => {
|
|
64
|
+
const userId = new mongoose.Types.ObjectId();
|
|
65
|
+
const data = {
|
|
66
|
+
filename: "test.pdf",
|
|
67
|
+
gcsKey: "uploads/unique-key",
|
|
68
|
+
mimeType: "application/pdf",
|
|
69
|
+
size: 1024,
|
|
70
|
+
url: "https://example.com/file",
|
|
71
|
+
userId,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
await FileAttachment.create(data);
|
|
75
|
+
await expect(FileAttachment.create(data)).rejects.toThrow();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should have virtual ownerId field", async () => {
|
|
79
|
+
const userId = new mongoose.Types.ObjectId();
|
|
80
|
+
const attachment = await FileAttachment.create({
|
|
81
|
+
filename: "test.txt",
|
|
82
|
+
gcsKey: "uploads/key",
|
|
83
|
+
mimeType: "text/plain",
|
|
84
|
+
size: 100,
|
|
85
|
+
url: "https://example.com/file",
|
|
86
|
+
userId,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
expect((attachment as any).ownerId.toString()).toBe(userId.toString());
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("soft delete", () => {
|
|
94
|
+
it("should filter out deleted records by default", async () => {
|
|
95
|
+
const userId = new mongoose.Types.ObjectId();
|
|
96
|
+
await FileAttachment.create({
|
|
97
|
+
deleted: true,
|
|
98
|
+
filename: "deleted.txt",
|
|
99
|
+
gcsKey: "uploads/deleted",
|
|
100
|
+
mimeType: "text/plain",
|
|
101
|
+
size: 100,
|
|
102
|
+
url: "https://example.com/deleted",
|
|
103
|
+
userId,
|
|
104
|
+
});
|
|
105
|
+
await FileAttachment.create({
|
|
106
|
+
filename: "active.txt",
|
|
107
|
+
gcsKey: "uploads/active",
|
|
108
|
+
mimeType: "text/plain",
|
|
109
|
+
size: 200,
|
|
110
|
+
url: "https://example.com/active",
|
|
111
|
+
userId,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const results = await FileAttachment.find({});
|
|
115
|
+
expect(results.length).toBe(1);
|
|
116
|
+
expect(results[0].filename).toBe("active.txt");
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {createdUpdatedPlugin, findExactlyOne, findOneOrNone, isDeletedPlugin} from "@terreno/api";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import type {FileAttachmentDocument, FileAttachmentModel} from "../types";
|
|
5
|
+
|
|
6
|
+
const fileAttachmentSchema = new mongoose.Schema<FileAttachmentDocument, FileAttachmentModel>(
|
|
7
|
+
{
|
|
8
|
+
filename: {required: true, type: String},
|
|
9
|
+
gcsKey: {required: true, type: String, unique: true},
|
|
10
|
+
mimeType: {required: true, type: String},
|
|
11
|
+
size: {required: true, type: Number},
|
|
12
|
+
url: {required: true, type: String},
|
|
13
|
+
userId: {index: true, ref: "User", required: true, type: mongoose.Schema.Types.ObjectId},
|
|
14
|
+
},
|
|
15
|
+
{strict: "throw", toJSON: {virtuals: true}, toObject: {virtuals: true}}
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
fileAttachmentSchema.plugin(createdUpdatedPlugin);
|
|
19
|
+
fileAttachmentSchema.plugin(isDeletedPlugin);
|
|
20
|
+
fileAttachmentSchema.plugin(findOneOrNone);
|
|
21
|
+
fileAttachmentSchema.plugin(findExactlyOne);
|
|
22
|
+
|
|
23
|
+
// Virtual ownerId alias so Permissions.IsOwner works with userId field
|
|
24
|
+
fileAttachmentSchema.virtual("ownerId").get(function () {
|
|
25
|
+
return this.userId;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const FileAttachment = mongoose.model<FileAttachmentDocument, FileAttachmentModel>(
|
|
29
|
+
"FileAttachment",
|
|
30
|
+
fileAttachmentSchema
|
|
31
|
+
);
|