@tinyweb_dev/agent-framework-core 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/LICENSE +21 -0
- package/README.md +24 -0
- package/package.json +33 -0
- package/src/error.ts +12 -0
- package/src/global.ts +27 -0
- package/src/id.ts +28 -0
- package/src/index.ts +7 -0
- package/src/log.ts +129 -0
- package/src/schema.ts +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tinyweb-agent-framework 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
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @tinyweb-agent-framework/core
|
|
2
|
+
|
|
3
|
+
Shared primitives for the tinyweb-agent framework. **You usually don't import this directly** — the higher-level packages re-export what you need.
|
|
4
|
+
|
|
5
|
+
## What's inside
|
|
6
|
+
|
|
7
|
+
- **`Log`** — small stderr-only logger with service tags, level filtering via `TINYWEB_LOG_LEVEL`, and `using` blocks via `Log.time(...)`.
|
|
8
|
+
- **`Id`** — ULID-based ID helpers: `Id.fresh()`, `Id.prefixed("ses")`, `Id.sessionId()`, `Id.messageId()`, `Id.partId()`, `Id.toolCallId()`.
|
|
9
|
+
- **`Global.Path`** — `~/.tinyweb-agent/{data,cache,state,log}` paths + `Global.init()` to mkdir them.
|
|
10
|
+
- **`TinywebError.NamedError(tag)`** — thin wrapper over `Data.TaggedError`.
|
|
11
|
+
- **Schema helpers** — `withStatics`, `Newtype`, `PositiveInt`, `NonNegativeInt`.
|
|
12
|
+
|
|
13
|
+
## Example
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Log, Id } from "@tinyweb-agent-framework/core"
|
|
17
|
+
|
|
18
|
+
const log = Log.create({ service: "my-agent" })
|
|
19
|
+
log.info("starting", { sessionId: Id.sessionId() })
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@tinyweb_dev/agent-framework-core",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"test": "bun test --timeout 30000"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./src/index.ts",
|
|
18
|
+
"./schema": "./src/schema.ts",
|
|
19
|
+
"./log": "./src/log.ts",
|
|
20
|
+
"./error": "./src/error.ts",
|
|
21
|
+
"./id": "./src/id.ts",
|
|
22
|
+
"./global": "./src/global.ts"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@tsconfig/bun": "1.0.9",
|
|
26
|
+
"@types/bun": "1.3.12",
|
|
27
|
+
"typescript": "5.8.2"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"effect": "4.0.0-beta.59",
|
|
31
|
+
"ulid": "3.0.1"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * as TinywebError from "./error"
|
|
2
|
+
|
|
3
|
+
import { Data } from "effect"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create a tagged error class for use inside `Effect.gen`:
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* class FileMissing extends NamedError("FileMissing")<{ readonly path: string }> {}
|
|
10
|
+
* yield* new FileMissing({ path: "/x" })
|
|
11
|
+
*/
|
|
12
|
+
export const NamedError = <Tag extends string>(tag: Tag) => Data.TaggedError(tag)
|
package/src/global.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export * as Global from "./global"
|
|
2
|
+
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import os from "node:os"
|
|
5
|
+
import fs from "node:fs/promises"
|
|
6
|
+
|
|
7
|
+
const app = "tinyweb-agent"
|
|
8
|
+
const home = process.env.TINYWEB_HOME ?? path.join(os.homedir(), ".tinyweb-agent")
|
|
9
|
+
|
|
10
|
+
export const Path = {
|
|
11
|
+
home,
|
|
12
|
+
data: path.join(home, "data"),
|
|
13
|
+
cache: path.join(home, "cache"),
|
|
14
|
+
state: path.join(home, "state"),
|
|
15
|
+
log: path.join(home, "log"),
|
|
16
|
+
tmp: path.join(os.tmpdir(), app),
|
|
17
|
+
} as const
|
|
18
|
+
|
|
19
|
+
export async function init() {
|
|
20
|
+
await Promise.all([
|
|
21
|
+
fs.mkdir(Path.data, { recursive: true }),
|
|
22
|
+
fs.mkdir(Path.cache, { recursive: true }),
|
|
23
|
+
fs.mkdir(Path.state, { recursive: true }),
|
|
24
|
+
fs.mkdir(Path.log, { recursive: true }),
|
|
25
|
+
fs.mkdir(Path.tmp, { recursive: true }),
|
|
26
|
+
])
|
|
27
|
+
}
|
package/src/id.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export * as Id from "./id"
|
|
2
|
+
|
|
3
|
+
import { ulid } from "ulid"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate a fresh ULID — a 26-char Crockford-base32 string, monotonic per ms.
|
|
7
|
+
*
|
|
8
|
+
* Used for IDs that should remain unbranded at the wire level (session ids,
|
|
9
|
+
* message ids, etc). Bound to a `Schema.brand(...)` at the callsite.
|
|
10
|
+
*/
|
|
11
|
+
export function fresh(): string {
|
|
12
|
+
return ulid()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generate a ULID with a fixed tag prefix, e.g. `prefixed("ses")` → `"ses_01H..."`.
|
|
17
|
+
*
|
|
18
|
+
* Mirrors OpenCode's `id.ts` helpers. Prefix isolates the namespace so wrong
|
|
19
|
+
* ids fail loudly without needing to inspect schema metadata.
|
|
20
|
+
*/
|
|
21
|
+
export function prefixed(prefix: string): string {
|
|
22
|
+
return `${prefix}_${ulid()}`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const sessionId = () => prefixed("ses")
|
|
26
|
+
export const messageId = () => prefixed("msg")
|
|
27
|
+
export const partId = () => prefixed("prt")
|
|
28
|
+
export const toolCallId = () => prefixed("tc")
|
package/src/index.ts
ADDED
package/src/log.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
export * as Log from "./log"
|
|
2
|
+
|
|
3
|
+
export type Level = "DEBUG" | "INFO" | "WARN" | "ERROR"
|
|
4
|
+
|
|
5
|
+
const levelPriority: Record<Level, number> = {
|
|
6
|
+
DEBUG: 0,
|
|
7
|
+
INFO: 1,
|
|
8
|
+
WARN: 2,
|
|
9
|
+
ERROR: 3,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let level: Level = (process.env.TINYWEB_LOG_LEVEL as Level) ?? "INFO"
|
|
13
|
+
|
|
14
|
+
function shouldLog(input: Level): boolean {
|
|
15
|
+
return levelPriority[input] >= levelPriority[level]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type Logger = {
|
|
19
|
+
debug(message?: unknown, extra?: Record<string, unknown>): void
|
|
20
|
+
info(message?: unknown, extra?: Record<string, unknown>): void
|
|
21
|
+
error(message?: unknown, extra?: Record<string, unknown>): void
|
|
22
|
+
warn(message?: unknown, extra?: Record<string, unknown>): void
|
|
23
|
+
tag(key: string, value: string): Logger
|
|
24
|
+
clone(): Logger
|
|
25
|
+
time(
|
|
26
|
+
message: string,
|
|
27
|
+
extra?: Record<string, unknown>,
|
|
28
|
+
): {
|
|
29
|
+
stop(): void
|
|
30
|
+
[Symbol.dispose](): void
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const loggers = new Map<string, Logger>()
|
|
35
|
+
|
|
36
|
+
export interface Options {
|
|
37
|
+
level?: Level
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const write = (msg: string) => {
|
|
41
|
+
process.stderr.write(msg)
|
|
42
|
+
return msg.length
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function setLevel(next: Level) {
|
|
46
|
+
level = next
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function init(options: Options) {
|
|
50
|
+
if (options.level) level = options.level
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function formatError(error: Error, depth = 0): string {
|
|
54
|
+
const result = error.message
|
|
55
|
+
return error.cause instanceof Error && depth < 10
|
|
56
|
+
? result + " Caused by: " + formatError(error.cause, depth + 1)
|
|
57
|
+
: result
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let last = Date.now()
|
|
61
|
+
|
|
62
|
+
export function create(tags?: Record<string, unknown>) {
|
|
63
|
+
const ownTags = tags ?? {}
|
|
64
|
+
|
|
65
|
+
const service = ownTags["service"]
|
|
66
|
+
if (typeof service === "string") {
|
|
67
|
+
const cached = loggers.get(service)
|
|
68
|
+
if (cached) return cached
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function build(message: unknown, extra?: Record<string, unknown>) {
|
|
72
|
+
const prefix = Object.entries({ ...ownTags, ...extra })
|
|
73
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
74
|
+
.map(([key, value]) => {
|
|
75
|
+
const head = `${key}=`
|
|
76
|
+
if (value instanceof Error) return head + formatError(value)
|
|
77
|
+
if (typeof value === "object") return head + JSON.stringify(value)
|
|
78
|
+
return head + String(value)
|
|
79
|
+
})
|
|
80
|
+
.join(" ")
|
|
81
|
+
const next = new Date()
|
|
82
|
+
const diff = next.getTime() - last
|
|
83
|
+
last = next.getTime()
|
|
84
|
+
return [next.toISOString().split(".")[0], "+" + diff + "ms", prefix, message].filter(Boolean).join(" ") + "\n"
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const result: Logger = {
|
|
88
|
+
debug(message, extra) {
|
|
89
|
+
if (shouldLog("DEBUG")) write("DEBUG " + build(message, extra))
|
|
90
|
+
},
|
|
91
|
+
info(message, extra) {
|
|
92
|
+
if (shouldLog("INFO")) write("INFO " + build(message, extra))
|
|
93
|
+
},
|
|
94
|
+
error(message, extra) {
|
|
95
|
+
if (shouldLog("ERROR")) write("ERROR " + build(message, extra))
|
|
96
|
+
},
|
|
97
|
+
warn(message, extra) {
|
|
98
|
+
if (shouldLog("WARN")) write("WARN " + build(message, extra))
|
|
99
|
+
},
|
|
100
|
+
tag(key, value) {
|
|
101
|
+
ownTags[key] = value
|
|
102
|
+
return result
|
|
103
|
+
},
|
|
104
|
+
clone() {
|
|
105
|
+
return create({ ...ownTags })
|
|
106
|
+
},
|
|
107
|
+
time(message, extra) {
|
|
108
|
+
const now = Date.now()
|
|
109
|
+
result.info(message, { status: "started", ...extra })
|
|
110
|
+
const stop = () =>
|
|
111
|
+
result.info(message, {
|
|
112
|
+
status: "completed",
|
|
113
|
+
duration: Date.now() - now,
|
|
114
|
+
...extra,
|
|
115
|
+
})
|
|
116
|
+
return {
|
|
117
|
+
stop,
|
|
118
|
+
[Symbol.dispose]() {
|
|
119
|
+
stop()
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (typeof service === "string") loggers.set(service, result)
|
|
126
|
+
return result
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export const Default = create({ service: "default" })
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Schema } from "effect"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Integer greater than zero.
|
|
5
|
+
*/
|
|
6
|
+
export const PositiveInt = Schema.Int.check(Schema.isGreaterThan(0))
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Integer greater than or equal to zero.
|
|
10
|
+
*/
|
|
11
|
+
export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Attach static methods to a schema object. Designed to be used with `.pipe()`:
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* export const Foo = fooSchema.pipe(
|
|
18
|
+
* withStatics((schema) => ({
|
|
19
|
+
* zero: schema.make(0),
|
|
20
|
+
* from: Schema.decodeUnknownOption(schema),
|
|
21
|
+
* }))
|
|
22
|
+
* )
|
|
23
|
+
*/
|
|
24
|
+
export const withStatics =
|
|
25
|
+
<S extends object, M extends Record<string, unknown>>(methods: (schema: S) => M) =>
|
|
26
|
+
(schema: S): S & M =>
|
|
27
|
+
Object.assign(schema, methods(schema))
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Nominal wrapper for scalar types. The class itself is a valid schema —
|
|
31
|
+
* pass it directly to `Schema.decode`, `Schema.decodeEffect`, etc.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* class SessionID extends Newtype<SessionID>()("SessionID", Schema.String) {
|
|
35
|
+
* static fresh(): SessionID {
|
|
36
|
+
* return this.make(ulid())
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
export function Newtype<Self>() {
|
|
41
|
+
return <const Tag extends string, S extends Schema.Top>(tag: Tag, schema: S) => {
|
|
42
|
+
abstract class Base {
|
|
43
|
+
declare readonly _newtype: Tag
|
|
44
|
+
|
|
45
|
+
static make(value: Schema.Schema.Type<S>): Self {
|
|
46
|
+
return value as unknown as Self
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
Object.setPrototypeOf(Base, schema)
|
|
51
|
+
|
|
52
|
+
return Base as unknown as (abstract new (_: never) => { readonly _newtype: Tag }) & {
|
|
53
|
+
readonly make: (value: Schema.Schema.Type<S>) => Self
|
|
54
|
+
} & Omit<Schema.Opaque<Self, S, {}>, "make" | "~type.make"> & {
|
|
55
|
+
readonly "~type.make": Self
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|