Skip to content
dsh-invest 文档
Esc
↑↓navigate↵open⌘Jpreview
On this page

快照与详情

实现 snapshot 与 detail 切面:24 小时快照、资讯/公告/简况。

标的详情:资讯、简况与盘口。

标的详情:资讯、简况与盘口。

目标

/data/:venue/tickers 返回 24 小时快照;/detail/:venue/:ticker/:section 返回 news / announcements / profile。详情面板与 agent 读同一条流水线。

要改哪些文件

src/shared/manifest.ts   声明 facets.snapshot 或 facets.detail
src/host/snapshot.ts     read(port) → { tickers, asOf }
src/host/detail.ts       accepts / news / announcements / profile / live
src/shared/descriptor.ts  section 名、优先级、缓存说明
tests/conformance.spec.ts describeVenue + 详情 section 的用例

纯资讯源用 detail-only manifest:markets: [],只声明 detail。

契约

snapshot

snapshot: {
  caps: { markets: ['spot'] },
  async read(port) {
    const { body } = await port.fetchJson('https://example.invalid/ticker/24hr');
    return {
      asOf: Date.now(),
      tickers: (body as Row[]).map((row) => ({
        ticker: row.symbol,
        price: Number(row.lastPrice),
        changePct: Number(row.priceChangePercent),
        quoteVolume: Number(row.quoteVolume),
      })),
    };
  },
}

asOf 是拿到快照的时刻(epoch ms),不是上游行情时间。没有成交额时整个字段缺席,不要填 0。

detail

detail: {
  caps: { sections: ['news', 'profile'] },
  priority: 100,
  accepts: ({ venue, symbol }) => venue === 'demo' && symbol !== '',
  async news(query, ports) {
    const { status, body } = await ports.fetchJson(`https://example.invalid/news?code=${query.ticker}`);
    if (status === 404) return { ok: false, code: 'not-covered', message: '没有这只标的。' };
    if (status !== 200) return { ok: false, code: 'upstream-error', message: `HTTP ${status}`, retryable: true };
    return { ok: true, data: { items: toItems(body) }, asOf: ports.now() };
  },
}

accepts 收的是目标标的 { venue, symbol },纯函数,抛错按 false。caps.sections 必须与方法一一对应。

同一 section 多个 provider 时按 priority 升序回退,不合并。一个 section 失败不拖垮另外两个;可选段失败写 ProfileDocument.warnings,整体仍 ok: true。

失败与重试

  • 没有这个标的:not-covered。不要用 K 线编一份盘口,也不要把「没有简况」做成 error。
  • 上游 5xx / 限流:upstream-error / rate-limited,带 retryable。
  • 缓存 TTL 由宿主管:news 60 秒、announcements 5 分钟、profile 10 分钟;失败不缓存。
  • live 可选;返回句柄后由宿主关闭。REST provider 不要为了「看起来实时」自己轮询。

验证

curl -s http://127.0.0.1:<port>/dsh-invest/data/demo/tickers | jq '.tickers | length'
curl -s http://127.0.0.1:<port>/dsh-invest/detail/demo/600519/profile | jq '.ok'

预期:第一条大于 0;第二条是 true。没有 section 时返回 not-covered,不是 500。

查看某只标的实际命中了哪些详情源:

curl -s 'http://127.0.0.1:<port>/dsh-invest/actions/detail.providers' \
  -H 'content-type: application/json' \
  -d '{"args":{"venue":"demo","symbol":"600519"}}' | jq

预期按 priority 返回 provider 列表;空列表说明 accepts 没认领。

Was this page helpful?