Skip to content

Commit b83431f

Browse files
authored
feat(devframe)!: upgrade nostics to v0.2.0, drop deprecated APIs (#20)
1 parent 6f75d5c commit b83431f

19 files changed

Lines changed: 149 additions & 147 deletions

AGENTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ Prefix: **`DF`**. Codes are sequential 4-digit numbers (e.g. `DF0033`). Check th
6666
import { diagnostics } from './diagnostics'
6767

6868
// For thrown errors — always prefix with `throw` for TypeScript control flow:
69-
throw diagnostics.DF0033.throw({ name })
69+
throw diagnostics.DF0033({ id, reason })
7070

7171
// For reported warnings/errors (not thrown). The default console method is `warn`;
7272
// override with the 2nd-arg reporter options when needed:
73-
diagnostics.DF0033.report({ name }) // console.warn
74-
diagnostics.DF0033.report({ name }, { method: 'error' }) // console.error
75-
diagnostics.DF0033.report({ name, cause: error }, { method: 'warn' }) // attach cause
73+
diagnostics.DF0033({ id, reason }) // console.warn
74+
diagnostics.DF0033({ id, reason }, { method: 'error' }) // console.error
75+
diagnostics.DF0033({ id, reason, cause: error }, { method: 'warn' }) // attach cause
7676
```
7777

7878
3. **Create a docs page** at `docs/errors/DF0033.md` (when `docs/` lands):

docs/guide/diagnostics.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function MyPlugin(): PluginWithDevTools {
5252
ctx.diagnostics.register(myDiagnostics)
5353

5454
// Emit through the host's shared reporter:
55-
myDiagnostics.MYP0002.report()
55+
myDiagnostics.MYP0002()
5656
},
5757
},
5858
}
@@ -76,26 +76,26 @@ Each definition supports a `why` (string or function — the message) and an opt
7676

7777
## Emit a diagnostic
7878

79-
Each registered code becomes a `DiagnosticHandle` on the typed result of `defineDiagnostics()` (and through the shared `ctx.diagnostics.logger` lookup). Handles expose `.report()` and `.throw()`.
79+
Each registered code becomes a `DiagnosticHandle` on the typed result of `defineDiagnostics()` (and through the shared `ctx.diagnostics.logger` lookup). Each handle is a callable — invoke it to report (returns the `Diagnostic`), or prefix with `throw` to raise.
8080

8181
```ts
8282
// Throw — control flow stops here
83-
throw myDiagnostics.MYP0001.throw({ name: 'foo' })
83+
throw myDiagnostics.MYP0001({ name: 'foo' })
8484

8585
// Report without throwing (default console method: `warn`)
86-
myDiagnostics.MYP0002.report()
86+
myDiagnostics.MYP0002()
8787

8888
// Override the console method per call
89-
myDiagnostics.MYP0002.report({}, { method: 'error' })
89+
myDiagnostics.MYP0002({}, { method: 'error' })
9090

9191
// Attach a `cause` — merged into the params object
92-
myDiagnostics.MYP0001.throw({ name: 'foo', cause: error })
92+
throw myDiagnostics.MYP0001({ name: 'foo', cause: error })
9393
```
9494

95-
`.throw()` is typed `never`, so TypeScript treats the line after as unreachable. Prefix the call with `throw` for control-flow narrowing:
95+
The callable returns a `Diagnostic` (which extends `Error`). Prefix with `throw` so TypeScript narrows the lines after as unreachable:
9696

9797
```ts
98-
throw myDiagnostics.MYP0001.throw({ name })
98+
throw myDiagnostics.MYP0001({ name })
9999
```
100100

101101
## Typed handle reference
@@ -114,7 +114,7 @@ const myDiagnostics = ctx.diagnostics.defineDiagnostics({
114114
ctx.diagnostics.register(myDiagnostics)
115115

116116
// Use the typed handle directly for autocompletion
117-
myDiagnostics.MYP0001.report({ name: 'foo' })
117+
myDiagnostics.MYP0001({ name: 'foo' })
118118
```
119119

120120
The host's `defineDiagnostics()` pre-wires its ANSI console reporter, so both the typed handle and the shared lookup produce the same output.
@@ -134,7 +134,7 @@ Each page covers the message, cause, example, and fix — see any [DF code page]
134134

135135
## When to use what
136136

137-
- **`ctx.diagnostics`** — coded conditions worth looking up: misconfiguration, deprecations, validation failures, internal invariants. Always docs-backed. Often `.throw()`.
137+
- **`ctx.diagnostics`** — coded conditions worth looking up: misconfiguration, deprecations, validation failures, internal invariants. Always docs-backed. Often thrown.
138138
- **`ctx.messages`** — user-facing activity surfaces in the DevTools UI: progress indicators, audit results, "URL copied" toasts. Just a message and a level.
139139

140140
Diagnostics target tool authors and CI; messages target the human in front of the DevTools panel.

packages/devframe/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "devframe",
33
"type": "module",
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"description": "Framework for building generic DevTools",
66
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
77
"license": "MIT",
@@ -54,7 +54,7 @@
5454
"./utils/when": "./dist/utils/when.mjs",
5555
"./package.json": "./package.json"
5656
},
57-
"types": "./dist/index.d.ts",
57+
"types": "./dist/index.d.mts",
5858
"files": [
5959
"dist",
6060
"skills"

packages/devframe/src/adapters/mcp/build-server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export async function createMcpServer(
102102
): Promise<McpServerHandle> {
103103
const transport = options.transport ?? 'stdio'
104104
if (transport !== 'stdio')
105-
throw diagnostics.DF0017.throw({ transport, reason: 'Only stdio transport is supported in this release.' })
105+
throw diagnostics.DF0017({ transport, reason: 'Only stdio transport is supported in this release.' })
106106

107107
const host: DevToolsHost = {
108108
mountStatic: () => { /* MCP has no static surface */ },
@@ -132,7 +132,7 @@ export async function createMcpServer(
132132
}
133133
catch (error) {
134134
const reason = error instanceof Error ? error.message : String(error)
135-
throw diagnostics.DF0017.throw({ transport, reason, cause: error })
135+
throw diagnostics.DF0017({ transport, reason, cause: error })
136136
}
137137

138138
options.onReady?.({ transport: 'stdio' })

packages/devframe/src/helpers/vite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function viteDevBridge(d: DevframeDefinition, options: ViteDevBridgeOptio
106106
})
107107
}
108108
catch (e) {
109-
diagnostics.DF0033.report({ id: d.id, reason: String(e), cause: e as Error }, { method: 'warn' })
109+
diagnostics.DF0033({ id: d.id, reason: String(e), cause: e as Error }, { method: 'warn' })
110110
return
111111
}
112112

packages/devframe/src/node/host-agent.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class DevToolsAgentHost implements DevToolsAgentHostType {
7474

7575
registerResource(input: AgentResourceInput): AgentHandle {
7676
if (this.resources.has(input.id))
77-
throw diagnostics.DF0016.throw({ id: input.id })
77+
throw diagnostics.DF0016({ id: input.id })
7878

7979
const resource: AgentResource = {
8080
id: input.id,
@@ -154,16 +154,16 @@ export class DevToolsAgentHost implements DevToolsAgentHostType {
154154

155155
private _validateToolId(id: string): void {
156156
if (this.tools.has(id))
157-
throw diagnostics.DF0015.throw({ id })
157+
throw diagnostics.DF0015({ id })
158158
// Collision with an RPC function that already carries an `agent` field.
159159
const rpcDef = this.context.rpc.definitions.get(id)
160160
if (rpcDef?.agent)
161-
throw diagnostics.DF0015.throw({ id })
161+
throw diagnostics.DF0015({ id })
162162
}
163163

164164
private _projectTool(input: AgentToolInput): AgentTool {
165165
if (!input.description || typeof input.description !== 'string')
166-
throw diagnostics.DF0014.throw({ name: input.id })
166+
throw diagnostics.DF0014({ name: input.id })
167167

168168
return {
169169
id: input.id,
@@ -185,7 +185,7 @@ export class DevToolsAgentHost implements DevToolsAgentHostType {
185185
if (!agent)
186186
continue
187187
if (!agent.description || typeof agent.description !== 'string')
188-
throw diagnostics.DF0014.throw({ name })
188+
throw diagnostics.DF0014({ name })
189189

190190
const type: RpcFunctionType = def.type ?? 'query'
191191
const safety = agent.safety ?? inferSafety(type)

packages/devframe/src/node/host-functions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class RpcFunctionsHost extends RpcFunctionsCollectorBase<DevToolsRpcServe
4545
...args: Args
4646
): Promise<Awaited<ReturnType<DevToolsRpcServerFunctions[T]>>> {
4747
if (!this.definitions.has(method as string)) {
48-
throw diagnostics.DF0006.throw({ name: String(method) })
48+
throw diagnostics.DF0006({ name: String(method) })
4949
}
5050

5151
const handler = await this.getHandler(method)
@@ -80,7 +80,7 @@ export class RpcFunctionsHost extends RpcFunctionsCollectorBase<DevToolsRpcServe
8080

8181
getCurrentRpcSession(): DevToolsNodeRpcSession | undefined {
8282
if (!this._asyncStorage)
83-
throw diagnostics.DF0007.throw()
83+
throw diagnostics.DF0007()
8484
return this._asyncStorage.getStore()
8585
}
8686
}

packages/devframe/src/node/host-views.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class DevToolsViewHost implements DevToolsViewHostType {
1515

1616
hostStatic(baseUrl: string, distDir: string) {
1717
if (!existsSync(distDir)) {
18-
throw diagnostics.DF0008.throw({ distDir })
18+
throw diagnostics.DF0008({ distDir })
1919
}
2020

2121
this.buildStaticDirs.push({ baseUrl, distDir })

packages/devframe/src/node/rpc-shared-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function createRpcSharedStateServerHost(
5050
return sharedState.get(key)!
5151
}
5252
if (options?.initialValue === undefined && options?.sharedState === undefined) {
53-
throw diagnostics.DF0013.throw({ key })
53+
throw diagnostics.DF0013({ key })
5454
}
5555
debug('new-state', key)
5656
const state = options.sharedState ?? createSharedState<T>({

packages/devframe/src/node/rpc-streaming.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ export function createRpcStreamingServerHost(rpc: RpcFunctionsHost): RpcStreamin
9797
handler(channelName: string, id: string, opts?: { afterSeq?: number }) {
9898
const state = channels.get(channelName)
9999
if (!state) {
100-
diagnostics.DF0030.report({ channel: channelName, id }, { method: 'error' })
100+
diagnostics.DF0030({ channel: channelName, id }, { method: 'error' })
101101
return
102102
}
103103
const record = state.streams.get(id)
104104
if (!record) {
105-
diagnostics.DF0030.report({ channel: channelName, id }, { method: 'error' })
105+
diagnostics.DF0030({ channel: channelName, id }, { method: 'error' })
106106
return
107107
}
108108
const session = rpc.getCurrentRpcSession()
@@ -181,7 +181,7 @@ export function createRpcStreamingServerHost(rpc: RpcFunctionsHost): RpcStreamin
181181
const state = channels.get(channelName)
182182
const record = state?.inbound.get(id)
183183
if (!record) {
184-
diagnostics.DF0030.report({ channel: channelName, id }, { method: 'error' })
184+
diagnostics.DF0030({ channel: channelName, id }, { method: 'error' })
185185
return
186186
}
187187
// Lock the inbound to the first session that writes; subsequent
@@ -218,7 +218,7 @@ export function createRpcStreamingServerHost(rpc: RpcFunctionsHost): RpcStreamin
218218

219219
function createChannel<T>(name: string, opts: RpcStreamingChannelOptions = {}): RpcStreamingChannel<T> {
220220
if (channels.has(name))
221-
throw diagnostics.DF0032.throw({ channel: name })
221+
throw diagnostics.DF0032({ channel: name })
222222

223223
const replayWindow = opts.replayWindow ?? 0
224224
const state: ChannelState<T> = {

0 commit comments

Comments
 (0)