@fragno-dev/chatno 0.0.10 → 0.0.12
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/CHANGELOG.md +17 -0
- package/LICENSE.md +16 -0
- package/package.json +13 -12
- package/src/index.test.ts +66 -0
- package/src/index.ts +36 -20
- package/src/vitest.config.ts +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @fragno-dev/chatno
|
|
2
2
|
|
|
3
|
+
## 0.0.12
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [ca57fac]
|
|
8
|
+
- @fragno-dev/core@0.1.4
|
|
9
|
+
|
|
10
|
+
## 0.0.11
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- bef9f6c: feat(testing): add `createFragmentForTest` test helper to easily test API routes defined
|
|
15
|
+
in Fragments
|
|
16
|
+
- Updated dependencies [bef9f6c]
|
|
17
|
+
- Updated dependencies [711226d]
|
|
18
|
+
- @fragno-dev/core@0.1.3
|
|
19
|
+
|
|
3
20
|
## 0.0.10
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Copyright 2025 - present "ReJot Nederland B.V.", and individual contributors.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
4
|
+
associated documentation files (the “Software”), to deal in the Software without restriction,
|
|
5
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
6
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
7
|
+
furnished to do so, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
10
|
+
portions of the Software.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
13
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
14
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
15
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
16
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fragno-dev/chatno",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
6
|
"development": {
|
|
7
7
|
"default": "./src/index.ts"
|
|
8
8
|
},
|
|
9
|
-
"bun": "./src/index.ts",
|
|
10
9
|
"types": "./dist/index.d.ts",
|
|
11
10
|
"default": "./dist/node/index.js"
|
|
12
11
|
},
|
|
@@ -54,17 +53,12 @@
|
|
|
54
53
|
"main": "./dist/node/index.js",
|
|
55
54
|
"module": "./dist/node/index.js",
|
|
56
55
|
"types": "./dist/node/index.d.ts",
|
|
57
|
-
"scripts": {
|
|
58
|
-
"build": "tsdown",
|
|
59
|
-
"build:watch": "tsdown --watch",
|
|
60
|
-
"types:check": "tsc --noEmit"
|
|
61
|
-
},
|
|
62
56
|
"type": "module",
|
|
63
57
|
"dependencies": {
|
|
64
|
-
"@fragno-dev/core": "0.1.2",
|
|
65
58
|
"nanostores": "^1.0.1",
|
|
66
59
|
"openai": "^5.20.0",
|
|
67
|
-
"zod": "^4.0.5"
|
|
60
|
+
"zod": "^4.0.5",
|
|
61
|
+
"@fragno-dev/core": "0.1.4"
|
|
68
62
|
},
|
|
69
63
|
"peerDependencies": {
|
|
70
64
|
"typescript": "^5.9.3",
|
|
@@ -74,8 +68,15 @@
|
|
|
74
68
|
"solid-js": ">=1.0.0"
|
|
75
69
|
},
|
|
76
70
|
"devDependencies": {
|
|
77
|
-
"@types/node": "^
|
|
71
|
+
"@types/node": "^22",
|
|
78
72
|
"@fragno-private/typescript-config": "0.0.1",
|
|
79
|
-
"@fragno-dev/unplugin-fragno": "0.0.
|
|
73
|
+
"@fragno-dev/unplugin-fragno": "0.0.3"
|
|
74
|
+
},
|
|
75
|
+
"scripts": {
|
|
76
|
+
"build": "tsdown",
|
|
77
|
+
"build:watch": "tsdown --watch",
|
|
78
|
+
"types:check": "tsc --noEmit",
|
|
79
|
+
"test": "vitest run",
|
|
80
|
+
"test:watch": "vitest --watch"
|
|
80
81
|
}
|
|
81
|
-
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, it, expect, assert } from "vitest";
|
|
2
|
+
import { createFragmentForTest } from "@fragno-dev/core/test";
|
|
3
|
+
import { chatnoDefinition, routes } from "./index";
|
|
4
|
+
|
|
5
|
+
describe("chatno", () => {
|
|
6
|
+
const fragment = createFragmentForTest(chatnoDefinition, {
|
|
7
|
+
config: {
|
|
8
|
+
openaiApiKey: "test-key",
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const [, healthRoute, simpleStreamRoute] = fragment.initRoutes(routes);
|
|
13
|
+
|
|
14
|
+
it("services - should return OpenAI URL from getOpenAIURL service", () => {
|
|
15
|
+
const openaiURL = fragment.services.getOpenAIURL();
|
|
16
|
+
|
|
17
|
+
expect(openaiURL).toBe("https://api.openai.com/v1");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("services - should return simple stream", async () => {
|
|
21
|
+
const collected = await Array.fromAsync(fragment.services.generateStreamMessages());
|
|
22
|
+
expect(collected).toEqual([
|
|
23
|
+
{ message: "Item 1" },
|
|
24
|
+
{ message: "Item 2" },
|
|
25
|
+
{ message: "Item 3" },
|
|
26
|
+
{ message: "Item 4" },
|
|
27
|
+
{ message: "Item 5" },
|
|
28
|
+
{ message: "Item 6" },
|
|
29
|
+
{ message: "Item 7" },
|
|
30
|
+
{ message: "Item 8" },
|
|
31
|
+
{ message: "Item 9" },
|
|
32
|
+
{ message: "Item 10" },
|
|
33
|
+
]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("routes - should return simple stream", async () => {
|
|
37
|
+
const response = await fragment.handler(simpleStreamRoute);
|
|
38
|
+
|
|
39
|
+
assert(response.type === "jsonStream");
|
|
40
|
+
const items = [];
|
|
41
|
+
for await (const item of response.stream) {
|
|
42
|
+
items.push(item);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
expect(items).toEqual([
|
|
46
|
+
{ message: "Item 1" },
|
|
47
|
+
{ message: "Item 2" },
|
|
48
|
+
{ message: "Item 3" },
|
|
49
|
+
{ message: "Item 4" },
|
|
50
|
+
{ message: "Item 5" },
|
|
51
|
+
{ message: "Item 6" },
|
|
52
|
+
{ message: "Item 7" },
|
|
53
|
+
{ message: "Item 8" },
|
|
54
|
+
{ message: "Item 9" },
|
|
55
|
+
{ message: "Item 10" },
|
|
56
|
+
]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("routes - should test health route", async () => {
|
|
60
|
+
const response = await fragment.handler(healthRoute);
|
|
61
|
+
|
|
62
|
+
assert(response.type === "json");
|
|
63
|
+
expect(response.status).toBe(200);
|
|
64
|
+
expect(response.data).toEqual({ status: "ok" });
|
|
65
|
+
});
|
|
66
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
createFragment,
|
|
5
5
|
type FragnoPublicClientConfig,
|
|
6
6
|
type FragnoPublicConfig,
|
|
7
|
+
defineRoutes,
|
|
7
8
|
} from "@fragno-dev/core";
|
|
8
9
|
import OpenAI from "openai";
|
|
9
10
|
import { z } from "zod";
|
|
@@ -28,27 +29,13 @@ const healthRoute = defineRoute({
|
|
|
28
29
|
},
|
|
29
30
|
});
|
|
30
31
|
|
|
31
|
-
const simpleStreamRoute = defineRoute({
|
|
32
|
-
method: "GET",
|
|
33
|
-
path: "/simple-stream",
|
|
34
|
-
outputSchema: z.array(
|
|
35
|
-
z.object({
|
|
36
|
-
message: z.string(),
|
|
37
|
-
}),
|
|
38
|
-
),
|
|
39
|
-
handler: async (_ctx, { jsonStream }) => {
|
|
40
|
-
return jsonStream(async (stream) => {
|
|
41
|
-
for (let i = 0; i < 10; i++) {
|
|
42
|
-
await stream.sleep(500);
|
|
43
|
-
await stream.write({ message: `Item ${i + 1}` });
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
|
|
49
32
|
const DEFAULT_SYSTEM_PROMPT = `You are an AI assistant integrated into a dashboard.`;
|
|
50
33
|
|
|
51
|
-
|
|
34
|
+
interface SimpleStreamService {
|
|
35
|
+
generateStreamMessages: () => AsyncGenerator<{ message: string }>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const chatnoDefinition = defineFragment<ChatnoServerConfig>("chatno")
|
|
52
39
|
.withDependencies(({ config }) => {
|
|
53
40
|
return {
|
|
54
41
|
openaiClient: new OpenAI({
|
|
@@ -59,10 +46,39 @@ const chatnoDefinition = defineFragment<ChatnoServerConfig>("chatno")
|
|
|
59
46
|
.withServices(({ deps }) => {
|
|
60
47
|
return {
|
|
61
48
|
getOpenAIURL: () => deps.openaiClient.baseURL,
|
|
49
|
+
generateStreamMessages: async function* () {
|
|
50
|
+
for (let i = 0; i < 10; i++) {
|
|
51
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
52
|
+
yield { message: `Item ${i + 1}` };
|
|
53
|
+
}
|
|
54
|
+
},
|
|
62
55
|
};
|
|
63
56
|
});
|
|
64
57
|
|
|
65
|
-
const
|
|
58
|
+
const simpleStreamRoute = defineRoutes<ChatnoServerConfig, {}, SimpleStreamService>().create(
|
|
59
|
+
({ services }) => {
|
|
60
|
+
return [
|
|
61
|
+
defineRoute({
|
|
62
|
+
method: "GET",
|
|
63
|
+
path: "/simple-stream",
|
|
64
|
+
outputSchema: z.array(
|
|
65
|
+
z.object({
|
|
66
|
+
message: z.string(),
|
|
67
|
+
}),
|
|
68
|
+
),
|
|
69
|
+
handler: async (_ctx, { jsonStream }) => {
|
|
70
|
+
return jsonStream(async (stream) => {
|
|
71
|
+
for await (const item of services.generateStreamMessages()) {
|
|
72
|
+
await stream.write(item);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
];
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
export const routes = [chatRouteFactory, healthRoute, simpleStreamRoute] as const;
|
|
66
82
|
|
|
67
83
|
// Server-side factory
|
|
68
84
|
export function createChatno(
|