@openwop/openwop 1.1.1 → 1.1.3
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/README.md +4 -0
- package/dist/client.d.ts +80 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +186 -0
- package/dist/client.js.map +1 -1
- package/dist/cost-attribution.d.ts +49 -0
- package/dist/cost-attribution.d.ts.map +1 -0
- package/dist/cost-attribution.js +65 -0
- package/dist/cost-attribution.js.map +1 -0
- package/dist/envelope-directive.d.ts +77 -0
- package/dist/envelope-directive.d.ts.map +1 -0
- package/dist/envelope-directive.js +89 -0
- package/dist/envelope-directive.js.map +1 -0
- package/dist/event-helpers.d.ts +95 -0
- package/dist/event-helpers.d.ts.map +1 -0
- package/dist/event-helpers.js +160 -0
- package/dist/event-helpers.js.map +1 -0
- package/dist/index.d.ts +14 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -1
- package/dist/parse-refusal.d.ts +114 -0
- package/dist/parse-refusal.d.ts.map +1 -0
- package/dist/parse-refusal.js +216 -0
- package/dist/parse-refusal.js.map +1 -0
- package/dist/registry-helpers.d.ts +118 -0
- package/dist/registry-helpers.d.ts.map +1 -0
- package/dist/registry-helpers.js +82 -0
- package/dist/registry-helpers.js.map +1 -0
- package/dist/types.d.ts +376 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/webhook-helpers.d.ts +73 -0
- package/dist/webhook-helpers.d.ts.map +1 -0
- package/dist/webhook-helpers.js +97 -0
- package/dist/webhook-helpers.js.map +1 -0
- package/package.json +1 -1
- package/src/client.ts +218 -0
- package/src/cost-attribution.ts +72 -0
- package/src/envelope-directive.ts +110 -0
- package/src/event-helpers.ts +238 -0
- package/src/index.ts +117 -0
- package/src/parse-refusal.ts +311 -0
- package/src/registry-helpers.ts +173 -0
- package/src/types.ts +424 -0
- package/src/webhook-helpers.ts +131 -0
package/src/index.ts
CHANGED
|
@@ -28,20 +28,61 @@ export type {
|
|
|
28
28
|
ErrorEnvelope,
|
|
29
29
|
ForkRunRequest,
|
|
30
30
|
ForkRunResponse,
|
|
31
|
+
DebugBundle,
|
|
32
|
+
DebugBundleOptions,
|
|
33
|
+
RegisterWebhookRequest,
|
|
34
|
+
RegisterWebhookResponse,
|
|
31
35
|
InterruptByTokenInspection,
|
|
36
|
+
PauseRunRequest,
|
|
37
|
+
PauseRunResponse,
|
|
32
38
|
PollEventsResponse,
|
|
33
39
|
ResolveInterruptByTokenResponse,
|
|
34
40
|
ResolveInterruptRequest,
|
|
35
41
|
ResolveInterruptResponse,
|
|
42
|
+
ResumeRunRequest,
|
|
43
|
+
ResumeRunResponse,
|
|
36
44
|
RunConfigurable,
|
|
37
45
|
RunEventDoc,
|
|
38
46
|
RunSnapshot,
|
|
39
47
|
RunStatus,
|
|
40
48
|
StreamMode,
|
|
49
|
+
TypedRunEvent,
|
|
50
|
+
AgentReasonedPayload,
|
|
51
|
+
AgentReasoningDeltaPayload,
|
|
52
|
+
AgentToolCalledPayload,
|
|
53
|
+
AgentToolReturnedPayload,
|
|
54
|
+
AgentHandoffPayload,
|
|
55
|
+
AgentDecidedPayload,
|
|
56
|
+
// RFC 0027 + RFC 0028 — Prompt library (spec/v1/prompts.md)
|
|
57
|
+
GetPromptRequest,
|
|
58
|
+
ListPromptsRequest,
|
|
59
|
+
ListPromptsResponse,
|
|
60
|
+
PromptKind,
|
|
61
|
+
PromptRef,
|
|
62
|
+
PromptTemplate,
|
|
63
|
+
PromptVariable,
|
|
64
|
+
RenderPromptRequest,
|
|
65
|
+
RenderPromptResponse,
|
|
41
66
|
} from './types.js';
|
|
42
67
|
export { streamEvents } from './sse.js';
|
|
43
68
|
export type { EventsStreamContext, EventsStreamOptions } from './sse.js';
|
|
44
69
|
|
|
70
|
+
// RFC 0002 + RFC 0024 typed event helpers — type guards over `RunEventDoc`
|
|
71
|
+
// plus a high-level streaming-reasoning subscription helper.
|
|
72
|
+
export {
|
|
73
|
+
isAgentReasoned,
|
|
74
|
+
isAgentReasoningDelta,
|
|
75
|
+
isAgentToolCalled,
|
|
76
|
+
isAgentToolReturned,
|
|
77
|
+
isAgentHandoff,
|
|
78
|
+
isAgentDecided,
|
|
79
|
+
subscribeToAgentReasoning,
|
|
80
|
+
} from './event-helpers.js';
|
|
81
|
+
export type {
|
|
82
|
+
AgentReasoningCallbacks,
|
|
83
|
+
Unsubscribe,
|
|
84
|
+
} from './event-helpers.js';
|
|
85
|
+
|
|
45
86
|
// Run-status + run-error helpers (added in 1). Forward-compat predicates
|
|
46
87
|
// over the wire-format unions declared above; full design + rationale in
|
|
47
88
|
// `run-helpers.ts`.
|
|
@@ -61,3 +102,79 @@ export type {
|
|
|
61
102
|
RunErrorCode,
|
|
62
103
|
RunError,
|
|
63
104
|
} from './run-helpers.js';
|
|
105
|
+
|
|
106
|
+
// Cost-attribution allowlist + sanitizer helpers
|
|
107
|
+
// (`spec/v1/observability.md §"Cost attribution attributes"`). Single
|
|
108
|
+
// source of truth for the canonical attribute names so independent
|
|
109
|
+
// hosts share one list instead of each re-deriving it.
|
|
110
|
+
export {
|
|
111
|
+
OPENWOP_COST_ATTRIBUTE_NAMES,
|
|
112
|
+
sanitizeCostAttributes,
|
|
113
|
+
} from './cost-attribution.js';
|
|
114
|
+
export type { OpenwopCostAttributeName } from './cost-attribution.js';
|
|
115
|
+
|
|
116
|
+
// Webhook-delivery verification helpers (SDK-3 close-out 2026-05-15).
|
|
117
|
+
// HMAC-SHA256 + timestamp freshness window verification per
|
|
118
|
+
// spec/v1/webhooks.md §"Signature recipe". Receivers MUST verify both
|
|
119
|
+
// the HMAC AND the timestamp to defeat replay attacks.
|
|
120
|
+
export {
|
|
121
|
+
DEFAULT_WEBHOOK_FRESHNESS_WINDOW_SECONDS,
|
|
122
|
+
verifyWebhookSignature,
|
|
123
|
+
signWebhookDelivery,
|
|
124
|
+
} from './webhook-helpers.js';
|
|
125
|
+
export type {
|
|
126
|
+
VerifyWebhookSignatureOptions,
|
|
127
|
+
VerifyWebhookOutcome,
|
|
128
|
+
} from './webhook-helpers.js';
|
|
129
|
+
|
|
130
|
+
// Public-registry read helpers (SDK-5 close-out 2026-05-15). Read-only
|
|
131
|
+
// typed client for the public node-pack registry at packs.openwop.dev
|
|
132
|
+
// per spec/v1/registry-operations.md.
|
|
133
|
+
export { RegistryClient } from './registry-helpers.js';
|
|
134
|
+
export type {
|
|
135
|
+
RegistryClientOptions,
|
|
136
|
+
RegistryDiscovery,
|
|
137
|
+
RegistryIndex,
|
|
138
|
+
RegistryIndexEntry,
|
|
139
|
+
RegistryPackMetadata,
|
|
140
|
+
RegistryVersionManifest,
|
|
141
|
+
} from './registry-helpers.js';
|
|
142
|
+
|
|
143
|
+
// AI Envelope types (DRAFT v1.x — spec/v1/ai-envelope.md). Inbound LLM-emission
|
|
144
|
+
// envelope, distinct from RunEventDoc (outbound) and ErrorEnvelope (host HTTP).
|
|
145
|
+
export type {
|
|
146
|
+
AIEnvelope,
|
|
147
|
+
AIEnvelopeErrorPayload,
|
|
148
|
+
ClarificationRequestPayload,
|
|
149
|
+
EnvelopeContract,
|
|
150
|
+
EnvelopeContractRefusal,
|
|
151
|
+
EnvelopeContractsCapability,
|
|
152
|
+
EnvelopeMeta,
|
|
153
|
+
EnvelopeOutcome,
|
|
154
|
+
EnvelopeStrictness,
|
|
155
|
+
PartialInfo,
|
|
156
|
+
SchemaRequestPayload,
|
|
157
|
+
SchemaResponsePayload,
|
|
158
|
+
ValidationDetail,
|
|
159
|
+
} from './types.js';
|
|
160
|
+
|
|
161
|
+
// RFC 0030 §A `reasoning` field prompt-directive helper. Hosts that
|
|
162
|
+
// advertise `capabilities.envelopes.reasoning.supported: true` use this
|
|
163
|
+
// to synthesize the system-prompt fragment that instructs the model to
|
|
164
|
+
// populate the OPTIONAL `reasoning` field. See `envelope-directive.ts`
|
|
165
|
+
// for the operational note on `"mandatory"` strength (provider-side
|
|
166
|
+
// refusal risk per the 2026-05-21 RFC 0030 amendment).
|
|
167
|
+
export { buildReasoningDirective } from './envelope-directive.js';
|
|
168
|
+
export type { ReasoningDirectiveStrength } from './envelope-directive.js';
|
|
169
|
+
|
|
170
|
+
// RFC 0032 §B.3 + RFC 0033 §D refusal detection helper. Normalizes
|
|
171
|
+
// per-provider safety-stop signals (Anthropic `stop_reason: "refusal"`,
|
|
172
|
+
// OpenAI `message.refusal` + `finish_reason: "content_filter"`, Gemini
|
|
173
|
+
// `finishReason: "SAFETY"` + `promptFeedback.blockReason: "SAFETY"`)
|
|
174
|
+
// to a canonical `{ refusalText, safetyCategory?, provider }` shape.
|
|
175
|
+
// Hosts route the non-null return through `envelope.refusal` emission
|
|
176
|
+
// + the `envelope_refusal` terminal error code (RFC 0033 §F).
|
|
177
|
+
// **SECURITY:** Pass `refusalText` through the BYOK redaction harness
|
|
178
|
+
// BEFORE persistence — this helper does NOT redact.
|
|
179
|
+
export { parseRefusal } from './parse-refusal.js';
|
|
180
|
+
export type { RefusalProvider, RefusalSignal } from './parse-refusal.js';
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parseRefusal — normalize per-provider LLM safety-stop signals to the
|
|
3
|
+
* canonical RFC 0032 §B.3 refusal shape.
|
|
4
|
+
*
|
|
5
|
+
* The three Tier-1 vendors (Anthropic / OpenAI / Google Gemini) surface
|
|
6
|
+
* refusals through different fields:
|
|
7
|
+
*
|
|
8
|
+
* - **Anthropic Messages API**: `stop_reason: "refusal"` (their 2025
|
|
9
|
+
* release) OR `stop_reason: "end_turn"` accompanied by safety-stop
|
|
10
|
+
* markers in the content. Refusal text MAY be inline in the
|
|
11
|
+
* `content[]` array's text blocks.
|
|
12
|
+
* - **OpenAI Chat Completions**: `choices[0].finish_reason:
|
|
13
|
+
* "content_filter"` OR `choices[0].message.refusal: <string>` (the
|
|
14
|
+
* refusal-text field added in their structured-output release).
|
|
15
|
+
* - **Google Gemini**: `candidates[0].finishReason: "SAFETY"` OR
|
|
16
|
+
* `promptFeedback.blockReason: "SAFETY"` (input-side block).
|
|
17
|
+
*
|
|
18
|
+
* Without normalization, every host re-implements this detection. This
|
|
19
|
+
* helper consolidates the per-vendor shape-detection and returns a
|
|
20
|
+
* canonical `RefusalSignal | null` — null when the response is a
|
|
21
|
+
* normal completion (no refusal detected).
|
|
22
|
+
*
|
|
23
|
+
* Per RFC 0032 §B.3 + RFC 0033 §D, hosts MUST NOT retry on refusal
|
|
24
|
+
* (circumvention concern). Callers route the non-null return through
|
|
25
|
+
* `envelope.refusal` emission + the `envelope_refusal` terminal error
|
|
26
|
+
* code (per RFC 0033 §F).
|
|
27
|
+
*
|
|
28
|
+
* Per SECURITY/invariants.yaml §envelope-refusal-no-prompt-leak,
|
|
29
|
+
* `refusalText` MUST be passed through the host's BYOK redaction
|
|
30
|
+
* harness BEFORE persistence — this helper does NOT redact; the
|
|
31
|
+
* caller is responsible for SR-1 carry-forward.
|
|
32
|
+
*
|
|
33
|
+
* @see RFCS/0032-envelope-reliability-events.md §B.3
|
|
34
|
+
* @see RFCS/0033-envelope-completion-contract.md §D + §F
|
|
35
|
+
* @see SECURITY/invariants.yaml §envelope-refusal-no-prompt-leak
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Provider identifier for the matched shape. `"unknown"` is reserved
|
|
40
|
+
* for refusals matched on heuristic signals without a definitive
|
|
41
|
+
* vendor-shape match.
|
|
42
|
+
*/
|
|
43
|
+
export type RefusalProvider = 'anthropic' | 'openai' | 'google' | 'unknown';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Canonical refusal signal. All three Tier-1 vendor shapes normalize
|
|
47
|
+
* to this form. `null` from `parseRefusal()` means "no refusal detected"
|
|
48
|
+
* (a normal completion); a non-null `RefusalSignal` means the caller
|
|
49
|
+
* SHOULD route through the RFC 0032 §B.3 refusal-emission path.
|
|
50
|
+
*/
|
|
51
|
+
export interface RefusalSignal {
|
|
52
|
+
/**
|
|
53
|
+
* The provider's refusal text, when surfaced. MAY be `null` even for
|
|
54
|
+
* detected refusals — Anthropic + Gemini frequently refuse without
|
|
55
|
+
* inline text (the safety filter is opaque to the model). OpenAI's
|
|
56
|
+
* `message.refusal` field is the most consistent source of human-
|
|
57
|
+
* readable refusal text.
|
|
58
|
+
*
|
|
59
|
+
* **SECURITY:** When non-null, `refusalText` MAY contain prompt
|
|
60
|
+
* content that triggered the safety filter. Callers MUST pass it
|
|
61
|
+
* through their BYOK + prompt-content redaction harness BEFORE
|
|
62
|
+
* persistence (per `envelope-refusal-no-prompt-leak` SECURITY
|
|
63
|
+
* invariant).
|
|
64
|
+
*/
|
|
65
|
+
refusalText: string | null;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Provider-specific safety-category identifier, when surfaced.
|
|
69
|
+
* Examples: Gemini's safety-rating categories
|
|
70
|
+
* (`HARM_CATEGORY_HARASSMENT`, etc.), OpenAI's content-filter category,
|
|
71
|
+
* Anthropic's policy-violation tag. Hosts MAY echo this on the
|
|
72
|
+
* `envelope.refusal.safetyCategory` event field for downstream
|
|
73
|
+
* observability.
|
|
74
|
+
*/
|
|
75
|
+
safetyCategory?: string;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Which provider's shape was matched. Useful for debugging
|
|
79
|
+
* cross-host integration + for hosts that want to route refusals
|
|
80
|
+
* differently per vendor (e.g., different operator notifications).
|
|
81
|
+
*/
|
|
82
|
+
provider: RefusalProvider;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface OpenAIChoiceMessage {
|
|
86
|
+
refusal?: unknown;
|
|
87
|
+
content?: unknown;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface OpenAIChoice {
|
|
91
|
+
finish_reason?: unknown;
|
|
92
|
+
message?: unknown;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface OpenAIResponse {
|
|
96
|
+
choices?: unknown;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface AnthropicTextBlock {
|
|
100
|
+
type?: unknown;
|
|
101
|
+
text?: unknown;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface AnthropicResponse {
|
|
105
|
+
stop_reason?: unknown;
|
|
106
|
+
content?: unknown;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface GeminiSafetyRating {
|
|
110
|
+
category?: unknown;
|
|
111
|
+
probability?: unknown;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface GeminiCandidate {
|
|
115
|
+
finishReason?: unknown;
|
|
116
|
+
safetyRatings?: unknown;
|
|
117
|
+
content?: unknown;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface GeminiPromptFeedback {
|
|
121
|
+
blockReason?: unknown;
|
|
122
|
+
safetyRatings?: unknown;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface GeminiResponse {
|
|
126
|
+
candidates?: unknown;
|
|
127
|
+
promptFeedback?: unknown;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Try to parse the response as OpenAI Chat Completions output.
|
|
132
|
+
*
|
|
133
|
+
* Detection: top-level `choices` array. Refusal signals:
|
|
134
|
+
* - `choices[0].finish_reason === "content_filter"`
|
|
135
|
+
* - `choices[0].message.refusal` is a non-empty string
|
|
136
|
+
*/
|
|
137
|
+
function tryParseOpenAI(response: unknown): RefusalSignal | null {
|
|
138
|
+
if (!response || typeof response !== 'object') return null;
|
|
139
|
+
const r = response as OpenAIResponse;
|
|
140
|
+
if (!Array.isArray(r.choices) || r.choices.length === 0) return null;
|
|
141
|
+
const choice = r.choices[0] as OpenAIChoice;
|
|
142
|
+
if (!choice || typeof choice !== 'object') return null;
|
|
143
|
+
|
|
144
|
+
const finishReason = choice.finish_reason;
|
|
145
|
+
const message = choice.message as OpenAIChoiceMessage | undefined;
|
|
146
|
+
const refusalField = message && typeof message === 'object' ? message.refusal : undefined;
|
|
147
|
+
|
|
148
|
+
// Primary signal: explicit refusal-text field. OpenAI's structured-output
|
|
149
|
+
// release populates this when the safety filter intervenes.
|
|
150
|
+
if (typeof refusalField === 'string' && refusalField.length > 0) {
|
|
151
|
+
return { refusalText: refusalField, provider: 'openai' };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Secondary signal: finish_reason. content_filter is the canonical
|
|
155
|
+
// safety-stop value.
|
|
156
|
+
if (finishReason === 'content_filter') {
|
|
157
|
+
const text =
|
|
158
|
+
message && typeof message === 'object' && typeof message.content === 'string'
|
|
159
|
+
? message.content
|
|
160
|
+
: null;
|
|
161
|
+
return { refusalText: text, safetyCategory: 'content_filter', provider: 'openai' };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Try to parse the response as Anthropic Messages API output.
|
|
169
|
+
*
|
|
170
|
+
* Detection: top-level `stop_reason` field (Anthropic's distinctive
|
|
171
|
+
* marker). Refusal signals:
|
|
172
|
+
* - `stop_reason === "refusal"` (their 2025 release)
|
|
173
|
+
*
|
|
174
|
+
* Anthropic does not surface a distinct safety-category field on
|
|
175
|
+
* refusals; the `refusal` stop_reason is the binary signal.
|
|
176
|
+
*/
|
|
177
|
+
function tryParseAnthropic(response: unknown): RefusalSignal | null {
|
|
178
|
+
if (!response || typeof response !== 'object') return null;
|
|
179
|
+
const r = response as AnthropicResponse;
|
|
180
|
+
if (typeof r.stop_reason !== 'string') return null;
|
|
181
|
+
|
|
182
|
+
if (r.stop_reason === 'refusal') {
|
|
183
|
+
// Extract refusal text from the content array (Anthropic returns an
|
|
184
|
+
// array of typed blocks; refusal text appears in `text`-type blocks).
|
|
185
|
+
let refusalText: string | null = null;
|
|
186
|
+
if (Array.isArray(r.content)) {
|
|
187
|
+
const textBlocks: string[] = [];
|
|
188
|
+
for (const block of r.content) {
|
|
189
|
+
if (block && typeof block === 'object') {
|
|
190
|
+
const b = block as AnthropicTextBlock;
|
|
191
|
+
if (b.type === 'text' && typeof b.text === 'string') {
|
|
192
|
+
textBlocks.push(b.text);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (textBlocks.length > 0) refusalText = textBlocks.join('\n');
|
|
197
|
+
}
|
|
198
|
+
return { refusalText, provider: 'anthropic' };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Try to parse the response as Google Gemini `generateContent` output.
|
|
206
|
+
*
|
|
207
|
+
* Detection: top-level `candidates` array OR top-level `promptFeedback`
|
|
208
|
+
* object. Refusal signals:
|
|
209
|
+
* - `candidates[0].finishReason === "SAFETY"` (output-side block)
|
|
210
|
+
* - `promptFeedback.blockReason === "SAFETY"` (input-side block)
|
|
211
|
+
*
|
|
212
|
+
* Gemini surfaces safety categories on `safetyRatings[]`; this helper
|
|
213
|
+
* picks the highest-probability HIGH/MEDIUM-tier category as
|
|
214
|
+
* `safetyCategory` when available.
|
|
215
|
+
*/
|
|
216
|
+
function tryParseGemini(response: unknown): RefusalSignal | null {
|
|
217
|
+
if (!response || typeof response !== 'object') return null;
|
|
218
|
+
const r = response as GeminiResponse;
|
|
219
|
+
|
|
220
|
+
// Output-side safety block.
|
|
221
|
+
if (Array.isArray(r.candidates) && r.candidates.length > 0) {
|
|
222
|
+
const candidate = r.candidates[0] as GeminiCandidate;
|
|
223
|
+
if (candidate && typeof candidate === 'object' && candidate.finishReason === 'SAFETY') {
|
|
224
|
+
const safetyCategory = extractGeminiHighestRiskCategory(candidate.safetyRatings);
|
|
225
|
+
const result: RefusalSignal = { refusalText: null, provider: 'google' };
|
|
226
|
+
if (safetyCategory !== undefined) result.safetyCategory = safetyCategory;
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Input-side safety block (Gemini rejected the prompt itself).
|
|
232
|
+
if (r.promptFeedback && typeof r.promptFeedback === 'object') {
|
|
233
|
+
const pf = r.promptFeedback as GeminiPromptFeedback;
|
|
234
|
+
if (typeof pf.blockReason === 'string' && pf.blockReason.toUpperCase().includes('SAFETY')) {
|
|
235
|
+
const safetyCategory = extractGeminiHighestRiskCategory(pf.safetyRatings);
|
|
236
|
+
const result: RefusalSignal = { refusalText: null, provider: 'google' };
|
|
237
|
+
if (safetyCategory !== undefined) result.safetyCategory = safetyCategory;
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* From a Gemini `safetyRatings[]` array, return the highest-probability
|
|
247
|
+
* non-NEGLIGIBLE category identifier. Returns `undefined` when the
|
|
248
|
+
* array is absent or all ratings are NEGLIGIBLE.
|
|
249
|
+
*/
|
|
250
|
+
function extractGeminiHighestRiskCategory(safetyRatings: unknown): string | undefined {
|
|
251
|
+
if (!Array.isArray(safetyRatings)) return undefined;
|
|
252
|
+
const PROBABILITY_RANK: Record<string, number> = {
|
|
253
|
+
HIGH: 3,
|
|
254
|
+
MEDIUM: 2,
|
|
255
|
+
LOW: 1,
|
|
256
|
+
NEGLIGIBLE: 0,
|
|
257
|
+
};
|
|
258
|
+
let best: { category: string; rank: number } | null = null;
|
|
259
|
+
for (const rating of safetyRatings) {
|
|
260
|
+
if (!rating || typeof rating !== 'object') continue;
|
|
261
|
+
const r = rating as GeminiSafetyRating;
|
|
262
|
+
if (typeof r.category !== 'string' || typeof r.probability !== 'string') continue;
|
|
263
|
+
const rank = PROBABILITY_RANK[r.probability.toUpperCase()] ?? 0;
|
|
264
|
+
if (rank === 0) continue;
|
|
265
|
+
if (best === null || rank > best.rank) {
|
|
266
|
+
best = { category: r.category, rank };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return best?.category;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Parse a provider response into a canonical refusal signal.
|
|
274
|
+
*
|
|
275
|
+
* Returns `null` when no refusal is detected (normal completion).
|
|
276
|
+
* Returns a `RefusalSignal` when the response matches one of the
|
|
277
|
+
* three Tier-1 vendors' safety-stop shapes.
|
|
278
|
+
*
|
|
279
|
+
* Detection order: OpenAI → Anthropic → Gemini. Each detector inspects
|
|
280
|
+
* a distinctive top-level field, so cross-vendor false-positives are
|
|
281
|
+
* unlikely. A response that doesn't match any vendor shape returns
|
|
282
|
+
* `null` (hosts that route through novel providers add their own
|
|
283
|
+
* detector + fall back to this for the three known ones).
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* import { parseRefusal } from '@openwop/openwop';
|
|
288
|
+
*
|
|
289
|
+
* const response = await callOpenAI({...});
|
|
290
|
+
* const refusal = parseRefusal(response);
|
|
291
|
+
* if (refusal) {
|
|
292
|
+
* // Route through envelope.refusal emission + envelope_refusal error code.
|
|
293
|
+
* // REMEMBER to redact refusalText through the BYOK harness before
|
|
294
|
+
* // persistence (SECURITY invariant envelope-refusal-no-prompt-leak).
|
|
295
|
+
* await emitEnvelopeRefusal({
|
|
296
|
+
* refusalText: redactBYOK(refusal.refusalText),
|
|
297
|
+
* safetyCategory: refusal.safetyCategory,
|
|
298
|
+
* provider: refusal.provider,
|
|
299
|
+
* });
|
|
300
|
+
* throw new EnvelopeRefusalError(...);
|
|
301
|
+
* }
|
|
302
|
+
* // ...normal-completion handling...
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
export function parseRefusal(providerResponse: unknown): RefusalSignal | null {
|
|
306
|
+
return (
|
|
307
|
+
tryParseOpenAI(providerResponse) ??
|
|
308
|
+
tryParseAnthropic(providerResponse) ??
|
|
309
|
+
tryParseGemini(providerResponse)
|
|
310
|
+
);
|
|
311
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public-registry read helpers per
|
|
3
|
+
* `spec/v1/registry-operations.md`.
|
|
4
|
+
*
|
|
5
|
+
* The OpenWOP host SDK targets the host wire surface
|
|
6
|
+
* (`/.well-known/openwop` + `/v1/runs/*` + `/v1/interrupts/*` etc).
|
|
7
|
+
* The **public node-pack registry** at `packs.openwop.dev` is a
|
|
8
|
+
* separate wire surface with its own discovery payload and pack-
|
|
9
|
+
* versioned read endpoints. This module exposes a thin typed client
|
|
10
|
+
* for that surface so adopters fetching pack manifests, indices, or
|
|
11
|
+
* signature material don't roll their own HTTP plumbing.
|
|
12
|
+
*
|
|
13
|
+
* Read-only by design — the public registry uses pull-request-driven
|
|
14
|
+
* publishing per `spec/v1/registry-operations.md` §"Submission flow"
|
|
15
|
+
* + the `registry-publish.yml` GitHub workflow. There is no write API.
|
|
16
|
+
*
|
|
17
|
+
* No auth required for public reads.
|
|
18
|
+
*
|
|
19
|
+
* @module @openwop/openwop/registry-helpers
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** Public registry discovery payload per `registry-operations.md` §"Discovery". */
|
|
23
|
+
export interface RegistryDiscovery {
|
|
24
|
+
registryVersion: string;
|
|
25
|
+
protocolVersion: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
operator?: string;
|
|
28
|
+
url?: string;
|
|
29
|
+
supportedNamespaces: readonly string[];
|
|
30
|
+
supportedSigningMethods: readonly string[];
|
|
31
|
+
supportedTrustModes?: readonly string[];
|
|
32
|
+
endpoints: {
|
|
33
|
+
registryIndex: string;
|
|
34
|
+
packMetadata: string;
|
|
35
|
+
versionManifest: string;
|
|
36
|
+
versionTarball: string;
|
|
37
|
+
versionSignature: string;
|
|
38
|
+
publicKey: string;
|
|
39
|
+
};
|
|
40
|
+
signingKeys?: ReadonlyArray<{
|
|
41
|
+
keyId: string;
|
|
42
|
+
algorithm: string;
|
|
43
|
+
publicKeyUrl?: string;
|
|
44
|
+
permittedNamespaces?: readonly string[];
|
|
45
|
+
operator?: string;
|
|
46
|
+
status?: string;
|
|
47
|
+
}>;
|
|
48
|
+
[key: string]: unknown;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Registry-wide index entry per `/v1/index.json` rows. */
|
|
52
|
+
export interface RegistryIndexEntry {
|
|
53
|
+
name: string;
|
|
54
|
+
latestVersion: string;
|
|
55
|
+
description?: string;
|
|
56
|
+
scope?: string;
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface RegistryIndex {
|
|
61
|
+
packs: ReadonlyArray<RegistryIndexEntry>;
|
|
62
|
+
generated?: string;
|
|
63
|
+
packCount?: number;
|
|
64
|
+
[key: string]: unknown;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Per-pack metadata document at `/v1/packs/{name}/index.json`. */
|
|
68
|
+
export interface RegistryPackMetadata {
|
|
69
|
+
name: string;
|
|
70
|
+
description?: string;
|
|
71
|
+
versions: ReadonlyArray<string>;
|
|
72
|
+
latestVersion?: string;
|
|
73
|
+
[key: string]: unknown;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Version manifest at `/v1/packs/{name}/-/{version}.json`. */
|
|
77
|
+
export interface RegistryVersionManifest {
|
|
78
|
+
name: string;
|
|
79
|
+
version: string;
|
|
80
|
+
description?: string;
|
|
81
|
+
/** SRI hash: `sha256-<43-char-b64>=`. */
|
|
82
|
+
integrity?: string;
|
|
83
|
+
signing?: {
|
|
84
|
+
method: 'ed25519' | 'manual' | string;
|
|
85
|
+
keyId: string;
|
|
86
|
+
publicKeyUrl?: string;
|
|
87
|
+
};
|
|
88
|
+
[key: string]: unknown;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface RegistryClientOptions {
|
|
92
|
+
/** Base URL for the registry. Defaults to `https://packs.openwop.dev`. */
|
|
93
|
+
baseUrl?: string;
|
|
94
|
+
/** Custom fetch implementation; defaults to `globalThis.fetch`. */
|
|
95
|
+
fetch?: typeof fetch;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Typed client for the public OpenWOP node-pack registry. Read-only;
|
|
100
|
+
* no auth required for the canonical public read surface.
|
|
101
|
+
*
|
|
102
|
+
* For host-side install-time verification (SRI + Ed25519 + lockfile),
|
|
103
|
+
* see `examples/hosts/postgres/src/pack-consumer.ts` — the registry
|
|
104
|
+
* client is the fetch surface; the consumer is the security surface.
|
|
105
|
+
*/
|
|
106
|
+
export class RegistryClient {
|
|
107
|
+
readonly baseUrl: string;
|
|
108
|
+
readonly #fetch: typeof fetch;
|
|
109
|
+
|
|
110
|
+
constructor(options: RegistryClientOptions = {}) {
|
|
111
|
+
this.baseUrl = (options.baseUrl ?? 'https://packs.openwop.dev').replace(/\/$/, '');
|
|
112
|
+
this.#fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `GET /.well-known/openwop-registry` — discovery + endpoint catalog. */
|
|
116
|
+
async discovery(): Promise<RegistryDiscovery> {
|
|
117
|
+
return this.#getJson<RegistryDiscovery>('/.well-known/openwop-registry');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** `GET /v1/index.json` — registry-wide pack index. */
|
|
121
|
+
async index(): Promise<RegistryIndex> {
|
|
122
|
+
return this.#getJson<RegistryIndex>('/v1/index.json');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** `GET /v1/packs/{name}/index.json` — per-pack metadata. */
|
|
126
|
+
async pack(name: string): Promise<RegistryPackMetadata> {
|
|
127
|
+
return this.#getJson<RegistryPackMetadata>(
|
|
128
|
+
`/v1/packs/${encodeURIComponent(name)}/index.json`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** `GET /v1/packs/{name}/-/{version}.json` — version manifest. */
|
|
133
|
+
async version(name: string, version: string): Promise<RegistryVersionManifest> {
|
|
134
|
+
return this.#getJson<RegistryVersionManifest>(
|
|
135
|
+
`/v1/packs/${encodeURIComponent(name)}/-/${encodeURIComponent(version)}.json`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Fetch raw tarball bytes. Caller MUST verify SRI + signature before trust. */
|
|
140
|
+
async tarball(name: string, version: string): Promise<Buffer> {
|
|
141
|
+
return this.#getBinary(
|
|
142
|
+
`/v1/packs/${encodeURIComponent(name)}/-/${encodeURIComponent(version)}.tgz`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Fetch raw 64-byte Ed25519 signature bytes. */
|
|
147
|
+
async signature(name: string, version: string): Promise<Buffer> {
|
|
148
|
+
return this.#getBinary(
|
|
149
|
+
`/v1/packs/${encodeURIComponent(name)}/-/${encodeURIComponent(version)}.sig`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Fetch a publisher's public key as PEM text. */
|
|
154
|
+
async publicKey(keyId: string): Promise<string> {
|
|
155
|
+
const res = await this.#fetch(`${this.baseUrl}/keys/${encodeURIComponent(keyId)}.pub`);
|
|
156
|
+
if (!res.ok) throw new Error(`registry: GET /keys/${keyId}.pub returned ${res.status}`);
|
|
157
|
+
return res.text();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async #getJson<T>(path: string): Promise<T> {
|
|
161
|
+
const res = await this.#fetch(`${this.baseUrl}${path}`, {
|
|
162
|
+
headers: { Accept: 'application/json' },
|
|
163
|
+
});
|
|
164
|
+
if (!res.ok) throw new Error(`registry: GET ${path} returned ${res.status}`);
|
|
165
|
+
return (await res.json()) as T;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async #getBinary(path: string): Promise<Buffer> {
|
|
169
|
+
const res = await this.#fetch(`${this.baseUrl}${path}`);
|
|
170
|
+
if (!res.ok) throw new Error(`registry: GET ${path} returned ${res.status}`);
|
|
171
|
+
return Buffer.from(await res.arrayBuffer());
|
|
172
|
+
}
|
|
173
|
+
}
|