Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/renderer/src/components/hark/HarkAskCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IconButton } from '@/components/ui/IconButton'
import { TextInput } from '@/components/ui/Field'
import { getBridge } from '@/lib/bridge'
import { cn } from '@/lib/cn'
import { secretFieldFromSchema } from '@/lib/harkSecretSchema'
import { useBotStore } from '@/store/botStore'

const DISMISSED_KEY = 'botark:hark-dismissed-asks'
Expand Down Expand Up @@ -168,16 +169,19 @@ export function HarkAskCards({ accountId, sessionId, asks, className = 'mx-5' }:
}
}

/** Like the login: save the key to the Hark vault, then hand its id to the waiting tool. Body shape inferred from the login form, not observed. */
/** Like the login: save the key to the Hark vault, then hand its id to the waiting tool.
* GOXF 2026-12-04: the field name comes from the form (screen input id), else Hark's own /secrets/screen schema — never guessed. */
const submitSecret = async (ask: HarkAskView) => {
const value = key[ask.id]?.trim()
if (!ask.secret || !ask.requestId || !value) return
setBusy(ask.id)
setError((e) => ({ ...e, [ask.id]: '' }))
try {
const type = ask.secret.type || 'api_key'
const field = ask.secret.field || await secretFieldFromSchema(hark.rpc, accountId, type, ask.secret.scope)
const saved = await hark.rpc({
accountId, op: 'call', method: 'POST', path: '/secrets',
body: { type: 'api_key', scope: ask.secret.scope, label: ask.secret.label, conversationId: sessionId, fields: { apiKey: value } }
body: { type, scope: ask.secret.scope, label: ask.secret.label, conversationId: sessionId, fields: { [field]: value } }
})
if (!saved.ok) throw new Error(saved.message || '保存失败')
const d = (saved.data ?? {}) as Record<string, unknown>
Expand Down
19 changes: 19 additions & 0 deletions src/renderer/src/lib/harkSecretSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it, vi } from 'vitest'
import { inputIdFromScreen, secretFieldFromSchema } from './harkSecretSchema'

const screen = { components: [{ type: 'instructions', text: 'x' }, { id: 'value', type: 'text_input', secret: true }] }

describe('harkSecretSchema', () => {
it('reads the input id from a screen, wrapped or bare', () => {
expect(inputIdFromScreen(screen)).toBe('value')
expect(inputIdFromScreen({ screen })).toBe('value')
expect(inputIdFromScreen({})).toBeUndefined()
})
it('asks /secrets/screen with type and scope, falls back to value', async () => {
const rpc = vi.fn().mockResolvedValue({ ok: true, data: { screen: { components: [{ id: 'token', type: 'text_input' }] } } })
expect(await secretFieldFromSchema(rpc, 'a', 'api_key', 'sctapi.ftqq.com')).toBe('token')
expect(rpc.mock.calls[0][0].path).toBe('/secrets/screen?type=api_key&scope=sctapi.ftqq.com')
expect(await secretFieldFromSchema(vi.fn().mockResolvedValue({ ok: false }), 'a', 'api_key')).toBe('value')
expect(await secretFieldFromSchema(vi.fn().mockRejectedValue(new Error('x')), 'a', 'api_key')).toBe('value')
})
})
20 changes: 20 additions & 0 deletions src/renderer/src/lib/harkSecretSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** GOXF 2026-12-04: Hark's web app asks GET /secrets/screen?type=…&scope=… for a secret's form; its input ids are the
* field names POST /secrets accepts (api_key → "value"). Used when an ask did not carry its own form. */
type Rpc = (req: { accountId: string; op: 'call'; method: 'GET'; path: string }) => Promise<{ ok: boolean; data?: unknown }>

const obj = (v: unknown): Record<string, unknown> => (v && typeof v === 'object' ? (v as Record<string, unknown>) : {})

export function inputIdFromScreen(data: unknown): string | undefined {
const d = obj(data)
const screen = obj(d['screen'] ?? d['data'] ?? d)
const comps = Array.isArray(screen['components']) ? (screen['components'] as unknown[]).map(obj) : []
const id = comps.find((c) => /input/i.test(String(c['type'] ?? '')))?.['id']
return typeof id === 'string' && /^[\w.-]{1,40}$/.test(id) ? id : undefined
}

export async function secretFieldFromSchema(rpc: Rpc, accountId: string, type: string, scope?: string): Promise<string> {
const q = new URLSearchParams({ type })
if (scope) q.set('scope', scope)
const res = await rpc({ accountId, op: 'call', method: 'GET', path: `/secrets/screen?${q.toString()}` }).catch(() => null)
return (res?.ok ? inputIdFromScreen(res.data) : undefined) ?? 'value'
}
5 changes: 3 additions & 2 deletions src/shared/harkAsks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ describe('GOXF: secure_screen key asks (2026-11-30)', () => {
it('an api_key screen gets the inline one-field form, its hint and its field label', () => {
const [ask] = pendingHarkAsks([screenAsk({ type: 'api_key', scope: 'bridge-dev.example.com' }, [
{ type: 'text', text: '只填链接 /mcp/ 后面那 40 位' },
{ type: 'input', id: 'apiKey', label: 'API Key' }
{ type: 'text_input', id: 'value', label: 'API Key', secret: true }
])])
expect(ask!.kind).toBe('credential')
expect(ask!.secret).toEqual({ scope: 'bridge-dev.example.com', label: 'bridge-dev 钥匙(40 位)', placeholder: 'API Key' })
// Real shape (user's frame log 2026-12-04): POST /secrets rejects any field but the form's own id "value".
expect(ask!.secret).toEqual({ scope: 'bridge-dev.example.com', label: 'bridge-dev 钥匙(40 位)', placeholder: 'API Key', type: 'api_key', field: 'value' })
expect(ask!.login).toBeUndefined()
expect(ask!.hint).toBe('只填链接 /mcp/ 后面那 40 位')
})
Expand Down
8 changes: 6 additions & 2 deletions src/shared/harkAsks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface HarkAskView {
/** Set when the ask is a site sign-in BotArk can fill natively (POST /secrets, then tool-response {secretId}). */
login?: { scope: string; label: string; fields: string[] }
/** Set when the ask wants one key / token (saved to the Hark vault as an api_key, then its id handed to the tool). */
secret?: { scope: string; label: string; placeholder?: string }
secret?: { scope: string; label: string; placeholder?: string; type?: string; field?: string }
/** Hark's own line under the title (where to find the value). */
hint?: string
/** secure_screen one-time code: saved to the vault like a login (type one_time_code), then its id handed over. */
Expand Down Expand Up @@ -84,10 +84,14 @@ export function secretSpec(a: RawAsk, kind: HarkAskKind): HarkAskView['secret']
if (/login|one_time_code|card|payment|cvv|cvc/i.test(screenType) || !/api[-_ ]?key|token|secret|key/i.test(screenType)) return undefined
const input = screenComponents(a).find((c) => /input/i.test(str(c['type'])))
const placeholder = input ? str(input['placeholder']) || str(input['label']) || undefined : undefined
// GOXF 2026-12-04: POST /secrets wants the form's own field id (Hark's api_key form uses "value"), not a guessed name.
const field = input ? str(input['id']) : ''
return {
scope: str(sc.secret['scope']) || sc.site || a.domain || '',
label: str(sc.secret['label']) || str(sc.secret['name']) || a.prompt || 'API key',
...(placeholder ? { placeholder } : {})
type: screenType,
...(placeholder ? { placeholder } : {}),
...(/^[\w.-]{1,40}$/.test(field) ? { field } : {})
}
}
const req = ((a.raw?.['request'] ?? a.raw?.['userInputRequest'] ?? a.raw) || {}) as Record<string, unknown>
Expand Down