@tinyweb_dev/agent-framework-core 0.2.0 → 0.3.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/dist/error.cjs +22 -0
- package/dist/error.cjs.map +1 -0
- package/dist/error.d.ts +12 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +19 -0
- package/dist/error.js.map +1 -0
- package/dist/global.cjs +50 -0
- package/dist/global.cjs.map +1 -0
- package/dist/global.d.ts +11 -0
- package/dist/global.d.ts.map +1 -0
- package/dist/global.js +40 -0
- package/dist/global.js.map +1 -0
- package/dist/id.cjs +41 -0
- package/dist/id.cjs.map +1 -0
- package/{src/id.ts → dist/id.d.ts} +8 -16
- package/dist/id.d.ts.map +1 -0
- package/dist/id.js +33 -0
- package/dist/id.js.map +1 -0
- package/dist/index.cjs +200 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +184 -0
- package/dist/index.js.map +1 -0
- package/dist/log.cjs +110 -0
- package/dist/log.cjs.map +1 -0
- package/dist/log.d.ts +22 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +104 -0
- package/dist/log.js.map +1 -0
- package/dist/schema.cjs +26 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/schema.d.ts +40 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +21 -0
- package/dist/schema.js.map +1 -0
- package/package.json +36 -13
- package/src/error.ts +0 -12
- package/src/global.ts +0 -27
- package/src/index.ts +0 -7
- package/src/log.ts +0 -129
- package/src/schema.ts +0 -58
package/package.json
CHANGED
|
@@ -1,25 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@tinyweb_dev/agent-framework-core",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"typecheck": "tsc --noEmit",
|
|
9
|
-
"test": "bun test --timeout 30000"
|
|
10
|
-
},
|
|
11
7
|
"files": [
|
|
12
|
-
"
|
|
8
|
+
"dist",
|
|
13
9
|
"README.md",
|
|
14
10
|
"LICENSE"
|
|
15
11
|
],
|
|
16
12
|
"exports": {
|
|
17
|
-
".":
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"./
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./schema": {
|
|
19
|
+
"types": "./dist/schema.d.ts",
|
|
20
|
+
"import": "./dist/schema.js",
|
|
21
|
+
"require": "./dist/schema.cjs"
|
|
22
|
+
},
|
|
23
|
+
"./log": {
|
|
24
|
+
"types": "./dist/log.d.ts",
|
|
25
|
+
"import": "./dist/log.js",
|
|
26
|
+
"require": "./dist/log.cjs"
|
|
27
|
+
},
|
|
28
|
+
"./error": {
|
|
29
|
+
"types": "./dist/error.d.ts",
|
|
30
|
+
"import": "./dist/error.js",
|
|
31
|
+
"require": "./dist/error.cjs"
|
|
32
|
+
},
|
|
33
|
+
"./id": {
|
|
34
|
+
"types": "./dist/id.d.ts",
|
|
35
|
+
"import": "./dist/id.js",
|
|
36
|
+
"require": "./dist/id.cjs"
|
|
37
|
+
},
|
|
38
|
+
"./global": {
|
|
39
|
+
"types": "./dist/global.d.ts",
|
|
40
|
+
"import": "./dist/global.js",
|
|
41
|
+
"require": "./dist/global.cjs"
|
|
42
|
+
}
|
|
23
43
|
},
|
|
24
44
|
"devDependencies": {
|
|
25
45
|
"@tsconfig/bun": "1.0.9",
|
|
@@ -29,5 +49,8 @@
|
|
|
29
49
|
"dependencies": {
|
|
30
50
|
"effect": "4.0.0-beta.59",
|
|
31
51
|
"ulid": "3.0.1"
|
|
32
|
-
}
|
|
52
|
+
},
|
|
53
|
+
"main": "./dist/index.cjs",
|
|
54
|
+
"module": "./dist/index.js",
|
|
55
|
+
"types": "./dist/index.d.ts"
|
|
33
56
|
}
|
package/src/error.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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/index.ts
DELETED
package/src/log.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
}
|