@ai-sdk-letta/server 0.1.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/CHANGELOG.md +12 -0
- package/LICENSE +202 -0
- package/NOTICE +9 -0
- package/README.md +37 -0
- package/dist/http.d.ts +33 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +114 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime.d.ts +154 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +438 -0
- package/dist/runtime.js.map +1 -0
- package/dist/serve.d.ts +40 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +128 -0
- package/dist/serve.js.map +1 -0
- package/package.json +68 -0
- package/src/http.ts +108 -0
- package/src/index.ts +9 -0
- package/src/runtime.ts +341 -0
- package/src/serve.ts +128 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# @ai-sdk-letta/server
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- cbca4fb: First public release. A local HTTP runtime for ai-sdk-letta agents: durable threads and runs, NDJSON events, exactly-once answers and cancellation, and image input on `POST /v1/runs`; `startGuiServer` for a loopback browser app (session cookie, CSRF, Origin and Host checks, strict CSP) and `startApiServer` for a token-authenticated server-to-server API.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [cbca4fb]
|
|
12
|
+
- ai-sdk-letta@0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
@ai-sdk-letta/server
|
|
2
|
+
Copyright 2026 Leonard Schneider
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
It is part of ai-sdk-letta (https://github.com/leonardschneider/ai-sdk-letta).
|
|
6
|
+
|
|
7
|
+
ai-sdk-letta is an independent project. It is not affiliated with, endorsed
|
|
8
|
+
by, or sponsored by Letta or Vercel. "Letta" and "Vercel" are trademarks of
|
|
9
|
+
their respective owners.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @ai-sdk-letta/server
|
|
2
|
+
|
|
3
|
+
Local HTTP runtime for [ai-sdk-letta](https://github.com/leonardschneider/ai-sdk-letta/tree/main/packages/ai-sdk-letta) agents. Unofficial; not
|
|
4
|
+
affiliated with Letta or Vercel.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @ai-sdk-letta/server ai-sdk-letta ai
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
- `ThreadRuntime`: durable threads and runs over one agent, NDJSON events,
|
|
11
|
+
exactly-once answers, cancellation, rename and archive. A run interrupted by
|
|
12
|
+
a restart blocks its thread instead of being replayed.
|
|
13
|
+
- `startGuiServer(definition, assetsDir, options)`: the loopback browser app
|
|
14
|
+
(session cookie, CSRF, Origin and Host checks, strict CSP).
|
|
15
|
+
- `startApiServer(definition, options)`: the same routes behind a 256-bit
|
|
16
|
+
bearer token and owner header, for server-to-server use.
|
|
17
|
+
|
|
18
|
+
Routes (under `/api` for the GUI): `GET /v1/capabilities`, `GET|POST /v1/threads`,
|
|
19
|
+
`PATCH /v1/threads/:id`, `GET /v1/threads/:id/history`, `GET /v1/threads/:id/view`,
|
|
20
|
+
`POST /v1/runs`, `GET /v1/runs/:id/events?after=N` (NDJSON), `POST /v1/runs/:id/answer`,
|
|
21
|
+
`POST /v1/runs/:id/cancel`.
|
|
22
|
+
|
|
23
|
+
`POST /v1/runs` takes `{ id, threadId, text, parentRunId, images? }`, where
|
|
24
|
+
`images` is a list of `{ mediaType, data }` (base64, no `data:` prefix).
|
|
25
|
+
Text may be empty when images are present. Images are validated against
|
|
26
|
+
`IMAGE_LIMITS` and rejected with a fixed code (`image_unsupported_type`,
|
|
27
|
+
`image_invalid`, `image_remote_url`: 400; `image_too_large`,
|
|
28
|
+
`images_too_many` (400), `images_too_large`: 413). Only this route accepts a
|
|
29
|
+
larger body (`RUN_BODY_LIMIT_BYTES`); others keep `BODY_LIMIT_BYTES` (24 KB)
|
|
30
|
+
and answer `payload_too_large` (413) beyond it. Runtime state stores each
|
|
31
|
+
image's type, size and SHA-256, never its bytes.
|
|
32
|
+
|
|
33
|
+
`startGuiServer` serves a built browser app from the directory you pass. The
|
|
34
|
+
assistant-ui app in this repository (`apps/web`) is not on npm yet; build it
|
|
35
|
+
from a checkout.
|
|
36
|
+
|
|
37
|
+
License: Apache-2.0.
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import type { ThreadRuntime } from './runtime.js';
|
|
3
|
+
/**
|
|
4
|
+
* Server-to-server API: bearer token (256-bit hex) plus an owner header, on
|
|
5
|
+
* 127.0.0.1 only. Browser origins and cross-site requests are never accepted.
|
|
6
|
+
*/
|
|
7
|
+
export declare function tokenApiApp(runtime: ThreadRuntime, token: string, owner: string, port: number, shutdown?: () => Promise<void>): express.Express;
|
|
8
|
+
/** JSON body limit for every route except `POST /v1/runs`. */
|
|
9
|
+
export declare const BODY_LIMIT_BYTES: number;
|
|
10
|
+
/**
|
|
11
|
+
* JSON body limit for `POST /v1/runs` only: base64 of the largest allowed
|
|
12
|
+
* image payload plus room for text and JSON. Still bounded; image limits are
|
|
13
|
+
* enforced again on the decoded bytes.
|
|
14
|
+
*/
|
|
15
|
+
export declare const RUN_BODY_LIMIT_BYTES: number;
|
|
16
|
+
/** Shared routes; the caller must install its transport-specific authentication first. */
|
|
17
|
+
export declare function runtimeRoutes(app: express.Express, runtime: ThreadRuntime, owner: string, shutdown?: () => Promise<void>): express.Express;
|
|
18
|
+
/** What the browser may know about the agent. */
|
|
19
|
+
export interface GuiAgentInfo {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
/** Tools whose calls always ask for approval (used for display only). */
|
|
23
|
+
approvalTools?: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Loopback-only, same-origin browser transport. No bearer token enters the
|
|
27
|
+
* browser: a random HttpOnly SameSite=Strict session cookie authenticates
|
|
28
|
+
* reads, and every mutation also needs the exact Origin and an in-memory CSRF
|
|
29
|
+
* token. Requests with a foreign Host, Origin or cross-site fetch metadata are
|
|
30
|
+
* rejected. Static assets are served with a strict CSP.
|
|
31
|
+
*/
|
|
32
|
+
export declare function guiApp(runtime: ThreadRuntime, owner: string, port: number, assets: string, agent: GuiAgentInfo): import("express-serve-static-core").Express;
|
|
33
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B,OAAO,KAAK,EAAgB,aAAa,EAAE,MAAM,cAAc,CAAC;AAGhE;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,mBAW7H;AAED,8DAA8D;AAC9D,eAAO,MAAM,gBAAgB,QAAY,CAAC;AAC1C;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,QAA4D,CAAC;AAE9F,0FAA0F;AAC1F,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,mBAiCxH;AAED,iDAAiD;AACjD,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,yEAAyE;IACzE,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,+CAwB9G"}
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
3
|
+
import { IMAGE_LIMITS, IMAGE_MEDIA_TYPES } from 'ai-sdk-letta';
|
|
4
|
+
import { RuntimeFault } from './runtime.js';
|
|
5
|
+
/**
|
|
6
|
+
* Server-to-server API: bearer token (256-bit hex) plus an owner header, on
|
|
7
|
+
* 127.0.0.1 only. Browser origins and cross-site requests are never accepted.
|
|
8
|
+
*/
|
|
9
|
+
export function tokenApiApp(runtime, token, owner, port, shutdown) {
|
|
10
|
+
if (!/^[a-f0-9]{64}$/.test(token))
|
|
11
|
+
throw new Error('A private 256-bit token is required');
|
|
12
|
+
const app = express();
|
|
13
|
+
app.disable('x-powered-by');
|
|
14
|
+
app.use((req, res, next) => {
|
|
15
|
+
res.set({ 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' });
|
|
16
|
+
const supplied = req.headers.authorization?.replace(/^Bearer /, '');
|
|
17
|
+
if (req.headers.host !== `127.0.0.1:${port || req.socket.localPort}` || req.headers.origin || req.headers['sec-fetch-site'] === 'cross-site' || !supplied || !/^[a-f0-9]{64}$/.test(supplied) || !timingSafeEqual(Buffer.from(supplied), Buffer.from(token)))
|
|
18
|
+
return res.status(401).json({ error: 'unauthorized' });
|
|
19
|
+
if (req.headers['x-runtime-owner'] !== owner)
|
|
20
|
+
return res.status(403).json({ error: 'forbidden' });
|
|
21
|
+
next();
|
|
22
|
+
});
|
|
23
|
+
return runtimeRoutes(app, runtime, owner, shutdown);
|
|
24
|
+
}
|
|
25
|
+
/** JSON body limit for every route except `POST /v1/runs`. */
|
|
26
|
+
export const BODY_LIMIT_BYTES = 24 * 1024;
|
|
27
|
+
/**
|
|
28
|
+
* JSON body limit for `POST /v1/runs` only: base64 of the largest allowed
|
|
29
|
+
* image payload plus room for text and JSON. Still bounded; image limits are
|
|
30
|
+
* enforced again on the decoded bytes.
|
|
31
|
+
*/
|
|
32
|
+
export const RUN_BODY_LIMIT_BYTES = Math.ceil(IMAGE_LIMITS.maxTotalBytes / 3) * 4 + 64 * 1024;
|
|
33
|
+
/** Shared routes; the caller must install its transport-specific authentication first. */
|
|
34
|
+
export function runtimeRoutes(app, runtime, owner, shutdown) {
|
|
35
|
+
const small = express.json({ limit: BODY_LIMIT_BYTES });
|
|
36
|
+
const runs = express.json({ limit: RUN_BODY_LIMIT_BYTES });
|
|
37
|
+
app.use((req, res, next) => (req.method === 'POST' && req.path === '/v1/runs' ? runs : small)(req, res, next));
|
|
38
|
+
app.get('/v1/capabilities', (_req, res) => res.json({ version: 1, stateful: true, tools: 'observed-only', interactions: ['approval', 'question'], history: true, replay: true, concurrency: 1, edits: false,
|
|
39
|
+
images: { mediaTypes: [...IMAGE_MEDIA_TYPES], maxImageBytes: IMAGE_LIMITS.maxImageBytes, maxImages: IMAGE_LIMITS.maxImages, maxTotalBytes: IMAGE_LIMITS.maxTotalBytes } }));
|
|
40
|
+
app.get('/v1/threads', (_req, res) => res.json(runtime.list(owner)));
|
|
41
|
+
app.post('/v1/threads', async (req, res) => res.status(201).json(await runtime.create(owner, req.body?.id, req.body?.title)));
|
|
42
|
+
app.patch('/v1/threads/:id', (req, res) => res.json(runtime.updateMetadata(owner, req.params.id, req.body)));
|
|
43
|
+
app.get('/v1/threads/:id/history', async (req, res) => res.json(await runtime.history(owner, req.params.id)));
|
|
44
|
+
app.get('/v1/threads/:id/view', async (req, res) => res.json(await runtime.view(owner, req.params.id)));
|
|
45
|
+
app.post('/v1/runs', async (req, res) => res.status(202).json(await runtime.start(owner, req.body)));
|
|
46
|
+
app.post('/v1/runs/:id/answer', (req, res) => { runtime.answer(owner, req.params.id, req.body); res.json({ accepted: true }); });
|
|
47
|
+
app.post('/v1/runs/:id/cancel', (req, res) => { runtime.cancel(owner, req.params.id); res.json({ accepted: true }); });
|
|
48
|
+
app.get('/v1/runs/:id/events', (req, res) => {
|
|
49
|
+
const snapshot = runtime.events(owner, req.params.id, Number(req.query.after ?? 0));
|
|
50
|
+
res.set({ 'Content-Type': 'application/x-ndjson', 'X-Accel-Buffering': 'no' });
|
|
51
|
+
res.flushHeaders();
|
|
52
|
+
const write = (event) => { if (!res.destroyed)
|
|
53
|
+
res.write(`${JSON.stringify(event)}\n`); if (event.type === 'completed' || event.type === 'failed')
|
|
54
|
+
res.end(); };
|
|
55
|
+
for (const event of snapshot.events)
|
|
56
|
+
write(event);
|
|
57
|
+
if (snapshot.status !== 'running') {
|
|
58
|
+
res.end();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const unsubscribe = runtime.subscribe(req.params.id, write);
|
|
62
|
+
const heartbeat = setInterval(() => { if (!res.destroyed)
|
|
63
|
+
res.write('\n'); }, 15_000);
|
|
64
|
+
res.on('close', () => { clearInterval(heartbeat); unsubscribe(); });
|
|
65
|
+
});
|
|
66
|
+
if (shutdown)
|
|
67
|
+
app.post('/v1/shutdown', async (_req, res) => { await shutdown(); res.json({ stopped: true }); });
|
|
68
|
+
app.use((_req, res) => res.status(404).json({ error: 'not_found' }));
|
|
69
|
+
app.use((error, _req, res, _next) => {
|
|
70
|
+
if (res.headersSent)
|
|
71
|
+
return res.end();
|
|
72
|
+
// Body parser rejections: fixed codes, never parser messages.
|
|
73
|
+
if (error?.type === 'entity.too.large')
|
|
74
|
+
return res.status(413).json({ error: 'payload_too_large' });
|
|
75
|
+
res.status(error instanceof RuntimeFault ? error.status : error instanceof SyntaxError ? 400 : 503).json({ error: error instanceof RuntimeFault ? error.code : 'runtime_unavailable' });
|
|
76
|
+
});
|
|
77
|
+
return app;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Loopback-only, same-origin browser transport. No bearer token enters the
|
|
81
|
+
* browser: a random HttpOnly SameSite=Strict session cookie authenticates
|
|
82
|
+
* reads, and every mutation also needs the exact Origin and an in-memory CSRF
|
|
83
|
+
* token. Requests with a foreign Host, Origin or cross-site fetch metadata are
|
|
84
|
+
* rejected. Static assets are served with a strict CSP.
|
|
85
|
+
*/
|
|
86
|
+
export function guiApp(runtime, owner, port, assets, agent) {
|
|
87
|
+
const app = express();
|
|
88
|
+
const session = randomBytes(32).toString('hex');
|
|
89
|
+
const csrf = randomBytes(32).toString('hex');
|
|
90
|
+
app.disable('x-powered-by');
|
|
91
|
+
app.use((req, res, next) => {
|
|
92
|
+
const host = `127.0.0.1:${port || req.socket.localPort}`;
|
|
93
|
+
res.set({ 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'no-referrer',
|
|
94
|
+
'Content-Security-Policy': "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'" });
|
|
95
|
+
if (req.headers.host !== host || (req.headers.origin && req.headers.origin !== `http://${host}`) || (req.headers['sec-fetch-site'] && !['same-origin', 'none'].includes(String(req.headers['sec-fetch-site']))))
|
|
96
|
+
return res.status(403).json({ error: 'invalid_origin' });
|
|
97
|
+
next();
|
|
98
|
+
});
|
|
99
|
+
app.get('/api/session', (_req, res) => {
|
|
100
|
+
res.cookie('ai_sdk_letta_session', session, { httpOnly: true, sameSite: 'strict', path: '/' });
|
|
101
|
+
res.json({ csrf, agent: { id: agent.id, name: agent.name, approvalTools: [...(agent.approvalTools ?? [])] } });
|
|
102
|
+
});
|
|
103
|
+
app.use('/api', (req, res, next) => {
|
|
104
|
+
const cookie = req.headers.cookie?.split(';').map(s => s.trim()).find(s => s.startsWith('ai_sdk_letta_session='))?.slice('ai_sdk_letta_session='.length);
|
|
105
|
+
if (!cookie || !/^[a-f0-9]{64}$/.test(cookie) || !timingSafeEqual(Buffer.from(cookie), Buffer.from(session)))
|
|
106
|
+
return res.status(401).json({ error: 'session_required' });
|
|
107
|
+
if (!['GET', 'HEAD'].includes(req.method) && (req.headers.origin !== `http://127.0.0.1:${port || req.socket.localPort}` || req.headers['x-csrf-token'] !== csrf))
|
|
108
|
+
return res.status(403).json({ error: 'csrf_required' });
|
|
109
|
+
next();
|
|
110
|
+
}, runtimeRoutes(express(), runtime, owner));
|
|
111
|
+
app.use(express.static(assets, { index: 'index.html', dotfiles: 'deny' }));
|
|
112
|
+
return app;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=http.js.map
|
package/dist/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAE/D,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,OAAsB,EAAE,KAAa,EAAE,KAAa,EAAE,IAAY,EAAE,QAA8B;IAC5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AC;IAC3E,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local HTTP runtime for ai-sdk-letta agents: a durable thread/run service,
|
|
3
|
+
* a token-authenticated server-to-server API, and a loopback browser app.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
export { ThreadRuntime, RuntimeFault, displayRun, toolFailureReason, type RuntimeEvent, type Run, type RunImage, type RunInput, type RuntimeHost, type RuntimeSession } from './runtime.js';
|
|
8
|
+
export { guiApp, tokenApiApp, runtimeRoutes, BODY_LIMIT_BYTES, RUN_BODY_LIMIT_BYTES, type GuiAgentInfo } from './http.js';
|
|
9
|
+
export { startGuiServer, startApiServer, closeOnSignals, DEFAULT_PORT, type ServeOptions, type RunningServer } from './serve.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,KAAK,YAAY,EAAE,KAAK,GAAG,EAAE,KAAK,QAAQ,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAC5L,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAC1H,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local HTTP runtime for ai-sdk-letta agents: a durable thread/run service,
|
|
3
|
+
* a token-authenticated server-to-server API, and a loopback browser app.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
export { ThreadRuntime, RuntimeFault, displayRun, toolFailureReason } from './runtime.js';
|
|
8
|
+
export { guiApp, tokenApiApp, runtimeRoutes, BODY_LIMIT_BYTES, RUN_BODY_LIMIT_BYTES } from './http.js';
|
|
9
|
+
export { startGuiServer, startApiServer, closeOnSignals, DEFAULT_PORT } from './serve.js';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAoG,MAAM,cAAc,CAAC;AAC5L,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAE,oBAAoB,EAAqB,MAAM,WAAW,CAAC;AAC1H,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,YAAY,EAAyC,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { UIMessage } from 'ai';
|
|
2
|
+
import { type InteractionResponse, type LettaAgent } from 'ai-sdk-letta';
|
|
3
|
+
/** One NDJSON event of a run. The application, never the HTTP consumer, owns tools. */
|
|
4
|
+
export type RuntimeEvent = {
|
|
5
|
+
sequence: number;
|
|
6
|
+
type: string;
|
|
7
|
+
data: Record<string, unknown>;
|
|
8
|
+
};
|
|
9
|
+
/** Metadata of an image sent with a run. The bytes live only in Letta history, never in runtime state. */
|
|
10
|
+
export type RunImage = {
|
|
11
|
+
mediaType: string;
|
|
12
|
+
bytes: number;
|
|
13
|
+
sha256: string;
|
|
14
|
+
};
|
|
15
|
+
/** A single user turn and the events observed while it ran. */
|
|
16
|
+
export type Run = {
|
|
17
|
+
id: string;
|
|
18
|
+
threadId: string;
|
|
19
|
+
input: string;
|
|
20
|
+
images?: RunImage[];
|
|
21
|
+
parentRunId: string | null;
|
|
22
|
+
status: 'running' | 'completed' | 'failed' | 'cancelled' | 'interrupted';
|
|
23
|
+
events: RuntimeEvent[];
|
|
24
|
+
startedAt?: string;
|
|
25
|
+
};
|
|
26
|
+
/** Input of `POST /v1/runs`. `images` carry base64 data (no data: prefix) and are validated against `IMAGE_LIMITS`. */
|
|
27
|
+
export type RunInput = {
|
|
28
|
+
id: string;
|
|
29
|
+
threadId: string;
|
|
30
|
+
text: string;
|
|
31
|
+
parentRunId: string | null;
|
|
32
|
+
images?: {
|
|
33
|
+
mediaType: string;
|
|
34
|
+
data: string;
|
|
35
|
+
}[];
|
|
36
|
+
};
|
|
37
|
+
/** An opened agent conversation as seen by the runtime. */
|
|
38
|
+
export interface RuntimeSession {
|
|
39
|
+
agentId: string;
|
|
40
|
+
conversationId: string;
|
|
41
|
+
history: UIMessage[];
|
|
42
|
+
agent: Pick<LettaAgent<any>, 'stream' | 'interactions' | 'transcript'>;
|
|
43
|
+
}
|
|
44
|
+
/** Opens and closes the single agent session the runtime drives. */
|
|
45
|
+
export interface RuntimeHost {
|
|
46
|
+
open(options: {
|
|
47
|
+
conversationId: string;
|
|
48
|
+
} | {
|
|
49
|
+
newTitle: string;
|
|
50
|
+
}): Promise<RuntimeSession>;
|
|
51
|
+
close(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
/** A client-visible failure with a fixed code and HTTP status. */
|
|
54
|
+
export declare class RuntimeFault extends Error {
|
|
55
|
+
readonly code: string;
|
|
56
|
+
readonly status: number;
|
|
57
|
+
constructor(code: string, status?: number);
|
|
58
|
+
}
|
|
59
|
+
export declare function toolFailureReason(error: unknown): {
|
|
60
|
+
reason?: string;
|
|
61
|
+
};
|
|
62
|
+
/** Transport observations for reconnects/failed-turn inspection only, never agent input. */
|
|
63
|
+
export declare function displayRun(run: Run): UIMessage[];
|
|
64
|
+
/**
|
|
65
|
+
* Single-owner, single-turn thread service over one agent.
|
|
66
|
+
*
|
|
67
|
+
* Runs are recorded durably before delivery; a run that was in flight when the
|
|
68
|
+
* process stopped is marked `interrupted` and blocks further turns on that
|
|
69
|
+
* thread (uncertain delivery is never replayed). Human answers are validated
|
|
70
|
+
* and accepted exactly once.
|
|
71
|
+
*/
|
|
72
|
+
export declare class ThreadRuntime {
|
|
73
|
+
private readonly host;
|
|
74
|
+
private readonly filename;
|
|
75
|
+
private readonly owner;
|
|
76
|
+
private readonly deadlineMs;
|
|
77
|
+
private readonly humanWaitMs;
|
|
78
|
+
private state;
|
|
79
|
+
private locked;
|
|
80
|
+
private current?;
|
|
81
|
+
private active?;
|
|
82
|
+
private pending?;
|
|
83
|
+
private listeners;
|
|
84
|
+
constructor(host: RuntimeHost, filename: string, owner: string, deadlineMs?: number, humanWaitMs?: number);
|
|
85
|
+
private save;
|
|
86
|
+
private authorize;
|
|
87
|
+
private thread;
|
|
88
|
+
private run;
|
|
89
|
+
private exclusive;
|
|
90
|
+
private open;
|
|
91
|
+
/** Display metadata only; timestamps are omitted for legacy threads that never recorded them. */
|
|
92
|
+
private summary;
|
|
93
|
+
list(owner: string): {
|
|
94
|
+
lastActivityAt?: string | undefined;
|
|
95
|
+
createdAt?: string | undefined;
|
|
96
|
+
id: string;
|
|
97
|
+
title: string;
|
|
98
|
+
state: "creating" | "ready";
|
|
99
|
+
archived: boolean;
|
|
100
|
+
}[];
|
|
101
|
+
/** Local display metadata only: never opens a session or edits backend history. */
|
|
102
|
+
updateMetadata(owner: string, id: string, input: unknown): {
|
|
103
|
+
lastActivityAt?: string | undefined;
|
|
104
|
+
createdAt?: string | undefined;
|
|
105
|
+
id: string;
|
|
106
|
+
title: string;
|
|
107
|
+
state: "creating" | "ready";
|
|
108
|
+
archived: boolean;
|
|
109
|
+
};
|
|
110
|
+
create(owner: string, id: string, title: string): Promise<{
|
|
111
|
+
id: string;
|
|
112
|
+
title: string;
|
|
113
|
+
}>;
|
|
114
|
+
history(owner: string, id: string): Promise<{
|
|
115
|
+
messages: UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[];
|
|
116
|
+
lastRunId: string | null;
|
|
117
|
+
}>;
|
|
118
|
+
view(owner: string, id: string): Promise<{
|
|
119
|
+
messages: UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[];
|
|
120
|
+
lastRunId: string | null;
|
|
121
|
+
live: {
|
|
122
|
+
images?: number | undefined;
|
|
123
|
+
id: string;
|
|
124
|
+
input: string;
|
|
125
|
+
};
|
|
126
|
+
status: string;
|
|
127
|
+
} | {
|
|
128
|
+
live: null;
|
|
129
|
+
status: "running" | "completed" | "failed" | "cancelled" | "interrupted" | null;
|
|
130
|
+
source: string;
|
|
131
|
+
messages: UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[];
|
|
132
|
+
lastRunId: string | null;
|
|
133
|
+
}>;
|
|
134
|
+
private emit;
|
|
135
|
+
/**
|
|
136
|
+
* Start one turn. Text, images or both; images are validated (type by
|
|
137
|
+
* content, size, count, total) and fail with a fixed `image_*` code.
|
|
138
|
+
* Only image metadata (type, size, SHA-256) is recorded in runtime state.
|
|
139
|
+
*/
|
|
140
|
+
start(owner: string, input: RunInput): Promise<{
|
|
141
|
+
id: string;
|
|
142
|
+
status: "running" | "completed" | "failed" | "cancelled" | "interrupted";
|
|
143
|
+
}>;
|
|
144
|
+
private drive;
|
|
145
|
+
events(owner: string, id: string, after: number): {
|
|
146
|
+
events: RuntimeEvent[];
|
|
147
|
+
status: "running" | "completed" | "failed" | "cancelled" | "interrupted";
|
|
148
|
+
};
|
|
149
|
+
subscribe(id: string, listener: (event: RuntimeEvent) => void): () => void;
|
|
150
|
+
answer(owner: string, runId: string, response: InteractionResponse): void;
|
|
151
|
+
cancel(owner: string, id: string): void;
|
|
152
|
+
close(): Promise<void>;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 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