@speeq/server 0.0.0-stage → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 speeq contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,103 @@
1
- # Temporary Holding Version
1
+ # @speeq/server
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The [speeq](../../README.md) runtime. It runs **in your backend**: tools use your database and the
4
+ signed-in user's permissions, and conversations go to your own store. speeq (the project) never
5
+ sees them.
6
+
7
+ ```bash
8
+ pnpm add @speeq/server@next @speeq/store-mongodb@next @speeq/llm-openai-compatible@next zod
9
+ ```
10
+
11
+ ## Next.js App Router
12
+
13
+ Supply your own database connection, user resolver, count query and admin check in the example below. For a runnable local setup, follow the [mock quickstart](../../docs/guides/nextjs.md).
14
+
15
+ ```ts
16
+ // lib/speeq.ts
17
+ import { createSpeeq, defineRoute, defineTool } from "@speeq/server";
18
+ import { createOpenAICompatibleProvider } from "@speeq/llm-openai-compatible";
19
+ import { createMongoStore } from "@speeq/store-mongodb";
20
+ import { z } from "zod";
21
+
22
+ export const speeq = createSpeeq({
23
+ secret: process.env.SPEEQ_SECRET,
24
+ agent: { name: "Helper", instructions: "You help students use this site." },
25
+ llm: createOpenAICompatibleProvider({
26
+ baseURL: process.env.GROQ_BASE_URL!,
27
+ apiKey: process.env.GROQ_API_KEY!,
28
+ model: process.env.GROQ_MODEL!,
29
+ }),
30
+ store: createMongoStore({ db: getDb() }),
31
+ resolveUser: async (req) => getUserFromYourAuth(req), // { id, roles } | null
32
+ routes: [defineRoute({ id: "practice", path: "/practice", title: "Practice" })],
33
+ tools: [
34
+ defineTool({
35
+ name: "count_questions",
36
+ description: "Count questions for a subject",
37
+ input: z.object({ subject: z.string() }),
38
+ handler: async ({ subject }, ctx) => ({ count: await countQuestions(subject, ctx.user) }),
39
+ }),
40
+ ],
41
+ audit: { authorize: async (req) => isAdmin(req) },
42
+ });
43
+ ```
44
+
45
+ ```ts
46
+ // app/api/speeq/[[...speeq]]/route.ts
47
+ import { createNextHandler } from "@speeq/server/next";
48
+ import { speeq } from "@/lib/speeq";
49
+
50
+ export const runtime = "nodejs";
51
+ export const { GET, POST, OPTIONS } = createNextHandler(speeq);
52
+ ```
53
+
54
+ `speeq.handler` is a plain `(Request) => Promise<Response>`, so it also works with any
55
+ Fetch-API server (Hono, Bun, Deno, Node with an adapter).
56
+
57
+ ## What it does on each turn
58
+
59
+ 1. Checks the signed session token and that the same user is still signed in.
60
+ 2. Stores the user message, then rebuilds history from your store (this is serverless-safe).
61
+ 3. Builds the system prompt: your instructions, the reply language, your route list, the page
62
+ context and the safety rules.
63
+ 4. Calls the model, streams `assistant.delta` events, and runs the tools it asks for. Arguments
64
+ are validated with Zod, tools run under a timeout, and output is truncated. It stops at
65
+ `maxSteps`; on the last step the model gets no tools.
66
+ 5. Write tools (`risk: "write"`) do **not** run. The stream ends with `confirmation.required`,
67
+ and the tool runs only after `POST /session/:id/confirm` with `approved: true`. Each
68
+ confirmation can be answered once, only by its own session and user.
69
+ 6. Writes every step to the audit log.
70
+
71
+ ## Built-in tools
72
+
73
+ - `navigate({ routeId, params? })`: only ids from `routes` are accepted, and params are checked
74
+ against the route's schema. The server only **emits** a `navigate` event. Your client decides
75
+ how to route.
76
+ - `search_knowledge({ query, sourceId? })`: available when `knowledge` sources are configured.
77
+
78
+ ## Security defaults
79
+
80
+ | Default | How to change it |
81
+ | --------------------------------------------------------------------------- | -------------------------------------------------------------- |
82
+ | Audit routes return 404 | Set `audit.authorize` (403 when it returns false) |
83
+ | Write tools need confirmation | `requiresConfirmation: false` (not recommended) |
84
+ | Same-origin only | `allowedOrigins: ["https://app.example"]` |
85
+ | 32 KiB body limit, 8 s tool timeout, 8000-char tool output | `guardrails` |
86
+ | 20 turns/min per user or IP and per session | `guardrails.rateLimit` (**best effort**: counted per instance) |
87
+ | Secret keys in tool args (`password`, `token`, …) redacted in the audit log | add `audit.redact` for more |
88
+
89
+ Raw error messages and stack traces never reach the client or the model. Tools can throw
90
+ `new SpeeqError("FORBIDDEN", { message })` to give the user a safe reason.
91
+
92
+ Redaction note: stored events are also the history the model sees on later turns, and a write
93
+ tool is re-validated from its stored arguments when it is confirmed. Don't put secrets in
94
+ write-tool arguments.
95
+
96
+ ## Configuration
97
+
98
+ ### Telemetry
99
+
100
+ Configure a platform endpoint or a custom transport to send anonymous adoption counters. Set `SPEEQ_TELEMETRY=0` or `telemetry.enabled: false` to disable collection. See the [telemetry reference](../../docs/reference/telemetry.md) for the payload and delivery rules.
101
+
102
+ See [`docs/reference/server-config.md`](../../docs/reference/server-config.md) for every option and
103
+ [`docs/reference/protocol.md`](../../docs/reference/protocol.md) for the wire protocol.