@terreno/ai 0.11.9 → 0.12.1

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/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "@types/multer": "^1.4.12",
23
23
  "@types/passport-local-mongoose": "^6.1.5",
24
24
  "@types/supertest": "^6.0.2",
25
- "@terreno/api": "0.11.9",
25
+ "@terreno/api": "0.12.1",
26
26
  "mongoose": "8.23.0",
27
27
  "passport-local-mongoose": "^9.0.1",
28
28
  "supertest": "^7.0.0",
@@ -63,7 +63,7 @@
63
63
  "access": "public"
64
64
  },
65
65
  "peerDependencies": {
66
- "@terreno/api": "0.11.9",
66
+ "@terreno/api": "0.12.1",
67
67
  "mongoose": "^8.0.0"
68
68
  },
69
69
  "repository": {
@@ -82,5 +82,5 @@
82
82
  "test:coverage": "bun run ../scripts/check-coverage.ts"
83
83
  },
84
84
  "types": "dist/index.d.ts",
85
- "version": "0.11.9"
85
+ "version": "0.12.1"
86
86
  }
@@ -1,6 +1,6 @@
1
1
  import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
2
2
  import {createdUpdatedPlugin, setupServer} from "@terreno/api";
3
- import type {LanguageModel, Tool} from "ai";
3
+ import {jsonSchema, type LanguageModel, type Tool, tool} from "ai";
4
4
  import type express from "express";
5
5
  import mongoose from "mongoose";
6
6
  import passportLocalMongoose from "passport-local-mongoose";
@@ -701,6 +701,90 @@ describe("AI Routes", () => {
701
701
  expect(body).toContain("toolResult");
702
702
  });
703
703
 
704
+ it("emits a file SSE event when a tool execution returns fileData", async () => {
705
+ let toolCallEmitted = false;
706
+ const fileToolModel = {
707
+ doGenerate: mock(async () => ({
708
+ content: [{text: "ok", type: "text" as const}],
709
+ finishReason: "stop" as const,
710
+ usage: {inputTokens: 1, outputTokens: 1},
711
+ })),
712
+ doStream: mock(async () => ({
713
+ stream: new ReadableStream({
714
+ start(controller) {
715
+ if (!toolCallEmitted) {
716
+ toolCallEmitted = true;
717
+ controller.enqueue({id: "t1", type: "text-start" as const});
718
+ controller.enqueue({
719
+ delta: "Generating",
720
+ id: "t1",
721
+ type: "text-delta" as const,
722
+ });
723
+ controller.enqueue({id: "t1", type: "text-end" as const});
724
+ controller.enqueue({
725
+ input: "{}",
726
+ toolCallId: "tc1",
727
+ toolName: "fileTool",
728
+ type: "tool-call" as const,
729
+ });
730
+ controller.enqueue({
731
+ finishReason: "tool-calls" as const,
732
+ type: "finish" as const,
733
+ usage: {inputTokens: 1, outputTokens: 1},
734
+ });
735
+ } else {
736
+ // Second step after tool execution: just finish.
737
+ controller.enqueue({id: "t2", type: "text-start" as const});
738
+ controller.enqueue({delta: "done", id: "t2", type: "text-delta" as const});
739
+ controller.enqueue({id: "t2", type: "text-end" as const});
740
+ controller.enqueue({
741
+ finishReason: "stop" as const,
742
+ type: "finish" as const,
743
+ usage: {inputTokens: 1, outputTokens: 1},
744
+ });
745
+ }
746
+ controller.close();
747
+ },
748
+ }),
749
+ })),
750
+ modelId: "file-tool-model",
751
+ provider: "mock-provider",
752
+ specificationVersion: "v2" as const,
753
+ supportedUrls: {},
754
+ };
755
+ const fileTool = tool({
756
+ description: "Returns a generated file",
757
+ execute: async () => ({
758
+ fileData: "data:application/pdf;base64,AAAA",
759
+ filename: "result.pdf",
760
+ mimeType: "application/pdf",
761
+ }),
762
+ inputSchema: jsonSchema({properties: {}, type: "object"}),
763
+ });
764
+ const fileToolApp = setupServer({
765
+ addRoutes: (router, options) => {
766
+ addGptRoutes(router, {
767
+ aiService: new AIService({model: fileToolModel as unknown as LanguageModel}),
768
+ openApiOptions: options,
769
+ tools: {fileTool},
770
+ });
771
+ },
772
+ skipListen: true,
773
+ userModel: UserModel,
774
+ });
775
+ const agent = await authAsUser(fileToolApp, "notAdmin");
776
+ const res = await agent
777
+ .post("/gpt/prompt")
778
+ .send({prompt: "make a file"})
779
+ .buffer(true)
780
+ .parse(sseCollect);
781
+ expect(res.status).toBe(200);
782
+ const body = (res as SseResponse).body;
783
+ expect(body).toContain("file");
784
+ expect(body).toContain("result.pdf");
785
+ expect(body).toContain("application/pdf");
786
+ });
787
+
704
788
  it("forwards mid-stream error parts as SSE error events", async () => {
705
789
  const errEventModel = {
706
790
  doGenerate: mock(async () => ({